xref: /llvm-project/clang/test/SemaCXX/undefined-internal.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy -std=c++11 %s
4 
5 // Make sure we don't produce invalid IR.
6 // RUN: %clang_cc1 -emit-llvm-only %s
7 // RUN: %clang_cc1 -emit-llvm-only -std=c++98 %s
8 // RUN: %clang_cc1 -emit-llvm-only -std=c++11 %s
9 
10 namespace test1 {
11   static void foo(); // expected-warning {{function 'test1::foo' has internal linkage but is not defined}}
12   template <class T> static void bar(); // expected-warning {{function 'test1::bar<int>' has internal linkage but is not defined}}
13 
14   void test() {
15     foo(); // expected-note {{used here}}
16     bar<int>(); // expected-note {{used here}}
17   }
18 }
19 
20 namespace test2 {
21   namespace {
22     void foo(); // expected-warning {{function 'test2::(anonymous namespace)::foo' has internal linkage but is not defined}}
23     extern int var; // expected-warning {{variable 'test2::(anonymous namespace)::var' has internal linkage but is not defined}}
24     template <class T> void bar(); // expected-warning {{function 'test2::(anonymous namespace)::bar<int>' has internal linkage but is not defined}}
25   }
26   void test() {
27     foo(); // expected-note {{used here}}
28     var = 0; // expected-note {{used here}}
29     bar<int>(); // expected-note {{used here}}
30   }
31 }
32 
33 namespace test3 {
34   namespace {
35     void foo();
36     extern int var;
37     template <class T> void bar();
38   }
39 
40   void test() {
41     foo();
42     var = 0;
43     bar<int>();
44   }
45 
46   namespace {
47     void foo() {}
48     int var = 0;
49     template <class T> void bar() {}
50   }
51 }
52 
53 namespace test4 {
54   namespace {
55     struct A {
56       A(); // expected-warning {{function 'test4::(anonymous namespace)::A::A' has internal linkage but is not defined}}
57       ~A();// expected-warning {{function 'test4::(anonymous namespace)::A::~A' has internal linkage but is not defined}}
58       virtual void foo(); // expected-warning {{function 'test4::(anonymous namespace)::A::foo' has internal linkage but is not defined}}
59       virtual void bar() = 0;
60       virtual void baz(); // expected-warning {{function 'test4::(anonymous namespace)::A::baz' has internal linkage but is not defined}}
61     };
62   }
63 
64   void test(A &a) {
65     a.foo(); // expected-note {{used here}}
66     a.bar();
67     a.baz(); // expected-note {{used here}}
68   }
69 
70   struct Test : A {
71     Test() {} // expected-note 2 {{used here}}
72   };
73 }
74 
75 namespace test5 {
76   namespace {
77     struct A {};
78   }
79 
80   template <class N> struct B {
81     static int var; // expected-warning {{variable 'test5::B<test5::(anonymous namespace)::A>::var' has internal linkage but is not defined}}
82     static void foo(); // expected-warning {{function 'test5::B<test5::(anonymous namespace)::A>::foo' has internal linkage but is not defined}}
83   };
84   extern template int B<A>::var;
85 
86   void test() {
87     B<A>::var = 0; // expected-note {{used here}}
88     B<A>::foo(); // expected-note {{used here}}
89   }
90 }
91 
92 namespace test6 {
93   template <class T> struct A {
94     static const int zero = 0;
95     static const int one = 1;
96     static const int two = 2;
97 
98     int value;
99 
100     A() : value(zero) {
101       value = one;
102     }
103   };
104 
105   namespace { struct Internal; }
106 
107   void test() {
108     A<Internal> a;
109     a.value = A<Internal>::two;
110   }
111 }
112 
113 // We support (as an extension) private, undefined copy constructors when
114 // a temporary is bound to a reference even in C++98. Similarly, we shouldn't
115 // warn about this copy constructor being used without a definition.
116 namespace PR9323 {
117   namespace {
118     struct Uncopyable {
119       Uncopyable() {}
120     private:
121       Uncopyable(const Uncopyable&); // expected-note {{declared private here}}
122     };
123   }
124   void f(const Uncopyable&) {}
125   void test() {
126     f(Uncopyable());
127 #if __cplusplus <= 199711L // C++03 or earlier modes
128     // expected-warning@-2 {{C++98 requires an accessible copy constructor}}
129 #else
130     // expected-warning@-4 {{copying parameter of type 'Uncopyable' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
131 #endif
132   };
133 }
134 
135 
136 namespace std { class type_info; };
137 namespace cxx11_odr_rules {
138   // Note: the way this test is written isn't really ideal, but there really
139   // isn't any other way to check that the odr-used logic for constants
140   // is working without working implicit capture in lambda-expressions.
141   // (The more accurate used-but-not-defined warning is the only other visible
142   // effect of accurate odr-used computation.)
143   //
144   // Note that the warning in question can trigger in cases some people would
145   // consider false positives; hopefully that happens rarely in practice.
146   //
147   // FIXME: Suppressing this test while I figure out how to fix a bug in the
148   // odr-use marking code.
149 
150   namespace {
151     struct A {
152       static const int unused = 10;
153       static const int used1 = 20; // xpected-warning {{internal linkage}}
154       static const int used2 = 20; // xpected-warning {{internal linkage}}
155       virtual ~A() {}
156     };
157   }
158 
159   void a(int,int);
160   A& p(const int&) { static A a; return a; }
161 
162   // Check handling of default arguments
163   void b(int = A::unused);
164 
165   void tests() {
166     // Basic test
167     a(A::unused, A::unused);
168 
169     // Check that nesting an unevaluated or constant-evaluated context does
170     // the right thing.
171     a(A::unused, sizeof(int[10]));
172 
173     // Check that the checks work with unevaluated contexts
174     (void)sizeof(p(A::used1));
175     (void)typeid(p(A::used1)); // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}} xpected-note {{used here}}
176 
177     // Misc other testing
178     a(A::unused, 1 ? A::used2 : A::used2); // xpected-note {{used here}}
179     b();
180   }
181 }
182 
183 
184 namespace OverloadUse {
185   namespace {
186     void f();
187     void f(int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
188     void f(int, int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
189 #if __cplusplus < 201103L
190     // expected-note@-3 {{here}}
191     // expected-note@-3 {{here}}
192 #endif
193   }
194   template<void x()> void t() { x(); }
195   template<void x(int)> void t(int*) { x(10); }
196   template<void x(int, int)> void t(int*, int*) {}
197   void g(int n) {
198     t<f>(&n); // expected-note {{used here}}
199     t<f>(&n, &n); // expected-note {{used here}}
200 #if __cplusplus < 201103L
201     // expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}
202     // expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}
203 #endif
204   }
205 }
206 
207 namespace test7 {
208   typedef struct { // expected-warning {{add a tag name}}
209     void bar(); // expected-note {{this member}}
210     void foo() {
211       bar();
212     }
213   } A; // expected-note {{this typedef}}
214 }
215 
216 namespace test8 {
217   typedef struct {
218     void bar(); // expected-warning {{function 'test8::(anonymous struct)::bar' has internal linkage but is not defined}}
219     void foo() {
220       bar(); // expected-note {{used here}}
221     }
222   } *A;
223 }
224 
225 namespace test9 {
226   namespace {
227     struct X {
228       virtual void notused() = 0;
229       virtual void used() = 0; // expected-warning {{function 'test9::(anonymous namespace)::X::used' has internal linkage but is not defined}}
230     };
231   }
232   void test(X &x) {
233     x.notused();
234     x.X::used(); // expected-note {{used here}}
235   }
236 }
237 
238 namespace test10 {
239   namespace {
240     struct X {
241       virtual void notused() = 0;
242       virtual void used() = 0; // expected-warning {{function 'test10::(anonymous namespace)::X::used' has internal linkage but is not defined}}
243 
244       void test() {
245         notused();
246         (void)&X::notused;
247         (this->*&X::notused)();
248         X::used();  // expected-note {{used here}}
249       }
250     };
251     struct Y : X {
252       using X::notused;
253     };
254   }
255 }
256 
257 namespace test11 {
258   namespace {
259     struct A {
260       virtual bool operator()() const = 0;
261       virtual void operator!() const = 0;
262       virtual bool operator+(const A&) const = 0;
263       virtual int operator[](int) const = 0;
264       virtual const A* operator->() const = 0;
265       int member;
266     };
267 
268     struct B {
269       bool operator()() const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator()' has internal linkage but is not defined}}
270       void operator!() const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator!' has internal linkage but is not defined}}
271       bool operator+(const B&) const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator+' has internal linkage but is not defined}}
272       int operator[](int) const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator[]' has internal linkage but is not defined}}
273       const B* operator->() const;  // expected-warning {{function 'test11::(anonymous namespace)::B::operator->' has internal linkage but is not defined}}
274       int member;
275     };
276   }
277 
278   void test1(A &a1, A &a2) {
279     a1();
280     !a1;
281     a1 + a2;
282     a1[0];
283     (void)a1->member;
284   }
285 
286   void test2(B &b1, B &b2) {
287     b1();  // expected-note {{used here}}
288     !b1;  // expected-note {{used here}}
289     b1 + b2;  // expected-note {{used here}}
290     b1[0];  // expected-note {{used here}}
291     (void)b1->member;  // expected-note {{used here}}
292   }
293 }
294 
295 namespace test12 {
296   class T1 {}; class T2 {}; class T3 {}; class T4 {}; class T5 {}; class T6 {};
297   class T7 {};
298 
299   namespace {
300     struct Cls {
301       virtual void f(int) = 0;
302       virtual void f(int, double) = 0;
303       void g(int);  // expected-warning {{function 'test12::(anonymous namespace)::Cls::g' has internal linkage but is not defined}}
304       void g(int, double);
305       virtual operator T1() = 0;
306       virtual operator T2() = 0;
307       virtual operator T3&() = 0;
308       operator T4();  // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T4' has internal linkage but is not defined}}
309       operator T5();  // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T5' has internal linkage but is not defined}}
310       operator T6&();  // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator test12::T6 &' has internal linkage but is not defined}}
311     };
312 
313     struct Cls2 {
314       Cls2(T7);  // expected-warning {{function 'test12::(anonymous namespace)::Cls2::Cls2' has internal linkage but is not defined}}
315     };
316   }
317 
318   void test(Cls &c) {
319     c.f(7);
320     c.g(7);  // expected-note {{used here}}
321     (void)static_cast<T1>(c);
322     T2 t2 = c;
323     T3 &t3 = c;
324     (void)static_cast<T4>(c); // expected-note {{used here}}
325     T5 t5 = c;  // expected-note {{used here}}
326     T6 &t6 = c;  // expected-note {{used here}}
327 
328     Cls2 obj1((T7()));  // expected-note {{used here}}
329   }
330 }
331 
332 namespace test13 {
333   namespace {
334     struct X {
335       virtual void f() { }
336     };
337 
338     struct Y : public X {
339       virtual void f() = 0;
340 
341       virtual void g() {
342         X::f();
343       }
344     };
345   }
346 }
347 
348 namespace test14 {
349   extern "C" const int foo;
350 
351   int f() {
352     return foo;
353   }
354 }
355