From nobody Tue Dec 24 13:21:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1734705149; cv=none; d=zohomail.com; s=zohoarc; b=TNjs6OvuutwykCh5v8PPPk7P7I9ZMXdtz5oeHEIfm78Aalff6nMSroiru7cMx0GmphVbf5TpCvmvSPb43BjPYcYanc6usxukt7B4plI8htZGpf0tG6YMi58kfYtH/4UlawXW5Ix0fP4qg+LvZCzy1a8vp5JiehH3zQOVbbECVyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1734705149; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Szoag27KIkqUli3n0xSrTUcaxn0aravcRGL/6Nt+Rm8=; b=OBmWbJZDqD6+5SxOw5lhcJEIR6BjYgwbniMbupFaGK3EGswjW6MCdHlrOtXNSVCWL/LRel5qR7shVuQJ3urIeNHZLYiu4V5wap5Ru0sxDdY+tbRFsyr1f9BcTtE0hSz0wYxQl+Zhq64Yr0yypGAu5x63hKRymLId3/z24YyRHY8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1734705148936477.1506540509813; Fri, 20 Dec 2024 06:32:28 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tOe1W-0004UK-4z; Fri, 20 Dec 2024 09:30:38 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tOe1P-0004Sn-BC for qemu-devel@nongnu.org; Fri, 20 Dec 2024 09:30:31 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tOe1L-0006bP-8M for qemu-devel@nongnu.org; Fri, 20 Dec 2024 09:30:30 -0500 Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-460-AEFODpjZPZ6tQICh2AhyoA-1; Fri, 20 Dec 2024 09:30:24 -0500 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-385d735965bso1178949f8f.1 for ; Fri, 20 Dec 2024 06:30:24 -0800 (PST) Received: from [192.168.10.47] ([151.81.118.45]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a1c8330d4sm4154986f8f.29.2024.12.20.06.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2024 06:30:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734705026; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Szoag27KIkqUli3n0xSrTUcaxn0aravcRGL/6Nt+Rm8=; b=QmQ4b/Umxa9zicyRjKfaTsnh4kVNaGfrwSsbsh3nVxotzH+Bvd7jOks0xCs059iT5xE1kJ aSw+zjTI0Aa7J80hf+zGxVsOes6MZ5U6xvuPSmIdB2OLvGyAudXksQ0qiYO226Hg2XeeOm AhsXdBht8j7vCXq/xs8qXKE9or+hn7o= X-MC-Unique: AEFODpjZPZ6tQICh2AhyoA-1 X-Mimecast-MFC-AGG-ID: AEFODpjZPZ6tQICh2AhyoA X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734705022; x=1735309822; 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=Szoag27KIkqUli3n0xSrTUcaxn0aravcRGL/6Nt+Rm8=; b=UykYY19OeyGPgESLmpBY6UJ7613RBHMSEKCCgq6zs+7aLrJbwsteFkOudmPxRZXoyY N/w22FPIS560BdCEsXBrTbCaEAQU+1M1G8rhDykUNCAr8BaagqMNiduiXwmR6KFO+vl2 Wto3Ua6WkK8W1/U+LEigXUqjDoXEfas1pen35be2j2ZspPaZMmNupTr7cnF6l+QcvT3H iXy/nO2It6On4d6xRoHrYZ04qOBBv8VHr6Z1HAnCT1FqEADowivVsz4a3MY+dq0IqiRn b06fzFCQUJHKrNDixQmwBt1UhBam9ON1s+h4tJjr+LbbPBZIDXCriG0AL4XVQedx45fa HL9w== X-Gm-Message-State: AOJu0YzMnyHeekvrGa72gMxMhSL5XVkHLo9TJ/SBATb73FzRrkeV/59D mroejnxHcCu+Uxllrfen0xMdgaBJZHTEpLtWknR5jcMg6tl5uIFNIci7TxqxPkFXuwHax4aY6ut cutQdl9i2pQTtoRf8/Q+Yf741s5gpYve5XaiHct2oq3U4f6L/Ce8JkD9JPS5yR5NbHt89nG9kM+ LYmR/crmhfz/4Lv6KxxGByXNWWE1py5pElZdL3 X-Gm-Gg: ASbGncu0FLGAA6GRqDuX0tO3ekVNceEL5LrrQ9NlRh92m94e6INPhwbbINm0x9AScTX PK/afkip1NgzONE0WE4SMhEmsf+wJT/BiQLGSV8c/UtRt6TxWQOR/YtuNiXmeMb4uMv+/2md1ag mQ7VCvx+wCG9spyAVGv5pWR+50AvzKeqKY46CiqrIaV5mk/raSNhZo5CNBsiYEoq62ro2cKKAS6 I43+qdixfIsAMWBkZuesHh9uVWE2Byhiph9KRAPTEiiubjjWvhWcBzRyI9r X-Received: by 2002:a5d:6d07:0:b0:385:f2a5:ef6a with SMTP id ffacd0b85a97d-38a221eaa5fmr3293928f8f.15.1734705022197; Fri, 20 Dec 2024 06:30:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHcGREriZdYrwfdKOQVlcM1YTgvD2rFEL6BRQMB0vo4IR5CccD8ADRPKD3WlS4diVwqozNl3Q== X-Received: by 2002:a5d:6d07:0:b0:385:f2a5:ef6a with SMTP id ffacd0b85a97d-38a221eaa5fmr3293884f8f.15.1734705021699; Fri, 20 Dec 2024 06:30:21 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: zhao1.liu@intel.com, junjie.mao@hotmail.com Subject: [PATCH 10/12] rust: qemu-api-macros: extend error reporting facility to parse errors Date: Fri, 20 Dec 2024 15:29:52 +0100 Message-ID: <20241220142955.652636-11-pbonzini@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241220142955.652636-1-pbonzini@redhat.com> References: <20241220142955.652636-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1.129, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1734705151111116600 Content-Type: text/plain; charset="utf-8" Generalize the CompileError tuple to an enum, that can be either an error message or a parse error from syn. Signed-off-by: Paolo Bonzini --- rust/qemu-api-macros/src/lib.rs | 27 ++++++++++----------------- rust/qemu-api-macros/src/utils.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 rust/qemu-api-macros/src/utils.rs diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib= .rs index 0f04cca3841..539c48df298 100644 --- a/rust/qemu-api-macros/src/lib.rs +++ b/rust/qemu-api-macros/src/lib.rs @@ -3,57 +3,50 @@ // SPDX-License-Identifier: GPL-2.0-or-later =20 use proc_macro::TokenStream; -use proc_macro2::Span; -use quote::{quote, quote_spanned}; +use quote::quote; use syn::{ parse_macro_input, parse_quote, punctuated::Punctuated, token::Comma, = Data, DeriveInput, Field, Fields, Ident, Type, Visibility, }; =20 -struct CompileError(String, Span); - -impl From for proc_macro2::TokenStream { - fn from(err: CompileError) -> Self { - let CompileError(msg, span) =3D err; - quote_spanned! { span =3D> compile_error!(#msg); } - } -} +mod utils; +use utils::MacroError; =20 fn get_fields<'a>( input: &'a DeriveInput, msg: &str, -) -> Result<&'a Punctuated, CompileError> { +) -> Result<&'a Punctuated, MacroError> { if let Data::Struct(s) =3D &input.data { if let Fields::Named(fs) =3D &s.fields { Ok(&fs.named) } else { - Err(CompileError( + Err(MacroError::Message( format!("Named fields required for {}", msg), input.ident.span(), )) } } else { - Err(CompileError( + Err(MacroError::Message( format!("Struct required for {}", msg), input.ident.span(), )) } } =20 -fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> { +fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), MacroError> { let expected =3D parse_quote! { #[repr(C)] }; =20 if input.attrs.iter().any(|attr| attr =3D=3D &expected) { Ok(()) } else { - Err(CompileError( + Err(MacroError::Message( format!("#[repr(C)] required for {}", msg), input.ident.span(), )) } } =20 -fn derive_object_or_error(input: DeriveInput) -> Result { +fn derive_object_or_error(input: DeriveInput) -> Result { is_c_repr(&input, "#[derive(Object)]")?; =20 let name =3D &input.ident; @@ -80,7 +73,7 @@ pub fn derive_object(input: TokenStream) -> TokenStream { } =20 #[rustfmt::skip::macros(quote)] -fn derive_offsets_or_error(input: DeriveInput) -> Result { +fn derive_offsets_or_error(input: DeriveInput) -> Result { is_c_repr(&input, "#[derive(offsets)]")?; =20 let name =3D &input.ident; diff --git a/rust/qemu-api-macros/src/utils.rs b/rust/qemu-api-macros/src/u= tils.rs new file mode 100644 index 00000000000..02c91aed7f6 --- /dev/null +++ b/rust/qemu-api-macros/src/utils.rs @@ -0,0 +1,26 @@ +// Procedural macro utilities. +// Author(s): Paolo Bonzini +// SPDX-License-Identifier: GPL-2.0-or-later + +use proc_macro2::Span; +use quote::quote_spanned; + +pub enum MacroError { + Message(String, Span), + ParseError(syn::Error), +} + +impl From for MacroError { + fn from(err: syn::Error) -> Self { + MacroError::ParseError(err) + } +} + +impl From for proc_macro2::TokenStream { + fn from(err: MacroError) -> Self { + match err { + MacroError::Message(msg, span) =3D> quote_spanned! { span =3D>= compile_error!(#msg); }, + MacroError::ParseError(err) =3D> err.into_compile_error(), + } + } +} --=20 2.47.1