Files
codeql/rust/ql/test/extractor-tests/macro-expansion/proc_macro.rs
Paolo Tranquilli 299fe2eb3f Rust: add proc-macro capabilities to QL tests
This adds the possibility to add a special `proc_macro.rs` source file
to QL tests, which will be generated into a `proc_macro` crate the
usual `lib` crate depends on.

This allow to define procedural macros in QL tests, and is here used to
move the `macro-expansion` integration test to be a language test
instead.

As the generated manifests involved were starting to get a bit complex,
they are now generated from a `mustache` template.
2025-06-17 16:58:33 +02:00

35 lines
1.0 KiB
Rust

use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_attribute]
pub fn repeat(attr: TokenStream, item: TokenStream) -> TokenStream {
let number = syn::parse_macro_input!(attr as syn::LitInt).base10_parse::<usize>().unwrap();
let ast = syn::parse_macro_input!(item as syn::ItemFn);
let items = (0..number)
.map(|i| {
let mut new_ast = ast.clone();
new_ast.sig.ident = syn::Ident::new(&format!("{}_{}", ast.sig.ident, i), ast.sig.ident.span());
new_ast
})
.collect::<Vec<_>>();
quote! {
#(#items)*
}.into()
}
#[proc_macro_attribute]
pub fn add_one(_attr: TokenStream, item: TokenStream) -> TokenStream {
let ast = syn::parse_macro_input!(item as syn::ItemFn);
let mut new_ast = ast.clone();
new_ast.sig.ident = syn::Ident::new(&format!("{}_new", ast.sig.ident), ast.sig.ident.span());
quote! {
#ast
#new_ast
}.into()
}
#[proc_macro_attribute]
pub fn erase(_attr: TokenStream, _item: TokenStream) -> TokenStream {
TokenStream::new()
}