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