Ejemplo n.º 1
0
        internal string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, string sequencePoints = null, string source = null)
        {
            Dictionary <int, string> markers = null;

            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 = ILValidation.GetSequencePointMarkers(actualPdbXml, source);
            }

            if (!realIL)
            {
                return(ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder, markers: markers));
            }

            if (_lazyModuleSymbol == null)
            {
                var targetReference = LoadTestEmittedExecutableForSymbolValidation(EmittedAssemblyData, _compilation.Options.OutputKind, display: _compilation.AssemblyName);
                _lazyModuleSymbol = GetSymbolFromMetadata(targetReference, MetadataImportOptions.All);
            }

            return(_lazyModuleSymbol != null?_visualizeRealIL(_lazyModuleSymbol, methodData, markers) : null);
        }
Ejemplo n.º 2
0
            internal string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, string sequencePoints = null)
            {
                Dictionary <int, string> markers = null;

                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);
                }

                if (!realIL)
                {
                    return(ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder, markers: markers));
                }

                if (_lazyModuleSymbol == null)
                {
                    _lazyModuleSymbol = GetModuleSymbolForEmittedImage(EmittedAssemblyData, MetadataImportOptions.All);
                }

                return(_lazyModuleSymbol != null?_test.VisualizeRealIL(_lazyModuleSymbol, methodData, markers) : null);
            }
Ejemplo n.º 3
0
        internal static void VerifyIL(
            this CompilationTestData.MethodData method,
            string expectedIL,
            [CallerLineNumber] int expectedValueSourceLine  = 0,
            [CallerFilePath] string expectedValueSourcePath = null
            )
        {
            const string moduleNamePlaceholder = "{#Module#}";
            string       actualIL = GetMethodIL(method);

            if (expectedIL.IndexOf(moduleNamePlaceholder) >= 0)
            {
                var module     = method.Method.ContainingModule;
                var moduleName = Path.GetFileNameWithoutExtension(module.Name);
                expectedIL = expectedIL.Replace(moduleNamePlaceholder, moduleName);
            }

            AssertEx.AssertEqualToleratingWhitespaceDifferences(
                expectedIL,
                actualIL,
                escapeQuotes: true,
                expectedValueSourcePath: expectedValueSourcePath,
                expectedValueSourceLine: expectedValueSourceLine
                );
        }
Ejemplo n.º 4
0
        private static void VerifyMethodData(CompilationTestData.MethodData methodData, string expectedType, string expectedIL)
        {
            methodData.VerifyIL(expectedIL);
            var method = (MethodSymbol)methodData.Method;

            VerifyTypeParameters(method);
            Assert.Equal(expectedType, method.ReturnType.ToTestDisplayString());
        }
Ejemplo n.º 5
0
        internal string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, string sequencePoints = null, string source = null)
        {
            Dictionary <int, string> markers = null;

            if (sequencePoints != null)
            {
                if (EmittedAssemblyPdb == null)
                {
                    throw new InvalidOperationException($"{nameof(EmittedAssemblyPdb)} is not set");
                }

                if (EmittedAssemblyData == null)
                {
                    throw new InvalidOperationException($"{nameof(EmittedAssemblyData)} is not set");
                }

                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);

                if (actualPdbXml.StartsWith("<error>"))
                {
                    throw new Exception($"Failed to extract PDB information for method '{sequencePoints}'. PdbToXmlConverter returned:\r\n{actualPdbXml}");
                }

                markers = ILValidation.GetSequencePointMarkers(actualPdbXml, source);
            }

            if (!realIL)
            {
                return(ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder, markers: markers));
            }

            if (_lazyModuleSymbol == null)
            {
                var targetReference = LoadTestEmittedExecutableForSymbolValidation(EmittedAssemblyData, _compilation.Options.OutputKind, display: _compilation.AssemblyName);
                _lazyModuleSymbol = GetSymbolFromMetadata(targetReference, MetadataImportOptions.All);
            }

            if (_lazyModuleSymbol != null)
            {
                if (_visualizeRealIL == null)
                {
                    throw new InvalidOperationException("IL visualization function is not set");
                }


                return(_visualizeRealIL(_lazyModuleSymbol, methodData, markers, _testData.Module.GetMethodBody(methodData.Method).AreLocalsZeroed));
            }

            return(null);
        }
