Skip to content

[lldb] Remove CompilerType::GetIndexOfFieldWithName #135963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

charles-zablit
Copy link
Contributor

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

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the lldb label Apr 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-lldb

Author: Charles Zablit (charles-zablit)

Changes

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 --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;
     }
 

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this API used for anything outside of that PlatformSigInfoTest? It doesn't seem to be used anywhere (including the apple fork). Should we just remove this API?

If we do want to keep it, we could add a test for this in lldb/unittests/Symbol/TestTypeSystemClang.cpp. You can create a custom CompilerType (search in that file for CreateRecordType) and then call GetIndexOfFieldWithName on it with some non-existent field name.

@charles-zablit
Copy link
Contributor Author

Is this API used for anything outside of that PlatformSigInfoTest? It doesn't seem to be used anywhere (including the apple fork). Should we just remove this API?

It's not used anywhere apart from the PlatformSigInfoTest. It's also inside the lldb_private namespace so I agree, I think it's best to remove it.

@charles-zablit charles-zablit force-pushed the charles-zablit/lldb/upgrade-compilertype-getindexoffieldwithname branch from aaf9c4e to 6dd67fe Compare April 16, 2025 15:32
@felipepiovezan
Copy link
Contributor

felipepiovezan commented Apr 16, 2025

It's worth mentioning that the LLVM convention (as opposed to what is done in the swift fork) is to avoid force-pushing.
Instead, you'd always add a new fixup commit (git commit --fixup) and squash them when merging (note that, for llvm, github will force you into squashing).

This obviously raises the question of: "what if I want to have small, independent commits?".
No good solutions, I'm afraid, other than stacked PRs and scrips that help with that.

@charles-zablit
Copy link
Contributor Author

It's worth mentioning that the LLVM convention (as opposed to what is done in the swift fork) is to avoid force-pushing.

Thanks! I will avoid that for the future PRs 👍

@charles-zablit charles-zablit changed the title [DRAFT][lldb] Upgrade CompilerType::GetIndexOfFieldWithName to return llvm::Expected [lldb] Upgrade CompilerType::GetIndexOfFieldWithName to return llvm::Expected Apr 16, 2025
@charles-zablit charles-zablit changed the title [lldb] Upgrade CompilerType::GetIndexOfFieldWithName to return llvm::Expected [lldb] Remove CompilerType::GetIndexOfFieldWithName Apr 16, 2025
@Michael137
Copy link
Member

We probably shouldnt be removing the test. Is there some way to test whatever we used to test without the API?

@charles-zablit
Copy link
Contributor Author

We probably shouldnt be removing the test. Is there some way to test whatever we used to test without the API?

I see 2 options:

  1. Move the body of CompilerType::GetIndexOfFieldWithName into the test directly. This way, we remove the API but keep the test.
  2. Keep the API, use llvm::Expected and keep the test.

I feel like 1 would be hiding the API, and I would therefore prefer 2.

@Michael137
Copy link
Member

We probably shouldnt be removing the test. Is there some way to test whatever we used to test without the API?

I see 2 options:

  1. Move the body of CompilerType::GetIndexOfFieldWithName into the test directly. This way, we remove the API but keep the test.
  2. Keep the API, use llvm::Expected and keep the test.

I feel like 1 would be hiding the API, and I would therefore prefer 2.

I think the test can just do this:

      uint64_t bit_offset;
      std::string name;
      field_type = field_type.GetFieldAtIndex(
              field_type.GetIndexOfChildWithName(field_name, /*omit_empty_base_classes=*/false),
              name, &bit_offset, nullptr, nullptr);
      ASSERT_TRUE(field_type);

Instead of using CompilerType::GetIndexOfFieldWithName (though I haven't actually tried to compile/run this)

Don't have a strong opinion on whether to remove or extend the API. Personally I prefer removing it just because we already have so many similarly named APIs across CompilerType/TypeSystemClang that do things slightly differently, that it would be nice to get rid of at least one of them.

@charles-zablit
Copy link
Contributor Author

I think the test can just do this:

      uint64_t bit_offset;
      std::string name;
      field_type = field_type.GetFieldAtIndex(
              field_type.GetIndexOfChildWithName(field_name, /*omit_empty_base_classes=*/false),
              name, &bit_offset, nullptr, nullptr);
      ASSERT_TRUE(field_type);

Tested this locally and the tests pass, I have updated the commit.

Instead of using CompilerType::GetIndexOfFieldWithName (though I haven't actually tried to compile/run this)

Don't have a strong opinion on whether to remove or extend the API. Personally I prefer removing it just because we already have so many similarly named APIs across CompilerType/TypeSystemClang that do things slightly differently, that it would be nice to get rid of at least one of them.

Ended up removing the API and used your suggestion to define the test without the API. Thanks!

@charles-zablit charles-zablit force-pushed the charles-zablit/lldb/upgrade-compilertype-getindexoffieldwithname branch from 57344b2 to 4d2c45a Compare April 17, 2025 10:05
@charles-zablit
Copy link
Contributor Author

Sorry I forgot to run clang-format I will configure a pre-commit hook.

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@Michael137 Michael137 merged commit f135ce6 into llvm:main Apr 17, 2025
8 of 10 checks passed
Copy link

@charles-zablit Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request May 8, 2025
This patch removes the unused `CompilerType::GetIndexOfFieldWithName` API (it wasn't used apart from in a single testcase). Given we have so many similarly named APIs already, it's best not to maintain this API that's not really used (and isnt tested).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants