Exemplo n.º 1
0
            /// <summary>
            /// Initializes a new instance of the Data class.
            /// </summary>
            /// <param name="core">The StyleCop core instance.</param>
            /// <param name="codeProjects">The list of code projects to analyze.</param>
            /// <param name="resultsCache">The results cache.</param>
            /// <param name="context">The run context.</param>
            /// <param name="autoSaveMode">Indicates whether to auto-save the document back to the source, if autoFixMode is true.</param>
            /// <param name="ignoreResultsCache">True to ignore the results cache.</param>
            /// <param name="settingsPath">The path to the settings to use during analysis.</param>
            public Data(
                StyleCopCore core,
                IList <CodeProject> codeProjects,
                ResultsCache resultsCache,
                RunContext context,
                bool autoSaveMode,
                bool ignoreResultsCache,
                string settingsPath)
            {
                Param.AssertNotNull(core, "core");
                Param.AssertNotNull(codeProjects, "codeProjects");
                Param.Ignore(resultsCache);
                Param.Ignore(context);
                Param.Ignore(autoSaveMode);
                Param.Ignore(ignoreResultsCache);
                Param.Ignore(settingsPath);

                this.core               = core;
                this.projects           = codeProjects;
                this.cache              = resultsCache;
                this.context            = context;
                this.autoSaveMode       = autoSaveMode;
                this.ignoreResultsCache = ignoreResultsCache;
                this.settingsPath       = settingsPath;
            }
Exemplo n.º 2
0
        private void Analyze(IList<CodeProject> projects, bool ignoreCache, string settingsPath)
        {
            Param.AssertNotNull(projects, "projects");
            Param.Ignore(ignoreCache);
            Param.Ignore(settingsPath);
            StyleCopTrace.In(projects, ignoreCache, settingsPath);

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

            //// TODO When we move to FRamework 4 we can re-introduce the threads and use the CountdownEvent to synchronize.
            //// 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.Min(CpuCount, 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;
                }
            }

            StyleCopTrace.Out();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes the project to prepare it for analysis.
        /// </summary>
        /// <param name="project">The project to initialize.</param>
        /// <param name="data">The analysis data object.</param>
        /// <param name="cache">The file cache.</param>
        private static void InitializeProjectForAnalysis(CodeProject project, StyleCopThread.Data data, ResultsCache cache)
        {
            Param.AssertNotNull(project, "project");
            Param.AssertNotNull(data, "data");
            Param.Ignore(cache);

            // Get the status object for the project.
            ProjectStatus projectStatus = data.GetProjectStatus(project);
            Debug.Assert(projectStatus != null, "There is no status for the given project.");

            // Load the settings for the project. If the project already contains settings, use those. 
            // Otherwise, load them from scratch.
            if (!project.SettingsLoaded)
            {
                project.Settings = data.GetSettings(project);
                project.SettingsLoaded = true;
            }

            StyleCopCore.CheckForStyleCopUpdate(project);

            // Load the project configuration from the cache and compare it to the
            // current project configuration.
            string configuration = cache == null ? null : cache.LoadProject(project);
            if (configuration == null)
            {
                projectStatus.IgnoreResultsCache = true;
            }
            else
            {
                projectStatus.IgnoreResultsCache = !StyleCopCore.CompareCachedConfiguration(
                    project.Configuration, configuration);
            }

            if (cache != null && project.WriteCache)
            {
                cache.SaveProject(project);
            }
        }
Exemplo n.º 4
0
            /// <summary>
            /// Initializes a new instance of the Data class.
            /// </summary>
            /// <param name="core">The StyleCop core instance.</param>
            /// <param name="codeProjects">The list of code projects to analyze.</param>
            /// <param name="resultsCache">The results cache.</param>
            /// <param name="context">The run context.</param>
            /// <param name="autoSaveMode">Indicates whether to auto-save the document back to the source, if autoFixMode is true.</param>
            /// <param name="ignoreResultsCache">True to ignore the results cache.</param>
            /// <param name="settingsPath">The path to the settings to use during analysis.</param>
            public Data(
                StyleCopCore core, 
                IList<CodeProject> codeProjects,
                ResultsCache resultsCache,
                RunContext context,
                bool autoSaveMode,
                bool ignoreResultsCache, 
                string settingsPath)
            {
                Param.AssertNotNull(core, "core");
                Param.AssertNotNull(codeProjects, "codeProjects");
                Param.Ignore(resultsCache);
                Param.Ignore(context);
                Param.Ignore(autoSaveMode);
                Param.Ignore(ignoreResultsCache);
                Param.Ignore(settingsPath);

                this.core = core;
                this.projects = codeProjects;
                this.cache = resultsCache;
                this.context = context;
                this.autoSaveMode = autoSaveMode;
                this.ignoreResultsCache = ignoreResultsCache;
                this.settingsPath = settingsPath;
            }