diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs index 75a50e471aa805d7c60f2f6fff20e46bafe69050..a8f09509a607af47bbe660053022d2b731867202 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenLocalFunctionTests.cs @@ -9,7 +9,7 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen { public static class LocalFunctionTestsUtil { - public static IMethodSymbol FindLocalFunction(this CommonTestBase.CompilationVerifier verifier, string localFunctionName) + public static IMethodSymbol FindLocalFunction(this CompilationVerifier verifier, string localFunctionName) { localFunctionName = (char)GeneratedNameKind.LocalFunction + "__" + localFunctionName; var methods = verifier.TestData.GetMethodsByName(); diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/GotoTest.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/GotoTest.cs index 146fddc1aa8621a157b4345999732658df61b7b1..284bf20c025c6ca64f3838ee23237449129402cb 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/GotoTest.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/GotoTest.cs @@ -2,6 +2,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Test.Utilities; +using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Roslyn.Utilities; using Xunit; @@ -703,14 +704,6 @@ public static IEnumerable Power(int number, int exponent) CompileAndVerify(text, expectedOutput: expectedOutput); } - // When ReflectionEmit supports writing exception handler info, this method - // can be removed and CompileAndVerify references above will resolve to - // the overload that emits with both CCI and ReflectionEmit. (Bug #7012) - private CompilationVerifier CompileAndVerify(string source, string expectedOutput = null) - { - return base.CompileAndVerify(source: source, expectedOutput: expectedOutput); - } - [WorkItem(540719, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/540719")] [Fact] public void LabelBetweenLocalAndInitialize() diff --git a/src/Compilers/CSharp/Test/WinRT/WinRTUtil.cs b/src/Compilers/CSharp/Test/WinRT/WinRTUtil.cs index 9fa0ac76ee32d1a68cdaa763bd696df53d0d3e25..27b1e5e0cddf05c328a1ebc916323218671ae540 100644 --- a/src/Compilers/CSharp/Test/WinRT/WinRTUtil.cs +++ b/src/Compilers/CSharp/Test/WinRT/WinRTUtil.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + using Microsoft.CodeAnalysis.CSharp.Test.Utilities; +using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; -using static Microsoft.CodeAnalysis.Test.Utilities.CommonTestBase; namespace Microsoft.CodeAnalysis.CSharp.UnitTests { diff --git a/src/Test/Utilities/Portable/CommonTestBase.CompilationVerifier.cs b/src/Test/Utilities/Portable/CommonTestBase.CompilationVerifier.cs index 0a15afaad15d69b9e7658f53cb1e2ffa659f89ae..b38100c8d7c75ed8f89f7e891effdd7c66ff5137 100644 --- a/src/Test/Utilities/Portable/CommonTestBase.CompilationVerifier.cs +++ b/src/Test/Utilities/Portable/CommonTestBase.CompilationVerifier.cs @@ -3,11 +3,8 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Diagnostics; using System.IO; using System.Linq; -using System.Reflection.Metadata; -using System.Reflection.Metadata.Ecma335; using System.Runtime.CompilerServices; using System.Xml.Linq; using Microsoft.CodeAnalysis; @@ -21,335 +18,330 @@ namespace Microsoft.CodeAnalysis.Test.Utilities { - public partial class CommonTestBase + public sealed class CompilationVerifier { - public class CompilationVerifier + private readonly Compilation _compilation; + private CompilationTestData _testData; + private readonly IEnumerable _dependencies; + private ImmutableArray _diagnostics; + private IModuleSymbol _lazyModuleSymbol; + private IList _allModuleData; + + internal ImmutableArray EmittedAssemblyData; + internal ImmutableArray EmittedAssemblyPdb; + + private readonly Func, string> _visualizeRealIL; + + internal CompilationVerifier( + Compilation compilation, + Func, string> visualizeRealIL = null, + IEnumerable dependencies = null) { - private readonly Compilation _compilation; - private CompilationTestData _testData; - private readonly IEnumerable _dependencies; - private ImmutableArray _diagnostics; - private IModuleSymbol _lazyModuleSymbol; - private IList _allModuleData; - - internal ImmutableArray EmittedAssemblyData; - internal ImmutableArray EmittedAssemblyPdb; - - private readonly Func, string> _visualizeRealIL; - - internal CompilationVerifier( - Compilation compilation, - Func, string> visualizeRealIL = null, - IEnumerable dependencies = null) + _compilation = compilation; + _dependencies = dependencies; + _visualizeRealIL = visualizeRealIL; + } + + internal CompilationTestData TestData => _testData; + public Compilation Compilation => _compilation; + internal ImmutableArray Diagnostics => _diagnostics; + + internal ImmutableArray GetAllModuleMetadata() + { + if (EmittedAssemblyData == null) { - _compilation = compilation; - _dependencies = dependencies; - _visualizeRealIL = visualizeRealIL; + throw new InvalidOperationException("You must call Emit before calling GetAllModuleMetadata."); } - internal CompilationTestData TestData => _testData; - public Compilation Compilation => _compilation; - internal ImmutableArray Diagnostics => _diagnostics; + ImmutableArray modules = ImmutableArray.Create(ModuleMetadata.CreateFromImage(EmittedAssemblyData)); - internal ImmutableArray GetAllModuleMetadata() + if (_allModuleData != null) { - if (EmittedAssemblyData == null) + var netModules = _allModuleData.Where(m => m.Kind == OutputKind.NetModule); + if (netModules.Any()) { - throw new InvalidOperationException("You must call Emit before calling GetAllModuleMetadata."); + modules = modules.Concat( + ImmutableArray.CreateRange(netModules.Select(m => ModuleMetadata.CreateFromImage(m.Image)))); } + } - ImmutableArray modules = ImmutableArray.Create(ModuleMetadata.CreateFromImage(EmittedAssemblyData)); + return modules; + } - if (_allModuleData != null) + public void Emit(string expectedOutput, int? expectedReturnCode, string[] args, IEnumerable manifestResources, EmitOptions emitOptions, bool peVerify, SignatureDescription[] expectedSignatures) + { + using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies)) + { + string mainModuleName = Emit(testEnvironment, manifestResources, emitOptions); + _allModuleData = testEnvironment.GetAllModuleData(); + + if (peVerify) { - var netModules = _allModuleData.Where(m => m.Kind == OutputKind.NetModule); - if (netModules.Any()) - { - modules = modules.Concat( - ImmutableArray.CreateRange(netModules.Select(m => ModuleMetadata.CreateFromImage(m.Image)))); - } + testEnvironment.PeVerify(); } - return modules; - } - - public void Emit(string expectedOutput, int? expectedReturnCode, string[] args, IEnumerable manifestResources, EmitOptions emitOptions, bool peVerify, SignatureDescription[] expectedSignatures) - { - using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies)) + if (expectedSignatures != null) { - string mainModuleName = Emit(testEnvironment, manifestResources, emitOptions); - _allModuleData = testEnvironment.GetAllModuleData(); - - if (peVerify) - { - testEnvironment.PeVerify(); - } + MetadataSignatureUnitTestHelper.VerifyMemberSignatures(testEnvironment, expectedSignatures); + } - if (expectedSignatures != null) - { - MetadataSignatureUnitTestHelper.VerifyMemberSignatures(testEnvironment, expectedSignatures); - } + if (expectedOutput != null || expectedReturnCode != null) + { + var returnCode = testEnvironment.Execute(mainModuleName, args, expectedOutput); - if (expectedOutput != null || expectedReturnCode != null) + if (expectedReturnCode is int exCode) { - var returnCode = testEnvironment.Execute(mainModuleName, args, expectedOutput); - - if (expectedReturnCode is int exCode) - { - Assert.Equal(exCode, returnCode); - } + Assert.Equal(exCode, returnCode); } } } + } - // TODO(tomat): Fold into CompileAndVerify. - // Replace bool verify parameter with string[] expectedPeVerifyOutput. If null, no verification. If empty verify have to succeed. Otherwise compare errors. - public void EmitAndVerify(params string[] expectedPeVerifyOutput) - { - using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies)) - { - string mainModuleName = Emit(testEnvironment, null, null); - string[] actualOutput = testEnvironment.PeVerifyModules(new[] { mainModuleName }, throwOnError: false); - Assert.Equal(expectedPeVerifyOutput, actualOutput); - } - } - - private string Emit(IRuntimeEnvironment testEnvironment, IEnumerable manifestResources, EmitOptions emitOptions) - { - testEnvironment.Emit(_compilation, manifestResources, emitOptions); - - _diagnostics = testEnvironment.GetDiagnostics(); - EmittedAssemblyData = testEnvironment.GetMainImage(); - EmittedAssemblyPdb = testEnvironment.GetMainPdb(); - _testData = ((IInternalRuntimeEnvironment)testEnvironment).GetCompilationTestData(); - - return _compilation.Assembly.Identity.GetDisplayName(); - } - - public CompilationVerifier VerifyIL( - string qualifiedMethodName, - XCData expectedIL, - bool realIL = false, - string sequencePoints = null, - [CallerFilePath]string callerPath = null, - [CallerLineNumber]int callerLine = 0) + // TODO(tomat): Fold into CompileAndVerify. + // Replace bool verify parameter with string[] expectedPeVerifyOutput. If null, no verification. If empty verify have to succeed. Otherwise compare errors. + public void EmitAndVerify(params string[] expectedPeVerifyOutput) + { + using (var testEnvironment = RuntimeEnvironmentFactory.Create(_dependencies)) { - return VerifyILImpl(qualifiedMethodName, expectedIL.Value, realIL, sequencePoints, callerPath, callerLine, escapeQuotes: false); + string mainModuleName = Emit(testEnvironment, null, null); + string[] actualOutput = testEnvironment.PeVerifyModules(new[] { mainModuleName }, throwOnError: false); + Assert.Equal(expectedPeVerifyOutput, actualOutput); } + } - public CompilationVerifier VerifyIL( - string qualifiedMethodName, - string expectedIL, - bool realIL = false, - string sequencePoints = null, - [CallerFilePath]string callerPath = null, - [CallerLineNumber]int callerLine = 0, - string source = null) - { - return VerifyILImpl(qualifiedMethodName, expectedIL, realIL, sequencePoints, callerPath, callerLine, escapeQuotes: true, source: source); - } + private string Emit(IRuntimeEnvironment testEnvironment, IEnumerable manifestResources, EmitOptions emitOptions) + { + testEnvironment.Emit(_compilation, manifestResources, emitOptions); - public void VerifyLocalSignature( - string qualifiedMethodName, - string expectedSignature, - [CallerLineNumber]int callerLine = 0, - [CallerFilePath]string callerPath = null) - { - var ilBuilder = _testData.GetMethodData(qualifiedMethodName).ILBuilder; - string actualSignature = ILBuilderVisualizer.LocalSignatureToString(ilBuilder); - AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedSignature, actualSignature, escapeQuotes: true, expectedValueSourcePath: callerPath, expectedValueSourceLine: callerLine); - } + _diagnostics = testEnvironment.GetDiagnostics(); + EmittedAssemblyData = testEnvironment.GetMainImage(); + EmittedAssemblyPdb = testEnvironment.GetMainPdb(); + _testData = ((IInternalRuntimeEnvironment)testEnvironment).GetCompilationTestData(); - private CompilationVerifier VerifyILImpl( - string qualifiedMethodName, - string expectedIL, - bool realIL, - string sequencePoints, - string callerPath, - int callerLine, - bool escapeQuotes, - string source = null) - { - string actualIL = VisualizeIL(qualifiedMethodName, realIL, sequencePoints, source); - AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedIL, actualIL, escapeQuotes, callerPath, callerLine); - return this; - } + return _compilation.Assembly.Identity.GetDisplayName(); + } - public CompilationVerifier VerifyPdb( - XElement expectedPdb, - IMethodSymbol debugEntryPoint = null, - DebugInformationFormat format = 0, - PdbToXmlOptions options = 0, - [CallerLineNumber]int expectedValueSourceLine = 0, - [CallerFilePath]string expectedValueSourcePath = null) - { - _compilation.VerifyPdb(expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); - return this; - } + public CompilationVerifier VerifyIL( + string qualifiedMethodName, + XCData expectedIL, + bool realIL = false, + string sequencePoints = null, + [CallerFilePath]string callerPath = null, + [CallerLineNumber]int callerLine = 0) + { + return VerifyILImpl(qualifiedMethodName, expectedIL.Value, realIL, sequencePoints, callerPath, callerLine, escapeQuotes: false); + } - public CompilationVerifier VerifyPdb( - string expectedPdb, - IMethodSymbol debugEntryPoint = null, - DebugInformationFormat format = 0, - PdbToXmlOptions options = 0, - [CallerLineNumber]int expectedValueSourceLine = 0, - [CallerFilePath]string expectedValueSourcePath = null) - { - _compilation.VerifyPdb(expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); - return this; - } + public CompilationVerifier VerifyIL( + string qualifiedMethodName, + string expectedIL, + bool realIL = false, + string sequencePoints = null, + [CallerFilePath]string callerPath = null, + [CallerLineNumber]int callerLine = 0, + string source = null) + { + return VerifyILImpl(qualifiedMethodName, expectedIL, realIL, sequencePoints, callerPath, callerLine, escapeQuotes: true, source: source); + } - public CompilationVerifier VerifyPdb( - string qualifiedMethodName, - string expectedPdb, - IMethodSymbol debugEntryPoint = null, - DebugInformationFormat format = 0, - PdbToXmlOptions options = 0, - [CallerLineNumber]int expectedValueSourceLine = 0, - [CallerFilePath]string expectedValueSourcePath = null) - { - _compilation.VerifyPdb(qualifiedMethodName, expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); - return this; - } + public void VerifyLocalSignature( + string qualifiedMethodName, + string expectedSignature, + [CallerLineNumber]int callerLine = 0, + [CallerFilePath]string callerPath = null) + { + var ilBuilder = _testData.GetMethodData(qualifiedMethodName).ILBuilder; + string actualSignature = ILBuilderVisualizer.LocalSignatureToString(ilBuilder); + AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedSignature, actualSignature, escapeQuotes: true, expectedValueSourcePath: callerPath, expectedValueSourceLine: callerLine); + } - public CompilationVerifier VerifyPdb( - string qualifiedMethodName, - XElement expectedPdb, - IMethodSymbol debugEntryPoint = null, - DebugInformationFormat format = 0, - PdbToXmlOptions options = 0, - [CallerLineNumber]int expectedValueSourceLine = 0, - [CallerFilePath]string expectedValueSourcePath = null) - { - _compilation.VerifyPdb(qualifiedMethodName, expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); - return this; - } + private CompilationVerifier VerifyILImpl( + string qualifiedMethodName, + string expectedIL, + bool realIL, + string sequencePoints, + string callerPath, + int callerLine, + bool escapeQuotes, + string source = null) + { + string actualIL = VisualizeIL(qualifiedMethodName, realIL, sequencePoints, source); + AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedIL, actualIL, escapeQuotes, callerPath, callerLine); + return this; + } - public ISymUnmanagedReader3 CreateSymReader() - { - var pdbStream = new MemoryStream(EmittedAssemblyPdb.ToArray()); - return SymReaderFactory.CreateReader(pdbStream, metadataReaderOpt: null, metadataMemoryOwnerOpt: null); - } + public CompilationVerifier VerifyPdb( + XElement expectedPdb, + IMethodSymbol debugEntryPoint = null, + DebugInformationFormat format = 0, + PdbToXmlOptions options = 0, + [CallerLineNumber]int expectedValueSourceLine = 0, + [CallerFilePath]string expectedValueSourcePath = null) + { + _compilation.VerifyPdb(expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); + return this; + } - public string VisualizeIL(string qualifiedMethodName, bool realIL = false, string sequencePoints = null, string source = null) - { - // TODO: Currently the qualifiedMethodName is a symbol display name while PDB need metadata name. - // So we need to pass the PDB metadata name of the method to sequencePoints (instead of just bool). + public CompilationVerifier VerifyPdb( + string expectedPdb, + IMethodSymbol debugEntryPoint = null, + DebugInformationFormat format = 0, + PdbToXmlOptions options = 0, + [CallerLineNumber]int expectedValueSourceLine = 0, + [CallerFilePath]string expectedValueSourcePath = null) + { + _compilation.VerifyPdb(expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); + return this; + } - return VisualizeIL(_testData.GetMethodData(qualifiedMethodName), realIL, sequencePoints, source); - } + public CompilationVerifier VerifyPdb( + string qualifiedMethodName, + string expectedPdb, + IMethodSymbol debugEntryPoint = null, + DebugInformationFormat format = 0, + PdbToXmlOptions options = 0, + [CallerLineNumber]int expectedValueSourceLine = 0, + [CallerFilePath]string expectedValueSourcePath = null) + { + _compilation.VerifyPdb(qualifiedMethodName, expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); + return this; + } - internal string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, string sequencePoints = null, string source = null) - { - Dictionary markers = null; + public CompilationVerifier VerifyPdb( + string qualifiedMethodName, + XElement expectedPdb, + IMethodSymbol debugEntryPoint = null, + DebugInformationFormat format = 0, + PdbToXmlOptions options = 0, + [CallerLineNumber]int expectedValueSourceLine = 0, + [CallerFilePath]string expectedValueSourcePath = null) + { + _compilation.VerifyPdb(qualifiedMethodName, expectedPdb, debugEntryPoint, format, options, expectedValueSourceLine, expectedValueSourcePath); + return this; + } - if (sequencePoints != null) - { - var actualPdbXml = PdbToXmlConverter.ToXml( - pdbStream: new MemoryStream(EmittedAssemblyPdb.ToArray()), - peStream: new MemoryStream(EmittedAssemblyData.ToArray()), - options: PdbToXmlOptions.ResolveTokens | - PdbToXmlOptions.ThrowOnError | - PdbToXmlOptions.ExcludeDocuments | - PdbToXmlOptions.ExcludeCustomDebugInformation | - PdbToXmlOptions.ExcludeScopes, - methodName: sequencePoints); - - markers = PdbValidation.GetMarkers(actualPdbXml, source); - } + public ISymUnmanagedReader3 CreateSymReader() + { + var pdbStream = new MemoryStream(EmittedAssemblyPdb.ToArray()); + return SymReaderFactory.CreateReader(pdbStream, metadataReaderOpt: null, metadataMemoryOwnerOpt: null); + } - if (!realIL) - { - return ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder, markers: markers); - } + public string VisualizeIL(string qualifiedMethodName, bool realIL = false, string sequencePoints = null, string source = null) + { + // TODO: Currently the qualifiedMethodName is a symbol display name while PDB need metadata name. + // So we need to pass the PDB metadata name of the method to sequencePoints (instead of just bool). - if (_lazyModuleSymbol == null) - { - _lazyModuleSymbol = GetModuleSymbolForEmittedImage(EmittedAssemblyData, MetadataImportOptions.All); - } + return VisualizeIL(_testData.GetMethodData(qualifiedMethodName), realIL, sequencePoints, source); + } - return _lazyModuleSymbol != null ? _visualizeRealIL(_lazyModuleSymbol, methodData, markers) : null; - } + internal string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, string sequencePoints = null, string source = null) + { + Dictionary markers = null; - public CompilationVerifier VerifyMemberInIL(string methodName, bool expected) + if (sequencePoints != null) { - Assert.Equal(expected, _testData.GetMethodsByName().ContainsKey(methodName)); - return this; + var actualPdbXml = PdbToXmlConverter.ToXml( + pdbStream: new MemoryStream(EmittedAssemblyPdb.ToArray()), + peStream: new MemoryStream(EmittedAssemblyData.ToArray()), + options: PdbToXmlOptions.ResolveTokens | + PdbToXmlOptions.ThrowOnError | + PdbToXmlOptions.ExcludeDocuments | + PdbToXmlOptions.ExcludeCustomDebugInformation | + PdbToXmlOptions.ExcludeScopes, + methodName: sequencePoints); + + markers = PdbValidation.GetMarkers(actualPdbXml, source); } - public CompilationVerifier VerifyDiagnostics(params DiagnosticDescription[] expected) + if (!realIL) { - _diagnostics.Verify(expected); - return this; + return ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder, markers: markers); } - public IModuleSymbol GetModuleSymbolForEmittedImage() + if (_lazyModuleSymbol == null) { - return GetModuleSymbolForEmittedImage(EmittedAssemblyData, _compilation.Options.MetadataImportOptions); + _lazyModuleSymbol = GetModuleSymbolForEmittedImage(EmittedAssemblyData, MetadataImportOptions.All); } - private IModuleSymbol GetModuleSymbolForEmittedImage(ImmutableArray peImage, MetadataImportOptions importOptions) - { - if (peImage.IsDefault) - { - return null; - } + return _lazyModuleSymbol != null ? _visualizeRealIL(_lazyModuleSymbol, methodData, markers) : null; + } - var targetReference = LoadTestEmittedExecutableForSymbolValidation(peImage, _compilation.Options.OutputKind, display: _compilation.AssemblyName); - var references = _compilation.References.Concat(new[] { targetReference }); - var assemblies = GetReferencesToModuleSymbols(references, importOptions); - return assemblies.Last(); - } + public CompilationVerifier VerifyMemberInIL(string methodName, bool expected) + { + Assert.Equal(expected, _testData.GetMethodsByName().ContainsKey(methodName)); + return this; + } + + public CompilationVerifier VerifyDiagnostics(params DiagnosticDescription[] expected) + { + _diagnostics.Verify(expected); + return this; + } + + public IModuleSymbol GetModuleSymbolForEmittedImage() + { + return GetModuleSymbolForEmittedImage(EmittedAssemblyData, _compilation.Options.MetadataImportOptions); + } - private IEnumerable GetReferencesToModuleSymbols(IEnumerable references, MetadataImportOptions importOptions) + private IModuleSymbol GetModuleSymbolForEmittedImage(ImmutableArray peImage, MetadataImportOptions importOptions) + { + if (peImage.IsDefault) { - var dummy = _compilation - .RemoveAllReferences() - .RemoveAllSyntaxTrees() - .WithReferences(references) - .WithAssemblyName("Dummy") - .WithOptions(_compilation.Options.WithMetadataImportOptions(importOptions)); - - return references.Select(reference => - { - if (reference.Properties.Kind == MetadataImageKind.Assembly) - { - return ((IAssemblySymbol)dummy.GetAssemblyOrModuleSymbol(reference))?.Modules.First(); - } - else - { - return (IModuleSymbol)dummy.GetAssemblyOrModuleSymbol(reference); - } - }); + return null; } + var targetReference = LoadTestEmittedExecutableForSymbolValidation(peImage, _compilation.Options.OutputKind, display: _compilation.AssemblyName); + var references = _compilation.References.Concat(new[] { targetReference }); + var assemblies = GetReferencesToModuleSymbols(references, importOptions); + return assemblies.Last(); + } - internal static MetadataReference LoadTestEmittedExecutableForSymbolValidation( - ImmutableArray image, - OutputKind outputKind, - string display = null) - { - var moduleMetadata = ModuleMetadata.CreateFromImage(image); - moduleMetadata.Module.PretendThereArentNoPiaLocalTypes(); + private IEnumerable GetReferencesToModuleSymbols(IEnumerable references, MetadataImportOptions importOptions) + { + var dummy = _compilation + .RemoveAllSyntaxTrees() + .WithReferences(references) + .WithAssemblyName("Dummy") + .WithOptions(_compilation.Options.WithMetadataImportOptions(importOptions)); - if (outputKind == OutputKind.NetModule) + return references.Select(reference => + { + if (reference.Properties.Kind == MetadataImageKind.Assembly) { - return moduleMetadata.GetReference(display: display); + return ((IAssemblySymbol)dummy.GetAssemblyOrModuleSymbol(reference))?.Modules.First(); } else { - return AssemblyMetadata.Create(moduleMetadata).GetReference(display: display); + return (IModuleSymbol)dummy.GetAssemblyOrModuleSymbol(reference); } - } + }); + } + + internal static MetadataReference LoadTestEmittedExecutableForSymbolValidation( + ImmutableArray image, + OutputKind outputKind, + string display = null) + { + var moduleMetadata = ModuleMetadata.CreateFromImage(image); + moduleMetadata.Module.PretendThereArentNoPiaLocalTypes(); - public void VerifyOperationTree(string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false) + if (outputKind == OutputKind.NetModule) { - _compilation.VerifyOperationTree(expectedOperationTree, skipImplicitlyDeclaredSymbols); + return moduleMetadata.GetReference(display: display); } - - public void VerifyOperationTree(string symbolToVerify, string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false) + else { - _compilation.VerifyOperationTree(symbolToVerify, expectedOperationTree, skipImplicitlyDeclaredSymbols); + return AssemblyMetadata.Create(moduleMetadata).GetReference(display: display); } } + + public void VerifyOperationTree(string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false) + { + _compilation.VerifyOperationTree(expectedOperationTree, skipImplicitlyDeclaredSymbols); + } + + public void VerifyOperationTree(string symbolToVerify, string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false) + { + _compilation.VerifyOperationTree(symbolToVerify, expectedOperationTree, skipImplicitlyDeclaredSymbols); + } } }