https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/96097
None >From b77b2d9b10ad90ee67893904732003bf11eec21d Mon Sep 17 00:00:00 2001 From: Corentin Jabot <corentinja...@gmail.com> Date: Wed, 19 Jun 2024 19:47:43 +0200 Subject: [PATCH] [Clang] Move the builtin workaround logic to the lexer --- .../include/clang/Basic/DiagnosticLexKinds.td | 4 + clang/include/clang/Basic/IdentifierTable.h | 19 +++- clang/include/clang/Parse/Parser.h | 4 - clang/lib/Basic/IdentifierTable.cpp | 75 +++++++++++++++ clang/lib/Lex/Preprocessor.cpp | 5 + clang/lib/Parse/ParseDecl.cpp | 19 +--- clang/lib/Parse/ParseDeclCXX.cpp | 71 --------------- clang/lib/Parse/ParseExpr.cpp | 91 ------------------- clang/test/SemaCXX/libstdcxx_is_pod_hack.cpp | 14 +-- 9 files changed, 109 insertions(+), 193 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td index 25fbfe83fa2bc..372d549dc59ba 100644 --- a/clang/include/clang/Basic/DiagnosticLexKinds.td +++ b/clang/include/clang/Basic/DiagnosticLexKinds.td @@ -85,6 +85,10 @@ def warn_c99_keyword : Warning<"'%0' is a keyword in C99">, def warn_c23_keyword : Warning<"'%0' is a keyword in C23">, InGroup<C23Compat>, DefaultIgnore; +def warn_deprecated_builtin_replacement : Warning< + "using the name of the builtin '%0' outside of " + "a builtin invocation is deprecated">, InGroup<KeywordCompat>; + def ext_unterminated_char_or_string : ExtWarn< "missing terminating %select{'|'\"'}0 character">, InGroup<InvalidPPToken>; def ext_empty_character : ExtWarn<"empty character constant">, diff --git a/clang/include/clang/Basic/IdentifierTable.h b/clang/include/clang/Basic/IdentifierTable.h index ae9ebd9f59154..00d1f9b7c9949 100644 --- a/clang/include/clang/Basic/IdentifierTable.h +++ b/clang/include/clang/Basic/IdentifierTable.h @@ -196,6 +196,9 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo { LLVM_PREFERRED_TYPE(bool) unsigned IsFinal : 1; + LLVM_PREFERRED_TYPE(bool) + unsigned IsReusableBuiltinName : 1; + // 22 bits left in a 64-bit word. // Managed by the language front-end. @@ -213,7 +216,8 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo { IsFromAST(false), ChangedAfterLoad(false), FEChangedAfterLoad(false), RevertedTokenID(false), OutOfDate(false), IsModulesImport(false), IsMangledOpenMPVariantName(false), IsDeprecatedMacro(false), - IsRestrictExpansion(false), IsFinal(false) {} + IsRestrictExpansion(false), IsFinal(false), + IsReusableBuiltinName(false) {} public: IdentifierInfo(const IdentifierInfo &) = delete; @@ -332,6 +336,16 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo { RevertedTokenID = false; } + bool isReusableBuiltinName() const { return IsReusableBuiltinName; }; + + void setIsReusableBuiltinName(bool Val) { + IsReusableBuiltinName = Val; + if (Val) + NeedsHandleIdentifier = true; + else + RecomputeNeedsHandleIdentifier(); + }; + /// Return the preprocessor keyword ID for this identifier. /// /// For example, "define" will return tok::pp_define. @@ -569,7 +583,8 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo { void RecomputeNeedsHandleIdentifier() { NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() || isExtensionToken() || isFutureCompatKeyword() || - isOutOfDate() || isModulesImport(); + isReusableBuiltinName() || isOutOfDate() || + isModulesImport(); } }; diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index d054b8cf0d240..ed5d32bf5e076 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -169,10 +169,6 @@ class Parser : public CodeCompletionHandler { mutable IdentifierInfo *Ident_import; mutable IdentifierInfo *Ident_module; - // C++ type trait keywords that can be reverted to identifiers and still be - // used as type traits. - llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind> RevertibleTypeTraits; - std::unique_ptr<PragmaHandler> AlignHandler; std::unique_ptr<PragmaHandler> GCCVisibilityHandler; std::unique_ptr<PragmaHandler> OptionsHandler; diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp index feea84544d62f..f2f2fb822a378 100644 --- a/clang/lib/Basic/IdentifierTable.cpp +++ b/clang/lib/Basic/IdentifierTable.cpp @@ -246,6 +246,79 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts, return CurStatus; } +static bool isReusableBuiltinName(tok::TokenKind TokenCode) { +#define RTT_JOIN(X, Y) X##Y +#define REVERTIBLE_TYPE_TRAIT(Name) \ + case RTT_JOIN(tok::kw_, Name): \ + return true; + + switch (TokenCode) { + default: + return false; + REVERTIBLE_TYPE_TRAIT(__is_abstract); + REVERTIBLE_TYPE_TRAIT(__is_aggregate); + REVERTIBLE_TYPE_TRAIT(__is_arithmetic); + REVERTIBLE_TYPE_TRAIT(__is_array); + REVERTIBLE_TYPE_TRAIT(__is_assignable); + REVERTIBLE_TYPE_TRAIT(__is_base_of); + REVERTIBLE_TYPE_TRAIT(__is_bounded_array); + REVERTIBLE_TYPE_TRAIT(__is_class); + REVERTIBLE_TYPE_TRAIT(__is_complete_type); + REVERTIBLE_TYPE_TRAIT(__is_compound); + REVERTIBLE_TYPE_TRAIT(__is_const); + REVERTIBLE_TYPE_TRAIT(__is_constructible); + REVERTIBLE_TYPE_TRAIT(__is_convertible); + REVERTIBLE_TYPE_TRAIT(__is_convertible_to); + REVERTIBLE_TYPE_TRAIT(__is_destructible); + REVERTIBLE_TYPE_TRAIT(__is_empty); + REVERTIBLE_TYPE_TRAIT(__is_enum); + REVERTIBLE_TYPE_TRAIT(__is_floating_point); + REVERTIBLE_TYPE_TRAIT(__is_final); + REVERTIBLE_TYPE_TRAIT(__is_function); + REVERTIBLE_TYPE_TRAIT(__is_fundamental); + REVERTIBLE_TYPE_TRAIT(__is_integral); + REVERTIBLE_TYPE_TRAIT(__is_interface_class); + REVERTIBLE_TYPE_TRAIT(__is_layout_compatible); + REVERTIBLE_TYPE_TRAIT(__is_literal); + REVERTIBLE_TYPE_TRAIT(__is_lvalue_expr); + REVERTIBLE_TYPE_TRAIT(__is_lvalue_reference); + REVERTIBLE_TYPE_TRAIT(__is_member_function_pointer); + REVERTIBLE_TYPE_TRAIT(__is_member_object_pointer); + REVERTIBLE_TYPE_TRAIT(__is_member_pointer); + REVERTIBLE_TYPE_TRAIT(__is_nothrow_assignable); + REVERTIBLE_TYPE_TRAIT(__is_nothrow_constructible); + REVERTIBLE_TYPE_TRAIT(__is_nothrow_destructible); + REVERTIBLE_TYPE_TRAIT(__is_nothrow_convertible); + REVERTIBLE_TYPE_TRAIT(__is_nullptr); + REVERTIBLE_TYPE_TRAIT(__is_object); + REVERTIBLE_TYPE_TRAIT(__is_pod); + REVERTIBLE_TYPE_TRAIT(__is_pointer); + REVERTIBLE_TYPE_TRAIT(__is_polymorphic); + REVERTIBLE_TYPE_TRAIT(__is_reference); + REVERTIBLE_TYPE_TRAIT(__is_referenceable); + REVERTIBLE_TYPE_TRAIT(__is_rvalue_expr); + REVERTIBLE_TYPE_TRAIT(__is_rvalue_reference); + REVERTIBLE_TYPE_TRAIT(__is_same); + REVERTIBLE_TYPE_TRAIT(__is_scalar); + REVERTIBLE_TYPE_TRAIT(__is_scoped_enum); + REVERTIBLE_TYPE_TRAIT(__is_sealed); + REVERTIBLE_TYPE_TRAIT(__is_signed); + REVERTIBLE_TYPE_TRAIT(__is_standard_layout); + REVERTIBLE_TYPE_TRAIT(__is_trivial); + REVERTIBLE_TYPE_TRAIT(__is_trivially_assignable); + REVERTIBLE_TYPE_TRAIT(__is_trivially_constructible); + REVERTIBLE_TYPE_TRAIT(__is_trivially_copyable); + REVERTIBLE_TYPE_TRAIT(__is_trivially_equality_comparable); + REVERTIBLE_TYPE_TRAIT(__is_unbounded_array); + REVERTIBLE_TYPE_TRAIT(__is_union); + REVERTIBLE_TYPE_TRAIT(__is_unsigned); + REVERTIBLE_TYPE_TRAIT(__is_void); + REVERTIBLE_TYPE_TRAIT(__is_volatile); + REVERTIBLE_TYPE_TRAIT(__reference_binds_to_temporary); + } + return false; +} + /// AddKeyword - This method is used to associate a token ID with specific /// identifiers because they are language keywords. This causes the lexer to /// automatically map matching identifiers to specialized token codes. @@ -261,6 +334,8 @@ static void AddKeyword(StringRef Keyword, Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode); Info.setIsExtensionToken(AddResult == KS_Extension); Info.setIsFutureCompatKeyword(AddResult == KS_Future); + Info.setIsReusableBuiltinName(LangOpts.CPlusPlus && + isReusableBuiltinName(TokenCode)); } /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 44b69a58f3411..7fb1e4db3b87e 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -837,6 +837,11 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) { II.setIsFutureCompatKeyword(false); } + if (II.isReusableBuiltinName() && !isNextPPTokenLParen()) { + Identifier.setKind(tok::identifier); + Diag(Identifier, diag::warn_deprecated_builtin_replacement) << II.getName(); + } + // If this is an extension token, diagnose its use. // We avoid diagnosing tokens that originate from macro definitions. // FIXME: This warning is disabled in cases where it shouldn't be, diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index c528917437332..c1dea20e298f0 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -3906,24 +3906,7 @@ void Parser::ParseDeclarationSpecifiers( continue; } - - case tok::kw___is_signed: - // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang - // typically treats it as a trait. If we see __is_signed as it appears - // in libstdc++, e.g., - // - // static const bool __is_signed; - // - // then treat __is_signed as an identifier rather than as a keyword. - if (DS.getTypeSpecType() == TST_bool && - DS.getTypeQualifiers() == DeclSpec::TQ_const && - DS.getStorageClassSpec() == DeclSpec::SCS_static) - TryKeywordIdentFallback(true); - - // We're done with the declaration-specifiers. - goto DoneWithDeclSpec; - - // typedef-name + // typedef-name case tok::kw___super: case tok::kw_decltype: case tok::identifier: diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index d02548f6441f9..e863f24770b10 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -1718,77 +1718,6 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, // C++11 attributes SourceLocation AttrFixitLoc = Tok.getLocation(); - if (TagType == DeclSpec::TST_struct && Tok.isNot(tok::identifier) && - !Tok.isAnnotation() && Tok.getIdentifierInfo() && - Tok.isOneOf( -#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait, -#include "clang/Basic/TransformTypeTraits.def" - tok::kw___is_abstract, - tok::kw___is_aggregate, - tok::kw___is_arithmetic, - tok::kw___is_array, - tok::kw___is_assignable, - tok::kw___is_base_of, - tok::kw___is_bounded_array, - tok::kw___is_class, - tok::kw___is_complete_type, - tok::kw___is_compound, - tok::kw___is_const, - tok::kw___is_constructible, - tok::kw___is_convertible, - tok::kw___is_convertible_to, - tok::kw___is_destructible, - tok::kw___is_empty, - tok::kw___is_enum, - tok::kw___is_floating_point, - tok::kw___is_final, - tok::kw___is_function, - tok::kw___is_fundamental, - tok::kw___is_integral, - tok::kw___is_interface_class, - tok::kw___is_literal, - tok::kw___is_lvalue_expr, - tok::kw___is_lvalue_reference, - tok::kw___is_member_function_pointer, - tok::kw___is_member_object_pointer, - tok::kw___is_member_pointer, - tok::kw___is_nothrow_assignable, - tok::kw___is_nothrow_constructible, - tok::kw___is_nothrow_convertible, - tok::kw___is_nothrow_destructible, - tok::kw___is_nullptr, - tok::kw___is_object, - tok::kw___is_pod, - tok::kw___is_pointer, - tok::kw___is_polymorphic, - tok::kw___is_reference, - tok::kw___is_referenceable, - tok::kw___is_rvalue_expr, - tok::kw___is_rvalue_reference, - tok::kw___is_same, - tok::kw___is_scalar, - tok::kw___is_scoped_enum, - tok::kw___is_sealed, - tok::kw___is_signed, - tok::kw___is_standard_layout, - tok::kw___is_trivial, - tok::kw___is_trivially_equality_comparable, - tok::kw___is_trivially_assignable, - tok::kw___is_trivially_constructible, - tok::kw___is_trivially_copyable, - tok::kw___is_unbounded_array, - tok::kw___is_union, - tok::kw___is_unsigned, - tok::kw___is_void, - tok::kw___is_volatile - )) - // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the - // name of struct templates, but some are keywords in GCC >= 4.3 - // and Clang. Therefore, when we see the token sequence "struct - // X", make X into a normal identifier rather than a keyword, to - // allow libstdc++ 4.2 and libc++ to work properly. - TryKeywordIdentFallback(true); - struct PreserveAtomicIdentifierInfoRAII { PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled) : AtomicII(nullptr) { diff --git a/clang/lib/Parse/ParseExpr.cpp b/clang/lib/Parse/ParseExpr.cpp index eb7447fa038e4..baea7df1d88d1 100644 --- a/clang/lib/Parse/ParseExpr.cpp +++ b/clang/lib/Parse/ParseExpr.cpp @@ -1097,97 +1097,6 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind, isVectorLiteral, NotPrimaryExpression); } - // If this identifier was reverted from a token ID, and the next token - // is a parenthesis, this is likely to be a use of a type trait. Check - // those tokens. - else if (Next.is(tok::l_paren) && Tok.is(tok::identifier) && - Tok.getIdentifierInfo()->hasRevertedTokenIDToIdentifier()) { - IdentifierInfo *II = Tok.getIdentifierInfo(); - // Build up the mapping of revertible type traits, for future use. - if (RevertibleTypeTraits.empty()) { -#define RTT_JOIN(X,Y) X##Y -#define REVERTIBLE_TYPE_TRAIT(Name) \ - RevertibleTypeTraits[PP.getIdentifierInfo(#Name)] \ - = RTT_JOIN(tok::kw_,Name) - - REVERTIBLE_TYPE_TRAIT(__is_abstract); - REVERTIBLE_TYPE_TRAIT(__is_aggregate); - REVERTIBLE_TYPE_TRAIT(__is_arithmetic); - REVERTIBLE_TYPE_TRAIT(__is_array); - REVERTIBLE_TYPE_TRAIT(__is_assignable); - REVERTIBLE_TYPE_TRAIT(__is_base_of); - REVERTIBLE_TYPE_TRAIT(__is_bounded_array); - REVERTIBLE_TYPE_TRAIT(__is_class); - REVERTIBLE_TYPE_TRAIT(__is_complete_type); - REVERTIBLE_TYPE_TRAIT(__is_compound); - REVERTIBLE_TYPE_TRAIT(__is_const); - REVERTIBLE_TYPE_TRAIT(__is_constructible); - REVERTIBLE_TYPE_TRAIT(__is_convertible); - REVERTIBLE_TYPE_TRAIT(__is_convertible_to); - REVERTIBLE_TYPE_TRAIT(__is_destructible); - REVERTIBLE_TYPE_TRAIT(__is_empty); - REVERTIBLE_TYPE_TRAIT(__is_enum); - REVERTIBLE_TYPE_TRAIT(__is_floating_point); - REVERTIBLE_TYPE_TRAIT(__is_final); - REVERTIBLE_TYPE_TRAIT(__is_function); - REVERTIBLE_TYPE_TRAIT(__is_fundamental); - REVERTIBLE_TYPE_TRAIT(__is_integral); - REVERTIBLE_TYPE_TRAIT(__is_interface_class); - REVERTIBLE_TYPE_TRAIT(__is_layout_compatible); - REVERTIBLE_TYPE_TRAIT(__is_literal); - REVERTIBLE_TYPE_TRAIT(__is_lvalue_expr); - REVERTIBLE_TYPE_TRAIT(__is_lvalue_reference); - REVERTIBLE_TYPE_TRAIT(__is_member_function_pointer); - REVERTIBLE_TYPE_TRAIT(__is_member_object_pointer); - REVERTIBLE_TYPE_TRAIT(__is_member_pointer); - REVERTIBLE_TYPE_TRAIT(__is_nothrow_assignable); - REVERTIBLE_TYPE_TRAIT(__is_nothrow_constructible); - REVERTIBLE_TYPE_TRAIT(__is_nothrow_destructible); - REVERTIBLE_TYPE_TRAIT(__is_nullptr); - REVERTIBLE_TYPE_TRAIT(__is_object); - REVERTIBLE_TYPE_TRAIT(__is_pod); - REVERTIBLE_TYPE_TRAIT(__is_pointer); - REVERTIBLE_TYPE_TRAIT(__is_polymorphic); - REVERTIBLE_TYPE_TRAIT(__is_reference); - REVERTIBLE_TYPE_TRAIT(__is_referenceable); - REVERTIBLE_TYPE_TRAIT(__is_rvalue_expr); - REVERTIBLE_TYPE_TRAIT(__is_rvalue_reference); - REVERTIBLE_TYPE_TRAIT(__is_same); - REVERTIBLE_TYPE_TRAIT(__is_scalar); - REVERTIBLE_TYPE_TRAIT(__is_scoped_enum); - REVERTIBLE_TYPE_TRAIT(__is_sealed); - REVERTIBLE_TYPE_TRAIT(__is_signed); - REVERTIBLE_TYPE_TRAIT(__is_standard_layout); - REVERTIBLE_TYPE_TRAIT(__is_trivial); - REVERTIBLE_TYPE_TRAIT(__is_trivially_assignable); - REVERTIBLE_TYPE_TRAIT(__is_trivially_constructible); - REVERTIBLE_TYPE_TRAIT(__is_trivially_copyable); - REVERTIBLE_TYPE_TRAIT(__is_unbounded_array); - REVERTIBLE_TYPE_TRAIT(__is_union); - REVERTIBLE_TYPE_TRAIT(__is_unsigned); - REVERTIBLE_TYPE_TRAIT(__is_void); - REVERTIBLE_TYPE_TRAIT(__is_volatile); - REVERTIBLE_TYPE_TRAIT(__reference_binds_to_temporary); -#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \ - REVERTIBLE_TYPE_TRAIT(RTT_JOIN(__, Trait)); -#include "clang/Basic/TransformTypeTraits.def" -#undef REVERTIBLE_TYPE_TRAIT -#undef RTT_JOIN - } - - // If we find that this is in fact the name of a type trait, - // update the token kind in place and parse again to treat it as - // the appropriate kind of type trait. - llvm::SmallDenseMap<IdentifierInfo *, tok::TokenKind>::iterator Known - = RevertibleTypeTraits.find(II); - if (Known != RevertibleTypeTraits.end()) { - Tok.setKind(Known->second); - return ParseCastExpression(ParseKind, isAddressOfOperand, - NotCastExpr, isTypeCast, - isVectorLiteral, NotPrimaryExpression); - } - } - else if ((!ColonIsSacred && Next.is(tok::colon)) || Next.isOneOf(tok::coloncolon, tok::less, tok::l_paren, tok::l_brace)) { diff --git a/clang/test/SemaCXX/libstdcxx_is_pod_hack.cpp b/clang/test/SemaCXX/libstdcxx_is_pod_hack.cpp index a853a484707df..f787ee7092836 100644 --- a/clang/test/SemaCXX/libstdcxx_is_pod_hack.cpp +++ b/clang/test/SemaCXX/libstdcxx_is_pod_hack.cpp @@ -7,27 +7,27 @@ // a keyword *unless* it is introduced following the struct keyword. template<typename T> -struct __is_pod { // expected-warning {{keyword '__is_pod' will be made available as an identifier}} - __is_pod() {} +struct __is_pod { // expected-warning {{using the name of the builtin '__is_pod' outside of a builtin invocation is deprecated}} + __is_pod() {} // expected-error {{expected member name or ';' after declaration specifier}} }; -__is_pod<int> ipi; +__is_pod<int> ipi; // expected-warning {{using the name of the builtin '__is_pod' outside of a builtin invocation is deprecated}} // Ditto for __is_same. template<typename T> -struct __is_same { // expected-warning {{keyword '__is_same' will be made available as an identifier}} +struct __is_same { // expected-warning {{using the name of the builtin '__is_same' outside of a builtin invocation is deprecated}} }; -__is_same<int> isi; +__is_same<int> isi; // expected-warning {{using the name of the builtin '__is_same' outside of a builtin invocation is deprecated}} // Another, similar egregious hack for __is_signed, which is a type // trait in Embarcadero's compiler but is used as an identifier in // libstdc++. struct test_is_signed { - static const bool __is_signed = true; // expected-warning {{keyword '__is_signed' will be made available as an identifier}} + static const bool __is_signed = true; // expected-warning {{using the name of the builtin '__is_signed' outside of a builtin invocation is deprecated}} }; -bool check_signed = test_is_signed::__is_signed; +bool check_signed = test_is_signed::__is_signed; // expected-warning {{using the name of the builtin '__is_signed' outside of a builtin invocation is deprecated}} template<bool B> struct must_be_true {}; template<> struct must_be_true<false>; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits