Browse Source

#1572: Do not generate variable names that match C# keywords.

pull/3416/head
Siegfried Pammer 6 years ago
parent
commit
a599aae54d
  1. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs
  2. 16
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs
  3. 9
      ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs
  4. 6
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs

@ -4943,8 +4943,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -4943,8 +4943,8 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void Issue1779(int value)
{
CustomStruct2 @struct = GetStruct();
@struct.IntProp += value;
CustomStruct2 customStruct = GetStruct();
customStruct.IntProp += value;
}
}
}

16
ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs

@ -182,11 +182,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -182,11 +182,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
GetRef<ReadOnlyStruct>().Method();
// call on a copy, not the original ref:
NormalStruct @ref = GetRef<NormalStruct>();
@ref.Method();
NormalStruct normalStruct = GetRef<NormalStruct>();
normalStruct.Method();
ReadOnlyStruct ref2 = GetRef<ReadOnlyStruct>();
ref2.Method();
ReadOnlyStruct readOnlyStruct = GetRef<ReadOnlyStruct>();
readOnlyStruct.Method();
}
public void CallOnReadOnlyRefReturn()
@ -293,13 +293,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -293,13 +293,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void RefReassignment(ref NormalStruct s)
{
ref NormalStruct @ref = ref GetRef<NormalStruct>();
RefReassignment(ref @ref);
ref NormalStruct reference = ref GetRef<NormalStruct>();
RefReassignment(ref reference);
if (s.GetHashCode() == 0)
{
@ref = ref GetRef<NormalStruct>();
reference = ref GetRef<NormalStruct>();
}
RefReassignment(ref @ref.GetHashCode() == 4 ? ref @ref : ref s);
RefReassignment(ref reference.GetHashCode() == 4 ? ref reference : ref s);
}
public static void Main(string[] args)

9
ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
// Copyright (c) 2010-2020 AlphaSierraPapa for the SharpDevelop Team
// Copyright (c) 2010-2020 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
@ -426,8 +426,9 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -426,8 +426,9 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
/// <summary>
/// Determines whether the specified identifier is a keyword in the given context.
/// If <paramref name="context"/> is <see langword="null" /> all keywords are treated as unconditional.
/// </summary>
public static bool IsKeyword(string identifier, AstNode context)
public static bool IsKeyword(string identifier, AstNode context = null)
{
// only 2-10 char lower-case identifiers can be keywords
if (identifier.Length > maxKeywordLength || identifier.Length < 2 || identifier[0] < 'a')
@ -440,10 +441,12 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -440,10 +441,12 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
}
if (queryKeywords.Contains(identifier))
{
return context.Ancestors.Any(ancestor => ancestor is QueryExpression);
return context == null || context.Ancestors.Any(ancestor => ancestor is QueryExpression);
}
if (identifier == "await")
{
if (context == null)
return true;
foreach (AstNode ancestor in context.Ancestors)
{
// with lambdas/anonymous methods,

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

@ -657,8 +657,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -657,8 +657,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (name.Length == 0)
return "obj";
else
return char.ToLower(name[0]) + name.Substring(1);
string lowerCaseName = char.ToLower(name[0]) + name.Substring(1);
if (CSharp.OutputVisitor.CSharpOutputVisitor.IsKeyword(lowerCaseName))
return null;
return lowerCaseName;
}
internal static IType GuessType(IType variableType, ILInstruction inst, ILTransformContext context)

Loading…
Cancel
Save