From 4a57ec79fc1d8787539b2a9e9eca692c9ec7ecc7 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sun, 11 Mar 2018 17:10:48 +0800 Subject: [PATCH 1/2] Derived Debug for the remaining types --- src/builder.rs | 1 + src/lib.rs | 1 + src/memory_buffer.rs | 1 + src/object_file.rs | 8 +++++++- src/passes.rs | 3 +++ src/values/fn_value.rs | 10 +++++++++- src/values/generic_value.rs | 1 + 7 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index d3b7b8623c708..c168d81a0fb9e 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -10,6 +10,7 @@ use types::{AsTypeRef, BasicType, PointerType, IntType, FloatType}; use std::ffi::CString; +#[derive(Debug)] pub struct Builder { builder: LLVMBuilderRef, } diff --git a/src/lib.rs b/src/lib.rs index 80e7067f7ad0b..a7b0d1c7d6284 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![deny(missing_debug_implementations)] extern crate either; #[macro_use] extern crate enum_methods; diff --git a/src/memory_buffer.rs b/src/memory_buffer.rs index 80944081b8630..ac0e34481ab35 100644 --- a/src/memory_buffer.rs +++ b/src/memory_buffer.rs @@ -9,6 +9,7 @@ use std::mem::zeroed; use std::path::Path; use std::ptr; +#[derive(Debug)] pub struct MemoryBuffer { pub(crate) memory_buffer: LLVMMemoryBufferRef } diff --git a/src/object_file.rs b/src/object_file.rs index 14ad572a80c75..0d46449bd8ec8 100644 --- a/src/object_file.rs +++ b/src/object_file.rs @@ -5,7 +5,7 @@ use std::ffi::CStr; // REVIEW: Make sure SectionIterator's object_file ptr doesn't outlive ObjectFile // REVIEW: This module is very untested // TODO: More references to account for lifetimes - +#[derive(Debug)] pub struct ObjectFile { object_file: LLVMObjectFileRef } @@ -44,6 +44,7 @@ impl Drop for ObjectFile { } } +#[derive(Debug)] pub struct SectionIterator { section_iterator: LLVMSectionIteratorRef, object_file: LLVMObjectFileRef, @@ -91,6 +92,7 @@ impl Drop for SectionIterator { } } +#[derive(Debug)] pub struct Section { section: LLVMSectionIteratorRef, object_file: LLVMObjectFileRef, @@ -139,6 +141,7 @@ impl Section { } } +#[derive(Debug)] pub struct RelocationIterator { relocation_iterator: LLVMRelocationIteratorRef, section_iterator: LLVMSectionIteratorRef, @@ -188,6 +191,7 @@ impl Drop for RelocationIterator { } } +#[derive(Debug)] pub struct Relocation { relocation: LLVMRelocationIteratorRef, object_file: LLVMObjectFileRef, @@ -236,6 +240,7 @@ impl Relocation { } } +#[derive(Debug)] pub struct SymbolIterator { symbol_iterator: LLVMSymbolIteratorRef, object_file: LLVMObjectFileRef, @@ -283,6 +288,7 @@ impl Drop for SymbolIterator { } } +#[derive(Debug)] pub struct Symbol { symbol: LLVMSymbolIteratorRef, } diff --git a/src/passes.rs b/src/passes.rs index 9bdbeb445245f..9531f447909c6 100644 --- a/src/passes.rs +++ b/src/passes.rs @@ -18,6 +18,7 @@ use values::{AsValueRef, FunctionValue}; // REVIEW: Opt Level might be identical to targets::Option // REVIEW: size_level 0-2 according to llvmlite +#[derive(Debug)] pub struct PassManagerBuilder { pass_manager_builder: LLVMPassManagerBuilderRef, } @@ -107,6 +108,7 @@ impl Drop for PassManagerBuilder { } // SubTypes: PassManager, PassManager +#[derive(Debug)] pub struct PassManager { pub(crate) pass_manager: LLVMPassManagerRef, } @@ -525,6 +527,7 @@ impl Drop for PassManager { } } +#[derive(Debug)] pub struct PassRegistry { pass_registry: LLVMPassRegistryRef, } diff --git a/src/values/fn_value.rs b/src/values/fn_value.rs index 99a6a4fc7549c..705f901fc3f90 100644 --- a/src/values/fn_value.rs +++ b/src/values/fn_value.rs @@ -330,10 +330,18 @@ impl fmt::Debug for FunctionValue { }; let is_null = self.is_null(); - write!(f, "FunctionValue {{\n name: {:?}\n address: {:?}\n is_const: {:?}\n is_null: {:?}\n llvm_value: {:?}\n llvm_type: {:?}\n}}", name, self.as_value_ref(), is_const, is_null, llvm_value, llvm_type.print_to_string()) + f.debug_struct("FunctionValue") + .field("name", &name) + .field("address", &self.as_value_ref()) + .field("is_const", &is_const) + .field("is_null", &is_null) + .field("llvm_value", &llvm_value) + .field("llvm_type", &llvm_type.print_to_string()) + .finish() } } +#[derive(Debug)] pub struct ParamValueIter { param_iter_value: LLVMValueRef, start: bool, diff --git a/src/values/generic_value.rs b/src/values/generic_value.rs index 0997f1d0138c8..c7f0f983ab942 100644 --- a/src/values/generic_value.rs +++ b/src/values/generic_value.rs @@ -4,6 +4,7 @@ use llvm_sys::execution_engine::{LLVMCreateGenericValueOfPointer, LLVMDisposeGen use types::{AsTypeRef, FloatType}; // SubTypes: GenericValue +#[derive(Debug)] pub struct GenericValue { pub(crate) generic_value: LLVMGenericValueRef, } From c25aed6a3445293f5f4c943f2ed23797c2b81e77 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sun, 11 Mar 2018 17:21:52 +0800 Subject: [PATCH 2/2] Used the Formatter::debug_struct() builder instead of string formatting --- src/basic_block.rs | 7 ++++++- src/data_layout.rs | 7 ++++--- src/types/fn_type.rs | 5 ++++- src/types/mod.rs | 5 ++++- src/values/array_value.rs | 11 ++++++++++- src/values/metadata_value.rs | 12 ++++++------ src/values/mod.rs | 10 +++++++++- 7 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/basic_block.rs b/src/basic_block.rs index 851e0f794996b..870dbaa658116 100644 --- a/src/basic_block.rs +++ b/src/basic_block.rs @@ -421,6 +421,11 @@ impl fmt::Debug for BasicBlock { LLVMIsConstant(self.basic_block as LLVMValueRef) == 1 }; - write!(f, "BasicBlock {{\n address: {:?}\n is_const: {:?}\n llvm_value: {:?}\n llvm_type: {:?}\n}}", self.basic_block, is_const, llvm_value, llvm_type) + f.debug_struct("BasicBlock") + .field("address", &self.basic_block) + .field("is_const", &is_const) + .field("llvm_value", &llvm_value) + .field("llvm_type", &llvm_type) + .finish() } } diff --git a/src/data_layout.rs b/src/data_layout.rs index d765ad4503210..7c878574f15fa 100644 --- a/src/data_layout.rs +++ b/src/data_layout.rs @@ -36,9 +36,10 @@ impl PartialEq for DataLayout { impl fmt::Debug for DataLayout { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "DataLayout {{\n ")?; - write!(f, "address: {:?}\n ", self.data_layout.get())?; - write!(f, "repr: {:?}\n}}", self.as_str()) + f.debug_struct("DataLayout") + .field("address", &self.data_layout.get()) + .field("repr", &self.as_str()) + .finish() } } diff --git a/src/types/fn_type.rs b/src/types/fn_type.rs index 06e0a2ae7c278..6826c345d2ea2 100644 --- a/src/types/fn_type.rs +++ b/src/types/fn_type.rs @@ -82,7 +82,10 @@ impl fmt::Debug for FunctionType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let llvm_type = self.print_to_string(); - write!(f, "FunctionType {{\n address: {:?}\n llvm_type: {:?}\n}}", self.as_type_ref(), llvm_type) + f.debug_struct("FunctionType") + .field("address", &self.as_type_ref()) + .field("llvm_type", &llvm_type) + .finish() } } diff --git a/src/types/mod.rs b/src/types/mod.rs index bc2451b0f99cf..bb4a358f1277b 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -171,6 +171,9 @@ impl fmt::Debug for Type { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let llvm_type = self.print_to_string(); - write!(f, "Type {{\n address: {:?}\n llvm_type: {:?}\n}}", self.type_, llvm_type) + f.debug_struct("Type") + .field("address", &self.type_) + .field("llvm_type", &llvm_type) + .finish() } } diff --git a/src/values/array_value.rs b/src/values/array_value.rs index aa20e7e2a51a2..952fb6a945524 100644 --- a/src/values/array_value.rs +++ b/src/values/array_value.rs @@ -93,6 +93,15 @@ impl fmt::Debug for ArrayValue { !LLVMIsAConstantDataArray(self.as_value_ref()).is_null() }; - write!(f, "Value {{\n name: {:?}\n address: {:?}\n is_const: {:?}\n is_const_array: {:?}\n is_const_data_array: {:?}\n is_null: {:?}\n llvm_value: {:?}\n llvm_type: {:?}\n}}", name, self.as_value_ref(), is_const, is_const_array, is_const_data_array, is_null, llvm_value, llvm_type.print_to_string()) + f.debug_struct("Value") + .field("name", &name) + .field("address", &self.as_value_ref()) + .field("is_const", &is_const) + .field("is_const_array", &is_const_array) + .field("is_const_data_array", &is_const_data_array) + .field("is_null", &is_null) + .field("llvm_value", &llvm_value) + .field("llvm_type", &llvm_type) + .finish() } } diff --git a/src/values/metadata_value.rs b/src/values/metadata_value.rs index f66682676b84b..260ef86bc21e8 100644 --- a/src/values/metadata_value.rs +++ b/src/values/metadata_value.rs @@ -143,17 +143,17 @@ impl AsValueRef for MetadataValue { impl fmt::Debug for MetadataValue { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "MetadataValue {{\n ")?; - write!(f, "address: {:?}\n", self.as_value_ref())?; + let mut d = f.debug_struct("MetadataValue"); + d.field("address", &self.as_value_ref()); if self.is_string() { - write!(f, "value: {:?}\n", self.get_string_value().unwrap())?; + d.field("value", &self.get_string_value().unwrap()); } else { - write!(f, "values: {:?}\n", self.get_node_values())?; + d.field("values", &self.get_node_values()); } - write!(f, "repr: {:?}", self.print_to_string())?; + d.field("repr", &self.print_to_string()); - write!(f, "\n}}") + d.finish() } } diff --git a/src/values/mod.rs b/src/values/mod.rs index 37d047ea4cb62..c0115f7e31e03 100644 --- a/src/values/mod.rs +++ b/src/values/mod.rs @@ -168,6 +168,14 @@ impl fmt::Debug for Value { let is_null = self.is_null(); let is_undef = self.is_undef(); - write!(f, "Value {{\n name: {:?}\n address: {:?}\n is_const: {:?}\n is_null: {:?}\n is_undef: {:?}\n llvm_value: {:?}\n llvm_type: {:?}\n}}", name, self.value, is_const, is_null, is_undef, llvm_value, llvm_type) + f.debug_struct("Value") + .field("name", &name) + .field("address", &self.value) + .field("is_const", &is_const) + .field("is_null", &is_null) + .field("is_undef", &is_undef) + .field("llvm_value", &llvm_value) + .field("llvm_type", &llvm_type) + .finish() } }