clang 22.0.0git
ChainedIncludesSource.cpp
Go to the documentation of this file.
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
25#include "llvm/Support/MemoryBuffer.h"
26
27using namespace clang;
28
29namespace {
30class ChainedIncludesSource : public ExternalSemaSource {
31public:
32 ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs)
33 : CIs(std::move(CIs)) {}
34
35protected:
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 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 }
50
51private:
52 std::vector<std::unique_ptr<CompilerInstance>> CIs;
53};
54} // end anonymous namespace
55
57createASTReader(CompilerInstance &CI, StringRef pchFile,
58 SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs,
60 ASTDeserializationListener *deserialListener = nullptr) {
62 auto Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
64 CI.getCodeGenOpts(),
65 /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
66 /*isysroot=*/"", DisableValidationForModuleKind::PCH);
67 for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
68 StringRef sr(bufNames[ti]);
69 Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti]));
70 }
71 Reader->setDeserializationListener(deserialListener);
72 switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(),
75 // Set the predefines buffer as suggested by the PCH reader.
76 PP.setPredefines(Reader->getSuggestedPredefines());
77 return Reader;
78
85 break;
86 }
87 return nullptr;
88}
89
93
94 std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
95 assert(!includes.empty() && "No '-chain-include' in options!");
96
97 std::vector<std::unique_ptr<CompilerInstance>> CIs;
98 InputKind IK = CI.getFrontendOpts().Inputs[0].getKind();
99
101 SmallVector<std::string, 4> serialBufNames;
102
103 for (unsigned i = 0, e = includes.size(); i != e; ++i) {
104 bool firstInclude = (i == 0);
105 std::unique_ptr<CompilerInvocation> CInvok;
106 CInvok.reset(new CompilerInvocation(CI.getInvocation()));
107
108 CInvok->getPreprocessorOpts().ChainedIncludes.clear();
109 CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear();
110 CInvok->getPreprocessorOpts().DisablePCHOrModuleValidation =
111 DisableValidationForModuleKind::PCH;
112 CInvok->getPreprocessorOpts().Includes.clear();
113 CInvok->getPreprocessorOpts().MacroIncludes.clear();
114 CInvok->getPreprocessorOpts().Macros.clear();
115
116 CInvok->getFrontendOpts().Inputs.clear();
117 FrontendInputFile InputFile(includes[i], IK);
118 CInvok->getFrontendOpts().Inputs.push_back(InputFile);
119
120 TextDiagnosticPrinter *DiagClient =
121 new TextDiagnosticPrinter(llvm::errs(), CI.getDiagnosticOpts());
122 auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
123 DiagnosticIDs::create(), CI.getDiagnosticOpts(), DiagClient);
124
125 auto Clang = std::make_unique<CompilerInstance>(
126 std::move(CInvok), CI.getPCHContainerOperations());
127 Clang->setDiagnostics(Diags);
128 Clang->setTarget(TargetInfo::CreateTargetInfo(
129 Clang->getDiagnostics(), Clang->getInvocation().getTargetOpts()));
130 Clang->createFileManager();
131 Clang->createSourceManager(Clang->getFileManager());
132 Clang->createPreprocessor(TU_Prefix);
133 Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
134 &Clang->getPreprocessor());
135 Clang->createASTContext();
136
137 auto Buffer = std::make_shared<PCHBuffer>();
139 auto consumer = std::make_unique<PCHGenerator>(
140 Clang->getPreprocessor(), Clang->getModuleCache(), "-", /*isysroot=*/"",
141 Buffer, Clang->getCodeGenOpts(), Extensions,
142 /*AllowASTWithErrors=*/true);
143 Clang->getASTContext().setASTMutationListener(
144 consumer->GetASTMutationListener());
145 Clang->setASTConsumer(std::move(consumer));
146 Clang->createSema(TU_Prefix, nullptr);
147
148 if (firstInclude) {
149 Preprocessor &PP = Clang->getPreprocessor();
151 PP.getLangOpts());
152 } else {
153 assert(!SerialBufs.empty());
155 // TODO: Pass through the existing MemoryBuffer instances instead of
156 // allocating new ones.
157 for (auto &SB : SerialBufs)
158 Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer()));
159 std::string pchName = includes[i-1];
160 llvm::raw_string_ostream os(pchName);
161 os << ".pch" << i-1;
162 serialBufNames.push_back(pchName);
163
165 Reader = createASTReader(
166 *Clang, pchName, Bufs, serialBufNames,
167 Clang->getASTConsumer().GetASTDeserializationListener());
168 if (!Reader)
169 return nullptr;
170 Clang->setASTReader(Reader);
171 Clang->getASTContext().setExternalSource(Reader);
172 }
173
174 if (!Clang->InitializeSourceManager(InputFile))
175 return nullptr;
176
177 ParseAST(Clang->getSema());
178 Clang->getDiagnosticClient().EndSourceFile();
179 assert(Buffer->IsComplete && "serialization did not complete");
180 auto &serialAST = Buffer->Data;
181 SerialBufs.push_back(llvm::MemoryBuffer::getMemBufferCopy(
182 StringRef(serialAST.data(), serialAST.size())));
183 serialAST.clear();
184 CIs.push_back(std::move(Clang));
185 }
186
187 assert(!SerialBufs.empty());
188 std::string pchName = includes.back() + ".pch-final";
189 serialBufNames.push_back(pchName);
190 OutReader = createASTReader(CI, pchName, SerialBufs, serialBufNames);
191 if (!OutReader)
192 return nullptr;
193
194 auto ChainedSrc =
195 llvm::makeIntrusiveRefCnt<ChainedIncludesSource>(std::move(CIs));
196 return llvm::makeIntrusiveRefCnt<MultiplexExternalSemaSource>(
197 std::move(ChainedSrc), OutReader);
198}
Defines enum values for all the target-independent builtin functions.
static llvm::IntrusiveRefCntPtr< ASTReader > createASTReader(CompilerInstance &CI, StringRef pchFile, SmallVectorImpl< std::unique_ptr< llvm::MemoryBuffer > > &MemBufs, SmallVectorImpl< std::string > &bufNames, ASTDeserializationListener *deserialListener=nullptr)
Defines the clang::Preprocessor interface.
@ ARR_None
The client can't handle any AST loading failures.
Definition: ASTReader.h:1828
@ Success
The control block was read successfully.
Definition: ASTReader.h:452
@ ConfigurationMismatch
The AST file was written with a different language/target configuration.
Definition: ASTReader.h:469
@ OutOfDate
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition: ASTReader.h:462
@ Failure
The AST file itself appears corrupted.
Definition: ASTReader.h:455
@ VersionMismatch
The AST file was written by a different version of Clang.
Definition: ASTReader.h:465
@ HadErrors
The AST file has errors.
Definition: ASTReader.h:472
@ Missing
The AST file was missing.
Definition: ASTReader.h:458
void initializeBuiltins(IdentifierTable &Table, const LangOptions &LangOpts)
Mark the identifiers for all the builtins with their appropriate builtin ID # and mark any non-portab...
Definition: Builtins.cpp:203
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
ModuleCache & getModuleCache() const
Preprocessor & getPreprocessor() const
Return the current preprocessor.
ASTContext & getASTContext() const
FrontendOptions & getFrontendOpts()
CompilerInvocation & getInvocation()
PreprocessorOptions & getPreprocessorOpts()
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
DiagnosticOptions & getDiagnosticOpts()
CodeGenOptions & getCodeGenOpts()
Helper class for holding the data necessary to invoke the compiler.
static llvm::IntrusiveRefCntPtr< DiagnosticIDs > create()
Abstract interface for external sources of AST nodes.
MemoryBufferSizes getMemoryBufferSizes() const
Return the amount of memory used by memory buffers, breaking down by heap-backed versus mmap'ed memor...
An abstract interface that should be implemented by external AST sources that also provide informatio...
An input file for the front end.
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
The kind of a file that we've been handed as an input.
std::vector< std::string > ChainedIncludes
Headers that will be converted to chained PCHs in memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:145
void setPredefines(std::string P)
Set the predefines for this Preprocessor.
IdentifierTable & getIdentifierTable()
Builtin::Context & getBuiltinInfo()
const LangOptions & getLangOpts() const
Encodes a location in the source.
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, TargetOptions &Opts)
Construct a target for the given options.
Definition: Targets.cpp:777
Defines the clang::TargetInfo interface.
@ MK_PCH
File is a PCH file treated as such.
Definition: ModuleFile.h:51
The JSON file list parser is used to communicate input to InstallAPI.
void ParseAST(Preprocessor &pp, ASTConsumer *C, ASTContext &Ctx, bool PrintStats=false, TranslationUnitKind TUKind=TU_Complete, CodeCompleteConsumer *CompletionConsumer=nullptr, bool SkipFunctionBodies=false)
Parse the entire file specified, notifying the ASTConsumer as the file is parsed.
Definition: ParseAST.cpp:99
IntrusiveRefCntPtr< ExternalSemaSource > createChainedIncludesSource(CompilerInstance &CI, IntrusiveRefCntPtr< ASTReader > &OutReader)
The ChainedIncludesSource class converts headers to chained PCHs in memory, mainly for testing.
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:1103