diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2016-07-22 18:15:38 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2016-07-22 18:15:38 +0000 |
commit | 22b955cca564a9a3a5b8c9d9dd1e295b7943c128 (patch) | |
tree | abdbd898676e1f853fca2d7e031d105d7ebcf676 /libgo/go/cmd/gofmt | |
parent | 9d04a3af4c6491536badf6bde9707c907e4d196b (diff) | |
download | gcc-22b955cca564a9a3a5b8c9d9dd1e295b7943c128.tar.gz |
libgo: update to go1.7rc3
Reviewed-on: https://go-review.googlesource.com/25150
From-SVN: r238662
Diffstat (limited to 'libgo/go/cmd/gofmt')
-rw-r--r-- | libgo/go/cmd/gofmt/gofmt.go | 5 | ||||
-rw-r--r-- | libgo/go/cmd/gofmt/gofmt_test.go | 4 | ||||
-rw-r--r-- | libgo/go/cmd/gofmt/internal.go | 6 | ||||
-rw-r--r-- | libgo/go/cmd/gofmt/rewrite.go | 4 | ||||
-rw-r--r-- | libgo/go/cmd/gofmt/simplify.go | 28 | ||||
-rw-r--r-- | libgo/go/cmd/gofmt/testdata/slices2.golden | 63 | ||||
-rw-r--r-- | libgo/go/cmd/gofmt/testdata/slices2.input | 63 |
7 files changed, 20 insertions, 153 deletions
diff --git a/libgo/go/cmd/gofmt/gofmt.go b/libgo/go/cmd/gofmt/gofmt.go index cfebeffe4a6..f29b6cb83d9 100644 --- a/libgo/go/cmd/gofmt/gofmt.go +++ b/libgo/go/cmd/gofmt/gofmt.go @@ -55,7 +55,6 @@ func report(err error) { func usage() { fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [path ...]\n") flag.PrintDefaults() - os.Exit(2) } func initParserMode() { @@ -143,7 +142,9 @@ func visitFile(path string, f os.FileInfo, err error) error { if err == nil && isGoFile(f) { err = processFile(path, nil, os.Stdout, false) } - if err != nil { + // Don't complain if a file was deleted in the meantime (i.e. + // the directory changed concurrently while running gofmt). + if err != nil && !os.IsNotExist(err) { report(err) } return nil diff --git a/libgo/go/cmd/gofmt/gofmt_test.go b/libgo/go/cmd/gofmt/gofmt_test.go index d1edb7bcc16..dea012764b3 100644 --- a/libgo/go/cmd/gofmt/gofmt_test.go +++ b/libgo/go/cmd/gofmt/gofmt_test.go @@ -159,7 +159,7 @@ func TestCRLF(t *testing.T) { if err != nil { t.Error(err) } - if bytes.Index(data, []byte("\r\n")) < 0 { + if !bytes.Contains(data, []byte("\r\n")) { t.Errorf("%s contains no CR/LF's", input) } @@ -167,7 +167,7 @@ func TestCRLF(t *testing.T) { if err != nil { t.Error(err) } - if bytes.Index(data, []byte("\r")) >= 0 { + if bytes.Contains(data, []byte("\r")) { t.Errorf("%s contains CR's", golden) } } diff --git a/libgo/go/cmd/gofmt/internal.go b/libgo/go/cmd/gofmt/internal.go index f764b10ebb0..cbc6983b612 100644 --- a/libgo/go/cmd/gofmt/internal.go +++ b/libgo/go/cmd/gofmt/internal.go @@ -28,9 +28,9 @@ func parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) ( ) { // Try as whole source file. 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 + // 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. + // try as a source fragment. Stop and return on any other error. if err == nil || !fragmentOk || !strings.Contains(err.Error(), "expected 'package'") { return } @@ -59,7 +59,7 @@ func parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) ( // If this is a statement list, make it a source file // by inserting a package clause and turning the list - // into a function body. This handles expressions too. + // 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. Add an extra '\n' before the '}' // to make sure comments are flushed before the '}'. diff --git a/libgo/go/cmd/gofmt/rewrite.go b/libgo/go/cmd/gofmt/rewrite.go index 069f96622ca..550492bf29a 100644 --- a/libgo/go/cmd/gofmt/rewrite.go +++ b/libgo/go/cmd/gofmt/rewrite.go @@ -1,4 +1,4 @@ -// Copyright 2009 The Go Authors. All rights reserved. +// Copyright 2009 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. @@ -158,7 +158,7 @@ func isWildcard(s string) bool { // recording wildcard submatches in m. // If m == nil, match checks whether pattern == val. func match(m map[string]reflect.Value, pattern, val reflect.Value) bool { - // Wildcard matches any expression. If it appears multiple + // Wildcard matches any expression. If it appears multiple // times in the pattern, it must match the same expression // each time. if m != nil && pattern.IsValid() && pattern.Type() == identType { diff --git a/libgo/go/cmd/gofmt/simplify.go b/libgo/go/cmd/gofmt/simplify.go index 69f7bf23c0b..2ebf4cde0be 100644 --- a/libgo/go/cmd/gofmt/simplify.go +++ b/libgo/go/cmd/gofmt/simplify.go @@ -10,11 +10,9 @@ import ( "reflect" ) -type simplifier struct { - hasDotImport bool // package file contains: import . "some/import/path" -} +type simplifier struct{} -func (s *simplifier) Visit(node ast.Node) ast.Visitor { +func (s simplifier) Visit(node ast.Node) ast.Visitor { switch n := node.(type) { case *ast.CompositeLit: // array, slice, and map composite literals may be simplified @@ -68,10 +66,13 @@ 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 n.Max != nil || s.hasDotImport { + // + // Note: This may not be correct because len may have been redeclared in another + // file belonging to the same package. However, this is extremely unlikely + // and so far (April 2016, after years of supporting this rewrite feature) + // has never come up, so let's keep it working as is (see also #15153). + if n.Max != nil { // - 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 { @@ -118,20 +119,11 @@ func isBlank(x ast.Expr) bool { } func simplify(f *ast.File) { - var s simplifier - - // determine if f contains dot imports - for _, imp := range f.Imports { - if imp.Name != nil && imp.Name.Name == "." { - s.hasDotImport = true - break - } - } - // remove empty declarations such as "const ()", etc removeEmptyDeclGroups(f) - ast.Walk(&s, f) + var s simplifier + ast.Walk(s, f) } func removeEmptyDeclGroups(f *ast.File) { diff --git a/libgo/go/cmd/gofmt/testdata/slices2.golden b/libgo/go/cmd/gofmt/testdata/slices2.golden deleted file mode 100644 index ab657004e64..00000000000 --- a/libgo/go/cmd/gofmt/testdata/slices2.golden +++ /dev/null @@ -1,63 +0,0 @@ -//gofmt -s - -// Test cases for slice expression simplification. -// Because of a dot import, these slices must remain untouched. -package p - -import . "math" - -var ( - a [10]byte - b [20]float32 - s []int - t struct { - s []byte - } - - _ = a[0:] - _ = a[1:10] - _ = a[2:len(a)] - _ = a[3:(len(a))] - _ = a[len(a) : len(a)-1] - _ = a[0:len(b)] - - _ = a[:] - _ = a[:10] - _ = a[:len(a)] - _ = a[:(len(a))] - _ = a[:len(a)-1] - _ = a[:len(b)] - - _ = s[0:] - _ = s[1:10] - _ = s[2:len(s)] - _ = s[3:(len(s))] - _ = s[len(a) : len(s)-1] - _ = s[0:len(b)] - - _ = s[:] - _ = s[:10] - _ = s[:len(s)] - _ = s[:(len(s))] - _ = s[:len(s)-1] - _ = s[:len(b)] - - _ = t.s[0:] - _ = t.s[1:10] - _ = t.s[2:len(t.s)] - _ = t.s[3:(len(t.s))] - _ = t.s[len(a) : len(t.s)-1] - _ = t.s[0:len(b)] - - _ = t.s[:] - _ = t.s[:10] - _ = t.s[:len(t.s)] - _ = t.s[:(len(t.s))] - _ = t.s[:len(t.s)-1] - _ = t.s[:len(b)] -) - -func _() { - s := s[0:len(s)] - _ = s -} diff --git a/libgo/go/cmd/gofmt/testdata/slices2.input b/libgo/go/cmd/gofmt/testdata/slices2.input deleted file mode 100644 index ab657004e64..00000000000 --- a/libgo/go/cmd/gofmt/testdata/slices2.input +++ /dev/null @@ -1,63 +0,0 @@ -//gofmt -s - -// Test cases for slice expression simplification. -// Because of a dot import, these slices must remain untouched. -package p - -import . "math" - -var ( - a [10]byte - b [20]float32 - s []int - t struct { - s []byte - } - - _ = a[0:] - _ = a[1:10] - _ = a[2:len(a)] - _ = a[3:(len(a))] - _ = a[len(a) : len(a)-1] - _ = a[0:len(b)] - - _ = a[:] - _ = a[:10] - _ = a[:len(a)] - _ = a[:(len(a))] - _ = a[:len(a)-1] - _ = a[:len(b)] - - _ = s[0:] - _ = s[1:10] - _ = s[2:len(s)] - _ = s[3:(len(s))] - _ = s[len(a) : len(s)-1] - _ = s[0:len(b)] - - _ = s[:] - _ = s[:10] - _ = s[:len(s)] - _ = s[:(len(s))] - _ = s[:len(s)-1] - _ = s[:len(b)] - - _ = t.s[0:] - _ = t.s[1:10] - _ = t.s[2:len(t.s)] - _ = t.s[3:(len(t.s))] - _ = t.s[len(a) : len(t.s)-1] - _ = t.s[0:len(b)] - - _ = t.s[:] - _ = t.s[:10] - _ = t.s[:len(t.s)] - _ = t.s[:(len(t.s))] - _ = t.s[:len(t.s)-1] - _ = t.s[:len(b)] -) - -func _() { - s := s[0:len(s)] - _ = s -} |