linkage2.cpp revision 29c41b51137b2cbc21a8c93f7c61149247c6b01a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// RUN: %clang_cc1 -fsyntax-only -verify %s
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// RUN: %clang_cc1 -fsyntax-only -verify -fmodules %s
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace test1 {
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int x; // expected-note {{previous definition is here}}
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int y;
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void f() {} // expected-note {{previous definition is here}}
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extern "C" {
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extern int x; // expected-error {{declaration of 'x' has a different language linkage}}
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extern int y; // OK, has internal linkage, so no language linkage.
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void f(); // expected-error {{declaration of 'f' has a different language linkage}}
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This is OK. Both test2_f don't have language linkage since they have
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// internal linkage.
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)extern "C" {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static void test2_f() {
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static void test2_f(int x) {
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace test3 {
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  extern "C" {
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    namespace {
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      extern int x2;
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      void f2();
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  namespace {
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int x2;
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void f2() {}
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace test4 {
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void dummy() {
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    void Bar();
41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    class A {
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      friend void Bar();
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    };
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
47a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace test5 {
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static void g();
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void f()
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void g();
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// pr14898
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace test6 {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  template <class _Rp>
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class __attribute__ ((__visibility__("default"))) shared_future;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  template <class _Rp>
60  class future {
61    template <class> friend class shared_future;
62    shared_future<_Rp> share();
63  };
64  template <class _Rp> future<_Rp>
65  get_future();
66  template <class _Rp>
67  struct shared_future<_Rp&> {
68    shared_future(future<_Rp&>&& __f); // expected-warning {{rvalue references are a C++11 extension}}
69  };
70  void f() {
71    typedef int T;
72    get_future<int>();
73    typedef int& U;
74    shared_future<int&> f1 = get_future<int&>();
75  }
76}
77
78// This is OK. The variables have internal linkage and therefore no language
79// linkage.
80extern "C" {
81  static int test7_x;
82}
83extern "C++" {
84  extern int test7_x;
85}
86extern "C++" {
87  static int test7_y;
88}
89extern "C" {
90  extern int test7_y;
91}
92extern "C" { typedef int test7_F(); static test7_F test7_f; }
93extern "C++" { extern test7_F test7_f; }
94
95// FIXME: This should be invalid. The function has no language linkage, but
96// the function type has, so this is redeclaring the function with a different
97// type.
98extern "C++" {
99  static void test8_f();
100}
101extern "C" {
102  extern void test8_f();
103}
104extern "C" {
105  static void test8_g();
106}
107extern "C++" {
108  extern void test8_g();
109}
110
111extern "C" {
112  void __attribute__((overloadable)) test9_f(int c); // expected-note {{previous declaration is here}}
113}
114extern "C++" {
115  void __attribute__((overloadable)) test9_f(int c); // expected-error {{declaration of 'test9_f' has a different language linkage}}
116}
117
118extern "C" {
119  void __attribute__((overloadable)) test10_f(int);
120  void __attribute__((overloadable)) test10_f(double);
121}
122
123extern "C" {
124  void test11_f() {
125    void  __attribute__((overloadable)) test11_g(int);
126    void  __attribute__((overloadable)) test11_g(double);
127  }
128}
129
130namespace test12 {
131  const int n = 0;
132  extern const int n;
133  void f() {
134    extern const int n;
135  }
136}
137
138namespace test13 {
139  static void a(void);
140  extern void a();
141  static void a(void) {}
142}
143
144namespace test14 {
145  namespace {
146    void a(void); // expected-note {{previous declaration is here}}
147    static void a(void) {} // expected-error {{static declaration of 'a' follows non-static declaration}}
148  }
149}
150