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