diff options
author | Ian Lance Taylor <iant@golang.org> | 2018-09-24 21:46:21 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2018-09-24 21:46:21 +0000 |
commit | dd931d9b48647e898dc80927c532ae93cc09e192 (patch) | |
tree | 71be2295cd79b8a182f6130611658db8628772d5 /libgo/go/go/printer | |
parent | 779d8a5ad09b01428726ea5a0e6c87bd9ac3c0e4 (diff) |
libgo: update to Go 1.11
Reviewed-on: https://go-review.googlesource.com/136435
gotools/:
* Makefile.am (mostlyclean-local): Run chmod on check-go-dir to
make sure it is writable.
(check-go-tools): Likewise.
(check-vet): Copy internal/objabi to check-vet-dir.
* Makefile.in: Rebuild.
From-SVN: r264546
Diffstat (limited to 'libgo/go/go/printer')
-rw-r--r-- | libgo/go/go/printer/nodes.go | 143 | ||||
-rw-r--r-- | libgo/go/go/printer/printer.go | 67 | ||||
-rw-r--r-- | libgo/go/go/printer/printer_test.go | 28 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/alignment.golden | 172 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/alignment.input | 179 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/comments.golden | 4 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/comments.input | 4 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/complit.input | 65 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/complit.x | 62 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/declarations.golden | 8 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/linebreaks.golden | 22 | ||||
-rw-r--r-- | libgo/go/go/printer/testdata/linebreaks.input | 24 |
12 files changed, 689 insertions, 89 deletions
diff --git a/libgo/go/go/printer/nodes.go b/libgo/go/go/printer/nodes.go index 3e2ff4f5ae3..1de7cd81b2c 100644 --- a/libgo/go/go/printer/nodes.go +++ b/libgo/go/go/printer/nodes.go @@ -12,6 +12,7 @@ import ( "bytes" "go/ast" "go/token" + "math" "strconv" "strings" "unicode" @@ -31,8 +32,10 @@ import ( // Print as many newlines as necessary (but at least min newlines) to get to // the current line. ws is printed before the first line break. If newSection -// is set, the first line break is printed as formfeed. Returns true if any -// line break was printed; returns false otherwise. +// is set, the first line break is printed as formfeed. Returns 0 if no line +// breaks were printed, returns 1 if there was exactly one newline printed, +// and returns a value > 1 if there was a formfeed or more than one newline +// printed. // // TODO(gri): linebreak may add too many lines if the next statement at "line" // is preceded by comments because the computation of n assumes @@ -42,7 +45,7 @@ import ( // linebreaks. At the moment there is no easy way to know about // future (not yet interspersed) comments in this function. // -func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (printedBreak bool) { +func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (nbreaks int) { n := nlimit(line - p.pos.Line) if n < min { n = min @@ -52,11 +55,12 @@ func (p *printer) linebreak(line, min int, ws whiteSpace, newSection bool) (prin if newSection { p.print(formfeed) n-- + nbreaks = 2 } + nbreaks += n for ; n > 0; n-- { p.print(newline) } - printedBreak = true } return } @@ -111,9 +115,11 @@ func (p *printer) identList(list []*ast.Ident, indent bool) { if !indent { mode = noIndent } - p.exprList(token.NoPos, xlist, 1, mode, token.NoPos) + p.exprList(token.NoPos, xlist, 1, mode, token.NoPos, false) } +const filteredMsg = "contains filtered or unexported fields" + // Print a list of expressions. If the list spans multiple // source lines, the original line breaks are respected between // expressions. @@ -121,8 +127,18 @@ func (p *printer) identList(list []*ast.Ident, indent bool) { // TODO(gri) Consider rewriting this to be independent of []ast.Expr // so that we can use the algorithm for any kind of list // (e.g., pass list via a channel over which to range). -func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exprListMode, next0 token.Pos) { +func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exprListMode, next0 token.Pos, isIncomplete bool) { if len(list) == 0 { + if isIncomplete { + prev := p.posFor(prev0) + next := p.posFor(next0) + if prev.IsValid() && prev.Line == next.Line { + p.print("/* " + filteredMsg + " */") + } else { + p.print(newline) + p.print(indent, "// "+filteredMsg, unindent, newline) + } + } return } @@ -141,23 +157,26 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp } p.expr0(x, depth) } + if isIncomplete { + p.print(token.COMMA, blank, "/* "+filteredMsg+" */") + } return } // list entries span multiple lines; // use source code positions to guide line breaks - // don't add extra indentation if noIndent is set; - // i.e., pretend that the first line is already indented + // Don't add extra indentation if noIndent is set; + // i.e., pretend that the first line is already indented. ws := ignore if mode&noIndent == 0 { ws = indent } - // the first linebreak is always a formfeed since this section must not - // depend on any previous formatting + // The first linebreak is always a formfeed since this section must not + // depend on any previous formatting. prevBreak := -1 // index of last expression that was followed by a linebreak - if prev.IsValid() && prev.Line < line && p.linebreak(line, 0, ws, true) { + if prev.IsValid() && prev.Line < line && p.linebreak(line, 0, ws, true) > 0 { ws = ignore prevBreak = 0 } @@ -165,22 +184,29 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp // initialize expression/key size: a zero value indicates expr/key doesn't fit on a single line size := 0 + // We use the ratio between the geometric mean of the previous key sizes and + // the current size to determine if there should be a break in the alignment. + // To compute the geometric mean we accumulate the ln(size) values (lnsum) + // and the number of sizes included (count). + lnsum := 0.0 + count := 0 + // print all list elements prevLine := prev.Line for i, x := range list { line = p.lineFor(x.Pos()) - // determine if the next linebreak, if any, needs to use formfeed: + // Determine if the next linebreak, if any, needs to use formfeed: // in general, use the entire node size to make the decision; for - // key:value expressions, use the key size + // key:value expressions, use the key size. // TODO(gri) for a better result, should probably incorporate both // the key and the node size into the decision process useFF := true - // determine element size: all bets are off if we don't have + // Determine element size: All bets are off if we don't have // position information for the previous and next token (likely // generated code - simply ignore the size in this case by setting - // it to 0) + // it to 0). prevSize := size const infinity = 1e6 // larger than any source line size = p.nodeSize(x, infinity) @@ -195,40 +221,51 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp size = 0 } - // if the previous line and the current line had single- + // If the previous line and the current line had single- // line-expressions and the key sizes are small or the - // the ratio between the key sizes does not exceed a - // threshold, align columns and do not use formfeed + // ratio between the current key and the geometric mean + // if the previous key sizes does not exceed a threshold, + // align columns and do not use formfeed. if prevSize > 0 && size > 0 { - const smallSize = 20 - if prevSize <= smallSize && size <= smallSize { + const smallSize = 40 + if count == 0 || prevSize <= smallSize && size <= smallSize { useFF = false } else { - const r = 4 // threshold - ratio := float64(size) / float64(prevSize) - useFF = ratio <= 1.0/r || r <= ratio + const r = 2.5 // threshold + geomean := math.Exp(lnsum / float64(count)) // count > 0 + ratio := float64(size) / geomean + useFF = r*ratio <= 1 || r <= ratio } } needsLinebreak := 0 < prevLine && prevLine < line if i > 0 { - // use position of expression following the comma as + // Use position of expression following the comma as // comma position for correct comment placement, but - // only if the expression is on the same line + // only if the expression is on the same line. if !needsLinebreak { p.print(x.Pos()) } p.print(token.COMMA) needsBlank := true if needsLinebreak { - // lines are broken using newlines so comments remain aligned - // unless forceFF is set or there are multiple expressions on - // the same line in which case formfeed is used - if p.linebreak(line, 0, ws, useFF || prevBreak+1 < i) { + // Lines are broken using newlines so comments remain aligned + // unless useFF is set or there are multiple expressions on + // the same line in which case formfeed is used. + nbreaks := p.linebreak(line, 0, ws, useFF || prevBreak+1 < i) + if nbreaks > 0 { ws = ignore prevBreak = i needsBlank = false // we got a line break instead } + // If there was a new section or more than one new line + // (which means that the tabwriter will implicitly break + // the section), reset the geomean variables since we are + // starting a new group of elements with the next element. + if nbreaks > 1 { + lnsum = 0 + count = 0 + } } if needsBlank { p.print(blank) @@ -236,10 +273,10 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp } if len(list) > 1 && isPair && size > 0 && needsLinebreak { - // we have a key:value expression that fits onto one line + // We have a key:value expression that fits onto one line // and it's not on the same line as the prior expression: - // use a column for the key such that consecutive entries - // can align if possible + // Use a column for the key such that consecutive entries + // can align if possible. // (needsLinebreak is set if we started a new line before) p.expr(pair.Key) p.print(pair.Colon, token.COLON, vtab) @@ -248,12 +285,21 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp p.expr0(x, depth) } + if size > 0 { + lnsum += math.Log(float64(size)) + count++ + } + prevLine = line } if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line { - // print a terminating comma if the next token is on a new line + // Print a terminating comma if the next token is on a new line. p.print(token.COMMA) + if isIncomplete { + p.print(newline) + p.print("// " + filteredMsg) + } if ws == ignore && mode&noIndent == 0 { // unindent if we indented p.print(unindent) @@ -262,6 +308,11 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp return } + if isIncomplete { + p.print(token.COMMA, newline) + p.print("// "+filteredMsg, newline) + } + if ws == ignore && mode&noIndent == 0 { // unindent if we indented p.print(unindent) @@ -296,7 +347,7 @@ func (p *printer) parameters(fields *ast.FieldList) { p.print(token.COMMA) } // separator if needed (linebreak or blank) - if needsLinebreak && p.linebreak(parLineBeg, 0, ws, true) { + if needsLinebreak && p.linebreak(parLineBeg, 0, ws, true) > 0 { // break line if the opening "(" or previous parameter ended on a different line ws = ignore } else if i > 0 { @@ -481,7 +532,7 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) p.print(formfeed) } p.flush(p.posFor(rbrace), token.RBRACE) // make sure we don't lose the last line comment - p.setLineComment("// contains filtered or unexported fields") + p.setLineComment("// " + filteredMsg) } } else { // interface @@ -667,7 +718,7 @@ func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int) { if xline != yline && xline > 0 && yline > 0 { // at least one line break, but respect an extra empty line // in the source - if p.linebreak(yline, 1, ws, true) { + if p.linebreak(yline, 1, ws, true) > 0 { ws = ignore printBlank = false // no blank after line break } @@ -833,13 +884,13 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) { } p.print(x.Lparen, token.LPAREN) if x.Ellipsis.IsValid() { - p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis) + p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis, false) p.print(x.Ellipsis, token.ELLIPSIS) if x.Rparen.IsValid() && p.lineFor(x.Ellipsis) < p.lineFor(x.Rparen) { p.print(token.COMMA, formfeed) } } else { - p.exprList(x.Lparen, x.Args, depth, commaTerm, x.Rparen) + p.exprList(x.Lparen, x.Args, depth, commaTerm, x.Rparen, false) } p.print(x.Rparen, token.RPAREN) if wasIndented { @@ -853,7 +904,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) { } p.level++ p.print(x.Lbrace, token.LBRACE) - p.exprList(x.Lbrace, x.Elts, 1, commaTerm, x.Rbrace) + p.exprList(x.Lbrace, x.Elts, 1, commaTerm, x.Rbrace, x.Incomplete) // do not insert extra line break following a /*-style comment // before the closing '}' as it might break the code if there // is no trailing ',' @@ -1163,9 +1214,9 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) { if len(s.Lhs) > 1 && len(s.Rhs) > 1 { depth++ } - p.exprList(s.Pos(), s.Lhs, depth, 0, s.TokPos) + p.exprList(s.Pos(), s.Lhs, depth, 0, s.TokPos, false) p.print(blank, s.TokPos, s.Tok, blank) - p.exprList(s.TokPos, s.Rhs, depth, 0, token.NoPos) + p.exprList(s.TokPos, s.Rhs, depth, 0, token.NoPos, false) case *ast.GoStmt: p.print(token.GO, blank) @@ -1186,10 +1237,10 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) { // lead to more nicely formatted code in general. if p.indentList(s.Results) { p.print(indent) - p.exprList(s.Pos(), s.Results, 1, noIndent, token.NoPos) + p.exprList(s.Pos(), s.Results, 1, noIndent, token.NoPos, false) p.print(unindent) } else { - p.exprList(s.Pos(), s.Results, 1, 0, token.NoPos) + p.exprList(s.Pos(), s.Results, 1, 0, token.NoPos, false) } } @@ -1225,7 +1276,7 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) { case *ast.CaseClause: if s.List != nil { p.print(token.CASE, blank) - p.exprList(s.Pos(), s.List, 1, 0, s.Colon) + p.exprList(s.Pos(), s.List, 1, 0, s.Colon, false) } else { p.print(token.DEFAULT) } @@ -1377,7 +1428,7 @@ func (p *printer) valueSpec(s *ast.ValueSpec, keepType bool) { } if s.Values != nil { p.print(vtab, token.ASSIGN, blank) - p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos) + p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos, false) extraTabs-- } if s.Comment != nil { @@ -1459,7 +1510,7 @@ func (p *printer) spec(spec ast.Spec, n int, doIndent bool) { } if s.Values != nil { p.print(blank, token.ASSIGN, blank) - p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos) + p.exprList(token.NoPos, s.Values, 1, 0, token.NoPos, false) } p.setComment(s.Comment) diff --git a/libgo/go/go/printer/printer.go b/libgo/go/go/printer/printer.go index dbb4bbd90cf..9143442a27c 100644 --- a/libgo/go/go/printer/printer.go +++ b/libgo/go/go/printer/printer.go @@ -11,7 +11,6 @@ import ( "go/token" "io" "os" - "strconv" "strings" "text/tabwriter" "unicode" @@ -56,14 +55,15 @@ type printer struct { fset *token.FileSet // Current state - output []byte // raw printer result - indent int // current indentation - level int // level == 0: outside composite literal; level > 0: inside composite literal - mode pmode // current printer mode - impliedSemi bool // if set, a linebreak implies a semicolon - lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace) - prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL - wsbuf []whiteSpace // delayed white space + output []byte // raw printer result + indent int // current indentation + level int // level == 0: outside composite literal; level > 0: inside composite literal + mode pmode // current printer mode + endAlignment bool // if set, terminate alignment immediately + impliedSemi bool // if set, a linebreak implies a semicolon + lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace) + prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL + wsbuf []whiteSpace // delayed white space // Positions // The out position differs from the pos position when the result @@ -192,13 +192,13 @@ func (p *printer) linesFrom(line int) int { func (p *printer) posFor(pos token.Pos) token.Position { // not used frequently enough to cache entire token.Position - return p.fset.Position(pos) + return p.fset.PositionFor(pos, false /* absolute position */) } func (p *printer) lineFor(pos token.Pos) int { if pos != p.cachedPos { p.cachedPos = pos - p.cachedLine = p.fset.Position(pos).Line + p.cachedLine = p.fset.PositionFor(pos, false /* absolute position */).Line } return p.cachedLine } @@ -233,6 +233,20 @@ func (p *printer) writeIndent() { // writeByte writes ch n times to p.output and updates p.pos. // Only used to write formatting (white space) characters. func (p *printer) writeByte(ch byte, n int) { + if p.endAlignment { + // Ignore any alignment control character; + // and at the end of the line, break with + // a formfeed to indicate termination of + // existing columns. + switch ch { + case '\t', '\v': + ch = ' ' + case '\n', '\f': + ch = '\f' + p.endAlignment = false + } + } + if p.out.Column == 1 { // no need to write line directives before white space p.writeIndent() @@ -299,10 +313,15 @@ func (p *printer) writeString(pos token.Position, s string, isLit bool) { nlines := 0 var li int // index of last newline; valid if nlines > 0 for i := 0; i < len(s); i++ { - // Go tokens cannot contain '\f' - no need to look for it - if s[i] == '\n' { + // Raw string literals may contain any character except back quote (`). + if ch := s[i]; ch == '\n' || ch == '\f' { + // account for line break nlines++ li = i + // A line break inside a literal will break whatever column + // formatting is in place; ignore any further alignment through + // the end of the line. + p.endAlignment = true } } p.pos.Offset += len(s) @@ -622,24 +641,10 @@ func (p *printer) writeComment(comment *ast.Comment) { const linePrefix = "//line " if strings.HasPrefix(text, linePrefix) && (!pos.IsValid() || pos.Column == 1) { - // possibly a line directive - ldir := strings.TrimSpace(text[len(linePrefix):]) - if i := strings.LastIndex(ldir, ":"); i >= 0 { - if line, err := strconv.Atoi(ldir[i+1:]); err == nil && line > 0 { - // The line directive we are about to print changed - // the Filename and Line number used for subsequent - // tokens. We have to update our AST-space position - // accordingly and suspend indentation temporarily. - indent := p.indent - p.indent = 0 - defer func() { - p.pos.Filename = ldir[:i] - p.pos.Line = line - p.pos.Column = 1 - p.indent = indent - }() - } - } + // Possibly a //-style line directive. + // Suspend indentation temporarily to keep line directive valid. + defer func(indent int) { p.indent = indent }(p.indent) + p.indent = 0 } // shortcut common case of //-style comments diff --git a/libgo/go/go/printer/printer_test.go b/libgo/go/go/printer/printer_test.go index 5984d2c4d24..27d46df6b18 100644 --- a/libgo/go/go/printer/printer_test.go +++ b/libgo/go/go/printer/printer_test.go @@ -188,12 +188,14 @@ var data = []entry{ {"comments.input", "comments.golden", 0}, {"comments.input", "comments.x", export}, {"comments2.input", "comments2.golden", idempotent}, + {"alignment.input", "alignment.golden", idempotent}, {"linebreaks.input", "linebreaks.golden", idempotent}, {"expressions.input", "expressions.golden", idempotent}, {"expressions.input", "expressions.raw", rawFormat | idempotent}, {"declarations.input", "declarations.golden", 0}, {"statements.input", "statements.golden", 0}, {"slow.input", "slow.golden", idempotent}, + {"complit.input", "complit.x", export}, } func TestFiles(t *testing.T) { @@ -325,7 +327,7 @@ func fibo(n int) { comment := f.Comments[0].List[0] pos := comment.Pos() - if fset.Position(pos).Offset != 1 { + if fset.PositionFor(pos, false /* absolute position */).Offset != 1 { t.Error("expected offset 1") // error in test } @@ -422,6 +424,7 @@ func (t *t) foo(a, b, c int) int { t.Errorf("got ident %s; want %s", i2.Name, i1.Name) } + // here we care about the relative (line-directive adjusted) positions l1 := fset.Position(i1.Pos()).Line l2 := fset.Position(i2.Pos()).Line if l2 != l1 { @@ -710,3 +713,26 @@ type bar int // comment2 t.Errorf("got %q, want %q", buf.String(), bar) } } + +func TestIssue11151(t *testing.T) { + const src = "package p\t/*\r/1\r*\r/2*\r\r\r\r/3*\r\r+\r\r/4*/\n" + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "", src, parser.ParseComments) + if err != nil { + t.Fatal(err) + } + + var buf bytes.Buffer + Fprint(&buf, fset, f) + got := buf.String() + const want = "package p\t/*/1*\r/2*\r/3*+/4*/\n" // \r following opening /* should be stripped + if got != want { + t.Errorf("\ngot : %q\nwant: %q", got, want) + } + + // the resulting program must be valid + _, err = parser.ParseFile(fset, "", got, 0) + if err != nil { + t.Errorf("%v\norig: %q\ngot : %q", err, src, got) + } +} diff --git a/libgo/go/go/printer/testdata/alignment.golden b/libgo/go/go/printer/testdata/alignment.golden new file mode 100644 index 00000000000..96086ed9068 --- /dev/null +++ b/libgo/go/go/printer/testdata/alignment.golden @@ -0,0 +1,172 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package alignment + +// ---------------------------------------------------------------------------- +// Examples from issue #7335. + +func main() { + z := MyStruct{ + Foo: "foo", + Bar: "bar", + Name: "name", + LongName: "longname", + Baz: "baz", + } + y := MyStruct{ + Foo: "foo", + Bar: "bar", + NameXX: "name", + LongNameXXXXXXXXXXXXX: "longname", + Baz: "baz", + } + z := MyStruct{ + Foo: "foo", + Bar: "bar", + Name: "name", + LongNameXXXXXXXXXXXXX: "longname", + Baz: "baz", + } +} + +// ---------------------------------------------------------------------------- +// Examples from issue #10392. + +var kcfg = KubeletConfig{ + Address: s.Address, + AllowPrivileged: s.AllowPrivileged, + HostNetworkSources: hostNetworkSources, + HostnameOverride: s.HostnameOverride, + RootDirectory: s.RootDirectory, + ConfigFile: s.Config, + ManifestURL: s.ManifestURL, + FileCheckFrequency: s.FileCheckFrequency, + HTTPCheckFrequency: s.HTTPCheckFrequency, + PodInfraContainerImage: s.PodInfraContainerImage, + SyncFrequency: s.SyncFrequency, + RegistryPullQPS: s.RegistryPullQPS, + RegistryBurst: s.RegistryBurst, + MinimumGCAge: s.MinimumGCAge, + MaxPerPodContainerCount: s.MaxPerPodContainerCount, + MaxContainerCount: s.MaxContainerCount, + ClusterDomain: s.ClusterDomain, + ClusterDNS: s.ClusterDNS, + Runonce: s.RunOnce, + Port: s.Port, + ReadOnlyPort: s.ReadOnlyPort, + CadvisorInterface: cadvisorInterface, + EnableServer: s.EnableServer, + EnableDebuggingHandlers: s.EnableDebuggingHandlers, + DockerClient: dockertools.ConnectToDockerOrDie(s.DockerEndpoint), + KubeClient: client, + MasterServiceNamespace: s.MasterServiceNamespace, + VolumePlugins: ProbeVolumePlugins(), + NetworkPlugins: ProbeNetworkPlugins(), + NetworkPluginName: s.NetworkPluginName, + StreamingConnectionIdleTimeout: s.StreamingConnectionIdleTimeout, + TLSOptions: tlsOptions, + ImageGCPolicy: imageGCPolicy, imageGCPolicy, + Cloud: cloud, + NodeStatusUpdateFrequency: s.NodeStatusUpdateFrequency, +} + +var a = A{ + Long: 1, + LongLong: 1, + LongLongLong: 1, + LongLongLongLong: 1, + LongLongLongLongLong: 1, + LongLongLongLongLongLong: 1, + LongLongLongLongLongLongLong: 1, + LongLongLongLongLongLongLongLong: 1, + Short: 1, + LongLongLongLongLongLongLongLongLong: 3, +} + +// ---------------------------------------------------------------------------- +// Examples from issue #22852. + +var fmtMap = map[string]string{ + "1": "123", + "12": "123", + "123": "123", + "1234": "123", + "12345": "123", + "123456": "123", + "12345678901234567890123456789": "123", + "abcde": "123", + "123456789012345678901234567890": "123", + "1234567": "123", + "abcdefghijklmnopqrstuvwxyzabcd": "123", + "abcd": "123", +} + +type Fmt struct { + abcdefghijklmnopqrstuvwx string + abcdefghijklmnopqrstuvwxy string + abcdefghijklmnopqrstuvwxyz string + abcdefghijklmnopqrstuvwxyza string + abcdefghijklmnopqrstuvwxyzab string + abcdefghijklmnopqrstuvwxyzabc string + abcde string + abcdefghijklmnopqrstuvwxyzabcde string + abcdefg string +} + +func main() { + _ := Fmt{ + abcdefghijklmnopqrstuvwx: "foo", + abcdefghijklmnopqrstuvwxyza: "foo", + abcdefghijklmnopqrstuvwxyzab: "foo", + abcdefghijklmnopqrstuvwxyzabc: "foo", + abcde: "foo", + abcdefghijklmnopqrstuvwxyzabcde: "foo", + abcdefg: "foo", + abcdefghijklmnopqrstuvwxy: "foo", + abcdefghijklmnopqrstuvwxyz: "foo", + } +} + +// ---------------------------------------------------------------------------- +// Examples from issue #26352. + +var _ = map[int]string{ + 1: "", + + 12345678901234567890123456789: "", + 12345678901234567890123456789012345678: "", +} + +func f() { + _ = map[int]string{ + 1: "", + + 12345678901234567: "", + 12345678901234567890123456789012345678901: "", + } +} + +// ---------------------------------------------------------------------------- +// Examples from issue #26930. + +var _ = S{ + F1: []string{}, + F2____: []string{}, +} + +var _ = S{ + F1: []string{}, + F2____: []string{}, +} + +var _ = S{ + F1____: []string{}, + F2: []string{}, +} + +var _ = S{ + F1____: []string{}, + F2: []string{}, +} diff --git a/libgo/go/go/printer/testdata/alignment.input b/libgo/go/go/printer/testdata/alignment.input new file mode 100644 index 00000000000..323d2689e0b --- /dev/null +++ b/libgo/go/go/printer/testdata/alignment.input @@ -0,0 +1,179 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package alignment + +// ---------------------------------------------------------------------------- +// Examples from issue #7335. + +func main() { + z := MyStruct{ + Foo: "foo", + Bar: "bar", + Name: "name", + LongName: "longname", + Baz: "baz", + } + y := MyStruct{ + Foo: "foo", + Bar: "bar", + NameXX: "name", + LongNameXXXXXXXXXXXXX: "longname", + Baz: "baz", + } + z := MyStruct{ + Foo: "foo", + Bar: "bar", + Name: "name", + LongNameXXXXXXXXXXXXX: "longname", + Baz: "baz", + } +} + +// ---------------------------------------------------------------------------- +// Examples from issue #10392. + +var kcfg = KubeletConfig{ + Address: s.Address, + AllowPrivileged: s.AllowPrivileged, + HostNetworkSources: hostNetworkSources, + HostnameOverride: s.HostnameOverride, + RootDirectory: s.RootDirectory, + ConfigFile: s.Config, + ManifestURL: s.ManifestURL, + FileCheckFrequency: s.FileCheckFrequency, + HTTPCheckFrequency: s.HTTPCheckFrequency, + PodInfraContainerImage: s.PodInfraContainerImage, + SyncFrequency: s.SyncFrequency, + RegistryPullQPS: s.RegistryPullQPS, + RegistryBurst: s.RegistryBurst, + MinimumGCAge: s.MinimumGCAge, + MaxPerPodContainerCount: s.MaxPerPodContainerCount, + MaxContainerCount: s.MaxContainerCount, + ClusterDomain: s.ClusterDomain, + ClusterDNS: s.ClusterDNS, + Runonce: s.RunOnce, + Port: s.Port, + ReadOnlyPort: s.ReadOnlyPort, + CadvisorInterface: cadvisorInterface, + EnableServer: s.EnableServer, + EnableDebuggingHandlers: s.EnableDebuggingHandlers, + DockerClient: dockertools.ConnectToDockerOrDie(s.DockerEndpoint), + KubeClient: client, + MasterServiceNamespace: s.MasterServiceNamespace, + VolumePlugins: ProbeVolumePlugins(), + NetworkPlugins: ProbeNetworkPlugins(), + NetworkPluginName: s.NetworkPluginName, + StreamingConnectionIdleTimeout: s.StreamingConnectionIdleTimeout, + TLSOptions: tlsOptions, + ImageGCPolicy: imageGCPolicy,imageGCPolicy, + Cloud: cloud, + NodeStatusUpdateFrequency: s.NodeStatusUpdateFrequency, +} + +var a = A{ + Long: 1, + LongLong: 1, + LongLongLong: 1, + LongLongLongLong: 1, + LongLongLongLongLong: 1, + LongLongLongLongLongLong: 1, + LongLongLongLongLongLongLong: 1, + LongLongLongLongLongLongLongLong: 1, + Short: 1, + LongLongLongLongLongLongLongLongLong: 3, +} + +// ---------------------------------------------------------------------------- +// Examples from issue #22852. + +var fmtMap = map[string]string{ + "1": "123", + "12": "123", + "123": "123", + "1234": "123", + "12345": "123", + "123456": "123", + "12345678901234567890123456789": "123", + "abcde": "123", + "123456789012345678901234567890": "123", + "1234567": "123", + "abcdefghijklmnopqrstuvwxyzabcd": "123", + "abcd": "123", +} + +type Fmt struct { + abcdefghijklmnopqrstuvwx string + abcdefghijklmnopqrstuvwxy string + abcdefghijklmnopqrstuvwxyz string + abcdefghijklmnopqrstuvwxyza string + abcdefghijklmnopqrstuvwxyzab string + abcdefghijklmnopqrstuvwxyzabc string + abcde string + abcdefghijklmnopqrstuvwxyzabcde string + abcdefg string +} + +func main() { + _ := Fmt{ + abcdefghijklmnopqrstuvwx: "foo", + abcdefghijklmnopqrstuvwxyza: "foo", + abcdefghijklmnopqrstuvwxyzab: "foo", + abcdefghijklmnopqrstuvwxyzabc: "foo", + abcde: "foo", + abcdefghijklmnopqrstuvwxyzabcde: "foo", + abcdefg: "foo", + abcdefghijklmnopqrstuvwxy: "foo", + abcdefghijklmnopqrstuvwxyz: "foo", + } +} + +// ---------------------------------------------------------------------------- +// Examples from issue #26352. + +var _ = map[int]string{ + 1: "", + + 12345678901234567890123456789: "", + 12345678901234567890123456789012345678: "", +} + +func f() { + _ = map[int]string{ + 1: "", + + 12345678901234567: "", + 12345678901234567890123456789012345678901: "", + } +} + +// ---------------------------------------------------------------------------- +// Examples from issue #26930. + +var _ = S{ + F1: []string{ + }, + F2____: []string{}, +} + +var _ = S{ + F1: []string{ + + + }, + F2____: []string{}, +} + +var _ = S{ + F1____: []string{ + }, + F2: []string{}, +} + +var _ = S{ + F1____: []string{ + + }, + F2: []string{}, +} diff --git a/libgo/go/go/printer/testdata/comments.golden b/libgo/go/go/printer/testdata/comments.golden index e1818e5fd53..1a21fff3314 100644 --- a/libgo/go/go/printer/testdata/comments.golden +++ b/libgo/go/go/printer/testdata/comments.golden @@ -702,8 +702,8 @@ func _() { //line foo:2 _ = 2 - // The following is not a legal line directive (negative line number): - //line foo:-3 + // The following is not a legal line directive (missing colon): +//line foo -3 _ = 3 } diff --git a/libgo/go/go/printer/testdata/comments.input b/libgo/go/go/printer/testdata/comments.input index f3eda12c229..aa428a2aa68 100644 --- a/libgo/go/go/printer/testdata/comments.input +++ b/libgo/go/go/printer/testdata/comments.input @@ -699,8 +699,8 @@ func _() { //line foo:2 _ = 2 -// The following is not a legal line directive (negative line number): -//line foo:-3 +// The following is not a legal line directive (missing colon): +//line foo -3 _ = 3 } diff --git a/libgo/go/go/printer/testdata/complit.input b/libgo/go/go/printer/testdata/complit.input new file mode 100644 index 00000000000..82806a4233d --- /dev/null +++ b/libgo/go/go/printer/testdata/complit.input @@ -0,0 +1,65 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package complit + +var ( + // Multi-line declarations + V1 = T{ + F1: "hello", + f2: 1, + } + V2 = T{ + f2: 1, + F1: "hello", + } + V3 = T{ + F1: "hello", + F2: T2{ + A: "world", + b: "hidden", + }, + f3: T2{ + A: "world", + }, + } + V4 = T{ + f2: 1, + } + + // Single-line declarations + V5 = T{F1: "hello", f2: 1} + V6 = T{f2: 1, F1: "hello"} + V7 = T{f2: 1} + + // Mixed-mode declarations + V8 = T{ + F1: "hello", f2: 1, + F3: "world", + f4: 2} + V9 = T{ + f2: 1, F1: "hello",} + V10 = T{ + F1: "hello", f2: 1, + f3: 2, + F4: "world", f5: 3, + } + + // Other miscellaneous declarations + V11 = T{ + t{ + A: "world", + b: "hidden", + }, + f2: t{ + A: "world", + b: "hidden", + }, + } + V12 = T{ + F1: make(chan int), + f2: []int{}, + F3: make(map[int]string), f4: 1, + } +)
\ No newline at end of file diff --git a/libgo/go/go/printer/testdata/complit.x b/libgo/go/go/printer/testdata/complit.x new file mode 100644 index 00000000000..458ac6117a7 --- /dev/null +++ b/libgo/go/go/printer/testdata/complit.x @@ -0,0 +1,62 @@ +package complit + +var ( + // Multi-line declarations + V1 = T{ + F1: "hello", + // contains filtered or unexported fields + } + V2 = T{ + + F1: "hello", + // contains filtered or unexported fields + } + V3 = T{ + F1: "hello", + F2: T2{ + A: "world", + // contains filtered or unexported fields + }, + // contains filtered or unexported fields + } + V4 = T{ + // contains filtered or unexported fields + } + + // Single-line declarations + V5 = T{F1: "hello", /* contains filtered or unexported fields */} + V6 = T{F1: "hello", /* contains filtered or unexported fields */} + V7 = T{/* contains filtered or unexported fields */} + + // Mixed-mode declarations + V8 = T{ + F1: "hello", + F3: "world", + // contains filtered or unexported fields + } + V9 = T{ + F1: "hello", + // contains filtered or unexported fields + } + V10 = T{ + F1: "hello", + + F4: "world", + // contains filtered or unexported fields + } + + // Other miscellaneous declarations + V11 = T{ + t{ + A: "world", + // contains filtered or unexported fields + }, + // contains filtered or unexported fields + } + V12 = T{ + F1: make(chan int), + + F3: make(map[int]string), + // contains filtered or unexported fields + } +) diff --git a/libgo/go/go/printer/testdata/declarations.golden b/libgo/go/go/printer/testdata/declarations.golden index bebc0eaa637..fe0f7838de5 100644 --- a/libgo/go/go/printer/testdata/declarations.golden +++ b/libgo/go/go/printer/testdata/declarations.golden @@ -688,8 +688,8 @@ var _ = T1{ // not aligned var _ = T2{ - a: x, - b: y, + a: x, + b: y, ccccccccccccccccccccc: z, } @@ -703,8 +703,8 @@ var _ = T3{ // not aligned var _ = T4{ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: x, - b: y, - c: z, + b: y, + c: z, } // no alignment of map composite entries if they are not the first entry on a line diff --git a/libgo/go/go/printer/testdata/linebreaks.golden b/libgo/go/go/printer/testdata/linebreaks.golden index 006cf171848..17d2b5cc50c 100644 --- a/libgo/go/go/printer/testdata/linebreaks.golden +++ b/libgo/go/go/printer/testdata/linebreaks.golden @@ -256,7 +256,7 @@ func _( ) { } -// Example from issue 2597. +// Example from issue #2597. func ManageStatus0( in <-chan *Status, req <-chan Request, @@ -272,4 +272,24 @@ func ManageStatus1( ) { } +// Example from issue #9064. +func (y *y) xerrors() error { + _ = "xerror.test" //TODO- + _ = []byte(` +foo bar foo bar foo bar +`) //TODO- +} + +func _() { + _ = "abc" // foo + _ = `abc_0123456789_` // foo +} + +func _() { + _ = "abc" // foo + _ = `abc +0123456789 +` // foo +} + // There should be exactly one linebreak after this comment. diff --git a/libgo/go/go/printer/testdata/linebreaks.input b/libgo/go/go/printer/testdata/linebreaks.input index e782bb04443..9e714f3eff4 100644 --- a/libgo/go/go/printer/testdata/linebreaks.input +++ b/libgo/go/go/printer/testdata/linebreaks.input @@ -252,7 +252,7 @@ func _( y T, ) {} -// Example from issue 2597. +// Example from issue #2597. func ManageStatus0( in <-chan *Status, req <-chan Request, @@ -267,5 +267,25 @@ func ManageStatus1( TargetHistorySize int, ) { } - + +// Example from issue #9064. +func (y *y) xerrors() error { + _ = "xerror.test" //TODO- + _ = []byte(` +foo bar foo bar foo bar +`) //TODO- +} + +func _() { + _ = "abc" // foo + _ = `abc_0123456789_` // foo +} + +func _() { + _ = "abc" // foo + _ = `abc +0123456789 +` // foo +} + // There should be exactly one linebreak after this comment. |