diff options
Diffstat (limited to 'include/llvm/Support/YAMLParser.h')
-rw-r--r-- | include/llvm/Support/YAMLParser.h | 97 |
1 files changed, 51 insertions, 46 deletions
diff --git a/include/llvm/Support/YAMLParser.h b/include/llvm/Support/YAMLParser.h index b9e3fa47752ce..c196dd6c1ddc9 100644 --- a/include/llvm/Support/YAMLParser.h +++ b/include/llvm/Support/YAMLParser.h @@ -1,4 +1,4 @@ -//===--- YAMLParser.h - Simple YAML parser --------------------------------===// +//===- YAMLParser.h - Simple YAML parser ------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -41,20 +41,25 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/SMLoc.h" +#include <cassert> +#include <cstddef> +#include <iterator> #include <map> +#include <memory> +#include <string> #include <system_error> -#include <utility> namespace llvm { + class MemoryBufferRef; class SourceMgr; -class Twine; class raw_ostream; +class Twine; namespace yaml { -class document_iterator; class Document; +class document_iterator; class Node; class Scanner; struct Token; @@ -87,6 +92,7 @@ public: document_iterator end(); void skip(); bool failed(); + bool validate() { skip(); return !failed(); @@ -95,10 +101,10 @@ public: void printError(Node *N, const Twine &Msg); private: + friend class Document; + std::unique_ptr<Scanner> scanner; std::unique_ptr<Document> CurrentDoc; - - friend class Document; }; /// \brief Abstract base class for all Nodes. @@ -119,6 +125,18 @@ public: Node(unsigned int Type, std::unique_ptr<Document> &, StringRef Anchor, StringRef Tag); + void *operator new(size_t Size, BumpPtrAllocator &Alloc, + size_t Alignment = 16) noexcept { + return Alloc.Allocate(Size, Alignment); + } + + void operator delete(void *Ptr, BumpPtrAllocator &Alloc, + size_t Size) noexcept { + Alloc.Deallocate(Ptr, Size); + } + + void operator delete(void *) noexcept = delete; + /// \brief Get the value of the anchor attached to this node. If it does not /// have one, getAnchor().size() will be 0. StringRef getAnchor() const { return Anchor; } @@ -146,22 +164,10 @@ public: unsigned int getType() const { return TypeID; } - void *operator new(size_t Size, BumpPtrAllocator &Alloc, - size_t Alignment = 16) noexcept { - return Alloc.Allocate(Size, Alignment); - } - - void operator delete(void *Ptr, BumpPtrAllocator &Alloc, - size_t Size) noexcept { - Alloc.Deallocate(Ptr, Size); - } - protected: std::unique_ptr<Document> &Doc; SMRange SourceRange; - void operator delete(void *) noexcept = delete; - ~Node() = default; private: @@ -268,8 +274,7 @@ class KeyValueNode final : public Node { public: KeyValueNode(std::unique_ptr<Document> &D) - : Node(NK_KeyValue, D, StringRef(), StringRef()), Key(nullptr), - Value(nullptr) {} + : Node(NK_KeyValue, D, StringRef(), StringRef()) {} /// \brief Parse and return the key. /// @@ -296,8 +301,8 @@ public: } private: - Node *Key; - Node *Value; + Node *Key = nullptr; + Node *Value = nullptr; }; /// \brief This is an iterator abstraction over YAML collections shared by both @@ -309,7 +314,7 @@ template <class BaseT, class ValueT> class basic_collection_iterator : public std::iterator<std::input_iterator_tag, ValueT> { public: - basic_collection_iterator() : Base(nullptr) {} + basic_collection_iterator() = default; basic_collection_iterator(BaseT *B) : Base(B) {} ValueT *operator->() const { @@ -358,7 +363,7 @@ public: } private: - BaseT *Base; + BaseT *Base = nullptr; }; // The following two templates are used for both MappingNode and Sequence Node. @@ -399,11 +404,12 @@ public: MappingNode(std::unique_ptr<Document> &D, StringRef Anchor, StringRef Tag, MappingType MT) - : Node(NK_Mapping, D, Anchor, Tag), Type(MT), IsAtBeginning(true), - IsAtEnd(false), CurrentEntry(nullptr) {} + : Node(NK_Mapping, D, Anchor, Tag), Type(MT) {} friend class basic_collection_iterator<MappingNode, KeyValueNode>; - typedef basic_collection_iterator<MappingNode, KeyValueNode> iterator; + + using iterator = basic_collection_iterator<MappingNode, KeyValueNode>; + template <class T> friend typename T::iterator yaml::begin(T &); template <class T> friend void yaml::skip(T &); @@ -419,9 +425,9 @@ public: private: MappingType Type; - bool IsAtBeginning; - bool IsAtEnd; - KeyValueNode *CurrentEntry; + bool IsAtBeginning = true; + bool IsAtEnd = false; + KeyValueNode *CurrentEntry = nullptr; void increment(); }; @@ -453,13 +459,12 @@ public: SequenceNode(std::unique_ptr<Document> &D, StringRef Anchor, StringRef Tag, SequenceType ST) - : Node(NK_Sequence, D, Anchor, Tag), SeqType(ST), IsAtBeginning(true), - IsAtEnd(false), - WasPreviousTokenFlowEntry(true), // Start with an imaginary ','. - CurrentEntry(nullptr) {} + : Node(NK_Sequence, D, Anchor, Tag), SeqType(ST) {} friend class basic_collection_iterator<SequenceNode, Node>; - typedef basic_collection_iterator<SequenceNode, Node> iterator; + + using iterator = basic_collection_iterator<SequenceNode, Node>; + template <class T> friend typename T::iterator yaml::begin(T &); template <class T> friend void yaml::skip(T &); @@ -477,10 +482,10 @@ public: private: SequenceType SeqType; - bool IsAtBeginning; - bool IsAtEnd; - bool WasPreviousTokenFlowEntry; - Node *CurrentEntry; + bool IsAtBeginning = true; + bool IsAtEnd = false; + bool WasPreviousTokenFlowEntry = true; // Start with an imaginary ','. + Node *CurrentEntry = nullptr; }; /// \brief Represents an alias to a Node with an anchor. @@ -507,11 +512,11 @@ private: /// node. class Document { public: + Document(Stream &ParentStream); + /// \brief Root for parsing a node. Returns a single node. Node *parseBlockNode(); - Document(Stream &ParentStream); - /// \brief Finish parsing the current document and return true if there are /// more. Return false otherwise. bool skip(); @@ -564,7 +569,7 @@ private: /// \brief Iterator abstraction for Documents over a Stream. class document_iterator { public: - document_iterator() : Doc(nullptr) {} + document_iterator() = default; document_iterator(std::unique_ptr<Document> &D) : Doc(&D) {} bool operator==(const document_iterator &Other) { @@ -593,11 +598,11 @@ public: private: bool isAtEnd() const { return !Doc || !*Doc; } - std::unique_ptr<Document> *Doc; + std::unique_ptr<Document> *Doc = nullptr; }; -} // End namespace yaml. +} // end namespace yaml -} // End namespace llvm. +} // end namespace llvm -#endif +#endif // LLVM_SUPPORT_YAMLPARSER_H |