Skip to content

Commit 50227b7

Browse files
committed
[clang][serialization] Enable ASTWriter to work with Preprocessor only (llvm#115237)
This PR builds on top of llvm#115235 and makes it possible to call `ASTWriter::WriteAST()` with `Preprocessor` only instead of full `Sema` object. So far, there are no clients that leverage the new capability - that will come in a follow-up commit. (cherry picked from commit a2f9d1d)
1 parent 7ac4050 commit 50227b7

File tree

9 files changed

+145
-119
lines changed

9 files changed

+145
-119
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
476476
llvm::StringMap<const Module *> PrimaryModuleNameMap;
477477
llvm::DenseMap<const Module *, const Module *> SameModuleLookupSet;
478478

479-
/// The include tree that is being built, if any.
480-
/// See \c FrontendOptions::CASIncludeTreeID.
481-
std::optional<std::string> CASIncludeTreeID;
482-
483-
/// The cas-fs tree that is being built, if any.
484-
/// See \c FileSystemOptions::CASFileSystemRootID.
485-
std::optional<std::string> CASFileSystemRootID;
486-
487479
static constexpr unsigned ConstantArrayTypesLog2InitSize = 8;
488480
static constexpr unsigned GeneralTypesLog2InitSize = 9;
489481
static constexpr unsigned FunctionProtoTypesLog2InitSize = 12;
@@ -1123,20 +1115,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
11231115
/// a module fragment or a module unit but not a C++20 module.
11241116
bool isInSameModule(const Module *M1, const Module *M2);
11251117

1126-
std::optional<std::string> getCASIncludeTreeID() const {
1127-
return CASIncludeTreeID;
1128-
}
1129-
void setCASIncludeTreeID(std::string ID) {
1130-
CASIncludeTreeID = std::move(ID);
1131-
}
1132-
1133-
std::optional<std::string> getCASFileSystemRootID() const {
1134-
return CASFileSystemRootID;
1135-
}
1136-
void setCASFileSystemRootID(std::string ID) {
1137-
CASFileSystemRootID = std::move(ID);
1138-
}
1139-
11401118
TranslationUnitDecl *getTranslationUnitDecl() const {
11411119
return TUDecl->getMostRecentDecl();
11421120
}

clang/include/clang/Lex/Preprocessor.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,28 @@ class Preprocessor {
24282428
!IsAtImport;
24292429
}
24302430

2431+
private:
2432+
/// The include tree that is being built, if any.
2433+
/// See \c FrontendOptions::CASIncludeTreeID.
2434+
std::optional<std::string> CASIncludeTreeID;
2435+
2436+
/// The cas-fs tree that is being built, if any.
2437+
/// See \c FileSystemOptions::CASFileSystemRootID.
2438+
std::optional<std::string> CASFileSystemRootID;
2439+
2440+
public:
2441+
std::optional<std::string> getCASIncludeTreeID() const {
2442+
return CASIncludeTreeID;
2443+
}
2444+
void setCASIncludeTreeID(std::string ID) { CASIncludeTreeID = std::move(ID); }
2445+
2446+
std::optional<std::string> getCASFileSystemRootID() const {
2447+
return CASFileSystemRootID;
2448+
}
2449+
void setCASFileSystemRootID(std::string ID) {
2450+
CASFileSystemRootID = std::move(ID);
2451+
}
2452+
24312453
/// Allocate a new MacroInfo object with the provided SourceLocation.
24322454
MacroInfo *AllocateMacroInfo(SourceLocation L);
24332455

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,7 @@ class ASTWriter : public ASTDeserializationListener,
537537
void WriteSubStmt(ASTContext &Context, Stmt *S);
538538

539539
void WriteBlockInfoBlock();
540-
void WriteControlBlock(ASTContext &Context, Preprocessor &PP,
541-
StringRef isysroot);
540+
void WriteControlBlock(Preprocessor &PP, StringRef isysroot);
542541

543542
/// Write out the signature and diagnostic options, and return the signature.
544543
void writeUnhashedControlBlock(Preprocessor &PP);
@@ -554,7 +553,7 @@ class ASTWriter : public ASTDeserializationListener,
554553
void WriteHeaderSearch(const HeaderSearch &HS);
555554
void WritePreprocessorDetail(PreprocessingRecord &PPRec,
556555
uint64_t MacroOffsetsBase);
557-
void WriteSubmodules(Module *WritingModule, ASTContext &Context);
556+
void WriteSubmodules(Module *WritingModule, ASTContext *Context);
558557

559558
void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
560559
bool isModule);
@@ -575,7 +574,7 @@ class ASTWriter : public ASTDeserializationListener,
575574
void WriteComments(ASTContext &Context);
576575
void WriteSelectors(Sema &SemaRef);
577576
void WriteReferencedSelectorsPool(Sema &SemaRef);
578-
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
577+
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver *IdResolver,
579578
bool IsModule);
580579
void WriteDeclAndTypes(ASTContext &Context);
581580
void PrepareWritingSpecialDecls(Sema &SemaRef);
@@ -632,7 +631,7 @@ class ASTWriter : public ASTDeserializationListener,
632631
void WriteDeclAbbrevs();
633632
void WriteDecl(ASTContext &Context, Decl *D);
634633

635-
ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
634+
ASTFileSignature WriteASTCore(Sema *SemaPtr, StringRef isysroot,
636635
Module *WritingModule);
637636

638637
public:
@@ -652,10 +651,13 @@ class ASTWriter : public ASTDeserializationListener,
652651
/// include timestamps in the output file.
653652
time_t getTimestampForOutput(const FileEntry *E) const;
654653

655-
/// Write a precompiled header for the given semantic analysis.
654+
/// Write a precompiled header or a module with the AST produced by the
655+
/// \c Sema object, or a dependency scanner module with the preprocessor state
656+
/// produced by the \c Preprocessor object.
656657
///
657-
/// \param SemaRef a reference to the semantic analysis object that processed
658-
/// the AST to be written into the precompiled header.
658+
/// \param Subject The \c Sema object that processed the AST to be written, or
659+
/// in the case of a dependency scanner module the \c Preprocessor that holds
660+
/// the state.
659661
///
660662
/// \param WritingModule The module that we are writing. If null, we are
661663
/// writing a precompiled header.
@@ -666,8 +668,9 @@ class ASTWriter : public ASTDeserializationListener,
666668
///
667669
/// \return the module signature, which eventually will be a hash of
668670
/// the module but currently is merely a random 32-bit number.
669-
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
670-
Module *WritingModule, StringRef isysroot,
671+
ASTFileSignature WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
672+
StringRef OutputFile, Module *WritingModule,
673+
StringRef isysroot,
671674
bool ShouldCacheASTInMemory = false);
672675

673676
/// Emit a token.

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ bool ASTUnit::Save(StringRef File) {
23582358

23592359
static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl<char> &Buffer,
23602360
Sema &S, raw_ostream &OS) {
2361-
Writer.WriteAST(S, std::string(), nullptr, "");
2361+
Writer.WriteAST(&S, std::string(), nullptr, "");
23622362

23632363
// Write the generated bitstream to "Out".
23642364
if (!Buffer.empty())

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
12081208
}
12091209

12101210
if (!CI.getFrontendOpts().CASIncludeTreeID.empty())
1211-
CI.getASTContext().setCASIncludeTreeID(
1211+
CI.getPreprocessor().setCASIncludeTreeID(
12121212
CI.getFrontendOpts().CASIncludeTreeID);
12131213

12141214
CI.setASTConsumer(std::move(Consumer));

0 commit comments

Comments
 (0)