Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Rewrite/DeltaTree.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DeltaTree.cpp - B-Tree for Rewrite Delta tracking ------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the DeltaTree and related classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Rewrite/Core/DeltaTree.h"
14
#include "clang/Basic/LLVM.h"
15
#include "llvm/Support/Casting.h"
16
#include <cassert>
17
#include <cstring>
18
19
using namespace clang;
20
21
/// The DeltaTree class is a multiway search tree (BTree) structure with some
22
/// fancy features.  B-Trees are generally more memory and cache efficient
23
/// than binary trees, because they store multiple keys/values in each node.
24
///
25
/// DeltaTree implements a key/value mapping from FileIndex to Delta, allowing
26
/// fast lookup by FileIndex.  However, an added (important) bonus is that it
27
/// can also efficiently tell us the full accumulated delta for a specific
28
/// file offset as well, without traversing the whole tree.
29
///
30
/// The nodes of the tree are made up of instances of two classes:
31
/// DeltaTreeNode and DeltaTreeInteriorNode.  The later subclasses the
32
/// former and adds children pointers.  Each node knows the full delta of all
33
/// entries (recursively) contained inside of it, which allows us to get the
34
/// full delta implied by a whole subtree in constant time.
35
36
namespace {
37
38
  /// SourceDelta - As code in the original input buffer is added and deleted,
39
  /// SourceDelta records are used to keep track of how the input SourceLocation
40
  /// object is mapped into the output buffer.
41
  struct SourceDelta {
42
    unsigned FileLoc;
43
    int Delta;
44
45
1.76M
    static SourceDelta get(unsigned Loc, int D) {
46
1.76M
      SourceDelta Delta;
47
1.76M
      Delta.FileLoc = Loc;
48
1.76M
      Delta.Delta = D;
49
1.76M
      return Delta;
50
1.76M
    }
51
  };
52
53
  /// DeltaTreeNode - The common part of all nodes.
54
  ///
55
  class DeltaTreeNode {
56
  public:
57
    struct InsertResult {
58
      DeltaTreeNode *LHS, *RHS;
59
      SourceDelta Split;
60
    };
61
62
  private:
63
    friend class DeltaTreeInteriorNode;
64
65
    /// WidthFactor - This controls the number of K/V slots held in the BTree:
66
    /// how wide it is.  Each level of the BTree is guaranteed to have at least
67
    /// WidthFactor-1 K/V pairs (except the root) and may have at most
68
    /// 2*WidthFactor-1 K/V pairs.
69
    enum { WidthFactor = 8 };
70
71
    /// Values - This tracks the SourceDelta's currently in this node.
72
    SourceDelta Values[2*WidthFactor-1];
73
74
    /// NumValuesUsed - This tracks the number of values this node currently
75
    /// holds.
76
    unsigned char NumValuesUsed = 0;
77
78
    /// IsLeaf - This is true if this is a leaf of the btree.  If false, this is
79
    /// an interior node, and is actually an instance of DeltaTreeInteriorNode.
80
    bool IsLeaf;
81
82
    /// FullDelta - This is the full delta of all the values in this node and
83
    /// all children nodes.
84
    int FullDelta = 0;
85
86
  public:
87
252k
    DeltaTreeNode(bool isLeaf = true) : IsLeaf(isLeaf) {}
88
89
22.6M
    bool isLeaf() const { return IsLeaf; }
90
516k
    int getFullDelta() const { return FullDelta; }
91
2.48M
    bool isFull() const { return NumValuesUsed == 2*WidthFactor-1; }
92
93
29.0M
    unsigned getNumValuesUsed() const { return NumValuesUsed; }
94
95
14.0M
    const SourceDelta &getValue(unsigned i) const {
96
14.0M
      assert(i < NumValuesUsed && "Invalid value #");
97
0
      return Values[i];
98
14.0M
    }
99
100
15.6M
    SourceDelta &getValue(unsigned i) {
101
15.6M
      assert(i < NumValuesUsed && "Invalid value #");
102
0
      return Values[i];
103
15.6M
    }
104
105
    /// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
106
    /// this node.  If insertion is easy, do it and return false.  Otherwise,
107
    /// split the node, populate InsertRes with info about the split, and return
108
    /// true.
109
    bool DoInsertion(unsigned FileIndex, int Delta, InsertResult *InsertRes);
110
111
    void DoSplit(InsertResult &InsertRes);
112
113
114
    /// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
115
    /// local walk over our contained deltas.
116
    void RecomputeFullDeltaLocally();
117
118
    void Destroy();
119
  };
120
121
  /// DeltaTreeInteriorNode - When isLeaf = false, a node has child pointers.
122
  /// This class tracks them.
123
  class DeltaTreeInteriorNode : public DeltaTreeNode {
124
    friend class DeltaTreeNode;
125
126
    DeltaTreeNode *Children[2*WidthFactor];
127
128
31.1k
    ~DeltaTreeInteriorNode() {
129
281k
      for (unsigned i = 0, e = NumValuesUsed+1; i != e; ++i)
130
250k
        Children[i]->Destroy();
131
31.1k
    }
132
133
  public:
134
30.2k
    DeltaTreeInteriorNode() : DeltaTreeNode(false /*nonleaf*/) {}
135
136
    DeltaTreeInteriorNode(const InsertResult &IR)
137
852
        : DeltaTreeNode(false /*nonleaf*/) {
138
852
      Children[0] = IR.LHS;
139
852
      Children[1] = IR.RHS;
140
852
      Values[0] = IR.Split;
141
852
      FullDelta = IR.LHS->getFullDelta()+IR.RHS->getFullDelta()+IR.Split.Delta;
142
852
      NumValuesUsed = 1;
143
852
    }
144
145
6.11M
    const DeltaTreeNode *getChild(unsigned i) const {
146
6.11M
      assert(i < getNumValuesUsed()+1 && "Invalid child");
147
0
      return Children[i];
148
6.11M
    }
149
150
484k
    DeltaTreeNode *getChild(unsigned i) {
151
484k
      assert(i < getNumValuesUsed()+1 && "Invalid child");
152
0
      return Children[i];
153
484k
    }
154
155
14.6M
    static bool classof(const DeltaTreeNode *N) { return !N->isLeaf(); }
156
  };
157
158
} // namespace
159
160
/// Destroy - A 'virtual' destructor.
161
252k
void DeltaTreeNode::Destroy() {
162
252k
  if (isLeaf())
163
221k
    delete this;
164
31.1k
  else
165
31.1k
    delete cast<DeltaTreeInteriorNode>(this);
166
252k
}
167
168
/// RecomputeFullDeltaLocally - Recompute the FullDelta field by doing a
169
/// local walk over our contained deltas.
170
499k
void DeltaTreeNode::RecomputeFullDeltaLocally() {
171
499k
  int NewFullDelta = 0;
172
3.99M
  for (unsigned i = 0, e = getNumValuesUsed(); i != e; ++i)
173
3.49M
    NewFullDelta += Values[i].Delta;
174
499k
  if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this))
