From patchwork Thu Aug 1 14:56:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967756 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=gDEr9wDw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZXYs6pLRz1ybV for ; Fri, 2 Aug 2024 01:14:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 22CC53865474 for ; Thu, 1 Aug 2024 15:14:24 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id CDB18385ED72 for ; Thu, 1 Aug 2024 14:59:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CDB18385ED72 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CDB18385ED72 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::133 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524387; cv=none; b=Rqgm4usoo0FyYJRUwdWB7EpiQYHykYQkpqSqjb/QJo5sO7e+1rxnaaQKtmuulPQaqHk1+GTAg+f5b8RunNtVYBSTGFl5yr5PZQ15GDmzwyZ/j0tWb1Lfu9bnKwt6ppEfIWHKeDdkazyMtZpTX4gav9bkCk2trTcy81lO5Z+zq+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524387; c=relaxed/simple; bh=+h6EgMZx+SfxZbmK5eKHiA9iIZ1IZW+2O2Qx0gJaLhs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=a3ygpELrsh0sX1l25NlDwUYVvYU5UH/p7/KQmqdm2vUVxMeFq6C14ITqzk7C3uG8K6mk0364B6LG9NT+YHrt93RxGwmdg3uhBK9/lvck+nPTOERtcbe9/zZh6u+kxei652xVLuO3g2KnN5y3O9F813QxW8QEkl0xoTv6lSwOBpA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-52ed741fe46so8078108e87.0 for ; Thu, 01 Aug 2024 07:59:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524352; x=1723129152; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=46pRccstlxGi9A887C+sJR6r9+68A6qOZcWBRJ0QDCw=; b=gDEr9wDwLd8vzLlAPjVGQFD0CH4dGTWOXm/UuezHaoyDhzUk+Kl/PX63GTjyLuA+7f qmMITKV5DrSQCbo5WvHG+qax+okkPKW2EAVpnTHbR/ZUx9dSqAGaZK3OHOK1uufZjxrt GZcVI6EQ+7nI1bN5zmqbrw3eKzbGe496utp6QZbkyKAKzCDJOJWZ8Wvk/ldZ7ZCuEEhx DaQk3l3PpBK/fVm83IXV6GNicG8ujeL0UrBIAqCGmY0m9kR2kKFj/xm3yJ0HTuUzaMWL r+4umRuVcOvDEpjvzbhUd9+F55jimzt7Mas3LbAdPPY+nKsltxmOXFBg24k90B5Y+V4Y dVcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524352; x=1723129152; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=46pRccstlxGi9A887C+sJR6r9+68A6qOZcWBRJ0QDCw=; b=tJgKtO0IkR751ggKCkZjjp8UCRIOQ93ZbWTUBxzt2Hc/bClKx15wHoRit/HaR/+oq+ JfALWvEKLVPDODoe1H9zJiT4JzWMwOMH9V/4cbCIoNPgg7J3itBVVDvNJOOZG2H6KmCr dX8Q0FMjg229jEujUkOqRW+4Z2Xwd64OjnZBkhslmXP/oDUaKrACnilDwW/EdV6iLqAN 4lTM2C7kHDUTMp5An9TCsFLFIHVQjRJ8kdZiIzi/Jy6NHxxkjYaf1LerOE5eJlj5jBL/ 1xZiAQW1Fdp0v9wRXHNpHiEKm65wFkm0TUW+4aVLkVhfPxSb2qOUX1MGaZzZeiI1ub2H o0eA== X-Gm-Message-State: AOJu0YxUT9aqA6n9qDlXgrQU7XlyZPqAzmQWXcJTyYwGiMvASdpXBbUh VWrss/3DD27ab3bDmY6qFcOaonZURmBhiWGz0BPsVALfrEXvKQICVSqUod5pZEKbK3776/Cqgf6 8G8lr X-Google-Smtp-Source: AGHT+IGH6dCFEeCIPDUUxrwxgF8PIUiLEBajFpvuc8v52QiLO8ZOANemHQZp9BWugUN2/345pbHC6Q== X-Received: by 2002:a05:6512:220d:b0:52e:8141:1b2b with SMTP id 2adb3069b0e04-530bb6c7fc0mr73320e87.57.1722524351915; Thu, 01 Aug 2024 07:59:11 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.59.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:11 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [PATCH 051/125] gccrs: format-args: Only pass the format string to the parser. Date: Thu, 1 Aug 2024 16:56:47 +0200 Message-ID: <20240801145809.366388-53-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This fixes an issue we had where the generated code ended with more static pieces than its rustc counterpart. gcc/rust/ChangeLog: * expand/rust-macro-builtins.cc (struct FormatArgsInput): Store the format_str as a string instead of an AST::Expr. (format_args_parse_arguments): Transform format_expr into a format string properly - add note for handling eager macro invocations later on. (MacroBuiltin::format_args_handler): Parse the correct input, append newline to format_str if necessary. --- gcc/rust/expand/rust-macro-builtins.cc | 37 +++++++++++++++----------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index 112713a4f97..a33a57752da 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -18,6 +18,7 @@ #include "expected.h" #include "libproc_macro_internal/tokenstream.h" +#include "optional.h" #include "rust-ast-full-decls.h" #include "rust-builtin-ast-nodes.h" #include "rust-expand-format-args.h" @@ -961,7 +962,7 @@ MacroBuiltin::stringify_handler (location_t invoc_locus, struct FormatArgsInput { - std::unique_ptr format_str; + std::string format_str; AST::FormatArguments args; // bool is_literal? }; @@ -985,12 +986,18 @@ format_args_parse_arguments (AST::MacroInvocData &invoc) auto args = AST::FormatArguments (); auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser); - std::unique_ptr format_str = nullptr; + std::unique_ptr format_expr = nullptr; // TODO: Handle the case where we're not parsing a string literal (macro // invocation for e.g.) if (parser.peek_current_token ()->get_id () == STRING_LITERAL) - format_str = parser.parse_literal_expr (); + format_expr = parser.parse_literal_expr (); + + // TODO(Arthur): Clean this up - if we haven't parsed a string literal but a + // macro invocation, what do we do here? return a tl::unexpected? + auto format_str = static_cast (*format_expr) + .get_literal () + .as_string (); // TODO: Allow implicit captures ONLY if the the first arg is a string literal // and not a macro invocation @@ -1053,6 +1060,13 @@ MacroBuiltin::format_args_handler (location_t invoc_locus, { auto input = format_args_parse_arguments (invoc); + if (!input) + { + rust_error_at (invoc_locus, + "could not parse arguments to %"); + return tl::nullopt; + } + // TODO(Arthur): We need to handle this // // if it is not a literal, it's an eager macro invocation - return it // if (!fmt_expr->is_literal ()) @@ -1080,20 +1094,13 @@ MacroBuiltin::format_args_handler (location_t invoc_locus, // rust_unreachable (); // } - // Remove the delimiters from the macro invocation: - // the invoc data for `format_args!(fmt, arg1, arg2)` is `(fmt, arg1, arg2)`, - // so we pop the front and back to remove the parentheses (or curly brackets, - // or brackets) - auto tokens = invoc.get_delim_tok_tree ().to_token_stream (); - tokens.erase (tokens.begin ()); - tokens.pop_back (); + bool append_newline = nl == AST::FormatArgs::Newline::Yes; - std::stringstream stream; - for (const auto &tok : tokens) - stream << tok->as_string () << ' '; + auto fmt_str = std::move (input->format_str); + if (append_newline) + fmt_str += '\n'; - auto append_newline = nl == AST::FormatArgs::Newline::Yes ? true : false; - auto pieces = Fmt::Pieces::collect (stream.str (), append_newline); + auto pieces = Fmt::Pieces::collect (fmt_str, append_newline); // TODO: // do the transformation into an AST::FormatArgs node