Skip to content

Commit 28f5933

Browse files
committed
--filenames:abs|canonical|magic
1 parent 1ed83d9 commit 28f5933

File tree

8 files changed

+55
-24
lines changed

8 files changed

+55
-24
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@
335335

336336
- Added `--spellSuggest` to show spelling suggestions on typos.
337337

338+
- Added `--filenames:abs|canonical|magic` which replaces --listFullPaths:on|off
339+
338340
- Source+Edit links now appear on top of every docgen'd page when
339341
`nim doc --git.url:url ...` is given.
340342

compiler/commands.nim

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ proc splitSwitch(conf: ConfigRef; switch: string, cmd, arg: var string, pass: TC
141141
elif switch[i] == '[': arg = substr(switch, i)
142142
else: invalidCmdLineOption(conf, pass, switch, info)
143143

144+
template switchOn(arg: string): bool =
145+
# xxx use `switchOn` wherever appropriate
146+
case arg.normalize
147+
of "", "on": true
148+
of "off": false
149+
else:
150+
localError(conf, info, errOnOrOffExpectedButXFound % arg)
151+
false
152+
144153
proc processOnOffSwitch(conf: ConfigRef; op: TOptions, arg: string, pass: TCmdLinePass,
145154
info: TLineInfo) =
146155
case arg.normalize
@@ -885,8 +894,15 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
885894
trackIde(conf, ideDus, arg, info)
886895
of "stdout":
887896
processOnOffSwitchG(conf, {optStdout}, arg, pass, info)
897+
of "filenames":
898+
case arg.normalize
899+
of "abs": conf.filenameOption = foAbs
900+
of "canonical": conf.filenameOption = foCanonical
901+
of "magic": conf.filenameOption = foMagicSauce
902+
else: localError(conf, info, "expected: abs|canonical|magic, got: $1" % arg)
888903
of "listfullpaths":
889-
processOnOffSwitchG(conf, {optListFullPaths}, arg, pass, info)
904+
conf.filenameOption = if switchOn(arg): foAbs else: foMagicSauce
905+
# xxx in future work, s/foMagicSauce/foCanonical/ here
890906
of "spellsuggest":
891907
if arg.len == 0: conf.spellSuggestMax = spellSuggestSecretSauce
892908
elif arg == "auto": conf.spellSuggestMax = spellSuggestSecretSauce

compiler/main.nim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ proc mainCommand*(graph: ModuleGraph) =
362362
rawMessage(conf, errGenerated, "invalid command: " & conf.command)
363363

364364
if conf.errorCounter == 0 and conf.cmd notin {cmdTcc, cmdDump, cmdNop}:
365+
# D20210419T170230:here
365366
let mem =
366367
when declared(system.getMaxMem): formatSize(getMaxMem()) & " peakmem"
367368
else: formatSize(getTotalMem()) & " totmem"
@@ -370,8 +371,8 @@ proc mainCommand*(graph: ModuleGraph) =
370371
elif isDefined(conf, "release"): "Release"
371372
else: "Debug"
372373
let sec = formatFloat(epochTime() - conf.lastCmdTime, ffDecimal, 3)
373-
let project = if optListFullPaths in conf.globalOptions: $conf.projectFull else: $conf.projectName
374-
374+
let project = if conf.filenameOption == foAbs: $conf.projectFull else: $conf.projectName
375+
# xxx honor conf.filenameOption more accurately
375376
var output: string
376377
if optCompileOnly in conf.globalOptions and conf.cmd != cmdJsonscript:
377378
output = $conf.jsonBuildFile
@@ -380,7 +381,8 @@ proc mainCommand*(graph: ModuleGraph) =
380381
output = "unknownOutput"
381382
else:
382383
output = $conf.absOutFile
383-
if optListFullPaths notin conf.globalOptions: output = output.AbsoluteFile.extractFilename
384+
if conf.filenameOption != foAbs: output = output.AbsoluteFile.extractFilename
385+
# xxx honor filenameOption more accurately
384386
if optProfileVM in conf.globalOptions:
385387
echo conf.dump(conf.vmProfileData)
386388
rawMessage(conf, hintSuccessX, [

compiler/msgs.nim

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,32 +241,30 @@ template toFullPath*(conf: ConfigRef; info: TLineInfo): string =
241241
template toFullPathConsiderDirty*(conf: ConfigRef; info: TLineInfo): string =
242242
string toFullPathConsiderDirty(conf, info.fileIndex)
243243

244-
type FilenameOption* = enum
245-
foAbs # absolute path, e.g.: /pathto/bar/foo.nim
246-
foRelProject # relative to project path, e.g.: ../foo.nim
247-
foMagicSauce # magic sauce, shortest of (foAbs, foRelProject)
248-
foName # lastPathPart, e.g.: foo.nim
249-
foStacktrace # if optExcessiveStackTrace: foAbs else: foName
250-
251244
proc toFilenameOption*(conf: ConfigRef, fileIdx: FileIndex, opt: FilenameOption): string =
252245
case opt
253246
of foAbs: result = toFullPath(conf, fileIdx)
254247
of foRelProject: result = toProjPath(conf, fileIdx)
255-
of foMagicSauce:
248+
of foCanonical:
256249
let absPath = toFullPath(conf, fileIdx)
257-
if optListFullPaths in conf.globalOptions:
258-
result = absPath
259-
else:
260-
result = canonicalImportAux(conf, absPath.AbsoluteFile)
250+
result = canonicalImportAux(conf, absPath.AbsoluteFile)
261251
of foName: result = toProjPath(conf, fileIdx).lastPathPart
252+
of foMagicSauce:
253+
let
254+
absPath = toFullPath(conf, fileIdx)
255+
relPath = toProjPath(conf, fileIdx)
256+
result = if (relPath.len > absPath.len) or (relPath.count("..") > 2):
257+
absPath
258+
else:
259+
relPath
262260
of foStacktrace:
263261
if optExcessiveStackTrace in conf.globalOptions:
264262
result = toFilenameOption(conf, fileIdx, foAbs)
265263
else:
266264
result = toFilenameOption(conf, fileIdx, foName)
267265

268-
proc toMsgFilename*(conf: ConfigRef; info: FileIndex): string =
269-
toFilenameOption(conf, info, foMagicSauce)
266+
proc toMsgFilename*(conf: ConfigRef; fileIdx: FileIndex): string =
267+
toFilenameOption(conf, fileIdx, conf.filenameOption)
270268

271269
template toMsgFilename*(conf: ConfigRef; info: TLineInfo): string =
272270
toMsgFilename(conf, info.fileIndex)

compiler/options.nim

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ type # please make sure we have under 32 options
7878
optWholeProject # for 'doc': output any dependency
7979
optDocInternal # generate documentation for non-exported symbols
8080
optMixedMode # true if some module triggered C++ codegen
81-
optListFullPaths # use full paths in toMsgFilename
8281
optDeclaredLocs # show declaration locations in messages
8382
optNoNimblePath
8483
optHotCodeReloading
@@ -258,6 +257,14 @@ type
258257
stdOrrStdout
259258
stdOrrStderr
260259

260+
FilenameOption* = enum
261+
foAbs # absolute path, e.g.: /pathto/bar/foo.nim
262+
foRelProject # relative to project path, e.g.: ../foo.nim
263+
foCanonical # canonical module name
264+
foMagicSauce # magic sauce, shortest of (foAbs, foRelProject)
265+
foName # lastPathPart, e.g.: foo.nim
266+
foStacktrace # if optExcessiveStackTrace: foAbs else: foName
267+
261268
ConfigRef* = ref object ## every global configuration
262269
## fields marked with '*' are subject to
263270
## the incremental compilation mechanisms
@@ -270,6 +277,7 @@ type
270277
macrosToExpand*: StringTableRef
271278
arcToExpand*: StringTableRef
272279
m*: MsgConfig
280+
filenameOption*: FilenameOption # how to render paths in compiler messages
273281
evalTemplateCounter*: int
274282
evalMacroCounter*: int
275283
exitcode*: int8
@@ -413,8 +421,7 @@ const
413421
optBoundsCheck, optOverflowCheck, optAssert, optWarns, optRefCheck,
414422
optHints, optStackTrace, optLineTrace, # consider adding `optStackTraceMsgs`
415423
optTrMacros, optStyleCheck, optCursorInference}
416-
DefaultGlobalOptions* = {optThreadAnalysis,
417-
optExcessiveStackTrace, optListFullPaths}
424+
DefaultGlobalOptions* = {optThreadAnalysis, optExcessiveStackTrace}
418425

419426
proc getSrcTimestamp(): DateTime =
420427
try:
@@ -461,6 +468,7 @@ proc newConfigRef*(): ConfigRef =
461468
macrosToExpand: newStringTable(modeStyleInsensitive),
462469
arcToExpand: newStringTable(modeStyleInsensitive),
463470
m: initMsgConfig(),
471+
filenameOption: foAbs,
464472
cppDefines: initHashSet[string](),
465473
headerFile: "", features: {}, legacyFeatures: {}, foreignPackageNotes: foreignPackageNotesDefault,
466474
notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1],
@@ -514,6 +522,7 @@ proc newConfigRef*(): ConfigRef =
514522

515523
proc newPartialConfigRef*(): ConfigRef =
516524
## create a new ConfigRef that is only good enough for error reporting.
525+
# xxx FACTOR with `newConfigRef`
517526
when defined(nimDebugUtils):
518527
result = getConfigRef()
519528
else:
@@ -522,6 +531,7 @@ proc newPartialConfigRef*(): ConfigRef =
522531
verbosity: 1,
523532
options: DefaultOptions,
524533
globalOptions: DefaultGlobalOptions,
534+
filenameOption: foAbs,
525535
foreignPackageNotes: foreignPackageNotesDefault,
526536
notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1])
527537

