Skip to content
Snippets Groups Projects
Commit c397a8c1 authored by Jakub Jelinek's avatar Jakub Jelinek Committed by Jakub Jelinek
Browse files

libcpp, genmatch: Use gcc_diag instead of printf for libcpp diagnostics

When working on #embed support, or -Wheader-guard or other recent libcpp
changes, I've been annoyed by the libcpp diagnostics being visually
different from normal gcc diagnostics, especially in the area of quoting
stuff in the diagnostic messages.
Normall GCC diagnostics is gcc_diag/gcc_tdiag, one can use
%</%>, %qs etc. in there, while libcpp diagnostics was marked as printf
and in libcpp we've been very creative with quoting stuff, either
no quotes at all, or "something" quoting, or 'something' quoting, or
`something' quoting (but in none of the cases it used colors consistently
with the rest of the compiler).

Now, libcpp diagnostics is always emitted using a callback,
pfile->cb.diagnostic.  On the gcc/ side, this callback is initialized with
genmatch.cc:  cb->diagnostic = diagnostic_cb;
c-family/c-opts.cc:  cb->diagnostic = c_cpp_diagnostic;
fortran/cpp.cc:  cb->diagnostic = cb_cpp_diagnostic;
where the latter two just use diagnostic_report_diagnostic, so actually
support all the gcc_diag stuff, only the genmatch.cc case didn't.

So, the following patch changes genmatch.cc to use pp_format* instead
of vfprintf so that it supports the gcc_diag formatting (pretty-print.o
unfortunately has various dependencies, so had to link genmatch with
libcommon.a libbacktrace.a and tweak Makefile.in so that there are no
circular dependencies) and marks the libcpp diagnostic routines as
gcc_diag rather than printf.  That change resulted in hundreds of
-Wformat-diag new warnings (most of them useful and resulting IMHO in
better diagnostics), so the rest of the patch is changing the format
strings to make -Wformat-diag happy and adjusting the testsuite for
the differences in how is the diagnostic reformatted.

Dunno if some out of GCC tree projects use libcpp, that case would
make it harder because one couldn't use vfprintf in the diagnostic
callback anymore, but there is always David's libdiagnostic which could
be used for that purpose IMHO.

2024-10-12  Jakub Jelinek  <jakub@redhat.com>

libcpp/
	* include/cpplib.h (ATTRIBUTE_CPP_PPDIAG): Define.
	(struct cpp_callbacks): Use ATTRIBUTE_CPP_PPDIAG instead of
	ATTRIBUTE_FPTR_PRINTF on diagnostic callback.
	(cpp_error, cpp_warning, cpp_pedwarning, cpp_warning_syshdr): Use
	ATTRIBUTE_CPP_PPDIAG (3, 4) instead of ATTRIBUTE_PRINTF_3.
	(cpp_warning_at, cpp_pedwarning_at): Use ATTRIBUTE_CPP_PPDIAG (4, 5)
	instead of ATTRIBUTE_PRINTF_4.
	(cpp_error_with_line, cpp_warning_with_line, cpp_pedwarning_with_line,
	cpp_warning_with_line_syshdr): Use ATTRIBUTE_CPP_PPDIAG (5, 6)
	instead of ATTRIBUTE_PRINTF_5.
	(cpp_error_at): Use ATTRIBUTE_CPP_PPDIAG (4, 5) instead of
	ATTRIBUTE_PRINTF_4.
	* Makefile.in (po/$(PACKAGE).pot): Use --language=GCC-source rather
	than --language=c.
	* errors.cc (cpp_diagnostic_at, cpp_diagnostic,
	cpp_diagnostic_with_line): Use ATTRIBUTE_CPP_PPDIAG instead of
	-ATTRIBUTE_FPTR_PRINTF.
	* charset.cc (cpp_host_to_exec_charset, _cpp_valid_ucn, convert_hex,
	convert_oct, convert_escape): Fix up -Wformat-diag warnings.
	(cpp_interpret_string_ranges, count_source_chars): Use
	ATTRIBUTE_CPP_PPDIAG instead of ATTRIBUTE_FPTR_PRINTF.
	(narrow_str_to_charconst): Fix up -Wformat-diag warnings.
	* directives.cc (check_eol_1, directive_diagnostics, lex_macro_node,
	do_undef, glue_header_name, parse_include, do_include_common,
	do_include_next, _cpp_parse_embed_params, do_embed, read_flag,
	do_line, do_linemarker, register_pragma_1, do_pragma_once,
	do_pragma_push_macro, do_pragma_pop_macro, do_pragma_poison,
	do_pragma_system_header, do_pragma_warning_or_error, _cpp_do__Pragma,
	do_else, do_elif, do_endif, parse_answer, do_assert,
	cpp_define_unused): Likewise.
	* expr.cc (cpp_classify_number, parse_defined, eval_token,
	_cpp_parse_expr, reduce, check_promotion): Likewise.
	* files.cc (_cpp_find_file, finish_base64_embed,
	_cpp_pop_file_buffer): Likewise.
	* init.cc (sanity_checks): Likewise.
	* lex.cc (_cpp_process_line_notes, maybe_warn_bidi_on_char,
	_cpp_warn_invalid_utf8, _cpp_skip_block_comment,
	warn_about_normalization, forms_identifier_p, maybe_va_opt_error,
	identifier_diagnostics_on_lex, cpp_maybe_module_directive): Likewise.
	* macro.cc (class vaopt_state, builtin_has_include_1,
	builtin_has_include, builtin_has_embed, _cpp_warn_if_unused_macro,
	_cpp_builtin_macro_text, builtin_macro, stringify_arg,
	_cpp_arguments_ok, collect_args, enter_macro_context,
	_cpp_save_parameter, parse_params, create_iso_definition,
	_cpp_create_definition, check_trad_stringification): Likewise.
	* pch.cc (cpp_valid_state): Likewise.
	* traditional.cc (_cpp_scan_out_logical_line, recursive_macro):
	Likewise.
gcc/
	* Makefile.in (generated_files): Remove {gimple,generic}-match*.
	(generated_match_files): New variable.  Add a dependency of
	$(filter-out $(OBJS-libcommon),$(ALL_HOST_OBJS)) files on those.
	(build/genmatch$(build_exeext)): Depend on and link against
	libcommon.a and $(LIBBACKTRACE).
	* genmatch.cc: Include pretty-print.h and input.h.
	(ggc_internal_cleared_alloc, ggc_free): Remove.
	(fatal): New function.
	(line_table): Remove.
	(linemap_client_expand_location_to_spelling_point): Remove.
	(diagnostic_cb): Use gcc_diag rather than printf format.  Use
	pp_format_verbatim on a temporary pretty_printer instead of
	vfprintf.
	(fatal_at, warning_at): Use gcc_diag rather than printf format.
	(output_line_directive): Rename location_hash to loc_hash.
	(parser::eat_ident, parser::parse_operation, parser::parse_expr,
	parser::parse_pattern, parser::finish_match_operand): Fix up
	-Wformat-diag warnings.
gcc/c-family/
	* c-lex.cc (c_common_has_attribute,
	c_common_lex_availability_macro): Fix up -Wformat-diag warnings.
gcc/testsuite/
	* c-c++-common/cpp/counter-2.c: Adjust expected diagnostics for
	libcpp diagnostic formatting changes.
	* c-c++-common/cpp/embed-3.c: Likewise.
	* c-c++-common/cpp/embed-4.c: Likewise.
	* c-c++-common/cpp/embed-16.c: Likewise.
	* c-c++-common/cpp/embed-18.c: Likewise.
	* c-c++-common/cpp/eof-2.c: Likewise.
	* c-c++-common/cpp/eof-3.c: Likewise.
	* c-c++-common/cpp/fmax-include-depth.c: Likewise.
	* c-c++-common/cpp/has-builtin.c: Likewise.
	* c-c++-common/cpp/line-2.c: Likewise.
	* c-c++-common/cpp/line-3.c: Likewise.
	* c-c++-common/cpp/macro-arg-count-1.c: Likewise.
	* c-c++-common/cpp/macro-arg-count-2.c: Likewise.
	* c-c++-common/cpp/macro-ranges.c: Likewise.
	* c-c++-common/cpp/named-universal-char-escape-4.c: Likewise.
	* c-c++-common/cpp/named-universal-char-escape-5.c: Likewise.
	* c-c++-common/cpp/pr88974.c: Likewise.
	* c-c++-common/cpp/va-opt-error.c: Likewise.
	* c-c++-common/cpp/va-opt-pedantic.c: Likewise.
	* c-c++-common/cpp/Wheader-guard-2.c: Likewise.
	* c-c++-common/cpp/Wheader-guard-3.c: Likewise.
	* c-c++-common/cpp/Winvalid-utf8-1.c: Likewise.
	* c-c++-common/cpp/Winvalid-utf8-2.c: Likewise.
	* c-c++-common/cpp/Winvalid-utf8-3.c: Likewise.
	* c-c++-common/diagnostic-format-sarif-file-bad-utf8-pr109098-1.c:
	Likewise.
	* c-c++-common/diagnostic-format-sarif-file-bad-utf8-pr109098-3.c:
	Likewise.
	* c-c++-common/pr68833-3.c: Likewise.
	* c-c++-common/raw-string-directive-1.c: Likewise.
	* gcc.dg/analyzer/named-constants-Wunused-macros.c: Likewise.
	* gcc.dg/binary-constants-4.c: Likewise.
	* gcc.dg/builtin-redefine.c: Likewise.
	* gcc.dg/cpp/19951025-1.c: Likewise.
	* gcc.dg/cpp/c11-warning-1.c: Likewise.
	* gcc.dg/cpp/c11-warning-2.c: Likewise.
	* gcc.dg/cpp/c11-warning-3.c: Likewise.
	* gcc.dg/cpp/c23-elifdef-2.c: Likewise.
	* gcc.dg/cpp/c23-warning-2.c: Likewise.
	* gcc.dg/cpp/embed-2.c: Likewise.
	* gcc.dg/cpp/embed-3.c: Likewise.
	* gcc.dg/cpp/embed-4.c: Likewise.
	* gcc.dg/cpp/expr.c: Likewise.
	* gcc.dg/cpp/gnu11-elifdef-2.c: Likewise.
	* gcc.dg/cpp/gnu11-elifdef-3.c: Likewise.
	* gcc.dg/cpp/gnu11-elifdef-4.c: Likewise.
	* gcc.dg/cpp/gnu11-warning-1.c: Likewise.
	* gcc.dg/cpp/gnu11-warning-2.c: Likewise.
	* gcc.dg/cpp/gnu11-warning-3.c: Likewise.
	* gcc.dg/cpp/gnu23-warning-2.c: Likewise.
	* gcc.dg/cpp/include6.c: Likewise.
	* gcc.dg/cpp/pr35322.c: Likewise.
	* gcc.dg/cpp/tr-warn6.c: Likewise.
	* gcc.dg/cpp/undef2.c: Likewise.
	* gcc.dg/cpp/warn-comments.c: Likewise.
	* gcc.dg/cpp/warn-comments-2.c: Likewise.
	* gcc.dg/cpp/warn-comments-3.c: Likewise.
	* gcc.dg/cpp/warn-cxx-compat.c: Likewise.
	* gcc.dg/cpp/warn-cxx-compat-2.c: Likewise.
	* gcc.dg/cpp/warn-deprecated.c: Likewise.
	* gcc.dg/cpp/warn-deprecated-2.c: Likewise.
	* gcc.dg/cpp/warn-long-long.c: Likewise.
	* gcc.dg/cpp/warn-long-long-2.c: Likewise.
	* gcc.dg/cpp/warn-normalized-1.c: Likewise.
	* gcc.dg/cpp/warn-normalized-2.c: Likewise.
	* gcc.dg/cpp/warn-normalized-3.c: Likewise.
	* gcc.dg/cpp/warn-normalized-4-bytes.c: Likewise.
	* gcc.dg/cpp/warn-normalized-4-unicode.c: Likewise.
	* gcc.dg/cpp/warn-redefined.c: Likewise.
	* gcc.dg/cpp/warn-redefined-2.c: Likewise.
	* gcc.dg/cpp/warn-traditional.c: Likewise.
	* gcc.dg/cpp/warn-traditional-2.c: Likewise.
	* gcc.dg/cpp/warn-trigraphs-1.c: Likewise.
	* gcc.dg/cpp/warn-trigraphs-2.c: Likewise.
	* gcc.dg/cpp/warn-trigraphs-3.c: Likewise.
	* gcc.dg/cpp/warn-trigraphs-4.c: Likewise.
	* gcc.dg/cpp/warn-undef.c: Likewise.
	* gcc.dg/cpp/warn-undef-2.c: Likewise.
	* gcc.dg/cpp/warn-unused-macros.c: Likewise.
	* gcc.dg/cpp/warn-unused-macros-2.c: Likewise.
	* gcc.dg/pch/counter-2.c: Likewise.
	* g++.dg/cpp0x/udlit-error1.C: Likewise.
	* g++.dg/cpp23/named-universal-char-escape1.C: Likewise.
	* g++.dg/cpp23/named-universal-char-escape2.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-1.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-2.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-3.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-4.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-5.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-6.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-7.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-8.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-9.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-10.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-11.C: Likewise.
	* g++.dg/cpp23/Winvalid-utf8-12.C: Likewise.
	* g++.dg/cpp/elifdef-3.C: Likewise.
	* g++.dg/cpp/elifdef-5.C: Likewise.
	* g++.dg/cpp/elifdef-6.C: Likewise.
	* g++.dg/cpp/elifdef-7.C: Likewise.
	* g++.dg/cpp/embed-1.C: Likewise.
	* g++.dg/cpp/embed-2.C: Likewise.
	* g++.dg/cpp/pedantic-errors.C: Likewise.
	* g++.dg/cpp/warning-1.C: Likewise.
	* g++.dg/cpp/warning-2.C: Likewise.
	* g++.dg/ext/bitint1.C: Likewise.
	* g++.dg/ext/bitint2.C: Likewise.
parent c20c9d84
No related branches found
No related tags found
Loading
Showing
with 227 additions and 235 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment