Skip to content

Commit 5e26ac6

Browse files
powerboat9philberty
authored andcommitted
Improve handling of AttrInputLiteral
Also adjusts a few error messages to be more in line with rustc (and more amenable to extract_string_literal usage). gcc/rust/ChangeLog: * util/rust-attributes.cc (Attributes::extract_string_literal): New function definition. * util/rust-attributes.h (Attributes::extract_string_literal): New function declaration. * ast/rust-collect-lang-items.cc (get_lang_item_attr): Use extract_string_literal. * backend/rust-compile-base.cc: Include "rust-attributes.h". (HIRCompileBase::handle_link_section_attribute_on_fndecl): Use extract_string_literal. (HIRCompileBase::handle_must_use_attribute_on_fndecl): Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_lang_item_attribute): Likewise. * rust-session-manager.cc (Session::handle_crate_name): Likewise. Signed-off-by: Owen Avery <[email protected]>
1 parent 78a1d69 commit 5e26ac6

File tree

6 files changed

+54
-35
lines changed

6 files changed

+54
-35
lines changed

gcc/rust/ast/rust-collect-lang-items.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,16 @@ get_lang_item_attr (const T &maybe_lang_item)
4343
continue;
4444
}
4545

46-
bool is_lang_item = str_path == Values::Attributes::LANG
47-
&& attr.has_attr_input ()
48-
&& attr.get_attr_input ().get_attr_input_type ()
49-
== AST::AttrInput::AttrInputType::LITERAL;
46+
bool is_lang_item = str_path == Values::Attributes::LANG;
5047

5148
if (is_lang_item)
5249
{
53-
auto &literal
54-
= static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
55-
const auto &lang_item_type_str = literal.get_literal ().as_string ();
50+
auto lang_item_type_str
51+
= Analysis::Attributes::extract_string_literal (attr);
5652

57-
return LangItem::Parse (lang_item_type_str);
53+
rust_assert (lang_item_type_str.has_value ());
54+
55+
return LangItem::Parse (*lang_item_type_str);
5856
}
5957
}
6058

gcc/rust/backend/rust-compile-base.cc

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "rust-type-util.h"
3333
#include "rust-compile-implitem.h"
3434
#include "rust-attribute-values.h"
35+
#include "rust-attributes.h"
3536
#include "rust-immutable-name-resolution-context.h"
3637

3738
#include "fold-const.h"
@@ -251,25 +252,21 @@ void
251252
HIRCompileBase::handle_link_section_attribute_on_fndecl (
252253
tree fndecl, const AST::Attribute &attr)
253254
{
254-
if (!attr.has_attr_input ())
255+
auto msg_str = Analysis::Attributes::extract_string_literal (attr);
256+
257+
if (!msg_str.has_value ())
255258
{
256259
rust_error_at (attr.get_locus (),
257-
"%<link_section%> expects exactly one argment");
260+
"malformed %<link_section%> attribute input");
258261
return;
259262
}
260263

261-
rust_assert (attr.get_attr_input ().get_attr_input_type ()
262-
== AST::AttrInput::AttrInputType::LITERAL);
263-
264-
auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
265-
const auto &msg_str = literal.get_literal ().as_string ();
266-
267264
if (decl_section_name (fndecl))
268265
{
269266
rust_warning_at (attr.get_locus (), 0, "section name redefined");
270267
}
271268

272-
set_decl_section_name (fndecl, msg_str.c_str ());
269+
set_decl_section_name (fndecl, msg_str->c_str ());
273270
}
274271

