Skip to content

Commit 26ca391

Browse files
committed
Delay diagnostic printing until plugSink
1 parent a529ad8 commit 26ca391

6 files changed

Lines changed: 100 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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class ErrorSinkCompiler : ErrorSink
9999

100100
void plugSink()
101101
{
102+
if (global.params.v.messageStyle == MessageStyle.diagreport)
103+
{
104+
}
105+
102106
// Exit if there are no collected diagnostics
103107
if (!diagnostics.length) return;
104108

@@ -452,6 +456,54 @@ private struct DiagnosticContext
452456
bool supplemental; // true if supplemental error
453457
}
454458

459+
/**
460+
* Collects diagnostics for the diagreport messagestyle.
461+
* Params:
462+
* loc = location of error
463+
* format = printf-style format specification
464+
* ap = printf-style variadic arguments
465+
* kind = kind of error being printed
466+
*/
467+
private void collectDiagnostic(const SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind) nothrow
468+
{
469+
// A new primary diagnostic means the previous causal group is complete
470+
if (diagnostics.length > 0)
471+
{
472+
completedEvents ~= diagnostics;
473+
diagnostics.length = 0;
474+
}
475+
476+
OutBuffer tmp;
477+
tmp.vprintf(format, ap);
478+
479+
Diagnostic d;
480+
d.loc = loc;
481+
d.kind = kind;
482+
d.message = tmp.extractSlice().idup;
483+
diagnostics ~= d;
484+
}
485+
486+
/**
487+
* Collects supplementals of diagnostics for the diagreport messagestyle.
488+
* Params:
489+
* loc = location of error
490+
* format = printf-style format specification
491+
* ap = printf-style variadic arguments
492+
* kind = kind of error being printed
493+
*/
494+
private void collectSupplemental(const SourceLoc loc, const(char)* format, va_list ap, ErrorKind kind) nothrow
495+
{
496+
// Append to the currently open causal group
497+
OutBuffer tmp;
498+
tmp.vprintf(format, ap);
499+
500+
Diagnostic d;
501+
d.loc = loc;
502+
d.kind = kind;
503+
d.message = tmp.extractSlice().idup;
504+
diagnostics ~= d;
505+
}
506+
455507
/**
456508
* Implements $(D error), $(D warning), $(D deprecation), $(D message), and
457509
* $(D tip). Report a diagnostic error, taking a va_list parameter, and
@@ -487,6 +539,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
487539
addSarifDiagnostic(loc, format, ap, kind);
488540
return;
489541
}
542+
if (global.params.v.messageStyle == MessageStyle.diagreport)
543+
{
544+
collectDiagnostic(loc, format, ap, kind);
545+
return;
546+
}
490547
printDiagnostic(format, ap, info);
491548
if (global.params.v.errorLimit && global.errors >= global.params.v.errorLimit)
492549
{
@@ -521,6 +578,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
521578
addSarifDiagnostic(loc, format, ap, kind);
522579
return;
523580
}
581+
if (global.params.v.messageStyle == MessageStyle.diagreport)
582+
{
583+
collectDiagnostic(loc, format, ap, kind);
584+
return;
585+
}
524586
printDiagnostic(format, ap, info);
525587
}
526588
}
@@ -542,6 +604,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
542604
addSarifDiagnostic(loc, format, ap, kind);
543605
return;
544606
}
607+
if (global.params.v.messageStyle == MessageStyle.diagreport)
608+
{
609+
collectDiagnostic(loc, format, ap, kind);
610+
return;
611+
}
545612
printDiagnostic(format, ap, info);
546613
if (global.params.useWarnings == DiagnosticReporting.error)
547614
global.warnings++;
@@ -558,6 +625,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
558625
addSarifDiagnostic(loc, format, ap, kind);
559626
return;
560627
}
628+
if (global.params.v.messageStyle == MessageStyle.diagreport)
629+
{
630+
collectDiagnostic(loc, format, ap, kind);
631+
return;
632+
}
561633
printDiagnostic(format, ap, info);
562634
}
563635
return;
@@ -578,6 +650,11 @@ private extern(C++) void vreportDiagnostic(const SourceLoc loc, const(char)* for
578650
addSarifDiagnostic(loc, format, ap, kind);
579651
return;
580652
}
653+
if (global.params.v.messageStyle == MessageStyle.diagreport)
654+
{
655+
collectDiagnostic(loc, format, ap, kind);
656+
return;
657+
}
581658
return;
582659
}
583660
}
@@ -614,6 +691,11 @@ private extern(C++) void vsupplementalDiagnostic(const SourceLoc loc, const(char
614691
}
615692
else
616693
info.headerColor = Classification.error;
694+
if (global.params.v.messageStyle == MessageStyle.diagreport)
695+
{
696+
collectSupplemental(loc, format, ap, kind);
697+
return;
698+
}
617699
printDiagnostic(format, ap, info);
618700
return;
619701

@@ -625,6 +707,11 @@ private extern(C++) void vsupplementalDiagnostic(const SourceLoc loc, const(char
625707
if (global.params.v.errorLimit == 0 || global.deprecations <= global.params.v.errorLimit)
626708
{
627709
info.headerColor = Classification.deprecation;
710+
if (global.params.v.messageStyle == MessageStyle.diagreport)
711+
{
712+
collectSupplemental(loc, format, ap, kind);
713+
return;
714+
}
628715
printDiagnostic(format, ap, info);
629716
}
630717
}

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)