mirror of
https://github.com/torvalds/linux.git
synced 2025-04-12 16:47:42 +00:00

Rename relative paths inside of the crate to still refer to the same items, also rename paths inside of the kernel crate and adjust the build system to build the crate. [ Remove the `expect` (and thus the `lint_reasons` feature) since the tree now uses `quote!` from `rust/macros/export.rs`. Remove the `TokenStream` import removal, since it is now used as well. In addition, temporarily (i.e. just for this commit) use an `--extern force:alloc` to prevent an unknown `new_uninit` error in the `rustdoc` target. For context, please see a similar case in: https://lore.kernel.org/lkml/20240422090644.525520-1-ojeda@kernel.org/ And adjusted the message above. - Miguel ] Signed-off-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Fiona Behrens <me@kloenk.dev> Tested-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://lore.kernel.org/r/20250308110339.2997091-16-benno.lossin@proton.me Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
182 lines
6.7 KiB
Rust
182 lines
6.7 KiB
Rust
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
|
|
use proc_macro::{TokenStream, TokenTree};
|
|
|
|
#[allow(dead_code)]
|
|
pub(crate) trait ToTokens {
|
|
fn to_tokens(&self, tokens: &mut TokenStream);
|
|
}
|
|
|
|
impl<T: ToTokens> ToTokens for Option<T> {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
if let Some(v) = self {
|
|
v.to_tokens(tokens);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ToTokens for proc_macro::Group {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
tokens.extend([TokenTree::from(self.clone())]);
|
|
}
|
|
}
|
|
|
|
impl ToTokens for proc_macro::Ident {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
tokens.extend([TokenTree::from(self.clone())]);
|
|
}
|
|
}
|
|
|
|
impl ToTokens for TokenTree {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
tokens.extend([self.clone()]);
|
|
}
|
|
}
|
|
|
|
impl ToTokens for TokenStream {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
tokens.extend(self.clone());
|
|
}
|
|
}
|
|
|
|
/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
|
|
/// the given span.
|
|
///
|
|
/// This is a similar to the
|
|
/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
|
|
/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
|
|
macro_rules! quote_spanned {
|
|
($span:expr => $($tt:tt)*) => {{
|
|
let mut tokens: ::std::vec::Vec<::proc_macro::TokenTree>;
|
|
#[allow(clippy::vec_init_then_push)]
|
|
{
|
|
tokens = ::std::vec::Vec::new();
|
|
let span = $span;
|
|
quote_spanned!(@proc tokens span $($tt)*);
|
|
}
|
|
::proc_macro::TokenStream::from_iter(tokens)
|
|
}};
|
|
(@proc $v:ident $span:ident) => {};
|
|
(@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
|
|
let mut ts = ::proc_macro::TokenStream::new();
|
|
$crate::quote::ToTokens::to_tokens(&$id, &mut ts);
|
|
$v.extend(ts);
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
|
|
for token in $id {
|
|
let mut ts = ::proc_macro::TokenStream::new();
|
|
$crate::quote::ToTokens::to_tokens(&token, &mut ts);
|
|
$v.extend(ts);
|
|
}
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
|
|
#[allow(unused_mut)]
|
|
let mut tokens = ::std::vec::Vec::<::proc_macro::TokenTree>::new();
|
|
quote_spanned!(@proc tokens $span $($inner)*);
|
|
$v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
|
|
::proc_macro::Delimiter::Parenthesis,
|
|
::proc_macro::TokenStream::from_iter(tokens)
|
|
)));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
|
|
let mut tokens = ::std::vec::Vec::new();
|
|
quote_spanned!(@proc tokens $span $($inner)*);
|
|
$v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
|
|
::proc_macro::Delimiter::Bracket,
|
|
::proc_macro::TokenStream::from_iter(tokens)
|
|
)));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
|
|
let mut tokens = ::std::vec::Vec::new();
|
|
quote_spanned!(@proc tokens $span $($inner)*);
|
|
$v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
|
|
::proc_macro::Delimiter::Brace,
|
|
::proc_macro::TokenStream::from_iter(tokens)
|
|
)));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident :: $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new(':', ::proc_macro::Spacing::Joint)
|
|
));
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident : $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident , $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new(',', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident @ $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new('@', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident ! $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new('!', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident ; $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new(';', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident + $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new('+', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident = $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new('=', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident # $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Punct(
|
|
::proc_macro::Punct::new('#', ::proc_macro::Spacing::Alone)
|
|
));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident _ $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new("_", $span)));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
(@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
|
|
$v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(stringify!($id), $span)));
|
|
quote_spanned!(@proc $v $span $($tt)*);
|
|
};
|
|
}
|
|
|
|
/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
|
|
/// mixed site span ([`Span::mixed_site()`]).
|
|
///
|
|
/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
|
|
/// from the `quote` crate but provides only just enough functionality needed by the current
|
|
/// `macros` crate.
|
|
///
|
|
/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
|
|
macro_rules! quote {
|
|
($($tt:tt)*) => {
|
|
quote_spanned!(::proc_macro::Span::mixed_site() => $($tt)*)
|
|
}
|
|
}
|