Skip to content

Commit edad96f

Browse files
Pasta-coderP-E-P
authored andcommitted
util/attributes: error on malformed #[link_section] input
Emit a diagnostic when #[link_section] is used without arguments on functions or static items, matching rustc behavior. This prevents silent acceptance of empty attributes and provides a helpful diagnostic that shows the expected form. Fixes #4229 gcc/rust/ChangeLog: * util/rust-attributes.cc (check_link_section_attribute): New helper. (AttributeChecker::visit): Check link_section on functions and statics. gcc/testsuite/ChangeLog: * rust/compile/link_section-malformed.rs: New test. Signed-off-by: Jayant Chauhan <[email protected]>
1 parent a1ee38a commit edad96f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

gcc/rust/util/rust-attributes.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,18 @@ AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes)
433433
check_inner_attribute (attr);
434434
}
435435

436+
static void
437+
check_link_section_attribute (const AST::Attribute &attribute)
438+
{
439+
if (!attribute.has_attr_input ())
440+
{
441+
rust_error_at (attribute.get_locus (),
442+
"malformed %<link_section%> attribute input");
443+
rust_inform (attribute.get_locus (),
444+
"must be of the form: %<#[link_section = \"name\"]%>");
445+
}
446+
}
447+
436448
void
437449
AttributeChecker::check_attribute (const AST::Attribute &attribute)
438450
{
@@ -896,7 +908,12 @@ AttributeChecker::visit (AST::Function &fun)
896908
"must be of the form: %<#[link_name = \"name\"]%>");
897909
}
898910
}
911+
else if (result.name == Attrs::LINK_SECTION)
912+
{
913+
check_link_section_attribute (attribute);
914+
}
899915
}
916+
900917
if (fun.has_body ())
901918
fun.get_definition ().value ()->accept_vis (*this);
902919
}
@@ -958,6 +975,15 @@ void
958975
AttributeChecker::visit (AST::StaticItem &item)
959976
{
960977
check_proc_macro_non_function (item.get_outer_attrs ());
978+
979+
BuiltinAttrDefinition result;
980+
for (auto &attribute : item.get_outer_attrs ())
981+
{
982+
if (is_builtin (attribute, result) && result.name == Attrs::LINK_SECTION)
983+
{
984+
check_link_section_attribute (attribute);
985+
}
986+
}
961987
}
962988

963989
void
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// { dg-options "-w" }
2+
#[link_section] // { dg-error "malformed .link_section. attribute input" }
3+
pub static VAR1: u32 = 1;
4+
5+
// { dg-note "must be of the form" "" { target *-*-* } .-3 }

0 commit comments

Comments
 (0)