Skip to content

Commit 43988ca

Browse files
authored
Merge pull request #36 from nedwardsnae/compilation_database
Create a JSON compilation database to store the compiler invocation arguments
2 parents b2f328d + 74058ae commit 43988ca

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

StructLayout/Shared/Common/LayoutParser.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6+
using System.Text;
67
using System.Threading.Tasks;
78
using System.Windows;
89
using System.Windows.Media;
10+
using Newtonsoft.Json;
911

1012
namespace StructLayout
1113
{
@@ -562,17 +564,42 @@ public async Task<ParseResult> ParseClangAsync(ProjectProperties projProperties,
562564

563565
string clangCmd = language + archStr + standard + flags + defines + includes + forceInc + workDir + extra;
564566

565-
string outputPath = OutputDirectory + @"tempResult.slbin";
566-
string contextCmd = AdjustPath(location.Filename) + " -r=" + location.Line + " -c=" + location.Column + " -o=" + AdjustPath(outputPath);
567+
string outputPath = Path.Combine(OutputDirectory, "tempResult.slbin");
568+
569+
string compileCommandsDir = Path.Combine(OutputDirectory, "compile_commands");
570+
errorStr = CreateDirectory(compileCommandsDir);
571+
if (errorStr != null)
572+
{
573+
return new ParseResult { Status = ParseResult.StatusCode.InvalidOutputDir, ParserLog = errorStr };
574+
}
575+
576+
string compileCommandsFilePath = Path.Combine(compileCommandsDir, "compile_commands.json");
577+
using (StreamWriter compileCommandsFile = File.CreateText(compileCommandsFilePath))
578+
using (JsonTextWriter writer = new JsonTextWriter(compileCommandsFile))
579+
{
580+
writer.Formatting = Formatting.Indented;
581+
writer.WriteStartArray();
582+
writer.WriteStartObject();
583+
if (projProperties.WorkingDirectory != null)
584+
{
585+
writer.WritePropertyName("directory");
586+
writer.WriteValue(projProperties.WorkingDirectory);
587+
}
588+
writer.WritePropertyName("command");
589+
writer.WriteValue($"clang {clangCmd} {AdjustPath(location.Filename)}");
590+
writer.WritePropertyName("file");
591+
writer.WriteValue(location.Filename);
592+
}
567593

568-
string toolCmd = contextCmd + " --" + clangCmd;
594+
string toolCmd = $"-r={location.Line} -c={location.Column} -o={AdjustPath(outputPath)} -p {AdjustPath(compileCommandsDir)} {AdjustPath(location.Filename)}";
569595

570596
OutputLog.Focus();
571597
OutputLog.Log("Looking for structures at " + location.Filename + ":" + location.Line + ":" + location.Column+"...");
572598

573599
if (PrintCommandLine)
574600
{
575-
OutputLog.Log("CLANG ARGUMENTS: " + clangCmd);
601+
OutputLog.Log($"TOOL ARGUMENTS: {toolCmd}");
602+
OutputLog.Log($"CLANG ARGUMENTS: {clangCmd}");
576603
//OutputLog.Log("GENERATED FILE: " + outputPath);
577604
}
578605

0 commit comments

Comments
 (0)