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