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