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

gogensig: add go mod init conf.Name and go get github.com/goplus/llgo #105

Merged
merged 6 commits into from
Oct 21, 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: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ _tinygo/
_output/
build.dir/
.vscode/
.devcontainer

*.cfg
*.json

# Test binary, built with `go test -c`
*.test
Expand Down
37 changes: 36 additions & 1 deletion chore/gogensig/convert/convert_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions chore/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Package struct {
p *gogen.Package
cvt *TypeConv
outputDir string
newTypes map[string]struct{} //todo(xlj): Temporary solution to avoid crashes
}

type PackageConfig struct {
Expand Down Expand Up @@ -67,6 +68,7 @@ func NewPackage(config *PackageConfig) *Package {
clib := p.p.Import("github.com/goplus/llgo/c")
typeMap := NewBuiltinTypeMapWithPkgRefS(clib, p.p.Unsafe())
p.cvt = NewConv(p.p.Types, typeMap)
p.newTypes = make(map[string]struct{}, 0)
return p
}

Expand Down Expand Up @@ -139,6 +141,7 @@ func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {
}
sig, err := p.cvt.ToSignature(funcDecl.Type)
if err != nil {
log.Printf("FuncDeclToSignature Fail: %s\n", err.Error())
return err
}
decl := p.p.NewFuncDecl(token.NoPos, string(goFuncName), sig)
Expand All @@ -150,10 +153,17 @@ func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {

// todo(zzy): for class,union,struct
func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
if typeDecl.Name == nil {
log.Printf("NewTypeDecl: %s\n", "nil")
return nil
}
name := p.cvt.RemovePrefixedName(typeDecl.Name.Name)
if _, ok := p.newTypes[name]; ok {
return nil
}
if debug {
log.Printf("NewTypeDecl: %s\n", typeDecl.Name.Name)
}
name := p.cvt.RemovePrefixedName(typeDecl.Name.Name)
typeBlock := p.p.NewTypeDefs()
typeBlock.SetComments(CommentGroup(typeDecl.Doc).CommentGroup)
decl := typeBlock.NewType(name)
Expand All @@ -162,10 +172,15 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
return err
}
decl.InitType(p.p, structType)
p.newTypes[name] = struct{}{}
return nil
}

