Skip to content

Commit

Permalink
feat(errors): generate sarif report on successful execution
Browse files Browse the repository at this point in the history
Signed-off-by: royalpinto007 <[email protected]>

fix(errors): tests dDoc, implicit conversion

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): implicit conversion

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): brace, default parameters, invert if-else

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): pre commit checks

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): arguments

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): va_list

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): imports

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): imports

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): va_list

Signed-off-by: royalpinto007 <[email protected]>

fix(errors): va_list

Signed-off-by: royalpinto007 <[email protected]>
  • Loading branch information
royalpinto007 committed Nov 6, 2024
1 parent ccd589d commit 796cf5f
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 92 deletions.
10 changes: 5 additions & 5 deletions compiler/src/dmd/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
info.headerColor = Classification.error;
if (global.params.v.messageStyle == MessageStyle.sarif)
{
generateSarifReport(loc, format, ap, info.kind);
generateSarifReport(loc, format, ap, info.kind, false);
return;
}
verrorPrint(format, ap, info);
Expand Down Expand Up @@ -510,7 +510,7 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
info.headerColor = Classification.deprecation;
if (global.params.v.messageStyle == MessageStyle.sarif)
{
generateSarifReport(loc, format, ap, info.kind);
generateSarifReport(loc, format, ap, info.kind, false);
return;
}
verrorPrint(format, ap, info);
Expand All @@ -531,7 +531,7 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
info.headerColor = Classification.warning;
if (global.params.v.messageStyle == MessageStyle.sarif)
{
generateSarifReport(loc, format, ap, info.kind);
generateSarifReport(loc, format, ap, info.kind, false);
return;
}
verrorPrint(format, ap, info);
Expand All @@ -551,7 +551,7 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
info.headerColor = Classification.tip;
if (global.params.v.messageStyle == MessageStyle.sarif)
{
generateSarifReport(loc, format, ap, info.kind);
generateSarifReport(loc, format, ap, info.kind, false);
return;
}
verrorPrint(format, ap, info);
Expand All @@ -571,7 +571,7 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
fflush(stdout); // ensure it gets written out in case of compiler aborts
if (global.params.v.messageStyle == MessageStyle.sarif)
{
generateSarifReport(loc, format, ap, info.kind);
generateSarifReport(loc, format, ap, info.kind, false);
return;
}
return;
Expand Down
9 changes: 9 additions & 0 deletions compiler/src/dmd/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ private:
private int tryMain(size_t argc, const(char)** argv, ref Param params)
{
import dmd.common.charactertables;
import dmd.sarif;
import core.stdc.stdarg;

Strings files;
Strings libmodules;
Expand All @@ -172,6 +174,13 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
// If we are here then compilation has ended
// gracefully as opposed to with `fatal`
global.plugErrorSinks();

if (global.errors == 0 && global.params.v.messageStyle == MessageStyle.sarif)
{
SourceLoc defaultLoc = SourceLoc(null, 0u, 0u);
va_list[1] ap;
generateSarifReport(defaultLoc, "", &ap[0], ErrorKind.message, true);
}
}

