Exemplo n.º 1
0
        /// <summary>
        /// Runs the list of analyzers against the given document.
        /// </summary>
        /// <param name="document">The document to analyze.</param>
        /// <param name="parser">The parser that created the document.</param>
        /// <param name="analyzers">The list of analyzsers to run against the document.</param>
        private void RunAnalyzers(
            ICodeDocument document, SourceParser parser, IEnumerable <SourceAnalyzer> analyzers)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers, "analyzers");

            if (analyzers != null)
            {
                if (parser.SkipAnalysisForDocument(document))
                {
                    this.data.Core.SignalOutput(
                        MessageImportance.Normal,
                        string.Format(CultureInfo.CurrentCulture, "Skipping {0}...", document.SourceCode.Name));
                }
                else
                {
                    // Loop through each of the analyzers attached to the parser.
                    foreach (SourceAnalyzer analyzer in analyzers)
                    {
                        // Make sure the user hasn't cancelled us.
                        if (this.data.Core.Cancel)
                        {
                            break;
                        }

                        // Only run the analyzers associated with the current parser.
                        if (analyzer.Parser == parser)
                        {
                            SourceParser.ClearAnalyzerTags(document);
                            try
                            {
                                // Check whether we are running in auto-fix mode, in which case we should call the
                                // AutoFixDocument method on analyzers that support it. Otherwise, we are simply running
                                // the rules and we should call the AnalyzeDocument method instead.
                                if (this.data.RunContext.AutoFix)
                                {
                                    analyzer.AutoFixDocument(document);
                                }
                                else
                                {
                                    analyzer.AnalyzeDocument(document);
                                }
                            }
                            catch (System.Exception)
                            {
                                string details = string.Format(
                                    CultureInfo.CurrentCulture,
                                    "Exception thrown by analyzer '{0}' while processing '{1}'.",
                                    analyzer.Name,
                                    document.SourceCode.Path);

                                this.data.Core.SignalOutput(MessageImportance.High, details);
                                throw;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the list of analyzers against the given document.
        /// </summary>
        /// <param name="document">
        /// The document to analyze.
        /// </param>
        /// <param name="parser">
        /// The parser that created the document.
        /// </param>
        /// <param name="analyzers">
        /// The list of analyzers to run against the document.
        /// </param>
        private void RunAnalyzers(CodeDocument document, SourceParser parser, IEnumerable <SourceAnalyzer> analyzers)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers, "analyzers");
            StyleCopTrace.In(document, parser, analyzers);

            if (analyzers != null)
            {
                if (parser.SkipAnalysisForDocument(document.SourceCode))
                {
                    string format = string.Format(CultureInfo.CurrentCulture, "Skipping: {0} - {1}", document.SourceCode.Project.Location.SubstringAfterLast('\\'), GetRelativeFileName(document.SourceCode));
                    this.data.Core.SignalOutput(MessageImportance.Normal, format);
                }
                else
                {
                    // Loop through each of the parser's analyzers.
                    // Only call analyzers that are also in the enabled list.
                    foreach (SourceAnalyzer analyzer in parser.Analyzers)
                    {
                        SourceAnalyzer localAnalyzer = analyzer;
                        if (analyzers.Any(enabledAnalyzers => enabledAnalyzers.Id == localAnalyzer.Id))
                        {
                            // Make sure the user hasn't cancelled us.
                            if (this.data.Core.Cancel)
                            {
                                break;
                            }

                            SourceParser.ClearAnalyzerTags(document);
                            try
                            {
                                if (analyzer.DoAnalysis(document))
                                {
                                    analyzer.AnalyzeDocument(document);
                                }
                            }
                            catch (Exception ex)
                            {
                                StringBuilder details = new StringBuilder();
                                details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception thrown by analyzer '{0}' while processing '{1}'.", analyzer.Name, document.SourceCode.Path));

                                // Add exception message for help on bugfix.
                                if (!string.IsNullOrEmpty(ex.Message))
                                {
                                    details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception message : {0}", ex.Message));
                                }

                                this.data.Core.SignalOutput(MessageImportance.High, details.ToString());
                                throw;
                            }
                        }
                    }
                }
            }

            StyleCopTrace.Out();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Runs the list of analyzers against the given document.
        /// </summary>
        /// <param name="document">
        /// The document to analyze.
        /// </param>
        /// <param name="parser">
        /// The parser that created the document.
        /// </param>
        /// <param name="analyzers">
        /// The list of analyzers to run against the document.
        /// </param>
        private void RunAnalyzers(CodeDocument document, SourceParser parser, IEnumerable<SourceAnalyzer> analyzers)
        {
            Param.AssertNotNull(document, "document");
            Param.AssertNotNull(parser, "parser");
            Param.Ignore(analyzers, "analyzers");
            StyleCopTrace.In(document, parser, analyzers);

            if (analyzers != null)
            {
                if (parser.SkipAnalysisForDocument(document.SourceCode))
                {
                    string format = string.Format(CultureInfo.CurrentCulture, "Skipping: {0} - {1}", document.SourceCode.Project.Location.SubstringAfterLast('\\'), GetRelativeFileName(document.SourceCode));
                    this.data.Core.SignalOutput(MessageImportance.Normal, format);
                }
                else
                {
                    // Loop through each of the parser's analyzers. 
                    // Only call analyzers that are also in the enabled list.
                    foreach (SourceAnalyzer analyzer in parser.Analyzers)
                    {
                        SourceAnalyzer localAnalyzer = analyzer;
                        if (analyzers.Any(enabledAnalyzers => enabledAnalyzers.Id == localAnalyzer.Id))
                        {
                            // Make sure the user hasn't cancelled us.
                            if (this.data.Core.Cancel)
                            {
                                break;
                            }

                            SourceParser.ClearAnalyzerTags(document);
                            try
                            {
                                if (analyzer.DoAnalysis(document))
                                {
                                    analyzer.AnalyzeDocument(document);
                                }
                            }
                            catch (Exception ex)
                            {
                                StringBuilder details = new StringBuilder();
                                details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception thrown by analyzer '{0}' while processing '{1}'.", analyzer.Name, document.SourceCode.Path));

                                // Add exception message for help on bugfix.
                                if (!string.IsNullOrEmpty(ex.Message))
                                {
                                    details.AppendLine(string.Format(CultureInfo.CurrentCulture, "Exception message : {0}"));
                                }

                                this.data.Core.SignalOutput(MessageImportance.High, details.ToString());
                                throw;
                            }
                        }
                    }
                }
            }

            StyleCopTrace.Out();
        }