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 DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
107  void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
108  const Attr *Attr) override;
109  void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
110  void AddedAttributeToRecord(const Attr *Attr,
111  const RecordDecl *Record) override;
112 
113 private:
114  std::vector<ASTMutationListener*> Listeners;
115 };
116 
119  : Listeners(L.begin(), L.end()) {
120 }
121 
123  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
124  Listeners[i]->CompletedTagDefinition(D);
125 }
126 
128  const DeclContext *DC, const Decl *D) {
129  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
130  Listeners[i]->AddedVisibleDecl(DC, D);
131 }
132 
134  const CXXRecordDecl *RD, const Decl *D) {
135  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
136  Listeners[i]->AddedCXXImplicitMember(RD, D);
137 }
140  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
141  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
142 }
144  const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
145  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
146  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
147 }
149  const FunctionTemplateDecl *TD, const FunctionDecl *D) {
150  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
151  Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
152 }
154  const FunctionDecl *FD) {
155  for (auto &Listener : Listeners)
156  Listener->ResolvedExceptionSpec(FD);
157 }
159  QualType ReturnType) {
160  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
161  Listeners[i]->DeducedReturnType(FD, ReturnType);
162 }
164  const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
165  for (auto *L : Listeners)
166  L->ResolvedOperatorDelete(DD, Delete, ThisArg);
167 }
169  const FunctionDecl *D) {
170  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
171  Listeners[i]->CompletedImplicitDefinition(D);
172 }
174  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
175  Listeners[i]->InstantiationRequested(D);
176 }
178  const VarDecl *D) {
179  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
180  Listeners[i]->VariableDefinitionInstantiated(D);
181 }
183  const FunctionDecl *D) {
184  for (auto &Listener : Listeners)
185  Listener->FunctionDefinitionInstantiated(D);
186 }
188  const ParmVarDecl *D) {
189  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
190  Listeners[i]->DefaultArgumentInstantiated(D);
191 }
193  const FieldDecl *D) {
194  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
195  Listeners[i]->DefaultMemberInitializerInstantiated(D);
196 }
198  const ObjCCategoryDecl *CatD,
199  const ObjCInterfaceDecl *IFD) {
200  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
201  Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
202 }
204  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
205  Listeners[i]->DeclarationMarkedUsed(D);
206 }
208  const Decl *D) {
209  for (size_t i = 0, e = Listeners.size(); i != e; ++i)
210  Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
211 }
213  const Decl *D, const Attr *A) {
214  for (ASTMutationListener *L : Listeners)
215  L->DeclarationMarkedOpenMPAllocate(D, A);
216 }
218  const Decl *D, const Attr *Attr) {
219  for (auto *L : Listeners)
220  L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
221 }
223  Module *M) {
224  for (auto *L : Listeners)
225  L->RedefinedHiddenDefinition(D, M);
226 }
227 
229  const Attr *Attr,
230  const RecordDecl *Record) {
231  for (auto *L : Listeners)
232  L->AddedAttributeToRecord(Attr, Record);
233 }
234 
235 } // end namespace clang
236 
238  std::vector<std::unique_ptr<ASTConsumer>> C)
239  : Consumers(std::move(C)), MutationListener(), DeserializationListener() {
240  // Collect the mutation listeners and deserialization listeners of all
241  // children, and create a multiplex listener each if so.
242  std::vector<ASTMutationListener*> mutationListeners;
243  std::vector<ASTDeserializationListener*> serializationListeners;
244  for (auto &Consumer : Consumers) {
245  if (auto *mutationListener = Consumer->GetASTMutationListener())
246  mutationListeners.push_back(mutationListener);
247  if (auto *serializationListener = Consumer->GetASTDeserializationListener())
248  serializationListeners.push_back(serializationListener);
249  }
250  if (!mutationListeners.empty()) {
251  MutationListener =
252  llvm::make_unique<MultiplexASTMutationListener>(mutationListeners);
253  }
254  if (!serializationListeners.empty()) {
255  DeserializationListener =
256  llvm::make_unique<MultiplexASTDeserializationListener>(
257  serializationListeners);
258  }
259 }
260 
262 
264  for (auto &Consumer : Consumers)
265  Consumer->Initialize(Context);
266 }
267 
269  bool Continue = true;
270  for (auto &Consumer : Consumers)
271  Continue = Continue && Consumer->HandleTopLevelDecl(D);
272  return Continue;
273 }
274 
276  for (auto &Consumer : Consumers)
277  Consumer->HandleInlineFunctionDefinition(D);
278 }
279 
281  for (auto &Consumer : Consumers)
282  Consumer->HandleCXXStaticMemberVarInstantiation(VD);
283 }
284 
286  for (auto &Consumer : Consumers)
287  Consumer->HandleInterestingDecl(D);
288 }
289 
291  for (auto &Consumer : Consumers)
292  Consumer->HandleTranslationUnit(Ctx);
293 }
294 
296  for (auto &Consumer : Consumers)
297  Consumer->HandleTagDeclDefinition(D);
298 }
299 
301  for (auto &Consumer : Consumers)
302  Consumer->HandleTagDeclRequiredDefinition(D);
303 }
304 
306  for (auto &Consumer : Consumers)
307  Consumer->HandleCXXImplicitFunctionInstantiation(D);
308 }
309 
311  for (auto &Consumer : Consumers)
312  Consumer->HandleTopLevelDeclInObjCContainer(D);
313 }
314 
316  for (auto &Consumer : Consumers)
317  Consumer->HandleImplicitImportDecl(D);
318 }
319 
321  for (auto &Consumer : Consumers)
322  Consumer->CompleteTentativeDefinition(D);
323 }
324 
326  for (auto &Consumer : Consumers)
327  Consumer->AssignInheritanceModel(RD);
328 }
329 
331  for (auto &Consumer : Consumers)
332  Consumer->HandleVTable(RD);
333 }
334 
336  return MutationListener.get();
337 }
338 
340  return DeserializationListener.get();
341 }
342 
344  for (auto &Consumer : Consumers)
345  Consumer->PrintStats();
346 }
347 
349  bool Skip = true;
350  for (auto &Consumer : Consumers)
351  Skip = Skip && Consumer->shouldSkipFunctionBody(D);
352  return Skip;
353 }
354 
356  for (auto &Consumer : Consumers)
357  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
358  SC->InitializeSema(S);
359 }
360 
362  for (auto &Consumer : Consumers)
363  if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
364  SC->ForgetSema();
365 }
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:639
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:87
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:2222
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:328
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:108
void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override
A declaration is marked as a variable with OpenMP allocator.
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:2713
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:4155
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:1266
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.