xref: /llvm-project/clang/lib/Tooling/Syntax/BuildTree.cpp (revision f5087d5c7248104b6580c7b079ed5f227332c2ef)
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/ASTFwd.h"
10 #include "clang/AST/Decl.h"
11 #include "clang/AST/DeclBase.h"
12 #include "clang/AST/DeclCXX.h"
13 #include "clang/AST/DeclarationName.h"
14 #include "clang/AST/Expr.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/AST/IgnoreExpr.h"
17 #include "clang/AST/OperationKinds.h"
18 #include "clang/AST/RecursiveASTVisitor.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/AST/TypeLoc.h"
21 #include "clang/AST/TypeLocVisitor.h"
22 #include "clang/Basic/LLVM.h"
23 #include "clang/Basic/SourceLocation.h"
24 #include "clang/Basic/SourceManager.h"
25 #include "clang/Basic/Specifiers.h"
26 #include "clang/Basic/TokenKinds.h"
27 #include "clang/Lex/Lexer.h"
28 #include "clang/Lex/LiteralSupport.h"
29 #include "clang/Tooling/Syntax/Nodes.h"
30 #include "clang/Tooling/Syntax/Tokens.h"
31 #include "clang/Tooling/Syntax/Tree.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/PointerUnion.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/ScopeExit.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/Support/Allocator.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/FormatVariadic.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include <cstddef>
45 #include <map>
46 
47 using namespace clang;
48 
49 // Ignores the implicit `CXXConstructExpr` for copy/move constructor calls
50 // generated by the compiler, as well as in implicit conversions like the one
51 // wrapping `1` in `X x = 1;`.
52 static Expr *IgnoreImplicitConstructorSingleStep(Expr *E) {
53   if (auto *C = dyn_cast<CXXConstructExpr>(E)) {
54     auto NumArgs = C->getNumArgs();
55     if (NumArgs == 1 || (NumArgs > 1 && isa<CXXDefaultArgExpr>(C->getArg(1)))) {
56       Expr *A = C->getArg(0);
57       if (C->getParenOrBraceRange().isInvalid())
58         return A;
59     }
60   }
61   return E;
62 }
63 
64 // In:
65 // struct X {
66 //   X(int)
67 // };
68 // X x = X(1);
69 // Ignores the implicit `CXXFunctionalCastExpr` that wraps
70 // `CXXConstructExpr X(1)`.
71 static Expr *IgnoreCXXFunctionalCastExprWrappingConstructor(Expr *E) {
72   if (auto *F = dyn_cast<CXXFunctionalCastExpr>(E)) {
73     if (F->getCastKind() == CK_ConstructorConversion)
74       return F->getSubExpr();
75   }
76   return E;
77 }
78 
79 static Expr *IgnoreImplicit(Expr *E) {
80   return IgnoreExprNodes(E, IgnoreImplicitSingleStep,
81                          IgnoreImplicitConstructorSingleStep,
82                          IgnoreCXXFunctionalCastExprWrappingConstructor);
83 }
84 
85 LLVM_ATTRIBUTE_UNUSED
86 static bool isImplicitExpr(Expr *E) { return IgnoreImplicit(E) != E; }
87 
88 namespace {
89 /// Get start location of the Declarator from the TypeLoc.
90 /// E.g.:
91 ///   loc of `(` in `int (a)`
92 ///   loc of `*` in `int *(a)`
93 ///   loc of the first `(` in `int (*a)(int)`
94 ///   loc of the `*` in `int *(a)(int)`
95 ///   loc of the first `*` in `const int *const *volatile a;`
96 ///
97 /// It is non-trivial to get the start location because TypeLocs are stored
98 /// inside out. In the example above `*volatile` is the TypeLoc returned
99 /// by `Decl.getTypeSourceInfo()`, and `*const` is what `.getPointeeLoc()`
100 /// returns.
101 struct GetStartLoc : TypeLocVisitor<GetStartLoc, SourceLocation> {
102   SourceLocation VisitParenTypeLoc(ParenTypeLoc T) {
103     auto L = Visit(T.getInnerLoc());
104     if (L.isValid())
105       return L;
106     return T.getLParenLoc();
107   }
108 
109   // Types spelled in the prefix part of the declarator.
110   SourceLocation VisitPointerTypeLoc(PointerTypeLoc T) {
111     return HandlePointer(T);
112   }
113 
114   SourceLocation VisitMemberPointerTypeLoc(MemberPointerTypeLoc T) {
115     return HandlePointer(T);
116   }
117 
118   SourceLocation VisitBlockPointerTypeLoc(BlockPointerTypeLoc T) {
119     return HandlePointer(T);
120   }
121 
122   SourceLocation VisitReferenceTypeLoc(ReferenceTypeLoc T) {
123     return HandlePointer(T);
124   }
125 
126   SourceLocation VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc T) {
127     return HandlePointer(T);
128   }
129 
130   // All other cases are not important, as they are either part of declaration
131   // specifiers (e.g. inheritors of TypeSpecTypeLoc) or introduce modifiers on
132   // existing declarators (e.g. QualifiedTypeLoc). They cannot start the
133   // declarator themselves, but their underlying type can.
134   SourceLocation VisitTypeLoc(TypeLoc T) {
135     auto N = T.getNextTypeLoc();
136     if (!N)
137       return SourceLocation();
138     return Visit(N);
139   }
140 
141   SourceLocation VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc T) {
142     if (T.getTypePtr()->hasTrailingReturn())
143       return SourceLocation(); // avoid recursing into the suffix of declarator.
144     return VisitTypeLoc(T);
145   }
146 
147 private:
148   template <class PtrLoc> SourceLocation HandlePointer(PtrLoc T) {
149     auto L = Visit(T.getPointeeLoc());
150     if (L.isValid())
151       return L;
152     return T.getLocalSourceRange().getBegin();
153   }
154 };
155 } // namespace
156 
157 static CallExpr::arg_range dropDefaultArgs(CallExpr::arg_range Args) {
158   auto firstDefaultArg = std::find_if(Args.begin(), Args.end(), [](auto it) {
159     return isa<CXXDefaultArgExpr>(it);
160   });
161   return llvm::make_range(Args.begin(), firstDefaultArg);
162 }
163 
164 static syntax::NodeKind getOperatorNodeKind(const CXXOperatorCallExpr &E) {
165   switch (E.getOperator()) {
166   // Comparison
167   case OO_EqualEqual:
168   case OO_ExclaimEqual:
169   case OO_Greater:
170   case OO_GreaterEqual:
171   case OO_Less:
172   case OO_LessEqual:
173   case OO_Spaceship:
174   // Assignment
175   case OO_Equal:
176   case OO_SlashEqual:
177   case OO_PercentEqual:
178   case OO_CaretEqual:
179   case OO_PipeEqual:
180   case OO_LessLessEqual:
181   case OO_GreaterGreaterEqual:
182   case OO_PlusEqual:
183   case OO_MinusEqual:
184   case OO_StarEqual:
185   case OO_AmpEqual:
186   // Binary computation
187   case OO_Slash:
188   case OO_Percent:
189   case OO_Caret:
190   case OO_Pipe:
191   case OO_LessLess:
192   case OO_GreaterGreater:
193   case OO_AmpAmp:
194   case OO_PipePipe:
195   case OO_ArrowStar:
196   case OO_Comma:
197     return syntax::NodeKind::BinaryOperatorExpression;
198   case OO_Tilde:
199   case OO_Exclaim:
200     return syntax::NodeKind::PrefixUnaryOperatorExpression;
201   // Prefix/Postfix increment/decrement
202   case OO_PlusPlus:
203   case OO_MinusMinus:
204     switch (E.getNumArgs()) {
205     case 1:
206       return syntax::NodeKind::PrefixUnaryOperatorExpression;
207     case 2:
208       return syntax::NodeKind::PostfixUnaryOperatorExpression;
209     default:
210       llvm_unreachable("Invalid number of arguments for operator");
211     }
212   // Operators that can be unary or binary
213   case OO_Plus:
214   case OO_Minus:
215   case OO_Star:
216   case OO_Amp:
217     switch (E.getNumArgs()) {
218     case 1:
219       return syntax::NodeKind::PrefixUnaryOperatorExpression;
220     case 2:
221       return syntax::NodeKind::BinaryOperatorExpression;
222     default:
223       llvm_unreachable("Invalid number of arguments for operator");
224     }
225     return syntax::NodeKind::BinaryOperatorExpression;
226   // Not yet supported by SyntaxTree
227   case OO_New:
228   case OO_Delete:
229   case OO_Array_New:
230   case OO_Array_Delete:
231   case OO_Coawait:
232   case OO_Subscript:
233   case OO_Arrow:
234     return syntax::NodeKind::UnknownExpression;
235   case OO_Call:
236     return syntax::NodeKind::CallExpression;
237   case OO_Conditional: // not overloadable
238   case NUM_OVERLOADED_OPERATORS:
239   case OO_None:
240     llvm_unreachable("Not an overloadable operator");
241   }
242   llvm_unreachable("Unknown OverloadedOperatorKind enum");
243 }
244 
245 /// Get the start of the qualified name. In the examples below it gives the
246 /// location of the `^`:
247 ///     `int ^a;`
248 ///     `int *^a;`
249 ///     `int ^a::S::f(){}`
250 static SourceLocation getQualifiedNameStart(NamedDecl *D) {
251   assert((isa<DeclaratorDecl, TypedefNameDecl>(D)) &&
252          "only DeclaratorDecl and TypedefNameDecl are supported.");
253 
254   auto DN = D->getDeclName();
255   bool IsAnonymous = DN.isIdentifier() && !DN.getAsIdentifierInfo();
256   if (IsAnonymous)
257     return SourceLocation();
258 
259   if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
260     if (DD->getQualifierLoc()) {
261       return DD->getQualifierLoc().getBeginLoc();
262     }
263   }
264 
265   return D->getLocation();
266 }
267 
268 /// Gets the range of the initializer inside an init-declarator C++ [dcl.decl].
269 ///     `int a;` -> range of ``,
270 ///     `int *a = nullptr` -> range of `= nullptr`.
271 ///     `int a{}` -> range of `{}`.
272 ///     `int a()` -> range of `()`.
273 static SourceRange getInitializerRange(Decl *D) {
274   if (auto *V = dyn_cast<VarDecl>(D)) {
275     auto *I = V->getInit();
276     // Initializers in range-based-for are not part of the declarator
277     if (I && !V->isCXXForRangeDecl())
278       return I->getSourceRange();
279   }
280 
281   return SourceRange();
282 }
283 
284 /// Gets the range of declarator as defined by the C++ grammar. E.g.
285 ///     `int a;` -> range of `a`,
286 ///     `int *a;` -> range of `*a`,
287 ///     `int a[10];` -> range of `a[10]`,
288 ///     `int a[1][2][3];` -> range of `a[1][2][3]`,
289 ///     `int *a = nullptr` -> range of `*a = nullptr`.
290 ///     `int S::f(){}` -> range of `S::f()`.
291 /// FIXME: \p Name must be a source range.
292 static SourceRange getDeclaratorRange(const SourceManager &SM, TypeLoc T,
293                                       SourceLocation Name,
294                                       SourceRange Initializer) {
295   SourceLocation Start = GetStartLoc().Visit(T);
296   SourceLocation End = T.getEndLoc();
297   assert(End.isValid());
298   if (Name.isValid()) {
299     if (Start.isInvalid())
300       Start = Name;
301     if (SM.isBeforeInTranslationUnit(End, Name))
302       End = Name;
303   }
304   if (Initializer.isValid()) {
305     auto InitializerEnd = Initializer.getEnd();
306     assert(SM.isBeforeInTranslationUnit(End, InitializerEnd) ||
307            End == InitializerEnd);
308     End = InitializerEnd;
309   }
310   return SourceRange(Start, End);
311 }
312 
313 namespace {
314 /// All AST hierarchy roots that can be represented as pointers.
315 using ASTPtr = llvm::PointerUnion<Stmt *, Decl *>;
316 /// Maintains a mapping from AST to syntax tree nodes. This class will get more
317 /// complicated as we support more kinds of AST nodes, e.g. TypeLocs.
318 /// FIXME: expose this as public API.
319 class ASTToSyntaxMapping {
320 public:
321   void add(ASTPtr From, syntax::Tree *To) {
322     assert(To != nullptr);
323     assert(!From.isNull());
324 
325     bool Added = Nodes.insert({From, To}).second;
326     (void)Added;
327     assert(Added && "mapping added twice");
328   }
329 
330   void add(NestedNameSpecifierLoc From, syntax::Tree *To) {
331     assert(To != nullptr);
332     assert(From.hasQualifier());
333 
334     bool Added = NNSNodes.insert({From, To}).second;
335     (void)Added;
336     assert(Added && "mapping added twice");
337   }
338 
339   syntax::Tree *find(ASTPtr P) const { return Nodes.lookup(P); }
340 
341   syntax::Tree *find(NestedNameSpecifierLoc P) const {
342     return NNSNodes.lookup(P);
343   }
344 
345 private:
346   llvm::DenseMap<ASTPtr, syntax::Tree *> Nodes;
347   llvm::DenseMap<NestedNameSpecifierLoc, syntax::Tree *> NNSNodes;
348 };
349 } // namespace
350 
351 /// A helper class for constructing the syntax tree while traversing a clang
352 /// AST.
353 ///
354 /// At each point of the traversal we maintain a list of pending nodes.
355 /// Initially all tokens are added as pending nodes. When processing a clang AST
356 /// node, the clients need to:
357 ///   - create a corresponding syntax node,
358 ///   - assign roles to all pending child nodes with 'markChild' and
359 ///     'markChildToken',
360 ///   - replace the child nodes with the new syntax node in the pending list
361 ///     with 'foldNode'.
362 ///
363 /// Note that all children are expected to be processed when building a node.
364 ///
365 /// Call finalize() to finish building the tree and consume the root node.
366 class syntax::TreeBuilder {
367 public:
368   TreeBuilder(syntax::Arena &Arena) : Arena(Arena), Pending(Arena) {
369     for (const auto &T : Arena.tokenBuffer().expandedTokens())
370       LocationToToken.insert({T.location().getRawEncoding(), &T});
371   }
372 
373   llvm::BumpPtrAllocator &allocator() { return Arena.allocator(); }
374   const SourceManager &sourceManager() const { return Arena.sourceManager(); }
375 
376   /// Populate children for \p New node, assuming it covers tokens from \p
377   /// Range.
378   void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree *New, ASTPtr From) {
379     assert(New);
380     Pending.foldChildren(Arena, Range, New);
381     if (From)
382       Mapping.add(From, New);
383   }
384 
385   void foldNode(ArrayRef<syntax::Token> Range, syntax::Tree *New, TypeLoc L) {
386     // FIXME: add mapping for TypeLocs
387     foldNode(Range, New, nullptr);
388   }
389 
390   void foldNode(llvm::ArrayRef<syntax::Token> Range, syntax::Tree *New,
391                 NestedNameSpecifierLoc From) {
392     assert(New);
393     Pending.foldChildren(Arena, Range, New);
394     if (From)
395       Mapping.add(From, New);
396   }
397 
398   /// Notifies that we should not consume trailing semicolon when computing
399   /// token range of \p D.
400   void noticeDeclWithoutSemicolon(Decl *D);
401 
402   /// Mark the \p Child node with a corresponding \p Role. All marked children
403   /// should be consumed by foldNode.
404   /// When called on expressions (clang::Expr is derived from clang::Stmt),
405   /// wraps expressions into expression statement.
406   void markStmtChild(Stmt *Child, NodeRole Role);
407   /// Should be called for expressions in non-statement position to avoid
408   /// wrapping into expression statement.
409   void markExprChild(Expr *Child, NodeRole Role);
410   /// Set role for a token starting at \p Loc.
411   void markChildToken(SourceLocation Loc, NodeRole R);
412   /// Set role for \p T.
413   void markChildToken(const syntax::Token *T, NodeRole R);
414 
415   /// Set role for \p N.
416   void markChild(syntax::Node *N, NodeRole R);
417   /// Set role for the syntax node matching \p N.
418   void markChild(ASTPtr N, NodeRole R);
419   /// Set role for the syntax node matching \p N.
420   void markChild(NestedNameSpecifierLoc N, NodeRole R);
421 
422   /// Finish building the tree and consume the root node.
423   syntax::TranslationUnit *finalize() && {
424     auto Tokens = Arena.tokenBuffer().expandedTokens();
425     assert(!Tokens.empty());
426     assert(Tokens.back().kind() == tok::eof);
427 
428     // Build the root of the tree, consuming all the children.
429     Pending.foldChildren(Arena, Tokens.drop_back(),
430                          new (Arena.allocator()) syntax::TranslationUnit);
431 
432     auto *TU = cast<syntax::TranslationUnit>(std::move(Pending).finalize());
433     TU->assertInvariantsRecursive();
434     return TU;
435   }
436 
437   /// Finds a token starting at \p L. The token must exist if \p L is valid.
438   const syntax::Token *findToken(SourceLocation L) const;
439 
440   /// Finds the syntax tokens corresponding to the \p SourceRange.
441   ArrayRef<syntax::Token> getRange(SourceRange Range) const {
442     assert(Range.isValid());
443     return getRange(Range.getBegin(), Range.getEnd());
444   }
445 
446   /// Finds the syntax tokens corresponding to the passed source locations.
447   /// \p First is the start position of the first token and \p Last is the start
448   /// position of the last token.
449   ArrayRef<syntax::Token> getRange(SourceLocation First,
450                                    SourceLocation Last) const {
451     assert(First.isValid());
452     assert(Last.isValid());
453     assert(First == Last ||
454            Arena.sourceManager().isBeforeInTranslationUnit(First, Last));
455     return llvm::makeArrayRef(findToken(First), std::next(findToken(Last)));
456   }
457 
458   ArrayRef<syntax::Token>
459   getTemplateRange(const ClassTemplateSpecializationDecl *D) const {
460     auto Tokens = getRange(D->getSourceRange());
461     return maybeAppendSemicolon(Tokens, D);
462   }
463 
464   /// Returns true if \p D is the last declarator in a chain and is thus
465   /// reponsible for creating SimpleDeclaration for the whole chain.
466   bool isResponsibleForCreatingDeclaration(const Decl *D) const {
467     assert((isa<DeclaratorDecl, TypedefNameDecl>(D)) &&
468            "only DeclaratorDecl and TypedefNameDecl are supported.");
469 
470     const Decl *Next = D->getNextDeclInContext();
471 
472     // There's no next sibling, this one is responsible.
473     if (Next == nullptr) {
474       return true;
475     }
476 
477     // Next sibling is not the same type, this one is responsible.
478     if (D->getKind() != Next->getKind()) {
479       return true;
480     }
481     // Next sibling doesn't begin at the same loc, it must be a different
482     // declaration, so this declarator is responsible.
483     if (Next->getBeginLoc() != D->getBeginLoc()) {
484       return true;
485     }
486 
487     // NextT is a member of the same declaration, and we need the last member to
488     // create declaration. This one is not responsible.
489     return false;
490   }
491 
492   ArrayRef<syntax::Token> getDeclarationRange(Decl *D) {
493     ArrayRef<syntax::Token> Tokens;
494     // We want to drop the template parameters for specializations.
495     if (const auto *S = dyn_cast<TagDecl>(D))
496       Tokens = getRange(S->TypeDecl::getBeginLoc(), S->getEndLoc());
497     else
498       Tokens = getRange(D->getSourceRange());
499     return maybeAppendSemicolon(Tokens, D);
500   }
501 
502   ArrayRef<syntax::Token> getExprRange(const Expr *E) const {
503     return getRange(E->getSourceRange());
504   }
505 
506   /// Find the adjusted range for the statement, consuming the trailing
507   /// semicolon when needed.
508   ArrayRef<syntax::Token> getStmtRange(const Stmt *S) const {
509     auto Tokens = getRange(S->getSourceRange());
510     if (isa<CompoundStmt>(S))
511       return Tokens;
512 
513     // Some statements miss a trailing semicolon, e.g. 'return', 'continue' and
514     // all statements that end with those. Consume this semicolon here.
515     if (Tokens.back().kind() == tok::semi)
516       return Tokens;
517     return withTrailingSemicolon(Tokens);
518   }
519 
520 private:
521   ArrayRef<syntax::Token> maybeAppendSemicolon(ArrayRef<syntax::Token> Tokens,
522                                                const Decl *D) const {
523     if (isa<NamespaceDecl>(D))
524       return Tokens;
525     if (DeclsWithoutSemicolons.count(D))
526       return Tokens;
527     // FIXME: do not consume trailing semicolon on function definitions.
528     // Most declarations own a semicolon in syntax trees, but not in clang AST.
529     return withTrailingSemicolon(Tokens);
530   }
531 
532   ArrayRef<syntax::Token>
533   withTrailingSemicolon(ArrayRef<syntax::Token> Tokens) const {
534     assert(!Tokens.empty());
535     assert(Tokens.back().kind() != tok::eof);
536     // We never consume 'eof', so looking at the next token is ok.
537     if (Tokens.back().kind() != tok::semi && Tokens.end()->kind() == tok::semi)
538       return llvm::makeArrayRef(Tokens.begin(), Tokens.end() + 1);
539     return Tokens;
540   }
541 
542   void setRole(syntax::Node *N, NodeRole R) {
543     assert(N->role() == NodeRole::Detached);
544     N->setRole(R);
545   }
546 
547   /// A collection of trees covering the input tokens.
548   /// When created, each tree corresponds to a single token in the file.
549   /// Clients call 'foldChildren' to attach one or more subtrees to a parent
550   /// node and update the list of trees accordingly.
551   ///
552   /// Ensures that added nodes properly nest and cover the whole token stream.
553   struct Forest {
554     Forest(syntax::Arena &A) {
555       assert(!A.tokenBuffer().expandedTokens().empty());
556       assert(A.tokenBuffer().expandedTokens().back().kind() == tok::eof);
557       // Create all leaf nodes.
558       // Note that we do not have 'eof' in the tree.
559       for (auto &T : A.tokenBuffer().expandedTokens().drop_back()) {
560         auto *L = new (A.allocator()) syntax::Leaf(&T);
561         L->Original = true;
562         L->CanModify = A.tokenBuffer().spelledForExpanded(T).hasValue();
563         Trees.insert(Trees.end(), {&T, L});
564       }
565     }
566 
567     void assignRole(ArrayRef<syntax::Token> Range, syntax::NodeRole Role) {
568       assert(!Range.empty());
569       auto It = Trees.lower_bound(Range.begin());
570       assert(It != Trees.end() && "no node found");
571       assert(It->first == Range.begin() && "no child with the specified range");
572       assert((std::next(It) == Trees.end() ||
573               std::next(It)->first == Range.end()) &&
574              "no child with the specified range");
575       assert(It->second->role() == NodeRole::Detached &&
576              "re-assigning role for a child");
577       It->second->setRole(Role);
578     }
579 
580     /// Add \p Node to the forest and attach child nodes based on \p Tokens.
581     void foldChildren(const syntax::Arena &A, ArrayRef<syntax::Token> Tokens,
582                       syntax::Tree *Node) {
583       // Attach children to `Node`.
584       assert(Node->firstChild() == nullptr && "node already has children");
585 
586       auto *FirstToken = Tokens.begin();
587       auto BeginChildren = Trees.lower_bound(FirstToken);
588 
589       assert((BeginChildren == Trees.end() ||
590               BeginChildren->first == FirstToken) &&
591              "fold crosses boundaries of existing subtrees");
592       auto EndChildren = Trees.lower_bound(Tokens.end());
593       assert(
594           (EndChildren == Trees.end() || EndChildren->first == Tokens.end()) &&
595           "fold crosses boundaries of existing subtrees");
596 
597       // We need to go in reverse order, because we can only prepend.
598       for (auto It = EndChildren; It != BeginChildren; --It) {
599         auto *C = std::prev(It)->second;
600         if (C->role() == NodeRole::Detached)
601           C->setRole(NodeRole::Unknown);
602         Node->prependChildLowLevel(C);
603       }
604 
605       // Mark that this node came from the AST and is backed by the source code.
606       Node->Original = true;
607       Node->CanModify = A.tokenBuffer().spelledForExpanded(Tokens).hasValue();
608 
609       Trees.erase(BeginChildren, EndChildren);
610       Trees.insert({FirstToken, Node});
611     }
612 
613     // EXPECTS: all tokens were consumed and are owned by a single root node.
614     syntax::Node *finalize() && {
615       assert(Trees.size() == 1);
616       auto *Root = Trees.begin()->second;
617       Trees = {};
618       return Root;
619     }
620 
621     std::string str(const syntax::Arena &A) const {
622       std::string R;
623       for (auto It = Trees.begin(); It != Trees.end(); ++It) {
624         unsigned CoveredTokens =
625             It != Trees.end()
626                 ? (std::next(It)->first - It->first)
627                 : A.tokenBuffer().expandedTokens().end() - It->first;
628 
629         R += std::string(
630             formatv("- '{0}' covers '{1}'+{2} tokens\n", It->second->kind(),
631                     It->first->text(A.sourceManager()), CoveredTokens));
632         R += It->second->dump(A.sourceManager());
633       }
634       return R;
635     }
636 
637   private:
638     /// Maps from the start token to a subtree starting at that token.
639     /// Keys in the map are pointers into the array of expanded tokens, so
640     /// pointer order corresponds to the order of preprocessor tokens.
641     std::map<const syntax::Token *, syntax::Node *> Trees;
642   };
643 
644   /// For debugging purposes.
645   std::string str() { return Pending.str(Arena); }
646 
647   syntax::Arena &Arena;
648   /// To quickly find tokens by their start location.
649   llvm::DenseMap</*SourceLocation*/ unsigned, const syntax::Token *>
650       LocationToToken;
651   Forest Pending;
652   llvm::DenseSet<Decl *> DeclsWithoutSemicolons;
653   ASTToSyntaxMapping Mapping;
654 };
655 
656 namespace {
657 class BuildTreeVisitor : public RecursiveASTVisitor<BuildTreeVisitor> {
658 public:
659   explicit BuildTreeVisitor(ASTContext &Context, syntax::TreeBuilder &Builder)
660       : Builder(Builder), Context(Context) {}
661 
662   bool shouldTraversePostOrder() const { return true; }
663 
664   bool WalkUpFromDeclaratorDecl(DeclaratorDecl *DD) {
665     return processDeclaratorAndDeclaration(DD);
666   }
667 
668   bool WalkUpFromTypedefNameDecl(TypedefNameDecl *TD) {
669     return processDeclaratorAndDeclaration(TD);
670   }
671 
672   bool VisitDecl(Decl *D) {
673     assert(!D->isImplicit());
674     Builder.foldNode(Builder.getDeclarationRange(D),
675                      new (allocator()) syntax::UnknownDeclaration(), D);
676     return true;
677   }
678 
679   // RAV does not call WalkUpFrom* on explicit instantiations, so we have to
680   // override Traverse.
681   // FIXME: make RAV call WalkUpFrom* instead.
682   bool
683   TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *C) {
684     if (!RecursiveASTVisitor::TraverseClassTemplateSpecializationDecl(C))
685       return false;
686     if (C->isExplicitSpecialization())
687       return true; // we are only interested in explicit instantiations.
688     auto *Declaration =
689         cast<syntax::SimpleDeclaration>(handleFreeStandingTagDecl(C));
690     foldExplicitTemplateInstantiation(
691         Builder.getTemplateRange(C), Builder.findToken(C->getExternLoc()),
692         Builder.findToken(C->getTemplateKeywordLoc()), Declaration, C);
693     return true;
694   }
695 
696   bool WalkUpFromTemplateDecl(TemplateDecl *S) {
697     foldTemplateDeclaration(
698         Builder.getDeclarationRange(S),
699         Builder.findToken(S->getTemplateParameters()->getTemplateLoc()),
700         Builder.getDeclarationRange(S->getTemplatedDecl()), S);
701     return true;
702   }
703 
704   bool WalkUpFromTagDecl(TagDecl *C) {
705     // FIXME: build the ClassSpecifier node.
706     if (!C->isFreeStanding()) {
707       assert(C->getNumTemplateParameterLists() == 0);
708       return true;
709     }
710     handleFreeStandingTagDecl(C);
711     return true;
712   }
713 
714   syntax::Declaration *handleFreeStandingTagDecl(TagDecl *C) {
715     assert(C->isFreeStanding());
716     // Class is a declaration specifier and needs a spanning declaration node.
717     auto DeclarationRange = Builder.getDeclarationRange(C);
718     syntax::Declaration *Result = new (allocator()) syntax::SimpleDeclaration;
719     Builder.foldNode(DeclarationRange, Result, nullptr);
720 
721     // Build TemplateDeclaration nodes if we had template parameters.
722     auto ConsumeTemplateParameters = [&](const TemplateParameterList &L) {
723       const auto *TemplateKW = Builder.findToken(L.getTemplateLoc());
724       auto R = llvm::makeArrayRef(TemplateKW, DeclarationRange.end());
725       Result =
726           foldTemplateDeclaration(R, TemplateKW, DeclarationRange, nullptr);
727       DeclarationRange = R;
728     };
729     if (auto *S = dyn_cast<ClassTemplatePartialSpecializationDecl>(C))
730       ConsumeTemplateParameters(*S->getTemplateParameters());
731     for (unsigned I = C->getNumTemplateParameterLists(); 0 < I; --I)
732       ConsumeTemplateParameters(*C->getTemplateParameterList(I - 1));
733     return Result;
734   }
735 
736   bool WalkUpFromTranslationUnitDecl(TranslationUnitDecl *TU) {
737     // We do not want to call VisitDecl(), the declaration for translation
738     // unit is built by finalize().
739     return true;
740   }
741 
742   bool WalkUpFromCompoundStmt(CompoundStmt *S) {
743     using NodeRole = syntax::NodeRole;
744 
745     Builder.markChildToken(S->getLBracLoc(), NodeRole::OpenParen);
746     for (auto *Child : S->body())
747       Builder.markStmtChild(Child, NodeRole::Statement);
748     Builder.markChildToken(S->getRBracLoc(), NodeRole::CloseParen);
749 
750     Builder.foldNode(Builder.getStmtRange(S),
751                      new (allocator()) syntax::CompoundStatement, S);
752     return true;
753   }
754 
755   // Some statements are not yet handled by syntax trees.
756   bool WalkUpFromStmt(Stmt *S) {
757     Builder.foldNode(Builder.getStmtRange(S),
758                      new (allocator()) syntax::UnknownStatement, S);
759     return true;
760   }
761 
762   bool TraverseCXXForRangeStmt(CXXForRangeStmt *S) {
763     // We override to traverse range initializer as VarDecl.
764     // RAV traverses it as a statement, we produce invalid node kinds in that
765     // case.
766     // FIXME: should do this in RAV instead?
767     bool Result = [&, this]() {
768       if (S->getInit() && !TraverseStmt(S->getInit()))
769         return false;
770       if (S->getLoopVariable() && !TraverseDecl(S->getLoopVariable()))
771         return false;
772       if (S->getRangeInit() && !TraverseStmt(S->getRangeInit()))
773         return false;
774       if (S->getBody() && !TraverseStmt(S->getBody()))
775         return false;
776       return true;
777     }();
778     WalkUpFromCXXForRangeStmt(S);
779     return Result;
780   }
781 
782   bool TraverseStmt(Stmt *S) {
783     if (auto *DS = dyn_cast_or_null<DeclStmt>(S)) {
784       // We want to consume the semicolon, make sure SimpleDeclaration does not.
785       for (auto *D : DS->decls())
786         Builder.noticeDeclWithoutSemicolon(D);
787     } else if (auto *E = dyn_cast_or_null<Expr>(S)) {
788       return RecursiveASTVisitor::TraverseStmt(IgnoreImplicit(E));
789     }
790     return RecursiveASTVisitor::TraverseStmt(S);
791   }
792 
793   // Some expressions are not yet handled by syntax trees.
794   bool WalkUpFromExpr(Expr *E) {
795     assert(!isImplicitExpr(E) && "should be handled by TraverseStmt");
796     Builder.foldNode(Builder.getExprRange(E),
797                      new (allocator()) syntax::UnknownExpression, E);
798     return true;
799   }
800 
801   bool TraverseUserDefinedLiteral(UserDefinedLiteral *S) {
802     // The semantic AST node `UserDefinedLiteral` (UDL) may have one child node
803     // referencing the location of the UDL suffix (`_w` in `1.2_w`). The
804     // UDL suffix location does not point to the beginning of a token, so we
805     // can't represent the UDL suffix as a separate syntax tree node.
806 
807     return WalkUpFromUserDefinedLiteral(S);
808   }
809 
810   syntax::UserDefinedLiteralExpression *
811   buildUserDefinedLiteral(UserDefinedLiteral *S) {
812     switch (S->getLiteralOperatorKind()) {
813     case UserDefinedLiteral::LOK_Integer:
814       return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
815     case UserDefinedLiteral::LOK_Floating:
816       return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
817     case UserDefinedLiteral::LOK_Character:
818       return new (allocator()) syntax::CharUserDefinedLiteralExpression;
819     case UserDefinedLiteral::LOK_String:
820       return new (allocator()) syntax::StringUserDefinedLiteralExpression;
821     case UserDefinedLiteral::LOK_Raw:
822     case UserDefinedLiteral::LOK_Template:
823       // For raw literal operator and numeric literal operator template we
824       // cannot get the type of the operand in the semantic AST. We get this
825       // information from the token. As integer and floating point have the same
826       // token kind, we run `NumericLiteralParser` again to distinguish them.
827       auto TokLoc = S->getBeginLoc();
828       auto TokSpelling =
829           Builder.findToken(TokLoc)->text(Context.getSourceManager());
830       auto Literal =
831           NumericLiteralParser(TokSpelling, TokLoc, Context.getSourceManager(),
832                                Context.getLangOpts(), Context.getTargetInfo(),
833                                Context.getDiagnostics());
834       if (Literal.isIntegerLiteral())
835         return new (allocator()) syntax::IntegerUserDefinedLiteralExpression;
836       else {
837         assert(Literal.isFloatingLiteral());
838         return new (allocator()) syntax::FloatUserDefinedLiteralExpression;
839       }
840     }
841     llvm_unreachable("Unknown literal operator kind.");
842   }
843 
844   bool WalkUpFromUserDefinedLiteral(UserDefinedLiteral *S) {
845     Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
846     Builder.foldNode(Builder.getExprRange(S), buildUserDefinedLiteral(S), S);
847     return true;
848   }
849 
850   // FIXME: Fix `NestedNameSpecifierLoc::getLocalSourceRange` for the
851   // `DependentTemplateSpecializationType` case.
852   /// Given a nested-name-specifier return the range for the last name
853   /// specifier.
854   ///
855   /// e.g. `std::T::template X<U>::` => `template X<U>::`
856   SourceRange getLocalSourceRange(const NestedNameSpecifierLoc &NNSLoc) {
857     auto SR = NNSLoc.getLocalSourceRange();
858 
859     // The method `NestedNameSpecifierLoc::getLocalSourceRange` *should*
860     // return the desired `SourceRange`, but there is a corner case. For a
861     // `DependentTemplateSpecializationType` this method returns its
862     // qualifiers as well, in other words in the example above this method
863     // returns `T::template X<U>::` instead of only `template X<U>::`
864     if (auto TL = NNSLoc.getTypeLoc()) {
865       if (auto DependentTL =
866               TL.getAs<DependentTemplateSpecializationTypeLoc>()) {
867         // The 'template' keyword is always present in dependent template
868         // specializations. Except in the case of incorrect code
869         // TODO: Treat the case of incorrect code.
870         SR.setBegin(DependentTL.getTemplateKeywordLoc());
871       }
872     }
873 
874     return SR;
875   }
876 
877   syntax::NodeKind getNameSpecifierKind(const NestedNameSpecifier &NNS) {
878     switch (NNS.getKind()) {
879     case NestedNameSpecifier::Global:
880       return syntax::NodeKind::GlobalNameSpecifier;
881     case NestedNameSpecifier::Namespace:
882     case NestedNameSpecifier::NamespaceAlias:
883     case NestedNameSpecifier::Identifier:
884       return syntax::NodeKind::IdentifierNameSpecifier;
885     case NestedNameSpecifier::TypeSpecWithTemplate:
886       return syntax::NodeKind::SimpleTemplateNameSpecifier;
887     case NestedNameSpecifier::TypeSpec: {
888       const auto *NNSType = NNS.getAsType();
889       assert(NNSType);
890       if (isa<DecltypeType>(NNSType))
891         return syntax::NodeKind::DecltypeNameSpecifier;
892       if (isa<TemplateSpecializationType, DependentTemplateSpecializationType>(
893               NNSType))
894         return syntax::NodeKind::SimpleTemplateNameSpecifier;
895       return syntax::NodeKind::IdentifierNameSpecifier;
896     }
897     default:
898       // FIXME: Support Microsoft's __super
899       llvm::report_fatal_error("We don't yet support the __super specifier",
900                                true);
901     }
902   }
903 
904   syntax::NameSpecifier *
905   buildNameSpecifier(const NestedNameSpecifierLoc &NNSLoc) {
906     assert(NNSLoc.hasQualifier());
907     auto NameSpecifierTokens =
908         Builder.getRange(getLocalSourceRange(NNSLoc)).drop_back();
909     switch (getNameSpecifierKind(*NNSLoc.getNestedNameSpecifier())) {
910     case syntax::NodeKind::GlobalNameSpecifier:
911       return new (allocator()) syntax::GlobalNameSpecifier;
912     case syntax::NodeKind::IdentifierNameSpecifier: {
913       assert(NameSpecifierTokens.size() == 1);
914       Builder.markChildToken(NameSpecifierTokens.begin(),
915                              syntax::NodeRole::Unknown);
916       auto *NS = new (allocator()) syntax::IdentifierNameSpecifier;
917       Builder.foldNode(NameSpecifierTokens, NS, nullptr);
918       return NS;
919     }
920     case syntax::NodeKind::SimpleTemplateNameSpecifier: {
921       // TODO: Build `SimpleTemplateNameSpecifier` children and implement
922       // accessors to them.
923       // Be aware, we cannot do that simply by calling `TraverseTypeLoc`,
924       // some `TypeLoc`s have inside them the previous name specifier and
925       // we want to treat them independently.
926       auto *NS = new (allocator()) syntax::SimpleTemplateNameSpecifier;
927       Builder.foldNode(NameSpecifierTokens, NS, nullptr);
928       return NS;
929     }
930     case syntax::NodeKind::DecltypeNameSpecifier: {
931       const auto TL = NNSLoc.getTypeLoc().castAs<DecltypeTypeLoc>();
932       if (!RecursiveASTVisitor::TraverseDecltypeTypeLoc(TL))
933         return nullptr;
934       auto *NS = new (allocator()) syntax::DecltypeNameSpecifier;
935       // TODO: Implement accessor to `DecltypeNameSpecifier` inner
936       // `DecltypeTypeLoc`.
937       // For that add mapping from `TypeLoc` to `syntax::Node*` then:
938       // Builder.markChild(TypeLoc, syntax::NodeRole);
939       Builder.foldNode(NameSpecifierTokens, NS, nullptr);
940       return NS;
941     }
942     default:
943       llvm_unreachable("getChildKind() does not return this value");
944     }
945   }
946 
947   // To build syntax tree nodes for NestedNameSpecifierLoc we override
948   // Traverse instead of WalkUpFrom because we want to traverse the children
949   // ourselves and build a list instead of a nested tree of name specifier
950   // prefixes.
951   bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc QualifierLoc) {
952     if (!QualifierLoc)
953       return true;
954     for (auto it = QualifierLoc; it; it = it.getPrefix()) {
955       auto *NS = buildNameSpecifier(it);
956       if (!NS)
957         return false;
958       Builder.markChild(NS, syntax::NodeRole::ListElement);
959       Builder.markChildToken(it.getEndLoc(), syntax::NodeRole::ListDelimiter);
960     }
961     Builder.foldNode(Builder.getRange(QualifierLoc.getSourceRange()),
962                      new (allocator()) syntax::NestedNameSpecifier,
963                      QualifierLoc);
964     return true;
965   }
966 
967   syntax::IdExpression *buildIdExpression(NestedNameSpecifierLoc QualifierLoc,
968                                           SourceLocation TemplateKeywordLoc,
969                                           SourceRange UnqualifiedIdLoc,
970                                           ASTPtr From) {
971     if (QualifierLoc) {
972       Builder.markChild(QualifierLoc, syntax::NodeRole::Qualifier);
973       if (TemplateKeywordLoc.isValid())
974         Builder.markChildToken(TemplateKeywordLoc,
975                                syntax::NodeRole::TemplateKeyword);
976     }
977 
978     auto *TheUnqualifiedId = new (allocator()) syntax::UnqualifiedId;
979     Builder.foldNode(Builder.getRange(UnqualifiedIdLoc), TheUnqualifiedId,
980                      nullptr);
981     Builder.markChild(TheUnqualifiedId, syntax::NodeRole::UnqualifiedId);
982 
983     auto IdExpressionBeginLoc =
984         QualifierLoc ? QualifierLoc.getBeginLoc() : UnqualifiedIdLoc.getBegin();
985 
986     auto *TheIdExpression = new (allocator()) syntax::IdExpression;
987     Builder.foldNode(
988         Builder.getRange(IdExpressionBeginLoc, UnqualifiedIdLoc.getEnd()),
989         TheIdExpression, From);
990 
991     return TheIdExpression;
992   }
993 
994   bool WalkUpFromMemberExpr(MemberExpr *S) {
995     // For `MemberExpr` with implicit `this->` we generate a simple
996     // `id-expression` syntax node, beacuse an implicit `member-expression` is
997     // syntactically undistinguishable from an `id-expression`
998     if (S->isImplicitAccess()) {
999       buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1000                         SourceRange(S->getMemberLoc(), S->getEndLoc()), S);
1001       return true;
1002     }
1003 
1004     auto *TheIdExpression = buildIdExpression(
1005         S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1006         SourceRange(S->getMemberLoc(), S->getEndLoc()), nullptr);
1007 
1008     Builder.markChild(TheIdExpression, syntax::NodeRole::Member);
1009 
1010     Builder.markExprChild(S->getBase(), syntax::NodeRole::Object);
1011     Builder.markChildToken(S->getOperatorLoc(), syntax::NodeRole::AccessToken);
1012 
1013     Builder.foldNode(Builder.getExprRange(S),
1014                      new (allocator()) syntax::MemberExpression, S);
1015     return true;
1016   }
1017 
1018   bool WalkUpFromDeclRefExpr(DeclRefExpr *S) {
1019     buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1020                       SourceRange(S->getLocation(), S->getEndLoc()), S);
1021 
1022     return true;
1023   }
1024 
1025   // Same logic as DeclRefExpr.
1026   bool WalkUpFromDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *S) {
1027     buildIdExpression(S->getQualifierLoc(), S->getTemplateKeywordLoc(),
1028                       SourceRange(S->getLocation(), S->getEndLoc()), S);
1029 
1030     return true;
1031   }
1032 
1033   bool WalkUpFromCXXThisExpr(CXXThisExpr *S) {
1034     if (!S->isImplicit()) {
1035       Builder.markChildToken(S->getLocation(),
1036                              syntax::NodeRole::IntroducerKeyword);
1037       Builder.foldNode(Builder.getExprRange(S),
1038                        new (allocator()) syntax::ThisExpression, S);
1039     }
1040     return true;
1041   }
1042 
1043   bool WalkUpFromParenExpr(ParenExpr *S) {
1044     Builder.markChildToken(S->getLParen(), syntax::NodeRole::OpenParen);
1045     Builder.markExprChild(S->getSubExpr(), syntax::NodeRole::SubExpression);
1046     Builder.markChildToken(S->getRParen(), syntax::NodeRole::CloseParen);
1047     Builder.foldNode(Builder.getExprRange(S),
1048                      new (allocator()) syntax::ParenExpression, S);
1049     return true;
1050   }
1051 
1052   bool WalkUpFromIntegerLiteral(IntegerLiteral *S) {
1053     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1054     Builder.foldNode(Builder.getExprRange(S),
1055                      new (allocator()) syntax::IntegerLiteralExpression, S);
1056     return true;
1057   }
1058 
1059   bool WalkUpFromCharacterLiteral(CharacterLiteral *S) {
1060     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1061     Builder.foldNode(Builder.getExprRange(S),
1062                      new (allocator()) syntax::CharacterLiteralExpression, S);
1063     return true;
1064   }
1065 
1066   bool WalkUpFromFloatingLiteral(FloatingLiteral *S) {
1067     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1068     Builder.foldNode(Builder.getExprRange(S),
1069                      new (allocator()) syntax::FloatingLiteralExpression, S);
1070     return true;
1071   }
1072 
1073   bool WalkUpFromStringLiteral(StringLiteral *S) {
1074     Builder.markChildToken(S->getBeginLoc(), syntax::NodeRole::LiteralToken);
1075     Builder.foldNode(Builder.getExprRange(S),
1076                      new (allocator()) syntax::StringLiteralExpression, S);
1077     return true;
1078   }
1079 
1080   bool WalkUpFromCXXBoolLiteralExpr(CXXBoolLiteralExpr *S) {
1081     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1082     Builder.foldNode(Builder.getExprRange(S),
1083                      new (allocator()) syntax::BoolLiteralExpression, S);
1084     return true;
1085   }
1086 
1087   bool WalkUpFromCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) {
1088     Builder.markChildToken(S->getLocation(), syntax::NodeRole::LiteralToken);
1089     Builder.foldNode(Builder.getExprRange(S),
1090                      new (allocator()) syntax::CxxNullPtrExpression, S);
1091     return true;
1092   }
1093 
1094   bool WalkUpFromUnaryOperator(UnaryOperator *S) {
1095     Builder.markChildToken(S->getOperatorLoc(),
1096                            syntax::NodeRole::OperatorToken);
1097     Builder.markExprChild(S->getSubExpr(), syntax::NodeRole::Operand);
1098 
1099     if (S->isPostfix())
1100       Builder.foldNode(Builder.getExprRange(S),
1101                        new (allocator()) syntax::PostfixUnaryOperatorExpression,
1102                        S);
1103     else
1104       Builder.foldNode(Builder.getExprRange(S),
1105                        new (allocator()) syntax::PrefixUnaryOperatorExpression,
1106                        S);
1107 
1108     return true;
1109   }
1110 
1111   bool WalkUpFromBinaryOperator(BinaryOperator *S) {
1112     Builder.markExprChild(S->getLHS(), syntax::NodeRole::LeftHandSide);
1113     Builder.markChildToken(S->getOperatorLoc(),
1114                            syntax::NodeRole::OperatorToken);
1115     Builder.markExprChild(S->getRHS(), syntax::NodeRole::RightHandSide);
1116     Builder.foldNode(Builder.getExprRange(S),
1117                      new (allocator()) syntax::BinaryOperatorExpression, S);
1118     return true;
1119   }
1120 
1121   /// Builds `CallArguments` syntax node from arguments that appear in source
1122   /// code, i.e. not default arguments.
1123   syntax::CallArguments *
1124   buildCallArguments(CallExpr::arg_range ArgsAndDefaultArgs) {
1125     auto Args = dropDefaultArgs(ArgsAndDefaultArgs);
1126     for (const auto &Arg : Args) {
1127       Builder.markExprChild(Arg, syntax::NodeRole::ListElement);
1128       const auto *DelimiterToken =
1129           std::next(Builder.findToken(Arg->getEndLoc()));
1130       if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1131         Builder.markChildToken(DelimiterToken, syntax::NodeRole::ListDelimiter);
1132     }
1133 
1134     auto *Arguments = new (allocator()) syntax::CallArguments;
1135     if (!Args.empty())
1136       Builder.foldNode(Builder.getRange((*Args.begin())->getBeginLoc(),
1137                                         (*(Args.end() - 1))->getEndLoc()),
1138                        Arguments, nullptr);
1139 
1140     return Arguments;
1141   }
1142 
1143   bool WalkUpFromCallExpr(CallExpr *S) {
1144     Builder.markExprChild(S->getCallee(), syntax::NodeRole::Callee);
1145 
1146     const auto *LParenToken =
1147         std::next(Builder.findToken(S->getCallee()->getEndLoc()));
1148     // FIXME: Assert that `LParenToken` is indeed a `l_paren` once we have fixed
1149     // the test on decltype desctructors.
1150     if (LParenToken->kind() == clang::tok::l_paren)
1151       Builder.markChildToken(LParenToken, syntax::NodeRole::OpenParen);
1152 
1153     Builder.markChild(buildCallArguments(S->arguments()),
1154                       syntax::NodeRole::Arguments);
1155 
1156     Builder.markChildToken(S->getRParenLoc(), syntax::NodeRole::CloseParen);
1157 
1158     Builder.foldNode(Builder.getRange(S->getSourceRange()),
1159                      new (allocator()) syntax::CallExpression, S);
1160     return true;
1161   }
1162 
1163   bool WalkUpFromCXXConstructExpr(CXXConstructExpr *S) {
1164     // Ignore the implicit calls to default constructors.
1165     if ((S->getNumArgs() == 0 || isa<CXXDefaultArgExpr>(S->getArg(0))) &&
1166         S->getParenOrBraceRange().isInvalid())
1167       return true;
1168     return RecursiveASTVisitor::WalkUpFromCXXConstructExpr(S);
1169   }
1170 
1171   bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
1172     // To construct a syntax tree of the same shape for calls to built-in and
1173     // user-defined operators, ignore the `DeclRefExpr` that refers to the
1174     // operator and treat it as a simple token. Do that by traversing
1175     // arguments instead of children.
1176     for (auto *child : S->arguments()) {
1177       // A postfix unary operator is declared as taking two operands. The
1178       // second operand is used to distinguish from its prefix counterpart. In
1179       // the semantic AST this "phantom" operand is represented as a
1180       // `IntegerLiteral` with invalid `SourceLocation`. We skip visiting this
1181       // operand because it does not correspond to anything written in source
1182       // code.
1183       if (child->getSourceRange().isInvalid()) {
1184         assert(getOperatorNodeKind(*S) ==
1185                syntax::NodeKind::PostfixUnaryOperatorExpression);
1186         continue;
1187       }
1188       if (!TraverseStmt(child))
1189         return false;
1190     }
1191     return WalkUpFromCXXOperatorCallExpr(S);
1192   }
1193 
1194   bool WalkUpFromCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
1195     switch (getOperatorNodeKind(*S)) {
1196     case syntax::NodeKind::BinaryOperatorExpression:
1197       Builder.markExprChild(S->getArg(0), syntax::NodeRole::LeftHandSide);
1198       Builder.markChildToken(S->getOperatorLoc(),
1199                              syntax::NodeRole::OperatorToken);
1200       Builder.markExprChild(S->getArg(1), syntax::NodeRole::RightHandSide);
1201       Builder.foldNode(Builder.getExprRange(S),
1202                        new (allocator()) syntax::BinaryOperatorExpression, S);
1203       return true;
1204     case syntax::NodeKind::PrefixUnaryOperatorExpression:
1205       Builder.markChildToken(S->getOperatorLoc(),
1206                              syntax::NodeRole::OperatorToken);
1207       Builder.markExprChild(S->getArg(0), syntax::NodeRole::Operand);
1208       Builder.foldNode(Builder.getExprRange(S),
1209                        new (allocator()) syntax::PrefixUnaryOperatorExpression,
1210                        S);
1211       return true;
1212     case syntax::NodeKind::PostfixUnaryOperatorExpression:
1213       Builder.markChildToken(S->getOperatorLoc(),
1214                              syntax::NodeRole::OperatorToken);
1215       Builder.markExprChild(S->getArg(0), syntax::NodeRole::Operand);
1216       Builder.foldNode(Builder.getExprRange(S),
1217                        new (allocator()) syntax::PostfixUnaryOperatorExpression,
1218                        S);
1219       return true;
1220     case syntax::NodeKind::CallExpression: {
1221       Builder.markExprChild(S->getArg(0), syntax::NodeRole::Callee);
1222 
1223       const auto *LParenToken =
1224           std::next(Builder.findToken(S->getArg(0)->getEndLoc()));
1225       // FIXME: Assert that `LParenToken` is indeed a `l_paren` once we have
1226       // fixed the test on decltype desctructors.
1227       if (LParenToken->kind() == clang::tok::l_paren)
1228         Builder.markChildToken(LParenToken, syntax::NodeRole::OpenParen);
1229 
1230       Builder.markChild(buildCallArguments(CallExpr::arg_range(
1231                             S->arg_begin() + 1, S->arg_end())),
1232                         syntax::NodeRole::Arguments);
1233 
1234       Builder.markChildToken(S->getRParenLoc(), syntax::NodeRole::CloseParen);
1235 
1236       Builder.foldNode(Builder.getRange(S->getSourceRange()),
1237                        new (allocator()) syntax::CallExpression, S);
1238       return true;
1239     }
1240     case syntax::NodeKind::UnknownExpression:
1241       return WalkUpFromExpr(S);
1242     default:
1243       llvm_unreachable("getOperatorNodeKind() does not return this value");
1244     }
1245   }
1246 
1247   bool WalkUpFromCXXDefaultArgExpr(CXXDefaultArgExpr *S) { return true; }
1248 
1249   bool WalkUpFromNamespaceDecl(NamespaceDecl *S) {
1250     auto Tokens = Builder.getDeclarationRange(S);
1251     if (Tokens.front().kind() == tok::coloncolon) {
1252       // Handle nested namespace definitions. Those start at '::' token, e.g.
1253       // namespace a^::b {}
1254       // FIXME: build corresponding nodes for the name of this namespace.
1255       return true;
1256     }
1257     Builder.foldNode(Tokens, new (allocator()) syntax::NamespaceDefinition, S);
1258     return true;
1259   }
1260 
1261   // FIXME: Deleting the `TraverseParenTypeLoc` override doesn't change test
1262   // results. Find test coverage or remove it.
1263   bool TraverseParenTypeLoc(ParenTypeLoc L) {
1264     // We reverse order of traversal to get the proper syntax structure.
1265     if (!WalkUpFromParenTypeLoc(L))
1266       return false;
1267     return TraverseTypeLoc(L.getInnerLoc());
1268   }
1269 
1270   bool WalkUpFromParenTypeLoc(ParenTypeLoc L) {
1271     Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen);
1272     Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen);
1273     Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getRParenLoc()),
1274                      new (allocator()) syntax::ParenDeclarator, L);
1275     return true;
1276   }
1277 
1278   // Declarator chunks, they are produced by type locs and some clang::Decls.
1279   bool WalkUpFromArrayTypeLoc(ArrayTypeLoc L) {
1280     Builder.markChildToken(L.getLBracketLoc(), syntax::NodeRole::OpenParen);
1281     Builder.markExprChild(L.getSizeExpr(), syntax::NodeRole::Size);
1282     Builder.markChildToken(L.getRBracketLoc(), syntax::NodeRole::CloseParen);
1283     Builder.foldNode(Builder.getRange(L.getLBracketLoc(), L.getRBracketLoc()),
1284                      new (allocator()) syntax::ArraySubscript, L);
1285     return true;
1286   }
1287 
1288   syntax::ParameterDeclarationList *
1289   buildParameterDeclarationList(ArrayRef<ParmVarDecl *> Params) {
1290     for (auto *P : Params) {
1291       Builder.markChild(P, syntax::NodeRole::ListElement);
1292       const auto *DelimiterToken = std::next(Builder.findToken(P->getEndLoc()));
1293       if (DelimiterToken->kind() == clang::tok::TokenKind::comma)
1294         Builder.markChildToken(DelimiterToken, syntax::NodeRole::ListDelimiter);
1295     }
1296     auto *Parameters = new (allocator()) syntax::ParameterDeclarationList;
1297     if (!Params.empty())
1298       Builder.foldNode(Builder.getRange(Params.front()->getBeginLoc(),
1299                                         Params.back()->getEndLoc()),
1300                        Parameters, nullptr);
1301     return Parameters;
1302   }
1303 
1304   bool WalkUpFromFunctionTypeLoc(FunctionTypeLoc L) {
1305     Builder.markChildToken(L.getLParenLoc(), syntax::NodeRole::OpenParen);
1306 
1307     Builder.markChild(buildParameterDeclarationList(L.getParams()),
1308                       syntax::NodeRole::Parameters);
1309 
1310     Builder.markChildToken(L.getRParenLoc(), syntax::NodeRole::CloseParen);
1311     Builder.foldNode(Builder.getRange(L.getLParenLoc(), L.getEndLoc()),
1312                      new (allocator()) syntax::ParametersAndQualifiers, L);
1313     return true;
1314   }
1315 
1316   bool WalkUpFromFunctionProtoTypeLoc(FunctionProtoTypeLoc L) {
1317     if (!L.getTypePtr()->hasTrailingReturn())
1318       return WalkUpFromFunctionTypeLoc(L);
1319 
1320     auto *TrailingReturnTokens = buildTrailingReturn(L);
1321     // Finish building the node for parameters.
1322     Builder.markChild(TrailingReturnTokens, syntax::NodeRole::TrailingReturn);
1323     return WalkUpFromFunctionTypeLoc(L);
1324   }
1325 
1326   bool TraverseMemberPointerTypeLoc(MemberPointerTypeLoc L) {
1327     // In the source code "void (Y::*mp)()" `MemberPointerTypeLoc` corresponds
1328     // to "Y::*" but it points to a `ParenTypeLoc` that corresponds to
1329     // "(Y::*mp)" We thus reverse the order of traversal to get the proper
1330     // syntax structure.
1331     if (!WalkUpFromMemberPointerTypeLoc(L))
1332       return false;
1333     return TraverseTypeLoc(L.getPointeeLoc());
1334   }
1335 
1336   bool WalkUpFromMemberPointerTypeLoc(MemberPointerTypeLoc L) {
1337     auto SR = L.getLocalSourceRange();
1338     Builder.foldNode(Builder.getRange(SR),
1339                      new (allocator()) syntax::MemberPointer, L);
1340     return true;
1341   }
1342 
1343   // The code below is very regular, it could even be generated with some
1344   // preprocessor magic. We merely assign roles to the corresponding children
1345   // and fold resulting nodes.
1346   bool WalkUpFromDeclStmt(DeclStmt *S) {
1347     Builder.foldNode(Builder.getStmtRange(S),
1348                      new (allocator()) syntax::DeclarationStatement, S);
1349     return true;
1350   }
1351 
1352   bool WalkUpFromNullStmt(NullStmt *S) {
1353     Builder.foldNode(Builder.getStmtRange(S),
1354                      new (allocator()) syntax::EmptyStatement, S);
1355     return true;
1356   }
1357 
1358   bool WalkUpFromSwitchStmt(SwitchStmt *S) {
1359     Builder.markChildToken(S->getSwitchLoc(),
1360                            syntax::NodeRole::IntroducerKeyword);
1361     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1362     Builder.foldNode(Builder.getStmtRange(S),
1363                      new (allocator()) syntax::SwitchStatement, S);
1364     return true;
1365   }
1366 
1367   bool WalkUpFromCaseStmt(CaseStmt *S) {
1368     Builder.markChildToken(S->getKeywordLoc(),
1369                            syntax::NodeRole::IntroducerKeyword);
1370     Builder.markExprChild(S->getLHS(), syntax::NodeRole::CaseValue);
1371     Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1372     Builder.foldNode(Builder.getStmtRange(S),
1373                      new (allocator()) syntax::CaseStatement, S);
1374     return true;
1375   }
1376 
1377   bool WalkUpFromDefaultStmt(DefaultStmt *S) {
1378     Builder.markChildToken(S->getKeywordLoc(),
1379                            syntax::NodeRole::IntroducerKeyword);
1380     Builder.markStmtChild(S->getSubStmt(), syntax::NodeRole::BodyStatement);
1381     Builder.foldNode(Builder.getStmtRange(S),
1382                      new (allocator()) syntax::DefaultStatement, S);
1383     return true;
1384   }
1385 
1386   bool WalkUpFromIfStmt(IfStmt *S) {
1387     Builder.markChildToken(S->getIfLoc(), syntax::NodeRole::IntroducerKeyword);
1388     Builder.markStmtChild(S->getThen(), syntax::NodeRole::ThenStatement);
1389     Builder.markChildToken(S->getElseLoc(), syntax::NodeRole::ElseKeyword);
1390     Builder.markStmtChild(S->getElse(), syntax::NodeRole::ElseStatement);
1391     Builder.foldNode(Builder.getStmtRange(S),
1392                      new (allocator()) syntax::IfStatement, S);
1393     return true;
1394   }
1395 
1396   bool WalkUpFromForStmt(ForStmt *S) {
1397     Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1398     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1399     Builder.foldNode(Builder.getStmtRange(S),
1400                      new (allocator()) syntax::ForStatement, S);
1401     return true;
1402   }
1403 
1404   bool WalkUpFromWhileStmt(WhileStmt *S) {
1405     Builder.markChildToken(S->getWhileLoc(),
1406                            syntax::NodeRole::IntroducerKeyword);
1407     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1408     Builder.foldNode(Builder.getStmtRange(S),
1409                      new (allocator()) syntax::WhileStatement, S);
1410     return true;
1411   }
1412 
1413   bool WalkUpFromContinueStmt(ContinueStmt *S) {
1414     Builder.markChildToken(S->getContinueLoc(),
1415                            syntax::NodeRole::IntroducerKeyword);
1416     Builder.foldNode(Builder.getStmtRange(S),
1417                      new (allocator()) syntax::ContinueStatement, S);
1418     return true;
1419   }
1420 
1421   bool WalkUpFromBreakStmt(BreakStmt *S) {
1422     Builder.markChildToken(S->getBreakLoc(),
1423                            syntax::NodeRole::IntroducerKeyword);
1424     Builder.foldNode(Builder.getStmtRange(S),
1425                      new (allocator()) syntax::BreakStatement, S);
1426     return true;
1427   }
1428 
1429   bool WalkUpFromReturnStmt(ReturnStmt *S) {
1430     Builder.markChildToken(S->getReturnLoc(),
1431                            syntax::NodeRole::IntroducerKeyword);
1432     Builder.markExprChild(S->getRetValue(), syntax::NodeRole::ReturnValue);
1433     Builder.foldNode(Builder.getStmtRange(S),
1434                      new (allocator()) syntax::ReturnStatement, S);
1435     return true;
1436   }
1437 
1438   bool WalkUpFromCXXForRangeStmt(CXXForRangeStmt *S) {
1439     Builder.markChildToken(S->getForLoc(), syntax::NodeRole::IntroducerKeyword);
1440     Builder.markStmtChild(S->getBody(), syntax::NodeRole::BodyStatement);
1441     Builder.foldNode(Builder.getStmtRange(S),
1442                      new (allocator()) syntax::RangeBasedForStatement, S);
1443     return true;
1444   }
1445 
1446   bool WalkUpFromEmptyDecl(EmptyDecl *S) {
1447     Builder.foldNode(Builder.getDeclarationRange(S),
1448                      new (allocator()) syntax::EmptyDeclaration, S);
1449     return true;
1450   }
1451 
1452   bool WalkUpFromStaticAssertDecl(StaticAssertDecl *S) {
1453     Builder.markExprChild(S->getAssertExpr(), syntax::NodeRole::Condition);
1454     Builder.markExprChild(S->getMessage(), syntax::NodeRole::Message);
1455     Builder.foldNode(Builder.getDeclarationRange(S),
1456                      new (allocator()) syntax::StaticAssertDeclaration, S);
1457     return true;
1458   }
1459 
1460   bool WalkUpFromLinkageSpecDecl(LinkageSpecDecl *S) {
1461     Builder.foldNode(Builder.getDeclarationRange(S),
1462                      new (allocator()) syntax::LinkageSpecificationDeclaration,
1463                      S);
1464     return true;
1465   }
1466 
1467   bool WalkUpFromNamespaceAliasDecl(NamespaceAliasDecl *S) {
1468     Builder.foldNode(Builder.getDeclarationRange(S),
1469                      new (allocator()) syntax::NamespaceAliasDefinition, S);
1470     return true;
1471   }
1472 
1473   bool WalkUpFromUsingDirectiveDecl(UsingDirectiveDecl *S) {
1474     Builder.foldNode(Builder.getDeclarationRange(S),
1475                      new (allocator()) syntax::UsingNamespaceDirective, S);
1476     return true;
1477   }
1478 
1479   bool WalkUpFromUsingDecl(UsingDecl *S) {
1480     Builder.foldNode(Builder.getDeclarationRange(S),
1481                      new (allocator()) syntax::UsingDeclaration, S);
1482     return true;
1483   }
1484 
1485   bool WalkUpFromUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *S) {
1486     Builder.foldNode(Builder.getDeclarationRange(S),
1487                      new (allocator()) syntax::UsingDeclaration, S);
1488     return true;
1489   }
1490 
1491   bool WalkUpFromUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *S) {
1492     Builder.foldNode(Builder.getDeclarationRange(S),
1493                      new (allocator()) syntax::UsingDeclaration, S);
1494     return true;
1495   }
1496 
1497   bool WalkUpFromTypeAliasDecl(TypeAliasDecl *S) {
1498     Builder.foldNode(Builder.getDeclarationRange(S),
1499                      new (allocator()) syntax::TypeAliasDeclaration, S);
1500     return true;
1501   }
1502 
1503 private:
1504   /// Folds SimpleDeclarator node (if present) and in case this is the last
1505   /// declarator in the chain it also folds SimpleDeclaration node.
1506   template <class T> bool processDeclaratorAndDeclaration(T *D) {
1507     auto Range = getDeclaratorRange(
1508         Builder.sourceManager(), D->getTypeSourceInfo()->getTypeLoc(),
1509         getQualifiedNameStart(D), getInitializerRange(D));
1510 
1511     // There doesn't have to be a declarator (e.g. `void foo(int)` only has
1512     // declaration, but no declarator).
1513     if (Range.getBegin().isValid()) {
1514       auto *N = new (allocator()) syntax::SimpleDeclarator;
1515       Builder.foldNode(Builder.getRange(Range), N, nullptr);
1516       Builder.markChild(N, syntax::NodeRole::Declarator);
1517     }
1518 
1519     if (Builder.isResponsibleForCreatingDeclaration(D)) {
1520       Builder.foldNode(Builder.getDeclarationRange(D),
1521                        new (allocator()) syntax::SimpleDeclaration, D);
1522     }
1523     return true;
1524   }
1525 
1526   /// Returns the range of the built node.
1527   syntax::TrailingReturnType *buildTrailingReturn(FunctionProtoTypeLoc L) {
1528     assert(L.getTypePtr()->hasTrailingReturn());
1529 
1530     auto ReturnedType = L.getReturnLoc();
1531     // Build node for the declarator, if any.
1532     auto ReturnDeclaratorRange = SourceRange(GetStartLoc().Visit(ReturnedType),
1533                                              ReturnedType.getEndLoc());
1534     syntax::SimpleDeclarator *ReturnDeclarator = nullptr;
1535     if (ReturnDeclaratorRange.isValid()) {
1536       ReturnDeclarator = new (allocator()) syntax::SimpleDeclarator;
1537       Builder.foldNode(Builder.getRange(ReturnDeclaratorRange),
1538                        ReturnDeclarator, nullptr);
1539     }
1540 
1541     // Build node for trailing return type.
1542     auto Return = Builder.getRange(ReturnedType.getSourceRange());
1543     const auto *Arrow = Return.begin() - 1;
1544     assert(Arrow->kind() == tok::arrow);
1545     auto Tokens = llvm::makeArrayRef(Arrow, Return.end());
1546     Builder.markChildToken(Arrow, syntax::NodeRole::ArrowToken);
1547     if (ReturnDeclarator)
1548       Builder.markChild(ReturnDeclarator, syntax::NodeRole::Declarator);
1549     auto *R = new (allocator()) syntax::TrailingReturnType;
1550     Builder.foldNode(Tokens, R, L);
1551     return R;
1552   }
1553 
1554   void foldExplicitTemplateInstantiation(
1555       ArrayRef<syntax::Token> Range, const syntax::Token *ExternKW,
1556       const syntax::Token *TemplateKW,
1557       syntax::SimpleDeclaration *InnerDeclaration, Decl *From) {
1558     assert(!ExternKW || ExternKW->kind() == tok::kw_extern);
1559     assert(TemplateKW && TemplateKW->kind() == tok::kw_template);
1560     Builder.markChildToken(ExternKW, syntax::NodeRole::ExternKeyword);
1561     Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1562     Builder.markChild(InnerDeclaration, syntax::NodeRole::Declaration);
1563     Builder.foldNode(
1564         Range, new (allocator()) syntax::ExplicitTemplateInstantiation, From);
1565   }
1566 
1567   syntax::TemplateDeclaration *foldTemplateDeclaration(
1568       ArrayRef<syntax::Token> Range, const syntax::Token *TemplateKW,
1569       ArrayRef<syntax::Token> TemplatedDeclaration, Decl *From) {
1570     assert(TemplateKW && TemplateKW->kind() == tok::kw_template);
1571     Builder.markChildToken(TemplateKW, syntax::NodeRole::IntroducerKeyword);
1572 
1573     auto *N = new (allocator()) syntax::TemplateDeclaration;
1574     Builder.foldNode(Range, N, From);
1575     Builder.markChild(N, syntax::NodeRole::Declaration);
1576     return N;
1577   }
1578 
1579   /// A small helper to save some typing.
1580   llvm::BumpPtrAllocator &allocator() { return Builder.allocator(); }
1581 
1582   syntax::TreeBuilder &Builder;
1583   const ASTContext &Context;
1584 };
1585 } // namespace
1586 
1587 void syntax::TreeBuilder::noticeDeclWithoutSemicolon(Decl *D) {
1588   DeclsWithoutSemicolons.insert(D);
1589 }
1590 
1591 void syntax::TreeBuilder::markChildToken(SourceLocation Loc, NodeRole Role) {
1592   if (Loc.isInvalid())
1593     return;
1594   Pending.assignRole(*findToken(Loc), Role);
1595 }
1596 
1597 void syntax::TreeBuilder::markChildToken(const syntax::Token *T, NodeRole R) {
1598   if (!T)
1599     return;
1600   Pending.assignRole(*T, R);
1601 }
1602 
1603 void syntax::TreeBuilder::markChild(syntax::Node *N, NodeRole R) {
1604   assert(N);
1605   setRole(N, R);
1606 }
1607 
1608 void syntax::TreeBuilder::markChild(ASTPtr N, NodeRole R) {
1609   auto *SN = Mapping.find(N);
1610   assert(SN != nullptr);
1611   setRole(SN, R);
1612 }
1613 void syntax::TreeBuilder::markChild(NestedNameSpecifierLoc NNSLoc, NodeRole R) {
1614   auto *SN = Mapping.find(NNSLoc);
1615   assert(SN != nullptr);
1616   setRole(SN, R);
1617 }
1618 
1619 void syntax::TreeBuilder::markStmtChild(Stmt *Child, NodeRole Role) {
1620   if (!Child)
1621     return;
1622 
1623   syntax::Tree *ChildNode;
1624   if (Expr *ChildExpr = dyn_cast<Expr>(Child)) {
1625     // This is an expression in a statement position, consume the trailing
1626     // semicolon and form an 'ExpressionStatement' node.
1627     markExprChild(ChildExpr, NodeRole::Expression);
1628     ChildNode = new (allocator()) syntax::ExpressionStatement;
1629     // (!) 'getStmtRange()' ensures this covers a trailing semicolon.
1630     Pending.foldChildren(Arena, getStmtRange(Child), ChildNode);
1631   } else {
1632     ChildNode = Mapping.find(Child);
1633   }
1634   assert(ChildNode != nullptr);
1635   setRole(ChildNode, Role);
1636 }
1637 
1638 void syntax::TreeBuilder::markExprChild(Expr *Child, NodeRole Role) {
1639   if (!Child)
1640     return;
1641   Child = IgnoreImplicit(Child);
1642 
1643   syntax::Tree *ChildNode = Mapping.find(Child);
1644   assert(ChildNode != nullptr);
1645   setRole(ChildNode, Role);
1646 }
1647 
1648 const syntax::Token *syntax::TreeBuilder::findToken(SourceLocation L) const {
1649   if (L.isInvalid())
1650     return nullptr;
1651   auto It = LocationToToken.find(L.getRawEncoding());
1652   assert(It != LocationToToken.end());
1653   return It->second;
1654 }
1655 
1656 syntax::TranslationUnit *
1657 syntax::buildSyntaxTree(Arena &A, const TranslationUnitDecl &TU) {
1658   TreeBuilder Builder(A);
1659   BuildTreeVisitor(TU.getASTContext(), Builder).TraverseAST(TU.getASTContext());
1660   return std::move(Builder).finalize();
1661 }
1662