1*a68d90faSPeter Collingbourne // RUN: %clangxx -DADD_I32 -fsanitize=signed-integer-overflow %s -o %t1 && %run %t1 2>&1 | FileCheck %s --check-prefix=CHECK-ADD_I32
2*a68d90faSPeter Collingbourne // RUN: %clangxx -DADD_I64 -fsanitize=signed-integer-overflow %s -o %t2 && %run %t2 2>&1 | FileCheck %s --check-prefix=CHECK-ADD_I64
3*a68d90faSPeter Collingbourne // RUN: %clangxx -DADD_I128 -fsanitize=signed-integer-overflow %s -o %t3 && %run %t3 2>&1 | FileCheck %s --check-prefix=CHECK-ADD_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(0x7f) + int8_t(0x7f));
11ba869e79SAlexey Samsonov   (void)(int16_t(0x3fff) + int16_t(0x4000));
12ba869e79SAlexey Samsonov 
13ba869e79SAlexey Samsonov #ifdef ADD_I32
14ba869e79SAlexey Samsonov   int32_t k = 0x12345678;
15ba869e79SAlexey Samsonov   k += 0x789abcde;
16ba869e79SAlexey Samsonov   // CHECK-ADD_I32: add-overflow.cpp:[[@LINE-1]]:5: runtime error: signed integer overflow: 305419896 + 2023406814 cannot be represented in type 'int'
17ba869e79SAlexey Samsonov #endif
18ba869e79SAlexey Samsonov 
19ba869e79SAlexey Samsonov #ifdef ADD_I64
20ba869e79SAlexey Samsonov   (void)(int64_t(8000000000000000000ll) + int64_t(2000000000000000000ll));
21ba869e79SAlexey Samsonov   // CHECK-ADD_I64: 8000000000000000000 + 2000000000000000000 cannot be represented in type '{{long( long)?}}'
22ba869e79SAlexey Samsonov #endif
23ba869e79SAlexey Samsonov 
24ba869e79SAlexey Samsonov #ifdef ADD_I128
25*a68d90faSPeter Collingbourne # if defined(__SIZEOF_INT128__) && !defined(_WIN32)
26ba869e79SAlexey Samsonov   (void)((__int128_t(1) << 126) + (__int128_t(1) << 126));
27ba869e79SAlexey Samsonov # else
28ba869e79SAlexey Samsonov   puts("__int128 not supported");
29ba869e79SAlexey Samsonov # endif
30ba869e79SAlexey Samsonov   // CHECK-ADD_I128: {{0x40000000000000000000000000000000 \+ 0x40000000000000000000000000000000 cannot be represented in type '__int128'|__int128 not supported}}
31ba869e79SAlexey Samsonov #endif
32ba869e79SAlexey Samsonov }
33