]> granicus.if.org Git - clang/commitdiff
Teach -Wunreachable-code about dead code caused by macro expansions. This should...
authorTed Kremenek <kremenek@apple.com>
Thu, 25 Aug 2011 19:28:55 +0000 (19:28 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 25 Aug 2011 19:28:55 +0000 (19:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138576 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ReachableCode.cpp
test/Sema/warn-unreachable.c

index e3194cb6a16ab70a009fef1fbd2ba0b32ab49c88..49317718c3818cfcb98c976739e84479f905af81 100644 (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;
 }
 
@@ -144,6 +142,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);
index 80f32cd73281f25a0089f6d33fd9a1fb3e6938e8..8db36b710012dcea29e406f958069896675428e6 100644 (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
+}
+
+