aboutsummaryrefslogtreecommitdiff
path: root/libcpp/include
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2018-08-27 14:02:05 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2018-08-27 14:02:05 +0000
commit85204e23e2fed09fc07159ab5607e0b760269561 (patch)
tree329a88e54e3b2d7854f2746bda51478b84b3e65e /libcpp/include
parentdf308f8160051f72679981d45ccbabe6b3f25396 (diff)
downloadgcc-85204e23e2fed09fc07159ab5607e0b760269561.zip
gcc-85204e23e2fed09fc07159ab5607e0b760269561.tar.gz
gcc-85204e23e2fed09fc07159ab5607e0b760269561.tar.bz2
Less verbose fix-it hints for missing header files (PR 87091)
This patch tweaks maybe_add_include_fixit so that if we're emitting a note about adding the header file, the note's primary location will be replaced by that of the fix-it hint, to avoid repeating a location we've already emitted (or one close to it). For example, this simplifies: ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:27: error: msg 1 87 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>; | ^~~~~~ ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: msg 2 73 | # include <debug/vector> +++ |+#include <vector> 74 | #endif .... 87 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>; | ^~~ to: ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:27: error: msg 1 87 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>; | ^~~~~~ ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2 73 | # include <debug/vector> +++ |+#include <vector> 74 | #endif eliminating the repetition of line 87 in the note. Doing so requires converting show_caret_p to a tri-state, to avoid meaninglessly printing a caret for the first column in the next line (and colorizing it): ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2 73 | # include <debug/vector> +++ |+#include <vector> 74 | #endif | ^ gcc/c-family/ChangeLog: PR 87091 * c-common.c (c_cpp_error): Update for conversion of show_caret_p to a tri-state. (maybe_suggest_missing_token_insertion): Likewise. (maybe_add_include_fixit): Add param "override_location". If set, and source-printing is enabled, then override the rich_location's primary location with that of the insertion point for the fix-it hint, marking it with SHOW_LINES_WITHOUT_RANGE. * c-common.h (extern void maybe_add_include_fixit): Add bool param. * c-format.c (selftest::test_type_mismatch_range_labels): Update for conversion of show_caret_p to a tri-state. * c-warn.c (warn_for_restrict): Likewise. * known-headers.cc (suggest_missing_header::~suggest_missing_header): Update call to maybe_add_include_fixit to suggest overriding the location, as it is for a note. gcc/c/ChangeLog: PR 87091 * c-decl.c (implicitly_declare): Update call to maybe_add_include_fixit to suggest overriding the location, as it is for a note. * c-objc-common.c (c_tree_printer): Update for conversion of show_caret_p to a tri-state. gcc/cp/ChangeLog: PR 87091 * decl.c (grokdeclarator): Update for conversion of show_caret_p to a tri-state. * error.c (cp_printer): Likewise. * name-lookup.c (maybe_suggest_missing_std_header): Update call to maybe_add_include_fixit to suggest overriding the location, as it is for a note. * parser.c (cp_parser_string_literal): Update for conversion of show_caret_p to a tri-state. (cp_parser_elaborated_type_specifier): Likewise. (set_and_check_decl_spec_loc): Likewise. * pt.c (listify): Update call to maybe_add_include_fixit to not override the location, as it is for an error. * rtti.c (typeid_ok_p): Likewise. gcc/ChangeLog: PR 87091 * diagnostic-show-locus.c (class layout_range): Update for conversion of show_caret_p to a tri-state. (layout_range::layout_range): Likewise. (make_range): Likewise. (layout::maybe_add_location_range): Likewise. (layout::should_print_annotation_line_p): Don't show annotation lines for ranges that are SHOW_LINES_WITHOUT_RANGE. (layout::get_state_at_point): Update for conversion of show_caret_p to a tri-state. Bail out early for SHOW_LINES_WITHOUT_RANGE, so that such ranges don't affect underlining or source colorization. (gcc_rich_location::add_location_if_nearby): Update for conversion of show_caret_p to a tri-state. (selftest::test_one_liner_multiple_carets_and_ranges): Likewise. (selftest::test_one_liner_fixit_replace_equal_secondary_range): Likewise. (selftest::test_one_liner_labels): Likewise. * gcc-rich-location.c (gcc_rich_location::add_expr): Update for conversion of show_caret_p to a tri-state. * pretty-print.c (text_info::set_location): Likewise. * pretty-print.h (text_info::set_location): Likewise. * substring-locations.c (format_warning_n_va): Likewise. * tree-diagnostic.c (default_tree_printer): Likewise. * tree-pretty-print.c (newline_and_indent): Likewise. gcc/fortran/ChangeLog: PR 87091 * error.c (gfc_format_decoder): Update for conversion of show_caret_p to a tri-state. gcc/testsuite/ChangeLog: PR 87091 * gcc.dg/empty.h: New file. * gcc.dg/fixits-pr84852-1.c: Update for move of fix-it hint to top of file and removal of redundant second printing of warning location. * gcc.dg/fixits-pr84852-2.c: Likewise. * gcc.dg/missing-header-fixit-3.c: Likewise. * gcc.dg/missing-header-fixit-4.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: Update for conversion of show_caret_p to a tri-state. libcpp/ChangeLog: PR 87091 * include/line-map.h (enum range_display_kind): New enum. (struct location_range): Replace field "m_show_caret_p" with "m_range_display_kind", converting from bool to the new enum. (class rich_location): Add example of line insertion fix-it hint. (rich_location::add_range): Convert param "show_caret_p" from bool to enum range_display_kind and rename to "range_display_kind", giving it a default of SHOW_RANGE_WITHOUT_CARET. (rich_location::set_range): Likewise, albeit without a default. * line-map.c (rich_location::rich_location): Update for conversion of show_caret_p to tri-state enum. (rich_location::add_range): Likewise. (rich_location::set_range): Likewise. From-SVN: r263885
Diffstat (limited to 'libcpp/include')
-rw-r--r--libcpp/include/line-map.h60
1 files changed, 48 insertions, 12 deletions
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 4f0ff87..e74ccbb 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -1283,6 +1283,36 @@ typedef struct
class range_label;
+/* A hint to diagnostic_show_locus on how to print a source range within a
+ rich_location.
+
+ Typically this is SHOW_RANGE_WITH_CARET for the 0th range, and
+ SHOW_RANGE_WITHOUT_CARET for subsequent ranges,
+ but the Fortran frontend uses SHOW_RANGE_WITH_CARET repeatedly for
+ printing things like:
+
+ x = x + y
+ 1 2
+ Error: Shapes for operands at (1) and (2) are not conformable
+
+ where "1" and "2" are notionally carets. */
+
+enum range_display_kind
+{
+ /* Show the pertinent source line(s), the caret, and underline(s). */
+ SHOW_RANGE_WITH_CARET,
+
+ /* Show the pertinent source line(s) and underline(s), but don't
+ show the caret (just an underline). */
+ SHOW_RANGE_WITHOUT_CARET,
+
+ /* Just show the source lines; don't show the range itself.
+ This is for use when displaying some line-insertion fix-it hints (for
+ showing the user context on the change, for when it doesn't make sense
+ to highlight the first column on the next line). */
+ SHOW_LINES_WITHOUT_RANGE
+};
+
/* A location within a rich_location: a caret&range, with
the caret potentially flagged for display, and an optional
label. */
@@ -1291,16 +1321,7 @@ struct location_range
{
source_location m_loc;
- /* Should a caret be drawn for this range? Typically this is
- true for the 0th range, and false for subsequent ranges,
- but the Fortran frontend overrides this for rendering things like:
-
- x = x + y
- 1 2
- Error: Shapes for operands at (1) and (2) are not conformable
-
- where "1" and "2" are notionally carets. */
- bool m_show_caret_p;
+ enum range_display_kind m_range_display_kind;
/* If non-NULL, the label for this range. */
const range_label *m_label;
@@ -1567,6 +1588,18 @@ class fixit_hint;
added via
richloc.add_fixit_replace ("color");
+ Example J: fix-it hint: line insertion
+ **************************************
+
+ 3 | #include <stddef.h>
+ + |+#include <stdio.h>
+ 4 | int the_next_line;
+
+ This rich location has a single range at line 4 column 1, marked
+ with SHOW_LINES_WITHOUT_RANGE (to avoid printing a meaningless caret
+ on the "i" of int). It has a insertion fix-it hint of the string
+ "#include <stdio.h>\n".
+
Adding a fix-it hint can fail: for example, attempts to insert content
at the transition between two line maps may fail due to there being no
source_location (aka location_t) value to express the new location.
@@ -1610,11 +1643,14 @@ class rich_location
source_location get_loc (unsigned int idx) const;
void
- add_range (source_location loc, bool show_caret_p,
+ add_range (source_location loc,
+ enum range_display_kind range_display_kind
+ = SHOW_RANGE_WITHOUT_CARET,
const range_label *label = NULL);
void
- set_range (unsigned int idx, source_location loc, bool show_caret_p);
+ set_range (unsigned int idx, source_location loc,
+ enum range_display_kind range_display_kind);
unsigned int get_num_locations () const { return m_ranges.count (); }