Skip to content

Commit 9c9d4b9

Browse files
authored
Merge pull request #826 from bjorn3/concat_idents_macro
Re-implement concat_idents! in libmacros
2 parents e5aaaba + 8758b75 commit 9c9d4b9

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

drivers/android/defs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use kernel::{
99

1010
macro_rules! pub_no_prefix {
1111
($prefix:ident, $($newname:ident),+) => {
12-
$(pub(crate) const $newname: u32 = concat_idents!($prefix, $newname);)+
12+
$(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+
1313
};
1414
}
1515

rust/kernel/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#![no_std]
1515
#![feature(allocator_api)]
1616
#![feature(associated_type_defaults)]
17-
#![feature(concat_idents)]
1817
#![feature(const_mut_refs)]
1918
#![feature(const_ptr_offset_from)]
2019
#![feature(const_refs_to_cell)]
@@ -41,6 +40,8 @@ mod allocator;
4140
#[doc(hidden)]
4241
pub use bindings;
4342

43+
pub use macros;
44+
4445
#[cfg(CONFIG_ARM_AMBA)]
4546
pub mod amba;
4647
pub mod chrdev;

rust/macros/concat_idents.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
use proc_macro::{token_stream, Ident, TokenStream, TokenTree};
4+
5+
use crate::helpers::expect_punct;
6+
7+
fn expect_ident(it: &mut token_stream::IntoIter) -> Ident {
8+
if let Some(TokenTree::Ident(ident)) = it.next() {
9+
ident
10+
} else {
11+
panic!("Expected Ident")
12+
}
13+
}
14+
15+
pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
16+
let mut it = ts.into_iter();
17+
let a = expect_ident(&mut it);
18+
assert_eq!(expect_punct(&mut it), ',');
19+
let b = expect_ident(&mut it);
20+
assert!(it.next().is_none(), "only two idents can be concatenated");
21+
let res = Ident::new(&(a.to_string() + &b.to_string()), b.span());
22+
TokenStream::from_iter([TokenTree::Ident(res)])
23+
}

rust/macros/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! Crate for all kernel procedural macros.
44
5+
mod concat_idents;
56
mod helpers;
67
mod module;
78
mod vtable;
@@ -144,3 +145,46 @@ pub fn module(ts: TokenStream) -> TokenStream {
144145
pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream {
145146
vtable::vtable(attr, ts)
146147
}
148+
149+
/// Concatenate two identifiers.
150+
///
151+
/// This is useful in macros that need to declare or reference items with names
152+
/// starting with a fixed prefix and ending in a user specified name. The resulting
153+
/// identifier has the span of the second argument.
154+
///
155+
/// # Examples
156+
///
157+
/// ```ignore
158+
/// use kernel::macro::concat_idents;
159+
///
160+
/// macro_rules! pub_no_prefix {
161+
/// ($prefix:ident, $($newname:ident),+) => {
162+
/// $(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+
163+
/// };
164+
/// }
165+
///
166+
/// pub_no_prefix!(
167+
/// binder_driver_return_protocol_,
168+
/// BR_OK,
169+
/// BR_ERROR,
170+
/// BR_TRANSACTION,
171+
/// BR_REPLY,
172+
/// BR_DEAD_REPLY,
173+
/// BR_TRANSACTION_COMPLETE,
174+
/// BR_INCREFS,
175+
/// BR_ACQUIRE,
176+
/// BR_RELEASE,
177+
/// BR_DECREFS,
178+
/// BR_NOOP,
179+
/// BR_SPAWN_LOOPER,
180+
/// BR_DEAD_BINDER,
181+
/// BR_CLEAR_DEATH_NOTIFICATION_DONE,
182+
/// BR_FAILED_REPLY
183+
/// );
184+
///
185+
/// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
186+
/// ```
187+
#[proc_macro]
188+
pub fn concat_idents(ts: TokenStream) -> TokenStream {
189+
concat_idents::concat_idents(ts)
190+
}

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ $(obj)/%.lst: $(src)/%.c FORCE
275275
# Compile Rust sources (.rs)
276276
# ---------------------------------------------------------------------------
277277

278-
rust_allowed_features := allocator_api,bench_black_box,concat_idents,core_ffi_c,generic_associated_types,const_ptr_offset_from,const_refs_to_cell
278+
rust_allowed_features := allocator_api,bench_black_box,core_ffi_c,generic_associated_types,const_ptr_offset_from,const_refs_to_cell
279279

280280
rust_common_cmd = \
281281
RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \

0 commit comments

Comments
 (0)