blob: b984c2ed0dd51dfe7ebb077df18f21e3456e08a4 [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"
Mehdi Amini9670f842016-07-18 19:02:11 +000020#include "clang/Lex/PreprocessorOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Parse/ParseAST.h"
Richard Smith21b3a032016-07-16 00:35:14 +000022#include "clang/Sema/MultiplexExternalSemaSource.h"
Chandler Carruthf887db02011-12-09 01:55:54 +000023#include "clang/Serialization/ASTReader.h"
24#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000025#include "llvm/Support/MemoryBuffer.h"
26
27using namespace clang;
28
Alp Toker9e0523d2014-07-07 11:07:10 +000029namespace {
Richard Smith21b3a032016-07-16 00:35:14 +000030class ChainedIncludesSourceImpl : public ExternalSemaSource {
Alp Toker9e0523d2014-07-07 11:07:10 +000031public:
Richard Smith21b3a032016-07-16 00:35:14 +000032 ChainedIncludesSourceImpl(std::vector<std::unique_ptr<CompilerInstance>> CIs)
33 : CIs(std::move(CIs)) {}
Alp Toker9e0523d2014-07-07 11:07:10 +000034
35protected:
36 //===----------------------------------------------------------------------===//
37 // ExternalASTSource interface.
38 //===----------------------------------------------------------------------===//
39
Alp Toker9e0523d2014-07-07 11:07:10 +000040 /// Return the amount of memory used by memory buffers, breaking down
41 /// by heap-backed versus mmap'ed memory.
Richard Smith21b3a032016-07-16 00:35:14 +000042 void getMemoryBufferSizes(MemoryBufferSizes &sizes) const override {
43 for (unsigned i = 0, e = CIs.size(); i != e; ++i) {
44 if (const ExternalASTSource *eSrc =
45 CIs[i]->getASTContext().getExternalSource()) {
46 eSrc->getMemoryBufferSizes(sizes);
47 }
48 }
49 }
Alp Toker9e0523d2014-07-07 11:07:10 +000050
Richard Smith21b3a032016-07-16 00:35:14 +000051private:
52 std::vector<std::unique_ptr<CompilerInstance>> CIs;
53};
Alp Toker9e0523d2014-07-07 11:07:10 +000054
Richard Smith21b3a032016-07-16 00:35:14 +000055/// Members of ChainedIncludesSource, factored out so we can initialize
56/// them before we initialize the ExternalSemaSource base class.
57struct ChainedIncludesSourceMembers {
Richard Smith5eb7e1b2016-07-17 20:00:59 +000058 ChainedIncludesSourceMembers(
59 std::vector<std::unique_ptr<CompilerInstance>> CIs,
60 IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
61 : Impl(std::move(CIs)), FinalReader(std::move(FinalReader)) {}
Richard Smith21b3a032016-07-16 00:35:14 +000062 ChainedIncludesSourceImpl Impl;
63 IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
64};
65
66/// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource
67/// calls to the final reader.
68class ChainedIncludesSource
69 : private ChainedIncludesSourceMembers,
70 public MultiplexExternalSemaSource {
71public:
72 ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
73 IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
Richard Smith5eb7e1b2016-07-17 20:00:59 +000074 : ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
Richard Smith21b3a032016-07-16 00:35:14 +000075 MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
Alp Toker9e0523d2014-07-07 11:07:10 +000076};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000077}
Alp Toker9e0523d2014-07-07 11:07:10 +000078
Craig Topper49a27902014-05-22 04:46:25 +000079static ASTReader *
80createASTReader(CompilerInstance &CI, StringRef pchFile,
Rafael Espindola5cd06f22014-08-18 19:16:31 +000081 SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs,
Craig Topper49a27902014-05-22 04:46:25 +000082 SmallVectorImpl<std::string> &bufNames,
83 ASTDeserializationListener *deserialListener = nullptr) {
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000084 Preprocessor &PP = CI.getPreprocessor();
Ahmed Charlesb8984322014-03-07 20:03:18 +000085 std::unique_ptr<ASTReader> Reader;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000086 Reader.reset(new ASTReader(PP, CI.getASTContext(),
Adrian Prantlfb2398d2015-07-17 01:19:54 +000087 CI.getPCHContainerReader(),
Douglas Gregor6623e1f2015-11-03 18:33:07 +000088 /*Extensions=*/{ },
Adrian Prantlbb165fb2015-06-20 18:53:08 +000089 /*isysroot=*/"", /*DisableValidation=*/true));
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +000090 for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
91 StringRef sr(bufNames[ti]);
Rafael Espindola5cd06f22014-08-18 19:16:31 +000092 Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti]));
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +000093 }
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000094 Reader->setDeserializationListener(deserialListener);
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +000095 switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(),
Douglas Gregor4b29c162012-10-22 23:51:00 +000096 ASTReader::ARR_None)) {
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +000097 case ASTReader::Success:
98 // Set the predefines buffer as suggested by the PCH reader.
99 PP.setPredefines(Reader->getSuggestedPredefines());
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000100 return Reader.release();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000101
102 case ASTReader::Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +0000103 case ASTReader::Missing:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000104 case ASTReader::OutOfDate:
105 case ASTReader::VersionMismatch:
106 case ASTReader::ConfigurationMismatch:
107 case ASTReader::HadErrors:
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000108 break;
109 }
Craig Topper49a27902014-05-22 04:46:25 +0000110 return nullptr;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000111}
112
Alp Toker9e0523d2014-07-07 11:07:10 +0000113IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
114 CompilerInstance &CI, IntrusiveRefCntPtr<ExternalSemaSource> &Reader) {
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000115
116 std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
117 assert(!includes.empty() && "No '-chain-include' in options!");
118
Richard Smith21b3a032016-07-16 00:35:14 +0000119 std::vector<std::unique_ptr<CompilerInstance>> CIs;
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000120 InputKind IK = CI.getFrontendOpts().Inputs[0].getKind();
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000121
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000122 SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> SerialBufs;
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +0000123 SmallVector<std::string, 4> serialBufNames;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000124
125 for (unsigned i = 0, e = includes.size(); i != e; ++i) {
126 bool firstInclude = (i == 0);
Ahmed Charlesb8984322014-03-07 20:03:18 +0000127 std::unique_ptr<CompilerInvocation> CInvok;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000128 CInvok.reset(new CompilerInvocation(CI.getInvocation()));
129
130 CInvok->getPreprocessorOpts().ChainedIncludes.clear();
131 CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear();
132 CInvok->getPreprocessorOpts().ImplicitPTHInclude.clear();
133 CInvok->getPreprocessorOpts().DisablePCHValidation = true;
134 CInvok->getPreprocessorOpts().Includes.clear();
135 CInvok->getPreprocessorOpts().MacroIncludes.clear();
136 CInvok->getPreprocessorOpts().Macros.clear();
137
138 CInvok->getFrontendOpts().Inputs.clear();
Argyrios Kyrtzidis1b3240b2012-11-09 19:40:33 +0000139 FrontendInputFile InputFile(includes[i], IK);
140 CInvok->getFrontendOpts().Inputs.push_back(InputFile);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000141
142 TextDiagnosticPrinter *DiagClient =
Douglas Gregor811db4e2012-10-23 22:26:28 +0000143 new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000144 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
145 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Douglas Gregor811db4e2012-10-23 22:26:28 +0000146 new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000147
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000148 std::unique_ptr<CompilerInstance> Clang(
149 new CompilerInstance(CI.getPCHContainerOperations()));
David Blaikieea4395e2017-01-06 19:49:01 +0000150 Clang->setInvocation(std::move(CInvok));
Alp Tokerf994cef2014-07-05 03:08:06 +0000151 Clang->setDiagnostics(Diags.get());
Alp Toker80758082014-07-06 05:26:44 +0000152 Clang->setTarget(TargetInfo::CreateTargetInfo(
Saleem Abdulrasool10a49722016-04-08 16:52:00 +0000153 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000154 Clang->createFileManager();
155 Clang->createSourceManager(Clang->getFileManager());
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000156 Clang->createPreprocessor(TU_Prefix);
Sebastian Redl604caf42011-03-31 19:29:18 +0000157 Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
158 &Clang->getPreprocessor());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000159 Clang->createASTContext();
160
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000161 auto Buffer = std::make_shared<PCHBuffer>();
David Blaikie61137e12017-01-05 18:23:18 +0000162 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions;
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000163 auto consumer = llvm::make_unique<PCHGenerator>(
Richard Smithbd97f352016-08-25 18:26:30 +0000164 Clang->getPreprocessor(), "-", /*isysroot=*/"", Buffer,
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +0000165 Extensions, /*AllowASTWithErrors=*/true);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000166 Clang->getASTContext().setASTMutationListener(
167 consumer->GetASTMutationListener());
David Blaikie6beb6aa2014-08-10 19:56:51 +0000168 Clang->setASTConsumer(std::move(consumer));
Craig Topper49a27902014-05-22 04:46:25 +0000169 Clang->createSema(TU_Prefix, nullptr);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000170
171 if (firstInclude) {
172 Preprocessor &PP = Clang->getPreprocessor();
Eric Christopher02d5d862015-08-06 01:01:12 +0000173 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000174 PP.getLangOpts());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000175 } else {
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000176 assert(!SerialBufs.empty());
177 SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> Bufs;
Alp Toker7eb95e22014-07-07 11:06:51 +0000178 // TODO: Pass through the existing MemoryBuffer instances instead of
179 // allocating new ones.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000180 for (auto &SB : SerialBufs)
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000181 Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000182 std::string pchName = includes[i-1];
183 llvm::raw_string_ostream os(pchName);
184 os << ".pch" << i-1;
Alp Toker7eb95e22014-07-07 11:06:51 +0000185 serialBufNames.push_back(os.str());
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +0000186
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000187 IntrusiveRefCntPtr<ASTReader> Reader;
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000188 Reader = createASTReader(
189 *Clang, pchName, Bufs, serialBufNames,
190 Clang->getASTConsumer().GetASTDeserializationListener());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000191 if (!Reader)
Craig Topper49a27902014-05-22 04:46:25 +0000192 return nullptr;
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000193 Clang->setModuleManager(Reader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000194 Clang->getASTContext().setExternalSource(Reader);
195 }
196
Argyrios Kyrtzidis1b3240b2012-11-09 19:40:33 +0000197 if (!Clang->InitializeSourceManager(InputFile))
Craig Topper49a27902014-05-22 04:46:25 +0000198 return nullptr;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000199
200 ParseAST(Clang->getSema());
Sebastian Redl604caf42011-03-31 19:29:18 +0000201 Clang->getDiagnosticClient().EndSourceFile();
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000202 assert(Buffer->IsComplete && "serialization did not complete");
203 auto &serialAST = Buffer->Data;
204 SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy(
205 StringRef(serialAST.data(), serialAST.size())));
206 serialAST.clear();
Richard Smith21b3a032016-07-16 00:35:14 +0000207 CIs.push_back(std::move(Clang));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000208 }
209
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000210 assert(!SerialBufs.empty());
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000211 std::string pchName = includes.back() + ".pch-final";
Jonathan D. Turnerdb1c9e32011-08-02 17:40:32 +0000212 serialBufNames.push_back(pchName);
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000213 Reader = createASTReader(CI, pchName, SerialBufs, serialBufNames);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000214 if (!Reader)
Craig Topper49a27902014-05-22 04:46:25 +0000215 return nullptr;
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000216
Richard Smith21b3a032016-07-16 00:35:14 +0000217 return IntrusiveRefCntPtr<ChainedIncludesSource>(
218 new ChainedIncludesSource(std::move(CIs), Reader));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000219}