diff --git a/cl/multifiles.go b/cl/multifiles.go index 62d7847..d20966e 100644 --- a/cl/multifiles.go +++ b/cl/multifiles.go @@ -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 { diff --git a/cl/multifiles_test.go b/cl/multifiles_test.go index 1fb99de..10991b0 100644 --- a/cl/multifiles_test.go +++ b/cl/multifiles_test.go @@ -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) + } +}