Skip to content

[clang][deps] Only write preprocessor info into PCMs #115239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/Lex/HeaderSearchOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ class HeaderSearchOptions {
LLVM_PREFERRED_TYPE(bool)
unsigned ModulesHashContent : 1;

/// Whether AST files should only contain the preprocessor information.
LLVM_PREFERRED_TYPE(bool)
unsigned ModulesSerializeOnlyPreprocessor : 1;

/// Whether we should include all things that could impact the module in the
/// hash.
///
Expand Down Expand Up @@ -288,6 +292,7 @@ class HeaderSearchOptions {
ModulesSkipHeaderSearchPaths(false),
ModulesSkipPragmaDiagnosticMappings(false),
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
ModulesSerializeOnlyPreprocessor(false),
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false),
AllowModuleMapSubdirectorySearch(true) {}

Expand Down
8 changes: 3 additions & 5 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,9 @@ class PCHGenerator : public SemaConsumer {
void anchor() override;

Preprocessor &PP;
llvm::PointerUnion<Sema *, Preprocessor *> Subject;
std::string OutputFile;
std::string isysroot;
Sema *SemaPtr;
std::shared_ptr<PCHBuffer> Buffer;
llvm::BitstreamWriter Stream;
ASTWriter Writer;
Expand All @@ -946,9 +946,7 @@ class PCHGenerator : public SemaConsumer {
bool isComplete() const { return Buffer->IsComplete; }
PCHBuffer *getBufferPtr() { return Buffer.get(); }
StringRef getOutputFile() const { return OutputFile; }
DiagnosticsEngine &getDiagnostics() const {
return SemaPtr->getDiagnostics();
}
DiagnosticsEngine &getDiagnostics() const;
Preprocessor &getPreprocessor() { return PP; }

virtual Module *getEmittingModule(ASTContext &Ctx);
Expand All @@ -964,7 +962,7 @@ class PCHGenerator : public SemaConsumer {
bool GeneratingReducedBMI = false);
~PCHGenerator() override;

void InitializeSema(Sema &S) override { SemaPtr = &S; }
void InitializeSema(Sema &S) override;
void HandleTranslationUnit(ASTContext &Ctx) override;
void HandleVTable(CXXRecordDecl *RD) override { Writer.handleVTable(RD); }
ASTMutationListener *GetASTMutationListener() override;
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3522,6 +3522,9 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
break;
}

if (Record.empty())
break;

if (SpecialTypes.size() != Record.size())
return llvm::createStringError(std::errc::illegal_byte_sequence,
"invalid special-types record");
Expand Down
19 changes: 14 additions & 5 deletions clang/lib/Serialization/GeneratePCH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ PCHGenerator::PCHGenerator(
bool AllowASTWithErrors, bool IncludeTimestamps,
bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
bool GeneratingReducedBMI)
: PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
: PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
AllowASTWithErrors(AllowASTWithErrors),
Expand All @@ -56,6 +56,17 @@ Module *PCHGenerator::getEmittingModule(ASTContext &) {
return M;
}

DiagnosticsEngine &PCHGenerator::getDiagnostics() const {
return PP.getDiagnostics();
}

void PCHGenerator::InitializeSema(Sema &S) {
if (!PP.getHeaderSearchInfo()
.getHeaderSearchOpts()
.ModulesSerializeOnlyPreprocessor)
Subject = &S;
}

void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
// Don't create a PCH if there were fatal failures during module loading.
if (PP.getModuleLoader().HadFatalFailure)
Expand All @@ -72,9 +83,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
if (AllowASTWithErrors)
PP.getDiagnostics().getClient()->clear();

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

Buffer->IsComplete = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ class DependencyScanningAction : public tooling::ToolAction {
// TODO: Implement diagnostic bucketing to reduce the impact of strict
// context hashing.
ScanInstance.getHeaderSearchOpts().ModulesStrictContextHash = true;
ScanInstance.getHeaderSearchOpts().ModulesSerializeOnlyPreprocessor = true;
ScanInstance.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
ScanInstance.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
ScanInstance.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings =
Expand Down
Loading