Skip to content

[InstallAPI] Cleanup I/O error handling for input lists #90664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticInstallAPIKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def err_no_matching_target : Error<"no matching target found for target variant
def err_unsupported_vendor : Error<"vendor '%0' is not supported: '%1'">;
def err_unsupported_environment : Error<"environment '%0' is not supported: '%1'">;
def err_unsupported_os : Error<"os '%0' is not supported: '%1'">;
def err_cannot_read_alias_list : Error<"could not read alias list '%0': %1">;
def err_cannot_read_input_list : Error<"could not read %select{alias list|filelist}0 '%1': %2">;
} // end of command line category.

let CategoryName = "Verification" in {
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/InstallAPI/FileList.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class FileListReader {
///
/// \param InputBuffer JSON input data.
/// \param Destination Container to load headers into.
/// \param FM Optional File Manager to validate input files exist.
static llvm::Error
loadHeaders(std::unique_ptr<llvm::MemoryBuffer> InputBuffer,
HeaderSeq &Destination);
HeaderSeq &Destination, clang::FileManager *FM = nullptr);

FileListReader() = delete;
};
Expand Down
10 changes: 9 additions & 1 deletion clang/lib/InstallAPI/FileList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Implementation {

public:
std::unique_ptr<MemoryBuffer> InputBuffer;
clang::FileManager *FM;
unsigned Version;
HeaderSeq HeaderList;

Expand Down Expand Up @@ -124,6 +125,12 @@ Error Implementation::parseHeaders(Array &Headers) {
HeaderFile{PathStr, *Type, /*IncludeName=*/"", Language});
continue;
}

if (FM)
if (!FM->getOptionalFileRef(PathStr))
return createFileError(
PathStr, make_error_code(std::errc::no_such_file_or_directory));

auto IncludeName = createIncludeHeaderName(PathStr);
HeaderList.emplace_back(PathStr, *Type,
IncludeName.has_value() ? IncludeName.value() : "",
Expand Down Expand Up @@ -170,9 +177,10 @@ Error Implementation::parse(StringRef Input) {

llvm::Error
FileListReader::loadHeaders(std::unique_ptr<MemoryBuffer> InputBuffer,
HeaderSeq &Destination) {
HeaderSeq &Destination, clang::FileManager *FM) {
Implementation Impl;
Impl.InputBuffer = std::move(InputBuffer);
Impl.FM = FM;

if (llvm::Error Err = Impl.parse(Impl.InputBuffer->getBuffer()))
return Err;
Expand Down
9 changes: 5 additions & 4 deletions clang/tools/clang-installapi/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ InstallAPIContext Options::createContext() {
}
Expected<AliasMap> Result = parseAliasList(Buffer.get());
if (!Result) {
Diags->Report(diag::err_cannot_read_alias_list)
<< ListPath << toString(Result.takeError());
Diags->Report(diag::err_cannot_read_input_list)
<< /*IsFileList=*/false << ListPath << toString(Result.takeError());
return Ctx;
}
Aliases.insert(Result.get().begin(), Result.get().end());
Expand All @@ -717,8 +717,9 @@ InstallAPIContext Options::createContext() {
return Ctx;
}
if (auto Err = FileListReader::loadHeaders(std::move(Buffer.get()),
Ctx.InputHeaders)) {
Diags->Report(diag::err_cannot_open_file) << ListPath << std::move(Err);
Ctx.InputHeaders, FM)) {
Diags->Report(diag::err_cannot_read_input_list)
<< /*IsFileList=*/true << ListPath << std::move(Err);
return Ctx;
}
}
Expand Down