Skip to content

Commit

Permalink
Move diagnostic handler to a separate struct
Browse files Browse the repository at this point in the history
Add the `DiagnosticHandler` struct which implements `LLVMDiagnosticHandler`,
so only that struct and not the whole `Linker` is passed to
`LLVMContextSetDiagnosticHandler`.

This is going to make writing a safe wrapper for `Context` easier.
  • Loading branch information
vadorovsky committed Dec 1, 2024
1 parent 621748f commit 5a46e29
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub struct Linker {
context: LLVMContextRef,
module: LLVMModuleRef,
target_machine: LLVMTargetMachineRef,
has_errors: bool,
diagnostic_handler: DiagnosticHandler,
}

impl Linker {
Expand All @@ -240,7 +240,7 @@ impl Linker {
context: ptr::null_mut(),
module: ptr::null_mut(),
target_machine: ptr::null_mut(),
has_errors: false,
diagnostic_handler: DiagnosticHandler::new(),
}
}

Expand Down Expand Up @@ -270,7 +270,7 @@ impl Linker {
}

pub fn has_errors(&self) -> bool {
self.has_errors
self.diagnostic_handler.has_errors
}

fn link_modules(&mut self) -> Result<(), LinkerError> {
Expand Down Expand Up @@ -537,8 +537,8 @@ impl Linker {
self.context = LLVMContextCreate();
LLVMContextSetDiagnosticHandler(
self.context,
Some(llvm::diagnostic_handler::<Self>),
self as *mut _ as _,
Some(llvm::diagnostic_handler::<DiagnosticHandler>),
&mut self.diagnostic_handler as *mut _ as _,
);
LLVMInstallFatalErrorHandler(Some(llvm::fatal_error));
LLVMEnablePrettyStackTrace();
Expand All @@ -551,7 +551,39 @@ impl Linker {
}
}

impl llvm::LLVMDiagnosticHandler for Linker {
impl Drop for Linker {
fn drop(&mut self) {
unsafe {
if !self.target_machine.is_null() {
LLVMDisposeTargetMachine(self.target_machine);
}
if !self.module.is_null() {
LLVMDisposeModule(self.module);
}
if !self.context.is_null() {
LLVMContextDispose(self.context);
}
}
}
}

pub struct DiagnosticHandler {
pub(crate) has_errors: bool,
}

impl Default for DiagnosticHandler {
fn default() -> Self {
Self::new()
}
}

impl DiagnosticHandler {
pub fn new() -> Self {
Self { has_errors: false }
}
}

impl llvm::LLVMDiagnosticHandler for DiagnosticHandler {
fn handle_diagnostic(&mut self, severity: llvm_sys::LLVMDiagnosticSeverity, message: &str) {
// TODO(https://reviews.llvm.org/D155894): Remove this when LLVM no longer emits these
// errors.
Expand Down Expand Up @@ -582,22 +614,6 @@ impl llvm::LLVMDiagnosticHandler for Linker {
}
}

impl Drop for Linker {
fn drop(&mut self) {
unsafe {
if !self.target_machine.is_null() {
LLVMDisposeTargetMachine(self.target_machine);
}
if !self.module.is_null() {
LLVMDisposeModule(self.module);
}
if !self.context.is_null() {
LLVMContextDispose(self.context);
}
}
}
}

fn detect_input_type(data: &[u8]) -> Option<InputType> {
if data.len() < 8 {
return None;
Expand Down

0 comments on commit 5a46e29

Please sign in to comment.