revmc_llvm/
dh.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use inkwell::{
    context::{AsContextRef, Context},
    llvm_sys::{core::*, prelude::*, LLVMDiagnosticHandler, LLVMDiagnosticSeverity::*},
};
use std::{ffi::c_void, fmt, ptr};

/// LLVM diagnostic handler guard.
pub(crate) struct DiagnosticHandlerGuard<'ctx> {
    cx: &'ctx Context,
    prev_dh: LLVMDiagnosticHandler,
    prev_dhc: *mut c_void,
}

impl fmt::Debug for DiagnosticHandlerGuard<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DiagnosticHandlerGuard").finish_non_exhaustive()
    }
}

impl<'ctx> DiagnosticHandlerGuard<'ctx> {
    pub(crate) fn new(cx: &'ctx Context) -> Self {
        unsafe {
            let c = cx.as_ctx_ref();
            let prev_dh = LLVMContextGetDiagnosticHandler(c);
            let prev_dhc = LLVMContextGetDiagnosticContext(c);
            LLVMContextSetDiagnosticHandler(c, Some(Self::diagnostic_handler), ptr::null_mut());
            Self { cx, prev_dh, prev_dhc }
        }
    }

    extern "C" fn diagnostic_handler(di: LLVMDiagnosticInfoRef, _context: *mut c_void) {
        unsafe {
            // `LLVMGetDiagInfoDescription` returns an LLVM `Message`.
            let msg_cstr = crate::llvm_string(LLVMGetDiagInfoDescription(di));
            let msg = msg_cstr.to_string_lossy();
            match LLVMGetDiagInfoSeverity(di) {
                LLVMDSError => error!(target: "llvm", "{msg}"),
                LLVMDSWarning => warn!(target: "llvm", "{msg}"),
                LLVMDSRemark => trace!(target: "llvm", "{msg}"),
                LLVMDSNote => debug!(target: "llvm", "{msg}"),
            }
        }
    }
}

impl Drop for DiagnosticHandlerGuard<'_> {
    fn drop(&mut self) {
        unsafe {
            LLVMContextSetDiagnosticHandler(self.cx.as_ctx_ref(), self.prev_dh, self.prev_dhc);
        }
    }
}