diff options
author | David Malcolm <dmalcolm@redhat.com> | 2022-07-07 15:50:26 -0400 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2022-07-07 15:50:26 -0400 |
commit | a8dce13c076019688f6d6aaaa338a2911022b336 (patch) | |
tree | 29dd272c16cb106efa3b06b095ef233dc11e5fba /libcpp | |
parent | 897b3b31f0a94b8bac59c6061655c6a32646d0a0 (diff) | |
download | gcc-a8dce13c076019688f6d6aaaa338a2911022b336.zip gcc-a8dce13c076019688f6d6aaaa338a2911022b336.tar.gz gcc-a8dce13c076019688f6d6aaaa338a2911022b336.tar.bz2 |
Convert label_text to C++11 move semantics
libcpp's class label_text stores a char * for a string and a flag saying
whether it owns the buffer. I added this class before we could use
C++11, and so to avoid lots of copying it required an explicit call
to label_text::maybe_free to potentially free the buffer.
Now that we can use C++11, this patch removes label_text::maybe_free in
favor of doing the cleanup in the destructor, and using C++ move
semantics to avoid any copying. This allows lots of messy cleanup code
to be eliminated in favor of implicit destruction (mostly in the
analyzer).
No functional change intended.
gcc/analyzer/ChangeLog:
* call-info.cc (call_info::print): Update for removal of
label_text::maybe_free in favor of automatic memory management.
* checker-path.cc (checker_event::dump): Likewise.
(checker_event::prepare_for_emission): Likewise.
(state_change_event::get_desc): Likewise.
(superedge_event::should_filter_p): Likewise.
(start_cfg_edge_event::get_desc): Likewise.
(warning_event::get_desc): Likewise.
(checker_path::dump): Likewise.
(checker_path::debug): Likewise.
* diagnostic-manager.cc
(diagnostic_manager::prune_for_sm_diagnostic): Likewise.
(diagnostic_manager::prune_interproc_events): Likewise.
* program-state.cc (sm_state_map::to_json): Likewise.
* region.cc (region::to_json): Likewise.
* sm-malloc.cc (inform_nonnull_attribute): Likewise.
* store.cc (binding_map::to_json): Likewise.
(store::to_json): Likewise.
* svalue.cc (svalue::to_json): Likewise.
gcc/c-family/ChangeLog:
* c-format.cc (range_label_for_format_type_mismatch::get_text):
Update for removal of label_text::maybe_free in favor of automatic
memory management.
gcc/ChangeLog:
* diagnostic-format-json.cc (json_from_location_range): Update for
removal of label_text::maybe_free in favor of automatic memory
management.
* diagnostic-format-sarif.cc
(sarif_builder::make_location_object): Likewise.
* diagnostic-show-locus.cc (struct pod_label_text): New.
(class line_label): Convert m_text from label_text to pod_label_text.
(layout::print_any_labels): Move "text" to the line_label.
* tree-diagnostic-path.cc (path_label::get_text): Update for
removal of label_text::maybe_free in favor of automatic memory
management.
(event_range::print): Likewise.
(default_tree_diagnostic_path_printer): Likewise.
(default_tree_make_json_for_path): Likewise.
libcpp/ChangeLog:
* include/line-map.h: Include <utility>.
(class label_text): Delete maybe_free method in favor of a
destructor. Add move ctor and assignment operator. Add deletion
of the copy ctor and copy-assignment operator. Rename field
m_caller_owned to m_owned. Add std::move where necessary; add
moved_from member function.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'libcpp')
-rw-r--r-- | libcpp/include/line-map.h | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h index 8033572..c6379ce 100644 --- a/libcpp/include/line-map.h +++ b/libcpp/include/line-map.h @@ -22,6 +22,8 @@ along with this program; see the file COPYING3. If not see #ifndef LIBCPP_LINE_MAP_H #define LIBCPP_LINE_MAP_H +#include <utility> + #ifndef GTY #define GTY(x) /* nothing */ #endif @@ -1836,43 +1838,71 @@ class label_text { public: label_text () - : m_buffer (NULL), m_caller_owned (false) + : m_buffer (NULL), m_owned (false) {} - void maybe_free () + ~label_text () + { + if (m_owned) + free (m_buffer); + } + + /* Move ctor. */ + label_text (label_text &&other) + : m_buffer (other.m_buffer), m_owned (other.m_owned) + { + other.moved_from (); + } + + /* Move assignment. */ + label_text & operator= (label_text &&other) { - if (m_caller_owned) + if (m_owned) free (m_buffer); + m_buffer = other.m_buffer; + m_owned = other.m_owned; + other.moved_from (); + return *this; } + /* Delete the copy ctor and copy-assignment operator. */ + label_text (const label_text &) = delete; + label_text & operator= (const label_text &) = delete; + /* 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); + return std::move (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); + return std::move (label_text (buffer, true)); } /* Take ownership of the buffer, copying if necessary. */ char *take_or_copy () { - if (m_caller_owned) + if (m_owned) return m_buffer; else return xstrdup (m_buffer); } + void moved_from () + { + m_buffer = NULL; + m_owned = false; + } + char *m_buffer; - bool m_caller_owned; + bool m_owned; private: label_text (char *buffer, bool owned) - : m_buffer (buffer), m_caller_owned (owned) + : m_buffer (buffer), m_owned (owned) {} }; |