github.com/goplus/gox@v1.14.13-0.20240308130321-6ff7f61cfae8/chore/gobyeff/by_effect.go (about) 1 /* 2 Copyright 2022 The GoPlus Authors (goplus.org) 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package main 15 16 import ( 17 "fmt" 18 "go/token" 19 "go/types" 20 "log" 21 "os" 22 23 "github.com/goplus/gox/packages" 24 ) 25 26 type none = struct{} 27 28 func usage() { 29 fmt.Fprintf(os.Stderr, "Usage: gobyeff pkgPath\n") 30 } 31 32 func main() { 33 if len(os.Args) < 2 { 34 usage() 35 return 36 } 37 fset := token.NewFileSet() 38 imp := packages.NewImporter(fset) 39 pkg, err := imp.Import(os.Args[1]) 40 check(err) 41 checkSideEffect(imp, pkg, make(map[string]none)) 42 } 43 44 func checkSideEffect(imp types.Importer, pkg *types.Package, checked map[string]none) { 45 pkgPath := pkg.Path() 46 if _, ok := checked[pkgPath]; ok { 47 return 48 } 49 checked[pkgPath] = none{} 50 fmt.Println("==> Checking", pkgPath) 51 if !pkg.Complete() { 52 pkg, _ = imp.Import(pkgPath) 53 } 54 scope := pkg.Scope() 55 for _, name := range scope.Names() { 56 switch o := scope.Lookup(name).(type) { 57 case *types.Var: 58 fmt.Println("var", o.Name(), o.Type()) 59 } 60 } 61 fmt.Println() 62 for _, ref := range pkg.Imports() { 63 checkSideEffect(imp, ref, checked) 64 } 65 } 66 67 func check(err error) { 68 if err != nil { 69 log.Panicln(err) 70 } 71 }