undefined-internal.cpp revision 4ceaf337be78fa89b4a97f351be6d0bda962d7de
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2
3// Make sure we don't produce invalid IR.
4// RUN: %clang_cc1 -emit-llvm-only %s
5
6namespace test1 {
7  static void foo(); // expected-warning {{function 'test1::foo' has internal linkage but is not defined}}
8  template <class T> static void bar(); // expected-warning {{function 'test1::bar<int>' has internal linkage but is not defined}}
9
10  void test() {
11    foo(); // expected-note {{used here}}
12    bar<int>(); // expected-note {{used here}}
13  }
14}
15
16namespace test2 {
17  namespace {
18    void foo(); // expected-warning {{function 'test2::<anonymous namespace>::foo' has internal linkage but is not defined}}
19    extern int var; // expected-warning {{variable 'test2::<anonymous namespace>::var' has internal linkage but is not defined}}
20    template <class T> void bar(); // expected-warning {{function 'test2::<anonymous namespace>::bar<int>' has internal linkage but is not defined}}
21  }
22  void test() {
23    foo(); // expected-note {{used here}}
24    var = 0; // expected-note {{used here}}
25    bar<int>(); // expected-note {{used here}}
26  }
27}
28
29namespace test3 {
30  namespace {
31    void foo();
32    extern int var;
33    template <class T> void bar();
34  }
35
36  void test() {
37    foo();
38    var = 0;
39    bar<int>();
40  }
41
42  namespace {
43    void foo() {}
44    int var = 0;
45    template <class T> void bar() {}
46  }
47}
48
49namespace test4 {
50  namespace {
51    struct A {
52      A(); // expected-warning {{function 'test4::<anonymous namespace>::A::A' has internal linkage but is not defined}}
53      ~A();// expected-warning {{function 'test4::<anonymous namespace>::A::~A' has internal linkage but is not defined}}
54      virtual void foo(); // expected-warning {{function 'test4::<anonymous namespace>::A::foo' has internal linkage but is not defined}}
55      virtual void bar() = 0;
56      virtual void baz(); // expected-warning {{function 'test4::<anonymous namespace>::A::baz' has internal linkage but is not defined}}
57    };
58  }
59
60  void test(A &a) {
61    a.foo(); // expected-note {{used here}}
62    a.bar();
63    a.baz(); // expected-note {{used here}}
64  }
65
66  struct Test : A {
67    Test() {} // expected-note 2 {{used here}}
68  };
69}
70
71// rdar://problem/9014651
72namespace test5 {
73  namespace {
74    struct A {};
75  }
76
77  template <class N> struct B {
78    static int var; // expected-warning {{variable 'test5::B<test5::<anonymous>::A>::var' has internal linkage but is not defined}}
79    static void foo(); // expected-warning {{function 'test5::B<test5::<anonymous>::A>::foo' has internal linkage but is not defined}}
80  };
81
82  void test() {
83    B<A>::var = 0; // expected-note {{used here}}
84    B<A>::foo(); // expected-note {{used here}}
85  }
86}
87
88namespace test6 {
89  template <class T> struct A {
90    static const int zero = 0;
91    static const int one = 1;
92    static const int two = 2;
93
94    int value;
95
96    A() : value(zero) {
97      value = one;
98    }
99  };
100
101  namespace { struct Internal; }
102
103  void test() {
104    A<Internal> a;
105    a.value = A<Internal>::two;
106  }
107}
108
109// We support (as an extension) private, undefined copy constructors when
110// a temporary is bound to a reference even in C++98. Similarly, we shouldn't
111// warn about this copy constructor being used without a definition.
112namespace PR9323 {
113  namespace {
114    struct Uncopyable {
115      Uncopyable() {}
116    private:
117      Uncopyable(const Uncopyable&); // expected-note {{declared private here}}
118    };
119  }
120  void f(const Uncopyable&) {}
121  void test() {
122    f(Uncopyable()); // expected-warning {{C++98 requires an accessible copy constructor}}
123  };
124}
125
126
127namespace std { class type_info; };
128namespace cxx11_odr_rules {
129  // Note: the way this test is written isn't really ideal, but there really
130  // isn't any other way to check that the odr-used logic for constants
131  // is working without working implicit capture in lambda-expressions.
132  // (The more accurate used-but-not-defined warning is the only other visible
133  // effect of accurate odr-used computation.)
134  //
135  // Note that the warning in question can trigger in cases some people would
136  // consider false positives; hopefully that happens rarely in practice.
137  //
138  // FIXME: Suppressing this test while I figure out how to fix a bug in the
139  // odr-use marking code.
140
141  namespace {
142    struct A {
143      static const int unused = 10;
144      static const int used1 = 20; // xpected-warning {{internal linkage}}
145      static const int used2 = 20; // xpected-warning {{internal linkage}}
146      virtual ~A() {}
147    };
148  }
149
150  void a(int,int);
151  A& p(const int&) { static A a; return a; }
152
153  // Check handling of default arguments
154  void b(int = A::unused);
155
156  void tests() {
157    // Basic test
158    a(A::unused, A::unused);
159
160    // Check that nesting an unevaluated or constant-evaluated context does
161    // the right thing.
162    a(A::unused, sizeof(int[10]));
163
164    // Check that the checks work with unevaluated contexts
165    (void)sizeof(p(A::used1));
166    (void)typeid(p(A::used1)); // xpected-note {{used here}}
167
168    // Misc other testing
169    a(A::unused, 1 ? A::used2 : A::used2); // xpected-note {{used here}}
170    b();
171  }
172}
173
174
175namespace OverloadUse {
176  namespace {
177    void f();
178    void f(int); // expected-warning {{function 'OverloadUse::<anonymous namespace>::f' has internal linkage but is not defined}}
179  }
180  template<void x()> void t(int*) { x(); }
181  template<void x(int)> void t(long*) { x(10); } // expected-note {{used here}}
182  void g() { long a; t<f>(&a); }
183}
184
185namespace test7 {
186  typedef struct {
187    void bar();
188    void foo() {
189      bar();
190    }
191  } A;
192}
193
194namespace test8 {
195  typedef struct {
196    void bar(); // expected-warning {{function 'test8::<anonymous struct>::bar' has internal linkage but is not defined}}
197    void foo() {
198      bar(); // expected-note {{used here}}
199    }
200  } *A;
201}
202
203namespace test9 {
204  namespace {
205    struct X {
206      virtual void notused() = 0;
207      virtual void used() = 0; // expected-warning {{function 'test9::<anonymous namespace>::X::used' has internal linkage but is not defined}}
208    };
209  }
210  void test(X &x) {
211    x.notused();
212    x.X::used(); // expected-note {{used here}}
213  }
214}
215