[clang] 72732ac - [SyntaxTree] Bug fix in `MutationsImpl::addAfter`.

Eduardo Caldas via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 14 02:26:10 PDT 2020


Author: Eduardo Caldas
Date: 2020-10-14T09:22:01Z
New Revision: 72732acade77d5ee55a818e2da77a2c5b7033ccb

URL: https://github.com/llvm/llvm-project/commit/72732acade77d5ee55a818e2da77a2c5b7033ccb
DIFF: https://github.com/llvm/llvm-project/commit/72732acade77d5ee55a818e2da77a2c5b7033ccb.diff

LOG: [SyntaxTree] Bug fix in `MutationsImpl::addAfter`.

* Add assertions to other `MutationsImpl` member functions
* `findPrevious` is a free function

Differential Revision: https://reviews.llvm.org/D89314

Added: 
    

Modified: 
    clang/lib/Tooling/Syntax/Mutations.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/Syntax/Mutations.cpp b/clang/lib/Tooling/Syntax/Mutations.cpp
index bf1bcda26455..8def1c729689 100644
--- a/clang/lib/Tooling/Syntax/Mutations.cpp
+++ b/clang/lib/Tooling/Syntax/Mutations.cpp
@@ -23,6 +23,19 @@
 
 using namespace clang;
 
+static syntax::Node *findPrevious(syntax::Node *N) {
+  assert(N);
+  assert(N->getParent());
+  if (N->getParent()->getFirstChild() == N)
+    return nullptr;
+  for (syntax::Node *C = N->getParent()->getFirstChild(); C != nullptr;
+       C = C->getNextSibling()) {
+    if (C->getNextSibling() == N)
+      return C;
+  }
+  llvm_unreachable("could not find a child node");
+}
+
 // This class has access to the internals of tree nodes. Its sole purpose is to
 // define helpers that allow implementing the high-level mutation operations.
 class syntax::MutationsImpl {
@@ -30,14 +43,15 @@ class syntax::MutationsImpl {
   /// Add a new node with a specified role.
   static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role) {
     assert(Anchor != nullptr);
+    assert(Anchor->Parent != nullptr);
     assert(New->Parent == nullptr);
     assert(New->NextSibling == nullptr);
-    assert(!New->isDetached());
+    assert(New->isDetached());
     assert(Role != NodeRole::Detached);
 
     New->setRole(Role);
     auto *P = Anchor->getParent();
-    P->replaceChildRangeLowLevel(Anchor, Anchor, New);
+    P->replaceChildRangeLowLevel(Anchor, Anchor->getNextSibling(), New);
 
     P->assertInvariants();
   }
@@ -60,6 +74,10 @@ class syntax::MutationsImpl {
 
   /// Completely remove the node from its parent.
   static void remove(syntax::Node *N) {
+    assert(N != nullptr);
+    assert(N->Parent != nullptr);
+    assert(N->canModify());
+
     auto *P = N->getParent();
     P->replaceChildRangeLowLevel(findPrevious(N), N->getNextSibling(),
                                  /*New=*/nullptr);
@@ -67,18 +85,6 @@ class syntax::MutationsImpl {
     P->assertInvariants();
     N->assertInvariants();
   }
-
-private:
-  static syntax::Node *findPrevious(syntax::Node *N) {
-    if (N->getParent()->getFirstChild() == N)
-      return nullptr;
-    for (syntax::Node *C = N->getParent()->getFirstChild(); C != nullptr;
-         C = C->getNextSibling()) {
-      if (C->getNextSibling() == N)
-        return C;
-    }
-    llvm_unreachable("could not find a child node");
-  }
 };
 
 void syntax::removeStatement(syntax::Arena &A, syntax::Statement *S) {


        


More information about the cfe-commits mailing list