Support compiling programs with test mode in test_mono

This commit is contained in:
Ayaz Hafiz 2022-12-07 10:48:41 -06:00
parent 2ec473ba11
commit 935255d06b
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
2 changed files with 41 additions and 20 deletions

View file

@ -1,14 +1,30 @@
//! Macros for use in `test_mono`.
extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree};
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_attribute]
pub fn mono_test(args: TokenStream, item: TokenStream) -> TokenStream {
let no_check = args
.into_iter()
.any(|tok| matches!(tok, TokenTree::Ident(id) if id.to_string() == "no_check"));
let mut no_check = false;
let mut mode = "exec".to_owned();
for arg in syn::parse_macro_input!(args as syn::AttributeArgs) {
use syn::{Lit, Meta, MetaNameValue, NestedMeta};
if matches!(&arg, NestedMeta::Meta(Meta::Path(p)) if p.is_ident("no_check")) {
no_check = true;
}
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
eq_token: _,
lit: Lit::Str(s),
})) = arg
{
if path.is_ident("mode") {
mode = s.value();
}
}
}
let task_fn = syn::parse_macro_input!(item as syn::ItemFn);
let args = task_fn.sig.inputs.clone();
@ -24,7 +40,7 @@ pub fn mono_test(args: TokenStream, item: TokenStream) -> TokenStream {
#[test]
#(#attributes)*
#visibility fn #name(#args) {
compiles_to_ir(#name_str, #body, #no_check);
compiles_to_ir(#name_str, #body, &#mode, #no_check);
}
};