Browse Source

Merge pull request #1850 from icsharpcode/issue1848

Additional settings #1848
pull/1855/head
Siegfried Pammer 6 years ago committed by GitHub
parent
commit
ab785fbe2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs
  2. 2
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs
  3. 51
      ICSharpCode.Decompiler/DecompilerSettings.cs
  4. 5
      ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs
  5. 27
      ILSpy/Properties/Resources.Designer.cs
  6. 9
      ILSpy/Properties/Resources.resx

13
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -432,6 +432,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -432,6 +432,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
&& identExpr.TypeArguments.Count == 0;
}
bool CombineDeclarationAndInitializer(VariableToDeclare v, TransformContext context)
{
if (v.Type.IsByRefLike)
return true; // by-ref-like variables always must be initialized at their declaration.
if (v.InsertionPoint.nextNode.Role == ForStatement.InitializerRole)
return true; // for-statement initializers always should combine declaration and initialization.
return !context.Settings.SeparateLocalVariableDeclarations;
}
void InsertVariableDeclarations(TransformContext context)
{
var replacements = new List<(AstNode, AstNode)>();
@ -439,7 +450,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -439,7 +450,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (v.RemovedDueToCollision)
continue;
if (IsMatchingAssignment(v, out AssignmentExpression assignment)) {
if (CombineDeclarationAndInitializer(v, context) && IsMatchingAssignment(v, out AssignmentExpression assignment)) {
// 'int v; v = expr;' can be combined to 'int v = expr;'
AstType type;
if (context.Settings.AnonymousTypes && v.Type.ContainsAnonymousType()) {

2
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -165,6 +165,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -165,6 +165,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
public ForStatement TransformFor(ExpressionStatement node)
{
if (!context.Settings.ForStatement)
return null;
Match m1 = variableAssignPattern.Match(node);
if (!m1.Success) return null;
var variable = m1.Get<IdentifierExpression>("variable").Single().GetILVariable();

51
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -1242,6 +1242,57 @@ namespace ICSharpCode.Decompiler @@ -1242,6 +1242,57 @@ namespace ICSharpCode.Decompiler
#endregion
bool forStatement = true;
/// <summary>
/// Gets/sets whether the decompiler should produce for loops.
/// </summary>
[Category("C# 1.0 / VS .NET")]
[Description("DecompilerSettings.ForStatement")]
public bool ForStatement {
get { return forStatement; }
set {
if (forStatement != value) {
forStatement = value;
OnPropertyChanged();
}
}
}
bool doWhileStatement = true;
/// <summary>
/// Gets/sets whether the decompiler should produce do-while loops.
/// </summary>
[Category("C# 1.0 / VS .NET")]
[Description("DecompilerSettings.DoWhileStatement")]
public bool DoWhileStatement {
get { return doWhileStatement; }
set {
if (doWhileStatement != value) {
doWhileStatement = value;
OnPropertyChanged();
}
}
}
bool separateLocalVariableDeclarations = false;
/// <summary>
/// Gets/sets whether the decompiler should separate local variable declarations from their initialization.
/// </summary>
[Category("DecompilerSettings.Other")]
[Description("DecompilerSettings.SeparateLocalVariableDeclarations")]
public bool SeparateLocalVariableDeclarations {
get { return separateLocalVariableDeclarations; }
set {
if (separateLocalVariableDeclarations != value) {
separateLocalVariableDeclarations = value;
OnPropertyChanged();
}
}
}
CSharpFormattingOptions csharpFormattingOptions;
[Browsable(false)]

5
ICSharpCode.Decompiler/IL/Transforms/HighLevelLoopTransform.cs

@ -42,10 +42,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -42,10 +42,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (loop.Kind != ContainerKind.Loop)
continue;
if (MatchWhileLoop(loop, out var condition, out var loopBody)) {
MatchForLoop(loop, condition, loopBody);
if (context.Settings.ForStatement)
MatchForLoop(loop, condition, loopBody);
continue;
}
if (MatchDoWhileLoop(loop))
if (context.Settings.DoWhileStatement && MatchDoWhileLoop(loop))
continue;
}
}

27
ILSpy/Properties/Resources.Designer.cs generated

@ -763,6 +763,24 @@ namespace ICSharpCode.ILSpy.Properties { @@ -763,6 +763,24 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Transform to do-while, if possible..
/// </summary>
public static string DecompilerSettings_DoWhileStatement {
get {
return ResourceManager.GetString("DecompilerSettings.DoWhileStatement", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Transform to for, if possible..
/// </summary>
public static string DecompilerSettings_ForStatement {
get {
return ResourceManager.GetString("DecompilerSettings.ForStatement", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to F#-specific options.
/// </summary>
@ -891,6 +909,15 @@ namespace ICSharpCode.ILSpy.Properties { @@ -891,6 +909,15 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Separate local variable declarations and initializers (int x = 5; -&gt; int x; x = 5;), if possible..
/// </summary>
public static string DecompilerSettings_SeparateLocalVariableDeclarations {
get {
return ResourceManager.GetString("DecompilerSettings.SeparateLocalVariableDeclarations", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Show info from debug symbols, if available.
/// </summary>

9
ILSpy/Properties/Resources.resx

@ -793,4 +793,13 @@ Are you sure you want to continue?</value> @@ -793,4 +793,13 @@ Are you sure you want to continue?</value>
<data name="DecompilerSettings.UsePatternBasedFixedStatement" xml:space="preserve">
<value>Use pattern-based fixed statement</value>
</data>
<data name="DecompilerSettings.DoWhileStatement" xml:space="preserve">
<value>Transform to do-while, if possible.</value>
</data>
<data name="DecompilerSettings.ForStatement" xml:space="preserve">
<value>Transform to for, if possible.</value>
</data>
<data name="DecompilerSettings.SeparateLocalVariableDeclarations" xml:space="preserve">
<value>Separate local variable declarations and initializers (int x = 5; -&gt; int x; x = 5;), if possible.</value>
</data>
</root>
Loading…
Cancel
Save