xref: /llvm-project/clang/test/C/C23/n2683.c (revision 50c81128de8616117118564eff22cf508cba7848)
1 // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 3
2 // RUN: %clang_cc1 -triple=x86_64 -verify -ffreestanding -std=c23 %s
3 
4 /* WG14 N2683: Clang 18
5  * Towards Integer Safety
6  */
7 #include <stdckdint.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 
test_semantic()11 void test_semantic() {
12   int64_t result64 = 0;
13   int32_t result32 = 0;
14   wchar_t wide_char = L'A'; // The ascii value of `A` is 65
15 
16   bool flag_add = ckd_add(&result64, INT32_MAX, 1);
17   bool flag_sub = ckd_sub(&result32, INT32_MAX, -1);
18   bool flag_mul = ckd_mul(&result64, INT64_MAX, 1);
19 
20   bool flag = ckd_add(&result64, wide_char, result32); // In C, wchar_t is a typedef to some integer type and is allowed.
21 
22   // FIXME: add static_assert calls to check the resulting values for correctness
23   // once the constant expression interpreter is able to handle the checked arithmetic
24   // builtins in C. Currently, they're only a valid constant expression in C++ due to
25   // looking for an ICE in C. Also all values in the tests of n2683_2.c should be checked.
26 }
27 
test_invalid_input()28 void test_invalid_input() {
29   _BitInt(33) a33 = 1;
30   char char_var = 'd'; // The ascii value of `d` is 100
31   bool bool_var = 1;
32   const int const_result = 0;
33   enum week{Mon, Tue, Wed};
34   enum week day = Mon;
35   int a = 100;
36   int b = 55;
37   int result = 10;
38   char plain_char[] = {U'牛'}; /* expected-warning {{implicit conversion from 'unsigned int' to 'char' changes value from 29275 to 91}}  */
39 
40   // invalid operand argument
41   bool flag_no_bitint = ckd_add(&result, a33, a); /* expected-error {{operand argument to checked integer operation must be an integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('_BitInt(33)' invalid)}} */
42   bool flag_no_bool = ckd_sub(&result, bool_var, b); /* expected-error {{operand argument to checked integer operation must be an integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('bool' invalid)}} */
43   bool flag_no_char = ckd_mul(&result, char_var, a); /* expected-error {{operand argument to checked integer operation must be an integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('char' invalid)}} */
44   bool flag_no_enum = ckd_mul(&result, day, b); /* expected-error {{operand argument to checked integer operation must be an integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('enum week' invalid)}} */
45 
46   // invalid result type
47   bool flag_nostr = ckd_sub(&plain_char, a, b); /* expected-error {{result argument to checked integer operation must be a pointer to a non-const integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('char (*)[1]' invalid)}} */
48   bool flag_nobool = ckd_mul(&bool_var, a, b); /* expected-error {{result argument to checked integer operation must be a pointer to a non-const integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('bool *' invalid)}} */
49   bool flag_noptr = ckd_add(result, a, b); /* expected-error {{result argument to checked integer operation must be a pointer to a non-const integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('int' invalid)}} */
50   bool flag_noconst = ckd_sub(&const_result, a, b); /* expected-error {{result argument to checked integer operation must be a pointer to a non-const integer type other than plain 'char', 'bool', bit-precise, or an enumeration ('const int *' invalid)}} */
51 }
52