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