Skip to content

Commit d602099

Browse files
committed
feat(hfile):platform file interface
1 parent cd47a5a commit d602099

6 files changed

Lines changed: 39 additions & 13 deletions

File tree

_xtool/internal/header/header.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@ import (
1111
)
1212

1313
type PkgHfilesInfo struct {
14-
Inters []string // From types.Config.Include
15-
Impls []string // From same root of types.Config.Include
16-
Thirds []string // Not Current Pkg's Files
14+
Inters []string // From types.Config.Include
15+
Impls []string // From same root of types.Config.Include
16+
Thirds []string // Not Current Pkg's Files
17+
PlatDiff []string // Platform Difference Files
1718
}
1819

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

24+
type Config struct {
25+
Includes []string
26+
// todo(zzy):support platform difference files
27+
PlatDiff []string
28+
Args []string
29+
Mix bool
30+
}
31+
2332
// PkgHfileInfo analyzes header files dependencies and categorizes them into three groups:
2433
// 1. Inters: Direct includes from types.Config.Include
2534
// 2. Impls: Header files from the same root directory as Inters
@@ -29,7 +38,7 @@ func (p *PkgHfilesInfo) CurPkgFiles() []string {
2938
// 1. Creating a temporary header file that includes all headers from conf.Include
3039
// 2. Using clang to parse the translation unit and analyze includes
3140
// 3. Categorizing includes based on their inclusion level and path relationship
32-
func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
41+
func PkgHfileInfo(conf *Config) *PkgHfilesInfo {
3342
info := &PkgHfilesInfo{
3443
Inters: []string{},
3544
Impls: []string{},
@@ -43,12 +52,12 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
4352

4453
inters := make(map[string]struct{})
4554
others := []string{} // impl & third
46-
for _, f := range includes {
55+
for _, f := range conf.Includes {
4756
content := "#include <" + f + ">"
4857
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
4958
File: content,
5059
Temp: true,
51-
Args: args,
60+
Args: conf.Args,
5261
})
5362
if err != nil {
5463
panic(err)
@@ -64,11 +73,11 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
6473
index.Dispose()
6574
}
6675

67-
clangtool.ComposeIncludes(includes, outfile.Name())
76+
clangtool.ComposeIncludes(conf.Includes, outfile.Name())
6877
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
6978
File: outfile.Name(),
7079
Temp: false,
71-
Args: args,
80+
Args: conf.Args,
7281
})
7382
defer unit.Dispose()
7483
defer index.Dispose()
@@ -84,7 +93,7 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo {
8493
}
8594
})
8695

87-
if mix {
96+
if conf.Mix {
8897
info.Thirds = others
8998
return info
9099
}

_xtool/internal/header/header_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ func TestPkgHfileInfo(t *testing.T) {
4141

4242
for i, tc := range cases {
4343
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
44-
info := header.PkgHfileInfo(tc.conf.Include, strings.Fields(tc.conf.CFlags), tc.conf.Mix)
44+
info := header.PkgHfileInfo(&header.Config{
45+
Includes: tc.conf.Include,
46+
Args: strings.Fields(tc.conf.CFlags),
47+
Mix: tc.conf.Mix,
48+
})
4549
if !reflect.DeepEqual(info.Inters, tc.want.Inters) {
4650
t.Fatalf("inter expected %v, but got %v", tc.want.Inters, info.Inters)
4751
}

_xtool/llcppsigfetch/internal/parse/parse.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ func Do(conf *Config) error {
117117
// As a solution, the resource directory is externally provided by llcppg.
118118
libclangFlags := []string{"-fparse-all-comments"}
119119

120-
pkgHfiles := header.PkgHfileInfo(conf.Conf.Include, append(libclangFlags, strings.Fields(conf.Conf.CFlags)...), conf.Conf.Mix)
120+
pkgHfiles := header.PkgHfileInfo(&header.Config{
121+
Includes: conf.Conf.Include,
122+
Args: append(libclangFlags, strings.Fields(conf.Conf.CFlags)...),
123+
Mix: conf.Conf.Mix,
124+
})
121125
if debugParse {
122126
fmt.Fprintln(os.Stderr, "interfaces", pkgHfiles.Inters)
123127
fmt.Fprintln(os.Stderr, "implements", pkgHfiles.Impls)

_xtool/llcppsymg/internal/symg/symg.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ func Do(conf *Config) (symbolTable []*llcppg.SymbolInfo, err error) {
4646
return
4747
}
4848

49-
pkgHfiles := header.PkgHfileInfo(conf.Includes, strings.Fields(conf.CFlags), conf.Mix)
49+
pkgHfiles := header.PkgHfileInfo(&header.Config{
50+
Includes: conf.Includes,
51+
Args: strings.Fields(conf.CFlags),
52+
Mix: conf.Mix,
53+
})
5054
if dbgSymbol {
5155
fmt.Println("interfaces", pkgHfiles.Inters)
5256
fmt.Println("implements", pkgHfiles.Impls)

_xtool/llcppsymg/internal/symg/symg_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,11 @@ func TestGen(t *testing.T) {
487487
defer os.Remove(tempFile.Name())
488488
clangtool.ComposeIncludes(cfg.Include, tempFile.Name())
489489

490-
pkgHfileInfo := header.PkgHfileInfo(cfg.Include, strings.Fields(cfg.CFlags), false)
490+
pkgHfileInfo := header.PkgHfileInfo(&header.Config{
491+
Includes: cfg.Include,
492+
Args: strings.Fields(cfg.CFlags),
493+
Mix: false,
494+
})
491495
headerSymbolMap, err := symg.ParseHeaderFile(tempFile.Name(), pkgHfileInfo.CurPkgFiles(), cfg.TrimPrefixes, strings.Fields(cfg.CFlags), cfg.SymMap, cfg.Cplusplus)
492496
if err != nil {
493497
t.Fatal(err)

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
Inter FileType = iota + 1
5252
Impl
5353
Third
54+
Plat
5455
)
5556

5657
type FileInfo struct {

0 commit comments

Comments
 (0)