ASTNodeTraverser.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- ASTNodeTraverser.h - Traversal of AST nodes ----------------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements the AST traversal facilities. Other users
  15. // of this class may make use of the same traversal logic by inheriting it,
  16. // similar to RecursiveASTVisitor.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_AST_ASTNODETRAVERSER_H
  20. #define LLVM_CLANG_AST_ASTNODETRAVERSER_H
  21. #include "clang/AST/ASTTypeTraits.h"
  22. #include "clang/AST/AttrVisitor.h"
  23. #include "clang/AST/CommentVisitor.h"
  24. #include "clang/AST/DeclVisitor.h"
  25. #include "clang/AST/LocInfoType.h"
  26. #include "clang/AST/StmtVisitor.h"
  27. #include "clang/AST/TemplateArgumentVisitor.h"
  28. #include "clang/AST/Type.h"
  29. #include "clang/AST/TypeVisitor.h"
  30. namespace clang {
  31. class APValue;
  32. /**
  33. ASTNodeTraverser traverses the Clang AST for dumping purposes.
  34. The `Derived::doGetNodeDelegate()` method is required to be an accessible member
  35. which returns a reference of type `NodeDelegateType &` which implements the
  36. following interface:
  37. struct {
  38. template <typename Fn> void AddChild(Fn DoAddChild);
  39. template <typename Fn> void AddChild(StringRef Label, Fn DoAddChild);
  40. void Visit(const comments::Comment *C, const comments::FullComment *FC);
  41. void Visit(const Attr *A);
  42. void Visit(const TemplateArgument &TA, SourceRange R = {},
  43. const Decl *From = nullptr, StringRef Label = {});
  44. void Visit(const Stmt *Node);
  45. void Visit(const Type *T);
  46. void Visit(QualType T);
  47. void Visit(const Decl *D);
  48. void Visit(const CXXCtorInitializer *Init);
  49. void Visit(const OMPClause *C);
  50. void Visit(const BlockDecl::Capture &C);
  51. void Visit(const GenericSelectionExpr::ConstAssociation &A);
  52. void Visit(const concepts::Requirement *R);
  53. void Visit(const APValue &Value, QualType Ty);
  54. };
  55. */
  56. template <typename Derived, typename NodeDelegateType>
  57. class ASTNodeTraverser
  58. : public ConstDeclVisitor<Derived>,
  59. public ConstStmtVisitor<Derived>,
  60. public comments::ConstCommentVisitor<Derived, void,
  61. const comments::FullComment *>,
  62. public TypeVisitor<Derived>,
  63. public ConstAttrVisitor<Derived>,
  64. public ConstTemplateArgumentVisitor<Derived> {
  65. /// Indicates whether we should trigger deserialization of nodes that had
  66. /// not already been loaded.
  67. bool Deserialize = false;
  68. TraversalKind Traversal = TraversalKind::TK_AsIs;
  69. NodeDelegateType &getNodeDelegate() {
  70. return getDerived().doGetNodeDelegate();
  71. }
  72. Derived &getDerived() { return *static_cast<Derived *>(this); }
  73. public:
  74. void setDeserialize(bool D) { Deserialize = D; }
  75. bool getDeserialize() const { return Deserialize; }
  76. void SetTraversalKind(TraversalKind TK) { Traversal = TK; }
  77. TraversalKind GetTraversalKind() const { return Traversal; }
  78. void Visit(const Decl *D) {
  79. if (Traversal == TK_IgnoreUnlessSpelledInSource && D->isImplicit())
  80. return;
  81. getNodeDelegate().AddChild([=] {
  82. getNodeDelegate().Visit(D);
  83. if (!D)
  84. return;
  85. ConstDeclVisitor<Derived>::Visit(D);
  86. for (const auto &A : D->attrs())
  87. Visit(A);
  88. if (const comments::FullComment *Comment =
  89. D->getASTContext().getLocalCommentForDeclUncached(D))
  90. Visit(Comment, Comment);
  91. // Decls within functions are visited by the body.
  92. if (!isa<FunctionDecl>(*D) && !isa<ObjCMethodDecl>(*D)) {
  93. if (Traversal != TK_AsIs) {
  94. if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
  95. auto SK = CTSD->getSpecializationKind();
  96. if (SK == TSK_ExplicitInstantiationDeclaration ||
  97. SK == TSK_ExplicitInstantiationDefinition)
  98. return;
  99. }
  100. }
  101. if (const auto *DC = dyn_cast<DeclContext>(D))
  102. dumpDeclContext(DC);
  103. }
  104. });
  105. }
  106. void Visit(const Stmt *Node, StringRef Label = {}) {
  107. getNodeDelegate().AddChild(Label, [=] {
  108. const Stmt *S = Node;
  109. if (auto *E = dyn_cast_or_null<Expr>(S)) {
  110. switch (Traversal) {
  111. case TK_AsIs:
  112. break;
  113. case TK_IgnoreUnlessSpelledInSource:
  114. S = E->IgnoreUnlessSpelledInSource();
  115. break;
  116. }
  117. }
  118. getNodeDelegate().Visit(S);
  119. if (!S) {
  120. return;
  121. }
  122. ConstStmtVisitor<Derived>::Visit(S);
  123. // Some statements have custom mechanisms for dumping their children.
  124. if (isa<DeclStmt>(S) || isa<GenericSelectionExpr>(S) ||
  125. isa<RequiresExpr>(S))
  126. return;
  127. if (Traversal == TK_IgnoreUnlessSpelledInSource &&
  128. isa<LambdaExpr, CXXForRangeStmt, CallExpr,
  129. CXXRewrittenBinaryOperator>(S))
  130. return;
  131. for (const Stmt *SubStmt : S->children())
  132. Visit(SubStmt);
  133. });
  134. }
  135. void Visit(QualType T) {
  136. SplitQualType SQT = T.split();
  137. if (!SQT.Quals.hasQualifiers())
  138. return Visit(SQT.Ty);
  139. getNodeDelegate().AddChild([=] {
  140. getNodeDelegate().Visit(T);
  141. Visit(T.split().Ty);
  142. });
  143. }
  144. void Visit(const Type *T) {
  145. getNodeDelegate().AddChild([=] {
  146. getNodeDelegate().Visit(T);
  147. if (!T)
  148. return;
  149. TypeVisitor<Derived>::Visit(T);
  150. QualType SingleStepDesugar =
  151. T->getLocallyUnqualifiedSingleStepDesugaredType();
  152. if (SingleStepDesugar != QualType(T, 0))
  153. Visit(SingleStepDesugar);
  154. });
  155. }
  156. void Visit(const Attr *A) {
  157. getNodeDelegate().AddChild([=] {
  158. getNodeDelegate().Visit(A);
  159. ConstAttrVisitor<Derived>::Visit(A);
  160. });
  161. }
  162. void Visit(const CXXCtorInitializer *Init) {
  163. if (Traversal == TK_IgnoreUnlessSpelledInSource && !Init->isWritten())
  164. return;
  165. getNodeDelegate().AddChild([=] {
  166. getNodeDelegate().Visit(Init);
  167. Visit(Init->getInit());
  168. });
  169. }
  170. void Visit(const TemplateArgument &A, SourceRange R = {},
  171. const Decl *From = nullptr, const char *Label = nullptr) {
  172. getNodeDelegate().AddChild([=] {
  173. getNodeDelegate().Visit(A, R, From, Label);
  174. ConstTemplateArgumentVisitor<Derived>::Visit(A);
  175. });
  176. }
  177. void Visit(const BlockDecl::Capture &C) {
  178. getNodeDelegate().AddChild([=] {
  179. getNodeDelegate().Visit(C);
  180. if (C.hasCopyExpr())
  181. Visit(C.getCopyExpr());
  182. });
  183. }
  184. void Visit(const OMPClause *C) {
  185. getNodeDelegate().AddChild([=] {
  186. getNodeDelegate().Visit(C);
  187. for (const auto *S : C->children())
  188. Visit(S);
  189. });
  190. }
  191. void Visit(const GenericSelectionExpr::ConstAssociation &A) {
  192. getNodeDelegate().AddChild([=] {
  193. getNodeDelegate().Visit(A);
  194. if (const TypeSourceInfo *TSI = A.getTypeSourceInfo())
  195. Visit(TSI->getType());
  196. Visit(A.getAssociationExpr());
  197. });
  198. }
  199. void Visit(const concepts::Requirement *R) {
  200. getNodeDelegate().AddChild([=] {
  201. getNodeDelegate().Visit(R);
  202. if (!R)
  203. return;
  204. if (auto *TR = dyn_cast<concepts::TypeRequirement>(R)) {
  205. if (!TR->isSubstitutionFailure())
  206. Visit(TR->getType()->getType().getTypePtr());
  207. } else if (auto *ER = dyn_cast<concepts::ExprRequirement>(R)) {
  208. if (!ER->isExprSubstitutionFailure())
  209. Visit(ER->getExpr());
  210. if (!ER->getReturnTypeRequirement().isEmpty())
  211. Visit(ER->getReturnTypeRequirement()
  212. .getTypeConstraint()
  213. ->getImmediatelyDeclaredConstraint());
  214. } else if (auto *NR = dyn_cast<concepts::NestedRequirement>(R)) {
  215. if (!NR->isSubstitutionFailure())
  216. Visit(NR->getConstraintExpr());
  217. }
  218. });
  219. }
  220. void Visit(const APValue &Value, QualType Ty) {
  221. getNodeDelegate().AddChild([=] { getNodeDelegate().Visit(Value, Ty); });
  222. }
  223. void Visit(const comments::Comment *C, const comments::FullComment *FC) {
  224. getNodeDelegate().AddChild([=] {
  225. getNodeDelegate().Visit(C, FC);
  226. if (!C) {
  227. return;
  228. }
  229. comments::ConstCommentVisitor<Derived, void,
  230. const comments::FullComment *>::visit(C,
  231. FC);
  232. for (comments::Comment::child_iterator I = C->child_begin(),
  233. E = C->child_end();
  234. I != E; ++I)
  235. Visit(*I, FC);
  236. });
  237. }
  238. void Visit(const DynTypedNode &N) {
  239. // FIXME: Improve this with a switch or a visitor pattern.
  240. if (const auto *D = N.get<Decl>())
  241. Visit(D);
  242. else if (const auto *S = N.get<Stmt>())
  243. Visit(S);
  244. else if (const auto *QT = N.get<QualType>())
  245. Visit(*QT);
  246. else if (const auto *T = N.get<Type>())
  247. Visit(T);
  248. else if (const auto *C = N.get<CXXCtorInitializer>())
  249. Visit(C);
  250. else if (const auto *C = N.get<OMPClause>())
  251. Visit(C);
  252. else if (const auto *T = N.get<TemplateArgument>())
  253. Visit(*T);
  254. }
  255. void dumpDeclContext(const DeclContext *DC) {
  256. if (!DC)
  257. return;
  258. for (const auto *D : (Deserialize ? DC->decls() : DC->noload_decls()))
  259. Visit(D);
  260. }
  261. void dumpTemplateParameters(const TemplateParameterList *TPL) {
  262. if (!TPL)
  263. return;
  264. for (const auto &TP : *TPL)
  265. Visit(TP);
  266. if (const Expr *RC = TPL->getRequiresClause())
  267. Visit(RC);
  268. }
  269. void
  270. dumpASTTemplateArgumentListInfo(const ASTTemplateArgumentListInfo *TALI) {
  271. if (!TALI)
  272. return;
  273. for (const auto &TA : TALI->arguments())
  274. dumpTemplateArgumentLoc(TA);
  275. }
  276. void dumpTemplateArgumentLoc(const TemplateArgumentLoc &A,
  277. const Decl *From = nullptr,
  278. const char *Label = nullptr) {
  279. Visit(A.getArgument(), A.getSourceRange(), From, Label);
  280. }
  281. void dumpTemplateArgumentList(const TemplateArgumentList &TAL) {
  282. for (unsigned i = 0, e = TAL.size(); i < e; ++i)
  283. Visit(TAL[i]);
  284. }
  285. void dumpObjCTypeParamList(const ObjCTypeParamList *typeParams) {
  286. if (!typeParams)
  287. return;
  288. for (const auto &typeParam : *typeParams) {
  289. Visit(typeParam);
  290. }
  291. }
  292. void VisitComplexType(const ComplexType *T) { Visit(T->getElementType()); }
  293. void VisitLocInfoType(const LocInfoType *T) {
  294. Visit(T->getTypeSourceInfo()->getType());
  295. }
  296. void VisitPointerType(const PointerType *T) { Visit(T->getPointeeType()); }
  297. void VisitBlockPointerType(const BlockPointerType *T) {
  298. Visit(T->getPointeeType());
  299. }
  300. void VisitReferenceType(const ReferenceType *T) {
  301. Visit(T->getPointeeType());
  302. }
  303. void VisitMemberPointerType(const MemberPointerType *T) {
  304. Visit(T->getClass());
  305. Visit(T->getPointeeType());
  306. }
  307. void VisitArrayType(const ArrayType *T) { Visit(T->getElementType()); }
  308. void VisitVariableArrayType(const VariableArrayType *T) {
  309. VisitArrayType(T);
  310. Visit(T->getSizeExpr());
  311. }
  312. void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
  313. Visit(T->getElementType());
  314. Visit(T->getSizeExpr());
  315. }
  316. void VisitDependentSizedExtVectorType(const DependentSizedExtVectorType *T) {
  317. Visit(T->getElementType());
  318. Visit(T->getSizeExpr());
  319. }
  320. void VisitVectorType(const VectorType *T) { Visit(T->getElementType()); }
  321. void VisitFunctionType(const FunctionType *T) { Visit(T->getReturnType()); }
  322. void VisitFunctionProtoType(const FunctionProtoType *T) {
  323. VisitFunctionType(T);
  324. for (const QualType &PT : T->getParamTypes())
  325. Visit(PT);
  326. }
  327. void VisitTypeOfExprType(const TypeOfExprType *T) {
  328. Visit(T->getUnderlyingExpr());
  329. }
  330. void VisitDecltypeType(const DecltypeType *T) {
  331. Visit(T->getUnderlyingExpr());
  332. }
  333. void VisitUnaryTransformType(const UnaryTransformType *T) {
  334. Visit(T->getBaseType());
  335. }
  336. void VisitAttributedType(const AttributedType *T) {
  337. // FIXME: AttrKind
  338. Visit(T->getModifiedType());
  339. }
  340. void VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) {
  341. Visit(T->getReplacedParameter());
  342. }
  343. void
  344. VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) {
  345. Visit(T->getReplacedParameter());
  346. Visit(T->getArgumentPack());
  347. }
  348. void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
  349. for (const auto &Arg : *T)
  350. Visit(Arg);
  351. }
  352. void VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
  353. Visit(T->getPointeeType());
  354. }
  355. void VisitAtomicType(const AtomicType *T) { Visit(T->getValueType()); }
  356. void VisitPipeType(const PipeType *T) { Visit(T->getElementType()); }
  357. void VisitAdjustedType(const AdjustedType *T) { Visit(T->getOriginalType()); }
  358. void VisitPackExpansionType(const PackExpansionType *T) {
  359. if (!T->isSugared())
  360. Visit(T->getPattern());
  361. }
  362. // FIXME: ElaboratedType, DependentNameType,
  363. // DependentTemplateSpecializationType, ObjCObjectType
  364. void VisitTypedefDecl(const TypedefDecl *D) { Visit(D->getUnderlyingType()); }
  365. void VisitEnumConstantDecl(const EnumConstantDecl *D) {
  366. if (const Expr *Init = D->getInitExpr())
  367. Visit(Init);
  368. }
  369. void VisitFunctionDecl(const FunctionDecl *D) {
  370. if (const auto *FTSI = D->getTemplateSpecializationInfo())
  371. dumpTemplateArgumentList(*FTSI->TemplateArguments);
  372. if (D->param_begin())
  373. for (const auto *Parameter : D->parameters())
  374. Visit(Parameter);
  375. if (const Expr *TRC = D->getTrailingRequiresClause())
  376. Visit(TRC);
  377. if (Traversal == TK_IgnoreUnlessSpelledInSource && D->isDefaulted())
  378. return;
  379. if (const auto *C = dyn_cast<CXXConstructorDecl>(D))
  380. for (const auto *I : C->inits())
  381. Visit(I);
  382. if (D->doesThisDeclarationHaveABody())
  383. Visit(D->getBody());
  384. }
  385. void VisitFieldDecl(const FieldDecl *D) {
  386. if (D->isBitField())
  387. Visit(D->getBitWidth());
  388. if (Expr *Init = D->getInClassInitializer())
  389. Visit(Init);
  390. }
  391. void VisitVarDecl(const VarDecl *D) {
  392. if (Traversal == TK_IgnoreUnlessSpelledInSource && D->isCXXForRangeDecl())
  393. return;
  394. if (D->hasInit())
  395. Visit(D->getInit());
  396. }
  397. void VisitDecompositionDecl(const DecompositionDecl *D) {
  398. VisitVarDecl(D);
  399. for (const auto *B : D->bindings())
  400. Visit(B);
  401. }
  402. void VisitBindingDecl(const BindingDecl *D) {
  403. if (Traversal == TK_IgnoreUnlessSpelledInSource)
  404. return;
  405. if (const auto *E = D->getBinding())
  406. Visit(E);
  407. }
  408. void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
  409. Visit(D->getAsmString());
  410. }
  411. void VisitCapturedDecl(const CapturedDecl *D) { Visit(D->getBody()); }
  412. void VisitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
  413. for (const auto *E : D->varlists())
  414. Visit(E);
  415. }
  416. void VisitOMPDeclareReductionDecl(const OMPDeclareReductionDecl *D) {
  417. Visit(D->getCombiner());
  418. if (const auto *Initializer = D->getInitializer())
  419. Visit(Initializer);
  420. }
  421. void VisitOMPDeclareMapperDecl(const OMPDeclareMapperDecl *D) {
  422. for (const auto *C : D->clauselists())
  423. Visit(C);
  424. }
  425. void VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) {
  426. Visit(D->getInit());
  427. }
  428. void VisitOMPAllocateDecl(const OMPAllocateDecl *D) {
  429. for (const auto *E : D->varlists())
  430. Visit(E);
  431. for (const auto *C : D->clauselists())
  432. Visit(C);
  433. }
  434. template <typename SpecializationDecl>
  435. void dumpTemplateDeclSpecialization(const SpecializationDecl *D) {
  436. for (const auto *RedeclWithBadType : D->redecls()) {
  437. // FIXME: The redecls() range sometimes has elements of a less-specific
  438. // type. (In particular, ClassTemplateSpecializationDecl::redecls() gives
  439. // us TagDecls, and should give CXXRecordDecls).
  440. auto *Redecl = dyn_cast<SpecializationDecl>(RedeclWithBadType);
  441. if (!Redecl) {
  442. // Found the injected-class-name for a class template. This will be
  443. // dumped as part of its surrounding class so we don't need to dump it
  444. // here.
  445. assert(isa<CXXRecordDecl>(RedeclWithBadType) &&
  446. "expected an injected-class-name");
  447. continue;
  448. }
  449. Visit(Redecl);
  450. }
  451. }
  452. template <typename TemplateDecl>
  453. void dumpTemplateDecl(const TemplateDecl *D) {
  454. dumpTemplateParameters(D->getTemplateParameters());
  455. Visit(D->getTemplatedDecl());
  456. if (Traversal == TK_AsIs) {
  457. for (const auto *Child : D->specializations())
  458. dumpTemplateDeclSpecialization(Child);
  459. }
  460. }
  461. void VisitTypeAliasDecl(const TypeAliasDecl *D) {
  462. Visit(D->getUnderlyingType());
  463. }
  464. void VisitTypeAliasTemplateDecl(const TypeAliasTemplateDecl *D) {
  465. dumpTemplateParameters(D->getTemplateParameters());
  466. Visit(D->getTemplatedDecl());
  467. }
  468. void VisitStaticAssertDecl(const StaticAssertDecl *D) {
  469. Visit(D->getAssertExpr());
  470. Visit(D->getMessage());
  471. }
  472. void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
  473. dumpTemplateDecl(D);
  474. }
  475. void VisitClassTemplateDecl(const ClassTemplateDecl *D) {
  476. dumpTemplateDecl(D);
  477. }
  478. void VisitClassTemplateSpecializationDecl(
  479. const ClassTemplateSpecializationDecl *D) {
  480. dumpTemplateArgumentList(D->getTemplateArgs());
  481. }
  482. void VisitClassTemplatePartialSpecializationDecl(
  483. const ClassTemplatePartialSpecializationDecl *D) {
  484. VisitClassTemplateSpecializationDecl(D);
  485. dumpTemplateParameters(D->getTemplateParameters());
  486. }
  487. void VisitClassScopeFunctionSpecializationDecl(
  488. const ClassScopeFunctionSpecializationDecl *D) {
  489. Visit(D->getSpecialization());
  490. dumpASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
  491. }
  492. void VisitVarTemplateDecl(const VarTemplateDecl *D) { dumpTemplateDecl(D); }
  493. void VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) {
  494. dumpTemplateParameters(D->getTemplateParameters());
  495. }
  496. void
  497. VisitVarTemplateSpecializationDecl(const VarTemplateSpecializationDecl *D) {
  498. dumpTemplateArgumentList(D->getTemplateArgs());
  499. VisitVarDecl(D);
  500. }
  501. void VisitVarTemplatePartialSpecializationDecl(
  502. const VarTemplatePartialSpecializationDecl *D) {
  503. dumpTemplateParameters(D->getTemplateParameters());
  504. VisitVarTemplateSpecializationDecl(D);
  505. }
  506. void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
  507. if (const auto *TC = D->getTypeConstraint())
  508. Visit(TC->getImmediatelyDeclaredConstraint());
  509. if (D->hasDefaultArgument())
  510. Visit(D->getDefaultArgument(), SourceRange(),
  511. D->getDefaultArgStorage().getInheritedFrom(),
  512. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  513. }
  514. void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
  515. if (const auto *E = D->getPlaceholderTypeConstraint())
  516. Visit(E);
  517. if (D->hasDefaultArgument())
  518. Visit(D->getDefaultArgument(), SourceRange(),
  519. D->getDefaultArgStorage().getInheritedFrom(),
  520. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  521. }
  522. void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
  523. dumpTemplateParameters(D->getTemplateParameters());
  524. if (D->hasDefaultArgument())
  525. dumpTemplateArgumentLoc(
  526. D->getDefaultArgument(), D->getDefaultArgStorage().getInheritedFrom(),
  527. D->defaultArgumentWasInherited() ? "inherited from" : "previous");
  528. }
  529. void VisitConceptDecl(const ConceptDecl *D) {
  530. dumpTemplateParameters(D->getTemplateParameters());
  531. Visit(D->getConstraintExpr());
  532. }
  533. void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *CSE) {
  534. if (CSE->hasExplicitTemplateArgs())
  535. for (const auto &ArgLoc : CSE->getTemplateArgsAsWritten()->arguments())
  536. dumpTemplateArgumentLoc(ArgLoc);
  537. }
  538. void VisitUsingShadowDecl(const UsingShadowDecl *D) {
  539. if (auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl()))
  540. Visit(TD->getTypeForDecl());
  541. }
  542. void VisitFriendDecl(const FriendDecl *D) {
  543. if (!D->getFriendType())
  544. Visit(D->getFriendDecl());
  545. }
  546. void VisitObjCMethodDecl(const ObjCMethodDecl *D) {
  547. if (D->isThisDeclarationADefinition())
  548. dumpDeclContext(D);
  549. else
  550. for (const ParmVarDecl *Parameter : D->parameters())
  551. Visit(Parameter);
  552. if (D->hasBody())
  553. Visit(D->getBody());
  554. }
  555. void VisitObjCCategoryDecl(const ObjCCategoryDecl *D) {
  556. dumpObjCTypeParamList(D->getTypeParamList());
  557. }
  558. void VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) {
  559. dumpObjCTypeParamList(D->getTypeParamListAsWritten());
  560. }
  561. void VisitObjCImplementationDecl(const ObjCImplementationDecl *D) {
  562. for (const auto &I : D->inits())
  563. Visit(I);
  564. }
  565. void VisitBlockDecl(const BlockDecl *D) {
  566. for (const auto &I : D->parameters())
  567. Visit(I);
  568. for (const auto &I : D->captures())
  569. Visit(I);
  570. Visit(D->getBody());
  571. }
  572. void VisitDeclStmt(const DeclStmt *Node) {
  573. for (const auto &D : Node->decls())
  574. Visit(D);
  575. }
  576. void VisitAttributedStmt(const AttributedStmt *Node) {
  577. for (const auto *A : Node->getAttrs())
  578. Visit(A);
  579. }
  580. void VisitCXXCatchStmt(const CXXCatchStmt *Node) {
  581. Visit(Node->getExceptionDecl());
  582. }
  583. void VisitCapturedStmt(const CapturedStmt *Node) {
  584. Visit(Node->getCapturedDecl());
  585. }
  586. void VisitOMPExecutableDirective(const OMPExecutableDirective *Node) {
  587. for (const auto *C : Node->clauses())
  588. Visit(C);
  589. }
  590. void VisitInitListExpr(const InitListExpr *ILE) {
  591. if (auto *Filler = ILE->getArrayFiller()) {
  592. Visit(Filler, "array_filler");
  593. }
  594. }
  595. void VisitBlockExpr(const BlockExpr *Node) { Visit(Node->getBlockDecl()); }
  596. void VisitOpaqueValueExpr(const OpaqueValueExpr *Node) {
  597. if (Expr *Source = Node->getSourceExpr())
  598. Visit(Source);
  599. }
  600. void VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
  601. Visit(E->getControllingExpr());
  602. Visit(E->getControllingExpr()->getType()); // FIXME: remove
  603. for (const auto Assoc : E->associations()) {
  604. Visit(Assoc);
  605. }
  606. }
  607. void VisitRequiresExpr(const RequiresExpr *E) {
  608. for (auto *D : E->getLocalParameters())
  609. Visit(D);
  610. for (auto *R : E->getRequirements())
  611. Visit(R);
  612. }
  613. void VisitLambdaExpr(const LambdaExpr *Node) {
  614. if (Traversal == TK_IgnoreUnlessSpelledInSource) {
  615. for (unsigned I = 0, N = Node->capture_size(); I != N; ++I) {
  616. const auto *C = Node->capture_begin() + I;
  617. if (!C->isExplicit())
  618. continue;
  619. if (Node->isInitCapture(C))
  620. Visit(C->getCapturedVar());
  621. else
  622. Visit(Node->capture_init_begin()[I]);
  623. }
  624. dumpTemplateParameters(Node->getTemplateParameterList());
  625. for (const auto *P : Node->getCallOperator()->parameters())
  626. Visit(P);
  627. Visit(Node->getBody());
  628. } else {
  629. return Visit(Node->getLambdaClass());
  630. }
  631. }
  632. void VisitSizeOfPackExpr(const SizeOfPackExpr *Node) {
  633. if (Node->isPartiallySubstituted())
  634. for (const auto &A : Node->getPartialArguments())
  635. Visit(A);
  636. }
  637. void VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) {
  638. Visit(E->getParameter());
  639. }
  640. void VisitSubstNonTypeTemplateParmPackExpr(
  641. const SubstNonTypeTemplateParmPackExpr *E) {
  642. Visit(E->getParameterPack());
  643. Visit(E->getArgumentPack());
  644. }
  645. void VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) {
  646. if (const VarDecl *CatchParam = Node->getCatchParamDecl())
  647. Visit(CatchParam);
  648. }
  649. void VisitCXXForRangeStmt(const CXXForRangeStmt *Node) {
  650. if (Traversal == TK_IgnoreUnlessSpelledInSource) {
  651. Visit(Node->getInit());
  652. Visit(Node->getLoopVariable());
  653. Visit(Node->getRangeInit());
  654. Visit(Node->getBody());
  655. }
  656. }
  657. void VisitCallExpr(const CallExpr *Node) {
  658. for (const auto *Child :
  659. make_filter_range(Node->children(), [this](const Stmt *Child) {
  660. if (Traversal != TK_IgnoreUnlessSpelledInSource)
  661. return false;
  662. return !isa<CXXDefaultArgExpr>(Child);
  663. })) {
  664. Visit(Child);
  665. }
  666. }
  667. void VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *Node) {
  668. if (Traversal == TK_IgnoreUnlessSpelledInSource) {
  669. Visit(Node->getLHS());
  670. Visit(Node->getRHS());
  671. } else {
  672. ConstStmtVisitor<Derived>::VisitCXXRewrittenBinaryOperator(Node);
  673. }
  674. }
  675. void VisitExpressionTemplateArgument(const TemplateArgument &TA) {
  676. Visit(TA.getAsExpr());
  677. }
  678. void VisitTypeTemplateArgument(const TemplateArgument &TA) {
  679. Visit(TA.getAsType());
  680. }
  681. void VisitPackTemplateArgument(const TemplateArgument &TA) {
  682. for (const auto &TArg : TA.pack_elements())
  683. Visit(TArg);
  684. }
  685. // Implements Visit methods for Attrs.
  686. #include "clang/AST/AttrNodeTraverse.inc"
  687. };
  688. } // namespace clang
  689. #endif // LLVM_CLANG_AST_ASTNODETRAVERSER_H
  690. #ifdef __GNUC__
  691. #pragma GCC diagnostic pop
  692. #endif