Browse Source

Fix #3079: Replace parameter names that consist of only whitespace

pull/3081/head
Siegfried Pammer 2 years ago
parent
commit
0fc003459f
  1. 5
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 3
      ICSharpCode.Decompiler/CSharp/CallBuilder.cs
  3. 3
      ICSharpCode.Decompiler/IL/ILReader.cs
  4. 8
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

5
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -30,7 +30,6 @@ using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp.OutputVisitor; using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.CSharp.Resolver; using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.Syntax; using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
using ICSharpCode.Decompiler.CSharp.Transforms; using ICSharpCode.Decompiler.CSharp.Transforms;
using ICSharpCode.Decompiler.DebugInfo; using ICSharpCode.Decompiler.DebugInfo;
using ICSharpCode.Decompiler.Disassembler; using ICSharpCode.Decompiler.Disassembler;
@ -1270,9 +1269,9 @@ namespace ICSharpCode.Decompiler.CSharp
int i = 0; int i = 0;
foreach (var parameter in entity.GetChildrenByRole(Roles.Parameter)) foreach (var parameter in entity.GetChildrenByRole(Roles.Parameter))
{ {
if (string.IsNullOrEmpty(parameter.Name) && !parameter.Type.IsArgList()) if (string.IsNullOrWhiteSpace(parameter.Name) && !parameter.Type.IsArgList())
{ {
// needs to be consistent with logic in ILReader.CreateILVarable(ParameterDefinition) // needs to be consistent with logic in ILReader.CreateILVarable
parameter.Name = "P_" + i; parameter.Name = "P_" + i;
} }
i++; i++;

3
ICSharpCode.Decompiler/CSharp/CallBuilder.cs

@ -26,6 +26,7 @@ using System.Text;
using ICSharpCode.Decompiler.CSharp.Resolver; using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.Syntax; using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.IL.Transforms;
using ICSharpCode.Decompiler.Semantics; using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.TypeSystem.Implementation;
@ -781,7 +782,7 @@ namespace ICSharpCode.Decompiler.CSharp
argumentNames = new string[method.Parameters.Count]; argumentNames = new string[method.Parameters.Count];
} }
parameter = method.Parameters[argumentToParameterMap[i]]; parameter = method.Parameters[argumentToParameterMap[i]];
if (argumentNames != null) if (argumentNames != null && AssignVariableNames.IsValidName(parameter.Name))
{ {
argumentNames[arguments.Count] = parameter.Name; argumentNames[arguments.Count] = parameter.Name;
} }

3
ICSharpCode.Decompiler/IL/ILReader.cs

@ -19,7 +19,6 @@
#nullable enable #nullable enable
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
@ -334,7 +333,7 @@ namespace ICSharpCode.Decompiler.IL
Debug.Assert(ilVar.StoreCount == 1); // count the initial store when the method is called with an argument Debug.Assert(ilVar.StoreCount == 1); // count the initial store when the method is called with an argument
if (index < 0) if (index < 0)
ilVar.Name = "this"; ilVar.Name = "this";
else if (string.IsNullOrEmpty(name)) else if (string.IsNullOrWhiteSpace(name))
ilVar.Name = "P_" + index; ilVar.Name = "P_" + index;
else else
ilVar.Name = name; ilVar.Name = name;

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

@ -202,7 +202,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
{ {
switch (v.Kind) switch (v.Kind)
{ {
case VariableKind.Parameter: // ignore case VariableKind.Parameter:
// Parameter names are handled in ILReader.CreateILVariable
// and CSharpDecompiler.FixParameterNames
break; break;
case VariableKind.InitializerTarget: // keep generated names case VariableKind.InitializerTarget: // keep generated names
AddExistingName(reservedVariableNames, v.Name); AddExistingName(reservedVariableNames, v.Name);
@ -326,9 +328,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false; return false;
} }
static bool IsValidName(string varName) internal static bool IsValidName(string varName)
{ {
if (string.IsNullOrEmpty(varName)) if (string.IsNullOrWhiteSpace(varName))
return false; return false;
if (!(char.IsLetter(varName[0]) || varName[0] == '_')) if (!(char.IsLetter(varName[0]) || varName[0] == '_'))
return false; return false;

Loading…
Cancel
Save