]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/diagnostic.rs
rollup merge of #20517: nikomatsakis/safety-issue-19997
[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;
17
18 use {ValueRef, TwineRef, DebugLocRef, DiagnosticInfoRef};
19
20 #[derive(Copy)]
21 pub enum OptimizationDiagnosticKind {
22     OptimizationRemark,
23     OptimizationMissed,
24     OptimizationAnalysis,
25     OptimizationFailure,
26 }
27
28 impl OptimizationDiagnosticKind {
29     pub fn describe(self) -> &'static str {
30         match self {
31             OptimizationRemark => "remark",
32             OptimizationMissed => "missed",
33             OptimizationAnalysis => "analysis",
34             OptimizationFailure => "failure",
35         }
36     }
37 }
38
39 pub struct OptimizationDiagnostic {
40     pub kind: OptimizationDiagnosticKind,
41     pub pass_name: *const c_char,
42     pub function: ValueRef,
43     pub debug_loc: DebugLocRef,
44     pub message: TwineRef,
45 }
46
47 impl Copy for OptimizationDiagnostic {}
48
49 impl OptimizationDiagnostic {
50     unsafe fn unpack(kind: OptimizationDiagnosticKind, di: DiagnosticInfoRef)
51             -> OptimizationDiagnostic {
52
53         let mut opt = OptimizationDiagnostic {
54             kind: kind,
55             pass_name: 0 as *const c_char,
56             function: 0 as ValueRef,
57             debug_loc: 0 as DebugLocRef,
58             message: 0 as TwineRef,
59         };
60
61         super::LLVMUnpackOptimizationDiagnostic(di,
62             &mut opt.pass_name,
63             &mut opt.function,
64             &mut opt.debug_loc,
65             &mut opt.message);
66
67         opt
68     }
69 }
70
71 #[derive(Copy)]
72 pub enum Diagnostic {
73     Optimization(OptimizationDiagnostic),
74
75     /// LLVM has other types that we do not wrap here.
76     UnknownDiagnostic(DiagnosticInfoRef),
77 }
78
79 impl Diagnostic {
80     pub unsafe fn unpack(di: DiagnosticInfoRef) -> Diagnostic {
81         let kind = super::LLVMGetDiagInfoKind(di);
82
83         match kind {
84             super::DK_OptimizationRemark
85                 => Optimization(OptimizationDiagnostic::unpack(OptimizationRemark, di)),
86
87             super::DK_OptimizationRemarkMissed
88                 => Optimization(OptimizationDiagnostic::unpack(OptimizationMissed, di)),
89
90             super::DK_OptimizationRemarkAnalysis
91                 => Optimization(OptimizationDiagnostic::unpack(OptimizationAnalysis, di)),
92
93             super::DK_OptimizationFailure
94                 => Optimization(OptimizationDiagnostic::unpack(OptimizationFailure, di)),
95
96             _ => UnknownDiagnostic(di)
97         }
98     }
99 }