@@ -61,71 +61,57 @@ SingleRawComment::SingleRawComment(CharSourceRange Range,
6161 const SourceManager &SourceMgr)
6262 : Range(Range), RawText(SourceMgr.extractText(Range)),
6363 Kind(static_cast <unsigned >(getCommentKind(RawText))) {
64- auto StartLineAndColumn =
65- SourceMgr.getLineAndColumnInBuffer (Range.getStart ());
66- StartLine = StartLineAndColumn.first ;
67- StartColumn = StartLineAndColumn.second ;
68- EndLine = SourceMgr.getLineAndColumnInBuffer (Range.getEnd ()).first ;
64+ ColumnIndent = SourceMgr.getLineAndColumnInBuffer (Range.getStart ()).second ;
6965}
7066
71- SingleRawComment::SingleRawComment (StringRef RawText, unsigned StartColumn )
67+ SingleRawComment::SingleRawComment (StringRef RawText, unsigned ColumnIndent )
7268 : RawText(RawText), Kind(static_cast <unsigned >(getCommentKind(RawText))),
73- StartColumn(StartColumn), StartLine(0 ), EndLine(0 ) {}
74-
75- static void addCommentToList (SmallVectorImpl<SingleRawComment> &Comments,
76- const SingleRawComment &SRC) {
77- // TODO: consider producing warnings when we decide not to merge comments.
78-
79- if (SRC.isOrdinary ()) {
80- // Skip gyb comments that are line number markers.
81- if (SRC.RawText .startswith (" // ###" ))
82- return ;
83-
84- Comments.clear ();
85- return ;
86- }
87-
88- // If this is the first documentation comment, save it (because there isn't
89- // anything to merge it with).
90- if (Comments.empty ()) {
91- Comments.push_back (SRC);
92- return ;
93- }
94-
95- auto &Last = Comments.back ();
96-
97- // Merge comments if they are on same or consecutive lines.
98- if (Last.EndLine + 1 < SRC.StartLine ) {
99- Comments.clear ();
100- return ;
101- }
102-
103- Comments.push_back (SRC);
104- }
69+ ColumnIndent(ColumnIndent) {}
10570
10671static RawComment toRawComment (ASTContext &Context, CharSourceRange Range) {
10772 if (Range.isInvalid ())
10873 return RawComment ();
10974
110- auto &SourceMgr = Context.SourceMgr ;
111- unsigned BufferID = SourceMgr .findBufferContainingLoc (Range.getStart ());
112- unsigned Offset = SourceMgr .getLocOffsetInBuffer (Range.getStart (), BufferID);
113- unsigned EndOffset = SourceMgr .getLocOffsetInBuffer (Range.getEnd (), BufferID);
75+ auto &SM = Context.SourceMgr ;
76+ unsigned BufferID = SM .findBufferContainingLoc (Range.getStart ());
77+ unsigned Offset = SM .getLocOffsetInBuffer (Range.getStart (), BufferID);
78+ unsigned EndOffset = SM .getLocOffsetInBuffer (Range.getEnd (), BufferID);
11479 LangOptions FakeLangOpts;
115- Lexer L (FakeLangOpts, SourceMgr, BufferID, nullptr , LexerMode::Swift,
116- HashbangMode::Disallowed,
117- CommentRetentionMode::ReturnAsTokens,
118- TriviaRetentionMode::WithoutTrivia,
119- Offset, EndOffset);
80+ Lexer L (FakeLangOpts, SM, BufferID, nullptr , LexerMode::Swift,
81+ HashbangMode::Disallowed, CommentRetentionMode::ReturnAsTokens,
82+ TriviaRetentionMode::WithoutTrivia, Offset, EndOffset);
83+
12084 SmallVector<SingleRawComment, 16 > Comments;
12185 Token Tok;
86+ unsigned LastEnd = 0 ;
12287 while (true ) {
12388 L.lex (Tok);
12489 if (Tok.is (tok::eof))
12590 break ;
12691 assert (Tok.is (tok::comment));
127- addCommentToList (Comments, SingleRawComment (Tok.getRange (), SourceMgr));
92+
93+ auto SRC = SingleRawComment (Tok.getRange (), SM);
94+ if (SRC.isOrdinary ()) {
95+ // Skip gyb comments that are line number markers.
96+ if (!SRC.RawText .startswith (" // ###" )) {
97+ Comments.clear ();
98+ LastEnd = 0 ;
99+ }
100+ continue ;
101+ }
102+
103+ // Merge comments if they are on same or consecutive lines.
104+ unsigned Start =
105+ SM.getLineAndColumnInBuffer (Tok.getRange ().getStart ()).first ;
106+ if (LastEnd > 0 && LastEnd + 1 < Start) {
107+ Comments.clear ();
108+ LastEnd = 0 ;
109+ } else {
110+ Comments.push_back (SRC);
111+ LastEnd = SM.getLineAndColumnInBuffer (Tok.getRange ().getEnd ()).first ;
112+ }
128113 }
114+
129115 RawComment Result;
130116 Result.Comments = Context.AllocateCopy (Comments);
131117 return Result;
@@ -159,25 +145,24 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
159145 switch (Unit->getKind ()) {
160146 case FileUnitKind::SerializedAST: {
161147 if (SerializedOK) {
162- if (const auto *CachedLocs = getSerializedLocs ()) {
163- if (!CachedLocs->DocRanges .empty ()) {
164- SmallVector<SingleRawComment, 4 > SRCs;
165- for (const auto &Range : CachedLocs->DocRanges ) {
166- if (Range.isValid ()) {
167- SRCs.push_back ({ Range, Context.SourceMgr });
168- } else {
169- // if we've run into an invalid range, don't bother trying to load
170- // any of the other comments
171- SRCs.clear ();
172- break ;
173- }
148+ auto *CachedLocs = getSerializedLocs ();
149+ if (!CachedLocs->DocRanges .empty ()) {
150+ SmallVector<SingleRawComment, 4 > SRCs;
151+ for (const auto &Range : CachedLocs->DocRanges ) {
152+ if (Range.isValid ()) {
153+ SRCs.push_back ({Range, Context.SourceMgr });
154+ } else {
155+ // if we've run into an invalid range, don't bother trying to load
156+ // any of the other comments
157+ SRCs.clear ();
158+ break ;
174159 }
175- auto RC = RawComment (Context. AllocateCopy ( llvm::makeArrayRef (SRCs)));
160+ }
176161
177- if (!RC. isEmpty ()) {
178- Context.setRawComment ( this , RC, true );
179- return RC ;
180- }
162+ if (!SRCs. empty ()) {
163+ auto RC = RawComment ( Context.AllocateCopy ( llvm::makeArrayRef (SRCs)) );
164+ Context. setRawComment ( this , RC, true ) ;
165+ return RC;
181166 }
182167 }
183168 }
0 commit comments