Teach -Wunreachable-code about dead code caused by macro expansions. This should suppress false positives resulting from 'assert' and friends.

llvm-svn: 138576
This commit is contained in:
Ted Kremenek 2011-08-25 19:28:55 +00:00
parent 5e30972cff
commit 1b7f49c2d6
2 changed files with 20 additions and 4 deletions

View File

@ -86,12 +86,10 @@ bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
}
static bool isValidDeadStmt(const Stmt *S) {
SourceLocation Loc = S->getLocStart();
if (!(Loc.isValid() && !Loc.isMacroID()))
if (S->getLocStart().isInvalid())
return false;
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
return BO->getOpcode() != BO_Comma;
}
return true;
}
@ -145,6 +143,12 @@ unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start,
continue;
}
// Specially handle macro-expanded code.
if (S->getLocStart().isMacroID()) {
count += clang::reachable_code::ScanReachableFromBlock(Block, Reachable);
continue;
}
if (isDeadCodeRoot(Block)) {
reportDeadCode(S, CB);
count += clang::reachable_code::ScanReachableFromBlock(Block, Reachable);

View File

@ -114,3 +114,15 @@ int test_enum_cases(enum Cases C) {
}
}
// Handle unreachable code triggered by macro expansions.
void __myassert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
#define myassert(e) \
(__builtin_expect(!(e), 0) ? __myassert_rtn(__func__, __FILE__, __LINE__, #e) : (void)0)
void test_assert() {
myassert(0 && "unreachable");
return; // no-warning
}