[PATCH 2/5] Add range_idx param to range_label::get_text
David Malcolm
dmalcolm@redhat.com
Fri Sep 14 17:31:00 GMT 2018
This patch updates the pure virtual function range_label::get_text
(and its implementations) so that the index of the range is passed
in, allowing for one label instance to be shared by multiple ranges.
gcc/c-family/ChangeLog:
* c-format.c (range_label_for_format_type_mismatch::get_text):
Update for new param.
gcc/c/ChangeLog:
* c-objc-common.c (range_label_for_type_mismatch::get_text):
Update for new param.
* c-typeck.c (maybe_range_label_for_tree_type_mismatch::get_text):
Likewise.
gcc/cp/ChangeLog:
* error.c (range_label_for_type_mismatch::get_text): Update for
new param.
gcc/ChangeLog:
* diagnostic-show-locus.c (class layout_range): Add field
"m_original_idx".
(layout_range::layout_range): Add "original_idx" param and use it
to initialize new field.
(make_range): Use 0 for original_idx.
(layout::layout): Pass in index to calls to
maybe_add_location_range.
(layout::maybe_add_location_range): Add param "original_idx" and
pass it on to layout_range.
(layout::print_any_labels): Pass on range->m_original_idx to
get_text call.
(gcc_rich_location::add_location_if_nearby): Use 0 for
original_idx.
* gcc-rich-location.h (text_range_label::get_text): Update for new
param.
(range_label_for_type_mismatch::get_text): Likewise.
libcpp/ChangeLog:
* include/line-map.h (range_label::get_text): Add param
"range_idx".
---
gcc/c-family/c-format.c | 4 ++--
gcc/c/c-objc-common.c | 2 +-
gcc/c/c-typeck.c | 4 ++--
gcc/cp/error.c | 2 +-
gcc/diagnostic-show-locus.c | 19 ++++++++++++++-----
gcc/gcc-rich-location.h | 4 ++--
libcpp/include/line-map.h | 6 ++++--
7 files changed, 26 insertions(+), 15 deletions(-)
diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index a7edfca..a1133c7 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -3597,9 +3597,9 @@ class range_label_for_format_type_mismatch
{
}
- label_text get_text () const FINAL OVERRIDE
+ label_text get_text (unsigned range_idx) const FINAL OVERRIDE
{
- label_text text = range_label_for_type_mismatch::get_text ();
+ label_text text = range_label_for_type_mismatch::get_text (range_idx);
if (text.m_buffer == NULL)
return text;
diff --git a/gcc/c/c-objc-common.c b/gcc/c/c-objc-common.c
index 12e777a..fee5268 100644
--- a/gcc/c/c-objc-common.c
+++ b/gcc/c/c-objc-common.c
@@ -218,7 +218,7 @@ c_tree_printer (pretty_printer *pp, text_info *text, const char *spec,
range_label_for_type_mismatch. */
label_text
-range_label_for_type_mismatch::get_text () const
+range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
{
if (m_labelled_type == NULL_TREE)
return label_text (NULL, false);
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 5f8df12..5388ddb 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -11049,7 +11049,7 @@ class maybe_range_label_for_tree_type_mismatch : public range_label
{
}
- label_text get_text () const FINAL OVERRIDE
+ label_text get_text (unsigned range_idx) const FINAL OVERRIDE
{
if (m_expr == NULL_TREE
|| !EXPR_P (m_expr))
@@ -11061,7 +11061,7 @@ class maybe_range_label_for_tree_type_mismatch : public range_label
other_type = TREE_TYPE (m_other_expr);
range_label_for_type_mismatch inner (expr_type, other_type);
- return inner.get_text ();
+ return inner.get_text (range_idx);
}
private:
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 5bab3f3..601f6d2 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -4289,7 +4289,7 @@ qualified_name_lookup_error (tree scope, tree name,
Compare with print_template_differences above. */
label_text
-range_label_for_type_mismatch::get_text () const
+range_label_for_type_mismatch::get_text (unsigned /*range_idx*/) const
{
if (m_labelled_type == NULL_TREE)
return label_text (NULL, false);
diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index 6ce8a0f..7dfb0a0 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -128,6 +128,7 @@ class layout_range
const expanded_location *finish_exploc,
enum range_display_kind range_display_kind,
const expanded_location *caret_exploc,
+ unsigned original_idx,
const range_label *label);
bool contains_point (linenum_type row, int column) const;
@@ -137,6 +138,7 @@ class layout_range
layout_point m_finish;
enum range_display_kind m_range_display_kind;
layout_point m_caret;
+ unsigned m_original_idx;
const range_label *m_label;
};
@@ -236,6 +238,7 @@ class layout
diagnostic_t diagnostic_kind);
bool maybe_add_location_range (const location_range *loc_range,
+ unsigned original_idx,
bool restrict_to_current_line_spans);
int get_num_line_spans () const { return m_line_spans.length (); }
@@ -414,11 +417,13 @@ layout_range::layout_range (const expanded_location *start_exploc,
const expanded_location *finish_exploc,
enum range_display_kind range_display_kind,
const expanded_location *caret_exploc,
+ unsigned original_idx,
const range_label *label)
: m_start (*start_exploc),
m_finish (*finish_exploc),
m_range_display_kind (range_display_kind),
m_caret (*caret_exploc),
+ m_original_idx (original_idx),
m_label (label)
{
}
@@ -546,7 +551,7 @@ make_range (int start_line, int start_col, int end_line, int end_col)
const expanded_location finish_exploc
= {"test.c", end_line, end_col, NULL, false};
return layout_range (&start_exploc, &finish_exploc, SHOW_RANGE_WITHOUT_CARET,
- &start_exploc, NULL);
+ &start_exploc, 0, NULL);
}
/* Selftests for layout_range::contains_point and
@@ -899,7 +904,7 @@ layout::layout (diagnostic_context * context,
/* This diagnostic printer can only cope with "sufficiently sane" ranges.
Ignore any ranges that are awkward to handle. */
const location_range *loc_range = richloc->get_range (idx);
- maybe_add_location_range (loc_range, false);
+ maybe_add_location_range (loc_range, idx, false);
}
/* Populate m_fixit_hints, filtering to only those that are in the
@@ -953,6 +958,9 @@ layout::layout (diagnostic_context * context,
/* Attempt to add LOC_RANGE to m_layout_ranges, filtering them to
those that we can sanely print.
+ ORIGINAL_IDX is the index of LOC_RANGE within its rich_location,
+ (for use as extrinsic state by label ranges FIXME).
+
If RESTRICT_TO_CURRENT_LINE_SPANS is true, then LOC_RANGE is also
filtered against this layout instance's current line spans: it
will only be added if the location is fully within the lines
@@ -962,6 +970,7 @@ layout::layout (diagnostic_context * context,
bool
layout::maybe_add_location_range (const location_range *loc_range,
+ unsigned original_idx,
bool restrict_to_current_line_spans)
{
gcc_assert (loc_range);
@@ -1001,7 +1010,7 @@ layout::maybe_add_location_range (const location_range *loc_range,
/* Everything is now known to be in the correct source file,
but it may require further sanitization. */
layout_range ri (&start, &finish, loc_range->m_range_display_kind, &caret,
- loc_range->m_label);
+ original_idx, loc_range->m_label);
/* If we have a range that finishes before it starts (perhaps
from something built via macro expansion), printing the
@@ -1488,7 +1497,7 @@ layout::print_any_labels (linenum_type row)
continue;
label_text text;
- text = range->m_label->get_text ();
+ text = range->m_label->get_text (range->m_original_idx);
/* Allow for labels that return NULL from their get_text
implementation (so e.g. such labels can control their own
@@ -2277,7 +2286,7 @@ gcc_rich_location::add_location_if_nearby (location_t loc)
location_range loc_range;
loc_range.m_loc = loc;
loc_range.m_range_display_kind = SHOW_RANGE_WITHOUT_CARET;
- if (!layout.maybe_add_location_range (&loc_range, true))
+ if (!layout.maybe_add_location_range (&loc_range, 0, true))
return false;
add_range (loc);
diff --git a/gcc/gcc-rich-location.h b/gcc/gcc-rich-location.h
index dc11ee8..e55dd76 100644
--- a/gcc/gcc-rich-location.h
+++ b/gcc/gcc-rich-location.h
@@ -109,7 +109,7 @@ class text_range_label : public range_label
public:
text_range_label (const char *text) : m_text (text) {}
- label_text get_text () const FINAL OVERRIDE
+ label_text get_text (unsigned /*range_idx*/) const FINAL OVERRIDE
{
return label_text (const_cast <char *> (m_text), false);
}
@@ -155,7 +155,7 @@ class range_label_for_type_mismatch : public range_label
{
}
- label_text get_text () const OVERRIDE;
+ label_text get_text (unsigned range_idx) const OVERRIDE;
protected:
tree m_labelled_type;
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index fd06758..c479dfa 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -1808,8 +1808,10 @@ class range_label
public:
virtual ~range_label () {}
- /* Get localized text for the label. */
- virtual label_text get_text () const = 0;
+ /* Get localized text for the label.
+ The RANGE_IDX is provided, allowing for range_label instances to be
+ shared by multiple ranges if need be (the "flyweight" design pattern). */
+ virtual label_text get_text (unsigned range_idx) const = 0;
};
/* A fix-it hint: a suggested insertion, replacement, or deletion of text.
--
1.8.5.3
More information about the Gcc-patches
mailing list