[clang-tools-extra] d79715b - [clang-tidy] Fix findNextTokenSkippingComments & rangeContainsExpansionsOrDirectives

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 2 09:05:24 PDT 2023


Author: Piotr Zegar
Date: 2023-04-02T16:05:15Z
New Revision: d79715b6a09c538ac60a8259d540a94d17fbc824

URL: https://github.com/llvm/llvm-project/commit/d79715b6a09c538ac60a8259d540a94d17fbc824
DIFF: https://github.com/llvm/llvm-project/commit/d79715b6a09c538ac60a8259d540a94d17fbc824.diff

LOG: [clang-tidy] Fix findNextTokenSkippingComments & rangeContainsExpansionsOrDirectives

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/utils/LexerUtils.cpp

Removed: 
    


################################################################################
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<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,
@@ -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;


        


More information about the cfe-commits mailing list