Skip to content

Commit

Permalink
unconvert: use x/tools/go/packages (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre authored and mdempsky committed Jan 10, 2019
1 parent dc69f12 commit b40e604
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions unconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import (
"sync"
"unicode"

"github.com/kisielk/gotool"
"golang.org/x/text/width"
"golang.org/x/tools/go/buildutil"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/packages"
)

// Unnecessary conversions are identified by the position
Expand Down Expand Up @@ -197,7 +196,7 @@ func main() {
defer pprof.StopCPUProfile()
}

importPaths := gotool.ImportPaths(flag.Args())
importPaths := flag.Args()
if len(importPaths) == 0 {
return
}
Expand Down Expand Up @@ -272,29 +271,17 @@ func mergeEdits(importPaths []string) fileToEditSet {
return m
}

type noImporter struct{}

func (noImporter) Import(path string) (*types.Package, error) {
panic("golang.org/x/tools/go/loader said this wouldn't be called")
}

func computeEdits(importPaths []string, os, arch string, cgoEnabled bool) fileToEditSet {
ctxt := build.Default
ctxt.GOOS = os
ctxt.GOARCH = arch
ctxt.CgoEnabled = cgoEnabled

var conf loader.Config
conf.Build = &ctxt
conf.TypeChecker.Importer = noImporter{}
for _, importPath := range importPaths {
if *flagTests {
conf.ImportWithTests(importPath)
} else {
conf.Import(importPath)
}
func computeEdits(importPaths []string, osname, arch string, cgoEnabled bool) fileToEditSet {
cgoEnabledVal := "0"
if cgoEnabled {
cgoEnabledVal = "1"
}
prog, err := conf.Load()

pkgs, err := packages.Load(&packages.Config{
Mode: packages.LoadSyntax,
Env: append(os.Environ(), "GOOS="+osname, "GOARCH="+arch, "CGO_ENABLED="+cgoEnabledVal),
Tests: *flagTests,
}, importPaths...)
if err != nil {
log.Fatal(err)
}
Expand All @@ -303,15 +290,16 @@ func computeEdits(importPaths []string, os, arch string, cgoEnabled bool) fileTo
file string
edits editSet
}

ch := make(chan res)
var wg sync.WaitGroup
for _, pkg := range prog.InitialPackages() {
for _, file := range pkg.Files {
for _, pkg := range pkgs {
for _, file := range pkg.Syntax {
pkg, file := pkg, file
wg.Add(1)
go func() {
defer wg.Done()
v := visitor{info: &pkg.Info, file: conf.Fset.File(file.Package), edits: make(editSet)}
v := visitor{info: pkg.TypesInfo, file: pkg.Fset.File(file.Package), edits: make(editSet)}
ast.Walk(&v, file)
ch <- res{v.file.Name(), v.edits}
}()
Expand Down

0 comments on commit b40e604

Please sign in to comment.