Skip to content

[clang] Make serialized diagnostics more reliable #100681

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions clang/include/clang/Basic/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -1776,8 +1776,12 @@ class DiagnosticConsumer {
/// BeginSourceFile() are inaccessible.
virtual void EndSourceFile() {}

/// Callback to inform the diagnostic client that processing of all
/// source files has ended.
/// Callback to inform the diagnostic client that processing of all source
/// files has ended, and that no more diagnostics will be emitted.
///
/// This is only called when the DiagnosticConsumer's destructor would not
/// otherwise be called. Subclasses that require this to be called must ensure
/// that in their destructor.
virtual void finish() {}

/// Indicates whether the diagnostics handled by this
Expand Down
6 changes: 0 additions & 6 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,12 +1016,6 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
// better. We generally expect frontend actions to be invoked with (nearly)
// DesiredStackSpace available.
noteBottomOfStack();

auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
// Notify the diagnostic client that all files were processed.
getDiagnosticClient().finish();
});

raw_ostream &OS = getVerboseOutputStream();

if (!Act.PrepareToExecute(*this))
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/Frontend/SerializedDiagnosticPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class SDiagsMerger : SerializedDiagnosticReader {
void writeRecordWithBlob(unsigned ID, RecordData &Record, StringRef Blob);
};

class SDiagsWriter : public DiagnosticConsumer {
class SDiagsWriter final : public DiagnosticConsumer {
friend class SDiagsRenderer;
friend class SDiagsMerger;

Expand All @@ -149,7 +149,13 @@ class SDiagsWriter : public DiagnosticConsumer {
EmitPreamble();
}

~SDiagsWriter() override {}
~SDiagsWriter() override {
// Not all uses of DiagnosticConsumer call finish, and not all uses invoke
// the destructor. This makes calling finish optional for cases where it
// is properly destructed.
if (!IsFinishing)
finish();
}

void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) override;
Expand Down
23 changes: 17 additions & 6 deletions clang/tools/driver/cc1_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "clang/Frontend/Utils.h"
#include "clang/FrontendTool/Utils.h"
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Config/llvm-config.h"
Expand Down Expand Up @@ -274,10 +275,21 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
static_cast<void*>(&Clang->getDiagnostics()));

DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
if (!Success) {

auto FinishDiagnosticClient = [&]() {
// Notify the diagnostic client that all files were processed.
Clang->getDiagnosticClient().finish();

// Our error handler depends on the Diagnostics object, which we're
// potentially about to delete. Uninstall the handler now so that any
// later errors use the default handling behavior instead.
llvm::remove_fatal_error_handler();
};
auto FinishDiagnosticClientScope =
llvm::make_scope_exit([&]() { FinishDiagnosticClient(); });

if (!Success)
return 1;
}

// Execute the frontend actions.
{
Expand Down Expand Up @@ -314,10 +326,9 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
}
}

// Our error handler depends on the Diagnostics object, which we're
// potentially about to delete. Uninstall the handler now so that any
// later errors use the default handling behavior instead.
llvm::remove_fatal_error_handler();
// Call this before the Clang pointer is moved below.
FinishDiagnosticClient();
FinishDiagnosticClientScope.release();

// When running with -disable-free, don't do any destruction or shutdown.
if (Clang->getFrontendOpts().DisableFree) {
Expand Down
Loading