More const-correctness in cooked indexer

Message ID 20230127162205.4125816-1-tromey@adacore.com
State New
Headers
Series More const-correctness in cooked indexer |

Commit Message

Tom Tromey Jan. 27, 2023, 4:22 p.m. UTC
  I noticed that iterating over the index yields non-const
cooked_index_entry objects.  However, after finalization, they should
not be modified.  This patch enforces this by adding const where
needed.
---
 gdb/dwarf2/cooked-index.c | 4 ++--
 gdb/dwarf2/cooked-index.h | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)
  

Comments

Simon Marchi Jan. 27, 2023, 8:11 p.m. UTC | #1
On 1/27/23 11:22, Tom Tromey via Gdb-patches wrote:
> I noticed that iterating over the index yields non-const
> cooked_index_entry objects.  However, after finalization, they should
> not be modified.  This patch enforces this by adding const where
> needed.

I think you could constify these methods at the same time (it would help
me for my dump patch):

- cooked_index::wait
- cooked_index::all_entries
- cooked_index_vector::wait
- cooked_index_vector::all_entries

Otherwise, LGTM.

Simon
  
Tom Tromey Jan. 27, 2023, 9:11 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

Simon> On 1/27/23 11:22, Tom Tromey via Gdb-patches wrote:
>> I noticed that iterating over the index yields non-const
>> cooked_index_entry objects.  However, after finalization, they should
>> not be modified.  This patch enforces this by adding const where
>> needed.

Simon> I think you could constify these methods at the same time (it would help
Simon> me for my dump patch):

Simon> - cooked_index::wait
Simon> - cooked_index::all_entries
Simon> - cooked_index_vector::wait
Simon> - cooked_index_vector::all_entries

I also made find const.  I'm going to check in the appended.

Tom

commit bd2e1e5511ac7c267544e5ce17b8dd41057d7fdb
Author: Tom Tromey <tromey@adacore.com>
Date:   Fri Jan 27 09:20:43 2023 -0700

    More const-correctness in cooked indexer
    
    I noticed that iterating over the index yields non-const
    cooked_index_entry objects.  However, after finalization, they should
    not be modified.  This patch enforces this by adding const where
    needed.
    
    v2 makes the find, all_entries, and wait methods const as well.

diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c
index 09b3fd70b26..1a568e6aae7 100644
--- a/gdb/dwarf2/cooked-index.c
+++ b/gdb/dwarf2/cooked-index.c
@@ -355,11 +355,11 @@ cooked_index::do_finalize ()
 /* See cooked-index.h.  */
 
 cooked_index::range
-cooked_index::find (const std::string &name, bool completing)
+cooked_index::find (const std::string &name, bool completing) const
 {
   wait ();
 
-  auto lower = std::lower_bound (m_entries.begin (), m_entries.end (), name,
+  auto lower = std::lower_bound (m_entries.cbegin (), m_entries.cend (), name,
 				 [=] (const cooked_index_entry *entry,
 				      const std::string &n)
   {
@@ -367,7 +367,7 @@ cooked_index::find (const std::string &name, bool completing)
 					completing);
   });
 
-  auto upper = std::upper_bound (m_entries.begin (), m_entries.end (), name,
+  auto upper = std::upper_bound (m_entries.cbegin (), m_entries.cend (), name,
 				 [=] (const std::string &n,
 				      const cooked_index_entry *entry)
   {
@@ -413,7 +413,7 @@ cooked_index_vector::get_addrmaps ()
 /* See cooked-index.h.  */
 
 cooked_index_vector::range
-cooked_index_vector::find (const std::string &name, bool completing)
+cooked_index_vector::find (const std::string &name, bool completing) const
 {
   std::vector<cooked_index::range> result_range;
   result_range.reserve (m_vector.size ());
diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h
index 55eaf9955ab..e12376cdf98 100644
--- a/gdb/dwarf2/cooked-index.h
+++ b/gdb/dwarf2/cooked-index.h
@@ -217,7 +217,7 @@ class cooked_index
   void finalize ();
 
   /* Wait for this index's finalization to be complete.  */
-  void wait ()
+  void wait () const
   {
     m_future.wait ();
   }
@@ -225,19 +225,20 @@ class cooked_index
   friend class cooked_index_vector;
 
   /* A simple range over part of m_entries.  */
-  typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
+  typedef iterator_range<std::vector<cooked_index_entry *>::const_iterator>
+       range;
 
   /* Return a range of all the entries.  */
-  range all_entries ()
+  range all_entries () const
   {
     wait ();
-    return { m_entries.begin (), m_entries.end () };
+    return { m_entries.cbegin (), m_entries.cend () };
   }
 
   /* Look up an entry by name.  Returns a range of all matching
      results.  If COMPLETING is true, then a larger range, suitable
      for completion, will be returned.  */
-  range find (const std::string &name, bool completing);
+  range find (const std::string &name, bool completing) const;
 
 private:
 
@@ -317,7 +318,7 @@ class cooked_index_vector : public dwarf_scanner_base
 
   /* Wait until the finalization of the entire cooked_index_vector is
      done.  */
-  void wait ()
+  void wait () const
   {
     for (auto &item : m_vector)
       item->wait ();
@@ -340,10 +341,10 @@ class cooked_index_vector : public dwarf_scanner_base
   /* Look up an entry by name.  Returns a range of all matching
      results.  If COMPLETING is true, then a larger range, suitable
      for completion, will be returned.  */
-  range find (const std::string &name, bool completing);
+  range find (const std::string &name, bool completing) const;
 
   /* Return a range of all the entries.  */
-  range all_entries ()
+  range all_entries () const
   {
     std::vector<cooked_index::range> result_range;
     result_range.reserve (m_vector.size ());
  

Patch

diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c
index 09b3fd70b26..bc4fe20aa2a 100644
--- a/gdb/dwarf2/cooked-index.c
+++ b/gdb/dwarf2/cooked-index.c
@@ -359,7 +359,7 @@  cooked_index::find (const std::string &name, bool completing)
 {
   wait ();
 
-  auto lower = std::lower_bound (m_entries.begin (), m_entries.end (), name,
+  auto lower = std::lower_bound (m_entries.cbegin (), m_entries.cend (), name,
 				 [=] (const cooked_index_entry *entry,
 				      const std::string &n)
   {
@@ -367,7 +367,7 @@  cooked_index::find (const std::string &name, bool completing)
 					completing);
   });
 
-  auto upper = std::upper_bound (m_entries.begin (), m_entries.end (), name,
+  auto upper = std::upper_bound (m_entries.cbegin (), m_entries.cend (), name,
 				 [=] (const std::string &n,
 				      const cooked_index_entry *entry)
   {
diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h
index 55eaf9955ab..cb6795b97a7 100644
--- a/gdb/dwarf2/cooked-index.h
+++ b/gdb/dwarf2/cooked-index.h
@@ -225,13 +225,14 @@  class cooked_index
   friend class cooked_index_vector;
 
   /* A simple range over part of m_entries.  */
-  typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
+  typedef iterator_range<std::vector<cooked_index_entry *>::const_iterator>
+       range;
 
   /* Return a range of all the entries.  */
   range all_entries ()
   {
     wait ();
-    return { m_entries.begin (), m_entries.end () };
+    return { m_entries.cbegin (), m_entries.cend () };
   }
 
   /* Look up an entry by name.  Returns a range of all matching