1mod cpp;
16
17use proc_macro::TokenStream;
18
19#[proc_macro]
20pub fn cpreprocess(tokens: TokenStream) -> TokenStream {
34 #[cfg(not(feature = "nightly"))]
35 let tokens = syn::parse_macro_input!(tokens as syn::LitStr).value();
36
37 #[cfg(feature = "nightly")]
38 let tokens = match syn::parse::<syn::LitStr>(tokens.clone()) {
39 Ok(tokens) => tokens.value(),
40 Err(_) => proc_macro_faithful_display::faithful_display(&tokens).to_string()
41 };
42
43 match cpp::preprocess(tokens.as_bytes())
44 .map(|result| {
45 result.and_then(|code| {
46 String::from_utf8_lossy(&code).parse().map_err(Into::into)
47 })
48 })
49 {
50 Some(Ok(code)) => code,
51 Some(Err(err)) => format!("compile_error!(\"{}\")", err.to_string().replace('\\', "\\\\").replace('"', "\\\"")).parse().unwrap(),
52 None => "compile_error!(\"Couldn't find a compatible C compiler on this system\")".parse().unwrap()
53 }
54}