1 //===----------------------------------------------------------------------===//
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
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"
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()),
38 class CXXDeallocatorChecker
: public Checker
<check::PreCall
> {
39 std::unique_ptr
<BugType
> BT_uninitField
;
42 CXXDeallocatorChecker()
43 : BT_uninitField(new BugType(this, "CXXDeallocator")) {}
45 void checkPreCall(const CallEvent
&Call
, CheckerContext
&C
) const {
46 const auto *DC
= dyn_cast
<CXXDeallocatorCall
>(&Call
);
51 SmallString
<100> WarningBuf
;
52 llvm::raw_svector_ostream
WarningOS(WarningBuf
);
53 WarningOS
<< "NumArgs: " << DC
->getNumArgs();
55 reportBug(this, *DC
, C
, WarningBuf
);
59 void addCXXDeallocatorChecker(AnalysisASTConsumer
&AnalysisConsumer
,
60 AnalyzerOptions
&AnOpts
) {
61 AnOpts
.CheckersAndPackages
= {{"test.CXXDeallocator", true}};
62 AnalysisConsumer
.AddCheckerRegistrationFn([](CheckerRegistry
&Registry
) {
63 Registry
.addChecker
<CXXDeallocatorChecker
>("test.CXXDeallocator",
68 // TODO: What we should really be testing here is all the different varieties
69 // of delete operators, and wether the retrieval of their arguments works as
70 // intended. At the time of writing this file, CXXDeallocatorCall doesn't pick
71 // up on much of those due to the AST not containing CXXDeleteExpr for most of
72 // the standard/custom deletes.
73 TEST(CXXDeallocatorCall
, SimpleDestructor
) {
75 EXPECT_TRUE(runCheckerOnCode
<addCXXDeallocatorChecker
>(R
"(
84 EXPECT_EQ(Diags
, "test.CXXDeallocator: NumArgs: 1\n");