@@ -84,19 +84,19 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
8484 // positives if sizeof is applied on template argument.
8585
8686 const auto IntegerExpr = ignoringParenImpCasts (integerLiteral ());
87- const auto ConstantExpr = expr ( ignoringParenImpCasts (
87+ const auto ConstantExpr = ignoringParenImpCasts (
8888 anyOf (integerLiteral (), unaryOperator (hasUnaryOperand (IntegerExpr)),
89- binaryOperator (hasLHS (IntegerExpr), hasRHS (IntegerExpr))))) ;
90- const auto IntegerCallExpr = expr ( ignoringParenImpCasts (
89+ binaryOperator (hasLHS (IntegerExpr), hasRHS (IntegerExpr))));
90+ const auto IntegerCallExpr = ignoringParenImpCasts (
9191 callExpr (anyOf (hasType (isInteger ()), hasType (enumType ())),
92- unless (isInTemplateInstantiation ())))) ;
92+ unless (isInTemplateInstantiation ())));
9393 const auto SizeOfExpr = expr (anyOf (
9494 sizeOfExpr (
9595 has (hasUnqualifiedDesugaredType (type ().bind (" sizeof-arg-type" )))),
9696 sizeOfExpr (has (expr (hasType (
9797 hasUnqualifiedDesugaredType (type ().bind (" sizeof-arg-type" ))))))));
98- const auto SizeOfZero = expr (
99- sizeOfExpr (has (ignoringParenImpCasts (expr ( integerLiteral (equals (0 )) )))));
98+ const auto SizeOfZero =
99+ sizeOfExpr (has (ignoringParenImpCasts (integerLiteral (equals (0 )))));
100100
101101 // Detect expression like: sizeof(ARRAYLEN);
102102 // Note: The expression 'sizeof(sizeof(0))' is a portable trick used to know
@@ -111,74 +111,69 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
111111
112112 // Detect sizeof(f())
113113 if (WarnOnSizeOfIntegerExpression) {
114- Finder->addMatcher (
115- expr (sizeOfExpr (ignoringParenImpCasts (has (IntegerCallExpr))))
116- .bind (" sizeof-integer-call" ),
117- this );
114+ Finder->addMatcher (sizeOfExpr (ignoringParenImpCasts (has (IntegerCallExpr)))
115+ .bind (" sizeof-integer-call" ),
116+ this );
118117 }
119118
120119 // Detect expression like: sizeof(this);
121120 if (WarnOnSizeOfThis) {
122- Finder->addMatcher (
123- expr (sizeOfExpr (has (ignoringParenImpCasts (expr (cxxThisExpr ())))))
124- .bind (" sizeof-this" ),
125- this );
121+ Finder->addMatcher (sizeOfExpr (has (ignoringParenImpCasts (cxxThisExpr ())))
122+ .bind (" sizeof-this" ),
123+ this );
126124 }
127125
128126 // Detect sizeof(kPtr) where kPtr is 'const char* kPtr = "abc"';
129127 const auto CharPtrType = pointerType (pointee (isAnyCharacter ()));
130128 const auto ConstStrLiteralDecl =
131- varDecl (isDefinition (), hasType (qualType ( hasCanonicalType (CharPtrType) )),
129+ varDecl (isDefinition (), hasType (hasCanonicalType (CharPtrType)),
132130 hasInitializer (ignoringParenImpCasts (stringLiteral ())));
133- Finder->addMatcher (expr (sizeOfExpr (has (ignoringParenImpCasts (expr (
134- hasType (qualType (hasCanonicalType (CharPtrType))),
135- ignoringParenImpCasts (declRefExpr (
136- hasDeclaration (ConstStrLiteralDecl))))))))
137- .bind (" sizeof-charp" ),
138- this );
131+ Finder->addMatcher (
132+ sizeOfExpr (has (ignoringParenImpCasts (
133+ expr (hasType (hasCanonicalType (CharPtrType)),
134+ ignoringParenImpCasts (declRefExpr (
135+ hasDeclaration (ConstStrLiteralDecl)))))))
136+ .bind (" sizeof-charp" ),
137+ this );
139138
140139 // Detect sizeof(ptr) where ptr points to an aggregate (i.e. sizeof(&S)).
141140 // Do not find it if RHS of a 'sizeof(arr) / sizeof(arr[0])' expression.
142- const auto ArrayExpr = expr ( ignoringParenImpCasts (
143- expr (hasType (qualType ( hasCanonicalType (arrayType ())) ))));
141+ const auto ArrayExpr =
142+ ignoringParenImpCasts (hasType (hasCanonicalType (arrayType ())));
144143 const auto ArrayCastExpr = expr (anyOf (
145144 unaryOperator (hasUnaryOperand (ArrayExpr), unless (hasOperatorName (" *" ))),
146145 binaryOperator (hasEitherOperand (ArrayExpr)),
147146 castExpr (hasSourceExpression (ArrayExpr))));
148- const auto PointerToArrayExpr = expr (ignoringParenImpCasts (expr (
149- hasType (qualType (hasCanonicalType (pointerType (pointee (arrayType ()))))))));
150-
151- const auto StructAddrOfExpr =
152- unaryOperator (hasOperatorName (" &" ),
153- hasUnaryOperand (ignoringParenImpCasts (expr (
154- hasType (qualType (hasCanonicalType (recordType ())))))));
155- const auto PointerToStructType = type (hasUnqualifiedDesugaredType (
156- pointerType (pointee (recordType ()))));
157- const auto PointerToStructExpr = expr (ignoringParenImpCasts (expr (
158- hasType (qualType (hasCanonicalType (PointerToStructType))),
159- unless (cxxThisExpr ()))));
160-
161- const auto ArrayOfPointersExpr = expr (ignoringParenImpCasts (expr (hasType (
162- qualType (hasCanonicalType (arrayType (hasElementType (pointerType ()))
163- .bind (" type-of-array-of-pointers" )))))));
147+ const auto PointerToArrayExpr = ignoringParenImpCasts (
148+ hasType (hasCanonicalType (pointerType (pointee (arrayType ())))));
149+
150+ const auto StructAddrOfExpr = unaryOperator (
151+ hasOperatorName (" &" ), hasUnaryOperand (ignoringParenImpCasts (
152+ hasType (hasCanonicalType (recordType ())))));
153+ const auto PointerToStructType =
154+ hasUnqualifiedDesugaredType (pointerType (pointee (recordType ())));
155+ const auto PointerToStructExpr = ignoringParenImpCasts (expr (
156+ hasType (hasCanonicalType (PointerToStructType)), unless (cxxThisExpr ())));
157+
158+ const auto ArrayOfPointersExpr = ignoringParenImpCasts (
159+ hasType (hasCanonicalType (arrayType (hasElementType (pointerType ()))
160+ .bind (" type-of-array-of-pointers" ))));
164161 const auto ArrayOfSamePointersExpr =
165- expr (ignoringParenImpCasts (expr (hasType (qualType (hasCanonicalType (
166- arrayType (equalsBoundNode (" type-of-array-of-pointers" ))))))));
167- const auto ZeroLiteral =
168- expr (ignoringParenImpCasts (integerLiteral (equals (0 ))));
162+ ignoringParenImpCasts (hasType (hasCanonicalType (
163+ arrayType (equalsBoundNode (" type-of-array-of-pointers" )))));
164+ const auto ZeroLiteral = ignoringParenImpCasts (integerLiteral (equals (0 )));
169165 const auto ArrayOfSamePointersZeroSubscriptExpr =
170- expr ( ignoringParenImpCasts (arraySubscriptExpr (
171- hasBase (ArrayOfSamePointersExpr), hasIndex (ZeroLiteral) )));
166+ ignoringParenImpCasts (arraySubscriptExpr (hasBase (ArrayOfSamePointersExpr),
167+ hasIndex (ZeroLiteral)));
172168 const auto ArrayLengthExprDenom =
173- expr (hasParent (expr (ignoringParenImpCasts (
174- binaryOperator (hasOperatorName (" /" ),
175- hasLHS (expr (ignoringParenImpCasts (expr (
176- sizeOfExpr (has (ArrayOfPointersExpr)))))))))),
169+ expr (hasParent (expr (ignoringParenImpCasts (binaryOperator (
170+ hasOperatorName (" /" ), hasLHS (ignoringParenImpCasts (sizeOfExpr (
171+ has (ArrayOfPointersExpr)))))))),
177172 sizeOfExpr (has (ArrayOfSamePointersZeroSubscriptExpr)));
178173
179- Finder->addMatcher (expr (anyOf (sizeOfExpr (has (expr ( ignoringParenImpCasts (anyOf (
174+ Finder->addMatcher (expr (anyOf (sizeOfExpr (has (ignoringParenImpCasts (anyOf (
180175 ArrayCastExpr, PointerToArrayExpr,
181- StructAddrOfExpr, PointerToStructExpr))))) ,
176+ StructAddrOfExpr, PointerToStructExpr)))),
182177 sizeOfExpr (has (PointerToStructType))),
183178 unless (ArrayLengthExprDenom))
184179 .bind (" sizeof-pointer-to-aggregate" ),
@@ -197,8 +192,8 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
197192 }
198193
199194 // Detect expression like: sizeof(expr, expr); most likely an error.
200- Finder->addMatcher (expr ( sizeOfExpr (has ( expr (ignoringParenImpCasts (
201- binaryOperator (hasOperatorName (" ," )) )))))
195+ Finder->addMatcher (sizeOfExpr (has (ignoringParenImpCasts (
196+ binaryOperator (hasOperatorName (" ," )))))
202197 .bind (" sizeof-comma-expr" ),
203198 this );
204199
@@ -212,9 +207,9 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
212207 const auto ElemType =
213208 arrayType (hasElementType (recordType ().bind (" elem-type" )));
214209 const auto ElemPtrType = pointerType (pointee (type ().bind (" elem-ptr-type" )));
215- const auto NumType = qualType ( hasCanonicalType (
216- type (anyOf (ElemType, ElemPtrType, type ())).bind (" num-type" ))) ;
217- const auto DenomType = qualType ( hasCanonicalType (type ().bind (" denom-type" ) ));
210+ const auto NumType = hasCanonicalType (
211+ type (anyOf (ElemType, ElemPtrType, type ())).bind (" num-type" ));
212+ const auto DenomType = hasCanonicalType (type ().bind (" denom-type" ));
218213
219214 Finder->addMatcher (
220215 binaryOperator (hasOperatorName (" /" ),
@@ -246,30 +241,29 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
246241
247242 // Detect strange double-sizeof expression like: sizeof(sizeof(...));
248243 // Note: The expression 'sizeof(sizeof(0))' is accepted.
249- Finder->addMatcher (
250- expr (sizeOfExpr (has (ignoringParenImpCasts (expr (
251- hasSizeOfDescendant (8 , expr (SizeOfExpr, unless (SizeOfZero))))))))
252- .bind (" sizeof-sizeof-expr" ),
253- this );
244+ Finder->addMatcher (sizeOfExpr (has (ignoringParenImpCasts (hasSizeOfDescendant (
245+ 8 , allOf (SizeOfExpr, unless (SizeOfZero))))))
246+ .bind (" sizeof-sizeof-expr" ),
247+ this );
254248
255249 // Detect sizeof in pointer arithmetic like: N * sizeof(S) == P1 - P2 or
256250 // (P1 - P2) / sizeof(S) where P1 and P2 are pointers to type S.
257251 const auto PtrDiffExpr = binaryOperator (
258252 hasOperatorName (" -" ),
259- hasLHS (expr ( hasType (hasUnqualifiedDesugaredType (pointerType (pointee (
260- hasUnqualifiedDesugaredType (type ().bind (" left-ptr-type" )))))))) ,
261- hasRHS (expr ( hasType (hasUnqualifiedDesugaredType (pointerType (pointee (
262- hasUnqualifiedDesugaredType (type ().bind (" right-ptr-type" ))))))))) ;
253+ hasLHS (hasType (hasUnqualifiedDesugaredType (pointerType (pointee (
254+ hasUnqualifiedDesugaredType (type ().bind (" left-ptr-type" ))))))),
255+ hasRHS (hasType (hasUnqualifiedDesugaredType (pointerType (pointee (
256+ hasUnqualifiedDesugaredType (type ().bind (" right-ptr-type" ))))))));
263257
264258 Finder->addMatcher (
265259 binaryOperator (
266260 hasAnyOperatorName (" ==" , " !=" , " <" , " <=" , " >" , " >=" , " +" , " -" ),
267- hasOperands (expr ( anyOf ( ignoringParenImpCasts (SizeOfExpr),
268- ignoringParenImpCasts (binaryOperator (
269- hasOperatorName ( " * " ),
270- hasEitherOperand (
271- ignoringParenImpCasts (SizeOfExpr) ))))),
272- ignoringParenImpCasts (PtrDiffExpr)))
261+ hasOperands (
262+ anyOf ( ignoringParenImpCasts (SizeOfExpr),
263+ ignoringParenImpCasts ( binaryOperator (
264+ hasOperatorName ( " * " ),
265+ hasEitherOperand ( ignoringParenImpCasts (SizeOfExpr))))),
266+ ignoringParenImpCasts (PtrDiffExpr)))
273267 .bind (" sizeof-in-ptr-arithmetic-mul" ),
274268 this );
275269
0 commit comments