xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/linkage2.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++11 %s
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wno-c++11-extensions -Wno-local-type-template-args %s
3f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -Wno-c++11-extensions -Wno-local-type-template-args -fmodules %s
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc namespace test1 {
6f4a2713aSLionel Sambuc   int x; // expected-note {{previous definition is here}}
7f4a2713aSLionel Sambuc   static int y;
f()8f4a2713aSLionel Sambuc   void f() {} // expected-note {{previous definition is here}}
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc   extern "C" {
11f4a2713aSLionel Sambuc     extern int x; // expected-error {{declaration of 'x' has a different language linkage}}
12f4a2713aSLionel Sambuc     extern int y; // OK, has internal linkage, so no language linkage.
13f4a2713aSLionel Sambuc     void f(); // expected-error {{declaration of 'f' has a different language linkage}}
14f4a2713aSLionel Sambuc   }
15f4a2713aSLionel Sambuc }
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc // This is OK. Both test2_f don't have language linkage since they have
18f4a2713aSLionel Sambuc // internal linkage.
19f4a2713aSLionel Sambuc extern "C" {
test2_f()20f4a2713aSLionel Sambuc   static void test2_f() {
21f4a2713aSLionel Sambuc   }
test2_f(int x)22f4a2713aSLionel Sambuc   static void test2_f(int x) {
23f4a2713aSLionel Sambuc   }
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc namespace test3 {
27f4a2713aSLionel Sambuc   extern "C" {
28f4a2713aSLionel Sambuc     namespace {
29f4a2713aSLionel Sambuc       extern int x2;
30f4a2713aSLionel Sambuc       void f2();
31f4a2713aSLionel Sambuc     }
32f4a2713aSLionel Sambuc   }
33f4a2713aSLionel Sambuc   namespace {
34f4a2713aSLionel Sambuc     int x2;
f2()35f4a2713aSLionel Sambuc     void f2() {}
36f4a2713aSLionel Sambuc   }
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc namespace test4 {
dummy()40f4a2713aSLionel Sambuc   void dummy() {
41f4a2713aSLionel Sambuc     void Bar();
42f4a2713aSLionel Sambuc     class A {
43f4a2713aSLionel Sambuc       friend void Bar();
44f4a2713aSLionel Sambuc     };
45f4a2713aSLionel Sambuc   }
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc namespace test5 {
49f4a2713aSLionel Sambuc   static void g();
f()50f4a2713aSLionel Sambuc   void f()
51f4a2713aSLionel Sambuc   {
52f4a2713aSLionel Sambuc     void g();
53f4a2713aSLionel Sambuc   }
54f4a2713aSLionel Sambuc }
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc // pr14898
57f4a2713aSLionel Sambuc namespace test6 {
58f4a2713aSLionel Sambuc   template <class _Rp>
59f4a2713aSLionel Sambuc   class __attribute__ ((__visibility__("default"))) shared_future;
60f4a2713aSLionel Sambuc   template <class _Rp>
61f4a2713aSLionel Sambuc   class future {
62f4a2713aSLionel Sambuc     template <class> friend class shared_future;
63f4a2713aSLionel Sambuc     shared_future<_Rp> share();
64f4a2713aSLionel Sambuc   };
65f4a2713aSLionel Sambuc   template <class _Rp> future<_Rp>
66f4a2713aSLionel Sambuc   get_future();
67f4a2713aSLionel Sambuc   template <class _Rp>
68f4a2713aSLionel Sambuc   struct shared_future<_Rp&> {
69f4a2713aSLionel Sambuc     shared_future(future<_Rp&>&& __f);
70f4a2713aSLionel Sambuc   };
f()71f4a2713aSLionel Sambuc   void f() {
72f4a2713aSLionel Sambuc     typedef int T;
73f4a2713aSLionel Sambuc     get_future<int>();
74f4a2713aSLionel Sambuc     typedef int& U;
75f4a2713aSLionel Sambuc     shared_future<int&> f1 = get_future<int&>();
76f4a2713aSLionel Sambuc   }
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc // This is OK. The variables have internal linkage and therefore no language
80f4a2713aSLionel Sambuc // linkage.
81f4a2713aSLionel Sambuc extern "C" {
82f4a2713aSLionel Sambuc   static int test7_x;
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc extern "C++" {
85f4a2713aSLionel Sambuc   extern int test7_x;
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc extern "C++" {
88f4a2713aSLionel Sambuc   static int test7_y;
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc extern "C" {
91f4a2713aSLionel Sambuc   extern int test7_y;
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc extern "C" { typedef int test7_F(); static test7_F test7_f; }
94f4a2713aSLionel Sambuc extern "C++" { extern test7_F test7_f; }
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc // FIXME: This should be invalid. The function has no language linkage, but
97f4a2713aSLionel Sambuc // the function type has, so this is redeclaring the function with a different
98f4a2713aSLionel Sambuc // type.
99f4a2713aSLionel Sambuc extern "C++" {
100f4a2713aSLionel Sambuc   static void test8_f();
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc extern "C" {
103f4a2713aSLionel Sambuc   extern void test8_f();
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc extern "C" {
106f4a2713aSLionel Sambuc   static void test8_g();
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc extern "C++" {
109f4a2713aSLionel Sambuc   extern void test8_g();
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc extern "C" {
113f4a2713aSLionel Sambuc   void __attribute__((overloadable)) test9_f(int c); // expected-note {{previous declaration is here}}
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc extern "C++" {
116f4a2713aSLionel Sambuc   void __attribute__((overloadable)) test9_f(int c); // expected-error {{declaration of 'test9_f' has a different language linkage}}
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc extern "C" {
120f4a2713aSLionel Sambuc   void __attribute__((overloadable)) test10_f(int);
121f4a2713aSLionel Sambuc   void __attribute__((overloadable)) test10_f(double);
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc extern "C" {
test11_f()125f4a2713aSLionel Sambuc   void test11_f() {
126f4a2713aSLionel Sambuc     void  __attribute__((overloadable)) test11_g(int);
127f4a2713aSLionel Sambuc     void  __attribute__((overloadable)) test11_g(double);
128f4a2713aSLionel Sambuc   }
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc namespace test12 {
132f4a2713aSLionel Sambuc   const int n = 0;
133f4a2713aSLionel Sambuc   extern const int n;
f()134f4a2713aSLionel Sambuc   void f() {
135f4a2713aSLionel Sambuc     extern const int n;
136f4a2713aSLionel Sambuc   }
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc namespace test13 {
140f4a2713aSLionel Sambuc   static void a(void);
141f4a2713aSLionel Sambuc   extern void a();
a(void)142f4a2713aSLionel Sambuc   static void a(void) {}
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc namespace test14 {
146f4a2713aSLionel Sambuc   namespace {
147f4a2713aSLionel Sambuc     void a(void); // expected-note {{previous declaration is here}}
a(void)148f4a2713aSLionel Sambuc     static void a(void) {} // expected-error {{static declaration of 'a' follows non-static declaration}}
149f4a2713aSLionel Sambuc   }
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc namespace test15 {
153f4a2713aSLionel Sambuc   const int a = 5; // expected-note {{previous definition is here}}
154f4a2713aSLionel Sambuc   static const int a; // expected-error {{redefinition of 'a'}}
155f4a2713aSLionel Sambuc }
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc namespace test16 {
158f4a2713aSLionel Sambuc   extern "C" {
159f4a2713aSLionel Sambuc     class Foo {
160f4a2713aSLionel Sambuc       int x;
161f4a2713aSLionel Sambuc       friend int bar(Foo *y);
162f4a2713aSLionel Sambuc     };
bar(Foo * y)163f4a2713aSLionel Sambuc     int bar(Foo *y) {
164f4a2713aSLionel Sambuc       return y->x;
165f4a2713aSLionel Sambuc     }
166f4a2713aSLionel Sambuc   }
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc namespace test17 {
170f4a2713aSLionel Sambuc   namespace {
171f4a2713aSLionel Sambuc     struct I {
172f4a2713aSLionel Sambuc     };
173f4a2713aSLionel Sambuc   }
foo()174f4a2713aSLionel Sambuc   template <typename T1, typename T2> void foo() {}
bar()175f4a2713aSLionel Sambuc   template <typename T, T x> void bar() {} // expected-note {{candidate function}}
g()176f4a2713aSLionel Sambuc   inline void *g() {
177f4a2713aSLionel Sambuc     struct L {
178f4a2713aSLionel Sambuc     };
179f4a2713aSLionel Sambuc     // foo<L, I>'s linkage should be the merge of UniqueExternalLinkage (or
180f4a2713aSLionel Sambuc     // InternalLinkage in c++11) and VisibleNoLinkage. The correct answer is
181f4a2713aSLionel Sambuc     // NoLinkage in both cases. This means that using foo<L, I> as a template
182f4a2713aSLionel Sambuc     // argument should fail.
183f4a2713aSLionel Sambuc     return reinterpret_cast<void*>(bar<typeof(foo<L, I>), foo<L, I> >); // expected-error {{reinterpret_cast cannot resolve overloaded function 'bar' to type 'void *}}
184f4a2713aSLionel Sambuc   }
h()185f4a2713aSLionel Sambuc   void h() {
186f4a2713aSLionel Sambuc     g();
187f4a2713aSLionel Sambuc   }
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc 
190f4a2713aSLionel Sambuc namespace test18 {
191f4a2713aSLionel Sambuc   template <typename T> struct foo {
ftest18::foo192f4a2713aSLionel Sambuc     template <T *P> static void f() {}
gtest18::foo193f4a2713aSLionel Sambuc     static void *g() { return (void *)f<&x>; }
194f4a2713aSLionel Sambuc     static T x;
195f4a2713aSLionel Sambuc   };
196f4a2713aSLionel Sambuc   template <typename T> T foo<T>::x;
f()197f4a2713aSLionel Sambuc   inline void *f() {
198f4a2713aSLionel Sambuc     struct S {
199f4a2713aSLionel Sambuc     };
200f4a2713aSLionel Sambuc     return foo<S>::g();
201f4a2713aSLionel Sambuc   }
h()202f4a2713aSLionel Sambuc   void *h() { return f(); }
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc 
205f4a2713aSLionel Sambuc extern "C" void pr16247_foo(int);
206f4a2713aSLionel Sambuc static void pr16247_foo(double);
pr16247_foo(int)207f4a2713aSLionel Sambuc void pr16247_foo(int) {}
pr16247_foo(double)208f4a2713aSLionel Sambuc void pr16247_foo(double) {}
209f4a2713aSLionel Sambuc 
210f4a2713aSLionel Sambuc namespace PR16247 {
211f4a2713aSLionel Sambuc   extern "C" void pr16247_bar(int);
212f4a2713aSLionel Sambuc   static void pr16247_bar(double);
pr16247_bar(int)213f4a2713aSLionel Sambuc   void pr16247_bar(int) {}
pr16247_bar(double)214f4a2713aSLionel Sambuc   void pr16247_bar(double) {}
215f4a2713aSLionel Sambuc }
216*0a6a1f1dSLionel Sambuc namespace PR18964 {
217*0a6a1f1dSLionel Sambuc   unsigned &*foo; //expected-error{{'foo' declared as a pointer to a reference of type}}
218*0a6a1f1dSLionel Sambuc   extern struct {} *foo; // don't assert
219*0a6a1f1dSLionel Sambuc }
220