63 lines · c
1// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t2// RUN: %check_clang_tidy -std=c++11-or-later %s bugprone-sizeof-expression %t -- -- -x c++3 4#ifdef __cplusplus5#define STRKWD6#else7#define STRKWD struct8#endif9 10int 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 type38 sum += sizeof(__typeof(&S));39 sum += sizeof(&TS);40 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type41 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 type48 sum += sizeof(PMyStruct);49 sum += sizeof(PS);50 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type51 sum += sizeof(PS2);52 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type53 sum += sizeof(&A10);54 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious usage of 'sizeof()' on an expression of pointer type55 56#ifdef __cplusplus57 MyStruct &rS = S;58 sum += sizeof(rS); // same as sizeof(S), not a pointer. So should not warn.59#endif60 61 return sum;62}63