Skip to content

Commit fa1bf44

Browse files
author
Daniel Marjamaki
committedOct 18, 2016
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
1 parent 6b6291a commit fa1bf44

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed
 

‎clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
147147
PathDiagnosticLocation DL;
148148
SourceLocation SL;
149149
if (const Stmt *S = getUnreachableStmt(CB)) {
150+
// In macros, 'do {...} while (0)' is often used. Don't warn about the
151+
// condition 0 when it is unreachable.
152+
if (S->getLocStart().isMacroID())
153+
if (const auto *I = dyn_cast<IntegerLiteral>(S))
154+
if (I->getValue() == 0ULL)
155+
if (const Stmt *Parent = PM->getParent(S))
156+
if (isa<DoStmt>(Parent))
157+
continue;
150158
SR = S->getSourceRange();
151159
DL = PathDiagnosticLocation::createBegin(S, B.getSourceManager(), LC);
152160
SL = DL.asLocation();

‎clang/test/Analysis/unreachable-code-path.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,10 @@ void test13(int i) {
206206
int x = inlineFunction(i);
207207
x && x < 10; // no-warning
208208
}
209+
210+
// Don't warn in a macro
211+
#define RETURN(X) do { return; } while (0)
212+
void macro(void) {
213+
RETURN(1); // no-warning
214+
}
215+

0 commit comments

Comments
 (0)
Please sign in to comment.