target.setTargetBuildDefaults();
Expand Down
186 changes: 99 additions & 87 deletions compiler/src/dmd/sarif.d
Original file line number Diff line number Diff line change
Expand Up @@ -165,44 +165,16 @@ Params:
format = A format string for constructing the error message.
ap = A variable argument list used with the format string.
kind = The kind of error (error, warning, deprecation, note, or message).
executionSuccessful = `true` if execution succeeded; includes an empty `results` array in the SARIF report.
Throws:
This function is marked as `nothrow` and does not throw exceptions.
See_Also:
$(LINK2 https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json, SARIF 2.1.0 schema)
*/
void generateSarifReport(const ref SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind) nothrow
void generateSarifReport(const ref SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind, bool executionSuccessful) nothrow
{
// Format the error message
string formattedMessage = formatErrorMessage(format, ap);

// Map ErrorKind to SARIF levels
const(char)* level;
string ruleId;
final switch (kind) {
case ErrorKind.error:
level = "error";
ruleId = "DMD-ERROR";
break;
case ErrorKind.warning:
level = "warning";
ruleId = "DMD-WARNING";
break;
case ErrorKind.deprecation:
level = "deprecation";
ruleId = "DMD-DEPRECATION";
break;
case ErrorKind.tip:
level = "note";
ruleId = "DMD-NOTE";
break;
case ErrorKind.message:
level = "none";
ruleId = "DMD-MESSAGE";
break;
}

// Create an OutBuffer to store the SARIF report
OutBuffer ob;
ob.doindent = true;
Expand Down Expand Up @@ -257,67 +229,107 @@ void generateSarifReport(const ref SourceLoc loc, const(char)* format, va_list a
// Invocation Information
ob.writestringln(`"invocations": [{`);
ob.level += 1;
ob.writestringln(`"executionSuccessful": false`);
ob.writestring(`"executionSuccessful": `);
ob.writestring(executionSuccessful ? "true" : "false");
ob.writestringln("");
ob.level -= 1;
ob.writestringln("}],");

// Results Array
ob.writestringln(`"results": [{`);
ob.level += 1;

// Rule ID
ob.writestring(`"ruleId": "`);
ob.writestring(ruleId);
ob.writestringln(`",`);

// Message Information
ob.writestringln(`"message": {`);
ob.level += 1;
ob.writestring(`"text": "`);
ob.writestring(formattedMessage.ptr);
ob.writestringln(`"`);
ob.level -= 1;
ob.writestringln(`},`);

// Error Severity Level
ob.writestring(`"level": "`);
ob.writestring(level);
ob.writestringln(`",`);

// Location Information
ob.writestringln(`"locations": [{`);
ob.level += 1;
ob.writestringln(`"physicalLocation": {`);
ob.level += 1;

// Artifact Location
ob.writestringln(`"artifactLocation": {`);
ob.level += 1;
ob.writestring(`"uri": "`);
ob.writestring(loc.filename);
ob.writestringln(`"`);
ob.level -= 1;
ob.writestringln(`},`);

// Region Information
ob.writestringln(`"region": {`);
ob.level += 1;
ob.writestring(`"startLine": `);
ob.printf(`%d,`, loc.linnum);
ob.writestringln(``);
ob.writestring(`"startColumn": `);
ob.printf(`%d`, loc.charnum);
ob.writestringln(``);
ob.level -= 1;
ob.writestringln(`}`);
// Empty results array for successful execution
if (executionSuccessful)
{
ob.writestringln(`"results": []`);
}
// Error information if execution was unsuccessful
else
{
// Format the error message
string formattedMessage = formatErrorMessage(format, ap);

// Map ErrorKind to SARIF levels
const(char)* level;
string ruleId;
final switch (kind) {
case ErrorKind.error:
level = "error";
ruleId = "DMD-ERROR";
break;
case ErrorKind.warning:
level = "warning";
ruleId = "DMD-WARNING";
break;
case ErrorKind.deprecation:
level = "deprecation";
ruleId = "DMD-DEPRECATION";
break;
case ErrorKind.tip:
level = "note";
ruleId = "DMD-NOTE";
break;
case ErrorKind.message:
level = "none";
ruleId = "DMD-MESSAGE";
break;
}

// Close physicalLocation and locations
ob.level -= 1;
ob.writestringln(`}`);
ob.level -= 1;
ob.writestringln(`}]`);
ob.level -= 1;
ob.writestringln("}]");
// Results Array for errors
ob.writestringln(`"results": [{`);
ob.level += 1;

// Rule ID
ob.writestring(`"ruleId": "`);
ob.writestring(ruleId);
ob.writestringln(`",`);

// Message Information
ob.writestringln(`"message": {`);
ob.level += 1;
ob.writestring(`"text": "`);
ob.writestring(formattedMessage.ptr);
ob.writestringln(`"`);
ob.level -= 1;
ob.writestringln("},");

// Error Severity Level
ob.writestring(`"level": "`);
ob.writestring(level);
ob.writestringln(`",`);

// Location Information
ob.writestringln(`"locations": [{`);
ob.level += 1;
ob.writestringln(`"physicalLocation": {`);
ob.level += 1;

// Artifact Location
ob.writestringln(`"artifactLocation": {`);
ob.level += 1;
ob.writestring(`"uri": "`);
ob.writestring(loc.filename);
ob.writestringln(`"`);
ob.level -= 1;
ob.writestringln("},");

// Region Information
ob.writestringln(`"region": {`);
ob.level += 1;
ob.writestring(`"startLine": `);
ob.printf(`%d,`, loc.linnum);
ob.writestringln("");
ob.writestring(`"startColumn": `);
ob.printf(`%d`, loc.charnum);
ob.writestringln("");
ob.level -= 1;
ob.writestringln("}");

// Close physicalLocation and locations
ob.level -= 1;
ob.writestringln("}");
ob.level -= 1;
ob.writestringln("}]");
ob.level -= 1;
ob.writestringln("}]");
}

// Close the run and SARIF JSON
ob.level -= 1;
Expand Down
27 changes: 27 additions & 0 deletions compiler/test/compilable/sarif_success_test.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
TEST_OUTPUT:
---
{
"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": true
}],
"results": []
}]
}
---
*/
// REQUIRED_ARGS: -verror-style=sarif

void main() {
int x = 5;
}

0 comments on commit 796cf5f

Please sign in to comment.