1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
//===--- AvoidCStyleCastsCheck.cpp - clang-tidy -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AvoidCStyleCastsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace google {
namespace readability {
void
AvoidCStyleCastsCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
Finder->addMatcher(
cStyleCastExpr(
// Filter out (EnumType)IntegerLiteral construct, which is generated
// for non-type template arguments of enum types.
// FIXME: Remove this once this is fixed in the AST.
unless(hasParent(substNonTypeTemplateParmExpr())),
// Avoid matches in template instantiations.
unless(isInTemplateInstantiation())).bind("cast"),
this);
}
static bool needsConstCast(QualType SourceType, QualType DestType) {
SourceType = SourceType.getNonReferenceType();
DestType = DestType.getNonReferenceType();
while (SourceType->isPointerType() && DestType->isPointerType()) {
SourceType = SourceType->getPointeeType();
DestType = DestType->getPointeeType();
if (SourceType.isConstQualified() && !DestType.isConstQualified())
return true;
}
return false;
}
static bool pointedTypesAreEqual(QualType SourceType, QualType DestType) {
SourceType = SourceType.getNonReferenceType();
DestType = DestType.getNonReferenceType();
while (SourceType->isPointerType() && DestType->isPointerType()) {
SourceType = SourceType->getPointeeType();
DestType = DestType->getPointeeType();
}
return SourceType.getUnqualifiedType() == DestType.getUnqualifiedType();
}
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *CastExpr = Result.Nodes.getNodeAs<CStyleCastExpr>("cast");
auto ParenRange = CharSourceRange::getTokenRange(CastExpr->getLParenLoc(),
CastExpr->getRParenLoc());
// Ignore casts in macros.
if (ParenRange.getBegin().isMacroID() || ParenRange.getEnd().isMacroID())
return;
// Casting to void is an idiomatic way to mute "unused variable" and similar
// warnings.
if (CastExpr->getTypeAsWritten()->isVoidType())
return;
QualType SourceType = CastExpr->getSubExprAsWritten()->getType();
QualType DestType = CastExpr->getTypeAsWritten();
if (SourceType == DestType) {
diag(CastExpr->getLocStart(), "redundant cast to the same type")
<< FixItHint::CreateRemoval(ParenRange);
return;
}
SourceType = SourceType.getCanonicalType();
DestType = DestType.getCanonicalType();
if (SourceType == DestType) {
diag(CastExpr->getLocStart(),
"possibly redundant cast between typedefs of the same type");
return;
}
// The rest of this check is only relevant to C++.
if (!Result.Context->getLangOpts().CPlusPlus)
return;
// Ignore code inside extern "C" {} blocks.
if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
.empty())
return;
// Ignore code in .c files and headers included from them, even if they are
// compiled as C++.
if (getCurrentMainFile().endswith(".c"))
return;
// Ignore code in .c files #included in other files (which shouldn't be done,
// but people still do this for test and other purposes).
SourceManager &SM = *Result.SourceManager;
if (SM.getFilename(SM.getSpellingLoc(CastExpr->getLocStart())).endswith(".c"))
return;
// Leave type spelling exactly as it was (unlike
// getTypeAsWritten().getAsString() which would spell enum types 'enum X').
StringRef DestTypeString =
Lexer::getSourceText(CharSourceRange::getTokenRange(
CastExpr->getLParenLoc().getLocWithOffset(1),
CastExpr->getRParenLoc().getLocWithOffset(-1)),
SM, Result.Context->getLangOpts());
auto diag_builder =
diag(CastExpr->getLocStart(), "C-style casts are discouraged. %0");
auto ReplaceWithCast = [&](StringRef CastType) {
diag_builder << ("Use " + CastType).str();
const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
std::string CastText = (CastType + "<" + DestTypeString + ">").str();
if (!isa<ParenExpr>(SubExpr)) {
CastText.push_back('(');
diag_builder << FixItHint::CreateInsertion(
Lexer::getLocForEndOfToken(SubExpr->getLocEnd(), 0, SM,
Result.Context->getLangOpts()),
")");
}
diag_builder << FixItHint::CreateReplacement(ParenRange, CastText);
};
// Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
switch (CastExpr->getCastKind()) {
case CK_NoOp:
if (needsConstCast(SourceType, DestType) &&
pointedTypesAreEqual(SourceType, DestType)) {
ReplaceWithCast("const_cast");
return;
}
if (DestType->isReferenceType() &&
(SourceType.getNonReferenceType() ==
DestType.getNonReferenceType().withConst() ||
SourceType.getNonReferenceType() == DestType.getNonReferenceType())) {
ReplaceWithCast("const_cast");
return;
}
// FALLTHROUGH
case clang::CK_IntegralCast:
// Convert integral and no-op casts between builtin types and enums to
// static_cast. A cast from enum to integer may be unnecessary, but it's
// still retained.
if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
(DestType->isBuiltinType() || DestType->isEnumeralType())) {
ReplaceWithCast("static_cast");
return;
}
break;
case CK_BitCast:
// FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
if (!needsConstCast(SourceType, DestType)) {
ReplaceWithCast("reinterpret_cast");
return;
}
break;
default:
break;
}
diag_builder << "Use static_cast/const_cast/reinterpret_cast";
}
} // namespace readability
} // namespace google
} // namespace tidy
} // namespace clang
|