Browse files

experimental support for outputting code snippets in error messages; …

…implements #301

see #301 for description.
currently, the feature is activated by setting the verbosity level to 2 or more
  • Loading branch information...
1 parent 7a4fe13 commit 7b3d9b752dd2100d02c7f253f8d48db85eab51ad @zah zah committed Apr 9, 2013
Showing with 21 additions and 0 deletions.
  1. +15 −0 compiler/msgs.nim
  2. +6 −0 compiler/options.nim
View
15 compiler/msgs.nim
@@ -506,6 +506,8 @@ var gCodegenLineInfo* = newLineInfo(int32(1), 1, 1)
proc raiseRecoverableError*(msg: string) {.noinline, noreturn.} =
raise newException(ERecoverableError, msg)
+proc sourceLine*(i: TLineInfo): PRope
+
var
gNotes*: TNoteKinds = {low(TNoteKind)..high(TNoteKind)} - {warnShadowIdent}
gErrorCounter*: int = 0 # counts the number of errors
@@ -707,6 +709,11 @@ proc rawMessage*(msg: TMsgKind, arg: string) =
var
lastError = UnknownLineInfo()
+proc writeSurroundingSrc(info: TLineInfo) =
+ const indent = " "
+ MsgWriteln(indent & info.sourceLine.data)
+ MsgWriteln(indent & repeatChar(info.col, ' ') & '^')
+
proc liMessage(info: TLineInfo, msg: TMsgKind, arg: string,
eh: TErrorHandling) =
var frmt: string
@@ -732,6 +739,8 @@ proc liMessage(info: TLineInfo, msg: TMsgKind, arg: string,
coordToStr(info.col), getMessageStr(msg, arg)]
if not ignoreMsg:
MsgWriteln(s)
+ if optPrintSurroundingSrc and msg in errMin..errMax:
+ info.writeSurroundingSrc
handleError(msg, eh, s)
proc Fatal*(info: TLineInfo, msg: TMsgKind, arg = "") =
@@ -771,6 +780,12 @@ proc addSourceLine*(fileIdx: int32, line: string) =
proc sourceLine*(i: TLineInfo): PRope =
if i.fileIndex < 0: return nil
+
+ if not optPreserveOrigSource and
+ fileInfos[i.fileIndex].lines.len == 0:
+ for line in lines(i.toFullPath):
+ addSourceLine i.fileIndex, line.string
+
InternalAssert i.fileIndex < fileInfos.len and
i.line <= fileInfos[i.fileIndex].lines.len
View
6 compiler/options.nim
@@ -113,6 +113,12 @@ proc usesNativeGC*(): bool {.inline.} = gSelectedGC >= gcRefc
template compilationCachePresent*: expr =
{optCaasEnabled, optSymbolFiles} * gGlobalOptions != {}
+template optPreserveOrigSource*: expr =
+ optEmbedOrigSrc in gGlobalOptions
+
+template optPrintSurroundingSrc*: expr =
+ gVerbosity >= 2
+
const
genSubDir* = "nimcache"
NimExt* = "nim"

0 comments on commit 7b3d9b7

Please sign in to comment.