From 325ccf1c3d92533f8bc5c48d741d3aa23a6b2542 Mon Sep 17 00:00:00 2001 From: Alex Bradbury Date: Mon, 17 Jul 2017 11:41:30 +0000 Subject: [PATCH] [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 --- include/llvm/Support/YAMLTraits.h | 4 ++++ lib/Support/YAMLTraits.cpp | 8 ++++++++ unittests/Support/YAMLIOTest.cpp | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/include/llvm/Support/YAMLTraits.h b/include/llvm/Support/YAMLTraits.h index 15b3b11db04..71fdf47f197 100644 --- a/include/llvm/Support/YAMLTraits.h +++ b/include/llvm/Support/YAMLTraits.h @@ -1114,6 +1114,10 @@ public: void *Ctxt = nullptr, SourceMgr::DiagHandlerTy DiagHandler = nullptr, void *DiagHandlerCtxt = nullptr); + Input(MemoryBufferRef Input, + void *Ctxt = nullptr, + SourceMgr::DiagHandlerTy DiagHandler = nullptr, + void *DiagHandlerCtxt = nullptr); ~Input() override; // Check if there was an syntax or semantic error during parsing. diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 601084f9eae..65eda246a7f 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -60,6 +60,14 @@ Input::Input(StringRef InputContent, void *Ctxt, DocIterator = Strm->begin(); } +Input::Input(MemoryBufferRef Input, void *Ctxt, + SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt) + : IO(Ctxt), Strm(new Stream(Input, SrcMgr, false, &EC)) { + if (DiagHandler) + SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt); + DocIterator = Strm->begin(); +} + Input::~Input() = default; std::error_code Input::error() { return EC; } diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index 5cf0e9d0f5b..120773a0c8d 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -232,6 +232,22 @@ TEST(YAMLIO, TestSequenceMapWriteAndRead) { } } +// +// Test YAML filename handling. +// +static void testErrorFilename(const llvm::SMDiagnostic &Error, void *) { + EXPECT_EQ(Error.getFilename(), "foo.yaml"); +} + +TEST(YAMLIO, TestGivenFilename) { + auto Buffer = llvm::MemoryBuffer::getMemBuffer("{ x: 42 }", "foo.yaml"); + Input yin(*Buffer, nullptr, testErrorFilename); + FooBar Value; + yin >> Value; + + EXPECT_TRUE(!!yin.error()); +} + //===----------------------------------------------------------------------===// // Test built-in types -- 2.50.0