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