From 1225ae248586a9cbbc860d3cb1efb5603f5e5a47 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Mon, 21 Apr 2025 12:52:37 -0700 Subject: [PATCH] [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 --- clang/lib/Frontend/CompileJobCacheKey.cpp | 9 ++------- clang/lib/Frontend/CompilerInstance.cpp | 12 ++++++++---- .../DependencyScanning/ModuleDepCollector.cpp | 5 +++-- clang/test/CAS/fmodule-file-cache-key-errors.c | 4 ++-- clang/test/ClangScanDeps/modules-cas-trees.c | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/clang/lib/Frontend/CompileJobCacheKey.cpp b/clang/lib/Frontend/CompileJobCacheKey.cpp index 354700d688249..440dc1353776e 100644 --- a/clang/lib/Frontend/CompileJobCacheKey.cpp +++ b/clang/lib/Frontend/CompileJobCacheKey.cpp @@ -364,12 +364,7 @@ Error clang::printCompileJobCacheKey(ObjectStore &CAS, const CASID &Key, if (!H) return H.takeError(); TreeSchema Schema(CAS); - if (!Schema.isNode(*H)) { - std::string ErrStr; - llvm::raw_string_ostream Err(ErrStr); - Err << "expected cache key to be a CAS tree; got "; - H->getID().print(Err); - return createStringError(inconvertibleErrorCode(), Err.str()); - } + if (!Schema.isNode(*H)) + return createStringError("unexpected cache key schema"); return ::printCompileJobCacheKey(CAS, *H, OS); } diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 6057730b1cd76..9674680dea0ea 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -2591,11 +2591,15 @@ static bool addCachedModuleFileToInMemoryCache( if (!Value) { Diag << Value.takeError(); } else { - std::string ErrStr("no such entry in action cache; expected compile:\n"); + std::string ErrStr("module file is not available in the CAS; expected to " + "be produced by:\n"); llvm::raw_string_ostream Err(ErrStr); - if (auto E = printCompileJobCacheKey(CAS, *ID, Err)) - Diag << std::move(E); - else + if (auto E = printCompileJobCacheKey(CAS, *ID, Err)) { + // Ignore the error and skip printing the cache key. The cache key can + // be setup by a different compiler that is using an unknown schema. + llvm::consumeError(std::move(E)); + Diag << "module file is not available in the CAS"; + } else Diag << Err.str(); } return true; diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index 9cc0fda021458..d5ab84c6a57ea 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -667,10 +667,11 @@ static void checkCompileCacheKeyMatch(cas::ObjectStore &CAS, llvm::report_fatal_error(OldKey.takeError()); SmallString<256> Err; llvm::raw_svector_ostream OS(Err); - OS << "Compile cache key for module changed; previously:"; + OS << "Compile cache key for module changed; previously: " + << OldKey->toString() << ": "; if (auto E = printCompileJobCacheKey(CAS, *OldKey, OS)) OS << std::move(E); - OS << "\nkey is now:"; + OS << "\nkey is now: " << NewKey.toString() << ": "; if (auto E = printCompileJobCacheKey(CAS, NewKey, OS)) OS << std::move(E); llvm::report_fatal_error(OS.str()); diff --git a/clang/test/CAS/fmodule-file-cache-key-errors.c b/clang/test/CAS/fmodule-file-cache-key-errors.c index bda2fbe4c657b..19b33ef3c864b 100644 --- a/clang/test/CAS/fmodule-file-cache-key-errors.c +++ b/clang/test/CAS/fmodule-file-cache-key-errors.c @@ -59,7 +59,7 @@ // RUN: -fcache-compile-job -Rcompile-job-cache &> %t/bad_key2.txt // RUN: cat %t/bad_key2.txt | FileCheck %s -check-prefix=BAD_KEY2 -// BAD_KEY2: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: cas object is not a valid cache key +// BAD_KEY2: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: module file is not available in the CAS // == Build A @@ -87,7 +87,7 @@ // RUN: -fcache-compile-job -Rcompile-job-cache &> %t/not_in_cache.txt // RUN: cat %t/not_in_cache.txt | FileCheck %s -check-prefix=NOT_IN_CACHE -DPREFIX=%/t -// NOT_IN_CACHE: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: no such entry in action cache; expected compile: +// NOT_IN_CACHE: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: module file is not available in the CAS; expected to be produced by: // NOT_IN_CACHE: command-line: // NOT_IN_CACHE: -cc1 // NOT_IN_CACHE: filesystem: diff --git a/clang/test/ClangScanDeps/modules-cas-trees.c b/clang/test/ClangScanDeps/modules-cas-trees.c index f1cab97f9ed89..e2dbb3689793a 100644 --- a/clang/test/ClangScanDeps/modules-cas-trees.c +++ b/clang/test/ClangScanDeps/modules-cas-trees.c @@ -42,7 +42,7 @@ // Missing pcm in action cache // RUN: not %clang @%t/Left.rsp 2> %t/error.txt // RUN: cat %t/error.txt | FileCheck %s -check-prefix=MISSING -// MISSING: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: no such entry in action cache +// MISSING: error: CAS cannot load module with key '{{.*}}' from -fmodule-file-cache-key: module file is not available in the CAS // Build everything // RUN: %clang @%t/Top.rsp 2>&1 | FileCheck %s -check-prefix=CACHE-MISS