Replace label_text ctor with "borrow" and "take"
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 10 Dec 2019 02:02:38 +0000 (02:02 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Tue, 10 Dec 2019 02:02:38 +0000 (02:02 +0000)
libcpp's label_text class wraps a text buffer, along with a flag to
determine if it "owns" the buffer.

The existing ctor exposed this directly, but I found it difficult
to remember the sense of flag, so this patch hides the ctor, in
favor of static member functions "borrow" and "take", to make
the effect on ownership explicit in the name.

gcc/c-family/ChangeLog:
* c-format.c (range_label_for_format_type_mismatch::get_text):
Replace label_text ctor called with true with label_text::take.

gcc/c/ChangeLog:
* c-objc-common.c (range_label_for_type_mismatch::get_text):
Replace label_text ctor calls.

gcc/cp/ChangeLog:
* error.c (range_label_for_type_mismatch::get_text): Replace
label_text ctor calls with label_text::borrow.

gcc/ChangeLog:
* gcc-rich-location.c
(maybe_range_label_for_tree_type_mismatch::get_text): Replace
label_text ctor call with label_text::borrow.
* gcc-rich-location.h (text_range_label::get_text): Replace
label_text ctor called with false with label_text::borrow.

libcpp/ChangeLog:
* include/line-map.h (label_text::label_text): Make private.
(label_text::borrow): New.
(label_text::take): New.
(label_text::take_or_copy): New.

From-SVN: r279153

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-format.c
gcc/c/ChangeLog
gcc/c/c-objc-common.c
gcc/cp/ChangeLog
gcc/cp/error.c
gcc/gcc-rich-location.c
gcc/gcc-rich-location.h
libcpp/ChangeLog
libcpp/include/line-map.h

index 704403dabc12a309c9de9a3c7233ac59b3e6f56d..42ae999103b7690a956e5a8d970dd8bc5c330679 100644 (file)
@@ -1,3 +1,11 @@
+2019-12-09  David Malcolm  <dmalcolm@redhat.com>
+
+       * gcc-rich-location.c
+       (maybe_range_label_for_tree_type_mismatch::get_text): Replace
+       label_text ctor call with label_text::borrow.
+       * gcc-rich-location.h (text_range_label::get_text): Replace
+       label_text ctor called with false with label_text::borrow.
+
 2019-12-09  David Malcolm  <dmalcolm@redhat.com>
 
        * diagnostic-show-locus.c (diagnostic_show_locus): Remove initial
index 09d7a040726bda53fba041a4809ca9fd7f2fc2d1..9d488555c42726f667d5c7cbabaf5b06dc052dda 100644 (file)
@@ -1,3 +1,8 @@
+2019-12-09  David Malcolm  <dmalcolm@redhat.com>
+
+       * c-format.c (range_label_for_format_type_mismatch::get_text):
+       Replace label_text ctor called with true with label_text::take.
+
 2019-12-09  David Malcolm  <dmalcolm@redhat.com>
 
        * c-format.c (selftest::test_type_mismatch_range_labels): Remove
index 0be13496d8f183473970f1b74c34b02c099eef46..c5fefd5707f1f5191c078a1583572d5deee572bd 100644 (file)
@@ -4629,7 +4629,7 @@ class range_label_for_format_type_mismatch
 
     char *result = concat (text.m_buffer, p, NULL);
     text.maybe_free ();
-    return label_text (result, true);
+    return label_text::take (result);
   }
 
  private:
index f9c6293d9e5ec1f221be8e6d3bcc8484d6f5948b..5371e9cc1863a5f4af9a24c9caa70c639c9008ea 100644 (file)
@@ -1,3 +1,8 @@
+2019-12-09  David Malcolm  <dmalcolm@redhat.com>
+
+       * c-objc-common.c (range_label_for_type_mismatch::get_text):
+       Replace label_text ctor calls.
+
 2019-12-04  Joseph Myers  <joseph@codesourcery.com>
 
        PR c/36941
index 10d72c57dfb829d4395e3516fccc1657aaa7e0e0..665c7a67eb22c4f93b4a82a6b5a5a1c143b6debd 100644 (file)
@@ -340,12 +340,12 @@ label_text
 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
 {
   if (m_labelled_type == NULL_TREE)
-    return label_text (NULL, false);
+    return label_text::borrow (NULL);
 
   c_pretty_printer cpp;
   bool quoted = false;
   print_type (&cpp, m_labelled_type, &quoted);
-  return label_text (xstrdup (pp_formatted_text (&cpp)), true);
+  return label_text::take (xstrdup (pp_formatted_text (&cpp)));
 }
 
 
