Skip to content

Commit

Permalink
unit separator (nim-lang#17730)
Browse files Browse the repository at this point in the history
* use the ASCII Unit Separator so that error messages can be handled precisely by the tooling
* updated testament
  • Loading branch information
Araq authored and PMunch committed Mar 28, 2022
1 parent be25513 commit ad77aa0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 39 deletions.
4 changes: 2 additions & 2 deletions compiler/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ proc mainCommand*(graph: ModuleGraph) =
msgWriteln(conf, $dumpdata, {msgStdout, msgSkipHook})
else:
msgWriteln(conf, "-- list of currently defined symbols --",
{msgStdout, msgSkipHook})
for s in definedSymbolNames(conf.symbols): msgWriteln(conf, s, {msgStdout, msgSkipHook})
{msgStdout, msgSkipHook, msgNoUnitSep})
for s in definedSymbolNames(conf.symbols): msgWriteln(conf, s, {msgStdout, msgSkipHook, msgNoUnitSep})
msgWriteln(conf, "-- end of list --", {msgStdout, msgSkipHook})

for it in conf.searchPaths: msgWriteln(conf, it.string)
Expand Down
61 changes: 35 additions & 26 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ template toFullPath*(conf: ConfigRef; info: TLineInfo): string =
template toFullPathConsiderDirty*(conf: ConfigRef; info: TLineInfo): string =
string toFullPathConsiderDirty(conf, info.fileIndex)

type FilenameOption* = enum
foAbs # absolute path, e.g.: /pathto/bar/foo.nim
foRelProject # relative to project path, e.g.: ../foo.nim
foMagicSauce # magic sauce, shortest of (foAbs, foRelProject)
foName # lastPathPart, e.g.: foo.nim
foStacktrace # if optExcessiveStackTrace: foAbs else: foName
type
FilenameOption* = enum
foAbs # absolute path, e.g.: /pathto/bar/foo.nim
foRelProject # relative to project path, e.g.: ../foo.nim
foMagicSauce # magic sauce, shortest of (foAbs, foRelProject)
foName # lastPathPart, e.g.: foo.nim
foStacktrace # if optExcessiveStackTrace: foAbs else: foName

proc toFilenameOption*(conf: ConfigRef, fileIdx: FileIndex, opt: FilenameOption): string =
case opt
Expand Down Expand Up @@ -295,10 +296,14 @@ proc `??`* (conf: ConfigRef; info: TLineInfo, filename: string): bool =
# only for debugging purposes
result = filename in toFilename(conf, info)

const
UnitSep = "\31"

type
MsgFlag* = enum ## flags altering msgWriteln behavior
msgStdout, ## force writing to stdout, even stderr is default
msgSkipHook ## skip message hook even if it is present
msgNoUnitSep ## the message is a complete "paragraph".
MsgFlags* = set[MsgFlag]

proc msgWriteln*(conf: ConfigRef; s: string, flags: MsgFlags = {}) =
Expand All @@ -310,17 +315,20 @@ proc msgWriteln*(conf: ConfigRef; s: string, flags: MsgFlags = {}) =
## This is used for 'nim dump' etc. where we don't have nimsuggest
## support.
#if conf.cmd == cmdIdeTools and optCDebug notin gGlobalOptions: return
let sep = if msgNoUnitSep notin flags: UnitSep else: ""
if not isNil(conf.writelnHook) and msgSkipHook notin flags:
conf.writelnHook(s)
conf.writelnHook(s & sep)
elif optStdout in conf.globalOptions or msgStdout in flags:
if eStdOut in conf.m.errorOutputs:
flushDot(conf)
writeLine(stdout, s)
write stdout, s
writeLine(stdout, sep)
flushFile(stdout)
else:
if eStdErr in conf.m.errorOutputs:
flushDot(conf)
writeLine(stderr, s)
write stderr, s
writeLine(stderr, sep)
# On Windows stderr is fully-buffered when piped, regardless of C std.
when defined(windows):
flushFile(stderr)
Expand Down Expand Up @@ -366,7 +374,7 @@ proc msgWrite(conf: ConfigRef; s: string) =
flushFile(stdOrr)
conf.lastMsgWasDot.incl stdOrr.toStdOrrKind() # subsequent writes need `flushDot`

template styledMsgWriteln*(args: varargs[typed]) =
template styledMsgWriteln(args: varargs[typed]) =
if not isNil(conf.writelnHook):
callIgnoringStyle(callWritelnHook, nil, args)
elif optStdout in conf.globalOptions:
Expand Down Expand Up @@ -407,7 +415,7 @@ proc quit(conf: ConfigRef; msg: TMsgKind) {.gcsafe.} =
styledMsgWriteln(fgRed, """
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp $1 <file>', see $2 for details""" %
[conf.command, "intern.html#debugging-the-compiler".createDocLink])
[conf.command, "intern.html#debugging-the-compiler".createDocLink], UnitSep)
quit 1

