1
0
mirror of https://github.com/golang/go synced 2024-11-25 03:47:57 -07:00

cmd/gofmt: fix race in long test

Fixes #3249.

R=rsc
CC=golang-dev
https://golang.org/cl/5792043
This commit is contained in:
Mikio Hara 2012-03-08 23:56:26 +09:00
parent a40065ac68
commit dfb1af4b97
2 changed files with 12 additions and 10 deletions

View File

@ -41,7 +41,7 @@ var (
) )
var ( var (
fset = token.NewFileSet() fileSet = token.NewFileSet() // per process FileSet
exitCode = 0 exitCode = 0
rewrite func(*ast.File) *ast.File rewrite func(*ast.File) *ast.File
parserMode parser.Mode parserMode parser.Mode
@ -98,7 +98,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
return err return err
} }
file, adjust, err := parse(filename, src, stdin) file, adjust, err := parse(fileSet, filename, src, stdin)
if err != nil { if err != nil {
return err return err
} }
@ -111,14 +111,14 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
} }
} }
ast.SortImports(fset, file) ast.SortImports(fileSet, file)
if *simplifyAST { if *simplifyAST {
simplify(file) simplify(file)
} }
var buf bytes.Buffer var buf bytes.Buffer
err = (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(&buf, fset, file) err = (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(&buf, fileSet, file)
if err != nil { if err != nil {
return err return err
} }
@ -254,7 +254,7 @@ func diff(b1, b2 []byte) (data []byte, err error) {
// parse parses src, which was read from filename, // parse parses src, which was read from filename,
// as a Go source file or statement list. // as a Go source file or statement list.
func parse(filename string, src []byte, stdin bool) (*ast.File, func(orig, src []byte) []byte, error) { func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.File, func(orig, src []byte) []byte, error) {
// Try as whole source file. // Try as whole source file.
file, err := parser.ParseFile(fset, filename, src, parserMode) file, err := parser.ParseFile(fset, filename, src, parserMode)
if err == nil { if err == nil {

View File

@ -14,6 +14,7 @@ import (
"fmt" "fmt"
"go/ast" "go/ast"
"go/printer" "go/printer"
"go/token"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -30,8 +31,8 @@ var (
nfiles int // number of files processed nfiles int // number of files processed
) )
func gofmt(filename string, src *bytes.Buffer) error { func gofmt(fset *token.FileSet, filename string, src *bytes.Buffer) error {
f, _, err := parse(filename, src.Bytes(), false) f, _, err := parse(fset, filename, src.Bytes(), false)
if err != nil { if err != nil {
return err return err
} }
@ -58,7 +59,8 @@ func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {
} }
// exclude files w/ syntax errors (typically test cases) // exclude files w/ syntax errors (typically test cases)
if _, _, err = parse(filename, b1.Bytes(), false); err != nil { fset := token.NewFileSet()
if _, _, err = parse(fset, filename, b1.Bytes(), false); err != nil {
if *verbose { if *verbose {
fmt.Fprintf(os.Stderr, "ignoring %s\n", err) fmt.Fprintf(os.Stderr, "ignoring %s\n", err)
} }
@ -66,7 +68,7 @@ func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {
} }
// gofmt file // gofmt file
if err = gofmt(filename, b1); err != nil { if err = gofmt(fset, filename, b1); err != nil {
t.Errorf("1st gofmt failed: %v", err) t.Errorf("1st gofmt failed: %v", err)
return return
} }
@ -76,7 +78,7 @@ func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {
b2.Write(b1.Bytes()) b2.Write(b1.Bytes())
// gofmt result again // gofmt result again
if err = gofmt(filename, b2); err != nil { if err = gofmt(fset, filename, b2); err != nil {
t.Errorf("2nd gofmt failed: %v", err) t.Errorf("2nd gofmt failed: %v", err)
return return
} }