Skip to content

Commit 564c472

Browse files
committed
[clang][deps][cas] Fix include-tree contents with -working-directory
When using -working-directory we were inconsistent about using relative or absolute paths, which was causing failures to find files in the include-tree ft logilesystem. Now we should be consistently using the absolute path. Note: this changes behaviour of things like __FILE__, which would normally have a relative path even with -working-directory. In the future we may want to find a way to preserve the relative paths, but that has been much harder and the current change at fixes the compilation failures. rdar://116135029 (cherry picked from commit 78e64df)
1 parent 4132bf5 commit 564c472

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,18 @@ IncludeTreeBuilder::getObjectForBuffer(const SrcMgr::FileInfo &FI) {
816816

817817
Expected<cas::ObjectRef>
818818
IncludeTreeBuilder::addToFileList(FileManager &FM, const FileEntry *FE) {
819+
SmallString<128> PathStorage;
819820
StringRef Filename = FE->getName();
821+
// Apply -working-directory to relative paths. This option causes filesystem
822+
// lookups to use absolute paths, so make paths in the include-tree filesystem
823+
// absolute to match.
824+
if (!llvm::sys::path::is_absolute(Filename) &&
825+
!FM.getFileSystemOpts().WorkingDir.empty()) {
826+
PathStorage = Filename;
827+
FM.FixupRelativePath(PathStorage);
828+
Filename = PathStorage;
829+
}
830+
820831
llvm::ErrorOr<std::optional<cas::ObjectRef>> CASContents =
821832
FM.getObjectRefForFileContent(Filename);
822833
if (!CASContents)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t/other
3+
// RUN: split-file %s %t
4+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
5+
6+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree-full -cas-path %t/cas \
7+
// RUN: > %t/deps.json
8+
9+
// Build the include-tree command
10+
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
11+
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
12+
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
13+
// RUN: ls %t/t.o
14+
15+
// CACHE-MISS: remark: compile job cache miss
16+
// CACHE-HIT: remark: compile job cache hit
17+
18+
// RUN: cat %t/tu.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/tu.casid
19+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/tu.casid | FileCheck %s -DPREFIX=%/t
20+
21+
// CHECK: [[PREFIX]]/t.c llvmcas://
22+
// CHECK: 1:1 <built-in> llvmcas://
23+
// CHECK: 2:1 [[PREFIX]]/relative/h1.h llvmcas://
24+
// CHECK: Files:
25+
// CHECK: [[PREFIX]]/t.c llvmcas://
26+
// CHECK: [[PREFIX]]/relative/h1.h llvmcas://
27+
28+
//--- cdb.json.template
29+
[{
30+
"directory": "DIR/other",
31+
"command": "clang -c t.c -I relative -working-directory DIR -o t.o",
32+
"file": "DIR/t.c"
33+
}]
34+
35+
//--- relative/h1.h
36+
37+
//--- t.c
38+
#include "h1.h"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t/other
3+
// RUN: split-file %s %t
4+
// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
5+
6+
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree-full -cas-path %t/cas \
7+
// RUN: > %t/deps.json
8+
9+
// Build the include-tree command
10+
// RUN: %deps-to-rsp %t/deps.json --module H > %t/H.rsp
11+
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
12+
// RUN: %clang @%t/H.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
13+
// RUN: %clang @%t/H.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
14+
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-MISS
15+
// RUN: %clang @%t/tu.rsp -Rcompile-job-cache 2>&1 | FileCheck %s -check-prefix=CACHE-HIT
16+
17+
// CACHE-MISS: remark: compile job cache miss
18+
// CACHE-HIT: remark: compile job cache hit
19+
20+
// RUN: cat %t/H.rsp | sed -E 's|.*"-fcas-include-tree" "(llvmcas://[[:xdigit:]]+)".*|\1|' > %t/H.casid
21+
// RUN: clang-cas-test -cas %t/cas -print-include-tree @%t/H.casid | FileCheck %s -DPREFIX=%/t
22+
23+
// CHEK:C <module-includes>
24+
// CHECK: 2:1 [[PREFIX]]/relative/h1.h llvmcas://
25+
// CHECK: Files:
26+
// CHECK: [[PREFIX]]/relative/h1.h llvmcas://
27+
28+
//--- cdb.json.template
29+
[{
30+
"directory": "DIR/other",
31+
"command": "clang -fsyntax-only t.c -I relative -working-directory DIR -fmodules -fimplicit-modules -fimplicit-module-maps",
32+
"file": "DIR/t.c"
33+
}]
34+
35+
//--- relative/h1.h
36+
37+
//--- relative/module.modulemap
38+
module H { header "h1.h" }
39+
40+
//--- t.c
41+
#include "h1.h"

0 commit comments

Comments
 (0)