diff --git a/include/swift/AST/RawComment.h b/include/swift/AST/RawComment.h index 5864c20e412..745462aa0cb 100644 --- a/include/swift/AST/RawComment.h +++ b/include/swift/AST/RawComment.h @@ -34,12 +34,10 @@ struct SingleRawComment { StringRef RawText; unsigned Kind : 8; - unsigned StartColumn : 16; - unsigned StartLine; - unsigned EndLine; + unsigned ColumnIndent : 16; SingleRawComment(CharSourceRange Range, const SourceManager &SourceMgr); - SingleRawComment(StringRef RawText, unsigned StartColumn); + SingleRawComment(StringRef RawText, unsigned ColumnIndent); SingleRawComment(const SingleRawComment &) = default; SingleRawComment &operator=(const SingleRawComment &) = default; diff --git a/lib/AST/ASTPrinter.cpp b/lib/AST/ASTPrinter.cpp index 87a0e8f0076..4a06736a5ef 100644 --- a/lib/AST/ASTPrinter.cpp +++ b/lib/AST/ASTPrinter.cpp @@ -641,7 +641,7 @@ class PrintAST : public ASTVisitor { Lines.clear(); StringRef RawText = SRC.RawText.rtrim("\n\r"); - unsigned WhitespaceToTrim = SRC.StartColumn - 1; + unsigned WhitespaceToTrim = SRC.ColumnIndent - 1; trimLeadingWhitespaceFromLines(RawText, WhitespaceToTrim, Lines); for (auto Line : Lines) { diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index 11e417914c9..96de79c3109 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -879,10 +879,10 @@ SourceFile::getBasicLocsForDecl(const Decl *D) const { Result.SourceFilePath = SM.getDisplayNameForLoc(D->getLoc()); for (const auto &SRC : D->getRawComment(/*SerializedOK*/false).Comments) { - Result.DocRanges.push_back(std::make_pair( - SourcePosition { SRC.StartLine, SRC.StartColumn }, - SRC.Range.getByteLength()) - ); + auto LineAndCol = SM.getLineAndColumnInBuffer(SRC.Range.getStart()); + Result.DocRanges.push_back( + std::make_pair(SourcePosition{LineAndCol.first, LineAndCol.second}, + SRC.Range.getByteLength())); } auto setLineColumn = [&SM](SourcePosition &Home, SourceLoc Loc) { diff --git a/lib/AST/RawComment.cpp b/lib/AST/RawComment.cpp index 1be278bcc8b..b1f945a345a 100644 --- a/lib/AST/RawComment.cpp +++ b/lib/AST/RawComment.cpp @@ -61,71 +61,57 @@ SingleRawComment::SingleRawComment(CharSourceRange Range, const SourceManager &SourceMgr) : Range(Range), RawText(SourceMgr.extractText(Range)), Kind(static_cast(getCommentKind(RawText))) { - auto StartLineAndColumn = - SourceMgr.getLineAndColumnInBuffer(Range.getStart()); - StartLine = StartLineAndColumn.first; - StartColumn = StartLineAndColumn.second; - EndLine = SourceMgr.getLineAndColumnInBuffer(Range.getEnd()).first; + ColumnIndent = SourceMgr.getLineAndColumnInBuffer(Range.getStart()).second; } -SingleRawComment::SingleRawComment(StringRef RawText, unsigned StartColumn) +SingleRawComment::SingleRawComment(StringRef RawText, unsigned ColumnIndent) : RawText(RawText), Kind(static_cast(getCommentKind(RawText))), - StartColumn(StartColumn), StartLine(0), EndLine(0) {} - -static void addCommentToList(SmallVectorImpl &Comments, - const SingleRawComment &SRC) { - // TODO: consider producing warnings when we decide not to merge comments. - - if (SRC.isOrdinary()) { - // Skip gyb comments that are line number markers. - if (SRC.RawText.startswith("// ###")) - return; - - Comments.clear(); - return; - } - - // If this is the first documentation comment, save it (because there isn't - // anything to merge it with). - if (Comments.empty()) { - Comments.push_back(SRC); - return; - } - - auto &Last = Comments.back(); - - // Merge comments if they are on same or consecutive lines. - if (Last.EndLine + 1 < SRC.StartLine) { - Comments.clear(); - return; - } - - Comments.push_back(SRC); -} + ColumnIndent(ColumnIndent) {} static RawComment toRawComment(ASTContext &Context, CharSourceRange Range) { if (Range.isInvalid()) return RawComment(); - auto &SourceMgr = Context.SourceMgr; - unsigned BufferID = SourceMgr.findBufferContainingLoc(Range.getStart()); - unsigned Offset = SourceMgr.getLocOffsetInBuffer(Range.getStart(), BufferID); - unsigned EndOffset = SourceMgr.getLocOffsetInBuffer(Range.getEnd(), BufferID); + auto &SM = Context.SourceMgr; + unsigned BufferID = SM.findBufferContainingLoc(Range.getStart()); + unsigned Offset = SM.getLocOffsetInBuffer(Range.getStart(), BufferID); + unsigned EndOffset = SM.getLocOffsetInBuffer(Range.getEnd(), BufferID); LangOptions FakeLangOpts; - Lexer L(FakeLangOpts, SourceMgr, BufferID, nullptr, LexerMode::Swift, - HashbangMode::Disallowed, - CommentRetentionMode::ReturnAsTokens, - TriviaRetentionMode::WithoutTrivia, - Offset, EndOffset); + Lexer L(FakeLangOpts, SM, BufferID, nullptr, LexerMode::Swift, + HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens, + TriviaRetentionMode::WithoutTrivia, Offset, EndOffset); + SmallVector Comments; Token Tok; + unsigned LastEnd = 0; while (true) { L.lex(Tok); if (Tok.is(tok::eof)) break; assert(Tok.is(tok::comment)); - addCommentToList(Comments, SingleRawComment(Tok.getRange(), SourceMgr)); + + auto SRC = SingleRawComment(Tok.getRange(), SM); + if (SRC.isOrdinary()) { + // Skip gyb comments that are line number markers. + if (!SRC.RawText.startswith("// ###")) { + Comments.clear(); + LastEnd = 0; + } + continue; + } + + // Merge comments if they are on same or consecutive lines. + unsigned Start = + SM.getLineAndColumnInBuffer(Tok.getRange().getStart()).first; + if (LastEnd > 0 && LastEnd + 1 < Start) { + Comments.clear(); + LastEnd = 0; + } else { + Comments.push_back(SRC); + LastEnd = SM.getLineAndColumnInBuffer(Tok.getRange().getEnd()).first; + } } + RawComment Result; Result.Comments = Context.AllocateCopy(Comments); return Result; @@ -159,25 +145,24 @@ RawComment Decl::getRawComment(bool SerializedOK) const { switch (Unit->getKind()) { case FileUnitKind::SerializedAST: { if (SerializedOK) { - if (const auto *CachedLocs = getSerializedLocs()) { - if (!CachedLocs->DocRanges.empty()) { - SmallVector SRCs; - for (const auto &Range : CachedLocs->DocRanges) { - if (Range.isValid()) { - SRCs.push_back({ Range, Context.SourceMgr }); - } else { - // if we've run into an invalid range, don't bother trying to load - // any of the other comments - SRCs.clear(); - break; - } + auto *CachedLocs = getSerializedLocs(); + if (!CachedLocs->DocRanges.empty()) { + SmallVector SRCs; + for (const auto &Range : CachedLocs->DocRanges) { + if (Range.isValid()) { + SRCs.push_back({Range, Context.SourceMgr}); + } else { + // if we've run into an invalid range, don't bother trying to load + // any of the other comments + SRCs.clear(); + break; } - auto RC = RawComment(Context.AllocateCopy(llvm::makeArrayRef(SRCs))); + } - if (!RC.isEmpty()) { - Context.setRawComment(this, RC, true); - return RC; - } + if (!SRCs.empty()) { + auto RC = RawComment(Context.AllocateCopy(llvm::makeArrayRef(SRCs))); + Context.setRawComment(this, RC, true); + return RC; } } } diff --git a/lib/Markup/LineList.cpp b/lib/Markup/LineList.cpp index 38e98b1b071..0c6e77e7bf2 100644 --- a/lib/Markup/LineList.cpp +++ b/lib/Markup/LineList.cpp @@ -118,7 +118,7 @@ LineList MarkupContext::getLineList(swift::RawComment RC) { unsigned NewlineBytes = swift::measureNewline(Cleaned); Cleaned = Cleaned.drop_front(NewlineBytes); CleanedStartLoc = CleanedStartLoc.getAdvancedLocOrInvalid(NewlineBytes); - HasASCIIArt = measureASCIIArt(Cleaned, C.StartColumn - 1) != 0; + HasASCIIArt = measureASCIIArt(Cleaned, C.ColumnIndent - 1) != 0; } while (!Cleaned.empty()) { @@ -129,7 +129,7 @@ LineList MarkupContext::getLineList(swift::RawComment RC) { // Skip over ASCII art, if present. if (HasASCIIArt) if (unsigned ASCIIArtBytes = - measureASCIIArt(Cleaned, C.StartColumn - 1)) { + measureASCIIArt(Cleaned, C.ColumnIndent - 1)) { Cleaned = Cleaned.drop_front(ASCIIArtBytes); CleanedStartLoc = CleanedStartLoc.getAdvancedLocOrInvalid(ASCIIArtBytes); diff --git a/lib/Serialization/SerializeDoc.cpp b/lib/Serialization/SerializeDoc.cpp index 1f8d25bdfd7..f4749686bcf 100644 --- a/lib/Serialization/SerializeDoc.cpp +++ b/lib/Serialization/SerializeDoc.cpp @@ -247,7 +247,7 @@ public: out << data.Brief; writer.write(data.Raw.Comments.size()); for (auto C : data.Raw.Comments) { - writer.write(C.StartColumn); + writer.write(C.ColumnIndent); writer.write(C.RawText.size()); out << C.RawText; }