Skip to content

Commit a6aa93c

Browse files
[CAS] Improve error message when module cache key is missing
When a module cache key is missing, emit better diagnostics that * It is clear it is a cache key missing, so a potential dependency missing, not a CAS error * For other compiler consumers, like a swift compiler that uses a different cache key schema than the clang include tree, it is possible that a detailed cache key cannot be printed. Don't emit extra schema mismatch message that can confuse the users rdar://149707188
1 parent 157de7b commit a6aa93c

File tree

6 files changed

+40
-37
lines changed

6 files changed

+40
-37
lines changed

clang/include/clang/Basic/DiagnosticCASKinds.td

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def err_cas_depscan_daemon_connection: Error<
2323
def err_cas_depscan_failed: Error<
2424
"CAS-based dependency scan failed: %0">, DefaultFatal;
2525
def err_cas_store: Error<"failed to store to CAS: %0">, DefaultFatal;
26-
def err_cas_cannot_get_module_cache_key : Error<
27-
"CAS cannot load module with key '%0' from %1: %2">, DefaultFatal;
26+
def err_cas_unloadable_module : Error<
27+
"module file '%0' not found: unloadable module cache key %1: %2">, DefaultFatal;
28+
def err_cas_missing_module : Error<
29+
"module file '%0' not found: missing module cache key %1: %2">, DefaultFatal;
2830
def err_cas_missing_root_id : Error<
2931
"CAS missing expected root-id '%0'">, DefaultFatal;
3032
def err_cas_cannot_parse_include_tree_id : Error<

clang/lib/Frontend/CompileJobCacheKey.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,7 @@ Error clang::printCompileJobCacheKey(ObjectStore &CAS, const CASID &Key,
364364
if (!H)
365365
return H.takeError();
366366
TreeSchema Schema(CAS);
367-
if (!Schema.isNode(*H)) {
368-
std::string ErrStr;
369-
llvm::raw_string_ostream Err(ErrStr);
370-
Err << "expected cache key to be a CAS tree; got ";
371-
H->getID().print(Err);
372-
return createStringError(inconvertibleErrorCode(), Err.str());
373-
}
367+
if (!Schema.isNode(*H))
368+
return createStringError("unexpected cache key schema");
374369
return ::printCompileJobCacheKey(CAS, *H, OS);
375370
}

clang/lib/Frontend/CompilerInstance.cpp

