Debian Codegraph @

interp/trace.go

raw symbols SCIP
1 package interp
2
3 import (
4 "fmt"
5 "reflect"
6 "strings"
7 )
8
9 // Set trace to true for debugging the cfg and other processes.
10 var trace = false
11
12 func traceIndent(n *node) string {
13 return strings.Repeat(" ", n.depth())
14 }
15
16 // tracePrintln works like fmt.Println, with indenting by depth
17 // and key info on given node.
18 func tracePrintln(n *node, v ...any) {
19 if !trace {
20 return
21 }
22 fmt.Println(append([]any{traceIndent(n), n}, v...)...)
23 }
24
25 // tracePrintTree is particularly useful in post-order for seeing the full
26 // structure of a given code segment of interest.
27 //
28 //nolint:unused // debugging facility
29 func tracePrintTree(n *node, v ...any) {
30 if !trace {
31 return
32 }
33 tracePrintln(n, v...)
34 n.Walk(func(n *node) bool {
35 tracePrintln(n)
36 return true
37 }, nil)
38 }
39
40 // nodeAddr returns the pointer address of node, short version.
41 func ptrAddr(v any) string {
42 p := fmt.Sprintf("%p", v)
43 return p[:2] + p[9:] // unique bits
44 }
45
46 // valString returns string rep of given value, showing underlying pointers etc.
47 //
48 //nolint:unused // debugging facility
49 func valString(v reflect.Value) string {
50 s := v.String()
51 if v.Kind() == reflect.Func || v.Kind() == reflect.Map || v.Kind() == reflect.Pointer || v.Kind() == reflect.Slice || v.Kind() == reflect.UnsafePointer {
52 p := fmt.Sprintf("%#x", v.Pointer())
53 ln := len(p)
54 s += " " + p[:2] + p[max(2, ln-4):]
55 }
56 return s
57 }
58
59 func (n *node) String() string {
60 s := n.kind.String()
61 if n.ident != "" {
62 s += " " + n.ident
63 }
64 s += " " + ptrAddr(n)
65 if n.sym != nil {
66 s += " sym:" + n.sym.String()
67 } else if n.typ != nil {
68 s += " typ:" + n.typ.String()
69 }
70 if n.findex >= 0 {
71 s += fmt.Sprintf(" fidx: %d lev: %d", n.findex, n.level)
72 }
73 if n.start != nil && n.start != n {
74 s += fmt.Sprintf(" ->start: %s %s", n.start.kind.String(), ptrAddr(n.start))
75 }
76 if n.tnext != nil {
77 s += fmt.Sprintf(" ->tnext: %s %s", n.tnext.kind.String(), ptrAddr(n.tnext))
78 }
79 if n.fnext != nil {
80 s += fmt.Sprintf(" ->fnext: %s %s", n.fnext.kind.String(), ptrAddr(n.fnext))
81 }
82 return s
83 }
84
85 func (n *node) depth() int {
86 if n.anc != nil {
87 return n.anc.depth() + 1
88 }
89 return 0
90 }
91
92 func (sy *symbol) String() string {
93 s := sy.kind.String()
94 if sy.typ != nil {
95 s += " (" + sy.typ.String() + ")"
96 }
97 if sy.rval.IsValid() {
98 s += " = " + sy.rval.String()
99 }
100 if sy.index >= 0 {
101 s += fmt.Sprintf(" idx: %d", sy.index)
102 }
103 if sy.node != nil {
104 s += " " + sy.node.String()
105 }
106 return s
107 }
108
109 func (t *itype) String() string {
110 if t.str != "" {
111 return t.str
112 }
113 s := t.cat.String()
114 if t.name != "" {
115 s += " (" + t.name + ")"
116 }
117 return s
118 }