Commit c896ecfe by David Malcolm Committed by David Malcolm

substring-locations: add class format_string_diagnostic_t

With the addition of ranges in r263564, format_warning_at_substring_n
has 10 arguments.

Reduce the number of args by bundling the shared ones into a
class format_string_diagnostic_t.

gcc/c-family/ChangeLog:
	* c-format.c (format_warning_at_char): Update for introduction of
	format_string_diagnostic_t.
	(format_type_warning): Likewise.

gcc/ChangeLog:
	* gimple-ssa-sprintf.c (fmtwarn): Update for introduction of
	format_string_diagnostic_t.
	(fmtwarn_n): Likewise.
	* substring-locations.c
	(format_string_diagnostic_t::format_string_diagnostic_t) New ctor.
	(format_warning_n_va): Convert to...
	(format_string_diagnostic_t::emit_warning_n_va): ...this.
	(format_warning_va): Convert to...
	(format_string_diagnostic_t::emit_warning_va): ...this.
	(format_warning_at_substring): Convert to...
	(format_string_diagnostic_t::emit_warning): ...this.
	(format_warning_at_substring_n): Convert to...
	(format_string_diagnostic_t::emit_warning_n): ...this.
	* substring-locations.h (class format_string_diagnostic_t): New
	class.
	(format_warning_va): Convert to
	format_string_diagnostic_t::emit_warning_va.
	(format_warning_n_va): Convert to
	format_string_diagnostic_t::emit_warning_n_va.
	(format_warning_at_substring): Convert to
	format_string_diagnostic_t::emit_warning.
	(format_warning_at_substring_n): Convert to
	format_string_diagnostic_t::emit_warning_n.

