[clang-tools-extra] 9e1a4ce - [clang-tidy] Fix for bugprone-sizeof-expression PR57167

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 19 14:30:02 PDT 2022


Author: eahcmrh
Date: 2022-08-19T23:29:32+02:00
New Revision: 9e1a4ce0b55db1388351550497e5ce43b588f71c

URL: https://github.com/llvm/llvm-project/commit/9e1a4ce0b55db1388351550497e5ce43b588f71c
DIFF: https://github.com/llvm/llvm-project/commit/9e1a4ce0b55db1388351550497e5ce43b588f71c.diff

LOG: [clang-tidy] Fix for bugprone-sizeof-expression PR57167

This addresses a change in behavior of the bugprone-sizeof-expression
checker after upstream commit 15f3cd6bfc6, which cleaned up
ElaboratedType sugaring in the AST.  This restores (mostly) the
beahvior of the checker prior to that commit, which may or may not have
been consistent with the intent of the checker, but at least gave a
tolerable level of what users would consider false positives.

Bug: https://github.com/llvm/llvm-project/issues/57167

Reviewed By: mizvekov, aaron.ballman

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

Change-Id: Ibe5aad77ad00977134aa7fa67efbbd6bd725fd79

Added: 
    clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c

Modified: 
    clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index e88b1846ef603..fe5c80658f268 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -147,8 +147,8 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
   const auto StructAddrOfExpr = unaryOperator(
       hasOperatorName("&"), hasUnaryOperand(ignoringParenImpCasts(
                                 hasType(hasCanonicalType(recordType())))));
-  const auto PointerToStructType = hasUnqualifiedDesugaredType(
-      pointerType(pointee(hasCanonicalType(recordType()))));
+  const auto PointerToStructType =
+      hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
   const auto PointerToStructExpr = ignoringParenImpCasts(expr(
       hasType(hasCanonicalType(PointerToStructType)), unless(cxxThisExpr())));
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c
new file mode 100644
index 0000000000000..8c4feb8f86169
--- /dev/null
+++ b/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;
+}

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
index 3f40d0e29024f..86dba31d6061f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
@@ -233,9 +233,7 @@ int Test5() {
   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);


        


More information about the cfe-commits mailing list