[LV] Add test showing debug output for loops with uncountable BTCs.
[llvm-project.git] / clang / unittests / StaticAnalyzer / CallEventTest.cpp
blobd5ca72acaca29d053b678ab57dbf2f2b1844a2dd
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
10 #include "CheckerRegistration.h"
11 #include "clang/Basic/LLVM.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
14 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
20 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
21 #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
22 #include "clang/Tooling/Tooling.h"
23 #include "gtest/gtest.h"
25 namespace clang {
26 namespace ento {
27 namespace {
29 void reportBug(const CheckerBase *Checker, const CallEvent &Call,
30 CheckerContext &C, StringRef WarningMsg) {
31 C.getBugReporter().EmitBasicReport(
32 nullptr, Checker, "", categories::LogicError, WarningMsg,
33 PathDiagnosticLocation(Call.getOriginExpr(), C.getSourceManager(),
34 C.getLocationContext()),
35 {});
38 class CXXDeallocatorChecker : public Checker<check::PreCall> {
39 public:
40 void checkPreCall(const CallEvent &Call, CheckerContext &C) const {
41 const auto *DC = dyn_cast<CXXDeallocatorCall>(&Call);
42 if (!DC) {
43 return;
46 SmallString<100> WarningBuf;
47 llvm::raw_svector_ostream WarningOS(WarningBuf);
48 WarningOS << "NumArgs: " << DC->getNumArgs();
50 reportBug(this, *DC, C, WarningBuf);
54 void addCXXDeallocatorChecker(AnalysisASTConsumer &AnalysisConsumer,
55 AnalyzerOptions &AnOpts) {
56 AnOpts.CheckersAndPackages = {{"test.CXXDeallocator", true}};
57 AnalysisConsumer.AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
58 Registry.addChecker<CXXDeallocatorChecker>("test.CXXDeallocator",
59 "Description", "");
60 });
63 // TODO: What we should really be testing here is all the different varieties
64 // of delete operators, and wether the retrieval of their arguments works as
65 // intended. At the time of writing this file, CXXDeallocatorCall doesn't pick
66 // up on much of those due to the AST not containing CXXDeleteExpr for most of
67 // the standard/custom deletes.
68 TEST(CXXDeallocatorCall, SimpleDestructor) {
69 std::string Diags;
70 EXPECT_TRUE(runCheckerOnCode<addCXXDeallocatorChecker>(R"(
71 struct A {};
73 void f() {
74 A *a = new A;
75 delete a;
77 )",
78 Diags));
79 #if defined(_AIX) || defined(__MVS__) || defined(__MINGW32__)
80 // AIX, ZOS and MinGW default to -fno-sized-deallocation.
81 EXPECT_EQ(Diags, "test.CXXDeallocator: NumArgs: 1\n");
82 #else
83 EXPECT_EQ(Diags, "test.CXXDeallocator: NumArgs: 2\n");
84 #endif
87 } // namespace
88 } // namespace ento
89 } // namespace clang