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 self.severity() == LLVMDiagnosticSeverity::LLVMDSError
46 }
47
48 fn severity(&self) -> LLVMDiagnosticSeverity {
49 unsafe { LLVMGetDiagInfoSeverity(self.diagnostic_info) }
50 }
51}
52
53pub(crate) extern "C" fn get_error_str_diagnostic_handler(
59 diagnostic_info: LLVMDiagnosticInfoRef,
60 void_ptr: *mut c_void,
61) {
62 let diagnostic_info = unsafe { DiagnosticInfo::new(diagnostic_info) };
63
64 if diagnostic_info.severity_is_error() {
65 let c_ptr_ptr = void_ptr as *mut *mut c_void as *mut *mut ::libc::c_char;
66
67 unsafe {
68 *c_ptr_ptr = diagnostic_info.get_description();
69 }
70 }
71}