Skip to content

Commit

Permalink
Merge #216
Browse files Browse the repository at this point in the history
216: debug: detailed information about visited locations r=saem a=haxscramper

- Add more detailed information about visited locations in the cyclic
  ASTs - show node numbers, and display `<visisted Kind in ID>` (e.g.
  `<visited Sym in 0>`
- Ignore repeated visitations of the `Empty` node - they are reused, so it
  only adds confusion, since ast looks like it is insanely cyclic.
- Fix duplicated `.comment` annotations from the `CommentStmtNode` -
  `addFlags()` and `addComment()` was called twice.



Co-authored-by: haxscramper <haxscramper@gmail.com>
  • Loading branch information
bors[bot] and haxscramper authored Jan 29, 2022
2 parents 5a4f7d7 + 6d7a8b3 commit dce4c78
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
48 changes: 37 additions & 11 deletions compiler/utils/astrepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import
],
experimental/colortext,
std/[
sets,
tables,
strutils,
strformat,
]
Expand All @@ -28,6 +28,7 @@ type
trfPackedFields
trfSkipAuxError
trfSymDefined
trfIndexVisisted

trfShowSymFlags
trfShowSymLineInfo
Expand Down Expand Up @@ -63,7 +64,8 @@ const treeReprAllFields* = { trfShowSymFlags .. trfShowNodeTypes }
const defaultTreeReprFlags* = {
trfPositionIndexed,
trfReportInfo,
trfSkipAuxError
trfSkipAuxError,
trfIndexVisisted
} + treeReprAllFields - {
trfShowNodeLineInfo,
trfShowSymLineInfo,
Expand Down Expand Up @@ -252,12 +254,15 @@ proc treeRepr*(
maxPath: int = 1,
indentIncrease: int = 0
): ColText =
## .. include:: tree_repr_doc.rst


coloredResult(1)


var visited: HashSet[int]
var visited: Table[int, int]
var res = addr result

var nodecount = 0
proc aux(n: PNode, idx: seq[int]) =
var indent = indentIncrease
genFields(res[], indent, flags)
Expand Down Expand Up @@ -297,32 +302,52 @@ proc treeRepr*(
add "<nil>" + fgRed
return

elif cast[int](n) in visited:
add " <visited>"
elif not (n.kind == nkEmpty and n.safeLen == 0) and
# empty nodes can be reused. Only check for visitation if it is
# not an empty (completely empty) node
cast[int](n) in visited:

if trfIndexVisisted in flags:
add "<visited "
add substr($n.kind, 2) + fgCyan
add " at "
add $visited[cast[int](n)] + styleDim
add ">"

else:
add "<visited>"

return

elif idx.len > maxDepth:
add " ..."
return

visited.incl cast[int](n)

visited[cast[int](n)] = nodecount
add substr($n.kind, 2) + fgCyan

if trfIndexVisisted in flags:
add " "
add $nodecount + styleDim

inc nodecount

when defined(useNodeids):
if trfShowNodeIds in flags:
hfield("nid")
add $n.id

proc addComment(sep: bool = true) =
if trfShowNodeComments in flags and n.comment.len > 0:
add "\n"
var nl = false
for line in split(n.comment.strip(leading = false), '\n'):
if nl: add "\n"
nl = true

addi indent, " # " & line + fgCyan
addi indent, " # " + termFg(4, 2, 1)
add line + termFg(4, 2, 1)

add "\n"

elif sep:
add " "
Expand Down Expand Up @@ -397,6 +422,7 @@ proc treeRepr*(

of nkCommentStmt:
addFlags()
add "\n"
addComment()

of nkError:
Expand All @@ -418,7 +444,7 @@ proc treeRepr*(
discard


if n.kind notin nkNone .. nkNilLit:
if n.kind notin {nkNone .. nkNilLit, nkCommentStmt}:
addFlags()
if n.len > 0:
add "\n"
Expand Down
34 changes: 18 additions & 16 deletions compiler/utils/tree_repr_doc.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
Generate tree representation from `PNode`.

Procedure arguments:

- `conf`: some configuration options are inferred from it, such as
`--filenames:canonical`, but it is mostly used to get full paths from
file ids
- `pnode`: node to be printed - can be `nil`, recursive, `nkError` or any
other kind form or shape.
- `flags`: Set of the formatting options for the procedure. Several
built-in constants are defined in this module that might be suitable for
different debugging or AST exploration tasks.
- `maxDepth`: Ignore all nodes that are placed deeper than that. Useful to
see a high-level overview for large nodes, such as full proc bodies
- `maxLength`: on each level, print upto that many subnodes. Useful to cut
out parts on large `case` trees, toplevel nodes for modules, statement
lists and such.
- `maxPath`: maximum path length for subnodes
- `indentIncreas`: start tree indentation from certain formatting

Generated output packs in a lot of information - here is a short list of
features, those will be elaborated on

Expand All @@ -19,22 +37,6 @@ features, those will be elaborated on
- symbol definition location if any
- type symbols definition location if any

Procedure arguments:

- `conf`: some configuration options are inferred from it, such as
`--filenames:canonical`, but it is mostly used to get full paths from
file ids
- `pnode`: node to be printed - can be `nil`, recursive, `nkError` or any
other kind form or shape.
- `flags`: Set of the formatting options for the procedure. Several
built-in constants are defined in this module that might be suitable for
different debugging or AST exploration tasks.
- `maxDepth`: Ignore all nodes that are placed deeper than that. Useful to
see a high-level overview for large nodes, such as full proc bodies
- `maxLength`: on each level, print upto that many subnodes. Useful to cut
out parts on large `case` trees, toplevel nodes for modules, statement
lists and such.


.. note:: Output examples in the documentation might be slightly different
from the actual output due to changes in the default formatting
Expand Down

0 comments on commit dce4c78

Please sign in to comment.