Skip to content

Commit

Permalink
Fix #3272: Missing variable declarations in repeated nested for-loops
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Sep 13, 2024
1 parent 533a773 commit 58e993d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ async Task Run([CallerMemberName] string testName = null, AssemblerOptions asmOp
var decompiled = await Tester.DecompileCSharp(exeFile, decompilerSettings ?? Tester.GetSettings(cscOptions)).ConfigureAwait(false);

// 3. Compile
CodeAssert.FilesAreEqual(csFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).ToArray());
CodeAssert.FilesAreEqual(csFile, decompiled, Tester.GetPreprocessorSymbols(cscOptions).Append("EXPECTED_OUTPUT").ToArray());
Tester.RepeatOnIOError(() => File.Delete(decompiled));
}
}
Expand Down
28 changes: 28 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,33 @@ private void Issue1881()
#pragma warning restore CS0219
}
#endif

private static void NestedForLoopTest(int sizeX, int sizeY, int[] array)
{
for (int y = 0; y < sizeY; y++)
{
for (int x = 0; x < sizeX; x++)
{
array[y * sizeX + x] = 0;
}
}
#if !EXPECTED_OUTPUT || (LEGACY_CSC && !OPT)
for (int y = 0; y < sizeY; y++)
{
for (int x = 0; x < sizeX; x++)
{
array[y * sizeX + x] = 1;
}
}
#else
for (int i = 0; i < sizeY; i++)
{
for (int j = 0; j < sizeX; j++)
{
array[i * sizeX + j] = 1;
}
}
#endif
}
}
}
2 changes: 2 additions & 0 deletions ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ string AssignName(ILVariable v, Dictionary<ILVariable, string> variableMapping)
// special case for loop counters,
// we don't want them to be named i, i2, ..., but i, j, ...
newName = GenerateNameForVariable(v);
nameWithoutNumber = newName;
newIndex = 1;
}
else
{
Expand Down

0 comments on commit 58e993d

Please sign in to comment.