Commit 06eeea21 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: permit use of %S (short) and %L (long) instead of %1v and %2v

First step towards cleaning up format use. Not yet enabled.

Change-Id: Ia8d76bf02fe05882fffb9d17c9a30dc38d28bf81
Reviewed-on: https://go-review.googlesource.com/28784Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent d38d59ff
...@@ -22,13 +22,13 @@ const ( // fmt.Format flag/width/prec ...@@ -22,13 +22,13 @@ const ( // fmt.Format flag/width/prec
FmtSharp // "#" => '#' FmtSharp // "#" => '#'
FmtSign // "+" => '+' FmtSign // "+" => '+'
FmtUnsigned // "u" => ' ' FmtUnsigned // "u" => ' '
FmtShort // "h" => hasWidth && width == 1 FmtShort // "h" => hasWidth && width == 1; or verb == 'S' (Short)
FmtLong // "l" => hasWidth && width == 2 FmtLong // "l" => hasWidth && width == 2; or verb == 'L' (Long)
FmtComma // "," => '.' (== hasPrec) FmtComma // "," => '.' (== hasPrec)
FmtByte // "hh" => '0' FmtByte // "hh" => '0'
) )
func fmtFlag(s fmt.State) FmtFlag { func fmtFlag(s fmt.State, verb rune) FmtFlag {
var flag FmtFlag var flag FmtFlag
if s.Flag('-') { if s.Flag('-') {
flag |= FmtLeft flag |= FmtLeft
...@@ -56,6 +56,12 @@ func fmtFlag(s fmt.State) FmtFlag { ...@@ -56,6 +56,12 @@ func fmtFlag(s fmt.State) FmtFlag {
if s.Flag('0') { if s.Flag('0') {
flag |= FmtByte flag |= FmtByte
} }
switch verb {
case 'S':
flag |= FmtShort
case 'L':
flag |= FmtLong
}
return flag return flag
} }
...@@ -220,18 +226,17 @@ func (o Op) GoString() string { ...@@ -220,18 +226,17 @@ func (o Op) GoString() string {
return fmt.Sprintf("%#v", o) return fmt.Sprintf("%#v", o)
} }
func (o Op) Format(s fmt.State, format rune) { func (o Op) Format(s fmt.State, verb rune) {
switch format { switch verb {
case 's', 'v': case 's', 'v':
o.oconv(s) o.oconv(s, fmtFlag(s, verb))
default: default:
fmt.Fprintf(s, "%%!%c(Op=%d)", format, o) fmt.Fprintf(s, "%%!%c(Op=%d)", verb, int(o))
} }
} }
func (o Op) oconv(s fmt.State) { func (o Op) oconv(s fmt.State, flag FmtFlag) {
flag := fmtFlag(s)
if (flag&FmtSharp != 0) || fmtmode != FDbg { if (flag&FmtSharp != 0) || fmtmode != FDbg {
if o >= 0 && int(o) < len(goopnames) && goopnames[o] != "" { if o >= 0 && int(o) < len(goopnames) && goopnames[o] != "" {
fmt.Fprint(s, goopnames[o]) fmt.Fprint(s, goopnames[o])
...@@ -244,7 +249,7 @@ func (o Op) oconv(s fmt.State) { ...@@ -244,7 +249,7 @@ func (o Op) oconv(s fmt.State) {
return return
} }
fmt.Fprintf(s, "O-%d", o) fmt.Fprintf(s, "O-%d", int(o))
} }
var classnames = []string{ var classnames = []string{
...@@ -257,22 +262,22 @@ var classnames = []string{ ...@@ -257,22 +262,22 @@ var classnames = []string{
"PFUNC", "PFUNC",
} }
func (n *Node) Format(s fmt.State, format rune) { func (n *Node) Format(s fmt.State, verb rune) {
switch format { switch verb {
case 's', 'v': case 's', 'v':
n.Nconv(s) n.Nconv(s, fmtFlag(s, verb))
case 'j': case 'j':
n.jconv(s) n.jconv(s, fmtFlag(s, verb))
default: default:
fmt.Fprintf(s, "%%!%c(*Node=%p)", format, n) fmt.Fprintf(s, "%%!%c(*Node=%p)", verb, n)
} }
} }
// Node details // Node details
func (n *Node) jconv(s fmt.State) { func (n *Node) jconv(s fmt.State, flag FmtFlag) {
c := fmtFlag(s) & FmtShort c := flag & FmtShort
if c == 0 && n.Ullman != 0 { if c == 0 && n.Ullman != 0 {
fmt.Fprintf(s, " u(%d)", n.Ullman) fmt.Fprintf(s, " u(%d)", n.Ullman)
...@@ -378,20 +383,18 @@ func (n *Node) jconv(s fmt.State) { ...@@ -378,20 +383,18 @@ func (n *Node) jconv(s fmt.State) {
} }
} }
func (v Val) Format(s fmt.State, format rune) { func (v Val) Format(s fmt.State, verb rune) {
switch format { switch verb {
case 's', 'v': case 's', 'v':
v.vconv(s) v.vconv(s, fmtFlag(s, verb))
default: default:
fmt.Fprintf(s, "%%!%c(Val)", format) fmt.Fprintf(s, "%%!%c(Val)", verb)
} }
} }
// Fmt "%V": Values // Fmt "%V": Values
func (v Val) vconv(s fmt.State) { func (v Val) vconv(s fmt.State, flag FmtFlag) {
flag := fmtFlag(s)
switch u := v.U.(type) { switch u := v.U.(type) {
case *Mpint: case *Mpint:
if !u.Rune { if !u.Rune {
...@@ -1582,13 +1585,13 @@ func (n *Node) nodedump(s fmt.State, flag FmtFlag) { ...@@ -1582,13 +1585,13 @@ func (n *Node) nodedump(s fmt.State, flag FmtFlag) {
} }
} }
func (s *Sym) Format(f fmt.State, format rune) { func (s *Sym) Format(f fmt.State, verb rune) {
switch format { switch verb {
case 's', 'v': case 's', 'v':
s.sconv(f) s.sconv(f, fmtFlag(f, verb))
default: default:
fmt.Fprintf(f, "%%!%c(*Sym=%p)", format, s) fmt.Fprintf(f, "%%!%c(*Sym=%p)", verb, s)
} }
} }
...@@ -1598,9 +1601,7 @@ func (s *Sym) String() string { ...@@ -1598,9 +1601,7 @@ func (s *Sym) String() string {
// Fmt "%S": syms // Fmt "%S": syms
// Flags: "%hS" suppresses qualifying with package // Flags: "%hS" suppresses qualifying with package
func (s *Sym) sconv(f fmt.State) { func (s *Sym) sconv(f fmt.State, flag FmtFlag) {
flag := fmtFlag(f)
if flag&FmtLong != 0 { if flag&FmtLong != 0 {
panic("linksymfmt") panic("linksymfmt")
} }
...@@ -1701,13 +1702,13 @@ func Fldconv(f *Field, flag FmtFlag) string { ...@@ -1701,13 +1702,13 @@ func Fldconv(f *Field, flag FmtFlag) string {
return str return str
} }
func (t *Type) Format(s fmt.State, format rune) { func (t *Type) Format(s fmt.State, verb rune) {
switch format { switch verb {
case 's', 'v': case 's', 'v':
t.tconv(s) t.tconv(s, fmtFlag(s, verb))
default: default:
fmt.Fprintf(s, "%%!%c(*Type=%p)", format, t) fmt.Fprintf(s, "%%!%c(*Type=%p)", verb, t)
} }
} }
...@@ -1715,9 +1716,7 @@ func (t *Type) Format(s fmt.State, format rune) { ...@@ -1715,9 +1716,7 @@ func (t *Type) Format(s fmt.State, format rune) {
// Flags: 'l' print definition, not name // Flags: 'l' print definition, not name
// 'h' omit 'func' and receiver from function types, short type names // 'h' omit 'func' and receiver from function types, short type names
// 'u' package name, not prefix (FTypeId mode, sticky) // 'u' package name, not prefix (FTypeId mode, sticky)
func (t *Type) tconv(s fmt.State) { func (t *Type) tconv(s fmt.State, flag FmtFlag) {
flag := fmtFlag(s)
if t == nil { if t == nil {
fmt.Fprint(s, "<T>") fmt.Fprint(s, "<T>")
return return
...@@ -1757,9 +1756,7 @@ func (n *Node) String() string { ...@@ -1757,9 +1756,7 @@ func (n *Node) String() string {
// Fmt '%N': Nodes. // Fmt '%N': Nodes.
// Flags: 'l' suffix with "(type %T)" where possible // Flags: 'l' suffix with "(type %T)" where possible
// '+h' in debug mode, don't recurse, no multiline output // '+h' in debug mode, don't recurse, no multiline output
func (n *Node) Nconv(s fmt.State) { func (n *Node) Nconv(s fmt.State, flag FmtFlag) {
flag := fmtFlag(s)
if n == nil { if n == nil {
fmt.Fprint(s, "<N>") fmt.Fprint(s, "<N>")
return return
...@@ -1785,13 +1782,13 @@ func (n *Node) Nconv(s fmt.State) { ...@@ -1785,13 +1782,13 @@ func (n *Node) Nconv(s fmt.State) {
fmtmode = sm fmtmode = sm
} }
func (l Nodes) Format(s fmt.State, format rune) { func (l Nodes) Format(s fmt.State, verb rune) {
switch format { switch verb {
case 's', 'v': case 's', 'v':
l.hconv(s) l.hconv(s, fmtFlag(s, verb))
default: default:
fmt.Fprintf(s, "%%!%c(Nodes)", format) fmt.Fprintf(s, "%%!%c(Nodes)", verb)
} }
} }
...@@ -1801,9 +1798,7 @@ func (n Nodes) String() string { ...@@ -1801,9 +1798,7 @@ func (n Nodes) String() string {
// Fmt '%H': Nodes. // Fmt '%H': Nodes.
// Flags: all those of %N plus ',': separate with comma's instead of semicolons. // Flags: all those of %N plus ',': separate with comma's instead of semicolons.
func (l Nodes) hconv(s fmt.State) { func (l Nodes) hconv(s fmt.State, flag FmtFlag) {
flag := fmtFlag(s)
if l.Len() == 0 && fmtmode == FDbg { if l.Len() == 0 && fmtmode == FDbg {
fmt.Fprint(s, "<nil>") fmt.Fprint(s, "<nil>")
return return
......
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