From 65226bce4027121e727ad145cc2646d232180688 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Thu, 9 Nov 2023 22:51:31 +0100 Subject: [PATCH] Add multiple check on variadics position Variadics are forbidden alone as well as non final position, this should be checked during ast validation. gcc/rust/ChangeLog: * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add check for additional named argument as well as variadic argument's position. * checks/errors/rust-ast-validation.h: Add visit function prototype for external functions. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/checks/errors/rust-ast-validation.cc | 19 +++++++++++++++++++ gcc/rust/checks/errors/rust-ast-validation.h | 1 + 2 files changed, 20 insertions(+) diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index 0ba73c674150..b06b027f0a5b 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -62,4 +62,23 @@ ASTValidation::visit (AST::ConstantItem &const_item) AST::ContextualASTVisitor::visit (const_item); } +void +ASTValidation::visit (AST::ExternalFunctionItem &item) +{ + auto ¶ms = item.get_function_params (); + + if (params.size () == 1) + rust_error_at ( + params[0].get_locus (), + "C-variadic function must be declared with at least one named argument"); + + for (auto it = params.begin (); it != params.end (); it++) + if (it->is_variadic () && it + 1 != params.end ()) + rust_error_at ( + it->get_locus (), + "%<...%> must be the last argument of a C-variadic function"); + + AST::ContextualASTVisitor::visit (item); +} + } // namespace Rust diff --git a/gcc/rust/checks/errors/rust-ast-validation.h b/gcc/rust/checks/errors/rust-ast-validation.h index a21bcc44f68b..6028b21ff006 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.h +++ b/gcc/rust/checks/errors/rust-ast-validation.h @@ -36,6 +36,7 @@ class ASTValidation : public AST::ContextualASTVisitor virtual void visit (AST::ConstantItem &const_item); virtual void visit (AST::Lifetime &lifetime); virtual void visit (AST::LoopLabel &label); + virtual void visit (AST::ExternalFunctionItem &item); }; } // namespace Rust