DataRecursiveASTVisitor.h revision ef8225444452a1486bd721f3285301fe84643b00
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 TraverseOMPExecutableDirective(OMPExecutableDirective *S); 427 bool TraverseOMPClause(OMPClause *C); 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> bool 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 for (const auto &I : D->captures()) { 1211 if (I.hasCopyExpr()) { 1212 TRY_TO(TraverseStmt(I.getCopyExpr())); 1213 } 1214 } 1215 // This return statement makes sure the traversal of nodes in 1216 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1217 // is skipped - don't remove it. 1218 return true; 1219}) 1220 1221DEF_TRAVERSE_DECL(CapturedDecl, { 1222 TRY_TO(TraverseStmt(D->getBody())); 1223 // This return statement makes sure the traversal of nodes in 1224 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1225 // is skipped - don't remove it. 1226 return true; 1227}) 1228 1229DEF_TRAVERSE_DECL(EmptyDecl, {}) 1230 1231DEF_TRAVERSE_DECL(FileScopeAsmDecl, 1232 { TRY_TO(TraverseStmt(D->getAsmString())); }) 1233 1234DEF_TRAVERSE_DECL(ImportDecl, {}) 1235 1236DEF_TRAVERSE_DECL(FriendDecl, { 1237 // Friend is either decl or a type. 1238 if (D->getFriendType()) 1239 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1240 else 1241 TRY_TO(TraverseDecl(D->getFriendDecl())); 1242}) 1243 1244DEF_TRAVERSE_DECL(FriendTemplateDecl, { 1245 if (D->getFriendType()) 1246 TRY_TO(TraverseTypeLoc(D->getFriendType()->getTypeLoc())); 1247 else 1248 TRY_TO(TraverseDecl(D->getFriendDecl())); 1249 for (unsigned I = 0, E = D->getNumTemplateParameters(); I < E; ++I) { 1250 TemplateParameterList *TPL = D->getTemplateParameterList(I); 1251 for (TemplateParameterList::iterator ITPL = TPL->begin(), ETPL = TPL->end(); 1252 ITPL != ETPL; ++ITPL) { 1253 TRY_TO(TraverseDecl(*ITPL)); 1254 } 1255 } 1256}) 1257 1258DEF_TRAVERSE_DECL(ClassScopeFunctionSpecializationDecl, 1259 { TRY_TO(TraverseDecl(D->getSpecialization())); }) 1260 1261DEF_TRAVERSE_DECL(LinkageSpecDecl, {}) 1262 1263DEF_TRAVERSE_DECL(ObjCPropertyImplDecl, {// FIXME: implement this 1264 }) 1265 1266DEF_TRAVERSE_DECL(StaticAssertDecl, { 1267 TRY_TO(TraverseStmt(D->getAssertExpr())); 1268 TRY_TO(TraverseStmt(D->getMessage())); 1269}) 1270 1271DEF_TRAVERSE_DECL( 1272 TranslationUnitDecl, 1273 {// Code in an unnamed namespace shows up automatically in 1274 // decls_begin()/decls_end(). Thus we don't need to recurse on 1275 // D->getAnonymousNamespace(). 1276 }) 1277 1278DEF_TRAVERSE_DECL(NamespaceAliasDecl, { 1279 // We shouldn't traverse an aliased namespace, since it will be 1280 // defined (and, therefore, traversed) somewhere else. 1281 // 1282 // This return statement makes sure the traversal of nodes in 1283 // decls_begin()/decls_end() (done in the DEF_TRAVERSE_DECL macro) 1284 // is skipped - don't remove it. 1285 return true; 1286}) 1287 1288DEF_TRAVERSE_DECL(LabelDecl, {// There is no code in a LabelDecl. 1289 }) 1290 1291DEF_TRAVERSE_DECL( 1292 NamespaceDecl, 1293 {// Code in an unnamed namespace shows up automatically in 1294 // decls_begin()/decls_end(). Thus we don't need to recurse on 1295 // D->getAnonymousNamespace(). 1296 }) 1297 1298DEF_TRAVERSE_DECL(ObjCCompatibleAliasDecl, {// FIXME: implement 1299 }) 1300 1301DEF_TRAVERSE_DECL(ObjCCategoryDecl, {// FIXME: implement 1302 }) 1303 1304DEF_TRAVERSE_DECL(ObjCCategoryImplDecl, {// FIXME: implement 1305 }) 1306 1307DEF_TRAVERSE_DECL(ObjCImplementationDecl, {// FIXME: implement 1308 }) 1309 1310DEF_TRAVERSE_DECL(ObjCInterfaceDecl, {// FIXME: implement 1311 }) 1312 1313DEF_TRAVERSE_DECL(ObjCProtocolDecl, {// FIXME: implement 1314 }) 1315 1316DEF_TRAVERSE_DECL(ObjCMethodDecl, { 1317 if (D->getReturnTypeSourceInfo()) { 1318 TRY_TO(TraverseTypeLoc(D->getReturnTypeSourceInfo()->getTypeLoc())); 1319 } 1320 for (ObjCMethodDecl::param_iterator I = D->param_begin(), E = D->param_end(); 1321 I != E; ++I) { 1322 TRY_TO(TraverseDecl(*I)); 1323 } 1324 if (D->isThisDeclarationADefinition()) { 1325 TRY_TO(TraverseStmt(D->getBody())); 1326 } 1327 return true; 1328}) 1329 1330DEF_TRAVERSE_DECL(ObjCPropertyDecl, { 1331 if (D->getTypeSourceInfo()) 1332 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1333 else 1334 TRY_TO(TraverseType(D->getType())); 1335 return true; 1336}) 1337 1338DEF_TRAVERSE_DECL(UsingDecl, { 1339 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1340 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1341}) 1342 1343DEF_TRAVERSE_DECL(UsingDirectiveDecl, { 1344 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1345}) 1346 1347DEF_TRAVERSE_DECL(UsingShadowDecl, {}) 1348 1349DEF_TRAVERSE_DECL(OMPThreadPrivateDecl, { 1350 for (auto *I : D->varlists()) { 1351 TRY_TO(TraverseStmt(I)); 1352 } 1353}) 1354 1355// A helper method for TemplateDecl's children. 1356template <typename Derived> 1357bool RecursiveASTVisitor<Derived>::TraverseTemplateParameterListHelper( 1358 TemplateParameterList *TPL) { 1359 if (TPL) { 1360 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1361 I != E; ++I) { 1362 TRY_TO(TraverseDecl(*I)); 1363 } 1364 } 1365 return true; 1366} 1367 1368// A helper method for traversing the implicit instantiations of a 1369// class template. 1370template <typename Derived> 1371bool RecursiveASTVisitor<Derived>::TraverseClassInstantiations( 1372 ClassTemplateDecl *D) { 1373 for (auto *SD : D->specializations()) { 1374 for (auto *RD : SD->redecls()) { 1375 // We don't want to visit injected-class-names in this traversal. 1376 if (cast<CXXRecordDecl>(RD)->isInjectedClassName()) 1377 continue; 1378 1379 switch ( 1380 cast<ClassTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1381 // Visit the implicit instantiations with the requested pattern. 1382 case TSK_Undeclared: 1383 case TSK_ImplicitInstantiation: 1384 TRY_TO(TraverseDecl(RD)); 1385 break; 1386 1387 // We don't need to do anything on an explicit instantiation 1388 // or explicit specialization because there will be an explicit 1389 // node for it elsewhere. 1390 case TSK_ExplicitInstantiationDeclaration: 1391 case TSK_ExplicitInstantiationDefinition: 1392 case TSK_ExplicitSpecialization: 1393 break; 1394 } 1395 } 1396 } 1397 1398 return true; 1399} 1400 1401DEF_TRAVERSE_DECL(ClassTemplateDecl, { 1402 CXXRecordDecl *TempDecl = D->getTemplatedDecl(); 1403 TRY_TO(TraverseDecl(TempDecl)); 1404 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1405 1406 // By default, we do not traverse the instantiations of 1407 // class templates since they do not appear in the user code. The 1408 // following code optionally traverses them. 1409 // 1410 // We only traverse the class instantiations when we see the canonical 1411 // declaration of the template, to ensure we only visit them once. 1412 if (getDerived().shouldVisitTemplateInstantiations() && 1413 D == D->getCanonicalDecl()) 1414 TRY_TO(TraverseClassInstantiations(D)); 1415 1416 // Note that getInstantiatedFromMemberTemplate() is just a link 1417 // from a template instantiation back to the template from which 1418 // it was instantiated, and thus should not be traversed. 1419}) 1420 1421// A helper method for traversing the implicit instantiations of a 1422// class template. 1423template <typename Derived> 1424bool RecursiveASTVisitor<Derived>::TraverseVariableInstantiations( 1425 VarTemplateDecl *D) { 1426 for (auto *SD : D->specializations()) { 1427 for (auto *RD : SD->redecls()) { 1428 switch ( 1429 cast<VarTemplateSpecializationDecl>(RD)->getSpecializationKind()) { 1430 // Visit the implicit instantiations with the requested pattern. 1431 case TSK_Undeclared: 1432 case TSK_ImplicitInstantiation: 1433 TRY_TO(TraverseDecl(RD)); 1434 break; 1435 1436 // We don't need to do anything on an explicit instantiation 1437 // or explicit specialization because there will be an explicit 1438 // node for it elsewhere. 1439 case TSK_ExplicitInstantiationDeclaration: 1440 case TSK_ExplicitInstantiationDefinition: 1441 case TSK_ExplicitSpecialization: 1442 break; 1443 } 1444 } 1445 } 1446 1447 return true; 1448} 1449 1450DEF_TRAVERSE_DECL(VarTemplateDecl, { 1451 VarDecl *TempDecl = D->getTemplatedDecl(); 1452 TRY_TO(TraverseDecl(TempDecl)); 1453 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1454 1455 // By default, we do not traverse the instantiations of 1456 // variable templates since they do not appear in the user code. The 1457 // following code optionally traverses them. 1458 // 1459 // We only traverse the variable instantiations when we see the canonical 1460 // declaration of the template, to ensure we only visit them once. 1461 if (getDerived().shouldVisitTemplateInstantiations() && 1462 D == D->getCanonicalDecl()) 1463 TRY_TO(TraverseVariableInstantiations(D)); 1464 1465 // Note that getInstantiatedFromMemberTemplate() is just a link 1466 // from a template instantiation back to the template from which 1467 // it was instantiated, and thus should not be traversed. 1468}) 1469 1470// A helper method for traversing the instantiations of a 1471// function while skipping its specializations. 1472template <typename Derived> 1473bool RecursiveASTVisitor<Derived>::TraverseFunctionInstantiations( 1474 FunctionTemplateDecl *D) { 1475 for (auto *FD : D->specializations()) { 1476 for (auto *RD : FD->redecls()) { 1477 switch (RD->getTemplateSpecializationKind()) { 1478 case TSK_Undeclared: 1479 case TSK_ImplicitInstantiation: 1480 // We don't know what kind of FunctionDecl this is. 1481 TRY_TO(TraverseDecl(RD)); 1482 break; 1483 1484 // No need to visit explicit instantiations, we'll find the node 1485 // eventually. 1486 // FIXME: This is incorrect; there is no other node for an explicit 1487 // instantiation of a function template specialization. 1488 case TSK_ExplicitInstantiationDeclaration: 1489 case TSK_ExplicitInstantiationDefinition: 1490 break; 1491 1492 case TSK_ExplicitSpecialization: 1493 break; 1494 } 1495 } 1496 } 1497 1498 return true; 1499} 1500 1501DEF_TRAVERSE_DECL(FunctionTemplateDecl, { 1502 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1503 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1504 1505 // By default, we do not traverse the instantiations of 1506 // function templates since they do not appear in the user code. The 1507 // following code optionally traverses them. 1508 // 1509 // We only traverse the function instantiations when we see the canonical 1510 // declaration of the template, to ensure we only visit them once. 1511 if (getDerived().shouldVisitTemplateInstantiations() && 1512 D == D->getCanonicalDecl()) 1513 TRY_TO(TraverseFunctionInstantiations(D)); 1514}) 1515 1516DEF_TRAVERSE_DECL(TemplateTemplateParmDecl, { 1517 // D is the "T" in something like 1518 // template <template <typename> class T> class container { }; 1519 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1520 if (D->hasDefaultArgument()) { 1521 TRY_TO(TraverseTemplateArgumentLoc(D->getDefaultArgument())); 1522 } 1523 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1524}) 1525 1526DEF_TRAVERSE_DECL(TemplateTypeParmDecl, { 1527 // D is the "T" in something like "template<typename T> class vector;" 1528 if (D->getTypeForDecl()) 1529 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1530 if (D->hasDefaultArgument()) 1531 TRY_TO(TraverseTypeLoc(D->getDefaultArgumentInfo()->getTypeLoc())); 1532}) 1533 1534DEF_TRAVERSE_DECL(TypedefDecl, { 1535 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1536 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1537 // declaring the typedef, not something that was written in the 1538 // source. 1539}) 1540 1541DEF_TRAVERSE_DECL(TypeAliasDecl, { 1542 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1543 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1544 // declaring the type alias, not something that was written in the 1545 // source. 1546}) 1547 1548DEF_TRAVERSE_DECL(TypeAliasTemplateDecl, { 1549 TRY_TO(TraverseDecl(D->getTemplatedDecl())); 1550 TRY_TO(TraverseTemplateParameterListHelper(D->getTemplateParameters())); 1551}) 1552 1553DEF_TRAVERSE_DECL(UnresolvedUsingTypenameDecl, { 1554 // A dependent using declaration which was marked with 'typename'. 1555 // template<class T> class A : public B<T> { using typename B<T>::foo; }; 1556 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1557 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1558 // declaring the type, not something that was written in the 1559 // source. 1560}) 1561 1562DEF_TRAVERSE_DECL(EnumDecl, { 1563 if (D->getTypeForDecl()) 1564 TRY_TO(TraverseType(QualType(D->getTypeForDecl(), 0))); 1565 1566 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1567 // The enumerators are already traversed by 1568 // decls_begin()/decls_end(). 1569}) 1570 1571// Helper methods for RecordDecl and its children. 1572template <typename Derived> 1573bool RecursiveASTVisitor<Derived>::TraverseRecordHelper(RecordDecl *D) { 1574 // We shouldn't traverse D->getTypeForDecl(); it's a result of 1575 // declaring the type, not something that was written in the source. 1576 1577 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1578 return true; 1579} 1580 1581template <typename Derived> 1582bool RecursiveASTVisitor<Derived>::TraverseCXXRecordHelper(CXXRecordDecl *D) { 1583 if (!TraverseRecordHelper(D)) 1584 return false; 1585 if (D->isCompleteDefinition()) { 1586 for (const auto &I : D->bases()) { 1587 TRY_TO(TraverseTypeLoc(I.getTypeSourceInfo()->getTypeLoc())); 1588 } 1589 // We don't traverse the friends or the conversions, as they are 1590 // already in decls_begin()/decls_end(). 1591 } 1592 return true; 1593} 1594 1595DEF_TRAVERSE_DECL(RecordDecl, { TRY_TO(TraverseRecordHelper(D)); }) 1596 1597DEF_TRAVERSE_DECL(CXXRecordDecl, { TRY_TO(TraverseCXXRecordHelper(D)); }) 1598 1599DEF_TRAVERSE_DECL(ClassTemplateSpecializationDecl, { 1600 // For implicit instantiations ("set<int> x;"), we don't want to 1601 // recurse at all, since the instatiated class isn't written in 1602 // the source code anywhere. (Note the instatiated *type* -- 1603 // set<int> -- is written, and will still get a callback of 1604 // TemplateSpecializationType). For explicit instantiations 1605 // ("template set<int>;"), we do need a callback, since this 1606 // is the only callback that's made for this instantiation. 1607 // We use getTypeAsWritten() to distinguish. 1608 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) 1609 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1610 1611 if (!getDerived().shouldVisitTemplateInstantiations() && 1612 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) 1613 // Returning from here skips traversing the 1614 // declaration context of the ClassTemplateSpecializationDecl 1615 // (embedded in the DEF_TRAVERSE_DECL() macro) 1616 // which contains the instantiated members of the class. 1617 return true; 1618}) 1619 1620template <typename Derived> 1621bool RecursiveASTVisitor<Derived>::TraverseTemplateArgumentLocsHelper( 1622 const TemplateArgumentLoc *TAL, unsigned Count) { 1623 for (unsigned I = 0; I < Count; ++I) { 1624 TRY_TO(TraverseTemplateArgumentLoc(TAL[I])); 1625 } 1626 return true; 1627} 1628 1629DEF_TRAVERSE_DECL(ClassTemplatePartialSpecializationDecl, { 1630 // The partial specialization. 1631 if (TemplateParameterList *TPL = D->getTemplateParameters()) { 1632 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1633 I != E; ++I) { 1634 TRY_TO(TraverseDecl(*I)); 1635 } 1636 } 1637 // The args that remains unspecialized. 1638 TRY_TO(TraverseTemplateArgumentLocsHelper( 1639 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1640 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1641 1642 // Don't need the ClassTemplatePartialSpecializationHelper, even 1643 // though that's our parent class -- we already visit all the 1644 // template args here. 1645 TRY_TO(TraverseCXXRecordHelper(D)); 1646 1647 // Instantiations will have been visited with the primary template. 1648}) 1649 1650DEF_TRAVERSE_DECL(EnumConstantDecl, { TRY_TO(TraverseStmt(D->getInitExpr())); }) 1651 1652DEF_TRAVERSE_DECL(UnresolvedUsingValueDecl, { 1653 // Like UnresolvedUsingTypenameDecl, but without the 'typename': 1654 // template <class T> Class A : public Base<T> { using Base<T>::foo; }; 1655 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1656 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1657}) 1658 1659DEF_TRAVERSE_DECL(IndirectFieldDecl, {}) 1660 1661template <typename Derived> 1662bool RecursiveASTVisitor<Derived>::TraverseDeclaratorHelper(DeclaratorDecl *D) { 1663 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1664 if (D->getTypeSourceInfo()) 1665 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1666 else 1667 TRY_TO(TraverseType(D->getType())); 1668 return true; 1669} 1670 1671DEF_TRAVERSE_DECL(MSPropertyDecl, { TRY_TO(TraverseDeclaratorHelper(D)); }) 1672 1673DEF_TRAVERSE_DECL(FieldDecl, { 1674 TRY_TO(TraverseDeclaratorHelper(D)); 1675 if (D->isBitField()) 1676 TRY_TO(TraverseStmt(D->getBitWidth())); 1677 else if (D->hasInClassInitializer()) 1678 TRY_TO(TraverseStmt(D->getInClassInitializer())); 1679}) 1680 1681DEF_TRAVERSE_DECL(ObjCAtDefsFieldDecl, { 1682 TRY_TO(TraverseDeclaratorHelper(D)); 1683 if (D->isBitField()) 1684 TRY_TO(TraverseStmt(D->getBitWidth())); 1685 // FIXME: implement the rest. 1686}) 1687 1688DEF_TRAVERSE_DECL(ObjCIvarDecl, { 1689 TRY_TO(TraverseDeclaratorHelper(D)); 1690 if (D->isBitField()) 1691 TRY_TO(TraverseStmt(D->getBitWidth())); 1692 // FIXME: implement the rest. 1693}) 1694 1695template <typename Derived> 1696bool RecursiveASTVisitor<Derived>::TraverseFunctionHelper(FunctionDecl *D) { 1697 TRY_TO(TraverseNestedNameSpecifierLoc(D->getQualifierLoc())); 1698 TRY_TO(TraverseDeclarationNameInfo(D->getNameInfo())); 1699 1700 // If we're an explicit template specialization, iterate over the 1701 // template args that were explicitly specified. If we were doing 1702 // this in typing order, we'd do it between the return type and 1703 // the function args, but both are handled by the FunctionTypeLoc 1704 // above, so we have to choose one side. I've decided to do before. 1705 if (const FunctionTemplateSpecializationInfo *FTSI = 1706 D->getTemplateSpecializationInfo()) { 1707 if (FTSI->getTemplateSpecializationKind() != TSK_Undeclared && 1708 FTSI->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) { 1709 // A specialization might not have explicit template arguments if it has 1710 // a templated return type and concrete arguments. 1711 if (const ASTTemplateArgumentListInfo *TALI = 1712 FTSI->TemplateArgumentsAsWritten) { 1713 TRY_TO(TraverseTemplateArgumentLocsHelper(TALI->getTemplateArgs(), 1714 TALI->NumTemplateArgs)); 1715 } 1716 } 1717 } 1718 1719 // Visit the function type itself, which can be either 1720 // FunctionNoProtoType or FunctionProtoType, or a typedef. This 1721 // also covers the return type and the function parameters, 1722 // including exception specifications. 1723 TRY_TO(TraverseTypeLoc(D->getTypeSourceInfo()->getTypeLoc())); 1724 1725 if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) { 1726 // Constructor initializers. 1727 for (auto *I : Ctor->inits()) { 1728 TRY_TO(TraverseConstructorInitializer(I)); 1729 } 1730 } 1731 1732 if (D->isThisDeclarationADefinition()) { 1733 TRY_TO(TraverseStmt(D->getBody())); // Function body. 1734 } 1735 return true; 1736} 1737 1738DEF_TRAVERSE_DECL(FunctionDecl, { 1739 // We skip decls_begin/decls_end, which are already covered by 1740 // TraverseFunctionHelper(). 1741 return TraverseFunctionHelper(D); 1742}) 1743 1744DEF_TRAVERSE_DECL(CXXMethodDecl, { 1745 // We skip decls_begin/decls_end, which are already covered by 1746 // TraverseFunctionHelper(). 1747 return TraverseFunctionHelper(D); 1748}) 1749 1750DEF_TRAVERSE_DECL(CXXConstructorDecl, { 1751 // We skip decls_begin/decls_end, which are already covered by 1752 // TraverseFunctionHelper(). 1753 return TraverseFunctionHelper(D); 1754}) 1755 1756// CXXConversionDecl is the declaration of a type conversion operator. 1757// It's not a cast expression. 1758DEF_TRAVERSE_DECL(CXXConversionDecl, { 1759 // We skip decls_begin/decls_end, which are already covered by 1760 // TraverseFunctionHelper(). 1761 return TraverseFunctionHelper(D); 1762}) 1763 1764DEF_TRAVERSE_DECL(CXXDestructorDecl, { 1765 // We skip decls_begin/decls_end, which are already covered by 1766 // TraverseFunctionHelper(). 1767 return TraverseFunctionHelper(D); 1768}) 1769 1770template <typename Derived> 1771bool RecursiveASTVisitor<Derived>::TraverseVarHelper(VarDecl *D) { 1772 TRY_TO(TraverseDeclaratorHelper(D)); 1773 // Default params are taken care of when we traverse the ParmVarDecl. 1774 if (!isa<ParmVarDecl>(D)) 1775 TRY_TO(TraverseStmt(D->getInit())); 1776 return true; 1777} 1778 1779DEF_TRAVERSE_DECL(VarDecl, { TRY_TO(TraverseVarHelper(D)); }) 1780 1781DEF_TRAVERSE_DECL(VarTemplateSpecializationDecl, { 1782 // For implicit instantiations, we don't want to 1783 // recurse at all, since the instatiated class isn't written in 1784 // the source code anywhere. 1785 if (TypeSourceInfo *TSI = D->getTypeAsWritten()) 1786 TRY_TO(TraverseTypeLoc(TSI->getTypeLoc())); 1787 1788 if (!getDerived().shouldVisitTemplateInstantiations() && 1789 D->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) 1790 // Returning from here skips traversing the 1791 // declaration context of the VarTemplateSpecializationDecl 1792 // (embedded in the DEF_TRAVERSE_DECL() macro). 1793 return true; 1794}) 1795 1796DEF_TRAVERSE_DECL(VarTemplatePartialSpecializationDecl, { 1797 // The partial specialization. 1798 if (TemplateParameterList *TPL = D->getTemplateParameters()) { 1799 for (TemplateParameterList::iterator I = TPL->begin(), E = TPL->end(); 1800 I != E; ++I) { 1801 TRY_TO(TraverseDecl(*I)); 1802 } 1803 } 1804 // The args that remains unspecialized. 1805 TRY_TO(TraverseTemplateArgumentLocsHelper( 1806 D->getTemplateArgsAsWritten()->getTemplateArgs(), 1807 D->getTemplateArgsAsWritten()->NumTemplateArgs)); 1808 1809 // Don't need the VarTemplatePartialSpecializationHelper, even 1810 // though that's our parent class -- we already visit all the 1811 // template args here. 1812 TRY_TO(TraverseVarHelper(D)); 1813 1814 // Instantiations will have been visited with the primary 1815 // template. 1816}) 1817 1818DEF_TRAVERSE_DECL(ImplicitParamDecl, { TRY_TO(TraverseVarHelper(D)); }) 1819 1820DEF_TRAVERSE_DECL(NonTypeTemplateParmDecl, { 1821 // A non-type template parameter, e.g. "S" in template<int S> class Foo ... 1822 TRY_TO(TraverseDeclaratorHelper(D)); 1823 TRY_TO(TraverseStmt(D->getDefaultArgument())); 1824}) 1825 1826DEF_TRAVERSE_DECL(ParmVarDecl, { 1827 TRY_TO(TraverseVarHelper(D)); 1828 1829 if (D->hasDefaultArg() && D->hasUninstantiatedDefaultArg() && 1830 !D->hasUnparsedDefaultArg()) 1831 TRY_TO(TraverseStmt(D->getUninstantiatedDefaultArg())); 1832 1833 if (D->hasDefaultArg() && !D->hasUninstantiatedDefaultArg() && 1834 !D->hasUnparsedDefaultArg()) 1835 TRY_TO(TraverseStmt(D->getDefaultArg())); 1836}) 1837 1838#undef DEF_TRAVERSE_DECL 1839 1840// ----------------- Stmt traversal ----------------- 1841// 1842// For stmts, we automate (in the DEF_TRAVERSE_STMT macro) iterating 1843// over the children defined in children() (every stmt defines these, 1844// though sometimes the range is empty). Each individual Traverse* 1845// method only needs to worry about children other than those. To see 1846// what children() does for a given class, see, e.g., 1847// http://clang.llvm.org/doxygen/Stmt_8cpp_source.html 1848 1849// This macro makes available a variable S, the passed-in stmt. 1850#define DEF_TRAVERSE_STMT(STMT, CODE) \ 1851 template <typename Derived> \ 1852 bool RecursiveASTVisitor<Derived>::Traverse##STMT(STMT *S) { \ 1853 TRY_TO(WalkUpFrom##STMT(S)); \ 1854 StmtQueueAction StmtQueue(*this); \ 1855 { CODE; } \ 1856 for (Stmt::child_range range = S->children(); range; ++range) { \ 1857 StmtQueue.queue(*range); \ 1858 } \ 1859 return true; \ 1860 } 1861 1862DEF_TRAVERSE_STMT(GCCAsmStmt, { 1863 StmtQueue.queue(S->getAsmString()); 1864 for (unsigned I = 0, E = S->getNumInputs(); I < E; ++I) { 1865 StmtQueue.queue(S->getInputConstraintLiteral(I)); 1866 } 1867 for (unsigned I = 0, E = S->getNumOutputs(); I < E; ++I) { 1868 StmtQueue.queue(S->getOutputConstraintLiteral(I)); 1869 } 1870 for (unsigned I = 0, E = S->getNumClobbers(); I < E; ++I) { 1871 StmtQueue.queue(S->getClobberStringLiteral(I)); 1872 } 1873 // children() iterates over inputExpr and outputExpr. 1874}) 1875 1876DEF_TRAVERSE_STMT( 1877 MSAsmStmt, 1878 {// FIXME: MS Asm doesn't currently parse Constraints, Clobbers, etc. Once 1879 // added this needs to be implemented. 1880 }) 1881 1882DEF_TRAVERSE_STMT(CXXCatchStmt, { 1883 TRY_TO(TraverseDecl(S->getExceptionDecl())); 1884 // children() iterates over the handler block. 1885}) 1886 1887DEF_TRAVERSE_STMT(DeclStmt, { 1888 for (auto *I : S->decls()) { 1889 TRY_TO(TraverseDecl(I)); 1890 } 1891 // Suppress the default iteration over children() by 1892 // returning. Here's why: A DeclStmt looks like 'type var [= 1893 // initializer]'. The decls above already traverse over the 1894 // initializers, so we don't have to do it again (which 1895 // children() would do). 1896 return true; 1897}) 1898 1899// These non-expr stmts (most of them), do not need any action except 1900// iterating over the children. 1901DEF_TRAVERSE_STMT(BreakStmt, {}) 1902DEF_TRAVERSE_STMT(CXXTryStmt, {}) 1903DEF_TRAVERSE_STMT(CaseStmt, {}) 1904DEF_TRAVERSE_STMT(CompoundStmt, {}) 1905DEF_TRAVERSE_STMT(ContinueStmt, {}) 1906DEF_TRAVERSE_STMT(DefaultStmt, {}) 1907DEF_TRAVERSE_STMT(DoStmt, {}) 1908DEF_TRAVERSE_STMT(ForStmt, {}) 1909DEF_TRAVERSE_STMT(GotoStmt, {}) 1910DEF_TRAVERSE_STMT(IfStmt, {}) 1911DEF_TRAVERSE_STMT(IndirectGotoStmt, {}) 1912DEF_TRAVERSE_STMT(LabelStmt, {}) 1913DEF_TRAVERSE_STMT(AttributedStmt, {}) 1914DEF_TRAVERSE_STMT(NullStmt, {}) 1915DEF_TRAVERSE_STMT(ObjCAtCatchStmt, {}) 1916DEF_TRAVERSE_STMT(ObjCAtFinallyStmt, {}) 1917DEF_TRAVERSE_STMT(ObjCAtSynchronizedStmt, {}) 1918DEF_TRAVERSE_STMT(ObjCAtThrowStmt, {}) 1919DEF_TRAVERSE_STMT(ObjCAtTryStmt, {}) 1920DEF_TRAVERSE_STMT(ObjCForCollectionStmt, {}) 1921DEF_TRAVERSE_STMT(ObjCAutoreleasePoolStmt, {}) 1922DEF_TRAVERSE_STMT(CXXForRangeStmt, {}) 1923DEF_TRAVERSE_STMT(MSDependentExistsStmt, { 1924 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1925 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1926}) 1927DEF_TRAVERSE_STMT(ReturnStmt, {}) 1928DEF_TRAVERSE_STMT(SwitchStmt, {}) 1929DEF_TRAVERSE_STMT(WhileStmt, {}) 1930 1931DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, { 1932 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1933 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 1934 if (S->hasExplicitTemplateArgs()) { 1935 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1936 S->getNumTemplateArgs())); 1937 } 1938}) 1939 1940DEF_TRAVERSE_STMT(DeclRefExpr, { 1941 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1942 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1943 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1944 S->getNumTemplateArgs())); 1945}) 1946 1947DEF_TRAVERSE_STMT(DependentScopeDeclRefExpr, { 1948 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1949 TRY_TO(TraverseDeclarationNameInfo(S->getNameInfo())); 1950 if (S->hasExplicitTemplateArgs()) { 1951 TRY_TO(TraverseTemplateArgumentLocsHelper( 1952 S->getExplicitTemplateArgs().getTemplateArgs(), 1953 S->getNumTemplateArgs())); 1954 } 1955}) 1956 1957DEF_TRAVERSE_STMT(MemberExpr, { 1958 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 1959 TRY_TO(TraverseDeclarationNameInfo(S->getMemberNameInfo())); 1960 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 1961 S->getNumTemplateArgs())); 1962}) 1963 1964DEF_TRAVERSE_STMT( 1965 ImplicitCastExpr, 1966 {// We don't traverse the cast type, as it's not written in the 1967 // source code. 1968 }) 1969 1970DEF_TRAVERSE_STMT(CStyleCastExpr, { 1971 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1972}) 1973 1974DEF_TRAVERSE_STMT(CXXFunctionalCastExpr, { 1975 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1976}) 1977 1978DEF_TRAVERSE_STMT(CXXConstCastExpr, { 1979 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1980}) 1981 1982DEF_TRAVERSE_STMT(CXXDynamicCastExpr, { 1983 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1984}) 1985 1986DEF_TRAVERSE_STMT(CXXReinterpretCastExpr, { 1987 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1988}) 1989 1990DEF_TRAVERSE_STMT(CXXStaticCastExpr, { 1991 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 1992}) 1993 1994// InitListExpr is a tricky one, because we want to do all our work on 1995// the syntactic form of the listexpr, but this method takes the 1996// semantic form by default. We can't use the macro helper because it 1997// calls WalkUp*() on the semantic form, before our code can convert 1998// to the syntactic form. 1999template <typename Derived> 2000bool RecursiveASTVisitor<Derived>::TraverseInitListExpr(InitListExpr *S) { 2001 if (InitListExpr *Syn = S->getSyntacticForm()) 2002 S = Syn; 2003 TRY_TO(WalkUpFromInitListExpr(S)); 2004 StmtQueueAction StmtQueue(*this); 2005 // All we need are the default actions. FIXME: use a helper function. 2006 for (Stmt::child_range range = S->children(); range; ++range) { 2007 StmtQueue.queue(*range); 2008 } 2009 return true; 2010} 2011 2012// GenericSelectionExpr is a special case because the types and expressions 2013// are interleaved. We also need to watch out for null types (default 2014// generic associations). 2015template <typename Derived> 2016bool RecursiveASTVisitor<Derived>::TraverseGenericSelectionExpr( 2017 GenericSelectionExpr *S) { 2018 TRY_TO(WalkUpFromGenericSelectionExpr(S)); 2019 StmtQueueAction StmtQueue(*this); 2020 StmtQueue.queue(S->getControllingExpr()); 2021 for (unsigned i = 0; i != S->getNumAssocs(); ++i) { 2022 if (TypeSourceInfo *TS = S->getAssocTypeSourceInfo(i)) 2023 TRY_TO(TraverseTypeLoc(TS->getTypeLoc())); 2024 StmtQueue.queue(S->getAssocExpr(i)); 2025 } 2026 return true; 2027} 2028 2029// PseudoObjectExpr is a special case because of the wierdness with 2030// syntactic expressions and opaque values. 2031template <typename Derived> 2032bool 2033RecursiveASTVisitor<Derived>::TraversePseudoObjectExpr(PseudoObjectExpr *S) { 2034 TRY_TO(WalkUpFromPseudoObjectExpr(S)); 2035 StmtQueueAction StmtQueue(*this); 2036 StmtQueue.queue(S->getSyntacticForm()); 2037 for (PseudoObjectExpr::semantics_iterator i = S->semantics_begin(), 2038 e = S->semantics_end(); 2039 i != e; ++i) { 2040 Expr *sub = *i; 2041 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub)) 2042 sub = OVE->getSourceExpr(); 2043 StmtQueue.queue(sub); 2044 } 2045 return true; 2046} 2047 2048DEF_TRAVERSE_STMT(CXXScalarValueInitExpr, { 2049 // This is called for code like 'return T()' where T is a built-in 2050 // (i.e. non-class) type. 2051 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2052}) 2053 2054DEF_TRAVERSE_STMT(CXXNewExpr, { 2055 // The child-iterator will pick up the other arguments. 2056 TRY_TO(TraverseTypeLoc(S->getAllocatedTypeSourceInfo()->getTypeLoc())); 2057}) 2058 2059DEF_TRAVERSE_STMT(OffsetOfExpr, { 2060 // The child-iterator will pick up the expression representing 2061 // the field. 2062 // FIMXE: for code like offsetof(Foo, a.b.c), should we get 2063 // making a MemberExpr callbacks for Foo.a, Foo.a.b, and Foo.a.b.c? 2064 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2065}) 2066 2067DEF_TRAVERSE_STMT(UnaryExprOrTypeTraitExpr, { 2068 // The child-iterator will pick up the arg if it's an expression, 2069 // but not if it's a type. 2070 if (S->isArgumentType()) 2071 TRY_TO(TraverseTypeLoc(S->getArgumentTypeInfo()->getTypeLoc())); 2072}) 2073 2074DEF_TRAVERSE_STMT(CXXTypeidExpr, { 2075 // The child-iterator will pick up the arg if it's an expression, 2076 // but not if it's a type. 2077 if (S->isTypeOperand()) 2078 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2079}) 2080 2081DEF_TRAVERSE_STMT(MSPropertyRefExpr, { 2082 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2083}) 2084 2085DEF_TRAVERSE_STMT(CXXUuidofExpr, { 2086 // The child-iterator will pick up the arg if it's an expression, 2087 // but not if it's a type. 2088 if (S->isTypeOperand()) 2089 TRY_TO(TraverseTypeLoc(S->getTypeOperandSourceInfo()->getTypeLoc())); 2090}) 2091 2092DEF_TRAVERSE_STMT(TypeTraitExpr, { 2093 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 2094 TRY_TO(TraverseTypeLoc(S->getArg(I)->getTypeLoc())); 2095}) 2096 2097DEF_TRAVERSE_STMT(ArrayTypeTraitExpr, { 2098 TRY_TO(TraverseTypeLoc(S->getQueriedTypeSourceInfo()->getTypeLoc())); 2099}) 2100 2101DEF_TRAVERSE_STMT(ExpressionTraitExpr, 2102 { StmtQueue.queue(S->getQueriedExpression()); }) 2103 2104DEF_TRAVERSE_STMT(VAArgExpr, { 2105 // The child-iterator will pick up the expression argument. 2106 TRY_TO(TraverseTypeLoc(S->getWrittenTypeInfo()->getTypeLoc())); 2107}) 2108 2109DEF_TRAVERSE_STMT(CXXTemporaryObjectExpr, { 2110 // This is called for code like 'return T()' where T is a class type. 2111 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2112}) 2113 2114// Walk only the visible parts of lambda expressions. 2115template <typename Derived> 2116bool RecursiveASTVisitor<Derived>::TraverseLambdaExpr(LambdaExpr *S) { 2117 TRY_TO(WalkUpFromLambdaExpr(S)); 2118 2119 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(), 2120 CEnd = S->explicit_capture_end(); 2121 C != CEnd; ++C) { 2122 TRY_TO(TraverseLambdaCapture(S, C)); 2123 } 2124 2125 if (S->hasExplicitParameters() || S->hasExplicitResultType()) { 2126 TypeLoc TL = S->getCallOperator()->getTypeSourceInfo()->getTypeLoc(); 2127 if (S->hasExplicitParameters() && S->hasExplicitResultType()) { 2128 // Visit the whole type. 2129 TRY_TO(TraverseTypeLoc(TL)); 2130 } else if (FunctionProtoTypeLoc Proto = TL.getAs<FunctionProtoTypeLoc>()) { 2131 if (S->hasExplicitParameters()) { 2132 // Visit parameters. 2133 for (unsigned I = 0, N = Proto.getNumParams(); I != N; ++I) { 2134 TRY_TO(TraverseDecl(Proto.getParam(I))); 2135 } 2136 } else { 2137 TRY_TO(TraverseTypeLoc(Proto.getReturnLoc())); 2138 } 2139 } 2140 } 2141 2142 TRY_TO(TraverseLambdaBody(S)); 2143 return true; 2144} 2145 2146DEF_TRAVERSE_STMT(CXXUnresolvedConstructExpr, { 2147 // This is called for code like 'T()', where T is a template argument. 2148 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2149}) 2150 2151// These expressions all might take explicit template arguments. 2152// We traverse those if so. FIXME: implement these. 2153DEF_TRAVERSE_STMT(CXXConstructExpr, {}) 2154DEF_TRAVERSE_STMT(CallExpr, {}) 2155DEF_TRAVERSE_STMT(CXXMemberCallExpr, {}) 2156 2157// These exprs (most of them), do not need any action except iterating 2158// over the children. 2159DEF_TRAVERSE_STMT(AddrLabelExpr, {}) 2160DEF_TRAVERSE_STMT(ArraySubscriptExpr, {}) 2161DEF_TRAVERSE_STMT(BlockExpr, { 2162 TRY_TO(TraverseDecl(S->getBlockDecl())); 2163 return true; // no child statements to loop through. 2164}) 2165DEF_TRAVERSE_STMT(ChooseExpr, {}) 2166DEF_TRAVERSE_STMT(CompoundLiteralExpr, { 2167 TRY_TO(TraverseTypeLoc(S->getTypeSourceInfo()->getTypeLoc())); 2168}) 2169DEF_TRAVERSE_STMT(CXXBindTemporaryExpr, {}) 2170DEF_TRAVERSE_STMT(CXXBoolLiteralExpr, {}) 2171DEF_TRAVERSE_STMT(CXXDefaultArgExpr, {}) 2172DEF_TRAVERSE_STMT(CXXDefaultInitExpr, {}) 2173DEF_TRAVERSE_STMT(CXXDeleteExpr, {}) 2174DEF_TRAVERSE_STMT(ExprWithCleanups, {}) 2175DEF_TRAVERSE_STMT(CXXNullPtrLiteralExpr, {}) 2176DEF_TRAVERSE_STMT(CXXStdInitializerListExpr, {}) 2177DEF_TRAVERSE_STMT(CXXPseudoDestructorExpr, { 2178 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2179 if (TypeSourceInfo *ScopeInfo = S->getScopeTypeInfo()) 2180 TRY_TO(TraverseTypeLoc(ScopeInfo->getTypeLoc())); 2181 if (TypeSourceInfo *DestroyedTypeInfo = S->getDestroyedTypeInfo()) 2182 TRY_TO(TraverseTypeLoc(DestroyedTypeInfo->getTypeLoc())); 2183}) 2184DEF_TRAVERSE_STMT(CXXThisExpr, {}) 2185DEF_TRAVERSE_STMT(CXXThrowExpr, {}) 2186DEF_TRAVERSE_STMT(UserDefinedLiteral, {}) 2187DEF_TRAVERSE_STMT(DesignatedInitExpr, {}) 2188DEF_TRAVERSE_STMT(ExtVectorElementExpr, {}) 2189DEF_TRAVERSE_STMT(GNUNullExpr, {}) 2190DEF_TRAVERSE_STMT(ImplicitValueInitExpr, {}) 2191DEF_TRAVERSE_STMT(ObjCBoolLiteralExpr, {}) 2192DEF_TRAVERSE_STMT(ObjCEncodeExpr, { 2193 if (TypeSourceInfo *TInfo = S->getEncodedTypeSourceInfo()) 2194 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2195}) 2196DEF_TRAVERSE_STMT(ObjCIsaExpr, {}) 2197DEF_TRAVERSE_STMT(ObjCIvarRefExpr, {}) 2198DEF_TRAVERSE_STMT(ObjCMessageExpr, { 2199 if (TypeSourceInfo *TInfo = S->getClassReceiverTypeInfo()) 2200 TRY_TO(TraverseTypeLoc(TInfo->getTypeLoc())); 2201}) 2202DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {}) 2203DEF_TRAVERSE_STMT(ObjCSubscriptRefExpr, {}) 2204DEF_TRAVERSE_STMT(ObjCProtocolExpr, {}) 2205DEF_TRAVERSE_STMT(ObjCSelectorExpr, {}) 2206DEF_TRAVERSE_STMT(ObjCIndirectCopyRestoreExpr, {}) 2207DEF_TRAVERSE_STMT(ObjCBridgedCastExpr, { 2208 TRY_TO(TraverseTypeLoc(S->getTypeInfoAsWritten()->getTypeLoc())); 2209}) 2210DEF_TRAVERSE_STMT(ParenExpr, {}) 2211DEF_TRAVERSE_STMT(ParenListExpr, {}) 2212DEF_TRAVERSE_STMT(PredefinedExpr, {}) 2213DEF_TRAVERSE_STMT(ShuffleVectorExpr, {}) 2214DEF_TRAVERSE_STMT(ConvertVectorExpr, {}) 2215DEF_TRAVERSE_STMT(StmtExpr, {}) 2216DEF_TRAVERSE_STMT(UnresolvedLookupExpr, { 2217 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2218 if (S->hasExplicitTemplateArgs()) { 2219 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2220 S->getNumTemplateArgs())); 2221 } 2222}) 2223 2224DEF_TRAVERSE_STMT(UnresolvedMemberExpr, { 2225 TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc())); 2226 if (S->hasExplicitTemplateArgs()) { 2227 TRY_TO(TraverseTemplateArgumentLocsHelper(S->getTemplateArgs(), 2228 S->getNumTemplateArgs())); 2229 } 2230}) 2231 2232DEF_TRAVERSE_STMT(SEHTryStmt, {}) 2233DEF_TRAVERSE_STMT(SEHExceptStmt, {}) 2234DEF_TRAVERSE_STMT(SEHFinallyStmt, {}) 2235DEF_TRAVERSE_STMT(SEHLeaveStmt, {}) 2236DEF_TRAVERSE_STMT(CapturedStmt, { TRY_TO(TraverseDecl(S->getCapturedDecl())); }) 2237 2238DEF_TRAVERSE_STMT(CXXOperatorCallExpr, {}) 2239DEF_TRAVERSE_STMT(OpaqueValueExpr, {}) 2240DEF_TRAVERSE_STMT(CUDAKernelCallExpr, {}) 2241 2242// These operators (all of them) do not need any action except 2243// iterating over the children. 2244DEF_TRAVERSE_STMT(BinaryConditionalOperator, {}) 2245DEF_TRAVERSE_STMT(ConditionalOperator, {}) 2246DEF_TRAVERSE_STMT(UnaryOperator, {}) 2247DEF_TRAVERSE_STMT(BinaryOperator, {}) 2248DEF_TRAVERSE_STMT(CompoundAssignOperator, {}) 2249DEF_TRAVERSE_STMT(CXXNoexceptExpr, {}) 2250DEF_TRAVERSE_STMT(PackExpansionExpr, {}) 2251DEF_TRAVERSE_STMT(SizeOfPackExpr, {}) 2252DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmPackExpr, {}) 2253DEF_TRAVERSE_STMT(SubstNonTypeTemplateParmExpr, {}) 2254DEF_TRAVERSE_STMT(FunctionParmPackExpr, {}) 2255DEF_TRAVERSE_STMT(MaterializeTemporaryExpr, {}) 2256DEF_TRAVERSE_STMT(AtomicExpr, {}) 2257 2258// These literals (all of them) do not need any action. 2259DEF_TRAVERSE_STMT(IntegerLiteral, {}) 2260DEF_TRAVERSE_STMT(CharacterLiteral, {}) 2261DEF_TRAVERSE_STMT(FloatingLiteral, {}) 2262DEF_TRAVERSE_STMT(ImaginaryLiteral, {}) 2263DEF_TRAVERSE_STMT(StringLiteral, {}) 2264DEF_TRAVERSE_STMT(ObjCStringLiteral, {}) 2265DEF_TRAVERSE_STMT(ObjCBoxedExpr, {}) 2266DEF_TRAVERSE_STMT(ObjCArrayLiteral, {}) 2267DEF_TRAVERSE_STMT(ObjCDictionaryLiteral, {}) 2268 2269// Traverse OpenCL: AsType, Convert. 2270DEF_TRAVERSE_STMT(AsTypeExpr, {}) 2271 2272// OpenMP directives. 2273template <typename Derived> 2274bool RecursiveASTVisitor<Derived>::TraverseOMPExecutableDirective( 2275 OMPExecutableDirective *S) { 2276 for (auto *C : S->clauses()) { 2277 TRY_TO(TraverseOMPClause(C)); 2278 } 2279 return true; 2280} 2281 2282DEF_TRAVERSE_STMT(OMPParallelDirective, 2283 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2284 2285DEF_TRAVERSE_STMT(OMPSimdDirective, 2286 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2287 2288DEF_TRAVERSE_STMT(OMPForDirective, 2289 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2290 2291DEF_TRAVERSE_STMT(OMPSectionsDirective, 2292 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2293 2294DEF_TRAVERSE_STMT(OMPSectionDirective, 2295 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2296 2297DEF_TRAVERSE_STMT(OMPSingleDirective, 2298 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2299 2300DEF_TRAVERSE_STMT(OMPParallelForDirective, 2301 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2302 2303DEF_TRAVERSE_STMT(OMPParallelSectionsDirective, 2304 { TRY_TO(TraverseOMPExecutableDirective(S)); }) 2305 2306// OpenMP clauses. 2307template <typename Derived> 2308bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) { 2309 if (!C) 2310 return true; 2311 switch (C->getClauseKind()) { 2312#define OPENMP_CLAUSE(Name, Class) \ 2313 case OMPC_##Name: \ 2314 TRY_TO(Visit##Class(static_cast<Class *>(C))); \ 2315 break; 2316#include "clang/Basic/OpenMPKinds.def" 2317 case OMPC_threadprivate: 2318 case OMPC_unknown: 2319 break; 2320 } 2321 return true; 2322} 2323 2324template <typename Derived> 2325bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) { 2326 TRY_TO(TraverseStmt(C->getCondition())); 2327 return true; 2328} 2329 2330template <typename Derived> 2331bool 2332RecursiveASTVisitor<Derived>::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 2333 TRY_TO(TraverseStmt(C->getNumThreads())); 2334 return true; 2335} 2336 2337template <typename Derived> 2338bool RecursiveASTVisitor<Derived>::VisitOMPSafelenClause(OMPSafelenClause *C) { 2339 TRY_TO(TraverseStmt(C->getSafelen())); 2340 return true; 2341} 2342 2343template <typename Derived> 2344bool 2345RecursiveASTVisitor<Derived>::VisitOMPCollapseClause(OMPCollapseClause *C) { 2346 TRY_TO(TraverseStmt(C->getNumForLoops())); 2347 return true; 2348} 2349 2350template <typename Derived> 2351bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *) { 2352 return true; 2353} 2354 2355template <typename Derived> 2356bool RecursiveASTVisitor<Derived>::VisitOMPProcBindClause(OMPProcBindClause *) { 2357 return true; 2358} 2359 2360template <typename Derived> 2361bool 2362RecursiveASTVisitor<Derived>::VisitOMPScheduleClause(OMPScheduleClause *C) { 2363 TRY_TO(TraverseStmt(C->getChunkSize())); 2364 return true; 2365} 2366 2367template <typename Derived> 2368bool RecursiveASTVisitor<Derived>::VisitOMPOrderedClause(OMPOrderedClause *) { 2369 return true; 2370} 2371 2372template <typename Derived> 2373bool RecursiveASTVisitor<Derived>::VisitOMPNowaitClause(OMPNowaitClause *) { 2374 return true; 2375} 2376 2377template <typename Derived> 2378template <typename T> 2379bool RecursiveASTVisitor<Derived>::VisitOMPClauseList(T *Node) { 2380 for (auto *E : Node->varlists()) { 2381 TRY_TO(TraverseStmt(E)); 2382 } 2383 return true; 2384} 2385 2386template <typename Derived> 2387bool RecursiveASTVisitor<Derived>::VisitOMPPrivateClause(OMPPrivateClause *C) { 2388 TRY_TO(VisitOMPClauseList(C)); 2389 return true; 2390} 2391 2392template <typename Derived> 2393bool RecursiveASTVisitor<Derived>::VisitOMPFirstprivateClause( 2394 OMPFirstprivateClause *C) { 2395 TRY_TO(VisitOMPClauseList(C)); 2396 return true; 2397} 2398 2399template <typename Derived> 2400bool RecursiveASTVisitor<Derived>::VisitOMPLastprivateClause( 2401 OMPLastprivateClause *C) { 2402 TRY_TO(VisitOMPClauseList(C)); 2403 return true; 2404} 2405 2406template <typename Derived> 2407bool RecursiveASTVisitor<Derived>::VisitOMPSharedClause(OMPSharedClause *C) { 2408 TRY_TO(VisitOMPClauseList(C)); 2409 return true; 2410} 2411 2412template <typename Derived> 2413bool RecursiveASTVisitor<Derived>::VisitOMPLinearClause(OMPLinearClause *C) { 2414 TRY_TO(TraverseStmt(C->getStep())); 2415 TRY_TO(VisitOMPClauseList(C)); 2416 return true; 2417} 2418 2419template <typename Derived> 2420bool RecursiveASTVisitor<Derived>::VisitOMPAlignedClause(OMPAlignedClause *C) { 2421 TRY_TO(TraverseStmt(C->getAlignment())); 2422 TRY_TO(VisitOMPClauseList(C)); 2423 return true; 2424} 2425 2426template <typename Derived> 2427bool RecursiveASTVisitor<Derived>::VisitOMPCopyinClause(OMPCopyinClause *C) { 2428 TRY_TO(VisitOMPClauseList(C)); 2429 return true; 2430} 2431 2432template <typename Derived> 2433bool RecursiveASTVisitor<Derived>::VisitOMPCopyprivateClause( 2434 OMPCopyprivateClause *C) { 2435 TRY_TO(VisitOMPClauseList(C)); 2436 return true; 2437} 2438 2439template <typename Derived> 2440bool 2441RecursiveASTVisitor<Derived>::VisitOMPReductionClause(OMPReductionClause *C) { 2442 TRY_TO(TraverseNestedNameSpecifierLoc(C->getQualifierLoc())); 2443 TRY_TO(TraverseDeclarationNameInfo(C->getNameInfo())); 2444 TRY_TO(VisitOMPClauseList(C)); 2445 return true; 2446} 2447 2448// FIXME: look at the following tricky-seeming exprs to see if we 2449// need to recurse on anything. These are ones that have methods 2450// returning decls or qualtypes or nestednamespecifier -- though I'm 2451// not sure if they own them -- or just seemed very complicated, or 2452// had lots of sub-types to explore. 2453// 2454// VisitOverloadExpr and its children: recurse on template args? etc? 2455 2456// FIXME: go through all the stmts and exprs again, and see which of them 2457// create new types, and recurse on the types (TypeLocs?) of those. 2458// Candidates: 2459// 2460// http://clang.llvm.org/doxygen/classclang_1_1CXXTypeidExpr.html 2461// http://clang.llvm.org/doxygen/classclang_1_1UnaryExprOrTypeTraitExpr.html 2462// http://clang.llvm.org/doxygen/classclang_1_1TypesCompatibleExpr.html 2463// Every class that has getQualifier. 2464 2465#undef DEF_TRAVERSE_STMT 2466 2467#undef TRY_TO 2468 2469#undef RecursiveASTVisitor 2470 2471} // end namespace clang 2472 2473#endif // LLVM_CLANG_LIBCLANG_RECURSIVEASTVISITOR_H 2474