Skip to content

Commit

Permalink
Merge pull request #91 from tsingbx/gogensig/symbol_not_found
Browse files Browse the repository at this point in the history
ignore symbol not found error
  • Loading branch information
luoliwoshang authored Dec 25, 2024
2 parents 43aad95 + 5771942 commit 63cdaab
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 38 deletions.
11 changes: 6 additions & 5 deletions cmd/gogensig/config/symb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package config

import (
"encoding/json"
"fmt"

"github.com/goplus/llcppg/cmd/gogensig/errs"
)

type MangleNameType = string
Expand Down Expand Up @@ -46,14 +47,14 @@ func CreateSymbolTable(symbs []SymbolEntry) *SymbolTable {

func (t *SymbolTable) LookupSymbol(name MangleNameType) (*SymbolEntry, error) {
if t == nil || t.t == nil {
return nil, fmt.Errorf("symbol table not initialized")
return nil, errs.NewSymbolTableNotInitializedError()
}
if len(name) <= 0 {
return nil, fmt.Errorf("symbol not found")
if len(name) == 0 {
return nil, errs.NewSymbolNotFoudError(name)
}
symbol, ok := t.t[name]
if ok {
return &symbol, nil
}
return nil, fmt.Errorf("symbol not found")
return nil, errs.NewSymbolNotFoudError(name)
}
9 changes: 6 additions & 3 deletions cmd/gogensig/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/goplus/llcppg/ast"
cfg "github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
"github.com/goplus/llcppg/cmd/gogensig/visitor"
cppgtypes "github.com/goplus/llcppg/types"
)
Expand All @@ -32,15 +33,15 @@ func NewAstConvert(config *AstConvertConfig) (*AstConvert, error) {
p.BaseDocVisitor = visitor.NewBaseDocVisitor(p)
symbTable, err := cfg.NewSymbolTable(config.SymbFile)
if err != nil {
if debug {
if dbg.GetDebugError() {
log.Printf("Can't get llcppg.symb.json from %s Use empty table\n", config.SymbFile)
}
symbTable = cfg.CreateSymbolTable([]cfg.SymbolEntry{})
}

conf, err := cfg.GetCppgCfgFromPath(config.CfgFile)
if err != nil {
if debug {
if dbg.GetDebugError() {
log.Printf("Cant get llcppg.cfg from %s Use empty config\n", config.CfgFile)
}
conf = &cppgtypes.Config{}
Expand Down Expand Up @@ -80,7 +81,9 @@ func (p *AstConvert) WritePubFile() {
func (p *AstConvert) VisitFuncDecl(funcDecl *ast.FuncDecl) {
err := p.Pkg.NewFuncDecl(funcDecl)
if err != nil {
log.Printf("NewFuncDecl %s Fail: %s\n", funcDecl.Name.Name, err.Error())
if dbg.GetDebugError() {
log.Printf("NewFuncDecl %s Fail: %s\n", funcDecl.Name.Name, err.Error())
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/gogensig/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/convert"
"github.com/goplus/llcppg/cmd/gogensig/convert/basic"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
"github.com/goplus/llcppg/cmd/gogensig/unmarshal"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/llgo/xtool/env"
)

func init() {
convert.SetDebug(convert.DbgFlagAll)
dbg.SetDebugAll()
}

func TestFromTestdata(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/gogensig/convert/headerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/goplus/llcppg/cmd/gogensig/convert/names"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
)

type HeaderFile struct {
Expand All @@ -22,7 +23,7 @@ func (p *HeaderFile) setSysIncPath(isSys bool, incPath string) error {
return fmt.Errorf("system header file %s has no include path", p.file)
}
p.sysIncPath = incPath
if debug {
if dbg.GetDebugLog() {
log.Printf("%s is a system header file,include path: %s\n", p.file, incPath)
}
return nil
Expand Down
39 changes: 13 additions & 26 deletions cmd/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,11 @@ import (
"github.com/goplus/llcppg/ast"
cfg "github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/convert/names"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
"github.com/goplus/llcppg/cmd/gogensig/errs"
"github.com/goplus/mod/gopmod"
)

const (
DbgFlagAll = 1
)

var (
debug bool
)

func SetDebug(flags int) {
if flags != 0 {
debug = true
}
}

// In Processing Package
type Package struct {
*PkgInfo
Expand Down Expand Up @@ -117,7 +104,7 @@ func (p *Package) SetCurFile(file string, incPath string, isHeaderFile bool, inC
p.files = append(p.files, curHeaderFile)
p.curFile = curHeaderFile
fileName := p.curFile.ToGoFileName()
if debug {
if dbg.GetDebugLog() {
log.Printf("SetCurFile: %s File in Current Package: %v\n", fileName, inCurPkg)
}
if _, err := p.p.SetCurFile(fileName, true); err != nil {
Expand Down Expand Up @@ -207,12 +194,12 @@ func (p *Package) newFuncDeclAndComment(goFuncName *GoFuncName, sig *types.Signa
func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {
skip, anony, err := p.cvt.handleSysType(funcDecl.Name, funcDecl.Loc, p.curFile.sysIncPath)
if skip {
if debug {
if dbg.GetDebugLog() {
log.Printf("NewFuncDecl: %v is a function of system header file\n", funcDecl.Name)
}
return err
}
if debug {
if dbg.GetDebugLog() {
log.Printf("NewFuncDecl: %v\n", funcDecl.Name)
}
if anony {
Expand Down Expand Up @@ -242,16 +229,16 @@ func (p *Package) NewFuncDecl(funcDecl *ast.FuncDecl) error {
func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
skip, anony, err := p.cvt.handleSysType(typeDecl.Name, typeDecl.Loc, p.curFile.sysIncPath)
if skip {
if debug {
if dbg.GetDebugLog() {
log.Printf("NewTypeDecl: %s type of system header\n", typeDecl.Name)
}
return err
}
if debug {
if dbg.GetDebugLog() {
log.Printf("NewTypeDecl: %v\n", typeDecl.Name)
}
if anony {
if debug {
if dbg.GetDebugLog() {
log.Println("NewTypeDecl:Skip a anonymous type")
}
return nil
Expand Down Expand Up @@ -326,12 +313,12 @@ func (p *Package) emptyTypeDecl(name string, doc *ast.CommentGroup) *gogen.TypeD
func (p *Package) NewTypedefDecl(typedefDecl *ast.TypedefDecl) error {
skip, _, err := p.cvt.handleSysType(typedefDecl.Name, typedefDecl.Loc, p.curFile.sysIncPath)
if skip {
if debug {
if dbg.GetDebugLog() {
log.Printf("NewTypedefDecl: %v is a typedef of system header file\n", typedefDecl.Name)
}
return err
}
if debug {
if dbg.GetDebugLog() {
log.Printf("NewTypedefDecl: %v\n", typedefDecl.Name)
}
name, changed, err := p.DeclName(typedefDecl.Name.Name)
Expand Down Expand Up @@ -408,12 +395,12 @@ func (p *Package) NewTypedefs(name string, typ types.Type) *gogen.TypeDecl {
func (p *Package) NewEnumTypeDecl(enumTypeDecl *ast.EnumTypeDecl) error {
skip, _, err := p.cvt.handleSysType(enumTypeDecl.Name, enumTypeDecl.Loc, p.curFile.sysIncPath)
if skip {
if debug {
if dbg.GetDebugLog() {
log.Printf("NewEnumTypeDecl: %v is a enum type of system header file\n", enumTypeDecl.Name)
}
return err
}
if debug {
if dbg.GetDebugLog() {
log.Printf("NewEnumTypeDecl: %v\n", enumTypeDecl.Name)
}
enumType, enumTypeName, err := p.createEnumType(enumTypeDecl.Name)
Expand Down Expand Up @@ -511,7 +498,7 @@ func (p *Package) WritePkgFiles() error {
func (p *Package) Write(headerFile string) error {
fileName := names.HeaderFileToGo(headerFile)
filePath := filepath.Join(p.GetOutputDir(), fileName)
if debug {
if dbg.GetDebugLog() {
log.Printf("Write HeaderFile [%s] from gogen:[%s] to [%s]\n", headerFile, fileName, filePath)
}
return p.writeToFile(fileName, filePath)
Expand All @@ -525,7 +512,7 @@ func (p *Package) WriteLinkFile() (string, error) {
return "", fmt.Errorf("failed to set current file: %w", err)
}
err = p.linkLib(p.conf.CppgConf.Libs)
if debug {
if dbg.GetDebugLog() {
log.Printf("Write LinkFile [%s] from gogen:[%s] to [%s]\n", fileName, fileName, filePath)
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/gogensig/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
cfg "github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/convert"
"github.com/goplus/llcppg/cmd/gogensig/convert/names"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
cppgtypes "github.com/goplus/llcppg/types"
"github.com/goplus/mod/gopmod"
)

var dir string

func init() {
convert.SetDebug(convert.DbgFlagAll)
dbg.SetDebugAll()
var err error
dir, err = os.Getwd()
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions cmd/gogensig/dbg/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dbg

type dbgFlags = int

var flags dbgFlags

const (
DbgSymbolNotFound dbgFlags = 1 << iota
DbgError // print when error ocur
DbgLog // print log info
DbgFlagAll = 0 | DbgError | DbgLog
)

func SetDebugSymbolNotFound() {
flags |= DbgSymbolNotFound
}

func GetDebugSymbolNotFound() bool {
return flags&DbgSymbolNotFound != 0
}

func SetDebugError() {
flags |= DbgError
}

func GetDebugError() bool {
return flags&DbgError != 0
}

func SetDebugLog() {
flags |= DbgLog
}

func GetDebugLog() bool {
return flags&DbgLog != 0
}

func SetDebugAll() {
flags = DbgFlagAll
}
28 changes: 28 additions & 0 deletions cmd/gogensig/errs/symbol_not_found_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package errs

import (
"fmt"
)

type SymbolNotFoudError struct {
name string
}

func (p *SymbolNotFoudError) Error() string {
return fmt.Sprintf("%s symbol not found", p.name)
}

func NewSymbolNotFoudError(name string) *SymbolNotFoudError {
return &SymbolNotFoudError{name: name}
}

type SymbolTableNotInitializedError struct {
}

func (p *SymbolTableNotInitializedError) Error() string {
return "symbol table not initialized"
}

func NewSymbolTableNotInitializedError() *SymbolTableNotInitializedError {
return &SymbolTableNotInitializedError{}
}
3 changes: 2 additions & 1 deletion cmd/gogensig/gogensig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/convert"
"github.com/goplus/llcppg/cmd/gogensig/convert/basic"
"github.com/goplus/llcppg/cmd/gogensig/dbg"
"github.com/goplus/llcppg/cmd/gogensig/unmarshal"
)

Expand All @@ -47,7 +48,7 @@ func main() {
}

if ags.Verbose {
convert.SetDebug(convert.DbgFlagAll)
dbg.SetDebugAll()
}

var data []byte
Expand Down

0 comments on commit 63cdaab

Please sign in to comment.