doc/advopt.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ Advanced options:
3636
to after all options have been processed
3737
--stdout:on|off output to stdout
3838
--colors:on|off turn compiler messages coloring on|off
39-
--listFullPaths:on|off list full paths in messages
39+
--filenames:abs|canonical|magic
40+
customize how filenames are rendered in compiler messages,
41+
defaults to `abs` (absolute)
4042
--declaredLocs:on|off show declaration locations in messages
4143
--spellSuggest|:num show at most `num >= 0` spelling suggestions on typos.
4244
if `num` is not specified (or `auto`), return

drnim/drnim.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ proc mainCommand(graph: ModuleGraph) =
12051205
registerPass graph, semPass
12061206
compileProject(graph)
12071207
if conf.errorCounter == 0:
1208+
# xxx deduplicate with D20210419T170230
12081209
let mem =
12091210
when declared(system.getMaxMem): formatSize(getMaxMem()) & " peakmem"
12101211
else: formatSize(getTotalMem()) & " totmem"
@@ -1213,7 +1214,7 @@ proc mainCommand(graph: ModuleGraph) =
12131214
elif isDefined(conf, "release"): "Release"
12141215
else: "Debug"
12151216
let sec = formatFloat(epochTime() - conf.lastCmdTime, ffDecimal, 3)
1216-
let project = if optListFullPaths in conf.globalOptions: $conf.projectFull else: $conf.projectName
1217+
let project = if conf.filenameOption == foAbs: $conf.projectFull else: $conf.projectName
12171218
rawMessage(conf, hintSuccessX, [
12181219
"loc", loc,
12191220
"sec", sec,

tests/config.nims

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ switch("path", "$lib/../testament/lib")
66
## prevent common user config settings to interfere with testament expectations
77
## Indifidual tests can override this if needed to test for these options.
88
switch("colors", "off")
9-
switch("listFullPaths", "off")
9+
switch("filenames", "magic")
1010
switch("excessiveStackTrace", "off")
1111
switch("spellSuggest", "0")
1212

0 commit comments

Comments
 (0)