clang API Documentation
00001 //===--- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the DeltaTree and related classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Rewrite/DeltaTree.h" 00015 #include "clang/Basic/LLVM.h" 00016 #include <cstring> 00017 #include <cstdio> 00018 using namespace clang; 00019 00020 /// The DeltaTree class is a multiway search tree (BTree) structure with some 00021 /// fancy features. B-Trees are generally more memory and cache efficient 00022 /// than binary trees, because they store multiple keys/values in each node. 00023 /// 00024 /// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing 00025 /// fast lookup by FileIndex. However, an added (important) bonus is that it 00026 /// can also efficiently tell us the full accumulated delta for a specific 00027 /// file offset as well, without traversing the whole tree. 00028 /// 00029 /// The nodes of the tree are made up of instances of two classes: 00030 /// DeltaTreeNode and DeltaTreeInteriorNode. The later subclasses the 00031 /// former and adds children pointers. Each node knows the full delta of all 00032 /// entries (recursively) contained inside of it, which allows us to get the 00033 /// full delta implied by a whole subtree in constant time. 00034 00035 namespace { 00036 /// SourceDelta - As code in the original input buffer is added and deleted, 00037 /// SourceDelta records are used to keep track of how the input SourceLocation 00038 /// object is mapped into the output buffer. 00039 struct SourceDelta { 00040 unsigned FileLoc; 00041 int Delta; 00042 00043 static SourceDelta get(unsigned Loc, int D) { 00044 SourceDelta Delta; 00045 Delta.FileLoc = Loc; 00046 Delta.Delta = D; 00047 return Delta; 00048 } 00049 }; 00050 00051 /// DeltaTreeNode - The common part of all nodes. 00052 /// 00053 class DeltaTreeNode { 00054 public: 00055 struct InsertResult { 00056 DeltaTreeNode *LHS, *RHS; 00057 SourceDelta Split; 00058 }; 00059 00060 private: 00061 friend class DeltaTreeInteriorNode; 00062 00063 /// WidthFactor - This controls the number of K/V slots held in the BTree: 00064 /// how wide it is. Each level of the BTree is guaranteed to have at least 00065 /// WidthFactor-1 K/V pairs (except the root) and may have at most 00066 /// 2*WidthFactor-1 K/V pairs. 00067 enum { WidthFactor = 8 }; 00068 00069 /// Values - This tracks the SourceDelta's currently in this node. 00070 /// 00071 SourceDelta Values[2*WidthFactor-1]; 00072 00073 /// NumValuesUsed - This tracks the number of values this node currently 00074 /// holds. 00075 unsigned char NumValuesUsed; 00076 00077 /// IsLeaf - This is true if this is a leaf of the btree. If false, this is 00078 /// an interior node, and is actually an instance of DeltaTreeInteriorNode. 00079 bool IsLeaf; 00080 00081 /// FullDelta - This is the full delta of all the values in this node and 00082 /// all children nodes. 00083 int FullDelta; 00084 public: 00085 DeltaTreeNode(bool isLeaf = true) 00086 : NumValuesUsed(0), IsLeaf(isLeaf), FullDelta(0) {} 00087 00088 bool isLeaf() const { return IsLeaf; } 00089 int getFullDelta() const { return FullDelta; } 00090 bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; } 00091 00092 unsigned getNumValuesUsed() const { return NumValuesUsed; } 00093 const SourceDelta &getValue(unsigned i) const { 00094 assert(i < NumValuesUsed && "Invalid value #"); 00095 return Values[i]; 00096 } 00097 SourceDelta &getValue(unsigned i) { 00098 assert(i < NumValuesUsed && "Invalid value #"); 00099 return Values[i]; 00100 } 00101 00102 /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into 00103 /// this node. If insertion is easy, do it and return false. Otherwise, 00104 /// split the node, populate InsertRes with info about the split, and return 00105 /// true. 00106 bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes); 00107 00108 void DoSplit(InsertResult &InsertRes); 00109 00110 00111 /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a 00112 /// local walk over our contained deltas. 00113 void RecomputeFullDeltaLocally(); 00114 00115 void Destroy(); 00116 00117 //static inline bool classof(const DeltaTreeNode *) { return true; } 00118 }; 00119 } // end anonymous namespace 00120 00121 namespace { 00122 /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers. 00123 /// This class tracks them. 00124 class DeltaTreeInteriorNode : public DeltaTreeNode { 00125 DeltaTreeNode *Children[2*WidthFactor]; 00126 ~DeltaTreeInteriorNode() { 00127 for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i) 00128 Children[i]->Destroy(); 00129 } 00130 friend class DeltaTreeNode; 00131 public: 00132 DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {} 00133 00134 DeltaTreeInteriorNode(const InsertResult &IR) 00135 : DeltaTreeNode(false /*nonleaf*/) { 00136 Children[0] = IR.LHS; 00137 Children[1] = IR.RHS; 00138 Values[0] = IR.Split; 00139 FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta; 00140 NumValuesUsed = 1; 00141 } 00142 00143 const DeltaTreeNode *getChild(unsigned i) const { 00144 assert(i < getNumValuesUsed()+1 && "Invalid child"); 00145 return Children[i]; 00146 } 00147 DeltaTreeNode *getChild(unsigned i) { 00148 assert(i < getNumValuesUsed()+1 && "Invalid child"); 00149 return Children[i]; 00150 } 00151 00152 //static inline bool classof(const DeltaTreeInteriorNode *) { return true; } 00153 static inline bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); } 00154 }; 00155 } 00156 00157 00158 /// Destroy - A 'virtual' destructor. 00159 void DeltaTreeNode::Destroy() { 00160 if (isLeaf()) 00161 delete this; 00162 else 00163 delete cast<DeltaTreeInteriorNode>(this); 00164 } 00165 00166 /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a 00167 /// local walk over our contained deltas. 00168 void DeltaTreeNode::RecomputeFullDeltaLocally() { 00169 int NewFullDelta = 0; 00170 for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i) 00171 NewFullDelta += Values[i].Delta; 00172 if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) 00173 for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i) 00174 NewFullDelta += IN->getChild(i)->getFullDelta(); 00175 FullDelta = NewFullDelta; 00176 } 00177 00178 /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into 00179 /// this node. If insertion is easy, do it and return false. Otherwise, 00180 /// split the node, populate InsertRes with info about the split, and return 00181 /// true. 00182 bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta, 00183 InsertResult *InsertRes) { 00184 // Maintain full delta for this node. 00185 FullDelta += Delta; 00186 00187 // Find the insertion point, the first delta whose index is >= FileIndex. 00188 unsigned i = 0, e = getNumValuesUsed(); 00189 while (i != e && FileIndex > getValue(i).FileLoc) 00190 ++i; 00191 00192 // If we found an a record for exactly this file index, just merge this 00193 // value into the pre-existing record and finish early. 00194 if (i != e && getValue(i).FileLoc == FileIndex) { 00195 // NOTE: Delta could drop to zero here. This means that the delta entry is 00196 // useless and could be removed. Supporting erases is more complex than 00197 // leaving an entry with Delta=0, so we just leave an entry with Delta=0 in 00198 // the tree. 00199 Values[i].Delta += Delta; 00200 return false; 00201 } 00202 00203 // Otherwise, we found an insertion point, and we know that the value at the 00204 // specified index is > FileIndex. Handle the leaf case first. 00205 if (isLeaf()) { 00206 if (!isFull()) { 00207 // For an insertion into a non-full leaf node, just insert the value in 00208 // its sorted position. This requires moving later values over. 00209 if (i != e) 00210 memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i)); 00211 Values[i] = SourceDelta::get(FileIndex, Delta); 00212 ++NumValuesUsed; 00213 return false; 00214 } 00215 00216 // Otherwise, if this is leaf is full, split the node at its median, insert 00217 // the value into one of the children, and return the result. 00218 assert(InsertRes && "No result location specified"); 00219 DoSplit(*InsertRes); 00220 00221 if (InsertRes->Split.FileLoc > FileIndex) 00222 InsertRes->LHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/); 00223 else 00224 InsertRes->RHS->DoInsertion(FileIndex, Delta, 0 /*can't fail*/); 00225 return true; 00226 } 00227 00228 // Otherwise, this is an interior node. Send the request down the tree. 00229 DeltaTreeInteriorNode *IN = cast<DeltaTreeInteriorNode>(this); 00230 if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes)) 00231 return false; // If there was space in the child, just return. 00232 00233 // Okay, this split the subtree, producing a new value and two children to 00234 // insert here. If this node is non-full, we can just insert it directly. 00235 if (!isFull()) { 00236 // Now that we have two nodes and a new element, insert the perclated value 00237 // into ourself by moving all the later values/children down, then inserting 00238 // the new one. 00239 if (i != e) 00240 memmove(&IN->Children[i+2], &IN->Children[i+1], 00241 (e-i)*sizeof(IN->Children[0])); 00242 IN->Children[i] = InsertRes->LHS; 00243 IN->Children[i+1] = InsertRes->RHS; 00244 00245 if (e != i) 00246 memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0])); 00247 Values[i] = InsertRes->Split; 00248 ++NumValuesUsed; 00249 return false; 00250 } 00251 00252 // Finally, if this interior node was full and a node is percolated up, split 00253 // ourself and return that up the chain. Start by saving all our info to 00254 // avoid having the split clobber it. 00255 IN->Children[i] = InsertRes->LHS; 00256 DeltaTreeNode *SubRHS = InsertRes->RHS; 00257 SourceDelta SubSplit = InsertRes->Split; 00258 00259 // Do the split. 00260 DoSplit(*InsertRes); 00261 00262 // Figure out where to insert SubRHS/NewSplit. 00263 DeltaTreeInteriorNode *InsertSide; 00264 if (SubSplit.FileLoc < InsertRes->Split.FileLoc) 00265 InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS); 00266 else 00267 InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS); 00268 00269 // We now have a non-empty interior node 'InsertSide' to insert 00270 // SubRHS/SubSplit into. Find out where to insert SubSplit. 00271 00272 // Find the insertion point, the first delta whose index is >SubSplit.FileLoc. 00273 i = 0; e = InsertSide->getNumValuesUsed(); 00274 while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc) 00275 ++i; 00276 00277 // Now we know that i is the place to insert the split value into. Insert it 00278 // and the child right after it. 00279 if (i != e) 00280 memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1], 00281 (e-i)*sizeof(IN->Children[0])); 00282 InsertSide->Children[i+1] = SubRHS; 00283 00284 if (e != i) 00285 memmove(&InsertSide->Values[i+1], &InsertSide->Values[i], 00286 (e-i)*sizeof(Values[0])); 00287 InsertSide->Values[i] = SubSplit; 00288 ++InsertSide->NumValuesUsed; 00289 InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta(); 00290 return true; 00291 } 00292 00293 /// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values) 00294 /// into two subtrees each with "WidthFactor-1" values and a pivot value. 00295 /// Return the pieces in InsertRes. 00296 void DeltaTreeNode::DoSplit(InsertResult &InsertRes) { 00297 assert(isFull() && "Why split a non-full node?"); 00298 00299 // Since this node is full, it contains 2*WidthFactor-1 values. We move 00300 // the first 'WidthFactor-1' values to the LHS child (which we leave in this 00301 // node), propagate one value up, and move the last 'WidthFactor-1' values 00302 // into the RHS child. 00303 00304 // Create the new child node. 00305 DeltaTreeNode *NewNode; 00306 if (DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(this)) { 00307 // If this is an interior node, also move over 'WidthFactor' children 00308 // into the new node. 00309 DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode(); 00310 memcpy(&New->Children[0], &IN->Children[WidthFactor], 00311 WidthFactor*sizeof(IN->Children[0])); 00312 NewNode = New; 00313 } else { 00314 // Just create the new leaf node. 00315 NewNode = new DeltaTreeNode(); 00316 } 00317 00318 // Move over the last 'WidthFactor-1' values from here to NewNode. 00319 memcpy(&NewNode->Values[0], &Values[WidthFactor], 00320 (WidthFactor-1)*sizeof(Values[0])); 00321 00322 // Decrease the number of values in the two nodes. 00323 NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1; 00324 00325 // Recompute the two nodes' full delta. 00326 NewNode->RecomputeFullDeltaLocally(); 00327 RecomputeFullDeltaLocally(); 00328 00329 InsertRes.LHS = this; 00330 InsertRes.RHS = NewNode; 00331 InsertRes.Split = Values[WidthFactor-1]; 00332 } 00333 00334 00335 00336 //===----------------------------------------------------------------------===// 00337 // DeltaTree Implementation 00338 //===----------------------------------------------------------------------===// 00339 00340 //#define VERIFY_TREE 00341 00342 #ifdef VERIFY_TREE 00343 /// VerifyTree - Walk the btree performing assertions on various properties to 00344 /// verify consistency. This is useful for debugging new changes to the tree. 00345 static void VerifyTree(const DeltaTreeNode *N) { 00346 const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(N); 00347 if (IN == 0) { 00348 // Verify leaves, just ensure that FullDelta matches up and the elements 00349 // are in proper order. 00350 int FullDelta = 0; 00351 for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) { 00352 if (i) 00353 assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc); 00354 FullDelta += N->getValue(i).Delta; 00355 } 00356 assert(FullDelta == N->getFullDelta()); 00357 return; 00358 } 00359 00360 // Verify interior nodes: Ensure that FullDelta matches up and the 00361 // elements are in proper order and the children are in proper order. 00362 int FullDelta = 0; 00363 for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) { 00364 const SourceDelta &IVal = N->getValue(i); 00365 const DeltaTreeNode *IChild = IN->getChild(i); 00366 if (i) 00367 assert(IN->getValue(i-1).FileLoc < IVal.FileLoc); 00368 FullDelta += IVal.Delta; 00369 FullDelta += IChild->getFullDelta(); 00370 00371 // The largest value in child #i should be smaller than FileLoc. 00372 assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc < 00373 IVal.FileLoc); 00374 00375 // The smallest value in child #i+1 should be larger than FileLoc. 00376 assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc); 00377 VerifyTree(IChild); 00378 } 00379 00380 FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta(); 00381 00382 assert(FullDelta == N->getFullDelta()); 00383 } 00384 #endif // VERIFY_TREE 00385 00386 static DeltaTreeNode *getRoot(void *Root) { 00387 return (DeltaTreeNode*)Root; 00388 } 00389 00390 DeltaTree::DeltaTree() { 00391 Root = new DeltaTreeNode(); 00392 } 00393 DeltaTree::DeltaTree(const DeltaTree &RHS) { 00394 // Currently we only support copying when the RHS is empty. 00395 assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 && 00396 "Can only copy empty tree"); 00397 Root = new DeltaTreeNode(); 00398 } 00399 00400 DeltaTree::~DeltaTree() { 00401 getRoot(Root)->Destroy(); 00402 } 00403 00404 /// getDeltaAt - Return the accumulated delta at the specified file offset. 00405 /// This includes all insertions or delections that occurred *before* the 00406 /// specified file index. 00407 int DeltaTree::getDeltaAt(unsigned FileIndex) const { 00408 const DeltaTreeNode *Node = getRoot(Root); 00409 00410 int Result = 0; 00411 00412 // Walk down the tree. 00413 while (1) { 00414 // For all nodes, include any local deltas before the specified file 00415 // index by summing them up directly. Keep track of how many were 00416 // included. 00417 unsigned NumValsGreater = 0; 00418 for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e; 00419 ++NumValsGreater) { 00420 const SourceDelta &Val = Node->getValue(NumValsGreater); 00421 00422 if (Val.FileLoc >= FileIndex) 00423 break; 00424 Result += Val.Delta; 00425 } 00426 00427 // If we have an interior node, include information about children and 00428 // recurse. Otherwise, if we have a leaf, we're done. 00429 const DeltaTreeInteriorNode *IN = dyn_cast<DeltaTreeInteriorNode>(Node); 00430 if (!IN) return Result; 00431 00432 // Include any children to the left of the values we skipped, all of 00433 // their deltas should be included as well. 00434 for (unsigned i = 0; i != NumValsGreater; ++i) 00435 Result += IN->getChild(i)->getFullDelta(); 00436 00437 // If we found exactly the value we were looking for, break off the 00438 // search early. There is no need to search the RHS of the value for 00439 // partial results. 00440 if (NumValsGreater != Node->getNumValuesUsed() && 00441 Node->getValue(NumValsGreater).FileLoc == FileIndex) 00442 return Result+IN->getChild(NumValsGreater)->getFullDelta(); 00443 00444 // Otherwise, traverse down the tree. The selected subtree may be 00445 // partially included in the range. 00446 Node = IN->getChild(NumValsGreater); 00447 } 00448 // NOT REACHED. 00449 } 00450 00451 /// AddDelta - When a change is made that shifts around the text buffer, 00452 /// this method is used to record that info. It inserts a delta of 'Delta' 00453 /// into the current DeltaTree at offset FileIndex. 00454 void DeltaTree::AddDelta(unsigned FileIndex, int Delta) { 00455 assert(Delta && "Adding a noop?"); 00456 DeltaTreeNode *MyRoot = getRoot(Root); 00457 00458 DeltaTreeNode::InsertResult InsertRes; 00459 if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) { 00460 Root = MyRoot = new DeltaTreeInteriorNode(InsertRes); 00461 } 00462 00463 #ifdef VERIFY_TREE 00464 VerifyTree(MyRoot); 00465 #endif 00466 } 00467