Skip to content

Commit 3097792

Browse files
committed
[clang] Port libclang-replay-job test to clang-scan-deps
This test was unintentionally using the native filesystem instead of the cas include-tree filesystem. Move to using clang-scan-deps to calculate the cc1 instead of trying to hand write it. (cherry picked from commit a838e29)
1 parent 4528ba3 commit 3097792

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

clang/lib/Frontend/CompileJobCache.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,11 @@ Expected<std::optional<int>> CompileJobCache::replayCachedResult(
500500
new TextDiagnosticPrinter(DiagOS, &Clang.getDiagnosticOpts()));
501501
Clang.setVerboseOutputStream(DiagOS);
502502

503+
// FIXME: we should create an include-tree filesystem based on the cache key
504+
// to guarantee that the filesystem used during diagnostic replay will match
505+
// the cached diagnostics. Currently we rely on the invocation having a
506+
// matching -fcas-include-tree option.
507+
503508
auto FinishDiagnosticClient =
504509
llvm::make_scope_exit([&]() { Clang.getDiagnosticClient().finish(); });
505510

clang/test/CAS/libclang-replay-job.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
// REQUIRES: shell
22

33
// RUN: rm -rf %t && mkdir -p %t
4-
// RUN: llvm-cas --cas %t/cas --ingest --data %s > %t/casid
5-
//
6-
// RUN: %clang -cc1 -triple x86_64-apple-macos11 \
7-
// RUN: -fcas-path %t/cas -fcas-fs @%t/casid -fcache-compile-job \
8-
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
9-
// RUN: -fcas-plugin-option upstream-path=%t/cas-upstream -fcas-plugin-option no-logging \
10-
// RUN: -dependency-file %t/t1.d -MT deps -emit-obj -o %t/output1.o %s
11-
// RUN: %clang -cc1 -triple x86_64-apple-macos11 \
12-
// RUN: -fcas-path %t/cas -fcas-fs @%t/casid -fcache-compile-job \
13-
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
4+
// RUN: split-file %s %t
5+
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
6+
7+
// RUN: clang-scan-deps -compilation-database %t/cdb.json \
8+
// RUN: -format experimental-include-tree-full \
9+
// RUN: -cas-path %t/cas -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
1410
// RUN: -fcas-plugin-option upstream-path=%t/cas-upstream -fcas-plugin-option no-logging \
15-
// RUN: -serialize-diagnostic-file %t/t1.dia -dependency-file %t/t1.d -MT deps \
16-
// RUN: -Rcompile-job-cache-hit -emit-obj -o %t/output1.o %s 2> %t/output1.txt
11+
// RUN: > %t/deps.json
12+
13+
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/cc1.rsp
14+
15+
// RUN: (cd %t; %clang @%t/cc1.rsp)
16+
// RUN: (cd %t; %clang @%t/cc1.rsp -Rcompile-job-cache-hit \
17+
// RUN: -serialize-diagnostic-file %t/t1.dia 2> %t/output1.txt)
1718

1819
// Verify the warning was recorded and we compare populated .dia files.
1920
// RUN: c-index-test -read-diagnostics %t/t1.dia 2>&1 | FileCheck %s --check-prefix=DIAGS
@@ -25,37 +26,54 @@
2526

2627
// Delete the "local" cache and use the "upstream" one to re-materialize the outputs locally.
2728
// RUN: rm -rf %t/cas
29+
30+
// Re-run the scan to populate the include-tree in the cas
31+
// RUN: clang-scan-deps -compilation-database %t/cdb.json \
32+
// RUN: -format experimental-include-tree-full \
33+
// RUN: -cas-path %t/cas -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
34+
// RUN: -fcas-plugin-option upstream-path=%t/cas-upstream -fcas-plugin-option no-logging \
35+
// RUN: > %t/deps2.json
36+
// RUN: diff -u %t/deps.json %t/deps2.json
37+
38+
2839
// RUN: c-index-test core -materialize-cached-job -cas-path %t/cas @%t/cache-key \
2940
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
3041
// RUN: -fcas-plugin-option upstream-path=%t/cas-upstream -fcas-plugin-option no-logging
3142

3243
// RUN: c-index-test core -replay-cached-job -cas-path %t/cas @%t/cache-key \
3344
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
3445
// RUN: -fcas-plugin-option no-logging \
35-
// RUN: -- -cc1 \
46+
// RUN: -working-dir %t \
47+
// RUN: -- @%t/cc1.rsp \
3648
// RUN: -serialize-diagnostic-file %t/t2.dia -Rcompile-job-cache-hit \
37-
// RUN: -dependency-file %t/t2.d -MT deps \
38-
// RUN: -o %t/output2.o 2> %t/output2.txt
49+
// RUN: -dependency-file %t/t2.d -o %t/output2.o 2> %t/output2.txt
3950

4051
// RUN: diff %t/output1.o %t/output2.o
4152
// RUN: diff -u %t/output1.txt %t/output2.txt
4253
// RUN: diff %t/t1.dia %t/t2.dia
4354
// RUN: diff -u %t/t1.d %t/t2.d
4455

