13ad841b2Smrg /* Source locations within string literals.
2*4c3eb207Smrg Copyright (C) 2016-2020 Free Software Foundation, Inc.
33ad841b2Smrg
43ad841b2Smrg This file is part of GCC.
53ad841b2Smrg
63ad841b2Smrg GCC is free software; you can redistribute it and/or modify it under
73ad841b2Smrg the terms of the GNU General Public License as published by the Free
83ad841b2Smrg Software Foundation; either version 3, or (at your option) any later
93ad841b2Smrg version.
103ad841b2Smrg
113ad841b2Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
123ad841b2Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
133ad841b2Smrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
143ad841b2Smrg for more details.
153ad841b2Smrg
163ad841b2Smrg You should have received a copy of the GNU General Public License
173ad841b2Smrg along with GCC; see the file COPYING3. If not see
183ad841b2Smrg <http://www.gnu.org/licenses/>. */
193ad841b2Smrg
203ad841b2Smrg #include "config.h"
213ad841b2Smrg #include "system.h"
223ad841b2Smrg #include "coretypes.h"
23cef8759bSmrg #include "intl.h"
243ad841b2Smrg #include "diagnostic.h"
253ad841b2Smrg #include "cpplib.h"
263ad841b2Smrg #include "tree.h"
273ad841b2Smrg #include "langhooks.h"
283ad841b2Smrg #include "substring-locations.h"
29627f7eb2Smrg #include "gcc-rich-location.h"
303ad841b2Smrg
31627f7eb2Smrg /* format_string_diagnostic_t's ctor, giving information for use by
32627f7eb2Smrg the emit_warning* member functions, as follows:
333ad841b2Smrg
34627f7eb2Smrg They attempt to obtain precise location information within a string
353ad841b2Smrg literal from FMT_LOC.
363ad841b2Smrg
373ad841b2Smrg Case 1: if substring location is available, and is within the range of
383ad841b2Smrg the format string itself, the primary location of the
393ad841b2Smrg diagnostic is the substring range obtained from FMT_LOC, with the
403ad841b2Smrg caret at the *end* of the substring range.
413ad841b2Smrg
423ad841b2Smrg For example:
433ad841b2Smrg
443ad841b2Smrg test.c:90:10: warning: problem with '%i' here [-Wformat=]
453ad841b2Smrg printf ("hello %i", msg);
463ad841b2Smrg ~^
473ad841b2Smrg
483ad841b2Smrg Case 2: if the substring location is available, but is not within
493ad841b2Smrg the range of the format string, the primary location is that of the
50627f7eb2Smrg format string, and a note is emitted showing the substring location.
513ad841b2Smrg
523ad841b2Smrg For example:
533ad841b2Smrg test.c:90:10: warning: problem with '%i' here [-Wformat=]
543ad841b2Smrg printf("hello " INT_FMT " world", msg);
553ad841b2Smrg ^~~~~~~~~~~~~~~~~~~~~~~~~
563ad841b2Smrg test.c:19: note: format string is defined here
573ad841b2Smrg #define INT_FMT "%i"
583ad841b2Smrg ~^
593ad841b2Smrg
603ad841b2Smrg Case 3: if precise substring information is unavailable, the primary
613ad841b2Smrg location is that of the whole string passed to FMT_LOC's constructor.
623ad841b2Smrg For example:
633ad841b2Smrg
643ad841b2Smrg test.c:90:10: warning: problem with '%i' here [-Wformat=]
653ad841b2Smrg printf(fmt, msg);
663ad841b2Smrg ^~~
673ad841b2Smrg
68cef8759bSmrg For each of cases 1-3, if param_loc is not UNKNOWN_LOCATION, then it is used
693ad841b2Smrg as a secondary range within the warning. For example, here it
703ad841b2Smrg is used with case 1:
713ad841b2Smrg
723ad841b2Smrg test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
733ad841b2Smrg printf ("foo %s bar", long_i + long_j);
743ad841b2Smrg ~^ ~~~~~~~~~~~~~~~
753ad841b2Smrg
763ad841b2Smrg and here with case 2:
773ad841b2Smrg
783ad841b2Smrg test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
793ad841b2Smrg printf ("foo " STR_FMT " bar", long_i + long_j);
803ad841b2Smrg ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
813ad841b2Smrg test.c:89:16: note: format string is defined here
823ad841b2Smrg #define STR_FMT "%s"
833ad841b2Smrg ~^
843ad841b2Smrg
853ad841b2Smrg and with case 3:
863ad841b2Smrg
873ad841b2Smrg test.c:90:10: warning: '%i' here, but arg 2 is "const char *' [-Wformat=]
883ad841b2Smrg printf(fmt, msg);
893ad841b2Smrg ^~~ ~~~
903ad841b2Smrg
91627f7eb2Smrg If non-NULL, then FMT_LABEL will be used to label the location within the
92627f7eb2Smrg string for cases 1 and 2; if non-NULL, then PARAM_LABEL will be used to label
93627f7eb2Smrg the parameter. For example with case 1:
94627f7eb2Smrg
95627f7eb2Smrg test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
96627f7eb2Smrg printf ("foo %s bar", long_i + long_j);
97627f7eb2Smrg ~^ ~~~~~~~~~~~~~~~
98627f7eb2Smrg |
99627f7eb2Smrg int
100627f7eb2Smrg
101627f7eb2Smrg and with case 2:
102627f7eb2Smrg
103627f7eb2Smrg test.c:90:10: warning: problem with '%i' here [-Wformat=]
104627f7eb2Smrg printf("hello " INT_FMT " world", msg);
105627f7eb2Smrg ^~~~~~~~~~~~~~~~~~~~~~~~~
106627f7eb2Smrg test.c:19: note: format string is defined here
107627f7eb2Smrg #define INT_FMT "%i"
108627f7eb2Smrg ~^
109627f7eb2Smrg |
110627f7eb2Smrg int
111627f7eb2Smrg
1123ad841b2Smrg If CORRECTED_SUBSTRING is non-NULL, use it for cases 1 and 2 to provide
1133ad841b2Smrg a fix-it hint, suggesting that it should replace the text within the
1143ad841b2Smrg substring range. For example:
1153ad841b2Smrg
1163ad841b2Smrg test.c:90:10: warning: problem with '%i' here [-Wformat=]
1173ad841b2Smrg printf ("hello %i", msg);
1183ad841b2Smrg ~^
1193ad841b2Smrg %s
1203ad841b2Smrg
121627f7eb2Smrg */
122627f7eb2Smrg
123627f7eb2Smrg format_string_diagnostic_t::
format_string_diagnostic_t(const substring_loc & fmt_loc,const range_label * fmt_label,location_t param_loc,const range_label * param_label,const char * corrected_substring)124627f7eb2Smrg format_string_diagnostic_t (const substring_loc &fmt_loc,
125627f7eb2Smrg const range_label *fmt_label,
126627f7eb2Smrg location_t param_loc,
127627f7eb2Smrg const range_label *param_label,
128627f7eb2Smrg const char *corrected_substring)
129627f7eb2Smrg : m_fmt_loc (fmt_loc),
130627f7eb2Smrg m_fmt_label (fmt_label),
131627f7eb2Smrg m_param_loc (param_loc),
132627f7eb2Smrg m_param_label (param_label),
133627f7eb2Smrg m_corrected_substring (corrected_substring)
134627f7eb2Smrg {
135627f7eb2Smrg }
136627f7eb2Smrg
137627f7eb2Smrg /* Emit a warning governed by option OPT, using SINGULAR_GMSGID as the
138627f7eb2Smrg format string (or if PLURAL_GMSGID is different from SINGULAR_GMSGID,
139627f7eb2Smrg using SINGULAR_GMSGID, PLURAL_GMSGID and N as arguments to ngettext)
140627f7eb2Smrg and AP as its arguments.
141627f7eb2Smrg
1423ad841b2Smrg Return true if a warning was emitted, false otherwise. */
1433ad841b2Smrg
1443ad841b2Smrg bool
emit_warning_n_va(int opt,unsigned HOST_WIDE_INT n,const char * singular_gmsgid,const char * plural_gmsgid,va_list * ap)145627f7eb2Smrg format_string_diagnostic_t::emit_warning_n_va (int opt,
146627f7eb2Smrg unsigned HOST_WIDE_INT n,
147cef8759bSmrg const char *singular_gmsgid,
148627f7eb2Smrg const char *plural_gmsgid,
149627f7eb2Smrg va_list *ap) const
1503ad841b2Smrg {
1513ad841b2Smrg bool substring_within_range = false;
1523ad841b2Smrg location_t primary_loc;
1533ad841b2Smrg location_t fmt_substring_loc = UNKNOWN_LOCATION;
1543ad841b2Smrg source_range fmt_loc_range
155627f7eb2Smrg = get_range_from_loc (line_table, m_fmt_loc.get_fmt_string_loc ());
156627f7eb2Smrg const char *err = m_fmt_loc.get_location (&fmt_substring_loc);
1573ad841b2Smrg source_range fmt_substring_range
1583ad841b2Smrg = get_range_from_loc (line_table, fmt_substring_loc);
1593ad841b2Smrg if (err)
1603ad841b2Smrg /* Case 3: unable to get substring location. */
161627f7eb2Smrg primary_loc = m_fmt_loc.get_fmt_string_loc ();
1623ad841b2Smrg else
1633ad841b2Smrg {
1643ad841b2Smrg if (fmt_substring_range.m_start >= fmt_loc_range.m_start
1653ad841b2Smrg && fmt_substring_range.m_start <= fmt_loc_range.m_finish
1663ad841b2Smrg && fmt_substring_range.m_finish >= fmt_loc_range.m_start
1673ad841b2Smrg && fmt_substring_range.m_finish <= fmt_loc_range.m_finish)
1683ad841b2Smrg /* Case 1. */
1693ad841b2Smrg {
1703ad841b2Smrg substring_within_range = true;
1713ad841b2Smrg primary_loc = fmt_substring_loc;
1723ad841b2Smrg }
1733ad841b2Smrg else
1743ad841b2Smrg /* Case 2. */
1753ad841b2Smrg {
1763ad841b2Smrg substring_within_range = false;
177627f7eb2Smrg primary_loc = m_fmt_loc.get_fmt_string_loc ();
1783ad841b2Smrg }
1793ad841b2Smrg }
1803ad841b2Smrg
181627f7eb2Smrg /* Only use fmt_label in the initial warning for case 1. */
182627f7eb2Smrg const range_label *primary_label = NULL;
183627f7eb2Smrg if (substring_within_range)
184627f7eb2Smrg primary_label = m_fmt_label;
1853ad841b2Smrg
186627f7eb2Smrg auto_diagnostic_group d;
187627f7eb2Smrg gcc_rich_location richloc (primary_loc, primary_label);
1883ad841b2Smrg
189627f7eb2Smrg if (m_param_loc != UNKNOWN_LOCATION)
190627f7eb2Smrg richloc.add_range (m_param_loc, SHOW_RANGE_WITHOUT_CARET, m_param_label);
191627f7eb2Smrg
192627f7eb2Smrg if (!err && m_corrected_substring && substring_within_range)
193627f7eb2Smrg richloc.add_fixit_replace (fmt_substring_range, m_corrected_substring);
1943ad841b2Smrg
1953ad841b2Smrg diagnostic_info diagnostic;
196cef8759bSmrg if (singular_gmsgid != plural_gmsgid)
197cef8759bSmrg {
198cef8759bSmrg unsigned long gtn;
199cef8759bSmrg
200cef8759bSmrg if (sizeof n <= sizeof gtn)
201cef8759bSmrg gtn = n;
202cef8759bSmrg else
203cef8759bSmrg /* Use the largest number ngettext can handle, otherwise
204cef8759bSmrg preserve the six least significant decimal digits for
205cef8759bSmrg languages where the plural form depends on them. */
206cef8759bSmrg gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
207cef8759bSmrg
208cef8759bSmrg const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
209cef8759bSmrg diagnostic_set_info_translated (&diagnostic, text, ap, &richloc,
210cef8759bSmrg DK_WARNING);
211cef8759bSmrg }
212cef8759bSmrg else
213cef8759bSmrg diagnostic_set_info (&diagnostic, singular_gmsgid, ap, &richloc,
214cef8759bSmrg DK_WARNING);
2153ad841b2Smrg diagnostic.option_index = opt;
216cef8759bSmrg bool warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
2173ad841b2Smrg
2183ad841b2Smrg if (!err && fmt_substring_loc && !substring_within_range)
2193ad841b2Smrg /* Case 2. */
2203ad841b2Smrg if (warned)
2213ad841b2Smrg {
222627f7eb2Smrg /* Use fmt_label in the note for case 2. */
223627f7eb2Smrg rich_location substring_richloc (line_table, fmt_substring_loc,
224627f7eb2Smrg m_fmt_label);
225627f7eb2Smrg if (m_corrected_substring)
2263ad841b2Smrg substring_richloc.add_fixit_replace (fmt_substring_range,
227627f7eb2Smrg m_corrected_substring);
228cef8759bSmrg inform (&substring_richloc,
2293ad841b2Smrg "format string is defined here");
2303ad841b2Smrg }
2313ad841b2Smrg
2323ad841b2Smrg return warned;
2333ad841b2Smrg }
2343ad841b2Smrg
235cef8759bSmrg /* Singular-only version of the above. */
236cef8759bSmrg
237cef8759bSmrg bool
emit_warning_va(int opt,const char * gmsgid,va_list * ap)238627f7eb2Smrg format_string_diagnostic_t::emit_warning_va (int opt, const char *gmsgid,
239627f7eb2Smrg va_list *ap) const
240cef8759bSmrg {
241627f7eb2Smrg return emit_warning_n_va (opt, 0, gmsgid, gmsgid, ap);
242cef8759bSmrg }
243cef8759bSmrg
244627f7eb2Smrg /* Variadic version of the above (singular only). */
2453ad841b2Smrg
2463ad841b2Smrg bool
emit_warning(int opt,const char * gmsgid,...)247627f7eb2Smrg format_string_diagnostic_t::emit_warning (int opt, const char *gmsgid,
248627f7eb2Smrg ...) const
2493ad841b2Smrg {
2503ad841b2Smrg va_list ap;
2513ad841b2Smrg va_start (ap, gmsgid);
252627f7eb2Smrg bool warned = emit_warning_va (opt, gmsgid, &ap);
2533ad841b2Smrg va_end (ap);
2543ad841b2Smrg
2553ad841b2Smrg return warned;
2563ad841b2Smrg }
2573ad841b2Smrg
258627f7eb2Smrg /* Variadic version of the above (singular vs plural). */
259cef8759bSmrg
260cef8759bSmrg bool
emit_warning_n(int opt,unsigned HOST_WIDE_INT n,const char * singular_gmsgid,const char * plural_gmsgid,...)261627f7eb2Smrg format_string_diagnostic_t::emit_warning_n (int opt, unsigned HOST_WIDE_INT n,
262cef8759bSmrg const char *singular_gmsgid,
263627f7eb2Smrg const char *plural_gmsgid,
264627f7eb2Smrg ...) const
265cef8759bSmrg {
266cef8759bSmrg va_list ap;
267cef8759bSmrg va_start (ap, plural_gmsgid);
268627f7eb2Smrg bool warned = emit_warning_n_va (opt, n, singular_gmsgid, plural_gmsgid,
269cef8759bSmrg &ap);
270cef8759bSmrg va_end (ap);
271cef8759bSmrg
272cef8759bSmrg return warned;
273cef8759bSmrg }
274cef8759bSmrg
2753ad841b2Smrg /* Attempt to determine the source location of the substring.
2763ad841b2Smrg If successful, return NULL and write the source location to *OUT_LOC.
2773ad841b2Smrg Otherwise return an error message. Error messages are intended
2783ad841b2Smrg for GCC developers (to help debugging) rather than for end-users. */
2793ad841b2Smrg
2803ad841b2Smrg const char *
get_location(location_t * out_loc)2813ad841b2Smrg substring_loc::get_location (location_t *out_loc) const
2823ad841b2Smrg {
2833ad841b2Smrg gcc_assert (out_loc);
2843ad841b2Smrg return lang_hooks.get_substring_location (*this, out_loc);
2853ad841b2Smrg }
286