Skip to content

Commit efedf12

Browse files
committed
[clang][modules] Report failures from lightweight deserialization, save potentially unnecessary heavyweight deserialization
1 parent 9981f50 commit efedf12

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ class ExternalSLocEntrySource {
534534
virtual bool ReadSLocEntry(int ID) = 0;
535535

536536
/// Get the index ID for the loaded SourceLocation offset.
537+
///
538+
/// \returns Invalid index ID (0) if an error occurred that prevented the
539+
/// SLocEntry from being loaded.
537540
virtual int getSLocEntryID(SourceLocation::UIntTy SLocOffset) = 0;
538541

539542
/// Retrieve the module import location and name for the given ID, if

clang/include/clang/Serialization/ASTReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,8 +2155,8 @@ class ASTReader
21552155
bool ReadSLocEntry(int ID) override;
21562156
/// Get the index ID for the loaded SourceLocation offset.
21572157
int getSLocEntryID(SourceLocation::UIntTy SLocOffset) override;
2158-
/// Read the offset of the SLocEntry at the given index in the given module
2159-
/// file.
2158+
/// Try to read the offset of the SLocEntry at the given index in the given
2159+
/// module file.
21602160
std::optional<SourceLocation::UIntTy> readSLocOffset(ModuleFile *F,
21612161
unsigned Index);
21622162

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,7 @@ FileID SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset) const {
864864
/// This function knows that the SourceLocation is in a loaded buffer, not a
865865
/// local one.
866866
FileID SourceManager::getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const {
867-
int ID = ExternalSLocEntries->getSLocEntryID(SLocOffset);
868-
bool Invalid = false;
869-
(void)getLoadedSLocEntryByID(ID, &Invalid);
870-
return Invalid ? FileID() : FileID::get(ID);
867+
return FileID::get(ExternalSLocEntries->getSLocEntryID(SLocOffset));
871868
}
872869

873870
SourceLocation SourceManager::

clang/lib/Serialization/ASTReader.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,20 +1491,28 @@ int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {
14911491
"Corrupted global sloc offset map");
14921492
ModuleFile *F = SLocMapI->second;
14931493

1494+
bool Invalid = false;
1495+
14941496
auto It = llvm::upper_bound(
14951497
llvm::index_range(0, F->LocalNumSLocEntries), SLocOffset,
14961498
[&](SourceLocation::UIntTy Offset, std::size_t Index) {
14971499
if (F->SLocEntryOffsetLoaded[Index] == -1U) {
14981500
auto MaybeEntryOffset = readSLocOffset(F, Index);
1499-
assert(MaybeEntryOffset && "Corrupted AST file");
1501+
if (!MaybeEntryOffset) {
1502+
Invalid = true;
1503+
return true;
1504+
}
15001505
F->SLocEntryOffsetLoaded[Index] = *MaybeEntryOffset;
15011506
}
15021507
return Offset < F->SLocEntryOffsetLoaded[Index];
15031508
});
1509+
1510+
if (Invalid)
1511+
return 0;
1512+
15041513
// The iterator points to the first entry with start offset greater than the
15051514
// offset of interest. The previous entry must contain the offset of interest.
1506-
It = std::prev(It);
1507-
return F->SLocEntryBaseID + *It;
1515+
return F->SLocEntryBaseID + *std::prev(It);
15081516
}
15091517

15101518
bool ASTReader::ReadSLocEntry(int ID) {

0 commit comments

Comments
 (0)