@@ -192,10 +192,12 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
192
192
}
193
193
194
194
// Detect expression like: sizeof(expr, expr); most likely an error.
195
- Finder->addMatcher (sizeOfExpr (has (ignoringParenImpCasts (
196
- binaryOperator (hasOperatorName (" ," )))))
197
- .bind (" sizeof-comma-expr" ),
198
- this );
195
+ Finder->addMatcher (
196
+ sizeOfExpr (
197
+ has (ignoringParenImpCasts (
198
+ binaryOperator (hasOperatorName (" ," )).bind (" sizeof-comma-binop" ))))
199
+ .bind (" sizeof-comma-expr" ),
200
+ this );
199
201
200
202
// Detect sizeof(...) /sizeof(...));
201
203
// FIXME:
@@ -255,51 +257,62 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
255
257
Finder->addMatcher (
256
258
binaryOperator (
257
259
hasAnyOperatorName (" ==" , " !=" , " <" , " <=" , " >" , " >=" , " +" , " -" ),
258
- hasOperands (
259
- anyOf (ignoringParenImpCasts (SizeOfExpr),
260
- ignoringParenImpCasts (binaryOperator (
261
- hasOperatorName (" *" ),
262
- hasEitherOperand (ignoringParenImpCasts (SizeOfExpr))))),
263
- ignoringParenImpCasts (PtrDiffExpr)))
260
+ hasOperands (anyOf (ignoringParenImpCasts (
261
+ SizeOfExpr.bind (" sizeof-ptr-mul-expr" )),
262
+ ignoringParenImpCasts (binaryOperator (
263
+ hasOperatorName (" *" ),
264
+ hasEitherOperand (ignoringParenImpCasts (
265
+ SizeOfExpr.bind (" sizeof-ptr-mul-expr" )))))),
266
+ ignoringParenImpCasts (PtrDiffExpr)))
264
267
.bind (" sizeof-in-ptr-arithmetic-mul" ),
265
268
this );
266
269
267
- Finder->addMatcher (binaryOperator (hasOperatorName (" /" ),
268
- hasLHS (ignoringParenImpCasts (PtrDiffExpr)),
269
- hasRHS (ignoringParenImpCasts (SizeOfExpr)))
270
- .bind (" sizeof-in-ptr-arithmetic-div" ),
271
- this );
270
+ Finder->addMatcher (
271
+ binaryOperator (
272
+ hasOperatorName (" /" ), hasLHS (ignoringParenImpCasts (PtrDiffExpr)),
273
+ hasRHS (ignoringParenImpCasts (SizeOfExpr.bind (" sizeof-ptr-div-expr" ))))
274
+ .bind (" sizeof-in-ptr-arithmetic-div" ),
275
+ this );
272
276
}
273
277
274
278
void SizeofExpressionCheck::check (const MatchFinder::MatchResult &Result) {
275
279
const ASTContext &Ctx = *Result.Context ;
276
280
277
281
if (const auto *E = Result.Nodes .getNodeAs <Expr>(" sizeof-constant" )) {
278
- diag (E->getBeginLoc (),
279
- " suspicious usage of 'sizeof(K)'; did you mean 'K'? " );
282
+ diag (E->getBeginLoc (), " suspicious usage of 'sizeof(K)'; did you mean 'K'? " )
283
+ << E-> getSourceRange ( );
280
284
} else if (const auto *E =
281
285
Result.Nodes .getNodeAs <Expr>(" sizeof-integer-call" )) {
282
286
diag (E->getBeginLoc (), " suspicious usage of 'sizeof()' on an expression "
283
- " that results in an integer" );
287
+ " that results in an integer" )
288
+ << E->getSourceRange ();
284
289
} else if (const auto *E = Result.Nodes .getNodeAs <Expr>(" sizeof-this" )) {
285
290
diag (E->getBeginLoc (),
286
- " suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'" );
291
+ " suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'" )
292
+ << E->getSourceRange ();
287
293
} else if (const auto *E = Result.Nodes .getNodeAs <Expr>(" sizeof-charp" )) {
288
294
diag (E->getBeginLoc (),
289
- " suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?" );
295
+ " suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?" )
296
+ << E->getSourceRange ();
290
297
} else if (const auto *E =
291
298
Result.Nodes .getNodeAs <Expr>(" sizeof-pointer-to-aggregate" )) {
292
299
diag (E->getBeginLoc (),
293
- " suspicious usage of 'sizeof(A*)'; pointer to aggregate" );
294
- } else if (const auto *E =
295
- Result.Nodes .getNodeAs <Expr>(" sizeof-compare-constant" )) {
296
- diag (E->getBeginLoc (),
297
- " suspicious comparison of 'sizeof(expr)' to a constant" );
300
+ " suspicious usage of 'sizeof(A*)'; pointer to aggregate" )
301
+ << E->getSourceRange ();
302
+ } else if (const auto *E = Result.Nodes .getNodeAs <BinaryOperator>(
303
+ " sizeof-compare-constant" )) {
304
+ diag (E->getOperatorLoc (),
305
+ " suspicious comparison of 'sizeof(expr)' to a constant" )
306
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
298
307
} else if (const auto *E =
299
308
Result.Nodes .getNodeAs <Expr>(" sizeof-comma-expr" )) {
300
- diag (E->getBeginLoc (), " suspicious usage of 'sizeof(..., ...)'" );
309
+ const auto *BO =
310
+ Result.Nodes .getNodeAs <BinaryOperator>(" sizeof-comma-binop" );
311
+ assert (BO);
312
+ diag (BO->getOperatorLoc (), " suspicious usage of 'sizeof(..., ...)'" )
313
+ << E->getSourceRange ();
301
314
} else if (const auto *E =
302
- Result.Nodes .getNodeAs <Expr >(" sizeof-divide-expr" )) {
315
+ Result.Nodes .getNodeAs <BinaryOperator >(" sizeof-divide-expr" )) {
303
316
const auto *NumTy = Result.Nodes .getNodeAs <Type>(" num-type" );
304
317
const auto *DenomTy = Result.Nodes .getNodeAs <Type>(" denom-type" );
305
318
const auto *ElementTy = Result.Nodes .getNodeAs <Type>(" elem-type" );
@@ -311,49 +324,64 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
311
324
312
325
if (DenominatorSize > CharUnits::Zero () &&
313
326
!NumeratorSize.isMultipleOf (DenominatorSize)) {
314
- diag (E->getBeginLoc (), " suspicious usage of 'sizeof(...)/sizeof(...)';"
315
- " numerator is not a multiple of denominator" );
327
+ diag (E->getOperatorLoc (), " suspicious usage of 'sizeof(...)/sizeof(...)';"
328
+ " numerator is not a multiple of denominator" )
329
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
316
330
} else if (ElementSize > CharUnits::Zero () &&
317
331
DenominatorSize > CharUnits::Zero () &&
318
332
ElementSize != DenominatorSize) {
319
- diag (E->getBeginLoc (), " suspicious usage of 'sizeof(...)/sizeof(...)';"
320
- " numerator is not a multiple of denominator" );
333
+ diag (E->getOperatorLoc (), " suspicious usage of 'sizeof(...)/sizeof(...)';"
334
+ " numerator is not a multiple of denominator" )
335
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
321
336
} else if (NumTy && DenomTy && NumTy == DenomTy) {
322
- diag (E->getBeginLoc (),
323
- " suspicious usage of sizeof pointer 'sizeof(T)/sizeof(T)'" );
337
+ diag (E->getOperatorLoc (),
338
+ " suspicious usage of sizeof pointer 'sizeof(T)/sizeof(T)'" )
339
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
324
340
} else if (PointedTy && DenomTy && PointedTy == DenomTy) {
325
- diag (E->getBeginLoc (),
326
- " suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'" );
341
+ diag (E->getOperatorLoc (),
342
+ " suspicious usage of sizeof pointer 'sizeof(T*)/sizeof(T)'" )
343
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
327
344
} else if (NumTy && DenomTy && NumTy->isPointerType () &&
328
345
DenomTy->isPointerType ()) {
329
- diag (E->getBeginLoc (),
330
- " suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'" );
346
+ diag (E->getOperatorLoc (),
347
+ " suspicious usage of sizeof pointer 'sizeof(P*)/sizeof(Q*)'" )
348
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
331
349
}
332
350
} else if (const auto *E =
333
351
Result.Nodes .getNodeAs <Expr>(" sizeof-sizeof-expr" )) {
334
- diag (E->getBeginLoc (), " suspicious usage of 'sizeof(sizeof(...))'" );
335
- } else if (const auto *E =
336
- Result.Nodes .getNodeAs <Expr>(" sizeof-multiply-sizeof" )) {
337
- diag (E->getBeginLoc (), " suspicious 'sizeof' by 'sizeof' multiplication" );
338
- } else if (const auto *E =
339
- Result.Nodes .getNodeAs <Expr>(" sizeof-in-ptr-arithmetic-mul" )) {
352
+ diag (E->getBeginLoc (), " suspicious usage of 'sizeof(sizeof(...))'" )
353
+ << E->getSourceRange ();
354
+ } else if (const auto *E = Result.Nodes .getNodeAs <BinaryOperator>(
355
+ " sizeof-multiply-sizeof" )) {
356
+ diag (E->getOperatorLoc (), " suspicious 'sizeof' by 'sizeof' multiplication" )
357
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
358
+ } else if (const auto *E = Result.Nodes .getNodeAs <BinaryOperator>(
359
+ " sizeof-in-ptr-arithmetic-mul" )) {
340
360
const auto *LPtrTy = Result.Nodes .getNodeAs <Type>(" left-ptr-type" );
341
361
const auto *RPtrTy = Result.Nodes .getNodeAs <Type>(" right-ptr-type" );
342
362
const auto *SizeofArgTy = Result.Nodes .getNodeAs <Type>(" sizeof-arg-type" );
363
+ const auto *SizeOfExpr =
364
+ Result.Nodes .getNodeAs <UnaryExprOrTypeTraitExpr>(" sizeof-ptr-mul-expr" );
343
365
344
366
if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
345
- diag (E->getBeginLoc (), " suspicious usage of 'sizeof(...)' in "
346
- " pointer arithmetic" );
367
+ diag (SizeOfExpr->getBeginLoc (), " suspicious usage of 'sizeof(...)' in "
368
+ " pointer arithmetic" )
369
+ << SizeOfExpr->getSourceRange () << E->getOperatorLoc ()
370
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
347
371
}
348
- } else if (const auto *E =
349
- Result. Nodes . getNodeAs <Expr>( " sizeof-in-ptr-arithmetic-div" )) {
372
+ } else if (const auto *E = Result. Nodes . getNodeAs <BinaryOperator>(
373
+ " sizeof-in-ptr-arithmetic-div" )) {
350
374
const auto *LPtrTy = Result.Nodes .getNodeAs <Type>(" left-ptr-type" );
351
375
const auto *RPtrTy = Result.Nodes .getNodeAs <Type>(" right-ptr-type" );
352
376
const auto *SizeofArgTy = Result.Nodes .getNodeAs <Type>(" sizeof-arg-type" );
377
+ const auto *SizeOfExpr =
378
+ Result.Nodes .getNodeAs <UnaryExprOrTypeTraitExpr>(" sizeof-ptr-div-expr" );
353
379
354
380
if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
355
- diag (E->getBeginLoc (), " suspicious usage of 'sizeof(...)' in "
356
- " pointer arithmetic" );
381
+ diag (SizeOfExpr->getBeginLoc (), " suspicious usage of 'sizeof(...)' in "
382
+ " pointer arithmetic" )
383
+ << SizeOfExpr->getSourceRange () << E->getOperatorLoc ()
384
+ << E->getLHS ()->getSourceRange () << E->getRHS ()->getSourceRange ();
357
385
}
358
386
}
359
387
}
0 commit comments