11baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth// RUN: %clang_cc1 -fsyntax-only -verify %s
21baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth
31baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth//   A program that calls for default-initialization or value-initialization of
41baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth//   an entity of reference type is illformed. If T is a cv-qualified type, the
51baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth//   cv-unqualified version of T is used for these definitions of
61baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth//   zero-initialization, default-initialization, and value-initialization.
7d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith
8d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstruct S { // expected-error {{implicit default constructor for 'S' must explicitly initialize the reference member}}
9d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  int &x; // expected-note {{declared here}} expected-error 3{{reference to type 'int' requires an initializer}}
101baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth};
111baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler CarruthS s; // expected-note {{implicit default constructor for 'S' first required here}}
121baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler CarruthS f() {
13d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  return S(); // expected-note {{in value-initialization of type 'S' here}}
141baf2f778c26a71d1353ff2cc1701fcd1dbeaf76Chandler Carruth}
15d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith
16d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstruct T
17d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  : S { // expected-note 2{{in value-initialization of type 'S' here}}
18d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith};
19d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard SmithT t = T(); // expected-note {{in value-initialization of type 'T' here}}
20d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith
21d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstruct U {
22d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  T t[3]; // expected-note {{in value-initialization of type 'T' here}}
23d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith};
24d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard SmithU u = U(); // expected-note {{in value-initialization of type 'U' here}}
25d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith
26d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith// Ensure that we handle C++11 in-class initializers properly as an extension.
27d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith// In this case, there is no user-declared default constructor, so we
28d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith// recursively apply the value-initialization checks, but we will emit a
29d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith// constructor call anyway, because the default constructor is not trivial.
30d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstruct V {
31d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  int n;
32d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  int &r = n; // expected-warning {{C++11}}
33d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith};
34d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard SmithV v = V(); // ok
35d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstruct W {
36d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  int n;
37d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith  S s = { n }; // expected-warning {{C++11}}
38d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith};
39d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard SmithW w = W(); // ok
40d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith
41d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith// Ensure we're not faking this up by making the default constructor
42d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith// non-trivial.
43d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smith#define static_assert(B, S) typedef int assert_failed[(B) ? 1 : -1];
44d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstatic_assert(__has_trivial_constructor(S), "");
45d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstatic_assert(__has_trivial_constructor(T), "");
46d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstatic_assert(__has_trivial_constructor(U), "");
47d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstatic_assert(!__has_trivial_constructor(V), "");
48d5bc867f6597ee8d4eb31ea217934e436fc7c7e3Richard Smithstatic_assert(!__has_trivial_constructor(W), "");
49