Ejemplo n.º 6
0
 internal static EditAndContinueMethodDebugInformation GetEncDebugInfo(this CompilationTestData.MethodData methodData)
 {
     // TODO:
     return(new EditAndContinueMethodDebugInformation(
                0,
                Cci.MetadataWriter.GetLocalSlotDebugInfos(methodData.ILBuilder.LocalSlotManager.LocalsInOrder()),
                closures: ImmutableArray <ClosureDebugInfo> .Empty,
                lambdas: ImmutableArray <LambdaDebugInfo> .Empty));
 }
        internal static void VerifyIL(
            this CompilationTestData.MethodData method,
            string expectedIL,
            [CallerLineNumber] int expectedValueSourceLine  = 0,
            [CallerFilePath] string expectedValueSourcePath = null)
        {
            string actualIL = GetMethodIL(method);

            AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedIL, actualIL, escapeQuotes: true, expectedValueSourcePath: expectedValueSourcePath, expectedValueSourceLine: expectedValueSourceLine);
        }
            private string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, bool useRefEmitter)
            {
                if (!realIL)
                {
                    return(ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder));
                }

                var module = this.GetModuleSymbolForEmittedImage();

                return(module != null?test.VisualizeRealIL(module, methodData) : null);
            }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns a string representation of IL read from metadata.
        /// </summary>
        /// <remarks>
        /// Currently unsupported IL decoding:
        /// - multidimensional arrays
        /// - vararg calls
        /// - winmd
        /// - global methods
        /// </remarks>
        internal unsafe static string VisualizeRealIL(PEModuleSymbol peModule, CompilationTestData.MethodData methodData, IReadOnlyDictionary <int, string> markers)
        {
            var typeName = GetContainingTypeMetadataName(methodData.Method);
            // TODO (tomat): global methods (typeName == null)

            var type = peModule.ContainingAssembly.GetTypeByMetadataName(typeName);

            // TODO (tomat): overloaded methods
            var method = (PEMethodSymbol)type.GetMembers(methodData.Method.MetadataName).Single();

            var bodyBlock = peModule.Module.GetMethodBodyOrThrow(method.Handle);

            Assert.NotNull(bodyBlock);

            var moduleDecoder = new MetadataDecoder(peModule);
            var peMethod      = (PEMethodSymbol)moduleDecoder.GetSymbolForILToken(method.Handle);

            StringBuilder sb      = new StringBuilder();
            var           ilBytes = bodyBlock.GetILContent();

            var ehHandlerRegions = ILVisualizer.GetHandlerSpans(bodyBlock.ExceptionRegions);

            var methodDecoder = new MetadataDecoder(peModule, peMethod);

            ImmutableArray <ILVisualizer.LocalInfo> localDefinitions;

            if (!bodyBlock.LocalSignature.IsNil)
            {
                var signature       = peModule.Module.MetadataReader.GetStandaloneSignature(bodyBlock.LocalSignature).Signature;
                var signatureReader = peModule.Module.GetMemoryReaderOrThrow(signature);
                var localInfos      = methodDecoder.DecodeLocalSignatureOrThrow(ref signatureReader);
                localDefinitions = ToLocalDefinitions(localInfos, methodData.ILBuilder);
            }
            else
            {
                localDefinitions = ImmutableArray.Create <ILVisualizer.LocalInfo>();
            }

            // TODO (tomat): the .maxstack in IL can't be less than 8, but many tests expect .maxstack < 8
            int maxStack = (bodyBlock.MaxStack == 8 && methodData.ILBuilder.MaxStack < 8) ? methodData.ILBuilder.MaxStack : bodyBlock.MaxStack;

            var visualizer = new Visualizer(new MetadataDecoder(peModule, peMethod));

            visualizer.DumpMethod(sb, maxStack, ilBytes, localDefinitions, ehHandlerRegions, markers);

            return(sb.ToString());
        }
            private string VisualizeIL(CompilationTestData.MethodData methodData, bool realIL, string sequencePoints, bool useRefEmitter)
            {
                Dictionary <int, string> markers = null;

                if (sequencePoints != null)
                {
                    var actualPdbXml = PdbToXmlConverter.ToXml(
                        pdbStream: new MemoryStream(EmittedAssemblyPdb.ToArray()),
                        peStream: new MemoryStream(EmittedAssemblyData.ToArray()),
                        options: PdbToXmlOptions.ResolveTokens | PdbToXmlOptions.ThrowOnError,
                        methodName: sequencePoints);

                    markers = GetMarkers(actualPdbXml);
                }

                if (!realIL)
                {
                    return(ILBuilderVisualizer.ILBuilderToString(methodData.ILBuilder, markers: markers));
                }

                var module = this.GetModuleSymbolForEmittedImage();

                return(module != null?_test.VisualizeRealIL(module, methodData, markers) : null);
            }
