1*38fd1498Szrj /* Source locations within string literals.
2*38fd1498Szrj    Copyright (C) 2016-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "intl.h"
24*38fd1498Szrj #include "diagnostic.h"
25*38fd1498Szrj #include "cpplib.h"
26*38fd1498Szrj #include "tree.h"
27*38fd1498Szrj #include "langhooks.h"
28*38fd1498Szrj #include "substring-locations.h"
29*38fd1498Szrj 
30*38fd1498Szrj /* Emit a warning governed by option OPT, using SINGULAR_GMSGID as the
31*38fd1498Szrj    format string (or if PLURAL_GMSGID is different from SINGULAR_GMSGID,
32*38fd1498Szrj    using SINGULAR_GMSGID, PLURAL_GMSGID and N as arguments to ngettext)
33*38fd1498Szrj    and AP as its arguments.
34*38fd1498Szrj 
35*38fd1498Szrj    Attempt to obtain precise location information within a string
36*38fd1498Szrj    literal from FMT_LOC.
37*38fd1498Szrj 
38*38fd1498Szrj    Case 1: if substring location is available, and is within the range of
39*38fd1498Szrj    the format string itself, the primary location of the
40*38fd1498Szrj    diagnostic is the substring range obtained from FMT_LOC, with the
41*38fd1498Szrj    caret at the *end* of the substring range.
42*38fd1498Szrj 
43*38fd1498Szrj    For example:
44*38fd1498Szrj 
45*38fd1498Szrj      test.c:90:10: warning: problem with '%i' here [-Wformat=]
46*38fd1498Szrj      printf ("hello %i", msg);
47*38fd1498Szrj                     ~^
48*38fd1498Szrj 
49*38fd1498Szrj    Case 2: if the substring location is available, but is not within
50*38fd1498Szrj    the range of the format string, the primary location is that of the
51*38fd1498Szrj    format string, and an note is emitted showing the substring location.
52*38fd1498Szrj 
53*38fd1498Szrj    For example:
54*38fd1498Szrj      test.c:90:10: warning: problem with '%i' here [-Wformat=]
55*38fd1498Szrj      printf("hello " INT_FMT " world", msg);
56*38fd1498Szrj             ^~~~~~~~~~~~~~~~~~~~~~~~~
57*38fd1498Szrj      test.c:19: note: format string is defined here
58*38fd1498Szrj      #define INT_FMT "%i"
59*38fd1498Szrj                       ~^
60*38fd1498Szrj 
61*38fd1498Szrj    Case 3: if precise substring information is unavailable, the primary
62*38fd1498Szrj    location is that of the whole string passed to FMT_LOC's constructor.
63*38fd1498Szrj    For example:
64*38fd1498Szrj 
65*38fd1498Szrj      test.c:90:10: warning: problem with '%i' here [-Wformat=]
66*38fd1498Szrj      printf(fmt, msg);
67*38fd1498Szrj             ^~~
68*38fd1498Szrj 
69*38fd1498Szrj    For each of cases 1-3, if param_loc is not UNKNOWN_LOCATION, then it is used
70*38fd1498Szrj    as a secondary range within the warning.  For example, here it
71*38fd1498Szrj    is used with case 1:
72*38fd1498Szrj 
73*38fd1498Szrj      test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
74*38fd1498Szrj      printf ("foo %s bar", long_i + long_j);
75*38fd1498Szrj                   ~^       ~~~~~~~~~~~~~~~
76*38fd1498Szrj 
77*38fd1498Szrj    and here with case 2:
78*38fd1498Szrj 
79*38fd1498Szrj      test.c:90:16: warning: '%s' here but arg 2 has 'long' type [-Wformat=]
80*38fd1498Szrj      printf ("foo " STR_FMT " bar", long_i + long_j);
81*38fd1498Szrj              ^~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~
82*38fd1498Szrj      test.c:89:16: note: format string is defined here
83*38fd1498Szrj      #define STR_FMT "%s"
84*38fd1498Szrj                       ~^
85*38fd1498Szrj 
86*38fd1498Szrj    and with case 3:
87*38fd1498Szrj 
88*38fd1498Szrj      test.c:90:10: warning: '%i' here, but arg 2 is "const char *' [-Wformat=]
89*38fd1498Szrj      printf(fmt, msg);
90*38fd1498Szrj             ^~~  ~~~
91*38fd1498Szrj 
92*38fd1498Szrj    If CORRECTED_SUBSTRING is non-NULL, use it for cases 1 and 2 to provide
93*38fd1498Szrj    a fix-it hint, suggesting that it should replace the text within the
94*38fd1498Szrj    substring range.  For example:
95*38fd1498Szrj 
96*38fd1498Szrj      test.c:90:10: warning: problem with '%i' here [-Wformat=]
97*38fd1498Szrj      printf ("hello %i", msg);
98*38fd1498Szrj                     ~^
99*38fd1498Szrj                     %s
100*38fd1498Szrj 
101*38fd1498Szrj    Return true if a warning was emitted, false otherwise.  */
102*38fd1498Szrj 
103*38fd1498Szrj bool
format_warning_n_va(const substring_loc & fmt_loc,location_t param_loc,const char * corrected_substring,int opt,unsigned HOST_WIDE_INT n,const char * singular_gmsgid,const char * plural_gmsgid,va_list * ap)104*38fd1498Szrj format_warning_n_va (const substring_loc &fmt_loc,
105*38fd1498Szrj 		     location_t param_loc,
106*38fd1498Szrj 		     const char *corrected_substring,
107*38fd1498Szrj 		     int opt, unsigned HOST_WIDE_INT n,
108*38fd1498Szrj 		     const char *singular_gmsgid,
109*38fd1498Szrj 		     const char *plural_gmsgid, va_list *ap)
110*38fd1498Szrj {
111*38fd1498Szrj   bool substring_within_range = false;
112*38fd1498Szrj   location_t primary_loc;
113*38fd1498Szrj   location_t fmt_substring_loc = UNKNOWN_LOCATION;
114*38fd1498Szrj   source_range fmt_loc_range
115*38fd1498Szrj     = get_range_from_loc (line_table, fmt_loc.get_fmt_string_loc ());
116*38fd1498Szrj   const char *err = fmt_loc.get_location (&fmt_substring_loc);
117*38fd1498Szrj   source_range fmt_substring_range
118*38fd1498Szrj     = get_range_from_loc (line_table, fmt_substring_loc);
119*38fd1498Szrj   if (err)
120*38fd1498Szrj     /* Case 3: unable to get substring location.  */
121*38fd1498Szrj     primary_loc = fmt_loc.get_fmt_string_loc ();
122*38fd1498Szrj   else
123*38fd1498Szrj     {
124*38fd1498Szrj       if (fmt_substring_range.m_start >= fmt_loc_range.m_start
125*38fd1498Szrj 	  && fmt_substring_range.m_start <= fmt_loc_range.m_finish
126*38fd1498Szrj 	  && fmt_substring_range.m_finish >= fmt_loc_range.m_start
127*38fd1498Szrj 	  && fmt_substring_range.m_finish <= fmt_loc_range.m_finish)
128*38fd1498Szrj 	/* Case 1.  */
129*38fd1498Szrj 	{
130*38fd1498Szrj 	  substring_within_range = true;
131*38fd1498Szrj 	  primary_loc = fmt_substring_loc;
132*38fd1498Szrj 	}
133*38fd1498Szrj       else
134*38fd1498Szrj 	/* Case 2.  */
135*38fd1498Szrj 	{
136*38fd1498Szrj 	  substring_within_range = false;
137*38fd1498Szrj 	  primary_loc = fmt_loc.get_fmt_string_loc ();
138*38fd1498Szrj 	}
139*38fd1498Szrj     }
140*38fd1498Szrj 
141*38fd1498Szrj   rich_location richloc (line_table, primary_loc);
142*38fd1498Szrj 
143*38fd1498Szrj   if (param_loc != UNKNOWN_LOCATION)
144*38fd1498Szrj     richloc.add_range (param_loc, false);
145*38fd1498Szrj 
146*38fd1498Szrj   if (!err && corrected_substring && substring_within_range)
147*38fd1498Szrj     richloc.add_fixit_replace (fmt_substring_range, corrected_substring);
148*38fd1498Szrj 
149*38fd1498Szrj   diagnostic_info diagnostic;
150*38fd1498Szrj   if (singular_gmsgid != plural_gmsgid)
151*38fd1498Szrj     {
152*38fd1498Szrj       unsigned long gtn;
153*38fd1498Szrj 
154*38fd1498Szrj       if (sizeof n <= sizeof gtn)
155*38fd1498Szrj 	gtn = n;
156*38fd1498Szrj       else
157*38fd1498Szrj 	/* Use the largest number ngettext can handle, otherwise
158*38fd1498Szrj 	   preserve the six least significant decimal digits for
159*38fd1498Szrj 	   languages where the plural form depends on them.  */
160*38fd1498Szrj 	gtn = n <= ULONG_MAX ? n : n % 1000000LU + 1000000LU;
161*38fd1498Szrj 
162*38fd1498Szrj       const char *text = ngettext (singular_gmsgid, plural_gmsgid, gtn);
163*38fd1498Szrj       diagnostic_set_info_translated (&diagnostic, text, ap, &richloc,
164*38fd1498Szrj 				      DK_WARNING);
165*38fd1498Szrj     }
166*38fd1498Szrj   else
167*38fd1498Szrj     diagnostic_set_info (&diagnostic, singular_gmsgid, ap, &richloc,
168*38fd1498Szrj 			 DK_WARNING);
169*38fd1498Szrj   diagnostic.option_index = opt;
170*38fd1498Szrj   bool warned = diagnostic_report_diagnostic (global_dc, &diagnostic);
171*38fd1498Szrj 
172*38fd1498Szrj   if (!err && fmt_substring_loc && !substring_within_range)
173*38fd1498Szrj     /* Case 2.  */
174*38fd1498Szrj     if (warned)
175*38fd1498Szrj       {
176*38fd1498Szrj 	rich_location substring_richloc (line_table, fmt_substring_loc);
177*38fd1498Szrj 	if (corrected_substring)
178*38fd1498Szrj 	  substring_richloc.add_fixit_replace (fmt_substring_range,
179*38fd1498Szrj 					       corrected_substring);
180*38fd1498Szrj 	inform (&substring_richloc,
181*38fd1498Szrj 		"format string is defined here");
182*38fd1498Szrj       }
183*38fd1498Szrj 
184*38fd1498Szrj   return warned;
185*38fd1498Szrj }
186*38fd1498Szrj 
187*38fd1498Szrj /* Singular-only version of the above.  */
188*38fd1498Szrj 
189*38fd1498Szrj bool
format_warning_va(const substring_loc & fmt_loc,location_t param_loc,const char * corrected_substring,int opt,const char * gmsgid,va_list * ap)190*38fd1498Szrj format_warning_va (const substring_loc &fmt_loc,
191*38fd1498Szrj 		   location_t param_loc,
192*38fd1498Szrj 		   const char *corrected_substring,
193*38fd1498Szrj 		   int opt, const char *gmsgid, va_list *ap)
194*38fd1498Szrj {
195*38fd1498Szrj   return format_warning_n_va (fmt_loc, param_loc, corrected_substring, opt,
196*38fd1498Szrj 			      0, gmsgid, gmsgid, ap);
197*38fd1498Szrj }
198*38fd1498Szrj 
199*38fd1498Szrj /* Variadic call to format_warning_va.  */
200*38fd1498Szrj 
201*38fd1498Szrj bool
format_warning_at_substring(const substring_loc & fmt_loc,location_t param_loc,const char * corrected_substring,int opt,const char * gmsgid,...)202*38fd1498Szrj format_warning_at_substring (const substring_loc &fmt_loc,
203*38fd1498Szrj 			     location_t param_loc,
204*38fd1498Szrj 			     const char *corrected_substring,
205*38fd1498Szrj 			     int opt, const char *gmsgid, ...)
206*38fd1498Szrj {
207*38fd1498Szrj   va_list ap;
208*38fd1498Szrj   va_start (ap, gmsgid);
209*38fd1498Szrj   bool warned = format_warning_va (fmt_loc, param_loc, corrected_substring,
210*38fd1498Szrj 				   opt, gmsgid, &ap);
211*38fd1498Szrj   va_end (ap);
212*38fd1498Szrj 
213*38fd1498Szrj   return warned;
214*38fd1498Szrj }
215*38fd1498Szrj 
216*38fd1498Szrj /* Variadic call to format_warning_n_va.  */
217*38fd1498Szrj 
218*38fd1498Szrj bool
format_warning_at_substring_n(const substring_loc & fmt_loc,location_t param_loc,const char * corrected_substring,int opt,unsigned HOST_WIDE_INT n,const char * singular_gmsgid,const char * plural_gmsgid,...)219*38fd1498Szrj format_warning_at_substring_n (const substring_loc &fmt_loc,
220*38fd1498Szrj 			       location_t param_loc,
221*38fd1498Szrj 			       const char *corrected_substring,
222*38fd1498Szrj 			       int opt, unsigned HOST_WIDE_INT n,
223*38fd1498Szrj 			       const char *singular_gmsgid,
224*38fd1498Szrj 			       const char *plural_gmsgid, ...)
225*38fd1498Szrj {
226*38fd1498Szrj   va_list ap;
227*38fd1498Szrj   va_start (ap, plural_gmsgid);
228*38fd1498Szrj   bool warned = format_warning_n_va (fmt_loc, param_loc, corrected_substring,
229*38fd1498Szrj 				     opt, n, singular_gmsgid, plural_gmsgid,
230*38fd1498Szrj 				     &ap);
231*38fd1498Szrj   va_end (ap);
232*38fd1498Szrj 
233*38fd1498Szrj   return warned;
234*38fd1498Szrj }
235*38fd1498Szrj 
236*38fd1498Szrj /* Attempt to determine the source location of the substring.
237*38fd1498Szrj    If successful, return NULL and write the source location to *OUT_LOC.
238*38fd1498Szrj    Otherwise return an error message.  Error messages are intended
239*38fd1498Szrj    for GCC developers (to help debugging) rather than for end-users.  */
240*38fd1498Szrj 
241*38fd1498Szrj const char *
get_location(location_t * out_loc)242*38fd1498Szrj substring_loc::get_location (location_t *out_loc) const
243*38fd1498Szrj {
244*38fd1498Szrj   gcc_assert (out_loc);
245*38fd1498Szrj   return lang_hooks.get_substring_location (*this, out_loc);
246*38fd1498Szrj }
247