Skip to content

Commit

Permalink
Merge #234
Browse files Browse the repository at this point in the history
234: Finalize 'active' configuration subset split r=saem a=haxscramper

Due to how this thing is implemented, it is hardly possible to separate the mutation part from everything else. So I ended up just writing more documentation and some tests for these parts. 

Co-authored-by: haxscramper <[email protected]>
  • Loading branch information
bors[bot] and haxscramper authored Feb 21, 2022
2 parents fe9e942 + 7084ad6 commit 34a1306
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 86 deletions.
3 changes: 3 additions & 0 deletions compiler/ast/reports.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export
from front/in_options import TOption, TOptions
type InstantiationInfo* = typeof(instantiationInfo())

# Importing and reexporting enums and 'external' reports in order to avoid
# needlessly cluttering the import lists of all modules that have to report
# something (and that would be almost all modules)
import report_enums
export report_enums

Expand Down
9 changes: 8 additions & 1 deletion compiler/backend/extccomp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ proc resetCompilationLists*(conf: ConfigRef) =
conf.externalToLink.setLen 0

proc addExternalFileToLink*(conf: ConfigRef; filename: AbsoluteFile) =
## Add new file to be linked with every compiled target.
conf.externalToLink.insert(filename.string, 0)

proc execWithEcho(conf: ConfigRef; cmd: string, execKind: ReportKind): int =
Expand Down Expand Up @@ -471,6 +472,10 @@ proc noAbsolutePaths(conf: ConfigRef): bool {.inline.} =
result = conf.globalOptions * {optGenScript, optGenMapping} != {}

proc cFileSpecificOptions(conf: ConfigRef; nimname, fullNimFile: string): string =
## Construct CLI options to to compile a specific nim file. Options are
## added in the following order:
## `[global(--passc)][opt=speed/size/debug][file-local][<nimname>.always]`.
## `<nimname>.always` is a configuration variable.
result = conf.compileOptions

for option in conf.compileOptionsCmd:
Expand Down Expand Up @@ -912,7 +917,9 @@ proc callCCompiler*(conf: ConfigRef) =
for idx, it in conf.toCompile:
# call the C compiler for the .c file:
if CfileFlag.Cached in it.flags: continue
let compileCmd = getCompileCFileCmd(conf, it, idx == conf.toCompile.len - 1, produceOutput=true)
let compileCmd = getCompileCFileCmd(
conf, it, idx == conf.toCompile.len - 1, produceOutput=true)

