diff options
Diffstat (limited to 'lib/Rewrite/DeltaTree.cpp')
-rw-r--r-- | lib/Rewrite/DeltaTree.cpp | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/lib/Rewrite/DeltaTree.cpp b/lib/Rewrite/DeltaTree.cpp index 352fab077a2e..1dfc26cc918f 100644 --- a/lib/Rewrite/DeltaTree.cpp +++ b/lib/Rewrite/DeltaTree.cpp @@ -1,4 +1,4 @@ -//===--- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ----------------===// +//===- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ------------------===// // // The LLVM Compiler Infrastructure // @@ -13,8 +13,10 @@ #include "clang/Rewrite/Core/DeltaTree.h" #include "clang/Basic/LLVM.h" -#include <cstdio> +#include "llvm/Support/Casting.h" +#include <cassert> #include <cstring> + using namespace clang; /// The DeltaTree class is a multiway search tree (BTree) structure with some @@ -33,6 +35,7 @@ using namespace clang; /// full delta implied by a whole subtree in constant time. namespace { + /// SourceDelta - As code in the original input buffer is added and deleted, /// SourceDelta records are used to keep track of how the input SourceLocation /// object is mapped into the output buffer. @@ -67,12 +70,11 @@ namespace { enum { WidthFactor = 8 }; /// Values - This tracks the SourceDelta's currently in this node. - /// SourceDelta Values[2*WidthFactor-1]; /// NumValuesUsed - This tracks the number of values this node currently /// holds. - unsigned char NumValuesUsed; + unsigned char NumValuesUsed = 0; /// IsLeaf - This is true if this is a leaf of the btree. If false, this is /// an interior node, and is actually an instance of DeltaTreeInteriorNode. @@ -80,20 +82,22 @@ namespace { /// FullDelta - This is the full delta of all the values in this node and /// all children nodes. - int FullDelta; + int FullDelta = 0; + public: - DeltaTreeNode(bool isLeaf = true) - : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {} + DeltaTreeNode(bool isLeaf = true) : IsLeaf(isLeaf) {} bool isLeaf() const { return IsLeaf; } int getFullDelta() const { return FullDelta; } bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; } unsigned getNumValuesUsed() const { return NumValuesUsed; } + const SourceDelta &getValue(unsigned i) const { assert(i < NumValuesUsed && "Invalid value #"); return Values[i]; } + SourceDelta &getValue(unsigned i) { assert(i < NumValuesUsed && "Invalid value #"); return Values[i]; @@ -114,23 +118,24 @@ namespace { void Destroy(); }; -} // end anonymous namespace -namespace { /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers. /// This class tracks them. class DeltaTreeInteriorNode : public DeltaTreeNode { + friend class DeltaTreeNode; + DeltaTreeNode *Children[2*WidthFactor]; + ~DeltaTreeInteriorNode() { for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i) Children[i]->Destroy(); } - friend class DeltaTreeNode; + public: DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {} DeltaTreeInteriorNode(const InsertResult &IR) - : DeltaTreeNode(false /*nonleaf*/) { + : DeltaTreeNode(false /*nonleaf*/) { Children[0] = IR.LHS; Children[1] = IR.RHS; Values[0] = IR.Split; @@ -142,15 +147,16 @@ namespace { assert(i < getNumValuesUsed()+1 && "Invalid child"); return Children[i]; } + DeltaTreeNode *getChild(unsigned i) { assert(i < getNumValuesUsed()+1 && "Invalid child"); return Children[i]; } - static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); } + static bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); } }; -} +} // namespace /// Destroy - A 'virtual' destructor. void DeltaTreeNode::Destroy() { @@ -166,7 +172,7 @@ void DeltaTreeNode::RecomputeFullDeltaLocally() { int NewFullDelta = 0; for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i) NewFullDelta += Values[i].Delta; - if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) + if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i) NewFullDelta += IN->getChild(i)->getFullDelta(); FullDelta = NewFullDelta; @@ -223,7 +229,7 @@ bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, } // Otherwise, this is an interior node. Send the request down the tree. - DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this); + auto *IN = cast<DeltaTreeInteriorNode>(this); if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes)) return false; // If there was space in the child, just return. @@ -300,7 +306,7 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { // Create the new child node. DeltaTreeNode *NewNode; - if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) { + if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) { // If this is an interior node, also move over 'WidthFactor' children // into the new node. DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode(); @@ -328,8 +334,6 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { InsertRes.Split = Values[WidthFactor-1]; } - - //===----------------------------------------------------------------------===// // DeltaTree Implementation //===----------------------------------------------------------------------===// @@ -340,7 +344,7 @@ void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { /// VerifyTree - Walk the btree performing assertions on various properties to /// verify consistency. This is useful for debugging new changes to the tree. static void VerifyTree(const DeltaTreeNode *N) { - const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N); + const auto *IN = dyn_cast<DeltaTreeInteriorNode>(N); if (IN == 0) { // Verify leaves, just ensure that FullDelta matches up and the elements // are in proper order. @@ -387,6 +391,7 @@ static DeltaTreeNode *getRoot(void *Root) { DeltaTree::DeltaTree() { Root = new DeltaTreeNode(); } + DeltaTree::DeltaTree(const DeltaTree &RHS) { // Currently we only support copying when the RHS is empty. assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 && @@ -407,7 +412,7 @@ int DeltaTree::getDeltaAt(unsigned FileIndex) const { int Result = 0; // Walk down the tree. - while (1) { + while (true) { // For all nodes, include any local deltas before the specified file // index by summing them up directly. Keep track of how many were // included. @@ -423,7 +428,7 @@ int DeltaTree::getDeltaAt(unsigned FileIndex) const { // If we have an interior node, include information about children and // recurse. Otherwise, if we have a leaf, we're done. - const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node); + const auto *IN = dyn_cast<DeltaTreeInteriorNode>(Node); if (!IN) return Result; // Include any children to the left of the values we skipped, all of @@ -461,4 +466,3 @@ void DeltaTree::AddDelta(unsigned FileIndex, int Delta) { VerifyTree(MyRoot); #endif } - |