From cca6ee5757f88f7f131c6d30f2f222c6f88e1886 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 1 Oct 2021 12:33:18 +0200 Subject: [PATCH] Expand `asm!` to `format_args!` --- crates/hir_expand/src/builtin_macro.rs | 27 +++++++++++++++---- .../test_data/highlight_strings.html | 3 +++ crates/ide/src/syntax_highlighting/tests.rs | 3 +++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 3d63e05195b..a4ddafbf7af 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -267,13 +267,30 @@ fn format_args_expand( fn asm_expand( _db: &dyn AstDatabase, _id: MacroCallId, - _tt: &tt::Subtree, + tt: &tt::Subtree, ) -> ExpandResult { - // both asm and llvm_asm don't return anything, so we can expand them to nothing, - // for now - let expanded = quote! { + // We expand all assembly snippets to `format_args!` invocations to get format syntax + // highlighting for them. + + let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() }; + + let mut literals = Vec::new(); + for tt in tt.token_trees.chunks(2) { + match tt { + [tt::TokenTree::Leaf(tt::Leaf::Literal(lit))] + | [tt::TokenTree::Leaf(tt::Leaf::Literal(lit)), tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: ',', id: _, spacing: _ }))] => + { + let krate = krate.clone(); + literals.push(quote!(#krate::format_args!(#lit);)); + } + _ => break, + } + } + + let expanded = quote! {{ + ##literals () - }; + }}; ExpandResult::ok(expanded) } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index f6d9d9aa14a..4f67c4316a4 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -82,6 +82,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd macro_rules! panic {} #[rustc_builtin_macro] macro_rules! assert {} +#[rustc_builtin_macro] +macro_rules! asm {} macro_rules! toho { () => ($crate::panic!("not yet implemented")); @@ -142,4 +144,5 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd assert!(true, "{}", 1); assert!(true, "{} asdasd", 1); toho!("{}fmt", 0); + asm!("mov eax, {0}"); } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index e7bfbcc6040..fd8e1a0e21d 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -477,6 +477,8 @@ mod panic { macro_rules! panic {} #[rustc_builtin_macro] macro_rules! assert {} +#[rustc_builtin_macro] +macro_rules! asm {} macro_rules! toho { () => ($crate::panic!("not yet implemented")); @@ -537,6 +539,7 @@ fn main() { assert!(true, "{}", 1); assert!(true, "{} asdasd", 1); toho!("{}fmt", 0); + asm!("mov eax, {0}"); }"# .trim(), expect_file!["./test_data/highlight_strings.html"],