[llvm] r299185 - Go binding: Add GetCurrentDebugLocation to obtain debug location from builder

Andrew Wilkins via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 21:59:57 PDT 2017


Author: axw
Date: Thu Mar 30 23:59:57 2017
New Revision: 299185

URL: http://llvm.org/viewvc/llvm-project?rev=299185&view=rev
Log:
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


Modified:
    llvm/trunk/bindings/go/llvm/IRBindings.cpp
    llvm/trunk/bindings/go/llvm/IRBindings.h
    llvm/trunk/bindings/go/llvm/ir.go
    llvm/trunk/bindings/go/llvm/ir_test.go

Modified: llvm/trunk/bindings/go/llvm/IRBindings.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/go/llvm/IRBindings.cpp?rev=299185&r1=299184&r2=299185&view=diff
==============================================================================
--- llvm/trunk/bindings/go/llvm/IRBindings.cpp (original)
+++ llvm/trunk/bindings/go/llvm/IRBindings.cpp Thu Mar 30 23:59:57 2017
@@ -14,6 +14,7 @@
 #include "IRBindings.h"
 #include "llvm/IR/Attributes.h"
 #include "llvm/IR/DebugLoc.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/LLVMContext.h"
@@ -71,6 +72,18 @@ void LLVMSetCurrentDebugLocation2(LLVMBu
                     InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
 }
 
+LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref) {
+  const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
+  const auto* InlinedAt = Loc.getInlinedAt();
+  const LLVMDebugLocMetadata md{
+    Loc.getLine(),
+    Loc.getCol(),
+    wrap(Loc.getScope()),
+    InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
+  };
+  return md;
+}
+
 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
   unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
 }

Modified: llvm/trunk/bindings/go/llvm/IRBindings.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/go/llvm/IRBindings.h?rev=299185&r1=299184&r2=299185&view=diff
==============================================================================
--- llvm/trunk/bindings/go/llvm/IRBindings.h (original)
+++ llvm/trunk/bindings/go/llvm/IRBindings.h Thu Mar 30 23:59:57 2017
@@ -27,6 +27,12 @@ extern "C" {
 #endif
 
 typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
+struct LLVMDebugLocMetadata{
+    unsigned Line;
+    unsigned Col;
+    LLVMMetadataRef Scope;
+    LLVMMetadataRef InlinedAt;
+};
 
 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef Val);
 
@@ -46,6 +52,8 @@ void LLVMSetCurrentDebugLocation2(LLVMBu
                                   unsigned Col, LLVMMetadataRef Scope,
                                   LLVMMetadataRef InlinedAt);
 
+struct LLVMDebugLocMetadata LLVMGetCurrentDebugLocation2(LLVMBuilderRef Bref);
+
 void LLVMSetSubprogram(LLVMValueRef Fn, LLVMMetadataRef SP);
 
 #ifdef __cplusplus

Modified: llvm/trunk/bindings/go/llvm/ir.go
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/go/llvm/ir.go?rev=299185&r1=299184&r2=299185&view=diff
==============================================================================
--- llvm/trunk/bindings/go/llvm/ir.go (original)
+++ llvm/trunk/bindings/go/llvm/ir.go Thu Mar 30 23:59:57 2017
@@ -1226,9 +1226,23 @@ func (b Builder) InsertWithName(instr Va
 func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
 
 // Metadata
+type DebugLoc struct {
+	Line, Col      uint
+	Scope          Metadata
+	InlinedAt      Metadata
+}
 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
 	C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
 }
+// Get current debug location. Please do not call this function until setting debug location with SetCurrentDebugLocation()
+func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
+	md := C.LLVMGetCurrentDebugLocation2(b.C)
+	loc.Line = uint(md.Line)
+	loc.Col = uint(md.Col)
+	loc.Scope = Metadata{C: md.Scope}
+	loc.InlinedAt = Metadata{C: md.InlinedAt}
+	return
+}
 func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
 func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
 	f := module.NamedFunction("llvm.dbg.declare")

Modified: llvm/trunk/bindings/go/llvm/ir_test.go
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/go/llvm/ir_test.go?rev=299185&r1=299184&r2=299185&view=diff
==============================================================================
--- llvm/trunk/bindings/go/llvm/ir_test.go (original)
+++ llvm/trunk/bindings/go/llvm/ir_test.go Thu Mar 30 23:59:57 2017
@@ -95,3 +95,42 @@ func TestAttributes(t *testing.T) {
 		testAttribute(t, name)
 	}
 }
+
+func TestDebugLoc(t *testing.T) {
+	mod := NewModule("")
+	defer mod.Dispose()
+
+	ctx := mod.Context()
+
+	b := ctx.NewBuilder()
+	defer b.Dispose()
+
+	d := NewDIBuilder(mod)
+	defer func() {
+		d.Destroy()
+	}()
+	file := d.CreateFile("dummy_file", "dummy_dir")
+	voidInfo := d.CreateBasicType(DIBasicType{Name: "void"})
+	typeInfo := d.CreateSubroutineType(DISubroutineType{file, []Metadata{voidInfo}})
+	scope := d.CreateFunction(file, DIFunction{
+		Name:         "foo",
+		LinkageName:  "foo",
+		Line:         10,
+		ScopeLine:    10,
+		Type:         typeInfo,
+		File:         file,
+		IsDefinition: true,
+	})
+
+	b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
+	loc := b.GetCurrentDebugLocation()
+	if loc.Line != 10 {
+		t.Errorf("Got line %d, though wanted 10", loc.Line)
+	}
+	if loc.Col != 20 {
+		t.Errorf("Got column %d, though wanted 20", loc.Col)
+	}
+	if loc.Scope.C != scope.C {
+		t.Errorf("Got metadata %v as scope, though wanted %v", loc.Scope.C, scope.C)
+	}
+}




More information about the llvm-commits mailing list