Browse Source

simplified switch sections by removing redundant blocks.

pull/275/head
Artur Zgodziński 14 years ago
parent
commit
f8ee8ae828
  1. 27
      ICSharpCode.Decompiler/Ast/Transforms/FlattenSwitchBlocks.cs
  2. 1
      ICSharpCode.Decompiler/Ast/Transforms/TransformationPipeline.cs
  3. 8
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  4. 14
      NRefactory/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs

27
ICSharpCode.Decompiler/Ast/Transforms/FlattenSwitchBlocks.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.CSharp;
namespace ICSharpCode.Decompiler.Ast.Transforms
{
class FlattenSwitchBlocks : IAstTransform
{
public void Run(AstNode compilationUnit)
{
foreach (var switchSection in compilationUnit.Descendants.OfType<SwitchSection>())
{
if (switchSection.Statements.Count != 1)
continue;
var blockStatement = switchSection.Statements.First() as BlockStatement;
if (blockStatement == null || blockStatement.Statements.Any(st => st is VariableDeclarationStatement))
continue;
blockStatement.Remove();
blockStatement.Statements.MoveTo(switchSection.Statements);
}
}
}
}

1
ICSharpCode.Decompiler/Ast/Transforms/TransformationPipeline.cs

@ -45,6 +45,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms @@ -45,6 +45,7 @@ namespace ICSharpCode.Decompiler.Ast.Transforms
new IntroduceExtensionMethods(context), // must run after IntroduceUsingDeclarations
new IntroduceQueryExpressions(context), // must run after IntroduceExtensionMethods
new CombineQueryExpressions(context),
new FlattenSwitchBlocks(),
};
}

8
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -64,6 +64,7 @@ @@ -64,6 +64,7 @@
<Compile Include="Ast\Transforms\DecimalConstantTransform.cs" />
<Compile Include="Ast\Transforms\DeclareVariables.cs" />
<Compile Include="Ast\Transforms\DelegateConstruction.cs" />
<Compile Include="Ast\Transforms\FlattenSwitchBlocks.cs" />
<Compile Include="Ast\Transforms\IntroduceExtensionMethods.cs" />
<Compile Include="Ast\Transforms\IntroduceQueryExpressions.cs" />
<Compile Include="Ast\Transforms\IntroduceUnsafeModifier.cs" />
@ -131,12 +132,7 @@ @@ -131,12 +132,7 @@
<Name>ICSharpCode.NRefactory</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Ast" />
<Folder Include="Ast\Transforms" />
<Folder Include="Disassembler" />
<Folder Include="ILAst" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<Target Name="BeforeBuild">
<MSBuild Projects="$(MSBuildProjectDirectory)\..\BuildTools\UpdateAssemblyInfo\UpdateAssemblyInfo.csproj" Targets="Build" Properties="Configuration=Debug" />

14
NRefactory/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -1734,14 +1734,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1734,14 +1734,16 @@ namespace ICSharpCode.NRefactory.CSharp
label.AcceptVisitor (this, data);
first = false;
}
if(!(switchSection.Statements.FirstOrDefault() is BlockStatement))
NewLine();
if (policy.IndentCaseBody)
formatter.Indent ();
foreach (var statement in switchSection.Statements) {
statement.AcceptVisitor (this, data);
if(switchSection.NextSibling != null)
NewLine();
}
foreach (var statement in switchSection.Statements)
statement.AcceptVisitor(this, data);
if (switchSection.NextSibling != null)
NewLine();
if (policy.IndentCaseBody)
formatter.Unindent ();

Loading…
Cancel
Save