Skip to content

Commit

Permalink
[clang-tidy] Extend bugprone-sizeof-expression to check sizeof(pointe…
Browse files Browse the repository at this point in the history
…rs to structures)

Accidentally taking the size of a struct-pointer type or a value of this type
is more common than explicitly using the & operator for the value. This patch
extends the check to include these cases.

Differential Revision: https://reviews.llvm.org/D61260

llvm-svn: 360114
  • Loading branch information
Adam Balogh authored and MrSidims committed May 17, 2019
1 parent d654ecb commit eba6504
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 9 additions & 2 deletions clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
Expand Up @@ -145,10 +145,17 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
unaryOperator(hasOperatorName("&"),
hasUnaryOperand(ignoringParenImpCasts(expr(
hasType(qualType(hasCanonicalType(recordType())))))));
const auto PointerToStructType = type(hasUnqualifiedDesugaredType(
pointerType(pointee(recordType()))));
const auto PointerToStructExpr = expr(ignoringParenImpCasts(expr(
hasType(qualType(hasCanonicalType(PointerToStructType))),
unless(cxxThisExpr()))));

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

Expand Down
12 changes: 12 additions & 0 deletions clang-tools-extra/test/clang-tidy/bugprone-sizeof-expression.cpp
Expand Up @@ -193,11 +193,15 @@ int Test5() {
Array10* ptr;
};
typedef const MyStruct TMyStruct;
typedef const MyStruct *PMyStruct;
typedef TMyStruct *PMyStruct2;

static TMyStruct kGlocalMyStruct = {};
static TMyStruct volatile * kGlocalMyStructPtr = &kGlocalMyStruct;

MyStruct S;
PMyStruct PS;
PMyStruct2 PS2;
Array10 A10;

int sum = 0;
Expand Down Expand Up @@ -225,6 +229,14 @@ int Test5() {
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(&S);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(MyStruct*);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(PMyStruct);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(PS);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(PS2);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(&A10);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate

Expand Down

0 comments on commit eba6504

Please sign in to comment.