Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_expand::base::{self, *};
use rustc_parse::parser::Parser;
use rustc_parse_format as parse;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::{
symbol::{kw, sym, Symbol},
BytePos,
};
use rustc_span::{InnerSpan, Span};
use rustc_target::asm::InlineAsmArch;

struct AsmArgs {
templates: Vec<P<ast::Expr>>,
Expand Down Expand Up @@ -424,6 +428,65 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast

let template_str = &template_str.as_str();
let template_snippet = ecx.source_map().span_to_snippet(template_sp).ok();

if let Some(snippet) = &template_snippet {
let default_dialect = match ecx.sess.asm_arch {
Some(InlineAsmArch::X86 | InlineAsmArch::X86_64) => 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,
Expand Down Expand Up @@ -631,3 +694,15 @@ pub fn expand_asm<'cx>(
}
}
}

fn check_syntax_directive<S: AsRef<str>>(piece: S, syntax: &str) -> Option<InnerSpan> {
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
}
}
23 changes: 23 additions & 0 deletions src/test/ui/asm/inline-syntax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![feature(asm, llvm_asm)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test needs to be conditional on target, or specify via compiler-flags what target to use. In later case, you will also need to make the test no_core – you can look at the other assembly tests (such as src/test/assembly/asm/x86-types.rs) to see how to set that up.

Copy link
Member

@nagisa nagisa Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should also check the behaviour when the target isn't x86_64, this can be done through revisions, you can see how those are used in the same x86-types.rs test as well.


fn main() {
unsafe {
asm!(".intel_syntax noprefix", "nop");
//~^ ERROR intel syntax is the default syntax on this target
asm!(".intel_syntax aaa noprefix", "nop");
//~^ 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
}
}
50 changes: 50 additions & 0 deletions src/test/ui/asm/inline-syntax.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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

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

error: using the .att_syntax directive may cause issues, use the att_syntax option instead
--> $DIR/inline-syntax.rs:9:15
|
LL | asm!(".att_syntax noprefix", "nop");
| ^^^^^^^^^^^^^^^^^^^^
|
help: remove the assembler directive and replace it with options(att_syntax)
|
LL | asm!("", "nop", options(att_syntax));
| -- ^^^^^^^^^^^^^^^^^^^^^

error: using the .att_syntax directive may cause issues, use the att_syntax option instead
--> $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)
|
LL | asm!("", "nop", options(att_syntax));
| -- ^^^^^^^^^^^^^^^^^^^^^

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