Skip to content

Commit cecc7ea

Browse files
[CAS] FileManager::getObjectRefForFileContent should error when no CAS
Update the API to return error when the CASBackedFileSystem is not correctly configured instead of return `std::nullopt`. There isn't any error handling can be done when getting `nullopt` in all usecases and it is nicer to surface the error directly.
1 parent 8b852e3 commit cecc7ea

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

clang/include/clang/Basic/FileManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class FileManager : public RefCountedBase<FileManager> {
279279
/// for its contents if supported by the file system, and then closes the
280280
/// file. If both the buffer and its `cas::ObjectRef` are needed use \p
281281
/// getBufferForFile to avoid the extra file lookup.
282-
llvm::ErrorOr<std::optional<cas::ObjectRef>>
282+
llvm::ErrorOr<cas::ObjectRef>
283283
getObjectRefForFileContent(const Twine &Filename);
284284

285285
private:

clang/lib/Basic/FileManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,17 +585,17 @@ FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
585585
return getBufferImpl(FilePath);
586586
}
587587

588-
llvm::ErrorOr<std::optional<cas::ObjectRef>>
588+
llvm::ErrorOr<cas::ObjectRef>
589589
FileManager::getObjectRefForFileContent(const Twine &Filename) {
590590
auto getObjectRefImpl =
591-
[&](const Twine &Name) -> llvm::ErrorOr<std::optional<cas::ObjectRef>> {
591+
[&](const Twine &Name) -> llvm::ErrorOr<cas::ObjectRef> {
592592
auto F = FS->openFileForRead(Name);
593593
if (!F)
594594
return F.getError();
595595

596596
auto *CASFile = dyn_cast<llvm::cas::CASBackedFile>(F->get());
597597
if (!CASFile)
598-
return std::nullopt;
598+
return std::make_error_code(std::errc::not_supported);
599599

600600
return CASFile->getObjectRefForContent();
601601
};

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ IncludeTreeBuilder::finishIncludeTree(CompilerInstance &ScanInstance,
700700
if (PPOpts.ImplicitPCHInclude.empty())
701701
return Error::success(); // no need for additional work.
702702

703-
llvm::ErrorOr<std::optional<cas::ObjectRef>> CASContents =
703+
llvm::ErrorOr<cas::ObjectRef> CASContents =
704704
FM.getObjectRefForFileContent(PPOpts.ImplicitPCHInclude);
705705
if (!CASContents)
706706
return llvm::errorCodeToError(CASContents.getError());
@@ -710,7 +710,7 @@ IncludeTreeBuilder::finishIncludeTree(CompilerInstance &ScanInstance,
710710
PCHFilename = PPOpts.ImplicitPCHInclude;
711711

712712
auto PCHFile =
713-
cas::IncludeTree::File::create(DB, PCHFilename, **CASContents);
713+
cas::IncludeTree::File::create(DB, PCHFilename, *CASContents);
714714
if (!PCHFile)
715715
return PCHFile.takeError();
716716
PCHRef = PCHFile->getRef();
@@ -897,15 +897,14 @@ Expected<cas::ObjectRef> IncludeTreeBuilder::addToFileList(FileManager &FM,
897897
Filename = PathStorage;
898898
}
899899

900-
llvm::ErrorOr<std::optional<cas::ObjectRef>> CASContents =
900+
llvm::ErrorOr<cas::ObjectRef> CASContents =
901901
FM.getObjectRefForFileContent(Filename);
902902
if (!CASContents)
903903
return llvm::errorCodeToError(CASContents.getError());
904-
assert(*CASContents);
905904

906905
auto addFile = [&](StringRef Filename) -> Expected<cas::ObjectRef> {
907906
assert(!Filename.empty());
908-
auto FileNode = createIncludeFile(Filename, **CASContents);
907+
auto FileNode = createIncludeFile(Filename, *CASContents);
909908
if (!FileNode)
910909
return FileNode.takeError();
911910
IncludedFiles.push_back(

0 commit comments

Comments
 (0)