11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
19static bool sameBasicType(
const ParmVarDecl *Lhs,
const ParmVarDecl *Rhs) {
23 .getNonReferenceType()
24 .getUnqualifiedType() == Rhs->getType()
26 .getNonReferenceType()
27 .getUnqualifiedType();
30static bool namesCollide(
const CXXMethodDecl &Lhs,
const CXXMethodDecl &Rhs) {
31 if (Lhs.getNameAsString() != Rhs.getNameAsString())
33 if (Lhs.isConst() != Rhs.isConst())
35 if (Lhs.getNumParams() != Rhs.getNumParams())
37 for (
unsigned int It = 0; It < Lhs.getNumParams(); ++It)
38 if (!
sameBasicType(Lhs.getParamDecl(It), Rhs.getParamDecl(It)))
45AST_MATCHER(CXXMethodDecl, nameCollidesWithMethodInBase) {
46 const CXXRecordDecl *DerivedClass = Node.getParent();
47 for (
const auto &Base : DerivedClass->bases()) {
49 Stack.push_back(&Base);
50 while (!Stack.empty()) {
51 const CXXBaseSpecifier *CurrentBaseSpec = Stack.back();
54 if (CurrentBaseSpec->getAccessSpecifier() == AccessSpecifier::AS_private)
57 const CXXRecordDecl *CurrentRecord =
58 CurrentBaseSpec->getType()->getAsCXXRecordDecl();
64 if (CurrentRecord->isInStdNamespace())
67 for (
const auto &BaseMethod : CurrentRecord->methods()) {
69 const ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
70 Builder->setBinding(
"base_method", DynTypedNode::create(*BaseMethod));
75 for (
const auto &SubBase : CurrentRecord->bases())
76 Stack.push_back(&SubBase);
83 return Node.getDescribedFunctionTemplate() !=
nullptr;
93 MatchFinder *Finder) {
96 unless(anyOf(isOutOfLine(), isStaticStorageClass(), isImplicit(),
97 cxxConstructorDecl(), isOverride(), isPrivate(),
100 isTemplate(), ast_matchers::isTemplateInstantiation(),
101 ast_matchers::isExplicitTemplateSpecialization())),
102 ofClass(cxxRecordDecl(isDerivedFrom(cxxRecordDecl()))
103 .bind(
"derived_class")),
104 nameCollidesWithMethodInBase())
105 .bind(
"shadowing_method"),
110 const MatchFinder::MatchResult &Result) {
111 const auto *ShadowingMethod =
112 Result.Nodes.getNodeAs<CXXMethodDecl>(
"shadowing_method");
113 const auto *DerivedClass =
114 Result.Nodes.getNodeAs<CXXRecordDecl>(
"derived_class");
115 const auto *BaseMethod = Result.Nodes.getNodeAs<CXXMethodDecl>(
"base_method");
117 if (!ShadowingMethod || !DerivedClass || !BaseMethod)
118 llvm_unreachable(
"Required binding not found");
120 diag(ShadowingMethod->getBeginLoc(),
121 "'%0' shadows method with the same name in class %1")
122 << ShadowingMethod->getQualifiedNameAsString() << BaseMethod->getParent();
123 diag(BaseMethod->getBeginLoc(),
"previous definition of %0 is here",
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DerivedMethodShadowingBaseMethodCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static bool namesCollide(const CXXMethodDecl &Lhs, const CXXMethodDecl &Rhs)
static bool sameBasicType(const ParmVarDecl *Lhs, const ParmVarDecl *Rhs)
AST_MATCHER(BinaryOperator, isRelationalOperator)