llvm.org GIT mirror llvm / b54cba4
Improve error handling in YAML parsing Some scanner errors were not checked and reported by the parser. Fix PR30934. Recommit r288014 after fixing unittest. Patch by: Serge Guelton <serge.guelton@telecom-bretagne.eu> Differential Revision: https://reviews.llvm.org/D26419 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288071 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
4 changed file(s) with 41 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
4141 #include "llvm/Support/Allocator.h"
4242 #include "llvm/Support/SMLoc.h"
4343 #include
44 #include
4445 #include
4546
4647 namespace llvm {
7475 class Stream {
7576 public:
7677 /// \brief This keeps a reference to the string referenced by \p Input.
77 Stream(StringRef Input, SourceMgr &, bool ShowColors = true);
78
79 Stream(MemoryBufferRef InputBuffer, SourceMgr &, bool ShowColors = true);
78 Stream(StringRef Input, SourceMgr &, bool ShowColors = true,
79 std::error_code *EC = nullptr);
80
81 Stream(MemoryBufferRef InputBuffer, SourceMgr &, bool ShowColors = true,
82 std::error_code *EC = nullptr);
8083 ~Stream();
8184
8285 document_iterator begin();
231231 /// @brief Scans YAML tokens from a MemoryBuffer.
232232 class Scanner {
233233 public:
234 Scanner(StringRef Input, SourceMgr &SM, bool ShowColors = true);
235 Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors = true);
234 Scanner(StringRef Input, SourceMgr &SM, bool ShowColors = true,
235 std::error_code *EC = nullptr);
236 Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors = true,
237 std::error_code *EC = nullptr);
236238
237239 /// @brief Parse the next token and return it without popping it.
238240 Token &peekNext();
248250 void setError(const Twine &Message, StringRef::iterator Position) {
249251 if (Current >= End)
250252 Current = End - 1;
253
254 // propagate the error if possible
255 if (EC)
256 *EC = make_error_code(std::errc::invalid_argument);
251257
252258 // Don't print out more errors after the first one we encounter. The rest
253259 // are just the result of the first, and have no meaning.
527533
528534 /// @brief Potential simple keys.
529535 SmallVector SimpleKeys;
536
537 std::error_code *EC;
530538 };
531539
532540 } // end namespace yaml
721729 return EscapedInput;
722730 }
723731
724 Scanner::Scanner(StringRef Input, SourceMgr &sm, bool ShowColors)
725 : SM(sm), ShowColors(ShowColors) {
732 Scanner::Scanner(StringRef Input, SourceMgr &sm, bool ShowColors,
733 std::error_code *EC)
734 : SM(sm), ShowColors(ShowColors), EC(EC) {
726735 init(MemoryBufferRef(Input, "YAML"));
727736 }
728737
729 Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors)
730 : SM(SM_), ShowColors(ShowColors) {
738 Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors,
739 std::error_code *EC)
740 : SM(SM_), ShowColors(ShowColors), EC(EC) {
731741 init(Buffer);
732742 }
733743
17251735 return false;
17261736 }
17271737
1728 Stream::Stream(StringRef Input, SourceMgr &SM, bool ShowColors)
1729 : scanner(new Scanner(Input, SM, ShowColors)), CurrentDoc() {}
1730
1731 Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM, bool ShowColors)
1732 : scanner(new Scanner(InputBuffer, SM, ShowColors)), CurrentDoc() {}
1738 Stream::Stream(StringRef Input, SourceMgr &SM, bool ShowColors,
1739 std::error_code *EC)
1740 : scanner(new Scanner(Input, SM, ShowColors, EC)), CurrentDoc() {}
1741
1742 Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM, bool ShowColors,
1743 std::error_code *EC)
1744 : scanner(new Scanner(InputBuffer, SM, ShowColors, EC)), CurrentDoc() {}
17331745
17341746 Stream::~Stream() {}
17351747
4343 // Input
4444 //===----------------------------------------------------------------------===//
4545
46 Input::Input(StringRef InputContent,
47 void *Ctxt,
48 SourceMgr::DiagHandlerTy DiagHandler,
49 void *DiagHandlerCtxt)
50 : IO(Ctxt),
51 Strm(new Stream(InputContent, SrcMgr)),
52 CurrentNode(nullptr) {
46 Input::Input(StringRef InputContent, void *Ctxt,
47 SourceMgr::DiagHandlerTy DiagHandler, void *DiagHandlerCtxt)
48 : IO(Ctxt), Strm(new Stream(InputContent, SrcMgr, false, &EC)),
49 CurrentNode(nullptr) {
5350 if (DiagHandler)
5451 SrcMgr.setDiagHandler(DiagHandler, DiagHandlerCtxt);
5552 DocIterator = Strm->begin();
23672367 out);
23682368 out.clear();
23692369 }
2370
2371 TEST(YAMLIO, InvalidInput) {
2372 // polluting 1 value in the sequence
2373 Input yin("---\n- foo: 3\n bar: 5\n1\n- foo: 3\n bar: 5\n...\n");
2374 std::vector Data;
2375 yin >> Data;
2376 EXPECT_TRUE((bool)yin.error());
2377 }