xref: /llvm-project/clang/test/C/C23/n2900_n3011.c (revision 50c81128de8616117118564eff22cf508cba7848)
1 // RUN: %clang_cc1 -std=c2x -fsyntax-only -Wpre-c2x-compat -verify=compat %s
2 // RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -Wno-comment -verify=pedantic %s
3 
4 /* WG14 N2900: yes
5  * Consistent, Warningless, and Intuitive Initialization with {}
6  */
7 
8 /* WG14 N3011: yes
9  * Consistent, Warningless, and Intuitive Initialization with {}
10  */
test(void)11 void test(void) {
12   struct S { int x, y; } s = {}; // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
13                                     pedantic-warning {{use of an empty initializer is a C23 extension}}
14   int i = {}; // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
15                                   pedantic-warning {{use of an empty initializer is a C23 extension}}
16   int j = (int){}; // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
17                       pedantic-warning {{use of an empty initializer is a C23 extension}}
18 
19   // C2x 6.7.10p4 says, in part: An array of unknown size shall not be
20   // initialized by an empty initializer.
21   // However, Clang allows zero-sized arrays as an extension in both C and C++,
22   // and this initialization form will deduce the array extent as zero. Given
23   // that we support empty initialization of an unbounded array in C++, we also
24   // support it in C.
25   int unknown_size[] = {}; // pedantic-warning {{zero size arrays are an extension}} \
26                               pedantic-warning {{use of an empty initializer is a C23 extension}} \
27                               compat-warning {{use of an empty initializer is incompatible with C standards before C23}}
28   int vla[i] = {}; // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
29                       pedantic-warning {{use of an empty initializer is a C23 extension}}
30   // C99 6.5.2.5 Compound literals constraint 1: The type name shall specify an
31   // object type or an array of unknown size, but not a variable length array
32   // type.
33   int *compound_literal_vla = (int[i]){}; // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
34                                              pedantic-warning {{use of an empty initializer is a C23 extension}}\
35                                              compat-error {{compound literal cannot be of variable-length array type}} \
36                                              pedantic-error {{compound literal cannot be of variable-length array type}}\
37 
38 
39   struct T {
40 	int i;
41     struct S s;
42   } t1 = { 1, {} }; // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
43                        pedantic-warning {{use of an empty initializer is a C23 extension}}
44 
45   struct T t2 = {
46     1, {
47       2, {} // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
48                pedantic-warning {{use of an empty initializer is a C23 extension}}
49     }
50   };
51 
52   struct T t3 = {
53     (int){}, // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
54                 pedantic-warning {{use of an empty initializer is a C23 extension}}
55     {} // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
56           pedantic-warning {{use of an empty initializer is a C23 extension}}
57   };
58 
59   // Ensure that zero initialization does what you'd expect in a constant expr.
60   // FIXME: the "not an ICE" warning is incorrect for C2x, but we don't yet
61   // implement WG14 N3038.
62   _Static_assert((int){} == 0, "what?");  // compat-warning {{use of an empty initializer is incompatible with C standards before C23}} \
63                                              pedantic-warning {{use of an empty initializer is a C23 extension}} \
64                                              pedantic-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
65 }
66 
67