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