proc handleError(conf: ConfigRef; msg: TMsgKind, eh: TErrorHandling, s: string) =
Expand Down Expand Up @@ -444,10 +452,11 @@ proc writeContext(conf: ConfigRef; lastinfo: TLineInfo) =
conf.structuredErrorHook(conf, context.info, instantiationFrom,
Severity.Hint)
else:
let message = if context.detail == "":
instantiationFrom
else:
instantiationOfFrom.format(context.detail)
let message =
if context.detail == "":
instantiationFrom
else:
instantiationOfFrom.format(context.detail)
styledMsgWriteln(styleBright, conf.toFileLineCol(context.info), " ", resetStyle, message)
info = context.info

Expand Down Expand Up @@ -479,11 +488,12 @@ proc sourceLine*(conf: ConfigRef; i: TLineInfo): string =

result = conf.m.fileInfos[i.fileIndex.int32].lines[i.line.int-1]

proc writeSurroundingSrc(conf: ConfigRef; info: TLineInfo) =
const indent = " "
msgWriteln(conf, indent & $sourceLine(conf, info))
if info.col >= 0:
msgWriteln(conf, indent & spaces(info.col) & '^')
proc getSurroundingSrc(conf: ConfigRef; info: TLineInfo): string =
if conf.hasHint(hintSource) and info != unknownLineInfo:
const indent = " "
result = "\n" & indent & $sourceLine(conf, info)
if info.col >= 0:
result.add "\n" & indent & spaces(info.col) & '^'

proc formatMsg*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string): string =
let title = case msg
Expand Down Expand Up @@ -545,14 +555,13 @@ proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
if msg == hintProcessing:
msgWrite(conf, ".")
else:
styledMsgWriteln(styleBright, loc, resetStyle, color, title, resetStyle, s, KindColor, kindmsg)
if conf.hasHint(hintSource) and info != unknownLineInfo:
conf.writeSurroundingSrc(info)
styledMsgWriteln(styleBright, loc, resetStyle, color, title, resetStyle, s, KindColor, kindmsg,
resetStyle, conf.getSurroundingSrc(info), UnitSep)
if hintMsgOrigin in conf.mainPackageNotes:
styledMsgWriteln(styleBright, toFileLineCol(info2), resetStyle,
" compiler msg initiated here", KindColor,
KindFormat % $hintMsgOrigin,
resetStyle)
resetStyle, UnitSep)
handleError(conf, msg, eh, s)

template rawMessage*(conf: ConfigRef; msg: TMsgKind, args: openArray[string]) =
Expand Down Expand Up @@ -632,8 +641,8 @@ proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
result = conf.m.fileInfos[i.fileIndex.int32].quotedName

template listMsg(title, r) =
msgWriteln(conf, title)
for a in r: msgWriteln(conf, " [$1] $2" % [if a in conf.notes: "x" else: " ", $a])
msgWriteln(conf, title, {msgNoUnitSep})
for a in r: msgWriteln(conf, " [$1] $2" % [if a in conf.notes: "x" else: " ", $a], {msgNoUnitSep})

proc listWarnings*(conf: ConfigRef) = listMsg("Warnings:", warnMin..warnMax)
proc listHints*(conf: ConfigRef) = listMsg("Hints:", hintMin..hintMax)
Expand Down
10 changes: 5 additions & 5 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ proc stackTraceAux(c: PCtx; x: PStackFrame; pc: int; recursionLimit=100) =
while x != nil:
inc calls
x = x.next
msgWriteln(c.config, $calls & " calls omitted\n")
msgWriteln(c.config, $calls & " calls omitted\n", {msgNoUnitSep})
return
stackTraceAux(c, x.next, x.comesFrom, recursionLimit-1)
var info = c.debug[pc]
Expand All @@ -59,12 +59,12 @@ proc stackTraceAux(c: PCtx; x: PStackFrame; pc: int; recursionLimit=100) =
if x.prc != nil:
for k in 1..max(1, 25-s.len): s.add(' ')
s.add(x.prc.name.s)
msgWriteln(c.config, s)
msgWriteln(c.config, s, {msgNoUnitSep})

