xref: /freebsd/contrib/llvm-project/clang/lib/Frontend/ChainedIncludesSource.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1 //===- ChainedIncludesSource.cpp - Chained PCHs in Memory -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ChainedIncludesSource class, which converts headers
10 //  to chained PCHs in memory, mainly used for testing.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Basic/TargetInfo.h"
15 #include "clang/Frontend/ASTUnit.h"
16 #include "clang/Frontend/CompilerInstance.h"
17 #include "clang/Frontend/TextDiagnosticPrinter.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Lex/PreprocessorOptions.h"
20 #include "clang/Parse/ParseAST.h"
21 #include "clang/Sema/MultiplexExternalSemaSource.h"
22 #include "clang/Serialization/ASTReader.h"
23 #include "clang/Serialization/ASTWriter.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 
26 using namespace clang;
27 
28 namespace {
29 class ChainedIncludesSourceImpl : public ExternalSemaSource {
30 public:
31   ChainedIncludesSourceImpl(std::vector<std::unique_ptr<CompilerInstance>> CIs)
32       : CIs(std::move(CIs)) {}
33 
34 protected:
35   //===----------------------------------------------------------------------===//
36   // ExternalASTSource interface.
37   //===----------------------------------------------------------------------===//
38 
39   /// Return the amount of memory used by memory buffers, breaking down
40   /// by heap-backed versus mmap'ed memory.
41   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   }
49 
50 private:
51   std::vector<std::unique_ptr<CompilerInstance>> CIs;
52 };
53 
54 /// Members of ChainedIncludesSource, factored out so we can initialize
55 /// them before we initialize the ExternalSemaSource base class.
56 struct ChainedIncludesSourceMembers {
57   ChainedIncludesSourceMembers(
58       std::vector<std::unique_ptr<CompilerInstance>> CIs,
59       IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
60       : Impl(std::move(CIs)), FinalReader(std::move(FinalReader)) {}
61   ChainedIncludesSourceImpl Impl;
62   IntrusiveRefCntPtr<ExternalSemaSource> FinalReader;
63 };
64 
65 /// Use MultiplexExternalSemaSource to dispatch all ExternalSemaSource
66 /// calls to the final reader.
67 class ChainedIncludesSource
68     : private ChainedIncludesSourceMembers,
69       public MultiplexExternalSemaSource {
70 public:
71   ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
72                         IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
73       : ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
74         MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
75 };
76 }
77 
78 static ASTReader *
79 createASTReader(CompilerInstance &CI, StringRef pchFile,
80                 SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs,
81                 SmallVectorImpl<std::string> &bufNames,
82                 ASTDeserializationListener *deserialListener = nullptr) {
83   Preprocessor &PP = CI.getPreprocessor();
84   std::unique_ptr<ASTReader> Reader;
85   Reader.reset(new ASTReader(PP, CI.getModuleCache(), &CI.getASTContext(),
86                              CI.getPCHContainerReader(),
87                              /*Extensions=*/{},
88                              /*isysroot=*/"", /*DisableValidation=*/true));
89   for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
90     StringRef sr(bufNames[ti]);
91     Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti]));
92   }
93   Reader->setDeserializationListener(deserialListener);
94   switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(),
95                           ASTReader::ARR_None)) {
96   case ASTReader::Success:
97     // Set the predefines buffer as suggested by the PCH reader.
98     PP.setPredefines(Reader->getSuggestedPredefines());
99     return Reader.release();
100 
101   case ASTReader::Failure:
102   case ASTReader::Missing:
103   case ASTReader::OutOfDate:
104   case ASTReader::VersionMismatch:
105   case ASTReader::ConfigurationMismatch:
106   case ASTReader::HadErrors:
107     break;
108   }
109   return nullptr;
110 }
111 
112 IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
113     CompilerInstance &CI, IntrusiveRefCntPtr<ExternalSemaSource> &Reader) {
114 
115   std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
116   assert(!includes.empty() && "No '-chain-include' in options!");
117 
118   std::vector<std::unique_ptr<CompilerInstance>> CIs;
119   InputKind IK = CI.getFrontendOpts().Inputs[0].getKind();
120 
121   SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> SerialBufs;
122   SmallVector<std::string, 4> serialBufNames;
123 
124   for (unsigned i = 0, e = includes.size(); i != e; ++i) {
125     bool firstInclude = (i == 0);
126     std::unique_ptr<CompilerInvocation> CInvok;
127     CInvok.reset(new CompilerInvocation(CI.getInvocation()));
128 
129     CInvok->getPreprocessorOpts().ChainedIncludes.clear();
130     CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear();
131     CInvok->getPreprocessorOpts().DisablePCHValidation = true;
132     CInvok->getPreprocessorOpts().Includes.clear();
133     CInvok->getPreprocessorOpts().MacroIncludes.clear();
134     CInvok->getPreprocessorOpts().Macros.clear();
135 
136     CInvok->getFrontendOpts().Inputs.clear();
137     FrontendInputFile InputFile(includes[i], IK);
138     CInvok->getFrontendOpts().Inputs.push_back(InputFile);
139 
140     TextDiagnosticPrinter *DiagClient =
141       new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
142     IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
143     IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
144         new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
145 
146     std::unique_ptr<CompilerInstance> Clang(
147         new CompilerInstance(CI.getPCHContainerOperations()));
148     Clang->setInvocation(std::move(CInvok));
149     Clang->setDiagnostics(Diags.get());
150     Clang->setTarget(TargetInfo::CreateTargetInfo(
151         Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
152     Clang->createFileManager();
153     Clang->createSourceManager(Clang->getFileManager());
154     Clang->createPreprocessor(TU_Prefix);
155     Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
156                                                  &Clang->getPreprocessor());
157     Clang->createASTContext();
158 
159     auto Buffer = std::make_shared<PCHBuffer>();
160     ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions;
161     auto consumer = std::make_unique<PCHGenerator>(
162         Clang->getPreprocessor(), Clang->getModuleCache(), "-", /*isysroot=*/"",
163         Buffer, Extensions, /*AllowASTWithErrors=*/true);
164     Clang->getASTContext().setASTMutationListener(
165                                             consumer->GetASTMutationListener());
166     Clang->setASTConsumer(std::move(consumer));
167     Clang->createSema(TU_Prefix, nullptr);
168 
169     if (firstInclude) {
170       Preprocessor &PP = Clang->getPreprocessor();
171       PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
172                                              PP.getLangOpts());
173     } else {
174       assert(!SerialBufs.empty());
175       SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> Bufs;
176       // TODO: Pass through the existing MemoryBuffer instances instead of
177       // allocating new ones.
178       for (auto &SB : SerialBufs)
179         Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer()));
180       std::string pchName = includes[i-1];
181       llvm::raw_string_ostream os(pchName);
182       os << ".pch" << i-1;
183       serialBufNames.push_back(os.str());
184 
185       IntrusiveRefCntPtr<ASTReader> Reader;
186       Reader = createASTReader(
187           *Clang, pchName, Bufs, serialBufNames,
188           Clang->getASTConsumer().GetASTDeserializationListener());
189       if (!Reader)
190         return nullptr;
191       Clang->setModuleManager(Reader);
192       Clang->getASTContext().setExternalSource(Reader);
193     }
194 
195     if (!Clang->InitializeSourceManager(InputFile))
196       return nullptr;
197 
198     ParseAST(Clang->getSema());
199     Clang->getDiagnosticClient().EndSourceFile();
200     assert(Buffer->IsComplete && "serialization did not complete");
201     auto &serialAST = Buffer->Data;
202     SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy(
203         StringRef(serialAST.data(), serialAST.size())));
204     serialAST.clear();
205     CIs.push_back(std::move(Clang));
206   }
207 
208   assert(!SerialBufs.empty());
209   std::string pchName = includes.back() + ".pch-final";
210   serialBufNames.push_back(pchName);
211   Reader = createASTReader(CI, pchName, SerialBufs, serialBufNames);
212   if (!Reader)
213     return nullptr;
214 
215   return IntrusiveRefCntPtr<ChainedIncludesSource>(
216       new ChainedIncludesSource(std::move(CIs), Reader));
217 }
218