Revert "[llvm] Improve llvm.objectsize computation by computing GEP, alloca and mallo...
[llvm-project.git] / clang / test / SemaCXX / undefined-internal.cpp
blob054e71b92f93d2fe1cb7b7ec7963b2cacba08449
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
20 namespace test1 {
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}}
24 void test() {
25 foo(); // expected-note {{used here}}
26 bar<int>(); // expected-note {{used here}}
30 namespace test2 {
31 namespace {
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}}
36 void test() {
37 foo(); // expected-note {{used here}}
38 var = 0; // expected-note {{used here}}
39 bar<int>(); // expected-note {{used here}}
43 namespace test3 {
44 namespace {
45 void foo();
46 extern int var;
47 template <class T> void bar();
50 void test() {
51 foo();
52 var = 0;
53 bar<int>();
56 namespace {
57 void foo() {}
58 int var = 0;
59 template <class T> void bar() {}
63 namespace test4 {
64 namespace {
65 struct A {
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}}
74 void test(A &a) {
75 a.foo(); // expected-note {{used here}}
76 a.bar();
77 a.baz(); // expected-note {{used here}}
80 struct Test : A {
81 Test() {} // expected-note 2 {{used here}}
85 namespace test5 {
86 namespace {
87 struct A {};
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;
96 void test() {
97 B<A>::var = 0; // expected-note {{used here}}
98 B<A>::foo(); // expected-note {{used here}}
102 namespace test6 {
103 template <class T> struct A {
104 static const int zero = 0;
105 static const int one = 1;
106 static const int two = 2;
108 int value;
110 A() : value(zero) {
111 value = one;
115 namespace { struct Internal; }
117 void test() {
118 A<Internal> a;
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.
126 namespace PR9323 {
127 namespace {
128 struct Uncopyable {
129 Uncopyable() {}
130 private:
131 Uncopyable(const Uncopyable&); // expected-note {{declared private here}}
134 void f(const Uncopyable&) {}
135 void test() {
136 f(Uncopyable());
137 #if __cplusplus <= 199711L // C++03 or earlier modes
138 // expected-warning@-2 {{C++98 requires an accessible copy constructor}}
139 #else
140 // expected-warning@-4 {{copying parameter of type 'Uncopyable' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
141 #endif
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.
160 namespace {
161 struct A {
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}}
165 virtual ~A() {}
169 void a(int,int);
170 A& p(const int&) { static A a; return a; }
172 // Check handling of default arguments
173 void b(int = A::unused);
175 void tests() {
176 // Basic test
177 a(A::unused, A::unused);
179 // Check that nesting an unevaluated or constant-evaluated context does
180 // the right thing.
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}}
189 b();
194 namespace OverloadUse {
195 namespace {
196 void f();
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}}
202 #endif
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*) {}
207 void g(int n) {
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}}
213 #endif
217 namespace test7 {
218 typedef struct { // expected-warning {{add a tag name}}
219 void bar(); // expected-note {{this member}}
220 void foo() {
221 bar();
223 } A; // expected-note {{this typedef}}
226 namespace test8 {
227 typedef struct {
228 void bar(); // expected-warning {{function 'test8::(anonymous struct)::bar' has internal linkage but is not defined}}
229 void foo() {
230 bar(); // expected-note {{used here}}
232 } *A;
235 namespace test9 {
236 namespace {
237 struct X {
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}}
242 void test(X &x) {
243 x.notused();
244 x.X::used(); // expected-note {{used here}}
248 namespace test10 {
249 namespace {
250 struct X {
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}}
254 void test() {
255 notused();
256 (void)&X::notused;
257 (this->*&X::notused)();
258 X::used(); // expected-note {{used here}}
261 struct Y : X {
262 using X::notused;
267 namespace test11 {
268 namespace {
269 struct A {
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;
275 int member;
278 struct B {
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}}
284 int member;
288 void test1(A &a1, A &a2) {
289 a1();
290 !a1;
291 a1 + a2;
292 a1[0];
293 (void)a1->member;
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}}
305 namespace test12 {
306 class T1 {}; class T2 {}; class T3 {}; class T4 {}; class T5 {}; class T6 {};
307 class T7 {};
309 namespace {
310 struct Cls {
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}}
314 void g(int, double);
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}}
323 struct Cls2 {
324 Cls2(T7); // expected-warning {{function 'test12::(anonymous namespace)::Cls2::Cls2' has internal linkage but is not defined}}
328 void test(Cls &c) {
329 c.f(7);
330 c.g(7); // expected-note {{used here}}
331 (void)static_cast<T1>(c);
332 T2 t2 = c;
333 T3 &t3 = 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}}
342 namespace test13 {
343 namespace {
344 struct X {
345 virtual void f() { }
348 struct Y : public X {
349 virtual void f() = 0;
351 virtual void g() {
352 X::f();
358 namespace test14 {
359 extern "C" const int foo;
361 int f() {
362 return foo;