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 
t0(char * base,int a,int b)13 char *t0(char *base, int a, int b) {
14   return base + a * b;
15   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: 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]]:17: 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]]:17: note: perform multiplication in a wider type
20   // CHECK-NOTES-C:                    (ptrdiff_t)
21   // CHECK-NOTES-CXX:                  static_cast<ptrdiff_t>()
22 }
t1(char * base,int a,int b)23 char *t1(char *base, int a, int b) {
24   return a * b + base;
25   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: 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]]:10: note: make conversion explicit to silence this warning
27   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
28 }
29 
t2(char * base,unsigned int a,int b)30 char *t2(char *base, unsigned int a, int b) {
31   return base + a * b;
32   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: 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]]:17: 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]]:17: note: perform multiplication in a wider type
37   // CHECK-NOTES-C:                    (size_t)
38   // CHECK-NOTES-CXX:                  static_cast<size_t>()
39 }
40 
t3(char * base,int a,unsigned int b)41 char *t3(char *base, int a, unsigned int b) {
42   return base + a * b;
43   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: 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]]:17: note: make conversion explicit to silence this warning
45   // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type
46 }
47 
t4(char * base,unsigned int a,unsigned int b)48 char *t4(char *base, unsigned int a, unsigned int b) {
49   return base + a * b;
50   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: 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]]:17: note: make conversion explicit to silence this warning
52   // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type
53 }
54 
t5(char * base,int a,int b,int c)55 char *t5(char *base, int a, int b, int c) {
56   return base + a * b + c;
57   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t'
58   // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning
59   // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type
60 }
t6(char * base,int a,int b,int c)61 char *t6(char *base, int a, int b, int c) {
62   return base + a + b * c;
63   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t'
64   // CHECK-NOTES-ALL: :[[@LINE-2]]:21: note: make conversion explicit to silence this warning
65   // CHECK-NOTES-ALL: :[[@LINE-3]]:21: note: perform multiplication in a wider type
66 }
67 
n7(char * base,int a,int b)68 char *n7(char *base, int a, int b) {
69   return base + (a * b);
70   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t'
71   // CHECK-NOTES-ALL: :[[@LINE-2]]:18: note: make conversion explicit to silence this warning
72   // CHECK-NOTES-ALL: :[[@LINE-3]]:18: note: perform multiplication in a wider type
73 }
n8(char * base,int a,int b,int c)74 char *n8(char *base, int a, int b, int c) {
75   return base + (a * b) + c;
76   // CHECK-NOTES-ALL: :[[@LINE-1]]:10: warning: result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t'
77   // CHECK-NOTES-ALL: :[[@LINE-2]]:18: note: make conversion explicit to silence this warning
78   // CHECK-NOTES-ALL: :[[@LINE-3]]:18: note: perform multiplication in a wider type
79 }
n9(char * base,int a,int b,int c)80 char *n9(char *base, int a, int b, int c) {
81   return base + (a * b + c);
82 }
83 
n10(char * base,int a,int b)84 char *n10(char *base, int a, int b) {
85   return base + (long)(a * b);
86 }
n11(char * base,int a,int b)87 char *n11(char *base, int a, int b) {
88   return base + (unsigned long)(a * b);
89 }
90 
91 #ifdef __cplusplus
92 template <typename T>
template_test(char * base,T a,T b)93 char *template_test(char *base, T a, T b) {
94   return base + a * b;
95 }
template_test_instantiation(char * base,int a,int b)96 char *template_test_instantiation(char *base, int a, int b) {
97   return template_test(base, a, b);
98 }
99 #endif
100