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: bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources: 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: bugprone-implicit-widening-of-multiplication-result.UseCXXStaticCastsInCppSources: false \
11// RUN: }}' -- -target x86_64-unknown-unknown -x c++
12
13char *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}
23char *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
30char *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
41char *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
48char *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
55char *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}
61char *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
68char *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}
74char *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}
80char *n9(char *base, int a, int b, int c) {
81 return base + (a * b + c);
82}
83
84char *n10(char *base, int a, int b) {
85 return base + (long)(a * b);
86}
87char *n11(char *base, int a, int b) {
88 return base + (unsigned long)(a * b);
89}
90
91#ifdef __cplusplus
92template <typename T>
93char *template_test(char *base, T a, T b) {
94 return base + a * b;
95}
96char *template_test_instantiation(char *base, int a, int b) {
97 return template_test(base, a, b);
98}
99#endif
100

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-pointer-offset.cpp