Skip to content

Commit ff889ff

Browse files
committed
[clang][cas] Fix issue when we emit caching-related diagnostic after source processing is done
Introduce `FrontendOptions.MayEmitDiagnosticsAfterProcessingSourceFiles` to indicate that `CompilerInstance::ExecuteAction` should not "finish" the diagnostic client. This is set for caching compilations. rdar://108014441
1 parent 497705d commit ff889ff

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ class FrontendOptions {
375375
/// caching of compilation outputs. This is used for testing purposes.
376376
unsigned DisableCachedCompileJobReplay : 1;
377377

378+
/// Keep the diagnostic client open for receiving diagnostics after the source
379+
/// files have been processed.
380+
unsigned MayEmitDiagnosticsAfterProcessingSourceFiles : 1;
381+
378382
/// Output (and read) PCM files regardless of compiler errors.
379383
unsigned AllowPCMWithCompilerErrors : 1;
380384

@@ -570,10 +574,11 @@ class FrontendOptions {
570574
ASTDumpDecls(false), ASTDumpLookups(false),
571575
BuildingImplicitModule(false), BuildingImplicitModuleUsesLock(true),
572576
ModulesEmbedAllFiles(false), IncludeTimestamps(true),
573-
UseTemporary(true),
574-
CacheCompileJob(false), DisableCachedCompileJobReplay(false),
575-
AllowPCMWithCompilerErrors(false),
576-
ModulesShareFileManager(true), TimeTraceGranularity(500) {}
577+
UseTemporary(true), CacheCompileJob(false),
578+
DisableCachedCompileJobReplay(false),
579+
MayEmitDiagnosticsAfterProcessingSourceFiles(false),
580+
AllowPCMWithCompilerErrors(false), ModulesShareFileManager(true),
581+
TimeTraceGranularity(500) {}
577582

578583
/// getInputKindForExtension - Return the appropriate input kind for a file
579584
/// extension. For example, "c" would return Language::C.

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,10 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
10401040
noteBottomOfStack();
10411041

10421042
auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
1043-
// Notify the diagnostic client that all files were processed.
1044-
getDiagnosticClient().finish();
1043+
if (!getFrontendOpts().MayEmitDiagnosticsAfterProcessingSourceFiles) {
1044+
// Notify the diagnostic client that all files were processed.
1045+
getDiagnosticClient().finish();
1046+
}
10451047
});
10461048

10471049
raw_ostream &OS = getVerboseOutputStream();
@@ -1244,6 +1246,7 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
12441246
// Force implicitly-built modules to hash the content of the module file.
12451247
HSOpts.ModulesHashContent = true;
12461248
FrontendOpts.Inputs = {Input};
1249+
FrontendOpts.MayEmitDiagnosticsAfterProcessingSourceFiles = false;
12471250

12481251
// Don't free the remapped file buffers; they are owned by our caller.
12491252
PPOpts.RetainRemappedFileBuffers = true;

clang/test/CAS/test-for-deterministic-outputs.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919
// CACHE-SKIPPED: remark: compile job cache skipped
2020

2121
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas %clang-cache \
22-
// RUN: %clang -target x86_64-apple-macos11 -c %s -o %t/t.o -Rcompile-job-cache -Wreproducible-caching 2> %t/out.txt
23-
// RUN: FileCheck %s --check-prefix=CACHE-WARN --input-file=%t/out.txt
22+
// RUN: %clang -target x86_64-apple-macos11 -c %s -o %t/t.o -Rcompile-job-cache -Wreproducible-caching -serialize-diagnostics %t/t.dia 2> %t/out.txt
23+
// RUN: FileCheck %s --check-prefix=CACHE-WARN --input-file=%t/out.txt -DREMARK=remark
24+
// RUN: c-index-test -read-diagnostics %t/t.dia 2>&1 | FileCheck %s --check-prefix=CACHE-WARN -DREMARK=warning
2425

2526
/// Check still a cache miss.
2627
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas %clang-cache \
2728
// RUN: %clang -target x86_64-apple-macos11 -c %s -o %t/t.o -Rcompile-job-cache -Wreproducible-caching 2> %t/out.txt
28-
// RUN: FileCheck %s --check-prefix=CACHE-WARN --input-file=%t/out.txt
29+
// RUN: FileCheck %s --check-prefix=CACHE-WARN --input-file=%t/out.txt -DREMARK=remark
2930

30-
// CACHE-WARN: remark: compile job cache miss
31+
// CACHE-WARN: [[REMARK]]: compile job cache miss
3132
// CACHE-WARN: warning: encountered non-reproducible token, caching will be skipped
3233
// CACHE-WARN: warning: encountered non-reproducible token, caching will be skipped
3334
// CACHE-WARN: warning: encountered non-reproducible token, caching will be skipped
34-
// CACHE-WARN: remark: compile job cache skipped
35+
// CACHE-WARN: [[REMARK]]: compile job cache skipped
3536

3637
/// Check -Werror doesn't actually error when we use the launcher.
3738
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas %clang-cache \

clang/tools/driver/cc1_main.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,15 +1220,17 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
12201220

12211221
DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
12221222

1223-
auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
1223+
auto FinishDiagnosticClient = [&]() {
12241224
// Notify the diagnostic client that all files were processed.
12251225
Clang->getDiagnosticClient().finish();
12261226

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

12331235
if (!Success)
12341236
return 1;
@@ -1241,8 +1243,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
12411243
if (std::optional<int> Status = JobCache.tryReplayCachedResult(*Clang))
12421244
return *Status; // FIXME: Should write out timers before exiting!
12431245

1244-
// ExecuteAction takes responsibility.
1245-
FinishDiagnosticClient.release();
1246+
Clang->getFrontendOpts().MayEmitDiagnosticsAfterProcessingSourceFiles = true;
12461247

12471248
// Execute the frontend actions.
12481249
{
@@ -1294,6 +1295,10 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
12941295
}
12951296
}
12961297

1298+
// Call this before the Clang pointer is moved below.
1299+
FinishDiagnosticClient();
1300+
FinishDiagnosticClientScope.release();
1301+
12971302
// When running with -disable-free, don't do any destruction or shutdown.
12981303
if (Clang->getFrontendOpts().DisableFree) {
12991304
llvm::BuryPointer(std::move(Clang));

clang/tools/driver/driver.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
382382
int RC = cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
383383
if (RC != 0)
384384
return RC;
385-
// FIXME: cc1_main should possibly clean up global state itself.
386-
llvm::remove_fatal_error_handler();
387385
}
388386
return cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
389387
}

0 commit comments

Comments
 (0)