diff --git a/llvm/bindings/go/llvm/dibuilder.go b/llvm/bindings/go/llvm/dibuilder.go --- a/llvm/bindings/go/llvm/dibuilder.go +++ b/llvm/bindings/go/llvm/dibuilder.go @@ -221,6 +221,43 @@ return Metadata{C: result} } +// DIGlobalVariableExpression holds the values for creating global variable +// debug metadata. +type DIGlobalVariableExpression struct { + Name string // Name of the variable. + LinkageName string // Mangled name of the variable + File Metadata // File where this variable is defined. + Line int // Line number. + Type Metadata // Variable Type. + LocalToUnit bool // Flag indicating whether this variable is externally visible or not. + Expr Metadata // The location of the global relative to the attached GlobalVariable. + Decl Metadata // Reference to the corresponding declaration. + AlignInBits uint32 // Variable alignment(or 0 if no alignment attr was specified). +} + +// CreateGlobalVariableExpression creates a new descriptor for the specified +// global variable. +func (d *DIBuilder) CreateGlobalVariableExpression(diScope Metadata, g DIGlobalVariableExpression) Metadata { + name := C.CString(g.Name) + defer C.free(unsafe.Pointer(name)) + linkageName := C.CString(g.LinkageName) + defer C.free(unsafe.Pointer(linkageName)) + result := C.LLVMDIBuilderCreateGlobalVariableExpression( + d.ref, // Builder + diScope.C, // Scope + name, C.size_t(len(g.Name)), // Name, NameLen + linkageName, C.size_t(len(g.LinkageName)), // Linkage, LinkLen + g.File.C, // File + C.unsigned(g.Line), // LineNo + g.Type.C, // Ty + C.LLVMBool(boolToCInt(g.LocalToUnit)), // LocalToUnit + g.Expr.C, // Expr + g.Decl.C, // Decl + C.uint32_t(g.AlignInBits), // AlignInBits + ) + return Metadata{C: result} +} + // DIAutoVariable holds the values for creating auto variable debug metadata. type DIAutoVariable struct { Name string @@ -590,6 +627,11 @@ return } +// AddMetadata adds a metadata entry of the given kind to a global object. +func (v Value) AddMetadata(kind int, md Metadata) { + C.LLVMGlobalObjectAddMetadata(v.C, C.unsigned(kind), md.C) +} + func boolToCInt(v bool) C.int { if v { return 1 diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3064,6 +3064,11 @@ */ void LLVMSetMetadata(LLVMValueRef Val, unsigned KindID, LLVMValueRef Node); +/** + * Add metadata to a global object. + */ +void LLVMGlobalObjectAddMetadata(LLVMValueRef Global, unsigned KindID, LLVMMetadataRef MD); + /** * Returns the metadata associated with an instruction value, but filters out * all the debug locations. diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -888,6 +888,12 @@ unwrap(Inst)->setMetadata(KindID, N); } +void LLVMGlobalObjectAddMetadata(LLVMValueRef Global, unsigned KindID, LLVMMetadataRef MD) { + MDNode *N = MD ? unwrap(MD) : nullptr; + GlobalObject *O = unwrap(Global); + O->addMetadata(KindID, *N); +} + struct LLVMOpaqueValueMetadataEntry { unsigned Kind; LLVMMetadataRef Metadata;