MultiplexConsumer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. void MultiplexASTDeserializationListener::ModuleImportRead(
  64. serialization::SubmoduleID ID, SourceLocation ImportLoc) {
  65. for (auto &Listener : Listeners)
  66. Listener->ModuleImportRead(ID, ImportLoc);
  67. }
  68. // This ASTMutationListener forwards its notifications to a set of
  69. // child listeners.
  70. class MultiplexASTMutationListener : public ASTMutationListener {
  71. public:
  72. // Does NOT take ownership of the elements in L.
  73. MultiplexASTMutationListener(ArrayRef<ASTMutationListener*> L);
  74. void CompletedTagDefinition(const TagDecl *D) override;
  75. void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
  76. void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
  77. void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
  78. const ClassTemplateSpecializationDecl *D) override;
  79. void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD,
  80. const VarTemplateSpecializationDecl *D) override;
  81. void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
  82. const FunctionDecl *D) override;
  83. void ResolvedExceptionSpec(const FunctionDecl *FD) override;
  84. void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
  85. void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
  86. const FunctionDecl *Delete,
  87. Expr *ThisArg) override;
  88. void CompletedImplicitDefinition(const FunctionDecl *D) override;
  89. void InstantiationRequested(const ValueDecl *D) override;
  90. void VariableDefinitionInstantiated(const VarDecl *D) override;
  91. void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
  92. void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
  93. void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
  94. void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
  95. const ObjCInterfaceDecl *IFD) override;
  96. void DeclarationMarkedUsed(const Decl *D) override;
  97. void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
  98. void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
  99. void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
  100. const Attr *Attr) override;
  101. void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
  102. void AddedAttributeToRecord(const Attr *Attr,
  103. const RecordDecl *Record) override;
  104. private:
  105. std::vector<ASTMutationListener*> Listeners;
  106. };
  107. MultiplexASTMutationListener::MultiplexASTMutationListener(
  108. ArrayRef<ASTMutationListener*> L)
  109. : Listeners(L.begin(), L.end()) {
  110. }
  111. void MultiplexASTMutationListener::CompletedTagDefinition(const TagDecl *D) {
  112. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  113. Listeners[i]->CompletedTagDefinition(D);
  114. }
  115. void MultiplexASTMutationListener::AddedVisibleDecl(
  116. const DeclContext *DC, const Decl *D) {
  117. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  118. Listeners[i]->AddedVisibleDecl(DC, D);
  119. }
  120. void MultiplexASTMutationListener::AddedCXXImplicitMember(
  121. const CXXRecordDecl *RD, const Decl *D) {
  122. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  123. Listeners[i]->AddedCXXImplicitMember(RD, D);
  124. }
  125. void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
  126. const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *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 VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
  132. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  133. Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
  134. }
  135. void MultiplexASTMutationListener::AddedCXXTemplateSpecialization(
  136. const FunctionTemplateDecl *TD, const FunctionDecl *D) {
  137. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  138. Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
  139. }
  140. void MultiplexASTMutationListener::ResolvedExceptionSpec(
  141. const FunctionDecl *FD) {
  142. for (auto &Listener : Listeners)
  143. Listener->ResolvedExceptionSpec(FD);
  144. }
  145. void MultiplexASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
  146. QualType ReturnType) {
  147. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  148. Listeners[i]->DeducedReturnType(FD, ReturnType);
  149. }
  150. void MultiplexASTMutationListener::ResolvedOperatorDelete(
  151. const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
  152. for (auto *L : Listeners)
  153. L->ResolvedOperatorDelete(DD, Delete, ThisArg);
  154. }
  155. void MultiplexASTMutationListener::CompletedImplicitDefinition(
  156. const FunctionDecl *D) {
  157. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  158. Listeners[i]->CompletedImplicitDefinition(D);
  159. }
  160. void MultiplexASTMutationListener::InstantiationRequested(const ValueDecl *D) {
  161. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  162. Listeners[i]->InstantiationRequested(D);
  163. }
  164. void MultiplexASTMutationListener::VariableDefinitionInstantiated(
  165. const VarDecl *D) {
  166. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  167. Listeners[i]->VariableDefinitionInstantiated(D);
  168. }
  169. void MultiplexASTMutationListener::FunctionDefinitionInstantiated(
  170. const FunctionDecl *D) {
  171. for (auto &Listener : Listeners)
  172. Listener->FunctionDefinitionInstantiated(D);
  173. }
  174. void MultiplexASTMutationListener::DefaultArgumentInstantiated(
  175. const ParmVarDecl *D) {
  176. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  177. Listeners[i]->DefaultArgumentInstantiated(D);
  178. }
  179. void MultiplexASTMutationListener::DefaultMemberInitializerInstantiated(
  180. const FieldDecl *D) {
  181. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  182. Listeners[i]->DefaultMemberInitializerInstantiated(D);
  183. }
  184. void MultiplexASTMutationListener::AddedObjCCategoryToInterface(
  185. const ObjCCategoryDecl *CatD,
  186. const ObjCInterfaceDecl *IFD) {
  187. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  188. Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
  189. }
  190. void MultiplexASTMutationListener::DeclarationMarkedUsed(const Decl *D) {
  191. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  192. Listeners[i]->DeclarationMarkedUsed(D);
  193. }
  194. void MultiplexASTMutationListener::DeclarationMarkedOpenMPThreadPrivate(
  195. const Decl *D) {
  196. for (size_t i = 0, e = Listeners.size(); i != e; ++i)
  197. Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
  198. }
  199. void MultiplexASTMutationListener::DeclarationMarkedOpenMPAllocate(
  200. const Decl *D, const Attr *A) {
  201. for (ASTMutationListener *L : Listeners)
  202. L->DeclarationMarkedOpenMPAllocate(D, A);
  203. }
  204. void MultiplexASTMutationListener::DeclarationMarkedOpenMPDeclareTarget(
  205. const Decl *D, const Attr *Attr) {
  206. for (auto *L : Listeners)
  207. L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
  208. }
  209. void MultiplexASTMutationListener::RedefinedHiddenDefinition(const NamedDecl *D,
  210. Module *M) {
  211. for (auto *L : Listeners)
  212. L->RedefinedHiddenDefinition(D, M);
  213. }
  214. void MultiplexASTMutationListener::AddedAttributeToRecord(
  215. const Attr *Attr,
  216. const RecordDecl *Record) {
  217. for (auto *L : Listeners)
  218. L->AddedAttributeToRecord(Attr, Record);
  219. }
  220. } // end namespace clang
  221. MultiplexConsumer::MultiplexConsumer(
  222. std::vector<std::unique_ptr<ASTConsumer>> C)
  223. : Consumers(std::move(C)) {
  224. // Collect the mutation listeners and deserialization listeners of all
  225. // children, and create a multiplex listener each if so.
  226. std::vector<ASTMutationListener *> mutationListeners;
  227. std::vector<ASTDeserializationListener*> serializationListeners;
  228. for (auto &Consumer : Consumers) {
  229. if (auto *mutationListener = Consumer->GetASTMutationListener())
  230. mutationListeners.push_back(mutationListener);
  231. if (auto *serializationListener = Consumer->GetASTDeserializationListener())
  232. serializationListeners.push_back(serializationListener);
  233. }
  234. if (!mutationListeners.empty()) {
  235. MutationListener =
  236. std::make_unique<MultiplexASTMutationListener>(mutationListeners);
  237. }
  238. if (!serializationListeners.empty()) {
  239. DeserializationListener =
  240. std::make_unique<MultiplexASTDeserializationListener>(
  241. serializationListeners);
  242. }
  243. }
  244. MultiplexConsumer::~MultiplexConsumer() {}
  245. void MultiplexConsumer::Initialize(ASTContext &Context) {
  246. for (auto &Consumer : Consumers)
  247. Consumer->Initialize(Context);
  248. }
  249. bool MultiplexConsumer::HandleTopLevelDecl(DeclGroupRef D) {
  250. bool Continue = true;
  251. for (auto &Consumer : Consumers)
  252. Continue = Continue && Consumer->HandleTopLevelDecl(D);
  253. return Continue;
  254. }
  255. void MultiplexConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
  256. for (auto &Consumer : Consumers)
  257. Consumer->HandleInlineFunctionDefinition(D);
  258. }
  259. void MultiplexConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
  260. for (auto &Consumer : Consumers)
  261. Consumer->HandleCXXStaticMemberVarInstantiation(VD);
  262. }
  263. void MultiplexConsumer::HandleInterestingDecl(DeclGroupRef D) {
  264. for (auto &Consumer : Consumers)
  265. Consumer->HandleInterestingDecl(D);
  266. }
  267. void MultiplexConsumer::HandleTranslationUnit(ASTContext &Ctx) {
  268. for (auto &Consumer : Consumers)
  269. Consumer->HandleTranslationUnit(Ctx);
  270. }
  271. void MultiplexConsumer::HandleTagDeclDefinition(TagDecl *D) {
  272. for (auto &Consumer : Consumers)
  273. Consumer->HandleTagDeclDefinition(D);
  274. }
  275. void MultiplexConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {
  276. for (auto &Consumer : Consumers)
  277. Consumer->HandleTagDeclRequiredDefinition(D);
  278. }
  279. void MultiplexConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D){
  280. for (auto &Consumer : Consumers)
  281. Consumer->HandleCXXImplicitFunctionInstantiation(D);
  282. }
  283. void MultiplexConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {
  284. for (auto &Consumer : Consumers)
  285. Consumer->HandleTopLevelDeclInObjCContainer(D);
  286. }
  287. void MultiplexConsumer::HandleImplicitImportDecl(ImportDecl *D) {
  288. for (auto &Consumer : Consumers)
  289. Consumer->HandleImplicitImportDecl(D);
  290. }
  291. void MultiplexConsumer::CompleteTentativeDefinition(VarDecl *D) {
  292. for (auto &Consumer : Consumers)
  293. Consumer->CompleteTentativeDefinition(D);
  294. }
  295. void MultiplexConsumer::CompleteExternalDeclaration(VarDecl *D) {
  296. for (auto &Consumer : Consumers)
  297. Consumer->CompleteExternalDeclaration(D);
  298. }
  299. void MultiplexConsumer::AssignInheritanceModel(CXXRecordDecl *RD) {
  300. for (auto &Consumer : Consumers)
  301. Consumer->AssignInheritanceModel(RD);
  302. }
  303. void MultiplexConsumer::HandleVTable(CXXRecordDecl *RD) {
  304. for (auto &Consumer : Consumers)
  305. Consumer->HandleVTable(RD);
  306. }
  307. ASTMutationListener *MultiplexConsumer::GetASTMutationListener() {
  308. return MutationListener.get();
  309. }
  310. ASTDeserializationListener *MultiplexConsumer::GetASTDeserializationListener() {
  311. return DeserializationListener.get();
  312. }
  313. void MultiplexConsumer::PrintStats() {
  314. for (auto &Consumer : Consumers)
  315. Consumer->PrintStats();
  316. }
  317. bool MultiplexConsumer::shouldSkipFunctionBody(Decl *D) {
  318. bool Skip = true;
  319. for (auto &Consumer : Consumers)
  320. Skip = Skip && Consumer->shouldSkipFunctionBody(D);
  321. return Skip;
  322. }
  323. void MultiplexConsumer::InitializeSema(Sema &S) {
  324. for (auto &Consumer : Consumers)
  325. if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
  326. SC->InitializeSema(S);
  327. }
  328. void MultiplexConsumer::ForgetSema() {
  329. for (auto &Consumer : Consumers)
  330. if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
  331. SC->ForgetSema();
  332. }