1 //===- BuildTree.cpp ------------------------------------------*- 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 #include "clang/Tooling/Syntax/BuildTree.h" 9 #include "clang/AST/Decl.h" 10 #include "clang/AST/DeclBase.h" 11 #include "clang/AST/RecursiveASTVisitor.h" 12 #include "clang/AST/Stmt.h" 13 #include "clang/Basic/LLVM.h" 14 #include "clang/Basic/SourceLocation.h" 15 #include "clang/Basic/SourceManager.h" 16 #include "clang/Basic/TokenKinds.h" 17 #include "clang/Lex/Lexer.h" 18 #include "clang/Tooling/Syntax/Nodes.h" 19 #include "clang/Tooling/Syntax/Tokens.h" 20 #include "clang/Tooling/Syntax/Tree.h" 21 #include "llvm/ADT/ArrayRef.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/Support/Allocator.h" 25 #include "llvm/Support/Casting.h" 26 #include "llvm/Support/Compiler.h" 27 #include "llvm/Support/FormatVariadic.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <map> 30 31 using namespace clang; 32 33 LLVM_ATTRIBUTE_UNUSED 34 static bool isImplicitExpr(clang::Expr *E) { return E->IgnoreImplicit() != E; } 35 36 /// A helper class for constructing the syntax tree while traversing a clang 37 /// AST. 38 /// 39 /// At each point of the traversal we maintain a list of pending nodes. 40 /// Initially all tokens are added as pending nodes. When processing a clang AST 41 /// node, the clients need to: 42 /// - create a corresponding syntax node, 43 /// - assign roles to all pending child nodes with 'markChild' and 44 /// 'markChildToken', 45 /// - replace the child nodes with the new syntax node in the pending list 46 /// with 'foldNode'. 47 /// 48 /// Note that all children are expected to be processed when building a node. 49 /// 50 /// Call finalize() to finish building the tree and consume the root node. 51 class syntax::TreeBuilder { 52 public: 53 TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) {} 54 55 llvm::BumpPtrAllocator &allocator() { return Arena.allocator(); } 56 57 /// Populate children for \p New node, assuming it covers tokens from \p 58 /// Range. 59 void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree *New); 60 61 /// Must be called with the range of each `DeclaratorDecl`. Ensures the 62 /// corresponding declarator nodes are covered by `SimpleDeclaration`. 63 void noticeDeclaratorRange(llvm::ArrayRef<syntax::Token> Range); 64 65 /// Notifies that we should not consume trailing semicolon when computing 66 /// token range of \p D. 67 void noticeDeclaratorWithoutSemicolon(Decl *D); 68 69 /// Mark the \p Child node with a corresponding \p Role. All marked children 70 /// should be consumed by foldNode. 71 /// (!) when called on expressions (clang::Expr is derived from clang::Stmt), 72 /// wraps expressions into expression statement. 73 void markStmtChild(Stmt *Child, NodeRole Role); 74 /// Should be called for expressions in non-statement position to avoid 75 /// wrapping into expression statement. 76 void markExprChild(Expr *Child, NodeRole Role); 77 78 /// Set role for a token starting at \p Loc. 79 void markChildToken(SourceLocation Loc, NodeRole R); 80 81 /// Finish building the tree and consume the root node. 82 syntax::TranslationUnit *finalize() && { 83 auto Tokens = Arena.tokenBuffer().expandedTokens(); 84 assert(!Tokens.empty()); 85 assert(Tokens.back().kind() == tok::eof); 86 87 // Build the root of the tree, consuming all the children. 88 Pending.foldChildren(Tokens.drop_back(), 89 new (Arena.allocator()) syntax::TranslationUnit); 90 91 return cast<syntax::TranslationUnit>(std::move(Pending).finalize()); 92 } 93 94 /// getRange() finds the syntax tokens corresponding to the passed source 95 /// locations. 96 /// \p First is the start position of the first token and \p Last is the start 97 /// position of the last token. 98 llvm::ArrayRef<syntax::Token> getRange(SourceLocation First, 99 SourceLocation Last) const { 100 assert(First.isValid()); 101 assert(Last.isValid()); 102 assert(First == Last || 103 Arena.sourceManager().isBeforeInTranslationUnit(First, Last)); 104 return llvm::makeArrayRef(findToken(First), std::next(findToken(Last))); 105 } 106 llvm::ArrayRef<syntax::Token> getRange(const Decl *D) const { 107 auto Tokens = getRange(D->getBeginLoc(), D->getEndLoc()); 108 if (llvm::isa<NamespaceDecl>(D)) 109 return Tokens; 110 if (DeclsWithoutSemicolons.count(D)) 111 return Tokens; 112 // FIXME: do not consume trailing semicolon on function definitions. 113 // Most declarations own a semicolon in syntax trees, but not in clang AST. 114 return withTrailingSemicolon(Tokens); 115 } 116 llvm::ArrayRef<syntax::Token> getExprRange(const Expr *E) const { 117 return getRange(E->getBeginLoc(), E->getEndLoc()); 118 } 119 /// Find the adjusted range for the statement, consuming the trailing 120 /// semicolon when needed. 121 llvm::ArrayRef<syntax::Token> getStmtRange(const Stmt *S) const { 122 auto Tokens = getRange(S->getBeginLoc(), S->getEndLoc()); 123 if (isa<CompoundStmt>(S)) 124 return Tokens; 125 126 // Some statements miss a trailing semicolon, e.g. 'return', 'continue' and 127 // all statements that end with those. Consume this semicolon here. 128 if (Tokens.back().kind() == tok::semi) 129 return Tokens; 130 return withTrailingSemicolon(Tokens); 131 } 132 133 private: 134 llvm::ArrayRef<syntax::Token> 135 withTrailingSemicolon(llvm::ArrayRef<syntax::Token> Tokens) const { 136 assert(!Tokens.empty()); 137 assert(Tokens.back().kind() != tok::eof); 138 // (!) we never consume 'eof', so looking at the next token is ok. 139 if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi) 140 return llvm::makeArrayRef(Tokens.begin(), Tokens.end() + 1); 141 return Tokens; 142 } 143 144 /// Finds a token starting at \p L. The token must exist. 145 const syntax::Token *findToken(SourceLocation L) const; 146 147 /// A collection of trees covering the input tokens. 148 /// When created, each tree corresponds to a single token in the file. 149 /// Clients call 'foldChildren' to attach one or more subtrees to a parent 150 /// node and update the list of trees accordingly. 151 /// 152 /// Ensures that added nodes properly nest and cover the whole token stream. 153 struct Forest { 154 Forest(syntax::Arena &A) { 155 assert(!A.tokenBuffer().expandedTokens().empty()); 156 assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof); 157 // Create all leaf nodes. 158 // Note that we do not have 'eof' in the tree. 159 for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) 160 Trees.insert(Trees.end(), 161 {&T, NodeAndRole{new (A.allocator()) syntax::Leaf(&T)}}); 162 } 163 164 ~Forest() { assert(DelayedFolds.empty()); } 165 166 void assignRole(llvm::ArrayRef<syntax::Token> Range, 167 syntax::NodeRole Role) { 168 assert(!Range.empty()); 169 auto It = Trees.lower_bound(Range.begin()); 170 assert(It != Trees.end() && "no node found"); 171 assert(It->first == Range.begin() && "no child with the specified range"); 172 assert((std::next(It) == Trees.end() || 173 std::next(It)->first == Range.end()) && 174 "no child with the specified range"); 175 It->second.Role = Role; 176 } 177 178 /// Add \p Node to the forest and attach child nodes based on \p Tokens. 179 void foldChildren(llvm::ArrayRef<syntax::Token> Tokens, 180 syntax::Tree *Node) { 181 // Execute delayed folds inside `Tokens`. 182 auto BeginExecuted = DelayedFolds.lower_bound(Tokens.begin()); 183 auto It = BeginExecuted; 184 for (; It != DelayedFolds.end() && It->second.End <= Tokens.end(); ++It) 185 foldChildrenEager(llvm::makeArrayRef(It->first, It->second.End), 186 It->second.Node); 187 DelayedFolds.erase(BeginExecuted, It); 188 189 // Attach children to `Node`. 190 foldChildrenEager(Tokens, Node); 191 } 192 193 /// Schedule a call to `foldChildren` that will only be executed when 194 /// containing node is folded. The range of delayed nodes can be extended by 195 /// calling `extendDelayedFold`. Only one delayed node for each starting 196 /// token is allowed. 197 void foldChildrenDelayed(llvm::ArrayRef<syntax::Token> Tokens, 198 syntax::Tree *Node) { 199 assert(!Tokens.empty()); 200 bool Inserted = 201 DelayedFolds.insert({Tokens.begin(), DelayedFold{Tokens.end(), Node}}) 202 .second; 203 (void)Inserted; 204 assert(Inserted && "Multiple delayed folds start at the same token"); 205 } 206 207 /// If there a delayed fold, starting at `ExtendedRange.begin()`, extends 208 /// its endpoint to `ExtendedRange.end()` and returns true. 209 /// Otherwise, returns false. 210 bool extendDelayedFold(llvm::ArrayRef<syntax::Token> ExtendedRange) { 211 assert(!ExtendedRange.empty()); 212 auto It = DelayedFolds.find(ExtendedRange.data()); 213 if (It == DelayedFolds.end()) 214 return false; 215 assert(It->second.End <= ExtendedRange.end()); 216 It->second.End = ExtendedRange.end(); 217 return true; 218 } 219 220 // EXPECTS: all tokens were consumed and are owned by a single root node. 221 syntax::Node *finalize() && { 222 assert(Trees.size() == 1); 223 auto *Root = Trees.begin()->second.Node; 224 Trees = {}; 225 return Root; 226 } 227 228 std::string str(const syntax::Arena &A) const { 229 std::string R; 230 for (auto It = Trees.begin(); It != Trees.end(); ++It) { 231 unsigned CoveredTokens = 232 It != Trees.end() 233 ? (std::next(It)->first - It->first) 234 : A.tokenBuffer().expandedTokens().end() - It->first; 235 236 R += llvm::formatv("- '{0}' covers '{1}'+{2} tokens\n", 237 It->second.Node->kind(), 238 It->first->text(A.sourceManager()), CoveredTokens); 239 R += It->second.Node->dump(A); 240 } 241 return R; 242 } 243 244 private: 245 /// Implementation detail of `foldChildren`, does acutal folding ignoring 246 /// delayed folds. 247 void foldChildrenEager(llvm::ArrayRef<syntax::Token> Tokens, 248 syntax::Tree *Node) { 249 assert(Node->firstChild() == nullptr && "node already has children"); 250 251 auto *FirstToken = Tokens.begin(); 252 auto BeginChildren = Trees.lower_bound(FirstToken); 253 assert((BeginChildren == Trees.end() || 254 BeginChildren->first == FirstToken) && 255 "fold crosses boundaries of existing subtrees"); 256 auto EndChildren = Trees.lower_bound(Tokens.end()); 257 assert( 258 (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) && 259 "fold crosses boundaries of existing subtrees"); 260 261 // (!) we need to go in reverse order, because we can only prepend. 262 for (auto It = EndChildren; It != BeginChildren; --It) 263 Node->prependChildLowLevel(std::prev(It)->second.Node, 264 std::prev(It)->second.Role); 265 266 Trees.erase(BeginChildren, EndChildren); 267 Trees.insert({FirstToken, NodeAndRole(Node)}); 268 } 269 /// A with a role that should be assigned to it when adding to a parent. 270 struct NodeAndRole { 271 explicit NodeAndRole(syntax::Node *Node) 272 : Node(Node), Role(NodeRole::Unknown) {} 273 274 syntax::Node *Node; 275 NodeRole Role; 276 }; 277 278 /// Maps from the start token to a subtree starting at that token. 279 /// Keys in the map are pointers into the array of expanded tokens, so 280 /// pointer order corresponds to the order of preprocessor tokens. 281 /// FIXME: storing the end tokens is redundant. 282 /// FIXME: the key of a map is redundant, it is also stored in NodeForRange. 283 std::map<const syntax::Token *, NodeAndRole> Trees; 284 285 /// See documentation of `foldChildrenDelayed` for details. 286 struct DelayedFold { 287 const syntax::Token *End = nullptr; 288 syntax::Tree *Node = nullptr; 289 }; 290 std::map<const syntax::Token *, DelayedFold> DelayedFolds; 291 }; 292 293 /// For debugging purposes. 294 std::string str() { return Pending.str(Arena); } 295 296 syntax::Arena &Arena; 297 Forest Pending; 298 llvm::DenseSet<Decl*> DeclsWithoutSemicolons; 299 }; 300 301 namespace { 302 class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> { 303 public: 304 explicit BuildTreeVisitor(ASTContext &Ctx, syntax::TreeBuilder &Builder) 305 : Builder(Builder), LangOpts(Ctx.getLangOpts()) {} 306 307 bool shouldTraversePostOrder() const { return true; } 308 309 bool WalkUpFromDeclaratorDecl(DeclaratorDecl *D) { 310 // Ensure declarators are covered by SimpleDeclaration. 311 Builder.noticeDeclaratorRange(Builder.getRange(D)); 312 // FIXME: build nodes for the declarator too. 313 return true; 314 } 315 bool WalkUpFromTypedefNameDecl(TypedefNameDecl *D) { 316 // Also a declarator. 317 Builder.noticeDeclaratorRange(Builder.getRange(D)); 318 // FIXME: build nodes for the declarator too. 319 return true; 320 } 321 322 bool VisitDecl(Decl *D) { 323 assert(!D->isImplicit()); 324 Builder.foldNode(Builder.getRange(D), 325 new (allocator()) syntax::UnknownDeclaration()); 326 return true; 327 } 328 329 bool WalkUpFromTagDecl(TagDecl *C) { 330 // Avoid building UnknownDeclaration here, syntatically 'struct X {}' and 331 // similar are part of declaration specifiers and do not introduce a new 332 // top-level declaration. 333 return true; 334 } 335 336 bool WalkUpFromTranslationUnitDecl(TranslationUnitDecl *TU) { 337 // (!) we do not want to call VisitDecl(), the declaration for translation 338 // unit is built by finalize(). 339 return true; 340 } 341 342 bool WalkUpFromCompoundStmt(CompoundStmt *S) { 343 using NodeRole = syntax::NodeRole; 344 345 Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen); 346 for (auto *Child : S->body()) 347 Builder.markStmtChild(Child, NodeRole::CompoundStatement_statement); 348 Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen); 349 350 Builder.foldNode(Builder.getStmtRange(S), 351 new (allocator()) syntax::CompoundStatement); 352 return true; 353 } 354 355 // Some statements are not yet handled by syntax trees. 356 bool WalkUpFromStmt(Stmt *S) { 357 Builder.foldNode(Builder.getStmtRange(S), 358 new (allocator()) syntax::UnknownStatement); 359 return true; 360 } 361 362 bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) { 363 // We override to traverse range initializer as VarDecl. 364 // RAV traverses it as a statement, we produce invalid node kinds in that 365 // case. 366 // FIXME: should do this in RAV instead? 367 if (S->getInit() && !TraverseStmt(S->getInit())) 368 return false; 369 if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable())) 370 return false; 371 if (S->getRangeInit() && !TraverseStmt(S->getRangeInit())) 372 return false; 373 if (S->getBody() && !TraverseStmt(S->getBody())) 374 return false; 375 return true; 376 } 377 378 bool TraverseStmt(Stmt *S) { 379 if (auto *DS = llvm::dyn_cast_or_null<DeclStmt>(S)) { 380 // We want to consume the semicolon, make sure SimpleDeclaration does not. 381 for (auto *D : DS->decls()) 382 Builder.noticeDeclaratorWithoutSemicolon(D); 383 } else if (auto *E = llvm::dyn_cast_or_null<Expr>(S)) { 384 // (!) do not recurse into subexpressions. 385 // we do not have syntax trees for expressions yet, so we only want to see 386 // the first top-level expression. 387 return WalkUpFromExpr(E->IgnoreImplicit()); 388 } 389 return RecursiveASTVisitor::TraverseStmt(S); 390 } 391 392 // Some expressions are not yet handled by syntax trees. 393 bool WalkUpFromExpr(Expr *E) { 394 assert(!isImplicitExpr(E) && "should be handled by TraverseStmt"); 395 Builder.foldNode(Builder.getExprRange(E), 396 new (allocator()) syntax::UnknownExpression); 397 return true; 398 } 399 400 // The code below is very regular, it could even be generated with some 401 // preprocessor magic. We merely assign roles to the corresponding children 402 // and fold resulting nodes. 403 bool WalkUpFromDeclStmt(DeclStmt *S) { 404 Builder.foldNode(Builder.getStmtRange(S), 405 new (allocator()) syntax::DeclarationStatement); 406 return true; 407 } 408 409 bool WalkUpFromNullStmt(NullStmt *S) { 410 Builder.foldNode(Builder.getStmtRange(S), 411 new (allocator()) syntax::EmptyStatement); 412 return true; 413 } 414 415 bool WalkUpFromSwitchStmt(SwitchStmt *S) { 416 Builder.markChildToken(S->getSwitchLoc(), 417 syntax::NodeRole::IntroducerKeyword); 418 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 419 Builder.foldNode(Builder.getStmtRange(S), 420 new (allocator()) syntax::SwitchStatement); 421 return true; 422 } 423 424 bool WalkUpFromCaseStmt(CaseStmt *S) { 425 Builder.markChildToken(S->getKeywordLoc(), 426 syntax::NodeRole::IntroducerKeyword); 427 Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseStatement_value); 428 Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 429 Builder.foldNode(Builder.getStmtRange(S), 430 new (allocator()) syntax::CaseStatement); 431 return true; 432 } 433 434 bool WalkUpFromDefaultStmt(DefaultStmt *S) { 435 Builder.markChildToken(S->getKeywordLoc(), 436 syntax::NodeRole::IntroducerKeyword); 437 Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement); 438 Builder.foldNode(Builder.getStmtRange(S), 439 new (allocator()) syntax::DefaultStatement); 440 return true; 441 } 442 443 bool WalkUpFromIfStmt(IfStmt *S) { 444 Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword); 445 Builder.markStmtChild(S->getThen(), 446 syntax::NodeRole::IfStatement_thenStatement); 447 Builder.markChildToken(S->getElseLoc(), 448 syntax::NodeRole::IfStatement_elseKeyword); 449 Builder.markStmtChild(S->getElse(), 450 syntax::NodeRole::IfStatement_elseStatement); 451 Builder.foldNode(Builder.getStmtRange(S), 452 new (allocator()) syntax::IfStatement); 453 return true; 454 } 455 456 bool WalkUpFromForStmt(ForStmt *S) { 457 Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 458 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 459 Builder.foldNode(Builder.getStmtRange(S), 460 new (allocator()) syntax::ForStatement); 461 return true; 462 } 463 464 bool WalkUpFromWhileStmt(WhileStmt *S) { 465 Builder.markChildToken(S->getWhileLoc(), 466 syntax::NodeRole::IntroducerKeyword); 467 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 468 Builder.foldNode(Builder.getStmtRange(S), 469 new (allocator()) syntax::WhileStatement); 470 return true; 471 } 472 473 bool WalkUpFromContinueStmt(ContinueStmt *S) { 474 Builder.markChildToken(S->getContinueLoc(), 475 syntax::NodeRole::IntroducerKeyword); 476 Builder.foldNode(Builder.getStmtRange(S), 477 new (allocator()) syntax::ContinueStatement); 478 return true; 479 } 480 481 bool WalkUpFromBreakStmt(BreakStmt *S) { 482 Builder.markChildToken(S->getBreakLoc(), 483 syntax::NodeRole::IntroducerKeyword); 484 Builder.foldNode(Builder.getStmtRange(S), 485 new (allocator()) syntax::BreakStatement); 486 return true; 487 } 488 489 bool WalkUpFromReturnStmt(ReturnStmt *S) { 490 Builder.markChildToken(S->getReturnLoc(), 491 syntax::NodeRole::IntroducerKeyword); 492 Builder.markExprChild(S->getRetValue(), 493 syntax::NodeRole::ReturnStatement_value); 494 Builder.foldNode(Builder.getStmtRange(S), 495 new (allocator()) syntax::ReturnStatement); 496 return true; 497 } 498 499 bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) { 500 Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword); 501 Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement); 502 Builder.foldNode(Builder.getStmtRange(S), 503 new (allocator()) syntax::RangeBasedForStatement); 504 return true; 505 } 506 507 private: 508 /// A small helper to save some typing. 509 llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); } 510 511 syntax::TreeBuilder &Builder; 512 const LangOptions &LangOpts; 513 }; 514 } // namespace 515 516 void syntax::TreeBuilder::foldNode(llvm::ArrayRef<syntax::Token> Range, 517 syntax::Tree *New) { 518 Pending.foldChildren(Range, New); 519 } 520 521 void syntax::TreeBuilder::noticeDeclaratorRange( 522 llvm::ArrayRef<syntax::Token> Range) { 523 if (Pending.extendDelayedFold(Range)) 524 return; 525 Pending.foldChildrenDelayed(Range, 526 new (allocator()) syntax::SimpleDeclaration); 527 } 528 529 void syntax::TreeBuilder::noticeDeclaratorWithoutSemicolon(Decl *D) { 530 DeclsWithoutSemicolons.insert(D); 531 } 532 533 void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) { 534 if (Loc.isInvalid()) 535 return; 536 Pending.assignRole(*findToken(Loc), Role); 537 } 538 539 void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) { 540 if (!Child) 541 return; 542 543 auto Range = getStmtRange(Child); 544 // This is an expression in a statement position, consume the trailing 545 // semicolon and form an 'ExpressionStatement' node. 546 if (auto *E = dyn_cast<Expr>(Child)) { 547 Pending.assignRole(getExprRange(E), 548 NodeRole::ExpressionStatement_expression); 549 // (!) 'getRange(Stmt)' ensures this already covers a trailing semicolon. 550 Pending.foldChildren(Range, new (allocator()) syntax::ExpressionStatement); 551 } 552 Pending.assignRole(Range, Role); 553 } 554 555 void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) { 556 Pending.assignRole(getExprRange(Child), Role); 557 } 558 559 const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const { 560 auto Tokens = Arena.tokenBuffer().expandedTokens(); 561 auto &SM = Arena.sourceManager(); 562 auto It = llvm::partition_point(Tokens, [&](const syntax::Token &T) { 563 return SM.isBeforeInTranslationUnit(T.location(), L); 564 }); 565 assert(It != Tokens.end()); 566 assert(It->location() == L); 567 return &*It; 568 } 569 570 syntax::TranslationUnit * 571 syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) { 572 TreeBuilder Builder(A); 573 BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext()); 574 return std::move(Builder).finalize(); 575 } 576