Skip to content

Commit c4e67e2

Browse files
committed
[Diagnostics] Add -diagnostic-style=(llvm|swift) to control printed output
This default formatting style remains the same "LLVM style". "Swift style" is what was previously enabled via -enable-experimental-diagnostic-formatting
1 parent 0ab9fd7 commit c4e67e2

13 files changed

+48
-22
lines changed

include/swift/Basic/DiagnosticOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class DiagnosticOptions {
3232
VerifyAndApplyFixes
3333
} VerifyMode = NoVerify;
3434

35+
enum FormattingStyle { LLVM, Swift };
36+
3537
/// Indicates whether to allow diagnostics for \c <unknown> locations if
3638
/// \c VerifyMode is not \c NoVerify.
3739
bool VerifyIgnoreUnknown = false;
@@ -61,7 +63,7 @@ class DiagnosticOptions {
6163

6264
// If set to true, use the more descriptive experimental formatting style for
6365
// diagnostics.
64-
bool EnableExperimentalFormatting = false;
66+
FormattingStyle PrintedFormattingStyle = FormattingStyle::LLVM;
6567

6668
std::string DiagnosticDocumentationPath = "";
6769

include/swift/Frontend/PrintingDiagnosticConsumer.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#ifndef SWIFT_PRINTINGDIAGNOSTICCONSUMER_H
1919
#define SWIFT_PRINTINGDIAGNOSTICCONSUMER_H
2020

21-
#include "swift/Basic/LLVM.h"
2221
#include "swift/AST/DiagnosticConsumer.h"
22+
#include "swift/Basic/DiagnosticOptions.h"
23+
#include "swift/Basic/LLVM.h"
2324

2425
#include "llvm/Support/raw_ostream.h"
2526
#include "llvm/Support/Process.h"
@@ -33,7 +34,8 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
3334
bool ForceColors = false;
3435
bool PrintEducationalNotes = false;
3536
bool DidErrorOccur = false;
36-
bool ExperimentalFormattingEnabled = false;
37+
DiagnosticOptions::FormattingStyle FormattingStyle =
38+
DiagnosticOptions::FormattingStyle::LLVM;
3739
// The current snippet used to display an error/warning/remark and the notes
3840
// implicitly associated with it. Uses `std::unique_ptr` so that
3941
// `AnnotatedSourceSnippet` can be forward declared.
@@ -65,7 +67,9 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
6567
PrintEducationalNotes = ShouldPrint;
6668
}
6769

68-
void enableExperimentalFormatting() { ExperimentalFormattingEnabled = true; }
70+
void setFormattingStyle(DiagnosticOptions::FormattingStyle style) {
71+
FormattingStyle = style;
72+
}
6973

7074
bool didErrorOccur() {
7175
return DidErrorOccur;

include/swift/Option/FrontendOptions.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ def enable_cross_import_overlays : Flag<["-"], "enable-cross-import-overlays">,
131131
def disable_cross_import_overlays : Flag<["-"], "disable-cross-import-overlays">,
132132
HelpText<"Do not automatically import declared cross-import overlays.">;
133133

134-
def enable_experimental_diagnostic_formatting :
135-
Flag<["-"], "enable-experimental-diagnostic-formatting">,
136-
HelpText<"Enable experimental diagnostic formatting features.">;
137-
138134
def diagnostic_documentation_path
139135
: Separate<["-"], "diagnostic-documentation-path">, MetaVarName<"<path>">,
140136
HelpText<"Path to diagnostic documentation resources">;

include/swift/Option/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,13 @@ def debug_diagnostic_names : Flag<["-"], "debug-diagnostic-names">,
370370
def print_educational_notes : Flag<["-"], "print-educational-notes">,
371371
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
372372
HelpText<"Include educational notes in printed diagnostic output, if available">;
373+
def diagnostic_style : Separate<["-"], "diagnostic-style">,
374+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
375+
MetaVarName<"<style>">,
376+
HelpText<"The formatting style used when printing diagnostics ('swift' or 'llvm')">;
377+
def diagnostic_style_EQ : Joined<["-"], "diagnostic-style=">,
378+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
379+
MetaVarName<"<style>">, Alias<diagnostic_style>;
373380

374381
def module_cache_path : Separate<["-"], "module-cache-path">,
375382
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
243243
inputArgs.AddLastArg(arguments, options::OPT_serialize_diagnostics_path);
244244
inputArgs.AddLastArg(arguments, options::OPT_debug_diagnostic_names);
245245
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);
246+
inputArgs.AddLastArg(arguments, options::OPT_diagnostic_style);
246247
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
247248
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
248249
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,19 +884,33 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
884884
Opts.SkipDiagnosticPasses |= Args.hasArg(OPT_disable_diagnostic_passes);
885885
Opts.ShowDiagnosticsAfterFatalError |=
886886
Args.hasArg(OPT_show_diagnostics_after_fatal);
887+
887888
Opts.UseColor |=
888889
Args.hasFlag(OPT_color_diagnostics,
889890
OPT_no_color_diagnostics,
890891
/*Default=*/llvm::sys::Process::StandardErrHasColors());
892+
// If no style options are specified, default to LLVM style.
893+
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::LLVM;
894+
if (const Arg *arg = Args.getLastArg(OPT_diagnostic_style)) {
895+
StringRef contents = arg->getValue();
896+
if (contents == "llvm") {
897+
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::LLVM;
898+
} else if (contents == "swift") {
899+
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::Swift;
900+
} else {
901+
Diags.diagnose(SourceLoc(), diag::error_unsupported_option_argument,
902+
arg->getOption().getPrefixedName(), arg->getValue());
903+
return true;
904+
}
905+
}
906+
891907
Opts.FixitCodeForAllDiagnostics |= Args.hasArg(OPT_fixit_all);
892908
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
893909
Opts.WarningsAsErrors = Args.hasFlag(options::OPT_warnings_as_errors,
894910
options::OPT_no_warnings_as_errors,
895911
false);
896912
Opts.PrintDiagnosticNames |= Args.hasArg(OPT_debug_diagnostic_names);
897913
Opts.PrintEducationalNotes |= Args.hasArg(OPT_print_educational_notes);
898-
Opts.EnableExperimentalFormatting |=
899-
Args.hasArg(OPT_enable_experimental_diagnostic_formatting);
900914
if (Arg *A = Args.getLastArg(OPT_diagnostic_documentation_path)) {
901915
Opts.DiagnosticDocumentationPath = A->getValue();
902916
}

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
932932
if (Info.IsChildNote)
933933
return;
934934

