11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
21 SourceType = SourceType.getNonReferenceType();
22 DestType = DestType.getNonReferenceType();
23 while (SourceType->isPointerType() && DestType->isPointerType()) {
24 SourceType = SourceType->getPointeeType();
25 DestType = DestType->getPointeeType();
26 if (SourceType.isConstQualified() && !DestType.isConstQualified())
32 void ProTypeCstyleCastCheck::registerMatchers(MatchFinder *
Finder) {
33 if (!getLangOpts().CPlusPlus)
37 cStyleCastExpr(unless(isInTemplateInstantiation())).bind(
"cast"),
this);
40 void ProTypeCstyleCastCheck::check(
const MatchFinder::MatchResult &
Result) {
41 const auto *MatchedCast = Result.Nodes.getNodeAs<CStyleCastExpr>(
"cast");
43 if (MatchedCast->getCastKind() == CK_BitCast ||
44 MatchedCast->getCastKind() == CK_LValueBitCast ||
45 MatchedCast->getCastKind() == CK_IntegralToPointer ||
46 MatchedCast->getCastKind() == CK_PointerToIntegral ||
47 MatchedCast->getCastKind() == CK_ReinterpretMemberPointer) {
48 diag(MatchedCast->getLocStart(),
49 "do not use C-style cast to convert between unrelated types");
53 QualType SourceType = MatchedCast->getSubExpr()->getType();
55 if (MatchedCast->getCastKind() == CK_BaseToDerived) {
56 const auto *SourceDecl = SourceType->getPointeeCXXRecordDecl();
58 SourceDecl = SourceType->getAsCXXRecordDecl();
62 if (SourceDecl->isPolymorphic()) {
66 StringRef DestTypeString = Lexer::getSourceText(
67 CharSourceRange::getTokenRange(
68 MatchedCast->getLParenLoc().getLocWithOffset(1),
69 MatchedCast->getRParenLoc().getLocWithOffset(-1)),
70 *Result.SourceManager, Result.Context->getLangOpts());
72 auto diag_builder = diag(
73 MatchedCast->getLocStart(),
74 "do not use C-style cast to downcast from a base to a derived class; "
75 "use dynamic_cast instead");
78 MatchedCast->getSubExprAsWritten()->IgnoreImpCasts();
79 std::string CastText = (
"dynamic_cast<" + DestTypeString +
">").str();
80 if (!isa<ParenExpr>(SubExpr)) {
81 CastText.push_back(
'(');
82 diag_builder << FixItHint::CreateInsertion(
83 Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0,
84 *Result.SourceManager,
85 Result.Context->getLangOpts()),
88 auto ParenRange = CharSourceRange::getTokenRange(
89 MatchedCast->getLParenLoc(), MatchedCast->getRParenLoc());
90 diag_builder << FixItHint::CreateReplacement(ParenRange, CastText);
93 MatchedCast->getLocStart(),
94 "do not use C-style cast to downcast from a base to a derived class");
99 if (MatchedCast->getCastKind() == CK_NoOp &&
101 diag(MatchedCast->getLocStart(),
102 "do not use C-style cast to cast away constness");
static bool needsConstCast(QualType SourceType, QualType DestType)
std::unique_ptr< ast_matchers::MatchFinder > Finder