Browse Source

Add test cases for naming conflicts related to foreach, using and fixed variables. Remove unnecessary ConflictWithLocal check in AssignVariableNames.

pull/3243/head
Siegfried Pammer 1 year ago
parent
commit
c67d086e2f
  1. 54
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNamingWithoutSymbols.cs
  2. 12
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

54
ICSharpCode.Decompiler.Tests/TestCases/Pretty/VariableNamingWithoutSymbols.cs

@ -1,4 +1,7 @@ @@ -1,4 +1,7 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
using System;
using System.Collections.Generic;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class VariableNamingWithoutSymbols
{
@ -25,5 +28,54 @@ @@ -25,5 +28,54 @@
string text2 = c.Text;
#endif
}
private static IDisposable GetData()
{
return null;
}
private static void UseData(IDisposable data)
{
}
private static IEnumerable<int> GetItems()
{
throw null;
}
private static byte[] GetMemory()
{
throw null;
}
private static void Test(int item)
{
foreach (int item2 in GetItems())
{
Console.WriteLine(item2);
}
}
private static void Test(IDisposable data)
{
#if CS80
using IDisposable data2 = GetData();
UseData(data2);
#else
using (IDisposable data2 = GetData())
{
UseData(data2);
}
#endif
}
private unsafe static void Test(byte[] memory)
{
fixed (byte* memory2 = GetMemory())
{
Console.WriteLine(*memory2);
}
}
}
}

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

@ -233,7 +233,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -233,7 +233,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
void AssignName()
{
if (v.HasGeneratedName || !IsValidName(v.Name) || ConflictWithLocal(v))
if (v.HasGeneratedName || !IsValidName(v.Name))
{
// don't use the name from the debug symbols if it looks like a generated name
v.Name = null;
@ -337,16 +337,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -337,16 +337,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}
}
bool ConflictWithLocal(ILVariable v)
{
if (v.Kind == VariableKind.UsingLocal || v.Kind == VariableKind.ForeachLocal)
{
if (reservedVariableNames.ContainsKey(v.Name))
return true;
}
return false;
}
internal static bool IsValidName(string varName)
{
if (string.IsNullOrWhiteSpace(varName))

Loading…
Cancel
Save