DataRecursiveASTVisitor.h revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
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 60namespace 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. 140template <typename Derived> class RecursiveASTVisitor { 141public: 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 411private: 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 TraverseOMPClause(OMPClause *C); 427 bool TraverseOMPExecutableDirective(OMPExecutableDirective *S); 428#define OPENMP_CLAUSE(Name, Class) bool Visit##Class(Class *C); 429#include "clang/Basic/OpenMPKinds.def" 430 /// \brief Process clauses with list of variables. 431 template <typename T> void VisitOMPClauseList(T *Node); 432 433 typedef SmallVector<Stmt *, 16> StmtsTy; 434 typedef SmallVector<StmtsTy *, 4> QueuesTy; 435 436 QueuesTy Queues; 437 438 class NewQueueRAII { 439 RecursiveASTVisitor &RAV; 440 441 public: 442 NewQueueRAII(StmtsTy &queue, RecursiveASTVisitor &RAV) : RAV(RAV) { 443 RAV.Queues.push_back(&queue); 444 } 445 ~NewQueueRAII() { RAV.Queues.pop_back(); } 446 }; 447 448 StmtsTy &getCurrentQueue() { 449 assert(!Queues.empty() && "base TraverseStmt was never called?"); 450 return *Queues.back(); 451 } 452 453public: 454 class StmtQueueAction { 455 StmtsTy &CurrQueue; 456 457 public: 458 explicit StmtQueueAction(RecursiveASTVisitor &RAV) 459 : CurrQueue(RAV.getCurrentQueue()) {} 460 461 void queue(Stmt *S) { CurrQueue.push_back(S); } 462 }; 463}; 464 465#define DISPATCH(NAME, CLASS, VAR) \ 466 return getDerived().Traverse##NAME(static_cast<CLASS *>(VAR)) 467 468template <typename Derived> 469bool RecursiveASTVisitor<Derived>::TraverseStmt(Stmt *S) { 470 if (!S) 471 return true; 472 473 StmtsTy Queue, StmtsToEnqueue; 474 Queue.push_back(S); 475 NewQueueRAII NQ(StmtsToEnqueue, *this); 476 477 while (!Queue.empty()) { 478 S = Queue.pop_back_val(); 479 if (!S) 480 continue; 481 482 StmtsToEnqueue.clear(); 483 484#define DISPATCH_STMT(NAME, CLASS, VAR) \ 485 TRY_TO(Traverse##NAME(static_cast<CLASS *>(VAR))); \ 486 break 487 488 // If we have a binary expr, dispatch to the subcode of the binop. A smart 489 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt 490 // below. 491 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { 492 switch (BinOp->getOpcode()) { 493#define OPERATOR(NAME) \ 494 case BO_##NAME: \ 495 DISPATCH_STMT(Bin##NAME, BinaryOperator, S); 496 497 BINOP_LIST() 498#undef OPERATOR 499#undef BINOP_LIST 500 501#define OPERATOR(NAME) \ 502 case BO_##NAME##Assign: \ 503 DISPATCH_STMT(Bin##NAME##Assign, CompoundAssignOperator, S); 504 505 CAO_LIST() 506#undef OPERATOR 507#undef CAO_LIST 508 } 509 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) { 510 switch (UnOp->getOpcode()) { 511#define OPERATOR(NAME) \ 512 case UO_##NAME: \ 513 DISPATCH_STMT(Unary##NAME, UnaryOperator, S); 514 515 UNARYOP_LIST() 516#undef OPERATOR 517#undef UNARYOP_LIST 518 } 519 } else { 520 521 // Top switch stmt: dispatch to TraverseFooStmt for each concrete FooStmt. 522 switch (S->getStmtClass()) { 523 case Stmt::NoStmtClass: 524 break; 525#define ABSTRACT_STMT(STMT) 526#define STMT(CLASS, PARENT) \ 527 case Stmt::CLASS##Class: \ 528 DISPATCH_STMT(CLASS, CLASS, S); 529#include "clang/AST/StmtNodes.inc" 530 } 531 } 532 533 for (SmallVectorImpl<Stmt *>::reverse_iterator RI = StmtsToEnqueue.rbegin(), 534 RE = StmtsToEnqueue.rend(); 535 RI != RE; ++RI) 536 Queue.push_back(*RI); 537 } 538 539 return true; 540} 541 542#undef DISPATCH_STMT 543 544template <typename Derived> 545bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) { 546 if (T.isNull()) 547 return true; 548 549 switch (T->getTypeClass()) { 550#define ABSTRACT_TYPE(CLASS, BASE) 551#define TYPE(CLASS, BASE) \ 552 case Type::CLASS: \ 553 DISPATCH(CLASS##Type, CLASS##Type, const_cast<Type *>(T.getTypePtr())); 554#include "clang/AST/TypeNodes.def" 555 } 556 557 return true; 558} 559 560template <typename Derived> 561bool RecursiveASTVisitor<Derived>::TraverseTypeLoc(TypeLoc TL) { 562 if (TL.isNull()) 563 return true; 564 565 switch (TL.getTypeLocClass()) { 566#define ABSTRACT_TYPELOC(CLASS, BASE) 567#define TYPELOC(CLASS, BASE) \ 568 case TypeLoc::CLASS: \ 569 return getDerived().Traverse##CLASS##TypeLoc(TL.castAs<CLASS##TypeLoc>()); 570#include "clang/AST/TypeLocNodes.def" 571 } 572 573 return true; 574} 575 576// Define the Traverse*Attr(Attr* A) methods 577#define VISITORCLASS RecursiveASTVisitor 578#include "clang/AST/AttrVisitor.inc" 579#undef VISITORCLASS 580 581template <typename Derived> 582bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) { 583 if (!D) 584 return true; 585 586 // As a syntax visitor, we want to ignore declarations for 587 // implicitly-defined declarations (ones not typed explicitly by the 588 // user). 589 if (D->isImplicit()) 590 return true; 591 592 switch (D->getKind()) { 593#define ABSTRACT_DECL(DECL) 594#define DECL(CLASS, BASE) \ 595 case Decl::CLASS: \ 596 if (!getDerived().Traverse##CLASS##Decl(static_cast<CLASS##Decl *>(D))) \ 597 return false; \ 598 break; 599#include "clang/AST/DeclNodes.inc" 600 } 601 602 // Visit any attributes attached to this declaration. 603 for (auto *I : D->attrs()) { 604 if (!getDerived().TraverseAttr(I)) 605 return false; 606 } 607 return true; 608} 609 610#undef DISPATCH 611 612template <typename Derived> 613bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifier( 614 NestedNameSpecifier *NNS) { 615 if (!NNS) 616 return true; 617 618 if (NNS->getPrefix()) 619 TRY_TO(TraverseNestedNameSpecifier(NNS->getPrefix())); 620 621 switch (NNS->getKind()) { 622 case NestedNameSpecifier::Identifier: 623 case NestedNameSpecifier::Namespace: 624 case NestedNameSpecifier::NamespaceAlias: 625 case NestedNameSpecifier::Global: 626 return true; 627 628 case NestedNameSpecifier::TypeSpec: 629 case NestedNameSpecifier::TypeSpecWithTemplate: 630 TRY_TO(TraverseType(QualType(NNS->getAsType(), 0))); 631 } 632 633 return true; 634} 635 636template <typename Derived> 637bool RecursiveASTVisitor<Derived>::TraverseNestedNameSpecifierLoc( 638 NestedNameSpecifierLoc NNS) { 639 if (!NNS) 640 return true; 641 642 if (NestedNameSpecifierLoc Prefix = NNS.getPrefix()) 643 TRY_TO(TraverseNestedNameSpecifierLoc(Prefix)); 644 645 switch (NNS.getNestedNameSpecifier()->getKind()) { 646 case NestedNameSpecifier::Identifier: 647 case NestedNameSpecifier::Namespace: 648 case NestedNameSpecifier::NamespaceAlias: 649 case NestedNameSpecifier::Global: 650 return true; 651 652 case NestedNameSpecifier::TypeSpec: 653 case NestedNameSpecifier::TypeSpecWithTemplate: 654 TRY_TO(TraverseTypeLoc(NNS.getTypeLoc())); 655 break; 656 } 657 658 return true; 659} 660 661template <typename Derived> 662bool RecursiveASTVisitor<Derived>::TraverseDeclarationNameInfo( 663 DeclarationNameInfo NameInfo) { 664 switch (NameInfo.getName().getNameKind()) { 665 case DeclarationName::CXXConstructorName: 666 case DeclarationName::CXXDestructorName: 667 case DeclarationName::CXXConversionFunctionName: 668 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) 669 TRY_TO(TraverseTypeLoc(TSInfo->getTypeLoc())); 670 671 break; 672 673 case DeclarationName::Identifier: 674 case DeclarationName::ObjCZeroArgSelector: 675 case DeclarationName::ObjCOneArgSelector: 676 case DeclarationName::ObjCMultiArgSelector: 677 case DeclarationName::CXXOperatorName: 678 case DeclarationName::CXXLiteralOperatorName: 679 case DeclarationName::CXXUsingDirective: 680 break; 681 } 682 683 return true; 684} 685 686template <typename Derived> 687bool RecursiveASTVisitor<Derived>::TraverseTemplateName(TemplateName Template) { 688 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) 689 TRY_TO(TraverseNestedNameSpecifier(DTN->getQualifier())); 690 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) 691 TRY_TO(TraverseNestedNameSpecifier(QTN->getQualifier())); 692 693 return true; 694} 695 696template <typename Derived> 697bool RecursiveASTVisitor<Derived>::TraverseTemplateArgument( 698 const TemplateArgument &Arg) { 699 switch (Arg.getKind()) { 700 case TemplateArgument::Null: 701 case TemplateArgument::Declaration: 702 case TemplateArgument::Integral: 703 case TemplateArgument::NullPtr: 704 return true; 705 706 case TemplateArgument::Type: 707 return getDerived().TraverseType(Arg.getAsType()); 708 709 case TemplateArgument::Template: 710 case TemplateArgument::TemplateExpansion: 711 return getDerived().TraverseTemplateName( 712 Arg.getAsTemplateOrTemplatePattern()); 713 714 case TemplateArgument::Expression: 715 return getDerived().TraverseStmt(Arg.getAsExpr()); 716 717 case TemplateArgument::Pack: 718 return getDerived().TraverseTemplateArguments(Arg.pack_begin(), 719 Arg.pack_size()); 720 } 721 722 return true; 723} 724 725// FIXME: no template name location? 726// FIXME: no source locations for a template argument pack? 727template <typename Derived> 728bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLoc( 729 const TemplateArgumentLoc &ArgLoc) { 730 const TemplateArgument &Arg = ArgLoc.getArgument(); 731 732 switch (Arg.getKind()) { 733 case TemplateArgument::Null: 734 case TemplateArgument::Declaration: 735 case TemplateArgument::Integral: 736 case TemplateArgument::NullPtr: 737 return true; 738 739 case TemplateArgument::Type: { 740 // FIXME: how can TSI ever be NULL? 741 if (TypeSourceInfo *TSI = ArgLoc.getTypeSourceInfo()) 742 return getDerived().TraverseTypeLoc(TSI->getTypeLoc()); 743 else 744 return getDerived().TraverseType(Arg.getAsType()); 745 } 746 747 case TemplateArgument::Template: 748 case TemplateArgument::TemplateExpansion: 749 if (ArgLoc.getTemplateQualifierLoc()) 750 TRY_TO(getDerived().TraverseNestedNameSpecifierLoc( 751 ArgLoc.getTemplateQualifierLoc())); 752 return getDerived().TraverseTemplateName( 753 Arg.getAsTemplateOrTemplatePattern()); 754 755 case TemplateArgument::Expression: 756 return getDerived().TraverseStmt(ArgLoc.getSourceExpression()); 757 758 case TemplateArgument::Pack: 759 return getDerived().TraverseTemplateArguments(Arg.pack_begin(), 760 Arg.pack_size()); 761 } 762 763 return true; 764} 765 766template <typename Derived> 767bool RecursiveASTVisitor<Derived>::TraverseTemplateArguments( 768 const TemplateArgument *Args, unsigned NumArgs) { 769 for (unsigned I = 0; I != NumArgs; ++I) { 770 TRY_TO(TraverseTemplateArgument(Args[I])); 771 } 772 773 return true; 774} 775 776template <typename Derived> 777bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer( 778 CXXCtorInitializer *Init) { 779 if (TypeSourceInfo *TInfo = Init->getTypeSourceInfo()) 780 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 781 782 if (Init->isWritten()) 783 TRY_TO(TraverseStmt(Init->getInit())); 784 return true; 785} 786 787template <typename Derived> 788bool 789RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE, 790 const LambdaCapture *C) { 791 if (C->isInitCapture()) 792 TRY_TO(TraverseDecl(C->getCapturedVar())); 793 return true; 794} 795 796template <typename Derived> 797bool RecursiveASTVisitor<Derived>::TraverseLambdaBody(LambdaExpr *LE) { 798 StmtQueueAction StmtQueue(*this); 799 StmtQueue.queue(LE->getBody()); 800 return true; 801} 802 803// ----------------- Type traversal ----------------- 804 805// This macro makes available a variable T, the passed-in type. 806#define DEF_TRAVERSE_TYPE(TYPE, CODE) \ 807 template <typename Derived> \ 808 bool RecursiveASTVisitor<Derived>::Traverse##TYPE(TYPE *T) { \ 809 TRY_TO(WalkUpFrom##TYPE(T)); \ 810 { CODE; } \ 811 return true; \ 812 } 813 814DEF_TRAVERSE_TYPE(BuiltinType, {}) 815 816DEF_TRAVERSE_TYPE(ComplexType, { TRY_TO(TraverseType(T->getElementType())); }) 817 818DEF_TRAVERSE_TYPE(PointerType, { TRY_TO(TraverseType(T->getPointeeType())); }) 819 820DEF_TRAVERSE_TYPE(BlockPointerType, 821 { TRY_TO(TraverseType(T->getPointeeType())); }) 822 823DEF_TRAVERSE_TYPE(LValueReferenceType, 824 { TRY_TO(TraverseType(T->getPointeeType())); }) 825 826DEF_TRAVERSE_TYPE(RValueReferenceType, 827 { TRY_TO(TraverseType(T->getPointeeType())); }) 828 829DEF_TRAVERSE_TYPE(MemberPointerType, { 830 TRY_TO(TraverseType(QualType(T->getClass(), 0))); 831 TRY_TO(TraverseType(T->getPointeeType())); 832}) 833 834DEF_TRAVERSE_TYPE(AdjustedType, { TRY_TO(TraverseType(T->getOriginalType())); }) 835 836DEF_TRAVERSE_TYPE(DecayedType, { TRY_TO(TraverseType(T->getOriginalType())); }) 837 838DEF_TRAVERSE_TYPE(ConstantArrayType, 839 { TRY_TO(TraverseType(T->getElementType())); }) 840 841DEF_TRAVERSE_TYPE(IncompleteArrayType, 842 { TRY_TO(TraverseType(T->getElementType())); }) 843 844DEF_TRAVERSE_TYPE(VariableArrayType, { 845 TRY_TO(TraverseType(T->getElementType())); 846 TRY_TO(TraverseStmt(T->getSizeExpr())); 847}) 848 849DEF_TRAVERSE_TYPE(DependentSizedArrayType, { 850 TRY_TO(TraverseType(T->getElementType())); 851 if (T->getSizeExpr()) 852 TRY_TO(TraverseStmt(T->getSizeExpr())); 853}) 854 855DEF_TRAVERSE_TYPE(DependentSizedExtVectorType, { 856 if (T->getSizeExpr()) 857 TRY_TO(TraverseStmt(T->getSizeExpr())); 858 TRY_TO(TraverseType(T->getElementType())); 859}) 860 861DEF_TRAVERSE_TYPE(VectorType, { TRY_TO(TraverseType(T->getElementType())); }) 862 863DEF_TRAVERSE_TYPE(ExtVectorType, { TRY_TO(TraverseType(T->getElementType())); }) 864 865DEF_TRAVERSE_TYPE(FunctionNoProtoType, 866 { TRY_TO(TraverseType(T->getReturnType())); }) 867 868DEF_TRAVERSE_TYPE(FunctionProtoType, { 869 TRY_TO(TraverseType(T->getReturnType())); 870 871 for (const auto &A : T->param_types()) { 872 TRY_TO(TraverseType(A)); 873 } 874 875 for (const auto &E : T->exceptions()) { 876 TRY_TO(TraverseType(E)); 877 } 878}) 879 880DEF_TRAVERSE_TYPE(UnresolvedUsingType, {}) 881DEF_TRAVERSE_TYPE(TypedefType, {}) 882 883DEF_TRAVERSE_TYPE(TypeOfExprType, 884 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) 885 886DEF_TRAVERSE_TYPE(TypeOfType, { TRY_TO(TraverseType(T->getUnderlyingType())); }) 887 888DEF_TRAVERSE_TYPE(DecltypeType, 889 { TRY_TO(TraverseStmt(T->getUnderlyingExpr())); }) 890 891DEF_TRAVERSE_TYPE(UnaryTransformType, { 892 TRY_TO(TraverseType(T->getBaseType())); 893 TRY_TO(TraverseType(T->getUnderlyingType())); 894}) 895 896DEF_TRAVERSE_TYPE(AutoType, { TRY_TO(TraverseType(T->getDeducedType())); }) 897 898DEF_TRAVERSE_TYPE(RecordType, {}) 899DEF_TRAVERSE_TYPE(EnumType, {}) 900DEF_TRAVERSE_TYPE(TemplateTypeParmType, {}) 901DEF_TRAVERSE_TYPE(SubstTemplateTypeParmType, {}) 902DEF_TRAVERSE_TYPE(SubstTemplateTypeParmPackType, {}) 903 904DEF_TRAVERSE_TYPE(TemplateSpecializationType, { 905 TRY_TO(TraverseTemplateName(T->getTemplateName())); 906 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 907}) 908 909DEF_TRAVERSE_TYPE(InjectedClassNameType, {}) 910 911DEF_TRAVERSE_TYPE(AttributedType, 912 { TRY_TO(TraverseType(T->getModifiedType())); }) 913 914DEF_TRAVERSE_TYPE(ParenType, { TRY_TO(TraverseType(T->getInnerType())); }) 915 916DEF_TRAVERSE_TYPE(ElaboratedType, { 917 if (T->getQualifier()) { 918 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); 919 } 920 TRY_TO(TraverseType(T->getNamedType())); 921}) 922 923DEF_TRAVERSE_TYPE(DependentNameType, 924 { TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); }) 925 926DEF_TRAVERSE_TYPE(DependentTemplateSpecializationType, { 927 TRY_TO(TraverseNestedNameSpecifier(T->getQualifier())); 928 TRY_TO(TraverseTemplateArguments(T->getArgs(), T->getNumArgs())); 929}) 930 931DEF_TRAVERSE_TYPE(PackExpansionType, { TRY_TO(TraverseType(T->getPattern())); }) 932 933DEF_TRAVERSE_TYPE(ObjCInterfaceType, {}) 934 935DEF_TRAVERSE_TYPE(ObjCObjectType, { 936 // We have to watch out here because an ObjCInterfaceType's base 937 // type is itself. 938 if (T->getBaseType().getTypePtr() != T) 939 TRY_TO(TraverseType(T->getBaseType())); 940}) 941 942DEF_TRAVERSE_TYPE(ObjCObjectPointerType, 943 { TRY_TO(TraverseType(T->getPointeeType())); }) 944 945DEF_TRAVERSE_TYPE(AtomicType, { TRY_TO(TraverseType(T->getValueType())); }) 946 947#undef DEF_TRAVERSE_TYPE 948 949// ----------------- TypeLoc traversal ----------------- 950 951// This macro makes available a variable TL, the passed-in TypeLoc. 952// If requested, it calls WalkUpFrom* for the Type in the given TypeLoc, 953// in addition to WalkUpFrom* for the TypeLoc itself, such that existing 954// clients that override the WalkUpFrom*Type() and/or Visit*Type() methods 955// continue to work. 956#define DEF_TRAVERSE_TYPELOC(TYPE, CODE) \ 957 template <typename Derived> \ 958 bool RecursiveASTVisitor<Derived>::Traverse##TYPE##Loc(TYPE##Loc TL) { \ 959 if (getDerived().shouldWalkTypesOfTypeLocs()) \ 960 TRY_TO(WalkUpFrom##TYPE(const_cast<TYPE *>(TL.getTypePtr()))); \ 961 TRY_TO(WalkUpFrom##TYPE##Loc(TL)); \ 962 { CODE; } \ 963 return true; \ 964 } 965 966template <typename Derived> 967bool 968RecursiveASTVisitor<Derived>::TraverseQualifiedTypeLoc(QualifiedTypeLoc TL) { 969 // Move this over to the 'main' typeloc tree. Note that this is a 970 // move -- we pretend that we were really looking at the unqualified 971 // typeloc all along -- rather than a recursion, so we don't follow 972 // the normal CRTP plan of going through 973 // getDerived().TraverseTypeLoc. If we did, we'd be traversing 974 // twice for the same type (once as a QualifiedTypeLoc version of 975 // the type, once as an UnqualifiedTypeLoc version of the type), 976 // which in effect means we'd call VisitTypeLoc twice with the 977 // 'same' type. This solves that problem, at the cost of never 978 // seeing the qualified version of the type (unless the client 979 // subclasses TraverseQualifiedTypeLoc themselves). It's not a 980 // perfect solution. A perfect solution probably requires making 981 // QualifiedTypeLoc a wrapper around TypeLoc -- like QualType is a 982 // wrapper around Type* -- rather than being its own class in the 983 // type hierarchy. 984 return TraverseTypeLoc(TL.getUnqualifiedLoc()); 985} 986 987DEF_TRAVERSE_TYPELOC(BuiltinType, {}) 988 989// FIXME: ComplexTypeLoc is unfinished 990DEF_TRAVERSE_TYPELOC(ComplexType, { 991 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 992}) 993 994DEF_TRAVERSE_TYPELOC(PointerType, 995 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 996 997DEF_TRAVERSE_TYPELOC(BlockPointerType, 998 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 999 1000DEF_TRAVERSE_TYPELOC(LValueReferenceType, 1001 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1002 1003DEF_TRAVERSE_TYPELOC(RValueReferenceType, 1004 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1005 1006// FIXME: location of base class? 1007// We traverse this in the type case as well, but how is it not reached through 1008// the pointee type? 1009DEF_TRAVERSE_TYPELOC(MemberPointerType, { 1010 TRY_TO(TraverseType(QualType(TL.getTypePtr()->getClass(), 0))); 1011 TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); 1012}) 1013 1014DEF_TRAVERSE_TYPELOC(AdjustedType, 1015 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) 1016 1017DEF_TRAVERSE_TYPELOC(DecayedType, 1018 { TRY_TO(TraverseTypeLoc(TL.getOriginalLoc())); }) 1019 1020template <typename Derived> 1021bool RecursiveASTVisitor<Derived>::TraverseArrayTypeLocHelper(ArrayTypeLoc TL) { 1022 // This isn't available for ArrayType, but is for the ArrayTypeLoc. 1023 TRY_TO(TraverseStmt(TL.getSizeExpr())); 1024 return true; 1025} 1026 1027DEF_TRAVERSE_TYPELOC(ConstantArrayType, { 1028 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1029 return TraverseArrayTypeLocHelper(TL); 1030}) 1031 1032DEF_TRAVERSE_TYPELOC(IncompleteArrayType, { 1033 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1034 return TraverseArrayTypeLocHelper(TL); 1035}) 1036 1037DEF_TRAVERSE_TYPELOC(VariableArrayType, { 1038 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1039 return TraverseArrayTypeLocHelper(TL); 1040}) 1041 1042DEF_TRAVERSE_TYPELOC(DependentSizedArrayType, { 1043 TRY_TO(TraverseTypeLoc(TL.getElementLoc())); 1044 return TraverseArrayTypeLocHelper(TL); 1045}) 1046 1047// FIXME: order? why not size expr first? 1048// FIXME: base VectorTypeLoc is unfinished 1049DEF_TRAVERSE_TYPELOC(DependentSizedExtVectorType, { 1050 if (TL.getTypePtr()->getSizeExpr()) 1051 TRY_TO(TraverseStmt(TL.getTypePtr()->getSizeExpr())); 1052 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1053}) 1054 1055// FIXME: VectorTypeLoc is unfinished 1056DEF_TRAVERSE_TYPELOC(VectorType, { 1057 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1058}) 1059 1060// FIXME: size and attributes 1061// FIXME: base VectorTypeLoc is unfinished 1062DEF_TRAVERSE_TYPELOC(ExtVectorType, { 1063 TRY_TO(TraverseType(TL.getTypePtr()->getElementType())); 1064}) 1065 1066DEF_TRAVERSE_TYPELOC(FunctionNoProtoType, 1067 { TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); }) 1068 1069// FIXME: location of exception specifications (attributes?) 1070DEF_TRAVERSE_TYPELOC(FunctionProtoType, { 1071 TRY_TO(TraverseTypeLoc(TL.getReturnLoc())); 1072 1073 const FunctionProtoType *T = TL.getTypePtr(); 1074 1075 for (unsigned I = 0, E = TL.getNumParams(); I != E; ++I) { 1076 if (TL.getParam(I)) { 1077 TRY_TO(TraverseDecl(TL.getParam(I))); 1078 } else if (I < T->getNumParams()) { 1079 TRY_TO(TraverseType(T->getParamType(I))); 1080 } 1081 } 1082 1083 for (const auto &E : T->exceptions()) { 1084 TRY_TO(TraverseType(E)); 1085 } 1086}) 1087 1088DEF_TRAVERSE_TYPELOC(UnresolvedUsingType, {}) 1089DEF_TRAVERSE_TYPELOC(TypedefType, {}) 1090 1091DEF_TRAVERSE_TYPELOC(TypeOfExprType, 1092 { TRY_TO(TraverseStmt(TL.getUnderlyingExpr())); }) 1093 1094DEF_TRAVERSE_TYPELOC(TypeOfType, { 1095 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc())); 1096}) 1097 1098// FIXME: location of underlying expr 1099DEF_TRAVERSE_TYPELOC(DecltypeType, { 1100 TRY_TO(TraverseStmt(TL.getTypePtr()->getUnderlyingExpr())); 1101}) 1102 1103DEF_TRAVERSE_TYPELOC(UnaryTransformType, { 1104 TRY_TO(TraverseTypeLoc(TL.getUnderlyingTInfo()->getTypeLoc())); 1105}) 1106 1107DEF_TRAVERSE_TYPELOC(AutoType, { 1108 TRY_TO(TraverseType(TL.getTypePtr()->getDeducedType())); 1109}) 1110 1111DEF_TRAVERSE_TYPELOC(RecordType, {}) 1112DEF_TRAVERSE_TYPELOC(EnumType, {}) 1113DEF_TRAVERSE_TYPELOC(TemplateTypeParmType, {}) 1114DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmType, {}) 1115DEF_TRAVERSE_TYPELOC(SubstTemplateTypeParmPackType, {}) 1116 1117// FIXME: use the loc for the template name? 1118DEF_TRAVERSE_TYPELOC(TemplateSpecializationType, { 1119 TRY_TO(TraverseTemplateName(TL.getTypePtr()->getTemplateName())); 1120 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { 1121 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1122 } 1123}) 1124 1125DEF_TRAVERSE_TYPELOC(InjectedClassNameType, {}) 1126 1127DEF_TRAVERSE_TYPELOC(ParenType, { TRY_TO(TraverseTypeLoc(TL.getInnerLoc())); }) 1128 1129DEF_TRAVERSE_TYPELOC(AttributedType, 1130 { TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); }) 1131 1132DEF_TRAVERSE_TYPELOC(ElaboratedType, { 1133 if (TL.getQualifierLoc()) { 1134 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1135 } 1136 TRY_TO(TraverseTypeLoc(TL.getNamedTypeLoc())); 1137}) 1138 1139DEF_TRAVERSE_TYPELOC(DependentNameType, { 1140 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1141}) 1142 1143DEF_TRAVERSE_TYPELOC(DependentTemplateSpecializationType, { 1144 if (TL.getQualifierLoc()) { 1145 TRY_TO(TraverseNestedNameSpecifierLoc(TL.getQualifierLoc())); 1146 } 1147 1148 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { 1149 TRY_TO(TraverseTemplateArgumentLoc(TL.getArgLoc(I))); 1150 } 1151}) 1152 1153DEF_TRAVERSE_TYPELOC(PackExpansionType, 1154 { TRY_TO(TraverseTypeLoc(TL.getPatternLoc())); }) 1155 1156DEF_TRAVERSE_TYPELOC(ObjCInterfaceType, {}) 1157 1158DEF_TRAVERSE_TYPELOC(ObjCObjectType, { 1159 // We have to watch out here because an ObjCInterfaceType's base 1160 // type is itself. 1161 if (TL.getTypePtr()->getBaseType().getTypePtr() != TL.getTypePtr()) 1162 TRY_TO(TraverseTypeLoc(TL.getBaseLoc())); 1163}) 1164 1165DEF_TRAVERSE_TYPELOC(ObjCObjectPointerType, 1166 { TRY_TO(TraverseTypeLoc(TL.getPointeeLoc())); }) 1167 1168DEF_TRAVERSE_TYPELOC(AtomicType, { TRY_TO(TraverseTypeLoc(TL.getValueLoc())); }) 1169 1170#undef DEF_TRAVERSE_TYPELOC 1171 1172// ----------------- Decl traversal ----------------- 1173// 1174// For a Decl, we automate (in the DEF_TRAVERSE_DECL macro) traversing 1175// the children that come from the DeclContext associated with it. 1176// Therefore each Traverse* only needs to worry about children other 1177// than those. 1178 1179template <typename Derived> 1180bool RecursiveASTVisitor<Derived>::TraverseDeclContextHelper(DeclContext *DC) { 1181 if (!DC) 1182 return true; 1183 1184 for (auto *Child : DC->decls()) { 1185 // BlockDecls and CapturedDecls are traversed through BlockExprs and 1186 // CapturedStmts respectively. 1187 if (!isa<BlockDecl>(Child) && !isa<CapturedDecl>(Child)) 1188 TRY_TO(TraverseDecl(Child)); 1189 } 1190 1191 return true; 1192} 1193 1194// This macro makes available a variable D, the passed-in decl. 1195#define DEF_TRAVERSE_DECL(DECL, CODE) \ 1196 template <typename Derived> \ 1197 bool RecursiveASTVisitor<Derived>::Traverse##DECL(DECL *D) { \ 1198 TRY_TO(WalkUpFrom##DECL(D)); \ 1199 { CODE; } \ 1200 TRY_TO(TraverseDeclContextHelper(dyn_cast<DeclContext>(D))); \ 1201 return true; \ 1202 } 1203 1204DEF_TRAVERSE_DECL(AccessSpecDecl, {}) 1205 1206DEF_TRAVERSE_DECL(BlockDecl, { 1207 if (TypeSourceInfo *TInfo = D->getSignatureAsWritten()) 1208 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 1209 TRY_TO(TraverseStmt(D->getBody())); 1210 // This return statement makes sure the traversal of nodes in 1211 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1212 // is skipped - don't remove it. 1213 return true; 1214}) 1215 1216DEF_TRAVERSE_DECL(CapturedDecl, { 1217 TRY_TO(TraverseStmt(D->getBody())); 1218 // This return statement makes sure the traversal of nodes in 1219 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1220 // is skipped - don't remove it. 1221 return true; 1222}) 1223 1224DEF_TRAVERSE_DECL(EmptyDecl, {}) 1225 1226DEF_TRAVERSE_DECL(FileScopeAsmDecl, 1227 { TRY_TO(TraverseStmt(D->getAsmString())); }) 1228 1229DEF_TRAVERSE_DECL(ImportDecl, {}) 1230 1231DEF_TRAVERSE_DECL(FriendDecl, { 1232 // Friend is either decl or a type. 1233 if (D->getFriendType()) 1234 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1235 else 1236 TRY_TO(TraverseDecl(D->getFriendDecl())); 1237}) 1238 1239DEF_TRAVERSE_DECL(FriendTemplateDecl, { 1240 if (D->getFriendType()) 1241 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1242 else 1243 TRY_TO(TraverseDecl(D->getFriendDecl())); 1244 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) { 1245 TemplateParameterList *TPL = D->getTemplateParameterList(I); 1246 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end(); 1247 ITPL != ETPL; ++ITPL) { 1248 TRY_TO(TraverseDecl(*ITPL)); 1249 } 1250 } 1251}) 1252 1253DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, 1254 { TRY_TO(TraverseDecl(D->getSpecialization())); }) 1255 1256DEF_TRAVERSE_DECL(LinkageSpecDecl, {}) 1257 1258DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this 1259 }) 1260 1261DEF_TRAVERSE_DECL(StaticAssertDecl, { 1262 TRY_TO(TraverseStmt(D->getAssertExpr())); 1263 TRY_TO(TraverseStmt(D->getMessage())); 1264}) 1265 1266DEF_TRAVERSE_DECL( 1267 TranslationUnitDecl, 1268 {// Code in an unnamed namespace shows up automatically in 1269 // decls_begin()/decls_end(). Thus we don't need to recurse on 1270 // D->getAnonymousNamespace(). 1271 }) 1272 1273DEF_TRAVERSE_DECL(NamespaceAliasDecl, { 1274 // We shouldn't traverse an aliased namespace, since it will be 1275 // defined (and, therefore, traversed) somewhere else. 1276 // 1277 // This return statement makes sure the traversal of nodes in 1278 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1279 // is skipped - don't remove it. 1280 return true; 1281}) 1282 1283DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl. 1284 }) 1285 1286DEF_TRAVERSE_DECL( 1287 NamespaceDecl, 1288 {// Code in an unnamed namespace shows up automatically in 1289 // decls_begin()/decls_end(). Thus we don't need to recurse on 1290 // D->getAnonymousNamespace(). 1291 }) 1292 1293DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement 1294 }) 1295 1296DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement 1297 }) 1298 1299DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement 1300 }) 1301 1302DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement 1303 }) 1304 1305DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement 1306 }) 1307 1308DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement 1309 }) 1310 1311DEF_TRAVERSE_DECL(ObjCMethodDecl, { 1312 if (D->getReturnTypeSourceInfo()) { 1313 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc())); 1314 } 1315 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end(); 1316 I != E; ++I) { 1317 TRY_TO(TraverseDecl(*I)); 1318 } 1319 if (D->isThisDeclarationADefinition()) { 1320 TRY_TO(TraverseStmt(D->getBody())); 1321 } 1322 return true; 1323}) 1324 1325DEF_TRAVERSE_DECL(ObjCPropertyDecl, {// FIXME: implement 1326 }) 1327 1328DEF_TRAVERSE_DECL(UsingDecl, { 1329 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1330 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1331}) 1332 1333DEF_TRAVERSE_DECL(UsingDirectiveDecl, { 1334 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1335}) 1336 1337DEF_TRAVERSE_DECL(UsingShadowDecl, {}) 1338 1339DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, { 1340 for (auto *I : D->varlists()) { 1341 TRY_TO(TraverseStmt(I)); 1342 } 1343}) 1344 1345// A helper method for TemplateDecl's children. 1346template <typename Derived> 1347bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper( 1348 TemplateParameterList *TPL) { 1349 if (TPL) { 1350 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1351 I != E; ++I) { 1352 TRY_TO(TraverseDecl(*I)); 1353 } 1354 } 1355 return true; 1356} 1357 1358// A helper method for traversing the implicit instantiations of a 1359// class template. 1360template <typename Derived> 1361bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations( 1362 ClassTemplateDecl *D) { 1363 for (auto *SD : D->specializations()) { 1364 for (auto *RD : SD->redecls()) { 1365 // We don't want to visit injected-class-names in this traversal. 1366 if (cast<CXXRecordDecl>(RD)->isInjectedClassName()) 1367 continue; 1368 1369 switch ( 1370 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1371 // Visit the implicit instantiations with the requested pattern. 1372 case TSK_Undeclared: 1373 case TSK_ImplicitInstantiation: 1374 TRY_TO(TraverseDecl(RD)); 1375 break; 1376 1377 // We don't need to do anything on an explicit instantiation 1378 // or explicit specialization because there will be an explicit 1379 // node for it elsewhere. 1380 case TSK_ExplicitInstantiationDeclaration: 1381 case TSK_ExplicitInstantiationDefinition: 1382 case TSK_ExplicitSpecialization: 1383 break; 1384 } 1385 } 1386 } 1387 1388 return true; 1389} 1390 1391DEF_TRAVERSE_DECL(ClassTemplateDecl, { 1392 CXXRecordDecl *TempDecl = D->getTemplatedDecl(); 1393 TRY_TO(TraverseDecl(TempDecl)); 1394 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1395 1396 // By default, we do not traverse the instantiations of 1397 // class templates since they do not appear in the user code. The 1398 // following code optionally traverses them. 1399 // 1400 // We only traverse the class instantiations when we see the canonical 1401 // declaration of the template, to ensure we only visit them once. 1402 if (getDerived().shouldVisitTemplateInstantiations() && 1403 D == D->getCanonicalDecl()) 1404 TRY_TO(TraverseClassInstantiations(D)); 1405 1406 // Note that getInstantiatedFromMemberTemplate() is just a link 1407 // from a template instantiation back to the template from which 1408 // it was instantiated, and thus should not be traversed. 1409}) 1410 1411// A helper method for traversing the implicit instantiations of a 1412// class template. 1413template <typename Derived> 1414bool RecursiveASTVisitor<Derived>::TraverseVariableInstantiations( 1415 VarTemplateDecl *D) { 1416 for (auto *SD : D->specializations()) { 1417 for (auto *RD : SD->redecls()) { 1418 switch ( 1419 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1420 // Visit the implicit instantiations with the requested pattern. 1421 case TSK_Undeclared: 1422 case TSK_ImplicitInstantiation: 1423 TRY_TO(TraverseDecl(RD)); 1424 break; 1425 1426 // We don't need to do anything on an explicit instantiation 1427 // or explicit specialization because there will be an explicit 1428 // node for it elsewhere. 1429 case TSK_ExplicitInstantiationDeclaration: 1430 case TSK_ExplicitInstantiationDefinition: 1431 case TSK_ExplicitSpecialization: 1432 break; 1433 } 1434 } 1435 } 1436 1437 return true; 1438} 1439 1440DEF_TRAVERSE_DECL(VarTemplateDecl, { 1441 VarDecl *TempDecl = D->getTemplatedDecl(); 1442 TRY_TO(TraverseDecl(TempDecl)); 1443 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1444 1445 // By default, we do not traverse the instantiations of 1446 // variable templates since they do not appear in the user code. The 1447 // following code optionally traverses them. 1448 // 1449 // We only traverse the variable instantiations when we see the canonical 1450 // declaration of the template, to ensure we only visit them once. 1451 if (getDerived().shouldVisitTemplateInstantiations() && 1452 D == D->getCanonicalDecl()) 1453 TRY_TO(TraverseVariableInstantiations(D)); 1454 1455 // Note that getInstantiatedFromMemberTemplate() is just a link 1456 // from a template instantiation back to the template from which 1457 // it was instantiated, and thus should not be traversed. 1458}) 1459 1460// A helper method for traversing the instantiations of a 1461// function while skipping its specializations. 1462template <typename Derived> 1463bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations( 1464 FunctionTemplateDecl *D) { 1465 for (auto *FD : D->specializations()) { 1466 for (auto *RD : FD->redecls()) { 1467 switch (RD->getTemplateSpecializationKind()) { 1468 case TSK_Undeclared: 1469 case TSK_ImplicitInstantiation: 1470 // We don't know what kind of FunctionDecl this is. 1471 TRY_TO(TraverseDecl(RD)); 1472 break; 1473 1474 // No need to visit explicit instantiations, we'll find the node 1475 // eventually. 1476 // FIXME: This is incorrect; there is no other node for an explicit 1477 // instantiation of a function template specialization. 1478 case TSK_ExplicitInstantiationDeclaration: 1479 case TSK_ExplicitInstantiationDefinition: 1480 break; 1481 1482 case TSK_ExplicitSpecialization: 1483 break; 1484 } 1485 } 1486 } 1487 1488 return true; 1489} 1490 1491DEF_TRAVERSE_DECL(FunctionTemplateDecl, { 1492 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1493 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1494 1495 // By default, we do not traverse the instantiations of 1496 // function templates since they do not appear in the user code. The 1497 // following code optionally traverses them. 1498 // 1499 // We only traverse the function instantiations when we see the canonical 1500 // declaration of the template, to ensure we only visit them once. 1501 if (getDerived().shouldVisitTemplateInstantiations() && 1502 D == D->getCanonicalDecl()) 1503 TRY_TO(TraverseFunctionInstantiations(D)); 1504}) 1505 1506DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, { 1507 // D is the "T" in something like 1508 // template <template <typename> class T> class container { }; 1509 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1510 if (D->hasDefaultArgument()) { 1511 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument())); 1512 } 1513 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1514}) 1515 1516DEF_TRAVERSE_DECL(TemplateTypeParmDecl, { 1517 // D is the "T" in something like "template<typename T> class vector;" 1518 if (D->getTypeForDecl()) 1519 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1520 if (D->hasDefaultArgument()) 1521 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc())); 1522}) 1523 1524DEF_TRAVERSE_DECL(TypedefDecl, { 1525 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1526 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1527 // declaring the typedef, not something that was written in the 1528 // source. 1529}) 1530 1531DEF_TRAVERSE_DECL(TypeAliasDecl, { 1532 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1533 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1534 // declaring the type alias, not something that was written in the 1535 // source. 1536}) 1537 1538DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, { 1539 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1540 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1541}) 1542 1543DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, { 1544 // A dependent using declaration which was marked with 'typename'. 1545 // template<class T> class A : public B<T> { using typename B<T>::foo; }; 1546 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1547 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1548 // declaring the type, not something that was written in the 1549 // source. 1550}) 1551 1552DEF_TRAVERSE_DECL(EnumDecl, { 1553 if (D->getTypeForDecl()) 1554 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1555 1556 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1557 // The enumerators are already traversed by 1558 // decls_begin()/decls_end(). 1559}) 1560 1561// Helper methods for RecordDecl and its children. 1562template <typename Derived> 1563bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) { 1564 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1565 // declaring the type, not something that was written in the source. 1566 1567 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1568 return true; 1569} 1570 1571template <typename Derived> 1572bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) { 1573 if (!TraverseRecordHelper(D)) 1574 return false; 1575 if (D->isCompleteDefinition()) { 1576 for (const auto &I : D->bases()) { 1577 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc())); 1578 } 1579 // We don't traverse the friends or the conversions, as they are 1580 // already in decls_begin()/decls_end(). 1581 } 1582 return true; 1583} 1584 1585DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) 1586 1587DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) 1588 1589DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, { 1590 // For implicit instantiations ("set<int> x;"), we don't want to 1591 // recurse at all, since the instatiated class isn't written in 1592 // the source code anywhere. (Note the instatiated *type* -- 1593 // set<int> -- is written, and will still get a callback of 1594 // TemplateSpecializationType). For explicit instantiations 1595 // ("template set<int>;"), we do need a callback, since this 1596 // is the only callback that's made for this instantiation. 1597 // We use getTypeAsWritten() to distinguish. 1598 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) 1599 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1600 1601 if (!getDerived().shouldVisitTemplateInstantiations() && 1602 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) 1603 // Returning from here skips traversing the 1604 // declaration context of the ClassTemplateSpecializationDecl 1605 // (embedded in the DEF_TRAVERSE_DECL() macro) 1606 // which contains the instantiated members of the class. 1607 return true; 1608}) 1609 1610template <typename Derived> 1611bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper( 1612 const TemplateArgumentLoc *TAL, unsigned Count) { 1613 for (unsigned I = 0; I < Count; ++I) { 1614 TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); 1615 } 1616 return true; 1617} 1618 1619DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, { 1620 // The partial specialization. 1621 if (TemplateParameterList *TPL = D->getTemplateParameters()) { 1622 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1623 I != E; ++I) { 1624 TRY_TO(TraverseDecl(*I)); 1625 } 1626 } 1627 // The args that remains unspecialized. 1628 TRY_TO(TraverseTemplateArgumentLocsHelper( 1629 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1630 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1631 1632 // Don't need the ClassTemplatePartialSpecializationHelper, even 1633 // though that's our parent class -- we already visit all the 1634 // template args here. 1635 TRY_TO(TraverseCXXRecordHelper(D)); 1636 1637 // Instantiations will have been visited with the primary template. 1638}) 1639 1640DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); }) 1641 1642DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, { 1643 // Like UnresolvedUsingTypenameDecl, but without the 'typename': 1644 // template <class T> Class A : public Base<T> { using Base<T>::foo; }; 1645 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1646 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1647}) 1648 1649DEF_TRAVERSE_DECL(IndirectFieldDecl, {}) 1650 1651template <typename Derived> 1652bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) { 1653 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1654 if (D->getTypeSourceInfo()) 1655 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1656 else 1657 TRY_TO(TraverseType(D->getType())); 1658 return true; 1659} 1660 1661DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); }) 1662 1663DEF_TRAVERSE_DECL(FieldDecl, { 1664 TRY_TO(TraverseDeclaratorHelper(D)); 1665 if (D->isBitField()) 1666 TRY_TO(TraverseStmt(D->getBitWidth())); 1667 else if (D->hasInClassInitializer()) 1668 TRY_TO(TraverseStmt(D->getInClassInitializer())); 1669}) 1670 1671DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, { 1672 TRY_TO(TraverseDeclaratorHelper(D)); 1673 if (D->isBitField()) 1674 TRY_TO(TraverseStmt(D->getBitWidth())); 1675 // FIXME: implement the rest. 1676}) 1677 1678DEF_TRAVERSE_DECL(ObjCIvarDecl, { 1679 TRY_TO(TraverseDeclaratorHelper(D)); 1680 if (D->isBitField()) 1681 TRY_TO(TraverseStmt(D->getBitWidth())); 1682 // FIXME: implement the rest. 1683}) 1684 1685template <typename Derived> 1686bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) { 1687 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1688 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1689 1690 // If we're an explicit template specialization, iterate over the 1691 // template args that were explicitly specified. If we were doing 1692 // this in typing order, we'd do it between the return type and 1693 // the function args, but both are handled by the FunctionTypeLoc 1694 // above, so we have to choose one side. I've decided to do before. 1695 if (const FunctionTemplateSpecializationInfo *FTSI = 1696 D->getTemplateSpecializationInfo()) { 1697 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared && 1698 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { 1699 // A specialization might not have explicit template arguments if it has 1700 // a templated return type and concrete arguments. 1701 if (const ASTTemplateArgumentListInfo *TALI = 1702 FTSI->TemplateArgumentsAsWritten) { 1703 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(), 1704 TALI->NumTemplateArgs)); 1705 } 1706 } 1707 } 1708 1709 // Visit the function type itself, which can be either 1710 // FunctionNoProtoType or FunctionProtoType, or a typedef. This 1711 // also covers the return type and the function parameters, 1712 // including exception specifications. 1713 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1714 1715 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) { 1716 // Constructor initializers. 1717 for (auto *I : Ctor->inits()) { 1718 TRY_TO(TraverseConstructorInitializer(I)); 1719 } 1720 } 1721 1722 if (D->isThisDeclarationADefinition()) { 1723 TRY_TO(TraverseStmt(D->getBody())); // Function body. 1724 } 1725 return true; 1726} 1727 1728DEF_TRAVERSE_DECL(FunctionDecl, { 1729 // We skip decls_begin/decls_end, which are already covered by 1730 // TraverseFunctionHelper(). 1731 return TraverseFunctionHelper(D); 1732}) 1733 1734DEF_TRAVERSE_DECL(CXXMethodDecl, { 1735 // We skip decls_begin/decls_end, which are already covered by 1736 // TraverseFunctionHelper(). 1737 return TraverseFunctionHelper(D); 1738}) 1739 1740DEF_TRAVERSE_DECL(CXXConstructorDecl, { 1741 // We skip decls_begin/decls_end, which are already covered by 1742 // TraverseFunctionHelper(). 1743 return TraverseFunctionHelper(D); 1744}) 1745 1746// CXXConversionDecl is the declaration of a type conversion operator. 1747// It's not a cast expression. 1748DEF_TRAVERSE_DECL(CXXConversionDecl, { 1749 // We skip decls_begin/decls_end, which are already covered by 1750 // TraverseFunctionHelper(). 1751 return TraverseFunctionHelper(D); 1752}) 1753 1754DEF_TRAVERSE_DECL(CXXDestructorDecl, { 1755 // We skip decls_begin/decls_end, which are already covered by 1756 // TraverseFunctionHelper(). 1757 return TraverseFunctionHelper(D); 1758}) 1759 1760template <typename Derived> 1761bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) { 1762 TRY_TO(TraverseDeclaratorHelper(D)); 1763 // Default params are taken care of when we traverse the ParmVarDecl. 1764 if (!isa<ParmVarDecl>(D)) 1765 TRY_TO(TraverseStmt(D->getInit())); 1766 return true; 1767} 1768 1769DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); }) 1770 1771DEF_TRAVERSE_DECL(VarTemplateSpecializationDecl, { 1772 // For implicit instantiations, we don't want to 1773 // recurse at all, since the instatiated class isn't written in 1774 // the source code anywhere. 1775 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) 1776 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1777 1778 if (!getDerived().shouldVisitTemplateInstantiations() && 1779 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) 1780 // Returning from here skips traversing the 1781 // declaration context of the VarTemplateSpecializationDecl 1782 // (embedded in the DEF_TRAVERSE_DECL() macro). 1783 return true; 1784}) 1785 1786DEF_TRAVERSE_DECL(VarTemplatePartialSpecializationDecl, { 1787 // The partial specialization. 1788 if (TemplateParameterList *TPL = D->getTemplateParameters()) { 1789 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1790 I != E; ++I) { 1791 TRY_TO(TraverseDecl(*I)); 1792 } 1793 } 1794 // The args that remains unspecialized. 1795 TRY_TO(TraverseTemplateArgumentLocsHelper( 1796 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1797 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1798 1799 // Don't need the VarTemplatePartialSpecializationHelper, even 1800 // though that's our parent class -- we already visit all the 1801 // template args here. 1802 TRY_TO(TraverseVarHelper(D)); 1803 1804 // Instantiations will have been visited with the primary 1805 // template. 1806}) 1807 1808DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); }) 1809 1810DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, { 1811 // A non-type template parameter, e.g. "S" in template<int S> class Foo ... 1812 TRY_TO(TraverseDeclaratorHelper(D)); 1813 TRY_TO(TraverseStmt(D->getDefaultArgument())); 1814}) 1815 1816DEF_TRAVERSE_DECL(ParmVarDecl, { 1817 TRY_TO(TraverseVarHelper(D)); 1818 1819 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() && 1820 !D->hasUnparsedDefaultArg()) 1821 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg())); 1822 1823 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() && 1824 !D->hasUnparsedDefaultArg()) 1825 TRY_TO(TraverseStmt(D->getDefaultArg())); 1826}) 1827 1828#undef DEF_TRAVERSE_DECL 1829 1830// ----------------- Stmt traversal ----------------- 1831// 1832// For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating 1833// over the children defined in children() (every stmt defines these, 1834// though sometimes the range is empty). Each individual Traverse* 1835// method only needs to worry about children other than those. To see 1836// what children() does for a given class, see, e.g., 1837// http://clang.llvm.org/doxygen/Stmt_8cpp_source.html 1838 1839// This macro makes available a variable S, the passed-in stmt. 1840#define DEF_TRAVERSE_STMT(STMT, CODE) \ 1841 template <typename Derived> \ 1842 bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) { \ 1843 TRY_TO(WalkUpFrom##STMT(S)); \ 1844 StmtQueueAction StmtQueue(*this); \ 1845 { CODE; } \ 1846 for (Stmt::child_range range = S->children(); range; ++range) { \ 1847 StmtQueue.queue(*range); \ 1848 } \ 1849 return true; \ 1850 } 1851 1852DEF_TRAVERSE_STMT(GCCAsmStmt, { 1853 StmtQueue.queue(S->getAsmString()); 1854 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) { 1855 StmtQueue.queue(S->getInputConstraintLiteral(I)); 1856 } 1857 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) { 1858 StmtQueue.queue(S->getOutputConstraintLiteral(I)); 1859 } 1860 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) { 1861 StmtQueue.queue(S->getClobberStringLiteral(I)); 1862 } 1863 // children() iterates over inputExpr and outputExpr. 1864}) 1865 1866DEF_TRAVERSE_STMT( 1867 MSAsmStmt, 1868 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once 1869 // added this needs to be implemented. 1870 }) 1871 1872DEF_TRAVERSE_STMT(CXXCatchStmt, { 1873 TRY_TO(TraverseDecl(S->getExceptionDecl())); 1874 // children() iterates over the handler block. 1875}) 1876 1877DEF_TRAVERSE_STMT(DeclStmt, { 1878 for (auto *I : S->decls()) { 1879 TRY_TO(TraverseDecl(I)); 1880 } 1881 // Suppress the default iteration over children() by 1882 // returning. Here's why: A DeclStmt looks like 'type var [= 1883 // initializer]'. The decls above already traverse over the 1884 // initializers, so we don't have to do it again (which 1885 // children() would do). 1886 return true; 1887}) 1888 1889// These non-expr stmts (most of them), do not need any action except 1890// iterating over the children. 1891DEF_TRAVERSE_STMT(BreakStmt, {}) 1892DEF_TRAVERSE_STMT(CXXTryStmt, {}) 1893DEF_TRAVERSE_STMT(CaseStmt, {}) 1894DEF_TRAVERSE_STMT(CompoundStmt, {}) 1895DEF_TRAVERSE_STMT(ContinueStmt, {}) 1896DEF_TRAVERSE_STMT(DefaultStmt, {}) 1897DEF_TRAVERSE_STMT(DoStmt, {}) 1898DEF_TRAVERSE_STMT(ForStmt, {}) 1899DEF_TRAVERSE_STMT(GotoStmt, {}) 1900DEF_TRAVERSE_STMT(IfStmt, {}) 1901DEF_TRAVERSE_STMT(IndirectGotoStmt, {}) 1902DEF_TRAVERSE_STMT(LabelStmt, {}) 1903DEF_TRAVERSE_STMT(AttributedStmt, {}) 1904DEF_TRAVERSE_STMT(NullStmt, {}) 1905DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {}) 1906DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {}) 1907DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {}) 1908DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {}) 1909DEF_TRAVERSE_STMT(ObjCAtTryStmt, {}) 1910DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {}) 1911DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {}) 1912DEF_TRAVERSE_STMT(CXXForRangeStmt, {}) 1913DEF_TRAVERSE_STMT(MSDependentExistsStmt, { 1914 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1915 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1916}) 1917DEF_TRAVERSE_STMT(ReturnStmt, {}) 1918DEF_TRAVERSE_STMT(SwitchStmt, {}) 1919DEF_TRAVERSE_STMT(WhileStmt, {}) 1920 1921DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, { 1922 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1923 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 1924 if (S->hasExplicitTemplateArgs()) { 1925 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1926 S->getNumTemplateArgs())); 1927 } 1928}) 1929 1930DEF_TRAVERSE_STMT(DeclRefExpr, { 1931 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1932 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1933 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1934 S->getNumTemplateArgs())); 1935}) 1936 1937DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, { 1938 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1939 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1940 if (S->hasExplicitTemplateArgs()) { 1941 TRY_TO(TraverseTemplateArgumentLocsHelper( 1942 S->getExplicitTemplateArgs().getTemplateArgs(), 1943 S->getNumTemplateArgs())); 1944 } 1945}) 1946 1947DEF_TRAVERSE_STMT(MemberExpr, { 1948 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1949 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 1950 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1951 S->getNumTemplateArgs())); 1952}) 1953 1954DEF_TRAVERSE_STMT( 1955 ImplicitCastExpr, 1956 {// We don't traverse the cast type, as it's not written in the 1957 // source code. 1958 }) 1959 1960DEF_TRAVERSE_STMT(CStyleCastExpr, { 1961 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1962}) 1963 1964DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, { 1965 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1966}) 1967 1968DEF_TRAVERSE_STMT(CXXConstCastExpr, { 1969 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1970}) 1971 1972DEF_TRAVERSE_STMT(CXXDynamicCastExpr, { 1973 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1974}) 1975 1976DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, { 1977 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1978}) 1979 1980DEF_TRAVERSE_STMT(CXXStaticCastExpr, { 1981 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1982}) 1983 1984// InitListExpr is a tricky one, because we want to do all our work on 1985// the syntactic form of the listexpr, but this method takes the 1986// semantic form by default. We can't use the macro helper because it 1987// calls WalkUp*() on the semantic form, before our code can convert 1988// to the syntactic form. 1989template <typename Derived> 1990bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) { 1991 if (InitListExpr *Syn = S->getSyntacticForm()) 1992 S = Syn; 1993 TRY_TO(WalkUpFromInitListExpr(S)); 1994 StmtQueueAction StmtQueue(*this); 1995 // All we need are the default actions. FIXME: use a helper function. 1996 for (Stmt::child_range range = S->children(); range; ++range) { 1997 StmtQueue.queue(*range); 1998 } 1999 return true; 2000} 2001 2002// GenericSelectionExpr is a special case because the types and expressions 2003// are interleaved. We also need to watch out for null types (default 2004// generic associations). 2005template <typename Derived> 2006bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr( 2007 GenericSelectionExpr *S) { 2008 TRY_TO(WalkUpFromGenericSelectionExpr(S)); 2009 StmtQueueAction StmtQueue(*this); 2010 StmtQueue.queue(S->getControllingExpr()); 2011 for (unsigned i = 0; i != S->getNumAssocs(); ++i) { 2012 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i)) 2013 TRY_TO(TraverseTypeLoc(TS->getTypeLoc())); 2014 StmtQueue.queue(S->getAssocExpr(i)); 2015 } 2016 return true; 2017} 2018 2019// PseudoObjectExpr is a special case because of the wierdness with 2020// syntactic expressions and opaque values. 2021template <typename Derived> 2022bool 2023RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) { 2024 TRY_TO(WalkUpFromPseudoObjectExpr(S)); 2025 StmtQueueAction StmtQueue(*this); 2026 StmtQueue.queue(S->getSyntacticForm()); 2027 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(), 2028 e = S->semantics_end(); 2029 i != e; ++i) { 2030 Expr *sub = *i; 2031 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub)) 2032 sub = OVE->getSourceExpr(); 2033 StmtQueue.queue(sub); 2034 } 2035 return true; 2036} 2037 2038DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, { 2039 // This is called for code like 'return T()' where T is a built-in 2040 // (i.e. non-class) type. 2041 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2042}) 2043 2044DEF_TRAVERSE_STMT(CXXNewExpr, { 2045 // The child-iterator will pick up the other arguments. 2046 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc())); 2047}) 2048 2049DEF_TRAVERSE_STMT(OffsetOfExpr, { 2050 // The child-iterator will pick up the expression representing 2051 // the field. 2052 // FIMXE: for code like offsetof(Foo, a.b.c), should we get 2053 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c? 2054 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2055}) 2056 2057DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, { 2058 // The child-iterator will pick up the arg if it's an expression, 2059 // but not if it's a type. 2060 if (S->isArgumentType()) 2061 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc())); 2062}) 2063 2064DEF_TRAVERSE_STMT(CXXTypeidExpr, { 2065 // The child-iterator will pick up the arg if it's an expression, 2066 // but not if it's a type. 2067 if (S->isTypeOperand()) 2068 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2069}) 2070 2071DEF_TRAVERSE_STMT(MSPropertyRefExpr, { 2072 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2073}) 2074 2075DEF_TRAVERSE_STMT(CXXUuidofExpr, { 2076 // The child-iterator will pick up the arg if it's an expression, 2077 // but not if it's a type. 2078 if (S->isTypeOperand()) 2079 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2080}) 2081 2082DEF_TRAVERSE_STMT(TypeTraitExpr, { 2083 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 2084 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc())); 2085}) 2086 2087DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, { 2088 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc())); 2089}) 2090 2091DEF_TRAVERSE_STMT(ExpressionTraitExpr, 2092 { StmtQueue.queue(S->getQueriedExpression()); }) 2093 2094DEF_TRAVERSE_STMT(VAArgExpr, { 2095 // The child-iterator will pick up the expression argument. 2096 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc())); 2097}) 2098 2099DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, { 2100 // This is called for code like 'return T()' where T is a class type. 2101 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2102}) 2103 2104// Walk only the visible parts of lambda expressions. 2105template <typename Derived> 2106bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) { 2107 TRY_TO(WalkUpFromLambdaExpr(S)); 2108 2109 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(), 2110 CEnd = S->explicit_capture_end(); 2111 C != CEnd; ++C) { 2112 TRY_TO(TraverseLambdaCapture(S, C)); 2113 } 2114 2115 if (S->hasExplicitParameters() || S->hasExplicitResultType()) { 2116 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); 2117 if (S->hasExplicitParameters() && S->hasExplicitResultType()) { 2118 // Visit the whole type. 2119 TRY_TO(TraverseTypeLoc(TL)); 2120 } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) { 2121 if (S->hasExplicitParameters()) { 2122 // Visit parameters. 2123 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) { 2124 TRY_TO(TraverseDecl(Proto.getParam(I))); 2125 } 2126 } else { 2127 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc())); 2128 } 2129 } 2130 } 2131 2132 TRY_TO(TraverseLambdaBody(S)); 2133 return true; 2134} 2135 2136DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, { 2137 // This is called for code like 'T()', where T is a template argument. 2138 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2139}) 2140 2141// These expressions all might take explicit template arguments. 2142// We traverse those if so. FIXME: implement these. 2143DEF_TRAVERSE_STMT(CXXConstructExpr, {}) 2144DEF_TRAVERSE_STMT(CallExpr, {}) 2145DEF_TRAVERSE_STMT(CXXMemberCallExpr, {}) 2146 2147// These exprs (most of them), do not need any action except iterating 2148// over the children. 2149DEF_TRAVERSE_STMT(AddrLabelExpr, {}) 2150DEF_TRAVERSE_STMT(ArraySubscriptExpr, {}) 2151DEF_TRAVERSE_STMT(BlockExpr, { 2152 TRY_TO(TraverseDecl(S->getBlockDecl())); 2153 return true; // no child statements to loop through. 2154}) 2155DEF_TRAVERSE_STMT(ChooseExpr, {}) 2156DEF_TRAVERSE_STMT(CompoundLiteralExpr, { 2157 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2158}) 2159DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {}) 2160DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {}) 2161DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {}) 2162DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {}) 2163DEF_TRAVERSE_STMT(CXXDeleteExpr, {}) 2164DEF_TRAVERSE_STMT(ExprWithCleanups, {}) 2165DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {}) 2166DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {}) 2167DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, { 2168 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2169 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo()) 2170 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc())); 2171 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo()) 2172 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc())); 2173}) 2174DEF_TRAVERSE_STMT(CXXThisExpr, {}) 2175DEF_TRAVERSE_STMT(CXXThrowExpr, {}) 2176DEF_TRAVERSE_STMT(UserDefinedLiteral, {}) 2177DEF_TRAVERSE_STMT(DesignatedInitExpr, {}) 2178DEF_TRAVERSE_STMT(ExtVectorElementExpr, {}) 2179DEF_TRAVERSE_STMT(GNUNullExpr, {}) 2180DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {}) 2181DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {}) 2182DEF_TRAVERSE_STMT(ObjCEncodeExpr, { 2183 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo()) 2184 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2185}) 2186DEF_TRAVERSE_STMT(ObjCIsaExpr, {}) 2187DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {}) 2188DEF_TRAVERSE_STMT(ObjCMessageExpr, { 2189 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo()) 2190 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2191}) 2192DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {}) 2193DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {}) 2194DEF_TRAVERSE_STMT(ObjCProtocolExpr, {}) 2195DEF_TRAVERSE_STMT(ObjCSelectorExpr, {}) 2196DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {}) 2197DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, { 2198 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2199}) 2200DEF_TRAVERSE_STMT(ParenExpr, {}) 2201DEF_TRAVERSE_STMT(ParenListExpr, {}) 2202DEF_TRAVERSE_STMT(PredefinedExpr, {}) 2203DEF_TRAVERSE_STMT(ShuffleVectorExpr, {}) 2204DEF_TRAVERSE_STMT(ConvertVectorExpr, {}) 2205DEF_TRAVERSE_STMT(StmtExpr, {}) 2206DEF_TRAVERSE_STMT(UnresolvedLookupExpr, { 2207 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2208 if (S->hasExplicitTemplateArgs()) { 2209 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2210 S->getNumTemplateArgs())); 2211 } 2212}) 2213 2214DEF_TRAVERSE_STMT(UnresolvedMemberExpr, { 2215 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2216 if (S->hasExplicitTemplateArgs()) { 2217 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2218 S->getNumTemplateArgs())); 2219 } 2220}) 2221 2222DEF_TRAVERSE_STMT(SEHTryStmt, {}) 2223DEF_TRAVERSE_STMT(SEHExceptStmt, {}) 2224DEF_TRAVERSE_STMT(SEHFinallyStmt, {}) 2225DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); }) 2226 2227DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {}) 2228DEF_TRAVERSE_STMT(OpaqueValueExpr, {}) 2229DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {}) 2230 2231// These operators (all of them) do not need any action except 2232// iterating over the children. 2233DEF_TRAVERSE_STMT(BinaryConditionalOperator, {}) 2234DEF_TRAVERSE_STMT(ConditionalOperator, {}) 2235DEF_TRAVERSE_STMT(UnaryOperator, {}) 2236DEF_TRAVERSE_STMT(BinaryOperator, {}) 2237DEF_TRAVERSE_STMT(CompoundAssignOperator, {}) 2238DEF_TRAVERSE_STMT(CXXNoexceptExpr, {}) 2239DEF_TRAVERSE_STMT(PackExpansionExpr, {}) 2240DEF_TRAVERSE_STMT(SizeOfPackExpr, {}) 2241DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {}) 2242DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {}) 2243DEF_TRAVERSE_STMT(FunctionParmPackExpr, {}) 2244DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {}) 2245DEF_TRAVERSE_STMT(AtomicExpr, {}) 2246 2247// These literals (all of them) do not need any action. 2248DEF_TRAVERSE_STMT(IntegerLiteral, {}) 2249DEF_TRAVERSE_STMT(CharacterLiteral, {}) 2250DEF_TRAVERSE_STMT(FloatingLiteral, {}) 2251DEF_TRAVERSE_STMT(ImaginaryLiteral, {}) 2252DEF_TRAVERSE_STMT(StringLiteral, {}) 2253DEF_TRAVERSE_STMT(ObjCStringLiteral, {}) 2254DEF_TRAVERSE_STMT(ObjCBoxedExpr, {}) 2255DEF_TRAVERSE_STMT(ObjCArrayLiteral, {}) 2256DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {}) 2257 2258// Traverse OpenCL: AsType, Convert. 2259DEF_TRAVERSE_STMT(AsTypeExpr, {}) 2260 2261// OpenMP directives. 2262template <typename Derived> 2263bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective( 2264 OMPExecutableDirective *S) { 2265 ArrayRef<OMPClause *> Clauses = S->clauses(); 2266 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 2267 I != E; ++I) 2268 if (!TraverseOMPClause(*I)) 2269 return false; 2270 return true; 2271} 2272 2273DEF_TRAVERSE_STMT(OMPParallelDirective, { 2274 if (!TraverseOMPExecutableDirective(S)) 2275 return false; 2276}) 2277 2278DEF_TRAVERSE_STMT(OMPSimdDirective, { 2279 if (!TraverseOMPExecutableDirective(S)) 2280 return false; 2281}) 2282 2283// OpenMP clauses. 2284template <typename Derived> 2285bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) { 2286 if (!C) 2287 return true; 2288 switch (C->getClauseKind()) { 2289#define OPENMP_CLAUSE(Name, Class) \ 2290 case OMPC_##Name: \ 2291 return getDerived().Visit##Class(static_cast<Class *>(C)); 2292#include "clang/Basic/OpenMPKinds.def" 2293 default: 2294 break; 2295 } 2296 return true; 2297} 2298 2299template <typename Derived> 2300bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) { 2301 TraverseStmt(C->getCondition()); 2302 return true; 2303} 2304 2305template <typename Derived> 2306bool 2307RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 2308 TraverseStmt(C->getNumThreads()); 2309 return true; 2310} 2311 2312template <typename Derived> 2313bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) { 2314 TraverseStmt(C->getSafelen()); 2315 return true; 2316} 2317 2318template <typename Derived> 2319bool 2320RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) { 2321 TraverseStmt(C->getNumForLoops()); 2322 return true; 2323} 2324 2325template <typename Derived> 2326bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *C) { 2327 return true; 2328} 2329 2330template <typename Derived> 2331bool 2332RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *C) { 2333 return true; 2334} 2335 2336template <typename Derived> 2337template <typename T> 2338void RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) { 2339 for (auto *I : Node->varlists()) 2340 TraverseStmt(I); 2341} 2342 2343template <typename Derived> 2344bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) { 2345 VisitOMPClauseList(C); 2346 return true; 2347} 2348 2349template <typename Derived> 2350bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause( 2351 OMPFirstprivateClause *C) { 2352 VisitOMPClauseList(C); 2353 return true; 2354} 2355 2356template <typename Derived> 2357bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) { 2358 VisitOMPClauseList(C); 2359 return true; 2360} 2361 2362template <typename Derived> 2363bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) { 2364 VisitOMPClauseList(C); 2365 TraverseStmt(C->getStep()); 2366 return true; 2367} 2368 2369template <typename Derived> 2370bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) { 2371 VisitOMPClauseList(C); 2372 return true; 2373} 2374 2375// FIXME: look at the following tricky-seeming exprs to see if we 2376// need to recurse on anything. These are ones that have methods 2377// returning decls or qualtypes or nestednamespecifier -- though I'm 2378// not sure if they own them -- or just seemed very complicated, or 2379// had lots of sub-types to explore. 2380// 2381// VisitOverloadExpr and its children: recurse on template args? etc? 2382 2383// FIXME: go through all the stmts and exprs again, and see which of them 2384// create new types, and recurse on the types (TypeLocs?) of those. 2385// Candidates: 2386// 2387// http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html 2388// http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html 2389// http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html 2390// Every class that has getQualifier. 2391 2392#undef DEF_TRAVERSE_STMT 2393 2394#undef TRY_TO 2395 2396#undef RecursiveASTVisitor 2397 2398} // end namespace clang 2399 2400#endif // LLVM_CLANG_LIBCLANG_RECURSIVEASTVISITOR_H 2401