aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-11-08 16:50:50 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 19:13:13 +0100
commit93ca83c5efd6908b6c5602642004d0364bf9144b (patch)
tree8461fd36bb771bcbb409eb9a23432324811d6ac1 /gcc
parentea59190d54612f9f0e09c77a0601023ef43f861d (diff)
downloadgcc-93ca83c5efd6908b6c5602642004d0364bf9144b.zip
gcc-93ca83c5efd6908b6c5602642004d0364bf9144b.tar.gz
gcc-93ca83c5efd6908b6c5602642004d0364bf9144b.tar.bz2
gccrs: Add validation pass for label name
Prevent from using reserved keyword in label name. gcc/rust/ChangeLog: * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Check if there is a label before visit. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Emit an error when a label has a forbidden name. * checks/errors/rust-ast-validation.h: Add function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast-visitor.cc4
-rw-r--r--gcc/rust/checks/errors/rust-ast-validation.cc15
-rw-r--r--gcc/rust/checks/errors/rust-ast-validation.h1
3 files changed, 19 insertions, 1 deletions
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index 63f850f..b9ff2f9 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -494,7 +494,9 @@ void
DefaultASTVisitor::visit (AST::BreakExpr &expr)
{
visit_outer_attrs (expr);
- visit (expr.get_label ());
+ if (expr.has_label ())
+ visit (expr.get_label ());
+
if (expr.has_break_expr ())
visit (expr.get_break_expr ());
}
diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc
index 3af5655..44dec61 100644
--- a/gcc/rust/checks/errors/rust-ast-validation.cc
+++ b/gcc/rust/checks/errors/rust-ast-validation.cc
@@ -37,6 +37,21 @@ ASTValidation::visit (AST::Lifetime &lifetime)
}
void
+ASTValidation::visit (AST::LoopLabel &label)
+{
+ auto name = label.get_lifetime ().get_lifetime_name ();
+ auto lifetime_name = '\'' + name;
+ auto &keywords = Values::Keywords::keywords;
+ if (keywords.find (name) != keywords.end ())
+ rust_error_at (label.get_locus (), "invalid label name %qs",
+ lifetime_name.c_str ());
+
+ // WARNING: Do not call ContextualASTVisitor, we don't want to visit the
+ // lifetime
+ // Maybe we should refactor LoopLabel instead ?
+}
+
+void
ASTValidation::visit (AST::ConstantItem &const_item)
{
if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL)
diff --git a/gcc/rust/checks/errors/rust-ast-validation.h b/gcc/rust/checks/errors/rust-ast-validation.h
index ef0b2ac..a21bcc4 100644
--- a/gcc/rust/checks/errors/rust-ast-validation.h
+++ b/gcc/rust/checks/errors/rust-ast-validation.h
@@ -35,6 +35,7 @@ public:
virtual void visit (AST::ConstantItem &const_item);
virtual void visit (AST::Lifetime &lifetime);
+ virtual void visit (AST::LoopLabel &label);
};
} // namespace Rust