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