Skip to content

Commit a838e29

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.
1 parent d0790cd commit a838e29

File tree

5 files changed

+73
-32
lines changed

5 files changed

+73
-32
lines changed

clang/lib/Frontend/CompileJobCache.cpp

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

538+
// FIXME: we should create an include-tree filesystem based on the cache key
539+
// to guarantee that the filesystem used during diagnostic replay will match
540+
// the cached diagnostics. Currently we rely on the invocation having a
541+
// matching -fcas-include-tree option.
542+
538543
auto FinishDiagnosticClient =
539544
llvm::make_scope_exit([&]() { Clang.getDiagnosticClient().finish(); });
540545

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: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static bool Verbose;
9696
static bool PrintTiming;
9797
static std::vector<const char *> CommandLine;
9898
static bool EmitCASCompDB;
99-
static std::string OnDiskCASPath;
99+
static CASOptions CASOpts;
100100
static bool InMemoryCAS;
101101
static std::string PrefixMapToolchain;
102102
static std::string PrefixMapSDK;
@@ -220,7 +220,14 @@ static void ParseArgs(int argc, char **argv) {
220220
InMemoryCAS = Args.hasArg(OPT_in_memory_cas);
221221

222222
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_cas_path_EQ))
223-
OnDiskCASPath = A->getValue();
223+
CASOpts.CASPath = A->getValue();
224+
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_fcas_plugin_path_EQ))
225+
CASOpts.PluginPath = A->getValue();
226+
for (const llvm::opt::Arg *A : Args.filtered(OPT_fcas_plugin_option_EQ)) {
227+
auto [L, R] = StringRef(A->getValue()).split('=');
228+
CASOpts.PluginOptions.emplace_back(std::string(L), std::string(R));
229+
}
230+
224231
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_prefix_map_toolchain_EQ))
225232
PrefixMapToolchain = A->getValue();
226233
if (const llvm::opt::Arg *A = Args.getLastArg(OPT_prefix_map_sdk_EQ))
@@ -556,7 +563,7 @@ static bool outputFormatRequiresCAS() {
556563
}
557564

558565
static bool useCAS() {
559-
return InMemoryCAS || !OnDiskCASPath.empty() || outputFormatRequiresCAS();
566+
return InMemoryCAS || !CASOpts.CASPath.empty() || outputFormatRequiresCAS();
560567
}
561568

562569
static llvm::json::Array toJSONSorted(const llvm::StringSet<> &Set) {
@@ -1132,17 +1139,12 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
11321139
DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions());
11331140
Diags.setClient(DiagsConsumer.get(), /*ShouldOwnClient=*/false);
11341141

1135-
CASOptions CASOpts;
11361142
std::shared_ptr<llvm::cas::ObjectStore> CAS;
11371143
std::shared_ptr<llvm::cas::ActionCache> Cache;
11381144
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> FS;
11391145
if (useCAS()) {
1140-
if (!InMemoryCAS) {
1141-
if (!OnDiskCASPath.empty())
1142-
CASOpts.CASPath = OnDiskCASPath;
1143-
else
1144-
CASOpts.ensurePersistentCAS();
1145-
}
1146+
if (!InMemoryCAS)
1147+
CASOpts.ensurePersistentCAS();
11461148

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

clang/tools/clang-scan-deps/Opts.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def emit_cas_compdb : F<"emit-cas-compdb", "Emit compilation DB with updated cla
3535

3636
defm cas_path : Eq<"cas-path", "Path for on-disk CAS.">;
3737

38+
defm fcas_plugin_path : Eq<"fcas-plugin-path", "Path to a shared library implementing the LLVM CAS plugin API">;
39+
defm fcas_plugin_option : Eq<"fcas-plugin-option", "Option to pass to the CAS plugin">;
40+
3841
def in_memory_cas : F<"in-memory-cas", "Use an in-memory CAS instead of on-disk.">;
3942

4043
defm prefix_map_toolchain : Eq<"prefix-map-toolchain", "Path to remap toolchain path to.">;

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"
@@ -415,10 +416,22 @@ CXCASReplayResult clang_experimental_cas_replayCompilation(
415416
auto *DiagsPrinter = new TextDiagnosticPrinter(DiagOS, DiagOpts.get());
416417
DiagnosticsEngine Diags(DiagID, DiagOpts.get(), DiagsPrinter);
417418

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

0 commit comments

Comments
 (0)