Skip to content

Commit 278774e

Browse files
authoredApr 30, 2024··
[InstallAPI] Cleanup I/O error handling for input lists (#90664)
Add validation in the FileList reader to check that the headers exist and use similar diagnostics in Options.cpp
1 parent 2647bd7 commit 278774e

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed
 

‎clang/include/clang/Basic/DiagnosticInstallAPIKinds.td

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def err_no_matching_target : Error<"no matching target found for target variant
2424
def err_unsupported_vendor : Error<"vendor '%0' is not supported: '%1'">;
2525
def err_unsupported_environment : Error<"environment '%0' is not supported: '%1'">;
2626
def err_unsupported_os : Error<"os '%0' is not supported: '%1'">;
27-
def err_cannot_read_alias_list : Error<"could not read alias list '%0': %1">;
27+
def err_cannot_read_input_list : Error<"could not read %select{alias list|filelist}0 '%1': %2">;
2828
} // end of command line category.
2929

3030
let CategoryName = "Verification" in {

‎clang/include/clang/InstallAPI/FileList.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ class FileListReader {
2929
///
3030
/// \param InputBuffer JSON input data.
3131
/// \param Destination Container to load headers into.
32+
/// \param FM Optional File Manager to validate input files exist.
3233
static llvm::Error
3334
loadHeaders(std::unique_ptr<llvm::MemoryBuffer> InputBuffer,
34-
HeaderSeq &Destination);
35+
HeaderSeq &Destination, clang::FileManager *FM = nullptr);
3536

3637
FileListReader() = delete;
3738
};

‎clang/lib/InstallAPI/FileList.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Implementation {
5151

5252
public:
5353
std::unique_ptr<MemoryBuffer> InputBuffer;
54+
clang::FileManager *FM;
5455
unsigned Version;
5556
HeaderSeq HeaderList;
5657

@@ -124,6 +125,12 @@ Error Implementation::parseHeaders(Array &Headers) {
124125
HeaderFile{PathStr, *Type, /*IncludeName=*/"", Language});
125126
continue;
126127
}
128+
129+
if (FM)
130+
if (!FM->getOptionalFileRef(PathStr))
131+
return createFileError(
132+
PathStr, make_error_code(std::errc::no_such_file_or_directory));
133+
127134
auto IncludeName = createIncludeHeaderName(PathStr);
128135
HeaderList.emplace_back(PathStr, *Type,
129136
IncludeName.has_value() ? IncludeName.value() : "",
@@ -170,9 +177,10 @@ Error Implementation::parse(StringRef Input) {
170177

171178
llvm::Error
172179
FileListReader::loadHeaders(std::unique_ptr<MemoryBuffer> InputBuffer,
173-
HeaderSeq &Destination) {
180+
HeaderSeq &Destination, clang::FileManager *FM) {
174181
Implementation Impl;
175182
Impl.InputBuffer = std::move(InputBuffer);
183+
Impl.FM = FM;
176184

177185
if (llvm::Error Err = Impl.parse(Impl.InputBuffer->getBuffer()))
178186
return Err;

‎clang/tools/clang-installapi/Options.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ InstallAPIContext Options::createContext() {
697697
}
698698
Expected<AliasMap> Result = parseAliasList(Buffer.get());
699699
if (!Result) {
700-
Diags->Report(diag::err_cannot_read_alias_list)
701-
<< ListPath << toString(Result.takeError());
700+
Diags->Report(diag::err_cannot_read_input_list)
701+
<< /*IsFileList=*/false << ListPath << toString(Result.takeError());
702702
return Ctx;
703703
}
704704
Aliases.insert(Result.get().begin(), Result.get().end());
@@ -717,8 +717,9 @@ InstallAPIContext Options::createContext() {
717717
return Ctx;
718718
}
719719
if (auto Err = FileListReader::loadHeaders(std::move(Buffer.get()),
720-
Ctx.InputHeaders)) {
721-
Diags->Report(diag::err_cannot_open_file) << ListPath << std::move(Err);
720+
Ctx.InputHeaders, FM)) {
721+
Diags->Report(diag::err_cannot_read_input_list)
722+
<< /*IsFileList=*/true << ListPath << std::move(Err);
722723
return Ctx;
723724
}
724725
}

0 commit comments

Comments
 (0)
Please sign in to comment.