blob: 3f126615b1eb632d63226119e82e4801b7fc84bd [file] [log] [blame]
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +00001//===- 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 Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000016#include "clang/Frontend/ASTUnit.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Frontend/CompilerInstance.h"
18#include "clang/Frontend/TextDiagnosticPrinter.h"
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Parse/ParseAST.h"
Richard Smith21b3a032016-07-16 00:35:14 +000021#include "clang/Sema/MultiplexExternalSemaSource.h"
Chandler Carruthf887db02011-12-09 01:55:54 +000022#include "clang/Serialization/ASTReader.h"
23#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000024#include "llvm/Support/MemoryBuffer.h"
25
26using namespace clang;
27
Alp Toker9e0523d2014-07-07 11:07:10 +000028namespace {
Richard Smith21b3a032016-07-16 00:35:14 +000029class ChainedIncludesSourceImpl : public ExternalSemaSource {
Alp Toker9e0523d2014-07-07 11:07:10 +000030public:
Richard Smith21b3a032016-07-16 00:35:14 +000031 ChainedIncludesSourceImpl(std::vector<std::unique_ptr<CompilerInstance>> CIs)
32 : CIs(std::move(CIs)) {}
Alp Toker9e0523d2014-07-07 11:07:10 +000033
34protected:
35 //===----------------------------------------------------------------------===//
36 // ExternalASTSource interface.
37 //===----------------------------------------------------------------------===//
38
Alp Toker9e0523d2014-07-07 11:07:10 +000039 /// Return the amount of memory used by memory buffers, breaking down
40 /// by heap-backed versus mmap'ed memory.
Richard Smith21b3a032016-07-16 00:35:14 +000041 void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override {
42 for (unsigned i = 0, e = CIs.size(); i != e; ++i) {
43 if (const ExternalASTSource *eSrc =
44 CIs[i]->getASTContext().getExternalSource()) {
45 eSrc->getMemoryBufferSizes(sizes);
46 }
47 }
48 }
Alp Toker9e0523d2014-07-07 11:07:10 +000049
Richard Smith21b3a032016-07-16 00:35:14 +000050private:
51 std::vector<std::unique_ptr<CompilerInstance>> CIs;
52};
Alp Toker9e0523d2014-07-07 11:07:10 +000053
Richard Smith21b3a032016-07-16 00:35:14 +000054/// Members of ChainedIncludesSource, factored out so we can initialize
55/// them before we initialize the ExternalSemaSource base class.
56struct ChainedIncludesSourceMembers {
Richard Smith5eb7e1b2016-07-17 20:00:59 +000057 ChainedIncludesSourceMembers(
58 std::vector<std::unique_ptr<CompilerInstance>> CIs,
59 IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
60 : Impl(std::move(CIs)), FinalReader(std::move(FinalReader)) {}
Richard Smith21b3a032016-07-16 00:35:14 +000061 ChainedIncludesSourceImpl Impl;
62 IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
63};
64
65/// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource
66/// calls to the final reader.
67class ChainedIncludesSource
68 : private ChainedIncludesSourceMembers,
69 public MultiplexExternalSemaSource {
70public:
71 ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
72 IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
Richard Smith5eb7e1b2016-07-17 20:00:59 +000073 : ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
Richard Smith21b3a032016-07-16 00:35:14 +000074 MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
Alp Toker9e0523d2014-07-07 11:07:10 +000075};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000076}
Alp Toker9e0523d2014-07-07 11:07:10 +000077
Craig Topper49a27902014-05-22 04:46:25 +000078static ASTReader *
79createASTReader(CompilerInstance &CI, StringRef pchFile,
Rafael Espindola5cd06f22014-08-18 19:16:31 +000080 SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs,
Craig Topper49a27902014-05-22 04:46:25 +000081 SmallVectorImpl<std::string> &bufNames,
82 ASTDeserializationListener *deserialListener = nullptr) {
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000083 Preprocessor &PP = CI.getPreprocessor();
Ahmed Charlesb8984322014-03-07 20:03:18 +000084 std::unique_ptr<ASTReader> Reader;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000085 Reader.reset(new ASTReader(PP, CI.getASTContext(),
Adrian Prantlfb2398d2015-07-17 01:19:54 +000086 CI.getPCHContainerReader(),
Douglas Gregor6623e1f2015-11-03 18:33:07 +000087 /*Extensions=*/{ },
Adrian Prantlbb165fb2015-06-20 18:53:08 +000088 /*isysroot=*/"", /*DisableValidation=*/true));
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +000089 for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
90 StringRef sr(bufNames[ti]);
Rafael Espindola5cd06f22014-08-18 19:16:31 +000091 Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti]));
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +000092 }
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000093 Reader->setDeserializationListener(deserialListener);
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +000094 switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(),
Douglas Gregor4b29c162012-10-22 23:51:00 +000095 ASTReader::ARR_None)) {
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000096 case ASTReader::Success:
97 // Set the predefines buffer as suggested by the PCH reader.
98 PP.setPredefines(Reader->getSuggestedPredefines());
Ahmed Charles9a16beb2014-03-07 19:33:25 +000099 return Reader.release();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000100
101 case ASTReader::Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +0000102 case ASTReader::Missing:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000103 case ASTReader::OutOfDate:
104 case ASTReader::VersionMismatch:
105 case ASTReader::ConfigurationMismatch:
106 case ASTReader::HadErrors:
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000107 break;
108 }
Craig Topper49a27902014-05-22 04:46:25 +0000109 return nullptr;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000110}
111
Alp Toker9e0523d2014-07-07 11:07:10 +0000112IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
113 CompilerInstance &CI, IntrusiveRefCntPtr<ExternalSemaSource> &Reader) {
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000114
115 std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
116 assert(!includes.empty() && "No '-chain-include' in options!");
117
Richard Smith21b3a032016-07-16 00:35:14 +0000118 std::vector<std::unique_ptr<CompilerInstance>> CIs;
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000119 InputKind IK = CI.getFrontendOpts().Inputs[0].getKind();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000120
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000121 SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> SerialBufs;
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +0000122 SmallVector<std::string, 4> serialBufNames;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000123
124 for (unsigned i = 0, e = includes.size(); i != e; ++i) {
125 bool firstInclude = (i == 0);
Ahmed Charlesb8984322014-03-07 20:03:18 +0000126 std::unique_ptr<CompilerInvocation> CInvok;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000127 CInvok.reset(new CompilerInvocation(CI.getInvocation()));
128
129 CInvok->getPreprocessorOpts().ChainedIncludes.clear();
130 CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear();
131 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
132 CInvok->getPreprocessorOpts().DisablePCHValidation = true;
133 CInvok->getPreprocessorOpts().Includes.clear();
134 CInvok->getPreprocessorOpts().MacroIncludes.clear();
135 CInvok->getPreprocessorOpts().Macros.clear();
136
137 CInvok->getFrontendOpts().Inputs.clear();
Argyrios Kyrtzidis1b3240b2012-11-09 19:40:33 +0000138 FrontendInputFile InputFile(includes[i], IK);
139 CInvok->getFrontendOpts().Inputs.push_back(InputFile);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000140
141 TextDiagnosticPrinter *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000142 new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000143 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
144 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000145 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000146
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000147 std::unique_ptr<CompilerInstance> Clang(
148 new CompilerInstance(CI.getPCHContainerOperations()));
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000149 Clang->setInvocation(CInvok.release());
Alp Tokerf994cef2014-07-05 03:08:06 +0000150 Clang->setDiagnostics(Diags.get());
Alp Toker80758082014-07-06 05:26:44 +0000151 Clang->setTarget(TargetInfo::CreateTargetInfo(
Saleem Abdulrasool10a49722016-04-08 16:52:00 +0000152 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000153 Clang->createFileManager();
154 Clang->createSourceManager(Clang->getFileManager());
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000155 Clang->createPreprocessor(TU_Prefix);
Sebastian Redl604caf42011-03-31 19:29:18 +0000156 Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
157 &Clang->getPreprocessor());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000158 Clang->createASTContext();
159
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000160 auto Buffer = std::make_shared<PCHBuffer>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000161 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions;
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000162 auto consumer = llvm::make_unique<PCHGenerator>(
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000163 Clang->getPreprocessor(), "-", nullptr, /*isysroot=*/"", Buffer,
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +0000164 Extensions, /*AllowASTWithErrors=*/true);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000165 Clang->getASTContext().setASTMutationListener(
166 consumer->GetASTMutationListener());
David Blaikie6beb6aa2014-08-10 19:56:51 +0000167 Clang->setASTConsumer(std::move(consumer));
Craig Topper49a27902014-05-22 04:46:25 +0000168 Clang->createSema(TU_Prefix, nullptr);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000169
170 if (firstInclude) {
171 Preprocessor &PP = Clang->getPreprocessor();
Eric Christopher02d5d862015-08-06 01:01:12 +0000172 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000173 PP.getLangOpts());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000174 } else {
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000175 assert(!SerialBufs.empty());
176 SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> Bufs;
Alp Toker7eb95e22014-07-07 11:06:51 +0000177 // TODO: Pass through the existing MemoryBuffer instances instead of
178 // allocating new ones.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000179 for (auto &SB : SerialBufs)
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000180 Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000181 std::string pchName = includes[i-1];
182 llvm::raw_string_ostream os(pchName);
183 os << ".pch" << i-1;
Alp Toker7eb95e22014-07-07 11:06:51 +0000184 serialBufNames.push_back(os.str());
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +0000185
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000186 IntrusiveRefCntPtr<ASTReader> Reader;
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000187 Reader = createASTReader(
188 *Clang, pchName, Bufs, serialBufNames,
189 Clang->getASTConsumer().GetASTDeserializationListener());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000190 if (!Reader)
Craig Topper49a27902014-05-22 04:46:25 +0000191 return nullptr;
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000192 Clang->setModuleManager(Reader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000193 Clang->getASTContext().setExternalSource(Reader);
194 }
195
Argyrios Kyrtzidis1b3240b2012-11-09 19:40:33 +0000196 if (!Clang->InitializeSourceManager(InputFile))
Craig Topper49a27902014-05-22 04:46:25 +0000197 return nullptr;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000198
199 ParseAST(Clang->getSema());
Sebastian Redl604caf42011-03-31 19:29:18 +0000200 Clang->getDiagnosticClient().EndSourceFile();
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000201 assert(Buffer->IsComplete && "serialization did not complete");
202 auto &serialAST = Buffer->Data;
203 SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy(
204 StringRef(serialAST.data(), serialAST.size())));
205 serialAST.clear();
Richard Smith21b3a032016-07-16 00:35:14 +0000206 CIs.push_back(std::move(Clang));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000207 }
208
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000209 assert(!SerialBufs.empty());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000210 std::string pchName = includes.back() + ".pch-final";
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +0000211 serialBufNames.push_back(pchName);
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000212 Reader = createASTReader(CI, pchName, SerialBufs, serialBufNames);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000213 if (!Reader)
Craig Topper49a27902014-05-22 04:46:25 +0000214 return nullptr;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000215
Richard Smith21b3a032016-07-16 00:35:14 +0000216 return IntrusiveRefCntPtr<ChainedIncludesSource>(
217 new ChainedIncludesSource(std::move(CIs), Reader));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000218}