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