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