Skip to content

Commit

Permalink
--filenames:abs|canonical|legacyRelProj for filenames in compiler m…
Browse files Browse the repository at this point in the history
…sgs (replaces `--listfullpaths:on|off`) (#17746)

* use canonicalImport for filename_magicSauce
* --filenames:abs|canonical|magic
* rename: magic => legacyRelProj
  • Loading branch information
timotheecour authored Apr 21, 2021
1 parent c631648 commit da1c1a7
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 41 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,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 "legacyrelproj": conf.filenameOption = foLegacyRelProj
else: localError(conf, info, "expected: abs|canonical|legacyRelProj, got: $1" % arg)
of "listfullpaths":
processOnOffSwitchG(conf, {optListFullPaths}, arg, pass, info)
# xxx in future work, use `warningDeprecated`
conf.filenameOption = if switchOn(arg): foAbs else: foCanonical
of "spellsuggest":
if arg.len == 0: conf.spellSuggestMax = spellSuggestSecretSauce
elif arg == "auto": conf.spellSuggestMax = spellSuggestSecretSauce
Expand Down
15 changes: 0 additions & 15 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,6 @@ proc prettyString(a: object): string =
for k, v in fieldPairs(a):
result.add k & ": " & $v & "\n"

proc canonicalImport*(conf: ConfigRef, file: AbsoluteFile): string =
##[
Shows the canonical module import, e.g.:
system, std/tables, fusion/pointers, system/assertions, std/private/asciitables
]##
var ret = getRelativePathFromConfigPath(conf, file, isTitle = true)
let dir = getNimbleFile(conf, $file).parentDir.AbsoluteDir
if not dir.isEmpty:
let relPath = relativeTo(file, dir)
if not relPath.isEmpty and (ret.isEmpty or relPath.string.len < ret.string.len):
ret = relPath
if ret.isEmpty:
ret = relativeTo(file, conf.projectPath)
result = ret.string.nativeToUnixPath.changeFileExt("")

proc presentationPath*(conf: ConfigRef, file: AbsoluteFile): RelativeFile =
## returns a relative file that will be appended to outDir
let file2 = $file
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
24 changes: 9 additions & 15 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,37 +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)
result = canonicalImportAux(conf, absPath.AbsoluteFile)
of foName: result = toProjPath(conf, fileIdx).lastPathPart
of foLegacyRelProj:
let
absPath = toFullPath(conf, fileIdx)
relPath = toProjPath(conf, fileIdx)
result = if (optListFullPaths in conf.globalOptions) or
(relPath.len > absPath.len) or
(relPath.count("..") > 2):
result = if (relPath.len > absPath.len) or (relPath.count("..") > 2):
absPath
else:
relPath
of foName: result = toProjPath(conf, fileIdx).lastPathPart
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 Expand Up @@ -558,6 +551,7 @@ proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
styledMsgWriteln(styleBright, loc, resetStyle, color, title, resetStyle, s, KindColor, kindmsg,
resetStyle, conf.getSurroundingSrc(info), UnitSep)
if hintMsgOrigin in conf.mainPackageNotes:
# xxx needs a bit of refactoring to honor `conf.filenameOption`
styledMsgWriteln(styleBright, toFileLineCol(info2), resetStyle,
" compiler msg initiated here", KindColor,
KindFormat % $hintMsgOrigin,
Expand Down
37 changes: 33 additions & 4 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import

from terminal import isatty
from times import utc, fromUnix, local, getTime, format, DateTime

from std/private/globs import nativeToUnixPath
const
hasTinyCBackend* = defined(tinyc)
useEffectSystem* = true
Expand Down 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
foLegacyRelProj # legacy, 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 Expand Up @@ -885,6 +895,25 @@ proc findProjectNimFile*(conf: ConfigRef; pkg: string): string =
if dir == "": break
return ""

proc canonicalImportAux*(conf: ConfigRef, file: AbsoluteFile): string =
##[
Shows the canonical module import, e.g.:
system, std/tables, fusion/pointers, system/assertions, std/private/asciitables
]##
var ret = getRelativePathFromConfigPath(conf, file, isTitle = true)
let dir = getNimbleFile(conf, $file).parentDir.AbsoluteDir
if not dir.isEmpty:
let relPath = relativeTo(file, dir)
if not relPath.isEmpty and (ret.isEmpty or relPath.string.len < ret.string.len):
ret = relPath
if ret.isEmpty:
ret = relativeTo(file, conf.projectPath)
result = ret.string

proc canonicalImport*(conf: ConfigRef, file: AbsoluteFile): string =
let ret = canonicalImportAux(conf, file)
result = ret.nativeToUnixPath.changeFileExt("")

proc canonDynlibName(s: string): string =
let start = if s.startsWith("lib"): 3 else: 0
let ende = strutils.find(s, {'(', ')', '.'})
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|legacyRelProj
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", "legacyRelProj")
switch("excessiveStackTrace", "off")
switch("spellSuggest", "0")

Expand Down

0 comments on commit da1c1a7

Please sign in to comment.