File: n2900_n3011.c

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (60 lines) | stat: -rw-r--r-- 3,619 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// RUN: %clang_cc1 -std=c2x -fsyntax-only -Wpre-c2x-compat -verify=compat %s
// RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -Wno-comment -verify=pedantic %s

/* WG14 N2900: yes
 * Consistent, Warningless, and Intuitive Initialization with {}
 */

/* WG14 N3011: yes
 * Consistent, Warningless, and Intuitive Initialization with {}
 */
void test(void) {
  struct S { int x, y; } s = {}; // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                                    pedantic-warning {{use of an empty initializer is a C2x extension}}
  int i = {}; // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                                  pedantic-warning {{use of an empty initializer is a C2x extension}}
  int j = (int){}; // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                      pedantic-warning {{use of an empty initializer is a C2x extension}}

  // C2x 6.7.10p4 says, in part: An array of unknown size shall not be
  // initialized by an empty initializer.
  // However, Clang allows zero-sized arrays as an extension in both C and C++,
  // and this initialization form will deduce the array extent as zero. Given
  // that we support empty initialization of an unbounded array in C++, we also
  // support it in C.
  int unknown_size[] = {}; // pedantic-warning {{zero size arrays are an extension}} \
                              pedantic-warning {{use of an empty initializer is a C2x extension}} \
                              compat-warning {{use of an empty initializer is incompatible with C standards before C2x}}
  int vla[i] = {}; // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                      pedantic-warning {{use of an empty initializer is a C2x extension}}
  int *compound_literal_vla = (int[i]){}; // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                                             pedantic-warning {{use of an empty initializer is a C2x extension}}

  struct T {
	int i;
    struct S s;
  } t1 = { 1, {} }; // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                       pedantic-warning {{use of an empty initializer is a C2x extension}}

  struct T t2 = {
    1, {
      2, {} // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
               pedantic-warning {{use of an empty initializer is a C2x extension}}
    }
  };

  struct T t3 = {
    (int){}, // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                pedantic-warning {{use of an empty initializer is a C2x extension}}
    {} // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
          pedantic-warning {{use of an empty initializer is a C2x extension}}
  };

  // Ensure that zero initialization does what you'd expect in a constant expr.
  // FIXME: the "not an ICE" warning is incorrect for C2x, but we don't yet
  // implement WG14 N3038.
  _Static_assert((int){} == 0, "what?");  // compat-warning {{use of an empty initializer is incompatible with C standards before C2x}} \
                                             pedantic-warning {{use of an empty initializer is a C2x extension}} \
                                             pedantic-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
}