Skip to content

Commit ff1155f

Browse files
committed
[clang][cas] Dump module cache key if it fails to load
If a module fails to load via fmodule-file-cache-key, dump out the structured form of the key (compiler arguments, cas-fs/include-tree) in the diagnostic. This makes it easier to triage such issues when only a build log is available, since it can be used to figure out if the command to produce the expected module was not run, or was run with the wrong compiler arguments, or inputs. While the diagnostic message with be "large" it is expected that these errors are rare and typically implicate a bug in the compiler or in the build system rather than an error the user is responsible for. (cherry picked from commit a4c1397)
1 parent c22f1fd commit ff1155f

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "clang/Config/config.h"
2525
#include "clang/Frontend/CASDependencyCollector.h"
2626
#include "clang/Frontend/ChainedDiagnosticConsumer.h"
27+
#include "clang/Frontend/CompileJobCacheKey.h"
2728
#include "clang/Frontend/CompileJobCacheResult.h"
2829
#include "clang/Frontend/FrontendAction.h"
2930
#include "clang/Frontend/FrontendActions.h"
@@ -2369,10 +2370,16 @@ static bool addCachedModuleFileToInMemoryCache(
23692370
if (!Value || !*Value) {
23702371
auto Diag = Diags.Report(diag::err_cas_cannot_get_module_cache_key)
23712372
<< CacheKey << Provider;
2372-
if (!Value)
2373+
if (!Value) {
23732374
Diag << Value.takeError();
2374-
else
2375-
Diag << "no such entry in action cache";
2375+
} else {
2376+
std::string ErrStr("no such entry in action cache; expected compile:\n");
2377+
llvm::raw_string_ostream Err(ErrStr);
2378+
if (auto E = printCompileJobCacheKey(CAS, *ID, Err))
2379+
Diag << std::move(E);
2380+
else
2381+
Diag << Err.str();
2382+
}
23762383
return true;
23772384
}
23782385
auto ValueRef = CAS.getReference(**Value);

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,48 @@
2828

2929
// BAD_KEY: error: CAS cannot load module with key 'KEY' from -fmodule-file-cache-key: invalid cas-id 'KEY'
3030

31+
// RUN: echo -n '-fmodule-file-cache-key=PATH=' > %t/bad_key2.rsp
32+
// RUN: cat %t/casid >> %t/bad_key2.rsp
33+
34+
// RUN: not %clang_cc1 -triple x86_64-apple-macos11 \
35+
// RUN: -fmodules -fno-implicit-modules \
36+
// RUN: @%t/bad_key2.rsp \
37+
// RUN: -fsyntax-only %t/tu.c \
38+
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache -fcas-fs @%t/casid \
39+
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/bad_key2.txt
40+
// RUN: cat %t/bad_key2.txt | FileCheck %s -check-prefix=BAD_KEY2
41+
42+
// BAD_KEY2: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: cas object is not a valid cache key
43+
44+
// == Build A
45+
46+
// RUN: %clang_cc1 -triple x86_64-apple-macos11 \
47+
// RUN: -fmodules -fmodule-name=A -fno-implicit-modules \
48+
// RUN: -emit-module %t/module.modulemap -o %t/A.pcm \
49+
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache -fcas-fs @%t/casid \
50+
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/A.out.txt
51+
// RUN: cat %t/A.out.txt | FileCheck %s --check-prefix=CACHE-MISS
52+
// CACHE-MISS: remark: compile job cache miss
53+
// RUN: cat %t/A.out.txt | sed -E "s:^.*cache [a-z]+ for '([^']+)'.*$:\1:" > %t/A.key
54+
55+
// == Try to import A with a different action cache, simulating a missing module
56+
3157
// RUN: echo -n '-fmodule-file-cache-key=PATH=' > %t/not_in_cache.rsp
32-
// RUN: cat %t/casid >> %t/not_in_cache.rsp
58+
// RUN: cat %t/A.key >> %t/not_in_cache.rsp
3359

3460
// RUN: not %clang_cc1 -triple x86_64-apple-macos11 \
3561
// RUN: -fmodules -fno-implicit-modules \
3662
// RUN: @%t/not_in_cache.rsp \
3763
// RUN: -fsyntax-only %t/tu.c \
38-
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache -fcas-fs @%t/casid \
64+
// RUN: -fcas-path %t/cas -faction-cache-path %t/cache_2 -fcas-fs @%t/casid \
3965
// RUN: -fcache-compile-job -Rcompile-job-cache &> %t/not_in_cache.txt
40-
// RUN: cat %t/not_in_cache.txt | FileCheck %s -check-prefix=NOT_IN_CACHE
66+
// RUN: cat %t/not_in_cache.txt | FileCheck %s -check-prefix=NOT_IN_CACHE -DPREFIX=%/t
4167

42-
// NOT_IN_CACHE: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: no such entry in action cache
68+
// NOT_IN_CACHE: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: no such entry in action cache; expected compile:
69+
// NOT_IN_CACHE: command-line:
70+
// NOT_IN_CACHE: -cc1
71+
// NOT_IN_CACHE: filesystem:
72+
// NOT_IN_CACHE: file llvmcas://{{.*}} [[PREFIX]]/A.h
4373

4474
//--- module.modulemap
4575
module A { header "A.h" }

0 commit comments

Comments
 (0)