blob: 20499bba5bb48947aeaed83f48ecf37cafa26bfa [file] [log] [blame]
package analysis
import (
"android.googlesource.com/platform/tools/gpu/api/ast"
"android.googlesource.com/platform/tools/gpu/api/resolver"
"android.googlesource.com/platform/tools/gpu/api/semantic"
)
// findUnreachables returns the list of blocks and statements that are found
// by traversing api which are not found in reached.
func findUnreachables(
api *semantic.API,
mappings *resolver.Mappings,
reached map[ast.Node]struct{}) []Unreachable {
// Output list of unreachable blocks and statements.
unreachables := []Unreachable{}
// Blocks and statements that have already been reported as unreachable.
reported := map[ast.Node]struct{}{}
// check looks to see if n has been marked as reached. If n has not been
// reached then it is added to the list of unreachables and check returns
// false.
check := func(n semantic.Node) bool {
for _, a := range mappings.SemanticToAST[n] {
if _, ok := reached[a]; !ok {
// Ensure this is reported only once.
if _, ok := reported[a]; !ok {
reported[a] = struct{}{}
at := mappings.CST(a)
unreachables = append(unreachables, Unreachable{At: at, Node: n})
}
return false
}
}
return true
}
// The recursive traversal function.
var traverse func(n semantic.Node)
traverse = func(n semantic.Node) {
switch n := n.(type) {
case *semantic.Function:
if n.GetAnnotation("ignore_unreachables") == nil {
semantic.Visit(n, traverse)
}
case *semantic.Block:
if check(n) {
for _, s := range n.Statements {
if !check(s) {
break
}
traverse(s)
}
}
case semantic.Type, *semantic.Callable:
// Not interested in these.
default:
semantic.Visit(n, traverse)
}
}
// Peform the traversal and find all unreachables.
traverse(api)
return unreachables
}