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
Original file line number Diff line number Diff line change
Expand Up @@ -2620,7 +2620,15 @@ private bool doProcessTranslates(IList<XSharpToken> line, out IList<XSharpToken>
var rule = _transRules.FindMatchingRule(temp, out var matchInfo);
if (rule != null)
{
var prevTemp = temp;
temp = doReplace(temp, rule, matchInfo);
if (isResultUnchanged(prevTemp, temp))
{
// the replacement did not change the tokens, so stop to prevent an endless loop
result.AddRange(temp);
temp.Clear();
break;
}
if (usedRules.HasRecursion(rule, temp))
{
// duplicate, so exit now
Expand Down Expand Up @@ -2686,7 +2694,13 @@ private bool doProcessCommands(IList<XSharpToken> line, out IList<XSharpToken> r
// nothing to do, so exit. Leave changed the way it is. This does not have to be the first iteration
break;
}
var prevResult = result;
result = doReplace(result, rule, matchInfo);
if (isResultUnchanged(prevResult, result))
{
// the replacement did not change the tokens, so stop to prevent an endless loop
break;
}
if (usedRules.HasRecursion(rule, result))
{
// duplicate so exit now
Expand Down Expand Up @@ -2796,5 +2810,19 @@ private List<XSharpToken> doReplace(IList<XSharpToken> line, PPRule rule, PPMatc
#endif
return result;
}

private static bool isResultUnchanged(IList<XSharpToken> input, IList<XSharpToken> output)
{
if (input.Count != output.Count)
return false;
for (int i = 0; i < input.Count; i++)
{
if (!string.Equals(input[i].Text, output[i].Text, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}
}
}
28 changes: 28 additions & 0 deletions src/Runtime/MacroCompiler/Preprocessor/XSharpPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,15 @@ private bool doProcessTranslates(IList<XSharpToken> line, out IList<XSharpToken>
var rule = _transRules.FindMatchingRule(temp, out var matchInfo);
if (rule != null)
{
var prevTemp = temp;
temp = doReplace(temp, rule, matchInfo);
if (isResultUnchanged(prevTemp, temp))
{
// the replacement did not change the tokens, so stop to prevent an endless loop
result.AddRange(temp);
temp.Clear();
break;
}
if (usedRules.HasRecursion(rule, temp))
{
// duplicate, so exit now
Expand Down Expand Up @@ -2606,7 +2614,13 @@ private bool doProcessCommands(IList<XSharpToken> line, out IList<XSharpToken> r
// nothing to do, so exit. Leave changed the way it is. This does not have to be the first iteration
break;
}
var prevResult = result;
result = doReplace(result, rule, matchInfo);
if (isResultUnchanged(prevResult, result))
{
// the replacement did not change the tokens, so stop to prevent an endless loop
break;
}
if (usedRules.HasRecursion(rule, result))
{
// duplicate so exit now
Expand Down Expand Up @@ -2716,5 +2730,19 @@ private List<XSharpToken> doReplace(IList<XSharpToken> line, PPRule rule, PPMatc
#endif
return result;
}

private static bool isResultUnchanged(IList<XSharpToken> input, IList<XSharpToken> output)
{
if (input.Count != output.Count)
return false;
for (int i = 0; i < input.Count; i++)
{
if (!string.Equals(input[i].Text, output[i].Text, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}
}
}