Skip to content

Commit

Permalink
llcppsigfetch:dump file type & consist test method
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Feb 21, 2025
1 parent e44835e commit 9147fef
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 92 deletions.
29 changes: 4 additions & 25 deletions _xtool/llcppsigfetch/llcppsigfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/goplus/llcppg/_xtool/llcppsymg/args"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/_xtool/llcppsymg/config"
"github.com/goplus/llcppg/_xtool/llcppsymg/config/cfgparse"
"github.com/goplus/llcppg/_xtool/llcppsymg/syspath"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
)
Expand Down Expand Up @@ -154,28 +152,7 @@ func runFromConfig(cfgFile string, useStdin bool, outputToFile bool, verbose boo
os.Exit(1)
}

cflag := cfgparse.ParseCFlags(conf.CFlags)
files, notFounds, err := cflag.GenHeaderFilePaths(conf.Include, syspath.GetIncludePaths())
check(err)

if verbose {
fmt.Fprintln(os.Stderr, "runFromConfig: header file paths", files)
if len(notFounds) > 0 {
fmt.Fprintln(os.Stderr, "runFromConfig: not found header files", notFounds)
}
}

// Generate include directory flags (-I flags)
incFlags := make([]string, 0, len(cflag.Paths))
for _, path := range cflag.Paths {
incFlags = append(incFlags, "-I"+path)
}

context := parse.NewContext(&parse.ContextConfig{
Conf: conf.Config,
IncFlags: incFlags,
})
err = context.ProcessFiles(files)
context, err := parse.Do(conf.Config)
check(err)

outputInfo(context, outputToFile)
Expand All @@ -188,7 +165,9 @@ func runExtract(file string, isTemp bool, isCpp bool, outToFile bool, otherArgs
IsCpp: isCpp,
Temp: isTemp,
}
converter, err := parse.NewConverter(cfg)
converter, err := parse.NewConverter(cfg, &config.PkgHfilesInfo{
Inters: []string{file},
})
check(err)
_, err = converter.Convert()
check(err)
Expand Down
42 changes: 34 additions & 8 deletions _xtool/llcppsigfetch/parse/cvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/goplus/llcppg/_xtool/llcppsigfetch/dbg"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/_xtool/llcppsymg/config"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llcppg/token"
Expand Down Expand Up @@ -41,7 +42,7 @@ type Config struct {
IsCpp bool
}

