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 QualType BaseReturnTy = BaseMD->getType()
41 ->castAs<FunctionType>()
44 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 QualType DTy = DerivedReturnTy->getPointeeType().getCanonicalType();
67 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;
104 if (!HasPublicAccess && !IsItself)
110 if (DerivedReturnTy.getLocalCVRQualifiers() !=
111 BaseReturnTy.getLocalCVRQualifiers())
116 if (DTy.isMoreQualifiedThan(BTy, *Context))
124 if (
const auto *Decayed = Type->getAs<DecayedType>())
125 return Decayed->getDecayedType();
131 const CXXMethodDecl *DerivedMD) {
132 unsigned NumParamA = BaseMD->getNumParams();
133 unsigned NumParamB = DerivedMD->getNumParams();
134 if (NumParamA != NumParamB)
137 for (
unsigned I = 0; I < NumParamA; I++) {
138 if (
getDecayedType(BaseMD->getParamDecl(I)->getType().getCanonicalType()) !=
140 DerivedMD->getParamDecl(I)->getType().getCanonicalType()))
149 const CXXMethodDecl *BaseMD,
150 const CXXMethodDecl *DerivedMD) {
151 if (BaseMD->isStatic() != DerivedMD->isStatic())
154 if (BaseMD->getType() == DerivedMD->getType())
169 const CXXMethodDecl *DerivedMD) {
170 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
171 E = DerivedMD->end_overridden_methods();
173 const CXXMethodDecl *OverriddenMD = *I;
174 if (BaseMD->getCanonicalDecl() == OverriddenMD->getCanonicalDecl())
181bool VirtualNearMissCheck::isPossibleToBeOverridden(
182 const CXXMethodDecl *BaseMD) {
183 auto [Iter, Inserted] = PossibleMap.try_emplace(BaseMD);
187 bool IsPossible = !BaseMD->isImplicit() && !isa<CXXConstructorDecl>(BaseMD) &&
188 !isa<CXXDestructorDecl>(BaseMD) && BaseMD->isVirtual() &&
189 !BaseMD->isOverloadedOperator() &&
190 !isa<CXXConversionDecl>(BaseMD);
191 Iter->second = IsPossible;
195bool VirtualNearMissCheck::isOverriddenByDerivedClass(
196 const CXXMethodDecl *BaseMD,
const CXXRecordDecl *DerivedRD) {
197 auto Key = std::make_pair(BaseMD, DerivedRD);
198 auto Iter = OverriddenMap.find(Key);
199 if (Iter != OverriddenMap.end())
202 bool IsOverridden =
false;
203 for (
const CXXMethodDecl *DerivedMD : DerivedRD->methods()) {
212 OverriddenMap[
Key] = IsOverridden;
219 unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
220 cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
221 isOverloadedOperator())))
227 const auto *DerivedMD = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
230 const ASTContext *Context = Result.Context;
232 const auto *DerivedRD = DerivedMD->getParent()->getDefinition();
235 for (
const auto &BaseSpec : DerivedRD->bases()) {
236 if (
const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
237 for (
const auto *BaseMD : BaseRD->methods()) {
238 if (!isPossibleToBeOverridden(BaseMD))
241 if (isOverriddenByDerivedClass(BaseMD, DerivedRD))
244 unsigned EditDistance = BaseMD->getName().edit_distance(
245 DerivedMD->getName(), EditDistanceThreshold);
246 if (EditDistance > 0 && EditDistance <= EditDistanceThreshold) {
249 auto Range = CharSourceRange::getTokenRange(
250 SourceRange(DerivedMD->getLocation()));
252 bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
253 !DerivedMD->isTemplateInstantiation();
255 diag(DerivedMD->getBeginLoc(),
256 "method '%0' has a similar name and the same signature as "
257 "virtual method '%1'; did you mean to override it?")
258 << DerivedMD->getQualifiedNameAsString()
259 << BaseMD->getQualifiedNameAsString();
261 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)