public static XDocument TryParseXmlAnnotationDoc(
            [NotNull] TextReader source,
            bool useFullTypeName,
            [NotNull] CompetitionState competitionState,
            [NotNull] string sourceDescription)
        {
            Code.NotNull(source, nameof(source));
            Code.NotNull(competitionState, nameof(competitionState));
            Code.NotNullNorEmpty(sourceDescription, nameof(sourceDescription));

            XDocument xmlAnnotationDoc;

            try
            {
                using (var reader = XmlReader.Create(source, GetXmlReaderSettings()))
                {
                    xmlAnnotationDoc = XDocument.Load(reader);
                    if (useFullTypeName)
                    {
                        MarkAsUsesFullTypeName(xmlAnnotationDoc);
                    }
                }
            }
            catch (ArgumentException ex)
            {
                competitionState.WriteExceptionMessage(
                    MessageSource.Analyser, MessageSeverity.SetupError,
                    $"{sourceDescription}: Could not parse annotation.", ex);
                return(null);
            }
            catch (XmlException ex)
            {
                competitionState.WriteExceptionMessage(
                    MessageSource.Analyser, MessageSeverity.SetupError,
                    $"{sourceDescription}: Could not parse annotation.", ex);
                return(null);
            }

            var rootNode = xmlAnnotationDoc.Element(CompetitionBenchmarksRootNode);

            if (rootNode == null)
            {
                competitionState.WriteMessage(
                    MessageSource.Analyser, MessageSeverity.SetupError,
                    $"{sourceDescription}: root node {CompetitionBenchmarksRootNode} not found.");

                return(null);
            }

            return(xmlAnnotationDoc);
        }
Exemple #2
0
            /// <summary>Tries lo load the xml annotation document.</summary>
            /// <param name="file">The file with xml annotation.</param>
            /// <param name="useFullTypeName">Use full type name in XML annotations.</param>
            /// <param name="competitionState">State of the run.</param>
            /// <returns>The document with competition limits for the benchmark or <c>null</c> if none.</returns>
            public XDocument TryGetXmlAnnotation(
                [NotNull] string file,
                bool useFullTypeName,
                [NotNull] CompetitionState competitionState)
            {
                Code.NotNullNorEmpty(file, nameof(file));
                Code.NotNull(competitionState, nameof(competitionState));
                Code.AssertState(!_sourceLines.ContainsKey(file), $"File '{file}' already loaded as source lines.");

                return(_xmlAnnotations.GetOrAdd(
                           file, f =>
                {
                    try
                    {
                        using (var reader = File.OpenText(f))
                        {
                            return XmlAnnotations.TryParseXmlAnnotationDoc(
                                reader, useFullTypeName, competitionState,
                                $"XML annotation '{file}'");
                        }
                    }
                    catch (IOException ex)
                    {
                        competitionState.WriteExceptionMessage(
                            MessageSource.Analyser, MessageSeverity.SetupError,
                            $"Could not access file '{file}'.", ex);

                        return null;
                    }
                }));
            }
Exemple #3
0
            public IReadOnlyList <string> TryGetFileLines(
                [NotNull] string file,
                [NotNull] CompetitionState competitionState)
            {
                Code.NotNullNorEmpty(file, nameof(file));
                Code.NotNull(competitionState, nameof(competitionState));
                Code.AssertState(!_xmlAnnotations.ContainsKey(file), $"File '{file}' already loaded as XML annotation.");

                return(_sourceLines.GetOrAdd(
                           file, f =>
                {
                    try
                    {
                        return File.ReadAllLines(f);
                    }
                    catch (IOException ex)
                    {
                        competitionState.WriteExceptionMessage(
                            MessageSource.Analyser, MessageSeverity.SetupError,
                            $"Could not access file '{file}'.", ex);

                        return Array <string> .Empty;
                    }
                }));
            }
Exemple #4
0
            // ReSharper disable once SuggestBaseTypeForParameter
            private static ISymUnmanagedMethod TryGetMethodSymbols(MethodBase method, CompetitionState competitionState)
            {
                try
                {
                    // ReSharper disable once PossibleNullReferenceException
                    var assembly          = method.DeclaringType.Assembly;
                    var codeBase          = new Uri(assembly.CodeBase).LocalPath;
                    var codeBaseDirectory = Path.GetDirectoryName(codeBase);

                    var dispenser = (IMetaDataDispenser) new CorMetaDataDispenser();
                    var import    = dispenser.OpenScope(codeBase, 0, typeof(IMetaDataImportStub).GUID);
                    var binder    = (ISymUnmanagedBinder) new CorSymBinder();

                    ISymUnmanagedReader reader;
                    var hr = binder.GetReaderForFile(import, codeBase, codeBaseDirectory, out reader);
                    ThrowExceptionForHR(hr);

                    ISymUnmanagedMethod methodSymbols;
                    hr = reader.GetMethod(method.MetadataToken, out methodSymbols);
                    ThrowExceptionForHR(hr);
                    return(methodSymbols);
                }
                catch (COMException ex)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    competitionState.WriteExceptionMessage(
                        MessageSource.Analyser, MessageSeverity.ExecutionError,
                        $"Method {method.DeclaringType.Name}.{method.Name}, no PDB data available.", ex);

                    return(null);
                }
            }
Exemple #5
0
            private static bool TryGetDocsAndLines(
                ISymUnmanagedMethod methodSymbols,
                CompetitionState competitionState,
                out ISymUnmanagedDocument[] documents,
                out int[] startLines)
            {
                documents  = Array <ISymUnmanagedDocument> .Empty;
                startLines = Array <int> .Empty;
                try
                {
                    int numAvailable;
                    var hr = methodSymbols.GetSequencePointCount(out numAvailable);
                    ThrowExceptionForHR(hr);

                    documents  = new ISymUnmanagedDocument[numAvailable];
                    startLines = new int[numAvailable];

                    if (numAvailable > 0)
                    {
                        var offsets      = new int[numAvailable];
                        var startColumns = new int[numAvailable];
                        var endLines     = new int[numAvailable];
                        var endColumns   = new int[numAvailable];

                        int numRead;
                        hr = methodSymbols.GetSequencePoints(
                            numAvailable, out numRead,
                            offsets, documents,
                            startLines, startColumns,
                            endLines, endColumns);
                        ThrowExceptionForHR(hr);

                        if (numRead != numAvailable)
                        {
                            throw new COMException($"Read only {numRead} of {numAvailable} sequence points.");
                        }
                    }
                }
                catch (COMException ex)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    competitionState.WriteExceptionMessage(
                        MessageSource.Analyser, MessageSeverity.ExecutionError,
                        "Could not parse method symbols.", ex);

                    return(false);
                }

                return(startLines.Length > 0);
            }