未验证 提交 1cd08cc2 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #29029 from siegfriedpammer/decompiler-v4

Update ICSharpCode.Decompiler to 4.0-beta1
......@@ -14,7 +14,7 @@
<dotnetxunitVersion>2.3.1</dotnetxunitVersion>
<FakeSignVersion>0.9.2</FakeSignVersion>
<HumanizerCoreVersion>2.2.0</HumanizerCoreVersion>
<ICSharpCodeDecompilerVersion>3.1.0.3652</ICSharpCodeDecompilerVersion>
<ICSharpCodeDecompilerVersion>4.0.0.4285-beta1</ICSharpCodeDecompilerVersion>
<LibGit2SharpVersion>0.22.0</LibGit2SharpVersion>
<MicroBuildCoreVersion>0.2.0</MicroBuildCoreVersion>
<MicroBuildCoreSentinelVersion>1.0.0</MicroBuildCoreSentinelVersion>
......
......@@ -185,10 +185,6 @@
"Microsoft.VisualStudio.Threading.resources.dll",
"Microsoft.VisualStudio.Validation.dll",
"Microsoft.Win32.Primitives.dll",
"Mono.Cecil.dll",
"Mono.Cecil.Mdb.dll",
"Mono.Cecil.Pdb.dll",
"Mono.Cecil.Rocks.dll",
"Nerdbank.FullDuplexStream.dll",
"Newtonsoft.Json.dll",
"NuGet.CommandLine.2.8.5.nupkg",
......
......@@ -7,12 +7,14 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.CSharp.Transforms;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.MetadataAsSource;
......@@ -20,7 +22,6 @@
using Microsoft.CodeAnalysis.SymbolMapping;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Mono.Cecil;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editor.Implementation.MetadataAsSource
......@@ -224,10 +225,11 @@ private async Task<Document> DecompileSymbolAsync(Document temporaryDocument, IS
}
// Load the assembly.
var assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyLocation, new ReaderParameters() { AssemblyResolver = new RoslynAssemblyResolver(compilation) });
var pefile = new PEFile(assemblyLocation, PEStreamOptions.PrefetchEntireImage);
// Initialize a decompiler with default settings.
var decompiler = new CSharpDecompiler(assemblyDefinition.MainModule, new DecompilerSettings());
var settings = new DecompilerSettings(LanguageVersion.Latest);
var decompiler = new CSharpDecompiler(pefile, new RoslynAssemblyResolver(compilation), settings);
// Escape invalid identifiers to prevent Roslyn from failing to parse the generated code.
// (This happens for example, when there is compiler-generated code that is not yet recognized/transformed by the decompiler.)
decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers());
......@@ -238,8 +240,8 @@ private async Task<Document> DecompileSymbolAsync(Document temporaryDocument, IS
// Add header to match output of metadata-only view.
// (This also makes debugging easier, because you can see which assembly was decompiled inside VS.)
var header = $"#region {FeaturesResources.Assembly} {assemblyDefinition.FullName}" + Environment.NewLine
+ $"// {assemblyDefinition.MainModule.FileName}" + Environment.NewLine
var header = $"#region {FeaturesResources.Assembly} {pefile.FullName}" + Environment.NewLine
+ $"// {assemblyLocation}" + Environment.NewLine
+ $"// Decompiled with ICSharpCode.Decompiler {decompilerVersion.FileVersion}" + Environment.NewLine
+ "#endregion" + Environment.NewLine;
......@@ -251,28 +253,31 @@ private async Task<Document> DecompileSymbolAsync(Document temporaryDocument, IS
private class RoslynAssemblyResolver : IAssemblyResolver
{
private readonly Compilation parentCompilation;
private static readonly Version zeroVersion = new Version(0, 0, 0, 0);
public RoslynAssemblyResolver(Compilation parentCompilation)
{
this.parentCompilation = parentCompilation;
}
public AssemblyDefinition Resolve(AssemblyNameReference name)
{
return Resolve(name, new ReaderParameters());
}
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
public PEFile Resolve(IAssemblyReference name)
{
foreach (var assembly in parentCompilation.GetReferencedAssemblySymbols())
{
// First, find the correct IAssemblySymbol by name and PublicKeyToken.
if (assembly.Identity.Name != name.Name
|| !assembly.Identity.PublicKeyToken.SequenceEqual(name.PublicKeyToken ?? Array.Empty<byte>()))
{
continue;
}
if (assembly.Identity.Version != name.Version
// Normally we skip versions that do not match, except if the reference is "mscorlib" (see comments below)
// or if the name.Version is '0.0.0.0'. This is because we require the metadata of all transitive references
// and modules, to achieve best decompilation results.
// In the case of .NET Standard projects for example, the 'netstandard' reference contains no references
// with actual versions. All versions are '0.0.0.0', therefore we have to ignore those version numbers,
// and can just use the references provided by Roslyn instead.
if (assembly.Identity.Version != name.Version && name.Version != zeroVersion
&& !string.Equals("mscorlib", assembly.Identity.Name, StringComparison.OrdinalIgnoreCase))
{
// MSBuild treats mscorlib special for the purpose of assembly resolution/unification, where all
......@@ -280,17 +285,24 @@ public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters p
continue;
}
// reference assemblies should be fine here...
// reference assemblies should be fine here, we only need the metadata of references.
var reference = parentCompilation.GetMetadataReference(assembly);
return AssemblyDefinition.ReadAssembly(reference.Display);
return new PEFile(reference.Display, PEStreamOptions.PrefetchMetadata);
}
// not found
return null;
}
public void Dispose()
public PEFile ResolveModule(PEFile mainModule, string moduleName)
{
// Primitive implementation to support multi-module assemblies
// where all modules are located next to the main module.
string baseDirectory = Path.GetDirectoryName(mainModule.FileName);
string moduleFileName = Path.Combine(baseDirectory, moduleName);
if (!File.Exists(moduleFileName))
return null;
return new PEFile(moduleFileName, PEStreamOptions.PrefetchMetadata);
}
}
......
......@@ -538,10 +538,6 @@ Public Class BuildDevDivInsertionFiles
add("Vsix\Roslyn.VisualStudio.Setup\System.Composition.Hosting.dll")
add("Vsix\Roslyn.VisualStudio.Setup\System.Composition.TypedParts.dll")
add("Vsix\Roslyn.VisualStudio.Setup\System.Threading.Tasks.Extensions.dll")
add("Vsix\Roslyn.VisualStudio.Setup\Mono.Cecil.dll")
add("Vsix\Roslyn.VisualStudio.Setup\Mono.Cecil.Mdb.dll")
add("Vsix\Roslyn.VisualStudio.Setup\Mono.Cecil.Pdb.dll")
add("Vsix\Roslyn.VisualStudio.Setup\Mono.Cecil.Rocks.dll")
add("Vsix\Roslyn.VisualStudio.Setup\ICSharpCode.Decompiler.dll")
add("Dlls\Microsoft.CodeAnalysis.VisualBasic.ExpressionCompiler\Microsoft.CodeAnalysis.VisualBasic.ExpressionEvaluator.ExpressionCompiler.vsdconfig")
add("Dlls\Microsoft.CodeAnalysis.VisualBasic.ResultProvider\Microsoft.CodeAnalysis.VisualBasic.ExpressionEvaluator.ResultProvider.vsdconfig")
......
......@@ -44,7 +44,6 @@
[assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\System.Threading.Tasks.Extensions.dll")]
[assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\Humanizer.dll")]
[assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\Mono.Cecil.dll")]
[assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\ICSharpCode.Decompiler.dll")]
[assembly: ProvideBindingRedirection(
......
......@@ -237,7 +237,6 @@
<NuGetPackageToIncludeInVsix Include="System.Composition.Hosting" />
<NuGetPackageToIncludeInVsix Include="System.Threading.Tasks.Extensions" />
<NuGetPackageToIncludeInVsix Include="ICSharpCode.Decompiler" />
<NuGetPackageToIncludeInVsix Include="Mono.Cecil" />
</ItemGroup>
<ItemGroup>
<VSIXSourceItem Include="$(NuGetPackageRoot)\SQLitePCLRaw.bundle_green\1.1.2\lib\net45\SQLitePCLRaw.batteries_green.dll" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册