10#include "llvm/ADT/StringSwitch.h"
11#include "llvm/Support/Error.h"
12#include "llvm/Support/JSON.h"
13#include "llvm/TextAPI/TextAPIError.h"
43 Expected<StringRef> parseString(
const Object *Obj, StringRef Key,
45 Expected<StringRef> parsePath(
const Object *Obj);
46 Expected<HeaderType> parseType(
const Object *Obj);
47 std::optional<clang::Language> parseLanguage(
const Object *Obj);
48 Error parseHeaders(Array &Headers);
51 std::unique_ptr<MemoryBuffer> InputBuffer;
52 clang::FileManager *FM;
56 Error parse(StringRef Input);
60Implementation::parseString(
const Object *Obj, StringRef Key, StringRef Error) {
61 auto Str = Obj->getString(Key);
63 return make_error<StringError>(Error, inconvertibleErrorCode());
67Expected<HeaderType> Implementation::parseType(
const Object *Obj) {
69 parseString(Obj,
"type",
"required field 'type' not specified");
71 return TypeStr.takeError();
73 if (*TypeStr ==
"public")
74 return HeaderType::Public;
75 else if (*TypeStr ==
"private")
76 return HeaderType::Private;
77 else if (*TypeStr ==
"project" && Version >= 2)
78 return HeaderType::Project;
80 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
81 "unsupported header type");
84Expected<StringRef> Implementation::parsePath(
const Object *Obj) {
85 auto Path = parseString(Obj,
"path",
"required field 'path' not specified");
87 return Path.takeError();
92std::optional<clang::Language>
93Implementation::parseLanguage(
const Object *Obj) {
94 auto Language = Obj->getString(
"language");
98 return StringSwitch<clang::Language>(*Language)
106Error Implementation::parseHeaders(Array &Headers) {
107 for (
const auto &H : Headers) {
108 auto *Obj = H.getAsObject();
110 return make_error<StringError>(
"expect a JSON object",
111 inconvertibleErrorCode());
112 auto Type = parseType(Obj);
114 return Type.takeError();
115 auto Path = parsePath(Obj);
117 return Path.takeError();
120 StringRef PathStr = *Path;
121 if (*Type == HeaderType::Project) {
122 HeaderList.emplace_back(
123 HeaderFile{PathStr, *Type,
"",
Language});
129 return createFileError(
133 HeaderList.emplace_back(PathStr, *Type,
134 IncludeName.has_value() ? IncludeName.value() :
"",
138 return Error::success();
141Error Implementation::parse(StringRef Input) {
142 auto Val = json::parse(Input);
144 return Val.takeError();
146 auto *Root = Val->getAsObject();
148 return make_error<StringError>(
"not a JSON object",
149 inconvertibleErrorCode());
151 auto VersionStr = Root->getString(
"version");
153 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
154 "required field 'version' not specified");
155 if (VersionStr->getAsInteger(10, Version))
156 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
157 "invalid version number");
159 if (Version < 1 || Version > 3)
160 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
161 "unsupported version");
164 auto Headers = Root->getArray(
"headers");
166 return Error::success();
168 Error Err = parseHeaders(*Headers);
172 return Error::success();
180 Impl.InputBuffer = std::move(InputBuffer);
183 if (llvm::Error Err = Impl.parse(Impl.InputBuffer->getBuffer()))
186 Destination.reserve(Destination.size() + Impl.HeaderList.size());
187 llvm::move(Impl.HeaderList, std::back_inserter(Destination));
189 return Error::success();
Implements support for file system lookup, file system caching, and directory search management.
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
static llvm::Error loadHeaders(std::unique_ptr< llvm::MemoryBuffer > InputBuffer, HeaderSeq &Destination, clang::FileManager *FM=nullptr)
Decode JSON input and append header input into destination container.
The DirectoryScanner for collecting library files on the file system.
std::optional< std::string > createIncludeHeaderName(const StringRef FullPath)
Assemble expected way header will be included by clients.
std::vector< HeaderFile > HeaderSeq
Language
The language for the input, used to select and validate the language standard and possible actions.
@ C
Languages that the frontend can parse and compile.
Diagnostic wrappers for TextAPI types for error reporting.