[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / extern-c.cpp
blob68d1494b94918f8306f7cedc96843d78694e5f98
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
3 namespace test1 {
4 extern "C" {
5 void test1_f() {
6 void test1_g(int);
10 int test1_g(int);
12 namespace test2 {
13 extern "C" {
14 void test2_f() {
15 extern int test2_x; // expected-note {{declared with C language linkage here}}
19 float test2_x; // expected-error {{declaration of 'test2_x' in global scope conflicts with declaration with C language linkage}}
21 namespace test3 {
22 extern "C" {
23 void test3_f() {
24 extern int test3_b; // expected-note {{previous declaration is here}}
27 extern "C" {
28 float test3_b; // expected-error {{redefinition of 'test3_b' with a different type: 'float' vs 'int'}}
32 namespace N {
33 extern "C" {
34 void test4_f() {
35 extern int test4_b; // expected-note {{declared with C language linkage here}}
39 static float test4_b; // expected-error {{declaration of 'test4_b' in global scope conflicts with declaration with C language linkage}}
41 extern "C" {
42 void test4c_f() {
43 extern int test4_c; // expected-note {{previous}}
46 static float test4_c; // expected-error {{redefinition of 'test4_c' with a different type: 'float' vs 'int'}}
48 namespace N {
49 extern "C" {
50 void test5_f() {
51 extern int test5_b; // expected-note {{declared with C language linkage here}}
55 extern "C" {
56 static float test5_b; // expected-error {{declaration of 'test5_b' in global scope conflicts with declaration with C language linkage}}
59 extern "C" {
60 void test5c_f() {
61 extern int test5_c; // expected-note {{previous}}
64 extern "C" {
65 static float test5_c; // expected-error {{redefinition of 'test5_c' with a different type: 'float' vs 'int'}}
68 extern "C" {
69 void f() {
70 extern int test6_b;
73 namespace foo {
74 extern "C" {
75 static float test6_b;
76 extern float test6_b;
80 namespace linkage {
81 namespace redecl {
82 extern "C" {
83 static void linkage_redecl();
84 static void linkage_redecl(int);
85 void linkage_redecl(); // ok, still not extern "C"
86 void linkage_redecl(int); // ok, still not extern "C"
87 void linkage_redecl(float); // expected-note {{previous}}
88 void linkage_redecl(double); // expected-error {{conflicting types}}
91 namespace from_outer {
92 void linkage_from_outer_1(); // expected-note {{previous}}
93 void linkage_from_outer_2(); // expected-note {{previous}}
94 extern "C" {
95 void linkage_from_outer_1(int);
96 void linkage_from_outer_1(); // expected-error {{different language linkage}}
97 void linkage_from_outer_2(); // expected-error {{different language linkage}}
100 namespace mixed {
101 extern "C" {
102 void linkage_mixed_1();
103 static void linkage_mixed_1(int);
105 static void linkage_mixed_2(int);
106 void linkage_mixed_2();
109 namespace across_scopes {
110 namespace X {
111 extern "C" void linkage_across_scopes_f() {
112 void linkage_across_scopes_g(); // expected-note {{previous}}
115 namespace Y {
116 extern "C" void linkage_across_scopes_g(int); // expected-error {{conflicting}}
121 int lookup_in_global_f; // expected-note {{here}}
122 namespace lookup_in_global {
123 void lookup_in_global_f();
124 void lookup_in_global_g();
125 extern "C" {
126 void lookup_in_global_f(int); // expected-error {{conflicts with declaration in global scope}}
127 void lookup_in_global_g(int); // expected-note {{here}}
130 int lookup_in_global_g; // expected-error {{conflicts with declaration with C language linkage}}
132 namespace N1 {
133 extern "C" int different_kind_1; // expected-note {{here}}
134 extern "C" void different_kind_2(); // expected-note {{here}}
136 namespace N2 {
137 extern "C" void different_kind_1(); // expected-error {{different kind of symbol}}
138 extern "C" int different_kind_2; // expected-error {{different kind of symbol}}
141 // We allow all these even though the standard says they are ill-formed.
142 extern "C" {
143 struct stat {}; // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
144 void stat(struct stat);
146 namespace X {
147 extern "C" {
148 void stat(struct ::stat);
151 int stat(int *p);
152 void global_fn_vs_extern_c_var_1();
153 namespace X {
154 extern "C" int global_fn_vs_extern_c_var_1;
155 extern "C" int global_fn_vs_extern_c_var_2;
157 void global_fn_vs_extern_c_var_2();
158 void global_fn_vs_extern_c_fn_1();
159 namespace X {
160 extern "C" int global_fn_vs_extern_c_fn_1(int);
161 extern "C" int global_fn_vs_extern_c_fn_2(int);
163 void global_fn_vs_extern_c_fn_2();
164 extern "C" void name_with_using_decl_1(int);
165 namespace using_decl {
166 void name_with_using_decl_1();
167 void name_with_using_decl_2();
168 void name_with_using_decl_3();
170 using using_decl::name_with_using_decl_1;
171 using using_decl::name_with_using_decl_2;
172 extern "C" void name_with_using_decl_2(int);
173 extern "C" void name_with_using_decl_3(int);
174 using using_decl::name_with_using_decl_3;
176 // We do not allow a global variable and an extern "C" function to have the same
177 // name, because such entities may have the same mangled name.
178 int global_var_vs_extern_c_fn_1; // expected-note {{here}}
179 namespace X {
180 extern "C" void global_var_vs_extern_c_fn_1(); // expected-error {{conflicts with declaration in global scope}}
181 extern "C" void global_var_vs_extern_c_fn_2(); // expected-note {{here}}
183 int global_var_vs_extern_c_fn_2; // expected-error {{conflicts with declaration with C language linkage}}
184 int global_var_vs_extern_c_var_1; // expected-note {{here}}
185 namespace X {
186 extern "C" double global_var_vs_extern_c_var_1; // expected-error {{conflicts with declaration in global scope}}
187 extern "C" double global_var_vs_extern_c_var_2; // expected-note {{here}}
189 int global_var_vs_extern_c_var_2; // expected-error {{conflicts with declaration with C language linkage}}
191 template <class T> struct pr5065_n1 {};
192 extern "C" {
193 union pr5065_1 {}; // expected-warning{{empty union has size 0 in C, size 1 in C++}}
194 struct pr5065_2 { int: 0; }; // expected-warning{{struct has size 0 in C, size 1 in C++}}
195 struct pr5065_3 {}; // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
196 struct pr5065_4 { // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
197 struct Inner {}; // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
199 // These should not warn
200 class pr5065_n3 {};
201 pr5065_n1<int> pr5065_v;
202 struct pr5065_n4 { void m() {} };
203 struct pr5065_n5 : public pr5065_3 {};
204 struct pr5065_n6 : public virtual pr5065_3 {};
206 struct pr5065_n7 {};
208 namespace tag_hiding {
209 namespace namespace_with_injected_name {
210 class Boo {
211 friend struct ExternCStruct1;
213 void ExternCStruct4(); // expected-note 2{{candidate}}
216 class Baz {
217 friend struct ExternCStruct2;
218 friend void ExternCStruct3();
221 using namespace namespace_with_injected_name;
223 extern "C" {
224 struct ExternCStruct1;
225 struct ExternCStruct2;
226 struct ExternCStruct3;
227 struct ExternCStruct4; // expected-note {{candidate}}
229 ExternCStruct1 *p1;
230 ExternCStruct2 *p2;
231 ExternCStruct3 *p3;
232 ExternCStruct4 *p4; // expected-error {{ambiguous}}
234 extern "C" {
235 struct ExternCStruct1;
236 struct ExternCStruct2;
237 struct ExternCStruct3;
238 struct ExternCStruct4; // expected-note {{candidate}}
240 ExternCStruct1 *q1 = p1;
241 ExternCStruct2 *q2 = p2;
242 ExternCStruct3 *q3 = p3;
243 ExternCStruct4 *q4 = p4; // expected-error {{ambiguous}}
246 namespace PR35697 {
247 typedef struct {} *ReturnStruct;
248 extern "C" ReturnStruct PR35697_f();
249 extern "C" ReturnStruct PR35697_v;
250 ReturnStruct PR35697_f();
251 ReturnStruct PR35697_v;
253 namespace {
254 extern "C" ReturnStruct PR35697_f();
255 extern "C" ReturnStruct PR35697_v;
256 void q() {
257 extern ReturnStruct PR35697_f();
258 extern ReturnStruct PR35697_v;
263 namespace PR46859 {
264 extern "bogus" // expected-error {{unknown linkage language}}
265 template<int> struct X {}; // expected-error {{templates can only be declared in namespace or class scope}}