Debian Codegraph @

clang/test/SemaCXX/cxx98-compat.cpp

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