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
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
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
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
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}}
25 foo(); // expected-note {{used here}}
26 bar
<int>(); // expected-note {{used here}}
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}}
37 foo(); // expected-note {{used here}}
38 var
= 0; // expected-note {{used here}}
39 bar
<int>(); // expected-note {{used here}}
47 template <class T
> void bar();
59 template <class T
> void bar() {}
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}}
75 a
.foo(); // expected-note {{used here}}
77 a
.baz(); // expected-note {{used here}}
81 Test() {} // expected-note 2 {{used here}}
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}}
94 extern template int B
<A
>::var
;
97 B
<A
>::var
= 0; // expected-note {{used here}}
98 B
<A
>::foo(); // expected-note {{used here}}
103 template <class T
> struct A
{
104 static const int zero
= 0;
105 static const int one
= 1;
106 static const int two
= 2;
115 namespace { struct Internal
; }
119 a
.value
= A
<Internal
>::two
;
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.
131 Uncopyable(const Uncopyable
&); // expected-note {{declared private here}}
134 void f(const Uncopyable
&) {}
137 #if __cplusplus <= 199711L // C++03 or earlier modes
138 // expected-warning@-2 {{C++98 requires an accessible copy constructor}}
140 // expected-warning@-4 {{copying parameter of type 'Uncopyable' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
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.)
154 // Note that the warning in question can trigger in cases some people would
155 // consider false positives; hopefully that happens rarely in practice.
157 // FIXME: Suppressing this test while I figure out how to fix a bug in the
158 // odr-use marking code.
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}}
170 A
& p(const int&) { static A a
; return a
; }
172 // Check handling of default arguments
173 void b(int = A::unused
);
177 a(A::unused
, A::unused
);
179 // Check that nesting an unevaluated or constant-evaluated context does
181 a(A::unused
, sizeof(int[10]));
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}}
187 // Misc other testing
188 a(A::unused
, 1 ? A::used2
: A::used2
); // xpected-note {{used here}}
194 namespace OverloadUse
{
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}}
204 template<void x()> void t() { x(); }
205 template<void x(int)> void t(int*) { x(10); }
206 template<void x(int, int)> void t(int*, int*) {}
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}}
218 typedef struct { // expected-warning {{add a tag name}}
219 void bar(); // expected-note {{this member}}
223 } A
; // expected-note {{this typedef}}
228 void bar(); // expected-warning {{function 'test8::(anonymous struct)::bar' has internal linkage but is not defined}}
230 bar(); // expected-note {{used here}}
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}}
244 x
.X::used(); // expected-note {{used here}}
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}}
257 (this->*&X::notused
)();
258 X::used(); // expected-note {{used here}}
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;
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}}
288 void test1(A
&a1
, A
&a2
) {
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}}
306 class T1
{}; class T2
{}; class T3
{}; class T4
{}; class T5
{}; class T6
{};
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}}
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}}
324 Cls2(T7
); // expected-warning {{function 'test12::(anonymous namespace)::Cls2::Cls2' has internal linkage but is not defined}}
330 c
.g(7); // expected-note {{used here}}
331 (void)static_cast<T1
>(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}}
338 Cls2
obj1((T7())); // expected-note {{used here}}
348 struct Y
: public X
{
349 virtual void f() = 0;
359 extern "C" const int foo
;