Commit 9449c18a authored by Robert Griesemer's avatar Robert Griesemer

cmd/api: rewrite using go/types

- adjusted test files so that they actually type-check
- adjusted go1.txt, go1.1.txt, next.txt
- to run, provide build tag: api_tool

Fixes #4538.

R=bradfitz
CC=golang-dev
https://golang.org/cl/12300043
parent de04bf24
......@@ -92,6 +92,7 @@ pkg encoding/json, method (Number) String() string
pkg encoding/json, type Number string
pkg encoding/xml, func EscapeText(io.Writer, []uint8) error
pkg encoding/xml, method (*Encoder) Indent(string, string)
pkg encoding/xml, method (Encoder) ReadFrom(io.Reader) (int64, error)
pkg encoding/xml, type Decoder struct, DefaultSpace string
pkg go/ast, func NewCommentMap(*token.FileSet, Node, []*CommentGroup) CommentMap
pkg go/ast, method (CommentMap) Comments() []*CommentGroup
......@@ -1700,6 +1701,7 @@ pkg testing, method (BenchmarkResult) AllocsPerOp() int64
pkg testing, method (BenchmarkResult) MemString() string
pkg testing, type BenchmarkResult struct, MemAllocs uint64
pkg testing, type BenchmarkResult struct, MemBytes uint64
pkg text/template, method (Template) ErrorContext(parse.Node) (string, string)
pkg text/template/parse, const NodeChain NodeType
pkg text/template/parse, const NodeNil NodeType
pkg text/template/parse, method (*ChainNode) Add(string)
......
This diff is collapsed.
This diff is collapsed.
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"go/ast"
"log"
"reflect"
)
const debugClone = false
// TODO(bradfitz): delete this function (and whole file) once
// http://golang.org/issue/4380 is fixed.
func clone(i interface{}) (cloned interface{}) {
if debugClone {
defer func() {
if !reflect.DeepEqual(i, cloned) {
log.Printf("cloned %T doesn't match: in=%#v out=%#v", i, i, cloned)
}
}()
}
switch v := i.(type) {
case nil:
return nil
case *ast.File:
o := &ast.File{
Doc: v.Doc, // shallow
Package: v.Package,
Comments: v.Comments, // shallow
Name: v.Name,
Scope: v.Scope,
}
for _, x := range v.Decls {
o.Decls = append(o.Decls, clone(x).(ast.Decl))
}
for _, x := range v.Imports {
o.Imports = append(o.Imports, clone(x).(*ast.ImportSpec))
}
for _, x := range v.Unresolved {
o.Unresolved = append(o.Unresolved, x)
}
return o
case *ast.GenDecl:
o := new(ast.GenDecl)
*o = *v
o.Specs = nil
for _, x := range v.Specs {
o.Specs = append(o.Specs, clone(x).(ast.Spec))
}
return o
case *ast.TypeSpec:
o := new(ast.TypeSpec)
*o = *v
o.Type = cloneExpr(v.Type)
return o
case *ast.InterfaceType:
o := new(ast.InterfaceType)
*o = *v
o.Methods = clone(v.Methods).(*ast.FieldList)
return o
case *ast.FieldList:
if v == nil {
return v
}
o := new(ast.FieldList)
*o = *v
o.List = nil
for _, x := range v.List {
o.List = append(o.List, clone(x).(*ast.Field))
}
return o
case *ast.Field:
o := &ast.Field{
Doc: v.Doc, // shallow
Type: cloneExpr(v.Type),
Tag: clone(v.Tag).(*ast.BasicLit),
Comment: v.Comment, // shallow
}
for _, x := range v.Names {
o.Names = append(o.Names, clone(x).(*ast.Ident))
}
return o
case *ast.FuncType:
if v == nil {
return v
}
return &ast.FuncType{
Func: v.Func,
Params: clone(v.Params).(*ast.FieldList),
Results: clone(v.Results).(*ast.FieldList),
}
case *ast.FuncDecl:
if v == nil {
return v
}
return &ast.FuncDecl{
Recv: clone(v.Recv).(*ast.FieldList),
Name: v.Name,
Type: clone(v.Type).(*ast.FuncType),
Body: v.Body, // shallow
}
case *ast.ValueSpec:
if v == nil {
return v
}
o := &ast.ValueSpec{
Type: cloneExpr(v.Type),
}
for _, x := range v.Names {
o.Names = append(o.Names, x)
}
for _, x := range v.Values {
o.Values = append(o.Values, cloneExpr(x))
}
return o
case *ast.CallExpr:
if v == nil {
return v
}
o := &ast.CallExpr{}
*o = *v
o.Args = cloneExprs(v.Args)
o.Fun = cloneExpr(v.Fun)
return o
case *ast.SelectorExpr:
if v == nil {
return nil
}
return &ast.SelectorExpr{
X: cloneExpr(v.X),
Sel: v.Sel,
}
case *ast.ArrayType:
return &ast.ArrayType{
Lbrack: v.Lbrack,
Len: cloneExpr(v.Len),
Elt: cloneExpr(v.Elt),
}
case *ast.StructType:
return &ast.StructType{
Struct: v.Struct,
Fields: clone(v.Fields).(*ast.FieldList),
Incomplete: v.Incomplete,
}
case *ast.StarExpr:
return &ast.StarExpr{
Star: v.Star,
X: cloneExpr(v.X),
}
case *ast.CompositeLit:
return &ast.CompositeLit{
Type: cloneExpr(v.Type),
Lbrace: v.Lbrace,
Elts: cloneExprs(v.Elts),
Rbrace: v.Rbrace,
}
case *ast.UnaryExpr:
return &ast.UnaryExpr{
OpPos: v.OpPos,
Op: v.Op,
X: cloneExpr(v.X),
}
case *ast.BinaryExpr:
return &ast.BinaryExpr{
OpPos: v.OpPos,
Op: v.Op,
X: cloneExpr(v.X),
Y: cloneExpr(v.Y),
}
case *ast.Ellipsis:
return &ast.Ellipsis{
Ellipsis: v.Ellipsis,
Elt: cloneExpr(v.Elt),
}
case *ast.KeyValueExpr:
return &ast.KeyValueExpr{
Key: cloneExpr(v.Key),
Colon: v.Colon,
Value: cloneExpr(v.Value),
}
case *ast.FuncLit:
return &ast.FuncLit{
Type: clone(v.Type).(*ast.FuncType),
Body: v.Body, // shallow
}
case *ast.MapType:
return &ast.MapType{
Map: v.Map,
Key: cloneExpr(v.Key),
Value: cloneExpr(v.Value),
}
case *ast.ParenExpr:
return &ast.ParenExpr{
Lparen: v.Lparen,
X: cloneExpr(v.X),
Rparen: v.Rparen,
}
case *ast.Ident, *ast.BasicLit:
return v
case *ast.ImportSpec:
return &ast.ImportSpec{
Doc: v.Doc, // shallow
Name: v.Name,
Path: clone(v.Path).(*ast.BasicLit),
Comment: v.Comment, // shallow
EndPos: v.EndPos,
}
case *ast.ChanType:
return &ast.ChanType{
Begin: v.Begin,
Arrow: v.Arrow,
Dir: v.Dir,
Value: cloneExpr(v.Value),
}
case *ast.TypeAssertExpr:
return &ast.TypeAssertExpr{
X: cloneExpr(v.X),
Type: cloneExpr(v.Type),
}
case *ast.IndexExpr:
return &ast.IndexExpr{
X: cloneExpr(v.X),
Index: cloneExpr(v.Index),
Lbrack: v.Lbrack,
Rbrack: v.Rbrack,
}
}
panic(fmt.Sprintf("Uncloneable type %T", i))
}
func cloneExpr(x ast.Expr) ast.Expr {
if x == nil {
return nil
}
return clone(x).(ast.Expr)
}
func cloneExprs(x []ast.Expr) []ast.Expr {
if x == nil {
return nil
}
o := make([]ast.Expr, len(x))
for i, x := range x {
o[i] = cloneExpr(x)
}
return o
}
This diff is collapsed.
// +build api_tool
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
......@@ -33,12 +35,10 @@ func TestGolden(t *testing.T) {
if !fi.IsDir() {
continue
}
w := NewWalker()
w.wantedPkg[fi.Name()] = true
w.root = "testdata/src/pkg"
goldenFile := filepath.Join("testdata", "src", "pkg", fi.Name(), "golden.txt")
w.WalkPackage(fi.Name())
w := NewWalker(nil, "testdata/src/pkg")
w.export(w.Import(fi.Name()))
if *updateGolden {
os.Remove(goldenFile)
......
pkg p1, const A ideal-int
pkg p1, const A64 int64
pkg p1, const AIsLowerA ideal-int
pkg p1, const B ideal-int
pkg p1, const B0 ideal-int
pkg p1, const ConstChase2 ideal-int
pkg p1, const ConversionConst MyInt
pkg p1, const FloatConst ideal-float
......@@ -10,6 +10,7 @@ pkg p1, func Bar(int8, int16, int64)
pkg p1, func Bar1(int8, int16, int64) uint64
pkg p1, func Bar2(int8, int16, int64) (uint8, uint64)
pkg p1, func BarE() Error
pkg p1, func Now() Time
pkg p1, func PlainFunc(int, int, string) (*B, error)
pkg p1, func TakesFunc(func(int) int)
pkg p1, method (*B) JustOnB()
......@@ -34,9 +35,9 @@ pkg p1, type ByteStruct struct, R int32
pkg p1, type Codec struct
pkg p1, type Codec struct, Func func(int, int) int
pkg p1, type EmbedSelector struct
pkg p1, type EmbedSelector struct, embedded time.Time
pkg p1, type EmbedSelector struct, embedded Time
pkg p1, type EmbedURLPtr struct
pkg p1, type EmbedURLPtr struct, embedded *url.URL
pkg p1, type EmbedURLPtr struct, embedded *URL
pkg p1, type Embedded struct
pkg p1, type Error interface { Error, Temporary }
pkg p1, type Error interface, Error() string
......@@ -58,7 +59,7 @@ pkg p1, type Public interface, X()
pkg p1, type Public interface, Y()
pkg p1, type S struct
pkg p1, type S struct, Public *int
pkg p1, type S struct, PublicTime time.Time
pkg p1, type S struct, PublicTime Time
pkg p1, type S2 struct
pkg p1, type S2 struct, Extra bool
pkg p1, type S2 struct, embedded S
......@@ -68,6 +69,8 @@ pkg p1, type T struct
pkg p1, type TPtrExported struct
pkg p1, type TPtrExported struct, embedded *Embedded
pkg p1, type TPtrUnexported struct
pkg p1, type Time struct
pkg p1, type URL struct
pkg p1, var Byte uint8
pkg p1, var ByteConv []uint8
pkg p1, var ByteFunc func(uint8) int32
......@@ -81,5 +84,5 @@ pkg p1, var V1 uint64
pkg p1, var V2 p2.Twoer
pkg p1, var VError Error
pkg p1, var X I
pkg p1, var X int64
pkg p1, var X0 int64
pkg p1, var Y int
......@@ -35,7 +35,7 @@ var (
var ChecksumError = ptwo.NewError("gzip checksum error")
const B = 2
const B0 = 2
const StrConst = "foo"
const FloatConst = 1.5
......@@ -43,14 +43,18 @@ type myInt int
type MyInt int
type Time struct{}
type S struct {
Public *int
private *int
PublicTime time.Time
PublicTime Time
}
type URL struct{}
type EmbedURLPtr struct {
*url.URL
*URL
}
type S2 struct {
......@@ -58,7 +62,7 @@ type S2 struct {
Extra bool
}
var X int64
var X0 int64
var (
Y int
......@@ -163,7 +167,7 @@ func (*common) OnBothTandBPtr() {}
func (common) OnBothTandBVal() {}
type EmbedSelector struct {
time.Time
Time
}
const (
......@@ -174,10 +178,15 @@ const (
func ellipsis(...string) {}
func Now() Time {
var now Time
return now
}
var x = &S{
Public: nil,
private: nil,
publicTime: time.Now(),
PublicTime: Now(),
}
var parenExpr = (1 + 5)
......
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