1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 namespace test1 {
4   extern "C" {
test1_f()5     void test1_f() {
6       void test1_g(int);
7     }
8   }
9 }
10 int test1_g(int);
11 
12 namespace test2 {
13   extern "C" {
test2_f()14     void test2_f() {
15       extern int test2_x; // expected-note {{declared with C language linkage here}}
16     }
17   }
18 }
19 float test2_x; // expected-error {{declaration of 'test2_x' in global scope conflicts with declaration with C language linkage}}
20 
21 namespace test3 {
22   extern "C" {
test3_f()23     void test3_f() {
24       extern int test3_b; // expected-note {{previous declaration is here}}
25     }
26   }
27   extern "C" {
28     float test3_b; // expected-error {{redefinition of 'test3_b' with a different type: 'float' vs 'int'}}
29   }
30 }
31 
32 namespace N {
33   extern "C" {
test4_f()34     void test4_f() {
35       extern int test4_b; // expected-note {{declared with C language linkage here}}
36     }
37   }
38 }
39 static float test4_b; // expected-error {{declaration of 'test4_b' in global scope conflicts with declaration with C language linkage}}
40 
41 extern "C" {
test4c_f()42   void test4c_f() {
43     extern int test4_c; // expected-note {{previous}}
44   }
45 }
46 static float test4_c; // expected-error {{redefinition of 'test4_c' with a different type: 'float' vs 'int'}}
47 
48 namespace N {
49   extern "C" {
test5_f()50     void test5_f() {
51       extern int test5_b; // expected-note {{declared with C language linkage here}}
52     }
53   }
54 }
55 extern "C" {
56   static float test5_b; // expected-error {{declaration of 'test5_b' in global scope conflicts with declaration with C language linkage}}
57 }
58 
59 extern "C" {
test5c_f()60   void test5c_f() {
61     extern int test5_c; // expected-note {{previous}}
62   }
63 }
64 extern "C" {
65   static float test5_c; // expected-error {{redefinition of 'test5_c' with a different type: 'float' vs 'int'}}
66 }
67 
68 extern "C" {
f()69   void f() {
70     extern int test6_b;
71   }
72 }
73 namespace foo {
74   extern "C" {
75     static float test6_b;
76     extern float test6_b;
77   }
78 }
79 
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}}
89     }
90   }
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}}
98     }
99   }
100   namespace mixed {
101     extern "C" {
102       void linkage_mixed_1();
103       static void linkage_mixed_1(int);
104 
105       static void linkage_mixed_2(int);
106       void linkage_mixed_2();
107     }
108   }
109   namespace across_scopes {
110     namespace X {
linkage_across_scopes_f()111       extern "C" void linkage_across_scopes_f() {
112         void linkage_across_scopes_g(); // expected-note {{previous}}
113       }
114     }
115     namespace Y {
116       extern "C" void linkage_across_scopes_g(int); // expected-error {{conflicting}}
117     }
118   }
119 }
120 
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}}
128   }
129 }
130 int lookup_in_global_g; // expected-error {{conflicts with declaration with C language linkage}}
131 
132 namespace N1 {
133   extern "C" int different_kind_1; // expected-note {{here}}
134   extern "C" void different_kind_2(); // expected-note {{here}}
135 }
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}}
139 }
140 
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);
145 }
146 namespace X {
147   extern "C" {
148     void stat(struct ::stat);
149   }
150 }
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;
156 }
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);
162 }
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();
169 }
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;
175 
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}}
182 }
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}}
188 }
189 int global_var_vs_extern_c_var_2; // expected-error {{conflicts with declaration with C language linkage}}
190 
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++}}
198   };
199   // These should not warn
200   class pr5065_n3 {};
201   pr5065_n1<int> pr5065_v;
mpr5065_n4202   struct pr5065_n4 { void m() {} };
203   struct pr5065_n5 : public pr5065_3 {};
204   struct pr5065_n6 : public virtual pr5065_3 {};
205 }
206 struct pr5065_n7 {};
207 
208 namespace tag_hiding {
209   namespace namespace_with_injected_name {
210     class Boo {
211       friend struct ExternCStruct1;
212     };
213     void ExternCStruct4(); // expected-note 2{{candidate}}
214   }
215 
216   class Baz {
217     friend struct ExternCStruct2;
218     friend void ExternCStruct3();
219   };
220 
221   using namespace namespace_with_injected_name;
222 
223   extern "C" {
224     struct ExternCStruct1;
225     struct ExternCStruct2;
226     struct ExternCStruct3;
227     struct ExternCStruct4; // expected-note {{candidate}}
228   }
229   ExternCStruct1 *p1;
230   ExternCStruct2 *p2;
231   ExternCStruct3 *p3;
232   ExternCStruct4 *p4; // expected-error {{ambiguous}}
233 
234   extern "C" {
235     struct ExternCStruct1;
236     struct ExternCStruct2;
237     struct ExternCStruct3;
238     struct ExternCStruct4; // expected-note {{candidate}}
239   }
240   ExternCStruct1 *q1 = p1;
241   ExternCStruct2 *q2 = p2;
242   ExternCStruct3 *q3 = p3;
243   ExternCStruct4 *q4 = p4; // expected-error {{ambiguous}}
244 }
245 
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;
252 
253   namespace {
254     extern "C" ReturnStruct PR35697_f();
255     extern "C" ReturnStruct PR35697_v;
q()256     void q() {
257       extern ReturnStruct PR35697_f();
258       extern ReturnStruct PR35697_v;
259     }
260   }
261 }
262 
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}}
266 }
267