Skip to content

[Diagnostics] Add -diagnostic-style=(llvm|swift) to control printed output #32017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/swift/Basic/DiagnosticOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class DiagnosticOptions {
VerifyAndApplyFixes
} VerifyMode = NoVerify;

enum FormattingStyle { LLVM, Swift };

/// Indicates whether to allow diagnostics for \c <unknown> locations if
/// \c VerifyMode is not \c NoVerify.
bool VerifyIgnoreUnknown = false;
Expand Down Expand Up @@ -61,7 +63,7 @@ class DiagnosticOptions {

// If set to true, use the more descriptive experimental formatting style for
// diagnostics.
bool EnableExperimentalFormatting = false;
FormattingStyle PrintedFormattingStyle = FormattingStyle::LLVM;

std::string DiagnosticDocumentationPath = "";

Expand Down
10 changes: 7 additions & 3 deletions include/swift/Frontend/PrintingDiagnosticConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#ifndef SWIFT_PRINTINGDIAGNOSTICCONSUMER_H
#define SWIFT_PRINTINGDIAGNOSTICCONSUMER_H

#include "swift/Basic/LLVM.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/Basic/DiagnosticOptions.h"
#include "swift/Basic/LLVM.h"

#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Process.h"
Expand All @@ -33,7 +34,8 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
bool ForceColors = false;
bool PrintEducationalNotes = false;
bool DidErrorOccur = false;
bool ExperimentalFormattingEnabled = false;
DiagnosticOptions::FormattingStyle FormattingStyle =
DiagnosticOptions::FormattingStyle::LLVM;
// The current snippet used to display an error/warning/remark and the notes
// implicitly associated with it. Uses `std::unique_ptr` so that
// `AnnotatedSourceSnippet` can be forward declared.
Expand Down Expand Up @@ -65,7 +67,9 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
PrintEducationalNotes = ShouldPrint;
}

void enableExperimentalFormatting() { ExperimentalFormattingEnabled = true; }
void setFormattingStyle(DiagnosticOptions::FormattingStyle style) {
FormattingStyle = style;
}

bool didErrorOccur() {
return DidErrorOccur;
Expand Down
4 changes: 0 additions & 4 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ def enable_cross_import_overlays : Flag<["-"], "enable-cross-import-overlays">,
def disable_cross_import_overlays : Flag<["-"], "disable-cross-import-overlays">,
HelpText<"Do not automatically import declared cross-import overlays.">;

def enable_experimental_diagnostic_formatting :
Flag<["-"], "enable-experimental-diagnostic-formatting">,
HelpText<"Enable experimental diagnostic formatting features.">;

def diagnostic_documentation_path
: Separate<["-"], "diagnostic-documentation-path">, MetaVarName<"<path>">,
HelpText<"Path to diagnostic documentation resources">;
Expand Down
7 changes: 7 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ def debug_diagnostic_names : Flag<["-"], "debug-diagnostic-names">,
def print_educational_notes : Flag<["-"], "print-educational-notes">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Include educational notes in printed diagnostic output, if available">;
def diagnostic_style : Separate<["-"], "diagnostic-style">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
MetaVarName<"<style>">,
HelpText<"The formatting style used when printing diagnostics ('swift' or 'llvm')">;
def diagnostic_style_EQ : Joined<["-"], "diagnostic-style=">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
MetaVarName<"<style>">, Alias<diagnostic_style>;

def module_cache_path : Separate<["-"], "module-cache-path">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,
Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_serialize_diagnostics_path);
inputArgs.AddLastArg(arguments, options::OPT_debug_diagnostic_names);
inputArgs.AddLastArg(arguments, options::OPT_print_educational_notes);
inputArgs.AddLastArg(arguments, options::OPT_diagnostic_style);
inputArgs.AddLastArg(arguments, options::OPT_enable_astscope_lookup);
inputArgs.AddLastArg(arguments, options::OPT_disable_astscope_lookup);
inputArgs.AddLastArg(arguments, options::OPT_disable_parser_lookup);
Expand Down
18 changes: 16 additions & 2 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,19 +884,33 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
Opts.SkipDiagnosticPasses |= Args.hasArg(OPT_disable_diagnostic_passes);
Opts.ShowDiagnosticsAfterFatalError |=
Args.hasArg(OPT_show_diagnostics_after_fatal);

