1 //===--- RecursiveASTVisitor.h - Recursive AST Visitor ----------*- 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 // 9 // This file defines the RecursiveASTVisitor interface, which recursively 10 // traverses the entire AST. 11 // 12 //===----------------------------------------------------------------------===// 13 #ifndef LLVM_CLANG_AST_RECURSIVEASTVISITOR_H 14 #define LLVM_CLANG_AST_RECURSIVEASTVISITOR_H 15 16 #include "clang/AST/Attr.h" 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclarationName.h" 19 #include "clang/AST/DeclBase.h" 20 #include "clang/AST/DeclCXX.h" 21 #include "clang/AST/DeclFriend.h" 22 #include "clang/AST/DeclObjC.h" 23 #include "clang/AST/DeclOpenMP.h" 24 #include "clang/AST/DeclTemplate.h" 25 #include "clang/AST/Expr.h" 26 #include "clang/AST/ExprConcepts.h" 27 #include "clang/AST/ExprCXX.h" 28 #include "clang/AST/ExprObjC.h" 29 #include "clang/AST/ExprOpenMP.h" 30 #include "clang/AST/LambdaCapture.h" 31 #include "clang/AST/NestedNameSpecifier.h" 32 #include "clang/AST/OpenMPClause.h" 33 #include "clang/AST/Stmt.h" 34 #include "clang/AST/StmtCXX.h" 35 #include "clang/AST/StmtObjC.h" 36 #include "clang/AST/StmtOpenMP.h" 37 #include "clang/AST/TemplateBase.h" 38 #include "clang/AST/TemplateName.h" 39 #include "clang/AST/Type.h" 40 #include "clang/AST/TypeLoc.h" 41 #include "clang/Basic/LLVM.h" 42 #include "clang/Basic/OpenMPKinds.h" 43 #include "clang/Basic/Specifiers.h" 44 #include "llvm/ADT/PointerIntPair.h" 45 #include "llvm/ADT/SmallVector.h" 46 #include "llvm/Support/Casting.h" 47 #include <algorithm> 48 #include <cstddef> 49 #include <type_traits> 50 51 namespace clang { 52 53 // A helper macro to implement short-circuiting when recursing. It 54 // invokes CALL_EXPR, which must be a method call, on the derived 55 // object (s.t. a user of RecursiveASTVisitor can override the method 56 // in CALL_EXPR). 57 #define TRY_TO(CALL_EXPR) \ 58 do { \ 59 if (!getDerived().CALL_EXPR) \ 60 return false; \ 61 } while (false) 62 63 namespace detail { 64 65 template <typename T, typename U> 66 struct has_same_member_pointer_type : std::false_type {}; 67 template <typename T, typename U, typename R, typename... P> 68 struct has_same_member_pointer_type<R (T::*)(P...), R (U::*)(P...)> 69 : std::true_type {}; 70 71 template <bool has_same_type> struct is_same_method_impl { 72 template <typename FirstMethodPtrTy, typename SecondMethodPtrTy> 73 static bool isSameMethod(FirstMethodPtrTy FirstMethodPtr, 74 SecondMethodPtrTy SecondMethodPtr) { 75 return false; 76 } 77 }; 78 79 template <> struct is_same_method_impl<true> { 80 template <typename FirstMethodPtrTy, typename SecondMethodPtrTy> 81 static bool isSameMethod(FirstMethodPtrTy FirstMethodPtr, 82 SecondMethodPtrTy SecondMethodPtr) { 83 return FirstMethodPtr == SecondMethodPtr; 84 } 85 }; 86 87 /// Returns true if and only if \p FirstMethodPtr and \p SecondMethodPtr 88 /// are pointers to the same non-static member function. 89 template <typename FirstMethodPtrTy, typename SecondMethodPtrTy> 90 bool isSameMethod(FirstMethodPtrTy FirstMethodPtr, 91 SecondMethodPtrTy SecondMethodPtr) { 92 return is_same_method_impl<has_same_member_pointer_type< 93 FirstMethodPtrTy, 94 SecondMethodPtrTy>::value>::isSameMethod(FirstMethodPtr, SecondMethodPtr); 95 } 96 97 } // end namespace detail 98 99 /// A class that does preorder or postorder 100 /// depth-first traversal on the entire Clang AST and visits each node. 101 /// 102 /// This class performs three distinct tasks: 103 /// 1. traverse the AST (i.e. go to each node); 104 /// 2. at a given node, walk up the class hierarchy, starting from 105 /// the node's dynamic type, until the top-most class (e.g. Stmt, 106 /// Decl, or Type) is reached. 107 /// 3. given a (node, class) combination, where 'class' is some base 108 /// class of the dynamic type of 'node', call a user-overridable 109 /// function to actually visit the node. 110 /// 111 /// These tasks are done by three groups of methods, respectively: 112 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point 113 /// for traversing an AST rooted at x. This method simply 114 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo 115 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and 116 /// then recursively visits the child nodes of x. 117 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work 118 /// similarly. 119 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit 120 /// any child node of x. Instead, it first calls WalkUpFromBar(x) 121 /// where Bar is the direct parent class of Foo (unless Foo has 122 /// no parent), and then calls VisitFoo(x) (see the next list item). 123 /// 3. VisitFoo(Foo *x) does task #3. 124 /// 125 /// These three method groups are tiered (Traverse* > WalkUpFrom* > 126 /// Visit*). A method (e.g. Traverse*) may call methods from the same 127 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*). 128 /// It may not call methods from a higher tier. 129 /// 130 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar 131 /// is Foo's super class) before calling VisitFoo(), the result is 132 /// that the Visit*() methods for a given node are called in the 133 /// top-down order (e.g. for a node of type NamespaceDecl, the order will 134 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()). 135 /// 136 /// This scheme guarantees that all Visit*() calls for the same AST 137 /// node are grouped together. In other words, Visit*() methods for 138 /// different nodes are never interleaved. 139 /// 140 /// Clients of this visitor should subclass the visitor (providing 141 /// themselves as the template argument, using the curiously recurring 142 /// template pattern) and override any of the Traverse*, WalkUpFrom*, 143 /// and Visit* methods for declarations, types, statements, 144 /// expressions, or other AST nodes where the visitor should customize 145 /// behavior. Most users only need to override Visit*. Advanced 146 /// users may override Traverse* and WalkUpFrom* to implement custom 147 /// traversal strategies. Returning false from one of these overridden 148 /// functions will abort the entire traversal. 149 /// 150 /// By default, this visitor tries to visit every part of the explicit 151 /// source code exactly once. The default policy towards templates 152 /// is to descend into the 'pattern' class or function body, not any 153 /// explicit or implicit instantiations. Explicit specializations 154 /// are still visited, and the patterns of partial specializations 155 /// are visited separately. This behavior can be changed by 156 /// overriding shouldVisitTemplateInstantiations() in the derived class 157 /// to return true, in which case all known implicit and explicit 158 /// instantiations will be visited at the same time as the pattern 159 /// from which they were produced. 160 /// 161 /// By default, this visitor preorder traverses the AST. If postorder traversal 162 /// is needed, the \c shouldTraversePostOrder method needs to be overridden 163 /// to return \c true. 164 template <typename Derived> class RecursiveASTVisitor { 165 public: 166 /// A queue used for performing data recursion over statements. 167 /// Parameters involving this type are used to implement data 168 /// recursion over Stmts and Exprs within this class, and should 169 /// typically not be explicitly specified by derived classes. 170 /// The bool bit indicates whether the statement has been traversed or not. 171 typedef SmallVectorImpl<llvm::PointerIntPair<Stmt *, 1, bool>> 172 DataRecursionQueue; 173 174 /// Return a reference to the derived class. 175 Derived &getDerived() { return *static_cast<Derived *>(this); } 176 177 /// Return whether this visitor should recurse into 178 /// template instantiations. 179 bool shouldVisitTemplateInstantiations() const { return false; } 180 181 /// Return whether this visitor should recurse into the types of 182 /// TypeLocs. 183 bool shouldWalkTypesOfTypeLocs() const { return true; } 184 185 /// Return whether this visitor should recurse into implicit 186 /// code, e.g., implicit constructors and destructors. 187 bool shouldVisitImplicitCode() const { return false; } 188 189 /// Return whether this visitor should traverse post-order. 190 bool shouldTraversePostOrder() const { return false; } 191 192 /// Recursively visits an entire AST, starting from the top-level Decls 193 /// in the AST traversal scope (by default, the TranslationUnitDecl). 194 /// \returns false if visitation was terminated early. 195 bool TraverseAST(ASTContext &AST) { 196 for (Decl *D : AST.getTraversalScope()) 197 if (!getDerived().TraverseDecl(D)) 198 return false; 199 return true; 200 } 201 202 /// Recursively visit a statement or expression, by 203 /// dispatching to Traverse*() based on the argument's dynamic type. 204 /// 205 /// \returns false if the visitation was terminated early, true 206 /// otherwise (including when the argument is nullptr). 207 bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue = nullptr); 208 209 /// Invoked before visiting a statement or expression via data recursion. 210 /// 211 /// \returns false to skip visiting the node, true otherwise. 212 bool dataTraverseStmtPre(Stmt *S) { return true; } 213 214 /// Invoked after visiting a statement or expression via data recursion. 215 /// This is not invoked if the previously invoked \c dataTraverseStmtPre 216 /// returned false. 217 /// 218 /// \returns false if the visitation was terminated early, true otherwise. 219 bool dataTraverseStmtPost(Stmt *S) { return true; } 220 221 /// Recursively visit a type, by dispatching to 222 /// Traverse*Type() based on the argument's getTypeClass() property. 223 /// 224 /// \returns false if the visitation was terminated early, true 225 /// otherwise (including when the argument is a Null type). 226 bool TraverseType(QualType T); 227 228 /// Recursively visit a type with location, by dispatching to 229 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property. 230 /// 231 /// \returns false if the visitation was terminated early, true 232 /// otherwise (including when the argument is a Null type location). 233 bool TraverseTypeLoc(TypeLoc TL); 234 235 /// Recursively visit an attribute, by dispatching to 236 /// Traverse*Attr() based on the argument's dynamic type. 237 /// 238 /// \returns false if the visitation was terminated early, true 239 /// otherwise (including when the argument is a Null type location). 240 bool TraverseAttr(Attr *At); 241 242 /// Recursively visit a declaration, by dispatching to 243 /// Traverse*Decl() based on the argument's dynamic type. 244 /// 245 /// \returns false if the visitation was terminated early, true 246 /// otherwise (including when the argument is NULL). 247 bool TraverseDecl(Decl *D); 248 249 /// Recursively visit a C++ nested-name-specifier. 250 /// 251 /// \returns false if the visitation was terminated early, true otherwise. 252 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS); 253 254 /// Recursively visit a C++ nested-name-specifier with location 255 /// information. 256 /// 257 /// \returns false if the visitation was terminated early, true otherwise. 258 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); 259 260 /// Recursively visit a name with its location information. 261 /// 262 /// \returns false if the visitation was terminated early, true otherwise. 263 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo); 264 265 /// Recursively visit a template name and dispatch to the 266 /// appropriate method. 267 /// 268 /// \returns false if the visitation was terminated early, true otherwise. 269 bool TraverseTemplateName(TemplateName Template); 270 271 /// Recursively visit a template argument and dispatch to the 272 /// appropriate method for the argument type. 273 /// 274 /// \returns false if the visitation was terminated early, true otherwise. 275 // FIXME: migrate callers to TemplateArgumentLoc instead. 276 bool TraverseTemplateArgument(const TemplateArgument &Arg); 277 278 /// Recursively visit a template argument location and dispatch to the 279 /// appropriate method for the argument type. 280 /// 281 /// \returns false if the visitation was terminated early, true otherwise. 282 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc); 283 284 /// Recursively visit a set of template arguments. 285 /// This can be overridden by a subclass, but it's not expected that 286 /// will be needed -- this visitor always dispatches to another. 287 /// 288 /// \returns false if the visitation was terminated early, true otherwise. 289 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead. 290 bool TraverseTemplateArguments(const TemplateArgument *Args, 291 unsigned NumArgs); 292 293 /// Recursively visit a base specifier. This can be overridden by a 294 /// subclass. 295 /// 296 /// \returns false if the visitation was terminated early, true otherwise. 297 bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base); 298 299 /// Recursively visit a constructor initializer. This 300 /// automatically dispatches to another visitor for the initializer 301 /// expression, but not for the name of the initializer, so may 302 /// be overridden for clients that need access to the name. 303 /// 304 /// \returns false if the visitation was terminated early, true otherwise. 305 bool TraverseConstructorInitializer(CXXCtorInitializer *Init); 306 307 /// Recursively visit a lambda capture. \c Init is the expression that 308 /// will be used to initialize the capture. 309 /// 310 /// \returns false if the visitation was terminated early, true otherwise. 311 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, 312 Expr *Init); 313 314 /// Recursively visit the syntactic or semantic form of an 315 /// initialization list. 316 /// 317 /// \returns false if the visitation was terminated early, true otherwise. 318 bool TraverseSynOrSemInitListExpr(InitListExpr *S, 319 DataRecursionQueue *Queue = nullptr); 320 321 /// Recursively visit a reference to a concept with potential arguments. 322 /// 323 /// \returns false if the visitation was terminated early, true otherwise. 324 bool TraverseConceptReference(const ConceptReference &C); 325 326 // ---- Methods on Attrs ---- 327 328 // Visit an attribute. 329 bool VisitAttr(Attr *A) { return true; } 330 331 // Declare Traverse* and empty Visit* for all Attr classes. 332 #define ATTR_VISITOR_DECLS_ONLY 333 #include "clang/AST/AttrVisitor.inc" 334 #undef ATTR_VISITOR_DECLS_ONLY 335 336 // ---- Methods on Stmts ---- 337 338 Stmt::child_range getStmtChildren(Stmt *S) { return S->children(); } 339 340 private: 341 // Traverse the given statement. If the most-derived traverse function takes a 342 // data recursion queue, pass it on; otherwise, discard it. Note that the 343 // first branch of this conditional must compile whether or not the derived 344 // class can take a queue, so if we're taking the second arm, make the first 345 // arm call our function rather than the derived class version. 346 #define TRAVERSE_STMT_BASE(NAME, CLASS, VAR, QUEUE) \ 347 (::clang::detail::has_same_member_pointer_type< \ 348 decltype(&RecursiveASTVisitor::Traverse##NAME), \ 349 decltype(&Derived::Traverse##NAME)>::value \ 350 ? static_cast<std::conditional_t< \ 351 ::clang::detail::has_same_member_pointer_type< \ 352 decltype(&RecursiveASTVisitor::Traverse##NAME), \ 353 decltype(&Derived::Traverse##NAME)>::value, \ 354 Derived &, RecursiveASTVisitor &>>(*this) \ 355 .Traverse##NAME(static_cast<CLASS *>(VAR), QUEUE) \ 356 : getDerived().Traverse##NAME(static_cast<CLASS *>(VAR))) 357 358 // Try to traverse the given statement, or enqueue it if we're performing data 359 // recursion in the middle of traversing another statement. Can only be called 360 // from within a DEF_TRAVERSE_STMT body or similar context. 361 #define TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S) \ 362 do { \ 363 if (!TRAVERSE_STMT_BASE(Stmt, Stmt, S, Queue)) \ 364 return false; \ 365 } while (false) 366 367 public: 368 // Declare Traverse*() for all concrete Stmt classes. 369 #define ABSTRACT_STMT(STMT) 370 #define STMT(CLASS, PARENT) \ 371 bool Traverse##CLASS(CLASS *S, DataRecursionQueue *Queue = nullptr); 372 #include "clang/AST/StmtNodes.inc" 373 // The above header #undefs ABSTRACT_STMT and STMT upon exit. 374 375 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes. 376 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); } 377 bool VisitStmt(Stmt *S) { return true; } 378 #define STMT(CLASS, PARENT) \ 379 bool WalkUpFrom##CLASS(CLASS *S) { \ 380 TRY_TO(WalkUpFrom##PARENT(S)); \ 381 TRY_TO(Visit##CLASS(S)); \ 382 return true; \ 383 } \ 384 bool Visit##CLASS(CLASS *S) { return true; } 385 #include "clang/AST/StmtNodes.inc" 386 387 // ---- Methods on Types ---- 388 // FIXME: revamp to take TypeLoc's rather than Types. 389 390 // Declare Traverse*() for all concrete Type classes. 391 #define ABSTRACT_TYPE(CLASS, BASE) 392 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T); 393 #include "clang/AST/TypeNodes.inc" 394 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit. 395 396 // Define WalkUpFrom*() and empty Visit*() for all Type classes. 397 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); } 398 bool VisitType(Type *T) { return true; } 399 #define TYPE(CLASS, BASE) \ 400 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \ 401 TRY_TO(WalkUpFrom##BASE(T)); \ 402 TRY_TO(Visit##CLASS##Type(T)); \ 403 return true; \ 404 } \ 405 bool Visit##CLASS##Type(CLASS##Type *T) { return true; } 406 #include "clang/AST/TypeNodes.inc" 407 408 // ---- Methods on TypeLocs ---- 409 // FIXME: this currently just calls the matching Type methods 410 411 // Declare Traverse*() for all concrete TypeLoc classes. 412 #define ABSTRACT_TYPELOC(CLASS, BASE) 413 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL); 414 #include "clang/AST/TypeLocNodes.def" 415 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit. 416 417 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes. 418 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); } 419 bool VisitTypeLoc(TypeLoc TL) { return true; } 420 421 // QualifiedTypeLoc and UnqualTypeLoc are not declared in 422 // TypeNodes.inc and thus need to be handled specially. 423 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) { 424 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc()); 425 } 426 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; } 427 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) { 428 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc()); 429 } 430 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; } 431 432 // Note that BASE includes trailing 'Type' which CLASS doesn't. 433 #define TYPE(CLASS, BASE) \ 434 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ 435 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \ 436 TRY_TO(Visit##CLASS##TypeLoc(TL)); \ 437 return true; \ 438 } \ 439 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; } 440 #include "clang/AST/TypeNodes.inc" 441 442 // ---- Methods on Decls ---- 443 444 // Declare Traverse*() for all concrete Decl classes. 445 #define ABSTRACT_DECL(DECL) 446 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D); 447 #include "clang/AST/DeclNodes.inc" 448 // The above header #undefs ABSTRACT_DECL and DECL upon exit. 449 450 // Define WalkUpFrom*() and empty Visit*() for all Decl classes. 451 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); } 452 bool VisitDecl(Decl *D) { return true; } 453 #define DECL(CLASS, BASE) \ 454 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \ 455 TRY_TO(WalkUpFrom##BASE(D)); \ 456 TRY_TO(Visit##CLASS##Decl(D)); \ 457 return true; \ 458 } \ 459 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; } 460 #include "clang/AST/DeclNodes.inc" 461 462 bool canIgnoreChildDeclWhileTraversingDeclContext(const Decl *Child); 463 464 private: 465 // These are helper methods used by more than one Traverse* method. 466 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL); 467 468 // Traverses template parameter lists of either a DeclaratorDecl or TagDecl. 469 template <typename T> 470 bool TraverseDeclTemplateParameterLists(T *D); 471 472 #define DEF_TRAVERSE_TMPL_INST(TMPLDECLKIND) \ 473 bool TraverseTemplateInstantiations(TMPLDECLKIND##TemplateDecl *D); 474 DEF_TRAVERSE_TMPL_INST(Class) 475 DEF_TRAVERSE_TMPL_INST(Var) 476 DEF_TRAVERSE_TMPL_INST(Function) 477 #undef DEF_TRAVERSE_TMPL_INST 478 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL, 479 unsigned Count); 480 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL); 481 bool TraverseRecordHelper(RecordDecl *D); 482 bool TraverseCXXRecordHelper(CXXRecordDecl *D); 483 bool TraverseDeclaratorHelper(DeclaratorDecl *D); 484 bool TraverseDeclContextHelper(DeclContext *DC); 485 bool TraverseFunctionHelper(FunctionDecl *D); 486 bool TraverseVarHelper(VarDecl *D); 487 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S); 488 bool TraverseOMPLoopDirective(OMPLoopDirective *S); 489 bool TraverseOMPClause(OMPClause *C); 490 #define OMP_CLAUSE_CLASS(Enum, Str, Class) bool Visit##Class(Class *C); 491 #include "llvm/Frontend/OpenMP/OMPKinds.def" 492 /// Process clauses with list of variables. 493 template <typename T> bool VisitOMPClauseList(T *Node); 494 /// Process clauses with pre-initis. 495 bool VisitOMPClauseWithPreInit(OMPClauseWithPreInit *Node); 496 bool VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *Node); 497 498 bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue); 499 bool PostVisitStmt(Stmt *S); 500 }; 501 502 template <typename Derived> 503 bool RecursiveASTVisitor<Derived>::dataTraverseNode(Stmt *S, 504 DataRecursionQueue *Queue) { 505 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt. 506 switch (S->getStmtClass()) { 507 case Stmt::NoStmtClass: 508 break; 509 #define ABSTRACT_STMT(STMT) 510 #define STMT(CLASS, PARENT) \ 511 case Stmt::CLASS##Class: \ 512 return TRAVERSE_STMT_BASE(CLASS, CLASS, S, Queue); 513 #include "clang/AST/StmtNodes.inc" 514 } 515 516 return true; 517 } 518 519 #undef DISPATCH_STMT 520 521 template <typename Derived> 522 bool RecursiveASTVisitor<Derived>::PostVisitStmt(Stmt *S) { 523 // In pre-order traversal mode, each Traverse##STMT method is responsible for 524 // calling WalkUpFrom. Therefore, if the user overrides Traverse##STMT and 525 // does not call the default implementation, the WalkUpFrom callback is not 526 // called. Post-order traversal mode should provide the same behavior 527 // regarding method overrides. 528 // 529 // In post-order traversal mode the Traverse##STMT method, when it receives a 530 // DataRecursionQueue, can't call WalkUpFrom after traversing children because 531 // it only enqueues the children and does not traverse them. TraverseStmt 532 // traverses the enqueued children, and we call WalkUpFrom here. 533 // 534 // However, to make pre-order and post-order modes identical with regards to 535 // whether they call WalkUpFrom at all, we call WalkUpFrom if and only if the 536 // user did not override the Traverse##STMT method. We implement the override 537 // check with isSameMethod calls below. 538 539 switch (S->getStmtClass()) { 540 case Stmt::NoStmtClass: 541 break; 542 #define ABSTRACT_STMT(STMT) 543 #define STMT(CLASS, PARENT) \ 544 case Stmt::CLASS##Class: \ 545 if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS, \ 546 &Derived::Traverse##CLASS)) { \ 547 TRY_TO(WalkUpFrom##CLASS(static_cast<CLASS *>(S))); \ 548 } \ 549 break; 550 #define INITLISTEXPR(CLASS, PARENT) \ 551 case Stmt::CLASS##Class: \ 552 if (::clang::detail::isSameMethod(&RecursiveASTVisitor::Traverse##CLASS, \ 553 &Derived::Traverse##CLASS)) { \ 554 auto ILE = static_cast<CLASS *>(S); \ 555 if (auto Syn = ILE->isSemanticForm() ? ILE->getSyntacticForm() : ILE) \ 556 TRY_TO(WalkUpFrom##CLASS(Syn)); \ 557 if (auto Sem = ILE->isSemanticForm() ? ILE : ILE->getSemanticForm()) \ 558 TRY_TO(WalkUpFrom##CLASS(Sem)); \ 559 } \ 560 break; 561 #include "clang/AST/StmtNodes.inc" 562 } 563 564 return true; 565 } 566 567 #undef DISPATCH_STMT 568 569 template <typename Derived> 570 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S, 571 DataRecursionQueue *Queue) { 572 if (!S) 573 return true; 574 575 if (Queue) { 576 Queue->push_back({S, false}); 577 return true; 578 } 579 580 SmallVector<llvm::PointerIntPair<Stmt *, 1, bool>, 8> LocalQueue; 581 LocalQueue.push_back({S, false}); 582 583 while (!LocalQueue.empty()) { 584 auto &CurrSAndVisited = LocalQueue.back(); 585 Stmt *CurrS = CurrSAndVisited.getPointer(); 586 bool Visited = CurrSAndVisited.getInt(); 587 if (Visited) { 588 LocalQueue.pop_back(); 589 TRY_TO(dataTraverseStmtPost(CurrS)); 590 if (getDerived().shouldTraversePostOrder()) { 591 TRY_TO(PostVisitStmt(CurrS)); 592 } 593 continue; 594 } 595 596 if (getDerived().dataTraverseStmtPre(CurrS)) { 597 CurrSAndVisited.setInt(true); 598 size_t N = LocalQueue.size(); 599 TRY_TO(dataTraverseNode(CurrS, &LocalQueue)); 600 // Process new children in the order they were added. 601 std::reverse(LocalQueue.begin() + N, LocalQueue.end()); 602 } else { 603 LocalQueue.pop_back(); 604 } 605 } 606 607 return true; 608 } 609 610 template <typename Derived> 611 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) { 612 if (T.isNull()) 613 return true; 614 615 switch (T->getTypeClass()) { 616 #define ABSTRACT_TYPE(CLASS, BASE) 617 #define TYPE(CLASS, BASE) \ 618 case Type::CLASS: \ 619 return getDerived().Traverse##CLASS##Type( \ 620 static_cast<CLASS##Type *>(const_cast<Type *>(T.getTypePtr()))); 621 #include "clang/AST/TypeNodes.inc" 622 } 623 624 return true; 625 } 626 627 template <typename Derived> 628 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) { 629 if (TL.isNull()) 630 return true; 631 632 switch (TL.getTypeLocClass()) { 633 #define ABSTRACT_TYPELOC(CLASS, BASE) 634 #define TYPELOC(CLASS, BASE) \ 635 case TypeLoc::CLASS: \ 636 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>()); 637 #include "clang/AST/TypeLocNodes.def" 638 } 639 640 return true; 641 } 642 643 // Define the Traverse*Attr(Attr* A) methods 644 #define VISITORCLASS RecursiveASTVisitor 645 #include "clang/AST/AttrVisitor.inc" 646 #undef VISITORCLASS 647 648 template <typename Derived> 649 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) { 650 if (!D) 651 return true; 652 653 // As a syntax visitor, by default we want to ignore declarations for 654 // implicit declarations (ones not typed explicitly by the user). 655 if (!getDerived().shouldVisitImplicitCode() && D->isImplicit()) 656 return true; 657 658 switch (D->getKind()) { 659 #define ABSTRACT_DECL(DECL) 660 #define DECL(CLASS, BASE) \ 661 case Decl::CLASS: \ 662 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \ 663 return false; \ 664 break; 665 #include "clang/AST/DeclNodes.inc" 666 } 667 return true; 668 } 669 670 template <typename Derived> 671 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier( 672 NestedNameSpecifier *NNS) { 673 if (!NNS) 674 return true; 675 676 if (NNS->getPrefix()) 677 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix())); 678 679 switch (NNS->getKind()) { 680 case NestedNameSpecifier::Identifier: 681 case NestedNameSpecifier::Namespace: 682 case NestedNameSpecifier::NamespaceAlias: 683 case NestedNameSpecifier::Global: 684 case NestedNameSpecifier::Super: 685 return true; 686 687 case NestedNameSpecifier::TypeSpec: 688 case NestedNameSpecifier::TypeSpecWithTemplate: 689 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0))); 690 } 691 692 return true; 693 } 694 695 template <typename Derived> 696 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc( 697 NestedNameSpecifierLoc NNS) { 698 if (!NNS) 699 return true; 700 701 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix()) 702 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix)); 703 704 switch (NNS.getNestedNameSpecifier()->getKind()) { 705 case NestedNameSpecifier::Identifier: 706 case NestedNameSpecifier::Namespace: 707 case NestedNameSpecifier::NamespaceAlias: 708 case NestedNameSpecifier::Global: 709 case NestedNameSpecifier::Super: 710 return true; 711 712 case NestedNameSpecifier::TypeSpec: 713 case NestedNameSpecifier::TypeSpecWithTemplate: 714 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc())); 715 break; 716 } 717 718 return true; 719 } 720 721 template <typename Derived> 722 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo( 723 DeclarationNameInfo NameInfo) { 724 switch (NameInfo.getName().getNameKind()) { 725 case DeclarationName::CXXConstructorName: 726 case DeclarationName::CXXDestructorName: 727 case DeclarationName::CXXConversionFunctionName: 728 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) 729 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc())); 730 break; 731 732 case DeclarationName::CXXDeductionGuideName: 733 TRY_TO(TraverseTemplateName( 734 TemplateName(NameInfo.getName().getCXXDeductionGuideTemplate()))); 735 break; 736 737 case DeclarationName::Identifier: 738 case DeclarationName::ObjCZeroArgSelector: 739 case DeclarationName::ObjCOneArgSelector: 740 case DeclarationName::ObjCMultiArgSelector: 741 case DeclarationName::CXXOperatorName: 742 case DeclarationName::CXXLiteralOperatorName: 743 case DeclarationName::CXXUsingDirective: 744 break; 745 } 746 747 return true; 748 } 749 750 template <typename Derived> 751 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) { 752 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) 753 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier())); 754 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) 755 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier())); 756 757 return true; 758 } 759 760 template <typename Derived> 761 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument( 762 const TemplateArgument &Arg) { 763 switch (Arg.getKind()) { 764 case TemplateArgument::Null: 765 case TemplateArgument::Declaration: 766 case TemplateArgument::Integral: 767 case TemplateArgument::NullPtr: 768 return true; 769 770 case TemplateArgument::Type: 771 return getDerived().TraverseType(Arg.getAsType()); 772 773 case TemplateArgument::Template: 774 case TemplateArgument::TemplateExpansion: 775 return getDerived().TraverseTemplateName( 776 Arg.getAsTemplateOrTemplatePattern()); 777 778 case TemplateArgument::Expression: 779 return getDerived().TraverseStmt(Arg.getAsExpr()); 780 781 case TemplateArgument::Pack: 782 return getDerived().TraverseTemplateArguments(Arg.pack_begin(), 783 Arg.pack_size()); 784 } 785 786 return true; 787 } 788 789 // FIXME: no template name location? 790 // FIXME: no source locations for a template argument pack? 791 template <typename Derived> 792 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc( 793 const TemplateArgumentLoc &ArgLoc) { 794 const TemplateArgument &Arg = ArgLoc.getArgument(); 795 796 switch (Arg.getKind()) { 797 case TemplateArgument::Null: 798 case TemplateArgument::Declaration: 799 case TemplateArgument::Integral: 800 case TemplateArgument::NullPtr: 801 return true; 802 803 case TemplateArgument::Type: { 804 // FIXME: how can TSI ever be NULL? 805 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo()) 806 return getDerived().TraverseTypeLoc(TSI->getTypeLoc()); 807 else 808 return getDerived().TraverseType(Arg.getAsType()); 809 } 810 811 case TemplateArgument::Template: 812 case TemplateArgument::TemplateExpansion: 813 if (ArgLoc.getTemplateQualifierLoc()) 814 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc( 815 ArgLoc.getTemplateQualifierLoc())); 816 return getDerived().TraverseTemplateName( 817 Arg.getAsTemplateOrTemplatePattern()); 818 819 case TemplateArgument::Expression: 820 return getDerived().TraverseStmt(ArgLoc.getSourceExpression()); 821 822 case TemplateArgument::Pack: 823 return getDerived().TraverseTemplateArguments(Arg.pack_begin(), 824 Arg.pack_size()); 825 } 826 827 return true; 828 } 829 830 template <typename Derived> 831 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments( 832 const TemplateArgument *Args, unsigned NumArgs) { 833 for (unsigned I = 0; I != NumArgs; ++I) { 834 TRY_TO(TraverseTemplateArgument(Args[I])); 835 } 836 837 return true; 838 } 839 840 template <typename Derived> 841 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer( 842 CXXCtorInitializer *Init) { 843 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) 844 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 845 846 if (Init->isWritten() || getDerived().shouldVisitImplicitCode()) 847 TRY_TO(TraverseStmt(Init->getInit())); 848 849 return true; 850 } 851 852 template <typename Derived> 853 bool 854 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE, 855 const LambdaCapture *C, 856 Expr *Init) { 857 if (LE->isInitCapture(C)) 858 TRY_TO(TraverseDecl(C->getCapturedVar())); 859 else 860 TRY_TO(TraverseStmt(Init)); 861 return true; 862 } 863 864 // ----------------- Type traversal ----------------- 865 866 // This macro makes available a variable T, the passed-in type. 867 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \ 868 template <typename Derived> \ 869 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \ 870 if (!getDerived().shouldTraversePostOrder()) \ 871 TRY_TO(WalkUpFrom##TYPE(T)); \ 872 { CODE; } \ 873 if (getDerived().shouldTraversePostOrder()) \ 874 TRY_TO(WalkUpFrom##TYPE(T)); \ 875 return true; \ 876 } 877 878 DEF_TRAVERSE_TYPE(BuiltinType, {}) 879 880 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); }) 881 882 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); }) 883 884 DEF_TRAVERSE_TYPE(BlockPointerType, 885 { TRY_TO(TraverseType(T->getPointeeType())); }) 886 887 DEF_TRAVERSE_TYPE(LValueReferenceType, 888 { TRY_TO(TraverseType(T->getPointeeType())); }) 889 890 DEF_TRAVERSE_TYPE(RValueReferenceType, 891 { TRY_TO(TraverseType(T->getPointeeType())); }) 892 893 DEF_TRAVERSE_TYPE(MemberPointerType, { 894 TRY_TO(TraverseType(QualType(T->getClass(), 0))); 895 TRY_TO(TraverseType(T->getPointeeType())); 896 }) 897 898 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); }) 899 900 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); }) 901 902 DEF_TRAVERSE_TYPE(ConstantArrayType, { 903 TRY_TO(TraverseType(T->getElementType())); 904 if (T->getSizeExpr()) 905 TRY_TO(TraverseStmt(const_cast<Expr*>(T->getSizeExpr()))); 906 }) 907 908 DEF_TRAVERSE_TYPE(IncompleteArrayType, 909 { TRY_TO(TraverseType(T->getElementType())); }) 910 911 DEF_TRAVERSE_TYPE(VariableArrayType, { 912 TRY_TO(TraverseType(T->getElementType())); 913 TRY_TO(TraverseStmt(T->getSizeExpr())); 914 }) 915 916 DEF_TRAVERSE_TYPE(DependentSizedArrayType, { 917 TRY_TO(TraverseType(T->getElementType())); 918 if (T->getSizeExpr()) 919 TRY_TO(TraverseStmt(T->getSizeExpr())); 920 }) 921 922 DEF_TRAVERSE_TYPE(DependentAddressSpaceType, { 923 TRY_TO(TraverseStmt(T->getAddrSpaceExpr())); 924 TRY_TO(TraverseType(T->getPointeeType())); 925 }) 926 927 DEF_TRAVERSE_TYPE(DependentPointerType, { 928 TRY_TO(TraverseType(T->getPointerType())); 929 }) 930 931 DEF_TRAVERSE_TYPE(DependentVectorType, { 932 if (T->getSizeExpr()) 933 TRY_TO(TraverseStmt(T->getSizeExpr())); 934 TRY_TO(TraverseType(T->getElementType())); 935 }) 936 937 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, { 938 if (T->getSizeExpr()) 939 TRY_TO(TraverseStmt(T->getSizeExpr())); 940 TRY_TO(TraverseType(T->getElementType())); 941 }) 942 943 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); }) 944 945 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); }) 946 947 DEF_TRAVERSE_TYPE(ConstantMatrixType, 948 { TRY_TO(TraverseType(T->getElementType())); }) 949 950 DEF_TRAVERSE_TYPE(DependentSizedMatrixType, { 951 if (T->getRowExpr()) 952 TRY_TO(TraverseStmt(T->getRowExpr())); 953 if (T->getColumnExpr()) 954 TRY_TO(TraverseStmt(T->getColumnExpr())); 955 TRY_TO(TraverseType(T->getElementType())); 956 }) 957 958 DEF_TRAVERSE_TYPE(FunctionNoProtoType, 959 { TRY_TO(TraverseType(T->getReturnType())); }) 960 961 DEF_TRAVERSE_TYPE(FunctionProtoType, { 962 TRY_TO(TraverseType(T->getReturnType())); 963 964 for (const auto &A : T->param_types()) { 965 TRY_TO(TraverseType(A)); 966 } 967 968 for (const auto &E : T->exceptions()) { 969 TRY_TO(TraverseType(E)); 970 } 971 972 if (Expr *NE = T->getNoexceptExpr()) 973 TRY_TO(TraverseStmt(NE)); 974 }) 975 976 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {}) 977 DEF_TRAVERSE_TYPE(TypedefType, {}) 978 979 DEF_TRAVERSE_TYPE(TypeOfExprType, 980 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) 981 982 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); }) 983 984 DEF_TRAVERSE_TYPE(DecltypeType, 985 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) 986 987 DEF_TRAVERSE_TYPE(UnaryTransformType, { 988 TRY_TO(TraverseType(T->getBaseType())); 989 TRY_TO(TraverseType(T->getUnderlyingType())); 990 }) 991 992 DEF_TRAVERSE_TYPE(AutoType, { 993 TRY_TO(TraverseType(T->getDeducedType())); 994 if (T->isConstrained()) { 995 TRY_TO(TraverseDecl(T->getTypeConstraintConcept())); 996 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 997 } 998 }) 999 DEF_TRAVERSE_TYPE(DeducedTemplateSpecializationType, { 1000 TRY_TO(TraverseTemplateName(T->getTemplateName())); 1001 TRY_TO(TraverseType(T->getDeducedType())); 1002 }) 1003 1004 DEF_TRAVERSE_TYPE(RecordType, {}) 1005 DEF_TRAVERSE_TYPE(EnumType, {}) 1006 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {}) 1007 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, { 1008 TRY_TO(TraverseType(T->getReplacementType())); 1009 }) 1010 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, { 1011 TRY_TO(TraverseTemplateArgument(T->getArgumentPack())); 1012 }) 1013 1014 DEF_TRAVERSE_TYPE(TemplateSpecializationType, { 1015 TRY_TO(TraverseTemplateName(T->getTemplateName())); 1016 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 1017 }) 1018 1019 DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) 1020 1021 DEF_TRAVERSE_TYPE(AttributedType, 1022 { TRY_TO(TraverseType(T->getModifiedType())); }) 1023 1024 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); }) 1025 1026 DEF_TRAVERSE_TYPE(MacroQualifiedType, 1027 { TRY_TO(TraverseType(T->getUnderlyingType())); }) 1028 1029 DEF_TRAVERSE_TYPE(ElaboratedType, { 1030 if (T->getQualifier()) { 1031 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); 1032 } 1033 TRY_TO(TraverseType(T->getNamedType())); 1034 }) 1035 1036 DEF_TRAVERSE_TYPE(DependentNameType, 1037 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); }) 1038 1039 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, { 1040 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); 1041 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 1042 }) 1043 1044 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); }) 1045 1046 DEF_TRAVERSE_TYPE(ObjCTypeParamType, {}) 1047 1048 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {}) 1049 1050 DEF_TRAVERSE_TYPE(ObjCObjectType, { 1051 // We have to watch out here because an ObjCInterfaceType's base 1052 // type is itself. 1053 if (T->getBaseType().getTypePtr() != T) 1054 TRY_TO(TraverseType(T->getBaseType())); 1055 for (auto typeArg : T->getTypeArgsAsWritten()) { 1056 TRY_TO(TraverseType(typeArg)); 1057 } 1058 }) 1059 1060 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, 1061 { TRY_TO(TraverseType(T->getPointeeType())); }) 1062 1063 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); }) 1064 1065 DEF_TRAVERSE_TYPE(PipeType, { TRY_TO(TraverseType(T->getElementType())); }) 1066 1067 DEF_TRAVERSE_TYPE(ExtIntType, {}) 1068 DEF_TRAVERSE_TYPE(DependentExtIntType, 1069 { TRY_TO(TraverseStmt(T->getNumBitsExpr())); }) 1070 1071 #undef DEF_TRAVERSE_TYPE 1072 1073 // ----------------- TypeLoc traversal ----------------- 1074 1075 // This macro makes available a variable TL, the passed-in TypeLoc. 1076 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc, 1077 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing 1078 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods 1079 // continue to work. 1080 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \ 1081 template <typename Derived> \ 1082 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \ 1083 if (!getDerived().shouldTraversePostOrder()) { \ 1084 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \ 1085 if (getDerived().shouldWalkTypesOfTypeLocs()) \ 1086 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \ 1087 } \ 1088 { CODE; } \ 1089 if (getDerived().shouldTraversePostOrder()) { \ 1090 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \ 1091 if (getDerived().shouldWalkTypesOfTypeLocs()) \ 1092 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \ 1093 } \ 1094 return true; \ 1095 } 1096 1097 template <typename Derived> 1098 bool 1099 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) { 1100 // Move this over to the 'main' typeloc tree. Note that this is a 1101 // move -- we pretend that we were really looking at the unqualified 1102 // typeloc all along -- rather than a recursion, so we don't follow 1103 // the normal CRTP plan of going through 1104 // getDerived().TraverseTypeLoc. If we did, we'd be traversing 1105 // twice for the same type (once as a QualifiedTypeLoc version of 1106 // the type, once as an UnqualifiedTypeLoc version of the type), 1107 // which in effect means we'd call VisitTypeLoc twice with the 1108 // 'same' type. This solves that problem, at the cost of never 1109 // seeing the qualified version of the type (unless the client 1110 // subclasses TraverseQualifiedTypeLoc themselves). It's not a 1111 // perfect solution. A perfect solution probably requires making 1112 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a 1113 // wrapper around Type* -- rather than being its own class in the 1114 // type hierarchy. 1115 return TraverseTypeLoc(TL.getUnqualifiedLoc()); 1116 } 1117 1118 DEF_TRAVERSE_TYPELOC(BuiltinType, {}) 1119 1120 // FIXME: ComplexTypeLoc is unfinished 1121 DEF_TRAVERSE_TYPELOC(ComplexType, { 1122 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1123 }) 1124 1125 DEF_TRAVERSE_TYPELOC(PointerType, 1126 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1127 1128 DEF_TRAVERSE_TYPELOC(BlockPointerType, 1129 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1130 1131 DEF_TRAVERSE_TYPELOC(LValueReferenceType, 1132 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1133 1134 DEF_TRAVERSE_TYPELOC(RValueReferenceType, 1135 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1136 1137 // We traverse this in the type case as well, but how is it not reached through 1138 // the pointee type? 1139 DEF_TRAVERSE_TYPELOC(MemberPointerType, { 1140 if (auto *TSI = TL.getClassTInfo()) 1141 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1142 else 1143 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0))); 1144 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); 1145 }) 1146 1147 DEF_TRAVERSE_TYPELOC(AdjustedType, 1148 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) 1149 1150 DEF_TRAVERSE_TYPELOC(DecayedType, 1151 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) 1152 1153 template <typename Derived> 1154 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) { 1155 // This isn't available for ArrayType, but is for the ArrayTypeLoc. 1156 TRY_TO(TraverseStmt(TL.getSizeExpr())); 1157 return true; 1158 } 1159 1160 DEF_TRAVERSE_TYPELOC(ConstantArrayType, { 1161 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1162 TRY_TO(TraverseArrayTypeLocHelper(TL)); 1163 }) 1164 1165 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, { 1166 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1167 TRY_TO(TraverseArrayTypeLocHelper(TL)); 1168 }) 1169 1170 DEF_TRAVERSE_TYPELOC(VariableArrayType, { 1171 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1172 TRY_TO(TraverseArrayTypeLocHelper(TL)); 1173 }) 1174 1175 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, { 1176 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1177 TRY_TO(TraverseArrayTypeLocHelper(TL)); 1178 }) 1179 1180 DEF_TRAVERSE_TYPELOC(DependentAddressSpaceType, { 1181 TRY_TO(TraverseStmt(TL.getTypePtr()->getAddrSpaceExpr())); 1182 TRY_TO(TraverseType(TL.getTypePtr()->getPointeeType())); 1183 }) 1184 1185 DEF_TRAVERSE_TYPELOC(DependentPointerType, { 1186 TRY_TO(TraverseType(TL.getTypePtr()->getPointerType())); 1187 }) 1188 1189 // FIXME: order? why not size expr first? 1190 // FIXME: base VectorTypeLoc is unfinished 1191 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, { 1192 if (TL.getTypePtr()->getSizeExpr()) 1193 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr())); 1194 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1195 }) 1196 1197 // FIXME: VectorTypeLoc is unfinished 1198 DEF_TRAVERSE_TYPELOC(VectorType, { 1199 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1200 }) 1201 1202 DEF_TRAVERSE_TYPELOC(DependentVectorType, { 1203 if (TL.getTypePtr()->getSizeExpr()) 1204 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr())); 1205 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1206 }) 1207 1208 // FIXME: size and attributes 1209 // FIXME: base VectorTypeLoc is unfinished 1210 DEF_TRAVERSE_TYPELOC(ExtVectorType, { 1211 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1212 }) 1213 1214 DEF_TRAVERSE_TYPELOC(ConstantMatrixType, { 1215 TRY_TO(TraverseStmt(TL.getAttrRowOperand())); 1216 TRY_TO(TraverseStmt(TL.getAttrColumnOperand())); 1217 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1218 }) 1219 1220 DEF_TRAVERSE_TYPELOC(DependentSizedMatrixType, { 1221 TRY_TO(TraverseStmt(TL.getAttrRowOperand())); 1222 TRY_TO(TraverseStmt(TL.getAttrColumnOperand())); 1223 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1224 }) 1225 1226 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, 1227 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); }) 1228 1229 // FIXME: location of exception specifications (attributes?) 1230 DEF_TRAVERSE_TYPELOC(FunctionProtoType, { 1231 TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); 1232 1233 const FunctionProtoType *T = TL.getTypePtr(); 1234 1235 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { 1236 if (TL.getParam(I)) { 1237 TRY_TO(TraverseDecl(TL.getParam(I))); 1238 } else if (I < T->getNumParams()) { 1239 TRY_TO(TraverseType(T->getParamType(I))); 1240 } 1241 } 1242 1243 for (const auto &E : T->exceptions()) { 1244 TRY_TO(TraverseType(E)); 1245 } 1246 1247 if (Expr *NE = T->getNoexceptExpr()) 1248 TRY_TO(TraverseStmt(NE)); 1249 }) 1250 1251 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {}) 1252 DEF_TRAVERSE_TYPELOC(TypedefType, {}) 1253 1254 DEF_TRAVERSE_TYPELOC(TypeOfExprType, 1255 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); }) 1256 1257 DEF_TRAVERSE_TYPELOC(TypeOfType, { 1258 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc())); 1259 }) 1260 1261 // FIXME: location of underlying expr 1262 DEF_TRAVERSE_TYPELOC(DecltypeType, { 1263 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr())); 1264 }) 1265 1266 DEF_TRAVERSE_TYPELOC(UnaryTransformType, { 1267 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc())); 1268 }) 1269 1270 DEF_TRAVERSE_TYPELOC(AutoType, { 1271 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType())); 1272 if (TL.isConstrained()) { 1273 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getNestedNameSpecifierLoc())); 1274 TRY_TO(TraverseDeclarationNameInfo(TL.getConceptNameInfo())); 1275 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 1276 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1277 } 1278 }) 1279 1280 DEF_TRAVERSE_TYPELOC(DeducedTemplateSpecializationType, { 1281 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName())); 1282 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType())); 1283 }) 1284 1285 DEF_TRAVERSE_TYPELOC(RecordType, {}) 1286 DEF_TRAVERSE_TYPELOC(EnumType, {}) 1287 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {}) 1288 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, { 1289 TRY_TO(TraverseType(TL.getTypePtr()->getReplacementType())); 1290 }) 1291 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, { 1292 TRY_TO(TraverseTemplateArgument(TL.getTypePtr()->getArgumentPack())); 1293 }) 1294 1295 // FIXME: use the loc for the template name? 1296 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, { 1297 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName())); 1298 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { 1299 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1300 } 1301 }) 1302 1303 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {}) 1304 1305 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) 1306 1307 DEF_TRAVERSE_TYPELOC(MacroQualifiedType, 1308 { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) 1309 1310 DEF_TRAVERSE_TYPELOC(AttributedType, 1311 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) 1312 1313 DEF_TRAVERSE_TYPELOC(ElaboratedType, { 1314 if (TL.getQualifierLoc()) { 1315 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1316 } 1317 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc())); 1318 }) 1319 1320 DEF_TRAVERSE_TYPELOC(DependentNameType, { 1321 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1322 }) 1323 1324 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, { 1325 if (TL.getQualifierLoc()) { 1326 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1327 } 1328 1329 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { 1330 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1331 } 1332 }) 1333 1334 DEF_TRAVERSE_TYPELOC(PackExpansionType, 1335 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); }) 1336 1337 DEF_TRAVERSE_TYPELOC(ObjCTypeParamType, {}) 1338 1339 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {}) 1340 1341 DEF_TRAVERSE_TYPELOC(ObjCObjectType, { 1342 // We have to watch out here because an ObjCInterfaceType's base 1343 // type is itself. 1344 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr()) 1345 TRY_TO(TraverseTypeLoc(TL.getBaseLoc())); 1346 for (unsigned i = 0, n = TL.getNumTypeArgs(); i != n; ++i) 1347 TRY_TO(TraverseTypeLoc(TL.getTypeArgTInfo(i)->getTypeLoc())); 1348 }) 1349 1350 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, 1351 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1352 1353 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); }) 1354 1355 DEF_TRAVERSE_TYPELOC(PipeType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); }) 1356 1357 DEF_TRAVERSE_TYPELOC(ExtIntType, {}) 1358 DEF_TRAVERSE_TYPELOC(DependentExtIntType, { 1359 TRY_TO(TraverseStmt(TL.getTypePtr()->getNumBitsExpr())); 1360 }) 1361 1362 #undef DEF_TRAVERSE_TYPELOC 1363 1364 // ----------------- Decl traversal ----------------- 1365 // 1366 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing 1367 // the children that come from the DeclContext associated with it. 1368 // Therefore each Traverse* only needs to worry about children other 1369 // than those. 1370 1371 template <typename Derived> 1372 bool RecursiveASTVisitor<Derived>::canIgnoreChildDeclWhileTraversingDeclContext( 1373 const Decl *Child) { 1374 // BlockDecls are traversed through BlockExprs, 1375 // CapturedDecls are traversed through CapturedStmts. 1376 if (isa<BlockDecl>(Child) || isa<CapturedDecl>(Child)) 1377 return true; 1378 // Lambda classes are traversed through LambdaExprs. 1379 if (const CXXRecordDecl* Cls = dyn_cast<CXXRecordDecl>(Child)) 1380 return Cls->isLambda(); 1381 return false; 1382 } 1383 1384 template <typename Derived> 1385 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) { 1386 if (!DC) 1387 return true; 1388 1389 for (auto *Child : DC->decls()) { 1390 if (!canIgnoreChildDeclWhileTraversingDeclContext(Child)) 1391 TRY_TO(TraverseDecl(Child)); 1392 } 1393 1394 return true; 1395 } 1396 1397 // This macro makes available a variable D, the passed-in decl. 1398 #define DEF_TRAVERSE_DECL(DECL, CODE) \ 1399 template <typename Derived> \ 1400 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \ 1401 bool ShouldVisitChildren = true; \ 1402 bool ReturnValue = true; \ 1403 if (!getDerived().shouldTraversePostOrder()) \ 1404 TRY_TO(WalkUpFrom##DECL(D)); \ 1405 { CODE; } \ 1406 if (ReturnValue && ShouldVisitChildren) \ 1407 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \ 1408 if (ReturnValue) { \ 1409 /* Visit any attributes attached to this declaration. */ \ 1410 for (auto *I : D->attrs()) \ 1411 TRY_TO(getDerived().TraverseAttr(I)); \ 1412 } \ 1413 if (ReturnValue && getDerived().shouldTraversePostOrder()) \ 1414 TRY_TO(WalkUpFrom##DECL(D)); \ 1415 return ReturnValue; \ 1416 } 1417 1418 DEF_TRAVERSE_DECL(AccessSpecDecl, {}) 1419 1420 DEF_TRAVERSE_DECL(BlockDecl, { 1421 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten()) 1422 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 1423 TRY_TO(TraverseStmt(D->getBody())); 1424 for (const auto &I : D->captures()) { 1425 if (I.hasCopyExpr()) { 1426 TRY_TO(TraverseStmt(I.getCopyExpr())); 1427 } 1428 } 1429 ShouldVisitChildren = false; 1430 }) 1431 1432 DEF_TRAVERSE_DECL(CapturedDecl, { 1433 TRY_TO(TraverseStmt(D->getBody())); 1434 ShouldVisitChildren = false; 1435 }) 1436 1437 DEF_TRAVERSE_DECL(EmptyDecl, {}) 1438 1439 DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, { 1440 TRY_TO(TraverseStmt(D->getTemporaryExpr())); 1441 }) 1442 1443 DEF_TRAVERSE_DECL(FileScopeAsmDecl, 1444 { TRY_TO(TraverseStmt(D->getAsmString())); }) 1445 1446 DEF_TRAVERSE_DECL(ImportDecl, {}) 1447 1448 DEF_TRAVERSE_DECL(FriendDecl, { 1449 // Friend is either decl or a type. 1450 if (D->getFriendType()) 1451 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1452 else 1453 TRY_TO(TraverseDecl(D->getFriendDecl())); 1454 }) 1455 1456 DEF_TRAVERSE_DECL(FriendTemplateDecl, { 1457 if (D->getFriendType()) 1458 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1459 else 1460 TRY_TO(TraverseDecl(D->getFriendDecl())); 1461 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) { 1462 TemplateParameterList *TPL = D->getTemplateParameterList(I); 1463 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end(); 1464 ITPL != ETPL; ++ITPL) { 1465 TRY_TO(TraverseDecl(*ITPL)); 1466 } 1467 } 1468 }) 1469 1470 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, { 1471 TRY_TO(TraverseDecl(D->getSpecialization())); 1472 1473 if (D->hasExplicitTemplateArgs()) { 1474 TRY_TO(TraverseTemplateArgumentLocsHelper( 1475 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1476 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1477 } 1478 }) 1479 1480 DEF_TRAVERSE_DECL(LinkageSpecDecl, {}) 1481 1482 DEF_TRAVERSE_DECL(ExportDecl, {}) 1483 1484 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this 1485 }) 1486 1487 DEF_TRAVERSE_DECL(StaticAssertDecl, { 1488 TRY_TO(TraverseStmt(D->getAssertExpr())); 1489 TRY_TO(TraverseStmt(D->getMessage())); 1490 }) 1491 1492 DEF_TRAVERSE_DECL( 1493 TranslationUnitDecl, 1494 {// Code in an unnamed namespace shows up automatically in 1495 // decls_begin()/decls_end(). Thus we don't need to recurse on 1496 // D->getAnonymousNamespace(). 1497 }) 1498 1499 DEF_TRAVERSE_DECL(PragmaCommentDecl, {}) 1500 1501 DEF_TRAVERSE_DECL(PragmaDetectMismatchDecl, {}) 1502 1503 DEF_TRAVERSE_DECL(ExternCContextDecl, {}) 1504 1505 DEF_TRAVERSE_DECL(NamespaceAliasDecl, { 1506 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1507 1508 // We shouldn't traverse an aliased namespace, since it will be 1509 // defined (and, therefore, traversed) somewhere else. 1510 ShouldVisitChildren = false; 1511 }) 1512 1513 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl. 1514 }) 1515 1516 DEF_TRAVERSE_DECL( 1517 NamespaceDecl, 1518 {// Code in an unnamed namespace shows up automatically in 1519 // decls_begin()/decls_end(). Thus we don't need to recurse on 1520 // D->getAnonymousNamespace(). 1521 }) 1522 1523 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement 1524 }) 1525 1526 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement 1527 if (ObjCTypeParamList *typeParamList = D->getTypeParamList()) { 1528 for (auto typeParam : *typeParamList) { 1529 TRY_TO(TraverseObjCTypeParamDecl(typeParam)); 1530 } 1531 } 1532 }) 1533 1534 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement 1535 }) 1536 1537 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement 1538 }) 1539 1540 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement 1541 if (ObjCTypeParamList *typeParamList = D->getTypeParamListAsWritten()) { 1542 for (auto typeParam : *typeParamList) { 1543 TRY_TO(TraverseObjCTypeParamDecl(typeParam)); 1544 } 1545 } 1546 1547 if (TypeSourceInfo *superTInfo = D->getSuperClassTInfo()) { 1548 TRY_TO(TraverseTypeLoc(superTInfo->getTypeLoc())); 1549 } 1550 }) 1551 1552 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement 1553 }) 1554 1555 DEF_TRAVERSE_DECL(ObjCMethodDecl, { 1556 if (D->getReturnTypeSourceInfo()) { 1557 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc())); 1558 } 1559 for (ParmVarDecl *Parameter : D->parameters()) { 1560 TRY_TO(TraverseDecl(Parameter)); 1561 } 1562 if (D->isThisDeclarationADefinition()) { 1563 TRY_TO(TraverseStmt(D->getBody())); 1564 } 1565 ShouldVisitChildren = false; 1566 }) 1567 1568 DEF_TRAVERSE_DECL(ObjCTypeParamDecl, { 1569 if (D->hasExplicitBound()) { 1570 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1571 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1572 // declaring the type alias, not something that was written in the 1573 // source. 1574 } 1575 }) 1576 1577 DEF_TRAVERSE_DECL(ObjCPropertyDecl, { 1578 if (D->getTypeSourceInfo()) 1579 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1580 else 1581 TRY_TO(TraverseType(D->getType())); 1582 ShouldVisitChildren = false; 1583 }) 1584 1585 DEF_TRAVERSE_DECL(UsingDecl, { 1586 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1587 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1588 }) 1589 1590 DEF_TRAVERSE_DECL(UsingPackDecl, {}) 1591 1592 DEF_TRAVERSE_DECL(UsingDirectiveDecl, { 1593 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1594 }) 1595 1596 DEF_TRAVERSE_DECL(UsingShadowDecl, {}) 1597 1598 DEF_TRAVERSE_DECL(ConstructorUsingShadowDecl, {}) 1599 1600 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, { 1601 for (auto *I : D->varlists()) { 1602 TRY_TO(TraverseStmt(I)); 1603 } 1604 }) 1605 1606 DEF_TRAVERSE_DECL(OMPRequiresDecl, { 1607 for (auto *C : D->clauselists()) { 1608 TRY_TO(TraverseOMPClause(C)); 1609 } 1610 }) 1611 1612 DEF_TRAVERSE_DECL(OMPDeclareReductionDecl, { 1613 TRY_TO(TraverseStmt(D->getCombiner())); 1614 if (auto *Initializer = D->getInitializer()) 1615 TRY_TO(TraverseStmt(Initializer)); 1616 TRY_TO(TraverseType(D->getType())); 1617 return true; 1618 }) 1619 1620 DEF_TRAVERSE_DECL(OMPDeclareMapperDecl, { 1621 for (auto *C : D->clauselists()) 1622 TRY_TO(TraverseOMPClause(C)); 1623 TRY_TO(TraverseType(D->getType())); 1624 return true; 1625 }) 1626 1627 DEF_TRAVERSE_DECL(OMPCapturedExprDecl, { TRY_TO(TraverseVarHelper(D)); }) 1628 1629 DEF_TRAVERSE_DECL(OMPAllocateDecl, { 1630 for (auto *I : D->varlists()) 1631 TRY_TO(TraverseStmt(I)); 1632 for (auto *C : D->clauselists()) 1633 TRY_TO(TraverseOMPClause(C)); 1634 }) 1635 1636 // A helper method for TemplateDecl's children. 1637 template <typename Derived> 1638 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper( 1639 TemplateParameterList *TPL) { 1640 if (TPL) { 1641 for (NamedDecl *D : *TPL) { 1642 TRY_TO(TraverseDecl(D)); 1643 } 1644 if (Expr *RequiresClause = TPL->getRequiresClause()) { 1645 TRY_TO(TraverseStmt(RequiresClause)); 1646 } 1647 } 1648 return true; 1649 } 1650 1651 template <typename Derived> 1652 template <typename T> 1653 bool RecursiveASTVisitor<Derived>::TraverseDeclTemplateParameterLists(T *D) { 1654 for (unsigned i = 0; i < D->getNumTemplateParameterLists(); i++) { 1655 TemplateParameterList *TPL = D->getTemplateParameterList(i); 1656 TraverseTemplateParameterListHelper(TPL); 1657 } 1658 return true; 1659 } 1660 1661 template <typename Derived> 1662 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations( 1663 ClassTemplateDecl *D) { 1664 for (auto *SD : D->specializations()) { 1665 for (auto *RD : SD->redecls()) { 1666 // We don't want to visit injected-class-names in this traversal. 1667 if (cast<CXXRecordDecl>(RD)->isInjectedClassName()) 1668 continue; 1669 1670 switch ( 1671 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1672 // Visit the implicit instantiations with the requested pattern. 1673 case TSK_Undeclared: 1674 case TSK_ImplicitInstantiation: 1675 TRY_TO(TraverseDecl(RD)); 1676 break; 1677 1678 // We don't need to do anything on an explicit instantiation 1679 // or explicit specialization because there will be an explicit 1680 // node for it elsewhere. 1681 case TSK_ExplicitInstantiationDeclaration: 1682 case TSK_ExplicitInstantiationDefinition: 1683 case TSK_ExplicitSpecialization: 1684 break; 1685 } 1686 } 1687 } 1688 1689 return true; 1690 } 1691 1692 template <typename Derived> 1693 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations( 1694 VarTemplateDecl *D) { 1695 for (auto *SD : D->specializations()) { 1696 for (auto *RD : SD->redecls()) { 1697 switch ( 1698 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1699 case TSK_Undeclared: 1700 case TSK_ImplicitInstantiation: 1701 TRY_TO(TraverseDecl(RD)); 1702 break; 1703 1704 case TSK_ExplicitInstantiationDeclaration: 1705 case TSK_ExplicitInstantiationDefinition: 1706 case TSK_ExplicitSpecialization: 1707 break; 1708 } 1709 } 1710 } 1711 1712 return true; 1713 } 1714 1715 // A helper method for traversing the instantiations of a 1716 // function while skipping its specializations. 1717 template <typename Derived> 1718 bool RecursiveASTVisitor<Derived>::TraverseTemplateInstantiations( 1719 FunctionTemplateDecl *D) { 1720 for (auto *FD : D->specializations()) { 1721 for (auto *RD : FD->redecls()) { 1722 switch (RD->getTemplateSpecializationKind()) { 1723 case TSK_Undeclared: 1724 case TSK_ImplicitInstantiation: 1725 // We don't know what kind of FunctionDecl this is. 1726 TRY_TO(TraverseDecl(RD)); 1727 break; 1728 1729 // FIXME: For now traverse explicit instantiations here. Change that 1730 // once they are represented as dedicated nodes in the AST. 1731 case TSK_ExplicitInstantiationDeclaration: 1732 case TSK_ExplicitInstantiationDefinition: 1733 TRY_TO(TraverseDecl(RD)); 1734 break; 1735 1736 case TSK_ExplicitSpecialization: 1737 break; 1738 } 1739 } 1740 } 1741 1742 return true; 1743 } 1744 1745 // This macro unifies the traversal of class, variable and function 1746 // template declarations. 1747 #define DEF_TRAVERSE_TMPL_DECL(TMPLDECLKIND) \ 1748 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateDecl, { \ 1749 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); \ 1750 TRY_TO(TraverseDecl(D->getTemplatedDecl())); \ 1751 \ 1752 /* By default, we do not traverse the instantiations of \ 1753 class templates since they do not appear in the user code. The \ 1754 following code optionally traverses them. \ 1755 \ 1756 We only traverse the class instantiations when we see the canonical \ 1757 declaration of the template, to ensure we only visit them once. */ \ 1758 if (getDerived().shouldVisitTemplateInstantiations() && \ 1759 D == D->getCanonicalDecl()) \ 1760 TRY_TO(TraverseTemplateInstantiations(D)); \ 1761 \ 1762 /* Note that getInstantiatedFromMemberTemplate() is just a link \ 1763 from a template instantiation back to the template from which \ 1764 it was instantiated, and thus should not be traversed. */ \ 1765 }) 1766 1767 DEF_TRAVERSE_TMPL_DECL(Class) 1768 DEF_TRAVERSE_TMPL_DECL(Var) 1769 DEF_TRAVERSE_TMPL_DECL(Function) 1770 1771 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, { 1772 // D is the "T" in something like 1773 // template <template <typename> class T> class container { }; 1774 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1775 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 1776 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument())); 1777 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1778 }) 1779 1780 DEF_TRAVERSE_DECL(BuiltinTemplateDecl, { 1781 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1782 }) 1783 1784 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, { 1785 // D is the "T" in something like "template<typename T> class vector;" 1786 if (D->getTypeForDecl()) 1787 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1788 if (const auto *TC = D->getTypeConstraint()) 1789 TRY_TO(TraverseConceptReference(*TC)); 1790 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 1791 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc())); 1792 }) 1793 1794 DEF_TRAVERSE_DECL(TypedefDecl, { 1795 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1796 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1797 // declaring the typedef, not something that was written in the 1798 // source. 1799 }) 1800 1801 DEF_TRAVERSE_DECL(TypeAliasDecl, { 1802 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1803 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1804 // declaring the type alias, not something that was written in the 1805 // source. 1806 }) 1807 1808 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, { 1809 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1810 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1811 }) 1812 1813 DEF_TRAVERSE_DECL(ConceptDecl, { 1814 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1815 TRY_TO(TraverseStmt(D->getConstraintExpr())); 1816 }) 1817 1818 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, { 1819 // A dependent using declaration which was marked with 'typename'. 1820 // template<class T> class A : public B<T> { using typename B<T>::foo; }; 1821 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1822 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1823 // declaring the type, not something that was written in the 1824 // source. 1825 }) 1826 1827 DEF_TRAVERSE_DECL(EnumDecl, { 1828 TRY_TO(TraverseDeclTemplateParameterLists(D)); 1829 1830 if (D->getTypeForDecl()) 1831 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1832 1833 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1834 // The enumerators are already traversed by 1835 // decls_begin()/decls_end(). 1836 }) 1837 1838 // Helper methods for RecordDecl and its children. 1839 template <typename Derived> 1840 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) { 1841 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1842 // declaring the type, not something that was written in the source. 1843 1844 TRY_TO(TraverseDeclTemplateParameterLists(D)); 1845 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1846 return true; 1847 } 1848 1849 template <typename Derived> 1850 bool RecursiveASTVisitor<Derived>::TraverseCXXBaseSpecifier( 1851 const CXXBaseSpecifier &Base) { 1852 TRY_TO(TraverseTypeLoc(Base.getTypeSourceInfo()->getTypeLoc())); 1853 return true; 1854 } 1855 1856 template <typename Derived> 1857 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) { 1858 if (!TraverseRecordHelper(D)) 1859 return false; 1860 if (D->isCompleteDefinition()) { 1861 for (const auto &I : D->bases()) { 1862 TRY_TO(TraverseCXXBaseSpecifier(I)); 1863 } 1864 // We don't traverse the friends or the conversions, as they are 1865 // already in decls_begin()/decls_end(). 1866 } 1867 return true; 1868 } 1869 1870 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) 1871 1872 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) 1873 1874 #define DEF_TRAVERSE_TMPL_SPEC_DECL(TMPLDECLKIND) \ 1875 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplateSpecializationDecl, { \ 1876 /* For implicit instantiations ("set<int> x;"), we don't want to \ 1877 recurse at all, since the instatiated template isn't written in \ 1878 the source code anywhere. (Note the instatiated *type* -- \ 1879 set<int> -- is written, and will still get a callback of \ 1880 TemplateSpecializationType). For explicit instantiations \ 1881 ("template set<int>;"), we do need a callback, since this \ 1882 is the only callback that's made for this instantiation. \ 1883 We use getTypeAsWritten() to distinguish. */ \ 1884 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) \ 1885 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); \ 1886 \ 1887 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); \ 1888 if (!getDerived().shouldVisitTemplateInstantiations() && \ 1889 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) \ 1890 /* Returning from here skips traversing the \ 1891 declaration context of the *TemplateSpecializationDecl \ 1892 (embedded in the DEF_TRAVERSE_DECL() macro) \ 1893 which contains the instantiated members of the template. */ \ 1894 return true; \ 1895 }) 1896 1897 DEF_TRAVERSE_TMPL_SPEC_DECL(Class) 1898 DEF_TRAVERSE_TMPL_SPEC_DECL(Var) 1899 1900 template <typename Derived> 1901 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper( 1902 const TemplateArgumentLoc *TAL, unsigned Count) { 1903 for (unsigned I = 0; I < Count; ++I) { 1904 TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); 1905 } 1906 return true; 1907 } 1908 1909 #define DEF_TRAVERSE_TMPL_PART_SPEC_DECL(TMPLDECLKIND, DECLKIND) \ 1910 DEF_TRAVERSE_DECL(TMPLDECLKIND##TemplatePartialSpecializationDecl, { \ 1911 /* The partial specialization. */ \ 1912 if (TemplateParameterList *TPL = D->getTemplateParameters()) { \ 1913 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); \ 1914 I != E; ++I) { \ 1915 TRY_TO(TraverseDecl(*I)); \ 1916 } \ 1917 } \ 1918 /* The args that remains unspecialized. */ \ 1919 TRY_TO(TraverseTemplateArgumentLocsHelper( \ 1920 D->getTemplateArgsAsWritten()->getTemplateArgs(), \ 1921 D->getTemplateArgsAsWritten()->NumTemplateArgs)); \ 1922 \ 1923 /* Don't need the *TemplatePartialSpecializationHelper, even \ 1924 though that's our parent class -- we already visit all the \ 1925 template args here. */ \ 1926 TRY_TO(Traverse##DECLKIND##Helper(D)); \ 1927 \ 1928 /* Instantiations will have been visited with the primary template. */ \ 1929 }) 1930 1931 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Class, CXXRecord) 1932 DEF_TRAVERSE_TMPL_PART_SPEC_DECL(Var, Var) 1933 1934 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); }) 1935 1936 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, { 1937 // Like UnresolvedUsingTypenameDecl, but without the 'typename': 1938 // template <class T> Class A : public Base<T> { using Base<T>::foo; }; 1939 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1940 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1941 }) 1942 1943 DEF_TRAVERSE_DECL(IndirectFieldDecl, {}) 1944 1945 template <typename Derived> 1946 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) { 1947 TRY_TO(TraverseDeclTemplateParameterLists(D)); 1948 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1949 if (D->getTypeSourceInfo()) 1950 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1951 else 1952 TRY_TO(TraverseType(D->getType())); 1953 return true; 1954 } 1955 1956 DEF_TRAVERSE_DECL(DecompositionDecl, { 1957 TRY_TO(TraverseVarHelper(D)); 1958 for (auto *Binding : D->bindings()) { 1959 TRY_TO(TraverseDecl(Binding)); 1960 } 1961 }) 1962 1963 DEF_TRAVERSE_DECL(BindingDecl, { 1964 if (getDerived().shouldVisitImplicitCode()) 1965 TRY_TO(TraverseStmt(D->getBinding())); 1966 }) 1967 1968 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); }) 1969 1970 DEF_TRAVERSE_DECL(MSGuidDecl, {}) 1971 1972 DEF_TRAVERSE_DECL(FieldDecl, { 1973 TRY_TO(TraverseDeclaratorHelper(D)); 1974 if (D->isBitField()) 1975 TRY_TO(TraverseStmt(D->getBitWidth())); 1976 else if (D->hasInClassInitializer()) 1977 TRY_TO(TraverseStmt(D->getInClassInitializer())); 1978 }) 1979 1980 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, { 1981 TRY_TO(TraverseDeclaratorHelper(D)); 1982 if (D->isBitField()) 1983 TRY_TO(TraverseStmt(D->getBitWidth())); 1984 // FIXME: implement the rest. 1985 }) 1986 1987 DEF_TRAVERSE_DECL(ObjCIvarDecl, { 1988 TRY_TO(TraverseDeclaratorHelper(D)); 1989 if (D->isBitField()) 1990 TRY_TO(TraverseStmt(D->getBitWidth())); 1991 // FIXME: implement the rest. 1992 }) 1993 1994 template <typename Derived> 1995 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) { 1996 TRY_TO(TraverseDeclTemplateParameterLists(D)); 1997 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1998 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1999 2000 // If we're an explicit template specialization, iterate over the 2001 // template args that were explicitly specified. If we were doing 2002 // this in typing order, we'd do it between the return type and 2003 // the function args, but both are handled by the FunctionTypeLoc 2004 // above, so we have to choose one side. I've decided to do before. 2005 if (const FunctionTemplateSpecializationInfo *FTSI = 2006 D->getTemplateSpecializationInfo()) { 2007 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared && 2008 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { 2009 // A specialization might not have explicit template arguments if it has 2010 // a templated return type and concrete arguments. 2011 if (const ASTTemplateArgumentListInfo *TALI = 2012 FTSI->TemplateArgumentsAsWritten) { 2013 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(), 2014 TALI->NumTemplateArgs)); 2015 } 2016 } 2017 } 2018 2019 // Visit the function type itself, which can be either 2020 // FunctionNoProtoType or FunctionProtoType, or a typedef. This 2021 // also covers the return type and the function parameters, 2022 // including exception specifications. 2023 if (TypeSourceInfo *TSI = D->getTypeSourceInfo()) { 2024 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 2025 } else if (getDerived().shouldVisitImplicitCode()) { 2026 // Visit parameter variable declarations of the implicit function 2027 // if the traverser is visiting implicit code. Parameter variable 2028 // declarations do not have valid TypeSourceInfo, so to visit them 2029 // we need to traverse the declarations explicitly. 2030 for (ParmVarDecl *Parameter : D->parameters()) { 2031 TRY_TO(TraverseDecl(Parameter)); 2032 } 2033 } 2034 2035 // Visit the trailing requires clause, if any. 2036 if (Expr *TrailingRequiresClause = D->getTrailingRequiresClause()) { 2037 TRY_TO(TraverseStmt(TrailingRequiresClause)); 2038 } 2039 2040 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) { 2041 // Constructor initializers. 2042 for (auto *I : Ctor->inits()) { 2043 if (I->isWritten() || getDerived().shouldVisitImplicitCode()) 2044 TRY_TO(TraverseConstructorInitializer(I)); 2045 } 2046 } 2047 2048 bool VisitBody = 2049 D->isThisDeclarationADefinition() && 2050 // Don't visit the function body if the function definition is generated 2051 // by clang. 2052 (!D->isDefaulted() || getDerived().shouldVisitImplicitCode()); 2053 2054 if (VisitBody) { 2055 TRY_TO(TraverseStmt(D->getBody())); // Function body. 2056 } 2057 return true; 2058 } 2059 2060 DEF_TRAVERSE_DECL(FunctionDecl, { 2061 // We skip decls_begin/decls_end, which are already covered by 2062 // TraverseFunctionHelper(). 2063 ShouldVisitChildren = false; 2064 ReturnValue = TraverseFunctionHelper(D); 2065 }) 2066 2067 DEF_TRAVERSE_DECL(CXXDeductionGuideDecl, { 2068 // We skip decls_begin/decls_end, which are already covered by 2069 // TraverseFunctionHelper(). 2070 ShouldVisitChildren = false; 2071 ReturnValue = TraverseFunctionHelper(D); 2072 }) 2073 2074 DEF_TRAVERSE_DECL(CXXMethodDecl, { 2075 // We skip decls_begin/decls_end, which are already covered by 2076 // TraverseFunctionHelper(). 2077 ShouldVisitChildren = false; 2078 ReturnValue = TraverseFunctionHelper(D); 2079 }) 2080 2081 DEF_TRAVERSE_DECL(CXXConstructorDecl, { 2082 // We skip decls_begin/decls_end, which are already covered by 2083 // TraverseFunctionHelper(). 2084 ShouldVisitChildren = false; 2085 ReturnValue = TraverseFunctionHelper(D); 2086 }) 2087 2088 // CXXConversionDecl is the declaration of a type conversion operator. 2089 // It's not a cast expression. 2090 DEF_TRAVERSE_DECL(CXXConversionDecl, { 2091 // We skip decls_begin/decls_end, which are already covered by 2092 // TraverseFunctionHelper(). 2093 ShouldVisitChildren = false; 2094 ReturnValue = TraverseFunctionHelper(D); 2095 }) 2096 2097 DEF_TRAVERSE_DECL(CXXDestructorDecl, { 2098 // We skip decls_begin/decls_end, which are already covered by 2099 // TraverseFunctionHelper(). 2100 ShouldVisitChildren = false; 2101 ReturnValue = TraverseFunctionHelper(D); 2102 }) 2103 2104 template <typename Derived> 2105 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) { 2106 TRY_TO(TraverseDeclaratorHelper(D)); 2107 // Default params are taken care of when we traverse the ParmVarDecl. 2108 if (!isa<ParmVarDecl>(D) && 2109 (!D->isCXXForRangeDecl() || getDerived().shouldVisitImplicitCode())) 2110 TRY_TO(TraverseStmt(D->getInit())); 2111 return true; 2112 } 2113 2114 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); }) 2115 2116 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); }) 2117 2118 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, { 2119 // A non-type template parameter, e.g. "S" in template<int S> class Foo ... 2120 TRY_TO(TraverseDeclaratorHelper(D)); 2121 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) 2122 TRY_TO(TraverseStmt(D->getDefaultArgument())); 2123 }) 2124 2125 DEF_TRAVERSE_DECL(ParmVarDecl, { 2126 TRY_TO(TraverseVarHelper(D)); 2127 2128 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() && 2129 !D->hasUnparsedDefaultArg()) 2130 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg())); 2131 2132 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() && 2133 !D->hasUnparsedDefaultArg()) 2134 TRY_TO(TraverseStmt(D->getDefaultArg())); 2135 }) 2136 2137 DEF_TRAVERSE_DECL(RequiresExprBodyDecl, {}) 2138 2139 #undef DEF_TRAVERSE_DECL 2140 2141 // ----------------- Stmt traversal ----------------- 2142 // 2143 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating 2144 // over the children defined in children() (every stmt defines these, 2145 // though sometimes the range is empty). Each individual Traverse* 2146 // method only needs to worry about children other than those. To see 2147 // what children() does for a given class, see, e.g., 2148 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html 2149 2150 // This macro makes available a variable S, the passed-in stmt. 2151 #define DEF_TRAVERSE_STMT(STMT, CODE) \ 2152 template <typename Derived> \ 2153 bool RecursiveASTVisitor<Derived>::Traverse##STMT( \ 2154 STMT *S, DataRecursionQueue *Queue) { \ 2155 bool ShouldVisitChildren = true; \ 2156 bool ReturnValue = true; \ 2157 if (!getDerived().shouldTraversePostOrder()) \ 2158 TRY_TO(WalkUpFrom##STMT(S)); \ 2159 { CODE; } \ 2160 if (ShouldVisitChildren) { \ 2161 for (Stmt * SubStmt : getDerived().getStmtChildren(S)) { \ 2162 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); \ 2163 } \ 2164 } \ 2165 /* Call WalkUpFrom if TRY_TO_TRAVERSE_OR_ENQUEUE_STMT has traversed the \ 2166 * children already. If TRY_TO_TRAVERSE_OR_ENQUEUE_STMT only enqueued the \ 2167 * children, PostVisitStmt will call WalkUpFrom after we are done visiting \ 2168 * children. */ \ 2169 if (!Queue && ReturnValue && getDerived().shouldTraversePostOrder()) { \ 2170 TRY_TO(WalkUpFrom##STMT(S)); \ 2171 } \ 2172 return ReturnValue; \ 2173 } 2174 2175 DEF_TRAVERSE_STMT(GCCAsmStmt, { 2176 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getAsmString()); 2177 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) { 2178 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInputConstraintLiteral(I)); 2179 } 2180 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) { 2181 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOutputConstraintLiteral(I)); 2182 } 2183 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) { 2184 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getClobberStringLiteral(I)); 2185 } 2186 // children() iterates over inputExpr and outputExpr. 2187 }) 2188 2189 DEF_TRAVERSE_STMT( 2190 MSAsmStmt, 2191 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once 2192 // added this needs to be implemented. 2193 }) 2194 2195 DEF_TRAVERSE_STMT(CXXCatchStmt, { 2196 TRY_TO(TraverseDecl(S->getExceptionDecl())); 2197 // children() iterates over the handler block. 2198 }) 2199 2200 DEF_TRAVERSE_STMT(DeclStmt, { 2201 for (auto *I : S->decls()) { 2202 TRY_TO(TraverseDecl(I)); 2203 } 2204 // Suppress the default iteration over children() by 2205 // returning. Here's why: A DeclStmt looks like 'type var [= 2206 // initializer]'. The decls above already traverse over the 2207 // initializers, so we don't have to do it again (which 2208 // children() would do). 2209 ShouldVisitChildren = false; 2210 }) 2211 2212 // These non-expr stmts (most of them), do not need any action except 2213 // iterating over the children. 2214 DEF_TRAVERSE_STMT(BreakStmt, {}) 2215 DEF_TRAVERSE_STMT(CXXTryStmt, {}) 2216 DEF_TRAVERSE_STMT(CaseStmt, {}) 2217 DEF_TRAVERSE_STMT(CompoundStmt, {}) 2218 DEF_TRAVERSE_STMT(ContinueStmt, {}) 2219 DEF_TRAVERSE_STMT(DefaultStmt, {}) 2220 DEF_TRAVERSE_STMT(DoStmt, {}) 2221 DEF_TRAVERSE_STMT(ForStmt, {}) 2222 DEF_TRAVERSE_STMT(GotoStmt, {}) 2223 DEF_TRAVERSE_STMT(IfStmt, {}) 2224 DEF_TRAVERSE_STMT(IndirectGotoStmt, {}) 2225 DEF_TRAVERSE_STMT(LabelStmt, {}) 2226 DEF_TRAVERSE_STMT(AttributedStmt, {}) 2227 DEF_TRAVERSE_STMT(NullStmt, {}) 2228 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {}) 2229 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {}) 2230 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {}) 2231 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {}) 2232 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {}) 2233 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {}) 2234 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {}) 2235 2236 DEF_TRAVERSE_STMT(CXXForRangeStmt, { 2237 if (!getDerived().shouldVisitImplicitCode()) { 2238 if (S->getInit()) 2239 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getInit()); 2240 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getLoopVarStmt()); 2241 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getRangeInit()); 2242 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody()); 2243 // Visit everything else only if shouldVisitImplicitCode(). 2244 ShouldVisitChildren = false; 2245 } 2246 }) 2247 2248 DEF_TRAVERSE_STMT(MSDependentExistsStmt, { 2249 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2250 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 2251 }) 2252 2253 DEF_TRAVERSE_STMT(ReturnStmt, {}) 2254 DEF_TRAVERSE_STMT(SwitchStmt, {}) 2255 DEF_TRAVERSE_STMT(WhileStmt, {}) 2256 2257 DEF_TRAVERSE_STMT(ConstantExpr, {}) 2258 2259 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, { 2260 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2261 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 2262 if (S->hasExplicitTemplateArgs()) { 2263 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2264 S->getNumTemplateArgs())); 2265 } 2266 }) 2267 2268 DEF_TRAVERSE_STMT(DeclRefExpr, { 2269 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2270 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 2271 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2272 S->getNumTemplateArgs())); 2273 }) 2274 2275 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, { 2276 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2277 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 2278 if (S->hasExplicitTemplateArgs()) { 2279 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2280 S->getNumTemplateArgs())); 2281 } 2282 }) 2283 2284 DEF_TRAVERSE_STMT(MemberExpr, { 2285 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2286 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 2287 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2288 S->getNumTemplateArgs())); 2289 }) 2290 2291 DEF_TRAVERSE_STMT( 2292 ImplicitCastExpr, 2293 {// We don't traverse the cast type, as it's not written in the 2294 // source code. 2295 }) 2296 2297 DEF_TRAVERSE_STMT(CStyleCastExpr, { 2298 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2299 }) 2300 2301 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, { 2302 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2303 }) 2304 2305 DEF_TRAVERSE_STMT(CXXAddrspaceCastExpr, { 2306 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2307 }) 2308 2309 DEF_TRAVERSE_STMT(CXXConstCastExpr, { 2310 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2311 }) 2312 2313 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, { 2314 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2315 }) 2316 2317 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, { 2318 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2319 }) 2320 2321 DEF_TRAVERSE_STMT(CXXStaticCastExpr, { 2322 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2323 }) 2324 2325 DEF_TRAVERSE_STMT(BuiltinBitCastExpr, { 2326 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2327 }) 2328 2329 template <typename Derived> 2330 bool RecursiveASTVisitor<Derived>::TraverseSynOrSemInitListExpr( 2331 InitListExpr *S, DataRecursionQueue *Queue) { 2332 if (S) { 2333 // Skip this if we traverse postorder. We will visit it later 2334 // in PostVisitStmt. 2335 if (!getDerived().shouldTraversePostOrder()) 2336 TRY_TO(WalkUpFromInitListExpr(S)); 2337 2338 // All we need are the default actions. FIXME: use a helper function. 2339 for (Stmt *SubStmt : S->children()) { 2340 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(SubStmt); 2341 } 2342 2343 if (!Queue && getDerived().shouldTraversePostOrder()) 2344 TRY_TO(WalkUpFromInitListExpr(S)); 2345 } 2346 return true; 2347 } 2348 2349 template<typename Derived> 2350 bool RecursiveASTVisitor<Derived>::TraverseConceptReference( 2351 const ConceptReference &C) { 2352 TRY_TO(TraverseNestedNameSpecifierLoc(C.getNestedNameSpecifierLoc())); 2353 TRY_TO(TraverseDeclarationNameInfo(C.getConceptNameInfo())); 2354 if (C.hasExplicitTemplateArgs()) 2355 TRY_TO(TraverseTemplateArgumentLocsHelper( 2356 C.getTemplateArgsAsWritten()->getTemplateArgs(), 2357 C.getTemplateArgsAsWritten()->NumTemplateArgs)); 2358 return true; 2359 } 2360 2361 // If shouldVisitImplicitCode() returns false, this method traverses only the 2362 // syntactic form of InitListExpr. 2363 // If shouldVisitImplicitCode() return true, this method is called once for 2364 // each pair of syntactic and semantic InitListExpr, and it traverses the 2365 // subtrees defined by the two forms. This may cause some of the children to be 2366 // visited twice, if they appear both in the syntactic and the semantic form. 2367 // 2368 // There is no guarantee about which form \p S takes when this method is called. 2369 template <typename Derived> 2370 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr( 2371 InitListExpr *S, DataRecursionQueue *Queue) { 2372 if (S->isSemanticForm() && S->isSyntacticForm()) { 2373 // `S` does not have alternative forms, traverse only once. 2374 TRY_TO(TraverseSynOrSemInitListExpr(S, Queue)); 2375 return true; 2376 } 2377 TRY_TO(TraverseSynOrSemInitListExpr( 2378 S->isSemanticForm() ? S->getSyntacticForm() : S, Queue)); 2379 if (getDerived().shouldVisitImplicitCode()) { 2380 // Only visit the semantic form if the clients are interested in implicit 2381 // compiler-generated. 2382 TRY_TO(TraverseSynOrSemInitListExpr( 2383 S->isSemanticForm() ? S : S->getSemanticForm(), Queue)); 2384 } 2385 return true; 2386 } 2387 2388 // GenericSelectionExpr is a special case because the types and expressions 2389 // are interleaved. We also need to watch out for null types (default 2390 // generic associations). 2391 DEF_TRAVERSE_STMT(GenericSelectionExpr, { 2392 TRY_TO(TraverseStmt(S->getControllingExpr())); 2393 for (const GenericSelectionExpr::Association Assoc : S->associations()) { 2394 if (TypeSourceInfo *TSI = Assoc.getTypeSourceInfo()) 2395 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 2396 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(Assoc.getAssociationExpr()); 2397 } 2398 ShouldVisitChildren = false; 2399 }) 2400 2401 // PseudoObjectExpr is a special case because of the weirdness with 2402 // syntactic expressions and opaque values. 2403 DEF_TRAVERSE_STMT(PseudoObjectExpr, { 2404 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getSyntacticForm()); 2405 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(), 2406 e = S->semantics_end(); 2407 i != e; ++i) { 2408 Expr *sub = *i; 2409 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub)) 2410 sub = OVE->getSourceExpr(); 2411 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(sub); 2412 } 2413 ShouldVisitChildren = false; 2414 }) 2415 2416 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, { 2417 // This is called for code like 'return T()' where T is a built-in 2418 // (i.e. non-class) type. 2419 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2420 }) 2421 2422 DEF_TRAVERSE_STMT(CXXNewExpr, { 2423 // The child-iterator will pick up the other arguments. 2424 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc())); 2425 }) 2426 2427 DEF_TRAVERSE_STMT(OffsetOfExpr, { 2428 // The child-iterator will pick up the expression representing 2429 // the field. 2430 // FIMXE: for code like offsetof(Foo, a.b.c), should we get 2431 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c? 2432 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2433 }) 2434 2435 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, { 2436 // The child-iterator will pick up the arg if it's an expression, 2437 // but not if it's a type. 2438 if (S->isArgumentType()) 2439 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc())); 2440 }) 2441 2442 DEF_TRAVERSE_STMT(CXXTypeidExpr, { 2443 // The child-iterator will pick up the arg if it's an expression, 2444 // but not if it's a type. 2445 if (S->isTypeOperand()) 2446 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2447 }) 2448 2449 DEF_TRAVERSE_STMT(MSPropertyRefExpr, { 2450 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2451 }) 2452 2453 DEF_TRAVERSE_STMT(MSPropertySubscriptExpr, {}) 2454 2455 DEF_TRAVERSE_STMT(CXXUuidofExpr, { 2456 // The child-iterator will pick up the arg if it's an expression, 2457 // but not if it's a type. 2458 if (S->isTypeOperand()) 2459 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2460 }) 2461 2462 DEF_TRAVERSE_STMT(TypeTraitExpr, { 2463 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 2464 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc())); 2465 }) 2466 2467 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, { 2468 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc())); 2469 }) 2470 2471 DEF_TRAVERSE_STMT(ExpressionTraitExpr, 2472 { TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getQueriedExpression()); }) 2473 2474 DEF_TRAVERSE_STMT(VAArgExpr, { 2475 // The child-iterator will pick up the expression argument. 2476 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc())); 2477 }) 2478 2479 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, { 2480 // This is called for code like 'return T()' where T is a class type. 2481 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2482 }) 2483 2484 // Walk only the visible parts of lambda expressions. 2485 DEF_TRAVERSE_STMT(LambdaExpr, { 2486 // Visit the capture list. 2487 for (unsigned I = 0, N = S->capture_size(); I != N; ++I) { 2488 const LambdaCapture *C = S->capture_begin() + I; 2489 if (C->isExplicit() || getDerived().shouldVisitImplicitCode()) { 2490 TRY_TO(TraverseLambdaCapture(S, C, S->capture_init_begin()[I])); 2491 } 2492 } 2493 2494 if (getDerived().shouldVisitImplicitCode()) { 2495 // The implicit model is simple: everything else is in the lambda class. 2496 TRY_TO(TraverseDecl(S->getLambdaClass())); 2497 } else { 2498 // We need to poke around to find the bits that might be explicitly written. 2499 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); 2500 FunctionProtoTypeLoc Proto = TL.getAsAdjusted<FunctionProtoTypeLoc>(); 2501 2502 for (Decl *D : S->getExplicitTemplateParameters()) { 2503 // Visit explicit template parameters. 2504 TRY_TO(TraverseDecl(D)); 2505 } 2506 if (S->hasExplicitParameters()) { 2507 // Visit parameters. 2508 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) 2509 TRY_TO(TraverseDecl(Proto.getParam(I))); 2510 } 2511 if (S->hasExplicitResultType()) 2512 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc())); 2513 2514 auto *T = Proto.getTypePtr(); 2515 for (const auto &E : T->exceptions()) 2516 TRY_TO(TraverseType(E)); 2517 2518 if (Expr *NE = T->getNoexceptExpr()) 2519 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(NE); 2520 2521 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody()); 2522 } 2523 ShouldVisitChildren = false; 2524 }) 2525 2526 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, { 2527 // This is called for code like 'T()', where T is a template argument. 2528 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2529 }) 2530 2531 // These expressions all might take explicit template arguments. 2532 // We traverse those if so. FIXME: implement these. 2533 DEF_TRAVERSE_STMT(CXXConstructExpr, {}) 2534 DEF_TRAVERSE_STMT(CallExpr, {}) 2535 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {}) 2536 2537 // These exprs (most of them), do not need any action except iterating 2538 // over the children. 2539 DEF_TRAVERSE_STMT(AddrLabelExpr, {}) 2540 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {}) 2541 DEF_TRAVERSE_STMT(MatrixSubscriptExpr, {}) 2542 DEF_TRAVERSE_STMT(OMPArraySectionExpr, {}) 2543 DEF_TRAVERSE_STMT(OMPArrayShapingExpr, {}) 2544 DEF_TRAVERSE_STMT(OMPIteratorExpr, {}) 2545 2546 DEF_TRAVERSE_STMT(BlockExpr, { 2547 TRY_TO(TraverseDecl(S->getBlockDecl())); 2548 return true; // no child statements to loop through. 2549 }) 2550 2551 DEF_TRAVERSE_STMT(ChooseExpr, {}) 2552 DEF_TRAVERSE_STMT(CompoundLiteralExpr, { 2553 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2554 }) 2555 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {}) 2556 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {}) 2557 2558 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, { 2559 if (getDerived().shouldVisitImplicitCode()) 2560 TRY_TO(TraverseStmt(S->getExpr())); 2561 }) 2562 2563 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {}) 2564 DEF_TRAVERSE_STMT(CXXDeleteExpr, {}) 2565 DEF_TRAVERSE_STMT(ExprWithCleanups, {}) 2566 DEF_TRAVERSE_STMT(CXXInheritedCtorInitExpr, {}) 2567 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {}) 2568 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {}) 2569 2570 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, { 2571 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2572 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo()) 2573 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc())); 2574 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo()) 2575 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc())); 2576 }) 2577 2578 DEF_TRAVERSE_STMT(CXXThisExpr, {}) 2579 DEF_TRAVERSE_STMT(CXXThrowExpr, {}) 2580 DEF_TRAVERSE_STMT(UserDefinedLiteral, {}) 2581 DEF_TRAVERSE_STMT(DesignatedInitExpr, {}) 2582 DEF_TRAVERSE_STMT(DesignatedInitUpdateExpr, {}) 2583 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {}) 2584 DEF_TRAVERSE_STMT(GNUNullExpr, {}) 2585 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {}) 2586 DEF_TRAVERSE_STMT(NoInitExpr, {}) 2587 DEF_TRAVERSE_STMT(ArrayInitLoopExpr, { 2588 // FIXME: The source expression of the OVE should be listed as 2589 // a child of the ArrayInitLoopExpr. 2590 if (OpaqueValueExpr *OVE = S->getCommonExpr()) 2591 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(OVE->getSourceExpr()); 2592 }) 2593 DEF_TRAVERSE_STMT(ArrayInitIndexExpr, {}) 2594 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {}) 2595 2596 DEF_TRAVERSE_STMT(ObjCEncodeExpr, { 2597 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo()) 2598 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2599 }) 2600 2601 DEF_TRAVERSE_STMT(ObjCIsaExpr, {}) 2602 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {}) 2603 2604 DEF_TRAVERSE_STMT(ObjCMessageExpr, { 2605 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo()) 2606 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2607 }) 2608 2609 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {}) 2610 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {}) 2611 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {}) 2612 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {}) 2613 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {}) 2614 2615 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, { 2616 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2617 }) 2618 2619 DEF_TRAVERSE_STMT(ObjCAvailabilityCheckExpr, {}) 2620 DEF_TRAVERSE_STMT(ParenExpr, {}) 2621 DEF_TRAVERSE_STMT(NoChangeBoundsExpr, {}) 2622 DEF_TRAVERSE_STMT(ParenListExpr, {}) 2623 DEF_TRAVERSE_STMT(PredefinedExpr, {}) 2624 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {}) 2625 DEF_TRAVERSE_STMT(ConvertVectorExpr, {}) 2626 DEF_TRAVERSE_STMT(StmtExpr, {}) 2627 DEF_TRAVERSE_STMT(SourceLocExpr, {}) 2628 2629 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, { 2630 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2631 if (S->hasExplicitTemplateArgs()) { 2632 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2633 S->getNumTemplateArgs())); 2634 } 2635 }) 2636 2637 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, { 2638 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2639 if (S->hasExplicitTemplateArgs()) { 2640 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2641 S->getNumTemplateArgs())); 2642 } 2643 }) 2644 2645 DEF_TRAVERSE_STMT(SEHTryStmt, {}) 2646 DEF_TRAVERSE_STMT(SEHExceptStmt, {}) 2647 DEF_TRAVERSE_STMT(SEHFinallyStmt, {}) 2648 DEF_TRAVERSE_STMT(SEHLeaveStmt, {}) 2649 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); }) 2650 2651 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {}) 2652 DEF_TRAVERSE_STMT(CXXRewrittenBinaryOperator, { 2653 if (!getDerived().shouldVisitImplicitCode()) { 2654 CXXRewrittenBinaryOperator::DecomposedForm Decomposed = 2655 S->getDecomposedForm(); 2656 TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.LHS))); 2657 TRY_TO(TraverseStmt(const_cast<Expr*>(Decomposed.RHS))); 2658 ShouldVisitChildren = false; 2659 } 2660 }) 2661 DEF_TRAVERSE_STMT(OpaqueValueExpr, {}) 2662 DEF_TRAVERSE_STMT(TypoExpr, {}) 2663 DEF_TRAVERSE_STMT(RecoveryExpr, {}) 2664 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {}) 2665 2666 // These operators (all of them) do not need any action except 2667 // iterating over the children. 2668 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {}) 2669 DEF_TRAVERSE_STMT(ConditionalOperator, {}) 2670 DEF_TRAVERSE_STMT(UnaryOperator, {}) 2671 DEF_TRAVERSE_STMT(BinaryOperator, {}) 2672 DEF_TRAVERSE_STMT(CompoundAssignOperator, {}) 2673 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {}) 2674 DEF_TRAVERSE_STMT(PackExpansionExpr, {}) 2675 DEF_TRAVERSE_STMT(SizeOfPackExpr, {}) 2676 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {}) 2677 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {}) 2678 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {}) 2679 DEF_TRAVERSE_STMT(CXXFoldExpr, {}) 2680 DEF_TRAVERSE_STMT(AtomicExpr, {}) 2681 2682 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, { 2683 if (S->getLifetimeExtendedTemporaryDecl()) { 2684 TRY_TO(TraverseLifetimeExtendedTemporaryDecl( 2685 S->getLifetimeExtendedTemporaryDecl())); 2686 ShouldVisitChildren = false; 2687 } 2688 }) 2689 // For coroutines expressions, traverse either the operand 2690 // as written or the implied calls, depending on what the 2691 // derived class requests. 2692 DEF_TRAVERSE_STMT(CoroutineBodyStmt, { 2693 if (!getDerived().shouldVisitImplicitCode()) { 2694 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getBody()); 2695 ShouldVisitChildren = false; 2696 } 2697 }) 2698 DEF_TRAVERSE_STMT(CoreturnStmt, { 2699 if (!getDerived().shouldVisitImplicitCode()) { 2700 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand()); 2701 ShouldVisitChildren = false; 2702 } 2703 }) 2704 DEF_TRAVERSE_STMT(CoawaitExpr, { 2705 if (!getDerived().shouldVisitImplicitCode()) { 2706 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand()); 2707 ShouldVisitChildren = false; 2708 } 2709 }) 2710 DEF_TRAVERSE_STMT(DependentCoawaitExpr, { 2711 if (!getDerived().shouldVisitImplicitCode()) { 2712 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand()); 2713 ShouldVisitChildren = false; 2714 } 2715 }) 2716 DEF_TRAVERSE_STMT(CoyieldExpr, { 2717 if (!getDerived().shouldVisitImplicitCode()) { 2718 TRY_TO_TRAVERSE_OR_ENQUEUE_STMT(S->getOperand()); 2719 ShouldVisitChildren = false; 2720 } 2721 }) 2722 2723 DEF_TRAVERSE_STMT(ConceptSpecializationExpr, { 2724 TRY_TO(TraverseConceptReference(*S)); 2725 }) 2726 2727 DEF_TRAVERSE_STMT(RequiresExpr, { 2728 TRY_TO(TraverseDecl(S->getBody())); 2729 for (ParmVarDecl *Parm : S->getLocalParameters()) 2730 TRY_TO(TraverseDecl(Parm)); 2731 for (concepts::Requirement *Req : S->getRequirements()) 2732 if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) { 2733 if (!TypeReq->isSubstitutionFailure()) 2734 TRY_TO(TraverseTypeLoc(TypeReq->getType()->getTypeLoc())); 2735 } else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) { 2736 if (!ExprReq->isExprSubstitutionFailure()) 2737 TRY_TO(TraverseStmt(ExprReq->getExpr())); 2738 auto &RetReq = ExprReq->getReturnTypeRequirement(); 2739 if (RetReq.isTypeConstraint()) 2740 TRY_TO(TraverseTemplateParameterListHelper( 2741 RetReq.getTypeConstraintTemplateParameterList())); 2742 } else { 2743 auto *NestedReq = cast<concepts::NestedRequirement>(Req); 2744 if (!NestedReq->isSubstitutionFailure()) 2745 TRY_TO(TraverseStmt(NestedReq->getConstraintExpr())); 2746 } 2747 }) 2748 2749 // These literals (all of them) do not need any action. 2750 DEF_TRAVERSE_STMT(IntegerLiteral, {}) 2751 DEF_TRAVERSE_STMT(FixedPointLiteral, {}) 2752 DEF_TRAVERSE_STMT(CharacterLiteral, {}) 2753 DEF_TRAVERSE_STMT(FloatingLiteral, {}) 2754 DEF_TRAVERSE_STMT(ImaginaryLiteral, {}) 2755 DEF_TRAVERSE_STMT(StringLiteral, {}) 2756 DEF_TRAVERSE_STMT(ObjCStringLiteral, {}) 2757 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {}) 2758 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {}) 2759 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {}) 2760 2761 // Traverse OpenCL: AsType, Convert. 2762 DEF_TRAVERSE_STMT(AsTypeExpr, {}) 2763 2764 // OpenMP directives. 2765 template <typename Derived> 2766 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective( 2767 OMPExecutableDirective *S) { 2768 for (auto *C : S->clauses()) { 2769 TRY_TO(TraverseOMPClause(C)); 2770 } 2771 return true; 2772 } 2773 2774 template <typename Derived> 2775 bool 2776 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) { 2777 return TraverseOMPExecutableDirective(S); 2778 } 2779 2780 DEF_TRAVERSE_STMT(OMPParallelDirective, 2781 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2782 2783 DEF_TRAVERSE_STMT(OMPSimdDirective, 2784 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2785 2786 DEF_TRAVERSE_STMT(OMPForDirective, 2787 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2788 2789 DEF_TRAVERSE_STMT(OMPForSimdDirective, 2790 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2791 2792 DEF_TRAVERSE_STMT(OMPSectionsDirective, 2793 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2794 2795 DEF_TRAVERSE_STMT(OMPSectionDirective, 2796 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2797 2798 DEF_TRAVERSE_STMT(OMPSingleDirective, 2799 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2800 2801 DEF_TRAVERSE_STMT(OMPMasterDirective, 2802 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2803 2804 DEF_TRAVERSE_STMT(OMPCriticalDirective, { 2805 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName())); 2806 TRY_TO(TraverseOMPExecutableDirective(S)); 2807 }) 2808 2809 DEF_TRAVERSE_STMT(OMPParallelForDirective, 2810 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2811 2812 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective, 2813 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2814 2815 DEF_TRAVERSE_STMT(OMPParallelMasterDirective, 2816 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2817 2818 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective, 2819 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2820 2821 DEF_TRAVERSE_STMT(OMPTaskDirective, 2822 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2823 2824 DEF_TRAVERSE_STMT(OMPTaskyieldDirective, 2825 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2826 2827 DEF_TRAVERSE_STMT(OMPBarrierDirective, 2828 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2829 2830 DEF_TRAVERSE_STMT(OMPTaskwaitDirective, 2831 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2832 2833 DEF_TRAVERSE_STMT(OMPTaskgroupDirective, 2834 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2835 2836 DEF_TRAVERSE_STMT(OMPCancellationPointDirective, 2837 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2838 2839 DEF_TRAVERSE_STMT(OMPCancelDirective, 2840 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2841 2842 DEF_TRAVERSE_STMT(OMPFlushDirective, 2843 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2844 2845 DEF_TRAVERSE_STMT(OMPDepobjDirective, 2846 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2847 2848 DEF_TRAVERSE_STMT(OMPScanDirective, 2849 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2850 2851 DEF_TRAVERSE_STMT(OMPOrderedDirective, 2852 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2853 2854 DEF_TRAVERSE_STMT(OMPAtomicDirective, 2855 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2856 2857 DEF_TRAVERSE_STMT(OMPTargetDirective, 2858 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2859 2860 DEF_TRAVERSE_STMT(OMPTargetDataDirective, 2861 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2862 2863 DEF_TRAVERSE_STMT(OMPTargetEnterDataDirective, 2864 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2865 2866 DEF_TRAVERSE_STMT(OMPTargetExitDataDirective, 2867 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2868 2869 DEF_TRAVERSE_STMT(OMPTargetParallelDirective, 2870 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2871 2872 DEF_TRAVERSE_STMT(OMPTargetParallelForDirective, 2873 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2874 2875 DEF_TRAVERSE_STMT(OMPTeamsDirective, 2876 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2877 2878 DEF_TRAVERSE_STMT(OMPTargetUpdateDirective, 2879 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2880 2881 DEF_TRAVERSE_STMT(OMPTaskLoopDirective, 2882 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2883 2884 DEF_TRAVERSE_STMT(OMPTaskLoopSimdDirective, 2885 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2886 2887 DEF_TRAVERSE_STMT(OMPMasterTaskLoopDirective, 2888 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2889 2890 DEF_TRAVERSE_STMT(OMPMasterTaskLoopSimdDirective, 2891 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2892 2893 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopDirective, 2894 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2895 2896 DEF_TRAVERSE_STMT(OMPParallelMasterTaskLoopSimdDirective, 2897 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2898 2899 DEF_TRAVERSE_STMT(OMPDistributeDirective, 2900 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2901 2902 DEF_TRAVERSE_STMT(OMPDistributeParallelForDirective, 2903 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2904 2905 DEF_TRAVERSE_STMT(OMPDistributeParallelForSimdDirective, 2906 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2907 2908 DEF_TRAVERSE_STMT(OMPDistributeSimdDirective, 2909 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2910 2911 DEF_TRAVERSE_STMT(OMPTargetParallelForSimdDirective, 2912 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2913 2914 DEF_TRAVERSE_STMT(OMPTargetSimdDirective, 2915 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2916 2917 DEF_TRAVERSE_STMT(OMPTeamsDistributeDirective, 2918 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2919 2920 DEF_TRAVERSE_STMT(OMPTeamsDistributeSimdDirective, 2921 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2922 2923 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForSimdDirective, 2924 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2925 2926 DEF_TRAVERSE_STMT(OMPTeamsDistributeParallelForDirective, 2927 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2928 2929 DEF_TRAVERSE_STMT(OMPTargetTeamsDirective, 2930 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2931 2932 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeDirective, 2933 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2934 2935 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForDirective, 2936 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2937 2938 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeParallelForSimdDirective, 2939 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2940 2941 DEF_TRAVERSE_STMT(OMPTargetTeamsDistributeSimdDirective, 2942 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2943 2944 // OpenMP clauses. 2945 template <typename Derived> 2946 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) { 2947 if (!C) 2948 return true; 2949 switch (C->getClauseKind()) { 2950 #define OMP_CLAUSE_CLASS(Enum, Str, Class) \ 2951 case llvm::omp::Clause::Enum: \ 2952 TRY_TO(Visit##Class(static_cast<Class *>(C))); \ 2953 break; 2954 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 2955 case llvm::omp::Clause::Enum: \ 2956 break; 2957 #include "llvm/Frontend/OpenMP/OMPKinds.def" 2958 default: 2959 break; 2960 } 2961 return true; 2962 } 2963 2964 template <typename Derived> 2965 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPreInit( 2966 OMPClauseWithPreInit *Node) { 2967 TRY_TO(TraverseStmt(Node->getPreInitStmt())); 2968 return true; 2969 } 2970 2971 template <typename Derived> 2972 bool RecursiveASTVisitor<Derived>::VisitOMPClauseWithPostUpdate( 2973 OMPClauseWithPostUpdate *Node) { 2974 TRY_TO(VisitOMPClauseWithPreInit(Node)); 2975 TRY_TO(TraverseStmt(Node->getPostUpdateExpr())); 2976 return true; 2977 } 2978 2979 template <typename Derived> 2980 bool RecursiveASTVisitor<Derived>::VisitOMPAllocatorClause( 2981 OMPAllocatorClause *C) { 2982 TRY_TO(TraverseStmt(C->getAllocator())); 2983 return true; 2984 } 2985 2986 template <typename Derived> 2987 bool RecursiveASTVisitor<Derived>::VisitOMPAllocateClause(OMPAllocateClause *C) { 2988 TRY_TO(TraverseStmt(C->getAllocator())); 2989 TRY_TO(VisitOMPClauseList(C)); 2990 return true; 2991 } 2992 2993 template <typename Derived> 2994 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) { 2995 TRY_TO(VisitOMPClauseWithPreInit(C)); 2996 TRY_TO(TraverseStmt(C->getCondition())); 2997 return true; 2998 } 2999 3000 template <typename Derived> 3001 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) { 3002 TRY_TO(VisitOMPClauseWithPreInit(C)); 3003 TRY_TO(TraverseStmt(C->getCondition())); 3004 return true; 3005 } 3006 3007 template <typename Derived> 3008 bool 3009 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 3010 TRY_TO(VisitOMPClauseWithPreInit(C)); 3011 TRY_TO(TraverseStmt(C->getNumThreads())); 3012 return true; 3013 } 3014 3015 template <typename Derived> 3016 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) { 3017 TRY_TO(TraverseStmt(C->getSafelen())); 3018 return true; 3019 } 3020 3021 template <typename Derived> 3022 bool RecursiveASTVisitor<Derived>::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 3023 TRY_TO(TraverseStmt(C->getSimdlen())); 3024 return true; 3025 } 3026 3027 template <typename Derived> 3028 bool 3029 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) { 3030 TRY_TO(TraverseStmt(C->getNumForLoops())); 3031 return true; 3032 } 3033 3034 template <typename Derived> 3035 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) { 3036 return true; 3037 } 3038 3039 template <typename Derived> 3040 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) { 3041 return true; 3042 } 3043 3044 template <typename Derived> 3045 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedAddressClause( 3046 OMPUnifiedAddressClause *) { 3047 return true; 3048 } 3049 3050 template <typename Derived> 3051 bool RecursiveASTVisitor<Derived>::VisitOMPUnifiedSharedMemoryClause( 3052 OMPUnifiedSharedMemoryClause *) { 3053 return true; 3054 } 3055 3056 template <typename Derived> 3057 bool RecursiveASTVisitor<Derived>::VisitOMPReverseOffloadClause( 3058 OMPReverseOffloadClause *) { 3059 return true; 3060 } 3061 3062 template <typename Derived> 3063 bool RecursiveASTVisitor<Derived>::VisitOMPDynamicAllocatorsClause( 3064 OMPDynamicAllocatorsClause *) { 3065 return true; 3066 } 3067 3068 template <typename Derived> 3069 bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause( 3070 OMPAtomicDefaultMemOrderClause *) { 3071 return true; 3072 } 3073 3074 template <typename Derived> 3075 bool 3076 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) { 3077 TRY_TO(VisitOMPClauseWithPreInit(C)); 3078 TRY_TO(TraverseStmt(C->getChunkSize())); 3079 return true; 3080 } 3081 3082 template <typename Derived> 3083 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *C) { 3084 TRY_TO(TraverseStmt(C->getNumForLoops())); 3085 return true; 3086 } 3087 3088 template <typename Derived> 3089 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) { 3090 return true; 3091 } 3092 3093 template <typename Derived> 3094 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) { 3095 return true; 3096 } 3097 3098 template <typename Derived> 3099 bool 3100 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) { 3101 return true; 3102 } 3103 3104 template <typename Derived> 3105 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) { 3106 return true; 3107 } 3108 3109 template <typename Derived> 3110 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) { 3111 return true; 3112 } 3113 3114 template <typename Derived> 3115 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) { 3116 return true; 3117 } 3118 3119 template <typename Derived> 3120 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) { 3121 return true; 3122 } 3123 3124 template <typename Derived> 3125 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) { 3126 return true; 3127 } 3128 3129 template <typename Derived> 3130 bool RecursiveASTVisitor<Derived>::VisitOMPAcqRelClause(OMPAcqRelClause *) { 3131 return true; 3132 } 3133 3134 template <typename Derived> 3135 bool RecursiveASTVisitor<Derived>::VisitOMPAcquireClause(OMPAcquireClause *) { 3136 return true; 3137 } 3138 3139 template <typename Derived> 3140 bool RecursiveASTVisitor<Derived>::VisitOMPReleaseClause(OMPReleaseClause *) { 3141 return true; 3142 } 3143 3144 template <typename Derived> 3145 bool RecursiveASTVisitor<Derived>::VisitOMPRelaxedClause(OMPRelaxedClause *) { 3146 return true; 3147 } 3148 3149 template <typename Derived> 3150 bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) { 3151 return true; 3152 } 3153 3154 template <typename Derived> 3155 bool RecursiveASTVisitor<Derived>::VisitOMPSIMDClause(OMPSIMDClause *) { 3156 return true; 3157 } 3158 3159 template <typename Derived> 3160 bool RecursiveASTVisitor<Derived>::VisitOMPNogroupClause(OMPNogroupClause *) { 3161 return true; 3162 } 3163 3164 template <typename Derived> 3165 bool RecursiveASTVisitor<Derived>::VisitOMPDestroyClause(OMPDestroyClause *) { 3166 return true; 3167 } 3168 3169 template <typename Derived> 3170 template <typename T> 3171 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) { 3172 for (auto *E : Node->varlists()) { 3173 TRY_TO(TraverseStmt(E)); 3174 } 3175 return true; 3176 } 3177 3178 template <typename Derived> 3179 bool RecursiveASTVisitor<Derived>::VisitOMPInclusiveClause( 3180 OMPInclusiveClause *C) { 3181 TRY_TO(VisitOMPClauseList(C)); 3182 return true; 3183 } 3184 3185 template <typename Derived> 3186 bool RecursiveASTVisitor<Derived>::VisitOMPExclusiveClause( 3187 OMPExclusiveClause *C) { 3188 TRY_TO(VisitOMPClauseList(C)); 3189 return true; 3190 } 3191 3192 template <typename Derived> 3193 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) { 3194 TRY_TO(VisitOMPClauseList(C)); 3195 for (auto *E : C->private_copies()) { 3196 TRY_TO(TraverseStmt(E)); 3197 } 3198 return true; 3199 } 3200 3201 template <typename Derived> 3202 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause( 3203 OMPFirstprivateClause *C) { 3204 TRY_TO(VisitOMPClauseList(C)); 3205 TRY_TO(VisitOMPClauseWithPreInit(C)); 3206 for (auto *E : C->private_copies()) { 3207 TRY_TO(TraverseStmt(E)); 3208 } 3209 for (auto *E : C->inits()) { 3210 TRY_TO(TraverseStmt(E)); 3211 } 3212 return true; 3213 } 3214 3215 template <typename Derived> 3216 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause( 3217 OMPLastprivateClause *C) { 3218 TRY_TO(VisitOMPClauseList(C)); 3219 TRY_TO(VisitOMPClauseWithPostUpdate(C)); 3220 for (auto *E : C->private_copies()) { 3221 TRY_TO(TraverseStmt(E)); 3222 } 3223 for (auto *E : C->source_exprs()) { 3224 TRY_TO(TraverseStmt(E)); 3225 } 3226 for (auto *E : C->destination_exprs()) { 3227 TRY_TO(TraverseStmt(E)); 3228 } 3229 for (auto *E : C->assignment_ops()) { 3230 TRY_TO(TraverseStmt(E)); 3231 } 3232 return true; 3233 } 3234 3235 template <typename Derived> 3236 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) { 3237 TRY_TO(VisitOMPClauseList(C)); 3238 return true; 3239 } 3240 3241 template <typename Derived> 3242 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) { 3243 TRY_TO(TraverseStmt(C->getStep())); 3244 TRY_TO(TraverseStmt(C->getCalcStep())); 3245 TRY_TO(VisitOMPClauseList(C)); 3246 TRY_TO(VisitOMPClauseWithPostUpdate(C)); 3247 for (auto *E : C->privates()) { 3248 TRY_TO(TraverseStmt(E)); 3249 } 3250 for (auto *E : C->inits()) { 3251 TRY_TO(TraverseStmt(E)); 3252 } 3253 for (auto *E : C->updates()) { 3254 TRY_TO(TraverseStmt(E)); 3255 } 3256 for (auto *E : C->finals()) { 3257 TRY_TO(TraverseStmt(E)); 3258 } 3259 return true; 3260 } 3261 3262 template <typename Derived> 3263 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) { 3264 TRY_TO(TraverseStmt(C->getAlignment())); 3265 TRY_TO(VisitOMPClauseList(C)); 3266 return true; 3267 } 3268 3269 template <typename Derived> 3270 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) { 3271 TRY_TO(VisitOMPClauseList(C)); 3272 for (auto *E : C->source_exprs()) { 3273 TRY_TO(TraverseStmt(E)); 3274 } 3275 for (auto *E : C->destination_exprs()) { 3276 TRY_TO(TraverseStmt(E)); 3277 } 3278 for (auto *E : C->assignment_ops()) { 3279 TRY_TO(TraverseStmt(E)); 3280 } 3281 return true; 3282 } 3283 3284 template <typename Derived> 3285 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause( 3286 OMPCopyprivateClause *C) { 3287 TRY_TO(VisitOMPClauseList(C)); 3288 for (auto *E : C->source_exprs()) { 3289 TRY_TO(TraverseStmt(E)); 3290 } 3291 for (auto *E : C->destination_exprs()) { 3292 TRY_TO(TraverseStmt(E)); 3293 } 3294 for (auto *E : C->assignment_ops()) { 3295 TRY_TO(TraverseStmt(E)); 3296 } 3297 return true; 3298 } 3299 3300 template <typename Derived> 3301 bool 3302 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) { 3303 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); 3304 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); 3305 TRY_TO(VisitOMPClauseList(C)); 3306 TRY_TO(VisitOMPClauseWithPostUpdate(C)); 3307 for (auto *E : C->privates()) { 3308 TRY_TO(TraverseStmt(E)); 3309 } 3310 for (auto *E : C->lhs_exprs()) { 3311 TRY_TO(TraverseStmt(E)); 3312 } 3313 for (auto *E : C->rhs_exprs()) { 3314 TRY_TO(TraverseStmt(E)); 3315 } 3316 for (auto *E : C->reduction_ops()) { 3317 TRY_TO(TraverseStmt(E)); 3318 } 3319 if (C->getModifier() == OMPC_REDUCTION_inscan) { 3320 for (auto *E : C->copy_ops()) { 3321 TRY_TO(TraverseStmt(E)); 3322 } 3323 for (auto *E : C->copy_array_temps()) { 3324 TRY_TO(TraverseStmt(E)); 3325 } 3326 for (auto *E : C->copy_array_elems()) { 3327 TRY_TO(TraverseStmt(E)); 3328 } 3329 } 3330 return true; 3331 } 3332 3333 template <typename Derived> 3334 bool RecursiveASTVisitor<Derived>::VisitOMPTaskReductionClause( 3335 OMPTaskReductionClause *C) { 3336 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); 3337 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); 3338 TRY_TO(VisitOMPClauseList(C)); 3339 TRY_TO(VisitOMPClauseWithPostUpdate(C)); 3340 for (auto *E : C->privates()) { 3341 TRY_TO(TraverseStmt(E)); 3342 } 3343 for (auto *E : C->lhs_exprs()) { 3344 TRY_TO(TraverseStmt(E)); 3345 } 3346 for (auto *E : C->rhs_exprs()) { 3347 TRY_TO(TraverseStmt(E)); 3348 } 3349 for (auto *E : C->reduction_ops()) { 3350 TRY_TO(TraverseStmt(E)); 3351 } 3352 return true; 3353 } 3354 3355 template <typename Derived> 3356 bool RecursiveASTVisitor<Derived>::VisitOMPInReductionClause( 3357 OMPInReductionClause *C) { 3358 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); 3359 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); 3360 TRY_TO(VisitOMPClauseList(C)); 3361 TRY_TO(VisitOMPClauseWithPostUpdate(C)); 3362 for (auto *E : C->privates()) { 3363 TRY_TO(TraverseStmt(E)); 3364 } 3365 for (auto *E : C->lhs_exprs()) { 3366 TRY_TO(TraverseStmt(E)); 3367 } 3368 for (auto *E : C->rhs_exprs()) { 3369 TRY_TO(TraverseStmt(E)); 3370 } 3371 for (auto *E : C->reduction_ops()) { 3372 TRY_TO(TraverseStmt(E)); 3373 } 3374 for (auto *E : C->taskgroup_descriptors()) 3375 TRY_TO(TraverseStmt(E)); 3376 return true; 3377 } 3378 3379 template <typename Derived> 3380 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) { 3381 TRY_TO(VisitOMPClauseList(C)); 3382 return true; 3383 } 3384 3385 template <typename Derived> 3386 bool RecursiveASTVisitor<Derived>::VisitOMPDepobjClause(OMPDepobjClause *C) { 3387 TRY_TO(TraverseStmt(C->getDepobj())); 3388 return true; 3389 } 3390 3391 template <typename Derived> 3392 bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) { 3393 TRY_TO(VisitOMPClauseList(C)); 3394 return true; 3395 } 3396 3397 template <typename Derived> 3398 bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) { 3399 TRY_TO(VisitOMPClauseWithPreInit(C)); 3400 TRY_TO(TraverseStmt(C->getDevice())); 3401 return true; 3402 } 3403 3404 template <typename Derived> 3405 bool RecursiveASTVisitor<Derived>::VisitOMPMapClause(OMPMapClause *C) { 3406 TRY_TO(VisitOMPClauseList(C)); 3407 return true; 3408 } 3409 3410 template <typename Derived> 3411 bool RecursiveASTVisitor<Derived>::VisitOMPNumTeamsClause( 3412 OMPNumTeamsClause *C) { 3413 TRY_TO(VisitOMPClauseWithPreInit(C)); 3414 TRY_TO(TraverseStmt(C->getNumTeams())); 3415 return true; 3416 } 3417 3418 template <typename Derived> 3419 bool RecursiveASTVisitor<Derived>::VisitOMPThreadLimitClause( 3420 OMPThreadLimitClause *C) { 3421 TRY_TO(VisitOMPClauseWithPreInit(C)); 3422 TRY_TO(TraverseStmt(C->getThreadLimit())); 3423 return true; 3424 } 3425 3426 template <typename Derived> 3427 bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause( 3428 OMPPriorityClause *C) { 3429 TRY_TO(VisitOMPClauseWithPreInit(C)); 3430 TRY_TO(TraverseStmt(C->getPriority())); 3431 return true; 3432 } 3433 3434 template <typename Derived> 3435 bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause( 3436 OMPGrainsizeClause *C) { 3437 TRY_TO(VisitOMPClauseWithPreInit(C)); 3438 TRY_TO(TraverseStmt(C->getGrainsize())); 3439 return true; 3440 } 3441 3442 template <typename Derived> 3443 bool RecursiveASTVisitor<Derived>::VisitOMPNumTasksClause( 3444 OMPNumTasksClause *C) { 3445 TRY_TO(VisitOMPClauseWithPreInit(C)); 3446 TRY_TO(TraverseStmt(C->getNumTasks())); 3447 return true; 3448 } 3449 3450 template <typename Derived> 3451 bool RecursiveASTVisitor<Derived>::VisitOMPHintClause(OMPHintClause *C) { 3452 TRY_TO(TraverseStmt(C->getHint())); 3453 return true; 3454 } 3455 3456 template <typename Derived> 3457 bool RecursiveASTVisitor<Derived>::VisitOMPDistScheduleClause( 3458 OMPDistScheduleClause *C) { 3459 TRY_TO(VisitOMPClauseWithPreInit(C)); 3460 TRY_TO(TraverseStmt(C->getChunkSize())); 3461 return true; 3462 } 3463 3464 template <typename Derived> 3465 bool 3466 RecursiveASTVisitor<Derived>::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 3467 return true; 3468 } 3469 3470 template <typename Derived> 3471 bool RecursiveASTVisitor<Derived>::VisitOMPToClause(OMPToClause *C) { 3472 TRY_TO(VisitOMPClauseList(C)); 3473 return true; 3474 } 3475 3476 template <typename Derived> 3477 bool RecursiveASTVisitor<Derived>::VisitOMPFromClause(OMPFromClause *C) { 3478 TRY_TO(VisitOMPClauseList(C)); 3479 return true; 3480 } 3481 3482 template <typename Derived> 3483 bool RecursiveASTVisitor<Derived>::VisitOMPUseDevicePtrClause( 3484 OMPUseDevicePtrClause *C) { 3485 TRY_TO(VisitOMPClauseList(C)); 3486 return true; 3487 } 3488 3489 template <typename Derived> 3490 bool RecursiveASTVisitor<Derived>::VisitOMPUseDeviceAddrClause( 3491 OMPUseDeviceAddrClause *C) { 3492 TRY_TO(VisitOMPClauseList(C)); 3493 return true; 3494 } 3495 3496 template <typename Derived> 3497 bool RecursiveASTVisitor<Derived>::VisitOMPIsDevicePtrClause( 3498 OMPIsDevicePtrClause *C) { 3499 TRY_TO(VisitOMPClauseList(C)); 3500 return true; 3501 } 3502 3503 template <typename Derived> 3504 bool RecursiveASTVisitor<Derived>::VisitOMPNontemporalClause( 3505 OMPNontemporalClause *C) { 3506 TRY_TO(VisitOMPClauseList(C)); 3507 for (auto *E : C->private_refs()) { 3508 TRY_TO(TraverseStmt(E)); 3509 } 3510 return true; 3511 } 3512 3513 template <typename Derived> 3514 bool RecursiveASTVisitor<Derived>::VisitOMPOrderClause(OMPOrderClause *) { 3515 return true; 3516 } 3517 3518 template <typename Derived> 3519 bool RecursiveASTVisitor<Derived>::VisitOMPDetachClause(OMPDetachClause *C) { 3520 TRY_TO(TraverseStmt(C->getEventHandler())); 3521 return true; 3522 } 3523 3524 template <typename Derived> 3525 bool RecursiveASTVisitor<Derived>::VisitOMPUsesAllocatorsClause( 3526 OMPUsesAllocatorsClause *C) { 3527 for (unsigned I = 0, E = C->getNumberOfAllocators(); I < E; ++I) { 3528 const OMPUsesAllocatorsClause::Data Data = C->getAllocatorData(I); 3529 TRY_TO(TraverseStmt(Data.Allocator)); 3530 TRY_TO(TraverseStmt(Data.AllocatorTraits)); 3531 } 3532 return true; 3533 } 3534 3535 template <typename Derived> 3536 bool RecursiveASTVisitor<Derived>::VisitOMPAffinityClause( 3537 OMPAffinityClause *C) { 3538 TRY_TO(TraverseStmt(C->getModifier())); 3539 for (Expr *E : C->varlists()) 3540 TRY_TO(TraverseStmt(E)); 3541 return true; 3542 } 3543 3544 // FIXME: look at the following tricky-seeming exprs to see if we 3545 // need to recurse on anything. These are ones that have methods 3546 // returning decls or qualtypes or nestednamespecifier -- though I'm 3547 // not sure if they own them -- or just seemed very complicated, or 3548 // had lots of sub-types to explore. 3549 // 3550 // VisitOverloadExpr and its children: recurse on template args? etc? 3551 3552 // FIXME: go through all the stmts and exprs again, and see which of them 3553 // create new types, and recurse on the types (TypeLocs?) of those. 3554 // Candidates: 3555 // 3556 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html 3557 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html 3558 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html 3559 // Every class that has getQualifier. 3560 3561 #undef DEF_TRAVERSE_STMT 3562 #undef TRAVERSE_STMT 3563 #undef TRAVERSE_STMT_BASE 3564 3565 #undef TRY_TO 3566 3567 } // end namespace clang 3568 3569 #endif // LLVM_CLANG_AST_RECURSIVEASTVISITOR_H 3570