From 5b1885c241172fc1f89221b1a83789032c13cf5a Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 14 Feb 2013 13:36:40 -0800 Subject: [PATCH] go/parser: cleanups following CL 7307085 - use the new AllErrors flag where appropriate - unless AllErrors is set, eliminate spurious errors before they are added to the errors list (it turns out that reporting spurious errors always leads to too many uninformative errors after all) R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/7323065 --- src/cmd/gofmt/gofmt.go | 4 ++-- src/pkg/exp/gotype/gotype.go | 4 ++-- src/pkg/go/parser/error_test.go | 2 +- src/pkg/go/parser/interface.go | 6 +++--- src/pkg/go/parser/parser.go | 18 +++++++++++++++--- src/pkg/go/types/check_test.go | 2 +- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index 0bc385b5b5..861ff9390f 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -29,7 +29,7 @@ var ( rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')") simplifyAST = flag.Bool("s", false, "simplify code") doDiff = flag.Bool("d", false, "display diffs instead of rewriting files") - allErrors = flag.Bool("e", false, "print all (including spurious) errors") + allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)") // layout control comments = flag.Bool("comments", true, "print comments") @@ -65,7 +65,7 @@ func initParserMode() { parserMode |= parser.ParseComments } if *allErrors { - parserMode |= parser.SpuriousErrors + parserMode |= parser.AllErrors } } diff --git a/src/pkg/exp/gotype/gotype.go b/src/pkg/exp/gotype/gotype.go index bb3237c37c..a9042ee05b 100644 --- a/src/pkg/exp/gotype/gotype.go +++ b/src/pkg/exp/gotype/gotype.go @@ -23,7 +23,7 @@ var ( pkgName = flag.String("p", "", "process only those files in package pkgName") recursive = flag.Bool("r", false, "recursively process subdirectories") verbose = flag.Bool("v", false, "verbose mode") - allErrors = flag.Bool("e", false, "print all (including spurious) errors") + allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)") // debugging support parseComments = flag.Bool("comments", false, "parse comments (ignored if -ast not set)") @@ -71,7 +71,7 @@ func parse(fset *token.FileSet, filename string, src []byte) *ast.File { // parse entire file mode := parser.DeclarationErrors if *allErrors { - mode |= parser.SpuriousErrors + mode |= parser.AllErrors } if *parseComments && *printAST { mode |= parser.ParseComments diff --git a/src/pkg/go/parser/error_test.go b/src/pkg/go/parser/error_test.go index b5d9a39cf3..b59fda11a3 100644 --- a/src/pkg/go/parser/error_test.go +++ b/src/pkg/go/parser/error_test.go @@ -139,7 +139,7 @@ func checkErrors(t *testing.T, filename string, input interface{}) { return } - _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors) + _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors|AllErrors) found, ok := err.(scanner.ErrorList) if err != nil && !ok { t.Error(err) diff --git a/src/pkg/go/parser/interface.go b/src/pkg/go/parser/interface.go index a732e75fd1..39affdd6b9 100644 --- a/src/pkg/go/parser/interface.go +++ b/src/pkg/go/parser/interface.go @@ -52,13 +52,13 @@ func readSource(filename string, src interface{}) ([]byte, error) { type Mode uint const ( - PackageClauseOnly Mode = 1 << iota // parsing stops after package clause - ImportsOnly // parsing stops after import declarations + PackageClauseOnly Mode = 1 << iota // stop parsing after package clause + ImportsOnly // stop parsing after import declarations ParseComments // parse comments and add them to AST Trace // print a trace of parsed productions DeclarationErrors // report declaration errors SpuriousErrors // same as AllErrors, for backward-compatibility - AllErrors = SpuriousErrors // report all (not just the first 10) errors per file + AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines) ) // ParseFile parses the source code of a single Go source file and returns diff --git a/src/pkg/go/parser/parser.go b/src/pkg/go/parser/parser.go index e816ca3262..40fa10d772 100644 --- a/src/pkg/go/parser/parser.go +++ b/src/pkg/go/parser/parser.go @@ -344,10 +344,22 @@ func (p *parser) next() { type bailout struct{} func (p *parser) error(pos token.Pos, msg string) { - if p.mode&SpuriousErrors == 0 && p.errors.Len() >= 10 { - panic(bailout{}) + epos := p.file.Position(pos) + + // If AllErrors is not set, discard errors reported on the same line + // as the last recorded error and stop parsing if there are more than + // 10 errors. + if p.mode&AllErrors == 0 { + n := len(p.errors) + if n > 0 && p.errors[n-1].Pos.Line == epos.Line { + return // discard - likely a spurious error + } + if n > 10 { + panic(bailout{}) + } } - p.errors.Add(p.file.Position(pos), msg) + + p.errors.Add(epos, msg) } func (p *parser) errorExpected(pos token.Pos, msg string) { diff --git a/src/pkg/go/types/check_test.go b/src/pkg/go/types/check_test.go index fe0e279827..8e6a93bb4b 100644 --- a/src/pkg/go/types/check_test.go +++ b/src/pkg/go/types/check_test.go @@ -92,7 +92,7 @@ func parseFiles(t *testing.T, testname string, filenames []string) ([]*ast.File, var files []*ast.File var errlist []error for _, filename := range filenames { - file, err := parser.ParseFile(fset, filename, nil, parser.DeclarationErrors) + file, err := parser.ParseFile(fset, filename, nil, parser.DeclarationErrors|parser.AllErrors) if file == nil { t.Fatalf("%s: could not parse file %s", testname, filename) } -- 2.48.1