]> granicus.if.org Git - clang/commitdiff
alpha.core.UnreachableCode - don't warn about unreachable code inside macro
authorDaniel Marjamaki <daniel.marjamaki@evidente.se>
Tue, 18 Oct 2016 13:16:53 +0000 (13:16 +0000)
committerDaniel Marjamaki <daniel.marjamaki@evidente.se>
Tue, 18 Oct 2016 13:16:53 +0000 (13:16 +0000)
In macros, 'do {...} while (0)' is often used. Don't warn about the condition 0 when it is unreachable.

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284477 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
test/Analysis/unreachable-code-path.c

index ff07a64d9b1f48fa823bd123aeb3433ab856db24..ccd8e9a18b00f42f885c8e3438ee1aa660ab3344 100644 (file)
@@ -147,6 +147,14 @@ void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
     PathDiagnosticLocation DL;
     SourceLocation SL;
     if (const Stmt *S = getUnreachableStmt(CB)) {
+      // In macros, 'do {...} while (0)' is often used. Don't warn about the
+      // condition 0 when it is unreachable.
+      if (S->getLocStart().isMacroID())
+        if (const auto *I = dyn_cast<IntegerLiteral>(S))
+          if (I->getValue() == 0ULL)
+            if (const Stmt *Parent = PM->getParent(S))
+              if (isa<DoStmt>(Parent))
+                continue;
       SR = S->getSourceRange();
       DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
       SL = DL.asLocation();
index 975c9887e4523182286bd0b8d3f2bfe6dd7ce61d..4ddfb21f0a0f3dbfbb73d6c92faf9196a36ff4f3 100644 (file)
@@ -206,3 +206,10 @@ void test13(int i) {
   int x = inlineFunction(i);
   x && x < 10; // no-warning
 }
+
+// Don't warn in a macro
+#define RETURN(X)  do { return; } while (0)
+void macro(void) {
+  RETURN(1); // no-warning
+}
+