Commit Graph

110 Commits

Author SHA1 Message Date
Martin Liska 19eda56db6 Fix min_location usage in line-map.c (PR preprocessor/90382).
2019-05-14  Martin Liska  <mliska@suse.cz>

	PR preprocessor/90382
	* line-map.c (first_map_in_common_1): Handle ADHOC
	locations.

From-SVN: r271163
2019-05-14 11:41:53 +00:00
Martin Liska e6fc8353fc Do a refactoring in linemap (PR preprocessor/90382).
2019-05-14  Martin Liska  <mliska@suse.cz>

	PR preprocessor/90382
	* include/line-map.h (get_data_from_adhoc_loc): Add const to
	the first argument.
	(get_location_from_adhoc_loc): Likewise.
	* line-map.c(get_data_from_adhoc_loc):  Add const to
	the first argument.
	(get_location_from_adhoc_loc): Likewise.
	(get_combined_adhoc_loc): Use get_location_from_adhoc_loc
	(or get_data_from_adhoc_loc).
	(get_range_from_adhoc_loc): Likewise.
	(get_pure_location): Likewise.
	(linemap_position_for_loc_and_offset): Likewise.
	(linemap_lookup): Likewise.
	(linemap_ordinary_map_lookup): Likewise.
	(linemap_macro_map_lookup): Likewise.
	(linemap_get_expansion_line): Likewise.
	(linemap_get_expansion_filename): Likewise.
	(linemap_location_in_system_header_p): Likewise.
	(linemap_location_from_macro_expansion_p): Likewise.
	(linemap_macro_loc_to_exp_point): Likewise.
	(linemap_resolve_location): Likewise.
	(linemap_unwind_toward_expansion): Likewise.
	(linemap_unwind_to_first_non_reserved_loc): Likewise.
	(linemap_expand_location): Likewise.
	(linemap_dump_location): Likewise.

From-SVN: r271162
2019-05-14 11:41:40 +00:00
Martin Liska a5f87af7ed Use 1UL constant in order to not overflow (PR c++/89383).
2019-02-18  Martin Liska  <mliska@suse.cz>

	PR c++/89383
	* line-map.c (linemap_line_start): Use 1UL in order
	to not overflow.

From-SVN: r268981
2019-02-18 09:46:19 +00:00
David Malcolm a4553534df linemap_line_start: protect against location_t overflow (PR lto/88147)
PR lto/88147 reports an assertion failure due to a bogus location_t value
when adding a line to a pre-existing line map, when there's a large
difference between the two line numbers.

For some "large differences", this leads to a location_t value that exceeds
LINE_MAP_MAX_LOCATION, in which case linemap_line_start returns 0.  This
isn't ideal, but at least should lead to safe degradation of location
information.

However, if the difference is very large, it's possible for the line
number offset (relative to the start of the map) to be sufficiently large
that overflow occurs when left-shifted by the column-bits, and hence
the check against the LINE_MAP_MAX_LOCATION limit fails, leading to
a seemingly-valid location_t value, but encoding the wrong location.  This
triggers the assertion failure:
  linemap_assert (SOURCE_LINE (map, r) == to_line);

The fix (thanks to Martin) is to check for overflow when determining
whether to reuse an existing map, and to not reuse it if it would occur.

gcc/ChangeLog: David Malcolm  <dmalcolm@redhat.com>
	PR lto/88147
	* input.c (selftest::test_line_offset_overflow): New selftest.
	(selftest::input_c_tests): Call it.

libcpp/ChangeLog: Martin Liska  <mliska@suse.cz>
	PR lto/88147
	* line-map.c (linemap_line_start): Don't reuse the existing line
	map if the line offset is sufficiently large to cause overflow
	when computing location_t values.

From-SVN: r268789
2019-02-12 01:09:31 +00:00
Jakub Jelinek a554497024 Update copyright years.
From-SVN: r267494
2019-01-01 13:31:55 +01:00
David Malcolm 620e594be5 Eliminate source_location in favor of location_t
Historically GCC used location_t, while libcpp used source_location.

This inconsistency has been annoying me for a while, so this patch
removes source_location in favor of location_t throughout
(as the latter is shorter).

gcc/ChangeLog:
	* builtins.c: Replace "source_location" with "location_t".
	* diagnostic-show-locus.c: Likewise.
	* diagnostic.c: Likewise.
	* dumpfile.c: Likewise.
	* gcc-rich-location.h: Likewise.
	* genmatch.c: Likewise.
	* gimple.h: Likewise.
	* gimplify.c: Likewise.
	* input.c: Likewise.
	* input.h: Likewise.  Eliminate the typedef.
	* omp-expand.c: Likewise.
	* selftest.h: Likewise.
	* substring-locations.h (get_source_location_for_substring):
	Rename to..
	(get_location_within_string): ...this.
	* tree-cfg.c: Replace "source_location" with "location_t".
	* tree-cfgcleanup.c: Likewise.
	* tree-diagnostic.c: Likewise.
	* tree-into-ssa.c: Likewise.
	* tree-outof-ssa.c: Likewise.
	* tree-parloops.c: Likewise.
	* tree-phinodes.c: Likewise.
	* tree-phinodes.h: Likewise.
	* tree-ssa-loop-ivopts.c: Likewise.
	* tree-ssa-loop-manip.c: Likewise.
	* tree-ssa-phiopt.c: Likewise.
	* tree-ssa-phiprop.c: Likewise.
	* tree-ssa-threadupdate.c: Likewise.
	* tree-ssa.c: Likewise.
	* tree-ssa.h: Likewise.
	* tree-vect-loop-manip.c: Likewise.

gcc/c-family/ChangeLog:
	* c-common.c (c_get_substring_location): Update for renaming of
	get_source_location_for_substring to get_location_within_string.
	* c-lex.c: Replace "source_location" with "location_t".
	* c-opts.c: Likewise.
	* c-ppoutput.c: Likewise.

gcc/c/ChangeLog:
	* c-decl.c: Replace "source_location" with "location_t".
	* c-tree.h: Likewise.
	* c-typeck.c: Likewise.
	* gimple-parser.c: Likewise.

gcc/cp/ChangeLog:
	* call.c: Replace "source_location" with "location_t".
	* cp-tree.h: Likewise.
	* cvt.c: Likewise.
	* name-lookup.c: Likewise.
	* parser.c: Likewise.
	* typeck.c: Likewise.

gcc/fortran/ChangeLog:
	* cpp.c: Replace "source_location" with "location_t".
	* gfortran.h: Likewise.

gcc/go/ChangeLog:
	* go-gcc-diagnostics.cc: Replace "source_location" with "location_t".
	* go-gcc.cc: Likewise.
	* go-linemap.cc: Likewise.
	* go-location.h: Likewise.
	* gofrontend/README: Likewise.

gcc/jit/ChangeLog:
	* jit-playback.c: Replace "source_location" with "location_t".

gcc/testsuite/ChangeLog:
	* g++.dg/plugin/comment_plugin.c: Replace "source_location" with
	"location_t".
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c: Likewise.

libcc1/ChangeLog:
	* libcc1plugin.cc: Replace "source_location" with "location_t".
	(plugin_context::get_source_location): Rename to...
	(plugin_context::get_location_t): ...this.
	* libcp1plugin.cc: Likewise.

libcpp/ChangeLog:
	* charset.c: Replace "source_location" with "location_t".
	* directives-only.c: Likewise.
	* directives.c: Likewise.
	* errors.c: Likewise.
	* expr.c: Likewise.
	* files.c: Likewise.
	* include/cpplib.h: Likewise.  Rename MAX_SOURCE_LOCATION to
	MAX_LOCATION_T.
	* include/line-map.h: Likewise.
	* init.c: Likewise.
	* internal.h: Likewise.
	* lex.c: Likewise.
	* line-map.c: Likewise.
	* location-example.txt: Likewise.
	* macro.c: Likewise.
	* pch.c: Likewise.
	* traditional.c: Likewise.

From-SVN: r266085
2018-11-13 20:05:03 +00:00
Nathan Sidwell 87bacc2b39 [4/7] Preprocessor location-kind predicates
https://gcc.gnu.org/ml/gcc-patches/2018-10/msg02040.html
	* include/line-map.h (IS_ORDINARY_LOC, IS_MACRO_LOC): New
	predicates.
	(IS_ADHOC_LOC): Move earlier.
	(MAP_ORDINARY_P): Use IS_ORDINARY_LOC.
	* line-map.c (linemap_location_from_macro_expansion_p): Use
	IS_MACRO_LOC.

From-SVN: r265689
2018-10-31 14:57:13 +00:00
Nathan Sidwell c1b48b2929 [PATCH] A couple of line map fixes
https://gcc.gnu.org/ml/gcc-patches/2018-10/msg00623.html
	* include/line-map.h (LINEMAPS_MACRO_LOWEST_LOCATION): Fix
	off-by-one error.
	* line-map.c (linemap_enter_macro): Use RAII.  Clear all of the
	macro_locations.

From-SVN: r265037
2018-10-11 12:42:37 +00:00
David Malcolm 85204e23e2 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
2018-08-27 14:02:05 +00:00
H.J. Lu cf806c7dc3 Set start_location to 0 if we ran out of line map space
With profiledbootstrap and --with-build-config=bootstrap-lto, linemap_add
may create a macro map when we run out of line map space.  This patch
changes start_location to UNKNOWN_LOCATION (0) in this case.

Tested with profiledbootstrap and --with-build-config=bootstrap-lto on
Linux/x86-64.

	PR bootstrap/86872
	* line-map.c (pure_location_p): Return true if linemap_lookup
	returns NULL.
	(linemap_add): Set start_location to 0 if we run out of line map
	space.

From-SVN: r263845
2018-08-24 16:37:53 -07:00
David Malcolm 96e6ae576c diagnostics: add labeling of source ranges
This patch adds the ability to label source ranges within a rich_location,
to be printed by diagnostic_show_locus.

For example:

pr69554-1.c:11:18: error: invalid operands to binary + (have 'const char *' and 'const char *')
11 |   return (p + 1) + (q + 1);
   |          ~~~~~~~ ^ ~~~~~~~
   |             |         |
   |             |         const char *
   |             const char *

The patch implements labels for various type mismatch errors in the C and
C++ frontends, and in -Wformat.  I implemented it wherever accurate location
information was guaranteed (there are other places that could benefit, but
we need better location information in those places).

