From: Daniel Marjamaki Date: Tue, 18 Oct 2016 13:16:53 +0000 (+0000) Subject: alpha.core.UnreachableCode - don't warn about unreachable code inside macro X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a56684f9d508e1d4c80eb238f712762f23247a27;p=clang 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284477 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp b/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp index ff07a64d9b..ccd8e9a18b 100644 --- a/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp +++ b/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/test/Analysis/unreachable-code-path.c b/test/Analysis/unreachable-code-path.c index 975c9887e4..4ddfb21f0a 100644 --- a/test/Analysis/unreachable-code-path.c +++ b/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 +} +