Skip to content

Commit fcaaa82

Browse files
authored
Merge pull request #5568 from apple/jan_svoboda/stable-20221013-cherry-pick
[clang][modules][deps] Cherry-pick explicit modules commits
2 parents 9311372 + d759352 commit fcaaa82

File tree

11 files changed

+329
-138
lines changed

11 files changed

+329
-138
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
11141114
/// the entry in SLocEntryTable which contains the specified location.
11151115
///
11161116
FileID getFileID(SourceLocation SpellingLoc) const {
1117-
SourceLocation::UIntTy SLocOffset = SpellingLoc.getOffset();
1118-
1119-
// If our one-entry cache covers this offset, just return it.
1120-
if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1121-
return LastFileIDLookup;
1122-
1123-
return getFileIDSlow(SLocOffset);
1117+
return getFileID(SpellingLoc.getOffset());
11241118
}
11251119

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

17481742
/// Returns true if \p Loc came from a PCH/Module.
17491743
bool isLoadedSourceLocation(SourceLocation Loc) const {
1750-
return Loc.getOffset() >= CurrentLoadedOffset;
1744+
return isLoadedOffset(Loc.getOffset());
17511745
}
17521746

17531747
/// Returns true if \p Loc did not come from a PCH/Module.
17541748
bool isLocalSourceLocation(SourceLocation Loc) const {
1755-
return Loc.getOffset() < NextLocalOffset;
1749+
return isLocalOffset(Loc.getOffset());
17561750
}
17571751

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

1819+
FileID getFileID(SourceLocation::UIntTy SLocOffset) const {
1820+
// If our one-entry cache covers this offset, just return it.
1821+
if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1822+
return LastFileIDLookup;
1823+
1824+
return getFileIDSlow(SLocOffset);
1825+
}
1826+
1827+
bool isLocalOffset(SourceLocation::UIntTy SLocOffset) const {
1828+
return SLocOffset < CurrentLoadedOffset;
1829+
}
1830+
1831+
bool isLoadedOffset(SourceLocation::UIntTy SLocOffset) const {
1832+
return SLocOffset >= CurrentLoadedOffset;
1833+
}
1834+
18251835
/// Implements the common elements of storing an expansion info struct into
18261836
/// the SLocEntry table and producing a source location that refers to it.
18271837
SourceLocation

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace serialization {
4141
/// Version 4 of AST files also requires that the version control branch and
4242
/// revision match exactly, since there is no backward compatibility of
4343
/// AST files at this time.
44-
const unsigned VERSION_MAJOR = 23;
44+
const unsigned VERSION_MAJOR = 24;
4545

4646
/// AST file minor version number supported by this version of
4747
/// Clang.

clang/include/clang/Serialization/ASTReader.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,18 @@ class ASTReader
21922192
return ReadSourceLocation(ModuleFile, Record[Idx++], Seq);
21932193
}
21942194

2195+
/// Read a FileID.
2196+
FileID ReadFileID(ModuleFile &F, const RecordDataImpl &Record,
2197+
unsigned &Idx) const {
2198+
return TranslateFileID(F, FileID::get(Record[Idx++]));
2199+
}
2200+
2201+
/// Translate a FileID from another module file's FileID space into ours.
2202+
FileID TranslateFileID(ModuleFile &F, FileID FID) const {
2203+
assert(FID.ID >= 0 && "Reading non-local FileID.");
2204+
return FileID::get(F.SLocEntryBaseID + FID.ID - 1);
2205+
}
2206+
21952207
/// Read a source range.
21962208
SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record,
21972209
unsigned &Idx, LocSeq *Seq = nullptr);

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,40 @@ class ASTWriter : public ASTDeserializationListener,
444444
std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
445445
ModuleFileExtensionWriters;
446446

447-
/// User ModuleMaps skipped when writing control block.
448-
std::set<const FileEntry *> SkippedModuleMaps;
447+
/// Mapping from a source location entry to whether it is affecting or not.
448+
llvm::BitVector IsSLocAffecting;
449+
450+
/// Mapping from \c FileID to an index into the FileID adjustment table.
451+
std::vector<FileID> NonAffectingFileIDs;
452+
std::vector<unsigned> NonAffectingFileIDAdjustments;
453+
454+
/// Mapping from an offset to an index into the offset adjustment table.
455+
std::vector<SourceRange> NonAffectingRanges;
456+
std::vector<SourceLocation::UIntTy> NonAffectingOffsetAdjustments;
457+
458+
/// Collects input files that didn't affect compilation of the current module,
459+
/// and initializes data structures necessary for leaving those files out
460+
/// during \c SourceManager serialization.
461+
void collectNonAffectingInputFiles();
462+
463+
/// Returns an adjusted \c FileID, accounting for any non-affecting input
464+
/// files.
465+
FileID getAdjustedFileID(FileID FID) const;
466+
/// Returns an adjusted number of \c FileIDs created within the specified \c
467+
/// FileID, accounting for any non-affecting input files.
468+
unsigned getAdjustedNumCreatedFIDs(FileID FID) const;
469+
/// Returns an adjusted \c SourceLocation, accounting for any non-affecting
470+
/// input files.
471+
SourceLocation getAdjustedLocation(SourceLocation Loc) const;
472+
/// Returns an adjusted \c SourceRange, accounting for any non-affecting input
473+
/// files.
474+
SourceRange getAdjustedRange(SourceRange Range) const;
475+
/// Returns an adjusted \c SourceLocation offset, accounting for any
476+
/// non-affecting input files.
477+
SourceLocation::UIntTy getAdjustedOffset(SourceLocation::UIntTy Offset) const;
478+
/// Returns an adjustment for offset into SourceManager, accounting for any
479+
/// non-affecting input files.
480+
SourceLocation::UIntTy getAdjustment(SourceLocation::UIntTy Offset) const;
449481

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

