444 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wc++98-compat -verify=expected,not-cpp20 %s2// RUN: %clang_cc1 -fsyntax-only -std=c++14 -Wc++98-compat -verify=expected,not-cpp20 %s -DCXX14COMPAT3// RUN: %clang_cc1 -fsyntax-only -std=c++17 -Wc++98-compat -verify=expected,not-cpp20 %s -DCXX14COMPAT -DCXX17COMPAT4// RUN: %clang_cc1 -fsyntax-only -std=c++20 -Wc++98-compat -verify=expected,cpp20 %s -DCXX14COMPAT -DCXX17COMPAT5 6namespace 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 18void test_other_auto_spellings() {19 __auto_type x = 0; // Ok20 decltype(auto) y = 0; // expected-warning {{'decltype' type specifier is incompatible with C++98}}21#ifndef CXX14COMPAT22 // expected-warning@-2 {{'decltype(auto)' type specifier is a C++14 extension}}23#else24 // expected-warning@-4 {{'decltype(auto)' type specifier is incompatible with C++ standards before C++14}}25#endif26}27 28template<typename ...T> // expected-warning {{variadic templates are incompatible with C++98}}29class Variadic1 {};30 31template<template<typename> class ...T> // expected-warning {{variadic templates are incompatible with C++98}}32class Variadic2 {};33 34template<int ...I> // expected-warning {{variadic templates are incompatible with C++98}}35class Variadic3 {};36 37alignas(8) int with_alignas; // expected-warning {{'alignas' is incompatible with C++98}}38int with_attribute [[ ]]; // expected-warning {{[[]] attributes are incompatible with C++ standards before C++11}}39 40void 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 54template<typename T> struct S {};55namespace 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 60void 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 66struct Ctor {67 Ctor(int, char);68 Ctor(double, long);69};70struct InitListCtor {71 InitListCtor(std::initializer_list<bool>);72};73 74int 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, aggregate82 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}93struct DelayedDefaultArgumentParseInitList {94 void f(int i = {1}) { // expected-warning {{generalized initializer lists are incompatible with C++98}} expected-warning {{scalar}}95 }96};97 98int operator""_hello(const char *); // expected-warning {{literal operators are incompatible with C++98}}99 100enum EnumFixed : int { // expected-warning {{enumeration types with a fixed underlying type are incompatible with C++98}}101};102 103enum class EnumScoped { // expected-warning {{scoped enumerations are incompatible with C++98}}104};105 106void Deleted() = delete; // expected-warning {{deleted function definitions are incompatible with C++98}}107struct Defaulted {108 Defaulted() = default; // expected-warning {{defaulted function definitions are incompatible with C++98}}109};110 111int &&RvalueReference = 0; // expected-warning {{rvalue references are incompatible with C++98}}112struct RefQualifier {113 void f() &; // expected-warning {{reference qualifiers on functions are incompatible with C++98}}114};115 116auto f() -> int; // expected-warning {{trailing return types are incompatible with C++98}}117#ifdef CXX14COMPAT118auto 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#endif121 122void 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 135struct InClassInit {136 int n = 0; // expected-warning {{default member initializer for non-static data members is incompatible with C++98}}137};138 139struct OverrideControlBase {140 virtual void f();141 virtual void g();142};143struct 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 148using AliasDecl = int; // expected-warning {{alias declarations are incompatible with C++98}}149template<typename T> using AliasTemplate = T; // expected-warning {{alias declarations are incompatible with C++98}}150 151inline namespace InlineNS { // expected-warning {{inline namespaces are incompatible with C++98}}152}153 154auto auto_deduction = 0; // expected-warning {{'auto' type specifier is incompatible with C++98}}155int *p = new auto(0); // expected-warning {{'auto' type specifier is incompatible with C++98}}156 157const int align_of = alignof(int); // expected-warning {{alignof expressions are incompatible with C++98}}158char16_t c16 = 0; // expected-warning {{'char16_t' type specifier is incompatible with C++98}}159char32_t c32 = 0; // expected-warning {{'char32_t' type specifier is incompatible with C++98}}160constexpr int const_expr = 0; // expected-warning {{'constexpr' specifier is incompatible with C++98}}161decltype(const_expr) decl_type = 0; // expected-warning {{'decltype' type specifier is incompatible with C++98}}162__decltype(const_expr) decl_type2 = 0; // ok163void no_except() noexcept; // expected-warning {{noexcept specifications are incompatible with C++98}}164bool no_except_expr = noexcept(1 + 1); // expected-warning {{noexcept expressions are incompatible with C++98}}165void *null = nullptr; // expected-warning {{'nullptr' is incompatible with C++98}}166static_assert(true, "!"); // expected-warning {{'static_assert' declarations are incompatible with C++98}}167 168struct InhCtorBase {169 InhCtorBase(int);170};171struct InhCtorDerived : InhCtorBase {172 using InhCtorBase::InhCtorBase; // expected-warning {{inheriting constructors are incompatible with C++98}}173};174 175struct 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 180struct DelegCtor {181 DelegCtor(int) : DelegCtor() {} // expected-warning {{delegating constructors are incompatible with C++98}}182 DelegCtor();183};184 185template<int n = 0> void DefaultFuncTemplateArg(); // expected-warning {{default template arguments for a function template are incompatible with C++98}}186 187template<typename T> int TemplateFn(T) { return 0; }188void 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}193struct {} obj_of_unnamed_type; // expected-note {{here}}194int 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++17198// template argument evaluation rules.199#ifndef CXX17COMPAT200namespace 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#endif207 208namespace TemplateSpecOutOfScopeNs {209 template<typename T> struct S {};210}211template<> struct TemplateSpecOutOfScopeNs::S<char> {};212 213struct Typename {214 template<typename T> struct Inner {};215};216typename ::Typename TypenameOutsideTemplate(); // expected-warning {{'typename' outside of a template is incompatible with C++98}}217Typename::template Inner<int> TemplateOutsideTemplate(); // expected-warning {{use of 'template' keyword outside of a template is incompatible with C++98}}218 219struct TrivialButNonPOD {220 int f(int);221private:222 int k;223};224void Ellipsis(int n, ...);225void 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 229struct HasExplicitConversion {230 // FIXME I think we should generate this diagnostic in C++20231 explicit operator bool(); // not-cpp20-warning {{explicit conversion functions are incompatible with C++98}}232};233 234struct Struct {};235enum Enum { enum_val = 0 };236struct 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 243int n = {}; // expected-warning {{scalar initialized from empty initializer list is incompatible with C++98}}244 245class PrivateMember {246 struct ImPrivate {};247};248template<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}251int SFINAEAccessControl(...) { return 0; }252int CheckSFINAEAccessControl = SFINAEAccessControl(PrivateMember()); // expected-note {{while substituting deduced template arguments into function template 'SFINAEAccessControl' [with T = PrivateMember]}}253 254namespace 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 283int EnumNNS = Enum::enum_val; // expected-warning {{enumeration type in nested name specifier is incompatible with C++98}}284template<typename T> void EnumNNSFn() {285 int k = T::enum_val; // expected-warning {{enumeration type in nested name specifier is incompatible with C++98}}286};287template void EnumNNSFn<Enum>(); // expected-note {{in instantiation}}288 289void 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 293DirectJump: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}}298Later: // 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 306namespace UnevaluatedMemberAccess {307 struct S {308 int n;309 int f() { return sizeof(S::n); } // ok310 };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 315namespace 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++17323// template argument evaluation rules.324#ifndef CXX17COMPAT325namespace 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 333namespace 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#endif341 342namespace 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 353namespace 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 369namespace 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 380template<typename T> T var = T(10);381#ifdef CXX14COMPAT382// expected-warning@-2 {{variable templates are incompatible with C++ standards before C++14}}383#else384// expected-warning@-4 {{variable templates are a C++14 extension}}385#endif386 387// No diagnostic for specializations of variable templates; we will have388// diagnosed the primary template.389template<typename T> T* var<T*> = new T();390template<> int var<int> = 10;391template char var<char>;392float fvar = var<float>;393 394class A {395 template<typename T> static T var = T(10);396#ifdef CXX14COMPAT397// expected-warning@-2 {{variable templates are incompatible with C++ standards before C++14}}398#else399// expected-warning@-4 {{variable templates are a C++14 extension}}400#endif401 402 template<typename T> static T* var<T*> = new T();403};404 405struct B { template<typename T> static T v; };406#ifdef CXX14COMPAT407// expected-warning@-2 {{variable templates are incompatible with C++ standards before C++14}}408#else409// expected-warning@-4 {{variable templates are a C++14 extension}}410#endif411 412template<typename T> T B::v = T();413#ifdef CXX14COMPAT414// expected-warning@-2 {{variable templates are incompatible with C++ standards before C++14}}415#else416// expected-warning@-4 {{variable templates are a C++14 extension}}417#endif418 419template<typename T> T* B::v<T*> = new T();420template<> int B::v<int> = 10;421template char B::v<char>;422float fsvar = B::v<float>;423 424#ifdef CXX14COMPAT425int digit_seps = 123'456; // expected-warning {{digit separators are incompatible with C++ standards before C++14}}426#endif427 428#ifdef CXX17COMPAT429template<class T> struct CTAD {};430void 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#endif435 436namespace GH161702 {437struct 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}444