]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: extract pkgqual from symfmt
authorMatthew Dempsky <mdempsky@google.com>
Wed, 7 Jul 2021 19:11:37 +0000 (12:11 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Wed, 7 Jul 2021 22:27:48 +0000 (22:27 +0000)
The logic in symfmt for deciding how to package-qualify an identifier
is easily refactored into a separate function, loosely similar to
go/types.Qualifier's API.

Passes toolstash -cmp.

Updates #47087.

Change-Id: Ib3e7cc35a6577dc28df8eca79ba3457c48168e86
Reviewed-on: https://go-review.googlesource.com/c/go/+/333161
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/types/fmt.go

index b538ea805429df5dadbe1060d75f77942cfc7cbd..0ce423dae70b28e181294efd5ec0463708da34fe 100644 (file)
@@ -137,47 +137,44 @@ func sconv2(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
 }
 
 func symfmt(b *bytes.Buffer, s *Sym, verb rune, mode fmtMode) {
+       if q := pkgqual(s.Pkg, verb, mode); q != "" {
+               b.WriteString(q)
+               b.WriteByte('.')
+       }
+       b.WriteString(s.Name)
+}
+
+// pkgqual returns the qualifier that should be used for printing
+// symbols from the given package in the given mode.
+// If it returns the empty string, no qualification is needed.
+func pkgqual(pkg *Pkg, verb rune, mode fmtMode) string {
        if verb != 'S' {
                switch mode {
                case fmtGo: // This is for the user
-                       if s.Pkg == BuiltinPkg || s.Pkg == LocalPkg {
-                               b.WriteString(s.Name)
-                               return
+                       if pkg == BuiltinPkg || pkg == LocalPkg {
+                               return ""
                        }
 
                        // If the name was used by multiple packages, display the full path,
-                       if s.Pkg.Name != "" && NumImport[s.Pkg.Name] > 1 {
-                               fmt.Fprintf(b, "%q.%s", s.Pkg.Path, s.Name)
-                               return
+                       if pkg.Name != "" && NumImport[pkg.Name] > 1 {
+                               return strconv.Quote(pkg.Path)
                        }
-                       b.WriteString(s.Pkg.Name)
-                       b.WriteByte('.')
-                       b.WriteString(s.Name)
-                       return
+                       return pkg.Name
 
                case fmtDebug:
-                       b.WriteString(s.Pkg.Name)
-                       b.WriteByte('.')
-                       b.WriteString(s.Name)
-                       return
+                       return pkg.Name
 
                case fmtTypeIDName:
                        // dcommontype, typehash
-                       b.WriteString(s.Pkg.Name)
-                       b.WriteByte('.')
-                       b.WriteString(s.Name)
-                       return
+                       return pkg.Name
 
                case fmtTypeID:
                        // (methodsym), typesym, weaksym
-                       b.WriteString(s.Pkg.Prefix)
-                       b.WriteByte('.')
-                       b.WriteString(s.Name)
-                       return
+                       return pkg.Prefix
                }
        }
 
-       b.WriteString(s.Name)
+       return ""
 }
 
 // Type