175
545k
    for (unsigned i = 0, e = getNumValuesUsed()+1; i != e; ++i)
176
484k
      NewFullDelta += IN->getChild(i)->getFullDelta();
177
499k
  FullDelta = NewFullDelta;
178
499k
}
179
180
/// DoInsertion - Do an insertion of the specified FileIndex/Delta pair into
181
/// this node.  If insertion is easy, do it and return false.  Otherwise,
182
/// split the node, populate InsertRes with info about the split, and return
183
/// true.
184
bool DeltaTreeNode::DoInsertion(unsigned FileIndex, int Delta,
185
7.79M
                                InsertResult *InsertRes) {
186
  // Maintain full delta for this node.
187
7.79M
  FullDelta += Delta;
188
189
  // Find the insertion point, the first delta whose index is >= FileIndex.
190
7.79M
  unsigned i = 0, e = getNumValuesUsed();
191
7.79M
  while (i != e && FileIndex > getValue(i).FileLoc)
192
0
    ++i;
193
194
  // If we found an a record for exactly this file index, just merge this
195
  // value into the pre-existing record and finish early.
196
7.79M
  if (i != e && getValue(i).FileLoc == FileIndex) {
197
    // NOTE: Delta could drop to zero here.  This means that the delta entry is
198
    // useless and could be removed.  Supporting erases is more complex than
199
    // leaving an entry with Delta=0, so we just leave an entry with Delta=0 in
200
    // the tree.
201
0
    Values[i].Delta += Delta;
202
0
    return false;
203
0
  }
204
205
  // Otherwise, we found an insertion point, and we know that the value at the
206
  // specified index is > FileIndex.  Handle the leaf case first.
207
7.79M
  if (isLeaf()) {
208
1.98M
    if (!isFull()) {
209
      // For an insertion into a non-full leaf node, just insert the value in
210
      // its sorted position.  This requires moving later values over.
211
1.76M
      if (i != e)
212
1.76M
        memmove(&Values[i+1], &Values[i], sizeof(Values[0])*(e-i));
213
1.76M
      Values[i] = SourceDelta::get(FileIndex, Delta);
214
1.76M
      ++NumValuesUsed;
215
1.76M
      return false;
216
1.76M
    }
217
218
    // Otherwise, if this is leaf is full, split the node at its median, insert
219
    // the value into one of the children, and return the result.
220
219k
    assert(InsertRes && "No result location specified");
221
0
    DoSplit(*InsertRes);
222
223
219k
    if (InsertRes->Split.FileLoc > FileIndex)
224
219k
      InsertRes->LHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
225
0
    else
226
0
      InsertRes->RHS->DoInsertion(FileIndex, Delta, nullptr /*can't fail*/);
227
219k
    return true;
228
1.98M
  }
229
230
  // Otherwise, this is an interior node.  Send the request down the tree.
231
5.81M
  auto *IN = cast<DeltaTreeInteriorNode>(this);
232
5.81M
  if (!IN->Children[i]->DoInsertion(FileIndex, Delta, InsertRes))
233
5.56M
    return false; // If there was space in the child, just return.
234
235
  // Okay, this split the subtree, producing a new value and two children to
236
  // insert here.  If this node is non-full, we can just insert it directly.
237
249k
  if (!isFull()) {
238
    // Now that we have two nodes and a new element, insert the perclated value
239
    // into ourself by moving all the later values/children down, then inserting
240
    // the new one.
241
218k
    if (i != e)
242
218k
      memmove(&IN->Children[i+2], &IN->Children[i+1],
243
218k
              (e-i)*sizeof(IN->Children[0]));
244
218k
    IN->Children[i] = InsertRes->LHS;
245
218k
    IN->Children[i+1] = InsertRes->RHS;
246
247
218k
    if (e != i)
248
218k
      memmove(&Values[i+1], &Values[i], (e-i)*sizeof(Values[0]));
249
218k
    Values[i] = InsertRes->Split;
250
218k
    ++NumValuesUsed;
251
218k
    return false;
252
218k
  }
253
254
  // Finally, if this interior node was full and a node is percolated up, split
255
  // ourself and return that up the chain.  Start by saving all our info to
256
  // avoid having the split clobber it.
257
30.2k
  IN->Children[i] = InsertRes->LHS;
258
30.2k
  DeltaTreeNode *SubRHS = InsertRes->RHS;
259
30.2k
  SourceDelta SubSplit = InsertRes->Split;
260
261
  // Do the split.
262
30.2k
  DoSplit(*InsertRes);
263
264
  // Figure out where to insert SubRHS/NewSplit.
265
30.2k
  DeltaTreeInteriorNode *InsertSide;
266
30.2k
  if (SubSplit.FileLoc < InsertRes->Split.FileLoc)
267
30.2k
    InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->LHS);
