[PATCH] D131926: [clang-tidy] Fix for bugprone-sizeof-expression PR57167
Chris Hamilton via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 16 08:32:07 PDT 2022
chrish_ericsson_atx updated this revision to Diff 453022.
chrish_ericsson_atx added a comment.
Added missing newline and added // FIXME comments, per reviewer comments.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D131926/new/
https://reviews.llvm.org/D131926
Files:
clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c
clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
@@ -232,10 +232,8 @@
// 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(MyStruct*); // FIXME: Warning here prior to 15f3cd6bfc6, consider whether to add it back.
+ sum += sizeof(PMyStruct); // FIXME: Warning here prior to 15f3cd6bfc6, consider whether to add it back.
sum += sizeof(PS);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
sum += sizeof(PS2);
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c
@@ -0,0 +1,62 @@
+// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- --
+// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -- -x c++
+
+#ifdef __cplusplus
+#define STRKWD
+#else
+#define STRKWD struct
+#endif
+
+int Test5() {
+ typedef int Array10[10];
+
+ struct MyStruct {
+ Array10 arr;
+ Array10* ptr;
+ };
+
+ typedef struct TypedefStruct {
+ Array10 arr;
+ Array10* ptr;
+ } TypedefStruct;
+
+ typedef const STRKWD MyStruct TMyStruct;
+ typedef const STRKWD MyStruct *PMyStruct;
+ typedef TMyStruct *PMyStruct2;
+ typedef const TypedefStruct *PTTStruct;
+
+ STRKWD MyStruct S;
+ TypedefStruct TS;
+ PMyStruct PS;
+ PMyStruct2 PS2;
+ Array10 A10;
+ PTTStruct PTTS;
+
+ int sum = 0;
+ sum += sizeof(&S);
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
+ sum += sizeof(__typeof(&S));
+ sum += sizeof(&TS);
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
+ sum += sizeof(__typeof(&TS));
+ sum += sizeof(STRKWD MyStruct*);
+ sum += sizeof(__typeof(STRKWD MyStruct*));
+ sum += sizeof(TypedefStruct*);
+ sum += sizeof(__typeof(TypedefStruct*));
+ sum += sizeof(PTTS);
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof(A*)'; pointer to aggregate
+ sum += sizeof(PMyStruct);
+ 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
+
+#ifdef __cplusplus
+ MyStruct &rS = S;
+ sum += sizeof(rS); // same as sizeof(S), not a pointer. So should not warn.
+#endif
+
+ return sum;
+}
Index: clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -148,7 +148,7 @@
hasOperatorName("&"), hasUnaryOperand(ignoringParenImpCasts(
hasType(hasCanonicalType(recordType())))));
const auto PointerToStructType = hasUnqualifiedDesugaredType(
- pointerType(pointee(hasCanonicalType(recordType()))));
+ pointerType(pointee(recordType())));
const auto PointerToStructExpr = ignoringParenImpCasts(expr(
hasType(hasCanonicalType(PointerToStructType)), unless(cxxThisExpr())));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131926.453022.patch
Type: text/x-patch
Size: 4079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220816/14bd7048/attachment-0001.bin>
More information about the cfe-commits
mailing list