clang 22.0.0git
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
19using namespace clang;
20
21namespace clang {
22
23class NamespaceDecl;
25
27 const std::vector<ASTDeserializationListener*>& L)
28 : Listeners(L) {
29}
30
32 ASTReader *Reader) {
33 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
34 Listeners[i]->ReaderInitialized(Reader);
35}
36
39 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
40 Listeners[i]->IdentifierRead(ID, II);
41}
42
45 for (auto &Listener : Listeners)
46 Listener->MacroRead(ID, MI);
47}
48
51 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
52 Listeners[i]->TypeRead(Idx, T);
53}
54
56 const Decl *D) {
57 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
58 Listeners[i]->DeclRead(ID, D);
59}
60
62 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
63 Listeners[i]->PredefinedDeclBuilt(ID, D);
64}
65
68 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
69 Listeners[i]->SelectorRead(ID, Sel);
70}
71
74 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
75 Listeners[i]->MacroDefinitionRead(ID, MD);
76}
77
80 for (auto &Listener : Listeners)
81 Listener->ModuleRead(ID, Mod);
82}
83
86 for (auto &Listener : Listeners)
87 Listener->ModuleImportRead(ID, ImportLoc);
88}
89
90// This ASTMutationListener forwards its notifications to a set of
91// child listeners.
93public:
94 // Does NOT take ownership of the elements in L.
96 void CompletedTagDefinition(const TagDecl *D) override;
97 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
98 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
100 const ClassTemplateSpecializationDecl *D) override;
102 const VarTemplateSpecializationDecl *D) override;
104 const FunctionDecl *D) override;
105 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
106 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
108 const FunctionDecl *Delete,
109 Expr *ThisArg) override;
111 const FunctionDecl *GlobDelete) override;
112 void CompletedImplicitDefinition(const FunctionDecl *D) override;
113 void InstantiationRequested(const ValueDecl *D) override;
114 void VariableDefinitionInstantiated(const VarDecl *D) override;
115 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
116 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
117 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
119 const ObjCInterfaceDecl *IFD) override;
120 void DeclarationMarkedUsed(const Decl *D) override;
121 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
122 void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
124 const Attr *Attr) override;
125 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
126 void AddedAttributeToRecord(const Attr *Attr,
127 const RecordDecl *Record) override;
128 void EnteringModulePurview() override;
129 void AddedManglingNumber(const Decl *D, unsigned) override;
130 void AddedStaticLocalNumbers(const Decl *D, unsigned) override;
132 NamespaceDecl *AnonNamespace) override;
133
134private:
135 std::vector<ASTMutationListener*> Listeners;
136};
137
142
144 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
145 Listeners[i]->CompletedTagDefinition(D);
146}
147
149 const DeclContext *DC, const Decl *D) {
150 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
151 Listeners[i]->AddedVisibleDecl(DC, D);
152}
153
155 const CXXRecordDecl *RD, const Decl *D) {
156 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
157 Listeners[i]->AddedCXXImplicitMember(RD, D);
158}
161 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
162 Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
163}
166 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
167 Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
168}
170 const FunctionTemplateDecl *TD, const FunctionDecl *D) {
171 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
172 Listeners[i]->AddedCXXTemplateSpecialization(TD, D);
173}
175 const FunctionDecl *FD) {
176 for (auto &Listener : Listeners)
177 Listener->ResolvedExceptionSpec(FD);
178}
180 QualType ReturnType) {
181 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
182 Listeners[i]->DeducedReturnType(FD, ReturnType);
183}
185 const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) {
186 for (auto *L : Listeners)
187 L->ResolvedOperatorDelete(DD, Delete, ThisArg);
188}
190 const CXXDestructorDecl *DD, const FunctionDecl *GlobDelete) {
191 for (auto *L : Listeners)
192 L->ResolvedOperatorGlobDelete(DD, GlobDelete);
193}
195 const FunctionDecl *D) {
196 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
197 Listeners[i]->CompletedImplicitDefinition(D);
198}
200 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
201 Listeners[i]->InstantiationRequested(D);
202}
204 const VarDecl *D) {
205 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
206 Listeners[i]->VariableDefinitionInstantiated(D);
207}
209 const FunctionDecl *D) {
210 for (auto &Listener : Listeners)
211 Listener->FunctionDefinitionInstantiated(D);
212}
214 const ParmVarDecl *D) {
215 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
216 Listeners[i]->DefaultArgumentInstantiated(D);
217}
219 const FieldDecl *D) {
220 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
221 Listeners[i]->DefaultMemberInitializerInstantiated(D);
222}
224 const ObjCCategoryDecl *CatD,
225 const ObjCInterfaceDecl *IFD) {
226 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
227 Listeners[i]->AddedObjCCategoryToInterface(CatD, IFD);
228}
230 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
231 Listeners[i]->DeclarationMarkedUsed(D);
232}
234 const Decl *D) {
235 for (size_t i = 0, e = Listeners.size(); i != e; ++i)
236 Listeners[i]->DeclarationMarkedOpenMPThreadPrivate(D);
237}
239 const Decl *D, const Attr *A) {
240 for (ASTMutationListener *L : Listeners)
241 L->DeclarationMarkedOpenMPAllocate(D, A);
242}
244 const Decl *D, const Attr *Attr) {
245 for (auto *L : Listeners)
246 L->DeclarationMarkedOpenMPDeclareTarget(D, Attr);
247}
249 Module *M) {
250 for (auto *L : Listeners)
251 L->RedefinedHiddenDefinition(D, M);
252}
253
255 const Attr *Attr,
256 const RecordDecl *Record) {
257 for (auto *L : Listeners)
258 L->AddedAttributeToRecord(Attr, Record);
259}
260
262 for (auto *L : Listeners)
263 L->EnteringModulePurview();
264}
265
267 unsigned Number) {
268 for (auto *L : Listeners)
269 L->AddedManglingNumber(D, Number);
270}
272 unsigned Number) {
273 for (auto *L : Listeners)
274 L->AddedStaticLocalNumbers(D, Number);
275}
277 const TranslationUnitDecl *TU, NamespaceDecl *AnonNamespace) {
278 for (auto *L : Listeners)
279 L->AddedAnonymousNamespace(TU, AnonNamespace);
280}
281
282} // end namespace clang
283
285 std::vector<std::unique_ptr<ASTConsumer>> C)
286 : Consumers(std::move(C)) {
287 // Collect the mutation listeners and deserialization listeners of all
288 // children, and create a multiplex listener each if so.
289 std::vector<ASTMutationListener *> mutationListeners;
290 std::vector<ASTDeserializationListener*> serializationListeners;
291 for (auto &Consumer : Consumers) {
292 if (auto *mutationListener = Consumer->GetASTMutationListener())
293 mutationListeners.push_back(mutationListener);
294 if (auto *serializationListener = Consumer->GetASTDeserializationListener())
295 serializationListeners.push_back(serializationListener);
296 }
297 if (!mutationListeners.empty()) {
298 MutationListener =
299 std::make_unique<MultiplexASTMutationListener>(mutationListeners);
300 }
301 if (!serializationListeners.empty()) {
302 DeserializationListener =
303 std::make_unique<MultiplexASTDeserializationListener>(
304 serializationListeners);
305 }
306}
307
308MultiplexConsumer::MultiplexConsumer(std::unique_ptr<ASTConsumer> C)
309 : MultiplexConsumer([](std::unique_ptr<ASTConsumer> Consumer) {
310 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
311 Consumers.push_back(std::move(Consumer));
312 return Consumers;
313 }(std::move(C))) {}
314
316
318 for (auto &Consumer : Consumers)
319 Consumer->Initialize(Context);
320}
321
323 bool Continue = true;
324 for (auto &Consumer : Consumers)
325 Continue = Continue && Consumer->HandleTopLevelDecl(D);
326 return Continue;
327}
328
330 for (auto &Consumer : Consumers)
331 Consumer->HandleInlineFunctionDefinition(D);
332}
333
335 for (auto &Consumer : Consumers)
336 Consumer->HandleCXXStaticMemberVarInstantiation(VD);
337}
338
340 for (auto &Consumer : Consumers)
341 Consumer->HandleInterestingDecl(D);
342}
343
345 for (auto &Consumer : Consumers)
346 Consumer->HandleTranslationUnit(Ctx);
347}
348
350 for (auto &Consumer : Consumers)
351 Consumer->HandleTagDeclDefinition(D);
352}
353
355 for (auto &Consumer : Consumers)
356 Consumer->HandleTagDeclRequiredDefinition(D);
357}
358
360 for (auto &Consumer : Consumers)
361 Consumer->HandleCXXImplicitFunctionInstantiation(D);
362}
363
365 for (auto &Consumer : Consumers)
366 Consumer->HandleTopLevelDeclInObjCContainer(D);
367}
368
370 for (auto &Consumer : Consumers)
371 Consumer->HandleImplicitImportDecl(D);
372}
373
375 for (auto &Consumer : Consumers)
376 Consumer->CompleteTentativeDefinition(D);
377}
378
380 for (auto &Consumer : Consumers)
381 Consumer->CompleteExternalDeclaration(D);
382}
383
385 for (auto &Consumer : Consumers)
386 Consumer->AssignInheritanceModel(RD);
387}
388
390 for (auto &Consumer : Consumers)
391 Consumer->HandleVTable(RD);
392}
393
397
401
403 for (auto &Consumer : Consumers)
404 Consumer->PrintStats();
405}
406
408 bool Skip = true;
409 for (auto &Consumer : Consumers)
410 Skip = Skip && Consumer->shouldSkipFunctionBody(D);
411 return Skip;
412}
413
415 for (auto &Consumer : Consumers)
416 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
417 SC->InitializeSema(S);
418}
419
421 for (auto &Consumer : Consumers)
422 if (SemaConsumer *SC = dyn_cast<SemaConsumer>(Consumer.get()))
423 SC->ForgetSema();
424}
llvm::MachO::Record Record
Definition MachO.h:31
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Definition ASTConsumer.h:34
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:188
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
Reads an AST files chain containing the contents of a translation unit.
Definition ASTReader.h:430
Attr - This represents one attribute.
Definition Attr.h:44
Represents a C++ destructor within a class.
Definition DeclCXX.h:2869
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Declaration of a class template.
Represents a class template specialization, which refers to a class template with a given set of temp...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:779
This represents one expression.
Definition Expr.h:112
Represents a member of a struct/union/class.
Definition Decl.h:3157
Represents a function declaration or definition.
Definition Decl.h:1999
Declaration of a template function.
One of these records is kept for each identifier that is lexed.
Describes a module import declaration, which makes the contents of the named module visible in the cu...
Definition Decl.h:5015
Record the location of a macro definition.
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:39
Describes a module or submodule.
Definition Module.h:144
void IdentifierRead(serialization::IdentifierID ID, IdentifierInfo *II) override
An identifier was deserialized from the AST file.
void MacroDefinitionRead(serialization::PreprocessedEntityID, MacroDefinitionRecord *MD) override
A macro definition was read from the AST file.
void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) override
A predefined decl was built during the serialization.
void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override
A module definition was read from the AST file.
void TypeRead(serialization::TypeIdx Idx, QualType T) override
A type was deserialized from the AST file.
void DeclRead(GlobalDeclID ID, const Decl *D) override
A decl was deserialized from the AST file.
void SelectorRead(serialization::SelectorID iD, Selector Sel) override
A selector was read from the AST file.
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override
A macro was read from the AST file.
void ReaderInitialized(ASTReader *Reader) override
The ASTReader was initialized.
void ModuleImportRead(serialization::SubmoduleID ID, SourceLocation ImportLoc) override
A module import was read from the AST file.
MultiplexASTDeserializationListener(const std::vector< ASTDeserializationListener * > &L)
void DeclarationMarkedUsed(const Decl *D) override
A declaration is marked used which was not previously marked used.
void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, const ObjCInterfaceDecl *IFD) override
A new objc category class was added for an interface.
void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override
A definition has been made visible by being redefined locally.
void ResolvedExceptionSpec(const FunctionDecl *FD) override
A function's exception specification has been evaluated or instantiated.
void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override
A declaration is marked as a variable with OpenMP allocator.
void CompletedImplicitDefinition(const FunctionDecl *D) override
An implicit member got a definition.
void EnteringModulePurview() override
The parser find the named module declaration.
void DefaultArgumentInstantiated(const ParmVarDecl *D) override
A default argument was instantiated.
void AddedStaticLocalNumbers(const Decl *D, unsigned) override
An static local number was added to a Decl.
void ResolvedOperatorGlobDelete(const CXXDestructorDecl *DD, const FunctionDecl *GlobDelete) override
A virtual destructor's operator global delete has been resolved.
void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override
A function's return type has been deduced.
void AddedManglingNumber(const Decl *D, unsigned) override
An mangling number was added to a Decl.
void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override
An implicit member was added after the definition was completed.
void VariableDefinitionInstantiated(const VarDecl *D) override
A templated variable's definition was implicitly instantiated.
void AddedAnonymousNamespace(const TranslationUnitDecl *, NamespaceDecl *AnonNamespace) override
An anonymous namespace was added the translation unit decl.
void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override
A declaration is marked as OpenMP threadprivate which was not previously marked as threadprivate.
MultiplexASTMutationListener(ArrayRef< ASTMutationListener * > L)
void ResolvedOperatorDelete(const CXXDestructorDecl *DD, const FunctionDecl *Delete, Expr *ThisArg) override
A virtual destructor's operator delete has been resolved.
void InstantiationRequested(const ValueDecl *D) override
The instantiation of a templated function or variable was requested.
void DeclarationMarkedOpenMPDeclareTarget(const Decl *D, const Attr *Attr) override
A declaration is marked as OpenMP declaretarget which was not previously marked as declaretarget.
void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override
A new declaration with name has been added to a DeclContext.
void FunctionDefinitionInstantiated(const FunctionDecl *D) override
A function template's definition was instantiated.
void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) override
A template specialization (or partial one) was added to the template declaration.
void DefaultMemberInitializerInstantiated(const FieldDecl *D) override
A default member initializer was instantiated.
void CompletedTagDefinition(const TagDecl *D) override
A new TagDecl definition was completed.
void AddedAttributeToRecord(const Attr *Attr, const RecordDecl *Record) override
An attribute was added to a RecordDecl.
void HandleImplicitImportDecl(ImportDecl *D) override
Handle an ImportDecl that was implicitly created due to an inclusion directive.
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override
Invoked when a function is implicitly instantiated.
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
void AssignInheritanceModel(CXXRecordDecl *RD) override
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
std::unique_ptr< MultiplexASTDeserializationListener > DeserializationListener
std::unique_ptr< MultiplexASTMutationListener > MutationListener
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
void HandleInterestingDecl(DeclGroupRef D) override
HandleInterestingDecl - Handle the specified interesting declaration.
std::vector< std::unique_ptr< ASTConsumer > > Consumers
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
void CompleteExternalDeclaration(DeclaratorDecl *D) override
CompleteExternalDeclaration - Callback invoked at the end of a translation unit to notify the consume...
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
bool shouldSkipFunctionBody(Decl *D) override
This callback is called for each function if the Parser was initialized with SkipFunctionBodies set t...
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
void PrintStats() override
PrintStats - If desired, print any statistics.
void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override
Handle the specified top-level declaration that occurred inside and ObjC container.
void ForgetSema() override
Inform the semantic consumer that Sema is no longer available.
MultiplexConsumer(std::vector< std::unique_ptr< ASTConsumer > > C)
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void Initialize(ASTContext &Context) override
Initialize - This is called to initialize the consumer, providing the ASTContext.
This represents a decl that may have a name.
Definition Decl.h:273
Represent a C++ namespace.
Definition Decl.h:591
ObjCCategoryDecl - Represents a category declaration.
Definition DeclObjC.h:2329
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
Represents a parameter to a function.
Definition Decl.h:1789
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4309
Smart pointer class that efficiently represents Objective-C method names.
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:854
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3714
The top declaration context.
Definition Decl.h:104
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:711
Represents a variable declaration or definition.
Definition Decl.h:925
Declaration of a variable template.
Represents a variable template specialization, which refers to a variable template with a given set o...
A type index; the type ID with the qualifier bits removed.
Definition ASTBitCodes.h:99
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
uint32_t MacroID
An ID number that refers to a macro in an AST file.
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
uint64_t IdentifierID
An ID number that refers to an identifier in an AST file.
Definition ASTBitCodes.h:63
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
@ Delete
'delete' clause, allowed on the 'exit data' construct.
PredefinedDeclIDs
Predefined declaration IDs.
Definition DeclID.h:31
const FunctionProtoType * T