本文整理匯總了Golang中golang.org/x/tools/go/loader.Config類的典型用法代碼示例。如果您正苦於以下問題:Golang Config類的具體用法?Golang Config怎麽用?Golang Config使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Config類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ParseAST
// Parse parses the ast for this file and returns a ParsedFile
func (w *Weave) ParseAST(fname string) *ast.File {
var err error
fset := token.NewFileSet()
af, err := parser.ParseFile(fset, fname, nil, 0)
if err != nil {
w.flog.Println(err)
}
loadcfg := loader.Config{}
loadcfg.CreateFromFilenames(fname)
info := types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
}
var conf types.Config
_, err = conf.Check(af.Name.Name, fset, []*ast.File{af}, &info)
if err != nil {
if w.warnAST {
w.flog.Println(err)
}
}
return af
}
示例2: loadWithSoftErrors
// loadWithSoftErrors calls lconf.Load, suppressing "soft" errors. (See Go issue 16530.)
// TODO(adonovan): Once the loader has an option to allow soft errors,
// replace calls to loadWithSoftErrors with loader calls with that parameter.
func loadWithSoftErrors(lconf *loader.Config) (*loader.Program, error) {
lconf.AllowErrors = true
// Ideally we would just return conf.Load() here, but go/types
// reports certain "soft" errors that gc does not (Go issue 14596).
// As a workaround, we set AllowErrors=true and then duplicate
// the loader's error checking but allow soft errors.
// It would be nice if the loader API permitted "AllowErrors: soft".
prog, err := lconf.Load()
if err != nil {
return nil, err
}
var errpkgs []string
// Report hard errors in indirectly imported packages.
for _, info := range prog.AllPackages {
if containsHardErrors(info.Errors) {
errpkgs = append(errpkgs, info.Pkg.Path())
} else {
// Enable SSA construction for packages containing only soft errors.
info.TransitivelyErrorFree = true
}
}
if errpkgs != nil {
var more string
if len(errpkgs) > 3 {
more = fmt.Sprintf(" and %d more", len(errpkgs)-3)
errpkgs = errpkgs[:3]
}
return nil, fmt.Errorf("couldn't load packages due to errors: %s%s",
strings.Join(errpkgs, ", "), more)
}
return prog, err
}
示例3: TestCwd
func TestCwd(t *testing.T) {
ctxt := fakeContext(map[string]string{"one/two/three": `package three`})
for _, test := range []struct {
cwd, arg, want string
}{
{cwd: "/go/src/one", arg: "./two/three", want: "one/two/three"},
{cwd: "/go/src/one", arg: "../one/two/three", want: "one/two/three"},
{cwd: "/go/src/one", arg: "one/two/three", want: "one/two/three"},
{cwd: "/go/src/one/two/three", arg: ".", want: "one/two/three"},
{cwd: "/go/src/one", arg: "two/three", want: ""},
} {
conf := loader.Config{
Cwd: test.cwd,
Build: ctxt,
}
conf.Import(test.arg)
var got string
prog, err := conf.Load()
if prog != nil {
got = imported(prog)
}
if got != test.want {
t.Errorf("Load(%s) from %s: Imported = %s, want %s",
test.arg, test.cwd, got, test.want)
if err != nil {
t.Errorf("Load failed: %v", err)
}
}
}
}
示例4: loadProgram
func loadProgram(args []string, tests bool) (*ssa.Program, error) {
conf := loader.Config{}
if len(args) == 0 {
fmt.Fprintln(os.Stderr, Usage)
os.Exit(1)
}
// Use the initial packages from the command line.
args, err := conf.FromArgs(args, tests)
if err != nil {
return nil, err
}
// Load, parse and type-check the whole program.
iprog, err := conf.Load()
if err != nil {
return nil, err
}
// Create and build SSA-form program representation.
prog := ssautil.CreateProgram(iprog, 0)
prog.BuildAll()
return prog, nil
}
示例5: TestStdlib
func TestStdlib(t *testing.T) {
defer func(saved func(format string, args ...interface{})) {
logf = saved
}(logf)
logf = t.Errorf
ctxt := build.Default // copy
// Enumerate $GOROOT packages.
saved := ctxt.GOPATH
ctxt.GOPATH = "" // disable GOPATH during AllPackages
pkgs := buildutil.AllPackages(&ctxt)
ctxt.GOPATH = saved
// Throw in a number of go.tools packages too.
pkgs = append(pkgs,
"golang.org/x/tools/cmd/godoc",
"golang.org/x/tools/refactor/lexical")
// Load, parse and type-check the program.
conf := loader.Config{Build: &ctxt}
for _, path := range pkgs {
conf.ImportWithTests(path)
}
iprog, err := conf.Load()
if err != nil {
t.Fatalf("Load failed: %v", err)
}
// This test ensures that Structure doesn't panic and that
// its internal sanity-checks against go/types don't fail.
for pkg, info := range iprog.AllPackages {
_ = Structure(iprog.Fset, pkg, &info.Info, info.Files)
}
}
示例6: NewUnexporter
// Create unexporter
func NewUnexporter(o *Config) (*unexporter, error) {
var conf loader.Config
for _, package_name := range buildutil.AllPackages(&build.Default) {
conf.ImportWithTests(package_name)
}
program, err := conf.Load()
if err != nil {
return nil, err
}
if program.Package(o.Pkg) == nil {
return nil, errors.New(fmt.Sprintf("'%s' is not a valid package", o.Pkg))
}
if o.Debug {
pkg := program.Package(o.Pkg).Pkg
fmt.Printf("finding unused identifiers for %s (%s)\n", pkg.Name(), pkg.Path())
}
unexporter := &unexporter{
Program: program,
MainPackage: program.Package(o.Pkg),
Verbose: o.Debug,
}
unexporter.run()
return unexporter, nil
}
示例7: myloader
// Return count of unused vars
func myloader(args []string) (count int) {
var conf loader.Config
conf.FromArgs(args, false)
prog, err := conf.Load()
if err != nil {
log.Fatal(err)
}
for _, pi := range prog.Created {
fmt.Println(pi)
}
info := prog.Package(args[0]).Info
fset := prog.Fset
ssaprog := ssautil.CreateProgram(prog, ssa.GlobalDebug)
ssaprog.Build()
for expr, object := range info.Defs {
unused := checkObj(expr, object, prog, ssaprog, fset)
if unused {
fmt.Fprintf(os.Stderr, "Unused assignment for '%v' %v\n", expr, fset.Position(expr.Pos()))
count++
}
}
for expr, object := range info.Uses {
unused := checkObj(expr, object, prog, ssaprog, fset)
if unused {
fmt.Fprintf(os.Stderr, "Unused assignment for '%v' %v\n", expr, fset.Position(expr.Pos()))
count++
}
}
return count
}
示例8: TestTransitivelyErrorFreeFlag
func TestTransitivelyErrorFreeFlag(t *testing.T) {
// Create an minimal custom build.Context
// that fakes the following packages:
//
// a --> b --> c! c has an error
// \ d and e are transitively error-free.
// e --> d
//
// Each package [a-e] consists of one file, x.go.
pkgs := map[string]string{
"a": `package a; import (_ "b"; _ "e")`,
"b": `package b; import _ "c"`,
"c": `package c; func f() { _ = int(false) }`, // type error within function body
"d": `package d;`,
"e": `package e; import _ "d"`,
}
conf := loader.Config{
AllowErrors: true,
SourceImports: true,
Build: fakeContext(pkgs),
}
conf.Import("a")
prog, err := conf.Load()
if err != nil {
t.Errorf("Load failed: %s", err)
}
if prog == nil {
t.Fatalf("Load returned nil *Program")
}
for pkg, info := range prog.AllPackages {
var wantErr, wantTEF bool
switch pkg.Path() {
case "a", "b":
case "c":
wantErr = true
case "d", "e":
wantTEF = true
default:
t.Errorf("unexpected package: %q", pkg.Path())
continue
}
if (info.Errors != nil) != wantErr {
if wantErr {
t.Errorf("Package %q.Error = nil, want error", pkg.Path())
} else {
t.Errorf("Package %q has unexpected Errors: %v",
pkg.Path(), info.Errors)
}
}
if info.TransitivelyErrorFree != wantTEF {
t.Errorf("Package %q.TransitivelyErrorFree=%t, want %t",
pkg.Path(), info.TransitivelyErrorFree, wantTEF)
}
}
}
示例9: createProgram
// createProgram returns a program containing packages specified.
func createProgram(packages ...string) (*loader.Program, error) {
var conf loader.Config
for _, name := range packages {
conf.CreateFromFilenames(name, getFileNames(name)...)
}
return conf.Load()
}
示例10: Query
// Query runs a single oracle query.
//
// args specify the main package in (*loader.Config).FromArgs syntax.
// mode is the query mode ("callers", etc).
// ptalog is the (optional) pointer-analysis log file.
// buildContext is the go/build configuration for locating packages.
// reflection determines whether to model reflection soundly (currently slow).
//
// Clients that intend to perform multiple queries against the same
// analysis scope should use this pattern instead:
//
// conf := loader.Config{Build: buildContext, SourceImports: true}
// ... populate config, e.g. conf.FromArgs(args) ...
// iprog, err := conf.Load()
// if err != nil { ... }
// o, err := oracle.New(iprog, nil, false)
// if err != nil { ... }
// for ... {
// qpos, err := oracle.ParseQueryPos(imp, pos, needExact)
// if err != nil { ... }
//
// res, err := o.Query(mode, qpos)
// if err != nil { ... }
//
// // use res
// }
//
// TODO(adonovan): the ideal 'needsExact' parameter for ParseQueryPos
// depends on the query mode; how should we expose this?
//
func Query(args []string, mode, pos string, ptalog io.Writer, buildContext *build.Context, reflection bool) (*Result, error) {
if mode == "what" {
// Bypass package loading, type checking, SSA construction.
return what(pos, buildContext)
}
minfo := findMode(mode)
if minfo == nil {
return nil, fmt.Errorf("invalid mode type: %q", mode)
}
conf := loader.Config{Build: buildContext, SourceImports: true}
// Determine initial packages.
args, err := conf.FromArgs(args, true)
if err != nil {
return nil, err
}
if len(args) > 0 {
return nil, fmt.Errorf("surplus arguments: %q", args)
}
// For queries needing only a single typed package,
// reduce the analysis scope to that package.
if minfo.needs&(needSSA|needRetainTypeInfo) == 0 {
reduceScope(pos, &conf)
}
// TODO(adonovan): report type errors to the user via Serial
// types, not stderr?
// conf.TypeChecker.Error = func(err error) {
// E := err.(types.Error)
// fmt.Fprintf(os.Stderr, "%s: %s\n", E.Fset.Position(E.Pos), E.Msg)
// }
// Load/parse/type-check the program.
iprog, err := conf.Load()
if err != nil {
return nil, err
}
o, err := newOracle(iprog, ptalog, minfo.needs, reflection)
if err != nil {
return nil, err
}
qpos, err := ParseQueryPos(iprog, pos, minfo.needs&needExactPos != 0)
if err != nil && minfo.needs&(needPos|needExactPos) != 0 {
return nil, err
}
// SSA is built and we have the QueryPos.
// Release the other ASTs and type info to the GC.
iprog = nil
return o.query(minfo, qpos)
}
示例11: TestLoad_BadDependency_AllowErrors
func TestLoad_BadDependency_AllowErrors(t *testing.T) {
for _, test := range []struct {
descr string
pkgs map[string]string
wantPkgs string
}{
{
descr: "missing dependency",
pkgs: map[string]string{
"a": `package a; import _ "b"`,
"b": `package b; import _ "c"`,
},
wantPkgs: "a b",
},
{
descr: "bad package decl in dependency",
pkgs: map[string]string{
"a": `package a; import _ "b"`,
"b": `package b; import _ "c"`,
"c": `package`,
},
wantPkgs: "a b",
},
{
descr: "parse error in dependency",
pkgs: map[string]string{
"a": `package a; import _ "b"`,
"b": `package b; import _ "c"`,
"c": `package c; var x = `,
},
wantPkgs: "a b c",
},
} {
conf := loader.Config{
AllowErrors: true,
SourceImports: true,
Build: fakeContext(test.pkgs),
}
conf.Import("a")
prog, err := conf.Load()
if err != nil {
t.Errorf("%s: Load failed unexpectedly: %v", test.descr, err)
}
if prog == nil {
t.Fatalf("%s: Load returned a nil Program", test.descr)
}
if got, want := imported(prog), "a"; got != want {
t.Errorf("%s: Imported = %s, want %s", test.descr, got, want)
}
if got := all(prog); strings.Join(got, " ") != test.wantPkgs {
t.Errorf("%s: AllPackages = %s, want %s", test.descr, got, test.wantPkgs)
}
}
}
示例12: allowErrors
// allowErrors causes type errors to be silently ignored.
// (Not suitable if SSA construction follows.)
func allowErrors(lconf *loader.Config) {
ctxt := *lconf.Build // copy
ctxt.CgoEnabled = false
lconf.Build = &ctxt
lconf.AllowErrors = true
// AllErrors makes the parser always return an AST instead of
// bailing out after 10 errors and returning an empty ast.File.
lconf.ParserMode = parser.AllErrors
lconf.TypeChecker.Error = func(err error) {}
}
示例13: TestErrorReporting
// Test that both syntax (scan/parse) and type errors are both recorded
// (in PackageInfo.Errors) and reported (via Config.TypeChecker.Error).
func TestErrorReporting(t *testing.T) {
pkgs := map[string]string{
"a": `package a; import _ "b"; var x int = false`,
"b": `package b; 'syntax error!`,
}
conf := loader.Config{
AllowErrors: true,
SourceImports: true,
Build: fakeContext(pkgs),
}
var allErrors []error
conf.TypeChecker.Error = func(err error) {
allErrors = append(allErrors, err)
}
conf.Import("a")
prog, err := conf.Load()
if err != nil {
t.Errorf("Load failed: %s", err)
}
if prog == nil {
t.Fatalf("Load returned nil *Program")
}
hasError := func(errors []error, substr string) bool {
for _, err := range errors {
if strings.Contains(err.Error(), substr) {
return true
}
}
return false
}
// TODO(adonovan): test keys of ImportMap.
// Check errors recorded in each PackageInfo.
for pkg, info := range prog.AllPackages {
switch pkg.Path() {
case "a":
if !hasError(info.Errors, "cannot convert false") {
t.Errorf("a.Errors = %v, want bool conversion (type) error", info.Errors)
}
case "b":
if !hasError(info.Errors, "rune literal not terminated") {
t.Errorf("b.Errors = %v, want unterminated literal (syntax) error", info.Errors)
}
}
}
// Check errors reported via error handler.
if !hasError(allErrors, "cannot convert false") ||
!hasError(allErrors, "rune literal not terminated") {
t.Errorf("allErrors = %v, want both syntax and type errors", allErrors)
}
}
示例14: ExampleConfig_Import
// This example imports three packages, including the tests for one of
// them, and loads all their dependencies.
func ExampleConfig_Import() {
// ImportWithTest("strconv") causes strconv to include
// internal_test.go, and creates an external test package,
// strconv_test.
// (Compare with the example of CreateFromFiles.)
var conf loader.Config
conf.Import("unicode/utf8")
conf.Import("errors")
conf.ImportWithTests("strconv")
prog, err := conf.Load()
if err != nil {
log.Fatal(err)
}
printProgram(prog)
printFilenames(prog.Fset, prog.Package("strconv"))
printFilenames(prog.Fset, prog.Package("strconv_test"))
// Output:
// created: [strconv_test]
// imported: [errors strconv unicode/utf8]
// initial: [errors strconv strconv_test unicode/utf8]
// all: [bufio bytes errors flag fmt io log math math/rand os reflect runtime runtime/pprof runtime/trace sort strconv strconv_test strings sync sync/atomic syscall testing text/tabwriter time unicode unicode/utf8]
// strconv.Files: [atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go internal_test.go]
// strconv_test.Files: [atob_test.go atof_test.go atoi_test.go decimal_test.go example_test.go fp_test.go ftoa_test.go itoa_test.go quote_test.go strconv_test.go]
}
示例15: main
func main() {
// Loader is a tool for opening Go files, it loads from a Config type
conf := loader.Config{
Build: &build.Default,
}
path, _ := filepath.Abs("looper")
file, err := conf.ParseFile(path+"/"+"looper.go", nil)
if err != nil {
fmt.Println(err)
return
}
// Creation of a single file main pacakge
conf.CreateFromFiles("looper", file)
conf.Import("runtime")
p, err := conf.Load()
if err != nil {
fmt.Println(err)
return
}
// Finally, create SSA representation from the package we've loaded
program := ssautil.CreateProgram(p, ssa.SanityCheckFunctions)
looperPkg := program.Package(p.Created[0].Pkg)
fmt.Println("RIGHT IN THE SINGLE STATIC ASSIGNMENT FORM:")
looperPkg.WriteTo(os.Stdout)
fmt.Println("LOOK AT THIS HERE LOOPER FUNC:")
looperFunc := looperPkg.Func("Looper")
looperFunc.WriteTo(os.Stdout)
}