From 6b9a34475243de834597562e2d5f19b7bf47f533 Mon Sep 17 00:00:00 2001 From: VioletGiraffe Date: Fri, 29 May 2015 11:49:20 +0300 Subject: [PATCH] Bugfixes for multiple projects checking --- CPPCheckPlugin/AnalyzerCppcheck.cs | 54 ++++++++++++++++++++---------- CPPCheckPlugin/ICodeAnalyzer.cs | 13 +++++-- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/CPPCheckPlugin/AnalyzerCppcheck.cs b/CPPCheckPlugin/AnalyzerCppcheck.cs index d8c0d9e..1a889a8 100644 --- a/CPPCheckPlugin/AnalyzerCppcheck.cs +++ b/CPPCheckPlugin/AnalyzerCppcheck.cs @@ -27,9 +27,9 @@ public AnalyzerCppcheck() { DateTime fileModifiedDate = File.GetLastWriteTime(file); - if (fileModifiedDate.AddMinutes(60) < DateTime.Now) + if (fileModifiedDate.AddMinutes(120) < DateTime.Now) { - // File hasn't been written to in the last 60 mins, so it must be + // File hasn't been written to in the last 120 minutes, so it must be // from an earlier instance which didn't exit gracefully. File.Delete(file); } @@ -40,12 +40,10 @@ public AnalyzerCppcheck() ~AnalyzerCppcheck() { - // Delete the temp file. Doesn't throw an exception if the file was never - // created, so we don't need to worry about that. - File.Delete(tempFileName); + cleanupTempFiles(); } - private string getCPPCheckArgs(ConfiguredFiles configuredFiles, bool analysisOnSavedFile, bool multipleProjects, StreamWriter tempFile) + private string getCPPCheckArgs(ConfiguredFiles configuredFiles, bool analysisOnSavedFile, bool multipleProjects, string tempFileName) { Debug.Assert(_numCores > 0); String cppheckargs = Properties.Settings.Default.DefaultArguments; @@ -112,10 +110,13 @@ private string getCPPCheckArgs(ConfiguredFiles configuredFiles, bool analysisOnS } } - foreach (SourceFile file in filesToAnalyze) + using (StreamWriter tempFile = new StreamWriter(tempFileName)) { - if (!matchMasksList(file.FileName, unitedSuppressionsInfo.SkippedFilesMask)) - tempFile.WriteLine(file.FilePath); + foreach (SourceFile file in filesToAnalyze) + { + if (!matchMasksList(file.FileName, unitedSuppressionsInfo.SkippedFilesMask)) + tempFile.WriteLine(file.FilePath); + } } cppheckargs += " --file-list=\"" + tempFileName + "\""; @@ -209,11 +210,8 @@ public override void analyze(List allConfiguredFiles, OutputWin return; List cppheckargs = new List(); - using( StreamWriter tempFile = new StreamWriter(tempFileName) ) - { - foreach (var configuredFiles in allConfiguredFiles) - cppheckargs.Add(getCPPCheckArgs(configuredFiles, analysisOnSavedFile, allConfiguredFiles.Count > 1, tempFile)); - } + foreach (var configuredFiles in allConfiguredFiles) + cppheckargs.Add(getCPPCheckArgs(configuredFiles, analysisOnSavedFile, allConfiguredFiles.Count > 1, createNewTempFileName())); string analyzerPath = Properties.Settings.Default.CPPcheckPath; while (!File.Exists(analyzerPath)) @@ -361,17 +359,37 @@ protected override List parseOutput(String output) return result; } - protected override void analysisFinished() + protected override void analysisFinished(string arguments) { if (_unfinishedProblem != null) addProblemToToolwindow(_unfinishedProblem); - // Delete the temp file. Doesn't throw an exception if the file was never - // created, so we don't need to worry about that. + const string fileListPattern = "--file-list=\""; + int filenamePos = arguments.IndexOf(fileListPattern) + fileListPattern.Length; + int filenameLength = arguments.IndexOf('\"', filenamePos) - filenamePos; + string tempFileName = arguments.Substring(filenamePos, filenameLength); + File.Delete(tempFileName); } + + private void cleanupTempFiles() + { + // Delete the temp files. Doesn't throw an exception if the file was never + // created, so we don't need to worry about that. + foreach (string name in _tempFileNamesInUse) + File.Delete(name); + + _tempFileNamesInUse.Clear(); + } + + private string createNewTempFileName() + { + string name = Path.GetTempPath() + tempFilePrefix + "_" + Path.GetRandomFileName(); + _tempFileNamesInUse.Add(name); + return name; + } private Problem _unfinishedProblem = null; - private string tempFileName = Path.GetTempPath() + tempFilePrefix + "_" + Path.GetRandomFileName(); + private List _tempFileNamesInUse = new List(); } } diff --git a/CPPCheckPlugin/ICodeAnalyzer.cs b/CPPCheckPlugin/ICodeAnalyzer.cs index 611fdac..b527430 100644 --- a/CPPCheckPlugin/ICodeAnalyzer.cs +++ b/CPPCheckPlugin/ICodeAnalyzer.cs @@ -77,7 +77,7 @@ protected ICodeAnalyzer() protected abstract List parseOutput(String output); - protected abstract void analysisFinished(); + protected abstract void analysisFinished(string arguments); protected void run(string analyzerExePath, List arguments, OutputWindowPane outputPane) { @@ -210,7 +210,14 @@ private void startAnalyzerProcess(string analyzerExePath, string arguments) var timer = Stopwatch.StartNew(); // Start the process. process.Start(); - process.PriorityClass = ProcessPriorityClass.Idle; + + try + { + process.PriorityClass = ProcessPriorityClass.Idle; + } + catch (System.InvalidOperationException ex) + { + } onProgressUpdated(0); @@ -227,7 +234,7 @@ private void startAnalyzerProcess(string analyzerExePath, string arguments) } } timer.Stop(); - analysisFinished(); + analysisFinished(arguments); if (process.ExitCode != 0) _outputPane.OutputString(analyzerExePath + " has exited with code " + process.ExitCode.ToString() + "\n"); else