Skip to content

Commit 52de30c

Browse files
committed
[Gardening] Do not store start/end line in SingleRawComment
The start and end lines were only used while constructing the comments, so move the line tracking into that method instead of storing it in each comment.
1 parent b15f77d commit 52de30c

File tree

6 files changed

+60
-77
lines changed

6 files changed

+60
-77
lines changed

include/swift/AST/RawComment.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ struct SingleRawComment {
3434
StringRef RawText;
3535

3636
unsigned Kind : 8;
37-
unsigned StartColumn : 16;
38-
unsigned StartLine;
39-
unsigned EndLine;
37+
unsigned ColumnIndent : 16;
4038

4139
SingleRawComment(CharSourceRange Range, const SourceManager &SourceMgr);
42-
SingleRawComment(StringRef RawText, unsigned StartColumn);
40+
SingleRawComment(StringRef RawText, unsigned ColumnIndent);
4341

4442
SingleRawComment(const SingleRawComment &) = default;
4543
SingleRawComment &operator=(const SingleRawComment &) = default;

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
641641
Lines.clear();
642642

643643
StringRef RawText = SRC.RawText.rtrim("\n\r");
644-
unsigned WhitespaceToTrim = SRC.StartColumn - 1;
644+
unsigned WhitespaceToTrim = SRC.ColumnIndent - 1;
645645
trimLeadingWhitespaceFromLines(RawText, WhitespaceToTrim, Lines);
646646

647647
for (auto Line : Lines) {

lib/AST/Module.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,10 +879,10 @@ SourceFile::getBasicLocsForDecl(const Decl *D) const {
879879
Result.SourceFilePath = SM.getDisplayNameForLoc(D->getLoc());
880880

881881
for (const auto &SRC : D->getRawComment(/*SerializedOK*/false).Comments) {
882-
Result.DocRanges.push_back(std::make_pair(
883-
SourcePosition { SRC.StartLine, SRC.StartColumn },
884-
SRC.Range.getByteLength())
885-
);
882+
auto LineAndCol = SM.getLineAndColumnInBuffer(SRC.Range.getStart());
883+
Result.DocRanges.push_back(
884+
std::make_pair(SourcePosition{LineAndCol.first, LineAndCol.second},
885+
SRC.Range.getByteLength()));
886886
}
887887

888888
auto setLineColumn = [&SM](SourcePosition &Home, SourceLoc Loc) {

lib/AST/RawComment.cpp

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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

10671
static 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
}

lib/Markup/LineList.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ LineList MarkupContext::getLineList(swift::RawComment RC) {
118118
unsigned NewlineBytes = swift::measureNewline(Cleaned);
119119
Cleaned = Cleaned.drop_front(NewlineBytes);
120120
CleanedStartLoc = CleanedStartLoc.getAdvancedLocOrInvalid(NewlineBytes);
121-
HasASCIIArt = measureASCIIArt(Cleaned, C.StartColumn - 1) != 0;
121+
HasASCIIArt = measureASCIIArt(Cleaned, C.ColumnIndent - 1) != 0;
122122
}
123123

124124
while (!Cleaned.empty()) {
@@ -129,7 +129,7 @@ LineList MarkupContext::getLineList(swift::RawComment RC) {
129129
// Skip over ASCII art, if present.
130130
if (HasASCIIArt)
131131
if (unsigned ASCIIArtBytes =
132-
measureASCIIArt(Cleaned, C.StartColumn - 1)) {
132+
measureASCIIArt(Cleaned, C.ColumnIndent - 1)) {
133133
Cleaned = Cleaned.drop_front(ASCIIArtBytes);
134134
CleanedStartLoc =
135135
CleanedStartLoc.getAdvancedLocOrInvalid(ASCIIArtBytes);

lib/Serialization/SerializeDoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class DeclCommentTableInfo {
247247
out << data.Brief;
248248
writer.write<uint32_t>(data.Raw.Comments.size());
249249
for (auto C : data.Raw.Comments) {
250-
writer.write<uint32_t>(C.StartColumn);
250+
writer.write<uint32_t>(C.ColumnIndent);
251251
writer.write<uint32_t>(C.RawText.size());
252252
out << C.RawText;
253253
}

0 commit comments

Comments
 (0)