Skip to content

[5.7][Parseable Output] Emit parseable messages on failure in CompilerInstance.setup #58902

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 31, 2022
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
157 changes: 84 additions & 73 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,57 +2072,8 @@ int swift::performFrontend(ArrayRef<const char *> Args,
PDC.setSuppressOutput(true);
}

// Because the serialized diagnostics consumer is initialized here,
// diagnostics emitted above, within CompilerInvocation::parseArgs, are never
// serialized. This is a non-issue because, in nearly all cases, frontend
// arguments are generated by the driver, not directly by a user. The driver
// is responsible for emitting diagnostics for its own errors. See SR-2683
// for details.
std::unique_ptr<DiagnosticConsumer> SerializedConsumerDispatcher =
createSerializedDiagnosticConsumerIfNeeded(
Invocation.getFrontendOptions().InputsAndOutputs);
if (SerializedConsumerDispatcher)
Instance->addDiagnosticConsumer(SerializedConsumerDispatcher.get());

std::unique_ptr<DiagnosticConsumer> FixItsConsumer =
createJSONFixItDiagnosticConsumerIfNeeded(Invocation);
if (FixItsConsumer)
Instance->addDiagnosticConsumer(FixItsConsumer.get());

if (Invocation.getDiagnosticOptions().UseColor)
PDC.forceColors();

PDC.setPrintEducationalNotes(
Invocation.getDiagnosticOptions().PrintEducationalNotes);

PDC.setFormattingStyle(
Invocation.getDiagnosticOptions().PrintedFormattingStyle);

if (Invocation.getFrontendOptions().PrintStats) {
llvm::EnableStatistics();
}

const DiagnosticOptions &diagOpts = Invocation.getDiagnosticOptions();
bool verifierEnabled = diagOpts.VerifyMode != DiagnosticOptions::NoVerify;

std::string InstanceSetupError;
if (Instance->setup(Invocation, InstanceSetupError)) {
return finishDiagProcessing(1, /*verifierEnabled*/ false);
}

// The compiler instance has been configured; notify our observer.
if (observer) {
observer->configuredCompiler(*Instance);
}

if (verifierEnabled) {
// Suppress printed diagnostic output during the compile if the verifier is
// enabled.
PDC.setSuppressOutput(true);
}

if (Invocation.getFrontendOptions().FrontendParseableOutput) {
const auto &IO = Invocation.getFrontendOptions().InputsAndOutputs;
auto emitParseableBeganMessage = [&Invocation, &Args]() {
const auto &IO = Invocation.getFrontendOptions().InputsAndOutputs;
const auto OSPid = getpid();
const auto ProcInfo = sys::TaskProcessInformation(OSPid);

Expand Down Expand Up @@ -2151,27 +2102,9 @@ int swift::performFrontend(ArrayRef<const char *> Args,
constructDetailedTaskDescription(Invocation, IO.getAllInputs(), Args),
OSPid, ProcInfo);
}
}

int ReturnValue = 0;
bool HadError = performCompile(*Instance, ReturnValue, observer);

if (verifierEnabled) {
DiagnosticEngine &diags = Instance->getDiags();
if (diags.hasFatalErrorOccurred() &&
!Invocation.getDiagnosticOptions().ShowDiagnosticsAfterFatalError) {
diags.resetHadAnyError();
PDC.setSuppressOutput(false);
diags.diagnose(SourceLoc(), diag::verify_encountered_fatal);
HadError = true;
}
}

auto r = finishDiagProcessing(HadError ? 1 : ReturnValue, verifierEnabled);
if (auto *StatsReporter = Instance->getStatsReporter())
StatsReporter->noteCurrentProcessExitStatus(r);
};

if (Invocation.getFrontendOptions().FrontendParseableOutput) {
auto emitParseableFinishedMessage = [&Invocation, &Args, &FileSpecificDiagnostics](int ExitStatus) {
const auto &IO = Invocation.getFrontendOptions().InputsAndOutputs;
const auto OSPid = getpid();
const auto ProcInfo = sys::TaskProcessInformation(OSPid);
Expand All @@ -2197,7 +2130,7 @@ int swift::performFrontend(ArrayRef<const char *> Args,

emitFinishedMessage(llvm::errs(),
mapFrontendInvocationToAction(Invocation),
JoinedDiags.str(), r, Pid - idx, ProcInfo);
JoinedDiags.str(), ExitStatus, Pid - idx, ProcInfo);
return false;
});
} else {
Expand All @@ -2214,10 +2147,88 @@ int swift::performFrontend(ArrayRef<const char *> Args,
std::ostream_iterator<std::string>(JoinedDiags, Delim));
emitFinishedMessage(llvm::errs(),
mapFrontendInvocationToAction(Invocation),
JoinedDiags.str(), r, OSPid, ProcInfo);
JoinedDiags.str(), ExitStatus, OSPid, ProcInfo);
}
};

