[analyzer] ExprEngine: remove second call to PreStmt<CastExpr>
authorAleksei Sidorin <a.sidorin@samsung.com>
Thu, 1 Sep 2016 13:55:38 +0000 (13:55 +0000)
committerAleksei Sidorin <a.sidorin@samsung.com>
Thu, 1 Sep 2016 13:55:38 +0000 (13:55 +0000)
This patch also introduces AnalysisOrderChecker which is intended for testing
of callback call correctness.

Differential Revision: https://reviews.llvm.org/D23804

llvm-svn: 280367

clang/include/clang/StaticAnalyzer/Checkers/Checkers.td
clang/lib/StaticAnalyzer/Checkers/AnalysisOrderChecker.cpp [new file with mode: 0644]
clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt
clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
clang/test/Analysis/castexpr-callback.c [new file with mode: 0644]

index a7a6623..51f22fa 100644 (file)
@@ -634,6 +634,10 @@ def LLVMConventionsChecker : Checker<"Conventions">,
 
 let ParentPackage = Debug in {
 
+def AnalysisOrderChecker : Checker<"AnalysisOrder">,
+  HelpText<"Print callbacks that are called during analysis in order">,
+  DescFile<"AnalysisOrder.cpp">;
+
 def DominatorsTreeDumper : Checker<"DumpDominators">,
   HelpText<"Print the dominance tree for a given CFG">,
   DescFile<"DebugCheckers.cpp">;
diff --git a/clang/lib/StaticAnalyzer/Checkers/AnalysisOrderChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/AnalysisOrderChecker.cpp
new file mode 100644 (file)
index 0000000..0bb0e33
--- /dev/null
@@ -0,0 +1,56 @@
+//===- AnalysisOrderChecker - Print callbacks called ------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This checker prints callbacks that are called during analysis.
+// This is required to ensure that callbacks are fired in order
+// and do not duplicate or get lost.
+// Feel free to extend this checker with any callback you need to check.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ClangSACheckers.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/CheckerManager.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+
+using namespace clang;
+using namespace ento;
+
+namespace {
+
+class AnalysisOrderChecker : public Checker< check::PreStmt<CastExpr>,
+                                             check::PostStmt<CastExpr>> {
+  bool isCallbackEnabled(CheckerContext &C, StringRef CallbackName) const {
+    AnalyzerOptions &Opts = C.getAnalysisManager().getAnalyzerOptions();
+    return Opts.getBooleanOption("*", false, this) ||
+        Opts.getBooleanOption(CallbackName, false, this);
+  }
+
+public:
+  void checkPreStmt(const CastExpr *CE, CheckerContext &C) const {
+    if (isCallbackEnabled(C, "PreStmtCastExpr"))
+      llvm::errs() << "PreStmt<CastExpr> (Kind : " << CE->getCastKindName()
+                   << ")\n";
+  }
+
+  void checkPostStmt(const CastExpr *CE, CheckerContext &C) const {
+    if (isCallbackEnabled(C, "PostStmtCastExpr"))
+      llvm::errs() << "PostStmt<CastExpr> (Kind : " << CE->getCastKindName()
+                   << ")\n";
+  }
+};
+}
+
+//===----------------------------------------------------------------------===//
+// Registration.
+//===----------------------------------------------------------------------===//
+
+void ento::registerAnalysisOrderChecker(CheckerManager &mgr) {
+  mgr.registerChecker<AnalysisOrderChecker>();
+}
index 75636c6..5a486cd 100644 (file)
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_clang_library(clangStaticAnalyzerCheckers
   AllocationDiagnostics.cpp
+  AnalysisOrderChecker.cpp
   AnalyzerStatsChecker.cpp
   ArrayBoundChecker.cpp
   ArrayBoundCheckerV2.cpp
index 6ca24c7..97f4d9f 100644 (file)
@@ -1212,16 +1212,8 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
     case Stmt::ObjCBridgedCastExprClass: {
       Bldr.takeNodes(Pred);
       const CastExpr *C = cast<CastExpr>(S);
-      // Handle the previsit checks.
-      ExplodedNodeSet dstPrevisit;
-      getCheckerManager().runCheckersForPreStmt(dstPrevisit, Pred, C, *this);
-
-      // Handle the expression itself.
       ExplodedNodeSet dstExpr;
-      for (ExplodedNodeSet::iterator i = dstPrevisit.begin(),
-                                     e = dstPrevisit.end(); i != e ; ++i) {
-        VisitCast(C, C->getSubExpr(), *i, dstExpr);
-      }
+      VisitCast(C, C->getSubExpr(), Pred, dstExpr);
 
       // Handle the postvisit checks.
       getCheckerManager().runCheckersForPostStmt(Dst, dstExpr, C, *this);
diff --git a/clang/test/Analysis/castexpr-callback.c b/clang/test/Analysis/castexpr-callback.c
new file mode 100644 (file)
index 0000000..73fa17a
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=debug.AnalysisOrder -analyzer-config debug.AnalysisOrder:PreStmtCastExpr=true,debug.AnalysisOrder:PostStmtCastExpr=true %s 2>&1 | FileCheck %s
+
+void test(char c) {
+  int i = (int)c;
+}
+
+// CHECK: PreStmt<CastExpr> (Kind : LValueToRValue)
+// CHECK-NEXT: PostStmt<CastExpr> (Kind : LValueToRValue)
+// CHECK-NEXT: PreStmt<CastExpr> (Kind : IntegralCast)
+// CHECK-NEXT: PostStmt<CastExpr> (Kind : IntegralCast)