From 124c8121e1e9410c0d292882ea215c0cb09ec456 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Mon, 14 Jan 2019 11:48:43 -0800 Subject: [PATCH] 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 --- protogen/names.go | 42 ------------------------------------------ protogen/protogen.go | 9 ++++++++- 2 files changed, 8 insertions(+), 43 deletions(-) diff --git a/protogen/names.go b/protogen/names.go index e291805f..a1bddf7b 100644 --- a/protogen/names.go +++ b/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 diff --git a/protogen/protogen.go b/protogen/protogen.go index 9c279b83..eb89cfa5 100644 --- a/protogen/protogen.go +++ b/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