The labels can be disabled via -fno-diagnostics-show-labels.

Similarly:

param-type-mismatch.C: In function 'int test_1(int, int, float)':
param-type-mismatch.C:11:27: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
11 |   return callee_1 (first, second, third);
   |                           ^~~~~~
   |                           |
   |                           int
param-type-mismatch.C:7:43: note:   initializing argument 2 of 'int callee_1(int, const char*, float)'
7 | extern int callee_1 (int one, const char *two, float three);
  |                               ~~~~~~~~~~~~^~~

where the first "error" describing the bad argument gets a label
describing the type inline (since it's non-obvious from "second").
The "note" describing the type of the param of the callee *doesn't*
get a label, since that information is explicit there in the
source ("const char *two").

The idea is that in any diagnostic where two aspects of the source aren't
in sync it ought to be easier for the user if we directly show them the
mismatching aspects inline (e.g. types).

As well as type mismatch errors, perhaps labels could also be used for
buffer overflow warnings, for describing the capacity of the destination
buffer vs the size of what's being written:

  sprintf (buf, "filename: %s\n", file);
           ^~~   ~~~~~~~~~~~^~~
           |                |
           capacity: 32     10 + strlen(file) + 2

or somesuch.  Another idea might be for macro expansion warnings:

warning: repeated side effects in macro expansion...
   x = MIN (p++, q++);
       ~~~~^~~~~~~~~~
note: ...expanded here as
 #define MIN(X,Y) (X<Y?X:Y)
         ^~~ ~ ~   ~ ~ ~ ~
             | |   | | | |
             | |   | | | q++
             | |   | | p++
             | |   | q++
             | q++ p++
             p++

The patch removes some logic from multiline.exp which special-cased
lines ending with a '|' character (thus complicating testing of this
patch).  I believe that this was a vestige from experiments I did to
support strippng dg directives from the output; it was present in the
earliest version of multiline.exp I posted:
  "[RFC, stage1] Richer source location information for gcc 6 (location ranges etc)"
    https://gcc.gnu.org/ml/gcc-patches/2015-03/msg00837.html
and I believe was neved used.

gcc/c-family/ChangeLog:
	* c-format.c: Include "selftest-diagnostic.h" and
	"gcc-rich-location.h".
	(format_warning_at_char): Pass NULL for new label params of
	format_warning_va.
	(class indirection_suffix): New class.
	(class range_label_for_format_type_mismatch): New class.
	(format_type_warning): Move logic for generating "*" suffix to
	class indirection_suffix.  Create "fmt_label" and "param_label"
	to show their types, and pass them to the
	format_warning_at_substring calls.
	(selftest::test_type_mismatch_range_labels): New test.
	(selftest::c_format_c_tests): Call it.

gcc/c/ChangeLog:
	* c-objc-common.c: Include "gcc-rich-location.h".
	(c_tree_printer): Move implemenation of '%T' to...
	(print_type): ...this new function.
	(range_label_for_type_mismatch::get_text): New function.
	* c-typeck.c (convert_for_assignment): Add type labels to the rhs
	range for the various ic_argpass cases.
	(class maybe_range_label_for_tree_type_mismatch): New class.
	(build_binary_op): Use it when calling binary_op_error.

gcc/cp/ChangeLog:
	* call.c: Include "gcc-rich-location.h".
	(convert_like_real): Add range label for "invalid conversion"
	diagnostic.
	(perform_implicit_conversion_flags): Add type label to the
	"could not convert" error.
	* error.c: Include "gcc-rich-location.h".
	(range_label_for_type_mismatch::get_text): New function.
	* typeck.c (convert_for_assignment): Add type label to
	the "cannot convert" error if a location is available.

gcc/ChangeLog:
	* common.opt (fdiagnostics-show-labels): New option.
	* diagnostic-show-locus.c (class layout_range): Add field
	"m_label".
	(class layout): Add field "m_show_labels_p".
	(layout_range::layout_range): Add param "label" and use it to
	initialize m_label.
	(make_range): Pass in NULL for new "label" param of layout_range's
	ctor.
	(layout::layout): Initialize m_show_labels_p.
	(layout::maybe_add_location_range): Pass in loc_range->m_label
	when constructing layout_range instances.
	(struct line_label): New struct.
	(layout::print_any_labels): New member function.
	(layout::print_line): Call it if label-printing is enabled.
	(selftest::test_one_liner_labels): New test.
	(selftest::test_diagnostic_show_locus_one_liner): Call it.
	* diagnostic.c (diagnostic_initialize): Initialize
	context->show_labels_p.
	* diagnostic.h (struct diagnostic_context): Add field
	"show_labels_p".
	* doc/invoke.texi (Diagnostic Message Formatting Options): Add
	-fno-diagnostics-show-labels.
	* dwarf2out.c (gen_producer_string): Add
	OPT_fdiagnostics_show_labels to the ignored options.
	* gcc-rich-location.c (gcc_rich_location::add_expr): Add "label"
	param.
	(gcc_rich_location::maybe_add_expr): Likewise.
	* gcc-rich-location.h (gcc_rich_location::gcc_rich_location): Add
	label" param, defaulting to NULL.
	(gcc_rich_location::add_expr): Add "label" param.
	(gcc_rich_location::maybe_add_expr): Likewise.
	(class text_range_label): New class.
	(class range_label_for_type_mismatch): New class.
	* gimple-ssa-sprintf.c (fmtwarn): Pass NULL for new label params
	of format_warning_va.
	(fmtwarn_n): Likewise for new params of format_warning_n_va.
	* lto-wrapper.c (merge_and_complain): Add
	OPT_fdiagnostics_show_labels to the "pick one setting" options.
	(append_compiler_options): Likewise to the dropped options.
	(append_diag_options): Likewise to the passed-on options.
	* opts.c (common_handle_option): Handle the new option.
	* selftest-diagnostic.c
	(test_diagnostic_context::test_diagnostic_context): Enable
	show_labels_p.
	* substring-locations.c: Include "gcc-rich-location.h".
	(format_warning_n_va): Add "fmt_label" and "param_label" params
	and use them as appropriate.
	(format_warning_va): Add "fmt_label" and "param_label" params,
	passing them on to format_warning_n_va.
	(format_warning_at_substring): Likewise.
	(format_warning_at_substring_n): Likewise.
	* substring-locations.h (format_warning_va): Add "fmt_label" and
	"param_label" params.
	(format_warning_n_va): Likewise.
	(format_warning_at_substring): Likewise.
	(format_warning_at_substring_n): Likewise.
	* toplev.c (general_init): Initialize global_dc->show_labels_p.

gcc/testsuite/ChangeLog:
	* g++.dg/diagnostic/aka3.C: New test.
	* g++.dg/diagnostic/param-type-mismatch-2.C: Update expected
	output to show range labels.
	* g++.dg/diagnostic/param-type-mismatch.C: Likewise.
	* g++.dg/plugin/plugin.exp (plugin_test_list): Add...
	* g++.dg/plugin/show-template-tree-color-labels.C: New test.
	* gcc.dg/bad-binary-ops.c: Update expected output to show range
	labels.  Add an "aka" example.
	* gcc.dg/cpp/pr66415-1.c: Update expected output to show range
	labels.
	* gcc.dg/format/diagnostic-ranges.c: Likewise.
	* gcc.dg/format/pr72858.c: Likewise.
	* gcc.dg/format/pr78498.c: Likewise.
	* gcc.dg/param-type-mismatch.c: Add "-Wpointer-sign" to options.
	Update expected output to show range labels.  Add examples of
	-Wincompatible-pointer-types and -Wpointer-sign for parameters.
	* gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c:
	Update expected output to show range labels.
	* gcc.dg/plugin/diagnostic-test-show-locus-bw.c: Likewise.
	(test_very_wide_line): Adjust so that label is at left-clipping
	boundary.
	(test_very_wide_line_2): New test.
	* gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c:
	Update expected output to show range labels.
	* gcc.dg/plugin/diagnostic-test-show-locus-color.c: Likewise.
	* gcc.dg/plugin/diagnostic-test-show-locus-no-labels.c: New test.
	* gcc.dg/plugin/diagnostic_plugin_show_trees.c (show_tree): Update
	for new param to gcc_rich_location::add_expr.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (add_range):
	Add "label" param.
	(test_show_locus): Add examples of labels to various tests.  Tweak
	the "very wide_line" test case and duplicate it, to cover the
	boundary values for clipping of labels against the left-margin.
	* gcc.dg/plugin/plugin.exp (plugin_test_list): Add
	diagnostic-test-show-locus-no-labels.c.
	* gcc.dg/pr69554-1.c: Update expected output to show range labels.
	Update line numbers of dg-locus directives.
	* gcc.dg/pr69627.c:  Update expected output to show range labels.
	* lib/multiline.exp (proc _build_multiline_regex): Remove
	special-case handling of lines with trailing '|'.

libcpp/ChangeLog:
	* include/line-map.h (struct location_range): Add "m_label" field.
	(class rich_location): Add description of labels to leading
	comment.
	(rich_location::rich_location): Add "label" param, defaulting to
	NULL.
	(rich_location::add_range): Likewise.
	(struct label_text): New struct.
	(class range_label): New abstract base class.
	* line-map.c (rich_location::rich_location): Add "label" param;
	use it.
	(rich_location::add_range): Likewise.

From-SVN: r263564
2018-08-15 18:09:35 +00:00
Nathan Sidwell f10a91352f [PATCH] line-map include-from representation
https://gcc.gnu.org/ml/gcc-patches/2018-08/msg00554.html
	Make linemap::included_from a location
	libcpp/
	* include/line-map.h (struct line_map_ordinary): Replace
	included_from map index with included_at source_location.
	(ORDINARY_MAP_INCLUDER_FILE_INDEX): Delete.
	(LAST_SOURCE_LINE_LOCATION): Delete.
	(LAST_SOURCE_LINE, LAST_SOURCE_COLUMN): Delete.
	(linemap_included_from): New.
	(linemap_included_from_linemap): Declare.
	(MAIN_FILE_P): Adjust.
	* line-map.c (linemap_included_from_linemap): New.
	(lonemap_check_files_exited): Use linemap_included_at.
	(linemap_add): Adjust inclusion setting.
	(linemap_dump, linemap_dump_location): Adjust.
	* directives.c (do_linemarker): Use linemap_included_at.
	gcc/
	* diagnostic.c (diagnostic_report_current_module): Use
	linemap_included_from & linemap_included_from_linemap.
	gcc/c-family/
	* c-common.c (try_to_locate_new_include_inertion_point): Use
	linemap_included_from_linemap.
	* c-lex.c (fe_file_change): Use linemap_included_from.
	* c-ppoutput.c (pp_file_change): Likewise.
	gcc/fortran/
	* cpp.c (cb_file_change): Use linemap_included_from.
	gcc/testsuite/
	* c-c++-common/inc-from-1a.h, c-c++-common/inc-from-1b.h,
	c-c++-common/inc-from-1.c: New

From-SVN: r263429
2018-08-08 18:13:00 +00:00
Nathan Sidwell e81c3c4dc1 [PATCH] Line map table allocation
https://gcc.gnu.org/ml/gcc-patches/2018-08/msg00434.html
	* line-map.c: (linemap_init): Set default allocator here.
	(new_linemap): Rather than here.  Refactor allocation logic.

From-SVN: r263366
2018-08-07 21:28:51 +00:00
David Malcolm 181463c271 libcpp: remove redundant parameter from rich_location::set_range
gcc/c-family/ChangeLog:
	* c-common.c (c_cpp_error): Remove redundant "line_table"
	parameter from call to rich_location::set_range.
	(maybe_suggest_missing_token_insertion): Likewise.

gcc/ChangeLog:
	* pretty-print.c (text_info::set_location): Remove redundant
	"line_table" parameter from call to rich_location::set_range.

libcpp/ChangeLog:
	* include/line-map.h (rich_location::set_range): Remove redundant
	line_maps * parameter.
	* line-map.c (rich_location::set_range): Likewise.

From-SVN: r262913
2018-07-20 21:39:14 +00:00
Nathan Sidwell 42a98b43bb Reorg line_map data structures for better packing.
* include/line-map.h (enum lc_reason): Add LC_HWM.
	(LINE_MAP_MAX_LOCATION): Define here.
	(struct line_map): Move reason field to line_map_ordinary.  Adjust
	GTY tagging.
	(struct line_map_ordinary): Reorder fields for less padding.
	(struct line_map_macro): Likewise.
	(MAP_ORDINARY_P): New.
	(linemap_check_ordinary, linemap_check_macro): Adjust.
	* line-map.c (LINE_MAP_MAX_SOURCE_LOCATION): Delete.
	(new_linemap): Take start_location, not reason.  Adjust.
	(linemap_add, linemap_enter_macro): Adjust.
	(linemap_line_start): Likewise.
	(linemap_macro_expansion_map_p): Use MAP_ORDINARY_P.
	(linemap_macro_loc_to_spelling_point): Likewise.
	(linemap_macro_loc_to_def_point): Likewise.
	(linemap_dump): Likewise.

From-SVN: r262348
2018-07-03 14:47:11 +00:00
Jason Merrill b2ff74575d line-map.c (linemap_init): Use placement new.
* line-map.c (linemap_init): Use placement new.

	* system.h: #include <new>.

From-SVN: r260343
2018-05-17 19:28:34 -04:00
Richard Biener fe74f9b4ed re PR bootstrap/82939 (genmatch fills up terminal with endless printing of periods)
2018-02-16  Richard Biener  <rguenther@suse.de>

	PR bootstrap/82939
	* line-map.c (linemap_init): Avoid broken value-init when compiling
	with GCC 4.2.

From-SVN: r257732
2018-02-16 12:02:34 +00:00
Jakub Jelinek 85ec4feb11 Update copyright years.
From-SVN: r256169
2018-01-03 11:03:58 +01:00
David Malcolm 6df8934f6a Reject fix-it hints for various awkward boundary cases (PR c/82050)
PR c/82050 reports a failed assertion deep within diagnostic_show_locus's
code for printing fix-it hints.

The root cause is a fix-it hint suggesting a textual replacement,
where the affected column numbers straddle the LINE_MAP_MAX_COLUMN_NUMBER
boundary, so that the start of the range has a column number, but the
end of the range doesn't.

The fix is to verify that the column numbers are sane when adding fix-it
hints to a rich_location, rejecting fix-it hints where they are not.

libcpp/ChangeLog:
	PR c/82050
	* include/line-map.h (LINE_MAP_MAX_COLUMN_NUMBER): Move here.
	* line-map.c (LINE_MAP_MAX_COLUMN_NUMBER): ...from here.
	(rich_location::maybe_add_fixit): Reject fix-it hints in which
	the start column exceeds the next column.

From-SVN: r255214
2017-11-28 19:24:35 +00:00
David Malcolm 738f7c2e12 libcpp: preserve ranges within macro expansions (PR c++/79300)
gcc/testsuite/ChangeLog:
	PR c++/79300
	* g++.dg/diagnostic/pr79300.C: New test case.

libcpp/ChangeLog:
	PR c++/79300
	* line-map.c (linemap_macro_loc_to_def_point): Preserve range
	information for macro expansions by delaying resolving ad-hoc
	locations until within the loop.

From-SVN: r250058
2017-07-07 18:49:09 +00:00
David Malcolm c471c6edcb diagnostics: fix end-points of ranges within macros (PR c++/79300)
gcc/ChangeLog:
	PR c++/79300
	* diagnostic-show-locus.c (layout::layout): Use start and finish
	spelling location for the start and finish of each range.
	* genmatch.c (linemap_client_expand_location_to_spelling_point):
	Add unused aspect param.
	* input.c (expand_location_1): Add "aspect" param, and use it
	to access the correct part of the location.
	(expand_location): Pass LOCATION_ASPECT_CARET to new param of
	expand_location_1.
	(expand_location_to_spelling_point): Likewise.
	(linemap_client_expand_location_to_spelling_point): Add "aspect"
	param, and pass it to expand_location_1.

gcc/testsuite/ChangeLog:
	PR c++/79300
	* c-c++-common/Wmisleading-indentation-3.c (fn_14): Update
	expected underlining within macro expansion.
	* c-c++-common/pr70264.c: Likewise.
	* g++.dg/plugin/diagnostic-test-expressions-1.C
	(test_within_macro_1): New test.
	(test_within_macro_2): Likewise.
	(test_within_macro_3): Likewise.
	(test_within_macro_4): Likewise.
	* gcc.dg/format/diagnostic-ranges.c (test_macro_3): Update
	expected underlining within macro expansion.
	(test_macro_4): Likewise.
	* gcc.dg/plugin/diagnostic-test-expressions-1.c
	(test_within_macro_1): New test.
	(test_within_macro_2): Likewise.
	(test_within_macro_3): Likewise.
	(test_within_macro_4): Likewise.
	* gcc.dg/spellcheck-fields-2.c (test_macro): Update expected
	underlining within macro expansion.

libcpp/ChangeLog:
	PR c++/79300
	* include/line-map.h (enum location_aspect): New enum.
	(linemap_client_expand_location_to_spelling_point): Add
	enum location_aspect param.
	* line-map.c (rich_location::get_expanded_location): Update for
	new param of linemap_client_expand_location_to_spelling_point.
	(rich_location::maybe_add_fixit): Likewise.
	(fixit_hint::affects_line_p): Likewise.

From-SVN: r250022
2017-07-06 14:17:24 +00:00
Jakub Jelinek 6d52273137 line-map.c (location_adhoc_data_update): Perform addition in uintptr_t type rather than char * type.
* line-map.c (location_adhoc_data_update): Perform addition in
	uintptr_t type rather than char * type.  Read *data using
	ptrdiff_t type instead of int64_t.
	(get_combined_adhoc_loc): Change offset type to ptrdiff_t from
	int64_t.

From-SVN: r249446
2017-06-21 12:59:12 +02:00
David Malcolm c7a980b80b Prevent fix-it hints from affecting more than one line
Attempts to apply a removal or replacement fix-it hint to a source
range that covers multiple lines currently lead to nonsensical
results from the printing code in diagnostic-show-locus.c.

We were already filtering them out in edit-context.c (leading
to -fdiagnostics-generate-patch not generating any output for
the whole TU).

Reject attempts to add such fix-it hints within rich_location,
fixing the diagnostic-show-locus.c issue.

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(selftest::test_fixit_deletion_affecting_newline): New function.
	(selftest::diagnostic_show_locus_c_tests): Call it.

libcpp/ChangeLog:
	* include/line-map.h (class rich_location): Document that attempts
	to delete or replace a range *affecting* multiple lines will fail.
	* line-map.c (rich_location::maybe_add_fixit): Implement this
	restriction.

From-SVN: r249403
2017-06-20 10:40:38 +00:00
Martin Sebor c3684b7b86 PR c++/80560 - warn on undefined memory operations involving non-trivial types
gcc/c-family/ChangeLog:

	PR c++/80560
	* c.opt (-Wclass-memaccess): New option.

gcc/cp/ChangeLog:

	PR c++/80560
	* call.c (first_non_public_field, maybe_warn_class_memaccess): New
	functions.
	(has_trivial_copy_assign_p, has_trivial_copy_p): Ditto.
	(build_cxx_call): Call maybe_warn_class_memaccess.

gcc/ChangeLog:

	PR c++/80560
	* dumpfile.c (dump_register): Avoid calling memset to initialize
	a class with a default ctor.
	* gcc.c (struct compiler): Remove const qualification.
	* genattrtab.c (gen_insn_reserv): Replace memset with initialization.
	* hash-table.h: Ditto.
	* ipa-cp.c (allocate_and_init_ipcp_value): Replace memset with
	  assignment.
	* ipa-prop.c (ipa_free_edge_args_substructures): Ditto.
	* omp-low.c (lower_omp_ordered_clauses): Replace memset with
	default ctor.
	* params.h (struct param_info): Make struct members non-const.
	* tree-switch-conversion.c (emit_case_bit_tests): Replace memset
	with default initialization.
	* vec.h (vec_copy_construct, vec_default_construct): New helper
	functions.
	(vec<T>::copy, vec<T>::splice, vec<T>::reserve): Replace memcpy
	with vec_copy_construct.
	(vect<T>::quick_grow_cleared): Replace memset with default ctor.
	(vect<T>::vec_safe_grow_cleared, vec_safe_grow_cleared): Same.
	* doc/invoke.texi (-Wclass-memaccess): Document.

libcpp/ChangeLog:

	PR c++/80560
	* line-map.c (line_maps::~line_maps): Avoid calling htab_delete
	with a null pointer.
	(linemap_init): Avoid calling memset on an object of a non-trivial
	type.

libitm/ChangeLog:

	PR c++/80560
	* beginend.cc (GTM::gtm_thread::rollback): Avoid calling memset
	on an object of a non-trivial type.
	(GTM::gtm_transaction_cp::commit): Use assignment instead of memcpy
	to copy an object.
	* method-ml.cc (orec_iterator::reinit): Avoid -Wclass-memaccess.

gcc/testsuite/ChangeLog:

	PR c++/80560
	* g++.dg/Wclass-memaccess.C: New test.

From-SVN: r249234
2017-06-15 21:48:59 -06:00
David Malcolm b09649fdc6 Add support for mutually-incompatible fix-it hints
This patch adds a method:
  rich_location::fixits_cannot_be_auto_applied
for ensuring that mutually-incompatible fix-its hints don't
lead to insane output from -fdiagnostics-generate-patch.

Fix-it hints within such rich_location instances are printed
as normal by diagnostic_show_locus, but don't affect the output
of -fdiagnostics-generate-patch.

gcc/ChangeLog:
	* diagnostic.c (diagnostic_report_diagnostic): Only add fixits
	to the edit_context if they can be auto-applied.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic-test-show-locus-bw.c
	(test_mutually_exclusive_suggestions): New test function.
	* gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c
	(test_mutually_exclusive_suggestions): New test function.
	* gcc.dg/plugin/diagnostic-test-show-locus-parseable-fixits.c
	(test_mutually_exclusive_suggestions): New test function.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
	(test_show_locus): Add special-case for
	"test_mutually_exclusive_suggestions".

libcpp/ChangeLog:
	* include/line-map.h
	(rich_location::fixits_cannot_be_auto_applied): New method.
	(rich_location::fixits_can_be_auto_applied_p): New accessor.
	(rich_location::m_fixits_cannot_be_auto_applied): New field.
	* line-map.c (rich_location::rich_location): Initialize new field.

From-SVN: r249081
2017-06-09 20:57:38 +00:00
David Malcolm ad53f12355 Support fix-it hints that add new lines
Previously fix-it hints couldn't contain newlines.  This is
due to the need to print something user-readable for them
within diagnostic-show-locus, and for handling them within
edit-context for printing diffs and regenerating content.

This patch enables limited support for fix-it hints with newlines,
for suggesting adding new lines.
Such a fix-it hint must have exactly one newline character, at the
end of the content.  It must be an insertion at the beginning of
a line (so that e.g. fix-its that split a pre-existing line are
still rejected).

They are printed by diagnostic-show-locus with a '+' in the
left-hand margin, like this:

test.c:42:4: note: suggest adding 'break;' here
+      break;
     case 'b':
     ^~~~~~~~~

and the printer injects "spans" if the insertion location is not
near the primary range of the diagnostic e.g.:

test.c:4:2: note: unrecognized 'putchar'; suggest including '<stdio.h>'
test.c:1:1:
+#include <stdio.h>

test.c:4:2:
  putchar (ch);
  ^~~~~~~

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(layout::should_print_annotation_line_p): Make private.
	(layout::print_annotation_line): Make private.
	(layout::annotation_line_showed_range_p): Make private.
	(layout::show_ruler): Make private.
	(layout::print_source_line): Make private.  Pass in line and
	line_width, rather than calling location_get_source_line.  Drop
	returned value.
	(layout::print_leading_fixits): New method.
	(layout::print_any_fixits): Rename to...
	(layout::print_trailing_fixits): ...this, and make private.
	Don't print newline fixits.
	(diagnostic_show_locus): Move logic for printing one row into...
	(layout::print_line): ...this new function.  Move the
	location_get_source_line call and error-handling from
	print_source_line to here.  Call print_leading_fixits, and rename
	print_any_fixits to print_trailing_fixits.
	(selftest::test_fixit_insert_containing_newline): Update now that
	newlines are partially supported.
	(selftest::test_fixit_insert_containing_newline_2): New test.
	(selftest::test_fixit_replace_containing_newline): Update comments.
	(selftest::diagnostic_show_locus_c_tests): Call the new test.
	* edit-context.c (class added_line): New class.
	(class edited_line): Describe newline handling in comment.
	(edited_line::actually_edited_p): New method.
	(edited_line::print_content): Delete redundant decl.
	(edited_line::m_predecessors): New field.
	(edited_file::print_content): Call edited_line::print_content.
	(edited_file::print_diff): Update to support newlines.
	(edited_file::print_diff_hunk): Likewise.
	(edited_file::print_run_of_changed_lines): New function.
	(edited_file::print_diff_line): Convert to...
	(print_diff_line): ...this.
	(edited_file::get_effective_line_count): New function.
	(edited_line::edited_line): Initialize new field m_predecessors.
	(edited_line::~edited_line): Clean up m_predecessors.
	(edited_line::apply_fixit): Handle newlines.
	(edited_line::get_effective_line_count): New function.
	(edited_line::print_content): New function.
	(edited_line::print_diff_lines): New function.
	(selftest::test_applying_fixits_insert_containing_newline): New
	test.
	(selftest::test_applying_fixits_replace_containing_newline): New
	test.
	(selftest::insert_line): New function.
	(selftest::test_applying_fixits_multiple_lines): Add example of
	inserting a line.
	(selftest::edit_context_c_tests): Call the new tests.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic-test-show-locus-bw.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic-test-show-locus-color.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic-test-show-locus-parseable-fixits.c
	(test_fixit_insert_newline): New function.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
	(test_show_locus): Handle test_fixit_insert_newline.

libcpp/ChangeLog:
	* include/line-map.h (class rich_location): Update description of
	newline handling.
	(class fixit_hint): Likewise.
	(fixit_hint::ends_with_newline_p): New decl.
	* line-map.c (rich_location::maybe_add_fixit): Support newlines
	in fix-it hints that are insertions of single lines at the start
	of a line.  Don't consolidate into such fix-it hints.
	(fixit_hint::ends_with_newline_p): New method.

From-SVN: r247522
2017-05-02 19:03:56 +00:00
David Malcolm 338035aa19 Eliminate fixit_hint class hierarchy
The original implementation of fix-it hints (r230674) had an abstract
base class "fixit_hint" and three subclasses, representing
each of insertions, replacements, and deletions.

Having multiple classes for fix-it hints was a nuisance, as it required
per-class logic everywhere that the hints were handled.

In r239632 I eliminated the deletion subclass in favor of replacement
with the empty string (two subclasses are easier than three).

This patch eliminates the class hierarchy altogether by implementing
insertion in terms of replacement, by representing replacements via
a half-open interval (so that for an insertion, start == next location,
and we're effectively replacing an empty range at the insertion point
with the new string).

This greatly simplifies the code for handling fix-it hints; for example
it allows removal of a parallel class hierarchy of line_event within
edit-context.c.

It also improves consolidation of hints: we can now consolidate
insertions at the same location, affecting a couple of tests
(selftest::test_one_liner_many_fixits and
gcc.dg/Wmissing-braces-fixits.c).

gcc/ChangeLog:
	* diagnostic-show-locus.c (layout::get_expanded_location): Rewrite
	to use new fixit_hint representation, using the "replace" logic.
	(get_line_span_for_fixit_hint): Likewise.
	(layout::print_any_fixits): Likewise.
	(selftest::test_one_liner_many_fixits): Rename to...
	(selftest::test_one_liner_many_fixits_1): ...this, and update
	comment and expected output to reflect that the multiple fix-it
	hints are now consolidated into one insertion.
	(selftest::test_one_liner_many_fixits_2): New test.
	(selftest::test_diagnostic_show_locus_one_liner): Update for
	above.
	(selftest::test_fixit_consolidation): Update for fix-it API
	change.
	* diagnostic.c (print_parseable_fixits): Likewise.
	* edit-context.c (edited_line::m_line_events): Convert from
	auto_vec <line_event *> to auto_vec <line_event>.
	(class line_event): Convert from abstract base class to a concrete
	class, taking over the role of replace_event.
	(class insert_event): Delete.
	(class replace_event): Rename to class line_event.  Convert to
	half-open range.
	(edit_context::add_fixits): Reimplement.
	(edit_context::apply_insert): Delete.
	(edit_context::apply_replace): Rename to...
	(edit_context::apply_fixit): ...this.  Convert to half-open range.
	(edited_file::apply_insert): Delete.
	(edited_file::apply_replace): Rename to...
	(edited_file::apply_fixit): ...this.
	(edited_line::~edited_line): Drop deletion of events.
	(edited_line::apply_insert): Delete.
	(edited_line::apply_replace): Rename to...
	(edited_line::apply_fixit): ...this.  Convert to half-open range.
	Update for change to type of m_line_events.
	* edit-context.h (edit_context::apply_insert): Delete.
	(edit_context::apply_replace): Rename to...
	(edit_context::apply_fixit): ...this.

gcc/testsuite/ChangeLog:
	* gcc.dg/Wmissing-braces-fixits.c: Update expected output to
	reflect insertion fix-it hints at the same location now being
	consolidated.

libcpp/ChangeLog:
	* include/line-map.h (source_range::intersects_line_p): Delete.
	(rich_location::add_fixit): Delete.
	(rich_location::maybe_add_fixit): New method.
	(class fixit_hint): Reimplement in terms of...
	(class fixit_replace): ...this.
	(class fixit_insert): Delete.
	* line-map.c (linemap_position_for_loc_and_offset): Drop overzealous
	linemap_assert_fails.
	(source_range::intersects_line_p): Rename to...
	(fixit_hint::affects_line_p): New function.
	(rich_location::add_fixit_insert_before): Reimplement in terms of
	maybe_add_fixit, moving validation there.
	(rich_location::add_fixit_insert_after): Likewise.
	(column_before_p): Delete.
	(rich_location::add_fixit_replace): Reimplement in terms of
	maybe_add_fixit, moving validation there.  Convert closed input range
	to half-open range.
	(rich_location::add_fixit): Delete.
	(rich_location::maybe_add_fixit): New function.
	(fixit_insert::fixit_insert): Delete.
	(fixit_insert::~fixit_insert): Delete.
	(fixit_insert::affects_line_p): Delete.
	(fixit_insert::maybe_append_replace): Delete.
	(fixit_replace::fixit_replace): Rename to...
	(fixit_hint::fixit_hint): ...this, rewriting as necessary.
	(fixit_replace::~fixit_replace): Delete.
	(fixit_replace::affects_line_p): Delete.
	(fixit_replace::maybe_append_replace): Rename to...
	(fixit_hint::maybe_append): ...this, rewriting as necessary.

From-SVN: r247445
2017-05-01 19:15:36 +00:00
David Malcolm b9f4757f8e Fix issues with unrepresentable column numbers (PR c++/77949)
PR c++/77949 identifies an ICE when the C++ frontend attempts to emit a
fix-it hint inserting a missing semicolon at column 4097 of a source file.

This column value exceeds LINE_MAP_MAX_COLUMN_NUMBER and hence isn't
representable using a location_t.

Attempting to do so leads to these problems, which this patch fixes:

(a) when encountering a column number > LINE_MAP_MAX_COLUMN_NUMBER we
create a new linemap with m_column_and_range_bits == 0, but
linemap_position_for_column doesn't check for this, and hence can emit
a bogus location_t value that's calculated relative to the previous
linemap start, but which will be decoded relative to the new linemap,
leading to very large incorrect line values.

(b) when encountering a column number that can't be represented, and
for which the linemap was pre-existing, the code would hit this assertion:
  if (linemap_assert_fails (column < (1u << map->m_column_and_range_bits)))
around a bail-out condition.  The patch replaces this assertion with a
simple conditional, to stop the ICE when this occurs, and fixes the
bit count (effective column bits, vs column+range bits)

(c) the C++ frontend wasn't checking for failure of
linemap_position_for_loc_and_offset when considering emitting the fix-it
hint.  The patch adds a conditional, so that no fix-it hint is emitted
if the location is bogus.

gcc/cp/ChangeLog:
	PR c++/77949
	* parser.c (cp_parser_class_specifier_1): Only suggest inserting
	a missing semicolon if we have a valid insertion location for
	the fix-it hint.

gcc/ChangeLog:
	PR c++/77949
	* input.c (selftest::test_accessing_ordinary_linemaps): Verify
	that we correctly handle column numbers greater than
	LINE_MAP_MAX_COLUMN_NUMBER.

gcc/testsuite/ChangeLog:
	PR c++/77949
	* g++.dg/diagnostic/pr77949.C: New test case.

libcpp/ChangeLog:
	PR c++/77949
	* line-map.c (linemap_position_for_column): When calling
	linemap_start_line, detect if a new linemap was created with
	0 column bits, and bail out early if this is the case.
	(linemap_position_for_loc_and_offset): Replace overzealous
	linemap_assert_fails with a simple conditional; use correct
	bit count.

From-SVN: r244292
2017-01-10 21:54:09 +00:00
David Malcolm 5ccf1d8d10 Fix linemap corruption after very wide source lines (PR c++/72803)
PR c++/72803 describes an issue where a fix-it hint is to be emitted at
column 512 of a 511-column source line, leading to an ICE.

The root cause is a bug in linemap_line_start, when transitioning from
lines >= 512 in width to narrow lines.

The wide line in the reproducer has a line map with:
  m_column_and_range_bits = 15, m_range_bits = 5
giving 10 effective bits for representing columns, so that columns <= 1023
can be represented.

When parsing the following line,
  linemap_line_start (..., ..., max_column_hint=0);
is called.  This leads to the "add_map" logic, due to this condition:
      || (max_column_hint <= 80 && effective_column_bits >= 10)
i.e. the new line is sufficiently narrower than the old one to
potentially use a new linemap (so as to conserve values within the
location_t space).

It then attempts to avoid allocating a new line map.  Part of the logic
to determine if we really need a new line map is this condition:
   SOURCE_COLUMN (map, highest) >= (1U << column_bits)
The above condition is incorrect: we need to determine if the highest
column we've handed out will fit within the proposed *effective* column
bits, but "column_bits" here is the column plus the range bits, rather
than just the column bits.

Hence in this case linemap_line_start erroneously decides that we don't
need a new line map, and updates the column bits within the existing
line map, so any location_t values we've already handed out within it
that are offset from the start by
  >= (1<<new_column_and_range_bits)
effectively change meaning, leading to incorrect line&column information
when decoding them, and various "interesting" ways for the linemap
code to fail.

The fix is to use the effective column bits in the above conditional.

gcc/ChangeLog:
	PR c++/72803
	* input.c (selftest::test_accessing_ordinary_linemaps): Verify
	that the transition from a max line width >= 1<<10 to narrower
	lines works correctly.

gcc/testsuite/ChangeLog:
	PR c++/72803
	* g++.dg/diagnostic/pr72803.C: New test case.

libcpp/ChangeLog:
	PR c++/72803
	* line-map.c (linemap_line_start): When determining if the highest
	column given out so far will fit into a proposed change to the
	current map, use the effective number of column bits, rather than
	the total number of column + range bits.

From-SVN: r244199
2017-01-07 21:33:59 +00:00
Jakub Jelinek cbe34bb5ed Update copyright years.
From-SVN: r243994
2017-01-01 13:07:43 +01:00
David Malcolm 2be1b79650 Implement ~line_maps ()
line_maps instances such as the global line_table are
GC-managed, but the htab within location_adhoc_data_map.htab
is not GC-managed.

Previously this was deleted manually by a call to
location_adhoc_data_fini within toplev::main.

However, on adding a call to forcibly_ggc_collect after the
selftests, all of the htabs for the various line_tables
created during the selftests start showing up as leaks
in "make selftest-valgrind", e.g.:

13,536 (1,344 direct, 12,192 indirect) bytes in 12 blocks are definitely lost in loss record 1,065 of 1,086
   at 0x4A081D4: calloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x16DB3B0: xcalloc (xmalloc.c:163)
   by 0x16D8D34: htab_create_typed_alloc (hashtab.c:358)
   by 0x16D8DBD: htab_create_alloc (hashtab.c:286)
   by 0x16A2CCC: linemap_init(line_maps*, unsigned int) (line-map.c:353)
   by 0x1685605: selftest::line_table_test::line_table_test(selftest::line_table_case const&) (input.c:1624)
   by 0x167D09C: selftest::test_applying_fixits_modernize_named_init(selftest::line_table_case const&) (edit-context.c:1430)
   by 0x1686827: selftest::for_each_line_table_case(void (*)(selftest::line_table_case const&)) (input.c:3227)
   by 0x167F067: selftest::edit_context_c_tests() (edit-context.c:1658)
   by 0x1616E67: selftest::run_tests() (selftest-run-tests.c:71)
   by 0xC0DB25: toplev::run_self_tests() (toplev.c:2076)
   by 0x618EB4: toplev::main(int, char**) (toplev.c:2153)

This patch removes the manual one-time cleanup in favor of
adding a destructor to class line_maps, which cleans up
the non-GC-managed htab.

Doing so improves "make selftest-valgrind" from:

==61118== LEAK SUMMARY:
==61118==    definitely lost: 121,248 bytes in 1,515 blocks
==61118==    indirectly lost: 974,344 bytes in 959 blocks
==61118==      possibly lost: 0 bytes in 0 blocks
==61118==    still reachable: 1,332,599 bytes in 3,684 blocks
==61118==         suppressed: 0 bytes in 0 blocks

to:

==57182== LEAK SUMMARY:
==57182==    definitely lost: 13,840 bytes in 556 blocks
==57182==    indirectly lost: 0 bytes in 0 blocks
==57182==      possibly lost: 0 bytes in 0 blocks
==57182==    still reachable: 1,355,703 bytes in 3,684 blocks
==57182==         suppressed: 0 bytes in 0 blocks

gcc/ChangeLog:
	* toplev.c (toplev::main): Remove call to
	location_adhoc_data_fini.

libcpp/ChangeLog:
	* include/line-map.h (line_maps::~line_maps): New dtor.
	(location_adhoc_data_fini): Delete decl.
	* line-map.c (line_maps::~line_maps): New dtor.
	(location_adhoc_data_fini): Delete.

From-SVN: r241533
2016-10-25 18:10:44 +00:00
Jason Merrill 63cb392608 Add from_macro_definition_at predicate for locations.
gcc/
	* input.h (from_macro_definition_at): New.
libcpp/
	* line-map.c (linemap_location_from_macro_definition_p): New.
	* line-map.h: Declare it.

From-SVN: r240330
2016-09-21 15:59:29 -04:00
David Malcolm 3131620863 fix-it hints can't contain newlines
I hope to implement newline support within fix-it hints at some point,
but currently it's not supported, and leads to misleading diagnostic
output, so for now, fail gracefully.

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(selftest::test_fixit_insert_containing_newline): New function.
	(selftest::test_fixit_replace_containing_newline): New function.
	(selftest::diagnostic_show_locus_c_tests): Call the above.

libcpp/ChangeLog:
	* include/line-map.h (class rich_location): Note that newlines
	aren't supported in fix-it text.
	* line-map.c (rich_location::add_fixit_insert_before): Reject
	attempts to add fix-its containing newlines.
	(rich_location::add_fixit_replace): Likewise.

From-SVN: r240169
2016-09-15 23:57:01 +00:00
David Malcolm 254830bab2 fix-it hints: insert_before vs insert_after
The API for adding "insert text" fix-it hints was unclear
about exactly where the text should be inserted relative
to the given insertion point.

This patch clarifies things by renaming the pertinent methods from
  richloc.add_fixit_insert
to
  richloc.add_fixit_insert_before
and adding:
  richloc.add_fixit_insert_after

The latter allows us to consolidate some failure-handling into
class rich_location, rather than having to have every such diagnostic
check for it.

The patch also adds a description of how fix-it hints work to the
comment for class rich_location within libcpp/include/line-map.h.

gcc/c-family/ChangeLog:
	* c-common.c (warn_logical_not_parentheses): Replace
	rich_location::add_fixit_insert calls with add_fixit_insert_before
	and add_fixit_insert_after, eliminating the "next_loc" calculation.

gcc/c/ChangeLog:
	* c-parser.c (c_parser_declaration_or_fndef): Update for renaming
	of add_fixit_insert to add_fixit_insert_before.

gcc/cp/ChangeLog:
	* parser.c (cp_parser_class_specifier_1): Update for renaming of
	add_fixit_insert to add_fixit_insert_before.
	(cp_parser_class_head): Likewise.

gcc/ChangeLog:
	* diagnostic-show-locus.c (selftest::test_one_liner_fixit_insert):
	Rename to...
	(selftest::test_one_liner_fixit_insert_before): ...this, and update
	for renaming of add_fixit_insert to add_fixit_insert_before.
	(selftest::test_one_liner_fixit_insert_after): New function.
	(selftest::test_one_liner_fixit_validation_adhoc_locations):
	Update for renaming of add_fixit_insert to
	add_fixit_insert_before.
	(selftest::test_one_liner_many_fixits): Likewise.
	(selftest::test_diagnostic_show_locus_one_liner): Update for
	renaming, call new test function.
	(selftest::test_diagnostic_show_locus_fixit_lines): Update for
	renaming of add_fixit_insert to add_fixit_insert_before.
	(selftest::test_fixit_consolidation): Likewise.
	* diagnostic.c (selftest::test_print_parseable_fixits_insert):
	Likewise.
	* edit-context.c (selftest::test_applying_fixits_insert): Rename
	to...
	(selftest::test_applying_fixits_insert_before): ...this.
	(selftest::test_applying_fixits_insert): Update for renaming of
	add_fixit_insert to add_fixit_insert_before.
	(selftest::test_applying_fixits_insert_after): New function.
	(selftest::test_applying_fixits_insert_after_at_line_end): New
	function.
	(selftest::test_applying_fixits_insert_after_failure): New
	function.
	(selftest::test_applying_fixits_multiple): Update for renaming of
	add_fixit_insert to add_fixit_insert_before.
	(selftest::change_line): Likewise.
	(selftest::test_applying_fixits_unreadable_file): Likewise.
	(selftest::test_applying_fixits_line_out_of_range): Likewise.
	(selftest::test_applying_fixits_column_validation): Likewise.
	(selftest::test_applying_fixits_column_validation): Likewise.
	(selftest::edit_context_c_tests): Update for renamed test
	function; call new test functions.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
	(test_show_locus): Replace rich_location::add_fixit_insert calls
	with add_fixit_insert_before and add_fixit_insert_after.

libcpp/ChangeLog:
	* include/line-map.h (class rich_location): Add description of
	fix-it hints to leading comment.
	(rich_location::add_fixit_insert): Rename both overloaded methods
	to..
	(rich_location::add_fixit_insert_before): ...this, updating their
	comments.
	(rich_location::add_fixit_insert_after): Two new overloaded
	methods.
	(rich_location::stop_supporting_fixits): New method.
	* line-map.c (rich_location::add_fixit_insert): Rename both
	overloaded methods to..
	(rich_location::add_fixit_insert_before): ...this, updating their
	comments.
	(rich_location::add_fixit_insert_after): Two new methods.
	(rich_location::reject_impossible_fixit): Split out
	failure-handling into...
	(rich_location::stop_supporting_fixits): New method.

From-SVN: r240115
2016-09-13 16:08:59 +00:00
David Malcolm 3d4f9f878d diagnostic-show-locus.c: handle fixits on lines outside the regular ranges
The diagnostic_show_locus implementation determines the set
of line spans that need printing based on the ranges within the
rich_location (in layout::calculate_line_spans).

Currently this doesn't take into account fix-it hints, and hence
we fail to print fix-it hints that are on lines outside of
those ranges.

This patch updates the implementation to take fix-it hints into
account when calculating the pertinent line spans, so that such fix-it
hints do get printed.  It also adds some validation, to ensure that
we don't attempt to print fix-its hints affecting a different source
file.

gcc/ChangeLog:
	* diagnostic-show-locus.c (class layout): Add field m_fixit_hints.
	(layout_range::intersects_line_p): New method.
	(test_range_contains_point_for_single_point): Rename to...
	(test_layout_range_for_single_point): ...this, and add testing
	for layout_range::intersects_line_p.
	(test_range_contains_point_for_single_line): Rename to...
	(test_layout_range_for_single_line): ...this,  and add testing
	for layout_range::intersects_line_p.
	(test_range_contains_point_for_multiple_lines): Rename to...
	(test_layout_range_for_multiple_lines): ...this,  and add testing
	for layout_range::intersects_line_p.
	(layout::layout): Populate m_fixit_hints.
	(layout::get_expanded_location): Handle the case of a line-span
	for a fix-it hint.
	(layout::validate_fixit_hint_p): New method.
	(get_line_span_for_fixit_hint): New function.
	(layout::calculate_line_spans): Add spans for fixit-hints.
	(layout::should_print_annotation_line_p): New method.
	(layout::print_any_fixits): Drop param "richloc", instead using
	validated fixits in m_fixit_hints.  Add "const" to hint pointers.
	(diagnostic_show_locus): Avoid printing blank annotation lines.
	(selftest::test_diagnostic_context::test_diagnostic_context):
	Initialize show_column and start_span.
	(selftest::test_diagnostic_context::start_span_cb): New static
	function.
	(selftest::test_diagnostic_show_locus_fixit_lines): New function.
	(selftest::diagnostic_show_locus_c_tests): Update for function
	renamings.  Call test_diagnostic_show_locus_fixit_lines.

libcpp/ChangeLog:
	* include/line-map.h (class fixit_remove): Remove stray decl.
	(fixit_hint::affects_line_p): Make const.
	(fixit_insert::affects_line_p): Likewise.
	(fixit_replace::affects_line_p): Likewise.
	* line-map.c (fixit_insert::affects_line_p): Likewise.
	(fixit_replace::affects_line_p): Likewise.

From-SVN: r239906
2016-08-31 18:54:55 +00:00
David Malcolm b816477a5a Remove arbitrary limits from rich_location
This patch eliminates the hard-coded limits within rich_location
(up to 3 ranges, up to 2 fixits).  The common case is still
handled by embedding the values inside rich_location - it only
uses dynamic allocation if these limits are exceeded, so
creation of rich_location instances on the stack should still
be fast.  This is implemented via a new container class,
semi_embedded_vec <T, N>.

gcc/ChangeLog:
	* diagnostic-show-locus.c (colorizer::begin_state): Support more
	than 3 ranges per diagnostic by alternating between color 1 and
	color 2.
	(layout::layout): Replace use of rich_location::MAX_RANGES
	with richloc->get_num_locations ().
	(layout::calculate_line_spans): Replace use of
	rich_location::MAX_RANGES with m_layout_ranges.length ().
	(layout::print_annotation_line): Handle arbitrary numbers of
	ranges in caret-printing by defaulting to '^'.
	(selftest::test_one_liner_many_fixits): New function.
	(test_diagnostic_show_locus_one_liner): Call it.
	* diagnostic.c (diagnostic_initialize): Update for renaming
	of rich_location::MAX_RANGES to
	rich_location::STATICALLY_ALLOCATED_RANGES.
	* diagnostic.h (struct diagnostic_context): Likewise.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/diagnostic-test-show-locus-bw.c
	(test_many_nested_locations): New function.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c
	(test_show_locus): Handle "test_many_nested_locations".

libcpp/ChangeLog:
	* include/line-map.h (class semi_embedded_vec): New class.
	(semi_embedded_vec<T, NUM_EMBEDDED>::semi_embedded_vec): New ctor.
	(semi_embedded_vec<T, NUM_EMBEDDED>::~semi_embedded_vec): New
	dtor.
	(semi_embedded_vec<T, NUM_EMBEDDED>::operator[]): New methods.
	(semi_embedded_vec<T, NUM_EMBEDDED>::push): New method.
	(semi_embedded_vec<T, NUM_EMBEDDED>::truncate): New method.
	(rich_location::get_num_locations): Reimplement in terms of
	m_ranges.
	(rich_location::get_range): Make non-inline.
	(rich_location::get_num_fixit_hints): Reimplement in terms of
	m_fixit_hints.
	(rich_location::add_fixit): New function.
	(rich_location::MAX_RANGES): Rename to...
	(rich_location::STATICALLY_ALLOCATED_RANGES): ...this.
	(rich_location::MAX_FIXIT_HINTS): Rename to...
	(rich_location::STATICALLY_ALLOCATED_RANGES): ...this, and make
	private.
	(rich_location::m_num_ranges): Eliminate in favor of...
	(rich_location::m_ranges): ...this, converting from a fixed-size
	array to a semi_embedded_vec.
	(rich_location::m_num_fixit_hints): Eliminate in favor of...
	(rich_location::m_fixit_hints): ...this, converting from a
	fixed-size array to a semi_embedded_vec.
	* line-map.c (rich_location::rich_location): Update for above
	changes.
	(rich_location::~rich_location): Likewise.
	(rich_location::get_loc): Likewise.
	(rich_location::get_range): New methods.
	(rich_location::add_range): Update for above changes.
	(rich_location::set_range): Likewise.
	(rich_location::add_fixit_insert): Likewise.
	(rich_location::add_fixit_replace): Likewise.
	(rich_location::get_last_fixit_hint): Likewise.
	(rich_location::reject_impossible_fixit): Likewise.
	(rich_location::add_fixit): New method.

From-SVN: r239879
2016-08-31 00:35:01 +00:00
David Malcolm f908779801 rich_location: add convenience overloads for adding fix-it hints
Adding a fix-it hint to a diagnostic usually follows one of these
patterns:
(a) an insertion fix-its, with the insertion at the primary caret location
(b) a removals/replacements, affecting the range of the primary location

(other cases are possible, e.g. multiple fix-its, and affecting other
locations, but these are the common ones)

Given these common cases, this patch adds overloads of the rich_location
methods for adding fix-it hints, so that the location information can
be omitted if it matches that of the primary location within the
rich_location.

Similarly when adding "remove" and "replace" fix-it hints to a diagnostic,
it's tedious to have to extract the source_range from a location_t
(aka source_location).  To make this more convenient, this patch
adds overload of the rich_location::add_fixit_remove/replace methods,
accepting a source_location directly.

The patch updates the various in-tree users of fix-it hints to use
the new simpler API where appropriate.  I didn't touch the case where
there are multiple fix-its in one rich_location, as it seems better to
be more explicit about locations for this case (adding a pair of parens
in warn_logical_not_parentheses).

The above makes the gcc_rich_location::add_fixit_misspelled_id overload
taking a const char * rather redundant, so I eliminated it.

gcc/c/ChangeLog:
	* c-decl.c (implicit_decl_warning): Use add_fixit_replace
	rather than add_fixit_misspelled_id.
	(undeclared_variable): Likewise.
	* c-parser.c (c_parser_declaration_or_fndef): Likewise.  Remove
	now-redundant "here" params from add_fixit_insert method calls.
	(c_parser_parameter_declaration): Likewise.
	* c-typeck.c (build_component_ref): Remove now-redundant range
	param from add_fixit_replace method calls.

gcc/cp/ChangeLog:
	* name-lookup.c (suggest_alternatives_for): Use add_fixit_replace
	rather than add_fixit_misspelled_id.
	* parser.c (cp_parser_diagnose_invalid_type_name): Likewise.

gcc/ChangeLog:
	* diagnostic-show-locus.c (test_one_liner_fixit_insert): Remove
	redundant location param.
	(test_one_liner_fixit_remove): Likewise.
	(test_one_liner_fixit_replace): Likewise.
	(test_one_liner_fixit_replace_equal_secondary_range): Likewise.
	* gcc-rich-location.c
	(gcc_rich_location::add_fixit_misspelled_id): Eliminate call to
	get_range_from_loc.  Drop overload taking a const char *.
	* gcc-rich-location.h
	(gcc_rich_location::add_fixit_misspelled_id): Drop overload taking
	a const char *.

libcpp/ChangeLog:
	* include/line-map.h (rich_location::add_fixit_insert): Add
	comments.  Add overload omitting the source_location param.
	(rich_location::add_fixit_remove): Add comments.  Add overloads
	omitting the range, and accepting a source_location.
	(rich_location::add_fixit_replace): Likewise.
	* line-map.c (rich_location::add_fixit_insert): Add comments.  Add
	overload omitting the source_location param.
	(rich_location::add_fixit_remove): Add comments.  Add overloads
	omitting the range, and accepting a source_location.
	(rich_location::add_fixit_replace): Likewise.

From-SVN: r239861
2016-08-30 13:54:48 +00:00
David Malcolm 2aa514130a Allow the use of ad-hoc locations for fix-it hints
Currently the fix-it validator rejects ad-hoc locations.
Fix this by calling get_pure_location on the input locations to
add_fixit_insert/replace.  Doing so requires moving get_pure_location
from gcc to libcpp.

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(selftest::test_one_liner_fixit_validation_adhoc_locations): New
	function.
	(selftest::test_diagnostic_show_locus_one_liner): Call it.
	* input.c (get_pure_location): Move to libcpp/line-map.c.
	* input.h (get_pure_location): Convert decl to an inline function
	calling implementation in libcpp.

libcpp/ChangeLog:
	* include/line-map.h (get_pure_location): New decl.
	* line-map.c (get_pure_location): Move here, from gcc/input.c, adding
	a line_maps * param.
	(rich_location::add_fixit_insert): Call get_pure_location on "where".
	(rich_location::add_fixit_replace): Call get_pure_location on the
	end-points.

From-SVN: r239843
2016-08-29 20:42:57 +00:00
David Malcolm ee90851679 Add validation and consolidation of fix-it hints
The first aspect of this patch is to add some checking of fix-it hints.
The idea is to put this checking within the rich_location machinery,
rather than requiring every diagnostic to implement it for itself.

The fixits within a rich_location are "atomic": all must be valid for
any to be applicable.

We reject any fixits involving locations above
LINE_MAP_MAX_LOCATION_WITH_COLS.

There's no guarantee that it's sane to modify a macro, so we reject
any fix-its that touch them.

For example, note the attempt to provide a fix-it for the definition
of the macro FIELD:

spellcheck-fields-2.c: In function ‘test_macro’:
spellcheck-fields-2.c:26:15: error: ‘union u’ has no member named ‘colour’; did you mean ‘color’?
 #define FIELD colour
               ^
               color
spellcheck-fields-2.c:27:15: note: in expansion of macro ‘FIELD’
   return ptr->FIELD;
               ^~~~~

After this patch, the fixit is not displayed:

spellcheck-fields-2.c: In function ‘test_macro’:
spellcheck-fields-2.c:26:15: error: ‘union u’ has no member named ‘colour’; did you mean ‘color’?
 #define FIELD colour
               ^
spellcheck-fields-2.c:27:15: note: in expansion of macro ‘FIELD’
   return ptr->FIELD;
               ^~~~~

We might want some way for a diagnostic to opt-in to fix-its that
affect macros, but for now it's simplest to reject them.

The other aspect of this patch is fix-it consolidation: in some cases
neighboring fix-its can be merged.  For example, in a diagnostic to
modernize old-style struct initializers from:

 struct s example = {
- foo: 1,
+ .foo = 1,
 };

one approach would be to replace the "foo" with ".foo" and the ":"
with " =".  This would give two "replace" fix-its:

  foo: 1,
  --- FIXIT 1
  .foo
     - FIXIT 2
     =

This patch allows them to be consolidated into a single "replace" fix-it:

  foo: 1,
  ----
  .foo =

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(selftest::test_fixit_consolidation): New function.
	(selftest::diagnostic_show_locus_c_tests): Call it.
	* gcc-rich-location.h (gcc_rich_location): Eliminate unused
	constructor based on source_range.

gcc/testsuite/ChangeLog:
	* gcc.dg/spellcheck-fields-2.c (test): Move
	dg-begin/end-multiline-output within function body.
	(test_macro): New function.

libcpp/ChangeLog:
	* include/line-map.h (rich_location): Eliminate unimplemented
	constructor based on source_range.
	(rich_location::get_last_fixit_hint): New method.
	(rich_location::reject_impossible_fixit): New method.
	(rich_location): Add fields m_line_table and
	m_seen_impossible_fixit.
	(fixit_hint::maybe_append_replace): New pure virtual function.
	(fixit_insert::maybe_append_replace): New function.
	(fixit_replace::maybe_append_replace): New function.
	* line-map.c (rich_location::rich_location): Initialize
	m_line_table and m_seen_impossible_fixit.
	(rich_location::add_fixit_insert): Call
	reject_impossible_fixit and bail out if true.
	(column_before_p): New function.
	(rich_location::add_fixit_replace): Call reject_impossible_fixit
	and bail out if true.  Attempt to consolidate with neighboring
	fixits.
	(rich_location::get_last_fixit_hint): New method.
	(rich_location::reject_impossible_fixit): New method.
	(fixit_insert::maybe_append_replace): New method.
	(fixit_replace::maybe_append_replace): New method.

From-SVN: r239789
2016-08-26 21:25:41 +00:00
David Malcolm 2ffe0809cb Reimplement removal fix-it hints in terms of replace
This patch eliminates class fixit_remove, reimplementing
rich_location::add_fixit_remove in terms of replacement with the
empty string.  Deleting the removal subclass simplifies
fixit-handling code, as we only have two concrete fixit_hint
subclasses to deal with, rather than three.

The patch also fixes some problems in diagnostic-show-locus.c for
situations where a replacement fix-it has a different range to the
range of the diagnostic, by unifying the drawing of the two kinds of
fixits.  For example, this:

  foo = bar.field;
      ^
            m_field

becomes:

  foo = bar.field;
      ^
            -----
            m_field

showing the range to be replaced.

gcc/ChangeLog:
	* diagnostic-show-locus.c
	(layout::annotation_line_showed_range_p): New method.
	(layout::print_any_fixits): Remove case fixit_hint::REMOVE.
	Reimplement case fixit_hint::REPLACE to cover removals, and
	replacements where the range of the replacement isn't one
	of the ranges in the rich_location.
	(test_one_liner_fixit_replace): Likewise.
	(selftest::test_one_liner_fixit_replace_non_equal_range): New
	function.
	(selftest::test_one_liner_fixit_replace_equal_secondary_range):
	New function.
	(selftest::test_diagnostic_show_locus_one_liner): Call the new
	functions.
	* diagnostic.c (print_parseable_fixits): Remove case
	fixit_hint::REMOVE.

libcpp/ChangeLog:
	* include/line-map.h (fixit_hint::kind): Delete REPLACE.
	(class fixit_remove): Delete.
	* line-map.c (rich_location::add_fixit_remove): Reimplement
	by calling add_fixit_replace with an empty string.
	(fixit_remove::fixit_remove): Delete.
	(fixit_remove::affects_line_p): Delete.

From-SVN: r239632
2016-08-19 21:18:05 +00:00
David Malcolm 741d3be543 input.c: add lexing selftests and a test matrix for line_table states
This patch adds explicit testing of lexing a source file,
generalizing this (and the test of ordinary line maps) over
a 2-dimensional test matrix covering:

  (1) line_table->default_range_bits: some frontends use a non-zero value
  and others use zero

  (2) the fallback modes within line-map.c: there are various threshold
  values for source_location/location_t beyond line-map.c changes
  behavior (disabling of the range-packing optimization, disabling
  of column-tracking).  We exercise these by starting the line_table
  at interesting values at or near these thresholds.

This helps ensures that location data works in all of these states,
and that (I hope) we don't have lingering bugs relating to the
transition between line_table states.

gcc/ChangeLog:
	* input.c: Include cpplib.h.
	(selftest::temp_source_file): New class.
	(selftest::temp_source_file::temp_source_file): New ctor.
	(selftest::temp_source_file::~temp_source_file): New dtor.
	(selftest::should_have_column_data_p): New function.
	(selftest::test_should_have_column_data_p): New function.
	(selftest::temp_line_table): New class.
	(selftest::temp_line_table::temp_line_table): New ctor.
	(selftest::temp_line_table::~temp_line_table): New dtor.
	(selftest::test_accessing_ordinary_linemaps): Add case_ param; use
	it to create a temp_line_table.
	(selftest::assert_loceq): Only verify LOCATION_COLUMN for
	locations that are known to have column data.
	(selftest::line_table_case): New struct.
	(selftest::test_reading_source_line): Move tempfile handling
	to class temp_source_file.
	(ASSERT_TOKEN_AS_TEXT_EQ): New macro.
	(selftest::assert_token_loc_eq): New function.
	(ASSERT_TOKEN_LOC_EQ): New macro.
	(selftest::test_lexer): New function.
	(selftest::boundary_locations): New array.
	(selftest::input_c_tests): Call test_should_have_column_data_p.
	Loop over a test matrix of interesting values of location and
	default_range_bits, calling test_lexer on each case in the matrix.
	Move call to test_accessing_ordinary_linemaps into the matrix.
	* selftest.h (ASSERT_EQ): Reimplement in terms of...
	(ASSERT_EQ_AT): New macro.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/location_overflow_plugin.c (plugin_init): Avoid
	hardcoding the values of LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
	and LINE_MAP_MAX_LOCATION_WITH_COLS.

libcpp/ChangeLog:
	* include/line-map.h (LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES):
	Move here from line-map.c.
	(LINE_MAP_MAX_LOCATION_WITH_COLS): Likewise.
	* line-map.c (LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES): Move from
	here to line-map.h.
	(LINE_MAP_MAX_LOCATION_WITH_COLS): Likewise.

From-SVN: r238213
2016-07-11 16:02:20 +00:00
John David Anglin fe55692cb3 c-common.c (get_source_date_epoch): Use int64_t instead of long long.
* c-common.c (get_source_date_epoch): Use int64_t instead of long long.

	* gcov-tool.c (profile_rewrite): Use int64_t instead of long long.
	(do_rewrite): likewise.

	* line-map.c (location_adhoc_data_update): Use int64_t instead of
	long long.
	(get_combined_adhoc_loc): Likewise.

From-SVN: r237676
2016-06-22 01:46:06 +00:00
Bernd Schmidt 3caf0ca1d3 re PR preprocessor/69650 (ICE in linemap_line_start, at libcpp/line-map.c:803)
PR lto/69650
	* directives.c (do_linemarker): Test for file left but not entered
	here.
	* line-map.c (linemap_add): Not here.

	PR lto/69650
	* gcc.dg/pr69650.c: New test.

From-SVN: r234481
2016-03-25 10:15:39 -06:00
Richard Henderson 64567cfd1d Fix compiling large files
* line-map.c (new_linemap): Make alloc_size a size_t.

From-SVN: r234239
2016-03-15 16:08:45 -07:00
David Malcolm b4f3232d69 PR c++/70105: prevent nonsensical underline spew for macro expansions
diagnostic_show_locus can sometimes do the wrong thing when handling
expressions built up from macros.

PR c++/70105 (currently marked as a P3 regression) has an example of
a diagnostic where over 500 lines of irrelevant source are printed,
and underlined, giving >1000 lines of useless spew to stderr.

This patch adds extra sanitization to diagnostic-show-locus.c, so that
we only attempt to print underlines and secondary locations if such
locations are "sufficiently sane" relative to the primary location
of a diagnostic.

This "sufficiently sane" condition is implemented by a new helper
function compatible_locations_p, which requires such locations to
have the same macro expansion hierarchy as the primary location,
using linemap_macro_map_loc_unwind_toward_spelling, effectively
mimicing the expansion performed by LRK_SPELLING_LOCATION.

This may be too strong a condition, but it effectively fixes
PR c++/70105, without removing any underlines in my testing.

Successfully bootstrapped&regrtested in combination with the previous
patch on x86_64-pc-linux-gnu; adds 15 new PASS results to g++.sum
and 4 new PASS results to gcc.sum.

gcc/ChangeLog:
	PR c/68473
	PR c++/70105
	* diagnostic-show-locus.c (compatible_locations_p): New function.
	(layout::layout): Sanitize ranges using compatible_locations_p.

gcc/testsuite/ChangeLog:
	PR c/68473
	PR c++/70105
	* g++.dg/diagnostic/pr70105.C: New test.
	* gcc.dg/plugin/diagnostic-test-expressions-1.c (foo): New decl.
	(test_multiple_ordinary_maps): New test function.

libcpp/ChangeLog:
	PR c/68473
	PR c++/70105
	* line-map.c (linemap_macro_map_loc_unwind_toward_spelling): Move
	decl...
	* include/line-map.h
	(linemap_macro_map_loc_unwind_toward_spelling): ...here,
	converting from static to extern.

From-SVN: r234088
2016-03-09 18:23:27 +00:00
David Malcolm 40499f81a6 PR c++/70105: Defer location expansion until diagnostic_show_locus
gcc/ChangeLog:
	PR c/68473
	PR c++/70105
	* diagnostic-show-locus.c (layout_range::layout_range): Replace
	location_range param with three const expanded_locations * and a
	bool.
	(layout::layout): Replace call to
	rich_location::lazily_expand_location with get_expanded_location.
	Extract the range and perform location expansion here, passing
	the results to the layout_range ctor.
	* diagnostic.c (source_range::debug): Delete.
	* diagnostic.h (diagnostic_expand_location): Reimplement in terms
	of rich_location::get_expanded_location.
	* gcc-rich-location.c (get_range_for_expr): Delete.
	(gcc_rich_location::add_expr): Reimplement to avoid the
	rich_location::add_range overload that took a location_range,
	passing a location_t instead.

gcc/testsuite/ChangeLog:
	PR c/68473
	PR c++/70105
	* gcc.dg/plugin/diagnostic_plugin_show_trees.c (show_tree):
	Drop range information from call to inform_at_rich_loc.
	* gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (add_range):
	New.
	(test_show_locus): Replace calls to rich_location::add_range with
	calls to add_range.  Rewrite the tests that used the now-defunct
	rich_location ctor taking a source_range.  Simplify other tests
	by replacing calls to COMBINE_LOCATION_DATA with calls to
	make_location.

libcpp/ChangeLog:
	PR c/68473
	PR c++/70105
	* include/line-map.h (source_range::debug): Delete.
	(struct location_range): Update comment.  Replace
	expanded_location fields "m_start", "m_finish", and "m_caret" with
	a source_location field: "m_loc".
	(class rich_location): Reword comment.
	(rich_location::get_loc): Reimplement in terms of a new overloaded
	variant which takes an unsigned int.
	(rich_location::get_loc_addr): Delete.
	(rich_location::add_range): Drop params "start" and "finish" in
	favor of param "loc".  Drop overloaded variants taking a
	source_range or location_range *.
	(rich_location::lazily_expand_location): Delete in favor of...
	(rich_location::get_expanded_location): New decl.
	(rich_location::m_loc): Delete field.
	(rich_location::m_column_override): New field.
	* line-map.c (rich_location::rich_location):  Drop name of
	line_maps * param.  Update initializations for deletion of field
	"m_loc" and addition of field "m_column_override".  Reimplement
	body as a call to add_range.  Delete overloaded variant taking a
	source_range.
	(rich_location::get_loc): New function.
	(rich_location::lazily_expand_location): Delete in favor of...
	(rich_location::get_expanded_location): New function.
	(rich_location::override_column): Reimplement.
	(rich_location::add_range): Drop params "start" and "finish" in
	favor of param "loc".  Eliminate location expansion in favor of
	simply storing loc.  Drop overloaded variants taking a
	source_range or location_range *.
	(rich_location::set_range): Eliminate location expansion.

From-SVN: r234087
2016-03-09 18:14:43 +00:00
David Malcolm 7168133a37 PR preprocessor/69985: fix ICE with long lines in -Wformat
gcc/testsuite/ChangeLog:
	PR preprocessor/69985
	* gcc.dg/cpp/pr69985.c: New test case.

libcpp/ChangeLog:
	PR preprocessor/69985
	(linemap_position_for_loc_and_offset): Rename param from "offset"
	to "column_offset".  Right-shift the column_offset by m_range_bits
	of the pertinent ordinary map whenever offsetting a
	source_location.  For clarity, offset the column by the column
	offset, rather than the other way around.

From-SVN: r233836
2016-03-01 01:02:49 +00:00
David Malcolm 196440f844 PR preprocessor/69126: avoid comparing ad-hoc and non-ad-hoc locations
gcc/testsuite/ChangeLog:
	PR preprocessor/69126
	PR preprocessor/69543
	* c-c++-common/pr69126-2-long.c: New test.
	* c-c++-common/pr69126-2-short.c: New test.
	* c-c++-common/pr69543-1.c: Remove xfail.

libcpp/ChangeLog:
	PR preprocessor/69126
	PR preprocessor/69543
	* line-map.c (linemap_compare_locations): At the function top,
	replace inlined bodies of get_location_from_adhoc_loc with calls
	to get_location_from_adhoc_loc.  Add a pair of calls to
	get_location_from_adhoc_loc at the bottom of the function, to
	avoid meaningless comparisons of ad-hoc and non-ad-hoc locations.

From-SVN: r233638
2016-02-23 17:44:28 +00:00
David Malcolm 44714d8ce1 PR preprocessor/69664: fix rich_location::override_column
gcc/testsuite/ChangeLog:
	PR preprocessor/69664
	* gcc.dg/cpp/trad/comment-2.c: Add expected column number.
	* gcc.dg/cpp/warn-comments.c: Likewise.

libcpp/ChangeLog:
	PR preprocessor/69664
	* errors.c (cpp_diagnostic_with_line): Only call
	rich_location::override_column if the column is non-zero.
	* line-map.c (rich_location::override_column): Update columns
	within m_ranges[0].  Add assertions to verify that doing so is
	sane.

From-SVN: r233223
2016-02-08 17:33:45 +00:00
David Malcolm c7df95d83e PR preprocessor/69177 and PR c++/68819: libcpp fallbacks and -Wmisleading-indentation
gcc/c-family/ChangeLog:
	PR c++/68819
	* c-indentation.c (get_visual_column): Add location_t param.
	Handle the column number being zero by effectively disabling the
	warning, with an "inform".
	(should_warn_for_misleading_indentation): Add location_t argument
	for all uses of get_visual_column.

gcc/testsuite/ChangeLog:
	PR c++/68819
	PR preprocessor/69177
	* gcc.dg/plugin/location-overflow-test-1.c: New test case.
	* gcc.dg/plugin/location-overflow-test-2.c: New test case.
	* gcc.dg/plugin/location_overflow_plugin.c: New test plugin.
	* gcc.dg/plugin/plugin.exp (plugin_test_list): Add the above.

libcpp/ChangeLog:
	PR preprocessor/69177
	* line-map.c (LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES): New
	constant.
	(LINE_MAP_MAX_LOCATION_WITH_COLS): Add note about unit tests
	to comment.
	(can_be_stored_compactly_p): Reduce threshold from
	LINE_MAP_MAX_LOCATION_WITH_COLS to
	LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES.
	(get_combined_adhoc_loc): Likewise.
	(get_range_from_loc): Likewise.
	(linemap_line_start): Ensure that a new ordinary map is created
	when transitioning from range-packing being enabled to disabled,
	at the LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES threshold.  Set
	range_bits to 0 for new ordinary maps when beyond this limit.
	Prevent the "increase the column bits of a freshly created map"
	optimization if the range bits has reduced.

From-SVN: r232379
2016-01-14 19:10:17 +00:00