Skip to content

Commit 5b9706f

Browse files
Pasta-coderP-E-P
authored andcommitted
util/attributes: error on malformed #[export_name] input
Emit a diagnostic when #[export_name] is used without arguments or with invalid arguments (non-string literals). This prevents silent failures or backend crashes when lowering the attribute to GIMPLE, ensuring the attribute follows the expected form: #[export_name = name]. Fixes #4387 gcc/rust/ChangeLog: * util/rust-attributes.cc (check_export_name_attribute): New helper. (AttributeChecker::visit): Check export_name on functions. gcc/testsuite/ChangeLog: * rust/compile/issue-4387.rs: New test. Signed-off-by: Jayant Chauhan <[email protected]>
1 parent 457a44e commit 5b9706f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

gcc/rust/util/rust-attributes.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,25 @@ check_link_section_attribute (const AST::Attribute &attribute)
446446
}
447447
}
448448

449+
static void
450+
check_export_name_attribute (const AST::Attribute &attribute)
451+
{
452+
if (!attribute.has_attr_input ())
453+
{
454+
rust_error_at (attribute.get_locus (),
455+
"malformed %<export_name%> attribute input");
456+
rust_inform (attribute.get_locus (),
457+
"must be of the form: %<#[export_name = \"name\"]%>");
458+
return;
459+
}
460+
461+
if (!Attributes::extract_string_literal (attribute))
462+
{
463+
rust_error_at (attribute.get_locus (),
464+
"attribute must be a string literal");
465+
}
466+
}
467+
449468
void
450469
AttributeChecker::check_attribute (const AST::Attribute &attribute)
451470
{
@@ -906,6 +925,10 @@ AttributeChecker::visit (AST::Function &fun)
906925
else
907926
check_no_mangle_function (attribute, fun);
908927
}
928+
else if (result.name == Attrs::EXPORT_NAME)
929+
{
930+
check_export_name_attribute (attribute);
931+
}
909932
else if (result.name == Attrs::LINK_NAME)
910933
{
911934
if (!attribute.has_attr_input ())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[export_name] // { dg-error "malformed" }
2+
fn foo() {}
3+
4+
#[export_name(123)] // { dg-error "attribute must be a string literal" }
5+
fn bar() {}
6+
7+
#[export_name = 123] // { dg-error "attribute must be a string literal" }
8+
fn baz() {}
9+
10+
#[export_name = "valid"]
11+
fn qux() {}

0 commit comments

Comments
 (0)