1 //===--- DataRecursiveASTVisitor.h - Data-Recursive AST Visitor -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the DataRecursiveASTVisitor interface, which recursively 11 // traverses the entire AST, using data recursion for Stmts/Exprs. 12 // 13 //===----------------------------------------------------------------------===// 14 #ifndef LLVM_CLANG_AST_DATARECURSIVEASTVISITOR_H 15 #define LLVM_CLANG_AST_DATARECURSIVEASTVISITOR_H 16 17 #include "clang/AST/Attr.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/DeclCXX.h" 20 #include "clang/AST/DeclFriend.h" 21 #include "clang/AST/DeclObjC.h" 22 #include "clang/AST/DeclOpenMP.h" 23 #include "clang/AST/DeclTemplate.h" 24 #include "clang/AST/Expr.h" 25 #include "clang/AST/ExprCXX.h" 26 #include "clang/AST/ExprObjC.h" 27 #include "clang/AST/NestedNameSpecifier.h" 28 #include "clang/AST/Stmt.h" 29 #include "clang/AST/StmtCXX.h" 30 #include "clang/AST/StmtObjC.h" 31 #include "clang/AST/StmtOpenMP.h" 32 #include "clang/AST/TemplateBase.h" 33 #include "clang/AST/TemplateName.h" 34 #include "clang/AST/Type.h" 35 #include "clang/AST/TypeLoc.h" 36 37 // The following three macros are used for meta programming. The code 38 // using them is responsible for defining macro OPERATOR(). 39 40 // All unary operators. 41 #define UNARYOP_LIST() \ 42 OPERATOR(PostInc) OPERATOR(PostDec) OPERATOR(PreInc) OPERATOR(PreDec) \ 43 OPERATOR(AddrOf) OPERATOR(Deref) OPERATOR(Plus) OPERATOR(Minus) \ 44 OPERATOR(Not) OPERATOR(LNot) OPERATOR(Real) OPERATOR(Imag) \ 45 OPERATOR(Extension) 46 47 // All binary operators (excluding compound assign operators). 48 #define BINOP_LIST() \ 49 OPERATOR(PtrMemD) OPERATOR(PtrMemI) OPERATOR(Mul) OPERATOR(Div) \ 50 OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) OPERATOR(Shl) OPERATOR(Shr) \ 51 OPERATOR(LT) OPERATOR(GT) OPERATOR(LE) OPERATOR(GE) OPERATOR(EQ) \ 52 OPERATOR(NE) OPERATOR(And) OPERATOR(Xor) OPERATOR(Or) OPERATOR(LAnd) \ 53 OPERATOR(LOr) OPERATOR(Assign) OPERATOR(Comma) 54 55 // All compound assign operators. 56 #define CAO_LIST() \ 57 OPERATOR(Mul) OPERATOR(Div) OPERATOR(Rem) OPERATOR(Add) OPERATOR(Sub) \ 58 OPERATOR(Shl) OPERATOR(Shr) OPERATOR(And) OPERATOR(Or) OPERATOR(Xor) 59 60 namespace clang { 61 62 // Reduce the diff between RecursiveASTVisitor / DataRecursiveASTVisitor to 63 // make it easier to track changes and keep the two in sync. 64 #define RecursiveASTVisitor DataRecursiveASTVisitor 65 66 // A helper macro to implement short-circuiting when recursing. It 67 // invokes CALL_EXPR, which must be a method call, on the derived 68 // object (s.t. a user of RecursiveASTVisitor can override the method 69 // in CALL_EXPR). 70 #define TRY_TO(CALL_EXPR) \ 71 do { \ 72 if (!getDerived().CALL_EXPR) \ 73 return false; \ 74 } while (0) 75 76 /// \brief A class that does preorder depth-first traversal on the 77 /// entire Clang AST and visits each node. 78 /// 79 /// This class performs three distinct tasks: 80 /// 1. traverse the AST (i.e. go to each node); 81 /// 2. at a given node, walk up the class hierarchy, starting from 82 /// the node's dynamic type, until the top-most class (e.g. Stmt, 83 /// Decl, or Type) is reached. 84 /// 3. given a (node, class) combination, where 'class' is some base 85 /// class of the dynamic type of 'node', call a user-overridable 86 /// function to actually visit the node. 87 /// 88 /// These tasks are done by three groups of methods, respectively: 89 /// 1. TraverseDecl(Decl *x) does task #1. It is the entry point 90 /// for traversing an AST rooted at x. This method simply 91 /// dispatches (i.e. forwards) to TraverseFoo(Foo *x) where Foo 92 /// is the dynamic type of *x, which calls WalkUpFromFoo(x) and 93 /// then recursively visits the child nodes of x. 94 /// TraverseStmt(Stmt *x) and TraverseType(QualType x) work 95 /// similarly. 96 /// 2. WalkUpFromFoo(Foo *x) does task #2. It does not try to visit 97 /// any child node of x. Instead, it first calls WalkUpFromBar(x) 98 /// where Bar is the direct parent class of Foo (unless Foo has 99 /// no parent), and then calls VisitFoo(x) (see the next list item). 100 /// 3. VisitFoo(Foo *x) does task #3. 101 /// 102 /// These three method groups are tiered (Traverse* > WalkUpFrom* > 103 /// Visit*). A method (e.g. Traverse*) may call methods from the same 104 /// tier (e.g. other Traverse*) or one tier lower (e.g. WalkUpFrom*). 105 /// It may not call methods from a higher tier. 106 /// 107 /// Note that since WalkUpFromFoo() calls WalkUpFromBar() (where Bar 108 /// is Foo's super class) before calling VisitFoo(), the result is 109 /// that the Visit*() methods for a given node are called in the 110 /// top-down order (e.g. for a node of type NamespaceDecl, the order will 111 /// be VisitDecl(), VisitNamedDecl(), and then VisitNamespaceDecl()). 112 /// 113 /// This scheme guarantees that all Visit*() calls for the same AST 114 /// node are grouped together. In other words, Visit*() methods for 115 /// different nodes are never interleaved. 116 /// 117 /// Stmts are traversed internally using a data queue to avoid a stack overflow 118 /// with hugely nested ASTs. 119 /// 120 /// Clients of this visitor should subclass the visitor (providing 121 /// themselves as the template argument, using the curiously recurring 122 /// template pattern) and override any of the Traverse*, WalkUpFrom*, 123 /// and Visit* methods for declarations, types, statements, 124 /// expressions, or other AST nodes where the visitor should customize 125 /// behavior. Most users only need to override Visit*. Advanced 126 /// users may override Traverse* and WalkUpFrom* to implement custom 127 /// traversal strategies. Returning false from one of these overridden 128 /// functions will abort the entire traversal. 129 /// 130 /// By default, this visitor tries to visit every part of the explicit 131 /// source code exactly once. The default policy towards templates 132 /// is to descend into the 'pattern' class or function body, not any 133 /// explicit or implicit instantiations. Explicit specializations 134 /// are still visited, and the patterns of partial specializations 135 /// are visited separately. This behavior can be changed by 136 /// overriding shouldVisitTemplateInstantiations() in the derived class 137 /// to return true, in which case all known implicit and explicit 138 /// instantiations will be visited at the same time as the pattern 139 /// from which they were produced. 140 template <typename Derived> class RecursiveASTVisitor { 141 public: 142 /// \brief Return a reference to the derived class. 143 Derived &getDerived() { return *static_cast<Derived *>(this); } 144 145 /// \brief Return whether this visitor should recurse into 146 /// template instantiations. 147 bool shouldVisitTemplateInstantiations() const { return false; } 148 149 /// \brief Return whether this visitor should recurse into the types of 150 /// TypeLocs. 151 bool shouldWalkTypesOfTypeLocs() const { return true; } 152 153 /// \brief Recursively visit a statement or expression, by 154 /// dispatching to Traverse*() based on the argument's dynamic type. 155 /// 156 /// \returns false if the visitation was terminated early, true 157 /// otherwise (including when the argument is NULL). 158 bool TraverseStmt(Stmt *S); 159 160 /// \brief Recursively visit a type, by dispatching to 161 /// Traverse*Type() based on the argument's getTypeClass() property. 162 /// 163 /// \returns false if the visitation was terminated early, true 164 /// otherwise (including when the argument is a Null type). 165 bool TraverseType(QualType T); 166 167 /// \brief Recursively visit a type with location, by dispatching to 168 /// Traverse*TypeLoc() based on the argument type's getTypeClass() property. 169 /// 170 /// \returns false if the visitation was terminated early, true 171 /// otherwise (including when the argument is a Null type location). 172 bool TraverseTypeLoc(TypeLoc TL); 173 174 /// \brief Recursively visit an attribute, by dispatching to 175 /// Traverse*Attr() based on the argument's dynamic type. 176 /// 177 /// \returns false if the visitation was terminated early, true 178 /// otherwise (including when the argument is a Null type location). 179 bool TraverseAttr(Attr *At); 180 181 /// \brief Recursively visit a declaration, by dispatching to 182 /// Traverse*Decl() based on the argument's dynamic type. 183 /// 184 /// \returns false if the visitation was terminated early, true 185 /// otherwise (including when the argument is NULL). 186 bool TraverseDecl(Decl *D); 187 188 /// \brief Recursively visit a C++ nested-name-specifier. 189 /// 190 /// \returns false if the visitation was terminated early, true otherwise. 191 bool TraverseNestedNameSpecifier(NestedNameSpecifier *NNS); 192 193 /// \brief Recursively visit a C++ nested-name-specifier with location 194 /// information. 195 /// 196 /// \returns false if the visitation was terminated early, true otherwise. 197 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); 198 199 /// \brief Recursively visit a name with its location information. 200 /// 201 /// \returns false if the visitation was terminated early, true otherwise. 202 bool TraverseDeclarationNameInfo(DeclarationNameInfo NameInfo); 203 204 /// \brief Recursively visit a template name and dispatch to the 205 /// appropriate method. 206 /// 207 /// \returns false if the visitation was terminated early, true otherwise. 208 bool TraverseTemplateName(TemplateName Template); 209 210 /// \brief Recursively visit a template argument and dispatch to the 211 /// appropriate method for the argument type. 212 /// 213 /// \returns false if the visitation was terminated early, true otherwise. 214 // FIXME: migrate callers to TemplateArgumentLoc instead. 215 bool TraverseTemplateArgument(const TemplateArgument &Arg); 216 217 /// \brief Recursively visit a template argument location and dispatch to the 218 /// appropriate method for the argument type. 219 /// 220 /// \returns false if the visitation was terminated early, true otherwise. 221 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc); 222 223 /// \brief Recursively visit a set of template arguments. 224 /// This can be overridden by a subclass, but it's not expected that 225 /// will be needed -- this visitor always dispatches to another. 226 /// 227 /// \returns false if the visitation was terminated early, true otherwise. 228 // FIXME: take a TemplateArgumentLoc* (or TemplateArgumentListInfo) instead. 229 bool TraverseTemplateArguments(const TemplateArgument *Args, 230 unsigned NumArgs); 231 232 /// \brief Recursively visit a constructor initializer. This 233 /// automatically dispatches to another visitor for the initializer 234 /// expression, but not for the name of the initializer, so may 235 /// be overridden for clients that need access to the name. 236 /// 237 /// \returns false if the visitation was terminated early, true otherwise. 238 bool TraverseConstructorInitializer(CXXCtorInitializer *Init); 239 240 /// \brief Recursively visit a lambda capture. 241 /// 242 /// \returns false if the visitation was terminated early, true otherwise. 243 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C); 244 245 /// \brief Recursively visit the body of a lambda expression. 246 /// 247 /// This provides a hook for visitors that need more context when visiting 248 /// \c LE->getBody(). 249 /// 250 /// \returns false if the visitation was terminated early, true otherwise. 251 bool TraverseLambdaBody(LambdaExpr *LE); 252 253 // ---- Methods on Attrs ---- 254 255 // \brief Visit an attribute. 256 bool VisitAttr(Attr *A) { return true; } 257 258 // Declare Traverse* and empty Visit* for all Attr classes. 259 #define ATTR_VISITOR_DECLS_ONLY 260 #include "clang/AST/AttrVisitor.inc" 261 #undef ATTR_VISITOR_DECLS_ONLY 262 263 // ---- Methods on Stmts ---- 264 265 // Declare Traverse*() for all concrete Stmt classes. 266 #define ABSTRACT_STMT(STMT) 267 #define STMT(CLASS, PARENT) bool Traverse##CLASS(CLASS *S); 268 #include "clang/AST/StmtNodes.inc" 269 // The above header #undefs ABSTRACT_STMT and STMT upon exit. 270 271 // Define WalkUpFrom*() and empty Visit*() for all Stmt classes. 272 bool WalkUpFromStmt(Stmt *S) { return getDerived().VisitStmt(S); } 273 bool VisitStmt(Stmt *S) { return true; } 274 #define STMT(CLASS, PARENT) \ 275 bool WalkUpFrom##CLASS(CLASS *S) { \ 276 TRY_TO(WalkUpFrom##PARENT(S)); \ 277 TRY_TO(Visit##CLASS(S)); \ 278 return true; \ 279 } \ 280 bool Visit##CLASS(CLASS *S) { return true; } 281 #include "clang/AST/StmtNodes.inc" 282 283 // Define Traverse*(), WalkUpFrom*(), and Visit*() for unary 284 // operator methods. Unary operators are not classes in themselves 285 // (they're all opcodes in UnaryOperator) but do have visitors. 286 #define OPERATOR(NAME) \ 287 bool TraverseUnary##NAME(UnaryOperator *S) { \ 288 TRY_TO(WalkUpFromUnary##NAME(S)); \ 289 StmtQueueAction StmtQueue(*this); \ 290 StmtQueue.queue(S->getSubExpr()); \ 291 return true; \ 292 } \ 293 bool WalkUpFromUnary##NAME(UnaryOperator *S) { \ 294 TRY_TO(WalkUpFromUnaryOperator(S)); \ 295 TRY_TO(VisitUnary##NAME(S)); \ 296 return true; \ 297 } \ 298 bool VisitUnary##NAME(UnaryOperator *S) { return true; } 299 300 UNARYOP_LIST() 301 #undef OPERATOR 302 303 // Define Traverse*(), WalkUpFrom*(), and Visit*() for binary 304 // operator methods. Binary operators are not classes in themselves 305 // (they're all opcodes in BinaryOperator) but do have visitors. 306 #define GENERAL_BINOP_FALLBACK(NAME, BINOP_TYPE) \ 307 bool TraverseBin##NAME(BINOP_TYPE *S) { \ 308 TRY_TO(WalkUpFromBin##NAME(S)); \ 309 StmtQueueAction StmtQueue(*this); \ 310 StmtQueue.queue(S->getLHS()); \ 311 StmtQueue.queue(S->getRHS()); \ 312 return true; \ 313 } \ 314 bool WalkUpFromBin##NAME(BINOP_TYPE *S) { \ 315 TRY_TO(WalkUpFrom##BINOP_TYPE(S)); \ 316 TRY_TO(VisitBin##NAME(S)); \ 317 return true; \ 318 } \ 319 bool VisitBin##NAME(BINOP_TYPE *S) { return true; } 320 321 #define OPERATOR(NAME) GENERAL_BINOP_FALLBACK(NAME, BinaryOperator) 322 BINOP_LIST() 323 #undef OPERATOR 324 325 // Define Traverse*(), WalkUpFrom*(), and Visit*() for compound 326 // assignment methods. Compound assignment operators are not 327 // classes in themselves (they're all opcodes in 328 // CompoundAssignOperator) but do have visitors. 329 #define OPERATOR(NAME) \ 330 GENERAL_BINOP_FALLBACK(NAME##Assign, CompoundAssignOperator) 331 332 CAO_LIST() 333 #undef OPERATOR 334 #undef GENERAL_BINOP_FALLBACK 335 336 // ---- Methods on Types ---- 337 // FIXME: revamp to take TypeLoc's rather than Types. 338 339 // Declare Traverse*() for all concrete Type classes. 340 #define ABSTRACT_TYPE(CLASS, BASE) 341 #define TYPE(CLASS, BASE) bool Traverse##CLASS##Type(CLASS##Type *T); 342 #include "clang/AST/TypeNodes.def" 343 // The above header #undefs ABSTRACT_TYPE and TYPE upon exit. 344 345 // Define WalkUpFrom*() and empty Visit*() for all Type classes. 346 bool WalkUpFromType(Type *T) { return getDerived().VisitType(T); } 347 bool VisitType(Type *T) { return true; } 348 #define TYPE(CLASS, BASE) \ 349 bool WalkUpFrom##CLASS##Type(CLASS##Type *T) { \ 350 TRY_TO(WalkUpFrom##BASE(T)); \ 351 TRY_TO(Visit##CLASS##Type(T)); \ 352 return true; \ 353 } \ 354 bool Visit##CLASS##Type(CLASS##Type *T) { return true; } 355 #include "clang/AST/TypeNodes.def" 356 357 // ---- Methods on TypeLocs ---- 358 // FIXME: this currently just calls the matching Type methods 359 360 // Declare Traverse*() for all concrete TypeLoc classes. 361 #define ABSTRACT_TYPELOC(CLASS, BASE) 362 #define TYPELOC(CLASS, BASE) bool Traverse##CLASS##TypeLoc(CLASS##TypeLoc TL); 363 #include "clang/AST/TypeLocNodes.def" 364 // The above header #undefs ABSTRACT_TYPELOC and TYPELOC upon exit. 365 366 // Define WalkUpFrom*() and empty Visit*() for all TypeLoc classes. 367 bool WalkUpFromTypeLoc(TypeLoc TL) { return getDerived().VisitTypeLoc(TL); } 368 bool VisitTypeLoc(TypeLoc TL) { return true; } 369 370 // QualifiedTypeLoc and UnqualTypeLoc are not declared in 371 // TypeNodes.def and thus need to be handled specially. 372 bool WalkUpFromQualifiedTypeLoc(QualifiedTypeLoc TL) { 373 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc()); 374 } 375 bool VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { return true; } 376 bool WalkUpFromUnqualTypeLoc(UnqualTypeLoc TL) { 377 return getDerived().VisitUnqualTypeLoc(TL.getUnqualifiedLoc()); 378 } 379 bool VisitUnqualTypeLoc(UnqualTypeLoc TL) { return true; } 380 381 // Note that BASE includes trailing 'Type' which CLASS doesn't. 382 #define TYPE(CLASS, BASE) \ 383 bool WalkUpFrom##CLASS##TypeLoc(CLASS##TypeLoc TL) { \ 384 TRY_TO(WalkUpFrom##BASE##Loc(TL)); \ 385 TRY_TO(Visit##CLASS##TypeLoc(TL)); \ 386 return true; \ 387 } \ 388 bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TL) { return true; } 389 #include "clang/AST/TypeNodes.def" 390 391 // ---- Methods on Decls ---- 392 393 // Declare Traverse*() for all concrete Decl classes. 394 #define ABSTRACT_DECL(DECL) 395 #define DECL(CLASS, BASE) bool Traverse##CLASS##Decl(CLASS##Decl *D); 396 #include "clang/AST/DeclNodes.inc" 397 // The above header #undefs ABSTRACT_DECL and DECL upon exit. 398 399 // Define WalkUpFrom*() and empty Visit*() for all Decl classes. 400 bool WalkUpFromDecl(Decl *D) { return getDerived().VisitDecl(D); } 401 bool VisitDecl(Decl *D) { return true; } 402 #define DECL(CLASS, BASE) \ 403 bool WalkUpFrom##CLASS##Decl(CLASS##Decl *D) { \ 404 TRY_TO(WalkUpFrom##BASE(D)); \ 405 TRY_TO(Visit##CLASS##Decl(D)); \ 406 return true; \ 407 } \ 408 bool Visit##CLASS##Decl(CLASS##Decl *D) { return true; } 409 #include "clang/AST/DeclNodes.inc" 410 411 private: 412 // These are helper methods used by more than one Traverse* method. 413 bool TraverseTemplateParameterListHelper(TemplateParameterList *TPL); 414 bool TraverseClassInstantiations(ClassTemplateDecl *D); 415 bool TraverseVariableInstantiations(VarTemplateDecl *D); 416 bool TraverseFunctionInstantiations(FunctionTemplateDecl *D); 417 bool TraverseTemplateArgumentLocsHelper(const TemplateArgumentLoc *TAL, 418 unsigned Count); 419 bool TraverseArrayTypeLocHelper(ArrayTypeLoc TL); 420 bool TraverseRecordHelper(RecordDecl *D); 421 bool TraverseCXXRecordHelper(CXXRecordDecl *D); 422 bool TraverseDeclaratorHelper(DeclaratorDecl *D); 423 bool TraverseDeclContextHelper(DeclContext *DC); 424 bool TraverseFunctionHelper(FunctionDecl *D); 425 bool TraverseVarHelper(VarDecl *D); 426 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S); 427 bool TraverseOMPLoopDirective(OMPLoopDirective *S); 428 bool TraverseOMPClause(OMPClause *C); 429 #define OPENMP_CLAUSE(Name, Class) bool Visit##Class(Class *C); 430 #include "clang/Basic/OpenMPKinds.def" 431 /// \brief Process clauses with list of variables. 432 template <typename T> bool VisitOMPClauseList(T *Node); 433 434 typedef SmallVector<Stmt *, 16> StmtsTy; 435 typedef SmallVector<StmtsTy *, 4> QueuesTy; 436 437 QueuesTy Queues; 438 439 class NewQueueRAII { 440 RecursiveASTVisitor &RAV; 441 442 public: 443 NewQueueRAII(StmtsTy &queue, RecursiveASTVisitor &RAV) : RAV(RAV) { 444 RAV.Queues.push_back(&queue); 445 } 446 ~NewQueueRAII() { RAV.Queues.pop_back(); } 447 }; 448 449 StmtsTy &getCurrentQueue() { 450 assert(!Queues.empty() && "base TraverseStmt was never called?"); 451 return *Queues.back(); 452 } 453 454 public: 455 class StmtQueueAction { 456 StmtsTy &CurrQueue; 457 458 public: 459 explicit StmtQueueAction(RecursiveASTVisitor &RAV) 460 : CurrQueue(RAV.getCurrentQueue()) {} 461 462 void queue(Stmt *S) { CurrQueue.push_back(S); } 463 }; 464 }; 465 466 #define DISPATCH(NAME, CLASS, VAR) \ 467 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)) 468 469 template <typename Derived> 470 bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) { 471 if (!S) 472 return true; 473 474 StmtsTy Queue, StmtsToEnqueue; 475 Queue.push_back(S); 476 NewQueueRAII NQ(StmtsToEnqueue, *this); 477 478 while (!Queue.empty()) { 479 S = Queue.pop_back_val(); 480 if (!S) 481 continue; 482 483 StmtsToEnqueue.clear(); 484 485 #define DISPATCH_STMT(NAME, CLASS, VAR) \ 486 TRY_TO(Traverse##NAME(static_cast<CLASS *>(VAR))); \ 487 break 488 489 // If we have a binary expr, dispatch to the subcode of the binop. A smart 490 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt 491 // below. 492 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { 493 switch (BinOp->getOpcode()) { 494 #define OPERATOR(NAME) \ 495 case BO_##NAME: \ 496 DISPATCH_STMT(Bin##NAME, BinaryOperator, S); 497 498 BINOP_LIST() 499 #undef OPERATOR 500 #undef BINOP_LIST 501 502 #define OPERATOR(NAME) \ 503 case BO_##NAME##Assign: \ 504 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S); 505 506 CAO_LIST() 507 #undef OPERATOR 508 #undef CAO_LIST 509 } 510 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) { 511 switch (UnOp->getOpcode()) { 512 #define OPERATOR(NAME) \ 513 case UO_##NAME: \ 514 DISPATCH_STMT(Unary##NAME, UnaryOperator, S); 515 516 UNARYOP_LIST() 517 #undef OPERATOR 518 #undef UNARYOP_LIST 519 } 520 } else { 521 522 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt. 523 switch (S->getStmtClass()) { 524 case Stmt::NoStmtClass: 525 break; 526 #define ABSTRACT_STMT(STMT) 527 #define STMT(CLASS, PARENT) \ 528 case Stmt::CLASS##Class: \ 529 DISPATCH_STMT(CLASS, CLASS, S); 530 #include "clang/AST/StmtNodes.inc" 531 } 532 } 533 534 for (SmallVectorImpl<Stmt *>::reverse_iterator RI = StmtsToEnqueue.rbegin(), 535 RE = StmtsToEnqueue.rend(); 536 RI != RE; ++RI) 537 Queue.push_back(*RI); 538 } 539 540 return true; 541 } 542 543 #undef DISPATCH_STMT 544 545 template <typename Derived> 546 bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) { 547 if (T.isNull()) 548 return true; 549 550 switch (T->getTypeClass()) { 551 #define ABSTRACT_TYPE(CLASS, BASE) 552 #define TYPE(CLASS, BASE) \ 553 case Type::CLASS: \ 554 DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr())); 555 #include "clang/AST/TypeNodes.def" 556 } 557 558 return true; 559 } 560 561 template <typename Derived> 562 bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) { 563 if (TL.isNull()) 564 return true; 565 566 switch (TL.getTypeLocClass()) { 567 #define ABSTRACT_TYPELOC(CLASS, BASE) 568 #define TYPELOC(CLASS, BASE) \ 569 case TypeLoc::CLASS: \ 570 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>()); 571 #include "clang/AST/TypeLocNodes.def" 572 } 573 574 return true; 575 } 576 577 // Define the Traverse*Attr(Attr* A) methods 578 #define VISITORCLASS RecursiveASTVisitor 579 #include "clang/AST/AttrVisitor.inc" 580 #undef VISITORCLASS 581 582 template <typename Derived> 583 bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) { 584 if (!D) 585 return true; 586 587 // As a syntax visitor, we want to ignore declarations for 588 // implicitly-defined declarations (ones not typed explicitly by the 589 // user). 590 if (D->isImplicit()) 591 return true; 592 593 switch (D->getKind()) { 594 #define ABSTRACT_DECL(DECL) 595 #define DECL(CLASS, BASE) \ 596 case Decl::CLASS: \ 597 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \ 598 return false; \ 599 break; 600 #include "clang/AST/DeclNodes.inc" 601 } 602 603 // Visit any attributes attached to this declaration. 604 for (auto *I : D->attrs()) { 605 if (!getDerived().TraverseAttr(I)) 606 return false; 607 } 608 return true; 609 } 610 611 #undef DISPATCH 612 613 template <typename Derived> 614 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier( 615 NestedNameSpecifier *NNS) { 616 if (!NNS) 617 return true; 618 619 if (NNS->getPrefix()) 620 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix())); 621 622 switch (NNS->getKind()) { 623 case NestedNameSpecifier::Identifier: 624 case NestedNameSpecifier::Namespace: 625 case NestedNameSpecifier::NamespaceAlias: 626 case NestedNameSpecifier::Global: 627 case NestedNameSpecifier::Super: 628 return true; 629 630 case NestedNameSpecifier::TypeSpec: 631 case NestedNameSpecifier::TypeSpecWithTemplate: 632 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0))); 633 } 634 635 return true; 636 } 637 638 template <typename Derived> 639 bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc( 640 NestedNameSpecifierLoc NNS) { 641 if (!NNS) 642 return true; 643 644 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix()) 645 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix)); 646 647 switch (NNS.getNestedNameSpecifier()->getKind()) { 648 case NestedNameSpecifier::Identifier: 649 case NestedNameSpecifier::Namespace: 650 case NestedNameSpecifier::NamespaceAlias: 651 case NestedNameSpecifier::Global: 652 case NestedNameSpecifier::Super: 653 return true; 654 655 case NestedNameSpecifier::TypeSpec: 656 case NestedNameSpecifier::TypeSpecWithTemplate: 657 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc())); 658 break; 659 } 660 661 return true; 662 } 663 664 template <typename Derived> 665 bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo( 666 DeclarationNameInfo NameInfo) { 667 switch (NameInfo.getName().getNameKind()) { 668 case DeclarationName::CXXConstructorName: 669 case DeclarationName::CXXDestructorName: 670 case DeclarationName::CXXConversionFunctionName: 671 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) 672 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc())); 673 674 break; 675 676 case DeclarationName::Identifier: 677 case DeclarationName::ObjCZeroArgSelector: 678 case DeclarationName::ObjCOneArgSelector: 679 case DeclarationName::ObjCMultiArgSelector: 680 case DeclarationName::CXXOperatorName: 681 case DeclarationName::CXXLiteralOperatorName: 682 case DeclarationName::CXXUsingDirective: 683 break; 684 } 685 686 return true; 687 } 688 689 template <typename Derived> 690 bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) { 691 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) 692 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier())); 693 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) 694 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier())); 695 696 return true; 697 } 698 699 template <typename Derived> 700 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument( 701 const TemplateArgument &Arg) { 702 switch (Arg.getKind()) { 703 case TemplateArgument::Null: 704 case TemplateArgument::Declaration: 705 case TemplateArgument::Integral: 706 case TemplateArgument::NullPtr: 707 return true; 708 709 case TemplateArgument::Type: 710 return getDerived().TraverseType(Arg.getAsType()); 711 712 case TemplateArgument::Template: 713 case TemplateArgument::TemplateExpansion: 714 return getDerived().TraverseTemplateName( 715 Arg.getAsTemplateOrTemplatePattern()); 716 717 case TemplateArgument::Expression: 718 return getDerived().TraverseStmt(Arg.getAsExpr()); 719 720 case TemplateArgument::Pack: 721 return getDerived().TraverseTemplateArguments(Arg.pack_begin(), 722 Arg.pack_size()); 723 } 724 725 return true; 726 } 727 728 // FIXME: no template name location? 729 // FIXME: no source locations for a template argument pack? 730 template <typename Derived> 731 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc( 732 const TemplateArgumentLoc &ArgLoc) { 733 const TemplateArgument &Arg = ArgLoc.getArgument(); 734 735 switch (Arg.getKind()) { 736 case TemplateArgument::Null: 737 case TemplateArgument::Declaration: 738 case TemplateArgument::Integral: 739 case TemplateArgument::NullPtr: 740 return true; 741 742 case TemplateArgument::Type: { 743 // FIXME: how can TSI ever be NULL? 744 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo()) 745 return getDerived().TraverseTypeLoc(TSI->getTypeLoc()); 746 else 747 return getDerived().TraverseType(Arg.getAsType()); 748 } 749 750 case TemplateArgument::Template: 751 case TemplateArgument::TemplateExpansion: 752 if (ArgLoc.getTemplateQualifierLoc()) 753 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc( 754 ArgLoc.getTemplateQualifierLoc())); 755 return getDerived().TraverseTemplateName( 756 Arg.getAsTemplateOrTemplatePattern()); 757 758 case TemplateArgument::Expression: 759 return getDerived().TraverseStmt(ArgLoc.getSourceExpression()); 760 761 case TemplateArgument::Pack: 762 return getDerived().TraverseTemplateArguments(Arg.pack_begin(), 763 Arg.pack_size()); 764 } 765 766 return true; 767 } 768 769 template <typename Derived> 770 bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments( 771 const TemplateArgument *Args, unsigned NumArgs) { 772 for (unsigned I = 0; I != NumArgs; ++I) { 773 TRY_TO(TraverseTemplateArgument(Args[I])); 774 } 775 776 return true; 777 } 778 779 template <typename Derived> 780 bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer( 781 CXXCtorInitializer *Init) { 782 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) 783 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 784 785 if (Init->isWritten()) 786 TRY_TO(TraverseStmt(Init->getInit())); 787 return true; 788 } 789 790 template <typename Derived> 791 bool 792 RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE, 793 const LambdaCapture *C) { 794 if (C->isInitCapture()) 795 TRY_TO(TraverseDecl(C->getCapturedVar())); 796 return true; 797 } 798 799 template <typename Derived> 800 bool RecursiveASTVisitor<Derived>::TraverseLambdaBody(LambdaExpr *LE) { 801 StmtQueueAction StmtQueue(*this); 802 StmtQueue.queue(LE->getBody()); 803 return true; 804 } 805 806 // ----------------- Type traversal ----------------- 807 808 // This macro makes available a variable T, the passed-in type. 809 #define DEF_TRAVERSE_TYPE(TYPE, CODE) \ 810 template <typename Derived> \ 811 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \ 812 TRY_TO(WalkUpFrom##TYPE(T)); \ 813 { CODE; } \ 814 return true; \ 815 } 816 817 DEF_TRAVERSE_TYPE(BuiltinType, {}) 818 819 DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); }) 820 821 DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); }) 822 823 DEF_TRAVERSE_TYPE(BlockPointerType, 824 { TRY_TO(TraverseType(T->getPointeeType())); }) 825 826 DEF_TRAVERSE_TYPE(LValueReferenceType, 827 { TRY_TO(TraverseType(T->getPointeeType())); }) 828 829 DEF_TRAVERSE_TYPE(RValueReferenceType, 830 { TRY_TO(TraverseType(T->getPointeeType())); }) 831 832 DEF_TRAVERSE_TYPE(MemberPointerType, { 833 TRY_TO(TraverseType(QualType(T->getClass(), 0))); 834 TRY_TO(TraverseType(T->getPointeeType())); 835 }) 836 837 DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); }) 838 839 DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); }) 840 841 DEF_TRAVERSE_TYPE(ConstantArrayType, 842 { TRY_TO(TraverseType(T->getElementType())); }) 843 844 DEF_TRAVERSE_TYPE(IncompleteArrayType, 845 { TRY_TO(TraverseType(T->getElementType())); }) 846 847 DEF_TRAVERSE_TYPE(VariableArrayType, { 848 TRY_TO(TraverseType(T->getElementType())); 849 TRY_TO(TraverseStmt(T->getSizeExpr())); 850 }) 851 852 DEF_TRAVERSE_TYPE(DependentSizedArrayType, { 853 TRY_TO(TraverseType(T->getElementType())); 854 if (T->getSizeExpr()) 855 TRY_TO(TraverseStmt(T->getSizeExpr())); 856 }) 857 858 DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, { 859 if (T->getSizeExpr()) 860 TRY_TO(TraverseStmt(T->getSizeExpr())); 861 TRY_TO(TraverseType(T->getElementType())); 862 }) 863 864 DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); }) 865 866 DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); }) 867 868 DEF_TRAVERSE_TYPE(FunctionNoProtoType, 869 { TRY_TO(TraverseType(T->getReturnType())); }) 870 871 DEF_TRAVERSE_TYPE(FunctionProtoType, { 872 TRY_TO(TraverseType(T->getReturnType())); 873 874 for (const auto &A : T->param_types()) { 875 TRY_TO(TraverseType(A)); 876 } 877 878 for (const auto &E : T->exceptions()) { 879 TRY_TO(TraverseType(E)); 880 } 881 882 if (Expr *NE = T->getNoexceptExpr()) 883 TRY_TO(TraverseStmt(NE)); 884 }) 885 886 DEF_TRAVERSE_TYPE(UnresolvedUsingType, {}) 887 DEF_TRAVERSE_TYPE(TypedefType, {}) 888 889 DEF_TRAVERSE_TYPE(TypeOfExprType, 890 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) 891 892 DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); }) 893 894 DEF_TRAVERSE_TYPE(DecltypeType, 895 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) 896 897 DEF_TRAVERSE_TYPE(UnaryTransformType, { 898 TRY_TO(TraverseType(T->getBaseType())); 899 TRY_TO(TraverseType(T->getUnderlyingType())); 900 }) 901 902 DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); }) 903 904 DEF_TRAVERSE_TYPE(RecordType, {}) 905 DEF_TRAVERSE_TYPE(EnumType, {}) 906 DEF_TRAVERSE_TYPE(TemplateTypeParmType, {}) 907 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {}) 908 DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {}) 909 910 DEF_TRAVERSE_TYPE(TemplateSpecializationType, { 911 TRY_TO(TraverseTemplateName(T->getTemplateName())); 912 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 913 }) 914 915 DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) 916 917 DEF_TRAVERSE_TYPE(AttributedType, 918 { TRY_TO(TraverseType(T->getModifiedType())); }) 919 920 DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); }) 921 922 DEF_TRAVERSE_TYPE(ElaboratedType, { 923 if (T->getQualifier()) { 924 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); 925 } 926 TRY_TO(TraverseType(T->getNamedType())); 927 }) 928 929 DEF_TRAVERSE_TYPE(DependentNameType, 930 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); }) 931 932 DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, { 933 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); 934 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 935 }) 936 937 DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); }) 938 939 DEF_TRAVERSE_TYPE(ObjCInterfaceType, {}) 940 941 DEF_TRAVERSE_TYPE(ObjCObjectType, { 942 // We have to watch out here because an ObjCInterfaceType's base 943 // type is itself. 944 if (T->getBaseType().getTypePtr() != T) 945 TRY_TO(TraverseType(T->getBaseType())); 946 }) 947 948 DEF_TRAVERSE_TYPE(ObjCObjectPointerType, 949 { TRY_TO(TraverseType(T->getPointeeType())); }) 950 951 DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); }) 952 953 #undef DEF_TRAVERSE_TYPE 954 955 // ----------------- TypeLoc traversal ----------------- 956 957 // This macro makes available a variable TL, the passed-in TypeLoc. 958 // If requested, it calls WalkUpFrom* for the Type in the given TypeLoc, 959 // in addition to WalkUpFrom* for the TypeLoc itself, such that existing 960 // clients that override the WalkUpFrom*Type() and/or Visit*Type() methods 961 // continue to work. 962 #define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \ 963 template <typename Derived> \ 964 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \ 965 if (getDerived().shouldWalkTypesOfTypeLocs()) \ 966 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \ 967 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \ 968 { CODE; } \ 969 return true; \ 970 } 971 972 template <typename Derived> 973 bool 974 RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) { 975 // Move this over to the 'main' typeloc tree. Note that this is a 976 // move -- we pretend that we were really looking at the unqualified 977 // typeloc all along -- rather than a recursion, so we don't follow 978 // the normal CRTP plan of going through 979 // getDerived().TraverseTypeLoc. If we did, we'd be traversing 980 // twice for the same type (once as a QualifiedTypeLoc version of 981 // the type, once as an UnqualifiedTypeLoc version of the type), 982 // which in effect means we'd call VisitTypeLoc twice with the 983 // 'same' type. This solves that problem, at the cost of never 984 // seeing the qualified version of the type (unless the client 985 // subclasses TraverseQualifiedTypeLoc themselves). It's not a 986 // perfect solution. A perfect solution probably requires making 987 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a 988 // wrapper around Type* -- rather than being its own class in the 989 // type hierarchy. 990 return TraverseTypeLoc(TL.getUnqualifiedLoc()); 991 } 992 993 DEF_TRAVERSE_TYPELOC(BuiltinType, {}) 994 995 // FIXME: ComplexTypeLoc is unfinished 996 DEF_TRAVERSE_TYPELOC(ComplexType, { 997 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 998 }) 999 1000 DEF_TRAVERSE_TYPELOC(PointerType, 1001 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1002 1003 DEF_TRAVERSE_TYPELOC(BlockPointerType, 1004 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1005 1006 DEF_TRAVERSE_TYPELOC(LValueReferenceType, 1007 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1008 1009 DEF_TRAVERSE_TYPELOC(RValueReferenceType, 1010 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1011 1012 // FIXME: location of base class? 1013 // We traverse this in the type case as well, but how is it not reached through 1014 // the pointee type? 1015 DEF_TRAVERSE_TYPELOC(MemberPointerType, { 1016 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0))); 1017 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); 1018 }) 1019 1020 DEF_TRAVERSE_TYPELOC(AdjustedType, 1021 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) 1022 1023 DEF_TRAVERSE_TYPELOC(DecayedType, 1024 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) 1025 1026 template <typename Derived> 1027 bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) { 1028 // This isn't available for ArrayType, but is for the ArrayTypeLoc. 1029 TRY_TO(TraverseStmt(TL.getSizeExpr())); 1030 return true; 1031 } 1032 1033 DEF_TRAVERSE_TYPELOC(ConstantArrayType, { 1034 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1035 return TraverseArrayTypeLocHelper(TL); 1036 }) 1037 1038 DEF_TRAVERSE_TYPELOC(IncompleteArrayType, { 1039 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1040 return TraverseArrayTypeLocHelper(TL); 1041 }) 1042 1043 DEF_TRAVERSE_TYPELOC(VariableArrayType, { 1044 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1045 return TraverseArrayTypeLocHelper(TL); 1046 }) 1047 1048 DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, { 1049 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1050 return TraverseArrayTypeLocHelper(TL); 1051 }) 1052 1053 // FIXME: order? why not size expr first? 1054 // FIXME: base VectorTypeLoc is unfinished 1055 DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, { 1056 if (TL.getTypePtr()->getSizeExpr()) 1057 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr())); 1058 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1059 }) 1060 1061 // FIXME: VectorTypeLoc is unfinished 1062 DEF_TRAVERSE_TYPELOC(VectorType, { 1063 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1064 }) 1065 1066 // FIXME: size and attributes 1067 // FIXME: base VectorTypeLoc is unfinished 1068 DEF_TRAVERSE_TYPELOC(ExtVectorType, { 1069 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1070 }) 1071 1072 DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, 1073 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); }) 1074 1075 // FIXME: location of exception specifications (attributes?) 1076 DEF_TRAVERSE_TYPELOC(FunctionProtoType, { 1077 TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); 1078 1079 const FunctionProtoType *T = TL.getTypePtr(); 1080 1081 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { 1082 if (TL.getParam(I)) { 1083 TRY_TO(TraverseDecl(TL.getParam(I))); 1084 } else if (I < T->getNumParams()) { 1085 TRY_TO(TraverseType(T->getParamType(I))); 1086 } 1087 } 1088 1089 for (const auto &E : T->exceptions()) { 1090 TRY_TO(TraverseType(E)); 1091 } 1092 1093 if (Expr *NE = T->getNoexceptExpr()) 1094 TRY_TO(TraverseStmt(NE)); 1095 }) 1096 1097 DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {}) 1098 DEF_TRAVERSE_TYPELOC(TypedefType, {}) 1099 1100 DEF_TRAVERSE_TYPELOC(TypeOfExprType, 1101 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); }) 1102 1103 DEF_TRAVERSE_TYPELOC(TypeOfType, { 1104 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc())); 1105 }) 1106 1107 // FIXME: location of underlying expr 1108 DEF_TRAVERSE_TYPELOC(DecltypeType, { 1109 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr())); 1110 }) 1111 1112 DEF_TRAVERSE_TYPELOC(UnaryTransformType, { 1113 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc())); 1114 }) 1115 1116 DEF_TRAVERSE_TYPELOC(AutoType, { 1117 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType())); 1118 }) 1119 1120 DEF_TRAVERSE_TYPELOC(RecordType, {}) 1121 DEF_TRAVERSE_TYPELOC(EnumType, {}) 1122 DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {}) 1123 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {}) 1124 DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {}) 1125 1126 // FIXME: use the loc for the template name? 1127 DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, { 1128 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName())); 1129 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { 1130 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1131 } 1132 }) 1133 1134 DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {}) 1135 1136 DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) 1137 1138 DEF_TRAVERSE_TYPELOC(AttributedType, 1139 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) 1140 1141 DEF_TRAVERSE_TYPELOC(ElaboratedType, { 1142 if (TL.getQualifierLoc()) { 1143 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1144 } 1145 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc())); 1146 }) 1147 1148 DEF_TRAVERSE_TYPELOC(DependentNameType, { 1149 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1150 }) 1151 1152 DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, { 1153 if (TL.getQualifierLoc()) { 1154 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1155 } 1156 1157 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { 1158 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1159 } 1160 }) 1161 1162 DEF_TRAVERSE_TYPELOC(PackExpansionType, 1163 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); }) 1164 1165 DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {}) 1166 1167 DEF_TRAVERSE_TYPELOC(ObjCObjectType, { 1168 // We have to watch out here because an ObjCInterfaceType's base 1169 // type is itself. 1170 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr()) 1171 TRY_TO(TraverseTypeLoc(TL.getBaseLoc())); 1172 }) 1173 1174 DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, 1175 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1176 1177 DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); }) 1178 1179 #undef DEF_TRAVERSE_TYPELOC 1180 1181 // ----------------- Decl traversal ----------------- 1182 // 1183 // For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing 1184 // the children that come from the DeclContext associated with it. 1185 // Therefore each Traverse* only needs to worry about children other 1186 // than those. 1187 1188 template <typename Derived> 1189 bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) { 1190 if (!DC) 1191 return true; 1192 1193 for (auto *Child : DC->decls()) { 1194 // BlockDecls and CapturedDecls are traversed through BlockExprs and 1195 // CapturedStmts respectively. 1196 if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child)) 1197 TRY_TO(TraverseDecl(Child)); 1198 } 1199 1200 return true; 1201 } 1202 1203 // This macro makes available a variable D, the passed-in decl. 1204 #define DEF_TRAVERSE_DECL(DECL, CODE) \ 1205 template <typename Derived> \ 1206 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \ 1207 TRY_TO(WalkUpFrom##DECL(D)); \ 1208 { CODE; } \ 1209 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \ 1210 return true; \ 1211 } 1212 1213 DEF_TRAVERSE_DECL(AccessSpecDecl, {}) 1214 1215 DEF_TRAVERSE_DECL(BlockDecl, { 1216 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten()) 1217 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 1218 TRY_TO(TraverseStmt(D->getBody())); 1219 for (const auto &I : D->captures()) { 1220 if (I.hasCopyExpr()) { 1221 TRY_TO(TraverseStmt(I.getCopyExpr())); 1222 } 1223 } 1224 // This return statement makes sure the traversal of nodes in 1225 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1226 // is skipped - don't remove it. 1227 return true; 1228 }) 1229 1230 DEF_TRAVERSE_DECL(CapturedDecl, { 1231 TRY_TO(TraverseStmt(D->getBody())); 1232 // This return statement makes sure the traversal of nodes in 1233 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1234 // is skipped - don't remove it. 1235 return true; 1236 }) 1237 1238 DEF_TRAVERSE_DECL(EmptyDecl, {}) 1239 1240 DEF_TRAVERSE_DECL(FileScopeAsmDecl, 1241 { TRY_TO(TraverseStmt(D->getAsmString())); }) 1242 1243 DEF_TRAVERSE_DECL(ImportDecl, {}) 1244 1245 DEF_TRAVERSE_DECL(FriendDecl, { 1246 // Friend is either decl or a type. 1247 if (D->getFriendType()) 1248 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1249 else 1250 TRY_TO(TraverseDecl(D->getFriendDecl())); 1251 }) 1252 1253 DEF_TRAVERSE_DECL(FriendTemplateDecl, { 1254 if (D->getFriendType()) 1255 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1256 else 1257 TRY_TO(TraverseDecl(D->getFriendDecl())); 1258 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) { 1259 TemplateParameterList *TPL = D->getTemplateParameterList(I); 1260 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end(); 1261 ITPL != ETPL; ++ITPL) { 1262 TRY_TO(TraverseDecl(*ITPL)); 1263 } 1264 } 1265 }) 1266 1267 DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, 1268 { TRY_TO(TraverseDecl(D->getSpecialization())); }) 1269 1270 DEF_TRAVERSE_DECL(LinkageSpecDecl, {}) 1271 1272 DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this 1273 }) 1274 1275 DEF_TRAVERSE_DECL(StaticAssertDecl, { 1276 TRY_TO(TraverseStmt(D->getAssertExpr())); 1277 TRY_TO(TraverseStmt(D->getMessage())); 1278 }) 1279 1280 DEF_TRAVERSE_DECL( 1281 TranslationUnitDecl, 1282 {// Code in an unnamed namespace shows up automatically in 1283 // decls_begin()/decls_end(). Thus we don't need to recurse on 1284 // D->getAnonymousNamespace(). 1285 }) 1286 1287 DEF_TRAVERSE_DECL(NamespaceAliasDecl, { 1288 // We shouldn't traverse an aliased namespace, since it will be 1289 // defined (and, therefore, traversed) somewhere else. 1290 // 1291 // This return statement makes sure the traversal of nodes in 1292 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1293 // is skipped - don't remove it. 1294 return true; 1295 }) 1296 1297 DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl. 1298 }) 1299 1300 DEF_TRAVERSE_DECL( 1301 NamespaceDecl, 1302 {// Code in an unnamed namespace shows up automatically in 1303 // decls_begin()/decls_end(). Thus we don't need to recurse on 1304 // D->getAnonymousNamespace(). 1305 }) 1306 1307 DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement 1308 }) 1309 1310 DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement 1311 }) 1312 1313 DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement 1314 }) 1315 1316 DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement 1317 }) 1318 1319 DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement 1320 }) 1321 1322 DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement 1323 }) 1324 1325 DEF_TRAVERSE_DECL(ObjCMethodDecl, { 1326 if (D->getReturnTypeSourceInfo()) { 1327 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc())); 1328 } 1329 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end(); 1330 I != E; ++I) { 1331 TRY_TO(TraverseDecl(*I)); 1332 } 1333 if (D->isThisDeclarationADefinition()) { 1334 TRY_TO(TraverseStmt(D->getBody())); 1335 } 1336 return true; 1337 }) 1338 1339 DEF_TRAVERSE_DECL(ObjCPropertyDecl, { 1340 if (D->getTypeSourceInfo()) 1341 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1342 else 1343 TRY_TO(TraverseType(D->getType())); 1344 return true; 1345 }) 1346 1347 DEF_TRAVERSE_DECL(UsingDecl, { 1348 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1349 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1350 }) 1351 1352 DEF_TRAVERSE_DECL(UsingDirectiveDecl, { 1353 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1354 }) 1355 1356 DEF_TRAVERSE_DECL(UsingShadowDecl, {}) 1357 1358 DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, { 1359 for (auto *I : D->varlists()) { 1360 TRY_TO(TraverseStmt(I)); 1361 } 1362 }) 1363 1364 // A helper method for TemplateDecl's children. 1365 template <typename Derived> 1366 bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper( 1367 TemplateParameterList *TPL) { 1368 if (TPL) { 1369 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1370 I != E; ++I) { 1371 TRY_TO(TraverseDecl(*I)); 1372 } 1373 } 1374 return true; 1375 } 1376 1377 // A helper method for traversing the implicit instantiations of a 1378 // class template. 1379 template <typename Derived> 1380 bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations( 1381 ClassTemplateDecl *D) { 1382 for (auto *SD : D->specializations()) { 1383 for (auto *RD : SD->redecls()) { 1384 // We don't want to visit injected-class-names in this traversal. 1385 if (cast<CXXRecordDecl>(RD)->isInjectedClassName()) 1386 continue; 1387 1388 switch ( 1389 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1390 // Visit the implicit instantiations with the requested pattern. 1391 case TSK_Undeclared: 1392 case TSK_ImplicitInstantiation: 1393 TRY_TO(TraverseDecl(RD)); 1394 break; 1395 1396 // We don't need to do anything on an explicit instantiation 1397 // or explicit specialization because there will be an explicit 1398 // node for it elsewhere. 1399 case TSK_ExplicitInstantiationDeclaration: 1400 case TSK_ExplicitInstantiationDefinition: 1401 case TSK_ExplicitSpecialization: 1402 break; 1403 } 1404 } 1405 } 1406 1407 return true; 1408 } 1409 1410 DEF_TRAVERSE_DECL(ClassTemplateDecl, { 1411 CXXRecordDecl *TempDecl = D->getTemplatedDecl(); 1412 TRY_TO(TraverseDecl(TempDecl)); 1413 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1414 1415 // By default, we do not traverse the instantiations of 1416 // class templates since they do not appear in the user code. The 1417 // following code optionally traverses them. 1418 // 1419 // We only traverse the class instantiations when we see the canonical 1420 // declaration of the template, to ensure we only visit them once. 1421 if (getDerived().shouldVisitTemplateInstantiations() && 1422 D == D->getCanonicalDecl()) 1423 TRY_TO(TraverseClassInstantiations(D)); 1424 1425 // Note that getInstantiatedFromMemberTemplate() is just a link 1426 // from a template instantiation back to the template from which 1427 // it was instantiated, and thus should not be traversed. 1428 }) 1429 1430 // A helper method for traversing the implicit instantiations of a 1431 // class template. 1432 template <typename Derived> 1433 bool RecursiveASTVisitor<Derived>::TraverseVariableInstantiations( 1434 VarTemplateDecl *D) { 1435 for (auto *SD : D->specializations()) { 1436 for (auto *RD : SD->redecls()) { 1437 switch ( 1438 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1439 // Visit the implicit instantiations with the requested pattern. 1440 case TSK_Undeclared: 1441 case TSK_ImplicitInstantiation: 1442 TRY_TO(TraverseDecl(RD)); 1443 break; 1444 1445 // We don't need to do anything on an explicit instantiation 1446 // or explicit specialization because there will be an explicit 1447 // node for it elsewhere. 1448 case TSK_ExplicitInstantiationDeclaration: 1449 case TSK_ExplicitInstantiationDefinition: 1450 case TSK_ExplicitSpecialization: 1451 break; 1452 } 1453 } 1454 } 1455 1456 return true; 1457 } 1458 1459 DEF_TRAVERSE_DECL(VarTemplateDecl, { 1460 VarDecl *TempDecl = D->getTemplatedDecl(); 1461 TRY_TO(TraverseDecl(TempDecl)); 1462 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1463 1464 // By default, we do not traverse the instantiations of 1465 // variable templates since they do not appear in the user code. The 1466 // following code optionally traverses them. 1467 // 1468 // We only traverse the variable instantiations when we see the canonical 1469 // declaration of the template, to ensure we only visit them once. 1470 if (getDerived().shouldVisitTemplateInstantiations() && 1471 D == D->getCanonicalDecl()) 1472 TRY_TO(TraverseVariableInstantiations(D)); 1473 1474 // Note that getInstantiatedFromMemberTemplate() is just a link 1475 // from a template instantiation back to the template from which 1476 // it was instantiated, and thus should not be traversed. 1477 }) 1478 1479 // A helper method for traversing the instantiations of a 1480 // function while skipping its specializations. 1481 template <typename Derived> 1482 bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations( 1483 FunctionTemplateDecl *D) { 1484 for (auto *FD : D->specializations()) { 1485 for (auto *RD : FD->redecls()) { 1486 switch (RD->getTemplateSpecializationKind()) { 1487 case TSK_Undeclared: 1488 case TSK_ImplicitInstantiation: 1489 // We don't know what kind of FunctionDecl this is. 1490 TRY_TO(TraverseDecl(RD)); 1491 break; 1492 1493 // No need to visit explicit instantiations, we'll find the node 1494 // eventually. 1495 // FIXME: This is incorrect; there is no other node for an explicit 1496 // instantiation of a function template specialization. 1497 case TSK_ExplicitInstantiationDeclaration: 1498 case TSK_ExplicitInstantiationDefinition: 1499 break; 1500 1501 case TSK_ExplicitSpecialization: 1502 break; 1503 } 1504 } 1505 } 1506 1507 return true; 1508 } 1509 1510 DEF_TRAVERSE_DECL(FunctionTemplateDecl, { 1511 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1512 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1513 1514 // By default, we do not traverse the instantiations of 1515 // function templates since they do not appear in the user code. The 1516 // following code optionally traverses them. 1517 // 1518 // We only traverse the function instantiations when we see the canonical 1519 // declaration of the template, to ensure we only visit them once. 1520 if (getDerived().shouldVisitTemplateInstantiations() && 1521 D == D->getCanonicalDecl()) 1522 TRY_TO(TraverseFunctionInstantiations(D)); 1523 }) 1524 1525 DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, { 1526 // D is the "T" in something like 1527 // template <template <typename> class T> class container { }; 1528 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1529 if (D->hasDefaultArgument()) { 1530 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument())); 1531 } 1532 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1533 }) 1534 1535 DEF_TRAVERSE_DECL(TemplateTypeParmDecl, { 1536 // D is the "T" in something like "template<typename T> class vector;" 1537 if (D->getTypeForDecl()) 1538 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1539 if (D->hasDefaultArgument()) 1540 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc())); 1541 }) 1542 1543 DEF_TRAVERSE_DECL(TypedefDecl, { 1544 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1545 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1546 // declaring the typedef, not something that was written in the 1547 // source. 1548 }) 1549 1550 DEF_TRAVERSE_DECL(TypeAliasDecl, { 1551 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1552 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1553 // declaring the type alias, not something that was written in the 1554 // source. 1555 }) 1556 1557 DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, { 1558 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1559 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1560 }) 1561 1562 DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, { 1563 // A dependent using declaration which was marked with 'typename'. 1564 // template<class T> class A : public B<T> { using typename B<T>::foo; }; 1565 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1566 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1567 // declaring the type, not something that was written in the 1568 // source. 1569 }) 1570 1571 DEF_TRAVERSE_DECL(EnumDecl, { 1572 if (D->getTypeForDecl()) 1573 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1574 1575 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1576 // The enumerators are already traversed by 1577 // decls_begin()/decls_end(). 1578 }) 1579 1580 // Helper methods for RecordDecl and its children. 1581 template <typename Derived> 1582 bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) { 1583 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1584 // declaring the type, not something that was written in the source. 1585 1586 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1587 return true; 1588 } 1589 1590 template <typename Derived> 1591 bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) { 1592 if (!TraverseRecordHelper(D)) 1593 return false; 1594 if (D->isCompleteDefinition()) { 1595 for (const auto &I : D->bases()) { 1596 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc())); 1597 } 1598 // We don't traverse the friends or the conversions, as they are 1599 // already in decls_begin()/decls_end(). 1600 } 1601 return true; 1602 } 1603 1604 DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) 1605 1606 DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) 1607 1608 DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, { 1609 // For implicit instantiations ("set<int> x;"), we don't want to 1610 // recurse at all, since the instatiated class isn't written in 1611 // the source code anywhere. (Note the instatiated *type* -- 1612 // set<int> -- is written, and will still get a callback of 1613 // TemplateSpecializationType). For explicit instantiations 1614 // ("template set<int>;"), we do need a callback, since this 1615 // is the only callback that's made for this instantiation. 1616 // We use getTypeAsWritten() to distinguish. 1617 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) 1618 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1619 1620 if (!getDerived().shouldVisitTemplateInstantiations() && 1621 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) 1622 // Returning from here skips traversing the 1623 // declaration context of the ClassTemplateSpecializationDecl 1624 // (embedded in the DEF_TRAVERSE_DECL() macro) 1625 // which contains the instantiated members of the class. 1626 return true; 1627 }) 1628 1629 template <typename Derived> 1630 bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper( 1631 const TemplateArgumentLoc *TAL, unsigned Count) { 1632 for (unsigned I = 0; I < Count; ++I) { 1633 TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); 1634 } 1635 return true; 1636 } 1637 1638 DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, { 1639 // The partial specialization. 1640 if (TemplateParameterList *TPL = D->getTemplateParameters()) { 1641 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1642 I != E; ++I) { 1643 TRY_TO(TraverseDecl(*I)); 1644 } 1645 } 1646 // The args that remains unspecialized. 1647 TRY_TO(TraverseTemplateArgumentLocsHelper( 1648 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1649 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1650 1651 // Don't need the ClassTemplatePartialSpecializationHelper, even 1652 // though that's our parent class -- we already visit all the 1653 // template args here. 1654 TRY_TO(TraverseCXXRecordHelper(D)); 1655 1656 // Instantiations will have been visited with the primary template. 1657 }) 1658 1659 DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); }) 1660 1661 DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, { 1662 // Like UnresolvedUsingTypenameDecl, but without the 'typename': 1663 // template <class T> Class A : public Base<T> { using Base<T>::foo; }; 1664 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1665 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1666 }) 1667 1668 DEF_TRAVERSE_DECL(IndirectFieldDecl, {}) 1669 1670 template <typename Derived> 1671 bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) { 1672 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1673 if (D->getTypeSourceInfo()) 1674 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1675 else 1676 TRY_TO(TraverseType(D->getType())); 1677 return true; 1678 } 1679 1680 DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); }) 1681 1682 DEF_TRAVERSE_DECL(FieldDecl, { 1683 TRY_TO(TraverseDeclaratorHelper(D)); 1684 if (D->isBitField()) 1685 TRY_TO(TraverseStmt(D->getBitWidth())); 1686 else if (D->hasInClassInitializer()) 1687 TRY_TO(TraverseStmt(D->getInClassInitializer())); 1688 }) 1689 1690 DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, { 1691 TRY_TO(TraverseDeclaratorHelper(D)); 1692 if (D->isBitField()) 1693 TRY_TO(TraverseStmt(D->getBitWidth())); 1694 // FIXME: implement the rest. 1695 }) 1696 1697 DEF_TRAVERSE_DECL(ObjCIvarDecl, { 1698 TRY_TO(TraverseDeclaratorHelper(D)); 1699 if (D->isBitField()) 1700 TRY_TO(TraverseStmt(D->getBitWidth())); 1701 // FIXME: implement the rest. 1702 }) 1703 1704 template <typename Derived> 1705 bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) { 1706 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1707 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1708 1709 // If we're an explicit template specialization, iterate over the 1710 // template args that were explicitly specified. If we were doing 1711 // this in typing order, we'd do it between the return type and 1712 // the function args, but both are handled by the FunctionTypeLoc 1713 // above, so we have to choose one side. I've decided to do before. 1714 if (const FunctionTemplateSpecializationInfo *FTSI = 1715 D->getTemplateSpecializationInfo()) { 1716 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared && 1717 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { 1718 // A specialization might not have explicit template arguments if it has 1719 // a templated return type and concrete arguments. 1720 if (const ASTTemplateArgumentListInfo *TALI = 1721 FTSI->TemplateArgumentsAsWritten) { 1722 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(), 1723 TALI->NumTemplateArgs)); 1724 } 1725 } 1726 } 1727 1728 // Visit the function type itself, which can be either 1729 // FunctionNoProtoType or FunctionProtoType, or a typedef. This 1730 // also covers the return type and the function parameters, 1731 // including exception specifications. 1732 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1733 1734 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) { 1735 // Constructor initializers. 1736 for (auto *I : Ctor->inits()) { 1737 TRY_TO(TraverseConstructorInitializer(I)); 1738 } 1739 } 1740 1741 if (D->isThisDeclarationADefinition()) { 1742 TRY_TO(TraverseStmt(D->getBody())); // Function body. 1743 } 1744 return true; 1745 } 1746 1747 DEF_TRAVERSE_DECL(FunctionDecl, { 1748 // We skip decls_begin/decls_end, which are already covered by 1749 // TraverseFunctionHelper(). 1750 return TraverseFunctionHelper(D); 1751 }) 1752 1753 DEF_TRAVERSE_DECL(CXXMethodDecl, { 1754 // We skip decls_begin/decls_end, which are already covered by 1755 // TraverseFunctionHelper(). 1756 return TraverseFunctionHelper(D); 1757 }) 1758 1759 DEF_TRAVERSE_DECL(CXXConstructorDecl, { 1760 // We skip decls_begin/decls_end, which are already covered by 1761 // TraverseFunctionHelper(). 1762 return TraverseFunctionHelper(D); 1763 }) 1764 1765 // CXXConversionDecl is the declaration of a type conversion operator. 1766 // It's not a cast expression. 1767 DEF_TRAVERSE_DECL(CXXConversionDecl, { 1768 // We skip decls_begin/decls_end, which are already covered by 1769 // TraverseFunctionHelper(). 1770 return TraverseFunctionHelper(D); 1771 }) 1772 1773 DEF_TRAVERSE_DECL(CXXDestructorDecl, { 1774 // We skip decls_begin/decls_end, which are already covered by 1775 // TraverseFunctionHelper(). 1776 return TraverseFunctionHelper(D); 1777 }) 1778 1779 template <typename Derived> 1780 bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) { 1781 TRY_TO(TraverseDeclaratorHelper(D)); 1782 // Default params are taken care of when we traverse the ParmVarDecl. 1783 if (!isa<ParmVarDecl>(D)) 1784 TRY_TO(TraverseStmt(D->getInit())); 1785 return true; 1786 } 1787 1788 DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); }) 1789 1790 DEF_TRAVERSE_DECL(VarTemplateSpecializationDecl, { 1791 // For implicit instantiations, we don't want to 1792 // recurse at all, since the instatiated class isn't written in 1793 // the source code anywhere. 1794 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) 1795 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1796 1797 if (!getDerived().shouldVisitTemplateInstantiations() && 1798 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) 1799 // Returning from here skips traversing the 1800 // declaration context of the VarTemplateSpecializationDecl 1801 // (embedded in the DEF_TRAVERSE_DECL() macro). 1802 return true; 1803 }) 1804 1805 DEF_TRAVERSE_DECL(VarTemplatePartialSpecializationDecl, { 1806 // The partial specialization. 1807 if (TemplateParameterList *TPL = D->getTemplateParameters()) { 1808 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1809 I != E; ++I) { 1810 TRY_TO(TraverseDecl(*I)); 1811 } 1812 } 1813 // The args that remains unspecialized. 1814 TRY_TO(TraverseTemplateArgumentLocsHelper( 1815 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1816 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1817 1818 // Don't need the VarTemplatePartialSpecializationHelper, even 1819 // though that's our parent class -- we already visit all the 1820 // template args here. 1821 TRY_TO(TraverseVarHelper(D)); 1822 1823 // Instantiations will have been visited with the primary 1824 // template. 1825 }) 1826 1827 DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); }) 1828 1829 DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, { 1830 // A non-type template parameter, e.g. "S" in template<int S> class Foo ... 1831 TRY_TO(TraverseDeclaratorHelper(D)); 1832 TRY_TO(TraverseStmt(D->getDefaultArgument())); 1833 }) 1834 1835 DEF_TRAVERSE_DECL(ParmVarDecl, { 1836 TRY_TO(TraverseVarHelper(D)); 1837 1838 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() && 1839 !D->hasUnparsedDefaultArg()) 1840 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg())); 1841 1842 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() && 1843 !D->hasUnparsedDefaultArg()) 1844 TRY_TO(TraverseStmt(D->getDefaultArg())); 1845 }) 1846 1847 #undef DEF_TRAVERSE_DECL 1848 1849 // ----------------- Stmt traversal ----------------- 1850 // 1851 // For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating 1852 // over the children defined in children() (every stmt defines these, 1853 // though sometimes the range is empty). Each individual Traverse* 1854 // method only needs to worry about children other than those. To see 1855 // what children() does for a given class, see, e.g., 1856 // http://clang.llvm.org/doxygen/Stmt_8cpp_source.html 1857 1858 // This macro makes available a variable S, the passed-in stmt. 1859 #define DEF_TRAVERSE_STMT(STMT, CODE) \ 1860 template <typename Derived> \ 1861 bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) { \ 1862 TRY_TO(WalkUpFrom##STMT(S)); \ 1863 StmtQueueAction StmtQueue(*this); \ 1864 { CODE; } \ 1865 for (Stmt::child_range range = S->children(); range; ++range) { \ 1866 StmtQueue.queue(*range); \ 1867 } \ 1868 return true; \ 1869 } 1870 1871 DEF_TRAVERSE_STMT(GCCAsmStmt, { 1872 StmtQueue.queue(S->getAsmString()); 1873 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) { 1874 StmtQueue.queue(S->getInputConstraintLiteral(I)); 1875 } 1876 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) { 1877 StmtQueue.queue(S->getOutputConstraintLiteral(I)); 1878 } 1879 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) { 1880 StmtQueue.queue(S->getClobberStringLiteral(I)); 1881 } 1882 // children() iterates over inputExpr and outputExpr. 1883 }) 1884 1885 DEF_TRAVERSE_STMT( 1886 MSAsmStmt, 1887 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once 1888 // added this needs to be implemented. 1889 }) 1890 1891 DEF_TRAVERSE_STMT(CXXCatchStmt, { 1892 TRY_TO(TraverseDecl(S->getExceptionDecl())); 1893 // children() iterates over the handler block. 1894 }) 1895 1896 DEF_TRAVERSE_STMT(DeclStmt, { 1897 for (auto *I : S->decls()) { 1898 TRY_TO(TraverseDecl(I)); 1899 } 1900 // Suppress the default iteration over children() by 1901 // returning. Here's why: A DeclStmt looks like 'type var [= 1902 // initializer]'. The decls above already traverse over the 1903 // initializers, so we don't have to do it again (which 1904 // children() would do). 1905 return true; 1906 }) 1907 1908 // These non-expr stmts (most of them), do not need any action except 1909 // iterating over the children. 1910 DEF_TRAVERSE_STMT(BreakStmt, {}) 1911 DEF_TRAVERSE_STMT(CXXTryStmt, {}) 1912 DEF_TRAVERSE_STMT(CaseStmt, {}) 1913 DEF_TRAVERSE_STMT(CompoundStmt, {}) 1914 DEF_TRAVERSE_STMT(ContinueStmt, {}) 1915 DEF_TRAVERSE_STMT(DefaultStmt, {}) 1916 DEF_TRAVERSE_STMT(DoStmt, {}) 1917 DEF_TRAVERSE_STMT(ForStmt, {}) 1918 DEF_TRAVERSE_STMT(GotoStmt, {}) 1919 DEF_TRAVERSE_STMT(IfStmt, {}) 1920 DEF_TRAVERSE_STMT(IndirectGotoStmt, {}) 1921 DEF_TRAVERSE_STMT(LabelStmt, {}) 1922 DEF_TRAVERSE_STMT(AttributedStmt, {}) 1923 DEF_TRAVERSE_STMT(NullStmt, {}) 1924 DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {}) 1925 DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {}) 1926 DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {}) 1927 DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {}) 1928 DEF_TRAVERSE_STMT(ObjCAtTryStmt, {}) 1929 DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {}) 1930 DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {}) 1931 DEF_TRAVERSE_STMT(CXXForRangeStmt, {}) 1932 DEF_TRAVERSE_STMT(MSDependentExistsStmt, { 1933 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1934 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1935 }) 1936 DEF_TRAVERSE_STMT(ReturnStmt, {}) 1937 DEF_TRAVERSE_STMT(SwitchStmt, {}) 1938 DEF_TRAVERSE_STMT(WhileStmt, {}) 1939 1940 DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, { 1941 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1942 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 1943 if (S->hasExplicitTemplateArgs()) { 1944 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1945 S->getNumTemplateArgs())); 1946 } 1947 }) 1948 1949 DEF_TRAVERSE_STMT(DeclRefExpr, { 1950 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1951 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1952 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1953 S->getNumTemplateArgs())); 1954 }) 1955 1956 DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, { 1957 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1958 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1959 if (S->hasExplicitTemplateArgs()) { 1960 TRY_TO(TraverseTemplateArgumentLocsHelper( 1961 S->getExplicitTemplateArgs().getTemplateArgs(), 1962 S->getNumTemplateArgs())); 1963 } 1964 }) 1965 1966 DEF_TRAVERSE_STMT(MemberExpr, { 1967 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1968 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 1969 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1970 S->getNumTemplateArgs())); 1971 }) 1972 1973 DEF_TRAVERSE_STMT( 1974 ImplicitCastExpr, 1975 {// We don't traverse the cast type, as it's not written in the 1976 // source code. 1977 }) 1978 1979 DEF_TRAVERSE_STMT(CStyleCastExpr, { 1980 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1981 }) 1982 1983 DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, { 1984 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1985 }) 1986 1987 DEF_TRAVERSE_STMT(CXXConstCastExpr, { 1988 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1989 }) 1990 1991 DEF_TRAVERSE_STMT(CXXDynamicCastExpr, { 1992 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1993 }) 1994 1995 DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, { 1996 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1997 }) 1998 1999 DEF_TRAVERSE_STMT(CXXStaticCastExpr, { 2000 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2001 }) 2002 2003 // InitListExpr is a tricky one, because we want to do all our work on 2004 // the syntactic form of the listexpr, but this method takes the 2005 // semantic form by default. We can't use the macro helper because it 2006 // calls WalkUp*() on the semantic form, before our code can convert 2007 // to the syntactic form. 2008 template <typename Derived> 2009 bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) { 2010 if (InitListExpr *Syn = S->getSyntacticForm()) 2011 S = Syn; 2012 TRY_TO(WalkUpFromInitListExpr(S)); 2013 StmtQueueAction StmtQueue(*this); 2014 // All we need are the default actions. FIXME: use a helper function. 2015 for (Stmt::child_range range = S->children(); range; ++range) { 2016 StmtQueue.queue(*range); 2017 } 2018 return true; 2019 } 2020 2021 // GenericSelectionExpr is a special case because the types and expressions 2022 // are interleaved. We also need to watch out for null types (default 2023 // generic associations). 2024 template <typename Derived> 2025 bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr( 2026 GenericSelectionExpr *S) { 2027 TRY_TO(WalkUpFromGenericSelectionExpr(S)); 2028 StmtQueueAction StmtQueue(*this); 2029 StmtQueue.queue(S->getControllingExpr()); 2030 for (unsigned i = 0; i != S->getNumAssocs(); ++i) { 2031 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i)) 2032 TRY_TO(TraverseTypeLoc(TS->getTypeLoc())); 2033 StmtQueue.queue(S->getAssocExpr(i)); 2034 } 2035 return true; 2036 } 2037 2038 // PseudoObjectExpr is a special case because of the wierdness with 2039 // syntactic expressions and opaque values. 2040 template <typename Derived> 2041 bool 2042 RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) { 2043 TRY_TO(WalkUpFromPseudoObjectExpr(S)); 2044 StmtQueueAction StmtQueue(*this); 2045 StmtQueue.queue(S->getSyntacticForm()); 2046 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(), 2047 e = S->semantics_end(); 2048 i != e; ++i) { 2049 Expr *sub = *i; 2050 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub)) 2051 sub = OVE->getSourceExpr(); 2052 StmtQueue.queue(sub); 2053 } 2054 return true; 2055 } 2056 2057 DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, { 2058 // This is called for code like 'return T()' where T is a built-in 2059 // (i.e. non-class) type. 2060 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2061 }) 2062 2063 DEF_TRAVERSE_STMT(CXXNewExpr, { 2064 // The child-iterator will pick up the other arguments. 2065 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc())); 2066 }) 2067 2068 DEF_TRAVERSE_STMT(OffsetOfExpr, { 2069 // The child-iterator will pick up the expression representing 2070 // the field. 2071 // FIMXE: for code like offsetof(Foo, a.b.c), should we get 2072 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c? 2073 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2074 }) 2075 2076 DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, { 2077 // The child-iterator will pick up the arg if it's an expression, 2078 // but not if it's a type. 2079 if (S->isArgumentType()) 2080 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc())); 2081 }) 2082 2083 DEF_TRAVERSE_STMT(CXXTypeidExpr, { 2084 // The child-iterator will pick up the arg if it's an expression, 2085 // but not if it's a type. 2086 if (S->isTypeOperand()) 2087 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2088 }) 2089 2090 DEF_TRAVERSE_STMT(MSPropertyRefExpr, { 2091 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2092 }) 2093 2094 DEF_TRAVERSE_STMT(CXXUuidofExpr, { 2095 // The child-iterator will pick up the arg if it's an expression, 2096 // but not if it's a type. 2097 if (S->isTypeOperand()) 2098 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2099 }) 2100 2101 DEF_TRAVERSE_STMT(TypeTraitExpr, { 2102 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 2103 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc())); 2104 }) 2105 2106 DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, { 2107 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc())); 2108 }) 2109 2110 DEF_TRAVERSE_STMT(ExpressionTraitExpr, 2111 { StmtQueue.queue(S->getQueriedExpression()); }) 2112 2113 DEF_TRAVERSE_STMT(VAArgExpr, { 2114 // The child-iterator will pick up the expression argument. 2115 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc())); 2116 }) 2117 2118 DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, { 2119 // This is called for code like 'return T()' where T is a class type. 2120 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2121 }) 2122 2123 // Walk only the visible parts of lambda expressions. 2124 template <typename Derived> 2125 bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) { 2126 TRY_TO(WalkUpFromLambdaExpr(S)); 2127 2128 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(), 2129 CEnd = S->explicit_capture_end(); 2130 C != CEnd; ++C) { 2131 TRY_TO(TraverseLambdaCapture(S, C)); 2132 } 2133 2134 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); 2135 FunctionProtoTypeLoc Proto = TL.castAs<FunctionProtoTypeLoc>(); 2136 2137 if (S->hasExplicitParameters() && S->hasExplicitResultType()) { 2138 // Visit the whole type. 2139 TRY_TO(TraverseTypeLoc(TL)); 2140 } else { 2141 if (S->hasExplicitParameters()) { 2142 // Visit parameters. 2143 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) { 2144 TRY_TO(TraverseDecl(Proto.getParam(I))); 2145 } 2146 } else if (S->hasExplicitResultType()) { 2147 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc())); 2148 } 2149 2150 auto *T = Proto.getTypePtr(); 2151 for (const auto &E : T->exceptions()) { 2152 TRY_TO(TraverseType(E)); 2153 } 2154 2155 if (Expr *NE = T->getNoexceptExpr()) 2156 TRY_TO(TraverseStmt(NE)); 2157 } 2158 2159 TRY_TO(TraverseLambdaBody(S)); 2160 return true; 2161 } 2162 2163 DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, { 2164 // This is called for code like 'T()', where T is a template argument. 2165 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2166 }) 2167 2168 // These expressions all might take explicit template arguments. 2169 // We traverse those if so. FIXME: implement these. 2170 DEF_TRAVERSE_STMT(CXXConstructExpr, {}) 2171 DEF_TRAVERSE_STMT(CallExpr, {}) 2172 DEF_TRAVERSE_STMT(CXXMemberCallExpr, {}) 2173 2174 // These exprs (most of them), do not need any action except iterating 2175 // over the children. 2176 DEF_TRAVERSE_STMT(AddrLabelExpr, {}) 2177 DEF_TRAVERSE_STMT(ArraySubscriptExpr, {}) 2178 DEF_TRAVERSE_STMT(BlockExpr, { 2179 TRY_TO(TraverseDecl(S->getBlockDecl())); 2180 return true; // no child statements to loop through. 2181 }) 2182 DEF_TRAVERSE_STMT(ChooseExpr, {}) 2183 DEF_TRAVERSE_STMT(CompoundLiteralExpr, { 2184 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2185 }) 2186 DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {}) 2187 DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {}) 2188 DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {}) 2189 DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {}) 2190 DEF_TRAVERSE_STMT(CXXDeleteExpr, {}) 2191 DEF_TRAVERSE_STMT(ExprWithCleanups, {}) 2192 DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {}) 2193 DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {}) 2194 DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, { 2195 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2196 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo()) 2197 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc())); 2198 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo()) 2199 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc())); 2200 }) 2201 DEF_TRAVERSE_STMT(CXXThisExpr, {}) 2202 DEF_TRAVERSE_STMT(CXXThrowExpr, {}) 2203 DEF_TRAVERSE_STMT(UserDefinedLiteral, {}) 2204 DEF_TRAVERSE_STMT(DesignatedInitExpr, {}) 2205 DEF_TRAVERSE_STMT(ExtVectorElementExpr, {}) 2206 DEF_TRAVERSE_STMT(GNUNullExpr, {}) 2207 DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {}) 2208 DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {}) 2209 DEF_TRAVERSE_STMT(ObjCEncodeExpr, { 2210 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo()) 2211 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2212 }) 2213 DEF_TRAVERSE_STMT(ObjCIsaExpr, {}) 2214 DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {}) 2215 DEF_TRAVERSE_STMT(ObjCMessageExpr, { 2216 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo()) 2217 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2218 }) 2219 DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {}) 2220 DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {}) 2221 DEF_TRAVERSE_STMT(ObjCProtocolExpr, {}) 2222 DEF_TRAVERSE_STMT(ObjCSelectorExpr, {}) 2223 DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {}) 2224 DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, { 2225 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2226 }) 2227 DEF_TRAVERSE_STMT(ParenExpr, {}) 2228 DEF_TRAVERSE_STMT(ParenListExpr, {}) 2229 DEF_TRAVERSE_STMT(PredefinedExpr, {}) 2230 DEF_TRAVERSE_STMT(ShuffleVectorExpr, {}) 2231 DEF_TRAVERSE_STMT(ConvertVectorExpr, {}) 2232 DEF_TRAVERSE_STMT(StmtExpr, {}) 2233 DEF_TRAVERSE_STMT(UnresolvedLookupExpr, { 2234 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2235 if (S->hasExplicitTemplateArgs()) { 2236 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2237 S->getNumTemplateArgs())); 2238 } 2239 }) 2240 2241 DEF_TRAVERSE_STMT(UnresolvedMemberExpr, { 2242 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2243 if (S->hasExplicitTemplateArgs()) { 2244 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2245 S->getNumTemplateArgs())); 2246 } 2247 }) 2248 2249 DEF_TRAVERSE_STMT(SEHTryStmt, {}) 2250 DEF_TRAVERSE_STMT(SEHExceptStmt, {}) 2251 DEF_TRAVERSE_STMT(SEHFinallyStmt, {}) 2252 DEF_TRAVERSE_STMT(SEHLeaveStmt, {}) 2253 DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); }) 2254 2255 DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {}) 2256 DEF_TRAVERSE_STMT(OpaqueValueExpr, {}) 2257 DEF_TRAVERSE_STMT(TypoExpr, {}) 2258 DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {}) 2259 2260 // These operators (all of them) do not need any action except 2261 // iterating over the children. 2262 DEF_TRAVERSE_STMT(BinaryConditionalOperator, {}) 2263 DEF_TRAVERSE_STMT(ConditionalOperator, {}) 2264 DEF_TRAVERSE_STMT(UnaryOperator, {}) 2265 DEF_TRAVERSE_STMT(BinaryOperator, {}) 2266 DEF_TRAVERSE_STMT(CompoundAssignOperator, {}) 2267 DEF_TRAVERSE_STMT(CXXNoexceptExpr, {}) 2268 DEF_TRAVERSE_STMT(PackExpansionExpr, {}) 2269 DEF_TRAVERSE_STMT(SizeOfPackExpr, {}) 2270 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {}) 2271 DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {}) 2272 DEF_TRAVERSE_STMT(FunctionParmPackExpr, {}) 2273 DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {}) 2274 DEF_TRAVERSE_STMT(CXXFoldExpr, {}) 2275 DEF_TRAVERSE_STMT(AtomicExpr, {}) 2276 2277 // These literals (all of them) do not need any action. 2278 DEF_TRAVERSE_STMT(IntegerLiteral, {}) 2279 DEF_TRAVERSE_STMT(CharacterLiteral, {}) 2280 DEF_TRAVERSE_STMT(FloatingLiteral, {}) 2281 DEF_TRAVERSE_STMT(ImaginaryLiteral, {}) 2282 DEF_TRAVERSE_STMT(StringLiteral, {}) 2283 DEF_TRAVERSE_STMT(ObjCStringLiteral, {}) 2284 DEF_TRAVERSE_STMT(ObjCBoxedExpr, {}) 2285 DEF_TRAVERSE_STMT(ObjCArrayLiteral, {}) 2286 DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {}) 2287 2288 // Traverse OpenCL: AsType, Convert. 2289 DEF_TRAVERSE_STMT(AsTypeExpr, {}) 2290 2291 // OpenMP directives. 2292 template <typename Derived> 2293 bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective( 2294 OMPExecutableDirective *S) { 2295 for (auto *C : S->clauses()) { 2296 TRY_TO(TraverseOMPClause(C)); 2297 } 2298 return true; 2299 } 2300 2301 template <typename Derived> 2302 bool 2303 RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) { 2304 return TraverseOMPExecutableDirective(S); 2305 } 2306 2307 DEF_TRAVERSE_STMT(OMPParallelDirective, 2308 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2309 2310 DEF_TRAVERSE_STMT(OMPSimdDirective, 2311 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2312 2313 DEF_TRAVERSE_STMT(OMPForDirective, 2314 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2315 2316 DEF_TRAVERSE_STMT(OMPForSimdDirective, 2317 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2318 2319 DEF_TRAVERSE_STMT(OMPSectionsDirective, 2320 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2321 2322 DEF_TRAVERSE_STMT(OMPSectionDirective, 2323 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2324 2325 DEF_TRAVERSE_STMT(OMPSingleDirective, 2326 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2327 2328 DEF_TRAVERSE_STMT(OMPMasterDirective, 2329 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2330 2331 DEF_TRAVERSE_STMT(OMPCriticalDirective, { 2332 TRY_TO(TraverseDeclarationNameInfo(S->getDirectiveName())); 2333 TRY_TO(TraverseOMPExecutableDirective(S)); 2334 }) 2335 2336 DEF_TRAVERSE_STMT(OMPParallelForDirective, 2337 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2338 2339 DEF_TRAVERSE_STMT(OMPParallelForSimdDirective, 2340 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2341 2342 DEF_TRAVERSE_STMT(OMPParallelSectionsDirective, 2343 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2344 2345 DEF_TRAVERSE_STMT(OMPTaskDirective, 2346 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2347 2348 DEF_TRAVERSE_STMT(OMPTaskyieldDirective, 2349 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2350 2351 DEF_TRAVERSE_STMT(OMPBarrierDirective, 2352 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2353 2354 DEF_TRAVERSE_STMT(OMPTaskwaitDirective, 2355 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2356 2357 DEF_TRAVERSE_STMT(OMPFlushDirective, 2358 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2359 2360 DEF_TRAVERSE_STMT(OMPOrderedDirective, 2361 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2362 2363 DEF_TRAVERSE_STMT(OMPAtomicDirective, 2364 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2365 2366 DEF_TRAVERSE_STMT(OMPTargetDirective, 2367 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2368 2369 DEF_TRAVERSE_STMT(OMPTeamsDirective, 2370 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2371 2372 // OpenMP clauses. 2373 template <typename Derived> 2374 bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) { 2375 if (!C) 2376 return true; 2377 switch (C->getClauseKind()) { 2378 #define OPENMP_CLAUSE(Name, Class) \ 2379 case OMPC_##Name: \ 2380 TRY_TO(Visit##Class(static_cast<Class *>(C))); \ 2381 break; 2382 #include "clang/Basic/OpenMPKinds.def" 2383 case OMPC_threadprivate: 2384 case OMPC_unknown: 2385 break; 2386 } 2387 return true; 2388 } 2389 2390 template <typename Derived> 2391 bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) { 2392 TRY_TO(TraverseStmt(C->getCondition())); 2393 return true; 2394 } 2395 2396 template <typename Derived> 2397 bool RecursiveASTVisitor<Derived>::VisitOMPFinalClause(OMPFinalClause *C) { 2398 TRY_TO(TraverseStmt(C->getCondition())); 2399 return true; 2400 } 2401 2402 template <typename Derived> 2403 bool 2404 RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 2405 TRY_TO(TraverseStmt(C->getNumThreads())); 2406 return true; 2407 } 2408 2409 template <typename Derived> 2410 bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) { 2411 TRY_TO(TraverseStmt(C->getSafelen())); 2412 return true; 2413 } 2414 2415 template <typename Derived> 2416 bool 2417 RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) { 2418 TRY_TO(TraverseStmt(C->getNumForLoops())); 2419 return true; 2420 } 2421 2422 template <typename Derived> 2423 bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) { 2424 return true; 2425 } 2426 2427 template <typename Derived> 2428 bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) { 2429 return true; 2430 } 2431 2432 template <typename Derived> 2433 bool 2434 RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) { 2435 TRY_TO(TraverseStmt(C->getChunkSize())); 2436 return true; 2437 } 2438 2439 template <typename Derived> 2440 bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) { 2441 return true; 2442 } 2443 2444 template <typename Derived> 2445 bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) { 2446 return true; 2447 } 2448 2449 template <typename Derived> 2450 bool RecursiveASTVisitor<Derived>::VisitOMPUntiedClause(OMPUntiedClause *) { 2451 return true; 2452 } 2453 2454 template <typename Derived> 2455 bool 2456 RecursiveASTVisitor<Derived>::VisitOMPMergeableClause(OMPMergeableClause *) { 2457 return true; 2458 } 2459 2460 template <typename Derived> 2461 bool RecursiveASTVisitor<Derived>::VisitOMPReadClause(OMPReadClause *) { 2462 return true; 2463 } 2464 2465 template <typename Derived> 2466 bool RecursiveASTVisitor<Derived>::VisitOMPWriteClause(OMPWriteClause *) { 2467 return true; 2468 } 2469 2470 template <typename Derived> 2471 bool RecursiveASTVisitor<Derived>::VisitOMPUpdateClause(OMPUpdateClause *) { 2472 return true; 2473 } 2474 2475 template <typename Derived> 2476 bool RecursiveASTVisitor<Derived>::VisitOMPCaptureClause(OMPCaptureClause *) { 2477 return true; 2478 } 2479 2480 template <typename Derived> 2481 bool RecursiveASTVisitor<Derived>::VisitOMPSeqCstClause(OMPSeqCstClause *) { 2482 return true; 2483 } 2484 2485 template <typename Derived> 2486 template <typename T> 2487 bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) { 2488 for (auto *E : Node->varlists()) { 2489 TRY_TO(TraverseStmt(E)); 2490 } 2491 return true; 2492 } 2493 2494 template <typename Derived> 2495 bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) { 2496 TRY_TO(VisitOMPClauseList(C)); 2497 for (auto *E : C->private_copies()) { 2498 TRY_TO(TraverseStmt(E)); 2499 } 2500 return true; 2501 } 2502 2503 template <typename Derived> 2504 bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause( 2505 OMPFirstprivateClause *C) { 2506 TRY_TO(VisitOMPClauseList(C)); 2507 for (auto *E : C->private_copies()) { 2508 TRY_TO(TraverseStmt(E)); 2509 } 2510 for (auto *E : C->inits()) { 2511 TRY_TO(TraverseStmt(E)); 2512 } 2513 return true; 2514 } 2515 2516 template <typename Derived> 2517 bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause( 2518 OMPLastprivateClause *C) { 2519 TRY_TO(VisitOMPClauseList(C)); 2520 return true; 2521 } 2522 2523 template <typename Derived> 2524 bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) { 2525 TRY_TO(VisitOMPClauseList(C)); 2526 return true; 2527 } 2528 2529 template <typename Derived> 2530 bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) { 2531 TRY_TO(TraverseStmt(C->getStep())); 2532 TRY_TO(VisitOMPClauseList(C)); 2533 return true; 2534 } 2535 2536 template <typename Derived> 2537 bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) { 2538 TRY_TO(TraverseStmt(C->getAlignment())); 2539 TRY_TO(VisitOMPClauseList(C)); 2540 return true; 2541 } 2542 2543 template <typename Derived> 2544 bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) { 2545 TRY_TO(VisitOMPClauseList(C)); 2546 return true; 2547 } 2548 2549 template <typename Derived> 2550 bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause( 2551 OMPCopyprivateClause *C) { 2552 TRY_TO(VisitOMPClauseList(C)); 2553 return true; 2554 } 2555 2556 template <typename Derived> 2557 bool 2558 RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) { 2559 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); 2560 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); 2561 TRY_TO(VisitOMPClauseList(C)); 2562 return true; 2563 } 2564 2565 template <typename Derived> 2566 bool RecursiveASTVisitor<Derived>::VisitOMPFlushClause(OMPFlushClause *C) { 2567 TRY_TO(VisitOMPClauseList(C)); 2568 return true; 2569 } 2570 2571 // FIXME: look at the following tricky-seeming exprs to see if we 2572 // need to recurse on anything. These are ones that have methods 2573 // returning decls or qualtypes or nestednamespecifier -- though I'm 2574 // not sure if they own them -- or just seemed very complicated, or 2575 // had lots of sub-types to explore. 2576 // 2577 // VisitOverloadExpr and its children: recurse on template args? etc? 2578 2579 // FIXME: go through all the stmts and exprs again, and see which of them 2580 // create new types, and recurse on the types (TypeLocs?) of those. 2581 // Candidates: 2582 // 2583 // http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html 2584 // http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html 2585 // http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html 2586 // Every class that has getQualifier. 2587 2588 #undef DEF_TRAVERSE_STMT 2589 2590 #undef TRY_TO 2591 2592 #undef RecursiveASTVisitor 2593 2594 } // end namespace clang 2595 2596 #endif // LLVM_CLANG_LIBCLANG_RECURSIVEASTVISITOR_H 2597