================ @@ -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 << "'"; ---------------- NagyDonat wrote:
Bit sad to see this string juggling, but I understand that this is sometimes unavoidable... No action needed. 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