[Lldb-commits] [lldb] [lldb][Clike] Add IdentifierMap (PR #222663)
Raphael Isemann via lldb-commits
lldb-commits at lists.llvm.org
Tue Sep 29 02:06:26 PDT 2026
https://github.com/Teemperor updated https://github.com/llvm/llvm-project/pull/222663
>From 96ff26b024ab9ef11dc7f5d76a13e8f7d2237924 Mon Sep 17 00:00:00 2001
From: Raphael Isemann <rise at apple.com>
Date: Tue, 8 Sep 2026 11:59:37 +0100
Subject: [PATCH] [lldb][Clike] Add IdentifierMap
TypeSystemClike needs to store the name of structs, enums, member
variables and similar entities. In the old TypeSystemClang this
information was stored in Clang's IdentifierTable.
This patch introduces an equivalent for TypeSystemClike called
`IdentifierMap`. It turns strings into unique Identifier objects, which
for now directly store the StringRef to the value. The actual storage
of the string contents is either (A) in the IdentifierMap itself
or (B) backed by ConstString/constant memory for strings. Option B
exists mainly because we often already have a ConstString around that
already contains the respective string, and it avoids us having to
save a copy like with TypeSystemClang.
Some design questions where I just picked one option:
(a) Do we really need to deduplicate strings?
There is no functional reason to do it, but it might save some memory
for heavily templated code (where each instantiation is its own time
with the same names for everything). We can benchmark this once the
system is working to see what is the right tradeoff.
(b) Can't we just store some small id in `Identifier` instead of a
StringRef (and the id would be some offset into an IdentifierMap data
structure)?
We could, but then you would need to pass the right IdentifierMap to
resolve the id in `Identifier::getName()`, and that is a bit fragile.
Especially in the context of multiple TypeSystems being used at the
same time, this can easily go wrong. Again, we might want to benchmark
this at the end to see if storing something smaller is worth it in
terms of memory.
---
.../Plugins/TypeSystem/Clike/CMakeLists.txt | 1 +
.../Plugins/TypeSystem/Clike/Identifier.cpp | 49 ++++++++++++++
.../Plugins/TypeSystem/Clike/Identifier.h | 65 +++++++++++++++++++
lldb/unittests/CMakeLists.txt | 1 +
lldb/unittests/TypeSystem/CMakeLists.txt | 1 +
.../unittests/TypeSystem/Clike/CMakeLists.txt | 6 ++
.../TypeSystem/Clike/IdentifierTest.cpp | 64 ++++++++++++++++++
7 files changed, 187 insertions(+)
create mode 100644 lldb/source/Plugins/TypeSystem/Clike/Identifier.cpp
create mode 100644 lldb/source/Plugins/TypeSystem/Clike/Identifier.h
create mode 100644 lldb/unittests/TypeSystem/CMakeLists.txt
create mode 100644 lldb/unittests/TypeSystem/Clike/CMakeLists.txt
create mode 100644 lldb/unittests/TypeSystem/Clike/IdentifierTest.cpp
diff --git a/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt b/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt
index 89e1771177109..3f8e742165cfd 100644
--- a/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt
+++ b/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt
@@ -1,4 +1,5 @@
add_lldb_library(lldbPluginTypeSystemClike PLUGIN
+ Identifier.cpp
TypeSystemClike.cpp
LINK_COMPONENTS
diff --git a/lldb/source/Plugins/TypeSystem/Clike/Identifier.cpp b/lldb/source/Plugins/TypeSystem/Clike/Identifier.cpp
new file mode 100644
index 0000000000000..e4d6a26023148
--- /dev/null
+++ b/lldb/source/Plugins/TypeSystem/Clike/Identifier.cpp
@@ -0,0 +1,49 @@
+//===-- Identifier.cpp ----------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Identifier.h"
+
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/ErrorHandling.h"
+
+#include <cstring>
+
+using namespace lldb_private;
+using namespace lldb_private::clike;
+
+Identifier IdentifierMap::get(llvm::StringRef name) {
+ auto it = m_names.find(name);
+ if (it != m_names.end())
+ return Identifier(*it);
+
+ // Copy the string into our own storage.
+ // TODO: Would it be beneficial to add a null terminator into the buffer?
+ char *storage = m_string_storage.Allocate<char>(name.size());
+ std::memcpy(storage, name.data(), name.size());
+ llvm::StringRef owned(storage, name.size());
+ m_names.insert(owned);
+ return Identifier(owned);
+}
+
+Identifier IdentifierMap::getWithStaticStorageStr(llvm::StringRef name) {
+ // The caller promises the backing storage outlives this map.
+ m_names.insert(name);
+ return Identifier(name);
+}
+
+IdentifierMap::~IdentifierMap() {
+#if LLVM_ADDRESS_SANITIZER_BUILD
+ // Verify every identifier still points at live memory.
+ for (llvm::StringRef name : m_names) {
+ if (!name.empty() &&
+ __asan_region_is_poisoned(const_cast<char *>(name.data()), name.size()))
+ llvm::report_fatal_error("IdentifierMap holds a freed string "
+ "(misuse of getWithStaticStorageStr?)");
+ }
+#endif
+}
diff --git a/lldb/source/Plugins/TypeSystem/Clike/Identifier.h b/lldb/source/Plugins/TypeSystem/Clike/Identifier.h
new file mode 100644
index 0000000000000..c193a3638d64a
--- /dev/null
+++ b/lldb/source/Plugins/TypeSystem/Clike/Identifier.h
@@ -0,0 +1,65 @@
+//===-- Identifier.h --------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLIKE_IDENTIFIER_H
+#define LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLIKE_IDENTIFIER_H
+
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Allocator.h"
+
+#include <vector>
+
+namespace lldb_private {
+namespace clike {
+
+class IdentifierMap;
+
+/// Represents a name in TypeSystemClike.
+class Identifier {
+public:
+ Identifier() = default;
+
+ llvm::StringRef GetName() const { return m_name; }
+
+private:
+ // Only IdentifierMap may build a non-empty Identifier.
+ friend class IdentifierMap;
+ explicit Identifier(llvm::StringRef name) : m_name(name) {}
+
+ llvm::StringRef m_name;
+};
+
+/// Turns strings into unique Identifier objects.
+class IdentifierMap {
+public:
+ ~IdentifierMap();
+
+ /// Returns an Identifier for \p name.
+ ///
+ /// This copies the string into storage owned by this map.
+ Identifier get(llvm::StringRef name);
+
+ /// Returns an Identifier for \p name.
+ ///
+ /// This does not make a copy of the passed string and the string storage
+ /// needs to outlive this IdentifierMap. This is used if `name` is a string
+ /// literal or backed by ConstString.
+ Identifier getWithStaticStorageStr(llvm::StringRef name);
+
+private:
+ /// Owns the copies made by get().
+ llvm::BumpPtrAllocator m_string_storage;
+ /// Set of all created Identifiers strings.
+ llvm::DenseSet<llvm::StringRef> m_names;
+};
+
+} // namespace clike
+} // namespace lldb_private
+
+#endif // LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLIKE_IDENTIFIER_H
diff --git a/lldb/unittests/CMakeLists.txt b/lldb/unittests/CMakeLists.txt
index b0b7f68a7dcd6..6ded2974adfd3 100644
--- a/lldb/unittests/CMakeLists.txt
+++ b/lldb/unittests/CMakeLists.txt
@@ -101,6 +101,7 @@ add_subdirectory(Symbol)
add_subdirectory(SymbolFile)
add_subdirectory(Target)
add_subdirectory(Thread)
+add_subdirectory(TypeSystem)
add_subdirectory(UnwindAssembly)
add_subdirectory(Utility)
add_subdirectory(ValueObject)
diff --git a/lldb/unittests/TypeSystem/CMakeLists.txt b/lldb/unittests/TypeSystem/CMakeLists.txt
new file mode 100644
index 0000000000000..61f1669b38ed0
--- /dev/null
+++ b/lldb/unittests/TypeSystem/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(Clike)
diff --git a/lldb/unittests/TypeSystem/Clike/CMakeLists.txt b/lldb/unittests/TypeSystem/Clike/CMakeLists.txt
new file mode 100644
index 0000000000000..c74cc7d651de1
--- /dev/null
+++ b/lldb/unittests/TypeSystem/Clike/CMakeLists.txt
@@ -0,0 +1,6 @@
+add_lldb_unittest(TypeSystemClikeUnitTests
+ IdentifierTest.cpp
+
+ LINK_LIBS
+ lldbPluginTypeSystemClike
+ )
diff --git a/lldb/unittests/TypeSystem/Clike/IdentifierTest.cpp b/lldb/unittests/TypeSystem/Clike/IdentifierTest.cpp
new file mode 100644
index 0000000000000..811ef71aa7c94
--- /dev/null
+++ b/lldb/unittests/TypeSystem/Clike/IdentifierTest.cpp
@@ -0,0 +1,64 @@
+//===-- IdentifierTest.cpp ------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Plugins/TypeSystem/Clike/Identifier.h"
+
+#include "gtest/gtest.h"
+
+using namespace lldb_private::clike;
+
+TEST(IdentifierTest, DefaultConstructed) {
+ Identifier id;
+ EXPECT_TRUE(id.GetName().empty());
+}
+
+TEST(IdentifierTest, GetInternsEqualStrings) {
+ IdentifierMap map;
+ Identifier a = map.get("foo");
+ Identifier b = map.get("foo");
+ EXPECT_EQ(a.GetName(), "foo");
+ EXPECT_EQ(a.GetName().data(), b.GetName().data());
+}
+
+TEST(IdentifierTest, GetDistinctStringsAreDistinct) {
+ IdentifierMap map;
+ Identifier a = map.get("foo");
+ Identifier b = map.get("bar");
+ EXPECT_NE(a.GetName(), b.GetName());
+}
+
+TEST(IdentifierTest, GetCopiesInput) {
+ IdentifierMap map;
+ Identifier id;
+ {
+ std::string temp = "temporary";
+ id = map.get(temp);
+ temp.clear();
+ }
+ // clear() should not have changed id.
+ EXPECT_EQ(id.GetName(), "temporary");
+}
+
+// getWithStaticStorageStr() does not copy: it hands back an Identifier
+// wrapping the exact same backing storage as the (static-lifetime) input.
+TEST(IdentifierTest, GetWithStaticStorageStrDoesNotCopy) {
+ IdentifierMap map;
+ static const char *kStatic = "int";
+ Identifier id = map.getWithStaticStorageStr(kStatic);
+ EXPECT_EQ(id.GetName().data(), kStatic);
+}
+
+// Interning via get() and via getWithStaticStorageStr() with the same content
+// still uniques to the same underlying storage.
+TEST(IdentifierTest, GetAndStaticShareStorage) {
+ IdentifierMap map;
+ static const char *kStatic = "shared";
+ Identifier a = map.getWithStaticStorageStr(kStatic);
+ Identifier b = map.get("shared");
+ EXPECT_EQ(a.GetName().data(), b.GetName().data());
+}
More information about the lldb-commits
mailing list