danielmarjamaki created this revision. danielmarjamaki added subscribers: cfe-commits, NoQ, zaks.anna, a.sidorin, xazax.hun. danielmarjamaki set the repository for this revision to rL LLVM.
This patch fixes false positives for vardecls that are technically unreachable but they are needed. ``` switch (x) { int a; // <- This is unreachable but needed case 1: a = ... ``` For this code there will be Wunused-variable: ``` if (1+2==45) { int x; } ``` For this code there is 'unreachable code' warning on the 'if (1)': ``` if (1+2==45) { int x; if (1) {} } ``` Repository: rL LLVM https://reviews.llvm.org/D24905 Files: lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp test/Analysis/unreachable-code-path.c Index: test/Analysis/unreachable-code-path.c =================================================================== --- test/Analysis/unreachable-code-path.c +++ test/Analysis/unreachable-code-path.c @@ -158,3 +158,16 @@ } } } + +// don't warn about unreachable vardecl +void varDecl() { + switch (x) { + int a; // No warning here. + case 1: + a=1; + break; + case 2: + a=2; + break; + } +} Index: lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp =================================================================== --- lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp +++ lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp @@ -191,8 +191,10 @@ // Find the Stmt* in a CFGBlock for reporting a warning const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) { for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) { - if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) - return S->getStmt(); + if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) { + if (isa<Expr>(S->getStmt())) + return S->getStmt(); + } } if (const Stmt *S = CB->getTerminator()) return S;
Index: test/Analysis/unreachable-code-path.c =================================================================== --- test/Analysis/unreachable-code-path.c +++ test/Analysis/unreachable-code-path.c @@ -158,3 +158,16 @@ } } } + +// don't warn about unreachable vardecl +void varDecl() { + switch (x) { + int a; // No warning here. + case 1: + a=1; + break; + case 2: + a=2; + break; + } +} Index: lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp =================================================================== --- lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp +++ lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp @@ -191,8 +191,10 @@ // Find the Stmt* in a CFGBlock for reporting a warning const Stmt *UnreachableCodeChecker::getUnreachableStmt(const CFGBlock *CB) { for (CFGBlock::const_iterator I = CB->begin(), E = CB->end(); I != E; ++I) { - if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) - return S->getStmt(); + if (Optional<CFGStmt> S = I->getAs<CFGStmt>()) { + if (isa<Expr>(S->getStmt())) + return S->getStmt(); + } } if (const Stmt *S = CB->getTerminator()) return S;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits