]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/SemaCXX/cxx98-compat.cpp
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
[FreeBSD/FreeBSD.git] / test / SemaCXX / cxx98-compat.cpp
1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wc++98-compat -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -std=c++1y -Wc++98-compat -verify %s -DCXX1YCOMPAT
3
4 namespace std {
5   struct type_info;
6   using size_t = decltype(sizeof(0)); // expected-warning {{decltype}} expected-warning {{alias}}
7   template<typename T> struct initializer_list {
8     initializer_list(T*, size_t);
9     T *p;
10     size_t n;
11     T *begin();
12     T *end();
13   };
14 }
15
16 template<typename ...T>  // expected-warning {{variadic templates are incompatible with C++98}}
17 class Variadic1 {};
18
19 template<template<typename> class ...T>  // expected-warning {{variadic templates are incompatible with C++98}}
20 class Variadic2 {};
21
22 template<int ...I>  // expected-warning {{variadic templates are incompatible with C++98}}
23 class Variadic3 {};
24
25 alignas(8) int with_alignas; // expected-warning {{'alignas' is incompatible with C++98}}
26 int with_attribute [[ ]]; // expected-warning {{attributes are incompatible with C++98}}
27
28 void Literals() {
29   (void)u8"str"; // expected-warning {{unicode literals are incompatible with C++98}}
30   (void)u"str"; // expected-warning {{unicode literals are incompatible with C++98}}
31   (void)U"str"; // expected-warning {{unicode literals are incompatible with C++98}}
32   (void)u'x'; // expected-warning {{unicode literals are incompatible with C++98}}
33   (void)U'x'; // expected-warning {{unicode literals are incompatible with C++98}}
34
35   (void)u8R"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}}
36   (void)uR"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}}
37   (void)UR"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}}
38   (void)R"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}}
39   (void)LR"X(str)X"; // expected-warning {{raw string literals are incompatible with C++98}}
40 }
41
42 template<typename T> struct S {};
43 namespace TemplateParsing {
44   S<::S<void> > s; // expected-warning {{'<::' is treated as digraph '<:' (aka '[') followed by ':' in C++98}}
45   S< ::S<void>> t; // expected-warning {{consecutive right angle brackets are incompatible with C++98 (use '> >')}}
46 }
47
48 void Lambda() {
49   []{}(); // expected-warning {{lambda expressions are incompatible with C++98}}
50 }
51
52 struct Ctor {
53   Ctor(int, char);
54   Ctor(double, long);
55 };
56 struct InitListCtor {
57   InitListCtor(std::initializer_list<bool>);
58 };
59
60 int InitList(int i = {}) { // expected-warning {{generalized initializer lists are incompatible with C++98}} \
61                            // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}}
62   (void)new int {}; // expected-warning {{generalized initializer lists are incompatible with C++98}} \
63                     // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}}
64   (void)int{}; // expected-warning {{generalized initializer lists are incompatible with C++98}} \
65                // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}}
66   int x { 0 }; // expected-warning {{generalized initializer lists are incompatible with C++98}}
67   S<int> s = {}; // ok, aggregate
68   s = {}; // expected-warning {{generalized initializer lists are incompatible with C++98}}
69   std::initializer_list<int> xs = { 1, 2, 3 }; // expected-warning {{initialization of initializer_list object is incompatible with C++98}}
70   auto ys = { 1, 2, 3 }; // expected-warning {{initialization of initializer_list object is incompatible with C++98}} \
71                          // expected-warning {{'auto' type specifier is incompatible with C++98}}
72   Ctor c1 = { 1, 2 }; // expected-warning {{constructor call from initializer list is incompatible with C++98}}
73   Ctor c2 = { 3.0, 4l }; // expected-warning {{constructor call from initializer list is incompatible with C++98}}
74   InitListCtor ilc = { true, false }; // expected-warning {{initialization of initializer_list object is incompatible with C++98}}
75   const int &r = { 0 }; // expected-warning {{reference initialized from initializer list is incompatible with C++98}}
76   struct { int a; const int &r; } rr = { 0, {0} }; // expected-warning {{reference initialized from initializer list is incompatible with C++98}}
77   return { 0 }; // expected-warning {{generalized initializer lists are incompatible with C++98}}
78 }
79 struct DelayedDefaultArgumentParseInitList {
80   void f(int i = {1}) { // expected-warning {{generalized initializer lists are incompatible with C++98}}
81   }
82 };
83
84 int operator"" _hello(const char *); // expected-warning {{literal operators are incompatible with C++98}}
85
86 enum EnumFixed : int { // expected-warning {{enumeration types with a fixed underlying type are incompatible with C++98}}
87 };
88
89 enum class EnumScoped { // expected-warning {{scoped enumerations are incompatible with C++98}}
90 };
91
92 void Deleted() = delete; // expected-warning {{deleted function definitions are incompatible with C++98}}
93 struct Defaulted {
94   Defaulted() = default; // expected-warning {{defaulted function definitions are incompatible with C++98}}
95 };
96
97 int &&RvalueReference = 0; // expected-warning {{rvalue references are incompatible with C++98}}
98 struct RefQualifier {
99   void f() &; // expected-warning {{reference qualifiers on functions are incompatible with C++98}}
100 };
101
102 auto f() -> int; // expected-warning {{trailing return types are incompatible with C++98}}
103
104 void RangeFor() {
105   int xs[] = {1, 2, 3};
106   for (int &a : xs) { // expected-warning {{range-based for loop is incompatible with C++98}}
107   }
108   for (auto &b : {1, 2, 3}) {
109   // expected-warning@-1 {{range-based for loop is incompatible with C++98}}
110   // expected-warning@-2 {{'auto' type specifier is incompatible with C++98}}
111   // expected-warning@-3 {{initialization of initializer_list object is incompatible with C++98}}
112   // expected-warning@-4 {{reference initialized from initializer list is incompatible with C++98}}
113   }
114   struct Agg { int a, b; } const &agg = { 1, 2 }; // expected-warning {{reference initialized from initializer list is incompatible with C++98}}
115 }
116
117 struct InClassInit {
118   int n = 0; // expected-warning {{in-class initialization of non-static data members is incompatible with C++98}}
119 };
120
121 struct OverrideControlBase {
122   virtual void f();
123   virtual void g();
124 };
125 struct OverrideControl final : OverrideControlBase { // expected-warning {{'final' keyword is incompatible with C++98}}
126   virtual void f() override; // expected-warning {{'override' keyword is incompatible with C++98}}
127   virtual void g() final; // expected-warning {{'final' keyword is incompatible with C++98}}
128 };
129
130 using AliasDecl = int; // expected-warning {{alias declarations are incompatible with C++98}}
131 template<typename T> using AliasTemplate = T; // expected-warning {{alias declarations are incompatible with C++98}}
132
133 inline namespace InlineNS { // expected-warning {{inline namespaces are incompatible with C++98}}
134 }
135
136 auto auto_deduction = 0; // expected-warning {{'auto' type specifier is incompatible with C++98}}
137 int *p = new auto(0); // expected-warning {{'auto' type specifier is incompatible with C++98}}
138
139 const int align_of = alignof(int); // expected-warning {{alignof expressions are incompatible with C++98}}
140 char16_t c16 = 0; // expected-warning {{'char16_t' type specifier is incompatible with C++98}}
141 char32_t c32 = 0; // expected-warning {{'char32_t' type specifier is incompatible with C++98}}
142 constexpr int const_expr = 0; // expected-warning {{'constexpr' specifier is incompatible with C++98}}
143 decltype(const_expr) decl_type = 0; // expected-warning {{'decltype' type specifier is incompatible with C++98}}
144 __decltype(const_expr) decl_type2 = 0; // ok
145 void no_except() noexcept; // expected-warning {{noexcept specifications are incompatible with C++98}}
146 bool no_except_expr = noexcept(1 + 1); // expected-warning {{noexcept expressions are incompatible with C++98}}
147 void *null = nullptr; // expected-warning {{'nullptr' is incompatible with C++98}}
148 static_assert(true, "!"); // expected-warning {{static_assert declarations are incompatible with C++98}}
149
150 struct InhCtorBase {
151   InhCtorBase(int);
152 };
153 struct InhCtorDerived : InhCtorBase {
154   using InhCtorBase::InhCtorBase; // expected-warning {{inheriting constructors are incompatible with C++98}}
155 };
156
157 struct FriendMember {
158   static void MemberFn();
159   friend void FriendMember::MemberFn(); // expected-warning {{friend declaration naming a member of the declaring class is incompatible with C++98}}
160 };
161
162 struct DelegCtor {
163   DelegCtor(int) : DelegCtor() {} // expected-warning {{delegating constructors are incompatible with C++98}}
164   DelegCtor();
165 };
166
167 template<int n = 0> void DefaultFuncTemplateArg(); // expected-warning {{default template arguments for a function template are incompatible with C++98}}
168
169 template<typename T> int TemplateFn(T) { return 0; }
170 void LocalTemplateArg() {
171   struct S {};
172   TemplateFn(S()); // expected-warning {{local type 'S' as template argument is incompatible with C++98}}
173 }
174 struct {} obj_of_unnamed_type; // expected-note {{here}}
175 int UnnamedTemplateArg = TemplateFn(obj_of_unnamed_type); // expected-warning {{unnamed type as template argument is incompatible with C++98}}
176
177 namespace RedundantParensInAddressTemplateParam {
178   int n;
179   template<int*p> struct S {};
180   S<(&n)> s; // expected-warning {{redundant parentheses surrounding address non-type template argument are incompatible with C++98}}
181   S<(((&n)))> t; // expected-warning {{redundant parentheses surrounding address non-type template argument are incompatible with C++98}}
182 }
183
184 namespace TemplateSpecOutOfScopeNs {
185   template<typename T> struct S {}; // expected-note {{here}}
186 }
187 template<> struct TemplateSpecOutOfScopeNs::S<char> {}; // expected-warning {{class template specialization of 'S' outside namespace 'TemplateSpecOutOfScopeNs' is incompatible with C++98}}
188
189 struct Typename {
190   template<typename T> struct Inner {};
191 };
192 typename ::Typename TypenameOutsideTemplate(); // expected-warning {{use of 'typename' outside of a template is incompatible with C++98}}
193 Typename::template Inner<int> TemplateOutsideTemplate(); // expected-warning {{use of 'template' keyword outside of a template is incompatible with C++98}}
194
195 struct TrivialButNonPOD {
196   int f(int);
197 private:
198   int k;
199 };
200 void Ellipsis(int n, ...);
201 void TrivialButNonPODThroughEllipsis() {
202   Ellipsis(1, TrivialButNonPOD()); // expected-warning {{passing object of trivial but non-POD type 'TrivialButNonPOD' through variadic function is incompatible with C++98}}
203 }
204
205 struct HasExplicitConversion {
206   explicit operator bool(); // expected-warning {{explicit conversion functions are incompatible with C++98}}
207 };
208
209 struct Struct {};
210 enum Enum { enum_val = 0 };
211 struct BadFriends {
212   friend enum ::Enum; // expected-warning {{befriending enumeration type 'enum ::Enum' is incompatible with C++98}}
213   friend int; // expected-warning {{non-class friend type 'int' is incompatible with C++98}}
214   friend Struct; // expected-warning {{befriending 'Struct' without 'struct' keyword is incompatible with C++98}}
215 };
216
217 int n = {}; // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}}
218
219 class PrivateMember {
220   struct ImPrivate {};
221 };
222 template<typename T> typename T::ImPrivate SFINAEAccessControl(T t) { // expected-warning {{substitution failure due to access control is incompatible with C++98}} expected-note {{while substituting deduced template arguments into function template 'SFINAEAccessControl' [with T = PrivateMember]}}
223   return typename T::ImPrivate();
224 }
225 int SFINAEAccessControl(...) { return 0; }
226 int CheckSFINAEAccessControl = SFINAEAccessControl(PrivateMember());
227
228 template<typename T>
229 struct FriendRedefinition {
230   friend void Friend() {} // expected-warning {{friend function 'Friend' would be implicitly redefined in C++98}} expected-note {{previous}}
231 };
232 FriendRedefinition<int> FriendRedef1;
233 FriendRedefinition<char> FriendRedef2; // expected-note {{requested here}}
234
235 namespace CopyCtorIssues {
236   struct Private {
237     Private();
238   private:
239     Private(const Private&); // expected-note {{declared private here}}
240   };
241   struct NoViable {
242     NoViable();
243     NoViable(NoViable&); // expected-note {{not viable}}
244   };
245   struct Ambiguous {
246     Ambiguous();
247     Ambiguous(const Ambiguous &, int = 0); // expected-note {{candidate}}
248     Ambiguous(const Ambiguous &, double = 0); // expected-note {{candidate}}
249   };
250   struct Deleted {
251     Private p; // expected-note {{implicitly deleted}}
252   };
253
254   const Private &a = Private(); // expected-warning {{copying variable of type 'CopyCtorIssues::Private' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
255   const NoViable &b = NoViable(); // expected-warning {{copying variable of type 'CopyCtorIssues::NoViable' when binding a reference to a temporary would find no viable constructor in C++98}}
256   const Ambiguous &c = Ambiguous(); // expected-warning {{copying variable of type 'CopyCtorIssues::Ambiguous' when binding a reference to a temporary would find ambiguous constructors in C++98}}
257   const Deleted &d = Deleted(); // expected-warning {{copying variable of type 'CopyCtorIssues::Deleted' when binding a reference to a temporary would invoke a deleted constructor in C++98}}
258 }
259
260 namespace UnionOrAnonStructMembers {
261   struct NonTrivCtor {
262     NonTrivCtor(); // expected-note 2{{user-provided default constructor}}
263   };
264   struct NonTrivCopy {
265     NonTrivCopy(const NonTrivCopy&); // expected-note 2{{user-provided copy constructor}}
266   };
267   struct NonTrivDtor {
268     ~NonTrivDtor(); // expected-note 2{{user-provided destructor}}
269   };
270   union BadUnion {
271     NonTrivCtor ntc; // expected-warning {{union member 'ntc' with a non-trivial constructor is incompatible with C++98}}
272     NonTrivCopy ntcp; // expected-warning {{union member 'ntcp' with a non-trivial copy constructor is incompatible with C++98}}
273     NonTrivDtor ntd; // expected-warning {{union member 'ntd' with a non-trivial destructor is incompatible with C++98}}
274   };
275   struct Wrap {
276     struct {
277       NonTrivCtor ntc; // expected-warning {{anonymous struct member 'ntc' with a non-trivial constructor is incompatible with C++98}}
278       NonTrivCopy ntcp; // expected-warning {{anonymous struct member 'ntcp' with a non-trivial copy constructor is incompatible with C++98}}
279       NonTrivDtor ntd; // expected-warning {{anonymous struct member 'ntd' with a non-trivial destructor is incompatible with C++98}}
280     };
281   };
282   union WithStaticDataMember {
283     static constexpr double d = 0.0; // expected-warning {{static data member 'd' in union is incompatible with C++98}} expected-warning {{'constexpr' specifier is incompatible with C++98}}
284     static const int n = 0; // expected-warning {{static data member 'n' in union is incompatible with C++98}}
285     static int k; // expected-warning {{static data member 'k' in union is incompatible with C++98}}
286   };
287 }
288
289 int EnumNNS = Enum::enum_val; // expected-warning {{enumeration type in nested name specifier is incompatible with C++98}}
290 template<typename T> void EnumNNSFn() {
291   int k = T::enum_val; // expected-warning {{enumeration type in nested name specifier is incompatible with C++98}}
292 };
293 template void EnumNNSFn<Enum>(); // expected-note {{in instantiation}}
294
295 void JumpDiagnostics(int n) {
296   goto DirectJump; // expected-warning {{goto would jump into protected scope in C++98}}
297   TrivialButNonPOD tnp1; // expected-note {{jump bypasses initialization of non-POD variable}}
298
299 DirectJump:
300   void *Table[] = {&&DirectJump, &&Later};
301   goto *Table[n]; // expected-warning {{indirect goto might cross protected scopes in C++98}}
302
303   TrivialButNonPOD tnp2; // expected-note {{jump bypasses initialization of non-POD variable}}
304 Later: // expected-note {{possible target of indirect goto}}
305   switch (n) {
306     TrivialButNonPOD tnp3; // expected-note {{jump bypasses initialization of non-POD variable}}
307   default: // expected-warning {{switch case would be in a protected scope in C++98}}
308     return;
309   }
310 }
311
312 namespace UnevaluatedMemberAccess {
313   struct S {
314     int n;
315     int f() { return sizeof(S::n); } // ok
316   };
317   int k = sizeof(S::n); // expected-warning {{use of non-static data member 'n' in an unevaluated context is incompatible with C++98}}
318   const std::type_info &ti = typeid(S::n); // expected-warning {{use of non-static data member 'n' in an unevaluated context is incompatible with C++98}}
319 }
320
321 namespace LiteralUCNs {
322   char c1 = '\u001e'; // expected-warning {{universal character name referring to a control character is incompatible with C++98}}
323   wchar_t c2 = L'\u0041'; // expected-warning {{specifying character 'A' with a universal character name is incompatible with C++98}}
324   const char *s1 = "foo\u0031"; // expected-warning {{specifying character '1' with a universal character name is incompatible with C++98}}
325   const wchar_t *s2 = L"bar\u0085"; // expected-warning {{universal character name referring to a control character is incompatible with C++98}}
326 }
327
328 namespace NonTypeTemplateArgs {
329   template<typename T, T v> struct S {};
330   const int k = 5; // expected-note {{here}}
331   static void f() {} // expected-note {{here}}
332   S<const int&, k> s1; // expected-warning {{non-type template argument referring to object 'k' with internal linkage is incompatible with C++98}}
333   S<void(&)(), f> s2; // expected-warning {{non-type template argument referring to function 'f' with internal linkage is incompatible with C++98}}
334 }
335
336 namespace NullPointerTemplateArg {
337   struct A {};
338   template<int*> struct X {};
339   template<int A::*> struct Y {};
340   X<(int*)0> x; // expected-warning {{use of null pointer as non-type template argument is incompatible with C++98}}
341   Y<(int A::*)0> y; // expected-warning {{use of null pointer as non-type template argument is incompatible with C++98}}
342 }
343
344 namespace PR13480 {
345   struct basic_iterator {
346     basic_iterator(const basic_iterator &it) {} // expected-note {{because type 'PR13480::basic_iterator' has a user-provided copy constructor}}
347     basic_iterator(basic_iterator &it) {}
348   };
349
350   union test {
351     basic_iterator it; // expected-warning {{union member 'it' with a non-trivial copy constructor is incompatible with C++98}}
352   };
353 }
354
355 namespace AssignOpUnion {
356   struct a {
357     void operator=(const a &it) {} // expected-note {{because type 'AssignOpUnion::a' has a user-provided copy assignment operator}}
358     void operator=(a &it) {}
359   };
360
361   struct b {
362     void operator=(const b &it) {} // expected-note {{because type 'AssignOpUnion::b' has a user-provided copy assignment operator}}
363   };
364
365   union test1 {
366     a x; // expected-warning {{union member 'x' with a non-trivial copy assignment operator is incompatible with C++98}}
367     b y; // expected-warning {{union member 'y' with a non-trivial copy assignment operator is incompatible with C++98}}
368   };
369 }
370
371 namespace rdar11736429 {
372   struct X { // expected-note {{because type 'rdar11736429::X' has no default constructor}}
373     X(const X&) = delete; // expected-warning{{deleted function definitions are incompatible with C++98}} \
374     // expected-note {{implicit default constructor suppressed by user-declared constructor}}
375   };
376
377   union S {
378     X x; // expected-warning{{union member 'x' with a non-trivial constructor is incompatible with C++98}}
379   };
380 }
381
382 template<typename T> T var = T(10);
383 #ifdef CXX1YCOMPAT
384 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
385 #else
386 // expected-warning@-4 {{variable templates are a C++1y extension}}
387 #endif
388
389 template<typename T> T* var<T*> = new T();
390 #ifdef CXX1YCOMPAT
391 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
392 #else
393 // expected-warning@-4 {{variable templates are a C++1y extension}}
394 #endif
395
396 template<> int var<int> = 10;
397 #ifdef CXX1YCOMPAT
398 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
399 #else
400 // expected-warning@-4 {{variable templates are a C++1y extension}}
401 #endif
402
403 template int var<int>;
404 float fvar = var<float>;
405
406 class A {  
407   template<typename T> static T var = T(10);
408 #ifdef CXX1YCOMPAT
409 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
410 #else
411 // expected-warning@-4 {{variable templates are a C++1y extension}}
412 #endif
413   
414   template<typename T> static T* var<T*> = new T(); 
415 #ifdef CXX1YCOMPAT
416 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
417 #else
418 // expected-warning@-4 {{variable templates are a C++1y extension}}
419 #endif
420
421 };
422
423 struct B {  template<typename T> static T v; };
424 #ifdef CXX1YCOMPAT
425 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
426 #else
427 // expected-warning@-4 {{variable templates are a C++1y extension}}
428 #endif
429
430 template<typename T> T B::v = T();
431 #ifdef CXX1YCOMPAT
432 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
433 #else
434 // expected-warning@-4 {{variable templates are a C++1y extension}}
435 #endif
436
437 template<typename T> T* B::v<T*> = new T();
438 #ifdef CXX1YCOMPAT
439 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
440 #else
441 // expected-warning@-4 {{variable templates are a C++1y extension}}
442 #endif
443
444 template<> int B::v<int> = 10;
445 #ifdef CXX1YCOMPAT
446 // expected-warning@-2 {{variable templates are incompatible with C++ standards before C++1y}}
447 #else
448 // expected-warning@-4 {{variable templates are a C++1y extension}}
449 #endif
450
451 template int B::v<int>;
452 float fsvar = B::v<float>;
453
454 #ifdef CXX1YCOMPAT
455 int digit_seps = 123'456; // expected-warning {{digit separators are incompatible with C++ standards before C++1y}}
456 #endif