+26-21
Original file line numberDiff line numberDiff line change
@@ -2579,40 +2579,45 @@ static bool addCachedModuleFileToInMemoryCache(
25792579

25802580
auto ID = CAS.parseID(CacheKey);
25812581
if (!ID) {
2582-
Diags.Report(diag::err_cas_cannot_get_module_cache_key)
2583-
<< CacheKey << Provider << ID.takeError();
2582+
Diags.Report(diag::err_cas_unloadable_module)
2583+
<< Path << CacheKey << ID.takeError();
25842584
return true;
25852585
}
25862586

25872587
auto Value = Cache.get(*ID);
2588-
if (!Value || !*Value) {
2589-
auto Diag = Diags.Report(diag::err_cas_cannot_get_module_cache_key)
2590-
<< CacheKey << Provider;
2591-
if (!Value) {
2592-
Diag << Value.takeError();
2593-
} else {
2594-
std::string ErrStr("no such entry in action cache; expected compile:\n");
2595-
llvm::raw_string_ostream Err(ErrStr);
2596-
if (auto E = printCompileJobCacheKey(CAS, *ID, Err))
2597-
Diag << std::move(E);
2598-
else
2599-
Diag << Err.str();
2600-
}
2588+
if (!Value) {
2589+
Diags.Report(diag::err_cas_unloadable_module)
2590+
<< Path << CacheKey << Value.takeError();
2591+
return true;
2592+
}
2593+
if (!*Value) {
2594+
auto Diag = Diags.Report(diag::err_cas_missing_module)
2595+
<< Path << CacheKey;
2596+
std::string ErrStr("expected to be produced by:\n");
2597+
llvm::raw_string_ostream Err(ErrStr);
2598+
if (auto E = printCompileJobCacheKey(CAS, *ID, Err)) {
2599+
// Ignore the error and skip printing the cache key. The cache key can
2600+
// be setup by a different compiler that is using an unknown schema.
2601+
llvm::consumeError(std::move(E));
2602+
Diag << "module file is not available in the CAS";
2603+
} else
2604+
Diag << Err.str();
2605+
26012606
return true;
26022607
}
26032608
auto ValueRef = CAS.getReference(**Value);
26042609
if (!ValueRef) {
2605-
Diags.Report(diag::err_cas_cannot_get_module_cache_key)
2606-
<< CacheKey << Provider << "result module doesn't exist in CAS";
2610+
Diags.Report(diag::err_cas_unloadable_module)
2611+
<< Path << CacheKey << "result module cannot be loaded from CAS";
26072612

26082613
return true;
26092614
}
26102615

26112616
std::optional<cas::CompileJobCacheResult> Result;
26122617
cas::CompileJobResultSchema Schema(CAS);
26132618
if (llvm::Error E = Schema.load(*ValueRef).moveInto(Result)) {
2614-
Diags.Report(diag::err_cas_cannot_get_module_cache_key)
2615-
<< CacheKey << Provider << std::move(E);
2619+
Diags.Report(diag::err_cas_unloadable_module)
2620+
<< Path << CacheKey << std::move(E);
26162621
return true;
26172622
}
26182623
auto Output =
@@ -2625,8 +2630,8 @@ static bool addCachedModuleFileToInMemoryCache(
26252630
// better network utilization.
26262631
auto OutputProxy = CAS.getProxy(Output->Object);
26272632
if (!OutputProxy) {
2628-
Diags.Report(diag::err_cas_cannot_get_module_cache_key)
2629-
<< CacheKey << Provider << OutputProxy.takeError();
2633+
Diags.Report(diag::err_cas_unloadable_module)
2634+
<< Path << CacheKey << OutputProxy.takeError();
26302635
return true;
26312636
}
26322637

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,11 @@ static void checkCompileCacheKeyMatch(cas::ObjectStore &CAS,
667667
llvm::report_fatal_error(OldKey.takeError());
668668
SmallString<256> Err;
669669
llvm::raw_svector_ostream OS(Err);
670-
OS << "Compile cache key for module changed; previously:";
670+
OS << "Compile cache key for module changed; previously: "
671+
<< OldKey->toString() << ": ";
671672
if (auto E = printCompileJobCacheKey(CAS, *OldKey, OS))
672673
OS << std::move(E);
673-
OS << "\nkey is now:";
674+
OS << "\nkey is now: " << NewKey.toString() << ": ";
674675
if (auto E = printCompileJobCacheKey(CAS, NewKey, OS))
675676
OS << std::move(E);
676677
llvm::report_fatal_error(OS.str());

clang/test/CAS/fmodule-file-cache-key-errors.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/invalid2.txt
2727
// RUN: FileCheck %s -check-prefix=INVALID2 -input-file=%t/invalid2.txt
2828

29-
// INVALID2: error: CAS cannot load module with key '-fsyntax-only' from -fmodule-file-cache-key
29+
// INVALID2: error: module file 'INVALID' not found: unloadable module cache key -fsyntax-only: invalid cas-id '-fsyntax-only'
3030

3131
// RUN: not %clang_cc1 -triple x86_64-apple-macos11 \
3232
// RUN: -fmodules -fno-implicit-modules \
@@ -46,7 +46,7 @@
4646
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/bad_key.txt
4747
// RUN: cat %t/bad_key.txt | FileCheck %s -check-prefix=BAD_KEY
4848

49-
// BAD_KEY: error: CAS cannot load module with key 'KEY' from -fmodule-file-cache-key: invalid cas-id 'KEY'
49+
// BAD_KEY: error: module file 'PATH' not found: unloadable module cache key KEY: invalid cas-id 'KEY'
5050

5151
// RUN: echo -n '-fmodule-file-cache-key PATH ' > %t/bad_key2.rsp
5252
// RUN: cat %t/casid >> %t/bad_key2.rsp
@@ -59,7 +59,7 @@
5959
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/bad_key2.txt
6060
// RUN: cat %t/bad_key2.txt | FileCheck %s -check-prefix=BAD_KEY2
6161

62-
// BAD_KEY2: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: cas object is not a valid cache key
62+
// BAD_KEY2: error: module file 'PATH' not found: missing module cache key {{.*}}: module file is not available in the CAS
6363

6464
// == Build A
6565

@@ -87,7 +87,7 @@
8787
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/not_in_cache.txt
8888
// RUN: cat %t/not_in_cache.txt | FileCheck %s -check-prefix=NOT_IN_CACHE -DPREFIX=%/t
8989

90-
// NOT_IN_CACHE: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: no such entry in action cache; expected compile:
90+
// NOT_IN_CACHE: error: module file 'PATH' not found: missing module cache key {{.*}}: expected to be produced by:
9191
// NOT_IN_CACHE: command-line:
9292
// NOT_IN_CACHE: -cc1
9393
// NOT_IN_CACHE: filesystem:

clang/test/ClangScanDeps/modules-cas-trees.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// Missing pcm in action cache
4343
// RUN: not %clang @%t/Left.rsp 2> %t/error.txt
4444
// RUN: cat %t/error.txt | FileCheck %s -check-prefix=MISSING
45-
// MISSING: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: no such entry in action cache
45+
// MISSING: error: module file '{{.*}}.pcm' not found: missing module cache key {{.*}}: expected to be produced by:
4646

4747
// Build everything
4848
// RUN: %clang @%t/Top.rsp 2>&1 | FileCheck %s -check-prefix=CACHE-MISS

0 commit comments

Comments
 (0)