================
@@ -0,0 +1,357 @@
+//===- unittests/StaticAnalyzer/BlockEntranceCallbackTest.cpp 
-------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CheckerRegistration.h"
+#include "clang/Analysis/AnalysisDeclContext.h"
+#include "clang/Analysis/ProgramPoint.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
+#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
+#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gtest/gtest.h"
+
+using namespace clang;
+using namespace ento;
+
+namespace {
+
+class BlockEntranceCallbackTester final : public Checker<check::BlockEntrance> 
{
+  const BugType Bug{this, "BlockEntranceTester"};
+
+public:
+  void checkBlockEntrance(const BlockEntrance &Entrance,
+                          CheckerContext &C) const {
+    ExplodedNode *Node = C.generateNonFatalErrorNode(C.getState());
+    if (!Node)
+      return;
+
+    const auto *FD =
+        cast<FunctionDecl>(C.getLocationContext()->getStackFrame()->getDecl());
+
+    std::string Description = llvm::formatv(
+        "Within '{0}' B{1} -> B{2}", FD->getIdentifier()->getName(),
+        Entrance.getPreviousBlock()->getBlockID(),
+        Entrance.getBlock()->getBlockID());
+    auto Report =
+        std::make_unique<PathSensitiveBugReport>(Bug, Description, Node);
+    C.emitReport(std::move(Report));
+  }
+};
+
+class BranchConditionCallbackTester final
+    : public Checker<check::BranchCondition> {
+  const BugType Bug{this, "BranchConditionCallbackTester"};
+
+public:
+  void checkBranchCondition(const Stmt *Condition, CheckerContext &C) const {
+    ExplodedNode *Node = C.generateNonFatalErrorNode(C.getState());
+    if (!Node)
+      return;
+    const auto *FD =
+        cast<FunctionDecl>(C.getLocationContext()->getStackFrame()->getDecl());
+
+    std::string Buffer =
+        (llvm::Twine("Within '") + FD->getIdentifier()->getName() +
+         "': branch condition '")
+            .str();
+    llvm::raw_string_ostream OS(Buffer);
+    Condition->printPretty(OS, /*Helper=*/nullptr,
+                           C.getASTContext().getPrintingPolicy());
+    OS << "'";
+    auto Report = std::make_unique<PathSensitiveBugReport>(Bug, Buffer, Node);
+    C.emitReport(std::move(Report));
+
+    C.addTransition();
+  }
+};
+
+template <typename Checker> void registerChecker(CheckerManager &Mgr) {
+  Mgr.registerChecker<Checker>();
+}
+
+bool shouldAlwaysRegister(const CheckerManager &) { return true; }
+
+void addBlockEntranceTester(AnalysisASTConsumer &AnalysisConsumer,
+                            AnalyzerOptions &AnOpts) {
+  AnOpts.CheckersAndPackages.emplace_back("test.BlockEntranceTester", true);
+  AnalysisConsumer.AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
+    Registry.addChecker(&registerChecker<BlockEntranceCallbackTester>,
+                        &shouldAlwaysRegister, "test.BlockEntranceTester",
+                        "EmptyDescription", "EmptyDocsUri",
+                        /*IsHidden=*/false);
----------------
NagyDonat wrote:

This will be yet another code fragment that will be prettified when I 
reintroduce `addMockChecker` in the spinoff NFC commit. There are already 10-15 
tests that use `addChecker` calls with lots of dummy parameters like this, it 
will be nice to get rid of them :grin: 

No action expected.

https://github.com/llvm/llvm-project/pull/140924
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to