10#include "clang/AST/ASTContext.h"
11#include "clang/AST/CXXInheritance.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
20AST_MATCHER(CXXMethodDecl, isStatic) {
return Node.isStatic(); }
23 return Node.isOverloadedOperator();
29 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
38 const CXXMethodDecl *BaseMD,
39 const CXXMethodDecl *DerivedMD) {
40 const QualType BaseReturnTy = BaseMD->getType()
41 ->castAs<FunctionType>()
44 const QualType DerivedReturnTy = DerivedMD->getType()
45 ->castAs<FunctionType>()
49 if (DerivedReturnTy->isDependentType() || BaseReturnTy->isDependentType())
53 if (ASTContext::hasSameType(DerivedReturnTy, BaseReturnTy))
59 if (!(BaseReturnTy->isPointerType() && DerivedReturnTy->isPointerType()) &&
60 !(BaseReturnTy->isReferenceType() && DerivedReturnTy->isReferenceType()))
66 const QualType DTy = DerivedReturnTy->getPointeeType().getCanonicalType();
67 const QualType BTy = BaseReturnTy->getPointeeType().getCanonicalType();
69 const CXXRecordDecl *DRD = DTy->getAsCXXRecordDecl();
70 const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl();
71 if (DRD ==
nullptr || BRD ==
nullptr)
74 if (!DRD->hasDefinition() || !BRD->hasDefinition())
80 if (!ASTContext::hasSameUnqualifiedType(DTy, BTy)) {
82 CXXBasePaths Paths(
true,
true,
86 if (!DRD->isDerivedFrom(BRD, Paths))
90 if (Paths.isAmbiguous(
91 ASTContext::getCanonicalType(BTy).getUnqualifiedType()))
98 DRD->getCanonicalDecl() == DerivedMD->getParent()->getCanonicalDecl();
99 bool HasPublicAccess =
false;
100 for (
const auto &Path : Paths)
101 if (Path.Access == AS_public)
102 HasPublicAccess =
true;
103 if (!HasPublicAccess && !IsItself)
109 if (DerivedReturnTy.getLocalCVRQualifiers() !=
110 BaseReturnTy.getLocalCVRQualifiers())
115 if (DTy.isMoreQualifiedThan(BTy, *Context))
123 if (
const auto *Decayed = Type->getAs<DecayedType>())
124 return Decayed->getDecayedType();
130 const CXXMethodDecl *DerivedMD) {
131 const unsigned NumParamA = BaseMD->getNumParams();
132 const unsigned NumParamB = DerivedMD->getNumParams();
133 if (NumParamA != NumParamB)
136 for (
unsigned I = 0; I < NumParamA; I++)
137 if (
getDecayedType(BaseMD->getParamDecl(I)->getType().getCanonicalType()) !=
139 DerivedMD->getParamDecl(I)->getType().getCanonicalType()))
147 const CXXMethodDecl *BaseMD,
148 const CXXMethodDecl *DerivedMD) {
149 if (BaseMD->isStatic() != DerivedMD->isStatic())
152 if (BaseMD->getType() == DerivedMD->getType())
167 const CXXMethodDecl *DerivedMD) {
168 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
169 E = DerivedMD->end_overridden_methods();
171 const CXXMethodDecl *OverriddenMD = *I;
172 if (BaseMD->getCanonicalDecl() == OverriddenMD->getCanonicalDecl())
179bool VirtualNearMissCheck::isPossibleToBeOverridden(
180 const CXXMethodDecl *BaseMD) {
181 auto [Iter, Inserted] = PossibleMap.try_emplace(BaseMD);
185 const bool IsPossible =
186 !BaseMD->isImplicit() && !isa<CXXConstructorDecl>(BaseMD) &&
187 !isa<CXXDestructorDecl>(BaseMD) && BaseMD->isVirtual() &&
188 !BaseMD->isOverloadedOperator() && !isa<CXXConversionDecl>(BaseMD);
189 Iter->second = IsPossible;
193bool VirtualNearMissCheck::isOverriddenByDerivedClass(
194 const CXXMethodDecl *BaseMD,
const CXXRecordDecl *DerivedRD) {
195 auto Key = std::make_pair(BaseMD, DerivedRD);
196 auto Iter = OverriddenMap.find(Key);
197 if (Iter != OverriddenMap.end())
200 bool IsOverridden =
false;
201 for (
const CXXMethodDecl *DerivedMD : DerivedRD->methods()) {
210 OverriddenMap[
Key] = IsOverridden;
217 unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
218 cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
219 isOverloadedOperator())))
225 const auto *DerivedMD = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
228 const ASTContext *Context = Result.Context;
230 const auto *DerivedRD = DerivedMD->getParent()->getDefinition();
233 for (
const auto &BaseSpec : DerivedRD->bases()) {
234 if (
const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
235 for (
const auto *BaseMD : BaseRD->methods()) {
236 if (!isPossibleToBeOverridden(BaseMD))
239 if (isOverriddenByDerivedClass(BaseMD, DerivedRD))
242 const unsigned EditDistance = BaseMD->getName().edit_distance(
243 DerivedMD->getName(), EditDistanceThreshold);
244 if (EditDistance > 0 && EditDistance <= EditDistanceThreshold) {
247 auto Range = CharSourceRange::getTokenRange(
248 SourceRange(DerivedMD->getLocation()));
250 const bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
251 !DerivedMD->isTemplateInstantiation();
253 diag(DerivedMD->getBeginLoc(),
254 "method '%0' has a similar name and the same signature as "
255 "virtual method '%1'; did you mean to override it?")
256 << DerivedMD->getQualifiedNameAsString()
257 << BaseMD->getQualifiedNameAsString();
259 Diag << FixItHint::CreateReplacement(Range, BaseMD->getName());
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static bool checkOverrideByDerivedMethod(const CXXMethodDecl *BaseMD, const CXXMethodDecl *DerivedMD)
Check whether BaseMD overrides DerivedMD.
static bool checkOverrideWithoutName(const ASTContext *Context, const CXXMethodDecl *BaseMD, const CXXMethodDecl *DerivedMD)
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
static bool checkParamTypes(const CXXMethodDecl *BaseMD, const CXXMethodDecl *DerivedMD)
static QualType getDecayedType(QualType Type)
static bool checkOverridingFunctionReturnType(const ASTContext *Context, const CXXMethodDecl *BaseMD, const CXXMethodDecl *DerivedMD)
Checks whether the return types are covariant, according to C++[class.virtual]p7.
AST_MATCHER(BinaryOperator, isRelationalOperator)