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 //===----------------------------------------------------------------------===// 8 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" 24 25 namespace clang { 26 namespace ento { 27 namespace { 28 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 {}); 36 } 37 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; 44 } 45 46 SmallString<100> WarningBuf; 47 llvm::raw_svector_ostream WarningOS(WarningBuf); 48 WarningOS << "NumArgs: " << DC->getNumArgs(); 49 50 reportBug(this, *DC, C, WarningBuf); 51 } 52 }; 53 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 }); 61 } 62 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 {}; 72 73 void f() { 74 A *a = new A; 75 delete a; 76 } 77 )", 78 Diags)); 79 EXPECT_EQ(Diags, "test.CXXDeallocator: NumArgs: 1\n"); 80 } 81 82 } // namespace 83 } // namespace ento 84 } // namespace clang 85