From a9fbf5ba297d77a34d564055f1f05414e0224bf9 Mon Sep 17 00:00:00 2001 From: Tom Care Date: Tue, 27 Jul 2010 23:26:07 +0000 Subject: [PATCH] Extracted out some useful common functions in IdempotentOperationChecker to their own CheckerHelpers file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109560 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Checker/PathSensitive/CheckerHelpers.h | 40 +++++++++ lib/Checker/CMakeLists.txt | 1 + lib/Checker/CheckerHelpers.cpp | 82 +++++++++++++++++ lib/Checker/IdempotentOperationChecker.cpp | 89 +------------------ 4 files changed, 126 insertions(+), 86 deletions(-) create mode 100644 include/clang/Checker/PathSensitive/CheckerHelpers.h create mode 100644 lib/Checker/CheckerHelpers.cpp diff --git a/include/clang/Checker/PathSensitive/CheckerHelpers.h b/include/clang/Checker/PathSensitive/CheckerHelpers.h new file mode 100644 index 0000000000..ea3c842ffc --- /dev/null +++ b/include/clang/Checker/PathSensitive/CheckerHelpers.h @@ -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 bool containsStmt(const Stmt *S) { + if (isa(S)) + return true; + + for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); + ++I) + if (const Stmt *child = *I) + if (containsStmt(child)) + return true; + + return false; +} + +} + +#endif diff --git a/lib/Checker/CMakeLists.txt b/lib/Checker/CMakeLists.txt index 85117ac26d..5b54f0d12d 100644 --- a/lib/Checker/CMakeLists.txt +++ b/lib/Checker/CMakeLists.txt @@ -24,6 +24,7 @@ add_clang_library(clangChecker CheckSecuritySyntaxOnly.cpp CheckSizeofPointer.cpp Checker.cpp + CheckerHelpers.cpp CocoaConventions.cpp CStringChecker.cpp DereferenceChecker.cpp diff --git a/lib/Checker/CheckerHelpers.cpp b/lib/Checker/CheckerHelpers.cpp new file mode 100644 index 0000000000..8a295fbf4c --- /dev/null +++ b/lib/Checker/CheckerHelpers.cpp @@ -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(S); + + if (DR && isa(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(S); + + if (DR) + if (const VarDecl *VD = dyn_cast(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(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; +} diff --git a/lib/Checker/IdempotentOperationChecker.cpp b/lib/Checker/IdempotentOperationChecker.cpp index f8bf708e5f..6e6f5bdd96 100644 --- a/lib/Checker/IdempotentOperationChecker.cpp +++ b/lib/Checker/IdempotentOperationChecker.cpp @@ -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 @@ class IdempotentOperationChecker /// 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 static bool containsStmt(const Stmt *S) { - if (isa(S)) - return true; - - for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); - ++I) - if (const Stmt *child = *I) - if (containsStmt(child)) - return true; - - return false; - } // Hash table typedef llvm::DenseMap AssumptionMap; @@ -313,8 +298,8 @@ void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G, 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 @@ inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A, } } -// 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(S); - - if (DR && isa(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(S); - - if (DR) - if (const VarDecl *VD = dyn_cast(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 @@ bool IdempotentOperationChecker::isTruncationExtensionAssignment( return dyn_cast(RHS->IgnoreParens()) == NULL; } - -// Recursively find any substatements containing __builtin_offset_of -bool IdempotentOperationChecker::containsBuiltinOffsetOf(const Stmt *S) { - const UnaryOperator *UO = dyn_cast(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(S); -- 2.40.0