189a1d03eSRichard // 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
289a1d03eSRichard // RUN: %check_clang_tidy -check-suffixes=ALL,CXX %s bugprone-implicit-widening-of-multiplication-result %t -- -- -target x86_64-unknown-unknown -x c++
389a1d03eSRichard 
489a1d03eSRichard // RUN: %check_clang_tidy -check-suffixes=ALL,C -std=c99 %s bugprone-implicit-widening-of-multiplication-result %t -- \
5*e8a3ddafSNathan James // RUN:     -config='{CheckOptions: { \
6*e8a3ddafSNathan James // RUN:         bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources: false \
7*e8a3ddafSNathan James // RUN:     }}' -- -target x86_64-unknown-unknown -x c
889a1d03eSRichard // RUN: %check_clang_tidy -check-suffixes=ALL,C %s bugprone-implicit-widening-of-multiplication-result %t -- \
9*e8a3ddafSNathan James // RUN:     -config='{CheckOptions: { \
10*e8a3ddafSNathan James // RUN:         bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources: false \
11*e8a3ddafSNathan James // RUN:     }}' -- -target x86_64-unknown-unknown -x c++
1289a1d03eSRichard 
t0(char * base,int a,int b)1389a1d03eSRichard char *t0(char *base, int a, int b) {
1489a1d03eSRichard   return base + a * b;
1589a1d03eSRichard   // 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'
1689a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning
1789a1d03eSRichard   // CHECK-NOTES-C:                    (ptrdiff_t)( )
1889a1d03eSRichard   // CHECK-NOTES-CXX:                  static_cast<ptrdiff_t>( )
1989a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-5]]:17: note: perform multiplication in a wider type
2089a1d03eSRichard   // CHECK-NOTES-C:                    (ptrdiff_t)
2189a1d03eSRichard   // CHECK-NOTES-CXX:                  static_cast<ptrdiff_t>( )
2289a1d03eSRichard }
t1(char * base,int a,int b)2389a1d03eSRichard char *t1(char *base, int a, int b) {
2489a1d03eSRichard   return a * b + base;
2589a1d03eSRichard   // 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'
2689a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:10: note: make conversion explicit to silence this warning
2789a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:10: note: perform multiplication in a wider type
2889a1d03eSRichard }
2989a1d03eSRichard 
t2(char * base,unsigned int a,int b)3089a1d03eSRichard char *t2(char *base, unsigned int a, int b) {
3189a1d03eSRichard   return base + a * b;
3289a1d03eSRichard   // 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'
3389a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning
3489a1d03eSRichard   // CHECK-NOTES-C:                    (size_t)( )
3589a1d03eSRichard   // CHECK-NOTES-CXX:                  static_cast<size_t>( )
3689a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-5]]:17: note: perform multiplication in a wider type
3789a1d03eSRichard   // CHECK-NOTES-C:                    (size_t)
3889a1d03eSRichard   // CHECK-NOTES-CXX:                  static_cast<size_t>( )
3989a1d03eSRichard }
4089a1d03eSRichard 
t3(char * base,int a,unsigned int b)4189a1d03eSRichard char *t3(char *base, int a, unsigned int b) {
4289a1d03eSRichard   return base + a * b;
4389a1d03eSRichard   // 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'
4489a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning
4589a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type
4689a1d03eSRichard }
4789a1d03eSRichard 
t4(char * base,unsigned int a,unsigned int b)4889a1d03eSRichard char *t4(char *base, unsigned int a, unsigned int b) {
4989a1d03eSRichard   return base + a * b;
5089a1d03eSRichard   // 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'
5189a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning
5289a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type
5389a1d03eSRichard }
5489a1d03eSRichard 
t5(char * base,int a,int b,int c)5589a1d03eSRichard char *t5(char *base, int a, int b, int c) {
5689a1d03eSRichard   return base + a * b + c;
5789a1d03eSRichard   // 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'
5889a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:17: note: make conversion explicit to silence this warning
5989a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:17: note: perform multiplication in a wider type
6089a1d03eSRichard }
t6(char * base,int a,int b,int c)6189a1d03eSRichard char *t6(char *base, int a, int b, int c) {
6289a1d03eSRichard   return base + a + b * c;
6389a1d03eSRichard   // 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'
6489a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:21: note: make conversion explicit to silence this warning
6589a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:21: note: perform multiplication in a wider type
6689a1d03eSRichard }
6789a1d03eSRichard 
n7(char * base,int a,int b)6889a1d03eSRichard char *n7(char *base, int a, int b) {
6989a1d03eSRichard   return base + (a * b);
7089a1d03eSRichard   // 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'
7189a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:18: note: make conversion explicit to silence this warning
7289a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:18: note: perform multiplication in a wider type
7389a1d03eSRichard }
n8(char * base,int a,int b,int c)7489a1d03eSRichard char *n8(char *base, int a, int b, int c) {
7589a1d03eSRichard   return base + (a * b) + c;
7689a1d03eSRichard   // 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'
7789a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-2]]:18: note: make conversion explicit to silence this warning
7889a1d03eSRichard   // CHECK-NOTES-ALL: :[[@LINE-3]]:18: note: perform multiplication in a wider type
7989a1d03eSRichard }
n9(char * base,int a,int b,int c)8089a1d03eSRichard char *n9(char *base, int a, int b, int c) {
8189a1d03eSRichard   return base + (a * b + c);
8289a1d03eSRichard }
8389a1d03eSRichard 
n10(char * base,int a,int b)8489a1d03eSRichard char *n10(char *base, int a, int b) {
8589a1d03eSRichard   return base + (long)(a * b);
8689a1d03eSRichard }
n11(char * base,int a,int b)8789a1d03eSRichard char *n11(char *base, int a, int b) {
8889a1d03eSRichard   return base + (unsigned long)(a * b);
8989a1d03eSRichard }
9089a1d03eSRichard 
9189a1d03eSRichard #ifdef __cplusplus
9289a1d03eSRichard template <typename T>
template_test(char * base,T a,T b)9389a1d03eSRichard char *template_test(char *base, T a, T b) {
9489a1d03eSRichard   return base + a * b;
9589a1d03eSRichard }
template_test_instantiation(char * base,int a,int b)9689a1d03eSRichard char *template_test_instantiation(char *base, int a, int b) {
9789a1d03eSRichard   return template_test(base, a, b);
9889a1d03eSRichard }
9989a1d03eSRichard #endif
100