Use string_view in quirk_rust_enum

Message ID 20240214150227.2367977-1-tromey@adacore.com
State New
Headers
Series Use string_view in quirk_rust_enum |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Tom Tromey Feb. 14, 2024, 3:02 p.m. UTC
  quirk_rust_enum makes string copies to store in an unordered_map.
However, the original strings have an appropriate lifetime, and so no
copying is needed.  With C++17, we can rely on string_view having a
std::hash specialization, so this patch changes this code to use
string_view rather than string.
---
 gdb/dwarf2/read.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Tom Tromey March 18, 2024, 2:11 p.m. UTC | #1
>>>>> "Tom" == Tom Tromey <tromey@adacore.com> writes:

Tom> quirk_rust_enum makes string copies to store in an unordered_map.
Tom> However, the original strings have an appropriate lifetime, and so no
Tom> copying is needed.  With C++17, we can rely on string_view having a
Tom> std::hash specialization, so this patch changes this code to use
Tom> string_view rather than string.

I'm checking this in.

Tom
  

Patch

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 486be7e4921..f9e80274618 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -5948,7 +5948,7 @@  quirk_rust_enum (struct type *type, struct objfile *objfile)
       /* We need a way to find the correct discriminant given a
 	 variant name.  For convenience we build a map here.  */
       struct type *enum_type = disr_field->type ();
-      std::unordered_map<std::string, ULONGEST> discriminant_map;
+      std::unordered_map<std::string_view, ULONGEST> discriminant_map;
       for (int i = 0; i < enum_type->num_fields (); ++i)
 	{
 	  if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)