Пример #1
0
        public override void analyze(List <SourceFile> filesToAnalyze, OutputWindowPane outputWindow, bool is64bitConfiguration,
                                     bool isDebugConfiguration, bool analysisOnSavedFile)
        {
            if (filesToAnalyze.Count == 0)
            {
                return;
            }

            Debug.Assert(_numCores > 0);
            String cppheckargs = Properties.Settings.Default.DefaultArguments;

            if (Properties.Settings.Default.SeveritiesString.Length != 0)
            {
                cppheckargs += " --enable=" + Properties.Settings.Default.SeveritiesString;
            }

            HashSet <string> suppressions = new HashSet <string>(Properties.Settings.Default.SuppressionsString.Split(','));

            suppressions.Add("unmatchedSuppression");

            HashSet <string> skippedFilesMask   = new HashSet <string>();
            HashSet <string> skippedIncludeMask = new HashSet <string>();

            SuppressionsInfo unitedSuppressionsInfo = readSuppressions(ICodeAnalyzer.SuppressionStorage.Global);

            unitedSuppressionsInfo.UnionWith(readSuppressions(ICodeAnalyzer.SuppressionStorage.Solution));

            // Creating the list of all different project locations (no duplicates)
            HashSet <string> projectPaths = new HashSet <string>();           // enforce uniqueness on the list of project paths

            foreach (var file in filesToAnalyze)
            {
                projectPaths.Add(file.BaseProjectPath);
            }

            Debug.Assert(projectPaths.Count == 1);
            _projectBasePath = projectPaths.First();
            _projectName     = filesToAnalyze[0].ProjectName;

            // Creating the list of all different suppressions (no duplicates)
            foreach (var path in projectPaths)
            {
                unitedSuppressionsInfo.UnionWith(readSuppressions(SuppressionStorage.Project, path, filesToAnalyze[0].ProjectName));
            }

            cppheckargs += (" --relative-paths=\"" + filesToAnalyze[0].BaseProjectPath + "\"");
            cppheckargs += (" -j " + _numCores.ToString());
            if (Properties.Settings.Default.InconclusiveChecksEnabled)
            {
                cppheckargs += " --inconclusive ";
            }

            suppressions.UnionWith(unitedSuppressionsInfo.SuppressionLines);
            foreach (string suppression in suppressions)
            {
                if (!String.IsNullOrWhiteSpace(suppression))
                {
                    cppheckargs += (" --suppress=" + suppression);
                }
            }

            // We only add include paths once, and then specify a set of files to check
            HashSet <string> includePaths = new HashSet <string>();

            foreach (var file in filesToAnalyze)
            {
                if (!matchMasksList(file.FilePath, unitedSuppressionsInfo.SkippedFilesMask))
                {
                    includePaths.UnionWith(file.IncludePaths);
                }
            }

            includePaths.Add(filesToAnalyze[0].BaseProjectPath);             // Fix for #60

            foreach (string path in includePaths)
            {
                if (!matchMasksList(path, unitedSuppressionsInfo.SkippedIncludesMask))
                {
                    String includeArgument = " -I\"" + path + "\"";
                    cppheckargs = cppheckargs + " " + includeArgument;
                }
            }

            foreach (SourceFile file in filesToAnalyze)
            {
                if (!matchMasksList(file.FileName, unitedSuppressionsInfo.SkippedFilesMask))
                {
                    cppheckargs += " \"" + file.FilePath + "\"";
                }
            }

            if ((analysisOnSavedFile && Properties.Settings.Default.FileOnlyCheckCurrentConfig) ||
                (!analysisOnSavedFile && Properties.Settings.Default.ProjectOnlyCheckCurrentConfig))                 // Only checking current macros configuration (for speed)
            {
                cppheckargs = cppheckargs.Replace("--force", "");
                // Creating the list of all different macros (no duplicates)
                HashSet <string> macros = new HashSet <string>();
                macros.Add("__cplusplus=199711L");                 // At least in VS2012, this is still 199711L
                // Assuming all files passed here are from the same project / same toolset, which should be true, so peeking the first file for global settings
                switch (filesToAnalyze[0].vcCompilerVersion)
                {
                case SourceFile.VCCompilerVersion.vc2003:
                    macros.Add("_MSC_VER=1310");
                    break;

                case SourceFile.VCCompilerVersion.vc2005:
                    macros.Add("_MSC_VER=1400");
                    break;

                case SourceFile.VCCompilerVersion.vc2008:
                    macros.Add("_MSC_VER=1500");
                    break;

                case SourceFile.VCCompilerVersion.vc2010:
                    macros.Add("_MSC_VER=1600");
                    break;

                case SourceFile.VCCompilerVersion.vc2012:
                    macros.Add("_MSC_VER=1700");
                    break;

                case SourceFile.VCCompilerVersion.vc2013:
                    macros.Add("_MSC_VER=1800");
                    break;

                case SourceFile.VCCompilerVersion.vcFuture:
                    macros.Add("_MSC_VER=1900");
                    break;
                }

                foreach (var file in filesToAnalyze)
                {
                    macros.UnionWith(file.Macros);
                }
                macros.Add("WIN32");
                macros.Add("_WIN32");

                if (is64bitConfiguration)
                {
                    macros.Add("_M_X64");
                    macros.Add("_WIN64");
                }
                else
                {
                    macros.Add("_M_IX86");
                }

                if (isDebugConfiguration)
                {
                    macros.Add("_DEBUG");
                }

                foreach (string macro in macros)
                {
                    if (!String.IsNullOrEmpty(macro) && !macro.Contains(" ") /* macros with spaces are invalid in VS */)
                    {
                        String macroArgument = " -D" + macro;
                        cppheckargs += macroArgument;
                    }
                }

                HashSet <string> macrosToUndefine = new HashSet <string>();
                foreach (var file in filesToAnalyze)
                {
                    macrosToUndefine.UnionWith(file.MacrosToUndefine);
                }

                foreach (string macro in macrosToUndefine)
                {
                    if (!String.IsNullOrEmpty(macro) && !macro.Contains(" ") /* macros with spaces are invalid in VS */)
                    {
                        String macroUndefArgument = " -U" + macro;
                        cppheckargs += macroUndefArgument;
                    }
                }
            }
            else if (!cppheckargs.Contains("--force"))
            {
                cppheckargs += " --force";
            }

            string analyzerPath = Properties.Settings.Default.CPPcheckPath;

            while (!File.Exists(analyzerPath))
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "cppcheck executable|cppcheck.exe";
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                analyzerPath = dialog.FileName;
            }

            Properties.Settings.Default.CPPcheckPath = analyzerPath;
            Properties.Settings.Default.Save();
            run(analyzerPath, cppheckargs, outputWindow);
        }
        private string getCPPCheckArgs(ConfiguredFiles configuredFiles, bool analysisOnSavedFile, bool multipleProjects, string tempFileName)
        {
            Debug.Assert(_numCores > 0);
            String cppheckargs = Properties.Settings.Default.DefaultArguments;

            if (Properties.Settings.Default.SeveritiesString.Length != 0)
            {
                cppheckargs += " --enable=" + Properties.Settings.Default.SeveritiesString;
            }

            HashSet <string> suppressions = new HashSet <string>(Properties.Settings.Default.SuppressionsString.Split(','));

            suppressions.Add("unmatchedSuppression");

            HashSet <string> skippedFilesMask   = new HashSet <string>();
            HashSet <string> skippedIncludeMask = new HashSet <string>();

            SuppressionsInfo unitedSuppressionsInfo = readSuppressions(ICodeAnalyzer.SuppressionStorage.Global);

            unitedSuppressionsInfo.UnionWith(readSuppressions(ICodeAnalyzer.SuppressionStorage.Solution));

            var filesToAnalyze = configuredFiles.Files;
            // Creating the list of all different project locations (no duplicates)
            HashSet <string> projectPaths = new HashSet <string>();           // enforce uniqueness on the list of project paths

            foreach (var file in filesToAnalyze)
            {
                projectPaths.Add(file.BaseProjectPath);
            }

            Debug.Assert(projectPaths.Count == 1);
            _projectBasePath = projectPaths.First();
            _projectName     = filesToAnalyze[0].ProjectName;

            // Creating the list of all different suppressions (no duplicates)
            foreach (var path in projectPaths)
            {
                unitedSuppressionsInfo.UnionWith(readSuppressions(SuppressionStorage.Project, path, filesToAnalyze[0].ProjectName));
            }

            if (!multipleProjects)
            {
                cppheckargs += (" --relative-paths=\"" + filesToAnalyze[0].BaseProjectPath + "\"");
            }
            cppheckargs += (" -j " + _numCores.ToString());
            if (Properties.Settings.Default.InconclusiveChecksEnabled)
            {
                cppheckargs += " --inconclusive ";
            }

            suppressions.UnionWith(unitedSuppressionsInfo.SuppressionLines);
            foreach (string suppression in suppressions)
            {
                if (!String.IsNullOrWhiteSpace(suppression))
                {
                    cppheckargs += (" --suppress=" + suppression);
                }
            }

            if (!(analysisOnSavedFile && Properties.Settings.Default.IgnoreIncludePaths))
            {
                // We only add include paths once, and then specify a set of files to check
                HashSet <string> includePaths = new HashSet <string>();
                foreach (var file in filesToAnalyze)
                {
                    if (!matchMasksList(file.FilePath, unitedSuppressionsInfo.SkippedFilesMask))
                    {
                        includePaths.UnionWith(file.IncludePaths);
                    }
                }

                includePaths.Add(filesToAnalyze[0].BaseProjectPath);                 // Fix for #60

                foreach (string path in includePaths)
                {
                    if (!matchMasksList(path, unitedSuppressionsInfo.SkippedIncludesMask))
                    {
                        String includeArgument = " -I\"" + path + "\"";
                        cppheckargs = cppheckargs + " " + includeArgument;
                    }
                }
            }

            using (StreamWriter tempFile = new StreamWriter(tempFileName))
            {
                foreach (SourceFile file in filesToAnalyze)
                {
                    if (!matchMasksList(file.FileName, unitedSuppressionsInfo.SkippedFilesMask))
                    {
                        tempFile.WriteLine(file.FilePath);
                    }
                }
            }

            cppheckargs += " --file-list=\"" + tempFileName + "\"";

            if ((analysisOnSavedFile && Properties.Settings.Default.FileOnlyCheckCurrentConfig) ||
                (!analysisOnSavedFile && Properties.Settings.Default.ProjectOnlyCheckCurrentConfig))                 // Only checking current macros configuration (for speed)
            {
                cppheckargs = cppheckargs.Replace("--force", "");
                // Creating the list of all different macros (no duplicates)
                HashSet <string> macros = new HashSet <string>();
                macros.Add("__cplusplus=199711L");                 // At least in VS2012, this is still 199711L
                // Assuming all files passed here are from the same project / same toolset, which should be true, so peeking the first file for global settings
                switch (filesToAnalyze[0].vcCompilerVersion)
                {
                case SourceFile.VCCompilerVersion.vc2003:
                    macros.Add("_MSC_VER=1310");
                    break;

                case SourceFile.VCCompilerVersion.vc2005:
                    macros.Add("_MSC_VER=1400");
                    break;

                case SourceFile.VCCompilerVersion.vc2008:
                    macros.Add("_MSC_VER=1500");
                    break;

                case SourceFile.VCCompilerVersion.vc2010:
                    macros.Add("_MSC_VER=1600");
                    break;

                case SourceFile.VCCompilerVersion.vc2012:
                    macros.Add("_MSC_VER=1700");
                    break;

                case SourceFile.VCCompilerVersion.vc2013:
                    macros.Add("_MSC_VER=1800");
                    break;

                case SourceFile.VCCompilerVersion.vcFuture:
                    macros.Add("_MSC_VER=1900");
                    break;
                }

                foreach (var file in filesToAnalyze)
                {
                    macros.UnionWith(file.Macros);
                }
                macros.Add("WIN32");
                macros.Add("_WIN32");

                if (configuredFiles.is64bitConfiguration())
                {
                    macros.Add("_M_X64");
                    macros.Add("_WIN64");
                }
                else
                {
                    macros.Add("_M_IX86");
                }

                if (configuredFiles.isDebugConfiguration())
                {
                    macros.Add("_DEBUG");
                }

                foreach (string macro in macros)
                {
                    if (!String.IsNullOrEmpty(macro) && !macro.Contains(" ") /* macros with spaces are invalid in VS */)
                    {
                        String macroArgument = " -D" + macro;
                        cppheckargs += macroArgument;
                    }
                }

                HashSet <string> macrosToUndefine = new HashSet <string>();
                foreach (var file in filesToAnalyze)
                {
                    macrosToUndefine.UnionWith(file.MacrosToUndefine);
                }

                foreach (string macro in macrosToUndefine)
                {
                    if (!String.IsNullOrEmpty(macro) && !macro.Contains(" ") /* macros with spaces are invalid in VS */)
                    {
                        String macroUndefArgument = " -U" + macro;
                        cppheckargs += macroUndefArgument;
                    }
                }
            }
            else if (!cppheckargs.Contains("--force"))
            {
                cppheckargs += " --force";
            }

            return(cppheckargs);
        }