diff --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp index 49718962f62f1..1b690e9f3db67 100644 --- a/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp +++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp @@ -78,11 +78,16 @@ SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, std::optional findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts) { - std::optional CurrentToken; - do { - CurrentToken = Lexer::findNextToken(Start, SM, LangOpts); - } while (CurrentToken && CurrentToken->is(tok::comment)); - return CurrentToken; + while (Start.isValid()) { + std::optional CurrentToken = + Lexer::findNextToken(Start, SM, LangOpts); + if (!CurrentToken || !CurrentToken->is(tok::comment)) + return CurrentToken; + + Start = CurrentToken->getLocation(); + } + + return std::nullopt; } bool rangeContainsExpansionsOrDirectives(SourceRange Range, @@ -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; @@ -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;