]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Avoid malformed attribute conversion
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 1 Oct 2025 14:35:37 +0000 (16:35 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 30 Oct 2025 20:30:55 +0000 (21:30 +0100)
Add location for malformed attribute errors and avoid processing of
malformed attributes.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (AttrInputMetaItemContainer::separate_cfg_attrs):
Avoid malformed attributes.
* util/rust-attributes.cc (AttributeChecker::visit): Change location.

gcc/testsuite/ChangeLog:

* rust/compile/attr_malformed_path.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ast/rust-ast.cc
gcc/rust/util/rust-attributes.cc
gcc/testsuite/rust/compile/attr_malformed_path.rs [new file with mode: 0644]

index 003a6edbc106a8baa032ee4f19f86a1f47664d63..a4b256c9e67febe83e30ced42f3f73e8034bd30d 100644 (file)
@@ -4128,6 +4128,12 @@ AttrInputMetaItemContainer::separate_cfg_attrs () const
 
   for (auto it = items.begin () + 1; it != items.end (); ++it)
     {
+      if ((*it)->get_kind () == MetaItemInner::Kind::MetaItem
+         && static_cast<MetaItem &> (**it).get_item_kind ()
+              == MetaItem::ItemKind::PathExpr
+         && !static_cast<MetaItemPathExpr &> (**it).get_expr ().is_literal ())
+       continue;
+
       Attribute attr = (*it)->to_attribute ();
       if (attr.is_empty ())
        {
index 49c4282755cc9ee10e4486557920d3b7717f17c4..ce29e59b06da917e916b42659e3aebb0784d9de5 100644 (file)
@@ -435,9 +435,9 @@ AttributeChecker::visit (AST::MetaItemPathExpr &attribute)
 {
   if (!attribute.get_expr ().is_literal ())
     {
-      rust_error_at (attribute.get_locus (),
+      rust_error_at (attribute.get_expr ().get_locus (),
                     "malformed %<path%> attribute input");
-      rust_inform (attribute.get_locus (),
+      rust_inform (attribute.get_expr ().get_locus (),
                   "must be of the form: %<#[path = \"file\"]%>");
     }
 }
diff --git a/gcc/testsuite/rust/compile/attr_malformed_path.rs b/gcc/testsuite/rust/compile/attr_malformed_path.rs
new file mode 100644 (file)
index 0000000..2bccf37
--- /dev/null
@@ -0,0 +1,3 @@
+#[cfg_attr(target_arch = "x86_64", path = (target_arch = "x86",    path = "x86.rs"))]
+mod imp {}
+// { dg-error "malformed .path. attribute input" "" { target *-*-* } .-2 }