Browse Source

Fix #3113: Remove GetAlternativeName and instead reuse existing names, if there are no conflicts.

pull/3138/head
Siegfried Pammer 2 years ago
parent
commit
566af5c2fb
  1. 62
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

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

@ -240,9 +240,13 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
else else
{ {
// use the name from the debug symbols // use the name from the debug symbols and update index appended to duplicates
// (but ensure we don't use the same name for two variables) string nameWithoutNumber = SplitName(v.Name, out int newIndex);
v.Name = GetAlternativeName(v.Name); if (!reservedVariableNames.TryGetValue(nameWithoutNumber, out int currentIndex))
{
currentIndex = 1;
}
reservedVariableNames[nameWithoutNumber] = Math.Max(newIndex, currentIndex);
} }
} }
} }
@ -274,7 +278,21 @@ namespace ICSharpCode.Decompiler.IL.Transforms
var newName = localFunction.Name; var newName = localFunction.Name;
if (newName == null) if (newName == null)
{ {
newName = GetAlternativeName("f"); string nameWithoutNumber = "f";
if (!reservedVariableNames.TryGetValue(nameWithoutNumber, out int currentIndex))
{
currentIndex = 1;
}
int count = Math.Max(1, currentIndex) + 1;
reservedVariableNames[nameWithoutNumber] = count;
if (count > 1)
{
newName = nameWithoutNumber + count.ToString();
}
else
{
newName = nameWithoutNumber;
}
} }
localFunction.Name = newName; localFunction.Name = newName;
localFunction.ReducedMethod.Name = newName; localFunction.ReducedMethod.Name = newName;
@ -342,42 +360,6 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return true; return true;
} }
public string GetAlternativeName(string oldVariableName)
{
if (oldVariableName.Length == 1 && oldVariableName[0] >= 'i' && oldVariableName[0] <= maxLoopVariableName)
{
for (char c = 'i'; c <= maxLoopVariableName; c++)
{
if (!reservedVariableNames.ContainsKey(c.ToString()))
{
reservedVariableNames.Add(c.ToString(), 1);
return c.ToString();
}
}
}
string nameWithoutDigits = SplitName(oldVariableName, out int number);
if (!reservedVariableNames.ContainsKey(nameWithoutDigits))
{
reservedVariableNames.Add(nameWithoutDigits, number - 1);
}
int count = ++reservedVariableNames[nameWithoutDigits];
string nameWithDigits = nameWithoutDigits + count.ToString();
if (oldVariableName == nameWithDigits)
{
return oldVariableName;
}
if (count != 1)
{
return nameWithDigits;
}
else
{
return nameWithoutDigits;
}
}
HashSet<ILVariable> CollectLoopCounters(ILFunction function) HashSet<ILVariable> CollectLoopCounters(ILFunction function)
{ {
var loopCounters = new HashSet<ILVariable>(); var loopCounters = new HashSet<ILVariable>();

Loading…
Cancel
Save