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