Skip to content

[Diagnostics] Updated diagnostic printing style #30027

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 19 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7b5ecad
[Diagnostics] Experimental diagnostic printing updates
owenv Oct 10, 2019
4dad89e
[Diagnostics] Stage educational notes and experimental formatting beh…
owenv Feb 23, 2020
d01dc92
[Diagnostics] Refactor expensive line lookups in diag formatting
owenv Feb 23, 2020
5e6ae98
[Diagnostics] minor experimental formatting style refactor
owenv Feb 23, 2020
183a4d2
[Diagnostics] Remove dead formatting code
owenv Feb 23, 2020
ad3fa79
[Diagnostics] Refactor some PrintingDiagnosticConsumer code into a fl…
owenv Feb 24, 2020
d87e913
[Diag-Experimental-Formatting] Custom formatting for Xcode editor pla…
owenv Feb 26, 2020
d2db08f
[Diagnostics] Make the experimental formatting tests less fragile
owenv Feb 26, 2020
1d9c00a
[Diag-Experimental-Formatting] Better and more consistent textual des…
owenv Feb 27, 2020
3382965
[Diag-Experimental-Formatting] Update some flag usage in tests post r…
owenv Feb 27, 2020
29b63c0
[Diags-Experimental-Formatting] Handle lines with tab characters corr…
owenv Feb 27, 2020
bde8f32
[Diag-Experimental-Formatting] Refactor byte-to-column mapping for ef…
owenv Feb 27, 2020
4fc9af8
[Diag-Experimental-Formatting] Fix line number indent calculation
owenv Feb 27, 2020
5ee88d0
[Diag-Experimental-Formatting] Include indicators of insertions and d…
owenv Feb 27, 2020
c03ffd8
[Diag-Experimental-Formatting] Change color of indicator arrow for no…
owenv Feb 27, 2020
8566c15
[Diag-Experimental-Formatting] Document some recent changes
owenv Feb 27, 2020
3d38dfd
[Diag-experimental-formatting] Make tests less sensitive to line numb…
owenv Feb 28, 2020
00d43d3
[Diag-Experimental-Formatting] Update tests to allow windows path sep…
owenv Feb 28, 2020
45a5883
[Diag-Experimental-Formatting] Bug fixes for the integrated REPL
owenv Feb 29, 2020
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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class DiagnosticConsumer {
/// \returns true if an error occurred while finishing-up.
virtual bool finishProcessing() { return false; }

/// Flush any in-flight diagnostics.
virtual void flush() {}

/// In batch mode, any error causes failure for all primary files, but
/// anyone consulting .dia files will only see an error for a particular
/// primary in that primary's serialized diagnostics file. For other
Expand Down
17 changes: 9 additions & 8 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ namespace swift {
/// Print diagnostic names after their messages
bool printDiagnosticNames = false;

/// Use descriptive diagnostic style when available.
bool useDescriptiveDiagnostics = false;
/// Use educational notes when available.
bool useEducationalNotes = false;

/// Path to diagnostic documentation directory.
std::string diagnosticDocumentationPath = "";
Expand Down Expand Up @@ -705,6 +705,11 @@ namespace swift {
return state.getShowDiagnosticsAfterFatalError();
}

void flushConsumers() {
for (auto consumer : Consumers)
consumer->flush();
}

/// Whether to skip emitting warnings
void setSuppressWarnings(bool val) { state.setSuppressWarnings(val); }
bool getSuppressWarnings() const {
Expand All @@ -725,12 +730,8 @@ namespace swift {
return printDiagnosticNames;
}

void setUseDescriptiveDiagnostics(bool val) {
useDescriptiveDiagnostics = val;
}
bool getUseDescriptiveDiagnostics() const {
return useDescriptiveDiagnostics;
}
void setUseEducationalNotes(bool val) { useEducationalNotes = val; }
bool getUseEducationalNotes() const { return useEducationalNotes; }

void setDiagnosticDocumentationPath(std::string path) {
diagnosticDocumentationPath = path;
Expand Down
10 changes: 7 additions & 3 deletions include/swift/Basic/DiagnosticOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ class DiagnosticOptions {
// When printing diagnostics, include the diagnostic name at the end
bool PrintDiagnosticNames = false;

/// If set to true, produce more descriptive diagnostic output if available.
/// Descriptive diagnostic output is not intended to be machine-readable.
bool EnableDescriptiveDiagnostics = false;
/// If set to true, display educational note content to the user if available.
/// Educational notes are documentation which supplement diagnostics.
bool EnableEducationalNotes = false;

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

std::string DiagnosticDocumentationPath = "";

Expand Down
2 changes: 2 additions & 0 deletions include/swift/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class SourceManager {
SourceLoc();
}

std::string getLineString(unsigned BufferID, unsigned LineNumber);

SourceLoc getLocFromExternalSource(StringRef Path, unsigned Line, unsigned Col);
private:
const VirtualFile *getVirtualFile(SourceLoc Loc) const;
Expand Down
21 changes: 18 additions & 3 deletions include/swift/Frontend/PrintingDiagnosticConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -25,24 +25,39 @@
#include "llvm/Support/Process.h"

namespace swift {
class AnnotatedSourceSnippet;

/// Diagnostic consumer that displays diagnostics to standard error.
class PrintingDiagnosticConsumer : public DiagnosticConsumer {
llvm::raw_ostream &Stream;
bool ForceColors = false;
bool DidErrorOccur = false;
bool ExperimentalFormattingEnabled = false;
// 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.
std::unique_ptr<AnnotatedSourceSnippet> currentSnippet;

public:
PrintingDiagnosticConsumer(llvm::raw_ostream &stream = llvm::errs()) :
Stream(stream) { }
PrintingDiagnosticConsumer(llvm::raw_ostream &stream = llvm::errs());
~PrintingDiagnosticConsumer();

virtual void handleDiagnostic(SourceManager &SM,
const DiagnosticInfo &Info) override;

virtual bool finishProcessing() override;

void flush(bool includeTrailingBreak);

virtual void flush() override { flush(false); }

void forceColors() {
ForceColors = true;
llvm::sys::Process::UseANSIEscapeCodes(true);
}

void enableExperimentalFormatting() { ExperimentalFormattingEnabled = true; }

bool didErrorOccur() {
return DidErrorOccur;
}
Expand Down
8 changes: 6 additions & 2 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ 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_descriptive_diagnostics : Flag<["-"], "enable-descriptive-diagnostics">,
HelpText<"Show descriptive diagnostic information, if available.">;
def enable_educational_notes : Flag<["-"], "enable-educational-notes">,
HelpText<"Show educational notes, if available.">;

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>">,
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
info->ChildDiagnosticInfo = childInfoPtrs;

SmallVector<std::string, 1> educationalNotePaths;
if (useDescriptiveDiagnostics) {
if (useEducationalNotes) {
auto associatedNotes = educationalNotes[(uint32_t)diagnostic.getID()];
while (associatedNotes && *associatedNotes) {
SmallString<128> notePath(getDiagnosticDocumentationPath());
Expand Down
5 changes: 3 additions & 2 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,9 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
Opts.WarningsAsErrors |= Args.hasArg(OPT_warnings_as_errors);
Opts.PrintDiagnosticNames |= Args.hasArg(OPT_debug_diagnostic_names);
Opts.EnableDescriptiveDiagnostics |=
Args.hasArg(OPT_enable_descriptive_diagnostics);
Opts.EnableEducationalNotes |= Args.hasArg(OPT_enable_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
4 changes: 2 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ void CompilerInstance::setUpDiagnosticOptions() {
if (Invocation.getDiagnosticOptions().PrintDiagnosticNames) {
Diagnostics.setPrintDiagnosticNames(true);
}
if (Invocation.getDiagnosticOptions().EnableDescriptiveDiagnostics) {
Diagnostics.setUseDescriptiveDiagnostics(true);
if (Invocation.getDiagnosticOptions().EnableEducationalNotes) {
Diagnostics.setUseEducationalNotes(true);
}
Diagnostics.setDiagnosticDocumentationPath(
Invocation.getDiagnosticOptions().DiagnosticDocumentationPath);
Expand Down
Loading