clang-tools 23.0.0git
AvoidCStyleCastCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Lex/Lexer.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::modernize {
18
19void AvoidCStyleCastCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
20 Finder->addMatcher(
21 cStyleCastExpr(
22 // Filter out (EnumType)IntegerLiteral construct, which is generated
23 // for non-type template arguments of enum types.
24 // FIXME: Remove this once this is fixed in the AST.
25 unless(hasParent(substNonTypeTemplateParmExpr())))
26 .bind("cast"),
27 this);
28
29 Finder->addMatcher(
30 cxxFunctionalCastExpr(
31 hasDestinationType(hasCanonicalType(anyOf(
32 builtinType(), references(qualType()), pointsTo(qualType())))),
33 unless(
34 hasSourceExpression(anyOf(cxxConstructExpr(), initListExpr()))))
35 .bind("cast"),
36 this);
37}
38
39static bool needsConstCast(QualType SourceType, QualType DestType) {
40 while ((SourceType->isPointerType() && DestType->isPointerType()) ||
41 (SourceType->isReferenceType() && DestType->isReferenceType())) {
42 SourceType = SourceType->getPointeeType();
43 DestType = DestType->getPointeeType();
44 if (SourceType.isConstQualified() && !DestType.isConstQualified()) {
45 return (SourceType->isPointerType() == DestType->isPointerType()) &&
46 (SourceType->isReferenceType() == DestType->isReferenceType());
47 }
48 }
49 return false;
50}
51
52static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2) {
53 while ((T1->isPointerType() && T2->isPointerType()) ||
54 (T1->isReferenceType() && T2->isReferenceType())) {
55 T1 = T1->getPointeeType();
56 T2 = T2->getPointeeType();
57 }
58 return T1.getUnqualifiedType() == T2.getUnqualifiedType();
59}
60
61static CharSourceRange getReplaceRange(const ExplicitCastExpr *Expr) {
62 if (const auto *CastExpr = dyn_cast<CStyleCastExpr>(Expr))
63 return CharSourceRange::getCharRange(
64 CastExpr->getLParenLoc(),
65 CastExpr->getSubExprAsWritten()->getBeginLoc());
66 if (const auto *CastExpr = dyn_cast<CXXFunctionalCastExpr>(Expr))
67 return CharSourceRange::getCharRange(CastExpr->getBeginLoc(),
68 CastExpr->getLParenLoc());
69 llvm_unreachable("Unsupported CastExpr");
70}
71
72static StringRef getDestTypeString(const SourceManager &SM,
73 const LangOptions &LangOpts,
74 const ExplicitCastExpr *Expr) {
75 SourceLocation BeginLoc;
76 SourceLocation EndLoc;
77
78 if (const auto *CastExpr = dyn_cast<CStyleCastExpr>(Expr)) {
79 BeginLoc = CastExpr->getLParenLoc().getLocWithOffset(1);
80 EndLoc = CastExpr->getRParenLoc().getLocWithOffset(-1);
81 } else if (const auto *CastExpr = dyn_cast<CXXFunctionalCastExpr>(Expr)) {
82 BeginLoc = CastExpr->getBeginLoc();
83 EndLoc = CastExpr->getLParenLoc().getLocWithOffset(-1);
84 } else {
85 llvm_unreachable("Unsupported CastExpr");
86 }
87
88 return Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
89 SM, LangOpts);
90}
91
92static bool sameTypeAsWritten(QualType X, QualType Y) {
93 if (X.getCanonicalType() != Y.getCanonicalType())
94 return false;
95
96 auto TC = X->getTypeClass();
97 if (TC != Y->getTypeClass())
98 return false;
99
100 switch (TC) {
101 case Type::Typedef:
102 return declaresSameEntity(cast<TypedefType>(X)->getDecl(),
103 cast<TypedefType>(Y)->getDecl());
104 case Type::Pointer:
105 return sameTypeAsWritten(cast<PointerType>(X)->getPointeeType(),
106 cast<PointerType>(Y)->getPointeeType());
107 case Type::RValueReference:
108 case Type::LValueReference:
109 return sameTypeAsWritten(cast<ReferenceType>(X)->getPointeeType(),
110 cast<ReferenceType>(Y)->getPointeeType());
111 default:
112 return true;
113 }
114}
115
116void AvoidCStyleCastCheck::check(const MatchFinder::MatchResult &Result) {
117 const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>("cast");
118
119 // Ignore casts in macros.
120 if (CastExpr->getExprLoc().isMacroID())
121 return;
122
123 // Casting to void is an idiomatic way to mute "unused variable" and similar
124 // warnings.
125 if (CastExpr->getCastKind() == CK_ToVoid)
126 return;
127
128 auto IsFunction = [](QualType T) {
129 T = T.getCanonicalType().getNonReferenceType();
130 return T->isFunctionType() || T->isFunctionPointerType() ||
131 T->isMemberFunctionPointerType();
132 };
133
134 const QualType DestTypeAsWritten =
135 CastExpr->getTypeAsWritten().getUnqualifiedType();
136 const QualType SourceTypeAsWritten =
137 CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
138 const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
139 const QualType DestType = DestTypeAsWritten.getCanonicalType();
140
141 CharSourceRange ReplaceRange = getReplaceRange(CastExpr);
142
143 const bool FnToFnCast =
144 IsFunction(SourceTypeAsWritten) && IsFunction(DestTypeAsWritten);
145
146 const bool ConstructorCast = !CastExpr->getTypeAsWritten().hasQualifiers() &&
147 DestTypeAsWritten->isRecordType() &&
148 !DestTypeAsWritten->isElaboratedTypeSpecifier();
149
150 if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast) {
151 // Function pointer/reference casts may be needed to resolve ambiguities in
152 // case of overloaded functions, so detection of redundant casts is trickier
153 // in this case. Don't emit "redundant cast" warnings for function
154 // pointer/reference types.
155 if (sameTypeAsWritten(SourceTypeAsWritten, DestTypeAsWritten)) {
156 diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
157 << FixItHint::CreateRemoval(ReplaceRange);
158 return;
159 }
160 }
161
162 // The rest of this check is only relevant to C++.
163 // We also disable it for Objective-C++.
164 if (!getLangOpts().CPlusPlus || getLangOpts().ObjC)
165 return;
166 // Ignore code inside extern "C" {} blocks.
167 if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
168 .empty())
169 return;
170 // Ignore code in .c files and headers included from them, even if they are
171 // compiled as C++.
172 if (getCurrentMainFile().ends_with(".c"))
173 return;
174
175 SourceManager &SM = *Result.SourceManager;
176
177 // Ignore code in .c files #included in other files (which shouldn't be done,
178 // but people still do this for test and other purposes).
179 if (SM.getFilename(SM.getSpellingLoc(CastExpr->getBeginLoc()))
180 .ends_with(".c"))
181 return;
182
183 // Leave type spelling exactly as it was (unlike
184 // getTypeAsWritten().getAsString() which would spell enum types 'enum X').
185 StringRef DestTypeString = getDestTypeString(SM, getLangOpts(), CastExpr);
186
187 auto Diag =
188 diag(CastExpr->getBeginLoc(), "C-style casts are discouraged; use %0");
189
190 auto ReplaceWithCast = [&](std::string CastText) {
191 const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
192 if (!isa<ParenExpr>(SubExpr) && !isa<CXXFunctionalCastExpr>(CastExpr)) {
193 CastText.push_back('(');
194 Diag << FixItHint::CreateInsertion(
195 Lexer::getLocForEndOfToken(SubExpr->getEndLoc(), 0, SM,
196 getLangOpts()),
197 ")");
198 }
199 Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
200 };
201 auto ReplaceWithNamedCast = [&](StringRef CastType) {
202 Diag << CastType;
203 ReplaceWithCast((CastType + "<" + DestTypeString + ">").str());
204 };
205 auto ReplaceWithConstructorCall = [&]() {
206 Diag << "constructor call syntax";
207 // FIXME: Validate DestTypeString, maybe.
208 ReplaceWithCast(DestTypeString.str());
209 };
210 // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
211 switch (CastExpr->getCastKind()) {
212 case CK_FunctionToPointerDecay:
213 ReplaceWithNamedCast("static_cast");
214 return;
215 case CK_ConstructorConversion:
216 if (ConstructorCast)
217 ReplaceWithConstructorCall();
218 else
219 ReplaceWithNamedCast("static_cast");
220 return;
221 case CK_NoOp:
222 if (FnToFnCast) {
223 ReplaceWithNamedCast("static_cast");
224 return;
225 }
226 if (SourceType == DestType) {
227 Diag << "static_cast (if needed, the cast may be redundant)";
228 ReplaceWithCast(("static_cast<" + DestTypeString + ">").str());
229 return;
230 }
231 if (needsConstCast(SourceType, DestType) &&
232 pointedUnqualifiedTypesAreEqual(SourceType, DestType)) {
233 ReplaceWithNamedCast("const_cast");
234 return;
235 }
236 if (ConstructorCast) {
237 ReplaceWithConstructorCall();
238 return;
239 }
240 if (DestType->isReferenceType()) {
241 const QualType Dest = DestType.getNonReferenceType();
242 const QualType Source = SourceType.getNonReferenceType();
243 if (Source == Dest.withConst() ||
244 SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
245 ReplaceWithNamedCast("const_cast");
246 return;
247 }
248 break;
249 }
250 if (DestType->isVoidPointerType() && SourceType->isPointerType() &&
251 !SourceType->getPointeeType()->isPointerType()) {
252 ReplaceWithNamedCast("reinterpret_cast");
253 return;
254 }
255
256 [[fallthrough]];
257 case CK_IntegralCast:
258 // Convert integral and no-op casts between builtin types and enums to
259 // static_cast. A cast from enum to integer may be unnecessary, but it's
260 // still retained.
261 if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
262 (DestType->isBuiltinType() || DestType->isEnumeralType())) {
263 ReplaceWithNamedCast("static_cast");
264 return;
265 }
266 break;
267 case CK_BitCast:
268 // FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
269 if (!needsConstCast(SourceType, DestType)) {
270 if (SourceType->isVoidPointerType())
271 ReplaceWithNamedCast("static_cast");
272 else
273 ReplaceWithNamedCast("reinterpret_cast");
274 return;
275 }
276 break;
277 case CK_BaseToDerived:
278 if (!needsConstCast(SourceType, DestType)) {
279 ReplaceWithNamedCast("static_cast");
280 return;
281 }
282 break;
283 default:
284 break;
285 }
286
287 Diag << "static_cast/const_cast/reinterpret_cast";
288}
289
290} // namespace clang::tidy::modernize
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static ClangTidyModuleRegistry::Add< ModernizeModule > X("modernize-module", "Add modernize checks.")
static bool needsConstCast(QualType SourceType, QualType DestType)
static StringRef getDestTypeString(const SourceManager &SM, const LangOptions &LangOpts, const ExplicitCastExpr *Expr)
static bool sameTypeAsWritten(QualType X, QualType Y)
static CharSourceRange getReplaceRange(const ExplicitCastExpr *Expr)
static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2)