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
8 changes: 8 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/llcppg-system.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"Name": "/system",
"Dir": "./include/llvm/Support",
"Files": [
"AMDGPUAddrSpace.h",
"AMDHSAKernelDescriptor.h",
"Atomic.h",
"AtomicOrdering.h",
"Compiler.h",
"DataTypes.h"
],
"Class": [],
"NonClass": [],
"FuncPrefix": ["Llvm"],
Expand Down
78 changes: 78 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/system/AMDGPUAddrSpace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

306 changes: 306 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/system/AMDHSAKernelDescriptor.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/system/Atomic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/system/AtomicOrdering.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/system/Compiler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tool/_testcpp/llvm-22.1.8-support/system/llcppg.i.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Name string `json:"Name"` // required, sub package name should sarts with '/'
Comment thread
xushiwei marked this conversation as resolved.
Language string `json:"Language"` // c, c++, etc. required
Dir string `json:"Dir"` // dir or dir/... (recursive), required
Files []string `json:"Files"` // selected header files, optional
Comment thread
xushiwei marked this conversation as resolved.
Stdlib string `json:"Stdlib"` // C stdlib include dir, optional
LLGoPackage string `json:"LLGoPackage"` // optional
CFlags string `json:"CFlags"` // optional
Expand Down
33 changes: 27 additions & 6 deletions tool/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"go/token"
"log"
"os"
"path"
"path/filepath"
"sort"
"strings"

"github.com/goplus/gogen"
Expand Down Expand Up @@ -69,7 +71,7 @@ const includeSuffix = string(os.PathSeparator) + "include"

// NewPackage loads the source files and converts them into a Go package according to the
// configuration.
func (cfg *Config) NewPackage(pkgPath, workDir string, index clang.Index) (ret cl.Package, lang cl.Language, err error) {
func (cfg *Config) NewPackage(pkgPath, pkgName, workDir string, index clang.Index) (ret cl.Package, lang cl.Language, err error) {
Comment thread
xushiwei marked this conversation as resolved.
lang, ok := cfg.Lang()
if !ok {
err = fmt.Errorf("invalid language: %q", cfg.Language)
Expand Down Expand Up @@ -138,9 +140,15 @@ func (cfg *Config) NewPackage(pkgPath, workDir string, index clang.Index) (ret c
log.Println("==> includeDirs:", incDirs)
}

topHeaders, err := pputil.TopHeaders(dir, recursive, false, incDirs)
if err != nil {
return
var topHeaders []string
var selFiles = cfg.Files
if len(selFiles) > 0 {
topHeaders = listHeaderFiles(selFiles, dir)
} else {
topHeaders, err = pputil.TopHeaders(dir, recursive, false, incDirs)
if err != nil {
return
}
}

files, err := ParseSources(index, topHeaders, incDirs, cfg.Language)
Expand All @@ -150,7 +158,10 @@ func (cfg *Config) NewPackage(pkgPath, workDir string, index clang.Index) (ret c
defer DisposeSources(files)

srcFilePrefix := dir + string(os.PathSeparator)
ret, err = cl.NewPackage(pkgPath, cfg.Name, files, &cl.Config{
if pkgName == "" {
pkgName = path.Base(cfg.Name)
}
ret, err = cl.NewPackage(pkgPath, pkgName, files, &cl.Config{
Fset: fset,
Importer: imp,
LLGoPackage: cfg.LLGoPackage,
Expand Down Expand Up @@ -186,6 +197,16 @@ func (cfg *Config) NewPackage(pkgPath, workDir string, index clang.Index) (ret c
return
}

func listHeaderFiles(selFiles []string, headerDir string) (topHeaders []string) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listHeaderFiles passes each cfg.Files entry straight to filepath.Join(headerDir, selFile) with no validation or de-duplication. Two things differ from the pputil.TopHeaders path it replaces:

  • No dedup: TopHeaders iterates a map and is inherently unique; here a duplicate entry in Files produces a duplicate top header fed into ParseSources/cl.NewPackage.
  • No containment check: because filepath.Join cleans .. and strips a leading separator, entries like ../../foo.h or absolute paths resolve outside headerDir. For this local, trusted-config tool that's low risk, but rejecting absolute/escaping entries (and de-duping) would be a cheap hardening and keep the two code paths consistent.

headerDir += string(os.PathSeparator)
topHeaders = make([]string, len(selFiles))
for i, selFile := range selFiles {
topHeaders[i] = filepath.Join(headerDir, selFile)
}
sort.Strings(topHeaders)
return
}

func goFileOf(at, srcFilePrefix string) (fname string, ok bool) {
const pathSep = string(os.PathSeparator)
const goFileExt = ".go"
Expand Down Expand Up @@ -324,7 +345,7 @@ func Gen(destDir, srcDir string, index clang.Index) (err error) {
return
}

pkg, _, err := cfg.NewPackage("", srcDir, index)
pkg, _, err := cfg.NewPackage("", "", srcDir, index)
if err != nil {
return
}
Expand Down
22 changes: 17 additions & 5 deletions tool/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func testFromDir(t *testing.T, sel, relDir string, single bool, subPkg ...string
if err != nil {
log.Fatal("LoadConf failed:", err)
}
pkgPrefix := conf.Name + "/"

var pkgSel string
if len(subPkg) > 0 {
Expand Down Expand Up @@ -233,7 +234,14 @@ func testFromDir(t *testing.T, sel, relDir string, single bool, subPkg ...string
return
}

pkg, lang, err := conf.NewPackage("", pkgDir, idx)
pkgName := conf.Name
destDir := pkgDir
if pos := strings.LastIndex(pkgName, "/"); pos >= 0 {
destDir = filepath.Join(pkgDir, pkgName[len(pkgPrefix):])
pkgName = pkgName[pos+1:]
os.MkdirAll(destDir, 0755)
}
pkg, lang, err := conf.NewPackage("", pkgName, pkgDir, idx)
if err != nil {
t.Error("conf.NewPackage:", err)
return
Expand All @@ -242,13 +250,13 @@ func testFromDir(t *testing.T, sel, relDir string, single bool, subPkg ...string
if file.Empty() {
return // skip empty Go files
}
exp, _ := os.ReadFile(pkgDir + "/" + fname)
testGenGo(t, pkg.Package, pkgDir, fname, exp)
exp, _ := os.ReadFile(destDir + "/" + fname)
testGenGo(t, pkg.Package, destDir, fname, exp)
})
wrapFile := "/wrap" + langExts[lang]
wrap, _ := os.ReadFile(pkgDir + wrapFile)
wrap, _ := os.ReadFile(destDir + wrapFile)
if pkg.Wrap != nil {
testDiff(t, pkgDir, wrapFile+".txt", &pkg.Wrap.Content, wrap)
testDiff(t, destDir, wrapFile+".txt", &pkg.Wrap.Content, wrap)
}
})
}
Expand All @@ -266,6 +274,10 @@ func TestSingleC(t *testing.T) {
testFromDir(t, "", "./_testc", true)
}

func TestCpp_LLVMSystem(t *testing.T) {
testFromDir(t, "", "./_testcpp", false, "system")
}

func TestLLVM_AMDGPUAddrSpace(t *testing.T) {
testFromDir(t, "AMDGPUAddrSpace", "./_testcpp", true, "system")
}
Expand Down
Loading