45-
// Check with `-working-dir` flag.
56+
// Check with different `-working-dir` flag.
4657
// RUN: mkdir -p %t/a/b
4758
// RUN: cd %t/a
4859
// RUN: c-index-test core -replay-cached-job -cas-path %t/cas @%t/cache-key \
4960
// RUN: -fcas-plugin-path %llvmshlibdir/libCASPluginTest%pluginext \
5061
// RUN: -fcas-plugin-option no-logging \
5162
// RUN: -working-dir %t/a/b \
52-
// RUN: -- -cc1 %t/a/b \
63+
// RUN: -- @%t/cc1.rsp \
5364
// RUN: -serialize-diagnostic-file rel.dia -Rcompile-job-cache-hit \
54-
// RUN: -dependency-file rel.d -MT deps \
55-
// RUN: -o reloutput.o
65+
// RUN: -dependency-file rel.d -o reloutput.o
5666

5767
// RUN: diff %t/output1.o %t/a/b/reloutput.o
5868
// RUN: diff -u %t/t1.d %t/a/b/rel.d
5969
// FIXME: Get clang's `-working-directory` to affect relative path for serialized diagnostics.
6070

71+
//--- cdb.json.template
72+
[{
73+
"directory": "DIR",
74+
"command": "clang -c DIR/main.c -target x86_64-apple-macos11 -MD -MF t1.d -MT deps -o output1.o",
75+
"file": "DIR/main.c"
76+
}]
77+
78+
//--- main.c
6179
#warning some warning

clang/tools/clang-scan-deps/ClangScanDeps.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ llvm::cl::opt<bool> EmitCASCompDB(
225225
llvm::cl::opt<std::string>
226226
OnDiskCASPath("cas-path", llvm::cl::desc("Path for on-disk CAS."),
227227
llvm::cl::cat(DependencyScannerCategory));
228+
llvm::cl::opt<std::string>
229+
CASPluginPath("fcas-plugin-path", llvm::cl::desc("Path to a shared library implementing the LLVM CAS plugin API"),
230+
llvm::cl::cat(DependencyScannerCategory));
231+
llvm::cl::list<std::string>
232+
CASPluginOptions("fcas-plugin-option", llvm::cl::desc("Option to pass to the CAS plugin"),
233+
llvm::cl::cat(DependencyScannerCategory));
228234

229235
llvm::cl::opt<bool> InMemoryCAS(
230236
"in-memory-cas", llvm::cl::desc("Use an in-memory CAS instead of on-disk."),
@@ -898,16 +904,19 @@ int main(int argc, const char **argv) {
898904
Diags.setClient(DiagsConsumer.get(), /*ShouldOwnClient=*/false);
899905

900906
CASOptions CASOpts;
907+
CASOpts.CASPath = OnDiskCASPath;
908+
CASOpts.PluginPath = CASPluginPath;
909+
for (StringRef Arg : CASPluginOptions) {
910+
auto [L, R] = Arg.split('=');
911+
CASOpts.PluginOptions.emplace_back(std::string(L), std::string(R));
912+
}
913+
901914
std::shared_ptr<llvm::cas::ObjectStore> CAS;
902915
std::shared_ptr<llvm::cas::ActionCache> Cache;
903916
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> FS;
904917
if (useCAS()) {
905-
if (!InMemoryCAS) {
906-
if (!OnDiskCASPath.empty())
907-
CASOpts.CASPath = OnDiskCASPath;
908-
else
909-
CASOpts.ensurePersistentCAS();
910-
}
918+
if (!InMemoryCAS)
919+
CASOpts.ensurePersistentCAS();
911920

912921
std::tie(CAS, Cache) = CASOpts.getOrCreateDatabases(Diags);
913922
if (!CAS)

clang/tools/libclang/CCAS.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "clang/Basic/LLVM.h"
1616
#include "clang/CAS/CASOptions.h"
17+
#include "clang/Driver/Driver.h"
1718
#include "clang/Frontend/CompileJobCache.h"
1819
#include "clang/Frontend/CompilerInvocation.h"
1920
#include "clang/Frontend/TextDiagnosticPrinter.h"
@@ -419,10 +420,22 @@ CXCASReplayResult clang_experimental_cas_replayCompilation(
419420
auto *DiagsPrinter = new TextDiagnosticPrinter(DiagOS, DiagOpts.get());
420421
DiagnosticsEngine Diags(DiagID, DiagOpts.get(), DiagsPrinter);
421422

423+
SmallVector<const char *, 256> Args(argv, argv + argc);
424+
llvm::BumpPtrAllocator Alloc;
425+
bool CLMode = driver::IsClangCL(
426+
driver::getDriverMode(argv[0], ArrayRef(Args).slice(1)));
427+
428+
if (llvm::Error E = driver::expandResponseFiles(Args, CLMode, Alloc)) {
429+
Diags.Report(diag::err_drv_expand_response_file)
430+
<< llvm::toString(std::move(E));
431+
if (OutError)
432+
*OutError = cxerror::create(DiagOS.str());
433+
return nullptr;
434+
}
435+
422436
auto Invok = std::make_shared<CompilerInvocation>();
423-
ArrayRef Argv(argv, argc);
424-
bool Success = CompilerInvocation::CreateFromArgs(*Invok, Argv.drop_front(),
425-
Diags, Argv.front());
437+
bool Success = CompilerInvocation::CreateFromArgs(*Invok, ArrayRef(Args).drop_front(),
438+
Diags, Args.front());
426439
if (!Success) {
427440
if (OutError)
428441
*OutError = cxerror::create(DiagOS.str());

0 commit comments

Comments
 (0)