1 //===- BuildTree.h - build syntax trees -----------------------*- 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 // Functions to construct a syntax tree from an AST.
9 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_TOOLING_SYNTAX_BUILDTREE_H
11 #define LLVM_CLANG_TOOLING_SYNTAX_BUILDTREE_H
12 
13 #include "clang/AST/Decl.h"
14 #include "clang/Basic/TokenKinds.h"
15 #include "clang/Tooling/Syntax/Nodes.h"
16 #include "clang/Tooling/Syntax/TokenBufferTokenManager.h"
17 #include "clang/Tooling/Syntax/Tree.h"
18 
19 namespace clang {
20 namespace syntax {
21 
22 /// Build a syntax tree for the main file.
23 /// This usually covers the whole TranslationUnitDecl, but can be restricted by
24 /// the ASTContext's traversal scope.
25 syntax::TranslationUnit *
26 buildSyntaxTree(Arena &A, TokenBufferTokenManager &TBTM, ASTContext &Context);
27 
28 // Create syntax trees from subtrees not backed by the source code.
29 
30 // Synthesis of Leafs
31 /// Create `Leaf` from token with `Spelling` and assert it has the desired
32 /// `TokenKind`.
33 syntax::Leaf *createLeaf(syntax::Arena &A, TokenBufferTokenManager &TBTM,
34                          tok::TokenKind K, StringRef Spelling);
35 
36 /// Infer the token spelling from its `TokenKind`, then create `Leaf` from
37 /// this token
38 syntax::Leaf *createLeaf(syntax::Arena &A, TokenBufferTokenManager &TBTM,
39                          tok::TokenKind K);
40 
41 // Synthesis of Trees
42 /// Creates the concrete syntax node according to the specified `NodeKind` `K`.
43 /// Returns it as a pointer to the base class `Tree`.
44 syntax::Tree *
45 createTree(syntax::Arena &A,
46            ArrayRef<std::pair<syntax::Node *, syntax::NodeRole>> Children,
47            syntax::NodeKind K);
48 
49 // Synthesis of Syntax Nodes
50 syntax::EmptyStatement *createEmptyStatement(syntax::Arena &A,
51                                              TokenBufferTokenManager &TBTM);
52 
53 /// Creates a completely independent copy of `N` with its macros expanded.
54 ///
55 /// The copy is:
56 /// * Detached, i.e. `Parent == NextSibling == nullptr` and
57 /// `Role == Detached`.
58 /// * Synthesized, i.e. `Original == false`.
59 syntax::Node *deepCopyExpandingMacros(syntax::Arena &A,
60                                       TokenBufferTokenManager &TBTM,
61                                       const syntax::Node *N);
62 } // namespace syntax
63 } // namespace clang
64 #endif
65