Skip to content

Commit

Permalink
style: improve indentation
Browse files Browse the repository at this point in the history
Signed-off-by: royalpinto007 <royalpinto007@gmail.com>
  • Loading branch information
royalpinto007 committed Oct 24, 2024
1 parent 643b1a2 commit e36e746
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 81 deletions.
87 changes: 43 additions & 44 deletions compiler/src/dmd/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void generateSarifReport(const ref Loc loc, const(char)* format, va_list ap, Err

// Create an OutBuffer to store the SARIF report
OutBuffer ob;
ob.doindent = true;

// Extract and clean the version string
const(char)* rawVersionChars = global.versionChars();
Expand All @@ -132,63 +133,61 @@ void generateSarifReport(const ref Loc loc, const(char)* format, va_list ap, Err

// Find the first non-numeric character after the version number
const(char)* nonNumeric = strchr(rawVersionChars, '-');
size_t length;
if (nonNumeric !is null) {
length = cast(size_t)(nonNumeric - rawVersionChars);
} else {
length = strlen(rawVersionChars);
}
size_t length = nonNumeric ? cast(size_t)(nonNumeric - rawVersionChars) : strlen(rawVersionChars);

// Build SARIF report
ob.level = 0;
ob.writestringln("{");
ob.level = 1;

// Build SARIF report using OutBuffer
ob.writestring("{\n");
ob.writestring(" \"version\": \"2.1.0\",\n");
ob.writestring(" \"$schema\": \"https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json\",\n");
ob.writestring(" \"runs\": [\n {\n");
ob.writestringln(`"version": "2.1.0",`);
ob.writestringln(`"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json",`);
ob.writestringln(`"runs": [{`);

// Tool Information
ob.writestring(" \"tool\": {\n");
ob.writestring(" \"driver\": {\n");
ob.printf(" \"name\": \"%s\",\n", global.compileEnv.vendor.ptr);
ob.printf(" \"version\": \"%.*s\",\n", cast(int)length, rawVersionChars);
ob.writestring(" \"informationUri\": \"https://dlang.org/dmd.html\"\n");
ob.writestring(" }\n");
ob.writestring(" },\n");
ob.level += 1;
ob.writestringln(`"tool": {`);
ob.writestringln(`"driver": {`);
ob.printf(`"name": "%s",`, global.compileEnv.vendor.ptr);
ob.printf(`"version": "%.*s",`, cast(int)length, rawVersionChars);
ob.writestringln(`"informationUri": "https://dlang.org/dmd.html"`);
ob.writestringln("}");
ob.writestringln("},");

// Invocation Information
ob.writestring(" \"invocations\": [\n");
ob.writestring(" {\n");
ob.writestring(" \"executionSuccessful\": false\n");
ob.writestring(" }\n");
ob.writestring(" ],\n");
ob.writestringln(`"invocations": [{`);
ob.writestringln(`"executionSuccessful": false`);
ob.writestringln("}],");

// Results Array
ob.writestring(" \"results\": [\n {\n");
ob.writestring(" \"ruleId\": \"DMD\",\n");
ob.printf(" \"message\": { \"text\": \"%s\" },\n", formattedMessage.ptr);
ob.writestringln(`"results": [{`);
ob.writestringln(`"ruleId": "DMD",`);
ob.printf(`"message": { "text": "%s" },`, formattedMessage.ptr);

// Location Information
ob.writestring(" \"locations\": [\n {\n");
ob.writestring(" \"physicalLocation\": {\n");
ob.writestring(" \"artifactLocation\": {\n");
ob.printf(" \"uri\": \"%s\"\n", loc.filename);
ob.writestring(" },\n");
ob.writestring(" \"region\": {\n");
ob.printf(" \"startLine\": %d,\n", loc.linnum);
ob.printf(" \"startColumn\": %d\n", loc.charnum);
ob.writestring(" }\n");
ob.writestring(" }\n");
ob.writestring(" }\n");
ob.writestring(" ]\n");
ob.writestring(" }\n ]\n");
ob.writestringln(`"locations": [{`);
ob.writestringln(`"physicalLocation": {`);
ob.writestringln(`"artifactLocation": {`);
ob.printf(`"uri": "%s"`, loc.filename);
ob.writestringln("},");
ob.writestringln(`"region": {`);
ob.printf(`"startLine": %d,`, loc.linnum);
ob.printf(`"startColumn": %d`, loc.charnum);
ob.writestringln("}");
ob.writestringln("}");
ob.writestringln("}]");
ob.writestringln("}]");

// Close the run and SARIF JSON
ob.writestring(" }\n ]\n");
ob.writestring("}\n");
ob.level -= 1;
ob.writestringln("}]");
ob.level = 0;
ob.writestringln("}");

// Extract the final null-terminated string and print it to stdout
const(char)* sarifOutput = ob.extractChars(); // Ensure null-terminated output
fputs(sarifOutput, stdout); // Output the SARIF report
fflush(stdout); // Ensure it gets written immediately
const(char)* sarifOutput = ob.extractChars();
fputs(sarifOutput, stdout);
fflush(stdout);
}

// Helper function to format error messages
Expand Down
60 changes: 23 additions & 37 deletions compiler/test/fail_compilation/sarif_test.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,29 @@ TEST_OUTPUT:
---
fail_compilation/sarif_test.d: Error: undefined identifier `x`
{
"version": "2.1.0",
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json",
"runs": [
{
"tool": {
"driver": {
"name": "Digital Mars D",
"version": "2.110.0",
"informationUri": "https://dlang.org/dmd.html"
}
},
"invocations": [
{
"executionSuccessful": false
}
],
"results": [
{
"ruleId": "DMD",
"message": { "text": "undefined identifier `x`" },
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "fail_compilation/sarif_test.d"
},
"region": {
"startLine": 49,
"startColumn": 5
}
}
}
]
}
]
}
]
"version": "2.1.0",
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json",
"runs": [{
"tool": {
"driver": {
"name": "Digital Mars D","version": "2.110.0","informationUri": "https://dlang.org/dmd.html"
}
},
"invocations": [{
"executionSuccessful": false
}],
"results": [{
"ruleId": "DMD",
"message": { "text": "undefined identifier `x`" },"locations": [{
"physicalLocation": {
"artifactLocation": {
"uri": "fail_compilation/sarif_test.d"},
"region": {
"startLine": 35,"startColumn": 5}
}
}]
}]
}]
}
---
*/
Expand Down

0 comments on commit e36e746

Please sign in to comment.