RecursiveASTVisitor.h 117 KB

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