1 // RUN: %check_clang_tidy -check-suffixes=ALL,C -std=c99 %s bugprone-implicit-widening-of-multiplication-result %t -- -- -target x86_64-unknown-unknown -x c 2 // RUN: %check_clang_tidy -check-suffixes=ALL,CXX %s bugprone-implicit-widening-of-multiplication-result %t -- -- -target x86_64-unknown-unknown -x c++ 3 4 // RUN: %check_clang_tidy -check-suffixes=ALL,C -std=c99 %s bugprone-implicit-widening-of-multiplication-result %t -- \ 5 // RUN: -config='{CheckOptions: [ \ 6 // RUN: {key: bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources, value: false} \ 7 // RUN: ]}' -- -target x86_64-unknown-unknown -x c 8 // RUN: %check_clang_tidy -check-suffixes=ALL,C %s bugprone-implicit-widening-of-multiplication-result %t -- \ 9 // RUN: -config='{CheckOptions: [ \ 10 // RUN: {key: bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources, value: false} \ 11 // RUN: ]}' -- -target x86_64-unknown-unknown -x c++ 12 13 char *t0(char *base, int a, int b) { 14 return &base[a * b]; 15 // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t' 16 // CHECK-NOTES-ALL: :[[@LINE-2]]:16: note: make conversion explicit to silence this warning 17 // CHECK-NOTES-C: (ptrdiff_t)( ) 18 // CHECK-NOTES-CXX: static_cast<ptrdiff_t>( ) 19 // CHECK-NOTES-ALL: :[[@LINE-5]]:16: note: perform multiplication in a wider type 20 // CHECK-NOTES-C: (ptrdiff_t) 21 // CHECK-NOTES-CXX: static_cast<ptrdiff_t>() 22 } 23 void *t1(char *base, int a, int b) { 24 return &((a * b)[base]); 25 // CHECK-NOTES-ALL: :[[@LINE-1]]:12: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t' 26 // CHECK-NOTES-ALL: :[[@LINE-2]]:13: note: make conversion explicit to silence this warning 27 // CHECK-NOTES-ALL: :[[@LINE-3]]:13: note: perform multiplication in a wider type 28 } 29 30 char *t2(char *base, unsigned int a, int b) { 31 return &base[a * b]; 32 // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: result of multiplication in type 'unsigned int' is used as a pointer offset after an implicit widening conversion to type 'size_t' 33 // CHECK-NOTES-ALL: :[[@LINE-2]]:16: note: make conversion explicit to silence this warning 34 // CHECK-NOTES-C: (size_t) 35 // CHECK-NOTES-CXX: static_cast<size_t>( ) 36 // CHECK-NOTES-ALL: :[[@LINE-5]]:16: note: perform multiplication in a wider type 37 // CHECK-NOTES-C: (size_t) 38 // CHECK-NOTES-CXX: static_cast<size_t>() 39 } 40 41 char *t3(char *base, int a, unsigned int b) { 42 return &base[a * b]; 43 // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: result of multiplication in type 'unsigned int' is used as a pointer offset after an implicit widening conversion to type 'size_t' 44 // CHECK-NOTES-ALL: :[[@LINE-2]]:16: note: make conversion explicit to silence this warning 45 // CHECK-NOTES-ALL: :[[@LINE-3]]:16: note: perform multiplication in a wider type 46 } 47 48 char *t4(char *base, unsigned int a, unsigned int b) { 49 return &base[a * b]; 50 // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: result of multiplication in type 'unsigned int' is used as a pointer offset after an implicit widening conversion to type 'size_t' 51 // CHECK-NOTES-ALL: :[[@LINE-2]]:16: note: make conversion explicit to silence this warning 52 // CHECK-NOTES-ALL: :[[@LINE-3]]:16: note: perform multiplication in a wider type 53 } 54 55 char *n5(char *base, int a, int b, int c) { 56 return &base[a * b + c]; 57 } 58 char *n6(char *base, int a, int b, int c) { 59 return &base[a + b * c]; 60 } 61 62 char *t7(char *base, int a, int b) { 63 return &base[(a * b)]; 64 // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t' 65 // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning 66 // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type 67 } 68 char *n8(char *base, int a, int b, int c) { 69 return &base[(a * b + c)]; 70 } 71 char *n9(char *base, int a, int b, int c) { 72 return &base[(a * b) + c]; 73 } 74 75 char *n10(char *base, int a, int b) { 76 return &base[(long)(a * b)]; 77 } 78 char *n11(char *base, int a, int b) { 79 return &base[(unsigned long)(a * b)]; 80 } 81 82 #ifdef __cplusplus 83 template <typename T> 84 char *template_test(char *base, T a, T b) { 85 return &base[a * b]; 86 } 87 char *template_test_instantiation(char *base, int a, int b) { 88 return template_test(base, a, b); 89 } 90 #endif 91