r205074 - [-Wunreachable-code] Expand paren-suppression heuristic to C++/ObjC bools.

Ted Kremenek kremenek at apple.com
Fri Mar 28 21:49:20 PDT 2014


Author: kremenek
Date: Fri Mar 28 23:49:20 2014
New Revision: 205074

URL: http://llvm.org/viewvc/llvm-project?rev=205074&view=rev
Log:
[-Wunreachable-code] Expand paren-suppression heuristic to C++/ObjC bools.

Modified:
    cfe/trunk/lib/Analysis/ReachableCode.cpp
    cfe/trunk/test/SemaCXX/warn-unreachable.cpp
    cfe/trunk/test/SemaObjC/warn-unreachable.m

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=205074&r1=205073&r2=205074&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Fri Mar 28 23:49:20 2014
@@ -141,12 +141,15 @@ static bool isConfigurationValue(const S
 
   // Special case looking for the sigil '()' around an integer literal.
   if (const ParenExpr *PE = dyn_cast<ParenExpr>(S))
-    return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal, 
-                                IncludeIntegers, true);
+    if (!PE->getLocStart().isMacroID())
+      return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal, 
+                                  IncludeIntegers, true);
 
   if (const Expr *Ex = dyn_cast<Expr>(S))
     S = Ex->IgnoreParenCasts();
 
+  bool IgnoreYES_NO = false;
+
   switch (S->getStmtClass()) {
     case Stmt::CallExprClass: {
       const FunctionDecl *Callee =
@@ -155,19 +158,21 @@ static bool isConfigurationValue(const S
     }
     case Stmt::DeclRefExprClass:
       return isConfigurationValue(cast<DeclRefExpr>(S)->getDecl(), PP);
+    case Stmt::ObjCBoolLiteralExprClass:
+      IgnoreYES_NO = true;
+      // Fallthrough.
+    case Stmt::CXXBoolLiteralExprClass:
     case Stmt::IntegerLiteralClass: {
-      const IntegerLiteral *E = cast<IntegerLiteral>(S);
+      const Expr *E = cast<Expr>(S);
       if (IncludeIntegers) {
         if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid())
           *SilenceableCondVal = E->getSourceRange();
-        return WrappedInParens || isExpandedFromConfigurationMacro(E, PP);
+        return WrappedInParens || isExpandedFromConfigurationMacro(E, PP, IgnoreYES_NO);
       }
       return false;
     }
     case Stmt::MemberExprClass:
       return isConfigurationValue(cast<MemberExpr>(S)->getMemberDecl(), PP);
-    case Stmt::ObjCBoolLiteralExprClass:
-      return isExpandedFromConfigurationMacro(S, PP, /* IgnoreYES_NO */ true);
     case Stmt::UnaryExprOrTypeTraitExprClass:
       return true;
     case Stmt::BinaryOperatorClass: {

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=205074&r1=205073&r2=205074&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Fri Mar 28 23:49:20 2014
@@ -294,3 +294,36 @@ void test_unreachable_forrange_increment
   }
 }
 
+void calledFun() {}
+
+// Test "silencing" with parentheses.
+void test_with_paren_silencing(int x) {
+  if (false) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+  if ((false)) calledFun(); // no-warning
+
+  if (true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+    calledFun();
+  else
+    calledFun(); // expected-warning {{will never be executed}}
+
+  if ((true))
+    calledFun();
+  else
+    calledFun(); // no-warning
+  
+  if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+    calledFun(); // expected-warning {{code will never be executed}}
+  else
+    calledFun();
+  
+  if ((!true))
+    calledFun(); // no-warning
+  else
+    calledFun();
+  
+  if (!(true))
+    calledFun(); // no-warning
+  else
+    calledFun();
+}
+

Modified: cfe/trunk/test/SemaObjC/warn-unreachable.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/warn-unreachable.m?rev=205074&r1=205073&r2=205074&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/warn-unreachable.m (original)
+++ cfe/trunk/test/SemaObjC/warn-unreachable.m Fri Mar 28 23:49:20 2014
@@ -49,3 +49,36 @@ void test_loop_increment(id container) {
   }
 }
 
+void calledFun() {}
+
+// Test "silencing" with parentheses.
+void test_with_paren_silencing(int x) {
+  if (NO) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+  if ((NO)) calledFun(); // no-warning
+
+  if (YES) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+    calledFun();
+  else
+    calledFun(); // expected-warning {{will never be executed}}
+
+  if ((YES))
+    calledFun();
+  else
+    calledFun(); // no-warning
+  
+  if (!YES) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+    calledFun(); // expected-warning {{code will never be executed}}
+  else
+    calledFun();
+  
+  if ((!YES))
+    calledFun(); // no-warning
+  else
+    calledFun();
+  
+  if (!(YES))
+    calledFun(); // no-warning
+  else
+    calledFun();
+}
+





More information about the cfe-commits mailing list