[Bug c++/87091] Malformed fix-it hint for missing header

2018-08-27 Thread dmalcolm at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

--- Comment #6 from David Malcolm  ---
I've been using this PR to track various tweaks to how we print that fix-it
hint.  As well as the bug fix referred to in comment #1, we've gone from:


In file included from
../x86_64-pc-linux-gnu/libstdc++-v3/include/debug/vector:34,
 from /tmp/t.cc:2:
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:27: error: ‘vector’ in
namespace ‘std’ does not name a template type
87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
   |   ^~
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: ‘std::vector’
is defined in header ‘’; did you forget to ‘#include ’?
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1:
++ |+#include 
74 | #endif
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22:
87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
   |  ^~~


to:


In file included from
../x86_64-pc-linux-gnu/libstdc++-v3/include/debug/vector:34,
 from /tmp/t.cc:2:
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:27: error: ‘vector’ in
namespace ‘std’ does not name a template type
87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
   |   ^~
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: ‘std::vector’ is
defined in header ‘’; did you forget to ‘#include ’?
73 | # include 
++ |+#include 
74 | #endif


I think that's all for now.

[Bug c++/87091] Malformed fix-it hint for missing header

2018-08-27 Thread dmalcolm at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

--- Comment #5 from David Malcolm  ---
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 
  +++ |+#include 
   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 
  +++ |+#include 
   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 
  +++ |+#include 
   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-pr

[Bug c++/87091] Malformed fix-it hint for missing header

2018-08-27 Thread dmalcolm at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

--- Comment #4 from David Malcolm  ---
Author: dmalcolm
Date: Mon Aug 27 13:46:32 2018
New Revision: 263884

URL: https://gcc.gnu.org/viewcvs?rev=263884&root=gcc&view=rev
Log:
diagnostics: show an extra line of context in line-insertion fix-it hints (PR
87091)

This patch tweaks how we print line-insertion fix-it hints, so that
the line before the insertion point is also printed, to give the user
more context on the proposed change.

For example, it changes:

  ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: message
  +++ |+#include 
   74 | #endif
  
   87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
  |  ^~~

to:

  ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: message
   73 | # include 
  +++ |+#include 
   74 | #endif
  
   87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
  |  ^~~

gcc/ChangeLog:
PR 87091
* diagnostic-show-locus.c (get_line_span_for_fixit_hint): Show the
line above for line-insertion fix-it hints.
(selftest::test_fixit_insert_containing_newline): Update the
expected results, and add a test with line-numbering enabled.

gcc/testsuite/ChangeLog:
PR 87091
* g++.dg/pr85523.C: Extend expected output to show line
before line-insertion fix-it hint.
* gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c
(test_fixit_insert_newline): Add previous line to expected output.
* gcc.dg/plugin/diagnostic-test-show-locus-bw.c: Likewise.
* gcc.dg/plugin/diagnostic-test-show-locus-color.c: Likewise.


Modified:
trunk/gcc/ChangeLog
trunk/gcc/diagnostic-show-locus.c
trunk/gcc/testsuite/ChangeLog
trunk/gcc/testsuite/g++.dg/pr85523.C
   
trunk/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c
trunk/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c
trunk/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c

[Bug c++/87091] Malformed fix-it hint for missing header

2018-08-24 Thread dmalcolm at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

--- Comment #3 from David Malcolm  ---
Author: dmalcolm
Date: Fri Aug 24 21:17:48 2018
New Revision: 263843

URL: https://gcc.gnu.org/viewcvs?rev=263843&root=gcc&view=rev
Log:
diagnostics: tweaks to line-spans vs line numbering (PR 87091)

This patch tweaks how line numbers are printed for a diagnostic
containing multiple line spans.

With this patch, rather than printing span headers:

  ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: message
  ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1:
  ++ |+#include 
  74 | #endif
  ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22:
  87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
 |  ^~~

we now print:

  ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: message
  +++ |+#include 
   74 | #endif
  
   87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
  |  ^~~

and for sufficiently close lines, rather than print a gap:

  + |+#include 
  1 | test (int ch)
  ..
  3 |  putchar (ch);
|  ^~~

we print the line itself:

  + |+#include 
  1 | test (int ch)
  2 | {
  3 |  putchar (ch);
|  ^~~

gcc/ChangeLog:
PR 87091
* diagnostic-show-locus.c (layout::layout): Ensure the margin is
wide enough for jumps in the line-numbering to be visible.
(layout::print_gap_in_line_numbering): New member function.
(layout::calculate_line_spans): When using line numbering, merge
line spans that are only 1 line apart.
(diagnostic_show_locus): When printing line numbers, show gaps in
line numbering directly, rather than printing headers.
(selftest::test_diagnostic_show_locus_fixit_lines): Add test of
line-numbering with multiple line spans.
(selftest::test_fixit_insert_containing_newline_2): Add test of
line-numbering, in which the spans are close enough to be merged.

gcc/testsuite/ChangeLog:
PR 87091
* gcc.dg/missing-header-fixit-3.c: Update for changes to how
line spans are printed with -fdiagnostics-show-line-numbers.


Modified:
trunk/gcc/ChangeLog
trunk/gcc/diagnostic-show-locus.c
trunk/gcc/testsuite/ChangeLog
trunk/gcc/testsuite/gcc.dg/missing-header-fixit-3.c

[Bug c++/87091] Malformed fix-it hint for missing header

2018-08-24 Thread dmalcolm at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

David Malcolm  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |FIXED

--- Comment #2 from David Malcolm  ---
(In reply to David Malcolm from comment #1)
> Looks a lot like the issue I fixed in r263606.

Indeed, with trunk (specifially r263787) I get:

In file included from
../x86_64-pc-linux-gnu/libstdc++-v3/include/debug/vector:34,
 from /tmp/t.cc:2:
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:27: error: ‘vector’ in
namespace ‘std’ does not name a template type
87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
   |   ^~
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: ‘std::vector’
is defined in header ‘’; did you forget to ‘#include ’?
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1:
++ |+#include 
74 | #endif
../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22:
87 |   using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
   |  ^~~

Resolving as FIXED; feel free to reopen if there's something else going on.

[Bug c++/87091] Malformed fix-it hint for missing header

2018-08-24 Thread dmalcolm at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87091

--- Comment #1 from David Malcolm  ---
Looks a lot like the issue I fixed in r263606.