Skip to content

Commit 7ac4050

Browse files
committed
[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. (cherry picked from commit 53e49f1)
1 parent 44d93ad commit 7ac4050

File tree

5 files changed

+113
-113
lines changed

5 files changed

+113
-113
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: 13 additions & 18 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

@@ -537,10 +534,11 @@ class ASTWriter : public ASTDeserializationListener,
537534
unsigned getSubmoduleID(Module *Mod);
538535

539536
/// Write the given subexpression to the bitstream.
540-
void WriteSubStmt(Stmt *S);
537+
void WriteSubStmt(ASTContext &Context, Stmt *S);
541538

542539
void WriteBlockInfoBlock();
543-
void WriteControlBlock(Preprocessor &PP, StringRef isysroot);
540+
void WriteControlBlock(ASTContext &Context, Preprocessor &PP,
541+
StringRef isysroot);
544542

545543
/// Write out the signature and diagnostic options, and return the signature.
546544
void writeUnhashedControlBlock(Preprocessor &PP);
@@ -556,34 +554,36 @@ class ASTWriter : public ASTDeserializationListener,
556554
void WriteHeaderSearch(const HeaderSearch &HS);
557555
void WritePreprocessorDetail(PreprocessingRecord &PPRec,
558556
uint64_t MacroOffsetsBase);
559-
void WriteSubmodules(Module *WritingModule);
557+
void WriteSubmodules(Module *WritingModule, ASTContext &Context);
560558

561559
void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
562560
bool isModule);
563561

564562
unsigned TypeExtQualAbbrev = 0;
565563
void WriteTypeAbbrevs();
566-
void WriteType(QualType T);
564+
void WriteType(ASTContext &Context, QualType T);
567565

568566
bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
569567

570-
void GenerateNameLookupTable(const DeclContext *DC,
568+
void GenerateNameLookupTable(ASTContext &Context, const DeclContext *DC,
571569
llvm::SmallVectorImpl<char> &LookupTable);
572570
uint64_t WriteDeclContextLexicalBlock(ASTContext &Context,
573571
const DeclContext *DC);
574572
uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
575573
void WriteTypeDeclOffsets();
576574
void WriteFileDeclIDsMap();
577-
void WriteComments();
575+
void WriteComments(ASTContext &Context);
578576
void WriteSelectors(Sema &SemaRef);
579577
void WriteReferencedSelectorsPool(Sema &SemaRef);
580578
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
581579
bool IsModule);
582580
void WriteDeclAndTypes(ASTContext &Context);
583581
void PrepareWritingSpecialDecls(Sema &SemaRef);
584582
void WriteSpecialDeclRecords(Sema &SemaRef);
585-
void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
586-
void WriteDeclContextVisibleUpdate(const DeclContext *DC);
583+
void WriteDeclUpdatesBlocks(ASTContext &Context,
584+
RecordDataImpl &OffsetsRecord);
585+
void WriteDeclContextVisibleUpdate(ASTContext &Context,
586+
const DeclContext *DC);
587587
void WriteFPPragmaOptions(const FPOptionsOverride &Opts);
588588
void WriteOpenCLExtensions(Sema &SemaRef);
589589
void WriteCUDAPragmas(Sema &SemaRef);
@@ -645,11 +645,6 @@ class ASTWriter : public ASTDeserializationListener,
645645
bool GeneratingReducedBMI = false);
646646
~ASTWriter() override;
647647

648-
ASTContext &getASTContext() const {
649-
assert(Context && "requested AST context when not writing AST");
650-
return *Context;
651-
}
652-
653648
const LangOptions &getLangOpts() const;
654649

655650
/// Get a timestamp for output into the AST file. The actual timestamp
@@ -715,10 +710,10 @@ class ASTWriter : public ASTDeserializationListener,
715710
uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
716711

717712
/// Emit a reference to a type.
718-
void AddTypeRef(QualType T, RecordDataImpl &Record);
713+
void AddTypeRef(ASTContext &Context, QualType T, RecordDataImpl &Record);
719714

720715
/// Force a type to be emitted and get its ID.
721-
serialization::TypeID GetOrCreateTypeID(QualType T);
716+
serialization::TypeID GetOrCreateTypeID(ASTContext &Context, QualType T);
722717

723718
/// Find the first local declaration of a given local redeclarable
724719
/// decl.

0 commit comments

Comments
 (0)