annotate clang/test/SemaCXX/anonymous-struct.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
anatofuz
parents:
diff changeset
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
anatofuz
parents:
diff changeset
4 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
anatofuz
parents:
diff changeset
5
anatofuz
parents:
diff changeset
6 struct S {
anatofuz
parents:
diff changeset
7 S();
anatofuz
parents:
diff changeset
8 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
9 // expected-note@-2 {{because type 'S' has a user-provided default constructor}}
anatofuz
parents:
diff changeset
10 #endif
anatofuz
parents:
diff changeset
11 };
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 struct { // expected-error {{anonymous structs and classes must be class members}} expected-warning {{does not declare anything}}
anatofuz
parents:
diff changeset
14 };
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 struct E {
anatofuz
parents:
diff changeset
17 struct {
anatofuz
parents:
diff changeset
18 S x;
anatofuz
parents:
diff changeset
19 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
20 // expected-error@-2 {{anonymous struct member 'x' has a non-trivial default constructor}}
anatofuz
parents:
diff changeset
21 #endif
anatofuz
parents:
diff changeset
22 };
anatofuz
parents:
diff changeset
23 static struct { // expected-warning {{does not declare anything}}
anatofuz
parents:
diff changeset
24 };
anatofuz
parents:
diff changeset
25 class {
anatofuz
parents:
diff changeset
26 int anon_priv_field; // expected-error {{anonymous struct cannot contain a private data member}}
anatofuz
parents:
diff changeset
27 };
anatofuz
parents:
diff changeset
28 };
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 template <class T> void foo(T);
anatofuz
parents:
diff changeset
31 typedef 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}}
anatofuz
parents:
diff changeset
32 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
33 // expected-note@-2 {{declared here}}
anatofuz
parents:
diff changeset
34 #endif
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 void test() { // expected-note {{type is not C-compatible due to this member declaration}}
anatofuz
parents:
diff changeset
37 foo(this);
anatofuz
parents:
diff changeset
38 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
39 // expected-warning@-2 {{template argument uses unnamed type}}
anatofuz
parents:
diff changeset
40 #endif
anatofuz
parents:
diff changeset
41 }
anatofuz
parents:
diff changeset
42 } A; // expected-note {{type is given name 'A' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
45 int x = 0; // expected-note {{type is not C-compatible due to this default member initializer}} expected-warning 0-1{{extension}}
anatofuz
parents:
diff changeset
46 } B; // expected-note {{type is given name 'B' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 typedef struct // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
49 : B { // expected-note {{type is not C-compatible due to this base class}}
anatofuz
parents:
diff changeset
50 } C; // expected-note {{type is given name 'C' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
51
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
52 #if __cplusplus > 201703L && __cplusplus < 202002L
150
anatofuz
parents:
diff changeset
53 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
54 static_assert([]{ return true; }()); // expected-note {{type is not C-compatible due to this lambda expression}}
anatofuz
parents:
diff changeset
55 } Lambda1; // expected-note {{type is given name 'Lambda1' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 template<int> struct X {};
anatofuz
parents:
diff changeset
58 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
59 X<[]{ return 0; }()> x; // expected-note {{type is not C-compatible due to this lambda expression}}
anatofuz
parents:
diff changeset
60 // FIXME: expected-error@-1 {{lambda expression cannot appear}}
anatofuz
parents:
diff changeset
61 } Lambda2; // expected-note {{type is given name 'Lambda2' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
64 enum E {
anatofuz
parents:
diff changeset
65 a = []{ return 1; }() // expected-note {{type is not C-compatible due to this lambda expression}}
anatofuz
parents:
diff changeset
66 };
anatofuz
parents:
diff changeset
67 } Lambda3; // expected-note {{type is given name 'Lambda3' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
68 #endif
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
71 template<int> void f() {} // expected-note {{type is not C-compatible due to this member declaration}}
anatofuz
parents:
diff changeset
72 } Template; // expected-note {{type is given name 'Template' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
75 struct U {
anatofuz
parents:
diff changeset
76 void f(); // expected-note {{type is not C-compatible due to this member declaration}}
anatofuz
parents:
diff changeset
77 };
anatofuz
parents:
diff changeset
78 } Nested; // expected-note {{type is given name 'Nested' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
81 friend void f() {} // expected-note {{type is not C-compatible due to this friend declaration}}
anatofuz
parents:
diff changeset
82 } Friend; // expected-note {{type is given name 'Friend' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
anatofuz
parents:
diff changeset
85 template<typename T> friend void f() {} // expected-note {{type is not C-compatible due to this friend declaration}}
anatofuz
parents:
diff changeset
86 } FriendTemplate; // expected-note {{type is given name 'FriendTemplate' for linkage purposes by this typedef declaration}}
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 // Check that we don't diagnose the permitted cases:
anatofuz
parents:
diff changeset
89 typedef struct {
anatofuz
parents:
diff changeset
90 // (non-members)
anatofuz
parents:
diff changeset
91 _Static_assert(true, "");
anatofuz
parents:
diff changeset
92 int : 0;
anatofuz
parents:
diff changeset
93 /*empty-declaration*/;
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 // non-static data members
anatofuz
parents:
diff changeset
96 int a;
anatofuz
parents:
diff changeset
97 // member enumerations
anatofuz
parents:
diff changeset
98 enum E { x, y, z };
anatofuz
parents:
diff changeset
99 // member classes
anatofuz
parents:
diff changeset
100 struct S {};
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 // recursively
anatofuz
parents:
diff changeset
103 struct T { int a; };
anatofuz
parents:
diff changeset
104 } OK;
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 // There are still some known permitted cases that require an early linkage
anatofuz
parents:
diff changeset
107 // computation. Ensure we diagnose those too.
anatofuz
parents:
diff changeset
108 namespace ValidButUnsupported {
anatofuz
parents:
diff changeset
109 #if __cplusplus >= 201402L
anatofuz
parents:
diff changeset
110 template<typename T> auto compute_linkage() {
anatofuz
parents:
diff changeset
111 static int n;
anatofuz
parents:
diff changeset
112 return &n;
anatofuz
parents:
diff changeset
113 }
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 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}}
anatofuz
parents:
diff changeset
116 struct X {};
anatofuz
parents:
diff changeset
117 decltype(compute_linkage<X>()) a;
anatofuz
parents:
diff changeset
118 } A; // expected-note {{by this typedef declaration}}
anatofuz
parents:
diff changeset
119 #endif
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 // This fails in some language modes but not others.
anatofuz
parents:
diff changeset
122 template<typename T> struct Y {
anatofuz
parents:
diff changeset
123 static const int value = 10;
anatofuz
parents:
diff changeset
124 };
anatofuz
parents:
diff changeset
125 typedef struct { // expected-error 0-1{{unsupported}}
anatofuz
parents:
diff changeset
126 enum X {};
anatofuz
parents:
diff changeset
127 int arr[Y<X>::value];
anatofuz
parents:
diff changeset
128 } B; // expected-note 0-1{{by this typedef}}
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 template<typename T> void f() {}
anatofuz
parents:
diff changeset
131 typedef struct { // expected-error {{unsupported}}
anatofuz
parents:
diff changeset
132 enum X {};
anatofuz
parents:
diff changeset
133 int arr[&f<X> ? 1 : 2];
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
134 #if __cplusplus < 201103L
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
135 // expected-warning@-2 {{folded to constant}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
136 #endif
150
anatofuz
parents:
diff changeset
137 } C; // expected-note {{by this typedef}}
anatofuz
parents:
diff changeset
138 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
139
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
140 namespace ImplicitDecls {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
141 struct Destructor {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
142 ~Destructor() {}
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
143 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
144 typedef struct {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
145 } Empty;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
146
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
147 typedef struct {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
148 Destructor x;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
149 } A;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
150
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
151 typedef struct {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
152 Empty E;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
153 } B;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
154
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
155 typedef struct {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
156 const Empty E;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
157 } C;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
158 } // namespace ImplicitDecls
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 struct {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 static int x; // expected-error {{static data member 'x' not allowed in anonymous struct}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
162 } static_member_1;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
164 class {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 struct A {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
166 static int x; // expected-error {{static data member 'x' not allowed in anonymous class}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
167 } x;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
168 } static_member_2;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
169
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
170 union {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
171 struct A {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
172 struct B {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
173 static int x; // expected-error {{static data member 'x' not allowed in anonymous union}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
174 } x;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 } x;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176 } static_member_3;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
177
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
178 // Ensure we don't compute the linkage of a member function just because it
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179 // happens to have the same name as a builtin.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
180 namespace BuiltinName {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
181 // Note that this is not an error: we didn't trigger linkage computation in this example.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
182 typedef struct { // expected-warning {{anonymous non-C-compatible type}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
183 void memcpy(); // expected-note {{due to this member}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
184 } A; // expected-note {{given name 'A' for linkage purposes by this typedef}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
185 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
186 namespace inline_defined_static_member {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
187 typedef struct { // expected-warning {{anonymous non-C-compatible type}}
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
188 static void f() { // expected-note {{due to this member}}
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
189 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
190 } A; // expected-note {{given name 'A' for linkage purposes by this typedef}}
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
191 }
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
192
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
193 #if __cplusplus > 201103L
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
194 namespace GH58800 {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
195 struct A {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
196 union {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
197 struct {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
198 float red = 0.0f;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
199 };
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
200 };
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
201 };
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
202
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
203 A GetA() {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
204 A result{};
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
205 return result;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
206 }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
207 }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
208 #endif