Debian Codegraph @

clang/test/C/C23/n3018.c

raw
1 // RUN: %clang_cc1 -std=c23 -verify -triple x86_64 -pedantic -Wno-conversion -Wno-constant-conversion %s
2 // RUN: %clang_cc1 -std=c23 -verify -triple x86_64 -pedantic -Wno-conversion -Wno-constant-conversion -fexperimental-new-constant-interpreter %s
3
4 /* WG14 N3018: Full
5 * The constexpr specifier for object definitions
6 */
7
8 #define ULLONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
9 #define UINT_MAX (__INT_MAX__ *2U +1U)
10
11 void Example0() {
12 constexpr unsigned int minusOne = -1;
13 // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned int'}}
14 constexpr unsigned int uint_max = -1U;
15 constexpr double onethird = 1.0/3.0;
16 constexpr double onethirdtrunc = (double)(1.0/3.0);
17
18 constexpr char string[] = { "\xFF", };
19 constexpr unsigned char ucstring[] = { "\xFF", };
20 // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned char'}}
21 constexpr char string1[] = { -1, 0, };
22 constexpr unsigned char ucstring1[] = { -1, 0, };
23 // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned char'}}
24
25 // TODO: Make sure these work correctly once char8_t and _Decimal are supported
26 // constexpr char8_t u8string[] = { 255, 0, }; // ok
27 // constexpr char8_t u8string[] = { u8"\xFF", }; // ok
28 // constexpr _Decimal32 small = DEC64_TRUE_MIN * 0;// constraint violation
29 }
30
31 void Example1() {
32 constexpr int K = 47;
33 enum {
34 A = K,
35 };
36 constexpr int L = K;
37 static int b = K + 1;
38 int array[K];
39 _Static_assert(K == 47);
40 }
41
42 constexpr int K = 47;
43 static const int b = K + 1;
44
45 void Example2() {
46 constexpr int A = 42LL;
47 constexpr signed short B = ULLONG_MAX;
48 // expected-error@-1 {{constexpr initializer evaluates to 18446744073709551615 which is not exactly representable in type 'const short'}}
49 constexpr float C = 47u;
50
51 constexpr float D = 432000000;
52 constexpr float E = 1.0 / 3.0;
53 // expected-error@-1 {{constexpr initializer evaluates to 3.333333e-01 which is not exactly representable in type 'const float'}}
54 constexpr float F = 1.0f / 3.0f;
55 }
56
57
58 void Example3() {
59 constexpr static unsigned short array[] = {
60 3000,
61 300000,
62 // expected-error@-1 {{constexpr initializer evaluates to 300000 which is not exactly representable in type 'const unsigned short'}}
63 -1
64 // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned short'}}
65 };
66
67 constexpr static unsigned short array1[] = {
68 3000,
69 3000,
70 -1
71 // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned short'}}
72 };
73
74 struct S {
75 int x, y;
76 };
77 constexpr struct S s = {
78 .x = __INT_MAX__,
79 .y = UINT_MAX,
80 // expected-error@-1 {{constexpr initializer evaluates to 4294967295 which is not exactly representable in type 'int'}}
81 };
82 }
83
84 void Example4() {
85 struct s { void *p; };
86 constexpr struct s A = { nullptr };
87 constexpr struct s B = A;
88 }