268
0
  else
269
0
    InsertSide = cast<DeltaTreeInteriorNode>(InsertRes->RHS);
270
271
  // We now have a non-empty interior node 'InsertSide' to insert
272
  // SubRHS/SubSplit into.  Find out where to insert SubSplit.
273
274
  // Find the insertion point, the first delta whose index is >SubSplit.FileLoc.
275
30.2k
  i = 0; e = InsertSide->getNumValuesUsed();
276
30.2k
  while (i != e && SubSplit.FileLoc > InsertSide->getValue(i).FileLoc)
277
0
    ++i;
278
279
  // Now we know that i is the place to insert the split value into.  Insert it
280
  // and the child right after it.
281
30.2k
  if (i != e)
282
30.2k
    memmove(&InsertSide->Children[i+2], &InsertSide->Children[i+1],
283
30.2k
            (e-i)*sizeof(IN->Children[0]));
284
30.2k
  InsertSide->Children[i+1] = SubRHS;
285
286
30.2k
  if (e != i)
287
30.2k
    memmove(&InsertSide->Values[i+1], &InsertSide->Values[i],
288
30.2k
            (e-i)*sizeof(Values[0]));
289
30.2k
  InsertSide->Values[i] = SubSplit;
290
30.2k
  ++InsertSide->NumValuesUsed;