From-SVN: r264372
parent 69d7aabf
2018-09-17 David Malcolm <dmalcolm@redhat.com>
* gimple-ssa-sprintf.c (fmtwarn): Update for introduction of
format_string_diagnostic_t.
(fmtwarn_n): Likewise.
* substring-locations.c
(format_string_diagnostic_t::format_string_diagnostic_t) New ctor.
(format_warning_n_va): Convert to...
(format_string_diagnostic_t::emit_warning_n_va): ...this.
(format_warning_va): Convert to...
(format_string_diagnostic_t::emit_warning_va): ...this.
(format_warning_at_substring): Convert to...
(format_string_diagnostic_t::emit_warning): ...this.
(format_warning_at_substring_n): Convert to...
(format_string_diagnostic_t::emit_warning_n): ...this.
* substring-locations.h (class format_string_diagnostic_t): New
class.
(format_warning_va): Convert to
format_string_diagnostic_t::emit_warning_va.
(format_warning_n_va): Convert to
format_string_diagnostic_t::emit_warning_n_va.
(format_warning_at_substring): Convert to
format_string_diagnostic_t::emit_warning.
(format_warning_at_substring_n): Convert to
format_string_diagnostic_t::emit_warning_n.
2018-09-17 Cesar Philippidis <cesar@codesourcery.com> 2018-09-17 Cesar Philippidis <cesar@codesourcery.com>
Bernd Schmidt <bernds_cb1@t-online.de> Bernd Schmidt <bernds_cb1@t-online.de>
......
2018-09-17 David Malcolm <dmalcolm@redhat.com>
* c-format.c (format_warning_at_char): Update for introduction of
format_string_diagnostic_t.
(format_type_warning): Likewise.
2018-09-17 Martin Jambor <mjambor@suse.cz> 2018-09-17 Martin Jambor <mjambor@suse.cz>
PR c/63886 PR c/63886
......
...@@ -100,8 +100,9 @@ format_warning_at_char (location_t fmt_string_loc, tree format_string_cst, ...@@ -100,8 +100,9 @@ format_warning_at_char (location_t fmt_string_loc, tree format_string_cst,
substring_loc fmt_loc (fmt_string_loc, string_type, char_idx, char_idx, substring_loc fmt_loc (fmt_string_loc, string_type, char_idx, char_idx,
char_idx); char_idx);
bool warned = format_warning_va (fmt_loc, NULL, UNKNOWN_LOCATION, NULL, format_string_diagnostic_t diag (fmt_loc, NULL, UNKNOWN_LOCATION, NULL,
NULL, opt, gmsgid, &ap); NULL);
bool warned = diag.emit_warning_va (opt, gmsgid, &ap);
va_end (ap); va_end (ap);
return warned; return warned;
...@@ -3694,13 +3695,13 @@ format_type_warning (const substring_loc &whole_fmt_loc, ...@@ -3694,13 +3695,13 @@ format_type_warning (const substring_loc &whole_fmt_loc,
char *corrected_substring char *corrected_substring
= get_corrected_substring (fmt_loc, type, arg_type, fki, = get_corrected_substring (fmt_loc, type, arg_type, fki,
offset_to_type_start, conversion_char); offset_to_type_start, conversion_char);
format_string_diagnostic_t diag (fmt_loc, &fmt_label, param_loc, &param_label,
corrected_substring);
if (wanted_type_name) if (wanted_type_name)
{ {
if (arg_type) if (arg_type)
format_warning_at_substring diag.emit_warning
(fmt_loc, &fmt_label, param_loc, &param_label, (OPT_Wformat_,
corrected_substring, OPT_Wformat_,
"%s %<%s%.*s%> expects argument of type %<%s%s%>, " "%s %<%s%.*s%> expects argument of type %<%s%s%>, "
"but argument %d has type %qT", "but argument %d has type %qT",
gettext (kind_descriptions[kind]), gettext (kind_descriptions[kind]),
...@@ -3708,9 +3709,8 @@ format_type_warning (const substring_loc &whole_fmt_loc, ...@@ -3708,9 +3709,8 @@ format_type_warning (const substring_loc &whole_fmt_loc,
format_length, format_start, format_length, format_start,
wanted_type_name, p, arg_num, arg_type); wanted_type_name, p, arg_num, arg_type);
else else
format_warning_at_substring diag.emit_warning
(fmt_loc, &fmt_label, param_loc, &param_label, (OPT_Wformat_,
corrected_substring, OPT_Wformat_,
"%s %<%s%.*s%> expects a matching %<%s%s%> argument", "%s %<%s%.*s%> expects a matching %<%s%s%> argument",
gettext (kind_descriptions[kind]), gettext (kind_descriptions[kind]),
(kind == CF_KIND_FORMAT ? "%" : ""), (kind == CF_KIND_FORMAT ? "%" : ""),
...@@ -3719,9 +3719,8 @@ format_type_warning (const substring_loc &whole_fmt_loc, ...@@ -3719,9 +3719,8 @@ format_type_warning (const substring_loc &whole_fmt_loc,
else else
{ {
if (arg_type) if (arg_type)
format_warning_at_substring diag.emit_warning
(fmt_loc, &fmt_label, param_loc, &param_label, (OPT_Wformat_,
corrected_substring, OPT_Wformat_,
"%s %<%s%.*s%> expects argument of type %<%T%s%>, " "%s %<%s%.*s%> expects argument of type %<%T%s%>, "
"but argument %d has type %qT", "but argument %d has type %qT",
gettext (kind_descriptions[kind]), gettext (kind_descriptions[kind]),
...@@ -3729,9 +3728,8 @@ format_type_warning (const substring_loc &whole_fmt_loc, ...@@ -3729,9 +3728,8 @@ format_type_warning (const substring_loc &whole_fmt_loc,
format_length, format_start, format_length, format_start,
wanted_type, p, arg_num, arg_type); wanted_type, p, arg_num, arg_type);
else else
format_warning_at_substring diag.emit_warning
(fmt_loc, &fmt_label, param_loc, &param_label, (OPT_Wformat_,
corrected_substring, OPT_Wformat_,
"%s %<%s%.*s%> expects a matching %<%T%s%> argument", "%s %<%s%.*s%> expects a matching %<%T%s%> argument",
gettext (kind_descriptions[kind]), gettext (kind_descriptions[kind]),
(kind == CF_KIND_FORMAT ? "%" : ""), (kind == CF_KIND_FORMAT ? "%" : ""),
......
...@@ -455,7 +455,8 @@ get_format_string (tree format, location_t *ploc) ...@@ -455,7 +455,8 @@ get_format_string (tree format, location_t *ploc)
} }
/* For convenience and brevity, shorter named entrypoints of /* For convenience and brevity, shorter named entrypoints of
format_warning_at_substring and format_warning_at_substring_n. format_string_diagnostic_t::emit_warning_va and
format_string_diagnostic_t::emit_warning_n_va.
These have to be functions with the attribute so that exgettext These have to be functions with the attribute so that exgettext
works properly. */ works properly. */
...@@ -464,10 +465,11 @@ ATTRIBUTE_GCC_DIAG (5, 6) ...@@ -464,10 +465,11 @@ ATTRIBUTE_GCC_DIAG (5, 6)
fmtwarn (const substring_loc &fmt_loc, location_t param_loc, fmtwarn (const substring_loc &fmt_loc, location_t param_loc,
const char *corrected_substring, int opt, const char *gmsgid, ...) const char *corrected_substring, int opt, const char *gmsgid, ...)
{ {
format_string_diagnostic_t diag (fmt_loc, NULL, param_loc, NULL,
corrected_substring);
va_list ap; va_list ap;
va_start (ap, gmsgid); va_start (ap, gmsgid);
bool warned = format_warning_va (fmt_loc, NULL, param_loc, NULL, bool warned = diag.emit_warning_va (opt, gmsgid, &ap);
corrected_substring, opt, gmsgid, &ap);
va_end (ap); va_end (ap);
return warned; return warned;
...@@ -479,11 +481,11 @@ fmtwarn_n (const substring_loc &fmt_loc, location_t param_loc, ...@@ -479,11 +481,11 @@ fmtwarn_n (const substring_loc &fmt_loc, location_t param_loc,
const char *corrected_substring, int opt, unsigned HOST_WIDE_INT n, const char *corrected_substring, int opt, unsigned HOST_WIDE_INT n,
const char *singular_gmsgid, const char *plural_gmsgid, ...) const char *singular_gmsgid, const char *plural_gmsgid, ...)
{ {
format_string_diagnostic_t diag (fmt_loc, NULL, param_loc, NULL,
corrected_substring);
va_list ap; va_list ap;
va_start (ap, plural_gmsgid); va_start (ap, plural_gmsgid);
bool warned = format_warning_n_va (fmt_loc, NULL, param_loc, NULL, bool warned = diag.emit_warning_n_va (opt, n, singular_gmsgid, plural_gmsgid,
corrected_substring,
opt, n, singular_gmsgid, plural_gmsgid,
&ap); &ap);
va_end (ap); va_end (ap);
......
...@@ -28,12 +28,10 @@ along with GCC; see the file COPYING3. If not see ...@@ -28,12 +28,10 @@ along with GCC; see the file COPYING3. If not see
#include "substring-locations.h" #include "substring-locations.h"
#include "gcc-rich-location.h" #include "gcc-rich-location.h"
/* Emit a warning governed by option OPT, using SINGULAR_GMSGID as the /* format_string_diagnostic_t's ctor, giving information for use by
format string (or if PLURAL_GMSGID is different from SINGULAR_GMSGID, the emit_warning* member functions, as follows:
using SINGULAR_GMSGID, PLURAL_GMSGID and N as arguments to ngettext)
and AP as its arguments.
Attempt to obtain precise location information within a string They attempt to obtain precise location information within a string
literal from FMT_LOC. literal from FMT_LOC.
Case 1: if substring location is available, and is within the range of Case 1: if substring location is available, and is within the range of
...@@ -49,7 +47,7 @@ along with GCC; see the file COPYING3. If not see ...@@ -49,7 +47,7 @@ along with GCC; see the file COPYING3. If not see
Case 2: if the substring location is available, but is not within Case 2: if the substring location is available, but is not within
the range of the format string, the primary location is that of the the range of the format string, the primary location is that of the
format string, and an note is emitted showing the substring location. format string, and a note is emitted showing the substring location.
For example: For example:
test.c:90:10: warning: problem with '%i' here [-Wformat=] test.c:90:10: warning: problem with '%i' here [-Wformat=]
...@@ -120,29 +118,47 @@ along with GCC; see the file COPYING3. If not see ...@@ -120,29 +118,47 @@ along with GCC; see the file COPYING3. If not see
~^ ~^
%s %s
Return true if a warning was emitted, false otherwise. */ */
bool format_string_diagnostic_t::
format_warning_n_va (const substring_loc &fmt_loc, format_string_diagnostic_t (const substring_loc &fmt_loc,
const range_label *fmt_label, const range_label *fmt_label,
location_t param_loc, location_t param_loc,
const range_label *param_label, const range_label *param_label,
const char *corrected_substring, const char *corrected_substring)
int opt, unsigned HOST_WIDE_INT n, : m_fmt_loc (fmt_loc),
m_fmt_label (fmt_label),
m_param_loc (param_loc),
m_param_label (param_label),
m_corrected_substring (corrected_substring)
{
}
/* Emit a warning governed by option OPT, using SINGULAR_GMSGID as the
format string (or if PLURAL_GMSGID is different from SINGULAR_GMSGID,
using SINGULAR_GMSGID, PLURAL_GMSGID and N as arguments to ngettext)
and AP as its arguments.
Return true if a warning was emitted, false otherwise. */
bool
format_string_diagnostic_t::emit_warning_n_va (int opt,
unsigned HOST_WIDE_INT n,
const char *singular_gmsgid, const char *singular_gmsgid,
const char *plural_gmsgid, va_list *ap) const char *plural_gmsgid,
va_list *ap) const
{ {
bool substring_within_range = false; bool substring_within_range = false;
location_t primary_loc; location_t primary_loc;
location_t fmt_substring_loc = UNKNOWN_LOCATION; location_t fmt_substring_loc = UNKNOWN_LOCATION;
source_range fmt_loc_range source_range fmt_loc_range
= get_range_from_loc (line_table, fmt_loc.get_fmt_string_loc ()); = get_range_from_loc (line_table, m_fmt_loc.get_fmt_string_loc ());
const char *err = fmt_loc.get_location (&fmt_substring_loc); const char *err = m_fmt_loc.get_location (&fmt_substring_loc);
source_range fmt_substring_range source_range fmt_substring_range
= get_range_from_loc (line_table, fmt_substring_loc); = get_range_from_loc (line_table, fmt_substring_loc);
if (err) if (err)
/* Case 3: unable to get substring location. */ /* Case 3: unable to get substring location. */
primary_loc = fmt_loc.get_fmt_string_loc (); primary_loc = m_fmt_loc.get_fmt_string_loc ();
else else
{ {
if (fmt_substring_range.m_start >= fmt_loc_range.m_start if (fmt_substring_range.m_start >= fmt_loc_range.m_start
...@@ -158,23 +174,23 @@ format_warning_n_va (const substring_loc &fmt_loc, ...@@ -158,23 +174,23 @@ format_warning_n_va (const substring_loc &fmt_loc,
/* Case 2. */ /* Case 2. */
{ {
substring_within_range = false; substring_within_range = false;
primary_loc = fmt_loc.get_fmt_string_loc (); primary_loc = m_fmt_loc.get_fmt_string_loc ();
} }
} }
/* Only use fmt_label in the initial warning for case 1. */ /* Only use fmt_label in the initial warning for case 1. */
const range_label *primary_label = NULL; const range_label *primary_label = NULL;
if (substring_within_range) if (substring_within_range)
primary_label = fmt_label; primary_label = m_fmt_label;
auto_diagnostic_group d; auto_diagnostic_group d;
gcc_rich_location richloc (primary_loc, primary_label); gcc_rich_location richloc (primary_loc, primary_label);
if (param_loc != UNKNOWN_LOCATION) if (m_param_loc != UNKNOWN_LOCATION)
richloc.add_range (param_loc, SHOW_RANGE_WITHOUT_CARET, param_label); richloc.add_range (m_param_loc, SHOW_RANGE_WITHOUT_CARET, m_param_label);
if (!err && corrected_substring && substring_within_range) if (!err && m_corrected_substring && substring_within_range)
richloc.add_fixit_replace (fmt_substring_range, corrected_substring); richloc.add_fixit_replace (fmt_substring_range, m_corrected_substring);
diagnostic_info diagnostic; diagnostic_info diagnostic;
if (singular_gmsgid != plural_gmsgid) if (singular_gmsgid != plural_gmsgid)
...@@ -205,10 +221,10 @@ format_warning_n_va (const substring_loc &fmt_loc, ...@@ -205,10 +221,10 @@ format_warning_n_va (const substring_loc &fmt_loc,
{ {
/* Use fmt_label in the note for case 2. */ /* Use fmt_label in the note for case 2. */
rich_location substring_richloc (line_table, fmt_substring_loc, rich_location substring_richloc (line_table, fmt_substring_loc,
fmt_label); m_fmt_label);
if (corrected_substring) if (m_corrected_substring)
substring_richloc.add_fixit_replace (fmt_substring_range, substring_richloc.add_fixit_replace (fmt_substring_range,
corrected_substring); m_corrected_substring);
inform (&substring_richloc, inform (&substring_richloc,
"format string is defined here"); "format string is defined here");
} }
...@@ -219,54 +235,37 @@ format_warning_n_va (const substring_loc &fmt_loc, ...@@ -219,54 +235,37 @@ format_warning_n_va (const substring_loc &fmt_loc,
/* Singular-only version of the above. */ /* Singular-only version of the above. */
bool bool
format_warning_va (const substring_loc &fmt_loc, format_string_diagnostic_t::emit_warning_va (int opt, const char *gmsgid,
const range_label *fmt_label, va_list *ap) const
location_t param_loc,
const range_label *param_label,
const char *corrected_substring,
int opt, const char *gmsgid, va_list *ap)
{ {
return format_warning_n_va (fmt_loc, fmt_label, param_loc, param_label, return emit_warning_n_va (opt, 0, gmsgid, gmsgid, ap);
corrected_substring, opt,
0, gmsgid, gmsgid, ap);
} }
/* Variadic call to format_warning_va. */ /* Variadic version of the above (singular only). */
bool bool
format_warning_at_substring (const substring_loc &fmt_loc, format_string_diagnostic_t::emit_warning (int opt, const char *gmsgid,
const range_label *fmt_label, ...) const
location_t param_loc,
const range_label *param_label,
const char *corrected_substring,
int opt, const char *gmsgid, ...)
{ {
va_list ap; va_list ap;
va_start (ap, gmsgid); va_start (ap, gmsgid);
bool warned = format_warning_va (fmt_loc, fmt_label, param_loc, param_label, bool warned = emit_warning_va (opt, gmsgid, &ap);
corrected_substring, opt, gmsgid, &ap);
va_end (ap); va_end (ap);
return warned; return warned;
} }
/* Variadic call to format_warning_n_va. */ /* Variadic version of the above (singular vs plural). */
bool bool
format_warning_at_substring_n (const substring_loc &fmt_loc, format_string_diagnostic_t::emit_warning_n (int opt, unsigned HOST_WIDE_INT n,
const range_label *fmt_label,
location_t param_loc,
const range_label *param_label,
const char *corrected_substring,
int opt, unsigned HOST_WIDE_INT n,
const char *singular_gmsgid, const char *singular_gmsgid,
const char *plural_gmsgid, ...) const char *plural_gmsgid,
...) const
{ {
va_list ap; va_list ap;
va_start (ap, plural_gmsgid); va_start (ap, plural_gmsgid);
bool warned = format_warning_n_va (fmt_loc, fmt_label, param_loc, param_label, bool warned = emit_warning_n_va (opt, n, singular_gmsgid, plural_gmsgid,
corrected_substring,
opt, n, singular_gmsgid, plural_gmsgid,
&ap); &ap);
va_end (ap); va_end (ap);
......
...@@ -74,43 +74,43 @@ class substring_loc ...@@ -74,43 +74,43 @@ class substring_loc
int m_end_idx; int m_end_idx;
}; };
/* Functions for emitting a warning about a format string. */ /* A bundle of state for emitting a diagnostic relating to a format string. */
extern bool format_warning_va (const substring_loc &fmt_loc, class format_string_diagnostic_t
{
public:
format_string_diagnostic_t (const substring_loc &fmt_loc,
const range_label *fmt_label, const range_label *fmt_label,
location_t param_loc, location_t param_loc,
const range_label *param_label, const range_label *param_label,
const char *corrected_substring, const char *corrected_substring);
int opt, const char *gmsgid, va_list *ap)
ATTRIBUTE_GCC_DIAG (7, 0);
extern bool format_warning_n_va (const substring_loc &fmt_loc, /* Functions for emitting a warning about a format string. */
const range_label *fmt_label,
location_t param_loc, bool emit_warning_va (int opt, const char *gmsgid, va_list *ap) const
const range_label *param_label, ATTRIBUTE_GCC_DIAG (3, 0);
const char *corrected_substring,
int opt, unsigned HOST_WIDE_INT n, bool emit_warning_n_va (int opt, unsigned HOST_WIDE_INT n,
const char *singular_gmsgid, const char *singular_gmsgid,
const char *plural_gmsgid, va_list *ap) const char *plural_gmsgid, va_list *ap) const
ATTRIBUTE_GCC_DIAG (8, 0) ATTRIBUTE_GCC_DIAG (9, 0); ATTRIBUTE_GCC_DIAG (4, 0) ATTRIBUTE_GCC_DIAG (5, 0);
extern bool format_warning_at_substring (const substring_loc &fmt_loc, bool emit_warning (int opt, const char *gmsgid, ...) const
const range_label *fmt_label, ATTRIBUTE_GCC_DIAG (3, 4);
location_t param_loc,
const range_label *param_label,
const char *corrected_substring,
int opt, const char *gmsgid, ...)
ATTRIBUTE_GCC_DIAG (7, 8);
extern bool format_warning_at_substring_n (const substring_loc &fmt_loc, bool emit_warning_n (int opt, unsigned HOST_WIDE_INT n,
const range_label *fmt_label,
location_t param_loc,
const range_label *param_label,
const char *corrected_substring,
int opt, unsigned HOST_WIDE_INT n,
const char *singular_gmsgid, const char *singular_gmsgid,
const char *plural_gmsgid, ...) const char *plural_gmsgid, ...) const
ATTRIBUTE_GCC_DIAG (8, 10) ATTRIBUTE_GCC_DIAG (9, 10); ATTRIBUTE_GCC_DIAG (4, 6) ATTRIBUTE_GCC_DIAG (5, 6);
private:
const substring_loc &m_fmt_loc;
const range_label *m_fmt_label;
location_t m_param_loc;
const range_label *m_param_label;
const char *m_corrected_substring;
};
/* Implementation detail, for use when implementing /* Implementation detail, for use when implementing
LANG_HOOKS_GET_SUBSTRING_LOCATION. */ LANG_HOOKS_GET_SUBSTRING_LOCATION. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment