Skip to content

Commit c9a7d33

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 91d8412 commit c9a7d33

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/linker.rs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub struct Linker {
233233
context: LLVMContextRef,
234234
module: LLVMModuleRef,
235235
target_machine: LLVMTargetMachineRef,
236-
has_errors: bool,
236+
diagnostic_handler: DiagnosticHandler,
237237
}
238238

239239
impl Linker {
@@ -244,7 +244,7 @@ impl Linker {
244244
context: ptr::null_mut(),
245245
module: ptr::null_mut(),
246246
target_machine: ptr::null_mut(),
247-
has_errors: false,
247+
diagnostic_handler: DiagnosticHandler::new(),
248248
}
249249
}
250250

@@ -274,7 +274,7 @@ impl Linker {
274274
}
275275

276276
pub fn has_errors(&self) -> bool {
277-
self.has_errors
277+
self.diagnostic_handler.has_errors
278278
}
279279

280280
fn link_modules(&mut self) -> Result<(), LinkerError> {
@@ -541,8 +541,8 @@ impl Linker {
541541
self.context = LLVMContextCreate();
542542
LLVMContextSetDiagnosticHandler(
543543
self.context,
544-
Some(llvm::diagnostic_handler::<Self>),
545-
self as *mut _ as _,
544+
Some(llvm::diagnostic_handler::<DiagnosticHandler>),
545+
&mut self.diagnostic_handler as *mut _ as _,
546546
);
547547
LLVMInstallFatalErrorHandler(Some(llvm::fatal_error));
548548
LLVMEnablePrettyStackTrace();
@@ -555,7 +555,33 @@ impl Linker {
555555
}
556556
}
557557

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

589-
impl Drop for Linker {
590-
fn drop(&mut self) {
591-
unsafe {
592-
if !self.target_machine.is_null() {
593-
LLVMDisposeTargetMachine(self.target_machine);
594-
}
595-
if !self.module.is_null() {
596-
LLVMDisposeModule(self.module);
597-
}
598-
if !self.context.is_null() {
599-
LLVMContextDispose(self.context);
600-
}
601-
}
602-
}
603-
}
604-
605615
fn detect_input_type(data: &[u8]) -> Option<InputType> {
606616
if data.len() < 8 {
607617
return None;

0 commit comments

Comments
 (0)