11ad15046SIlya Biryukov //===- Mutations.cpp ------------------------------------------*- C++ -*-=====//
21ad15046SIlya Biryukov //
31ad15046SIlya Biryukov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41ad15046SIlya Biryukov // See https://llvm.org/LICENSE.txt for license information.
51ad15046SIlya Biryukov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61ad15046SIlya Biryukov //
71ad15046SIlya Biryukov //===----------------------------------------------------------------------===//
81ad15046SIlya Biryukov #include "clang/Tooling/Syntax/Mutations.h"
91ad15046SIlya Biryukov #include "clang/Basic/LLVM.h"
101ad15046SIlya Biryukov #include "clang/Basic/SourceLocation.h"
111ad15046SIlya Biryukov #include "clang/Lex/Token.h"
121ad15046SIlya Biryukov #include "clang/Tooling/Core/Replacement.h"
131ad15046SIlya Biryukov #include "clang/Tooling/Syntax/BuildTree.h"
141ad15046SIlya Biryukov #include "clang/Tooling/Syntax/Nodes.h"
151ad15046SIlya Biryukov #include "clang/Tooling/Syntax/Tokens.h"
161ad15046SIlya Biryukov #include "clang/Tooling/Syntax/Tree.h"
171ad15046SIlya Biryukov #include "llvm/ADT/ArrayRef.h"
181ad15046SIlya Biryukov #include "llvm/ADT/Optional.h"
191ad15046SIlya Biryukov #include "llvm/ADT/STLExtras.h"
201ad15046SIlya Biryukov #include "llvm/Support/Casting.h"
211ad15046SIlya Biryukov #include <cassert>
221ad15046SIlya Biryukov #include <string>
231ad15046SIlya Biryukov
241ad15046SIlya Biryukov using namespace clang;
251ad15046SIlya Biryukov
261ad15046SIlya Biryukov // This class has access to the internals of tree nodes. Its sole purpose is to
271ad15046SIlya Biryukov // define helpers that allow implementing the high-level mutation operations.
281ad15046SIlya Biryukov class syntax::MutationsImpl {
291ad15046SIlya Biryukov public:
301ad15046SIlya Biryukov /// Add a new node with a specified role.
addAfter(syntax::Node * Anchor,syntax::Node * New,NodeRole Role)311ad15046SIlya Biryukov static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role) {
323b929fe7SIlya Biryukov assert(Anchor != nullptr);
3372732acaSEduardo Caldas assert(Anchor->Parent != nullptr);
341ad15046SIlya Biryukov assert(New->Parent == nullptr);
351ad15046SIlya Biryukov assert(New->NextSibling == nullptr);
3623657d9cSEduardo Caldas assert(New->PreviousSibling == nullptr);
3772732acaSEduardo Caldas assert(New->isDetached());
381ad15046SIlya Biryukov assert(Role != NodeRole::Detached);
391ad15046SIlya Biryukov
40a711a3a4SMarcel Hlopko New->setRole(Role);
414c14ee61SEduardo Caldas auto *P = Anchor->getParent();
4223657d9cSEduardo Caldas P->replaceChildRangeLowLevel(Anchor->getNextSibling(),
4323657d9cSEduardo Caldas Anchor->getNextSibling(), New);
443b929fe7SIlya Biryukov
453b929fe7SIlya Biryukov P->assertInvariants();
461ad15046SIlya Biryukov }
471ad15046SIlya Biryukov
481ad15046SIlya Biryukov /// Replace the node, keeping the role.
replace(syntax::Node * Old,syntax::Node * New)491ad15046SIlya Biryukov static void replace(syntax::Node *Old, syntax::Node *New) {
503b929fe7SIlya Biryukov assert(Old != nullptr);
513b929fe7SIlya Biryukov assert(Old->Parent != nullptr);
523b929fe7SIlya Biryukov assert(Old->canModify());
531ad15046SIlya Biryukov assert(New->Parent == nullptr);
541ad15046SIlya Biryukov assert(New->NextSibling == nullptr);
5523657d9cSEduardo Caldas assert(New->PreviousSibling == nullptr);
561ad15046SIlya Biryukov assert(New->isDetached());
571ad15046SIlya Biryukov
581ad15046SIlya Biryukov New->Role = Old->Role;
594c14ee61SEduardo Caldas auto *P = Old->getParent();
6023657d9cSEduardo Caldas P->replaceChildRangeLowLevel(Old, Old->getNextSibling(), New);
613b929fe7SIlya Biryukov
623b929fe7SIlya Biryukov P->assertInvariants();
631ad15046SIlya Biryukov }
641ad15046SIlya Biryukov
651ad15046SIlya Biryukov /// Completely remove the node from its parent.
remove(syntax::Node * N)661ad15046SIlya Biryukov static void remove(syntax::Node *N) {
6772732acaSEduardo Caldas assert(N != nullptr);
6872732acaSEduardo Caldas assert(N->Parent != nullptr);
6972732acaSEduardo Caldas assert(N->canModify());
7072732acaSEduardo Caldas
714c14ee61SEduardo Caldas auto *P = N->getParent();
7223657d9cSEduardo Caldas P->replaceChildRangeLowLevel(N, N->getNextSibling(),
731ad15046SIlya Biryukov /*New=*/nullptr);
743b929fe7SIlya Biryukov
753b929fe7SIlya Biryukov P->assertInvariants();
763b929fe7SIlya Biryukov N->assertInvariants();
771ad15046SIlya Biryukov }
781ad15046SIlya Biryukov };
791ad15046SIlya Biryukov
removeStatement(syntax::Arena & A,TokenBufferTokenManager & TBTM,syntax::Statement * S)80*263dcf45SHaojian Wu void syntax::removeStatement(syntax::Arena &A, TokenBufferTokenManager &TBTM,
81*263dcf45SHaojian Wu syntax::Statement *S) {
823b929fe7SIlya Biryukov assert(S);
831ad15046SIlya Biryukov assert(S->canModify());
841ad15046SIlya Biryukov
854c14ee61SEduardo Caldas if (isa<CompoundStatement>(S->getParent())) {
861ad15046SIlya Biryukov // A child of CompoundStatement can just be safely removed.
871ad15046SIlya Biryukov MutationsImpl::remove(S);
881ad15046SIlya Biryukov return;
891ad15046SIlya Biryukov }
901ad15046SIlya Biryukov // For the rest, we have to replace with an empty statement.
911ad15046SIlya Biryukov if (isa<EmptyStatement>(S))
921ad15046SIlya Biryukov return; // already an empty statement, nothing to do.
931ad15046SIlya Biryukov
94*263dcf45SHaojian Wu MutationsImpl::replace(S, createEmptyStatement(A, TBTM));
951ad15046SIlya Biryukov }
96