Skip to content

Commit

Permalink
Fix #3079: Replace parameter names that consist of only whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Sep 9, 2023
1 parent 0c2e4b1 commit 0fc0034
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
5 changes: 2 additions & 3 deletions ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
using ICSharpCode.Decompiler.CSharp.Transforms;
using ICSharpCode.Decompiler.DebugInfo;
using ICSharpCode.Decompiler.Disassembler;
Expand Down Expand Up @@ -1270,9 +1269,9 @@ void FixParameterNames(EntityDeclaration entity)
int i = 0;
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;
}
i++;
Expand Down
3 changes: 2 additions & 1 deletion ICSharpCode.Decompiler/CSharp/CallBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.IL.Transforms;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
Expand Down Expand Up @@ -781,7 +782,7 @@ private ArgumentList BuildArgumentList(ExpectedTargetDetails expectedTargetDetai
argumentNames = new string[method.Parameters.Count];
}
parameter = method.Parameters[argumentToParameterMap[i]];
if (argumentNames != null)
if (argumentNames != null && AssignVariableNames.IsValidName(parameter.Name))
{
argumentNames[arguments.Count] = parameter.Name;
}
Expand Down
3 changes: 1 addition & 2 deletions ICSharpCode.Decompiler/IL/ILReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#nullable enable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand Down Expand Up @@ -334,7 +333,7 @@ ILVariable CreateILVariable(int index, IType parameterType, string name)
Debug.Assert(ilVar.StoreCount == 1); // count the initial store when the method is called with an argument
if (index < 0)
ilVar.Name = "this";
else if (string.IsNullOrEmpty(name))
else if (string.IsNullOrWhiteSpace(name))
ilVar.Name = "P_" + index;
else
ilVar.Name = name;
Expand Down
8 changes: 5 additions & 3 deletions ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ void PerformAssignment(ILFunction function)
{
switch (v.Kind)
{
case VariableKind.Parameter: // ignore
case VariableKind.Parameter:
// Parameter names are handled in ILReader.CreateILVariable
// and CSharpDecompiler.FixParameterNames
break;
case VariableKind.InitializerTarget: // keep generated names
AddExistingName(reservedVariableNames, v.Name);
Expand Down Expand Up @@ -326,9 +328,9 @@ bool ConflictWithLocal(ILVariable v)
return false;
}

static bool IsValidName(string varName)
internal static bool IsValidName(string varName)
{
if (string.IsNullOrEmpty(varName))
if (string.IsNullOrWhiteSpace(varName))
return false;
if (!(char.IsLetter(varName[0]) || varName[0] == '_'))
return false;
Expand Down

0 comments on commit 0fc0034

Please sign in to comment.