Skip to content

Commit 5a46e29

Browse files
committed
Move diagnostic handler to a separate struct
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.
1 parent 621748f commit 5a46e29

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

src/linker.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub struct Linker {
229229
context: LLVMContextRef,
230230
module: LLVMModuleRef,
231231
target_machine: LLVMTargetMachineRef,
232-
has_errors: bool,
232+
diagnostic_handler: DiagnosticHandler,
233233
}
234234

235235
impl Linker {
@@ -240,7 +240,7 @@ impl Linker {
240240
context: ptr::null_mut(),
241241
module: ptr::null_mut(),
242242
target_machine: ptr::null_mut(),
243-
has_errors: false,
243+
diagnostic_handler: DiagnosticHandler::new(),
244244
}
245245
}
246246

@@ -270,7 +270,7 @@ impl Linker {
270270
}
271271

272272
pub fn has_errors(&self) -> bool {
273-
self.has_errors
273+
self.diagnostic_handler.has_errors
274274
}
275275

276276
fn link_modules(&mut self) -> Result<(), LinkerError> {
@@ -537,8 +537,8 @@ impl Linker {
537537
self.context = LLVMContextCreate();
538538
LLVMContextSetDiagnosticHandler(
539539
self.context,
540-
Some(llvm::diagnostic_handler::<Self>),
541-
self as *mut _ as _,
540+
Some(llvm::diagnostic_handler::<DiagnosticHandler>),
541+
&mut self.diagnostic_handler as *mut _ as _,
542542
);
543543
LLVMInstallFatalErrorHandler(Some(llvm::fatal_error));
544544
LLVMEnablePrettyStackTrace();
@@ -551,7 +551,39 @@ impl Linker {
551551
}
552552
}
553553

554-
impl llvm::LLVMDiagnosticHandler for Linker {
554+
impl Drop for Linker {
555+
fn drop(&mut self) {
556+
unsafe {
557+
if !self.target_machine.is_null() {
558+
LLVMDisposeTargetMachine(self.target_machine);
559+
}
560+
if !self.module.is_null() {
561+
LLVMDisposeModule(self.module);
562+
}
563+
if !self.context.is_null() {
564+
LLVMContextDispose(self.context);
565+
}
566+
}
567+
}
568+
}
569+
570+
pub struct DiagnosticHandler {
571+
pub(crate) has_errors: bool,
572+
}
573+
574+
impl Default for DiagnosticHandler {
575+
fn default() -> Self {
576+
Self::new()
577+
}
578+
}
579+
580+
impl DiagnosticHandler {
581+
pub fn new() -> Self {
582+
Self { has_errors: false }
583+
}
584+
}
585+
586+
impl llvm::LLVMDiagnosticHandler for DiagnosticHandler {
555587
fn handle_diagnostic(&mut self, severity: llvm_sys::LLVMDiagnosticSeverity, message: &str) {
556588
// TODO(https://reviews.llvm.org/D155894): Remove this when LLVM no longer emits these
557589
// errors.
@@ -582,22 +614,6 @@ impl llvm::LLVMDiagnosticHandler for Linker {
582614
}
583615
}
584616

585-
impl Drop for Linker {
586-
fn drop(&mut self) {
587-
unsafe {
588-
if !self.target_machine.is_null() {
589-
LLVMDisposeTargetMachine(self.target_machine);
590-
}
591-
if !self.module.is_null() {
592-
LLVMDisposeModule(self.module);
593-
}
594-
if !self.context.is_null() {
595-
LLVMContextDispose(self.context);
596-
}
597-
}
598-
}
599-
}
600-
601617
fn detect_input_type(data: &[u8]) -> Option<InputType> {
602618
if data.len() < 8 {
603619
return None;

0 commit comments

Comments
 (0)