Skip to content

Commit e40e86f

Browse files
authored
Merge pull request #32017 from owenv/diag-style-opt
[Diagnostics] Add -diagnostic-style=(llvm|swift) to control printed output
2 parents f95de76 + c4e67e2 commit e40e86f

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
@@ -244,6 +244,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
244244
inputArgs.AddLastArg(arguments, options::OPT_serialize_diagnostics_path);
245245
inputArgs.AddLastArg(arguments, options::OPT_debug_diagnostic_names);
246246
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);
247+
inputArgs.AddLastArg(arguments, options::OPT_diagnostic_style);
247248
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
248249
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
249250
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
@@ -887,19 +887,33 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
887887
Opts.SkipDiagnosticPasses |= Args.hasArg(OPT_disable_diagnostic_passes);
888888
Opts.ShowDiagnosticsAfterFatalError |=
889889
Args.hasArg(OPT_show_diagnostics_after_fatal);
890+
890891
Opts.UseColor |=
891892
Args.hasFlag(OPT_color_diagnostics,
892893
OPT_no_color_diagnostics,
893894
/*Default=*/llvm::sys::Process::StandardErrHasColors());
895+
// If no style options are specified, default to LLVM style.
896+
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::LLVM;
897+
if (const Arg *arg = Args.getLastArg(OPT_diagnostic_style)) {
898+
StringRef contents = arg->getValue();
899+
if (contents == "llvm") {
900+
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::LLVM;
901+
} else if (contents == "swift") {
902+
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::Swift;
903+
} else {
904+
Diags.diagnose(SourceLoc(), diag::error_unsupported_option_argument,
905+
arg->getOption().getPrefixedName(), arg->getValue());
906+
return true;
907+
}
908+
}
909+
894910
Opts.FixitCodeForAllDiagnostics |= Args.hasArg(OPT_fixit_all);
895911
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
896912
Opts.WarningsAsErrors = Args.hasFlag(options::OPT_warnings_as_errors,
897913
options::OPT_no_warnings_as_errors,
898914
false);
899915
Opts.PrintDiagnosticNames |= Args.hasArg(OPT_debug_diagnostic_names);
900916
Opts.PrintEducationalNotes |= Args.hasArg(OPT_print_educational_notes);
901-
Opts.EnableExperimentalFormatting |=
902-
Args.hasArg(OPT_enable_experimental_diagnostic_formatting);
903917
if (Arg *A = Args.getLastArg(OPT_diagnostic_documentation_path)) {
904918
Opts.DiagnosticDocumentationPath = A->getValue();
905919
}

lib/Frontend/PrintingDiagnosticConsumer.cpp

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

937-
if (ExperimentalFormattingEnabled) {
937+
switch (FormattingStyle) {
938+
case DiagnosticOptions::FormattingStyle::Swift:
938939
if (Info.Kind == DiagnosticKind::Note && currentSnippet) {
939940
// If this is a note and we have an in-flight message, add it to that
940941
// instead of emitting it separately.
@@ -952,7 +953,9 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
952953
BufferedEducationalNotes.push_back(buffer->get()->getBuffer().str());
953954
}
954955
}
955-
} else {
956+
break;
957+
958+
case DiagnosticOptions::FormattingStyle::LLVM:
956959
printDiagnostic(SM, Info);
957960

958961
if (PrintEducationalNotes) {
@@ -967,6 +970,7 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
967970
for (auto ChildInfo : Info.ChildDiagnosticInfo) {
968971
printDiagnostic(SM, *ChildInfo);
969972
}
973+
break;
970974
}
971975
}
972976

lib/FrontendTool/FrontendTool.cpp

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

2127-
// Temporarily stage the new diagnostic formatting style behind
2128-
// -enable-descriptive-diagnostics
2129-
if (Invocation.getDiagnosticOptions().EnableExperimentalFormatting)
2130-
PDC.enableExperimentalFormatting();
2127+
PDC.setFormattingStyle(
2128+
Invocation.getDiagnosticOptions().PrintedFormattingStyle);
21312129

21322130
if (Invocation.getFrontendOptions().DebugTimeCompilation)
21332131
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 +

test/diagnostics/pretty-printed-diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swift-frontend -enable-experimental-diagnostic-formatting -typecheck %s 2>&1 | %FileCheck %s
1+
// RUN: not %target-swift-frontend -diagnostic-style=swift -typecheck %s 2>&1 | %FileCheck %s
22

33
1 + 2
44

test/diagnostics/pretty-printed-diags-in-clang-buffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t.mcp)
2-
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-diagnostic-formatting -I %S/Inputs/ -typecheck %s -module-cache-path %t.mcp 2>&1 | %FileCheck %s
2+
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -diagnostic-style=swift -I %S/Inputs/ -typecheck %s -module-cache-path %t.mcp 2>&1 | %FileCheck %s
33

44
// REQUIRES: objc_interop
55

test/diagnostics/pretty-printed-source-loc-directive-diags.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-swift-frontend -enable-experimental-diagnostic-formatting -typecheck %s 2>&1 | %FileCheck %s
1+
// RUN: not %target-swift-frontend -diagnostic-style=swift -typecheck %s 2>&1 | %FileCheck %s
22

33
// Error split between the real file and a virtual one.
44
#sourceLocation(file: "abc.swift", line: 9)

0 commit comments

Comments
 (0)