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