[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/4] qemu-api-macros: switch #[property] parsing to use combinato
From: |
Paolo Bonzini |
Subject: |
[PATCH 4/4] qemu-api-macros: switch #[property] parsing to use combinators |
Date: |
Thu, 17 Jul 2025 08:27:27 +0200 |
attrs is a simple crate that provides parser combinators for
attribute arguments. Use it instead of a handwritten parser,
and adjust tests.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api-macros/src/lib.rs | 84 +++++++++++--------------------
rust/qemu-api-macros/src/tests.rs | 8 +--
2 files changed, 35 insertions(+), 57 deletions(-)
diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index a0a8d758bcc..b3d44da9300 100644
--- a/rust/qemu-api-macros/src/lib.rs
+++ b/rust/qemu-api-macros/src/lib.rs
@@ -3,11 +3,11 @@
// SPDX-License-Identifier: GPL-2.0-or-later
use proc_macro::TokenStream;
-use quote::{quote, quote_spanned, ToTokens};
+use quote::{quote, quote_spanned};
use syn::{
- parse::Parse, parse_macro_input, parse_quote, punctuated::Punctuated,
spanned::Spanned,
- token::Comma, Data, DeriveInput, Error, Field, Fields, FieldsUnnamed,
Ident, Meta, Path, Token,
- Variant,
+ parse::Parse, parse::ParseStream, parse_macro_input, parse_quote,
punctuated::Punctuated,
+ spanned::Spanned, token::Comma, Attribute, Data, DeriveInput, Error,
Field, Fields, FieldsUnnamed,
+ Ident, Meta, Path, Token, Variant,
};
mod bits;
use bits::BitsConstInternal;
@@ -153,61 +153,37 @@ enum DevicePropertyName {
Str(syn::LitStr),
}
-#[derive(Debug)]
+impl Parse for DevicePropertyName {
+ fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
+ let lo = input.lookahead1();
+ if lo.peek(syn::LitStr) {
+ Ok(Self::Str(input.parse()?))
+ } else if lo.peek(syn::LitCStr) {
+ Ok(Self::CStr(input.parse()?))
+ } else {
+ Err(lo.error())
+ }
+ }
+}
+
+#[derive(Default, Debug)]
struct DeviceProperty {
rename: Option<DevicePropertyName>,
defval: Option<syn::Expr>,
}
-impl Parse for DeviceProperty {
- fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
- let _: syn::Token![#] = input.parse()?;
- let bracketed;
- _ = syn::bracketed!(bracketed in input);
- let attribute = bracketed.parse::<syn::Ident>()?;
- debug_assert_eq!(&attribute.to_string(), "property");
- let mut retval = Self {
- rename: None,
- defval: None,
- };
- let content;
- _ = syn::parenthesized!(content in bracketed);
- while !content.is_empty() {
- let value: syn::Ident = content.parse()?;
- if value == "rename" {
- let _: syn::Token![=] = content.parse()?;
- if retval.rename.is_some() {
- return Err(syn::Error::new(
- value.span(),
- "`rename` can only be used at most once",
- ));
- }
- if content.peek(syn::LitStr) {
- retval.rename =
Some(DevicePropertyName::Str(content.parse::<syn::LitStr>()?));
- } else {
- retval.rename =
-
Some(DevicePropertyName::CStr(content.parse::<syn::LitCStr>()?));
- }
- } else if value == "default" {
- let _: syn::Token![=] = content.parse()?;
- if retval.defval.is_some() {
- return Err(syn::Error::new(
- value.span(),
- "`default` can only be used at most once",
- ));
- }
- retval.defval = Some(content.parse()?);
- } else {
- return Err(syn::Error::new(
- value.span(),
- format!("unrecognized field `{value}`"),
- ));
- }
+impl DeviceProperty {
+ fn parse_from(&mut self, a: &Attribute) -> syn::Result<()> {
+ use attrs::{set, with, Attrs};
+ let mut parser = Attrs::new();
+ parser.once("rename", with::eq(set::parse(&mut self.rename)));
+ parser.once("default", with::eq(set::parse(&mut self.defval)));
+ a.parse_args_with(&mut parser)
+ }
- if !content.is_empty() {
- let _: syn::Token![,] = content.parse()?;
- }
- }
+ fn parse(a: &Attribute) -> syn::Result<Self> {
+ let mut retval = Self::default();
+ retval.parse_from(a)?;
Ok(retval)
}
}
@@ -229,7 +205,7 @@ fn derive_device_or_error(input: DeriveInput) ->
Result<proc_macro2::TokenStream
f.attrs
.iter()
.filter(|a| a.path().is_ident("property"))
- .map(|a| Ok((f.clone(), syn::parse2(a.to_token_stream())?)))
+ .map(|a| Ok((f.clone(), DeviceProperty::parse(&a)?)))
})
.collect::<Result<Vec<_>, Error>>()?;
let name = &input.ident;
diff --git a/rust/qemu-api-macros/src/tests.rs
b/rust/qemu-api-macros/src/tests.rs
index 0e5a5728908..3c056652666 100644
--- a/rust/qemu-api-macros/src/tests.rs
+++ b/rust/qemu-api-macros/src/tests.rs
@@ -60,7 +60,7 @@ struct DummyState {
migrate_clock: bool,
}
},
- "unrecognized field `defalt`"
+ "Expected one of `default` or `rename`"
);
// Check that repeated attributes are not allowed:
derive_compile_fail!(
@@ -73,7 +73,8 @@ struct DummyState {
migrate_clock: bool,
}
},
- "`rename` can only be used at most once"
+ "Duplicate argument",
+ "Already used here",
);
derive_compile_fail!(
derive_device_or_error,
@@ -85,7 +86,8 @@ struct DummyState {
migrate_clock: bool,
}
},
- "`default` can only be used at most once"
+ "Duplicate argument",
+ "Already used here",
);
// Check that the field name is preserved when `rename` isn't used:
derive_compile!(
--
2.50.1