clang  9.0.0svn
MultiplexConsumer.cpp
Go to the documentation of this file.
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 
17 #include "clang/AST/DeclGroup.h"
18 
19 using namespace clang;
20 
21 namespace clang {
22 
24  const std::vector<ASTDeserializationListener*>& L)
25  : Listeners(L) {
26 }
27 
29  ASTReader *Reader) {
30  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
31  Listeners[i]->ReaderInitialized(Reader);
32 }
33 
36  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
37  Listeners[i]->IdentifierRead(ID, II);
38 }
39 
42  for (auto &Listener : Listeners)
43  Listener->MacroRead(ID, MI);
44 }
45 
48  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
49  Listeners[i]->TypeRead(Idx, T);
50 }
51 
53  serialization::DeclID ID, const Decl *D) {
54  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
55  Listeners[i]->DeclRead(ID, D);
56 }
57 
60  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
61  Listeners[i]->SelectorRead(ID, Sel);
62 }
63 
66  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
67  Listeners[i]->MacroDefinitionRead(ID, MD);
68 }
69 
72  for (auto &Listener : Listeners)
73  Listener->ModuleRead(ID, Mod);
74 }
75 
76 // This ASTMutationListener forwards its notifications to a set of
77 // child listeners.
79 public:
80  // Does NOT take ownership of the elements in L.
82  void CompletedTagDefinition(const TagDecl *D) override;
83  void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
84  void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
85  void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
86  const ClassTemplateSpecializationDecl *D) override;
87  void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD,
88  const VarTemplateSpecializationDecl *D) override;
89  void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
90  const FunctionDecl *D) override;
91  void ResolvedExceptionSpec(const FunctionDecl *FD) override;
92  void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
93  void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
94  const FunctionDecl *Delete,
95  Expr *ThisArg) override;
96  void CompletedImplicitDefinition(const FunctionDecl *D) override;
97  void InstantiationRequested(const ValueDecl *D) override;
98  void VariableDefinitionInstantiated(const VarDecl *D) override;
99  void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
100  void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
101  void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
102  void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
103  const ObjCInterfaceDecl *IFD) override;
104  void DeclarationMarkedUsed(const Decl *D) override;
105  void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
106  void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
107  const Attr *Attr) override;
108  void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
109  void AddedAttributeToRecord(const Attr *Attr,
110  const RecordDecl *Record) override;
111 
112 private:
113  std::vector<ASTMutationListener*> Listeners;
114 };
115 
118  : Listeners(L.begin(), L.end()) {
119 }
120 
122  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
123  Listeners[i]->CompletedTagDefinition(D);
124 }
125 
127  const DeclContext *DC, const Decl *D) {
128  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
129  Listeners[i]->AddedVisibleDecl(DC, D);
130 }
131 
133  const CXXRecordDecl *RD, const Decl *D) {
134  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
135  Listeners[i]->AddedCXXImplicitMember(RD, D);
136 }
139  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
140  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
141 }
143  const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
144  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
145  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
146 }
148  const FunctionTemplateDecl *TD, const FunctionDecl *D) {
149  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
150  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
151 }
153  const FunctionDecl *FD) {
154  for (auto &Listener : Listeners)
155  Listener->ResolvedExceptionSpec(FD);
156 }
158  QualType ReturnType) {
159  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
160  Listeners[i]->DeducedReturnType(FD, ReturnType);
161 }
163  const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
164  for (auto *L : Listeners)
165  L->ResolvedOperatorDelete(DD, Delete, ThisArg);
166 }
168  const FunctionDecl *D) {
169  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
170  Listeners[i]->CompletedImplicitDefinition(D);
171 }
173  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
174  Listeners[i]->InstantiationRequested(D);
175 }
177  const VarDecl *D) {
178  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
179  Listeners[i]->VariableDefinitionInstantiated(D);
180 }
182  const FunctionDecl *D) {
183  for (auto &Listener : Listeners)
184  Listener->FunctionDefinitionInstantiated(D);
185 }
187  const ParmVarDecl *D) {
188  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
189  Listeners[i]->DefaultArgumentInstantiated(D);
190 }
192  const FieldDecl *D) {
193  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
194  Listeners[i]->DefaultMemberInitializerInstantiated(D);
195 }
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 }
203  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
204  Listeners[i]->DeclarationMarkedUsed(D);
205 }
207  const Decl *D) {
208  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
209  Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
210 }
212  const Decl *D, const Attr *Attr) {
213  for (auto *L : Listeners)
214  L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
215 }
217  Module *M) {
218  for (auto *L : Listeners)
219  L->RedefinedHiddenDefinition(D, M);
220 }
221 
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 
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 
256 
258  for (auto &Consumer : Consumers)
259  Consumer->Initialize(Context);
260 }
261 
263  bool Continue = true;
264  for (auto &Consumer : Consumers)
265  Continue = Continue && Consumer->HandleTopLevelDecl(D);
266  return Continue;
267 }
268 
270  for (auto &Consumer : Consumers)
271  Consumer->HandleInlineFunctionDefinition(D);
272 }
273 
275  for (auto &Consumer : Consumers)
276  Consumer->HandleCXXStaticMemberVarInstantiation(VD);
277 }
278 
280  for (auto &Consumer : Consumers)
281  Consumer->HandleInterestingDecl(D);
282 }
283 
285  for (auto &Consumer : Consumers)
286  Consumer->HandleTranslationUnit(Ctx);
287 }
288 
290  for (auto &Consumer : Consumers)
291  Consumer->HandleTagDeclDefinition(D);
292 }
293 
295  for (auto &Consumer : Consumers)
296  Consumer->HandleTagDeclRequiredDefinition(D);
297 }
298 
300  for (auto &Consumer : Consumers)
301  Consumer->HandleCXXImplicitFunctionInstantiation(D);
302 }
303 
305  for (auto &Consumer : Consumers)
306  Consumer->HandleTopLevelDeclInObjCContainer(D);
307 }
308 
310  for (auto &Consumer : Consumers)
311  Consumer->HandleImplicitImportDecl(D);
312 }
313 
315  for (auto &Consumer : Consumers)
316  Consumer->CompleteTentativeDefinition(D);
317 }
318 
320  for (auto &Consumer : Consumers)
321  Consumer->AssignInheritanceModel(RD);
322 }
323 
325  for (auto &Consumer : Consumers)
326  Consumer->HandleVTable(RD);
327 }
328 
330  return MutationListener.get();
331 }
332 
334  return DeserializationListener.get();
335 }
336 
338  for (auto &Consumer : Consumers)
339  Consumer->PrintStats();
340 }
341 
343  bool Skip = true;
344  for (auto &Consumer : Consumers)
345  Skip = Skip && Consumer->shouldSkipFunctionBody(D);
346  return Skip;
347 }
348 
350  for (auto &Consumer : Consumers)
351  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
352  SC->InitializeSema(S);
353 }
354 
356  for (auto &Consumer : Consumers)
357  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
358  SC->ForgetSema();
359 }
MultiplexASTMutationListener(ArrayRef< ASTMutationListener *> L)
void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override
An implicit member was added after the definition was completed.
Represents a function declaration or definition.
Definition: Decl.h:1737
void DeclarationMarkedUsed(const Decl *D) override
A declaration is marked used which was not previously marked used.
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation, it should return a pointer to an ASTMutationListener here.
void CompletedImplicitDefinition(const FunctionDecl *D) override
An implicit member got a definition.
Smart pointer class that efficiently represents Objective-C method names.
void SelectorRead(serialization::SelectorID iD, Selector Sel) override
A selector was read from the AST file.
void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override
Handle the specified top-level declaration that occurred inside and ObjC container.
A (possibly-)qualified type.
Definition: Type.h:634
void TypeRead(serialization::TypeIdx Idx, QualType T) override
A type was deserialized from the AST file.
void ResolvedExceptionSpec(const FunctionDecl *FD) override
A function&#39;s exception specification has been evaluated or instantiated.
void VariableDefinitionInstantiated(const VarDecl *D) override
A templated variable&#39;s definition was implicitly instantiated.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
Declaration of a variable template.
void DeclRead(serialization::DeclID ID, const Decl *D) override
A decl was deserialized from the AST file.
Represents a variable declaration or definition.
Definition: Decl.h:812
Represents a variable template specialization, which refers to a variable template with a given set o...
void MacroDefinitionRead(serialization::PreprocessedEntityID, MacroDefinitionRecord *MD) override
A macro definition was read from the AST file.
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
Represents a parameter to a function.
Definition: Decl.h:1549
Represents a struct/union/class.
Definition: Decl.h:3592
Represents a class template specialization, which refers to a class template with a given set of temp...
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Record the location of a macro definition.
Represents a member of a struct/union/class.
Definition: Decl.h:2578
void ResolvedOperatorDelete(const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) override
A virtual destructor&#39;s operator delete has been resolved.
Definition: Format.h:2071
void PrintStats() override
PrintStats - If desired, print any statistics.
uint32_t MacroID
An ID number that refers to a macro in an AST file.
Definition: ASTBitCodes.h:140
void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override
A declaration is marked as OpenMP threadprivate which was not previously marked as threadprivate...
Describes a module or submodule.
Definition: Module.h:64
An abstract interface that should be implemented by clients that read ASTs and then require further s...
Definition: SemaConsumer.h:25
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
MultiplexConsumer(std::vector< std::unique_ptr< ASTConsumer >> C)
void AddedAttributeToRecord(const Attr *Attr, const RecordDecl *Record) override
An attribute was added to a RecordDecl.
void DefaultArgumentInstantiated(const ParmVarDecl *D) override
A default argument was instantiated.
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
Represents an ObjC class declaration.
Definition: DeclObjC.h:1171
void ReaderInitialized(ASTReader *Reader) override
The ASTReader was initialized.
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:277
void Initialize(ASTContext &Context) override
Initialize - This is called to initialize the consumer, providing the ASTContext. ...
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:636
This represents one expression.
Definition: Expr.h:106
void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) override
A template specialization (or partial one) was added to the template declaration. ...
void FunctionDefinitionInstantiated(const FunctionDecl *D) override
A function template&#39;s definition was instantiated.
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2705
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
void DefaultMemberInitializerInstantiated(const FieldDecl *D) override
A default member initializer was instantiated.
void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override
A function&#39;s return type has been deduced.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
void HandleInterestingDecl(DeclGroupRef D) override
HandleInterestingDecl - Handle the specified interesting declaration.
void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override
An identifier was deserialized from the AST file.
void DeclarationMarkedOpenMPDeclareTarget(const Decl *D, const Attr *Attr) override
A declaration is marked as OpenMP declaretarget which was not previously marked as declaretarget...
void CompletedTagDefinition(const TagDecl *D) override
A new TagDecl definition was completed.
void AssignInheritanceModel(CXXRecordDecl *RD) override
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
Definition: ASTBitCodes.h:171
void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override
Invoked when a function is implicitly instantiated.
void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override
A module definition was read from the AST file.
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3063
ObjCCategoryDecl - Represents a category declaration.
Definition: DeclObjC.h:2279
void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override
A new declaration with name has been added to a DeclContext.
void HandleImplicitImportDecl(ImportDecl *D) override
Handle an ImportDecl that was implicitly created due to an inclusion directive.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
Definition: ASTBitCodes.h:168
void ForgetSema() override
Inform the semantic consumer that Sema is no longer available.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition: Decl.h:4148
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Definition: ASTBitCodes.h:153
Dataflow Directional Tag Classes.
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1261
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:354
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:68
void InstantiationRequested(const ValueDecl *D) override
The instantiation of a templated function or variable was requested.
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
Definition: ASTBitCodes.h:134
MultiplexASTDeserializationListener(const std::vector< ASTDeserializationListener *> &L)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override
A macro was read from the AST file.
Declaration of a class template.
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, const ObjCInterfaceDecl *IFD) override
A new objc category class was added for an interface.
This represents a decl that may have a name.
Definition: Decl.h:248
bool shouldSkipFunctionBody(Decl *D) override
This callback is called for each function if the Parser was initialized with SkipFunctionBodies set t...
Declaration of a template function.
Definition: DeclTemplate.h:968
A type index; the type ID with the qualifier bits removed.
Definition: ASTBitCodes.h:88
Attr - This represents one attribute.
Definition: Attr.h:43
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override
A definition has been made visible by being redefined locally.