[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 11/38] rust: qemu-api-macros: extend error reporting facility to p
From: |
Paolo Bonzini |
Subject: |
[PULL 11/38] rust: qemu-api-macros: extend error reporting facility to parse errors |
Date: |
Fri, 10 Jan 2025 19:45:52 +0100 |
Generalize the CompileError tuple to an enum, that can be either an error
message or a parse error from syn.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
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
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,
};
-struct CompileError(String, Span);
-
-impl From<CompileError> for proc_macro2::TokenStream {
- fn from(err: CompileError) -> Self {
- let CompileError(msg, span) = err;
- quote_spanned! { span => compile_error!(#msg); }
- }
-}
+mod utils;
+use utils::MacroError;
fn get_fields<'a>(
input: &'a DeriveInput,
msg: &str,
-) -> Result<&'a Punctuated<Field, Comma>, CompileError> {
+) -> Result<&'a Punctuated<Field, Comma>, MacroError> {
if let Data::Struct(s) = &input.data {
if let Fields::Named(fs) = &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(),
))
}
}
-fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), CompileError> {
+fn is_c_repr(input: &DeriveInput, msg: &str) -> Result<(), MacroError> {
let expected = parse_quote! { #[repr(C)] };
if input.attrs.iter().any(|attr| attr == &expected) {
Ok(())
} else {
- Err(CompileError(
+ Err(MacroError::Message(
format!("#[repr(C)] required for {}", msg),
input.ident.span(),
))
}
}
-fn derive_object_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream, CompileError> {
+fn derive_object_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream, MacroError> {
is_c_repr(&input, "#[derive(Object)]")?;
let name = &input.ident;
@@ -80,7 +73,7 @@ pub fn derive_object(input: TokenStream) -> TokenStream {
}
#[rustfmt::skip::macros(quote)]
-fn derive_offsets_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream, CompileError> {
+fn derive_offsets_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream, MacroError> {
is_c_repr(&input, "#[derive(offsets)]")?;
let name = &input.ident;
diff --git a/rust/qemu-api-macros/src/utils.rs
b/rust/qemu-api-macros/src/utils.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 <pbonzini@redhat.com>
+// 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<syn::Error> for MacroError {
+ fn from(err: syn::Error) -> Self {
+ MacroError::ParseError(err)
+ }
+}
+
+impl From<MacroError> for proc_macro2::TokenStream {
+ fn from(err: MacroError) -> Self {
+ match err {
+ MacroError::Message(msg, span) => quote_spanned! { span =>
compile_error!(#msg); },
+ MacroError::ParseError(err) => err.into_compile_error(),
+ }
+ }
+}
--
2.47.1
- Re: [PULL 02/38] rust: add --check-cfg test to rustc arguments, (continued)
- [PULL 04/38] rust: add a utility module for compile-time type checks, Paolo Bonzini, 2025/01/10
- [PULL 16/38] make-release: only leave tarball of wrap-file subprojects, Paolo Bonzini, 2025/01/10
- [PULL 15/38] qom: remove unused field, Paolo Bonzini, 2025/01/10
- [PULL 03/38] rust: qom: add ParentField, Paolo Bonzini, 2025/01/10
- [PULL 10/38] rust: qom: make INSTANCE_POST_INIT take a shared reference, Paolo Bonzini, 2025/01/10
- [PULL 17/38] target/i386: improve code generation for BT, Paolo Bonzini, 2025/01/10
- [PULL 06/38] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField, Paolo Bonzini, 2025/01/10
- [PULL 05/38] rust: macros: check that #[derive(Object)] requires #[repr(C)], Paolo Bonzini, 2025/01/10
- [PULL 11/38] rust: qemu-api-macros: extend error reporting facility to parse errors,
Paolo Bonzini <=
- [PULL 13/38] rust: qdev: expose inherited methods to subclasses of SysBusDevice, Paolo Bonzini, 2025/01/10
- [PULL 21/38] target/i386/kvm: Remove local MSR_KVM_WALL_CLOCK and MSR_KVM_SYSTEM_TIME definitions, Paolo Bonzini, 2025/01/10
- [PULL 24/38] target/i386/confidential-guest: Fix comment of x86_confidential_guest_kvm_type(), Paolo Bonzini, 2025/01/10
- [PULL 33/38] i386/topology: Introduce helpers for various topology info of different level, Paolo Bonzini, 2025/01/10
- [PULL 30/38] i386/cpu: Drop the variable smp_cores and smp_threads in x86_cpu_pre_plug(), Paolo Bonzini, 2025/01/10
- [PULL 34/38] i386/cpu: Track a X86CPUTopoInfo directly in CPUX86State, Paolo Bonzini, 2025/01/10
- [PULL 28/38] target/i386/kvm: Replace ARRAY_SIZE(msr_handlers) with KVM_MSR_FILTER_MAX_RANGES, Paolo Bonzini, 2025/01/10
- [PULL 29/38] i386/cpu: Extract a common fucntion to setup value of MSR_CORE_THREAD_COUNT, Paolo Bonzini, 2025/01/10
- [PULL 18/38] target/i386: use shr to load high-byte registers into T0/T1, Paolo Bonzini, 2025/01/10
- [PULL 19/38] i386/cpu: Mark avx10_version filtered when prefix is NULL, Paolo Bonzini, 2025/01/10