12#include "llvm/ADT/StringSwitch.h"
13#include "llvm/Support/Error.h"
14#include "llvm/Support/JSON.h"
15#include "llvm/TextAPI/TextAPIError.h"
49 std::optional<clang::Language> parseLanguage(
const Object *Obj);
50 Error parseHeaders(Array &Headers);
53 std::unique_ptr<MemoryBuffer> InputBuffer;
58 Error parse(StringRef Input);
62Implementation::parseString(
const Object *Obj, StringRef Key, StringRef Error) {
63 auto Str = Obj->getString(Key);
65 return make_error<StringError>(Error, inconvertibleErrorCode());
71 parseString(Obj,
"type",
"required field 'type' not specified");
73 return TypeStr.takeError();
75 if (*TypeStr ==
"public")
76 return HeaderType::Public;
77 else if (*TypeStr ==
"private")
78 return HeaderType::Private;
79 else if (*TypeStr ==
"project" && Version >= 2)
80 return HeaderType::Project;
82 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
83 "unsupported header type");
87 auto Path = parseString(Obj,
"path",
"required field 'path' not specified");
89 return Path.takeError();
94std::optional<clang::Language>
95Implementation::parseLanguage(
const Object *Obj) {
96 auto Language = Obj->getString(
"language");
100 return StringSwitch<clang::Language>(*
Language)
108Error Implementation::parseHeaders(Array &Headers) {
109 for (
const auto &H : Headers) {
110 auto *Obj = H.getAsObject();
112 return make_error<StringError>(
"expect a JSON object",
113 inconvertibleErrorCode());
114 auto Type = parseType(Obj);
116 return Type.takeError();
117 auto Path = parsePath(Obj);
119 return Path.takeError();
122 StringRef PathStr = *
Path;
123 if (*
Type == HeaderType::Project) {
124 HeaderList.emplace_back(
130 if (!FM->getOptionalFileRef(PathStr))
131 return createFileError(
135 HeaderList.emplace_back(PathStr, *
Type,
136 IncludeName.has_value() ? IncludeName.value() :
"",
140 return Error::success();
143Error Implementation::parse(StringRef Input) {
144 auto Val = json::parse(Input);
146 return Val.takeError();
148 auto *Root = Val->getAsObject();
150 return make_error<StringError>(
"not a JSON object",
151 inconvertibleErrorCode());
153 auto VersionStr = Root->getString(
"version");
155 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
156 "required field 'version' not specified");
157 if (VersionStr->getAsInteger(10, Version))
158 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
159 "invalid version number");
161 if (Version < 1 || Version > 3)
162 return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
163 "unsupported version");
166 auto Headers = Root->getArray(
"headers");
168 return Error::success();
170 Error Err = parseHeaders(*Headers);
174 return Error::success();
182 Impl.InputBuffer = std::move(InputBuffer);
185 if (llvm::Error Err = Impl.parse(Impl.InputBuffer->getBuffer()))
188 Destination.reserve(Destination.size() + Impl.HeaderList.size());
189 llvm::move(Impl.HeaderList, std::back_inserter(Destination));
191 return Error::success();
Implements support for file system lookup, file system caching, and directory search management.
The base class of the type hierarchy.
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::vector< HeaderFile > HeaderSeq
std::optional< std::string > createIncludeHeaderName(const StringRef FullPath)
Assemble expected way header will be included by clients.
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.