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