Skip to content

Commit 9b5e4c6

Browse files
committed
Ensure ASM syntax detect global_asm! and asm! only on x86 architectures
1 parent 3b36b37 commit 9b5e4c6

File tree

6 files changed

+219
-57
lines changed

6 files changed

+219
-57
lines changed

clippy_lints/src/asm_syntax.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use std::fmt;
22

33
use clippy_utils::diagnostics::span_lint_and_help;
44
use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions};
5-
use rustc_lint::{EarlyContext, EarlyLintPass, Lint};
5+
use rustc_ast::{InlineAsm, Item, ItemKind};
6+
use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext};
67
use rustc_session::declare_lint_pass;
8+
use rustc_span::Span;
9+
use rustc_target::asm::InlineAsmArch;
710

811
#[derive(Clone, Copy, PartialEq, Eq)]
912
enum AsmStyle {
@@ -31,8 +34,14 @@ impl std::ops::Not for AsmStyle {
3134
}
3235
}
3336

34-
fn check_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr, check_for: AsmStyle) {
35-
if let ExprKind::InlineAsm(ref inline_asm) = expr.kind {
37+
fn check_asm_syntax(
38+
lint: &'static Lint,
39+
cx: &EarlyContext<'_>,
40+
inline_asm: &InlineAsm,
41+
span: Span,
42+
check_for: AsmStyle,
43+
) {
44+
if matches!(cx.sess().asm_arch, Some(InlineAsmArch::X86 | InlineAsmArch::X86_64)) {
3645
let style = if inline_asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
3746
AsmStyle::Att
3847
} else {
@@ -43,7 +52,7 @@ fn check_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr
4352
span_lint_and_help(
4453
cx,
4554
lint,
46-
expr.span,
55+
span,
4756
&format!("{style} x86 assembly syntax used"),
4857
None,
4958
&format!("use {} x86 assembly syntax", !style),
@@ -89,7 +98,15 @@ declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
8998

9099
impl EarlyLintPass for InlineAsmX86IntelSyntax {
91100
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
92-
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Intel);
101+
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
102+
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Intel);
103+
}
104+
}
105+
106+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
107+
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
108+
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Intel);
109+
}
93110
}
94111
}
95112

@@ -130,6 +147,14 @@ declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
130147

131148
impl EarlyLintPass for InlineAsmX86AttSyntax {
132149
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
133-
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Att);
150+
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
151+
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Att);
152+
}
153+
}
154+
155+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
156+
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
157+
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Att);
158+
}
134159
}
135160
}

tests/ui/asm_syntax.stderr

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/ui/asm_syntax_not_x86.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ignore-target-i686
2+
//@ignore-target-x86
3+
//@needs-asm-support
4+
5+
#[warn(clippy::inline_asm_x86_intel_syntax)]
6+
#[warn(clippy::inline_asm_x86_att_syntax)]
7+
mod dont_warn {
8+
use std::arch::{asm, global_asm};
9+
10+
pub(super) unsafe fn use_asm() {
11+
asm!("");
12+
asm!("", options());
13+
asm!("", options(nostack));
14+
}
15+
16+
global_asm!("");
17+
global_asm!("", options());
18+
}
19+
20+
fn main() {
21+
unsafe {
22+
dont_warn::use_asm();
23+
}
24+
}

tests/ui/asm_syntax_x86.i686.stderr

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
error: Intel x86 assembly syntax used
2+
--> $DIR/asm_syntax_x86.rs:10:9
3+
|
4+
LL | asm!("");
5+
| ^^^^^^^^
6+
|
7+
= help: use AT&T x86 assembly syntax
8+
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_intel_syntax)]`
10+
11+
error: Intel x86 assembly syntax used
12+
--> $DIR/asm_syntax_x86.rs:12:9
13+
|
14+
LL | asm!("", options());
15+
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= help: use AT&T x86 assembly syntax
18+
19+
error: Intel x86 assembly syntax used
20+
--> $DIR/asm_syntax_x86.rs:14:9
21+
|
22+
LL | asm!("", options(nostack));
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: use AT&T x86 assembly syntax
26+
27+
error: Intel x86 assembly syntax used
28+
--> $DIR/asm_syntax_x86.rs:20:5
29+
|
30+
LL | global_asm!("");
31+
| ^^^^^^^^^^^^^^^
32+
|
33+
= help: use AT&T x86 assembly syntax
34+
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
35+
36+
error: Intel x86 assembly syntax used
37+
--> $DIR/asm_syntax_x86.rs:22:5
38+
|
39+
LL | global_asm!("", options());
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
|
42+
= help: use AT&T x86 assembly syntax
43+
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
45+
error: AT&T x86 assembly syntax used
46+
--> $DIR/asm_syntax_x86.rs:35:9
47+
|
48+
LL | asm!("", options(att_syntax));
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
|
51+
= help: use Intel x86 assembly syntax
52+
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
53+
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_att_syntax)]`
54+
55+
error: AT&T x86 assembly syntax used
56+
--> $DIR/asm_syntax_x86.rs:37:9
57+
|
58+
LL | asm!("", options(nostack, att_syntax));
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
|
61+
= help: use Intel x86 assembly syntax
62+
63+
error: AT&T x86 assembly syntax used
64+
--> $DIR/asm_syntax_x86.rs:43:5
65+
|
66+
LL | global_asm!("", options(att_syntax));
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
|
69+
= help: use Intel x86 assembly syntax
70+
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
71+
72+
error: aborting due to 8 previous errors
73+

