-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang][InstallAPI] Add input file support to library
This patch adds support for expected InstallAPI inputs. InstallAPI accepts a well defined filelist of headers and how those headers represent a single library. InstallAPI captures header files to determine linkable symbols to then compare against what was compiled in a binary dylib and generate TBD files.
- Loading branch information
1 parent
0fc5786
commit 9d0da00
Showing
11 changed files
with
603 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//===- InstallAPI/FileList.h ------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// The JSON file list parser is used to communicate input to InstallAPI. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_INSTALLAPI_FILELIST_H | ||
#define LLVM_CLANG_INSTALLAPI_FILELIST_H | ||
|
||
#include "clang/Basic/Diagnostic.h" | ||
#include "clang/Basic/FileManager.h" | ||
#include "clang/InstallAPI/HeaderFile.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
|
||
namespace clang { | ||
namespace installapi { | ||
|
||
/// JSON decoder for InstallAPI Inputs. | ||
class FileListReader { | ||
|
||
class Implementation; | ||
Implementation &Impl; | ||
|
||
FileListReader(std::unique_ptr<llvm::MemoryBuffer> InputBuffer, | ||
llvm::Error &Err); | ||
|
||
public: | ||
/// Decode JSON input and append header input into destination container. | ||
/// Headers are loaded in the order they appear in the JSON input. | ||
/// | ||
/// \param InputBuffer JSON input data. | ||
/// \param Destination Container to load headers into. | ||
static llvm::Error | ||
loadHeaders(std::unique_ptr<llvm::MemoryBuffer> InputBuffer, | ||
HeaderSeq &Destination); | ||
|
||
~FileListReader(); | ||
|
||
FileListReader(const FileListReader &) = delete; | ||
FileListReader &operator=(const FileListReader &) = delete; | ||
}; | ||
|
||
} // namespace installapi | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_INSTALLAPI_FILELIST_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//===- InstallAPI/HeaderFile.h ----------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// Representations of a library's headers for InstallAPI. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_INSTALLAPI_HEADERFILE_H | ||
#define LLVM_CLANG_INSTALLAPI_HEADERFILE_H | ||
|
||
#include "clang/Basic/LangStandard.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/Regex.h" | ||
#include <optional> | ||
#include <string> | ||
|
||
namespace clang::installapi { | ||
enum class HeaderType { | ||
/// Represents declarations accessible to all clients. | ||
Public, | ||
/// Represents declarations accessible to a disclosed set of clients. | ||
Private, | ||
/// Represents declarations only accessible as implementation details to the | ||
/// input library. | ||
Project, | ||
}; | ||
|
||
class HeaderFile { | ||
/// Full input path to header. | ||
std::string FullPath; | ||
/// Access level of header. | ||
HeaderType Type; | ||
/// Expected way header will be included by clients. | ||
std::string IncludeName; | ||
/// Supported language mode for header. | ||
std::optional<clang::Language> Language; | ||
|
||
public: | ||
HeaderFile(StringRef FullPath, HeaderType Type, | ||
StringRef IncludeName = StringRef(), | ||
std::optional<clang::Language> Language = std::nullopt) | ||
: FullPath(FullPath), Type(Type), IncludeName(IncludeName), | ||
Language(Language) {} | ||
|
||
static llvm::Regex getFrameworkIncludeRule(); | ||
|
||
bool operator==(const HeaderFile &Other) const { | ||
return std::tie(Type, FullPath, IncludeName, Language) == | ||
std::tie(Other.Type, Other.FullPath, Other.IncludeName, | ||
Other.Language); | ||
} | ||
}; | ||
|
||
/// Assemble expected way header will be included by clients. | ||
/// As in what maps inside the brackets of `#include <IncludeName.h>` | ||
/// For example, | ||
/// "/System/Library/Frameworks/Foo.framework/Headers/Foo.h" returns | ||
/// "Foo/Foo.h" | ||
/// | ||
/// \param FullPath Path to the header file which includes the library | ||
/// structure. | ||
std::optional<std::string> createIncludeHeaderName(const StringRef FullPath); | ||
using HeaderSeq = std::vector<HeaderFile>; | ||
|
||
} // namespace clang::installapi | ||
|
||
#endif // LLVM_CLANG_INSTALLAPI_HEADERFILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,6 @@ add_clang_library(clangExtractAPI | |
clangBasic | ||
clangFrontend | ||
clangIndex | ||
clangInstallAPI | ||
clangLex | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
//===- FileList.cpp ---------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/InstallAPI/FileList.h" | ||
#include "clang/Basic/DiagnosticFrontend.h" | ||
#include "clang/InstallAPI/FileList.h" | ||
#include "llvm/ADT/StringSwitch.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/JSON.h" | ||
#include "llvm/TextAPI/TextAPIError.h" | ||
#include <optional> | ||
|
||
// clang-format off | ||
/* | ||
InstallAPI JSON Input Format specification. | ||
{ | ||
"headers" : [ # Required: Key must exist. | ||
{ # Optional: May contain 0 or more header inputs. | ||
"path" : "/usr/include/mach-o/dlfn.h", # Required: Path should point to destination | ||
# location where applicable. | ||
"type" : "public", # Required: Maps to HeaderType for header. | ||
"language": "c++" # Optional: Language mode for header. | ||
} | ||
], | ||
"version" : "3" # Required: Version 3 supports language mode | ||
& project header input. | ||
} | ||
*/ | ||
// clang-format on | ||
|
||
using namespace llvm; | ||
using namespace llvm::json; | ||
using namespace llvm::MachO; | ||
using namespace clang::installapi; | ||
|
||
class FileListReader::Implementation { | ||
private: | ||
Expected<StringRef> parseString(const Object *Obj, StringRef Key, | ||
StringRef Error); | ||
Expected<StringRef> parsePath(const Object *Obj); | ||
Expected<HeaderType> parseType(const Object *Obj); | ||
std::optional<clang::Language> parseLanguage(const Object *Obj); | ||
Error parseHeaders(Array &Headers); | ||
|
||
public: | ||
std::unique_ptr<MemoryBuffer> InputBuffer; | ||
unsigned Version; | ||
HeaderSeq HeaderList; | ||
|
||
Error parse(StringRef Input); | ||
}; | ||
|
||
Expected<StringRef> | ||
FileListReader::Implementation::parseString(const Object *Obj, StringRef Key, | ||
StringRef Error) { | ||
auto Str = Obj->getString(Key); | ||
if (!Str) | ||
return make_error<StringError>(Error, inconvertibleErrorCode()); | ||
return *Str; | ||
} | ||
|
||
Expected<HeaderType> | ||
FileListReader::Implementation::parseType(const Object *Obj) { | ||
auto TypeStr = | ||
parseString(Obj, "type", "required field 'type' not specified"); | ||
if (!TypeStr) | ||
return TypeStr.takeError(); | ||
|
||
if (*TypeStr == "public") | ||
return HeaderType::Public; | ||
else if (*TypeStr == "private") | ||
return HeaderType::Private; | ||
else if (*TypeStr == "project" && Version >= 2) | ||
return HeaderType::Project; | ||
|
||
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat, | ||
"unsupported header type"); | ||
} | ||
|
||
Expected<StringRef> | ||
FileListReader::Implementation::parsePath(const Object *Obj) { | ||
auto Path = parseString(Obj, "path", "required field 'path' not specified"); | ||
if (!Path) | ||
return Path.takeError(); | ||
|
||
return *Path; | ||
} | ||
|
||
std::optional<clang::Language> | ||
FileListReader::Implementation::parseLanguage(const Object *Obj) { | ||
auto Language = Obj->getString("language"); | ||
if (!Language) | ||
return std::nullopt; | ||
|
||
return StringSwitch<clang::Language>(*Language) | ||
.Case("c", clang::Language::C) | ||
.Case("c++", clang::Language::CXX) | ||
.Case("objective-c", clang::Language::ObjC) | ||
.Case("objective-c++", clang::Language::ObjCXX) | ||
.Default(clang::Language::Unknown); | ||
} | ||
|
||
Error FileListReader::Implementation::parseHeaders(Array &Headers) { | ||
for (const auto &H : Headers) { | ||
auto *Obj = H.getAsObject(); | ||
if (!Obj) | ||
return make_error<StringError>("expect a JSON object", | ||
inconvertibleErrorCode()); | ||
auto Type = parseType(Obj); | ||
if (!Type) | ||
return Type.takeError(); | ||
auto Path = parsePath(Obj); | ||
if (!Path) | ||
return Path.takeError(); | ||
auto Language = parseLanguage(Obj); | ||
|
||
StringRef PathStr = *Path; | ||
if (*Type == HeaderType::Project) { | ||
HeaderList.emplace_back( | ||
HeaderFile{PathStr, *Type, /*IncludeName=*/"", Language}); | ||
continue; | ||
} | ||
auto IncludeName = createIncludeHeaderName(PathStr); | ||
HeaderList.emplace_back(PathStr, *Type, | ||
IncludeName.has_value() ? IncludeName.value() : "", | ||
Language); | ||
} | ||
|
||
return Error::success(); | ||
} | ||
|
||
Error FileListReader::Implementation::parse(StringRef Input) { | ||
auto Val = json::parse(Input); | ||
if (!Val) | ||
return Val.takeError(); | ||
|
||
auto *Root = Val->getAsObject(); | ||
if (!Root) | ||
return make_error<StringError>("not a JSON object", | ||
inconvertibleErrorCode()); | ||
|
||
auto VersionStr = Root->getString("version"); | ||
if (!VersionStr) | ||
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat, | ||
"required field 'version' not specified"); | ||
if (VersionStr->getAsInteger(10, Version)) | ||
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat, | ||
"invalid version number"); | ||
|
||
if (Version < 1 || Version > 3) | ||
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat, | ||
"unsupported version"); | ||
|
||
// Not specifying any header files should be atypical, but valid. | ||
auto Headers = Root->getArray("headers"); | ||
if (!Headers) | ||
return Error::success(); | ||
|
||
Error Err = parseHeaders(*Headers); | ||
if (Err) | ||
return Err; | ||
|
||
return Error::success(); | ||
} | ||
|
||
FileListReader::FileListReader(std::unique_ptr<MemoryBuffer> InputBuffer, | ||
Error &Error) | ||
: Impl(*new FileListReader::Implementation()) { | ||
ErrorAsOutParameter ErrorAsOutParam(&Error); | ||
Impl.InputBuffer = std::move(InputBuffer); | ||
|
||
Error = Impl.parse(Impl.InputBuffer->getBuffer()); | ||
} | ||
|
||
llvm::Error | ||
FileListReader::loadHeaders(std::unique_ptr<MemoryBuffer> InputBuffer, | ||
HeaderSeq &Destination) { | ||
llvm::Error Err = Error::success(); | ||
std::unique_ptr<FileListReader> Reader( | ||
new FileListReader(std::move(InputBuffer), Err)); | ||
if (Err) | ||
return Err; | ||
|
||
Destination.reserve(Destination.size() + Reader->Impl.HeaderList.size()); | ||
llvm::move(Reader->Impl.HeaderList, std::back_inserter(Destination)); | ||
|
||
return Err; | ||
} | ||
|
||
FileListReader::~FileListReader() { delete &Impl; } |
Oops, something went wrong.