Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
204: debug: add debug(), debugAst() etc. to astrepr r=haxscramper a=haxscramper

Closes nim-works#203 - adds missing
debug procedures to the `astrepr` module, returning the old API for
debugging.


Co-authored-by: haxscramper <haxscramper@gmail.com>
  • Loading branch information
bors[bot] and haxscramper authored Jan 25, 2022
2 parents cc7e3ed + a8bdd49 commit b9ea9a1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 39 deletions.
30 changes: 2 additions & 28 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import
tables,
intsets,
json,
strtabs
strtabs,
os
],
experimental/[
colortext
Expand Down Expand Up @@ -96,33 +97,6 @@ func wrap(conf: ConfigRef, text: ColText): string =
toString(text, conf.useColor())


import std/[os]

proc formatPath(conf: ConfigRef, path: string): string =
## Format absolute path for reporting
if path in conf.m.filenameToIndexTbl:
# Check if path is registered in filename table index - in that case
# formatting is done using `FileInfo` data from the config.
let id = conf.m.filenameToIndexTbl[path]
result = toFilenameOption(conf, id, conf.filenameOption)

else:
# Path not registered in the filename table - most likely an
# instantiation info report location
when compileOption"excessiveStackTrace":
# instLoc(), when `--excessiveStackTrace` is used, generates full
# paths that /might/ need to be filtered if `--filenames:canonical`.
const compilerRoot = currentSourcePath().parentDir().parentDir()
if conf.filenameOption == foCanonical and
path.startsWith(compilerRoot):
result = path[(compilerRoot.len + 1) .. ^1]

else:
result = path

else:
result = path

proc formatTrace*(conf: ConfigRef, trace: seq[StackTraceEntry]): string =
## Format stack trace entries for reporting
var paths: seq[string]
Expand Down
32 changes: 32 additions & 0 deletions compiler/front/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@ proc toFilenameOption*(conf: ConfigRef, fileIdx: FileIndex, opt: FilenameOption)
else:
result = toFilenameOption(conf, fileIdx, foName)

proc formatPath*(conf: ConfigRef, path: string): string =
## Format absolute file path for error message reporting. If path is not
## registered in the `filenameToIndexTbl` and is not a path to the
## compiler source, return it unchanged. If configuration is nil also
## return path unchanged.
if isNil(conf):
return path

if path in conf.m.filenameToIndexTbl:
# Check if path is registered in filename table index - in that case
# formatting is done using `FileInfo` data from the config.
let id = conf.m.filenameToIndexTbl[path]
result = toFilenameOption(conf, id, conf.filenameOption)

else:
# Path not registered in the filename table - most likely an
# instantiation info report location
when compileOption"excessiveStackTrace":
# instLoc(), when `--excessiveStackTrace` is used, generates full
# paths that /might/ need to be filtered if `--filenames:canonical`.
const compilerRoot = currentSourcePath().parentDir().parentDir()
if conf.filenameOption == foCanonical and
path.startsWith(compilerRoot):
result = path[(compilerRoot.len + 1) .. ^1]

else:
result = path

else:
result = path


proc toMsgFilename*(conf: ConfigRef; fileIdx: FileIndex): string =
toFilenameOption(conf, fileIdx, conf.filenameOption)

Expand Down
76 changes: 66 additions & 10 deletions compiler/utils/astrepr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import
ast,
reports,
renderer,
lineinfos
lineinfos,
errorhandling
],
front/[
options,
Expand Down Expand Up @@ -152,12 +153,10 @@ proc symFields(
if trfShowSymTypes in flags and not sym.typ.isNil():
field("typ", conf.textRepr(sym.typ))

if trfShowSymLineInfo in flags and sym.info != unknownLineInfo:
if not isNil(conf) and trfShowSymLineInfo in flags and sym.info != unknownLineInfo:
field("info", conf.toMsgFilename(sym.info.fileIndex) + fgBlue)
add "("
add $sym.info.line + fgCyan
add "."
add $sym.info.col + fgCyan
add "(", $sym.info.line + fgCyan
add ".", $sym.info.col + fgCyan
add ")"

if trfShowSymOwner in flags and not sym.owner.isNil():
Expand Down Expand Up @@ -349,7 +348,8 @@ proc treeRepr*(
else:
field("typ", conf.textRepr(n.typ))

if trfShowNodeLineInfo in flags and n.info != unknownLineInfo:
if not conf.isNil() and trfShowNodeLineInfo in flags and
n.info != unknownLineInfo:
field("info", conf.toMsgFilename(n.info.fileIndex) + fgBlue)
add "("
add $n.info.line + fgCyan
Expand Down Expand Up @@ -400,9 +400,19 @@ proc treeRepr*(
addComment()

of nkError:
let report = conf.getReport(n).semReport
field("err", substr($report.kind, 4) + termFg(5, 2, 0))
hfield("errid", $n.reportId.int + fgRed)
if isNil(conf):
field("err", substr($n.errorKind(), 4) + termFg(5, 2, 0))

else:
let report = conf.getReport(n).semReport
field("err", substr($report.kind, 4) + termFg(5, 2, 0))
hfield("errid", $n.reportId.int + fgRed)

let (file, line, col) = n.compilerInstInfo()
field("info", formatPath(conf, file) + fgBlue)
add "(", $line + fgCyan
add ".", $col + fgCyan
add ")"

else:
discard
Expand Down Expand Up @@ -432,3 +442,49 @@ proc treeRepr*(
add "\n"

aux(pnode, @[])

proc debugAst*(it: PNode) {.exportc.} =
## Print out tree representation of the AST node.
##
## .. note:: there is no `ConfigRef` argument, and because of that some
## information cannot be fully retrieved from the AST (such as full
## paths of the FileIndex entries).
##
## .. tip:: This proc is annotated with `{.exportc.}` which means it's
## mangled name exactly the same - `debugAst` and you can call it
## from the `gdb` debugging session.
echo treeRepr(nil, it)

proc debugType*(it: PType) {.exportc.} =
## Print out tree represntation of the type. Can also be used in gdb
## debugging session due to `.exportc.` annotation
echo treeRepr(nil, it)

proc debugSym*(it: PSym) {.exportc.} =
## Print out tree represntation of the symbol. Can also be used in gdb
## debugging session due to `.exportc.` annotation
echo treeRepr(nil, it)

proc debug*(it: PNode) =
## Convenience overload of `debugAst`
debugAst(it)

proc debug*(it: PType) =
## Convenience overload of `debugType`
debugType(it)

proc debug*(it: PSym) =
## Convenience overload of `debugSym`
debugSym(it)

proc debug*(conf: ConfigRef, it: PNode) =
## Print tree representation of the AST
echo treeRepr(conf, it)

proc debug*(conf: ConfigRef, it: PType) =
## Print tree representation of the type
echo treeRepr(conf, it)

proc debug*(conf: ConfigRef, it: PSym) =
## Print tree reprsentation of the symbol
echo treeRepr(conf, it)
3 changes: 2 additions & 1 deletion lib/experimental/colortext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,8 @@ template coloredResult*(indentationStep: int = 2): untyped =

template add(arg: untyped): untyped {.used.} = outPtr[].add arg
template add(arg1, arg2: untyped): untyped {.used.} =
outPtr[].add(arg1, arg2)
outPtr[].add(arg1)
outPtr[].add(arg2)

template addIndent(level: int, sep: int = indentationStep): untyped {.used.} =
outPtr[].addIndent(level, sep)
Expand Down

0 comments on commit b9ea9a1

Please sign in to comment.