aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler/check.go
diff options
context:
space:
mode:
authorDokyung Song <dokyungs@google.com>2018-09-06 15:49:23 -0700
committerDmitry Vyukov <dvyukov@google.com>2018-09-11 13:01:53 +0200
commit11c256cdcd601b67fcf5611fe8aed55b9ff00143 (patch)
tree63bff88ba1faed749a2fddad2b22e8d56d5b9981 /pkg/compiler/check.go
parente3d1d545d390055ca67987d27243e0d2b37feedd (diff)
sys/fuchsia: prune unused structs in syscall description generated by fidlgen
After generating syscall description for fidl files using fidlgen, prune all unused structs using the exact same mechanism used by the compiler's check for unused structs. This allows the FIDL compiler to support modular compilation; it does not need to have global knowledge of whether each struct is used or not.
Diffstat (limited to 'pkg/compiler/check.go')
-rw-r--r--pkg/compiler/check.go75
1 files changed, 46 insertions, 29 deletions
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go
index 7b4b63fa8..087075a12 100644
--- a/pkg/compiler/check.go
+++ b/pkg/compiler/check.go
@@ -11,6 +11,7 @@ import (
"github.com/google/syzkaller/pkg/ast"
"github.com/google/syzkaller/prog"
+ "github.com/google/syzkaller/sys/targets"
)
func (comp *compiler) typecheck() {
@@ -371,6 +372,48 @@ func (comp *compiler) checkLenTarget(t *ast.Type, name, target string, fields []
comp.error(t.Pos, "%v target %v does not exist", t.Ident, target)
}
+func CollectUnused(desc *ast.Description, target *targets.Target) []ast.Node {
+ comp := createCompiler(desc, target, nil)
+ comp.typecheck()
+ return comp.collectUnused()
+}
+
+func (comp *compiler) collectUnused() []ast.Node {
+ var unused []ast.Node
+
+ comp.used, _, _ = comp.collectUsed(false)
+ structs, flags, strflags := comp.collectUsed(true)
+ _, _, _ = structs, flags, strflags
+
+ for name, n := range comp.intFlags {
+ if !flags[name] {
+ unused = append(unused, n)
+ }
+ }
+ for name, n := range comp.strFlags {
+ if !strflags[name] && builtinStrFlags[name] == nil {
+ unused = append(unused, n)
+ }
+ }
+ for name, n := range comp.resources {
+ if !structs[name] {
+ unused = append(unused, n)
+ }
+ }
+ for name, n := range comp.structs {
+ if !structs[name] {
+ unused = append(unused, n)
+ }
+ }
+ for name, n := range comp.typedefs {
+ if !comp.usedTypedefs[name] {
+ unused = append(unused, n)
+ }
+ }
+
+ return unused
+}
+
func (comp *compiler) collectUsed(all bool) (structs, flags, strflags map[string]bool) {
structs = make(map[string]bool)
flags = make(map[string]bool)
@@ -432,35 +475,9 @@ func (comp *compiler) collectUsedType(structs, flags, strflags map[string]bool,
}
func (comp *compiler) checkUnused() {
- comp.used, _, _ = comp.collectUsed(false)
- structs, flags, strflags := comp.collectUsed(true)
- _, _, _ = structs, flags, strflags
-
- for name, n := range comp.intFlags {
- if !flags[name] {
- comp.error(n.Pos, "unused flags %v", name)
- }
- }
- for name, n := range comp.strFlags {
- if !strflags[name] && builtinStrFlags[name] == nil {
- comp.error(n.Pos, "unused string flags %v", name)
- }
- }
- for name, n := range comp.resources {
- if !structs[name] {
- comp.error(n.Pos, "unused resource %v", name)
- }
- }
- for name, n := range comp.structs {
- if !structs[name] {
- _, typ, _ := n.Info()
- comp.error(n.Pos, "unused %v %v", typ, name)
- }
- }
- for name, n := range comp.typedefs {
- if !comp.usedTypedefs[name] {
- comp.error(n.Pos, "unused type %v", name)
- }
+ for _, n := range comp.collectUnused() {
+ pos, typ, name := n.Info()
+ comp.error(pos, fmt.Sprintf("unused %v %v", typ, name))
}
}