[PATCH] D146881: [clang-tidy] Fix findNextTokenSkippingComments & rangeContainsExpansionsOrDirectives

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 25 11:42:23 PDT 2023


PiotrZSL created this revision.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a reviewer: njames93.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146881

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


Index: clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
+++ clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
@@ -78,11 +78,16 @@
 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 @@
   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 @@
     if (Tok->is(tok::hash))
       return true;
 
-    Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts).getLocWithOffset(1);
+    Loc = Tok->getLocation();
   }
 
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146881.508331.patch
Type: text/x-patch
Size: 1414 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230325/8b099bd3/attachment.bin>


More information about the cfe-commits mailing list