10
10
#include " MisplacedWideningCastCheck.h"
11
11
#include " clang/AST/ASTContext.h"
12
12
#include " clang/ASTMatchers/ASTMatchFinder.h"
13
- #include " llvm/ADT/DenseMap.h"
14
13
15
14
using namespace clang ::ast_matchers;
16
15
@@ -29,18 +28,19 @@ void MisplacedWideningCastCheck::storeOptions(
29
28
}
30
29
31
30
void MisplacedWideningCastCheck::registerMatchers (MatchFinder *Finder) {
32
- auto Calc = expr (anyOf (binaryOperator (anyOf (
33
- hasOperatorName (" +" ), hasOperatorName (" -" ),
34
- hasOperatorName (" *" ), hasOperatorName (" <<" ))),
35
- unaryOperator (hasOperatorName (" ~" ))),
36
- hasType (isInteger ()))
37
- .bind (" Calc" );
38
-
39
- auto ExplicitCast =
31
+ const auto Calc =
32
+ expr (anyOf (binaryOperator (
33
+ anyOf (hasOperatorName (" +" ), hasOperatorName (" -" ),
34
+ hasOperatorName (" *" ), hasOperatorName (" <<" ))),
35
+ unaryOperator (hasOperatorName (" ~" ))),
36
+ hasType (isInteger ()))
37
+ .bind (" Calc" );
38
+
39
+ const auto ExplicitCast =
40
40
explicitCastExpr (hasDestinationType (isInteger ()), has (Calc));
41
- auto ImplicitCast =
41
+ const auto ImplicitCast =
42
42
implicitCastExpr (hasImplicitDestinationType (isInteger ()), has (Calc));
43
- auto Cast = expr (anyOf (ExplicitCast, ImplicitCast)).bind (" Cast" );
43
+ const auto Cast = expr (anyOf (ExplicitCast, ImplicitCast)).bind (" Cast" );
44
44
45
45
Finder->addMatcher (varDecl (hasInitializer (Cast)), this );
46
46
Finder->addMatcher (returnStmt (hasReturnValue (Cast)), this );
@@ -50,11 +50,12 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) {
50
50
binaryOperator (anyOf (hasOperatorName (" ==" ), hasOperatorName (" !=" ),
51
51
hasOperatorName (" <" ), hasOperatorName (" <=" ),
52
52
hasOperatorName (" >" ), hasOperatorName (" >=" )),
53
- anyOf ( hasLHS ( Cast), hasRHS (Cast) )),
53
+ hasEitherOperand ( Cast)),
54
54
this );
55
55
}
56
56
57
- static unsigned getMaxCalculationWidth (ASTContext &Context, const Expr *E) {
57
+ static unsigned getMaxCalculationWidth (const ASTContext &Context,
58
+ const Expr *E) {
58
59
E = E->IgnoreParenImpCasts ();
59
60
60
61
if (const auto *Bop = dyn_cast<BinaryOperator>(E)) {
@@ -94,64 +95,89 @@ static unsigned getMaxCalculationWidth(ASTContext &Context, const Expr *E) {
94
95
return Context.getIntWidth (E->getType ());
95
96
}
96
97
97
- static llvm::SmallDenseMap<int , int > createRelativeIntSizesMap () {
98
- llvm::SmallDenseMap<int , int > Result;
99
- Result[BuiltinType::UChar] = 1 ;
100
- Result[BuiltinType::SChar] = 1 ;
101
- Result[BuiltinType::Char_U] = 1 ;
102
- Result[BuiltinType::Char_S] = 1 ;
103
- Result[BuiltinType::UShort] = 2 ;
104
- Result[BuiltinType::Short] = 2 ;
105
- Result[BuiltinType::UInt] = 3 ;
106
- Result[BuiltinType::Int] = 3 ;
107
- Result[BuiltinType::ULong] = 4 ;
108
- Result[BuiltinType::Long] = 4 ;
109
- Result[BuiltinType::ULongLong] = 5 ;
110
- Result[BuiltinType::LongLong] = 5 ;
111
- Result[BuiltinType::UInt128] = 6 ;
112
- Result[BuiltinType::Int128] = 6 ;
113
- return Result;
98
+ static int relativeIntSizes (BuiltinType::Kind Kind) {
99
+ switch (Kind) {
100
+ case BuiltinType::UChar:
101
+ return 1 ;
102
+ case BuiltinType::SChar:
103
+ return 1 ;
104
+ case BuiltinType::Char_U:
105
+ return 1 ;
106
+ case BuiltinType::Char_S:
107
+ return 1 ;
108
+ case BuiltinType::UShort:
109
+ return 2 ;
110
+ case BuiltinType::Short:
111
+ return 2 ;
112
+ case BuiltinType::UInt:
113
+ return 3 ;
114
+ case BuiltinType::Int:
115
+ return 3 ;
116
+ case BuiltinType::ULong:
117
+ return 4 ;
118
+ case BuiltinType::Long:
119
+ return 4 ;
120
+ case BuiltinType::ULongLong:
121
+ return 5 ;
122
+ case BuiltinType::LongLong:
123
+ return 5 ;
124
+ case BuiltinType::UInt128:
125
+ return 6 ;
126
+ case BuiltinType::Int128:
127
+ return 6 ;
128
+ default :
129
+ return 0 ;
130
+ }
114
131
}
115
132
116
- static llvm::SmallDenseMap<int , int > createRelativeCharSizesMap () {
117
- llvm::SmallDenseMap<int , int > Result;
118
- Result[BuiltinType::UChar] = 1 ;
119
- Result[BuiltinType::SChar] = 1 ;
120
- Result[BuiltinType::Char_U] = 1 ;
121
- Result[BuiltinType::Char_S] = 1 ;
122
- Result[BuiltinType::Char16] = 2 ;
123
- Result[BuiltinType::Char32] = 3 ;
124
- return Result;
133
+ static int relativeCharSizes (BuiltinType::Kind Kind) {
134
+ switch (Kind) {
135
+ case BuiltinType::UChar:
136
+ return 1 ;
137
+ case BuiltinType::SChar:
138
+ return 1 ;
139
+ case BuiltinType::Char_U:
140
+ return 1 ;
141
+ case BuiltinType::Char_S:
142
+ return 1 ;
143
+ case BuiltinType::Char16:
144
+ return 2 ;
145
+ case BuiltinType::Char32:
146
+ return 3 ;
147
+ default :
148
+ return 0 ;
149
+ }
125
150
}
126
151
127
- static llvm::SmallDenseMap<int , int > createRelativeCharSizesWMap () {
128
- llvm::SmallDenseMap<int , int > Result;
129
- Result[BuiltinType::UChar] = 1 ;
130
- Result[BuiltinType::SChar] = 1 ;
131
- Result[BuiltinType::Char_U] = 1 ;
132
- Result[BuiltinType::Char_S] = 1 ;
133
- Result[BuiltinType::WChar_U] = 2 ;
134
- Result[BuiltinType::WChar_S] = 2 ;
135
- return Result;
152
+ static int relativeCharSizesW (BuiltinType::Kind Kind) {
153
+ switch (Kind) {
154
+ case BuiltinType::UChar:
155
+ return 1 ;
156
+ case BuiltinType::SChar:
157
+ return 1 ;
158
+ case BuiltinType::Char_U:
159
+ return 1 ;
160
+ case BuiltinType::Char_S:
161
+ return 1 ;
162
+ case BuiltinType::WChar_U:
163
+ return 2 ;
164
+ case BuiltinType::WChar_S:
165
+ return 2 ;
166
+ default :
167
+ return 0 ;
168
+ }
136
169
}
137
170
138
171
static bool isFirstWider (BuiltinType::Kind First, BuiltinType::Kind Second) {
139
- static const llvm::SmallDenseMap<int , int > RelativeIntSizes (
140
- createRelativeIntSizesMap ());
141
- static const llvm::SmallDenseMap<int , int > RelativeCharSizes (
142
- createRelativeCharSizesMap ());
143
- static const llvm::SmallDenseMap<int , int > RelativeCharSizesW (
144
- createRelativeCharSizesWMap ());
145
-
146
172
int FirstSize, SecondSize;
147
- if ((FirstSize = RelativeIntSizes. lookup (First)) &&
148
- (SecondSize = RelativeIntSizes. lookup (Second)))
173
+ if ((FirstSize = relativeIntSizes (First)) != 0 &&
174
+ (SecondSize = relativeIntSizes (Second)) != 0 )
149
175
return FirstSize > SecondSize;
150
- if ((FirstSize = RelativeCharSizes. lookup (First)) &&
151
- (SecondSize = RelativeCharSizes. lookup (Second)))
176
+ if ((FirstSize = relativeCharSizes (First)) != 0 &&
177
+ (SecondSize = relativeCharSizes (Second)) != 0 )
152
178
return FirstSize > SecondSize;
153
- if ((FirstSize = RelativeCharSizesW. lookup (First)) &&
154
- (SecondSize = RelativeCharSizesW. lookup (Second)))
179
+ if ((FirstSize = relativeCharSizesW (First)) != 0 &&
180
+ (SecondSize = relativeCharSizesW (Second)) != 0 )
155
181
return FirstSize > SecondSize;
156
182
return false ;
157
183
}
0 commit comments