[PATCH] D89314: [SyntaxTree] Bug fix in `MutationsImpl::addAfter`.
Eduardo Caldas via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 13 06:15:13 PDT 2020
eduucaldas created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
eduucaldas requested review of this revision.
- Add assertions to other `MutationsImpl` member functions
- `findPrevious` is a free function
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D89314
Files:
clang/lib/Tooling/Syntax/Mutations.cpp
Index: clang/lib/Tooling/Syntax/Mutations.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Mutations.cpp
+++ 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 @@
/// 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 @@
/// 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 @@
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) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89314.297840.patch
Type: text/x-patch
Size: 2344 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201013/851407e5/attachment.bin>
More information about the cfe-commits
mailing list