1// RUN: %clang -x c -fsanitize=implicit-conversion -O0 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
2// RUN: %clang -x c -fsanitize=implicit-conversion -O1 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
3// RUN: %clang -x c -fsanitize=implicit-conversion -O2 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
4// RUN: %clang -x c -fsanitize=implicit-conversion -O3 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
5
6// RUN: %clangxx -x c++ -fsanitize=implicit-conversion -O0 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
7// RUN: %clangxx -x c++ -fsanitize=implicit-conversion -O1 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
8// RUN: %clangxx -x c++ -fsanitize=implicit-conversion -O2 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
9// RUN: %clangxx -x c++ -fsanitize=implicit-conversion -O3 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK
10
11void test_unsigned() {
12 unsigned char x;
13
14 x = 0;
15 x++;
16 x = 0;
17 ++x;
18
19 x = 0;
20 x--;
21 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:4: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned char' changed the value to 255 (8-bit, unsigned)
22 x = 0;
23 --x;
24 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:3: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned char' changed the value to 255 (8-bit, unsigned)
25
26 x = 1;
27 x++;
28 x = 1;
29 ++x;
30
31 x = 1;
32 x--;
33 x = 1;
34 --x;
35
36 x = 254;
37 x++;
38 x = 254;
39 ++x;
40
41 x = 254;
42 x--;
43 x = 254;
44 --x;
45
46 x = 255;
47 x++;
48 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:4: runtime error: implicit conversion from type 'int' of value 256 (32-bit, signed) to type 'unsigned char' changed the value to 0 (8-bit, unsigned)
49 x = 255;
50 ++x;
51 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:3: runtime error: implicit conversion from type 'int' of value 256 (32-bit, signed) to type 'unsigned char' changed the value to 0 (8-bit, unsigned)
52
53 x = 255;
54 x--;
55 x = 255;
56 --x;
57}
58
59void test_signed() {
60 signed char x;
61
62 x = -128;
63 x++;
64 x = -128;
65 ++x;
66
67 x = -128;
68 x--;
69 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:4: runtime error: implicit conversion from type 'int' of value -129 (32-bit, signed) to type 'signed char' changed the value to 127 (8-bit, signed)
70 x = -128;
71 --x;
72 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:3: runtime error: implicit conversion from type 'int' of value -129 (32-bit, signed) to type 'signed char' changed the value to 127 (8-bit, signed)
73
74 x = -1;
75 x++;
76 x = -1;
77 ++x;
78
79 x = -1;
80 x--;
81 x = -1;
82 --x;
83
84 x = 0;
85 x++;
86 x = 0;
87 ++x;
88
89 x = 0;
90 x--;
91 x = 0;
92 --x;
93
94 x = 1;
95 x++;
96 x = 1;
97 ++x;
98
99 x = 1;
100 x--;
101 x = 1;
102 --x;
103
104 x = 127;
105 x++;
106 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:4: runtime error: implicit conversion from type 'int' of value 128 (32-bit, signed) to type 'signed char' changed the value to -128 (8-bit, signed)
107 x = 127;
108 ++x;
109 // CHECK: {{.*}}integer-conversion-incdec.c:[[@LINE-1]]:3: runtime error: implicit conversion from type 'int' of value 128 (32-bit, signed) to type 'signed char' changed the value to -128 (8-bit, signed)
110
111 x = 127;
112 x--;
113 x = 127;
114 --x;
115}
116
117int main() {
118 test_unsigned();
119 test_signed();
120
121 return 0;
122}
123

source code of compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-conversion-incdec.c