Skip to content

Commit

Permalink
[clang][modules] NFCI: Unify FileID writing/reading
Browse files Browse the repository at this point in the history
This patch adds new functions for writing/reading `FileID`s and uses them to replace some ad-hoc code.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D137211
  • Loading branch information
jansvoboda11 committed Nov 2, 2022
1 parent da137e1 commit fdbc55a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
12 changes: 12 additions & 0 deletions clang/include/clang/Serialization/ASTReader.h
Expand Up @@ -2196,6 +2196,18 @@ class ASTReader
return ReadSourceLocation(ModuleFile, Record[Idx++], Seq);
}

/// Read a FileID.
FileID ReadFileID(ModuleFile &F, const RecordDataImpl &Record,
unsigned &Idx) const {
return TranslateFileID(F, FileID::get(Record[Idx++]));
}

/// Translate a FileID from another module file's FileID space into ours.
FileID TranslateFileID(ModuleFile &F, FileID FID) const {
assert(FID.ID >= 0 && "Reading non-local FileID.");
return FileID::get(F.SLocEntryBaseID + FID.ID - 1);
}

/// Read a source range.
SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record,
unsigned &Idx, LocSeq *Seq = nullptr);
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Serialization/ASTWriter.h
Expand Up @@ -582,6 +582,9 @@ class ASTWriter : public ASTDeserializationListener,
void AddAlignPackInfo(const Sema::AlignPackInfo &Info,
RecordDataImpl &Record);

/// Emit a FileID.
void AddFileID(FileID FID, RecordDataImpl &Record);

/// Emit a source location.
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
LocSeq *Seq = nullptr);
Expand Down
13 changes: 4 additions & 9 deletions clang/lib/Serialization/ASTReader.cpp
Expand Up @@ -1338,10 +1338,7 @@ void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
// Parse the line entries
std::vector<LineEntry> Entries;
while (Idx < Record.size()) {
int FID = Record[Idx++];
assert(FID >= 0 && "Serialized line entries for non-local file.");
// Remap FileID from 1-based old view.
FID += F.SLocEntryBaseID - 1;
FileID FID = ReadFileID(F, Record, Idx);

// Extract the line entries
unsigned NumEntries = Record[Idx++];
Expand All @@ -1358,7 +1355,7 @@ void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
FileKind, IncludeOffset));
}
LineTable.AddEntry(FileID::get(FID), Entries);
LineTable.AddEntry(FID, Entries);
}
}

Expand Down Expand Up @@ -4294,10 +4291,8 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,

// Map the original source file ID into the ID space of the current
// compilation.
if (F.OriginalSourceFileID.isValid()) {
F.OriginalSourceFileID = FileID::get(
F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
}
if (F.OriginalSourceFileID.isValid())
F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);

// Preload all the pending interesting identifiers by marking them out of
// date.
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/Serialization/ASTWriter.cpp
Expand Up @@ -1469,12 +1469,12 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,

Record.clear();
Record.push_back(ORIGINAL_FILE);
Record.push_back(SM.getMainFileID().getOpaqueValue());
AddFileID(SM.getMainFileID(), Record);
EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
}

Record.clear();
Record.push_back(SM.getMainFileID().getOpaqueValue());
AddFileID(SM.getMainFileID(), Record);
Stream.EmitRecord(ORIGINAL_FILE_ID, Record);

std::set<const FileEntry *> AffectingClangModuleMaps;
Expand Down Expand Up @@ -2206,8 +2206,7 @@ void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
if (L.first.ID < 0)
continue;

// Emit the file ID
Record.push_back(L.first.ID);
AddFileID(L.first, Record);

// Emit the line entries
Record.push_back(L.second.size());
Expand Down Expand Up @@ -5228,6 +5227,10 @@ void ASTWriter::AddAlignPackInfo(const Sema::AlignPackInfo &Info,
Record.push_back(Raw);
}

void ASTWriter::AddFileID(FileID FID, RecordDataImpl &Record) {
Record.push_back(FID.getOpaqueValue());
}

void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
SourceLocationSequence *Seq) {
Record.push_back(SourceLocationEncoding::encode(Loc, Seq));
Expand Down

0 comments on commit fdbc55a

Please sign in to comment.