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