Skip to content

Commit bb91b1c

Browse files
[clang][IncludeTree] Do not preserve working directory from scanning
clang incldue-tree has been consistently using absolute path to encode all the inputs so there is no need to preserve `-working-directory` option from the scanner. This does affect output paths so make sure the output paths are updated as well to absolute path.
1 parent 986a497 commit bb91b1c

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

clang/lib/Tooling/DependencyScanning/ScanAndUpdateArgs.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@
1111
#include "clang/Frontend/CompilerInvocation.h"
1212
#include "clang/Lex/HeaderSearchOptions.h"
1313
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
14-
#include "llvm/CAS/CachingOnDiskFileSystem.h"
1514
#include "llvm/CAS/ObjectStore.h"
15+
#include "llvm/Support/Path.h"
1616
#include "llvm/Support/PrefixMapper.h"
1717

1818
using namespace clang;
1919
using namespace clang::tooling::dependencies;
2020
using llvm::Error;
2121

22+
static void updateRelativePath(std::string &Path,
23+
const std::string &WorkingDir) {
24+
if (Path.empty() || llvm::sys::path::is_absolute(Path) || WorkingDir.empty())
25+
return;
26+
27+
SmallString<128> PathStorage(WorkingDir);
28+
llvm::sys::path::append(PathStorage, Path);
29+
Path = PathStorage.str();
30+
}
31+
2232
void tooling::dependencies::configureInvocationForCaching(
2333
CompilerInvocation &CI, CASOptions CASOpts, std::string InputID,
2434
CachingInputKind InputKind, std::string WorkingDir) {
@@ -92,6 +102,14 @@ void tooling::dependencies::configureInvocationForCaching(
92102
}
93103
// Clear APINotes options.
94104
CI.getAPINotesOpts().ModuleSearchPaths = {};
105+
106+
// Update output paths, and clear working directory.
107+
auto CWD = FileSystemOpts.WorkingDir;
108+
updateRelativePath(FrontendOpts.OutputFile, CWD);
109+
updateRelativePath(CI.getDiagnosticOpts().DiagnosticSerializationFile, CWD);
110+
updateRelativePath(CI.getDiagnosticOpts().DiagnosticLogFile, CWD);
111+
updateRelativePath(CI.getDependencyOutputOpts().OutputFile, CWD);
112+
FileSystemOpts.WorkingDir.clear();
95113
break;
96114
}
97115
case CachingInputKind::FileSystemRoot: {

clang/test/ClangScanDeps/include-tree-working-directory.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
1212
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
1313
// RUN: ls %t/t.o
14+
// RUN: ls %t/t.d
15+
// RUN: ls %t/t.dia
1416

1517
// CACHE-MISS: remark: compile job cache miss
1618
// CACHE-HIT: remark: compile job cache hit
@@ -25,10 +27,26 @@
2527
// CHECK: [[PREFIX]]/t.c llvmcas://
2628
// CHECK: [[PREFIX]]/relative/h1.h llvmcas://
2729

30+
/// Using a different working directory should cache hit as well.
31+
/// FIXME: Working directory affects some codegen options added by clang driver, preserve them to make sure the cache hit.
32+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb2.json.template > %t/cdb2.json
33+
// RUN: clang-scan-deps -compilation-database %t/cdb2.json -format experimental-include-tree-full -cas-path %t/cas \
34+
// RUN: > %t/deps2.json
35+
// RUN: %deps-to-rsp %t/deps2.json --tu-index 0 > %t/tu2.rsp
36+
// RUN: %clang @%t/tu2.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
37+
38+
2839
//--- cdb.json.template
2940
[{
3041
"directory": "DIR/other",
31-
"command": "clang -c t.c -I relative -working-directory DIR -o t.o",
42+
"command": "clang -c t.c -I relative -working-directory DIR -o t.o -MD -serialize-diagnostics t.dia",
43+
"file": "DIR/t.c"
44+
}]
45+
46+
//--- cdb2.json.template
47+
[{
48+
"directory": "DIR/other",
49+
"command": "clang -c DIR/t.c -I DIR/relative -working-directory DIR/other -o DIR/t2.o -MD -serialize-diagnostics DIR/t2.dia -fdebug-compilation-dir=DIR -fcoverage-compilation-dir=DIR",
3250
"file": "DIR/t.c"
3351
}]
3452

0 commit comments

Comments
 (0)