libcpp: Improve encapsulation of label_text

This adjusts the API of label_text so that the data members are private
and cannot be modified by callers.  Add accessors for them instead, and
make the accessors const-correct.  Also rename moved_from () to the more
idiomatic release ().  Also remove the unused take_or_copy () member
function which has confusing ownership semantics.

gcc/analyzer/ChangeLog:

	* call-info.cc (call_info::print): Adjust to new label_text API.
	* checker-path.cc (checker_event::dump): Likewise.
	(region_creation_event::get_desc): Likewise.
	(state_change_event::get_desc): Likewise.
	(superedge_event::should_filter_p): Likewise.
	(start_cfg_edge_event::get_desc): Likewise.
	(call_event::get_desc): Likewise.
	(return_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.
	* engine.cc (feasibility_state::maybe_update_for_edge):
	Likewise.
	* program-state.cc (sm_state_map::to_json): Likewise.
	* region-model-impl-calls.cc (region_model::impl_call_analyzer_describe): Likewise.
	(region_model::impl_call_analyzer_dump_capacity): 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.
	* supergraph.cc (superedge::dump): Likewise.
	* svalue.cc (svalue::to_json): Likewise.

gcc/c-family/ChangeLog:

	* c-format.cc (class range_label_for_format_type_mismatch):
	Adjust to new label_text API.

gcc/ChangeLog:

	* diagnostic-format-json.cc (json_from_location_range): Adjust
	to new label_text API.
	* diagnostic-format-sarif.cc (sarif_builder::make_location_object):
	Likewise.
	* diagnostic-show-locus.cc (struct pod_label_text): Likewise.
	(layout::print_any_labels): Likewise.
	* tree-diagnostic-path.cc (class path_label): Likewise.
	(struct event_range): Likewise.
	(default_tree_diagnostic_path_printer): Likewise.
	(default_tree_make_json_for_path): Likewise.

libcpp/ChangeLog:

	* include/line-map.h (label_text::take_or_copy): Remove.
	(label_text::moved_from): Rename to release.
	(label_text::m_buffer, label_text::m_owned): Make private.
	(label_text::get, label_text::is_owned): New accessors.
This commit is contained in:
Jonathan Wakely
2022-07-13 11:58:05 +01:00
parent ae69e6f61b
commit f858fe7a8b
17 changed files with 81 additions and 79 deletions
+14 -13
View File
@@ -1851,7 +1851,7 @@ public:
label_text (label_text &&other)
: m_buffer (other.m_buffer), m_owned (other.m_owned)
{
other.moved_from ();
other.release ();
}
/* Move assignment. */
@@ -1861,7 +1861,7 @@ public:
free (m_buffer);
m_buffer = other.m_buffer;
m_owned = other.m_owned;
other.moved_from ();
other.release ();
return *this;
}
@@ -1882,25 +1882,26 @@ public:
return label_text (buffer, true);
}
/* Take ownership of the buffer, copying if necessary. */
char *take_or_copy ()
{
if (m_owned)
return m_buffer;
else
return xstrdup (m_buffer);
}
void moved_from ()
void release ()
{
m_buffer = NULL;
m_owned = false;
}
const char *get () const
{
return m_buffer;
}
bool is_owner () const
{
return m_owned;
}
private:
char *m_buffer;
bool m_owned;
private:
label_text (char *buffer, bool owned)
: m_buffer (buffer), m_owned (owned)
{}