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