1 /* Source locations within string literals.
2 Copyright (C) 2016-2025 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "diagnostic.h"
27 #include "langhooks.h"
28 #include "substring-locations.h"
29 #include "gcc-rich-location.h"
30 #include "diagnostic-highlight-colors.h"
33 format_string_diagnostic_t::highlight_color_format_string
34 = highlight_colors::expected
;
37 format_string_diagnostic_t::highlight_color_param
38 = highlight_colors::actual
;
40 /* format_string_diagnostic_t's ctor, giving information for use by
41 the emit_warning* member functions, as follows:
43 They attempt to obtain precise location information within a string
46 Case 1: if substring location is available, and is within the range of
47 the format string itself, the primary location of the
48 diagnostic is the substring range obtained from FMT_LOC, with the
49 caret at the *end* of the substring range.
53 test.c:90:10: warning: problem with '%i' here [-Wformat=]
54 printf ("hello %i", msg);
57 Case 2: if the substring location is available, but is not within
58 the range of the format string, the primary location is that of the
59 format string, and a note is emitted showing the substring location.
62 test.c:90:10: warning: problem with '%i' here [-Wformat=]
63 printf("hello " INT_FMT " world", msg);
64 ^~~~~~~~~~~~~~~~~~~~~~~~~
65 test.c:19: note: format string is defined here
69 Case 3: if precise substring information is unavailable, the primary
70 location is that of the whole string passed to FMT_LOC's constructor.
73 test.c:90:10: warning: problem with '%i' here [-Wformat=]
77 For each of cases 1-3, if param_loc is not UNKNOWN_LOCATION, then it is used
78 as a secondary range within the warning. For example, here it
81 test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
82 printf ("foo %s bar", long_i + long_j);
87 test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
88 printf ("foo " STR_FMT " bar", long_i + long_j);
89 ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
90 test.c:89:16: note: format string is defined here
96 test.c:90:10: warning: '%i' here, but arg 2 is "const char *' [-Wformat=]
100 If non-NULL, then FMT_LABEL will be used to label the location within the
101 string for cases 1 and 2; if non-NULL, then PARAM_LABEL will be used to label
102 the parameter. For example with case 1:
104 test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
105 printf ("foo %s bar", long_i + long_j);
112 test.c:90:10: warning: problem with '%i' here [-Wformat=]
113 printf("hello " INT_FMT " world", msg);
114 ^~~~~~~~~~~~~~~~~~~~~~~~~
115 test.c:19: note: format string is defined here
121 If CORRECTED_SUBSTRING is non-NULL, use it for cases 1 and 2 to provide
122 a fix-it hint, suggesting that it should replace the text within the
123 substring range. For example:
125 test.c:90:10: warning: problem with '%i' here [-Wformat=]
126 printf ("hello %i", msg);
132 format_string_diagnostic_t::
133 format_string_diagnostic_t (const substring_loc
&fmt_loc
,
134 const range_label
*fmt_label
,
135 location_t param_loc
,
136 const range_label
*param_label
,
137 const char *corrected_substring
)
138 : m_fmt_loc (fmt_loc
),
139 m_fmt_label (fmt_label
),
140 m_param_loc (param_loc
),
141 m_param_label (param_label
),
142 m_corrected_substring (corrected_substring
)
146 /* Emit a warning governed by option OPTION_ID, using SINGULAR_GMSGID as the
147 format string (or if PLURAL_GMSGID is different from SINGULAR_GMSGID,
148 using SINGULAR_GMSGID, PLURAL_GMSGID and N as arguments to ngettext)
149 and AP as its arguments.
151 Return true if a warning was emitted, false otherwise. */
154 format_string_diagnostic_t::emit_warning_n_va (diagnostic_option_id option_id
,
155 unsigned HOST_WIDE_INT n
,
156 const char *singular_gmsgid
,
157 const char *plural_gmsgid
,
160 bool substring_within_range
= false;
161 location_t primary_loc
;
162 location_t fmt_substring_loc
= UNKNOWN_LOCATION
;
163 source_range fmt_loc_range
164 = get_range_from_loc (line_table
, m_fmt_loc
.get_fmt_string_loc ());
165 const char *err
= m_fmt_loc
.get_location (&fmt_substring_loc
);
166 source_range fmt_substring_range
167 = get_range_from_loc (line_table
, fmt_substring_loc
);
169 /* Case 3: unable to get substring location. */
170 primary_loc
= m_fmt_loc
.get_fmt_string_loc ();
173 if (fmt_substring_range
.m_start
>= fmt_loc_range
.m_start
174 && fmt_substring_range
.m_start
<= fmt_loc_range
.m_finish
175 && fmt_substring_range
.m_finish
>= fmt_loc_range
.m_start
176 && fmt_substring_range
.m_finish
<= fmt_loc_range
.m_finish
)
179 substring_within_range
= true;
180 primary_loc
= fmt_substring_loc
;
185 substring_within_range
= false;
186 primary_loc
= m_fmt_loc
.get_fmt_string_loc ();
190 /* Only use fmt_label in the initial warning for case 1. */
191 const range_label
*primary_label
= NULL
;
192 if (substring_within_range
)
193 primary_label
= m_fmt_label
;
195 auto_diagnostic_group d
;
196 gcc_rich_location
richloc (primary_loc
, primary_label
,
197 highlight_color_format_string
);
199 if (m_param_loc
!= UNKNOWN_LOCATION
)
200 richloc
.add_range (m_param_loc
, SHOW_RANGE_WITHOUT_CARET
, m_param_label
,
201 highlight_color_param
);
203 if (!err
&& m_corrected_substring
&& substring_within_range
)
204 richloc
.add_fixit_replace (fmt_substring_range
, m_corrected_substring
);
206 diagnostic_info diagnostic
;
207 if (singular_gmsgid
!= plural_gmsgid
)
211 if (sizeof n
<= sizeof gtn
)
214 /* Use the largest number ngettext can handle, otherwise
215 preserve the six least significant decimal digits for
216 languages where the plural form depends on them. */
217 gtn
= n
<= ULONG_MAX
? n
: n
% 1000000LU + 1000000LU;
219 const char *text
= ngettext (singular_gmsgid
, plural_gmsgid
, gtn
);
220 diagnostic_set_info_translated (&diagnostic
, text
, ap
, &richloc
,
224 diagnostic_set_info (&diagnostic
, singular_gmsgid
, ap
, &richloc
,
226 diagnostic
.option_id
= option_id
;
227 bool warned
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
229 if (!err
&& fmt_substring_loc
&& !substring_within_range
)
233 /* Use fmt_label in the note for case 2. */
234 rich_location substring_richloc
235 (line_table
, fmt_substring_loc
,
237 highlight_color_format_string
);
238 if (m_corrected_substring
)
239 substring_richloc
.add_fixit_replace (fmt_substring_range
,
240 m_corrected_substring
);
241 inform (&substring_richloc
,
242 "format string is defined here");
248 /* Singular-only version of the above. */
251 format_string_diagnostic_t::emit_warning_va (diagnostic_option_id option_id
,
255 return emit_warning_n_va (option_id
, 0, gmsgid
, gmsgid
, ap
);
258 /* Variadic version of the above (singular only). */
261 format_string_diagnostic_t::emit_warning (diagnostic_option_id option_id
,
266 va_start (ap
, gmsgid
);
267 bool warned
= emit_warning_va (option_id
, gmsgid
, &ap
);
273 /* Variadic version of the above (singular vs plural). */
276 format_string_diagnostic_t::emit_warning_n (diagnostic_option_id option_id
,
277 unsigned HOST_WIDE_INT n
,
278 const char *singular_gmsgid
,
279 const char *plural_gmsgid
,
283 va_start (ap
, plural_gmsgid
);
284 bool warned
= emit_warning_n_va (option_id
, n
, singular_gmsgid
, plural_gmsgid
,
291 /* Attempt to determine the source location of the substring.
292 If successful, return NULL and write the source location to *OUT_LOC.
293 Otherwise return an error message. Error messages are intended
294 for GCC developers (to help debugging) rather than for end-users. */
297 substring_loc::get_location (location_t
*out_loc
) const
299 gcc_assert (out_loc
);
300 return lang_hooks
.get_substring_location (*this, out_loc
);