Skip to content

Commit 0a9106e

Browse files
jansvoboda11Groverkss
authored andcommitted
[clang][serialization] Pass ASTContext explicitly (llvm#115235)
This patch removes `ASTWriter::Context` and starts passing `ASTContext &` explicitly to functions that actually need it. This is a non-functional change with the end-goal of being able to write lightweight PCM files with no `ASTContext` at all.
1 parent aec15a2 commit 0a9106e

File tree

5 files changed

+111
-112
lines changed

5 files changed

+111
-112
lines changed

clang/include/clang/Serialization/ASTRecordWriter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ class ASTRecordWriter
6060

6161
public:
6262
/// Construct a ASTRecordWriter that uses the default encoding scheme.
63-
ASTRecordWriter(ASTWriter &W, ASTWriter::RecordDataImpl &Record)
64-
: DataStreamBasicWriter(W.getASTContext()), Writer(&W), Record(&Record) {}
63+
ASTRecordWriter(ASTContext &Context, ASTWriter &W,
64+
ASTWriter::RecordDataImpl &Record)
65+
: DataStreamBasicWriter(Context), Writer(&W), Record(&Record) {}
6566

6667
/// Construct a ASTRecordWriter that uses the same encoding scheme as another
6768
/// ASTRecordWriter.
@@ -208,7 +209,7 @@ class ASTRecordWriter
208209

209210
/// Emit a reference to a type.
210211
void AddTypeRef(QualType T) {
211-
return Writer->AddTypeRef(T, *Record);
212+
return Writer->AddTypeRef(getASTContext(), T, *Record);
212213
}
213214
void writeQualType(QualType T) {
214215
AddTypeRef(T);

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ class ASTWriter : public ASTDeserializationListener,
119119
/// The PCM manager which manages memory buffers for pcm files.
120120
InMemoryModuleCache &ModuleCache;
121121

122-
/// The ASTContext we're writing.
123-
ASTContext *Context = nullptr;
124-
125122
/// The preprocessor we're writing.
126123
Preprocessor *PP = nullptr;
127124

@@ -545,7 +542,7 @@ class ASTWriter : public ASTDeserializationListener,
545542
unsigned getSubmoduleID(Module *Mod);
546543

547544
/// Write the given subexpression to the bitstream.
548-
void WriteSubStmt(Stmt *S);
545+
void WriteSubStmt(ASTContext &Context, Stmt *S);
549546

550547
void WriteBlockInfoBlock();
551548
void WriteControlBlock(Preprocessor &PP, StringRef isysroot);
@@ -564,34 +561,36 @@ class ASTWriter : public ASTDeserializationListener,
564561
void WriteHeaderSearch(const HeaderSearch &HS);
565562
void WritePreprocessorDetail(PreprocessingRecord &PPRec,
566563
uint64_t MacroOffsetsBase);
567-
void WriteSubmodules(Module *WritingModule);
564+
void WriteSubmodules(Module *WritingModule, ASTContext &Context);
568565

569566
void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
570567
bool isModule);
571568

572569
unsigned TypeExtQualAbbrev = 0;
573570
void WriteTypeAbbrevs();
574-
void WriteType(QualType T);
571+
void WriteType(ASTContext &Context, QualType T);
575572

576573
bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
577574

578-
void GenerateNameLookupTable(const DeclContext *DC,
575+
void GenerateNameLookupTable(ASTContext &Context, const DeclContext *DC,
579576
llvm::SmallVectorImpl<char> &LookupTable);
580577
uint64_t WriteDeclContextLexicalBlock(ASTContext &Context,
581578
const DeclContext *DC);
582579
uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
583580
void WriteTypeDeclOffsets();
584581
void WriteFileDeclIDsMap();
585-
void WriteComments();
582+
void WriteComments(ASTContext &Context);
586583
void WriteSelectors(Sema &SemaRef);
587584
void WriteReferencedSelectorsPool(Sema &SemaRef);
588585
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
589586
bool IsModule);
590587
void WriteDeclAndTypes(ASTContext &Context);
591588
void PrepareWritingSpecialDecls(Sema &SemaRef);
592589
void WriteSpecialDeclRecords(Sema &SemaRef);
593-
void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
594-
void WriteDeclContextVisibleUpdate(const DeclContext *DC);
590+
void WriteDeclUpdatesBlocks(ASTContext &Context,
591+
RecordDataImpl &OffsetsRecord);
592+
void WriteDeclContextVisibleUpdate(ASTContext &Context,
593+
const DeclContext *DC);
595594
void WriteFPPragmaOptions(const FPOptionsOverride &Opts);
596595
void WriteOpenCLExtensions(Sema &SemaRef);
597596
void WriteCUDAPragmas(Sema &SemaRef);
@@ -653,11 +652,6 @@ class ASTWriter : public ASTDeserializationListener,
653652
bool GeneratingReducedBMI = false);
654653
~ASTWriter() override;
655654

656-
ASTContext &getASTContext() const {
657-
assert(Context && "requested AST context when not writing AST");
658-
return *Context;
659-
}
660-
661655
const LangOptions &getLangOpts() const;
662656

663657
/// Get a timestamp for output into the AST file. The actual timestamp
@@ -723,10 +717,10 @@ class ASTWriter : public ASTDeserializationListener,
723717
uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
724718

725719
/// Emit a reference to a type.
726-
void AddTypeRef(QualType T, RecordDataImpl &Record);
720+
void AddTypeRef(ASTContext &Context, QualType T, RecordDataImpl &Record);
727721

728722
/// Force a type to be emitted and get its ID.
729-
serialization::TypeID GetOrCreateTypeID(QualType T);
723+
serialization::TypeID GetOrCreateTypeID(ASTContext &Context, QualType T);
730724

731725
/// Find the first local declaration of a given local redeclarable
732726
/// decl.

0 commit comments

Comments
 (0)