1*a68d90faSPeter Collingbourne // RUN: %clangxx -DSUB_I32 -fsanitize=signed-integer-overflow %s -o %t1 && %run %t1 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I32
2*a68d90faSPeter Collingbourne // RUN: %clangxx -DSUB_I64 -fsanitize=signed-integer-overflow %s -o %t2 && %run %t2 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I64
3*a68d90faSPeter Collingbourne // RUN: %clangxx -DSUB_I128 -fsanitize=signed-integer-overflow %s -o %t3 && %run %t3 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I128
4ba869e79SAlexey Samsonov 
5ba869e79SAlexey Samsonov #include <stdint.h>
6ba869e79SAlexey Samsonov #include <stdio.h>
7ba869e79SAlexey Samsonov 
main()8ba869e79SAlexey Samsonov int main() {
9ba869e79SAlexey Samsonov   // These promote to 'int'.
10ba869e79SAlexey Samsonov   (void)(int8_t(-2) - int8_t(0x7f));
11ba869e79SAlexey Samsonov   (void)(int16_t(-2) - int16_t(0x7fff));
12ba869e79SAlexey Samsonov 
13ba869e79SAlexey Samsonov #ifdef SUB_I32
14ba869e79SAlexey Samsonov   (void)(int32_t(-2) - int32_t(0x7fffffff));
15ba869e79SAlexey Samsonov   // CHECK-SUB_I32: sub-overflow.cpp:[[@LINE-1]]:22: runtime error: signed integer overflow: -2 - 2147483647 cannot be represented in type 'int'
16ba869e79SAlexey Samsonov #endif
17ba869e79SAlexey Samsonov 
18ba869e79SAlexey Samsonov #ifdef SUB_I64
19ba869e79SAlexey Samsonov   (void)(int64_t(-8000000000000000000ll) - int64_t(2000000000000000000ll));
20ba869e79SAlexey Samsonov   // CHECK-SUB_I64: -8000000000000000000 - 2000000000000000000 cannot be represented in type '{{long( long)?}}'
21ba869e79SAlexey Samsonov #endif
22ba869e79SAlexey Samsonov 
23ba869e79SAlexey Samsonov #ifdef SUB_I128
24*a68d90faSPeter Collingbourne # if defined(__SIZEOF_INT128__) && !defined(_WIN32)
25ba869e79SAlexey Samsonov   (void)(-(__int128_t(1) << 126) - (__int128_t(1) << 126) - 1);
26ba869e79SAlexey Samsonov # else
27ba869e79SAlexey Samsonov   puts("__int128 not supported");
28ba869e79SAlexey Samsonov # endif
29ba869e79SAlexey Samsonov   // CHECK-SUB_I128: {{0x80000000000000000000000000000000 - 1 cannot be represented in type '__int128'|__int128 not supported}}
30ba869e79SAlexey Samsonov #endif
31ba869e79SAlexey Samsonov }
32