clang-tools 22.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 clang::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 return Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
88 SM, LangOpts);
89}
90
91static bool sameTypeAsWritten(QualType X, QualType Y) {
92 if (X.getCanonicalType() != Y.getCanonicalType())
93 return false;
94
95 auto TC = X->getTypeClass();
96 if (TC != Y->getTypeClass())
97 return false;
98
99 switch (TC) {
100 case Type::Typedef:
101 return declaresSameEntity(cast<TypedefType>(X)->getDecl(),
102 cast<TypedefType>(Y)->getDecl());
103 case Type::Pointer:
104 return sameTypeAsWritten(cast<PointerType>(X)->getPointeeType(),
105 cast<PointerType>(Y)->getPointeeType());
106 case Type::RValueReference:
107 case Type::LValueReference:
108 return sameTypeAsWritten(cast<ReferenceType>(X)->getPointeeType(),
109 cast<ReferenceType>(Y)->getPointeeType());
110 default:
111 return true;
112 }
113}
114
115void AvoidCStyleCastCheck::check(const MatchFinder::MatchResult &Result) {
116 const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>("cast");
117
118 // Ignore casts in macros.
119 if (CastExpr->getExprLoc().isMacroID())
120 return;
121
122 // Casting to void is an idiomatic way to mute "unused variable" and similar
123 // warnings.
124 if (CastExpr->getCastKind() == CK_ToVoid)
125 return;
126
127 auto IsFunction = [](QualType T) {
128 T = T.getCanonicalType().getNonReferenceType();
129 return T->isFunctionType() || T->isFunctionPointerType() ||
130 T->isMemberFunctionPointerType();
131 };
132
133 const QualType DestTypeAsWritten =
134 CastExpr->getTypeAsWritten().getUnqualifiedType();
135 const QualType SourceTypeAsWritten =
136 CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
137 const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
138 const QualType DestType = DestTypeAsWritten.getCanonicalType();
139
140 CharSourceRange ReplaceRange = getReplaceRange(CastExpr);
141
142 const bool FnToFnCast =
143 IsFunction(SourceTypeAsWritten) && IsFunction(DestTypeAsWritten);
144
145 const bool ConstructorCast = !CastExpr->getTypeAsWritten().hasQualifiers() &&
146 DestTypeAsWritten->isRecordType() &&
147 !DestTypeAsWritten->isElaboratedTypeSpecifier();
148
149 if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast) {
150 // Function pointer/reference casts may be needed to resolve ambiguities in
151 // case of overloaded functions, so detection of redundant casts is trickier
152 // in this case. Don't emit "redundant cast" warnings for function
153 // pointer/reference types.
154 if (sameTypeAsWritten(SourceTypeAsWritten, DestTypeAsWritten)) {
155 diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
156 << FixItHint::CreateRemoval(ReplaceRange);
157 return;
158 }
159 }
160
161 // The rest of this check is only relevant to C++.
162 // We also disable it for Objective-C++.
163 if (!getLangOpts().CPlusPlus || getLangOpts().ObjC)
164 return;
165 // Ignore code inside extern "C" {} blocks.
166 if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
167 .empty())
168 return;
169 // Ignore code in .c files and headers included from them, even if they are
170 // compiled as C++.
171 if (getCurrentMainFile().ends_with(".c"))
172 return;
173
174 SourceManager &SM = *Result.SourceManager;
175
176 // Ignore code in .c files #included in other files (which shouldn't be done,
177 // but people still do this for test and other purposes).
178 if (SM.getFilename(SM.getSpellingLoc(CastExpr->getBeginLoc()))
179 .ends_with(".c"))
180 return;
181
182 // Leave type spelling exactly as it was (unlike
183 // getTypeAsWritten().getAsString() which would spell enum types 'enum X').
184 StringRef DestTypeString = getDestTypeString(SM, getLangOpts(), CastExpr);
185
186 auto Diag =
187 diag(CastExpr->getBeginLoc(), "C-style casts are discouraged; use %0");
188
189 auto ReplaceWithCast = [&](std::string CastText) {
190 const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
191 if (!isa<ParenExpr>(SubExpr) && !isa<CXXFunctionalCastExpr>(CastExpr)) {
192 CastText.push_back('(');
193 Diag << FixItHint::CreateInsertion(
194 Lexer::getLocForEndOfToken(SubExpr->getEndLoc(), 0, SM,
195 getLangOpts()),
196 ")");
197 }
198 Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
199 };
200 auto ReplaceWithNamedCast = [&](StringRef CastType) {
201 Diag << CastType;
202 ReplaceWithCast((CastType + "<" + DestTypeString + ">").str());
203 };
204 auto ReplaceWithConstructorCall = [&]() {
205 Diag << "constructor call syntax";
206 // FIXME: Validate DestTypeString, maybe.
207 ReplaceWithCast(DestTypeString.str());
208 };
209 // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
210 switch (CastExpr->getCastKind()) {
211 case CK_FunctionToPointerDecay:
212 ReplaceWithNamedCast("static_cast");
213 return;
214 case CK_ConstructorConversion:
215 if (ConstructorCast) {
216 ReplaceWithConstructorCall();
217 } else {
218 ReplaceWithNamedCast("static_cast");
219 }
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 clang::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 clang::CharSourceRange getReplaceRange(const ExplicitCastExpr *Expr)
static StringRef getDestTypeString(const SourceManager &SM, const LangOptions &LangOpts, const ExplicitCastExpr *Expr)
static bool sameTypeAsWritten(QualType X, QualType Y)
static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2)