From 1ed019771d4b44d0def11c6b8ae3e08d33d3cca2 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 15 Nov 2018 20:00:00 +0100 Subject: [PATCH] compiler: set debug info when defining a function Move attaching debug info to where the function is defined. As LLVM does not allow setting debug info on declarations, this makes more sense and is less error-prone. This commit fixes debug info when using CGo. --- compiler/compiler.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 0409cc66..ab58d134 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -854,21 +854,6 @@ func (c *Compiler) parseFuncDecl(f *ir.Function) (*Frame, error) { frame.fn.LLVMFn = llvm.AddFunction(c.mod, name, fnType) } - if c.Debug && f.Synthetic == "package initializer" { - difunc, err := c.attachDebugInfoRaw(f, f.LLVMFn, "", "", 0) - if err != nil { - return nil, err - } - frame.difunc = difunc - } else if c.Debug && f.Syntax() != nil && len(f.Blocks) != 0 { - // Create debug info file if needed. - difunc, err := c.attachDebugInfo(f) - if err != nil { - return nil, err - } - frame.difunc = difunc - } - return frame, nil } @@ -1217,7 +1202,24 @@ func (c *Compiler) parseFunc(frame *Frame) error { frame.fn.LLVMFn.SetFunctionCallConv(85) // CallingConv::AVR_SIGNAL } + // Add debug info, if needed. if c.Debug { + if frame.fn.Synthetic == "package initializer" { + // Package initializers have no debug info. Create some fake debug + // info to at least have *something*. + difunc, err := c.attachDebugInfoRaw(frame.fn, frame.fn.LLVMFn, "", "", 0) + if err != nil { + return err + } + frame.difunc = difunc + } else if frame.fn.Syntax() != nil { + // Create debug info file if needed. + difunc, err := c.attachDebugInfo(frame.fn) + if err != nil { + return err + } + frame.difunc = difunc + } pos := c.ir.Program.Fset.Position(frame.fn.Pos()) c.builder.SetCurrentDebugLocation(uint(pos.Line), uint(pos.Column), frame.difunc, llvm.Metadata{}) }