Skip to content

Commit abce9e7

Browse files
committed
Auto merge of rust-lang#6092 - jethrogb:jb/inline-asm-syntax-lint, r=ebroto
Add lint for inline assembly syntax style preference changelog: Add lint for inline assembly syntax style preference
2 parents 99483be + 507561e commit abce9e7

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,8 @@ Released 2018-09-13
16351635
[`inherent_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string
16361636
[`inherent_to_string_shadow_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display
16371637
[`inline_always`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_always
1638+
[`inline_asm_x86_att_syntax`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_asm_x86_att_syntax
1639+
[`inline_asm_x86_intel_syntax`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_asm_x86_intel_syntax
16381640
[`inline_fn_without_body`]: https://rust-lang.github.io/rust-clippy/master/index.html#inline_fn_without_body
16391641
[`int_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#int_plus_one
16401642
[`integer_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic

clippy_lints/src/asm_syntax.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use std::fmt;
2+
3+
use crate::utils::span_lint_and_help;
4+
use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions};
5+
use rustc_lint::{EarlyContext, EarlyLintPass, Lint};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
8+
#[derive(Clone, Copy, PartialEq, Eq)]
9+
enum AsmStyle {
10+
Intel,
11+
Att,
12+
}
13+
14+
impl fmt::Display for AsmStyle {
15+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16+
match self {
17+
AsmStyle::Intel => f.write_str("Intel"),
18+
AsmStyle::Att => f.write_str("AT&T"),
19+
}
20+
}
21+
}
22+
23+
impl std::ops::Not for AsmStyle {
24+
type Output = AsmStyle;
25+
26+
fn not(self) -> AsmStyle {
27+
match self {
28+
AsmStyle::Intel => AsmStyle::Att,
29+
AsmStyle::Att => AsmStyle::Intel,
30+
}
31+
}
32+
}
33+
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 {
36+
let style = if inline_asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
37+
AsmStyle::Att
38+
} else {
39+
AsmStyle::Intel
40+
};
41+
42+
if style == check_for {
43+
span_lint_and_help(
44+
cx,
45+
lint,
46+
expr.span,
47+
&format!("{} x86 assembly syntax used", style),
48+
None,
49+
&format!("use {} x86 assembly syntax", !style),
50+
);
51+
}
52+
}
53+
}
54+
55+
declare_clippy_lint! {
56+
/// **What it does:** Checks for usage of Intel x86 assembly syntax.
57+
///
58+
/// **Why is this bad?** The lint has been enabled to indicate a preference
59+
/// for AT&T x86 assembly syntax.
60+
///
61+
/// **Known problems:** None.
62+
///
63+
/// **Example:**
64+
///
65+
/// ```rust,no_run
66+
/// # #![feature(asm)]
67+
/// # unsafe { let ptr = "".as_ptr();
68+
/// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr);
69+
/// # }
70+
/// ```
71+
/// Use instead:
72+
/// ```rust,no_run
73+
/// # #![feature(asm)]
74+
/// # unsafe { let ptr = "".as_ptr();
75+
/// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax));
76+
/// # }
77+
/// ```
78+
pub INLINE_ASM_X86_INTEL_SYNTAX,
79+
restriction,
80+
"prefer AT&T x86 assembly syntax"
81+
}
82+
83+
declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
84+
85+
impl EarlyLintPass for InlineAsmX86IntelSyntax {
86+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
87+
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Intel);
88+
}
89+
}
90+
91+
declare_clippy_lint! {
92+
/// **What it does:** Checks for usage of AT&T x86 assembly syntax.
93+
///
94+
/// **Why is this bad?** The lint has been enabled to indicate a preference
95+
/// for Intel x86 assembly syntax.
96+
///
97+
/// **Known problems:** None.
98+
///
99+
/// **Example:**
100+
///
101+
/// ```rust,no_run
102+
/// # #![feature(asm)]
103+
/// # unsafe { let ptr = "".as_ptr();
104+
/// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax));
105+
/// # }
106+
/// ```
107+
/// Use instead:
108+
/// ```rust,no_run
109+
/// # #![feature(asm)]
110+
/// # unsafe { let ptr = "".as_ptr();
111+
/// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr);
112+
/// # }
113+
/// ```
114+
pub INLINE_ASM_X86_ATT_SYNTAX,
115+
restriction,
116+
"prefer Intel x86 assembly syntax"
117+
}
118+
119+
declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
120+
121+
impl EarlyLintPass for InlineAsmX86AttSyntax {
122+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
123+
check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Att);
124+
}
125+
}

