[kdevelop/kdevelop] kdevplatform/language/duchain: DUChain: stabilize the hash value of ReferencedTopDUContext

Jarmo Tiitto <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit dfca0399fb72d910df0b8ba353f85c9f8fce5d88 by Jarmo Tiitto.
Committed on 02/08/2026 at 13:12.
Pushed by jatothrim into branch 'master'.

DUChain: stabilize the hash value of ReferencedTopDUContext

ReferencedTopDUContext hash value is currently computed from a unstable
m_topContext pointer value. Moreover, the returned value is consumed by
QSet<ReferencedTopDUContext> DUChain::m_openDocumentContexts, which
therefore mandates a size_t (64-bit) hash value to be used.

m_topContext->ownIndex() is a persistent unique index of TopDUContext
serialized as TopDUContextData::m_ownIndex. The TopDUContext indexes are
reserved with DUChain::newTopContextIndex(). Compute the hash instead
based on the m_topContext->ownIndex() using qHash() to expand and mix it
as a 64-bit value. This makes the hash value stable across multiple
session reloads.

M  +8    -0    kdevplatform/language/duchain/topducontext.cpp
M  +1    -4    kdevplatform/language/duchain/topducontext.h

https://invent.kde.org/kdevelop/kdevelop/-/commit/dfca0399fb72d910df0b8ba353f85c9f8fce5d88

diff --git a/kdevplatform/language/duchain/topducontext.cpp b/kdevplatform/language/duchain/topducontext.cpp
index 2b4b7bc27c..f1526d694a 100644
--- a/kdevplatform/language/duchain/topducontext.cpp
+++ b/kdevplatform/language/duchain/topducontext.cpp
@@ -28,6 +28,7 @@
 
 #include <language/interfaces/iastcontainer.h>
 
+#include <QHash>
 #include <QMutexLocker>
 #include <QRecursiveMutex>
 
@@ -77,6 +78,13 @@ ReferencedTopDUContext& ReferencedTopDUContext::operator=(const ReferencedTopDUC
     return *this;
 }
 
+size_t ReferencedTopDUContext::hash() const
+{
+    // This must be based on a unique disk-persistent value of m_topContext and return a 64-bit hash.
+    Q_ASSERT(m_topContext);
+    return ::qHash(size_t(m_topContext->ownIndex()));
+}
+
 DEFINE_LIST_MEMBER_HASH(TopDUContextData, m_usedDeclarationIds, DeclarationId)
 DEFINE_LIST_MEMBER_HASH(TopDUContextData, m_problems, LocalIndexedProblem)
 REGISTER_DUCHAIN_ITEM(TopDUContext);
diff --git a/kdevplatform/language/duchain/topducontext.h b/kdevplatform/language/duchain/topducontext.h
index 21ebedced6..e768433a4d 100644
--- a/kdevplatform/language/duchain/topducontext.h
+++ b/kdevplatform/language/duchain/topducontext.h
@@ -80,10 +80,7 @@ public:
         return m_topContext;
     }
 
-    inline uint hash() const
-    {
-        return ( uint )((( quint64 )m_topContext) * 37);
-    }
+    size_t hash() const;
 
 private:
     TopDUContext* m_topContext;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.