From 3c9a0d8a41dc8240840de03d991753f5fc488495 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 23 Sep 2017 19:56:38 +0200 Subject: [PATCH] Ad #866: No longer use Debug.Assert, but add warnings to AST. --- ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs | 6 ++++++ ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs | 7 ++++++- ICSharpCode.Decompiler/IL/ILReader.cs | 4 +++- ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs | 11 +++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs index 5b7876ab1..3ad5d70ed 100644 --- a/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs @@ -663,6 +663,12 @@ namespace ICSharpCode.Decompiler.CSharp AddDefinesForConditionalAttributes(function); var statementBuilder = new StatementBuilder(specializingTypeSystem, decompilationContext, method, function, settings, CancellationToken); var body = statementBuilder.ConvertAsBlock(function.Body); + + Comment prev = null; + foreach (string warning in function.Warnings) { + body.InsertChildAfter(prev, prev = new Comment(warning), Roles.Comment); + } + entityDecl.AddChild(body, Roles.Body); if (function.IsIterator) { diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index e18fd8b96..634572b1d 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -1009,7 +1009,12 @@ namespace ICSharpCode.Decompiler.CSharp var body = builder.ConvertAsBlock(function.Body); bool isLambda = false; bool isMultiLineLambda = false; - + + Comment prev = null; + foreach (string warning in function.Warnings) { + body.InsertChildAfter(prev, prev = new Comment(warning), Roles.Comment); + } + // if there is an anonymous type involved, we are forced to use a lambda expression. if (ame.Parameters.Any(p => p.Type.IsNull)) { isLambda = true; diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index e307e68bf..d98a50350 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -38,6 +38,7 @@ namespace ICSharpCode.Decompiler.IL readonly IDecompilerTypeSystem typeSystem; public bool UseDebugSymbols { get; set; } + public List Warnings { get; } = new List(); public ILReader(IDecompilerTypeSystem typeSystem) { @@ -182,7 +183,7 @@ namespace ICSharpCode.Decompiler.IL /// void Warn(string message) { - Debug.Fail(string.Format("IL_{0:x4}: {1}", currentInstruction.Offset, message)); + Warnings.Add(string.Format("IL_{0:x4}: {1}", currentInstruction.Offset, message)); } void MergeStacks(ImmutableStack a, ImmutableStack b) @@ -324,6 +325,7 @@ namespace ICSharpCode.Decompiler.IL foreach (var c in function.Descendants.OfType()) { c.SortBlocks(); } + function.Warnings.AddRange(Warnings); return function; } diff --git a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs index 0616a4c50..d570ae62b 100644 --- a/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs +++ b/ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs @@ -34,6 +34,11 @@ namespace ICSharpCode.Decompiler.IL public readonly MethodDefinition Method; public readonly ILVariableCollection Variables; + /// + /// List of warnings of ILReader. + /// + public List Warnings { get; } = new List(); + /// /// Gets whether this function is a decompiled iterator (is using yield). /// This flag gets set by the YieldReturnDecompiler. @@ -103,6 +108,12 @@ namespace ICSharpCode.Decompiler.IL output.MarkFoldEnd(); output.WriteLine(); body.WriteTo(output, options); + + foreach (string warning in Warnings) { + output.WriteLine("//" + warning); + } + + body.WriteTo(output); output.WriteLine(); output.Unindent();