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