[clang] [clang] Optimize SourceManager.getSpellingLocSlowCase and SourceManager.getFileLocSlowCase (PR #164269)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 31 02:53:28 PDT 2025


https://github.com/SergejSalnikov updated https://github.com/llvm/llvm-project/pull/164269

>From f27f647b0b43a68c2f4f1773908b200164f5d6ce Mon Sep 17 00:00:00 2001
From: SKill <41159490+SergejSalnikov at users.noreply.github.com>
Date: Mon, 20 Oct 2025 17:17:47 +0200
Subject: [PATCH] Optimize getSpellingLocSlowCase and getFileLocSlowCase

Optimize implementations of  `getSpellingLocSlowCase` and `getFileLocSlowCase` by inlining methods to avoid repeated calls to `getSLocEntry`
---
 clang/lib/Basic/SourceManager.cpp | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index d8ec837f0f7b9..cedbd31dcef72 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -908,19 +908,23 @@ getExpansionLocSlowCase(SourceLocation Loc) const {
 
 SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
   do {
-    FileIDAndOffset LocInfo = getDecomposedLoc(Loc);
-    Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
-    Loc = Loc.getLocWithOffset(LocInfo.second);
+    const SLocEntry &Entry = getSLocEntry(getFileID(Loc));
+    Loc = Entry.getExpansion().getSpellingLoc().getLocWithOffset(
+        Loc.getOffset() - Entry.getOffset());
   } while (!Loc.isFileID());
   return Loc;
 }
 
 SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
   do {
-    if (isMacroArgExpansion(Loc))
-      Loc = getImmediateSpellingLoc(Loc);
-    else
-      Loc = getImmediateExpansionRange(Loc).getBegin();
+    const SLocEntry &Entry = getSLocEntry(getFileID(Loc));
+    const ExpansionInfo &ExpInfo = Entry.getExpansion();
+    if (ExpInfo.isMacroArgExpansion()) {
+      Loc = ExpInfo.getSpellingLoc().getLocWithOffset(Loc.getOffset() -
+                                                      Entry.getOffset());
+    } else {
+      Loc = ExpInfo.getExpansionLocStart();
+    }
   } while (!Loc.isFileID());
   return Loc;
 }



More information about the cfe-commits mailing list