/src/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 ChainedIncludesSource : public ExternalSemaSource { |
31 | | public: |
32 | | ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs) |
33 | 0 | : 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 | | } // end anonymous namespace |
55 | | |
56 | | static ASTReader * |
57 | | createASTReader(CompilerInstance &CI, StringRef pchFile, |
58 | | SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs, |
59 | | SmallVectorImpl<std::string> &bufNames, |
60 | 0 | ASTDeserializationListener *deserialListener = nullptr) { |
61 | 0 | Preprocessor &PP = CI.getPreprocessor(); |
62 | 0 | std::unique_ptr<ASTReader> Reader; |
63 | 0 | Reader.reset(new ASTReader( |
64 | 0 | PP, CI.getModuleCache(), &CI.getASTContext(), CI.getPCHContainerReader(), |
65 | 0 | /*Extensions=*/{}, |
66 | 0 | /*isysroot=*/"", DisableValidationForModuleKind::PCH)); |
67 | 0 | for (unsigned ti = 0; ti < bufNames.size(); ++ti) { |
68 | 0 | StringRef sr(bufNames[ti]); |
69 | 0 | Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti])); |
70 | 0 | } |
71 | 0 | Reader->setDeserializationListener(deserialListener); |
72 | 0 | switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(), |
73 | 0 | ASTReader::ARR_None)) { |
74 | 0 | case ASTReader::Success: |
75 | | // Set the predefines buffer as suggested by the PCH reader. |
76 | 0 | PP.setPredefines(Reader->getSuggestedPredefines()); |
77 | 0 | return Reader.release(); |
78 | | |
79 | 0 | case ASTReader::Failure: |
80 | 0 | case ASTReader::Missing: |
81 | 0 | case ASTReader::OutOfDate: |
82 | 0 | case ASTReader::VersionMismatch: |
83 | 0 | case ASTReader::ConfigurationMismatch: |
84 | 0 | case ASTReader::HadErrors: |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | return nullptr; |
88 | 0 | } |
89 | | |
90 | | IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource( |
91 | 0 | CompilerInstance &CI, IntrusiveRefCntPtr<ExternalSemaSource> &Reader) { |
92 | |
|
93 | 0 | std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes; |
94 | 0 | assert(!includes.empty() && "No '-chain-include' in options!"); |
95 | | |
96 | 0 | std::vector<std::unique_ptr<CompilerInstance>> CIs; |
97 | 0 | InputKind IK = CI.getFrontendOpts().Inputs[0].getKind(); |
98 | |
|
99 | 0 | SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> SerialBufs; |
100 | 0 | SmallVector<std::string, 4> serialBufNames; |
101 | |
|
102 | 0 | for (unsigned i = 0, e = includes.size(); i != e; ++i) { |
103 | 0 | bool firstInclude = (i == 0); |
104 | 0 | std::unique_ptr<CompilerInvocation> CInvok; |
105 | 0 | CInvok.reset(new CompilerInvocation(CI.getInvocation())); |
106 | |
|
107 | 0 | CInvok->getPreprocessorOpts().ChainedIncludes.clear(); |
108 | 0 | CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear(); |
109 | 0 | CInvok->getPreprocessorOpts().DisablePCHOrModuleValidation = |
110 | 0 | DisableValidationForModuleKind::PCH; |
111 | 0 | CInvok->getPreprocessorOpts().Includes.clear(); |
112 | 0 | CInvok->getPreprocessorOpts().MacroIncludes.clear(); |
113 | 0 | CInvok->getPreprocessorOpts().Macros.clear(); |
114 | |
|
115 | 0 | CInvok->getFrontendOpts().Inputs.clear(); |
116 | 0 | FrontendInputFile InputFile(includes[i], IK); |
117 | 0 | CInvok->getFrontendOpts().Inputs.push_back(InputFile); |
118 | |
|
119 | 0 | TextDiagnosticPrinter *DiagClient = |
120 | 0 | new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions()); |
121 | 0 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
122 | 0 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
123 | 0 | new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient)); |
124 | |
|
125 | 0 | std::unique_ptr<CompilerInstance> Clang( |
126 | 0 | new CompilerInstance(CI.getPCHContainerOperations())); |
127 | 0 | Clang->setInvocation(std::move(CInvok)); |
128 | 0 | Clang->setDiagnostics(Diags.get()); |
129 | 0 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
130 | 0 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
131 | 0 | Clang->createFileManager(); |
132 | 0 | Clang->createSourceManager(Clang->getFileManager()); |
133 | 0 | Clang->createPreprocessor(TU_Prefix); |
134 | 0 | Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(), |
135 | 0 | &Clang->getPreprocessor()); |
136 | 0 | Clang->createASTContext(); |
137 | |
|
138 | 0 | auto Buffer = std::make_shared<PCHBuffer>(); |
139 | 0 | ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions; |
140 | 0 | auto consumer = std::make_unique<PCHGenerator>( |
141 | 0 | Clang->getPreprocessor(), Clang->getModuleCache(), "-", /*isysroot=*/"", |
142 | 0 | Buffer, Extensions, /*AllowASTWithErrors=*/true); |
143 | 0 | Clang->getASTContext().setASTMutationListener( |
144 | 0 | consumer->GetASTMutationListener()); |
145 | 0 | Clang->setASTConsumer(std::move(consumer)); |
146 | 0 | Clang->createSema(TU_Prefix, nullptr); |
147 | |
|
148 | 0 | if (firstInclude) { |
149 | 0 | Preprocessor &PP = Clang->getPreprocessor(); |
150 | 0 | PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(), |
151 | 0 | PP.getLangOpts()); |
152 | 0 | } else { |
153 | 0 | assert(!SerialBufs.empty()); |
154 | 0 | SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 4> Bufs; |
155 | | // TODO: Pass through the existing MemoryBuffer instances instead of |
156 | | // allocating new ones. |
157 | 0 | for (auto &SB : SerialBufs) |
158 | 0 | Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer())); |
159 | 0 | std::string pchName = includes[i-1]; |
160 | 0 | llvm::raw_string_ostream os(pchName); |
161 | 0 | os << ".pch" << i-1; |
162 | 0 | serialBufNames.push_back(os.str()); |
163 | |
|
164 | 0 | IntrusiveRefCntPtr<ASTReader> Reader; |
165 | 0 | Reader = createASTReader( |
166 | 0 | *Clang, pchName, Bufs, serialBufNames, |
167 | 0 | Clang->getASTConsumer().GetASTDeserializationListener()); |
168 | 0 | if (!Reader) |
169 | 0 | return nullptr; |
170 | 0 | Clang->setASTReader(Reader); |
171 | 0 | Clang->getASTContext().setExternalSource(Reader); |
172 | 0 | } |
173 | | |
174 | 0 | if (!Clang->InitializeSourceManager(InputFile)) |
175 | 0 | return nullptr; |
176 | | |
177 | 0 | ParseAST(Clang->getSema()); |
178 | 0 | Clang->getDiagnosticClient().EndSourceFile(); |
179 | 0 | assert(Buffer->IsComplete && "serialization did not complete"); |
180 | 0 | auto &serialAST = Buffer->Data; |
181 | 0 | SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy( |
182 | 0 | StringRef(serialAST.data(), serialAST.size()))); |
183 | 0 | serialAST.clear(); |
184 | 0 | CIs.push_back(std::move(Clang)); |
185 | 0 | } |
186 | | |
187 | 0 | assert(!SerialBufs.empty()); |
188 | 0 | std::string pchName = includes.back() + ".pch-final"; |
189 | 0 | serialBufNames.push_back(pchName); |
190 | 0 | Reader = createASTReader(CI, pchName, SerialBufs, serialBufNames); |
191 | 0 | if (!Reader) |
192 | 0 | return nullptr; |
193 | | |
194 | 0 | auto ChainedSrc = |
195 | 0 | llvm::makeIntrusiveRefCnt<ChainedIncludesSource>(std::move(CIs)); |
196 | 0 | return llvm::makeIntrusiveRefCnt<MultiplexExternalSemaSource>( |
197 | 0 | ChainedSrc.get(), Reader.get()); |
198 | 0 | } |