Skip to content

Commit

Permalink
Merge pull request #92 from tsingbx/size_t
Browse files Browse the repository at this point in the history
convert includes before convert ast file, fix size_t type not found panic, fix type redefined panic.
  • Loading branch information
luoliwoshang authored Oct 17, 2024
2 parents 980d1cc + 219dc17 commit 5bb8fc2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 7 deletions.
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
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

0 comments on commit 5bb8fc2

Please sign in to comment.