Skip to content

Commit

Permalink
tools/trim: add tracer
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Feb 13, 2021
1 parent 500e431 commit 15934d9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tools/trim/debug.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions tools/trim/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -77,6 +81,8 @@ func Files(files []*ast.File, inst *cue.Instance, cfg *Config) error {
Config: *cfg,
ctx: adt.NewContext(r, v),
remove: map[ast.Node]bool{},
debug: Debug,
w: os.Stderr,
}

t.findSubordinates(v)
Expand All @@ -102,11 +108,20 @@ type trimmer struct {

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))
}
}
}

Expand Down Expand Up @@ -148,6 +163,8 @@ const (
)

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.
Expand Down
2 changes: 2 additions & 0 deletions tools/trim/trim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ func TestX(t *testing.T) {

files := instances[0].Files

Debug = true

err := Files(files, inst, &Config{
Trace: true,
})
Expand Down

0 comments on commit 15934d9

Please sign in to comment.