Skip to content

Commit 1ff21df

Browse files
committed
Implement API for adding debug info to global variables
I implemented what seems like the most generic API possible. For example, there is also GlobalVariable->addDebugInfo but that is basically just a helper for GlobalObject->addMetadata. Backport of https://reviews.llvm.org/D72206.
1 parent ad71f3d commit 1ff21df

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

backports.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ void LLVMPassManagerBuilderAddCoroutinePassesToExtensionPoints_backport(LLVMPass
2020
llvm::PassManagerBuilder *Builder = llvm::unwrap(PMB);
2121
llvm::addCoroutinePassesToExtensionPoints(*Builder);
2222
}
23+
24+
void LLVMGlobalObjectAddMetadata(LLVMValueRef Global, unsigned KindID, LLVMMetadataRef MD) {
25+
llvm::MDNode *N = MD ? llvm::unwrap<llvm::MDNode>(MD) : nullptr;
26+
llvm::GlobalObject *O = llvm::unwrap<llvm::GlobalObject>(Global);
27+
O->addMetadata(KindID, *N);
28+
}

backports.h

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88

99
void LLVMPassManagerBuilderAddCoroutinePassesToExtensionPoints_backport(LLVMPassManagerBuilderRef PMB);
1010

11+
void LLVMGlobalObjectAddMetadata(LLVMValueRef objValue, unsigned KindID, LLVMMetadataRef md);
12+
1113
#ifdef __cplusplus
1214
}
1315
#endif /* defined(__cplusplus) */

dibuilder.go

+43
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package llvm
1414

1515
/*
1616
#include "IRBindings.h"
17+
#include "backports.h"
1718
#include <stdlib.h>
1819
*/
1920
import "C"
@@ -220,6 +221,43 @@ func (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata {
220221
return Metadata{C: result}
221222
}
222223

224+
// DIGlobalVariableExpression holds the values for creating global variable
225+
// debug metadata.
226+
type DIGlobalVariableExpression struct {
227+
Name string // Name of the variable.
228+
LinkageName string // Mangled name of the variable
229+
File Metadata // File where this variable is defined.
230+
Line int // Line number.
231+
Type Metadata // Variable Type.
232+
LocalToUnit bool // Flag indicating whether this variable is externally visible or not.
233+
Expr Metadata // The location of the global relative to the attached GlobalVariable.
234+
Decl Metadata // Reference to the corresponding declaration.
235+
AlignInBits uint32 // Variable alignment(or 0 if no alignment attr was specified).
236+
}
237+
238+
// CreateGlobalVariableExpression creates a new descriptor for the specified
239+
// global variable.
240+
func (d *DIBuilder) CreateGlobalVariableExpression(diScope Metadata, g DIGlobalVariableExpression) Metadata {
241+
name := C.CString(g.Name)
242+
defer C.free(unsafe.Pointer(name))
243+
linkageName := C.CString(g.LinkageName)
244+
defer C.free(unsafe.Pointer(linkageName))
245+
result := C.LLVMDIBuilderCreateGlobalVariableExpression(
246+
d.ref, // Builder
247+
diScope.C, // Scope
248+
name, C.size_t(len(g.Name)), // Name, NameLen
249+
linkageName, C.size_t(len(g.LinkageName)), // Linkage, LinkLen
250+
g.File.C, // File
251+
C.unsigned(g.Line), // LineNo
252+
g.Type.C, // Ty
253+
C.LLVMBool(boolToCInt(g.LocalToUnit)), // LocalToUnit
254+
g.Expr.C, // Expr
255+
g.Decl.C, // Decl
256+
C.uint32_t(g.AlignInBits), // AlignInBits
257+
)
258+
return Metadata{C: result}
259+
}
260+
223261
// DIAutoVariable holds the values for creating auto variable debug metadata.
224262
type DIAutoVariable struct {
225263
Name string
@@ -589,6 +627,11 @@ func (v Value) Subprogram() (md Metadata) {
589627
return
590628
}
591629

630+
// AddMetadata adds a metadata entry of the given kind to a global object.
631+
func (v Value) AddMetadata(kind int, md Metadata) {
632+
C.LLVMGlobalObjectAddMetadata(v.C, C.unsigned(kind), md.C)
633+
}
634+
592635
func boolToCInt(v bool) C.int {
593636
if v {
594637
return 1

0 commit comments

Comments
 (0)