]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/diagnostic.rs
Auto merge of #28938 - GlenDC:master, r=Manishearth
[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, Clone)]
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, Clone)]
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 #[derive(Copy, Clone)]
73 pub struct InlineAsmDiagnostic {
74     pub cookie: c_uint,
75     pub message: TwineRef,
76     pub instruction: ValueRef,
77 }
78
79 impl InlineAsmDiagnostic {
80     unsafe fn unpack(di: DiagnosticInfoRef)
81             -> InlineAsmDiagnostic {
82
83         let mut opt = InlineAsmDiagnostic {
84             cookie: 0,
85             message: ptr::null_mut(),
86             instruction: ptr::null_mut(),
87         };
88
89         super::LLVMUnpackInlineAsmDiagnostic(di,
90             &mut opt.cookie,
91             &mut opt.message,
92             &mut opt.instruction);
93
94         opt
95     }
96 }
97
98 #[derive(Copy, Clone)]
99 pub enum Diagnostic {
100     Optimization(OptimizationDiagnostic),
101     InlineAsm(InlineAsmDiagnostic),
102
103     /// LLVM has other types that we do not wrap here.
104     UnknownDiagnostic(DiagnosticInfoRef),
105 }
106
107 impl Diagnostic {
108     pub unsafe fn unpack(di: DiagnosticInfoRef) -> Diagnostic {
109         let kind = super::LLVMGetDiagInfoKind(di);
110
111         match kind {
112             super::DK_InlineAsm
113                 => InlineAsm(InlineAsmDiagnostic::unpack(di)),
114
115             super::DK_OptimizationRemark
116                 => Optimization(OptimizationDiagnostic::unpack(OptimizationRemark, di)),
117
118             super::DK_OptimizationRemarkMissed
119                 => Optimization(OptimizationDiagnostic::unpack(OptimizationMissed, di)),
120
121             super::DK_OptimizationRemarkAnalysis
122                 => Optimization(OptimizationDiagnostic::unpack(OptimizationAnalysis, di)),
123
124             super::DK_OptimizationFailure
125                 => Optimization(OptimizationDiagnostic::unpack(OptimizationFailure, di)),
126
127             _ => UnknownDiagnostic(di)
128         }
129     }
130 }