From: Pierre-Emmanuel Patry <[email protected]>

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 <[email protected]>
---
 gcc/rust/ast/rust-ast.cc                          | 6 ++++++
 gcc/rust/util/rust-attributes.cc                  | 4 ++--
 gcc/testsuite/rust/compile/attr_malformed_path.rs | 3 +++
 3 files changed, 11 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/attr_malformed_path.rs

diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index 003a6edbc10..a4b256c9e67 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -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 ())
        {
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 49c4282755c..ce29e59b06d 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -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
index 00000000000..2bccf37bd79
--- /dev/null
+++ b/gcc/testsuite/rust/compile/attr_malformed_path.rs
@@ -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 }
-- 
2.50.1

Reply via email to