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