[clang] 5d7796e - [NFC] [C++20] [Modules] Refactor ReducedBMIGenerator

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 12 20:24:27 PDT 2024


Author: Chuanqi Xu
Date: 2024-03-13T11:22:32+08:00
New Revision: 5d7796e674224be54c48a8db981f4134845bcc7c

URL: https://github.com/llvm/llvm-project/commit/5d7796e674224be54c48a8db981f4134845bcc7c
DIFF: https://github.com/llvm/llvm-project/commit/5d7796e674224be54c48a8db981f4134845bcc7c.diff

LOG: [NFC] [C++20] [Modules] Refactor ReducedBMIGenerator

Changes:
- Don't lookup the emitting module from HeaderSearch. We will use the
  module from the ASTContext directly.
- Remove some useless arguments. Let's addback in the future if
  required.

Added: 
    

Modified: 
    clang/include/clang/Serialization/ASTWriter.h
    clang/lib/Frontend/FrontendActions.cpp
    clang/lib/Serialization/GeneratePCH.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h
index e5db486a71a490..3ed9803fa3745b 100644
--- a/clang/include/clang/Serialization/ASTWriter.h
+++ b/clang/include/clang/Serialization/ASTWriter.h
@@ -868,6 +868,8 @@ class PCHGenerator : public SemaConsumer {
     return SemaPtr->getDiagnostics();
   }
 
+  virtual Module *getEmittingModule(ASTContext &Ctx);
+
 public:
   PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
                StringRef OutputFile, StringRef isysroot,
@@ -887,10 +889,12 @@ class PCHGenerator : public SemaConsumer {
 };
 
 class ReducedBMIGenerator : public PCHGenerator {
+protected:
+  virtual Module *getEmittingModule(ASTContext &Ctx) override;
+
 public:
   ReducedBMIGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
-                      StringRef OutputFile, std::shared_ptr<PCHBuffer> Buffer,
-                      bool IncludeTimestamps);
+                      StringRef OutputFile);
 
   void HandleTranslationUnit(ASTContext &Ctx) override;
 };

diff  --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp
index 50338bfa670f83..81fcd8d5ae9bd3 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -293,11 +293,9 @@ GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
 std::unique_ptr<ASTConsumer>
 GenerateReducedModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI,
                                                         StringRef InFile) {
-  auto Buffer = std::make_shared<PCHBuffer>();
-  return std::make_unique<ReducedBMIGenerator>(
-      CI.getPreprocessor(), CI.getModuleCache(),
-      CI.getFrontendOpts().OutputFile, Buffer,
-      /*IncludeTimestamps=*/+CI.getFrontendOpts().IncludeTimestamps);
+  return std::make_unique<ReducedBMIGenerator>(CI.getPreprocessor(),
+                                               CI.getModuleCache(),
+                                               CI.getFrontendOpts().OutputFile);
 }
 
 bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) {

diff  --git a/clang/lib/Serialization/GeneratePCH.cpp b/clang/lib/Serialization/GeneratePCH.cpp
index 2b511b2d5a90a2..f54db36d4a0199 100644
--- a/clang/lib/Serialization/GeneratePCH.cpp
+++ b/clang/lib/Serialization/GeneratePCH.cpp
@@ -41,6 +41,21 @@ PCHGenerator::PCHGenerator(
 PCHGenerator::~PCHGenerator() {
 }
 
+Module *PCHGenerator::getEmittingModule(ASTContext &) {
+  Module *M = nullptr;
+
+  if (PP.getLangOpts().isCompilingModule()) {
+    M = PP.getHeaderSearchInfo().lookupModule(PP.getLangOpts().CurrentModule,
+                                              SourceLocation(),
+                                              /*AllowSearch*/ false);
+    if (!M)
+      assert(PP.getDiagnostics().hasErrorOccurred() &&
+             "emitting module but current module doesn't exist");
+  }
+
+  return M;
+}
+
 void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
   // Don't create a PCH if there were fatal failures during module loading.
   if (PP.getModuleLoader().HadFatalFailure)
@@ -50,16 +65,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
   if (hasErrors && !AllowASTWithErrors)
     return;
 
-  Module *Module = nullptr;
-  if (PP.getLangOpts().isCompilingModule()) {
-    Module = PP.getHeaderSearchInfo().lookupModule(
-        PP.getLangOpts().CurrentModule, SourceLocation(),
-        /*AllowSearch*/ false);
-    if (!Module) {
-      assert(hasErrors && "emitting module but current module doesn't exist");
-      return;
-    }
-  }
+  Module *Module = getEmittingModule(Ctx);
 
   // Errors that do not prevent the PCH from being written should not cause the
   // overall compilation to fail either.
@@ -84,16 +90,22 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
 
 ReducedBMIGenerator::ReducedBMIGenerator(const Preprocessor &PP,
                                          InMemoryModuleCache &ModuleCache,
-                                         StringRef OutputFile,
-                                         std::shared_ptr<PCHBuffer> Buffer,
-                                         bool IncludeTimestamps)
+                                         StringRef OutputFile)
     : PCHGenerator(
-          PP, ModuleCache, OutputFile, llvm::StringRef(), Buffer,
+          PP, ModuleCache, OutputFile, llvm::StringRef(),
+          std::make_shared<PCHBuffer>(),
           /*Extensions=*/ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
-          /*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/IncludeTimestamps,
+          /*AllowASTWithErrors*/ false, /*IncludeTimestamps=*/false,
           /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false,
           /*GeneratingReducedBMI=*/true) {}
 
+Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
+  Module *M = Ctx.getCurrentNamedModule();
+  assert(M->isNamedModuleUnit() &&
+         "ReducedBMIGenerator should only be used with C++20 Named modules.");
+  return M;
+}
+
 void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
   PCHGenerator::HandleTranslationUnit(Ctx);
 


        


More information about the cfe-commits mailing list