[clang-tools-extra] r366353 - [clang-tidy] Fix crash on end location inside macro

Nathan Huckleberry via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 10:22:43 PDT 2019


Author: nathan-huckleberry
Date: Wed Jul 17 10:22:43 2019
New Revision: 366353

URL: http://llvm.org/viewvc/llvm-project?rev=366353&view=rev
Log:
[clang-tidy] Fix crash on end location inside macro

Summary:
Lexer::getLocForEndOfToken is defined to return an
invalid location if the given location is inside a macro.
Other checks conditionally warn based off location
validity. Updating this check to do the same.

Reviewers: JonasToth, aaron.ballman, nickdesaulniers

Reviewed By: nickdesaulniers

Subscribers: lebedev.ri, nickdesaulniers, xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64607

Added:
    clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c
Modified:
    clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp?rev=366353&r1=366352&r2=366353&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp Wed Jul 17 10:22:43 2019
@@ -132,9 +132,12 @@ void BranchCloneCheck::check(const Match
           // We report the first occurence only when we find the second one.
           diag(Branches[i]->getBeginLoc(),
                "repeated branch in conditional chain");
-          diag(Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
-                                          *Result.SourceManager, getLangOpts()),
-               "end of the original", DiagnosticIDs::Note);
+          SourceLocation End =
+              Lexer::getLocForEndOfToken(Branches[i]->getEndLoc(), 0,
+                                         *Result.SourceManager, getLangOpts());
+          if (End.isValid()) {
+            diag(End, "end of the original", DiagnosticIDs::Note);
+          }
         }
 
         diag(Branches[j]->getBeginLoc(), "clone %0 starts here",
@@ -208,10 +211,12 @@ void BranchCloneCheck::check(const Match
 
         if (EndLoc.isMacroID())
           EndLoc = Context.getSourceManager().getExpansionLoc(EndLoc);
+        EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
+                                            getLangOpts());
 
-        diag(Lexer::getLocForEndOfToken(EndLoc, 0, *Result.SourceManager,
-                                        getLangOpts()),
-             "last of these clones ends here", DiagnosticIDs::Note);
+        if (EndLoc.isValid()) {
+          diag(EndLoc, "last of these clones ends here", DiagnosticIDs::Note);
+        }
       }
       BeginCurrent = EndCurrent;
     }

Added: clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c?rev=366353&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c (added)
+++ clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c Wed Jul 17 10:22:43 2019
@@ -0,0 +1,14 @@
+// RUN: %check_clang_tidy %s bugprone-branch-clone %t
+int x = 0;
+int y = 1;
+#define a(b, c) \
+  typeof(b) d;  \
+  if (b)        \
+    d = b;      \
+  else if (c)   \
+    d = b;
+
+f() {
+  // CHECK-MESSAGES: warning: repeated branch in conditional chain [bugprone-branch-clone]
+  a(x, y)
+}




More information about the cfe-commits mailing list