291
30.2k
  InsertSide->FullDelta += SubSplit.Delta + SubRHS->getFullDelta();
292
30.2k
  return true;
293
249k
}
294
295
/// DoSplit - Split the currently full node (which has 2*WidthFactor-1 values)
296
/// into two subtrees each with "WidthFactor-1" values and a pivot value.
297
/// Return the pieces in InsertRes.
298
249k
void DeltaTreeNode::DoSplit(InsertResult &InsertRes) {
299
249k
  assert(isFull() && "Why split a non-full node?");
300
301
  // Since this node is full, it contains 2*WidthFactor-1 values.  We move
302
  // the first 'WidthFactor-1' values to the LHS child (which we leave in this
303
  // node), propagate one value up, and move the last 'WidthFactor-1' values
304
  // into the RHS child.
305
306
  // Create the new child node.
307
0
  DeltaTreeNode *NewNode;
308
249k
  if (auto *IN = dyn_cast<DeltaTreeInteriorNode>(this)) {
309
    // If this is an interior node, also move over 'WidthFactor' children
310
    // into the new node.
311
30.2k
    DeltaTreeInteriorNode *New = new DeltaTreeInteriorNode();
312
30.2k
    memcpy(&New->Children[0], &IN->Children[WidthFactor],
313
30.2k
           WidthFactor*sizeof(IN->Children[0]));
314
30.2k
    NewNode = New;
315
219k
  } else {
316
    // Just create the new leaf node.
317
219k
    NewNode = new DeltaTreeNode();
318
219k
  }
319
320
  // Move over the last 'WidthFactor-1' values from here to NewNode.
321
249k
  memcpy(&NewNode->Values[0], &Values[WidthFactor],
322
249k
         (WidthFactor-1)*sizeof(Values[0]));
323
324
  // Decrease the number of values in the two nodes.
325
249k
  NewNode->NumValuesUsed = NumValuesUsed = WidthFactor-1;
326
327
  // Recompute the two nodes' full delta.
328
249k
  NewNode->RecomputeFullDeltaLocally();
329
249k
  RecomputeFullDeltaLocally();
330
331
249k
  InsertRes.LHS = this;
332
249k
  InsertRes.RHS = NewNode;
333
249k
  InsertRes.Split = Values[WidthFactor-1];
334
249k
}
335
336
//===----------------------------------------------------------------------===//
337
//                        DeltaTree Implementation
338
//===----------------------------------------------------------------------===//
339
340
//#define VERIFY_TREE
341
342
#ifdef VERIFY_TREE
343
/// VerifyTree - Walk the btree performing assertions on various properties to
344
/// verify consistency.  This is useful for debugging new changes to the tree.
345
static void VerifyTree(const DeltaTreeNode *N) {
346
  const auto *IN = dyn_cast<DeltaTreeInteriorNode>(N);
347
  if (IN == 0) {
348
    // Verify leaves, just ensure that FullDelta matches up and the elements
349
    // are in proper order.
350
    int FullDelta = 0;
351
    for (unsigned i = 0, e = N->getNumValuesUsed(); i != e; ++i) {
352
      if (i)
353
        assert(N->getValue(i-1).FileLoc < N->getValue(i).FileLoc);
354
      FullDelta += N->getValue(i).Delta;
355
    }
356
    assert(FullDelta == N->getFullDelta());
357
    return;
358
  }
359
360
  // Verify interior nodes: Ensure that FullDelta matches up and the
361
  // elements are in proper order and the children are in proper order.
362
  int FullDelta = 0;
363
  for (unsigned i = 0, e = IN->getNumValuesUsed(); i != e; ++i) {
364
    const SourceDelta &IVal = N->getValue(i);
365
    const DeltaTreeNode *IChild = IN->getChild(i);
366
    if (i)
367
      assert(IN->getValue(i-1).FileLoc < IVal.FileLoc);
368
    FullDelta += IVal.Delta;
369
    FullDelta += IChild->getFullDelta();
370
371
    // The largest value in child #i should be smaller than FileLoc.
372
    assert(IChild->getValue(IChild->getNumValuesUsed()-1).FileLoc <
373
           IVal.FileLoc);
374
375
    // The smallest value in child #i+1 should be larger than FileLoc.
376
    assert(IN->getChild(i+1)->getValue(0).FileLoc > IVal.FileLoc);
377
    VerifyTree(IChild);
378
  }
379
380
  FullDelta += IN->getChild(IN->getNumValuesUsed())->getFullDelta();
381
382
  assert(FullDelta == N->getFullDelta());
383
}
384
#endif  // VERIFY_TREE
385
386
3.62M
static DeltaTreeNode *getRoot(void *Root) {
387
3.62M
  return (DeltaTreeNode*)Root;
388
3.62M
}
389
390
544
DeltaTree::DeltaTree() {
391
544
  Root = new DeltaTreeNode();
392
544
}
393
394
1.08k
DeltaTree::DeltaTree(const DeltaTree &RHS) {
395
  // Currently we only support copying when the RHS is empty.
396
1.08k
  assert(getRoot(RHS.Root)->getNumValuesUsed() == 0 &&
397
1.08k
         "Can only copy empty tree");
398
0
  Root = new DeltaTreeNode();
399
1.08k
}
400
401
1.63k
DeltaTree::~DeltaTree() {
402
1.63k
  getRoot(Root)->Destroy();
403
1.63k
}
404
405
/// getDeltaAt - Return the accumulated delta at the specified file offset.
406
/// This includes all insertions or delections that occurred *before* the
407
/// specified file index.
408
1.85M
int DeltaTree::getDeltaAt(unsigned FileIndex) const {
409
1.85M
  const DeltaTreeNode *Node = getRoot(Root);
410
411
1.85M
  int Result = 0;
412
413
  // Walk down the tree.
414
7.97M
  while (true) {
415
    // For all nodes, include any local deltas before the specified file
416
    // index by summing them up directly.  Keep track of how many were
417
    // included.
418
7.97M
    unsigned NumValsGreater = 0;
419
7.97M
    for (unsigned e = Node->getNumValuesUsed(); NumValsGreater != e;
420
7.97M
         ++NumValsGreater) {
421
7.97M
      const SourceDelta &Val = Node->getValue(NumValsGreater);
422
423
7.97M
      if (Val.FileLoc >= FileIndex)
424
7.97M
        break;
425
0
      Result += Val.Delta;
426
0
    }
427
428
    // If we have an interior node, include information about children and
429
    // recurse.  Otherwise, if we have a leaf, we're done.
430
7.97M
    const auto *IN = dyn_cast<DeltaTreeInteriorNode>(Node);
431
7.97M
    if (!IN) return Result;
432
433
    // Include any children to the left of the values we skipped, all of
434
    // their deltas should be included as well.
435
6.11M
    for (unsigned i = 0; i != NumValsGreater; ++i)
436
0
      Result += IN->getChild(i)->getFullDelta();
437
438
    // If we found exactly the value we were looking for, break off the
439
    // search early.  There is no need to search the RHS of the value for
440
    // partial results.
441
6.11M
    if (NumValsGreater != Node->getNumValuesUsed() &&
442
6.11M
        Node->getValue(NumValsGreater).FileLoc == FileIndex)
443
0
      return Result+IN->getChild(NumValsGreater)->getFullDelta();
444
445
    // Otherwise, traverse down the tree.  The selected subtree may be
446
    // partially included in the range.
447
6.11M
    Node = IN->getChild(NumValsGreater);
448
6.11M
  }
449
  // NOT REACHED.
450
1.85M
}
451
452
/// AddDelta - When a change is made that shifts around the text buffer,
453
/// this method is used to record that info.  It inserts a delta of 'Delta'
454
/// into the current DeltaTree at offset FileIndex.
455
1.76M
void DeltaTree::AddDelta(unsigned FileIndex, int Delta) {
456
1.76M
  assert(Delta && "Adding a noop?");
457
0
  DeltaTreeNode *MyRoot = getRoot(Root);
458
459
1.76M
  DeltaTreeNode::InsertResult InsertRes;
460
1.76M
  if (MyRoot->DoInsertion(FileIndex, Delta, &InsertRes)) {
461
852
    Root = new DeltaTreeInteriorNode(InsertRes);
462
#ifdef VERIFY_TREE
463
    MyRoot = Root;
464
#endif
465
852
  }
466
467
#ifdef VERIFY_TREE
468
  VerifyTree(MyRoot);
469
#endif
470
1.76M
}