]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/diagnostic.rs
Auto merge of #21692 - pnkfelix:fsk-fix-coerce-match-20055, r=eddyb
[rust.git] / src / librustc_llvm / diagnostic.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! LLVM diagnostic reports.
12
13 pub use self::OptimizationDiagnosticKind::*;
14 pub use self::Diagnostic::*;
15
16 use libc::{c_char, c_uint};
17 use std::ptr;
18
19 use {ValueRef, TwineRef, DebugLocRef, DiagnosticInfoRef};
20
21 #[derive(Copy)]
22 pub enum OptimizationDiagnosticKind {
23     OptimizationRemark,
24     OptimizationMissed,
25     OptimizationAnalysis,
26     OptimizationFailure,
27 }
28
29 impl OptimizationDiagnosticKind {
30     pub fn describe(self) -> &'static str {
31         match self {
32             OptimizationRemark => "remark",
33             OptimizationMissed => "missed",
34             OptimizationAnalysis => "analysis",
35             OptimizationFailure => "failure",
36         }
37     }
38 }
39
40 #[allow(raw_pointer_derive)]
41 #[derive(Copy)]
42 pub struct OptimizationDiagnostic {
43     pub kind: OptimizationDiagnosticKind,
44     pub pass_name: *const c_char,
45     pub function: ValueRef,
46     pub debug_loc: DebugLocRef,
47     pub message: TwineRef,
48 }
49
50 impl OptimizationDiagnostic {
51     unsafe fn unpack(kind: OptimizationDiagnosticKind, di: DiagnosticInfoRef)
52             -> OptimizationDiagnostic {
53
54         let mut opt = OptimizationDiagnostic {
55             kind: kind,
56             pass_name: ptr::null(),
57             function: ptr::null_mut(),
58             debug_loc: ptr::null_mut(),
59             message: ptr::null_mut(),
60         };
61
62         super::LLVMUnpackOptimizationDiagnostic(di,
63             &mut opt.pass_name,
64             &mut opt.function,
65             &mut opt.debug_loc,
66             &mut opt.message);
67
68         opt
69     }
70 }
71
72 pub struct InlineAsmDiagnostic {
73     pub cookie: c_uint,
74     pub message: TwineRef,
75     pub instruction: ValueRef,
76 }
77
78 impl Copy for InlineAsmDiagnostic {}
79
80 impl InlineAsmDiagnostic {
81     unsafe fn unpack(di: DiagnosticInfoRef)
82             -> InlineAsmDiagnostic {
83
84         let mut opt = InlineAsmDiagnostic {
85             cookie: 0,
86             message: ptr::null_mut(),
87             instruction: ptr::null_mut(),
88         };
89
90         super::LLVMUnpackInlineAsmDiagnostic(di,
91             &mut opt.cookie,
92             &mut opt.message,
93             &mut opt.instruction);
94
95         opt
96     }
97 }
98
99 #[derive(Copy)]
100 pub enum Diagnostic {
101     Optimization(OptimizationDiagnostic),
102     InlineAsm(InlineAsmDiagnostic),
103
104     /// LLVM has other types that we do not wrap here.
105     UnknownDiagnostic(DiagnosticInfoRef),
106 }
107
108 impl Diagnostic {
109     pub unsafe fn unpack(di: DiagnosticInfoRef) -> Diagnostic {
110         let kind = super::LLVMGetDiagInfoKind(di);
111
112         match kind {
113             super::DK_InlineAsm
114                 => InlineAsm(InlineAsmDiagnostic::unpack(di)),
115
116             super::DK_OptimizationRemark
117                 => Optimization(OptimizationDiagnostic::unpack(OptimizationRemark, di)),
118
119             super::DK_OptimizationRemarkMissed
120                 => Optimization(OptimizationDiagnostic::unpack(OptimizationMissed, di)),
121
122             super::DK_OptimizationRemarkAnalysis
123                 => Optimization(OptimizationDiagnostic::unpack(OptimizationAnalysis, di)),
124
125             super::DK_OptimizationFailure
126                 => Optimization(OptimizationDiagnostic::unpack(OptimizationFailure, di)),
127
128             _ => UnknownDiagnostic(di)
129         }
130     }
131 }