diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-03-03 09:23:06 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-03-03 11:08:18 +0000 |
commit | d6e1771291c792f665f5b9ed7d065bf292051e6a (patch) | |
tree | d26c37ac592965fdb52caa73c41cd6e95ae322c8 /gcc/rust/backend/rust-compile-base.cc | |
parent | 57b50607988481238275d8a2f73722896cbe0fe2 (diff) | |
download | gcc-d6e1771291c792f665f5b9ed7d065bf292051e6a.zip gcc-d6e1771291c792f665f5b9ed7d065bf292051e6a.tar.gz gcc-d6e1771291c792f665f5b9ed7d065bf292051e6a.tar.bz2 |
must use attribute support
This ports over the code from the cpp front-end which implements the cpp
nodiscard attribute which has the same behaviour and is also implemented
in the front-end explicitly.
Fixes #856
Diffstat (limited to 'gcc/rust/backend/rust-compile-base.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 3fc7360..a705da7 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -22,6 +22,8 @@ #include "rust-compile-fnparam.h" #include "rust-compile-var-decl.h" +#include "rust-expr.h" // for AST::AttrInputLiteral + #include "fold-const.h" #include "stringpool.h" @@ -52,10 +54,16 @@ HIRCompileBase::setup_attributes_on_fndecl ( for (const auto &attr : attrs) { bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0; + bool is_must_use + = attr.get_path ().as_string ().compare ("must_use") == 0; if (is_inline) { handle_inline_attribute_on_fndecl (fndecl, attr); } + else if (is_must_use) + { + handle_must_use_attribute_on_fndecl (fndecl, attr); + } } } @@ -108,6 +116,30 @@ HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl, } void +HIRCompileBase::handle_must_use_attribute_on_fndecl (tree fndecl, + const AST::Attribute &attr) +{ + tree nodiscard = get_identifier ("nodiscard"); + tree value = NULL_TREE; + + if (attr.has_attr_input ()) + { + rust_assert (attr.get_attr_input ().get_attr_input_type () + == AST::AttrInput::AttrInputType::LITERAL); + + auto &literal + = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ()); + const auto &msg_str = literal.get_literal ().as_string (); + tree message = build_string (msg_str.size (), msg_str.c_str ()); + + value = tree_cons (nodiscard, message, NULL_TREE); + } + + DECL_ATTRIBUTES (fndecl) + = tree_cons (nodiscard, value, DECL_ATTRIBUTES (fndecl)); +} + +void HIRCompileBase::setup_abi_options (tree fndecl, ABI abi) { switch (abi) @@ -262,7 +294,8 @@ HIRCompileBase::compile_function_body (Context *ctx, tree fndecl, auto compiled_expr = CompileStmt::Compile (s.get (), ctx); if (compiled_expr != nullptr) { - ctx->add_statement (compiled_expr); + tree s = convert_to_void (compiled_expr, ICV_STATEMENT); + ctx->add_statement (s); } } |