YAMLTraits.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. //===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/YAMLTraits.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/Support/Casting.h"
  15. #include "llvm/Support/Errc.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/LineIterator.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include "llvm/Support/VersionTuple.h"
  21. #include "llvm/Support/YAMLParser.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <algorithm>
  24. #include <cassert>
  25. #include <cstdint>
  26. #include <cstring>
  27. #include <string>
  28. #include <vector>
  29. using namespace llvm;
  30. using namespace yaml;
  31. //===----------------------------------------------------------------------===//
  32. // IO
  33. //===----------------------------------------------------------------------===//
  34. IO::IO(void *Context) : Ctxt(Context) {}
  35. IO::~IO() = default;
  36. void *IO::getContext() const {
  37. return Ctxt;
  38. }
  39. void IO::setContext(void *Context) {
  40. Ctxt = Context;
  41. }
  42. void IO::setAllowUnknownKeys(bool Allow) {
  43. llvm_unreachable("Only supported for Input");
  44. }
  45. //===----------------------------------------------------------------------===//
  46. // Input
  47. //===----------------------------------------------------------------------===//
  48. Input::Input(StringRef InputContent, void *Ctxt,
  49. SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
  50. : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)) {
  51. if (DiagHandler)
  52. SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
  53. DocIterator = Strm->begin();
  54. }
  55. Input::Input(MemoryBufferRef Input, void *Ctxt,
  56. SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
  57. : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
  58. if (DiagHandler)
  59. SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
  60. DocIterator = Strm->begin();
  61. }
  62. Input::~Input() = default;
  63. std::error_code Input::error() { return EC; }
  64. // Pin the vtables to this file.
  65. void Input::HNode::anchor() {}
  66. void Input::EmptyHNode::anchor() {}
  67. void Input::ScalarHNode::anchor() {}
  68. void Input::MapHNode::anchor() {}
  69. void Input::SequenceHNode::anchor() {}
  70. bool Input::outputting() const {
  71. return false;
  72. }
  73. bool Input::setCurrentDocument() {
  74. if (DocIterator != Strm->end()) {
  75. Node *N = DocIterator->getRoot();
  76. if (!N) {
  77. EC = make_error_code(errc::invalid_argument);
  78. return false;
  79. }
  80. if (isa<NullNode>(N)) {
  81. // Empty files are allowed and ignored
  82. ++DocIterator;
  83. return setCurrentDocument();
  84. }
  85. TopNode = createHNodes(N);
  86. CurrentNode = TopNode.get();
  87. return true;
  88. }
  89. return false;
  90. }
  91. bool Input::nextDocument() {
  92. return ++DocIterator != Strm->end();
  93. }
  94. const Node *Input::getCurrentNode() const {
  95. return CurrentNode ? CurrentNode->_node : nullptr;
  96. }
  97. bool Input::mapTag(StringRef Tag, bool Default) {
  98. // CurrentNode can be null if setCurrentDocument() was unable to
  99. // parse the document because it was invalid or empty.
  100. if (!CurrentNode)
  101. return false;
  102. std::string foundTag = CurrentNode->_node->getVerbatimTag();
  103. if (foundTag.empty()) {
  104. // If no tag found and 'Tag' is the default, say it was found.
  105. return Default;
  106. }
  107. // Return true iff found tag matches supplied tag.
  108. return Tag.equals(foundTag);
  109. }
  110. void Input::beginMapping() {
  111. if (EC)
  112. return;
  113. // CurrentNode can be null if the document is empty.
  114. MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
  115. if (MN) {
  116. MN->ValidKeys.clear();
  117. }
  118. }
  119. std::vector<StringRef> Input::keys() {
  120. MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
  121. std::vector<StringRef> Ret;
  122. if (!MN) {
  123. setError(CurrentNode, "not a mapping");
  124. return Ret;
  125. }
  126. for (auto &P : MN->Mapping)
  127. Ret.push_back(P.first());
  128. return Ret;
  129. }
  130. bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
  131. void *&SaveInfo) {
  132. UseDefault = false;
  133. if (EC)
  134. return false;
  135. // CurrentNode is null for empty documents, which is an error in case required
  136. // nodes are present.
  137. if (!CurrentNode) {
  138. if (Required)
  139. EC = make_error_code(errc::invalid_argument);
  140. return false;
  141. }
  142. MapHNode *MN = dyn_cast<MapHNode>(CurrentNode);
  143. if (!MN) {
  144. if (Required || !isa<EmptyHNode>(CurrentNode))
  145. setError(CurrentNode, "not a mapping");
  146. else
  147. UseDefault = true;
  148. return false;
  149. }
  150. MN->ValidKeys.push_back(Key);
  151. HNode *Value = MN->Mapping[Key].first.get();
  152. if (!Value) {
  153. if (Required)
  154. setError(CurrentNode, Twine("missing required key '") + Key + "'");
  155. else
  156. UseDefault = true;
  157. return false;
  158. }
  159. SaveInfo = CurrentNode;
  160. CurrentNode = Value;
  161. return true;
  162. }
  163. void Input::postflightKey(void *saveInfo) {
  164. CurrentNode = reinterpret_cast<HNode *>(saveInfo);
  165. }
  166. void Input::endMapping() {
  167. if (EC)
  168. return;
  169. // CurrentNode can be null if the document is empty.
  170. MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
  171. if (!MN)
  172. return;
  173. for (const auto &NN : MN->Mapping) {
  174. if (!is_contained(MN->ValidKeys, NN.first())) {
  175. const SMRange &ReportLoc = NN.second.second;
  176. if (!AllowUnknownKeys) {
  177. setError(ReportLoc, Twine("unknown key '") + NN.first() + "'");
  178. break;
  179. } else
  180. reportWarning(ReportLoc, Twine("unknown key '") + NN.first() + "'");
  181. }
  182. }
  183. }
  184. void Input::beginFlowMapping() { beginMapping(); }
  185. void Input::endFlowMapping() { endMapping(); }
  186. unsigned Input::beginSequence() {
  187. if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode))
  188. return SQ->Entries.size();
  189. if (isa<EmptyHNode>(CurrentNode))
  190. return 0;
  191. // Treat case where there's a scalar "null" value as an empty sequence.
  192. if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
  193. if (isNull(SN->value()))
  194. return 0;
  195. }
  196. // Any other type of HNode is an error.
  197. setError(CurrentNode, "not a sequence");
  198. return 0;
  199. }
  200. void Input::endSequence() {
  201. }
  202. bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
  203. if (EC)
  204. return false;
  205. if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
  206. SaveInfo = CurrentNode;
  207. CurrentNode = SQ->Entries[Index].get();
  208. return true;
  209. }
  210. return false;
  211. }
  212. void Input::postflightElement(void *SaveInfo) {
  213. CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
  214. }
  215. unsigned Input::beginFlowSequence() { return beginSequence(); }
  216. bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
  217. if (EC)
  218. return false;
  219. if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
  220. SaveInfo = CurrentNode;
  221. CurrentNode = SQ->Entries[index].get();
  222. return true;
  223. }
  224. return false;
  225. }
  226. void Input::postflightFlowElement(void *SaveInfo) {
  227. CurrentNode = reinterpret_cast<HNode *>(SaveInfo);
  228. }
  229. void Input::endFlowSequence() {
  230. }
  231. void Input::beginEnumScalar() {
  232. ScalarMatchFound = false;
  233. }
  234. bool Input::matchEnumScalar(const char *Str, bool) {
  235. if (ScalarMatchFound)
  236. return false;
  237. if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
  238. if (SN->value().equals(Str)) {
  239. ScalarMatchFound = true;
  240. return true;
  241. }
  242. }
  243. return false;
  244. }
  245. bool Input::matchEnumFallback() {
  246. if (ScalarMatchFound)
  247. return false;
  248. ScalarMatchFound = true;
  249. return true;
  250. }
  251. void Input::endEnumScalar() {
  252. if (!ScalarMatchFound) {
  253. setError(CurrentNode, "unknown enumerated scalar");
  254. }
  255. }
  256. bool Input::beginBitSetScalar(bool &DoClear) {
  257. BitValuesUsed.clear();
  258. if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
  259. BitValuesUsed.resize(SQ->Entries.size());
  260. } else {
  261. setError(CurrentNode, "expected sequence of bit values");
  262. }
  263. DoClear = true;
  264. return true;
  265. }
  266. bool Input::bitSetMatch(const char *Str, bool) {
  267. if (EC)
  268. return false;
  269. if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
  270. unsigned Index = 0;
  271. for (auto &N : SQ->Entries) {
  272. if (ScalarHNode *SN = dyn_cast<ScalarHNode>(N.get())) {
  273. if (SN->value().equals(Str)) {
  274. BitValuesUsed[Index] = true;
  275. return true;
  276. }
  277. } else {
  278. setError(CurrentNode, "unexpected scalar in sequence of bit values");
  279. }
  280. ++Index;
  281. }
  282. } else {
  283. setError(CurrentNode, "expected sequence of bit values");
  284. }
  285. return false;
  286. }
  287. void Input::endBitSetScalar() {
  288. if (EC)
  289. return;
  290. if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
  291. assert(BitValuesUsed.size() == SQ->Entries.size());
  292. for (unsigned i = 0; i < SQ->Entries.size(); ++i) {
  293. if (!BitValuesUsed[i]) {
  294. setError(SQ->Entries[i].get(), "unknown bit value");
  295. return;
  296. }
  297. }
  298. }
  299. }
  300. void Input::scalarString(StringRef &S, QuotingType) {
  301. if (ScalarHNode *SN = dyn_cast<ScalarHNode>(CurrentNode)) {
  302. S = SN->value();
  303. } else {
  304. setError(CurrentNode, "unexpected scalar");
  305. }
  306. }
  307. void Input::blockScalarString(StringRef &S) { scalarString(S, QuotingType::None); }
  308. void Input::scalarTag(std::string &Tag) {
  309. Tag = CurrentNode->_node->getVerbatimTag();
  310. }
  311. void Input::setError(HNode *hnode, const Twine &message) {
  312. assert(hnode && "HNode must not be NULL");
  313. setError(hnode->_node, message);
  314. }
  315. NodeKind Input::getNodeKind() {
  316. if (isa<ScalarHNode>(CurrentNode))
  317. return NodeKind::Scalar;
  318. else if (isa<MapHNode>(CurrentNode))
  319. return NodeKind::Map;
  320. else if (isa<SequenceHNode>(CurrentNode))
  321. return NodeKind::Sequence;
  322. llvm_unreachable("Unsupported node kind");
  323. }
  324. void Input::setError(Node *node, const Twine &message) {
  325. Strm->printError(node, message);
  326. EC = make_error_code(errc::invalid_argument);
  327. }
  328. void Input::setError(const SMRange &range, const Twine &message) {
  329. Strm->printError(range, message);
  330. EC = make_error_code(errc::invalid_argument);
  331. }
  332. void Input::reportWarning(HNode *hnode, const Twine &message) {
  333. assert(hnode && "HNode must not be NULL");
  334. Strm->printError(hnode->_node, message, SourceMgr::DK_Warning);
  335. }
  336. void Input::reportWarning(Node *node, const Twine &message) {
  337. Strm->printError(node, message, SourceMgr::DK_Warning);
  338. }
  339. void Input::reportWarning(const SMRange &range, const Twine &message) {
  340. Strm->printError(range, message, SourceMgr::DK_Warning);
  341. }
  342. std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
  343. SmallString<128> StringStorage;
  344. if (ScalarNode *SN = dyn_cast<ScalarNode>(N)) {
  345. StringRef KeyStr = SN->getValue(StringStorage);
  346. if (!StringStorage.empty()) {
  347. // Copy string to permanent storage
  348. KeyStr = StringStorage.str().copy(StringAllocator);
  349. }
  350. return std::make_unique<ScalarHNode>(N, KeyStr);
  351. } else if (BlockScalarNode *BSN = dyn_cast<BlockScalarNode>(N)) {
  352. StringRef ValueCopy = BSN->getValue().copy(StringAllocator);
  353. return std::make_unique<ScalarHNode>(N, ValueCopy);
  354. } else if (SequenceNode *SQ = dyn_cast<SequenceNode>(N)) {
  355. auto SQHNode = std::make_unique<SequenceHNode>(N);
  356. for (Node &SN : *SQ) {
  357. auto Entry = createHNodes(&SN);
  358. if (EC)
  359. break;
  360. SQHNode->Entries.push_back(std::move(Entry));
  361. }
  362. return std::move(SQHNode);
  363. } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
  364. auto mapHNode = std::make_unique<MapHNode>(N);
  365. for (KeyValueNode &KVN : *Map) {
  366. Node *KeyNode = KVN.getKey();
  367. ScalarNode *Key = dyn_cast_or_null<ScalarNode>(KeyNode);
  368. Node *Value = KVN.getValue();
  369. if (!Key || !Value) {
  370. if (!Key)
  371. setError(KeyNode, "Map key must be a scalar");
  372. if (!Value)
  373. setError(KeyNode, "Map value must not be empty");
  374. break;
  375. }
  376. StringStorage.clear();
  377. StringRef KeyStr = Key->getValue(StringStorage);
  378. if (!StringStorage.empty()) {
  379. // Copy string to permanent storage
  380. KeyStr = StringStorage.str().copy(StringAllocator);
  381. }
  382. auto ValueHNode = createHNodes(Value);
  383. if (EC)
  384. break;
  385. mapHNode->Mapping[KeyStr] =
  386. std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange());
  387. }
  388. return std::move(mapHNode);
  389. } else if (isa<NullNode>(N)) {
  390. return std::make_unique<EmptyHNode>(N);
  391. } else {
  392. setError(N, "unknown node kind");
  393. return nullptr;
  394. }
  395. }
  396. void Input::setError(const Twine &Message) {
  397. setError(CurrentNode, Message);
  398. }
  399. void Input::setAllowUnknownKeys(bool Allow) { AllowUnknownKeys = Allow; }
  400. bool Input::canElideEmptySequence() {
  401. return false;
  402. }
  403. //===----------------------------------------------------------------------===//
  404. // Output
  405. //===----------------------------------------------------------------------===//
  406. Output::Output(raw_ostream &yout, void *context, int WrapColumn)
  407. : IO(context), Out(yout), WrapColumn(WrapColumn) {}
  408. Output::~Output() = default;
  409. bool Output::outputting() const {
  410. return true;
  411. }
  412. void Output::beginMapping() {
  413. StateStack.push_back(inMapFirstKey);
  414. PaddingBeforeContainer = Padding;
  415. Padding = "\n";
  416. }
  417. bool Output::mapTag(StringRef Tag, bool Use) {
  418. if (Use) {
  419. // If this tag is being written inside a sequence we should write the start
  420. // of the sequence before writing the tag, otherwise the tag won't be
  421. // attached to the element in the sequence, but rather the sequence itself.
  422. bool SequenceElement = false;
  423. if (StateStack.size() > 1) {
  424. auto &E = StateStack[StateStack.size() - 2];
  425. SequenceElement = inSeqAnyElement(E) || inFlowSeqAnyElement(E);
  426. }
  427. if (SequenceElement && StateStack.back() == inMapFirstKey) {
  428. newLineCheck();
  429. } else {
  430. output(" ");
  431. }
  432. output(Tag);
  433. if (SequenceElement) {
  434. // If we're writing the tag during the first element of a map, the tag
  435. // takes the place of the first element in the sequence.
  436. if (StateStack.back() == inMapFirstKey) {
  437. StateStack.pop_back();
  438. StateStack.push_back(inMapOtherKey);
  439. }
  440. // Tags inside maps in sequences should act as keys in the map from a
  441. // formatting perspective, so we always want a newline in a sequence.
  442. Padding = "\n";
  443. }
  444. }
  445. return Use;
  446. }
  447. void Output::endMapping() {
  448. // If we did not map anything, we should explicitly emit an empty map
  449. if (StateStack.back() == inMapFirstKey) {
  450. Padding = PaddingBeforeContainer;
  451. newLineCheck();
  452. output("{}");
  453. Padding = "\n";
  454. }
  455. StateStack.pop_back();
  456. }
  457. std::vector<StringRef> Output::keys() {
  458. report_fatal_error("invalid call");
  459. }
  460. bool Output::preflightKey(const char *Key, bool Required, bool SameAsDefault,
  461. bool &UseDefault, void *&SaveInfo) {
  462. UseDefault = false;
  463. SaveInfo = nullptr;
  464. if (Required || !SameAsDefault || WriteDefaultValues) {
  465. auto State = StateStack.back();
  466. if (State == inFlowMapFirstKey || State == inFlowMapOtherKey) {
  467. flowKey(Key);
  468. } else {
  469. newLineCheck();
  470. paddedKey(Key);
  471. }
  472. return true;
  473. }
  474. return false;
  475. }
  476. void Output::postflightKey(void *) {
  477. if (StateStack.back() == inMapFirstKey) {
  478. StateStack.pop_back();
  479. StateStack.push_back(inMapOtherKey);
  480. } else if (StateStack.back() == inFlowMapFirstKey) {
  481. StateStack.pop_back();
  482. StateStack.push_back(inFlowMapOtherKey);
  483. }
  484. }
  485. void Output::beginFlowMapping() {
  486. StateStack.push_back(inFlowMapFirstKey);
  487. newLineCheck();
  488. ColumnAtMapFlowStart = Column;
  489. output("{ ");
  490. }
  491. void Output::endFlowMapping() {
  492. StateStack.pop_back();
  493. outputUpToEndOfLine(" }");
  494. }
  495. void Output::beginDocuments() {
  496. outputUpToEndOfLine("---");
  497. }
  498. bool Output::preflightDocument(unsigned index) {
  499. if (index > 0)
  500. outputUpToEndOfLine("\n---");
  501. return true;
  502. }
  503. void Output::postflightDocument() {
  504. }
  505. void Output::endDocuments() {
  506. output("\n...\n");
  507. }
  508. unsigned Output::beginSequence() {
  509. StateStack.push_back(inSeqFirstElement);
  510. PaddingBeforeContainer = Padding;
  511. Padding = "\n";
  512. return 0;
  513. }
  514. void Output::endSequence() {
  515. // If we did not emit anything, we should explicitly emit an empty sequence
  516. if (StateStack.back() == inSeqFirstElement) {
  517. Padding = PaddingBeforeContainer;
  518. newLineCheck(/*EmptySequence=*/true);
  519. output("[]");
  520. Padding = "\n";
  521. }
  522. StateStack.pop_back();
  523. }
  524. bool Output::preflightElement(unsigned, void *&SaveInfo) {
  525. SaveInfo = nullptr;
  526. return true;
  527. }
  528. void Output::postflightElement(void *) {
  529. if (StateStack.back() == inSeqFirstElement) {
  530. StateStack.pop_back();
  531. StateStack.push_back(inSeqOtherElement);
  532. } else if (StateStack.back() == inFlowSeqFirstElement) {
  533. StateStack.pop_back();
  534. StateStack.push_back(inFlowSeqOtherElement);
  535. }
  536. }
  537. unsigned Output::beginFlowSequence() {
  538. StateStack.push_back(inFlowSeqFirstElement);
  539. newLineCheck();
  540. ColumnAtFlowStart = Column;
  541. output("[ ");
  542. NeedFlowSequenceComma = false;
  543. return 0;
  544. }
  545. void Output::endFlowSequence() {
  546. StateStack.pop_back();
  547. outputUpToEndOfLine(" ]");
  548. }
  549. bool Output::preflightFlowElement(unsigned, void *&SaveInfo) {
  550. if (NeedFlowSequenceComma)
  551. output(", ");
  552. if (WrapColumn && Column > WrapColumn) {
  553. output("\n");
  554. for (int i = 0; i < ColumnAtFlowStart; ++i)
  555. output(" ");
  556. Column = ColumnAtFlowStart;
  557. output(" ");
  558. }
  559. SaveInfo = nullptr;
  560. return true;
  561. }
  562. void Output::postflightFlowElement(void *) {
  563. NeedFlowSequenceComma = true;
  564. }
  565. void Output::beginEnumScalar() {
  566. EnumerationMatchFound = false;
  567. }
  568. bool Output::matchEnumScalar(const char *Str, bool Match) {
  569. if (Match && !EnumerationMatchFound) {
  570. newLineCheck();
  571. outputUpToEndOfLine(Str);
  572. EnumerationMatchFound = true;
  573. }
  574. return false;
  575. }
  576. bool Output::matchEnumFallback() {
  577. if (EnumerationMatchFound)
  578. return false;
  579. EnumerationMatchFound = true;
  580. return true;
  581. }
  582. void Output::endEnumScalar() {
  583. if (!EnumerationMatchFound)
  584. llvm_unreachable("bad runtime enum value");
  585. }
  586. bool Output::beginBitSetScalar(bool &DoClear) {
  587. newLineCheck();
  588. output("[ ");
  589. NeedBitValueComma = false;
  590. DoClear = false;
  591. return true;
  592. }
  593. bool Output::bitSetMatch(const char *Str, bool Matches) {
  594. if (Matches) {
  595. if (NeedBitValueComma)
  596. output(", ");
  597. output(Str);
  598. NeedBitValueComma = true;
  599. }
  600. return false;
  601. }
  602. void Output::endBitSetScalar() {
  603. outputUpToEndOfLine(" ]");
  604. }
  605. void Output::scalarString(StringRef &S, QuotingType MustQuote) {
  606. newLineCheck();
  607. if (S.empty()) {
  608. // Print '' for the empty string because leaving the field empty is not
  609. // allowed.
  610. outputUpToEndOfLine("''");
  611. return;
  612. }
  613. if (MustQuote == QuotingType::None) {
  614. // Only quote if we must.
  615. outputUpToEndOfLine(S);
  616. return;
  617. }
  618. const char *const Quote = MustQuote == QuotingType::Single ? "'" : "\"";
  619. output(Quote); // Starting quote.
  620. // When using double-quoted strings (and only in that case), non-printable characters may be
  621. // present, and will be escaped using a variety of unicode-scalar and special short-form
  622. // escapes. This is handled in yaml::escape.
  623. if (MustQuote == QuotingType::Double) {
  624. output(yaml::escape(S, /* EscapePrintable= */ false));
  625. outputUpToEndOfLine(Quote);
  626. return;
  627. }
  628. unsigned i = 0;
  629. unsigned j = 0;
  630. unsigned End = S.size();
  631. const char *Base = S.data();
  632. // When using single-quoted strings, any single quote ' must be doubled to be escaped.
  633. while (j < End) {
  634. if (S[j] == '\'') { // Escape quotes.
  635. output(StringRef(&Base[i], j - i)); // "flush".
  636. output(StringLiteral("''")); // Print it as ''
  637. i = j + 1;
  638. }
  639. ++j;
  640. }
  641. output(StringRef(&Base[i], j - i));
  642. outputUpToEndOfLine(Quote); // Ending quote.
  643. }
  644. void Output::blockScalarString(StringRef &S) {
  645. if (!StateStack.empty())
  646. newLineCheck();
  647. output(" |");
  648. outputNewLine();
  649. unsigned Indent = StateStack.empty() ? 1 : StateStack.size();
  650. auto Buffer = MemoryBuffer::getMemBuffer(S, "", false);
  651. for (line_iterator Lines(*Buffer, false); !Lines.is_at_end(); ++Lines) {
  652. for (unsigned I = 0; I < Indent; ++I) {
  653. output(" ");
  654. }
  655. output(*Lines);
  656. outputNewLine();
  657. }
  658. }
  659. void Output::scalarTag(std::string &Tag) {
  660. if (Tag.empty())
  661. return;
  662. newLineCheck();
  663. output(Tag);
  664. output(" ");
  665. }
  666. void Output::setError(const Twine &message) {
  667. }
  668. bool Output::canElideEmptySequence() {
  669. // Normally, with an optional key/value where the value is an empty sequence,
  670. // the whole key/value can be not written. But, that produces wrong yaml
  671. // if the key/value is the only thing in the map and the map is used in
  672. // a sequence. This detects if the this sequence is the first key/value
  673. // in map that itself is embedded in a sequence.
  674. if (StateStack.size() < 2)
  675. return true;
  676. if (StateStack.back() != inMapFirstKey)
  677. return true;
  678. return !inSeqAnyElement(StateStack[StateStack.size() - 2]);
  679. }
  680. void Output::output(StringRef s) {
  681. Column += s.size();
  682. Out << s;
  683. }
  684. void Output::outputUpToEndOfLine(StringRef s) {
  685. output(s);
  686. if (StateStack.empty() || (!inFlowSeqAnyElement(StateStack.back()) &&
  687. !inFlowMapAnyKey(StateStack.back())))
  688. Padding = "\n";
  689. }
  690. void Output::outputNewLine() {
  691. Out << "\n";
  692. Column = 0;
  693. }
  694. // if seq at top, indent as if map, then add "- "
  695. // if seq in middle, use "- " if firstKey, else use " "
  696. //
  697. void Output::newLineCheck(bool EmptySequence) {
  698. if (Padding != "\n") {
  699. output(Padding);
  700. Padding = {};
  701. return;
  702. }
  703. outputNewLine();
  704. Padding = {};
  705. if (StateStack.size() == 0 || EmptySequence)
  706. return;
  707. unsigned Indent = StateStack.size() - 1;
  708. bool OutputDash = false;
  709. if (StateStack.back() == inSeqFirstElement ||
  710. StateStack.back() == inSeqOtherElement) {
  711. OutputDash = true;
  712. } else if ((StateStack.size() > 1) &&
  713. ((StateStack.back() == inMapFirstKey) ||
  714. inFlowSeqAnyElement(StateStack.back()) ||
  715. (StateStack.back() == inFlowMapFirstKey)) &&
  716. inSeqAnyElement(StateStack[StateStack.size() - 2])) {
  717. --Indent;
  718. OutputDash = true;
  719. }
  720. for (unsigned i = 0; i < Indent; ++i) {
  721. output(" ");
  722. }
  723. if (OutputDash) {
  724. output("- ");
  725. }
  726. }
  727. void Output::paddedKey(StringRef key) {
  728. output(key);
  729. output(":");
  730. const char *spaces = " ";
  731. if (key.size() < strlen(spaces))
  732. Padding = &spaces[key.size()];
  733. else
  734. Padding = " ";
  735. }
  736. void Output::flowKey(StringRef Key) {
  737. if (StateStack.back() == inFlowMapOtherKey)
  738. output(", ");
  739. if (WrapColumn && Column > WrapColumn) {
  740. output("\n");
  741. for (int I = 0; I < ColumnAtMapFlowStart; ++I)
  742. output(" ");
  743. Column = ColumnAtMapFlowStart;
  744. output(" ");
  745. }
  746. output(Key);
  747. output(": ");
  748. }
  749. NodeKind Output::getNodeKind() { report_fatal_error("invalid call"); }
  750. bool Output::inSeqAnyElement(InState State) {
  751. return State == inSeqFirstElement || State == inSeqOtherElement;
  752. }
  753. bool Output::inFlowSeqAnyElement(InState State) {
  754. return State == inFlowSeqFirstElement || State == inFlowSeqOtherElement;
  755. }
  756. bool Output::inMapAnyKey(InState State) {
  757. return State == inMapFirstKey || State == inMapOtherKey;
  758. }
  759. bool Output::inFlowMapAnyKey(InState State) {
  760. return State == inFlowMapFirstKey || State == inFlowMapOtherKey;
  761. }
  762. //===----------------------------------------------------------------------===//
  763. // traits for built-in types
  764. //===----------------------------------------------------------------------===//
  765. void ScalarTraits<bool>::output(const bool &Val, void *, raw_ostream &Out) {
  766. Out << (Val ? "true" : "false");
  767. }
  768. StringRef ScalarTraits<bool>::input(StringRef Scalar, void *, bool &Val) {
  769. if (llvm::Optional<bool> Parsed = parseBool(Scalar)) {
  770. Val = *Parsed;
  771. return StringRef();
  772. }
  773. return "invalid boolean";
  774. }
  775. void ScalarTraits<StringRef>::output(const StringRef &Val, void *,
  776. raw_ostream &Out) {
  777. Out << Val;
  778. }
  779. StringRef ScalarTraits<StringRef>::input(StringRef Scalar, void *,
  780. StringRef &Val) {
  781. Val = Scalar;
  782. return StringRef();
  783. }
  784. void ScalarTraits<std::string>::output(const std::string &Val, void *,
  785. raw_ostream &Out) {
  786. Out << Val;
  787. }
  788. StringRef ScalarTraits<std::string>::input(StringRef Scalar, void *,
  789. std::string &Val) {
  790. Val = Scalar.str();
  791. return StringRef();
  792. }
  793. void ScalarTraits<uint8_t>::output(const uint8_t &Val, void *,
  794. raw_ostream &Out) {
  795. // use temp uin32_t because ostream thinks uint8_t is a character
  796. uint32_t Num = Val;
  797. Out << Num;
  798. }
  799. StringRef ScalarTraits<uint8_t>::input(StringRef Scalar, void *, uint8_t &Val) {
  800. unsigned long long n;
  801. if (getAsUnsignedInteger(Scalar, 0, n))
  802. return "invalid number";
  803. if (n > 0xFF)
  804. return "out of range number";
  805. Val = n;
  806. return StringRef();
  807. }
  808. void ScalarTraits<uint16_t>::output(const uint16_t &Val, void *,
  809. raw_ostream &Out) {
  810. Out << Val;
  811. }
  812. StringRef ScalarTraits<uint16_t>::input(StringRef Scalar, void *,
  813. uint16_t &Val) {
  814. unsigned long long n;
  815. if (getAsUnsignedInteger(Scalar, 0, n))
  816. return "invalid number";
  817. if (n > 0xFFFF)
  818. return "out of range number";
  819. Val = n;
  820. return StringRef();
  821. }
  822. void ScalarTraits<uint32_t>::output(const uint32_t &Val, void *,
  823. raw_ostream &Out) {
  824. Out << Val;
  825. }
  826. StringRef ScalarTraits<uint32_t>::input(StringRef Scalar, void *,
  827. uint32_t &Val) {
  828. unsigned long long n;
  829. if (getAsUnsignedInteger(Scalar, 0, n))
  830. return "invalid number";
  831. if (n > 0xFFFFFFFFUL)
  832. return "out of range number";
  833. Val = n;
  834. return StringRef();
  835. }
  836. void ScalarTraits<uint64_t>::output(const uint64_t &Val, void *,
  837. raw_ostream &Out) {
  838. Out << Val;
  839. }
  840. StringRef ScalarTraits<uint64_t>::input(StringRef Scalar, void *,
  841. uint64_t &Val) {
  842. unsigned long long N;
  843. if (getAsUnsignedInteger(Scalar, 0, N))
  844. return "invalid number";
  845. Val = N;
  846. return StringRef();
  847. }
  848. void ScalarTraits<int8_t>::output(const int8_t &Val, void *, raw_ostream &Out) {
  849. // use temp in32_t because ostream thinks int8_t is a character
  850. int32_t Num = Val;
  851. Out << Num;
  852. }
  853. StringRef ScalarTraits<int8_t>::input(StringRef Scalar, void *, int8_t &Val) {
  854. long long N;
  855. if (getAsSignedInteger(Scalar, 0, N))
  856. return "invalid number";
  857. if ((N > 127) || (N < -128))
  858. return "out of range number";
  859. Val = N;
  860. return StringRef();
  861. }
  862. void ScalarTraits<int16_t>::output(const int16_t &Val, void *,
  863. raw_ostream &Out) {
  864. Out << Val;
  865. }
  866. StringRef ScalarTraits<int16_t>::input(StringRef Scalar, void *, int16_t &Val) {
  867. long long N;
  868. if (getAsSignedInteger(Scalar, 0, N))
  869. return "invalid number";
  870. if ((N > INT16_MAX) || (N < INT16_MIN))
  871. return "out of range number";
  872. Val = N;
  873. return StringRef();
  874. }
  875. void ScalarTraits<int32_t>::output(const int32_t &Val, void *,
  876. raw_ostream &Out) {
  877. Out << Val;
  878. }
  879. StringRef ScalarTraits<int32_t>::input(StringRef Scalar, void *, int32_t &Val) {
  880. long long N;
  881. if (getAsSignedInteger(Scalar, 0, N))
  882. return "invalid number";
  883. if ((N > INT32_MAX) || (N < INT32_MIN))
  884. return "out of range number";
  885. Val = N;
  886. return StringRef();
  887. }
  888. void ScalarTraits<int64_t>::output(const int64_t &Val, void *,
  889. raw_ostream &Out) {
  890. Out << Val;
  891. }
  892. StringRef ScalarTraits<int64_t>::input(StringRef Scalar, void *, int64_t &Val) {
  893. long long N;
  894. if (getAsSignedInteger(Scalar, 0, N))
  895. return "invalid number";
  896. Val = N;
  897. return StringRef();
  898. }
  899. void ScalarTraits<double>::output(const double &Val, void *, raw_ostream &Out) {
  900. Out << format("%g", Val);
  901. }
  902. StringRef ScalarTraits<double>::input(StringRef Scalar, void *, double &Val) {
  903. if (to_float(Scalar, Val))
  904. return StringRef();
  905. return "invalid floating point number";
  906. }
  907. void ScalarTraits<float>::output(const float &Val, void *, raw_ostream &Out) {
  908. Out << format("%g", Val);
  909. }
  910. StringRef ScalarTraits<float>::input(StringRef Scalar, void *, float &Val) {
  911. if (to_float(Scalar, Val))
  912. return StringRef();
  913. return "invalid floating point number";
  914. }
  915. void ScalarTraits<Hex8>::output(const Hex8 &Val, void *, raw_ostream &Out) {
  916. Out << format("0x%" PRIX8, (uint8_t)Val);
  917. }
  918. StringRef ScalarTraits<Hex8>::input(StringRef Scalar, void *, Hex8 &Val) {
  919. unsigned long long n;
  920. if (getAsUnsignedInteger(Scalar, 0, n))
  921. return "invalid hex8 number";
  922. if (n > 0xFF)
  923. return "out of range hex8 number";
  924. Val = n;
  925. return StringRef();
  926. }
  927. void ScalarTraits<Hex16>::output(const Hex16 &Val, void *, raw_ostream &Out) {
  928. Out << format("0x%" PRIX16, (uint16_t)Val);
  929. }
  930. StringRef ScalarTraits<Hex16>::input(StringRef Scalar, void *, Hex16 &Val) {
  931. unsigned long long n;
  932. if (getAsUnsignedInteger(Scalar, 0, n))
  933. return "invalid hex16 number";
  934. if (n > 0xFFFF)
  935. return "out of range hex16 number";
  936. Val = n;
  937. return StringRef();
  938. }
  939. void ScalarTraits<Hex32>::output(const Hex32 &Val, void *, raw_ostream &Out) {
  940. Out << format("0x%" PRIX32, (uint32_t)Val);
  941. }
  942. StringRef ScalarTraits<Hex32>::input(StringRef Scalar, void *, Hex32 &Val) {
  943. unsigned long long n;
  944. if (getAsUnsignedInteger(Scalar, 0, n))
  945. return "invalid hex32 number";
  946. if (n > 0xFFFFFFFFUL)
  947. return "out of range hex32 number";
  948. Val = n;
  949. return StringRef();
  950. }
  951. void ScalarTraits<Hex64>::output(const Hex64 &Val, void *, raw_ostream &Out) {
  952. Out << format("0x%" PRIX64, (uint64_t)Val);
  953. }
  954. StringRef ScalarTraits<Hex64>::input(StringRef Scalar, void *, Hex64 &Val) {
  955. unsigned long long Num;
  956. if (getAsUnsignedInteger(Scalar, 0, Num))
  957. return "invalid hex64 number";
  958. Val = Num;
  959. return StringRef();
  960. }
  961. void ScalarTraits<VersionTuple>::output(const VersionTuple &Val, void *,
  962. llvm::raw_ostream &Out) {
  963. Out << Val.getAsString();
  964. }
  965. StringRef ScalarTraits<VersionTuple>::input(StringRef Scalar, void *,
  966. VersionTuple &Val) {
  967. if (Val.tryParse(Scalar))
  968. return "invalid version format";
  969. return StringRef();
  970. }