Просмотр исходного кода

protogen: use types.Universe.Names for list of builtin identifiers

Rather than maintaining our own list of builtin Go identifiers, use the
types.Universe API in the standard library instead.

It is okay if this list of names changes over time since we use this only
to derive a locally-used package name in the generated file.

Change-Id: Ib1688abc47d5c97b557f6e1f4d60c78e0951e65b
Reviewed-on: https://go-review.googlesource.com/c/157818
Reviewed-by: Damien Neil <dneil@google.com>
Joe Tsai 7 лет назад
Родитель
Сommit
124c8121e1
2 измененных файлов с 8 добавлено и 43 удалено
  1. 0 42
      protogen/names.go
  2. 8 1
      protogen/protogen.go

+ 0 - 42
protogen/names.go

@@ -83,48 +83,6 @@ func cleanGoName(s string, mustExport bool) string {
 	return s
 }
 
-var isGoPredeclaredIdentifier = map[string]bool{
-	"append":     true,
-	"bool":       true,
-	"byte":       true,
-	"cap":        true,
-	"close":      true,
-	"complex":    true,
-	"complex128": true,
-	"complex64":  true,
-	"copy":       true,
-	"delete":     true,
-	"error":      true,
-	"false":      true,
-	"float32":    true,
-	"float64":    true,
-	"imag":       true,
-	"int":        true,
-	"int16":      true,
-	"int32":      true,
-	"int64":      true,
-	"int8":       true,
-	"iota":       true,
-	"len":        true,
-	"make":       true,
-	"new":        true,
-	"nil":        true,
-	"panic":      true,
-	"print":      true,
-	"println":    true,
-	"real":       true,
-	"recover":    true,
-	"rune":       true,
-	"string":     true,
-	"true":       true,
-	"uint":       true,
-	"uint16":     true,
-	"uint32":     true,
-	"uint64":     true,
-	"uint8":      true,
-	"uintptr":    true,
-}
-
 // baseName returns the last path element of the name, with the last dotted suffix removed.
 func baseName(name string) string {
 	// First, find the last element

+ 8 - 1
protogen/protogen.go

@@ -19,6 +19,7 @@ import (
 	"go/parser"
 	"go/printer"
 	"go/token"
+	"go/types"
 	"io/ioutil"
 	"os"
 	"path"
@@ -851,6 +852,12 @@ func (gen *Plugin) NewGeneratedFile(filename string, goImportPath GoImportPath)
 		manualImports:    make(map[GoImportPath]bool),
 		annotations:      make(map[string][]Location),
 	}
+
+	// All predeclared identifiers in Go are already used.
+	for _, s := range types.Universe.Names() {
+		g.usedPackageNames[GoPackageName(s)] = true
+	}
+
 	gen.genFiles = append(gen.genFiles, g)
 	return g
 }
@@ -906,7 +913,7 @@ func (g *GeneratedFile) QualifiedGoIdent(ident GoIdent) string {
 		return string(packageName) + "." + ident.GoName
 	}
 	packageName := cleanPackageName(baseName(string(ident.GoImportPath)))
-	for i, orig := 1, packageName; g.usedPackageNames[packageName] || isGoPredeclaredIdentifier[string(packageName)]; i++ {
+	for i, orig := 1, packageName; g.usedPackageNames[packageName]; i++ {
 		packageName = orig + GoPackageName(strconv.Itoa(i))
 	}
 	g.packageNames[ident.GoImportPath] = packageName