Skip to content

Commit 8b852e3

Browse files
[CASFS] Fix FileWithFixedStatus after CASFS refactoring #11235
Add CASFileWithFixedStatus to keep CAS references when fetching VFS overlayed file from CASBackedFileSystem. Fixes: #11616
1 parent c32a36c commit 8b852e3

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// REQUIRES: ondisk_cas
2+
3+
// This is a reduced test for https://github.com/swiftlang/llvm-project/issues/11616
4+
5+
// RUN: rm -rf %t
6+
// RUN: split-file %s %t
7+
// RUN: sed -e "s|DIR|%/t|g" %t/vfs-overlay.yaml.template > %t/vfs-overlay.yaml
8+
9+
// RUN: %clang -I%t -fdepscan=inline -fdepscan-include-tree -Xclang -fcas-path -Xclang %t/cas -Xclang -ivfsoverlay -Xclang %t/vfs-overlay.yaml -c %t/test.c
10+
11+
//--- test.c
12+
#include "header.h"
13+
#include "header1.h"
14+
15+
//--- header.h
16+
17+
//--- header2.h
18+
19+
//--- header3.h
20+
21+
//--- vfs-overlay.yaml.template
22+
version: 0
23+
case-sensitive: false
24+
roots:
25+
- name: "DIR"
26+
type: directory
27+
contents:
28+
- name: header.h
29+
type: file
30+
external-contents: "DIR/header.h"
31+
- name: header1.h
32+
type: file
33+
external-contents: "DIR/header2.h"
34+
- name: header2.h
35+
type: file
36+
external-contents: "DIR/header3.h"

llvm/lib/CAS/CASFileSystem.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using namespace llvm;
1515
using namespace llvm::cas;
1616

17-
const char CASBackedFile::ID = 0;
1817
const char CASBackedFileSystem::ID = 0;
1918

2019
llvm::Expected<std::pair<std::unique_ptr<llvm::MemoryBuffer>, cas::ObjectRef>>

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/Twine.h"
2323
#include "llvm/ADT/iterator_range.h"
2424
#include "llvm/CAS/CASReference.h"
25+
#include "llvm/CAS/CASFileSystem.h"
2526
#include "llvm/Config/llvm-config.h"
2627
#include "llvm/Support/Casting.h"
2728
#include "llvm/Support/Chrono.h"
@@ -2534,6 +2535,33 @@ class FileWithFixedStatus : public File {
25342535
void setPath(const Twine &Path) override { S = S.copyWithNewName(S, Path); }
25352536
};
25362537

2538+
2539+
class CASFileWithFixedStatus : public cas::CASBackedFile {
2540+
std::unique_ptr<cas::CASBackedFile> InnerFile;
2541+
Status S;
2542+
2543+
public:
2544+
CASFileWithFixedStatus(std::unique_ptr<CASBackedFile> InnerFile, Status S)
2545+
: InnerFile(std::move(InnerFile)), S(std::move(S)) {}
2546+
2547+
ErrorOr<Status> status() override { return S; }
2548+
ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
2549+
2550+
getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
2551+
bool IsVolatile) override {
2552+
return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
2553+
IsVolatile);
2554+
}
2555+
2556+
std::error_code close() override { return InnerFile->close(); }
2557+
2558+
void setPath(const Twine &Path) override { S = S.copyWithNewName(S, Path); }
2559+
2560+
cas::ObjectRef getObjectRefForContent() override {
2561+
return InnerFile->getObjectRefForContent();
2562+
}
2563+
};
2564+
25372565
} // namespace
25382566

25392567
ErrorOr<std::unique_ptr<File>>
@@ -2607,6 +2635,12 @@ RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) {
26072635
// replace the underlying path if the external name is being used.
26082636
Status S = getRedirectedFileStatus(
26092637
OriginalPath, RE->useExternalName(UseExternalNames), *ExternalStatus);
2638+
2639+
if (std::unique_ptr<cas::CASBackedFile> CASFile =
2640+
dyn_cast<cas::CASBackedFile>(*ExternalFile)) {
2641+
return std::unique_ptr<File>(
2642+
std::make_unique<CASFileWithFixedStatus>(std::move(CASFile), S));
2643+
}
26102644
return std::unique_ptr<File>(
26112645
std::make_unique<FileWithFixedStatus>(std::move(*ExternalFile), S));
26122646
}
@@ -2991,3 +3025,7 @@ const char ProxyFileSystem::ID = 0;
29913025
const char InMemoryFileSystem::ID = 0;
29923026
const char RedirectingFileSystem::ID = 0;
29933027
const char TracingFileSystem::ID = 0;
3028+
3029+
// FIXME: Temporarily put the CASBackedFile::ID in Support library to workaround
3030+
// the layer issue that `dyn_cast` check for the type needs to happen here.
3031+
const char cas::CASBackedFile::ID = 0;

0 commit comments

Comments
 (0)