Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,6 @@ proc processSwitch(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
expectNoArg(switch, arg, pass, info)
useNimNamespace = true
defineSymbol("cppCompileToNamespace")

else:
if strutils.find(switch, '.') >= 0: options.setConfigVar(switch, arg)
else: invalidCmdLineOption(pass, switch, info)
Expand Down
5 changes: 3 additions & 2 deletions compiler/evaltempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ proc evalTemplateArgs(n: PNode, s: PSym; fromHlo: bool): PNode =
for i in 1 .. genericParams:
result.addSon n.sons[givenRegularParams + i]

# to prevent endless recursion in template instantiation
const evalTemplateLimit* = 1000
var evalTemplateCounter* = 0
# to prevent endless recursion in templates instantiation

proc wrapInComesFrom*(info: TLineInfo; sym: PSym; res: PNode): PNode =
when true:
Expand All @@ -133,7 +134,7 @@ proc wrapInComesFrom*(info: TLineInfo; sym: PSym; res: PNode): PNode =

proc evalTemplate*(n: PNode, tmpl, genSymOwner: PSym; fromHlo=false): PNode =
inc(evalTemplateCounter)
if evalTemplateCounter > 100:
if evalTemplateCounter > evalTemplateLimit:
globalError(n.info, errTemplateInstantiationTooNested)
result = n

Expand Down
2 changes: 1 addition & 1 deletion compiler/hlo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ proc applyPatterns(c: PContext, n: PNode): PNode =
assert x.kind in {nkStmtList, nkCall}
# better be safe than sorry, so check evalTemplateCounter too:
inc(evalTemplateCounter)
if evalTemplateCounter > 100:
if evalTemplateCounter > evalTemplateLimit:
globalError(n.info, errTemplateInstantiationTooNested)
# deactivate this pattern:
c.patterns[i] = nil
Expand Down
6 changes: 4 additions & 2 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ type
errNoReturnTypeDeclared,
errNoCommand, errInvalidCommandX, errXOnlyAtModuleScope,
errXNeedsParamObjectType,
errTemplateInstantiationTooNested, errInstantiationFrom,
errTemplateInstantiationTooNested, errMacroInstantiationTooNested,
errInstantiationFrom,
errInvalidIndexValueForTuple, errCommandExpectsFilename,
errMainModuleMustBeSpecified,
errXExpected,
Expand Down Expand Up @@ -329,7 +330,8 @@ const
errInvalidCommandX: "invalid command: \'$1\'",
errXOnlyAtModuleScope: "\'$1\' is only allowed at top level",
errXNeedsParamObjectType: "'$1' needs a parameter that has an object type",
errTemplateInstantiationTooNested: "template/macro instantiation too nested",
errTemplateInstantiationTooNested: "template instantiation too nested, try --evalTemplateLimit:N",
errMacroInstantiationTooNested: "macro instantiation too nested, try --evalMacroLimit:N",
errInstantiationFrom: "template/generic instantiation from here",
errInvalidIndexValueForTuple: "invalid index value for tuple subscript",
errCommandExpectsFilename: "command expects a filename argument",
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ proc semAfterMacroCall(c: PContext, call, macroResult: PNode,
## reassigned, and binding the unbound identifiers that the macro output
## contains.
inc(evalTemplateCounter)
if evalTemplateCounter > 100:
if evalTemplateCounter > evalTemplateLimit:
globalError(s.info, errTemplateInstantiationTooNested)
c.friendModules.add(s.owner.getModule)

Expand Down
6 changes: 4 additions & 2 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1729,14 +1729,16 @@ iterator genericParamsInMacroCall*(macroSym: PSym, call: PNode): (PSym, PNode) =
let posInCall = macroSym.typ.len + i
yield (genericParam, call[posInCall])

# to prevent endless recursion in macro instantiation
const evalMacroLimit = 1000
var evalMacroCounter: int

proc evalMacroCall*(module: PSym; cache: IdentCache, n, nOrig: PNode,
sym: PSym): PNode =
# XXX globalError() is ugly here, but I don't know a better solution for now
inc(evalMacroCounter)
if evalMacroCounter > 100:
globalError(n.info, errTemplateInstantiationTooNested)
if evalMacroCounter > evalMacroLimit:
globalError(n.info, errMacroInstantiationTooNested)

# immediate macros can bypass any type and arity checking so we check the
# arity here too:
Expand Down
2 changes: 1 addition & 1 deletion doc/advopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Advanced options:
--NimblePath:PATH add a path for Nimble support
--noNimblePath deactivate the Nimble path
--noCppExceptions use default exception handling with C++ backend
--cppCompileToNamespace use namespace "Nim" for the generated C++ code
--cppCompileToNamespace use namespace "Nim" for the generated C++ code
--excludePath:PATH exclude a path from the list of search paths
--dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL
has no effect and can be statically linked instead;
Expand Down