summaryrefslogtreecommitdiff
path: root/gcc/diagnostic.c
AgeCommit message (Collapse)Author
2020-02-17diagnostics: don't generate URLs that won't be usedDavid Malcolm
The two places in diagnostics.c where URLs are printed both do non-trivial work to generate the URL strings (including malloc/free), so don't do this work if URL-printing is disabled. gcc/ChangeLog: * diagnostic.c (print_any_cwe): Don't call get_cwe_url if URLs won't be printed. (print_option_information): Don't call get_option_url if URLs won't be printed.
2020-02-15PR 87488: Add --with-diagnostics-urls configuration optionBernd Edlinger
2020-02-15 David Malcolm <dmalcolm@redhat.com> Bernd Edlinger <bernd.edlinger@hotmail.de> PR 87488 PR other/93168 * config.in (DIAGNOSTICS_URLS_DEFAULT): New define. * configure.ac (--with-diagnostics-urls): New configuration option, based on --with-diagnostics-color. (DIAGNOSTICS_URLS_DEFAULT): New define. * config.h: Regenerate. * configure: Regenerate. * diagnostic.c (diagnostic_urls_init): Handle -1 for DIAGNOSTICS_URLS_DEFAULT from configure-time --with-diagnostics-urls=auto-if-env by querying for a GCC_URLS and TERM_URLS environment variable. * diagnostic-url.h (diagnostic_url_format): New enum type. (diagnostic_urls_enabled_p): rename to... (determine_url_format): ... this, and change return type. * diagnostic-color.c (parse_env_vars_for_urls): New helper function. (auto_enable_urls): Disable URLs on xfce4-terminal, gnome-terminal, the linux console, and mingw. (diagnostic_urls_enabled_p): rename to... (determine_url_format): ... this, and adjust. * pretty-print.h (pretty_printer::show_urls): rename to... (pretty_printer::url_format): ... this, and change to enum. * pretty-print.c (pretty_printer::pretty_printer, pp_begin_url, pp_end_url, test_urls): Adjust. * doc/install.texi (--with-diagnostics-urls): Document the new configuration option. (--with-diagnostics-color): Document the existing interaction with GCC_COLORS better. * doc/invoke.texi (-fdiagnostics-urls): Add GCC_URLS and TERM_URLS vindex reference. Update description of defaults based on the above. (-fdiagnostics-color): Update description of how -fdiagnostics-color interacts with GCC_COLORS.
2020-01-28diagnostic_metadata: unbreak xgettext (v2)David Malcolm
Changed in v2: - rename from warning_with_metadata_at to warning_meta - fix test plugins While C++ can have overloads, xgettext can't deal with overloads that have different argument positions, leading to two failures in "make gcc.pot": emit_diagnostic_valist used incompatibly as both --keyword=emit_diagnostic_valist:4 --flag=emit_diagnostic_valist:4:gcc-internal-format and --keyword=emit_diagnostic_valist:5 --flag=emit_diagnostic_valist:5:gcc-internal-format warning_at used incompatibly as both --keyword=warning_at:3 --flag=warning_at:3:gcc-internal-format and --keyword=warning_at:4 --flag=warning_at:4:gcc-internal-format The emit_diagnostic_valist overload isn't used anywhere (I think it's a leftover from an earlier iteration of the analyzer patch kit). The warning_at overload is used throughout the analyzer but nowhere else. Ideally I'd like to consolidate this argument with something constructable in various ways: - from a metadata and an int or - from an int (or, better an "enum opt_code"), so that the overload happens when implicitly choosing the ctor, but that feels like stage 1 material. In the meantime, fix xgettext by deleting the unused overload and renaming the used one. gcc/analyzer/ChangeLog: * region-model.cc (poisoned_value_diagnostic::emit): Update for renaming of warning_at overload to warning_meta. * sm-file.cc (file_leak::emit): Likewise. * sm-malloc.cc (double_free::emit): Likewise. (possible_null_deref::emit): Likewise. (possible_null_arg::emit): Likewise. (null_deref::emit): Likewise. (null_arg::emit): Likewise. (use_after_free::emit): Likewise. (malloc_leak::emit): Likewise. (free_of_non_heap::emit): Likewise. * sm-sensitive.cc (exposure_through_output_file::emit): Likewise. * sm-signal.cc (signal_unsafe_call::emit): Likewise. * sm-taint.cc (tainted_array_index::emit): Likewise. gcc/ChangeLog: * diagnostic-core.h (warning_at): Rename overload to... (warning_meta): ...this. (emit_diagnostic_valist): Delete decl of overload taking diagnostic_metadata. * diagnostic.c (emit_diagnostic_valist): Likewise for defn. (warning_at): Rename overload taking diagnostic_metadata to... (warning_meta): ...this. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_test_metadata.c: Update for renaming of warning_at overload to warning_meta. * gcc.dg/plugin/diagnostic_plugin_test_paths.c: Likewise.
2020-01-10Add diagnostic pathsDavid Malcolm
This patch adds support for associating a "diagnostic_path" with a diagnostic: a sequence of events predicted by the compiler that leads to the problem occurring, with their locations in the user's source, text descriptions, and stack information (for handling interprocedural paths). For example, the following (hypothetical) error has a 3-event intraprocedural path: test.c: In function 'demo': test.c:29:5: error: passing NULL as argument 1 to 'PyList_Append' which requires a non-NULL parameter 29 | PyList_Append(list, item); | ^~~~~~~~~~~~~~~~~~~~~~~~~ 'demo': events 1-3 | | 25 | list = PyList_New(0); | | ^~~~~~~~~~~~~ | | | | | (1) when 'PyList_New' fails, returning NULL | 26 | | 27 | for (i = 0; i < count; i++) { | | ~~~ | | | | | (2) when 'i < count' | 28 | item = PyLong_FromLong(random()); | 29 | PyList_Append(list, item); | | ~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) when calling 'PyList_Append', passing NULL from (1) as argument 1 | The patch adds a new "%@" format code for printing event IDs, so that in the above, the description of event (3) mentions event (1), showing the user where the bogus NULL value comes from (the event IDs are colorized to draw the user's attention to them). There is a separation between data vs presentation: the above shows how the diagnostic-printing code has consolidated the path into a single run of events, since all the events are near each other and within the same function; more complicated examples (such as interprocedural paths) might be printed as multiple runs of events. Examples of how interprocedural paths are printed can be seen in the test suite (which uses a plugin to exercise the code without relying on specific warnings using this functionality). Other output formats include - JSON, - printing each event as a separate "note", and - to not emit paths. gcc/ChangeLog: * Makefile.in (OBJS): Add tree-diagnostic-path.o. * common.opt (fdiagnostics-path-format=): New option. (diagnostic_path_format): New enum. (fdiagnostics-show-path-depths): New option. * coretypes.h (diagnostic_event_id_t): New forward decl. * diagnostic-color.c (color_dict): Add "path". * diagnostic-event-id.h: New file. * diagnostic-format-json.cc (json_from_expanded_location): Make non-static. (json_end_diagnostic): Call context->make_json_for_path if it exists and the diagnostic has a path. (diagnostic_output_format_init): Clear context->print_path. * diagnostic-path.h: New file. * diagnostic-show-locus.c (colorizer::set_range): Special-case when printing a run of events in a diagnostic_path so that they all get the same color. (layout::m_diagnostic_path_p): New field. (layout::layout): Initialize it. (layout::print_any_labels): Don't colorize the label text for an event in a diagnostic_path. (gcc_rich_location::add_location_if_nearby): Add "restrict_to_current_line_spans" and "label" params. Pass the former to layout.maybe_add_location_range; pass the latter when calling add_range. * diagnostic.c: Include "diagnostic-path.h". (diagnostic_initialize): Initialize context->path_format and context->show_path_depths. (diagnostic_show_any_path): New function. (diagnostic_path::interprocedural_p): New function. (diagnostic_report_diagnostic): Call diagnostic_show_any_path. (simple_diagnostic_path::num_events): New function. (simple_diagnostic_path::get_event): New function. (simple_diagnostic_path::add_event): New function. (simple_diagnostic_event::simple_diagnostic_event): New ctor. (simple_diagnostic_event::~simple_diagnostic_event): New dtor. (debug): New overload taking a diagnostic_path *. * diagnostic.def (DK_DIAGNOSTIC_PATH): New. * diagnostic.h (enum diagnostic_path_format): New enum. (json::value): New forward decl. (diagnostic_context::path_format): New field. (diagnostic_context::show_path_depths): New field. (diagnostic_context::print_path): New callback field. (diagnostic_context::make_json_for_path): New callback field. (diagnostic_show_any_path): New decl. (json_from_expanded_location): New decl. * doc/invoke.texi (-fdiagnostics-path-format=): New option. (-fdiagnostics-show-path-depths): New option. (-fdiagnostics-color): Add "path" to description of default GCC_COLORS; describe it. (-fdiagnostics-format=json): Document how diagnostic paths are represented in the JSON output format. * gcc-rich-location.h (gcc_rich_location::add_location_if_nearby): Add optional params "restrict_to_current_line_spans" and "label". * opts.c (common_handle_option): Handle OPT_fdiagnostics_path_format_ and OPT_fdiagnostics_show_path_depths. * pretty-print.c: Include "diagnostic-event-id.h". (pp_format): Implement "%@" format code for printing diagnostic_event_id_t *. (selftest::test_pp_format): Add tests for "%@". * selftest-run-tests.c (selftest::run_tests): Call selftest::tree_diagnostic_path_cc_tests. * selftest.h (selftest::tree_diagnostic_path_cc_tests): New decl. * toplev.c (general_init): Initialize global_dc->path_format and global_dc->show_path_depths. * tree-diagnostic-path.cc: New file. * tree-diagnostic.c (maybe_unwind_expanded_macro_loc): Make non-static. Drop "diagnostic" param in favor of storing the original value of "where" and re-using it. (virt_loc_aware_diagnostic_finalizer): Update for dropped param of maybe_unwind_expanded_macro_loc. (tree_diagnostics_defaults): Initialize context->print_path and context->make_json_for_path. * tree-diagnostic.h (default_tree_diagnostic_path_printer): New decl. (default_tree_make_json_for_path): New decl. (maybe_unwind_expanded_macro_loc): New decl. gcc/c-family/ChangeLog: * c-format.c (local_event_ptr_node): New. (PP_FORMAT_CHAR_TABLE): Add entry for "%@". (init_dynamic_diag_info): Initialize local_event_ptr_node. * c-format.h (T_EVENT_PTR): New define. gcc/testsuite/ChangeLog: * gcc.dg/format/gcc_diag-10.c (diagnostic_event_id_t): New typedef. (test_diag): Add coverage of "%@". * gcc.dg/plugin/diagnostic-path-format-default.c: New test. * gcc.dg/plugin/diagnostic-path-format-inline-events-1.c: New test. * gcc.dg/plugin/diagnostic-path-format-inline-events-2.c: New test. * gcc.dg/plugin/diagnostic-path-format-inline-events-3.c: New test. * gcc.dg/plugin/diagnostic-path-format-none.c: New test. * gcc.dg/plugin/diagnostic-test-paths-1.c: New test. * gcc.dg/plugin/diagnostic-test-paths-2.c: New test. * gcc.dg/plugin/diagnostic-test-paths-3.c: New test. * gcc.dg/plugin/diagnostic-test-paths-4.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_paths.c: New. * gcc.dg/plugin/plugin.exp: Add the new plugin and test cases. libcpp/ChangeLog: * include/line-map.h (class diagnostic_path): New forward decl. (rich_location::get_path): New accessor. (rich_location::set_path): New function. (rich_location::m_path): New field. * line-map.c (rich_location::rich_location): Initialize m_path. From-SVN: r280142
2020-01-01Update copyright years.Jakub Jelinek
From-SVN: r279813
2019-12-18Add diagnostic_metadata and CWE supportDavid Malcolm
This patch adds support for associating a diagnostic message with an optional diagnostic_metadata object, so that plugins can add extra data to their diagnostics (e.g. mapping a diagnostic to a taxonomy or coding standard such as from CERT or MISRA). Currently this only supports associating a CWE identifier with a diagnostic (which is what I'm using for the warnings in the analyzer patch kit), but adding a diagnostic_metadata class allows for future growth in this area without an explosion of further "warning_at" overloads for all of the different kinds of custom data that a plugin might want to add. This version of the patch renames the overly-general -fdiagnostics-show-metadata to -fdiagnostics-show-cwe and adds test coverage for it via a plugin. It also adds a note to the documentation that no GCC diagnostics currently use this; it's a feature for plugins (and, at some point, I hope, the analyzer). gcc/ChangeLog: * common.opt (fdiagnostics-show-cwe): Add. * diagnostic-core.h (class diagnostic_metadata): New forward decl. (warning_at): Add overload taking a const diagnostic_metadata &. (emit_diagnostic_valist): Add overload taking a const diagnostic_metadata *. * diagnostic-format-json.cc: Include "diagnostic-metadata.h". (json_from_metadata): New function. (json_end_diagnostic): Call it to add "metadata" child for diagnostics with metadata. (diagnostic_output_format_init): Clear context->show_cwe. * diagnostic-metadata.h: New file. * diagnostic.c: Include "diagnostic-metadata.h". (diagnostic_impl): Add const diagnostic_metadata * param. (diagnostic_n_impl): Likewise. (diagnostic_initialize): Initialize context->show_cwe. (diagnostic_set_info_translated): Initialize diagnostic->metadata. (get_cwe_url): New function. (print_any_cwe): New function. (diagnostic_report_diagnostic): Call print_any_cwe if the diagnostic has non-NULL metadata. (emit_diagnostic): Pass NULL as the metadata in the call to diagnostic_impl. (emit_diagnostic_valist): Likewise. (emit_diagnostic_valist): New overload taking a const diagnostic_metadata *. (inform): Pass NULL as the metadata in the call to diagnostic_impl. (inform_n): Likewise for diagnostic_n_impl. (warning): Likewise. (warning_at): Likewise. Add overload that takes a const diagnostic_metadata &. (warning_n): Pass NULL as the metadata in the call to diagnostic_n_impl. (pedwarn): Likewise for diagnostic_impl. (permerror): Likewise. (error): Likewise. (error_n): Likewise. (error_at): Likewise. (sorry): Likewise. (sorry_at): Likewise. (fatal_error): Likewise. (internal_error): Likewise. (internal_error_no_backtrace): Likewise. * diagnostic.h (diagnostic_info::metadata): New field. (diagnostic_context::show_cwe): New field. * doc/invoke.texi (-fno-diagnostics-show-cwe): New option. * opts.c (common_handle_option): Handle OPT_fdiagnostics_show_cwe. * toplev.c (general_init): Initialize global_dc->show_cwe. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic-test-metadata.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_metadata.c: New test plugin. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add them. From-SVN: r279556
2019-12-10diagnostic_show_locus: move initial newline to callersDavid Malcolm
diagnostic_show_locus adds a newline before doing anything (including the do-nothing-else case). This patch removes this initial newline, adding it to all callers of diagnostic_show_locus instead. Doing so makes diagnostic_show_locus more flexible, allowing it to be used in my analyzer patch kit for printing diagnostic paths. gcc/c-family/ChangeLog: * c-format.c (selftest::test_type_mismatch_range_labels): Remove initial newline from expected outputs. * c-opts.c (c_diagnostic_finalizer): Add pp_newline call before call to diagnostic_show_locus. gcc/ChangeLog: * diagnostic-show-locus.c (diagnostic_show_locus): Remove initial newline. (selftest::test_diagnostic_show_locus_unknown_location): Remove initial newline from expected outputs. (selftest::test_one_liner_simple_caret): Likewise. (selftest::test_one_liner_caret_and_range): Likewise. (selftest::test_one_liner_multiple_carets_and_ranges): Likewise. (selftest::test_one_liner_fixit_insert_before): Likewise. (selftest::test_one_liner_fixit_insert_after): Likewise. (selftest::test_one_liner_fixit_remove): Likewise. (selftest::test_one_liner_fixit_replace): Likewise. (selftest::test_one_liner_fixit_replace_non_equal_range): Likewise. (selftest::test_one_liner_fixit_replace_equal_secondary_range): Likewise. (selftest::test_one_liner_fixit_validation_adhoc_locations): Likewise. (selftest::test_one_liner_many_fixits_1): Likewise. (selftest::test_one_liner_many_fixits_2): Likewise. (selftest::test_one_liner_labels): Likewise. (selftest::test_one_liner_simple_caret_utf8): Likewise. (selftest::test_one_liner_caret_and_range_utf8): Likewise. (selftest::test_one_liner_multiple_carets_and_ranges_utf8): Likewise. (selftest::test_one_liner_fixit_insert_before_utf8): Likewise. (selftest::test_one_liner_fixit_insert_after_utf8): Likewise. (selftest::test_one_liner_fixit_remove_utf8): Likewise. (selftest::test_one_liner_fixit_replace_utf8): Likewise. (selftest::test_one_liner_fixit_replace_non_equal_range_utf8): Likewise. (selftest::test_one_liner_fixit_replace_equal_secondary_range_utf8): Likewise. (selftest::test_one_liner_fixit_validation_adhoc_locations_utf8): Likewise. (selftest::test_one_liner_many_fixits_1_utf8): Likewise. (selftest::test_one_liner_many_fixits_2_utf8): Likewise. (selftest::test_one_liner_labels_utf8): Likewise. (selftest::test_add_location_if_nearby): Likewise. (selftest::test_diagnostic_show_locus_fixit_lines): Likewise. (selftest::test_overlapped_fixit_printing): Likewise. (selftest::test_overlapped_fixit_printing_utf8): Likewise. (selftest::test_overlapped_fixit_printing_2): Likewise. (selftest::test_fixit_insert_containing_newline): Likewise. (selftest::test_fixit_insert_containing_newline_2): Likewise. (selftest::test_fixit_replace_containing_newline): Likewise. (selftest::test_fixit_deletion_affecting_newline): Likewise. (selftest::test_line_numbers_multiline_range): Likewise. * diagnostic.c (default_diagnostic_finalizer): Add pp_newline call before call to diagnostic_show_locus. (diagnostic_append_note): Likewise. gcc/fortran/ChangeLog: * error.c (gfc_diagnostic_starter): Add pp_newline call before call to diagnostic_show_locus. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (custom_diagnostic_finalizer): Add pp_newline call before call to diagnostic_show_locus. From-SVN: r279152
2019-10-10Documentation hyperlinks for [-Wname-of-option] (PR 87488)David Malcolm
This patch uses the support for pretty-printing escaped URLs added in the previous patch (PR 87488) so that in a sufficiently capable terminal the [-Wname-of-option] emitted at the end of each diagnostic becomes a hyperlink to the documentation for that option on the GCC website. I've tested it with Fedora 30's GNOME Terminal (3.32.2 with VTE 0.56.3); the option text is printed with a dotted underline, hovering shows the URL, and on right-clicking it offers menu items to visit/copy the URL. gcc/ChangeLog: PR 87488 * Makefile.in (CFLAGS-opts.o): Pass in DOCUMENTATION_ROOT_URL via -D. * configure.ac (--with-documentation-root-url): New option. * configure: Regenerate. * diagnostic-format-json.cc (json_end_diagnostic): If there is an option URL, add it as a new string field of the diagnostic option. * diagnostic.c (diagnostic_initialize): Initialize get_option_url. (print_option_information): If get_option_url is non-NULL, call it, and if the result is non-NULL, potentially emit an escape sequence to markup the option text with the resulting URL. * diagnostic.h (diagnostic_context::get_option_url): New callback. * doc/invoke.texi (-fdiagnostics-format=): Add "option_url" to example of JSON output. * opts-diagnostic.h (get_option_url): New decl. * opts.c (get_option_url): New function. * toplev.c (general_init): Initialize the get_option_url callback. gcc/testsuite/ChangeLog: PR 87488 * c-c++-common/diagnostic-format-json-2.c: Expect an "option_url" field. * c-c++-common/diagnostic-format-json-3.c: Likewise. * gfortran.dg/diagnostic-format-json-2.F90: Likewise. * gfortran.dg/diagnostic-format-json-3.F90: Likewise. * jit.dg/test-error-array-bounds.c (create_code): Ensure that error messages don't contain escaped URLs. From-SVN: r276843
2019-10-10pretty-print: support URL escape sequences (PR 87488)David Malcolm
https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda describes an emerging standard for embedding URLs in escape sequences for marking up text output. This is supported e.g. by recent releases of GNOME Terminal. This patch adds support to our pretty-printing framework for emitting URLs. A followup patch uses this to add URLs to the pertinent documentation for the output of -fdiagnostics-show-option. gcc/ChangeLog: PR 87488 * common.opt (fdiagnostics-urls=): New option. (diagnostic-url.h): Add SourceInclude. (diagnostic_url_rule): New enum. * diagnostic-color.c: Include "diagnostic-url.h". (diagnostic_urls_enabled_p): New function. * diagnostic-url.h: New file. * diagnostic.c: Include "diagnostic-url.h". (diagnostic_urls_init): New function. * diagnostic.h (diagnostic_urls_init): New decl. * doc/invoke.texi (Diagnostic Message Formatting Options): Add -fdiagnostics-urls to the list. (-fdiagnostics-urls): New option. * gcc.c (driver_handle_option): Handle OPT_fdiagnostics_urls_. (driver::global_initializations): Call diagnostic_urls_init. * opts-global.c (init_options_once): Likewise. * opts.c (common_handle_option): Handle OPT_fdiagnostics_urls_. * pretty-print.c (pretty_printer::pretty_printer): Initialize show_urls. (pp_begin_url): New function. (pp_end_url): New function. (selftest::test_urls): New selftest. (selftest::pretty_print_c_tests): Call it. * pretty-print.h (pretty_printer::show_urls): New field. (pp_begin_url): New decl. (pp_end_url): New decl. gcc/testsuite/ChangeLog: PR 87488 * lib/prune.exp (TEST_ALWAYS_FLAGS): Add -fdiagnostics-urls=never. From-SVN: r276841
2019-10-01Support prefixes in diagnostic_show_locusDavid Malcolm
Previously, diagnostic_show_locus saved and restored the pretty_printer's prefix, clearing it for the duration of the call. I have a patch kit in development that can benefit from applying a prefix to the output of d_s_l, so this patch adds support to d_s_l for printing such prefixes. It moves the save and restore of the pp's prefix from d_s_l to all of its callers, and updates diagnostic-show-locus.c to properly handle prefixes. gcc/c-family/ChangeLog: * c-opts.c (c_diagnostic_finalizer): Temporarily clear prefix when calling diagnostic_show_locus, rather than destroying it afterwards. gcc/ChangeLog: * diagnostic-show-locus.c (layout::print_gap_in_line_numbering): Call pp_emit_prefix. (layout::print_source_line): Likewise. (layout::start_annotation_line): Likewise. (diagnostic_show_locus): Remove call to temporarily clear the prefix. (selftest::test_one_liner_fixit_remove): Add test coverage for the interaction of pp_set_prefix with rulers and fix-it hints. * diagnostic.c (default_diagnostic_finalizer): Temporarily clear prefix when calling diagnostic_show_locus, rather than destroying it afterwards. (print_parseable_fixits): Temporarily clear prefix. * pretty-print.c (pp_format): Save and restore line_length, rather than assuming it is zero. (pp_output_formatted_text): Remove assertion that line_length is zero. gcc/fortran/ChangeLog: * error.c (gfc_diagnostic_starter): Clear the prefix before calling diagnostic_show_locus. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_group_plugin.c (test_begin_group_cb): Clear the prefix before emitting the "END GROUP" line. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (custom_diagnostic_finalizer): Temporarily clear prefix when calling diagnostic_show_locus, rather than destroying it afterwards. From-SVN: r276433
2019-07-24PR driver/80545 - option -Wstringop-overflow not recognized by FortranMartin Sebor
gcc/cp/ChangeLog: PR driver/80545 * decl.c (finish_function): Use lang_mask. gcc/testsuite/ChangeLog: PR driver/80545 * gcc.misc-tests/help.exp: Add tests. * lib/options.exp: Handle C++. gcc/ChangeLog: PR driver/80545 * diagnostic.c (diagnostic_classify_diagnostic): Use lang_mask. (diagnostic_report_diagnostic): Same. * diagnostic.h (diagnostic_context::option_enabled): Add an argument. (diagnostic_context::lang_mask): New data member. * ipa-pure-const.c (suggest_attribute): Use lang_hooks.option_lang_mask (). * opts-common.c (option_enabled): Handle new argument. (get_option_state): Pass an additional argument. * opts.c (print_filtered_help): Print supported languages for unsupported options. Adjust printing of current state. * opts.h (option_enabled): Add argument. * toplev.c (print_switch_values): Use lang_mask. (general_init): Set global_dc->lang_mask. From-SVN: r273771
2019-06-05c-decl.c (start_decl): Adjust quoting and hyphenation in diagnostics.Martin Sebor
gcc/c/ChangeLog: * c-decl.c (start_decl): Adjust quoting and hyphenation in diagnostics. (finish_decl): Same. (finish_enum): Same. (start_function): Same. (declspecs_add_type): Same. * c-parser.c (warn_for_abs): Same. * c-typeck.c (build_binary_op): Same. gcc/c-family/ChangeLog: * c-attribs.c (handle_mode_attribute): Adjust quoting and hyphenation. (handle_alias_ifunc_attribute): Same. (handle_copy_attribute): Same. (handle_weakref_attribute): Same. (handle_nonnull_attribute): Same. * c-warn.c (warn_for_sign_compare): Same. (warn_for_restrict): Same. * c.opt: Same. gcc/cp/ChangeLog: * call.c (build_conditional_expr_1): Adjust quoting and hyphenation. (convert_like_real): Same. (convert_arg_to_ellipsis): Same. * constexpr.c (diag_array_subscript): Same. * constraint.cc (diagnose_trait_expression): Same. * cvt.c (ocp_convert): Same. * decl.c (start_decl): Same. (check_for_uninitialized_const_var): Same. (grokfndecl): Same. (check_special_function_return_type): Same. (finish_enum_value_list): Same. (start_preparsed_function): Same. * parser.c (cp_parser_decl_specifier_seq): Same. * typeck.c (cp_build_binary_op): Same. (build_static_cast_1): Same. gcc/lto/ChangeLog: * lto-common.c (lto_file_finalize): Adjust quoting and hyphenation. gcc/objc/ChangeLog: * objc-act.c (objc_build_setter_call): Adjust quoting and hyphenation. * objc-encoding.c (encode_gnu_bitfield): Same. gcc/ChangeLog: * config/i386/i386-features.c (ix86_get_function_versions_dispatcher): Adjust quoting and hyphenation. * convert.c (convert_to_real_1): Same. * gcc.c (driver_wrong_lang_callback): Same. (driver::handle_unrecognized_options): Same. * gimple-ssa-nonnull-compare.c (do_warn_nonnull_compare): Same. * opts-common.c (cmdline_handle_error): Same. (read_cmdline_option): Same. * opts-global.c (complain_wrong_lang): Same. (print_ignored_options): Same. (handle_common_deferred_options): Same. * pretty-print.h: Same. * print-rtl.c (debug_bb_n_slim): Same. * sched-rgn.c (make_pass_sched_fusion): Same. * tree-cfg.c (verify_gimple_assign_unary): Same. (verify_gimple_label): Same. * tree-ssa-operands.c (verify_ssa_operands): Same. * varasm.c (do_assemble_alias): Same. (assemble_alias): Same. From-SVN: r271971
2019-01-01Update copyright years.Jakub Jelinek
From-SVN: r267494
2018-11-30C++: fix-it hint for missing parenthesesDavid Malcolm
Consider: class t1 { public: double length () const { return m_length; } private: double m_length; }; missing-parens-fixit.C: In function 'bool test_1(const t1&)': missing-parens-fixit.C:14:15: error: invalid use of member function 'double t1::length() const' (did you forget the '()' ?) 14 | return inst.length > 0.0; | ~~~~~^~~~~~ This patch adds a fix-it hint for the case where the member function takes no parameters, suggesting the addition of the parentheses: 14 | return inst.length > 0.0; | ~~~~~^~~~~~ | () so that an IDE can potentially apply the fix. gcc/cp/ChangeLog: * typeck2.c: Include "gcc-rich-location.h". (cxx_incomplete_type_diagnostic): When complaining about possibly missing parens, add a fix-it hint if the member function takes no additional params. gcc/ChangeLog: * diagnostic-core.h (emit_diagnostic): New decl. * diagnostic.c (emit_diagnostic): New overload, taking a rich_location *. gcc/testsuite/ChangeLog: * g++.dg/parse/missing-parens-fixit.C: New test. From-SVN: r266696
2018-11-27PR preprocessor/83173: Enhance -fdump-internal-locations outputMike Gulick
gcc/ChangeLog: 2018-11-27 Mike Gulick <mgulick@mathworks.com> PR preprocessor/83173 * input.c (dump_location_info): Dump reason and included_from fields from line_map_ordinary struct. Fix indentation when location > 5 digits. * diagnostic-show-locus.c (num_digits, num_digits): Move to diagnostic.c to allow it to be utilized by input.c. * diagnostic.c (num_digits, selftest::test_num_digits): Moved here. (selftest::diagnostic_c_tests): Run selftest::test_num_digits. * diagnostic.h (num_digits): Add extern definition. libcpp/ChangeLog: 2018-11-27 Mike Gulick <mgulick@mathworks.com> PR preprocessor/83173 * location-example.txt: Update example -fdump-internal-locations output. From-SVN: r266520
2018-11-15Machine-readable diagnostic output (PR other/19165)David Malcolm
This patch implements a -fdiagnostics-format=json option which converts the diagnostics to be output to stderr in a JSON format; see the documentation in invoke.texi. Logically-related diagnostics are nested at the JSON level, using the auto_diagnostic_group mechanism. gcc/ChangeLog: PR other/19165 * Makefile.in (OBJS): Move json.o to... (OBJS-libcommon): ...here and add diagnostic-format-json.o. * common.opt (fdiagnostics-format=): New option. (diagnostics_output_format): New enum. * diagnostic-format-json.cc: New file. * diagnostic.c (default_diagnostic_final_cb): New function, taken from start of diagnostic_finish. (diagnostic_initialize): Initialize final_cb to default_diagnostic_final_cb. (diagnostic_finish): Move "being treated as errors" messages to default_diagnostic_final_cb. Call any final_cb. (default_diagnostic_finalizer): Add diagnostic_t param. (diagnostic_report_diagnostic): Pass "orig_diag_kind" to diagnostic_finalizer callback. * diagnostic.h (enum diagnostics_output_format): New enum. (diagnostic_finalizer_fn): Reimplement, adding diagnostic_t param. (struct diagnostic_context): Add "final_cb". (default_diagnostic_finalizer): Add diagnostic_t param. (diagnostic_output_format_init): New decl. * doc/invoke.texi (-fdiagnostics-format): New option. * dwarf2out.c (gen_producer_string): Ignore OPT_fdiagnostics_format_. * gcc.c (driver_handle_option): Handle OPT_fdiagnostics_format_. * lto-wrapper.c (append_diag_options): Ignore it. * opts.c (common_handle_option): Handle it. gcc/c-family/ChangeLog: PR other/19165 * c-opts.c (c_diagnostic_finalizer): Add diagnostic_t param. gcc/fortran/ChangeLog: PR other/19165 * error.c (gfc_diagnostic_finalizer): Add diagnostic_t param. gcc/jit/ChangeLog: PR other/19165 * dummy-frontend.c (jit_begin_diagnostic): Add diagnostic_t param. gcc/testsuite/ChangeLog: PR other/19165 * c-c++-common/diagnostic-format-json-1.c: New test. * c-c++-common/diagnostic-format-json-2.c: New test. * c-c++-common/diagnostic-format-json-3.c: New test. * c-c++-common/diagnostic-format-json-4.c: New test. * c-c++-common/diagnostic-format-json-5.c: New test. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (custom_diagnostic_finalizer): Add diagnostic_t param. * gcc.dg/plugin/location_overflow_plugin.c (verify_unpacked_ranges): Likewise. (verify_no_columns): Likewise. * gfortran.dg/diagnostic-format-json-1.F90: New test. * gfortran.dg/diagnostic-format-json-2.F90: New test. * gfortran.dg/diagnostic-format-json-3.F90: New test. From-SVN: r266186
2018-11-13Eliminate source_location in favor of location_tDavid Malcolm
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-10-15diagnostics: add minimum width to left margin for line numbersDavid Malcolm
This patch adds a minimum width to the left margin used for printing line numbers. I set the default to 6. Hence rather than: some-filename:9:1: some message 9 | some source text | ^~~~~~~~~~~~~~~~ some-filename:10:1: another message 10 | more source text | ^~~~~~~~~~~~~~~~ we now print: some-filename:9:42: some message 9 | some source text | ^~~~~~~~~~~~~~~~ some-filename:10:42: another message 10 | more source text | ^~~~~~~~~~~~~~~~ This implicitly fixes issues with margins failing to line up due to different lengths of the number when we haven't read the full file yet and so don't know the highest possible line number, for line numbers up to 99999. Doing so adds some whitespace on the left-hand side, for non-huge files, at least. I believe that this makes it easier to see where each diagnostic starts, by visually breaking things up at the leftmost column; my hope is to make it easier for the eye to see the different diagnostics as if they were different "paragraphs". gcc/ChangeLog: * common.opt (fdiagnostics-minimum-margin-width=): New option. * diagnostic-show-locus.c (layout::layout): Apply the minimum margin width. (layout::start_annotation_line): Only print up to 3 of the margin character, to avoid touching the left-hand side. (selftest::test_diagnostic_show_locus_fixit_lines): Update for minimum margin width, as set by test_diagnostic_context's ctor. (selftest::test_fixit_insert_containing_newline): Likewise. (selftest::test_fixit_insert_containing_newline_2): Likewise. (selftest::test_line_numbers_multiline_range): Clear dc.min_margin_width. * diagnostic.c (diagnostic_initialize): Initialize min_margin_width. * diagnostic.h (struct diagnostic_context): Add field "min_margin_width". * doc/invoke.texi: Add -fdiagnostics-minimum-margin-width=. * opts.c (common_handle_option): Handle OPT_fdiagnostics_minimum_margin_width_. * selftest-diagnostic.c (selftest::test_diagnostic_context::test_diagnostic_context): Initialize min_margin_width to 6. * toplev.c (general_init): Initialize global_dc->min_margin_width. gcc/testsuite/ChangeLog: * gcc.dg/missing-header-fixit-3.c: Update expected indentation to reflect minimum margin width. * gcc.dg/missing-header-fixit-4.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers-2.c: New test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add it. From-SVN: r265178
2018-09-11Add sorry_at diagnostic function.Andrew Stubbs
The plain "sorry" diagnostic only gives the "current" location, which is typically the last line of the function or translation unit by time we get to the back end. GCN uses "sorry" to report unsupported language features, such as static constructors, so it's useful to have a "sorry_at" variant. This patch implements "sorry_at" according to the pattern of the other "at" variants. 2018-09-11 Andrew Stubbs <ams@codesourcery.com> gcc/ * diagnostic-core.h (sorry_at): New prototype. * diagnostic.c (sorry_at): New function. From-SVN: r264204
2018-08-20Add support for grouping of related diagnostics (PR other/84889)David Malcolm
We often emit logically-related groups of diagnostics. A relatively simple case is this: if (warning_at (body_loc, OPT_Wmultistatement_macros, "macro expands to multiple statements")) inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); where the "note" diagnostic issued by the "inform" call is guarded by the -Wmultistatement_macros warning. More complicated examples can be seen in the C++ frontend, where e.g. print_z_candidates can lead to numerous "note" diagnostics being emitted. I'm looking at various ways to improve how we handle such related diagnostics, but, prior to this patch, there was no explicit relationship between these diagnostics: the diagnostics subsystem had no way of "knowing" that these were related. This patch introduces a simple way to group the diagnostics: an auto_diagnostic_group class: all diagnostics emitted within the lifetime of an auto_diagnostic_group instance are logically grouped. Hence in the above example, the two diagnostics can be grouped by simply adding an auto_diagnostic_group instance: auto_diagnostic_group d; if (warning_at (body_loc, OPT_Wmultistatement_macros, "macro expands to multiple statements")) inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); Some more awkard cases are of the form: if (some_condition && warning_at (...) && more_conditions) inform (...); which thus need restructuring to: if (some_condition) { auto_diagnostic_group d; warning_at (...); if (more_conditions) inform (...); } so that the lifetime of the auto_diagnostic_group groups the warning_at and the inform call. Nesting is handled by simply tracking a nesting depth within the diagnostic_context.: all diagnostics are treated as grouped until the final auto_diagnostic_group is popped. diagnostic.c uses this internally, so that all diagnostics are part of a group - those that are "by themselves" are treated as being part of a group with one element. The diagnostic_context gains optional callbacks for displaying the start of a group (when the first diagnostic is emitted within it), and the end of a group (for when the group was non-empty); these callbacks are unused by default, but a test plugin demonstrates them (and verifies that the machinery is working). As noted above, I'm looking at various ways to use the grouping to improve how we output the diagnostics. FWIW, I experimented with a more involved implementation, of the form: diagnostic_group d; if (d.warning_at (body_loc, OPT_Wmultistatement_macros, "macro expands to multiple statements")) d.inform (guard_loc, "some parts of macro expansion are not guarded by " "this %qs clause", guard_tinfo_to_string (keyword)); which had the advantage of allowing auto-detection of the places where groups were needed (by converting ::warning_at's return type to bool), but it was a much more invasive patch, especially when dealing with the places in the C++ frontend that can emit numerous notes after an error or warning (and thus having to pass the group around) Hence I went with this simpler approach. gcc/c-family/ChangeLog: PR other/84889 * c-attribs.c (common_handle_aligned_attribute): Add auto_diagnostic_group instance. * c-indentation.c (warn_for_misleading_indentation): Likewise. * c-opts.c (c_common_post_options): Likewise. * c-warn.c (warn_logical_not_parentheses): Likewise. (warn_duplicated_cond_add_or_warn): Likewise. (warn_for_multistatement_macros): Likewise. gcc/c/ChangeLog: PR other/84889 * c-decl.c (pushtag): Add auto_diagnostic_group instance. (diagnose_mismatched_decls): Likewise, in various places. (warn_if_shadowing): Likewise. (implicit_decl_warning): Likewise. (implicitly_declare): Likewise. (undeclared_variable): Likewise. (declare_label): Likewise. (grokdeclarator): Likewise. (start_function): Likewise. * c-parser.c (c_parser_declaration_or_fndef): Likewise. (c_parser_parameter_declaration): Likewise. (c_parser_binary_expression): Likewise. * c-typeck.c (c_expr_sizeof_expr): Likewise. (parser_build_binary_op): Likewise. (build_unary_op): Likewise. (error_init): Likewise. (pedwarn_init): Likewise. (warning_init): Likewise. (convert_for_assignment): Likewise. gcc/cp/ChangeLog: PR other/84889 * call.c (build_user_type_conversion_1): Add auto_diagnostic_group instance(s). (print_error_for_call_failure): Likewise. (build_op_call_1): Likewise. (build_conditional_expr_1): Likewise. (build_new_op_1): Likewise. (build_op_delete_call): Likewise. (convert_like_real): Likewise. (build_over_call): Likewise. (build_new_method_call_1): Likewise. (joust): Likewise. * class.c (check_tag): Likewise. (finish_struct_anon_r): Likewise. (one_inherited_ctor): Likewise. (finalize_literal_type_property): Likewise. (explain_non_literal_class): Likewise. (find_flexarrays): Likewise. (resolve_address_of_overloaded_function): Likewise. * constexpr.c (ensure_literal_type_for_constexpr_object): Likewise. (is_valid_constexpr_fn): Likewise. (cx_check_missing_mem_inits): Likewise. * cp-gimplify.c (cp_genericize_r): Likewise. * cvt.c (maybe_warn_nodiscard): Likewise. * decl.c (warn_extern_redeclared_static): Likewise. (check_redeclaration_exception_specification): Likewise. (check_no_redeclaration_friend_default_args): Likewise. (duplicate_decls): Likewise. (redeclaration_error_message): Likewise. (warn_misplaced_attr_for_class_type): Likewise. * decl2.c (finish_static_data_member_decl): Likewise. (no_linkage_error): Likewise. (cp_warn_deprecated_use): Likewise. * error.c (qualified_name_lookup_error): Likewise. * friend.c (make_friend_class): Likewise. (do_friend): Likewise. * init.c (perform_member_init): Likewise. (build_new_1): Likewise. (build_vec_delete_1): Likewise. (build_delete): Likewise. * lex.c (unqualified_name_lookup_error): Likewise. * name-lookup.c (check_extern_c_conflict): Likewise. (inform_shadowed): New function. (check_local_shadow): Add auto_diagnostic_group instances, replacing goto "inform_shadowed" label with call to subroutine. (set_local_extern_decl_linkage): Add auto_diagnostic_group instance(s). * parser.c (cp_parser_diagnose_invalid_type_name): Likewise. (cp_parser_namespace_name): Likewise. * pt.c (check_specialization_namespace): Likewise. (check_template_variable): Likewise. (warn_spec_missing_attributes): Likewise. (check_explicit_specialization): Likewise. (process_partial_specialization): Likewise. (lookup_template_class_1): Likewise. (finish_template_variable): Likewise. (do_auto_deduction): Likewise. * search.c (check_final_overrider): Likewise. (look_for_overrides_r): Likewise. * tree.c (maybe_warn_parm_abi): Likewise. * typeck.c (cxx_sizeof_expr): Likewise. (cp_build_function_call_vec): Likewise. (cp_build_binary_op): Likewise. (convert_for_assignment): Likewise. (maybe_warn_about_returning_address_of_local): Likewise. * typeck2.c (abstract_virtuals_error_sfinae): Likewise. (check_narrowing): Likewise. gcc/ChangeLog: PR other/84889 * attribs.c (diag_attr_exclusions): Add auto_diagnostic_group instance. (decl_attributes): Likewise. * calls.c (maybe_warn_nonstring_arg): Add auto_diagnostic_group instance. * cgraphunit.c (maybe_diag_incompatible_alias): Likewise. * diagnostic-core.h (class auto_diagnostic_group): New class. * diagnostic.c (diagnostic_initialize): Initialize the new fields. (diagnostic_report_diagnostic): Handle the first diagnostics within a group. (emit_diagnostic): Add auto_diagnostic_group instance. (inform): Likewise. (inform_n): Likewise. (warning): Likewise. (warning_at): Likewise. (warning_n): Likewise. (pedwarn): Likewise. (permerror): Likewise. (error): Likewise. (error_n): Likewise. (error_at): Likewise. (sorry): Likewise. (fatal_error): Likewise. (internal_error): Likewise. (internal_error_no_backtrace): Likewise. (auto_diagnostic_group::auto_diagnostic_group): New ctor. (auto_diagnostic_group::~auto_diagnostic_group): New dtor. * diagnostic.h (struct diagnostic_context): Add fields "diagnostic_group_nesting_depth", "diagnostic_group_emission_count", "begin_group_cb", "end_group_cb". * gimple-ssa-isolate-paths.c (find_implicit_erroneous_behavior): Add auto_diagnostic_group instance(s). (find_explicit_erroneous_behavior): Likewise. * gimple-ssa-warn-alloca.c (pass_walloca::execute): Likewise. * gimple-ssa-warn-restrict.c (maybe_diag_offset_bounds): Likewise. * gimplify.c (warn_implicit_fallthrough_r): Likewise. (gimplify_va_arg_expr): Likewise. * hsa-gen.c (HSA_SORRY_ATV): Likewise. (HSA_SORRY_AT): Likewise. * ipa-devirt.c (compare_virtual_tables): Likewise. (warn_odr): Likewise. * multiple_target.c (expand_target_clones): Likewise. * opts-common.c (cmdline_handle_error): Likewise. * reginfo.c (globalize_reg): Likewise. * substring-locations.c (format_warning_n_va): Likewise. * tree-inline.c (expand_call_inline): Likewise. * tree-ssa-ccp.c (pass_post_ipa_warn::execute): Likewise. * tree-ssa-loop-niter.c (do_warn_aggressive_loop_optimizations): Likewise. * tree-ssa-uninit.c (warn_uninit): Likewise. * tree.c (warn_deprecated_use): Likewise. gcc/testsuite/ChangeLog: PR other/84889 * gcc.dg/plugin/diagnostic-group-test-1.c: New test. * gcc.dg/plugin/diagnostic_group_plugin.c: New test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests. From-SVN: r263675
2018-08-16diagnostics: fix bad interaction between line spans and line numbersDavid Malcolm
Without this patch, the "line span" markers and the line numbering interacted badly, leading to stray copies of the line-span markers appearing as prefixes on the first source line in a span: missing-header-fixit-3.c: In function 'test': missing-header-fixit-3.c:9:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration] 9 | printf ("%i of %i\n", i, j); | ^~~~~~ missing-header-fixit-3.c:9:3: warning: incompatible implicit declaration of built-in function 'printf' missing-header-fixit-3.c:9:3: note: include '<stdio.h>' or provide a declaration of 'printf' missing-header-fixit-3.c:1:1: |+#include <stdio.h> missing-header-fixit-3.c:1:1:1 | /* Example of a fix-it hint that adds a #include directive, missing-header-fixit-3.c:9:3: missing-header-fixit-3.c:9:3:9 | printf ("%i of %i\n", i, j); | ^~~~~~ With this patch, we now correctly print: missing-header-fixit-3.c: In function 'test': missing-header-fixit-3.c:9:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration] 9 | printf ("%i of %i\n", i, j); | ^~~~~~ missing-header-fixit-3.c:9:3: warning: incompatible implicit declaration of built-in function 'printf' missing-header-fixit-3.c:9:3: note: include '<stdio.h>' or provide a declaration of 'printf' missing-header-fixit-3.c:1:1: + |+#include <stdio.h> 1 | /* Example of a fix-it hint that adds a #include directive, missing-header-fixit-3.c:9:3: 9 | printf ("%i of %i\n", i, j); | ^~~~~~ gcc/ChangeLog: * diagnostic.c (default_diagnostic_start_span_fn): Call pp_string to emit the span, rather than setting it as the prefix. gcc/testsuite/ChangeLog: * gcc.dg/missing-header-fixit-3.c: New test. From-SVN: r263606
2018-08-15diagnostics: add labeling of source rangesDavid Malcolm
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-09diagnostics: add line numbers to source (PR other/84889)David Malcolm
This patch adds a left margin to the lines of source (and annotations) printed by diagnostic_show_locus, so that e.g. rather than: test.c: In function 'test': test.c:12:15: error: 'struct foo' has no member named 'm_bar'; did you mean 'bar'? return ptr->m_bar; ^~~~~ bar we print: test.c: In function 'test': test.c:12:15: error: 'struct foo' has no member named 'm_bar'; did you mean 'bar'? 12 | return ptr->m_bar; | ^~~~~ | bar Similarly, for a multiline case (in C++ this time), this: bad-binary-ops.C: In function 'int test_2()': bad-binary-ops.C:26:4: error: no match for 'operator+' (operand types are 's' and 't') return (some_function () ~~~~~~~~~~~~~~~~ + some_other_function ()); ^~~~~~~~~~~~~~~~~~~~~~~~ becomes: bad-binary-ops.C: In function 'int test_2()': bad-binary-ops.C:26:4: error: no match for 'operator+' (operand types are 's' and 't') 25 | return (some_function () | ~~~~~~~~~~~~~~~~ 26 | + some_other_function ()); | ^~~~~~~~~~~~~~~~~~~~~~~~ I believe this slightly improves the readability of the output, in that it: - distinguishes between the user's source code vs the annotation lines that we're adding (the underlinings and fix-it hints here) - shows the line numbers in another place (potentially helpful for multiline diagnostics, where the user can see the line numbers directly, rather than have to figure them out relative to the caret: in the 2nd example, note how the diagnostic is reported at line 26, but the first line printed is actually line 25) I'm not sure that this is the precise format we want to go with [1], but I think it's an improvement over the status quo, and we're in stage 1 of gcc 9, so there's plenty of time to shake out issues. I've turned it on by default; it can be disabled via -fno-diagnostics-show-line-numbers (it's also turned off in the testsuite, to avoid breaking numerous existing test cases). [1] Some possible variants: - maybe just "LL|" rather than "LL | " - maybe ':' rather than '|' - maybe we should have some leading indentation, to better split up the diagnostics visually via the left-hand column - etc gcc/ChangeLog: PR other/84889 * common.opt (fdiagnostics-show-line-numbers): New option. * diagnostic-show-locus.c (class layout): Add fields "m_show_line_numbers_p" and "m_linenum_width"; (num_digits): New function. (test_num_digits): New function. (layout::layout): Initialize new fields. Update m_x_offset logic to handle any left margin. (layout::print_source_line): Print line number when requested. (layout::start_annotation_line): New member function. (layout::print_annotation_line): Call it. (layout::print_leading_fixits): Likewise. (layout::print_trailing_fixits): Likewise. Update calls to move_to_column for new parameter. (layout::get_x_bound_for_row): Add "add_left_margin" param and use it to potentially call start_annotation_line. (layout::show_ruler): Call start_annotation_line. (selftest::test_line_numbers_multiline_range): New selftest. (selftest::diagnostic_show_locus_c_tests): Call test_num_digits and selftest::test_line_numbers_multiline_range. * diagnostic.c (diagnostic_initialize): Initialize show_line_numbers_p. * diagnostic.h (struct diagnostic_context): Add field "show_line_numbers_p". * doc/invoke.texi (Diagnostic Message Formatting Options): Add -fno-diagnostics-show-line-numbers. * dwarf2out.c (gen_producer_string): Add OPT_fdiagnostics_show_line_numbers to the ignored options. * lto-wrapper.c (merge_and_complain): Likewise 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. * toplev.c (general_init): Set up global_dc->show_line_numbers_p. gcc/testsuite/ChangeLog: PR other/84889 * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c: New test. * gcc.dg/plugin/diagnostic-test-show-locus-color-line-numbers.c: New test. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add the new tests. * lib/prune.exp: Add -fno-diagnostics-show-line-numbers to TEST_ALWAYS_FLAGS. From-SVN: r263450
2018-08-08[PATCH] line-map include-from representationNathan Sidwell
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-06[PATCH] Diagnostic included-from loopNathan Sidwell
https://gcc.gnu.org/ml/gcc-patches/2018-08/msg00416.html * diagnostic.c (diagnostic_report_current_module): Reroll included-at loop. Translate text. From-SVN: r263341
2018-08-02Fix memory leak of pretty_printer prefixesDavid Malcolm
We were rather sloppy about handling the ownership of prefixes for pretty_printer, and this lead to a memory leak for any time a diagnostic_show_locus call emits multiple line spans. This showed up in "make selftest-valgrind" as: 3,976 bytes in 28 blocks are definitely lost in loss record 632 of 669 at 0x4A0645D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x1F08227: xmalloc (xmalloc.c:147) by 0x1F083E6: xvasprintf (xvasprintf.c:58) by 0x1E7EC7D: build_message_string(char const*, ...) (diagnostic.c:78) by 0x1E7F438: diagnostic_get_location_text(diagnostic_context*, expanded_location) (diagnostic.c:328) by 0x1E7FD54: default_diagnostic_start_span_fn(diagnostic_context*, expanded_location) (diagnostic.c:626) by 0x1EB3508: selftest::test_diagnostic_context::start_span_cb(diagnostic_context*, expanded_location) (selftest-diagnostic.c:57) by 0x1E89215: diagnostic_show_locus(diagnostic_context*, rich_location*, diagnostic_t) (diagnostic-show-locus.c:1992) by 0x1E8ECAD: selftest::test_fixit_insert_containing_newline_2(selftest::line_table_case const&) (diagnostic-show-locus.c:3044) by 0x1EB0606: selftest::for_each_line_table_case(void (*)(selftest::line_table_case const&)) (input.c:3525) by 0x1E8F3F5: selftest::diagnostic_show_locus_c_tests() (diagnostic-show-locus.c:3164) by 0x1E010BF: selftest::run_tests() (selftest-run-tests.c:88) 4,004 bytes in 28 blocks are definitely lost in loss record 633 of 669 at 0x4A0645D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x1F08227: xmalloc (xmalloc.c:147) by 0x1F083E6: xvasprintf (xvasprintf.c:58) by 0x1E7EC7D: build_message_string(char const*, ...) (diagnostic.c:78) by 0x1E7F438: diagnostic_get_location_text(diagnostic_context*, expanded_location) (diagnostic.c:328) by 0x1E7FD54: default_diagnostic_start_span_fn(diagnostic_context*, expanded_location) (diagnostic.c:626) by 0x1EB3508: selftest::test_diagnostic_context::start_span_cb(diagnostic_context*, expanded_location) (selftest-diagnostic.c:57) by 0x1E89215: diagnostic_show_locus(diagnostic_context*, rich_location*, diagnostic_t) (diagnostic-show-locus.c:1992) by 0x1E8B373: selftest::test_diagnostic_show_locus_fixit_lines(selftest::line_table_case const&) (diagnostic-show-locus.c:2500) by 0x1EB0606: selftest::for_each_line_table_case(void (*)(selftest::line_table_case const&)) (input.c:3525) by 0x1E8F3B9: selftest::diagnostic_show_locus_c_tests() (diagnostic-show-locus.c:3159) by 0x1E010BF: selftest::run_tests() (selftest-run-tests.c:88) This patch fixes the leaks by ensuring that the pretty_printer "owns" the prefix if it's non-NULL, freeing it in the dtor and in pp_set_prefix. gcc/cp/ChangeLog: * error.c (cxx_print_error_function): Duplicate "file" before passing it to pp_set_prefix. (cp_print_error_function): Use pp_take_prefix when saving the existing prefix. gcc/ChangeLog: * diagnostic-show-locus.c (diagnostic_show_locus): Use pp_take_prefix when saving the existing prefix. * diagnostic.c (diagnostic_append_note): Likewise. * langhooks.c (lhd_print_error_function): Likewise. * pretty-print.c (pp_set_prefix): Drop the "const" from "prefix" param's type. Free the existing prefix. (pp_take_prefix): New function. (pretty_printer::pretty_printer): Drop the prefix parameter. Rename the length parameter to match the comment. (pretty_printer::~pretty_printer): Free the prefix. * pretty-print.h (pretty_printer::pretty_printer): Drop the prefix parameter. (struct pretty_printer): Drop the "const" from "prefix" field's type and clarify memory management. (pp_set_prefix): Drop the "const" from the 2nd param. (pp_take_prefix): New decl. From-SVN: r263275
2018-02-27PR translation/84207 - Hard coded plural in gimple-fold.cMartin Sebor
gcc/ChangeLog: PR translation/84207 * diagnostic-core.h (warning_n, error_n, inform_n): Change n argument to unsigned HOST_WIDE_INT. * diagnostic.c (warning_n, error_n, inform_n): Ditto. (diagnostic_n_impl): Ditto. Handle arguments in excess of LONG_MAX. * gimple-ssa-sprintf.c (format_directive): Simplify inform_n call. * tree-ssa-strlen.c (maybe_diag_stxncpy_trunc): Use warning_n. From-SVN: r258044
2018-01-03Update copyright years.Jakub Jelinek
From-SVN: r256169
2017-11-13[Diagnostic Patch] don't print column zeroNathan Sidwell
https://gcc.gnu.org/ml/gcc-patches/2017-10/msg01911.html * diagnostic.c (maybe_line_and_column): New. (diagnostic_get_location_text): Use it. (diagnostic_report_current_module): Likewise. (test_diagnostic_get_location_text): Add tests. * lib/gcc-dg.exp (process-message): Use -: for no column. * c-c++-common/cilk-plus/CK/cilk_for_grain_errors.c: Mark elided column messages. * c-c++-common/cpp/pr58844-1.c: Likewise. * c-c++-common/cpp/pr58844-2.c: Likewise. * c-c++-common/cpp/warning-zero-location.c: Likewise. * g++.dg/diagnostic/pr77949.C: Likewise. * g++.dg/gomp/macro-4.C: Likewise. * gcc.dg/Wunknownprag.c: Likewise. * gcc.dg/builtin-redefine.c: Likewise. * gcc.dg/cpp/Wunknown-pragmas-1.c: Likewise. * gcc.dg/cpp/Wunused.c: Likewise. * gcc.dg/cpp/misspelled-directive-1.c: Likewise. * gcc.dg/cpp/redef2.c: Likewise. * gcc.dg/cpp/redef3.c: Likewise. * gcc.dg/cpp/redef4.c: Likewise. * gcc.dg/cpp/trad/Wunused.c: Likewise. * gcc.dg/cpp/trad/argcount.c: Likewise. * gcc.dg/cpp/trad/comment-3.c: Likewise. * gcc.dg/cpp/trad/comment.c: Likewise. * gcc.dg/cpp/trad/defined.c: Likewise. * gcc.dg/cpp/trad/directive.c: Likewise. * gcc.dg/cpp/trad/funlike-3.c: Likewise. * gcc.dg/cpp/trad/funlike.c: Likewise. * gcc.dg/cpp/trad/literals-2.c: Likewise. * gcc.dg/cpp/trad/macro.c: Likewise. * gcc.dg/cpp/trad/pr65238-4.c: Likewise. * gcc.dg/cpp/trad/recurse-1.c: Likewise. * gcc.dg/cpp/trad/recurse-2.c: Likewise. * gcc.dg/cpp/trad/redef2.c: Likewise. * gcc.dg/cpp/ucnid-11.c: Likewise. * gcc.dg/cpp/unc1.c: Likewise. * gcc.dg/cpp/unc2.c: Likewise. * gcc.dg/cpp/unc3.c: Likewise. * gcc.dg/cpp/unc4.c: Likewise. * gcc.dg/cpp/undef2.c: Likewise. * gcc.dg/cpp/warn-redefined-2.c: Likewise. * gcc.dg/cpp/warn-redefined.c: Likewise. * gcc.dg/cpp/warn-unused-macros-2.c: Likewise. * gcc.dg/cpp/warn-unused-macros.c: Likewise. * gcc.dg/empty-source-2.c: Likewise. * gcc.dg/empty-source-3.c: Likewise. * gcc.dg/gomp/macro-4.c: Likewise. * gcc.dg/noncompile/pr35447-1.c: Likewise. * gcc.dg/plugin/location-overflow-test-1.c: Likewise. * gcc.dg/pr20245-1.c: Likewise. * gcc.dg/pr28419.c: Likewise. * gcc.dg/rtl/truncated-rtl-file.c: Likewise. * gcc.dg/unclosed-init.c: Likewise. From-SVN: r254691
2017-11-02Add selftest for diagnostic_get_location_textDavid Malcolm
gcc/ChangeLog: * diagnostic.c: Include "selftest-diagnostic.h". (selftest::assert_location_text): New function. (selftest::test_diagnostic_get_location_text): New function. (selftest::diagnostic_c_tests): Call it. From-SVN: r254355
2017-10-31diagnostics: get rid of *_at_rich_loc in favor of overloadingDavid Malcolm
Adding a fix-it hint currently involves changing e.g.: error_at (token->location, "unknown type name %qE; did you mean %qs?", token->value, hint); to: gcc_rich_location richloc (token->location); richloc.add_fixit_replace (hint); error_at_rich_loc (&richloc, "unknown type name %qE; did you mean %qs?", token->value, hint); to make the change from taking a location_t to a rich_location *. This patch renames the "*_at_rich_loc" diagnostic entrypoints to use the same function names for rich_location * as for location_t, via overloading, to simplify the above change to just changing from: error_at (token->location, "unknown type name %qE; did you mean %qs?", token->value, hint); to: gcc_rich_location richloc (token->location); richloc.add_fixit_replace (hint); error_at (&richloc, "unknown type name %qE; did you mean %qs?", token->value, hint); thus saving space (and typing) and usually avoiding the need to reindent the "error_at" invocation. With this change, 0 is no longer acceptable as a location_t to these entrypoints, as e.g.: ../../src/gcc/auto-profile.c:855:37: error: call of overloaded 'inform(int, const char [18])' is ambiguous inform (0, "Not expected TAG."); ^ In file included from ../../src/gcc/auto-profile.c:35:0: ../../src/gcc/diagnostic-core.h:88:13: note: candidate: 'void inform(location_t, const char*, ...)' extern void inform (location_t, const char *, ...) ATTRIBUTE_GCC_DIAG(2,3); ^~~~~~ ../../src/gcc/diagnostic-core.h:89:13: note: candidate: 'void inform(rich_location*, const char*, ...)' extern void inform (rich_location *, const char *, ...) ATTRIBUTE_GCC_DIAG(2,3); ^~~~~~ Such locations now need to be spelled out as UNKNOWN_LOCATION, rather than 0. I considered making the API take a rich_location & rather than a rich_location *, but doing so would mean replacing diagnostic_set_info and diagnostic_set_info_translated with a constructor for diagnostic_info, which was a more invasive change. Maybe in the future. gcc/ChangeLog: * auto-profile.c (autofdo_source_profile::read): Use UNKNOWN_LOCATION rather than 0. * diagnostic-core.h (warning_at_rich_loc): Rename to... (warning_at): ...this overload. (warning_at_rich_loc_n): Rename to... (warning_n): ...this overload. (error_at_rich_loc): Rename to... (error_at): ...this overload. (pedwarn_at_rich_loc): Rename to... (pedwarn): ...this overload. (permerror_at_rich_loc): Rename to... (permerror): ...this overload. (inform_at_rich_loc): Rename to... (inform): ...this overload. * diagnostic.c: (diagnostic_n_impl): Delete location_t-based decl. (diagnostic_n_impl_richloc): Rename to... (diagnostic_n_impl): ...this rich_location *-based decl. (inform_at_rich_loc): Rename to... (inform): ...this, and add an assertion. (inform_n): Update for removal of location_t-based diagnostic_n_impl. (warning_at_rich_loc): Rename to... (warning_at): ...this, and add an assertion. (warning_at_rich_loc_n): Rename to... (warning_n): ...this, and add an assertion. (warning_n): Update location_t-based implementation for removal of location_t-based diagnostic_n_impl. (pedwarn_at_rich_loc): Rename to... (pedwarn): ...this, and add an assertion. (permerror_at_rich_loc): Rename to... (permerror): ...this, and add an assertion. (error_n): Update for removal of location_t-based diagnostic_n_impl. (error_at_rich_loc): Rename to... (error_at): ...this, and add an assertion. * gcc.c (do_spec_1): Use UNKNOWN_LOCATION rather than 0. (driver::do_spec_on_infiles): Likewise. * substring-locations.c (format_warning_va): Update for renaming of inform_at_rich_loc. gcc/c-family/ChangeLog: * c-common.c (binary_op_error): Update for renaming of error_at_rich_loc. (c_parse_error): Likewise. * c-warn.c (warn_logical_not_parentheses): Likewise for renaming of inform_at_rich_loc. (warn_for_restrict): Likewise for renaming of warning_at_rich_loc_n. gcc/c/ChangeLog: * c-decl.c (implicit_decl_warning): Update for renaming of pedwarn_at_rich_loc and warning_at_rich_loc. (implicitly_declare): Likewise for renaming of inform_at_rich_loc. (undeclared_variable): Likewise for renaming of error_at_rich_loc. * c-parser.c (c_parser_declaration_or_fndef): Likewise. (c_parser_struct_or_union_specifier): Likewise for renaming of pedwarn_at_rich_loc. (c_parser_parameter_declaration): Likewise for renaming of error_at_rich_loc. * c-typeck.c (build_component_ref): Likewise. (build_unary_op): Likewise for renaming of inform_at_rich_loc. (pop_init_level): Likewise for renaming of warning_at_rich_loc. (set_init_label): Likewise for renaming of error_at_rich_loc. gcc/cp/ChangeLog: * class.c (explain_non_literal_class): Use UNKNOWN_LOCATION rather than 0. * name-lookup.c (suggest_alternatives_for): Update for renaming of inform_at_rich_loc. (maybe_suggest_missing_header): Likewise. (suggest_alternative_in_explicit_scope): Likewise. * parser.c (cp_parser_diagnose_invalid_type_name): Likewise for renaming of error_at_rich_loc. (cp_parser_string_literal): Likewise. (cp_parser_nested_name_specifier_opt): Likewise. (cp_parser_cast_expression): Likewise for renaming of warning_at_rich_loc. (cp_parser_decl_specifier_seq): Likewise for renaming of error_at_rich_loc and warning_at_rich_loc. (cp_parser_elaborated_type_specifier): Likewise for renaming of pedwarn_at_rich_loc. (cp_parser_cv_qualifier_seq_opt): Likewise for renaming of error_at_rich_loc. (cp_parser_virt_specifier_seq_opt): Likewise. (cp_parser_class_specifier_1): Likewise. (cp_parser_class_head): Likewise. (cp_parser_member_declaration): Likewise for renaming of pedwarn_at_rich_loc, warning_at_rich_loc, and error_at_rich_loc. (cp_parser_enclosed_template_argument_list): Likewise for renaming of error_at_rich_loc. (set_and_check_decl_spec_loc): Likewise. * pt.c (listify): Likewise. * rtti.c (typeid_ok_p): Likewise. * semantics.c (process_outer_var_ref): Use UNKNOWN_LOCATION rather than 0. * typeck.c (access_failure_info::maybe_suggest_accessor): Update for renaming of inform_at_rich_loc. (finish_class_member_access_expr): Likewise for renaming of error_at_rich_loc. gcc/objc/ChangeLog: * objc-gnu-runtime-abi-01.c (objc_gnu_runtime_abi_01_init): Use UNKNOWN_LOCATION rather than 0. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_show_trees.c (show_tree): Update for renaming of error_at_rich_loc and inform_at_rich_loc. * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (test_show_locus): Likewise for renaming of warning_at_rich_loc. libcpp/ChangeLog: * directives.c (_cpp_handle_directive): Update for renaming of cpp_error_at_richloc to cpp_error_at. * errors.c (cpp_diagnostic_at_richloc): Rename to... (cpp_diagnostic_at): ...this, dropping the location_t-based implementation. (cpp_diagnostic): Update for removal of location_t-based cpp_diagnostic_at. (cpp_error_at): Likewise. (cpp_error_at_richloc): Rename to... (cpp_error_at): ...this, and update for renaming of cpp_diagnostic_at_richloc. * include/cpplib.h (cpp_error_at_richloc): Rename to... (cpp_error_at): ...this. From-SVN: r254280
2017-08-09re PR c/81233 (--Wdiscarded-qualifiers and Wincompatible-pointer-types ↵Marek Polacek
missing important detail) PR c/81233 * c-typeck.c (pedwarn_init): Make the function take a variable list. Call emit_diagnostic_valist instead of pedwarn. (convert_for_assignment): Unroll the PEDWARN_FOR_ASSIGNMENT macro. Print the relevant types in diagnostics. * diagnostic-core.h (emit_diagnostic_valist): Add declaration. * diagnostic.c (emit_diagnostic): Add a comment. (emit_diagnostic_valist): New function. * gcc.dg/diagnostic-types-1.c: New test. * gcc.dg/assign-warn-1.c: Update warning messages. * gcc.dg/assign-warn-2.c: Likewise. * gcc.dg/c90-const-expr-5.c: Likewise. * gcc.dg/c99-const-expr-5.c: Likewise. * gcc.dg/conv-2.c: Likewise. * gcc.dg/init-bad-7.c: Likewise. * gcc.dg/overflow-warn-1.c: Likewise. * gcc.dg/overflow-warn-2.c: Likewise. * gcc.dg/overflow-warn-3.c: Likewise. * gcc.dg/overflow-warn-4.c: Likewise. * gcc.dg/pointer-array-atomic.c: Likewise. * gcc.dg/pr26865.c: Likewise. * gcc.dg/pr61162-2.c: Likewise. * gcc.dg/pr61162.c: Likewise. * gcc.dg/pr67730-2.c: Likewise. * gcc.dg/pr69156.c: Likewise. * gcc.dg/pr70174.c: Likewise. * objc.dg/proto-lossage-4.m: Likewise. From-SVN: r250985
2017-06-09Add support for mutually-incompatible fix-it hintsDavid Malcolm
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-05-05Get rid of macros for diagnostic_report_current_moduleDavid Malcolm
diagnostic.h has a couple of macros (diagnostic_last_module_changed and diagnostic_set_last_module) which are only used within diagnostic_report_current_module. This patch eliminates the macros in favor of static functions within diagnostic.c. No functional change intended. gcc/ChangeLog: * diagnostic.c (last_module_changed_p): New function. (set_last_module): New function. (diagnostic_report_current_module): Convert macro usage to the above functions. * diagnostic.h (diagnostic_context::last_module): Strengthen from const line_map * to const line_map_ordinary *. (diagnostic_last_module_changed): Delete macro. (diagnostic_set_last_module): Delete macro. From-SVN: r247664
2017-05-05Eliminate report_diagnostic macroDavid Malcolm
This patch eliminates the report_diagnostic macro, manually expanding it in all sites in the code. No functional change intended. gcc/c-family/ChangeLog: * c-common.c (c_cpp_error): Replace report_diagnostic with diagnostic_report_diagnostic. gcc/c/ChangeLog: * c-decl.c (warn_defaults_to): Replace report_diagnostic with diagnostic_report_diagnostic. * c-errors.c (pedwarn_c99): Likewise. (pedwarn_c90): Likewise. gcc/cp/ChangeLog: * error.c (pedwarn_cxx98): Replace report_diagnostic with diagnostic_report_diagnostic. gcc/ChangeLog: * diagnostic.c (diagnostic_impl): Replace report_diagnostic with diagnostic_report_diagnostic. (diagnostic_n_impl_richloc): Likewise. * diagnostic.h (report_diagnostic): Delete macro. * rtl-error.c (diagnostic_for_asm): Replace report_diagnostic with diagnostic_report_diagnostic. * substring-locations.c (format_warning_va): Likewise. gcc/fortran/ChangeLog: * cpp.c (cb_cpp_error): Replace report_diagnostic with diagnostic_report_diagnostic. * error.c (gfc_warning): Likewise. (gfc_warning_now_at): Likewise. (gfc_warning_now): Likewise. (gfc_warning_internal): Likewise. (gfc_error_now): Likewise. (gfc_fatal_error): Likewise. (gfc_error_opt): Likewise. (gfc_internal_error): Likewise. From-SVN: r247663
2017-05-05diagnostic.c: add print_option_informationDavid Malcolm
This patch simplifies diagnostic_report_diagnostic by moving option-printing to a new subroutine. Doing so required a slight rewrite. In both the old and new code, context->option_name returns a malloc-ed string. The old behavior was to then use ACONCAT to manipulate the format_spec, appending the option metadata. ACONCAT calcs the buffer size, then uses alloca, and then copies the data to the on-stack buffer. Given the alloca, this needs rewriting when moving the printing to a subroutine. In the new version, the metadata is simply printed using pp_* calls (so it's hitting the obstack within the pretty_printer). This means we can get rid of the save/restore of format_spec: I don't believe anything else in the code modifies it. It also seems inherently simpler; it seems odd to me to be appending metadata to the formatting string, rather than simply printing the metadata after the formatted string is printed (the old code also assumed that no option name contained a '%'). No functional change intended. gcc/ChangeLog: * diagnostic.c (diagnostic_report_diagnostic): Eliminate save/restor of format_spec. Move option-printing code to... (print_option_information): ...this new function, and reimplement by simply printing to the pretty_printer, rather than appending to the format string. From-SVN: r247661
2017-05-05diagnostic_report_diagnostic: refactor pragma-handlingDavid Malcolm
This patch simplifies diagnostic_report_diagnostic by moving the pragma-handling logic into a subroutine. No functional change intended. gcc/ChangeLog: * diagnostic.c (diagnostic_report_diagnostic): Split out pragma handling logic into... (update_effective_level_from_pragmas): ...this new function. From-SVN: r247660
2017-05-01Eliminate fixit_hint class hierarchyDavid Malcolm
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-01-01Update copyright years.Jakub Jelinek
From-SVN: r243994
2016-12-05diagnostic.c (diagnostic_check_max_errors): New, broken out of ...Nathan Sidwell
gcc/ * diagnostic.c (diagnostic_check_max_errors): New, broken out of ... (diagnostic_action_after_output): ... here. (diagnostic_report_diagnostic): Call it for non-notes. * diagnostic.h (struct diagnostic_context): Make max_errors signed int. (diagnostic_check_max_errors): Declare. gcc/fortran/ * error.c (gfc_warning_check): Call diagnostic_check_max_errors. (gfc_error_check): Likewise. gcc/testsuite/ * c-c++-common/fmax_errors.c: Check notes after last error are emitted. From-SVN: r243254
2016-12-02diagnostic.c (diagnostic_report_diagnostic): Remove extraneous braces.Nathan Sidwell
* diagnostic.c (diagnostic_report_diagnostic): Remove extraneous braces. From-SVN: r243177
2016-10-15revert: diagnostic.c (diagnostic_action_after_output): Remove max error ↵Nathan Sidwell
handling here .... Revert * diagnostic.c (diagnostic_action_after_output): Remove max error handling here .... (diagnostic_report_diagnostic): ... do it here instead. testsuite/ * c-c++-common/fmax-errors.c: Make sure note is emitted. From-SVN: r241198
2016-10-14diagnostic.c (diagnostic_action_after_output): Remove max error handling ↵Nathan Sidwell
here .... * diagnostic.c (diagnostic_action_after_output): Remove max error handling here .... (diagnostic_report_diagnostic): ... do it here instead. testsuite/ * c-c++-common/fmax-errors.c: Make sure note is emitted. From-SVN: r241186
2016-10-12* diagnostic.c (diagnostc_report_diagnostic): Fix formatting.Nathan Sidwell
From-SVN: r241027
2016-10-08diagnostic-core.h (warning_at_rich_loc_n): Declare.Prathamesh Kulkarni
2016-10-08 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org> * diagnostic-core.h (warning_at_rich_loc_n): Declare. * diagnostic.c (warning_at_rich_loc_n): New function. (diagnostic_n_impl_richloc): Likewise. (diagnostic_n_impl): Move most of the function to diagnostic_n_impl_richloc and call it. From-SVN: r240891
2016-09-13fix-it hints: insert_before vs insert_afterDavid Malcolm
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-02Add -fdiagnostics-generate-patchDavid Malcolm
gcc/ChangeLog: * common.opt (fdiagnostics-generate-patch): New option. * diagnostic.c: Include "edit-context.h". (diagnostic_initialize): Initialize context->edit_context_ptr. (diagnostic_finish): Delete context->edit_context_ptr. (diagnostic_report_diagnostic): Add fix-it hints from the diagnostic to context->edit_context_ptr, if any. * diagnostic.h (class edit_context): Add forward decl. (struct diagnostic_context): Add field "edit_context_ptr". * doc/invoke.texi (Diagnostic Message Formatting Options): Add -fdiagnostics-generate-patch. (-fdiagnostics-generate-patch): New item. * toplev.c: Include "edit-context.h". (process_options): Set global_dc->edit_context_ptr to a new edit_context if the options need one. (toplev::main): Handle -fdiagnostics-generate-patch by using global_dc->edit_context_ptr. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic-test-show-locus-generate-patch.c: New test case. * gcc.dg/plugin/plugin.exp (plugin_test_list): Add diagnostic-test-show-locus-generate-patch.c to the sources for diagnostic_plugin_test_show_locus.c. From-SVN: r239965
2016-08-31Remove arbitrary limits from rich_locationDavid Malcolm
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-19Reimplement removal fix-it hints in terms of replaceDavid Malcolm
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-18Allow calling diagnostic_show_locus without a diagnostic_infoDavid Malcolm
Much of diagnostic-show-locus.c currently expects a diagnostic_info *, but it only uses the rich_location and the diagnostic_t. Change the signature of diagnostic_show_locus from: void diagnostic_show_locus (diagnostic_context *, const diagnostic_info *); to: void diagnostic_show_locus (diagnostic_context *, rich_location *richloc, diagnostic_t diagnostic_kind); so that it can be used for things other than diagnostics. Use this flexibility to add selftests for diagnostic_show_locus. gcc/c-family/ChangeLog: * c-opts.c (c_diagnostic_finalizer): Update for change to diagnostic_show_locus. gcc/ChangeLog: * diagnostic-show-locus.c (colorizer::colorizer): Replace diagnostic param with diagnostic_kind. (class colorizer): Similarly replace field m_diagnostic with m_diagnostic_kind. (colorizer::colorizer): Replace diagnostic param with diagnostic_kind. (colorizer::begin_state): Update for above field change. (layout::layout): Replace diagnostic param with rich_location * and diagnostic_kind. (diagnostic_show_locus): Replace diagnostic param with richloc and diagnostic_kind. (class selftest::test_diagnostic_context): New class. (selftest::test_diagnostic_show_locus_unknown_location): New function. (selftest::test_one_liner_simple_caret): New function. (selftest::test_one_liner_caret_and_range): New function. (selftest::test_one_liner_multiple_carets_and_ranges): New function. (selftest::test_one_liner_fixit_remove): New function. (selftest::test_one_liner_fixit_replace): New function. (selftest::test_diagnostic_show_locus_one_liner): New function. (selftest::diagnostic_show_locus_c_tests): Call the new test functions. * diagnostic.c (diagnostic_initialize): Initialize colorize_source_p, show_ruler_p and parseable_fixits_p. (default_diagnostic_finalizer): Update for change to diagnostic_show_locus. (diagnostic_append_note): Likewise. * diagnostic.h (diagnostic_show_locus): Replace const diagnostic_info * param with location * and diagnostic_t. gcc/fortran/ChangeLog: * error.c (gfc_diagnostic_starter): Update for change to diagnostic_show_locus. gcc/testsuite/ChangeLog: * gcc.dg/plugin/diagnostic_plugin_test_show_locus.c (custom_diagnostic_finalizer): Update for change to diagnostic_show_locus. From-SVN: r239586