Skip to content

Commit e3fe9c1

Browse files
committed
new style diagreport with delayed diagnostic printing in errors.d
1 parent a529ad8 commit e3fe9c1

6 files changed

Lines changed: 102 additions & 4 deletions

File tree

compiler/include/dmd/globals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ enum class MessageStyle : unsigned char
3535
{
3636
digitalmars, // file(line,column): message
3737
gnu, // file:line:column: message
38-
sarif // JSON SARIF output, see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html
38+
sarif, // JSON SARIF output, see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html
39+
diagreport // diagnostics reporting messagestyle
3940
};
4041

4142
// The state of array bounds checking

compiler/src/dmd/cli.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,14 +935,15 @@ dmd -cov -unittest myprog.d
935935
Option("vcolumns",
936936
"print character (column) numbers in diagnostics"
937937
),
938-
Option("verror-style=[digitalmars|gnu|sarif]",
938+
Option("verror-style=[digitalmars|gnu|sarif|diagreport]",
939939
"set the style for file/line number annotations on compiler messages",
940940
`Set the style for file/line number annotations on compiler messages,
941941
where:
942942
$(DL
943943
$(DT digitalmars)$(DD 'file(line[,column]): message'. This is the default.)
944944
$(DT gnu)$(DD 'file:line[:column]: message', conforming to the GNU standard used by gcc and clang.)
945945
$(DT sarif)$(DD 'Generates JSON output conforming to the SARIF (Static Analysis Results Interchange Format) standard, useful for integration with tools like GitHub and other SARIF readers.')
946+
$(DT diagreport)$(DD 'Generates diagnostic report of errors, warnings, deprecations and tips.')
946947
)`,
947948
),
948949
Option("verror-supplements=<num>",

compiler/src/dmd/errors.d

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class ErrorSinkCompiler : ErrorSink
9999

100100
void plugSink()
101101
{
102+
if (global.params.v.messageStyle == MessageStyle.diagreport)
103+
{
104+
foreach (ref group; completedEvents)
105+
// callEvent(group);
106+
}
107+
102108
// Exit if there are no collected diagnostics
103109
if (!diagnostics.length) return;
104110

@@ -452,6 +458,54 @@ private struct DiagnosticContext
452458
bool supplemental; // true if supplemental error
453459
}
454460

461+
/**
462+
* Collects diagnostics for the diagreport messagestyle.
463+
* Params:
464+
* loc = location of error
465+
* format = printf-style format specification
466+
* ap = printf-style variadic arguments
467+
* kind = kind of error being printed
468+
*/
469+
private void collectDiagnostic(const SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind) nothrow
470+
{
471+
// A new primary diagnostic means the previous causal group is complete
472+
if (diagnostics.length > 0)
473+
{
474+
completedEvents ~= diagnostics;
475+
diagnostics.length = 0;
476+
}
477+
478+
OutBuffer tmp;
479+
tmp.vprintf(format, ap);
480+
481+
Diagnostic d;
482+
d.loc = loc;
483+
d.kind = kind;
484+
d.message = tmp.extractSlice().idup;
485+
diagnostics ~= d;
486+
}
487+
488+
/**
489+
* Collects supplementals of diagnostics for the diagreport messagestyle.
490+
* Params:
491+
* loc = location of error
492+
* format = printf-style format specification
493+
* ap = printf-style variadic arguments
494+
* kind = kind of error being printed
495+
*/
496+
private void collectSupplemental(const SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind) nothrow
497+
{
498+
// Append to the currently open causal group
499+
OutBuffer tmp;
500+
tmp.vprintf(format, ap);
501+
502+
Diagnostic d;
503+
d.loc = loc;
504+
d.kind = kind;
505+
d.message = tmp.extractSlice().idup;
506+
diagnostics ~= d;
507+
}
508+
455509
/**
456510
* Implements $(D error), $(D warning), $(D deprecation), $(D message), and
457511
* $(D tip). Report a diagnostic error, taking a va_list parameter, and
@@ -487,6 +541,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
487541
addSarifDiagnostic(loc, format, ap, kind);
488542
return;
489543
}
544+
if (global.params.v.messageStyle == MessageStyle.diagreport)
545+
{
546+
collectDiagnostic(loc, format, ap, kind);
547+
return;
548+
}
490549
printDiagnostic(format, ap, info);
491550
if (global.params.v.errorLimit && global.errors >= global.params.v.errorLimit)
492551
{
@@ -521,6 +580,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
521580
addSarifDiagnostic(loc, format, ap, kind);
522581
return;
523582
}
583+
if (global.params.v.messageStyle == MessageStyle.diagreport)
584+
{
585+
collectDiagnostic(loc, format, ap, kind);
586+
return;
587+
}
524588
printDiagnostic(format, ap, info);
525589
}
526590
}
@@ -542,6 +606,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
542606
addSarifDiagnostic(loc, format, ap, kind);
543607
return;
544608
}
609+
if (global.params.v.messageStyle == MessageStyle.diagreport)
610+
{
611+
collectDiagnostic(loc, format, ap, kind);
612+
return;
613+
}
545614
printDiagnostic(format, ap, info);
546615
if (global.params.useWarnings == DiagnosticReporting.error)
547616
global.warnings++;
@@ -558,6 +627,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
558627
addSarifDiagnostic(loc, format, ap, kind);
559628
return;
560629
}
630+
if (global.params.v.messageStyle == MessageStyle.diagreport)
631+
{
632+
collectDiagnostic(loc, format, ap, kind);
633+
return;
634+
}
561635
printDiagnostic(format, ap, info);
562636
}
563637
return;
@@ -578,6 +652,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
578652
addSarifDiagnostic(loc, format, ap, kind);
579653
return;
580654
}
655+
if (global.params.v.messageStyle == MessageStyle.diagreport)
656+
{
657+
collectDiagnostic(loc, format, ap, kind);
658+
return;
659+
}
581660
return;
582661
}
583662
}
@@ -614,6 +693,11 @@ private extern(C++) void vsupplementalDiagnostic(const SourceLoc loc, const(char
614693
}
615694
else
616695
info.headerColor = Classification.error;
696+
if (global.params.v.messageStyle == MessageStyle.diagreport)
697+
{
698+
collectSupplemental(loc, format, ap, kind);
699+
return;
700+
}
617701
printDiagnostic(format, ap, info);
618702
return;
619703

@@ -625,6 +709,11 @@ private extern(C++) void vsupplementalDiagnostic(const SourceLoc loc, const(char
625709
if (global.params.v.errorLimit == 0 || global.deprecations <= global.params.v.errorLimit)
626710
{
627711
info.headerColor = Classification.deprecation;
712+
if (global.params.v.messageStyle == MessageStyle.diagreport)
713+
{
714+
collectSupplemental(loc, format, ap, kind);
715+
return;
716+
}
628717
printDiagnostic(format, ap, info);
629718
}
630719
}

compiler/src/dmd/frontend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ enum class MessageStyle : uint8_t
370370
digitalmars = 0u,
371371
gnu = 1u,
372372
sarif = 2u,
373+
diagreport = 3u,
373374
};
374375

375376
struct SourceLoc final

compiler/src/dmd/location.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ enum MessageStyle : ubyte
2323
{
2424
digitalmars, /// filename.d(line): message
2525
gnu, /// filename.d:line: message, see https://www.gnu.org/prep/standards/html_node/Errors.html
26-
sarif /// JSON SARIF output, see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html
26+
sarif, /// JSON SARIF output, see https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html
27+
diagreport /// diagnostics reporting messagestyle
2728
}
2829
/**
2930
A source code location
@@ -228,6 +229,8 @@ void writeSourceLoc(ref OutBuffer buf,
228229
case MessageStyle.sarif: // https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html
229230
// No formatting needed here for SARIF
230231
break;
232+
case MessageStyle.diagreport:
233+
break;
231234
}
232235
}
233236

compiler/src/dmd/mars.d

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,11 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, out Param
11361136
case "sarif":
11371137
params.v.messageStyle = MessageStyle.sarif;
11381138
break;
1139+
case "diagreport":
1140+
params.v.messageStyle = MessageStyle.diagreport;
1141+
break;
11391142
default:
1140-
error("unknown error style '%.*s', must be 'digitalmars', 'gnu', or 'sarif'", cast(int) style.length, style.ptr);
1143+
error("unknown error style '%.*s', must be 'digitalmars', 'gnu','sarif' or 'diagreport'", cast(int) style.length, style.ptr);
11411144
}
11421145
}
11431146
else if (startsWith(p + 1, "target"))

0 commit comments

Comments
 (0)