compiler/ropes.nim has no global error handler anymore

This commit is contained in:
Andreas Rumpf 2018-05-17 15:45:41 +02:00
commit b2c7910fb6
6 changed files with 48 additions and 42 deletions

View file

@ -19,7 +19,8 @@ import
import strutils except `%` # collides with ropes.`%` import strutils except `%` # collides with ropes.`%`
from modulegraphs import ModuleGraph from modulegraphs import ModuleGraph
from configuration import warnGcMem, errXMustBeCompileTime, hintDependency, errGenerated from configuration import
warnGcMem, errXMustBeCompileTime, hintDependency, errGenerated, errCannotOpenFile
import dynlib import dynlib
when not declared(dynlib.libCandidates): when not declared(dynlib.libCandidates):
@ -1347,7 +1348,8 @@ proc writeHeader(m: BModule) =
result.addf("N_CDECL(void, NimMain)(void);$n", []) result.addf("N_CDECL(void, NimMain)(void);$n", [])
if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim() if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim()
result.addf("#endif /* $1 */$n", [guard]) result.addf("#endif /* $1 */$n", [guard])
writeRope(result, m.filename) if not writeRope(result, m.filename):
rawMessage(m.config, errCannotOpenFile, m.filename)
proc getCFile(m: BModule): string = proc getCFile(m: BModule): string =
let ext = let ext =
@ -1395,12 +1397,14 @@ proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool =
echo "diff ", cfile.cname, ".backup ", cfile.cname echo "diff ", cfile.cname, ".backup ", cfile.cname
else: else:
echo "new file ", cfile.cname echo "new file ", cfile.cname
writeRope(code, cfile.cname) if not writeRope(code, cfile.cname):
rawMessage(m.config, errCannotOpenFile, cfile.cname)
return return
if existsFile(cfile.obj) and os.fileNewer(cfile.obj, cfile.cname): if existsFile(cfile.obj) and os.fileNewer(cfile.obj, cfile.cname):
result = false result = false
else: else:
writeRope(code, cfile.cname) if not writeRope(code, cfile.cname):
rawMessage(m.config, errCannotOpenFile, cfile.cname)
# We need 2 different logics here: pending modules (including # We need 2 different logics here: pending modules (including
# 'nim__dat') may require file merging for the combination of dead code # 'nim__dat') may require file merging for the combination of dead code
@ -1435,7 +1439,8 @@ proc writeModule(m: BModule, pending: bool) =
genInitCode(m) genInitCode(m)
finishTypeDescriptions(m) finishTypeDescriptions(m)
var code = genModule(m, cf) var code = genModule(m, cf)
writeRope(code, cfile) if not writeRope(code, cfile):
rawMessage(m.config, errCannotOpenFile, cfile)
addFileToCompile(m.config, cf) addFileToCompile(m.config, cf)
else: else:
# Consider: first compilation compiles ``system.nim`` and produces # Consider: first compilation compiles ``system.nim`` and produces
@ -1456,7 +1461,8 @@ proc updateCachedModule(m: BModule) =
finishTypeDescriptions(m) finishTypeDescriptions(m)
var code = genModule(m, cf) var code = genModule(m, cf)
writeRope(code, cfile) if not writeRope(code, cfile):
rawMessage(m.config, errCannotOpenFile, cfile)
else: else:
cf.flags = {CfileFlag.Cached} cf.flags = {CfileFlag.Cached}
addFileToCompile(m.config, cf) addFileToCompile(m.config, cf)

View file

@ -14,8 +14,6 @@ import
from modulegraphs import ModuleGraph from modulegraphs import ModuleGraph
proc generateDot*(project: string)
type type
TGen = object of TPassContext TGen = object of TPassContext
module*: PSym module*: PSym
@ -44,8 +42,8 @@ proc addDotDependency(c: PPassContext, n: PNode): PNode =
else: else:
discard discard
proc generateDot(project: string) = proc generateDot*(project: string) =
writeRope("digraph $1 {$n$2}$n" % [ discard writeRope("digraph $1 {$n$2}$n" % [
rope(changeFileExt(extractFilename(project), "")), gDotGraph], rope(changeFileExt(extractFilename(project), "")), gDotGraph],
changeFileExt(project, "dot")) changeFileExt(project, "dot"))

View file

