Peter Collingbourne | 49d29ed | 2013-10-20 21:29:32 +0000 | [diff] [blame^] | 1 | // RUN: %clangxx -DLSH_OVERFLOW -DOP='<<' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-LSH_OVERFLOW |
| 2 | // RUN: %clangxx -DLSH_OVERFLOW -DOP='<<=' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-LSH_OVERFLOW |
| 3 | // RUN: %clangxx -DTOO_LOW -DOP='<<' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_LOW |
| 4 | // RUN: %clangxx -DTOO_LOW -DOP='>>' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_LOW |
| 5 | // RUN: %clangxx -DTOO_LOW -DOP='<<=' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_LOW |
| 6 | // RUN: %clangxx -DTOO_LOW -DOP='>>=' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_LOW |
| 7 | // RUN: %clangxx -DTOO_HIGH -DOP='<<' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_HIGH |
| 8 | // RUN: %clangxx -DTOO_HIGH -DOP='>>' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_HIGH |
| 9 | // RUN: %clangxx -DTOO_HIGH -DOP='<<=' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_HIGH |
| 10 | // RUN: %clangxx -DTOO_HIGH -DOP='>>=' -fsanitize=shift %s -o %t && %t 2>&1 | FileCheck %s --check-prefix=CHECK-TOO_HIGH |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 11 | |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | int main() { |
| 15 | int a = 1; |
| 16 | unsigned b = 1; |
| 17 | |
| 18 | a <<= 31; // ok in C++11, not ok in C99/C11 |
| 19 | b <<= 31; // ok |
| 20 | b <<= 1; // still ok, unsigned |
| 21 | |
| 22 | #ifdef LSH_OVERFLOW |
Will Dietz | 7cbd7e5 | 2012-12-02 18:43:33 +0000 | [diff] [blame] | 23 | // CHECK-LSH_OVERFLOW: shift.cpp:24:5: runtime error: left shift of negative value -2147483648 |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 24 | a OP 1; |
| 25 | #endif |
| 26 | |
| 27 | #ifdef TOO_LOW |
Will Dietz | 7cbd7e5 | 2012-12-02 18:43:33 +0000 | [diff] [blame] | 28 | // CHECK-TOO_LOW: shift.cpp:29:5: runtime error: shift exponent -3 is negative |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 29 | a OP (-3); |
| 30 | #endif |
| 31 | |
| 32 | #ifdef TOO_HIGH |
| 33 | a = 0; |
Will Dietz | 7cbd7e5 | 2012-12-02 18:43:33 +0000 | [diff] [blame] | 34 | // CHECK-TOO_HIGH: shift.cpp:35:5: runtime error: shift exponent 32 is too large for 32-bit type 'int' |
Richard Smith | 6ebe451 | 2012-10-09 19:34:32 +0000 | [diff] [blame] | 35 | a OP 32; |
| 36 | #endif |
| 37 | } |