diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 168dee6678d6..bb529f9e5c2b 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -12,6 +12,7 @@ use rustc_span::{ BytePos, }; use rustc_span::{InnerSpan, Span}; +use rustc_target::asm::InlineAsmArch; struct AsmArgs { templates: Vec>, @@ -427,6 +428,65 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P ast::LlvmAsmDialect::Intel, + _ => ast::LlvmAsmDialect::Att, + }; + + let snippet = snippet.trim_matches('"'); + match default_dialect { + ast::LlvmAsmDialect::Intel => { + if let Some(span) = check_syntax_directive(snippet, ".intel_syntax") { + let span = template_span.from_inner(span); + let mut err = ecx.struct_span_err(span, "intel syntax is the default syntax on this target, and trying to use this directive may cause issues"); + err.span_suggestion( + span, + "remove this assembler directive", + "".to_string(), + Applicability::MachineApplicable, + ); + err.emit(); + } + + if let Some(span) = check_syntax_directive(snippet, ".att_syntax") { + let span = template_span.from_inner(span); + let mut err = ecx.struct_span_err(span, "using the .att_syntax directive may cause issues, use the att_syntax option instead"); + let asm_end = sp.hi() - BytePos(2); + let suggestions = vec![ + (span, "".to_string()), + ( + Span::new(asm_end, asm_end, sp.ctxt()), + ", options(att_syntax)".to_string(), + ), + ]; + err.multipart_suggestion( + "remove the assembler directive and replace it with options(att_syntax)", + suggestions, + Applicability::MachineApplicable, + ); + err.emit(); + } + } + ast::LlvmAsmDialect::Att => { + if let Some(span) = check_syntax_directive(snippet, ".att_syntax") { + let span = template_span.from_inner(span); + let mut err = ecx.struct_span_err(span, "att syntax is the default syntax on this target, and trying to use this directive may cause issues"); + err.span_suggestion( + span, + "remove this assembler directive", + "".to_string(), + Applicability::MachineApplicable, + ); + err.emit(); + } + + // Use of .intel_syntax is ignored + } + } + } + let mut parser = parse::Parser::new( template_str, str_style, @@ -468,54 +528,6 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P { - if let Some(idx) = s.find(".intel_syntax") { - let mut end = idx + ".intel_syntax".len(); - if let Some(prefix_idx) = s.split_at(end).1.find("noprefix") { - // Should be a space and it should be immediately after - if prefix_idx == 1 { - end += " noprefix".len(); - } - } - - let syntax_span = - template_span.from_inner(InnerSpan::new(idx + 1, end + 1)); - let mut err = ecx.struct_span_err(syntax_span, "intel sytnax is the default syntax, and trying to use this directive may cause issues"); - err.span_suggestion( - syntax_span, - "Remove this assembler directive", - s.replace(&s[idx..end], "").to_string(), - Applicability::MachineApplicable, - ); - err.emit(); - } - - if let Some(idx) = s.find(".att_syntax") { - let mut end = idx + ".att_syntax".len(); - if let Some(prefix_idx) = s.split_at(end).1.find("noprefix") { - // Should be a space and it should be immediately after - if prefix_idx == 1 { - end += " noprefix".len(); - } - } - - let syntax_span = - template_span.from_inner(InnerSpan::new(idx + 1, end + 1)); - let mut err = ecx.struct_span_err(syntax_span, "using the .att_syntax directive may cause issues, use the att_syntax option instead"); - let asm_end = sp.hi() - BytePos(2); - let suggestions = vec![ - (syntax_span, "".to_string()), - ( - Span::new(asm_end, asm_end, sp.ctxt()), - ", options(att_syntax)".to_string(), - ), - ]; - err.multipart_suggestion( - "Remove the assembler directive and replace it with options(att_syntax)", - suggestions, - Applicability::MachineApplicable, - ); - err.emit(); - } template.push(ast::InlineAsmTemplatePiece::String(s.to_string())) } parse::Piece::NextArgument(arg) => { @@ -682,3 +694,15 @@ pub fn expand_asm<'cx>( } } } + +fn check_syntax_directive>(piece: S, syntax: &str) -> Option { + let piece = piece.as_ref(); + if let Some(idx) = piece.find(syntax) { + let end = + idx + &piece[idx..].find(|c| matches!(c, '\n' | ';')).unwrap_or(piece[idx..].len()); + // Offset by one because these represent the span with the " removed + Some(InnerSpan::new(idx + 1, end + 1)) + } else { + None + } +} diff --git a/src/test/ui/asm/inline-syntax.rs b/src/test/ui/asm/inline-syntax.rs index ec8ff0718857..31e7f2cc7966 100644 --- a/src/test/ui/asm/inline-syntax.rs +++ b/src/test/ui/asm/inline-syntax.rs @@ -3,12 +3,21 @@ fn main() { unsafe { asm!(".intel_syntax noprefix", "nop"); - //~^ ERROR intel sytnax is the default syntax + //~^ ERROR intel syntax is the default syntax on this target asm!(".intel_syntax aaa noprefix", "nop"); - //~^ ERROR intel sytnax is the default syntax + //~^ ERROR intel syntax is the default syntax on this target asm!(".att_syntax noprefix", "nop"); //~^ ERROR using the .att_syntax directive may cause issues asm!(".att_syntax bbb noprefix", "nop"); //~^ ERROR using the .att_syntax directive may cause issues + asm!(".intel_syntax noprefix; nop"); + //~^ ERROR intel syntax is the default syntax on this target + + asm!( + r" + .intel_syntax noprefix + nop" + ); + //~^^^ ERROR intel syntax is the default syntax on this target } } diff --git a/src/test/ui/asm/inline-syntax.stderr b/src/test/ui/asm/inline-syntax.stderr index bd792660c6ae..241b302ad647 100644 --- a/src/test/ui/asm/inline-syntax.stderr +++ b/src/test/ui/asm/inline-syntax.stderr @@ -1,14 +1,14 @@ -error: intel sytnax is the default syntax, and trying to use this directive may cause issues +error: intel syntax is the default syntax on this target, and trying to use this directive may cause issues --> $DIR/inline-syntax.rs:5:15 | LL | asm!(".intel_syntax noprefix", "nop"); - | ^^^^^^^^^^^^^^^^^^^^^^ help: Remove this assembler directive + | ^^^^^^^^^^^^^^^^^^^^^^ help: remove this assembler directive -error: intel sytnax is the default syntax, and trying to use this directive may cause issues +error: intel syntax is the default syntax on this target, and trying to use this directive may cause issues --> $DIR/inline-syntax.rs:7:15 | LL | asm!(".intel_syntax aaa noprefix", "nop"); - | ^^^^^^^^^^^^^ help: Remove this assembler directive: `aaa noprefix` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this assembler directive error: using the .att_syntax directive may cause issues, use the att_syntax option instead --> $DIR/inline-syntax.rs:9:15 @@ -16,7 +16,7 @@ error: using the .att_syntax directive may cause issues, use the att_syntax opti LL | asm!(".att_syntax noprefix", "nop"); | ^^^^^^^^^^^^^^^^^^^^ | -help: Remove the assembler directive and replace it with options(att_syntax) +help: remove the assembler directive and replace it with options(att_syntax) | LL | asm!("", "nop", options(att_syntax)); | -- ^^^^^^^^^^^^^^^^^^^^^ @@ -25,12 +25,26 @@ error: using the .att_syntax directive may cause issues, use the att_syntax opti --> $DIR/inline-syntax.rs:11:15 | LL | asm!(".att_syntax bbb noprefix", "nop"); - | ^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^ | -help: Remove the assembler directive and replace it with options(att_syntax) +help: remove the assembler directive and replace it with options(att_syntax) | -LL | asm!(" bbb noprefix", "nop", options(att_syntax)); - | -- ^^^^^^^^^^^^^^^^^^^^^ +LL | asm!("", "nop", options(att_syntax)); + | -- ^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 4 previous errors +error: intel syntax is the default syntax on this target, and trying to use this directive may cause issues + --> $DIR/inline-syntax.rs:13:15 + | +LL | asm!(".intel_syntax noprefix; nop"); + | ^^^^^^^^^^^^^^^^^^^^^^ help: remove this assembler directive + +error: intel syntax is the default syntax on this target, and trying to use this directive may cause issues + --> $DIR/inline-syntax.rs:18:14 + | +LL | .intel_syntax noprefix + | ______________^ +LL | | nop" + | |_ help: remove this assembler directive + +error: aborting due to 6 previous errors