llvm.org GIT mirror llvm / 43752f2
Added std::string as a built-in type for mapping. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193344 91177308-0d34-0410-b5e6-96231b3b80d8 John Thompson 5 years ago
4 changed file(s) with 50 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
233233 * float
234234 * double
235235 * StringRef
236 * std::string
236237 * int64_t
237238 * int32_t
238239 * int16_t
None //===- llvm/Supporrt/YAMLTraits.h -------------------------------*- C++ -*-===//
0 //===- llvm/Support/YAMLTraits.h -------------------------------*- C++ -*-===//
11 //
22 // The LLVM Linker
33 //
535535 struct ScalarTraits {
536536 static void output(const StringRef &, void*, llvm::raw_ostream &);
537537 static StringRef input(StringRef, void*, StringRef &);
538 };
539
540 template<>
541 struct ScalarTraits {
542 static void output(const std::string &, void*, llvm::raw_ostream &);
543 static StringRef input(StringRef, void*, std::string &);
538544 };
539545
540546 template<>
647647 return StringRef();
648648 }
649649
650 void ScalarTraits::output(const std::string &Val, void *,
651 raw_ostream &Out) {
652 Out << Val;
653 }
654
655 StringRef ScalarTraits::input(StringRef Scalar, void *,
656 std::string &Val) {
657 Val = Scalar.str();
658 return StringRef();
659 }
660
650661 void ScalarTraits::output(const uint8_t &Val, void *,
651662 raw_ostream &Out) {
652663 // use temp uin32_t because ostream thinks uint8_t is a character
129129
130130 struct BuiltInTypes {
131131 llvm::StringRef str;
132 std::string stdstr;
132133 uint64_t u64;
133134 uint32_t u32;
134135 uint16_t u16;
152153 struct MappingTraits {
153154 static void mapping(IO &io, BuiltInTypes& bt) {
154155 io.mapRequired("str", bt.str);
156 io.mapRequired("stdstr", bt.stdstr);
155157 io.mapRequired("u64", bt.u64);
156158 io.mapRequired("u32", bt.u32);
157159 io.mapRequired("u16", bt.u16);
180182 BuiltInTypes map;
181183 Input yin("---\n"
182184 "str: hello there\n"
185 "stdstr: hello where?\n"
183186 "u64: 5000000000\n"
184187 "u32: 4000000000\n"
185188 "u16: 65000\n"
200203
201204 EXPECT_FALSE(yin.error());
202205 EXPECT_TRUE(map.str.equals("hello there"));
206 EXPECT_TRUE(map.stdstr == "hello where?");
203207 EXPECT_EQ(map.u64, 5000000000ULL);
204208 EXPECT_EQ(map.u32, 4000000000U);
205209 EXPECT_EQ(map.u16, 65000);
226230 {
227231 BuiltInTypes map;
228232 map.str = "one two";
233 map.stdstr = "three four";
229234 map.u64 = 6000000000ULL;
230235 map.u32 = 3000000000U;
231236 map.u16 = 50000;
254259
255260 EXPECT_FALSE(yin.error());
256261 EXPECT_TRUE(map.str.equals("one two"));
262 EXPECT_TRUE(map.stdstr == "three four");
257263 EXPECT_EQ(map.u64, 6000000000ULL);
258264 EXPECT_EQ(map.u32, 3000000000U);
259265 EXPECT_EQ(map.u16, 50000);
278284 llvm::StringRef str3;
279285 llvm::StringRef str4;
280286 llvm::StringRef str5;
287 std::string stdstr1;
288 std::string stdstr2;
289 std::string stdstr3;
290 std::string stdstr4;
291 std::string stdstr5;
281292 };
282293
283294 namespace llvm {
290301 io.mapRequired("str3", st.str3);
291302 io.mapRequired("str4", st.str4);
292303 io.mapRequired("str5", st.str5);
304 io.mapRequired("stdstr1", st.stdstr1);
305 io.mapRequired("stdstr2", st.stdstr2);
306 io.mapRequired("stdstr3", st.stdstr3);
307 io.mapRequired("stdstr4", st.stdstr4);
308 io.mapRequired("stdstr5", st.stdstr5);
293309 }
294310 };
295311 }
304320 map.str3 = "`ccc";
305321 map.str4 = "@ddd";
306322 map.str5 = "";
323 map.stdstr1 = "'eee";
324 map.stdstr2 = "\"fff";
325 map.stdstr3 = "`ggg";
326 map.stdstr4 = "@hhh";
327 map.stdstr5 = "";
307328
308329 llvm::raw_string_ostream ostr(intermediate);
309330 Output yout(ostr);
316337 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'`ccc'"));
317338 EXPECT_NE(llvm::StringRef::npos, flowOut.find("'@ddd'"));
318339 EXPECT_NE(llvm::StringRef::npos, flowOut.find("''\n"));
340 EXPECT_NE(std::string::npos, flowOut.find("'''eee"));
341 EXPECT_NE(std::string::npos, flowOut.find("'\"fff'"));
342 EXPECT_NE(std::string::npos, flowOut.find("'`ggg'"));
343 EXPECT_NE(std::string::npos, flowOut.find("'@hhh'"));
344 EXPECT_NE(std::string::npos, flowOut.find("''\n"));
319345
320346 {
321347 Input yin(intermediate);
328354 EXPECT_TRUE(map.str3.equals("`ccc"));
329355 EXPECT_TRUE(map.str4.equals("@ddd"));
330356 EXPECT_TRUE(map.str5.equals(""));
357 EXPECT_TRUE(map.stdstr1 == "'eee");
358 EXPECT_TRUE(map.stdstr2 == "\"fff");
359 EXPECT_TRUE(map.stdstr3 == "`ggg");
360 EXPECT_TRUE(map.stdstr4 == "@hhh");
361 EXPECT_TRUE(map.stdstr5 == "");
331362 }
332363 }
333364