Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ICSharpCode.Decompiler to 4.0-beta1 #29029

Merged
merged 5 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/Targets/Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 0 additions & 4 deletions build/config/SignToolData.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@
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;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.SymbolMapping;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Mono.Cecil;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Implementation.MetadataAsSource
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line between comment and the var decompiler = … line.

// (This happens for example, when there is compiler-generated code that is not yet recognized/transformed by the decompiler.)
decompiler.AstTransforms.Add(new EscapeInvalidIdentifiers());
Expand All @@ -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;

Expand All @@ -251,46 +253,56 @@ 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
// versions of the assembly are considered equal. The same policy is adopted here.
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);
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/Setup/DevDivInsertionFiles/BuildDevDivInsertionFiles.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Imports System.IO.Packaging
Imports System.IO.Packaging
Imports System.IO
Imports System.Threading
Imports Newtonsoft.Json
Expand Down Expand Up @@ -574,10 +574,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")
Expand Down Expand Up @@ -945,4 +941,4 @@ set DEVPATH=%RoslynToolsRoot%;%DEVPATH%"
Console.WriteLine(s)
End If
End Sub
End Class
End Class
1 change: 0 additions & 1 deletion src/VisualStudio/Setup/AssemblyRedirects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion src/VisualStudio/Setup/Roslyn.VisualStudio.Setup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down