Browse Source

Initialize variables with default(T) if no other initialization is done

pull/728/head
Siegfried Pammer 9 years ago
parent
commit
cc4f8463ca
  1. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 12
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -587,6 +587,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -587,6 +587,7 @@ namespace ICSharpCode.Decompiler.CSharp
if (v.Kind == VariableKind.Local || v.Kind == VariableKind.StackSlot) {
var type = typeSystemAstBuilder.ConvertType(v.Type);
var varDecl = new VariableDeclarationStatement(type, v.Name);
varDecl.Variables.Single().AddAnnotation(v);
body.Statements.InsertAfter(prevVarDecl, varDecl);
prevVarDecl = varDecl;
}

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

@ -56,11 +56,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -56,11 +56,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (v.ReplacedAssignment == null) {
BlockStatement block = (BlockStatement)v.InsertionPoint.Parent;
var decl = new VariableDeclarationStatement((AstType)v.Type.Clone(), v.Name);
if (v.ILVariable != null)
decl.Variables.Single().AddAnnotation(v.ILVariable);
block.Statements.InsertBefore(
v.InsertionPoint,
decl);
var ilVar = v.ILVariable;
if (ilVar != null) {
decl.Variables.Single().AddAnnotation(ilVar);
if (ilVar.HasInitialValue && ilVar.Kind == VariableKind.Local)
decl.Variables.Single().Initializer = new DefaultValueExpression((AstType)v.Type.Clone());
}
block.Statements.InsertBefore(v.InsertionPoint, decl);
}
}
// First do all the insertions, then do all the replacements. This is necessary because a replacement might remove our reference point from the AST.

Loading…
Cancel
Save