Browse Source

Fix #3278: Missing variable declaration in nested for-loop after many other loops

pull/3283/head
Siegfried Pammer 10 months ago
parent
commit
966b99a7f9
  1. 75
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNaming.cs
  2. 6
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

75
ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNaming.cs

@ -88,5 +88,80 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -88,5 +88,80 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
#endif
}
private static void NestedForLoopTest2()
{
for (int i = 0; i < 10; i++)
{
Nop(i);
}
#if EXPECTED_OUTPUT && !(LEGACY_CSC && !OPT)
for (int j = 0; j < 10; j++)
{
Nop(j);
}
for (int k = 0; k < 10; k++)
{
Nop(k);
}
for (int l = 0; l < 10; l++)
{
Nop(l);
}
for (int m = 0; m < 10; m++)
{
for (int n = 0; n < 10; n++)
{
Nop(n);
}
}
for (int num = 0; num < 10; num++)
{
for (int num2 = 0; num2 < 10; num2++)
{
Nop(num2);
}
}
#else
for (int i = 0; i < 10; i++)
{
Nop(i);
}
for (int i = 0; i < 10; i++)
{
Nop(i);
}
for (int i = 0; i < 10; i++)
{
Nop(i);
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Nop(j);
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Nop(j);
}
}
#endif
}
private static void Nop(int v)
{
}
}
}

6
ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

@ -289,7 +289,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -289,7 +289,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms
string nameWithoutNumber = SplitName(newName, out int newIndex);
if (reservedVariableNames.TryGetValue(nameWithoutNumber, out int lastUsedIndex))
{
// name without number was already used
if (v.Type.IsKnownType(KnownTypeCode.Int32) && loopCounters.Contains(v))
{
// special case for loop counters,
@ -298,8 +297,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -298,8 +297,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
nameWithoutNumber = newName;
newIndex = 1;
}
else
}
if (reservedVariableNames.TryGetValue(nameWithoutNumber, out lastUsedIndex))
{
// name without number was already used
if (newIndex > lastUsedIndex)
{
// new index is larger than last, so we can use it
@ -312,7 +313,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -312,7 +313,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// resolve conflicts by appending the index to the new name:
newName = nameWithoutNumber + newIndex.ToString();
}
}
// update the last used index
reservedVariableNames[nameWithoutNumber] = newIndex;
variableMapping.Add(v, newName);

Loading…
Cancel
Save