224 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s2// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx98 -std=c++98 %s3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s4// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s5 6struct S {7 S();8#if __cplusplus <= 199711L9 // expected-note@-2 {{because type 'S' has a user-provided default constructor}}10#endif11};12 13struct { // expected-error {{anonymous structs and classes must be class members}} expected-warning {{does not declare anything}}14};15 16struct E {17 struct {18 S x;19#if __cplusplus <= 199711L20 // expected-error@-2 {{anonymous struct member 'x' has a non-trivial default constructor}}21#endif22 };23 static struct { // expected-warning {{does not declare anything}}24 };25 class {26 int anon_priv_field; // expected-error {{anonymous struct cannot contain a private data member}}27 };28};29 30template <class T> void foo(T);31typedef struct { // expected-error {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration after its linkage was computed; add a tag name here to establish linkage prior to definition}}32// expected-note@-1 {{unnamed type used in template argument was declared here}}33 34 void test() { // expected-note {{type is not C-compatible due to this member declaration}}35 foo(this);36#if __cplusplus <= 199711L37 // expected-warning@-2 {{template argument uses unnamed type}}38 // expected-note@-3 {{while substituting deduced template arguments}}39#endif40 }41} A; // expected-note {{type is given name 'A' for linkage purposes by this typedef declaration}}42 43typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}44 int x = 0; // expected-note {{type is not C-compatible due to this default member initializer}} expected-warning 0-1{{extension}}45} B; // expected-note {{type is given name 'B' for linkage purposes by this typedef declaration}}46 47typedef struct // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}48: B { // expected-note {{type is not C-compatible due to this base class}}49} C; // expected-note {{type is given name 'C' for linkage purposes by this typedef declaration}}50 51#if __cplusplus > 201703L && __cplusplus < 202002L52typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}53 static_assert([]{ return true; }()); // expected-note {{type is not C-compatible due to this lambda expression}}54} Lambda1; // expected-note {{type is given name 'Lambda1' for linkage purposes by this typedef declaration}}55 56template<int> struct X {};57typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}58 X<[]{ return 0; }()> x; // expected-note {{type is not C-compatible due to this lambda expression}}59 // FIXME: expected-error@-1 {{lambda expression cannot appear}}60} Lambda2; // expected-note {{type is given name 'Lambda2' for linkage purposes by this typedef declaration}}61 62typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}63 enum E {64 a = []{ return 1; }() // expected-note {{type is not C-compatible due to this lambda expression}}65 };66} Lambda3; // expected-note {{type is given name 'Lambda3' for linkage purposes by this typedef declaration}}67#endif68 69typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}70 template<int> void f() {} // expected-note {{type is not C-compatible due to this member declaration}}71} Template; // expected-note {{type is given name 'Template' for linkage purposes by this typedef declaration}}72 73typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}74 struct U {75 void f(); // expected-note {{type is not C-compatible due to this member declaration}}76 };77} Nested; // expected-note {{type is given name 'Nested' for linkage purposes by this typedef declaration}}78 79typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}80 friend void f() {} // expected-note {{type is not C-compatible due to this friend declaration}}81} Friend; // expected-note {{type is given name 'Friend' for linkage purposes by this typedef declaration}}82 83typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}84 template<typename T> friend void f() {} // expected-note {{type is not C-compatible due to this friend declaration}}85} FriendTemplate; // expected-note {{type is given name 'FriendTemplate' for linkage purposes by this typedef declaration}}86 87// Check that we don't diagnose the permitted cases:88typedef struct {89 // (non-members)90 _Static_assert(true, "");91 int : 0;92 /*empty-declaration*/;93 94 // non-static data members95 int a;96 // member enumerations97 enum E { x, y, z };98 // member classes99 struct S {};100 101 // recursively102 struct T { int a; };103} OK;104 105// There are still some known permitted cases that require an early linkage106// computation. Ensure we diagnose those too.107namespace ValidButUnsupported {108#if __cplusplus >= 201402L109 template<typename T> auto compute_linkage() {110 static int n;111 return &n;112 }113 114 typedef struct { // expected-error {{unsupported: anonymous type given name for linkage purposes by typedef declaration after its linkage was computed; add a tag name here to establish linkage}}115 struct X {};116 decltype(compute_linkage<X>()) a;117 } A; // expected-note {{by this typedef declaration}}118#endif119 120 // This fails in some language modes but not others.121 template<typename T> struct Y {122 static const int value = 10;123 };124 typedef struct { // expected-error 0-1{{unsupported}}125 enum X {};126 int arr[Y<X>::value];127 } B; // expected-note 0-1{{by this typedef}}128 129 template<typename T> void f() {}130 typedef struct { // expected-error {{unsupported}}131 enum X {};132 int arr[&f<X> ? 1 : 2];133#if __cplusplus < 201103L134 // expected-warning@-2 {{folded to constant}}135 // expected-warning@-3 {{variable length arrays in C++ are a Clang extension}}136#endif137 } C; // expected-note {{by this typedef}}138}139 140namespace ImplicitDecls {141struct Destructor {142 ~Destructor() {}143};144typedef struct {145} Empty;146 147typedef struct {148 Destructor x;149} A;150 151typedef struct {152 Empty E;153} B;154 155typedef struct {156 const Empty E;157} C;158} // namespace ImplicitDecls159 160struct {161 static int x; // expected-error {{static data member 'x' not allowed in anonymous struct}}162} static_member_1;163 164class {165 struct A {166 static int x; // expected-error {{static data member 'x' not allowed in anonymous class}}167 } x;168} static_member_2;169 170union {171 struct A {172 struct B {173 static int x; // expected-error {{static data member 'x' not allowed in anonymous union}}174 } x;175 } x;176} static_member_3;177 178// Ensure we don't compute the linkage of a member function just because it179// happens to have the same name as a builtin.180namespace BuiltinName {181 // Note that this is not an error: we didn't trigger linkage computation in this example.182 typedef struct { // expected-warning {{anonymous non-C-compatible type}}183 void memcpy(); // expected-note {{due to this member}}184 } A; // expected-note {{given name 'A' for linkage purposes by this typedef}}185}186namespace inline_defined_static_member {187typedef struct { // expected-warning {{anonymous non-C-compatible type}}188 static void f() { // expected-note {{due to this member}}189 }190} A; // expected-note {{given name 'A' for linkage purposes by this typedef}}191}192 193#if __cplusplus > 201103L194namespace GH58800 {195struct A {196 union {197 struct {198 float red = 0.0f;199 };200 };201};202 203A GetA() {204 A result{};205 return result;206}207}208#endif209 210namespace ForwardDeclaredMember {211 struct A;212 struct A {213 int x = 0;214 // cxx98-warning@-1 {{default member initializer for non-static data member is a C++11 extension}}215 // cxx98-note@-2 {{because field 'x' has an initializer}}216 };217 struct B {218 struct {219 A y;220 // cxx98-error@-1 {{anonymous struct member 'y' has a non-trivial default constructor}}221 };222 };223}224