r203363 - [-Wunreachable-code] Tweak heuristic for configuration values to include arithmetic operations involving sizeof(), but not raw integers.

Ted Kremenek kremenek at apple.com
Sat Mar 8 15:20:12 PST 2014


Author: kremenek
Date: Sat Mar  8 17:20:11 2014
New Revision: 203363

URL: http://llvm.org/viewvc/llvm-project?rev=203363&view=rev
Log:
[-Wunreachable-code] Tweak heuristic for configuration values to include arithmetic operations involving sizeof(), but not raw integers.

This case was motivated by a false positive with the
llvm::AlignOf<> specialization in LLVM.

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

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=203363&r1=203362&r2=203363&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Sat Mar  8 17:20:11 2014
@@ -189,7 +189,8 @@ static bool isExpandedFromConfigurationM
 /// "sometimes unreachable" code.  Such code is usually not interesting
 /// to report as unreachable, and may mask truly unreachable code within
 /// those blocks.
-static bool isConfigurationValue(const Stmt *S) {
+static bool isConfigurationValue(const Stmt *S,
+                                 bool IncludeIntegers = true) {
   if (!S)
     return false;
 
@@ -201,7 +202,7 @@ static bool isConfigurationValue(const S
       const DeclRefExpr *DR = cast<DeclRefExpr>(S);
       const ValueDecl *D = DR->getDecl();
       if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D))
-        return ED ? isConfigurationValue(ED->getInitExpr()) : false;
+        return isConfigurationValue(ED->getInitExpr());
       if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
         // As a heuristic, treat globals as configuration values.  Note
         // that we only will get here if Sema evaluated this
@@ -215,14 +216,18 @@ static bool isConfigurationValue(const S
       return false;
     }
     case Stmt::IntegerLiteralClass:
-      return isExpandedFromConfigurationMacro(S);
+      return IncludeIntegers ? isExpandedFromConfigurationMacro(S)
+                             : false;
     case Stmt::UnaryExprOrTypeTraitExprClass:
       return true;
     case Stmt::BinaryOperatorClass: {
       const BinaryOperator *B = cast<BinaryOperator>(S);
-      return (B->isLogicalOp() || B->isComparisonOp()) &&
-             (isConfigurationValue(B->getLHS()) ||
-              isConfigurationValue(B->getRHS()));
+      // Only include raw integers (not enums) as configuration
+      // values if they are used in a logical or comparison operator
+      // (not arithmetic).
+      IncludeIntegers &= (B->isLogicalOp() || B->isComparisonOp());
+      return isConfigurationValue(B->getLHS(), IncludeIntegers) ||
+             isConfigurationValue(B->getRHS(), IncludeIntegers);
     }
     case Stmt::UnaryOperatorClass: {
       const UnaryOperator *UO = cast<UnaryOperator>(S);

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=203363&r1=203362&r2=203363&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Sat Mar  8 17:20:11 2014
@@ -175,3 +175,27 @@ void raze(const A& x);
 void test_with_unreachable_tmp_dtors(int x) {
   raze(x ? A() : A()); // no-warning
 }
+
+// Test sizeof - sizeof in enum declaration.
+enum { BrownCow = sizeof(long) - sizeof(char) };
+enum { CowBrown = 8 - 1 };
+
+
+int test_enum_sizeof_arithmetic() {
+  if (BrownCow)
+    return 1;
+  return 2;
+}
+
+int test_enum_arithmetic() {
+  if (CowBrown)
+    return 1;
+  return 2; // expected-warning {{never be executed}}
+}
+
+int test_arithmetic() {
+  if (8 -1)
+    return 1;
+  return 2; // expected-warning {{never be executed}}
+}
+





More information about the cfe-commits mailing list