summaryrefslogtreecommitdiff
path: root/test/SemaCXX/linkage2.cpp
blob: 3cc9f61f088213d98ddd3b415c95be2e35453f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++11 %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-c++11-extensions -Wno-local-type-template-args %s -std=gnu++98
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-c++11-extensions -Wno-local-type-template-args -fmodules %s

namespace test1 {
  int x; // expected-note {{previous definition is here}}
  static int y;
  void f() {} // expected-note {{previous definition is here}}

  extern "C" {
    extern int x; // expected-error {{declaration of 'x' has a different language linkage}}
    extern int y; // OK, has internal linkage, so no language linkage.
    void f(); // expected-error {{declaration of 'f' has a different language linkage}}
  }
}

// This is OK. Both test2_f don't have language linkage since they have
// internal linkage.
extern "C" {
  static void test2_f() {
  }
  static void test2_f(int x) {
  }
}

namespace test3 {
  extern "C" {
    namespace {
      extern int x2;
      void f2();
    }
  }
  namespace {
    int x2;
    void f2() {}
  }
}

namespace test4 {
  void dummy() {
    void Bar();
    class A {
      friend void Bar();
    };
  }
}

namespace test5 {
  static void g();
  void f()
  {
    void g();
  }
}

// pr14898
namespace test6 {
  template <class _Rp>
  class __attribute__ ((__visibility__("default"))) shared_future;
  template <class _Rp>
  class future {
    template <class> friend class shared_future;
    shared_future<_Rp> share();
  };
  template <class _Rp> future<_Rp>
  get_future();
  template <class _Rp>
  struct shared_future<_Rp&> {
    shared_future(future<_Rp&>&& __f);
  };
  void f() {
    typedef int T;
    get_future<int>();
    typedef int& U;
    shared_future<int&> f1 = get_future<int&>();
  }
}

// This is OK. The variables have internal linkage and therefore no language
// linkage.
extern "C" {
  static int test7_x;
}
extern "C++" {
  extern int test7_x;
}
extern "C++" {
  static int test7_y;
}
extern "C" {
  extern int test7_y;
}
extern "C" { typedef int test7_F(); static test7_F test7_f; }
extern "C++" { extern test7_F test7_f; }

// FIXME: This should be invalid. The function has no language linkage, but
// the function type has, so this is redeclaring the function with a different
// type.
extern "C++" {
  static void test8_f();
}
extern "C" {
  extern void test8_f();
}
extern "C" {
  static void test8_g();
}
extern "C++" {
  extern void test8_g();
}

extern "C" {
  void __attribute__((overloadable)) test9_f(int c); // expected-note {{previous declaration is here}}
}
extern "C++" {
  void __attribute__((overloadable)) test9_f(int c); // expected-error {{declaration of 'test9_f' has a different language linkage}}
}

extern "C" {
  void __attribute__((overloadable)) test10_f(int);
  void __attribute__((overloadable)) test10_f(double);
}

extern "C" {
  void test11_f() {
    void  __attribute__((overloadable)) test11_g(int);
    void  __attribute__((overloadable)) test11_g(double);
  }
}

namespace test12 {
  const int n = 0;
  extern const int n;
  void f() {
    extern const int n;
  }
}

namespace test13 {
  static void a(void);
  extern void a();
  static void a(void) {}
}

namespace test14 {
  // Anonymous namespace implies internal linkage, so 'static' has no effect.
  namespace {
    void a(void);
    static void a(void) {}
  }
}

namespace test15 {
  const int a = 5; // expected-note {{previous definition is here}}
  static const int a; // expected-error {{redefinition of 'a'}}
}

namespace test16 {
  extern "C" {
    class Foo {
      int x;
      friend int bar(Foo *y);
    };
    int bar(Foo *y) {
      return y->x;
    }
  }
}

namespace test17 {
  namespace {
    struct I {
    };
  }
  template <typename T1, typename T2> void foo() {}
  template <typename T, T x> void bar() {} // expected-note {{candidate function}}
  inline void *g() {
    struct L {
    };
    // foo<L, I>'s linkage should be the merge of UniqueExternalLinkage (or
    // InternalLinkage in c++11) and VisibleNoLinkage. The correct answer is
    // NoLinkage in both cases. This means that using foo<L, I> as a template
    // argument should fail.
    return reinterpret_cast<void*>(bar<typeof(foo<L, I>), foo<L, I> >); // expected-error {{reinterpret_cast cannot resolve overloaded function 'bar' to type 'void *}}
  }
  void h() {
    g();
  }
}

namespace test18 {
  template <typename T> struct foo {
    template <T *P> static void f() {}
    static void *g() { return (void *)f<&x>; }
    static T x;
  };
  template <typename T> T foo<T>::x;
  inline void *f() {
    struct S {
    };
    return foo<S>::g();
  }
  void *h() { return f(); }
}

extern "C" void pr16247_foo(int);
static void pr16247_foo(double);
void pr16247_foo(int) {}
void pr16247_foo(double) {}

namespace PR16247 {
  extern "C" void pr16247_bar(int);
  static void pr16247_bar(double);
  void pr16247_bar(int) {}
  void pr16247_bar(double) {}
}
namespace PR18964 {
  unsigned &*foo; //expected-error{{'foo' declared as a pointer to a reference of type}}
  extern struct {} *foo; // don't assert
}

namespace typedef_name_for_linkage {
  template<typename T> struct Use {};

  struct A { A(); A(const A&); ~A(); };

  typedef struct {
    A a;
  } B;

  struct C {
    typedef struct {
      A a;
    } D;
  };

  typedef struct {
    void f() { static int n; struct Inner {};}
  } E;

  // FIXME: Ideally this would be accepted in all modes. In C++98, we trigger a
  // linkage calculation to drive the "internal linkage type as template
  // argument" warning.
  typedef struct {
    void f() { struct Inner {}; Use<Inner> ui; }
  } F;
#if __cplusplus < 201103L
  // expected-error@-2 {{unsupported: typedef changes linkage of anonymous type, but linkage was already computed}}
  // expected-note@-5 {{use a tag name here}}
#endif
}