-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lint for inline assembly syntax style preference
- Loading branch information
Jethro Beekman
committed
Sep 28, 2020
1 parent
db6fb90
commit 26150a9
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use std::fmt; | ||
|
||
use crate::utils::span_lint_and_help; | ||
use rustc_ast::{ | ||
ast::MacCall, | ||
token::{Token, TokenKind}, | ||
tokenstream::{self, TokenTree}, | ||
}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, Lint}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::Span; | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq)] | ||
enum AsmStyle { | ||
Intel, | ||
Att, | ||
} | ||
|
||
impl fmt::Display for AsmStyle { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
AsmStyle::Intel => f.write_str("Intel"), | ||
AsmStyle::Att => f.write_str("AT&T"), | ||
} | ||
} | ||
} | ||
|
||
impl std::ops::Not for AsmStyle { | ||
type Output = AsmStyle; | ||
|
||
fn not(self) -> AsmStyle { | ||
match self { | ||
AsmStyle::Intel => AsmStyle::Att, | ||
AsmStyle::Att => AsmStyle::Intel, | ||
} | ||
} | ||
} | ||
|
||
fn check_mac_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, mac: &MacCall, check_for: AsmStyle) { | ||
/// Find the `options(...)` specified in an `asm!` macro call, if any | ||
fn find_options_tts(mac: &MacCall) -> Option<(Span, tokenstream::Cursor)> { | ||
let mut cursor = mac.args.inner_tokens().trees().fuse(); | ||
for tt in &mut cursor { | ||
match tt { | ||
TokenTree::Token(Token { | ||
kind: TokenKind::Ident(ident, _), | ||
span: ident_span, | ||
}) if ident == sym!(options) => { | ||
if let TokenTree::Delimited(tts_span, _, stream) = cursor.next()? { | ||
return Some((ident_span.to(tts_span.close), stream.trees())); | ||
} else { | ||
return None; | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Find the `att_syntax` specified in an `asm!` macro call options, if any | ||
fn find_att_syntax_ident(cursor: tokenstream::Cursor) -> Option<Span> { | ||
for tt in cursor { | ||
match tt { | ||
TokenTree::Token(Token { | ||
kind: TokenKind::Ident(ident, _), | ||
span, | ||
}) if ident == sym!(att_syntax) => return Some(span), | ||
_ => {}, | ||
} | ||
} | ||
None | ||
} | ||
|
||
if mac.path == sym!(asm) { | ||
let (style, relevant_span) = match find_options_tts(mac) { | ||
Some((opt_span, options)) => match find_att_syntax_ident(options) { | ||
Some(att_span) => (AsmStyle::Att, att_span), | ||
None => (AsmStyle::Intel, opt_span), | ||
}, | ||
None => (AsmStyle::Intel, mac.span()), | ||
}; | ||
|
||
if style == check_for { | ||
span_lint_and_help( | ||
cx, | ||
lint, | ||
relevant_span, | ||
&format!("{} x86 assembly syntax used", style), | ||
None, | ||
&format!("use {} x86 assembly syntax", !style), | ||
); | ||
} | ||
} | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for usage of Intel x86 assembly syntax. | ||
/// | ||
/// **Why is this bad?** The lint has been enabled to indicate a preference | ||
/// for AT&T x86 assembly syntax. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax)); | ||
/// ``` | ||
pub INLINE_ASM_X86_INTEL_SYNTAX, | ||
restriction, | ||
"prefer AT&T x86 assembly syntax" | ||
} | ||
|
||
declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]); | ||
|
||
impl EarlyLintPass for InlineAsmX86IntelSyntax { | ||
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) { | ||
check_mac_asm_syntax(Self::get_lints()[0], cx, mac, AsmStyle::Intel); | ||
} | ||
} | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for usage of AT&T x86 assembly syntax. | ||
/// | ||
/// **Why is this bad?** The lint has been enabled to indicate a preference | ||
/// for Intel x86 assembly syntax. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax)); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr); | ||
/// ``` | ||
pub INLINE_ASM_X86_ATT_SYNTAX, | ||
restriction, | ||
"prefer Intel x86 assembly syntax" | ||
} | ||
|
||
declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]); | ||
|
||
impl EarlyLintPass for InlineAsmX86AttSyntax { | ||
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) { | ||
check_mac_asm_syntax(Self::get_lints()[0], cx, mac, AsmStyle::Att); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![feature(asm)] | ||
|
||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] | ||
#[warn(clippy::inline_asm_x86_intel_syntax)] | ||
mod warn_intel { | ||
pub(super) unsafe fn use_asm() { | ||
asm!(""); | ||
asm!("", options()); | ||
asm!("", options(att_syntax)); | ||
} | ||
} | ||
|
||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] | ||
#[warn(clippy::inline_asm_x86_att_syntax)] | ||
mod warn_att { | ||
pub(super) unsafe fn use_asm() { | ||
asm!(""); | ||
asm!("", options()); | ||
asm!("", options(att_syntax)); | ||
} | ||
} | ||
|
||
fn main() { | ||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] | ||
unsafe { | ||
warn_att::use_asm(); | ||
warn_intel::use_asm(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: Intel x86 assembly syntax used | ||
--> $DIR/asm_syntax.rs:7:9 | ||
| | ||
LL | asm!(""); | ||
| ^^^^^^^^ | ||
| | ||
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings` | ||
= help: use AT&T x86 assembly syntax | ||
|
||
error: Intel x86 assembly syntax used | ||
--> $DIR/asm_syntax.rs:8:18 | ||
| | ||
LL | asm!("", options()); | ||
| ^^^^^^^^^ | ||
| | ||
= help: use AT&T x86 assembly syntax | ||
|
||
error: AT&T x86 assembly syntax used | ||
--> $DIR/asm_syntax.rs:19:26 | ||
| | ||
LL | asm!("", options(att_syntax)); | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings` | ||
= help: use Intel x86 assembly syntax | ||
|
||
error: aborting due to 3 previous errors | ||
|