275272
void
@@ -416,13 +413,10 @@ HIRCompileBase::handle_must_use_attribute_on_fndecl (tree fndecl,
416413

417414
if (attr.has_attr_input ())
418415
{
419-
rust_assert (attr.get_attr_input ().get_attr_input_type ()
420-
== AST::AttrInput::AttrInputType::LITERAL);
416+
auto msg_str = Analysis::Attributes::extract_string_literal (attr);
417+
rust_assert (msg_str.has_value ());
421418

422-
auto &literal
423-
= static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
424-
const auto &msg_str = literal.get_literal ().as_string ();
425-
tree message = build_string (msg_str.size (), msg_str.c_str ());
419+
tree message = build_string (msg_str->size (), msg_str->c_str ());
426420

427421
value = tree_cons (nodiscard, message, NULL_TREE);
428422
}

gcc/rust/hir/rust-ast-lower-base.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,10 @@ void
850850
ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
851851
const AST::Attribute &attr)
852852
{
853-
auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
854-
const auto &lang_item_type_str = literal.get_literal ().as_string ();
855-
auto lang_item_type = LangItem::Parse (lang_item_type_str);
853+
auto lang_item_type_str = Analysis::Attributes::extract_string_literal (attr);
854+
rust_assert (lang_item_type_str.has_value ());
855+
856+
auto lang_item_type = LangItem::Parse (*lang_item_type_str);
856857

857858
if (lang_item_type)
858859
mappings.insert_lang_item (*lang_item_type,

gcc/rust/rust-session-manager.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,32 +422,31 @@ Session::handle_crate_name (const char *filename,
422422
{
423423
if (attr.get_path () != "crate_name")
424424
continue;
425-
if (!attr.has_attr_input ())
425+
426+
auto msg_str = Analysis::Attributes::extract_string_literal (attr);
427+
if (!msg_str.has_value ())
426428
{
427429
rust_error_at (attr.get_locus (),
428-
"%<crate_name%> accepts one argument");
430+
"malformed %<crate_name%> attribute input");
429431
continue;
430432
}
431433

432-
auto &literal
433-
= static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
434-
const auto &msg_str = literal.get_literal ().as_string ();
435-
if (!validate_crate_name (msg_str, error))
434+
if (!validate_crate_name (*msg_str, error))
436435
{
437436
error.locus = attr.get_locus ();
438437
error.emit ();
439438
continue;
440439
}
441440

442-
if (options.crate_name_set_manually && (options.crate_name != msg_str))
441+
if (options.crate_name_set_manually && (options.crate_name != *msg_str))
443442
{
444443
rust_error_at (attr.get_locus (),
445444
"%<-frust-crate-name%> and %<#[crate_name]%> are "
446445
"required to match, but %qs does not match %qs",
447-
options.crate_name.c_str (), msg_str.c_str ());
446+
options.crate_name.c_str (), msg_str->c_str ());
448447
}
449448
crate_name_found = true;
450-
options.set_crate_name (msg_str);
449+
options.set_crate_name (*msg_str);
451450
}
452451

453452
options.crate_name_set_manually |= crate_name_found;

gcc/rust/util/rust-attributes.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ Attributes::is_known (const std::string &attribute_path)
3838
return !lookup.is_error ();
3939
}
4040

41+
tl::optional<std::string>
42+
Attributes::extract_string_literal (const AST::Attribute &attr)
43+
{
44+
if (!attr.has_attr_input ())
45+
return tl::nullopt;
46+
47+
auto &attr_input = attr.get_attr_input ();
48+
49+
if (attr_input.get_attr_input_type ()
50+
!= AST::AttrInput::AttrInputType::LITERAL)
51+
return tl::nullopt;
52+
53+
auto &literal_expr
54+
= static_cast<AST::AttrInputLiteral &> (attr_input).get_literal ();
55+
56+
auto lit_type = literal_expr.get_lit_type ();
57+
58+
// TODO: bring escape sequence handling out of lexing?
59+
if (lit_type != AST::Literal::LitType::STRING
60+
&& lit_type != AST::Literal::LitType::RAW_STRING)
61+
return tl::nullopt;
62+
63+
return literal_expr.as_string ();
64+
}
65+
4166
using Attrs = Values::Attributes;
4267

4368
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248

gcc/rust/util/rust-attributes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Attributes
2929
{
3030
public:
3131
static bool is_known (const std::string &attribute_path);
32+
static tl::optional<std::string>
33+
extract_string_literal (const AST::Attribute &attr);
3234
};
3335

3436
enum CompilerPass

0 commit comments

Comments
 (0)