[PATCH] D64607: [clang-tidy] Fix crash on end location inside macro

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rL366353: [clang-tidy] Fix crash on end location inside macro (authored by Nathan-Huckleberry, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D64607?vs=209524&id=210364#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64607/new/

https://reviews.llvm.org/D64607

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


Index: clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/bugprone/BranchCloneCheck.cpp
@@ -132,9 +132,12 @@
           // 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 @@
 
         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;
     }
Index: clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c
===================================================================
--- clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c
+++ clang-tools-extra/trunk/test/clang-tidy/bugprone-branch-clone-macro-crash.c
@@ -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)
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64607.210364.patch
Type: text/x-patch
Size: 2345 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190717/40924abb/attachment-0001.bin>


More information about the cfe-commits mailing list