-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.rs
186 lines (167 loc) · 5.54 KB
/
main.rs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#![feature(box_syntax)]
#![feature(rustc_private)]
extern crate rustc;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate syntax;
use rustc::{declare_lint, lint_array};
use rustc::hir::*;
use rustc::hir::intravisit::FnKind;
use rustc::lint::*;
use rustc::mir;
use rustc::ty;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_driver::driver;
use syntax::ast::{Ident, NodeId};
use syntax::source_map::Span;
use std::collections::{HashMap, HashSet};
use if_chain::if_chain;
declare_lint! {
pub NO_INT_DIV,
Forbid,
"integer division can panic, use checked_div"
}
pub struct Pass;
impl LintPass for Pass {
fn get_lints(&self) -> LintArray {
lint_array!(NO_INT_DIV)
}
}
declare_lint! {
pub TRANSMUTE,
Forbid,
"the interns keep taking shortcuts that bite us later"
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if_chain! {
if let ExprKind::Binary(ref op, _, _) = expr.node;
if BinOpKind::Div == op.node;
then {
let ty = cx.tables.expr_ty(expr);
match ty.sty {
ty::Int(_) | ty::Uint(_) => {
cx.span_lint(
NO_INT_DIV,
expr.span,
"This might panic",
);
},
_ => {},
}
}
}
}
}
pub struct NoTransmute;
impl LintPass for NoTransmute {
fn get_lints(&self) -> LintArray {
lint_array!(TRANSMUTE)
}
}
impl EarlyLintPass for NoTransmute {
fn check_ident(&mut self, cx: &EarlyContext, ident: Ident) {
if ident.to_string().contains("transmute") {
cx.span_lint(
TRANSMUTE,
ident.span,
"no. No. NO. NOOOOOO!!!! Like seriously, doesn't anyone read our coding guidelines?",
);
}
}
}
declare_lint! {
pub STATE_MACHINE,
Forbid,
"ensures that the code upholds the state machine models"
}
pub struct StateMachine {
start: &'static str,
transitions: HashMap<&'static str, (&'static str, &'static str)>,
}
impl LintPass for StateMachine {
fn get_lints(&self) -> LintArray {
lint_array!(STATE_MACHINE)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StateMachine {
fn check_fn(
&mut self,
cx: &LateContext<'a, 'tcx>,
_: FnKind<'tcx>,
_: &'tcx FnDecl,
body: &'tcx Body,
_: Span,
_: NodeId,
) {
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
let mir = cx.tcx.optimized_mir(def_id);
let mut states: IndexVec<mir::BasicBlock, HashSet<&'static str>> =
IndexVec::from_elem(HashSet::new(), mir.basic_blocks());
states[mir::START_BLOCK].insert(self.start);
for (bb, bbdata) in mir.basic_blocks().iter_enumerated() {
if let mir::TerminatorKind::Call {
func,
destination: Some((_, succ)),
..
} = &bbdata.terminator().kind
{
let transition = self.transitions.iter().find(|(&transition, _)| {
match func.ty(&mir.local_decls, cx.tcx).sty {
ty::FnDef(did, _) => cx.tcx.item_name(did) == transition,
_ => false,
}
});
if let Some((transition, (start, end))) = transition {
if states[bb].contains(start) {
states[*succ].insert(end);
} else {
cx.span_lint(
STATE_MACHINE,
bbdata.terminator().source_info.span,
&format!(
"state transition `{}` not applicable for states {:?}",
transition, states[bb]
),
);
}
}
} else {
// the next block contains all states that the previous one did
let new = states[bb].clone();
for &succ in bbdata.terminator().successors() {
states[succ].extend(&new)
}
}
}
}
}
pub fn main() {
let args: Vec<_> = std::env::args().collect();
rustc_driver::run(move || {
let mut compiler = driver::CompileController::basic();
compiler.after_parse.callback = Box::new(move |state| {
let mut ls = state.session.lint_store.borrow_mut();
ls.register_early_pass(None, false, box NoTransmute);
ls.register_late_pass(None, false, box Pass);
let transitions = vec![
("begin_incoming_call", ("HangedUp", "Ringing")),
("accept_call", ("Ringing", "Calling")),
("end_call", ("Calling", "HangedUp")),
("begin_outgoing_call", ("HangedUp", "Dialing")),
("finished_dialing", ("Dialing", "Waiting")),
("call_accepted", ("Waiting", "Calling")),
];
let transitions = transitions.into_iter().collect();
ls.register_late_pass(
None,
false,
box StateMachine {
start: "HangedUp",
transitions,
},
);
});
rustc_driver::run_compiler(&args, Box::new(compiler), None, None)
});
}