From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id C8806383F977; Thu, 30 Jun 2022 18:50:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C8806383F977 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] backend: handle deprecated attribute X-Act-Checkin: gcc X-Git-Author: liushuyu X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 65a06a817585faf7fb44fbc1c71173a00f9a407f X-Git-Newrev: 04cc46cfe8cad9736ccf2d5c884af50e57b67ce6 Message-Id: <20220630185032.C8806383F977@sourceware.org> Date: Thu, 30 Jun 2022 18:50:32 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jun 2022 18:50:32 -0000 https://gcc.gnu.org/g:04cc46cfe8cad9736ccf2d5c884af50e57b67ce6 commit 04cc46cfe8cad9736ccf2d5c884af50e57b67ce6 Author: liushuyu Date: Thu Apr 21 18:21:10 2022 -0600 backend: handle deprecated attribute Signed-off-by: Zixing Liu Diff: --- gcc/rust/ast/rust-ast-full-test.cc | 9 ++++- gcc/rust/ast/rust-ast.h | 2 ++ gcc/rust/ast/rust-macro.h | 8 +++++ gcc/rust/backend/rust-compile-base.cc | 68 ++++++++++++++++++++++++++++++++++- gcc/rust/backend/rust-compile-base.h | 3 ++ gcc/rust/util/rust-attributes.cc | 1 + 6 files changed, 89 insertions(+), 2 deletions(-) diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 0d4d36b36cc..81791135bc4 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -20,10 +20,12 @@ along with GCC; see the file COPYING3. If not see // FIXME: This does not work on Windows #include #include +#include #include "rust-ast-full.h" #include "rust-diagnostics.h" #include "rust-ast-visitor.h" +#include "rust-macro.h" #include "rust-session-manager.h" #include "rust-lex.h" #include "rust-parse.h" @@ -3862,7 +3864,12 @@ MetaItemInner::~MetaItemInner () = default; std::unique_ptr MetaItemInner::to_meta_name_value_str () const { - // TODO parse foo = bar + if (is_key_value_pair ()) + { + auto converted_item = static_cast (this); + return converted_item->to_meta_name_value_str (); + } + // TODO actually parse foo = bar return nullptr; } diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 76324dc7304..51fe3c49c59 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -645,6 +645,8 @@ public: virtual Attribute to_attribute () const { return Attribute::create_empty (); } virtual bool check_cfg_predicate (const Session &session) const = 0; + + virtual bool is_key_value_pair () const { return false; } }; // Container used to store MetaItems as AttrInput (bridge-ish kinda thing) diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 1bf8912083a..ce515db0aad 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -21,6 +21,7 @@ #include "rust-ast.h" #include "rust-location.h" +#include namespace Rust { namespace AST { @@ -816,6 +817,13 @@ public: Attribute to_attribute () const override; + inline std::pair get_name_value_pair () const + { + return std::pair (ident, str); + } + + bool is_key_value_pair () const override { return true; } + protected: // Use covariance to implement clone function as returning this type MetaNameValueStr *clone_meta_item_inner_impl () const override diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 8fa3fa18793..335a6d411a5 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -22,7 +22,8 @@ #include "rust-compile-fnparam.h" #include "rust-compile-var-decl.h" -#include "rust-expr.h" // for AST::AttrInputLiteral +#include "rust-expr.h" // for AST::AttrInputLiteral +#include "rust-macro.h" // for AST::MetaNameValueStr #include "fold-const.h" #include "stringpool.h" @@ -66,6 +67,9 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point, bool is_link_section = attr.get_path ().as_string ().compare ("link_section") == 0; bool no_mangle = attr.get_path ().as_string ().compare ("no_mangle") == 0; + bool is_deprecated + = attr.get_path ().as_string ().compare ("deprecated") == 0; + if (is_inline) { handle_inline_attribute_on_fndecl (fndecl, attr); @@ -82,6 +86,10 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point, { handle_link_section_attribute_on_fndecl (fndecl, attr); } + else if (is_deprecated) + { + handle_deprecated_attribute_on_fndecl (fndecl, attr); + } else if (no_mangle) { handle_no_mangle_attribute_on_fndecl (fndecl, attr); @@ -147,6 +155,64 @@ HIRCompileBase::handle_no_mangle_attribute_on_fndecl ( DECL_ATTRIBUTES (fndecl)); } +void +HIRCompileBase::handle_deprecated_attribute_on_fndecl ( + tree fndecl, const AST::Attribute &attr) +{ + tree value = NULL_TREE; + TREE_DEPRECATED (fndecl) = 1; + + // simple #[deprecated] + if (!attr.has_attr_input ()) + return; + + const AST::AttrInput &input = attr.get_attr_input (); + auto input_type = input.get_attr_input_type (); + + if (input_type == AST::AttrInput::AttrInputType::LITERAL) + { + // handle #[deprecated = "message"] + auto &literal + = static_cast (attr.get_attr_input ()); + const auto &msg_str = literal.get_literal ().as_string (); + value = build_string (msg_str.size (), msg_str.c_str ()); + } + else if (input_type == AST::AttrInput::AttrInputType::TOKEN_TREE) + { + // handle #[deprecated(since = "...", note = "...")] + const auto &option = static_cast (input); + AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item (); + for (const auto &item : meta_item->get_items ()) + { + auto converted_item = item->to_meta_name_value_str (); + if (!converted_item) + continue; + auto key_value = converted_item->get_name_value_pair (); + if (key_value.first.compare ("since") == 0) + { + // valid, but this is handled by Cargo and some third-party audit + // tools + continue; + } + else if (key_value.first.compare ("note") == 0) + { + const auto &msg_str = key_value.second; + if (value) + rust_error_at (attr.get_locus (), "multiple % items"); + value = build_string (msg_str.size (), msg_str.c_str ()); + } + else + { + rust_error_at (attr.get_locus (), "unknown meta item %qs", + key_value.first.c_str ()); + } + } + } + + DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("deprecated"), value, + DECL_ATTRIBUTES (fndecl)); +} + void HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr) diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h index 52e0568c88e..f993d06cb33 100644 --- a/gcc/rust/backend/rust-compile-base.h +++ b/gcc/rust/backend/rust-compile-base.h @@ -92,6 +92,9 @@ protected: static void handle_link_section_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr); + static void + handle_deprecated_attribute_on_fndecl (tree fndecl, + const AST::Attribute &attr); static void handle_no_mangle_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr); diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index c36d462537d..870ca21f87c 100644 --- a/gcc/rust/util/rust-attributes.cc +++ b/gcc/rust/util/rust-attributes.cc @@ -27,6 +27,7 @@ static const BuiltinAttrDefinition __definitions[] = { {"cold", CODE_GENERATION}, {"cfg", EXPANSION}, {"cfg_attr", EXPANSION}, + {"deprecated", STATIC_ANALYSIS}, {"allow", STATIC_ANALYSIS}, {"doc", HIR_LOWERING}, {"must_use", STATIC_ANALYSIS},