Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct generated pos info #33

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.18"
const REVISION = "d7fa92e870f1be98c43f42107ad24f4a3152ef5a+1"
const NUMBER = 157
const REVISION = "e0fff32b787ec10d8c27b655f546a8a43e090b61+1"
const NUMBER = 158

func getRevision() string {
return fmt.Sprintf("%s %s BUILD_%d", VERSION, REVISION, NUMBER)
Expand Down
8 changes: 4 additions & 4 deletions patch/adapter_go1.17_18_19_20_21.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ func takeAddrs(fn *ir.Func, t *types.Type, nameOnly bool) ir.Expr {
return wrapListType(ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, typeNode(intfSlice), paramList))
}

func getFieldNames(fn *ir.Func, t *types.Type) ir.Expr {
func getFieldNames(pos src.XPos, fn *ir.Func, t *types.Type) ir.Expr {
if t.NumFields() == 0 {
return NewNilExpr(fn.Pos(), strSlice)
return NewNilExpr(pos, strSlice)
}
paramList := make([]ir.Node, t.NumFields())
i := 0
ForEachField(t, func(field *types.Field) bool {
fieldName := getFieldName(fn, field)
paramList[i] = NewStringLit(fn.Pos(), fieldName)
paramList[i] = NewStringLit(pos, fieldName)
i++
return true
})
return wrapListType(ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, typeNode(strSlice), paramList))
return wrapListType(ir.NewCompLitExpr(pos, ir.OCOMPLIT, typeNode(strSlice), paramList))
}

func getTypeNames(params *types.Type) []ir.Node {
Expand Down
8 changes: 4 additions & 4 deletions patch/adapter_go1.22.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ func takeAddrs(fn *ir.Func, t []*types.Field, nameOnly bool) ir.Expr {
}

// NOTE: []*types.Field instead of *types.Type(go1.21)
func getFieldNames(fn *ir.Func, t []*types.Field) ir.Expr {
func getFieldNames(pos src.XPos, fn *ir.Func, t []*types.Field) ir.Expr {
if len(t) == 0 {
return NewNilExpr(fn.Pos(), strSlice)
return NewNilExpr(pos, strSlice)
}
paramList := make([]ir.Node, len(t))
i := 0
ForEachField(t, func(field *types.Field) bool {
fieldName := getFieldName(fn, field)
paramList[i] = NewStringLit(fn.Pos(), fieldName)
paramList[i] = NewStringLit(pos, fieldName)
i++
return true
})
return ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, strSlice, paramList)
return ir.NewCompLitExpr(pos, ir.OCOMPLIT, strSlice, paramList)
}

func getTypeNames(params []*types.Field) []ir.Node {
Expand Down
10 changes: 8 additions & 2 deletions patch/link_name.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package patch

import (
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/typecheck"
xgo_ctxt "cmd/compile/internal/xgo_rewrite_internal/patch/ctxt"
xgo_record "cmd/compile/internal/xgo_rewrite_internal/patch/record"
xgo_syntax "cmd/compile/internal/xgo_rewrite_internal/patch/syntax"
"cmd/internal/src"
"strings"
)

Expand Down Expand Up @@ -137,14 +139,18 @@ func replaceWithRuntimeCall(fn *ir.Func, name string) {
// callNode = ir.NewAssignListStmt(fnPos, ir.OAS2, resNames, []ir.Node{callNode})
// }
}
replaceFuncBody(fn, []ir.Node{
replaceFuncBodyWithPos(base.AutogeneratedPos, fn, []ir.Node{
// debugPrint("debug getg"),
callNode,
})
}

func replaceFuncBody(fn *ir.Func, nodes []ir.Node) {
node := ifConstant(fn.Pos(), true, nodes, fn.Body)
replaceFuncBodyWithPos(fn.Pos(), fn, nodes)
}

func replaceFuncBodyWithPos(pos src.XPos, fn *ir.Func, nodes []ir.Node) {
node := ifConstant(pos, true, nodes, fn.Body)

fn.Body = []ir.Node{node}
xgo_record.SetRewrittenBody(fn, fn.Body)
Expand Down
9 changes: 5 additions & 4 deletions patch/trap.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,17 +392,18 @@ func initClosureRegs() {
trueLit := NewBoolLit(base.AutogeneratedPos, true)
emptyStrLit := NewStringLit(base.AutogeneratedPos, "")
for _, fn := range trappedClosures {
pos := fn.Pos()
pos := base.AutogeneratedPos
fnPos := fn.Pos()

posInfo := getPosInfo(pos)
posInfo := getPosInfo(fnPos)
fileName := posInfo.AbsFilename()
adjustFile := getAdjustedFile(fileName)
if adjustFile != "" {
fileName = adjustFile
}
fnType := fn.Type()
argNames := getFieldNames(fn, fnType.Params())
resNames := getFieldNames(fn, fnType.Results())
argNames := getFieldNames(pos, fn, fnType.Params())
resNames := getFieldNames(pos, fn, fnType.Results())
// TODO: why fn.Name() always returns nil?
var fnRef ir.Node
if !closureMayBeEliminatedDueToIfConst {
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.18"
const REVISION = "d7fa92e870f1be98c43f42107ad24f4a3152ef5a+1"
const NUMBER = 157
const REVISION = "e0fff32b787ec10d8c27b655f546a8a43e090b61+1"
const NUMBER = 158

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
31 changes: 7 additions & 24 deletions runtime/test/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,18 @@
package debug

import (
"context"
"testing"
"time"

"github.com/xhd2015/xgo/runtime/core"
"github.com/xhd2015/xgo/runtime/mock"
)

func TestMockTimeSleep(t *testing.T) {
begin := time.Now()
sleepDur := 1 * time.Second
var haveCalledMock bool
var mockArg time.Duration
mock.Mock(time.Sleep, func(ctx context.Context, fn *core.FuncInfo, args, results core.Object) error {
haveCalledMock = true
mockArg = args.GetFieldIndex(0).Value().(time.Duration)
return nil
func TestDebug(t *testing.T) {
mock.Patch(greet, func(s string) string {
return "mock " + s
})
time.Sleep(sleepDur)

cost := time.Since(begin)
greet("world")
}

if !haveCalledMock {
t.Fatalf("expect haveCalledMock, actually not")
}
if mockArg != sleepDur {
t.Fatalf("expect mockArg to be %v, actual: %v", sleepDur, mockArg)
}
if cost > 100*time.Millisecond {
t.Fatalf("expect time.Sleep mocked, actual cost: %v", cost)
}
func greet(s string) string {
return "hello " + s
}
Loading