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" 289b3f38f9SIlya Biryukov #include "llvm/Support/raw_ostream.h" 299b3f38f9SIlya Biryukov #include <map> 309b3f38f9SIlya Biryukov 319b3f38f9SIlya Biryukov using namespace clang; 329b3f38f9SIlya Biryukov 3396065cf7SIlya Biryukov LLVM_ATTRIBUTE_UNUSED 3458fa50f4SIlya Biryukov static bool isImplicitExpr(clang::Expr *E) { return E->IgnoreImplicit() != E; } 3558fa50f4SIlya Biryukov 369b3f38f9SIlya Biryukov /// A helper class for constructing the syntax tree while traversing a clang 379b3f38f9SIlya Biryukov /// AST. 389b3f38f9SIlya Biryukov /// 399b3f38f9SIlya Biryukov /// At each point of the traversal we maintain a list of pending nodes. 409b3f38f9SIlya Biryukov /// Initially all tokens are added as pending nodes. When processing a clang AST 419b3f38f9SIlya Biryukov /// node, the clients need to: 429b3f38f9SIlya Biryukov /// - create a corresponding syntax node, 439b3f38f9SIlya Biryukov /// - assign roles to all pending child nodes with 'markChild' and 449b3f38f9SIlya Biryukov /// 'markChildToken', 459b3f38f9SIlya Biryukov /// - replace the child nodes with the new syntax node in the pending list 469b3f38f9SIlya Biryukov /// with 'foldNode'. 479b3f38f9SIlya Biryukov /// 489b3f38f9SIlya Biryukov /// Note that all children are expected to be processed when building a node. 499b3f38f9SIlya Biryukov /// 509b3f38f9SIlya Biryukov /// Call finalize() to finish building the tree and consume the root node. 519b3f38f9SIlya Biryukov class syntax::TreeBuilder { 529b3f38f9SIlya Biryukov public: 539b3f38f9SIlya Biryukov TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) {} 549b3f38f9SIlya Biryukov 559b3f38f9SIlya Biryukov llvm::BumpPtrAllocator &allocator() { return Arena.allocator(); } 569b3f38f9SIlya Biryukov 579b3f38f9SIlya Biryukov /// Populate children for \p New node, assuming it covers tokens from \p 589b3f38f9SIlya Biryukov /// Range. 599b3f38f9SIlya Biryukov void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree *New); 609b3f38f9SIlya Biryukov 61e702bdb8SIlya Biryukov /// Must be called with the range of each `DeclaratorDecl`. Ensures the 62e702bdb8SIlya Biryukov /// corresponding declarator nodes are covered by `SimpleDeclaration`. 63e702bdb8SIlya Biryukov void noticeDeclaratorRange(llvm::ArrayRef<syntax::Token> Range); 64e702bdb8SIlya Biryukov 65e702bdb8SIlya Biryukov /// Notifies that we should not consume trailing semicolon when computing 66e702bdb8SIlya Biryukov /// token range of \p D. 67e702bdb8SIlya Biryukov void noticeDeclaratorWithoutSemicolon(Decl *D); 68e702bdb8SIlya Biryukov 6958fa50f4SIlya Biryukov /// Mark the \p Child node with a corresponding \p Role. All marked children 7058fa50f4SIlya Biryukov /// should be consumed by foldNode. 7158fa50f4SIlya Biryukov /// (!) when called on expressions (clang::Expr is derived from clang::Stmt), 7258fa50f4SIlya Biryukov /// wraps expressions into expression statement. 7358fa50f4SIlya Biryukov void markStmtChild(Stmt *Child, NodeRole Role); 7458fa50f4SIlya Biryukov /// Should be called for expressions in non-statement position to avoid 7558fa50f4SIlya Biryukov /// wrapping into expression statement. 7658fa50f4SIlya Biryukov void markExprChild(Expr *Child, NodeRole Role); 7758fa50f4SIlya Biryukov 789b3f38f9SIlya Biryukov /// Set role for a token starting at \p Loc. 79def65bb4SIlya Biryukov void markChildToken(SourceLocation Loc, NodeRole R); 809b3f38f9SIlya Biryukov 819b3f38f9SIlya Biryukov /// Finish building the tree and consume the root node. 829b3f38f9SIlya Biryukov syntax::TranslationUnit *finalize() && { 839b3f38f9SIlya Biryukov auto Tokens = Arena.tokenBuffer().expandedTokens(); 84bfbf6b6cSIlya Biryukov assert(!Tokens.empty()); 85bfbf6b6cSIlya Biryukov assert(Tokens.back().kind() == tok::eof); 86bfbf6b6cSIlya Biryukov 879b3f38f9SIlya Biryukov // Build the root of the tree, consuming all the children. 88bfbf6b6cSIlya Biryukov Pending.foldChildren(Tokens.drop_back(), 899b3f38f9SIlya Biryukov new (Arena.allocator()) syntax::TranslationUnit); 909b3f38f9SIlya Biryukov 919b3f38f9SIlya Biryukov return cast<syntax::TranslationUnit>(std::move(Pending).finalize()); 929b3f38f9SIlya Biryukov } 939b3f38f9SIlya Biryukov 949b3f38f9SIlya Biryukov /// getRange() finds the syntax tokens corresponding to the passed source 959b3f38f9SIlya Biryukov /// locations. 969b3f38f9SIlya Biryukov /// \p First is the start position of the first token and \p Last is the start 979b3f38f9SIlya Biryukov /// position of the last token. 989b3f38f9SIlya Biryukov llvm::ArrayRef<syntax::Token> getRange(SourceLocation First, 999b3f38f9SIlya Biryukov SourceLocation Last) const { 1009b3f38f9SIlya Biryukov assert(First.isValid()); 1019b3f38f9SIlya Biryukov assert(Last.isValid()); 1029b3f38f9SIlya Biryukov assert(First == Last || 1039b3f38f9SIlya Biryukov Arena.sourceManager().isBeforeInTranslationUnit(First, Last)); 1049b3f38f9SIlya Biryukov return llvm::makeArrayRef(findToken(First), std::next(findToken(Last))); 1059b3f38f9SIlya Biryukov } 1069b3f38f9SIlya Biryukov llvm::ArrayRef<syntax::Token> getRange(const Decl *D) const { 107e702bdb8SIlya Biryukov auto Tokens = getRange(D->getBeginLoc(), D->getEndLoc()); 108e702bdb8SIlya Biryukov if (llvm::isa<NamespaceDecl>(D)) 109e702bdb8SIlya Biryukov return Tokens; 110e702bdb8SIlya Biryukov if (DeclsWithoutSemicolons.count(D)) 111e702bdb8SIlya Biryukov return Tokens; 112e702bdb8SIlya Biryukov // FIXME: do not consume trailing semicolon on function definitions. 113e702bdb8SIlya Biryukov // Most declarations own a semicolon in syntax trees, but not in clang AST. 114e702bdb8SIlya Biryukov return withTrailingSemicolon(Tokens); 1159b3f38f9SIlya Biryukov } 11658fa50f4SIlya Biryukov llvm::ArrayRef<syntax::Token> getExprRange(const Expr *E) const { 11758fa50f4SIlya Biryukov return getRange(E->getBeginLoc(), E->getEndLoc()); 11858fa50f4SIlya Biryukov } 11958fa50f4SIlya Biryukov /// Find the adjusted range for the statement, consuming the trailing 12058fa50f4SIlya Biryukov /// semicolon when needed. 12158fa50f4SIlya Biryukov llvm::ArrayRef<syntax::Token> getStmtRange(const Stmt *S) const { 12258fa50f4SIlya Biryukov auto Tokens = getRange(S->getBeginLoc(), S->getEndLoc()); 12358fa50f4SIlya Biryukov if (isa<CompoundStmt>(S)) 12458fa50f4SIlya Biryukov return Tokens; 12558fa50f4SIlya Biryukov 12658fa50f4SIlya Biryukov // Some statements miss a trailing semicolon, e.g. 'return', 'continue' and 12758fa50f4SIlya Biryukov // all statements that end with those. Consume this semicolon here. 128e702bdb8SIlya Biryukov if (Tokens.back().kind() == tok::semi) 129e702bdb8SIlya Biryukov return Tokens; 130e702bdb8SIlya Biryukov return withTrailingSemicolon(Tokens); 131e702bdb8SIlya Biryukov } 132e702bdb8SIlya Biryukov 133e702bdb8SIlya Biryukov private: 134e702bdb8SIlya Biryukov llvm::ArrayRef<syntax::Token> 135e702bdb8SIlya Biryukov withTrailingSemicolon(llvm::ArrayRef<syntax::Token> Tokens) const { 136e702bdb8SIlya Biryukov assert(!Tokens.empty()); 137e702bdb8SIlya Biryukov assert(Tokens.back().kind() != tok::eof); 138e702bdb8SIlya Biryukov // (!) we never consume 'eof', so looking at the next token is ok. 13958fa50f4SIlya Biryukov if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi) 14058fa50f4SIlya Biryukov return llvm::makeArrayRef(Tokens.begin(), Tokens.end() + 1); 14158fa50f4SIlya Biryukov return Tokens; 1429b3f38f9SIlya Biryukov } 1439b3f38f9SIlya Biryukov 1449b3f38f9SIlya Biryukov /// Finds a token starting at \p L. The token must exist. 1459b3f38f9SIlya Biryukov const syntax::Token *findToken(SourceLocation L) const; 1469b3f38f9SIlya Biryukov 1479b3f38f9SIlya Biryukov /// A collection of trees covering the input tokens. 1489b3f38f9SIlya Biryukov /// When created, each tree corresponds to a single token in the file. 1499b3f38f9SIlya Biryukov /// Clients call 'foldChildren' to attach one or more subtrees to a parent 1509b3f38f9SIlya Biryukov /// node and update the list of trees accordingly. 1519b3f38f9SIlya Biryukov /// 1529b3f38f9SIlya Biryukov /// Ensures that added nodes properly nest and cover the whole token stream. 1539b3f38f9SIlya Biryukov struct Forest { 1549b3f38f9SIlya Biryukov Forest(syntax::Arena &A) { 155bfbf6b6cSIlya Biryukov assert(!A.tokenBuffer().expandedTokens().empty()); 156bfbf6b6cSIlya Biryukov assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof); 1579b3f38f9SIlya Biryukov // Create all leaf nodes. 158bfbf6b6cSIlya Biryukov // Note that we do not have 'eof' in the tree. 159bfbf6b6cSIlya Biryukov for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) 1609b3f38f9SIlya Biryukov Trees.insert(Trees.end(), 1619b3f38f9SIlya Biryukov {&T, NodeAndRole{new (A.allocator()) syntax::Leaf(&T)}}); 1629b3f38f9SIlya Biryukov } 1639b3f38f9SIlya Biryukov 164e702bdb8SIlya Biryukov ~Forest() { assert(DelayedFolds.empty()); } 165e702bdb8SIlya Biryukov 1669b3f38f9SIlya Biryukov void assignRole(llvm::ArrayRef<syntax::Token> Range, 1679b3f38f9SIlya Biryukov syntax::NodeRole Role) { 1689b3f38f9SIlya Biryukov assert(!Range.empty()); 1699b3f38f9SIlya Biryukov auto It = Trees.lower_bound(Range.begin()); 1709b3f38f9SIlya Biryukov assert(It != Trees.end() && "no node found"); 1719b3f38f9SIlya Biryukov assert(It->first == Range.begin() && "no child with the specified range"); 1729b3f38f9SIlya Biryukov assert((std::next(It) == Trees.end() || 1739b3f38f9SIlya Biryukov std::next(It)->first == Range.end()) && 1749b3f38f9SIlya Biryukov "no child with the specified range"); 1759b3f38f9SIlya Biryukov It->second.Role = Role; 1769b3f38f9SIlya Biryukov } 1779b3f38f9SIlya Biryukov 178e702bdb8SIlya Biryukov /// Add \p Node to the forest and attach child nodes based on \p Tokens. 179e702bdb8SIlya Biryukov void foldChildren(llvm::ArrayRef<syntax::Token> Tokens, 1809b3f38f9SIlya Biryukov syntax::Tree *Node) { 181e702bdb8SIlya Biryukov // Execute delayed folds inside `Tokens`. 182e702bdb8SIlya Biryukov auto BeginExecuted = DelayedFolds.lower_bound(Tokens.begin()); 183e702bdb8SIlya Biryukov auto It = BeginExecuted; 184e702bdb8SIlya Biryukov for (; It != DelayedFolds.end() && It->second.End <= Tokens.end(); ++It) 185e702bdb8SIlya Biryukov foldChildrenEager(llvm::makeArrayRef(It->first, It->second.End), 186e702bdb8SIlya Biryukov It->second.Node); 187e702bdb8SIlya Biryukov DelayedFolds.erase(BeginExecuted, It); 1889b3f38f9SIlya Biryukov 189e702bdb8SIlya Biryukov // Attach children to `Node`. 190e702bdb8SIlya Biryukov foldChildrenEager(Tokens, Node); 191e702bdb8SIlya Biryukov } 1929b3f38f9SIlya Biryukov 193e702bdb8SIlya Biryukov /// Schedule a call to `foldChildren` that will only be executed when 194e702bdb8SIlya Biryukov /// containing node is folded. The range of delayed nodes can be extended by 195e702bdb8SIlya Biryukov /// calling `extendDelayedFold`. Only one delayed node for each starting 196e702bdb8SIlya Biryukov /// token is allowed. 197e702bdb8SIlya Biryukov void foldChildrenDelayed(llvm::ArrayRef<syntax::Token> Tokens, 198e702bdb8SIlya Biryukov syntax::Tree *Node) { 199e702bdb8SIlya Biryukov assert(!Tokens.empty()); 200e702bdb8SIlya Biryukov bool Inserted = 201e702bdb8SIlya Biryukov DelayedFolds.insert({Tokens.begin(), DelayedFold{Tokens.end(), Node}}) 202e702bdb8SIlya Biryukov .second; 203e702bdb8SIlya Biryukov (void)Inserted; 204e702bdb8SIlya Biryukov assert(Inserted && "Multiple delayed folds start at the same token"); 205e702bdb8SIlya Biryukov } 2069b3f38f9SIlya Biryukov 207e702bdb8SIlya Biryukov /// If there a delayed fold, starting at `ExtendedRange.begin()`, extends 208e702bdb8SIlya Biryukov /// its endpoint to `ExtendedRange.end()` and returns true. 209e702bdb8SIlya Biryukov /// Otherwise, returns false. 210e702bdb8SIlya Biryukov bool extendDelayedFold(llvm::ArrayRef<syntax::Token> ExtendedRange) { 211e702bdb8SIlya Biryukov assert(!ExtendedRange.empty()); 212e702bdb8SIlya Biryukov auto It = DelayedFolds.find(ExtendedRange.data()); 213e702bdb8SIlya Biryukov if (It == DelayedFolds.end()) 214e702bdb8SIlya Biryukov return false; 215e702bdb8SIlya Biryukov assert(It->second.End <= ExtendedRange.end()); 216e702bdb8SIlya Biryukov It->second.End = ExtendedRange.end(); 217e702bdb8SIlya Biryukov return true; 2189b3f38f9SIlya Biryukov } 2199b3f38f9SIlya Biryukov 2209b3f38f9SIlya Biryukov // EXPECTS: all tokens were consumed and are owned by a single root node. 2219b3f38f9SIlya Biryukov syntax::Node *finalize() && { 2229b3f38f9SIlya Biryukov assert(Trees.size() == 1); 2239b3f38f9SIlya Biryukov auto *Root = Trees.begin()->second.Node; 2249b3f38f9SIlya Biryukov Trees = {}; 2259b3f38f9SIlya Biryukov return Root; 2269b3f38f9SIlya Biryukov } 2279b3f38f9SIlya Biryukov 2289b3f38f9SIlya Biryukov std::string str(const syntax::Arena &A) const { 2299b3f38f9SIlya Biryukov std::string R; 2309b3f38f9SIlya Biryukov for (auto It = Trees.begin(); It != Trees.end(); ++It) { 2319b3f38f9SIlya Biryukov unsigned CoveredTokens = 2329b3f38f9SIlya Biryukov It != Trees.end() 2339b3f38f9SIlya Biryukov ? (std::next(It)->first - It->first) 2349b3f38f9SIlya Biryukov : A.tokenBuffer().expandedTokens().end() - It->first; 2359b3f38f9SIlya Biryukov 2369b3f38f9SIlya Biryukov R += llvm::formatv("- '{0}' covers '{1}'+{2} tokens\n", 2379b3f38f9SIlya Biryukov It->second.Node->kind(), 2389b3f38f9SIlya Biryukov It->first->text(A.sourceManager()), CoveredTokens); 2399b3f38f9SIlya Biryukov R += It->second.Node->dump(A); 2409b3f38f9SIlya Biryukov } 2419b3f38f9SIlya Biryukov return R; 2429b3f38f9SIlya Biryukov } 2439b3f38f9SIlya Biryukov 2449b3f38f9SIlya Biryukov private: 245e702bdb8SIlya Biryukov /// Implementation detail of `foldChildren`, does acutal folding ignoring 246e702bdb8SIlya Biryukov /// delayed folds. 247e702bdb8SIlya Biryukov void foldChildrenEager(llvm::ArrayRef<syntax::Token> Tokens, 248e702bdb8SIlya Biryukov syntax::Tree *Node) { 249e702bdb8SIlya Biryukov assert(Node->firstChild() == nullptr && "node already has children"); 250e702bdb8SIlya Biryukov 251e702bdb8SIlya Biryukov auto *FirstToken = Tokens.begin(); 252e702bdb8SIlya Biryukov auto BeginChildren = Trees.lower_bound(FirstToken); 253e702bdb8SIlya Biryukov assert((BeginChildren == Trees.end() || 254e702bdb8SIlya Biryukov BeginChildren->first == FirstToken) && 255e702bdb8SIlya Biryukov "fold crosses boundaries of existing subtrees"); 256e702bdb8SIlya Biryukov auto EndChildren = Trees.lower_bound(Tokens.end()); 257e702bdb8SIlya Biryukov assert( 258e702bdb8SIlya Biryukov (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) && 259e702bdb8SIlya Biryukov "fold crosses boundaries of existing subtrees"); 260e702bdb8SIlya Biryukov 261e702bdb8SIlya Biryukov // (!) we need to go in reverse order, because we can only prepend. 262e702bdb8SIlya Biryukov for (auto It = EndChildren; It != BeginChildren; --It) 263e702bdb8SIlya Biryukov Node->prependChildLowLevel(std::prev(It)->second.Node, 264e702bdb8SIlya Biryukov std::prev(It)->second.Role); 265e702bdb8SIlya Biryukov 266e702bdb8SIlya Biryukov Trees.erase(BeginChildren, EndChildren); 267e702bdb8SIlya Biryukov Trees.insert({FirstToken, NodeAndRole(Node)}); 268e702bdb8SIlya Biryukov } 2699b3f38f9SIlya Biryukov /// A with a role that should be assigned to it when adding to a parent. 2709b3f38f9SIlya Biryukov struct NodeAndRole { 2719b3f38f9SIlya Biryukov explicit NodeAndRole(syntax::Node *Node) 27251dad419SIlya Biryukov : Node(Node), Role(NodeRole::Unknown) {} 2739b3f38f9SIlya Biryukov 2749b3f38f9SIlya Biryukov syntax::Node *Node; 2759b3f38f9SIlya Biryukov NodeRole Role; 2769b3f38f9SIlya Biryukov }; 2779b3f38f9SIlya Biryukov 2789b3f38f9SIlya Biryukov /// Maps from the start token to a subtree starting at that token. 279302cb3bcSIlya Biryukov /// Keys in the map are pointers into the array of expanded tokens, so 280302cb3bcSIlya Biryukov /// pointer order corresponds to the order of preprocessor tokens. 2819b3f38f9SIlya Biryukov /// FIXME: storing the end tokens is redundant. 2829b3f38f9SIlya Biryukov /// FIXME: the key of a map is redundant, it is also stored in NodeForRange. 2839b3f38f9SIlya Biryukov std::map<const syntax::Token *, NodeAndRole> Trees; 284e702bdb8SIlya Biryukov 285e702bdb8SIlya Biryukov /// See documentation of `foldChildrenDelayed` for details. 286e702bdb8SIlya Biryukov struct DelayedFold { 287e702bdb8SIlya Biryukov const syntax::Token *End = nullptr; 288e702bdb8SIlya Biryukov syntax::Tree *Node = nullptr; 289e702bdb8SIlya Biryukov }; 290e702bdb8SIlya Biryukov std::map<const syntax::Token *, DelayedFold> DelayedFolds; 2919b3f38f9SIlya Biryukov }; 2929b3f38f9SIlya Biryukov 2939b3f38f9SIlya Biryukov /// For debugging purposes. 2949b3f38f9SIlya Biryukov std::string str() { return Pending.str(Arena); } 2959b3f38f9SIlya Biryukov 2969b3f38f9SIlya Biryukov syntax::Arena &Arena; 2979b3f38f9SIlya Biryukov Forest Pending; 298e702bdb8SIlya Biryukov llvm::DenseSet<Decl *> DeclsWithoutSemicolons; 2999b3f38f9SIlya Biryukov }; 3009b3f38f9SIlya Biryukov 3019b3f38f9SIlya Biryukov namespace { 3029b3f38f9SIlya Biryukov class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> { 3039b3f38f9SIlya Biryukov public: 3049b3f38f9SIlya Biryukov explicit BuildTreeVisitor(ASTContext &Ctx, syntax::TreeBuilder &Builder) 3059b3f38f9SIlya Biryukov : Builder(Builder), LangOpts(Ctx.getLangOpts()) {} 3069b3f38f9SIlya Biryukov 3079b3f38f9SIlya Biryukov bool shouldTraversePostOrder() const { return true; } 3089b3f38f9SIlya Biryukov 309e702bdb8SIlya Biryukov bool WalkUpFromDeclaratorDecl(DeclaratorDecl *D) { 310e702bdb8SIlya Biryukov // Ensure declarators are covered by SimpleDeclaration. 311e702bdb8SIlya Biryukov Builder.noticeDeclaratorRange(Builder.getRange(D)); 312e702bdb8SIlya Biryukov // FIXME: build nodes for the declarator too. 313e702bdb8SIlya Biryukov return true; 314e702bdb8SIlya Biryukov } 315e702bdb8SIlya Biryukov bool WalkUpFromTypedefNameDecl(TypedefNameDecl *D) { 316e702bdb8SIlya Biryukov // Also a declarator. 317e702bdb8SIlya Biryukov Builder.noticeDeclaratorRange(Builder.getRange(D)); 318e702bdb8SIlya Biryukov // FIXME: build nodes for the declarator too. 319e702bdb8SIlya Biryukov return true; 3209b3f38f9SIlya Biryukov } 3219b3f38f9SIlya Biryukov 3229b3f38f9SIlya Biryukov bool VisitDecl(Decl *D) { 3239b3f38f9SIlya Biryukov assert(!D->isImplicit()); 3249b3f38f9SIlya Biryukov Builder.foldNode(Builder.getRange(D), 325e702bdb8SIlya Biryukov new (allocator()) syntax::UnknownDeclaration()); 326e702bdb8SIlya Biryukov return true; 327e702bdb8SIlya Biryukov } 328e702bdb8SIlya Biryukov 329e702bdb8SIlya Biryukov bool WalkUpFromTagDecl(TagDecl *C) { 330e702bdb8SIlya Biryukov // Avoid building UnknownDeclaration here, syntatically 'struct X {}' and 331e702bdb8SIlya Biryukov // similar are part of declaration specifiers and do not introduce a new 332e702bdb8SIlya Biryukov // top-level declaration. 3339b3f38f9SIlya Biryukov return true; 3349b3f38f9SIlya Biryukov } 3359b3f38f9SIlya Biryukov 3369b3f38f9SIlya Biryukov bool WalkUpFromTranslationUnitDecl(TranslationUnitDecl *TU) { 3379b3f38f9SIlya Biryukov // (!) we do not want to call VisitDecl(), the declaration for translation 3389b3f38f9SIlya Biryukov // unit is built by finalize(). 3399b3f38f9SIlya Biryukov return true; 3409b3f38f9SIlya Biryukov } 3419b3f38f9SIlya Biryukov 3429b3f38f9SIlya Biryukov bool WalkUpFromCompoundStmt(CompoundStmt *S) { 34351dad419SIlya Biryukov using NodeRole = syntax::NodeRole; 3449b3f38f9SIlya Biryukov 345def65bb4SIlya Biryukov Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen); 34658fa50f4SIlya Biryukov for (auto *Child : S->body()) 34758fa50f4SIlya Biryukov Builder.markStmtChild(Child, NodeRole::CompoundStatement_statement); 348def65bb4SIlya Biryukov Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen); 3499b3f38f9SIlya Biryukov 35058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 3519b3f38f9SIlya Biryukov new (allocator()) syntax::CompoundStatement); 3529b3f38f9SIlya Biryukov return true; 3539b3f38f9SIlya Biryukov } 3549b3f38f9SIlya Biryukov 35558fa50f4SIlya Biryukov // Some statements are not yet handled by syntax trees. 35658fa50f4SIlya Biryukov bool WalkUpFromStmt(Stmt *S) { 35758fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 35858fa50f4SIlya Biryukov new (allocator()) syntax::UnknownStatement); 35958fa50f4SIlya Biryukov return true; 36058fa50f4SIlya Biryukov } 36158fa50f4SIlya Biryukov 36258fa50f4SIlya Biryukov bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) { 36358fa50f4SIlya Biryukov // We override to traverse range initializer as VarDecl. 36458fa50f4SIlya Biryukov // RAV traverses it as a statement, we produce invalid node kinds in that 36558fa50f4SIlya Biryukov // case. 36658fa50f4SIlya Biryukov // FIXME: should do this in RAV instead? 36758fa50f4SIlya Biryukov if (S->getInit() && !TraverseStmt(S->getInit())) 36858fa50f4SIlya Biryukov return false; 36958fa50f4SIlya Biryukov if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable())) 37058fa50f4SIlya Biryukov return false; 37158fa50f4SIlya Biryukov if (S->getRangeInit() && !TraverseStmt(S->getRangeInit())) 37258fa50f4SIlya Biryukov return false; 37358fa50f4SIlya Biryukov if (S->getBody() && !TraverseStmt(S->getBody())) 37458fa50f4SIlya Biryukov return false; 37558fa50f4SIlya Biryukov return true; 37658fa50f4SIlya Biryukov } 37758fa50f4SIlya Biryukov 37858fa50f4SIlya Biryukov bool TraverseStmt(Stmt *S) { 379e702bdb8SIlya Biryukov if (auto *DS = llvm::dyn_cast_or_null<DeclStmt>(S)) { 380e702bdb8SIlya Biryukov // We want to consume the semicolon, make sure SimpleDeclaration does not. 381e702bdb8SIlya Biryukov for (auto *D : DS->decls()) 382e702bdb8SIlya Biryukov Builder.noticeDeclaratorWithoutSemicolon(D); 383e702bdb8SIlya Biryukov } else if (auto *E = llvm::dyn_cast_or_null<Expr>(S)) { 38458fa50f4SIlya Biryukov // (!) do not recurse into subexpressions. 38558fa50f4SIlya Biryukov // we do not have syntax trees for expressions yet, so we only want to see 38658fa50f4SIlya Biryukov // the first top-level expression. 38758fa50f4SIlya Biryukov return WalkUpFromExpr(E->IgnoreImplicit()); 38858fa50f4SIlya Biryukov } 38958fa50f4SIlya Biryukov return RecursiveASTVisitor::TraverseStmt(S); 39058fa50f4SIlya Biryukov } 39158fa50f4SIlya Biryukov 39258fa50f4SIlya Biryukov // Some expressions are not yet handled by syntax trees. 39358fa50f4SIlya Biryukov bool WalkUpFromExpr(Expr *E) { 39458fa50f4SIlya Biryukov assert(!isImplicitExpr(E) && "should be handled by TraverseStmt"); 39558fa50f4SIlya Biryukov Builder.foldNode(Builder.getExprRange(E), 39658fa50f4SIlya Biryukov new (allocator()) syntax::UnknownExpression); 39758fa50f4SIlya Biryukov return true; 39858fa50f4SIlya Biryukov } 39958fa50f4SIlya Biryukov 400*be14a22bSIlya Biryukov bool WalkUpFromNamespaceDecl(NamespaceDecl *S) { 401*be14a22bSIlya Biryukov auto Tokens = Builder.getRange(S); 402*be14a22bSIlya Biryukov if (Tokens.front().kind() == tok::coloncolon) { 403*be14a22bSIlya Biryukov // Handle nested namespace definitions. Those start at '::' token, e.g. 404*be14a22bSIlya Biryukov // namespace a^::b {} 405*be14a22bSIlya Biryukov // FIXME: build corresponding nodes for the name of this namespace. 406*be14a22bSIlya Biryukov return true; 407*be14a22bSIlya Biryukov } 408*be14a22bSIlya Biryukov Builder.foldNode(Tokens, new (allocator()) syntax::NamespaceDefinition); 409*be14a22bSIlya Biryukov return true; 410*be14a22bSIlya Biryukov } 411*be14a22bSIlya Biryukov 41258fa50f4SIlya Biryukov // The code below is very regular, it could even be generated with some 41358fa50f4SIlya Biryukov // preprocessor magic. We merely assign roles to the corresponding children 41458fa50f4SIlya Biryukov // and fold resulting nodes. 41558fa50f4SIlya Biryukov bool WalkUpFromDeclStmt(DeclStmt *S) { 41658fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 41758fa50f4SIlya Biryukov new (allocator()) syntax::DeclarationStatement); 41858fa50f4SIlya Biryukov return true; 41958fa50f4SIlya Biryukov } 42058fa50f4SIlya Biryukov 42158fa50f4SIlya Biryukov bool WalkUpFromNullStmt(NullStmt *S) { 42258fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 42358fa50f4SIlya Biryukov new (allocator()) syntax::EmptyStatement); 42458fa50f4SIlya Biryukov return true; 42558fa50f4SIlya Biryukov } 42658fa50f4SIlya Biryukov 42758fa50f4SIlya Biryukov bool WalkUpFromSwitchStmt(SwitchStmt *S) { 428def65bb4SIlya Biryukov Builder.markChildToken(S->getSwitchLoc(), 42958fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 43058fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 43158fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 43258fa50f4SIlya Biryukov new (allocator()) syntax::SwitchStatement); 43358fa50f4SIlya Biryukov return true; 43458fa50f4SIlya Biryukov } 43558fa50f4SIlya Biryukov 43658fa50f4SIlya Biryukov bool WalkUpFromCaseStmt(CaseStmt *S) { 437def65bb4SIlya Biryukov Builder.markChildToken(S->getKeywordLoc(), 43858fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 43958fa50f4SIlya Biryukov Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseStatement_value); 44058fa50f4SIlya Biryukov Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 44158fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 44258fa50f4SIlya Biryukov new (allocator()) syntax::CaseStatement); 44358fa50f4SIlya Biryukov return true; 44458fa50f4SIlya Biryukov } 44558fa50f4SIlya Biryukov 44658fa50f4SIlya Biryukov bool WalkUpFromDefaultStmt(DefaultStmt *S) { 447def65bb4SIlya Biryukov Builder.markChildToken(S->getKeywordLoc(), 44858fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 44958fa50f4SIlya Biryukov Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 45058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 45158fa50f4SIlya Biryukov new (allocator()) syntax::DefaultStatement); 45258fa50f4SIlya Biryukov return true; 45358fa50f4SIlya Biryukov } 45458fa50f4SIlya Biryukov 45558fa50f4SIlya Biryukov bool WalkUpFromIfStmt(IfStmt *S) { 456def65bb4SIlya Biryukov Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword); 45758fa50f4SIlya Biryukov Builder.markStmtChild(S->getThen(), 45858fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_thenStatement); 459def65bb4SIlya Biryukov Builder.markChildToken(S->getElseLoc(), 46058fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_elseKeyword); 46158fa50f4SIlya Biryukov Builder.markStmtChild(S->getElse(), 46258fa50f4SIlya Biryukov syntax::NodeRole::IfStatement_elseStatement); 46358fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 46458fa50f4SIlya Biryukov new (allocator()) syntax::IfStatement); 46558fa50f4SIlya Biryukov return true; 46658fa50f4SIlya Biryukov } 46758fa50f4SIlya Biryukov 46858fa50f4SIlya Biryukov bool WalkUpFromForStmt(ForStmt *S) { 469def65bb4SIlya Biryukov Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 47058fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 47158fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 47258fa50f4SIlya Biryukov new (allocator()) syntax::ForStatement); 47358fa50f4SIlya Biryukov return true; 47458fa50f4SIlya Biryukov } 47558fa50f4SIlya Biryukov 47658fa50f4SIlya Biryukov bool WalkUpFromWhileStmt(WhileStmt *S) { 477def65bb4SIlya Biryukov Builder.markChildToken(S->getWhileLoc(), 47858fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 47958fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 48058fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 48158fa50f4SIlya Biryukov new (allocator()) syntax::WhileStatement); 48258fa50f4SIlya Biryukov return true; 48358fa50f4SIlya Biryukov } 48458fa50f4SIlya Biryukov 48558fa50f4SIlya Biryukov bool WalkUpFromContinueStmt(ContinueStmt *S) { 486def65bb4SIlya Biryukov Builder.markChildToken(S->getContinueLoc(), 48758fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 48858fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 48958fa50f4SIlya Biryukov new (allocator()) syntax::ContinueStatement); 49058fa50f4SIlya Biryukov return true; 49158fa50f4SIlya Biryukov } 49258fa50f4SIlya Biryukov 49358fa50f4SIlya Biryukov bool WalkUpFromBreakStmt(BreakStmt *S) { 494def65bb4SIlya Biryukov Builder.markChildToken(S->getBreakLoc(), 49558fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 49658fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 49758fa50f4SIlya Biryukov new (allocator()) syntax::BreakStatement); 49858fa50f4SIlya Biryukov return true; 49958fa50f4SIlya Biryukov } 50058fa50f4SIlya Biryukov 50158fa50f4SIlya Biryukov bool WalkUpFromReturnStmt(ReturnStmt *S) { 502def65bb4SIlya Biryukov Builder.markChildToken(S->getReturnLoc(), 50358fa50f4SIlya Biryukov syntax::NodeRole::IntroducerKeyword); 50458fa50f4SIlya Biryukov Builder.markExprChild(S->getRetValue(), 50558fa50f4SIlya Biryukov syntax::NodeRole::ReturnStatement_value); 50658fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 50758fa50f4SIlya Biryukov new (allocator()) syntax::ReturnStatement); 50858fa50f4SIlya Biryukov return true; 50958fa50f4SIlya Biryukov } 51058fa50f4SIlya Biryukov 51158fa50f4SIlya Biryukov bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) { 512def65bb4SIlya Biryukov Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 51358fa50f4SIlya Biryukov Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 51458fa50f4SIlya Biryukov Builder.foldNode(Builder.getStmtRange(S), 51558fa50f4SIlya Biryukov new (allocator()) syntax::RangeBasedForStatement); 51658fa50f4SIlya Biryukov return true; 51758fa50f4SIlya Biryukov } 51858fa50f4SIlya Biryukov 519*be14a22bSIlya Biryukov bool WalkUpFromEmptyDecl(EmptyDecl *S) { 520*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 521*be14a22bSIlya Biryukov new (allocator()) syntax::EmptyDeclaration); 522*be14a22bSIlya Biryukov return true; 523*be14a22bSIlya Biryukov } 524*be14a22bSIlya Biryukov 525*be14a22bSIlya Biryukov bool WalkUpFromStaticAssertDecl(StaticAssertDecl *S) { 526*be14a22bSIlya Biryukov Builder.markExprChild(S->getAssertExpr(), 527*be14a22bSIlya Biryukov syntax::NodeRole::StaticAssertDeclaration_condition); 528*be14a22bSIlya Biryukov Builder.markExprChild(S->getMessage(), 529*be14a22bSIlya Biryukov syntax::NodeRole::StaticAssertDeclaration_message); 530*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 531*be14a22bSIlya Biryukov new (allocator()) syntax::StaticAssertDeclaration); 532*be14a22bSIlya Biryukov return true; 533*be14a22bSIlya Biryukov } 534*be14a22bSIlya Biryukov 535*be14a22bSIlya Biryukov bool WalkUpFromLinkageSpecDecl(LinkageSpecDecl *S) { 536*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 537*be14a22bSIlya Biryukov new (allocator()) syntax::LinkageSpecificationDeclaration); 538*be14a22bSIlya Biryukov return true; 539*be14a22bSIlya Biryukov } 540*be14a22bSIlya Biryukov 541*be14a22bSIlya Biryukov bool WalkUpFromNamespaceAliasDecl(NamespaceAliasDecl *S) { 542*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 543*be14a22bSIlya Biryukov new (allocator()) syntax::NamespaceAliasDefinition); 544*be14a22bSIlya Biryukov return true; 545*be14a22bSIlya Biryukov } 546*be14a22bSIlya Biryukov 547*be14a22bSIlya Biryukov bool WalkUpFromUsingDirectiveDecl(UsingDirectiveDecl *S) { 548*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 549*be14a22bSIlya Biryukov new (allocator()) syntax::UsingNamespaceDirective); 550*be14a22bSIlya Biryukov return true; 551*be14a22bSIlya Biryukov } 552*be14a22bSIlya Biryukov 553*be14a22bSIlya Biryukov bool WalkUpFromUsingDecl(UsingDecl *S) { 554*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 555*be14a22bSIlya Biryukov new (allocator()) syntax::UsingDeclaration); 556*be14a22bSIlya Biryukov return true; 557*be14a22bSIlya Biryukov } 558*be14a22bSIlya Biryukov 559*be14a22bSIlya Biryukov bool WalkUpFromUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *S) { 560*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 561*be14a22bSIlya Biryukov new (allocator()) syntax::UsingDeclaration); 562*be14a22bSIlya Biryukov return true; 563*be14a22bSIlya Biryukov } 564*be14a22bSIlya Biryukov 565*be14a22bSIlya Biryukov bool WalkUpFromUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *S) { 566*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 567*be14a22bSIlya Biryukov new (allocator()) syntax::UsingDeclaration); 568*be14a22bSIlya Biryukov return true; 569*be14a22bSIlya Biryukov } 570*be14a22bSIlya Biryukov 571*be14a22bSIlya Biryukov bool WalkUpFromTypeAliasDecl(TypeAliasDecl *S) { 572*be14a22bSIlya Biryukov Builder.foldNode(Builder.getRange(S), 573*be14a22bSIlya Biryukov new (allocator()) syntax::TypeAliasDeclaration); 574*be14a22bSIlya Biryukov return true; 575*be14a22bSIlya Biryukov } 576*be14a22bSIlya Biryukov 5779b3f38f9SIlya Biryukov private: 5789b3f38f9SIlya Biryukov /// A small helper to save some typing. 5799b3f38f9SIlya Biryukov llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); } 5809b3f38f9SIlya Biryukov 5819b3f38f9SIlya Biryukov syntax::TreeBuilder &Builder; 5829b3f38f9SIlya Biryukov const LangOptions &LangOpts; 5839b3f38f9SIlya Biryukov }; 5849b3f38f9SIlya Biryukov } // namespace 5859b3f38f9SIlya Biryukov 5869b3f38f9SIlya Biryukov void syntax::TreeBuilder::foldNode(llvm::ArrayRef<syntax::Token> Range, 5879b3f38f9SIlya Biryukov syntax::Tree *New) { 5889b3f38f9SIlya Biryukov Pending.foldChildren(Range, New); 5899b3f38f9SIlya Biryukov } 5909b3f38f9SIlya Biryukov 591e702bdb8SIlya Biryukov void syntax::TreeBuilder::noticeDeclaratorRange( 592e702bdb8SIlya Biryukov llvm::ArrayRef<syntax::Token> Range) { 593e702bdb8SIlya Biryukov if (Pending.extendDelayedFold(Range)) 594e702bdb8SIlya Biryukov return; 595e702bdb8SIlya Biryukov Pending.foldChildrenDelayed(Range, 596e702bdb8SIlya Biryukov new (allocator()) syntax::SimpleDeclaration); 597e702bdb8SIlya Biryukov } 598e702bdb8SIlya Biryukov 599e702bdb8SIlya Biryukov void syntax::TreeBuilder::noticeDeclaratorWithoutSemicolon(Decl *D) { 600e702bdb8SIlya Biryukov DeclsWithoutSemicolons.insert(D); 601e702bdb8SIlya Biryukov } 602e702bdb8SIlya Biryukov 603def65bb4SIlya Biryukov void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) { 6049b3f38f9SIlya Biryukov if (Loc.isInvalid()) 6059b3f38f9SIlya Biryukov return; 6069b3f38f9SIlya Biryukov Pending.assignRole(*findToken(Loc), Role); 6079b3f38f9SIlya Biryukov } 6089b3f38f9SIlya Biryukov 60958fa50f4SIlya Biryukov void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) { 61058fa50f4SIlya Biryukov if (!Child) 61158fa50f4SIlya Biryukov return; 61258fa50f4SIlya Biryukov 61358fa50f4SIlya Biryukov auto Range = getStmtRange(Child); 61458fa50f4SIlya Biryukov // This is an expression in a statement position, consume the trailing 61558fa50f4SIlya Biryukov // semicolon and form an 'ExpressionStatement' node. 61658fa50f4SIlya Biryukov if (auto *E = dyn_cast<Expr>(Child)) { 61758fa50f4SIlya Biryukov Pending.assignRole(getExprRange(E), 61858fa50f4SIlya Biryukov NodeRole::ExpressionStatement_expression); 61958fa50f4SIlya Biryukov // (!) 'getRange(Stmt)' ensures this already covers a trailing semicolon. 62058fa50f4SIlya Biryukov Pending.foldChildren(Range, new (allocator()) syntax::ExpressionStatement); 62158fa50f4SIlya Biryukov } 62258fa50f4SIlya Biryukov Pending.assignRole(Range, Role); 62358fa50f4SIlya Biryukov } 62458fa50f4SIlya Biryukov 62558fa50f4SIlya Biryukov void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) { 626*be14a22bSIlya Biryukov if (!Child) 627*be14a22bSIlya Biryukov return; 628*be14a22bSIlya Biryukov 62958fa50f4SIlya Biryukov Pending.assignRole(getExprRange(Child), Role); 63058fa50f4SIlya Biryukov } 63158fa50f4SIlya Biryukov 6329b3f38f9SIlya Biryukov const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const { 6339b3f38f9SIlya Biryukov auto Tokens = Arena.tokenBuffer().expandedTokens(); 6349b3f38f9SIlya Biryukov auto &SM = Arena.sourceManager(); 6359b3f38f9SIlya Biryukov auto It = llvm::partition_point(Tokens, [&](const syntax::Token &T) { 6369b3f38f9SIlya Biryukov return SM.isBeforeInTranslationUnit(T.location(), L); 6379b3f38f9SIlya Biryukov }); 6389b3f38f9SIlya Biryukov assert(It != Tokens.end()); 6399b3f38f9SIlya Biryukov assert(It->location() == L); 6409b3f38f9SIlya Biryukov return &*It; 6419b3f38f9SIlya Biryukov } 6429b3f38f9SIlya Biryukov 6439b3f38f9SIlya Biryukov syntax::TranslationUnit * 6449b3f38f9SIlya Biryukov syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) { 6459b3f38f9SIlya Biryukov TreeBuilder Builder(A); 6469b3f38f9SIlya Biryukov BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext()); 6479b3f38f9SIlya Biryukov return std::move(Builder).finalize(); 6489b3f38f9SIlya Biryukov } 649