Skip to content

Commit

Permalink
[clang-tidy][NFCI] Simplify bugprone-sizeof-expression (llvm#93024)
Browse files Browse the repository at this point in the history
This commit eliminates a redundant matcher subexpression from the
implementation of the "sizeof-pointer-to-aggregate" part of the
clang-tidy check `bugprone-sizeof-expression`.

I'm fairly certain that anything that was previously matched by the
deleted matcher `StructAddrOfExpr` is also covered by the more general
`PointerToStructExpr` (which remains in the same `anyOf`).

This commit is made to "prepare the ground" for a followup change that
would merge the functionality of the Clang Static Analyzer checker
`alpha.core.SizeofPtr` into this clang-tidy check.
  • Loading branch information
NagyDonat authored May 22, 2024
1 parent a699ccb commit 5272768
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,13 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
unaryOperator(hasUnaryOperand(ArrayExpr), unless(hasOperatorName("*"))),
binaryOperator(hasEitherOperand(ArrayExpr)),
castExpr(hasSourceExpression(ArrayExpr))));
const auto PointerToArrayExpr = ignoringParenImpCasts(
hasType(hasCanonicalType(pointerType(pointee(arrayType())))));
const auto PointerToArrayExpr =
hasType(hasCanonicalType(pointerType(pointee(arrayType()))));

const auto StructAddrOfExpr = unaryOperator(
hasOperatorName("&"), hasUnaryOperand(ignoringParenImpCasts(
hasType(hasCanonicalType(recordType())))));
const auto PointerToStructType =
hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
const auto PointerToStructExpr = ignoringParenImpCasts(expr(
hasType(hasCanonicalType(PointerToStructType)), unless(cxxThisExpr())));
const auto PointerToStructExpr = expr(
hasType(hasCanonicalType(PointerToStructType)), unless(cxxThisExpr()));

const auto ArrayOfPointersExpr = ignoringParenImpCasts(
hasType(hasCanonicalType(arrayType(hasElementType(pointerType()))
Expand All @@ -166,18 +163,19 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
ignoringParenImpCasts(arraySubscriptExpr(
hasBase(ArrayOfSamePointersExpr), hasIndex(ZeroLiteral)));
const auto ArrayLengthExprDenom =
expr(hasParent(expr(ignoringParenImpCasts(binaryOperator(
hasOperatorName("/"), hasLHS(ignoringParenImpCasts(sizeOfExpr(
has(ArrayOfPointersExpr)))))))),
expr(hasParent(binaryOperator(hasOperatorName("/"),
hasLHS(ignoringParenImpCasts(sizeOfExpr(
has(ArrayOfPointersExpr)))))),
sizeOfExpr(has(ArrayOfSamePointersZeroSubscriptExpr)));

Finder->addMatcher(expr(anyOf(sizeOfExpr(has(ignoringParenImpCasts(anyOf(
ArrayCastExpr, PointerToArrayExpr,
StructAddrOfExpr, PointerToStructExpr)))),
sizeOfExpr(has(PointerToStructType))),
unless(ArrayLengthExprDenom))
.bind("sizeof-pointer-to-aggregate"),
this);
Finder->addMatcher(
expr(sizeOfExpr(anyOf(
has(ignoringParenImpCasts(anyOf(
ArrayCastExpr, PointerToArrayExpr, PointerToStructExpr))),
has(PointerToStructType))),
unless(ArrayLengthExprDenom))
.bind("sizeof-pointer-to-aggregate"),
this);
}

// Detect expression like: sizeof(expr) <= k for a suspicious constant 'k'.
Expand Down

0 comments on commit 5272768

Please sign in to comment.