468-
void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
469-
std::set<const FileEntry *> &AffectingModuleMaps);
500+
void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
470501
void WriteSourceManagerBlock(SourceManager &SourceMgr,
471502
const Preprocessor &PP);
472503
void writeIncludedFiles(raw_ostream &Out, const Preprocessor &PP);
@@ -582,6 +613,9 @@ class ASTWriter : public ASTDeserializationListener,
582613
void AddAlignPackInfo(const Sema::AlignPackInfo &Info,
583614
RecordDataImpl &Record);
584615

616+
/// Emit a FileID.
617+
void AddFileID(FileID FID, RecordDataImpl &Record);
618+
585619
/// Emit a source location.
586620
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
587621
LocSeq *Seq = nullptr);

clang/lib/Serialization/ASTReader.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,10 +1344,7 @@ void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
13441344
// Parse the line entries
13451345
std::vector<LineEntry> Entries;
13461346
while (Idx < Record.size()) {
1347-
int FID = Record[Idx++];
1348-
assert(FID >= 0 && "Serialized line entries for non-local file.");
1349-
// Remap FileID from 1-based old view.
1350-
FID += F.SLocEntryBaseID - 1;
1347+
FileID FID = ReadFileID(F, Record, Idx);
13511348

13521349
// Extract the line entries
13531350
unsigned NumEntries = Record[Idx++];
@@ -1364,7 +1361,7 @@ void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
13641361
Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
13651362
FileKind, IncludeOffset));
13661363
}
1367-
LineTable.AddEntry(FileID::get(FID), Entries);
1364+
LineTable.AddEntry(FID, Entries);
13681365
}
13691366
}
13701367

@@ -4299,10 +4296,8 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
42994296

43004297
// Map the original source file ID into the ID space of the current
43014298
// compilation.
4302-
if (F.OriginalSourceFileID.isValid()) {
4303-
F.OriginalSourceFileID = FileID::get(
4304-
F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4305-
}
4299+
if (F.OriginalSourceFileID.isValid())
4300+
F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);
43064301

43074302
// Preload all the pending interesting identifiers by marking them out of
43084303
// date.
@@ -6312,9 +6307,8 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
63126307

63136308
DiagStates.clear();
63146309

6315-
auto ReadDiagState =
6316-
[&](const DiagState &BasedOn, SourceLocation Loc,
6317-
bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6310+
auto ReadDiagState = [&](const DiagState &BasedOn,
6311+
bool IncludeNonPragmaStates) {
63186312
unsigned BackrefID = Record[Idx++];
63196313
if (BackrefID != 0)
63206314
return DiagStates[BackrefID - 1];
@@ -6375,7 +6369,7 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
63756369
Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
63766370
Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
63776371
Initial.ExtBehavior = (diag::Severity)Flags;
6378-
FirstState = ReadDiagState(Initial, SourceLocation(), true);
6372+
FirstState = ReadDiagState(Initial, true);
63796373

63806374
assert(F.OriginalSourceFileID.isValid());
63816375

@@ -6388,31 +6382,27 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
63886382
// For prefix ASTs, start with whatever the user configured on the
63896383
// command line.
63906384
Idx++; // Skip flags.
6391-
FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6392-
SourceLocation(), false);
6385+
FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);
63936386
}
63946387

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

64066397
// Note that we don't need to set up Parent/ParentOffset here, because
64076398
// we won't be changing the diagnostic state within imported FileIDs
64086399
// (other than perhaps appending to the main source file, which has no
64096400
// parent).
6410-
auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6401+
auto &F = Diag.DiagStatesByLoc.Files[FID];
64116402
F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
64126403
for (unsigned I = 0; I != Transitions; ++I) {
64136404
unsigned Offset = Record[Idx++];
6414-
auto *State =
6415-
ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6405+
auto *State = ReadDiagState(*FirstState, false);
64166406
F.StateTransitions.push_back({State, Offset});
64176407
}
64186408
}
@@ -6422,7 +6412,7 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
64226412
"Invalid data, missing final pragma diagnostic state");
64236413
SourceLocation CurStateLoc =
64246414
ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6425-
auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6415+
auto *CurState = ReadDiagState(*FirstState, false);
64266416

64276417
if (!F.isModule()) {
64286418
Diag.DiagStatesByLoc.CurDiagState = CurState;

0 commit comments

Comments
 (0)