index b39a304b17d692b471cc36b6cc7571ff5a12da0c..51407878bbaa16163b1957999db6b6ff79c2cfe4 100644 (file)
@@ -1,3 +1,8 @@
+2019-12-09  David Malcolm  <dmalcolm@redhat.com>
+
+       * error.c (range_label_for_type_mismatch::get_text): Replace
+       label_text ctor calls with label_text::borrow.
+
 2019-12-09  Paolo Carlini  <paolo.carlini@oracle.com>
 
        * typeck.c (check_for_casting_away_constness): Add location_t
index a15230a1f012af34689b836acc99e5ff9885d033..7c46c17c1e18d30b000526cd9c699c525262f891 100644 (file)
@@ -4528,7 +4528,7 @@ label_text
 range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
 {
   if (m_labelled_type == NULL_TREE)
-    return label_text (NULL, false);
+    return label_text::borrow (NULL);
 
   const bool verbose = false;
   const bool show_color = false;
@@ -4543,5 +4543,5 @@ range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
 
   /* Both of the above return GC-allocated buffers, so the caller mustn't
      free them.  */
-  return label_text (const_cast <char *> (result), false);
+  return label_text::borrow (result);
 }
index 82d4f52b2d420d0b672e0b5d7bb829c04ddcc9e9..071463ee513b12304fa21dc7062bea9bac739fe7 100644 (file)
@@ -196,7 +196,7 @@ maybe_range_label_for_tree_type_mismatch::get_text (unsigned range_idx) const
 {
   if (m_expr == NULL_TREE
       || !EXPR_P (m_expr))
-    return label_text (NULL, false);
+    return label_text::borrow (NULL);
   tree expr_type = TREE_TYPE (m_expr);
 
   tree other_type = NULL_TREE;
index 3bee2e82ad5ccdbc68ac27f83bea8df372869a4f..71f4f3d4207730f74552fee175246ae68da9d7a3 100644 (file)
@@ -111,7 +111,7 @@ class text_range_label : public range_label
 
   label_text get_text (unsigned /*range_idx*/) const FINAL OVERRIDE
   {
-    return label_text (const_cast <char *> (m_text), false);
+    return label_text::borrow (m_text);
   }
 
  private:
index f4376e6cf80f8c85b88691b8bc4ff9f6c31b5e02..2090bd72103c16de6bc266130a9812bce0045063 100644 (file)
@@ -1,3 +1,10 @@
+2019-12-09  David Malcolm  <dmalcolm@redhat.com>
+
+       * include/line-map.h (label_text::label_text): Make private.
+       (label_text::borrow): New.
+       (label_text::take): New.
+       (label_text::take_or_copy): New.
+
 2019-12-09  Lewis Hyatt  <lhyatt@gmail.com>
 
        PR preprocessor/49973
index 6f4cf5b9c95683f89f240b39f252bcad10e277dd..e78249fa93069a19bf1dcef48a67621fc995cba8 100644 (file)
@@ -1764,18 +1764,41 @@ public:
   : m_buffer (NULL), m_caller_owned (false)
   {}
 
-  label_text (char *buffer, bool caller_owned)
-  : m_buffer (buffer), m_caller_owned (caller_owned)
-  {}
-
   void maybe_free ()
   {
     if (m_caller_owned)
       free (m_buffer);
   }
 
+  /* Create a label_text instance that borrows BUFFER from a
+     longer-lived owner.  */
+  static label_text borrow (const char *buffer)
+  {
+    return label_text (const_cast <char *> (buffer), false);
+  }
+
+  /* Create a label_text instance that takes ownership of BUFFER.  */
+  static label_text take (char *buffer)
+  {
+    return label_text (buffer, true);
+  }
+
+  /* Take ownership of the buffer, copying if necessary.  */
+  char *take_or_copy ()
+  {
+    if (m_caller_owned)
+      return m_buffer;
+    else
+      return xstrdup (m_buffer);
+  }
+
   char *m_buffer;
   bool m_caller_owned;
+
+private:
+  label_text (char *buffer, bool owned)
+  : m_buffer (buffer), m_caller_owned (owned)
+  {}
 };
 
 /* Abstract base class for labelling a range within a rich_location