From 8149dec50d887b3322b76ac694f08fe28a3e3627 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 10 Oct 2018 14:27:22 -0400 Subject: [PATCH] go/analysis: validate: report duplicates among analyzers (roots) Duplicate root analyzers caused duplicate flag registration and other problems. Change-Id: Id0c2761529c57ed1f9a63b669e62401ebf035cc2 Reviewed-on: https://go-review.googlesource.com/c/141159 Reviewed-by: Michael Matloob --- go/analysis/validate.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/go/analysis/validate.go b/go/analysis/validate.go index dd9d25b6..6e6cf498 100644 --- a/go/analysis/validate.go +++ b/go/analysis/validate.go @@ -20,14 +20,20 @@ func Validate(analyzers []*Analyzer) error { factTypes := make(map[reflect.Type]*Analyzer) // Traverse the Requires graph, depth first. - color := make(map[*Analyzer]uint8) // 0=white 1=grey 2=black + const ( + white = iota + grey + black + finished + ) + color := make(map[*Analyzer]uint8) var visit func(a *Analyzer) error visit = func(a *Analyzer) error { if a == nil { return fmt.Errorf("nil *Analyzer") } - if color[a] == 0 { // white - color[a] = 1 // grey + if color[a] == white { + color[a] = grey // names if !validIdent(a.Name) { @@ -64,7 +70,7 @@ func Validate(analyzers []*Analyzer) error { return fmt.Errorf("%s.Requires[%d]: %v", a.Name, i, err) } } - color[a] = 2 // black + color[a] = black } return nil @@ -75,6 +81,16 @@ func Validate(analyzers []*Analyzer) error { } } + // Reject duplicates among analyzers. + // Precondition: color[a] == black. + // Postcondition: color[a] == finished. + for _, a := range analyzers { + if color[a] == finished { + return fmt.Errorf("duplicate analyzer: %s", a.Name) + } + color[a] = finished + } + return nil }