llvm_plugin_inkwell/support/
error_handling.rs1use libc::c_void;
4use llvm_sys::core::{LLVMGetDiagInfoDescription, LLVMGetDiagInfoSeverity};
5use llvm_sys::error_handling::{LLVMInstallFatalErrorHandler, LLVMResetFatalErrorHandler};
6use llvm_sys::prelude::LLVMDiagnosticInfoRef;
7use llvm_sys::LLVMDiagnosticSeverity;
8
9pub unsafe fn install_fatal_error_handler(handler: extern "C" fn(*const ::libc::c_char)) {
23 LLVMInstallFatalErrorHandler(Some(handler))
24}
25
26pub fn reset_fatal_error_handler() {
28 unsafe { LLVMResetFatalErrorHandler() }
29}
30
31pub(crate) struct DiagnosticInfo {
32 diagnostic_info: LLVMDiagnosticInfoRef,
33}
34
35impl DiagnosticInfo {
36 pub unsafe fn new(diagnostic_info: LLVMDiagnosticInfoRef) -> Self {
37 DiagnosticInfo { diagnostic_info }
38 }
39
40 pub(crate) fn get_description(&self) -> *mut ::libc::c_char {
41 unsafe { LLVMGetDiagInfoDescription(self.diagnostic_info) }
42 }
43
44 pub(crate) fn severity_is_error(&self) -> bool {
45 unsafe {
46 match LLVMGetDiagInfoSeverity(self.diagnostic_info) {
47 LLVMDiagnosticSeverity::LLVMDSError => true,
48 _ => false,
49 }
50 }
51 }
52}
53
54pub(crate) extern "C" fn get_error_str_diagnostic_handler(
60 diagnostic_info: LLVMDiagnosticInfoRef,
61 void_ptr: *mut c_void,
62) {
63 let diagnostic_info = unsafe { DiagnosticInfo::new(diagnostic_info) };
64
65 if diagnostic_info.severity_is_error() {
66 let c_ptr_ptr = void_ptr as *mut *mut c_void as *mut *mut ::libc::c_char;
67
68 unsafe {
69 *c_ptr_ptr = diagnostic_info.get_description();
70 }
71 }
72}