935-
if (ExperimentalFormattingEnabled) {
935+
switch (FormattingStyle) {
936+
case DiagnosticOptions::FormattingStyle::Swift:
936937
if (Info.Kind == DiagnosticKind::Note && currentSnippet) {
937938
// If this is a note and we have an in-flight message, add it to that
938939
// instead of emitting it separately.
@@ -950,7 +951,9 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
950951
BufferedEducationalNotes.push_back(buffer->get()->getBuffer().str());
951952
}
952953
}
953-
} else {
954+
break;
955+
956+
case DiagnosticOptions::FormattingStyle::LLVM:
954957
printDiagnostic(SM, Info);
955958

956959
if (PrintEducationalNotes) {
@@ -965,6 +968,7 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
965968
for (auto ChildInfo : Info.ChildDiagnosticInfo) {
966969
printDiagnostic(SM, *ChildInfo);
967970
}
971+
break;
968972
}
969973
}
970974

lib/FrontendTool/FrontendTool.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,10 +2111,8 @@ int swift::performFrontend(ArrayRef<const char *> Args,
21112111
PDC.setPrintEducationalNotes(
21122112
Invocation.getDiagnosticOptions().PrintEducationalNotes);
21132113

2114-
// Temporarily stage the new diagnostic formatting style behind
2115-
// -enable-descriptive-diagnostics
2116-
if (Invocation.getDiagnosticOptions().EnableExperimentalFormatting)
2117-
PDC.enableExperimentalFormatting();
2114+
PDC.setFormattingStyle(
2115+
Invocation.getDiagnosticOptions().PrintedFormattingStyle);
21182116

21192117
if (Invocation.getFrontendOptions().DebugTimeCompilation)
21202118
SharedTimer::enableCompilationTimers();

test/Driver/color-diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swiftc_driver -color-diagnostics -emit-executable -o %t %s 2>&1 \
1+
// RUN: not %target-swiftc_driver -color-diagnostics -diagnostic-style=llvm -emit-executable -o %t %s 2>&1 \
22
// RUN: | %FileCheck -check-prefix=CHECK-CD %s
33
// CHECK-CD: [0m1 = 2{{$}}
44

test/diagnostics/educational-notes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: not %target-swift-frontend -color-diagnostics -print-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace
1+
// RUN: not %target-swift-frontend -color-diagnostics -diagnostic-style=llvm -print-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace
22
// RUN: not %target-swift-frontend -no-color-diagnostics -print-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --match-full-lines --strict-whitespace --check-prefix=NO-COLOR
3-
// RUN: not %target-swift-frontend -enable-experimental-diagnostic-formatting -print-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK-DESCRIPTIVE
3+
// RUN: not %target-swift-frontend -diagnostic-style=swift -print-educational-notes -diagnostic-documentation-path %S/test-docs/ -typecheck %s 2>&1 | %FileCheck %s --check-prefix=CHECK-DESCRIPTIVE
44

55
// A diagnostic with no educational notes
66
let x = 1 +

0 commit comments

Comments
 (0)