summary refs log tree commit diff stats
path: root/compiler/options.nim
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-04-21 06:26:27 -0700
committerGitHub <noreply@github.com>2021-04-21 15:26:27 +0200
commitda1c1a711780e21a372ef70f9080aebe5b9ef987 (patch)
treee068b6248c791ba0899bad85efac113d8c5f8076 /compiler/options.nim
parentc631648cb31b11d61d249e0745181acb0fcc30cc (diff)
downloadNim-da1c1a711780e21a372ef70f9080aebe5b9ef987.tar.gz
`--filenames:abs|canonical|legacyRelProj` for filenames in compiler msgs (replaces `--listfullpaths:on|off`) (#17746)
* use canonicalImport for filename_magicSauce
* --filenames:abs|canonical|magic
* rename: magic => legacyRelProj
Diffstat (limited to 'compiler/options.nim')
-rw-r--r--compiler/options.nim37
1 files changed, 33 insertions, 4 deletions
diff --git a/compiler/options.nim b/compiler/options.nim
index c1ac6b1e0..2aaaf5844 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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:
@@ -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],
@@ -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:
@@ -522,6 +531,7 @@ proc newPartialConfigRef*(): ConfigRef =
       verbosity: 1,
       options: DefaultOptions,
       globalOptions: DefaultGlobalOptions,
+      filenameOption: foAbs,
       foreignPackageNotes: foreignPackageNotesDefault,
       notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1])
 
@@ -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, {'(', ')', '.'})