Skip to content

Commit

Permalink
[NFC] [C++20] [Modules] [Reduced BMI] Make sure the size of reduced B…
Browse files Browse the repository at this point in the history
…MI is not large than full BMI

Before this patch, the size of the reduced BMI may be large than the
full BMI when the source codes is pretty small. This violates the design
principles. The root cause is an oversight that we skipped something
in full BMI but forgot to make it in reduced BMI.
  • Loading branch information
ChuanqiXu9 committed Apr 3, 2024
1 parent 986435c commit ed1cfff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
7 changes: 4 additions & 3 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ class ASTWriter : public ASTDeserializationListener,
/// AST and semantic-analysis consumer that generates a
/// precompiled header from the parsed source code.
class PCHGenerator : public SemaConsumer {
const Preprocessor &PP;
Preprocessor &PP;
std::string OutputFile;
std::string isysroot;
Sema *SemaPtr;
Expand All @@ -867,11 +867,12 @@ class PCHGenerator : public SemaConsumer {
DiagnosticsEngine &getDiagnostics() const {
return SemaPtr->getDiagnostics();
}
Preprocessor &getPreprocessor() { return PP; }

virtual Module *getEmittingModule(ASTContext &Ctx);

public:
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
PCHGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, StringRef isysroot,
std::shared_ptr<PCHBuffer> Buffer,
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
Expand All @@ -893,7 +894,7 @@ class ReducedBMIGenerator : public PCHGenerator {
virtual Module *getEmittingModule(ASTContext &Ctx) override;

public:
ReducedBMIGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile);

void HandleTranslationUnit(ASTContext &Ctx) override;
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Frontend/PrecompiledPreamble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ class PrecompilePreambleAction : public ASTFrontendAction {

class PrecompilePreambleConsumer : public PCHGenerator {
public:
PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
const Preprocessor &PP,
PrecompilePreambleConsumer(PrecompilePreambleAction &Action, Preprocessor &PP,
InMemoryModuleCache &ModuleCache,
StringRef isysroot,
std::shared_ptr<PCHBuffer> Buffer)
Expand Down
19 changes: 16 additions & 3 deletions clang/lib/Serialization/GeneratePCH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/SemaConsumer.h"
#include "clang/Serialization/ASTReader.h"
Expand All @@ -23,8 +24,8 @@
using namespace clang;

PCHGenerator::PCHGenerator(
const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile,
StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
bool AllowASTWithErrors, bool IncludeTimestamps,
bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
Expand Down Expand Up @@ -88,7 +89,7 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
return &Writer;
}

ReducedBMIGenerator::ReducedBMIGenerator(const Preprocessor &PP,
ReducedBMIGenerator::ReducedBMIGenerator(Preprocessor &PP,
InMemoryModuleCache &ModuleCache,
StringRef OutputFile)
: PCHGenerator(
Expand All @@ -107,6 +108,18 @@ Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
}

void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
// We need to do this to make sure the size of reduced BMI not to be larger
// than full BMI.
//
// FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
// since this is not about searching header really.
// FIXME2: We'd better to move the class writing full BMI with reduced BMI.
HeaderSearchOptions &HSOpts =
getPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
HSOpts.ModulesSkipDiagnosticOptions = true;
HSOpts.ModulesSkipHeaderSearchPaths = true;
HSOpts.ModulesSkipPragmaDiagnosticMappings = true;

PCHGenerator::HandleTranslationUnit(Ctx);

if (!isComplete())
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Modules/reduced-bmi-size.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Ensure that the size of the reduced BMI is not larger than the full BMI
// in the most simple case.

// This test requires linux commands.
// REQUIRES: system-linux

// RUN: rm -fr %t
// RUN: mkdir %t
//
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %s -o %t/a.pcm
// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %s -o %t/a.reduced.pcm
//
// %s implies the current source file. So we can't use it directly.
// RUN: [ $(stat -c%\s "%t/a.pcm") -le $(stat -c%\s "%t/a.reduced.pcm") ]

export module a;

0 comments on commit ed1cfff

Please sign in to comment.