Skip to content

Commit

Permalink
[clang-tidy] Fix findNextTokenSkippingComments & rangeContainsExpansi…
Browse files Browse the repository at this point in the history
…onsOrDirectives

findNextTokenSkippingComments is actually a endless loop,
implementing it correctly.
rangeContainsExpansionsOrDirectives were skiping every second
token, if there were no whitespaces bettwen tokens.

Reviewed By: carlosgalvezp

Differential Revision: https://reviews.llvm.org/D146881
  • Loading branch information
PiotrZSL committed Apr 2, 2023
1 parent ebb41f7 commit d79715b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM,
std::optional<Token>
findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM,
const LangOptions &LangOpts) {
std::optional<Token> CurrentToken;
do {
CurrentToken = Lexer::findNextToken(Start, SM, LangOpts);
} while (CurrentToken && CurrentToken->is(tok::comment));
return CurrentToken;
while (Start.isValid()) {
std::optional<Token> CurrentToken =
Lexer::findNextToken(Start, SM, LangOpts);
if (!CurrentToken || !CurrentToken->is(tok::comment))
return CurrentToken;

Start = CurrentToken->getLocation();
}

return std::nullopt;
}

bool rangeContainsExpansionsOrDirectives(SourceRange Range,
Expand All @@ -91,7 +96,7 @@ bool rangeContainsExpansionsOrDirectives(SourceRange Range,
assert(Range.isValid() && "Invalid Range for relexing provided");
SourceLocation Loc = Range.getBegin();

while (Loc < Range.getEnd()) {
while (Loc <= Range.getEnd()) {
if (Loc.isMacroID())
return true;

Expand All @@ -103,7 +108,7 @@ bool rangeContainsExpansionsOrDirectives(SourceRange Range,
if (Tok->is(tok::hash))
return true;

Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts).getLocWithOffset(1);
Loc = Tok->getLocation();
}

return false;
Expand Down

0 comments on commit d79715b

Please sign in to comment.