clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

NewDeleteOverloadsCheck.cpp
Go to the documentation of this file.
1 //===--- NewDeleteOverloadsCheck.cpp - clang-tidy--------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "NewDeleteOverloadsCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace {
18 AST_MATCHER(FunctionDecl, isPlacementOverload) {
19 bool New;
20 switch (Node.getOverloadedOperator()) {
21 default:
22 return false;
23 case OO_New:
24 case OO_Array_New:
25 New = true;
26 break;
27 case OO_Delete:
28 case OO_Array_Delete:
29 New = false;
30 break;
31 }
32
33 // Variadic functions are always placement functions.
34 if (Node.isVariadic())
35 return true;
36
37 // Placement new is easy: it always has more than one parameter (the first
38 // parameter is always the size). If it's an overload of delete or delete[]
39 // that has only one parameter, it's never a placement delete.
40 if (New)
41 return Node.getNumParams() > 1;
42 if (Node.getNumParams() == 1)
43 return false;
44
45 // Placement delete is a little more challenging. They always have more than
46 // one parameter with the first parameter being a pointer. However, the
47 // second parameter can be a size_t for sized deallocation, and that is never
48 // a placement delete operator.
49 if (Node.getNumParams() <= 1 || Node.getNumParams() > 2)
50 return true;
51
52 const auto *FPT = Node.getType()->castAs<FunctionProtoType>();
53 ASTContext &Ctx = Node.getASTContext();
54 if (Ctx.getLangOpts().SizedDeallocation &&
55 Ctx.hasSameType(FPT->getParamType(1), Ctx.getSizeType()))
56 return false;
57
58 return true;
59 }
60 } // namespace
61
62 namespace tidy {
63 namespace misc {
64
65 namespace {
66 OverloadedOperatorKind getCorrespondingOverload(const FunctionDecl *FD) {
67 switch (FD->getOverloadedOperator()) {
68 default: break;
69 case OO_New:
70 return OO_Delete;
71 case OO_Delete:
72 return OO_New;
73 case OO_Array_New:
74 return OO_Array_Delete;
75 case OO_Array_Delete:
76 return OO_Array_New;
77 }
78 llvm_unreachable("Not an overloaded allocation operator");
79 }
80
81 const char *getOperatorName(OverloadedOperatorKind K) {
82 switch (K) {
83 default: break;
84 case OO_New:
85 return "operator new";
86 case OO_Delete:
87 return "operator delete";
88 case OO_Array_New:
89 return "operator new[]";
90 case OO_Array_Delete:
91 return "operator delete[]";
92 }
93 llvm_unreachable("Not an overloaded allocation operator");
94 }
95
96 bool areCorrespondingOverloads(const FunctionDecl *LHS,
97 const FunctionDecl *RHS) {
98 return RHS->getOverloadedOperator() == getCorrespondingOverload(LHS);
99 }
100
101 bool hasCorrespondingOverloadInBaseClass(const CXXMethodDecl *MD,
102 const CXXRecordDecl *RD = nullptr) {
103 if (RD) {
104 // Check the methods in the given class and accessible to derived classes.
105 for (const auto *BMD : RD->methods())
106 if (BMD->isOverloadedOperator() && BMD->getAccess() != AS_private &&
107 areCorrespondingOverloads(MD, BMD))
108 return true;
109 } else {
110 // Get the parent class of the method; we do not need to care about checking
111 // the methods in this class as the caller has already done that by looking
112 // at the declaration contexts.
113 RD = MD->getParent();
114 }
115
116 for (const auto &BS : RD->bases()) {
117 // We can't say much about a dependent base class, but to avoid false
118 // positives assume it can have a corresponding overload.
119 if (BS.getType()->isDependentType())
120 return true;
121 if (const auto *BaseRD = BS.getType()->getAsCXXRecordDecl())
122 if (hasCorrespondingOverloadInBaseClass(MD, BaseRD))
123 return true;
124 }
125
126 return false;
127 }
128 } // anonymous namespace
129
130 void NewDeleteOverloadsCheck::registerMatchers(MatchFinder *Finder) {
131 if (!getLangOpts().CPlusPlus)
132 return;
133
134 // Match all operator new and operator delete overloads (including the array
135 // forms). Do not match implicit operators, placement operators, or
136 // deleted/private operators.
137 //
138 // Technically, trivially-defined operator delete seems like a reasonable
139 // thing to also skip. e.g., void operator delete(void *) {}
140 // However, I think it's more reasonable to warn in this case as the user
141 // should really be writing that as a deleted function.
142 Finder->addMatcher(
143 functionDecl(
144 unless(anyOf(isImplicit(), isPlacementOverload(), isDeleted(),
145 cxxMethodDecl(isPrivate()))),
146 anyOf(hasOverloadedOperatorName("new"),
147 hasOverloadedOperatorName("new[]"),
148 hasOverloadedOperatorName("delete"),
149 hasOverloadedOperatorName("delete[]")))
150 .bind("func"),
151 this);
152 }
153
154 void NewDeleteOverloadsCheck::check(const MatchFinder::MatchResult &Result) {
155 // Add any matches we locate to the list of things to be checked at the
156 // end of the translation unit.
157 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func");
158 const CXXRecordDecl *RD = nullptr;
159 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
160 RD = MD->getParent();
161 Overloads[RD].push_back(FD);
162 }
163
164 void NewDeleteOverloadsCheck::onEndOfTranslationUnit() {
165 // Walk over the list of declarations we've found to see if there is a
166 // corresponding overload at the same declaration context or within a base
167 // class. If there is not, add the element to the list of declarations to
168 // diagnose.
169 SmallVector<const FunctionDecl *, 4> Diagnose;
170 for (const auto &RP : Overloads) {
171 // We don't care about the CXXRecordDecl key in the map; we use it as a way
172 // to shard the overloads by declaration context to reduce the algorithmic
173 // complexity when searching for corresponding free store functions.
174 for (const auto *Overload : RP.second) {
175 const auto *Match =
176 std::find_if(RP.second.begin(), RP.second.end(),
177 [&Overload](const FunctionDecl *FD) {
178 if (FD == Overload)
179 return false;
180 // If the declaration contexts don't match, we don't
181 // need to check any further.
182 if (FD->getDeclContext() != Overload->getDeclContext())
183 return false;
184
185 // Since the declaration contexts match, see whether
186 // the current element is the corresponding operator.
187 if (!areCorrespondingOverloads(Overload, FD))
188 return false;
189
190 return true;
191 });
192
193 if (Match == RP.second.end()) {
194 // Check to see if there is a corresponding overload in a base class
195 // context. If there isn't, or if the overload is not a class member
196 // function, then we should diagnose.
197 const auto *MD = dyn_cast<CXXMethodDecl>(Overload);
198 if (!MD || !hasCorrespondingOverloadInBaseClass(MD))
199 Diagnose.push_back(Overload);
200 }
201 }
202 }
203
204 for (const auto *FD : Diagnose)
205 diag(FD->getLocation(), "declaration of %0 has no matching declaration "
206 "of '%1' at the same scope")
207 << FD << getOperatorName(getCorrespondingOverload(FD));
208 }
209
210 } // namespace misc
211 } // namespace tidy
212 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
NewDeleteOverloadsCheck.h
clang::tidy::AST_MATCHER
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
Definition: ProBoundsArrayToPointerDecayCheck.cpp:25
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121