tools/trim: add tracer
Change-Id: I99af9bfdb5448578cc4fdf0aacee9305a00f4851
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8701
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/tools/trim/debug.go b/tools/trim/debug.go
new file mode 100644
index 0000000..6f67bf5
--- /dev/null
+++ b/tools/trim/debug.go
@@ -0,0 +1,50 @@
+// Copyright 2021 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package trim
+
+import (
+ "fmt"
+ "strings"
+
+ "cuelang.org/go/internal/core/adt"
+)
+
+func (t *trimmer) trace(v *adt.Vertex) (*trimmer, *adt.Vertex) {
+ if t.debug {
+ t.indent++
+ fmt.Fprintf(t.w, "%s%s {\n",
+ strings.Repeat("..", t.indent),
+ v.Label.SelectorString(t.ctx))
+ }
+ return t, v
+}
+
+func un(t *trimmer, v *adt.Vertex) {
+ if !t.debug {
+ return
+ }
+ fmt.Fprintf(t.w, "%s}\n",
+ strings.Repeat("..", t.indent))
+ t.indent--
+}
+
+func (t *trimmer) logf(format string, args ...interface{}) {
+ if !t.debug {
+ return
+ }
+ fmt.Fprint(t.w, strings.Repeat("..", t.indent+1))
+ fmt.Fprintf(t.w, format, args...)
+ fmt.Fprintln(t.w)
+}
diff --git a/tools/trim/trim.go b/tools/trim/trim.go
index 96a6a03..5c4100f 100644
--- a/tools/trim/trim.go
+++ b/tools/trim/trim.go
@@ -50,11 +50,15 @@
package trim
import (
+ "io"
+ "os"
+
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
+ "cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/core/subsume"
)
@@ -77,6 +81,8 @@
Config: *cfg,
ctx: adt.NewContext(r, v),
remove: map[ast.Node]bool{},
+ debug: Debug,
+ w: os.Stderr,
}
t.findSubordinates(v)
@@ -102,11 +108,20 @@
ctx *adt.OpContext
remove map[ast.Node]bool
+
+ debug bool
+ indent int
+ w io.Writer
}
+var Debug bool = false
+
func (t *trimmer) markRemove(c adt.Conjunct) {
if src := c.Expr().Source(); src != nil {
t.remove[src] = true
+ if t.debug {
+ t.logf("removing %s", debug.NodeString(t.ctx, c.Expr(), nil))
+ }
}
}
@@ -148,6 +163,8 @@
)
func (t *trimmer) findSubordinates(v *adt.Vertex) int {
+ defer un(t.trace(v))
+
// TODO(structure sharing): do not descend into vertices whose parent is not
// equal to the parent. This is not relevant at this time, but may be so in
// the future.
diff --git a/tools/trim/trim_test.go b/tools/trim/trim_test.go
index 167192a..734e905 100644
--- a/tools/trim/trim_test.go
+++ b/tools/trim/trim_test.go
@@ -333,6 +333,8 @@
files := instances[0].Files
+ Debug = true
+
err := Files(files, inst, &Config{
Trace: true,
})