xref: /freebsd/contrib/llvm-project/clang/lib/Tooling/Syntax/Synthesis.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 //===- Synthesis.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 //===----------------------------------------------------------------------===//
8 #include "clang/Tooling/Syntax/BuildTree.h"
9 
10 using namespace clang;
11 
12 /// Exposes private syntax tree APIs required to implement node synthesis.
13 /// Should not be used for anything else.
14 class syntax::FactoryImpl {
15 public:
16   static void setCanModify(syntax::Node *N) { N->CanModify = true; }
17 
18   static void prependChildLowLevel(syntax::Tree *T, syntax::Node *Child,
19                                    syntax::NodeRole R) {
20     T->prependChildLowLevel(Child, R);
21   }
22 };
23 
24 clang::syntax::Leaf *syntax::createPunctuation(clang::syntax::Arena &A,
25                                                clang::tok::TokenKind K) {
26   auto Tokens = A.lexBuffer(llvm::MemoryBuffer::getMemBuffer(
27                                 clang::tok::getPunctuatorSpelling(K)))
28                     .second;
29   assert(Tokens.size() == 1);
30   assert(Tokens.front().kind() == K);
31   auto *L = new (A.allocator()) clang::syntax::Leaf(Tokens.begin());
32   FactoryImpl::setCanModify(L);
33   L->assertInvariants();
34   return L;
35 }
36 
37 clang::syntax::EmptyStatement *
38 syntax::createEmptyStatement(clang::syntax::Arena &A) {
39   auto *S = new (A.allocator()) clang::syntax::EmptyStatement;
40   FactoryImpl::setCanModify(S);
41   FactoryImpl::prependChildLowLevel(S, createPunctuation(A, clang::tok::semi),
42                                     NodeRole::Unknown);
43   S->assertInvariants();
44   return S;
45 }
46