1 //===- lib/Support/YAMLTraits.cpp -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/YAMLTraits.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/LineIterator.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/VersionTuple.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Support/raw_ostream.h"
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 IO::IO(void *Context
) : Ctxt(Context
) {}
42 void *IO::getContext() const {
46 void IO::setContext(void *Context
) {
50 void IO::setAllowUnknownKeys(bool Allow
) {
51 llvm_unreachable("Only supported for Input");
54 //===----------------------------------------------------------------------===//
56 //===----------------------------------------------------------------------===//
58 Input::Input(StringRef InputContent
, void *Ctxt
,
59 SourceMgr::DiagHandlerTy DiagHandler
, void *DiagHandlerCtxt
)
60 : IO(Ctxt
), Strm(new Stream(InputContent
, SrcMgr
, false, &EC
)) {
62 SrcMgr
.setDiagHandler(DiagHandler
, DiagHandlerCtxt
);
63 DocIterator
= Strm
->begin();
66 Input::Input(MemoryBufferRef Input
, void *Ctxt
,
67 SourceMgr::DiagHandlerTy DiagHandler
, void *DiagHandlerCtxt
)
68 : IO(Ctxt
), Strm(new Stream(Input
, SrcMgr
, false, &EC
)) {
70 SrcMgr
.setDiagHandler(DiagHandler
, DiagHandlerCtxt
);
71 DocIterator
= Strm
->begin();
74 Input::~Input() = default;
76 std::error_code
Input::error() { return EC
; }
78 bool Input::outputting() const {
82 bool Input::setCurrentDocument() {
83 if (DocIterator
!= Strm
->end()) {
84 Node
*N
= DocIterator
->getRoot();
86 EC
= make_error_code(errc::invalid_argument
);
90 if (isa
<NullNode
>(N
)) {
91 // Empty files are allowed and ignored
93 return setCurrentDocument();
95 releaseHNodeBuffers();
96 TopNode
= createHNodes(N
);
97 CurrentNode
= TopNode
;
103 bool Input::nextDocument() {
104 return ++DocIterator
!= Strm
->end();
107 const Node
*Input::getCurrentNode() const {
108 return CurrentNode
? CurrentNode
->_node
: nullptr;
111 bool Input::mapTag(StringRef Tag
, bool Default
) {
112 // CurrentNode can be null if setCurrentDocument() was unable to
113 // parse the document because it was invalid or empty.
117 std::string foundTag
= CurrentNode
->_node
->getVerbatimTag();
118 if (foundTag
.empty()) {
119 // If no tag found and 'Tag' is the default, say it was found.
122 // Return true iff found tag matches supplied tag.
123 return Tag
.equals(foundTag
);
126 void Input::beginMapping() {
129 // CurrentNode can be null if the document is empty.
130 MapHNode
*MN
= dyn_cast_or_null
<MapHNode
>(CurrentNode
);
132 MN
->ValidKeys
.clear();
136 std::vector
<StringRef
> Input::keys() {
137 MapHNode
*MN
= dyn_cast
<MapHNode
>(CurrentNode
);
138 std::vector
<StringRef
> Ret
;
140 setError(CurrentNode
, "not a mapping");
143 for (auto &P
: MN
->Mapping
)
144 Ret
.push_back(P
.first());
148 bool Input::preflightKey(const char *Key
, bool Required
, bool, bool &UseDefault
,
154 // CurrentNode is null for empty documents, which is an error in case required
155 // nodes are present.
158 EC
= make_error_code(errc::invalid_argument
);
162 MapHNode
*MN
= dyn_cast
<MapHNode
>(CurrentNode
);
164 if (Required
|| !isa
<EmptyHNode
>(CurrentNode
))
165 setError(CurrentNode
, "not a mapping");
170 MN
->ValidKeys
.push_back(Key
);
171 HNode
*Value
= MN
->Mapping
[Key
].first
;
174 setError(CurrentNode
, Twine("missing required key '") + Key
+ "'");
179 SaveInfo
= CurrentNode
;
184 void Input::postflightKey(void *saveInfo
) {
185 CurrentNode
= reinterpret_cast<HNode
*>(saveInfo
);
188 void Input::endMapping() {
191 // CurrentNode can be null if the document is empty.
192 MapHNode
*MN
= dyn_cast_or_null
<MapHNode
>(CurrentNode
);
195 for (const auto &NN
: MN
->Mapping
) {
196 if (!is_contained(MN
->ValidKeys
, NN
.first())) {
197 const SMRange
&ReportLoc
= NN
.second
.second
;
198 if (!AllowUnknownKeys
) {
199 setError(ReportLoc
, Twine("unknown key '") + NN
.first() + "'");
202 reportWarning(ReportLoc
, Twine("unknown key '") + NN
.first() + "'");
207 void Input::beginFlowMapping() { beginMapping(); }
209 void Input::endFlowMapping() { endMapping(); }
211 unsigned Input::beginSequence() {
212 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
))
213 return SQ
->Entries
.size();
214 if (isa
<EmptyHNode
>(CurrentNode
))
216 // Treat case where there's a scalar "null" value as an empty sequence.
217 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(CurrentNode
)) {
218 if (isNull(SN
->value()))
221 // Any other type of HNode is an error.
222 setError(CurrentNode
, "not a sequence");
226 void Input::endSequence() {
229 bool Input::preflightElement(unsigned Index
, void *&SaveInfo
) {
232 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
233 SaveInfo
= CurrentNode
;
234 CurrentNode
= SQ
->Entries
[Index
];
240 void Input::postflightElement(void *SaveInfo
) {
241 CurrentNode
= reinterpret_cast<HNode
*>(SaveInfo
);
244 unsigned Input::beginFlowSequence() { return beginSequence(); }
246 bool Input::preflightFlowElement(unsigned index
, void *&SaveInfo
) {
249 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
250 SaveInfo
= CurrentNode
;
251 CurrentNode
= SQ
->Entries
[index
];
257 void Input::postflightFlowElement(void *SaveInfo
) {
258 CurrentNode
= reinterpret_cast<HNode
*>(SaveInfo
);
261 void Input::endFlowSequence() {
264 void Input::beginEnumScalar() {
265 ScalarMatchFound
= false;
268 bool Input::matchEnumScalar(const char *Str
, bool) {
269 if (ScalarMatchFound
)
271 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(CurrentNode
)) {
272 if (SN
->value().equals(Str
)) {
273 ScalarMatchFound
= true;
280 bool Input::matchEnumFallback() {
281 if (ScalarMatchFound
)
283 ScalarMatchFound
= true;
287 void Input::endEnumScalar() {
288 if (!ScalarMatchFound
) {
289 setError(CurrentNode
, "unknown enumerated scalar");
293 bool Input::beginBitSetScalar(bool &DoClear
) {
294 BitValuesUsed
.clear();
295 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
296 BitValuesUsed
.resize(SQ
->Entries
.size());
298 setError(CurrentNode
, "expected sequence of bit values");
304 bool Input::bitSetMatch(const char *Str
, bool) {
307 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
309 for (auto &N
: SQ
->Entries
) {
310 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(N
)) {
311 if (SN
->value().equals(Str
)) {
312 BitValuesUsed
[Index
] = true;
316 setError(CurrentNode
, "unexpected scalar in sequence of bit values");
321 setError(CurrentNode
, "expected sequence of bit values");
326 void Input::endBitSetScalar() {
329 if (SequenceHNode
*SQ
= dyn_cast
<SequenceHNode
>(CurrentNode
)) {
330 assert(BitValuesUsed
.size() == SQ
->Entries
.size());
331 for (unsigned i
= 0; i
< SQ
->Entries
.size(); ++i
) {
332 if (!BitValuesUsed
[i
]) {
333 setError(SQ
->Entries
[i
], "unknown bit value");
340 void Input::scalarString(StringRef
&S
, QuotingType
) {
341 if (ScalarHNode
*SN
= dyn_cast
<ScalarHNode
>(CurrentNode
)) {
344 setError(CurrentNode
, "unexpected scalar");
348 void Input::blockScalarString(StringRef
&S
) { scalarString(S
, QuotingType::None
); }
350 void Input::scalarTag(std::string
&Tag
) {
351 Tag
= CurrentNode
->_node
->getVerbatimTag();
354 void Input::setError(HNode
*hnode
, const Twine
&message
) {
355 assert(hnode
&& "HNode must not be NULL");
356 setError(hnode
->_node
, message
);
359 NodeKind
Input::getNodeKind() {
360 if (isa
<ScalarHNode
>(CurrentNode
))
361 return NodeKind::Scalar
;
362 else if (isa
<MapHNode
>(CurrentNode
))
363 return NodeKind::Map
;
364 else if (isa
<SequenceHNode
>(CurrentNode
))
365 return NodeKind::Sequence
;
366 llvm_unreachable("Unsupported node kind");
369 void Input::setError(Node
*node
, const Twine
&message
) {
370 Strm
->printError(node
, message
);
371 EC
= make_error_code(errc::invalid_argument
);
374 void Input::setError(const SMRange
&range
, const Twine
&message
) {
375 Strm
->printError(range
, message
);
376 EC
= make_error_code(errc::invalid_argument
);
379 void Input::reportWarning(HNode
*hnode
, const Twine
&message
) {
380 assert(hnode
&& "HNode must not be NULL");
381 Strm
->printError(hnode
->_node
, message
, SourceMgr::DK_Warning
);
384 void Input::reportWarning(Node
*node
, const Twine
&message
) {
385 Strm
->printError(node
, message
, SourceMgr::DK_Warning
);
388 void Input::reportWarning(const SMRange
&range
, const Twine
&message
) {
389 Strm
->printError(range
, message
, SourceMgr::DK_Warning
);
392 void Input::releaseHNodeBuffers() {
393 EmptyHNodeAllocator
.DestroyAll();
394 ScalarHNodeAllocator
.DestroyAll();
395 SequenceHNodeAllocator
.DestroyAll();
396 MapHNodeAllocator
.DestroyAll();
399 Input::HNode
*Input::createHNodes(Node
*N
) {
400 SmallString
<128> StringStorage
;
401 switch (N
->getType()) {
402 case Node::NK_Scalar
: {
403 ScalarNode
*SN
= dyn_cast
<ScalarNode
>(N
);
404 StringRef KeyStr
= SN
->getValue(StringStorage
);
405 if (!StringStorage
.empty()) {
406 // Copy string to permanent storage
407 KeyStr
= StringStorage
.str().copy(StringAllocator
);
409 return new (ScalarHNodeAllocator
.Allocate()) ScalarHNode(N
, KeyStr
);
411 case Node::NK_BlockScalar
: {
412 BlockScalarNode
*BSN
= dyn_cast
<BlockScalarNode
>(N
);
413 StringRef ValueCopy
= BSN
->getValue().copy(StringAllocator
);
414 return new (ScalarHNodeAllocator
.Allocate()) ScalarHNode(N
, ValueCopy
);
416 case Node::NK_Sequence
: {
417 SequenceNode
*SQ
= dyn_cast
<SequenceNode
>(N
);
418 auto SQHNode
= new (SequenceHNodeAllocator
.Allocate()) SequenceHNode(N
);
419 for (Node
&SN
: *SQ
) {
420 auto Entry
= createHNodes(&SN
);
423 SQHNode
->Entries
.push_back(Entry
);
427 case Node::NK_Mapping
: {
428 MappingNode
*Map
= dyn_cast
<MappingNode
>(N
);
429 auto mapHNode
= new (MapHNodeAllocator
.Allocate()) MapHNode(N
);
430 for (KeyValueNode
&KVN
: *Map
) {
431 Node
*KeyNode
= KVN
.getKey();
432 ScalarNode
*Key
= dyn_cast_or_null
<ScalarNode
>(KeyNode
);
433 Node
*Value
= KVN
.getValue();
434 if (!Key
|| !Value
) {
436 setError(KeyNode
, "Map key must be a scalar");
438 setError(KeyNode
, "Map value must not be empty");
441 StringStorage
.clear();
442 StringRef KeyStr
= Key
->getValue(StringStorage
);
443 if (!StringStorage
.empty()) {
444 // Copy string to permanent storage
445 KeyStr
= StringStorage
.str().copy(StringAllocator
);
447 if (mapHNode
->Mapping
.count(KeyStr
))
448 // From YAML spec: "The content of a mapping node is an unordered set of
449 // key/value node pairs, with the restriction that each of the keys is
451 setError(KeyNode
, Twine("duplicated mapping key '") + KeyStr
+ "'");
452 auto ValueHNode
= createHNodes(Value
);
455 mapHNode
->Mapping
[KeyStr
] =
456 std::make_pair(std::move(ValueHNode
), KeyNode
->getSourceRange());
458 return std::move(mapHNode
);
461 return new (EmptyHNodeAllocator
.Allocate()) EmptyHNode(N
);
463 setError(N
, "unknown node kind");
468 void Input::setError(const Twine
&Message
) {
469 setError(CurrentNode
, Message
);
472 void Input::setAllowUnknownKeys(bool Allow
) { AllowUnknownKeys
= Allow
; }
474 bool Input::canElideEmptySequence() {
478 //===----------------------------------------------------------------------===//
480 //===----------------------------------------------------------------------===//
482 Output::Output(raw_ostream
&yout
, void *context
, int WrapColumn
)
483 : IO(context
), Out(yout
), WrapColumn(WrapColumn
) {}
485 Output::~Output() = default;
487 bool Output::outputting() const {
491 void Output::beginMapping() {
492 StateStack
.push_back(inMapFirstKey
);
493 PaddingBeforeContainer
= Padding
;
497 bool Output::mapTag(StringRef Tag
, bool Use
) {
499 // If this tag is being written inside a sequence we should write the start
500 // of the sequence before writing the tag, otherwise the tag won't be
501 // attached to the element in the sequence, but rather the sequence itself.
502 bool SequenceElement
= false;
503 if (StateStack
.size() > 1) {
504 auto &E
= StateStack
[StateStack
.size() - 2];
505 SequenceElement
= inSeqAnyElement(E
) || inFlowSeqAnyElement(E
);
507 if (SequenceElement
&& StateStack
.back() == inMapFirstKey
) {
513 if (SequenceElement
) {
514 // If we're writing the tag during the first element of a map, the tag
515 // takes the place of the first element in the sequence.
516 if (StateStack
.back() == inMapFirstKey
) {
517 StateStack
.pop_back();
518 StateStack
.push_back(inMapOtherKey
);
520 // Tags inside maps in sequences should act as keys in the map from a
521 // formatting perspective, so we always want a newline in a sequence.
528 void Output::endMapping() {
529 // If we did not map anything, we should explicitly emit an empty map
530 if (StateStack
.back() == inMapFirstKey
) {
531 Padding
= PaddingBeforeContainer
;
536 StateStack
.pop_back();
539 std::vector
<StringRef
> Output::keys() {
540 report_fatal_error("invalid call");
543 bool Output::preflightKey(const char *Key
, bool Required
, bool SameAsDefault
,
544 bool &UseDefault
, void *&SaveInfo
) {
547 if (Required
|| !SameAsDefault
|| WriteDefaultValues
) {
548 auto State
= StateStack
.back();
549 if (State
== inFlowMapFirstKey
|| State
== inFlowMapOtherKey
) {
560 void Output::postflightKey(void *) {
561 if (StateStack
.back() == inMapFirstKey
) {
562 StateStack
.pop_back();
563 StateStack
.push_back(inMapOtherKey
);
564 } else if (StateStack
.back() == inFlowMapFirstKey
) {
565 StateStack
.pop_back();
566 StateStack
.push_back(inFlowMapOtherKey
);
570 void Output::beginFlowMapping() {
571 StateStack
.push_back(inFlowMapFirstKey
);
573 ColumnAtMapFlowStart
= Column
;
577 void Output::endFlowMapping() {
578 StateStack
.pop_back();
579 outputUpToEndOfLine(" }");
582 void Output::beginDocuments() {
583 outputUpToEndOfLine("---");
586 bool Output::preflightDocument(unsigned index
) {
588 outputUpToEndOfLine("\n---");
592 void Output::postflightDocument() {
595 void Output::endDocuments() {
599 unsigned Output::beginSequence() {
600 StateStack
.push_back(inSeqFirstElement
);
601 PaddingBeforeContainer
= Padding
;
606 void Output::endSequence() {
607 // If we did not emit anything, we should explicitly emit an empty sequence
608 if (StateStack
.back() == inSeqFirstElement
) {
609 Padding
= PaddingBeforeContainer
;
610 newLineCheck(/*EmptySequence=*/true);
614 StateStack
.pop_back();
617 bool Output::preflightElement(unsigned, void *&SaveInfo
) {
622 void Output::postflightElement(void *) {
623 if (StateStack
.back() == inSeqFirstElement
) {
624 StateStack
.pop_back();
625 StateStack
.push_back(inSeqOtherElement
);
626 } else if (StateStack
.back() == inFlowSeqFirstElement
) {
627 StateStack
.pop_back();
628 StateStack
.push_back(inFlowSeqOtherElement
);
632 unsigned Output::beginFlowSequence() {
633 StateStack
.push_back(inFlowSeqFirstElement
);
635 ColumnAtFlowStart
= Column
;
637 NeedFlowSequenceComma
= false;
641 void Output::endFlowSequence() {
642 StateStack
.pop_back();
643 outputUpToEndOfLine(" ]");
646 bool Output::preflightFlowElement(unsigned, void *&SaveInfo
) {
647 if (NeedFlowSequenceComma
)
649 if (WrapColumn
&& Column
> WrapColumn
) {
651 for (int i
= 0; i
< ColumnAtFlowStart
; ++i
)
653 Column
= ColumnAtFlowStart
;
660 void Output::postflightFlowElement(void *) {
661 NeedFlowSequenceComma
= true;
664 void Output::beginEnumScalar() {
665 EnumerationMatchFound
= false;
668 bool Output::matchEnumScalar(const char *Str
, bool Match
) {
669 if (Match
&& !EnumerationMatchFound
) {
671 outputUpToEndOfLine(Str
);
672 EnumerationMatchFound
= true;
677 bool Output::matchEnumFallback() {
678 if (EnumerationMatchFound
)
680 EnumerationMatchFound
= true;
684 void Output::endEnumScalar() {
685 if (!EnumerationMatchFound
)
686 llvm_unreachable("bad runtime enum value");
689 bool Output::beginBitSetScalar(bool &DoClear
) {
692 NeedBitValueComma
= false;
697 bool Output::bitSetMatch(const char *Str
, bool Matches
) {
699 if (NeedBitValueComma
)
702 NeedBitValueComma
= true;
707 void Output::endBitSetScalar() {
708 outputUpToEndOfLine(" ]");
711 void Output::scalarString(StringRef
&S
, QuotingType MustQuote
) {
714 // Print '' for the empty string because leaving the field empty is not
716 outputUpToEndOfLine("''");
719 if (MustQuote
== QuotingType::None
) {
720 // Only quote if we must.
721 outputUpToEndOfLine(S
);
725 const char *const Quote
= MustQuote
== QuotingType::Single
? "'" : "\"";
726 output(Quote
); // Starting quote.
728 // When using double-quoted strings (and only in that case), non-printable characters may be
729 // present, and will be escaped using a variety of unicode-scalar and special short-form
730 // escapes. This is handled in yaml::escape.
731 if (MustQuote
== QuotingType::Double
) {
732 output(yaml::escape(S
, /* EscapePrintable= */ false));
733 outputUpToEndOfLine(Quote
);
739 unsigned End
= S
.size();
740 const char *Base
= S
.data();
742 // When using single-quoted strings, any single quote ' must be doubled to be escaped.
744 if (S
[j
] == '\'') { // Escape quotes.
745 output(StringRef(&Base
[i
], j
- i
)); // "flush".
746 output(StringLiteral("''")); // Print it as ''
751 output(StringRef(&Base
[i
], j
- i
));
752 outputUpToEndOfLine(Quote
); // Ending quote.
755 void Output::blockScalarString(StringRef
&S
) {
756 if (!StateStack
.empty())
761 unsigned Indent
= StateStack
.empty() ? 1 : StateStack
.size();
763 auto Buffer
= MemoryBuffer::getMemBuffer(S
, "", false);
764 for (line_iterator
Lines(*Buffer
, false); !Lines
.is_at_end(); ++Lines
) {
765 for (unsigned I
= 0; I
< Indent
; ++I
) {
773 void Output::scalarTag(std::string
&Tag
) {
781 void Output::setError(const Twine
&message
) {
784 bool Output::canElideEmptySequence() {
785 // Normally, with an optional key/value where the value is an empty sequence,
786 // the whole key/value can be not written. But, that produces wrong yaml
787 // if the key/value is the only thing in the map and the map is used in
788 // a sequence. This detects if the this sequence is the first key/value
789 // in map that itself is embedded in a sequence.
790 if (StateStack
.size() < 2)
792 if (StateStack
.back() != inMapFirstKey
)
794 return !inSeqAnyElement(StateStack
[StateStack
.size() - 2]);
797 void Output::output(StringRef s
) {
802 void Output::outputUpToEndOfLine(StringRef s
) {
804 if (StateStack
.empty() || (!inFlowSeqAnyElement(StateStack
.back()) &&
805 !inFlowMapAnyKey(StateStack
.back())))
809 void Output::outputNewLine() {
814 // if seq at top, indent as if map, then add "- "
815 // if seq in middle, use "- " if firstKey, else use " "
818 void Output::newLineCheck(bool EmptySequence
) {
819 if (Padding
!= "\n") {
827 if (StateStack
.size() == 0 || EmptySequence
)
830 unsigned Indent
= StateStack
.size() - 1;
831 bool OutputDash
= false;
833 if (StateStack
.back() == inSeqFirstElement
||
834 StateStack
.back() == inSeqOtherElement
) {
836 } else if ((StateStack
.size() > 1) &&
837 ((StateStack
.back() == inMapFirstKey
) ||
838 inFlowSeqAnyElement(StateStack
.back()) ||
839 (StateStack
.back() == inFlowMapFirstKey
)) &&
840 inSeqAnyElement(StateStack
[StateStack
.size() - 2])) {
845 for (unsigned i
= 0; i
< Indent
; ++i
) {
853 void Output::paddedKey(StringRef key
) {
856 const char *spaces
= " ";
857 if (key
.size() < strlen(spaces
))
858 Padding
= &spaces
[key
.size()];
863 void Output::flowKey(StringRef Key
) {
864 if (StateStack
.back() == inFlowMapOtherKey
)
866 if (WrapColumn
&& Column
> WrapColumn
) {
868 for (int I
= 0; I
< ColumnAtMapFlowStart
; ++I
)
870 Column
= ColumnAtMapFlowStart
;
877 NodeKind
Output::getNodeKind() { report_fatal_error("invalid call"); }
879 bool Output::inSeqAnyElement(InState State
) {
880 return State
== inSeqFirstElement
|| State
== inSeqOtherElement
;
883 bool Output::inFlowSeqAnyElement(InState State
) {
884 return State
== inFlowSeqFirstElement
|| State
== inFlowSeqOtherElement
;
887 bool Output::inMapAnyKey(InState State
) {
888 return State
== inMapFirstKey
|| State
== inMapOtherKey
;
891 bool Output::inFlowMapAnyKey(InState State
) {
892 return State
== inFlowMapFirstKey
|| State
== inFlowMapOtherKey
;
895 //===----------------------------------------------------------------------===//
896 // traits for built-in types
897 //===----------------------------------------------------------------------===//
899 void ScalarTraits
<bool>::output(const bool &Val
, void *, raw_ostream
&Out
) {
900 Out
<< (Val
? "true" : "false");
903 StringRef ScalarTraits
<bool>::input(StringRef Scalar
, void *, bool &Val
) {
904 if (std::optional
<bool> Parsed
= parseBool(Scalar
)) {
908 return "invalid boolean";
911 void ScalarTraits
<StringRef
>::output(const StringRef
&Val
, void *,
916 StringRef ScalarTraits
<StringRef
>::input(StringRef Scalar
, void *,
922 void ScalarTraits
<std::string
>::output(const std::string
&Val
, void *,
927 StringRef ScalarTraits
<std::string
>::input(StringRef Scalar
, void *,
933 void ScalarTraits
<uint8_t>::output(const uint8_t &Val
, void *,
935 // use temp uin32_t because ostream thinks uint8_t is a character
940 StringRef ScalarTraits
<uint8_t>::input(StringRef Scalar
, void *, uint8_t &Val
) {
941 unsigned long long n
;
942 if (getAsUnsignedInteger(Scalar
, 0, n
))
943 return "invalid number";
945 return "out of range number";
950 void ScalarTraits
<uint16_t>::output(const uint16_t &Val
, void *,
955 StringRef ScalarTraits
<uint16_t>::input(StringRef Scalar
, void *,
957 unsigned long long n
;
958 if (getAsUnsignedInteger(Scalar
, 0, n
))
959 return "invalid number";
961 return "out of range number";
966 void ScalarTraits
<uint32_t>::output(const uint32_t &Val
, void *,
971 StringRef ScalarTraits
<uint32_t>::input(StringRef Scalar
, void *,
973 unsigned long long n
;
974 if (getAsUnsignedInteger(Scalar
, 0, n
))
975 return "invalid number";
976 if (n
> 0xFFFFFFFFUL
)
977 return "out of range number";
982 void ScalarTraits
<uint64_t>::output(const uint64_t &Val
, void *,
987 StringRef ScalarTraits
<uint64_t>::input(StringRef Scalar
, void *,
989 unsigned long long N
;
990 if (getAsUnsignedInteger(Scalar
, 0, N
))
991 return "invalid number";
996 void ScalarTraits
<int8_t>::output(const int8_t &Val
, void *, raw_ostream
&Out
) {
997 // use temp in32_t because ostream thinks int8_t is a character
1002 StringRef ScalarTraits
<int8_t>::input(StringRef Scalar
, void *, int8_t &Val
) {
1004 if (getAsSignedInteger(Scalar
, 0, N
))
1005 return "invalid number";
1006 if ((N
> 127) || (N
< -128))
1007 return "out of range number";
1012 void ScalarTraits
<int16_t>::output(const int16_t &Val
, void *,
1017 StringRef ScalarTraits
<int16_t>::input(StringRef Scalar
, void *, int16_t &Val
) {
1019 if (getAsSignedInteger(Scalar
, 0, N
))
1020 return "invalid number";
1021 if ((N
> INT16_MAX
) || (N
< INT16_MIN
))
1022 return "out of range number";
1027 void ScalarTraits
<int32_t>::output(const int32_t &Val
, void *,
1032 StringRef ScalarTraits
<int32_t>::input(StringRef Scalar
, void *, int32_t &Val
) {
1034 if (getAsSignedInteger(Scalar
, 0, N
))
1035 return "invalid number";
1036 if ((N
> INT32_MAX
) || (N
< INT32_MIN
))
1037 return "out of range number";
1042 void ScalarTraits
<int64_t>::output(const int64_t &Val
, void *,
1047 StringRef ScalarTraits
<int64_t>::input(StringRef Scalar
, void *, int64_t &Val
) {
1049 if (getAsSignedInteger(Scalar
, 0, N
))
1050 return "invalid number";
1055 void ScalarTraits
<double>::output(const double &Val
, void *, raw_ostream
&Out
) {
1056 Out
<< format("%g", Val
);
1059 StringRef ScalarTraits
<double>::input(StringRef Scalar
, void *, double &Val
) {
1060 if (to_float(Scalar
, Val
))
1062 return "invalid floating point number";
1065 void ScalarTraits
<float>::output(const float &Val
, void *, raw_ostream
&Out
) {
1066 Out
<< format("%g", Val
);
1069 StringRef ScalarTraits
<float>::input(StringRef Scalar
, void *, float &Val
) {
1070 if (to_float(Scalar
, Val
))
1072 return "invalid floating point number";
1075 void ScalarTraits
<Hex8
>::output(const Hex8
&Val
, void *, raw_ostream
&Out
) {
1076 Out
<< format("0x%" PRIX8
, (uint8_t)Val
);
1079 StringRef ScalarTraits
<Hex8
>::input(StringRef Scalar
, void *, Hex8
&Val
) {
1080 unsigned long long n
;
1081 if (getAsUnsignedInteger(Scalar
, 0, n
))
1082 return "invalid hex8 number";
1084 return "out of range hex8 number";
1089 void ScalarTraits
<Hex16
>::output(const Hex16
&Val
, void *, raw_ostream
&Out
) {
1090 Out
<< format("0x%" PRIX16
, (uint16_t)Val
);
1093 StringRef ScalarTraits
<Hex16
>::input(StringRef Scalar
, void *, Hex16
&Val
) {
1094 unsigned long long n
;
1095 if (getAsUnsignedInteger(Scalar
, 0, n
))
1096 return "invalid hex16 number";
1098 return "out of range hex16 number";
1103 void ScalarTraits
<Hex32
>::output(const Hex32
&Val
, void *, raw_ostream
&Out
) {
1104 Out
<< format("0x%" PRIX32
, (uint32_t)Val
);
1107 StringRef ScalarTraits
<Hex32
>::input(StringRef Scalar
, void *, Hex32
&Val
) {
1108 unsigned long long n
;
1109 if (getAsUnsignedInteger(Scalar
, 0, n
))
1110 return "invalid hex32 number";
1111 if (n
> 0xFFFFFFFFUL
)
1112 return "out of range hex32 number";
1117 void ScalarTraits
<Hex64
>::output(const Hex64
&Val
, void *, raw_ostream
&Out
) {
1118 Out
<< format("0x%" PRIX64
, (uint64_t)Val
);
1121 StringRef ScalarTraits
<Hex64
>::input(StringRef Scalar
, void *, Hex64
&Val
) {
1122 unsigned long long Num
;
1123 if (getAsUnsignedInteger(Scalar
, 0, Num
))
1124 return "invalid hex64 number";
1129 void ScalarTraits
<VersionTuple
>::output(const VersionTuple
&Val
, void *,
1130 llvm::raw_ostream
&Out
) {
1131 Out
<< Val
.getAsString();
1134 StringRef ScalarTraits
<VersionTuple
>::input(StringRef Scalar
, void *,
1135 VersionTuple
&Val
) {
1136 if (Val
.tryParse(Scalar
))
1137 return "invalid version format";