From a599aae54d19557e388971b4a56ff0444aeefed0 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 4 Oct 2019 10:35:35 +0200 Subject: [PATCH] #1572: Do not generate variable names that match C# keywords. --- .../TestCases/Pretty/CompoundAssignmentTest.cs | 4 ++-- .../TestCases/Pretty/RefLocalsAndReturns.cs | 16 ++++++++-------- .../CSharp/OutputVisitor/CSharpOutputVisitor.cs | 9 ++++++--- .../IL/Transforms/AssignVariableNames.cs | 6 ++++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs index b0cb624b6..b133ac078 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs @@ -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; } } } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs index 7a65ff10a..192503f48 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs @@ -182,11 +182,11 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty GetRef().Method(); // call on a copy, not the original ref: - NormalStruct @ref = GetRef(); - @ref.Method(); + NormalStruct normalStruct = GetRef(); + normalStruct.Method(); - ReadOnlyStruct ref2 = GetRef(); - ref2.Method(); + ReadOnlyStruct readOnlyStruct = GetRef(); + readOnlyStruct.Method(); } public void CallOnReadOnlyRefReturn() @@ -293,13 +293,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty public void RefReassignment(ref NormalStruct s) { - ref NormalStruct @ref = ref GetRef(); - RefReassignment(ref @ref); + ref NormalStruct reference = ref GetRef(); + RefReassignment(ref reference); if (s.GetHashCode() == 0) { - @ref = ref GetRef(); + reference = ref GetRef(); } - RefReassignment(ref @ref.GetHashCode() == 4 ? ref @ref : ref s); + RefReassignment(ref reference.GetHashCode() == 4 ? ref reference : ref s); } public static void Main(string[] args) diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs index a462ccb9f..a4f448d2d 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs +++ b/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 // software and associated documentation files (the "Software"), to deal in the Software @@ -426,8 +426,9 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor /// /// Determines whether the specified identifier is a keyword in the given context. + /// If is all keywords are treated as unconditional. /// - 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 } 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, diff --git a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs index bee806143..c3ce39109 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs @@ -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)