if optCompileOnly notin conf.globalOptions:
cmds.add(compileCmd)
prettyCmds.add displayProgressCC(conf, $it.cname, compileCmd)
Expand Down
10 changes: 6 additions & 4 deletions compiler/front/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ proc dynlibOverride(conf: ConfigRef; switch, arg: string, pass: TCmdLinePass, in
expectArg(conf, switch, arg, pass, info)
options.inclDynlibOverride(conf, arg)

template handleStdinOrCmdInput =
proc handleStdinOrCmdInput(conf: ConfigRef) =
conf.projectFull = conf.projectName.AbsoluteFile
conf.projectPath = AbsoluteDir getCurrentDir()
if conf.outDir.isEmpty:
Expand All @@ -597,11 +597,11 @@ template handleStdinOrCmdInput =
proc handleStdinInput*(conf: ConfigRef) =
conf.projectName = "stdinfile"
conf.projectIsStdin = true
handleStdinOrCmdInput()
handleStdinOrCmdInput(conf)

proc handleCmdInput*(conf: ConfigRef) =
conf.projectName = "cmdfile"
handleStdinOrCmdInput()
handleStdinOrCmdInput(conf)

proc parseCommand*(command: string): Command =
# NOTE when adding elements to this list, sync with `cmdNames` const
Expand Down Expand Up @@ -1373,7 +1373,9 @@ proc addCmdPrefix*(result: var string, kind: CmdLineKind) =
of cmdArgument, cmdEnd: discard

proc processCmdLine*(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
## Process input command-line parameters into `config` settings
## Process input command-line parameters into `config` settings. Input is
## a joined list of command-line arguments with multiple options and/or
## configurations.
var p = parseopt.initOptParser(cmd)
var argsCount = 0

Expand Down
34 changes: 28 additions & 6 deletions compiler/front/in_options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,33 @@ type
## system.
backend*: TBackend ## set via `nim x` or `nim --backend:x`
target*: Target # (+)
localOptions*: TOptions # (+)
globalOptions*: TGlobalOptions # (+)
localOptions*: TOptions ## Localized configuration options - they can
## be set via command-line or using region-local pragmas.
globalOptions*: TGlobalOptions ## Global configuration options that can
## only be supplied from the command line or the configuration files.
cppDefines*: HashSet[string] #[ (*) ]# ## `--cppdefine` ??
features*: set[Feature]
legacyFeatures*: set[LegacyFeature]

symbolFiles*: SymbolFilesOption
symbols*: StringTableRef ## We need to use a StringTableRef here as
## defined symbols are always guaranteed to be style insensitive.
## Otherwise hell would break lose.
prefixDir*: AbsoluteDir
nimcacheDir*: AbsoluteDir ## Directory to write temporary generated
## files to.

libpath*: AbsoluteDir ## Path to the standard library
nimblePaths*: seq[AbsoluteDir] ## List of provided `--nimblePath`
## directories
searchPaths*: seq[AbsoluteDir] ## Explicitly added list of the search
## paths for modules. Those are queried first.
lazyPaths*: seq[AbsoluteDir] ## Implicitly constructed list of the
## search paths for modules. Updated when `--nimblePath` option is
## provided, and consists of explicitly provided nimble paths to the
## found package directories. Last part allows to specify directory for
## packages and avoid specifying `--path` for every single one of them.

nimblePaths*: seq[AbsoluteDir]
searchPaths*: seq[AbsoluteDir]
lazyPaths*: seq[AbsoluteDir]

macrosToExpand*: StringTableRef ## Table of the target macros to expand.
# Used as set for some reason, probably should actually be a set.
Expand Down Expand Up @@ -313,6 +327,14 @@ type

linkOptionsCmd*: seq[string] ## options passed from `passl` on the
## command line.
compileOptionsCmd*: seq[string] ## `passc` on the command line
compileOptionsCmd*: seq[string] ## `passc` on the command line.
## Compilation options that would be used for every single file. They
## are placed in front of the file-specific options.

cppCustomNamespace*: string

configVars*: StringTableRef ## Additional configuration variables for
## providing extra options for different compiler subsystems.

func flip*[I](s: var set[I], it: I, val: bool) =
if val: s.incl it else: s.excl it
41 changes: 23 additions & 18 deletions compiler/front/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ when defined(nimpretty):
# Wrapped in `when defined()` because `.fullContent` is not defined
# without it.
proc fileSection*(conf: ConfigRef; fid: FileIndex; a, b: int): string =
substr(conf.m.fileInfos[fid.int].fullContent, a, b)
substr(conf[fid].fullContent, a, b)

proc canonicalCase(path: var string) =
## the idea is to only use this for checking whether a path is already in
Expand Down Expand Up @@ -211,18 +211,23 @@ template toFilename*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil:
(if fileIdx == commandLineIdx: commandLineDesc else: "???")
else:
conf.m.fileInfos[fileIdx.int32].shortName
conf[fileIdx].shortName

proc toProjPath*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil:
(if fileIdx == commandLineIdx: commandLineDesc else: "???")
else: conf.m.fileInfos[fileIdx.int32].projPath.string
if fileIdx == commandLineIdx:
commandLineDesc
else:
"???"

else:
conf[fileIdx].projPath.string

proc toFullPath*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil:
result = (if fileIdx == commandLineIdx: commandLineDesc else: "???")
else:
result = conf.m.fileInfos[fileIdx.int32].fullPath.string
result = conf[fileIdx].fullPath.string

proc toReportLineInfo*(conf: ConfigRef, info: TLineInfo): ReportLineInfo =
ReportLineInfo(
Expand All @@ -231,24 +236,24 @@ proc toReportLineInfo*(conf: ConfigRef, info: TLineInfo): ReportLineInfo =

proc setDirtyFile*(conf: ConfigRef; fileIdx: FileIndex; filename: AbsoluteFile) =
assert fileIdx.int32 >= 0
conf.m.fileInfos[fileIdx.int32].dirtyFile = filename
setLen conf.m.fileInfos[fileIdx.int32].lines, 0
conf[fileIdx].dirtyFile = filename
setLen conf[fileIdx].lines, 0

proc setHash*(conf: ConfigRef; fileIdx: FileIndex; hash: string) =
assert fileIdx.int32 >= 0
shallowCopy(conf.m.fileInfos[fileIdx.int32].hash, hash)
shallowCopy(conf[fileIdx].hash, hash)

proc getHash*(conf: ConfigRef; fileIdx: FileIndex): string =
assert fileIdx.int32 >= 0
shallowCopy(result, conf.m.fileInfos[fileIdx.int32].hash)
shallowCopy(result, conf[fileIdx].hash)

proc toFullPathConsiderDirty*(conf: ConfigRef; fileIdx: FileIndex): AbsoluteFile =
if fileIdx.int32 < 0:
result = AbsoluteFile(if fileIdx == commandLineIdx: commandLineDesc else: "???")
elif not conf.m.fileInfos[fileIdx.int32].dirtyFile.isEmpty:
result = conf.m.fileInfos[fileIdx.int32].dirtyFile
elif not conf[fileIdx].dirtyFile.isEmpty:
result = conf[fileIdx].dirtyFile
else:
result = conf.m.fileInfos[fileIdx.int32].fullPath
result = conf[fileIdx].fullPath

template toFilename*(conf: ConfigRef; info: TLineInfo): string =
toFilename(conf, info.fileIndex)
Expand Down Expand Up @@ -461,19 +466,19 @@ proc getContext*(conf: ConfigRef; lastinfo: TLineInfo): seq[ReportContext] =
info = context.info

proc addSourceLine(conf: ConfigRef; fileIdx: FileIndex, line: string) =
conf.m.fileInfos[fileIdx.int32].lines.add line
conf[fileIdx].lines.add line

proc numLines*(conf: ConfigRef, fileIdx: FileIndex): int =
## xxx there's an off by 1 error that should be fixed; if a file ends with "foo" or "foo\n"
## it will return same number of lines (ie, a trailing empty line is discounted)
result = conf.m.fileInfos[fileIdx.int32].lines.len
result = conf[fileIdx].lines.len
if result == 0:
try:
for line in lines(toFullPathConsiderDirty(conf, fileIdx).string):
addSourceLine conf, fileIdx, line
except IOError:
discard
result = conf.m.fileInfos[fileIdx.int32].lines.len
result = conf[fileIdx].lines.len

proc sourceLine*(conf: ConfigRef; i: TLineInfo): string =
## 1-based index (matches editor line numbers); 1st line is for i.line = 1
Expand All @@ -483,7 +488,7 @@ proc sourceLine*(conf: ConfigRef; i: TLineInfo): string =
# can happen if the error points to EOF:
if i.line.int > num: return ""

result = conf.m.fileInfos[i.fileIndex.int32].lines[i.line.int-1]
result = conf[i.fileIndex].lines[i.line.int - 1]

proc getSurroundingSrc(conf: ConfigRef; info: TLineInfo): string =
if conf.hasHint(rintSource) and info != unknownLineInfo:
Expand Down Expand Up @@ -700,10 +705,10 @@ proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
result = makeCString "???"

elif optExcessiveStackTrace in conf.globalOptions:
result = conf.m.fileInfos[i.fileIndex.int32].quotedFullName
result = conf[i.fileIndex].quotedFullName

else:
result = conf.m.fileInfos[i.fileIndex.int32].quotedName
result = conf[i.fileIndex].quotedName

proc listWarnings*(conf: ConfigRef) =
conf.localReport(InternalReport(
Expand Down
Loading

0 comments on commit 34a1306

Please sign in to comment.