Skip to content

Commit

Permalink
fix patching const test
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 8, 2024
1 parent c12e5a6 commit 75c04e2
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 98 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ jobs:
go-version: '1.20'
cache: false

- name: Check spelling of files
uses: crate-ci/typos@master
continue-on-error: false
with:
files: ./

- name: Build
run: go build -v ./cmd/...

Expand All @@ -41,10 +47,4 @@ jobs:
run: ~/.xgo/bin/xgo revision

- name: Check Go Version
run: ~/.xgo/bin/xgo exec go version

- name: Check spelling of files
uses: crate-ci/typos@master
continue-on-error: false
with:
files: ./
run: ~/.xgo/bin/xgo exec go version
8 changes: 5 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Thanks for helping, this document helps you get started.
First, clone this repository:
```sh
git clone https://github.com/xhd2015/xgo
cd cgo
cd xgo
```

Then, setup git hooks:
Expand All @@ -19,12 +19,14 @@ chmod +x .git/hooks/post-commit
All are set, now start to development, try:
```sh
# help
go run ./cmd/xgo help
go -tags dev run ./cmd/xgo help

# run Hello world test
go test -run TestHelloWorld -v ./test
go -tags dev test -run TestHelloWorld -v ./test
```

NOTE: when developing, always add `-tags dev` to tell go that we building with dev mode.

If you want to check instrumented GOROOT, run:
```sh
go run ./script/setup-dev
Expand Down
2 changes: 2 additions & 0 deletions cmd/xgo/patch_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var compilerFiles = []_FilePath{
type2AssignmentsPatch.FilePath,
syntaxWalkPatch.FilePath,
noderWriterPatch.FilePath,
noderExprPatch.FilePath,
syntaxPrinterPatch.FilePath,
syntaxExtra,
}

Expand Down
76 changes: 70 additions & 6 deletions cmd/xgo/patch_compiler_ast_type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (
const convertXY = `
if xgoConv, ok := x.expr.(*syntax.XgoSimpleConvert); ok {
var isConst bool
switch y.expr.(type) {
case *syntax.XgoSimpleConvert,*syntax.BasicLit:
isConst=true
if isUntyped(y.typ) {
isConst = true
} else {
switch y.expr.(type) {
case *syntax.XgoSimpleConvert, *syntax.BasicLit:
isConst = true
}
}
if !isConst{
t := y.typ
Expand All @@ -35,9 +40,13 @@ if xgoConv, ok := x.expr.(*syntax.XgoSimpleConvert); ok {
}
}else if xgoConv,ok := y.expr.(*syntax.XgoSimpleConvert);ok {
var isConst bool
switch x.expr.(type) {
case *syntax.XgoSimpleConvert,*syntax.BasicLit:
isConst=true
if isUntyped(x.typ) {
isConst = true
} else {
switch x.expr.(type) {
case *syntax.XgoSimpleConvert, *syntax.BasicLit:
isConst = true
}
}
if !isConst{
t := x.typ
Expand Down Expand Up @@ -228,6 +237,51 @@ var noderWriterPatch = &FilePatch{
},
},
}

var noderExprPatch = &FilePatch{
FilePath: _FilePath{"src", "cmd", "compile", "internal", "noder", "expr.go"},
Patches: []*Patch{
{
Mark: "noder_expr_const_expr_op_xgo_simple_convert",
InsertIndex: 1,
InsertBefore: true,
Anchors: []string{
`func constExprOp(expr syntax.Expr) ir.Op {`,
`case *syntax.BasicLit:`,
},
Content: `
case *syntax.XgoSimpleConvert:
return constExprOp(expr.X)
`,
CheckGoVersion: func(goVersion *goinfo.GoVersion) bool {
return goVersion.Major == 1 && goVersion.Minor <= 21
},
},
},
}

var syntaxPrinterPatch = &FilePatch{
FilePath: _FilePath{"src", "cmd", "compile", "internal", "syntax", "printer.go"},
Patches: []*Patch{
{
Mark: "noder_syntax_print_xgo_simple_convert",
InsertIndex: 1,
InsertBefore: true,
Anchors: []string{
`func (p *printer) printRawNode(n Node) {`,
`case *BasicLit:`,
},
Content: `
case *XgoSimpleConvert:
p.printRawNode(n.X)
`,
CheckGoVersion: func(goVersion *goinfo.GoVersion) bool {
return goVersion.Major == 1 && goVersion.Minor <= 21
},
},
},
}

var syntaxExtra = _FilePath{"src", "cmd", "compile", "internal", "syntax", "xgo_extra.go"}

const syntaxExtraPatch = `
Expand Down Expand Up @@ -268,5 +322,15 @@ func patchCompilerAstTypeCheck(goroot string, goVersion *goinfo.GoVersion) error
if err != nil {
return err
}
if goVersion.Major == 1 && goVersion.Minor <= 21 {
err = noderExprPatch.Apply(goroot, goVersion)
if err != nil {
return err
}
err = syntaxPrinterPatch.Apply(goroot, goVersion)
if err != nil {
return err
}
}
return nil
}
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.19"
const REVISION = "fe2e2f38793f24bda0a8690eedba01828ef79916+1"
const NUMBER = 169
const REVISION = "9f58dbf7230665a152fdc35eca356fb0f269a85d_DEV_2024-04-08T07:03:58Z"
const NUMBER = 170

func getRevision() string {
revSuffix := ""
Expand Down
23 changes: 23 additions & 0 deletions patch/syntax/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,30 @@ func fillNames(pos syntax.Pos, recv *syntax.Field, funcType *syntax.FuncType, pr
fillFieldNames(pos, funcType.ParamList, preset, "_a")
fillFieldNames(pos, funcType.ResultList, preset, "_r")
}
func getFuncDeclNamesNoBlank(recv *syntax.Field, funcType *syntax.FuncType) []string {
var names []string
recvName := getFieldName(recv)
if !isBlankName(recvName) {
names = append(names, recvName)
}
paramNames := getFieldNames(funcType.ParamList)
for _, name := range paramNames {
if !isBlankName(name) {
names = append(names, name)
}
}
resultNames := getFieldNames(funcType.ResultList)
for _, name := range resultNames {
if !isBlankName(name) {
names = append(names, name)
}
}
return names
}

func isBlankName(name string) bool {
return name == "" || name == "_"
}
func getRefSlice(pos syntax.Pos, fields []*syntax.Field) []syntax.Expr {
return doGetRefAddrSlice(pos, fields, false)
}
Expand Down
18 changes: 10 additions & 8 deletions patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,17 +765,19 @@ func generateRegHelperCode(pkgName string) string {
return strings.Join(autoGenStmts, "\n")
}

func getFieldNames(x []*syntax.Field) []string {
names := make([]string, 0, len(x))
for _, p := range x {
var name string
if p.Name != nil {
name = p.Name.Value
}
names = append(names, name)
func getFieldNames(fields []*syntax.Field) []string {
names := make([]string, 0, len(fields))
for _, field := range fields {
names = append(names, getFieldName(field))
}
return names
}
func getFieldName(f *syntax.Field) string {
if f == nil || f.Name == nil {
return ""
}
return f.Name.Value
}

func quoteNamesExpr(names []string) string {
if len(names) == 0 {
Expand Down
Loading

0 comments on commit 75c04e2

Please sign in to comment.