@@ -61,71 +61,57 @@ SingleRawComment::SingleRawComment(CharSourceRange Range,
61
61
const SourceManager &SourceMgr)
62
62
: Range(Range), RawText(SourceMgr.extractText(Range)),
63
63
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 ;
69
65
}
70
66
71
- SingleRawComment::SingleRawComment (StringRef RawText, unsigned StartColumn )
67
+ SingleRawComment::SingleRawComment (StringRef RawText, unsigned ColumnIndent )
72
68
: 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) {}
105
70
106
71
static RawComment toRawComment (ASTContext &Context, CharSourceRange Range) {
107
72
if (Range.isInvalid ())
108
73
return RawComment ();
109
74
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);
114
79
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
+
120
84
SmallVector<SingleRawComment, 16 > Comments;
121
85
Token Tok;
86
+ unsigned LastEnd = 0 ;
122
87
while (true ) {
123
88
L.lex (Tok);
124
89
if (Tok.is (tok::eof))
125
90
break ;
126
91
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
+ }
128
113
}
114
+
129
115
RawComment Result;
130
116
Result.Comments = Context.AllocateCopy (Comments);
131
117
return Result;
@@ -159,25 +145,24 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
159
145
switch (Unit->getKind ()) {
160
146
case FileUnitKind::SerializedAST: {
161
147
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 ;
174
159
}
175
- auto RC = RawComment (Context. AllocateCopy ( llvm::makeArrayRef (SRCs)));
160
+ }
176
161
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;
181
166
}
182
167
}
183
168
}
0 commit comments