From 3d0abc2db87a7c08d3b4929ce1e2d9ad626ab659 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Fri, 14 Sep 2018 10:26:53 +0200 Subject: [PATCH 1/3] avoid a trailing comma after the last compile command --- .../Utility/QueuedFileWriter.cs | 2 +- .../Wizard/WindowCreateCDB.cs | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/SourcetrailExtension/Utility/QueuedFileWriter.cs b/SourcetrailExtension/Utility/QueuedFileWriter.cs index f7417cb..4fa7ee6 100644 --- a/SourcetrailExtension/Utility/QueuedFileWriter.cs +++ b/SourcetrailExtension/Utility/QueuedFileWriter.cs @@ -164,7 +164,7 @@ private void WriteQueueToFile(ref Queue messageQueue) while (messageQueue.Count > 0) { _messageWrittenCount++; - writer.WriteLine(messageQueue.Dequeue()); + writer.Write(messageQueue.Dequeue()); } writer.Close(); diff --git a/SourcetrailExtension/Wizard/WindowCreateCDB.cs b/SourcetrailExtension/Wizard/WindowCreateCDB.cs index bde18d9..851be74 100644 --- a/SourcetrailExtension/Wizard/WindowCreateCDB.cs +++ b/SourcetrailExtension/Wizard/WindowCreateCDB.cs @@ -201,25 +201,38 @@ private void CreateCompilationCommands(Utility.QueuedFileWriter fileWriter, EnvD { try { + bool isFirstCommand = true; + SolutionParser.SolutionParser solutionParser = new SolutionParser.SolutionParser(new VsPathResolver(_targetDir)); solutionParser.CreateCompileCommands( project, _configurationName, _platformName, _cStandard, _additionalClangOptions, _nonSystemIncludesUseAngleBrackets, (CompileCommand command) => { string serializedCommand = ""; + + if (!isFirstCommand) + { + serializedCommand += ",\n"; + } + else + { + isFirstCommand = false; + } + foreach (string line in command.SerializeToJson().Split('\n')) { serializedCommand += " " + line + "\n"; } + serializedCommand = serializedCommand.TrimEnd('\n'); - fileWriter.PushMessage(serializedCommand + ",\n"); + fileWriter.PushMessage(serializedCommand); } ); - if (lastProject) + if (!lastProject) { - fileWriter.PushMessage("\n"); + fileWriter.PushMessage(",\n"); } lock (_lockObject) From 34d9cd56e1b2c7796c0e7ee6de7086180db96217 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Fri, 14 Sep 2018 10:27:59 +0200 Subject: [PATCH 2/3] fix unnecessary comma between compile commands if project did not generate compile commands --- SourcetrailExtension/Wizard/WindowCreateCDB.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SourcetrailExtension/Wizard/WindowCreateCDB.cs b/SourcetrailExtension/Wizard/WindowCreateCDB.cs index 851be74..7bbbe3f 100644 --- a/SourcetrailExtension/Wizard/WindowCreateCDB.cs +++ b/SourcetrailExtension/Wizard/WindowCreateCDB.cs @@ -230,7 +230,8 @@ private void CreateCompilationCommands(Utility.QueuedFileWriter fileWriter, EnvD } ); - if (!lastProject) + if (!lastProject && // do not append a trailing comma after the last indexer command + !isFirstCommand) // do not append a comma if the project is empty { fileWriter.PushMessage(",\n"); } From 6b4f6cc0c3663af2a89f1e2fbd6d720bd243b360 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Fri, 14 Sep 2018 10:55:40 +0200 Subject: [PATCH 3/3] fix trailing comma still an issue if last project is empty --- .../SourcetrailExtension.csproj | 1 + .../Utility/CompileCommandFileWriter.cs | 65 +++++++++++++++++++ .../Wizard/WindowCreateCDB.cs | 40 ++---------- 3 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 SourcetrailExtension/Utility/CompileCommandFileWriter.cs diff --git a/SourcetrailExtension/SourcetrailExtension.csproj b/SourcetrailExtension/SourcetrailExtension.csproj index def2a85..9abb7f8 100644 --- a/SourcetrailExtension/SourcetrailExtension.csproj +++ b/SourcetrailExtension/SourcetrailExtension.csproj @@ -147,6 +147,7 @@ + diff --git a/SourcetrailExtension/Utility/CompileCommandFileWriter.cs b/SourcetrailExtension/Utility/CompileCommandFileWriter.cs new file mode 100644 index 0000000..aa455cd --- /dev/null +++ b/SourcetrailExtension/Utility/CompileCommandFileWriter.cs @@ -0,0 +1,65 @@ +/* + * Copyright 2018 Coati Software KG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using CoatiSoftware.SourcetrailExtension.SolutionParser; + +namespace CoatiSoftware.SourcetrailExtension.Utility +{ + class CompileCommandFileWriter + { + private bool _isFirstCommand; + private QueuedFileWriter _fileWriter = null; + + public CompileCommandFileWriter(string fileName, string targetDirectory) + { + _isFirstCommand = true; + _fileWriter = new QueuedFileWriter(fileName, targetDirectory); + } + + public void PushCommand(CompileCommand command) + { + string serializedCommand = ""; + + if (!_isFirstCommand) + { + serializedCommand += ",\n"; + } + else + { + _isFirstCommand = false; + } + + foreach (string line in command.SerializeToJson().Split('\n')) + { + serializedCommand += " " + line + "\n"; + } + + serializedCommand = serializedCommand.TrimEnd('\n'); + + _fileWriter.PushMessage(serializedCommand); + } + + public void StartWorking() + { + _fileWriter.StartWorking(); + } + + public void StopWorking() + { + _fileWriter.StopWorking(); + } + } +} diff --git a/SourcetrailExtension/Wizard/WindowCreateCDB.cs b/SourcetrailExtension/Wizard/WindowCreateCDB.cs index 7bbbe3f..27f4470 100644 --- a/SourcetrailExtension/Wizard/WindowCreateCDB.cs +++ b/SourcetrailExtension/Wizard/WindowCreateCDB.cs @@ -197,45 +197,17 @@ private CreationResult CreateCdb() return result; } - private void CreateCompilationCommands(Utility.QueuedFileWriter fileWriter, EnvDTE.Project project, ref int projectsProcessed, bool lastProject = false) + private void CreateCompilationCommands(Utility.CompileCommandFileWriter fileWriter, EnvDTE.Project project, ref int projectsProcessed) { try { - bool isFirstCommand = true; - SolutionParser.SolutionParser solutionParser = new SolutionParser.SolutionParser(new VsPathResolver(_targetDir)); solutionParser.CreateCompileCommands( project, _configurationName, _platformName, _cStandard, _additionalClangOptions, _nonSystemIncludesUseAngleBrackets, - (CompileCommand command) => { - string serializedCommand = ""; - - if (!isFirstCommand) - { - serializedCommand += ",\n"; - } - else - { - isFirstCommand = false; - } - - foreach (string line in command.SerializeToJson().Split('\n')) - { - serializedCommand += " " + line + "\n"; - } - - serializedCommand = serializedCommand.TrimEnd('\n'); - - fileWriter.PushMessage(serializedCommand); - } + (CompileCommand command) => { fileWriter.PushCommand(command); } ); - if (!lastProject && // do not append a trailing comma after the last indexer command - !isFirstCommand) // do not append a comma if the project is empty - { - fileWriter.PushMessage(",\n"); - } - lock (_lockObject) { projectsProcessed++; @@ -258,7 +230,7 @@ private void CreateCompilationDatabase() File.WriteAllText(_targetDir + "\\" + _fileName + ".json", ""); File.AppendAllText(_targetDir + "\\" + _fileName + ".json", "[\n"); - Utility.QueuedFileWriter fileWriter = new Utility.QueuedFileWriter(_fileName + ".json", _targetDir); + Utility.CompileCommandFileWriter fileWriter = new Utility.CompileCommandFileWriter(_fileName + ".json", _targetDir); fileWriter.StartWorking(); try @@ -268,7 +240,7 @@ private void CreateCompilationDatabase() List tasks = new List(); int projectsProcessed = 0; - foreach (EnvDTE.Project project in _projects.GetRange(0, _projects.Count - 1)) + foreach (EnvDTE.Project project in _projects.GetRange(0, _projects.Count)) { Logging.Logging.LogInfo("Scheduling project \"" + Logging.Obfuscation.NameObfuscator.GetObfuscatedName(project.Name) + "\" for parsing."); @@ -280,12 +252,8 @@ private void CreateCompilationDatabase() tasks.Add(task); } - int threadCount = System.Diagnostics.Process.GetCurrentProcess().Threads.Count; - Task.WaitAll(tasks.ToArray()); - CreateCompilationCommands(fileWriter, _projects[_projects.Count - 1], ref projectsProcessed, true); - fileWriter.StopWorking(); backgroundWorker1.ReportProgress(100, "Writing data to file. This might take several minutes...");