compiler/msgs: Default to stderr for diagnostics

Previously we were defaulting to stdout for diagnostics, which could interfere
with scripts or `nim c -r' programs outputting their results to stdout,
possibly mixing their output with compiler messages.

This change makes now Nim to be inline with other compilers emitting
diagnostics to stderr. Also now --stdout option has proper meaning making all
diagnostics to be sent to stdout instead.

NOTE: Tester now calls compiler with --stdout.
This commit is contained in:
Adam Strzelecki
2015-10-13 13:49:34 +02:00
parent 2bc6acc808
commit abb82554b7
2 changed files with 23 additions and 18 deletions

View File

@@ -734,20 +734,22 @@ proc outWriteln*(s: string) =
flushFile(stdout)
proc msgWriteln*(s: string) =
## Writes to stdout. If --stdout option is given, writes to stderr instead.
## Writes to stderr. If --stdout option is given, writes to stdout instead.
#if gCmd == cmdIdeTools and optCDebug notin gGlobalOptions: return
if not isNil(writelnHook):
writelnHook(s)
elif optStdout in gGlobalOptions:
if eStdErr in errorOutputs:
writeLine(stderr, s)
flushFile(stderr)
else:
if eStdOut in errorOutputs:
writeLine(stdout, s)
flushFile(stdout)
else:
if eStdErr in errorOutputs:
writeLine(stderr, s)
# On Windows stderr is fully-buffered when piped, regardless of C std.
when defined(windows):
flushFile(stderr)
macro callIgnoringStyle(theProc: typed, first: typed,
args: varargs[expr]): stmt =
@@ -767,8 +769,9 @@ macro callIgnoringStyle(theProc: typed, first: typed,
typ != typTerminalCmd:
result.add(arg)
macro callStyledEcho(args: varargs[expr]): stmt =
result = newCall(bindSym"styledEcho")
macro callStyledWriteLineStderr(args: varargs[expr]): stmt =
result = newCall(bindSym"styledWriteLine")
result.add(bindSym"stderr")
for arg in children(args[0][1]):
result.add(arg)
@@ -782,16 +785,18 @@ template styledMsgWriteln*(args: varargs[expr]) =
if not isNil(writelnHook):
callIgnoringStyle(callWritelnHook, nil, args)
elif optStdout in gGlobalOptions:
if eStdErr in errorOutputs:
callIgnoringStyle(writeLine, stderr, args)
flushFile(stderr)
else:
if eStdOut in errorOutputs:
callIgnoringStyle(writeLine, stdout, args)
flushFile(stdout)
else:
if eStdErr in errorOutputs:
if optUseColors in gGlobalOptions:
callStyledEcho(args)
callStyledWriteLineStderr(args)
else:
callIgnoringStyle(writeLine, stdout, args)
flushFile stdout
callIgnoringStyle(writeLine, stderr, args)
# On Windows stderr is fully-buffered when piped, regardless of C std.
when defined(windows):
flushFile(stderr)
proc coordToStr(coord: int): string =
if coord == -1: result = "???"
@@ -885,8 +890,7 @@ proc rawMessage*(msg: TMsgKind, arg: string) =
proc resetAttributes* =
if {optUseColors, optStdout} * gGlobalOptions == {optUseColors}:
terminal.resetAttributes()
stdout.flushFile()
terminal.resetAttributes(stderr)
proc writeSurroundingSrc(info: TLineInfo) =
const indent = " "
@@ -1032,5 +1036,5 @@ proc listHints*() =
])
# enable colors by default on terminals
if terminal.isatty(stdout):
if terminal.isatty(stderr):
incl(gGlobalOptions, optUseColors)

View File

@@ -292,7 +292,8 @@ proc testSpec(r: var TResults, test: TTest) =
case expected.action
of actionCompile:
var given = callCompiler(expected.cmd, test.name,
test.options & " --hint[Path]:off --hint[Processing]:off", test.target)
test.options & " --stdout --hint[Path]:off --hint[Processing]:off",
test.target)
compilerOutputTests(test, given, expected, r)
of actionRun, actionRunNoSpec:
# In this branch of code "early return" pattern is clearer than deep