10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
17static bool sameBasicType(
const ParmVarDecl *Lhs,
const ParmVarDecl *Rhs) {
21 .getNonReferenceType()
22 .getUnqualifiedType() == Rhs->getType()
24 .getNonReferenceType()
25 .getUnqualifiedType();
28static bool namesCollide(
const CXXMethodDecl &Lhs,
const CXXMethodDecl &Rhs) {
29 if (Lhs.getNameAsString() != Rhs.getNameAsString())
31 if (Lhs.isConst() != Rhs.isConst())
33 if (Lhs.getNumParams() != Rhs.getNumParams())
35 for (
unsigned int It = 0; It < Lhs.getNumParams(); ++It)
36 if (!
sameBasicType(Lhs.getParamDecl(It), Rhs.getParamDecl(It)))
43AST_MATCHER(CXXMethodDecl, nameCollidesWithMethodInBase) {
44 const CXXRecordDecl *DerivedClass = Node.getParent();
45 for (
const auto &Base : DerivedClass->bases()) {
47 Stack.push_back(&Base);
48 while (!Stack.empty()) {
49 const CXXBaseSpecifier *CurrentBaseSpec = Stack.back();
52 if (CurrentBaseSpec->getAccessSpecifier() == AccessSpecifier::AS_private)
55 const CXXRecordDecl *CurrentRecord =
56 CurrentBaseSpec->getType()->getAsCXXRecordDecl();
62 if (CurrentRecord->isInStdNamespace())
65 for (
const auto &BaseMethod : CurrentRecord->methods()) {
67 const ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
68 Builder->setBinding(
"base_method", DynTypedNode::create(*BaseMethod));
73 for (
const auto &SubBase : CurrentRecord->bases())
74 Stack.push_back(&SubBase);
82AST_MATCHER(CXXMethodDecl, isOutOfLine) {
return Node.isOutOfLine(); }
91 MatchFinder *Finder) {
94 unless(anyOf(isOutOfLine(), isStaticStorageClass(), isImplicit(),
95 cxxConstructorDecl(), isOverride(), isPrivate(),
98 ast_matchers::isTemplateInstantiation(),
99 ast_matchers::isExplicitTemplateSpecialization())),
100 ofClass(cxxRecordDecl(isDerivedFrom(cxxRecordDecl()))
101 .bind(
"derived_class")),
102 nameCollidesWithMethodInBase())
103 .bind(
"shadowing_method"),
108 const MatchFinder::MatchResult &Result) {
109 const auto *ShadowingMethod =
110 Result.Nodes.getNodeAs<CXXMethodDecl>(
"shadowing_method");
111 const auto *DerivedClass =
112 Result.Nodes.getNodeAs<CXXRecordDecl>(
"derived_class");
113 const auto *BaseMethod = Result.Nodes.getNodeAs<CXXMethodDecl>(
"base_method");
115 if (!ShadowingMethod || !DerivedClass || !BaseMethod)
116 llvm_unreachable(
"Required binding not found");
118 diag(ShadowingMethod->getBeginLoc(),
119 "'%0' shadows method with the same name in class %1")
120 << ShadowingMethod->getQualifiedNameAsString() << BaseMethod->getParent();
121 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)