Skip to content
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
25 changes: 19 additions & 6 deletions _xtool/internal/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ type PkgHfilesInfo struct {
Inters []string // From types.Config.Include
Impls []string // From same root of types.Config.Include
Thirds []string // Not Current Pkg's Files
Plats []string // Platform Difference Files
}

func (p *PkgHfilesInfo) CurPkgFiles() []string {
return append(p.Inters, p.Impls...)
}

type Config struct {
// Includes specifies the header file include paths to be processed.
// These are the paths used in #include directives, such as:
// - "zlib.h"
// - "openssl/ssl.h"
Includes []string
// PlatDiff specifies header file include paths that differ between platforms,these are include paths in Includes.
PlatDiff []string
Comment thread
MeteorsLiu marked this conversation as resolved.
Comment thread
MeteorsLiu marked this conversation as resolved.
Args []string
Mix bool
}

// PkgHfileInfo analyzes header files dependencies and categorizes them into three groups:
// 1. Inters: Direct includes from types.Config.Include
// 2. Impls: Header files from the same root directory as Inters
Expand All @@ -29,7 +42,7 @@ func (p *PkgHfilesInfo) CurPkgFiles() []string {
// 1. Creating a temporary header file that includes all headers from conf.Include
// 2. Using clang to parse the translation unit and analyze includes
// 3. Categorizing includes based on their inclusion level and path relationship
func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
func PkgHfileInfo(conf *Config) *PkgHfilesInfo {
info := &PkgHfilesInfo{
Inters: []string{},
Impls: []string{},
Expand All @@ -43,12 +56,12 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {

inters := make(map[string]struct{})
others := []string{} // impl & third
for _, f := range includes {
for _, f := range conf.Includes {
content := "#include <" + f + ">"
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
File: content,
Temp: true,
Args: args,
Args: conf.Args,
})
if err != nil {
panic(err)
Expand All @@ -64,11 +77,11 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
index.Dispose()
}

clangtool.ComposeIncludes(includes, outfile.Name())
clangtool.ComposeIncludes(conf.Includes, outfile.Name())
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
File: outfile.Name(),
Temp: false,
Args: args,
Args: conf.Args,
})
defer unit.Dispose()
defer index.Dispose()
Expand All @@ -84,7 +97,7 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
}
})

if mix {
if conf.Mix {
info.Thirds = others
return info
}
Expand Down
6 changes: 5 additions & 1 deletion _xtool/internal/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func TestPkgHfileInfo(t *testing.T) {

for i, tc := range cases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
info := header.PkgHfileInfo(tc.conf.Include, strings.Fields(tc.conf.CFlags), tc.conf.Mix)
info := header.PkgHfileInfo(&header.Config{
Includes: tc.conf.Include,
Args: strings.Fields(tc.conf.CFlags),
Mix: tc.conf.Mix,
})
if !reflect.DeepEqual(info.Inters, tc.want.Inters) {
t.Fatalf("inter expected %v, but got %v", tc.want.Inters, info.Inters)
}
Expand Down
6 changes: 5 additions & 1 deletion _xtool/llcppsigfetch/internal/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ func Do(conf *Config) error {
// As a solution, the resource directory is externally provided by llcppg.
libclangFlags := []string{"-fparse-all-comments"}

pkgHfiles := header.PkgHfileInfo(conf.Conf.Include, append(libclangFlags, strings.Fields(conf.Conf.CFlags)...), conf.Conf.Mix)
pkgHfiles := header.PkgHfileInfo(&header.Config{
Includes: conf.Conf.Include,
Args: append(libclangFlags, strings.Fields(conf.Conf.CFlags)...),
Mix: conf.Conf.Mix,
})
if debugParse {
fmt.Fprintln(os.Stderr, "interfaces", pkgHfiles.Inters)
fmt.Fprintln(os.Stderr, "implements", pkgHfiles.Impls)
Expand Down
6 changes: 5 additions & 1 deletion _xtool/llcppsymg/internal/symg/symg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func Do(conf *Config) (symbolTable []*llcppg.SymbolInfo, err error) {
return
}

pkgHfiles := header.PkgHfileInfo(conf.Includes, strings.Fields(conf.CFlags), conf.Mix)
pkgHfiles := header.PkgHfileInfo(&header.Config{
Includes: conf.Includes,
Args: strings.Fields(conf.CFlags),
Mix: conf.Mix,
})
if dbgSymbol {
fmt.Println("interfaces", pkgHfiles.Inters)
fmt.Println("implements", pkgHfiles.Impls)
Expand Down
6 changes: 5 additions & 1 deletion _xtool/llcppsymg/internal/symg/symg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ func TestGen(t *testing.T) {
defer os.Remove(tempFile.Name())
clangtool.ComposeIncludes(cfg.Include, tempFile.Name())

pkgHfileInfo := header.PkgHfileInfo(cfg.Include, strings.Fields(cfg.CFlags), false)
pkgHfileInfo := header.PkgHfileInfo(&header.Config{
Includes: cfg.Include,
Args: strings.Fields(cfg.CFlags),
Mix: false,
})
headerSymbolMap, err := symg.ParseHeaderFile(tempFile.Name(), pkgHfileInfo.CurPkgFiles(), cfg.TrimPrefixes, strings.Fields(cfg.CFlags), cfg.SymMap, cfg.Cplusplus)
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
Inter FileType = iota + 1
Impl
Third
Plat
)

type FileInfo struct {
Expand Down
Loading