Skip to content

Commit

Permalink
fix(errors): adhere to SARIF 2.1.0 schema and fix output stream handling
Browse files Browse the repository at this point in the history
Signed-off-by: royalpinto007 <royalpinto007@gmail.com>

fix: global driver name, version

Signed-off-by: royalpinto007 <royalpinto007@gmail.com>

fix: version to have a cleaner result

Signed-off-by: royalpinto007 <royalpinto007@gmail.com>

fix: unwanted change

Signed-off-by: royalpinto007 <royalpinto007@gmail.com>

fix: more cleaner version

Signed-off-by: royalpinto007 <royalpinto007@gmail.com>

fix: more cleaner version without trailing beta

Signed-off-by: royalpinto007 <royalpinto007@gmail.com>

fix: unused variable removal

Signed-off-by: royalpinto007 <royalpinto007@gmail.com>
  • Loading branch information
royalpinto007 committed Oct 22, 2024
1 parent a0f15b5 commit 643b1a2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 39 deletions.
78 changes: 56 additions & 22 deletions compiler/src/dmd/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,67 @@ void generateSarifReport(const ref Loc loc, const(char)* format, va_list ap, Err
// Create an OutBuffer to store the SARIF report
OutBuffer ob;

// Extract and clean the version string
const(char)* rawVersionChars = global.versionChars();

Check warning on line 126 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L126

Added line #L126 was not covered by tests

// Remove 'v' prefix if it exists
if (*rawVersionChars == 'v') {
rawVersionChars += 1;

Check warning on line 130 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L129-L130

Added lines #L129 - L130 were not covered by tests
}

// 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);

Check warning on line 137 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L134-L137

Added lines #L134 - L137 were not covered by tests
} else {
length = strlen(rawVersionChars);

Check warning on line 139 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L139

Added line #L139 was not covered by tests
}

// Build SARIF report using OutBuffer
ob.writestring("{\n");
ob.writestring(" \"invocation\": {\n");
ob.writestring(" \"executionSuccessful\": false\n");
ob.writestring(" },\n");
ob.writestring(" \"results\": [\n {\n");

// Write location information with relative path
ob.writestring(" \"location\": {\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(" \"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");

Check warning on line 146 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L144-L146

Added lines #L144 - L146 were not covered by tests

// 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");

Check warning on line 153 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L149-L153

Added lines #L149 - L153 were not covered by tests
ob.writestring(" }\n");
ob.writestring(" },\n");

// Write message and ruleId
ob.printf(" \"message\": \"%s\",\n", formattedMessage.ptr);
ob.writestring(" \"ruleId\": \"DMD\"\n");

// Close the results and SARIF report
ob.writestring(" }\n ],\n");
ob.writestring(" \"tool\": {\n");
ob.printf(" \"name\": \"DMD\"\n");
ob.writestring(" }\n");
// Invocation Information
ob.writestring(" \"invocations\": [\n");
ob.writestring(" {\n");
ob.writestring(" \"executionSuccessful\": false\n");
ob.writestring(" }\n");
ob.writestring(" ],\n");

Check warning on line 162 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L158-L162

Added lines #L158 - L162 were not covered by tests

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

Check warning on line 167 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L165-L167

Added lines #L165 - L167 were not covered by tests

// 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");

Check warning on line 182 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L170-L182

Added lines #L170 - L182 were not covered by tests

// Close the run and SARIF JSON
ob.writestring(" }\n ]\n");

Check warning on line 185 in compiler/src/dmd/errors.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/errors.d#L185

Added line #L185 was not covered by tests
ob.writestring("}\n");

// Extract the final null-terminated string and print it to stdout
Expand Down
50 changes: 33 additions & 17 deletions compiler/test/fail_compilation/sarif_test.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,43 @@ TEST_OUTPUT:
---
fail_compilation/sarif_test.d: Error: undefined identifier `x`
{
"invocation": {
"executionSuccessful": false
},
"results": [
"version": "2.1.0",
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json",
"runs": [
{
"location": {
"artifactLocation": {
"uri": "fail_compilation/sarif_test.d"
},
"region": {
"startLine": 33,
"startColumn": 5
"tool": {
"driver": {
"name": "Digital Mars D",
"version": "2.110.0",
"informationUri": "https://dlang.org/dmd.html"
}
},
"message": "undefined identifier `x`",
"ruleId": "DMD"
"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
}
}
}
]
}
]
}
],
"tool": {
"name": "DMD"
}
]
}
---
*/
Expand Down

0 comments on commit 643b1a2

Please sign in to comment.