Coverage Report

Created: 2024-01-17 10:31

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