[cfe-commits] r94084 - in /cfe/trunk: lib/Analysis/CFG.cpp lib/Sema/SemaChecking.cpp test/Sema/warn-unreachable.c test/SemaCXX/warn-unreachable.cpp

Mike Stump mrs at apple.com
Thu Jan 21 09:21:23 PST 2010


Author: mrs
Date: Thu Jan 21 11:21:23 2010
New Revision: 94084

URL: http://llvm.org/viewvc/llvm-project?rev=94084&view=rev
Log:
Improve unreachable code warnings with respect to dead binary and
unary operators.

Modified:
    cfe/trunk/lib/Analysis/CFG.cpp
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/warn-unreachable.c
    cfe/trunk/test/SemaCXX/warn-unreachable.cpp

Modified: cfe/trunk/lib/Analysis/CFG.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/CFG.cpp?rev=94084&r1=94083&r2=94084&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/CFG.cpp (original)
+++ cfe/trunk/lib/Analysis/CFG.cpp Thu Jan 21 11:21:23 2010
@@ -237,7 +237,8 @@
 ///  transferred to the caller.  If CFG construction fails, this method returns
 ///  NULL.
 CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
-                          bool AddEHEdges, bool AddScopes) {
+                          bool addehedges, bool AddScopes) {
+  AddEHEdges = addehedges;
   Context = C;
   assert(cfg.get());
   if (!Statement)

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=94084&r1=94083&r2=94084&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Jan 21 11:21:23 2010
@@ -2064,7 +2064,8 @@
   return count;
 }
 
-static SourceLocation GetUnreachableLoc(CFGBlock &b) {
+static SourceLocation GetUnreachableLoc(CFGBlock &b, SourceRange &R1,
+                                        SourceRange &R2) {
   Stmt *S;
   if (!b.empty())
     S = b[0].getStmt();
@@ -2075,7 +2076,10 @@
 
   switch (S->getStmtClass()) {
   case Expr::BinaryOperatorClass: {
-    if (b.size() < 2) {
+    BinaryOperator *BO = cast<BinaryOperator>(S);
+    if (BO->getOpcode() == BinaryOperator::Comma) {
+      if (b.size() >= 2)
+        return b[1].getStmt()->getLocStart();
       CFGBlock *n = &b;
       while (1) {
         if (n->getTerminator())
@@ -2089,7 +2093,14 @@
           return n[0][0].getStmt()->getLocStart();
       }
     }
-    return b[1].getStmt()->getLocStart();
+    R1 = BO->getLHS()->getSourceRange();
+    R2 = BO->getRHS()->getSourceRange();
+    return BO->getOperatorLoc();
+  }
+  case Expr::UnaryOperatorClass: {
+    const UnaryOperator *UO = cast<UnaryOperator>(S);
+    R1 = UO->getSubExpr()->getSourceRange();
+    return UO->getOperatorLoc();
   }
   case Stmt::CXXTryStmtClass: {
     return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
@@ -2104,7 +2115,8 @@
   std::queue<CFGBlock*> workq;
   // Prep work queue
   workq.push(e);
-  SourceLocation top = GetUnreachableLoc(*e);
+  SourceRange R1, R2;
+  SourceLocation top = GetUnreachableLoc(*e, R1, R2);
   bool FromMainFile = false;
   bool FromSystemHeader = false;
   bool TopValid = false;
@@ -2117,7 +2129,7 @@
   while (!workq.empty()) {
     CFGBlock *item = workq.front();
     workq.pop();
-    SourceLocation c = GetUnreachableLoc(*item);
+    SourceLocation c = GetUnreachableLoc(*item, R1, R2);
     if (c.isValid()
         && (!TopValid
             || (SM.isFromMainFile(c) && !FromMainFile)
@@ -2169,6 +2181,8 @@
     // If there are no dead blocks, we're done.
     return;
 
+  SourceRange R1, R2;
+
   llvm::SmallVector<SourceLocation, 24> lines;
   bool AddEHEdges = AC.getAddEHEdges();
   // First, give warnings for blocks with no predecessors, as they
@@ -2184,7 +2198,7 @@
           count += MarkLive(&b, live);
           continue;
         }
-        SourceLocation c = GetUnreachableLoc(b);
+        SourceLocation c = GetUnreachableLoc(b, R1, R2);
         if (!c.isValid()) {
           // Blocks without a location can't produce a warning, so don't mark
           // reachable blocks from here as live.

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=94084&r1=94083&r2=94084&view=diff

==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Thu Jan 21 11:21:23 2010
@@ -30,14 +30,14 @@
       dead();   // expected-warning {{will never be executed}}
 
   case 2:
-    live(),
-      halt(),
+    live(), halt(),
       dead();   // expected-warning {{will never be executed}}
 
   case 3:
   live()
-    + halt();
-  dead();     // expected-warning {{will never be executed}}
+    +           // expected-warning {{will never be executed}}
+    halt();
+  dead();
 
   case 4:
   a4:
@@ -72,5 +72,11 @@
       halt();
   b6:
     goto c6;
+  case 7:
+    halt()
+      +         // expected-warning {{will never be executed}}
+      dead();
+    -           // expected-warning {{will never be executed}}
+      halt();
   }
 }

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=94084&r1=94083&r2=94084&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Thu Jan 21 11:21:23 2010
@@ -1,6 +1,7 @@
 // RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wunreachable-code -Wno-unused-value
 
-int live();
+int &halt() __attribute__((noreturn));
+int &live();
 int dead();
 int liveti() throw(int);
 int (*livetip)() throw(int);
@@ -33,3 +34,9 @@
   throw 1;
   dead();       // expected-warning {{will never be executed}}
 }
+
+
+void test3() {
+  halt()
+    --;         // expected-warning {{will never be executed}}
+}





More information about the cfe-commits mailing list