Skip to content

Commit ed6e1a8

Browse files
committed
rust: macros: replace quote! with quote::quote and use proc-macro2
We now have access to the `quote` crate providing the `quote!` macro that does the same (and more) as our own `quote!` macro. Thus replace our own version with the one from the crate. Since the `quote` crate uses the `proc-macro2` library, we also use that in our macros instead of `proc-macro`. Signed-off-by: Benno Lossin <[email protected]>
1 parent 1b729e1 commit ed6e1a8

File tree

10 files changed

+31
-187
lines changed

10 files changed

+31
-187
lines changed

rust/macros/concat_idents.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use proc_macro::{token_stream, Ident, TokenStream, TokenTree};
3+
use proc_macro2::{token_stream, Ident, TokenStream, TokenTree};
44

55
use crate::helpers::expect_punct;
66

rust/macros/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use proc_macro::{token_stream, Group, TokenStream, TokenTree};
3+
use proc_macro2::{token_stream, Group, TokenStream, TokenTree};
44

55
pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
66
if let Some(TokenTree::Ident(ident)) = it.next() {
@@ -166,7 +166,7 @@ pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec<TokenTree>) {
166166
1 => {
167167
// Here depending on the token, it might be a generic variable name.
168168
match tt.clone() {
169-
TokenTree::Ident(i) if at_start && i.to_string() == "const" => {
169+
TokenTree::Ident(i) if at_start && i == "const" => {
170170
let Some(name) = toks.next() else {
171171
// Parsing error.
172172
break;

rust/macros/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
//! Crate for all kernel procedural macros.
44
5-
#[macro_use]
6-
mod quote;
75
mod concat_idents;
86
mod helpers;
97
mod module;
@@ -76,7 +74,7 @@ use proc_macro::TokenStream;
7674
/// - `alias`: byte array of alias name of the kernel module.
7775
#[proc_macro]
7876
pub fn module(ts: TokenStream) -> TokenStream {
79-
module::module(ts)
77+
module::module(ts.into()).into()
8078
}
8179

8280
/// Declares or implements a vtable trait.
@@ -151,7 +149,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
151149
/// [`kernel::error::VTABLE_DEFAULT_ERROR`]: ../kernel/error/constant.VTABLE_DEFAULT_ERROR.html
152150
#[proc_macro_attribute]
153151
pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
154-
vtable::vtable(attr, ts)
152+
vtable::vtable(attr.into(), ts.into()).into()
155153
}
156154

157155
/// Concatenate two identifiers.
@@ -194,7 +192,7 @@ pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
194192
/// ```
195193
#[proc_macro]
196194
pub fn concat_idents(ts: TokenStream) -> TokenStream {
197-
concat_idents::concat_idents(ts)
195+
concat_idents::concat_idents(ts.into()).into()
198196
}
199197

200198
/// Used to specify the pinning information of the fields of a struct.
@@ -243,7 +241,7 @@ pub fn concat_idents(ts: TokenStream) -> TokenStream {
243241
// ^ cannot use direct link, since `kernel` is not a dependency of `macros`.
244242
#[proc_macro_attribute]
245243
pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
246-
pin_data::pin_data(inner, item)
244+
pin_data::pin_data(inner.into(), item.into()).into()
247245
}
248246

249247
/// Used to implement `PinnedDrop` safely.
@@ -270,7 +268,7 @@ pub fn pin_data(inner: TokenStream, item: TokenStream) -> TokenStream {
270268
/// ```
271269
#[proc_macro_attribute]
272270
pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
273-
pinned_drop::pinned_drop(args, input)
271+
pinned_drop::pinned_drop(args.into(), input.into()).into()
274272
}
275273

276274
/// Paste identifiers together.
@@ -382,9 +380,13 @@ pub fn pinned_drop(args: TokenStream, input: TokenStream) -> TokenStream {
382380
/// [`paste`]: https://docs.rs/paste/
383381
#[proc_macro]
384382
pub fn paste(input: TokenStream) -> TokenStream {
383+
let input: proc_macro2::TokenStream = input.into();
385384
let mut tokens = input.into_iter().collect();
386385
paste::expand(&mut tokens);
387-
tokens.into_iter().collect()
386+
tokens
387+
.into_iter()
388+
.collect::<proc_macro2::TokenStream>()
389+
.into()
388390
}
389391

390392
/// Derives the [`Zeroable`] trait for the given struct.
@@ -403,5 +405,5 @@ pub fn paste(input: TokenStream) -> TokenStream {
403405
/// ```
404406
#[proc_macro_derive(Zeroable)]
405407
pub fn derive_zeroable(input: TokenStream) -> TokenStream {
406-
zeroable::derive(input)
408+
zeroable::derive(input.into()).into()
407409
}

rust/macros/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use crate::helpers::*;
4-
use proc_macro::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
4+
use proc_macro2::{token_stream, Delimiter, Literal, TokenStream, TokenTree};
55
use std::fmt::Write;
66

77
fn expect_string_array(it: &mut token_stream::IntoIter) -> Vec<String> {

rust/macros/paste.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use proc_macro::{Delimiter, Group, Ident, Spacing, Span, TokenTree};
3+
use proc_macro2::{Delimiter, Group, Ident, Spacing, Span, TokenTree};
44

55
fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
66
let mut tokens = tokens.iter();

rust/macros/pin_data.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

33
use crate::helpers::{parse_generics, Generics};
4-
use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};
4+
use proc_macro2::{Group, Punct, Spacing, TokenStream, TokenTree};
5+
use quote::quote;
56

67
pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
78
// This proc-macro only does some pre-parsing and then delegates the actual parsing to
@@ -25,7 +26,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
2526
// The name of the struct with ty_generics.
2627
let struct_name = rest
2728
.iter()
28-
.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string() == "struct"))
29+
.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if *i == "struct"))
2930
.nth(1)
3031
.and_then(|tt| match tt {
3132
TokenTree::Ident(_) => {
@@ -62,7 +63,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
6263
.into_iter()
6364
.flat_map(|tt| {
6465
// We ignore top level `struct` tokens, since they would emit a compile error.
65-
if matches!(&tt, TokenTree::Ident(i) if i.to_string() == "struct") {
66+
if matches!(&tt, TokenTree::Ident(i) if *i == "struct") {
6667
vec![tt]
6768
} else {
6869
replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)
@@ -95,11 +96,7 @@ fn replace_self_and_deny_type_defs(
9596
) -> Vec<TokenTree> {
9697
match tt {
9798
TokenTree::Ident(ref i)
98-
if i.to_string() == "enum"
99-
|| i.to_string() == "trait"
100-
|| i.to_string() == "struct"
101-
|| i.to_string() == "union"
102-
|| i.to_string() == "impl" =>
99+
if *i == "enum" || *i == "trait" || *i == "struct" || *i == "union" || *i == "impl" =>
103100
{
104101
errs.extend(
105102
format!(
@@ -116,7 +113,7 @@ fn replace_self_and_deny_type_defs(
116113
);
117114
vec![tt]
118115
}
119-
TokenTree::Ident(i) if i.to_string() == "Self" => struct_name.clone(),
116+
TokenTree::Ident(i) if i == "Self" => struct_name.clone(),
120117
TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],
121118
TokenTree::Group(g) => vec![TokenTree::Group(Group::new(
122119
g.delimiter(),

rust/macros/pinned_drop.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3-
use proc_macro::{TokenStream, TokenTree};
3+
use proc_macro2::{TokenStream, TokenTree};
4+
use quote::quote;
45

56
pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
67
let mut toks = input.into_iter().collect::<Vec<_>>();
78
assert!(!toks.is_empty());
89
// Ensure that we have an `impl` item.
9-
assert!(matches!(&toks[0], TokenTree::Ident(i) if i.to_string() == "impl"));
10+
assert!(matches!(&toks[0], TokenTree::Ident(i) if *i == "impl"));
1011
// Ensure that we are implementing `PinnedDrop`.
1112
let mut nesting: usize = 0;
1213
let mut pinned_drop_idx = None;
@@ -24,7 +25,7 @@ pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream
2425
if i >= 1 && nesting == 0 {
2526
// Found the end of the generics, this should be `PinnedDrop`.
2627
assert!(
27-
matches!(tt, TokenTree::Ident(i) if i.to_string() == "PinnedDrop"),
28+
matches!(tt, TokenTree::Ident(i) if *i == "PinnedDrop"),
2829
"expected 'PinnedDrop', found: '{:?}'",
2930
tt
3031
);

rust/macros/quote.rs

Lines changed: 0 additions & 157 deletions
This file was deleted.

rust/macros/vtable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
3+
use proc_macro2::{Delimiter, Group, TokenStream, TokenTree};
44
use std::collections::HashSet;
55
use std::fmt::Write;
66

@@ -31,15 +31,15 @@ pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream {
3131
let mut consts = HashSet::new();
3232
while let Some(token) = body_it.next() {
3333
match token {
34-
TokenTree::Ident(ident) if ident.to_string() == "fn" => {
34+
TokenTree::Ident(ident) if ident == "fn" => {
3535
let fn_name = match body_it.next() {
3636
Some(TokenTree::Ident(ident)) => ident.to_string(),
3737
// Possibly we've encountered a fn pointer type instead.
3838
_ => continue,
3939
};
4040
functions.push(fn_name);
4141
}
42-
TokenTree::Ident(ident) if ident.to_string() == "const" => {
42+
TokenTree::Ident(ident) if ident == "const" => {
4343
let const_name = match body_it.next() {
4444
Some(TokenTree::Ident(ident)) => ident.to_string(),
4545
// Possibly we've encountered an inline const block instead.

rust/macros/zeroable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
use crate::helpers::{parse_generics, Generics};
4-
use proc_macro::{TokenStream, TokenTree};
4+
use proc_macro2::{TokenStream, TokenTree};
5+
use quote::quote;
56

67
pub(crate) fn derive(input: TokenStream) -> TokenStream {
78
let (

0 commit comments

Comments
 (0)