Skip to content

Commit b9d78bd

Browse files
committed
[clang][modules] Use relative offsets for input files
This patch replaces absolute offsets into the input files block with offsets relative to the block start. This makes the whole section "relocatable". I confirmed all other uses of `GetCurrentBitNo()` are turned into relative offsets before being serialized into the AST file. Reviewed By: benlangmuir Differential Revision: https://reviews.llvm.org/D158572
1 parent 41db543 commit b9d78bd

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ class ModuleFile {
245245
/// The cursor to the start of the input-files block.
246246
llvm::BitstreamCursor InputFilesCursor;
247247

248-
/// Offsets for all of the input file entries in the AST file.
248+
/// Absolute offset of the start of the input-files block.
249+
uint64_t InputFilesOffsetBase = 0;
250+
251+
/// Relative offsets for all of the input file entries in the AST file.
249252
const llvm::support::unaligned_uint64_t *InputFileOffsets = nullptr;
250253

251254
/// The input files that have been loaded from this AST file.

clang/lib/Serialization/ASTReader.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,7 +2326,8 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
23262326
// Go find this input file.
23272327
BitstreamCursor &Cursor = F.InputFilesCursor;
23282328
SavedStreamPosition SavedPosition(Cursor);
2329-
if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2329+
if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2330+
F.InputFileOffsets[ID - 1])) {
23302331
// FIXME this drops errors on the floor.
23312332
consumeError(std::move(Err));
23322333
}
@@ -2410,7 +2411,8 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
24102411
// Go find this input file.
24112412
BitstreamCursor &Cursor = F.InputFilesCursor;
24122413
SavedStreamPosition SavedPosition(Cursor);
2413-
if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2414+
if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +
2415+
F.InputFileOffsets[ID - 1])) {
24142416
// FIXME this drops errors on the floor.
24152417
consumeError(std::move(Err));
24162418
}
@@ -2788,6 +2790,7 @@ ASTReader::ReadControlBlock(ModuleFile &F,
27882790
Error("malformed block record in AST file");
27892791
return Failure;
27902792
}
2793+
F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();
27912794
continue;
27922795

27932796
case OPTIONS_BLOCK_ID:
@@ -5328,6 +5331,7 @@ bool ASTReader::readASTFileControlBlock(
53285331
bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
53295332
bool NeedsImports = Listener.needsImportVisitation();
53305333
BitstreamCursor InputFilesCursor;
5334+
uint64_t InputFilesOffsetBase = 0;
53315335

53325336
RecordData Record;
53335337
std::string ModuleDir;
@@ -5363,6 +5367,7 @@ bool ASTReader::readASTFileControlBlock(
53635367
if (NeedsInputFiles &&
53645368
ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
53655369
return true;
5370+
InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();
53665371
break;
53675372

53685373
default:
@@ -5435,7 +5440,8 @@ bool ASTReader::readASTFileControlBlock(
54355440

54365441
BitstreamCursor &Cursor = InputFilesCursor;
54375442
SavedStreamPosition SavedPosition(Cursor);
5438-
if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5443+
if (llvm::Error Err =
5444+
Cursor.JumpToBit(InputFilesOffsetBase + InputFileOffs[I])) {
54395445
// FIXME this drops errors on the floor.
54405446
consumeError(std::move(Err));
54415447
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,8 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
15701570
IFHAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
15711571
unsigned IFHAbbrevCode = Stream.EmitAbbrev(std::move(IFHAbbrev));
15721572

1573+
uint64_t InputFilesOffsetBase = Stream.GetCurrentBitNo();
1574+
15731575
// Get all ContentCache objects for files.
15741576
std::vector<InputFileEntry> UserFiles;
15751577
std::vector<InputFileEntry> SystemFiles;
@@ -1633,7 +1635,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16331635
continue; // already recorded this file.
16341636

16351637
// Record this entry's offset.
1636-
InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1638+
InputFileOffsets.push_back(Stream.GetCurrentBitNo() - InputFilesOffsetBase);
16371639

16381640
InputFileID = InputFileOffsets.size();
16391641

0 commit comments

Comments
 (0)