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 // 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
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}}
15 foo(); // expected-note {{used here}}
16 bar
<int>(); // expected-note {{used here}}
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}}
27 foo(); // expected-note {{used here}}
28 var
= 0; // expected-note {{used here}}
29 bar
<int>(); // expected-note {{used here}}
37 template <class T
> void bar();
49 template <class T
> void bar() {}
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}}
65 a
.foo(); // expected-note {{used here}}
67 a
.baz(); // expected-note {{used here}}
71 Test() {} // expected-note 2 {{used here}}
80 template <class N
> struct B
{
81 static int var
; // expected-warning {{variable 'test5::B<test5::(anonymous namespace)::A>::var' has internal linkage but is not defined}}
82 static void foo(); // expected-warning {{function 'test5::B<test5::(anonymous namespace)::A>::foo' has internal linkage but is not defined}}
84 extern template int B
<A
>::var
;
87 B
<A
>::var
= 0; // expected-note {{used here}}
88 B
<A
>::foo(); // expected-note {{used here}}
93 template <class T
> struct A
{
94 static const int zero
= 0;
95 static const int one
= 1;
96 static const int two
= 2;
105 namespace { struct Internal
; }
109 a
.value
= A
<Internal
>::two
;
113 // We support (as an extension) private, undefined copy constructors when
114 // a temporary is bound to a reference even in C++98. Similarly, we shouldn't
115 // warn about this copy constructor being used without a definition.
121 Uncopyable(const Uncopyable
&); // expected-note {{declared private here}}
124 void f(const Uncopyable
&) {}
127 #if __cplusplus <= 199711L // C++03 or earlier modes
128 // expected-warning@-2 {{C++98 requires an accessible copy constructor}}
130 // expected-warning@-4 {{copying parameter of type 'Uncopyable' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
136 namespace std
{ class type_info
; };
137 namespace cxx11_odr_rules
{
138 // Note: the way this test is written isn't really ideal, but there really
139 // isn't any other way to check that the odr-used logic for constants
140 // is working without working implicit capture in lambda-expressions.
141 // (The more accurate used-but-not-defined warning is the only other visible
142 // effect of accurate odr-used computation.)
144 // Note that the warning in question can trigger in cases some people would
145 // consider false positives; hopefully that happens rarely in practice.
147 // FIXME: Suppressing this test while I figure out how to fix a bug in the
148 // odr-use marking code.
152 static const int unused
= 10;
153 static const int used1
= 20; // xpected-warning {{internal linkage}}
154 static const int used2
= 20; // xpected-warning {{internal linkage}}
160 A
& p(const int&) { static A a
; return a
; }
162 // Check handling of default arguments
163 void b(int = A::unused
);
167 a(A::unused
, A::unused
);
169 // Check that nesting an unevaluated or constant-evaluated context does
171 a(A::unused
, sizeof(int[10]));
173 // Check that the checks work with unevaluated contexts
174 (void)sizeof(p(A::used1
));
175 (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 // Misc other testing
178 a(A::unused
, 1 ? A::used2
: A::used2
); // xpected-note {{used here}}
184 namespace OverloadUse
{
187 void f(int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
188 void f(int, int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
189 #if __cplusplus < 201103L
190 // expected-note@-3 {{here}}
191 // expected-note@-3 {{here}}
194 template<void x()> void t() { x(); }
195 template<void x(int)> void t(int*) { x(10); }
196 template<void x(int, int)> void t(int*, int*) {}
198 t
<f
>(&n
); // expected-note {{used here}}
199 t
<f
>(&n
, &n
); // expected-note {{used here}}
200 #if __cplusplus < 201103L
201 // expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}
202 // expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}
208 typedef struct { // expected-warning {{add a tag name}}
209 void bar(); // expected-note {{this member}}
213 } A
; // expected-note {{this typedef}}
218 void bar(); // expected-warning {{function 'test8::(anonymous struct)::bar' has internal linkage but is not defined}}
220 bar(); // expected-note {{used here}}
228 virtual void notused() = 0;
229 virtual void used() = 0; // expected-warning {{function 'test9::(anonymous namespace)::X::used' has internal linkage but is not defined}}
234 x
.X::used(); // expected-note {{used here}}
241 virtual void notused() = 0;
242 virtual void used() = 0; // expected-warning {{function 'test10::(anonymous namespace)::X::used' has internal linkage but is not defined}}
247 (this->*&X::notused
)();
248 X::used(); // expected-note {{used here}}
260 virtual bool operator()() const = 0;
261 virtual void operator!() const = 0;
262 virtual bool operator+(const A
&) const = 0;
263 virtual int operator[](int) const = 0;
264 virtual const A
* operator->() const = 0;
269 bool operator()() const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator()' has internal linkage but is not defined}}
270 void operator!() const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator!' has internal linkage but is not defined}}
271 bool operator+(const B
&) const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator+' has internal linkage but is not defined}}
272 int operator[](int) const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator[]' has internal linkage but is not defined}}
273 const B
* operator->() const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator->' has internal linkage but is not defined}}
278 void test1(A
&a1
, A
&a2
) {
286 void test2(B
&b1
, B
&b2
) {
287 b1(); // expected-note {{used here}}
288 !b1
; // expected-note {{used here}}
289 b1
+ b2
; // expected-note {{used here}}
290 b1
[0]; // expected-note {{used here}}
291 (void)b1
->member
; // expected-note {{used here}}
296 class T1
{}; class T2
{}; class T3
{}; class T4
{}; class T5
{}; class T6
{};
301 virtual void f(int) = 0;
302 virtual void f(int, double) = 0;
303 void g(int); // expected-warning {{function 'test12::(anonymous namespace)::Cls::g' has internal linkage but is not defined}}
305 virtual operator T1() = 0;
306 virtual operator T2() = 0;
307 virtual operator T3
&() = 0;
308 operator T4(); // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T4' has internal linkage but is not defined}}
309 operator T5(); // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T5' has internal linkage but is not defined}}
310 operator T6
&(); // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator test12::T6 &' has internal linkage but is not defined}}
314 Cls2(T7
); // expected-warning {{function 'test12::(anonymous namespace)::Cls2::Cls2' has internal linkage but is not defined}}
320 c
.g(7); // expected-note {{used here}}
321 (void)static_cast<T1
>(c
);
324 (void)static_cast<T4
>(c
); // expected-note {{used here}}
325 T5 t5
= c
; // expected-note {{used here}}
326 T6
&t6
= c
; // expected-note {{used here}}
328 Cls2
obj1((T7())); // expected-note {{used here}}
338 struct Y
: public X
{
339 virtual void f() = 0;
349 extern "C" const int foo
;