func NewConverter(config *clangutils.Config) (*Converter, error) {
func NewConverter(config *clangutils.Config, pkgFileInfo *config.PkgHfilesInfo) (*Converter, error) {
if dbg.GetDebugParse() {
fmt.Fprintln(os.Stderr, "NewConverter: config")
fmt.Fprintln(os.Stderr, "config.File", config.File)
Expand All @@ -54,8 +55,7 @@ func NewConverter(config *clangutils.Config) (*Converter, error) {
if err != nil {
return nil, err
}

files := initFileEntries(unit)
files := initFileEntries(unit, pkgFileInfo)

return &Converter{
Files: files,
Expand All @@ -71,7 +71,22 @@ func (ct *Converter) Dispose() {
ct.unit.Dispose()
}

func initFileEntries(unit *clang.TranslationUnit) []*llcppg.FileEntry {
func initFileEntries(unit *clang.TranslationUnit, pkgHfileInfo *config.PkgHfilesInfo) []*llcppg.FileEntry {
inters := make(map[string]struct{})
impls := make(map[string]struct{})
thirds := make(map[string]struct{})
if pkgHfileInfo != nil {
for _, file := range pkgHfileInfo.Inters {
inters[file] = struct{}{}
}
for _, file := range pkgHfileInfo.Impls {
impls[file] = struct{}{}
}
for _, file := range pkgHfileInfo.Thirds {
thirds[file] = struct{}{}
}
}

files := make([]*llcppg.FileEntry, 0)
clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) {
loc := unit.GetLocation(inced, 1, 1)
Expand All @@ -81,11 +96,22 @@ func initFileEntries(unit *clang.TranslationUnit) []*llcppg.FileEntry {
cur := unit.GetCursor(&incins[0])
incPath = toStr(cur.String())
}
var fileType llcppg.FileType
if _, ok := inters[incedFile]; ok {
fileType = llcppg.Inter
} else if _, ok := impls[incedFile]; ok {
fileType = llcppg.Impl
} else if _, ok := thirds[incedFile]; ok {
fileType = llcppg.Third
} else {
panic("unknown file type: " + incedFile)
}
files = append(files, &llcppg.FileEntry{
Path: incedFile,
IncPath: incPath,
IsSys: loc.IsInSystemHeader() != 0,
Doc: &ast.File{},
Path: incedFile,
IncPath: incPath,
IsSys: loc.IsInSystemHeader() != 0,
Doc: &ast.File{},
FileType: fileType,
})
})
return files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
test "github.com/goplus/llcppg/_xtool/llcppsigfetch/parse/cvt_test"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/llcppg"
)

func main() {
Expand All @@ -11,17 +11,15 @@ func main() {
}

func TestForwardDecl() {
test.RunTestWithConfig(&clangutils.Config{
File: "./hfile/forwarddecl.h",
Temp: false,
IsCpp: false,
test.RunTestWithConfig(&llcppg.Config{
Include: []string{"forwarddecl.h"},
CFlags: "-I./hfile",
})
}

func TestForwardDeclCrossFile() {
test.RunTestWithConfig(&clangutils.Config{
File: "./hfile/def.h",
Temp: false,
IsCpp: false,
test.RunTestWithConfig(&llcppg.Config{
Include: []string{"def.h"},
CFlags: "-I./hfile",
})
}
29 changes: 16 additions & 13 deletions _xtool/llcppsigfetch/parse/cvt_test/cvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,46 @@ package cvttest

import (
"fmt"
"os"

"github.com/goplus/llcppg/_xtool/llcppsigfetch/parse"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/c/clang"
)

func RunTest(testName string, testCases []string) {
for i, content := range testCases {
tempIncFile := clangutils.TEMP_FILE
if err := os.WriteFile(tempIncFile, []byte(content), 0644); err != nil {
panic(err)
}
include := []string{tempIncFile}
cflags := "-I./"
c.Printf(c.Str("%s Case %d:\n"), c.AllocaCStr(testName), c.Int(i+1))
RunTestWithConfig(&clangutils.Config{
File: content,
Temp: true,
IsCpp: true,
RunTestWithConfig(&llcppg.Config{
Cplusplus: true,
Include: include,
CFlags: cflags,
})
os.Remove(tempIncFile)
}
}

func RunTestWithConfig(config *clangutils.Config) {
converter, err := parse.NewConverter(config)
if err != nil {
panic(err)
}

_, err = converter.Convert()
func RunTestWithConfig(config *llcppg.Config) {
context, err := parse.Do(config)
if err != nil {
panic(err)
}

result := converter.MarshalASTFiles()
result := parse.MarshalASTFiles(context.FileSet)
str := result.Print()
c.Printf(c.Str("%s\n\n"), str)

cjson.FreeCStr(str)
result.Delete()
converter.Dispose()
}

type GetTypeOptions struct {
Expand Down
45 changes: 21 additions & 24 deletions _xtool/llcppsigfetch/parse/cvt_test/preprocess_test/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/goplus/llcppg/_xtool/llcppsigfetch/parse"
test "github.com/goplus/llcppg/_xtool/llcppsigfetch/parse/cvt_test"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llgo/c"
)

Expand Down Expand Up @@ -39,16 +39,15 @@ func TestInclude() {

func TestInclusionMap() {
fmt.Println("=== TestInclusionMap ===")
converter, err := parse.NewConverter(&clangutils.Config{
File: "#include <sys/types.h>",
Temp: true,
IsCpp: false,
context, err := parse.Do(&llcppg.Config{
Include: []string{"sys.h"},
CFlags: "-I./testdata/sysinc",
})
if err != nil {
panic(err)
}
found := false
for _, f := range converter.Files {
for _, f := range context.FileSet {
if f.IncPath == "sys/types.h" {
found = true
}
Expand All @@ -62,39 +61,38 @@ func TestInclusionMap() {

func TestSystemHeader() {
fmt.Println("=== TestSystemHeader ===")
converter, err := parse.NewConverter(&clangutils.Config{
File: "#include <stdio.h>",
Temp: true,
IsCpp: false,
context, err := parse.Do(&llcppg.Config{
Include: []string{"inc.h"},
CFlags: "-I./testdata/sysinc",
})
if err != nil {
panic(err)
}
converter.Convert()
if len(converter.Files) < 2 {

if len(context.FileSet) < 2 {
panic("expect 2 files")
}
if converter.Files[0].IsSys {
if context.FileSet[0].IsSys {
panic("entry file is not system header")
}

includePath := converter.Files[0].Doc.Includes[0].Path
includePath := context.FileSet[0].Doc.Includes[0].Path
if strings.HasSuffix(includePath, "stdio.h") && filepath.IsAbs(includePath) {
fmt.Println("stdio.h is absolute path")
}

for i := 1; i < len(converter.Files); i++ {
if !converter.Files[i].IsSys {
panic(fmt.Errorf("include file is not system header: %s", converter.Files[i].Path))
for i := 1; i < len(context.FileSet); i++ {
if !context.FileSet[i].IsSys {
panic(fmt.Errorf("include file is not system header: %s", context.FileSet[i].Path))
}
for _, decl := range converter.Files[i].Doc.Decls {
for _, decl := range context.FileSet[i].Doc.Decls {
switch decl := decl.(type) {
case *ast.TypeDecl:
case *ast.EnumTypeDecl:
case *ast.FuncDecl:
case *ast.TypedefDecl:
if decl.DeclBase.Loc.File != converter.Files[i].Path {
fmt.Println("Decl is not in the file", decl.DeclBase.Loc.File, "expect", converter.Files[i].Path)
if decl.DeclBase.Loc.File != context.FileSet[i].Path {
fmt.Println("Decl is not in the file", decl.DeclBase.Loc.File, "expect", context.FileSet[i].Path)
}
}
}
Expand All @@ -104,9 +102,8 @@ func TestSystemHeader() {

func TestMacroExpansionOtherFile() {
c.Printf(c.Str("TestMacroExpansionOtherFile:\n"))
test.RunTestWithConfig(&clangutils.Config{
File: "./testdata/macroexpan/ref.h",
Temp: false,
IsCpp: false,
test.RunTestWithConfig(&llcppg.Config{
Include: []string{"ref.h"},
CFlags: "-I./testdata/macroexpan",
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <stdio.h>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <sys/types.h>
1 change: 1 addition & 0 deletions _xtool/llcppsigfetch/parse/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func MarshalFileSet(files []*llcppg.FileEntry) *cjson.JSON {
f.SetItem(c.Str("path"), path)
f.SetItem(c.Str("doc"), MarshalASTFile(entry.Doc))
f.SetItem(c.Str("isSys"), boolField(entry.IsSys))
f.SetItem(c.Str("fileType"), numberField(uint(entry.FileType)))
root.AddItem(f)
}
return root
Expand Down
Loading

0 comments on commit 9147fef

Please sign in to comment.