summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-10-21 06:37:29 -0700
committerGitHub <noreply@github.com>2020-10-21 15:37:29 +0200
commit05752cd5d027a746053897d124f9dae513a0e664 (patch)
tree7340e50ef750bb63720d322eac252fc88189e0cb
parent4b0b3818c3deaa56aa8763bb015d835479b69f67 (diff)
downloadNim-05752cd5d027a746053897d124f9dae513a0e664.tar.gz
add --declaredlocs (#15666)
-rw-r--r--changelog.md2
-rw-r--r--compiler/commands.nim2
-rw-r--r--compiler/lineinfos.nim6
-rw-r--r--compiler/options.nim1
-rw-r--r--compiler/semcall.nim11
-rw-r--r--compiler/types.nim9
-rw-r--r--doc/advopt.txt1
7 files changed, 21 insertions, 11 deletions
diff --git a/changelog.md b/changelog.md
index f585f4e15..dcaffe23c 100644
--- a/changelog.md
+++ b/changelog.md
@@ -11,7 +11,7 @@
 
 
 ## Compiler changes
-
+add `--declaredlocs` to show symbol declaration location in messages
 
 
 ## Tool changes
diff --git a/compiler/commands.nim b/compiler/commands.nim
index b9199c1b0..325b362a9 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -794,6 +794,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
     processOnOffSwitchG(conf, {optStdout}, arg, pass, info)
   of "listfullpaths":
     processOnOffSwitchG(conf, {optListFullPaths}, arg, pass, info)
+  of "declaredlocs":
+    processOnOffSwitchG(conf, {optDeclaredLocs}, arg, pass, info)
   of "dynliboverride":
     dynlibOverride(conf, switch, arg, pass, info)
   of "dynliboverrideall":
diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim
index a0b1d1965..4a7c09276 100644
--- a/compiler/lineinfos.nim
+++ b/compiler/lineinfos.nim
@@ -69,6 +69,7 @@ type
     hintUser, hintUserRaw,
     hintExtendedContext,
     hintMsgOrigin, # since 1.3.5
+    hintDeclaredLoc, # since 1.5.1
 
 const
   MsgKindToStr*: array[TMsgKind, string] = [
@@ -159,6 +160,7 @@ const
     hintUserRaw: "$1",
     hintExtendedContext: "$1",
     hintMsgOrigin: "$1",
+    hintDeclaredLoc: "$1",
   ]
 
 const
@@ -186,7 +188,7 @@ const
     "ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",
     "Path", "CondTrue", "CondFalse", "Name", "Pattern", "Exec", "Link", "Dependency",
     "Source", "Performance", "StackTrace", "GCStats", "GlobalVar", "ExpandMacro",
-    "User", "UserRaw", "ExtendedContext", "MsgOrigin",
+    "User", "UserRaw", "ExtendedContext", "MsgOrigin", "DeclaredLoc"
   ]
 
 const
@@ -215,7 +217,7 @@ type
 
 proc computeNotesVerbosity(): array[0..3, TNoteKinds] =
   result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores}
-  result[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext}
+  result[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext, hintDeclaredLoc}
   result[1] = result[2] - {warnProveField, warnProveIndex,
     warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd,
     hintSource, hintGlobalVar, hintGCStats, hintMsgOrigin}
diff --git a/compiler/options.nim b/compiler/options.nim
index 3be7c5099..872ab9582 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -81,6 +81,7 @@ type                          # please make sure we have under 32 options
     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
     optDynlibOverrideAll
diff --git a/compiler/semcall.nim b/compiler/semcall.nim
index dedae225b..50df1085f 100644
--- a/compiler/semcall.nim
+++ b/compiler/semcall.nim
@@ -208,6 +208,7 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
             {renderNoBody, renderNoComments, renderNoPragmas}))
     else:
       candidates.add(getProcHeader(c.config, err.sym, prefer))
+    candidates.addDeclaredLocMaybe(c.config, err.sym)
     candidates.add("\n")
     let nArg = if err.firstMismatch.arg < n.len: n[err.firstMismatch.arg] else: nil
     let nameParam = if err.firstMismatch.formal != nil: err.firstMismatch.formal.name.s else: ""
@@ -230,9 +231,8 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
         doAssert err.firstMismatch.formal != nil
         candidates.add("\n  required type for " & nameParam &  ": ")
         candidates.add typeToString(wanted)
-        when false:
-          if wanted.sym != nil:
-            candidates.add "(" & (c.config $ wanted.sym.info) & ")"
+        if wanted.sym != nil:
+          candidates.addDeclaredLocMaybe(c.config, wanted.sym)
         candidates.add "\n  but expression '"
         if err.firstMismatch.kind == kVarNeeded:
           candidates.add renderNotLValue(nArg)
@@ -242,9 +242,8 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
           candidates.add "' is of type: "
           var got = nArg.typ
           candidates.add typeToString(got)
-          when false:
-            if got.sym != nil:
-              candidates.add "(" & (c.config $ got.sym.info) & ")"
+          if got.sym != nil:
+            candidates.addDeclaredLocMaybe(c.config, got.sym)
 
           doAssert wanted != nil
           if got != nil: effectProblem(wanted, got, candidates, c)
diff --git a/compiler/types.nim b/compiler/types.nim
index b20bf9c06..75e12cac4 100644
--- a/compiler/types.nim
+++ b/compiler/types.nim
@@ -123,8 +123,13 @@ proc isIntLit*(t: PType): bool {.inline.} =
 proc isFloatLit*(t: PType): bool {.inline.} =
   result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
 
-proc addDeclaredLoc(result: var string, conf: ConfigRef; sym: PSym) =
-  result.add " [declared in " & conf$sym.info & "]"
+proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
+  # result.add " [declared in " & conf$sym.info & "]"
+  result.add " [declared in " & toFileLineCol(conf, sym.info) & "]"
+
+proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
+  if optDeclaredLocs in conf.globalOptions:
+    addDeclaredLoc(result, conf, sym)
 
 proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
   result.add typeToString(typ, prefer)
diff --git a/doc/advopt.txt b/doc/advopt.txt
index f1e6f376d..e5cf7d894 100644
--- a/doc/advopt.txt
+++ b/doc/advopt.txt
@@ -35,6 +35,7 @@ Advanced options:
   --stdout:on|off           output to stdout
   --colors:on|off           turn compiler messages coloring on|off
   --listFullPaths:on|off    list full paths in messages
+  --declaredlocs:on|off     show declaration locations in messages
   -w:on|off|list, --warnings:on|off|list
                             turn all warnings on|off or list all available
   --warning[X]:on|off       turn specific warning X on|off