Skip to content

Commit 0bfc97e

Browse files
committed
[clang][modules] NFCI: Scaffolding for serialization of adjusted SourceManager offsets
This patch is a NFC prep for D136624, where we start adjusting offsets into `SourceManager`. Depends on D137213. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D137214
1 parent f61c135 commit 0bfc97e

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,22 @@ class ASTWriter : public ASTDeserializationListener,
447447
/// User ModuleMaps skipped when writing control block.
448448
std::set<const FileEntry *> SkippedModuleMaps;
449449

450+
/// Returns an adjusted \c FileID, accounting for any non-affecting input
451+
/// files.
452+
FileID getAdjustedFileID(FileID FID) const;
453+
/// Returns an adjusted number of \c FileIDs created within the specified \c
454+
/// FileID, accounting for any non-affecting input files.
455+
unsigned getAdjustedNumCreatedFIDs(FileID FID) const;
456+
/// Returns an adjusted \c SourceLocation, accounting for any non-affecting
457+
/// input files.
458+
SourceLocation getAdjustedLocation(SourceLocation Loc) const;
459+
/// Returns an adjusted \c SourceRange, accounting for any non-affecting input
460+
/// files.
461+
SourceRange getAdjustedRange(SourceRange Range) const;
462+
/// Returns an adjusted \c SourceLocation offset, accounting for any
463+
/// non-affecting input files.
464+
SourceLocation::UIntTy getAdjustedOffset(SourceLocation::UIntTy Offset) const;
465+
450466
/// Retrieve or create a submodule ID for this module.
451467
unsigned getSubmoduleID(Module *Mod);
452468

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
20632063
Record.push_back(Code);
20642064

20652065
// Starting offset of this entry within this module, so skip the dummy.
2066-
Record.push_back(SLoc->getOffset() - 2);
2066+
Record.push_back(getAdjustedOffset(SLoc->getOffset()) - 2);
20672067
if (SLoc->isFile()) {
20682068
const SrcMgr::FileInfo &File = SLoc->getFile();
20692069
const SrcMgr::ContentCache *Content = &File.getContentCache();
@@ -2086,7 +2086,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
20862086
assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
20872087
Record.push_back(InputFileIDs[Content->OrigEntry]);
20882088

2089-
Record.push_back(File.NumCreatedFIDs);
2089+
Record.push_back(getAdjustedNumCreatedFIDs(FID));
20902090

20912091
FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
20922092
if (FDI != FileDeclIDs.end()) {
@@ -2146,7 +2146,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
21462146
SourceLocation::UIntTy NextOffset = SourceMgr.getNextLocalOffset();
21472147
if (I + 1 != N)
21482148
NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
2149-
Record.push_back(NextOffset - SLoc->getOffset() - 1);
2149+
Record.push_back(getAdjustedOffset(NextOffset - SLoc->getOffset()) - 1);
21502150
Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
21512151
}
21522152
}
@@ -2170,7 +2170,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
21702170
{
21712171
RecordData::value_type Record[] = {
21722172
SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2173-
SourceMgr.getNextLocalOffset() - 1 /* skip dummy */,
2173+
getAdjustedOffset(SourceMgr.getNextLocalOffset()) - 1 /* skip dummy */,
21742174
SLocEntryOffsetsBase - SourceManagerBlockOffset};
21752175
Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
21762176
bytes(SLocEntryOffsets));
@@ -2569,7 +2569,7 @@ void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec,
25692569
uint64_t Offset = Stream.GetCurrentBitNo() - MacroOffsetsBase;
25702570
assert((Offset >> 32) == 0 && "Preprocessed entity offset too large");
25712571
PreprocessedEntityOffsets.push_back(
2572-
PPEntityOffset((*E)->getSourceRange(), Offset));
2572+
PPEntityOffset(getAdjustedRange((*E)->getSourceRange()), Offset));
25732573

25742574
if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
25752575
// Record this macro definition's ID.
@@ -3014,7 +3014,7 @@ void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
30143014

30153015
Record.push_back(FileIDAndFile.second.StateTransitions.size());
30163016
for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3017-
Record.push_back(StatePoint.Offset);
3017+
Record.push_back(getAdjustedOffset(StatePoint.Offset));
30183018
AddDiagState(StatePoint.State, false);
30193019
}
30203020
}
@@ -5225,12 +5225,42 @@ void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
52255225
Record.push_back(Raw);
52265226
}
52275227

5228+
FileID ASTWriter::getAdjustedFileID(FileID FID) const {
5229+
// TODO: Actually adjust this.
5230+
return FID;
5231+
}
5232+
5233+
unsigned ASTWriter::getAdjustedNumCreatedFIDs(FileID FID) const {
5234+
// TODO: Actually adjust this.
5235+
return PP->getSourceManager()
5236+
.getLocalSLocEntry(FID.ID)
5237+
.getFile()
5238+
.NumCreatedFIDs;
5239+
}
5240+
5241+
SourceLocation ASTWriter::getAdjustedLocation(SourceLocation Loc) const {
5242+
// TODO: Actually adjust this.
5243+
return Loc;
5244+
}
5245+
5246+
SourceRange ASTWriter::getAdjustedRange(SourceRange Range) const {
5247+
// TODO: Actually adjust this.
5248+
return Range;
5249+
}
5250+
5251+
SourceLocation::UIntTy
5252+
ASTWriter::getAdjustedOffset(SourceLocation::UIntTy Offset) const {
5253+
// TODO: Actually adjust this.
5254+
return Offset;
5255+
}
5256+
52285257
void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
5229-
Record.push_back(FID.getOpaqueValue());
5258+
Record.push_back(getAdjustedFileID(FID).getOpaqueValue());
52305259
}
52315260

52325261
void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
52335262
SourceLocationSequence *Seq) {
5263+
Loc = getAdjustedLocation(Loc);
52345264
Record.push_back(SourceLocationEncoding::encode(Loc, Seq));
52355265
}
52365266

clang/lib/Serialization/ASTWriterDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,11 +2457,12 @@ void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
24572457
SourceLocation Loc = D->getLocation();
24582458
unsigned Index = ID - FirstDeclID;
24592459
if (DeclOffsets.size() == Index)
2460-
DeclOffsets.emplace_back(Loc, Offset, DeclTypesBlockStartOffset);
2460+
DeclOffsets.emplace_back(getAdjustedLocation(Loc), Offset,
2461+
DeclTypesBlockStartOffset);
24612462
else if (DeclOffsets.size() < Index) {
24622463
// FIXME: Can/should this happen?
24632464
DeclOffsets.resize(Index+1);
2464-
DeclOffsets[Index].setLocation(Loc);
2465+
DeclOffsets[Index].setLocation(getAdjustedLocation(Loc));
24652466
DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
24662467
} else {
24672468
llvm_unreachable("declarations should be emitted in ID order");

0 commit comments

Comments
 (0)