240 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-keyword %s2// RUN: %clang_cc1 -fsyntax-only -verify -Wc++-compat %s3// RUN: %clang_cc1 -fsyntax-only -verify=good %s4// RUN: %clang_cc1 -fsyntax-only -verify=cxx -x c++ -std=c++2c %s5// good-no-diagnostics6 7// 'try', 'this' and 'throw' are not tested as identifiers, but are instead8// tested as other constructs (otherwise there would be redefinition errors in9// C).10int catch; // expected-warning {{identifier 'catch' conflicts with a C++ keyword}} \11 cxx-error {{expected unqualified-id}}12int class; // expected-warning {{identifier 'class' conflicts with a C++ keyword}} \13 cxx-error {{declaration of anonymous class must be a definition}} \14 cxx-warning {{declaration does not declare anything}}15int const_cast; // expected-warning {{identifier 'const_cast' conflicts with a C++ keyword}} \16 cxx-error {{expected unqualified-id}}17int delete; // expected-warning {{identifier 'delete' conflicts with a C++ keyword}} \18 cxx-error {{expected unqualified-id}}19int dynamic_cast; // expected-warning {{identifier 'dynamic_cast' conflicts with a C++ keyword}} \20 cxx-error {{expected unqualified-id}}21int explicit; // expected-warning {{identifier 'explicit' conflicts with a C++ keyword}} \22 cxx-error {{'explicit' can only appear on non-static member functions}} \23 cxx-warning {{declaration does not declare anything}}24int export; // expected-warning {{identifier 'export' conflicts with a C++ keyword}} \25 cxx-error {{expected unqualified-id}}26int friend; // expected-warning {{identifier 'friend' conflicts with a C++ keyword}} \27 cxx-error {{'friend' used outside of class}} \28 cxx-warning {{declaration does not declare anything}}29int mutable; // expected-warning {{identifier 'mutable' conflicts with a C++ keyword}} \30 cxx-warning {{declaration does not declare anything}}31int namespace; // expected-warning {{identifier 'namespace' conflicts with a C++ keyword}} \32 cxx-error {{expected unqualified-id}}33int new; // expected-warning {{identifier 'new' conflicts with a C++ keyword}} \34 cxx-error {{expected unqualified-id}}35int operator; // expected-warning {{identifier 'operator' conflicts with a C++ keyword}} \36 cxx-error {{expected a type}}37int private; // expected-warning {{identifier 'private' conflicts with a C++ keyword}} \38 cxx-error {{expected unqualified-id}}39int protected; // expected-warning {{identifier 'protected' conflicts with a C++ keyword}} \40 cxx-error {{expected unqualified-id}}41int public; // expected-warning {{identifier 'public' conflicts with a C++ keyword}} \42 cxx-error {{expected unqualified-id}}43int reinterpret_cast; // expected-warning {{identifier 'reinterpret_cast' conflicts with a C++ keyword}} \44 cxx-error {{expected unqualified-id}}45int static_cast; // expected-warning {{identifier 'static_cast' conflicts with a C++ keyword}} \46 cxx-error {{expected unqualified-id}}47int template; // expected-warning {{identifier 'template' conflicts with a C++ keyword}} \48 cxx-error {{expected unqualified-id}}49int typename; // expected-warning {{identifier 'typename' conflicts with a C++ keyword}} \50 cxx-error {{expected a qualified name after 'typename'}}51int typeid; // expected-warning {{identifier 'typeid' conflicts with a C++ keyword}} \52 cxx-error {{expected unqualified-id}}53int using; // expected-warning {{identifier 'using' conflicts with a C++ keyword}} \54 cxx-error {{expected unqualified-id}}55int virtual; // expected-warning {{identifier 'virtual' conflicts with a C++ keyword}} \56 cxx-error {{'virtual' can only appear on non-static member functions}} \57 cxx-warning {{declaration does not declare anything}}58int wchar_t; // expected-warning {{identifier 'wchar_t' conflicts with a C++ keyword}} \59 cxx-error {{cannot combine with previous 'int' declaration specifier}} \60 cxx-warning {{declaration does not declare anything}}61int char8_t; // expected-warning {{identifier 'char8_t' conflicts with a C++ keyword}} \62 cxx-error {{cannot combine with previous 'int' declaration specifier}} \63 cxx-warning {{declaration does not declare anything}}64int char16_t; // expected-warning {{identifier 'char16_t' conflicts with a C++ keyword}} \65 cxx-error {{cannot combine with previous 'int' declaration specifier}} \66 cxx-warning {{declaration does not declare anything}}67int char32_t; // expected-warning {{identifier 'char32_t' conflicts with a C++ keyword}} \68 cxx-error {{cannot combine with previous 'int' declaration specifier}} \69 cxx-warning {{declaration does not declare anything}}70int noexcept; // expected-warning {{identifier 'noexcept' conflicts with a C++ keyword}} \71 cxx-error {{expected unqualified-id}}72int co_await; // expected-warning {{identifier 'co_await' conflicts with a C++ keyword}} \73 cxx-error {{expected unqualified-id}}74int co_return; // expected-warning {{identifier 'co_return' conflicts with a C++ keyword}} \75 cxx-error {{expected unqualified-id}}76int co_yield; // expected-warning {{identifier 'co_yield' conflicts with a C++ keyword}} \77 cxx-error {{expected unqualified-id}}78int consteval; // expected-warning {{identifier 'consteval' conflicts with a C++ keyword}} \79 cxx-error {{consteval can only be used in function declarations}}80int constinit; // expected-warning {{identifier 'constinit' conflicts with a C++ keyword}} \81 cxx-error {{constinit can only be used in variable declarations}}82int concept; // expected-warning {{identifier 'concept' conflicts with a C++ keyword}} \83 cxx-error {{expected unqualified-id}}84int requires; // expected-warning {{identifier 'requires' conflicts with a C++ keyword}} \85 cxx-error {{expected unqualified-id}}86 87// Now try the same thing, but as struct members.88struct S {89 int catch; // expected-warning {{identifier 'catch' conflicts with a C++ keyword}} \90 cxx-error {{expected member name or ';' after declaration specifiers}}91 int class; // expected-warning {{identifier 'class' conflicts with a C++ keyword}} \92 cxx-error {{declaration of anonymous class must be a definition}} \93 cxx-warning {{declaration does not declare anything}}94 int const_cast; // expected-warning {{identifier 'const_cast' conflicts with a C++ keyword}} \95 cxx-error {{expected member name or ';' after declaration specifiers}}96 int delete; // expected-warning {{identifier 'delete' conflicts with a C++ keyword}} \97 cxx-error {{expected member name or ';' after declaration specifiers}}98 int dynamic_cast; // expected-warning {{identifier 'dynamic_cast' conflicts with a C++ keyword}} \99 cxx-error {{expected member name or ';' after declaration specifiers}}100 int explicit; // expected-warning {{identifier 'explicit' conflicts with a C++ keyword}} \101 cxx-error {{'explicit' can only appear on non-static member functions}} \102 cxx-warning {{declaration does not declare anything}}103 int export; // expected-warning {{identifier 'export' conflicts with a C++ keyword}} \104 cxx-error {{expected member name or ';' after declaration specifiers}}105 int friend; // expected-warning {{identifier 'friend' conflicts with a C++ keyword}} \106 cxx-error {{'friend' must appear first in a non-function declaration}}107 int mutable; // expected-warning {{identifier 'mutable' conflicts with a C++ keyword}} \108 cxx-warning {{declaration does not declare anything}}109 int namespace; // expected-warning {{identifier 'namespace' conflicts with a C++ keyword}} \110 cxx-error {{expected member name or ';' after declaration specifiers}}111 int new; // expected-warning {{identifier 'new' conflicts with a C++ keyword}} \112 cxx-error {{expected member name or ';' after declaration specifiers}}113 int operator; // expected-warning {{identifier 'operator' conflicts with a C++ keyword}} \114 cxx-error {{expected a type}}115 int private; // expected-warning {{identifier 'private' conflicts with a C++ keyword}} \116 cxx-error {{expected member name or ';' after declaration specifiers}}117 int protected; // expected-warning {{identifier 'protected' conflicts with a C++ keyword}} \118 cxx-error {{expected member name or ';' after declaration specifiers}}119 int public; // expected-warning {{identifier 'public' conflicts with a C++ keyword}} \120 cxx-error {{expected member name or ';' after declaration specifiers}}121 int reinterpret_cast; // expected-warning {{identifier 'reinterpret_cast' conflicts with a C++ keyword}} \122 cxx-error {{expected member name or ';' after declaration specifiers}}123 int static_cast; // expected-warning {{identifier 'static_cast' conflicts with a C++ keyword}} \124 cxx-error {{expected member name or ';' after declaration specifiers}}125 int template; // expected-warning {{identifier 'template' conflicts with a C++ keyword}} \126 cxx-error {{expected member name or ';' after declaration specifiers}}127 int this; // expected-warning {{identifier 'this' conflicts with a C++ keyword}} \128 cxx-error {{expected member name or ';' after declaration specifiers}}129 int throw; // expected-warning {{identifier 'throw' conflicts with a C++ keyword}} \130 cxx-error {{expected member name or ';' after declaration specifiers}}131 int try; // expected-warning {{identifier 'try' conflicts with a C++ keyword}} \132 cxx-error {{expected member name or ';' after declaration specifiers}}133 int typename; // expected-warning {{identifier 'typename' conflicts with a C++ keyword}} \134 cxx-error {{expected a qualified name after 'typename'}}135 int typeid; // expected-warning {{identifier 'typeid' conflicts with a C++ keyword}} \136 cxx-error {{expected member name or ';' after declaration specifiers}}137 int using; // expected-warning {{identifier 'using' conflicts with a C++ keyword}} \138 cxx-error {{expected member name or ';' after declaration specifiers}}139 int virtual; // expected-warning {{identifier 'virtual' conflicts with a C++ keyword}} \140 cxx-error {{'virtual' can only appear on non-static member functions}} \141 cxx-warning {{declaration does not declare anything}}142 int wchar_t; // expected-warning {{identifier 'wchar_t' conflicts with a C++ keyword}} \143 cxx-error {{cannot combine with previous 'int' declaration specifier}} \144 cxx-warning {{declaration does not declare anything}}145 int char8_t; // expected-warning {{identifier 'char8_t' conflicts with a C++ keyword}} \146 cxx-error {{cannot combine with previous 'int' declaration specifier}} \147 cxx-warning {{declaration does not declare anything}}148 int char16_t; // expected-warning {{identifier 'char16_t' conflicts with a C++ keyword}} \149 cxx-error {{cannot combine with previous 'int' declaration specifier}} \150 cxx-warning {{declaration does not declare anything}}151 int char32_t; // expected-warning {{identifier 'char32_t' conflicts with a C++ keyword}} \152 cxx-error {{cannot combine with previous 'int' declaration specifier}} \153 cxx-warning {{declaration does not declare anything}}154 int noexcept; // expected-warning {{identifier 'noexcept' conflicts with a C++ keyword}} \155 cxx-error {{expected member name or ';' after declaration specifiers}}156 int co_await; // expected-warning {{identifier 'co_await' conflicts with a C++ keyword}} \157 cxx-error {{expected member name or ';' after declaration specifiers}}158 int co_return; // expected-warning {{identifier 'co_return' conflicts with a C++ keyword}} \159 cxx-error {{expected member name or ';' after declaration specifiers}}160 int co_yield; // expected-warning {{identifier 'co_yield' conflicts with a C++ keyword}} \161 cxx-error {{expected member name or ';' after declaration specifiers}}162 int consteval; // expected-warning {{identifier 'consteval' conflicts with a C++ keyword}} \163 cxx-error {{consteval can only be used in function declarations}}164 int constinit; // expected-warning {{identifier 'constinit' conflicts with a C++ keyword}} \165 cxx-error {{constinit can only be used in variable declarations}}166 int concept; // expected-warning {{identifier 'concept' conflicts with a C++ keyword}} \167 cxx-error {{concept declarations may only appear in global or namespace scope}}168 int requires; // expected-warning {{identifier 'requires' conflicts with a C++ keyword}} \169 cxx-error {{expected member name or ';' after declaration specifiers}} \170 cxx-error {{trailing requires clause can only be used when declaring a function}} \171 cxx-error {{expected expression}}172};173 174// Smoke test that we catch a keyword used as an enumerator. If we diagnose175// one, we'll diagnose them all.176enum E {177 throw, // expected-warning {{identifier 'throw' conflicts with a C++ keyword}} \178 cxx-error {{expected identifier}}179};180 181// Smoke test that we catch a keyword used as a tag name.182struct try { // expected-warning {{identifier 'try' conflicts with a C++ keyword}} \183 cxx-error {{declaration of anonymous struct must be a definition}} \184 cxx-warning {{declaration does not declare anything}}185 int x;186};187 188// Smoke test that we catch keyword use in a function name.189void this(void); // expected-warning {{identifier 'this' conflicts with a C++ keyword}} \190 cxx-error {{expected unqualified-id}}191 192// Smoke test that we catch keyword use in function parameters too.193void func(int private); // expected-warning {{identifier 'private' conflicts with a C++ keyword}} \194 cxx-error {{invalid parameter name: 'private' is a keyword}}195 196// These are conditionally a keyword in C++, so they're intentionally not being197// diagnosed as a keyword.198int module;199int import;200int override;201int final;202 203// We do not diagnose use of C++ keywords when used as a macro name because204// that does not conflict with C++ (the macros will be replaced before the205// keyword is seen by the parser).206#define this 12207 208// FIXME: These tests are disabled for C++ because it causes a crash.209// See GH114815.210#ifndef __cplusplus211int decltype; // expected-warning {{identifier 'decltype' conflicts with a C++ keyword}}212struct T {213 int decltype; // expected-warning {{identifier 'decltype' conflicts with a C++ keyword}}214};215#endif // __cplusplus216 217// Check alternative operator names.218int and; // expected-warning {{identifier 'and' conflicts with a C++ keyword}} \219 cxx-error {{expected unqualified-id}}220int and_eq; // expected-warning {{identifier 'and_eq' conflicts with a C++ keyword}} \221 cxx-error {{expected unqualified-id}}222int bitand; // expected-warning {{identifier 'bitand' conflicts with a C++ keyword}} \223 cxx-error {{expected unqualified-id}}224int bitor; // expected-warning {{identifier 'bitor' conflicts with a C++ keyword}} \225 cxx-error {{expected unqualified-id}}226int compl; // expected-warning {{identifier 'compl' conflicts with a C++ keyword}} \227 cxx-error {{expected a class name after '~' to name a destructor}}228int not; // expected-warning {{identifier 'not' conflicts with a C++ keyword}} \229 cxx-error {{expected unqualified-id}}230int not_eq; // expected-warning {{identifier 'not_eq' conflicts with a C++ keyword}} \231 cxx-error {{expected unqualified-id}}232int or; // expected-warning {{identifier 'or' conflicts with a C++ keyword}} \233 cxx-error {{expected unqualified-id}}234int or_eq; // expected-warning {{identifier 'or_eq' conflicts with a C++ keyword}} \235 cxx-error {{expected unqualified-id}}236int xor; // expected-warning {{identifier 'xor' conflicts with a C++ keyword}} \237 cxx-error {{expected unqualified-id}}238int xor_eq; // expected-warning {{identifier 'xor_eq' conflicts with a C++ keyword}} \239 cxx-error {{expected unqualified-id}}240