llvm.org GIT mirror llvm / 325ccf1
[YAMLTraits] Add filename support to yaml::Input Summary: The current yaml::Input constructor takes a StringRef of data as its first parameter, discarding any filename information that may have been present when a YAML file was opened. Add an alterate yaml::Input constructor that takes a MemoryBufferRef, which can have a filename associated with it. This leads to clearer diagnostic messages. Sponsored By: DARPA, AFRL Reviewed By: arphaman Differential Revision: https://reviews.llvm.org/D35398 Patch by: Jonathan Anderson (trombonehero) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308172 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Bradbury 3 years ago
3 changed file(s) with 28 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11131113 void *Ctxt = nullptr,
11141114 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
11151115 void *DiagHandlerCtxt = nullptr);
1116 Input(MemoryBufferRef Input,
1117 void *Ctxt = nullptr,
1118 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
1119 void *DiagHandlerCtxt = nullptr);
11161120 ~Input() override;
11171121
11181122 // Check if there was an syntax or semantic error during parsing.
5959 DocIterator = Strm->begin();
6060 }
6161
62 Input::Input(MemoryBufferRef Input, void *Ctxt,
63 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
64 : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) {
65 if (DiagHandler)
66 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
67 DocIterator = Strm->begin();
68 }
69
6270 Input::~Input() = default;
6371
6472 std::error_code Input::error() { return EC; }
229229 EXPECT_EQ(map2.foo, 257);
230230 EXPECT_EQ(map2.bar, 0);
231231 }
232 }
233
234 //
235 // Test YAML filename handling.
236 //
237 static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) {
238 EXPECT_EQ(Error.getFilename(), "foo.yaml");
239 }
240
241 TEST(YAMLIO, TestGivenFilename) {
242 auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml");
243 Input yin(*Buffer, nullptr, testErrorFilename);
244 FooBar Value;
245 yin >> Value;
246
247 EXPECT_TRUE(!!yin.error());
232248 }
233249
234250