[Lldb-commits] [lldb] [DRAFT][lldb] Upgrade CompilerType::GetIndexOfFieldWithName to return llvm::Expected (PR #135963)

via lldb-commits lldb-commits at lists.llvm.org
Wed Apr 16 06:55:44 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Charles Zablit (charles-zablit)

<details>
<summary>Changes</summary>

This patch updates the `CompilerType::GetIndexOfFieldWithName` API to use `llvm::Expected` if no index is found instead of `UINT32_MAX`.

---
Full diff: https://github.com/llvm/llvm-project/pull/135963.diff


3 Files Affected:

- (modified) lldb/include/lldb/Symbol/CompilerType.h (+6-5) 
- (modified) lldb/source/Symbol/CompilerType.cpp (+2-2) 
- (modified) lldb/unittests/Platform/PlatformSiginfoTest.cpp (+4-3) 


``````````diff
diff --git a/lldb/include/lldb/Symbol/CompilerType.h b/lldb/include/lldb/Symbol/CompilerType.h
index 41a1676dabd76..79998922cfc93 100644
--- a/lldb/include/lldb/Symbol/CompilerType.h
+++ b/lldb/include/lldb/Symbol/CompilerType.h
@@ -433,11 +433,12 @@ class CompilerType {
 
   CompilerDecl GetStaticFieldWithName(llvm::StringRef name) const;
 
-  uint32_t GetIndexOfFieldWithName(const char *name,
-                                   CompilerType *field_compiler_type = nullptr,
-                                   uint64_t *bit_offset_ptr = nullptr,
-                                   uint32_t *bitfield_bit_size_ptr = nullptr,
-                                   bool *is_bitfield_ptr = nullptr) const;
+  llvm::Expected<uint32_t>
+  GetIndexOfFieldWithName(const char *name,
+                          CompilerType *field_compiler_type = nullptr,
+                          uint64_t *bit_offset_ptr = nullptr,
+                          uint32_t *bitfield_bit_size_ptr = nullptr,
+                          bool *is_bitfield_ptr = nullptr) const;
 
   llvm::Expected<CompilerType> GetChildCompilerTypeAtIndex(
       ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
diff --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp
index 22fdd24fc7cd5..0a36b390a645c 100644
--- a/lldb/source/Symbol/CompilerType.cpp
+++ b/lldb/source/Symbol/CompilerType.cpp
@@ -893,7 +893,7 @@ CompilerDecl CompilerType::GetStaticFieldWithName(llvm::StringRef name) const {
   return CompilerDecl();
 }
 
-uint32_t CompilerType::GetIndexOfFieldWithName(
+llvm::Expected<uint32_t> CompilerType::GetIndexOfFieldWithName(
     const char *name, CompilerType *field_compiler_type_ptr,
     uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
     bool *is_bitfield_ptr) const {
@@ -909,7 +909,7 @@ uint32_t CompilerType::GetIndexOfFieldWithName(
       return index;
     }
   }
-  return UINT32_MAX;
+  return llvm::createStringError("Invalid name: Cannot find index");
 }
 
 llvm::Expected<CompilerType> CompilerType::GetChildCompilerTypeAtIndex(
diff --git a/lldb/unittests/Platform/PlatformSiginfoTest.cpp b/lldb/unittests/Platform/PlatformSiginfoTest.cpp
index 4b2c93a68a94a..e48d8ea667ad8 100644
--- a/lldb/unittests/Platform/PlatformSiginfoTest.cpp
+++ b/lldb/unittests/Platform/PlatformSiginfoTest.cpp
@@ -60,9 +60,10 @@ class PlatformSiginfoTest : public ::testing::Test {
     uint64_t total_offset = 0;
     for (auto field_name : llvm::split(path, '.')) {
       uint64_t bit_offset;
-      ASSERT_NE(field_type.GetIndexOfFieldWithName(field_name.str().c_str(),
-                                                   &field_type, &bit_offset),
-                UINT32_MAX);
+      ASSERT(llvm::expectedToOptional(
+                 field_type.GetIndexOfFieldWithName(field_name.str().c_str(),
+                                                    &field_type, &bit_offset))
+                 .has_value());
       total_offset += bit_offset;
     }
 

``````````

</details>


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


More information about the lldb-commits mailing list