@ -777,10 +777,16 @@ proc getOutFile2(conf: ConfigRef; filename, ext, dir: string): string =
proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) = proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) =
var content = genOutFile(d) var content = genOutFile(d)
var success = true
var filename: string
if optStdout in d.conf.globalOptions: if optStdout in d.conf.globalOptions:
writeRope(stdout, content) writeRope(stdout, content)
filename = "<stdout>"
else: else:
writeRope(content, getOutFile2(d.conf, filename, outExt, "htmldocs"), useWarning) filename = getOutFile2(d.conf, filename, outExt, "htmldocs")
success = writeRope(content, filename)
if not success:
rawMessage(d.conf, if useWarning: warnCannotOpenFile else: errCannotOpenFile, filename)
proc writeOutputJson*(d: PDoc, filename, outExt: string, proc writeOutputJson*(d: PDoc, filename, outExt: string,
useWarning = false) = useWarning = false) =
@ -861,7 +867,9 @@ proc commandJson*(conf: ConfigRef) =
writeRope(stdout, content) writeRope(stdout, content)
else: else:
#echo getOutFile(gProjectFull, JsonExt) #echo getOutFile(gProjectFull, JsonExt)
writeRope(content, getOutFile(conf, conf.projectFull, JsonExt), useWarning = false) let filename = getOutFile(conf, conf.projectFull, JsonExt)
if not writeRope(content, filename):
rawMessage(conf, errCannotOpenFile, filename)
proc commandTags*(conf: ConfigRef) = proc commandTags*(conf: ConfigRef) =
var ast = parseFile(conf.projectMainIdx.FileIndex, newIdentCache(), conf) var ast = parseFile(conf.projectMainIdx.FileIndex, newIdentCache(), conf)
@ -876,7 +884,9 @@ proc commandTags*(conf: ConfigRef) =
writeRope(stdout, content) writeRope(stdout, content)
else: else:
#echo getOutFile(gProjectFull, TagsExt) #echo getOutFile(gProjectFull, TagsExt)
writeRope(content, getOutFile(conf, conf.projectFull, TagsExt), useWarning = false) let filename = getOutFile(conf, conf.projectFull, TagsExt)
if not writeRope(content, filename):
rawMessage(conf, errCannotOpenFile, filename)
proc commandBuildIndex*(conf: ConfigRef) = proc commandBuildIndex*(conf: ConfigRef) =
var content = mergeIndexes(conf.projectFull).rope var content = mergeIndexes(conf.projectFull).rope
@ -887,4 +897,6 @@ proc commandBuildIndex*(conf: ConfigRef) =
["Index".rope, nil, nil, rope(getDateStr()), ["Index".rope, nil, nil, rope(getDateStr()),
rope(getClockStr()), content, nil, nil, nil]) rope(getClockStr()), content, nil, nil, nil])
# no analytics because context is not available # no analytics because context is not available
writeRope(code, getOutFile(conf, "theindex", HtmlExt)) let filename = getOutFile(conf, "theindex", HtmlExt)
if not writeRope(code, filename):
rawMessage(conf, errCannotOpenFile, filename)

View file

@ -438,9 +438,12 @@ proc execExternalProgram*(conf: ConfigRef; cmd: string, msg = hintExecuting) =
proc generateScript(conf: ConfigRef; projectFile: string, script: Rope) = proc generateScript(conf: ConfigRef; projectFile: string, script: Rope) =
let (dir, name, ext) = splitFile(projectFile) let (dir, name, ext) = splitFile(projectFile)
writeRope(script, getNimcacheDir(conf) / addFileExt("compile_" & name, let filename = getNimcacheDir(conf) / addFileExt("compile_" & name,
platform.OS[targetOS].scriptExt)) platform.OS[targetOS].scriptExt)
copyFile(conf.libpath / "nimbase.h", getNimcacheDir(conf) / "nimbase.h") if writeRope(script, filename):
copyFile(conf.libpath / "nimbase.h", getNimcacheDir(conf) / "nimbase.h")
else:
rawMessage(conf, errGenerated, "could not write to file: " & filename)
proc getOptSpeed(conf: ConfigRef; c: TSystemCC): string = proc getOptSpeed(conf: ConfigRef; c: TSystemCC): string =
result = getConfigVar(conf, c, ".options.speed") result = getConfigVar(conf, c, ".options.speed")
@ -872,4 +875,6 @@ proc writeMapping*(conf: ConfigRef; symbolMapping: Rope) =
add(code, strutils.escape(conf.libpath)) add(code, strutils.escape(conf.libpath))
addf(code, "\n[Symbols]$n$1", [symbolMapping]) addf(code, "\n[Symbols]$n$1", [symbolMapping])
writeRope(code, joinPath(conf.projectPath, "mapping.txt")) let filename = joinPath(conf.projectPath, "mapping.txt")
if not writeRope(code, filename):
rawMessage(conf, errGenerated, "could not write to file: " & filename)

