clang  14.0.0git
Mutations.cpp
Go to the documentation of this file.
1 //===- Mutations.cpp ------------------------------------------*- C++ -*-=====//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "clang/Basic/LLVM.h"
11 #include "clang/Lex/Token.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Casting.h"
21 #include <cassert>
22 #include <string>
23 
24 using namespace clang;
25 
26 // This class has access to the internals of tree nodes. Its sole purpose is to
27 // define helpers that allow implementing the high-level mutation operations.
29 public:
30  /// Add a new node with a specified role.
31  static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role) {
32  assert(Anchor != nullptr);
33  assert(Anchor->Parent != nullptr);
34  assert(New->Parent == nullptr);
35  assert(New->NextSibling == nullptr);
36  assert(New->PreviousSibling == nullptr);
37  assert(New->isDetached());
38  assert(Role != NodeRole::Detached);
39 
40  New->setRole(Role);
41  auto *P = Anchor->getParent();
42  P->replaceChildRangeLowLevel(Anchor->getNextSibling(),
43  Anchor->getNextSibling(), New);
44 
45  P->assertInvariants();
46  }
47 
48  /// Replace the node, keeping the role.
49  static void replace(syntax::Node *Old, syntax::Node *New) {
50  assert(Old != nullptr);
51  assert(Old->Parent != nullptr);
52  assert(Old->canModify());
53  assert(New->Parent == nullptr);
54  assert(New->NextSibling == nullptr);
55  assert(New->PreviousSibling == nullptr);
56  assert(New->isDetached());
57 
58  New->Role = Old->Role;
59  auto *P = Old->getParent();
60  P->replaceChildRangeLowLevel(Old, Old->getNextSibling(), New);
61 
62  P->assertInvariants();
63  }
64 
65  /// Completely remove the node from its parent.
66  static void remove(syntax::Node *N) {
67  assert(N != nullptr);
68  assert(N->Parent != nullptr);
69  assert(N->canModify());
70 
71  auto *P = N->getParent();
72  P->replaceChildRangeLowLevel(N, N->getNextSibling(),
73  /*New=*/nullptr);
74 
75  P->assertInvariants();
76  N->assertInvariants();
77  }
78 };
79 
81  assert(S);
82  assert(S->canModify());
83 
84  if (isa<CompoundStatement>(S->getParent())) {
85  // A child of CompoundStatement can just be safely removed.
87  return;
88  }
89  // For the rest, we have to replace with an empty statement.
90  if (isa<EmptyStatement>(S))
91  return; // already an empty statement, nothing to do.
92 
93  MutationsImpl::replace(S, createEmptyStatement(A));
94 }
clang::syntax::Node::getParent
const Tree * getParent() const
Definition: Tree.h:116
clang::syntax::Node
A node in a syntax tree.
Definition: Tree.h:80
clang::syntax::NodeRole::Detached
@ Detached
A node without a parent.
BuildTree.h
clang::syntax::Node::canModify
bool canModify() const
If this function return false, the tree cannot be modified because there is no reasonable way to prod...
Definition: Tree.h:114
clang::syntax::MutationsImpl
Definition: Mutations.cpp:28
clang::syntax::MutationsImpl::addAfter
static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role)
Add a new node with a specified role.
Definition: Mutations.cpp:31
clang::syntax::Node::isDetached
bool isDetached() const
Whether the node is detached from a tree, i.e. does not have a parent.
Definition: Tree.cpp:62
clang::syntax::MutationsImpl::replace
static void replace(syntax::Node *Old, syntax::Node *New)
Replace the node, keeping the role.
Definition: Mutations.cpp:49
clang::syntax::removeStatement
void removeStatement(syntax::Arena &A, syntax::Statement *S)
Removes a statement or replaces it with an empty statement where one is required syntactically.
Definition: Mutations.cpp:80
clang::transformer::remove
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: RewriteRule.cpp:146
clang::syntax::MutationsImpl::remove
static void remove(syntax::Node *N)
Completely remove the node from its parent.
Definition: Mutations.cpp:66
SourceLocation.h
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
Tokens.h
Replacement.h
clang::syntax::Arena
A memory arena for syntax trees.
Definition: Tree.h:41
LLVM.h
clang::syntax::createEmptyStatement
syntax::EmptyStatement * createEmptyStatement(syntax::Arena &A)
Definition: Synthesis.cpp:227
Tree.h
Nodes.h
clang::syntax::NodeRole
NodeRole
A relation between a parent and child node, e.g.
Definition: Nodes.h:59
clang
Definition: CalledOnceCheck.h:17
clang::syntax::Node::getNextSibling
const Node * getNextSibling() const
Definition: Tree.h:119
Token.h
clang::syntax::Node::assertInvariants
void assertInvariants() const
Asserts invariants on this node of the tree and its immediate children.
Definition: Tree.cpp:269
clang::syntax::Statement
An abstract node for C++ statements, e.g.
Definition: Nodes.h:214
Mutations.h