77 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfPointerToAggregate: false}}" --2 3class C {4 int size() { return sizeof(this); }5 // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: suspicious usage of 'sizeof(this)'6};7 8#pragma pack(1)9struct S { char a, b, c; };10 11int Test5() {12 typedef int Array10[10];13 typedef C ArrayC[10];14 15 struct MyStruct {16 Array10 arr;17 Array10* ptr;18 };19 typedef const MyStruct TMyStruct;20 typedef const MyStruct *PMyStruct;21 typedef TMyStruct *PMyStruct2;22 23 static TMyStruct kGlocalMyStruct = {};24 static TMyStruct volatile * kGlocalMyStructPtr = &kGlocalMyStruct;25 26 MyStruct S;27 PMyStruct PS;28 PMyStruct2 PS2;29 Array10 A10;30 C *PtrArray[10];31 C *PC;32 33 int sum = 0;34 sum += sizeof(&S.arr);35 // No warning.36 sum += sizeof(&kGlocalMyStruct.arr);37 // No warning.38 sum += sizeof(&kGlocalMyStructPtr->arr);39 // No warning.40 sum += sizeof(S.arr + 0);41 // No warning.42 sum += sizeof(+ S.arr);43 // No warning.44 sum += sizeof((int*)S.arr);45 // No warning.46 47 sum += sizeof(S.ptr);48 // No warning.49 sum += sizeof(kGlocalMyStruct.ptr);50 // No warning.51 sum += sizeof(kGlocalMyStructPtr->ptr);52 // No warning.53 54 sum += sizeof(&kGlocalMyStruct);55 // No warning.56 sum += sizeof(&S);57 // No warning.58 sum += sizeof(MyStruct*);59 sum += sizeof(PMyStruct);60 sum += sizeof(PS);61 // No warning.62 sum += sizeof(PS2);63 // No warning.64 sum += sizeof(&A10);65 // No warning.66 sum += sizeof(PtrArray) / sizeof(PtrArray[1]);67 // No warning.68 sum += sizeof(A10) / sizeof(PtrArray[0]);69 // No warning.70 sum += sizeof(PC) / sizeof(PtrArray[0]);71 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions have the same type72 sum += sizeof(ArrayC) / sizeof(PtrArray[0]);73 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: suspicious usage of 'sizeof(...)/sizeof(...)'; numerator is not a multiple of denominator74 75 return sum;76}77