[llvm] [DebugInfo] getMergedLocation: match scopes based on their location (PR #132286)

Stephen Tozer via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 17 12:24:22 PDT 2025


================
@@ -187,28 +287,31 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) {
     if (L1->getScope()->getSubprogram() != L2->getScope()->getSubprogram())
       return nullptr;
 
-    // Return the nearest common scope inside a subprogram.
-    auto GetNearestCommonScope = [](DIScope *S1, DIScope *S2) -> DIScope * {
-      SmallPtrSet<DIScope *, 8> Scopes;
-      for (; S1; S1 = S1->getScope()) {
-        Scopes.insert(S1);
-        if (isa<DISubprogram>(S1))
-          break;
-      }
-
-      for (; S2; S2 = S2->getScope()) {
-        if (Scopes.count(S2))
-          return S2;
-        if (isa<DISubprogram>(S2))
-          break;
-      }
-
-      return nullptr;
-    };
-
-    auto Scope = GetNearestCommonScope(L1->getScope(), L2->getScope());
+    // Find nearest common scope inside subprogram.
+    DIScope *Scope = getNearestMatchingScope<EqualScopesMatcher>(L1, L2).first;
     assert(Scope && "No common scope in the same subprogram?");
 
+    // Try using the nearest scope with common location if files are different.
+    if (Scope->getFile() != L1->getFile() || L1->getFile() != L2->getFile()) {
+      auto [CommonLocScope, CommonLoc] =
+          getNearestMatchingScope<ScopeLocationsMatcher>(L1, L2);
----------------
SLTozer wrote:

I can imagine a small optimization where for this second search, we cap the search up through `L1`'s scope chain at `Scope`, i.e. we never consider any parent scopes of `Scope`; but that's a minor point that may not be worth making the code less legible - your call on whether to do it.

https://github.com/llvm/llvm-project/pull/132286


More information about the llvm-commits mailing list