11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Lex/Lexer.h"
16 using namespace clang::ast_matchers;
21 namespace readability {
23 void AvoidCStyleCastsCheck::registerMatchers(
24 ast_matchers::MatchFinder *
Finder) {
30 unless(hasParent(substNonTypeTemplateParmExpr())),
32 unless(isInTemplateInstantiation()))
38 while ((SourceType->isPointerType() && DestType->isPointerType()) ||
39 (SourceType->isReferenceType() && DestType->isReferenceType())) {
40 SourceType = SourceType->getPointeeType();
41 DestType = DestType->getPointeeType();
42 if (SourceType.isConstQualified() && !DestType.isConstQualified()) {
43 return (SourceType->isPointerType() == DestType->isPointerType()) &&
44 (SourceType->isReferenceType() == DestType->isReferenceType());
51 while ((T1->isPointerType() && T2->isPointerType()) ||
52 (T1->isReferenceType() && T2->isReferenceType())) {
53 T1 = T1->getPointeeType();
54 T2 = T2->getPointeeType();
56 return T1.getUnqualifiedType() == T2.getUnqualifiedType();
59 void AvoidCStyleCastsCheck::check(
const MatchFinder::MatchResult &Result) {
60 const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>(
"cast");
63 if (CastExpr->getExprLoc().isMacroID())
68 if (CastExpr->getCastKind() == CK_ToVoid)
71 auto isFunction = [](QualType T) {
72 T = T.getCanonicalType().getNonReferenceType();
73 return T->isFunctionType() || T->isFunctionPointerType() ||
74 T->isMemberFunctionPointerType();
77 const QualType DestTypeAsWritten =
78 CastExpr->getTypeAsWritten().getUnqualifiedType();
79 const QualType SourceTypeAsWritten =
80 CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
81 const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
82 const QualType DestType = DestTypeAsWritten.getCanonicalType();
84 auto ReplaceRange = CharSourceRange::getCharRange(
85 CastExpr->getLParenLoc(), CastExpr->getSubExprAsWritten()->getLocStart());
88 isFunction(SourceTypeAsWritten) && isFunction(DestTypeAsWritten);
90 if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast) {
95 if (SourceTypeAsWritten == DestTypeAsWritten) {
96 diag(CastExpr->getLocStart(),
"redundant cast to the same type")
97 << FixItHint::CreateRemoval(ReplaceRange);
103 if (!getLangOpts().CPlusPlus)
106 if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
111 if (getCurrentMainFile().endswith(
".c"))
114 SourceManager &
SM = *Result.SourceManager;
118 if (SM.getFilename(SM.getSpellingLoc(CastExpr->getLocStart())).endswith(
".c"))
123 StringRef DestTypeString =
124 Lexer::getSourceText(CharSourceRange::getTokenRange(
125 CastExpr->getLParenLoc().getLocWithOffset(1),
126 CastExpr->getRParenLoc().getLocWithOffset(-1)),
130 diag(CastExpr->getLocStart(),
"C-style casts are discouraged; use %0");
132 auto ReplaceWithCast = [&](std::string CastText) {
133 const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
134 if (!isa<ParenExpr>(SubExpr)) {
135 CastText.push_back(
'(');
136 Diag << FixItHint::CreateInsertion(
137 Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0,
SM,
141 Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
143 auto ReplaceWithNamedCast = [&](StringRef CastType) {
145 ReplaceWithCast((CastType +
"<" + DestTypeString +
">").str());
149 switch (CastExpr->getCastKind()) {
150 case CK_FunctionToPointerDecay:
151 ReplaceWithNamedCast(
"static_cast");
153 case CK_ConstructorConversion:
154 if (!CastExpr->getTypeAsWritten().hasQualifiers() &&
155 DestTypeAsWritten->isRecordType() &&
156 !DestTypeAsWritten->isElaboratedTypeSpecifier()) {
157 Diag <<
"constructor call syntax";
159 ReplaceWithCast(DestTypeString.str());
161 ReplaceWithNamedCast(
"static_cast");
166 ReplaceWithNamedCast(
"static_cast");
169 if (SourceType == DestType) {
170 Diag <<
"static_cast (if needed, the cast may be redundant)";
171 ReplaceWithCast((
"static_cast<" + DestTypeString +
">").str());
176 ReplaceWithNamedCast(
"const_cast");
179 if (DestType->isReferenceType()) {
180 QualType Dest = DestType.getNonReferenceType();
181 QualType Source = SourceType.getNonReferenceType();
182 if (Source == Dest.withConst() ||
183 SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
184 ReplaceWithNamedCast(
"const_cast");
190 case clang::CK_IntegralCast:
194 if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
195 (DestType->isBuiltinType() || DestType->isEnumeralType())) {
196 ReplaceWithNamedCast(
"static_cast");
203 if (SourceType->isVoidPointerType())
204 ReplaceWithNamedCast(
"static_cast");
206 ReplaceWithNamedCast(
"reinterpret_cast");
214 Diag <<
"static_cast/const_cast/reinterpret_cast";
std::unique_ptr< ast_matchers::MatchFinder > Finder
static bool needsConstCast(QualType SourceType, QualType DestType)
static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2)