From d68f5d458d7df12b31d27094e8f7ed0208ccf693 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 10 Dec 2019 02:02:38 +0000 Subject: [PATCH] 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 --- gcc/ChangeLog | 8 ++++++++ gcc/c-family/ChangeLog | 5 +++++ gcc/c-family/c-format.c | 2 +- gcc/c/ChangeLog | 5 +++++ gcc/c/c-objc-common.c | 4 ++-- gcc/cp/ChangeLog | 5 +++++ gcc/cp/error.c | 4 ++-- gcc/gcc-rich-location.c | 2 +- gcc/gcc-rich-location.h | 2 +- libcpp/ChangeLog | 7 +++++++ libcpp/include/line-map.h | 31 +++++++++++++++++++++++++++---- 11 files changed, 64 insertions(+), 11 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 704403dabc1..42ae999103b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2019-12-09 David Malcolm + + * 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 * diagnostic-show-locus.c (diagnostic_show_locus): Remove initial diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index 09d7a040726..9d488555c42 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2019-12-09 David Malcolm + + * 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 * c-format.c (selftest::test_type_mismatch_range_labels): Remove diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index 0be13496d8f..c5fefd5707f 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -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: diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index f9c6293d9e5..5371e9cc186 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,8 @@ +2019-12-09 David Malcolm + + * c-objc-common.c (range_label_for_type_mismatch::get_text): + Replace label_text ctor calls. + 2019-12-04 Joseph Myers PR c/36941 diff --git a/gcc/c/c-objc-common.c b/gcc/c/c-objc-common.c index 10d72c57dfb..665c7a67eb2 100644 --- a/gcc/c/c-objc-common.c +++ b/gcc/c/c-objc-common.c @@ -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, "ed); - return label_text (xstrdup (pp_formatted_text (&cpp)), true); + return label_text::take (xstrdup (pp_formatted_text (&cpp))); } diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index b39a304b17d..51407878bba 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2019-12-09 David Malcolm + + * error.c (range_label_for_type_mismatch::get_text): Replace + label_text ctor calls with label_text::borrow. + 2019-12-09 Paolo Carlini * typeck.c (check_for_casting_away_constness): Add location_t diff --git a/gcc/cp/error.c b/gcc/cp/error.c index a15230a1f01..7c46c17c1e1 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -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 (result), false); + return label_text::borrow (result); } diff --git a/gcc/gcc-rich-location.c b/gcc/gcc-rich-location.c index 82d4f52b2d4..071463ee513 100644 --- a/gcc/gcc-rich-location.c +++ b/gcc/gcc-rich-location.c @@ -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; diff --git a/gcc/gcc-rich-location.h b/gcc/gcc-rich-location.h index 3bee2e82ad5..71f4f3d4207 100644 --- a/gcc/gcc-rich-location.h +++ b/gcc/gcc-rich-location.h @@ -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 (m_text), false); + return label_text::borrow (m_text); } private: 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 + + * 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 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 (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 -- 2.30.2