r284477 - alpha.core.UnreachableCode - don't warn about unreachable code inside macro

Daniel Marjamaki via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 18 06:16:53 PDT 2016


Author: danielmarjamaki
Date: Tue Oct 18 08:16:53 2016
New Revision: 284477

URL: http://llvm.org/viewvc/llvm-project?rev=284477&view=rev
Log:
alpha.core.UnreachableCode - don't warn about unreachable code inside macro

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


Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
    cfe/trunk/test/Analysis/unreachable-code-path.c

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp?rev=284477&r1=284476&r2=284477&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp Tue Oct 18 08:16:53 2016
@@ -147,6 +147,14 @@ void UnreachableCodeChecker::checkEndAna
     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();

Modified: cfe/trunk/test/Analysis/unreachable-code-path.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/unreachable-code-path.c?rev=284477&r1=284476&r2=284477&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/unreachable-code-path.c (original)
+++ cfe/trunk/test/Analysis/unreachable-code-path.c Tue Oct 18 08:16:53 2016
@@ -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
+}
+




More information about the cfe-commits mailing list