llvm.org GIT mirror llvm / 04fca67
Add dyn_cast<> support to YAML I/O's IO class git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194655 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Kledzik 5 years ago
3 changed file(s) with 80 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
1717 #include "llvm/ADT/StringRef.h"
1818 #include "llvm/ADT/StringSwitch.h"
1919 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/Casting.h"
2021 #include "llvm/Support/Compiler.h"
2122 #include "llvm/Support/SourceMgr.h"
2223 #include "llvm/Support/YAMLParser.h"
316317 IO(void *Ctxt=NULL);
317318 virtual ~IO();
318319
319 virtual bool outputting() = 0;
320 virtual bool outputting() const = 0;
320321
321322 virtual unsigned beginSequence() = 0;
322323 virtual bool preflightElement(unsigned, void *&) = 0;
693694 // To set alternate error reporting.
694695 void setDiagHandler(llvm::SourceMgr::DiagHandlerTy Handler, void *Ctxt = 0);
695696
697 static bool classof(const IO *io) { return !io->outputting(); }
698
696699 private:
697 virtual bool outputting();
700 virtual bool outputting() const;
698701 virtual bool mapTag(StringRef, bool);
699702 virtual void beginMapping();
700703 virtual void endMapping();
818821 Output(llvm::raw_ostream &, void *Ctxt=NULL);
819822 virtual ~Output();
820823
821 virtual bool outputting();
824 static bool classof(const IO *io) { return io->outputting(); }
825
826 virtual bool outputting() const;
822827 virtual bool mapTag(StringRef, bool);
823828 virtual void beginMapping();
824829 virtual void endMapping();
5858 SrcMgr.setDiagHandler(Handler, Ctxt);
5959 }
6060
61 bool Input::outputting() {
61 bool Input::outputting() const {
6262 return false;
6363 }
6464
381381 Output::~Output() {
382382 }
383383
384 bool Output::outputting() {
384 bool Output::outputting() const {
385385 return true;
386386 }
387387
10731073 }
10741074
10751075
1076 //===----------------------------------------------------------------------===//
1077 // Test dyn_cast<> on IO object
1078 //===----------------------------------------------------------------------===//
1079
1080 struct DynCast {
1081 int value;
1082 };
1083 typedef std::vector DynCastSequence;
1084
1085 LLVM_YAML_IS_SEQUENCE_VECTOR(DynCast)
1086
1087 namespace llvm {
1088 namespace yaml {
1089 template <>
1090 struct MappingTraits {
1091 static void mapping(IO &io, DynCast& info) {
1092 // Change 10 to 13 when writing yaml.
1093 if (Output *output = dyn_cast(&io)) {
1094 (void)output;
1095 if (info.value == 10)
1096 info.value = 13;
1097 }
1098 io.mapRequired("value", info.value);
1099 // Change 20 to 23 when parsing yaml.
1100 if (Input *input = dyn_cast(&io)) {
1101 (void)input;
1102 if (info.value == 20)
1103 info.value = 23;
1104 }
1105 }
1106 };
1107 }
1108 }
1109
1110 //
1111 // Test writing then reading back a sequence of mappings
1112 //
1113 TEST(YAMLIO, TestDynCast) {
1114 std::string intermediate;
1115 {
1116 DynCast entry1;
1117 entry1.value = 10;
1118 DynCast entry2;
1119 entry2.value = 20;
1120 DynCast entry3;
1121 entry3.value = 30;
1122 DynCastSequence seq;
1123 seq.push_back(entry1);
1124 seq.push_back(entry2);
1125 seq.push_back(entry3);
1126
1127 llvm::raw_string_ostream ostr(intermediate);
1128 Output yout(ostr);
1129 yout << seq;
1130 }
1131
1132 {
1133 Input yin(intermediate);
1134 DynCastSequence seq2;
1135 yin >> seq2;
1136
1137 EXPECT_FALSE(yin.error());
1138 EXPECT_EQ(seq2.size(), 3UL);
1139 EXPECT_EQ(seq2[0].value, 13); // Verify changed to 13.
1140 EXPECT_EQ(seq2[1].value, 23); // Verify changed to 23.
1141 EXPECT_EQ(seq2[2].value, 30); // Verify stays same.
1142 }
1143 }
1144
1145
10761146
10771147 //===----------------------------------------------------------------------===//
10781148 // Test error handling