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