- 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
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")
parserMode |= parser.ParseComments
}
if *allErrors {
- parserMode |= parser.SpuriousErrors
+ parserMode |= parser.AllErrors
}
}
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)")
// parse entire file
mode := parser.DeclarationErrors
if *allErrors {
- mode |= parser.SpuriousErrors
+ mode |= parser.AllErrors
}
if *parseComments && *printAST {
mode |= parser.ParseComments
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)
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
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) {
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)
}