[PATCH] D61260: [clang-tidy] Extend bugprone-sizeof-expression to check sizeof(pointers to structures)
Balogh, Ádám via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 29 06:39:35 PDT 2019
baloghadamsoftware created this revision.
baloghadamsoftware added reviewers: alexfh, aaron.ballman, lebedev.ri, JonasToth.
baloghadamsoftware added a project: clang-tools-extra.
Herald added subscribers: gamesh411, rnkovacs.
Herald added a project: clang.
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.
Repository:
rCTE Clang Tools Extra
https://reviews.llvm.org/D61260
Files:
clang-tidy/bugprone/SizeofExpressionCheck.cpp
test/clang-tidy/bugprone-sizeof-expression.cpp
Index: test/clang-tidy/bugprone-sizeof-expression.cpp
===================================================================
--- test/clang-tidy/bugprone-sizeof-expression.cpp
+++ test/clang-tidy/bugprone-sizeof-expression.cpp
@@ -193,11 +193,13 @@
Array10* ptr;
};
typedef const MyStruct TMyStruct;
+ typedef const MyStruct *PMyStruct;
static TMyStruct kGlocalMyStruct = {};
static TMyStruct volatile * kGlocalMyStructPtr = &kGlocalMyStruct;
MyStruct S;
+ PMyStruct PS;
Array10 A10;
int sum = 0;
@@ -225,6 +227,12 @@
// 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(&A10);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
Index: clang-tidy/bugprone/SizeofExpressionCheck.cpp
===================================================================
--- clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -142,10 +142,17 @@
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);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61260.197101.patch
Type: text/x-patch
Size: 2485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190429/1887c6b3/attachment.bin>
More information about the cfe-commits
mailing list