[cfe-commits] r51651 - in /cfe/trunk: include/clang/Rewrite/RewriteRope.h lib/Rewrite/RewriteRope.cpp

Chris Lattner sabre at nondot.org
Wed May 28 11:45:57 PDT 2008


Author: lattner
Date: Wed May 28 13:45:56 2008
New Revision: 51651

URL: http://llvm.org/viewvc/llvm-project?rev=51651&view=rev
Log:
Fix rewrite rope to keep the leaf list up-to-date as it erases leaves
from the rope.  rdar://5952468

Modified:
    cfe/trunk/include/clang/Rewrite/RewriteRope.h
    cfe/trunk/lib/Rewrite/RewriteRope.cpp

Modified: cfe/trunk/include/clang/Rewrite/RewriteRope.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/RewriteRope.h?rev=51651&r1=51650&r2=51651&view=diff

==============================================================================
--- cfe/trunk/include/clang/Rewrite/RewriteRope.h (original)
+++ cfe/trunk/include/clang/Rewrite/RewriteRope.h Wed May 28 13:45:56 2008
@@ -181,7 +181,7 @@
   RopeRefCountString *AllocBuffer;
   unsigned AllocOffs;
   enum { AllocChunkSize = 4080 };
-  
+
 public:
   RewriteRope() :  AllocBuffer(0), AllocOffs(AllocChunkSize) {}
   RewriteRope(const RewriteRope &RHS) 

Modified: cfe/trunk/lib/Rewrite/RewriteRope.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/RewriteRope.cpp?rev=51651&r1=51650&r2=51651&view=diff

==============================================================================
--- cfe/trunk/lib/Rewrite/RewriteRope.cpp (original)
+++ cfe/trunk/lib/Rewrite/RewriteRope.cpp Wed May 28 13:45:56 2008
@@ -147,9 +147,14 @@
     
     /// NextLeaf - This is a pointer to the next leaf in the tree, allowing
     /// efficient in-order forward iteration of the tree without traversal.
-    const RopePieceBTreeLeaf *NextLeaf;
+    RopePieceBTreeLeaf **PrevLeaf, *NextLeaf;
   public:
-    RopePieceBTreeLeaf() : RopePieceBTreeNode(true), NumPieces(0), NextLeaf(0){}
+    RopePieceBTreeLeaf() : RopePieceBTreeNode(true), NumPieces(0),
+                           PrevLeaf(0), NextLeaf(0) {}
+    ~RopePieceBTreeLeaf() {
+      if (PrevLeaf || NextLeaf)
+        removeFromLeafInOrder();
+    }
     
     bool isFull() const { return NumPieces == 2*WidthFactor; }
     
@@ -168,7 +173,25 @@
     }
     
     const RopePieceBTreeLeaf *getNextLeafInOrder() const { return NextLeaf; }
-    void setNextLeafInOrder(const RopePieceBTreeLeaf *NL) { NextLeaf = NL; }
+    void insertAfterLeafInOrder(RopePieceBTreeLeaf *Node) {
+      assert(PrevLeaf == 0 && NextLeaf == 0 && "Already in ordering");
+      
+      NextLeaf = Node->NextLeaf;
+      if (NextLeaf)
+        NextLeaf->PrevLeaf = &NextLeaf;
+      PrevLeaf = &Node->NextLeaf;
+      Node->NextLeaf = this;
+    }
+    
+    void removeFromLeafInOrder() {
+      if (PrevLeaf) {
+        *PrevLeaf = NextLeaf;
+        if (NextLeaf)
+          NextLeaf->PrevLeaf = PrevLeaf;
+      } else if (NextLeaf) {
+        NextLeaf->PrevLeaf = 0;
+      }
+    }
     
     /// FullRecomputeSizeLocally - This method recomputes the 'Size' field by
     /// summing the size of all RopePieces.
@@ -302,8 +325,7 @@
   FullRecomputeSizeLocally();
   
   // Update the list of leaves.
-  NewNode->setNextLeafInOrder(this->getNextLeafInOrder());
-  this->setNextLeafInOrder(NewNode);
+  NewNode->insertAfterLeafInOrder(this);
   
   // These insertions can't fail.
   if (this->size() >= Offset)





More information about the cfe-commits mailing list