extern-c.cpp revision a41c97a5d1912ffd184381d269fd8e5a25ee5e59
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3namespace test1 {
4  extern "C" {
5    void test1_f() {
6      void test1_g(int);
7    }
8  }
9}
10int test1_g(int);
11
12namespace test2 {
13  extern "C" {
14    void test2_f() {
15      extern int test2_x; // expected-note {{declared with C language linkage here}}
16    }
17  }
18}
19float test2_x; // expected-error {{declaration of 'test2_x' in global scope conflicts with declaration with C language linkage}}
20
21namespace test3 {
22  extern "C" {
23    void test3_f() {
24      extern int test3_b; // expected-note {{previous definition 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
32namespace N {
33  extern "C" {
34    void test4_f() {
35      extern int test4_b; // expected-note {{declared with C language linkage here}}
36    }
37  }
38}
39static float test4_b; // expected-error {{declaration of 'test4_b' in global scope conflicts with declaration with C language linkage}}
40
41extern "C" {
42  void test4c_f() {
43    extern int test4_c; // expected-note {{previous}}
44  }
45}
46static float test4_c; // expected-error {{redefinition of 'test4_c' with a different type: 'float' vs 'int'}}
47
48namespace N {
49  extern "C" {
50    void test5_f() {
51      extern int test5_b; // expected-note {{declared with C language linkage here}}
52    }
53  }
54}
55extern "C" {
56  static float test5_b; // expected-error {{declaration of 'test5_b' in global scope conflicts with declaration with C language linkage}}
57}
58
59extern "C" {
60  void test5c_f() {
61    extern int test5_c; // expected-note {{previous}}
62  }
63}
64extern "C" {
65  static float test5_c; // expected-error {{redefinition of 'test5_c' with a different type: 'float' vs 'int'}}
66}
67
68extern "C" {
69  void f() {
70    extern int test6_b;
71  }
72}
73namespace foo {
74  extern "C" {
75    static float test6_b;
76    extern float test6_b;
77  }
78}
79
80namespace 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 {
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
121int lookup_in_global_f; // expected-note {{here}}
122namespace 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}
130int lookup_in_global_g; // expected-error {{conflicts with declaration with C language linkage}}
131
132namespace N1 {
133  extern "C" int different_kind_1; // expected-note {{here}}
134  extern "C" void different_kind_2(); // expected-note {{here}}
135}
136namespace 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.
142extern "C" {
143  struct stat {};
144  void stat(struct stat);
145}
146namespace X {
147  extern "C" {
148    void stat(struct ::stat);
149  }
150}
151int stat(int *p);
152void global_fn_vs_extern_c_var_1();
153namespace X {
154  extern "C" int global_fn_vs_extern_c_var_1;
155  extern "C" int global_fn_vs_extern_c_var_2;
156}
157void global_fn_vs_extern_c_var_2();
158void global_fn_vs_extern_c_fn_1();
159namespace 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}
163void global_fn_vs_extern_c_fn_2();
164extern "C" void name_with_using_decl_1(int);
165namespace using_decl {
166  void name_with_using_decl_1();
167  void name_with_using_decl_2();
168  void name_with_using_decl_3();
169}
170using using_decl::name_with_using_decl_1;
171using using_decl::name_with_using_decl_2;
172extern "C" void name_with_using_decl_2(int);
173extern "C" void name_with_using_decl_3(int);
174using 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.
178int global_var_vs_extern_c_fn_1; // expected-note {{here}}
179namespace 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}
183int global_var_vs_extern_c_fn_2; // expected-error {{conflicts with declaration with C language linkage}}
184int global_var_vs_extern_c_var_1; // expected-note {{here}}
185namespace 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}
189int global_var_vs_extern_c_var_2; // expected-error {{conflicts with declaration with C language linkage}}
190