mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
Auto merge of #12305 - beetrees:asm-syntax, r=Manishearth
Ensure ASM syntax detect `global_asm!` and `asm!` only on x86 architectures The ASM syntax lint is only relevant on x86 architectures, so this PR ensures it doesn't trigger on other architectures. This PR also makes the lints check `global_asm!` items as well as `asm!` expressions. changelog: Check `global_asm!` items in the ASM syntax lints, and fix false positives on non-x86 architectures.
This commit is contained in:
commit
5471e0645a
6 changed files with 219 additions and 57 deletions
|
@ -2,8 +2,11 @@ use std::fmt;
|
|||
|
||||
use clippy_utils::diagnostics::span_lint_and_help;
|
||||
use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions};
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass, Lint};
|
||||
use rustc_ast::{InlineAsm, Item, ItemKind};
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::Span;
|
||||
use rustc_target::asm::InlineAsmArch;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum AsmStyle {
|
||||
|
@ -31,8 +34,14 @@ impl std::ops::Not for AsmStyle {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr, check_for: AsmStyle) {
|
||||
if let ExprKind::InlineAsm(ref inline_asm) = expr.kind {
|
||||
fn check_asm_syntax(
|
||||
lint: &'static Lint,
|
||||
cx: &EarlyContext<'_>,
|
||||
inline_asm: &InlineAsm,
|
||||
span: Span,
|
||||
check_for: AsmStyle,
|
||||
) {
|
||||
if matches!(cx.sess().asm_arch, Some(InlineAsmArch::X86 | InlineAsmArch::X86_64)) {
|
||||
let style = if inline_asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
|
||||
AsmStyle::Att
|
||||
} else {
|
||||
|
@ -43,7 +52,7 @@ fn check_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr
|
|||
span_lint_and_help(
|
||||
cx,
|
||||
lint,
|
||||
expr.span,
|
||||
span,
|
||||
&format!("{style} x86 assembly syntax used"),
|
||||
None,
|
||||
&format!("use {} x86 assembly syntax", !style),
|
||||
|
@ -89,7 +98,15 @@ declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
|
|||
|
||||
impl EarlyLintPass for InlineAsmX86IntelSyntax {
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Intel);
|
||||
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
|
||||
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Intel);
|
||||
}
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
||||
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
|
||||
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Intel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,6 +147,14 @@ declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
|
|||
|
||||
impl EarlyLintPass for InlineAsmX86AttSyntax {
|
||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Att);
|
||||
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
|
||||
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Att);
|
||||
}
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
||||
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
|
||||
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Att);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax.rs:8:9
|
||||
|
|
||||
LL | asm!("");
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_intel_syntax)]`
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax.rs:10:9
|
||||
|
|
||||
LL | asm!("", options());
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax.rs:12:9
|
||||
|
|
||||
LL | asm!("", options(nostack));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax.rs:26:9
|
||||
|
|
||||
LL | asm!("", options(att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_att_syntax)]`
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax.rs:28:9
|
||||
|
|
||||
LL | asm!("", options(nostack, att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
24
tests/ui/asm_syntax_not_x86.rs
Normal file
24
tests/ui/asm_syntax_not_x86.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
//@ignore-target-i686
|
||||
//@ignore-target-x86
|
||||
//@needs-asm-support
|
||||
|
||||
#[warn(clippy::inline_asm_x86_intel_syntax)]
|
||||
#[warn(clippy::inline_asm_x86_att_syntax)]
|
||||
mod dont_warn {
|
||||
use std::arch::{asm, global_asm};
|
||||
|
||||
pub(super) unsafe fn use_asm() {
|
||||
asm!("");
|
||||
asm!("", options());
|
||||
asm!("", options(nostack));
|
||||
}
|
||||
|
||||
global_asm!("");
|
||||
global_asm!("", options());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
dont_warn::use_asm();
|
||||
}
|
||||
}
|
73
tests/ui/asm_syntax_x86.i686.stderr
Normal file
73
tests/ui/asm_syntax_x86.i686.stderr
Normal file
|
@ -0,0 +1,73 @@
|
|||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:10:9
|
||||
|
|
||||
LL | asm!("");
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_intel_syntax)]`
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:12:9
|
||||
|
|
||||
LL | asm!("", options());
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:14:9
|
||||
|
|
||||
LL | asm!("", options(nostack));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:20:5
|
||||
|
|
||||
LL | global_asm!("");
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:22:5
|
||||
|
|
||||
LL | global_asm!("", options());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:35:9
|
||||
|
|
||||
LL | asm!("", options(att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_att_syntax)]`
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:37:9
|
||||
|
|
||||
LL | asm!("", options(nostack, att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:43:5
|
||||
|
|
||||
LL | global_asm!("", options(att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
//@only-target-x86_64
|
||||
//@ignore-target-aarch64
|
||||
//@revisions: i686 x86_64
|
||||
//@[i686] only-target-i686
|
||||
//@[x86_64] only-target-x86_64
|
||||
|
||||
#[warn(clippy::inline_asm_x86_intel_syntax)]
|
||||
mod warn_intel {
|
||||
use std::arch::{asm, global_asm};
|
||||
|
||||
pub(super) unsafe fn use_asm() {
|
||||
use std::arch::asm;
|
||||
asm!("");
|
||||
//~^ ERROR: Intel x86 assembly syntax used
|
||||
asm!("", options());
|
||||
|
@ -14,12 +16,19 @@ mod warn_intel {
|
|||
asm!("", options(att_syntax));
|
||||
asm!("", options(nostack, att_syntax));
|
||||
}
|
||||
|
||||
global_asm!("");
|
||||
//~^ ERROR: Intel x86 assembly syntax used
|
||||
global_asm!("", options());
|
||||
//~^ ERROR: Intel x86 assembly syntax used
|
||||
global_asm!("", options(att_syntax));
|
||||
}
|
||||
|
||||
#[warn(clippy::inline_asm_x86_att_syntax)]
|
||||
mod warn_att {
|
||||
use std::arch::{asm, global_asm};
|
||||
|
||||
pub(super) unsafe fn use_asm() {
|
||||
use std::arch::asm;
|
||||
asm!("");
|
||||
asm!("", options());
|
||||
asm!("", options(nostack));
|
||||
|
@ -28,9 +37,13 @@ mod warn_att {
|
|||
asm!("", options(nostack, att_syntax));
|
||||
//~^ ERROR: AT&T x86 assembly syntax used
|
||||
}
|
||||
|
||||
global_asm!("");
|
||||
global_asm!("", options());
|
||||
global_asm!("", options(att_syntax));
|
||||
//~^ ERROR: AT&T x86 assembly syntax used
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
fn main() {
|
||||
unsafe {
|
||||
warn_att::use_asm();
|
73
tests/ui/asm_syntax_x86.x86_64.stderr
Normal file
73
tests/ui/asm_syntax_x86.x86_64.stderr
Normal file
|
@ -0,0 +1,73 @@
|
|||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:10:9
|
||||
|
|
||||
LL | asm!("");
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_intel_syntax)]`
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:12:9
|
||||
|
|
||||
LL | asm!("", options());
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:14:9
|
||||
|
|
||||
LL | asm!("", options(nostack));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:20:5
|
||||
|
|
||||
LL | global_asm!("");
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: Intel x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:22:5
|
||||
|
|
||||
LL | global_asm!("", options());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use AT&T x86 assembly syntax
|
||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:35:9
|
||||
|
|
||||
LL | asm!("", options(att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_att_syntax)]`
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:37:9
|
||||
|
|
||||
LL | asm!("", options(nostack, att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
|
||||
error: AT&T x86 assembly syntax used
|
||||
--> $DIR/asm_syntax_x86.rs:43:5
|
||||
|
|
||||
LL | global_asm!("", options(att_syntax));
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: use Intel x86 assembly syntax
|
||||
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
Loading…
Reference in a new issue