Skip to content

Commit dff2735

Browse files
committed
[clang][deps] Only write preprocessor info into PCMs
1 parent 5e66ce9 commit dff2735

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

clang/include/clang/Lex/HeaderSearchOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ class HeaderSearchOptions {
255255
LLVM_PREFERRED_TYPE(bool)
256256
unsigned ModulesHashContent : 1;
257257

258+
/// Whether AST files should only contain the preprocessor information.
259+
LLVM_PREFERRED_TYPE(bool)
260+
unsigned ModulesSerializeOnlyPreprocessor : 1;
261+
258262
/// Whether we should include all things that could impact the module in the
259263
/// hash.
260264
///
@@ -288,6 +292,7 @@ class HeaderSearchOptions {
288292
ModulesSkipHeaderSearchPaths(false),
289293
ModulesSkipPragmaDiagnosticMappings(false),
290294
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
295+
ModulesSerializeOnlyPreprocessor(false),
291296
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false),
292297
AllowModuleMapSubdirectorySearch(true) {}
293298

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,9 @@ class PCHGenerator : public SemaConsumer {
923923
void anchor() override;
924924

925925
Preprocessor &PP;
926+
llvm::PointerUnion<Sema *, Preprocessor *> Subject;
926927
std::string OutputFile;
927928
std::string isysroot;
928-
Sema *SemaPtr;
929929
std::shared_ptr<PCHBuffer> Buffer;
930930
llvm::BitstreamWriter Stream;
931931
ASTWriter Writer;
@@ -940,9 +940,7 @@ class PCHGenerator : public SemaConsumer {
940940
bool isComplete() const { return Buffer->IsComplete; }
941941
PCHBuffer *getBufferPtr() { return Buffer.get(); }
942942
StringRef getOutputFile() const { return OutputFile; }
943-
DiagnosticsEngine &getDiagnostics() const {
944-
return SemaPtr->getDiagnostics();
945-
}
943+
DiagnosticsEngine &getDiagnostics() const;
946944
Preprocessor &getPreprocessor() { return PP; }
947945

948946
virtual Module *getEmittingModule(ASTContext &Ctx);
@@ -958,7 +956,7 @@ class PCHGenerator : public SemaConsumer {
958956
bool GeneratingReducedBMI = false);
959957
~PCHGenerator() override;
960958

961-
void InitializeSema(Sema &S) override { SemaPtr = &S; }
959+
void InitializeSema(Sema &S) override;
962960
void HandleTranslationUnit(ASTContext &Ctx) override;
963961
void HandleVTable(CXXRecordDecl *RD) override { Writer.handleVTable(RD); }
964962
ASTMutationListener *GetASTMutationListener() override;

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ PCHGenerator::PCHGenerator(
2929
bool AllowASTWithErrors, bool IncludeTimestamps,
3030
bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
3131
bool GeneratingReducedBMI)
32-
: PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33-
SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
32+
: PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33+
Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
3434
Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
3535
IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
3636
AllowASTWithErrors(AllowASTWithErrors),
@@ -56,6 +56,17 @@ Module *PCHGenerator::getEmittingModule(ASTContext &) {
5656
return M;
5757
}
5858

59+
DiagnosticsEngine &PCHGenerator::getDiagnostics() const {
60+
return PP.getDiagnostics();
61+
}
62+
63+
void PCHGenerator::InitializeSema(Sema &S) {
64+
if (!PP.getHeaderSearchInfo()
65+
.getHeaderSearchOpts()
66+
.ModulesSerializeOnlyPreprocessor)
67+
Subject = &S;
68+
}
69+
5970
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
6071
// Don't create a PCH if there were fatal failures during module loading.
6172
if (PP.getModuleLoader().HadFatalFailure)
@@ -72,9 +83,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
7283
if (AllowASTWithErrors)
7384
PP.getDiagnostics().getClient()->clear();
7485

75-
// Emit the PCH file to the Buffer.
76-
assert(SemaPtr && "No Sema?");
77-
Buffer->Signature = Writer.WriteAST(SemaPtr, OutputFile, Module, isysroot,
86+
Buffer->Signature = Writer.WriteAST(Subject, OutputFile, Module, isysroot,
7887
ShouldCacheASTInMemory);
7988

8089
Buffer->IsComplete = true;

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ class DependencyScanningAction : public tooling::ToolAction {
420420
// TODO: Implement diagnostic bucketing to reduce the impact of strict
421421
// context hashing.
422422
ScanInstance.getHeaderSearchOpts().ModulesStrictContextHash = true;
423+
ScanInstance.getHeaderSearchOpts().ModulesSerializeOnlyPreprocessor = true;
423424
ScanInstance.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
424425
ScanInstance.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
425426
ScanInstance.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings =

0 commit comments

Comments
 (0)