[cfe-commits] r109560 - in /cfe/trunk: include/clang/Checker/PathSensitive/CheckerHelpers.h lib/Checker/CMakeLists.txt lib/Checker/CheckerHelpers.cpp lib/Checker/IdempotentOperationChecker.cpp

Tom Care tcare at apple.com
Tue Jul 27 16:26:08 PDT 2010


Author: tcare
Date: Tue Jul 27 18:26:07 2010
New Revision: 109560

URL: http://llvm.org/viewvc/llvm-project?rev=109560&view=rev
Log:
Extracted out some useful common functions in IdempotentOperationChecker to their own CheckerHelpers file.

Added:
    cfe/trunk/include/clang/Checker/PathSensitive/CheckerHelpers.h
    cfe/trunk/lib/Checker/CheckerHelpers.cpp
Modified:
    cfe/trunk/lib/Checker/CMakeLists.txt
    cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp

Added: cfe/trunk/include/clang/Checker/PathSensitive/CheckerHelpers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Checker/PathSensitive/CheckerHelpers.h?rev=109560&view=auto
==============================================================================
--- cfe/trunk/include/clang/Checker/PathSensitive/CheckerHelpers.h (added)
+++ cfe/trunk/include/clang/Checker/PathSensitive/CheckerHelpers.h Tue Jul 27 18:26:07 2010
@@ -0,0 +1,40 @@
+//== CheckerHelpers.h - Helper functions for checkers ------------*- C++ -*--=//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines CheckerVisitor.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_CHECKER_PATHSENSITIVE_CHECKERHELPERS
+#define LLVM_CLANG_CHECKER_PATHSENSITIVE_CHECKERHELPERS
+
+#include "clang/AST/Stmt.h"
+
+namespace clang {
+
+bool containsMacro(const Stmt *S);
+bool containsEnum(const Stmt *S);
+bool containsStaticLocal(const Stmt *S);
+bool containsBuiltinOffsetOf(const Stmt *S);
+template <class T> bool containsStmt(const Stmt *S) {
+  if (isa<T>(S))
+      return true;
+
+  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
+      ++I)
+    if (const Stmt *child = *I)
+      if (containsStmt<T>(child))
+        return true;
+
+  return false;
+}
+
+}
+
+#endif

Modified: cfe/trunk/lib/Checker/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/CMakeLists.txt?rev=109560&r1=109559&r2=109560&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/CMakeLists.txt (original)
+++ cfe/trunk/lib/Checker/CMakeLists.txt Tue Jul 27 18:26:07 2010
@@ -24,6 +24,7 @@
   CheckSecuritySyntaxOnly.cpp
   CheckSizeofPointer.cpp
   Checker.cpp
+  CheckerHelpers.cpp
   CocoaConventions.cpp
   CStringChecker.cpp
   DereferenceChecker.cpp

Added: cfe/trunk/lib/Checker/CheckerHelpers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/CheckerHelpers.cpp?rev=109560&view=auto
==============================================================================
--- cfe/trunk/lib/Checker/CheckerHelpers.cpp (added)
+++ cfe/trunk/lib/Checker/CheckerHelpers.cpp Tue Jul 27 18:26:07 2010
@@ -0,0 +1,82 @@
+//===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines several static functions for use in checkers.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Checker/PathSensitive/CheckerHelpers.h"
+#include "clang/AST/Expr.h"
+
+// Recursively find any substatements containing macros
+bool clang::containsMacro(const Stmt *S) {
+  if (S->getLocStart().isMacroID())
+    return true;
+
+  if (S->getLocEnd().isMacroID())
+    return true;
+
+  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
+      ++I)
+    if (const Stmt *child = *I)
+      if (containsMacro(child))
+        return true;
+
+  return false;
+}
+
+// Recursively find any substatements containing enum constants
+bool clang::containsEnum(const Stmt *S) {
+  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
+
+  if (DR && isa<EnumConstantDecl>(DR->getDecl()))
+    return true;
+
+  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
+      ++I)
+    if (const Stmt *child = *I)
+      if (containsEnum(child))
+        return true;
+
+  return false;
+}
+
+// Recursively find any substatements containing static vars
+bool clang::containsStaticLocal(const Stmt *S) {
+  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
+
+  if (DR)
+    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
+      if (VD->isStaticLocal())
+        return true;
+
+  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
+      ++I)
+    if (const Stmt *child = *I)
+      if (containsStaticLocal(child))
+        return true;
+
+  return false;
+}
+
+// Recursively find any substatements containing __builtin_offset_of
+bool clang::containsBuiltinOffsetOf(const Stmt *S) {
+  const UnaryOperator *UO = dyn_cast<UnaryOperator>(S);
+
+  if (UO && UO->getOpcode() == UnaryOperator::OffsetOf)
+    return true;
+
+  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
+      ++I)
+    if (const Stmt *child = *I)
+      if (containsBuiltinOffsetOf(child))
+        return true;
+
+  return false;
+}

