Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
ctx.autoStaticName: use baseOfFile
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 16, 2022
1 parent 40cbbea commit 56fa412
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cl/multifiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,24 @@ func (p *blockCtx) getSuName(v *ast.Node, tag string) (string, int) {
}

func (p *blockCtx) autoStaticName(name string) string {
if file := p.srcfile; file != "" {
return name + "_" + baseOfFile(file)
}
*p.base++
return name + "_cgo" + strconv.Itoa(*p.base)
}

func baseOfFile(file string) string {
base := filepath.Base(file)
pos := strings.IndexFunc(base, func(r rune) bool {
return !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '_')
})
if pos > 0 {
base = base[:pos]
}
return base
}

func (p *blockCtx) logFile(node *ast.Node) {
if f := node.Loc.PresumedFile; f != "" {
if p.hasMulti {
Expand Down
6 changes: 6 additions & 0 deletions cl/multifiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func TestInitDepPkgs(t *testing.T) {
initDepPkgs(pkg, deps)
})
}

func TestBaseOfFile(t *testing.T) {
if ret := baseOfFile("src/errno/strerror.c.i"); ret != "strerror" {
t.Fatal("baseOfFile:", ret)
}
}

0 comments on commit 56fa412

Please sign in to comment.