From 33af9e878b83314b955410fdf0b7e9e7f088948a Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 8 Aug 2024 15:59:08 +0800 Subject: [PATCH 01/67] llcppsigfech:basic struct llcppsigfetch:basic parse converter llcppsigfetch:converter top decl llcppg:converter test --- chore/_xtool/llcppsigfetch/llcppsigfetch.go | 61 +++++++++++- chore/_xtool/llcppsigfetch/parse/cvt.go | 98 +++++++++++++++++++ .../llcppsigfetch/parse/cvt_test/cvt.go | 24 +++++ chore/_xtool/llcppsigfetch/parse/parse.go | 60 ++++++++++++ 4 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go create mode 100644 chore/_xtool/llcppsigfetch/parse/parse.go diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 30190a18..19746b09 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -16,6 +16,65 @@ package main +import ( + "fmt" + "io" + "os" + "path/filepath" + "strings" + + "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" + "github.com/goplus/llgo/chore/_xtool/llcppsymg/config" +) + func main() { - // TODO(xsw): implement llcppsigfetch tool + cfgFile := "llcppg.cfg" + if len(os.Args) > 1 { + cfgFile = os.Args[1] + } + + var data []byte + var err error + if cfgFile == "-" { + data, err = io.ReadAll(os.Stdin) + } else { + data, err = os.ReadFile(cfgFile) + } + check(err) + + conf, err := config.GetConf(data) + check(err) + defer conf.Delete() + + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to parse config file:", cfgFile) + } + + files := getHeaderFiles(conf.CFlags, conf.Include) + + context := parse.NewContext() + err = context.ProcessFiles(files) + check(err) + + outputInfo(context) +} + +func check(err error) { + if err != nil { + panic(err) + } +} + +func getHeaderFiles(cflags string, files []string) []string { + prefix := cflags + prefix = strings.TrimPrefix(prefix, "-I") + var paths []string + for _, f := range files { + paths = append(paths, filepath.Join(prefix, f)) + } + return paths +} + +func outputInfo(context *parse.Context) { + // TODO(zzy): } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go new file mode 100644 index 00000000..806db4ee --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -0,0 +1,98 @@ +package parse + +import ( + "errors" + "fmt" + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/clang" + "github.com/goplus/llgo/chore/llcppg/ast" +) + +type Converter struct { + files map[string]*ast.File + typeMap map[clang.Type]*ast.Expr + declMap map[clang.Cursor]*ast.Decl + curLoc ast.Location + index *clang.Index + unit *clang.TranslationUnit +} + +func NewConverter(filepath string) (*Converter, error) { + args := []*c.Char{ + c.Str("-x"), + c.Str("c++"), + c.Str("-std=c++11"), + } + index := clang.CreateIndex(0, 0) + unit := index.ParseTranslationUnit( + c.AllocaCStr(filepath), + unsafe.SliceData(args), 3, + nil, 0, + clang.DetailedPreprocessingRecord, + ) + if unit == nil { + return nil, errors.New("failed to parse translation unit") + } + + return &Converter{ + typeMap: make(map[clang.Type]*ast.Expr), + declMap: make(map[clang.Cursor]*ast.Decl), + files: make(map[string]*ast.File), + index: index, + unit: unit, + }, nil +} + +func (ct *Converter) Dispose() { + ct.index.Dispose() + ct.unit.Dispose() +} + +// visit top decls (struct,class,function,enum & marco,include) +func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + ct := (*Converter)(clientData) + switch cursor.Kind { + case clang.CursorInclusionDirective: + fmt.Println("todo: Process include") + return clang.ChildVisit_Continue + case clang.CursorMacroDefinition: + fmt.Println("todo: Process macro") + return clang.ChildVisit_Continue + case clang.CursorEnumDecl: + fmt.Println("todo: Process enum") + return clang.ChildVisit_Continue + case clang.CursorClassDecl: + ct.ProcessClass(cursor) + return clang.ChildVisit_Continue + case clang.CursorStructDecl: + fmt.Println("todo: Process struct") + return clang.ChildVisit_Continue + case clang.CursorFunctionDecl: + ct.ProcessFunc(cursor) + return clang.ChildVisit_Continue + default: + // non-top-level decl, continue recursion + return clang.ChildVisit_Recurse + } +} + +func (ct *Converter) ProcessFunc(cursor clang.Cursor) { + println("todo: Process func") +} + +func (ct *Converter) ProcessClass(cursor clang.Cursor) { + println("todo: Process class") +} + +func (ct *Converter) Convert() (map[string]*ast.File, error) { + cursor := ct.unit.Cursor() + // visit top decls (struct,class,function & marco,include) + clang.VisitChildren(cursor, visit, c.Pointer(ct)) + return nil, nil +} + +func (ct *Converter) UpdateLocation(loc ast.Location) { + ct.curLoc = loc +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go new file mode 100644 index 00000000..34358743 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" +) + +func main() { + if c.Argc != 2 { + fmt.Fprintln(os.Stderr, "Usage: cvt ") + return + } + sourceFile := *c.Advance(c.Argv, 1) + c.Printf(c.Str("sourceFile: %s\n"), sourceFile) + converter, err := parse.NewConverter(c.GoString(sourceFile)) + if err != nil { + panic(err) + } + defer converter.Dispose() + converter.Convert() +} diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go new file mode 100644 index 00000000..a024686c --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -0,0 +1,60 @@ +package parse + +import ( + "errors" + + "github.com/goplus/llgo/chore/llcppg/ast" +) + +type Context struct { + Files map[string]*ast.File +} + +func NewContext() *Context { + return &Context{ + Files: make(map[string]*ast.File), + } +} + +// ProcessFiles processes the given files and adds them to the context +func (p *Context) ProcessFiles(files []string) error { + for _, file := range files { + if err := p.processFile(file); err != nil { + return err + } + } + return nil +} + +// parse file and add it to the context,avoid duplicate parsing +func (p *Context) processFile(path string) error { + if _, exists := p.Files[path]; exists { + return nil + } + parsedfiles, err := p.parseFile(path) + if err != nil { + return errors.New("failed to parse file: " + path) + } + + for path, file := range parsedfiles { + if _, exist := p.Files[path]; !exist { + p.Files[path] = file + } + } + return nil +} + +func (p *Context) parseFile(path string) (map[string]*ast.File, error) { + converter, err := NewConverter(path) + if err != nil { + return nil, errors.New("failed to create converter " + path) + } + defer converter.Dispose() + + files, err := converter.Convert() + if err != nil { + return nil, err + } + + return files, nil +} From 364d3996f4801310de589123b251b008b0b75dfe Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 9 Aug 2024 19:13:20 +0800 Subject: [PATCH 02/67] llcppsigfetch:basic process type --- chore/_xtool/llcppsigfetch/parse/cvt.go | 65 +++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 806db4ee..ac04b414 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -12,11 +12,13 @@ import ( type Converter struct { files map[string]*ast.File - typeMap map[clang.Type]*ast.Expr - declMap map[clang.Cursor]*ast.Decl + typeMap map[clang.Type]ast.Expr // todo(zzy):maybe a other map key for typemap is better + declMap map[clang.Cursor]ast.Decl curLoc ast.Location + curFile *ast.File index *clang.Index unit *clang.TranslationUnit + // todo(zzy):current namespace expr } func NewConverter(filepath string) (*Converter, error) { @@ -37,8 +39,8 @@ func NewConverter(filepath string) (*Converter, error) { } return &Converter{ - typeMap: make(map[clang.Type]*ast.Expr), - declMap: make(map[clang.Cursor]*ast.Decl), + typeMap: make(map[clang.Type]ast.Expr), + declMap: make(map[clang.Cursor]ast.Decl), files: make(map[string]*ast.File), index: index, unit: unit, @@ -52,6 +54,7 @@ func (ct *Converter) Dispose() { // visit top decls (struct,class,function,enum & marco,include) func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + // todo(zzy): set current file ct := (*Converter)(clientData) switch cursor.Kind { case clang.CursorInclusionDirective: @@ -78,8 +81,56 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi } } +func (ct *Converter) ProcessType(t clang.Type) ast.Expr { + // todo(zzy):a other map key for typemap + if cache, ok := ct.typeMap[t]; ok { + return cache + } + var expr ast.Expr + switch t.Kind { + case clang.TypePointer: + expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} + case clang.TypeFunctionProto: + ret := ct.ProcessType(t.ResultType()) + params := &ast.FieldList{} + for i := 0; i < int(t.NumArgTypes()); i++ { + argType := ct.ProcessType(t.ArgType(c.Uint(i))) + params.List = append(params.List, &ast.Field{Type: argType}) + // todo(zzy):field name + } + expr = &ast.FuncType{Params: params, Ret: ret} + case clang.TypeTypedef: + expr = ct.ProcessType(t.CanonicalType()) + case clang.TypeConstantArray, clang.TypeVariableArray, clang.TypeIncompleteArray, clang.TypeDependentSizedArray: + expr = &ast.ArrayType{ + Elt: ct.ProcessType(t.ArrayElementType()), + } + // todo(zzy):array length + } + ct.typeMap[t] = expr + return expr +} + func (ct *Converter) ProcessFunc(cursor clang.Cursor) { - println("todo: Process func") + name := cursor.String() + defer name.Dispose() + + funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType) + if !ok { + fmt.Println("failed to process function type") + return + } + + fn := &ast.FuncDecl{ + Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Type: funcType, + // todo(zzy):DeclBase use the converter's current namespace expr + } + + decls := ct.GetCurFile() + decls.Decls = append(decls.Decls, fn) + + ct.declMap[cursor] = fn } func (ct *Converter) ProcessClass(cursor clang.Cursor) { @@ -96,3 +147,7 @@ func (ct *Converter) Convert() (map[string]*ast.File, error) { func (ct *Converter) UpdateLocation(loc ast.Location) { ct.curLoc = loc } + +func (ct *Converter) GetCurFile() *ast.File { + return ct.curFile +} From 95dc01cdcb076fbc1ad1e509686e8d7140711c7d Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sun, 11 Aug 2024 19:39:33 +0800 Subject: [PATCH 03/67] llcppsigfetch:basic fileset --- chore/_xtool/llcppsigfetch/parse/cvt.go | 89 +++++++++++++++---------- chore/llcppg/ast/ast.go | 1 + 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index ac04b414..009d9048 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -11,9 +11,9 @@ import ( ) type Converter struct { - files map[string]*ast.File - typeMap map[clang.Type]ast.Expr // todo(zzy):maybe a other map key for typemap is better - declMap map[clang.Cursor]ast.Decl + files map[string]*ast.File + // typeMap map[clang.Type]ast.Expr // todo(zzy):cache type + // declMap map[clang.Cursor]ast.Decl curLoc ast.Location curFile *ast.File index *clang.Index @@ -39,11 +39,11 @@ func NewConverter(filepath string) (*Converter, error) { } return &Converter{ - typeMap: make(map[clang.Type]ast.Expr), - declMap: make(map[clang.Cursor]ast.Decl), - files: make(map[string]*ast.File), - index: index, - unit: unit, + // typeMap: make(map[clang.Type]ast.Expr), + // declMap: make(map[clang.Cursor]ast.Decl), + files: make(map[string]*ast.File), + index: index, + unit: unit, }, nil } @@ -54,8 +54,9 @@ func (ct *Converter) Dispose() { // visit top decls (struct,class,function,enum & marco,include) func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { - // todo(zzy): set current file ct := (*Converter)(clientData) + ct.UpdateCurFile(cursor) + switch cursor.Kind { case clang.CursorInclusionDirective: fmt.Println("todo: Process include") @@ -81,11 +82,47 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi } } -func (ct *Converter) ProcessType(t clang.Type) ast.Expr { - // todo(zzy):a other map key for typemap - if cache, ok := ct.typeMap[t]; ok { - return cache +func (ct *Converter) Convert() (map[string]*ast.File, error) { + cursor := ct.unit.Cursor() + // visit top decls (struct,class,function & marco,include) + clang.VisitChildren(cursor, visit, c.Pointer(ct)) + return nil, nil +} + +func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { + loc := cursor.Location() + var file clang.File + loc.SpellingLocation(&file, nil, nil, nil) + filename := file.FileName() + defer filename.Dispose() + + if filename.CStr() == nil { + // For some built-in macros, there is no file. + println("todo: filename is empty") + return } + + filePath := c.GoString(filename.CStr()) + if ct.curFile == nil || ct.curFile.Path != filePath { + if f, ok := ct.files[filePath]; ok { + ct.curFile = f + } else { + ct.curFile = &ast.File{ + Path: filePath, + Decls: make([]ast.Decl, 0), + Includes: make([]*ast.Include, 0), + Macros: make([]*ast.Macro, 0), + } + } + ct.files[filePath] = ct.curFile + } +} + +func (ct *Converter) ProcessType(t clang.Type) ast.Expr { + // todo(zzy):cache type + // if cache, ok := ct.typeMap[t]; ok { + // return cache + // } var expr ast.Expr switch t.Kind { case clang.TypePointer: @@ -107,47 +144,27 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { } // todo(zzy):array length } - ct.typeMap[t] = expr + // ct.typeMap[t] = expr return expr } func (ct *Converter) ProcessFunc(cursor clang.Cursor) { name := cursor.String() defer name.Dispose() - funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType) if !ok { fmt.Println("failed to process function type") return } - fn := &ast.FuncDecl{ Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: funcType, // todo(zzy):DeclBase use the converter's current namespace expr } - - decls := ct.GetCurFile() - decls.Decls = append(decls.Decls, fn) - - ct.declMap[cursor] = fn + ct.curFile.Decls = append(ct.curFile.Decls, fn) + // ct.declMap[cursor] = fn } func (ct *Converter) ProcessClass(cursor clang.Cursor) { println("todo: Process class") } - -func (ct *Converter) Convert() (map[string]*ast.File, error) { - cursor := ct.unit.Cursor() - // visit top decls (struct,class,function & marco,include) - clang.VisitChildren(cursor, visit, c.Pointer(ct)) - return nil, nil -} - -func (ct *Converter) UpdateLocation(loc ast.Location) { - ct.curLoc = loc -} - -func (ct *Converter) GetCurFile() *ast.File { - return ct.curFile -} diff --git a/chore/llcppg/ast/ast.go b/chore/llcppg/ast/ast.go index 46c16dc3..6f924a4a 100644 --- a/chore/llcppg/ast/ast.go +++ b/chore/llcppg/ast/ast.go @@ -370,6 +370,7 @@ func (*Macro) ppdNode() {} // ------------------------------------------------ type File struct { + Path string `json:"path"` Decls []Decl `json:"decls"` Includes []*Include `json:"includes,omitempty"` Macros []*Macro `json:"macros,omitempty"` From 0de9c57adee834ce612333eade53f25ae426642f Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 12 Aug 2024 12:07:45 +0800 Subject: [PATCH 04/67] llcppsigfetch:builtin type convert --- chore/_xtool/llcppsigfetch/parse/cvt.go | 73 +++++++++++++++++++ .../llcppsigfetch/parse/cvt_test/cvt.go | 62 ++++++++++++---- .../llcppsigfetch/parse/cvt_test/llgo.expect | 30 ++++++++ 3 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 009d9048..2861d772 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -3,6 +3,7 @@ package parse import ( "errors" "fmt" + "os" "unsafe" "github.com/goplus/llgo/c" @@ -124,6 +125,9 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { // return cache // } var expr ast.Expr + if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { + return ct.ProcessBuiltinType(t) + } switch t.Kind { case clang.TypePointer: expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} @@ -168,3 +172,72 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) { func (ct *Converter) ProcessClass(cursor clang.Cursor) { println("todo: Process class") } + +func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { + kind := ast.Void + var flags ast.TypeFlag + + switch t.Kind { + case clang.TypeVoid: + kind = ast.Void + case clang.TypeBool: + kind = ast.Bool + case clang.TypeCharU, clang.TypeUChar, clang.TypeCharS, clang.TypeSChar: + kind = ast.Char + case clang.TypeChar16: + kind = ast.Char16 + case clang.TypeChar32: + kind = ast.Char32 + case clang.TypeWChar: + kind = ast.WChar + case clang.TypeShort, clang.TypeUShort: + kind = ast.Int + flags |= ast.Short + case clang.TypeInt, clang.TypeUInt: + kind = ast.Int + case clang.TypeLong, clang.TypeULong: + kind = ast.Int + flags |= ast.Long + case clang.TypeLongLong, clang.TypeULongLong: + kind = ast.Int + flags |= ast.LongLong + case clang.TypeInt128, clang.TypeUInt128: + kind = ast.Int128 + case clang.TypeFloat: + kind = ast.Float + case clang.TypeHalf, clang.TypeFloat16: + kind = ast.Float16 + case clang.TypeDouble: + kind = ast.Float + flags |= ast.Double + case clang.TypeLongDouble: + kind = ast.Float + flags |= ast.Long | ast.Double + case clang.TypeFloat128: + kind = ast.Float128 + default: + // like IBM128,NullPtr,Accum + fmt.Fprintln(os.Stderr, "todo: unknown builtin type:", c.GoString(t.Kind.String().CStr())) + } + + if IsExplicitSigned(t) { + flags |= ast.Signed + } else if IsExplicitUnsigned(t) { + flags |= ast.Unsigned + } + + return &ast.BuiltinType{ + Kind: kind, + Flags: flags, + } +} +func IsExplicitSigned(t clang.Type) bool { + return t.Kind == clang.TypeCharS || t.Kind == clang.TypeSChar +} + +func IsExplicitUnsigned(t clang.Type) bool { + return t.Kind == clang.TypeCharU || t.Kind == clang.TypeUChar || + t.Kind == clang.TypeUShort || t.Kind == clang.TypeUInt || + t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong || + t.Kind == clang.TypeUInt128 +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go index 34358743..29d985cb 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go @@ -2,23 +2,57 @@ package main import ( "fmt" - "os" - "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" + "github.com/goplus/llgo/chore/llcppg/ast" ) func main() { - if c.Argc != 2 { - fmt.Fprintln(os.Stderr, "Usage: cvt ") - return - } - sourceFile := *c.Advance(c.Argv, 1) - c.Printf(c.Str("sourceFile: %s\n"), sourceFile) - converter, err := parse.NewConverter(c.GoString(sourceFile)) - if err != nil { - panic(err) - } - defer converter.Dispose() - converter.Convert() + TestBuiltinType() +} +func TestBuiltinType() { + tests := []struct { + name string + typeKind clang.TypeKind + expected ast.BuiltinType + }{ + {"Void", clang.TypeVoid, ast.BuiltinType{Kind: ast.Void}}, + {"Bool", clang.TypeBool, ast.BuiltinType{Kind: ast.Bool}}, + {"Char_S", clang.TypeCharS, ast.BuiltinType{Kind: ast.Char, Flags: ast.Signed}}, + {"Char_U", clang.TypeCharU, ast.BuiltinType{Kind: ast.Char, Flags: ast.Unsigned}}, + {"Char16", clang.TypeChar16, ast.BuiltinType{Kind: ast.Char16}}, + {"Char32", clang.TypeChar32, ast.BuiltinType{Kind: ast.Char32}}, + {"WChar", clang.TypeWChar, ast.BuiltinType{Kind: ast.WChar}}, + {"Short", clang.TypeShort, ast.BuiltinType{Kind: ast.Int, Flags: ast.Short}}, + {"UShort", clang.TypeUShort, ast.BuiltinType{Kind: ast.Int, Flags: ast.Short | ast.Unsigned}}, + {"Int", clang.TypeInt, ast.BuiltinType{Kind: ast.Int}}, + {"UInt", clang.TypeUInt, ast.BuiltinType{Kind: ast.Int, Flags: ast.Unsigned}}, + {"Long", clang.TypeLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.Long}}, + {"ULong", clang.TypeULong, ast.BuiltinType{Kind: ast.Int, Flags: ast.Long | ast.Unsigned}}, + {"LongLong", clang.TypeLongLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong}}, + {"ULongLong", clang.TypeULongLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong | ast.Unsigned}}, + {"Int128", clang.TypeInt128, ast.BuiltinType{Kind: ast.Int128}}, + {"UInt128", clang.TypeUInt128, ast.BuiltinType{Kind: ast.Int128, Flags: ast.Unsigned}}, + {"Float", clang.TypeFloat, ast.BuiltinType{Kind: ast.Float}}, + {"Half", clang.TypeHalf, ast.BuiltinType{Kind: ast.Float16}}, + {"Float16", clang.TypeFloat16, ast.BuiltinType{Kind: ast.Float16}}, + {"Double", clang.TypeDouble, ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}}, + {"LongDouble", clang.TypeLongDouble, ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}}, + {"Float128", clang.TypeFloat128, ast.BuiltinType{Kind: ast.Float128}}, + {"Unknown", clang.TypeIbm128, ast.BuiltinType{Kind: ast.Void}}, + } + converter := &parse.Converter{} + converter.Convert() + for _, bt := range tests { + t := clang.Type{Kind: bt.typeKind} + res := converter.ProcessBuiltinType(t) + if res.Kind != bt.expected.Kind { + fmt.Printf("%s Kind mismatch:got %d want %d, \n", bt.name, res.Kind, bt.expected.Kind) + } + if res.Flags != bt.expected.Flags { + fmt.Printf("%s Flags mismatch:got %d,want %d\n", bt.name, res.Flags, bt.expected.Flags) + } + fmt.Printf("%s:flags:%d kind:%d\n", bt.name, res.Flags, res.Kind) + } } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect new file mode 100644 index 00000000..9d5fac9c --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect @@ -0,0 +1,30 @@ +#stdout +Void:flags:0 kind:0 +Bool:flags:0 kind:1 +Char_S:flags:1 kind:2 +Char_U:flags:2 kind:2 +Char16:flags:0 kind:3 +Char32:flags:0 kind:4 +WChar:flags:0 kind:5 +Short:flags:32 kind:6 +UShort:flags:34 kind:6 +Int:flags:0 kind:6 +UInt:flags:2 kind:6 +Long:flags:4 kind:6 +ULong:flags:6 kind:6 +LongLong:flags:8 kind:6 +ULongLong:flags:10 kind:6 +Int128:flags:0 kind:7 +UInt128:flags:2 kind:7 +Float:flags:0 kind:8 +Half:flags:0 kind:9 +Float16:flags:0 kind:9 +Double:flags:16 kind:8 +LongDouble:flags:20 kind:8 +Float128:flags:0 kind:10 +Unknown:flags:0 kind:0 + +#stderr +todo: unknown builtin type: Ibm128 + +#exit 0 From a796f9f8a8f5e7d48226ca97ddc440b35bc23e7e Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 12 Aug 2024 16:28:28 +0800 Subject: [PATCH 05/67] llcppsigfetch:tempfile converter option --- chore/_xtool/llcppsigfetch/parse/cvt.go | 35 +++++++++++++++++------ chore/_xtool/llcppsigfetch/parse/parse.go | 2 +- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 2861d772..2588b760 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -22,19 +22,38 @@ type Converter struct { // todo(zzy):current namespace expr } -func NewConverter(filepath string) (*Converter, error) { +func NewConverter(file string, temp bool) (*Converter, error) { args := []*c.Char{ c.Str("-x"), c.Str("c++"), c.Str("-std=c++11"), } index := clang.CreateIndex(0, 0) - unit := index.ParseTranslationUnit( - c.AllocaCStr(filepath), - unsafe.SliceData(args), 3, - nil, 0, - clang.DetailedPreprocessingRecord, - ) + + var unit *clang.TranslationUnit + + if temp { + content := c.AllocaCStr(file) + tempFile := &clang.UnsavedFile{ + Filename: c.Str("temp.h"), + Contents: content, + Length: c.Ulong(c.Strlen(content)), + } + unit = index.ParseTranslationUnit( + tempFile.Filename, + unsafe.SliceData(args), c.Int(len(args)), + tempFile, 1, + clang.DetailedPreprocessingRecord, + ) + } else { + unit = index.ParseTranslationUnit( + c.AllocaCStr(file), + unsafe.SliceData(args), c.Int(len(args)), + nil, 0, + clang.DetailedPreprocessingRecord, + ) + } + if unit == nil { return nil, errors.New("failed to parse translation unit") } @@ -87,7 +106,7 @@ func (ct *Converter) Convert() (map[string]*ast.File, error) { cursor := ct.unit.Cursor() // visit top decls (struct,class,function & marco,include) clang.VisitChildren(cursor, visit, c.Pointer(ct)) - return nil, nil + return ct.files, nil } func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index a024686c..ce36a352 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -45,7 +45,7 @@ func (p *Context) processFile(path string) error { } func (p *Context) parseFile(path string) (map[string]*ast.File, error) { - converter, err := NewConverter(path) + converter, err := NewConverter(path, false) if err != nil { return nil, errors.New("failed to create converter " + path) } From 6297f69e703e6713001de562a08021c86e0a88e1 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 12 Aug 2024 18:20:15 +0800 Subject: [PATCH 06/67] llcppsigfetch:basic cjson dump for ast.File --- chore/_xtool/llcppsigfetch/parse/cvt.go | 74 +++++++++++++++++-- .../parse/cvt_test/decl_test/decl.go | 26 +++++++ .../parse/cvt_test/decl_test/llgo.expect | 30 ++++++++ 3 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 2588b760..de9965d0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -7,6 +7,7 @@ import ( "unsafe" "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/llcppg/ast" ) @@ -79,19 +80,19 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi switch cursor.Kind { case clang.CursorInclusionDirective: - fmt.Println("todo: Process include") + // todo(zzy) return clang.ChildVisit_Continue case clang.CursorMacroDefinition: - fmt.Println("todo: Process macro") + // todo(zzy) return clang.ChildVisit_Continue case clang.CursorEnumDecl: - fmt.Println("todo: Process enum") + // todo(zzy) return clang.ChildVisit_Continue case clang.CursorClassDecl: ct.ProcessClass(cursor) return clang.ChildVisit_Continue case clang.CursorStructDecl: - fmt.Println("todo: Process struct") + // todo(zzy) return clang.ChildVisit_Continue case clang.CursorFunctionDecl: ct.ProcessFunc(cursor) @@ -117,8 +118,7 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { defer filename.Dispose() if filename.CStr() == nil { - // For some built-in macros, there is no file. - println("todo: filename is empty") + //todo(zzy): For some built-in macros, there is no file. return } @@ -260,3 +260,65 @@ func IsExplicitUnsigned(t clang.Type) bool { t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong || t.Kind == clang.TypeUInt128 } + +func (ct *Converter) GetFilesJSON() *cjson.JSON { + root := cjson.Object() + + Files := cjson.Object() + root.SetItem(c.Str("Files"), Files) + + for _, file := range ct.files { + f := cjson.Object() + f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path))) + ct.FileJSON(file, f) + Files.SetItem(c.AllocaCStr(file.Path), f) + } + return root +} + +func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) { + decls := cjson.Array() + includes := cjson.Array() + macros := cjson.Array() + + for _, decl := range file.Decls { + ct.DeclJSON(decl, decls) + } + + root.SetItem(c.Str("decls"), decls) + root.SetItem(c.Str("includes"), includes) + root.SetItem(c.Str("macros"), macros) +} + +func (ct *Converter) DeclJSON(decl ast.Decl, root *cjson.JSON) { + switch d := decl.(type) { + case *ast.FuncDecl: + fn := cjson.Object() + fntype := cjson.Object() + fn.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name.Name))) + ct.TypeJSON(d.Type, fntype) + fn.SetItem(c.Str("Type"), fntype) + root.AddItem(fn) + } +} +func (ct *Converter) TypeJSON(t ast.Expr, root *cjson.JSON) { + + switch d := t.(type) { + case *ast.FuncType: + params := cjson.Array() + + for _, p := range d.Params.List { + param := cjson.Object() + ct.TypeJSON(p.Type, param) + params.AddItem(param) + } + + root.SetItem(c.Str("Params"), params) + ret := cjson.Object() + ct.TypeJSON(d.Ret, ret) + root.SetItem(c.Str("Ret"), ret) + case *ast.BuiltinType: + root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) + root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go new file mode 100644 index 00000000..3302084e --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" +) + +func main() { + TestFuncDecl() +} + +func TestFuncDecl() { + content := `int foo(int a, int b);` + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + defer converter.Dispose() + converter.Convert() + if err != nil { + panic(err) + } + json := converter.GetFilesJSON() + c.Printf(c.Str("%s\n"), json.Print()) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect new file mode 100644 index 00000000..6300d577 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -0,0 +1,30 @@ +#stdout +{ + "Files": { + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Name": "foo", + "Type": { + "Params": [{ + "Kind": 6, + "Flags": 0 + }, { + "Kind": 6, + "Flags": 0 + }], + "Ret": { + "Kind": 6, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } + } +} + +#stderr + +#exit 0 From 1996db4b95b9de7ec1b48f27e25e7a97b00091d4 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 13 Aug 2024 17:05:20 +0800 Subject: [PATCH 07/67] llcppsigfetch:refine cjson dump logic --- chore/_xtool/llcppsigfetch/parse/cvt.go | 74 +-------------- chore/_xtool/llcppsigfetch/parse/dump.go | 111 +++++++++++++++++++++++ 2 files changed, 112 insertions(+), 73 deletions(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/dump.go diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index de9965d0..94e4ff32 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -7,15 +7,12 @@ import ( "unsafe" "github.com/goplus/llgo/c" - "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/llcppg/ast" ) type Converter struct { - files map[string]*ast.File - // typeMap map[clang.Type]ast.Expr // todo(zzy):cache type - // declMap map[clang.Cursor]ast.Decl + files map[string]*ast.File curLoc ast.Location curFile *ast.File index *clang.Index @@ -60,8 +57,6 @@ func NewConverter(file string, temp bool) (*Converter, error) { } return &Converter{ - // typeMap: make(map[clang.Type]ast.Expr), - // declMap: make(map[clang.Cursor]ast.Decl), files: make(map[string]*ast.File), index: index, unit: unit, @@ -139,10 +134,6 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { } func (ct *Converter) ProcessType(t clang.Type) ast.Expr { - // todo(zzy):cache type - // if cache, ok := ct.typeMap[t]; ok { - // return cache - // } var expr ast.Expr if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { return ct.ProcessBuiltinType(t) @@ -167,7 +158,6 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { } // todo(zzy):array length } - // ct.typeMap[t] = expr return expr } @@ -260,65 +250,3 @@ func IsExplicitUnsigned(t clang.Type) bool { t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong || t.Kind == clang.TypeUInt128 } - -func (ct *Converter) GetFilesJSON() *cjson.JSON { - root := cjson.Object() - - Files := cjson.Object() - root.SetItem(c.Str("Files"), Files) - - for _, file := range ct.files { - f := cjson.Object() - f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path))) - ct.FileJSON(file, f) - Files.SetItem(c.AllocaCStr(file.Path), f) - } - return root -} - -func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) { - decls := cjson.Array() - includes := cjson.Array() - macros := cjson.Array() - - for _, decl := range file.Decls { - ct.DeclJSON(decl, decls) - } - - root.SetItem(c.Str("decls"), decls) - root.SetItem(c.Str("includes"), includes) - root.SetItem(c.Str("macros"), macros) -} - -func (ct *Converter) DeclJSON(decl ast.Decl, root *cjson.JSON) { - switch d := decl.(type) { - case *ast.FuncDecl: - fn := cjson.Object() - fntype := cjson.Object() - fn.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name.Name))) - ct.TypeJSON(d.Type, fntype) - fn.SetItem(c.Str("Type"), fntype) - root.AddItem(fn) - } -} -func (ct *Converter) TypeJSON(t ast.Expr, root *cjson.JSON) { - - switch d := t.(type) { - case *ast.FuncType: - params := cjson.Array() - - for _, p := range d.Params.List { - param := cjson.Object() - ct.TypeJSON(p.Type, param) - params.AddItem(param) - } - - root.SetItem(c.Str("Params"), params) - ret := cjson.Object() - ct.TypeJSON(d.Ret, ret) - root.SetItem(c.Str("Ret"), ret) - case *ast.BuiltinType: - root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) - root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) - } -} diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go new file mode 100644 index 00000000..7c1dc9d7 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -0,0 +1,111 @@ +package parse + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" + "github.com/goplus/llgo/chore/llcppg/ast" +) + +func (ct *Converter) GetFilesJSON() *cjson.JSON { + root := cjson.Object() + for _, file := range ct.files { + f := cjson.Object() + f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path))) + ct.FileJSON(file, f) + root.SetItem(c.AllocaCStr(file.Path), f) + } + return root +} + +func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) { + decls := cjson.Array() + includes := cjson.Array() + macros := cjson.Array() + + for _, decl := range file.Decls { + decls.AddItem(ct.DeclJSON(decl)) + } + + root.SetItem(c.Str("decls"), decls) + root.SetItem(c.Str("includes"), includes) + root.SetItem(c.Str("macros"), macros) +} + +func (ct *Converter) DeclJSON(decl ast.Decl) *cjson.JSON { + if decl == nil { + return cjson.Null() + } + root := cjson.Object() + switch d := decl.(type) { + case *ast.FuncDecl: + ct.DeclBaseJSON(d.DeclBase, root) + root.SetItem(c.Str("Name"), ct.TypeJSON(d.Name)) + root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) + } + return root +} +func (ct *Converter) DeclBaseJSON(decl ast.DeclBase, root *cjson.JSON) { + if decl.Loc == nil { + root.SetItem(c.Str("Loc"), cjson.Null()) + } else { + loc := cjson.Object() + loc.SetItem(c.Str("File"), cjson.String(c.AllocaCStr(decl.Loc.File))) + root.SetItem(c.Str("Loc"), loc) + } + root.SetItem(c.Str("Doc"), ct.TypeJSON(decl.Doc)) + root.SetItem(c.Str("Parent"), ct.TypeJSON(decl.Parent)) +} + +func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { + if t == nil { + return cjson.Null() + } + + root := cjson.Object() + + switch d := t.(type) { + case *ast.FuncType: + root.SetItem(c.Str("Params"), ct.TypeJSON(d.Params)) + root.SetItem(c.Str("Ret"), ct.TypeJSON(d.Ret)) + case *ast.FieldList: + if d == nil { + return cjson.Null() + } + list := cjson.Array() + for _, f := range d.List { + list.AddItem(ct.TypeJSON(f)) + } + root.SetItem(c.Str("List"), list) + case *ast.Field: + root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) + root.SetItem(c.Str("Doc"), ct.TypeJSON(d.Doc)) + root.SetItem(c.Str("Comment"), ct.TypeJSON(d.Comment)) + names := cjson.Array() + for _, n := range d.Names { + names.AddItem(ct.TypeJSON(n)) + } + root.SetItem(c.Str("Names"), names) + case *ast.Ident: + root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + case *ast.PointerType: + root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) + case *ast.BuiltinType: + root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) + root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) + case *ast.Comment: + root.SetItem(c.Str("Text"), cjson.String(c.AllocaCStr(d.Text))) + case *ast.CommentGroup: + if d == nil { + return cjson.Null() + } + list := cjson.Array() + for _, c := range d.List { + println(c.Text) + list.AddItem(ct.TypeJSON(c)) + } + root.SetItem(c.Str("List"), list) + default: + return cjson.Null() + } + return root +} From 755cdbb238d7abce023ff236e2efcef3d7081873 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 13 Aug 2024 18:35:33 +0800 Subject: [PATCH 08/67] llcppsigfetch:collect func param name --- chore/_xtool/llcppsigfetch/parse/cvt.go | 50 +++- .../parse/cvt_test/decl_test/decl.go | 33 ++- .../parse/cvt_test/decl_test/llgo.expect | 271 ++++++++++++++++-- 3 files changed, 319 insertions(+), 35 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 94e4ff32..cd9fb2e2 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -142,14 +142,9 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { case clang.TypePointer: expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} case clang.TypeFunctionProto: + // function type will only collect return type, params will be collected in ProcessFunc ret := ct.ProcessType(t.ResultType()) - params := &ast.FieldList{} - for i := 0; i < int(t.NumArgTypes()); i++ { - argType := ct.ProcessType(t.ArgType(c.Uint(i))) - params.List = append(params.List, &ast.Field{Type: argType}) - // todo(zzy):field name - } - expr = &ast.FuncType{Params: params, Ret: ret} + expr = &ast.FuncType{Ret: ret} case clang.TypeTypedef: expr = ct.ProcessType(t.CanonicalType()) case clang.TypeConstantArray, clang.TypeVariableArray, clang.TypeIncompleteArray, clang.TypeDependentSizedArray: @@ -164,11 +159,16 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { func (ct *Converter) ProcessFunc(cursor clang.Cursor) { name := cursor.String() defer name.Dispose() + + // function type will only collect return type + // ProcessType can't get the field names,will collect in follows funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType) if !ok { fmt.Println("failed to process function type") return } + params := ct.ProcessFuncParams(cursor) + funcType.Params = params fn := &ast.FuncDecl{ Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: funcType, @@ -178,6 +178,42 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) { // ct.declMap[cursor] = fn } +type visitParamContext struct { + params *ast.FieldList + converter *Converter +} + +// visit top decls (struct,class,function,enum & marco,include) +func visitParamDecl(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + ctx := (*visitParamContext)(clientData) + if cursor.Kind == clang.CursorParmDecl { + paramName := cursor.String() + defer paramName.Dispose() + argType := ctx.converter.ProcessType(cursor.Type()) + + // In C language, parameter lists do not have similar parameter grouping in Go. + // func foo(a, b int) + ctx.params.List = append(ctx.params.List, + &ast.Field{ + Type: argType, + Names: []*ast.Ident{ + {Name: c.GoString(paramName.CStr())}, + }, + }) + } + return clang.ChildVisit_Continue +} + +func (ct *Converter) ProcessFuncParams(cursor clang.Cursor) *ast.FieldList { + params := &ast.FieldList{List: []*ast.Field{}} + ctx := &visitParamContext{ + params: params, + converter: ct, + } + clang.VisitChildren(cursor, visitParamDecl, c.Pointer(ctx)) + return params +} + func (ct *Converter) ProcessClass(cursor clang.Cursor) { println("todo: Process class") } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 3302084e..848cdf77 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -10,17 +10,30 @@ func main() { } func TestFuncDecl() { - content := `int foo(int a, int b);` - converter, err := parse.NewConverter(content, true) - if err != nil { - panic(err) + testCases := []string{ + `void foo();`, + `void foo(int a);`, + `float foo(int a,double b);`, + + `void foo(char* str, double x);`, + `float* foo(char* str, double x);`, + `float* foo(char*** str, double x);`, } - defer converter.Dispose() - converter.Convert() - if err != nil { - panic(err) + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.GetFilesJSON() + c.Printf(c.Str("Test Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() } - json := converter.GetFilesJSON() - c.Printf(c.Str("%s\n"), json.Print()) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 6300d577..244091c2 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -1,30 +1,265 @@ #stdout +Test Case 1: { - "Files": { - "temp.h": { - "Path": "temp.h", - "decls": [{ - "Name": "foo", - "Type": { - "Params": [{ - "Kind": 6, - "Flags": 0 + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +Test Case 2: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "a" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +Test Case 3: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "a" + }] }, { - "Kind": 6, - "Flags": 0 - }], - "Ret": { - "Kind": 6, + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +Test Case 4: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "Kind": 2, + "Flags": 1 + } + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +Test Case 5: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "Kind": 2, + "Flags": 1 + } + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, "Flags": 0 } } - }], - "includes": [], - "macros": [] - } + } + }], + "includes": [], + "macros": [] } } +Test Case 6: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "X": { + "X": { + "Kind": 2, + "Flags": 1 + } + } + } + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + + #stderr #exit 0 From 9a77a0c2015114347781b242a3ddc6b9e56c38eb Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 13 Aug 2024 18:42:56 +0800 Subject: [PATCH 09/67] llcppsigfetch:move type test --- .../llcppsigfetch/parse/cvt_test/{ => type_test}/llgo.expect | 0 .../llcppsigfetch/parse/cvt_test/{cvt.go => type_test/type.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename chore/_xtool/llcppsigfetch/parse/cvt_test/{ => type_test}/llgo.expect (100%) rename chore/_xtool/llcppsigfetch/parse/cvt_test/{cvt.go => type_test/type.go} (100%) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect similarity index 100% rename from chore/_xtool/llcppsigfetch/parse/cvt_test/llgo.expect rename to chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go similarity index 100% rename from chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go rename to chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go From 6b1bc15f37bf7f7bbdca7f890031a86d5dc943b2 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 14 Aug 2024 18:22:14 +0800 Subject: [PATCH 10/67] llcppsigfetch:Constant & Incomplete Array --- chore/_xtool/llcppsigfetch/parse/cvt.go | 17 ++- .../parse/cvt_test/decl_test/decl.go | 3 + .../parse/cvt_test/decl_test/llgo.expect | 101 ++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 6 ++ 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index cd9fb2e2..5e5a84c0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -147,11 +147,20 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { expr = &ast.FuncType{Ret: ret} case clang.TypeTypedef: expr = ct.ProcessType(t.CanonicalType()) - case clang.TypeConstantArray, clang.TypeVariableArray, clang.TypeIncompleteArray, clang.TypeDependentSizedArray: - expr = &ast.ArrayType{ - Elt: ct.ProcessType(t.ArrayElementType()), + case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: + if t.Kind == clang.TypeConstantArray { + valueStr := make([]c.Char, 20) + c.Sprintf(unsafe.SliceData(valueStr), c.Str("%d"), t.ArraySize()) + expr = &ast.ArrayType{ + Elt: ct.ProcessType(t.ArrayElementType()), + Len: &ast.BasicLit{Kind: ast.IntLit, Value: c.GoString(unsafe.SliceData(valueStr))}, + } + } else if t.Kind == clang.TypeIncompleteArray { + // incomplete array havent len expr + expr = &ast.ArrayType{ + Elt: ct.ProcessType(t.ArrayElementType()), + } } - // todo(zzy):array length } return expr } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 848cdf77..d76bd84f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -18,6 +18,9 @@ func TestFuncDecl() { `void foo(char* str, double x);`, `float* foo(char* str, double x);`, `float* foo(char*** str, double x);`, + + `float* foo(char str[], double x);`, + `float* foo(int arr[3][4]);`, } for i, content := range testCases { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 244091c2..f39f47d7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -259,6 +259,107 @@ Test Case 6: } } +Test Case 7: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Elt": { + "Kind": 2, + "Flags": 1 + }, + "Len": null + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + +Test Case 8: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Elt": { + "Elt": { + "Kind": 6, + "Flags": 0 + }, + "Len": { + "Kind": 0, + "Value": "4" + } + }, + "Len": { + "Kind": 0, + "Value": "3" + } + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "arr" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 7c1dc9d7..5b0ce8ac 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -87,8 +87,14 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Names"), names) case *ast.Ident: root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + case *ast.BasicLit: + root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) + root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) case *ast.PointerType: root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) + case *ast.ArrayType: + root.SetItem(c.Str("Elt"), ct.TypeJSON(d.Elt)) + root.SetItem(c.Str("Len"), ct.TypeJSON(d.Len)) case *ast.BuiltinType: root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) From f613316046f52bd5694e37fc6cbfae89913a48d3 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 15 Aug 2024 09:27:41 +0800 Subject: [PATCH 11/67] chore:remove the capital of Path --- chore/_xtool/llcppsigfetch/parse/cvt.go | 4 +++- .../parse/cvt_test/decl_test/llgo.expect | 16 ++++++++-------- chore/_xtool/llcppsigfetch/parse/dump.go | 11 +++++------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 5e5a84c0..00cbbac5 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -271,7 +271,9 @@ func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { kind = ast.Float128 default: // like IBM128,NullPtr,Accum - fmt.Fprintln(os.Stderr, "todo: unknown builtin type:", c.GoString(t.Kind.String().CStr())) + kindStr := t.Kind.String() + defer kindStr.Dispose() + fmt.Fprintln(os.Stderr, "todo: unknown builtin type:", c.GoString(kindStr.CStr())) } if IsExplicitSigned(t) { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index f39f47d7..266d5992 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -2,7 +2,7 @@ Test Case 1: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -28,7 +28,7 @@ Test Case 1: Test Case 2: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -64,7 +64,7 @@ Test Case 2: Test Case 3: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -110,7 +110,7 @@ Test Case 3: Test Case 4: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -158,7 +158,7 @@ Test Case 4: Test Case 5: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -208,7 +208,7 @@ Test Case 5: Test Case 6: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -262,7 +262,7 @@ Test Case 6: Test Case 7: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, @@ -313,7 +313,7 @@ Test Case 7: Test Case 8: { "temp.h": { - "Path": "temp.h", + "path": "temp.h", "decls": [{ "Loc": null, "Doc": null, diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 5b0ce8ac..21616892 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -9,15 +9,13 @@ import ( func (ct *Converter) GetFilesJSON() *cjson.JSON { root := cjson.Object() for _, file := range ct.files { - f := cjson.Object() - f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path))) - ct.FileJSON(file, f) - root.SetItem(c.AllocaCStr(file.Path), f) + root.SetItem(c.AllocaCStr(file.Path), ct.FileJSON(file)) } return root } -func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) { +func (ct *Converter) FileJSON(file *ast.File) *cjson.JSON { + root := cjson.Object() decls := cjson.Array() includes := cjson.Array() macros := cjson.Array() @@ -26,9 +24,11 @@ func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) { decls.AddItem(ct.DeclJSON(decl)) } + root.SetItem(c.Str("path"), cjson.String(c.AllocaCStr(file.Path))) root.SetItem(c.Str("decls"), decls) root.SetItem(c.Str("includes"), includes) root.SetItem(c.Str("macros"), macros) + return root } func (ct *Converter) DeclJSON(decl ast.Decl) *cjson.JSON { @@ -106,7 +106,6 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { } list := cjson.Array() for _, c := range d.List { - println(c.Text) list.AddItem(ct.TypeJSON(c)) } root.SetItem(c.Str("List"), list) From 2c8a9d1160e7e3af4550afc3fd361605e6a31fe2 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 15 Aug 2024 11:26:34 +0800 Subject: [PATCH 12/67] llcppsigfetch:Scope Context --- chore/_xtool/llcppsigfetch/parse/cvt.go | 117 +++++++++------ .../parse/cvt_test/decl_test/decl.go | 34 ++++- .../parse/cvt_test/decl_test/llgo.expect | 141 ++++++++++++++++-- chore/_xtool/llcppsigfetch/parse/dump.go | 3 + 4 files changed, 236 insertions(+), 59 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 00cbbac5..dd9b282e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -12,12 +12,12 @@ import ( ) type Converter struct { - files map[string]*ast.File - curLoc ast.Location - curFile *ast.File - index *clang.Index - unit *clang.TranslationUnit - // todo(zzy):current namespace expr + files map[string]*ast.File + curLoc ast.Location + curFile *ast.File + index *clang.Index + unit *clang.TranslationUnit + scopeStack []ast.Expr //namespace & class } func NewConverter(file string, temp bool) (*Converter, error) { @@ -68,41 +68,31 @@ func (ct *Converter) Dispose() { ct.unit.Dispose() } -// visit top decls (struct,class,function,enum & marco,include) -func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { - ct := (*Converter)(clientData) - ct.UpdateCurFile(cursor) +func (ct *Converter) PushScope(cursor clang.Cursor) { + name := cursor.String() + defer name.Dispose() + ident := &ast.Ident{Name: c.GoString(name.CStr())} - switch cursor.Kind { - case clang.CursorInclusionDirective: - // todo(zzy) - return clang.ChildVisit_Continue - case clang.CursorMacroDefinition: - // todo(zzy) - return clang.ChildVisit_Continue - case clang.CursorEnumDecl: - // todo(zzy) - return clang.ChildVisit_Continue - case clang.CursorClassDecl: - ct.ProcessClass(cursor) - return clang.ChildVisit_Continue - case clang.CursorStructDecl: - // todo(zzy) - return clang.ChildVisit_Continue - case clang.CursorFunctionDecl: - ct.ProcessFunc(cursor) - return clang.ChildVisit_Continue - default: - // non-top-level decl, continue recursion - return clang.ChildVisit_Recurse + if len(ct.scopeStack) == 0 { + ct.scopeStack = append(ct.scopeStack, ident) + } else { + parent := ct.scopeStack[len(ct.scopeStack)-1] + newContext := &ast.ScopingExpr{Parent: parent, X: ident} + ct.scopeStack = append(ct.scopeStack, newContext) } } -func (ct *Converter) Convert() (map[string]*ast.File, error) { - cursor := ct.unit.Cursor() - // visit top decls (struct,class,function & marco,include) - clang.VisitChildren(cursor, visit, c.Pointer(ct)) - return ct.files, nil +func (ct *Converter) PopScope() { + if len(ct.scopeStack) > 0 { + ct.scopeStack = ct.scopeStack[:len(ct.scopeStack)-1] + } +} + +func (ct *Converter) GetCurScope() ast.Expr { + if len(ct.scopeStack) == 0 { + return nil + } + return ct.scopeStack[len(ct.scopeStack)-1] } func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { @@ -118,6 +108,8 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { } filePath := c.GoString(filename.CStr()) + ct.curLoc = ast.Location{File: filePath} + if ct.curFile == nil || ct.curFile.Path != filePath { if f, ok := ct.files[filePath]; ok { ct.curFile = f @@ -128,11 +120,53 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { Includes: make([]*ast.Include, 0), Macros: make([]*ast.Macro, 0), } + ct.files[filePath] = ct.curFile } - ct.files[filePath] = ct.curFile } } +func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { + return ast.DeclBase{ + Loc: &ct.curLoc, + Parent: ct.GetCurScope(), + } +} + +// visit top decls (struct,class,function,enum & marco,include) +func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + ct := (*Converter)(clientData) + ct.UpdateCurFile(cursor) + + switch cursor.Kind { + case clang.CursorInclusionDirective: + // todo(zzy) + case clang.CursorMacroDefinition: + // todo(zzy) + case clang.CursorEnumDecl: + // todo(zzy) + case clang.CursorClassDecl: + ct.PushScope(cursor) + ct.ProcessClass(cursor) + ct.PopScope() + case clang.CursorStructDecl: + // todo(zzy) + case clang.CursorFunctionDecl: + ct.ProcessFunc(cursor) + case clang.CursorNamespace: + ct.PushScope(cursor) + clang.VisitChildren(cursor, visit, c.Pointer(ct)) + ct.PopScope() + } + return clang.ChildVisit_Continue +} + +func (ct *Converter) Convert() (map[string]*ast.File, error) { + cursor := ct.unit.Cursor() + // visit top decls (struct,class,function & marco,include) + clang.VisitChildren(cursor, visit, c.Pointer(ct)) + return ct.files, nil +} + func (ct *Converter) ProcessType(t clang.Type) ast.Expr { var expr ast.Expr if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { @@ -179,12 +213,11 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) { params := ct.ProcessFuncParams(cursor) funcType.Params = params fn := &ast.FuncDecl{ - Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Type: funcType, - // todo(zzy):DeclBase use the converter's current namespace expr + DeclBase: ct.CreateDeclBase(cursor), + Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Type: funcType, } ct.curFile.Decls = append(ct.curFile.Decls, fn) - // ct.declMap[cursor] = fn } type visitParamContext struct { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index d76bd84f..2391b8dc 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -7,6 +7,7 @@ import ( func main() { TestFuncDecl() + TestScope() } func TestFuncDecl() { @@ -35,7 +36,38 @@ func TestFuncDecl() { } json := converter.GetFilesJSON() - c.Printf(c.Str("Test Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + c.Printf(c.Str("TestFuncDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} + +func TestScope() { + testCases := []string{ + `void foo();`, + `namespace a { + void foo(); + }`, + `namespace a { + namespace b { + void foo(); + } + }`, + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.GetFilesJSON() + c.Printf(c.Str("TestScope Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) converter.Dispose() } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 266d5992..206886c3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -1,10 +1,12 @@ #stdout -Test Case 1: +TestFuncDecl Case 1: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -25,12 +27,14 @@ Test Case 1: } } -Test Case 2: +TestFuncDecl Case 2: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -61,12 +65,14 @@ Test Case 2: } } -Test Case 3: +TestFuncDecl Case 3: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -107,12 +113,14 @@ Test Case 3: } } -Test Case 4: +TestFuncDecl Case 4: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -155,12 +163,14 @@ Test Case 4: } } -Test Case 5: +TestFuncDecl Case 5: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -205,12 +215,14 @@ Test Case 5: } } -Test Case 6: +TestFuncDecl Case 6: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -259,12 +271,14 @@ Test Case 6: } } -Test Case 7: +TestFuncDecl Case 7: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -310,12 +324,14 @@ Test Case 7: } } -Test Case 8: +TestFuncDecl Case 8: { "temp.h": { "path": "temp.h", "decls": [{ - "Loc": null, + "Loc": { + "File": "temp.h" + }, "Doc": null, "Parent": null, "Name": { @@ -360,6 +376,99 @@ Test Case 8: } } +TestScope Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "Name": "a" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 21616892..c5eeb004 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -109,6 +109,9 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { list.AddItem(ct.TypeJSON(c)) } root.SetItem(c.Str("List"), list) + case *ast.ScopingExpr: + root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) + root.SetItem(c.Str("Parent"), ct.TypeJSON(d.Parent)) default: return cjson.Null() } From 762ed994c14d78f944139456f8a85c4071936e0a Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 15 Aug 2024 16:08:50 +0800 Subject: [PATCH 13/67] llcppsigfetch:Decl Comment --- chore/_xtool/llcppsigfetch/parse/cvt.go | 19 + .../parse/cvt_test/decl_test/decl.go | 46 +++ .../parse/cvt_test/decl_test/llgo.expect | 340 +++++++++++++++++- 3 files changed, 394 insertions(+), 11 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index dd9b282e..19169be3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strings" "unsafe" "github.com/goplus/llgo/c" @@ -126,12 +127,30 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { } func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { + rawComment := cursor.RawCommentText() + defer rawComment.Dispose() + + commentGroup := &ast.CommentGroup{} + if rawComment.CStr() != nil { + commentGroup = ct.ParseComment(c.GoString(rawComment.CStr())) + } + return ast.DeclBase{ Loc: &ct.curLoc, Parent: ct.GetCurScope(), + Doc: commentGroup, } } +func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { + lines := strings.Split(rawComment, "\n") + commentGroup := &ast.CommentGroup{} + for _, line := range lines { + commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line}) + } + return commentGroup +} + // visit top decls (struct,class,function,enum & marco,include) func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ct := (*Converter)(clientData) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 2391b8dc..4decd928 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -8,6 +8,7 @@ import ( func main() { TestFuncDecl() TestScope() + TestComment() } func TestFuncDecl() { @@ -72,3 +73,48 @@ func TestScope() { converter.Dispose() } } +func TestComment() { + testCases := []string{ + `// not read comment 1 + void foo();`, + `/* not read comment 2 */ + void foo();`, + `/// comment + void foo();`, + `/** comment */ + void foo();`, + `/*! comment */ + void foo();`, + `/// comment 1 +/// comment 2 +void foo();`, + `/*! comment 1 */ +/*! comment 2 */ +void foo();`, + `/** comment 1 */ +/** comment 1 */ +void foo();`, + `/** + * comment 1 + * comment 2 + */ +void foo();`, + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.GetFilesJSON() + c.Printf(c.Str("TestComment Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 206886c3..87b9e634 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -7,7 +7,9 @@ TestFuncDecl Case 1: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -35,7 +37,9 @@ TestFuncDecl Case 2: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -73,7 +77,9 @@ TestFuncDecl Case 3: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -121,7 +127,9 @@ TestFuncDecl Case 4: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -171,7 +179,9 @@ TestFuncDecl Case 5: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -223,7 +233,9 @@ TestFuncDecl Case 6: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -279,7 +291,9 @@ TestFuncDecl Case 7: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -332,7 +346,9 @@ TestFuncDecl Case 8: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -384,7 +400,9 @@ TestScope Case 1: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -412,7 +430,9 @@ TestScope Case 2: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": { "Name": "a" }, @@ -442,7 +462,9 @@ TestScope Case 3: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": { "X": { "Name": "b" @@ -469,6 +491,302 @@ TestScope Case 3: } } +TestComment Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/// comment" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 4: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/** comment */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 5: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/*! comment */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 6: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/// comment 1" + }, { + "Text": "/// comment 2" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 7: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/*! comment 1 */" + }, { + "Text": "/*! comment 2 */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 8: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/** comment 1 */" + }, { + "Text": "/** comment 1 */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 9: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/**" + }, { + "Text": " * comment 1" + }, { + "Text": " * comment 2" + }, { + "Text": " */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr From 9087dac6fe20cdbebd6b7f9dca4b463ae4ac6824 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 15 Aug 2024 18:21:09 +0800 Subject: [PATCH 14/67] llcppsigfetch:basic class & struct 's process --- chore/_xtool/llcppsigfetch/parse/cvt.go | 90 ++- .../parse/cvt_test/decl_test/decl.go | 76 +++ .../parse/cvt_test/decl_test/llgo.expect | 557 +++++++++++++++++- chore/_xtool/llcppsigfetch/parse/dump.go | 9 + 4 files changed, 691 insertions(+), 41 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 19169be3..be52e641 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -168,9 +168,9 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi ct.ProcessClass(cursor) ct.PopScope() case clang.CursorStructDecl: - // todo(zzy) + ct.ProcessStruct(cursor) case clang.CursorFunctionDecl: - ct.ProcessFunc(cursor) + ct.curFile.Decls = append(ct.curFile.Decls, ct.ProcessFunc(cursor)) case clang.CursorNamespace: ct.PushScope(cursor) clang.VisitChildren(cursor, visit, c.Pointer(ct)) @@ -218,45 +218,51 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { return expr } -func (ct *Converter) ProcessFunc(cursor clang.Cursor) { +func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() - // function type will only collect return type // ProcessType can't get the field names,will collect in follows funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType) if !ok { fmt.Println("failed to process function type") - return + return nil } - params := ct.ProcessFuncParams(cursor) + params := ct.ProcessFieldList(cursor) funcType.Params = params fn := &ast.FuncDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: funcType, } - ct.curFile.Decls = append(ct.curFile.Decls, fn) + return fn } -type visitParamContext struct { +type visitFieldContext struct { params *ast.FieldList converter *Converter } -// visit top decls (struct,class,function,enum & marco,include) -func visitParamDecl(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { - ctx := (*visitParamContext)(clientData) - if cursor.Kind == clang.CursorParmDecl { +func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + ctx := (*visitFieldContext)(clientData) + if cursor.Kind == clang.CursorParmDecl || cursor.Kind == clang.CursorFieldDecl { paramName := cursor.String() defer paramName.Dispose() argType := ctx.converter.ProcessType(cursor.Type()) // In C language, parameter lists do not have similar parameter grouping in Go. // func foo(a, b int) + + // For follows struct, it will also parse to two FieldDecl + // struct A { + // int a, b; + // }; ctx.params.List = append(ctx.params.List, &ast.Field{ - Type: argType, + //todo(zzy): comment & doc + Doc: &ast.CommentGroup{}, + Comment: &ast.CommentGroup{}, + Type: argType, Names: []*ast.Ident{ {Name: c.GoString(paramName.CStr())}, }, @@ -265,18 +271,68 @@ func visitParamDecl(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan return clang.ChildVisit_Continue } -func (ct *Converter) ProcessFuncParams(cursor clang.Cursor) *ast.FieldList { +func (ct *Converter) ProcessFieldList(cursor clang.Cursor) *ast.FieldList { params := &ast.FieldList{List: []*ast.Field{}} - ctx := &visitParamContext{ + ctx := &visitFieldContext{ params: params, converter: ct, } - clang.VisitChildren(cursor, visitParamDecl, c.Pointer(ctx)) + clang.VisitChildren(cursor, visitFieldList, c.Pointer(ctx)) return params } +type visitMethodsContext struct { + methods *[]*ast.FuncDecl + converter *Converter +} + +func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + ctx := (*visitMethodsContext)(clientData) + if cursor.Kind == clang.CursorCXXMethod { + method := ctx.converter.ProcessFunc(cursor) + if method != nil { + *ctx.methods = append(*ctx.methods, method) + } + } + return clang.ChildVisit_Continue +} + +func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { + methods := make([]*ast.FuncDecl, 0) + ctx := &visitMethodsContext{ + methods: &methods, + converter: ct, + } + clang.VisitChildren(cursor, visitMethods, c.Pointer(ctx)) + return methods +} + +func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { + name := cursor.String() + defer name.Dispose() + + fields := ct.ProcessFieldList(cursor) + methods := ct.ProcessMethods(cursor) + + decl := &ast.TypeDecl{ + DeclBase: ct.CreateDeclBase(cursor), + Tag: tag, + Fields: fields, + Methods: methods, + } + + return decl +} + +func (ct *Converter) ProcessStruct(cursor clang.Cursor) { + structDecl := ct.ProcessStructOrClass(cursor, ast.Struct) + ct.curFile.Decls = append(ct.curFile.Decls, structDecl) +} + func (ct *Converter) ProcessClass(cursor clang.Cursor) { - println("todo: Process class") + classDecl := ct.ProcessStructOrClass(cursor, ast.Class) + // other logic for class + ct.curFile.Decls = append(ct.curFile.Decls, classDecl) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 4decd928..908fcc3e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -9,6 +9,8 @@ func main() { TestFuncDecl() TestScope() TestComment() + TestStructDecl() + TestClassDecl() } func TestFuncDecl() { @@ -54,6 +56,14 @@ func TestScope() { void foo(); } }`, + `class a { + void foo(); + };`, + `namespace a { + class b { + void foo(); + }; + }`, } for i, content := range testCases { @@ -73,6 +83,7 @@ func TestScope() { converter.Dispose() } } + func TestComment() { testCases := []string{ `// not read comment 1 @@ -118,3 +129,68 @@ void foo();`, converter.Dispose() } } + +func TestStructDecl() { + testCases := []string{ + `struct A { + int a; + int b; + };`, + `struct A { + int a, b; + };`, + `struct A { + int a; + int b; + float foo(int a,double b);; + };`, + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.GetFilesJSON() + c.Printf(c.Str("TestStructDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} + +func TestClassDecl() { + testCases := []string{ + `class A { + int a; + int b; + };`, + `class A { + int a; + int b; + float foo(int a,double b);; + };`, + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.GetFilesJSON() + c.Printf(c.Str("TestClassDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 87b9e634..47e9cb36 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -51,8 +51,12 @@ TestFuncDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "a" }] @@ -91,8 +95,12 @@ TestFuncDecl Case 3: "Kind": 6, "Flags": 0 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "a" }] @@ -101,8 +109,12 @@ TestFuncDecl Case 3: "Kind": 8, "Flags": 16 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "b" }] @@ -143,8 +155,12 @@ TestFuncDecl Case 4: "Flags": 1 } }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "str" }] @@ -153,8 +169,12 @@ TestFuncDecl Case 4: "Kind": 8, "Flags": 16 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "x" }] @@ -195,8 +215,12 @@ TestFuncDecl Case 5: "Flags": 1 } }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "str" }] @@ -205,8 +229,12 @@ TestFuncDecl Case 5: "Kind": 8, "Flags": 16 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "x" }] @@ -253,8 +281,12 @@ TestFuncDecl Case 6: } } }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "str" }] @@ -263,8 +295,12 @@ TestFuncDecl Case 6: "Kind": 8, "Flags": 16 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "x" }] @@ -308,8 +344,12 @@ TestFuncDecl Case 7: }, "Len": null }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "str" }] @@ -318,8 +358,12 @@ TestFuncDecl Case 7: "Kind": 8, "Flags": 16 }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "x" }] @@ -372,8 +416,12 @@ TestFuncDecl Case 8: "Value": "3" } }, - "Doc": null, - "Comment": null, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, "Names": [{ "Name": "arr" }] @@ -491,6 +539,110 @@ TestScope Case 3: } } +TestScope Case 4: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "a" + }, + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "a" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }] + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 5: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }] + }], + "includes": [], + "macros": [] + } +} + TestComment Case 1: { "temp.h": { @@ -787,6 +939,363 @@ TestComment Case 9: } } +TestStructDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } + } + }] + }], + "includes": [], + "macros": [] + } +} + +TestClassDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestClassDecl Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } + } + }] + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index c5eeb004..e36f2617 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -41,6 +41,15 @@ func (ct *Converter) DeclJSON(decl ast.Decl) *cjson.JSON { ct.DeclBaseJSON(d.DeclBase, root) root.SetItem(c.Str("Name"), ct.TypeJSON(d.Name)) root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) + case *ast.TypeDecl: + ct.DeclBaseJSON(d.DeclBase, root) + root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) + root.SetItem(c.Str("Fields"), ct.TypeJSON(d.Fields)) + methods := cjson.Array() + for _, m := range d.Methods { + methods.AddItem(ct.DeclJSON(m)) + } + root.SetItem(c.Str("Methods"), methods) } return root } From 2b1d4b667234b15bdb0868143ee53436f2b3ee1e Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 16 Aug 2024 11:52:22 +0800 Subject: [PATCH 15/67] llcppsigfetch:output result --- chore/_xtool/llcppsigfetch/llcppsigfetch.go | 6 +- chore/_xtool/llcppsigfetch/parse/cvt.go | 19 ++-- .../parse/cvt_test/decl_test/decl.go | 10 +-- chore/_xtool/llcppsigfetch/parse/dump.go | 87 ++++++++++--------- chore/_xtool/llcppsigfetch/parse/parse.go | 14 +++ 5 files changed, 84 insertions(+), 52 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 19746b09..9c3cd6b3 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -22,7 +22,9 @@ import ( "os" "path/filepath" "strings" + "unsafe" + "github.com/goplus/llgo/c" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" "github.com/goplus/llgo/chore/_xtool/llcppsymg/config" ) @@ -76,5 +78,7 @@ func getHeaderFiles(cflags string, files []string) []string { } func outputInfo(context *parse.Context) { - // TODO(zzy): + output := context.Output().Print() + defer c.Free(unsafe.Pointer(output)) + c.Printf(output) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index be52e641..a87a7aed 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -8,12 +8,13 @@ import ( "unsafe" "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/llcppg/ast" ) type Converter struct { - files map[string]*ast.File + Files map[string]*ast.File curLoc ast.Location curFile *ast.File index *clang.Index @@ -58,7 +59,7 @@ func NewConverter(file string, temp bool) (*Converter, error) { } return &Converter{ - files: make(map[string]*ast.File), + Files: make(map[string]*ast.File), index: index, unit: unit, }, nil @@ -112,7 +113,7 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { ct.curLoc = ast.Location{File: filePath} if ct.curFile == nil || ct.curFile.Path != filePath { - if f, ok := ct.files[filePath]; ok { + if f, ok := ct.Files[filePath]; ok { ct.curFile = f } else { ct.curFile = &ast.File{ @@ -121,7 +122,7 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { Includes: make([]*ast.Include, 0), Macros: make([]*ast.Macro, 0), } - ct.files[filePath] = ct.curFile + ct.Files[filePath] = ct.curFile } } } @@ -135,8 +136,9 @@ func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { commentGroup = ct.ParseComment(c.GoString(rawComment.CStr())) } + loc := ct.curLoc return ast.DeclBase{ - Loc: &ct.curLoc, + Loc: &loc, Parent: ct.GetCurScope(), Doc: commentGroup, } @@ -183,7 +185,7 @@ func (ct *Converter) Convert() (map[string]*ast.File, error) { cursor := ct.unit.Cursor() // visit top decls (struct,class,function & marco,include) clang.VisitChildren(cursor, visit, c.Pointer(ct)) - return ct.files, nil + return ct.Files, nil } func (ct *Converter) ProcessType(t clang.Type) ast.Expr { @@ -395,6 +397,11 @@ func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { Flags: flags, } } + +func (ct *Converter) MarshalASTFiles() *cjson.JSON { + return MarshalASTFiles(ct.Files) +} + func IsExplicitSigned(t clang.Type) bool { return t.Kind == clang.TypeCharS || t.Kind == clang.TypeSChar } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 908fcc3e..bba657f6 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -38,7 +38,7 @@ func TestFuncDecl() { panic(err) } - json := converter.GetFilesJSON() + json := converter.MarshalASTFiles() c.Printf(c.Str("TestFuncDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) converter.Dispose() @@ -77,7 +77,7 @@ func TestScope() { panic(err) } - json := converter.GetFilesJSON() + json := converter.MarshalASTFiles() c.Printf(c.Str("TestScope Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) converter.Dispose() @@ -123,7 +123,7 @@ void foo();`, panic(err) } - json := converter.GetFilesJSON() + json := converter.MarshalASTFiles() c.Printf(c.Str("TestComment Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) converter.Dispose() @@ -157,7 +157,7 @@ func TestStructDecl() { panic(err) } - json := converter.GetFilesJSON() + json := converter.MarshalASTFiles() c.Printf(c.Str("TestStructDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) converter.Dispose() @@ -188,7 +188,7 @@ func TestClassDecl() { panic(err) } - json := converter.GetFilesJSON() + json := converter.MarshalASTFiles() c.Printf(c.Str("TestClassDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) converter.Dispose() diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index e36f2617..c8c20c90 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -6,66 +6,73 @@ import ( "github.com/goplus/llgo/chore/llcppg/ast" ) -func (ct *Converter) GetFilesJSON() *cjson.JSON { +func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON { root := cjson.Object() - for _, file := range ct.files { - root.SetItem(c.AllocaCStr(file.Path), ct.FileJSON(file)) + for _, file := range files { + root.SetItem(c.AllocaCStr(file.Path), MarshalASTFile(file)) } return root } -func (ct *Converter) FileJSON(file *ast.File) *cjson.JSON { +func MarshalASTFile(file *ast.File) *cjson.JSON { root := cjson.Object() decls := cjson.Array() - includes := cjson.Array() - macros := cjson.Array() for _, decl := range file.Decls { - decls.AddItem(ct.DeclJSON(decl)) + decls.AddItem(MarshalASTDecl(decl)) } root.SetItem(c.Str("path"), cjson.String(c.AllocaCStr(file.Path))) root.SetItem(c.Str("decls"), decls) - root.SetItem(c.Str("includes"), includes) - root.SetItem(c.Str("macros"), macros) + + // json:includes,omitempty + if file.Includes != nil { + includes := cjson.Array() + root.SetItem(c.Str("includes"), includes) + } + + // json:macros,omitempty + if file.Macros != nil { + macros := cjson.Array() + root.SetItem(c.Str("macros"), macros) + } + return root } -func (ct *Converter) DeclJSON(decl ast.Decl) *cjson.JSON { +func MarshalASTDecl(decl ast.Decl) *cjson.JSON { if decl == nil { return cjson.Null() } root := cjson.Object() switch d := decl.(type) { case *ast.FuncDecl: - ct.DeclBaseJSON(d.DeclBase, root) - root.SetItem(c.Str("Name"), ct.TypeJSON(d.Name)) - root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) + MarshalASTDeclBase(d.DeclBase, root) + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) case *ast.TypeDecl: - ct.DeclBaseJSON(d.DeclBase, root) + MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) - root.SetItem(c.Str("Fields"), ct.TypeJSON(d.Fields)) + root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) methods := cjson.Array() for _, m := range d.Methods { - methods.AddItem(ct.DeclJSON(m)) + methods.AddItem(MarshalASTDecl(m)) } root.SetItem(c.Str("Methods"), methods) } return root } -func (ct *Converter) DeclBaseJSON(decl ast.DeclBase, root *cjson.JSON) { - if decl.Loc == nil { - root.SetItem(c.Str("Loc"), cjson.Null()) - } else { - loc := cjson.Object() - loc.SetItem(c.Str("File"), cjson.String(c.AllocaCStr(decl.Loc.File))) - root.SetItem(c.Str("Loc"), loc) - } - root.SetItem(c.Str("Doc"), ct.TypeJSON(decl.Doc)) - root.SetItem(c.Str("Parent"), ct.TypeJSON(decl.Parent)) + +func MarshalASTDeclBase(decl ast.DeclBase, root *cjson.JSON) { + loc := cjson.Object() + loc.SetItem(c.Str("File"), cjson.String(c.AllocaCStr(decl.Loc.File))) + root.SetItem(c.Str("Loc"), loc) + + root.SetItem(c.Str("Doc"), MarshalASTExpr(decl.Doc)) + root.SetItem(c.Str("Parent"), MarshalASTExpr(decl.Parent)) } -func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { +func MarshalASTExpr(t ast.Expr) *cjson.JSON { if t == nil { return cjson.Null() } @@ -74,24 +81,24 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { switch d := t.(type) { case *ast.FuncType: - root.SetItem(c.Str("Params"), ct.TypeJSON(d.Params)) - root.SetItem(c.Str("Ret"), ct.TypeJSON(d.Ret)) + root.SetItem(c.Str("Params"), MarshalASTExpr(d.Params)) + root.SetItem(c.Str("Ret"), MarshalASTExpr(d.Ret)) case *ast.FieldList: if d == nil { return cjson.Null() } list := cjson.Array() for _, f := range d.List { - list.AddItem(ct.TypeJSON(f)) + list.AddItem(MarshalASTExpr(f)) } root.SetItem(c.Str("List"), list) case *ast.Field: - root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) - root.SetItem(c.Str("Doc"), ct.TypeJSON(d.Doc)) - root.SetItem(c.Str("Comment"), ct.TypeJSON(d.Comment)) + root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) + root.SetItem(c.Str("Doc"), MarshalASTExpr(d.Doc)) + root.SetItem(c.Str("Comment"), MarshalASTExpr(d.Comment)) names := cjson.Array() for _, n := range d.Names { - names.AddItem(ct.TypeJSON(n)) + names.AddItem(MarshalASTExpr(n)) } root.SetItem(c.Str("Names"), names) case *ast.Ident: @@ -100,10 +107,10 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) case *ast.PointerType: - root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) + root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.ArrayType: - root.SetItem(c.Str("Elt"), ct.TypeJSON(d.Elt)) - root.SetItem(c.Str("Len"), ct.TypeJSON(d.Len)) + root.SetItem(c.Str("Elt"), MarshalASTExpr(d.Elt)) + root.SetItem(c.Str("Len"), MarshalASTExpr(d.Len)) case *ast.BuiltinType: root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) @@ -115,12 +122,12 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { } list := cjson.Array() for _, c := range d.List { - list.AddItem(ct.TypeJSON(c)) + list.AddItem(MarshalASTExpr(c)) } root.SetItem(c.Str("List"), list) case *ast.ScopingExpr: - root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) - root.SetItem(c.Str("Parent"), ct.TypeJSON(d.Parent)) + root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) + root.SetItem(c.Str("Parent"), MarshalASTExpr(d.Parent)) default: return cjson.Null() } diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index ce36a352..e7b3bee4 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -3,6 +3,8 @@ package parse import ( "errors" + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/chore/llcppg/ast" ) @@ -16,6 +18,18 @@ func NewContext() *Context { } } +func (p *Context) Output() *cjson.JSON { + root := cjson.Array() + for path, file := range p.Files { + f := cjson.Object() + path := cjson.String(c.AllocaCStr(path)) + f.SetItem(c.Str("path"), path) + f.SetItem(c.Str("doc"), MarshalASTFile(file)) + root.AddItem(f) + } + return root +} + // ProcessFiles processes the given files and adds them to the context func (p *Context) ProcessFiles(files []string) error { for _, file := range files { From 02651c93a7e0d08af62bd023f57b27ea07e11fb7 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 16 Aug 2024 18:39:09 +0800 Subject: [PATCH 16/67] llcppsigfetch:basic marco process --- chore/_xtool/llcppsigfetch/parse/cvt.go | 37 +++++++++++++++++++ .../cvt_test/preprocess_test/llgo.expect | 24 ++++++++++++ .../cvt_test/preprocess_test/preprocess.go | 33 +++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 17 ++++++++- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index a87a7aed..b8b6dc7c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -163,6 +163,7 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi // todo(zzy) case clang.CursorMacroDefinition: // todo(zzy) + ct.ProcessMarco(cursor) case clang.CursorEnumDecl: // todo(zzy) case clang.CursorClassDecl: @@ -240,6 +241,42 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { return fn } +// current only collect marco which defined in file +func (ct *Converter) ProcessMarco(cursor clang.Cursor) { + if ct.curFile == nil { + return + } + name := cursor.String() + defer name.Dispose() + + ran := cursor.Extent() + var numTokens c.Uint + var tokens *clang.Token + ct.unit.Tokenize(ran, &tokens, &numTokens) + tokensSlice := unsafe.Slice(tokens, int(numTokens)) + + macro := &ast.Macro{ + Name: &ast.TokenInfo{ + Token: ast.Token(tokensSlice[0].Kind()), + Lit: c.GoString(ct.unit.Token(tokensSlice[0]).CStr()), + }, + Body: make([]*ast.TokenInfo, 0), + } + + if numTokens > 1 { //have body + for i := 1; i < int(numTokens); i++ { + tok := tokensSlice[i] + tokStr := ct.unit.Token(tok) + macro.Body = append(macro.Body, &ast.TokenInfo{ + Token: ast.Token(tok.Kind()), + Lit: c.GoString(tokStr.CStr()), + }) + tokStr.Dispose() + } + } + ct.curFile.Macros = append(ct.curFile.Macros, macro) +} + type visitFieldContext struct { params *ast.FieldList converter *Converter diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect new file mode 100644 index 00000000..72eb299f --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect @@ -0,0 +1,24 @@ +#stdout +TestDefine Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [], + "includes": [], + "macros": [{ + "Name": { + "Token": 2, + "Lit": "foo" + }, + "Body": [{ + "Token": 3, + "Lit": "1" + }] + }] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go new file mode 100644 index 00000000..d342b2b0 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go @@ -0,0 +1,33 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" +) + +func main() { + TestDefine() +} + +func TestDefine() { + testCases := []string{ + `#define foo 1`, + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.MarshalASTFiles() + c.Printf(c.Str("TestDefine Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index c8c20c90..a72bac5a 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -34,9 +34,24 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { // json:macros,omitempty if file.Macros != nil { macros := cjson.Array() + for _, m := range file.Macros { + marco := cjson.Object() + marco.SetItem(c.Str("Name"), TokenInfo(m.Name)) + body := cjson.Array() + for _, b := range m.Body { + body.AddItem(TokenInfo(b)) + } + marco.SetItem(c.Str("Body"), body) + macros.AddItem(marco) + } root.SetItem(c.Str("macros"), macros) } - + return root +} +func TokenInfo(t *ast.TokenInfo) *cjson.JSON { + root := cjson.Object() + root.SetItem(c.Str("Token"), cjson.Number(float64(t.Token))) + root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(t.Lit))) return root } From 9d16df5f2588cd0b8e8cca2618f0030196badf57 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sun, 18 Aug 2024 11:16:43 +0800 Subject: [PATCH 17/67] llcppsigfetch:include --- chore/_xtool/llcppsigfetch/parse/cvt.go | 9 ++- .../cvt_test/preprocess_test/llgo.expect | 66 ++++++++++++++++++- .../cvt_test/preprocess_test/preprocess.go | 28 +++++++- chore/_xtool/llcppsigfetch/parse/dump.go | 5 ++ 4 files changed, 104 insertions(+), 4 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index b8b6dc7c..37aecf9e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -160,9 +160,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi switch cursor.Kind { case clang.CursorInclusionDirective: - // todo(zzy) + ct.ProcessInclude(cursor) case clang.CursorMacroDefinition: - // todo(zzy) ct.ProcessMarco(cursor) case clang.CursorEnumDecl: // todo(zzy) @@ -277,6 +276,12 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) { ct.curFile.Macros = append(ct.curFile.Macros, macro) } +func (ct *Converter) ProcessInclude(cursor clang.Cursor) { + name := cursor.String() + defer name.Dispose() + ct.curFile.Includes = append(ct.curFile.Includes, &ast.Include{Path: c.GoString(name.CStr())}) +} + type visitFieldContext struct { params *ast.FieldList converter *Converter diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect index 72eb299f..879d6316 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect @@ -8,7 +8,7 @@ TestDefine Case 1: "macros": [{ "Name": { "Token": 2, - "Lit": "foo" + "Lit": "OK" }, "Body": [{ "Token": 3, @@ -18,6 +18,70 @@ TestDefine Case 1: } } +TestDefine Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [], + "includes": [], + "macros": [{ + "Name": { + "Token": 2, + "Lit": "SQUARE" + }, + "Body": [{ + "Token": 0, + "Lit": "(" + }, { + "Token": 2, + "Lit": "x" + }, { + "Token": 0, + "Lit": ")" + }, { + "Token": 0, + "Lit": "(" + }, { + "Token": 0, + "Lit": "(" + }, { + "Token": 2, + "Lit": "x" + }, { + "Token": 0, + "Lit": ")" + }, { + "Token": 0, + "Lit": "*" + }, { + "Token": 0, + "Lit": "(" + }, { + "Token": 2, + "Lit": "x" + }, { + "Token": 0, + "Lit": ")" + }, { + "Token": 0, + "Lit": ")" + }] + }] + } +} + +TestInclude Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [], + "includes": [{ + "Path": "foo.h" + }], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go index d342b2b0..f5ef45a5 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go @@ -7,11 +7,13 @@ import ( func main() { TestDefine() + TestInclude() } func TestDefine() { testCases := []string{ - `#define foo 1`, + `#define OK 1`, + `#define SQUARE(x) ((x) * (x))`, } for i, content := range testCases { @@ -31,3 +33,27 @@ func TestDefine() { converter.Dispose() } } + +func TestInclude() { + testCases := []string{ + `#include "foo.h"`, + // `#include `, // Standard libraries are mostly platform-dependent + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.MarshalASTFiles() + c.Printf(c.Str("TestInclude Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index a72bac5a..91a9c594 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -28,6 +28,11 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { // json:includes,omitempty if file.Includes != nil { includes := cjson.Array() + for _, i := range file.Includes { + include := cjson.Object() + include.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(i.Path))) + includes.AddItem(include) + } root.SetItem(c.Str("includes"), includes) } From 090e689689b8bef45a9950cf46ff1f0aa6814152 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 19 Aug 2024 12:07:30 +0800 Subject: [PATCH 18/67] llcppsigfetch:basic enum --- chore/_xtool/llcppsigfetch/parse/cvt.go | 52 ++++++- .../parse/cvt_test/decl_test/decl.go | 38 +++++ .../parse/cvt_test/decl_test/llgo.expect | 138 ++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 11 ++ 4 files changed, 235 insertions(+), 4 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 37aecf9e..07d4efd4 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -164,7 +164,7 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi case clang.CursorMacroDefinition: ct.ProcessMarco(cursor) case clang.CursorEnumDecl: - // todo(zzy) + ct.ProcessEnum(cursor) case clang.CursorClassDecl: ct.PushScope(cursor) ct.ProcessClass(cursor) @@ -204,11 +204,12 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { expr = ct.ProcessType(t.CanonicalType()) case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: if t.Kind == clang.TypeConstantArray { - valueStr := make([]c.Char, 20) - c.Sprintf(unsafe.SliceData(valueStr), c.Str("%d"), t.ArraySize()) + len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) + c.Sprintf(len, c.Str("%lld"), t.ArraySize()) + defer c.Free(unsafe.Pointer(len)) expr = &ast.ArrayType{ Elt: ct.ProcessType(t.ArrayElementType()), - Len: &ast.BasicLit{Kind: ast.IntLit, Value: c.GoString(unsafe.SliceData(valueStr))}, + Len: &ast.BasicLit{Kind: ast.IntLit, Value: c.GoString(len)}, } } else if t.Kind == clang.TypeIncompleteArray { // incomplete array havent len expr @@ -240,6 +241,49 @@ func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { return fn } +type visitEnumContext struct { + enum *[]*ast.EnumItem + converter *Converter +} + +func visitEnum(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + ctx := (*visitEnumContext)(clientData) + if cursor.Kind == clang.CursorEnumConstantDecl { + name := cursor.String() + val := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) + c.Sprintf(val, c.Str("%lld"), cursor.EnumConstantDeclValue()) + defer c.Free(unsafe.Pointer(val)) + defer name.Dispose() + enum := &ast.EnumItem{ + Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Value: &ast.BasicLit{ + Kind: ast.IntLit, + Value: c.GoString(val), + }, + } + *ctx.enum = append(*ctx.enum, enum) + } + return clang.ChildVisit_Continue +} + +func (ct *Converter) ProcessEnum(cursor clang.Cursor) { + name := cursor.String() + defer name.Dispose() + items := make([]*ast.EnumItem, 0) + ctx := &visitEnumContext{ + enum: &items, + converter: ct, + } + clang.VisitChildren(cursor, visitEnum, c.Pointer(ctx)) + + enum := &ast.EnumTypeDecl{ + DeclBase: ct.CreateDeclBase(cursor), + Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Items: items, + } + ct.curFile.Decls = append(ct.curFile.Decls, enum) +} + // current only collect marco which defined in file func (ct *Converter) ProcessMarco(cursor clang.Cursor) { if ct.curFile == nil { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index bba657f6..e9b9a73e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -11,6 +11,7 @@ func main() { TestComment() TestStructDecl() TestClassDecl() + TestEnumDecl() } func TestFuncDecl() { @@ -194,3 +195,40 @@ func TestClassDecl() { converter.Dispose() } } + +func TestEnumDecl() { + testCases := []string{ + `enum Foo { + a, + b, + c, + };`, + `enum Foo { + a = 1, + b = 2, + c = 4, + };`, + `enum Foo { + a = 1, + b, + c, + };`, + } + + for i, content := range testCases { + converter, err := parse.NewConverter(content, true) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.MarshalASTFiles() + c.Printf(c.Str("TestEnumDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 47e9cb36..893bc838 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -1296,6 +1296,144 @@ TestClassDecl Case 2: } } +TestEnumDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "0" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }] + }], + "includes": [], + "macros": [] + } +} + +TestEnumDecl Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "4" + } + }] + }], + "includes": [], + "macros": [] + } +} + +TestEnumDecl Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "3" + } + }] + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 91a9c594..a632a17c 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -66,6 +66,14 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { } root := cjson.Object() switch d := decl.(type) { + case *ast.EnumTypeDecl: + MarshalASTDeclBase(d.DeclBase, root) + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + items := cjson.Array() + for _, i := range d.Items { + items.AddItem(MarshalASTExpr(i)) + } + root.SetItem(c.Str("Items"), items) case *ast.FuncDecl: MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) @@ -123,6 +131,9 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Names"), names) case *ast.Ident: root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + case *ast.EnumItem: + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value)) case *ast.BasicLit: root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) From 48efd6689e7dda7ba6aab8ef01b0cf3257cdea93 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 19 Aug 2024 18:01:46 +0800 Subject: [PATCH 19/67] llcppsigfetch:conveter config --- chore/_xtool/llcppsigfetch/parse/cvt.go | 93 ++++++++++++------- .../parse/cvt_test/decl_test/decl.go | 30 ++++-- .../cvt_test/preprocess_test/preprocess.go | 10 +- .../parse/cvt_test/type_test/type.go | 58 ++++++------ chore/_xtool/llcppsigfetch/parse/parse.go | 5 +- 5 files changed, 127 insertions(+), 69 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 07d4efd4..ff93aa0f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -22,40 +22,16 @@ type Converter struct { scopeStack []ast.Expr //namespace & class } -func NewConverter(file string, temp bool) (*Converter, error) { - args := []*c.Char{ - c.Str("-x"), - c.Str("c++"), - c.Str("-std=c++11"), - } - index := clang.CreateIndex(0, 0) +type Config struct { + File string + Temp bool + Args []string +} - var unit *clang.TranslationUnit - - if temp { - content := c.AllocaCStr(file) - tempFile := &clang.UnsavedFile{ - Filename: c.Str("temp.h"), - Contents: content, - Length: c.Ulong(c.Strlen(content)), - } - unit = index.ParseTranslationUnit( - tempFile.Filename, - unsafe.SliceData(args), c.Int(len(args)), - tempFile, 1, - clang.DetailedPreprocessingRecord, - ) - } else { - unit = index.ParseTranslationUnit( - c.AllocaCStr(file), - unsafe.SliceData(args), c.Int(len(args)), - nil, 0, - clang.DetailedPreprocessingRecord, - ) - } - - if unit == nil { - return nil, errors.New("failed to parse translation unit") +func NewConverter(config *Config) (*Converter, error) { + index, unit, err := CreateTranslationUnit(config) + if err != nil { + return nil, err } return &Converter{ @@ -65,6 +41,52 @@ func NewConverter(file string, temp bool) (*Converter, error) { }, nil } +func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) { + if config.Args == nil { + config.Args = []string{"-x", "c++", "-std=c++11"} + } + + cArgs := make([]*c.Char, len(config.Args)) + for i, arg := range config.Args { + cArgs[i] = c.AllocaCStr(arg) + } + + index := clang.CreateIndex(0, 0) + + var unit *clang.TranslationUnit + + if config.Temp { + content := c.AllocaCStr(config.File) + tempFile := &clang.UnsavedFile{ + Filename: c.Str("temp.h"), + Contents: content, + Length: c.Ulong(c.Strlen(content)), + } + + unit = index.ParseTranslationUnit( + tempFile.Filename, + unsafe.SliceData(cArgs), c.Int(len(cArgs)), + tempFile, 1, + clang.DetailedPreprocessingRecord, + ) + + } else { + cFile := c.AllocaCStr(config.File) + unit = index.ParseTranslationUnit( + cFile, + unsafe.SliceData(cArgs), c.Int(len(cArgs)), + nil, 0, + clang.DetailedPreprocessingRecord, + ) + } + + if unit == nil { + return nil, nil, errors.New("failed to parse translation unit") + } + + return index, unit, nil +} + func (ct *Converter) Dispose() { ct.index.Dispose() ct.unit.Dispose() @@ -281,6 +303,7 @@ func (ct *Converter) ProcessEnum(cursor clang.Cursor) { Name: &ast.Ident{Name: c.GoString(name.CStr())}, Items: items, } + ct.curFile.Decls = append(ct.curFile.Decls, enum) } @@ -465,6 +488,10 @@ func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { flags |= ast.Long | ast.Double case clang.TypeFloat128: kind = ast.Float128 + case clang.TypeComplex: // double | float + kind = ast.Complex + complexKind := t.ElementType().Kind + println("complex kind:", complexKind) // in unit test, will panic default: // like IBM128,NullPtr,Accum kindStr := t.Kind.String() diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index e9b9a73e..bf60ef25 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -29,7 +29,10 @@ func TestFuncDecl() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } @@ -68,7 +71,10 @@ func TestScope() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } @@ -114,7 +120,10 @@ void foo();`, } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } @@ -148,7 +157,10 @@ func TestStructDecl() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } @@ -179,7 +191,10 @@ func TestClassDecl() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } @@ -216,7 +231,10 @@ func TestEnumDecl() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go index f5ef45a5..e7d32afd 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go @@ -17,7 +17,10 @@ func TestDefine() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } @@ -41,7 +44,10 @@ func TestInclude() { } for i, content := range testCases { - converter, err := parse.NewConverter(content, true) + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) if err != nil { panic(err) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index 29d985cb..e414e985 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -14,39 +14,39 @@ func main() { func TestBuiltinType() { tests := []struct { name string - typeKind clang.TypeKind + typ *clang.Type expected ast.BuiltinType }{ - {"Void", clang.TypeVoid, ast.BuiltinType{Kind: ast.Void}}, - {"Bool", clang.TypeBool, ast.BuiltinType{Kind: ast.Bool}}, - {"Char_S", clang.TypeCharS, ast.BuiltinType{Kind: ast.Char, Flags: ast.Signed}}, - {"Char_U", clang.TypeCharU, ast.BuiltinType{Kind: ast.Char, Flags: ast.Unsigned}}, - {"Char16", clang.TypeChar16, ast.BuiltinType{Kind: ast.Char16}}, - {"Char32", clang.TypeChar32, ast.BuiltinType{Kind: ast.Char32}}, - {"WChar", clang.TypeWChar, ast.BuiltinType{Kind: ast.WChar}}, - {"Short", clang.TypeShort, ast.BuiltinType{Kind: ast.Int, Flags: ast.Short}}, - {"UShort", clang.TypeUShort, ast.BuiltinType{Kind: ast.Int, Flags: ast.Short | ast.Unsigned}}, - {"Int", clang.TypeInt, ast.BuiltinType{Kind: ast.Int}}, - {"UInt", clang.TypeUInt, ast.BuiltinType{Kind: ast.Int, Flags: ast.Unsigned}}, - {"Long", clang.TypeLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.Long}}, - {"ULong", clang.TypeULong, ast.BuiltinType{Kind: ast.Int, Flags: ast.Long | ast.Unsigned}}, - {"LongLong", clang.TypeLongLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong}}, - {"ULongLong", clang.TypeULongLong, ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong | ast.Unsigned}}, - {"Int128", clang.TypeInt128, ast.BuiltinType{Kind: ast.Int128}}, - {"UInt128", clang.TypeUInt128, ast.BuiltinType{Kind: ast.Int128, Flags: ast.Unsigned}}, - {"Float", clang.TypeFloat, ast.BuiltinType{Kind: ast.Float}}, - {"Half", clang.TypeHalf, ast.BuiltinType{Kind: ast.Float16}}, - {"Float16", clang.TypeFloat16, ast.BuiltinType{Kind: ast.Float16}}, - {"Double", clang.TypeDouble, ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}}, - {"LongDouble", clang.TypeLongDouble, ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}}, - {"Float128", clang.TypeFloat128, ast.BuiltinType{Kind: ast.Float128}}, - {"Unknown", clang.TypeIbm128, ast.BuiltinType{Kind: ast.Void}}, + {"Void", btType(clang.TypeVoid), ast.BuiltinType{Kind: ast.Void}}, + {"Bool", btType(clang.TypeBool), ast.BuiltinType{Kind: ast.Bool}}, + {"Char_S", btType(clang.TypeCharS), ast.BuiltinType{Kind: ast.Char, Flags: ast.Signed}}, + {"Char_U", btType(clang.TypeCharU), ast.BuiltinType{Kind: ast.Char, Flags: ast.Unsigned}}, + {"Char16", btType(clang.TypeChar16), ast.BuiltinType{Kind: ast.Char16}}, + {"Char32", btType(clang.TypeChar32), ast.BuiltinType{Kind: ast.Char32}}, + {"WChar", btType(clang.TypeWChar), ast.BuiltinType{Kind: ast.WChar}}, + {"Short", btType(clang.TypeShort), ast.BuiltinType{Kind: ast.Int, Flags: ast.Short}}, + {"UShort", btType(clang.TypeUShort), ast.BuiltinType{Kind: ast.Int, Flags: ast.Short | ast.Unsigned}}, + {"Int", btType(clang.TypeInt), ast.BuiltinType{Kind: ast.Int}}, + {"UInt", btType(clang.TypeUInt), ast.BuiltinType{Kind: ast.Int, Flags: ast.Unsigned}}, + {"Long", btType(clang.TypeLong), ast.BuiltinType{Kind: ast.Int, Flags: ast.Long}}, + {"ULong", btType(clang.TypeULong), ast.BuiltinType{Kind: ast.Int, Flags: ast.Long | ast.Unsigned}}, + {"LongLong", btType(clang.TypeLongLong), ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong}}, + {"ULongLong", btType(clang.TypeULongLong), ast.BuiltinType{Kind: ast.Int, Flags: ast.LongLong | ast.Unsigned}}, + {"Int128", btType(clang.TypeInt128), ast.BuiltinType{Kind: ast.Int128}}, + {"UInt128", btType(clang.TypeUInt128), ast.BuiltinType{Kind: ast.Int128, Flags: ast.Unsigned}}, + {"Float", btType(clang.TypeFloat), ast.BuiltinType{Kind: ast.Float}}, + {"Half", btType(clang.TypeHalf), ast.BuiltinType{Kind: ast.Float16}}, + {"Float16", btType(clang.TypeFloat16), ast.BuiltinType{Kind: ast.Float16}}, + {"Double", btType(clang.TypeDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}}, + {"LongDouble", btType(clang.TypeLongDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}}, + {"Float128", btType(clang.TypeFloat128), ast.BuiltinType{Kind: ast.Float128}}, + {"Unknown", btType(clang.TypeIbm128), ast.BuiltinType{Kind: ast.Void}}, } + converter := &parse.Converter{} converter.Convert() for _, bt := range tests { - t := clang.Type{Kind: bt.typeKind} - res := converter.ProcessBuiltinType(t) + res := converter.ProcessBuiltinType(*bt.typ) if res.Kind != bt.expected.Kind { fmt.Printf("%s Kind mismatch:got %d want %d, \n", bt.name, res.Kind, bt.expected.Kind) } @@ -56,3 +56,7 @@ func TestBuiltinType() { fmt.Printf("%s:flags:%d kind:%d\n", bt.name, res.Flags, res.Kind) } } + +func btType(kind clang.TypeKind) *clang.Type { + return &clang.Type{Kind: kind} +} diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index e7b3bee4..ee42da75 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -59,7 +59,10 @@ func (p *Context) processFile(path string) error { } func (p *Context) parseFile(path string) (map[string]*ast.File, error) { - converter, err := NewConverter(path, false) + converter, err := NewConverter(&Config{ + File: path, + Temp: false, + }) if err != nil { return nil, errors.New("failed to create converter " + path) } From f0e92343cb9aaac0a2b99b7ecdebbfee211d54d6 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 19 Aug 2024 21:39:42 +0800 Subject: [PATCH 20/67] llcppsigfetch:complex --- chore/_xtool/llcppsigfetch/parse/cvt.go | 9 +++- .../parse/cvt_test/type_test/llgo.expect | 3 ++ .../parse/cvt_test/type_test/type.go | 50 +++++++++++++++++-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index ff93aa0f..4340dd4e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -488,10 +488,15 @@ func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { flags |= ast.Long | ast.Double case clang.TypeFloat128: kind = ast.Float128 - case clang.TypeComplex: // double | float + case clang.TypeComplex: kind = ast.Complex complexKind := t.ElementType().Kind - println("complex kind:", complexKind) // in unit test, will panic + if complexKind == clang.TypeLongDouble { + flags |= ast.Long | ast.Double + } else if complexKind == clang.TypeDouble { + flags |= ast.Double + } + // float complfex flag is not set default: // like IBM128,NullPtr,Accum kindStr := t.Kind.String() diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 9d5fac9c..4ef2b3c1 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -22,6 +22,9 @@ Float16:flags:0 kind:9 Double:flags:16 kind:8 LongDouble:flags:20 kind:8 Float128:flags:0 kind:10 +Complex:flags:0 kind:11 +Complex:flags:16 kind:11 +Complex:flags:20 kind:11 Unknown:flags:0 kind:0 #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index e414e985..c7260d10 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "unsafe" "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" @@ -14,7 +15,7 @@ func main() { func TestBuiltinType() { tests := []struct { name string - typ *clang.Type + typ clang.Type expected ast.BuiltinType }{ {"Void", btType(clang.TypeVoid), ast.BuiltinType{Kind: ast.Void}}, @@ -40,13 +41,16 @@ func TestBuiltinType() { {"Double", btType(clang.TypeDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}}, {"LongDouble", btType(clang.TypeLongDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}}, {"Float128", btType(clang.TypeFloat128), ast.BuiltinType{Kind: ast.Float128}}, + {"Complex", mockComplexType(0), ast.BuiltinType{Kind: ast.Complex}}, + {"Complex", mockComplexType(ast.Double), ast.BuiltinType{Flags: ast.Double, Kind: ast.Complex}}, + {"Complex", mockComplexType(ast.Long | ast.Double), ast.BuiltinType{Flags: ast.Long | ast.Double, Kind: ast.Complex}}, {"Unknown", btType(clang.TypeIbm128), ast.BuiltinType{Kind: ast.Void}}, } converter := &parse.Converter{} converter.Convert() for _, bt := range tests { - res := converter.ProcessBuiltinType(*bt.typ) + res := converter.ProcessBuiltinType(bt.typ) if res.Kind != bt.expected.Kind { fmt.Printf("%s Kind mismatch:got %d want %d, \n", bt.name, res.Kind, bt.expected.Kind) } @@ -57,6 +61,44 @@ func TestBuiltinType() { } } -func btType(kind clang.TypeKind) *clang.Type { - return &clang.Type{Kind: kind} +func btType(kind clang.TypeKind) clang.Type { + return clang.Type{Kind: kind} +} + +func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + typ := (*clang.Type)(clientData) + if cursor.Kind == clang.CursorVarDecl && cursor.Type().Kind == clang.TypeComplex { + *typ = cursor.Type() + } + return clang.ChildVisit_Continue +} + +// mock complex type, this type cannot be directly created in Go +func mockComplexType(flag ast.TypeFlag) clang.Type { + var typeStr string + if flag&(ast.Long|ast.Double) == (ast.Long | ast.Double) { + typeStr = "long double" + } else if flag&ast.Double != 0 { + typeStr = "double" + } else { + typeStr = "float" + } + + code := fmt.Sprintf("#include \n%s complex z;", typeStr) + index, unit, err := parse.CreateTranslationUnit(&parse.Config{ + File: code, + Temp: true, + Args: []string{"-x", "c", "-std=c99"}, + }) + if err != nil { + panic(err) + } + + defer index.Dispose() + + cursor := unit.Cursor() + complex := &clang.Type{} + clang.VisitChildren(cursor, visit, unsafe.Pointer(complex)) + + return *complex } From bf8aa502f9aed02f0a220a1e9f4bfef018b6f3c9 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 20 Aug 2024 10:08:25 +0800 Subject: [PATCH 21/67] llcppsigfetch:marco tokens & refine test --- chore/_xtool/llcppsigfetch/parse/cvt.go | 33 +++-- .../llcppsigfetch/parse/cvt_test/cvt.go | 28 ++++ .../parse/cvt_test/decl_test/decl.go | 129 +----------------- .../cvt_test/preprocess_test/llgo.expect | 62 +++++---- .../cvt_test/preprocess_test/preprocess.go | 46 +------ chore/_xtool/llcppsigfetch/parse/dump.go | 16 +-- 6 files changed, 104 insertions(+), 210 deletions(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 4340dd4e..b91a5a32 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -11,6 +11,7 @@ import ( "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/llcppg/ast" + "github.com/goplus/llgo/chore/llcppg/token" ) type Converter struct { @@ -322,23 +323,17 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) { tokensSlice := unsafe.Slice(tokens, int(numTokens)) macro := &ast.Macro{ - Name: &ast.TokenInfo{ - Token: ast.Token(tokensSlice[0].Kind()), - Lit: c.GoString(ct.unit.Token(tokensSlice[0]).CStr()), - }, - Body: make([]*ast.TokenInfo, 0), + Name: c.GoString(name.CStr()), + Tokens: make([]*ast.Token, 0), } - if numTokens > 1 { //have body - for i := 1; i < int(numTokens); i++ { - tok := tokensSlice[i] - tokStr := ct.unit.Token(tok) - macro.Body = append(macro.Body, &ast.TokenInfo{ - Token: ast.Token(tok.Kind()), - Lit: c.GoString(tokStr.CStr()), - }) - tokStr.Dispose() - } + for _, tok := range tokensSlice { + tokStr := ct.unit.Token(tok) + macro.Tokens = append(macro.Tokens, &ast.Token{ + Token: toToken(tok), + Lit: c.GoString(tokStr.CStr()), + }) + tokStr.Dispose() } ct.curFile.Macros = append(ct.curFile.Macros, macro) } @@ -530,3 +525,11 @@ func IsExplicitUnsigned(t clang.Type) bool { t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong || t.Kind == clang.TypeUInt128 } + +func toToken(tok clang.Token) token.Token { + if tok.Kind() < clang.Punctuation || tok.Kind() > clang.Comment { + return token.ILLEGAL + } else { + return token.Token(tok.Kind() + 1) + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go new file mode 100644 index 00000000..3b59cd85 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go @@ -0,0 +1,28 @@ +package cvttest + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" +) + +func RunTest(testName string, testCases []string) { + for i, content := range testCases { + converter, err := parse.NewConverter(&parse.Config{ + File: content, + Temp: true, + }) + if err != nil { + panic(err) + } + + _, err = converter.Convert() + if err != nil { + panic(err) + } + + json := converter.MarshalASTFiles() + c.Printf(c.Str("%s Case %d:\n%s\n\n"), c.AllocaCStr(testName), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index bf60ef25..27cd2253 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -1,8 +1,7 @@ package main import ( - "github.com/goplus/llgo/c" - "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" + test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" ) func main() { @@ -27,26 +26,7 @@ func TestFuncDecl() { `float* foo(char str[], double x);`, `float* foo(int arr[3][4]);`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestFuncDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestFuncDecl", testCases) } func TestScope() { @@ -69,26 +49,7 @@ func TestScope() { }; }`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestScope Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestScope", testCases) } func TestComment() { @@ -118,26 +79,7 @@ void foo();`, */ void foo();`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestComment Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestComment", testCases) } func TestStructDecl() { @@ -155,26 +97,7 @@ func TestStructDecl() { float foo(int a,double b);; };`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestStructDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestStructDecl", testCases) } func TestClassDecl() { @@ -189,26 +112,7 @@ func TestClassDecl() { float foo(int a,double b);; };`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestClassDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestClassDecl", testCases) } func TestEnumDecl() { @@ -229,24 +133,5 @@ func TestEnumDecl() { c, };`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestEnumDecl Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestEnumDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect index 879d6316..3332d313 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect @@ -6,13 +6,10 @@ TestDefine Case 1: "decls": [], "includes": [], "macros": [{ - "Name": { - "Token": 2, - "Lit": "OK" - }, - "Body": [{ + "Name": "DEBUG", + "Tokens": [{ "Token": 3, - "Lit": "1" + "Lit": "DEBUG" }] }] } @@ -25,45 +22,64 @@ TestDefine Case 2: "decls": [], "includes": [], "macros": [{ - "Name": { - "Token": 2, - "Lit": "SQUARE" - }, - "Body": [{ - "Token": 0, + "Name": "OK", + "Tokens": [{ + "Token": 3, + "Lit": "OK" + }, { + "Token": 4, + "Lit": "1" + }] + }] + } +} + +TestDefine Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [], + "includes": [], + "macros": [{ + "Name": "SQUARE", + "Tokens": [{ + "Token": 3, + "Lit": "SQUARE" + }, { + "Token": 1, "Lit": "(" }, { - "Token": 2, + "Token": 3, "Lit": "x" }, { - "Token": 0, + "Token": 1, "Lit": ")" }, { - "Token": 0, + "Token": 1, "Lit": "(" }, { - "Token": 0, + "Token": 1, "Lit": "(" }, { - "Token": 2, + "Token": 3, "Lit": "x" }, { - "Token": 0, + "Token": 1, "Lit": ")" }, { - "Token": 0, + "Token": 1, "Lit": "*" }, { - "Token": 0, + "Token": 1, "Lit": "(" }, { - "Token": 2, + "Token": 3, "Lit": "x" }, { - "Token": 0, + "Token": 1, "Lit": ")" }, { - "Token": 0, + "Token": 1, "Lit": ")" }] }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go index e7d32afd..df41579b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go @@ -1,8 +1,7 @@ package main import ( - "github.com/goplus/llgo/c" - "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" + test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" ) func main() { @@ -12,29 +11,11 @@ func main() { func TestDefine() { testCases := []string{ + `#define DEBUG`, `#define OK 1`, `#define SQUARE(x) ((x) * (x))`, } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestDefine Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestDefine", testCases) } func TestInclude() { @@ -42,24 +23,5 @@ func TestInclude() { `#include "foo.h"`, // `#include `, // Standard libraries are mostly platform-dependent } - - for i, content := range testCases { - converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, - }) - if err != nil { - panic(err) - } - - _, err = converter.Convert() - if err != nil { - panic(err) - } - - json := converter.MarshalASTFiles() - c.Printf(c.Str("TestInclude Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) - - converter.Dispose() - } + test.RunTest("TestInclude", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index a632a17c..716ecfe1 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -41,22 +41,22 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { macros := cjson.Array() for _, m := range file.Macros { marco := cjson.Object() - marco.SetItem(c.Str("Name"), TokenInfo(m.Name)) - body := cjson.Array() - for _, b := range m.Body { - body.AddItem(TokenInfo(b)) + marco.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(m.Name))) + tokens := cjson.Array() + for _, tok := range m.Tokens { + tokens.AddItem(Token(tok)) } - marco.SetItem(c.Str("Body"), body) + marco.SetItem(c.Str("Tokens"), tokens) macros.AddItem(marco) } root.SetItem(c.Str("macros"), macros) } return root } -func TokenInfo(t *ast.TokenInfo) *cjson.JSON { +func Token(tok *ast.Token) *cjson.JSON { root := cjson.Object() - root.SetItem(c.Str("Token"), cjson.Number(float64(t.Token))) - root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(t.Lit))) + root.SetItem(c.Str("Token"), cjson.Number(float64(tok.Token))) + root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(tok.Lit))) return root } From 43bcf1051dd37a3267dca93933c709902cf6ee23 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 20 Aug 2024 10:43:01 +0800 Subject: [PATCH 22/67] llcppsigfetch:union --- chore/_xtool/llcppsigfetch/parse/cvt.go | 7 +++ .../parse/cvt_test/decl_test/decl.go | 11 ++++ .../parse/cvt_test/decl_test/llgo.expect | 51 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index b91a5a32..64ed372f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -194,6 +194,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi ct.PopScope() case clang.CursorStructDecl: ct.ProcessStruct(cursor) + case clang.CursorUnionDecl: + ct.ProcessUnion(cursor) case clang.CursorFunctionDecl: ct.curFile.Decls = append(ct.curFile.Decls, ct.ProcessFunc(cursor)) case clang.CursorNamespace: @@ -435,6 +437,11 @@ func (ct *Converter) ProcessStruct(cursor clang.Cursor) { ct.curFile.Decls = append(ct.curFile.Decls, structDecl) } +func (ct *Converter) ProcessUnion(cursor clang.Cursor) { + structDecl := ct.ProcessStructOrClass(cursor, ast.Union) + ct.curFile.Decls = append(ct.curFile.Decls, structDecl) +} + func (ct *Converter) ProcessClass(cursor clang.Cursor) { classDecl := ct.ProcessStructOrClass(cursor, ast.Class) // other logic for class diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 27cd2253..ec00ba19 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -10,6 +10,7 @@ func main() { TestComment() TestStructDecl() TestClassDecl() + TestUnionDecl() TestEnumDecl() } @@ -100,6 +101,16 @@ func TestStructDecl() { test.RunTest("TestStructDecl", testCases) } +func TestUnionDecl() { + testCases := []string{ + `union A { + int a; + int b; + };`, + } + test.RunTest("TestUnionDecl", testCases) +} + func TestClassDecl() { testCases := []string{ `class A { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 893bc838..5a37e43c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -1296,6 +1296,57 @@ TestClassDecl Case 2: } } +TestUnionDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + TestEnumDecl Case 1: { "temp.h": { From 1f72a520155d36ad1c3e89a868dc29f9eed8bacf Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 20 Aug 2024 11:01:08 +0800 Subject: [PATCH 23/67] llcppsigfetch:free token --- chore/_xtool/llcppsigfetch/parse/cvt.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 64ed372f..7ee1e1e8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -322,6 +322,8 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) { var numTokens c.Uint var tokens *clang.Token ct.unit.Tokenize(ran, &tokens, &numTokens) + defer ct.unit.DisposeTokens(tokens, numTokens) + tokensSlice := unsafe.Slice(tokens, int(numTokens)) macro := &ast.Macro{ From 697c21b120af3b725f0da396964db26142da4f02 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 20 Aug 2024 14:19:48 +0800 Subject: [PATCH 24/67] llcppsigfetch:typedecl name --- chore/_xtool/llcppsigfetch/parse/cvt.go | 1 + .../parse/cvt_test/decl_test/llgo.expect | 24 +++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 1 + 3 files changed, 26 insertions(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 7ee1e1e8..88c2703b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -427,6 +427,7 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast decl := &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Tag: tag, + Name: &ast.Ident{Name: c.GoString(name.CStr())}, Fields: fields, Methods: methods, } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 5a37e43c..b97682c7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -554,6 +554,9 @@ TestScope Case 4: "Name": "a" }, "Tag": 3, + "Name": { + "Name": "a" + }, "Fields": { "List": [] }, @@ -606,6 +609,9 @@ TestScope Case 5: } }, "Tag": 3, + "Name": { + "Name": "b" + }, "Fields": { "List": [] }, @@ -952,6 +958,9 @@ TestStructDecl Case 1: }, "Parent": null, "Tag": 0, + "Name": { + "Name": "A" + }, "Fields": { "List": [{ "Type": { @@ -1003,6 +1012,9 @@ TestStructDecl Case 2: }, "Parent": null, "Tag": 0, + "Name": { + "Name": "A" + }, "Fields": { "List": [{ "Type": { @@ -1054,6 +1066,9 @@ TestStructDecl Case 3: }, "Parent": null, "Tag": 0, + "Name": { + "Name": "A" + }, "Fields": { "List": [{ "Type": { @@ -1155,6 +1170,9 @@ TestClassDecl Case 1: "Name": "A" }, "Tag": 3, + "Name": { + "Name": "A" + }, "Fields": { "List": [{ "Type": { @@ -1208,6 +1226,9 @@ TestClassDecl Case 2: "Name": "A" }, "Tag": 3, + "Name": { + "Name": "A" + }, "Fields": { "List": [{ "Type": { @@ -1309,6 +1330,9 @@ TestUnionDecl Case 1: }, "Parent": null, "Tag": 1, + "Name": { + "Name": "A" + }, "Fields": { "List": [{ "Type": { diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 716ecfe1..c43951da 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -81,6 +81,7 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { case *ast.TypeDecl: MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) methods := cjson.Array() for _, m := range d.Methods { From 2974b23f26adab72675a3ac5c8a72ccfb69987be Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 20 Aug 2024 17:58:06 +0800 Subject: [PATCH 25/67] llcppsigfetch:refine file handling, remove curFile field --- chore/_xtool/llcppsigfetch/parse/cvt.go | 93 +++++++++++++------------ 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 88c2703b..df10cac8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -17,7 +17,6 @@ import ( type Converter struct { Files map[string]*ast.File curLoc ast.Location - curFile *ast.File index *clang.Index unit *clang.TranslationUnit scopeStack []ast.Expr //namespace & class @@ -120,7 +119,7 @@ func (ct *Converter) GetCurScope() ast.Expr { return ct.scopeStack[len(ct.scopeStack)-1] } -func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { +func (ct *Converter) UpdateLoc(cursor clang.Cursor) { loc := cursor.Location() var file clang.File loc.SpellingLocation(&file, nil, nil, nil) @@ -129,25 +128,30 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { if filename.CStr() == nil { //todo(zzy): For some built-in macros, there is no file. + ct.curLoc = ast.Location{File: ""} return } filePath := c.GoString(filename.CStr()) ct.curLoc = ast.Location{File: filePath} - if ct.curFile == nil || ct.curFile.Path != filePath { - if f, ok := ct.Files[filePath]; ok { - ct.curFile = f - } else { - ct.curFile = &ast.File{ - Path: filePath, - Decls: make([]ast.Decl, 0), - Includes: make([]*ast.Include, 0), - Macros: make([]*ast.Macro, 0), - } - ct.Files[filePath] = ct.curFile - } +} + +func (ct *Converter) GetCurFile() *ast.File { + if ct.curLoc.File == "" { + return nil } + file, ok := ct.Files[ct.curLoc.File] + if !ok { + file = &ast.File{ + Path: ct.curLoc.File, + Decls: make([]ast.Decl, 0), + Includes: make([]*ast.Include, 0), + Macros: make([]*ast.Macro, 0), + } + ct.Files[ct.curLoc.File] = file + } + return file } func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { @@ -179,25 +183,36 @@ func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { // visit top decls (struct,class,function,enum & marco,include) func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ct := (*Converter)(clientData) - ct.UpdateCurFile(cursor) + ct.UpdateLoc(cursor) + + curFile := ct.GetCurFile() + if curFile == nil { + return clang.ChildVisit_Continue + } switch cursor.Kind { case clang.CursorInclusionDirective: - ct.ProcessInclude(cursor) + include := ct.ProcessInclude(cursor) + curFile.Includes = append(curFile.Includes, include) case clang.CursorMacroDefinition: - ct.ProcessMarco(cursor) + marco := ct.ProcessMarco(cursor) + curFile.Macros = append(curFile.Macros, marco) case clang.CursorEnumDecl: - ct.ProcessEnum(cursor) + enum := ct.ProcessEnum(cursor) + curFile.Decls = append(curFile.Decls, enum) case clang.CursorClassDecl: ct.PushScope(cursor) - ct.ProcessClass(cursor) + classDecl := ct.ProcessClass(cursor) + curFile.Decls = append(curFile.Decls, classDecl) ct.PopScope() case clang.CursorStructDecl: - ct.ProcessStruct(cursor) + structDecl := ct.ProcessStruct(cursor) + curFile.Decls = append(curFile.Decls, structDecl) case clang.CursorUnionDecl: - ct.ProcessUnion(cursor) + unionDecl := ct.ProcessUnion(cursor) + curFile.Decls = append(curFile.Decls, unionDecl) case clang.CursorFunctionDecl: - ct.curFile.Decls = append(ct.curFile.Decls, ct.ProcessFunc(cursor)) + curFile.Decls = append(curFile.Decls, ct.ProcessFunc(cursor)) case clang.CursorNamespace: ct.PushScope(cursor) clang.VisitChildren(cursor, visit, c.Pointer(ct)) @@ -291,7 +306,7 @@ func visitEnum(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chi return clang.ChildVisit_Continue } -func (ct *Converter) ProcessEnum(cursor clang.Cursor) { +func (ct *Converter) ProcessEnum(cursor clang.Cursor) *ast.EnumTypeDecl { name := cursor.String() defer name.Dispose() items := make([]*ast.EnumItem, 0) @@ -301,20 +316,15 @@ func (ct *Converter) ProcessEnum(cursor clang.Cursor) { } clang.VisitChildren(cursor, visitEnum, c.Pointer(ctx)) - enum := &ast.EnumTypeDecl{ + return &ast.EnumTypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, Items: items, } - - ct.curFile.Decls = append(ct.curFile.Decls, enum) } // current only collect marco which defined in file -func (ct *Converter) ProcessMarco(cursor clang.Cursor) { - if ct.curFile == nil { - return - } +func (ct *Converter) ProcessMarco(cursor clang.Cursor) *ast.Macro { name := cursor.String() defer name.Dispose() @@ -339,13 +349,13 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) { }) tokStr.Dispose() } - ct.curFile.Macros = append(ct.curFile.Macros, macro) + return macro } -func (ct *Converter) ProcessInclude(cursor clang.Cursor) { +func (ct *Converter) ProcessInclude(cursor clang.Cursor) *ast.Include { name := cursor.String() defer name.Dispose() - ct.curFile.Includes = append(ct.curFile.Includes, &ast.Include{Path: c.GoString(name.CStr())}) + return &ast.Include{Path: c.GoString(name.CStr())} } type visitFieldContext struct { @@ -418,6 +428,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { } func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { + name := cursor.String() defer name.Dispose() @@ -435,20 +446,16 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast return decl } -func (ct *Converter) ProcessStruct(cursor clang.Cursor) { - structDecl := ct.ProcessStructOrClass(cursor, ast.Struct) - ct.curFile.Decls = append(ct.curFile.Decls, structDecl) +func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessStructOrClass(cursor, ast.Struct) } -func (ct *Converter) ProcessUnion(cursor clang.Cursor) { - structDecl := ct.ProcessStructOrClass(cursor, ast.Union) - ct.curFile.Decls = append(ct.curFile.Decls, structDecl) +func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessStructOrClass(cursor, ast.Union) } -func (ct *Converter) ProcessClass(cursor clang.Cursor) { - classDecl := ct.ProcessStructOrClass(cursor, ast.Class) - // other logic for class - ct.curFile.Decls = append(ct.curFile.Decls, classDecl) +func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessStructOrClass(cursor, ast.Class) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { From 5e5c84ba27e8a46fce9cd1bdc7d28a938e544021 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 20 Aug 2024 18:06:01 +0800 Subject: [PATCH 26/67] llcppsigfetch:json memory free --- chore/_xtool/llcppsigfetch/llcppsigfetch.go | 10 ++++++---- chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go | 8 ++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 9c3cd6b3..6f9dcd37 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -22,9 +22,9 @@ import ( "os" "path/filepath" "strings" - "unsafe" "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" "github.com/goplus/llgo/chore/_xtool/llcppsymg/config" ) @@ -78,7 +78,9 @@ func getHeaderFiles(cflags string, files []string) []string { } func outputInfo(context *parse.Context) { - output := context.Output().Print() - defer c.Free(unsafe.Pointer(output)) - c.Printf(output) + info := context.Output() + str := info.Print() + defer cjson.FreeCStr(str) + defer info.Delete() + c.Printf(str) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go index 3b59cd85..974a4bb7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go @@ -2,6 +2,7 @@ package cvttest import ( "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" ) @@ -20,9 +21,12 @@ func RunTest(testName string, testCases []string) { panic(err) } - json := converter.MarshalASTFiles() - c.Printf(c.Str("%s Case %d:\n%s\n\n"), c.AllocaCStr(testName), c.Int(i+1), json.Print()) + result := converter.MarshalASTFiles() + str := result.Print() + c.Printf(c.Str("%s Case %d:\n%s\n\n"), c.AllocaCStr(testName), c.Int(i+1), str) + cjson.FreeCStr(str) + result.Delete() converter.Dispose() } } From 815fe25f2cecf425d0ce81e62971249badf0fff1 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 21 Aug 2024 14:50:42 +0800 Subject: [PATCH 27/67] llcppsigfetch:anonymous record name --- chore/_xtool/llcppsigfetch/parse/cvt.go | 19 ++- .../parse/cvt_test/decl_test/decl.go | 7 + .../parse/cvt_test/decl_test/llgo.expect | 124 +++++++++++++++--- chore/_xtool/llcppsigfetch/parse/dump.go | 6 + 4 files changed, 132 insertions(+), 24 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index df10cac8..ee3a2f1b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -427,10 +427,15 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { return methods } -func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { +func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { + anony := cursor.IsAnonymousRecordDecl() - name := cursor.String() - defer name.Dispose() + var name *ast.Ident + if anony == 0 { + cursorName := cursor.String() + defer cursorName.Dispose() + name = &ast.Ident{Name: c.GoString(cursorName.CStr())} + } fields := ct.ProcessFieldList(cursor) methods := ct.ProcessMethods(cursor) @@ -438,7 +443,7 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast decl := &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Tag: tag, - Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Name: name, Fields: fields, Methods: methods, } @@ -447,15 +452,15 @@ func (ct *Converter) ProcessStructOrClass(cursor clang.Cursor, tag ast.Tag) *ast } func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessStructOrClass(cursor, ast.Struct) + return ct.ProcessRecord(cursor, ast.Struct) } func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessStructOrClass(cursor, ast.Union) + return ct.ProcessRecord(cursor, ast.Union) } func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessStructOrClass(cursor, ast.Class) + return ct.ProcessRecord(cursor, ast.Class) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index ec00ba19..7b5126f7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -85,6 +85,9 @@ void foo();`, func TestStructDecl() { testCases := []string{ + `struct { + int a; + };`, `struct A { int a; int b; @@ -103,6 +106,10 @@ func TestStructDecl() { func TestUnionDecl() { testCases := []string{ + `union { + int a; + int b; + };`, `union A { int a; int b; diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index b97682c7..425ba1a6 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -958,9 +958,7 @@ TestStructDecl Case 1: }, "Parent": null, "Tag": 0, - "Name": { - "Name": "A" - }, + "Name": null, "Fields": { "List": [{ "Type": { @@ -976,20 +974,6 @@ TestStructDecl Case 1: "Names": [{ "Name": "a" }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] }] }, "Methods": [] @@ -1054,6 +1038,60 @@ TestStructDecl Case 2: } TestStructDecl Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 0, + "Name": { + "Name": "A" + }, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 4: { "temp.h": { "path": "temp.h", @@ -1318,6 +1356,58 @@ TestClassDecl Case 2: } TestUnionDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 1, + "Name": null, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + }], + "includes": [], + "macros": [] + } +} + +TestUnionDecl Case 2: { "temp.h": { "path": "temp.h", diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index c43951da..ce20623b 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -131,6 +131,9 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } root.SetItem(c.Str("Names"), names) case *ast.Ident: + if d == nil { + return cjson.Null() + } root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) case *ast.EnumItem: root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) @@ -147,6 +150,9 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) case *ast.Comment: + if d == nil { + return cjson.Null() + } root.SetItem(c.Str("Text"), cjson.String(c.AllocaCStr(d.Text))) case *ast.CommentGroup: if d == nil { From e09c5fcb3c019d258a14e6f59d37f481b88fd546 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 21 Aug 2024 18:25:51 +0800 Subject: [PATCH 28/67] llcppsigfetch:TypedefDecl & Elaborated Type Refer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 25 ++- .../parse/cvt_test/decl_test/decl.go | 18 ++ .../parse/cvt_test/decl_test/llgo.expect | 192 ++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 4 + 4 files changed, 237 insertions(+), 2 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index ee3a2f1b..bc9ace67 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -213,6 +213,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi curFile.Decls = append(curFile.Decls, unionDecl) case clang.CursorFunctionDecl: curFile.Decls = append(curFile.Decls, ct.ProcessFunc(cursor)) + case clang.CursorTypedefDecl: + curFile.Decls = append(curFile.Decls, ct.ProcessTypeDef(cursor)) case clang.CursorNamespace: ct.PushScope(cursor) clang.VisitChildren(cursor, visit, c.Pointer(ct)) @@ -240,8 +242,6 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { // function type will only collect return type, params will be collected in ProcessFunc ret := ct.ProcessType(t.ResultType()) expr = &ast.FuncType{Ret: ret} - case clang.TypeTypedef: - expr = ct.ProcessType(t.CanonicalType()) case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: if t.Kind == clang.TypeConstantArray { len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) @@ -261,6 +261,27 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { return expr } +func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { + name := cursor.String() + defer name.Dispose() + return &ast.TypedefDecl{ + DeclBase: ct.CreateDeclBase(cursor), + Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Type: ct.ProcessUnderLyingType(cursor), + } +} + +func (ct *Converter) ProcessUnderLyingType(cursor clang.Cursor) ast.Expr { + underlying := cursor.TypedefDeclUnderlyingType() + // enum,union,class,struct,typedef -> elaborated type + if underlying.Kind == clang.TypeElaborated { + return &ast.Ident{ + Name: c.GoString(underlying.String().CStr()), + } + } + return ct.ProcessType(underlying) +} + func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 7b5126f7..577afd85 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -12,6 +12,7 @@ func main() { TestClassDecl() TestUnionDecl() TestEnumDecl() + TestTypeDefDecl() } func TestFuncDecl() { @@ -153,3 +154,20 @@ func TestEnumDecl() { } test.RunTest("TestEnumDecl", testCases) } + +func TestTypeDefDecl() { + testCases := []string{ + `typedef int INT;`, + `typedef int INT; + typedef INT STANDARD_INT;`, + `struct StructFoo {}; + union UnionFoo {}; + class ClassFoo {}; + enum EnumFoo {}; + typedef StructFoo STRUCT_FOO; + typedef UnionFoo UNION_FOO; + typedef ClassFoo CLASS_FOO; + typedef EnumFoo ENUM_FOO;`, + } + test.RunTest("TestTypeDefDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 425ba1a6..cfc8fc73 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -1599,6 +1599,198 @@ TestEnumDecl Case 3: } } +TestTypeDefDecl Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "INT" + }, + "Type": { + "Kind": 6, + "Flags": 0 + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "INT" + }, + "Type": { + "Kind": 6, + "Flags": 0 + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "STANDARD_INT" + }, + "Type": { + "Name": "INT" + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 0, + "Name": { + "Name": "StructFoo" + }, + "Fields": { + "List": [] + }, + "Methods": [] + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Tag": 1, + "Name": { + "Name": "UnionFoo" + }, + "Fields": { + "List": [] + }, + "Methods": [] + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "ClassFoo" + }, + "Tag": 3, + "Name": { + "Name": "ClassFoo" + }, + "Fields": { + "List": [] + }, + "Methods": [] + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "EnumFoo" + }, + "Items": [] + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "STRUCT_FOO" + }, + "Type": { + "Name": "StructFoo" + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "UNION_FOO" + }, + "Type": { + "Name": "UnionFoo" + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "CLASS_FOO" + }, + "Type": { + "Name": "ClassFoo" + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "ENUM_FOO" + }, + "Type": { + "Name": "EnumFoo" + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index ce20623b..2e44514f 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -74,6 +74,10 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { items.AddItem(MarshalASTExpr(i)) } root.SetItem(c.Str("Items"), items) + case *ast.TypedefDecl: + MarshalASTDeclBase(d.DeclBase, root) + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) case *ast.FuncDecl: MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) From d4fa379f1150591adefbe7a479995b273716b73d Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 09:51:14 +0800 Subject: [PATCH 29/67] llcppsigfetch:record type --- chore/_xtool/llcppsigfetch/parse/cvt.go | 8 +- .../parse/cvt_test/decl_test/llgo.expect | 844 +++++++++--------- chore/_xtool/llcppsigfetch/parse/dump.go | 16 +- 3 files changed, 449 insertions(+), 419 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index bc9ace67..e6f3c6a8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -463,10 +463,12 @@ func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDe decl := &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), - Tag: tag, Name: name, - Fields: fields, - Methods: methods, + Type: &ast.RecordType{ + Tag: tag, + Fields: fields, + Methods: methods, + }, } return decl diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index cfc8fc73..cc9d4027 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -553,36 +553,38 @@ TestScope Case 4: "Parent": { "Name": "a" }, - "Tag": 3, "Name": { "Name": "a" }, - "Fields": { - "List": [] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "a" - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { "List": [] }, - "Ret": { - "Kind": 0, - "Flags": 0 + "Parent": { + "Name": "a" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } } - } - }] + }] + } }], "includes": [], "macros": [] @@ -608,41 +610,43 @@ TestScope Case 5: "Name": "a" } }, - "Tag": 3, "Name": { "Name": "b" }, - "Fields": { - "List": [] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "X": { - "Name": "b" + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" }, - "Parent": { - "Name": "a" - } - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { + "Doc": { "List": [] }, - "Ret": { - "Kind": 0, - "Flags": 0 + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } } - } - }] + }] + } }], "includes": [], "macros": [] @@ -957,26 +961,28 @@ TestStructDecl Case 1: "List": [] }, "Parent": null, - "Tag": 0, "Name": null, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }] - }, - "Methods": [] + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }] + }, + "Methods": [] + } }], "includes": [], "macros": [] @@ -995,42 +1001,44 @@ TestStructDecl Case 2: "List": [] }, "Parent": null, - "Tag": 0, "Name": { "Name": "A" }, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } }], "includes": [], "macros": [] @@ -1049,42 +1057,44 @@ TestStructDecl Case 3: "List": [] }, "Parent": null, - "Tag": 0, "Name": { "Name": "A" }, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } }], "includes": [], "macros": [] @@ -1103,90 +1113,92 @@ TestStructDecl Case 4: "List": [] }, "Parent": null, - "Tag": 0, "Name": { "Name": "A" }, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" }, - "Ret": { - "Kind": 8, - "Flags": 0 + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } } - } - }] + }] + } }], "includes": [], "macros": [] @@ -1207,42 +1219,44 @@ TestClassDecl Case 1: "Parent": { "Name": "A" }, - "Tag": 3, "Name": { "Name": "A" }, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] + "Type": { + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } }], "includes": [], "macros": [] @@ -1263,92 +1277,94 @@ TestClassDecl Case 2: "Parent": { "Name": "A" }, - "Tag": 3, "Name": { "Name": "A" }, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "A" - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] + "Type": { + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" }, - "Ret": { - "Kind": 8, - "Flags": 0 + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } } - } - }] + }] + } }], "includes": [], "macros": [] @@ -1367,40 +1383,42 @@ TestUnionDecl Case 1: "List": [] }, "Parent": null, - "Tag": 1, "Name": null, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] + "Type": { + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } }], "includes": [], "macros": [] @@ -1419,42 +1437,44 @@ TestUnionDecl Case 2: "List": [] }, "Parent": null, - "Tag": 1, "Name": { "Name": "A" }, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] + "Type": { + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } }], "includes": [], "macros": [] @@ -1675,14 +1695,16 @@ TestTypeDefDecl Case 3: "List": [] }, "Parent": null, - "Tag": 0, "Name": { "Name": "StructFoo" }, - "Fields": { - "List": [] - }, - "Methods": [] + "Type": { + "Tag": 0, + "Fields": { + "List": [] + }, + "Methods": [] + } }, { "Loc": { "File": "temp.h" @@ -1691,14 +1713,16 @@ TestTypeDefDecl Case 3: "List": [] }, "Parent": null, - "Tag": 1, "Name": { "Name": "UnionFoo" }, - "Fields": { - "List": [] - }, - "Methods": [] + "Type": { + "Tag": 1, + "Fields": { + "List": [] + }, + "Methods": [] + } }, { "Loc": { "File": "temp.h" @@ -1709,14 +1733,16 @@ TestTypeDefDecl Case 3: "Parent": { "Name": "ClassFoo" }, - "Tag": 3, "Name": { "Name": "ClassFoo" }, - "Fields": { - "List": [] - }, - "Methods": [] + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [] + } }, { "Loc": { "File": "temp.h" diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 2e44514f..ce5b8240 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -84,14 +84,8 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) case *ast.TypeDecl: MarshalASTDeclBase(d.DeclBase, root) - root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) - root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) - methods := cjson.Array() - for _, m := range d.Methods { - methods.AddItem(MarshalASTDecl(m)) - } - root.SetItem(c.Str("Methods"), methods) + root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) } return root } @@ -113,6 +107,14 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root := cjson.Object() switch d := t.(type) { + case *ast.RecordType: + root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) + root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) + methods := cjson.Array() + for _, m := range d.Methods { + methods.AddItem(MarshalASTDecl(m)) + } + root.SetItem(c.Str("Methods"), methods) case *ast.FuncType: root.SetItem(c.Str("Params"), MarshalASTExpr(d.Params)) root.SetItem(c.Str("Ret"), MarshalASTExpr(d.Ret)) From 1557a76225a556a07970311a06b5d9734e36ae8e Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 11:28:19 +0800 Subject: [PATCH 30/67] llcppsigfetch:split test directory --- chore/_xtool/llcppsigfetch/parse/cvt.go | 1 - .../cvt_test/decl_test/class_test/class.go | 22 + .../cvt_test/decl_test/class_test/llgo.expect | 169 ++ .../decl_test/comment_test/comment.go | 37 + .../decl_test/comment_test/llgo.expect | 292 +++ .../parse/cvt_test/decl_test/decl.go | 173 -- .../cvt_test/decl_test/enum_test/enum.go | 28 + .../cvt_test/decl_test/enum_test/llgo.expect | 140 ++ .../cvt_test/decl_test/func_test/func.go | 23 + .../cvt_test/decl_test/func_test/llgo.expect | 438 ++++ .../parse/cvt_test/decl_test/llgo.expect | 1823 ----------------- .../cvt_test/decl_test/scope_test/llgo.expect | 213 ++ .../cvt_test/decl_test/scope_test/scope.go | 30 + .../decl_test/struct_test/llgo.expect | 257 +++ .../cvt_test/decl_test/struct_test/struct.go | 28 + .../decl_test/typedef_test/llgo.expect | 200 ++ .../decl_test/typedef_test/typedef.go | 24 + .../cvt_test/decl_test/union_test/llgo.expect | 113 + .../cvt_test/decl_test/union_test/union.go | 21 + .../cvt_test/preprocess_test/llgo.expect | 4 - chore/_xtool/llcppsigfetch/parse/dump.go | 5 +- chore/llcppg/ast/ast.go | 1 - 22 files changed, 2037 insertions(+), 2005 deletions(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect delete mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/enum.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect delete mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect create mode 100644 chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index e6f3c6a8..c92afa49 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -144,7 +144,6 @@ func (ct *Converter) GetCurFile() *ast.File { file, ok := ct.Files[ct.curLoc.File] if !ok { file = &ast.File{ - Path: ct.curLoc.File, Decls: make([]ast.Decl, 0), Includes: make([]*ast.Include, 0), Macros: make([]*ast.Macro, 0), diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go new file mode 100644 index 00000000..747688f9 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -0,0 +1,22 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestClassDecl() +} + +func TestClassDecl() { + testCases := []string{ + `class A { + int a; + int b; + };`, + `class A { + int a; + int b; + float foo(int a,double b); + };`, + } + test.RunTest("TestClassDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect new file mode 100644 index 00000000..b8542bd2 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -0,0 +1,169 @@ +#stdout +TestClassDecl Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + +TestClassDecl Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } + } + }] + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go new file mode 100644 index 00000000..12010b72 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go @@ -0,0 +1,37 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestComment() +} + +func TestComment() { + testCases := []string{ + `// not read comment 1 + void foo();`, + `/* not read comment 2 */ + void foo();`, + `/// comment + void foo();`, + `/** comment */ + void foo();`, + `/*! comment */ + void foo();`, + `/// comment 1 +/// comment 2 +void foo();`, + `/*! comment 1 */ +/*! comment 2 */ +void foo();`, + `/** comment 1 */ +/** comment 1 */ +void foo();`, + `/** + * comment 1 + * comment 2 + */ +void foo();`, + } + test.RunTest("TestComment", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect new file mode 100644 index 00000000..6ec789be --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -0,0 +1,292 @@ +#stdout +TestComment Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/// comment" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/** comment */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 5: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/*! comment */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 6: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/// comment 1" + }, { + "Text": "/// comment 2" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 7: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/*! comment 1 */" + }, { + "Text": "/*! comment 2 */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 8: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/** comment 1 */" + }, { + "Text": "/** comment 1 */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 9: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/**" + }, { + "Text": " * comment 1" + }, { + "Text": " * comment 2" + }, { + "Text": " */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go deleted file mode 100644 index 577afd85..00000000 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ /dev/null @@ -1,173 +0,0 @@ -package main - -import ( - test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" -) - -func main() { - TestFuncDecl() - TestScope() - TestComment() - TestStructDecl() - TestClassDecl() - TestUnionDecl() - TestEnumDecl() - TestTypeDefDecl() -} - -func TestFuncDecl() { - testCases := []string{ - `void foo();`, - `void foo(int a);`, - `float foo(int a,double b);`, - - `void foo(char* str, double x);`, - `float* foo(char* str, double x);`, - `float* foo(char*** str, double x);`, - - `float* foo(char str[], double x);`, - `float* foo(int arr[3][4]);`, - } - test.RunTest("TestFuncDecl", testCases) -} - -func TestScope() { - testCases := []string{ - `void foo();`, - `namespace a { - void foo(); - }`, - `namespace a { - namespace b { - void foo(); - } - }`, - `class a { - void foo(); - };`, - `namespace a { - class b { - void foo(); - }; - }`, - } - test.RunTest("TestScope", testCases) -} - -func TestComment() { - testCases := []string{ - `// not read comment 1 - void foo();`, - `/* not read comment 2 */ - void foo();`, - `/// comment - void foo();`, - `/** comment */ - void foo();`, - `/*! comment */ - void foo();`, - `/// comment 1 -/// comment 2 -void foo();`, - `/*! comment 1 */ -/*! comment 2 */ -void foo();`, - `/** comment 1 */ -/** comment 1 */ -void foo();`, - `/** - * comment 1 - * comment 2 - */ -void foo();`, - } - test.RunTest("TestComment", testCases) -} - -func TestStructDecl() { - testCases := []string{ - `struct { - int a; - };`, - `struct A { - int a; - int b; - };`, - `struct A { - int a, b; - };`, - `struct A { - int a; - int b; - float foo(int a,double b);; - };`, - } - test.RunTest("TestStructDecl", testCases) -} - -func TestUnionDecl() { - testCases := []string{ - `union { - int a; - int b; - };`, - `union A { - int a; - int b; - };`, - } - test.RunTest("TestUnionDecl", testCases) -} - -func TestClassDecl() { - testCases := []string{ - `class A { - int a; - int b; - };`, - `class A { - int a; - int b; - float foo(int a,double b);; - };`, - } - test.RunTest("TestClassDecl", testCases) -} - -func TestEnumDecl() { - testCases := []string{ - `enum Foo { - a, - b, - c, - };`, - `enum Foo { - a = 1, - b = 2, - c = 4, - };`, - `enum Foo { - a = 1, - b, - c, - };`, - } - test.RunTest("TestEnumDecl", testCases) -} - -func TestTypeDefDecl() { - testCases := []string{ - `typedef int INT;`, - `typedef int INT; - typedef INT STANDARD_INT;`, - `struct StructFoo {}; - union UnionFoo {}; - class ClassFoo {}; - enum EnumFoo {}; - typedef StructFoo STRUCT_FOO; - typedef UnionFoo UNION_FOO; - typedef ClassFoo CLASS_FOO; - typedef EnumFoo ENUM_FOO;`, - } - test.RunTest("TestTypeDefDecl", testCases) -} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/enum.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/enum.go new file mode 100644 index 00000000..8ed4cba7 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/enum.go @@ -0,0 +1,28 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestEnumDecl() +} + +func TestEnumDecl() { + testCases := []string{ + `enum Foo { + a, + b, + c, + };`, + `enum Foo { + a = 1, + b = 2, + c = 4, + };`, + `enum Foo { + a = 1, + b, + c, + };`, + } + test.RunTest("TestEnumDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect new file mode 100644 index 00000000..c4965551 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect @@ -0,0 +1,140 @@ +#stdout +TestEnumDecl Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "0" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }] + }], + "includes": [], + "macros": [] + } +} + +TestEnumDecl Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "4" + } + }] + }], + "includes": [], + "macros": [] + } +} + +TestEnumDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "3" + } + }] + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go new file mode 100644 index 00000000..4bebe5aa --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go @@ -0,0 +1,23 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestFuncDecl() +} + +func TestFuncDecl() { + testCases := []string{ + `void foo();`, + `void foo(int a);`, + `float foo(int a,double b);`, + + `void foo(char* str, double x);`, + `float* foo(char* str, double x);`, + `float* foo(char*** str, double x);`, + + `float* foo(char str[], double x);`, + `float* foo(int arr[3][4]);`, + } + test.RunTest("TestFuncDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect new file mode 100644 index 00000000..98a83ac2 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -0,0 +1,438 @@ +#stdout +TestFuncDecl Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "Kind": 2, + "Flags": 1 + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 5: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "Kind": 2, + "Flags": 1 + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 6: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "X": { + "X": { + "Kind": 2, + "Flags": 1 + } + } + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 7: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Elt": { + "Kind": 2, + "Flags": 1 + }, + "Len": null + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 8: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Elt": { + "Elt": { + "Kind": 6, + "Flags": 0 + }, + "Len": { + "Kind": 0, + "Value": "4" + } + }, + "Len": { + "Kind": 0, + "Value": "3" + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "arr" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect deleted file mode 100644 index cc9d4027..00000000 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ /dev/null @@ -1,1823 +0,0 @@ -#stdout -TestFuncDecl Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 3: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Ret": { - "Kind": 8, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 4: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "Kind": 2, - "Flags": 1 - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 5: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "Kind": 2, - "Flags": 1 - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] - }] - }, - "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 6: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "X": { - "X": { - "Kind": 2, - "Flags": 1 - } - } - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] - }] - }, - "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 7: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Elt": { - "Kind": 2, - "Flags": 1 - }, - "Len": null - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] - }] - }, - "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 8: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Elt": { - "Elt": { - "Kind": 6, - "Flags": 0 - }, - "Len": { - "Kind": 0, - "Value": "4" - } - }, - "Len": { - "Kind": 0, - "Value": "3" - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "arr" - }] - }] - }, - "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } - } - } - }], - "includes": [], - "macros": [] - } -} - -TestScope Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestScope Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "a" - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestScope Case 3: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestScope Case 4: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "a" - }, - "Name": { - "Name": "a" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "a" - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }] - } - }], - "includes": [], - "macros": [] - } -} - -TestScope Case 5: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } - }, - "Name": { - "Name": "b" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }] - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 3: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/// comment" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 4: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/** comment */" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 5: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/*! comment */" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 6: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/// comment 1" - }, { - "Text": "/// comment 2" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 7: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/*! comment 1 */" - }, { - "Text": "/*! comment 2 */" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 8: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/** comment 1 */" - }, { - "Text": "/** comment 1 */" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestComment Case 9: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [{ - "Text": "/**" - }, { - "Text": " * comment 1" - }, { - "Text": " * comment 2" - }, { - "Text": " */" - }] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestStructDecl Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": null, - "Type": { - "Tag": 0, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }] - }, - "Methods": [] - } - }], - "includes": [], - "macros": [] - } -} - -TestStructDecl Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] - } - }], - "includes": [], - "macros": [] - } -} - -TestStructDecl Case 3: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] - } - }], - "includes": [], - "macros": [] - } -} - -TestStructDecl Case 4: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Ret": { - "Kind": 8, - "Flags": 0 - } - } - }] - } - }], - "includes": [], - "macros": [] - } -} - -TestClassDecl Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "A" - }, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] - } - }], - "includes": [], - "macros": [] - } -} - -TestClassDecl Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "A" - }, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "A" - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Ret": { - "Kind": 8, - "Flags": 0 - } - } - }] - } - }], - "includes": [], - "macros": [] - } -} - -TestUnionDecl Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": null, - "Type": { - "Tag": 1, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] - } - }], - "includes": [], - "macros": [] - } -} - -TestUnionDecl Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 1, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [] - } - }], - "includes": [], - "macros": [] - } -} - -TestEnumDecl Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "Foo" - }, - "Items": [{ - "Name": { - "Name": "a" - }, - "Value": { - "Kind": 0, - "Value": "0" - } - }, { - "Name": { - "Name": "b" - }, - "Value": { - "Kind": 0, - "Value": "1" - } - }, { - "Name": { - "Name": "c" - }, - "Value": { - "Kind": 0, - "Value": "2" - } - }] - }], - "includes": [], - "macros": [] - } -} - -TestEnumDecl Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "Foo" - }, - "Items": [{ - "Name": { - "Name": "a" - }, - "Value": { - "Kind": 0, - "Value": "1" - } - }, { - "Name": { - "Name": "b" - }, - "Value": { - "Kind": 0, - "Value": "2" - } - }, { - "Name": { - "Name": "c" - }, - "Value": { - "Kind": 0, - "Value": "4" - } - }] - }], - "includes": [], - "macros": [] - } -} - -TestEnumDecl Case 3: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "Foo" - }, - "Items": [{ - "Name": { - "Name": "a" - }, - "Value": { - "Kind": 0, - "Value": "1" - } - }, { - "Name": { - "Name": "b" - }, - "Value": { - "Kind": 0, - "Value": "2" - } - }, { - "Name": { - "Name": "c" - }, - "Value": { - "Kind": 0, - "Value": "3" - } - }] - }], - "includes": [], - "macros": [] - } -} - -TestTypeDefDecl Case 1: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "INT" - }, - "Type": { - "Kind": 6, - "Flags": 0 - } - }], - "includes": [], - "macros": [] - } -} - -TestTypeDefDecl Case 2: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "INT" - }, - "Type": { - "Kind": 6, - "Flags": 0 - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "STANDARD_INT" - }, - "Type": { - "Name": "INT" - } - }], - "includes": [], - "macros": [] - } -} - -TestTypeDefDecl Case 3: -{ - "temp.h": { - "path": "temp.h", - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "StructFoo" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "UnionFoo" - }, - "Type": { - "Tag": 1, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "Name": "ClassFoo" - }, - "Name": { - "Name": "ClassFoo" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "EnumFoo" - }, - "Items": [] - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "STRUCT_FOO" - }, - "Type": { - "Name": "StructFoo" - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "UNION_FOO" - }, - "Type": { - "Name": "UnionFoo" - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "CLASS_FOO" - }, - "Type": { - "Name": "ClassFoo" - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "ENUM_FOO" - }, - "Type": { - "Name": "EnumFoo" - } - }], - "includes": [], - "macros": [] - } -} - - -#stderr - -#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect new file mode 100644 index 00000000..fd8ce82c --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -0,0 +1,213 @@ +#stdout +TestScope Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "a" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "a" + }, + "Name": { + "Name": "a" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "a" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }] + } + }], + "includes": [], + "macros": [] + } +} + +TestScope Case 5: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Name": { + "Name": "b" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }] + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go new file mode 100644 index 00000000..4c6985ce --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go @@ -0,0 +1,30 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestScope() +} + +func TestScope() { + testCases := []string{ + `void foo();`, + `namespace a { + void foo(); + }`, + `namespace a { + namespace b { + void foo(); + } + }`, + `class a { + void foo(); + };`, + `namespace a { + class b { + void foo(); + }; + }`, + } + test.RunTest("TestScope", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect new file mode 100644 index 00000000..16b90baf --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -0,0 +1,257 @@ +#stdout +TestStructDecl Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": null, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + +TestStructDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 8, + "Flags": 0 + } + } + }] + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go new file mode 100644 index 00000000..fd14b720 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go @@ -0,0 +1,28 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestStructDecl() +} + +func TestStructDecl() { + testCases := []string{ + `struct { + int a; + };`, + `struct A { + int a; + int b; + };`, + `struct A { + int a, b; + };`, + `struct A { + int a; + int b; + float foo(int a,double b);; + };`, + } + test.RunTest("TestStructDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect new file mode 100644 index 00000000..4eed90e6 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -0,0 +1,200 @@ +#stdout +TestTypeDefDecl Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "INT" + }, + "Type": { + "Kind": 6, + "Flags": 0 + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "INT" + }, + "Type": { + "Kind": 6, + "Flags": 0 + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "STANDARD_INT" + }, + "Type": { + "Name": "INT" + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "StructFoo" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "UnionFoo" + }, + "Type": { + "Tag": 1, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "ClassFoo" + }, + "Name": { + "Name": "ClassFoo" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "EnumFoo" + }, + "Items": [] + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "STRUCT_FOO" + }, + "Type": { + "Name": "StructFoo" + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "UNION_FOO" + }, + "Type": { + "Name": "UnionFoo" + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "CLASS_FOO" + }, + "Type": { + "Name": "ClassFoo" + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "ENUM_FOO" + }, + "Type": { + "Name": "EnumFoo" + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go new file mode 100644 index 00000000..81daceca --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -0,0 +1,24 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestTypeDefDecl() +} + +func TestTypeDefDecl() { + testCases := []string{ + `typedef int INT;`, + `typedef int INT; + typedef INT STANDARD_INT;`, + `struct StructFoo {}; + union UnionFoo {}; + class ClassFoo {}; + enum EnumFoo {}; + typedef StructFoo STRUCT_FOO; + typedef UnionFoo UNION_FOO; + typedef ClassFoo CLASS_FOO; + typedef EnumFoo ENUM_FOO;`, + } + test.RunTest("TestTypeDefDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect new file mode 100644 index 00000000..7c0943df --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect @@ -0,0 +1,113 @@ +#stdout +TestUnionDecl Case 1: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": null, + "Type": { + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + +TestUnionDecl Case 2: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + + +#stderr + +#exit 0 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go new file mode 100644 index 00000000..1f5f7942 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go @@ -0,0 +1,21 @@ +package main + +import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" + +func main() { + TestUnionDecl() +} + +func TestUnionDecl() { + testCases := []string{ + `union { + int a; + int b; + };`, + `union A { + int a; + int b; + };`, + } + test.RunTest("TestUnionDecl", testCases) +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect index 3332d313..9e9f5b3d 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect @@ -2,7 +2,6 @@ TestDefine Case 1: { "temp.h": { - "path": "temp.h", "decls": [], "includes": [], "macros": [{ @@ -18,7 +17,6 @@ TestDefine Case 1: TestDefine Case 2: { "temp.h": { - "path": "temp.h", "decls": [], "includes": [], "macros": [{ @@ -37,7 +35,6 @@ TestDefine Case 2: TestDefine Case 3: { "temp.h": { - "path": "temp.h", "decls": [], "includes": [], "macros": [{ @@ -89,7 +86,6 @@ TestDefine Case 3: TestInclude Case 1: { "temp.h": { - "path": "temp.h", "decls": [], "includes": [{ "Path": "foo.h" diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index ce5b8240..2f2e71dd 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -8,8 +8,8 @@ import ( func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON { root := cjson.Object() - for _, file := range files { - root.SetItem(c.AllocaCStr(file.Path), MarshalASTFile(file)) + for path, file := range files { + root.SetItem(c.AllocaCStr(path), MarshalASTFile(file)) } return root } @@ -22,7 +22,6 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { decls.AddItem(MarshalASTDecl(decl)) } - root.SetItem(c.Str("path"), cjson.String(c.AllocaCStr(file.Path))) root.SetItem(c.Str("decls"), decls) // json:includes,omitempty diff --git a/chore/llcppg/ast/ast.go b/chore/llcppg/ast/ast.go index 6f924a4a..46c16dc3 100644 --- a/chore/llcppg/ast/ast.go +++ b/chore/llcppg/ast/ast.go @@ -370,7 +370,6 @@ func (*Macro) ppdNode() {} // ------------------------------------------------ type File struct { - Path string `json:"path"` Decls []Decl `json:"decls"` Includes []*Include `json:"includes,omitempty"` Macros []*Macro `json:"macros,omitempty"` From 0a8e25b4054fc9c644b4f17f47f966b658106488 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 12:11:53 +0800 Subject: [PATCH 31/67] llcppsigfetch:simpilfy func test --- .../cvt_test/decl_test/func_test/func.go | 10 +-- .../cvt_test/decl_test/func_test/llgo.expect | 78 ++++--------------- 2 files changed, 20 insertions(+), 68 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go index 4bebe5aa..646427ce 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go @@ -11,13 +11,13 @@ func TestFuncDecl() { `void foo();`, `void foo(int a);`, `float foo(int a,double b);`, + `float* foo(int a,double b);`, - `void foo(char* str, double x);`, - `float* foo(char* str, double x);`, - `float* foo(char*** str, double x);`, + `void foo(char* str);`, + `void* foo(char*** str);`, - `float* foo(char str[], double x);`, - `float* foo(int arr[3][4]);`, + `void foo(char str[]);`, + `void foo(int arr[3][4]);`, } test.RunTest("TestFuncDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 98a83ac2..900c5d07 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -146,10 +146,8 @@ TestFuncDecl Case 4: "Params": { "List": [{ "Type": { - "X": { - "Kind": 2, - "Flags": 1 - } + "Kind": 6, + "Flags": 0 }, "Doc": { "List": [] @@ -158,7 +156,7 @@ TestFuncDecl Case 4: "List": [] }, "Names": [{ - "Name": "str" + "Name": "a" }] }, { "Type": { @@ -172,13 +170,15 @@ TestFuncDecl Case 4: "List": [] }, "Names": [{ - "Name": "x" + "Name": "b" }] }] }, "Ret": { - "Kind": 0, - "Flags": 0 + "X": { + "Kind": 8, + "Flags": 0 + } } } }], @@ -219,27 +219,11 @@ TestFuncDecl Case 5: "Names": [{ "Name": "str" }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] }] }, "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } + "Kind": 0, + "Flags": 0 } } }], @@ -284,25 +268,11 @@ TestFuncDecl Case 6: "Names": [{ "Name": "str" }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] }] }, "Ret": { "X": { - "Kind": 8, + "Kind": 0, "Flags": 0 } } @@ -346,27 +316,11 @@ TestFuncDecl Case 7: "Names": [{ "Name": "str" }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "x" - }] }] }, "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } + "Kind": 0, + "Flags": 0 } } }], @@ -420,10 +374,8 @@ TestFuncDecl Case 8: }] }, "Ret": { - "X": { - "Kind": 8, - "Flags": 0 - } + "Kind": 0, + "Flags": 0 } } }], From a4f850c0c6599c0305d89aae2daf977209902757 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 12:15:06 +0800 Subject: [PATCH 32/67] llcppsigfetch:lvalue & rvalue refer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 2 + .../cvt_test/decl_test/func_test/func.go | 3 + .../cvt_test/decl_test/func_test/llgo.expect | 90 +++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 4 + 4 files changed, 99 insertions(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index c92afa49..e5d34eb2 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -237,6 +237,8 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { switch t.Kind { case clang.TypePointer: expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} + case clang.TypeLValueReference, clang.TypeRValueReference: + expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeFunctionProto: // function type will only collect return type, params will be collected in ProcessFunc ret := ct.ProcessType(t.ResultType()) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go index 646427ce..9a555bbc 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go @@ -18,6 +18,9 @@ func TestFuncDecl() { `void foo(char str[]);`, `void foo(int arr[3][4]);`, + + `void foo(int& a);`, + `void foo(int&& a);`, } test.RunTest("TestFuncDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 900c5d07..1769dc0f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -384,6 +384,96 @@ TestFuncDecl Case 8: } } +TestFuncDecl Case 9: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "Kind": 6, + "Flags": 0 + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 10: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "X": { + "Kind": 6, + "Flags": 0 + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 2f2e71dd..0f8024d0 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -146,6 +146,10 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { case *ast.BasicLit: root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) + case *ast.LvalueRefType: + root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) + case *ast.RvalueRefType: + root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.PointerType: root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.ArrayType: From eb4d7211752df63933e343ece2b6a05222718b9a Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 15:57:21 +0800 Subject: [PATCH 33/67] llcppsigfetch:tag expr --- chore/_xtool/llcppsigfetch/parse/cvt.go | 51 ++++-- .../decl_test/typedef_test/llgo.expect | 145 ++++++++++++++++++ .../decl_test/typedef_test/typedef.go | 10 ++ chore/_xtool/llcppsigfetch/parse/dump.go | 3 + 4 files changed, 196 insertions(+), 13 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index e5d34eb2..a10d4a50 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -230,10 +230,16 @@ func (ct *Converter) Convert() (map[string]*ast.File, error) { } func (ct *Converter) ProcessType(t clang.Type) ast.Expr { - var expr ast.Expr + if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { return ct.ProcessBuiltinType(t) } + + if t.Kind == clang.TypeElaborated { + return ct.ProcessElaboratedType(t) + } + + var expr ast.Expr switch t.Kind { case clang.TypePointer: expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} @@ -268,21 +274,10 @@ func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { return &ast.TypedefDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Type: ct.ProcessUnderLyingType(cursor), + Type: ct.ProcessType(cursor.TypedefDeclUnderlyingType()), } } -func (ct *Converter) ProcessUnderLyingType(cursor clang.Cursor) ast.Expr { - underlying := cursor.TypedefDeclUnderlyingType() - // enum,union,class,struct,typedef -> elaborated type - if underlying.Kind == clang.TypeElaborated { - return &ast.Ident{ - Name: c.GoString(underlying.String().CStr()), - } - } - return ct.ProcessType(underlying) -} - func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() @@ -487,6 +482,36 @@ func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { return ct.ProcessRecord(cursor, ast.Class) } +func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { + name := t.String() + defer name.Dispose() + + typeName := c.GoString(name.CStr()) + + tagMap := map[string]ast.Tag{ + "struct": ast.Struct, + "union": ast.Union, + "enum": ast.Enum, + "class": ast.Class, + } + + // for elaborated type, it could have a tag description + // like struct A, union B, class C, enum D + parts := strings.SplitN(typeName, " ", 2) + if len(parts) == 2 { + if tagValue, ok := tagMap[parts[0]]; ok { + return &ast.TagExpr{ + Tag: tagValue, + Name: &ast.Ident{Name: parts[1]}, + } + } + } + + return &ast.Ident{ + Name: typeName, + } +} + func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { kind := ast.Void var flags ast.TypeFlag diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 4eed90e6..fbaed500 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -194,6 +194,151 @@ TestTypeDefDecl Case 3: } } +TestTypeDefDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "StructFoo" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "UnionFoo" + }, + "Type": { + "Tag": 1, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "ClassFoo" + }, + "Name": { + "Name": "ClassFoo" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "EnumFoo" + }, + "Items": [] + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "STRUCT_FOO" + }, + "Type": { + "Name": { + "Name": "StructFoo" + }, + "Tag": 0 + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "UNION_FOO" + }, + "Type": { + "Name": { + "Name": "UnionFoo" + }, + "Tag": 1 + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "CLASS_FOO" + }, + "Type": { + "Name": { + "Name": "ClassFoo" + }, + "Tag": 3 + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "ENUM_FOO" + }, + "Type": { + "Name": { + "Name": "EnumFoo" + }, + "Tag": 2 + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 81daceca..9c90c6c7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -10,6 +10,7 @@ func TestTypeDefDecl() { testCases := []string{ `typedef int INT;`, `typedef int INT; + typedef INT STANDARD_INT;`, `struct StructFoo {}; union UnionFoo {}; @@ -19,6 +20,15 @@ func TestTypeDefDecl() { typedef UnionFoo UNION_FOO; typedef ClassFoo CLASS_FOO; typedef EnumFoo ENUM_FOO;`, + + `struct StructFoo {}; + union UnionFoo {}; + class ClassFoo {}; + enum EnumFoo {}; + typedef struct StructFoo STRUCT_FOO; + typedef union UnionFoo UNION_FOO; + typedef class ClassFoo CLASS_FOO; + typedef enum EnumFoo ENUM_FOO;`, } test.RunTest("TestTypeDefDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 0f8024d0..1acb8c49 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -140,6 +140,9 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { return cjson.Null() } root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + case *ast.TagExpr: + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) case *ast.EnumItem: root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value)) From 319e746a55f6059247404607a022947f8c9d6287 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 18:07:01 +0800 Subject: [PATCH 34/67] llcppsigfetch:qualified name refer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 14 +++ .../cvt_test/decl_test/scope_test/llgo.expect | 95 +++++++++++++++++++ .../cvt_test/decl_test/scope_test/scope.go | 9 ++ 3 files changed, 118 insertions(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index a10d4a50..003df96b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -507,6 +507,20 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { } } + // qualified name + // todo(zzy): qualified name with tag,like struct A::B + if strings.Contains(typeName, "::") { + scopeParts := strings.Split(typeName, "::") + var expr ast.Expr = &ast.Ident{Name: scopeParts[0]} + for _, part := range scopeParts[1:] { + expr = &ast.ScopingExpr{ + Parent: expr, + X: &ast.Ident{Name: part}, + } + } + return expr + } + return &ast.Ident{ Name: typeName, } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index fd8ce82c..bba9d9b9 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -207,6 +207,101 @@ TestScope Case 5: } } +TestScope Case 6: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "c" + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + } + }, + "Name": { + "Name": "c" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "c" + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + } + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "C" + }, + "Type": { + "X": { + "Name": "c" + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go index 4c6985ce..d039e7c2 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go @@ -25,6 +25,15 @@ func TestScope() { void foo(); }; }`, + + `namespace a { + namespace b { + class c { + void foo(); + }; + } + } + typedef a::b::c C;`, } test.RunTest("TestScope", testCases) } From 14b335a51e9b8c459e811ed92146ace949b46d17 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 18:49:00 +0800 Subject: [PATCH 35/67] llcppsigfetch:correct class scoping --- chore/_xtool/llcppsigfetch/parse/cvt.go | 70 +++++++++++-------- .../cvt_test/decl_test/class_test/llgo.expect | 8 +-- .../cvt_test/decl_test/scope_test/llgo.expect | 20 ++---- .../decl_test/typedef_test/llgo.expect | 8 +-- .../decl_test/typedef_test/typedef.go | 3 +- 5 files changed, 49 insertions(+), 60 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 003df96b..60664aef 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -197,23 +197,21 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi marco := ct.ProcessMarco(cursor) curFile.Macros = append(curFile.Macros, marco) case clang.CursorEnumDecl: - enum := ct.ProcessEnum(cursor) + enum := ct.ProcessEnumDecl(cursor) curFile.Decls = append(curFile.Decls, enum) case clang.CursorClassDecl: - ct.PushScope(cursor) - classDecl := ct.ProcessClass(cursor) + classDecl := ct.ProcessClassDecl(cursor) curFile.Decls = append(curFile.Decls, classDecl) - ct.PopScope() case clang.CursorStructDecl: - structDecl := ct.ProcessStruct(cursor) + structDecl := ct.ProcessStructDecl(cursor) curFile.Decls = append(curFile.Decls, structDecl) case clang.CursorUnionDecl: - unionDecl := ct.ProcessUnion(cursor) + unionDecl := ct.ProcessUnionDecl(cursor) curFile.Decls = append(curFile.Decls, unionDecl) case clang.CursorFunctionDecl: - curFile.Decls = append(curFile.Decls, ct.ProcessFunc(cursor)) + curFile.Decls = append(curFile.Decls, ct.ProcessFuncDecl(cursor)) case clang.CursorTypedefDecl: - curFile.Decls = append(curFile.Decls, ct.ProcessTypeDef(cursor)) + curFile.Decls = append(curFile.Decls, ct.ProcessTypeDefDecl(cursor)) case clang.CursorNamespace: ct.PushScope(cursor) clang.VisitChildren(cursor, visit, c.Pointer(ct)) @@ -246,7 +244,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { case clang.TypeLValueReference, clang.TypeRValueReference: expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeFunctionProto: - // function type will only collect return type, params will be collected in ProcessFunc + // function type will only collect return type, params will be collected in ProcessFuncDecl ret := ct.ProcessType(t.ResultType()) expr = &ast.FuncType{Ret: ret} case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: @@ -268,7 +266,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { return expr } -func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { +func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { name := cursor.String() defer name.Dispose() return &ast.TypedefDecl{ @@ -278,7 +276,7 @@ func (ct *Converter) ProcessTypeDef(cursor clang.Cursor) *ast.TypedefDecl { } } -func (ct *Converter) ProcessFunc(cursor clang.Cursor) *ast.FuncDecl { +func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() // function type will only collect return type @@ -323,7 +321,7 @@ func visitEnum(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chi return clang.ChildVisit_Continue } -func (ct *Converter) ProcessEnum(cursor clang.Cursor) *ast.EnumTypeDecl { +func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { name := cursor.String() defer name.Dispose() items := make([]*ast.EnumItem, 0) @@ -426,7 +424,7 @@ type visitMethodsContext struct { func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitMethodsContext)(clientData) if cursor.Kind == clang.CursorCXXMethod { - method := ctx.converter.ProcessFunc(cursor) + method := ctx.converter.ProcessFuncDecl(cursor) if method != nil { *ctx.methods = append(*ctx.methods, method) } @@ -444,7 +442,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { return methods } -func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { +func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { anony := cursor.IsAnonymousRecordDecl() var name *ast.Ident @@ -454,32 +452,42 @@ func (ct *Converter) ProcessRecord(cursor clang.Cursor, tag ast.Tag) *ast.TypeDe name = &ast.Ident{Name: c.GoString(cursorName.CStr())} } - fields := ct.ProcessFieldList(cursor) - methods := ct.ProcessMethods(cursor) - - decl := &ast.TypeDecl{ + return &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: name, - Type: &ast.RecordType{ - Tag: tag, - Fields: fields, - Methods: methods, - }, + Type: ct.ProcessRecordType(cursor, tag), } - - return decl } -func (ct *Converter) ProcessStruct(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecord(cursor, ast.Struct) +func (ct *Converter) ProcessStructDecl(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessRecordDecl(cursor, ast.Struct) } -func (ct *Converter) ProcessUnion(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecord(cursor, ast.Union) +func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) *ast.TypeDecl { + return ct.ProcessRecordDecl(cursor, ast.Union) } -func (ct *Converter) ProcessClass(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecord(cursor, ast.Class) +func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { + // Pushing class scope before processing its type and popping after + base := ct.CreateDeclBase(cursor) + + ct.PushScope(cursor) + typ := ct.ProcessRecordType(cursor, ast.Class) + ct.PopScope() + + return &ast.TypeDecl{ + DeclBase: base, + Name: &ast.Ident{Name: c.GoString(cursor.String().CStr())}, + Type: typ, + } +} + +func (ct *Converter) ProcessRecordType(cursor clang.Cursor, tag ast.Tag) *ast.RecordType { + return &ast.RecordType{ + Tag: tag, + Fields: ct.ProcessFieldList(cursor), + Methods: ct.ProcessMethods(cursor), + } } func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index b8542bd2..9b66edd8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -9,9 +9,7 @@ TestClassDecl Case 1: "Doc": { "List": [] }, - "Parent": { - "Name": "A" - }, + "Parent": null, "Name": { "Name": "A" }, @@ -66,9 +64,7 @@ TestClassDecl Case 2: "Doc": { "List": [] }, - "Parent": { - "Name": "A" - }, + "Parent": null, "Name": { "Name": "A" }, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index bba9d9b9..58b035dd 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -105,9 +105,7 @@ TestScope Case 4: "Doc": { "List": [] }, - "Parent": { - "Name": "a" - }, + "Parent": null, "Name": { "Name": "a" }, @@ -157,12 +155,7 @@ TestScope Case 5: "List": [] }, "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } + "Name": "a" }, "Name": { "Name": "b" @@ -219,15 +212,10 @@ TestScope Case 6: }, "Parent": { "X": { - "Name": "c" + "Name": "b" }, "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } + "Name": "a" } }, "Name": { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index fbaed500..63084752 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -107,9 +107,7 @@ TestTypeDefDecl Case 3: "Doc": { "List": [] }, - "Parent": { - "Name": "ClassFoo" - }, + "Parent": null, "Name": { "Name": "ClassFoo" }, @@ -240,9 +238,7 @@ TestTypeDefDecl Case 4: "Doc": { "List": [] }, - "Parent": { - "Name": "ClassFoo" - }, + "Parent": null, "Name": { "Name": "ClassFoo" }, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 9c90c6c7..9487e245 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -9,9 +9,10 @@ func main() { func TestTypeDefDecl() { testCases := []string{ `typedef int INT;`, - `typedef int INT; + `typedef int INT; typedef INT STANDARD_INT;`, + `struct StructFoo {}; union UnionFoo {}; class ClassFoo {}; From e57ea9b5013529b22bf885d836bb204c390e0f53 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 22 Aug 2024 21:24:25 +0800 Subject: [PATCH 36/67] llcppsigfetch/chore:rename --- chore/_xtool/llcppsigfetch/parse/cvt.go | 44 +++++++++---------- .../decl_test/comment_test/comment.go | 32 +++++++------- .../decl_test/comment_test/llgo.expect | 40 ++++++++--------- 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 60664aef..aea635a9 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -15,11 +15,11 @@ import ( ) type Converter struct { - Files map[string]*ast.File - curLoc ast.Location - index *clang.Index - unit *clang.TranslationUnit - scopeStack []ast.Expr //namespace & class + Files map[string]*ast.File + curLoc ast.Location + index *clang.Index + unit *clang.TranslationUnit + scopeContext []ast.Expr //namespace & class } type Config struct { @@ -97,26 +97,26 @@ func (ct *Converter) PushScope(cursor clang.Cursor) { defer name.Dispose() ident := &ast.Ident{Name: c.GoString(name.CStr())} - if len(ct.scopeStack) == 0 { - ct.scopeStack = append(ct.scopeStack, ident) + if len(ct.scopeContext) == 0 { + ct.scopeContext = append(ct.scopeContext, ident) } else { - parent := ct.scopeStack[len(ct.scopeStack)-1] + parent := ct.scopeContext[len(ct.scopeContext)-1] newContext := &ast.ScopingExpr{Parent: parent, X: ident} - ct.scopeStack = append(ct.scopeStack, newContext) + ct.scopeContext = append(ct.scopeContext, newContext) } } func (ct *Converter) PopScope() { - if len(ct.scopeStack) > 0 { - ct.scopeStack = ct.scopeStack[:len(ct.scopeStack)-1] + if len(ct.scopeContext) > 0 { + ct.scopeContext = ct.scopeContext[:len(ct.scopeContext)-1] } } func (ct *Converter) GetCurScope() ast.Expr { - if len(ct.scopeStack) == 0 { + if len(ct.scopeContext) == 0 { return nil } - return ct.scopeStack[len(ct.scopeStack)-1] + return ct.scopeContext[len(ct.scopeContext)-1] } func (ct *Converter) UpdateLoc(cursor clang.Cursor) { @@ -179,8 +179,8 @@ func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { return commentGroup } -// visit top decls (struct,class,function,enum & marco,include) -func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { +// visit top decls (struct,class,function,enum & macro,include) +func visitTop(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ct := (*Converter)(clientData) ct.UpdateLoc(cursor) @@ -194,8 +194,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi include := ct.ProcessInclude(cursor) curFile.Includes = append(curFile.Includes, include) case clang.CursorMacroDefinition: - marco := ct.ProcessMarco(cursor) - curFile.Macros = append(curFile.Macros, marco) + macro := ct.ProcessMacro(cursor) + curFile.Macros = append(curFile.Macros, macro) case clang.CursorEnumDecl: enum := ct.ProcessEnumDecl(cursor) curFile.Decls = append(curFile.Decls, enum) @@ -214,7 +214,7 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi curFile.Decls = append(curFile.Decls, ct.ProcessTypeDefDecl(cursor)) case clang.CursorNamespace: ct.PushScope(cursor) - clang.VisitChildren(cursor, visit, c.Pointer(ct)) + clang.VisitChildren(cursor, visitTop, c.Pointer(ct)) ct.PopScope() } return clang.ChildVisit_Continue @@ -222,8 +222,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi func (ct *Converter) Convert() (map[string]*ast.File, error) { cursor := ct.unit.Cursor() - // visit top decls (struct,class,function & marco,include) - clang.VisitChildren(cursor, visit, c.Pointer(ct)) + // visit top decls (struct,class,function & macro,include) + clang.VisitChildren(cursor, visitTop, c.Pointer(ct)) return ct.Files, nil } @@ -338,8 +338,8 @@ func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { } } -// current only collect marco which defined in file -func (ct *Converter) ProcessMarco(cursor clang.Cursor) *ast.Macro { +// current only collect macro which defined in file +func (ct *Converter) ProcessMacro(cursor clang.Cursor) *ast.Macro { name := cursor.String() defer name.Dispose() diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go index 12010b72..154a9c5d 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go @@ -3,35 +3,35 @@ package main import test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" func main() { - TestComment() + TestDoc() } -func TestComment() { +func TestDoc() { testCases := []string{ - `// not read comment 1 + `// not read doc 1 void foo();`, - `/* not read comment 2 */ + `/* not read doc 2 */ void foo();`, - `/// comment + `/// doc void foo();`, - `/** comment */ + `/** doc */ void foo();`, - `/*! comment */ + `/*! doc */ void foo();`, - `/// comment 1 -/// comment 2 + `/// doc 1 +/// doc 2 void foo();`, - `/*! comment 1 */ -/*! comment 2 */ + `/*! doc 1 */ +/*! doc 2 */ void foo();`, - `/** comment 1 */ -/** comment 1 */ + `/** doc 1 */ +/** doc 1 */ void foo();`, `/** - * comment 1 - * comment 2 + * doc 1 + * doc 2 */ void foo();`, } - test.RunTest("TestComment", testCases) + test.RunTest("TestDoc", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect index 6ec789be..57bc9f6a 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -1,5 +1,5 @@ #stdout -TestComment Case 1: +TestDoc Case 1: { "temp.h": { "decls": [{ @@ -28,7 +28,7 @@ TestComment Case 1: } } -TestComment Case 2: +TestDoc Case 2: { "temp.h": { "decls": [{ @@ -57,7 +57,7 @@ TestComment Case 2: } } -TestComment Case 3: +TestDoc Case 3: { "temp.h": { "decls": [{ @@ -66,7 +66,7 @@ TestComment Case 3: }, "Doc": { "List": [{ - "Text": "/// comment" + "Text": "/// doc" }] }, "Parent": null, @@ -88,7 +88,7 @@ TestComment Case 3: } } -TestComment Case 4: +TestDoc Case 4: { "temp.h": { "decls": [{ @@ -97,7 +97,7 @@ TestComment Case 4: }, "Doc": { "List": [{ - "Text": "/** comment */" + "Text": "/** doc */" }] }, "Parent": null, @@ -119,7 +119,7 @@ TestComment Case 4: } } -TestComment Case 5: +TestDoc Case 5: { "temp.h": { "decls": [{ @@ -128,7 +128,7 @@ TestComment Case 5: }, "Doc": { "List": [{ - "Text": "/*! comment */" + "Text": "/*! doc */" }] }, "Parent": null, @@ -150,7 +150,7 @@ TestComment Case 5: } } -TestComment Case 6: +TestDoc Case 6: { "temp.h": { "decls": [{ @@ -159,9 +159,9 @@ TestComment Case 6: }, "Doc": { "List": [{ - "Text": "/// comment 1" + "Text": "/// doc 1" }, { - "Text": "/// comment 2" + "Text": "/// doc 2" }] }, "Parent": null, @@ -183,7 +183,7 @@ TestComment Case 6: } } -TestComment Case 7: +TestDoc Case 7: { "temp.h": { "decls": [{ @@ -192,9 +192,9 @@ TestComment Case 7: }, "Doc": { "List": [{ - "Text": "/*! comment 1 */" + "Text": "/*! doc 1 */" }, { - "Text": "/*! comment 2 */" + "Text": "/*! doc 2 */" }] }, "Parent": null, @@ -216,7 +216,7 @@ TestComment Case 7: } } -TestComment Case 8: +TestDoc Case 8: { "temp.h": { "decls": [{ @@ -225,9 +225,9 @@ TestComment Case 8: }, "Doc": { "List": [{ - "Text": "/** comment 1 */" + "Text": "/** doc 1 */" }, { - "Text": "/** comment 1 */" + "Text": "/** doc 1 */" }] }, "Parent": null, @@ -249,7 +249,7 @@ TestComment Case 8: } } -TestComment Case 9: +TestDoc Case 9: { "temp.h": { "decls": [{ @@ -260,9 +260,9 @@ TestComment Case 9: "List": [{ "Text": "/**" }, { - "Text": " * comment 1" + "Text": " * doc 1" }, { - "Text": " * comment 2" + "Text": " * doc 2" }, { "Text": " */" }] From 3ac95a92133fa48f357025d49a1ae20ff5543882 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 23 Aug 2024 14:38:51 +0800 Subject: [PATCH 37/67] llcppsigfetch:refactor type reference test logic --- .../llcppsigfetch/parse/cvt_test/cvt.go | 53 +++ .../cvt_test/decl_test/func_test/func.go | 10 - .../cvt_test/decl_test/func_test/llgo.expect | 344 ------------------ .../cvt_test/decl_test/scope_test/llgo.expect | 90 ----- .../cvt_test/decl_test/scope_test/scope.go | 9 - .../cvt_test/decl_test/struct_test/struct.go | 2 +- .../decl_test/typedef_test/llgo.expect | 274 -------------- .../decl_test/typedef_test/typedef.go | 18 - .../parse/cvt_test/type_test/llgo.expect | 126 +++++++ .../parse/cvt_test/type_test/type.go | 106 ++++-- 10 files changed, 257 insertions(+), 775 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go index 974a4bb7..f879a181 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go @@ -1,8 +1,12 @@ package cvttest import ( + "fmt" + "unsafe" + "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/cjson" + "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" ) @@ -30,3 +34,52 @@ func RunTest(testName string, testCases []string) { converter.Dispose() } } + +type GetTypeOptions struct { + TypeCode string // e.g. "char*", "char**" + + // ExpectTypeKind specifies the expected type kind (optional) + // Use clang.Type_Invalid to accept any type (default behavior) + // *For complex types (when is included), specifying this is crucial + // to filter out the correct type, as there will be multiple VarDecl fields present + ExpectTypeKind clang.TypeKind + + // Args contains additional compilation arguments passed to Clang (optional) + // Default is []string{"-x", "c++", "-std=c++11"} + // *For complex C types, C language args Must be specified, e.g., []string{"-x", "c", "-std=c99"} + Args []string +} + +// GetType returns the clang.Type of the given type code +// Need to dispose the index and unit after using +// e.g. index.Dispose(), unit.Dispose() +func GetType(option *GetTypeOptions) (clang.Type, *clang.Index, *clang.TranslationUnit) { + code := fmt.Sprintf("%s placeholder;", option.TypeCode) + index, unit, err := parse.CreateTranslationUnit(&parse.Config{ + File: code, + Temp: true, + Args: option.Args, + }) + if err != nil { + panic(err) + } + cursor := unit.Cursor() + visitType := &typeVisitData{typ: &clang.Type{}, expectTypeKind: option.ExpectTypeKind} + + clang.VisitChildren(cursor, typeVisit, unsafe.Pointer(visitType)) + return *visitType.typ, index, unit +} + +type typeVisitData struct { + typ *clang.Type + expectTypeKind clang.TypeKind +} + +func typeVisit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { + visitData := (*typeVisitData)(clientData) + if cursor.Kind == clang.CursorVarDecl && (visitData.expectTypeKind == clang.TypeInvalid || cursor.Type().Kind == visitData.expectTypeKind) { + *visitData.typ = cursor.Type() + return clang.ChildVisit_Break + } + return clang.ChildVisit_Continue +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go index 9a555bbc..49eb4c23 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go @@ -10,17 +10,7 @@ func TestFuncDecl() { testCases := []string{ `void foo();`, `void foo(int a);`, - `float foo(int a,double b);`, `float* foo(int a,double b);`, - - `void foo(char* str);`, - `void* foo(char*** str);`, - - `void foo(char str[]);`, - `void foo(int arr[3][4]);`, - - `void foo(int& a);`, - `void foo(int&& a);`, } test.RunTest("TestFuncDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 1769dc0f..0e363a10 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -72,63 +72,6 @@ TestFuncDecl Case 2: } TestFuncDecl Case 3: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Ret": { - "Kind": 8, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 4: { "temp.h": { "decls": [{ @@ -187,293 +130,6 @@ TestFuncDecl Case 4: } } -TestFuncDecl Case 5: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "Kind": 2, - "Flags": 1 - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 6: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "X": { - "X": { - "Kind": 2, - "Flags": 1 - } - } - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }] - }, - "Ret": { - "X": { - "Kind": 0, - "Flags": 0 - } - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 7: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Elt": { - "Kind": 2, - "Flags": 1 - }, - "Len": null - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "str" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 8: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Elt": { - "Elt": { - "Kind": 6, - "Flags": 0 - }, - "Len": { - "Kind": 0, - "Value": "4" - } - }, - "Len": { - "Kind": 0, - "Value": "3" - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "arr" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 9: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "Kind": 6, - "Flags": 0 - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - -TestFuncDecl Case 10: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "X": { - "Kind": 6, - "Flags": 0 - } - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }], - "includes": [], - "macros": [] - } -} - #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index 58b035dd..28adaff8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -200,96 +200,6 @@ TestScope Case 5: } } -TestScope Case 6: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } - }, - "Name": { - "Name": "c" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": { - "X": { - "Name": "c" - }, - "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } - } - }, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [] - }, - "Ret": { - "Kind": 0, - "Flags": 0 - } - } - }] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "C" - }, - "Type": { - "X": { - "Name": "c" - }, - "Parent": { - "X": { - "Name": "b" - }, - "Parent": { - "Name": "a" - } - } - } - }], - "includes": [], - "macros": [] - } -} - #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go index d039e7c2..4c6985ce 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go @@ -25,15 +25,6 @@ func TestScope() { void foo(); }; }`, - - `namespace a { - namespace b { - class c { - void foo(); - }; - } - } - typedef a::b::c C;`, } test.RunTest("TestScope", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go index fd14b720..c18eecca 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go @@ -21,7 +21,7 @@ func TestStructDecl() { `struct A { int a; int b; - float foo(int a,double b);; + float foo(int a,double b); };`, } test.RunTest("TestStructDecl", testCases) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 63084752..064cb627 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -61,280 +61,6 @@ TestTypeDefDecl Case 2: } } -TestTypeDefDecl Case 3: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "StructFoo" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "UnionFoo" - }, - "Type": { - "Tag": 1, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "ClassFoo" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "EnumFoo" - }, - "Items": [] - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "STRUCT_FOO" - }, - "Type": { - "Name": "StructFoo" - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "UNION_FOO" - }, - "Type": { - "Name": "UnionFoo" - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "CLASS_FOO" - }, - "Type": { - "Name": "ClassFoo" - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "ENUM_FOO" - }, - "Type": { - "Name": "EnumFoo" - } - }], - "includes": [], - "macros": [] - } -} - -TestTypeDefDecl Case 4: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "StructFoo" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "UnionFoo" - }, - "Type": { - "Tag": 1, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "ClassFoo" - }, - "Type": { - "Tag": 3, - "Fields": { - "List": [] - }, - "Methods": [] - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "EnumFoo" - }, - "Items": [] - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "STRUCT_FOO" - }, - "Type": { - "Name": { - "Name": "StructFoo" - }, - "Tag": 0 - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "UNION_FOO" - }, - "Type": { - "Name": { - "Name": "UnionFoo" - }, - "Tag": 1 - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "CLASS_FOO" - }, - "Type": { - "Name": { - "Name": "ClassFoo" - }, - "Tag": 3 - } - }, { - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "ENUM_FOO" - }, - "Type": { - "Name": { - "Name": "EnumFoo" - }, - "Tag": 2 - } - }], - "includes": [], - "macros": [] - } -} - #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 9487e245..5700e11f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -12,24 +12,6 @@ func TestTypeDefDecl() { `typedef int INT; typedef INT STANDARD_INT;`, - - `struct StructFoo {}; - union UnionFoo {}; - class ClassFoo {}; - enum EnumFoo {}; - typedef StructFoo STRUCT_FOO; - typedef UnionFoo UNION_FOO; - typedef ClassFoo CLASS_FOO; - typedef EnumFoo ENUM_FOO;`, - - `struct StructFoo {}; - union UnionFoo {}; - class ClassFoo {}; - enum EnumFoo {}; - typedef struct StructFoo STRUCT_FOO; - typedef union UnionFoo UNION_FOO; - typedef class ClassFoo CLASS_FOO; - typedef enum EnumFoo ENUM_FOO;`, } test.RunTest("TestTypeDefDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 4ef2b3c1..df92c28e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -26,6 +26,132 @@ Complex:flags:0 kind:11 Complex:flags:16 kind:11 Complex:flags:20 kind:11 Unknown:flags:0 kind:0 +Type: char *: +{ + "X": { + "Kind": 2, + "Flags": 1 + } +} +Type: char ***: +{ + "X": { + "X": { + "X": { + "Kind": 2, + "Flags": 1 + } + } + } +} +Type: char[]: +{ + "Elt": { + "Kind": 2, + "Flags": 1 + }, + "Len": null +} +Type: char[10]: +{ + "Elt": { + "Kind": 2, + "Flags": 1 + }, + "Len": { + "Kind": 0, + "Value": "10" + } +} +Type: char[3][4]: +{ + "Elt": { + "Elt": { + "Kind": 2, + "Flags": 1 + }, + "Len": { + "Kind": 0, + "Value": "4" + } + }, + "Len": { + "Kind": 0, + "Value": "3" + } +} +Type: int &: +{ + "X": { + "Kind": 6, + "Flags": 0 + } +} +Type: int &&: +{ + "X": { + "Kind": 6, + "Flags": 0 + } +} +Type: Foo: +{ + "Name": "Foo" +} +Type: struct Foo: +{ + "Name": { + "Name": "Foo" + }, + "Tag": 0 +} +Type: Foo: +{ + "Name": "Foo" +} +Type: union Foo: +{ + "Name": { + "Name": "Foo" + }, + "Tag": 1 +} +Type: Foo: +{ + "Name": "Foo" +} +Type: enum Foo: +{ + "Name": { + "Name": "Foo" + }, + "Tag": 2 +} +Type: Foo: +{ + "Name": "Foo" +} +Type: class Foo: +{ + "Name": { + "Name": "Foo" + }, + "Tag": 3 +} +Type: a::b::c: +{ + "X": { + "Name": "c" + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + } +} #stderr todo: unknown builtin type: Ibm128 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index c7260d10..560a8021 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -2,15 +2,18 @@ package main import ( "fmt" - "unsafe" + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/c/clang" "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse" + test "github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse/cvt_test" "github.com/goplus/llgo/chore/llcppg/ast" ) func main() { TestBuiltinType() + TestNonBuiltinTypes() } func TestBuiltinType() { tests := []struct { @@ -41,9 +44,9 @@ func TestBuiltinType() { {"Double", btType(clang.TypeDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Double}}, {"LongDouble", btType(clang.TypeLongDouble), ast.BuiltinType{Kind: ast.Float, Flags: ast.Long | ast.Double}}, {"Float128", btType(clang.TypeFloat128), ast.BuiltinType{Kind: ast.Float128}}, - {"Complex", mockComplexType(0), ast.BuiltinType{Kind: ast.Complex}}, - {"Complex", mockComplexType(ast.Double), ast.BuiltinType{Flags: ast.Double, Kind: ast.Complex}}, - {"Complex", mockComplexType(ast.Long | ast.Double), ast.BuiltinType{Flags: ast.Long | ast.Double, Kind: ast.Complex}}, + {"Complex", getComplexType(0), ast.BuiltinType{Kind: ast.Complex}}, + {"Complex", getComplexType(ast.Double), ast.BuiltinType{Flags: ast.Double, Kind: ast.Complex}}, + {"Complex", getComplexType(ast.Long | ast.Double), ast.BuiltinType{Flags: ast.Long | ast.Double, Kind: ast.Complex}}, {"Unknown", btType(clang.TypeIbm128), ast.BuiltinType{Kind: ast.Void}}, } @@ -61,20 +64,72 @@ func TestBuiltinType() { } } +func TestNonBuiltinTypes() { + tests := []string{ + "char*", + "char***", + + "char[]", + "char[10]", + "char[3][4]", + + "int&", + "int&&", + + `struct Foo {}; + Foo`, + `struct Foo {}; + struct Foo`, + + `union Foo {}; + Foo`, + `union Foo {}; + union Foo`, + + `enum Foo {}; + Foo`, + `enum Foo {}; + enum Foo`, + + `class Foo {}; + Foo`, + `class Foo {}; + class Foo`, + + `namespace a { + namespace b { + class c { + }; + } + } + a::b::c`, + } + + for _, t := range tests { + typ, index, unit := test.GetType(&test.GetTypeOptions{ + TypeCode: t, + }) + converter := &parse.Converter{} + expr := converter.ProcessType(typ) + json := parse.MarshalASTExpr(expr) + str := json.Print() + + c.Printf(c.Str("Type: %s:\n"), typ.String()) + c.Printf(c.Str("%s\n"), str) + + cjson.FreeCStr(str) + json.Delete() + index.Dispose() + unit.Dispose() + } +} + func btType(kind clang.TypeKind) clang.Type { return clang.Type{Kind: kind} } -func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { - typ := (*clang.Type)(clientData) - if cursor.Kind == clang.CursorVarDecl && cursor.Type().Kind == clang.TypeComplex { - *typ = cursor.Type() - } - return clang.ChildVisit_Continue -} - -// mock complex type, this type cannot be directly created in Go -func mockComplexType(flag ast.TypeFlag) clang.Type { +// get complex type from source code parsed +func getComplexType(flag ast.TypeFlag) clang.Type { var typeStr string if flag&(ast.Long|ast.Double) == (ast.Long | ast.Double) { typeStr = "long double" @@ -84,21 +139,14 @@ func mockComplexType(flag ast.TypeFlag) clang.Type { typeStr = "float" } - code := fmt.Sprintf("#include \n%s complex z;", typeStr) - index, unit, err := parse.CreateTranslationUnit(&parse.Config{ - File: code, - Temp: true, - Args: []string{"-x", "c", "-std=c99"}, + code := fmt.Sprintf("#include \n%s complex", typeStr) + + // todo(zzy):free index and unit after test + typ, _, _ := test.GetType(&test.GetTypeOptions{ + TypeCode: code, + ExpectTypeKind: clang.TypeComplex, + Args: []string{"-x", "c", "-std=c99"}, }) - if err != nil { - panic(err) - } - defer index.Dispose() - - cursor := unit.Cursor() - complex := &clang.Type{} - clang.VisitChildren(cursor, visit, unsafe.Pointer(complex)) - - return *complex + return typ } From cd19625522e79df9c07f6104723a642bec4d995a Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 23 Aug 2024 14:57:36 +0800 Subject: [PATCH 38/67] llcppsigfetch:enum type --- chore/_xtool/llcppsigfetch/parse/cvt.go | 4 +- .../cvt_test/decl_test/enum_test/llgo.expect | 156 +++++++++--------- chore/_xtool/llcppsigfetch/parse/dump.go | 18 +- 3 files changed, 94 insertions(+), 84 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index aea635a9..2a9f9df9 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -334,7 +334,9 @@ func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { return &ast.EnumTypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Items: items, + Type: &ast.EnumType{ + Items: items, + }, } } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect index c4965551..55ae0666 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect @@ -13,31 +13,33 @@ TestEnumDecl Case 1: "Name": { "Name": "Foo" }, - "Items": [{ - "Name": { - "Name": "a" - }, - "Value": { - "Kind": 0, - "Value": "0" - } - }, { - "Name": { - "Name": "b" - }, - "Value": { - "Kind": 0, - "Value": "1" - } - }, { - "Name": { - "Name": "c" - }, - "Value": { - "Kind": 0, - "Value": "2" - } - }] + "Type": { + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "0" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }] + } }], "includes": [], "macros": [] @@ -58,31 +60,33 @@ TestEnumDecl Case 2: "Name": { "Name": "Foo" }, - "Items": [{ - "Name": { - "Name": "a" - }, - "Value": { - "Kind": 0, - "Value": "1" - } - }, { - "Name": { - "Name": "b" - }, - "Value": { - "Kind": 0, - "Value": "2" - } - }, { - "Name": { - "Name": "c" - }, - "Value": { - "Kind": 0, - "Value": "4" - } - }] + "Type": { + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "4" + } + }] + } }], "includes": [], "macros": [] @@ -103,31 +107,33 @@ TestEnumDecl Case 3: "Name": { "Name": "Foo" }, - "Items": [{ - "Name": { - "Name": "a" - }, - "Value": { - "Kind": 0, - "Value": "1" - } - }, { - "Name": { - "Name": "b" - }, - "Value": { - "Kind": 0, - "Value": "2" - } - }, { - "Name": { - "Name": "c" - }, - "Value": { - "Kind": 0, - "Value": "3" - } - }] + "Type": { + "Items": [{ + "Name": { + "Name": "a" + }, + "Value": { + "Kind": 0, + "Value": "1" + } + }, { + "Name": { + "Name": "b" + }, + "Value": { + "Kind": 0, + "Value": "2" + } + }, { + "Name": { + "Name": "c" + }, + "Value": { + "Kind": 0, + "Value": "3" + } + }] + } }], "includes": [], "macros": [] diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 1acb8c49..70dddf8e 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -68,11 +68,7 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { case *ast.EnumTypeDecl: MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) - items := cjson.Array() - for _, i := range d.Items { - items.AddItem(MarshalASTExpr(i)) - } - root.SetItem(c.Str("Items"), items) + root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) case *ast.TypedefDecl: MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) @@ -106,6 +102,15 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root := cjson.Object() switch d := t.(type) { + case *ast.EnumType: + items := cjson.Array() + for _, e := range d.Items { + items.AddItem(MarshalASTExpr(e)) + } + root.SetItem(c.Str("Items"), items) + case *ast.EnumItem: + root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value)) case *ast.RecordType: root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) @@ -143,9 +148,6 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { case *ast.TagExpr: root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) - case *ast.EnumItem: - root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) - root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value)) case *ast.BasicLit: root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) From c6336e920fcebdbfac6ee735eac5991f228ce22e Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 23 Aug 2024 15:13:15 +0800 Subject: [PATCH 39/67] llcppsigfetch:qualified name whith tag --- chore/_xtool/llcppsigfetch/parse/cvt.go | 32 ++++++++----------- .../parse/cvt_test/type_test/llgo.expect | 17 ++++++++++ .../parse/cvt_test/type_test/type.go | 8 +++++ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 2a9f9df9..e83f6f95 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -512,28 +512,12 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { if tagValue, ok := tagMap[parts[0]]; ok { return &ast.TagExpr{ Tag: tagValue, - Name: &ast.Ident{Name: parts[1]}, + Name: qualifiedExpr(parts[1]), } } } - // qualified name - // todo(zzy): qualified name with tag,like struct A::B - if strings.Contains(typeName, "::") { - scopeParts := strings.Split(typeName, "::") - var expr ast.Expr = &ast.Ident{Name: scopeParts[0]} - for _, part := range scopeParts[1:] { - expr = &ast.ScopingExpr{ - Parent: expr, - X: &ast.Ident{Name: part}, - } - } - return expr - } - - return &ast.Ident{ - Name: typeName, - } + return qualifiedExpr(typeName) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { @@ -628,3 +612,15 @@ func toToken(tok clang.Token) token.Token { return token.Token(tok.Kind() + 1) } } + +func qualifiedExpr(name string) ast.Expr { + parts := strings.Split(name, "::") + var expr ast.Expr = &ast.Ident{Name: parts[0]} + for _, part := range parts[1:] { + expr = &ast.ScopingExpr{ + Parent: expr, + X: &ast.Ident{Name: part}, + } + } + return expr +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index df92c28e..0f8b23b8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -152,6 +152,23 @@ Type: a::b::c: } } } +Type: class a::b::c: +{ + "Name": { + "X": { + "Name": "c" + }, + "Parent": { + "X": { + "Name": "b" + }, + "Parent": { + "Name": "a" + } + } + }, + "Tag": 3 +} #stderr todo: unknown builtin type: Ibm128 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index 560a8021..c4c51a35 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -103,6 +103,14 @@ func TestNonBuiltinTypes() { } } a::b::c`, + + `namespace a { + namespace b { + class c { + }; + } + } + class a::b::c`, } for _, t := range tests { From 5e5c975a9cd343e75ee322ab16578f3e193a1d14 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 23 Aug 2024 19:01:48 +0800 Subject: [PATCH 40/67] llcppsigfetch:constructor,destructor,static,override,virtual --- chore/_xtool/llcppsigfetch/parse/cvt.go | 35 +- .../cvt_test/decl_test/class_test/class.go | 16 + .../cvt_test/decl_test/class_test/llgo.expect | 352 +++++++++++++++++- .../decl_test/comment_test/llgo.expect | 90 ++++- .../cvt_test/decl_test/func_test/llgo.expect | 30 +- .../cvt_test/decl_test/scope_test/llgo.expect | 50 ++- .../decl_test/struct_test/llgo.expect | 10 +- chore/_xtool/llcppsigfetch/parse/dump.go | 49 ++- 8 files changed, 599 insertions(+), 33 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index e83f6f95..12ee6359 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -279,6 +279,7 @@ func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() + // function type will only collect return type // ProcessType can't get the field names,will collect in follows funcType, ok := ct.ProcessType(cursor.Type()).(*ast.FuncType) @@ -286,6 +287,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { fmt.Println("failed to process function type") return nil } + params := ct.ProcessFieldList(cursor) funcType.Params = params fn := &ast.FuncDecl{ @@ -293,6 +295,37 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: funcType, } + + // other info of function&method + if cursor.Kind == clang.CursorDestructor { + fn.IsDestructor = true + } + + if cursor.Kind == clang.CursorConstructor { + fn.IsConstructor = true + if cursor.IsExplicit() != 0 { + fn.IsExplicit = true + } + } + + if cursor.IsStatic() != 0 { + fn.IsStatic = true + } + + // virtual & pure virtual + if cursor.IsVirtual() != 0 || cursor.IsPureVirtual() != 0 { + fn.IsVirtual = true + } + + // todo(zzy):inline & const + + var numOverridden c.Uint + var overridden *clang.Cursor + cursor.OverriddenCursors(&overridden, &numOverridden) + if numOverridden > 0 { + fn.IsOverride = true + } + return fn } @@ -425,7 +458,7 @@ type visitMethodsContext struct { func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitMethodsContext)(clientData) - if cursor.Kind == clang.CursorCXXMethod { + if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor { method := ctx.converter.ProcessFuncDecl(cursor) if method != nil { *ctx.methods = append(*ctx.methods, method) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go index 747688f9..d3765433 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -17,6 +17,22 @@ func TestClassDecl() { int b; float foo(int a,double b); };`, + `class A { + A(); + explicit A(); + ~A(); + };`, + `class Base { + Base(); + virtual ~Base(); + virtual void foo(); + }; + class Derived : public Base { + Derived(); + ~Derived() override; + void foo() override; + }; + `, } test.RunTest("TestClassDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index 9b66edd8..2b893eff 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -150,7 +150,357 @@ TestClassDecl Case 2: "Kind": 8, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }] + } + }], + "includes": [], + "macros": [] + } +} + +TestClassDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "A" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": true, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "A" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": true, + "IsConstructor": true, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "~A" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": true, + "IsVirtual": false, + "IsOverride": false + }] + } + }], + "includes": [], + "macros": [] + } +} + +TestClassDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Base" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "Base" + }, + "Name": { + "Name": "Base" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": true, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "Base" + }, + "Name": { + "Name": "~Base" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": true, + "IsVirtual": true, + "IsOverride": false + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "Base" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": true, + "IsOverride": false + }] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Derived" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "Derived" + }, + "Name": { + "Name": "Derived" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": true, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "Derived" + }, + "Name": { + "Name": "~Derived" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": true, + "IsVirtual": true, + "IsOverride": true + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "Derived" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": true, + "IsOverride": true }] } }], diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect index 57bc9f6a..1ba87aff 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -21,7 +21,15 @@ TestDoc Case 1: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -50,7 +58,15 @@ TestDoc Case 2: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -81,7 +97,15 @@ TestDoc Case 3: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -112,7 +136,15 @@ TestDoc Case 4: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -143,7 +175,15 @@ TestDoc Case 5: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -176,7 +216,15 @@ TestDoc Case 6: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -209,7 +257,15 @@ TestDoc Case 7: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -242,7 +298,15 @@ TestDoc Case 8: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -279,7 +343,15 @@ TestDoc Case 9: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 0e363a10..5b3ceb20 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -21,7 +21,15 @@ TestFuncDecl Case 1: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -64,7 +72,15 @@ TestFuncDecl Case 2: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -123,7 +139,15 @@ TestFuncDecl Case 3: "Flags": 0 } } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index 28adaff8..91e06267 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -21,7 +21,15 @@ TestScope Case 1: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -52,7 +60,15 @@ TestScope Case 2: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -88,7 +104,15 @@ TestScope Case 3: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }], "includes": [], "macros": [] @@ -135,7 +159,15 @@ TestScope Case 4: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }] } }], @@ -191,7 +223,15 @@ TestScope Case 5: "Kind": 0, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }] } }], diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index 16b90baf..d6020d24 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -242,7 +242,15 @@ TestStructDecl Case 4: "Kind": 8, "Flags": 0 } - } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }] } }], diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 70dddf8e..17b37f1e 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -29,7 +29,7 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { includes := cjson.Array() for _, i := range file.Includes { include := cjson.Object() - include.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(i.Path))) + include.SetItem(c.Str("Path"), stringField(i.Path)) includes.AddItem(include) } root.SetItem(c.Str("includes"), includes) @@ -40,7 +40,7 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { macros := cjson.Array() for _, m := range file.Macros { marco := cjson.Object() - marco.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(m.Name))) + marco.SetItem(c.Str("Name"), stringField(m.Name)) tokens := cjson.Array() for _, tok := range m.Tokens { tokens.AddItem(Token(tok)) @@ -54,8 +54,8 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { } func Token(tok *ast.Token) *cjson.JSON { root := cjson.Object() - root.SetItem(c.Str("Token"), cjson.Number(float64(tok.Token))) - root.SetItem(c.Str("Lit"), cjson.String(c.AllocaCStr(tok.Lit))) + root.SetItem(c.Str("Token"), numberField(uint(tok.Token))) + root.SetItem(c.Str("Lit"), stringField(tok.Lit)) return root } @@ -77,6 +77,14 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) + root.SetItem(c.Str("IsInline"), boolField(d.IsInline)) + root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic)) + root.SetItem(c.Str("IsConst"), boolField(d.IsConst)) + root.SetItem(c.Str("IsExplicit"), boolField(d.IsExplicit)) + root.SetItem(c.Str("IsConstructor"), boolField(d.IsConstructor)) + root.SetItem(c.Str("IsDestructor"), boolField(d.IsDestructor)) + root.SetItem(c.Str("IsVirtual"), boolField(d.IsVirtual)) + root.SetItem(c.Str("IsOverride"), boolField(d.IsOverride)) case *ast.TypeDecl: MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) @@ -87,7 +95,7 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { func MarshalASTDeclBase(decl ast.DeclBase, root *cjson.JSON) { loc := cjson.Object() - loc.SetItem(c.Str("File"), cjson.String(c.AllocaCStr(decl.Loc.File))) + loc.SetItem(c.Str("File"), stringField(decl.Loc.File)) root.SetItem(c.Str("Loc"), loc) root.SetItem(c.Str("Doc"), MarshalASTExpr(decl.Doc)) @@ -112,7 +120,7 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value)) case *ast.RecordType: - root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) + root.SetItem(c.Str("Tag"), numberField(uint(d.Tag))) root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) methods := cjson.Array() for _, m := range d.Methods { @@ -144,13 +152,13 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { if d == nil { return cjson.Null() } - root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + root.SetItem(c.Str("Name"), stringField(d.Name)) case *ast.TagExpr: root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) - root.SetItem(c.Str("Tag"), cjson.Number(float64(d.Tag))) + root.SetItem(c.Str("Tag"), numberField(uint(d.Tag))) case *ast.BasicLit: - root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) - root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) + root.SetItem(c.Str("Kind"), numberField(uint(d.Kind))) + root.SetItem(c.Str("Value"), stringField(d.Value)) case *ast.LvalueRefType: root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.RvalueRefType: @@ -161,13 +169,13 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Elt"), MarshalASTExpr(d.Elt)) root.SetItem(c.Str("Len"), MarshalASTExpr(d.Len)) case *ast.BuiltinType: - root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) - root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) + root.SetItem(c.Str("Kind"), numberField(uint(d.Kind))) + root.SetItem(c.Str("Flags"), numberField(uint(d.Flags))) case *ast.Comment: if d == nil { return cjson.Null() } - root.SetItem(c.Str("Text"), cjson.String(c.AllocaCStr(d.Text))) + root.SetItem(c.Str("Text"), stringField(d.Text)) case *ast.CommentGroup: if d == nil { return cjson.Null() @@ -185,3 +193,18 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } return root } + +func stringField(s string) *cjson.JSON { + return cjson.String(c.AllocaCStr(s)) +} + +func numberField(n uint) *cjson.JSON { + return cjson.Number(float64(n)) +} + +func boolField(b bool) *cjson.JSON { + if b { + return cjson.True() + } + return cjson.False() +} From 24fd2e184958465202e712fe99e1acfcd1d73ed1 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 23 Aug 2024 23:09:29 +0800 Subject: [PATCH 41/67] llcppsigfetch:free override cursor --- chore/_xtool/llcppsigfetch/parse/cvt.go | 1 + 1 file changed, 1 insertion(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 12ee6359..5bf9be0f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -325,6 +325,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { if numOverridden > 0 { fn.IsOverride = true } + overridden.DisposeOverriddenCursors() return fn } From b1225951f2ddb1abd5b0166de6b4f7b022fd26f1 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 26 Aug 2024 10:56:36 +0800 Subject: [PATCH 42/67] llcppsigfetch:func inline & static --- chore/_xtool/llcppsigfetch/parse/cvt.go | 59 +++++++++-------- .../cvt_test/decl_test/class_test/class.go | 1 + .../cvt_test/decl_test/class_test/llgo.expect | 30 +++++++++ .../cvt_test/decl_test/func_test/func.go | 1 + .../cvt_test/decl_test/func_test/llgo.expect | 65 +++++++++++++++++++ 5 files changed, 130 insertions(+), 26 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 5bf9be0f..a4f38f99 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -296,37 +296,44 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { Type: funcType, } - // other info of function&method - if cursor.Kind == clang.CursorDestructor { - fn.IsDestructor = true + if cursor.IsFunctionInlined() != 0 { + fn.IsInline = true } - if cursor.Kind == clang.CursorConstructor { - fn.IsConstructor = true - if cursor.IsExplicit() != 0 { - fn.IsExplicit = true + if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorDestructor || cursor.Kind == clang.CursorConstructor { + if cursor.Kind == clang.CursorDestructor { + fn.IsDestructor = true + } + if cursor.Kind == clang.CursorConstructor { + fn.IsConstructor = true + if cursor.IsExplicit() != 0 { + fn.IsExplicit = true + } + } + if cursor.IsStatic() != 0 { + fn.IsStatic = true + } + // virtual & pure virtual + if cursor.IsVirtual() != 0 || cursor.IsPureVirtual() != 0 { + fn.IsVirtual = true + } + if cursor.IsConst() != 0 { + fn.IsConst = true + } + + var numOverridden c.Uint + var overridden *clang.Cursor + cursor.OverriddenCursors(&overridden, &numOverridden) + if numOverridden > 0 { + fn.IsOverride = true + } + overridden.DisposeOverriddenCursors() + } else { + if cursor.StorageClass() == clang.SCStatic { + fn.IsStatic = true } } - if cursor.IsStatic() != 0 { - fn.IsStatic = true - } - - // virtual & pure virtual - if cursor.IsVirtual() != 0 || cursor.IsPureVirtual() != 0 { - fn.IsVirtual = true - } - - // todo(zzy):inline & const - - var numOverridden c.Uint - var overridden *clang.Cursor - cursor.OverriddenCursors(&overridden, &numOverridden) - if numOverridden > 0 { - fn.IsOverride = true - } - overridden.DisposeOverriddenCursors() - return fn } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go index d3765433..5da535c8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -21,6 +21,7 @@ func TestClassDecl() { A(); explicit A(); ~A(); + static inline void foo(); };`, `class Base { Base(); diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index 2b893eff..f116cbd2 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -276,6 +276,36 @@ TestClassDecl Case 3: "IsDestructor": true, "IsVirtual": false, "IsOverride": false + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": true, + "IsStatic": true, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false }] } }], diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go index 49eb4c23..55caff06 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go @@ -11,6 +11,7 @@ func TestFuncDecl() { `void foo();`, `void foo(int a);`, `float* foo(int a,double b);`, + `static inline int add(int a, int b);`, } test.RunTest("TestFuncDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 5b3ceb20..638976bb 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -154,6 +154,71 @@ TestFuncDecl Case 3: } } +TestFuncDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "add" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "b" + }] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + }, + "IsInline": true, + "IsStatic": true, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }], + "includes": [], + "macros": [] + } +} + #stderr From fc04083cb2be2dd0e527ff39cb56e1fa9dacb5f7 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 26 Aug 2024 14:17:33 +0800 Subject: [PATCH 43/67] llcppsigfetch:collect public methods --- chore/_xtool/llcppsigfetch/parse/cvt.go | 7 +++++-- .../parse/cvt_test/decl_test/class_test/class.go | 7 +++++++ .../parse/cvt_test/decl_test/scope_test/scope.go | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index a4f38f99..d59485c3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -300,7 +300,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { fn.IsInline = true } - if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorDestructor || cursor.Kind == clang.CursorConstructor { + if isMethod(cursor) { if cursor.Kind == clang.CursorDestructor { fn.IsDestructor = true } @@ -466,7 +466,7 @@ type visitMethodsContext struct { func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitMethodsContext)(clientData) - if cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor { + if isMethod(cursor) && cursor.CXXAccessSpecifier() != clang.CXXPrivate { method := ctx.converter.ProcessFuncDecl(cursor) if method != nil { *ctx.methods = append(*ctx.methods, method) @@ -653,6 +653,9 @@ func toToken(tok clang.Token) token.Token { return token.Token(tok.Kind() + 1) } } +func isMethod(cursor clang.Cursor) bool { + return cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor +} func qualifiedExpr(name string) ast.Expr { parts := strings.Split(name, "::") diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go index 5da535c8..65f4ca5f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -9,26 +9,33 @@ func main() { func TestClassDecl() { testCases := []string{ `class A { + public: int a; int b; };`, `class A { + public: int a; int b; float foo(int a,double b); + private: + void bar(); };`, `class A { + public: A(); explicit A(); ~A(); static inline void foo(); };`, `class Base { + public: Base(); virtual ~Base(); virtual void foo(); }; class Derived : public Base { + public: Derived(); ~Derived() override; void foo() override; diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go index 4c6985ce..29332bfb 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/scope.go @@ -18,10 +18,12 @@ func TestScope() { } }`, `class a { + public: void foo(); };`, `namespace a { class b { + public: void foo(); }; }`, From b524472b9e0cada021976b264de070c509df1077 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 26 Aug 2024 18:43:38 +0800 Subject: [PATCH 44/67] llcppsigfetch:out of class method --- chore/_xtool/llcppsigfetch/parse/cvt.go | 26 +++++++- .../cvt_test/decl_test/class_test/class.go | 5 ++ .../cvt_test/decl_test/class_test/llgo.expect | 64 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index d59485c3..14757cba 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -208,7 +208,9 @@ func visitTop(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chil case clang.CursorUnionDecl: unionDecl := ct.ProcessUnionDecl(cursor) curFile.Decls = append(curFile.Decls, unionDecl) - case clang.CursorFunctionDecl: + case clang.CursorFunctionDecl, clang.CursorCXXMethod, clang.CursorConstructor, clang.CursorDestructor: + // Handle functions and class methods (including out-of-class method) + // Example: void MyClass::myMethod() { ... } out-of-class method curFile.Decls = append(curFile.Decls, ct.ProcessFuncDecl(cursor)) case clang.CursorTypedefDecl: curFile.Decls = append(curFile.Decls, ct.ProcessTypeDefDecl(cursor)) @@ -276,6 +278,7 @@ func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { } } +// converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() @@ -301,6 +304,11 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { } if isMethod(cursor) { + + if parent := cursor.SemanticParent(); parent.Equal(cursor.LexicalParent()) != 1 { + fn.DeclBase.Parent = qualifiedExpr(buildQualifiedName(cursor)) + } + if cursor.Kind == clang.CursorDestructor { fn.IsDestructor = true } @@ -657,6 +665,22 @@ func isMethod(cursor clang.Cursor) bool { return cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor } +func buildQualifiedName(cursor clang.Cursor) string { + var parts []string + cursor = cursor.SemanticParent() + + // Traverse up the semantic parents + for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit { + name := cursor.String() + qualified := c.GoString(name.CStr()) + parts = append([]string{qualified}, parts...) + cursor = cursor.SemanticParent() + name.Dispose() + } + + return strings.Join(parts, "::") +} + func qualifiedExpr(name string) ast.Expr { parts := strings.Split(name, "::") var expr ast.Expr = &ast.Ident{Name: parts[0]} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go index 65f4ca5f..3997016b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -41,6 +41,11 @@ func TestClassDecl() { void foo() override; }; `, + `namespace A{ + class Foo{} + } + void A::Foo::bar(); + `, } test.RunTest("TestClassDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index f116cbd2..a613664f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -539,6 +539,70 @@ TestClassDecl Case 4: } } +TestClassDecl Case 5: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "Name": "A" + }, + "Name": { + "Name": "Foo" + }, + "Type": { + "Tag": 3, + "Fields": { + "List": [] + }, + "Methods": [] + } + }, { + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": { + "X": { + "Name": "Foo" + }, + "Parent": { + "Name": "A" + } + }, + "Name": { + "Name": "bar" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }], + "includes": [], + "macros": [] + } +} + #stderr From 38eb981d2c6b6ae7685333755cce65ea845365fc Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 27 Aug 2024 10:17:46 +0800 Subject: [PATCH 45/67] llcppsigfetch:remove redundant test case --- .../decl_test/struct_test/llgo.expect | 111 ------------------ .../cvt_test/decl_test/struct_test/struct.go | 5 - 2 files changed, 116 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index d6020d24..03962fa5 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -148,117 +148,6 @@ TestStructDecl Case 3: } } -TestStructDecl Case 4: -{ - "temp.h": { - "decls": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "A" - }, - "Type": { - "Tag": 0, - "Fields": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Methods": [{ - "Loc": { - "File": "temp.h" - }, - "Doc": { - "List": [] - }, - "Parent": null, - "Name": { - "Name": "foo" - }, - "Type": { - "Params": { - "List": [{ - "Type": { - "Kind": 6, - "Flags": 0 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "a" - }] - }, { - "Type": { - "Kind": 8, - "Flags": 16 - }, - "Doc": { - "List": [] - }, - "Comment": { - "List": [] - }, - "Names": [{ - "Name": "b" - }] - }] - }, - "Ret": { - "Kind": 8, - "Flags": 0 - } - }, - "IsInline": false, - "IsStatic": false, - "IsConst": false, - "IsExplicit": false, - "IsConstructor": false, - "IsDestructor": false, - "IsVirtual": false, - "IsOverride": false - }] - } - }], - "includes": [], - "macros": [] - } -} - #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go index c18eecca..d73a46bf 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go @@ -18,11 +18,6 @@ func TestStructDecl() { `struct A { int a, b; };`, - `struct A { - int a; - int b; - float foo(int a,double b); - };`, } test.RunTest("TestStructDecl", testCases) } From a897683272e547df8e75816d755b3233de7babd8 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 27 Aug 2024 11:40:29 +0800 Subject: [PATCH 46/67] Use semantic parent to refactor scoping expression construction --- chore/_xtool/llcppsigfetch/parse/cvt.go | 61 +++++++------------------ 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 14757cba..459b46e7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -15,11 +15,10 @@ import ( ) type Converter struct { - Files map[string]*ast.File - curLoc ast.Location - index *clang.Index - unit *clang.TranslationUnit - scopeContext []ast.Expr //namespace & class + Files map[string]*ast.File + curLoc ast.Location + index *clang.Index + unit *clang.TranslationUnit } type Config struct { @@ -92,33 +91,6 @@ func (ct *Converter) Dispose() { ct.unit.Dispose() } -func (ct *Converter) PushScope(cursor clang.Cursor) { - name := cursor.String() - defer name.Dispose() - ident := &ast.Ident{Name: c.GoString(name.CStr())} - - if len(ct.scopeContext) == 0 { - ct.scopeContext = append(ct.scopeContext, ident) - } else { - parent := ct.scopeContext[len(ct.scopeContext)-1] - newContext := &ast.ScopingExpr{Parent: parent, X: ident} - ct.scopeContext = append(ct.scopeContext, newContext) - } -} - -func (ct *Converter) PopScope() { - if len(ct.scopeContext) > 0 { - ct.scopeContext = ct.scopeContext[:len(ct.scopeContext)-1] - } -} - -func (ct *Converter) GetCurScope() ast.Expr { - if len(ct.scopeContext) == 0 { - return nil - } - return ct.scopeContext[len(ct.scopeContext)-1] -} - func (ct *Converter) UpdateLoc(cursor clang.Cursor) { loc := cursor.Location() var file clang.File @@ -165,7 +137,7 @@ func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { loc := ct.curLoc return ast.DeclBase{ Loc: &loc, - Parent: ct.GetCurScope(), + Parent: buildScopingExpr(cursor.SemanticParent()), Doc: commentGroup, } } @@ -215,9 +187,7 @@ func visitTop(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chil case clang.CursorTypedefDecl: curFile.Decls = append(curFile.Decls, ct.ProcessTypeDefDecl(cursor)) case clang.CursorNamespace: - ct.PushScope(cursor) clang.VisitChildren(cursor, visitTop, c.Pointer(ct)) - ct.PopScope() } return clang.ChildVisit_Continue } @@ -306,7 +276,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { if isMethod(cursor) { if parent := cursor.SemanticParent(); parent.Equal(cursor.LexicalParent()) != 1 { - fn.DeclBase.Parent = qualifiedExpr(buildQualifiedName(cursor)) + fn.DeclBase.Parent = buildScopingExpr(cursor.SemanticParent()) } if cursor.Kind == clang.CursorDestructor { @@ -522,9 +492,7 @@ func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { // Pushing class scope before processing its type and popping after base := ct.CreateDeclBase(cursor) - ct.PushScope(cursor) typ := ct.ProcessRecordType(cursor, ast.Class) - ct.PopScope() return &ast.TypeDecl{ DeclBase: base, @@ -561,12 +529,12 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { if tagValue, ok := tagMap[parts[0]]; ok { return &ast.TagExpr{ Tag: tagValue, - Name: qualifiedExpr(parts[1]), + Name: buildScopingExpr(t.TypeDeclaration()), } } } - return qualifiedExpr(typeName) + return buildScopingExpr(t.TypeDeclaration()) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { @@ -665,9 +633,9 @@ func isMethod(cursor clang.Cursor) bool { return cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor } -func buildQualifiedName(cursor clang.Cursor) string { +// Constructs a complete scoping expression by traversing the semantic parents, starting from the given clang.Cursor +func buildScopingExpr(cursor clang.Cursor) ast.Expr { var parts []string - cursor = cursor.SemanticParent() // Traverse up the semantic parents for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit { @@ -678,11 +646,14 @@ func buildQualifiedName(cursor clang.Cursor) string { name.Dispose() } - return strings.Join(parts, "::") + return buildScopingFromParts(parts) } -func qualifiedExpr(name string) ast.Expr { - parts := strings.Split(name, "::") +func buildScopingFromParts(parts []string) ast.Expr { + if len(parts) == 0 { + return nil + } + var expr ast.Expr = &ast.Ident{Name: parts[0]} for _, part := range parts[1:] { expr = &ast.ScopingExpr{ From e57ee175322027a6bb3dc4a943c34bb35f5bb9cc Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 27 Aug 2024 15:14:44 +0800 Subject: [PATCH 47/67] llcppsigfetch:func pointer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 28 ++++++- .../decl_test/struct_test/llgo.expect | 78 +++++++++++++++++++ .../cvt_test/decl_test/struct_test/struct.go | 4 + .../decl_test/typedef_test/llgo.expect | 47 +++++++++++ .../decl_test/typedef_test/typedef.go | 2 + .../parse/cvt_test/type_test/llgo.expect | 28 +++++++ .../parse/cvt_test/type_test/type.go | 2 + 7 files changed, 186 insertions(+), 3 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 459b46e7..13c22898 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -217,8 +217,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeFunctionProto: // function type will only collect return type, params will be collected in ProcessFuncDecl - ret := ct.ProcessType(t.ResultType()) - expr = &ast.FuncType{Ret: ret} + expr = ct.ProcessFunctionType(t) case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: if t.Kind == clang.TypeConstantArray { len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) @@ -238,6 +237,30 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { return expr } +// For function types, we can only obtain the parameter types, but not the parameter names. +// This is because we cannot reverse-lookup the corresponding declaration node from a function type. +// Note: For function declarations, parameter names are collected in the ProcessFuncDecl method. +func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { + // Note: Attempting to get the type declaration for a function type will result in CursorNoDeclFound + // cursor := t.TypeDeclaration() + // This would return CursorNoDeclFound + + ret := ct.ProcessType(t.ResultType()) + params := &ast.FieldList{List: make([]*ast.Field, 0)} + numArgs := t.NumArgTypes() + for i := 0; i < int(numArgs); i++ { + argType := t.ArgType(c.Uint(i)) + params.List = append(params.List, &ast.Field{ + Type: ct.ProcessType(argType), + }) + } + + return &ast.FuncType{ + Ret: ret, + Params: params, + } +} + func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { name := cursor.String() defer name.Dispose() @@ -260,7 +283,6 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { fmt.Println("failed to process function type") return nil } - params := ct.ProcessFieldList(cursor) funcType.Params = params fn := &ast.FuncDecl{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index 03962fa5..8d208c32 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -148,6 +148,84 @@ TestStructDecl Case 3: } } +TestStructDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "X": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "Foo" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go index d73a46bf..6e05aae3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go @@ -18,6 +18,10 @@ func TestStructDecl() { `struct A { int a, b; };`, + `struct A { + int a; + int (*Foo)(int, int); + };`, } test.RunTest("TestStructDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 064cb627..98f60b2c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -61,6 +61,53 @@ TestTypeDefDecl Case 2: } } +TestTypeDefDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Type": { + "X": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 5700e11f..7b2548cf 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -12,6 +12,8 @@ func TestTypeDefDecl() { `typedef int INT; typedef INT STANDARD_INT;`, + + `typedef int (*Foo)(int, int);`, } test.RunTest("TestTypeDefDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 0f8b23b8..7adead79 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -169,6 +169,34 @@ Type: class a::b::c: }, "Tag": 3 } +Type: int (*)(int, char): +{ + "X": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 2, + "Flags": 1 + }, + "Doc": null, + "Comment": null, + "Names": [] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + } +} #stderr todo: unknown builtin type: Ibm128 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index c4c51a35..789dfa3c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -111,6 +111,8 @@ func TestNonBuiltinTypes() { } } class a::b::c`, + + `int (*p)(int, char);`, } for _, t := range tests { From 0ac48369fe7f85eb40f0d112ec79f2aa8e839b37 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 27 Aug 2024 16:18:29 +0800 Subject: [PATCH 48/67] llcppsigfetch:variadic param --- chore/_xtool/llcppsigfetch/parse/cvt.go | 80 +++++++++++-------- .../cvt_test/decl_test/func_test/func.go | 1 + .../cvt_test/decl_test/func_test/llgo.expect | 59 +++++++++++++- .../decl_test/typedef_test/llgo.expect | 6 ++ .../decl_test/typedef_test/typedef.go | 2 +- chore/_xtool/llcppsigfetch/parse/dump.go | 1 + 6 files changed, 115 insertions(+), 34 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 13c22898..842c5bd6 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -255,6 +255,12 @@ func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { }) } + if t.IsFunctionTypeVariadic() != 0 { + params.List = append(params.List, &ast.Field{ + Type: &ast.Variadic{}, + }) + } + return &ast.FuncType{ Ret: ret, Params: params, @@ -296,38 +302,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { } if isMethod(cursor) { - - if parent := cursor.SemanticParent(); parent.Equal(cursor.LexicalParent()) != 1 { - fn.DeclBase.Parent = buildScopingExpr(cursor.SemanticParent()) - } - - if cursor.Kind == clang.CursorDestructor { - fn.IsDestructor = true - } - if cursor.Kind == clang.CursorConstructor { - fn.IsConstructor = true - if cursor.IsExplicit() != 0 { - fn.IsExplicit = true - } - } - if cursor.IsStatic() != 0 { - fn.IsStatic = true - } - // virtual & pure virtual - if cursor.IsVirtual() != 0 || cursor.IsPureVirtual() != 0 { - fn.IsVirtual = true - } - if cursor.IsConst() != 0 { - fn.IsConst = true - } - - var numOverridden c.Uint - var overridden *clang.Cursor - cursor.OverriddenCursors(&overridden, &numOverridden) - if numOverridden > 0 { - fn.IsOverride = true - } - overridden.DisposeOverriddenCursors() + ct.ProcessMethodAttributes(cursor, fn) } else { if cursor.StorageClass() == clang.SCStatic { fn.IsStatic = true @@ -337,6 +312,41 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { return fn } +// get Methods Attributes +func (ct *Converter) ProcessMethodAttributes(cursor clang.Cursor, fn *ast.FuncDecl) { + if parent := cursor.SemanticParent(); parent.Equal(cursor.LexicalParent()) != 1 { + fn.DeclBase.Parent = buildScopingExpr(cursor.SemanticParent()) + } + + switch cursor.Kind { + case clang.CursorDestructor: + fn.IsDestructor = true + case clang.CursorConstructor: + fn.IsConstructor = true + if cursor.IsExplicit() != 0 { + fn.IsExplicit = true + } + } + + if cursor.IsStatic() != 0 { + fn.IsStatic = true + } + if cursor.IsVirtual() != 0 || cursor.IsPureVirtual() != 0 { + fn.IsVirtual = true + } + if cursor.IsConst() != 0 { + fn.IsConst = true + } + + var numOverridden c.Uint + var overridden *clang.Cursor + cursor.OverriddenCursors(&overridden, &numOverridden) + if numOverridden > 0 { + fn.IsOverride = true + } + overridden.DisposeOverriddenCursors() +} + type visitEnumContext struct { enum *[]*ast.EnumItem converter *Converter @@ -449,12 +459,18 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan return clang.ChildVisit_Continue } +// For Record Type(struct,union ...) & Func 's FieldList func (ct *Converter) ProcessFieldList(cursor clang.Cursor) *ast.FieldList { params := &ast.FieldList{List: []*ast.Field{}} ctx := &visitFieldContext{ params: params, converter: ct, } + if (cursor.Kind == clang.CursorFunctionDecl || isMethod(cursor)) && cursor.IsVariadic() != 0 { + params.List = append(params.List, &ast.Field{ + Type: &ast.Variadic{}, + }) + } clang.VisitChildren(cursor, visitFieldList, c.Pointer(ctx)) return params } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go index 55caff06..f2e18e17 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/func.go @@ -10,6 +10,7 @@ func TestFuncDecl() { testCases := []string{ `void foo();`, `void foo(int a);`, + `void foo(int a,...);`, `float* foo(int a,double b);`, `static inline int add(int a, int b);`, } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 638976bb..227f446c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -88,6 +88,63 @@ TestFuncDecl Case 2: } TestFuncDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }], + "includes": [], + "macros": [] + } +} + +TestFuncDecl Case 4: { "temp.h": { "decls": [{ @@ -154,7 +211,7 @@ TestFuncDecl Case 3: } } -TestFuncDecl Case 4: +TestFuncDecl Case 5: { "temp.h": { "decls": [{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 98f60b2c..d88c973a 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -94,6 +94,12 @@ TestTypeDefDecl Case 3: "Doc": null, "Comment": null, "Names": [] + }, { + "Type": { + }, + "Doc": null, + "Comment": null, + "Names": [] }] }, "Ret": { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 7b2548cf..240fc660 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -13,7 +13,7 @@ func TestTypeDefDecl() { `typedef int INT; typedef INT STANDARD_INT;`, - `typedef int (*Foo)(int, int);`, + `typedef int (*Foo)(int, int, ...);`, } test.RunTest("TestTypeDefDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 17b37f1e..5cc02bb0 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -148,6 +148,7 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { names.AddItem(MarshalASTExpr(n)) } root.SetItem(c.Str("Names"), names) + case *ast.Variadic: case *ast.Ident: if d == nil { return cjson.Null() From 9351a1f9000c28e10159246f7c2562d711c32ed1 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 27 Aug 2024 18:50:51 +0800 Subject: [PATCH 49/67] llcppsigfetch:anonymous elaborated type refer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 52 ++++++--- .../decl_test/struct_test/llgo.expect | 100 ++++++++++++++++++ .../cvt_test/decl_test/struct_test/struct.go | 8 ++ .../cvt_test/decl_test/union_test/llgo.expect | 100 ++++++++++++++++++ .../cvt_test/decl_test/union_test/union.go | 8 ++ .../parse/cvt_test/type_test/llgo.expect | 78 ++++++++++++++ .../parse/cvt_test/type_test/type.go | 14 ++- 7 files changed, 346 insertions(+), 14 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 842c5bd6..9ce3af3d 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -372,22 +372,26 @@ func visitEnum(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chi return clang.ChildVisit_Continue } -func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { - name := cursor.String() - defer name.Dispose() +func (ct *Converter) ProcessEnumType(cursor clang.Cursor) *ast.EnumType { items := make([]*ast.EnumItem, 0) ctx := &visitEnumContext{ enum: &items, converter: ct, } clang.VisitChildren(cursor, visitEnum, c.Pointer(ctx)) + return &ast.EnumType{ + Items: items, + } +} + +func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { + name := cursor.String() + defer name.Dispose() return &ast.EnumTypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Type: &ast.EnumType{ - Items: items, - }, + Type: ct.ProcessEnumType(cursor), } } @@ -501,7 +505,7 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { return methods } -func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor, tag ast.Tag) *ast.TypeDecl { +func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) *ast.TypeDecl { anony := cursor.IsAnonymousRecordDecl() var name *ast.Ident @@ -514,23 +518,23 @@ func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor, tag ast.Tag) *ast.Ty return &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: name, - Type: ct.ProcessRecordType(cursor, tag), + Type: ct.ProcessRecordType(cursor), } } func (ct *Converter) ProcessStructDecl(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecordDecl(cursor, ast.Struct) + return ct.ProcessRecordDecl(cursor) } func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) *ast.TypeDecl { - return ct.ProcessRecordDecl(cursor, ast.Union) + return ct.ProcessRecordDecl(cursor) } func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { // Pushing class scope before processing its type and popping after base := ct.CreateDeclBase(cursor) - typ := ct.ProcessRecordType(cursor, ast.Class) + typ := ct.ProcessRecordType(cursor) return &ast.TypeDecl{ DeclBase: base, @@ -539,18 +543,40 @@ func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { } } -func (ct *Converter) ProcessRecordType(cursor clang.Cursor, tag ast.Tag) *ast.RecordType { +func (ct *Converter) ProcessRecordType(cursor clang.Cursor) *ast.RecordType { + tagMap := map[clang.CursorKind]ast.Tag{ + clang.CursorStructDecl: ast.Struct, + clang.CursorUnionDecl: ast.Union, + clang.CursorClassDecl: ast.Class, + } return &ast.RecordType{ - Tag: tag, + Tag: tagMap[cursor.Kind], Fields: ct.ProcessFieldList(cursor), Methods: ct.ProcessMethods(cursor), } } +// process ElaboratedType Reference +// +// 1. Named elaborated type references: +// - Examples: struct MyStruct, union MyUnion, class MyClass, enum MyEnum +// - Handling: Constructed as TagExpr or ScopingExpr references +// +// 2. Anonymous elaborated type references: +// - Examples: struct { int x; int y; }, union { int a; float b; } +// - Handling: Retrieve their corresponding concrete types func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { name := t.String() defer name.Dispose() + decl := t.TypeDeclaration() + if decl.IsAnonymous() != 0 { + if decl.Kind == clang.CursorEnumDecl { + return ct.ProcessEnumType(decl) + } + return ct.ProcessRecordType(decl) + } + typeName := c.GoString(name.CStr()) tagMap := map[string]ast.Tag{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index 8d208c32..f3021595 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -226,6 +226,106 @@ TestStructDecl Case 4: } } +TestStructDecl Case 5: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Person" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "age" + }] + }, { + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "year" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "day" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "month" + }] + }] + }, + "Methods": [] + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "birthday" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go index 6e05aae3..39d40d8c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go @@ -22,6 +22,14 @@ func TestStructDecl() { int a; int (*Foo)(int, int); };`, + `struct Person { + int age; + struct { + int year; + int day; + int month; + } birthday; + };`, } test.RunTest("TestStructDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect index 7c0943df..07db788f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect @@ -107,6 +107,106 @@ TestUnionDecl Case 2: } } +TestUnionDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "OuterUnion" + }, + "Type": { + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "i" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "f" + }] + }, { + "Type": { + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 2, + "Flags": 1 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "c" + }] + }, { + "Type": { + "Kind": 6, + "Flags": 32 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "s" + }] + }] + }, + "Methods": [] + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "inner" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go index 1f5f7942..5e8c18b5 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/union.go @@ -16,6 +16,14 @@ func TestUnionDecl() { int a; int b; };`, + `union OuterUnion { + int i; + float f; + union { + char c; + short s; + } inner; + };`, } test.RunTest("TestUnionDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 7adead79..443dab45 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -105,6 +105,28 @@ Type: struct Foo: }, "Tag": 0 } +Type: struct (unnamed struct at temp.h:1:1): +{ + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Methods": [] +} Type: Foo: { "Name": "Foo" @@ -116,6 +138,28 @@ Type: union Foo: }, "Tag": 1 } +Type: union (unnamed union at temp.h:1:1): +{ + "Tag": 1, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Methods": [] +} Type: Foo: { "Name": "Foo" @@ -127,6 +171,18 @@ Type: enum Foo: }, "Tag": 2 } +Type: enum (unnamed enum at temp.h:1:1): +{ + "Items": [{ + "Name": { + "Name": "x" + }, + "Value": { + "Kind": 0, + "Value": "42" + } + }] +} Type: Foo: { "Name": "Foo" @@ -138,6 +194,28 @@ Type: class Foo: }, "Tag": 3 } +Type: class (unnamed class at temp.h:1:1): +{ + "Tag": 3, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "x" + }] + }] + }, + "Methods": [] +} Type: a::b::c: { "X": { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index 789dfa3c..3d33814e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -80,21 +80,31 @@ func TestNonBuiltinTypes() { Foo`, `struct Foo {}; struct Foo`, + `struct { + int x; + }`, `union Foo {}; Foo`, `union Foo {}; union Foo`, + `union { + int x; + }`, `enum Foo {}; Foo`, `enum Foo {}; enum Foo`, + `enum { x = 42 }`, `class Foo {}; Foo`, `class Foo {}; class Foo`, + `class { + int x; + }`, `namespace a { namespace b { @@ -123,10 +133,12 @@ func TestNonBuiltinTypes() { expr := converter.ProcessType(typ) json := parse.MarshalASTExpr(expr) str := json.Print() + typstr := typ.String() - c.Printf(c.Str("Type: %s:\n"), typ.String()) + c.Printf(c.Str("Type: %s:\n"), typstr) c.Printf(c.Str("%s\n"), str) + typstr.Dispose() cjson.FreeCStr(str) json.Delete() index.Dispose() From 7d0b47c5cb6013ea0b06b2059f84363cab8e3bed Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 28 Aug 2024 14:51:36 +0800 Subject: [PATCH 50/67] llcppsigfetch:field access & static field --- chore/_xtool/llcppsigfetch/parse/cvt.go | 46 +++++++++++++------ .../cvt_test/decl_test/class_test/class.go | 6 ++- .../cvt_test/decl_test/class_test/llgo.expect | 12 +++++ .../cvt_test/decl_test/func_test/llgo.expect | 14 ++++++ .../decl_test/struct_test/llgo.expect | 28 +++++++++++ .../decl_test/typedef_test/llgo.expect | 6 +++ .../cvt_test/decl_test/union_test/llgo.expect | 18 ++++++++ .../parse/cvt_test/type_test/llgo.expect | 10 ++++ chore/_xtool/llcppsigfetch/parse/dump.go | 2 + 9 files changed, 127 insertions(+), 15 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 9ce3af3d..4ae82c0e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -437,11 +437,11 @@ type visitFieldContext struct { func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitFieldContext)(clientData) - if cursor.Kind == clang.CursorParmDecl || cursor.Kind == clang.CursorFieldDecl { + + switch cursor.Kind { + case clang.CursorParmDecl, clang.CursorFieldDecl: paramName := cursor.String() defer paramName.Dispose() - argType := ctx.converter.ProcessType(cursor.Type()) - // In C language, parameter lists do not have similar parameter grouping in Go. // func foo(a, b int) @@ -449,17 +449,36 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan // struct A { // int a, b; // }; - ctx.params.List = append(ctx.params.List, - &ast.Field{ - //todo(zzy): comment & doc - Doc: &ast.CommentGroup{}, - Comment: &ast.CommentGroup{}, - Type: argType, - Names: []*ast.Ident{ - {Name: c.GoString(paramName.CStr())}, - }, + field := &ast.Field{ + Doc: &ast.CommentGroup{}, + Comment: &ast.CommentGroup{}, + Type: ctx.converter.ProcessType(cursor.Type()), + Names: []*ast.Ident{{Name: c.GoString(paramName.CStr())}}, + } + + if cursor.Kind == clang.CursorFieldDecl { + field.Access = ast.AccessSpecifier(cursor.CXXAccessSpecifier()) + } + + ctx.params.List = append(ctx.params.List, field) + + case clang.CursorVarDecl: + if cursor.StorageClass() == clang.SCStatic { + // static member variable + fieldname := cursor.String() + defer fieldname.Dispose() + //todo(zzy): comment & doc + ctx.params.List = append(ctx.params.List, &ast.Field{ + Doc: &ast.CommentGroup{}, + Comment: &ast.CommentGroup{}, + Type: ctx.converter.ProcessType(cursor.Type()), + Access: ast.AccessSpecifier(cursor.CXXAccessSpecifier()), + IsStatic: true, + Names: []*ast.Ident{{Name: c.GoString(fieldname.CStr())}}, }) + } } + return clang.ChildVisit_Continue } @@ -486,7 +505,7 @@ type visitMethodsContext struct { func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitMethodsContext)(clientData) - if isMethod(cursor) && cursor.CXXAccessSpecifier() != clang.CXXPrivate { + if isMethod(cursor) && cursor.CXXAccessSpecifier() == clang.CXXPublic { method := ctx.converter.ProcessFuncDecl(cursor) if method != nil { *ctx.methods = append(*ctx.methods, method) @@ -495,6 +514,7 @@ func visitMethods(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang. return clang.ChildVisit_Continue } +// Note:Public Method is considered func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { methods := make([]*ast.FuncDecl, 0) ctx := &visitMethodsContext{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go index 3997016b..fd2d3847 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/class.go @@ -15,11 +15,13 @@ func TestClassDecl() { };`, `class A { public: - int a; + static int a; int b; float foo(int a,double b); private: - void bar(); + static void bar(); + protected: + void bar2(); };`, `class A { public: diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index a613664f..f7435e0b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -27,6 +27,8 @@ TestClassDecl Case 1: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -41,6 +43,8 @@ TestClassDecl Case 1: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "b" }] @@ -82,6 +86,8 @@ TestClassDecl Case 2: "Comment": { "List": [] }, + "IsStatic": true, + "Access": 1, "Names": [{ "Name": "a" }] @@ -96,6 +102,8 @@ TestClassDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "b" }] @@ -127,6 +135,8 @@ TestClassDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "a" }] @@ -141,6 +151,8 @@ TestClassDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "b" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 227f446c..454819d7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -63,6 +63,8 @@ TestFuncDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "a" }] @@ -108,6 +110,8 @@ TestFuncDecl Case 3: }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }, { "Type": { @@ -120,6 +124,8 @@ TestFuncDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "a" }] @@ -171,6 +177,8 @@ TestFuncDecl Case 4: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "a" }] @@ -185,6 +193,8 @@ TestFuncDecl Case 4: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "b" }] @@ -238,6 +248,8 @@ TestFuncDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "a" }] @@ -252,6 +264,8 @@ TestFuncDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 0, "Names": [{ "Name": "b" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index f3021595..ec15acd8 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -25,6 +25,8 @@ TestStructDecl Case 1: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -66,6 +68,8 @@ TestStructDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -80,6 +84,8 @@ TestStructDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "b" }] @@ -121,6 +127,8 @@ TestStructDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -135,6 +143,8 @@ TestStructDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "b" }] @@ -176,6 +186,8 @@ TestStructDecl Case 4: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -190,6 +202,8 @@ TestStructDecl Case 4: }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }, { "Type": { @@ -198,6 +212,8 @@ TestStructDecl Case 4: }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }] }, @@ -213,6 +229,8 @@ TestStructDecl Case 4: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "Foo" }] @@ -254,6 +272,8 @@ TestStructDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "age" }] @@ -272,6 +292,8 @@ TestStructDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "year" }] @@ -286,6 +308,8 @@ TestStructDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "day" }] @@ -300,6 +324,8 @@ TestStructDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "month" }] @@ -313,6 +339,8 @@ TestStructDecl Case 5: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "birthday" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index d88c973a..5e32efa0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -85,6 +85,8 @@ TestTypeDefDecl Case 3: }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }, { "Type": { @@ -93,12 +95,16 @@ TestTypeDefDecl Case 3: }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }, { "Type": { }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }] }, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect index 07db788f..cc620177 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect @@ -25,6 +25,8 @@ TestUnionDecl Case 1: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -39,6 +41,8 @@ TestUnionDecl Case 1: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "b" }] @@ -80,6 +84,8 @@ TestUnionDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "a" }] @@ -94,6 +100,8 @@ TestUnionDecl Case 2: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "b" }] @@ -135,6 +143,8 @@ TestUnionDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "i" }] @@ -149,6 +159,8 @@ TestUnionDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "f" }] @@ -167,6 +179,8 @@ TestUnionDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "c" }] @@ -181,6 +195,8 @@ TestUnionDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "s" }] @@ -194,6 +210,8 @@ TestUnionDecl Case 3: "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "inner" }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 443dab45..3afa06d7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -120,6 +120,8 @@ Type: struct (unnamed struct at temp.h:1:1): "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "x" }] @@ -153,6 +155,8 @@ Type: union (unnamed union at temp.h:1:1): "Comment": { "List": [] }, + "IsStatic": false, + "Access": 1, "Names": [{ "Name": "x" }] @@ -209,6 +213,8 @@ Type: class (unnamed class at temp.h:1:1): "Comment": { "List": [] }, + "IsStatic": false, + "Access": 3, "Names": [{ "Name": "x" }] @@ -258,6 +264,8 @@ Type: int (*)(int, char): }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }, { "Type": { @@ -266,6 +274,8 @@ Type: int (*)(int, char): }, "Doc": null, "Comment": null, + "IsStatic": false, + "Access": 0, "Names": [] }] }, diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 5cc02bb0..c3e0a187 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -143,6 +143,8 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) root.SetItem(c.Str("Doc"), MarshalASTExpr(d.Doc)) root.SetItem(c.Str("Comment"), MarshalASTExpr(d.Comment)) + root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic)) + root.SetItem(c.Str("Access"), numberField(uint(d.Access))) names := cjson.Array() for _, n := range d.Names { names.AddItem(MarshalASTExpr(n)) From 021ddefb10e0521a268efbc0efa647ccac57691c Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 28 Aug 2024 18:50:05 +0800 Subject: [PATCH 51/67] llcppsigfetch:dump node type for unmarshal --- .../cvt_test/decl_test/class_test/llgo.expect | 170 ++++++++++++++++++ .../decl_test/comment_test/llgo.expect | 85 +++++++++ .../cvt_test/decl_test/enum_test/llgo.expect | 45 +++++ .../cvt_test/decl_test/func_test/llgo.expect | 73 ++++++++ .../cvt_test/decl_test/scope_test/llgo.expect | 61 +++++++ .../decl_test/struct_test/llgo.expect | 102 +++++++++++ .../decl_test/typedef_test/llgo.expect | 32 ++++ .../cvt_test/decl_test/union_test/llgo.expect | 66 +++++++ .../cvt_test/preprocess_test/llgo.expect | 24 +++ .../parse/cvt_test/type_test/llgo.expect | 76 ++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 28 ++- 11 files changed, 761 insertions(+), 1 deletion(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index f7435e0b..bf967035 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -2,50 +2,67 @@ TestClassDecl Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] @@ -61,104 +78,139 @@ TestClassDecl Case 1: TestClassDecl Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": true, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] }, "Methods": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "A" }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 8, "Flags": 16 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 8, "Flags": 0 } @@ -182,40 +234,55 @@ TestClassDecl Case 2: TestClassDecl Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [] }, "Methods": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "A" }, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -229,23 +296,31 @@ TestClassDecl Case 3: "IsVirtual": false, "IsOverride": false }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "A" }, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -259,23 +334,31 @@ TestClassDecl Case 3: "IsVirtual": false, "IsOverride": false }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "A" }, "Name": { + "_Type": "Ident", "Name": "~A" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -289,23 +372,31 @@ TestClassDecl Case 3: "IsVirtual": false, "IsOverride": false }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "A" }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -329,40 +420,55 @@ TestClassDecl Case 3: TestClassDecl Case 4: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Base" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [] }, "Methods": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "Base" }, "Name": { + "_Type": "Ident", "Name": "Base" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -376,23 +482,31 @@ TestClassDecl Case 4: "IsVirtual": false, "IsOverride": false }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "Base" }, "Name": { + "_Type": "Ident", "Name": "~Base" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -406,23 +520,31 @@ TestClassDecl Case 4: "IsVirtual": true, "IsOverride": false }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "Base" }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -438,39 +560,53 @@ TestClassDecl Case 4: }] } }, { + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Derived" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [] }, "Methods": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "Derived" }, "Name": { + "_Type": "Ident", "Name": "Derived" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -484,23 +620,31 @@ TestClassDecl Case 4: "IsVirtual": false, "IsOverride": false }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "Derived" }, "Name": { + "_Type": "Ident", "Name": "~Derived" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -514,23 +658,31 @@ TestClassDecl Case 4: "IsVirtual": true, "IsOverride": true }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "Derived" }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -554,49 +706,67 @@ TestClassDecl Case 4: TestClassDecl Case 5: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "A" }, "Name": { + "_Type": "Ident", "Name": "Foo" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [] }, "Methods": [] } }, { + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "Foo" }, "Parent": { + "_Type": "Ident", "Name": "A" } }, "Name": { + "_Type": "Ident", "Name": "bar" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect index 1ba87aff..8b1e0216 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -2,22 +2,30 @@ TestDoc Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -39,22 +47,30 @@ TestDoc Case 1: TestDoc Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -76,24 +92,33 @@ TestDoc Case 2: TestDoc Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/// doc" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -115,24 +140,33 @@ TestDoc Case 3: TestDoc Case 4: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/** doc */" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -154,24 +188,33 @@ TestDoc Case 4: TestDoc Case 5: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/*! doc */" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -193,26 +236,36 @@ TestDoc Case 5: TestDoc Case 6: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/// doc 1" }, { + "_Type": "Comment", "Text": "/// doc 2" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -234,26 +287,36 @@ TestDoc Case 6: TestDoc Case 7: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/*! doc 1 */" }, { + "_Type": "Comment", "Text": "/*! doc 2 */" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -275,26 +338,36 @@ TestDoc Case 7: TestDoc Case 8: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/** doc 1 */" }, { + "_Type": "Comment", "Text": "/** doc 1 */" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -316,30 +389,42 @@ TestDoc Case 8: TestDoc Case 9: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [{ + "_Type": "Comment", "Text": "/**" }, { + "_Type": "Comment", "Text": " * doc 1" }, { + "_Type": "Comment", "Text": " * doc 2" }, { + "_Type": "Comment", "Text": " */" }] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect index 55ae0666..9ce9dd51 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect @@ -2,39 +2,54 @@ TestEnumDecl Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "EnumTypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Foo" }, "Type": { + "_Type": "EnumType", "Items": [{ + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "a" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "0" } }, { + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "b" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "1" } }, { + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "c" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "2" } @@ -49,39 +64,54 @@ TestEnumDecl Case 1: TestEnumDecl Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "EnumTypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Foo" }, "Type": { + "_Type": "EnumType", "Items": [{ + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "a" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "1" } }, { + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "b" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "2" } }, { + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "c" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "4" } @@ -96,39 +126,54 @@ TestEnumDecl Case 2: TestEnumDecl Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "EnumTypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Foo" }, "Type": { + "_Type": "EnumType", "Items": [{ + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "a" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "1" } }, { + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "b" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "2" } }, { + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "c" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "3" } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 454819d7..27471a41 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -2,22 +2,30 @@ TestFuncDecl Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -39,38 +47,51 @@ TestFuncDecl Case 1: TestFuncDecl Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "a" }] }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -92,21 +113,30 @@ TestFuncDecl Case 2: TestFuncDecl Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "Variadic" }, "Doc": null, "Comment": null, @@ -114,24 +144,30 @@ TestFuncDecl Case 3: "Access": 0, "Names": [] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "a" }] }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -153,55 +189,74 @@ TestFuncDecl Case 3: TestFuncDecl Case 4: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 8, "Flags": 16 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] }, "Ret": { + "_Type": "PointerType", "X": { + "_Type": "BuiltinType", "Kind": 8, "Flags": 0 } @@ -224,54 +279,72 @@ TestFuncDecl Case 4: TestFuncDecl Case 5: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "add" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 0, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index 91e06267..ae68923b 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -2,22 +2,30 @@ TestScope Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -39,24 +47,33 @@ TestScope Case 1: TestScope Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "a" }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -78,29 +95,40 @@ TestScope Case 2: TestScope Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "b" }, "Parent": { + "_Type": "Ident", "Name": "a" } }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -122,40 +150,55 @@ TestScope Case 3: TestScope Case 4: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "a" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [] }, "Methods": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "a" }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } @@ -179,47 +222,65 @@ TestScope Case 4: TestScope Case 5: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "Ident", "Name": "a" }, "Name": { + "_Type": "Ident", "Name": "b" }, "Type": { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [] }, "Methods": [{ + "_Type": "FuncDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "b" }, "Parent": { + "_Type": "Ident", "Name": "a" } }, "Name": { + "_Type": "Ident", "Name": "foo" }, "Type": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [] }, "Ret": { + "_Type": "BuiltinType", "Kind": 0, "Flags": 0 } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index ec15acd8..9c724cd1 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -2,32 +2,43 @@ TestStructDecl Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": null, "Type": { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }] @@ -43,50 +54,67 @@ TestStructDecl Case 1: TestStructDecl Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] @@ -102,50 +130,67 @@ TestStructDecl Case 2: TestStructDecl Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] @@ -161,42 +206,60 @@ TestStructDecl Case 3: TestStructDecl Case 4: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "PointerType", "X": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, @@ -206,7 +269,9 @@ TestStructDecl Case 4: "Access": 0, "Names": [] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, @@ -218,20 +283,24 @@ TestStructDecl Case 4: }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } } }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "Foo" }] }] @@ -247,86 +316,116 @@ TestStructDecl Case 4: TestStructDecl Case 5: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Person" }, "Type": { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "age" }] }, { + "_Type": "Field", "Type": { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "year" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "day" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "month" }] }] @@ -334,14 +433,17 @@ TestStructDecl Case 5: "Methods": [] }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "birthday" }] }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 5e32efa0..3a769e03 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -2,18 +2,24 @@ TestTypeDefDecl Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypedefDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "INT" }, "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } @@ -26,33 +32,44 @@ TestTypeDefDecl Case 1: TestTypeDefDecl Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypedefDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "INT" }, "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } }, { + "_Type": "TypedefDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "STANDARD_INT" }, "Type": { + "_Type": "Ident", "Name": "INT" } }], @@ -64,22 +81,32 @@ TestTypeDefDecl Case 2: TestTypeDefDecl Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypedefDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "Foo" }, "Type": { + "_Type": "PointerType", "X": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, @@ -89,7 +116,9 @@ TestTypeDefDecl Case 3: "Access": 0, "Names": [] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, @@ -99,7 +128,9 @@ TestTypeDefDecl Case 3: "Access": 0, "Names": [] }, { + "_Type": "Field", "Type": { + "_Type": "Variadic" }, "Doc": null, "Comment": null, @@ -109,6 +140,7 @@ TestTypeDefDecl Case 3: }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect index cc620177..798fb796 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect @@ -2,48 +2,64 @@ TestUnionDecl Case 1: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": null, "Type": { + "_Type": "RecordType", "Tag": 1, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] @@ -59,50 +75,67 @@ TestUnionDecl Case 1: TestUnionDecl Case 2: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "A" }, "Type": { + "_Type": "RecordType", "Tag": 1, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "a" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "b" }] }] @@ -118,86 +151,116 @@ TestUnionDecl Case 2: TestUnionDecl Case 3: { "temp.h": { + "_Type": "File", "decls": [{ + "_Type": "TypeDecl", "Loc": { + "_Type": "Location", "File": "temp.h" }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Parent": null, "Name": { + "_Type": "Ident", "Name": "OuterUnion" }, "Type": { + "_Type": "RecordType", "Tag": 1, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "i" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 8, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "f" }] }, { + "_Type": "Field", "Type": { + "_Type": "RecordType", "Tag": 1, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "c" }] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 32 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "s" }] }] @@ -205,14 +268,17 @@ TestUnionDecl Case 3: "Methods": [] }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "inner" }] }] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect index 9e9f5b3d..ac0d4605 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/preprocess_test/llgo.expect @@ -2,11 +2,14 @@ TestDefine Case 1: { "temp.h": { + "_Type": "File", "decls": [], "includes": [], "macros": [{ + "_Type": "Macro", "Name": "DEBUG", "Tokens": [{ + "_Type": "Token", "Token": 3, "Lit": "DEBUG" }] @@ -17,14 +20,18 @@ TestDefine Case 1: TestDefine Case 2: { "temp.h": { + "_Type": "File", "decls": [], "includes": [], "macros": [{ + "_Type": "Macro", "Name": "OK", "Tokens": [{ + "_Type": "Token", "Token": 3, "Lit": "OK" }, { + "_Type": "Token", "Token": 4, "Lit": "1" }] @@ -35,47 +42,62 @@ TestDefine Case 2: TestDefine Case 3: { "temp.h": { + "_Type": "File", "decls": [], "includes": [], "macros": [{ + "_Type": "Macro", "Name": "SQUARE", "Tokens": [{ + "_Type": "Token", "Token": 3, "Lit": "SQUARE" }, { + "_Type": "Token", "Token": 1, "Lit": "(" }, { + "_Type": "Token", "Token": 3, "Lit": "x" }, { + "_Type": "Token", "Token": 1, "Lit": ")" }, { + "_Type": "Token", "Token": 1, "Lit": "(" }, { + "_Type": "Token", "Token": 1, "Lit": "(" }, { + "_Type": "Token", "Token": 3, "Lit": "x" }, { + "_Type": "Token", "Token": 1, "Lit": ")" }, { + "_Type": "Token", "Token": 1, "Lit": "*" }, { + "_Type": "Token", "Token": 1, "Lit": "(" }, { + "_Type": "Token", "Token": 3, "Lit": "x" }, { + "_Type": "Token", "Token": 1, "Lit": ")" }, { + "_Type": "Token", "Token": 1, "Lit": ")" }] @@ -86,8 +108,10 @@ TestDefine Case 3: TestInclude Case 1: { "temp.h": { + "_Type": "File", "decls": [], "includes": [{ + "_Type": "Include", "Path": "foo.h" }], "macros": [] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 3afa06d7..a7445c9a 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -28,16 +28,22 @@ Complex:flags:20 kind:11 Unknown:flags:0 kind:0 Type: char *: { + "_Type": "PointerType", "X": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 } } Type: char ***: { + "_Type": "PointerType", "X": { + "_Type": "PointerType", "X": { + "_Type": "PointerType", "X": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 } @@ -46,7 +52,9 @@ Type: char ***: } Type: char[]: { + "_Type": "ArrayType", "Elt": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 }, @@ -54,75 +62,97 @@ Type: char[]: } Type: char[10]: { + "_Type": "ArrayType", "Elt": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 }, "Len": { + "_Type": "BasicLit", "Kind": 0, "Value": "10" } } Type: char[3][4]: { + "_Type": "ArrayType", "Elt": { + "_Type": "ArrayType", "Elt": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 }, "Len": { + "_Type": "BasicLit", "Kind": 0, "Value": "4" } }, "Len": { + "_Type": "BasicLit", "Kind": 0, "Value": "3" } } Type: int &: { + "_Type": "LvalueRefType", "X": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } } Type: int &&: { + "_Type": "LvalueRefType", "X": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } } Type: Foo: { + "_Type": "Ident", "Name": "Foo" } Type: struct Foo: { + "_Type": "TagExpr", "Name": { + "_Type": "Ident", "Name": "Foo" }, "Tag": 0 } Type: struct (unnamed struct at temp.h:1:1): { + "_Type": "RecordType", "Tag": 0, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "x" }] }] @@ -131,33 +161,43 @@ Type: struct (unnamed struct at temp.h:1:1): } Type: Foo: { + "_Type": "Ident", "Name": "Foo" } Type: union Foo: { + "_Type": "TagExpr", "Name": { + "_Type": "Ident", "Name": "Foo" }, "Tag": 1 } Type: union (unnamed union at temp.h:1:1): { + "_Type": "RecordType", "Tag": 1, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 1, "Names": [{ + "_Type": "Ident", "Name": "x" }] }] @@ -166,22 +206,29 @@ Type: union (unnamed union at temp.h:1:1): } Type: Foo: { + "_Type": "Ident", "Name": "Foo" } Type: enum Foo: { + "_Type": "TagExpr", "Name": { + "_Type": "Ident", "Name": "Foo" }, "Tag": 2 } Type: enum (unnamed enum at temp.h:1:1): { + "_Type": "EnumType", "Items": [{ + "_Type": "EnumItem", "Name": { + "_Type": "Ident", "Name": "x" }, "Value": { + "_Type": "BasicLit", "Kind": 0, "Value": "42" } @@ -189,33 +236,43 @@ Type: enum (unnamed enum at temp.h:1:1): } Type: Foo: { + "_Type": "Ident", "Name": "Foo" } Type: class Foo: { + "_Type": "TagExpr", "Name": { + "_Type": "Ident", "Name": "Foo" }, "Tag": 3 } Type: class (unnamed class at temp.h:1:1): { + "_Type": "RecordType", "Tag": 3, "Fields": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, "Doc": { + "_Type": "CommentGroup", "List": [] }, "Comment": { + "_Type": "CommentGroup", "List": [] }, "IsStatic": false, "Access": 3, "Names": [{ + "_Type": "Ident", "Name": "x" }] }] @@ -224,29 +281,40 @@ Type: class (unnamed class at temp.h:1:1): } Type: a::b::c: { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "c" }, "Parent": { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "b" }, "Parent": { + "_Type": "Ident", "Name": "a" } } } Type: class a::b::c: { + "_Type": "TagExpr", "Name": { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "c" }, "Parent": { + "_Type": "ScopingExpr", "X": { + "_Type": "Ident", "Name": "b" }, "Parent": { + "_Type": "Ident", "Name": "a" } } @@ -255,10 +323,15 @@ Type: class a::b::c: } Type: int (*)(int, char): { + "_Type": "PointerType", "X": { + "_Type": "FuncType", "Params": { + "_Type": "FieldList", "List": [{ + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 }, @@ -268,7 +341,9 @@ Type: int (*)(int, char): "Access": 0, "Names": [] }, { + "_Type": "Field", "Type": { + "_Type": "BuiltinType", "Kind": 2, "Flags": 1 }, @@ -280,6 +355,7 @@ Type: int (*)(int, char): }] }, "Ret": { + "_Type": "BuiltinType", "Kind": 6, "Flags": 0 } diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index c3e0a187..0b771fb7 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -21,7 +21,7 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { for _, decl := range file.Decls { decls.AddItem(MarshalASTDecl(decl)) } - + root.SetItem(c.Str("_Type"), stringField("File")) root.SetItem(c.Str("decls"), decls) // json:includes,omitempty @@ -29,6 +29,7 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { includes := cjson.Array() for _, i := range file.Includes { include := cjson.Object() + include.SetItem(c.Str("_Type"), stringField("Include")) include.SetItem(c.Str("Path"), stringField(i.Path)) includes.AddItem(include) } @@ -40,6 +41,7 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { macros := cjson.Array() for _, m := range file.Macros { marco := cjson.Object() + marco.SetItem(c.Str("_Type"), stringField("Macro")) marco.SetItem(c.Str("Name"), stringField(m.Name)) tokens := cjson.Array() for _, tok := range m.Tokens { @@ -54,6 +56,7 @@ func MarshalASTFile(file *ast.File) *cjson.JSON { } func Token(tok *ast.Token) *cjson.JSON { root := cjson.Object() + root.SetItem(c.Str("_Type"), stringField("Token")) root.SetItem(c.Str("Token"), numberField(uint(tok.Token))) root.SetItem(c.Str("Lit"), stringField(tok.Lit)) return root @@ -66,14 +69,17 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { root := cjson.Object() switch d := decl.(type) { case *ast.EnumTypeDecl: + root.SetItem(c.Str("_Type"), stringField("EnumTypeDecl")) MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) case *ast.TypedefDecl: + root.SetItem(c.Str("_Type"), stringField("TypedefDecl")) MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) case *ast.FuncDecl: + root.SetItem(c.Str("_Type"), stringField("FuncDecl")) MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) @@ -86,6 +92,7 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { root.SetItem(c.Str("IsVirtual"), boolField(d.IsVirtual)) root.SetItem(c.Str("IsOverride"), boolField(d.IsOverride)) case *ast.TypeDecl: + root.SetItem(c.Str("_Type"), stringField("TypeDecl")) MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) @@ -95,6 +102,7 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { func MarshalASTDeclBase(decl ast.DeclBase, root *cjson.JSON) { loc := cjson.Object() + loc.SetItem(c.Str("_Type"), stringField("Location")) loc.SetItem(c.Str("File"), stringField(decl.Loc.File)) root.SetItem(c.Str("Loc"), loc) @@ -111,15 +119,18 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { switch d := t.(type) { case *ast.EnumType: + root.SetItem(c.Str("_Type"), stringField("EnumType")) items := cjson.Array() for _, e := range d.Items { items.AddItem(MarshalASTExpr(e)) } root.SetItem(c.Str("Items"), items) case *ast.EnumItem: + root.SetItem(c.Str("_Type"), stringField("EnumItem")) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value)) case *ast.RecordType: + root.SetItem(c.Str("_Type"), stringField("RecordType")) root.SetItem(c.Str("Tag"), numberField(uint(d.Tag))) root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields)) methods := cjson.Array() @@ -128,9 +139,11 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } root.SetItem(c.Str("Methods"), methods) case *ast.FuncType: + root.SetItem(c.Str("_Type"), stringField("FuncType")) root.SetItem(c.Str("Params"), MarshalASTExpr(d.Params)) root.SetItem(c.Str("Ret"), MarshalASTExpr(d.Ret)) case *ast.FieldList: + root.SetItem(c.Str("_Type"), stringField("FieldList")) if d == nil { return cjson.Null() } @@ -140,6 +153,7 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } root.SetItem(c.Str("List"), list) case *ast.Field: + root.SetItem(c.Str("_Type"), stringField("Field")) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) root.SetItem(c.Str("Doc"), MarshalASTExpr(d.Doc)) root.SetItem(c.Str("Comment"), MarshalASTExpr(d.Comment)) @@ -151,35 +165,46 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } root.SetItem(c.Str("Names"), names) case *ast.Variadic: + root.SetItem(c.Str("_Type"), stringField("Variadic")) case *ast.Ident: + root.SetItem(c.Str("_Type"), stringField("Ident")) if d == nil { return cjson.Null() } root.SetItem(c.Str("Name"), stringField(d.Name)) case *ast.TagExpr: + root.SetItem(c.Str("_Type"), stringField("TagExpr")) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) root.SetItem(c.Str("Tag"), numberField(uint(d.Tag))) case *ast.BasicLit: + root.SetItem(c.Str("_Type"), stringField("BasicLit")) root.SetItem(c.Str("Kind"), numberField(uint(d.Kind))) root.SetItem(c.Str("Value"), stringField(d.Value)) case *ast.LvalueRefType: + root.SetItem(c.Str("_Type"), stringField("LvalueRefType")) root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.RvalueRefType: + root.SetItem(c.Str("_Type"), stringField("RvalueRefType")) root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.PointerType: + root.SetItem(c.Str("_Type"), stringField("PointerType")) root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) case *ast.ArrayType: + root.SetItem(c.Str("_Type"), stringField("ArrayType")) root.SetItem(c.Str("Elt"), MarshalASTExpr(d.Elt)) root.SetItem(c.Str("Len"), MarshalASTExpr(d.Len)) case *ast.BuiltinType: + root.SetItem(c.Str("_Type"), stringField("BuiltinType")) root.SetItem(c.Str("Kind"), numberField(uint(d.Kind))) root.SetItem(c.Str("Flags"), numberField(uint(d.Flags))) case *ast.Comment: + root.SetItem(c.Str("_Type"), stringField("Comment")) if d == nil { return cjson.Null() } root.SetItem(c.Str("Text"), stringField(d.Text)) case *ast.CommentGroup: + root.SetItem(c.Str("_Type"), stringField("CommentGroup")) if d == nil { return cjson.Null() } @@ -189,6 +214,7 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { } root.SetItem(c.Str("List"), list) case *ast.ScopingExpr: + root.SetItem(c.Str("_Type"), stringField("ScopingExpr")) root.SetItem(c.Str("X"), MarshalASTExpr(d.X)) root.SetItem(c.Str("Parent"), MarshalASTExpr(d.Parent)) default: From 2842a109da0d6ca9d2858b30e09f270d9234d3de Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 29 Aug 2024 11:31:42 +0800 Subject: [PATCH 52/67] llcppsigfetch:right reference --- chore/_xtool/llcppsigfetch/parse/cvt.go | 4 +++- .../_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 4ae82c0e..bb7d9edc 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -213,8 +213,10 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { switch t.Kind { case clang.TypePointer: expr = &ast.PointerType{X: ct.ProcessType(t.PointeeType())} - case clang.TypeLValueReference, clang.TypeRValueReference: + case clang.TypeLValueReference: expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} + case clang.TypeRValueReference: + expr = &ast.RvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeFunctionProto: // function type will only collect return type, params will be collected in ProcessFuncDecl expr = ct.ProcessFunctionType(t) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index a7445c9a..1ba26bf6 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -107,7 +107,7 @@ Type: int &: } Type: int &&: { - "_Type": "LvalueRefType", + "_Type": "RvalueRefType", "X": { "_Type": "BuiltinType", "Kind": 6, From da6706cb9307c34bfe142fd2ce9694afd828fafb Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 30 Aug 2024 18:55:02 +0800 Subject: [PATCH 53/67] llcppsigfetch:complex typedef decl --- .../decl_test/typedef_test/llgo.expect | 365 ++++++++++++++++++ .../decl_test/typedef_test/typedef.go | 10 + 2 files changed, 375 insertions(+) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 3a769e03..07225285 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -79,6 +79,83 @@ TestTypeDefDecl Case 2: } TestTypeDefDecl Case 3: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "INT" + }, + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "IntPtr" + }, + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + } + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "IntArr" + }, + "Type": { + "_Type": "ArrayType", + "Elt": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Len": null + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 4: { "temp.h": { "_Type": "File", @@ -152,6 +229,294 @@ TestTypeDefDecl Case 3: } } +TestTypeDefDecl Case 5: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "Foo" + }, + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "FuncType", + "Params": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 0, + "Names": [] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 0, + "Names": [] + }] + }, + "Ret": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + } + } + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "Bar" + }, + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "FuncType", + "Params": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "BuiltinType", + "Kind": 0, + "Flags": 0 + } + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 0, + "Names": [] + }, { + "_Type": "Field", + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "BuiltinType", + "Kind": 0, + "Flags": 0 + } + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 0, + "Names": [] + }] + }, + "Ret": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 6: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + }, + "Name": { + "_Type": "Ident", + "Name": "Foo" + }, + "Type": { + "_Type": "RecordType", + "Tag": 3, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Comment": { + "_Type": "CommentGroup", + "List": [] + }, + "IsStatic": false, + "Access": 3, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] + }, + "Methods": [] + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + }, + "Name": { + "_Type": "Ident", + "Name": "MyClass" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "Foo" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Tag": 3 + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + }, + "Name": { + "_Type": "Ident", + "Name": "MyClassPtr" + }, + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "Foo" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Tag": 3 + } + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [] + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + }, + "Name": { + "_Type": "Ident", + "Name": "MyClassArray" + }, + "Type": { + "_Type": "ArrayType", + "Elt": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "Foo" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Tag": 3 + }, + "Len": null + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 240fc660..27dd0f19 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -13,7 +13,17 @@ func TestTypeDefDecl() { `typedef int INT; typedef INT STANDARD_INT;`, + `typedef int INT,*IntPtr,IntArr[];`, + `typedef int (*Foo)(int, int, ...);`, + + `typedef int (*Foo)(int, int),(*Bar)(void*,void*);`, + + `namespace A { + typedef class Foo{ + int x; + } MyClass,*MyClassPtr,MyClassArray[]; + }`, } test.RunTest("TestTypeDefDecl", testCases) } From 86b50b0a930cf2354ded1b7c96b2352925e4ba7d Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 2 Sep 2024 15:47:05 +0800 Subject: [PATCH 54/67] llcppsigfetch:null processing --- chore/_xtool/llcppsigfetch/parse/cvt.go | 41 ++-- .../cvt_test/decl_test/class_test/llgo.expect | 180 +++++------------- .../decl_test/comment_test/llgo.expect | 28 ++- .../cvt_test/decl_test/enum_test/llgo.expect | 15 +- .../cvt_test/decl_test/func_test/llgo.expect | 89 ++------- .../cvt_test/decl_test/scope_test/llgo.expect | 49 ++--- .../decl_test/struct_test/llgo.expect | 149 +++------------ .../decl_test/typedef_test/llgo.expect | 89 +++------ .../cvt_test/decl_test/union_test/llgo.expect | 105 ++-------- .../parse/cvt_test/type_test/llgo.expect | 34 +--- chore/_xtool/llcppsigfetch/parse/dump.go | 132 ++++++++----- 11 files changed, 278 insertions(+), 633 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index bb7d9edc..675633c5 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -129,17 +129,20 @@ func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { rawComment := cursor.RawCommentText() defer rawComment.Dispose() + res := ast.DeclBase{ + Loc: &ct.curLoc, + Parent: buildScopingExpr(cursor.SemanticParent()), + } + commentGroup := &ast.CommentGroup{} if rawComment.CStr() != nil { commentGroup = ct.ParseComment(c.GoString(rawComment.CStr())) + if len(commentGroup.List) > 0 { + res.Doc = commentGroup + } } - loc := ct.curLoc - return ast.DeclBase{ - Loc: &loc, - Parent: buildScopingExpr(cursor.SemanticParent()), - Doc: commentGroup, - } + return res } func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { @@ -248,7 +251,7 @@ func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { // This would return CursorNoDeclFound ret := ct.ProcessType(t.ResultType()) - params := &ast.FieldList{List: make([]*ast.Field, 0)} + params := &ast.FieldList{} numArgs := t.NumArgTypes() for i := 0; i < int(numArgs); i++ { argType := t.ArgType(c.Uint(i)) @@ -256,7 +259,6 @@ func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { Type: ct.ProcessType(argType), }) } - if t.IsFunctionTypeVariadic() != 0 { params.List = append(params.List, &ast.Field{ Type: &ast.Variadic{}, @@ -452,10 +454,12 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan // int a, b; // }; field := &ast.Field{ - Doc: &ast.CommentGroup{}, - Comment: &ast.CommentGroup{}, - Type: ctx.converter.ProcessType(cursor.Type()), - Names: []*ast.Ident{{Name: c.GoString(paramName.CStr())}}, + // todo(zzy):comment & doc + Type: ctx.converter.ProcessType(cursor.Type()), + } + + if paramName.CStr() != nil { + field.Names = []*ast.Ident{{Name: c.GoString(paramName.CStr())}} } if cursor.Kind == clang.CursorFieldDecl { @@ -470,14 +474,15 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan fieldname := cursor.String() defer fieldname.Dispose() //todo(zzy): comment & doc - ctx.params.List = append(ctx.params.List, &ast.Field{ - Doc: &ast.CommentGroup{}, - Comment: &ast.CommentGroup{}, + field := &ast.Field{ Type: ctx.converter.ProcessType(cursor.Type()), Access: ast.AccessSpecifier(cursor.CXXAccessSpecifier()), IsStatic: true, - Names: []*ast.Ident{{Name: c.GoString(fieldname.CStr())}}, - }) + } + if fieldname.CStr() != nil && c.GoString(fieldname.CStr()) != "" { + field.Names = []*ast.Ident{{Name: c.GoString(fieldname.CStr())}} + } + ctx.params.List = append(ctx.params.List, field) } } @@ -486,7 +491,7 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan // For Record Type(struct,union ...) & Func 's FieldList func (ct *Converter) ProcessFieldList(cursor clang.Cursor) *ast.FieldList { - params := &ast.FieldList{List: []*ast.Field{}} + params := &ast.FieldList{} ctx := &visitFieldContext{ params: params, converter: ct, diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index bf967035..2b13b4e1 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -9,10 +9,7 @@ TestClassDecl Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -30,14 +27,8 @@ TestClassDecl Case 1: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -51,14 +42,8 @@ TestClassDecl Case 1: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -85,10 +70,7 @@ TestClassDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -106,14 +88,8 @@ TestClassDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": true, "Access": 1, "Names": [{ @@ -127,14 +103,8 @@ TestClassDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -149,10 +119,7 @@ TestClassDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -172,14 +139,8 @@ TestClassDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -193,14 +154,8 @@ TestClassDecl Case 2: "Kind": 8, "Flags": 16 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -241,10 +196,7 @@ TestClassDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -255,7 +207,7 @@ TestClassDecl Case 3: "Tag": 3, "Fields": { "_Type": "FieldList", - "List": [] + "List": null }, "Methods": [{ "_Type": "FuncDecl", @@ -263,10 +215,7 @@ TestClassDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -279,7 +228,7 @@ TestClassDecl Case 3: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -301,10 +250,7 @@ TestClassDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -317,7 +263,7 @@ TestClassDecl Case 3: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -339,10 +285,7 @@ TestClassDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -355,7 +298,7 @@ TestClassDecl Case 3: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -377,10 +320,7 @@ TestClassDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -393,7 +333,7 @@ TestClassDecl Case 3: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -427,10 +367,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -441,7 +378,7 @@ TestClassDecl Case 4: "Tag": 3, "Fields": { "_Type": "FieldList", - "List": [] + "List": null }, "Methods": [{ "_Type": "FuncDecl", @@ -449,10 +386,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "Base" @@ -465,7 +399,7 @@ TestClassDecl Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -487,10 +421,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "Base" @@ -503,7 +434,7 @@ TestClassDecl Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -525,10 +456,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "Base" @@ -541,7 +469,7 @@ TestClassDecl Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -565,10 +493,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -579,7 +504,7 @@ TestClassDecl Case 4: "Tag": 3, "Fields": { "_Type": "FieldList", - "List": [] + "List": null }, "Methods": [{ "_Type": "FuncDecl", @@ -587,10 +512,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "Derived" @@ -603,7 +525,7 @@ TestClassDecl Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -625,10 +547,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "Derived" @@ -641,7 +560,7 @@ TestClassDecl Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -663,10 +582,7 @@ TestClassDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "Derived" @@ -679,7 +595,7 @@ TestClassDecl Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -713,10 +629,7 @@ TestClassDecl Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -730,7 +643,7 @@ TestClassDecl Case 5: "Tag": 3, "Fields": { "_Type": "FieldList", - "List": [] + "List": null }, "Methods": [] } @@ -740,10 +653,7 @@ TestClassDecl Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "ScopingExpr", "X": { @@ -763,7 +673,7 @@ TestClassDecl Case 5: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect index 8b1e0216..a07cf51f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -9,10 +9,7 @@ TestDoc Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -22,7 +19,7 @@ TestDoc Case 1: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -54,10 +51,7 @@ TestDoc Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -67,7 +61,7 @@ TestDoc Case 2: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -115,7 +109,7 @@ TestDoc Case 3: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -163,7 +157,7 @@ TestDoc Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -211,7 +205,7 @@ TestDoc Case 5: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -262,7 +256,7 @@ TestDoc Case 6: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -313,7 +307,7 @@ TestDoc Case 7: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -364,7 +358,7 @@ TestDoc Case 8: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -421,7 +415,7 @@ TestDoc Case 9: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect index 9ce9dd51..b82d9957 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/enum_test/llgo.expect @@ -9,10 +9,7 @@ TestEnumDecl Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -71,10 +68,7 @@ TestEnumDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -133,10 +127,7 @@ TestEnumDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 27471a41..135b4292 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -9,10 +9,7 @@ TestFuncDecl Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -22,7 +19,7 @@ TestFuncDecl Case 1: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -54,10 +51,7 @@ TestFuncDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -74,14 +68,8 @@ TestFuncDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -120,10 +108,7 @@ TestFuncDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -142,7 +127,7 @@ TestFuncDecl Case 3: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -150,14 +135,8 @@ TestFuncDecl Case 3: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -196,10 +175,7 @@ TestFuncDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -216,14 +192,8 @@ TestFuncDecl Case 4: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -237,14 +207,8 @@ TestFuncDecl Case 4: "Kind": 8, "Flags": 16 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -286,10 +250,7 @@ TestFuncDecl Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -306,14 +267,8 @@ TestFuncDecl Case 5: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ @@ -327,14 +282,8 @@ TestFuncDecl Case 5: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 0, "Names": [{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index ae68923b..72584baa 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -9,10 +9,7 @@ TestScope Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -22,7 +19,7 @@ TestScope Case 1: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -54,10 +51,7 @@ TestScope Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "a" @@ -70,7 +64,7 @@ TestScope Case 2: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -102,10 +96,7 @@ TestScope Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "ScopingExpr", "X": { @@ -125,7 +116,7 @@ TestScope Case 3: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -157,10 +148,7 @@ TestScope Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -171,7 +159,7 @@ TestScope Case 4: "Tag": 3, "Fields": { "_Type": "FieldList", - "List": [] + "List": null }, "Methods": [{ "_Type": "FuncDecl", @@ -179,10 +167,7 @@ TestScope Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "a" @@ -195,7 +180,7 @@ TestScope Case 4: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", @@ -229,10 +214,7 @@ TestScope Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "a" @@ -246,7 +228,7 @@ TestScope Case 5: "Tag": 3, "Fields": { "_Type": "FieldList", - "List": [] + "List": null }, "Methods": [{ "_Type": "FuncDecl", @@ -254,10 +236,7 @@ TestScope Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "ScopingExpr", "X": { @@ -277,7 +256,7 @@ TestScope Case 5: "_Type": "FuncType", "Params": { "_Type": "FieldList", - "List": [] + "List": null }, "Ret": { "_Type": "BuiltinType", diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index 9c724cd1..3440c6e3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -9,10 +9,7 @@ TestStructDecl Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": null, "Type": { @@ -27,14 +24,8 @@ TestStructDecl Case 1: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -61,10 +52,7 @@ TestStructDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -82,14 +70,8 @@ TestStructDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -103,14 +85,8 @@ TestStructDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -137,10 +113,7 @@ TestStructDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -158,14 +131,8 @@ TestStructDecl Case 3: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -179,14 +146,8 @@ TestStructDecl Case 3: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -213,10 +174,7 @@ TestStructDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -234,14 +192,8 @@ TestStructDecl Case 4: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -267,7 +219,7 @@ TestStructDecl Case 4: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -279,7 +231,7 @@ TestStructDecl Case 4: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }] }, "Ret": { @@ -289,14 +241,8 @@ TestStructDecl Case 4: } } }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -323,10 +269,7 @@ TestStructDecl Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -344,14 +287,8 @@ TestStructDecl Case 5: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -372,14 +309,8 @@ TestStructDecl Case 5: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -393,14 +324,8 @@ TestStructDecl Case 5: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -414,14 +339,8 @@ TestStructDecl Case 5: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -432,14 +351,8 @@ TestStructDecl Case 5: }, "Methods": [] }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 07225285..873e35b7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -9,10 +9,7 @@ TestTypeDefDecl Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -39,10 +36,7 @@ TestTypeDefDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -59,10 +53,7 @@ TestTypeDefDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -88,10 +79,7 @@ TestTypeDefDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -108,10 +96,7 @@ TestTypeDefDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -131,10 +116,7 @@ TestTypeDefDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -165,10 +147,7 @@ TestTypeDefDecl Case 4: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -191,7 +170,7 @@ TestTypeDefDecl Case 4: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -203,7 +182,7 @@ TestTypeDefDecl Case 4: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -213,7 +192,7 @@ TestTypeDefDecl Case 4: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }] }, "Ret": { @@ -239,10 +218,7 @@ TestTypeDefDecl Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -265,7 +241,7 @@ TestTypeDefDecl Case 5: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -277,7 +253,7 @@ TestTypeDefDecl Case 5: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }] }, "Ret": { @@ -293,10 +269,7 @@ TestTypeDefDecl Case 5: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -322,7 +295,7 @@ TestTypeDefDecl Case 5: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -337,7 +310,7 @@ TestTypeDefDecl Case 5: "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }] }, "Ret": { @@ -363,10 +336,7 @@ TestTypeDefDecl Case 6: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -387,14 +357,8 @@ TestTypeDefDecl Case 6: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 3, "Names": [{ @@ -411,10 +375,7 @@ TestTypeDefDecl Case 6: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -444,10 +405,7 @@ TestTypeDefDecl Case 6: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" @@ -480,10 +438,7 @@ TestTypeDefDecl Case 6: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": { "_Type": "Ident", "Name": "A" diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect index 798fb796..7be1d152 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/union_test/llgo.expect @@ -9,10 +9,7 @@ TestUnionDecl Case 1: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": null, "Type": { @@ -27,14 +24,8 @@ TestUnionDecl Case 1: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -48,14 +39,8 @@ TestUnionDecl Case 1: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -82,10 +67,7 @@ TestUnionDecl Case 2: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -103,14 +85,8 @@ TestUnionDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -124,14 +100,8 @@ TestUnionDecl Case 2: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -158,10 +128,7 @@ TestUnionDecl Case 3: "_Type": "Location", "File": "temp.h" }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, "Parent": null, "Name": { "_Type": "Ident", @@ -179,14 +146,8 @@ TestUnionDecl Case 3: "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -200,14 +161,8 @@ TestUnionDecl Case 3: "Kind": 8, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -228,14 +183,8 @@ TestUnionDecl Case 3: "Kind": 2, "Flags": 1 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -249,14 +198,8 @@ TestUnionDecl Case 3: "Kind": 6, "Flags": 32 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -267,14 +210,8 @@ TestUnionDecl Case 3: }, "Methods": [] }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 1ba26bf6..d7e93459 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -141,14 +141,8 @@ Type: struct (unnamed struct at temp.h:1:1): "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -186,14 +180,8 @@ Type: union (unnamed union at temp.h:1:1): "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 1, "Names": [{ @@ -261,14 +249,8 @@ Type: class (unnamed class at temp.h:1:1): "Kind": 6, "Flags": 0 }, - "Doc": { - "_Type": "CommentGroup", - "List": [] - }, - "Comment": { - "_Type": "CommentGroup", - "List": [] - }, + "Doc": null, + "Comment": null, "IsStatic": false, "Access": 3, "Names": [{ @@ -339,7 +321,7 @@ Type: int (*)(int, char): "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }, { "_Type": "Field", "Type": { @@ -351,7 +333,7 @@ Type: int (*)(int, char): "Comment": null, "IsStatic": false, "Access": 0, - "Names": [] + "Names": null }] }, "Ret": { diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 0b771fb7..932d8995 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -14,47 +14,89 @@ func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON { return root } -func MarshalASTFile(file *ast.File) *cjson.JSON { - root := cjson.Object() - decls := cjson.Array() - - for _, decl := range file.Decls { - decls.AddItem(MarshalASTDecl(decl)) +func MarshalDeclList(list []ast.Decl) *cjson.JSON { + if list == nil { + return cjson.Null() } - root.SetItem(c.Str("_Type"), stringField("File")) - root.SetItem(c.Str("decls"), decls) - - // json:includes,omitempty - if file.Includes != nil { - includes := cjson.Array() - for _, i := range file.Includes { - include := cjson.Object() - include.SetItem(c.Str("_Type"), stringField("Include")) - include.SetItem(c.Str("Path"), stringField(i.Path)) - includes.AddItem(include) - } - root.SetItem(c.Str("includes"), includes) - } - - // json:macros,omitempty - if file.Macros != nil { - macros := cjson.Array() - for _, m := range file.Macros { - marco := cjson.Object() - marco.SetItem(c.Str("_Type"), stringField("Macro")) - marco.SetItem(c.Str("Name"), stringField(m.Name)) - tokens := cjson.Array() - for _, tok := range m.Tokens { - tokens.AddItem(Token(tok)) - } - marco.SetItem(c.Str("Tokens"), tokens) - macros.AddItem(marco) - } - root.SetItem(c.Str("macros"), macros) + root := cjson.Array() + for _, item := range list { + root.AddItem(MarshalASTDecl(item)) } return root } -func Token(tok *ast.Token) *cjson.JSON { + +func MarshalFieldList(list []*ast.Field) *cjson.JSON { + if list == nil { + return cjson.Null() + } + root := cjson.Array() + for _, item := range list { + root.AddItem(MarshalASTExpr(item)) + } + return root +} + +func MarshalIncludeList(list []*ast.Include) *cjson.JSON { + if list == nil { + return cjson.Null() + } + root := cjson.Array() + for _, item := range list { + include := cjson.Object() + include.SetItem(c.Str("_Type"), stringField("Include")) + include.SetItem(c.Str("Path"), stringField(item.Path)) + root.AddItem(include) + } + return root +} + +func MarshalMacroList(list []*ast.Macro) *cjson.JSON { + if list == nil { + return cjson.Null() + } + root := cjson.Array() + for _, item := range list { + macro := cjson.Object() + macro.SetItem(c.Str("_Type"), stringField("Macro")) + macro.SetItem(c.Str("Name"), stringField(item.Name)) + macro.SetItem(c.Str("Tokens"), MarshalTokenList(item.Tokens)) + root.AddItem(macro) + } + return root +} + +func MarshalTokenList(list []*ast.Token) *cjson.JSON { + if list == nil { + return cjson.Null() + } + root := cjson.Array() + for _, item := range list { + root.AddItem(MarshalToken(item)) + } + return root +} + +func MarshalIdentList(list []*ast.Ident) *cjson.JSON { + if list == nil { + return cjson.Null() + } + root := cjson.Array() + for _, item := range list { + root.AddItem(MarshalASTExpr(item)) + } + return root +} + +func MarshalASTFile(file *ast.File) *cjson.JSON { + root := cjson.Object() + root.SetItem(c.Str("_Type"), stringField("File")) + root.SetItem(c.Str("decls"), MarshalDeclList(file.Decls)) + root.SetItem(c.Str("includes"), MarshalIncludeList(file.Includes)) + root.SetItem(c.Str("macros"), MarshalMacroList(file.Macros)) + return root +} + +func MarshalToken(tok *ast.Token) *cjson.JSON { root := cjson.Object() root.SetItem(c.Str("_Type"), stringField("Token")) root.SetItem(c.Str("Token"), numberField(uint(tok.Token))) @@ -105,7 +147,6 @@ func MarshalASTDeclBase(decl ast.DeclBase, root *cjson.JSON) { loc.SetItem(c.Str("_Type"), stringField("Location")) loc.SetItem(c.Str("File"), stringField(decl.Loc.File)) root.SetItem(c.Str("Loc"), loc) - root.SetItem(c.Str("Doc"), MarshalASTExpr(decl.Doc)) root.SetItem(c.Str("Parent"), MarshalASTExpr(decl.Parent)) } @@ -144,14 +185,7 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Ret"), MarshalASTExpr(d.Ret)) case *ast.FieldList: root.SetItem(c.Str("_Type"), stringField("FieldList")) - if d == nil { - return cjson.Null() - } - list := cjson.Array() - for _, f := range d.List { - list.AddItem(MarshalASTExpr(f)) - } - root.SetItem(c.Str("List"), list) + root.SetItem(c.Str("List"), MarshalFieldList(d.List)) case *ast.Field: root.SetItem(c.Str("_Type"), stringField("Field")) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) @@ -159,11 +193,7 @@ func MarshalASTExpr(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Comment"), MarshalASTExpr(d.Comment)) root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic)) root.SetItem(c.Str("Access"), numberField(uint(d.Access))) - names := cjson.Array() - for _, n := range d.Names { - names.AddItem(MarshalASTExpr(n)) - } - root.SetItem(c.Str("Names"), names) + root.SetItem(c.Str("Names"), MarshalIdentList(d.Names)) case *ast.Variadic: root.SetItem(c.Str("_Type"), stringField("Variadic")) case *ast.Ident: From 3c9bfb5b4df55261e58ebcc5467cba37f0722abf Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 3 Sep 2024 18:29:48 +0800 Subject: [PATCH 55/67] llcppsigfetch:determine unexpected named typedecl in typedef anonymous --- chore/_xtool/llcppsigfetch/parse/cvt.go | 161 +++++++++++++++++++----- 1 file changed, 127 insertions(+), 34 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 675633c5..98d97503 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -19,6 +19,28 @@ type Converter struct { curLoc ast.Location index *clang.Index unit *clang.TranslationUnit + + typeDecls map[string]ast.Decl // cursorUsr -> ast.Decl + + // anonyTypeMap stores mappings for unexpected named declarations in typedefs + // that actually represent anonymous types. + // + // Key: The USR (Unified Symbol Resolution) of the declaration cursor. + // Value: The generated name for the anonymous type. + // + // This map is necessary due to a limitation in libclang where anonymous + // structs, unions, or enums within typedefs are incorrectly reported as + // named declarations. We use this map to keep track of these cases and + // generate appropriate names for them. + // + // Additionally, for all nodes referencing these anonymous types, their + // name references are updated to use the corresponding anonyname from + // this map. This ensures consistent naming across the entire AST for + // these anonymous types. + // + // Example: + // typedef struct { int x; } MyStruct; + anonyTypeMap map[string]string // cursorUsr -> anonyname } type Config struct { @@ -34,9 +56,11 @@ func NewConverter(config *Config) (*Converter, error) { } return &Converter{ - Files: make(map[string]*ast.File), - index: index, - unit: unit, + Files: make(map[string]*ast.File), + index: index, + unit: unit, + anonyTypeMap: make(map[string]string), + typeDecls: make(map[string]ast.Decl), }, nil } @@ -91,6 +115,27 @@ func (ct *Converter) Dispose() { ct.unit.Dispose() } +func (ct *Converter) GetTokens(cursor clang.Cursor) []*ast.Token { + ran := cursor.Extent() + var numTokens c.Uint + var tokens *clang.Token + ct.unit.Tokenize(ran, &tokens, &numTokens) + defer ct.unit.DisposeTokens(tokens, numTokens) + + tokensSlice := unsafe.Slice(tokens, int(numTokens)) + + result := make([]*ast.Token, 0, int(numTokens)) + for _, tok := range tokensSlice { + tokStr := ct.unit.Token(tok) + result = append(result, &ast.Token{ + Token: toToken(tok), + Lit: c.GoString(tokStr.CStr()), + }) + tokStr.Dispose() + } + return result +} + func (ct *Converter) UpdateLoc(cursor clang.Cursor) { loc := cursor.Location() var file clang.File @@ -106,7 +151,6 @@ func (ct *Converter) UpdateLoc(cursor clang.Cursor) { filePath := c.GoString(filename.CStr()) ct.curLoc = ast.Location{File: filePath} - } func (ct *Converter) GetCurFile() *ast.File { @@ -125,6 +169,21 @@ func (ct *Converter) GetCurFile() *ast.File { return file } +func (ct *Converter) AddTypeDecl(cursor clang.Cursor, decl ast.Decl) { + usr := cursor.USR() + usrStr := c.GoString(usr.CStr()) + ct.typeDecls[usrStr] = decl + usr.Dispose() +} + +func (ct *Converter) GetTypeDecl(cursor clang.Cursor) (ast.Decl, bool) { + usr := cursor.USR() + usrStr := c.GoString(usr.CStr()) + decl, ok := ct.typeDecls[usrStr] + usr.Dispose() + return decl, ok +} + func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { rawComment := cursor.RawCommentText() defer rawComment.Dispose() @@ -274,11 +333,33 @@ func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { name := cursor.String() defer name.Dispose() - return &ast.TypedefDecl{ + + var typ ast.Expr + underlyingTyp := cursor.TypedefDeclUnderlyingType() + if underlyingTyp.Kind != clang.TypeElaborated { + typ = ct.ProcessType(underlyingTyp) + } else { + typ = ct.ProcessElaboratedType(underlyingTyp) + referTypeCursor := underlyingTyp.TypeDeclaration() + if _, ok := typ.(*ast.TagExpr); ok && isCursorChildOf(referTypeCursor, cursor) { + // Handle unexpected named structures generated from anonymous RecordTypes in Typedefs + // In this case, libclang incorrectly reports an anonymous struct as a named struct + // The reference style is TagRefer, for example: struct MyStruct + sourceCode := ct.GetTokens(referTypeCursor) + if sourceCode[0].Token == token.KEYWORD && (sourceCode[1].Token == token.PUNCT && sourceCode[1].Lit == "{") { + println("todo:unexpected named decl in typedef anonymous decl") + } + } + } + + decl := &ast.TypedefDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Type: ct.ProcessType(cursor.TypedefDeclUnderlyingType()), + Type: typ, } + + ct.AddTypeDecl(cursor, decl) + return decl } // converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. @@ -295,25 +376,27 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { } params := ct.ProcessFieldList(cursor) funcType.Params = params - fn := &ast.FuncDecl{ + funcDecl := &ast.FuncDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: funcType, } if cursor.IsFunctionInlined() != 0 { - fn.IsInline = true + funcDecl.IsInline = true } if isMethod(cursor) { - ct.ProcessMethodAttributes(cursor, fn) + ct.ProcessMethodAttributes(cursor, funcDecl) } else { if cursor.StorageClass() == clang.SCStatic { - fn.IsStatic = true + funcDecl.IsStatic = true } } - return fn + ct.AddTypeDecl(cursor, funcDecl) + + return funcDecl } // get Methods Attributes @@ -392,11 +475,13 @@ func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { name := cursor.String() defer name.Dispose() - return &ast.EnumTypeDecl{ + decl := &ast.EnumTypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: ct.ProcessEnumType(cursor), } + ct.AddTypeDecl(cursor, decl) + return decl } // current only collect macro which defined in file @@ -404,26 +489,9 @@ func (ct *Converter) ProcessMacro(cursor clang.Cursor) *ast.Macro { name := cursor.String() defer name.Dispose() - ran := cursor.Extent() - var numTokens c.Uint - var tokens *clang.Token - ct.unit.Tokenize(ran, &tokens, &numTokens) - defer ct.unit.DisposeTokens(tokens, numTokens) - - tokensSlice := unsafe.Slice(tokens, int(numTokens)) - macro := &ast.Macro{ Name: c.GoString(name.CStr()), - Tokens: make([]*ast.Token, 0), - } - - for _, tok := range tokensSlice { - tokStr := ct.unit.Token(tok) - macro.Tokens = append(macro.Tokens, &ast.Token{ - Token: toToken(tok), - Lit: c.GoString(tokStr.CStr()), - }) - tokStr.Dispose() + Tokens: ct.GetTokens(cursor), } return macro } @@ -534,7 +602,6 @@ func (ct *Converter) ProcessMethods(cursor clang.Cursor) []*ast.FuncDecl { func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) *ast.TypeDecl { anony := cursor.IsAnonymousRecordDecl() - var name *ast.Ident if anony == 0 { cursorName := cursor.String() @@ -542,11 +609,14 @@ func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) *ast.TypeDecl { name = &ast.Ident{Name: c.GoString(cursorName.CStr())} } - return &ast.TypeDecl{ + decl := &ast.TypeDecl{ DeclBase: ct.CreateDeclBase(cursor), Name: name, Type: ct.ProcessRecordType(cursor), } + ct.AddTypeDecl(cursor, decl) + + return decl } func (ct *Converter) ProcessStructDecl(cursor clang.Cursor) *ast.TypeDecl { @@ -560,14 +630,16 @@ func (ct *Converter) ProcessUnionDecl(cursor clang.Cursor) *ast.TypeDecl { func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { // Pushing class scope before processing its type and popping after base := ct.CreateDeclBase(cursor) - typ := ct.ProcessRecordType(cursor) - return &ast.TypeDecl{ + decl := &ast.TypeDecl{ DeclBase: base, Name: &ast.Ident{Name: c.GoString(cursor.String().CStr())}, Type: typ, } + ct.AddTypeDecl(cursor, decl) + + return decl } func (ct *Converter) ProcessRecordType(cursor clang.Cursor) *ast.RecordType { @@ -598,12 +670,14 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { decl := t.TypeDeclaration() if decl.IsAnonymous() != 0 { + // anonymous type refer (except anonymous RecordType&EnumType in TypedefDecl) if decl.Kind == clang.CursorEnumDecl { return ct.ProcessEnumType(decl) } return ct.ProcessRecordType(decl) } + // type name refer typeName := c.GoString(name.CStr()) tagMap := map[string]ast.Tag{ @@ -754,3 +828,22 @@ func buildScopingFromParts(parts []string) ast.Expr { } return expr } + +// isCursorChildOf checks if the child cursor is contained within the parent cursor. +// This function is necessary because libclang doesn't correctly report the lexical +// or semantic parent for anonymous structs inside typedefs. By comparing source ranges, +// we can determine if one cursor is nested inside another. +func isCursorChildOf(child, parent clang.Cursor) bool { + return isRangeChildOf(child.Extent(), parent.Extent()) +} + +func isRangeChildOf(childRange, parentRange clang.SourceRange) bool { + return getOffset(childRange.RangeStart()) >= getOffset(parentRange.RangeStart()) && + getOffset(childRange.RangeEnd()) <= getOffset(parentRange.RangeEnd()) +} + +func getOffset(location clang.SourceLocation) c.Uint { + var offset c.Uint + location.SpellingLocation(nil, nil, nil, &offset) + return offset +} From e1236f9deb7ce1a92eac7751469a007438bc9314 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 4 Sep 2024 17:55:54 +0800 Subject: [PATCH 56/67] llcppsigfetch:typedef anonymous record type correct refer by __ANONY_ name --- chore/_xtool/llcppsigfetch/parse/cvt.go | 180 ++++-- .../decl_test/typedef_test/llgo.expect | 583 ++++++++++++++++++ .../decl_test/typedef_test/typedef.go | 24 + 3 files changed, 736 insertions(+), 51 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 98d97503..c047e5bf 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -43,6 +43,13 @@ type Converter struct { anonyTypeMap map[string]string // cursorUsr -> anonyname } +var tagMap = map[string]ast.Tag{ + "struct": ast.Struct, + "union": ast.Union, + "enum": ast.Enum, + "class": ast.Class, +} + type Config struct { File string Temp bool @@ -169,7 +176,22 @@ func (ct *Converter) GetCurFile() *ast.File { return file } -func (ct *Converter) AddTypeDecl(cursor clang.Cursor, decl ast.Decl) { +func (ct *Converter) SetAnonyType(cursor clang.Cursor, anonyname string) { + usr := cursor.USR() + usrStr := c.GoString(usr.CStr()) + defer usr.Dispose() + ct.anonyTypeMap[usrStr] = anonyname +} + +func (ct *Converter) GetAnonyType(cursor clang.Cursor) (string, bool) { + usr := cursor.USR() + usrStr := c.GoString(usr.CStr()) + defer usr.Dispose() + anonyname, ok := ct.anonyTypeMap[usrStr] + return anonyname, ok +} + +func (ct *Converter) SetTypeDecl(cursor clang.Cursor, decl ast.Decl) { usr := cursor.USR() usrStr := c.GoString(usr.CStr()) ct.typeDecls[usrStr] = decl @@ -190,7 +212,7 @@ func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { res := ast.DeclBase{ Loc: &ct.curLoc, - Parent: buildScopingExpr(cursor.SemanticParent()), + Parent: ct.BuildScopingExpr(cursor.SemanticParent()), } commentGroup := &ast.CommentGroup{} @@ -334,23 +356,7 @@ func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { name := cursor.String() defer name.Dispose() - var typ ast.Expr - underlyingTyp := cursor.TypedefDeclUnderlyingType() - if underlyingTyp.Kind != clang.TypeElaborated { - typ = ct.ProcessType(underlyingTyp) - } else { - typ = ct.ProcessElaboratedType(underlyingTyp) - referTypeCursor := underlyingTyp.TypeDeclaration() - if _, ok := typ.(*ast.TagExpr); ok && isCursorChildOf(referTypeCursor, cursor) { - // Handle unexpected named structures generated from anonymous RecordTypes in Typedefs - // In this case, libclang incorrectly reports an anonymous struct as a named struct - // The reference style is TagRefer, for example: struct MyStruct - sourceCode := ct.GetTokens(referTypeCursor) - if sourceCode[0].Token == token.KEYWORD && (sourceCode[1].Token == token.PUNCT && sourceCode[1].Lit == "{") { - println("todo:unexpected named decl in typedef anonymous decl") - } - } - } + typ := ct.ProcessUnderlyingType(cursor) decl := &ast.TypedefDecl{ DeclBase: ct.CreateDeclBase(cursor), @@ -358,10 +364,59 @@ func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { Type: typ, } - ct.AddTypeDecl(cursor, decl) + ct.SetTypeDecl(cursor, decl) return decl } +func (ct *Converter) ProcessUnderlyingType(cursor clang.Cursor) ast.Expr { + underlyingTyp := cursor.TypedefDeclUnderlyingType() + if underlyingTyp.Kind != clang.TypeElaborated { + return ct.ProcessType(underlyingTyp) + } + + typ := ct.ProcessElaboratedType(underlyingTyp) + referTypeCursor := underlyingTyp.TypeDeclaration() + + // If the type decl for the reference already exists in anonyTypeMap + // then the refer has been processed in ProcessElaboratedType + if _, ok := ct.GetAnonyType(referTypeCursor); !ok && isCursorChildOf(referTypeCursor, cursor) { + // Handle unexpected named structures generated from anonymous RecordTypes in Typedefs + // In this case, libclang incorrectly reports an anonymous struct as a named struct + sourceCode := ct.GetTokens(referTypeCursor) + if isAnonymousStructure(sourceCode) { + anonyName := ct.GenerateAnonymousName(referTypeCursor) + // update reference's name + setInnerName(typ, anonyName) + ct.SetAnonyType(referTypeCursor, anonyName) + typ, isValidType := ct.GetTypeDecl(referTypeCursor) + if isValidType { + // There will be no anonymous classes,here will execute enum,union,struct + switch declType := typ.(type) { + case *ast.EnumTypeDecl: + declType.Name.Name = anonyName + case *ast.TypeDecl: + if declType.Type.Tag != ast.Class { + declType.Name.Name = anonyName + } else { + // Unreachable: There should be no anonymous classes in this context + fmt.Fprintln(os.Stderr, "unexpect typedef anonymous class %s", declType.Name.Name) + } + } + } else { + // Unreachable:When referencing an anonymous node, its collection must have been completed beforehand + fmt.Fprintln(os.Stderr, "anonymous node not collected before reference") + } + } + } + + return typ +} + +// generates a name for an anonymous structure like:__ANONY_A_B_MYSTRUCT +func (ct *Converter) GenerateAnonymousName(cursor clang.Cursor) string { + return fmt.Sprintf("__ANONY_%s", strings.Join(ct.BuildScopingParts(cursor), "_")) +} + // converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() @@ -394,7 +449,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { } } - ct.AddTypeDecl(cursor, funcDecl) + ct.SetTypeDecl(cursor, funcDecl) return funcDecl } @@ -402,7 +457,7 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { // get Methods Attributes func (ct *Converter) ProcessMethodAttributes(cursor clang.Cursor, fn *ast.FuncDecl) { if parent := cursor.SemanticParent(); parent.Equal(cursor.LexicalParent()) != 1 { - fn.DeclBase.Parent = buildScopingExpr(cursor.SemanticParent()) + fn.DeclBase.Parent = ct.BuildScopingExpr(cursor.SemanticParent()) } switch cursor.Kind { @@ -480,7 +535,7 @@ func (ct *Converter) ProcessEnumDecl(cursor clang.Cursor) *ast.EnumTypeDecl { Name: &ast.Ident{Name: c.GoString(name.CStr())}, Type: ct.ProcessEnumType(cursor), } - ct.AddTypeDecl(cursor, decl) + ct.SetTypeDecl(cursor, decl) return decl } @@ -614,7 +669,7 @@ func (ct *Converter) ProcessRecordDecl(cursor clang.Cursor) *ast.TypeDecl { Name: name, Type: ct.ProcessRecordType(cursor), } - ct.AddTypeDecl(cursor, decl) + ct.SetTypeDecl(cursor, decl) return decl } @@ -637,7 +692,7 @@ func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { Name: &ast.Ident{Name: c.GoString(cursor.String().CStr())}, Type: typ, } - ct.AddTypeDecl(cursor, decl) + ct.SetTypeDecl(cursor, decl) return decl } @@ -677,16 +732,8 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { return ct.ProcessRecordType(decl) } - // type name refer typeName := c.GoString(name.CStr()) - tagMap := map[string]ast.Tag{ - "struct": ast.Struct, - "union": ast.Union, - "enum": ast.Enum, - "class": ast.Class, - } - // for elaborated type, it could have a tag description // like struct A, union B, class C, enum D parts := strings.SplitN(typeName, " ", 2) @@ -694,12 +741,12 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { if tagValue, ok := tagMap[parts[0]]; ok { return &ast.TagExpr{ Tag: tagValue, - Name: buildScopingExpr(t.TypeDeclaration()), + Name: ct.BuildScopingExpr(decl), } } } - return buildScopingExpr(t.TypeDeclaration()) + return ct.BuildScopingExpr(decl) } func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { @@ -772,6 +819,32 @@ func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType { } } +// Constructs a complete scoping expression by traversing the semantic parents, starting from the given clang.Cursor +// For anonymous decl of typedef references, use their anonymous name +func (ct *Converter) BuildScopingExpr(cursor clang.Cursor) ast.Expr { + parts := ct.BuildScopingParts(cursor) + return buildScopingFromParts(parts) +} + +func (ct *Converter) BuildScopingParts(cursor clang.Cursor) []string { + var parts []string + // Traverse up the semantic parents + for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit { + var qualified string + // anonymous decl quote + if anonyName, ok := ct.GetAnonyType(cursor); ok { + qualified = anonyName + } else { + name := cursor.String() + qualified = c.GoString(name.CStr()) + name.Dispose() + } + parts = append([]string{qualified}, parts...) + cursor = cursor.SemanticParent() + } + return parts +} + func (ct *Converter) MarshalASTFiles() *cjson.JSON { return MarshalASTFiles(ct.Files) } @@ -798,22 +871,6 @@ func isMethod(cursor clang.Cursor) bool { return cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor } -// Constructs a complete scoping expression by traversing the semantic parents, starting from the given clang.Cursor -func buildScopingExpr(cursor clang.Cursor) ast.Expr { - var parts []string - - // Traverse up the semantic parents - for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit { - name := cursor.String() - qualified := c.GoString(name.CStr()) - parts = append([]string{qualified}, parts...) - cursor = cursor.SemanticParent() - name.Dispose() - } - - return buildScopingFromParts(parts) -} - func buildScopingFromParts(parts []string) ast.Expr { if len(parts) == 0 { return nil @@ -847,3 +904,24 @@ func getOffset(location clang.SourceLocation) c.Uint { location.SpellingLocation(nil, nil, nil, &offset) return offset } + +// setTagName updates the name of the innermost Ident in a Name Expr +func setInnerName(expr ast.Expr, name string) { + switch e := expr.(type) { + case *ast.TagExpr: + setInnerName(e.Name, name) + case *ast.ScopingExpr: + setInnerName(e.X, name) + case *ast.Ident: + e.Name = name + } +} + +// checks if the source code represents an actual anonymous structure +func isAnonymousStructure(sourceCode []*ast.Token) bool { + _, isValidTag := tagMap[sourceCode[0].Lit] + return sourceCode[0].Token == token.KEYWORD && + isValidTag && + sourceCode[1].Token == token.PUNCT && + sourceCode[1].Lit == "{" +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 873e35b7..9e527797 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -472,6 +472,589 @@ TestTypeDefDecl Case 6: } } +TestTypeDefDecl Case 7: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Type": { + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] + }, + "Methods": [] + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "MyStruct" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Tag": 0 + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 8: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyUnion" + }, + "Type": { + "_Type": "RecordType", + "Tag": 1, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] + }, + "Methods": [] + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "MyUnion" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyUnion" + }, + "Tag": 1 + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 9: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "EnumTypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyEnum" + }, + "Type": { + "_Type": "EnumType", + "Items": [{ + "_Type": "EnumItem", + "Name": { + "_Type": "Ident", + "Name": "RED" + }, + "Value": { + "_Type": "BasicLit", + "Kind": 0, + "Value": "0" + } + }, { + "_Type": "EnumItem", + "Name": { + "_Type": "Ident", + "Name": "GREEN" + }, + "Value": { + "_Type": "BasicLit", + "Kind": 0, + "Value": "1" + } + }, { + "_Type": "EnumItem", + "Name": { + "_Type": "Ident", + "Name": "BLUE" + }, + "Value": { + "_Type": "BasicLit", + "Kind": 0, + "Value": "2" + } + }] + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "MyEnum" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyEnum" + }, + "Tag": 2 + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 10: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Type": { + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] + }, + "Methods": [] + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "MyStruct" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Tag": 0 + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "MyStruct2" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Tag": 0 + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "StructPtr" + }, + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Tag": 0 + } + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "StructArr" + }, + "Type": { + "_Type": "ArrayType", + "Elt": { + "_Type": "TagExpr", + "Name": { + "_Type": "Ident", + "Name": "__ANONY_MyStruct" + }, + "Tag": 0 + }, + "Len": null + } + }], + "includes": [], + "macros": [] + } +} + +TestTypeDefDecl Case 11: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Name": { + "_Type": "Ident", + "Name": "__ANONY_A_B_MyStruct" + }, + "Type": { + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] + }, + "Methods": [] + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Name": { + "_Type": "Ident", + "Name": "MyStruct" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "__ANONY_A_B_MyStruct" + }, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + } + }, + "Tag": 0 + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Name": { + "_Type": "Ident", + "Name": "MyStruct2" + }, + "Type": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "__ANONY_A_B_MyStruct" + }, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + } + }, + "Tag": 0 + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Name": { + "_Type": "Ident", + "Name": "StructPtr" + }, + "Type": { + "_Type": "PointerType", + "X": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "__ANONY_A_B_MyStruct" + }, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + } + }, + "Tag": 0 + } + } + }, { + "_Type": "TypedefDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + }, + "Name": { + "_Type": "Ident", + "Name": "StructArr" + }, + "Type": { + "_Type": "ArrayType", + "Elt": { + "_Type": "TagExpr", + "Name": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "__ANONY_A_B_MyStruct" + }, + "Parent": { + "_Type": "ScopingExpr", + "X": { + "_Type": "Ident", + "Name": "B" + }, + "Parent": { + "_Type": "Ident", + "Name": "A" + } + } + }, + "Tag": 0 + }, + "Len": null + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 27dd0f19..623b4dde 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -24,6 +24,30 @@ func TestTypeDefDecl() { int x; } MyClass,*MyClassPtr,MyClassArray[]; }`, + + `typedef struct { + int x; + } MyStruct`, + `typedef union { + int x; + } MyUnion`, + `typedef enum { + RED, + GREEN, + BLUE + } MyEnum`, + + `typedef struct { + int x; + } MyStruct,MyStruct2,*StructPtr, StructArr[];`, + + `namespace A{ + namespace B{ + typedef struct { + int x; + } MyStruct,MyStruct2,*StructPtr, StructArr[]; + } + }`, } test.RunTest("TestTypeDefDecl", testCases) } From ae71f3c186e11c44fc3867ac8e52681fb41154f7 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 5 Sep 2024 12:18:37 +0800 Subject: [PATCH 57/67] llcppsigfetch:extract info command --- chore/_xtool/llcppsigfetch/llcppsigfetch.go | 67 +++++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/cvt.go | 2 +- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 6f9dcd37..898cb9fc 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -30,6 +30,43 @@ import ( ) func main() { + if len(os.Args) == 1 { + // run with default config file + runFromConfig() + return + } + + if os.Args[1] == "--extract" { + runExtract() + } else if os.Args[1] == "--help" || os.Args[1] == "-h" { + printUsage() + } else { + runFromConfig() + } +} + +func printUsage() { + fmt.Println("Usage:") + fmt.Println(" llcppsigfetch []") + fmt.Println(" OR") + fmt.Println(" llcppsigfetch --extract [args...]") + fmt.Println("") + fmt.Println("Options:") + fmt.Println(" []: Path to the configuration file (use '-' for stdin)") + fmt.Println(" If not provided, uses default 'llcppg.cfg'") + fmt.Println("") + fmt.Println(" --extract: Extract information from a single file") + fmt.Println(" : When is false: the path to the file to process") + fmt.Println(" When is true: the content of the file to process") + fmt.Println(" : 'true' if contains file content, 'false' if it's a file path") + fmt.Println(" [args]: Optional additional arguments (default: -x c++ -std=c++11)") + fmt.Println("") + fmt.Println(" --help, -h: Show this help message") + fmt.Println("") + fmt.Println("Note: The two usage modes are mutually exclusive. Use either [] OR --extract, not both.") +} + +func runFromConfig() { cfgFile := "llcppg.cfg" if len(os.Args) > 1 { cfgFile = os.Args[1] @@ -61,6 +98,36 @@ func main() { outputInfo(context) } +func runExtract() { + if len(os.Args) < 4 { + printUsage() + os.Exit(1) + } + + cfg := &parse.Config{ + File: os.Args[2], + Temp: os.Args[3] == "true", + Args: os.Args[4:], + } + if !cfg.Temp { + absPath, err := filepath.Abs(cfg.File) + check(err) + cfg.File = absPath + println(cfg.File) + } + + converter, err := parse.NewConverter(cfg) + check(err) + _, err = converter.Convert() + check(err) + result := converter.MarshalASTFiles() + cstr := result.Print() + c.Printf(cstr) + cjson.FreeCStr(cstr) + result.Delete() + converter.Dispose() +} + func check(err error) { if err != nil { panic(err) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index c047e5bf..3fc04865 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -72,7 +72,7 @@ func NewConverter(config *Config) (*Converter, error) { } func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) { - if config.Args == nil { + if config.Args == nil || len(config.Args) == 0 { config.Args = []string{"-x", "c++", "-std=c++11"} } From 07519732a1f38604f34536c961f2ab55e4c00797 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 6 Sep 2024 11:44:56 +0800 Subject: [PATCH 58/67] llcppsigfetch:based on language configuration analysis --- chore/_xtool/llcppsigfetch/llcppsigfetch.go | 59 ++++++++++++++----- chore/_xtool/llcppsigfetch/parse/cvt.go | 20 ++++--- .../llcppsigfetch/parse/cvt_test/cvt.go | 23 +++++--- .../parse/cvt_test/type_test/type.go | 3 +- chore/_xtool/llcppsigfetch/parse/parse.go | 9 ++- 5 files changed, 82 insertions(+), 32 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 898cb9fc..944ce300 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -21,6 +21,7 @@ import ( "io" "os" "path/filepath" + "strconv" "strings" "github.com/goplus/llgo/c" @@ -56,10 +57,14 @@ func printUsage() { fmt.Println(" If not provided, uses default 'llcppg.cfg'") fmt.Println("") fmt.Println(" --extract: Extract information from a single file") - fmt.Println(" : When is false: the path to the file to process") - fmt.Println(" When is true: the content of the file to process") - fmt.Println(" : 'true' if contains file content, 'false' if it's a file path") - fmt.Println(" [args]: Optional additional arguments (default: -x c++ -std=c++11)") + fmt.Println(" : Path to the file to process, or file content if -temp=true") + fmt.Println(" -temp=: Optional. Set to 'true' if contains file content,") + fmt.Println(" 'false' (default) if it's a file path") + fmt.Println(" -cpp=: Optional. Set to 'true' if the language is C++ (default: true)") + fmt.Println(" If not present, is a file path") + fmt.Println(" [args]: Optional additional arguments") + fmt.Println(" Default for C++: -x c++") + fmt.Println(" Default for C: -x c") fmt.Println("") fmt.Println(" --help, -h: Show this help message") fmt.Println("") @@ -91,7 +96,7 @@ func runFromConfig() { files := getHeaderFiles(conf.CFlags, conf.Include) - context := parse.NewContext() + context := parse.NewContext(conf.Cplusplus) err = context.ProcessFiles(files) check(err) @@ -99,21 +104,33 @@ func runFromConfig() { } func runExtract() { - if len(os.Args) < 4 { + if len(os.Args) < 3 { + fmt.Println("Error: Insufficient arguments for --extract") printUsage() os.Exit(1) } cfg := &parse.Config{ - File: os.Args[2], - Temp: os.Args[3] == "true", - Args: os.Args[4:], + File: os.Args[2], + Args: []string{}, + IsCpp: true, + Temp: false, } - if !cfg.Temp { - absPath, err := filepath.Abs(cfg.File) - check(err) - cfg.File = absPath - println(cfg.File) + + for i := 3; i < len(os.Args); i++ { + arg := os.Args[i] + switch { + case strings.HasPrefix(arg, "-temp="): + cfg.Temp = parseBoolArg(arg, "temp", false) + os.Args = append(os.Args[:i], os.Args[i+1:]...) + i-- + case strings.HasPrefix(arg, "-cpp="): + cfg.IsCpp = parseBoolArg(arg, "cpp", true) + os.Args = append(os.Args[:i], os.Args[i+1:]...) + i-- + default: + cfg.Args = append(cfg.Args, arg) + } } converter, err := parse.NewConverter(cfg) @@ -151,3 +168,17 @@ func outputInfo(context *parse.Context) { defer info.Delete() c.Printf(str) } + +func parseBoolArg(arg, name string, defaultValue bool) bool { + parts := strings.SplitN(arg, "=", 2) + if len(parts) != 2 { + fmt.Printf("Warning: Invalid -%s= argument, defaulting to %v\n", name, defaultValue) + return defaultValue + } + value, err := strconv.ParseBool(parts[1]) + if err != nil { + fmt.Printf("Warning: Invalid -%s= value '%s', defaulting to %v\n", name, parts[1], defaultValue) + return defaultValue + } + return value +} diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 3fc04865..e2ca9344 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -51,9 +51,10 @@ var tagMap = map[string]ast.Tag{ } type Config struct { - File string - Temp bool - Args []string + File string + Temp bool + Args []string + IsCpp bool } func NewConverter(config *Config) (*Converter, error) { @@ -72,12 +73,16 @@ func NewConverter(config *Config) (*Converter, error) { } func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) { - if config.Args == nil || len(config.Args) == 0 { - config.Args = []string{"-x", "c++", "-std=c++11"} + // default use the c/c++ standard of clang; c:gnu17 c++:gnu++17 + // https://clang.llvm.org/docs/CommandGuide/clang.html + defaultArgs := []string{"-x", "c"} + if config.IsCpp { + defaultArgs = []string{"-x", "c++"} } + allArgs := append(defaultArgs, config.Args...) - cArgs := make([]*c.Char, len(config.Args)) - for i, arg := range config.Args { + cArgs := make([]*c.Char, len(allArgs)) + for i, arg := range allArgs { cArgs[i] = c.AllocaCStr(arg) } @@ -418,6 +423,7 @@ func (ct *Converter) GenerateAnonymousName(cursor clang.Cursor) string { } // converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. +// todo(zzy): manglename (c++) func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() defer name.Dispose() diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go index f879a181..a72e16b0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/cvt.go @@ -13,8 +13,9 @@ import ( func RunTest(testName string, testCases []string) { for i, content := range testCases { converter, err := parse.NewConverter(&parse.Config{ - File: content, - Temp: true, + File: content, + Temp: true, + IsCpp: true, }) if err != nil { panic(err) @@ -45,9 +46,16 @@ type GetTypeOptions struct { ExpectTypeKind clang.TypeKind // Args contains additional compilation arguments passed to Clang (optional) - // Default is []string{"-x", "c++", "-std=c++11"} - // *For complex C types, C language args Must be specified, e.g., []string{"-x", "c", "-std=c99"} + // These are appended after the default language-specific arguments + // Example: []string{"-std=c++11"} Args []string + + // IsCpp indicates whether the code should be treated as C++ (true) or C (false) + // This affects the default language arguments passed to Clang: + // - For C++: []string{"-x", "c++"} + // - For C: []string{"-x", "c"} + // *For complex C types, C Must be specified + IsCpp bool } // GetType returns the clang.Type of the given type code @@ -56,9 +64,10 @@ type GetTypeOptions struct { func GetType(option *GetTypeOptions) (clang.Type, *clang.Index, *clang.TranslationUnit) { code := fmt.Sprintf("%s placeholder;", option.TypeCode) index, unit, err := parse.CreateTranslationUnit(&parse.Config{ - File: code, - Temp: true, - Args: option.Args, + File: code, + Temp: true, + Args: option.Args, + IsCpp: option.IsCpp, }) if err != nil { panic(err) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index 3d33814e..03a5771f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -128,6 +128,7 @@ func TestNonBuiltinTypes() { for _, t := range tests { typ, index, unit := test.GetType(&test.GetTypeOptions{ TypeCode: t, + IsCpp: true, }) converter := &parse.Converter{} expr := converter.ProcessType(typ) @@ -167,7 +168,7 @@ func getComplexType(flag ast.TypeFlag) clang.Type { typ, _, _ := test.GetType(&test.GetTypeOptions{ TypeCode: code, ExpectTypeKind: clang.TypeComplex, - Args: []string{"-x", "c", "-std=c99"}, + IsCpp: false, }) return typ diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index ee42da75..b86281e1 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -10,11 +10,13 @@ import ( type Context struct { Files map[string]*ast.File + IsCpp bool } -func NewContext() *Context { +func NewContext(isCpp bool) *Context { return &Context{ Files: make(map[string]*ast.File), + IsCpp: isCpp, } } @@ -60,8 +62,9 @@ func (p *Context) processFile(path string) error { func (p *Context) parseFile(path string) (map[string]*ast.File, error) { converter, err := NewConverter(&Config{ - File: path, - Temp: false, + File: path, + Temp: false, + IsCpp: p.IsCpp, }) if err != nil { return nil, errors.New("failed to create converter " + path) From b9aaba7b1626fc13de8b7dcdc91ec056b2f1ddc5 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 6 Sep 2024 15:28:17 +0800 Subject: [PATCH 59/67] llcppsigfetch:fix nil type of FuncNoProto in C mode --- chore/_xtool/llcppsigfetch/parse/cvt.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index e2ca9344..3da93309 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -289,7 +289,6 @@ func (ct *Converter) Convert() (map[string]*ast.File, error) { } func (ct *Converter) ProcessType(t clang.Type) ast.Expr { - if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { return ct.ProcessBuiltinType(t) } @@ -306,7 +305,8 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeRValueReference: expr = &ast.RvalueRefType{X: ct.ProcessType(t.NonReferenceType())} - case clang.TypeFunctionProto: + case clang.TypeFunctionProto, clang.TypeFunctionNoProto: + // treating TypeFunctionNoProto as a general function without parameters // function type will only collect return type, params will be collected in ProcessFuncDecl expr = ct.ProcessFunctionType(t) case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: From b369321e2f0a63551685a30b040e45c271d6f96b Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 6 Sep 2024 17:27:00 +0800 Subject: [PATCH 60/67] llcppsigfetch:unexpect typedef record type's named anonymous record decl resolve to normal anonymous record refer --- chore/_xtool/llcppsigfetch/parse/cvt.go | 60 +-- .../decl_test/typedef_test/llgo.expect | 382 ++++++++++++------ 2 files changed, 268 insertions(+), 174 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 3da93309..c575c6a0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -40,7 +40,7 @@ type Converter struct { // // Example: // typedef struct { int x; } MyStruct; - anonyTypeMap map[string]string // cursorUsr -> anonyname + anonyTypeMap map[string]bool // cursorUsr } var tagMap = map[string]ast.Tag{ @@ -67,7 +67,7 @@ func NewConverter(config *Config) (*Converter, error) { Files: make(map[string]*ast.File), index: index, unit: unit, - anonyTypeMap: make(map[string]string), + anonyTypeMap: make(map[string]bool), typeDecls: make(map[string]ast.Decl), }, nil } @@ -181,19 +181,19 @@ func (ct *Converter) GetCurFile() *ast.File { return file } -func (ct *Converter) SetAnonyType(cursor clang.Cursor, anonyname string) { +func (ct *Converter) SetAnonyType(cursor clang.Cursor) { usr := cursor.USR() usrStr := c.GoString(usr.CStr()) defer usr.Dispose() - ct.anonyTypeMap[usrStr] = anonyname + ct.anonyTypeMap[usrStr] = true } -func (ct *Converter) GetAnonyType(cursor clang.Cursor) (string, bool) { +func (ct *Converter) GetAnonyType(cursor clang.Cursor) (bool, bool) { usr := cursor.USR() usrStr := c.GoString(usr.CStr()) defer usr.Dispose() - anonyname, ok := ct.anonyTypeMap[usrStr] - return anonyname, ok + isAnony, ok := ct.anonyTypeMap[usrStr] + return isAnony, ok } func (ct *Converter) SetTypeDecl(cursor clang.Cursor, decl ast.Decl) { @@ -379,7 +379,6 @@ func (ct *Converter) ProcessUnderlyingType(cursor clang.Cursor) ast.Expr { return ct.ProcessType(underlyingTyp) } - typ := ct.ProcessElaboratedType(underlyingTyp) referTypeCursor := underlyingTyp.TypeDeclaration() // If the type decl for the reference already exists in anonyTypeMap @@ -389,19 +388,17 @@ func (ct *Converter) ProcessUnderlyingType(cursor clang.Cursor) ast.Expr { // In this case, libclang incorrectly reports an anonymous struct as a named struct sourceCode := ct.GetTokens(referTypeCursor) if isAnonymousStructure(sourceCode) { - anonyName := ct.GenerateAnonymousName(referTypeCursor) - // update reference's name - setInnerName(typ, anonyName) - ct.SetAnonyType(referTypeCursor, anonyName) + ct.SetAnonyType(referTypeCursor) typ, isValidType := ct.GetTypeDecl(referTypeCursor) if isValidType { // There will be no anonymous classes,here will execute enum,union,struct + // according to a normal anonymous decl switch declType := typ.(type) { case *ast.EnumTypeDecl: - declType.Name.Name = anonyName + declType.Name = nil case *ast.TypeDecl: if declType.Type.Tag != ast.Class { - declType.Name.Name = anonyName + declType.Name = nil } else { // Unreachable: There should be no anonymous classes in this context fmt.Fprintln(os.Stderr, "unexpect typedef anonymous class %s", declType.Name.Name) @@ -414,12 +411,7 @@ func (ct *Converter) ProcessUnderlyingType(cursor clang.Cursor) ast.Expr { } } - return typ -} - -// generates a name for an anonymous structure like:__ANONY_A_B_MYSTRUCT -func (ct *Converter) GenerateAnonymousName(cursor clang.Cursor) string { - return fmt.Sprintf("__ANONY_%s", strings.Join(ct.BuildScopingParts(cursor), "_")) + return ct.ProcessElaboratedType(underlyingTyp) } // converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. @@ -730,7 +722,9 @@ func (ct *Converter) ProcessElaboratedType(t clang.Type) ast.Expr { defer name.Dispose() decl := t.TypeDeclaration() - if decl.IsAnonymous() != 0 { + isAnony, ok := ct.GetAnonyType(decl) + + if decl.IsAnonymous() != 0 || isAnony && ok { // anonymous type refer (except anonymous RecordType&EnumType in TypedefDecl) if decl.Kind == clang.CursorEnumDecl { return ct.ProcessEnumType(decl) @@ -836,17 +830,11 @@ func (ct *Converter) BuildScopingParts(cursor clang.Cursor) []string { var parts []string // Traverse up the semantic parents for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit { - var qualified string - // anonymous decl quote - if anonyName, ok := ct.GetAnonyType(cursor); ok { - qualified = anonyName - } else { - name := cursor.String() - qualified = c.GoString(name.CStr()) - name.Dispose() - } + name := cursor.String() + qualified := c.GoString(name.CStr()) parts = append([]string{qualified}, parts...) cursor = cursor.SemanticParent() + name.Dispose() } return parts } @@ -911,18 +899,6 @@ func getOffset(location clang.SourceLocation) c.Uint { return offset } -// setTagName updates the name of the innermost Ident in a Name Expr -func setInnerName(expr ast.Expr, name string) { - switch e := expr.(type) { - case *ast.TagExpr: - setInnerName(e.Name, name) - case *ast.ScopingExpr: - setInnerName(e.X, name) - case *ast.Ident: - e.Name = name - } -} - // checks if the source code represents an actual anonymous structure func isAnonymousStructure(sourceCode []*ast.Token) bool { _, isValidTag := tagMap[sourceCode[0].Lit] diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 9e527797..43bb1f8e 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -484,10 +484,7 @@ TestTypeDefDecl Case 7: }, "Doc": null, "Parent": null, - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" - }, + "Name": null, "Type": { "_Type": "RecordType", "Tag": 0, @@ -525,12 +522,28 @@ TestTypeDefDecl Case 7: "Name": "MyStruct" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } }], "includes": [], @@ -550,10 +563,7 @@ TestTypeDefDecl Case 8: }, "Doc": null, "Parent": null, - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyUnion" - }, + "Name": null, "Type": { "_Type": "RecordType", "Tag": 1, @@ -591,12 +601,28 @@ TestTypeDefDecl Case 8: "Name": "MyUnion" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyUnion" + "_Type": "RecordType", + "Tag": 1, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 1 + "Methods": [] } }], "includes": [], @@ -616,10 +642,7 @@ TestTypeDefDecl Case 9: }, "Doc": null, "Parent": null, - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyEnum" - }, + "Name": null, "Type": { "_Type": "EnumType", "Items": [{ @@ -670,12 +693,41 @@ TestTypeDefDecl Case 9: "Name": "MyEnum" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyEnum" - }, - "Tag": 2 + "_Type": "EnumType", + "Items": [{ + "_Type": "EnumItem", + "Name": { + "_Type": "Ident", + "Name": "RED" + }, + "Value": { + "_Type": "BasicLit", + "Kind": 0, + "Value": "0" + } + }, { + "_Type": "EnumItem", + "Name": { + "_Type": "Ident", + "Name": "GREEN" + }, + "Value": { + "_Type": "BasicLit", + "Kind": 0, + "Value": "1" + } + }, { + "_Type": "EnumItem", + "Name": { + "_Type": "Ident", + "Name": "BLUE" + }, + "Value": { + "_Type": "BasicLit", + "Kind": 0, + "Value": "2" + } + }] } }], "includes": [], @@ -695,10 +747,7 @@ TestTypeDefDecl Case 10: }, "Doc": null, "Parent": null, - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" - }, + "Name": null, "Type": { "_Type": "RecordType", "Tag": 0, @@ -736,12 +785,28 @@ TestTypeDefDecl Case 10: "Name": "MyStruct" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } }, { "_Type": "TypedefDecl", @@ -756,12 +821,28 @@ TestTypeDefDecl Case 10: "Name": "MyStruct2" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } }, { "_Type": "TypedefDecl", @@ -778,12 +859,28 @@ TestTypeDefDecl Case 10: "Type": { "_Type": "PointerType", "X": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } } }, { @@ -801,12 +898,28 @@ TestTypeDefDecl Case 10: "Type": { "_Type": "ArrayType", "Elt": { - "_Type": "TagExpr", - "Name": { - "_Type": "Ident", - "Name": "__ANONY_MyStruct" + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] }, "Len": null } @@ -838,10 +951,7 @@ TestTypeDefDecl Case 11: "Name": "A" } }, - "Name": { - "_Type": "Ident", - "Name": "__ANONY_A_B_MyStruct" - }, + "Name": null, "Type": { "_Type": "RecordType", "Tag": 0, @@ -889,26 +999,28 @@ TestTypeDefDecl Case 11: "Name": "MyStruct" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "__ANONY_A_B_MyStruct" - }, - "Parent": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "B" - }, - "Parent": { - "_Type": "Ident", - "Name": "A" - } - } + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } }, { "_Type": "TypedefDecl", @@ -933,26 +1045,28 @@ TestTypeDefDecl Case 11: "Name": "MyStruct2" }, "Type": { - "_Type": "TagExpr", - "Name": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "__ANONY_A_B_MyStruct" - }, - "Parent": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "B" - }, - "Parent": { - "_Type": "Ident", - "Name": "A" - } - } + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } }, { "_Type": "TypedefDecl", @@ -979,26 +1093,28 @@ TestTypeDefDecl Case 11: "Type": { "_Type": "PointerType", "X": { - "_Type": "TagExpr", - "Name": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "__ANONY_A_B_MyStruct" - }, - "Parent": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "B" - }, - "Parent": { - "_Type": "Ident", - "Name": "A" - } - } + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] } } }, { @@ -1026,26 +1142,28 @@ TestTypeDefDecl Case 11: "Type": { "_Type": "ArrayType", "Elt": { - "_Type": "TagExpr", - "Name": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "__ANONY_A_B_MyStruct" - }, - "Parent": { - "_Type": "ScopingExpr", - "X": { - "_Type": "Ident", - "Name": "B" - }, - "Parent": { - "_Type": "Ident", - "Name": "A" - } - } + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }] }, - "Tag": 0 + "Methods": [] }, "Len": null } From 140352b6378c1918d59bd456fd40f5517794a625 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 9 Sep 2024 16:34:26 +0800 Subject: [PATCH 61/67] llcppsigfetch:avoid tab --- .../decl_test/comment_test/comment.go | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go index 154a9c5d..faa3fe70 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go @@ -8,26 +8,35 @@ func main() { func TestDoc() { testCases := []string{ - `// not read doc 1 - void foo();`, - `/* not read doc 2 */ - void foo();`, - `/// doc - void foo();`, - `/** doc */ - void foo();`, - `/*! doc */ - void foo();`, - `/// doc 1 + ` +// not read doc 1 +void foo();`, + ` +/* not read doc 2 */ +void foo();`, + ` +/// doc +void foo();`, + ` +/** doc */ +void foo();`, + ` +/*! doc */ +void foo();`, + ` +/// doc 1 /// doc 2 void foo();`, - `/*! doc 1 */ + ` +/*! doc 1 */ /*! doc 2 */ void foo();`, - `/** doc 1 */ + ` +/** doc 1 */ /** doc 1 */ void foo();`, - `/** + ` +/** * doc 1 * doc 2 */ From ee5cd06077ce3e9f1903e440045a40b16a7eff53 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 9 Sep 2024 18:33:13 +0800 Subject: [PATCH 62/67] llcppsigfetch:distinguish collect doc&comment --- chore/_xtool/llcppsigfetch/parse/cvt.go | 79 +++-- .../decl_test/comment_test/comment.go | 32 ++ .../decl_test/comment_test/llgo.expect | 289 ++++++++++++++++++ 3 files changed, 371 insertions(+), 29 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index c575c6a0..83fb4e99 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -212,23 +212,42 @@ func (ct *Converter) GetTypeDecl(cursor clang.Cursor) (ast.Decl, bool) { } func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { - rawComment := cursor.RawCommentText() - defer rawComment.Dispose() - - res := ast.DeclBase{ + base := ast.DeclBase{ Loc: &ct.curLoc, Parent: ct.BuildScopingExpr(cursor.SemanticParent()), } + commentGroup, isDoc := ct.ParseCommentGroup(cursor) + if isDoc { + base.Doc = commentGroup + } + return base +} +// extracts and parses comments associated with a given Clang cursor, +// distinguishing between documentation comments and line comments. +// It uses libclang to parse only Doxygen-style comments. + +// Reference for Doxygen documentation blocks: https://www.doxygen.nl/manual/docblocks.html + +// The function determines whether a comment is a documentation comment or a line comment by +// comparing the range of the comment node with the range of the declaration node in the AST. + +// Note: In cases where both documentation comments and line comments conceptually exist, +// only the line comment will be preserved. +func (ct *Converter) ParseCommentGroup(cursor clang.Cursor) (comentGroup *ast.CommentGroup, isDoc bool) { + rawComment := cursor.RawCommentText() + defer rawComment.Dispose() commentGroup := &ast.CommentGroup{} if rawComment.CStr() != nil { + commentRange := cursor.CommentRange() + cursorRange := cursor.Extent() + isDoc := getOffset(commentRange.RangeStart()) < getOffset(cursorRange.RangeStart()) commentGroup = ct.ParseComment(c.GoString(rawComment.CStr())) if len(commentGroup.List) > 0 { - res.Doc = commentGroup + return commentGroup, isDoc } } - - return res + return nil, false } func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { @@ -560,13 +579,31 @@ type visitFieldContext struct { converter *Converter } +func (p *visitFieldContext) createBaseField(cursor clang.Cursor) *ast.Field { + field := &ast.Field{ + Type: p.converter.ProcessType(cursor.Type()), + } + fieldName := cursor.String() + defer fieldName.Dispose() + + commentGroup, isDoc := p.converter.ParseCommentGroup(cursor) + if commentGroup != nil { + if isDoc { + field.Doc = commentGroup + } else { + field.Comment = commentGroup + } + } + if name := fieldName.CStr(); name != nil { + field.Names = []*ast.Ident{{Name: c.GoString(name)}} + } + return field +} func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ctx := (*visitFieldContext)(clientData) switch cursor.Kind { case clang.CursorParmDecl, clang.CursorFieldDecl: - paramName := cursor.String() - defer paramName.Dispose() // In C language, parameter lists do not have similar parameter grouping in Go. // func foo(a, b int) @@ -574,15 +611,7 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan // struct A { // int a, b; // }; - field := &ast.Field{ - // todo(zzy):comment & doc - Type: ctx.converter.ProcessType(cursor.Type()), - } - - if paramName.CStr() != nil { - field.Names = []*ast.Ident{{Name: c.GoString(paramName.CStr())}} - } - + field := ctx.createBaseField(cursor) if cursor.Kind == clang.CursorFieldDecl { field.Access = ast.AccessSpecifier(cursor.CXXAccessSpecifier()) } @@ -592,17 +621,9 @@ func visitFieldList(cursor, parent clang.Cursor, clientData unsafe.Pointer) clan case clang.CursorVarDecl: if cursor.StorageClass() == clang.SCStatic { // static member variable - fieldname := cursor.String() - defer fieldname.Dispose() - //todo(zzy): comment & doc - field := &ast.Field{ - Type: ctx.converter.ProcessType(cursor.Type()), - Access: ast.AccessSpecifier(cursor.CXXAccessSpecifier()), - IsStatic: true, - } - if fieldname.CStr() != nil && c.GoString(fieldname.CStr()) != "" { - field.Names = []*ast.Ident{{Name: c.GoString(fieldname.CStr())}} - } + field := ctx.createBaseField(cursor) + field.Access = ast.AccessSpecifier(cursor.CXXAccessSpecifier()) + field.IsStatic = true ctx.params.List = append(ctx.params.List, field) } } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go index faa3fe70..0747e142 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/comment.go @@ -41,6 +41,38 @@ void foo();`, * doc 2 */ void foo();`, + ` + struct Foo { + /// doc + int x; + int y; ///< comment + /** + * field doc (parse ignore with comment in same cursor) + */ + int z; /*!< comment */ + }; + `, + ` +class Doc +{ + public: + /** + * static field doc + */ + static int x; + static int y; /*!< static field comment */ + /** + * field doc + */ + int a; + int b; ///< field comment + /** + * method doc + */ + void Foo(); + protected: + int value; /*!< protected field comment */ +};`, } test.RunTest("TestDoc", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect index a07cf51f..2b11b87f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -437,6 +437,295 @@ TestDoc Case 9: } } +TestDoc Case 10: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "Foo" + }, + "Type": { + "_Type": "RecordType", + "Tag": 0, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/// doc" + }] + }, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "///< comment" + }] + }, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "y" + }] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/*!< comment */" + }] + }, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "z" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + +TestDoc Case 11: +{ + "temp.h": { + "_Type": "File", + "decls": [{ + "_Type": "TypeDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": null, + "Parent": null, + "Name": { + "_Type": "Ident", + "Name": "Doc" + }, + "Type": { + "_Type": "RecordType", + "Tag": 3, + "Fields": { + "_Type": "FieldList", + "List": [{ + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/** " + }, { + "_Type": "Comment", + "Text": " * static field doc" + }, { + "_Type": "Comment", + "Text": " */" + }] + }, + "Comment": null, + "IsStatic": true, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "x" + }] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/*!< static field comment */" + }] + }, + "IsStatic": true, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "y" + }] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/** " + }, { + "_Type": "Comment", + "Text": " * field doc" + }, { + "_Type": "Comment", + "Text": " */" + }] + }, + "Comment": null, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "a" + }] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "///< field comment" + }] + }, + "IsStatic": false, + "Access": 1, + "Names": [{ + "_Type": "Ident", + "Name": "b" + }] + }, { + "_Type": "Field", + "Type": { + "_Type": "BuiltinType", + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/*!< protected field comment */" + }] + }, + "IsStatic": false, + "Access": 2, + "Names": [{ + "_Type": "Ident", + "Name": "value" + }] + }] + }, + "Methods": [{ + "_Type": "FuncDecl", + "Loc": { + "_Type": "Location", + "File": "temp.h" + }, + "Doc": { + "_Type": "CommentGroup", + "List": [{ + "_Type": "Comment", + "Text": "/** " + }, { + "_Type": "Comment", + "Text": " * method doc" + }, { + "_Type": "Comment", + "Text": " */" + }] + }, + "Parent": { + "_Type": "Ident", + "Name": "Doc" + }, + "Name": { + "_Type": "Ident", + "Name": "Foo" + }, + "Type": { + "_Type": "FuncType", + "Params": { + "_Type": "FieldList", + "List": null + }, + "Ret": { + "_Type": "BuiltinType", + "Kind": 0, + "Flags": 0 + } + }, + "IsInline": false, + "IsStatic": false, + "IsConst": false, + "IsExplicit": false, + "IsConstructor": false, + "IsDestructor": false, + "IsVirtual": false, + "IsOverride": false + }] + } + }], + "includes": [], + "macros": [] + } +} + #stderr From c8e06b58370d480daace4b5672570dd2c67f778c Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 10 Sep 2024 12:04:46 +0800 Subject: [PATCH 63/67] llcppsigfetch:func mangled name --- chore/_xtool/llcppsigfetch/parse/cvt.go | 16 ++++++++++++---- .../cvt_test/decl_test/class_test/llgo.expect | 12 ++++++++++++ .../cvt_test/decl_test/comment_test/llgo.expect | 10 ++++++++++ .../cvt_test/decl_test/func_test/llgo.expect | 5 +++++ .../cvt_test/decl_test/scope_test/llgo.expect | 5 +++++ chore/_xtool/llcppsigfetch/parse/dump.go | 1 + 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 83fb4e99..d6540994 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -434,10 +434,11 @@ func (ct *Converter) ProcessUnderlyingType(cursor clang.Cursor) ast.Expr { } // converts functions, methods, constructors, destructors (including out-of-class decl) to ast.FuncDecl nodes. -// todo(zzy): manglename (c++) func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { name := cursor.String() + mangledName := cursor.Mangling() defer name.Dispose() + defer mangledName.Dispose() // function type will only collect return type // ProcessType can't get the field names,will collect in follows @@ -448,10 +449,17 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { } params := ct.ProcessFieldList(cursor) funcType.Params = params + + mangledNameStr := c.GoString(mangledName.CStr()) + if len(mangledNameStr) >= 1 && mangledNameStr[0] == '_' { + mangledNameStr = mangledNameStr[1:] + } + funcDecl := &ast.FuncDecl{ - DeclBase: ct.CreateDeclBase(cursor), - Name: &ast.Ident{Name: c.GoString(name.CStr())}, - Type: funcType, + DeclBase: ct.CreateDeclBase(cursor), + Name: &ast.Ident{Name: c.GoString(name.CStr())}, + Type: funcType, + MangledName: mangledNameStr, } if cursor.IsFunctionInlined() != 0 { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect index 2b13b4e1..c75368df 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/class_test/llgo.expect @@ -128,6 +128,7 @@ TestClassDecl Case 2: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN1A3fooEid", "Type": { "_Type": "FuncType", "Params": { @@ -224,6 +225,7 @@ TestClassDecl Case 3: "_Type": "Ident", "Name": "A" }, + "MangledName": "_ZN1AC1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -259,6 +261,7 @@ TestClassDecl Case 3: "_Type": "Ident", "Name": "A" }, + "MangledName": "_ZN1AC1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -294,6 +297,7 @@ TestClassDecl Case 3: "_Type": "Ident", "Name": "~A" }, + "MangledName": "_ZN1AD1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -329,6 +333,7 @@ TestClassDecl Case 3: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN1A3fooEv", "Type": { "_Type": "FuncType", "Params": { @@ -395,6 +400,7 @@ TestClassDecl Case 4: "_Type": "Ident", "Name": "Base" }, + "MangledName": "_ZN4BaseC1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -430,6 +436,7 @@ TestClassDecl Case 4: "_Type": "Ident", "Name": "~Base" }, + "MangledName": "_ZN4BaseD1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -465,6 +472,7 @@ TestClassDecl Case 4: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN4Base3fooEv", "Type": { "_Type": "FuncType", "Params": { @@ -521,6 +529,7 @@ TestClassDecl Case 4: "_Type": "Ident", "Name": "Derived" }, + "MangledName": "_ZN7DerivedC1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -556,6 +565,7 @@ TestClassDecl Case 4: "_Type": "Ident", "Name": "~Derived" }, + "MangledName": "_ZN7DerivedD1Ev", "Type": { "_Type": "FuncType", "Params": { @@ -591,6 +601,7 @@ TestClassDecl Case 4: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN7Derived3fooEv", "Type": { "_Type": "FuncType", "Params": { @@ -669,6 +680,7 @@ TestClassDecl Case 5: "_Type": "Ident", "Name": "bar" }, + "MangledName": "_ZN1A3Foo3barEv", "Type": { "_Type": "FuncType", "Params": { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect index 2b11b87f..0a08f88d 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/comment_test/llgo.expect @@ -15,6 +15,7 @@ TestDoc Case 1: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -57,6 +58,7 @@ TestDoc Case 2: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -105,6 +107,7 @@ TestDoc Case 3: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -153,6 +156,7 @@ TestDoc Case 4: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -201,6 +205,7 @@ TestDoc Case 5: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -252,6 +257,7 @@ TestDoc Case 6: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -303,6 +309,7 @@ TestDoc Case 7: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -354,6 +361,7 @@ TestDoc Case 8: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -411,6 +419,7 @@ TestDoc Case 9: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -698,6 +707,7 @@ TestDoc Case 11: "_Type": "Ident", "Name": "Foo" }, + "MangledName": "_ZN3Doc3FooEv", "Type": { "_Type": "FuncType", "Params": { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 135b4292..3332ed99 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -15,6 +15,7 @@ TestFuncDecl Case 1: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -57,6 +58,7 @@ TestFuncDecl Case 2: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3fooi", "Type": { "_Type": "FuncType", "Params": { @@ -114,6 +116,7 @@ TestFuncDecl Case 3: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3fooiz", "Type": { "_Type": "FuncType", "Params": { @@ -181,6 +184,7 @@ TestFuncDecl Case 4: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3fooid", "Type": { "_Type": "FuncType", "Params": { @@ -256,6 +260,7 @@ TestFuncDecl Case 5: "_Type": "Ident", "Name": "add" }, + "MangledName": "_ZL3addii", "Type": { "_Type": "FuncType", "Params": { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect index 72584baa..66093d7f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/scope_test/llgo.expect @@ -15,6 +15,7 @@ TestScope Case 1: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_Z3foov", "Type": { "_Type": "FuncType", "Params": { @@ -60,6 +61,7 @@ TestScope Case 2: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN1a3fooEv", "Type": { "_Type": "FuncType", "Params": { @@ -112,6 +114,7 @@ TestScope Case 3: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN1a1b3fooEv", "Type": { "_Type": "FuncType", "Params": { @@ -176,6 +179,7 @@ TestScope Case 4: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN1a3fooEv", "Type": { "_Type": "FuncType", "Params": { @@ -252,6 +256,7 @@ TestScope Case 5: "_Type": "Ident", "Name": "foo" }, + "MangledName": "_ZN1a1b3fooEv", "Type": { "_Type": "FuncType", "Params": { diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 932d8995..c235c4f2 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -124,6 +124,7 @@ func MarshalASTDecl(decl ast.Decl) *cjson.JSON { root.SetItem(c.Str("_Type"), stringField("FuncDecl")) MarshalASTDeclBase(d.DeclBase, root) root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name)) + root.SetItem(c.Str("MangledName"), stringField(d.MangledName)) root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type)) root.SetItem(c.Str("IsInline"), boolField(d.IsInline)) root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic)) From bf87b76adbdaae57bf5b2f3792177ea4a5c0a8fe Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Fri, 13 Sep 2024 16:06:06 +0800 Subject: [PATCH 64/67] llcppsigfetch:variadic order --- chore/_xtool/llcppsigfetch/parse/cvt.go | 2 +- .../cvt_test/decl_test/func_test/llgo.expect | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index d6540994..ef32e700 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -646,12 +646,12 @@ func (ct *Converter) ProcessFieldList(cursor clang.Cursor) *ast.FieldList { params: params, converter: ct, } + clang.VisitChildren(cursor, visitFieldList, c.Pointer(ctx)) if (cursor.Kind == clang.CursorFunctionDecl || isMethod(cursor)) && cursor.IsVariadic() != 0 { params.List = append(params.List, &ast.Field{ Type: &ast.Variadic{}, }) } - clang.VisitChildren(cursor, visitFieldList, c.Pointer(ctx)) return params } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect index 3332ed99..383660fb 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/func_test/llgo.expect @@ -122,16 +122,6 @@ TestFuncDecl Case 3: "Params": { "_Type": "FieldList", "List": [{ - "_Type": "Field", - "Type": { - "_Type": "Variadic" - }, - "Doc": null, - "Comment": null, - "IsStatic": false, - "Access": 0, - "Names": null - }, { "_Type": "Field", "Type": { "_Type": "BuiltinType", @@ -146,6 +136,16 @@ TestFuncDecl Case 3: "_Type": "Ident", "Name": "a" }] + }, { + "_Type": "Field", + "Type": { + "_Type": "Variadic" + }, + "Doc": null, + "Comment": null, + "IsStatic": false, + "Access": 0, + "Names": null }] }, "Ret": { From e0cb6d4531a8bbc9766c4ba449d2729ae98e3733 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 19 Sep 2024 15:35:42 +0800 Subject: [PATCH 65/67] llcppsigfetch:output fileset --- chore/_xtool/llcppsigfetch/llcppsigfetch.go | 2 +- chore/_xtool/llcppsigfetch/parse/cvt.go | 4 ++++ chore/_xtool/llcppsigfetch/parse/dump.go | 12 ++++++++++++ chore/_xtool/llcppsigfetch/parse/parse.go | 11 +---------- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 944ce300..61b12cf8 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -137,7 +137,7 @@ func runExtract() { check(err) _, err = converter.Convert() check(err) - result := converter.MarshalASTFiles() + result := converter.MarshalOutputASTFiles() cstr := result.Print() c.Printf(cstr) cjson.FreeCStr(cstr) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index ef32e700..c77863e3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -872,6 +872,10 @@ func (ct *Converter) MarshalASTFiles() *cjson.JSON { return MarshalASTFiles(ct.Files) } +func (ct *Converter) MarshalOutputASTFiles() *cjson.JSON { + return MarshalOutputASTFiles(ct.Files) +} + func IsExplicitSigned(t clang.Type) bool { return t.Kind == clang.TypeCharS || t.Kind == clang.TypeSChar } diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index c235c4f2..553de95c 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -6,6 +6,18 @@ import ( "github.com/goplus/llgo/chore/llcppg/ast" ) +func MarshalOutputASTFiles(files map[string]*ast.File) *cjson.JSON { + root := cjson.Array() + for path, file := range files { + f := cjson.Object() + path := cjson.String(c.AllocaCStr(path)) + f.SetItem(c.Str("path"), path) + f.SetItem(c.Str("doc"), MarshalASTFile(file)) + root.AddItem(f) + } + return root +} + func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON { root := cjson.Object() for path, file := range files { diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index b86281e1..90684c80 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -3,7 +3,6 @@ package parse import ( "errors" - "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/cjson" "github.com/goplus/llgo/chore/llcppg/ast" ) @@ -21,15 +20,7 @@ func NewContext(isCpp bool) *Context { } func (p *Context) Output() *cjson.JSON { - root := cjson.Array() - for path, file := range p.Files { - f := cjson.Object() - path := cjson.String(c.AllocaCStr(path)) - f.SetItem(c.Str("path"), path) - f.SetItem(c.Str("doc"), MarshalASTFile(file)) - root.AddItem(f) - } - return root + return MarshalOutputASTFiles(p.Files) } // ProcessFiles processes the given files and adds them to the context From 2c3d46bb8054432941d2c61ea18417c54c8d8f7b Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 19 Sep 2024 16:49:05 +0800 Subject: [PATCH 66/67] llcppsigfetch:order output --- chore/_xtool/llcppsigfetch/parse/cvt.go | 37 ++++++++++++++--------- chore/_xtool/llcppsigfetch/parse/dump.go | 14 ++++----- chore/_xtool/llcppsigfetch/parse/parse.go | 21 ++++++------- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index c77863e3..26c66fab 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -14,11 +14,17 @@ import ( "github.com/goplus/llgo/chore/llcppg/token" ) +type FileEntry struct { + Path string + Doc *ast.File +} + type Converter struct { - Files map[string]*ast.File - curLoc ast.Location - index *clang.Index - unit *clang.TranslationUnit + Files []*FileEntry + FileOrder []string // todo(zzy): more efficient struct + curLoc ast.Location + index *clang.Index + unit *clang.TranslationUnit typeDecls map[string]ast.Decl // cursorUsr -> ast.Decl @@ -64,7 +70,7 @@ func NewConverter(config *Config) (*Converter, error) { } return &Converter{ - Files: make(map[string]*ast.File), + Files: make([]*FileEntry, 0), index: index, unit: unit, anonyTypeMap: make(map[string]bool), @@ -169,16 +175,19 @@ func (ct *Converter) GetCurFile() *ast.File { if ct.curLoc.File == "" { return nil } - file, ok := ct.Files[ct.curLoc.File] - if !ok { - file = &ast.File{ - Decls: make([]ast.Decl, 0), - Includes: make([]*ast.Include, 0), - Macros: make([]*ast.Macro, 0), + // todo(zzy): more efficient + for i, entry := range ct.Files { + if entry.Path == ct.curLoc.File { + return ct.Files[i].Doc } - ct.Files[ct.curLoc.File] = file } - return file + newDoc := &ast.File{ + Decls: make([]ast.Decl, 0), + Includes: make([]*ast.Include, 0), + Macros: make([]*ast.Macro, 0), + } + ct.Files = append(ct.Files, &FileEntry{Path: ct.curLoc.File, Doc: newDoc}) + return newDoc } func (ct *Converter) SetAnonyType(cursor clang.Cursor) { @@ -300,7 +309,7 @@ func visitTop(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.Chil return clang.ChildVisit_Continue } -func (ct *Converter) Convert() (map[string]*ast.File, error) { +func (ct *Converter) Convert() ([]*FileEntry, error) { cursor := ct.unit.Cursor() // visit top decls (struct,class,function & macro,include) clang.VisitChildren(cursor, visitTop, c.Pointer(ct)) diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 553de95c..bb58a02c 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -6,22 +6,22 @@ import ( "github.com/goplus/llgo/chore/llcppg/ast" ) -func MarshalOutputASTFiles(files map[string]*ast.File) *cjson.JSON { +func MarshalOutputASTFiles(files []*FileEntry) *cjson.JSON { root := cjson.Array() - for path, file := range files { + for _, entry := range files { f := cjson.Object() - path := cjson.String(c.AllocaCStr(path)) + path := cjson.String(c.AllocaCStr(entry.Path)) f.SetItem(c.Str("path"), path) - f.SetItem(c.Str("doc"), MarshalASTFile(file)) + f.SetItem(c.Str("doc"), MarshalASTFile(entry.Doc)) root.AddItem(f) } return root } -func MarshalASTFiles(files map[string]*ast.File) *cjson.JSON { +func MarshalASTFiles(files []*FileEntry) *cjson.JSON { root := cjson.Object() - for path, file := range files { - root.SetItem(c.AllocaCStr(path), MarshalASTFile(file)) + for _, entry := range files { + root.SetItem(c.AllocaCStr(entry.Path), MarshalASTFile(entry.Doc)) } return root } diff --git a/chore/_xtool/llcppsigfetch/parse/parse.go b/chore/_xtool/llcppsigfetch/parse/parse.go index 90684c80..9a1c3978 100644 --- a/chore/_xtool/llcppsigfetch/parse/parse.go +++ b/chore/_xtool/llcppsigfetch/parse/parse.go @@ -4,17 +4,16 @@ import ( "errors" "github.com/goplus/llgo/c/cjson" - "github.com/goplus/llgo/chore/llcppg/ast" ) type Context struct { - Files map[string]*ast.File + Files []*FileEntry IsCpp bool } func NewContext(isCpp bool) *Context { return &Context{ - Files: make(map[string]*ast.File), + Files: make([]*FileEntry, 0), IsCpp: isCpp, } } @@ -35,23 +34,21 @@ func (p *Context) ProcessFiles(files []string) error { // parse file and add it to the context,avoid duplicate parsing func (p *Context) processFile(path string) error { - if _, exists := p.Files[path]; exists { - return nil + for _, entry := range p.Files { + if entry.Path == path { + return nil + } } - parsedfiles, err := p.parseFile(path) + parsedFiles, err := p.parseFile(path) if err != nil { return errors.New("failed to parse file: " + path) } - for path, file := range parsedfiles { - if _, exist := p.Files[path]; !exist { - p.Files[path] = file - } - } + p.Files = append(p.Files, parsedFiles...) return nil } -func (p *Context) parseFile(path string) (map[string]*ast.File, error) { +func (p *Context) parseFile(path string) ([]*FileEntry, error) { converter, err := NewConverter(&Config{ File: path, Temp: false, From c8a064af3eee2a4b4cf9c34f0f7ae88321fff59f Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Sat, 21 Sep 2024 01:52:19 +0800 Subject: [PATCH 67/67] llcppsigfetch:handle unexpect tag --- chore/_xtool/llcppsigfetch/parse/cvt.go | 26 ++++++++++++++---------- chore/_xtool/llcppsigfetch/parse/dump.go | 9 -------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 26c66fab..310adc6c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -181,11 +181,7 @@ func (ct *Converter) GetCurFile() *ast.File { return ct.Files[i].Doc } } - newDoc := &ast.File{ - Decls: make([]ast.Decl, 0), - Includes: make([]*ast.Include, 0), - Macros: make([]*ast.Macro, 0), - } + newDoc := &ast.File{} ct.Files = append(ct.Files, &FileEntry{Path: ct.curLoc.File, Doc: newDoc}) return newDoc } @@ -734,13 +730,8 @@ func (ct *Converter) ProcessClassDecl(cursor clang.Cursor) *ast.TypeDecl { } func (ct *Converter) ProcessRecordType(cursor clang.Cursor) *ast.RecordType { - tagMap := map[clang.CursorKind]ast.Tag{ - clang.CursorStructDecl: ast.Struct, - clang.CursorUnionDecl: ast.Union, - clang.CursorClassDecl: ast.Class, - } return &ast.RecordType{ - Tag: tagMap[cursor.Kind], + Tag: toTag(cursor.Kind), Fields: ct.ProcessFieldList(cursor), Methods: ct.ProcessMethods(cursor), } @@ -896,6 +887,19 @@ func IsExplicitUnsigned(t clang.Type) bool { t.Kind == clang.TypeUInt128 } +func toTag(kind clang.CursorKind) ast.Tag { + switch kind { + case clang.CursorStructDecl: + return ast.Struct + case clang.CursorUnionDecl: + return ast.Union + case clang.CursorClassDecl: + return ast.Class + default: + panic(fmt.Sprintf("Unexpected cursor kind in toTag: %v", kind)) + } +} + func toToken(tok clang.Token) token.Token { if tok.Kind() < clang.Punctuation || tok.Kind() > clang.Comment { return token.ILLEGAL diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index bb58a02c..2606edbe 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -27,9 +27,6 @@ func MarshalASTFiles(files []*FileEntry) *cjson.JSON { } func MarshalDeclList(list []ast.Decl) *cjson.JSON { - if list == nil { - return cjson.Null() - } root := cjson.Array() for _, item := range list { root.AddItem(MarshalASTDecl(item)) @@ -49,9 +46,6 @@ func MarshalFieldList(list []*ast.Field) *cjson.JSON { } func MarshalIncludeList(list []*ast.Include) *cjson.JSON { - if list == nil { - return cjson.Null() - } root := cjson.Array() for _, item := range list { include := cjson.Object() @@ -63,9 +57,6 @@ func MarshalIncludeList(list []*ast.Include) *cjson.JSON { } func MarshalMacroList(list []*ast.Macro) *cjson.JSON { - if list == nil { - return cjson.Null() - } root := cjson.Array() for _, item := range list { macro := cjson.Object()