clang  14.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 
14 #include "clang/Basic/Builtins.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Frontend/ASTUnit.h"
19 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Parse/ParseAST.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  : 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  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 
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  : 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,
71 public:
72  ChainedIncludesSource(std::vector<std::unique_ptr<CompilerInstance>> CIs,
73  IntrusiveRefCntPtr<ExternalSemaSource> FinalReader)
74  : ChainedIncludesSourceMembers(std::move(CIs), std::move(FinalReader)),
75  MultiplexExternalSemaSource(Impl, *this->FinalReader) {}
76 };
77 }
78 
79 static ASTReader *
80 createASTReader(CompilerInstance &CI, StringRef pchFile,
81  SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> &MemBufs,
83  ASTDeserializationListener *deserialListener = nullptr) {
84  Preprocessor &PP = CI.getPreprocessor();
85  std::unique_ptr<ASTReader> Reader;
86  Reader.reset(new ASTReader(
88  /*Extensions=*/{},
89  /*isysroot=*/"", DisableValidationForModuleKind::PCH));
90  for (unsigned ti = 0; ti < bufNames.size(); ++ti) {
91  StringRef sr(bufNames[ti]);
92  Reader->addInMemoryBuffer(sr, std::move(MemBufs[ti]));
93  }
94  Reader->setDeserializationListener(deserialListener);
95  switch (Reader->ReadAST(pchFile, serialization::MK_PCH, SourceLocation(),
97  case ASTReader::Success:
98  // Set the predefines buffer as suggested by the PCH reader.
99  PP.setPredefines(Reader->getSuggestedPredefines());
100  return Reader.release();
101 
102  case ASTReader::Failure:
103  case ASTReader::Missing:
108  break;
109  }
110  return nullptr;
111 }
112 
115 
116  std::vector<std::string> &includes = CI.getPreprocessorOpts().ChainedIncludes;
117  assert(!includes.empty() && "No '-chain-include' in options!");
118 
119  std::vector<std::unique_ptr<CompilerInstance>> CIs;
120  InputKind IK = CI.getFrontendOpts().Inputs[0].getKind();
121 
123  SmallVector<std::string, 4> serialBufNames;
124 
125  for (unsigned i = 0, e = includes.size(); i != e; ++i) {
126  bool firstInclude = (i == 0);
127  std::unique_ptr<CompilerInvocation> CInvok;
128  CInvok.reset(new CompilerInvocation(CI.getInvocation()));
129 
130  CInvok->getPreprocessorOpts().ChainedIncludes.clear();
131  CInvok->getPreprocessorOpts().ImplicitPCHInclude.clear();
132  CInvok->getPreprocessorOpts().DisablePCHOrModuleValidation =
134  CInvok->getPreprocessorOpts().Includes.clear();
135  CInvok->getPreprocessorOpts().MacroIncludes.clear();
136  CInvok->getPreprocessorOpts().Macros.clear();
137 
138  CInvok->getFrontendOpts().Inputs.clear();
139  FrontendInputFile InputFile(includes[i], IK);
140  CInvok->getFrontendOpts().Inputs.push_back(InputFile);
141 
142  TextDiagnosticPrinter *DiagClient =
143  new TextDiagnosticPrinter(llvm::errs(), new DiagnosticOptions());
146  new DiagnosticsEngine(DiagID, &CI.getDiagnosticOpts(), DiagClient));
147 
148  std::unique_ptr<CompilerInstance> Clang(
150  Clang->setInvocation(std::move(CInvok));
151  Clang->setDiagnostics(Diags.get());
152  Clang->setTarget(TargetInfo::CreateTargetInfo(
153  Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
154  Clang->createFileManager();
155  Clang->createSourceManager(Clang->getFileManager());
156  Clang->createPreprocessor(TU_Prefix);
157  Clang->getDiagnosticClient().BeginSourceFile(Clang->getLangOpts(),
158  &Clang->getPreprocessor());
159  Clang->createASTContext();
160 
161  auto Buffer = std::make_shared<PCHBuffer>();
163  auto consumer = std::make_unique<PCHGenerator>(
164  Clang->getPreprocessor(), Clang->getModuleCache(), "-", /*isysroot=*/"",
165  Buffer, Extensions, /*AllowASTWithErrors=*/true);
166  Clang->getASTContext().setASTMutationListener(
167  consumer->GetASTMutationListener());
168  Clang->setASTConsumer(std::move(consumer));
169  Clang->createSema(TU_Prefix, nullptr);
170 
171  if (firstInclude) {
172  Preprocessor &PP = Clang->getPreprocessor();
174  PP.getLangOpts());
175  } else {
176  assert(!SerialBufs.empty());
178  // TODO: Pass through the existing MemoryBuffer instances instead of
179  // allocating new ones.
180  for (auto &SB : SerialBufs)
181  Bufs.push_back(llvm::MemoryBuffer::getMemBuffer(SB->getBuffer()));
182  std::string pchName = includes[i-1];
183  llvm::raw_string_ostream os(pchName);
184  os << ".pch" << i-1;
185  serialBufNames.push_back(os.str());
186 
188  Reader = createASTReader(
189  *Clang, pchName, Bufs, serialBufNames,
190  Clang->getASTConsumer().GetASTDeserializationListener());
191  if (!Reader)
192  return nullptr;
193  Clang->setASTReader(Reader);
194  Clang->getASTContext().setExternalSource(Reader);
195  }
196 
197  if (!Clang->InitializeSourceManager(InputFile))
198  return nullptr;
199 
200  ParseAST(Clang->getSema());
201  Clang->getDiagnosticClient().EndSourceFile();
202  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();
207  CIs.push_back(std::move(Clang));
208  }
209 
210  assert(!SerialBufs.empty());
211  std::string pchName = includes.back() + ".pch-final";
212  serialBufNames.push_back(pchName);
213  Reader = createASTReader(CI, pchName, SerialBufs, serialBufNames);
214  if (!Reader)
215  return nullptr;
216 
218  new ChainedIncludesSource(std::move(CIs), Reader));
219 }
Builtins.h
clang::ASTReader::HadErrors
@ HadErrors
The AST file has errors.
Definition: ASTReader.h:390
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::ASTReader::Failure
@ Failure
The AST file itself appears corrupted.
Definition: ASTReader.h:373
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
TargetInfo.h
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::FrontendInputFile
An input file for the front end.
Definition: FrontendOptions.h:185
ASTWriter.h
clang::InputKind
The kind of a file that we've been handed as an input.
Definition: FrontendOptions.h:145
clang::CompilerInstance::getPCHContainerReader
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
Definition: CompilerInstance.h:544
clang::Preprocessor::getIdentifierTable
IdentifierTable & getIdentifierTable()
Definition: Preprocessor.h:967
clang::FrontendOptions::Inputs
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
Definition: FrontendOptions.h:386
clang::Preprocessor::getLangOpts
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:960
Preprocessor.h
clang::TU_Prefix
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:709
clang::ASTReader::Success
@ Success
The control block was read successfully.
Definition: ASTReader.h:370
clang::Preprocessor::setPredefines
void setPredefines(const char *P)
Set the predefines for this Preprocessor.
Definition: Preprocessor.h:1238
MultiplexExternalSemaSource.h
clang::ASTReader::ARR_None
@ ARR_None
The client can't handle any AST loading failures.
Definition: ASTReader.h:1535
clang::PreprocessorOptions::ChainedIncludes
std::vector< std::string > ChainedIncludes
Headers that will be converted to chained PCHs in memory.
Definition: PreprocessorOptions.h:99
clang::ExternalSemaSource
An abstract interface that should be implemented by external AST sources that also provide informatio...
Definition: ExternalSemaSource.h:52
clang::MultiplexExternalSemaSource
An abstract interface that should be implemented by external AST sources that also provide informatio...
Definition: MultiplexExternalSemaSource.h:38
clang::TargetInfo::CreateTargetInfo
static TargetInfo * CreateTargetInfo(DiagnosticsEngine &Diags, const std::shared_ptr< TargetOptions > &Opts)
Construct a target for the given options.
Definition: Targets.cpp:656
clang::CompilerInstance::getPreprocessor
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Definition: CompilerInstance.h:444
clang::CompilerInstance::getASTContext
ASTContext & getASTContext() const
Definition: CompilerInstance.h:464
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:74
clang::CompilerInstance::getPreprocessorOpts
PreprocessorOptions & getPreprocessorOpts()
Definition: CompilerInstance.h:308
clang::DisableValidationForModuleKind::PCH
@ PCH
Disable validation for a precompiled header and the modules it depends on.
clang::CompilerInstance::getPCHContainerOperations
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
Definition: CompilerInstance.h:524
clang::CompilerInstance::getModuleCache
InMemoryModuleCache & getModuleCache() const
Definition: CompilerInstance.h:819
ParseAST.h
clang::Preprocessor::getBuiltinInfo
Builtin::Context & getBuiltinInfo()
Definition: Preprocessor.h:970
clang::ASTDeserializationListener
Definition: ASTDeserializationListener.h:30
TextDiagnosticPrinter.h
clang::ASTReader::OutOfDate
@ OutOfDate
The AST file is out-of-date relative to its input files, and needs to be regenerated.
Definition: ASTReader.h:380
llvm::ArrayRef
Definition: LLVM.h:34
clang::CompilerInstance::getInvocation
CompilerInvocation & getInvocation()
Definition: CompilerInstance.h:231
clang::ASTReader::ConfigurationMismatch
@ ConfigurationMismatch
The AST file was writtten with a different language/target configuration.
Definition: ASTReader.h:387
createASTReader
static ASTReader * createASTReader(CompilerInstance &CI, StringRef pchFile, SmallVectorImpl< std::unique_ptr< llvm::MemoryBuffer >> &MemBufs, SmallVectorImpl< std::string > &bufNames, ASTDeserializationListener *deserialListener=nullptr)
Definition: ChainedIncludesSource.cpp:80
ASTUnit.h
clang::DiagnosticIDs
Used for handling and querying diagnostic IDs.
Definition: DiagnosticIDs.h:166
clang::ASTReader
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:342
std
Definition: Format.h:4034
clang::CompilerInstance::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts()
Definition: CompilerInstance.h:270
clang::createChainedIncludesSource
IntrusiveRefCntPtr< ExternalSemaSource > createChainedIncludesSource(CompilerInstance &CI, IntrusiveRefCntPtr< ExternalSemaSource > &Reader)
The ChainedIncludesSource class converts headers to chained PCHs in memory, mainly for testing.
Definition: ChainedIncludesSource.cpp:113
clang
Definition: CalledOnceCheck.h:17
ASTReader.h
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:193
clang::ASTReader::VersionMismatch
@ VersionMismatch
The AST file was written by a different version of Clang.
Definition: ASTReader.h:383
clang::TextDiagnosticPrinter
Definition: TextDiagnosticPrinter.h:27
CompilerInstance.h
clang::CompilerInstance::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInstance.h:284
clang::serialization::MK_PCH
@ MK_PCH
File is a PCH file treated as such.
Definition: ModuleFile.h:50
llvm::SmallVectorImpl
Definition: LLVM.h:39
PreprocessorOptions.h
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::ParseAST
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
clang::ExternalASTSource
Abstract interface for external sources of AST nodes.
Definition: ExternalASTSource.h:60
clang::Builtin::Context::initializeBuiltins
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:93
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70
clang::ASTReader::Missing
@ Missing
The AST file was missing.
Definition: ASTReader.h:376