clippy_lints/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ mod utils;
153153
mod approx_const;
154154
mod arithmetic;
155155
mod as_conversions;
156+
mod asm_syntax;
156157
mod assertions_on_constants;
157158
mod assign_ops;
158159
mod async_yields_async;
@@ -487,6 +488,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
487488
&arithmetic::FLOAT_ARITHMETIC,
488489
&arithmetic::INTEGER_ARITHMETIC,
489490
&as_conversions::AS_CONVERSIONS,
491+
&asm_syntax::INLINE_ASM_X86_ATT_SYNTAX,
492+
&asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX,
490493
&assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
491494
&assign_ops::ASSIGN_OP_PATTERN,
492495
&assign_ops::MISREFACTORED_ASSIGN_OP,
@@ -1123,12 +1126,16 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11231126
store.register_late_pass(|| box utils::internal_lints::MatchTypeOnDiagItem);
11241127
let disallowed_methods = conf.disallowed_methods.iter().cloned().collect::<FxHashSet<_>>();
11251128
store.register_late_pass(move || box disallowed_method::DisallowedMethod::new(&disallowed_methods));
1129+
store.register_early_pass(|| box asm_syntax::InlineAsmX86AttSyntax);
1130+
store.register_early_pass(|| box asm_syntax::InlineAsmX86IntelSyntax);
11261131

11271132

11281133
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
11291134
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
11301135
LintId::of(&arithmetic::INTEGER_ARITHMETIC),
11311136
LintId::of(&as_conversions::AS_CONVERSIONS),
1137+
LintId::of(&asm_syntax::INLINE_ASM_X86_ATT_SYNTAX),
1138+
LintId::of(&asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX),
11321139
LintId::of(&create_dir::CREATE_DIR),
11331140
LintId::of(&dbg_macro::DBG_MACRO),
11341141
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),

src/lintlist/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,20 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
899899
deprecation: None,
900900
module: "attrs",
901901
},
902+
Lint {
903+
name: "inline_asm_x86_att_syntax",
904+
group: "restriction",
905+
desc: "prefer Intel x86 assembly syntax",
906+
deprecation: None,
907+
module: "asm_syntax",
908+
},
909+
Lint {
910+
name: "inline_asm_x86_intel_syntax",
911+
group: "restriction",
912+
desc: "prefer AT&T x86 assembly syntax",
913+
deprecation: None,
914+
module: "asm_syntax",
915+
},
902916
Lint {
903917
name: "inline_fn_without_body",
904918
group: "correctness",

tests/ui/asm_syntax.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![feature(asm)]
2+
// only-x86_64
3+
4+
#[warn(clippy::inline_asm_x86_intel_syntax)]
5+
mod warn_intel {
6+
pub(super) unsafe fn use_asm() {
7+
asm!("");
8+
asm!("", options());
9+
asm!("", options(nostack));
10+
asm!("", options(att_syntax));
11+
asm!("", options(nostack, att_syntax));
12+
}
13+
}
14+
15+
#[warn(clippy::inline_asm_x86_att_syntax)]
16+
mod warn_att {
17+
pub(super) unsafe fn use_asm() {
18+
asm!("");
19+
asm!("", options());
20+
asm!("", options(nostack));
21+
asm!("", options(att_syntax));
22+
asm!("", options(nostack, att_syntax));
23+
}
24+
}
25+
26+
fn main() {
27+
unsafe {
28+
warn_att::use_asm();
29+
warn_intel::use_asm();
30+
}
31+
}

tests/ui/asm_syntax.stderr

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error: Intel x86 assembly syntax used
2+
--> $DIR/asm_syntax.rs:7:9
3+
|
4+
LL | asm!("");
5+
| ^^^^^^^^^
6+
|
7+
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
8+
= help: use AT&T x86 assembly syntax
9+
10+
error: Intel x86 assembly syntax used
11+
--> $DIR/asm_syntax.rs:8:9
12+
|
13+
LL | asm!("", options());
14+
| ^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: use AT&T x86 assembly syntax
17+
18+
error: Intel x86 assembly syntax used
19+
--> $DIR/asm_syntax.rs:9:9
20+
|
21+
LL | asm!("", options(nostack));
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: use AT&T x86 assembly syntax
25+
26+
error: AT&T x86 assembly syntax used
27+
--> $DIR/asm_syntax.rs:21:9
28+
|
29+
LL | asm!("", options(att_syntax));
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
33+
= help: use Intel x86 assembly syntax
34+
35+
error: AT&T x86 assembly syntax used
36+
--> $DIR/asm_syntax.rs:22:9
37+
|
38+
LL | asm!("", options(nostack, att_syntax));
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
= help: use Intel x86 assembly syntax
42+
43+
error: aborting due to 5 previous errors
44+

0 commit comments

Comments
 (0)