367 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy %s2// RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy -std=c++98 %s3// RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy -std=c++11 %s4 5// RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy %s -fexperimental-new-constant-interpreter6// RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy -std=c++98 %s -fexperimental-new-constant-interpreter7// RUN: %clang_cc1 -fsyntax-only -verify -Wbind-to-temporary-copy -std=c++11 %s -fexperimental-new-constant-interpreter8 9 10// Make sure we don't produce invalid IR.11// RUN: %clang_cc1 -emit-llvm-only %s12// RUN: %clang_cc1 -emit-llvm-only -std=c++98 %s13// RUN: %clang_cc1 -emit-llvm-only -std=c++11 %s14 15// RUN: %clang_cc1 -emit-llvm-only %s -fexperimental-new-constant-interpreter16// RUN: %clang_cc1 -emit-llvm-only -std=c++98 %s -fexperimental-new-constant-interpreter17// RUN: %clang_cc1 -emit-llvm-only -std=c++11 %s -fexperimental-new-constant-interpreter18 19 20namespace 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}}23 24 void test() {25 foo(); // expected-note {{used here}}26 bar<int>(); // expected-note {{used here}}27 }28}29 30namespace 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}}35 }36 void test() {37 foo(); // expected-note {{used here}}38 var = 0; // expected-note {{used here}}39 bar<int>(); // expected-note {{used here}}40 }41}42 43namespace test3 {44 namespace {45 void foo();46 extern int var;47 template <class T> void bar();48 }49 50 void test() {51 foo();52 var = 0;53 bar<int>();54 }55 56 namespace {57 void foo() {}58 int var = 0;59 template <class T> void bar() {}60 }61}62 63namespace 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}}71 };72 }73 74 void test(A &a) {75 a.foo(); // expected-note {{used here}}76 a.bar();77 a.baz(); // expected-note {{used here}}78 }79 80 struct Test : A {81 Test() {} // expected-note 2 {{used here}}82 };83}84 85namespace test5 {86 namespace {87 struct A {};88 }89 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}}93 };94 extern template int B<A>::var;95 96 void test() {97 B<A>::var = 0; // expected-note {{used here}}98 B<A>::foo(); // expected-note {{used here}}99 }100}101 102namespace 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;107 108 int value;109 110 A() : value(zero) {111 value = one;112 }113 };114 115 namespace { struct Internal; }116 117 void test() {118 A<Internal> a;119 a.value = A<Internal>::two;120 }121}122 123// We support (as an extension) private, undefined copy constructors when124// a temporary is bound to a reference even in C++98. Similarly, we shouldn't125// warn about this copy constructor being used without a definition.126namespace PR9323 {127 namespace {128 struct Uncopyable {129 Uncopyable() {}130 private:131 Uncopyable(const Uncopyable&); // expected-note {{declared private here}}132 };133 }134 void f(const Uncopyable&) {}135 void test() {136 f(Uncopyable());137#if __cplusplus <= 199711L // C++03 or earlier modes138 // expected-warning@-2 {{C++98 requires an accessible copy constructor}}139#else140 // expected-warning@-4 {{copying parameter of type 'Uncopyable' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}141#endif142 };143}144 145 146namespace std { class type_info; };147namespace cxx11_odr_rules {148 // Note: the way this test is written isn't really ideal, but there really149 // isn't any other way to check that the odr-used logic for constants150 // is working without working implicit capture in lambda-expressions.151 // (The more accurate used-but-not-defined warning is the only other visible152 // effect of accurate odr-used computation.)153 //154 // Note that the warning in question can trigger in cases some people would155 // consider false positives; hopefully that happens rarely in practice.156 //157 // FIXME: Suppressing this test while I figure out how to fix a bug in the158 // odr-use marking code.159 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() {}166 };167 }168 169 void a(int,int);170 A& p(const int&) { static A a; return a; }171 172 // Check handling of default arguments173 void b(int = A::unused);174 175 void tests() {176 // Basic test177 a(A::unused, A::unused);178 179 // Check that nesting an unevaluated or constant-evaluated context does180 // the right thing.181 a(A::unused, sizeof(int[10]));182 183 // Check that the checks work with unevaluated contexts184 (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}}186 187 // Misc other testing188 a(A::unused, 1 ? A::used2 : A::used2); // xpected-note {{used here}}189 b();190 }191}192 193 194namespace 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 < 201103L200 // expected-note@-3 {{here}}201 // expected-note@-3 {{here}}202#endif203 }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 < 201103L211 // expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}212 // expected-note@-4 {{while substituting explicitly-specified template arguments}}213 // expected-warning@-4 {{non-type template argument referring to function 'f' with internal linkage}}214 // expected-note@-5 {{while substituting explicitly-specified template arguments}}215#endif216 }217}218 219namespace test7 {220 typedef struct { // expected-warning {{add a tag name}}221 void bar(); // expected-note {{this member}}222 void foo() {223 bar();224 }225 } A; // expected-note {{this typedef}}226}227 228namespace test8 {229 typedef struct {230 void bar(); // expected-warning {{function 'test8::(anonymous struct)::bar' has internal linkage but is not defined}}231 void foo() {232 bar(); // expected-note {{used here}}233 }234 } *A;235}236 237namespace test9 {238 namespace {239 struct X {240 virtual void notused() = 0;241 virtual void used() = 0; // expected-warning {{function 'test9::(anonymous namespace)::X::used' has internal linkage but is not defined}}242 };243 }244 void test(X &x) {245 x.notused();246 x.X::used(); // expected-note {{used here}}247 }248}249 250namespace test10 {251 namespace {252 struct X {253 virtual void notused() = 0;254 virtual void used() = 0; // expected-warning {{function 'test10::(anonymous namespace)::X::used' has internal linkage but is not defined}}255 256 void test() {257 notused();258 (void)&X::notused;259 (this->*&X::notused)();260 X::used(); // expected-note {{used here}}261 }262 };263 struct Y : X {264 using X::notused;265 };266 }267}268 269namespace test11 {270 namespace {271 struct A {272 virtual bool operator()() const = 0;273 virtual void operator!() const = 0;274 virtual bool operator+(const A&) const = 0;275 virtual int operator[](int) const = 0;276 virtual const A* operator->() const = 0;277 int member;278 };279 280 struct B {281 bool operator()() const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator()' has internal linkage but is not defined}}282 void operator!() const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator!' has internal linkage but is not defined}}283 bool operator+(const B&) const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator+' has internal linkage but is not defined}}284 int operator[](int) const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator[]' has internal linkage but is not defined}}285 const B* operator->() const; // expected-warning {{function 'test11::(anonymous namespace)::B::operator->' has internal linkage but is not defined}}286 int member;287 };288 }289 290 void test1(A &a1, A &a2) {291 a1();292 !a1;293 a1 + a2;294 a1[0];295 (void)a1->member;296 }297 298 void test2(B &b1, B &b2) {299 b1(); // expected-note {{used here}}300 !b1; // expected-note {{used here}}301 b1 + b2; // expected-note {{used here}}302 b1[0]; // expected-note {{used here}}303 (void)b1->member; // expected-note {{used here}}304 }305}306 307namespace test12 {308 class T1 {}; class T2 {}; class T3 {}; class T4 {}; class T5 {}; class T6 {};309 class T7 {};310 311 namespace {312 struct Cls {313 virtual void f(int) = 0;314 virtual void f(int, double) = 0;315 void g(int); // expected-warning {{function 'test12::(anonymous namespace)::Cls::g' has internal linkage but is not defined}}316 void g(int, double);317 virtual operator T1() = 0;318 virtual operator T2() = 0;319 virtual operator T3&() = 0;320 operator T4(); // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T4' has internal linkage but is not defined}}321 operator T5(); // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator T5' has internal linkage but is not defined}}322 operator T6&(); // expected-warning {{function 'test12::(anonymous namespace)::Cls::operator test12::T6 &' has internal linkage but is not defined}}323 };324 325 struct Cls2 {326 Cls2(T7); // expected-warning {{function 'test12::(anonymous namespace)::Cls2::Cls2' has internal linkage but is not defined}}327 };328 }329 330 void test(Cls &c) {331 c.f(7);332 c.g(7); // expected-note {{used here}}333 (void)static_cast<T1>(c);334 T2 t2 = c;335 T3 &t3 = c;336 (void)static_cast<T4>(c); // expected-note {{used here}}337 T5 t5 = c; // expected-note {{used here}}338 T6 &t6 = c; // expected-note {{used here}}339 340 Cls2 obj1((T7())); // expected-note {{used here}}341 }342}343 344namespace test13 {345 namespace {346 struct X {347 virtual void f() { }348 };349 350 struct Y : public X {351 virtual void f() = 0;352 353 virtual void g() {354 X::f();355 }356 };357 }358}359 360namespace test14 {361 extern "C" const int foo;362 363 int f() {364 return foo;365 }366}367