From 6ab58168661b27778dd6bd64bedf70a7889f5aa0 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 6 Apr 2021 13:01:34 -0700 Subject: [PATCH] add compiler/debugutils.nim to help debugging compiler (#17652) * add compiler/debugutils.nim * address comments --- compiler/debugutils.nim | 19 +++++++++++++++++++ compiler/options.nim | 23 ++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 compiler/debugutils.nim diff --git a/compiler/debugutils.nim b/compiler/debugutils.nim new file mode 100644 index 000000000..235c100b3 --- /dev/null +++ b/compiler/debugutils.nim @@ -0,0 +1,19 @@ +##[ +Utilities to help with debugging nim compiler. + +Experimental API, subject to change. +]## + +import options + +var conf0: ConfigRef + +proc onNewConfigRef*(conf: ConfigRef) {.inline.} = + ## Caches `conf`, which can be retrieved with `getConfigRef`. + ## This avoids having to forward `conf` all the way down the call chain to + ## procs that need it during a debugging session. + conf0 = conf + +proc getConfigRef*(): ConfigRef = + ## nil, if -d:nimDebugUtils wasn't specified + result = conf0 diff --git a/compiler/options.nim b/compiler/options.nim index 2c8d43733..0bdb51159 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -446,6 +446,10 @@ proc newProfileData(): ProfileData = const foreignPackageNotesDefault* = { hintProcessing, warnUnknownMagic, hintQuitCalled, hintExecuting, hintUser, warnUser} +proc isDefined*(conf: ConfigRef; symbol: string): bool + +import debugutils + proc newConfigRef*(): ConfigRef = result = ConfigRef( selectedGC: gcRefc, @@ -504,16 +508,21 @@ proc newConfigRef*(): ConfigRef = # enable colors by default on terminals if terminal.isatty(stderr): incl(result.globalOptions, optUseColors) + when defined(nimDebugUtils): + onNewConfigRef(result) proc newPartialConfigRef*(): ConfigRef = ## create a new ConfigRef that is only good enough for error reporting. - result = ConfigRef( - selectedGC: gcRefc, - verbosity: 1, - options: DefaultOptions, - globalOptions: DefaultGlobalOptions, - foreignPackageNotes: foreignPackageNotesDefault, - notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1]) + when defined(nimDebugUtils): + result = getConfigRef() + else: + result = ConfigRef( + selectedGC: gcRefc, + verbosity: 1, + options: DefaultOptions, + globalOptions: DefaultGlobalOptions, + foreignPackageNotes: foreignPackageNotesDefault, + notes: NotesVerbosity[1], mainPackageNotes: NotesVerbosity[1]) proc cppDefine*(c: ConfigRef; define: string) = c.cppDefines.incl define