https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

--- Comment #5 from David Malcolm <dmalcolm at gcc dot gnu.org> ---
Author: dmalcolm
Date: Mon Aug 27 14:02:05 2018
New Revision: 263885

URL: https://gcc.gnu.org/viewcvs?rev=263885&root=gcc&view=rev
Log:
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.


Added:
    trunk/gcc/testsuite/gcc.dg/empty.h
    trunk/gcc/testsuite/gcc.dg/missing-header-fixit-4.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/c-family/c-common.h
    trunk/gcc/c-family/c-format.c
    trunk/gcc/c-family/c-warn.c
    trunk/gcc/c-family/known-headers.cc
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-decl.c
    trunk/gcc/c/c-objc-common.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/error.c
    trunk/gcc/cp/name-lookup.c
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/rtti.c
    trunk/gcc/diagnostic-show-locus.c
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/error.c
    trunk/gcc/gcc-rich-location.c
    trunk/gcc/pretty-print.c
    trunk/gcc/pretty-print.h
    trunk/gcc/substring-locations.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/fixits-pr84852-1.c
    trunk/gcc/testsuite/gcc.dg/fixits-pr84852-2.c
    trunk/gcc/testsuite/gcc.dg/missing-header-fixit-3.c
    trunk/gcc/testsuite/gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
    trunk/gcc/tree-diagnostic.c
    trunk/gcc/tree-pretty-print.c
    trunk/libcpp/ChangeLog
    trunk/libcpp/include/line-map.h
    trunk/libcpp/line-map.c

Reply via email to