clang 22.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"
13#include <cassert>
14
15using namespace clang;
16
17// This class has access to the internals of tree nodes. Its sole purpose is to
18// define helpers that allow implementing the high-level mutation operations.
20public:
21 /// Add a new node with a specified role.
22 static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role) {
23 assert(Anchor != nullptr);
24 assert(Anchor->Parent != nullptr);
25 assert(New->Parent == nullptr);
26 assert(New->NextSibling == nullptr);
27 assert(New->PreviousSibling == nullptr);
28 assert(New->isDetached());
29 assert(Role != NodeRole::Detached);
30
31 New->setRole(Role);
32 auto *P = Anchor->getParent();
33 P->replaceChildRangeLowLevel(Anchor->getNextSibling(),
34 Anchor->getNextSibling(), New);
35
36 P->assertInvariants();
37 }
38
39 /// Replace the node, keeping the role.
40 static void replace(syntax::Node *Old, syntax::Node *New) {
41 assert(Old != nullptr);
42 assert(Old->Parent != nullptr);
43 assert(Old->canModify());
44 assert(New->Parent == nullptr);
45 assert(New->NextSibling == nullptr);
46 assert(New->PreviousSibling == nullptr);
47 assert(New->isDetached());
48
49 New->Role = Old->Role;
50 auto *P = Old->getParent();
51 P->replaceChildRangeLowLevel(Old, Old->getNextSibling(), New);
52
53 P->assertInvariants();
54 }
55
56 /// Completely remove the node from its parent.
57 static void remove(syntax::Node *N) {
58 assert(N != nullptr);
59 assert(N->Parent != nullptr);
60 assert(N->canModify());
61
62 auto *P = N->getParent();
63 P->replaceChildRangeLowLevel(N, N->getNextSibling(),
64 /*New=*/nullptr);
65
66 P->assertInvariants();
68 }
69};
70
73 assert(S);
74 assert(S->canModify());
75
76 if (isa<CompoundStatement>(S->getParent())) {
77 // A child of CompoundStatement can just be safely removed.
78 MutationsImpl::remove(S);
79 return;
80 }
81 // For the rest, we have to replace with an empty statement.
82 if (isa<EmptyStatement>(S))
83 return; // already an empty statement, nothing to do.
84
85 MutationsImpl::replace(S, createEmptyStatement(A, TBTM));
86}
StringRef P
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
A memory arena for syntax trees.
Definition: Tree.h:36
A node in a syntax tree.
Definition: Tree.h:54
void assertInvariants() const
Asserts invariants on this node of the tree and its immediate children.
Definition: Tree.cpp:238
bool canModify() const
If this function return false, the tree cannot be modified because there is no reasonable way to prod...
Definition: Tree.h:88
const Node * getNextSibling() const
Definition: Tree.h:93
const Tree * getParent() const
Definition: Tree.h:90
An abstract node for C++ statements, e.g.
Definition: Nodes.h:209
A TokenBuffer-powered token manager.
static void addAfter(syntax::Node *Anchor, syntax::Node *New, NodeRole Role)
Add a new node with a specified role.
Definition: Mutations.cpp:22
static void remove(syntax::Node *N)
Completely remove the node from its parent.
Definition: Mutations.cpp:57
static void replace(syntax::Node *Old, syntax::Node *New)
Replace the node, keeping the role.
Definition: Mutations.cpp:40
NodeRole
A relation between a parent and child node, e.g.
Definition: Nodes.h:54
@ Detached
A node without a parent.
syntax::EmptyStatement * createEmptyStatement(syntax::Arena &A, TokenBufferTokenManager &TBTM)
Definition: Synthesis.cpp:235
void removeStatement(syntax::Arena &A, TokenBufferTokenManager &TBTM, syntax::Statement *S)
Removes a statement or replaces it with an empty statement where one is required syntactically.
Definition: Mutations.cpp:71
The JSON file list parser is used to communicate input to InstallAPI.