12d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx -DSUB_I32 -fsanitize=signed-integer-overflow %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I32 22d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx -DSUB_I64 -fsanitize=signed-integer-overflow %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I64 32d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx -DSUB_I128 -fsanitize=signed-integer-overflow %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUB_I128 46ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith 56ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#include <stdint.h> 60a7062f2cbb01b348bbc09be21a577957a2c68c2Richard Smith#include <stdio.h> 76ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith 86ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smithint main() { 96ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith // These promote to 'int'. 106ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith (void)(int8_t(-2) - int8_t(0x7f)); 116ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith (void)(int16_t(-2) - int16_t(0x7fff)); 126ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith 136ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#ifdef SUB_I32 146ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith (void)(int32_t(-2) - int32_t(0x7fffffff)); 157cbd7e502e993320a3a1578179d336c268b80604Will Dietz // CHECK-SUB_I32: sub-overflow.cpp:[[@LINE-1]]:22: runtime error: signed integer overflow: -2 - 2147483647 cannot be represented in type 'int' 166ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#endif 176ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith 186ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#ifdef SUB_I64 196ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith (void)(int64_t(-8000000000000000000ll) - int64_t(2000000000000000000ll)); 205b16dba10c171bc74a3d55a61b4406377aecbab8Alexey Samsonov // CHECK-SUB_I64: -8000000000000000000 - 2000000000000000000 cannot be represented in type '{{long( long)?}}' 216ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#endif 226ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith 236ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#ifdef SUB_I128 240a7062f2cbb01b348bbc09be21a577957a2c68c2Richard Smith# ifdef __SIZEOF_INT128__ 256ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith (void)(-(__int128_t(1) << 126) - (__int128_t(1) << 126) - 1); 260a7062f2cbb01b348bbc09be21a577957a2c68c2Richard Smith# else 270a7062f2cbb01b348bbc09be21a577957a2c68c2Richard Smith puts("__int128 not supported"); 280a7062f2cbb01b348bbc09be21a577957a2c68c2Richard Smith# endif 290a7062f2cbb01b348bbc09be21a577957a2c68c2Richard Smith // CHECK-SUB_I128: {{0x80000000000000000000000000000000 - 1 cannot be represented in type '__int128'|__int128 not supported}} 306ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith#endif 316ebe45146a2d93eb010b9bb5ea34cb94c6900f83Richard Smith} 32