1*480093f4SDimitry Andric //===- Synthesis.cpp ------------------------------------------*- C++ -*-=====// 2*480093f4SDimitry Andric // 3*480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*480093f4SDimitry Andric // 7*480093f4SDimitry Andric //===----------------------------------------------------------------------===// 8*480093f4SDimitry Andric #include "clang/Tooling/Syntax/BuildTree.h" 9*480093f4SDimitry Andric 10*480093f4SDimitry Andric using namespace clang; 11*480093f4SDimitry Andric 12*480093f4SDimitry Andric /// Exposes private syntax tree APIs required to implement node synthesis. 13*480093f4SDimitry Andric /// Should not be used for anything else. 14*480093f4SDimitry Andric class syntax::FactoryImpl { 15*480093f4SDimitry Andric public: 16*480093f4SDimitry Andric static void setCanModify(syntax::Node *N) { N->CanModify = true; } 17*480093f4SDimitry Andric 18*480093f4SDimitry Andric static void prependChildLowLevel(syntax::Tree *T, syntax::Node *Child, 19*480093f4SDimitry Andric syntax::NodeRole R) { 20*480093f4SDimitry Andric T->prependChildLowLevel(Child, R); 21*480093f4SDimitry Andric } 22*480093f4SDimitry Andric }; 23*480093f4SDimitry Andric 24*480093f4SDimitry Andric clang::syntax::Leaf *syntax::createPunctuation(clang::syntax::Arena &A, 25*480093f4SDimitry Andric clang::tok::TokenKind K) { 26*480093f4SDimitry Andric auto Tokens = A.lexBuffer(llvm::MemoryBuffer::getMemBuffer( 27*480093f4SDimitry Andric clang::tok::getPunctuatorSpelling(K))) 28*480093f4SDimitry Andric .second; 29*480093f4SDimitry Andric assert(Tokens.size() == 1); 30*480093f4SDimitry Andric assert(Tokens.front().kind() == K); 31*480093f4SDimitry Andric auto *L = new (A.allocator()) clang::syntax::Leaf(Tokens.begin()); 32*480093f4SDimitry Andric FactoryImpl::setCanModify(L); 33*480093f4SDimitry Andric L->assertInvariants(); 34*480093f4SDimitry Andric return L; 35*480093f4SDimitry Andric } 36*480093f4SDimitry Andric 37*480093f4SDimitry Andric clang::syntax::EmptyStatement * 38*480093f4SDimitry Andric syntax::createEmptyStatement(clang::syntax::Arena &A) { 39*480093f4SDimitry Andric auto *S = new (A.allocator()) clang::syntax::EmptyStatement; 40*480093f4SDimitry Andric FactoryImpl::setCanModify(S); 41*480093f4SDimitry Andric FactoryImpl::prependChildLowLevel(S, createPunctuation(A, clang::tok::semi), 42*480093f4SDimitry Andric NodeRole::Unknown); 43*480093f4SDimitry Andric S->assertInvariants(); 44*480093f4SDimitry Andric return S; 45*480093f4SDimitry Andric } 46