Ejemplo n.º 11
0
 internal override string VisualizeRealIL(IModuleSymbol peModule, CompilationTestData.MethodData methodData, IReadOnlyDictionary <int, string> markers)
 {
     return(VisualizeRealIL((PEModuleSymbol)peModule, methodData, markers));
 }
Ejemplo n.º 12
0
 internal static Func <MethodDefinitionHandle, EditAndContinueMethodDebugInformation> EncDebugInfoProvider(this CompilationTestData.MethodData methodData)
 {
     return(_ => methodData.GetEncDebugInfo());
 }
Ejemplo n.º 13
0
 internal static string GetMethodIL(this CompilationTestData.MethodData method)
 {
     return(ILBuilderVisualizer.ILBuilderToString(method.ILBuilder));
 }
Ejemplo n.º 14
0
 internal override string VisualizeRealIL(
     IModuleSymbol peModule,
     CompilationTestData.MethodData methodData,
     IReadOnlyDictionary <int, string> markers,
     bool areLocalsZeroed
     ) => throw new NotImplementedException();
Ejemplo n.º 15
0
 internal static EditAndContinueMethodDebugInformation GetEncDebugInfo(this CompilationTestData.MethodData methodData)
 {
     return(Cci.CustomDebugInfoWriter.GetEncDebugInfoForLocals(methodData.ILBuilder.LocalSlotManager.LocalsInOrder()));
 }
Ejemplo n.º 16
0
        internal static void VerifyIL(this CompilationTestData.MethodData method, string expectedIL)
        {
            string actualIL = GetMethodIL(method);

            AssertEx.AssertEqualToleratingWhitespaceDifferences(expectedIL, actualIL);
        }
Ejemplo n.º 17
0
 internal abstract string VisualizeRealIL(IModuleSymbol peModule, CompilationTestData.MethodData methodData);
Ejemplo n.º 18
0
        internal static ImmutableArray <string> GetLocalNames(CompilationTestData.MethodData methodData)
        {
            var locals = methodData.ILBuilder.LocalSlotManager.LocalsInOrder();

            return(locals.SelectAsArray(l => l.Name));
        }
Ejemplo n.º 19
0
        private static bool TryGetMethodData(ImmutableDictionary <string, CompilationTestData.MethodData> map, string qualifiedMethodName, out CompilationTestData.MethodData methodData)
        {
            if (map.TryGetValue(qualifiedMethodName, out methodData))
            {
                return(true);
            }

            // caller may not have specified parameter list, so try to match parameterless method
            if (map.TryGetValue(qualifiedMethodName + "()", out methodData))
            {
                return(true);
            }

            // now try to match single method with any parameter list
            var keys = map.Keys.Where(k => k.StartsWith(qualifiedMethodName + "(", StringComparison.Ordinal));

            if (keys.Count() == 1)
            {
                methodData = map[keys.First()];
                return(true);
            }
            else if (keys.Count() > 1)
            {
                throw new AmbiguousMatchException(
                          "Could not determine best match for method named: " + qualifiedMethodName + Environment.NewLine +
                          string.Join(Environment.NewLine, keys.Select(s => "    " + s)) + Environment.NewLine);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 20
0
 internal abstract string VisualizeRealIL(IModuleSymbol peModule, CompilationTestData.MethodData methodData, IReadOnlyDictionary <int, string> markers);
Ejemplo n.º 21
0
        internal static bool TryGetMethodData(this CompilationTestData data, string qualifiedMethodName, out CompilationTestData.MethodData methodData)
        {
            var map = data.GetMethodsByName();

            return(TryGetMethodData(map, qualifiedMethodName, out methodData));
        }
Ejemplo n.º 22
0
 internal override string VisualizeRealIL(IModuleSymbol peModule, CompilationTestData.MethodData methodData)
 {
     return(VisualizeRealIL((PEModuleSymbol)peModule, methodData));
 }
Ejemplo n.º 23
0
     > EncDebugInfoProvider(this CompilationTestData.MethodData methodData)
 {
     return(_ => methodData.GetEncDebugInfo());
 }