]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/transform/no_landing_pads.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / librustc_mir / transform / no_landing_pads.rs
1 //! This pass removes the unwind branch of all the terminators when the no-landing-pads option is
2 //! specified.
3
4 use rustc::ty::TyCtxt;
5 use rustc::mir::*;
6 use rustc::mir::visit::MutVisitor;
7 use crate::transform::{MirPass, MirSource};
8
9 pub struct NoLandingPads<'tcx> {
10 tcx: TyCtxt<'tcx>,
11 }
12
13 impl<'tcx> NoLandingPads<'tcx> {
14 pub fn new(tcx: TyCtxt<'tcx>) -> Self {
15 NoLandingPads { tcx }
16 }
17 }
18
19 impl<'tcx> MirPass<'tcx> for NoLandingPads<'tcx> {
20 fn run_pass(&self, tcx: TyCtxt<'tcx>, _: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
21 no_landing_pads(tcx, body)
22 }
23 }
24
25 pub fn no_landing_pads<'tcx>(tcx: TyCtxt<'tcx>, body: &mut BodyAndCache<'tcx>) {
26 if tcx.sess.no_landing_pads() {
27 NoLandingPads::new(tcx).visit_body(body);
28 }
29 }
30
31 impl<'tcx> MutVisitor<'tcx> for NoLandingPads<'tcx> {
32 fn tcx(&self) -> TyCtxt<'tcx> {
33 self.tcx
34 }
35
36 fn visit_terminator_kind(&mut self,
37 kind: &mut TerminatorKind<'tcx>,
38 location: Location) {
39 if let Some(unwind) = kind.unwind_mut() {
40 unwind.take();
41 }
42 self.super_terminator_kind(kind, location);
43 }
44 }