1480093f4SDimitry Andric //===- Mutations.cpp ------------------------------------------*- C++ -*-=====//
2480093f4SDimitry Andric //
3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6480093f4SDimitry Andric //
7480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8480093f4SDimitry Andric #include "clang/Tooling/Syntax/Mutations.h"
9480093f4SDimitry Andric #include "clang/Basic/LLVM.h"
10480093f4SDimitry Andric #include "clang/Basic/SourceLocation.h"
11480093f4SDimitry Andric #include "clang/Lex/Token.h"
12480093f4SDimitry Andric #include "clang/Tooling/Core/Replacement.h"
13480093f4SDimitry Andric #include "clang/Tooling/Syntax/BuildTree.h"
14480093f4SDimitry Andric #include "clang/Tooling/Syntax/Nodes.h"
15480093f4SDimitry Andric #include "clang/Tooling/Syntax/Tokens.h"
16480093f4SDimitry Andric #include "clang/Tooling/Syntax/Tree.h"
17480093f4SDimitry Andric #include "llvm/ADT/ArrayRef.h"
18480093f4SDimitry Andric #include "llvm/ADT/STLExtras.h"
19480093f4SDimitry Andric #include "llvm/Support/Casting.h"
20480093f4SDimitry Andric #include <cassert>
21480093f4SDimitry Andric #include <string>
22480093f4SDimitry Andric
23480093f4SDimitry Andric using namespace clang;
24480093f4SDimitry Andric
25480093f4SDimitry Andric // This class has access to the internals of tree nodes. Its sole purpose is to
26480093f4SDimitry Andric // define helpers that allow implementing the high-level mutation operations.
27480093f4SDimitry Andric class syntax::MutationsImpl {
28480093f4SDimitry Andric public:
29480093f4SDimitry Andric /// Add a new node with a specified role.
addAfter(syntax::Node * Anchor,syntax::Node * New,NodeRole Role)30480093f4SDimitry Andric static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role) {
31480093f4SDimitry Andric assert(Anchor != nullptr);
32e8d8bef9SDimitry Andric assert(Anchor->Parent != nullptr);
33480093f4SDimitry Andric assert(New->Parent == nullptr);
34480093f4SDimitry Andric assert(New->NextSibling == nullptr);
35e8d8bef9SDimitry Andric assert(New->PreviousSibling == nullptr);
36e8d8bef9SDimitry Andric assert(New->isDetached());
37480093f4SDimitry Andric assert(Role != NodeRole::Detached);
38480093f4SDimitry Andric
395ffd83dbSDimitry Andric New->setRole(Role);
40e8d8bef9SDimitry Andric auto *P = Anchor->getParent();
41e8d8bef9SDimitry Andric P->replaceChildRangeLowLevel(Anchor->getNextSibling(),
42e8d8bef9SDimitry Andric Anchor->getNextSibling(), New);
43480093f4SDimitry Andric
44480093f4SDimitry Andric P->assertInvariants();
45480093f4SDimitry Andric }
46480093f4SDimitry Andric
47480093f4SDimitry Andric /// Replace the node, keeping the role.
replace(syntax::Node * Old,syntax::Node * New)48480093f4SDimitry Andric static void replace(syntax::Node *Old, syntax::Node *New) {
49480093f4SDimitry Andric assert(Old != nullptr);
50480093f4SDimitry Andric assert(Old->Parent != nullptr);
51480093f4SDimitry Andric assert(Old->canModify());
52480093f4SDimitry Andric assert(New->Parent == nullptr);
53480093f4SDimitry Andric assert(New->NextSibling == nullptr);
54e8d8bef9SDimitry Andric assert(New->PreviousSibling == nullptr);
55480093f4SDimitry Andric assert(New->isDetached());
56480093f4SDimitry Andric
57480093f4SDimitry Andric New->Role = Old->Role;
58e8d8bef9SDimitry Andric auto *P = Old->getParent();
59e8d8bef9SDimitry Andric P->replaceChildRangeLowLevel(Old, Old->getNextSibling(), New);
60480093f4SDimitry Andric
61480093f4SDimitry Andric P->assertInvariants();
62480093f4SDimitry Andric }
63480093f4SDimitry Andric
64480093f4SDimitry Andric /// Completely remove the node from its parent.
remove(syntax::Node * N)65480093f4SDimitry Andric static void remove(syntax::Node *N) {
66e8d8bef9SDimitry Andric assert(N != nullptr);
67e8d8bef9SDimitry Andric assert(N->Parent != nullptr);
68e8d8bef9SDimitry Andric assert(N->canModify());
69e8d8bef9SDimitry Andric
70e8d8bef9SDimitry Andric auto *P = N->getParent();
71e8d8bef9SDimitry Andric P->replaceChildRangeLowLevel(N, N->getNextSibling(),
72480093f4SDimitry Andric /*New=*/nullptr);
73480093f4SDimitry Andric
74480093f4SDimitry Andric P->assertInvariants();
75480093f4SDimitry Andric N->assertInvariants();
76480093f4SDimitry Andric }
77480093f4SDimitry Andric };
78480093f4SDimitry Andric
removeStatement(syntax::Arena & A,TokenBufferTokenManager & TBTM,syntax::Statement * S)79*fcaf7f86SDimitry Andric void syntax::removeStatement(syntax::Arena &A, TokenBufferTokenManager &TBTM,
80*fcaf7f86SDimitry Andric syntax::Statement *S) {
81480093f4SDimitry Andric assert(S);
82480093f4SDimitry Andric assert(S->canModify());
83480093f4SDimitry Andric
84e8d8bef9SDimitry Andric if (isa<CompoundStatement>(S->getParent())) {
85480093f4SDimitry Andric // A child of CompoundStatement can just be safely removed.
86480093f4SDimitry Andric MutationsImpl::remove(S);
87480093f4SDimitry Andric return;
88480093f4SDimitry Andric }
89480093f4SDimitry Andric // For the rest, we have to replace with an empty statement.
90480093f4SDimitry Andric if (isa<EmptyStatement>(S))
91480093f4SDimitry Andric return; // already an empty statement, nothing to do.
92480093f4SDimitry Andric
93*fcaf7f86SDimitry Andric MutationsImpl::replace(S, createEmptyStatement(A, TBTM));
94480093f4SDimitry Andric }
95