1 //===--- ParentVirtualCallCheck.cpp - clang-tidy---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ParentVirtualCallCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Tooling/FixIt.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
19 using namespace clang::ast_matchers;
25 using BasesVector = llvm::SmallVector<const CXXRecordDecl *, 5>;
27 static bool isParentOf(const CXXRecordDecl &Parent,
28 const CXXRecordDecl &ThisClass) {
29 if (Parent.getCanonicalDecl() == ThisClass.getCanonicalDecl())
31 const CXXRecordDecl *ParentCanonicalDecl = Parent.getCanonicalDecl();
32 return ThisClass.bases_end() !=
33 llvm::find_if(ThisClass.bases(), [=](const CXXBaseSpecifier &Base) {
34 auto *BaseDecl = Base.getType()->getAsCXXRecordDecl();
36 return ParentCanonicalDecl == BaseDecl->getCanonicalDecl();
40 static BasesVector getParentsByGrandParent(const CXXRecordDecl &GrandParent,
41 const CXXRecordDecl &ThisClass,
42 const CXXMethodDecl &MemberDecl) {
44 for (const auto &Base : ThisClass.bases()) {
45 const auto *BaseDecl = Base.getType()->getAsCXXRecordDecl();
46 const CXXMethodDecl *ActualMemberDecl =
47 MemberDecl.getCorrespondingMethodInClass(BaseDecl);
48 if (!ActualMemberDecl)
50 // TypePtr is the nearest base class to ThisClass between ThisClass and
51 // GrandParent, where MemberDecl is overridden. TypePtr is the class the
52 // check proposes to fix to.
54 ActualMemberDecl->getThisType(ActualMemberDecl->getASTContext())
56 const CXXRecordDecl *RecordDeclType = TypePtr->getPointeeCXXRecordDecl();
57 assert(RecordDeclType && "TypePtr is not a pointer to CXXRecordDecl!");
58 if (RecordDeclType->getCanonicalDecl()->isDerivedFrom(&GrandParent))
59 Result.emplace_back(RecordDeclType);
65 static std::string getNameAsString(const NamedDecl *Decl) {
67 llvm::raw_string_ostream OS(QualName);
68 PrintingPolicy PP(Decl->getASTContext().getPrintingPolicy());
69 PP.SuppressUnwrittenScope = true;
70 Decl->printQualifiedName(OS, PP);
74 // Returns E as written in the source code. Used to handle 'using' and
75 // 'typedef'ed names of grand-parent classes.
76 static std::string getExprAsString(const clang::Expr &E,
77 clang::ASTContext &AC) {
78 std::string Text = tooling::fixit::getText(E, AC).str();
82 [](char C) { return std::isspace(static_cast<unsigned char>(C)); }),
87 void ParentVirtualCallCheck::registerMatchers(MatchFinder *Finder) {
90 callee(memberExpr(hasDescendant(implicitCastExpr(
91 hasImplicitDestinationType(pointsTo(
92 type(anything()).bind("castToType"))),
93 hasSourceExpression(cxxThisExpr(hasType(
94 type(anything()).bind("thisType")))))))
96 callee(cxxMethodDecl(isVirtual()))),
100 void ParentVirtualCallCheck::check(const MatchFinder::MatchResult &Result) {
101 const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
104 if (!Member->getQualifier())
107 const auto *MemberDecl = cast<CXXMethodDecl>(Member->getMemberDecl());
109 const auto *ThisTypePtr = Result.Nodes.getNodeAs<PointerType>("thisType");
112 const auto *ThisType = ThisTypePtr->getPointeeCXXRecordDecl();
115 const auto *CastToTypePtr = Result.Nodes.getNodeAs<Type>("castToType");
116 assert(CastToTypePtr);
118 const auto *CastToType = CastToTypePtr->getAsCXXRecordDecl();
121 if (isParentOf(*CastToType, *ThisType))
124 const BasesVector Parents =
125 getParentsByGrandParent(*CastToType, *ThisType, *MemberDecl);
130 std::string ParentsStr;
131 ParentsStr.reserve(30 * Parents.size());
132 for (const CXXRecordDecl *Parent : Parents) {
133 if (!ParentsStr.empty())
134 ParentsStr.append(" or ");
135 ParentsStr.append("'").append(getNameAsString(Parent)).append("'");
138 assert(Member->getQualifierLoc().getSourceRange().getBegin().isValid());
139 auto Diag = diag(Member->getQualifierLoc().getSourceRange().getBegin(),
140 "qualified name '%0' refers to a member overridden "
141 "in subclass%1; did you mean %2?")
142 << getExprAsString(*Member, *Result.Context)
143 << (Parents.size() > 1 ? "es" : "") << ParentsStr;
145 // Propose a fix if there's only one parent class...
146 if (Parents.size() == 1 &&
147 // ...unless parent class is templated
148 !isa<ClassTemplateSpecializationDecl>(Parents.front()))
149 Diag << FixItHint::CreateReplacement(
150 Member->getQualifierLoc().getSourceRange(),
151 getNameAsString(Parents.front()) + "::");
154 } // namespace bugprone