about summary refs log tree commit diff
path: root/clippy_lints/src/asm_syntax.rs
blob: b970c71b753cec24a83c6cc20f65918cab12d0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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_session::{declare_lint_pass, declare_tool_lint};

#[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_expr_asm_syntax(lint: &'static Lint, cx: &EarlyContext<'_>, expr: &Expr, check_for: AsmStyle) {
    if let ExprKind::InlineAsm(ref inline_asm) = expr.kind {
        let style = if inline_asm.options.contains(InlineAsmOptions::ATT_SYNTAX) {
            AsmStyle::Att
        } else {
            AsmStyle::Intel
        };

        if style == check_for {
            span_lint_and_help(
                cx,
                lint,
                expr.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,no_run
    /// # #![feature(asm)]
    /// # unsafe { let ptr = "".as_ptr();
    /// asm!("lea {}, [{}]", lateout(reg) _, in(reg) ptr);
    /// # }
    /// ```
    /// Use instead:
    /// ```rust,no_run
    /// # #![feature(asm)]
    /// # unsafe { let ptr = "".as_ptr();
    /// 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_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
        check_expr_asm_syntax(Self::get_lints()[0], cx, expr, 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,no_run
    /// # #![feature(asm)]
    /// # unsafe { let ptr = "".as_ptr();
    /// asm!("lea ({}), {}", in(reg) ptr, lateout(reg) _, options(att_syntax));
    /// # }
    /// ```
    /// Use instead:
    /// ```rust,no_run
    /// # #![feature(asm)]
    /// # unsafe { let ptr = "".as_ptr();
    /// 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_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
        check_expr_asm_syntax(Self::get_lints()[0], cx, expr, AsmStyle::Att);
    }
}