// Because the serialized diagnostics consumer is initialized here,
// diagnostics emitted above, within CompilerInvocation::parseArgs, are never
// serialized. This is a non-issue because, in nearly all cases, frontend
// arguments are generated by the driver, not directly by a user. The driver
// is responsible for emitting diagnostics for its own errors. See SR-2683
// for details.
std::unique_ptr<DiagnosticConsumer> SerializedConsumerDispatcher =
createSerializedDiagnosticConsumerIfNeeded(
Invocation.getFrontendOptions().InputsAndOutputs);
if (SerializedConsumerDispatcher)
Instance->addDiagnosticConsumer(SerializedConsumerDispatcher.get());

std::unique_ptr<DiagnosticConsumer> FixItsConsumer =
createJSONFixItDiagnosticConsumerIfNeeded(Invocation);
if (FixItsConsumer)
Instance->addDiagnosticConsumer(FixItsConsumer.get());

if (Invocation.getDiagnosticOptions().UseColor)
PDC.forceColors();

PDC.setPrintEducationalNotes(
Invocation.getDiagnosticOptions().PrintEducationalNotes);

PDC.setFormattingStyle(
Invocation.getDiagnosticOptions().PrintedFormattingStyle);

if (Invocation.getFrontendOptions().PrintStats) {
llvm::EnableStatistics();
}


if (Invocation.getFrontendOptions().FrontendParseableOutput)
emitParseableBeganMessage();

const DiagnosticOptions &diagOpts = Invocation.getDiagnosticOptions();
bool verifierEnabled = diagOpts.VerifyMode != DiagnosticOptions::NoVerify;

std::string InstanceSetupError;
if (Instance->setup(Invocation, InstanceSetupError)) {
int ReturnCode = 1;
if (Invocation.getFrontendOptions().FrontendParseableOutput)
emitParseableFinishedMessage(ReturnCode);

return finishDiagProcessing(ReturnCode, /*verifierEnabled*/ false);
}

// The compiler instance has been configured; notify our observer.
if (observer) {
observer->configuredCompiler(*Instance);
}

if (verifierEnabled) {
// Suppress printed diagnostic output during the compile if the verifier is
// enabled.
PDC.setSuppressOutput(true);
}

int ReturnValue = 0;
bool HadError = performCompile(*Instance, ReturnValue, observer);

if (verifierEnabled) {
DiagnosticEngine &diags = Instance->getDiags();
if (diags.hasFatalErrorOccurred() &&
!Invocation.getDiagnosticOptions().ShowDiagnosticsAfterFatalError) {
diags.resetHadAnyError();
PDC.setSuppressOutput(false);
diags.diagnose(SourceLoc(), diag::verify_encountered_fatal);
HadError = true;
}
}

auto r = finishDiagProcessing(HadError ? 1 : ReturnValue, verifierEnabled);
if (auto *StatsReporter = Instance->getStatsReporter())
StatsReporter->noteCurrentProcessExitStatus(r);

if (Invocation.getFrontendOptions().FrontendParseableOutput)
emitParseableFinishedMessage(r);

return r;
}

Expand Down
9 changes: 9 additions & 0 deletions test/Frontend/parseable_output_early.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: not %target-swift-frontend(mock-sdk: -sdk %/S/../ModuleInterface/Inputs/BadStdlib.sdk -module-cache-path %/t/module-cache -resource-dir %/S/../ModuleInterface/Inputs/BadStdlib.sdk) -primary-file %s -o %t.out -emit-module -emit-module-path %t.swiftmodule -module-name parseable_output_early -frontend-parseable-output 2>&1 | %FileCheck %s

// CHECK: {{[1-9][0-9]*}}
// CHECK-NEXT: {
// CHECK-NEXT: "kind": "began",
// CHECK-NEXT: "name": "compile",

// CHECK: "kind": "finished",
// CHECK-NEXT: "name": "compile",