10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Lex/Lexer.h" 18 namespace cppcoreguidelines {
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->getBeginLoc(),
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(
68 MatchedCast->getLParenLoc().getLocWithOffset(1),
69 MatchedCast->getRParenLoc().getLocWithOffset(-1)),
70 *Result.SourceManager, getLangOpts());
72 auto diag_builder = diag(
73 MatchedCast->getBeginLoc(),
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->getEndLoc(), 0,
84 *Result.SourceManager, getLangOpts()),
88 MatchedCast->getLParenLoc(), MatchedCast->getRParenLoc());
89 diag_builder << FixItHint::CreateReplacement(ParenRange, CastText);
92 MatchedCast->getBeginLoc(),
93 "do not use C-style cast to downcast from a base to a derived class");
98 if (MatchedCast->getCastKind() == CK_NoOp &&
100 diag(MatchedCast->getBeginLoc(),
101 "do not use C-style cast to cast away constness");
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
static bool needsConstCast(QualType SourceType, QualType DestType)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//