Skip to content

Commit

Permalink
no more guessing where compiler msgs came from (nim-lang#14317)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored and EchoPouet committed Jun 13, 2020
1 parent d15d9c9 commit 4d834d6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ proc mydiv(a, b): int {.raises: [].} =
(likewise with other hints and warnings), which is consistent with all other bool flags.
(since 1.3.3).
- `nim doc -r main` and `nim rst2html -r main` now call openDefaultBrowser
- new hint: `--hint:msgOrigin` will show where a compiler msg (hint|warning|error) was generated; this
helps in particular when it's non obvious where it came from either because multiple locations generate
the same message, or because the message involves runtime formatting.

## Tool changes

8 changes: 5 additions & 3 deletions compiler/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ type
hintSource, hintPerformance, hintStackTrace, hintGCStats,
hintGlobalVar, hintExpandMacro,
hintUser, hintUserRaw,
hintExtendedContext
hintExtendedContext,
hintMsgOrigin, # since 1.3.5

const
MsgKindToStr*: array[TMsgKind, string] = [
Expand Down Expand Up @@ -140,6 +141,7 @@ const
hintUser: "$1",
hintUserRaw: "$1",
hintExtendedContext: "$1",
hintMsgOrigin: "$1",
]

const
Expand All @@ -166,7 +168,7 @@ const
"ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",
"Path", "CondTrue", "CondFalse", "Name", "Pattern", "Exec", "Link", "Dependency",
"Source", "Performance", "StackTrace", "GCStats", "GlobalVar", "ExpandMacro",
"User", "UserRaw", "ExtendedContext",
"User", "UserRaw", "ExtendedContext", "MsgOrigin",
]

const
Expand All @@ -192,7 +194,7 @@ proc computeNotesVerbosity(): array[0..3, TNoteKinds] =
result[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext}
result[1] = result[2] - {warnProveField, warnProveIndex,
warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,
hintSource, hintGlobalVar, hintGCStats}
hintSource, hintGlobalVar, hintGCStats, hintMsgOrigin}
result[0] = result[1] - {hintSuccessX, hintSuccess, hintConf,
hintProcessing, hintPattern, hintExecuting, hintLinking, hintCC}

Expand Down
48 changes: 32 additions & 16 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import
lineinfos, pathutils
import std/private/miscdollars

type InstantiationInfo = typeof(instantiationInfo())

proc toCChar*(c: char; result: var string) =
case c
of '\0'..'\x1F', '\x7F'..'\xFF':
Expand Down Expand Up @@ -255,6 +257,9 @@ proc toLinenumber*(info: TLineInfo): int {.inline.} =
proc toColumn*(info: TLineInfo): int {.inline.} =
result = info.col

proc toFileLineCol(info: InstantiationInfo): string {.inline.} =
result.toLocation(info.filename, info.line, info.column + ColOffset)

proc toFileLineCol*(conf: ConfigRef; info: TLineInfo): string {.inline.} =
result.toLocation(toMsgFilename(conf, info), info.line.int, info.col.int + ColOffset)

Expand Down Expand Up @@ -487,7 +492,7 @@ proc formatMsg*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string): s
conf.toFileLineCol(info) & " " & title & getMessageStr(msg, arg)

proc liMessage(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
eh: TErrorHandling) =
eh: TErrorHandling, info2: InstantiationInfo) {.noinline.} =
var
title: string
color: ForegroundColor
Expand Down Expand Up @@ -533,36 +538,47 @@ proc liMessage(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
styledMsgWriteln(styleBright, x, resetStyle, color, title, resetStyle, s)
if conf.hasHint(hintSource):
conf.writeSurroundingSrc(info)
if conf.hasHint(hintMsgOrigin):
styledMsgWriteln(styleBright, toFileLineCol(info2), resetStyle, " compiler msg initiated here", KindColor, KindFormat % HintsToStr[ord(hintMsgOrigin) - ord(hintMin)], resetStyle)

handleError(conf, msg, eh, s)

proc fatal*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
template fatal*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
# this fixes bug #7080 so that it is at least obvious 'fatal'
# was executed.
conf.m.errorOutputs = {eStdOut, eStdErr}
liMessage(conf, info, msg, arg, doAbort)
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, msg, arg, doAbort, info2)

proc globalError*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
liMessage(conf, info, msg, arg, doRaise)
template globalError*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, msg, arg, doRaise, info2)

proc globalError*(conf: ConfigRef; info: TLineInfo, arg: string) =
liMessage(conf, info, errGenerated, arg, doRaise)
template globalError*(conf: ConfigRef; info: TLineInfo, arg: string) =
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, errGenerated, arg, doRaise, info2)

proc localError*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
liMessage(conf, info, msg, arg, doNothing)
template localError*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, msg, arg, doNothing, info2)

proc localError*(conf: ConfigRef; info: TLineInfo, arg: string) =
liMessage(conf, info, errGenerated, arg, doNothing)
template localError*(conf: ConfigRef; info: TLineInfo, arg: string) =
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, errGenerated, arg, doNothing, info2)

proc localError*(conf: ConfigRef; info: TLineInfo, format: string, params: openArray[string]) =
localError(conf, info, format % params)
template localError*(conf: ConfigRef; info: TLineInfo, format: string, params: openArray[string]) =
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, errGenerated, format % params, doNothing, info2)

proc message*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
liMessage(conf, info, msg, arg, doNothing)
template message*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
const info2 = instantiationInfo(-1, fullPaths = true)
liMessage(conf, info, msg, arg, doNothing, info2)

proc internalError*(conf: ConfigRef; info: TLineInfo, errMsg: string) =
if conf.cmd == cmdIdeTools and conf.structuredErrorHook.isNil: return
const info2 = instantiationInfo(-1, fullPaths = true)
writeContext(conf, info)
liMessage(conf, info, errInternal, errMsg, doAbort)
liMessage(conf, info, errInternal, errMsg, doAbort, info2)

proc internalError*(conf: ConfigRef; errMsg: string) =
if conf.cmd == cmdIdeTools and conf.structuredErrorHook.isNil: return
Expand Down

0 comments on commit 4d834d6

Please sign in to comment.