about summary refs log tree commit diff
diff options
context:
space:
mode:
authorasquared31415 <34665709+asquared31415@users.noreply.github.com>2021-02-20 01:17:18 -0500
committerasquared31415 <34665709+asquared31415@users.noreply.github.com>2021-02-20 01:17:18 -0500
commit39dcd01bf5e0c69c487f18903f44074f49ef205b (patch)
tree9c2d9eb9a148301e2b3fabf701ae43f9dc25f10d
parent12c6a12d62fda3d5d070fe3915f03154ff7c80df (diff)
downloadrust-39dcd01bf5e0c69c487f18903f44074f49ef205b.tar.gz
rust-39dcd01bf5e0c69c487f18903f44074f49ef205b.zip
Take into account target default syntax
-rw-r--r--compiler/rustc_builtin_macros/src/asm.rs120
-rw-r--r--src/test/ui/asm/inline-syntax.rs13
-rw-r--r--src/test/ui/asm/inline-syntax.stderr34
3 files changed, 107 insertions, 60 deletions
diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs
index 168dee6678d..bb529f9e5c2 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<P<ast::Expr>>,
@@ -427,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,
@@ -468,54 +528,6 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, sp: Span, args: AsmArgs) -> P<ast
         for piece in unverified_pieces {
             match piece {
                 parse::Piece::String(s) => {
-                    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<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
+    }
+}
diff --git a/src/test/ui/asm/inline-syntax.rs b/src/test/ui/asm/inline-syntax.rs
index ec8ff071885..31e7f2cc796 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 bd792660c6a..241b302ad64 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: 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 4 previous errors
+error: aborting due to 6 previous errors