| 1 | // RUN: %clang -x c -fsanitize=implicit-integer-sign-change -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-integer-sign-change -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-integer-sign-change -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-integer-sign-change -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-integer-sign-change -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-integer-sign-change -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-integer-sign-change -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-integer-sign-change -O3 %s -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK |
| 10 | |
| 11 | void 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-sign-change-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-sign-change-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 | x = 255; |
| 49 | ++x; |
| 50 | |
| 51 | x = 255; |
| 52 | x--; |
| 53 | x = 255; |
| 54 | --x; |
| 55 | } |
| 56 | |
| 57 | void test_signed() { |
| 58 | signed char x; |
| 59 | |
| 60 | x = -128; |
| 61 | x++; |
| 62 | x = -128; |
| 63 | ++x; |
| 64 | |
| 65 | x = -128; |
| 66 | x--; |
| 67 | // CHECK: {{.*}}integer-sign-change-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) |
| 68 | x = -128; |
| 69 | --x; |
| 70 | // CHECK: {{.*}}integer-sign-change-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) |
| 71 | |
| 72 | x = -1; |
| 73 | x++; |
| 74 | x = -1; |
| 75 | ++x; |
| 76 | |
| 77 | x = -1; |
| 78 | x--; |
| 79 | x = -1; |
| 80 | --x; |
| 81 | |
| 82 | x = 0; |
| 83 | x++; |
| 84 | x = 0; |
| 85 | ++x; |
| 86 | |
| 87 | x = 0; |
| 88 | x--; |
| 89 | x = 0; |
| 90 | --x; |
| 91 | |
| 92 | x = 1; |
| 93 | x++; |
| 94 | x = 1; |
| 95 | ++x; |
| 96 | |
| 97 | x = 1; |
| 98 | x--; |
| 99 | x = 1; |
| 100 | --x; |
| 101 | |
| 102 | x = 127; |
| 103 | x++; |
| 104 | // CHECK: {{.*}}integer-sign-change-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) |
| 105 | x = 127; |
| 106 | ++x; |
| 107 | // CHECK: {{.*}}integer-sign-change-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) |
| 108 | |
| 109 | x = 127; |
| 110 | x--; |
| 111 | x = 127; |
| 112 | --x; |
| 113 | } |
| 114 | |
| 115 | int main() { |
| 116 | test_unsigned(); |
| 117 | test_signed(); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |