19b3f38f9SIlya Biryukov //===- BuildTree.cpp ------------------------------------------*- C++ -*-=====// 29b3f38f9SIlya Biryukov // 39b3f38f9SIlya Biryukov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 49b3f38f9SIlya Biryukov // See https://llvm.org/LICENSE.txt for license information. 59b3f38f9SIlya Biryukov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 69b3f38f9SIlya Biryukov // 79b3f38f9SIlya Biryukov //===----------------------------------------------------------------------===// 89b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/BuildTree.h" 9e702bdb8SIlya Biryukov #include "clang/AST/Decl.h" 10e702bdb8SIlya Biryukov #include "clang/AST/DeclBase.h" 119b3f38f9SIlya Biryukov #include "clang/AST/RecursiveASTVisitor.h" 129b3f38f9SIlya Biryukov #include "clang/AST/Stmt.h" 139b3f38f9SIlya Biryukov #include "clang/Basic/LLVM.h" 149b3f38f9SIlya Biryukov #include "clang/Basic/SourceLocation.h" 159b3f38f9SIlya Biryukov #include "clang/Basic/SourceManager.h" 169b3f38f9SIlya Biryukov #include "clang/Basic/TokenKinds.h" 179b3f38f9SIlya Biryukov #include "clang/Lex/Lexer.h" 189b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/Nodes.h" 199b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/Tokens.h" 209b3f38f9SIlya Biryukov #include "clang/Tooling/Syntax/Tree.h" 219b3f38f9SIlya Biryukov #include "llvm/ADT/ArrayRef.h" 229b3f38f9SIlya Biryukov #include "llvm/ADT/STLExtras.h" 239b3f38f9SIlya Biryukov #include "llvm/ADT/SmallVector.h" 249b3f38f9SIlya Biryukov #include "llvm/Support/Allocator.h" 259b3f38f9SIlya Biryukov #include "llvm/Support/Casting.h" 2696065cf7SIlya Biryukov #include "llvm/Support/Compiler.h" 279b3f38f9SIlya Biryukov #include "llvm/Support/FormatVariadic.h" 281ad15046SIlya Biryukov #include "llvm/Support/MemoryBuffer.h" 299b3f38f9SIlya Biryukov #include "llvm/Support/raw_ostream.h" 309b3f38f9SIlya Biryukov #include <map> 319b3f38f9SIlya Biryukov 329b3f38f9SIlya Biryukov using namespace clang; 339b3f38f9SIlya Biryukov 3496065cf7SIlya Biryukov LLVM_ATTRIBUTE_UNUSED 3558fa50f4SIlya Biryukov static bool isImplicitExpr(clang::Expr *E) { return E->IgnoreImplicit() != E; } 3658fa50f4SIlya Biryukov 379b3f38f9SIlya Biryukov /// A helper class for constructing the syntax tree while traversing a clang 389b3f38f9SIlya Biryukov /// AST. 399b3f38f9SIlya Biryukov /// 409b3f38f9SIlya Biryukov /// At each point of the traversal we maintain a list of pending nodes. 419b3f38f9SIlya Biryukov /// Initially all tokens are added as pending nodes. When processing a clang AST 429b3f38f9SIlya Biryukov /// node, the clients need to: 439b3f38f9SIlya Biryukov /// - create a corresponding syntax node, 449b3f38f9SIlya Biryukov /// - assign roles to all pending child nodes with 'markChild' and 459b3f38f9SIlya Biryukov /// 'markChildToken', 469b3f38f9SIlya Biryukov /// - replace the child nodes with the new syntax node in the pending list 479b3f38f9SIlya Biryukov /// with 'foldNode'. 489b3f38f9SIlya Biryukov /// 499b3f38f9SIlya Biryukov /// Note that all children are expected to be processed when building a node. 509b3f38f9SIlya Biryukov /// 519b3f38f9SIlya Biryukov /// Call finalize() to finish building the tree and consume the root node. 529b3f38f9SIlya Biryukov class syntax::TreeBuilder { 539b3f38f9SIlya Biryukov public: 54*c1bbefefSIlya Biryukov TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) { 55*c1bbefefSIlya Biryukov for (const auto &T : Arena.tokenBuffer().expandedTokens()) 56*c1bbefefSIlya Biryukov LocationToToken.insert({T.location().getRawEncoding(), &T}); 57*c1bbefefSIlya Biryukov } 589b3f38f9SIlya Biryukov 599b3f38f9SIlya Biryukov llvm::BumpPtrAllocator &allocator() { return Arena.allocator(); } 609b3f38f9SIlya Biryukov 619b3f38f9SIlya Biryukov /// Populate children for \p New node, assuming it covers tokens from \p 629b3f38f9SIlya Biryukov /// Range. 639b3f38f9SIlya Biryukov void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree *New); 649b3f38f9SIlya Biryukov 65e702bdb8SIlya Biryukov /// Must be called with the range of each `DeclaratorDecl`. Ensures the 66e702bdb8SIlya Biryukov /// corresponding declarator nodes are covered by `SimpleDeclaration`. 67e702bdb8SIlya Biryukov void noticeDeclaratorRange(llvm::ArrayRef<syntax::Token> Range); 68e702bdb8SIlya Biryukov 69e702bdb8SIlya Biryukov /// Notifies that we should not consume trailing semicolon when computing 70e702bdb8SIlya Biryukov /// token range of \p D. 71e702bdb8SIlya Biryukov void noticeDeclaratorWithoutSemicolon(Decl *D); 72e702bdb8SIlya Biryukov 7358fa50f4SIlya Biryukov /// Mark the \p Child node with a corresponding \p Role. All marked children 7458fa50f4SIlya Biryukov /// should be consumed by foldNode. 7558fa50f4SIlya Biryukov /// (!) when called on expressions (clang::Expr is derived from clang::Stmt), 7658fa50f4SIlya Biryukov /// wraps expressions into expression statement. 7758fa50f4SIlya Biryukov void markStmtChild(Stmt *Child, NodeRole Role); 7858fa50f4SIlya Biryukov /// Should be called for expressions in non-statement position to avoid 7958fa50f4SIlya Biryukov /// wrapping into expression statement. 8058fa50f4SIlya Biryukov void markExprChild(Expr *Child, NodeRole Role); 8158fa50f4SIlya Biryukov 829b3f38f9SIlya Biryukov /// Set role for a token starting at \p Loc. 83def65bb4SIlya Biryukov void markChildToken(SourceLocation Loc, NodeRole R); 849b3f38f9SIlya Biryukov 859b3f38f9SIlya Biryukov /// Finish building the tree and consume the root node. 869b3f38f9SIlya Biryukov syntax::TranslationUnit *finalize() && { 879b3f38f9SIlya Biryukov auto Tokens = Arena.tokenBuffer().expandedTokens(); 88bfbf6b6cSIlya Biryukov assert(!Tokens.empty()); 89bfbf6b6cSIlya Biryukov assert(Tokens.back().kind() == tok::eof); 90bfbf6b6cSIlya Biryukov 919b3f38f9SIlya Biryukov // Build the root of the tree, consuming all the children. 921ad15046SIlya Biryukov Pending.foldChildren(Arena, Tokens.drop_back(), 939b3f38f9SIlya Biryukov new (Arena.allocator()) syntax::TranslationUnit); 949b3f38f9SIlya Biryukov 959b3f38f9SIlya Biryukov return cast<syntax::TranslationUnit>(std::move(Pending).finalize()); 969b3f38f9SIlya Biryukov } 979b3f38f9SIlya Biryukov 989b3f38f9SIlya Biryukov /// getRange() finds the syntax tokens corresponding to the passed source 999b3f38f9SIlya Biryukov /// locations. 1009b3f38f9SIlya Biryukov /// \p First is the start position of the first token and \p Last is the start 1019b3f38f9SIlya Biryukov /// position of the last token. 1029b3f38f9SIlya Biryukov llvm::ArrayRef<syntax::Token> getRange(SourceLocation First, 1039b3f38f9SIlya Biryukov SourceLocation Last) const { 1049b3f38f9SIlya Biryukov assert(First.isValid()); 1059b3f38f9SIlya Biryukov assert(Last.isValid()); 1069b3f38f9SIlya Biryukov assert(First == Last || 1079b3f38f9SIlya Biryukov Arena.sourceManager().isBeforeInTranslationUnit(First, Last)); 1089b3f38f9SIlya Biryukov return llvm::makeArrayRef(findToken(First), std::next(findToken(Last))); 1099b3f38f9SIlya Biryukov } 1109b3f38f9SIlya Biryukov llvm::ArrayRef<syntax::Token> getRange(const Decl *D) const { 111e702bdb8SIlya Biryukov auto Tokens = getRange(D->getBeginLoc(), D->getEndLoc()); 112e702bdb8SIlya Biryukov if (llvm::isa<NamespaceDecl>(D)) 113e702bdb8SIlya Biryukov return Tokens; 114e702bdb8SIlya Biryukov if (DeclsWithoutSemicolons.count(D)) 115e702bdb8SIlya Biryukov return Tokens; 116e702bdb8SIlya Biryukov // FIXME: do not consume trailing semicolon on function definitions. 117e702bdb8SIlya Biryukov // Most declarations own a semicolon in syntax trees, but not in clang AST. 118e702bdb8SIlya Biryukov return withTrailingSemicolon(Tokens); 1199b3f38f9SIlya Biryukov } 12058fa50f4SIlya Biryukov llvm::ArrayRef<syntax::Token> getExprRange(const Expr *E) const { 12158fa50f4SIlya Biryukov return getRange(E->getBeginLoc(), E->getEndLoc()); 12258fa50f4SIlya Biryukov } 12358fa50f4SIlya Biryukov /// Find the adjusted range for the statement, consuming the trailing 12458fa50f4SIlya Biryukov /// semicolon when needed. 12558fa50f4SIlya Biryukov llvm::ArrayRef<syntax::Token> getStmtRange(const Stmt *S) const { 12658fa50f4SIlya Biryukov auto Tokens = getRange(S->getBeginLoc(), S->getEndLoc()); 12758fa50f4SIlya Biryukov if (isa<CompoundStmt>(S)) 12858fa50f4SIlya Biryukov return Tokens; 12958fa50f4SIlya Biryukov 13058fa50f4SIlya Biryukov // Some statements miss a trailing semicolon, e.g. 'return', 'continue' and 13158fa50f4SIlya Biryukov // all statements that end with those. Consume this semicolon here. 132e702bdb8SIlya Biryukov if (Tokens.back().kind() == tok::semi) 133e702bdb8SIlya Biryukov return Tokens; 134e702bdb8SIlya Biryukov return withTrailingSemicolon(Tokens); 135e702bdb8SIlya Biryukov } 136e702bdb8SIlya Biryukov 137e702bdb8SIlya Biryukov private: 138e702bdb8SIlya Biryukov llvm::ArrayRef<syntax::Token> 139e702bdb8SIlya Biryukov withTrailingSemicolon(llvm::ArrayRef<syntax::Token> Tokens) const { 140e702bdb8SIlya Biryukov assert(!Tokens.empty()); 141e702bdb8SIlya Biryukov assert(Tokens.back().kind() != tok::eof); 142e702bdb8SIlya Biryukov // (!) we never consume 'eof', so looking at the next token is ok. 14358fa50f4SIlya Biryukov if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi) 14458fa50f4SIlya Biryukov return llvm::makeArrayRef(Tokens.begin(), Tokens.end() + 1); 14558fa50f4SIlya Biryukov return Tokens; 1469b3f38f9SIlya Biryukov } 1479b3f38f9SIlya Biryukov 1489b3f38f9SIlya Biryukov /// Finds a token starting at \p L. The token must exist. 1499b3f38f9SIlya Biryukov const syntax::Token *findToken(SourceLocation L) const; 1509b3f38f9SIlya Biryukov 1519b3f38f9SIlya Biryukov /// A collection of trees covering the input tokens. 1529b3f38f9SIlya Biryukov /// When created, each tree corresponds to a single token in the file. 1539b3f38f9SIlya Biryukov /// Clients call 'foldChildren' to attach one or more subtrees to a parent 1549b3f38f9SIlya Biryukov /// node and update the list of trees accordingly. 1559b3f38f9SIlya Biryukov /// 1569b3f38f9SIlya Biryukov /// Ensures that added nodes properly nest and cover the whole token stream. 1579b3f38f9SIlya Biryukov struct Forest { 1589b3f38f9SIlya Biryukov Forest(syntax::Arena &A) { 159bfbf6b6cSIlya Biryukov assert(!A.tokenBuffer().expandedTokens().empty()); 160bfbf6b6cSIlya Biryukov assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof); 1619b3f38f9SIlya Biryukov // Create all leaf nodes. 162bfbf6b6cSIlya Biryukov // Note that we do not have 'eof' in the tree. 1631ad15046SIlya Biryukov for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) { 1641ad15046SIlya Biryukov auto *L = new (A.allocator()) syntax::Leaf(&T); 1651ad15046SIlya Biryukov L->Original = true; 1661ad15046SIlya Biryukov L->CanModify = A.tokenBuffer().spelledForExpanded(T).hasValue(); 1671ad15046SIlya Biryukov Trees.insert(Trees.end(), {&T, NodeAndRole{L}}); 1681ad15046SIlya Biryukov } 1699b3f38f9SIlya Biryukov } 1709b3f38f9SIlya Biryukov 171e702bdb8SIlya Biryukov ~Forest() { assert(DelayedFolds.empty()); } 172e702bdb8SIlya Biryukov 1739b3f38f9SIlya Biryukov void assignRole(llvm::ArrayRef<syntax::Token> Range, 1749b3f38f9SIlya Biryukov syntax::NodeRole Role) { 1759b3f38f9SIlya Biryukov assert(!Range.empty()); 1769b3f38f9SIlya Biryukov auto It = Trees.lower_bound(Range.begin()); 1779b3f38f9SIlya Biryukov assert(It != Trees.end() && "no node found"); 1789b3f38f9SIlya Biryukov assert(It->first == Range.begin() && "no child with the specified range"); 1799b3f38f9SIlya Biryukov assert((std::next(It) == Trees.end() || 1809b3f38f9SIlya Biryukov std::next(It)->first == Range.end()) && 1819b3f38f9SIlya Biryukov "no child with the specified range"); 1829b3f38f9SIlya Biryukov It->second.Role = Role; 1839b3f38f9SIlya Biryukov } 1849b3f38f9SIlya Biryukov 185e702bdb8SIlya Biryukov /// Add \p Node to the forest and attach child nodes based on \p Tokens. 1861ad15046SIlya Biryukov void foldChildren(const syntax::Arena &A, 1871ad15046SIlya Biryukov llvm::ArrayRef<syntax::Token> Tokens, 1889b3f38f9SIlya Biryukov syntax::Tree *Node) { 189e702bdb8SIlya Biryukov // Execute delayed folds inside `Tokens`. 190e702bdb8SIlya Biryukov auto BeginExecuted = DelayedFolds.lower_bound(Tokens.begin()); 191e702bdb8SIlya Biryukov auto It = BeginExecuted; 192e702bdb8SIlya Biryukov for (; It != DelayedFolds.end() && It->second.End <= Tokens.end(); ++It) 1931ad15046SIlya Biryukov foldChildrenEager(A, llvm::makeArrayRef(It->first, It->second.End), 194e702bdb8SIlya Biryukov It->second.Node); 195e702bdb8SIlya Biryukov DelayedFolds.erase(BeginExecuted, It); 1969b3f38f9SIlya Biryukov 197e702bdb8SIlya Biryukov // Attach children to `Node`. 1981ad15046SIlya Biryukov foldChildrenEager(A, Tokens, Node); 199e702bdb8SIlya Biryukov } 2009b3f38f9SIlya Biryukov 201e702bdb8SIlya Biryukov /// Schedule a call to `foldChildren` that will only be executed when 202e702bdb8SIlya Biryukov /// containing node is folded. The range of delayed nodes can be extended by 203e702bdb8SIlya Biryukov /// calling `extendDelayedFold`. Only one delayed node for each starting 204e702bdb8SIlya Biryukov /// token is allowed. 205e702bdb8SIlya Biryukov void foldChildrenDelayed(llvm::ArrayRef<syntax::Token> Tokens, 206e702bdb8SIlya Biryukov syntax::Tree *Node) { 207e702bdb8SIlya Biryukov assert(!Tokens.empty()); 208e702bdb8SIlya Biryukov bool Inserted = 209e702bdb8SIlya Biryukov DelayedFolds.insert({Tokens.begin(), DelayedFold{Tokens.end(), Node}}) 210e702bdb8SIlya Biryukov .second; 211e702bdb8SIlya Biryukov (void)Inserted; 212e702bdb8SIlya Biryukov assert(Inserted && "Multiple delayed folds start at the same token"); 213e702bdb8SIlya Biryukov } 2149b3f38f9SIlya Biryukov 215e702bdb8SIlya Biryukov /// If there a delayed fold, starting at `ExtendedRange.begin()`, extends 216e702bdb8SIlya Biryukov /// its endpoint to `ExtendedRange.end()` and returns true. 217e702bdb8SIlya Biryukov /// Otherwise, returns false. 218e702bdb8SIlya Biryukov bool extendDelayedFold(llvm::ArrayRef<syntax::Token> ExtendedRange) { 219e702bdb8SIlya Biryukov assert(!ExtendedRange.empty()); 220e702bdb8SIlya Biryukov auto It = DelayedFolds.find(ExtendedRange.data()); 221e702bdb8SIlya Biryukov if (It == DelayedFolds.end()) 222e702bdb8SIlya Biryukov return false; 223e702bdb8SIlya Biryukov assert(It->second.End <= ExtendedRange.end()); 224e702bdb8SIlya Biryukov It->second.End = ExtendedRange.end(); 225e702bdb8SIlya Biryukov return true; 2269b3f38f9SIlya Biryukov } 2279b3f38f9SIlya Biryukov 2289b3f38f9SIlya Biryukov // EXPECTS: all tokens were consumed and are owned by a single root node. 2299b3f38f9SIlya Biryukov syntax::Node *finalize() && { 2309b3f38f9SIlya Biryukov assert(Trees.size() == 1); 2319b3f38f9SIlya Biryukov auto *Root = Trees.begin()->second.Node; 2329b3f38f9SIlya Biryukov Trees = {}; 2339b3f38f9SIlya Biryukov return Root; 2349b3f38f9SIlya Biryukov } 2359b3f38f9SIlya Biryukov 2369b3f38f9SIlya Biryukov std::string str(const syntax::Arena &A) const { 2379b3f38f9SIlya Biryukov std::string R; 2389b3f38f9SIlya Biryukov for (auto It = Trees.begin(); It != Trees.end(); ++It) { 2399b3f38f9SIlya Biryukov unsigned CoveredTokens = 2409b3f38f9SIlya Biryukov It != Trees.end() 2419b3f38f9SIlya Biryukov ? (std::next(It)->first - It->first) 2429b3f38f9SIlya Biryukov : A.tokenBuffer().expandedTokens().end() - It->first; 2439b3f38f9SIlya Biryukov 2449b3f38f9SIlya Biryukov R += llvm::formatv("- '{0}' covers '{1}'+{2} tokens\n", 2459b3f38f9SIlya Biryukov It->second.Node->kind(), 2469b3f38f9SIlya Biryukov It->first->text(A.sourceManager()), CoveredTokens); 2479b3f38f9SIlya Biryukov R += It->second.Node->dump(A); 2489b3f38f9SIlya Biryukov } 2499b3f38f9SIlya Biryukov return R; 2509b3f38f9SIlya Biryukov } 2519b3f38f9SIlya Biryukov 2529b3f38f9SIlya Biryukov private: 253e702bdb8SIlya Biryukov /// Implementation detail of `foldChildren`, does acutal folding ignoring 254e702bdb8SIlya Biryukov /// delayed folds. 2551ad15046SIlya Biryukov void foldChildrenEager(const syntax::Arena &A, 2561ad15046SIlya Biryukov llvm::ArrayRef<syntax::Token> Tokens, 257e702bdb8SIlya Biryukov syntax::Tree *Node) { 258e702bdb8SIlya Biryukov assert(Node->firstChild() == nullptr && "node already has children"); 259e702bdb8SIlya Biryukov 260e702bdb8SIlya Biryukov auto *FirstToken = Tokens.begin(); 261e702bdb8SIlya Biryukov auto BeginChildren = Trees.lower_bound(FirstToken); 262e702bdb8SIlya Biryukov assert((BeginChildren == Trees.end() || 263e702bdb8SIlya Biryukov BeginChildren->first == FirstToken) && 264e702bdb8SIlya Biryukov "fold crosses boundaries of existing subtrees"); 265e702bdb8SIlya Biryukov auto EndChildren = Trees.lower_bound(Tokens.end()); 266e702bdb8SIlya Biryukov assert( 267e702bdb8SIlya Biryukov (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) && 268e702bdb8SIlya Biryukov "fold crosses boundaries of existing subtrees"); 269e702bdb8SIlya Biryukov 270e702bdb8SIlya Biryukov // (!) we need to go in reverse order, because we can only prepend. 271e702bdb8SIlya Biryukov for (auto It = EndChildren; It != BeginChildren; --It) 272e702bdb8SIlya Biryukov Node->prependChildLowLevel(std::prev(It)->second.Node, 273e702bdb8SIlya Biryukov std::prev(It)->second.Role); 274e702bdb8SIlya Biryukov 2751ad15046SIlya Biryukov // Mark that this node came from the AST and is backed by the source code. 2761ad15046SIlya Biryukov Node->Original = true; 2771ad15046SIlya Biryukov Node->CanModify = A.tokenBuffer().spelledForExpanded(Tokens).hasValue(); 2781ad15046SIlya Biryukov 279e702bdb8SIlya Biryukov Trees.erase(BeginChildren, EndChildren); 280e702bdb8SIlya Biryukov Trees.insert({FirstToken, NodeAndRole(Node)}); 281e702bdb8SIlya Biryukov } 2829b3f38f9SIlya Biryukov /// A with a role that should be assigned to it when adding to a parent. 2839b3f38f9SIlya Biryukov struct NodeAndRole { 2849b3f38f9SIlya Biryukov explicit NodeAndRole(syntax::Node *Node) 28551dad419SIlya Biryukov : Node(Node), Role(NodeRole::Unknown) {} 2869b3f38f9SIlya Biryukov 2879b3f38f9SIlya Biryukov syntax::Node *Node; 2889b3f38f9SIlya Biryukov NodeRole Role; 2899b3f38f9SIlya Biryukov }; 2909b3f38f9SIlya Biryukov 2919b3f38f9SIlya Biryukov /// Maps from the start token to a subtree starting at that token. 292302cb3bcSIlya Biryukov /// Keys in the map are pointers into the array of expanded tokens, so 293302cb3bcSIlya Biryukov /// pointer order corresponds to the order of preprocessor tokens. 2949b3f38f9SIlya Biryukov /// FIXME: storing the end tokens is redundant. 2959b3f38f9SIlya Biryukov /// FIXME: the key of a map is redundant, it is also stored in NodeForRange. 2969b3f38f9SIlya Biryukov std::map<const syntax::Token *, NodeAndRole> Trees; 297e702bdb8SIlya Biryukov 298e702bdb8SIlya Biryukov /// See documentation of `foldChildrenDelayed` for details. 299e702bdb8SIlya Biryukov struct DelayedFold { 300e702bdb8SIlya Biryukov const syntax::Token *End = nullptr; 301e702bdb8SIlya Biryukov syntax::Tree *Node = nullptr; 302e702bdb8SIlya Biryukov }; 303e702bdb8SIlya Biryukov std::map<const syntax::Token *, DelayedFold> DelayedFolds; 3049b3f38f9SIlya Biryukov }; 3059b3f38f9SIlya Biryukov 3069b3f38f9SIlya Biryukov /// For debugging purposes. 3079b3f38f9SIlya Biryukov std::string str() { return Pending.str(Arena); } 3089b3f38f9SIlya Biryukov 3099b3f38f9SIlya Biryukov syntax::Arena &Arena; 310*c1bbefefSIlya Biryukov /// To quickly find tokens by their start location. 311*c1bbefefSIlya Biryukov llvm::DenseMap</*SourceLocation*/ unsigned, const syntax::Token *> 312*c1bbefefSIlya Biryukov LocationToToken; 3139b3f38f9SIlya Biryukov Forest Pending; 314e702bdb8SIlya Biryukov llvm::DenseSet<Decl *> DeclsWithoutSemicolons; 3159b3f38f9SIlya Biryukov }; 3169b3f38f9SIlya Biryukov 3179b3f38f9SIlya Biryukov namespace { 3189b3f38f9SIlya Biryukov class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> { 3199b3f38f9SIlya Biryukov public: 3209b3f38f9SIlya Biryukov explicit BuildTreeVisitor(ASTContext &Ctx, syntax::TreeBuilder &Builder) 3219b3f38f9SIlya Biryukov : Builder(Builder), LangOpts(Ctx.getLangOpts()) {} 3229b3f38f9SIlya Biryukov 3239b3f38f9SIlya Biryukov bool shouldTraversePostOrder() const { return true; } 3249b3f38f9SIlya Biryukov 325e702bdb8SIlya Biryukov bool WalkUpFromDeclaratorDecl(DeclaratorDecl *D) { 326e702bdb8SIlya Biryukov // Ensure declarators are covered by SimpleDeclaration. 327e702bdb8SIlya Biryukov Builder.noticeDeclaratorRange(Builder.getRange(D)); 328e702bdb8SIlya Biryukov // FIXME: build nodes for the declarator too. 329e702bdb8SIlya Biryukov return true; 330e702bdb8SIlya Biryukov } 331e702bdb8SIlya Biryukov bool WalkUpFromTypedefNameDecl(TypedefNameDecl *D) { 332e702bdb8SIlya Biryukov // Also a declarator. 333e702bdb8SIlya Biryukov Builder.noticeDeclaratorRange(Builder.getRange(D)); 334e702bdb8SIlya Biryukov // FIXME: build nodes for the declarator too. 335e702bdb8SIlya Biryukov return true; 3369b3f38f9SIlya Biryukov } 3379b3f38f9SIlya Biryukov 3389b3f38f9SIlya Biryukov bool VisitDecl(Decl *D) { 3399b3f38f9SIlya Biryukov assert(!D->isImplicit()); 3409b3f38f9SIlya Biryukov Builder.foldNode(Builder.getRange(D), 341e702bdb8SIlya Biryukov new (allocator()) syntax::UnknownDeclaration()); 342e702bdb8SIlya Biryukov return true; 343e702bdb8SIlya Biryukov } 344e702bdb8SIlya Biryukov 345e702bdb8SIlya Biryukov bool WalkUpFromTagDecl(TagDecl *C) { 346e702bdb8SIlya Biryukov // Avoid building UnknownDeclaration here, syntatically 'struct X {}' and 347e702bdb8SIlya Biryukov // similar are part of declaration specifiers and do not introduce a new 348e702bdb8SIlya Biryukov // top-level declaration. 3499b3f38f9SIlya Biryukov return true; 3509b3f38f9SIlya Biryukov } 3519b3f38f9SIlya Biryukov 3529b3f38f9SIlya Biryukov bool WalkUpFromTranslationUnitDecl(TranslationUnitDecl *TU) { 3539b3f38f9SIlya Biryukov // (!) we do not want to call VisitDecl(), the declaration for translation 3549b3f38f9SIlya Biryukov // unit is built by finalize(). 3559b3f38f9SIlya Biryukov return true; 3569b3f38f9SIlya Biryukov } 3579b3f38f9SIlya Biryukov 3589b3f38f9SIlya Biryukov bool WalkUpFromCompoundStmt(CompoundStmt *S) { 35951dad419SIlya Biryukov using NodeRole = syntax::NodeRole; 3609b3f38f9SIlya Biryukov 361def65bb4SIlya Biryukov Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen); 36258fa50f4SIlya Biryukov for (auto *Child : S->body()) 36358fa50f4SIlya Biryukov Builder.markStmtChild(Child, NodeRole::CompoundStatement_statement); 364def65bb4SIlya Biryukov Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen); 3659b3f38f9SIlya Biryukov 36658fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 3679b3f38f9SIlya Biryukov new (allocator()) syntax::CompoundStatement); 3689b3f38f9SIlya Biryukov return true; 3699b3f38f9SIlya Biryukov } 3709b3f38f9SIlya Biryukov 37158fa50f4SIlya Biryukov // Some statements are not yet handled by syntax trees. 37258fa50f4SIlya Biryukov bool WalkUpFromStmt(Stmt *S) { 37358fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 37458fa50f4SIlya Biryukov new (allocator()) syntax::UnknownStatement); 37558fa50f4SIlya Biryukov return true; 37658fa50f4SIlya Biryukov } 37758fa50f4SIlya Biryukov 37858fa50f4SIlya Biryukov bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) { 37958fa50f4SIlya Biryukov // We override to traverse range initializer as VarDecl. 38058fa50f4SIlya Biryukov // RAV traverses it as a statement, we produce invalid node kinds in that 38158fa50f4SIlya Biryukov // case. 38258fa50f4SIlya Biryukov // FIXME: should do this in RAV instead? 38358fa50f4SIlya Biryukov if (S->getInit() && !TraverseStmt(S->getInit())) 38458fa50f4SIlya Biryukov return false; 38558fa50f4SIlya Biryukov if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable())) 38658fa50f4SIlya Biryukov return false; 38758fa50f4SIlya Biryukov if (S->getRangeInit() && !TraverseStmt(S->getRangeInit())) 38858fa50f4SIlya Biryukov return false; 38958fa50f4SIlya Biryukov if (S->getBody() && !TraverseStmt(S->getBody())) 39058fa50f4SIlya Biryukov return false; 39158fa50f4SIlya Biryukov return true; 39258fa50f4SIlya Biryukov } 39358fa50f4SIlya Biryukov 39458fa50f4SIlya Biryukov bool TraverseStmt(Stmt *S) { 395e702bdb8SIlya Biryukov if (auto *DS = llvm::dyn_cast_or_null<DeclStmt>(S)) { 396e702bdb8SIlya Biryukov // We want to consume the semicolon, make sure SimpleDeclaration does not. 397e702bdb8SIlya Biryukov for (auto *D : DS->decls()) 398e702bdb8SIlya Biryukov Builder.noticeDeclaratorWithoutSemicolon(D); 399e702bdb8SIlya Biryukov } else if (auto *E = llvm::dyn_cast_or_null<Expr>(S)) { 40058fa50f4SIlya Biryukov // (!) do not recurse into subexpressions. 40158fa50f4SIlya Biryukov // we do not have syntax trees for expressions yet, so we only want to see 40258fa50f4SIlya Biryukov // the first top-level expression. 40358fa50f4SIlya Biryukov return WalkUpFromExpr(E->IgnoreImplicit()); 40458fa50f4SIlya Biryukov } 40558fa50f4SIlya Biryukov return RecursiveASTVisitor::TraverseStmt(S); 40658fa50f4SIlya Biryukov } 40758fa50f4SIlya Biryukov 40858fa50f4SIlya Biryukov // Some expressions are not yet handled by syntax trees. 40958fa50f4SIlya Biryukov bool WalkUpFromExpr(Expr *E) { 41058fa50f4SIlya Biryukov assert(!isImplicitExpr(E) && "should be handled by TraverseStmt"); 41158fa50f4SIlya Biryukov Builder.foldNode(Builder.getExprRange(E), 41258fa50f4SIlya Biryukov new (allocator()) syntax::UnknownExpression); 41358fa50f4SIlya Biryukov return true; 41458fa50f4SIlya Biryukov } 41558fa50f4SIlya Biryukov 416be14a22bSIlya Biryukov bool WalkUpFromNamespaceDecl(NamespaceDecl *S) { 417be14a22bSIlya Biryukov auto Tokens = Builder.getRange(S); 418be14a22bSIlya Biryukov if (Tokens.front().kind() == tok::coloncolon) { 419be14a22bSIlya Biryukov // Handle nested namespace definitions. Those start at '::' token, e.g. 420be14a22bSIlya Biryukov // namespace a^::b {} 421be14a22bSIlya Biryukov // FIXME: build corresponding nodes for the name of this namespace. 422be14a22bSIlya Biryukov return true; 423be14a22bSIlya Biryukov } 424be14a22bSIlya Biryukov Builder.foldNode(Tokens, new (allocator()) syntax::NamespaceDefinition); 425be14a22bSIlya Biryukov return true; 426be14a22bSIlya Biryukov } 427be14a22bSIlya Biryukov 42858fa50f4SIlya Biryukov // The code below is very regular, it could even be generated with some 42958fa50f4SIlya Biryukov // preprocessor magic. We merely assign roles to the corresponding children 43058fa50f4SIlya Biryukov // and fold resulting nodes. 43158fa50f4SIlya Biryukov bool WalkUpFromDeclStmt(DeclStmt *S) { 43258fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 43358fa50f4SIlya Biryukov new (allocator()) syntax::DeclarationStatement); 43458fa50f4SIlya Biryukov return true; 43558fa50f4SIlya Biryukov } 43658fa50f4SIlya Biryukov 43758fa50f4SIlya Biryukov bool WalkUpFromNullStmt(NullStmt *S) { 43858fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 43958fa50f4SIlya Biryukov new (allocator()) syntax::EmptyStatement); 44058fa50f4SIlya Biryukov return true; 44158fa50f4SIlya Biryukov } 44258fa50f4SIlya Biryukov 44358fa50f4SIlya Biryukov bool WalkUpFromSwitchStmt(SwitchStmt *S) { 444def65bb4SIlya Biryukov Builder.markChildToken(S->getSwitchLoc(), 44558fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 44658fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 44758fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 44858fa50f4SIlya Biryukov new (allocator()) syntax::SwitchStatement); 44958fa50f4SIlya Biryukov return true; 45058fa50f4SIlya Biryukov } 45158fa50f4SIlya Biryukov 45258fa50f4SIlya Biryukov bool WalkUpFromCaseStmt(CaseStmt *S) { 453def65bb4SIlya Biryukov Builder.markChildToken(S->getKeywordLoc(), 45458fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 45558fa50f4SIlya Biryukov Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseStatement_value); 45658fa50f4SIlya Biryukov Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 45758fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 45858fa50f4SIlya Biryukov new (allocator()) syntax::CaseStatement); 45958fa50f4SIlya Biryukov return true; 46058fa50f4SIlya Biryukov } 46158fa50f4SIlya Biryukov 46258fa50f4SIlya Biryukov bool WalkUpFromDefaultStmt(DefaultStmt *S) { 463def65bb4SIlya Biryukov Builder.markChildToken(S->getKeywordLoc(), 46458fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 46558fa50f4SIlya Biryukov Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 46658fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 46758fa50f4SIlya Biryukov new (allocator()) syntax::DefaultStatement); 46858fa50f4SIlya Biryukov return true; 46958fa50f4SIlya Biryukov } 47058fa50f4SIlya Biryukov 47158fa50f4SIlya Biryukov bool WalkUpFromIfStmt(IfStmt *S) { 472def65bb4SIlya Biryukov Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword); 47358fa50f4SIlya Biryukov Builder.markStmtChild(S->getThen(), 47458fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_thenStatement); 475def65bb4SIlya Biryukov Builder.markChildToken(S->getElseLoc(), 47658fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_elseKeyword); 47758fa50f4SIlya Biryukov Builder.markStmtChild(S->getElse(), 47858fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_elseStatement); 47958fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 48058fa50f4SIlya Biryukov new (allocator()) syntax::IfStatement); 48158fa50f4SIlya Biryukov return true; 48258fa50f4SIlya Biryukov } 48358fa50f4SIlya Biryukov 48458fa50f4SIlya Biryukov bool WalkUpFromForStmt(ForStmt *S) { 485def65bb4SIlya Biryukov Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 48658fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 48758fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 48858fa50f4SIlya Biryukov new (allocator()) syntax::ForStatement); 48958fa50f4SIlya Biryukov return true; 49058fa50f4SIlya Biryukov } 49158fa50f4SIlya Biryukov 49258fa50f4SIlya Biryukov bool WalkUpFromWhileStmt(WhileStmt *S) { 493def65bb4SIlya Biryukov Builder.markChildToken(S->getWhileLoc(), 49458fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 49558fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 49658fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 49758fa50f4SIlya Biryukov new (allocator()) syntax::WhileStatement); 49858fa50f4SIlya Biryukov return true; 49958fa50f4SIlya Biryukov } 50058fa50f4SIlya Biryukov 50158fa50f4SIlya Biryukov bool WalkUpFromContinueStmt(ContinueStmt *S) { 502def65bb4SIlya Biryukov Builder.markChildToken(S->getContinueLoc(), 50358fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 50458fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 50558fa50f4SIlya Biryukov new (allocator()) syntax::ContinueStatement); 50658fa50f4SIlya Biryukov return true; 50758fa50f4SIlya Biryukov } 50858fa50f4SIlya Biryukov 50958fa50f4SIlya Biryukov bool WalkUpFromBreakStmt(BreakStmt *S) { 510def65bb4SIlya Biryukov Builder.markChildToken(S->getBreakLoc(), 51158fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 51258fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 51358fa50f4SIlya Biryukov new (allocator()) syntax::BreakStatement); 51458fa50f4SIlya Biryukov return true; 51558fa50f4SIlya Biryukov } 51658fa50f4SIlya Biryukov 51758fa50f4SIlya Biryukov bool WalkUpFromReturnStmt(ReturnStmt *S) { 518def65bb4SIlya Biryukov Builder.markChildToken(S->getReturnLoc(), 51958fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 52058fa50f4SIlya Biryukov Builder.markExprChild(S->getRetValue(), 52158fa50f4SIlya Biryukov syntax::NodeRole::ReturnStatement_value); 52258fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 52358fa50f4SIlya Biryukov new (allocator()) syntax::ReturnStatement); 52458fa50f4SIlya Biryukov return true; 52558fa50f4SIlya Biryukov } 52658fa50f4SIlya Biryukov 52758fa50f4SIlya Biryukov bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) { 528def65bb4SIlya Biryukov Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 52958fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 53058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 53158fa50f4SIlya Biryukov new (allocator()) syntax::RangeBasedForStatement); 53258fa50f4SIlya Biryukov return true; 53358fa50f4SIlya Biryukov } 53458fa50f4SIlya Biryukov 535be14a22bSIlya Biryukov bool WalkUpFromEmptyDecl(EmptyDecl *S) { 536be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 537be14a22bSIlya Biryukov new (allocator()) syntax::EmptyDeclaration); 538be14a22bSIlya Biryukov return true; 539be14a22bSIlya Biryukov } 540be14a22bSIlya Biryukov 541be14a22bSIlya Biryukov bool WalkUpFromStaticAssertDecl(StaticAssertDecl *S) { 542be14a22bSIlya Biryukov Builder.markExprChild(S->getAssertExpr(), 543be14a22bSIlya Biryukov syntax::NodeRole::StaticAssertDeclaration_condition); 544be14a22bSIlya Biryukov Builder.markExprChild(S->getMessage(), 545be14a22bSIlya Biryukov syntax::NodeRole::StaticAssertDeclaration_message); 546be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 547be14a22bSIlya Biryukov new (allocator()) syntax::StaticAssertDeclaration); 548be14a22bSIlya Biryukov return true; 549be14a22bSIlya Biryukov } 550be14a22bSIlya Biryukov 551be14a22bSIlya Biryukov bool WalkUpFromLinkageSpecDecl(LinkageSpecDecl *S) { 552be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 553be14a22bSIlya Biryukov new (allocator()) syntax::LinkageSpecificationDeclaration); 554be14a22bSIlya Biryukov return true; 555be14a22bSIlya Biryukov } 556be14a22bSIlya Biryukov 557be14a22bSIlya Biryukov bool WalkUpFromNamespaceAliasDecl(NamespaceAliasDecl *S) { 558be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 559be14a22bSIlya Biryukov new (allocator()) syntax::NamespaceAliasDefinition); 560be14a22bSIlya Biryukov return true; 561be14a22bSIlya Biryukov } 562be14a22bSIlya Biryukov 563be14a22bSIlya Biryukov bool WalkUpFromUsingDirectiveDecl(UsingDirectiveDecl *S) { 564be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 565be14a22bSIlya Biryukov new (allocator()) syntax::UsingNamespaceDirective); 566be14a22bSIlya Biryukov return true; 567be14a22bSIlya Biryukov } 568be14a22bSIlya Biryukov 569be14a22bSIlya Biryukov bool WalkUpFromUsingDecl(UsingDecl *S) { 570be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 571be14a22bSIlya Biryukov new (allocator()) syntax::UsingDeclaration); 572be14a22bSIlya Biryukov return true; 573be14a22bSIlya Biryukov } 574be14a22bSIlya Biryukov 575be14a22bSIlya Biryukov bool WalkUpFromUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *S) { 576be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 577be14a22bSIlya Biryukov new (allocator()) syntax::UsingDeclaration); 578be14a22bSIlya Biryukov return true; 579be14a22bSIlya Biryukov } 580be14a22bSIlya Biryukov 581be14a22bSIlya Biryukov bool WalkUpFromUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *S) { 582be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 583be14a22bSIlya Biryukov new (allocator()) syntax::UsingDeclaration); 584be14a22bSIlya Biryukov return true; 585be14a22bSIlya Biryukov } 586be14a22bSIlya Biryukov 587be14a22bSIlya Biryukov bool WalkUpFromTypeAliasDecl(TypeAliasDecl *S) { 588be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 589be14a22bSIlya Biryukov new (allocator()) syntax::TypeAliasDeclaration); 590be14a22bSIlya Biryukov return true; 591be14a22bSIlya Biryukov } 592be14a22bSIlya Biryukov 5939b3f38f9SIlya Biryukov private: 5949b3f38f9SIlya Biryukov /// A small helper to save some typing. 5959b3f38f9SIlya Biryukov llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); } 5969b3f38f9SIlya Biryukov 5979b3f38f9SIlya Biryukov syntax::TreeBuilder &Builder; 5989b3f38f9SIlya Biryukov const LangOptions &LangOpts; 5999b3f38f9SIlya Biryukov }; 6009b3f38f9SIlya Biryukov } // namespace 6019b3f38f9SIlya Biryukov 6029b3f38f9SIlya Biryukov void syntax::TreeBuilder::foldNode(llvm::ArrayRef<syntax::Token> Range, 6039b3f38f9SIlya Biryukov syntax::Tree *New) { 6041ad15046SIlya Biryukov Pending.foldChildren(Arena, Range, New); 6059b3f38f9SIlya Biryukov } 6069b3f38f9SIlya Biryukov 607e702bdb8SIlya Biryukov void syntax::TreeBuilder::noticeDeclaratorRange( 608e702bdb8SIlya Biryukov llvm::ArrayRef<syntax::Token> Range) { 609e702bdb8SIlya Biryukov if (Pending.extendDelayedFold(Range)) 610e702bdb8SIlya Biryukov return; 611e702bdb8SIlya Biryukov Pending.foldChildrenDelayed(Range, 612e702bdb8SIlya Biryukov new (allocator()) syntax::SimpleDeclaration); 613e702bdb8SIlya Biryukov } 614e702bdb8SIlya Biryukov 615e702bdb8SIlya Biryukov void syntax::TreeBuilder::noticeDeclaratorWithoutSemicolon(Decl *D) { 616e702bdb8SIlya Biryukov DeclsWithoutSemicolons.insert(D); 617e702bdb8SIlya Biryukov } 618e702bdb8SIlya Biryukov 619def65bb4SIlya Biryukov void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) { 6209b3f38f9SIlya Biryukov if (Loc.isInvalid()) 6219b3f38f9SIlya Biryukov return; 6229b3f38f9SIlya Biryukov Pending.assignRole(*findToken(Loc), Role); 6239b3f38f9SIlya Biryukov } 6249b3f38f9SIlya Biryukov 62558fa50f4SIlya Biryukov void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) { 62658fa50f4SIlya Biryukov if (!Child) 62758fa50f4SIlya Biryukov return; 62858fa50f4SIlya Biryukov 62958fa50f4SIlya Biryukov auto Range = getStmtRange(Child); 63058fa50f4SIlya Biryukov // This is an expression in a statement position, consume the trailing 63158fa50f4SIlya Biryukov // semicolon and form an 'ExpressionStatement' node. 63258fa50f4SIlya Biryukov if (auto *E = dyn_cast<Expr>(Child)) { 63358fa50f4SIlya Biryukov Pending.assignRole(getExprRange(E), 63458fa50f4SIlya Biryukov NodeRole::ExpressionStatement_expression); 63558fa50f4SIlya Biryukov // (!) 'getRange(Stmt)' ensures this already covers a trailing semicolon. 6361ad15046SIlya Biryukov Pending.foldChildren(Arena, Range, 6371ad15046SIlya Biryukov new (allocator()) syntax::ExpressionStatement); 63858fa50f4SIlya Biryukov } 63958fa50f4SIlya Biryukov Pending.assignRole(Range, Role); 64058fa50f4SIlya Biryukov } 64158fa50f4SIlya Biryukov 64258fa50f4SIlya Biryukov void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) { 643be14a22bSIlya Biryukov if (!Child) 644be14a22bSIlya Biryukov return; 645be14a22bSIlya Biryukov 64658fa50f4SIlya Biryukov Pending.assignRole(getExprRange(Child), Role); 64758fa50f4SIlya Biryukov } 64858fa50f4SIlya Biryukov 6499b3f38f9SIlya Biryukov const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const { 650*c1bbefefSIlya Biryukov auto It = LocationToToken.find(L.getRawEncoding()); 651*c1bbefefSIlya Biryukov assert(It != LocationToToken.end()); 652*c1bbefefSIlya Biryukov return It->second; 6539b3f38f9SIlya Biryukov } 6549b3f38f9SIlya Biryukov 6559b3f38f9SIlya Biryukov syntax::TranslationUnit * 6569b3f38f9SIlya Biryukov syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) { 6579b3f38f9SIlya Biryukov TreeBuilder Builder(A); 6589b3f38f9SIlya Biryukov BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext()); 6599b3f38f9SIlya Biryukov return std::move(Builder).finalize(); 6609b3f38f9SIlya Biryukov } 661