Skip to content

Commit

Permalink
--filenames:abs|canonical|magic
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Apr 20, 2021
1 parent 1ed83d9 commit 28f5933
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 24 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@

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

- Added `--filenames:abs|canonical|magic` which replaces --listFullPaths:on|off

- Source+Edit links now appear on top of every docgen'd page when
`nim doc --git.url:url ...` is given.

Expand Down
18 changes: 17 additions & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ proc splitSwitch(conf: ConfigRef; switch: string, cmd, arg: var string, pass: TC
elif switch[i] == '[': arg = substr(switch, i)
else: invalidCmdLineOption(conf, pass, switch, info)

template switchOn(arg: string): bool =
# xxx use `switchOn` wherever appropriate
case arg.normalize
of "", "on": true
of "off": false
else:
localError(conf, info, errOnOrOffExpectedButXFound % arg)
false

proc processOnOffSwitch(conf: ConfigRef; op: TOptions, arg: string, pass: TCmdLinePass,
info: TLineInfo) =
case arg.normalize
Expand Down Expand Up @@ -885,8 +894,15 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
trackIde(conf, ideDus, arg, info)
of "stdout":
processOnOffSwitchG(conf, {optStdout}, arg, pass, info)
of "filenames":
case arg.normalize
of "abs": conf.filenameOption = foAbs
of "canonical": conf.filenameOption = foCanonical
of "magic": conf.filenameOption = foMagicSauce
else: localError(conf, info, "expected: abs|canonical|magic, got: $1" % arg)
of "listfullpaths":
processOnOffSwitchG(conf, {optListFullPaths}, arg, pass, info)
conf.filenameOption = if switchOn(arg): foAbs else: foMagicSauce
# xxx in future work, s/foMagicSauce/foCanonical/ here
of "spellsuggest":
if arg.len == 0: conf.spellSuggestMax = spellSuggestSecretSauce
elif arg == "auto": conf.spellSuggestMax = spellSuggestSecretSauce
Expand Down
8 changes: 5 additions & 3 deletions compiler/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ proc mainCommand*(graph: ModuleGraph) =
rawMessage(conf, errGenerated, "invalid command: " & conf.command)

if conf.errorCounter == 0 and conf.cmd notin {cmdTcc, cmdDump, cmdNop}:
# D20210419T170230:here
let mem =
when declared(system.getMaxMem): formatSize(getMaxMem()) & " peakmem"
else: formatSize(getTotalMem()) & " totmem"
Expand All @@ -370,8 +371,8 @@ proc mainCommand*(graph: ModuleGraph) =
elif isDefined(conf, "release"): "Release"
else: "Debug"
let sec = formatFloat(epochTime() - conf.lastCmdTime, ffDecimal, 3)
let project = if optListFullPaths in conf.globalOptions: $conf.projectFull else: $conf.projectName

let project = if conf.filenameOption == foAbs: $conf.projectFull else: $conf.projectName
# xxx honor conf.filenameOption more accurately
var output: string
if optCompileOnly in conf.globalOptions and conf.cmd != cmdJsonscript:
output = $conf.jsonBuildFile
Expand All @@ -380,7 +381,8 @@ proc mainCommand*(graph: ModuleGraph) =
output = "unknownOutput"
else:
output = $conf.absOutFile
if optListFullPaths notin conf.globalOptions: output = output.AbsoluteFile.extractFilename
if conf.filenameOption != foAbs: output = output.AbsoluteFile.extractFilename
# xxx honor filenameOption more accurately
if optProfileVM in conf.globalOptions:
echo conf.dump(conf.vmProfileData)
rawMessage(conf, hintSuccessX, [
Expand Down
26 changes: 12 additions & 14 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,32 +241,30 @@ template toFullPath*(conf: ConfigRef; info: TLineInfo): string =
template toFullPathConsiderDirty*(conf: ConfigRef; info: TLineInfo): string =
string toFullPathConsiderDirty(conf, info.fileIndex)

type FilenameOption* = enum
foAbs # absolute path, e.g.: /pathto/bar/foo.nim
foRelProject # relative to project path, e.g.: ../foo.nim
foMagicSauce # magic sauce, shortest of (foAbs, foRelProject)
foName # lastPathPart, e.g.: foo.nim
foStacktrace # if optExcessiveStackTrace: foAbs else: foName

proc toFilenameOption*(conf: ConfigRef, fileIdx: FileIndex, opt: FilenameOption): string =
case opt
of foAbs: result = toFullPath(conf, fileIdx)
of foRelProject: result = toProjPath(conf, fileIdx)
of foMagicSauce:
of foCanonical:
let absPath = toFullPath(conf, fileIdx)
if optListFullPaths in conf.globalOptions:
result = absPath
else:
result = canonicalImportAux(conf, absPath.AbsoluteFile)
result = canonicalImportAux(conf, absPath.AbsoluteFile)
of foName: result = toProjPath(conf, fileIdx).lastPathPart
of foMagicSauce:
let
absPath = toFullPath(conf, fileIdx)
relPath = toProjPath(conf, fileIdx)
result = if (relPath.len > absPath.len) or (relPath.count("..") > 2):
absPath
else:
relPath
of foStacktrace:
if optExcessiveStackTrace in conf.globalOptions:
result = toFilenameOption(conf, fileIdx, foAbs)
else:
result = toFilenameOption(conf, fileIdx, foName)

proc toMsgFilename*(conf: ConfigRef; info: FileIndex): string =
toFilenameOption(conf, info, foMagicSauce)
proc toMsgFilename*(conf: ConfigRef; fileIdx: FileIndex): string =
toFilenameOption(conf, fileIdx, conf.filenameOption)

template toMsgFilename*(conf: ConfigRef; info: TLineInfo): string =
toMsgFilename(conf, info.fileIndex)
Expand Down
16 changes: 13 additions & 3 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ type # please make sure we have under 32 options
optWholeProject # for 'doc': output any dependency
optDocInternal # generate documentation for non-exported symbols
optMixedMode # true if some module triggered C++ codegen
optListFullPaths # use full paths in toMsgFilename
optDeclaredLocs # show declaration locations in messages
optNoNimblePath
optHotCodeReloading
Expand Down Expand Up @@ -258,6 +257,14 @@ type
stdOrrStdout
stdOrrStderr

FilenameOption* = enum
foAbs # absolute path, e.g.: /pathto/bar/foo.nim
foRelProject # relative to project path, e.g.: ../foo.nim
foCanonical # canonical module name
foMagicSauce # magic sauce, shortest of (foAbs, foRelProject)
foName # lastPathPart, e.g.: foo.nim
foStacktrace # if optExcessiveStackTrace: foAbs else: foName

ConfigRef* = ref object ## every global configuration
## fields marked with '*' are subject to
## the incremental compilation mechanisms
Expand All @@ -270,6 +277,7 @@ type
macrosToExpand*: StringTableRef
arcToExpand*: StringTableRef
m*: MsgConfig
filenameOption*: FilenameOption # how to render paths in compiler messages
evalTemplateCounter*: int
evalMacroCounter*: int
exitcode*: int8
Expand Down Expand Up @@ -413,8 +421,7 @@ const
optBoundsCheck, optOverflowCheck, optAssert, optWarns, optRefCheck,
optHints, optStackTrace, optLineTrace, # consider adding `optStackTraceMsgs`
optTrMacros, optStyleCheck, optCursorInference}
DefaultGlobalOptions* = {optThreadAnalysis,
optExcessiveStackTrace, optListFullPaths}
DefaultGlobalOptions* = {optThreadAnalysis, optExcessiveStackTrace}

proc getSrcTimestamp(): DateTime =
try:
Expand Down Expand Up @@ -461,6 +468,7 @@ proc newConfigRef*(): ConfigRef =
macrosToExpand: newStringTable(modeStyleInsensitive),
arcToExpand: newStringTable(modeStyleInsensitive),
m: initMsgConfig(),
filenameOption: foAbs,
cppDefines: initHashSet[string](),
headerFile: "", features: {}, legacyFeatures: {}, foreignPackageNotes: foreignPackageNotesDefault,
notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1],
Expand Down Expand Up @@ -514,6 +522,7 @@ proc newConfigRef*(): ConfigRef =

proc newPartialConfigRef*(): ConfigRef =
## create a new ConfigRef that is only good enough for error reporting.
# xxx FACTOR with `newConfigRef`
when defined(nimDebugUtils):
result = getConfigRef()
else:
Expand All @@ -522,6 +531,7 @@ proc newPartialConfigRef*(): ConfigRef =
verbosity: 1,
options: DefaultOptions,
globalOptions: DefaultGlobalOptions,
filenameOption: foAbs,
foreignPackageNotes: foreignPackageNotesDefault,
notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1])

