llvm.org GIT mirror llvm / 903a962
Go binding: Add GetCurrentDebugLocation to obtain debug location from builder Summary: Currently Go binding only has SetCurrentDebugLocation method. I added GetCurrentDebugLocation method to IRBuilder instance. I added this because I want to save current debug location, change debug location temporary and restore the saved one finally. This is useful when source location jumps and goes back after while LLVM IR generation. I also added tests for this to ir_test.go. I confirmed that all test passed with this patch based on r298890 Patch by Ryuichi Hayashida! Differential Revision: https://reviews.llvm.org/D31415 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299185 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Wilkins 2 years ago
4 changed file(s) with 74 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1313 #include "IRBindings.h"
1414 #include "llvm/IR/Attributes.h"
1515 #include "llvm/IR/DebugLoc.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
1617 #include "llvm/IR/Function.h"
1718 #include "llvm/IR/IRBuilder.h"
1819 #include "llvm/IR/LLVMContext.h"
7071 InlinedAt ? unwrap(InlinedAt) : nullptr));
7172 }
7273
74 LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref) {
75 const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
76 const auto* InlinedAt = Loc.getInlinedAt();
77 const LLVMDebugLocMetadata md{
78 Loc.getLine(),
79 Loc.getCol(),
80 wrap(Loc.getScope()),
81 InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
82 };
83 return md;
84 }
85
7386 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
7487 unwrap(Func)->setSubprogram(unwrap(SP));
7588 }
2626 #endif
2727
2828 typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
29 struct LLVMDebugLocMetadata{
30 unsigned Line;
31 unsigned Col;
32 LLVMMetadataRef Scope;
33 LLVMMetadataRef InlinedAt;
34 };
2935
3036 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef Val);
3137
4551 unsigned Col, LLVMMetadataRef Scope,
4652 LLVMMetadataRef InlinedAt);
4753
54 struct LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref);
55
4856 void LLVMSetSubprogram(LLVMValueRef Fn, LLVMMetadataRef SP);
4957
5058 #ifdef __cplusplus
12251225 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
12261226
12271227 // Metadata
1228 type DebugLoc struct {
1229 Line, Col uint
1230 Scope Metadata
1231 InlinedAt Metadata
1232 }
12281233 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
12291234 C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
1235 }
1236 // Get current debug location. Please do not call this function until setting debug location with SetCurrentDebugLocation()
1237 func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
1238 md := C.LLVMGetCurrentDebugLocation2(b.C)
1239 loc.Line = uint(md.Line)
1240 loc.Col = uint(md.Col)
1241 loc.Scope = Metadata{C: md.Scope}
1242 loc.InlinedAt = Metadata{C: md.InlinedAt}
1243 return
12301244 }
12311245 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
12321246 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
9494 testAttribute(t, name)
9595 }
9696 }
97
98 func TestDebugLoc(t *testing.T) {
99 mod := NewModule("")
100 defer mod.Dispose()
101
102 ctx := mod.Context()
103
104 b := ctx.NewBuilder()
105 defer b.Dispose()
106
107 d := NewDIBuilder(mod)
108 defer func() {
109 d.Destroy()
110 }()
111 file := d.CreateFile("dummy_file", "dummy_dir")
112 voidInfo := d.CreateBasicType(DIBasicType{Name: "void"})
113 typeInfo := d.CreateSubroutineType(DISubroutineType{file, []Metadata{voidInfo}})
114 scope := d.CreateFunction(file, DIFunction{
115 Name: "foo",
116 LinkageName: "foo",
117 Line: 10,
118 ScopeLine: 10,
119 Type: typeInfo,
120 File: file,
121 IsDefinition: true,
122 })
123
124 b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
125 loc := b.GetCurrentDebugLocation()
126 if loc.Line != 10 {
127 t.Errorf("Got line %d, though wanted 10", loc.Line)
128 }
129 if loc.Col != 20 {
130 t.Errorf("Got column %d, though wanted 20", loc.Col)
131 }
132 if loc.Scope.C != scope.C {
133 t.Errorf("Got metadata %v as scope, though wanted %v", loc.Scope.C, scope.C)
134 }
135 }