Commit eea958fd authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 248d426b
...@@ -484,70 +484,58 @@ func (s StrSet) Itemv() []string { ...@@ -484,70 +484,58 @@ func (s StrSet) Itemv() []string {
return itemv 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 // we don't load what should be generated by us for 2 reasons:
// cwd is "current" directory for resolving local imports (e.g. packages like "./some/package") // - code generated could be wrong with older version of the
func tracegen(pkgpath string, buildCtx *build.Context, cwd string) error { // tool - it should not prevent from regenerating.
// XXX ignore build tags? // - generated code imports packages which might be not there
bpkg, err := buildCtx.Import(pkgpath, cwd, 0) // yet in gopath (lab.nexedi.com/kirr/go123/tracing)
if err != nil { func findPackageNoZTrace(ctxt *build.Context, fromDir, importPath string, mode build.ImportMode) (*build.Package, error) {
return err bp, err := ctxt.Import(importPath, fromDir, mode)
}
filter := func(filev *[]string) {
// canonical package path e.g. "." -> "path/to/package" okv := []string{}
pkgpath = bpkg.ImportPath for _, f := range *filev {
if strings.HasPrefix(f, "ztrace") {
// XXX reject on .InvalidGoFiles ? bp.IgnoredGoFiles = append(bp.IgnoredGoFiles, f)
} else {
fset := token.NewFileSet() // XXX -> Package? okv = append(okv, f)
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
} }
continue
} }
*filev = okv
}
f, err := parser.ParseFile(fset, fpath, nil, parser.ParseComments) if bp != nil {
if err != nil { filter(&bp.GoFiles)
return err 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 return bp, err
}
// XXX test-only with .TestGoFiles .XTestGoFiles
// 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{ conf := loader.Config{
ParserMode: parser.ParseComments, ParserMode: parser.ParseComments,
TypeCheckFuncBodies: func(path string) bool { return false }, TypeCheckFuncBodies: func(path string) bool { return false },
Build: buildCtx, Build: ctxt,
Cwd: cwd, Cwd: cwd,
FindPackage: findPackageNoZTrace,
} }
conf.Import(pkgpath) conf.Import(pkgpath)
// load package + all its imports // load package + all its imports
// XXX ignore trace.go & trace.s on load here? -> TODO remove the first
lprog, err := conf.Load() lprog, err := conf.Load()
if err != nil { if err != nil {
return err return err
......
...@@ -133,7 +133,7 @@ func TestGoTraceGen(t *testing.T) { ...@@ -133,7 +133,7 @@ func TestGoTraceGen(t *testing.T) {
} }
// XXX autodetect (go list ?) // XXX autodetect (go list ?)
testv := []string{"a/pkg1", "b/pkg2"} testv := []string{"a/pkg1", "b/pkg2", "c/pkg3"}
for _, tpkg := range testv { for _, tpkg := range testv {
err = tracegen(tpkg, tBuildCtx, "" /* = local imorts disabled */) 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