Skip to content

Commit 4de9775

Browse files
Fix #3079: Replace parameter names that consist of only whitespace
1 parent d25980b commit 4de9775

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
3131
using ICSharpCode.Decompiler.CSharp.Resolver;
3232
using ICSharpCode.Decompiler.CSharp.Syntax;
33-
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
3433
using ICSharpCode.Decompiler.CSharp.Transforms;
3534
using ICSharpCode.Decompiler.DebugInfo;
3635
using ICSharpCode.Decompiler.Disassembler;
@@ -1270,9 +1269,9 @@ void FixParameterNames(EntityDeclaration entity)
12701269
int i = 0;
12711270
foreach (var parameter in entity.GetChildrenByRole(Roles.Parameter))
12721271
{
1273-
if (string.IsNullOrEmpty(parameter.Name) && !parameter.Type.IsArgList())
1272+
if (string.IsNullOrWhiteSpace(parameter.Name) && !parameter.Type.IsArgList())
12741273
{
1275-
// needs to be consistent with logic in ILReader.CreateILVarable(ParameterDefinition)
1274+
// needs to be consistent with logic in ILReader.CreateILVarable
12761275
parameter.Name = "P_" + i;
12771276
}
12781277
i++;

ICSharpCode.Decompiler/CSharp/CallBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using ICSharpCode.Decompiler.CSharp.Resolver;
2727
using ICSharpCode.Decompiler.CSharp.Syntax;
2828
using ICSharpCode.Decompiler.IL;
29+
using ICSharpCode.Decompiler.IL.Transforms;
2930
using ICSharpCode.Decompiler.Semantics;
3031
using ICSharpCode.Decompiler.TypeSystem;
3132
using ICSharpCode.Decompiler.TypeSystem.Implementation;
@@ -781,7 +782,7 @@ private ArgumentList BuildArgumentList(ExpectedTargetDetails expectedTargetDetai
781782
argumentNames = new string[method.Parameters.Count];
782783
}
783784
parameter = method.Parameters[argumentToParameterMap[i]];
784-
if (argumentNames != null)
785+
if (argumentNames != null && AssignVariableNames.IsValidName(parameter.Name))
785786
{
786787
argumentNames[arguments.Count] = parameter.Name;
787788
}

ICSharpCode.Decompiler/IL/ILReader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#nullable enable
2020

2121
using System;
22-
using System.Collections;
2322
using System.Collections.Generic;
2423
using System.Collections.Immutable;
2524
using System.Diagnostics;
@@ -334,7 +333,7 @@ ILVariable CreateILVariable(int index, IType parameterType, string name)
334333
Debug.Assert(ilVar.StoreCount == 1); // count the initial store when the method is called with an argument
335334
if (index < 0)
336335
ilVar.Name = "this";
337-
else if (string.IsNullOrEmpty(name))
336+
else if (string.IsNullOrWhiteSpace(name))
338337
ilVar.Name = "P_" + index;
339338
else
340339
ilVar.Name = name;

ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ void PerformAssignment(ILFunction function)
202202
{
203203
switch (v.Kind)
204204
{
205-
case VariableKind.Parameter: // ignore
205+
case VariableKind.Parameter:
206+
// Parameter names are handled in ILReader.CreateILVariable
207+
// and CSharpDecompiler.FixParameterNames
206208
break;
207209
case VariableKind.InitializerTarget: // keep generated names
208210
AddExistingName(reservedVariableNames, v.Name);
@@ -326,9 +328,9 @@ bool ConflictWithLocal(ILVariable v)
326328
return false;
327329
}
328330

329-
static bool IsValidName(string varName)
331+
internal static bool IsValidName(string varName)
330332
{
331-
if (string.IsNullOrEmpty(varName))
333+
if (string.IsNullOrWhiteSpace(varName))
332334
return false;
333335
if (!(char.IsLetter(varName[0]) || varName[0] == '_'))
334336
return false;

0 commit comments

Comments
 (0)