Opts.UseColor |=
Args.hasFlag(OPT_color_diagnostics,
OPT_no_color_diagnostics,
/*Default=*/llvm::sys::Process::StandardErrHasColors());
// If no style options are specified, default to LLVM style.
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::LLVM;
if (const Arg *arg = Args.getLastArg(OPT_diagnostic_style)) {
StringRef contents = arg->getValue();
if (contents == "llvm") {
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::LLVM;
} else if (contents == "swift") {
Opts.PrintedFormattingStyle = DiagnosticOptions::FormattingStyle::Swift;
} else {
Diags.diagnose(SourceLoc(), diag::error_unsupported_option_argument,
arg->getOption().getPrefixedName(), arg->getValue());
return true;
}
}

Opts.FixitCodeForAllDiagnostics |= Args.hasArg(OPT_fixit_all);
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
Opts.WarningsAsErrors = Args.hasFlag(options::OPT_warnings_as_errors,
options::OPT_no_warnings_as_errors,
false);
Opts.PrintDiagnosticNames |= Args.hasArg(OPT_debug_diagnostic_names);
Opts.PrintEducationalNotes |= Args.hasArg(OPT_print_educational_notes);
Opts.EnableExperimentalFormatting |=
Args.hasArg(OPT_enable_experimental_diagnostic_formatting);
if (Arg *A = Args.getLastArg(OPT_diagnostic_documentation_path)) {
Opts.DiagnosticDocumentationPath = A->getValue();
}
Expand Down
8 changes: 6 additions & 2 deletions lib/Frontend/PrintingDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
if (Info.IsChildNote)
return;

if (ExperimentalFormattingEnabled) {
switch (FormattingStyle) {
case DiagnosticOptions::FormattingStyle::Swift:
if (Info.Kind == DiagnosticKind::Note && currentSnippet) {
// If this is a note and we have an in-flight message, add it to that
// instead of emitting it separately.
Expand All @@ -950,7 +951,9 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
BufferedEducationalNotes.push_back(buffer->get()->getBuffer().str());
}
}
} else {
break;

case DiagnosticOptions::FormattingStyle::LLVM:
printDiagnostic(SM, Info);

if (PrintEducationalNotes) {
Expand All @@ -965,6 +968,7 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
for (auto ChildInfo : Info.ChildDiagnosticInfo) {
printDiagnostic(SM, *ChildInfo);
}
break;
}
}

Expand Down
6 changes: 2 additions & 4 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2111,10 +2111,8 @@ int swift::performFrontend(ArrayRef<const char *> Args,
PDC.setPrintEducationalNotes(
Invocation.getDiagnosticOptions().PrintEducationalNotes);

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

if (Invocation.getFrontendOptions().DebugTimeCompilation)
SharedTimer::enableCompilationTimers();
Expand Down
2 changes: 1 addition & 1 deletion test/Driver/color-diagnostics.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swiftc_driver -color-diagnostics -emit-executable -o %t %s 2>&1 \
// RUN: not %target-swiftc_driver -color-diagnostics -diagnostic-style=llvm -emit-executable -o %t %s 2>&1 \
// RUN: | %FileCheck -check-prefix=CHECK-CD %s
// CHECK-CD: [0m1 = 2{{$}}

Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics/educational-notes.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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
// 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
// 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
// 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
// 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

// A diagnostic with no educational notes
let x = 1 +
Expand Down
2 changes: 1 addition & 1 deletion test/diagnostics/pretty-printed-diagnostics.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swift-frontend -enable-experimental-diagnostic-formatting -typecheck %s 2>&1 | %FileCheck %s
// RUN: not %target-swift-frontend -diagnostic-style=swift -typecheck %s 2>&1 | %FileCheck %s

1 + 2

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %empty-directory(%t.mcp)
// 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
// 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

// REQUIRES: objc_interop

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swift-frontend -enable-experimental-diagnostic-formatting -typecheck %s 2>&1 | %FileCheck %s
// RUN: not %target-swift-frontend -diagnostic-style=swift -typecheck %s 2>&1 | %FileCheck %s

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