Browse Source

Improve GenerateForeachVariableName.

pull/871/head
Siegfried Pammer 8 years ago
parent
commit
27aa58a532
  1. 4
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs
  2. 39
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

4
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -311,7 +311,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -311,7 +311,7 @@ namespace ICSharpCode.Decompiler.CSharp
if (needsUninlining) {
itemVariable = currentFunction.RegisterVariable(
VariableKind.ForeachLocal, type,
AssignVariableNames.GenerateVariableName(currentFunction, collectionExpr.Annotation<ILInstruction>(), "item")
AssignVariableNames.GenerateForeachVariableName(currentFunction, collectionExpr.Annotation<ILInstruction>())
);
instToReplace.ReplaceWith(new LdLoc(itemVariable));
body.Instructions.Insert(0, new StLoc(itemVariable, instToReplace));
@ -320,7 +320,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -320,7 +320,7 @@ namespace ICSharpCode.Decompiler.CSharp
return null;
itemVariable.Type = type;
itemVariable.Kind = VariableKind.ForeachLocal;
itemVariable.Name = AssignVariableNames.GenerateVariableName(currentFunction, collectionExpr.Annotation<ILInstruction>(), "item", itemVariable);
itemVariable.Name = AssignVariableNames.GenerateForeachVariableName(currentFunction, collectionExpr.Annotation<ILInstruction>(), itemVariable);
}
var whileLoop = (WhileStatement)ConvertAsBlock(inst.Body).First();
BlockStatement foreachBody = (BlockStatement)whileLoop.EmbeddedStatement.Detach();

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

@ -335,19 +335,31 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -335,19 +335,31 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return char.ToLower(name[0]) + name.Substring(1);
}
internal static string GenerateVariableName(ILFunction currentFunction, ILInstruction valueContext, string fallback, ILVariable existingVariable = null)
internal static string GenerateForeachVariableName(ILFunction function, ILInstruction valueContext, ILVariable existingVariable = null)
{
if (currentFunction == null)
throw new ArgumentNullException(nameof(currentFunction));
if (function == null)
throw new ArgumentNullException(nameof(function));
var reservedVariableNames = new Dictionary<string, int>();
foreach (var v in currentFunction.Descendants.OfType<ILFunction>().SelectMany(m => m.Variables)) {
foreach (var v in function.Descendants.OfType<ILFunction>().SelectMany(m => m.Variables)) {
if (v != existingVariable)
AddExistingName(reservedVariableNames, v.Name);
}
var proposedName = GetNameFromInstruction(valueContext);
foreach (var f in function.Method.DeclaringType.Fields.Select(f => f.Name))
AddExistingName(reservedVariableNames, f);
if (string.IsNullOrEmpty(proposedName)) {
proposedName = fallback;
string baseName = GetNameFromInstruction(valueContext);
string proposedName = "item";
if (!string.IsNullOrEmpty(baseName)) {
if (!IsPlural(baseName, ref proposedName)) {
if (baseName.Length > 4 && baseName.EndsWith("List", StringComparison.Ordinal)) {
proposedName = baseName.Substring(0, baseName.Length - 4);
} else if (baseName.Equals("list", StringComparison.OrdinalIgnoreCase)) {
proposedName = "item";
} else if (baseName.EndsWith("children", StringComparison.OrdinalIgnoreCase)) {
proposedName = baseName.Remove(baseName.Length - 3);
}
}
}
// remove any numbers from the proposed name
@ -363,5 +375,18 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -363,5 +375,18 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return proposedName;
}
}
private static bool IsPlural(string baseName, ref string proposedName)
{
if (baseName.EndsWith("ies", StringComparison.OrdinalIgnoreCase) && baseName.Length > 3) {
proposedName = baseName.Remove(baseName.Length - 3) + "y";
return true;
}
if (baseName.EndsWith("s", StringComparison.OrdinalIgnoreCase) && baseName.Length > 1) {
proposedName = baseName.Remove(baseName.Length - 1);
return true;
}
return false;
}
}
}

Loading…
Cancel
Save