[cfe-commits] r111190 - in /cfe/trunk: lib/Checker/IdempotentOperationChecker.cpp test/Analysis/idempotent-operations.c

Tom Care tcare at apple.com
Mon Aug 16 14:43:52 PDT 2010


Author: tcare
Date: Mon Aug 16 16:43:52 2010
New Revision: 111190

URL: http://llvm.org/viewvc/llvm-project?rev=111190&view=rev
Log:
Added basic psuedoconstant checking in IdempotentOperationChecker and fixed some test cases.

Modified:
    cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp
    cfe/trunk/test/Analysis/idempotent-operations.c

Modified: cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp?rev=111190&r1=111189&r2=111190&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp (original)
+++ cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp Mon Aug 16 16:43:52 2010
@@ -81,6 +81,7 @@
                                           const CFGBlock *CB,
                                           const GRCoreEngine &CE);
     static bool CanVary(const Expr *Ex, ASTContext &Ctx);
+    static bool isPseudoConstant(const DeclRefExpr *D);
 
     // Hash table
     typedef llvm::DenseMap<const BinaryOperator *,
@@ -530,8 +531,7 @@
     return SE->getTypeOfArgument()->isVariableArrayType();
   }
   case Stmt::DeclRefExprClass:
-    //    return !IsPseudoConstant(cast<DeclRefExpr>(Ex));
-    return true;
+    return !isPseudoConstant(cast<DeclRefExpr>(Ex));
 
   // The next cases require recursion for subexpressions
   case Stmt::BinaryOperatorClass: {
@@ -555,3 +555,17 @@
   }
 }
 
+// Returns true if a DeclRefExpr behaves like a constant.
+bool IdempotentOperationChecker::isPseudoConstant(const DeclRefExpr *DR) {
+  // Check for an enum
+  if (isa<EnumConstantDecl>(DR->getDecl()))
+    return true;
+
+  // Check for a static variable
+  // FIXME: Analysis should model static vars
+  if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
+    if (VD->isStaticLocal())
+      return true;
+
+  return false;
+}

Modified: cfe/trunk/test/Analysis/idempotent-operations.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/idempotent-operations.c?rev=111190&r1=111189&r2=111190&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/idempotent-operations.c (original)
+++ cfe/trunk/test/Analysis/idempotent-operations.c Mon Aug 16 16:43:52 2010
@@ -74,12 +74,14 @@
 // False positive tests
 
 unsigned false1() {
-  return (5 - 2 - 3); // no-warning
+  int a = 10;
+  return a * (5 - 2 - 3); // no-warning
 }
 
 enum testenum { enum1 = 0, enum2 };
 unsigned false2() {
-  return enum1; // no-warning
+  int a = 1234;
+  return enum1 + a; // no-warning
 }
 
 extern unsigned foo();





More information about the cfe-commits mailing list