MultiplexConsumer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //===- MultiplexConsumer.cpp - AST Consumer for PCH Generation --*- 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 MultiplexConsumer class. It also declares and defines
  10. // MultiplexASTDeserializationListener and MultiplexASTMutationListener, which
  11. // are implementation details of MultiplexConsumer.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Frontend/MultiplexConsumer.h"
  15. #include "clang/AST/ASTMutationListener.h"
  16. #include "clang/AST/DeclGroup.h"
  17. using namespace clang;
  18. namespace clang {
  19. MultiplexASTDeserializationListener::MultiplexASTDeserializationListener(
  20. const std::vector<ASTDeserializationListener*>& L)
  21. : Listeners(L) {
  22. }
  23. void MultiplexASTDeserializationListener::ReaderInitialized(
  24. ASTReader *Reader) {
  25. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  26. Listeners[i]->ReaderInitialized(Reader);
  27. }
  28. void MultiplexASTDeserializationListener::IdentifierRead(
  29. serialization::IdentID ID, IdentifierInfo *II) {
  30. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  31. Listeners[i]->IdentifierRead(ID, II);
  32. }
  33. void MultiplexASTDeserializationListener::MacroRead(
  34. serialization::MacroID ID, MacroInfo *MI) {
  35. for (auto &Listener : Listeners)
  36. Listener->MacroRead(ID, MI);
  37. }
  38. void MultiplexASTDeserializationListener::TypeRead(
  39. serialization::TypeIdx Idx, QualType T) {
  40. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  41. Listeners[i]->TypeRead(Idx, T);
  42. }
  43. void MultiplexASTDeserializationListener::DeclRead(
  44. serialization::DeclID ID, const Decl *D) {
  45. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  46. Listeners[i]->DeclRead(ID, D);
  47. }
  48. void MultiplexASTDeserializationListener::SelectorRead(
  49. serialization::SelectorID ID, Selector Sel) {
  50. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  51. Listeners[i]->SelectorRead(ID, Sel);
  52. }
  53. void MultiplexASTDeserializationListener::MacroDefinitionRead(
  54. serialization::PreprocessedEntityID ID, MacroDefinitionRecord *MD) {
  55. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  56. Listeners[i]->MacroDefinitionRead(ID, MD);
  57. }
  58. void MultiplexASTDeserializationListener::ModuleRead(
  59. serialization::SubmoduleID ID, Module *Mod) {
  60. for (auto &Listener : Listeners)
  61. Listener->ModuleRead(ID, Mod);
  62. }
  63. // This ASTMutationListener forwards its notifications to a set of
  64. // child listeners.
  65. class MultiplexASTMutationListener : public ASTMutationListener {
  66. public:
  67. // Does NOT take ownership of the elements in L.
  68. MultiplexASTMutationListener(ArrayRef<ASTMutationListener*> L);
  69. void CompletedTagDefinition(const TagDecl *D) override;
  70. void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
  71. void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
  72. void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
  73. const ClassTemplateSpecializationDecl *D) override;
  74. void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD,
  75. const VarTemplateSpecializationDecl *D) override;
  76. void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
  77. const FunctionDecl *D) override;
  78. void ResolvedExceptionSpec(const FunctionDecl *FD) override;
  79. void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
  80. void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
  81. const FunctionDecl *Delete,
  82. Expr *ThisArg) override;
  83. void CompletedImplicitDefinition(const FunctionDecl *D) override;
  84. void InstantiationRequested(const ValueDecl *D) override;
  85. void VariableDefinitionInstantiated(const VarDecl *D) override;
  86. void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
  87. void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
  88. void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
  89. void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
  90. const ObjCInterfaceDecl *IFD) override;
  91. void DeclarationMarkedUsed(const Decl *D) override;
  92. void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
  93. void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
  94. void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
  95. const Attr *Attr) override;
  96. void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
  97. void AddedAttributeToRecord(const Attr *Attr,
  98. const RecordDecl *Record) override;
  99. private:
  100. std::vector<ASTMutationListener*> Listeners;
  101. };
  102. MultiplexASTMutationListener::MultiplexASTMutationListener(
  103. ArrayRef<ASTMutationListener*> L)
  104. : Listeners(L.begin(), L.end()) {
  105. }
  106. void MultiplexASTMutationListener::CompletedTagDefinition(const TagDecl *D) {
  107. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  108. Listeners[i]->CompletedTagDefinition(D);
  109. }
  110. void MultiplexASTMutationListener::AddedVisibleDecl(
  111. const DeclContext *DC, const Decl *D) {
  112. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  113. Listeners[i]->AddedVisibleDecl(DC, D);
  114. }
  115. void MultiplexASTMutationListener::AddedCXXImplicitMember(
  116. const CXXRecordDecl *RD, const Decl *D) {
  117. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  118. Listeners[i]->AddedCXXImplicitMember(RD, D);
  119. }
  120. void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
  121. const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
  122. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  123. Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
  124. }
  125. void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
  126. const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
  127. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  128. Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
  129. }
  130. void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
  131. const FunctionTemplateDecl *TD, const FunctionDecl *D) {
  132. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  133. Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
  134. }
  135. void MultiplexASTMutationListener::ResolvedExceptionSpec(
  136. const FunctionDecl *FD) {
  137. for (auto &Listener : Listeners)
  138. Listener->ResolvedExceptionSpec(FD);
  139. }
  140. void MultiplexASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
  141. QualType ReturnType) {
  142. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  143. Listeners[i]->DeducedReturnType(FD, ReturnType);
  144. }
  145. void MultiplexASTMutationListener::ResolvedOperatorDelete(
  146. const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
  147. for (auto *L : Listeners)
  148. L->ResolvedOperatorDelete(DD, Delete, ThisArg);
  149. }
  150. void MultiplexASTMutationListener::CompletedImplicitDefinition(
  151. const FunctionDecl *D) {
  152. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  153. Listeners[i]->CompletedImplicitDefinition(D);
  154. }
  155. void MultiplexASTMutationListener::InstantiationRequested(const ValueDecl *D) {
  156. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  157. Listeners[i]->InstantiationRequested(D);
  158. }
  159. void MultiplexASTMutationListener::VariableDefinitionInstantiated(
  160. const VarDecl *D) {
  161. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  162. Listeners[i]->VariableDefinitionInstantiated(D);
  163. }
  164. void MultiplexASTMutationListener::FunctionDefinitionInstantiated(
  165. const FunctionDecl *D) {
  166. for (auto &Listener : Listeners)
  167. Listener->FunctionDefinitionInstantiated(D);
  168. }
  169. void MultiplexASTMutationListener::DefaultArgumentInstantiated(
  170. const ParmVarDecl *D) {
  171. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  172. Listeners[i]->DefaultArgumentInstantiated(D);
  173. }
  174. void MultiplexASTMutationListener::DefaultMemberInitializerInstantiated(
  175. const FieldDecl *D) {
  176. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  177. Listeners[i]->DefaultMemberInitializerInstantiated(D);
  178. }
  179. void MultiplexASTMutationListener::AddedObjCCategoryToInterface(
  180. const ObjCCategoryDecl *CatD,
  181. const ObjCInterfaceDecl *IFD) {
  182. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  183. Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
  184. }
  185. void MultiplexASTMutationListener::DeclarationMarkedUsed(const Decl *D) {
  186. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  187. Listeners[i]->DeclarationMarkedUsed(D);
  188. }
  189. void MultiplexASTMutationListener::DeclarationMarkedOpenMPThreadPrivate(
  190. const Decl *D) {
  191. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  192. Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
  193. }
  194. void MultiplexASTMutationListener::DeclarationMarkedOpenMPAllocate(
  195. const Decl *D, const Attr *A) {
  196. for (ASTMutationListener *L : Listeners)
  197. L->DeclarationMarkedOpenMPAllocate(D, A);
  198. }
  199. void MultiplexASTMutationListener::DeclarationMarkedOpenMPDeclareTarget(
  200. const Decl *D, const Attr *Attr) {
  201. for (auto *L : Listeners)
  202. L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
  203. }
  204. void MultiplexASTMutationListener::RedefinedHiddenDefinition(const NamedDecl *D,
  205. Module *M) {
  206. for (auto *L : Listeners)
  207. L->RedefinedHiddenDefinition(D, M);
  208. }
  209. void MultiplexASTMutationListener::AddedAttributeToRecord(
  210. const Attr *Attr,
  211. const RecordDecl *Record) {
  212. for (auto *L : Listeners)
  213. L->AddedAttributeToRecord(Attr, Record);
  214. }
  215. } // end namespace clang
  216. MultiplexConsumer::MultiplexConsumer(
  217. std::vector<std::unique_ptr<ASTConsumer>> C)
  218. : Consumers(std::move(C)) {
  219. // Collect the mutation listeners and deserialization listeners of all
  220. // children, and create a multiplex listener each if so.
  221. std::vector<ASTMutationListener *> mutationListeners;
  222. std::vector<ASTDeserializationListener*> serializationListeners;
  223. for (auto &Consumer : Consumers) {
  224. if (auto *mutationListener = Consumer->GetASTMutationListener())
  225. mutationListeners.push_back(mutationListener);
  226. if (auto *serializationListener = Consumer->GetASTDeserializationListener())
  227. serializationListeners.push_back(serializationListener);
  228. }
  229. if (!mutationListeners.empty()) {
  230. MutationListener =
  231. std::make_unique<MultiplexASTMutationListener>(mutationListeners);
  232. }
  233. if (!serializationListeners.empty()) {
  234. DeserializationListener =
  235. std::make_unique<MultiplexASTDeserializationListener>(
  236. serializationListeners);
  237. }
  238. }
  239. MultiplexConsumer::~MultiplexConsumer() {}
  240. void MultiplexConsumer::Initialize(ASTContext &Context) {
  241. for (auto &Consumer : Consumers)
  242. Consumer->Initialize(Context);
  243. }
  244. bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
  245. bool Continue = true;
  246. for (auto &Consumer : Consumers)
  247. Continue = Continue && Consumer->HandleTopLevelDecl(D);
  248. return Continue;
  249. }
  250. void MultiplexConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
  251. for (auto &Consumer : Consumers)
  252. Consumer->HandleInlineFunctionDefinition(D);
  253. }
  254. void MultiplexConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
  255. for (auto &Consumer : Consumers)
  256. Consumer->HandleCXXStaticMemberVarInstantiation(VD);
  257. }
  258. void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) {
  259. for (auto &Consumer : Consumers)
  260. Consumer->HandleInterestingDecl(D);
  261. }
  262. void MultiplexConsumer::HandleTranslationUnit(ASTContext &Ctx) {
  263. for (auto &Consumer : Consumers)
  264. Consumer->HandleTranslationUnit(Ctx);
  265. }
  266. void MultiplexConsumer::HandleTagDeclDefinition(TagDecl *D) {
  267. for (auto &Consumer : Consumers)
  268. Consumer->HandleTagDeclDefinition(D);
  269. }
  270. void MultiplexConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {
  271. for (auto &Consumer : Consumers)
  272. Consumer->HandleTagDeclRequiredDefinition(D);
  273. }
  274. void MultiplexConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D){
  275. for (auto &Consumer : Consumers)
  276. Consumer->HandleCXXImplicitFunctionInstantiation(D);
  277. }
  278. void MultiplexConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
  279. for (auto &Consumer : Consumers)
  280. Consumer->HandleTopLevelDeclInObjCContainer(D);
  281. }
  282. void MultiplexConsumer::HandleImplicitImportDecl(ImportDecl *D) {
  283. for (auto &Consumer : Consumers)
  284. Consumer->HandleImplicitImportDecl(D);
  285. }
  286. void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) {
  287. for (auto &Consumer : Consumers)
  288. Consumer->CompleteTentativeDefinition(D);
  289. }
  290. void MultiplexConsumer::CompleteExternalDeclaration(VarDecl *D) {
  291. for (auto &Consumer : Consumers)
  292. Consumer->CompleteExternalDeclaration(D);
  293. }
  294. void MultiplexConsumer::AssignInheritanceModel(CXXRecordDecl *RD) {
  295. for (auto &Consumer : Consumers)
  296. Consumer->AssignInheritanceModel(RD);
  297. }
  298. void MultiplexConsumer::HandleVTable(CXXRecordDecl *RD) {
  299. for (auto &Consumer : Consumers)
  300. Consumer->HandleVTable(RD);
  301. }
  302. ASTMutationListener *MultiplexConsumer::GetASTMutationListener() {
  303. return MutationListener.get();
  304. }
  305. ASTDeserializationListener *MultiplexConsumer::GetASTDeserializationListener() {
  306. return DeserializationListener.get();
  307. }
  308. void MultiplexConsumer::PrintStats() {
  309. for (auto &Consumer : Consumers)
  310. Consumer->PrintStats();
  311. }
  312. bool MultiplexConsumer::shouldSkipFunctionBody(Decl *D) {
  313. bool Skip = true;
  314. for (auto &Consumer : Consumers)
  315. Skip = Skip && Consumer->shouldSkipFunctionBody(D);
  316. return Skip;
  317. }
  318. void MultiplexConsumer::InitializeSema(Sema &S) {
  319. for (auto &Consumer : Consumers)
  320. if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
  321. SC->InitializeSema(S);
  322. }
  323. void MultiplexConsumer::ForgetSema() {
  324. for (auto &Consumer : Consumers)
  325. if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
  326. SC->ForgetSema();
  327. }