diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h --- a/llvm/include/llvm/Support/YAMLTraits.h +++ b/llvm/include/llvm/Support/YAMLTraits.h @@ -1464,20 +1464,27 @@ bool canElideEmptySequence() override; class HNode { - virtual void anchor(); - public: HNode(Node *n) : _node(n) { } - virtual ~HNode() = default; static bool classof(const HNode *) { return true; } Node *_node; + + 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, 0); + } + + void operator delete(void *) noexcept = delete; }; class EmptyHNode : public HNode { - void anchor() override; - public: EmptyHNode(Node *n) : HNode(n) { } @@ -1487,8 +1494,6 @@ }; class ScalarHNode : public HNode { - void anchor() override; - public: ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { } @@ -1506,8 +1511,6 @@ }; class MapHNode : public HNode { - void anchor() override; - public: MapHNode(Node *n) : HNode(n) { } @@ -1517,16 +1520,13 @@ static bool classof(const MapHNode *) { return true; } - using NameToNodeAndLoc = - StringMap, SMRange>>; + using NameToNodeAndLoc = StringMap>; NameToNodeAndLoc Mapping; SmallVector ValidKeys; }; class SequenceHNode : public HNode { - void anchor() override; - public: SequenceHNode(Node *n) : HNode(n) { } @@ -1536,10 +1536,10 @@ static bool classof(const SequenceHNode *) { return true; } - std::vector> Entries; + std::vector Entries; }; - std::unique_ptr createHNodes(Node *node); + Input::HNode *createHNodes(Node *node); void setError(HNode *hnode, const Twine &message); void setError(Node *node, const Twine &message); void setError(const SMRange &Range, const Twine &message); @@ -1562,9 +1562,10 @@ private: SourceMgr SrcMgr; // must be before Strm std::unique_ptr Strm; - std::unique_ptr TopNode; + HNode *TopNode = nullptr; std::error_code EC; BumpPtrAllocator StringAllocator; + BumpPtrAllocator HNodeAllocator; document_iterator DocIterator; llvm::BitVector BitValuesUsed; HNode *CurrentNode = nullptr; diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -75,13 +75,6 @@ std::error_code Input::error() { return EC; } -// Pin the vtables to this file. -void Input::HNode::anchor() {} -void Input::EmptyHNode::anchor() {} -void Input::ScalarHNode::anchor() {} -void Input::MapHNode::anchor() {} -void Input::SequenceHNode::anchor() {} - bool Input::outputting() const { return false; } @@ -100,7 +93,7 @@ return setCurrentDocument(); } TopNode = createHNodes(N); - CurrentNode = TopNode.get(); + CurrentNode = TopNode; return true; } return false; @@ -174,7 +167,7 @@ return false; } MN->ValidKeys.push_back(Key); - HNode *Value = MN->Mapping[Key].first.get(); + HNode *Value = MN->Mapping[Key].first; if (!Value) { if (Required) setError(CurrentNode, Twine("missing required key '") + Key + "'"); @@ -237,7 +230,7 @@ return false; if (SequenceHNode *SQ = dyn_cast(CurrentNode)) { SaveInfo = CurrentNode; - CurrentNode = SQ->Entries[Index].get(); + CurrentNode = SQ->Entries[Index]; return true; } return false; @@ -254,7 +247,7 @@ return false; if (SequenceHNode *SQ = dyn_cast(CurrentNode)) { SaveInfo = CurrentNode; - CurrentNode = SQ->Entries[index].get(); + CurrentNode = SQ->Entries[index]; return true; } return false; @@ -313,7 +306,7 @@ if (SequenceHNode *SQ = dyn_cast(CurrentNode)) { unsigned Index = 0; for (auto &N : SQ->Entries) { - if (ScalarHNode *SN = dyn_cast(N.get())) { + if (ScalarHNode *SN = dyn_cast(N)) { if (SN->value().equals(Str)) { BitValuesUsed[Index] = true; return true; @@ -336,7 +329,7 @@ assert(BitValuesUsed.size() == SQ->Entries.size()); for (unsigned i = 0; i < SQ->Entries.size(); ++i) { if (!BitValuesUsed[i]) { - setError(SQ->Entries[i].get(), "unknown bit value"); + setError(SQ->Entries[i], "unknown bit value"); return; } } @@ -395,7 +388,7 @@ Strm->printError(range, message, SourceMgr::DK_Warning); } -std::unique_ptr Input::createHNodes(Node *N) { +Input::HNode *Input::createHNodes(Node *N) { SmallString<128> StringStorage; switch (N->getType()) { case Node::NK_Scalar: { @@ -405,27 +398,27 @@ // Copy string to permanent storage KeyStr = StringStorage.str().copy(StringAllocator); } - return std::make_unique(N, KeyStr); + return new (HNodeAllocator) ScalarHNode(N, KeyStr); } case Node::NK_BlockScalar: { BlockScalarNode *BSN = dyn_cast(N); StringRef ValueCopy = BSN->getValue().copy(StringAllocator); - return std::make_unique(N, ValueCopy); + return new (HNodeAllocator) ScalarHNode(N, ValueCopy); } case Node::NK_Sequence: { SequenceNode *SQ = dyn_cast(N); - auto SQHNode = std::make_unique(N); + auto SQHNode = new (HNodeAllocator) SequenceHNode(N); for (Node &SN : *SQ) { auto Entry = createHNodes(&SN); if (EC) break; - SQHNode->Entries.push_back(std::move(Entry)); + SQHNode->Entries.push_back(Entry); } - return std::move(SQHNode); + return SQHNode; } case Node::NK_Mapping: { MappingNode *Map = dyn_cast(N); - auto mapHNode = std::make_unique(N); + auto mapHNode = new (HNodeAllocator) MapHNode(N); for (KeyValueNode &KVN : *Map) { Node *KeyNode = KVN.getKey(); ScalarNode *Key = dyn_cast_or_null(KeyNode); @@ -451,13 +444,12 @@ auto ValueHNode = createHNodes(Value); if (EC) break; - mapHNode->Mapping[KeyStr] = - std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange()); + mapHNode->Mapping[KeyStr] = {ValueHNode, KeyNode->getSourceRange()}; } return std::move(mapHNode); } case Node::NK_Null: - return std::make_unique(N); + return new (HNodeAllocator) EmptyHNode(N); default: setError(N, "unknown node kind"); return nullptr;