func (p *Package) NewTypedefDecl(typedefDecl *ast.TypedefDecl) error {
name := p.cvt.RemovePrefixedName(typedefDecl.Name.Name)
if _, ok := p.newTypes[name]; ok {
return nil
}
if debug {
log.Printf("NewTypedefDecl: %s\n", typedefDecl.Name.Name)
}
Expand All @@ -178,7 +193,6 @@ func (p *Package) NewTypedefDecl(typedefDecl *ast.TypedefDecl) error {
if typ == nil {
return fmt.Errorf("underlying type must not be nil")
}
name := p.cvt.RemovePrefixedName(typedefDecl.Name.Name)
if named, ok := typ.(*types.Named); ok {
// Compare the type name with typedefDecl.Name.Name
if named.Obj().Name() == name {
Expand All @@ -188,6 +202,7 @@ func (p *Package) NewTypedefDecl(typedefDecl *ast.TypedefDecl) error {
}
typeSpecdecl := genDecl.NewType(name)
typeSpecdecl.InitType(p.p, typ)
p.newTypes[name] = struct{}{}
if _, ok := typ.(*types.Signature); ok {
genDecl.SetComments(NewTypecDocComments())
}
Expand Down
23 changes: 23 additions & 0 deletions chore/gogensig/gogensig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package main
import (
"io"
"os"
"os/exec"
"path/filepath"

"github.com/goplus/llgo/chore/gogensig/config"
"github.com/goplus/llgo/chore/gogensig/convert"
Expand All @@ -27,6 +29,23 @@ import (
"github.com/goplus/llgo/chore/gogensig/visitor"
)

func runCommand(dir, cmdName string, args ...string) error {
execCmd := exec.Command(cmdName, args...)
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr
execCmd.Dir = dir
return execCmd.Run()
}

func runGoCmds(pkg string) {
wd, _ := os.Getwd()
dir := filepath.Join(wd, pkg)
os.MkdirAll(dir, 0744)
os.Chdir(pkg)
runCommand(dir, "go", "mod", "init", pkg)
runCommand(dir, "go", "get", "github.com/goplus/llgo")
}

func main() {
var data []byte
var err error
Expand All @@ -45,9 +64,12 @@ func main() {
data, err = os.ReadFile(sigfetchFile)
}
check(err)

conf, err := config.GetCppgCfgFromPath("./llcppg.cfg")
check(err)

runGoCmds(conf.Name)

astConvert, err := convert.NewAstConvert(&convert.AstConvertConfig{
PkgName: conf.Name,
SymbFile: "./llcppg.symb.json",
Expand All @@ -60,6 +82,7 @@ func main() {
err = p.ProcessFileSet(inputdata)
check(err)
}

func check(err error) {
if err != nil {
panic(err)
Expand Down
55 changes: 51 additions & 4 deletions chore/gogensig/processor/processor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package processor

import (
"os"
"path/filepath"

"github.com/goplus/llgo/chore/gogensig/config"
"github.com/goplus/llgo/chore/gogensig/unmarshal"
"github.com/goplus/llgo/chore/gogensig/visitor"
Expand All @@ -25,17 +28,61 @@ func (p *DocVisitorManager) visit(node ast.Node, docPath string) bool {
}

type DocFileSetProcessor struct {
docVisitorList []visitor.DocVisitor
docVisitorList []visitor.DocVisitor
absPathFunc func(s string, files unmarshal.FileSet) string
visitedFile map[string]struct{}
visitedIncludeFile map[string]struct{}
}

func NewDocFileSetProcessor(docVisitorList []visitor.DocVisitor) *DocFileSetProcessor {
return &DocFileSetProcessor{docVisitorList: docVisitorList}
p := &DocFileSetProcessor{docVisitorList: docVisitorList}
p.visitedFile = make(map[string]struct{})
p.visitedIncludeFile = make(map[string]struct{})
p.SetAbsPathFunc(func(s string, fs unmarshal.FileSet) string {
if filepath.IsAbs(s) {
return s
}
includePath := fs.IncludeDir(s)
absIncludePath := filepath.Join(includePath, s)
_, err := os.Stat(absIncludePath)
if err == nil {
return absIncludePath
}
return s
})
return p
}

func (p *DocFileSetProcessor) SetAbsPathFunc(fn func(s string, files unmarshal.FileSet) string) {
p.absPathFunc = fn
}

func (p *DocFileSetProcessor) visitFile(docVisitor *DocVisitorManager, file unmarshal.FileEntry, files unmarshal.FileSet) {
if _, ok := p.visitedFile[file.Path]; ok {
return
}
if p.absPathFunc != nil {
for _, inc := range file.Doc.Includes {
if _, ok := p.visitedIncludeFile[inc.Path]; ok {
continue
}
p.visitedIncludeFile[inc.Path] = struct{}{}
absPath := p.absPathFunc(inc.Path, files)
idx := files.FindEntry(absPath)
if idx >= 0 {
findFile := files[idx]
p.visitFile(docVisitor, findFile, files)
}
}
}
docVisitor.visit(file.Doc, file.Path)
p.visitedFile[file.Path] = struct{}{}
}

func (p *DocFileSetProcessor) ProcessFileSet(files unmarshal.FileSet) error {
docVisitor := NewDocVisitorManager(p.docVisitorList)
for _, file := range files {
docVisitor := NewDocVisitorManager(p.docVisitorList)
docVisitor.visit(file.Doc, file.Path)
p.visitFile(docVisitor, file, files)
}
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions chore/gogensig/unmarshal/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package unmarshal
import (
"encoding/json"
"fmt"
"strings"

"github.com/goplus/llgo/chore/llcppg/ast"
)
Expand All @@ -13,6 +14,25 @@ var nodeUnmarshalers map[string]NodeUnmarshaler

type FileSet []FileEntry

func (s FileSet) FindEntry(absIncludePath string) int {
for i, e := range s {
if e.Path == absIncludePath {
return i
}
}
return -1
}

func (s FileSet) IncludeDir(includeFile string) string {
for _, f := range s {
after, found := strings.CutSuffix(f.Path, includeFile)
if found {
return after
}
}
return ""
}

type FileEntry struct {
Path string
Doc *ast.File
Expand Down
Loading