Skip to content

[clang][modules][deps] Cherry-pick explicit modules commits #5568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -1114,13 +1114,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// the entry in SLocEntryTable which contains the specified location.
///
FileID getFileID(SourceLocation SpellingLoc) const {
SourceLocation::UIntTy SLocOffset = SpellingLoc.getOffset();

// If our one-entry cache covers this offset, just return it.
if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
return LastFileIDLookup;

return getFileIDSlow(SLocOffset);
return getFileID(SpellingLoc.getOffset());
}

/// Return the filename of the file containing a SourceLocation.
Expand Down Expand Up @@ -1747,12 +1741,12 @@ class SourceManager : public RefCountedBase<SourceManager> {

/// Returns true if \p Loc came from a PCH/Module.
bool isLoadedSourceLocation(SourceLocation Loc) const {
return Loc.getOffset() >= CurrentLoadedOffset;
return isLoadedOffset(Loc.getOffset());
}

/// Returns true if \p Loc did not come from a PCH/Module.
bool isLocalSourceLocation(SourceLocation Loc) const {
return Loc.getOffset() < NextLocalOffset;
return isLocalOffset(Loc.getOffset());
}

/// Returns true if \p FID came from a PCH/Module.
Expand Down Expand Up @@ -1822,6 +1816,22 @@ class SourceManager : public RefCountedBase<SourceManager> {
return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
}

FileID getFileID(SourceLocation::UIntTy SLocOffset) const {
// If our one-entry cache covers this offset, just return it.
if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
return LastFileIDLookup;

return getFileIDSlow(SLocOffset);
}

bool isLocalOffset(SourceLocation::UIntTy SLocOffset) const {
return SLocOffset < CurrentLoadedOffset;
}

bool isLoadedOffset(SourceLocation::UIntTy SLocOffset) const {
return SLocOffset >= CurrentLoadedOffset;
}

/// Implements the common elements of storing an expansion info struct into
/// the SLocEntry table and producing a source location that refers to it.
SourceLocation
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace serialization {
/// Version 4 of AST files also requires that the version control branch and
/// revision match exactly, since there is no backward compatibility of
/// AST files at this time.
const unsigned VERSION_MAJOR = 23;
const unsigned VERSION_MAJOR = 24;

/// AST file minor version number supported by this version of
/// Clang.
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,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
42 changes: 38 additions & 4 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,40 @@ class ASTWriter : public ASTDeserializationListener,
std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
ModuleFileExtensionWriters;

/// User ModuleMaps skipped when writing control block.
std::set<const FileEntry *> SkippedModuleMaps;
/// Mapping from a source location entry to whether it is affecting or not.
llvm::BitVector IsSLocAffecting;

/// Mapping from \c FileID to an index into the FileID adjustment table.
std::vector<FileID> NonAffectingFileIDs;
std::vector<unsigned> NonAffectingFileIDAdjustments;

/// Mapping from an offset to an index into the offset adjustment table.
std::vector<SourceRange> NonAffectingRanges;
std::vector<SourceLocation::UIntTy> NonAffectingOffsetAdjustments;

/// Collects input files that didn't affect compilation of the current module,
/// and initializes data structures necessary for leaving those files out
/// during \c SourceManager serialization.
void collectNonAffectingInputFiles();

/// Returns an adjusted \c FileID, accounting for any non-affecting input
/// files.
FileID getAdjustedFileID(FileID FID) const;
/// Returns an adjusted number of \c FileIDs created within the specified \c
/// FileID, accounting for any non-affecting input files.
unsigned getAdjustedNumCreatedFIDs(FileID FID) const;
/// Returns an adjusted \c SourceLocation, accounting for any non-affecting
/// input files.
SourceLocation getAdjustedLocation(SourceLocation Loc) const;
/// Returns an adjusted \c SourceRange, accounting for any non-affecting input
/// files.
SourceRange getAdjustedRange(SourceRange Range) const;
/// Returns an adjusted \c SourceLocation offset, accounting for any
/// non-affecting input files.
SourceLocation::UIntTy getAdjustedOffset(SourceLocation::UIntTy Offset) const;
/// Returns an adjustment for offset into SourceManager, accounting for any
/// non-affecting input files.
SourceLocation::UIntTy getAdjustment(SourceLocation::UIntTy Offset) const;

/// Retrieve or create a submodule ID for this module.
unsigned getSubmoduleID(Module *Mod);
Expand All @@ -465,8 +497,7 @@ class ASTWriter : public ASTDeserializationListener,
static std::pair<ASTFileSignature, ASTFileSignature>
createSignature(StringRef AllBytes, StringRef ASTBlockBytes);

void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
std::set<const FileEntry *> &AffectingModuleMaps);
void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
void WriteSourceManagerBlock(SourceManager &SourceMgr,
const Preprocessor &PP);
void writeIncludedFiles(raw_ostream &Out, const Preprocessor &PP);
Expand Down Expand Up @@ -582,6 +613,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
36 changes: 13 additions & 23 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,10 +1344,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 @@ -1364,7 +1361,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 @@ -4295,10 +4292,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 Expand Up @@ -6295,9 +6290,8 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {

DiagStates.clear();

auto ReadDiagState =
[&](const DiagState &BasedOn, SourceLocation Loc,
bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
auto ReadDiagState = [&](const DiagState &BasedOn,
bool IncludeNonPragmaStates) {
unsigned BackrefID = Record[Idx++];
if (BackrefID != 0)
return DiagStates[BackrefID - 1];
Expand Down Expand Up @@ -6358,7 +6352,7 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
Initial.ExtBehavior = (diag::Severity)Flags;
FirstState = ReadDiagState(Initial, SourceLocation(), true);
FirstState = ReadDiagState(Initial, true);

assert(F.OriginalSourceFileID.isValid());

Expand All @@ -6371,31 +6365,27 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
// For prefix ASTs, start with whatever the user configured on the
// command line.
Idx++; // Skip flags.
FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
SourceLocation(), false);
FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
}

// Read the state transitions.
unsigned NumLocations = Record[Idx++];
while (NumLocations--) {
assert(Idx < Record.size() &&
"Invalid data, missing pragma diagnostic states");
SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
assert(IDAndOffset.second == 0 && "not a start location for a FileID");
FileID FID = ReadFileID(F, Record, Idx);
assert(FID.isValid() && "invalid FileID for transition");
unsigned Transitions = Record[Idx++];

// Note that we don't need to set up Parent/ParentOffset here, because
// we won't be changing the diagnostic state within imported FileIDs
// (other than perhaps appending to the main source file, which has no
// parent).
auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
auto &F = Diag.DiagStatesByLoc.Files[FID];
F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
for (unsigned I = 0; I != Transitions; ++I) {
unsigned Offset = Record[Idx++];
auto *State =
ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
auto *State = ReadDiagState(*FirstState, false);
F.StateTransitions.push_back({State, Offset});
}
}
Expand All @@ -6405,7 +6395,7 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
"Invalid data, missing final pragma diagnostic state");
SourceLocation CurStateLoc =
ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
auto *CurState = ReadDiagState(*FirstState, false);

if (!F.isModule()) {
Diag.DiagStatesByLoc.CurDiagState = CurState;
Expand Down
Loading