From fa1bf447d904bdcbe8e78812d27f50c580e49c43 Mon Sep 17 00:00:00 2001 From: Daniel Marjamaki Date: Tue, 18 Oct 2016 13:16:53 +0000 Subject: [PATCH] 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 llvm-svn: 284477 --- clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp | 8 ++++++++ clang/test/Analysis/unreachable-code-path.c | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp index ff07a64..ccd8e9a 100644 --- a/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp @@ -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(S)) + if (I->getValue() == 0ULL) + if (const Stmt *Parent = PM->getParent(S)) + if (isa(Parent)) + continue; SR = S->getSourceRange(); DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC); SL = DL.asLocation(); diff --git a/clang/test/Analysis/unreachable-code-path.c b/clang/test/Analysis/unreachable-code-path.c index 975c988..4ddfb21 100644 --- a/clang/test/Analysis/unreachable-code-path.c +++ b/clang/test/Analysis/unreachable-code-path.c @@ -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 +} + -- 2.7.4