100 const auto LoopCondExpr =
101 [](
const ast_matchers::internal::Matcher<Stmt> &InnerMatcher) {
102 return stmt(anyOf(forStmt(hasCondition(InnerMatcher)),
103 whileStmt(hasCondition(InnerMatcher)),
104 doStmt(hasCondition(InnerMatcher))));
107 const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
108 const auto ConstantExpr = ignoringParenImpCasts(
109 anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
110 binaryOperator(hasLHS(IntegerExpr), hasRHS(IntegerExpr))));
111 const auto IntegerCallExpr = ignoringParenImpCasts(callExpr(
112 anyOf(hasType(isInteger()), hasType(hasCanonicalType(enumType()))),
113 unless(isInTemplateInstantiation())));
114 const auto SizeOfExpr = sizeOfExpr(hasArgumentOfType(
115 hasUnqualifiedDesugaredType(type().bind(
"sizeof-arg-type"))));
116 const auto SizeOfZero =
117 sizeOfExpr(has(ignoringParenImpCasts(integerLiteral(equals(0)))));
122 if (WarnOnSizeOfConstant) {
124 expr(sizeOfExpr(has(ignoringParenImpCasts(ConstantExpr))),
126 .bind(
"sizeof-constant"),
131 if (WarnOnSizeOfIntegerExpression) {
132 Finder->addMatcher(sizeOfExpr(ignoringParenImpCasts(has(IntegerCallExpr)))
133 .bind(
"sizeof-integer-call"),
138 if (WarnOnSizeOfThis) {
139 Finder->addMatcher(sizeOfExpr(has(ignoringParenImpCasts(cxxThisExpr())))
140 .bind(
"sizeof-this"),
144 if (WarnOnSizeOfInLoopTermination) {
145 auto CondExpr = binaryOperator(
146 allOf(has(SizeOfExpr.bind(
"sizeof-expr")), isComparisonOperator()));
147 Finder->addMatcher(LoopCondExpr(anyOf(CondExpr, hasDescendant(CondExpr)))
153 const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
154 const auto ConstStrLiteralDecl =
155 varDecl(isDefinition(), hasType(hasCanonicalType(CharPtrType)),
156 hasInitializer(ignoringParenImpCasts(stringLiteral())));
157 const auto VarWithConstStrLiteralDecl = expr(
158 hasType(hasCanonicalType(CharPtrType)),
159 ignoringParenImpCasts(declRefExpr(hasDeclaration(ConstStrLiteralDecl))));
161 sizeOfExpr(has(ignoringParenImpCasts(VarWithConstStrLiteralDecl)))
162 .bind(
"sizeof-charp"),
180 if (WarnOnSizeOfPointerToAggregate || WarnOnSizeOfPointer) {
181 const auto ArrayExpr =
182 ignoringParenImpCasts(hasType(hasCanonicalType(arrayType())));
183 const auto ArrayCastExpr = expr(anyOf(
184 unaryOperator(hasUnaryOperand(ArrayExpr), unless(hasOperatorName(
"*"))),
185 binaryOperator(hasEitherOperand(ArrayExpr)),
186 castExpr(hasSourceExpression(ArrayExpr))));
187 const auto PointerToArrayExpr =
188 hasType(hasCanonicalType(pointerType(pointee(arrayType()))));
190 const auto PointerToStructType =
191 hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
192 const auto PointerToStructExpr =
193 expr(hasType(hasCanonicalType(PointerToStructType)));
195 const auto PointerToDetectedExpr =
197 ? expr(hasType(hasUnqualifiedDesugaredType(pointerType())))
198 : expr(anyOf(ArrayCastExpr, PointerToArrayExpr,
199 PointerToStructExpr));
201 const auto ZeroLiteral = ignoringParenImpCasts(integerLiteral(equals(0)));
202 const auto SubscriptExprWithZeroIndex =
203 arraySubscriptExpr(hasIndex(ZeroLiteral));
204 const auto DerefExpr =
205 ignoringParenImpCasts(unaryOperator(hasOperatorName(
"*")));
208 expr(sizeOfExpr(has(ignoringParenImpCasts(expr(
209 PointerToDetectedExpr, unless(DerefExpr),
210 unless(SubscriptExprWithZeroIndex),
211 unless(VarWithConstStrLiteralDecl), unless(cxxThisExpr()))))))
212 .bind(
"sizeof-pointer"),
217 if (WarnOnSizeOfCompareToConstant) {
219 binaryOperator(matchers::isRelationalOperator(),
220 hasOperands(ignoringParenImpCasts(SizeOfExpr),
221 ignoringParenImpCasts(integerLiteral(anyOf(
222 equals(0), isBiggerThan(0x80000))))))
223 .bind(
"sizeof-compare-constant"),
230 has(ignoringParenImpCasts(
231 binaryOperator(hasOperatorName(
",")).bind(
"sizeof-comma-binop"))))
232 .bind(
"sizeof-comma-expr"),
242 const auto ElemType =
243 arrayType(hasElementType(recordType().bind(
"elem-type")));
244 const auto ElemPtrType = pointerType(pointee(type().bind(
"elem-ptr-type")));
245 const auto SizeofDivideExpr = binaryOperator(
246 hasOperatorName(
"/"),
248 ignoringParenImpCasts(sizeOfExpr(hasArgumentOfType(hasCanonicalType(
249 type(anyOf(ElemType, ElemPtrType, type())).bind(
"num-type")))))),
250 hasRHS(ignoringParenImpCasts(sizeOfExpr(
251 hasArgumentOfType(hasCanonicalType(type().bind(
"denom-type")))))));
253 Finder->addMatcher(SizeofDivideExpr.bind(
"sizeof-divide-expr"),
this);
256 Finder->addMatcher(binaryOperator(hasOperatorName(
"*"),
257 hasLHS(ignoringParenImpCasts(SizeOfExpr)),
258 hasRHS(ignoringParenImpCasts(SizeOfExpr)))
259 .bind(
"sizeof-multiply-sizeof"),
263 binaryOperator(hasOperatorName(
"*"),
264 hasOperands(ignoringParenImpCasts(SizeOfExpr),
265 ignoringParenImpCasts(binaryOperator(
266 hasOperatorName(
"*"),
268 ignoringParenImpCasts(SizeOfExpr))))))
269 .bind(
"sizeof-multiply-sizeof"),
274 Finder->addMatcher(sizeOfExpr(has(ignoringParenImpCasts(hasSizeOfDescendant(
275 8, allOf(SizeOfExpr, unless(SizeOfZero))))))
276 .bind(
"sizeof-sizeof-expr"),
282 const auto PtrDiffExpr = binaryOperator(
283 hasOperatorName(
"-"),
284 hasLHS(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
285 hasUnqualifiedDesugaredType(type().bind(
"left-ptr-type"))))))),
286 hasRHS(hasType(hasUnqualifiedDesugaredType(pointerType(pointee(
287 hasUnqualifiedDesugaredType(type().bind(
"right-ptr-type"))))))));
291 hasAnyOperatorName(
"==",
"!=",
"<",
"<=",
">",
">=",
"+",
"-"),
292 hasOperands(anyOf(ignoringParenImpCasts(
293 SizeOfExpr.bind(
"sizeof-ptr-mul-expr")),
294 ignoringParenImpCasts(binaryOperator(
295 hasOperatorName(
"*"),
296 hasEitherOperand(ignoringParenImpCasts(
297 SizeOfExpr.bind(
"sizeof-ptr-mul-expr")))))),
298 ignoringParenImpCasts(PtrDiffExpr)))
299 .bind(
"sizeof-in-ptr-arithmetic-mul"),
304 hasOperatorName(
"/"), hasLHS(ignoringParenImpCasts(PtrDiffExpr)),
305 hasRHS(ignoringParenImpCasts(SizeOfExpr.bind(
"sizeof-ptr-div-expr"))))
306 .bind(
"sizeof-in-ptr-arithmetic-div"),
321 const auto InterestingPtrTyForPtrArithmetic =
322 pointerType(pointee(qualType().bind(
"pointee-type")));
323 const auto SizeofLikeScaleExpr =
324 expr(anyOf(unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf)),
325 unaryExprOrTypeTraitExpr(ofKind(UETT_AlignOf)),
327 .bind(
"sizeof-in-ptr-arithmetic-scale-expr");
328 const auto PtrArithmeticIntegerScaleExpr = binaryOperator(
329 WarnOnOffsetDividedBySizeOf ? binaryOperator(hasAnyOperatorName(
"*",
"/"))
330 : binaryOperator(hasOperatorName(
"*")),
333 hasOperands(expr(hasType(isInteger()), unless(SizeofLikeScaleExpr)),
334 SizeofLikeScaleExpr));
335 const auto PtrArithmeticScaledIntegerExpr =
336 expr(anyOf(SizeofLikeScaleExpr, PtrArithmeticIntegerScaleExpr),
337 unless(SizeofDivideExpr));
341 binaryOperator(hasAnyOperatorName(
"+",
"-"),
342 hasOperands(hasType(InterestingPtrTyForPtrArithmetic),
343 PtrArithmeticScaledIntegerExpr))
344 .bind(
"sizeof-in-ptr-arithmetic-plusminus"),
345 binaryOperator(hasAnyOperatorName(
"+=",
"-="),
346 hasLHS(hasType(InterestingPtrTyForPtrArithmetic)),
347 hasRHS(PtrArithmeticScaledIntegerExpr))
348 .bind(
"sizeof-in-ptr-arithmetic-plusminus"))),
353 const ASTContext &Ctx = *Result.Context;
355 if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-constant")) {
356 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(K)'; did you mean 'K'?")
357 << E->getSourceRange();
358 }
else if (
const auto *E =
359 Result.Nodes.getNodeAs<Expr>(
"sizeof-integer-call")) {
360 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof()' on an expression "
362 << E->getSourceRange();
363 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-this")) {
364 diag(E->getBeginLoc(),
365 "suspicious usage of 'sizeof(this)'; did you mean 'sizeof(*this)'")
366 << E->getSourceRange();
367 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-charp")) {
368 diag(E->getBeginLoc(),
369 "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
370 << E->getSourceRange();
371 }
else if (Result.Nodes.getNodeAs<Stmt>(
"loop-expr")) {
372 auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>(
"sizeof-arg-type");
373 if (
const auto *Member = dyn_cast<MemberPointerType>(SizeofArgTy))
374 SizeofArgTy = Member->getPointeeType().getTypePtr();
376 const auto *SzOfExpr = Result.Nodes.getNodeAs<Expr>(
"sizeof-expr");
380 if (
const auto *Type = dyn_cast<ArrayType>(SizeofArgTy);
382 !
getSizeOfType(Ctx, Type->getElementType().getTypePtr()).isOne())
383 diag(SzOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof' in the loop")
384 << SzOfExpr->getSourceRange();
386 }
else if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"sizeof-pointer")) {
387 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof()' on an expression "
389 << E->getSourceRange();
390 }
else if (
const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
391 "sizeof-compare-constant")) {
392 diag(E->getOperatorLoc(),
393 "suspicious comparison of 'sizeof(expr)' to a constant")
394 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
395 }
else if (
const auto *E =
396 Result.Nodes.getNodeAs<Expr>(
"sizeof-comma-expr")) {
398 Result.Nodes.getNodeAs<BinaryOperator>(
"sizeof-comma-binop");
400 diag(BO->getOperatorLoc(),
"suspicious usage of 'sizeof(..., ...)'")
401 << E->getSourceRange();
402 }
else if (
const auto *E =
403 Result.Nodes.getNodeAs<BinaryOperator>(
"sizeof-divide-expr")) {
404 const auto *NumTy = Result.Nodes.getNodeAs<Type>(
"num-type");
405 const auto *DenomTy = Result.Nodes.getNodeAs<Type>(
"denom-type");
406 const auto *ElementTy = Result.Nodes.getNodeAs<Type>(
"elem-type");
407 const auto *PointedTy = Result.Nodes.getNodeAs<Type>(
"elem-ptr-type");
410 const CharUnits DenominatorSize =
getSizeOfType(Ctx, DenomTy);
413 if (DenominatorSize > CharUnits::Zero() &&
414 !NumeratorSize.isMultipleOf(DenominatorSize)) {
415 diag(E->getOperatorLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)';"
416 " numerator is not a multiple of denominator")
417 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
418 }
else if (ElementSize > CharUnits::Zero() &&
419 DenominatorSize > CharUnits::Zero() &&
420 ElementSize != DenominatorSize) {
422 diag(E->getOperatorLoc(),
423 "suspicious usage of 'sizeof(array)/sizeof(...)';"
424 " denominator differs from the size of array elements")
425 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
426 }
else if (NumTy && DenomTy && ASTContext::hasSameType(NumTy, DenomTy) &&
427 !NumTy->isDependentType()) {
429 diag(E->getOperatorLoc(),
430 "suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions "
431 "have the same type")
432 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
433 }
else if (!WarnOnSizeOfPointer) {
435 if (PointedTy && DenomTy && ASTContext::hasSameType(PointedTy, DenomTy)) {
436 diag(E->getOperatorLoc(),
437 "suspicious usage of 'sizeof(...)/sizeof(...)'; size of pointer "
438 "is divided by size of pointed type")
439 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
440 }
else if (NumTy && DenomTy && NumTy->isPointerType() &&
441 DenomTy->isPointerType()) {
442 diag(E->getOperatorLoc(),
443 "suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions "
444 "have pointer types")
445 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
448 }
else if (
const auto *E =
449 Result.Nodes.getNodeAs<Expr>(
"sizeof-sizeof-expr")) {
450 diag(E->getBeginLoc(),
"suspicious usage of 'sizeof(sizeof(...))'")
451 << E->getSourceRange();
452 }
else if (
const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
453 "sizeof-multiply-sizeof")) {
454 diag(E->getOperatorLoc(),
"suspicious 'sizeof' by 'sizeof' multiplication")
455 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
456 }
else if (
const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
457 "sizeof-in-ptr-arithmetic-mul")) {
458 const auto *LPtrTy = Result.Nodes.getNodeAs<Type>(
"left-ptr-type");
459 const auto *RPtrTy = Result.Nodes.getNodeAs<Type>(
"right-ptr-type");
460 const auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>(
"sizeof-arg-type");
461 const auto *SizeOfExpr =
462 Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>(
"sizeof-ptr-mul-expr");
464 if (ASTContext::hasSameType(LPtrTy, RPtrTy) &&
465 ASTContext::hasSameType(LPtrTy, SizeofArgTy)) {
466 diag(SizeOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof(...)' in "
467 "pointer arithmetic")
468 << SizeOfExpr->getSourceRange() << E->getOperatorLoc()
469 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
471 }
else if (
const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
472 "sizeof-in-ptr-arithmetic-div")) {
473 const auto *LPtrTy = Result.Nodes.getNodeAs<Type>(
"left-ptr-type");
474 const auto *RPtrTy = Result.Nodes.getNodeAs<Type>(
"right-ptr-type");
475 const auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>(
"sizeof-arg-type");
476 const auto *SizeOfExpr =
477 Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>(
"sizeof-ptr-div-expr");
479 if (ASTContext::hasSameType(LPtrTy, RPtrTy) &&
480 ASTContext::hasSameType(LPtrTy, SizeofArgTy)) {
481 diag(SizeOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof(...)' in "
482 "pointer arithmetic")
483 << SizeOfExpr->getSourceRange() << E->getOperatorLoc()
484 << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
486 }
else if (
const auto *E = Result.Nodes.getNodeAs<BinaryOperator>(
487 "sizeof-in-ptr-arithmetic-plusminus")) {
488 const auto *PointeeTy = Result.Nodes.getNodeAs<QualType>(
"pointee-type");
489 const auto *ScaleExpr =
490 Result.Nodes.getNodeAs<Expr>(
"sizeof-in-ptr-arithmetic-scale-expr");
491 const CharUnits PointeeSize =
getSizeOfType(Ctx, PointeeTy->getTypePtr());
492 const int ScaleKind = [ScaleExpr]() {
493 if (
const auto *UTTE = dyn_cast<UnaryExprOrTypeTraitExpr>(ScaleExpr))
494 switch (UTTE->getKind()) {
503 if (isa<OffsetOfExpr>(ScaleExpr))
509 if (ScaleKind != -1 && PointeeSize > CharUnits::One()) {
510 diag(E->getExprLoc(),
511 "suspicious usage of '%select{sizeof|alignof|offsetof}0(...)' in "
512 "pointer arithmetic; this scaled value will be scaled again by the "
514 << ScaleKind << E->getOpcodeStr() << ScaleExpr->getSourceRange();
515 diag(E->getExprLoc(),
516 "'%0' in pointer arithmetic internally scales with 'sizeof(%1)' == "
520 << PointeeTy->getAsString(Ctx.getPrintingPolicy())
521 << PointeeSize.getQuantity();