brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.9 KiB · f8be0e4 Raw
145 lines · cpp
1//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- 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-exception6//7//===----------------------------------------------------------------------===//8//9//  This file defines the PCHGenerator, which as a SemaConsumer that generates10//  a PCH file.11//12//===----------------------------------------------------------------------===//13 14#include "clang/AST/ASTContext.h"15#include "clang/Basic/DiagnosticFrontend.h"16#include "clang/Lex/HeaderSearch.h"17#include "clang/Lex/HeaderSearchOptions.h"18#include "clang/Lex/Preprocessor.h"19#include "clang/Sema/SemaConsumer.h"20#include "clang/Serialization/ASTWriter.h"21#include "llvm/Bitstream/BitstreamWriter.h"22 23using namespace clang;24 25PCHGenerator::PCHGenerator(26    Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile,27    StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,28    const CodeGenOptions &CodeGenOpts,29    ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,30    bool AllowASTWithErrors, bool IncludeTimestamps,31    bool BuildingImplicitModule, bool ShouldCacheASTInMemory,32    bool GeneratingReducedBMI)33    : PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),34      Buffer(std::move(Buffer)), Stream(this->Buffer->Data),35      Writer(Stream, this->Buffer->Data, ModCache, CodeGenOpts, Extensions,36             IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),37      AllowASTWithErrors(AllowASTWithErrors),38      ShouldCacheASTInMemory(ShouldCacheASTInMemory) {39  this->Buffer->IsComplete = false;40}41 42PCHGenerator::~PCHGenerator() {43}44 45Module *PCHGenerator::getEmittingModule(ASTContext &) {46  Module *M = nullptr;47 48  if (PP.getLangOpts().isCompilingModule()) {49    M = PP.getHeaderSearchInfo().lookupModule(PP.getLangOpts().CurrentModule,50                                              SourceLocation(),51                                              /*AllowSearch*/ false);52    if (!M)53      assert(PP.getDiagnostics().hasErrorOccurred() &&54             "emitting module but current module doesn't exist");55  }56 57  return M;58}59 60DiagnosticsEngine &PCHGenerator::getDiagnostics() const {61  return PP.getDiagnostics();62}63 64void PCHGenerator::InitializeSema(Sema &S) {65  if (!PP.getHeaderSearchInfo()66           .getHeaderSearchOpts()67           .ModulesSerializeOnlyPreprocessor)68    Subject = &S;69}70 71void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {72  // Don't create a PCH if there were fatal failures during module loading.73  if (PP.getModuleLoader().HadFatalFailure)74    return;75 76  bool hasErrors = PP.getDiagnostics().hasErrorOccurred();77  if (hasErrors && !AllowASTWithErrors)78    return;79 80  Module *Module = getEmittingModule(Ctx);81 82  // Errors that do not prevent the PCH from being written should not cause the83  // overall compilation to fail either.84  if (AllowASTWithErrors)85    PP.getDiagnostics().getClient()->clear();86 87  Buffer->Signature = Writer.WriteAST(Subject, OutputFile, Module, isysroot,88                                      ShouldCacheASTInMemory);89 90  Buffer->IsComplete = true;91}92 93ASTMutationListener *PCHGenerator::GetASTMutationListener() {94  return &Writer;95}96 97ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {98  return &Writer;99}100 101void PCHGenerator::anchor() {}102 103CXX20ModulesGenerator::CXX20ModulesGenerator(Preprocessor &PP,104                                             ModuleCache &ModCache,105                                             StringRef OutputFile,106                                             const CodeGenOptions &CodeGenOpts,107                                             bool GeneratingReducedBMI,108                                             bool AllowASTWithErrors)109    : PCHGenerator(110          PP, ModCache, OutputFile, llvm::StringRef(),111          std::make_shared<PCHBuffer>(), CodeGenOpts,112          /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),113          AllowASTWithErrors, /*IncludeTimestamps=*/false,114          /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,115          GeneratingReducedBMI) {}116 117Module *CXX20ModulesGenerator::getEmittingModule(ASTContext &Ctx) {118  Module *M = Ctx.getCurrentNamedModule();119  assert(M && M->isNamedModuleUnit() &&120         "CXX20ModulesGenerator should only be used with C++20 Named modules.");121  return M;122}123 124void CXX20ModulesGenerator::HandleTranslationUnit(ASTContext &Ctx) {125  PCHGenerator::HandleTranslationUnit(Ctx);126 127  if (!isComplete())128    return;129 130  std::error_code EC;131  auto OS = std::make_unique<llvm::raw_fd_ostream>(getOutputFile(), EC);132  if (EC) {133    getDiagnostics().Report(diag::err_fe_unable_to_open_output)134        << getOutputFile() << EC.message() << "\n";135    return;136  }137 138  *OS << getBufferPtr()->Data;139  OS->flush();140}141 142void CXX20ModulesGenerator::anchor() {}143 144void ReducedBMIGenerator::anchor() {}145