Browse Source

Add HasGeneratedName flag

pull/832/head
Siegfried Pammer 8 years ago
parent
commit
074aa45acf
  1. 11
      ICSharpCode.Decompiler/IL/ILReader.cs
  2. 2
      ICSharpCode.Decompiler/IL/ILVariable.cs
  3. 3
      ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs
  4. 16
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs
  5. 1
      ICSharpCode.Decompiler/IL/Transforms/SplitVariables.cs

11
ICSharpCode.Decompiler/IL/ILReader.cs

@ -128,10 +128,15 @@ namespace ICSharpCode.Decompiler.IL @@ -128,10 +128,15 @@ namespace ICSharpCode.Decompiler.IL
{
VariableKind kind = v.IsPinned ? VariableKind.PinnedLocal : VariableKind.Local;
ILVariable ilVar = new ILVariable(kind, typeSystem.Resolve(v.VariableType), v.Index);
if (!UseDebugSymbols || debugInfo == null || !debugInfo.TryGetName(v, out string name))
if (!UseDebugSymbols || debugInfo == null || !debugInfo.TryGetName(v, out string name)) {
ilVar.Name = "V_" + v.Index;
else
ilVar.Name = !string.IsNullOrWhiteSpace(name) ? name : "V_" + v.Index;
ilVar.HasGeneratedName = true;
} else if (string.IsNullOrWhiteSpace(name)) {
ilVar.Name = "V_" + v.Index;
ilVar.HasGeneratedName = true;
} else {
ilVar.Name = name;
}
return ilVar;
}

2
ICSharpCode.Decompiler/IL/ILVariable.cs

@ -91,6 +91,8 @@ namespace ICSharpCode.Decompiler.IL @@ -91,6 +91,8 @@ namespace ICSharpCode.Decompiler.IL
public string Name { get; set; }
public bool HasGeneratedName { get; set; }
/// <summary>
/// Gets the function in which this variable is declared.
/// </summary>

3
ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs

@ -127,6 +127,7 @@ namespace ICSharpCode.Decompiler.IL @@ -127,6 +127,7 @@ namespace ICSharpCode.Decompiler.IL
public ILVariable RegisterVariable(VariableKind kind, IType type, string name = null)
{
int index = Variables.Where(v => v.Kind == kind).MaxOrDefault(v => v.Index, -1) + 1;
var variable = new ILVariable(kind, type, index);
if (string.IsNullOrWhiteSpace(name)) {
switch (kind) {
case VariableKind.Local:
@ -145,8 +146,8 @@ namespace ICSharpCode.Decompiler.IL @@ -145,8 +146,8 @@ namespace ICSharpCode.Decompiler.IL
throw new NotSupportedException();
}
name += index;
variable.HasGeneratedName = true;
}
var variable = new ILVariable(kind, type, index);
variable.Name = name;
Variables.Add(variable);
return variable;

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

@ -59,6 +59,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -59,6 +59,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
this.context = context;
currentFieldNames = function.Method.DeclaringType.Fields.Select(f => f.Name).ToArray();
reservedVariableNames = new Dictionary<string, int>();
foreach (var p in function.Descendants.OfType<ILFunction>().Select(f => f.Method).SelectMany(m => m.Parameters))
AddExistingName(p.Name);
foreach (ILFunction f in function.Descendants.OfType<ILFunction>().Reverse()) {
PerformAssignment(f);
}
@ -66,27 +68,21 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -66,27 +68,21 @@ namespace ICSharpCode.Decompiler.IL.Transforms
void PerformAssignment(ILFunction function)
{
foreach (var p in function.Method.Parameters)
AddExistingName(p.Name);
foreach (var v in function.Variables) {
switch (v.Kind) {
case VariableKind.Parameter:
case VariableKind.Parameter: // ignore
break;
case VariableKind.StackSlot: // keep generated names
AddExistingName(v.Name);
break;
default:
string varName = v.Name;
if (context.Settings.UseDebugSymbols) {
if (string.IsNullOrEmpty(varName) || varName.StartsWith("V_", StringComparison.Ordinal) || !IsValidName(varName)) {
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;
} else {
// use the name from the debug symbols
// (but ensure we don't use the same name for two variables)
v.Name = GetAlternativeName(varName);
}
} else {
v.Name = null;
v.Name = GetAlternativeName(v.Name);
}
break;
}

1
ICSharpCode.Decompiler/IL/Transforms/SplitVariables.cs

@ -97,6 +97,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -97,6 +97,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (!newVariables.TryGetValue(representative, out v)) {
v = new ILVariable(inst.Variable.Kind, inst.Variable.Type, inst.Variable.StackType, inst.Variable.Index);
v.Name = inst.Variable.Name;
v.HasGeneratedName = inst.Variable.HasGeneratedName;
v.StateMachineField = inst.Variable.StateMachineField;
v.HasInitialValue = false; // we'll set HasInitialValue when we encounter an uninit load
newVariables.Add(representative, v);

Loading…
Cancel
Save