11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/CXXInheritance.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Lex/Lexer.h"
16 using namespace clang::ast_matchers;
22 AST_MATCHER(CXXMethodDecl, isStatic) {
return Node.isStatic(); }
25 return Node.isOverloadedOperator();
30 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
39 const CXXMethodDecl *BaseMD,
40 const CXXMethodDecl *DerivedMD) {
41 QualType BaseReturnTy = BaseMD->getType()
42 ->getAs<FunctionType>()
45 QualType DerivedReturnTy = DerivedMD->getType()
46 ->getAs<FunctionType>()
50 if (DerivedReturnTy->isDependentType() || BaseReturnTy->isDependentType())
54 if (Context->hasSameType(DerivedReturnTy, BaseReturnTy))
60 if (!(BaseReturnTy->isPointerType() && DerivedReturnTy->isPointerType()) &&
61 !(BaseReturnTy->isReferenceType() && DerivedReturnTy->isReferenceType()))
67 QualType DTy = DerivedReturnTy->getPointeeType().getCanonicalType();
68 QualType BTy = BaseReturnTy->getPointeeType().getCanonicalType();
70 const CXXRecordDecl *DRD = DTy->getAsCXXRecordDecl();
71 const CXXRecordDecl *BRD = BTy->getAsCXXRecordDecl();
72 if (DRD ==
nullptr || BRD ==
nullptr)
75 if (!DRD->hasDefinition() || !BRD->hasDefinition())
81 if (!Context->hasSameUnqualifiedType(DTy, BTy)) {
83 CXXBasePaths Paths(
true,
true,
87 if (!DRD->isDerivedFrom(BRD, Paths))
91 if (Paths.isAmbiguous(Context->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))
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())
181 bool VirtualNearMissCheck::isPossibleToBeOverridden(
182 const CXXMethodDecl *BaseMD) {
183 auto Iter = PossibleMap.find(BaseMD);
184 if (Iter != PossibleMap.end())
187 bool IsPossible = !BaseMD->isImplicit() && !isa<CXXConstructorDecl>(BaseMD) &&
188 !isa<CXXDestructorDecl>(BaseMD) && BaseMD->isVirtual() &&
189 !BaseMD->isOverloadedOperator() &&
190 !isa<CXXConversionDecl>(BaseMD);
191 PossibleMap[BaseMD] = IsPossible;
195 bool 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;
216 void VirtualNearMissCheck::registerMatchers(MatchFinder *
Finder) {
217 if (!getLangOpts().CPlusPlus)
222 unless(anyOf(isOverride(), isImplicit(), cxxConstructorDecl(),
223 cxxDestructorDecl(), cxxConversionDecl(), isStatic(),
224 isOverloadedOperator())))
229 void VirtualNearMissCheck::check(
const MatchFinder::MatchResult &Result) {
230 const auto *DerivedMD = Result.Nodes.getNodeAs<CXXMethodDecl>(
"method");
233 const ASTContext *
Context = Result.Context;
235 const auto *DerivedRD = DerivedMD->getParent()->getDefinition();
238 for (
const auto &BaseSpec : DerivedRD->bases()) {
239 if (
const auto *BaseRD = BaseSpec.getType()->getAsCXXRecordDecl()) {
240 for (
const auto *BaseMD : BaseRD->methods()) {
241 if (!isPossibleToBeOverridden(BaseMD))
244 if (isOverriddenByDerivedClass(BaseMD, DerivedRD))
247 unsigned EditDistance =
248 BaseMD->getName().edit_distance(DerivedMD->getName());
249 if (EditDistance > 0 && EditDistance <= EditDistanceThreshold) {
252 auto Range = CharSourceRange::getTokenRange(
253 SourceRange(DerivedMD->getLocation()));
255 bool ApplyFix = !BaseMD->isTemplateInstantiation() &&
256 !DerivedMD->isTemplateInstantiation();
258 diag(DerivedMD->getLocStart(),
259 "method '%0' has a similar name and the same signature as "
260 "virtual method '%1'; did you mean to override it?")
261 << DerivedMD->getQualifiedNameAsString()
262 << BaseMD->getQualifiedNameAsString();
264 Diag << FixItHint::CreateReplacement(
Range, BaseMD->getName());
std::unique_ptr< ast_matchers::MatchFinder > Finder
static bool checkParamTypes(const CXXMethodDecl *BaseMD, const CXXMethodDecl *DerivedMD)
static QualType getDecayedType(QualType Type)
std::vector< HeaderHandle > Path
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
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)
CharSourceRange Range
SourceRange for the file name.
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. ...
ClangTidyContext & Context
AST_MATCHER(VarDecl, isAsm)