clang 24.0.0git
NoDeleteChecker.cpp
Go to the documentation of this file.
1//=======- NoDeleteChecker.cpp -----------------------------------*- C++ -*-==//
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 "DiagOutputUtils.h"
10#include "PtrTypesSemantics.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/DeclCXX.h"
22
23using namespace clang;
24using namespace ento;
25
26namespace {
27
28class NoDeleteChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
29 BugType Bug;
30 mutable BugReporter *BR = nullptr;
31 mutable TrivialFunctionAnalysis TFA;
32
33public:
34 NoDeleteChecker()
35 : Bug(this,
36 "Incorrect [[clang::annotate_type(\"webkit.nodelete\")]] "
37 "annotation",
38 "WebKit coding guidelines") {}
39
40 void checkASTDecl(const TranslationUnitDecl *TUD, AnalysisManager &MGR,
41 BugReporter &BRArg) const {
42 BR = &BRArg;
43
44 // The calls to checkAST* from AnalysisConsumer don't
45 // visit template instantiations or lambda classes. We
46 // want to visit those, so we make our own visitor.
47 struct LocalVisitor final : public ConstDynamicRecursiveASTVisitor {
48 const NoDeleteChecker *Checker;
49 Decl *DeclWithIssue{nullptr};
50
51 explicit LocalVisitor(const NoDeleteChecker *Checker) : Checker(Checker) {
52 assert(Checker);
53 ShouldVisitTemplateInstantiations = true;
54 ShouldWalkTypesOfTypeLocs = true;
55 ShouldVisitImplicitCode = false;
56 ShouldVisitLambdaBody = true;
57 }
58
59 bool VisitFunctionDecl(const FunctionDecl *FD) override {
60 Checker->visitFunctionDecl(FD);
61 return true;
62 }
63 };
64
65 LocalVisitor visitor(this);
66 visitor.TraverseDecl(const_cast<TranslationUnitDecl *>(TUD));
67 }
68
69 void visitFunctionDecl(const FunctionDecl *FD) const {
71 return;
72
73 if (!isNoDeleteFunction(FD))
74 return;
75
76 auto Body = FD->getBody();
77 if (!Body)
78 return;
79
80 NamedDecl *ParamDecl = nullptr;
81 for (auto *D : FD->parameters()) {
82 if (!TFA.hasTrivialDtor(D)) {
83 ParamDecl = D;
84 break;
85 }
86 }
87
88 const FieldDecl *Field = nullptr;
89 const Stmt *OffendingStmt = nullptr;
90 bool IsCtor = false;
91 bool IsDtor = false;
92 if (auto *Ctor = dyn_cast<CXXConstructorDecl>(FD)) {
93 IsCtor = true;
94 Field = TFA.fieldWithNonTrivialCtor(Ctor->getParent());
95 if (!Field) {
96 for (auto *CtorInit : Ctor->inits()) {
97 if (!TFA.isTrivial(CtorInit->getInit(), &OffendingStmt)) {
98 if (!OffendingStmt)
99 OffendingStmt = CtorInit->getInit();
100 break;
101 }
102 }
103 }
104 } else if (auto *Dtor = dyn_cast<CXXDestructorDecl>(FD)) {
105 IsDtor = true;
106 Field = TFA.fieldWithNonTrivialDtor(Dtor->getParent());
107 }
108
109 if (!ParamDecl && !Field && !OffendingStmt &&
110 TFA.isTrivial(Body, &OffendingStmt))
111 return;
112
113 SmallString<100> Buf;
114 llvm::raw_svector_ostream Os(Buf);
115
116 if (IsCtor)
117 Os << "A constructor ";
118 else if (IsDtor)
119 Os << "A destructor ";
120 else
121 Os << "A function ";
122 printQuotedName(Os, FD);
123 // FIXME: Update this to say clang::annotate("webkit.nodelete").
124 Os << " has [[clang::annotate_type(\"webkit.nodelete\")]] but it ";
125 if (IsCtor && Field)
126 Os << "constructs ";
127 else if (IsDtor && Field)
128 Os << "destructs ";
129 else
130 Os << "contains ";
131 SourceLocation SrcLocToReport;
132 SourceRange Range;
133 if (ParamDecl) {
134 Os << "a parameter ";
135 printQuotedName(Os, ParamDecl);
136 Os << " which could destruct an object.";
137 SrcLocToReport = FD->getBeginLoc();
138 Range = ParamDecl->getSourceRange();
139 } else if (Field) {
140 Os << "a member variable ";
141 printQuotedName(Os, Field);
142 Os << " that could destruct an object.";
143 SrcLocToReport = FD->getBeginLoc();
144 Range = Field->getSourceRange();
145 } else {
146 Os << "code that could destruct an object.";
147 SrcLocToReport = OffendingStmt->getBeginLoc();
148 Range = OffendingStmt->getSourceRange();
149 }
150
151 PathDiagnosticLocation BSLoc(SrcLocToReport, BR->getSourceManager());
152 auto Report = std::make_unique<BasicBugReport>(Bug, Os.str(), BSLoc);
153 Report->addRange(Range);
154 Report->setDeclWithIssue(FD);
155 BR->emitReport(std::move(Report));
156 }
157};
158
159} // namespace
160
161void ento::registerNoDeleteChecker(CheckerManager &Mgr) {
162 Mgr.registerChecker<NoDeleteChecker>();
163}
164
165bool ento::shouldRegisterNoDeleteChecker(const CheckerManager &) {
166 return true;
167}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::SourceLocation class and associated facilities.
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:435
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Decl.h:832
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3266
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2905
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition Decl.h:2440
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:565
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
Top level wrappers for InstallAPI frontend operations.
void printQuotedName(llvm::raw_ostream &Os, const NamedDeclDerivedT &D)
DynamicRecursiveASTVisitorBase< true > ConstDynamicRecursiveASTVisitor
bool isNoDeleteFunction(const FunctionDecl *F)