Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1 | //===- ChainedIncludesSource.cpp - Chained PCHs in Memory -------*- 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 ChainedIncludesSource class, which converts headers |
| 11 | // to chained PCHs in memory, mainly used for testing. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Serialization/ChainedIncludesSource.h" |
| 16 | #include "clang/Serialization/ASTReader.h" |
| 17 | #include "clang/Serialization/ASTWriter.h" |
| 18 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 19 | #include "clang/Frontend/CompilerInstance.h" |
| 20 | #include "clang/Frontend/ASTUnit.h" |
| 21 | #include "clang/Parse/ParseAST.h" |
| 22 | #include "clang/Lex/Preprocessor.h" |
| 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | static ASTReader *createASTReader(CompilerInstance &CI, |
| 29 | llvm::StringRef pchFile, |
| 30 | llvm::MemoryBuffer **memBufs, |
| 31 | unsigned numBufs, |
| 32 | ASTDeserializationListener *deserialListener = 0) { |
| 33 | Preprocessor &PP = CI.getPreprocessor(); |
| 34 | llvm::OwningPtr<ASTReader> Reader; |
| 35 | Reader.reset(new ASTReader(PP, &CI.getASTContext(), /*isysroot=*/0, |
| 36 | /*DisableValidation=*/true)); |
| 37 | Reader->setASTMemoryBuffers(memBufs, numBufs); |
| 38 | Reader->setDeserializationListener(deserialListener); |
| 39 | switch (Reader->ReadAST(pchFile, ASTReader::PCH)) { |
| 40 | case ASTReader::Success: |
| 41 | // Set the predefines buffer as suggested by the PCH reader. |
| 42 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 43 | return Reader.take(); |
| 44 | |
| 45 | case ASTReader::Failure: |
| 46 | case ASTReader::IgnorePCH: |
| 47 | break; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | ChainedIncludesSource::~ChainedIncludesSource() { |
| 53 | for (unsigned i = 0, e = CIs.size(); i != e; ++i) |
| 54 | delete CIs[i]; |
| 55 | } |
| 56 | |
| 57 | ChainedIncludesSource *ChainedIncludesSource::create(CompilerInstance &CI) { |
| 58 | |
| 59 | std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes; |
| 60 | assert(!includes.empty() && "No '-chain-include' in options!"); |
| 61 | |
| 62 | llvm::OwningPtr<ChainedIncludesSource> source(new ChainedIncludesSource()); |
| 63 | InputKind IK = CI.getFrontendOpts().Inputs[0].first; |
| 64 | |
| 65 | llvm::SmallVector<llvm::MemoryBuffer *, 4> serialBufs; |
| 66 | |
| 67 | for (unsigned i = 0, e = includes.size(); i != e; ++i) { |
| 68 | bool firstInclude = (i == 0); |
| 69 | llvm::OwningPtr<CompilerInvocation> CInvok; |
| 70 | CInvok.reset(new CompilerInvocation(CI.getInvocation())); |
| 71 | |
| 72 | CInvok->getPreprocessorOpts().ChainedIncludes.clear(); |
| 73 | CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear(); |
| 74 | CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear(); |
| 75 | CInvok->getPreprocessorOpts().DisablePCHValidation = true; |
| 76 | CInvok->getPreprocessorOpts().Includes.clear(); |
| 77 | CInvok->getPreprocessorOpts().MacroIncludes.clear(); |
| 78 | CInvok->getPreprocessorOpts().Macros.clear(); |
| 79 | |
| 80 | CInvok->getFrontendOpts().Inputs.clear(); |
| 81 | CInvok->getFrontendOpts().Inputs.push_back(std::make_pair(IK, includes[i])); |
| 82 | |
| 83 | TextDiagnosticPrinter *DiagClient = |
| 84 | new TextDiagnosticPrinter(llvm::errs(), DiagnosticOptions()); |
| 85 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 86 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID, |
| 87 | DiagClient)); |
| 88 | |
| 89 | llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance()); |
| 90 | Clang->setInvocation(CInvok.take()); |
| 91 | Clang->setDiagnostics(Diags.getPtr()); |
| 92 | Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), |
| 93 | Clang->getTargetOpts())); |
| 94 | Clang->createFileManager(); |
| 95 | Clang->createSourceManager(Clang->getFileManager()); |
| 96 | Clang->createPreprocessor(); |
Sebastian Redl | 0ab547c | 2011-03-31 19:29:18 +0000 | [diff] [blame] | 97 | Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(), |
| 98 | &Clang->getPreprocessor()); |
Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 99 | Clang->createASTContext(); |
| 100 | |
| 101 | llvm::SmallVector<char, 256> serialAST; |
| 102 | llvm::raw_svector_ostream OS(serialAST); |
| 103 | llvm::OwningPtr<ASTConsumer> consumer; |
| 104 | consumer.reset(new PCHGenerator(Clang->getPreprocessor(), "-", |
| 105 | /*Chaining=*/!firstInclude, |
| 106 | /*isysroot=*/0, &OS)); |
| 107 | Clang->getASTContext().setASTMutationListener( |
| 108 | consumer->GetASTMutationListener()); |
| 109 | Clang->setASTConsumer(consumer.take()); |
| 110 | Clang->createSema(/*CompleteTranslationUnit=*/false, 0); |
| 111 | |
| 112 | if (firstInclude) { |
| 113 | Preprocessor &PP = Clang->getPreprocessor(); |
| 114 | PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), |
| 115 | PP.getLangOptions()); |
| 116 | } else { |
| 117 | assert(!serialBufs.empty()); |
| 118 | llvm::SmallVector<llvm::MemoryBuffer *, 4> bufs; |
Argyrios Kyrtzidis | 910ee57 | 2011-03-09 23:15:22 +0000 | [diff] [blame] | 119 | for (unsigned si = 0, se = serialBufs.size(); si != se; ++si) { |
Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 120 | bufs.push_back(llvm::MemoryBuffer::getMemBufferCopy( |
Argyrios Kyrtzidis | 910ee57 | 2011-03-09 23:15:22 +0000 | [diff] [blame] | 121 | llvm::StringRef(serialBufs[si]->getBufferStart(), |
| 122 | serialBufs[si]->getBufferSize()))); |
Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 123 | } |
| 124 | std::string pchName = includes[i-1]; |
| 125 | llvm::raw_string_ostream os(pchName); |
| 126 | os << ".pch" << i-1; |
| 127 | os.flush(); |
| 128 | llvm::OwningPtr<ExternalASTSource> Reader; |
| 129 | Reader.reset(createASTReader(*Clang, pchName, bufs.data(), bufs.size(), |
| 130 | Clang->getASTConsumer().GetASTDeserializationListener())); |
| 131 | if (!Reader) |
| 132 | return 0; |
| 133 | Clang->getASTContext().setExternalSource(Reader); |
| 134 | } |
| 135 | |
| 136 | if (!Clang->InitializeSourceManager(includes[i])) |
| 137 | return 0; |
| 138 | |
| 139 | ParseAST(Clang->getSema()); |
| 140 | OS.flush(); |
Sebastian Redl | 0ab547c | 2011-03-31 19:29:18 +0000 | [diff] [blame] | 141 | Clang->getDiagnosticClient().EndSourceFile(); |
Argyrios Kyrtzidis | b0f4b9a | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 142 | serialBufs.push_back( |
| 143 | llvm::MemoryBuffer::getMemBufferCopy(llvm::StringRef(serialAST.data(), |
| 144 | serialAST.size()))); |
| 145 | source->CIs.push_back(Clang.take()); |
| 146 | } |
| 147 | |
| 148 | assert(!serialBufs.empty()); |
| 149 | std::string pchName = includes.back() + ".pch-final"; |
| 150 | llvm::OwningPtr<ASTReader> Reader; |
| 151 | Reader.reset(createASTReader(CI, pchName, |
| 152 | serialBufs.data(), serialBufs.size())); |
| 153 | if (!Reader) |
| 154 | return 0; |
| 155 | |
| 156 | source->FinalReader.reset(Reader.take()); |
| 157 | return source.take(); |
| 158 | } |
| 159 | |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | // ExternalASTSource interface. |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | |
| 164 | Decl *ChainedIncludesSource::GetExternalDecl(uint32_t ID) { |
| 165 | return getFinalReader().GetExternalDecl(ID); |
| 166 | } |
| 167 | Selector ChainedIncludesSource::GetExternalSelector(uint32_t ID) { |
| 168 | return getFinalReader().GetExternalSelector(ID); |
| 169 | } |
| 170 | uint32_t ChainedIncludesSource::GetNumExternalSelectors() { |
| 171 | return getFinalReader().GetNumExternalSelectors(); |
| 172 | } |
| 173 | Stmt *ChainedIncludesSource::GetExternalDeclStmt(uint64_t Offset) { |
| 174 | return getFinalReader().GetExternalDeclStmt(Offset); |
| 175 | } |
| 176 | CXXBaseSpecifier * |
| 177 | ChainedIncludesSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) { |
| 178 | return getFinalReader().GetExternalCXXBaseSpecifiers(Offset); |
| 179 | } |
| 180 | DeclContextLookupResult |
| 181 | ChainedIncludesSource::FindExternalVisibleDeclsByName(const DeclContext *DC, |
| 182 | DeclarationName Name) { |
| 183 | return getFinalReader().FindExternalVisibleDeclsByName(DC, Name); |
| 184 | } |
| 185 | void ChainedIncludesSource::MaterializeVisibleDecls(const DeclContext *DC) { |
| 186 | return getFinalReader().MaterializeVisibleDecls(DC); |
| 187 | } |
| 188 | bool ChainedIncludesSource::FindExternalLexicalDecls(const DeclContext *DC, |
| 189 | bool (*isKindWeWant)(Decl::Kind), |
| 190 | llvm::SmallVectorImpl<Decl*> &Result) { |
| 191 | return getFinalReader().FindExternalLexicalDecls(DC, isKindWeWant, Result); |
| 192 | } |
| 193 | void ChainedIncludesSource::CompleteType(TagDecl *Tag) { |
| 194 | return getFinalReader().CompleteType(Tag); |
| 195 | } |
| 196 | void ChainedIncludesSource::CompleteType(ObjCInterfaceDecl *Class) { |
| 197 | return getFinalReader().CompleteType(Class); |
| 198 | } |
| 199 | void ChainedIncludesSource::StartedDeserializing() { |
| 200 | return getFinalReader().StartedDeserializing(); |
| 201 | } |
| 202 | void ChainedIncludesSource::FinishedDeserializing() { |
| 203 | return getFinalReader().FinishedDeserializing(); |
| 204 | } |
| 205 | void ChainedIncludesSource::StartTranslationUnit(ASTConsumer *Consumer) { |
| 206 | return getFinalReader().StartTranslationUnit(Consumer); |
| 207 | } |
| 208 | void ChainedIncludesSource::PrintStats() { |
| 209 | return getFinalReader().PrintStats(); |
| 210 | } |
Sebastian Redl | ce0682f | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 211 | |
| 212 | void ChainedIncludesSource::InitializeSema(Sema &S) { |
| 213 | return getFinalReader().InitializeSema(S); |
| 214 | } |
| 215 | void ChainedIncludesSource::ForgetSema() { |
| 216 | return getFinalReader().ForgetSema(); |
| 217 | } |
| 218 | std::pair<ObjCMethodList,ObjCMethodList> |
| 219 | ChainedIncludesSource::ReadMethodPool(Selector Sel) { |
| 220 | return getFinalReader().ReadMethodPool(Sel); |
| 221 | } |
| 222 | bool ChainedIncludesSource::LookupUnqualified(LookupResult &R, Scope *S) { |
| 223 | return getFinalReader().LookupUnqualified(R, S); |
| 224 | } |
| 225 | |