xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/extern-c.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc namespace test1 {
4*f4a2713aSLionel Sambuc   extern "C" {
test1_f()5*f4a2713aSLionel Sambuc     void test1_f() {
6*f4a2713aSLionel Sambuc       void test1_g(int);
7*f4a2713aSLionel Sambuc     }
8*f4a2713aSLionel Sambuc   }
9*f4a2713aSLionel Sambuc }
10*f4a2713aSLionel Sambuc int test1_g(int);
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc namespace test2 {
13*f4a2713aSLionel Sambuc   extern "C" {
test2_f()14*f4a2713aSLionel Sambuc     void test2_f() {
15*f4a2713aSLionel Sambuc       extern int test2_x; // expected-note {{declared with C language linkage here}}
16*f4a2713aSLionel Sambuc     }
17*f4a2713aSLionel Sambuc   }
18*f4a2713aSLionel Sambuc }
19*f4a2713aSLionel Sambuc float test2_x; // expected-error {{declaration of 'test2_x' in global scope conflicts with declaration with C language linkage}}
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc namespace test3 {
22*f4a2713aSLionel Sambuc   extern "C" {
test3_f()23*f4a2713aSLionel Sambuc     void test3_f() {
24*f4a2713aSLionel Sambuc       extern int test3_b; // expected-note {{previous definition is here}}
25*f4a2713aSLionel Sambuc     }
26*f4a2713aSLionel Sambuc   }
27*f4a2713aSLionel Sambuc   extern "C" {
28*f4a2713aSLionel Sambuc     float test3_b; // expected-error {{redefinition of 'test3_b' with a different type: 'float' vs 'int'}}
29*f4a2713aSLionel Sambuc   }
30*f4a2713aSLionel Sambuc }
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc namespace N {
33*f4a2713aSLionel Sambuc   extern "C" {
test4_f()34*f4a2713aSLionel Sambuc     void test4_f() {
35*f4a2713aSLionel Sambuc       extern int test4_b; // expected-note {{declared with C language linkage here}}
36*f4a2713aSLionel Sambuc     }
37*f4a2713aSLionel Sambuc   }
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc static float test4_b; // expected-error {{declaration of 'test4_b' in global scope conflicts with declaration with C language linkage}}
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc extern "C" {
test4c_f()42*f4a2713aSLionel Sambuc   void test4c_f() {
43*f4a2713aSLionel Sambuc     extern int test4_c; // expected-note {{previous}}
44*f4a2713aSLionel Sambuc   }
45*f4a2713aSLionel Sambuc }
46*f4a2713aSLionel Sambuc static float test4_c; // expected-error {{redefinition of 'test4_c' with a different type: 'float' vs 'int'}}
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc namespace N {
49*f4a2713aSLionel Sambuc   extern "C" {
test5_f()50*f4a2713aSLionel Sambuc     void test5_f() {
51*f4a2713aSLionel Sambuc       extern int test5_b; // expected-note {{declared with C language linkage here}}
52*f4a2713aSLionel Sambuc     }
53*f4a2713aSLionel Sambuc   }
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc extern "C" {
56*f4a2713aSLionel Sambuc   static float test5_b; // expected-error {{declaration of 'test5_b' in global scope conflicts with declaration with C language linkage}}
57*f4a2713aSLionel Sambuc }
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc extern "C" {
test5c_f()60*f4a2713aSLionel Sambuc   void test5c_f() {
61*f4a2713aSLionel Sambuc     extern int test5_c; // expected-note {{previous}}
62*f4a2713aSLionel Sambuc   }
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc extern "C" {
65*f4a2713aSLionel Sambuc   static float test5_c; // expected-error {{redefinition of 'test5_c' with a different type: 'float' vs 'int'}}
66*f4a2713aSLionel Sambuc }
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc extern "C" {
f()69*f4a2713aSLionel Sambuc   void f() {
70*f4a2713aSLionel Sambuc     extern int test6_b;
71*f4a2713aSLionel Sambuc   }
72*f4a2713aSLionel Sambuc }
73*f4a2713aSLionel Sambuc namespace foo {
74*f4a2713aSLionel Sambuc   extern "C" {
75*f4a2713aSLionel Sambuc     static float test6_b;
76*f4a2713aSLionel Sambuc     extern float test6_b;
77*f4a2713aSLionel Sambuc   }
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc namespace linkage {
81*f4a2713aSLionel Sambuc   namespace redecl {
82*f4a2713aSLionel Sambuc     extern "C" {
83*f4a2713aSLionel Sambuc       static void linkage_redecl();
84*f4a2713aSLionel Sambuc       static void linkage_redecl(int);
85*f4a2713aSLionel Sambuc       void linkage_redecl(); // ok, still not extern "C"
86*f4a2713aSLionel Sambuc       void linkage_redecl(int); // ok, still not extern "C"
87*f4a2713aSLionel Sambuc       void linkage_redecl(float); // expected-note {{previous}}
88*f4a2713aSLionel Sambuc       void linkage_redecl(double); // expected-error {{conflicting types}}
89*f4a2713aSLionel Sambuc     }
90*f4a2713aSLionel Sambuc   }
91*f4a2713aSLionel Sambuc   namespace from_outer {
92*f4a2713aSLionel Sambuc     void linkage_from_outer_1(); // expected-note {{previous}}
93*f4a2713aSLionel Sambuc     void linkage_from_outer_2(); // expected-note {{previous}}
94*f4a2713aSLionel Sambuc     extern "C" {
95*f4a2713aSLionel Sambuc       void linkage_from_outer_1(int);
96*f4a2713aSLionel Sambuc       void linkage_from_outer_1(); // expected-error {{different language linkage}}
97*f4a2713aSLionel Sambuc       void linkage_from_outer_2(); // expected-error {{different language linkage}}
98*f4a2713aSLionel Sambuc     }
99*f4a2713aSLionel Sambuc   }
100*f4a2713aSLionel Sambuc   namespace mixed {
101*f4a2713aSLionel Sambuc     extern "C" {
102*f4a2713aSLionel Sambuc       void linkage_mixed_1();
103*f4a2713aSLionel Sambuc       static void linkage_mixed_1(int);
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc       static void linkage_mixed_2(int);
106*f4a2713aSLionel Sambuc       void linkage_mixed_2();
107*f4a2713aSLionel Sambuc     }
108*f4a2713aSLionel Sambuc   }
109*f4a2713aSLionel Sambuc   namespace across_scopes {
110*f4a2713aSLionel Sambuc     namespace X {
linkage_across_scopes_f()111*f4a2713aSLionel Sambuc       extern "C" void linkage_across_scopes_f() {
112*f4a2713aSLionel Sambuc         void linkage_across_scopes_g(); // expected-note {{previous}}
113*f4a2713aSLionel Sambuc       }
114*f4a2713aSLionel Sambuc     }
115*f4a2713aSLionel Sambuc     namespace Y {
116*f4a2713aSLionel Sambuc       extern "C" void linkage_across_scopes_g(int); // expected-error {{conflicting}}
117*f4a2713aSLionel Sambuc     }
118*f4a2713aSLionel Sambuc   }
119*f4a2713aSLionel Sambuc }
120*f4a2713aSLionel Sambuc 
121*f4a2713aSLionel Sambuc int lookup_in_global_f; // expected-note {{here}}
122*f4a2713aSLionel Sambuc namespace lookup_in_global {
123*f4a2713aSLionel Sambuc   void lookup_in_global_f();
124*f4a2713aSLionel Sambuc   void lookup_in_global_g();
125*f4a2713aSLionel Sambuc   extern "C" {
126*f4a2713aSLionel Sambuc     void lookup_in_global_f(int); // expected-error {{conflicts with declaration in global scope}}
127*f4a2713aSLionel Sambuc     void lookup_in_global_g(int); // expected-note {{here}}
128*f4a2713aSLionel Sambuc   }
129*f4a2713aSLionel Sambuc }
130*f4a2713aSLionel Sambuc int lookup_in_global_g; // expected-error {{conflicts with declaration with C language linkage}}
131*f4a2713aSLionel Sambuc 
132*f4a2713aSLionel Sambuc namespace N1 {
133*f4a2713aSLionel Sambuc   extern "C" int different_kind_1; // expected-note {{here}}
134*f4a2713aSLionel Sambuc   extern "C" void different_kind_2(); // expected-note {{here}}
135*f4a2713aSLionel Sambuc }
136*f4a2713aSLionel Sambuc namespace N2 {
137*f4a2713aSLionel Sambuc   extern "C" void different_kind_1(); // expected-error {{different kind of symbol}}
138*f4a2713aSLionel Sambuc   extern "C" int different_kind_2; // expected-error {{different kind of symbol}}
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc // We allow all these even though the standard says they are ill-formed.
142*f4a2713aSLionel Sambuc extern "C" {
143*f4a2713aSLionel Sambuc   struct stat {};   // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
144*f4a2713aSLionel Sambuc   void stat(struct stat);
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc namespace X {
147*f4a2713aSLionel Sambuc   extern "C" {
148*f4a2713aSLionel Sambuc     void stat(struct ::stat);
149*f4a2713aSLionel Sambuc   }
150*f4a2713aSLionel Sambuc }
151*f4a2713aSLionel Sambuc int stat(int *p);
152*f4a2713aSLionel Sambuc void global_fn_vs_extern_c_var_1();
153*f4a2713aSLionel Sambuc namespace X {
154*f4a2713aSLionel Sambuc   extern "C" int global_fn_vs_extern_c_var_1;
155*f4a2713aSLionel Sambuc   extern "C" int global_fn_vs_extern_c_var_2;
156*f4a2713aSLionel Sambuc }
157*f4a2713aSLionel Sambuc void global_fn_vs_extern_c_var_2();
158*f4a2713aSLionel Sambuc void global_fn_vs_extern_c_fn_1();
159*f4a2713aSLionel Sambuc namespace X {
160*f4a2713aSLionel Sambuc   extern "C" int global_fn_vs_extern_c_fn_1(int);
161*f4a2713aSLionel Sambuc   extern "C" int global_fn_vs_extern_c_fn_2(int);
162*f4a2713aSLionel Sambuc }
163*f4a2713aSLionel Sambuc void global_fn_vs_extern_c_fn_2();
164*f4a2713aSLionel Sambuc extern "C" void name_with_using_decl_1(int);
165*f4a2713aSLionel Sambuc namespace using_decl {
166*f4a2713aSLionel Sambuc   void name_with_using_decl_1();
167*f4a2713aSLionel Sambuc   void name_with_using_decl_2();
168*f4a2713aSLionel Sambuc   void name_with_using_decl_3();
169*f4a2713aSLionel Sambuc }
170*f4a2713aSLionel Sambuc using using_decl::name_with_using_decl_1;
171*f4a2713aSLionel Sambuc using using_decl::name_with_using_decl_2;
172*f4a2713aSLionel Sambuc extern "C" void name_with_using_decl_2(int);
173*f4a2713aSLionel Sambuc extern "C" void name_with_using_decl_3(int);
174*f4a2713aSLionel Sambuc using using_decl::name_with_using_decl_3;
175*f4a2713aSLionel Sambuc 
176*f4a2713aSLionel Sambuc // We do not allow a global variable and an extern "C" function to have the same
177*f4a2713aSLionel Sambuc // name, because such entities may have the same mangled name.
178*f4a2713aSLionel Sambuc int global_var_vs_extern_c_fn_1; // expected-note {{here}}
179*f4a2713aSLionel Sambuc namespace X {
180*f4a2713aSLionel Sambuc   extern "C" void global_var_vs_extern_c_fn_1(); // expected-error {{conflicts with declaration in global scope}}
181*f4a2713aSLionel Sambuc   extern "C" void global_var_vs_extern_c_fn_2(); // expected-note {{here}}
182*f4a2713aSLionel Sambuc }
183*f4a2713aSLionel Sambuc int global_var_vs_extern_c_fn_2; // expected-error {{conflicts with declaration with C language linkage}}
184*f4a2713aSLionel Sambuc int global_var_vs_extern_c_var_1; // expected-note {{here}}
185*f4a2713aSLionel Sambuc namespace X {
186*f4a2713aSLionel Sambuc   extern "C" double global_var_vs_extern_c_var_1; // expected-error {{conflicts with declaration in global scope}}
187*f4a2713aSLionel Sambuc   extern "C" double global_var_vs_extern_c_var_2; // expected-note {{here}}
188*f4a2713aSLionel Sambuc }
189*f4a2713aSLionel Sambuc int global_var_vs_extern_c_var_2; // expected-error {{conflicts with declaration with C language linkage}}
190*f4a2713aSLionel Sambuc 
191*f4a2713aSLionel Sambuc template <class T> struct pr5065_n1 {};
192*f4a2713aSLionel Sambuc extern "C" {
193*f4a2713aSLionel Sambuc   union pr5065_1 {}; // expected-warning{{empty union has size 0 in C, size 1 in C++}}
194*f4a2713aSLionel Sambuc   struct pr5065_2 { int: 0; }; // expected-warning{{struct has size 0 in C, size 1 in C++}}
195*f4a2713aSLionel Sambuc   struct pr5065_3 {}; // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
196*f4a2713aSLionel Sambuc   struct pr5065_4 { // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
197*f4a2713aSLionel Sambuc     struct Inner {}; // expected-warning{{empty struct has size 0 in C, size 1 in C++}}
198*f4a2713aSLionel Sambuc   };
199*f4a2713aSLionel Sambuc   // These should not warn
200*f4a2713aSLionel Sambuc   class pr5065_n3 {};
201*f4a2713aSLionel Sambuc   pr5065_n1<int> pr5065_v;
mpr5065_n4202*f4a2713aSLionel Sambuc   struct pr5065_n4 { void m() {} };
203*f4a2713aSLionel Sambuc   struct pr5065_n5 : public pr5065_3 {};
204*f4a2713aSLionel Sambuc   struct pr5065_n6 : public virtual pr5065_3 {};
205*f4a2713aSLionel Sambuc }
206*f4a2713aSLionel Sambuc struct pr5065_n7 {};
207