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
public void Issue1779(int value) public void Issue1779(int value)
{ {
CustomStruct2 @struct = GetStruct(); CustomStruct2 customStruct = GetStruct();
@struct.IntProp += value; customStruct.IntProp += value;
} }
} }
} }

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

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

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

@ -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 // 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 // software and associated documentation files (the "Software"), to deal in the Software
@ -426,8 +426,9 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
/// <summary> /// <summary>
/// Determines whether the specified identifier is a keyword in the given context. /// 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> /// </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 // only 2-10 char lower-case identifiers can be keywords
if (identifier.Length > maxKeywordLength || identifier.Length < 2 || identifier[0] < 'a') if (identifier.Length > maxKeywordLength || identifier.Length < 2 || identifier[0] < 'a')
@ -440,10 +441,12 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
} }
if (queryKeywords.Contains(identifier)) 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 (identifier == "await")
{ {
if (context == null)
return true;
foreach (AstNode ancestor in context.Ancestors) foreach (AstNode ancestor in context.Ancestors)
{ {
// with lambdas/anonymous methods, // with lambdas/anonymous methods,

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

@ -657,8 +657,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (name.Length == 0) if (name.Length == 0)
return "obj"; return "obj";
else string lowerCaseName = char.ToLower(name[0]) + name.Substring(1);
return 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) internal static IType GuessType(IType variableType, ILInstruction inst, ILTransformContext context)

Loading…
Cancel
Save