Fixed stack overflow during execution of MacroEvaluationProgram #997
+128
−47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
During the migration of a large project (~1800 targets) to a new version of Xcode, we faced constant build system crashes.
The first crash occurred during recursive
lookupMacroDeclarationandlookupConditionParameterevaluation, which is being fixed in #992.A second crash appeared in multiple places but always reduced to
MacroEvaluationProgram.executeInContext. Evaluating the instructionevalNamedMacroand then callingvalue.expression.evaluate(context:resultBuilder:alwaysEvalAsString)blew the call stack.This PR converts that recursive evaluation of the value expression to a stack-based approach that can handle any depth.
Each frame on the stack represents an in-progress evaluation of a macro expression. When we need to evaluate a nested macro, instead of making a recursive call, we push the current state onto the stack and start evaluating the nested expression. When we complete an expression evaluation, we pop the stack and continue.
This fix, combined with the one in #992, resolves the issue described in https://forums.swift.org/t/xcode-15-3-xcbuildservice-crash-infinite-loop-when-building-ios-project/70643/7.