Browse Source

Fix NullReferenceException in AstMethodBodyBuilder introduced by the bugfix for #282.

Improvement coding style of generated switch statements.
pull/348/head
Daniel Grunwald 14 years ago
parent
commit
5d3a812ac0
  1. 2
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 2
      ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs
  3. 22
      ICSharpCode.Decompiler/DecompilerSettings.cs
  4. 9
      NRefactory/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs

2
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -159,7 +159,7 @@ namespace ICSharpCode.Decompiler.Ast
astCompileUnit.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); astCompileUnit.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
var outputFormatter = new TextOutputFormatter(output); var outputFormatter = new TextOutputFormatter(output);
var formattingPolicy = FormattingOptionsFactory.CreateAllman(); var formattingPolicy = context.Settings.CSharpFormattingOptions;
astCompileUnit.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, formattingPolicy)); astCompileUnit.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, formattingPolicy));
} }

2
ICSharpCode.Decompiler/Ast/AstMethodBodyBuilder.cs

@ -550,7 +550,7 @@ namespace ICSharpCode.Decompiler.Ast
else else
goto case ILCode.Castclass; goto case ILCode.Castclass;
case ILCode.Castclass: case ILCode.Castclass:
if (byteCode.Arguments[0].InferredType.IsGenericParameter || ((Cecil.TypeReference)operand).IsGenericParameter) if ((byteCode.Arguments[0].InferredType != null && byteCode.Arguments[0].InferredType.IsGenericParameter) || ((Cecil.TypeReference)operand).IsGenericParameter)
return arg1.CastTo(new PrimitiveType("object")).CastTo(operandAsTypeRef); return arg1.CastTo(new PrimitiveType("object")).CastTo(operandAsTypeRef);
else else
return arg1.CastTo(operandAsTypeRef); return arg1.CastTo(operandAsTypeRef);

22
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -18,6 +18,7 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using ICSharpCode.NRefactory.CSharp;
namespace ICSharpCode.Decompiler namespace ICSharpCode.Decompiler
{ {
@ -271,6 +272,26 @@ namespace ICSharpCode.Decompiler
} }
#endregion #endregion
CSharpFormattingOptions csharpFormattingOptions;
public CSharpFormattingOptions CSharpFormattingOptions {
get {
if (csharpFormattingOptions == null) {
csharpFormattingOptions = FormattingOptionsFactory.CreateAllman();
csharpFormattingOptions.IndentSwitchBody = false;
}
return csharpFormattingOptions;
}
set {
if (value == null)
throw new ArgumentNullException();
if (csharpFormattingOptions != value) {
csharpFormattingOptions = value;
OnPropertyChanged("CSharpFormattingOptions");
}
}
}
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) protected virtual void OnPropertyChanged(string propertyName)
@ -283,6 +304,7 @@ namespace ICSharpCode.Decompiler
public DecompilerSettings Clone() public DecompilerSettings Clone()
{ {
DecompilerSettings settings = (DecompilerSettings)MemberwiseClone(); DecompilerSettings settings = (DecompilerSettings)MemberwiseClone();
settings.csharpFormattingOptions = csharpFormattingOptions.Clone();
settings.PropertyChanged = null; settings.PropertyChanged = null;
return settings; return settings;
} }

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

@ -1837,16 +1837,19 @@ namespace ICSharpCode.NRefactory.CSharp
label.AcceptVisitor(this); label.AcceptVisitor(this);
first = false; first = false;
} }
if (policy.IndentCaseBody) { bool isBlock = switchSection.Statements.Count == 1 && switchSection.Statements.Single() is BlockStatement;
if (policy.IndentCaseBody && !isBlock) {
formatter.Indent(); formatter.Indent();
} }
foreach (var statement in switchSection.Statements) { if (!isBlock)
NewLine(); NewLine();
foreach (var statement in switchSection.Statements) {
statement.AcceptVisitor(this); statement.AcceptVisitor(this);
} }
if (policy.IndentCaseBody) { if (policy.IndentCaseBody && !isBlock) {
formatter.Unindent(); formatter.Unindent();
} }

Loading…
Cancel
Save