Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

llcppg:ast.FileEntry -> llcppg.FileEntry #185

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions _xtool/llcppsigfetch/parse/cvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"github.com/goplus/llcppg/_xtool/llcppsigfetch/dbg"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llcppg/token"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/c/clang"
)

type Converter struct {
Files []*ast.FileEntry
Files []*llcppg.FileEntry
FileOrder []string // todo(zzy): more efficient struct
curLoc ast.Location
index *clang.Index
Expand Down Expand Up @@ -70,8 +71,8 @@ func (ct *Converter) Dispose() {
ct.unit.Dispose()
}

func initFileEntries(unit *clang.TranslationUnit) []*ast.FileEntry {
files := make([]*ast.FileEntry, 0)
func initFileEntries(unit *clang.TranslationUnit) []*llcppg.FileEntry {
files := make([]*llcppg.FileEntry, 0)
clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) {
loc := unit.GetLocation(inced, 1, 1)
incedFile := toStr(inced.FileName())
Expand All @@ -80,7 +81,7 @@ func initFileEntries(unit *clang.TranslationUnit) []*ast.FileEntry {
cur := unit.GetCursor(&incins[0])
incPath = toStr(cur.String())
}
files = append(files, &ast.FileEntry{
files = append(files, &llcppg.FileEntry{
Path: incedFile,
IncPath: incPath,
IsSys: loc.IsInSystemHeader() != 0,
Expand Down Expand Up @@ -161,7 +162,7 @@ func (ct *Converter) GetCurFile(cursor clang.Cursor) *ast.File {
}
}
ct.logln("GetCurFile: Create New ast.File", filePath)
entry := &ast.FileEntry{Path: filePath, Doc: &ast.File{}, IsSys: false}
entry := &llcppg.FileEntry{Path: filePath, Doc: &ast.File{}, IsSys: false}
if loc.IsInSystemHeader() != 0 {
entry.IsSys = true
}
Expand Down Expand Up @@ -300,7 +301,7 @@ func (ct *Converter) visitTop(cursor, parent clang.Cursor) clang.ChildVisitResul
return clang.ChildVisit_Continue
}

func (ct *Converter) Convert() ([]*ast.FileEntry, error) {
func (ct *Converter) Convert() ([]*llcppg.FileEntry, error) {
cursor := ct.unit.Cursor()
// visit top decls (struct,class,function & macro,include)
clangutils.VisitChildren(cursor, ct.visitTop)
Expand Down
5 changes: 3 additions & 2 deletions _xtool/llcppsigfetch/parse/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package parse

import (
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
)

func MarshalFileSet(files []*ast.FileEntry) *cjson.JSON {
func MarshalFileSet(files []*llcppg.FileEntry) *cjson.JSON {
root := cjson.Array()
for _, entry := range files {
f := cjson.Object()
Expand All @@ -22,7 +23,7 @@ func MarshalFileSet(files []*ast.FileEntry) *cjson.JSON {
return root
}

func MarshalASTFiles(files []*ast.FileEntry) *cjson.JSON {
func MarshalASTFiles(files []*llcppg.FileEntry) *cjson.JSON {
root := cjson.Object()
for _, entry := range files {
root.SetItem(c.AllocaCStr(entry.Path), MarshalASTFile(entry.Doc))
Expand Down
7 changes: 3 additions & 4 deletions _xtool/llcppsigfetch/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (

"github.com/goplus/llcppg/_xtool/llcppsigfetch/dbg"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c/cjson"
)

type Context struct {
FileSet []*ast.FileEntry
FileSet []*llcppg.FileEntry
*ContextConfig
}

Expand All @@ -25,7 +24,7 @@ type ContextConfig struct {

func NewContext(cfg *ContextConfig) *Context {
return &Context{
FileSet: make([]*ast.FileEntry, 0),
FileSet: make([]*llcppg.FileEntry, 0),
ContextConfig: &ContextConfig{
Conf: cfg.Conf,
IncFlags: cfg.IncFlags,
Expand Down Expand Up @@ -72,7 +71,7 @@ func (p *Context) processFile(path string) error {
return nil
}

func (p *Context) parseFile(path string) ([]*ast.FileEntry, error) {
func (p *Context) parseFile(path string) ([]*llcppg.FileEntry, error) {
if dbg.GetDebugParse() {
fmt.Fprintln(os.Stderr, "parseFile: path", path)
}
Expand Down
7 changes: 0 additions & 7 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,4 @@ type File struct {
Macros []*Macro `json:"macros,omitempty"`
}

type FileEntry struct {
Path string
IncPath string
IsSys bool
Doc *File
}

// =============================================================================
3 changes: 1 addition & 2 deletions cmd/gogensig/config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sort"
"strings"

"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/cmd/gogensig/unmarshal"
"github.com/goplus/llcppg/llcppg"
)
Expand All @@ -35,7 +34,7 @@ func GetPubFromPath(filePath string) (map[string]string, error) {
return ReadPubFile(filePath)
}

func GetCppgSigfetchFromByte(data []byte) ([]*ast.FileEntry, error) {
func GetCppgSigfetchFromByte(data []byte) ([]*llcppg.FileEntry, error) {
return unmarshal.FileSet(data)
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/gogensig/convert/filesetprocessor/doc_fileset_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"os"
"path/filepath"

"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/cmd/gogensig/config"
"github.com/goplus/llcppg/cmd/gogensig/convert"
"github.com/goplus/llcppg/cmd/gogensig/visitor"
"github.com/goplus/llcppg/llcppg"
)

type DocFileSetProcessor struct {
Expand All @@ -20,7 +20,7 @@
depIncs []string // abs path
}

type Exec func(*ast.FileEntry) error
type Exec func(*llcppg.FileEntry) error

type ProcesserConfig struct {
Exec Exec
Expand All @@ -43,7 +43,7 @@
return p
}

func (p *DocFileSetProcessor) visitFile(path string, files []*ast.FileEntry) {
func (p *DocFileSetProcessor) visitFile(path string, files []*llcppg.FileEntry) {
if _, ok := p.visitedFile[path]; ok {
return
}
Expand All @@ -69,7 +69,7 @@
delete(p.processing, findFile.Path)
}

func (p *DocFileSetProcessor) ProcessFileSet(files []*ast.FileEntry) error {
func (p *DocFileSetProcessor) ProcessFileSet(files []*llcppg.FileEntry) error {
for _, inc := range p.depIncs {
idx := FindEntry(files, inc)
if idx < 0 {
Expand Down Expand Up @@ -103,7 +103,7 @@
}

// FindEntry finds the entry in FileSet. If useIncPath is true, it searches by IncPath, otherwise by Path
func FindEntry(files []*ast.FileEntry, path string) int {
func FindEntry(files []*llcppg.FileEntry, path string) int {
for i, e := range files {
if e.Path == path {
return i
Expand Down Expand Up @@ -139,7 +139,7 @@
incs := astConvert.Pkg.DepIncPaths()

return NewDocFileSetProcessor(&ProcesserConfig{
Exec: func(file *ast.FileEntry) error {
Exec: func(file *llcppg.FileEntry) error {
visitorList.Visit(file.Doc, file.Path, file.IncPath, file.IsSys)
return nil
},
Expand Down Expand Up @@ -167,7 +167,7 @@
incs := astConvert.Pkg.DepIncPaths()

p := NewDocFileSetProcessor(&ProcesserConfig{
Exec: func(file *ast.FileEntry) error {
Exec: func(file *llcppg.FileEntry) error {

Check warning on line 170 in cmd/gogensig/convert/filesetprocessor/doc_fileset_processor.go

View check run for this annotation

Codecov / codecov/patch

cmd/gogensig/convert/filesetprocessor/doc_fileset_processor.go#L170

Added line #L170 was not covered by tests
visitorList.Visit(file.Doc, file.Path, file.IncPath, file.IsSys)
return nil
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/goplus/llcppg/cmd/gogensig/convert"
"github.com/goplus/llcppg/cmd/gogensig/convert/filesetprocessor"
"github.com/goplus/llcppg/cmd/gogensig/visitor"
"github.com/goplus/llcppg/llcppg"
)

func TestProcessValidSigfetchContent(t *testing.T) {
Expand Down Expand Up @@ -89,7 +90,7 @@ func TestProcessFileNotExist(t *testing.T) {
docVisitors := []visitor.DocVisitor{astConvert}
manager := visitor.NewDocVisitorList(docVisitors)
p := filesetprocessor.NewDocFileSetProcessor(&filesetprocessor.ProcesserConfig{
Exec: func(file *ast.FileEntry) error {
Exec: func(file *llcppg.FileEntry) error {
manager.Visit(file.Doc, file.Path, file.IncPath, file.IsSys)
return nil
},
Expand Down Expand Up @@ -126,7 +127,7 @@ func TestProcessInvalidSigfetchContent(t *testing.T) {
docVisitors := []visitor.DocVisitor{astConvert}
manager := visitor.NewDocVisitorList(docVisitors)
p := filesetprocessor.NewDocFileSetProcessor(&filesetprocessor.ProcesserConfig{
Exec: func(file *ast.FileEntry) error {
Exec: func(file *llcppg.FileEntry) error {
manager.Visit(file.Doc, file.Path, file.IncPath, file.IsSys)
return nil
},
Expand All @@ -146,15 +147,15 @@ func TestCustomExec(t *testing.T) {
t.Errorf("%s", "expect panic")
}
}()
file := []*ast.FileEntry{
file := []*llcppg.FileEntry{
{
Path: "/path/to/foo.h",
IsSys: false,
Doc: &ast.File{},
},
}
p := filesetprocessor.NewDocFileSetProcessor(&filesetprocessor.ProcesserConfig{
Exec: func(file *ast.FileEntry) error {
Exec: func(file *llcppg.FileEntry) error {
return errCustomExec
},
})
Expand All @@ -166,7 +167,7 @@ func TestCustomExec(t *testing.T) {

func TestExecOrder(t *testing.T) {
depIncs := []string{"/path/to/int16_t.h"}
fileSet := []*ast.FileEntry{
fileSet := []*llcppg.FileEntry{
{
Path: "/path/to/foo.h",
IncPath: "foo.h",
Expand Down Expand Up @@ -251,7 +252,7 @@ func TestExecOrder(t *testing.T) {
"/path/to/bar.h",
}
p := filesetprocessor.NewDocFileSetProcessor(&filesetprocessor.ProcesserConfig{
Exec: func(file *ast.FileEntry) error {
Exec: func(file *llcppg.FileEntry) error {
processFiles = append(processFiles, file.Path)
return nil
},
Expand Down
11 changes: 6 additions & 5 deletions cmd/gogensig/unmarshal/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"

"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
)

type NodeUnmarshaler func(data []byte) (ast.Node, error)
Expand Down Expand Up @@ -48,22 +49,22 @@ func init() {
}
}

func FileSet(data []byte) ([]*ast.FileEntry, error) {
func FileSet(data []byte) ([]*llcppg.FileEntry, error) {
type fileSetTemp []json.RawMessage

var fileSetData fileSetTemp
if err := json.Unmarshal(data, &fileSetData); err != nil {
return nil, newDeserializeError("FileSet", fileSetData, data, err)
}
fileSet := []*ast.FileEntry{}
fileSet := []*llcppg.FileEntry{}
for _, fileData := range fileSetData {
fileNode, err := Node(fileData)
if err != nil {
return nil, newUnmarshalFieldError("FileSet", fileSetData, "Files", data, err)
}
file, ok := fileNode.(*ast.FileEntry)
file, ok := fileNode.(*llcppg.FileEntry)
if !ok {
return nil, newUnexpectType("FileSet", fileNode, &ast.FileEntry{})
return nil, newUnexpectType("FileSet", fileNode, &llcppg.FileEntry{})
}
fileSet = append(fileSet, file)
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func FileEntry(data []byte) (ast.Node, error) {
return nil, newUnexpectType("FileEntry", docNode, &ast.File{})
}

return &ast.FileEntry{
return &llcppg.FileEntry{
Path: fileEntryData.Path,
IsSys: fileEntryData.IsSys,
IncPath: fileEntryData.IncPath,
Expand Down
5 changes: 3 additions & 2 deletions cmd/gogensig/unmarshal/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/cmd/gogensig/unmarshal"
"github.com/goplus/llcppg/llcppg"
)

func TestUnmarshalNode(t *testing.T) {
Expand Down Expand Up @@ -1511,7 +1512,7 @@ func TestUnmarshalFileSet(t *testing.T) {
}
]`

expected := []*ast.FileEntry{
expected := []*llcppg.FileEntry{
{
Path: "/opt/homebrew/Cellar/inih/58/include/INIReader.h",
IsSys: false,
Expand Down Expand Up @@ -2259,7 +2260,7 @@ func TestUnmarshalFileSetErrors(t *testing.T) {
{
name: "Invalid doc",
input: `[{"_Type": "Token", "Token": 1, "Lit": "test"}]`,
expectedErr: "unmarshal error in FileSet: got *ast.Token, want *ast.FileEntry",
expectedErr: "unmarshal error in FileSet: got *ast.Token, want *llcppg.FileEntry",
},
}

Expand Down
9 changes: 9 additions & 0 deletions llcppg/llcppg.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package llcppg

import "github.com/goplus/llcppg/ast"

const LLCPPG_CFG = "llcppg.cfg"
const LLCPPG_SYMB = "llcppg.symb.json"
const LLCPPG_SIGFETCH = "llcppg.sigfetch.json"
Expand Down Expand Up @@ -42,3 +44,10 @@ type SymbolInfo struct {
CPP string `json:"c++"` // C++ function name
Go string `json:"go"` // Go function name
}

type FileEntry struct {
Path string
IncPath string
IsSys bool
Doc *ast.File
}
Loading