Browse Source

protoc-gen-go: predeclared identifiers in cleanPackageName (#722)

Don't generate identifiers that conflict with predeclared identifiers,
prepend with "_" instead.

Change-Id: I85e29d1861bc225df29949b26f4ae4a740bb4a66
Cherry-Pick: github.com/golang/protobuf@31e0d063dd98c052257e5b69eeb006818133f45c
Original-Author: Sam Smith <sesmith177@gmail.com>
Reviewed-on: https://go-review.googlesource.com/c/151423
Reviewed-by: Damien Neil <dneil@google.com>
Joe Tsai 7 years ago
parent
commit
e52a201209
2 changed files with 46 additions and 3 deletions
  1. 45 3
      protoc-gen-go/generator/generator.go
  2. 1 0
      protoc-gen-go/generator/name_test.go

+ 45 - 3
protoc-gen-go/generator/generator.go

@@ -568,7 +568,8 @@ func RegisterUniquePackageName(pkg string, f *FileDescriptor) string {
 	return string(name)
 	return string(name)
 }
 }
 
 
-var isGoKeyword = map[string]bool{
+var isGoKeywordOrPredeclaredIdentifier = map[string]bool{
+	// Keywords
 	"break":       true,
 	"break":       true,
 	"case":        true,
 	"case":        true,
 	"chan":        true,
 	"chan":        true,
@@ -594,12 +595,53 @@ var isGoKeyword = map[string]bool{
 	"switch":      true,
 	"switch":      true,
 	"type":        true,
 	"type":        true,
 	"var":         true,
 	"var":         true,
+
+	// Predeclared Identifiers
+	"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,
 }
 }
 
 
 func cleanPackageName(name string) GoPackageName {
 func cleanPackageName(name string) GoPackageName {
 	name = strings.Map(badToUnderscore, name)
 	name = strings.Map(badToUnderscore, name)
-	// Identifier must not be keyword: insert _.
-	if isGoKeyword[name] {
+	// Identifier must not be keyword or predeclared identifier: insert _.
+	if isGoKeywordOrPredeclaredIdentifier[name] {
 		name = "_" + name
 		name = "_" + name
 	}
 	}
 	// Identifier must not begin with digit: insert _.
 	// Identifier must not begin with digit: insert _.

+ 1 - 0
protoc-gen-go/generator/name_test.go

@@ -68,6 +68,7 @@ func TestGoPackageOption(t *testing.T) {
 		{"foo", "", "foo", true},
 		{"foo", "", "foo", true},
 		{"github.com/golang/bar", "github.com/golang/bar", "bar", true},
 		{"github.com/golang/bar", "github.com/golang/bar", "bar", true},
 		{"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
 		{"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
+		{"github.com/golang/string", "github.com/golang/string", "_string", true},
 	}
 	}
 	for _, tc := range tests {
 	for _, tc := range tests {
 		d := &FileDescriptor{
 		d := &FileDescriptor{