[Lldb-commits] [lldb] [lldb] Add GetPointerDiffType to TypeSystemClang (PR #185314)

Ilia Kuklin via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 9 15:48:31 PDT 2026


https://github.com/kuilpd updated https://github.com/llvm/llvm-project/pull/185314

>From 084acacd819842799abc1ee7d6fc853fa22c5658 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Sun, 8 Mar 2026 23:34:03 +0500
Subject: [PATCH 1/3] [lldb] Add GetPointerDiffType to TypeSystemClang

---
 lldb/include/lldb/Symbol/TypeSystem.h                    | 2 ++
 lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 9 +++++++++
 lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h   | 2 ++
 lldb/unittests/Symbol/TestTypeSystemClang.cpp            | 7 +++++++
 4 files changed, 20 insertions(+)

diff --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h
index d7f4cfcf0ffc7..8ee7df40051bc 100644
--- a/lldb/include/lldb/Symbol/TypeSystem.h
+++ b/lldb/include/lldb/Symbol/TypeSystem.h
@@ -221,6 +221,8 @@ class TypeSystem : public PluginInterface,
 
   virtual uint32_t GetPointerByteSize() = 0;
 
+  virtual CompilerType GetPointerDiffType(bool is_signed) = 0;
+
   virtual unsigned GetPtrAuthKey(lldb::opaque_compiler_type_t type) = 0;
 
   virtual unsigned
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 0984d4d7190e7..514f4c5f21d11 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -2401,6 +2401,15 @@ CompilerType TypeSystemClang::GetPointerSizedIntType(bool is_signed) {
       getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed);
 }
 
+CompilerType TypeSystemClang::GetPointerDiffType(bool is_signed) {
+  if (!getASTContext().VoidPtrTy)
+    return {};
+
+  if (is_signed)
+    return GetType(getASTContext().getPointerDiffType());
+  return GetType(getASTContext().getUnsignedPointerDiffType());
+}
+
 void TypeSystemClang::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
   if (decl_ctx) {
     DumpDeclContextHiearchy(decl_ctx->getParent());
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
index aac1b5966f90c..e2c63741c82f8 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
@@ -510,6 +510,8 @@ class TypeSystemClang : public TypeSystem {
 
   CompilerType GetPointerSizedIntType(bool is_signed);
 
+  CompilerType GetPointerDiffType(bool is_signed) override;
+
   // Floating point functions
 
   static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast,
diff --git a/lldb/unittests/Symbol/TestTypeSystemClang.cpp b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
index 76abdd12d32a6..9b7cbf402a61a 100644
--- a/lldb/unittests/Symbol/TestTypeSystemClang.cpp
+++ b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
@@ -348,6 +348,8 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) {
                    .IsValid());
   EXPECT_FALSE(ast.GetPointerSizedIntType(/*is_signed=*/false));
   EXPECT_FALSE(ast.GetIntTypeFromBitSize(8, /*is_signed=*/false));
+  EXPECT_FALSE(ast.GetPointerDiffType(/*is_signed=*/true));
+  EXPECT_FALSE(ast.GetPointerDiffType(/*is_signed=*/false));
 
   CompilerType record_type =
       ast.CreateRecordType(nullptr, OptionalClangModuleID(), "Record",
@@ -360,6 +362,11 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) {
   TypeSystemClang::CompleteTagDeclarationDefinition(record_type);
 }
 
+TEST_F(TestTypeSystemClang, TestGetPointerDiffType) {
+  CompilerType ptrdiff_t = m_ast->GetPointerDiffType(/*is_signed=*/true);
+  EXPECT_EQ(ptrdiff_t.GetDisplayTypeName(), "__ptrdiff_t");
+}
+
 TEST_F(TestTypeSystemClang, TestGetBuiltinTypeByName_BitInt) {
   auto holder =
       std::make_unique<clang_utils::TypeSystemClangHolder>("bitint_ast");

>From a7dbcf05eeac55e49b203fe68be898f68048b7de Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Mon, 9 Mar 2026 00:11:58 +0500
Subject: [PATCH 2/3] Add unsigned ptrdiff_t test

---
 lldb/unittests/Symbol/TestTypeSystemClang.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lldb/unittests/Symbol/TestTypeSystemClang.cpp b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
index 9b7cbf402a61a..6911f02f4387e 100644
--- a/lldb/unittests/Symbol/TestTypeSystemClang.cpp
+++ b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
@@ -365,6 +365,12 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) {
 TEST_F(TestTypeSystemClang, TestGetPointerDiffType) {
   CompilerType ptrdiff_t = m_ast->GetPointerDiffType(/*is_signed=*/true);
   EXPECT_EQ(ptrdiff_t.GetDisplayTypeName(), "__ptrdiff_t");
+
+  CompilerType uptrdiff_t = m_ast->GetPointerDiffType(/*is_signed=*/false);
+  EXPECT_FALSE(uptrdiff_t.IsSigned());
+  EXPECT_EQ(
+      llvm::expectedToOptional(uptrdiff_t.GetByteSize(nullptr)).value_or(0),
+      m_ast->GetPointerByteSize());
 }
 
 TEST_F(TestTypeSystemClang, TestGetBuiltinTypeByName_BitInt) {

>From 7dff99070966ad28eb6888214d52e86a97ce22fb Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Tue, 10 Mar 2026 03:44:19 +0500
Subject: [PATCH 3/3] Add tests, add a comment

---
 lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 1 +
 lldb/unittests/Symbol/TestTypeSystemClang.cpp            | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 514f4c5f21d11..211630f24c7f4 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -2402,6 +2402,7 @@ CompilerType TypeSystemClang::GetPointerSizedIntType(bool is_signed) {
 }
 
 CompilerType TypeSystemClang::GetPointerDiffType(bool is_signed) {
+  // Check if builtin types are initialized.
   if (!getASTContext().VoidPtrTy)
     return {};
 
diff --git a/lldb/unittests/Symbol/TestTypeSystemClang.cpp b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
index 6911f02f4387e..d8d95f17e5104 100644
--- a/lldb/unittests/Symbol/TestTypeSystemClang.cpp
+++ b/lldb/unittests/Symbol/TestTypeSystemClang.cpp
@@ -365,6 +365,10 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) {
 TEST_F(TestTypeSystemClang, TestGetPointerDiffType) {
   CompilerType ptrdiff_t = m_ast->GetPointerDiffType(/*is_signed=*/true);
   EXPECT_EQ(ptrdiff_t.GetDisplayTypeName(), "__ptrdiff_t");
+  EXPECT_TRUE(ptrdiff_t.IsSigned());
+  EXPECT_EQ(
+      llvm::expectedToOptional(ptrdiff_t.GetByteSize(nullptr)).value_or(0),
+      m_ast->GetPointerByteSize());
 
   CompilerType uptrdiff_t = m_ast->GetPointerDiffType(/*is_signed=*/false);
   EXPECT_FALSE(uptrdiff_t.IsSigned());



More information about the lldb-commits mailing list