Skip to content

Commit 8f035aa

Browse files
committed
[clang] Clarify FileID comparison
1 parent 61e8961 commit 8f035aa

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

clang/lib/Basic/SourceManager.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,48 +2100,49 @@ std::pair<bool, bool> SourceManager::isInTheSameTranslationUnit(
21002100

21012101
// A location within a FileID on the path up from LOffs to the main file.
21022102
struct Entry {
2103-
unsigned Offset;
2104-
FileID ParentFID; // Used for breaking ties.
2103+
std::pair<FileID, unsigned> DecomposedLoc; // FileID redundant, but clearer.
2104+
FileID ChildFID; // Used for breaking ties. Invalid for the initial loc.
21052105
};
21062106
llvm::SmallDenseMap<FileID, Entry, 16> LChain;
21072107

2108-
FileID Parent;
2108+
FileID LChild;
21092109
do {
2110-
LChain.try_emplace(LOffs.first, Entry{LOffs.second, Parent});
2110+
LChain.try_emplace(LOffs.first, Entry{LOffs, LChild});
21112111
// We catch the case where LOffs is in a file included by ROffs and
21122112
// quit early. The other way round unfortunately remains suboptimal.
21132113
if (LOffs.first == ROffs.first)
21142114
break;
2115-
Parent = LOffs.first;
2115+
LChild = LOffs.first;
21162116
} while (!MoveUpIncludeHierarchy(LOffs, *this));
21172117

2118-
Parent = FileID();
2118+
FileID RChild;
21192119
do {
2120-
auto I = LChain.find(ROffs.first);
2121-
if (I != LChain.end()) {
2120+
auto LIt = LChain.find(ROffs.first);
2121+
if (LIt != LChain.end()) {
21222122
// Compare the locations within the common file and cache them.
2123-
LOffs.first = I->first;
2124-
LOffs.second = I->second.Offset;
2125-
// The relative order of LParent and RParent is a tiebreaker when
2123+
LOffs = LIt->second.DecomposedLoc;
2124+
LChild = LIt->second.ChildFID;
2125+
// The relative order of LChild and RChild is a tiebreaker when
21262126
// - locs expand to the same location (occurs in macro arg expansion)
21272127
// - one loc is a parent of the other (we consider the parent as "first")
2128-
// For the parent to be first, the invalid file ID must compare smaller.
2128+
// For the parent entry to be first, its invalid child file ID must
2129+
// compare smaller to the valid child file ID of the other entry.
21292130
// However loaded FileIDs are <0, so we perform *unsigned* comparison!
21302131
// This changes the relative order of local vs loaded FileIDs, but it
21312132
// doesn't matter as these are never mixed in macro expansion.
2132-
unsigned LParent = I->second.ParentFID.ID;
2133-
unsigned RParent = Parent.ID;
2133+
unsigned LChildID = LChild.ID;
2134+
unsigned RChildID = RChild.ID;
21342135
assert(((LOffs.second != ROffs.second) ||
2135-
(LParent == 0 || RParent == 0) ||
2136-
isInSameSLocAddrSpace(getComposedLoc(I->second.ParentFID, 0),
2137-
getComposedLoc(Parent, 0), nullptr)) &&
2136+
(LChildID == 0 || RChildID == 0) ||
2137+
isInSameSLocAddrSpace(getComposedLoc(LChild, 0),
2138+
getComposedLoc(RChild, 0), nullptr)) &&
21382139
"Mixed local/loaded FileIDs with same include location?");
21392140
IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second,
2140-
LParent < RParent);
2141+
LChildID < RChildID);
21412142
return std::make_pair(
21422143
true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
21432144
}
2144-
Parent = ROffs.first;
2145+
RChild = ROffs.first;
21452146
} while (!MoveUpIncludeHierarchy(ROffs, *this));
21462147

21472148
// If we found no match, we're not in the same TU.

0 commit comments

Comments
 (0)