Expand Down
4 changes: 3 additions & 1 deletion doc/advopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Advanced options:
to after all options have been processed
--stdout:on|off output to stdout
--colors:on|off turn compiler messages coloring on|off
--listFullPaths:on|off list full paths in messages
--filenames:abs|canonical|magic
customize how filenames are rendered in compiler messages,
defaults to `abs` (absolute)
--declaredLocs:on|off show declaration locations in messages
--spellSuggest|:num show at most `num >= 0` spelling suggestions on typos.
if `num` is not specified (or `auto`), return
Expand Down
3 changes: 2 additions & 1 deletion drnim/drnim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ proc mainCommand(graph: ModuleGraph) =
registerPass graph, semPass
compileProject(graph)
if conf.errorCounter == 0:
# xxx deduplicate with D20210419T170230
let mem =
when declared(system.getMaxMem): formatSize(getMaxMem()) & " peakmem"
else: formatSize(getTotalMem()) & " totmem"
Expand All @@ -1213,7 +1214,7 @@ proc mainCommand(graph: ModuleGraph) =
elif isDefined(conf, "release"): "Release"
else: "Debug"
let sec = formatFloat(epochTime() - conf.lastCmdTime, ffDecimal, 3)
let project = if optListFullPaths in conf.globalOptions: $conf.projectFull else: $conf.projectName
let project = if conf.filenameOption == foAbs: $conf.projectFull else: $conf.projectName
rawMessage(conf, hintSuccessX, [
"loc", loc,
"sec", sec,
Expand Down
2 changes: 1 addition & 1 deletion tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ switch("path", "$lib/../testament/lib")
## prevent common user config settings to interfere with testament expectations
## Indifidual tests can override this if needed to test for these options.
switch("colors", "off")
switch("listFullPaths", "off")
switch("filenames", "magic")
switch("excessiveStackTrace", "off")
switch("spellSuggest", "0")

Expand Down

0 comments on commit 28f5933

Please sign in to comment.