From c6ac1cc9699b16829fe4cd2d44948f12378711a5 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 5 Mar 2023 17:36:01 +0100 Subject: [PATCH] compiler: add debug location to string values This is helpful for WebAssembly: it makes it possible to attribute many more data to locations in the source code, which makes `-size=full` more useful. --- compiler/compiler.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compiler/compiler.go b/compiler/compiler.go index 214ef63c..902885c8 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -2837,6 +2837,18 @@ func (c *compilerContext) createConst(expr *ssa.Const, pos token.Pos) llvm.Value global.SetGlobalConstant(true) global.SetUnnamedAddr(true) global.SetAlignment(1) + if c.Debug { + // Unfortunately, expr.Pos() is always token.NoPos. + position := c.program.Fset.Position(pos) + diglobal := c.dibuilder.CreateGlobalVariableExpression(llvm.Metadata{}, llvm.DIGlobalVariableExpression{ + File: c.getDIFile(position.Filename), + Line: position.Line, + Type: c.getDIType(types.NewArray(types.Typ[types.Byte], int64(len(str)))), + LocalToUnit: true, + Expr: c.dibuilder.CreateExpression(nil), + }) + global.AddMetadata(0, diglobal) + } zero := llvm.ConstInt(c.ctx.Int32Type(), 0, false) strPtr = llvm.ConstInBoundsGEP(globalType, global, []llvm.Value{zero, zero}) } else {