mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
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.
35 lines
1.0 KiB
Rust
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()
|
|
}
|