msgs, ccgstmts: fixes #11572 (#11621)

* [refactor] msgs: toFilename now return just the filename

The C codegen uses just the file name for stacktrace when
excessiveStackTrace is off (see quotedName),
so there aren't any reason for other codegen to not do the same.

The file name is now cached in TFileInfo.shortName, which was introduced
for nimsuggest, and went unused after several refactoring of the
compiler.

A toProjPath() proc has been added for the previous behavior of
toFilename().

* ccgstmt: use quotedFilename() for raiseExceptionEx

This is the same proc used for stacktrace when --stacktrace:on

Fixes #11572

* msgs: handle case where file name is not available
This commit is contained in:
alaviss 2019-06-30 10:30:37 +00:00 • committed by Andreas Rumpf
commit e259f80fc7
2 changed files with 15 additions and 8 deletions

View file

@ -690,7 +690,7 @@ proc genRaiseStmt(p: BProc, t: PNode) =
lineCg(p, cpsStmts, "#raiseExceptionEx((#Exception*)$1, $2, $3, $4, $5);$n", lineCg(p, cpsStmts, "#raiseExceptionEx((#Exception*)$1, $2, $3, $4, $5);$n",
[e, makeCString(typ.sym.name.s), [e, makeCString(typ.sym.name.s),
makeCString(if p.prc != nil: p.prc.name.s else: p.module.module.name.s), makeCString(if p.prc != nil: p.prc.name.s else: p.module.module.name.s),
makeCString(toFileName(p.config, t.info)), toLinenumber(t.info)]) quotedFilename(p.config, t.info), toLinenumber(t.info)])
if optNimV2 in p.config.globalOptions: if optNimV2 in p.config.globalOptions:
lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [e]) lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [e])
else: else:

View file

@ -40,9 +40,8 @@ proc newFileInfo(fullPath: AbsoluteFile, projPath: RelativeFile): TFileInfo =
#shallow(result.fullPath) #shallow(result.fullPath)
result.projPath = projPath result.projPath = projPath
#shallow(result.projPath) #shallow(result.projPath)
let fileName = fullPath.extractFilename result.shortName = fullPath.extractFilename
result.shortName = fileName.changeFileExt("") result.quotedName = result.shortName.makeCString
result.quotedName = fileName.makeCString
result.quotedFullName = fullPath.string.makeCString result.quotedFullName = fullPath.string.makeCString
result.lines = @[] result.lines = @[]
when defined(nimpretty): when defined(nimpretty):
@ -165,7 +164,11 @@ template toFilename*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil: if fileIdx.int32 < 0 or conf == nil:
"???" "???"
else: else:
conf.m.fileInfos[fileIdx.int32].projPath.string conf.m.fileInfos[fileIdx.int32].shortName
proc toProjPath*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil: "???"
else: conf.m.fileInfos[fileIdx.int32].projPath.string
proc toFullPath*(conf: ConfigRef; fileIdx: FileIndex): string = proc toFullPath*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil: result = "???" if fileIdx.int32 < 0 or conf == nil: result = "???"
@ -195,6 +198,9 @@ proc toFullPathConsiderDirty*(conf: ConfigRef; fileIdx: FileIndex): AbsoluteFile
template toFilename*(conf: ConfigRef; info: TLineInfo): string = template toFilename*(conf: ConfigRef; info: TLineInfo): string =
toFilename(conf, info.fileIndex) toFilename(conf, info.fileIndex)
template toProjPath*(conf: ConfigRef; info: TLineInfo): string =
toProjPath(conf, info.fileIndex)
template toFullPath*(conf: ConfigRef; info: TLineInfo): string = template toFullPath*(conf: ConfigRef; info: TLineInfo): string =
toFullPath(conf, info.fileIndex) toFullPath(conf, info.fileIndex)
@ -204,7 +210,7 @@ template toFullPathConsiderDirty*(conf: ConfigRef; info: TLineInfo): string =
proc toMsgFilename*(conf: ConfigRef; info: FileIndex): string = proc toMsgFilename*(conf: ConfigRef; info: FileIndex): string =
let let
absPath = toFullPath(conf, info) absPath = toFullPath(conf, info)
relPath = toFilename(conf, info) relPath = toProjPath(conf, info)
result = if (optListFullPaths in conf.globalOptions) or result = if (optListFullPaths in conf.globalOptions) or
(relPath.len > absPath.len) or (relPath.len > absPath.len) or
(relPath.count("..") > 2): (relPath.count("..") > 2):
@ -560,8 +566,9 @@ template internalAssert*(conf: ConfigRef, e: bool) =
if not e: internalError(conf, $instantiationInfo()) if not e: internalError(conf, $instantiationInfo())
proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope = proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
assert i.fileIndex.int32 >= 0 if i.fileIndex.int32 < 0:
if optExcessiveStackTrace in conf.globalOptions: result = makeCString "???"
elif optExcessiveStackTrace in conf.globalOptions:
result = conf.m.fileInfos[i.fileIndex.int32].quotedFullName result = conf.m.fileInfos[i.fileIndex.int32].quotedFullName
else: else:
result = conf.m.fileInfos[i.fileIndex.int32].quotedName result = conf.m.fileInfos[i.fileIndex.int32].quotedName