From d60209975327b9ecedabe06e5ce6ab28500c37d2 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 25 Jun 2025 15:57:53 +0800 Subject: [PATCH 1/2] feat(hfile):platform file interface --- _xtool/internal/header/header.go | 27 +++++++++++++------- _xtool/internal/header/header_test.go | 6 ++++- _xtool/llcppsigfetch/internal/parse/parse.go | 6 ++++- _xtool/llcppsymg/internal/symg/symg.go | 6 ++++- _xtool/llcppsymg/internal/symg/symg_test.go | 6 ++++- config/config.go | 1 + 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/_xtool/internal/header/header.go b/_xtool/internal/header/header.go index f753711c..371233f1 100644 --- a/_xtool/internal/header/header.go +++ b/_xtool/internal/header/header.go @@ -11,15 +11,24 @@ import ( ) 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 + Inters []string // From types.Config.Include + Impls []string // From same root of types.Config.Include + Thirds []string // Not Current Pkg's Files + PlatDiff []string // Platform Difference Files } func (p *PkgHfilesInfo) CurPkgFiles() []string { return append(p.Inters, p.Impls...) } +type Config struct { + Includes []string + // todo(zzy):support platform difference files + PlatDiff []string + 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 @@ -29,7 +38,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{}, @@ -43,12 +52,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) @@ -64,11 +73,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() @@ -84,7 +93,7 @@ func PkgHfileInfo(includes []string, args []string, mix bool) *PkgHfilesInfo { } }) - if mix { + if conf.Mix { info.Thirds = others return info } diff --git a/_xtool/internal/header/header_test.go b/_xtool/internal/header/header_test.go index 6c288289..293ddcd2 100644 --- a/_xtool/internal/header/header_test.go +++ b/_xtool/internal/header/header_test.go @@ -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) } diff --git a/_xtool/llcppsigfetch/internal/parse/parse.go b/_xtool/llcppsigfetch/internal/parse/parse.go index 9d97dc1e..ad0a5990 100644 --- a/_xtool/llcppsigfetch/internal/parse/parse.go +++ b/_xtool/llcppsigfetch/internal/parse/parse.go @@ -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) diff --git a/_xtool/llcppsymg/internal/symg/symg.go b/_xtool/llcppsymg/internal/symg/symg.go index 013ac5aa..593c8e04 100644 --- a/_xtool/llcppsymg/internal/symg/symg.go +++ b/_xtool/llcppsymg/internal/symg/symg.go @@ -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) diff --git a/_xtool/llcppsymg/internal/symg/symg_test.go b/_xtool/llcppsymg/internal/symg/symg_test.go index 67b65484..098c9211 100644 --- a/_xtool/llcppsymg/internal/symg/symg_test.go +++ b/_xtool/llcppsymg/internal/symg/symg_test.go @@ -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) diff --git a/config/config.go b/config/config.go index 44916a89..44486a97 100644 --- a/config/config.go +++ b/config/config.go @@ -51,6 +51,7 @@ const ( Inter FileType = iota + 1 Impl Third + Plat ) type FileInfo struct { From f729cb19df57e8404a30f163043ea93f60da5aa8 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 25 Jun 2025 17:07:06 +0800 Subject: [PATCH 2/2] use doc to specific --- _xtool/internal/header/header.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/_xtool/internal/header/header.go b/_xtool/internal/header/header.go index 371233f1..bc3f6b68 100644 --- a/_xtool/internal/header/header.go +++ b/_xtool/internal/header/header.go @@ -11,10 +11,10 @@ import ( ) 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 - PlatDiff []string // Platform Difference Files + 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 { @@ -22,8 +22,12 @@ func (p *PkgHfilesInfo) CurPkgFiles() []string { } 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 - // todo(zzy):support platform difference files + // PlatDiff specifies header file include paths that differ between platforms,these are include paths in Includes. PlatDiff []string Args []string Mix bool