10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
21 switch (
Node.getOverloadedOperator()) {
35 if (
Node.isVariadic())
42 return Node.getNumParams() > 1;
43 if (
Node.getNumParams() == 1)
50 if (
Node.getNumParams() <= 1 ||
Node.getNumParams() > 2)
53 const auto *FPT =
Node.getType()->castAs<FunctionProtoType>();
54 ASTContext &Ctx =
Node.getASTContext();
55 if (Ctx.getLangOpts().SizedDeallocation &&
56 Ctx.hasSameType(FPT->getParamType(1), Ctx.getSizeType()))
62OverloadedOperatorKind getCorrespondingOverload(
const FunctionDecl *FD) {
63 switch (FD->getOverloadedOperator()) {
71 return OO_Array_Delete;
75 llvm_unreachable(
"Not an overloaded allocation operator");
78const char *getOperatorName(OverloadedOperatorKind
K) {
83 return "operator new";
85 return "operator delete";
87 return "operator new[]";
89 return "operator delete[]";
91 llvm_unreachable(
"Not an overloaded allocation operator");
94bool areCorrespondingOverloads(
const FunctionDecl *LHS,
95 const FunctionDecl *RHS) {
96 return RHS->getOverloadedOperator() == getCorrespondingOverload(LHS);
99bool hasCorrespondingOverloadInBaseClass(
const CXXMethodDecl *MD,
100 const CXXRecordDecl *RD =
nullptr) {
103 for (
const auto *BMD : RD->methods())
104 if (BMD->isOverloadedOperator() && BMD->getAccess() != AS_private &&
105 areCorrespondingOverloads(MD, BMD))
111 RD = MD->getParent();
114 for (
const auto &BS : RD->bases()) {
117 if (BS.getType()->isDependentType())
119 if (
const auto *BaseRD = BS.getType()->getAsCXXRecordDecl())
120 if (hasCorrespondingOverloadInBaseClass(MD, BaseRD))
139 functionDecl(unless(anyOf(isImplicit(), isPlacementOverload(),
140 isDeleted(), cxxMethodDecl(isPrivate()))),
141 anyOf(hasOverloadedOperatorName(
"new"),
142 hasOverloadedOperatorName(
"new[]"),
143 hasOverloadedOperatorName(
"delete"),
144 hasOverloadedOperatorName(
"delete[]")))
152 const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
153 const CXXRecordDecl *RD =
nullptr;
154 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD))
155 RD = MD->getParent();
156 Overloads[RD].push_back(FD);
164 SmallVector<const FunctionDecl *, 4> Diagnose;
165 for (
const auto &RP : Overloads) {
169 for (
const auto *Overload : RP.second) {
171 std::find_if(RP.second.begin(), RP.second.end(),
172 [&Overload](
const FunctionDecl *FD) {
177 if (FD->getDeclContext() != Overload->getDeclContext())
182 if (!areCorrespondingOverloads(Overload, FD))
188 if (Match == RP.second.end()) {
192 const auto *MD = dyn_cast<CXXMethodDecl>(Overload);
193 if (!MD || !hasCorrespondingOverloadInBaseClass(MD))
194 Diagnose.push_back(Overload);
199 for (
const auto *FD : Diagnose)
200 diag(FD->getLocation(),
"declaration of %0 has no matching declaration "
201 "of '%1' at the same scope")
202 << FD << getOperatorName(getCorrespondingOverload(FD));
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void onEndOfTranslationUnit() override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(Expr, isMacroID)