Skip to content

Commit a75b3e9

Browse files
committed
[NFC] [Serialization] Extract logics to write decls and types into a standalone function
This patch extract logics in ASTWriter::WriteASTCore about writing decls and types into a standalone function. The WriteASTCore function is pretty long and hard to read. It should be helpful for readability to extract the common logics into a standalone function. This is also helpful for further changes e.g., removing unreachable declarations.
1 parent 7edddee commit a75b3e9

File tree

2 files changed

+68
-67
lines changed

2 files changed

+68
-67
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ class ASTWriter : public ASTDeserializationListener,
542542
void WriteReferencedSelectorsPool(Sema &SemaRef);
543543
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
544544
bool IsModule);
545+
void WriteDeclAndTypes(ASTContext &Context);
545546
void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
546547
void WriteDeclContextVisibleUpdate(const DeclContext *DC);
547548
void WriteFPPragmaOptions(const FPOptionsOverride &Opts);

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5107,69 +5107,7 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
51075107
for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
51085108
DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));
51095109

5110-
{
5111-
auto Abv = std::make_shared<BitCodeAbbrev>();
5112-
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
5113-
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5114-
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5115-
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
5116-
}
5117-
5118-
RecordData DeclUpdatesOffsetsRecord;
5119-
5120-
// Keep writing types, declarations, and declaration update records
5121-
// until we've emitted all of them.
5122-
Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5123-
DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5124-
WriteTypeAbbrevs();
5125-
WriteDeclAbbrevs();
5126-
do {
5127-
WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5128-
while (!DeclTypesToEmit.empty()) {
5129-
DeclOrType DOT = DeclTypesToEmit.front();
5130-
DeclTypesToEmit.pop();
5131-
if (DOT.isType())
5132-
WriteType(DOT.getType());
5133-
else
5134-
WriteDecl(Context, DOT.getDecl());
5135-
}
5136-
} while (!DeclUpdates.empty());
5137-
Stream.ExitBlock();
5138-
5139-
DoneWritingDeclsAndTypes = true;
5140-
5141-
// These things can only be done once we've written out decls and types.
5142-
WriteTypeDeclOffsets();
5143-
if (!DeclUpdatesOffsetsRecord.empty())
5144-
Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5145-
5146-
// Create a lexical update block containing all of the declarations in the
5147-
// translation unit that do not come from other AST files.
5148-
{
5149-
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
5150-
for (const auto *D : TU->noload_decls()) {
5151-
if (!D->isFromASTFile()) {
5152-
NewGlobalKindDeclPairs.push_back(D->getKind());
5153-
NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
5154-
}
5155-
}
5156-
5157-
auto Abv = std::make_shared<BitCodeAbbrev>();
5158-
Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
5159-
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5160-
unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
5161-
5162-
RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
5163-
Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
5164-
bytes(NewGlobalKindDeclPairs));
5165-
}
5166-
5167-
// And a visible updates block for the translation unit.
5168-
WriteDeclContextVisibleUpdate(TU);
5169-
5170-
// If we have any extern "C" names, write out a visible update for them.
5171-
if (Context.ExternCContext)
5172-
WriteDeclContextVisibleUpdate(Context.ExternCContext);
5110+
WriteDeclAndTypes(Context);
51735111

51745112
WriteFileDeclIDsMap();
51755113
WriteSourceManagerBlock(Context.getSourceManager(), PP);
@@ -5255,10 +5193,6 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
52555193
if (!DeleteExprsToAnalyze.empty())
52565194
Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
52575195

5258-
// Write the visible updates to DeclContexts.
5259-
for (auto *DC : UpdatedDeclContexts)
5260-
WriteDeclContextVisibleUpdate(DC);
5261-
52625196
if (!WritingModule) {
52635197
// Write the submodules that were imported, if any.
52645198
struct ModuleInfo {
@@ -5323,6 +5257,72 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
53235257
return backpatchSignature();
53245258
}
53255259

5260+
void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
5261+
// Keep writing types, declarations, and declaration update records
5262+
// until we've emitted all of them.
5263+
RecordData DeclUpdatesOffsetsRecord;
5264+
Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
5265+
DeclTypesBlockStartOffset = Stream.GetCurrentBitNo();
5266+
WriteTypeAbbrevs();
5267+
WriteDeclAbbrevs();
5268+
do {
5269+
WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
5270+
while (!DeclTypesToEmit.empty()) {
5271+
DeclOrType DOT = DeclTypesToEmit.front();
5272+
DeclTypesToEmit.pop();
5273+
if (DOT.isType())
5274+
WriteType(DOT.getType());
5275+
else
5276+
WriteDecl(Context, DOT.getDecl());
5277+
}
5278+
} while (!DeclUpdates.empty());
5279+
Stream.ExitBlock();
5280+
5281+
DoneWritingDeclsAndTypes = true;
5282+
5283+
// These things can only be done once we've written out decls and types.
5284+
WriteTypeDeclOffsets();
5285+
if (!DeclUpdatesOffsetsRecord.empty())
5286+
Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5287+
5288+
const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
5289+
// Create a lexical update block containing all of the declarations in the
5290+
// translation unit that do not come from other AST files.
5291+
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
5292+
for (const auto *D : TU->noload_decls()) {
5293+
if (!D->isFromASTFile()) {
5294+
NewGlobalKindDeclPairs.push_back(D->getKind());
5295+
NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
5296+
}
5297+
}
5298+
5299+
auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
5300+
Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
5301+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5302+
unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
5303+
5304+
RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
5305+
Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
5306+
bytes(NewGlobalKindDeclPairs));
5307+
5308+
Abv = std::make_shared<llvm::BitCodeAbbrev>();
5309+
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
5310+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5311+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5312+
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
5313+
5314+
// And a visible updates block for the translation unit.
5315+
WriteDeclContextVisibleUpdate(TU);
5316+
5317+
// If we have any extern "C" names, write out a visible update for them.
5318+
if (Context.ExternCContext)
5319+
WriteDeclContextVisibleUpdate(Context.ExternCContext);
5320+
5321+
// Write the visible updates to DeclContexts.
5322+
for (auto *DC : UpdatedDeclContexts)
5323+
WriteDeclContextVisibleUpdate(DC);
5324+
}
5325+
53265326
void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
53275327
if (DeclUpdates.empty())
53285328
return;

0 commit comments

Comments
 (0)