2018-04-07 04:02:50 +08:00
|
|
|
//===--- ParentVirtualCallCheck.cpp - clang-tidy---------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-04-07 04:02:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ParentVirtualCallCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Tooling/FixIt.h"
|
2018-04-13 15:46:27 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-04-07 04:02:50 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2018-04-07 04:39:23 +08:00
|
|
|
#include <algorithm>
|
2018-04-07 04:02:50 +08:00
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace bugprone {
|
|
|
|
|
|
|
|
using BasesVector = llvm::SmallVector<const CXXRecordDecl *, 5>;
|
|
|
|
|
|
|
|
static bool isParentOf(const CXXRecordDecl &Parent,
|
|
|
|
const CXXRecordDecl &ThisClass) {
|
|
|
|
if (Parent.getCanonicalDecl() == ThisClass.getCanonicalDecl())
|
|
|
|
return true;
|
2018-04-13 16:43:47 +08:00
|
|
|
const CXXRecordDecl *ParentCanonicalDecl = Parent.getCanonicalDecl();
|
2018-04-13 15:46:27 +08:00
|
|
|
return ThisClass.bases_end() !=
|
|
|
|
llvm::find_if(ThisClass.bases(), [=](const CXXBaseSpecifier &Base) {
|
|
|
|
auto *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
|
|
|
assert(BaseDecl);
|
2018-04-13 16:43:47 +08:00
|
|
|
return ParentCanonicalDecl == BaseDecl->getCanonicalDecl();
|
2018-04-13 15:46:27 +08:00
|
|
|
});
|
2018-04-07 04:02:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static BasesVector getParentsByGrandParent(const CXXRecordDecl &GrandParent,
|
|
|
|
const CXXRecordDecl &ThisClass,
|
|
|
|
const CXXMethodDecl &MemberDecl) {
|
|
|
|
BasesVector Result;
|
|
|
|
for (const auto &Base : ThisClass.bases()) {
|
|
|
|
const auto *BaseDecl = Base.getType()->getAsCXXRecordDecl();
|
|
|
|
const CXXMethodDecl *ActualMemberDecl =
|
|
|
|
MemberDecl.getCorrespondingMethodInClass(BaseDecl);
|
|
|
|
if (!ActualMemberDecl)
|
|
|
|
continue;
|
|
|
|
// TypePtr is the nearest base class to ThisClass between ThisClass and
|
|
|
|
// GrandParent, where MemberDecl is overridden. TypePtr is the class the
|
|
|
|
// check proposes to fix to.
|
2019-01-11 10:12:31 +08:00
|
|
|
const Type *TypePtr = ActualMemberDecl->getThisType().getTypePtr();
|
2018-04-07 04:02:50 +08:00
|
|
|
const CXXRecordDecl *RecordDeclType = TypePtr->getPointeeCXXRecordDecl();
|
|
|
|
assert(RecordDeclType && "TypePtr is not a pointer to CXXRecordDecl!");
|
|
|
|
if (RecordDeclType->getCanonicalDecl()->isDerivedFrom(&GrandParent))
|
|
|
|
Result.emplace_back(RecordDeclType);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNameAsString(const NamedDecl *Decl) {
|
|
|
|
std::string QualName;
|
|
|
|
llvm::raw_string_ostream OS(QualName);
|
|
|
|
PrintingPolicy PP(Decl->getASTContext().getPrintingPolicy());
|
|
|
|
PP.SuppressUnwrittenScope = true;
|
|
|
|
Decl->printQualifiedName(OS, PP);
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns E as written in the source code. Used to handle 'using' and
|
|
|
|
// 'typedef'ed names of grand-parent classes.
|
|
|
|
static std::string getExprAsString(const clang::Expr &E,
|
|
|
|
clang::ASTContext &AC) {
|
|
|
|
std::string Text = tooling::fixit::getText(E, AC).str();
|
2018-04-07 05:00:18 +08:00
|
|
|
Text.erase(
|
2018-04-13 15:46:27 +08:00
|
|
|
llvm::remove_if(
|
|
|
|
Text,
|
2020-05-02 21:34:53 +08:00
|
|
|
[](char C) { return llvm::isSpace(static_cast<unsigned char>(C)); }),
|
2018-04-07 05:00:18 +08:00
|
|
|
Text.end());
|
2018-04-07 04:02:50 +08:00
|
|
|
return Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParentVirtualCallCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(
|
2019-11-12 23:15:56 +08:00
|
|
|
traverse(
|
2020-12-11 07:52:35 +08:00
|
|
|
TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
cxxMemberCallExpr(
|
|
|
|
callee(memberExpr(hasDescendant(implicitCastExpr(
|
|
|
|
hasImplicitDestinationType(pointsTo(
|
|
|
|
type(anything()).bind("castToType"))),
|
|
|
|
hasSourceExpression(cxxThisExpr(hasType(
|
|
|
|
type(anything()).bind("thisType")))))))
|
|
|
|
.bind("member")),
|
|
|
|
callee(cxxMethodDecl(isVirtual())))),
|
2018-04-07 04:02:50 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParentVirtualCallCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
|
|
|
|
assert(Member);
|
|
|
|
|
|
|
|
if (!Member->getQualifier())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto *MemberDecl = cast<CXXMethodDecl>(Member->getMemberDecl());
|
|
|
|
|
|
|
|
const auto *ThisTypePtr = Result.Nodes.getNodeAs<PointerType>("thisType");
|
|
|
|
assert(ThisTypePtr);
|
|
|
|
|
|
|
|
const auto *ThisType = ThisTypePtr->getPointeeCXXRecordDecl();
|
|
|
|
assert(ThisType);
|
|
|
|
|
|
|
|
const auto *CastToTypePtr = Result.Nodes.getNodeAs<Type>("castToType");
|
|
|
|
assert(CastToTypePtr);
|
|
|
|
|
|
|
|
const auto *CastToType = CastToTypePtr->getAsCXXRecordDecl();
|
|
|
|
assert(CastToType);
|
|
|
|
|
|
|
|
if (isParentOf(*CastToType, *ThisType))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const BasesVector Parents =
|
|
|
|
getParentsByGrandParent(*CastToType, *ThisType, *MemberDecl);
|
|
|
|
|
|
|
|
if (Parents.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string ParentsStr;
|
|
|
|
ParentsStr.reserve(30 * Parents.size());
|
|
|
|
for (const CXXRecordDecl *Parent : Parents) {
|
|
|
|
if (!ParentsStr.empty())
|
|
|
|
ParentsStr.append(" or ");
|
|
|
|
ParentsStr.append("'").append(getNameAsString(Parent)).append("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Member->getQualifierLoc().getSourceRange().getBegin().isValid());
|
|
|
|
auto Diag = diag(Member->getQualifierLoc().getSourceRange().getBegin(),
|
|
|
|
"qualified name '%0' refers to a member overridden "
|
2021-02-27 03:10:25 +08:00
|
|
|
"in %plural{1:subclass|:subclasses}1; did you mean %2?")
|
2018-04-07 04:02:50 +08:00
|
|
|
<< getExprAsString(*Member, *Result.Context)
|
2021-02-27 03:10:25 +08:00
|
|
|
<< static_cast<unsigned>(Parents.size()) << ParentsStr;
|
2018-04-07 04:02:50 +08:00
|
|
|
|
|
|
|
// Propose a fix if there's only one parent class...
|
|
|
|
if (Parents.size() == 1 &&
|
|
|
|
// ...unless parent class is templated
|
|
|
|
!isa<ClassTemplateSpecializationDecl>(Parents.front()))
|
|
|
|
Diag << FixItHint::CreateReplacement(
|
|
|
|
Member->getQualifierLoc().getSourceRange(),
|
|
|
|
getNameAsString(Parents.front()) + "::");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bugprone
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|