tests/ui/asm_syntax.rs renamed to tests/ui/asm_syntax_x86.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//@only-target-x86_64
2-
//@ignore-target-aarch64
1+
//@revisions: i686 x86_64
2+
//@[i686] only-target-i686
3+
//@[x86_64] only-target-x86_64
34

45
#[warn(clippy::inline_asm_x86_intel_syntax)]
56
mod warn_intel {
7+
use std::arch::{asm, global_asm};
8+
69
pub(super) unsafe fn use_asm() {
7-
use std::arch::asm;
810
asm!("");
911
//~^ ERROR: Intel x86 assembly syntax used
1012
asm!("", options());
@@ -14,12 +16,19 @@ mod warn_intel {
1416
asm!("", options(att_syntax));
1517
asm!("", options(nostack, att_syntax));
1618
}
19+
20+
global_asm!("");
21+
//~^ ERROR: Intel x86 assembly syntax used
22+
global_asm!("", options());
23+
//~^ ERROR: Intel x86 assembly syntax used
24+
global_asm!("", options(att_syntax));
1725
}
1826

1927
#[warn(clippy::inline_asm_x86_att_syntax)]
2028
mod warn_att {
29+
use std::arch::{asm, global_asm};
30+
2131
pub(super) unsafe fn use_asm() {
22-
use std::arch::asm;
2332
asm!("");
2433
asm!("", options());
2534
asm!("", options(nostack));
@@ -28,9 +37,13 @@ mod warn_att {
2837
asm!("", options(nostack, att_syntax));
2938
//~^ ERROR: AT&T x86 assembly syntax used
3039
}
40+
41+
global_asm!("");
42+
global_asm!("", options());
43+
global_asm!("", options(att_syntax));
44+
//~^ ERROR: AT&T x86 assembly syntax used
3145
}
3246

33-
#[cfg(target_arch = "x86_64")]
3447
fn main() {
3548
unsafe {
3649
warn_att::use_asm();

tests/ui/asm_syntax_x86.x86_64.stderr

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
error: Intel x86 assembly syntax used
2+
--> $DIR/asm_syntax_x86.rs:10:9
3+
|
4+
LL | asm!("");
5+
| ^^^^^^^^
6+
|
7+
= help: use AT&T x86 assembly syntax
8+
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_intel_syntax)]`
10+
11+
error: Intel x86 assembly syntax used
12+
--> $DIR/asm_syntax_x86.rs:12:9
13+
|
14+
LL | asm!("", options());
15+
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= help: use AT&T x86 assembly syntax
18+
19+
error: Intel x86 assembly syntax used
20+
--> $DIR/asm_syntax_x86.rs:14:9
21+
|
22+
LL | asm!("", options(nostack));
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: use AT&T x86 assembly syntax
26+
27+
error: Intel x86 assembly syntax used
28+
--> $DIR/asm_syntax_x86.rs:20:5
29+
|
30+
LL | global_asm!("");
31+
| ^^^^^^^^^^^^^^^
32+
|
33+
= help: use AT&T x86 assembly syntax
34+
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
35+
36+
error: Intel x86 assembly syntax used
37+
--> $DIR/asm_syntax_x86.rs:22:5
38+
|
39+
LL | global_asm!("", options());
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
|
42+
= help: use AT&T x86 assembly syntax
43+
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
45+
error: AT&T x86 assembly syntax used
46+
--> $DIR/asm_syntax_x86.rs:35:9
47+
|
48+
LL | asm!("", options(att_syntax));
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
|
51+
= help: use Intel x86 assembly syntax
52+
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
53+
= help: to override `-D warnings` add `#[allow(clippy::inline_asm_x86_att_syntax)]`
54+
55+
error: AT&T x86 assembly syntax used
56+
--> $DIR/asm_syntax_x86.rs:37:9
57+
|
58+
LL | asm!("", options(nostack, att_syntax));
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
|
61+
= help: use Intel x86 assembly syntax
62+
63+
error: AT&T x86 assembly syntax used
64+
--> $DIR/asm_syntax_x86.rs:43:5
65+
|
66+
LL | global_asm!("", options(att_syntax));
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
|
69+
= help: use Intel x86 assembly syntax
70+
= note: this error originates in the macro `global_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
71+
72+
error: aborting due to 8 previous errors
73+

0 commit comments

Comments
 (0)