Skip to content

Commit 4f61863

Browse files
[clang] Optimize SourceManager.getSpellingLocSlowCase and SourceManager.getFileLocSlowCase (#164269)
Optimize implementations of `getSpellingLocSlowCase` and `getFileLocSlowCase` by inlining called methods to avoid repeated calls to `getSLocEntry` and `getFileID`. a performance improvement follow up for #160667
1 parent 332f9b5 commit 4f61863

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

clang/lib/Basic/SourceManager.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,19 +907,23 @@ getExpansionLocSlowCase(SourceLocation Loc) const {
907907

908908
SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
909909
do {
910-
FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
911-
Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
912-
Loc = Loc.getLocWithOffset(LocInfo.second);
910+
const SLocEntry &Entry = getSLocEntry(getFileID(Loc));
911+
Loc = Entry.getExpansion().getSpellingLoc().getLocWithOffset(
912+
Loc.getOffset() - Entry.getOffset());
913913
} while (!Loc.isFileID());
914914
return Loc;
915915
}
916916

917917
SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
918918
do {
919-
if (isMacroArgExpansion(Loc))
920-
Loc = getImmediateSpellingLoc(Loc);
921-
else
922-
Loc = getImmediateExpansionRange(Loc).getBegin();
919+
const SLocEntry &Entry = getSLocEntry(getFileID(Loc));
920+
const ExpansionInfo &ExpInfo = Entry.getExpansion();
921+
if (ExpInfo.isMacroArgExpansion()) {
922+
Loc = ExpInfo.getSpellingLoc().getLocWithOffset(Loc.getOffset() -
923+
Entry.getOffset());
924+
} else {
925+
Loc = ExpInfo.getExpansionLocStart();
926+
}
923927
} while (!Loc.isFileID());
924928
return Loc;
925929
}

0 commit comments

Comments
 (0)