Browse Source

Add transform to fix name collisions.

This allows us to decompile events to valid code without having to match the compiler-generated event pattern.
pull/728/head
Daniel Grunwald 9 years ago
parent
commit
b3974c48b3
  1. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 3
      ICSharpCode.Decompiler/CSharp/Transforms/EscapeInvalidIdentifiers.cs
  3. 80
      ICSharpCode.Decompiler/CSharp/Transforms/FixNameCollisions.cs
  4. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -69,6 +69,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -69,6 +69,7 @@ namespace ICSharpCode.Decompiler.CSharp
new ConvertConstructorCallIntoInitializer(), // must run after DeclareVariables
new DecimalConstantTransform(),
new IntroduceUsingDeclarations(),
new FixNameCollisions(),
//new IntroduceExtensionMethods(context), // must run after IntroduceUsingDeclarations
//new IntroduceQueryExpressions(context), // must run after IntroduceExtensionMethods
//new CombineQueryExpressions(context),

3
ICSharpCode.Decompiler/CSharp/Transforms/EscapeInvalidIdentifiers.cs

@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
@ -27,7 +28,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -27,7 +28,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
/// <remarks>
/// This transform is not enabled by default.
/// </remarks>
public class EscapeInvalidIdentifiers : IAstTransform
public class EscapeInvalidIdentifiers : IAstTransform
{
bool IsValid(char ch)
{

80
ICSharpCode.Decompiler/CSharp/Transforms/FixNameCollisions.cs

@ -0,0 +1,80 @@ @@ -0,0 +1,80 @@
// Copyright (c) 2016 Daniel Grunwald
//
// 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
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.Decompiler.CSharp.Transforms
{
/// <summary>
/// Rename entities to solve name collisions that make the code uncompilable.
/// </summary>
/// <remarks>
/// Currently, we only rename private fields that collide with property or event names.
/// This helps especially with compiler-generated events that were not detected as a pattern.
/// </remarks>
public class FixNameCollisions : IAstTransform
{
public void Run(AstNode rootNode, TransformContext context)
{
var renamedSymbols = new Dictionary<ISymbol, string>();
foreach (var typeDecl in rootNode.DescendantsAndSelf.OfType<TypeDeclaration>()) {
var memberNames = typeDecl.Members.Select(m => m.Name).ToHashSet();
// memberNames does not include fields or non-custom events because those
// don't have a single name, but a list of VariableInitializers.
foreach (var fieldDecl in typeDecl.Members.OfType<FieldDeclaration>()) {
if (fieldDecl.Variables.Count != 1)
continue;
string oldName = fieldDecl.Variables.Single().Name;
if (memberNames.Contains(oldName)) {
string newName = PickNewName(memberNames, oldName);
ISymbol symbol = fieldDecl.GetSymbol();
if (symbol != null) {
fieldDecl.Variables.Single().Name = newName;
renamedSymbols[symbol] = newName;
}
}
}
}
foreach (var node in rootNode.DescendantsAndSelf) {
if (node is IdentifierExpression || node is MemberReferenceExpression) {
ISymbol symbol = node.GetSymbol();
string newName;
if (symbol != null && renamedSymbols.TryGetValue(symbol, out newName)) {
node.GetChildByRole(Roles.Identifier).Name = newName;
}
}
}
}
string PickNewName(ISet<string> memberNames, string name)
{
if (!memberNames.Contains("m_" + name))
return "m_" + name;
for (int num = 2;; num++) {
string newName = name + num;
if (!memberNames.Contains(newName))
return newName;
}
}
}
}

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -66,6 +66,7 @@ @@ -66,6 +66,7 @@
<Compile Include="CSharp\DecompilerSettings.cs" />
<Compile Include="CSharp\Transforms\DecimalConstantTransform.cs" />
<Compile Include="CSharp\Transforms\EscapeInvalidIdentifiers.cs" />
<Compile Include="CSharp\Transforms\FixNameCollisions.cs" />
<Compile Include="CSharp\Transforms\IntroduceUsingDeclarations.cs" />
<Compile Include="CSharp\Transforms\TransformContext.cs" />
<Compile Include="CSharp\TranslatedExpression.cs" />

Loading…
Cancel
Save