From 074aa45acf86bcc76c2535d4c50d4f3254c3df38 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 27 Aug 2017 19:06:10 +0200 Subject: [PATCH] Add HasGeneratedName flag --- ICSharpCode.Decompiler/IL/ILReader.cs | 11 ++++++--- ICSharpCode.Decompiler/IL/ILVariable.cs | 2 ++ .../IL/Instructions/ILFunction.cs | 3 ++- .../IL/Transforms/AssignVariableNames.cs | 24 ++++++++----------- .../IL/Transforms/SplitVariables.cs | 1 + 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 76321e2d3..4e551c45c 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -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; } diff --git a/ICSharpCode.Decompiler/IL/ILVariable.cs b/ICSharpCode.Decompiler/IL/ILVariable.cs index 29c311f79..42414186b 100644 --- a/ICSharpCode.Decompiler/IL/ILVariable.cs +++ b/ICSharpCode.Decompiler/IL/ILVariable.cs @@ -91,6 +91,8 @@ namespace ICSharpCode.Decompiler.IL public string Name { get; set; } + public bool HasGeneratedName { get; set; } + /// /// Gets the function in which this variable is declared. /// diff --git a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs index f076d2e79..c9d4d9a9f 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs @@ -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 throw new NotSupportedException(); } name += index; + variable.HasGeneratedName = true; } - var variable = new ILVariable(kind, type, index); variable.Name = name; Variables.Add(variable); return variable; diff --git a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs index da549e2db..2e676a2d7 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs @@ -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(); + foreach (var p in function.Descendants.OfType().Select(f => f.Method).SelectMany(m => m.Parameters)) + AddExistingName(p.Name); foreach (ILFunction f in function.Descendants.OfType().Reverse()) { PerformAssignment(f); } @@ -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)) { - // 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 { + 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(v.Name); } break; } diff --git a/ICSharpCode.Decompiler/IL/Transforms/SplitVariables.cs b/ICSharpCode.Decompiler/IL/Transforms/SplitVariables.cs index 69b8a318c..6ab102915 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/SplitVariables.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/SplitVariables.cs @@ -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);