Modified: cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp?rev=109560&r1=109559&r2=109560&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp (original)
+++ cfe/trunk/lib/Checker/IdempotentOperationChecker.cpp Tue Jul 27 18:26:07 2010
@@ -50,6 +50,7 @@
 
 #include "GRExprEngineInternalChecks.h"
 #include "clang/Checker/BugReporter/BugType.h"
+#include "clang/Checker/PathSensitive/CheckerHelpers.h"
 #include "clang/Checker/PathSensitive/CheckerVisitor.h"
 #include "clang/Checker/PathSensitive/SVals.h"
 #include "clang/AST/Stmt.h"
@@ -76,27 +77,11 @@
 
     /// contains* - Useful recursive methods to see if a statement contains an
     ///   element somewhere. Used in static analysis to reduce false positives.
-    static bool containsMacro(const Stmt *S);
-    static bool containsEnum(const Stmt *S);
-    static bool containsStaticLocal(const Stmt *S);
     static bool isParameterSelfAssign(const Expr *LHS, const Expr *RHS);
     static bool isTruncationExtensionAssignment(const Expr *LHS,
                                                 const Expr *RHS);
-    static bool containsBuiltinOffsetOf(const Stmt *S);
     static bool containsZeroConstant(const Stmt *S);
     static bool containsOneConstant(const Stmt *S);
-    template <class T> static bool containsStmt(const Stmt *S) {
-      if (isa<T>(S))
-          return true;
-
-      for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
-          ++I)
-        if (const Stmt *child = *I)
-          if (containsStmt<T>(child))
-            return true;
-
-        return false;
-    }
 
     // Hash table
     typedef llvm::DenseMap<const BinaryOperator *, Assumption> AssumptionMap;
@@ -313,8 +298,8 @@
                                                   BugReporter &BR,
                                                   bool hasWorkRemaining) {
   // If there is any work remaining we cannot be 100% sure about our warnings
-  if (hasWorkRemaining)
-    return;
+//  if (hasWorkRemaining)
+//    return;
 
   // Iterate over the hash to see if we have any paths with definite
   // idempotent operations.
@@ -384,57 +369,6 @@
   }
 }
 
-// Recursively find any substatements containing macros
-bool IdempotentOperationChecker::containsMacro(const Stmt *S) {
-  if (S->getLocStart().isMacroID())
-    return true;
-
-  if (S->getLocEnd().isMacroID())
-    return true;
-
-  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
-      ++I)
-    if (const Stmt *child = *I)
-      if (containsMacro(child))
-        return true;
-
-  return false;
-}
-
-// Recursively find any substatements containing enum constants
-bool IdempotentOperationChecker::containsEnum(const Stmt *S) {
-  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
-
-  if (DR && isa<EnumConstantDecl>(DR->getDecl()))
-    return true;
-
-  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
-      ++I)
-    if (const Stmt *child = *I)
-      if (containsEnum(child))
-        return true;
-
-  return false;
-}
-
-// Recursively find any substatements containing static vars
-bool IdempotentOperationChecker::containsStaticLocal(const Stmt *S) {
-  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
-
-  if (DR)
-    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
-      if (VD->isStaticLocal())
-        return true;
-
-  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
-      ++I)
-    if (const Stmt *child = *I)
-      if (containsStaticLocal(child))
-        return true;
-
-  return false;
-}
-
 // Check for a statement were a parameter is self assigned (to avoid an unused
 // variable warning)
 bool IdempotentOperationChecker::isParameterSelfAssign(const Expr *LHS,
@@ -480,23 +414,6 @@
   return dyn_cast<DeclRefExpr>(RHS->IgnoreParens()) == NULL;
 }
 
-
-// Recursively find any substatements containing __builtin_offset_of
-bool IdempotentOperationChecker::containsBuiltinOffsetOf(const Stmt *S) {
-  const UnaryOperator *UO = dyn_cast<UnaryOperator>(S);
-
-  if (UO && UO->getOpcode() == UnaryOperator::OffsetOf)
-    return true;
-
-  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
-      ++I)
-    if (const Stmt *child = *I)
-      if (containsBuiltinOffsetOf(child))
-        return true;
-
-  return false;
-}
-
 // Check for a integer or float constant of 0
 bool IdempotentOperationChecker::containsZeroConstant(const Stmt *S) {
   const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(S);





More information about the cfe-commits mailing list