mirror of
https://github.com/dlang/dmd.git
synced 2025-05-11 23:05:18 +03:00
feat(errors): integrate warnings and enhance SARIF formatting (#17032)
Signed-off-by: royalpinto007 <royalpinto007@gmail.com>
This commit is contained in:
parent
4a90885ef8
commit
6c09f58feb
3 changed files with 107 additions and 22 deletions
|
@ -529,6 +529,11 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
|
||||||
if (!global.gag)
|
if (!global.gag)
|
||||||
{
|
{
|
||||||
info.headerColor = Classification.warning;
|
info.headerColor = Classification.warning;
|
||||||
|
if (global.params.v.messageStyle == MessageStyle.sarif)
|
||||||
|
{
|
||||||
|
generateSarifReport(loc, format, ap, info.kind);
|
||||||
|
return;
|
||||||
|
}
|
||||||
verrorPrint(format, ap, info);
|
verrorPrint(format, ap, info);
|
||||||
if (global.params.useWarnings == DiagnosticReporting.error)
|
if (global.params.useWarnings == DiagnosticReporting.error)
|
||||||
global.warnings++;
|
global.warnings++;
|
||||||
|
@ -567,6 +572,7 @@ private extern(C++) void verrorReport(const SourceLoc loc, const(char)* format,
|
||||||
if (global.params.v.messageStyle == MessageStyle.sarif)
|
if (global.params.v.messageStyle == MessageStyle.sarif)
|
||||||
{
|
{
|
||||||
generateSarifReport(loc, format, ap, info.kind);
|
generateSarifReport(loc, format, ap, info.kind);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,6 +106,26 @@ void generateSarifReport(const ref SourceLoc loc, const(char)* format, va_list a
|
||||||
// Format the error message
|
// Format the error message
|
||||||
string formattedMessage = formatErrorMessage(format, ap);
|
string formattedMessage = formatErrorMessage(format, ap);
|
||||||
|
|
||||||
|
// Map ErrorKind to SARIF levels
|
||||||
|
const(char)* level;
|
||||||
|
final switch (kind) {
|
||||||
|
case ErrorKind.error:
|
||||||
|
level = "error";
|
||||||
|
break;
|
||||||
|
case ErrorKind.warning:
|
||||||
|
level = "warning";
|
||||||
|
break;
|
||||||
|
case ErrorKind.deprecation:
|
||||||
|
level = "deprecation";
|
||||||
|
break;
|
||||||
|
case ErrorKind.tip:
|
||||||
|
level = "note";
|
||||||
|
break;
|
||||||
|
case ErrorKind.message:
|
||||||
|
level = "none";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Create an OutBuffer to store the SARIF report
|
// Create an OutBuffer to store the SARIF report
|
||||||
OutBuffer ob;
|
OutBuffer ob;
|
||||||
ob.doindent = true;
|
ob.doindent = true;
|
||||||
|
@ -134,36 +154,86 @@ void generateSarifReport(const ref SourceLoc loc, const(char)* format, va_list a
|
||||||
// Tool Information
|
// Tool Information
|
||||||
ob.level += 1;
|
ob.level += 1;
|
||||||
ob.writestringln(`"tool": {`);
|
ob.writestringln(`"tool": {`);
|
||||||
|
ob.level += 1;
|
||||||
ob.writestringln(`"driver": {`);
|
ob.writestringln(`"driver": {`);
|
||||||
ob.printf(`"name": "%s",`, global.compileEnv.vendor.ptr);
|
ob.level += 1;
|
||||||
ob.printf(`"version": "%.*s",`, cast(int)length, rawVersionChars);
|
|
||||||
|
// Write "name" field
|
||||||
|
ob.writestring(`"name": "`);
|
||||||
|
ob.writestring(global.compileEnv.vendor.ptr);
|
||||||
|
ob.writestringln(`",`);
|
||||||
|
|
||||||
|
// Write "version" field
|
||||||
|
ob.writestring(`"version": "`);
|
||||||
|
ob.writestring(cast(string)rawVersionChars[0 .. length]);
|
||||||
|
ob.writestringln(`",`);
|
||||||
|
|
||||||
|
// Write "informationUri" field
|
||||||
ob.writestringln(`"informationUri": "https://dlang.org/dmd.html"`);
|
ob.writestringln(`"informationUri": "https://dlang.org/dmd.html"`);
|
||||||
|
ob.level -= 1;
|
||||||
ob.writestringln("}");
|
ob.writestringln("}");
|
||||||
|
ob.level -= 1;
|
||||||
ob.writestringln("},");
|
ob.writestringln("},");
|
||||||
|
|
||||||
// Invocation Information
|
// Invocation Information
|
||||||
ob.writestringln(`"invocations": [{`);
|
ob.writestringln(`"invocations": [{`);
|
||||||
|
ob.level += 1;
|
||||||
ob.writestringln(`"executionSuccessful": false`);
|
ob.writestringln(`"executionSuccessful": false`);
|
||||||
|
ob.level -= 1;
|
||||||
ob.writestringln("}],");
|
ob.writestringln("}],");
|
||||||
|
|
||||||
// Results Array
|
// Results Array
|
||||||
ob.writestringln(`"results": [{`);
|
ob.writestringln(`"results": [{`);
|
||||||
|
ob.level += 1;
|
||||||
ob.writestringln(`"ruleId": "DMD",`);
|
ob.writestringln(`"ruleId": "DMD",`);
|
||||||
ob.printf(`"message": { "text": "%s" },`, formattedMessage.ptr);
|
|
||||||
|
// 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
|
// Location Information
|
||||||
ob.writestringln(`"locations": [{`);
|
ob.writestringln(`"locations": [{`);
|
||||||
|
ob.level += 1;
|
||||||
ob.writestringln(`"physicalLocation": {`);
|
ob.writestringln(`"physicalLocation": {`);
|
||||||
|
ob.level += 1;
|
||||||
|
|
||||||
|
// Artifact Location
|
||||||
ob.writestringln(`"artifactLocation": {`);
|
ob.writestringln(`"artifactLocation": {`);
|
||||||
|
ob.level += 1;
|
||||||
ob.writestring(`"uri": "`);
|
ob.writestring(`"uri": "`);
|
||||||
ob.writestring(loc.filename);
|
ob.writestring(loc.filename);
|
||||||
ob.writestringln(`"},`);
|
ob.writestringln(`"`);
|
||||||
|
ob.level -= 1;
|
||||||
|
ob.writestringln(`},`);
|
||||||
|
|
||||||
|
// Region Information
|
||||||
ob.writestringln(`"region": {`);
|
ob.writestringln(`"region": {`);
|
||||||
ob.printf(`"startLine": %d,`, loc.linnum);
|
ob.level += 1;
|
||||||
ob.printf(`"startColumn": %d`, loc.charnum);
|
ob.writestring(`"startLine": `);
|
||||||
ob.writestringln("}");
|
ob.printf(`%d,`, loc.linnum);
|
||||||
ob.writestringln("}");
|
ob.writestringln(``);
|
||||||
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("}]");
|
ob.writestringln("}]");
|
||||||
|
|
||||||
// Close the run and SARIF JSON
|
// Close the run and SARIF JSON
|
||||||
|
|
|
@ -7,7 +7,9 @@ TEST_OUTPUT:
|
||||||
"runs": [{
|
"runs": [{
|
||||||
"tool": {
|
"tool": {
|
||||||
"driver": {
|
"driver": {
|
||||||
"name": "Digital Mars D","version": "2.110.0","informationUri": "https://dlang.org/dmd.html"
|
"name": "Digital Mars D",
|
||||||
|
"version": "2.110.0",
|
||||||
|
"informationUri": "https://dlang.org/dmd.html"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"invocations": [{
|
"invocations": [{
|
||||||
|
@ -15,12 +17,19 @@ TEST_OUTPUT:
|
||||||
}],
|
}],
|
||||||
"results": [{
|
"results": [{
|
||||||
"ruleId": "DMD",
|
"ruleId": "DMD",
|
||||||
"message": { "text": "undefined identifier `x`" },"locations": [{
|
"message": {
|
||||||
|
"text": "undefined identifier `x`"
|
||||||
|
},
|
||||||
|
"level": "error",
|
||||||
|
"locations": [{
|
||||||
"physicalLocation": {
|
"physicalLocation": {
|
||||||
"artifactLocation": {
|
"artifactLocation": {
|
||||||
"uri": "fail_compilation/sarif_test.d"},
|
"uri": "fail_compilation/sarif_test.d"
|
||||||
|
},
|
||||||
"region": {
|
"region": {
|
||||||
"startLine": 34,"startColumn": 5}
|
"startLine": 43,
|
||||||
|
"startColumn": 5
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
}]
|
}]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue