78
78
//! /* current_scope */ lexical_block.as_debug_info_scope(),
79
79
//! /* inlined_at */ None);
80
80
//! builder.set_current_debug_location(&context, loc);
81
+ //!
82
+ //! // Create global variable
83
+ //! let gv = module.add_global(context.i64_type(), Some(inkwell::AddressSpace::Global), "gv");
84
+ //!
85
+ //!
86
+ //! let const_v = di.create_constant_expression(10);
87
+ //!
88
+ //! let gv_debug = di.create_global_variable_expression(cu.get_file().as_debug_info_scope(), "gv", "", cu.get_file(), 1, ditype.as_type(), true, Some(const_v), None, 8);
89
+ //!
90
+ //! let meta_value: inkwell::values::BasicMetadataValueEnum = gv_debug.as_metadata_value(&context).into();
91
+ //! let metadata = context.metadata_node(&[meta_value]);
92
+ //! gv.set_metadata(metadata, 0);//dbg
93
+ //!
81
94
//! ```
82
95
//!
83
96
//! ## Finalize debug info
@@ -91,7 +104,8 @@ use crate::basic_block::BasicBlock;
91
104
use crate :: context:: Context ;
92
105
pub use crate :: debug_info:: flags:: { DIFlags , DIFlagsConstants } ;
93
106
use crate :: module:: Module ;
94
- use crate :: values:: { AsValueRef , BasicValueEnum , InstructionValue , PointerValue } ;
107
+ use crate :: values:: { AsValueRef , BasicValueEnum , InstructionValue , PointerValue , MetadataValue } ;
108
+
95
109
#[ llvm_versions( 8.0 ..=latest) ]
96
110
use llvm_sys:: debuginfo:: LLVMDIBuilderCreateTypedef ;
97
111
pub use llvm_sys:: debuginfo:: LLVMDWARFTypeEncoding ;
@@ -104,14 +118,17 @@ use llvm_sys::debuginfo::{
104
118
LLVMDIBuilderCreateAutoVariable , LLVMDIBuilderCreateBasicType , LLVMDIBuilderCreateCompileUnit ,
105
119
LLVMDIBuilderCreateDebugLocation , LLVMDIBuilderCreateExpression , LLVMDIBuilderCreateFile ,
106
120
LLVMDIBuilderCreateFunction , LLVMDIBuilderCreateLexicalBlock , LLVMDIBuilderCreateMemberType ,
107
- LLVMDIBuilderCreateNameSpace , LLVMDIBuilderCreateParameterVariable ,
121
+ LLVMDIBuilderCreateNameSpace , LLVMDIBuilderCreateParameterVariable ,
108
122
LLVMDIBuilderCreateStructType , LLVMDIBuilderCreateSubroutineType , LLVMDIBuilderCreateUnionType ,
109
123
LLVMDIBuilderFinalize , LLVMDIBuilderInsertDbgValueBefore , LLVMDIBuilderInsertDeclareAtEnd ,
110
124
LLVMDIBuilderInsertDeclareBefore , LLVMDILocationGetColumn , LLVMDILocationGetLine ,
111
125
LLVMDILocationGetScope , LLVMDITypeGetAlignInBits , LLVMDITypeGetOffsetInBits ,
112
126
LLVMDITypeGetSizeInBits ,
113
127
} ;
128
+ #[ llvm_versions( 8.0 ..=latest) ]
129
+ use llvm_sys:: debuginfo:: { LLVMDIBuilderCreateGlobalVariableExpression , LLVMDIBuilderCreateConstantValueExpression } ;
114
130
use llvm_sys:: prelude:: { LLVMDIBuilderRef , LLVMMetadataRef } ;
131
+ use llvm_sys:: core:: LLVMMetadataAsValue ;
115
132
use std:: convert:: TryInto ;
116
133
use std:: marker:: PhantomData ;
117
134
@@ -582,6 +599,63 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
582
599
}
583
600
}
584
601
602
+ #[ llvm_versions( 8.0 ..=latest) ]
603
+ pub fn create_global_variable_expression (
604
+ & self ,
605
+ scope : DIScope < ' ctx > ,
606
+ name : & str ,
607
+ linkage : & str ,
608
+ file : DIFile < ' ctx > ,
609
+ line_no : u32 ,
610
+ ty : DIType < ' ctx > ,
611
+ local_to_unit : bool ,
612
+ expression : Option < DIExpression > ,
613
+ declaration : Option < DIScope > ,
614
+ align_in_bits : u32 ,
615
+ ) -> DIGlobalVariableExpression < ' ctx > {
616
+ let expression_ptr = expression. map_or ( std:: ptr:: null_mut ( ) , |dt| dt. metadata_ref ) ;
617
+ let decl_ptr = declaration. map_or ( std:: ptr:: null_mut ( ) , |dt| dt. metadata_ref ) ;
618
+ let metadata_ref = unsafe {
619
+ LLVMDIBuilderCreateGlobalVariableExpression (
620
+ self . builder ,
621
+ scope. metadata_ref ,
622
+ name. as_ptr ( ) as _ ,
623
+ name. len ( ) ,
624
+ linkage. as_ptr ( ) as _ ,
625
+ linkage. len ( ) ,
626
+ file. metadata_ref ,
627
+ line_no,
628
+ ty. metadata_ref ,
629
+ local_to_unit as _ ,
630
+ expression_ptr,
631
+ decl_ptr,
632
+ align_in_bits,
633
+ )
634
+ } ;
635
+ DIGlobalVariableExpression {
636
+ metadata_ref,
637
+ _marker : PhantomData ,
638
+ }
639
+ }
640
+
641
+ #[ llvm_versions( 8.0 ..=latest) ]
642
+ pub fn create_constant_expression (
643
+ & self ,
644
+ value : i64 ,
645
+ ) -> DIExpression < ' ctx > {
646
+ let metadata_ref = unsafe {
647
+ LLVMDIBuilderCreateConstantValueExpression (
648
+ self . builder ,
649
+ value,
650
+ )
651
+ } ;
652
+
653
+ DIExpression {
654
+ metadata_ref,
655
+ _marker : PhantomData ,
656
+ }
657
+ }
658
+
585
659
/// Create function parameter variable.
586
660
pub fn create_parameter_variable (
587
661
& self ,
@@ -1028,6 +1102,21 @@ pub struct DILocalVariable<'ctx> {
1028
1102
_marker : PhantomData < & ' ctx Context > ,
1029
1103
}
1030
1104
1105
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
1106
+ pub struct DIGlobalVariableExpression < ' ctx > {
1107
+ pub ( crate ) metadata_ref : LLVMMetadataRef ,
1108
+ _marker : PhantomData < & ' ctx Context > ,
1109
+ }
1110
+
1111
+ impl < ' ctx > DIGlobalVariableExpression < ' ctx > {
1112
+ pub fn as_metadata_value ( & self , context : & Context ) -> MetadataValue < ' ctx > {
1113
+ let value = unsafe {
1114
+ LLVMMetadataAsValue ( context. context , self . metadata_ref )
1115
+ } ;
1116
+ MetadataValue :: new ( value)
1117
+ }
1118
+ }
1119
+
1031
1120
/// https://llvm.org/docs/LangRef.html#diexpression
1032
1121
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
1033
1122
pub struct DIExpression < ' ctx > {
0 commit comments