proc stackTraceImpl(c: PCtx, tos: PStackFrame, pc: int,
msg: string, lineInfo: TLineInfo, infoOrigin: InstantiationInfo) {.noinline.} =
# noinline to avoid code bloat
msgWriteln(c.config, "stack trace: (most recent call last)")
msgWriteln(c.config, "stack trace: (most recent call last)", {msgNoUnitSep})
stackTraceAux(c, tos, pc)
let action = if c.mode == emRepl: doRaise else: doNothing
# XXX test if we want 'globalError' for every mode
Expand Down Expand Up @@ -470,7 +470,7 @@ template handleJmpBack() {.dirty.} =
if allowInfiniteLoops in c.features:
c.loopIterations = c.config.maxLoopIterationsVM
else:
msgWriteln(c.config, "stack trace: (most recent call last)")
msgWriteln(c.config, "stack trace: (most recent call last)", {msgNoUnitSep})
stackTraceAux(c, tos, pc)
globalError(c.config, c.debug[pc], errTooManyIterations % $c.config.maxLoopIterationsVM)
dec(c.loopIterations)
Expand Down Expand Up @@ -1163,7 +1163,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
stackTrace(c, tos, pc, "node is not a proc symbol")
of opcEcho:
let rb = instr.regB
template fn(s) = msgWriteln(c.config, s, {msgStdout})
template fn(s) = msgWriteln(c.config, s, {msgStdout, msgNoUnitSep})
if rb == 1: fn(regs[ra].node.strVal)
else:
var outp = ""
Expand Down
2 changes: 1 addition & 1 deletion testament/important_packages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pkg "gnuplot", "nim c gnuplot.nim"
pkg "hts", "nim c -o:htss src/hts.nim"
pkg "httpauth"
pkg "illwill", "nimble examples"
pkg "inim"
pkg "inim", allowFailure=true
pkg "itertools", "nim doc src/itertools.nim"
pkg "iterutils"
pkg "karax", "nim c -r tests/tester.nim"
Expand Down
2 changes: 1 addition & 1 deletion testament/specs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ proc extractSpec(filename: string; spec: var TSpec): string =

# look for """ only in the first section
if a >= 0 and b > a and a < 40:
result = s.substr(a+3, b-1).replace("'''", tripleQuote)
result = s.substr(a+3, b-1).multiReplace({"'''": tripleQuote, "\\31": "\31"})
else:
#echo "warning: file does not contain spec: " & filename
result = ""
Expand Down
7 changes: 7 additions & 0 deletions testament/testament.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import compiler/nodejs
import lib/stdtest/testutils
from lib/stdtest/specialpaths import splitTestFile

proc trimUnitSep(x: var string) =
let L = x.len
if L > 0 and x[^1] == '\31':
setLen x, L-1

var useColors = true
var backendLogging = true
var simulate = false
Expand Down Expand Up @@ -172,6 +177,7 @@ proc callNimCompiler(cmdTemplate, filename, options, nimcache: string,
result.nimout = ""
while true:
if outp.readLine(x):
trimUnitSep x
result.nimout.add(x & '\n')
if x =~ pegOfInterest:
# `err` should contain the last error/warning message
Expand All @@ -196,6 +202,7 @@ proc callNimCompiler(cmdTemplate, filename, options, nimcache: string,
result.msg = matches[0]
elif suc.isSuccess:
result.err = reSuccess
trimUnitSep result.msg

proc callCCompiler(cmdTemplate, filename, options: string,
target: TTarget): TSpec =
Expand Down
4 changes: 2 additions & 2 deletions tests/misc/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ tests/newconfig/bar/mfoo.nims""".splitLines
var expected = ""
for a in files:
let b = dir / a
expected.add &"Hint: used config file '{b}' [Conf]\n"
expected.add &"Hint: used config file '{b}' [Conf]\31\n"
doAssert outp.endsWith expected, outp & "\n" & expected

block: # mfoo2.customext
let filename = testsDir / "newconfig/foo2/mfoo2.customext"
let cmd = fmt"{nim} e --hint:conf {filename}"
let (outp, exitCode) = execCmdEx(cmd, options = {poStdErrToStdOut})
doAssert exitCode == 0
var expected = &"Hint: used config file '{filename}' [Conf]\n"
var expected = &"Hint: used config file '{filename}' [Conf]\31\n"
doAssert outp.endsWith "123" & "\n" & expected


Expand Down
4 changes: 2 additions & 2 deletions tests/osproc/treadlines.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
output: '''Error: cannot open 'a.nim'
Error: cannot open 'b.nim'
output: '''Error: cannot open 'a.nim'\31
Error: cannot open 'b.nim'\31
'''
targets: "c"
"""
Expand Down

0 comments on commit ad77aa0

Please sign in to comment.