diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2021-02-16 20:13:02 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2021-02-16 20:13:02 +0000 |
| commit | b60736ec1405bb0a8dd40989f67ef4c93da068ab (patch) | |
| tree | 5c43fbb7c9fc45f0f87e0e6795a86267dbd12f9d /clang/lib/Tooling/Syntax/Mutations.cpp | |
| parent | cfca06d7963fa0909f90483b42a6d7d194d01e08 (diff) | |
Diffstat (limited to 'clang/lib/Tooling/Syntax/Mutations.cpp')
| -rw-r--r-- | clang/lib/Tooling/Syntax/Mutations.cpp | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/clang/lib/Tooling/Syntax/Mutations.cpp b/clang/lib/Tooling/Syntax/Mutations.cpp index 24048b297a11..f8a652219b22 100644 --- a/clang/lib/Tooling/Syntax/Mutations.cpp +++ b/clang/lib/Tooling/Syntax/Mutations.cpp @@ -30,14 +30,17 @@ public: /// 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->PreviousSibling == nullptr); + assert(New->isDetached()); assert(Role != NodeRole::Detached); New->setRole(Role); - auto *P = Anchor->parent(); - P->replaceChildRangeLowLevel(Anchor, Anchor, New); + auto *P = Anchor->getParent(); + P->replaceChildRangeLowLevel(Anchor->getNextSibling(), + Anchor->getNextSibling(), New); P->assertInvariants(); } @@ -49,43 +52,36 @@ public: assert(Old->canModify()); assert(New->Parent == nullptr); assert(New->NextSibling == nullptr); + assert(New->PreviousSibling == nullptr); assert(New->isDetached()); New->Role = Old->Role; - auto *P = Old->parent(); - P->replaceChildRangeLowLevel(findPrevious(Old), Old->nextSibling(), New); + auto *P = Old->getParent(); + P->replaceChildRangeLowLevel(Old, Old->getNextSibling(), New); P->assertInvariants(); } /// Completely remove the node from its parent. static void remove(syntax::Node *N) { - auto *P = N->parent(); - P->replaceChildRangeLowLevel(findPrevious(N), N->nextSibling(), + assert(N != nullptr); + assert(N->Parent != nullptr); + assert(N->canModify()); + + auto *P = N->getParent(); + P->replaceChildRangeLowLevel(N, N->getNextSibling(), /*New=*/nullptr); P->assertInvariants(); N->assertInvariants(); } - -private: - static syntax::Node *findPrevious(syntax::Node *N) { - if (N->parent()->firstChild() == N) - return nullptr; - for (syntax::Node *C = N->parent()->firstChild(); C != nullptr; - C = C->nextSibling()) { - if (C->nextSibling() == N) - return C; - } - llvm_unreachable("could not find a child node"); - } }; void syntax::removeStatement(syntax::Arena &A, syntax::Statement *S) { assert(S); assert(S->canModify()); - if (isa<CompoundStatement>(S->parent())) { + if (isa<CompoundStatement>(S->getParent())) { // A child of CompoundStatement can just be safely removed. MutationsImpl::remove(S); return; |