View file

@ -618,13 +618,6 @@ proc quotedFilename*(conf: ConfigRef; i: TLineInfo): Rope =
else: else:
result = fileInfos[i.fileIndex.int32].quotedName result = fileInfos[i.fileIndex.int32].quotedName
ropes.errorHandler = proc (err: RopesError, msg: string, useWarning: bool) =
case err
of rInvalidFormatStr:
internalError(newPartialConfigRef(), "ropes: invalid format string: " & msg)
of rCannotOpenFile:
rawMessage(newPartialConfigRef(), if useWarning: warnCannotOpenFile else: errCannotOpenFile, msg)
proc listWarnings*(conf: ConfigRef) = proc listWarnings*(conf: ConfigRef) =
msgWriteln(conf, "Warnings:") msgWriteln(conf, "Warnings:")
for warn in warnMin..warnMax: for warn in warnMin..warnMax:

View file

@ -72,15 +72,6 @@ type
RopeSeq* = seq[Rope] RopeSeq* = seq[Rope]
RopesError* = enum
rCannotOpenFile
rInvalidFormatStr
# implementation
var errorHandler*: proc(err: RopesError, msg: string, useWarning = false)
# avoid dependency on msgs.nim
proc len*(a: Rope): int = proc len*(a: Rope): int =
## the rope's length ## the rope's length
if a == nil: result = 0 if a == nil: result = 0
@ -204,13 +195,14 @@ proc writeRope*(f: File, r: Rope) =
## writes a rope to a file. ## writes a rope to a file.
for s in leaves(r): write(f, s) for s in leaves(r): write(f, s)
proc writeRope*(head: Rope, filename: string, useWarning = false) = proc writeRope*(head: Rope, filename: string): bool =
var f: File var f: File
if open(f, filename, fmWrite): if open(f, filename, fmWrite):
if head != nil: writeRope(f, head) if head != nil: writeRope(f, head)
close(f) close(f)
result = true
else: else:
errorHandler(rCannotOpenFile, filename, useWarning) result = false
proc `$`*(r: Rope): string = proc `$`*(r: Rope): string =
## converts a rope back to a string. ## converts a rope back to a string.
@ -254,7 +246,7 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
if i >= frmt.len or frmt[i] notin {'0'..'9'}: break if i >= frmt.len or frmt[i] notin {'0'..'9'}: break
num = j num = j
if j > high(args) + 1: if j > high(args) + 1:
errorHandler(rInvalidFormatStr, $(j)) doAssert false, "invalid format string: " & frmt
else: else:
add(result, args[j-1]) add(result, args[j-1])
of '{': of '{':
@ -265,10 +257,11 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
inc(i) inc(i)
num = j num = j
if frmt[i] == '}': inc(i) if frmt[i] == '}': inc(i)
else: errorHandler(rInvalidFormatStr, $(frmt[i])) else:
doAssert false, "invalid format string: " & frmt
if j > high(args) + 1: if j > high(args) + 1:
errorHandler(rInvalidFormatStr, $(j)) doAssert false, "invalid format string: " & frmt
else: else:
add(result, args[j-1]) add(result, args[j-1])
of 'n': of 'n':
@ -278,7 +271,7 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
add(result, rnl) add(result, rnl)
inc(i) inc(i)
else: else:
errorHandler(rInvalidFormatStr, $(frmt[i])) doAssert false, "invalid format string: " & frmt
var start = i var start = i
while i < length: while i < length:
if frmt[i] != '$': inc(i) if frmt[i] != '$': inc(i)
@ -350,7 +343,6 @@ proc equalsFile*(r: Rope, filename: string): bool =
proc writeRopeIfNotEqual*(r: Rope, filename: string): bool = proc writeRopeIfNotEqual*(r: Rope, filename: string): bool =
# returns true if overwritten # returns true if overwritten
if not equalsFile(r, filename): if not equalsFile(r, filename):
writeRope(r, filename) result = writeRope(r, filename)
result = true
else: else:
result = false result = false