blob: c62ba86f6bc33836a56f55bd056b9cee419d2372 [file] [log] [blame]
Aaron Ballmande349852015-09-29 13:12:21 +00001//===--- 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
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace {
18AST_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
62namespace tidy {
63namespace misc {
64
65namespace {
66OverloadedOperatorKind 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
81const 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
96bool areCorrespondingOverloads(const FunctionDecl *LHS,
97 const FunctionDecl *RHS) {
98 return RHS->getOverloadedOperator() == getCorrespondingOverload(LHS);
99}
100
101bool hasCorrespondingOverloadInOneClass(const CXXRecordDecl *RD,
102 const CXXMethodDecl *MD) {
103 // Check the methods in the given class and accessible to derived classes.
104 for (const auto *BMD : RD->methods())
105 if (BMD->isOverloadedOperator() && BMD->getAccess() != AS_private &&
106 areCorrespondingOverloads(MD, BMD))
107 return true;
108
109 // Check base classes.
110 for (const auto &BS : RD->bases())
111 if (hasCorrespondingOverloadInOneClass(BS.getType()->getAsCXXRecordDecl(),
112 MD))
113 return true;
114
115 return false;
116}
117bool hasCorrespondingOverloadInBaseClass(const CXXMethodDecl *MD) {
118 // Get the parent class of the method; we do not need to care about checking
119 // the methods in this class as the caller has already done that by looking
120 // at the declaration contexts.
121 const CXXRecordDecl *RD = MD->getParent();
122
123 for (const auto &BS : RD->bases())
124 if (hasCorrespondingOverloadInOneClass(BS.getType()->getAsCXXRecordDecl(),
125 MD))
126 return true;
127
128 return false;
129}
130} // anonymous namespace
131
132void NewDeleteOverloadsCheck::registerMatchers(MatchFinder *Finder) {
133 if (!getLangOpts().CPlusPlus)
134 return;
135
136 // Match all operator new and operator delete overloads (including the array
137 // forms). Do not match implicit operators, placement operators, or
138 // deleted/private operators.
139 //
140 // Technically, trivially-defined operator delete seems like a reasonable
141 // thing to also skip. e.g., void operator delete(void *) {}
142 // However, I think it's more reasonable to warn in this case as the user
143 // should really be writing that as a deleted function.
144 Finder->addMatcher(
145 functionDecl(
146 unless(anyOf(isImplicit(), isPlacementOverload(), isDeleted(),
147 cxxMethodDecl(isPrivate()))),
148 anyOf(hasOverloadedOperatorName("new"),
149 hasOverloadedOperatorName("new[]"),
150 hasOverloadedOperatorName("delete"),
151 hasOverloadedOperatorName("delete[]")))
152 .bind("func"),
153 this);
154}
155
156void NewDeleteOverloadsCheck::check(const MatchFinder::MatchResult &Result) {
157 // Add any matches we locate to the list of things to be checked at the
158 // end of the translation unit.
159 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func");
160 const CXXRecordDecl *RD = nullptr;
161 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
162 RD = MD->getParent();
163 Overloads[RD].push_back(FD);
164}
165
166void NewDeleteOverloadsCheck::onEndOfTranslationUnit() {
167 // Walk over the list of declarations we've found to see if there is a
168 // corresponding overload at the same declaration context or within a base
169 // class. If there is not, add the element to the list of declarations to
170 // diagnose.
171 SmallVector<const FunctionDecl *, 4> Diagnose;
172 for (const auto &RP : Overloads) {
173 // We don't care about the CXXRecordDecl key in the map; we use it as a way
174 // to shard the overloads by declaration context to reduce the algorithmic
175 // complexity when searching for corresponding free store functions.
176 for (const auto *Overload : RP.second) {
177 const auto *Match = std::find_if(
178 RP.second.begin(), RP.second.end(), [&](const FunctionDecl *FD) {
179 if (FD == Overload)
180 return false;
181 // If the declaration contexts don't match, we don't
182 // need to check
183 // any further.
184 if (FD->getDeclContext() != Overload->getDeclContext())
185 return false;
186
187 // Since the declaration contexts match, see whether
188 // the current
189 // element is the corresponding operator.
190 if (!areCorrespondingOverloads(Overload, FD))
191 return false;
192
193 return true;
194 });
195
196 if (Match == RP.second.end()) {
197 // Check to see if there is a corresponding overload in a base class
198 // context. If there isn't, or if the overload is not a class member
199 // function, then we should diagnose.
200 const auto *MD = dyn_cast<CXXMethodDecl>(Overload);
201 if (!MD || !hasCorrespondingOverloadInBaseClass(MD))
202 Diagnose.push_back(Overload);
203 }
204 }
205 }
206
207 for (const auto *FD : Diagnose)
208 diag(FD->getLocation(), "declaration of %0 has no matching declaration "
209 "of '%1' at the same scope")
210 << FD << getOperatorName(getCorrespondingOverload(FD));
211}
212
213} // namespace misc
214} // namespace tidy
215} // namespace clang