]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: cleanups following CL 7307085
authorRobert Griesemer <gri@golang.org>
Thu, 14 Feb 2013 21:36:40 +0000 (13:36 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 14 Feb 2013 21:36:40 +0000 (13:36 -0800)
- 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
src/pkg/exp/gotype/gotype.go
src/pkg/go/parser/error_test.go
src/pkg/go/parser/interface.go
src/pkg/go/parser/parser.go
src/pkg/go/types/check_test.go

index 0bc385b5b56072a6c919df20f314f0b285e20f7d..861ff9390f3512f5be1fccaac16b8d8e219c66d1 100644 (file)
@@ -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
        }
 }
 
index bb3237c37cb63252a0d1a4704cb9c74a932df52d..a9042ee05be51046f55e94b4a4e16e821f937b1d 100644 (file)
@@ -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
index b5d9a39cf3b9224df24ef8e1b97477bc79830c3c..b59fda11a3be545f0a519b857d018c0fadddb2d5 100644 (file)
@@ -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)
index a732e75fd1847e088f5237b8a2f23ffe385e54f4..39affdd6b988b07d1557eabf02e4a4a857b4d626 100644 (file)
@@ -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
index e816ca326272d54bb4eab749103a671e07ee1fc3..40fa10d772cba3701ba1da907a55acafff475e6e 100644 (file)
@@ -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) {
index fe0e2798270cbf1cf5a42befcab66b7de2a59925..8e6a93bb4bcb3c5fba62d3d9a3f85c9ed3e4748a 100644 (file)
@@ -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)
                }