[clang-tools-extra] [clang-tidy][NFCI] Simplify bugprone-sizeof-expression (PR #93024)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Wed May 22 05:31:27 PDT 2024
https://github.com/NagyDonat updated https://github.com/llvm/llvm-project/pull/93024
>From b7fb1707601c73bd53b6ac810cd39a94f5b3cd53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 22 May 2024 13:45:13 +0200
Subject: [PATCH 1/3] [clang-tidy][NFCI] Simplify bugprone-sizeof-expression
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.
---
.../clang-tidy/bugprone/SizeofExpressionCheck.cpp | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index a1cffbc666199..f119711638111 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -147,9 +147,6 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
const auto PointerToArrayExpr = ignoringParenImpCasts(
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(
@@ -171,9 +168,9 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
has(ArrayOfPointersExpr)))))))),
sizeOfExpr(has(ArrayOfSamePointersZeroSubscriptExpr)));
- Finder->addMatcher(expr(anyOf(sizeOfExpr(has(ignoringParenImpCasts(anyOf(
- ArrayCastExpr, PointerToArrayExpr,
- StructAddrOfExpr, PointerToStructExpr)))),
+ Finder->addMatcher(expr(anyOf(sizeOfExpr(has(ignoringParenImpCasts(
+ anyOf(ArrayCastExpr, PointerToArrayExpr,
+ PointerToStructExpr)))),
sizeOfExpr(has(PointerToStructType))),
unless(ArrayLengthExprDenom))
.bind("sizeof-pointer-to-aggregate"),
>From 89552a0539b587e9fcbc431db0d9ad6511f36050 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 22 May 2024 14:10:31 +0200
Subject: [PATCH 2/3] Simplify some matcher expressions
Eliminate a few redundant `ignoreParenImpCasts` calls and simplify the
final matcher expression.
---
.../bugprone/SizeofExpressionCheck.cpp | 23 ++++++++++---------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index f119711638111..913be196a29b5 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -144,13 +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 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()))
@@ -168,13 +168,14 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
has(ArrayOfPointersExpr)))))))),
sizeOfExpr(has(ArrayOfSamePointersZeroSubscriptExpr)));
- Finder->addMatcher(expr(anyOf(sizeOfExpr(has(ignoringParenImpCasts(
- anyOf(ArrayCastExpr, PointerToArrayExpr,
- 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'.
>From c6dcbd59d287f0238fbddeb51633228fba6099a4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Wed, 22 May 2024 14:22:45 +0200
Subject: [PATCH 3/3] Remove another useless ignoringParenImpCasts() call
The traversal matcher `hasParent` moves a single layer outwards, so it
cannot skip over paren or implicit cast layers that could be ignored at
that point.
---
.../clang-tidy/bugprone/SizeofExpressionCheck.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 913be196a29b5..5e64d23874ec1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -163,9 +163,9 @@ 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(
More information about the cfe-commits
mailing list