summaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/gofmt')
-rw-r--r--src/cmd/gofmt/doc.go2
-rw-r--r--src/cmd/gofmt/gofmt.go193
-rw-r--r--src/cmd/gofmt/gofmt_test.go121
-rw-r--r--src/cmd/gofmt/long_test.go4
-rw-r--r--src/cmd/gofmt/rewrite.go3
-rw-r--r--src/cmd/gofmt/simplify.go50
-rw-r--r--src/cmd/gofmt/testdata/composites.golden2
-rw-r--r--src/cmd/gofmt/testdata/composites.input2
-rw-r--r--src/cmd/gofmt/testdata/crlf.golden1
-rw-r--r--src/cmd/gofmt/testdata/crlf.input1
-rw-r--r--src/cmd/gofmt/testdata/emptydecl.golden14
-rw-r--r--src/cmd/gofmt/testdata/emptydecl.input16
-rw-r--r--src/cmd/gofmt/testdata/ranges.golden30
-rw-r--r--src/cmd/gofmt/testdata/ranges.input20
-rw-r--r--src/cmd/gofmt/testdata/rewrite1.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite1.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite2.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite2.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite3.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite3.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite4.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite4.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite5.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite5.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite6.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite6.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite7.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite7.input2
-rw-r--r--src/cmd/gofmt/testdata/rewrite8.golden2
-rw-r--r--src/cmd/gofmt/testdata/rewrite8.input2
-rw-r--r--src/cmd/gofmt/testdata/slices1.golden8
-rw-r--r--src/cmd/gofmt/testdata/slices1.input8
-rw-r--r--src/cmd/gofmt/testdata/slices2.golden2
-rw-r--r--src/cmd/gofmt/testdata/slices2.input2
-rw-r--r--src/cmd/gofmt/testdata/stdin1.golden2
-rw-r--r--src/cmd/gofmt/testdata/stdin1.golden.gofmt3
-rw-r--r--src/cmd/gofmt/testdata/stdin1.input2
-rw-r--r--src/cmd/gofmt/testdata/stdin1.input.gofmt3
-rw-r--r--src/cmd/gofmt/testdata/stdin2.golden2
-rw-r--r--src/cmd/gofmt/testdata/stdin2.golden.gofmt10
-rw-r--r--src/cmd/gofmt/testdata/stdin2.input2
-rw-r--r--src/cmd/gofmt/testdata/stdin2.input.gofmt11
-rw-r--r--src/cmd/gofmt/testdata/stdin3.golden1
-rw-r--r--src/cmd/gofmt/testdata/stdin3.golden.gofmt7
-rw-r--r--src/cmd/gofmt/testdata/stdin3.input1
-rw-r--r--src/cmd/gofmt/testdata/stdin3.input.gofmt7
-rw-r--r--src/cmd/gofmt/testdata/stdin4.golden2
-rw-r--r--src/cmd/gofmt/testdata/stdin4.golden.gofmt3
-rw-r--r--src/cmd/gofmt/testdata/stdin4.input2
-rw-r--r--src/cmd/gofmt/testdata/stdin4.input.gofmt3
-rw-r--r--src/cmd/gofmt/testdata/stdin5.golden3
-rw-r--r--src/cmd/gofmt/testdata/stdin5.input3
-rw-r--r--src/cmd/gofmt/testdata/stdin6.golden19
-rw-r--r--src/cmd/gofmt/testdata/stdin6.input21
-rw-r--r--src/cmd/gofmt/testdata/stdin7.golden19
-rw-r--r--src/cmd/gofmt/testdata/stdin7.input21
56 files changed, 482 insertions, 176 deletions
diff --git a/src/cmd/gofmt/doc.go b/src/cmd/gofmt/doc.go
index 8f73ef5b9..3fc043954 100644
--- a/src/cmd/gofmt/doc.go
+++ b/src/cmd/gofmt/doc.go
@@ -67,7 +67,7 @@ To remove the parentheses:
To convert the package tree from explicit slice upper bounds to implicit ones:
- gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src/pkg
+ gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src
The simplify command
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
index 576cae522..81da21ff1 100644
--- a/src/cmd/gofmt/gofmt.go
+++ b/src/cmd/gofmt/gofmt.go
@@ -87,13 +87,13 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
return err
}
- file, adjust, err := parse(fileSet, filename, src, stdin)
+ file, sourceAdj, indentAdj, err := parse(fileSet, filename, src, stdin)
if err != nil {
return err
}
if rewrite != nil {
- if adjust == nil {
+ if sourceAdj == nil {
file = rewrite(file)
} else {
fmt.Fprintf(os.Stderr, "warning: rewrite ignored for incomplete programs\n")
@@ -106,15 +106,10 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
simplify(file)
}
- var buf bytes.Buffer
- err = (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(&buf, fileSet, file)
+ res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
if err != nil {
return err
}
- res := buf.Bytes()
- if adjust != nil {
- res = adjust(src, res)
- }
if !bytes.Equal(src, res) {
// formatting has changed
@@ -122,7 +117,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
fmt.Fprintln(out, filename)
}
if *write {
- err = ioutil.WriteFile(filename, res, 0)
+ err = ioutil.WriteFile(filename, res, 0644)
if err != nil {
return err
}
@@ -186,6 +181,11 @@ func gofmtMain() {
initRewrite()
if flag.NArg() == 0 {
+ if *write {
+ fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
+ exitCode = 2
+ return
+ }
if err := processFile("<standard input>", os.Stdin, os.Stdout, true); err != nil {
report(err)
}
@@ -235,19 +235,29 @@ func diff(b1, b2 []byte) (data []byte, err error) {
}
-// parse parses src, which was read from filename,
-// as a Go source file or statement list.
-func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.File, func(orig, src []byte) []byte, error) {
+// ----------------------------------------------------------------------------
+// Support functions
+//
+// The functions parse, format, and isSpace below are identical to the
+// respective functions in src/go/format/format.go - keep them in sync!
+//
+// TODO(gri) Factor out this functionality, eventually.
+
+// parse parses src, which was read from the named file,
+// as a Go source file, declaration, or statement list.
+func parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) (
+ file *ast.File,
+ sourceAdj func(src []byte, indent int) []byte,
+ indentAdj int,
+ err error,
+) {
// Try as whole source file.
- file, err := parser.ParseFile(fset, filename, src, parserMode)
- if err == nil {
- return file, nil, nil
- }
- // If the error is that the source file didn't begin with a
- // package line and this is standard input, fall through to
+ file, err = parser.ParseFile(fset, filename, src, parserMode)
+ // If there's no error, return. If the error is that the source file didn't begin with a
+ // package line and source fragments are ok, fall through to
// try as a source fragment. Stop and return on any other error.
- if !stdin || !strings.Contains(err.Error(), "expected 'package'") {
- return nil, nil, err
+ if err == nil || !fragmentOk || !strings.Contains(err.Error(), "expected 'package'") {
+ return
}
// If this is a declaration list, make it a source file
@@ -257,19 +267,19 @@ func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.F
psrc := append([]byte("package p;"), src...)
file, err = parser.ParseFile(fset, filename, psrc, parserMode)
if err == nil {
- adjust := func(orig, src []byte) []byte {
+ sourceAdj = func(src []byte, indent int) []byte {
// Remove the package clause.
// Gofmt has turned the ; into a \n.
- src = src[len("package p\n"):]
- return matchSpace(orig, src)
+ src = src[indent+len("package p\n"):]
+ return bytes.TrimSpace(src)
}
- return file, adjust, nil
+ return
}
// If the error is that the source file didn't begin with a
// declaration, fall through to try as a statement list.
// Stop and return on any other error.
if !strings.Contains(err.Error(), "expected declaration") {
- return nil, nil, err
+ return
}
// If this is a statement list, make it a source file
@@ -277,68 +287,101 @@ func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.F
// into a function body. This handles expressions too.
// Insert using a ;, not a newline, so that the line numbers
// in fsrc match the ones in src.
- fsrc := append(append([]byte("package p; func _() {"), src...), '}')
+ fsrc := append(append([]byte("package p; func _() {"), src...), '\n', '}')
file, err = parser.ParseFile(fset, filename, fsrc, parserMode)
if err == nil {
- adjust := func(orig, src []byte) []byte {
+ sourceAdj = func(src []byte, indent int) []byte {
+ // Cap adjusted indent to zero.
+ if indent < 0 {
+ indent = 0
+ }
// Remove the wrapping.
// Gofmt has turned the ; into a \n\n.
- src = src[len("package p\n\nfunc _() {"):]
- src = src[:len(src)-len("}\n")]
- // Gofmt has also indented the function body one level.
- // Remove that indent.
- src = bytes.Replace(src, []byte("\n\t"), []byte("\n"), -1)
- return matchSpace(orig, src)
+ // There will be two non-blank lines with indent, hence 2*indent.
+ src = src[2*indent+len("package p\n\nfunc _() {"):]
+ src = src[:len(src)-(indent+len("\n}\n"))]
+ return bytes.TrimSpace(src)
}
- return file, adjust, nil
+ // Gofmt has also indented the function body one level.
+ // Adjust that with indentAdj.
+ indentAdj = -1
}
- // Failed, and out of options.
- return nil, nil, err
+ // Succeeded, or out of options.
+ return
}
-func cutSpace(b []byte) (before, middle, after []byte) {
- i := 0
- for i < len(b) && (b[i] == ' ' || b[i] == '\t' || b[i] == '\n') {
- i++
- }
- j := len(b)
- for j > 0 && (b[j-1] == ' ' || b[j-1] == '\t' || b[j-1] == '\n') {
- j--
- }
- if i <= j {
- return b[:i], b[i:j], b[j:]
+// format formats the given package file originally obtained from src
+// and adjusts the result based on the original source via sourceAdj
+// and indentAdj.
+func format(
+ fset *token.FileSet,
+ file *ast.File,
+ sourceAdj func(src []byte, indent int) []byte,
+ indentAdj int,
+ src []byte,
+ cfg printer.Config,
+) ([]byte, error) {
+ if sourceAdj == nil {
+ // Complete source file.
+ var buf bytes.Buffer
+ err := cfg.Fprint(&buf, fset, file)
+ if err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
}
- return nil, nil, b[j:]
-}
-// matchSpace reformats src to use the same space context as orig.
-// 1) If orig begins with blank lines, matchSpace inserts them at the beginning of src.
-// 2) matchSpace copies the indentation of the first non-blank line in orig
-// to every non-blank line in src.
-// 3) matchSpace copies the trailing space from orig and uses it in place
-// of src's trailing space.
-func matchSpace(orig []byte, src []byte) []byte {
- before, _, after := cutSpace(orig)
- i := bytes.LastIndex(before, []byte{'\n'})
- before, indent := before[:i+1], before[i+1:]
-
- _, src, _ = cutSpace(src)
-
- var b bytes.Buffer
- b.Write(before)
- for len(src) > 0 {
- line := src
- if i := bytes.IndexByte(line, '\n'); i >= 0 {
- line, src = line[:i+1], line[i+1:]
- } else {
- src = nil
+ // Partial source file.
+ // Determine and prepend leading space.
+ i, j := 0, 0
+ for j < len(src) && isSpace(src[j]) {
+ if src[j] == '\n' {
+ i = j + 1 // byte offset of last line in leading space
}
- if len(line) > 0 && line[0] != '\n' { // not blank
- b.Write(indent)
+ j++
+ }
+ var res []byte
+ res = append(res, src[:i]...)
+
+ // Determine and prepend indentation of first code line.
+ // Spaces are ignored unless there are no tabs,
+ // in which case spaces count as one tab.
+ indent := 0
+ hasSpace := false
+ for _, b := range src[i:j] {
+ switch b {
+ case ' ':
+ hasSpace = true
+ case '\t':
+ indent++
}
- b.Write(line)
}
- b.Write(after)
- return b.Bytes()
+ if indent == 0 && hasSpace {
+ indent = 1
+ }
+ for i := 0; i < indent; i++ {
+ res = append(res, '\t')
+ }
+
+ // Format the source.
+ // Write it without any leading and trailing space.
+ cfg.Indent = indent + indentAdj
+ var buf bytes.Buffer
+ err := cfg.Fprint(&buf, fset, file)
+ if err != nil {
+ return nil, err
+ }
+ res = append(res, sourceAdj(buf.Bytes(), cfg.Indent)...)
+
+ // Determine and append trailing space.
+ i = len(src)
+ for i > 0 && isSpace(src[i-1]) {
+ i--
+ }
+ return append(res, src[i:]...), nil
+}
+
+func isSpace(b byte) bool {
+ return b == ' ' || b == '\t' || b == '\n' || b == '\r'
}
diff --git a/src/cmd/gofmt/gofmt_test.go b/src/cmd/gofmt/gofmt_test.go
index b9335b8f3..d1edb7bcc 100644
--- a/src/cmd/gofmt/gofmt_test.go
+++ b/src/cmd/gofmt/gofmt_test.go
@@ -6,18 +6,60 @@ package main
import (
"bytes"
+ "flag"
"io/ioutil"
+ "os"
"path/filepath"
"strings"
"testing"
+ "text/scanner"
)
-func runTest(t *testing.T, in, out, flags string) {
+var update = flag.Bool("update", false, "update .golden files")
+
+// gofmtFlags looks for a comment of the form
+//
+// //gofmt flags
+//
+// within the first maxLines lines of the given file,
+// and returns the flags string, if any. Otherwise it
+// returns the empty string.
+func gofmtFlags(filename string, maxLines int) string {
+ f, err := os.Open(filename)
+ if err != nil {
+ return "" // ignore errors - they will be found later
+ }
+ defer f.Close()
+
+ // initialize scanner
+ var s scanner.Scanner
+ s.Init(f)
+ s.Error = func(*scanner.Scanner, string) {} // ignore errors
+ s.Mode = scanner.GoTokens &^ scanner.SkipComments // want comments
+
+ // look for //gofmt comment
+ for s.Line <= maxLines {
+ switch s.Scan() {
+ case scanner.Comment:
+ const prefix = "//gofmt "
+ if t := s.TokenText(); strings.HasPrefix(t, prefix) {
+ return strings.TrimSpace(t[len(prefix):])
+ }
+ case scanner.EOF:
+ return ""
+ }
+
+ }
+
+ return ""
+}
+
+func runTest(t *testing.T, in, out string) {
// process flags
*simplifyAST = false
*rewriteRule = ""
stdin := false
- for _, flag := range strings.Split(flags, " ") {
+ for _, flag := range strings.Split(gofmtFlags(in, 20), " ") {
elts := strings.SplitN(flag, "=", 2)
name := elts[0]
value := ""
@@ -56,6 +98,17 @@ func runTest(t *testing.T, in, out, flags string) {
}
if got := buf.Bytes(); !bytes.Equal(got, expected) {
+ if *update {
+ if in != out {
+ if err := ioutil.WriteFile(out, got, 0666); err != nil {
+ t.Error(err)
+ }
+ return
+ }
+ // in == out: don't accidentally destroy input
+ t.Errorf("WARNING: -update did not rewrite input file %s", in)
+ }
+
t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in)
d, err := diff(expected, got)
if err == nil {
@@ -67,51 +120,37 @@ func runTest(t *testing.T, in, out, flags string) {
}
}
-var tests = []struct {
- in, flags string
-}{
- {"gofmt.go", ""},
- {"gofmt_test.go", ""},
- {"testdata/composites.input", "-s"},
- {"testdata/slices1.input", "-s"},
- {"testdata/slices2.input", "-s"},
- {"testdata/old.input", ""},
- {"testdata/rewrite1.input", "-r=Foo->Bar"},
- {"testdata/rewrite2.input", "-r=int->bool"},
- {"testdata/rewrite3.input", "-r=x->x"},
- {"testdata/rewrite4.input", "-r=(x)->x"},
- {"testdata/rewrite5.input", "-r=x+x->2*x"},
- {"testdata/rewrite6.input", "-r=fun(x)->Fun(x)"},
- {"testdata/rewrite7.input", "-r=fun(x...)->Fun(x)"},
- {"testdata/rewrite8.input", "-r=interface{}->int"},
- {"testdata/stdin*.input", "-stdin"},
- {"testdata/comments.input", ""},
- {"testdata/import.input", ""},
- {"testdata/crlf.input", ""}, // test case for issue 3961; see also TestCRLF
- {"testdata/typeswitch.input", ""}, // test case for issue 4470
-}
-
+// TestRewrite processes testdata/*.input files and compares them to the
+// corresponding testdata/*.golden files. The gofmt flags used to process
+// a file must be provided via a comment of the form
+//
+// //gofmt flags
+//
+// in the processed file within the first 20 lines, if any.
func TestRewrite(t *testing.T) {
- for _, test := range tests {
- match, err := filepath.Glob(test.in)
- if err != nil {
- t.Error(err)
- continue
+ // determine input files
+ match, err := filepath.Glob("testdata/*.input")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // add larger examples
+ match = append(match, "gofmt.go", "gofmt_test.go")
+
+ for _, in := range match {
+ out := in // for files where input and output are identical
+ if strings.HasSuffix(in, ".input") {
+ out = in[:len(in)-len(".input")] + ".golden"
}
- for _, in := range match {
- out := in
- if strings.HasSuffix(in, ".input") {
- out = in[:len(in)-len(".input")] + ".golden"
- }
- runTest(t, in, out, test.flags)
- if in != out {
- // Check idempotence.
- runTest(t, out, out, test.flags)
- }
+ runTest(t, in, out)
+ if in != out {
+ // Check idempotence.
+ runTest(t, out, out)
}
}
}
+// Test case for issue 3961.
func TestCRLF(t *testing.T) {
const input = "testdata/crlf.input" // must contain CR/LF's
const golden = "testdata/crlf.golden" // must not contain any CR's
diff --git a/src/cmd/gofmt/long_test.go b/src/cmd/gofmt/long_test.go
index 108278b33..237b86021 100644
--- a/src/cmd/gofmt/long_test.go
+++ b/src/cmd/gofmt/long_test.go
@@ -32,7 +32,7 @@ var (
)
func gofmt(fset *token.FileSet, filename string, src *bytes.Buffer) error {
- f, _, err := parse(fset, filename, src.Bytes(), false)
+ f, _, _, err := parse(fset, filename, src.Bytes(), false)
if err != nil {
return err
}
@@ -60,7 +60,7 @@ func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {
// exclude files w/ syntax errors (typically test cases)
fset := token.NewFileSet()
- if _, _, err = parse(fset, filename, b1.Bytes(), false); err != nil {
+ if _, _, _, err = parse(fset, filename, b1.Bytes(), false); err != nil {
if *verbose {
fmt.Fprintf(os.Stderr, "ignoring %s\n", err)
}
diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go
index fb6c6fc81..d267cfcc1 100644
--- a/src/cmd/gofmt/rewrite.go
+++ b/src/cmd/gofmt/rewrite.go
@@ -226,9 +226,6 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
return true
case reflect.Struct:
- if p.NumField() != v.NumField() {
- return false
- }
for i := 0; i < p.NumField(); i++ {
if !match(m, p.Field(i), v.Field(i)) {
return false
diff --git a/src/cmd/gofmt/simplify.go b/src/cmd/gofmt/simplify.go
index 45d000d67..69f7bf23c 100644
--- a/src/cmd/gofmt/simplify.go
+++ b/src/cmd/gofmt/simplify.go
@@ -68,9 +68,10 @@ func (s *simplifier) Visit(node ast.Node) ast.Visitor {
// a slice expression of the form: s[a:len(s)]
// can be simplified to: s[a:]
// if s is "simple enough" (for now we only accept identifiers)
- if s.hasDotImport {
- // if dot imports are present, we cannot be certain that an
- // unresolved "len" identifier refers to the predefined len()
+ if n.Max != nil || s.hasDotImport {
+ // - 3-index slices always require the 2nd and 3rd index
+ // - if dot imports are present, we cannot be certain that an
+ // unresolved "len" identifier refers to the predefined len()
break
}
if s, _ := n.X.(*ast.Ident); s != nil && s.Obj != nil {
@@ -96,16 +97,26 @@ func (s *simplifier) Visit(node ast.Node) ast.Visitor {
// x, y := b[:n], b[n:]
case *ast.RangeStmt:
- // a range of the form: for x, _ = range v {...}
+ // - a range of the form: for x, _ = range v {...}
// can be simplified to: for x = range v {...}
- if ident, _ := n.Value.(*ast.Ident); ident != nil && ident.Name == "_" {
+ // - a range of the form: for _ = range v {...}
+ // can be simplified to: for range v {...}
+ if isBlank(n.Value) {
n.Value = nil
}
+ if isBlank(n.Key) && n.Value == nil {
+ n.Key = nil
+ }
}
return s
}
+func isBlank(x ast.Expr) bool {
+ ident, ok := x.(*ast.Ident)
+ return ok && ident.Name == "_"
+}
+
func simplify(f *ast.File) {
var s simplifier
@@ -117,5 +128,34 @@ func simplify(f *ast.File) {
}
}
+ // remove empty declarations such as "const ()", etc
+ removeEmptyDeclGroups(f)
+
ast.Walk(&s, f)
}
+
+func removeEmptyDeclGroups(f *ast.File) {
+ i := 0
+ for _, d := range f.Decls {
+ if g, ok := d.(*ast.GenDecl); !ok || !isEmpty(f, g) {
+ f.Decls[i] = d
+ i++
+ }
+ }
+ f.Decls = f.Decls[:i]
+}
+
+func isEmpty(f *ast.File, g *ast.GenDecl) bool {
+ if g.Doc != nil || g.Specs != nil {
+ return false
+ }
+
+ for _, c := range f.Comments {
+ // if there is a comment in the declaration, it is not considered empty
+ if g.Pos() <= c.Pos() && c.End() <= g.End() {
+ return false
+ }
+ }
+
+ return true
+}
diff --git a/src/cmd/gofmt/testdata/composites.golden b/src/cmd/gofmt/testdata/composites.golden
index b2825e732..fc9c98e62 100644
--- a/src/cmd/gofmt/testdata/composites.golden
+++ b/src/cmd/gofmt/testdata/composites.golden
@@ -1,3 +1,5 @@
+//gofmt -s
+
package P
type T struct {
diff --git a/src/cmd/gofmt/testdata/composites.input b/src/cmd/gofmt/testdata/composites.input
index 7210dafc9..fc7598af9 100644
--- a/src/cmd/gofmt/testdata/composites.input
+++ b/src/cmd/gofmt/testdata/composites.input
@@ -1,3 +1,5 @@
+//gofmt -s
+
package P
type T struct {
diff --git a/src/cmd/gofmt/testdata/crlf.golden b/src/cmd/gofmt/testdata/crlf.golden
index 57679f770..193dbacc7 100644
--- a/src/cmd/gofmt/testdata/crlf.golden
+++ b/src/cmd/gofmt/testdata/crlf.golden
@@ -2,6 +2,7 @@
Source containing CR/LF line endings.
The gofmt'ed output must only have LF
line endings.
+ Test case for issue 3961.
*/
package main
diff --git a/src/cmd/gofmt/testdata/crlf.input b/src/cmd/gofmt/testdata/crlf.input
index 61a1aa0b4..ae7e14dbf 100644
--- a/src/cmd/gofmt/testdata/crlf.input
+++ b/src/cmd/gofmt/testdata/crlf.input
@@ -2,6 +2,7 @@
Source containing CR/LF line endings.
The gofmt'ed output must only have LF
line endings.
+ Test case for issue 3961.
*/
package main
diff --git a/src/cmd/gofmt/testdata/emptydecl.golden b/src/cmd/gofmt/testdata/emptydecl.golden
new file mode 100644
index 000000000..33d6435e0
--- /dev/null
+++ b/src/cmd/gofmt/testdata/emptydecl.golden
@@ -0,0 +1,14 @@
+//gofmt -s
+
+// Test case for issue 7631.
+
+package main
+
+// Keep this declaration
+var ()
+
+const (
+// Keep this declaration
+)
+
+func main() {}
diff --git a/src/cmd/gofmt/testdata/emptydecl.input b/src/cmd/gofmt/testdata/emptydecl.input
new file mode 100644
index 000000000..4948a61f0
--- /dev/null
+++ b/src/cmd/gofmt/testdata/emptydecl.input
@@ -0,0 +1,16 @@
+//gofmt -s
+
+// Test case for issue 7631.
+
+package main
+
+// Keep this declaration
+var ()
+
+const (
+// Keep this declaration
+)
+
+type ()
+
+func main() {} \ No newline at end of file
diff --git a/src/cmd/gofmt/testdata/ranges.golden b/src/cmd/gofmt/testdata/ranges.golden
new file mode 100644
index 000000000..506b3a035
--- /dev/null
+++ b/src/cmd/gofmt/testdata/ranges.golden
@@ -0,0 +1,30 @@
+//gofmt -s
+
+// Test cases for range simplification.
+package p
+
+func _() {
+ for a, b = range x {
+ }
+ for a = range x {
+ }
+ for _, b = range x {
+ }
+ for range x {
+ }
+
+ for a = range x {
+ }
+ for range x {
+ }
+
+ for a, b := range x {
+ }
+ for a := range x {
+ }
+ for _, b := range x {
+ }
+
+ for a := range x {
+ }
+}
diff --git a/src/cmd/gofmt/testdata/ranges.input b/src/cmd/gofmt/testdata/ranges.input
new file mode 100644
index 000000000..df5f8333c
--- /dev/null
+++ b/src/cmd/gofmt/testdata/ranges.input
@@ -0,0 +1,20 @@
+//gofmt -s
+
+// Test cases for range simplification.
+package p
+
+func _() {
+ for a, b = range x {}
+ for a, _ = range x {}
+ for _, b = range x {}
+ for _, _ = range x {}
+
+ for a = range x {}
+ for _ = range x {}
+
+ for a, b := range x {}
+ for a, _ := range x {}
+ for _, b := range x {}
+
+ for a := range x {}
+}
diff --git a/src/cmd/gofmt/testdata/rewrite1.golden b/src/cmd/gofmt/testdata/rewrite1.golden
index d9beb3705..3ee5373a7 100644
--- a/src/cmd/gofmt/testdata/rewrite1.golden
+++ b/src/cmd/gofmt/testdata/rewrite1.golden
@@ -1,3 +1,5 @@
+//gofmt -r=Foo->Bar
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite1.input b/src/cmd/gofmt/testdata/rewrite1.input
index bdb894320..a84c8f781 100644
--- a/src/cmd/gofmt/testdata/rewrite1.input
+++ b/src/cmd/gofmt/testdata/rewrite1.input
@@ -1,3 +1,5 @@
+//gofmt -r=Foo->Bar
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite2.golden b/src/cmd/gofmt/testdata/rewrite2.golden
index 64c67ffa6..f980e0353 100644
--- a/src/cmd/gofmt/testdata/rewrite2.golden
+++ b/src/cmd/gofmt/testdata/rewrite2.golden
@@ -1,3 +1,5 @@
+//gofmt -r=int->bool
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite2.input b/src/cmd/gofmt/testdata/rewrite2.input
index 21171447a..489be4e07 100644
--- a/src/cmd/gofmt/testdata/rewrite2.input
+++ b/src/cmd/gofmt/testdata/rewrite2.input
@@ -1,3 +1,5 @@
+//gofmt -r=int->bool
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite3.golden b/src/cmd/gofmt/testdata/rewrite3.golden
index 0d16d1601..261a220c6 100644
--- a/src/cmd/gofmt/testdata/rewrite3.golden
+++ b/src/cmd/gofmt/testdata/rewrite3.golden
@@ -1,3 +1,5 @@
+//gofmt -r=x->x
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite3.input b/src/cmd/gofmt/testdata/rewrite3.input
index 0d16d1601..261a220c6 100644
--- a/src/cmd/gofmt/testdata/rewrite3.input
+++ b/src/cmd/gofmt/testdata/rewrite3.input
@@ -1,3 +1,5 @@
+//gofmt -r=x->x
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite4.golden b/src/cmd/gofmt/testdata/rewrite4.golden
index 8dfc81a07..b05547b4b 100644
--- a/src/cmd/gofmt/testdata/rewrite4.golden
+++ b/src/cmd/gofmt/testdata/rewrite4.golden
@@ -1,3 +1,5 @@
+//gofmt -r=(x)->x
+
// Copyright 2012 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.
diff --git a/src/cmd/gofmt/testdata/rewrite4.input b/src/cmd/gofmt/testdata/rewrite4.input
index 164cc0451..081709920 100644
--- a/src/cmd/gofmt/testdata/rewrite4.input
+++ b/src/cmd/gofmt/testdata/rewrite4.input
@@ -1,3 +1,5 @@
+//gofmt -r=(x)->x
+
// Copyright 2012 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.
diff --git a/src/cmd/gofmt/testdata/rewrite5.golden b/src/cmd/gofmt/testdata/rewrite5.golden
index 5a448a63d..9beb34aee 100644
--- a/src/cmd/gofmt/testdata/rewrite5.golden
+++ b/src/cmd/gofmt/testdata/rewrite5.golden
@@ -1,3 +1,5 @@
+//gofmt -r=x+x->2*x
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite5.input b/src/cmd/gofmt/testdata/rewrite5.input
index 0d759e69b..d7a6122d0 100644
--- a/src/cmd/gofmt/testdata/rewrite5.input
+++ b/src/cmd/gofmt/testdata/rewrite5.input
@@ -1,3 +1,5 @@
+//gofmt -r=x+x->2*x
+
// Copyright 2011 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.
diff --git a/src/cmd/gofmt/testdata/rewrite6.golden b/src/cmd/gofmt/testdata/rewrite6.golden
index e565dbdd9..48ec9aa0d 100644
--- a/src/cmd/gofmt/testdata/rewrite6.golden
+++ b/src/cmd/gofmt/testdata/rewrite6.golden
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x)->Fun(x)
+
// Copyright 2013 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.
diff --git a/src/cmd/gofmt/testdata/rewrite6.input b/src/cmd/gofmt/testdata/rewrite6.input
index 8c088b3e8..b085a84fe 100644
--- a/src/cmd/gofmt/testdata/rewrite6.input
+++ b/src/cmd/gofmt/testdata/rewrite6.input
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x)->Fun(x)
+
// Copyright 2013 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.
diff --git a/src/cmd/gofmt/testdata/rewrite7.golden b/src/cmd/gofmt/testdata/rewrite7.golden
index 29babad9f..8386a0b2a 100644
--- a/src/cmd/gofmt/testdata/rewrite7.golden
+++ b/src/cmd/gofmt/testdata/rewrite7.golden
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x...)->Fun(x)
+
// Copyright 2013 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.
diff --git a/src/cmd/gofmt/testdata/rewrite7.input b/src/cmd/gofmt/testdata/rewrite7.input
index 073e2a3e6..c1984708e 100644
--- a/src/cmd/gofmt/testdata/rewrite7.input
+++ b/src/cmd/gofmt/testdata/rewrite7.input
@@ -1,3 +1,5 @@
+//gofmt -r=fun(x...)->Fun(x)
+
// Copyright 2013 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.
diff --git a/src/cmd/gofmt/testdata/rewrite8.golden b/src/cmd/gofmt/testdata/rewrite8.golden
index cfc452b03..62f0419df 100644
--- a/src/cmd/gofmt/testdata/rewrite8.golden
+++ b/src/cmd/gofmt/testdata/rewrite8.golden
@@ -1,3 +1,5 @@
+//gofmt -r=interface{}->int
+
// Copyright 2013 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.
diff --git a/src/cmd/gofmt/testdata/rewrite8.input b/src/cmd/gofmt/testdata/rewrite8.input
index 235efa91c..7964c5c75 100644
--- a/src/cmd/gofmt/testdata/rewrite8.input
+++ b/src/cmd/gofmt/testdata/rewrite8.input
@@ -1,3 +1,5 @@
+//gofmt -r=interface{}->int
+
// Copyright 2013 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.
diff --git a/src/cmd/gofmt/testdata/slices1.golden b/src/cmd/gofmt/testdata/slices1.golden
index 61e074f68..04bc16f21 100644
--- a/src/cmd/gofmt/testdata/slices1.golden
+++ b/src/cmd/gofmt/testdata/slices1.golden
@@ -1,3 +1,5 @@
+//gofmt -s
+
// Test cases for slice expression simplification.
package p
@@ -15,6 +17,7 @@ var (
_ = a[3:(len(a))]
_ = a[len(a) : len(a)-1]
_ = a[0:len(b)]
+ _ = a[2:len(a):len(a)]
_ = a[:]
_ = a[:10]
@@ -22,6 +25,7 @@ var (
_ = a[:(len(a))]
_ = a[:len(a)-1]
_ = a[:len(b)]
+ _ = a[:len(a):len(a)]
_ = s[0:]
_ = s[1:10]
@@ -29,6 +33,7 @@ var (
_ = s[3:(len(s))]
_ = s[len(a) : len(s)-1]
_ = s[0:len(b)]
+ _ = s[2:len(s):len(s)]
_ = s[:]
_ = s[:10]
@@ -36,6 +41,7 @@ var (
_ = s[:(len(s))]
_ = s[:len(s)-1]
_ = s[:len(b)]
+ _ = s[:len(s):len(s)]
_ = t.s[0:]
_ = t.s[1:10]
@@ -43,6 +49,7 @@ var (
_ = t.s[3:(len(t.s))]
_ = t.s[len(a) : len(t.s)-1]
_ = t.s[0:len(b)]
+ _ = t.s[2:len(t.s):len(t.s)]
_ = t.s[:]
_ = t.s[:10]
@@ -50,6 +57,7 @@ var (
_ = t.s[:(len(t.s))]
_ = t.s[:len(t.s)-1]
_ = t.s[:len(b)]
+ _ = t.s[:len(t.s):len(t.s)]
)
func _() {
diff --git a/src/cmd/gofmt/testdata/slices1.input b/src/cmd/gofmt/testdata/slices1.input
index 4d2cbfff4..1f25c43cc 100644
--- a/src/cmd/gofmt/testdata/slices1.input
+++ b/src/cmd/gofmt/testdata/slices1.input
@@ -1,3 +1,5 @@
+//gofmt -s
+
// Test cases for slice expression simplification.
package p
@@ -15,6 +17,7 @@ var (
_ = a[3:(len(a))]
_ = a[len(a) : len(a)-1]
_ = a[0:len(b)]
+ _ = a[2:len(a):len(a)]
_ = a[:]
_ = a[:10]
@@ -22,6 +25,7 @@ var (
_ = a[:(len(a))]
_ = a[:len(a)-1]
_ = a[:len(b)]
+ _ = a[:len(a):len(a)]
_ = s[0:]
_ = s[1:10]
@@ -29,6 +33,7 @@ var (
_ = s[3:(len(s))]
_ = s[len(a) : len(s)-1]
_ = s[0:len(b)]
+ _ = s[2:len(s):len(s)]
_ = s[:]
_ = s[:10]
@@ -36,6 +41,7 @@ var (
_ = s[:(len(s))]
_ = s[:len(s)-1]
_ = s[:len(b)]
+ _ = s[:len(s):len(s)]
_ = t.s[0:]
_ = t.s[1:10]
@@ -43,6 +49,7 @@ var (
_ = t.s[3:(len(t.s))]
_ = t.s[len(a) : len(t.s)-1]
_ = t.s[0:len(b)]
+ _ = t.s[2:len(t.s):len(t.s)]
_ = t.s[:]
_ = t.s[:10]
@@ -50,6 +57,7 @@ var (
_ = t.s[:(len(t.s))]
_ = t.s[:len(t.s)-1]
_ = t.s[:len(b)]
+ _ = t.s[:len(t.s):len(t.s)]
)
func _() {
diff --git a/src/cmd/gofmt/testdata/slices2.golden b/src/cmd/gofmt/testdata/slices2.golden
index 433788e1e..ab657004e 100644
--- a/src/cmd/gofmt/testdata/slices2.golden
+++ b/src/cmd/gofmt/testdata/slices2.golden
@@ -1,3 +1,5 @@
+//gofmt -s
+
// Test cases for slice expression simplification.
// Because of a dot import, these slices must remain untouched.
package p
diff --git a/src/cmd/gofmt/testdata/slices2.input b/src/cmd/gofmt/testdata/slices2.input
index 433788e1e..ab657004e 100644
--- a/src/cmd/gofmt/testdata/slices2.input
+++ b/src/cmd/gofmt/testdata/slices2.input
@@ -1,3 +1,5 @@
+//gofmt -s
+
// Test cases for slice expression simplification.
// Because of a dot import, these slices must remain untouched.
package p
diff --git a/src/cmd/gofmt/testdata/stdin1.golden b/src/cmd/gofmt/testdata/stdin1.golden
index ff8b0b7ab..9e4dcd20f 100644
--- a/src/cmd/gofmt/testdata/stdin1.golden
+++ b/src/cmd/gofmt/testdata/stdin1.golden
@@ -1,3 +1,5 @@
+ //gofmt -stdin
+
if x {
y
}
diff --git a/src/cmd/gofmt/testdata/stdin1.golden.gofmt b/src/cmd/gofmt/testdata/stdin1.golden.gofmt
deleted file mode 100644
index 1f888877d..000000000
--- a/src/cmd/gofmt/testdata/stdin1.golden.gofmt
+++ /dev/null
@@ -1,3 +0,0 @@
- if x {
- y
-}
diff --git a/src/cmd/gofmt/testdata/stdin1.input b/src/cmd/gofmt/testdata/stdin1.input
index ff8b0b7ab..9e4dcd20f 100644
--- a/src/cmd/gofmt/testdata/stdin1.input
+++ b/src/cmd/gofmt/testdata/stdin1.input
@@ -1,3 +1,5 @@
+ //gofmt -stdin
+
if x {
y
}
diff --git a/src/cmd/gofmt/testdata/stdin1.input.gofmt b/src/cmd/gofmt/testdata/stdin1.input.gofmt
deleted file mode 100644
index 1f888877d..000000000
--- a/src/cmd/gofmt/testdata/stdin1.input.gofmt
+++ /dev/null
@@ -1,3 +0,0 @@
- if x {
- y
-}
diff --git a/src/cmd/gofmt/testdata/stdin2.golden b/src/cmd/gofmt/testdata/stdin2.golden
index 7eb1b54fe..57df35540 100644
--- a/src/cmd/gofmt/testdata/stdin2.golden
+++ b/src/cmd/gofmt/testdata/stdin2.golden
@@ -1,4 +1,4 @@
-
+//gofmt -stdin
var x int
diff --git a/src/cmd/gofmt/testdata/stdin2.golden.gofmt b/src/cmd/gofmt/testdata/stdin2.golden.gofmt
deleted file mode 100644
index 85e800300..000000000
--- a/src/cmd/gofmt/testdata/stdin2.golden.gofmt
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-var x int
-
-func f() {
- y := z
-}
-
-
diff --git a/src/cmd/gofmt/testdata/stdin2.input b/src/cmd/gofmt/testdata/stdin2.input
index 99defd2d1..69d6bdd68 100644
--- a/src/cmd/gofmt/testdata/stdin2.input
+++ b/src/cmd/gofmt/testdata/stdin2.input
@@ -1,4 +1,4 @@
-
+//gofmt -stdin
var x int
diff --git a/src/cmd/gofmt/testdata/stdin2.input.gofmt b/src/cmd/gofmt/testdata/stdin2.input.gofmt
deleted file mode 100644
index 7eb1b54fe..000000000
--- a/src/cmd/gofmt/testdata/stdin2.input.gofmt
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-var x int
-
-func f() {
- y := z
- /* this is a comment */
- // this is a comment too
-}
-
-
diff --git a/src/cmd/gofmt/testdata/stdin3.golden b/src/cmd/gofmt/testdata/stdin3.golden
index 1bf2f5a48..d6da0e417 100644
--- a/src/cmd/gofmt/testdata/stdin3.golden
+++ b/src/cmd/gofmt/testdata/stdin3.golden
@@ -1,3 +1,4 @@
+ //gofmt -stdin
/* note: no newline at end of file */
for i := 0; i < 10; i++ {
diff --git a/src/cmd/gofmt/testdata/stdin3.golden.gofmt b/src/cmd/gofmt/testdata/stdin3.golden.gofmt
deleted file mode 100644
index b4d1d4663..000000000
--- a/src/cmd/gofmt/testdata/stdin3.golden.gofmt
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
- /* note: no newline at end of file */
- for i := 0; i < 10; i++ {
- s += i
- }
- \ No newline at end of file
diff --git a/src/cmd/gofmt/testdata/stdin3.input b/src/cmd/gofmt/testdata/stdin3.input
index d963bd0d2..ab46c1063 100644
--- a/src/cmd/gofmt/testdata/stdin3.input
+++ b/src/cmd/gofmt/testdata/stdin3.input
@@ -1,3 +1,4 @@
+ //gofmt -stdin
/* note: no newline at end of file */
for i := 0; i < 10; i++ { s += i }
diff --git a/src/cmd/gofmt/testdata/stdin3.input.gofmt b/src/cmd/gofmt/testdata/stdin3.input.gofmt
deleted file mode 100644
index b4d1d4663..000000000
--- a/src/cmd/gofmt/testdata/stdin3.input.gofmt
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
- /* note: no newline at end of file */
- for i := 0; i < 10; i++ {
- s += i
- }
- \ No newline at end of file
diff --git a/src/cmd/gofmt/testdata/stdin4.golden b/src/cmd/gofmt/testdata/stdin4.golden
index 5f7343551..0c7acace5 100644
--- a/src/cmd/gofmt/testdata/stdin4.golden
+++ b/src/cmd/gofmt/testdata/stdin4.golden
@@ -1,3 +1,5 @@
+ //gofmt -stdin
+
// comment
i := 0
diff --git a/src/cmd/gofmt/testdata/stdin4.golden.gofmt b/src/cmd/gofmt/testdata/stdin4.golden.gofmt
deleted file mode 100644
index 5f7343551..000000000
--- a/src/cmd/gofmt/testdata/stdin4.golden.gofmt
+++ /dev/null
@@ -1,3 +0,0 @@
- // comment
-
- i := 0
diff --git a/src/cmd/gofmt/testdata/stdin4.input b/src/cmd/gofmt/testdata/stdin4.input
index f02a54fb1..1fc73f31e 100644
--- a/src/cmd/gofmt/testdata/stdin4.input
+++ b/src/cmd/gofmt/testdata/stdin4.input
@@ -1,3 +1,5 @@
+ //gofmt -stdin
+
// comment
i := 0
diff --git a/src/cmd/gofmt/testdata/stdin4.input.gofmt b/src/cmd/gofmt/testdata/stdin4.input.gofmt
deleted file mode 100644
index 5f7343551..000000000
--- a/src/cmd/gofmt/testdata/stdin4.input.gofmt
+++ /dev/null
@@ -1,3 +0,0 @@
- // comment
-
- i := 0
diff --git a/src/cmd/gofmt/testdata/stdin5.golden b/src/cmd/gofmt/testdata/stdin5.golden
new file mode 100644
index 000000000..31ce6b248
--- /dev/null
+++ b/src/cmd/gofmt/testdata/stdin5.golden
@@ -0,0 +1,3 @@
+//gofmt -stdin
+
+i := 5 // Line comment without newline. \ No newline at end of file
diff --git a/src/cmd/gofmt/testdata/stdin5.input b/src/cmd/gofmt/testdata/stdin5.input
new file mode 100644
index 000000000..0a7c97d18
--- /dev/null
+++ b/src/cmd/gofmt/testdata/stdin5.input
@@ -0,0 +1,3 @@
+//gofmt -stdin
+
+i :=5// Line comment without newline. \ No newline at end of file
diff --git a/src/cmd/gofmt/testdata/stdin6.golden b/src/cmd/gofmt/testdata/stdin6.golden
new file mode 100644
index 000000000..ffcea8011
--- /dev/null
+++ b/src/cmd/gofmt/testdata/stdin6.golden
@@ -0,0 +1,19 @@
+ //gofmt -stdin
+
+ if err != nil {
+ source := strings.NewReader(`line 1.
+line 2.
+`)
+ return source
+ }
+
+ f := func(hat, tail string) {
+
+ fmt.Println(hat+`
+foo
+
+
+`+tail,
+ "more",
+ "and more")
+ }
diff --git a/src/cmd/gofmt/testdata/stdin6.input b/src/cmd/gofmt/testdata/stdin6.input
new file mode 100644
index 000000000..78330020c
--- /dev/null
+++ b/src/cmd/gofmt/testdata/stdin6.input
@@ -0,0 +1,21 @@
+ //gofmt -stdin
+
+ if err != nil {
+ source := strings.NewReader(`line 1.
+line 2.
+`)
+ return source
+ }
+
+ f:=func( hat, tail string){
+
+
+
+ fmt. Println ( hat+ `
+foo
+
+
+`+ tail ,
+ "more" ,
+ "and more" )
+ }
diff --git a/src/cmd/gofmt/testdata/stdin7.golden b/src/cmd/gofmt/testdata/stdin7.golden
new file mode 100644
index 000000000..bbac7133c
--- /dev/null
+++ b/src/cmd/gofmt/testdata/stdin7.golden
@@ -0,0 +1,19 @@
+ //gofmt -stdin
+
+ if err != nil {
+ source := strings.NewReader(`line 1.
+line 2.
+`)
+ return source
+ }
+
+ f := func(hat, tail string) {
+
+ fmt.Println(hat+`
+ foo
+
+
+ `+tail,
+ "more",
+ "and more")
+ }
diff --git a/src/cmd/gofmt/testdata/stdin7.input b/src/cmd/gofmt/testdata/stdin7.input
new file mode 100644
index 000000000..fd772a3c4
--- /dev/null
+++ b/src/cmd/gofmt/testdata/stdin7.input
@@ -0,0 +1,21 @@
+ //gofmt -stdin
+
+ if err != nil {
+ source := strings.NewReader(`line 1.
+line 2.
+`)
+ return source
+ }
+
+ f:=func( hat, tail string){
+
+
+
+ fmt. Println ( hat+ `
+ foo
+
+
+ `+ tail ,
+ "more" ,
+ "and more" )
+ }