Commit eea958fd authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 248d426b
......@@ -484,70 +484,58 @@ func (s StrSet) Itemv() []string {
return itemv
}
// tracegen generates code according to tracing directives in a package @ pkgpath
// findPackageNoZTrace is (*build.Context).Import but filters-out ztrace* files from found package
//
// buildCtx is build context for discovering packages
// cwd is "current" directory for resolving local imports (e.g. packages like "./some/package")
func tracegen(pkgpath string, buildCtx *build.Context, cwd string) error {
// XXX ignore build tags?
bpkg, err := buildCtx.Import(pkgpath, cwd, 0)
if err != nil {
return err
}
// canonical package path e.g. "." -> "path/to/package"
pkgpath = bpkg.ImportPath
// XXX reject on .InvalidGoFiles ?
fset := token.NewFileSet() // XXX -> Package?
filev := []*ast.File{}
// parse go and cgo sources
for _, fgo := range append(bpkg.GoFiles, bpkg.CgoFiles...) {
fpath := bpkg.Dir + "/" + fgo
// don't load what should be generated by us. reasons:
// - code generated could be wrong with older version of the
// tool - it should not prevent from regenerating.
// - generated code imports packages which might be not there
// yet in gopath (lab.nexedi.com/kirr/go123/tracing)
if strings.HasPrefix(fgo, "ztrace") {
// if it is there but not created by us - complain
err = checkCanWrite(fpath)
if err != nil {
return err
// we don't load what should be generated by us for 2 reasons:
// - code generated could be wrong with older version of the
// tool - it should not prevent from regenerating.
// - generated code imports packages which might be not there
// yet in gopath (lab.nexedi.com/kirr/go123/tracing)
func findPackageNoZTrace(ctxt *build.Context, fromDir, importPath string, mode build.ImportMode) (*build.Package, error) {
bp, err := ctxt.Import(importPath, fromDir, mode)
filter := func(filev *[]string) {
okv := []string{}
for _, f := range *filev {
if strings.HasPrefix(f, "ztrace") {
bp.IgnoredGoFiles = append(bp.IgnoredGoFiles, f)
} else {
okv = append(okv, f)
}
continue
}
*filev = okv
}
f, err := parser.ParseFile(fset, fpath, nil, parser.ParseComments)
if err != nil {
return err
}
if bp != nil {
filter(&bp.GoFiles)
filter(&bp.CgoFiles)
filter(&bp.TestGoFiles)
filter(&bp.XTestGoFiles)
filter(&bp.SFiles)
filev = append(filev, f)
// XXX also adjust .Import{s,Pos}, .TestImport{s,Pos}, .XTestImport{s,Pos} ?
}
return nil
// XXX test-only with .TestGoFiles .XTestGoFiles
return bp, err
}
// tracegen generates code according to tracing directives in a package @ pkgpath
//
// ctxt is build context for discovering packages
// cwd is "current" directory for resolving local imports (e.g. packages like "./some/package")
func tracegen(pkgpath string, ctxt *build.Context, cwd string) error {
// TODO test-only with .TestGoFiles .XTestGoFiles
// XXX typechecking is much slower than parsing + we don't need to
// load anything except the package in question
// TODO -> use just AST parsing for loading?
conf := loader.Config{
ParserMode: parser.ParseComments,
TypeCheckFuncBodies: func(path string) bool { return false },
Build: buildCtx,
Build: ctxt,
Cwd: cwd,
FindPackage: findPackageNoZTrace,
}
conf.Import(pkgpath)
// load package + all its imports
// XXX ignore trace.go & trace.s on load here? -> TODO remove the first
lprog, err := conf.Load()
if err != nil {
return err
......
......@@ -133,7 +133,7 @@ func TestGoTraceGen(t *testing.T) {
}
// XXX autodetect (go list ?)
testv := []string{"a/pkg1", "b/pkg2"}
testv := []string{"a/pkg1", "b/pkg2", "c/pkg3"}
for _, tpkg := range testv {
err = tracegen(tpkg, tBuildCtx, "" /* = local imorts disabled */)
......
// Code generated by lab.nexedi.com/kirr/go123/tracing/cmd/gotrace; DO NOT EDIT.
Bad bad bad - I'm invalid go file.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment