Skip to content

Commit ed1cfff

Browse files
committed
[NFC] [C++20] [Modules] [Reduced BMI] Make sure the size of reduced BMI 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.
1 parent 986435c commit ed1cfff

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ class ASTWriter : public ASTDeserializationListener,
846846
/// AST and semantic-analysis consumer that generates a
847847
/// precompiled header from the parsed source code.
848848
class PCHGenerator : public SemaConsumer {
849-
const Preprocessor &PP;
849+
Preprocessor &PP;
850850
std::string OutputFile;
851851
std::string isysroot;
852852
Sema *SemaPtr;
@@ -867,11 +867,12 @@ class PCHGenerator : public SemaConsumer {
867867
DiagnosticsEngine &getDiagnostics() const {
868868
return SemaPtr->getDiagnostics();
869869
}
870+
Preprocessor &getPreprocessor() { return PP; }
870871

871872
virtual Module *getEmittingModule(ASTContext &Ctx);
872873

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

895896
public:
896-
ReducedBMIGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
897+
ReducedBMIGenerator(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
897898
StringRef OutputFile);
898899

899900
void HandleTranslationUnit(ASTContext &Ctx) override;

clang/lib/Frontend/PrecompiledPreamble.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ class PrecompilePreambleAction : public ASTFrontendAction {
290290

291291
class PrecompilePreambleConsumer : public PCHGenerator {
292292
public:
293-
PrecompilePreambleConsumer(PrecompilePreambleAction &Action,
294-
const Preprocessor &PP,
293+
PrecompilePreambleConsumer(PrecompilePreambleAction &Action, Preprocessor &PP,
295294
InMemoryModuleCache &ModuleCache,
296295
StringRef isysroot,
297296
std::shared_ptr<PCHBuffer> Buffer)

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/AST/ASTContext.h"
1515
#include "clang/Frontend/FrontendDiagnostic.h"
1616
#include "clang/Lex/HeaderSearch.h"
17+
#include "clang/Lex/HeaderSearchOptions.h"
1718
#include "clang/Lex/Preprocessor.h"
1819
#include "clang/Sema/SemaConsumer.h"
1920
#include "clang/Serialization/ASTReader.h"
@@ -23,8 +24,8 @@
2324
using namespace clang;
2425

2526
PCHGenerator::PCHGenerator(
26-
const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
27-
StringRef OutputFile, StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
27+
Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile,
28+
StringRef isysroot, std::shared_ptr<PCHBuffer> Buffer,
2829
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
2930
bool AllowASTWithErrors, bool IncludeTimestamps,
3031
bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
@@ -88,7 +89,7 @@ ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
8889
return &Writer;
8990
}
9091

91-
ReducedBMIGenerator::ReducedBMIGenerator(const Preprocessor &PP,
92+
ReducedBMIGenerator::ReducedBMIGenerator(Preprocessor &PP,
9293
InMemoryModuleCache &ModuleCache,
9394
StringRef OutputFile)
9495
: PCHGenerator(
@@ -107,6 +108,18 @@ Module *ReducedBMIGenerator::getEmittingModule(ASTContext &Ctx) {
107108
}
108109

109110
void ReducedBMIGenerator::HandleTranslationUnit(ASTContext &Ctx) {
111+
// We need to do this to make sure the size of reduced BMI not to be larger
112+
// than full BMI.
113+
//
114+
// FIMXE: We'd better to wrap such options to a new class ASTWriterOptions
115+
// since this is not about searching header really.
116+
// FIXME2: We'd better to move the class writing full BMI with reduced BMI.
117+
HeaderSearchOptions &HSOpts =
118+
getPreprocessor().getHeaderSearchInfo().getHeaderSearchOpts();
119+
HSOpts.ModulesSkipDiagnosticOptions = true;
120+
HSOpts.ModulesSkipHeaderSearchPaths = true;
121+
HSOpts.ModulesSkipPragmaDiagnosticMappings = true;
122+
110123
PCHGenerator::HandleTranslationUnit(Ctx);
111124

112125
if (!isComplete())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Ensure that the size of the reduced BMI is not larger than the full BMI
2+
// in the most simple case.
3+
4+
// This test requires linux commands.
5+
// REQUIRES: system-linux
6+
7+
// RUN: rm -fr %t
8+
// RUN: mkdir %t
9+
//
10+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %s -o %t/a.pcm
11+
// RUN: %clang_cc1 -std=c++20 -emit-reduced-module-interface %s -o %t/a.reduced.pcm
12+
//
13+
// %s implies the current source file. So we can't use it directly.
14+
// RUN: [ $(stat -c%\s "%t/a.pcm") -le $(stat -c%\s "%t/a.reduced.pcm") ]
15+
16+
export module a;

0 commit comments

Comments
 (0)