Skip to content

Commit 85a7598

Browse files
akyrtzibenlangmuir
authored andcommitted
[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 (cherry picked from commit ff889ff)
1 parent c09f6d0 commit 85a7598

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 7 additions & 1 deletion
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,7 +574,9 @@ class FrontendOptions {
570574
ASTDumpLookups(false), BuildingImplicitModule(false),
571575
BuildingImplicitModuleUsesLock(true), ModulesEmbedAllFiles(false),
572576
IncludeTimestamps(true), UseTemporary(true), CacheCompileJob(false),
573-
DisableCachedCompileJobReplay(false), AllowPCMWithCompilerErrors(false),
577+
DisableCachedCompileJobReplay(false),
578+
MayEmitDiagnosticsAfterProcessingSourceFiles(false),
579+
AllowPCMWithCompilerErrors(false),
574580
ModulesShareFileManager(true), TimeTraceGranularity(500) {}
575581

576582
/// getInputKindForExtension - Return the appropriate input kind for a file

clang/lib/Frontend/CompilerInstance.cpp

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

10931093
auto FinishDiagnosticClient = llvm::make_scope_exit([&]() {
1094-
// Notify the diagnostic client that all files were processed.
1095-
getDiagnosticClient().finish();
1094+
if (!getFrontendOpts().MayEmitDiagnosticsAfterProcessingSourceFiles) {
1095+
// Notify the diagnostic client that all files were processed.
1096+
getDiagnosticClient().finish();
1097+
}
10961098
});
10971099

10981100
raw_ostream &OS = getVerboseOutputStream();
@@ -1292,6 +1294,7 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
12921294
// Force implicitly-built modules to hash the content of the module file.
12931295
HSOpts.ModulesHashContent = true;
12941296
FrontendOpts.Inputs = {Input};
1297+
FrontendOpts.MayEmitDiagnosticsAfterProcessingSourceFiles = false;
12951298

12961299
// Don't free the remapped file buffers; they are owned by our caller.
12971300
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
@@ -1218,15 +1218,17 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
12181218

12191219
DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
12201220

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

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

12311233
if (!Success)
12321234
return 1;
@@ -1239,8 +1241,7 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
12391241
if (Optional<int> Status = JobCache.tryReplayCachedResult(*Clang))
12401242
return *Status; // FIXME: Should write out timers before exiting!
12411243

1242-
// ExecuteAction takes responsibility.
1243-
FinishDiagnosticClient.release();
1244+
Clang->getFrontendOpts().MayEmitDiagnosticsAfterProcessingSourceFiles = true;
12441245

12451246
// Execute the frontend actions.
12461247
{
@@ -1279,6 +1280,10 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
12791280
}
12801281
}
12811282

1283+
// Call this before the Clang pointer is moved below.
1284+
FinishDiagnosticClient();
1285+
FinishDiagnosticClientScope.release();
1286+
12821287
// When running with -disable-free, don't do any destruction or shutdown.
12831288
if (Clang->getFrontendOpts().DisableFree) {
12841289
llvm::BuryPointer(std::move(Clang));

clang/tools/driver/driver.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
372372
int RC = cc1_main(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
373373
if (RC != 0)
374374
return RC;
375-
// FIXME: cc1_main should possibly clean up global state itself.
376-
llvm::remove_fatal_error_handler();
377375
}
378376
return cc1_main(makeArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
379377
}

0 commit comments

Comments
 (0)