11#include "clang/AST/ASTContext.h"
12#include "clang/AST/CXXInheritance.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
23 return MD->size_overridden_methods() > 0 ||
MD->hasAttr<OverrideAttr>();
32 const CXXMethodDecl *BaseMD,
33 const CXXMethodDecl *DerivedMD) {
34 const QualType BaseReturnTy = BaseMD->getType()
35 ->castAs<FunctionType>()
38 const QualType DerivedReturnTy = DerivedMD->getType()
39 ->castAs<FunctionType>()
43 if (DerivedReturnTy->isDependentType() || BaseReturnTy->isDependentType())
47 if (ASTContext::hasSameType(DerivedReturnTy, BaseReturnTy))
53 if (!(BaseReturnTy->isPointerType() && DerivedReturnTy->isPointerType()) &&
54 !(BaseReturnTy->isReferenceType() && DerivedReturnTy->isReferenceType()))
60 const QualType DTy = DerivedReturnTy->getPointeeType().getCanonicalType();
61 const QualType BTy = BaseReturnTy->getPointeeType().getCanonicalType();
63 const CXXRecordDecl *DRD = DTy->getAsCXXRecordDecl();
64 const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl();
65 if (DRD ==
nullptr || BRD ==
nullptr)
68 if (!DRD->hasDefinition() || !BRD->hasDefinition())
74 if (!ASTContext::hasSameUnqualifiedType(DTy, BTy)) {
76 CXXBasePaths Paths(
true,
true,
80 if (!DRD->isDerivedFrom(BRD, Paths))
84 if (Paths.isAmbiguous(
85 ASTContext::getCanonicalType(BTy).getUnqualifiedType()))
92 DRD->getCanonicalDecl() == DerivedMD->getParent()->getCanonicalDecl();
93 bool HasPublicAccess =
false;
94 for (
const auto &Path : Paths)
95 if (Path.Access == AS_public)
96 HasPublicAccess =
true;
97 if (!HasPublicAccess && !IsItself)
103 if (DerivedReturnTy.getLocalCVRQualifiers() !=
104 BaseReturnTy.getLocalCVRQualifiers())
109 if (DTy.isMoreQualifiedThan(BTy, *Context))
117 if (
const auto *Decayed = Type->getAs<DecayedType>())
118 return Decayed->getDecayedType();
124 const CXXMethodDecl *DerivedMD) {
125 const unsigned NumParamA = BaseMD->getNumParams();
126 const unsigned NumParamB = DerivedMD->getNumParams();
127 if (NumParamA != NumParamB)
130 for (
unsigned I = 0; I < NumParamA; I++)
131 if (
getDecayedType(BaseMD->getParamDecl(I)->getType().getCanonicalType()) !=
133 DerivedMD->getParamDecl(I)->getType().getCanonicalType()))
141 const CXXMethodDecl *BaseMD,
142 const CXXMethodDecl *DerivedMD) {
143 if (BaseMD->isStatic() != DerivedMD->isStatic())
146 if (BaseMD->getType() == DerivedMD->getType())
161 const CXXMethodDecl *DerivedMD) {
162 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
163 E = DerivedMD->end_overridden_methods();
165 const CXXMethodDecl *OverriddenMD = *I;
166 if (BaseMD->getCanonicalDecl() == OverriddenMD->getCanonicalDecl())
173bool VirtualNearMissCheck::isPossibleToBeOverridden(
174 const CXXMethodDecl *BaseMD) {
175 auto [Iter, Inserted] = PossibleMap.try_emplace(BaseMD);
179 const bool IsPossible =
180 !BaseMD->isImplicit() && !isa<CXXConstructorDecl>(BaseMD) &&
181 !isa<CXXDestructorDecl>(BaseMD) && BaseMD->isVirtual() &&
182 !BaseMD->isOverloadedOperator() && !isa<CXXConversionDecl>(BaseMD);
183 Iter->second = IsPossible;
187bool VirtualNearMissCheck::isOverriddenByDerivedClass(
188 const CXXMethodDecl *BaseMD,
const CXXRecordDecl *DerivedRD) {
189 const std::pair Key(BaseMD, DerivedRD);
190 const auto Iter = OverriddenMap.find(Key);
191 if (Iter != OverriddenMap.end())
194 bool IsOverridden =
false;
195 for (
const CXXMethodDecl *DerivedMD : DerivedRD->methods()) {
204 OverriddenMap[
Key] = IsOverridden;
211 unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
212 cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
213 isOverloadedOperator())))
219 const auto *DerivedMD = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
222 const ASTContext *Context = Result.Context;
224 const auto *DerivedRD = DerivedMD->getParent()->getDefinition();
227 for (
const auto &BaseSpec : DerivedRD->bases()) {
228 if (
const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
229 for (
const auto *BaseMD : BaseRD->methods()) {
230 if (!isPossibleToBeOverridden(BaseMD))
233 if (isOverriddenByDerivedClass(BaseMD, DerivedRD))
236 const unsigned EditDistance = BaseMD->getName().edit_distance(
237 DerivedMD->getName(), EditDistanceThreshold);
238 if (EditDistance > 0 && EditDistance <= EditDistanceThreshold &&
241 const auto Range = CharSourceRange::getTokenRange(
242 SourceRange(DerivedMD->getLocation()));
244 const bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
245 !DerivedMD->isTemplateInstantiation();
247 diag(DerivedMD->getBeginLoc(),
248 "method '%0' has a similar name and the same signature as "
249 "virtual method '%1'; did you mean to override it?")
250 << DerivedMD->getQualifiedNameAsString()
251 << BaseMD->getQualifiedNameAsString();
253 Diag << FixItHint::CreateReplacement(Range, BaseMD->getName());
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for Markdown output.")
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.