xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.c (revision 267ad430fc54d6d548cd7d25c7e59c3b6b650097)
1 // RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- --
2 // RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -- -x c++
3 
4 #ifdef __cplusplus
5 #define STRKWD
6 #else
7 #define STRKWD struct
8 #endif
9 
10 int Test5() {
11   typedef int Array10[10];
12 
13   struct MyStruct {
14     Array10 arr;
15     Array10* ptr;
16   };
17 
18   typedef struct TypedefStruct {
19     Array10 arr;
20     Array10* ptr;
21   } TypedefStruct;
22 
23   typedef const STRKWD MyStruct TMyStruct;
24   typedef const STRKWD MyStruct *PMyStruct;
25   typedef TMyStruct *PMyStruct2;
26   typedef const TypedefStruct *PTTStruct;
27 
28   STRKWD MyStruct S;
29   TypedefStruct TS;
30   PMyStruct PS;
31   PMyStruct2 PS2;
32   Array10 A10;
33   PTTStruct PTTS;
34 
35   int sum = 0;
36   sum += sizeof(&S);
37   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type
38   sum += sizeof(__typeof(&S));
39   sum += sizeof(&TS);
40   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type
41   sum += sizeof(__typeof(&TS));
42   sum += sizeof(STRKWD MyStruct*);
43   sum += sizeof(__typeof(STRKWD MyStruct*));
44   sum += sizeof(TypedefStruct*);
45   sum += sizeof(__typeof(TypedefStruct*));
46   sum += sizeof(PTTS);
47   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type
48   sum += sizeof(PMyStruct);
49   sum += sizeof(PS);
50   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type
51   sum += sizeof(PS2);
52   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type
53   sum += sizeof(&A10);
54   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type
55 
56 #ifdef __cplusplus
57   MyStruct &rS = S;
58   sum += sizeof(rS); // same as sizeof(S), not a pointer.  So should not warn.
59 #endif
60 
61   return sum;
62 }
63