Skip to content

Commit 550c85b

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 9b2c88e commit 550c85b

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
@@ -274,7 +274,7 @@ class FileManager : public RefCountedBase<FileManager> {
274274
/// for its contents if supported by the file system, and then closes the
275275
/// file. If both the buffer and its `cas::ObjectRef` are needed use \p
276276
/// getBufferForFile to avoid the extra file lookup.
277-
llvm::ErrorOr<std::optional<cas::ObjectRef>>
277+
llvm::ErrorOr<cas::ObjectRef>
278278
getObjectRefForFileContent(const Twine &Filename);
279279

280280
private:

clang/lib/Basic/FileManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,17 +580,17 @@ FileManager::getBufferForFileImpl(StringRef Filename, int64_t FileSize,
580580
return getBufferImpl(FilePath);
581581
}
582582

583-
llvm::ErrorOr<std::optional<cas::ObjectRef>>
583+
llvm::ErrorOr<cas::ObjectRef>
584584
FileManager::getObjectRefForFileContent(const Twine &Filename) {
585585
auto getObjectRefImpl =
586-
[&](const Twine &Name) -> llvm::ErrorOr<std::optional<cas::ObjectRef>> {
586+
[&](const Twine &Name) -> llvm::ErrorOr<cas::ObjectRef> {
587587
auto F = FS->openFileForRead(Name);
588588
if (!F)
589589
return F.getError();
590590

591591
auto *CASFile = dyn_cast<llvm::cas::CASBackedFile>(F->get());
592592
if (!CASFile)
593-
return std::nullopt;
593+
return std::make_error_code(std::errc::not_supported);
594594

595595
return CASFile->getObjectRefForContent();
596596
};

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

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

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

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

901-
llvm::ErrorOr<std::optional<cas::ObjectRef>> CASContents =
901+
llvm::ErrorOr<cas::ObjectRef> CASContents =
902902
FM.getObjectRefForFileContent(Filename);
903903
if (!CASContents)
904904
return llvm::errorCodeToError(CASContents.getError());
905-
assert(*CASContents);
906905

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

0 commit comments

Comments
 (0)