summaryrefslogtreecommitdiff
path: root/libcpp
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2019-12-10 02:02:38 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2019-12-10 02:02:38 +0000
commitd68f5d458d7df12b31d27094e8f7ed0208ccf693 (patch)
treec30d686a52fa2b8eecdade9221ca87d28f6f4909 /libcpp
parentd3e28653fa2cd9ad8d5c6acb4bd182bd473c0995 (diff)
Replace label_text ctor with "borrow" and "take"
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
Diffstat (limited to 'libcpp')
-rw-r--r--libcpp/ChangeLog7
-rw-r--r--libcpp/include/line-map.h31
2 files changed, 34 insertions, 4 deletions
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index f4376e6cf80..2090bd72103 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -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
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 6f4cf5b9c95..e78249fa930 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -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