clang-tools 24.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 bool needsLeadingSpace(CharSourceRange Range, StringRef ReplacementText,
73 const SourceManager &SM,
74 const LangOptions &LangOpts) {
75 if (ReplacementText.empty())
76 return false;
77
78 const SourceLocation Begin = Range.getBegin();
79 if (Begin.isInvalid() || Begin.isMacroID())
80 return false;
81
82 const auto BeginInfo = SM.getDecomposedLoc(Begin);
83 bool Invalid = false;
84 StringRef Buffer = SM.getBufferData(BeginInfo.first, &Invalid);
85 if (Invalid || BeginInfo.second == 0)
86 return false;
87
88 return Lexer::isAsciiIdentifierContinueChar(Buffer[BeginInfo.second - 1],
89 LangOpts) &&
90 Lexer::isAsciiIdentifierContinueChar(ReplacementText.front(),
91 LangOpts);
92}
93
94static StringRef getDestTypeString(const SourceManager &SM,
95 const LangOptions &LangOpts,
96 const ExplicitCastExpr *Expr) {
97 SourceLocation BeginLoc;
98 SourceLocation EndLoc;
99
100 if (const auto *CastExpr = dyn_cast<CStyleCastExpr>(Expr)) {
101 BeginLoc = CastExpr->getLParenLoc().getLocWithOffset(1);
102 EndLoc = CastExpr->getRParenLoc().getLocWithOffset(-1);
103 } else if (const auto *CastExpr = dyn_cast<CXXFunctionalCastExpr>(Expr)) {
104 BeginLoc = CastExpr->getBeginLoc();
105 EndLoc = CastExpr->getLParenLoc().getLocWithOffset(-1);
106 } else {
107 llvm_unreachable("Unsupported CastExpr");
108 }
109
110 return Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
111 SM, LangOpts);
112}
113
114static bool sameTypeAsWritten(QualType X, QualType Y) {
115 if (X.getCanonicalType() != Y.getCanonicalType())
116 return false;
117
118 const auto TC = X->getTypeClass();
119 if (TC != Y->getTypeClass())
120 return false;
121
122 switch (TC) {
123 case Type::Typedef:
124 return declaresSameEntity(cast<TypedefType>(X)->getDecl(),
125 cast<TypedefType>(Y)->getDecl());
126 case Type::Pointer:
127 return sameTypeAsWritten(cast<PointerType>(X)->getPointeeType(),
128 cast<PointerType>(Y)->getPointeeType());
129 case Type::RValueReference:
130 case Type::LValueReference:
131 return sameTypeAsWritten(cast<ReferenceType>(X)->getPointeeType(),
132 cast<ReferenceType>(Y)->getPointeeType());
133 default:
134 return true;
135 }
136}
137
138void AvoidCStyleCastCheck::check(const MatchFinder::MatchResult &Result) {
139 const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>("cast");
140
141 // Ignore casts in macros.
142 if (CastExpr->getExprLoc().isMacroID())
143 return;
144
145 // Casting to void is an idiomatic way to mute "unused variable" and similar
146 // warnings.
147 if (CastExpr->getCastKind() == CK_ToVoid)
148 return;
149
150 const auto IsFunction = [](QualType T) {
151 T = T.getCanonicalType().getNonReferenceType();
152 return T->isFunctionType() || T->isFunctionPointerType() ||
153 T->isMemberFunctionPointerType();
154 };
155
156 const QualType DestTypeAsWritten =
157 CastExpr->getTypeAsWritten().getUnqualifiedType();
158 const QualType SourceTypeAsWritten =
159 CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
160 const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
161 const QualType DestType = DestTypeAsWritten.getCanonicalType();
162
163 CharSourceRange ReplaceRange = getReplaceRange(CastExpr);
164
165 const bool FnToFnCast =
166 IsFunction(SourceTypeAsWritten) && IsFunction(DestTypeAsWritten);
167
168 const bool ConstructorCast = !CastExpr->getTypeAsWritten().hasQualifiers() &&
169 DestTypeAsWritten->isRecordType() &&
170 !DestTypeAsWritten->isElaboratedTypeSpecifier();
171
172 // Function pointer/reference casts may be needed to resolve ambiguities in
173 // case of overloaded functions, so detection of redundant casts is trickier
174 // in this case. Don't emit "redundant cast" warnings for function
175 // pointer/reference types.
176 if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast &&
177 sameTypeAsWritten(SourceTypeAsWritten, DestTypeAsWritten)) {
178 diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
179 << FixItHint::CreateRemoval(ReplaceRange);
180 return;
181 }
182
183 // The rest of this check is only relevant to C++.
184 // We also disable it for Objective-C++.
185 if (!getLangOpts().CPlusPlus || getLangOpts().ObjC)
186 return;
187 // Ignore code inside extern "C" {} blocks.
188 if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
189 .empty())
190 return;
191 // Ignore code in .c files and headers included from them, even if they are
192 // compiled as C++.
193 if (getCurrentMainFile().ends_with(".c"))
194 return;
195
196 SourceManager &SM = *Result.SourceManager;
197
198 // Ignore code in .c files #included in other files (which shouldn't be done,
199 // but people still do this for test and other purposes).
200 if (SM.getFilename(SM.getSpellingLoc(CastExpr->getBeginLoc()))
201 .ends_with(".c"))
202 return;
203
204 // Leave type spelling exactly as it was (unlike
205 // getTypeAsWritten().getAsString() which would spell enum types 'enum X').
206 StringRef DestTypeString = getDestTypeString(SM, getLangOpts(), CastExpr);
207
208 auto Diag =
209 diag(CastExpr->getBeginLoc(), "C-style casts are discouraged; use %0");
210
211 auto ReplaceWithCast = [&](std::string CastText) {
212 const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
213 if (!isa<ParenExpr>(SubExpr) && !isa<CXXFunctionalCastExpr>(CastExpr)) {
214 CastText.push_back('(');
215 Diag << FixItHint::CreateInsertion(
216 Lexer::getLocForEndOfToken(SubExpr->getEndLoc(), 0, SM,
217 getLangOpts()),
218 ")");
219 }
220 if (needsLeadingSpace(ReplaceRange, CastText, SM, getLangOpts()))
221 CastText.insert(CastText.begin(), ' ');
222 Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
223 };
224 const auto ReplaceWithNamedCast = [&](StringRef CastType) {
225 Diag << CastType;
226 ReplaceWithCast((CastType + "<" + DestTypeString + ">").str());
227 };
228 const auto ReplaceWithConstructorCall = [&]() {
229 Diag << "constructor call syntax";
230 // FIXME: Validate DestTypeString, maybe.
231 ReplaceWithCast(DestTypeString.str());
232 };
233 // Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
234 switch (CastExpr->getCastKind()) {
235 case CK_FunctionToPointerDecay:
236 ReplaceWithNamedCast("static_cast");
237 return;
238 case CK_ConstructorConversion:
239 if (ConstructorCast)
240 ReplaceWithConstructorCall();
241 else
242 ReplaceWithNamedCast("static_cast");
243 return;
244 case CK_NoOp:
245 if (FnToFnCast) {
246 ReplaceWithNamedCast("static_cast");
247 return;
248 }
249 if (SourceType == DestType) {
250 Diag << "static_cast (if needed, the cast may be redundant)";
251 ReplaceWithCast(("static_cast<" + DestTypeString + ">").str());
252 return;
253 }
254 if (needsConstCast(SourceType, DestType) &&
255 pointedUnqualifiedTypesAreEqual(SourceType, DestType)) {
256 ReplaceWithNamedCast("const_cast");
257 return;
258 }
259 if (ConstructorCast) {
260 ReplaceWithConstructorCall();
261 return;
262 }
263 if (DestType->isReferenceType()) {
264 const QualType Dest = DestType.getNonReferenceType();
265 const QualType Source = SourceType.getNonReferenceType();
266 if (Source == Dest.withConst() ||
267 SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
268 ReplaceWithNamedCast("const_cast");
269 return;
270 }
271 break;
272 }
273 if (DestType->isVoidPointerType() && SourceType->isPointerType() &&
274 !SourceType->getPointeeType()->isPointerType()) {
275 ReplaceWithNamedCast("reinterpret_cast");
276 return;
277 }
278
279 [[fallthrough]];
280 case CK_IntegralCast:
281 // Convert integral and no-op casts between builtin types and enums to
282 // static_cast. A cast from enum to integer may be unnecessary, but it's
283 // still retained.
284 if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
285 (DestType->isBuiltinType() || DestType->isEnumeralType())) {
286 ReplaceWithNamedCast("static_cast");
287 return;
288 }
289 break;
290 case CK_BitCast:
291 // FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
292 if (!needsConstCast(SourceType, DestType)) {
293 if (SourceType->isVoidPointerType())
294 ReplaceWithNamedCast("static_cast");
295 else
296 ReplaceWithNamedCast("reinterpret_cast");
297 return;
298 }
299 break;
300 case CK_BaseToDerived:
301 if (!needsConstCast(SourceType, DestType)) {
302 ReplaceWithNamedCast("static_cast");
303 return;
304 }
305 break;
306 default:
307 break;
308 }
309
310 Diag << "static_cast/const_cast/reinterpret_cast";
311}
312
313} // namespace clang::tidy::modernize
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static bool needsLeadingSpace(CharSourceRange Range, StringRef ReplacementText, const SourceManager &SM, const LangOptions &LangOpts)
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)