Skip to content

Commit 421f585

Browse files
committed
Address review feedback
- Refactored some functionality with the help of clang-tidy - Added comments to the complicated lexing functions - Renamed variables to follow LLVM/Clang conventions
1 parent 4432d32 commit 421f585

File tree

1 file changed

+71
-47
lines changed

1 file changed

+71
-47
lines changed

clang/lib/AST/CommentParser.cpp

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class TextTokenRetokenizer {
7878
char peekNext(unsigned offset) const {
7979
assert(!isEnd());
8080
assert(Pos.BufferPtr != Pos.BufferEnd);
81-
if (Pos.BufferPtr + offset <= Pos.BufferEnd) {
81+
if (Pos.BufferPtr + offset < Pos.BufferEnd) {
8282
return *(Pos.BufferPtr + offset);
8383
} else {
8484
return '\0';
@@ -108,7 +108,7 @@ class TextTokenRetokenizer {
108108
}
109109
}
110110

111-
bool continueInt(SmallString<32> &NextToken) {
111+
bool shouldContinueLexingIntegralType(SmallString<32> &NextToken) {
112112
return NextToken.ends_with(StringRef("char")) ||
113113
NextToken.ends_with(StringRef("int")) ||
114114
NextToken.ends_with(StringRef("char*")) ||
@@ -117,39 +117,44 @@ class TextTokenRetokenizer {
117117
NextToken.ends_with(StringRef("int&"));
118118
}
119119

120-
bool lexInt(SmallString<32> &WordText, SmallString<32> &NextToken) {
120+
/// Lex an integral type, such as unsigned long long, etc.
121+
bool lexIntegral(SmallString<32> &WordText, SmallString<32> &NextToken) {
121122
unsigned LongCounter = (WordText.ends_with(StringRef("long"))) ? 1 : 0;
122-
bool complete = false;
123+
bool IsLexingComplete = false;
123124

124125
while (!isEnd()) {
125126
const char C = peek();
126127
if (!isWhitespace(C)) {
127128
WordText.push_back(C);
128129
consumeChar();
129130
} else {
130-
131131
NextToken.clear();
132132
peekNextToken(NextToken);
133133

134134
if (WordText.ends_with(StringRef("long"))) {
135135
LongCounter++;
136-
if (continueInt(NextToken)) {
136+
// Use the next token to determine if we should continue parsing
137+
if (shouldContinueLexingIntegralType(NextToken)) {
137138
WordText.push_back(C);
138139
consumeChar();
139-
complete = true;
140+
IsLexingComplete = true;
140141
continue;
141-
} else {
142-
if (LongCounter == 2) {
143-
return true;
144-
}
145142
}
146-
} else {
143+
// Maximum number of consecutive "long" is 2, so we can return if
144+
// we've hit that.
145+
if (LongCounter == 2) {
146+
return true;
147+
}
148+
}
147149

148-
if (complete || continueInt(WordText)) {
150+
// If current word doesn't end with long, check if we should exit early
151+
else {
152+
if (IsLexingComplete || shouldContinueLexingIntegralType(WordText)) {
149153
return true;
150154
}
151155
}
152156

157+
// If next token ends with long then we consume it and continue parsing
153158
if (NextToken.ends_with(StringRef("long"))) {
154159
WordText.push_back(C);
155160
consumeChar();
@@ -206,7 +211,7 @@ class TextTokenRetokenizer {
206211
return WordText.ends_with(StringRef("::"));
207212
}
208213

209-
bool isInt(SmallString<32> &WordText) {
214+
bool isIntegral(SmallString<32> &WordText) {
210215
return WordText.ends_with(StringRef("unsigned")) ||
211216
WordText.ends_with(StringRef("long")) ||
212217
WordText.ends_with(StringRef("signed"));
@@ -280,7 +285,12 @@ class TextTokenRetokenizer {
280285
bool lexType(Token &Tok) {
281286
if (isEnd())
282287
return false;
288+
289+
// Save current position in case we need to rollback because the type is
290+
// empty.
283291
Position SavedPos = Pos;
292+
293+
// Consume any leading whitespace.
284294
consumeWhitespace();
285295
SmallString<32> NextToken;
286296
SmallString<32> WordText;
@@ -289,10 +299,12 @@ class TextTokenRetokenizer {
289299
StringRef ConstVal = StringRef("const");
290300
StringRef PointerVal = StringRef("*");
291301
StringRef ReferenceVal = StringRef("&");
292-
bool ConstPointer = false;
302+
bool IsTypeConstPointerOrRef = false;
293303

294304
while (!isEnd()) {
295305
const char C = peek();
306+
// For non-whitespace characters we check if it's a template or otherwise
307+
// continue reading the text into a word.
296308
if (!isWhitespace(C)) {
297309
if (C == '<') {
298310
if (!lexTemplate(WordText))
@@ -301,47 +313,59 @@ class TextTokenRetokenizer {
301313
WordText.push_back(C);
302314
consumeChar();
303315
}
304-
} else {
305-
if (ConstPointer) {
316+
}
317+
// For whitespace, we start inspecting the constructed word
318+
else {
319+
// If we encounter a pointer/reference, we can stop parsing since we're
320+
// only parsing expressions.
321+
if (IsTypeConstPointerOrRef) {
306322
consumeChar();
307323
break;
308-
} else {
309-
if (isInt(WordText)) {
310-
WordText.push_back(C);
311-
consumeChar();
312-
if (!lexInt(WordText, NextToken))
313-
return false;
314-
}
315-
if (continueParsing(WordText)) {
316-
WordText.push_back(C);
317-
consumeChar();
324+
}
325+
// Parse out integral types
326+
if (isIntegral(WordText)) {
327+
WordText.push_back(C);
328+
consumeChar();
329+
if (!lexIntegral(WordText, NextToken))
330+
return false;
331+
}
332+
// Certain types, like qualified names or types with CVR to name a few,
333+
// may have whitespace inside of the typename, so we need to check and
334+
// continue parsing if that's the case
335+
if (continueParsing(WordText)) {
336+
WordText.push_back(C);
337+
consumeChar();
338+
}
339+
// Handles cases without qualified names or type qualifiers
340+
else {
341+
NextToken.clear();
342+
peekNextToken(NextToken);
343+
// Check for pointer/ref vals, and mark the type as a pointer/ref for
344+
// the rest of the lex
345+
if (WordText.ends_with(PointerVal) ||
346+
WordText.ends_with(ReferenceVal)) {
347+
if (NextToken.equals(ConstVal)) {
348+
IsTypeConstPointerOrRef = true;
349+
WordText.push_back(C);
350+
consumeChar();
351+
} else {
352+
consumeChar();
353+
break;
354+
}
318355
} else {
319-
NextToken.clear();
320-
peekNextToken(NextToken);
321-
if (WordText.ends_with(PointerVal) ||
322-
WordText.ends_with(ReferenceVal)) {
323-
if (NextToken.equals(ConstVal)) {
324-
ConstPointer = true;
356+
// Check if the next token is a pointer/ref
357+
if ((NextToken.ends_with(PointerVal) ||
358+
NextToken.ends_with(ReferenceVal))) {
359+
WordText.push_back(C);
360+
consumeChar();
361+
} else {
362+
if (continueParsing(NextToken)) {
325363
WordText.push_back(C);
326364
consumeChar();
327365
} else {
328366
consumeChar();
329367
break;
330368
}
331-
} else {
332-
if ((NextToken.ends_with(PointerVal) ||
333-
NextToken.ends_with(ReferenceVal))) {
334-
WordText.push_back(C);
335-
consumeChar();
336-
} else {
337-
if (continueParsing(NextToken)) {
338-
WordText.push_back(C);
339-
consumeChar();
340-
} else {
341-
consumeChar();
342-
break;
343-
}
344-
}
345369
}
346370
}
347371
}

0 commit comments

Comments
 (0)