Example #1
0
        private void Analyze(IList<CodeProject> projects, bool ignoreCache, string settingsPath)
        {
            Param.AssertNotNull(projects, "projects");
            Param.Ignore(ignoreCache);
            Param.Ignore(settingsPath);

            // Indicate that we're analyzing.
            lock (this)
            {
                this.analyzing = true;
                this.cancel = false;
            }

            // Get the CPU count.
            #if DEBUGTHREADING
            // For debugging, only create a single worker thread.
            int threadCount = 1;
            #else
            // Create a maximum of two worker threads.
            int threadCount = Math.Max(GetCpuCount(), 2);
            #endif

            try
            {
                // Intialize each of the parsers.
                foreach (SourceParser parser in this.parsers.Values)
                {
                    parser.PreParse();

                    // Initialize each of the enabled rules dictionaries for the analyzers.
                    foreach (SourceAnalyzer analyzer in parser.Analyzers)
                    {
                        analyzer.PreAnalyze();
                    }
                }

                // Reads and writes the results cache.
                ResultsCache resultsCache = null;

                if (this.writeResultsCache)
                {
                    resultsCache = new ResultsCache(this);
                }

                // Create a data object which will passed to each worker.
                StyleCopThread.Data data = new StyleCopThread.Data(
                    this, projects, resultsCache, ignoreCache, settingsPath);

                // Initialize each of the projects before analysis.
                foreach (CodeProject project in projects)
                {
                    StyleCopCore.InitializeProjectForAnalysis(project, data, resultsCache);
                }

                // Run until each of the parsers have completely finished analyzing all of the files.
                while (!this.Cancel)
                {
                    // Reset the file enumeration index.
                    data.ResetEmumerator();

                    // Run the worker threads and wait for them to complete.
                    if (this.RunWorkerThreads(data, threadCount))
                    {
                        // Analysis of all files has been completed.
                        break;
                    }

                    // Increment the pass number for the next round.
                    ++data.PassNumber;
                }

                // Save the cache files back to the disk.
                if (resultsCache != null)
                {
                    resultsCache.Flush();
                }

                // Finalize each of the parsers.
                foreach (SourceParser parser in this.parsers.Values)
                {
                    parser.PostParse();
                }

                // Clear the enabled rules lists from all analyzers since they are no longer needed.
                foreach (SourceParser parser in this.Parsers)
                {
                    foreach (SourceAnalyzer analyzer in parser.Analyzers)
                    {
                        analyzer.PostAnalyze();
                    }
                }
            }
            catch (OutOfMemoryException)
            {
                // Don't log OutOfMemoryExceptions since there is no memory!
                throw;
            }
            catch (ThreadAbortException)
            {
                // The thread is being aborted. Stop analyzing the source files.
            }
            catch (Exception ex)
            {
                // We catch all exceptions here so that we can log a violation.
                Debug.Assert(false, "Unhandled exception while analyzing files: " + ex.Message);
                this.coreParser.AddViolation(null, 1, Rules.ExceptionOccurred, ex.GetType(), ex.Message);

                // Do not re-throw the exception as this can crash Visual Studio or the build system that StyleCop is running under.
            }
            finally
            {
                // Indicate that we're done analyzing.
                lock (this)
                {
                    this.analyzing = false;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the StyleCopThreadCompletedEventArgs class.
 /// </summary>
 /// <param name="data">The thread data.</param>
 public StyleCopThreadCompletedEventArgs(StyleCopThread.Data data)
 {
     Param.AssertNotNull(data, "data");
     this.data = data;
 }
Example #3
0
 private void Analyze(IList<CodeProject> projects, bool ignoreCache, string settingsPath)
 {
     lock (this)
     {
         this.analyzing = true;
         this.cancel = false;
     }
     int count = Math.Max(GetCpuCount(), 2);
     try
     {
         foreach (SourceParser parser in this.parsers.Values)
         {
             parser.PreParse();
             foreach (SourceAnalyzer analyzer in parser.Analyzers)
             {
                 analyzer.EnabledRules = new Dictionary<CodeProject, Dictionary<string, Rule>>();
                 analyzer.PreAnalyze();
             }
         }
         ResultsCache resultsCache = null;
         if (this.writeResultsCache)
         {
             resultsCache = new ResultsCache(this);
         }
         StyleCopThread.Data data = new StyleCopThread.Data(this, projects, resultsCache, ignoreCache, settingsPath);
         foreach (CodeProject project in projects)
         {
             InitializeProjectForAnalysis(project, data, resultsCache);
         }
         while (!this.Cancel)
         {
             data.ResetEmumerator();
             if (this.RunWorkerThreads(data, count))
             {
                 break;
             }
             data.PassNumber++;
         }
         if (resultsCache != null)
         {
             resultsCache.Flush();
         }
         foreach (SourceParser parser2 in this.parsers.Values)
         {
             parser2.PostParse();
         }
         foreach (SourceParser parser3 in this.Parsers)
         {
             foreach (SourceAnalyzer analyzer2 in parser3.Analyzers)
             {
                 analyzer2.EnabledRules = null;
                 analyzer2.PostAnalyze();
             }
         }
     }
     catch (OutOfMemoryException)
     {
         throw;
     }
     catch (ThreadAbortException)
     {
     }
     catch (Exception exception)
     {
         this.coreParser.AddViolation(null, 1, Rules.ExceptionOccurred, new object[] { exception.GetType(), exception.Message });
     }
     finally
     {
         lock (this)
         {
             this.analyzing = false;
         }
     }
 }