diff --git a/clang/lib/Tooling/Syntax/Mutations.cpp b/clang/lib/Tooling/Syntax/Mutations.cpp index bf1bcda26455b3..8def1c7296899d 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) {