annotate gcc/gimple-ssa-sprintf.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 /* Copyright (C) 2016-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
2 Contributed by Martin Sebor <msebor@redhat.com>.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 /* This file implements the printf-return-value pass. The pass does
kono
parents:
diff changeset
21 two things: 1) it analyzes calls to formatted output functions like
kono
parents:
diff changeset
22 sprintf looking for possible buffer overflows and calls to bounded
kono
parents:
diff changeset
23 functions like snprintf for early truncation (and under the control
kono
parents:
diff changeset
24 of the -Wformat-length option issues warnings), and 2) under the
kono
parents:
diff changeset
25 control of the -fprintf-return-value option it folds the return
kono
parents:
diff changeset
26 value of safe calls into constants, making it possible to eliminate
kono
parents:
diff changeset
27 code that depends on the value of those constants.
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 For all functions (bounded or not) the pass uses the size of the
kono
parents:
diff changeset
30 destination object. That means that it will diagnose calls to
kono
parents:
diff changeset
31 snprintf not on the basis of the size specified by the function's
kono
parents:
diff changeset
32 second argument but rathger on the basis of the size the first
kono
parents:
diff changeset
33 argument points to (if possible). For bound-checking built-ins
kono
parents:
diff changeset
34 like __builtin___snprintf_chk the pass uses the size typically
kono
parents:
diff changeset
35 determined by __builtin_object_size and passed to the built-in
kono
parents:
diff changeset
36 by the Glibc inline wrapper.
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 The pass handles all forms standard sprintf format directives,
kono
parents:
diff changeset
39 including character, integer, floating point, pointer, and strings,
kono
parents:
diff changeset
40 with the standard C flags, widths, and precisions. For integers
kono
parents:
diff changeset
41 and strings it computes the length of output itself. For floating
kono
parents:
diff changeset
42 point it uses MPFR to fornmat known constants with up and down
kono
parents:
diff changeset
43 rounding and uses the resulting range of output lengths. For
kono
parents:
diff changeset
44 strings it uses the length of string literals and the sizes of
kono
parents:
diff changeset
45 character arrays that a character pointer may point to as a bound
kono
parents:
diff changeset
46 on the longest string. */
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 #include "config.h"
kono
parents:
diff changeset
49 #include "system.h"
kono
parents:
diff changeset
50 #include "coretypes.h"
kono
parents:
diff changeset
51 #include "backend.h"
kono
parents:
diff changeset
52 #include "tree.h"
kono
parents:
diff changeset
53 #include "gimple.h"
kono
parents:
diff changeset
54 #include "tree-pass.h"
kono
parents:
diff changeset
55 #include "ssa.h"
kono
parents:
diff changeset
56 #include "gimple-fold.h"
kono
parents:
diff changeset
57 #include "gimple-pretty-print.h"
kono
parents:
diff changeset
58 #include "diagnostic-core.h"
kono
parents:
diff changeset
59 #include "fold-const.h"
kono
parents:
diff changeset
60 #include "gimple-iterator.h"
kono
parents:
diff changeset
61 #include "tree-ssa.h"
kono
parents:
diff changeset
62 #include "tree-object-size.h"
kono
parents:
diff changeset
63 #include "params.h"
kono
parents:
diff changeset
64 #include "tree-cfg.h"
kono
parents:
diff changeset
65 #include "tree-ssa-propagate.h"
kono
parents:
diff changeset
66 #include "calls.h"
kono
parents:
diff changeset
67 #include "cfgloop.h"
kono
parents:
diff changeset
68 #include "intl.h"
kono
parents:
diff changeset
69 #include "langhooks.h"
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 #include "builtins.h"
kono
parents:
diff changeset
72 #include "stor-layout.h"
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 #include "realmpfr.h"
kono
parents:
diff changeset
75 #include "target.h"
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 #include "cpplib.h"
kono
parents:
diff changeset
78 #include "input.h"
kono
parents:
diff changeset
79 #include "toplev.h"
kono
parents:
diff changeset
80 #include "substring-locations.h"
kono
parents:
diff changeset
81 #include "diagnostic.h"
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
82 #include "domwalk.h"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
83 #include "alloc-pool.h"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
84 #include "vr-values.h"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
85 #include "gimple-ssa-evrp-analyze.h"
111
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 /* The likely worst case value of MB_LEN_MAX for the target, large enough
kono
parents:
diff changeset
88 for UTF-8. Ideally, this would be obtained by a target hook if it were
kono
parents:
diff changeset
89 to be used for optimization but it's good enough as is for warnings. */
kono
parents:
diff changeset
90 #define target_mb_len_max() 6
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 /* The maximum number of bytes a single non-string directive can result
kono
parents:
diff changeset
93 in. This is the result of printf("%.*Lf", INT_MAX, -LDBL_MAX) for
kono
parents:
diff changeset
94 LDBL_MAX_10_EXP of 4932. */
kono
parents:
diff changeset
95 #define IEEE_MAX_10_EXP 4932
kono
parents:
diff changeset
96 #define target_dir_max() (target_int_max () + IEEE_MAX_10_EXP + 2)
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 namespace {
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 const pass_data pass_data_sprintf_length = {
kono
parents:
diff changeset
101 GIMPLE_PASS, // pass type
kono
parents:
diff changeset
102 "printf-return-value", // pass name
kono
parents:
diff changeset
103 OPTGROUP_NONE, // optinfo_flags
kono
parents:
diff changeset
104 TV_NONE, // tv_id
kono
parents:
diff changeset
105 PROP_cfg, // properties_required
kono
parents:
diff changeset
106 0, // properties_provided
kono
parents:
diff changeset
107 0, // properties_destroyed
kono
parents:
diff changeset
108 0, // properties_start
kono
parents:
diff changeset
109 0, // properties_finish
kono
parents:
diff changeset
110 };
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 /* Set to the warning level for the current function which is equal
kono
parents:
diff changeset
113 either to warn_format_trunc for bounded functions or to
kono
parents:
diff changeset
114 warn_format_overflow otherwise. */
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 static int warn_level;
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 struct format_result;
kono
parents:
diff changeset
119
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
120 class sprintf_dom_walker : public dom_walker
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
121 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
122 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
123 sprintf_dom_walker () : dom_walker (CDI_DOMINATORS) {}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
124 ~sprintf_dom_walker () {}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
125
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
126 edge before_dom_children (basic_block) FINAL OVERRIDE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
127 void after_dom_children (basic_block) FINAL OVERRIDE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
128 bool handle_gimple_call (gimple_stmt_iterator *);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
129
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
130 struct call_info;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
131 bool compute_format_length (call_info &, format_result *);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
132 class evrp_range_analyzer evrp_range_analyzer;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
133 };
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
134
111
kono
parents:
diff changeset
135 class pass_sprintf_length : public gimple_opt_pass
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 bool fold_return_value;
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 public:
kono
parents:
diff changeset
140 pass_sprintf_length (gcc::context *ctxt)
kono
parents:
diff changeset
141 : gimple_opt_pass (pass_data_sprintf_length, ctxt),
kono
parents:
diff changeset
142 fold_return_value (false)
kono
parents:
diff changeset
143 { }
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 opt_pass * clone () { return new pass_sprintf_length (m_ctxt); }
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 virtual bool gate (function *);
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 virtual unsigned int execute (function *);
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 void set_pass_param (unsigned int n, bool param)
kono
parents:
diff changeset
152 {
kono
parents:
diff changeset
153 gcc_assert (n == 0);
kono
parents:
diff changeset
154 fold_return_value = param;
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 };
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 bool
kono
parents:
diff changeset
160 pass_sprintf_length::gate (function *)
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 /* Run the pass iff -Warn-format-overflow or -Warn-format-truncation
kono
parents:
diff changeset
163 is specified and either not optimizing and the pass is being invoked
kono
parents:
diff changeset
164 early, or when optimizing and the pass is being invoked during
kono
parents:
diff changeset
165 optimization (i.e., "late"). */
kono
parents:
diff changeset
166 return ((warn_format_overflow > 0
kono
parents:
diff changeset
167 || warn_format_trunc > 0
kono
parents:
diff changeset
168 || flag_printf_return_value)
kono
parents:
diff changeset
169 && (optimize > 0) == fold_return_value);
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 /* The minimum, maximum, likely, and unlikely maximum number of bytes
kono
parents:
diff changeset
173 of output either a formatting function or an individual directive
kono
parents:
diff changeset
174 can result in. */
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 struct result_range
kono
parents:
diff changeset
177 {
kono
parents:
diff changeset
178 /* The absolute minimum number of bytes. The result of a successful
kono
parents:
diff changeset
179 conversion is guaranteed to be no less than this. (An erroneous
kono
parents:
diff changeset
180 conversion can be indicated by MIN > HOST_WIDE_INT_MAX.) */
kono
parents:
diff changeset
181 unsigned HOST_WIDE_INT min;
kono
parents:
diff changeset
182 /* The likely maximum result that is used in diagnostics. In most
kono
parents:
diff changeset
183 cases MAX is the same as the worst case UNLIKELY result. */
kono
parents:
diff changeset
184 unsigned HOST_WIDE_INT max;
kono
parents:
diff changeset
185 /* The likely result used to trigger diagnostics. For conversions
kono
parents:
diff changeset
186 that result in a range of bytes [MIN, MAX], LIKELY is somewhere
kono
parents:
diff changeset
187 in that range. */
kono
parents:
diff changeset
188 unsigned HOST_WIDE_INT likely;
kono
parents:
diff changeset
189 /* In rare cases (e.g., for nultibyte characters) UNLIKELY gives
kono
parents:
diff changeset
190 the worst cases maximum result of a directive. In most cases
kono
parents:
diff changeset
191 UNLIKELY == MAX. UNLIKELY is used to control the return value
kono
parents:
diff changeset
192 optimization but not in diagnostics. */
kono
parents:
diff changeset
193 unsigned HOST_WIDE_INT unlikely;
kono
parents:
diff changeset
194 };
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 /* The result of a call to a formatted function. */
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 struct format_result
kono
parents:
diff changeset
199 {
kono
parents:
diff changeset
200 /* Range of characters written by the formatted function.
kono
parents:
diff changeset
201 Setting the minimum to HOST_WIDE_INT_MAX disables all
kono
parents:
diff changeset
202 length tracking for the remainder of the format string. */
kono
parents:
diff changeset
203 result_range range;
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 /* True when the range above is obtained from known values of
kono
parents:
diff changeset
206 directive arguments, or bounds on the amount of output such
kono
parents:
diff changeset
207 as width and precision, and not the result of heuristics that
kono
parents:
diff changeset
208 depend on warning levels. It's used to issue stricter diagnostics
kono
parents:
diff changeset
209 in cases where strings of unknown lengths are bounded by the arrays
kono
parents:
diff changeset
210 they are determined to refer to. KNOWNRANGE must not be used for
kono
parents:
diff changeset
211 the return value optimization. */
kono
parents:
diff changeset
212 bool knownrange;
kono
parents:
diff changeset
213
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
214 /* True if no individual directive could fail or result in more than
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
215 4095 bytes of output (the total NUMBER_CHARS_{MIN,MAX} might be
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
216 greater). Implementations are not required to handle directives
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
217 that produce more than 4K bytes (leading to undefined behavior)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
218 and so when one is found it disables the return value optimization.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
219 Similarly, directives that can fail (such as wide character
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
220 directives) disable the optimization. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
221 bool posunder4k;
111
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 /* True when a floating point directive has been seen in the format
kono
parents:
diff changeset
224 string. */
kono
parents:
diff changeset
225 bool floating;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 /* True when an intermediate result has caused a warning. Used to
kono
parents:
diff changeset
228 avoid issuing duplicate warnings while finishing the processing
kono
parents:
diff changeset
229 of a call. WARNED also disables the return value optimization. */
kono
parents:
diff changeset
230 bool warned;
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 /* Preincrement the number of output characters by 1. */
kono
parents:
diff changeset
233 format_result& operator++ ()
kono
parents:
diff changeset
234 {
kono
parents:
diff changeset
235 return *this += 1;
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 /* Postincrement the number of output characters by 1. */
kono
parents:
diff changeset
239 format_result operator++ (int)
kono
parents:
diff changeset
240 {
kono
parents:
diff changeset
241 format_result prev (*this);
kono
parents:
diff changeset
242 *this += 1;
kono
parents:
diff changeset
243 return prev;
kono
parents:
diff changeset
244 }
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 /* Increment the number of output characters by N. */
kono
parents:
diff changeset
247 format_result& operator+= (unsigned HOST_WIDE_INT);
kono
parents:
diff changeset
248 };
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 format_result&
kono
parents:
diff changeset
251 format_result::operator+= (unsigned HOST_WIDE_INT n)
kono
parents:
diff changeset
252 {
kono
parents:
diff changeset
253 gcc_assert (n < HOST_WIDE_INT_MAX);
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 if (range.min < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
256 range.min += n;
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 if (range.max < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
259 range.max += n;
kono
parents:
diff changeset
260
kono
parents:
diff changeset
261 if (range.likely < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
262 range.likely += n;
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 if (range.unlikely < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
265 range.unlikely += n;
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 return *this;
kono
parents:
diff changeset
268 }
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 /* Return the value of INT_MIN for the target. */
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 static inline HOST_WIDE_INT
kono
parents:
diff changeset
273 target_int_min ()
kono
parents:
diff changeset
274 {
kono
parents:
diff changeset
275 return tree_to_shwi (TYPE_MIN_VALUE (integer_type_node));
kono
parents:
diff changeset
276 }
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 /* Return the value of INT_MAX for the target. */
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 static inline unsigned HOST_WIDE_INT
kono
parents:
diff changeset
281 target_int_max ()
kono
parents:
diff changeset
282 {
kono
parents:
diff changeset
283 return tree_to_uhwi (TYPE_MAX_VALUE (integer_type_node));
kono
parents:
diff changeset
284 }
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 /* Return the value of SIZE_MAX for the target. */
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 static inline unsigned HOST_WIDE_INT
kono
parents:
diff changeset
289 target_size_max ()
kono
parents:
diff changeset
290 {
kono
parents:
diff changeset
291 return tree_to_uhwi (TYPE_MAX_VALUE (size_type_node));
kono
parents:
diff changeset
292 }
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 /* A straightforward mapping from the execution character set to the host
kono
parents:
diff changeset
295 character set indexed by execution character. */
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 static char target_to_host_charmap[256];
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 /* Initialize a mapping from the execution character set to the host
kono
parents:
diff changeset
300 character set. */
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 static bool
kono
parents:
diff changeset
303 init_target_to_host_charmap ()
kono
parents:
diff changeset
304 {
kono
parents:
diff changeset
305 /* If the percent sign is non-zero the mapping has already been
kono
parents:
diff changeset
306 initialized. */
kono
parents:
diff changeset
307 if (target_to_host_charmap['%'])
kono
parents:
diff changeset
308 return true;
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 /* Initialize the target_percent character (done elsewhere). */
kono
parents:
diff changeset
311 if (!init_target_chars ())
kono
parents:
diff changeset
312 return false;
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 /* The subset of the source character set used by printf conversion
kono
parents:
diff changeset
315 specifications (strictly speaking, not all letters are used but
kono
parents:
diff changeset
316 they are included here for the sake of simplicity). The dollar
kono
parents:
diff changeset
317 sign must be included even though it's not in the basic source
kono
parents:
diff changeset
318 character set. */
kono
parents:
diff changeset
319 const char srcset[] = " 0123456789!\"#%&'()*+,-./:;<=>?[\\]^_{|}~$"
kono
parents:
diff changeset
320 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 /* Set the mapping for all characters to some ordinary value (i,e.,
kono
parents:
diff changeset
323 not none used in printf conversion specifications) and overwrite
kono
parents:
diff changeset
324 those that are used by conversion specifications with their
kono
parents:
diff changeset
325 corresponding values. */
kono
parents:
diff changeset
326 memset (target_to_host_charmap + 1, '?', sizeof target_to_host_charmap - 1);
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 /* Are the two sets of characters the same? */
kono
parents:
diff changeset
329 bool all_same_p = true;
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 for (const char *pc = srcset; *pc; ++pc)
kono
parents:
diff changeset
332 {
kono
parents:
diff changeset
333 /* Slice off the high end bits in case target characters are
kono
parents:
diff changeset
334 signed. All values are expected to be non-nul, otherwise
kono
parents:
diff changeset
335 there's a problem. */
kono
parents:
diff changeset
336 if (unsigned char tc = lang_hooks.to_target_charset (*pc))
kono
parents:
diff changeset
337 {
kono
parents:
diff changeset
338 target_to_host_charmap[tc] = *pc;
kono
parents:
diff changeset
339 if (tc != *pc)
kono
parents:
diff changeset
340 all_same_p = false;
kono
parents:
diff changeset
341 }
kono
parents:
diff changeset
342 else
kono
parents:
diff changeset
343 return false;
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 }
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 /* Set the first element to a non-zero value if the mapping
kono
parents:
diff changeset
348 is 1-to-1, otherwise leave it clear (NUL is assumed to be
kono
parents:
diff changeset
349 the same in both character sets). */
kono
parents:
diff changeset
350 target_to_host_charmap[0] = all_same_p;
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 return true;
kono
parents:
diff changeset
353 }
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 /* Return the host source character corresponding to the character
kono
parents:
diff changeset
356 CH in the execution character set if one exists, or some innocuous
kono
parents:
diff changeset
357 (non-special, non-nul) source character otherwise. */
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 static inline unsigned char
kono
parents:
diff changeset
360 target_to_host (unsigned char ch)
kono
parents:
diff changeset
361 {
kono
parents:
diff changeset
362 return target_to_host_charmap[ch];
kono
parents:
diff changeset
363 }
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 /* Convert an initial substring of the string TARGSTR consisting of
kono
parents:
diff changeset
366 characters in the execution character set into a string in the
kono
parents:
diff changeset
367 source character set on the host and store up to HOSTSZ characters
kono
parents:
diff changeset
368 in the buffer pointed to by HOSTR. Return HOSTR. */
kono
parents:
diff changeset
369
kono
parents:
diff changeset
370 static const char*
kono
parents:
diff changeset
371 target_to_host (char *hostr, size_t hostsz, const char *targstr)
kono
parents:
diff changeset
372 {
kono
parents:
diff changeset
373 /* Make sure the buffer is reasonably big. */
kono
parents:
diff changeset
374 gcc_assert (hostsz > 4);
kono
parents:
diff changeset
375
kono
parents:
diff changeset
376 /* The interesting subset of source and execution characters are
kono
parents:
diff changeset
377 the same so no conversion is necessary. However, truncate
kono
parents:
diff changeset
378 overlong strings just like the translated strings are. */
kono
parents:
diff changeset
379 if (target_to_host_charmap['\0'] == 1)
kono
parents:
diff changeset
380 {
kono
parents:
diff changeset
381 strncpy (hostr, targstr, hostsz - 4);
kono
parents:
diff changeset
382 if (strlen (targstr) >= hostsz)
kono
parents:
diff changeset
383 strcpy (hostr + hostsz - 4, "...");
kono
parents:
diff changeset
384 return hostr;
kono
parents:
diff changeset
385 }
kono
parents:
diff changeset
386
kono
parents:
diff changeset
387 /* Convert the initial substring of TARGSTR to the corresponding
kono
parents:
diff changeset
388 characters in the host set, appending "..." if TARGSTR is too
kono
parents:
diff changeset
389 long to fit. Using the static buffer assumes the function is
kono
parents:
diff changeset
390 not called in between sequence points (which it isn't). */
kono
parents:
diff changeset
391 for (char *ph = hostr; ; ++targstr)
kono
parents:
diff changeset
392 {
kono
parents:
diff changeset
393 *ph++ = target_to_host (*targstr);
kono
parents:
diff changeset
394 if (!*targstr)
kono
parents:
diff changeset
395 break;
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 if (size_t (ph - hostr) == hostsz - 4)
kono
parents:
diff changeset
398 {
kono
parents:
diff changeset
399 *ph = '\0';
kono
parents:
diff changeset
400 strcat (ph, "...");
kono
parents:
diff changeset
401 break;
kono
parents:
diff changeset
402 }
kono
parents:
diff changeset
403 }
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 return hostr;
kono
parents:
diff changeset
406 }
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 /* Convert the sequence of decimal digits in the execution character
kono
parents:
diff changeset
409 starting at S to a long, just like strtol does. Return the result
kono
parents:
diff changeset
410 and set *END to one past the last converted character. On range
kono
parents:
diff changeset
411 error set ERANGE to the digit that caused it. */
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413 static inline long
kono
parents:
diff changeset
414 target_strtol10 (const char **ps, const char **erange)
kono
parents:
diff changeset
415 {
kono
parents:
diff changeset
416 unsigned HOST_WIDE_INT val = 0;
kono
parents:
diff changeset
417 for ( ; ; ++*ps)
kono
parents:
diff changeset
418 {
kono
parents:
diff changeset
419 unsigned char c = target_to_host (**ps);
kono
parents:
diff changeset
420 if (ISDIGIT (c))
kono
parents:
diff changeset
421 {
kono
parents:
diff changeset
422 c -= '0';
kono
parents:
diff changeset
423
kono
parents:
diff changeset
424 /* Check for overflow. */
kono
parents:
diff changeset
425 if (val > (LONG_MAX - c) / 10LU)
kono
parents:
diff changeset
426 {
kono
parents:
diff changeset
427 val = LONG_MAX;
kono
parents:
diff changeset
428 *erange = *ps;
kono
parents:
diff changeset
429
kono
parents:
diff changeset
430 /* Skip the remaining digits. */
kono
parents:
diff changeset
431 do
kono
parents:
diff changeset
432 c = target_to_host (*++*ps);
kono
parents:
diff changeset
433 while (ISDIGIT (c));
kono
parents:
diff changeset
434 break;
kono
parents:
diff changeset
435 }
kono
parents:
diff changeset
436 else
kono
parents:
diff changeset
437 val = val * 10 + c;
kono
parents:
diff changeset
438 }
kono
parents:
diff changeset
439 else
kono
parents:
diff changeset
440 break;
kono
parents:
diff changeset
441 }
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 return val;
kono
parents:
diff changeset
444 }
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 /* Given FORMAT, set *PLOC to the source location of the format string
kono
parents:
diff changeset
447 and return the format string if it is known or null otherwise. */
kono
parents:
diff changeset
448
kono
parents:
diff changeset
449 static const char*
kono
parents:
diff changeset
450 get_format_string (tree format, location_t *ploc)
kono
parents:
diff changeset
451 {
kono
parents:
diff changeset
452 *ploc = EXPR_LOC_OR_LOC (format, input_location);
kono
parents:
diff changeset
453
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
454 return c_getstr (format);
111
kono
parents:
diff changeset
455 }
kono
parents:
diff changeset
456
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
457 /* For convenience and brevity, shorter named entrypoints of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
458 format_string_diagnostic_t::emit_warning_va and
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
459 format_string_diagnostic_t::emit_warning_n_va.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
460 These have to be functions with the attribute so that exgettext
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
461 works properly. */
111
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 static bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
464 ATTRIBUTE_GCC_DIAG (5, 6)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
465 fmtwarn (const substring_loc &fmt_loc, location_t param_loc,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
466 const char *corrected_substring, int opt, const char *gmsgid, ...)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
467 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
468 format_string_diagnostic_t diag (fmt_loc, NULL, param_loc, NULL,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
469 corrected_substring);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
470 va_list ap;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
471 va_start (ap, gmsgid);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
472 bool warned = diag.emit_warning_va (opt, gmsgid, &ap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
473 va_end (ap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
474
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
475 return warned;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
476 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
477
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
478 static bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
479 ATTRIBUTE_GCC_DIAG (6, 8) ATTRIBUTE_GCC_DIAG (7, 8)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
480 fmtwarn_n (const substring_loc &fmt_loc, location_t param_loc,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
481 const char *corrected_substring, int opt, unsigned HOST_WIDE_INT n,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
482 const char *singular_gmsgid, const char *plural_gmsgid, ...)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
483 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
484 format_string_diagnostic_t diag (fmt_loc, NULL, param_loc, NULL,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
485 corrected_substring);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
486 va_list ap;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
487 va_start (ap, plural_gmsgid);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
488 bool warned = diag.emit_warning_n_va (opt, n, singular_gmsgid, plural_gmsgid,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
489 &ap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
490 va_end (ap);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
491
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
492 return warned;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
493 }
111
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 /* Format length modifiers. */
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 enum format_lengths
kono
parents:
diff changeset
498 {
kono
parents:
diff changeset
499 FMT_LEN_none,
kono
parents:
diff changeset
500 FMT_LEN_hh, // char argument
kono
parents:
diff changeset
501 FMT_LEN_h, // short
kono
parents:
diff changeset
502 FMT_LEN_l, // long
kono
parents:
diff changeset
503 FMT_LEN_ll, // long long
kono
parents:
diff changeset
504 FMT_LEN_L, // long double (and GNU long long)
kono
parents:
diff changeset
505 FMT_LEN_z, // size_t
kono
parents:
diff changeset
506 FMT_LEN_t, // ptrdiff_t
kono
parents:
diff changeset
507 FMT_LEN_j // intmax_t
kono
parents:
diff changeset
508 };
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 /* Description of the result of conversion either of a single directive
kono
parents:
diff changeset
512 or the whole format string. */
kono
parents:
diff changeset
513
kono
parents:
diff changeset
514 struct fmtresult
kono
parents:
diff changeset
515 {
kono
parents:
diff changeset
516 /* Construct a FMTRESULT object with all counters initialized
kono
parents:
diff changeset
517 to MIN. KNOWNRANGE is set when MIN is valid. */
kono
parents:
diff changeset
518 fmtresult (unsigned HOST_WIDE_INT min = HOST_WIDE_INT_MAX)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
519 : argmin (), argmax (), nonstr (),
111
kono
parents:
diff changeset
520 knownrange (min < HOST_WIDE_INT_MAX),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
521 mayfail (), nullp ()
111
kono
parents:
diff changeset
522 {
kono
parents:
diff changeset
523 range.min = min;
kono
parents:
diff changeset
524 range.max = min;
kono
parents:
diff changeset
525 range.likely = min;
kono
parents:
diff changeset
526 range.unlikely = min;
kono
parents:
diff changeset
527 }
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 /* Construct a FMTRESULT object with MIN, MAX, and LIKELY counters.
kono
parents:
diff changeset
530 KNOWNRANGE is set when both MIN and MAX are valid. */
kono
parents:
diff changeset
531 fmtresult (unsigned HOST_WIDE_INT min, unsigned HOST_WIDE_INT max,
kono
parents:
diff changeset
532 unsigned HOST_WIDE_INT likely = HOST_WIDE_INT_MAX)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
533 : argmin (), argmax (), nonstr (),
111
kono
parents:
diff changeset
534 knownrange (min < HOST_WIDE_INT_MAX && max < HOST_WIDE_INT_MAX),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
535 mayfail (), nullp ()
111
kono
parents:
diff changeset
536 {
kono
parents:
diff changeset
537 range.min = min;
kono
parents:
diff changeset
538 range.max = max;
kono
parents:
diff changeset
539 range.likely = max < likely ? min : likely;
kono
parents:
diff changeset
540 range.unlikely = max;
kono
parents:
diff changeset
541 }
kono
parents:
diff changeset
542
kono
parents:
diff changeset
543 /* Adjust result upward to reflect the RANGE of values the specified
kono
parents:
diff changeset
544 width or precision is known to be in. */
kono
parents:
diff changeset
545 fmtresult& adjust_for_width_or_precision (const HOST_WIDE_INT[2],
kono
parents:
diff changeset
546 tree = NULL_TREE,
kono
parents:
diff changeset
547 unsigned = 0, unsigned = 0);
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 /* Return the maximum number of decimal digits a value of TYPE
kono
parents:
diff changeset
550 formats as on output. */
kono
parents:
diff changeset
551 static unsigned type_max_digits (tree, int);
kono
parents:
diff changeset
552
kono
parents:
diff changeset
553 /* The range a directive's argument is in. */
kono
parents:
diff changeset
554 tree argmin, argmax;
kono
parents:
diff changeset
555
kono
parents:
diff changeset
556 /* The minimum and maximum number of bytes that a directive
kono
parents:
diff changeset
557 results in on output for an argument in the range above. */
kono
parents:
diff changeset
558 result_range range;
kono
parents:
diff changeset
559
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
560 /* Non-nul when the argument of a string directive is not a nul
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
561 terminated string. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
562 tree nonstr;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
563
111
kono
parents:
diff changeset
564 /* True when the range above is obtained from a known value of
kono
parents:
diff changeset
565 a directive's argument or its bounds and not the result of
kono
parents:
diff changeset
566 heuristics that depend on warning levels. */
kono
parents:
diff changeset
567 bool knownrange;
kono
parents:
diff changeset
568
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
569 /* True for a directive that may fail (such as wide character
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
570 directives). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
571 bool mayfail;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
572
111
kono
parents:
diff changeset
573 /* True when the argument is a null pointer. */
kono
parents:
diff changeset
574 bool nullp;
kono
parents:
diff changeset
575 };
kono
parents:
diff changeset
576
kono
parents:
diff changeset
577 /* Adjust result upward to reflect the range ADJUST of values the
kono
parents:
diff changeset
578 specified width or precision is known to be in. When non-null,
kono
parents:
diff changeset
579 TYPE denotes the type of the directive whose result is being
kono
parents:
diff changeset
580 adjusted, BASE gives the base of the directive (octal, decimal,
kono
parents:
diff changeset
581 or hex), and ADJ denotes the additional adjustment to the LIKELY
kono
parents:
diff changeset
582 counter that may need to be added when ADJUST is a range. */
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 fmtresult&
kono
parents:
diff changeset
585 fmtresult::adjust_for_width_or_precision (const HOST_WIDE_INT adjust[2],
kono
parents:
diff changeset
586 tree type /* = NULL_TREE */,
kono
parents:
diff changeset
587 unsigned base /* = 0 */,
kono
parents:
diff changeset
588 unsigned adj /* = 0 */)
kono
parents:
diff changeset
589 {
kono
parents:
diff changeset
590 bool minadjusted = false;
kono
parents:
diff changeset
591
kono
parents:
diff changeset
592 /* Adjust the minimum and likely counters. */
kono
parents:
diff changeset
593 if (adjust[0] >= 0)
kono
parents:
diff changeset
594 {
kono
parents:
diff changeset
595 if (range.min < (unsigned HOST_WIDE_INT)adjust[0])
kono
parents:
diff changeset
596 {
kono
parents:
diff changeset
597 range.min = adjust[0];
kono
parents:
diff changeset
598 minadjusted = true;
kono
parents:
diff changeset
599 }
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 /* Adjust the likely counter. */
kono
parents:
diff changeset
602 if (range.likely < range.min)
kono
parents:
diff changeset
603 range.likely = range.min;
kono
parents:
diff changeset
604 }
kono
parents:
diff changeset
605 else if (adjust[0] == target_int_min ()
kono
parents:
diff changeset
606 && (unsigned HOST_WIDE_INT)adjust[1] == target_int_max ())
kono
parents:
diff changeset
607 knownrange = false;
kono
parents:
diff changeset
608
kono
parents:
diff changeset
609 /* Adjust the maximum counter. */
kono
parents:
diff changeset
610 if (adjust[1] > 0)
kono
parents:
diff changeset
611 {
kono
parents:
diff changeset
612 if (range.max < (unsigned HOST_WIDE_INT)adjust[1])
kono
parents:
diff changeset
613 {
kono
parents:
diff changeset
614 range.max = adjust[1];
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 /* Set KNOWNRANGE if both the minimum and maximum have been
kono
parents:
diff changeset
617 adjusted. Otherwise leave it at what it was before. */
kono
parents:
diff changeset
618 knownrange = minadjusted;
kono
parents:
diff changeset
619 }
kono
parents:
diff changeset
620 }
kono
parents:
diff changeset
621
kono
parents:
diff changeset
622 if (warn_level > 1 && type)
kono
parents:
diff changeset
623 {
kono
parents:
diff changeset
624 /* For large non-constant width or precision whose range spans
kono
parents:
diff changeset
625 the maximum number of digits produced by the directive for
kono
parents:
diff changeset
626 any argument, set the likely number of bytes to be at most
kono
parents:
diff changeset
627 the number digits plus other adjustment determined by the
kono
parents:
diff changeset
628 caller (one for sign or two for the hexadecimal "0x"
kono
parents:
diff changeset
629 prefix). */
kono
parents:
diff changeset
630 unsigned dirdigs = type_max_digits (type, base);
kono
parents:
diff changeset
631 if (adjust[0] < dirdigs && dirdigs < adjust[1]
kono
parents:
diff changeset
632 && range.likely < dirdigs)
kono
parents:
diff changeset
633 range.likely = dirdigs + adj;
kono
parents:
diff changeset
634 }
kono
parents:
diff changeset
635 else if (range.likely < (range.min ? range.min : 1))
kono
parents:
diff changeset
636 {
kono
parents:
diff changeset
637 /* Conservatively, set LIKELY to at least MIN but no less than
kono
parents:
diff changeset
638 1 unless MAX is zero. */
kono
parents:
diff changeset
639 range.likely = (range.min
kono
parents:
diff changeset
640 ? range.min
kono
parents:
diff changeset
641 : range.max && (range.max < HOST_WIDE_INT_MAX
kono
parents:
diff changeset
642 || warn_level > 1) ? 1 : 0);
kono
parents:
diff changeset
643 }
kono
parents:
diff changeset
644
kono
parents:
diff changeset
645 /* Finally adjust the unlikely counter to be at least as large as
kono
parents:
diff changeset
646 the maximum. */
kono
parents:
diff changeset
647 if (range.unlikely < range.max)
kono
parents:
diff changeset
648 range.unlikely = range.max;
kono
parents:
diff changeset
649
kono
parents:
diff changeset
650 return *this;
kono
parents:
diff changeset
651 }
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 /* Return the maximum number of digits a value of TYPE formats in
kono
parents:
diff changeset
654 BASE on output, not counting base prefix . */
kono
parents:
diff changeset
655
kono
parents:
diff changeset
656 unsigned
kono
parents:
diff changeset
657 fmtresult::type_max_digits (tree type, int base)
kono
parents:
diff changeset
658 {
kono
parents:
diff changeset
659 unsigned prec = TYPE_PRECISION (type);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
660 switch (base)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
661 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
662 case 8:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
663 return (prec + 2) / 3;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
664 case 10:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
665 /* Decimal approximation: yields 3, 5, 10, and 20 for precision
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
666 of 8, 16, 32, and 64 bits. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
667 return prec * 301 / 1000 + 1;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
668 case 16:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
669 return prec / 4;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
670 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
671
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
672 gcc_unreachable ();
111
kono
parents:
diff changeset
673 }
kono
parents:
diff changeset
674
kono
parents:
diff changeset
675 static bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
676 get_int_range (tree, HOST_WIDE_INT *, HOST_WIDE_INT *, bool, HOST_WIDE_INT,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
677 class vr_values *vr_values);
111
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 /* Description of a format directive. A directive is either a plain
kono
parents:
diff changeset
680 string or a conversion specification that starts with '%'. */
kono
parents:
diff changeset
681
kono
parents:
diff changeset
682 struct directive
kono
parents:
diff changeset
683 {
kono
parents:
diff changeset
684 /* The 1-based directive number (for debugging). */
kono
parents:
diff changeset
685 unsigned dirno;
kono
parents:
diff changeset
686
kono
parents:
diff changeset
687 /* The first character of the directive and its length. */
kono
parents:
diff changeset
688 const char *beg;
kono
parents:
diff changeset
689 size_t len;
kono
parents:
diff changeset
690
kono
parents:
diff changeset
691 /* A bitmap of flags, one for each character. */
kono
parents:
diff changeset
692 unsigned flags[256 / sizeof (int)];
kono
parents:
diff changeset
693
kono
parents:
diff changeset
694 /* The range of values of the specified width, or -1 if not specified. */
kono
parents:
diff changeset
695 HOST_WIDE_INT width[2];
kono
parents:
diff changeset
696 /* The range of values of the specified precision, or -1 if not
kono
parents:
diff changeset
697 specified. */
kono
parents:
diff changeset
698 HOST_WIDE_INT prec[2];
kono
parents:
diff changeset
699
kono
parents:
diff changeset
700 /* Length modifier. */
kono
parents:
diff changeset
701 format_lengths modifier;
kono
parents:
diff changeset
702
kono
parents:
diff changeset
703 /* Format specifier character. */
kono
parents:
diff changeset
704 char specifier;
kono
parents:
diff changeset
705
kono
parents:
diff changeset
706 /* The argument of the directive or null when the directive doesn't
kono
parents:
diff changeset
707 take one or when none is available (such as for vararg functions). */
kono
parents:
diff changeset
708 tree arg;
kono
parents:
diff changeset
709
kono
parents:
diff changeset
710 /* Format conversion function that given a directive and an argument
kono
parents:
diff changeset
711 returns the formatting result. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
712 fmtresult (*fmtfunc) (const directive &, tree, vr_values *);
111
kono
parents:
diff changeset
713
kono
parents:
diff changeset
714 /* Return True when a the format flag CHR has been used. */
kono
parents:
diff changeset
715 bool get_flag (char chr) const
kono
parents:
diff changeset
716 {
kono
parents:
diff changeset
717 unsigned char c = chr & 0xff;
kono
parents:
diff changeset
718 return (flags[c / (CHAR_BIT * sizeof *flags)]
kono
parents:
diff changeset
719 & (1U << (c % (CHAR_BIT * sizeof *flags))));
kono
parents:
diff changeset
720 }
kono
parents:
diff changeset
721
kono
parents:
diff changeset
722 /* Make a record of the format flag CHR having been used. */
kono
parents:
diff changeset
723 void set_flag (char chr)
kono
parents:
diff changeset
724 {
kono
parents:
diff changeset
725 unsigned char c = chr & 0xff;
kono
parents:
diff changeset
726 flags[c / (CHAR_BIT * sizeof *flags)]
kono
parents:
diff changeset
727 |= (1U << (c % (CHAR_BIT * sizeof *flags)));
kono
parents:
diff changeset
728 }
kono
parents:
diff changeset
729
kono
parents:
diff changeset
730 /* Reset the format flag CHR. */
kono
parents:
diff changeset
731 void clear_flag (char chr)
kono
parents:
diff changeset
732 {
kono
parents:
diff changeset
733 unsigned char c = chr & 0xff;
kono
parents:
diff changeset
734 flags[c / (CHAR_BIT * sizeof *flags)]
kono
parents:
diff changeset
735 &= ~(1U << (c % (CHAR_BIT * sizeof *flags)));
kono
parents:
diff changeset
736 }
kono
parents:
diff changeset
737
kono
parents:
diff changeset
738 /* Set both bounds of the width range to VAL. */
kono
parents:
diff changeset
739 void set_width (HOST_WIDE_INT val)
kono
parents:
diff changeset
740 {
kono
parents:
diff changeset
741 width[0] = width[1] = val;
kono
parents:
diff changeset
742 }
kono
parents:
diff changeset
743
kono
parents:
diff changeset
744 /* Set the width range according to ARG, with both bounds being
kono
parents:
diff changeset
745 no less than 0. For a constant ARG set both bounds to its value
kono
parents:
diff changeset
746 or 0, whichever is greater. For a non-constant ARG in some range
kono
parents:
diff changeset
747 set width to its range adjusting each bound to -1 if it's less.
kono
parents:
diff changeset
748 For an indeterminate ARG set width to [0, INT_MAX]. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
749 void set_width (tree arg, vr_values *vr_values)
111
kono
parents:
diff changeset
750 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
751 get_int_range (arg, width, width + 1, true, 0, vr_values);
111
kono
parents:
diff changeset
752 }
kono
parents:
diff changeset
753
kono
parents:
diff changeset
754 /* Set both bounds of the precision range to VAL. */
kono
parents:
diff changeset
755 void set_precision (HOST_WIDE_INT val)
kono
parents:
diff changeset
756 {
kono
parents:
diff changeset
757 prec[0] = prec[1] = val;
kono
parents:
diff changeset
758 }
kono
parents:
diff changeset
759
kono
parents:
diff changeset
760 /* Set the precision range according to ARG, with both bounds being
kono
parents:
diff changeset
761 no less than -1. For a constant ARG set both bounds to its value
kono
parents:
diff changeset
762 or -1 whichever is greater. For a non-constant ARG in some range
kono
parents:
diff changeset
763 set precision to its range adjusting each bound to -1 if it's less.
kono
parents:
diff changeset
764 For an indeterminate ARG set precision to [-1, INT_MAX]. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
765 void set_precision (tree arg, vr_values *vr_values)
111
kono
parents:
diff changeset
766 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
767 get_int_range (arg, prec, prec + 1, false, -1, vr_values);
111
kono
parents:
diff changeset
768 }
kono
parents:
diff changeset
769
kono
parents:
diff changeset
770 /* Return true if both width and precision are known to be
kono
parents:
diff changeset
771 either constant or in some range, false otherwise. */
kono
parents:
diff changeset
772 bool known_width_and_precision () const
kono
parents:
diff changeset
773 {
kono
parents:
diff changeset
774 return ((width[1] < 0
kono
parents:
diff changeset
775 || (unsigned HOST_WIDE_INT)width[1] <= target_int_max ())
kono
parents:
diff changeset
776 && (prec[1] < 0
kono
parents:
diff changeset
777 || (unsigned HOST_WIDE_INT)prec[1] < target_int_max ()));
kono
parents:
diff changeset
778 }
kono
parents:
diff changeset
779 };
kono
parents:
diff changeset
780
kono
parents:
diff changeset
781 /* Return the logarithm of X in BASE. */
kono
parents:
diff changeset
782
kono
parents:
diff changeset
783 static int
kono
parents:
diff changeset
784 ilog (unsigned HOST_WIDE_INT x, int base)
kono
parents:
diff changeset
785 {
kono
parents:
diff changeset
786 int res = 0;
kono
parents:
diff changeset
787 do
kono
parents:
diff changeset
788 {
kono
parents:
diff changeset
789 ++res;
kono
parents:
diff changeset
790 x /= base;
kono
parents:
diff changeset
791 } while (x);
kono
parents:
diff changeset
792 return res;
kono
parents:
diff changeset
793 }
kono
parents:
diff changeset
794
kono
parents:
diff changeset
795 /* Return the number of bytes resulting from converting into a string
kono
parents:
diff changeset
796 the INTEGER_CST tree node X in BASE with a minimum of PREC digits.
kono
parents:
diff changeset
797 PLUS indicates whether 1 for a plus sign should be added for positive
kono
parents:
diff changeset
798 numbers, and PREFIX whether the length of an octal ('O') or hexadecimal
kono
parents:
diff changeset
799 ('0x') prefix should be added for nonzero numbers. Return -1 if X cannot
kono
parents:
diff changeset
800 be represented. */
kono
parents:
diff changeset
801
kono
parents:
diff changeset
802 static HOST_WIDE_INT
kono
parents:
diff changeset
803 tree_digits (tree x, int base, HOST_WIDE_INT prec, bool plus, bool prefix)
kono
parents:
diff changeset
804 {
kono
parents:
diff changeset
805 unsigned HOST_WIDE_INT absval;
kono
parents:
diff changeset
806
kono
parents:
diff changeset
807 HOST_WIDE_INT res;
kono
parents:
diff changeset
808
kono
parents:
diff changeset
809 if (TYPE_UNSIGNED (TREE_TYPE (x)))
kono
parents:
diff changeset
810 {
kono
parents:
diff changeset
811 if (tree_fits_uhwi_p (x))
kono
parents:
diff changeset
812 {
kono
parents:
diff changeset
813 absval = tree_to_uhwi (x);
kono
parents:
diff changeset
814 res = plus;
kono
parents:
diff changeset
815 }
kono
parents:
diff changeset
816 else
kono
parents:
diff changeset
817 return -1;
kono
parents:
diff changeset
818 }
kono
parents:
diff changeset
819 else
kono
parents:
diff changeset
820 {
kono
parents:
diff changeset
821 if (tree_fits_shwi_p (x))
kono
parents:
diff changeset
822 {
kono
parents:
diff changeset
823 HOST_WIDE_INT i = tree_to_shwi (x);
kono
parents:
diff changeset
824 if (HOST_WIDE_INT_MIN == i)
kono
parents:
diff changeset
825 {
kono
parents:
diff changeset
826 /* Avoid undefined behavior due to negating a minimum. */
kono
parents:
diff changeset
827 absval = HOST_WIDE_INT_MAX;
kono
parents:
diff changeset
828 res = 1;
kono
parents:
diff changeset
829 }
kono
parents:
diff changeset
830 else if (i < 0)
kono
parents:
diff changeset
831 {
kono
parents:
diff changeset
832 absval = -i;
kono
parents:
diff changeset
833 res = 1;
kono
parents:
diff changeset
834 }
kono
parents:
diff changeset
835 else
kono
parents:
diff changeset
836 {
kono
parents:
diff changeset
837 absval = i;
kono
parents:
diff changeset
838 res = plus;
kono
parents:
diff changeset
839 }
kono
parents:
diff changeset
840 }
kono
parents:
diff changeset
841 else
kono
parents:
diff changeset
842 return -1;
kono
parents:
diff changeset
843 }
kono
parents:
diff changeset
844
kono
parents:
diff changeset
845 int ndigs = ilog (absval, base);
kono
parents:
diff changeset
846
kono
parents:
diff changeset
847 res += prec < ndigs ? ndigs : prec;
kono
parents:
diff changeset
848
kono
parents:
diff changeset
849 /* Adjust a non-zero value for the base prefix, either hexadecimal,
kono
parents:
diff changeset
850 or, unless precision has resulted in a leading zero, also octal. */
kono
parents:
diff changeset
851 if (prefix && absval && (base == 16 || prec <= ndigs))
kono
parents:
diff changeset
852 {
kono
parents:
diff changeset
853 if (base == 8)
kono
parents:
diff changeset
854 res += 1;
kono
parents:
diff changeset
855 else if (base == 16)
kono
parents:
diff changeset
856 res += 2;
kono
parents:
diff changeset
857 }
kono
parents:
diff changeset
858
kono
parents:
diff changeset
859 return res;
kono
parents:
diff changeset
860 }
kono
parents:
diff changeset
861
kono
parents:
diff changeset
862 /* Given the formatting result described by RES and NAVAIL, the number
kono
parents:
diff changeset
863 of available in the destination, return the range of bytes remaining
kono
parents:
diff changeset
864 in the destination. */
kono
parents:
diff changeset
865
kono
parents:
diff changeset
866 static inline result_range
kono
parents:
diff changeset
867 bytes_remaining (unsigned HOST_WIDE_INT navail, const format_result &res)
kono
parents:
diff changeset
868 {
kono
parents:
diff changeset
869 result_range range;
kono
parents:
diff changeset
870
kono
parents:
diff changeset
871 if (HOST_WIDE_INT_MAX <= navail)
kono
parents:
diff changeset
872 {
kono
parents:
diff changeset
873 range.min = range.max = range.likely = range.unlikely = navail;
kono
parents:
diff changeset
874 return range;
kono
parents:
diff changeset
875 }
kono
parents:
diff changeset
876
kono
parents:
diff changeset
877 /* The lower bound of the available range is the available size
kono
parents:
diff changeset
878 minus the maximum output size, and the upper bound is the size
kono
parents:
diff changeset
879 minus the minimum. */
kono
parents:
diff changeset
880 range.max = res.range.min < navail ? navail - res.range.min : 0;
kono
parents:
diff changeset
881
kono
parents:
diff changeset
882 range.likely = res.range.likely < navail ? navail - res.range.likely : 0;
kono
parents:
diff changeset
883
kono
parents:
diff changeset
884 if (res.range.max < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
885 range.min = res.range.max < navail ? navail - res.range.max : 0;
kono
parents:
diff changeset
886 else
kono
parents:
diff changeset
887 range.min = range.likely;
kono
parents:
diff changeset
888
kono
parents:
diff changeset
889 range.unlikely = (res.range.unlikely < navail
kono
parents:
diff changeset
890 ? navail - res.range.unlikely : 0);
kono
parents:
diff changeset
891
kono
parents:
diff changeset
892 return range;
kono
parents:
diff changeset
893 }
kono
parents:
diff changeset
894
kono
parents:
diff changeset
895 /* Description of a call to a formatted function. */
kono
parents:
diff changeset
896
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
897 struct sprintf_dom_walker::call_info
111
kono
parents:
diff changeset
898 {
kono
parents:
diff changeset
899 /* Function call statement. */
kono
parents:
diff changeset
900 gimple *callstmt;
kono
parents:
diff changeset
901
kono
parents:
diff changeset
902 /* Function called. */
kono
parents:
diff changeset
903 tree func;
kono
parents:
diff changeset
904
kono
parents:
diff changeset
905 /* Called built-in function code. */
kono
parents:
diff changeset
906 built_in_function fncode;
kono
parents:
diff changeset
907
kono
parents:
diff changeset
908 /* Format argument and format string extracted from it. */
kono
parents:
diff changeset
909 tree format;
kono
parents:
diff changeset
910 const char *fmtstr;
kono
parents:
diff changeset
911
kono
parents:
diff changeset
912 /* The location of the format argument. */
kono
parents:
diff changeset
913 location_t fmtloc;
kono
parents:
diff changeset
914
kono
parents:
diff changeset
915 /* The destination object size for __builtin___xxx_chk functions
kono
parents:
diff changeset
916 typically determined by __builtin_object_size, or -1 if unknown. */
kono
parents:
diff changeset
917 unsigned HOST_WIDE_INT objsize;
kono
parents:
diff changeset
918
kono
parents:
diff changeset
919 /* Number of the first variable argument. */
kono
parents:
diff changeset
920 unsigned HOST_WIDE_INT argidx;
kono
parents:
diff changeset
921
kono
parents:
diff changeset
922 /* True for functions like snprintf that specify the size of
kono
parents:
diff changeset
923 the destination, false for others like sprintf that don't. */
kono
parents:
diff changeset
924 bool bounded;
kono
parents:
diff changeset
925
kono
parents:
diff changeset
926 /* True for bounded functions like snprintf that specify a zero-size
kono
parents:
diff changeset
927 buffer as a request to compute the size of output without actually
kono
parents:
diff changeset
928 writing any. NOWRITE is cleared in response to the %n directive
kono
parents:
diff changeset
929 which has side-effects similar to writing output. */
kono
parents:
diff changeset
930 bool nowrite;
kono
parents:
diff changeset
931
kono
parents:
diff changeset
932 /* Return true if the called function's return value is used. */
kono
parents:
diff changeset
933 bool retval_used () const
kono
parents:
diff changeset
934 {
kono
parents:
diff changeset
935 return gimple_get_lhs (callstmt);
kono
parents:
diff changeset
936 }
kono
parents:
diff changeset
937
kono
parents:
diff changeset
938 /* Return the warning option corresponding to the called function. */
kono
parents:
diff changeset
939 int warnopt () const
kono
parents:
diff changeset
940 {
kono
parents:
diff changeset
941 return bounded ? OPT_Wformat_truncation_ : OPT_Wformat_overflow_;
kono
parents:
diff changeset
942 }
kono
parents:
diff changeset
943 };
kono
parents:
diff changeset
944
kono
parents:
diff changeset
945 /* Return the result of formatting a no-op directive (such as '%n'). */
kono
parents:
diff changeset
946
kono
parents:
diff changeset
947 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
948 format_none (const directive &, tree, vr_values *)
111
kono
parents:
diff changeset
949 {
kono
parents:
diff changeset
950 fmtresult res (0);
kono
parents:
diff changeset
951 return res;
kono
parents:
diff changeset
952 }
kono
parents:
diff changeset
953
kono
parents:
diff changeset
954 /* Return the result of formatting the '%%' directive. */
kono
parents:
diff changeset
955
kono
parents:
diff changeset
956 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
957 format_percent (const directive &, tree, vr_values *)
111
kono
parents:
diff changeset
958 {
kono
parents:
diff changeset
959 fmtresult res (1);
kono
parents:
diff changeset
960 return res;
kono
parents:
diff changeset
961 }
kono
parents:
diff changeset
962
kono
parents:
diff changeset
963
kono
parents:
diff changeset
964 /* Compute intmax_type_node and uintmax_type_node similarly to how
kono
parents:
diff changeset
965 tree.c builds size_type_node. */
kono
parents:
diff changeset
966
kono
parents:
diff changeset
967 static void
kono
parents:
diff changeset
968 build_intmax_type_nodes (tree *pintmax, tree *puintmax)
kono
parents:
diff changeset
969 {
kono
parents:
diff changeset
970 if (strcmp (UINTMAX_TYPE, "unsigned int") == 0)
kono
parents:
diff changeset
971 {
kono
parents:
diff changeset
972 *pintmax = integer_type_node;
kono
parents:
diff changeset
973 *puintmax = unsigned_type_node;
kono
parents:
diff changeset
974 }
kono
parents:
diff changeset
975 else if (strcmp (UINTMAX_TYPE, "long unsigned int") == 0)
kono
parents:
diff changeset
976 {
kono
parents:
diff changeset
977 *pintmax = long_integer_type_node;
kono
parents:
diff changeset
978 *puintmax = long_unsigned_type_node;
kono
parents:
diff changeset
979 }
kono
parents:
diff changeset
980 else if (strcmp (UINTMAX_TYPE, "long long unsigned int") == 0)
kono
parents:
diff changeset
981 {
kono
parents:
diff changeset
982 *pintmax = long_long_integer_type_node;
kono
parents:
diff changeset
983 *puintmax = long_long_unsigned_type_node;
kono
parents:
diff changeset
984 }
kono
parents:
diff changeset
985 else
kono
parents:
diff changeset
986 {
kono
parents:
diff changeset
987 for (int i = 0; i < NUM_INT_N_ENTS; i++)
kono
parents:
diff changeset
988 if (int_n_enabled_p[i])
kono
parents:
diff changeset
989 {
kono
parents:
diff changeset
990 char name[50];
kono
parents:
diff changeset
991 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
kono
parents:
diff changeset
992
kono
parents:
diff changeset
993 if (strcmp (name, UINTMAX_TYPE) == 0)
kono
parents:
diff changeset
994 {
kono
parents:
diff changeset
995 *pintmax = int_n_trees[i].signed_type;
kono
parents:
diff changeset
996 *puintmax = int_n_trees[i].unsigned_type;
kono
parents:
diff changeset
997 return;
kono
parents:
diff changeset
998 }
kono
parents:
diff changeset
999 }
kono
parents:
diff changeset
1000 gcc_unreachable ();
kono
parents:
diff changeset
1001 }
kono
parents:
diff changeset
1002 }
kono
parents:
diff changeset
1003
kono
parents:
diff changeset
1004 /* Determine the range [*PMIN, *PMAX] that the expression ARG is
kono
parents:
diff changeset
1005 in and that is representable in type int.
kono
parents:
diff changeset
1006 Return true when the range is a subrange of that of int.
kono
parents:
diff changeset
1007 When ARG is null it is as if it had the full range of int.
kono
parents:
diff changeset
1008 When ABSOLUTE is true the range reflects the absolute value of
kono
parents:
diff changeset
1009 the argument. When ABSOLUTE is false, negative bounds of
kono
parents:
diff changeset
1010 the determined range are replaced with NEGBOUND. */
kono
parents:
diff changeset
1011
kono
parents:
diff changeset
1012 static bool
kono
parents:
diff changeset
1013 get_int_range (tree arg, HOST_WIDE_INT *pmin, HOST_WIDE_INT *pmax,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1014 bool absolute, HOST_WIDE_INT negbound,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1015 class vr_values *vr_values)
111
kono
parents:
diff changeset
1016 {
kono
parents:
diff changeset
1017 /* The type of the result. */
kono
parents:
diff changeset
1018 const_tree type = integer_type_node;
kono
parents:
diff changeset
1019
kono
parents:
diff changeset
1020 bool knownrange = false;
kono
parents:
diff changeset
1021
kono
parents:
diff changeset
1022 if (!arg)
kono
parents:
diff changeset
1023 {
kono
parents:
diff changeset
1024 *pmin = tree_to_shwi (TYPE_MIN_VALUE (type));
kono
parents:
diff changeset
1025 *pmax = tree_to_shwi (TYPE_MAX_VALUE (type));
kono
parents:
diff changeset
1026 }
kono
parents:
diff changeset
1027 else if (TREE_CODE (arg) == INTEGER_CST
kono
parents:
diff changeset
1028 && TYPE_PRECISION (TREE_TYPE (arg)) <= TYPE_PRECISION (type))
kono
parents:
diff changeset
1029 {
kono
parents:
diff changeset
1030 /* For a constant argument return its value adjusted as specified
kono
parents:
diff changeset
1031 by NEGATIVE and NEGBOUND and return true to indicate that the
kono
parents:
diff changeset
1032 result is known. */
kono
parents:
diff changeset
1033 *pmin = tree_fits_shwi_p (arg) ? tree_to_shwi (arg) : tree_to_uhwi (arg);
kono
parents:
diff changeset
1034 *pmax = *pmin;
kono
parents:
diff changeset
1035 knownrange = true;
kono
parents:
diff changeset
1036 }
kono
parents:
diff changeset
1037 else
kono
parents:
diff changeset
1038 {
kono
parents:
diff changeset
1039 /* True if the argument's range cannot be determined. */
kono
parents:
diff changeset
1040 bool unknown = true;
kono
parents:
diff changeset
1041
kono
parents:
diff changeset
1042 tree argtype = TREE_TYPE (arg);
kono
parents:
diff changeset
1043
kono
parents:
diff changeset
1044 /* Ignore invalid arguments with greater precision that that
kono
parents:
diff changeset
1045 of the expected type (e.g., in sprintf("%*i", 12LL, i)).
kono
parents:
diff changeset
1046 They will have been detected and diagnosed by -Wformat and
kono
parents:
diff changeset
1047 so it's not important to complicate this code to try to deal
kono
parents:
diff changeset
1048 with them again. */
kono
parents:
diff changeset
1049 if (TREE_CODE (arg) == SSA_NAME
kono
parents:
diff changeset
1050 && INTEGRAL_TYPE_P (argtype)
kono
parents:
diff changeset
1051 && TYPE_PRECISION (argtype) <= TYPE_PRECISION (type))
kono
parents:
diff changeset
1052 {
kono
parents:
diff changeset
1053 /* Try to determine the range of values of the integer argument. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1054 value_range *vr = vr_values->get_value_range (arg);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1055 if (range_int_cst_p (vr))
111
kono
parents:
diff changeset
1056 {
kono
parents:
diff changeset
1057 HOST_WIDE_INT type_min
kono
parents:
diff changeset
1058 = (TYPE_UNSIGNED (argtype)
kono
parents:
diff changeset
1059 ? tree_to_uhwi (TYPE_MIN_VALUE (argtype))
kono
parents:
diff changeset
1060 : tree_to_shwi (TYPE_MIN_VALUE (argtype)));
kono
parents:
diff changeset
1061
kono
parents:
diff changeset
1062 HOST_WIDE_INT type_max = tree_to_uhwi (TYPE_MAX_VALUE (argtype));
kono
parents:
diff changeset
1063
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1064 *pmin = TREE_INT_CST_LOW (vr->min ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1065 *pmax = TREE_INT_CST_LOW (vr->max ());
111
kono
parents:
diff changeset
1066
kono
parents:
diff changeset
1067 if (*pmin < *pmax)
kono
parents:
diff changeset
1068 {
kono
parents:
diff changeset
1069 /* Return true if the adjusted range is a subrange of
kono
parents:
diff changeset
1070 the full range of the argument's type. *PMAX may
kono
parents:
diff changeset
1071 be less than *PMIN when the argument is unsigned
kono
parents:
diff changeset
1072 and its upper bound is in excess of TYPE_MAX. In
kono
parents:
diff changeset
1073 that (invalid) case disregard the range and use that
kono
parents:
diff changeset
1074 of the expected type instead. */
kono
parents:
diff changeset
1075 knownrange = type_min < *pmin || *pmax < type_max;
kono
parents:
diff changeset
1076
kono
parents:
diff changeset
1077 unknown = false;
kono
parents:
diff changeset
1078 }
kono
parents:
diff changeset
1079 }
kono
parents:
diff changeset
1080 }
kono
parents:
diff changeset
1081
kono
parents:
diff changeset
1082 /* Handle an argument with an unknown range as if none had been
kono
parents:
diff changeset
1083 provided. */
kono
parents:
diff changeset
1084 if (unknown)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1085 return get_int_range (NULL_TREE, pmin, pmax, absolute,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1086 negbound, vr_values);
111
kono
parents:
diff changeset
1087 }
kono
parents:
diff changeset
1088
kono
parents:
diff changeset
1089 /* Adjust each bound as specified by ABSOLUTE and NEGBOUND. */
kono
parents:
diff changeset
1090 if (absolute)
kono
parents:
diff changeset
1091 {
kono
parents:
diff changeset
1092 if (*pmin < 0)
kono
parents:
diff changeset
1093 {
kono
parents:
diff changeset
1094 if (*pmin == *pmax)
kono
parents:
diff changeset
1095 *pmin = *pmax = -*pmin;
kono
parents:
diff changeset
1096 else
kono
parents:
diff changeset
1097 {
kono
parents:
diff changeset
1098 /* Make sure signed overlow is avoided. */
kono
parents:
diff changeset
1099 gcc_assert (*pmin != HOST_WIDE_INT_MIN);
kono
parents:
diff changeset
1100
kono
parents:
diff changeset
1101 HOST_WIDE_INT tmp = -*pmin;
kono
parents:
diff changeset
1102 *pmin = 0;
kono
parents:
diff changeset
1103 if (*pmax < tmp)
kono
parents:
diff changeset
1104 *pmax = tmp;
kono
parents:
diff changeset
1105 }
kono
parents:
diff changeset
1106 }
kono
parents:
diff changeset
1107 }
kono
parents:
diff changeset
1108 else if (*pmin < negbound)
kono
parents:
diff changeset
1109 *pmin = negbound;
kono
parents:
diff changeset
1110
kono
parents:
diff changeset
1111 return knownrange;
kono
parents:
diff changeset
1112 }
kono
parents:
diff changeset
1113
kono
parents:
diff changeset
1114 /* With the range [*ARGMIN, *ARGMAX] of an integer directive's actual
kono
parents:
diff changeset
1115 argument, due to the conversion from either *ARGMIN or *ARGMAX to
kono
parents:
diff changeset
1116 the type of the directive's formal argument it's possible for both
kono
parents:
diff changeset
1117 to result in the same number of bytes or a range of bytes that's
kono
parents:
diff changeset
1118 less than the number of bytes that would result from formatting
kono
parents:
diff changeset
1119 some other value in the range [*ARGMIN, *ARGMAX]. This can be
kono
parents:
diff changeset
1120 determined by checking for the actual argument being in the range
kono
parents:
diff changeset
1121 of the type of the directive. If it isn't it must be assumed to
kono
parents:
diff changeset
1122 take on the full range of the directive's type.
kono
parents:
diff changeset
1123 Return true when the range has been adjusted to the full range
kono
parents:
diff changeset
1124 of DIRTYPE, and false otherwise. */
kono
parents:
diff changeset
1125
kono
parents:
diff changeset
1126 static bool
kono
parents:
diff changeset
1127 adjust_range_for_overflow (tree dirtype, tree *argmin, tree *argmax)
kono
parents:
diff changeset
1128 {
kono
parents:
diff changeset
1129 tree argtype = TREE_TYPE (*argmin);
kono
parents:
diff changeset
1130 unsigned argprec = TYPE_PRECISION (argtype);
kono
parents:
diff changeset
1131 unsigned dirprec = TYPE_PRECISION (dirtype);
kono
parents:
diff changeset
1132
kono
parents:
diff changeset
1133 /* If the actual argument and the directive's argument have the same
kono
parents:
diff changeset
1134 precision and sign there can be no overflow and so there is nothing
kono
parents:
diff changeset
1135 to adjust. */
kono
parents:
diff changeset
1136 if (argprec == dirprec && TYPE_SIGN (argtype) == TYPE_SIGN (dirtype))
kono
parents:
diff changeset
1137 return false;
kono
parents:
diff changeset
1138
kono
parents:
diff changeset
1139 /* The logic below was inspired/lifted from the CONVERT_EXPR_CODE_P
kono
parents:
diff changeset
1140 branch in the extract_range_from_unary_expr function in tree-vrp.c. */
kono
parents:
diff changeset
1141
kono
parents:
diff changeset
1142 if (TREE_CODE (*argmin) == INTEGER_CST
kono
parents:
diff changeset
1143 && TREE_CODE (*argmax) == INTEGER_CST
kono
parents:
diff changeset
1144 && (dirprec >= argprec
kono
parents:
diff changeset
1145 || integer_zerop (int_const_binop (RSHIFT_EXPR,
kono
parents:
diff changeset
1146 int_const_binop (MINUS_EXPR,
kono
parents:
diff changeset
1147 *argmax,
kono
parents:
diff changeset
1148 *argmin),
kono
parents:
diff changeset
1149 size_int (dirprec)))))
kono
parents:
diff changeset
1150 {
kono
parents:
diff changeset
1151 *argmin = force_fit_type (dirtype, wi::to_widest (*argmin), 0, false);
kono
parents:
diff changeset
1152 *argmax = force_fit_type (dirtype, wi::to_widest (*argmax), 0, false);
kono
parents:
diff changeset
1153
kono
parents:
diff changeset
1154 /* If *ARGMIN is still less than *ARGMAX the conversion above
kono
parents:
diff changeset
1155 is safe. Otherwise, it has overflowed and would be unsafe. */
kono
parents:
diff changeset
1156 if (tree_int_cst_le (*argmin, *argmax))
kono
parents:
diff changeset
1157 return false;
kono
parents:
diff changeset
1158 }
kono
parents:
diff changeset
1159
kono
parents:
diff changeset
1160 *argmin = TYPE_MIN_VALUE (dirtype);
kono
parents:
diff changeset
1161 *argmax = TYPE_MAX_VALUE (dirtype);
kono
parents:
diff changeset
1162 return true;
kono
parents:
diff changeset
1163 }
kono
parents:
diff changeset
1164
kono
parents:
diff changeset
1165 /* Return a range representing the minimum and maximum number of bytes
kono
parents:
diff changeset
1166 that the format directive DIR will output for any argument given
kono
parents:
diff changeset
1167 the WIDTH and PRECISION (extracted from DIR). This function is
kono
parents:
diff changeset
1168 used when the directive argument or its value isn't known. */
kono
parents:
diff changeset
1169
kono
parents:
diff changeset
1170 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1171 format_integer (const directive &dir, tree arg, vr_values *vr_values)
111
kono
parents:
diff changeset
1172 {
kono
parents:
diff changeset
1173 tree intmax_type_node;
kono
parents:
diff changeset
1174 tree uintmax_type_node;
kono
parents:
diff changeset
1175
kono
parents:
diff changeset
1176 /* Base to format the number in. */
kono
parents:
diff changeset
1177 int base;
kono
parents:
diff changeset
1178
kono
parents:
diff changeset
1179 /* True when a conversion is preceded by a prefix indicating the base
kono
parents:
diff changeset
1180 of the argument (octal or hexadecimal). */
kono
parents:
diff changeset
1181 bool maybebase = dir.get_flag ('#');
kono
parents:
diff changeset
1182
kono
parents:
diff changeset
1183 /* True when a signed conversion is preceded by a sign or space. */
kono
parents:
diff changeset
1184 bool maybesign = false;
kono
parents:
diff changeset
1185
kono
parents:
diff changeset
1186 /* True for signed conversions (i.e., 'd' and 'i'). */
kono
parents:
diff changeset
1187 bool sign = false;
kono
parents:
diff changeset
1188
kono
parents:
diff changeset
1189 switch (dir.specifier)
kono
parents:
diff changeset
1190 {
kono
parents:
diff changeset
1191 case 'd':
kono
parents:
diff changeset
1192 case 'i':
kono
parents:
diff changeset
1193 /* Space and '+' are only meaningful for signed conversions. */
kono
parents:
diff changeset
1194 maybesign = dir.get_flag (' ') | dir.get_flag ('+');
kono
parents:
diff changeset
1195 sign = true;
kono
parents:
diff changeset
1196 base = 10;
kono
parents:
diff changeset
1197 break;
kono
parents:
diff changeset
1198 case 'u':
kono
parents:
diff changeset
1199 base = 10;
kono
parents:
diff changeset
1200 break;
kono
parents:
diff changeset
1201 case 'o':
kono
parents:
diff changeset
1202 base = 8;
kono
parents:
diff changeset
1203 break;
kono
parents:
diff changeset
1204 case 'X':
kono
parents:
diff changeset
1205 case 'x':
kono
parents:
diff changeset
1206 base = 16;
kono
parents:
diff changeset
1207 break;
kono
parents:
diff changeset
1208 default:
kono
parents:
diff changeset
1209 gcc_unreachable ();
kono
parents:
diff changeset
1210 }
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 /* The type of the "formal" argument expected by the directive. */
kono
parents:
diff changeset
1213 tree dirtype = NULL_TREE;
kono
parents:
diff changeset
1214
kono
parents:
diff changeset
1215 /* Determine the expected type of the argument from the length
kono
parents:
diff changeset
1216 modifier. */
kono
parents:
diff changeset
1217 switch (dir.modifier)
kono
parents:
diff changeset
1218 {
kono
parents:
diff changeset
1219 case FMT_LEN_none:
kono
parents:
diff changeset
1220 if (dir.specifier == 'p')
kono
parents:
diff changeset
1221 dirtype = ptr_type_node;
kono
parents:
diff changeset
1222 else
kono
parents:
diff changeset
1223 dirtype = sign ? integer_type_node : unsigned_type_node;
kono
parents:
diff changeset
1224 break;
kono
parents:
diff changeset
1225
kono
parents:
diff changeset
1226 case FMT_LEN_h:
kono
parents:
diff changeset
1227 dirtype = sign ? short_integer_type_node : short_unsigned_type_node;
kono
parents:
diff changeset
1228 break;
kono
parents:
diff changeset
1229
kono
parents:
diff changeset
1230 case FMT_LEN_hh:
kono
parents:
diff changeset
1231 dirtype = sign ? signed_char_type_node : unsigned_char_type_node;
kono
parents:
diff changeset
1232 break;
kono
parents:
diff changeset
1233
kono
parents:
diff changeset
1234 case FMT_LEN_l:
kono
parents:
diff changeset
1235 dirtype = sign ? long_integer_type_node : long_unsigned_type_node;
kono
parents:
diff changeset
1236 break;
kono
parents:
diff changeset
1237
kono
parents:
diff changeset
1238 case FMT_LEN_L:
kono
parents:
diff changeset
1239 case FMT_LEN_ll:
kono
parents:
diff changeset
1240 dirtype = (sign
kono
parents:
diff changeset
1241 ? long_long_integer_type_node
kono
parents:
diff changeset
1242 : long_long_unsigned_type_node);
kono
parents:
diff changeset
1243 break;
kono
parents:
diff changeset
1244
kono
parents:
diff changeset
1245 case FMT_LEN_z:
kono
parents:
diff changeset
1246 dirtype = signed_or_unsigned_type_for (!sign, size_type_node);
kono
parents:
diff changeset
1247 break;
kono
parents:
diff changeset
1248
kono
parents:
diff changeset
1249 case FMT_LEN_t:
kono
parents:
diff changeset
1250 dirtype = signed_or_unsigned_type_for (!sign, ptrdiff_type_node);
kono
parents:
diff changeset
1251 break;
kono
parents:
diff changeset
1252
kono
parents:
diff changeset
1253 case FMT_LEN_j:
kono
parents:
diff changeset
1254 build_intmax_type_nodes (&intmax_type_node, &uintmax_type_node);
kono
parents:
diff changeset
1255 dirtype = sign ? intmax_type_node : uintmax_type_node;
kono
parents:
diff changeset
1256 break;
kono
parents:
diff changeset
1257
kono
parents:
diff changeset
1258 default:
kono
parents:
diff changeset
1259 return fmtresult ();
kono
parents:
diff changeset
1260 }
kono
parents:
diff changeset
1261
kono
parents:
diff changeset
1262 /* The type of the argument to the directive, either deduced from
kono
parents:
diff changeset
1263 the actual non-constant argument if one is known, or from
kono
parents:
diff changeset
1264 the directive itself when none has been provided because it's
kono
parents:
diff changeset
1265 a va_list. */
kono
parents:
diff changeset
1266 tree argtype = NULL_TREE;
kono
parents:
diff changeset
1267
kono
parents:
diff changeset
1268 if (!arg)
kono
parents:
diff changeset
1269 {
kono
parents:
diff changeset
1270 /* When the argument has not been provided, use the type of
kono
parents:
diff changeset
1271 the directive's argument as an approximation. This will
kono
parents:
diff changeset
1272 result in false positives for directives like %i with
kono
parents:
diff changeset
1273 arguments with smaller precision (such as short or char). */
kono
parents:
diff changeset
1274 argtype = dirtype;
kono
parents:
diff changeset
1275 }
kono
parents:
diff changeset
1276 else if (TREE_CODE (arg) == INTEGER_CST)
kono
parents:
diff changeset
1277 {
kono
parents:
diff changeset
1278 /* When a constant argument has been provided use its value
kono
parents:
diff changeset
1279 rather than type to determine the length of the output. */
kono
parents:
diff changeset
1280 fmtresult res;
kono
parents:
diff changeset
1281
kono
parents:
diff changeset
1282 if ((dir.prec[0] <= 0 && dir.prec[1] >= 0) && integer_zerop (arg))
kono
parents:
diff changeset
1283 {
kono
parents:
diff changeset
1284 /* As a special case, a precision of zero with a zero argument
kono
parents:
diff changeset
1285 results in zero bytes except in base 8 when the '#' flag is
kono
parents:
diff changeset
1286 specified, and for signed conversions in base 8 and 10 when
kono
parents:
diff changeset
1287 either the space or '+' flag has been specified and it results
kono
parents:
diff changeset
1288 in just one byte (with width having the normal effect). This
kono
parents:
diff changeset
1289 must extend to the case of a specified precision with
kono
parents:
diff changeset
1290 an unknown value because it can be zero. */
kono
parents:
diff changeset
1291 res.range.min = ((base == 8 && dir.get_flag ('#')) || maybesign);
kono
parents:
diff changeset
1292 if (res.range.min == 0 && dir.prec[0] != dir.prec[1])
kono
parents:
diff changeset
1293 {
kono
parents:
diff changeset
1294 res.range.max = 1;
kono
parents:
diff changeset
1295 res.range.likely = 1;
kono
parents:
diff changeset
1296 }
kono
parents:
diff changeset
1297 else
kono
parents:
diff changeset
1298 {
kono
parents:
diff changeset
1299 res.range.max = res.range.min;
kono
parents:
diff changeset
1300 res.range.likely = res.range.min;
kono
parents:
diff changeset
1301 }
kono
parents:
diff changeset
1302 }
kono
parents:
diff changeset
1303 else
kono
parents:
diff changeset
1304 {
kono
parents:
diff changeset
1305 /* Convert the argument to the type of the directive. */
kono
parents:
diff changeset
1306 arg = fold_convert (dirtype, arg);
kono
parents:
diff changeset
1307
kono
parents:
diff changeset
1308 res.range.min = tree_digits (arg, base, dir.prec[0],
kono
parents:
diff changeset
1309 maybesign, maybebase);
kono
parents:
diff changeset
1310 if (dir.prec[0] == dir.prec[1])
kono
parents:
diff changeset
1311 res.range.max = res.range.min;
kono
parents:
diff changeset
1312 else
kono
parents:
diff changeset
1313 res.range.max = tree_digits (arg, base, dir.prec[1],
kono
parents:
diff changeset
1314 maybesign, maybebase);
kono
parents:
diff changeset
1315 res.range.likely = res.range.min;
kono
parents:
diff changeset
1316 res.knownrange = true;
kono
parents:
diff changeset
1317 }
kono
parents:
diff changeset
1318
kono
parents:
diff changeset
1319 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
1320
kono
parents:
diff changeset
1321 /* Bump up the counters if WIDTH is greater than LEN. */
kono
parents:
diff changeset
1322 res.adjust_for_width_or_precision (dir.width, dirtype, base,
kono
parents:
diff changeset
1323 (sign | maybebase) + (base == 16));
kono
parents:
diff changeset
1324 /* Bump up the counters again if PRECision is greater still. */
kono
parents:
diff changeset
1325 res.adjust_for_width_or_precision (dir.prec, dirtype, base,
kono
parents:
diff changeset
1326 (sign | maybebase) + (base == 16));
kono
parents:
diff changeset
1327
kono
parents:
diff changeset
1328 return res;
kono
parents:
diff changeset
1329 }
kono
parents:
diff changeset
1330 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg))
kono
parents:
diff changeset
1331 || TREE_CODE (TREE_TYPE (arg)) == POINTER_TYPE)
kono
parents:
diff changeset
1332 /* Determine the type of the provided non-constant argument. */
kono
parents:
diff changeset
1333 argtype = TREE_TYPE (arg);
kono
parents:
diff changeset
1334 else
kono
parents:
diff changeset
1335 /* Don't bother with invalid arguments since they likely would
kono
parents:
diff changeset
1336 have already been diagnosed, and disable any further checking
kono
parents:
diff changeset
1337 of the format string by returning [-1, -1]. */
kono
parents:
diff changeset
1338 return fmtresult ();
kono
parents:
diff changeset
1339
kono
parents:
diff changeset
1340 fmtresult res;
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 /* Using either the range the non-constant argument is in, or its
kono
parents:
diff changeset
1343 type (either "formal" or actual), create a range of values that
kono
parents:
diff changeset
1344 constrain the length of output given the warning level. */
kono
parents:
diff changeset
1345 tree argmin = NULL_TREE;
kono
parents:
diff changeset
1346 tree argmax = NULL_TREE;
kono
parents:
diff changeset
1347
kono
parents:
diff changeset
1348 if (arg
kono
parents:
diff changeset
1349 && TREE_CODE (arg) == SSA_NAME
kono
parents:
diff changeset
1350 && INTEGRAL_TYPE_P (argtype))
kono
parents:
diff changeset
1351 {
kono
parents:
diff changeset
1352 /* Try to determine the range of values of the integer argument
kono
parents:
diff changeset
1353 (range information is not available for pointers). */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1354 value_range *vr = vr_values->get_value_range (arg);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1355 if (range_int_cst_p (vr))
111
kono
parents:
diff changeset
1356 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1357 argmin = vr->min ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1358 argmax = vr->max ();
111
kono
parents:
diff changeset
1359
kono
parents:
diff changeset
1360 /* Set KNOWNRANGE if the argument is in a known subrange
kono
parents:
diff changeset
1361 of the directive's type and neither width nor precision
kono
parents:
diff changeset
1362 is unknown. (KNOWNRANGE may be reset below). */
kono
parents:
diff changeset
1363 res.knownrange
kono
parents:
diff changeset
1364 = ((!tree_int_cst_equal (TYPE_MIN_VALUE (dirtype), argmin)
kono
parents:
diff changeset
1365 || !tree_int_cst_equal (TYPE_MAX_VALUE (dirtype), argmax))
kono
parents:
diff changeset
1366 && dir.known_width_and_precision ());
kono
parents:
diff changeset
1367
kono
parents:
diff changeset
1368 res.argmin = argmin;
kono
parents:
diff changeset
1369 res.argmax = argmax;
kono
parents:
diff changeset
1370 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1371 else if (vr->kind () == VR_ANTI_RANGE)
111
kono
parents:
diff changeset
1372 {
kono
parents:
diff changeset
1373 /* Handle anti-ranges if/when bug 71690 is resolved. */
kono
parents:
diff changeset
1374 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1375 else if (vr->varying_p () || vr->undefined_p ())
111
kono
parents:
diff changeset
1376 {
kono
parents:
diff changeset
1377 /* The argument here may be the result of promoting the actual
kono
parents:
diff changeset
1378 argument to int. Try to determine the type of the actual
kono
parents:
diff changeset
1379 argument before promotion and narrow down its range that
kono
parents:
diff changeset
1380 way. */
kono
parents:
diff changeset
1381 gimple *def = SSA_NAME_DEF_STMT (arg);
kono
parents:
diff changeset
1382 if (is_gimple_assign (def))
kono
parents:
diff changeset
1383 {
kono
parents:
diff changeset
1384 tree_code code = gimple_assign_rhs_code (def);
kono
parents:
diff changeset
1385 if (code == INTEGER_CST)
kono
parents:
diff changeset
1386 {
kono
parents:
diff changeset
1387 arg = gimple_assign_rhs1 (def);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1388 return format_integer (dir, arg, vr_values);
111
kono
parents:
diff changeset
1389 }
kono
parents:
diff changeset
1390
kono
parents:
diff changeset
1391 if (code == NOP_EXPR)
kono
parents:
diff changeset
1392 {
kono
parents:
diff changeset
1393 tree type = TREE_TYPE (gimple_assign_rhs1 (def));
kono
parents:
diff changeset
1394 if (INTEGRAL_TYPE_P (type)
kono
parents:
diff changeset
1395 || TREE_CODE (type) == POINTER_TYPE)
kono
parents:
diff changeset
1396 argtype = type;
kono
parents:
diff changeset
1397 }
kono
parents:
diff changeset
1398 }
kono
parents:
diff changeset
1399 }
kono
parents:
diff changeset
1400 }
kono
parents:
diff changeset
1401
kono
parents:
diff changeset
1402 if (!argmin)
kono
parents:
diff changeset
1403 {
kono
parents:
diff changeset
1404 if (TREE_CODE (argtype) == POINTER_TYPE)
kono
parents:
diff changeset
1405 {
kono
parents:
diff changeset
1406 argmin = build_int_cst (pointer_sized_int_node, 0);
kono
parents:
diff changeset
1407 argmax = build_all_ones_cst (pointer_sized_int_node);
kono
parents:
diff changeset
1408 }
kono
parents:
diff changeset
1409 else
kono
parents:
diff changeset
1410 {
kono
parents:
diff changeset
1411 argmin = TYPE_MIN_VALUE (argtype);
kono
parents:
diff changeset
1412 argmax = TYPE_MAX_VALUE (argtype);
kono
parents:
diff changeset
1413 }
kono
parents:
diff changeset
1414 }
kono
parents:
diff changeset
1415
kono
parents:
diff changeset
1416 /* Clear KNOWNRANGE if the range has been adjusted to the maximum
kono
parents:
diff changeset
1417 of the directive. If it has been cleared then since ARGMIN and/or
kono
parents:
diff changeset
1418 ARGMAX have been adjusted also adjust the corresponding ARGMIN and
kono
parents:
diff changeset
1419 ARGMAX in the result to include in diagnostics. */
kono
parents:
diff changeset
1420 if (adjust_range_for_overflow (dirtype, &argmin, &argmax))
kono
parents:
diff changeset
1421 {
kono
parents:
diff changeset
1422 res.knownrange = false;
kono
parents:
diff changeset
1423 res.argmin = argmin;
kono
parents:
diff changeset
1424 res.argmax = argmax;
kono
parents:
diff changeset
1425 }
kono
parents:
diff changeset
1426
kono
parents:
diff changeset
1427 /* Recursively compute the minimum and maximum from the known range. */
kono
parents:
diff changeset
1428 if (TYPE_UNSIGNED (dirtype) || tree_int_cst_sgn (argmin) >= 0)
kono
parents:
diff changeset
1429 {
kono
parents:
diff changeset
1430 /* For unsigned conversions/directives or signed when
kono
parents:
diff changeset
1431 the minimum is positive, use the minimum and maximum to compute
kono
parents:
diff changeset
1432 the shortest and longest output, respectively. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1433 res.range.min = format_integer (dir, argmin, vr_values).range.min;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1434 res.range.max = format_integer (dir, argmax, vr_values).range.max;
111
kono
parents:
diff changeset
1435 }
kono
parents:
diff changeset
1436 else if (tree_int_cst_sgn (argmax) < 0)
kono
parents:
diff changeset
1437 {
kono
parents:
diff changeset
1438 /* For signed conversions/directives if maximum is negative,
kono
parents:
diff changeset
1439 use the minimum as the longest output and maximum as the
kono
parents:
diff changeset
1440 shortest output. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1441 res.range.min = format_integer (dir, argmax, vr_values).range.min;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1442 res.range.max = format_integer (dir, argmin, vr_values).range.max;
111
kono
parents:
diff changeset
1443 }
kono
parents:
diff changeset
1444 else
kono
parents:
diff changeset
1445 {
kono
parents:
diff changeset
1446 /* Otherwise, 0 is inside of the range and minimum negative. Use 0
kono
parents:
diff changeset
1447 as the shortest output and for the longest output compute the
kono
parents:
diff changeset
1448 length of the output of both minimum and maximum and pick the
kono
parents:
diff changeset
1449 longer. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1450 unsigned HOST_WIDE_INT max1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1451 = format_integer (dir, argmin, vr_values).range.max;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1452 unsigned HOST_WIDE_INT max2
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1453 = format_integer (dir, argmax, vr_values).range.max;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1454 res.range.min
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1455 = format_integer (dir, integer_zero_node, vr_values).range.min;
111
kono
parents:
diff changeset
1456 res.range.max = MAX (max1, max2);
kono
parents:
diff changeset
1457 }
kono
parents:
diff changeset
1458
kono
parents:
diff changeset
1459 /* If the range is known, use the maximum as the likely length. */
kono
parents:
diff changeset
1460 if (res.knownrange)
kono
parents:
diff changeset
1461 res.range.likely = res.range.max;
kono
parents:
diff changeset
1462 else
kono
parents:
diff changeset
1463 {
kono
parents:
diff changeset
1464 /* Otherwise, use the minimum. Except for the case where for %#x or
kono
parents:
diff changeset
1465 %#o the minimum is just for a single value in the range (0) and
kono
parents:
diff changeset
1466 for all other values it is something longer, like 0x1 or 01.
kono
parents:
diff changeset
1467 Use the length for value 1 in that case instead as the likely
kono
parents:
diff changeset
1468 length. */
kono
parents:
diff changeset
1469 res.range.likely = res.range.min;
kono
parents:
diff changeset
1470 if (maybebase
kono
parents:
diff changeset
1471 && base != 10
kono
parents:
diff changeset
1472 && (tree_int_cst_sgn (argmin) < 0 || tree_int_cst_sgn (argmax) > 0))
kono
parents:
diff changeset
1473 {
kono
parents:
diff changeset
1474 if (res.range.min == 1)
kono
parents:
diff changeset
1475 res.range.likely += base == 8 ? 1 : 2;
kono
parents:
diff changeset
1476 else if (res.range.min == 2
kono
parents:
diff changeset
1477 && base == 16
kono
parents:
diff changeset
1478 && (dir.width[0] == 2 || dir.prec[0] == 2))
kono
parents:
diff changeset
1479 ++res.range.likely;
kono
parents:
diff changeset
1480 }
kono
parents:
diff changeset
1481 }
kono
parents:
diff changeset
1482
kono
parents:
diff changeset
1483 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
1484 res.adjust_for_width_or_precision (dir.width, dirtype, base,
kono
parents:
diff changeset
1485 (sign | maybebase) + (base == 16));
kono
parents:
diff changeset
1486 res.adjust_for_width_or_precision (dir.prec, dirtype, base,
kono
parents:
diff changeset
1487 (sign | maybebase) + (base == 16));
kono
parents:
diff changeset
1488
kono
parents:
diff changeset
1489 return res;
kono
parents:
diff changeset
1490 }
kono
parents:
diff changeset
1491
kono
parents:
diff changeset
1492 /* Return the number of bytes that a format directive consisting of FLAGS,
kono
parents:
diff changeset
1493 PRECision, format SPECification, and MPFR rounding specifier RNDSPEC,
kono
parents:
diff changeset
1494 would result for argument X under ideal conditions (i.e., if PREC
kono
parents:
diff changeset
1495 weren't excessive). MPFR 3.1 allocates large amounts of memory for
kono
parents:
diff changeset
1496 values of PREC with large magnitude and can fail (see MPFR bug #21056).
kono
parents:
diff changeset
1497 This function works around those problems. */
kono
parents:
diff changeset
1498
kono
parents:
diff changeset
1499 static unsigned HOST_WIDE_INT
kono
parents:
diff changeset
1500 get_mpfr_format_length (mpfr_ptr x, const char *flags, HOST_WIDE_INT prec,
kono
parents:
diff changeset
1501 char spec, char rndspec)
kono
parents:
diff changeset
1502 {
kono
parents:
diff changeset
1503 char fmtstr[40];
kono
parents:
diff changeset
1504
kono
parents:
diff changeset
1505 HOST_WIDE_INT len = strlen (flags);
kono
parents:
diff changeset
1506
kono
parents:
diff changeset
1507 fmtstr[0] = '%';
kono
parents:
diff changeset
1508 memcpy (fmtstr + 1, flags, len);
kono
parents:
diff changeset
1509 memcpy (fmtstr + 1 + len, ".*R", 3);
kono
parents:
diff changeset
1510 fmtstr[len + 4] = rndspec;
kono
parents:
diff changeset
1511 fmtstr[len + 5] = spec;
kono
parents:
diff changeset
1512 fmtstr[len + 6] = '\0';
kono
parents:
diff changeset
1513
kono
parents:
diff changeset
1514 spec = TOUPPER (spec);
kono
parents:
diff changeset
1515 if (spec == 'E' || spec == 'F')
kono
parents:
diff changeset
1516 {
kono
parents:
diff changeset
1517 /* For %e, specify the precision explicitly since mpfr_sprintf
kono
parents:
diff changeset
1518 does its own thing just to be different (see MPFR bug 21088). */
kono
parents:
diff changeset
1519 if (prec < 0)
kono
parents:
diff changeset
1520 prec = 6;
kono
parents:
diff changeset
1521 }
kono
parents:
diff changeset
1522 else
kono
parents:
diff changeset
1523 {
kono
parents:
diff changeset
1524 /* Avoid passing negative precisions with larger magnitude to MPFR
kono
parents:
diff changeset
1525 to avoid exposing its bugs. (A negative precision is supposed
kono
parents:
diff changeset
1526 to be ignored.) */
kono
parents:
diff changeset
1527 if (prec < 0)
kono
parents:
diff changeset
1528 prec = -1;
kono
parents:
diff changeset
1529 }
kono
parents:
diff changeset
1530
kono
parents:
diff changeset
1531 HOST_WIDE_INT p = prec;
kono
parents:
diff changeset
1532
kono
parents:
diff changeset
1533 if (spec == 'G' && !strchr (flags, '#'))
kono
parents:
diff changeset
1534 {
kono
parents:
diff changeset
1535 /* For G/g without the pound flag, precision gives the maximum number
kono
parents:
diff changeset
1536 of significant digits which is bounded by LDBL_MAX_10_EXP, or, for
kono
parents:
diff changeset
1537 a 128 bit IEEE extended precision, 4932. Using twice as much here
kono
parents:
diff changeset
1538 should be more than sufficient for any real format. */
kono
parents:
diff changeset
1539 if ((IEEE_MAX_10_EXP * 2) < prec)
kono
parents:
diff changeset
1540 prec = IEEE_MAX_10_EXP * 2;
kono
parents:
diff changeset
1541 p = prec;
kono
parents:
diff changeset
1542 }
kono
parents:
diff changeset
1543 else
kono
parents:
diff changeset
1544 {
kono
parents:
diff changeset
1545 /* Cap precision arbitrarily at 1KB and add the difference
kono
parents:
diff changeset
1546 (if any) to the MPFR result. */
kono
parents:
diff changeset
1547 if (prec > 1024)
kono
parents:
diff changeset
1548 p = 1024;
kono
parents:
diff changeset
1549 }
kono
parents:
diff changeset
1550
kono
parents:
diff changeset
1551 len = mpfr_snprintf (NULL, 0, fmtstr, (int)p, x);
kono
parents:
diff changeset
1552
kono
parents:
diff changeset
1553 /* Handle the unlikely (impossible?) error by returning more than
kono
parents:
diff changeset
1554 the maximum dictated by the function's return type. */
kono
parents:
diff changeset
1555 if (len < 0)
kono
parents:
diff changeset
1556 return target_dir_max () + 1;
kono
parents:
diff changeset
1557
kono
parents:
diff changeset
1558 /* Adjust the return value by the difference. */
kono
parents:
diff changeset
1559 if (p < prec)
kono
parents:
diff changeset
1560 len += prec - p;
kono
parents:
diff changeset
1561
kono
parents:
diff changeset
1562 return len;
kono
parents:
diff changeset
1563 }
kono
parents:
diff changeset
1564
kono
parents:
diff changeset
1565 /* Return the number of bytes to format using the format specifier
kono
parents:
diff changeset
1566 SPEC and the precision PREC the largest value in the real floating
kono
parents:
diff changeset
1567 TYPE. */
kono
parents:
diff changeset
1568
kono
parents:
diff changeset
1569 static unsigned HOST_WIDE_INT
kono
parents:
diff changeset
1570 format_floating_max (tree type, char spec, HOST_WIDE_INT prec)
kono
parents:
diff changeset
1571 {
kono
parents:
diff changeset
1572 machine_mode mode = TYPE_MODE (type);
kono
parents:
diff changeset
1573
kono
parents:
diff changeset
1574 /* IBM Extended mode. */
kono
parents:
diff changeset
1575 if (MODE_COMPOSITE_P (mode))
kono
parents:
diff changeset
1576 mode = DFmode;
kono
parents:
diff changeset
1577
kono
parents:
diff changeset
1578 /* Get the real type format desription for the target. */
kono
parents:
diff changeset
1579 const real_format *rfmt = REAL_MODE_FORMAT (mode);
kono
parents:
diff changeset
1580 REAL_VALUE_TYPE rv;
kono
parents:
diff changeset
1581
kono
parents:
diff changeset
1582 real_maxval (&rv, 0, mode);
kono
parents:
diff changeset
1583
kono
parents:
diff changeset
1584 /* Convert the GCC real value representation with the precision
kono
parents:
diff changeset
1585 of the real type to the mpfr_t format with the GCC default
kono
parents:
diff changeset
1586 round-to-nearest mode. */
kono
parents:
diff changeset
1587 mpfr_t x;
kono
parents:
diff changeset
1588 mpfr_init2 (x, rfmt->p);
kono
parents:
diff changeset
1589 mpfr_from_real (x, &rv, GMP_RNDN);
kono
parents:
diff changeset
1590
kono
parents:
diff changeset
1591 /* Return a value one greater to account for the leading minus sign. */
kono
parents:
diff changeset
1592 unsigned HOST_WIDE_INT r
kono
parents:
diff changeset
1593 = 1 + get_mpfr_format_length (x, "", prec, spec, 'D');
kono
parents:
diff changeset
1594 mpfr_clear (x);
kono
parents:
diff changeset
1595 return r;
kono
parents:
diff changeset
1596 }
kono
parents:
diff changeset
1597
kono
parents:
diff changeset
1598 /* Return a range representing the minimum and maximum number of bytes
kono
parents:
diff changeset
1599 that the directive DIR will output for any argument. PREC gives
kono
parents:
diff changeset
1600 the adjusted precision range to account for negative precisions
kono
parents:
diff changeset
1601 meaning the default 6. This function is used when the directive
kono
parents:
diff changeset
1602 argument or its value isn't known. */
kono
parents:
diff changeset
1603
kono
parents:
diff changeset
1604 static fmtresult
kono
parents:
diff changeset
1605 format_floating (const directive &dir, const HOST_WIDE_INT prec[2])
kono
parents:
diff changeset
1606 {
kono
parents:
diff changeset
1607 tree type;
kono
parents:
diff changeset
1608
kono
parents:
diff changeset
1609 switch (dir.modifier)
kono
parents:
diff changeset
1610 {
kono
parents:
diff changeset
1611 case FMT_LEN_l:
kono
parents:
diff changeset
1612 case FMT_LEN_none:
kono
parents:
diff changeset
1613 type = double_type_node;
kono
parents:
diff changeset
1614 break;
kono
parents:
diff changeset
1615
kono
parents:
diff changeset
1616 case FMT_LEN_L:
kono
parents:
diff changeset
1617 type = long_double_type_node;
kono
parents:
diff changeset
1618 break;
kono
parents:
diff changeset
1619
kono
parents:
diff changeset
1620 case FMT_LEN_ll:
kono
parents:
diff changeset
1621 type = long_double_type_node;
kono
parents:
diff changeset
1622 break;
kono
parents:
diff changeset
1623
kono
parents:
diff changeset
1624 default:
kono
parents:
diff changeset
1625 return fmtresult ();
kono
parents:
diff changeset
1626 }
kono
parents:
diff changeset
1627
kono
parents:
diff changeset
1628 /* The minimum and maximum number of bytes produced by the directive. */
kono
parents:
diff changeset
1629 fmtresult res;
kono
parents:
diff changeset
1630
kono
parents:
diff changeset
1631 /* The minimum output as determined by flags. It's always at least 1.
kono
parents:
diff changeset
1632 When plus or space are set the output is preceded by either a sign
kono
parents:
diff changeset
1633 or a space. */
kono
parents:
diff changeset
1634 unsigned flagmin = (1 /* for the first digit */
kono
parents:
diff changeset
1635 + (dir.get_flag ('+') | dir.get_flag (' ')));
kono
parents:
diff changeset
1636
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1637 /* The minimum is 3 for "inf" and "nan" for all specifiers, plus 1
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1638 for the plus sign/space with the '+' and ' ' flags, respectively,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1639 unless reduced below. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1640 res.range.min = 2 + flagmin;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1641
111
kono
parents:
diff changeset
1642 /* When the pound flag is set the decimal point is included in output
kono
parents:
diff changeset
1643 regardless of precision. Whether or not a decimal point is included
kono
parents:
diff changeset
1644 otherwise depends on the specification and precision. */
kono
parents:
diff changeset
1645 bool radix = dir.get_flag ('#');
kono
parents:
diff changeset
1646
kono
parents:
diff changeset
1647 switch (dir.specifier)
kono
parents:
diff changeset
1648 {
kono
parents:
diff changeset
1649 case 'A':
kono
parents:
diff changeset
1650 case 'a':
kono
parents:
diff changeset
1651 {
kono
parents:
diff changeset
1652 HOST_WIDE_INT minprec = 6 + !radix /* decimal point */;
kono
parents:
diff changeset
1653 if (dir.prec[0] <= 0)
kono
parents:
diff changeset
1654 minprec = 0;
kono
parents:
diff changeset
1655 else if (dir.prec[0] > 0)
kono
parents:
diff changeset
1656 minprec = dir.prec[0] + !radix /* decimal point */;
kono
parents:
diff changeset
1657
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1658 res.range.likely = (2 /* 0x */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1659 + flagmin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1660 + radix
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1661 + minprec
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1662 + 3 /* p+0 */);
111
kono
parents:
diff changeset
1663
kono
parents:
diff changeset
1664 res.range.max = format_floating_max (type, 'a', prec[1]);
kono
parents:
diff changeset
1665
kono
parents:
diff changeset
1666 /* The unlikely maximum accounts for the longest multibyte
kono
parents:
diff changeset
1667 decimal point character. */
kono
parents:
diff changeset
1668 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
1669 if (dir.prec[1] > 0)
kono
parents:
diff changeset
1670 res.range.unlikely += target_mb_len_max () - 1;
kono
parents:
diff changeset
1671
kono
parents:
diff changeset
1672 break;
kono
parents:
diff changeset
1673 }
kono
parents:
diff changeset
1674
kono
parents:
diff changeset
1675 case 'E':
kono
parents:
diff changeset
1676 case 'e':
kono
parents:
diff changeset
1677 {
kono
parents:
diff changeset
1678 /* Minimum output attributable to precision and, when it's
kono
parents:
diff changeset
1679 non-zero, decimal point. */
kono
parents:
diff changeset
1680 HOST_WIDE_INT minprec = prec[0] ? prec[0] + !radix : 0;
kono
parents:
diff changeset
1681
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1682 /* The likely minimum output is "[-+]1.234567e+00" regardless
111
kono
parents:
diff changeset
1683 of the value of the actual argument. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1684 res.range.likely = (flagmin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1685 + radix
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1686 + minprec
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1687 + 2 /* e+ */ + 2);
111
kono
parents:
diff changeset
1688
kono
parents:
diff changeset
1689 res.range.max = format_floating_max (type, 'e', prec[1]);
kono
parents:
diff changeset
1690
kono
parents:
diff changeset
1691 /* The unlikely maximum accounts for the longest multibyte
kono
parents:
diff changeset
1692 decimal point character. */
kono
parents:
diff changeset
1693 if (dir.prec[0] != dir.prec[1]
kono
parents:
diff changeset
1694 || dir.prec[0] == -1 || dir.prec[0] > 0)
kono
parents:
diff changeset
1695 res.range.unlikely = res.range.max + target_mb_len_max () -1;
kono
parents:
diff changeset
1696 else
kono
parents:
diff changeset
1697 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
1698 break;
kono
parents:
diff changeset
1699 }
kono
parents:
diff changeset
1700
kono
parents:
diff changeset
1701 case 'F':
kono
parents:
diff changeset
1702 case 'f':
kono
parents:
diff changeset
1703 {
kono
parents:
diff changeset
1704 /* Minimum output attributable to precision and, when it's non-zero,
kono
parents:
diff changeset
1705 decimal point. */
kono
parents:
diff changeset
1706 HOST_WIDE_INT minprec = prec[0] ? prec[0] + !radix : 0;
kono
parents:
diff changeset
1707
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1708 /* For finite numbers (i.e., not infinity or NaN) the lower bound
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1709 when precision isn't specified is 8 bytes ("1.23456" since
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1710 precision is taken to be 6). When precision is zero, the lower
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1711 bound is 1 byte (e.g., "1"). Otherwise, when precision is greater
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1712 than zero, then the lower bound is 2 plus precision (plus flags).
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1713 But in all cases, the lower bound is no greater than 3. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1714 unsigned HOST_WIDE_INT min = flagmin + radix + minprec;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1715 if (min < res.range.min)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1716 res.range.min = min;
111
kono
parents:
diff changeset
1717
kono
parents:
diff changeset
1718 /* Compute the upper bound for -TYPE_MAX. */
kono
parents:
diff changeset
1719 res.range.max = format_floating_max (type, 'f', prec[1]);
kono
parents:
diff changeset
1720
kono
parents:
diff changeset
1721 /* The minimum output with unknown precision is a single byte
kono
parents:
diff changeset
1722 (e.g., "0") but the more likely output is 3 bytes ("0.0"). */
kono
parents:
diff changeset
1723 if (dir.prec[0] < 0 && dir.prec[1] > 0)
kono
parents:
diff changeset
1724 res.range.likely = 3;
kono
parents:
diff changeset
1725 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1726 res.range.likely = min;
111
kono
parents:
diff changeset
1727
kono
parents:
diff changeset
1728 /* The unlikely maximum accounts for the longest multibyte
kono
parents:
diff changeset
1729 decimal point character. */
kono
parents:
diff changeset
1730 if (dir.prec[0] != dir.prec[1]
kono
parents:
diff changeset
1731 || dir.prec[0] == -1 || dir.prec[0] > 0)
kono
parents:
diff changeset
1732 res.range.unlikely = res.range.max + target_mb_len_max () - 1;
kono
parents:
diff changeset
1733 break;
kono
parents:
diff changeset
1734 }
kono
parents:
diff changeset
1735
kono
parents:
diff changeset
1736 case 'G':
kono
parents:
diff changeset
1737 case 'g':
kono
parents:
diff changeset
1738 {
kono
parents:
diff changeset
1739 /* The %g output depends on precision and the exponent of
kono
parents:
diff changeset
1740 the argument. Since the value of the argument isn't known
kono
parents:
diff changeset
1741 the lower bound on the range of bytes (not counting flags
kono
parents:
diff changeset
1742 or width) is 1 plus radix (i.e., either "0" or "0." for
kono
parents:
diff changeset
1743 "%g" and "%#g", respectively, with a zero argument). */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1744 unsigned HOST_WIDE_INT min = flagmin + radix;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1745 if (min < res.range.min)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1746 res.range.min = min;
111
kono
parents:
diff changeset
1747
kono
parents:
diff changeset
1748 char spec = 'g';
kono
parents:
diff changeset
1749 HOST_WIDE_INT maxprec = dir.prec[1];
kono
parents:
diff changeset
1750 if (radix && maxprec)
kono
parents:
diff changeset
1751 {
kono
parents:
diff changeset
1752 /* When the pound flag (radix) is set, trailing zeros aren't
kono
parents:
diff changeset
1753 trimmed and so the longest output is the same as for %e,
kono
parents:
diff changeset
1754 except with precision minus 1 (as specified in C11). */
kono
parents:
diff changeset
1755 spec = 'e';
kono
parents:
diff changeset
1756 if (maxprec > 0)
kono
parents:
diff changeset
1757 --maxprec;
kono
parents:
diff changeset
1758 else if (maxprec < 0)
kono
parents:
diff changeset
1759 maxprec = 5;
kono
parents:
diff changeset
1760 }
kono
parents:
diff changeset
1761 else
kono
parents:
diff changeset
1762 maxprec = prec[1];
kono
parents:
diff changeset
1763
kono
parents:
diff changeset
1764 res.range.max = format_floating_max (type, spec, maxprec);
kono
parents:
diff changeset
1765
kono
parents:
diff changeset
1766 /* The likely output is either the maximum computed above
kono
parents:
diff changeset
1767 minus 1 (assuming the maximum is positive) when precision
kono
parents:
diff changeset
1768 is known (or unspecified), or the same minimum as for %e
kono
parents:
diff changeset
1769 (which is computed for a non-negative argument). Unlike
kono
parents:
diff changeset
1770 for the other specifiers above the likely output isn't
kono
parents:
diff changeset
1771 the minimum because for %g that's 1 which is unlikely. */
kono
parents:
diff changeset
1772 if (dir.prec[1] < 0
kono
parents:
diff changeset
1773 || (unsigned HOST_WIDE_INT)dir.prec[1] < target_int_max ())
kono
parents:
diff changeset
1774 res.range.likely = res.range.max - 1;
kono
parents:
diff changeset
1775 else
kono
parents:
diff changeset
1776 {
kono
parents:
diff changeset
1777 HOST_WIDE_INT minprec = 6 + !radix /* decimal point */;
kono
parents:
diff changeset
1778 res.range.likely = (flagmin
kono
parents:
diff changeset
1779 + radix
kono
parents:
diff changeset
1780 + minprec
kono
parents:
diff changeset
1781 + 2 /* e+ */ + 2);
kono
parents:
diff changeset
1782 }
kono
parents:
diff changeset
1783
kono
parents:
diff changeset
1784 /* The unlikely maximum accounts for the longest multibyte
kono
parents:
diff changeset
1785 decimal point character. */
kono
parents:
diff changeset
1786 res.range.unlikely = res.range.max + target_mb_len_max () - 1;
kono
parents:
diff changeset
1787 break;
kono
parents:
diff changeset
1788 }
kono
parents:
diff changeset
1789
kono
parents:
diff changeset
1790 default:
kono
parents:
diff changeset
1791 return fmtresult ();
kono
parents:
diff changeset
1792 }
kono
parents:
diff changeset
1793
kono
parents:
diff changeset
1794 /* Bump up the byte counters if WIDTH is greater. */
kono
parents:
diff changeset
1795 res.adjust_for_width_or_precision (dir.width);
kono
parents:
diff changeset
1796 return res;
kono
parents:
diff changeset
1797 }
kono
parents:
diff changeset
1798
kono
parents:
diff changeset
1799 /* Return a range representing the minimum and maximum number of bytes
kono
parents:
diff changeset
1800 that the directive DIR will write on output for the floating argument
kono
parents:
diff changeset
1801 ARG. */
kono
parents:
diff changeset
1802
kono
parents:
diff changeset
1803 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1804 format_floating (const directive &dir, tree arg, vr_values *)
111
kono
parents:
diff changeset
1805 {
kono
parents:
diff changeset
1806 HOST_WIDE_INT prec[] = { dir.prec[0], dir.prec[1] };
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1807 tree type = (dir.modifier == FMT_LEN_L || dir.modifier == FMT_LEN_ll
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1808 ? long_double_type_node : double_type_node);
111
kono
parents:
diff changeset
1809
kono
parents:
diff changeset
1810 /* For an indeterminate precision the lower bound must be assumed
kono
parents:
diff changeset
1811 to be zero. */
kono
parents:
diff changeset
1812 if (TOUPPER (dir.specifier) == 'A')
kono
parents:
diff changeset
1813 {
kono
parents:
diff changeset
1814 /* Get the number of fractional decimal digits needed to represent
kono
parents:
diff changeset
1815 the argument without a loss of accuracy. */
kono
parents:
diff changeset
1816 unsigned fmtprec
kono
parents:
diff changeset
1817 = REAL_MODE_FORMAT (TYPE_MODE (type))->p;
kono
parents:
diff changeset
1818
kono
parents:
diff changeset
1819 /* The precision of the IEEE 754 double format is 53.
kono
parents:
diff changeset
1820 The precision of all other GCC binary double formats
kono
parents:
diff changeset
1821 is 56 or less. */
kono
parents:
diff changeset
1822 unsigned maxprec = fmtprec <= 56 ? 13 : 15;
kono
parents:
diff changeset
1823
kono
parents:
diff changeset
1824 /* For %a, leave the minimum precision unspecified to let
kono
parents:
diff changeset
1825 MFPR trim trailing zeros (as it and many other systems
kono
parents:
diff changeset
1826 including Glibc happen to do) and set the maximum
kono
parents:
diff changeset
1827 precision to reflect what it would be with trailing zeros
kono
parents:
diff changeset
1828 present (as Solaris and derived systems do). */
kono
parents:
diff changeset
1829 if (dir.prec[1] < 0)
kono
parents:
diff changeset
1830 {
kono
parents:
diff changeset
1831 /* Both bounds are negative implies that precision has
kono
parents:
diff changeset
1832 not been specified. */
kono
parents:
diff changeset
1833 prec[0] = maxprec;
kono
parents:
diff changeset
1834 prec[1] = -1;
kono
parents:
diff changeset
1835 }
kono
parents:
diff changeset
1836 else if (dir.prec[0] < 0)
kono
parents:
diff changeset
1837 {
kono
parents:
diff changeset
1838 /* With a negative lower bound and a non-negative upper
kono
parents:
diff changeset
1839 bound set the minimum precision to zero and the maximum
kono
parents:
diff changeset
1840 to the greater of the maximum precision (i.e., with
kono
parents:
diff changeset
1841 trailing zeros present) and the specified upper bound. */
kono
parents:
diff changeset
1842 prec[0] = 0;
kono
parents:
diff changeset
1843 prec[1] = dir.prec[1] < maxprec ? maxprec : dir.prec[1];
kono
parents:
diff changeset
1844 }
kono
parents:
diff changeset
1845 }
kono
parents:
diff changeset
1846 else if (dir.prec[0] < 0)
kono
parents:
diff changeset
1847 {
kono
parents:
diff changeset
1848 if (dir.prec[1] < 0)
kono
parents:
diff changeset
1849 {
kono
parents:
diff changeset
1850 /* A precision in a strictly negative range is ignored and
kono
parents:
diff changeset
1851 the default of 6 is used instead. */
kono
parents:
diff changeset
1852 prec[0] = prec[1] = 6;
kono
parents:
diff changeset
1853 }
kono
parents:
diff changeset
1854 else
kono
parents:
diff changeset
1855 {
kono
parents:
diff changeset
1856 /* For a precision in a partly negative range, the lower bound
kono
parents:
diff changeset
1857 must be assumed to be zero and the new upper bound is the
kono
parents:
diff changeset
1858 greater of 6 (the default precision used when the specified
kono
parents:
diff changeset
1859 precision is negative) and the upper bound of the specified
kono
parents:
diff changeset
1860 range. */
kono
parents:
diff changeset
1861 prec[0] = 0;
kono
parents:
diff changeset
1862 prec[1] = dir.prec[1] < 6 ? 6 : dir.prec[1];
kono
parents:
diff changeset
1863 }
kono
parents:
diff changeset
1864 }
kono
parents:
diff changeset
1865
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1866 if (!arg
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1867 || TREE_CODE (arg) != REAL_CST
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1868 || !useless_type_conversion_p (type, TREE_TYPE (arg)))
111
kono
parents:
diff changeset
1869 return format_floating (dir, prec);
kono
parents:
diff changeset
1870
kono
parents:
diff changeset
1871 /* The minimum and maximum number of bytes produced by the directive. */
kono
parents:
diff changeset
1872 fmtresult res;
kono
parents:
diff changeset
1873
kono
parents:
diff changeset
1874 /* Get the real type format desription for the target. */
kono
parents:
diff changeset
1875 const REAL_VALUE_TYPE *rvp = TREE_REAL_CST_PTR (arg);
kono
parents:
diff changeset
1876 const real_format *rfmt = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (arg)));
kono
parents:
diff changeset
1877
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1878 if (!real_isfinite (rvp))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1879 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1880 /* The format for Infinity and NaN is "[-]inf"/"[-]infinity"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1881 and "[-]nan" with the choice being implementation-defined
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1882 but not locale dependent. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1883 bool sign = dir.get_flag ('+') || real_isneg (rvp);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1884 res.range.min = 3 + sign;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1885
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1886 res.range.likely = res.range.min;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1887 res.range.max = res.range.min;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1888 /* The unlikely maximum is "[-/+]infinity" or "[-/+][qs]nan".
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1889 For NaN, the C/POSIX standards specify two formats:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1890 "[-/+]nan"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1891 and
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1892 "[-/+]nan(n-char-sequence)"
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1893 No known printf implementation outputs the latter format but AIX
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1894 outputs QNaN and SNaN for quiet and signalling NaN, respectively,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1895 so the unlikely maximum reflects that. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1896 res.range.unlikely = sign + (real_isinf (rvp) ? 8 : 4);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1897
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1898 /* The range for infinity and NaN is known unless either width
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1899 or precision is unknown. Width has the same effect regardless
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1900 of whether the argument is finite. Precision is either ignored
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1901 (e.g., Glibc) or can have an effect on the short vs long format
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1902 such as inf/infinity (e.g., Solaris). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1903 res.knownrange = dir.known_width_and_precision ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1904
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1905 /* Adjust the range for width but ignore precision. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1906 res.adjust_for_width_or_precision (dir.width);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1907
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1908 return res;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1909 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1910
111
kono
parents:
diff changeset
1911 char fmtstr [40];
kono
parents:
diff changeset
1912 char *pfmt = fmtstr;
kono
parents:
diff changeset
1913
kono
parents:
diff changeset
1914 /* Append flags. */
kono
parents:
diff changeset
1915 for (const char *pf = "-+ #0"; *pf; ++pf)
kono
parents:
diff changeset
1916 if (dir.get_flag (*pf))
kono
parents:
diff changeset
1917 *pfmt++ = *pf;
kono
parents:
diff changeset
1918
kono
parents:
diff changeset
1919 *pfmt = '\0';
kono
parents:
diff changeset
1920
kono
parents:
diff changeset
1921 {
kono
parents:
diff changeset
1922 /* Set up an array to easily iterate over. */
kono
parents:
diff changeset
1923 unsigned HOST_WIDE_INT* const minmax[] = {
kono
parents:
diff changeset
1924 &res.range.min, &res.range.max
kono
parents:
diff changeset
1925 };
kono
parents:
diff changeset
1926
kono
parents:
diff changeset
1927 for (int i = 0; i != sizeof minmax / sizeof *minmax; ++i)
kono
parents:
diff changeset
1928 {
kono
parents:
diff changeset
1929 /* Convert the GCC real value representation with the precision
kono
parents:
diff changeset
1930 of the real type to the mpfr_t format rounding down in the
kono
parents:
diff changeset
1931 first iteration that computes the minimm and up in the second
kono
parents:
diff changeset
1932 that computes the maximum. This order is arbibtrary because
kono
parents:
diff changeset
1933 rounding in either direction can result in longer output. */
kono
parents:
diff changeset
1934 mpfr_t mpfrval;
kono
parents:
diff changeset
1935 mpfr_init2 (mpfrval, rfmt->p);
kono
parents:
diff changeset
1936 mpfr_from_real (mpfrval, rvp, i ? GMP_RNDU : GMP_RNDD);
kono
parents:
diff changeset
1937
kono
parents:
diff changeset
1938 /* Use the MPFR rounding specifier to round down in the first
kono
parents:
diff changeset
1939 iteration and then up. In most but not all cases this will
kono
parents:
diff changeset
1940 result in the same number of bytes. */
kono
parents:
diff changeset
1941 char rndspec = "DU"[i];
kono
parents:
diff changeset
1942
kono
parents:
diff changeset
1943 /* Format it and store the result in the corresponding member
kono
parents:
diff changeset
1944 of the result struct. */
kono
parents:
diff changeset
1945 *minmax[i] = get_mpfr_format_length (mpfrval, fmtstr, prec[i],
kono
parents:
diff changeset
1946 dir.specifier, rndspec);
kono
parents:
diff changeset
1947 mpfr_clear (mpfrval);
kono
parents:
diff changeset
1948 }
kono
parents:
diff changeset
1949 }
kono
parents:
diff changeset
1950
kono
parents:
diff changeset
1951 /* Make sure the minimum is less than the maximum (MPFR rounding
kono
parents:
diff changeset
1952 in the call to mpfr_snprintf can result in the reverse. */
kono
parents:
diff changeset
1953 if (res.range.max < res.range.min)
kono
parents:
diff changeset
1954 {
kono
parents:
diff changeset
1955 unsigned HOST_WIDE_INT tmp = res.range.min;
kono
parents:
diff changeset
1956 res.range.min = res.range.max;
kono
parents:
diff changeset
1957 res.range.max = tmp;
kono
parents:
diff changeset
1958 }
kono
parents:
diff changeset
1959
kono
parents:
diff changeset
1960 /* The range is known unless either width or precision is unknown. */
kono
parents:
diff changeset
1961 res.knownrange = dir.known_width_and_precision ();
kono
parents:
diff changeset
1962
kono
parents:
diff changeset
1963 /* For the same floating point constant, unless width or precision
kono
parents:
diff changeset
1964 is unknown, use the longer output as the likely maximum since
kono
parents:
diff changeset
1965 with round to nearest either is equally likely. Otheriwse, when
kono
parents:
diff changeset
1966 precision is unknown, use the greater of the minimum and 3 as
kono
parents:
diff changeset
1967 the likely output (for "0.0" since zero precision is unlikely). */
kono
parents:
diff changeset
1968 if (res.knownrange)
kono
parents:
diff changeset
1969 res.range.likely = res.range.max;
kono
parents:
diff changeset
1970 else if (res.range.min < 3
kono
parents:
diff changeset
1971 && dir.prec[0] < 0
kono
parents:
diff changeset
1972 && (unsigned HOST_WIDE_INT)dir.prec[1] == target_int_max ())
kono
parents:
diff changeset
1973 res.range.likely = 3;
kono
parents:
diff changeset
1974 else
kono
parents:
diff changeset
1975 res.range.likely = res.range.min;
kono
parents:
diff changeset
1976
kono
parents:
diff changeset
1977 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
1978
kono
parents:
diff changeset
1979 if (res.range.max > 2 && (prec[0] != 0 || prec[1] != 0))
kono
parents:
diff changeset
1980 {
kono
parents:
diff changeset
1981 /* Unless the precision is zero output longer than 2 bytes may
kono
parents:
diff changeset
1982 include the decimal point which must be a single character
kono
parents:
diff changeset
1983 up to MB_LEN_MAX in length. This is overly conservative
kono
parents:
diff changeset
1984 since in some conversions some constants result in no decimal
kono
parents:
diff changeset
1985 point (e.g., in %g). */
kono
parents:
diff changeset
1986 res.range.unlikely += target_mb_len_max () - 1;
kono
parents:
diff changeset
1987 }
kono
parents:
diff changeset
1988
kono
parents:
diff changeset
1989 res.adjust_for_width_or_precision (dir.width);
kono
parents:
diff changeset
1990 return res;
kono
parents:
diff changeset
1991 }
kono
parents:
diff changeset
1992
kono
parents:
diff changeset
1993 /* Return a FMTRESULT struct set to the lengths of the shortest and longest
kono
parents:
diff changeset
1994 strings referenced by the expression STR, or (-1, -1) when not known.
kono
parents:
diff changeset
1995 Used by the format_string function below. */
kono
parents:
diff changeset
1996
kono
parents:
diff changeset
1997 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1998 get_string_length (tree str, unsigned eltsize)
111
kono
parents:
diff changeset
1999 {
kono
parents:
diff changeset
2000 if (!str)
kono
parents:
diff changeset
2001 return fmtresult ();
kono
parents:
diff changeset
2002
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2003 c_strlen_data data;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2004 memset (&data, 0, sizeof (c_strlen_data));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2005 tree slen = c_strlen (str, 1, &data, eltsize);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2006 if (slen && TREE_CODE (slen) == INTEGER_CST)
111
kono
parents:
diff changeset
2007 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2008 /* The string is properly terminated and
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2009 we know its length. */
111
kono
parents:
diff changeset
2010 fmtresult res (tree_to_shwi (slen));
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2011 res.nonstr = NULL_TREE;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2012 return res;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2013 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2014 else if (!slen
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2015 && data.decl
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2016 && data.len
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2017 && TREE_CODE (data.len) == INTEGER_CST)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2018 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2019 /* STR was not properly NUL terminated, but we have
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2020 length information about the unterminated string. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2021 fmtresult res (tree_to_shwi (data.len));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2022 res.nonstr = data.decl;
111
kono
parents:
diff changeset
2023 return res;
kono
parents:
diff changeset
2024 }
kono
parents:
diff changeset
2025
kono
parents:
diff changeset
2026 /* Determine the length of the shortest and longest string referenced
kono
parents:
diff changeset
2027 by STR. Strings of unknown lengths are bounded by the sizes of
kono
parents:
diff changeset
2028 arrays that subexpressions of STR may refer to. Pointers that
kono
parents:
diff changeset
2029 aren't known to point any such arrays result in LENRANGE[1] set
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2030 to SIZE_MAX. NONSTR is set to the declaration of the constant
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2031 array that is known not to be nul-terminated. */
111
kono
parents:
diff changeset
2032 tree lenrange[2];
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2033 tree nonstr;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2034 bool flexarray = get_range_strlen (str, lenrange, eltsize, false, &nonstr);
111
kono
parents:
diff changeset
2035
kono
parents:
diff changeset
2036 if (lenrange [0] || lenrange [1])
kono
parents:
diff changeset
2037 {
kono
parents:
diff changeset
2038 HOST_WIDE_INT min
kono
parents:
diff changeset
2039 = (tree_fits_uhwi_p (lenrange[0])
kono
parents:
diff changeset
2040 ? tree_to_uhwi (lenrange[0])
kono
parents:
diff changeset
2041 : 0);
kono
parents:
diff changeset
2042
kono
parents:
diff changeset
2043 HOST_WIDE_INT max
kono
parents:
diff changeset
2044 = (tree_fits_uhwi_p (lenrange[1])
kono
parents:
diff changeset
2045 ? tree_to_uhwi (lenrange[1])
kono
parents:
diff changeset
2046 : HOST_WIDE_INT_M1U);
kono
parents:
diff changeset
2047
kono
parents:
diff changeset
2048 /* get_range_strlen() returns the target value of SIZE_MAX for
kono
parents:
diff changeset
2049 strings of unknown length. Bump it up to HOST_WIDE_INT_M1U
kono
parents:
diff changeset
2050 which may be bigger. */
kono
parents:
diff changeset
2051 if ((unsigned HOST_WIDE_INT)min == target_size_max ())
kono
parents:
diff changeset
2052 min = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2053 if ((unsigned HOST_WIDE_INT)max == target_size_max ())
kono
parents:
diff changeset
2054 max = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2055
kono
parents:
diff changeset
2056 fmtresult res (min, max);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2057 res.nonstr = nonstr;
111
kono
parents:
diff changeset
2058
kono
parents:
diff changeset
2059 /* Set RES.KNOWNRANGE to true if and only if all strings referenced
kono
parents:
diff changeset
2060 by STR are known to be bounded (though not necessarily by their
kono
parents:
diff changeset
2061 actual length but perhaps by their maximum possible length). */
kono
parents:
diff changeset
2062 if (res.range.max < target_int_max ())
kono
parents:
diff changeset
2063 {
kono
parents:
diff changeset
2064 res.knownrange = true;
kono
parents:
diff changeset
2065 /* When the the length of the longest string is known and not
kono
parents:
diff changeset
2066 excessive use it as the likely length of the string(s). */
kono
parents:
diff changeset
2067 res.range.likely = res.range.max;
kono
parents:
diff changeset
2068 }
kono
parents:
diff changeset
2069 else
kono
parents:
diff changeset
2070 {
kono
parents:
diff changeset
2071 /* When the upper bound is unknown (it can be zero or excessive)
kono
parents:
diff changeset
2072 set the likely length to the greater of 1 and the length of
kono
parents:
diff changeset
2073 the shortest string and reset the lower bound to zero. */
kono
parents:
diff changeset
2074 res.range.likely = res.range.min ? res.range.min : warn_level > 1;
kono
parents:
diff changeset
2075 res.range.min = 0;
kono
parents:
diff changeset
2076 }
kono
parents:
diff changeset
2077
kono
parents:
diff changeset
2078 /* If the range of string length has been estimated from the size
kono
parents:
diff changeset
2079 of an array at the end of a struct assume that it's longer than
kono
parents:
diff changeset
2080 the array bound says it is in case it's used as a poor man's
kono
parents:
diff changeset
2081 flexible array member, such as in struct S { char a[4]; }; */
kono
parents:
diff changeset
2082 res.range.unlikely = flexarray ? HOST_WIDE_INT_MAX : res.range.max;
kono
parents:
diff changeset
2083
kono
parents:
diff changeset
2084 return res;
kono
parents:
diff changeset
2085 }
kono
parents:
diff changeset
2086
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2087 return fmtresult ();
111
kono
parents:
diff changeset
2088 }
kono
parents:
diff changeset
2089
kono
parents:
diff changeset
2090 /* Return the minimum and maximum number of characters formatted
kono
parents:
diff changeset
2091 by the '%c' format directives and its wide character form for
kono
parents:
diff changeset
2092 the argument ARG. ARG can be null (for functions such as
kono
parents:
diff changeset
2093 vsprinf). */
kono
parents:
diff changeset
2094
kono
parents:
diff changeset
2095 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2096 format_character (const directive &dir, tree arg, vr_values *vr_values)
111
kono
parents:
diff changeset
2097 {
kono
parents:
diff changeset
2098 fmtresult res;
kono
parents:
diff changeset
2099
kono
parents:
diff changeset
2100 res.knownrange = true;
kono
parents:
diff changeset
2101
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2102 if (dir.specifier == 'C'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2103 || dir.modifier == FMT_LEN_l)
111
kono
parents:
diff changeset
2104 {
kono
parents:
diff changeset
2105 /* A wide character can result in as few as zero bytes. */
kono
parents:
diff changeset
2106 res.range.min = 0;
kono
parents:
diff changeset
2107
kono
parents:
diff changeset
2108 HOST_WIDE_INT min, max;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2109 if (get_int_range (arg, &min, &max, false, 0, vr_values))
111
kono
parents:
diff changeset
2110 {
kono
parents:
diff changeset
2111 if (min == 0 && max == 0)
kono
parents:
diff changeset
2112 {
kono
parents:
diff changeset
2113 /* The NUL wide character results in no bytes. */
kono
parents:
diff changeset
2114 res.range.max = 0;
kono
parents:
diff changeset
2115 res.range.likely = 0;
kono
parents:
diff changeset
2116 res.range.unlikely = 0;
kono
parents:
diff changeset
2117 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2118 else if (min >= 0 && min < 128)
111
kono
parents:
diff changeset
2119 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2120 /* Be conservative if the target execution character set
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2121 is not a 1-to-1 mapping to the source character set or
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2122 if the source set is not ASCII. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2123 bool one_2_one_ascii
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2124 = (target_to_host_charmap[0] == 1 && target_to_host ('a') == 97);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2125
111
kono
parents:
diff changeset
2126 /* A wide character in the ASCII range most likely results
kono
parents:
diff changeset
2127 in a single byte, and only unlikely in up to MB_LEN_MAX. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2128 res.range.max = one_2_one_ascii ? 1 : target_mb_len_max ();;
111
kono
parents:
diff changeset
2129 res.range.likely = 1;
kono
parents:
diff changeset
2130 res.range.unlikely = target_mb_len_max ();
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2131 res.mayfail = !one_2_one_ascii;
111
kono
parents:
diff changeset
2132 }
kono
parents:
diff changeset
2133 else
kono
parents:
diff changeset
2134 {
kono
parents:
diff changeset
2135 /* A wide character outside the ASCII range likely results
kono
parents:
diff changeset
2136 in up to two bytes, and only unlikely in up to MB_LEN_MAX. */
kono
parents:
diff changeset
2137 res.range.max = target_mb_len_max ();
kono
parents:
diff changeset
2138 res.range.likely = 2;
kono
parents:
diff changeset
2139 res.range.unlikely = res.range.max;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2140 /* Converting such a character may fail. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2141 res.mayfail = true;
111
kono
parents:
diff changeset
2142 }
kono
parents:
diff changeset
2143 }
kono
parents:
diff changeset
2144 else
kono
parents:
diff changeset
2145 {
kono
parents:
diff changeset
2146 /* An unknown wide character is treated the same as a wide
kono
parents:
diff changeset
2147 character outside the ASCII range. */
kono
parents:
diff changeset
2148 res.range.max = target_mb_len_max ();
kono
parents:
diff changeset
2149 res.range.likely = 2;
kono
parents:
diff changeset
2150 res.range.unlikely = res.range.max;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2151 res.mayfail = true;
111
kono
parents:
diff changeset
2152 }
kono
parents:
diff changeset
2153 }
kono
parents:
diff changeset
2154 else
kono
parents:
diff changeset
2155 {
kono
parents:
diff changeset
2156 /* A plain '%c' directive. Its ouput is exactly 1. */
kono
parents:
diff changeset
2157 res.range.min = res.range.max = 1;
kono
parents:
diff changeset
2158 res.range.likely = res.range.unlikely = 1;
kono
parents:
diff changeset
2159 res.knownrange = true;
kono
parents:
diff changeset
2160 }
kono
parents:
diff changeset
2161
kono
parents:
diff changeset
2162 /* Bump up the byte counters if WIDTH is greater. */
kono
parents:
diff changeset
2163 return res.adjust_for_width_or_precision (dir.width);
kono
parents:
diff changeset
2164 }
kono
parents:
diff changeset
2165
kono
parents:
diff changeset
2166 /* Return the minimum and maximum number of characters formatted
kono
parents:
diff changeset
2167 by the '%s' format directive and its wide character form for
kono
parents:
diff changeset
2168 the argument ARG. ARG can be null (for functions such as
kono
parents:
diff changeset
2169 vsprinf). */
kono
parents:
diff changeset
2170
kono
parents:
diff changeset
2171 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2172 format_string (const directive &dir, tree arg, vr_values *)
111
kono
parents:
diff changeset
2173 {
kono
parents:
diff changeset
2174 fmtresult res;
kono
parents:
diff changeset
2175
kono
parents:
diff changeset
2176 /* Compute the range the argument's length can be in. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2177 int count_by = 1;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2178 if (dir.specifier == 'S' || dir.modifier == FMT_LEN_l)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2179 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2180 /* Get a node for a C type that will be the same size
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2181 as a wchar_t on the target. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2182 tree node = get_typenode_from_name (MODIFIED_WCHAR_TYPE);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2183
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2184 /* Now that we have a suitable node, get the number of
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2185 bytes it occupies. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2186 count_by = int_size_in_bytes (node);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2187 gcc_checking_assert (count_by == 2 || count_by == 4);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2188 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2189
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2190 fmtresult slen = get_string_length (arg, count_by);
111
kono
parents:
diff changeset
2191 if (slen.range.min == slen.range.max
kono
parents:
diff changeset
2192 && slen.range.min < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
2193 {
kono
parents:
diff changeset
2194 /* The argument is either a string constant or it refers
kono
parents:
diff changeset
2195 to one of a number of strings of the same length. */
kono
parents:
diff changeset
2196
kono
parents:
diff changeset
2197 /* A '%s' directive with a string argument with constant length. */
kono
parents:
diff changeset
2198 res.range = slen.range;
kono
parents:
diff changeset
2199
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2200 if (dir.specifier == 'S'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2201 || dir.modifier == FMT_LEN_l)
111
kono
parents:
diff changeset
2202 {
kono
parents:
diff changeset
2203 /* In the worst case the length of output of a wide string S
kono
parents:
diff changeset
2204 is bounded by MB_LEN_MAX * wcslen (S). */
kono
parents:
diff changeset
2205 res.range.max *= target_mb_len_max ();
kono
parents:
diff changeset
2206 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
2207 /* It's likely that the the total length is not more that
kono
parents:
diff changeset
2208 2 * wcslen (S).*/
kono
parents:
diff changeset
2209 res.range.likely = res.range.min * 2;
kono
parents:
diff changeset
2210
kono
parents:
diff changeset
2211 if (dir.prec[1] >= 0
kono
parents:
diff changeset
2212 && (unsigned HOST_WIDE_INT)dir.prec[1] < res.range.max)
kono
parents:
diff changeset
2213 {
kono
parents:
diff changeset
2214 res.range.max = dir.prec[1];
kono
parents:
diff changeset
2215 res.range.likely = dir.prec[1];
kono
parents:
diff changeset
2216 res.range.unlikely = dir.prec[1];
kono
parents:
diff changeset
2217 }
kono
parents:
diff changeset
2218
kono
parents:
diff changeset
2219 if (dir.prec[0] < 0 && dir.prec[1] > -1)
kono
parents:
diff changeset
2220 res.range.min = 0;
kono
parents:
diff changeset
2221 else if (dir.prec[0] >= 0)
kono
parents:
diff changeset
2222 res.range.likely = dir.prec[0];
kono
parents:
diff changeset
2223
kono
parents:
diff changeset
2224 /* Even a non-empty wide character string need not convert into
kono
parents:
diff changeset
2225 any bytes. */
kono
parents:
diff changeset
2226 res.range.min = 0;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2227
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2228 /* A non-empty wide character conversion may fail. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2229 if (slen.range.max > 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2230 res.mayfail = true;
111
kono
parents:
diff changeset
2231 }
kono
parents:
diff changeset
2232 else
kono
parents:
diff changeset
2233 {
kono
parents:
diff changeset
2234 res.knownrange = true;
kono
parents:
diff changeset
2235
kono
parents:
diff changeset
2236 if (dir.prec[0] < 0 && dir.prec[1] > -1)
kono
parents:
diff changeset
2237 res.range.min = 0;
kono
parents:
diff changeset
2238 else if ((unsigned HOST_WIDE_INT)dir.prec[0] < res.range.min)
kono
parents:
diff changeset
2239 res.range.min = dir.prec[0];
kono
parents:
diff changeset
2240
kono
parents:
diff changeset
2241 if ((unsigned HOST_WIDE_INT)dir.prec[1] < res.range.max)
kono
parents:
diff changeset
2242 {
kono
parents:
diff changeset
2243 res.range.max = dir.prec[1];
kono
parents:
diff changeset
2244 res.range.likely = dir.prec[1];
kono
parents:
diff changeset
2245 res.range.unlikely = dir.prec[1];
kono
parents:
diff changeset
2246 }
kono
parents:
diff changeset
2247 }
kono
parents:
diff changeset
2248 }
kono
parents:
diff changeset
2249 else if (arg && integer_zerop (arg))
kono
parents:
diff changeset
2250 {
kono
parents:
diff changeset
2251 /* Handle null pointer argument. */
kono
parents:
diff changeset
2252
kono
parents:
diff changeset
2253 fmtresult res (0);
kono
parents:
diff changeset
2254 res.nullp = true;
kono
parents:
diff changeset
2255 return res;
kono
parents:
diff changeset
2256 }
kono
parents:
diff changeset
2257 else
kono
parents:
diff changeset
2258 {
kono
parents:
diff changeset
2259 /* For a '%s' and '%ls' directive with a non-constant string (either
kono
parents:
diff changeset
2260 one of a number of strings of known length or an unknown string)
kono
parents:
diff changeset
2261 the minimum number of characters is lesser of PRECISION[0] and
kono
parents:
diff changeset
2262 the length of the shortest known string or zero, and the maximum
kono
parents:
diff changeset
2263 is the lessser of the length of the longest known string or
kono
parents:
diff changeset
2264 PTRDIFF_MAX and PRECISION[1]. The likely length is either
kono
parents:
diff changeset
2265 the minimum at level 1 and the greater of the minimum and 1
kono
parents:
diff changeset
2266 at level 2. This result is adjust upward for width (if it's
kono
parents:
diff changeset
2267 specified). */
kono
parents:
diff changeset
2268
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2269 if (dir.specifier == 'S'
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2270 || dir.modifier == FMT_LEN_l)
111
kono
parents:
diff changeset
2271 {
kono
parents:
diff changeset
2272 /* A wide character converts to as few as zero bytes. */
kono
parents:
diff changeset
2273 slen.range.min = 0;
kono
parents:
diff changeset
2274 if (slen.range.max < target_int_max ())
kono
parents:
diff changeset
2275 slen.range.max *= target_mb_len_max ();
kono
parents:
diff changeset
2276
kono
parents:
diff changeset
2277 if (slen.range.likely < target_int_max ())
kono
parents:
diff changeset
2278 slen.range.likely *= 2;
kono
parents:
diff changeset
2279
kono
parents:
diff changeset
2280 if (slen.range.likely < target_int_max ())
kono
parents:
diff changeset
2281 slen.range.unlikely *= target_mb_len_max ();
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2282
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2283 /* A non-empty wide character conversion may fail. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2284 if (slen.range.max > 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2285 res.mayfail = true;
111
kono
parents:
diff changeset
2286 }
kono
parents:
diff changeset
2287
kono
parents:
diff changeset
2288 res.range = slen.range;
kono
parents:
diff changeset
2289
kono
parents:
diff changeset
2290 if (dir.prec[0] >= 0)
kono
parents:
diff changeset
2291 {
kono
parents:
diff changeset
2292 /* Adjust the minimum to zero if the string length is unknown,
kono
parents:
diff changeset
2293 or at most the lower bound of the precision otherwise. */
kono
parents:
diff changeset
2294 if (slen.range.min >= target_int_max ())
kono
parents:
diff changeset
2295 res.range.min = 0;
kono
parents:
diff changeset
2296 else if ((unsigned HOST_WIDE_INT)dir.prec[0] < slen.range.min)
kono
parents:
diff changeset
2297 res.range.min = dir.prec[0];
kono
parents:
diff changeset
2298
kono
parents:
diff changeset
2299 /* Make both maxima no greater than the upper bound of precision. */
kono
parents:
diff changeset
2300 if ((unsigned HOST_WIDE_INT)dir.prec[1] < slen.range.max
kono
parents:
diff changeset
2301 || slen.range.max >= target_int_max ())
kono
parents:
diff changeset
2302 {
kono
parents:
diff changeset
2303 res.range.max = dir.prec[1];
kono
parents:
diff changeset
2304 res.range.unlikely = dir.prec[1];
kono
parents:
diff changeset
2305 }
kono
parents:
diff changeset
2306
kono
parents:
diff changeset
2307 /* If precision is constant, set the likely counter to the lesser
kono
parents:
diff changeset
2308 of it and the maximum string length. Otherwise, if the lower
kono
parents:
diff changeset
2309 bound of precision is greater than zero, set the likely counter
kono
parents:
diff changeset
2310 to the minimum. Otherwise set it to zero or one based on
kono
parents:
diff changeset
2311 the warning level. */
kono
parents:
diff changeset
2312 if (dir.prec[0] == dir.prec[1])
kono
parents:
diff changeset
2313 res.range.likely
kono
parents:
diff changeset
2314 = ((unsigned HOST_WIDE_INT)dir.prec[0] < slen.range.max
kono
parents:
diff changeset
2315 ? dir.prec[0] : slen.range.max);
kono
parents:
diff changeset
2316 else if (dir.prec[0] > 0)
kono
parents:
diff changeset
2317 res.range.likely = res.range.min;
kono
parents:
diff changeset
2318 else
kono
parents:
diff changeset
2319 res.range.likely = warn_level > 1;
kono
parents:
diff changeset
2320 }
kono
parents:
diff changeset
2321 else if (dir.prec[1] >= 0)
kono
parents:
diff changeset
2322 {
kono
parents:
diff changeset
2323 res.range.min = 0;
kono
parents:
diff changeset
2324 if ((unsigned HOST_WIDE_INT)dir.prec[1] < slen.range.max)
kono
parents:
diff changeset
2325 res.range.max = dir.prec[1];
kono
parents:
diff changeset
2326 res.range.likely = dir.prec[1] ? warn_level > 1 : 0;
kono
parents:
diff changeset
2327 }
kono
parents:
diff changeset
2328 else if (slen.range.min >= target_int_max ())
kono
parents:
diff changeset
2329 {
kono
parents:
diff changeset
2330 res.range.min = 0;
kono
parents:
diff changeset
2331 res.range.max = HOST_WIDE_INT_MAX;
kono
parents:
diff changeset
2332 /* At level 1 strings of unknown length are assumed to be
kono
parents:
diff changeset
2333 empty, while at level 1 they are assumed to be one byte
kono
parents:
diff changeset
2334 long. */
kono
parents:
diff changeset
2335 res.range.likely = warn_level > 1;
kono
parents:
diff changeset
2336 }
kono
parents:
diff changeset
2337 else
kono
parents:
diff changeset
2338 {
kono
parents:
diff changeset
2339 /* A string of unknown length unconstrained by precision is
kono
parents:
diff changeset
2340 assumed to be empty at level 1 and just one character long
kono
parents:
diff changeset
2341 at higher levels. */
kono
parents:
diff changeset
2342 if (res.range.likely >= target_int_max ())
kono
parents:
diff changeset
2343 res.range.likely = warn_level > 1;
kono
parents:
diff changeset
2344 }
kono
parents:
diff changeset
2345
kono
parents:
diff changeset
2346 res.range.unlikely = res.range.max;
kono
parents:
diff changeset
2347 }
kono
parents:
diff changeset
2348
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2349 /* If the argument isn't a nul-terminated string and the number
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2350 of bytes on output isn't bounded by precision, set NONSTR. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2351 if (slen.nonstr && slen.range.min < (unsigned HOST_WIDE_INT)dir.prec[0])
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2352 res.nonstr = slen.nonstr;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2353
111
kono
parents:
diff changeset
2354 /* Bump up the byte counters if WIDTH is greater. */
kono
parents:
diff changeset
2355 return res.adjust_for_width_or_precision (dir.width);
kono
parents:
diff changeset
2356 }
kono
parents:
diff changeset
2357
kono
parents:
diff changeset
2358 /* Format plain string (part of the format string itself). */
kono
parents:
diff changeset
2359
kono
parents:
diff changeset
2360 static fmtresult
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2361 format_plain (const directive &dir, tree, vr_values *)
111
kono
parents:
diff changeset
2362 {
kono
parents:
diff changeset
2363 fmtresult res (dir.len);
kono
parents:
diff changeset
2364 return res;
kono
parents:
diff changeset
2365 }
kono
parents:
diff changeset
2366
kono
parents:
diff changeset
2367 /* Return true if the RESULT of a directive in a call describe by INFO
kono
parents:
diff changeset
2368 should be diagnosed given the AVAILable space in the destination. */
kono
parents:
diff changeset
2369
kono
parents:
diff changeset
2370 static bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2371 should_warn_p (const sprintf_dom_walker::call_info &info,
111
kono
parents:
diff changeset
2372 const result_range &avail, const result_range &result)
kono
parents:
diff changeset
2373 {
kono
parents:
diff changeset
2374 if (result.max <= avail.min)
kono
parents:
diff changeset
2375 {
kono
parents:
diff changeset
2376 /* The least amount of space remaining in the destination is big
kono
parents:
diff changeset
2377 enough for the longest output. */
kono
parents:
diff changeset
2378 return false;
kono
parents:
diff changeset
2379 }
kono
parents:
diff changeset
2380
kono
parents:
diff changeset
2381 if (info.bounded)
kono
parents:
diff changeset
2382 {
kono
parents:
diff changeset
2383 if (warn_format_trunc == 1 && result.min <= avail.max
kono
parents:
diff changeset
2384 && info.retval_used ())
kono
parents:
diff changeset
2385 {
kono
parents:
diff changeset
2386 /* The likely amount of space remaining in the destination is big
kono
parents:
diff changeset
2387 enough for the least output and the return value is used. */
kono
parents:
diff changeset
2388 return false;
kono
parents:
diff changeset
2389 }
kono
parents:
diff changeset
2390
kono
parents:
diff changeset
2391 if (warn_format_trunc == 1 && result.likely <= avail.likely
kono
parents:
diff changeset
2392 && !info.retval_used ())
kono
parents:
diff changeset
2393 {
kono
parents:
diff changeset
2394 /* The likely amount of space remaining in the destination is big
kono
parents:
diff changeset
2395 enough for the likely output and the return value is unused. */
kono
parents:
diff changeset
2396 return false;
kono
parents:
diff changeset
2397 }
kono
parents:
diff changeset
2398
kono
parents:
diff changeset
2399 if (warn_format_trunc == 2
kono
parents:
diff changeset
2400 && result.likely <= avail.min
kono
parents:
diff changeset
2401 && (result.max <= avail.min
kono
parents:
diff changeset
2402 || result.max > HOST_WIDE_INT_MAX))
kono
parents:
diff changeset
2403 {
kono
parents:
diff changeset
2404 /* The minimum amount of space remaining in the destination is big
kono
parents:
diff changeset
2405 enough for the longest output. */
kono
parents:
diff changeset
2406 return false;
kono
parents:
diff changeset
2407 }
kono
parents:
diff changeset
2408 }
kono
parents:
diff changeset
2409 else
kono
parents:
diff changeset
2410 {
kono
parents:
diff changeset
2411 if (warn_level == 1 && result.likely <= avail.likely)
kono
parents:
diff changeset
2412 {
kono
parents:
diff changeset
2413 /* The likely amount of space remaining in the destination is big
kono
parents:
diff changeset
2414 enough for the likely output. */
kono
parents:
diff changeset
2415 return false;
kono
parents:
diff changeset
2416 }
kono
parents:
diff changeset
2417
kono
parents:
diff changeset
2418 if (warn_level == 2
kono
parents:
diff changeset
2419 && result.likely <= avail.min
kono
parents:
diff changeset
2420 && (result.max <= avail.min
kono
parents:
diff changeset
2421 || result.max > HOST_WIDE_INT_MAX))
kono
parents:
diff changeset
2422 {
kono
parents:
diff changeset
2423 /* The minimum amount of space remaining in the destination is big
kono
parents:
diff changeset
2424 enough for the longest output. */
kono
parents:
diff changeset
2425 return false;
kono
parents:
diff changeset
2426 }
kono
parents:
diff changeset
2427 }
kono
parents:
diff changeset
2428
kono
parents:
diff changeset
2429 return true;
kono
parents:
diff changeset
2430 }
kono
parents:
diff changeset
2431
kono
parents:
diff changeset
2432 /* At format string location describe by DIRLOC in a call described
kono
parents:
diff changeset
2433 by INFO, issue a warning for a directive DIR whose output may be
kono
parents:
diff changeset
2434 in excess of the available space AVAIL_RANGE in the destination
kono
parents:
diff changeset
2435 given the formatting result FMTRES. This function does nothing
kono
parents:
diff changeset
2436 except decide whether to issue a warning for a possible write
kono
parents:
diff changeset
2437 past the end or truncation and, if so, format the warning.
kono
parents:
diff changeset
2438 Return true if a warning has been issued. */
kono
parents:
diff changeset
2439
kono
parents:
diff changeset
2440 static bool
kono
parents:
diff changeset
2441 maybe_warn (substring_loc &dirloc, location_t argloc,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2442 const sprintf_dom_walker::call_info &info,
111
kono
parents:
diff changeset
2443 const result_range &avail_range, const result_range &res,
kono
parents:
diff changeset
2444 const directive &dir)
kono
parents:
diff changeset
2445 {
kono
parents:
diff changeset
2446 if (!should_warn_p (info, avail_range, res))
kono
parents:
diff changeset
2447 return false;
kono
parents:
diff changeset
2448
kono
parents:
diff changeset
2449 /* A warning will definitely be issued below. */
kono
parents:
diff changeset
2450
kono
parents:
diff changeset
2451 /* The maximum byte count to reference in the warning. Larger counts
kono
parents:
diff changeset
2452 imply that the upper bound is unknown (and could be anywhere between
kono
parents:
diff changeset
2453 RES.MIN + 1 and SIZE_MAX / 2) are printed as "N or more bytes" rather
kono
parents:
diff changeset
2454 than "between N and X" where X is some huge number. */
kono
parents:
diff changeset
2455 unsigned HOST_WIDE_INT maxbytes = target_dir_max ();
kono
parents:
diff changeset
2456
kono
parents:
diff changeset
2457 /* True when there is enough room in the destination for the least
kono
parents:
diff changeset
2458 amount of a directive's output but not enough for its likely or
kono
parents:
diff changeset
2459 maximum output. */
kono
parents:
diff changeset
2460 bool maybe = (res.min <= avail_range.max
kono
parents:
diff changeset
2461 && (avail_range.min < res.likely
kono
parents:
diff changeset
2462 || (res.max < HOST_WIDE_INT_MAX
kono
parents:
diff changeset
2463 && avail_range.min < res.max)));
kono
parents:
diff changeset
2464
kono
parents:
diff changeset
2465 /* Buffer for the directive in the host character set (used when
kono
parents:
diff changeset
2466 the source character set is different). */
kono
parents:
diff changeset
2467 char hostdir[32];
kono
parents:
diff changeset
2468
kono
parents:
diff changeset
2469 if (avail_range.min == avail_range.max)
kono
parents:
diff changeset
2470 {
kono
parents:
diff changeset
2471 /* The size of the destination region is exact. */
kono
parents:
diff changeset
2472 unsigned HOST_WIDE_INT navail = avail_range.max;
kono
parents:
diff changeset
2473
kono
parents:
diff changeset
2474 if (target_to_host (*dir.beg) != '%')
kono
parents:
diff changeset
2475 {
kono
parents:
diff changeset
2476 /* For plain character directives (i.e., the format string itself)
kono
parents:
diff changeset
2477 but not others, point the caret at the first character that's
kono
parents:
diff changeset
2478 past the end of the destination. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2479 if (navail < dir.len)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2480 dirloc.set_caret_index (dirloc.get_caret_idx () + navail);
111
kono
parents:
diff changeset
2481 }
kono
parents:
diff changeset
2482
kono
parents:
diff changeset
2483 if (*dir.beg == '\0')
kono
parents:
diff changeset
2484 {
kono
parents:
diff changeset
2485 /* This is the terminating nul. */
kono
parents:
diff changeset
2486 gcc_assert (res.min == 1 && res.min == res.max);
kono
parents:
diff changeset
2487
kono
parents:
diff changeset
2488 return fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2489 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2490 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2491 ? G_("%qE output may be truncated before the "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2492 "last format character")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2493 : G_("%qE output truncated before the last "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2494 "format character"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2495 : (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2496 ? G_("%qE may write a terminating nul past the "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2497 "end of the destination")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2498 : G_("%qE writing a terminating nul past the "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2499 "end of the destination")),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2500 info.func);
111
kono
parents:
diff changeset
2501 }
kono
parents:
diff changeset
2502
kono
parents:
diff changeset
2503 if (res.min == res.max)
kono
parents:
diff changeset
2504 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2505 const char *d = target_to_host (hostdir, sizeof hostdir, dir.beg);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2506 if (!info.bounded)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2507 return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2508 "%<%.*s%> directive writing %wu byte into a "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2509 "region of size %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2510 "%<%.*s%> directive writing %wu bytes into a "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2511 "region of size %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2512 (int) dir.len, d, res.min, navail);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2513 else if (maybe)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2514 return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2515 "%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2516 "writing %wu byte into a region of size %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2517 "%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2518 "writing %wu bytes into a region of size %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2519 (int) dir.len, d, res.min, navail);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2520 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2521 return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2522 "%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2523 "%wu byte into a region of size %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2524 "%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2525 "%wu bytes into a region of size %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2526 (int) dir.len, d, res.min, navail);
111
kono
parents:
diff changeset
2527 }
kono
parents:
diff changeset
2528 if (res.min == 0 && res.max < maxbytes)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2529 return fmtwarn (dirloc, argloc, NULL,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2530 info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2531 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2532 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2533 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2534 "writing up to %wu bytes into a region of "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2535 "size %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2536 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2537 "up to %wu bytes into a region of size %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2538 : G_("%<%.*s%> directive writing up to %wu bytes "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2539 "into a region of size %wu"), (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2540 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2541 res.max, navail);
111
kono
parents:
diff changeset
2542
kono
parents:
diff changeset
2543 if (res.min == 0 && maxbytes <= res.max)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2544 /* This is a special case to avoid issuing the potentially
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2545 confusing warning:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2546 writing 0 or more bytes into a region of size 0. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2547 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2548 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2549 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2550 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2551 "writing likely %wu or more bytes into a "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2552 "region of size %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2553 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2554 "likely %wu or more bytes into a region of "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2555 "size %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2556 : G_("%<%.*s%> directive writing likely %wu or more "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2557 "bytes into a region of size %wu"), (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2558 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2559 res.likely, navail);
111
kono
parents:
diff changeset
2560
kono
parents:
diff changeset
2561 if (res.max < maxbytes)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2562 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2563 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2564 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2565 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2566 "writing between %wu and %wu bytes into a "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2567 "region of size %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2568 : G_("%<%.*s%> directive output truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2569 "writing between %wu and %wu bytes into a "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2570 "region of size %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2571 : G_("%<%.*s%> directive writing between %wu and "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2572 "%wu bytes into a region of size %wu"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2573 (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2574 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2575 res.min, res.max, navail);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2576
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2577 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2578 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2579 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2580 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2581 "writing %wu or more bytes into a region of "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2582 "size %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2583 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2584 "%wu or more bytes into a region of size %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2585 : G_("%<%.*s%> directive writing %wu or more bytes "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2586 "into a region of size %wu"), (int) dir.len,
111
kono
parents:
diff changeset
2587 target_to_host (hostdir, sizeof hostdir, dir.beg),
kono
parents:
diff changeset
2588 res.min, navail);
kono
parents:
diff changeset
2589 }
kono
parents:
diff changeset
2590
kono
parents:
diff changeset
2591 /* The size of the destination region is a range. */
kono
parents:
diff changeset
2592
kono
parents:
diff changeset
2593 if (target_to_host (*dir.beg) != '%')
kono
parents:
diff changeset
2594 {
kono
parents:
diff changeset
2595 unsigned HOST_WIDE_INT navail = avail_range.max;
kono
parents:
diff changeset
2596
kono
parents:
diff changeset
2597 /* For plain character directives (i.e., the format string itself)
kono
parents:
diff changeset
2598 but not others, point the caret at the first character that's
kono
parents:
diff changeset
2599 past the end of the destination. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2600 if (navail < dir.len)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2601 dirloc.set_caret_index (dirloc.get_caret_idx () + navail);
111
kono
parents:
diff changeset
2602 }
kono
parents:
diff changeset
2603
kono
parents:
diff changeset
2604 if (*dir.beg == '\0')
kono
parents:
diff changeset
2605 {
kono
parents:
diff changeset
2606 gcc_assert (res.min == 1 && res.min == res.max);
kono
parents:
diff changeset
2607
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2608 return fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2609 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2610 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2611 ? G_("%qE output may be truncated before the last "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2612 "format character")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2613 : G_("%qE output truncated before the last format "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2614 "character"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2615 : (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2616 ? G_("%qE may write a terminating nul past the end "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2617 "of the destination")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2618 : G_("%qE writing a terminating nul past the end "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2619 "of the destination")), info.func);
111
kono
parents:
diff changeset
2620 }
kono
parents:
diff changeset
2621
kono
parents:
diff changeset
2622 if (res.min == res.max)
kono
parents:
diff changeset
2623 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2624 const char *d = target_to_host (hostdir, sizeof hostdir, dir.beg);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2625 if (!info.bounded)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2626 return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2627 "%<%.*s%> directive writing %wu byte into a region "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2628 "of size between %wu and %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2629 "%<%.*s%> directive writing %wu bytes into a region "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2630 "of size between %wu and %wu", (int) dir.len, d,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2631 res.min, avail_range.min, avail_range.max);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2632 else if (maybe)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2633 return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2634 "%<%.*s%> directive output may be truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2635 "%wu byte into a region of size between %wu and %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2636 "%<%.*s%> directive output may be truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2637 "%wu bytes into a region of size between %wu and "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2638 "%wu", (int) dir.len, d, res.min, avail_range.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2639 avail_range.max);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2640 else
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2641 return fmtwarn_n (dirloc, argloc, NULL, info.warnopt (), res.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2642 "%<%.*s%> directive output truncated writing %wu "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2643 "byte into a region of size between %wu and %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2644 "%<%.*s%> directive output truncated writing %wu "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2645 "bytes into a region of size between %wu and %wu",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2646 (int) dir.len, d, res.min, avail_range.min,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2647 avail_range.max);
111
kono
parents:
diff changeset
2648 }
kono
parents:
diff changeset
2649
kono
parents:
diff changeset
2650 if (res.min == 0 && res.max < maxbytes)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2651 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2652 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2653 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2654 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2655 "writing up to %wu bytes into a region of size "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2656 "between %wu and %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2657 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2658 "up to %wu bytes into a region of size between "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2659 "%wu and %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2660 : G_("%<%.*s%> directive writing up to %wu bytes "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2661 "into a region of size between %wu and %wu"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2662 (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2663 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2664 res.max, avail_range.min, avail_range.max);
111
kono
parents:
diff changeset
2665
kono
parents:
diff changeset
2666 if (res.min == 0 && maxbytes <= res.max)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2667 /* This is a special case to avoid issuing the potentially confusing
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2668 warning:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2669 writing 0 or more bytes into a region of size between 0 and N. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2670 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2671 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2672 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2673 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2674 "writing likely %wu or more bytes into a region "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2675 "of size between %wu and %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2676 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2677 "likely %wu or more bytes into a region of size "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2678 "between %wu and %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2679 : G_("%<%.*s%> directive writing likely %wu or more bytes "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2680 "into a region of size between %wu and %wu"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2681 (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2682 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2683 res.likely, avail_range.min, avail_range.max);
111
kono
parents:
diff changeset
2684
kono
parents:
diff changeset
2685 if (res.max < maxbytes)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2686 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2687 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2688 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2689 ? G_("%<%.*s%> directive output may be truncated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2690 "writing between %wu and %wu bytes into a region "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2691 "of size between %wu and %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2692 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2693 "between %wu and %wu bytes into a region of size "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2694 "between %wu and %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2695 : G_("%<%.*s%> directive writing between %wu and "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2696 "%wu bytes into a region of size between %wu and "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2697 "%wu"), (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2698 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2699 res.min, res.max, avail_range.min, avail_range.max);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2700
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2701 return fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2702 info.bounded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2703 ? (maybe
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2704 ? G_("%<%.*s%> directive output may be truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2705 "%wu or more bytes into a region of size between "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2706 "%wu and %wu")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2707 : G_("%<%.*s%> directive output truncated writing "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2708 "%wu or more bytes into a region of size between "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2709 "%wu and %wu"))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2710 : G_("%<%.*s%> directive writing %wu or more bytes "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2711 "into a region of size between %wu and %wu"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2712 (int) dir.len,
111
kono
parents:
diff changeset
2713 target_to_host (hostdir, sizeof hostdir, dir.beg),
kono
parents:
diff changeset
2714 res.min, avail_range.min, avail_range.max);
kono
parents:
diff changeset
2715 }
kono
parents:
diff changeset
2716
kono
parents:
diff changeset
2717 /* Compute the length of the output resulting from the directive DIR
kono
parents:
diff changeset
2718 in a call described by INFO and update the overall result of the call
kono
parents:
diff changeset
2719 in *RES. Return true if the directive has been handled. */
kono
parents:
diff changeset
2720
kono
parents:
diff changeset
2721 static bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2722 format_directive (const sprintf_dom_walker::call_info &info,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2723 format_result *res, const directive &dir,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2724 class vr_values *vr_values)
111
kono
parents:
diff changeset
2725 {
kono
parents:
diff changeset
2726 /* Offset of the beginning of the directive from the beginning
kono
parents:
diff changeset
2727 of the format string. */
kono
parents:
diff changeset
2728 size_t offset = dir.beg - info.fmtstr;
kono
parents:
diff changeset
2729 size_t start = offset;
kono
parents:
diff changeset
2730 size_t length = offset + dir.len - !!dir.len;
kono
parents:
diff changeset
2731
kono
parents:
diff changeset
2732 /* Create a location for the whole directive from the % to the format
kono
parents:
diff changeset
2733 specifier. */
kono
parents:
diff changeset
2734 substring_loc dirloc (info.fmtloc, TREE_TYPE (info.format),
kono
parents:
diff changeset
2735 offset, start, length);
kono
parents:
diff changeset
2736
kono
parents:
diff changeset
2737 /* Also get the location of the argument if possible.
kono
parents:
diff changeset
2738 This doesn't work for integer literals or function calls. */
kono
parents:
diff changeset
2739 location_t argloc = UNKNOWN_LOCATION;
kono
parents:
diff changeset
2740 if (dir.arg)
kono
parents:
diff changeset
2741 argloc = EXPR_LOCATION (dir.arg);
kono
parents:
diff changeset
2742
kono
parents:
diff changeset
2743 /* Bail when there is no function to compute the output length,
kono
parents:
diff changeset
2744 or when minimum length checking has been disabled. */
kono
parents:
diff changeset
2745 if (!dir.fmtfunc || res->range.min >= HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
2746 return false;
kono
parents:
diff changeset
2747
kono
parents:
diff changeset
2748 /* Compute the range of lengths of the formatted output. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2749 fmtresult fmtres = dir.fmtfunc (dir, dir.arg, vr_values);
111
kono
parents:
diff changeset
2750
kono
parents:
diff changeset
2751 /* Record whether the output of all directives is known to be
kono
parents:
diff changeset
2752 bounded by some maximum, implying that their arguments are
kono
parents:
diff changeset
2753 either known exactly or determined to be in a known range
kono
parents:
diff changeset
2754 or, for strings, limited by the upper bounds of the arrays
kono
parents:
diff changeset
2755 they refer to. */
kono
parents:
diff changeset
2756 res->knownrange &= fmtres.knownrange;
kono
parents:
diff changeset
2757
kono
parents:
diff changeset
2758 if (!fmtres.knownrange)
kono
parents:
diff changeset
2759 {
kono
parents:
diff changeset
2760 /* Only when the range is known, check it against the host value
kono
parents:
diff changeset
2761 of INT_MAX + (the number of bytes of the "%.*Lf" directive with
kono
parents:
diff changeset
2762 INT_MAX precision, which is the longest possible output of any
kono
parents:
diff changeset
2763 single directive). That's the largest valid byte count (though
kono
parents:
diff changeset
2764 not valid call to a printf-like function because it can never
kono
parents:
diff changeset
2765 return such a count). Otherwise, the range doesn't correspond
kono
parents:
diff changeset
2766 to known values of the argument. */
kono
parents:
diff changeset
2767 if (fmtres.range.max > target_dir_max ())
kono
parents:
diff changeset
2768 {
kono
parents:
diff changeset
2769 /* Normalize the MAX counter to avoid having to deal with it
kono
parents:
diff changeset
2770 later. The counter can be less than HOST_WIDE_INT_M1U
kono
parents:
diff changeset
2771 when compiling for an ILP32 target on an LP64 host. */
kono
parents:
diff changeset
2772 fmtres.range.max = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2773 /* Disable exact and maximum length checking after a failure
kono
parents:
diff changeset
2774 to determine the maximum number of characters (for example
kono
parents:
diff changeset
2775 for wide characters or wide character strings) but continue
kono
parents:
diff changeset
2776 tracking the minimum number of characters. */
kono
parents:
diff changeset
2777 res->range.max = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2778 }
kono
parents:
diff changeset
2779
kono
parents:
diff changeset
2780 if (fmtres.range.min > target_dir_max ())
kono
parents:
diff changeset
2781 {
kono
parents:
diff changeset
2782 /* Disable exact length checking after a failure to determine
kono
parents:
diff changeset
2783 even the minimum number of characters (it shouldn't happen
kono
parents:
diff changeset
2784 except in an error) but keep tracking the minimum and maximum
kono
parents:
diff changeset
2785 number of characters. */
kono
parents:
diff changeset
2786 return true;
kono
parents:
diff changeset
2787 }
kono
parents:
diff changeset
2788 }
kono
parents:
diff changeset
2789
kono
parents:
diff changeset
2790 /* Buffer for the directive in the host character set (used when
kono
parents:
diff changeset
2791 the source character set is different). */
kono
parents:
diff changeset
2792 char hostdir[32];
kono
parents:
diff changeset
2793
kono
parents:
diff changeset
2794 int dirlen = dir.len;
kono
parents:
diff changeset
2795
kono
parents:
diff changeset
2796 if (fmtres.nullp)
kono
parents:
diff changeset
2797 {
kono
parents:
diff changeset
2798 fmtwarn (dirloc, argloc, NULL, info.warnopt (),
kono
parents:
diff changeset
2799 "%<%.*s%> directive argument is null",
kono
parents:
diff changeset
2800 dirlen, target_to_host (hostdir, sizeof hostdir, dir.beg));
kono
parents:
diff changeset
2801
kono
parents:
diff changeset
2802 /* Don't bother processing the rest of the format string. */
kono
parents:
diff changeset
2803 res->warned = true;
kono
parents:
diff changeset
2804 res->range.min = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2805 res->range.max = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2806 return false;
kono
parents:
diff changeset
2807 }
kono
parents:
diff changeset
2808
kono
parents:
diff changeset
2809 /* Compute the number of available bytes in the destination. There
kono
parents:
diff changeset
2810 must always be at least one byte of space for the terminating
kono
parents:
diff changeset
2811 NUL that's appended after the format string has been processed. */
kono
parents:
diff changeset
2812 result_range avail_range = bytes_remaining (info.objsize, *res);
kono
parents:
diff changeset
2813
kono
parents:
diff changeset
2814 bool warned = res->warned;
kono
parents:
diff changeset
2815
kono
parents:
diff changeset
2816 if (!warned)
kono
parents:
diff changeset
2817 warned = maybe_warn (dirloc, argloc, info, avail_range,
kono
parents:
diff changeset
2818 fmtres.range, dir);
kono
parents:
diff changeset
2819
kono
parents:
diff changeset
2820 /* Bump up the total maximum if it isn't too big. */
kono
parents:
diff changeset
2821 if (res->range.max < HOST_WIDE_INT_MAX
kono
parents:
diff changeset
2822 && fmtres.range.max < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
2823 res->range.max += fmtres.range.max;
kono
parents:
diff changeset
2824
kono
parents:
diff changeset
2825 /* Raise the total unlikely maximum by the larger of the maximum
kono
parents:
diff changeset
2826 and the unlikely maximum. */
kono
parents:
diff changeset
2827 unsigned HOST_WIDE_INT save = res->range.unlikely;
kono
parents:
diff changeset
2828 if (fmtres.range.max < fmtres.range.unlikely)
kono
parents:
diff changeset
2829 res->range.unlikely += fmtres.range.unlikely;
kono
parents:
diff changeset
2830 else
kono
parents:
diff changeset
2831 res->range.unlikely += fmtres.range.max;
kono
parents:
diff changeset
2832
kono
parents:
diff changeset
2833 if (res->range.unlikely < save)
kono
parents:
diff changeset
2834 res->range.unlikely = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
2835
kono
parents:
diff changeset
2836 res->range.min += fmtres.range.min;
kono
parents:
diff changeset
2837 res->range.likely += fmtres.range.likely;
kono
parents:
diff changeset
2838
kono
parents:
diff changeset
2839 /* Has the minimum directive output length exceeded the maximum
kono
parents:
diff changeset
2840 of 4095 bytes required to be supported? */
kono
parents:
diff changeset
2841 bool minunder4k = fmtres.range.min < 4096;
kono
parents:
diff changeset
2842 bool maxunder4k = fmtres.range.max < 4096;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2843 /* Clear POSUNDER4K in the overall result if the maximum has exceeded
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2844 the 4k (this is necessary to avoid the return value optimization
111
kono
parents:
diff changeset
2845 that may not be safe in the maximum case). */
kono
parents:
diff changeset
2846 if (!maxunder4k)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2847 res->posunder4k = false;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2848 /* Also clear POSUNDER4K if the directive may fail. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2849 if (fmtres.mayfail)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2850 res->posunder4k = false;
111
kono
parents:
diff changeset
2851
kono
parents:
diff changeset
2852 if (!warned
kono
parents:
diff changeset
2853 /* Only warn at level 2. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2854 && warn_level > 1
111
kono
parents:
diff changeset
2855 && (!minunder4k
kono
parents:
diff changeset
2856 || (!maxunder4k && fmtres.range.max < HOST_WIDE_INT_MAX)))
kono
parents:
diff changeset
2857 {
kono
parents:
diff changeset
2858 /* The directive output may be longer than the maximum required
kono
parents:
diff changeset
2859 to be handled by an implementation according to 7.21.6.1, p15
kono
parents:
diff changeset
2860 of C11. Warn on this only at level 2 but remember this and
kono
parents:
diff changeset
2861 prevent folding the return value when done. This allows for
kono
parents:
diff changeset
2862 the possibility of the actual libc call failing due to ENOMEM
kono
parents:
diff changeset
2863 (like Glibc does under some conditions). */
kono
parents:
diff changeset
2864
kono
parents:
diff changeset
2865 if (fmtres.range.min == fmtres.range.max)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2866 warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (),
111
kono
parents:
diff changeset
2867 "%<%.*s%> directive output of %wu bytes exceeds "
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2868 "minimum required size of 4095", dirlen,
111
kono
parents:
diff changeset
2869 target_to_host (hostdir, sizeof hostdir, dir.beg),
kono
parents:
diff changeset
2870 fmtres.range.min);
kono
parents:
diff changeset
2871 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2872 warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2873 minunder4k
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2874 ? G_("%<%.*s%> directive output between %wu and %wu "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2875 "bytes may exceed minimum required size of "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2876 "4095")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2877 : G_("%<%.*s%> directive output between %wu and %wu "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2878 "bytes exceeds minimum required size of 4095"),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2879 dirlen,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2880 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2881 fmtres.range.min, fmtres.range.max);
111
kono
parents:
diff changeset
2882 }
kono
parents:
diff changeset
2883
kono
parents:
diff changeset
2884 /* Has the likely and maximum directive output exceeded INT_MAX? */
kono
parents:
diff changeset
2885 bool likelyximax = *dir.beg && res->range.likely > target_int_max ();
kono
parents:
diff changeset
2886 /* Don't consider the maximum to be in excess when it's the result
kono
parents:
diff changeset
2887 of a string of unknown length (i.e., whose maximum has been set
kono
parents:
diff changeset
2888 to be greater than or equal to HOST_WIDE_INT_MAX. */
kono
parents:
diff changeset
2889 bool maxximax = (*dir.beg
kono
parents:
diff changeset
2890 && res->range.max > target_int_max ()
kono
parents:
diff changeset
2891 && res->range.max < HOST_WIDE_INT_MAX);
kono
parents:
diff changeset
2892
kono
parents:
diff changeset
2893 if (!warned
kono
parents:
diff changeset
2894 /* Warn for the likely output size at level 1. */
kono
parents:
diff changeset
2895 && (likelyximax
kono
parents:
diff changeset
2896 /* But only warn for the maximum at level 2. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2897 || (warn_level > 1
111
kono
parents:
diff changeset
2898 && maxximax
kono
parents:
diff changeset
2899 && fmtres.range.max < HOST_WIDE_INT_MAX)))
kono
parents:
diff changeset
2900 {
kono
parents:
diff changeset
2901 /* The directive output causes the total length of output
kono
parents:
diff changeset
2902 to exceed INT_MAX bytes. */
kono
parents:
diff changeset
2903
kono
parents:
diff changeset
2904 if (fmtres.range.min == fmtres.range.max)
kono
parents:
diff changeset
2905 warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (),
kono
parents:
diff changeset
2906 "%<%.*s%> directive output of %wu bytes causes "
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2907 "result to exceed %<INT_MAX%>", dirlen,
111
kono
parents:
diff changeset
2908 target_to_host (hostdir, sizeof hostdir, dir.beg),
kono
parents:
diff changeset
2909 fmtres.range.min);
kono
parents:
diff changeset
2910 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2911 warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2912 fmtres.range.min > target_int_max ()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2913 ? G_("%<%.*s%> directive output between %wu and "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2914 "%wu bytes causes result to exceed "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2915 "%<INT_MAX%>")
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2916 : G_("%<%.*s%> directive output between %wu and "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2917 "%wu bytes may cause result to exceed "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2918 "%<INT_MAX%>"), dirlen,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2919 target_to_host (hostdir, sizeof hostdir, dir.beg),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2920 fmtres.range.min, fmtres.range.max);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2921 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2922
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2923 if (!warned && fmtres.nonstr)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2924 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2925 warned = fmtwarn (dirloc, argloc, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2926 "%<%.*s%> directive argument is not a nul-terminated "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2927 "string",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2928 dirlen,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2929 target_to_host (hostdir, sizeof hostdir, dir.beg));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2930 if (warned && DECL_P (fmtres.nonstr))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2931 inform (DECL_SOURCE_LOCATION (fmtres.nonstr),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2932 "referenced argument declared here");
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2933 return false;
111
kono
parents:
diff changeset
2934 }
kono
parents:
diff changeset
2935
kono
parents:
diff changeset
2936 if (warned && fmtres.range.min < fmtres.range.likely
kono
parents:
diff changeset
2937 && fmtres.range.likely < fmtres.range.max)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2938 inform_n (info.fmtloc, fmtres.range.likely,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2939 "assuming directive output of %wu byte",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2940 "assuming directive output of %wu bytes",
111
kono
parents:
diff changeset
2941 fmtres.range.likely);
kono
parents:
diff changeset
2942
kono
parents:
diff changeset
2943 if (warned && fmtres.argmin)
kono
parents:
diff changeset
2944 {
kono
parents:
diff changeset
2945 if (fmtres.argmin == fmtres.argmax)
kono
parents:
diff changeset
2946 inform (info.fmtloc, "directive argument %qE", fmtres.argmin);
kono
parents:
diff changeset
2947 else if (fmtres.knownrange)
kono
parents:
diff changeset
2948 inform (info.fmtloc, "directive argument in the range [%E, %E]",
kono
parents:
diff changeset
2949 fmtres.argmin, fmtres.argmax);
kono
parents:
diff changeset
2950 else
kono
parents:
diff changeset
2951 inform (info.fmtloc,
kono
parents:
diff changeset
2952 "using the range [%E, %E] for directive argument",
kono
parents:
diff changeset
2953 fmtres.argmin, fmtres.argmax);
kono
parents:
diff changeset
2954 }
kono
parents:
diff changeset
2955
kono
parents:
diff changeset
2956 res->warned |= warned;
kono
parents:
diff changeset
2957
kono
parents:
diff changeset
2958 if (!dir.beg[0] && res->warned && info.objsize < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
2959 {
kono
parents:
diff changeset
2960 /* If a warning has been issued for buffer overflow or truncation
kono
parents:
diff changeset
2961 (but not otherwise) help the user figure out how big a buffer
kono
parents:
diff changeset
2962 they need. */
kono
parents:
diff changeset
2963
kono
parents:
diff changeset
2964 location_t callloc = gimple_location (info.callstmt);
kono
parents:
diff changeset
2965
kono
parents:
diff changeset
2966 unsigned HOST_WIDE_INT min = res->range.min;
kono
parents:
diff changeset
2967 unsigned HOST_WIDE_INT max = res->range.max;
kono
parents:
diff changeset
2968
kono
parents:
diff changeset
2969 if (min == max)
kono
parents:
diff changeset
2970 inform (callloc,
kono
parents:
diff changeset
2971 (min == 1
kono
parents:
diff changeset
2972 ? G_("%qE output %wu byte into a destination of size %wu")
kono
parents:
diff changeset
2973 : G_("%qE output %wu bytes into a destination of size %wu")),
kono
parents:
diff changeset
2974 info.func, min, info.objsize);
kono
parents:
diff changeset
2975 else if (max < HOST_WIDE_INT_MAX)
kono
parents:
diff changeset
2976 inform (callloc,
kono
parents:
diff changeset
2977 "%qE output between %wu and %wu bytes into "
kono
parents:
diff changeset
2978 "a destination of size %wu",
kono
parents:
diff changeset
2979 info.func, min, max, info.objsize);
kono
parents:
diff changeset
2980 else if (min < res->range.likely && res->range.likely < max)
kono
parents:
diff changeset
2981 inform (callloc,
kono
parents:
diff changeset
2982 "%qE output %wu or more bytes (assuming %wu) into "
kono
parents:
diff changeset
2983 "a destination of size %wu",
kono
parents:
diff changeset
2984 info.func, min, res->range.likely, info.objsize);
kono
parents:
diff changeset
2985 else
kono
parents:
diff changeset
2986 inform (callloc,
kono
parents:
diff changeset
2987 "%qE output %wu or more bytes into a destination of size %wu",
kono
parents:
diff changeset
2988 info.func, min, info.objsize);
kono
parents:
diff changeset
2989 }
kono
parents:
diff changeset
2990
kono
parents:
diff changeset
2991 if (dump_file && *dir.beg)
kono
parents:
diff changeset
2992 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2993 fprintf (dump_file,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2994 " Result: "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2995 HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC ", "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2996 HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC " ("
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2997 HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC ", "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2998 HOST_WIDE_INT_PRINT_DEC ", " HOST_WIDE_INT_PRINT_DEC ")\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2999 fmtres.range.min, fmtres.range.likely,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3000 fmtres.range.max, fmtres.range.unlikely,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3001 res->range.min, res->range.likely,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3002 res->range.max, res->range.unlikely);
111
kono
parents:
diff changeset
3003 }
kono
parents:
diff changeset
3004
kono
parents:
diff changeset
3005 return true;
kono
parents:
diff changeset
3006 }
kono
parents:
diff changeset
3007
kono
parents:
diff changeset
3008 /* Parse a format directive in function call described by INFO starting
kono
parents:
diff changeset
3009 at STR and populate DIR structure. Bump up *ARGNO by the number of
kono
parents:
diff changeset
3010 arguments extracted for the directive. Return the length of
kono
parents:
diff changeset
3011 the directive. */
kono
parents:
diff changeset
3012
kono
parents:
diff changeset
3013 static size_t
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3014 parse_directive (sprintf_dom_walker::call_info &info,
111
kono
parents:
diff changeset
3015 directive &dir, format_result *res,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3016 const char *str, unsigned *argno,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3017 vr_values *vr_values)
111
kono
parents:
diff changeset
3018 {
kono
parents:
diff changeset
3019 const char *pcnt = strchr (str, target_percent);
kono
parents:
diff changeset
3020 dir.beg = str;
kono
parents:
diff changeset
3021
kono
parents:
diff changeset
3022 if (size_t len = pcnt ? pcnt - str : *str ? strlen (str) : 1)
kono
parents:
diff changeset
3023 {
kono
parents:
diff changeset
3024 /* This directive is either a plain string or the terminating nul
kono
parents:
diff changeset
3025 (which isn't really a directive but it simplifies things to
kono
parents:
diff changeset
3026 handle it as if it were). */
kono
parents:
diff changeset
3027 dir.len = len;
kono
parents:
diff changeset
3028 dir.fmtfunc = format_plain;
kono
parents:
diff changeset
3029
kono
parents:
diff changeset
3030 if (dump_file)
kono
parents:
diff changeset
3031 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3032 fprintf (dump_file, " Directive %u at offset "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3033 HOST_WIDE_INT_PRINT_UNSIGNED ": \"%.*s\", "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3034 "length = " HOST_WIDE_INT_PRINT_UNSIGNED "\n",
111
kono
parents:
diff changeset
3035 dir.dirno,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3036 (unsigned HOST_WIDE_INT)(size_t)(dir.beg - info.fmtstr),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3037 (int)dir.len, dir.beg, (unsigned HOST_WIDE_INT) dir.len);
111
kono
parents:
diff changeset
3038 }
kono
parents:
diff changeset
3039
kono
parents:
diff changeset
3040 return len - !*str;
kono
parents:
diff changeset
3041 }
kono
parents:
diff changeset
3042
kono
parents:
diff changeset
3043 const char *pf = pcnt + 1;
kono
parents:
diff changeset
3044
kono
parents:
diff changeset
3045 /* POSIX numbered argument index or zero when none. */
kono
parents:
diff changeset
3046 HOST_WIDE_INT dollar = 0;
kono
parents:
diff changeset
3047
kono
parents:
diff changeset
3048 /* With and precision. -1 when not specified, HOST_WIDE_INT_MIN
kono
parents:
diff changeset
3049 when given by a va_list argument, and a non-negative value
kono
parents:
diff changeset
3050 when specified in the format string itself. */
kono
parents:
diff changeset
3051 HOST_WIDE_INT width = -1;
kono
parents:
diff changeset
3052 HOST_WIDE_INT precision = -1;
kono
parents:
diff changeset
3053
kono
parents:
diff changeset
3054 /* Pointers to the beginning of the width and precision decimal
kono
parents:
diff changeset
3055 string (if any) within the directive. */
kono
parents:
diff changeset
3056 const char *pwidth = 0;
kono
parents:
diff changeset
3057 const char *pprec = 0;
kono
parents:
diff changeset
3058
kono
parents:
diff changeset
3059 /* When the value of the decimal string that specifies width or
kono
parents:
diff changeset
3060 precision is out of range, points to the digit that causes
kono
parents:
diff changeset
3061 the value to exceed the limit. */
kono
parents:
diff changeset
3062 const char *werange = NULL;
kono
parents:
diff changeset
3063 const char *perange = NULL;
kono
parents:
diff changeset
3064
kono
parents:
diff changeset
3065 /* Width specified via the asterisk. Need not be INTEGER_CST.
kono
parents:
diff changeset
3066 For vararg functions set to void_node. */
kono
parents:
diff changeset
3067 tree star_width = NULL_TREE;
kono
parents:
diff changeset
3068
kono
parents:
diff changeset
3069 /* Width specified via the asterisk. Need not be INTEGER_CST.
kono
parents:
diff changeset
3070 For vararg functions set to void_node. */
kono
parents:
diff changeset
3071 tree star_precision = NULL_TREE;
kono
parents:
diff changeset
3072
kono
parents:
diff changeset
3073 if (ISDIGIT (target_to_host (*pf)))
kono
parents:
diff changeset
3074 {
kono
parents:
diff changeset
3075 /* This could be either a POSIX positional argument, the '0'
kono
parents:
diff changeset
3076 flag, or a width, depending on what follows. Store it as
kono
parents:
diff changeset
3077 width and sort it out later after the next character has
kono
parents:
diff changeset
3078 been seen. */
kono
parents:
diff changeset
3079 pwidth = pf;
kono
parents:
diff changeset
3080 width = target_strtol10 (&pf, &werange);
kono
parents:
diff changeset
3081 }
kono
parents:
diff changeset
3082 else if (target_to_host (*pf) == '*')
kono
parents:
diff changeset
3083 {
kono
parents:
diff changeset
3084 /* Similarly to the block above, this could be either a POSIX
kono
parents:
diff changeset
3085 positional argument or a width, depending on what follows. */
kono
parents:
diff changeset
3086 if (*argno < gimple_call_num_args (info.callstmt))
kono
parents:
diff changeset
3087 star_width = gimple_call_arg (info.callstmt, (*argno)++);
kono
parents:
diff changeset
3088 else
kono
parents:
diff changeset
3089 star_width = void_node;
kono
parents:
diff changeset
3090 ++pf;
kono
parents:
diff changeset
3091 }
kono
parents:
diff changeset
3092
kono
parents:
diff changeset
3093 if (target_to_host (*pf) == '$')
kono
parents:
diff changeset
3094 {
kono
parents:
diff changeset
3095 /* Handle the POSIX dollar sign which references the 1-based
kono
parents:
diff changeset
3096 positional argument number. */
kono
parents:
diff changeset
3097 if (width != -1)
kono
parents:
diff changeset
3098 dollar = width + info.argidx;
kono
parents:
diff changeset
3099 else if (star_width
kono
parents:
diff changeset
3100 && TREE_CODE (star_width) == INTEGER_CST
kono
parents:
diff changeset
3101 && (TYPE_PRECISION (TREE_TYPE (star_width))
kono
parents:
diff changeset
3102 <= TYPE_PRECISION (integer_type_node)))
kono
parents:
diff changeset
3103 dollar = width + tree_to_shwi (star_width);
kono
parents:
diff changeset
3104
kono
parents:
diff changeset
3105 /* Bail when the numbered argument is out of range (it will
kono
parents:
diff changeset
3106 have already been diagnosed by -Wformat). */
kono
parents:
diff changeset
3107 if (dollar == 0
kono
parents:
diff changeset
3108 || dollar == (int)info.argidx
kono
parents:
diff changeset
3109 || dollar > gimple_call_num_args (info.callstmt))
kono
parents:
diff changeset
3110 return false;
kono
parents:
diff changeset
3111
kono
parents:
diff changeset
3112 --dollar;
kono
parents:
diff changeset
3113
kono
parents:
diff changeset
3114 star_width = NULL_TREE;
kono
parents:
diff changeset
3115 width = -1;
kono
parents:
diff changeset
3116 ++pf;
kono
parents:
diff changeset
3117 }
kono
parents:
diff changeset
3118
kono
parents:
diff changeset
3119 if (dollar || !star_width)
kono
parents:
diff changeset
3120 {
kono
parents:
diff changeset
3121 if (width != -1)
kono
parents:
diff changeset
3122 {
kono
parents:
diff changeset
3123 if (width == 0)
kono
parents:
diff changeset
3124 {
kono
parents:
diff changeset
3125 /* The '0' that has been interpreted as a width above is
kono
parents:
diff changeset
3126 actually a flag. Reset HAVE_WIDTH, set the '0' flag,
kono
parents:
diff changeset
3127 and continue processing other flags. */
kono
parents:
diff changeset
3128 width = -1;
kono
parents:
diff changeset
3129 dir.set_flag ('0');
kono
parents:
diff changeset
3130 }
kono
parents:
diff changeset
3131 else if (!dollar)
kono
parents:
diff changeset
3132 {
kono
parents:
diff changeset
3133 /* (Non-zero) width has been seen. The next character
kono
parents:
diff changeset
3134 is either a period or a digit. */
kono
parents:
diff changeset
3135 goto start_precision;
kono
parents:
diff changeset
3136 }
kono
parents:
diff changeset
3137 }
kono
parents:
diff changeset
3138 /* When either '$' has been seen, or width has not been seen,
kono
parents:
diff changeset
3139 the next field is the optional flags followed by an optional
kono
parents:
diff changeset
3140 width. */
kono
parents:
diff changeset
3141 for ( ; ; ) {
kono
parents:
diff changeset
3142 switch (target_to_host (*pf))
kono
parents:
diff changeset
3143 {
kono
parents:
diff changeset
3144 case ' ':
kono
parents:
diff changeset
3145 case '0':
kono
parents:
diff changeset
3146 case '+':
kono
parents:
diff changeset
3147 case '-':
kono
parents:
diff changeset
3148 case '#':
kono
parents:
diff changeset
3149 dir.set_flag (target_to_host (*pf++));
kono
parents:
diff changeset
3150 break;
kono
parents:
diff changeset
3151
kono
parents:
diff changeset
3152 default:
kono
parents:
diff changeset
3153 goto start_width;
kono
parents:
diff changeset
3154 }
kono
parents:
diff changeset
3155 }
kono
parents:
diff changeset
3156
kono
parents:
diff changeset
3157 start_width:
kono
parents:
diff changeset
3158 if (ISDIGIT (target_to_host (*pf)))
kono
parents:
diff changeset
3159 {
kono
parents:
diff changeset
3160 werange = 0;
kono
parents:
diff changeset
3161 pwidth = pf;
kono
parents:
diff changeset
3162 width = target_strtol10 (&pf, &werange);
kono
parents:
diff changeset
3163 }
kono
parents:
diff changeset
3164 else if (target_to_host (*pf) == '*')
kono
parents:
diff changeset
3165 {
kono
parents:
diff changeset
3166 if (*argno < gimple_call_num_args (info.callstmt))
kono
parents:
diff changeset
3167 star_width = gimple_call_arg (info.callstmt, (*argno)++);
kono
parents:
diff changeset
3168 else
kono
parents:
diff changeset
3169 {
kono
parents:
diff changeset
3170 /* This is (likely) a va_list. It could also be an invalid
kono
parents:
diff changeset
3171 call with insufficient arguments. */
kono
parents:
diff changeset
3172 star_width = void_node;
kono
parents:
diff changeset
3173 }
kono
parents:
diff changeset
3174 ++pf;
kono
parents:
diff changeset
3175 }
kono
parents:
diff changeset
3176 else if (target_to_host (*pf) == '\'')
kono
parents:
diff changeset
3177 {
kono
parents:
diff changeset
3178 /* The POSIX apostrophe indicating a numeric grouping
kono
parents:
diff changeset
3179 in the current locale. Even though it's possible to
kono
parents:
diff changeset
3180 estimate the upper bound on the size of the output
kono
parents:
diff changeset
3181 based on the number of digits it probably isn't worth
kono
parents:
diff changeset
3182 continuing. */
kono
parents:
diff changeset
3183 return 0;
kono
parents:
diff changeset
3184 }
kono
parents:
diff changeset
3185 }
kono
parents:
diff changeset
3186
kono
parents:
diff changeset
3187 start_precision:
kono
parents:
diff changeset
3188 if (target_to_host (*pf) == '.')
kono
parents:
diff changeset
3189 {
kono
parents:
diff changeset
3190 ++pf;
kono
parents:
diff changeset
3191
kono
parents:
diff changeset
3192 if (ISDIGIT (target_to_host (*pf)))
kono
parents:
diff changeset
3193 {
kono
parents:
diff changeset
3194 pprec = pf;
kono
parents:
diff changeset
3195 precision = target_strtol10 (&pf, &perange);
kono
parents:
diff changeset
3196 }
kono
parents:
diff changeset
3197 else if (target_to_host (*pf) == '*')
kono
parents:
diff changeset
3198 {
kono
parents:
diff changeset
3199 if (*argno < gimple_call_num_args (info.callstmt))
kono
parents:
diff changeset
3200 star_precision = gimple_call_arg (info.callstmt, (*argno)++);
kono
parents:
diff changeset
3201 else
kono
parents:
diff changeset
3202 {
kono
parents:
diff changeset
3203 /* This is (likely) a va_list. It could also be an invalid
kono
parents:
diff changeset
3204 call with insufficient arguments. */
kono
parents:
diff changeset
3205 star_precision = void_node;
kono
parents:
diff changeset
3206 }
kono
parents:
diff changeset
3207 ++pf;
kono
parents:
diff changeset
3208 }
kono
parents:
diff changeset
3209 else
kono
parents:
diff changeset
3210 {
kono
parents:
diff changeset
3211 /* The decimal precision or the asterisk are optional.
kono
parents:
diff changeset
3212 When neither is dirified it's taken to be zero. */
kono
parents:
diff changeset
3213 precision = 0;
kono
parents:
diff changeset
3214 }
kono
parents:
diff changeset
3215 }
kono
parents:
diff changeset
3216
kono
parents:
diff changeset
3217 switch (target_to_host (*pf))
kono
parents:
diff changeset
3218 {
kono
parents:
diff changeset
3219 case 'h':
kono
parents:
diff changeset
3220 if (target_to_host (pf[1]) == 'h')
kono
parents:
diff changeset
3221 {
kono
parents:
diff changeset
3222 ++pf;
kono
parents:
diff changeset
3223 dir.modifier = FMT_LEN_hh;
kono
parents:
diff changeset
3224 }
kono
parents:
diff changeset
3225 else
kono
parents:
diff changeset
3226 dir.modifier = FMT_LEN_h;
kono
parents:
diff changeset
3227 ++pf;
kono
parents:
diff changeset
3228 break;
kono
parents:
diff changeset
3229
kono
parents:
diff changeset
3230 case 'j':
kono
parents:
diff changeset
3231 dir.modifier = FMT_LEN_j;
kono
parents:
diff changeset
3232 ++pf;
kono
parents:
diff changeset
3233 break;
kono
parents:
diff changeset
3234
kono
parents:
diff changeset
3235 case 'L':
kono
parents:
diff changeset
3236 dir.modifier = FMT_LEN_L;
kono
parents:
diff changeset
3237 ++pf;
kono
parents:
diff changeset
3238 break;
kono
parents:
diff changeset
3239
kono
parents:
diff changeset
3240 case 'l':
kono
parents:
diff changeset
3241 if (target_to_host (pf[1]) == 'l')
kono
parents:
diff changeset
3242 {
kono
parents:
diff changeset
3243 ++pf;
kono
parents:
diff changeset
3244 dir.modifier = FMT_LEN_ll;
kono
parents:
diff changeset
3245 }
kono
parents:
diff changeset
3246 else
kono
parents:
diff changeset
3247 dir.modifier = FMT_LEN_l;
kono
parents:
diff changeset
3248 ++pf;
kono
parents:
diff changeset
3249 break;
kono
parents:
diff changeset
3250
kono
parents:
diff changeset
3251 case 't':
kono
parents:
diff changeset
3252 dir.modifier = FMT_LEN_t;
kono
parents:
diff changeset
3253 ++pf;
kono
parents:
diff changeset
3254 break;
kono
parents:
diff changeset
3255
kono
parents:
diff changeset
3256 case 'z':
kono
parents:
diff changeset
3257 dir.modifier = FMT_LEN_z;
kono
parents:
diff changeset
3258 ++pf;
kono
parents:
diff changeset
3259 break;
kono
parents:
diff changeset
3260 }
kono
parents:
diff changeset
3261
kono
parents:
diff changeset
3262 switch (target_to_host (*pf))
kono
parents:
diff changeset
3263 {
kono
parents:
diff changeset
3264 /* Handle a sole '%' character the same as "%%" but since it's
kono
parents:
diff changeset
3265 undefined prevent the result from being folded. */
kono
parents:
diff changeset
3266 case '\0':
kono
parents:
diff changeset
3267 --pf;
kono
parents:
diff changeset
3268 res->range.min = res->range.max = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
3269 /* FALLTHRU */
kono
parents:
diff changeset
3270 case '%':
kono
parents:
diff changeset
3271 dir.fmtfunc = format_percent;
kono
parents:
diff changeset
3272 break;
kono
parents:
diff changeset
3273
kono
parents:
diff changeset
3274 case 'a':
kono
parents:
diff changeset
3275 case 'A':
kono
parents:
diff changeset
3276 case 'e':
kono
parents:
diff changeset
3277 case 'E':
kono
parents:
diff changeset
3278 case 'f':
kono
parents:
diff changeset
3279 case 'F':
kono
parents:
diff changeset
3280 case 'g':
kono
parents:
diff changeset
3281 case 'G':
kono
parents:
diff changeset
3282 res->floating = true;
kono
parents:
diff changeset
3283 dir.fmtfunc = format_floating;
kono
parents:
diff changeset
3284 break;
kono
parents:
diff changeset
3285
kono
parents:
diff changeset
3286 case 'd':
kono
parents:
diff changeset
3287 case 'i':
kono
parents:
diff changeset
3288 case 'o':
kono
parents:
diff changeset
3289 case 'u':
kono
parents:
diff changeset
3290 case 'x':
kono
parents:
diff changeset
3291 case 'X':
kono
parents:
diff changeset
3292 dir.fmtfunc = format_integer;
kono
parents:
diff changeset
3293 break;
kono
parents:
diff changeset
3294
kono
parents:
diff changeset
3295 case 'p':
kono
parents:
diff changeset
3296 /* The %p output is implementation-defined. It's possible
kono
parents:
diff changeset
3297 to determine this format but due to extensions (edirially
kono
parents:
diff changeset
3298 those of the Linux kernel -- see bug 78512) the first %p
kono
parents:
diff changeset
3299 in the format string disables any further processing. */
kono
parents:
diff changeset
3300 return false;
kono
parents:
diff changeset
3301
kono
parents:
diff changeset
3302 case 'n':
kono
parents:
diff changeset
3303 /* %n has side-effects even when nothing is actually printed to
kono
parents:
diff changeset
3304 any buffer. */
kono
parents:
diff changeset
3305 info.nowrite = false;
kono
parents:
diff changeset
3306 dir.fmtfunc = format_none;
kono
parents:
diff changeset
3307 break;
kono
parents:
diff changeset
3308
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3309 case 'C':
111
kono
parents:
diff changeset
3310 case 'c':
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3311 /* POSIX wide character and C/POSIX narrow character. */
111
kono
parents:
diff changeset
3312 dir.fmtfunc = format_character;
kono
parents:
diff changeset
3313 break;
kono
parents:
diff changeset
3314
kono
parents:
diff changeset
3315 case 'S':
kono
parents:
diff changeset
3316 case 's':
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3317 /* POSIX wide string and C/POSIX narrow character string. */
111
kono
parents:
diff changeset
3318 dir.fmtfunc = format_string;
kono
parents:
diff changeset
3319 break;
kono
parents:
diff changeset
3320
kono
parents:
diff changeset
3321 default:
kono
parents:
diff changeset
3322 /* Unknown conversion specification. */
kono
parents:
diff changeset
3323 return 0;
kono
parents:
diff changeset
3324 }
kono
parents:
diff changeset
3325
kono
parents:
diff changeset
3326 dir.specifier = target_to_host (*pf++);
kono
parents:
diff changeset
3327
kono
parents:
diff changeset
3328 /* Store the length of the format directive. */
kono
parents:
diff changeset
3329 dir.len = pf - pcnt;
kono
parents:
diff changeset
3330
kono
parents:
diff changeset
3331 /* Buffer for the directive in the host character set (used when
kono
parents:
diff changeset
3332 the source character set is different). */
kono
parents:
diff changeset
3333 char hostdir[32];
kono
parents:
diff changeset
3334
kono
parents:
diff changeset
3335 if (star_width)
kono
parents:
diff changeset
3336 {
kono
parents:
diff changeset
3337 if (INTEGRAL_TYPE_P (TREE_TYPE (star_width)))
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3338 dir.set_width (star_width, vr_values);
111
kono
parents:
diff changeset
3339 else
kono
parents:
diff changeset
3340 {
kono
parents:
diff changeset
3341 /* Width specified by a va_list takes on the range [0, -INT_MIN]
kono
parents:
diff changeset
3342 (width is the absolute value of that specified). */
kono
parents:
diff changeset
3343 dir.width[0] = 0;
kono
parents:
diff changeset
3344 dir.width[1] = target_int_max () + 1;
kono
parents:
diff changeset
3345 }
kono
parents:
diff changeset
3346 }
kono
parents:
diff changeset
3347 else
kono
parents:
diff changeset
3348 {
kono
parents:
diff changeset
3349 if (width == LONG_MAX && werange)
kono
parents:
diff changeset
3350 {
kono
parents:
diff changeset
3351 size_t begin = dir.beg - info.fmtstr + (pwidth - pcnt);
kono
parents:
diff changeset
3352 size_t caret = begin + (werange - pcnt);
kono
parents:
diff changeset
3353 size_t end = pf - info.fmtstr - 1;
kono
parents:
diff changeset
3354
kono
parents:
diff changeset
3355 /* Create a location for the width part of the directive,
kono
parents:
diff changeset
3356 pointing the caret at the first out-of-range digit. */
kono
parents:
diff changeset
3357 substring_loc dirloc (info.fmtloc, TREE_TYPE (info.format),
kono
parents:
diff changeset
3358 caret, begin, end);
kono
parents:
diff changeset
3359
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3360 fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3361 "%<%.*s%> directive width out of range", (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3362 target_to_host (hostdir, sizeof hostdir, dir.beg));
111
kono
parents:
diff changeset
3363 }
kono
parents:
diff changeset
3364
kono
parents:
diff changeset
3365 dir.set_width (width);
kono
parents:
diff changeset
3366 }
kono
parents:
diff changeset
3367
kono
parents:
diff changeset
3368 if (star_precision)
kono
parents:
diff changeset
3369 {
kono
parents:
diff changeset
3370 if (INTEGRAL_TYPE_P (TREE_TYPE (star_precision)))
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3371 dir.set_precision (star_precision, vr_values);
111
kono
parents:
diff changeset
3372 else
kono
parents:
diff changeset
3373 {
kono
parents:
diff changeset
3374 /* Precision specified by a va_list takes on the range [-1, INT_MAX]
kono
parents:
diff changeset
3375 (unlike width, negative precision is ignored). */
kono
parents:
diff changeset
3376 dir.prec[0] = -1;
kono
parents:
diff changeset
3377 dir.prec[1] = target_int_max ();
kono
parents:
diff changeset
3378 }
kono
parents:
diff changeset
3379 }
kono
parents:
diff changeset
3380 else
kono
parents:
diff changeset
3381 {
kono
parents:
diff changeset
3382 if (precision == LONG_MAX && perange)
kono
parents:
diff changeset
3383 {
kono
parents:
diff changeset
3384 size_t begin = dir.beg - info.fmtstr + (pprec - pcnt) - 1;
kono
parents:
diff changeset
3385 size_t caret = dir.beg - info.fmtstr + (perange - pcnt) - 1;
kono
parents:
diff changeset
3386 size_t end = pf - info.fmtstr - 2;
kono
parents:
diff changeset
3387
kono
parents:
diff changeset
3388 /* Create a location for the precision part of the directive,
kono
parents:
diff changeset
3389 including the leading period, pointing the caret at the first
kono
parents:
diff changeset
3390 out-of-range digit . */
kono
parents:
diff changeset
3391 substring_loc dirloc (info.fmtloc, TREE_TYPE (info.format),
kono
parents:
diff changeset
3392 caret, begin, end);
kono
parents:
diff changeset
3393
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3394 fmtwarn (dirloc, UNKNOWN_LOCATION, NULL, info.warnopt (),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3395 "%<%.*s%> directive precision out of range", (int) dir.len,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3396 target_to_host (hostdir, sizeof hostdir, dir.beg));
111
kono
parents:
diff changeset
3397 }
kono
parents:
diff changeset
3398
kono
parents:
diff changeset
3399 dir.set_precision (precision);
kono
parents:
diff changeset
3400 }
kono
parents:
diff changeset
3401
kono
parents:
diff changeset
3402 /* Extract the argument if the directive takes one and if it's
kono
parents:
diff changeset
3403 available (e.g., the function doesn't take a va_list). Treat
kono
parents:
diff changeset
3404 missing arguments the same as va_list, even though they will
kono
parents:
diff changeset
3405 have likely already been diagnosed by -Wformat. */
kono
parents:
diff changeset
3406 if (dir.specifier != '%'
kono
parents:
diff changeset
3407 && *argno < gimple_call_num_args (info.callstmt))
kono
parents:
diff changeset
3408 dir.arg = gimple_call_arg (info.callstmt, dollar ? dollar : (*argno)++);
kono
parents:
diff changeset
3409
kono
parents:
diff changeset
3410 if (dump_file)
kono
parents:
diff changeset
3411 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3412 fprintf (dump_file,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3413 " Directive %u at offset " HOST_WIDE_INT_PRINT_UNSIGNED
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3414 ": \"%.*s\"",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3415 dir.dirno,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3416 (unsigned HOST_WIDE_INT)(size_t)(dir.beg - info.fmtstr),
111
kono
parents:
diff changeset
3417 (int)dir.len, dir.beg);
kono
parents:
diff changeset
3418 if (star_width)
kono
parents:
diff changeset
3419 {
kono
parents:
diff changeset
3420 if (dir.width[0] == dir.width[1])
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3421 fprintf (dump_file, ", width = " HOST_WIDE_INT_PRINT_DEC,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3422 dir.width[0]);
111
kono
parents:
diff changeset
3423 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3424 fprintf (dump_file,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3425 ", width in range [" HOST_WIDE_INT_PRINT_DEC
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3426 ", " HOST_WIDE_INT_PRINT_DEC "]",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3427 dir.width[0], dir.width[1]);
111
kono
parents:
diff changeset
3428 }
kono
parents:
diff changeset
3429
kono
parents:
diff changeset
3430 if (star_precision)
kono
parents:
diff changeset
3431 {
kono
parents:
diff changeset
3432 if (dir.prec[0] == dir.prec[1])
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3433 fprintf (dump_file, ", precision = " HOST_WIDE_INT_PRINT_DEC,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3434 dir.prec[0]);
111
kono
parents:
diff changeset
3435 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3436 fprintf (dump_file,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3437 ", precision in range [" HOST_WIDE_INT_PRINT_DEC
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3438 HOST_WIDE_INT_PRINT_DEC "]",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3439 dir.prec[0], dir.prec[1]);
111
kono
parents:
diff changeset
3440 }
kono
parents:
diff changeset
3441 fputc ('\n', dump_file);
kono
parents:
diff changeset
3442 }
kono
parents:
diff changeset
3443
kono
parents:
diff changeset
3444 return dir.len;
kono
parents:
diff changeset
3445 }
kono
parents:
diff changeset
3446
kono
parents:
diff changeset
3447 /* Compute the length of the output resulting from the call to a formatted
kono
parents:
diff changeset
3448 output function described by INFO and store the result of the call in
kono
parents:
diff changeset
3449 *RES. Issue warnings for detected past the end writes. Return true
kono
parents:
diff changeset
3450 if the complete format string has been processed and *RES can be relied
kono
parents:
diff changeset
3451 on, false otherwise (e.g., when a unknown or unhandled directive was seen
kono
parents:
diff changeset
3452 that caused the processing to be terminated early). */
kono
parents:
diff changeset
3453
kono
parents:
diff changeset
3454 bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3455 sprintf_dom_walker::compute_format_length (call_info &info,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3456 format_result *res)
111
kono
parents:
diff changeset
3457 {
kono
parents:
diff changeset
3458 if (dump_file)
kono
parents:
diff changeset
3459 {
kono
parents:
diff changeset
3460 location_t callloc = gimple_location (info.callstmt);
kono
parents:
diff changeset
3461 fprintf (dump_file, "%s:%i: ",
kono
parents:
diff changeset
3462 LOCATION_FILE (callloc), LOCATION_LINE (callloc));
kono
parents:
diff changeset
3463 print_generic_expr (dump_file, info.func, dump_flags);
kono
parents:
diff changeset
3464
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3465 fprintf (dump_file,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3466 ": objsize = " HOST_WIDE_INT_PRINT_UNSIGNED
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3467 ", fmtstr = \"%s\"\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3468 info.objsize, info.fmtstr);
111
kono
parents:
diff changeset
3469 }
kono
parents:
diff changeset
3470
kono
parents:
diff changeset
3471 /* Reset the minimum and maximum byte counters. */
kono
parents:
diff changeset
3472 res->range.min = res->range.max = 0;
kono
parents:
diff changeset
3473
kono
parents:
diff changeset
3474 /* No directive has been seen yet so the length of output is bounded
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3475 by the known range [0, 0] (with no conversion resulting in a failure
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3476 or producing more than 4K bytes) until determined otherwise. */
111
kono
parents:
diff changeset
3477 res->knownrange = true;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3478 res->posunder4k = true;
111
kono
parents:
diff changeset
3479 res->floating = false;
kono
parents:
diff changeset
3480 res->warned = false;
kono
parents:
diff changeset
3481
kono
parents:
diff changeset
3482 /* 1-based directive counter. */
kono
parents:
diff changeset
3483 unsigned dirno = 1;
kono
parents:
diff changeset
3484
kono
parents:
diff changeset
3485 /* The variadic argument counter. */
kono
parents:
diff changeset
3486 unsigned argno = info.argidx;
kono
parents:
diff changeset
3487
kono
parents:
diff changeset
3488 for (const char *pf = info.fmtstr; ; ++dirno)
kono
parents:
diff changeset
3489 {
kono
parents:
diff changeset
3490 directive dir = directive ();
kono
parents:
diff changeset
3491 dir.dirno = dirno;
kono
parents:
diff changeset
3492
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3493 size_t n = parse_directive (info, dir, res, pf, &argno,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3494 evrp_range_analyzer.get_vr_values ());
111
kono
parents:
diff changeset
3495
kono
parents:
diff changeset
3496 /* Return failure if the format function fails. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3497 if (!format_directive (info, res, dir,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3498 evrp_range_analyzer.get_vr_values ()))
111
kono
parents:
diff changeset
3499 return false;
kono
parents:
diff changeset
3500
kono
parents:
diff changeset
3501 /* Return success the directive is zero bytes long and it's
kono
parents:
diff changeset
3502 the last think in the format string (i.e., it's the terminating
kono
parents:
diff changeset
3503 nul, which isn't really a directive but handling it as one makes
kono
parents:
diff changeset
3504 things simpler). */
kono
parents:
diff changeset
3505 if (!n)
kono
parents:
diff changeset
3506 return *pf == '\0';
kono
parents:
diff changeset
3507
kono
parents:
diff changeset
3508 pf += n;
kono
parents:
diff changeset
3509 }
kono
parents:
diff changeset
3510
kono
parents:
diff changeset
3511 /* The complete format string was processed (with or without warnings). */
kono
parents:
diff changeset
3512 return true;
kono
parents:
diff changeset
3513 }
kono
parents:
diff changeset
3514
kono
parents:
diff changeset
3515 /* Return the size of the object referenced by the expression DEST if
kono
parents:
diff changeset
3516 available, or -1 otherwise. */
kono
parents:
diff changeset
3517
kono
parents:
diff changeset
3518 static unsigned HOST_WIDE_INT
kono
parents:
diff changeset
3519 get_destination_size (tree dest)
kono
parents:
diff changeset
3520 {
kono
parents:
diff changeset
3521 /* Initialize object size info before trying to compute it. */
kono
parents:
diff changeset
3522 init_object_sizes ();
kono
parents:
diff changeset
3523
kono
parents:
diff changeset
3524 /* Use __builtin_object_size to determine the size of the destination
kono
parents:
diff changeset
3525 object. When optimizing, determine the smallest object (such as
kono
parents:
diff changeset
3526 a member array as opposed to the whole enclosing object), otherwise
kono
parents:
diff changeset
3527 use type-zero object size to determine the size of the enclosing
kono
parents:
diff changeset
3528 object (the function fails without optimization in this type). */
kono
parents:
diff changeset
3529 int ost = optimize > 0;
kono
parents:
diff changeset
3530 unsigned HOST_WIDE_INT size;
kono
parents:
diff changeset
3531 if (compute_builtin_object_size (dest, ost, &size))
kono
parents:
diff changeset
3532 return size;
kono
parents:
diff changeset
3533
kono
parents:
diff changeset
3534 return HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
3535 }
kono
parents:
diff changeset
3536
kono
parents:
diff changeset
3537 /* Return true if the call described by INFO with result RES safe to
kono
parents:
diff changeset
3538 optimize (i.e., no undefined behavior), and set RETVAL to the range
kono
parents:
diff changeset
3539 of its return values. */
kono
parents:
diff changeset
3540
kono
parents:
diff changeset
3541 static bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3542 is_call_safe (const sprintf_dom_walker::call_info &info,
111
kono
parents:
diff changeset
3543 const format_result &res, bool under4k,
kono
parents:
diff changeset
3544 unsigned HOST_WIDE_INT retval[2])
kono
parents:
diff changeset
3545 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3546 if (under4k && !res.posunder4k)
111
kono
parents:
diff changeset
3547 return false;
kono
parents:
diff changeset
3548
kono
parents:
diff changeset
3549 /* The minimum return value. */
kono
parents:
diff changeset
3550 retval[0] = res.range.min;
kono
parents:
diff changeset
3551
kono
parents:
diff changeset
3552 /* The maximum return value is in most cases bounded by RES.RANGE.MAX
kono
parents:
diff changeset
3553 but in cases involving multibyte characters could be as large as
kono
parents:
diff changeset
3554 RES.RANGE.UNLIKELY. */
kono
parents:
diff changeset
3555 retval[1]
kono
parents:
diff changeset
3556 = res.range.unlikely < res.range.max ? res.range.max : res.range.unlikely;
kono
parents:
diff changeset
3557
kono
parents:
diff changeset
3558 /* Adjust the number of bytes which includes the terminating nul
kono
parents:
diff changeset
3559 to reflect the return value of the function which does not.
kono
parents:
diff changeset
3560 Because the valid range of the function is [INT_MIN, INT_MAX],
kono
parents:
diff changeset
3561 a valid range before the adjustment below is [0, INT_MAX + 1]
kono
parents:
diff changeset
3562 (the functions only return negative values on error or undefined
kono
parents:
diff changeset
3563 behavior). */
kono
parents:
diff changeset
3564 if (retval[0] <= target_int_max () + 1)
kono
parents:
diff changeset
3565 --retval[0];
kono
parents:
diff changeset
3566 if (retval[1] <= target_int_max () + 1)
kono
parents:
diff changeset
3567 --retval[1];
kono
parents:
diff changeset
3568
kono
parents:
diff changeset
3569 /* Avoid the return value optimization when the behavior of the call
kono
parents:
diff changeset
3570 is undefined either because any directive may have produced 4K or
kono
parents:
diff changeset
3571 more of output, or the return value exceeds INT_MAX, or because
kono
parents:
diff changeset
3572 the output overflows the destination object (but leave it enabled
kono
parents:
diff changeset
3573 when the function is bounded because then the behavior is well-
kono
parents:
diff changeset
3574 defined). */
kono
parents:
diff changeset
3575 if (retval[0] == retval[1]
kono
parents:
diff changeset
3576 && (info.bounded || retval[0] < info.objsize)
kono
parents:
diff changeset
3577 && retval[0] <= target_int_max ())
kono
parents:
diff changeset
3578 return true;
kono
parents:
diff changeset
3579
kono
parents:
diff changeset
3580 if ((info.bounded || retval[1] < info.objsize)
kono
parents:
diff changeset
3581 && (retval[0] < target_int_max ()
kono
parents:
diff changeset
3582 && retval[1] < target_int_max ()))
kono
parents:
diff changeset
3583 return true;
kono
parents:
diff changeset
3584
kono
parents:
diff changeset
3585 if (!under4k && (info.bounded || retval[0] < info.objsize))
kono
parents:
diff changeset
3586 return true;
kono
parents:
diff changeset
3587
kono
parents:
diff changeset
3588 return false;
kono
parents:
diff changeset
3589 }
kono
parents:
diff changeset
3590
kono
parents:
diff changeset
3591 /* Given a suitable result RES of a call to a formatted output function
kono
parents:
diff changeset
3592 described by INFO, substitute the result for the return value of
kono
parents:
diff changeset
3593 the call. The result is suitable if the number of bytes it represents
kono
parents:
diff changeset
3594 is known and exact. A result that isn't suitable for substitution may
kono
parents:
diff changeset
3595 have its range set to the range of return values, if that is known.
kono
parents:
diff changeset
3596 Return true if the call is removed and gsi_next should not be performed
kono
parents:
diff changeset
3597 in the caller. */
kono
parents:
diff changeset
3598
kono
parents:
diff changeset
3599 static bool
kono
parents:
diff changeset
3600 try_substitute_return_value (gimple_stmt_iterator *gsi,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3601 const sprintf_dom_walker::call_info &info,
111
kono
parents:
diff changeset
3602 const format_result &res)
kono
parents:
diff changeset
3603 {
kono
parents:
diff changeset
3604 tree lhs = gimple_get_lhs (info.callstmt);
kono
parents:
diff changeset
3605
kono
parents:
diff changeset
3606 /* Set to true when the entire call has been removed. */
kono
parents:
diff changeset
3607 bool removed = false;
kono
parents:
diff changeset
3608
kono
parents:
diff changeset
3609 /* The minimum and maximum return value. */
kono
parents:
diff changeset
3610 unsigned HOST_WIDE_INT retval[2];
kono
parents:
diff changeset
3611 bool safe = is_call_safe (info, res, true, retval);
kono
parents:
diff changeset
3612
kono
parents:
diff changeset
3613 if (safe
kono
parents:
diff changeset
3614 && retval[0] == retval[1]
kono
parents:
diff changeset
3615 /* Not prepared to handle possibly throwing calls here; they shouldn't
kono
parents:
diff changeset
3616 appear in non-artificial testcases, except when the __*_chk routines
kono
parents:
diff changeset
3617 are badly declared. */
kono
parents:
diff changeset
3618 && !stmt_ends_bb_p (info.callstmt))
kono
parents:
diff changeset
3619 {
kono
parents:
diff changeset
3620 tree cst = build_int_cst (integer_type_node, retval[0]);
kono
parents:
diff changeset
3621
kono
parents:
diff changeset
3622 if (lhs == NULL_TREE
kono
parents:
diff changeset
3623 && info.nowrite)
kono
parents:
diff changeset
3624 {
kono
parents:
diff changeset
3625 /* Remove the call to the bounded function with a zero size
kono
parents:
diff changeset
3626 (e.g., snprintf(0, 0, "%i", 123)) if there is no lhs. */
kono
parents:
diff changeset
3627 unlink_stmt_vdef (info.callstmt);
kono
parents:
diff changeset
3628 gsi_remove (gsi, true);
kono
parents:
diff changeset
3629 removed = true;
kono
parents:
diff changeset
3630 }
kono
parents:
diff changeset
3631 else if (info.nowrite)
kono
parents:
diff changeset
3632 {
kono
parents:
diff changeset
3633 /* Replace the call to the bounded function with a zero size
kono
parents:
diff changeset
3634 (e.g., snprintf(0, 0, "%i", 123) with the constant result
kono
parents:
diff changeset
3635 of the function. */
kono
parents:
diff changeset
3636 if (!update_call_from_tree (gsi, cst))
kono
parents:
diff changeset
3637 gimplify_and_update_call_from_tree (gsi, cst);
kono
parents:
diff changeset
3638 gimple *callstmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
3639 update_stmt (callstmt);
kono
parents:
diff changeset
3640 }
kono
parents:
diff changeset
3641 else if (lhs)
kono
parents:
diff changeset
3642 {
kono
parents:
diff changeset
3643 /* Replace the left-hand side of the call with the constant
kono
parents:
diff changeset
3644 result of the formatted function. */
kono
parents:
diff changeset
3645 gimple_call_set_lhs (info.callstmt, NULL_TREE);
kono
parents:
diff changeset
3646 gimple *g = gimple_build_assign (lhs, cst);
kono
parents:
diff changeset
3647 gsi_insert_after (gsi, g, GSI_NEW_STMT);
kono
parents:
diff changeset
3648 update_stmt (info.callstmt);
kono
parents:
diff changeset
3649 }
kono
parents:
diff changeset
3650
kono
parents:
diff changeset
3651 if (dump_file)
kono
parents:
diff changeset
3652 {
kono
parents:
diff changeset
3653 if (removed)
kono
parents:
diff changeset
3654 fprintf (dump_file, " Removing call statement.");
kono
parents:
diff changeset
3655 else
kono
parents:
diff changeset
3656 {
kono
parents:
diff changeset
3657 fprintf (dump_file, " Substituting ");
kono
parents:
diff changeset
3658 print_generic_expr (dump_file, cst, dump_flags);
kono
parents:
diff changeset
3659 fprintf (dump_file, " for %s.\n",
kono
parents:
diff changeset
3660 info.nowrite ? "statement" : "return value");
kono
parents:
diff changeset
3661 }
kono
parents:
diff changeset
3662 }
kono
parents:
diff changeset
3663 }
kono
parents:
diff changeset
3664 else if (lhs)
kono
parents:
diff changeset
3665 {
kono
parents:
diff changeset
3666 bool setrange = false;
kono
parents:
diff changeset
3667
kono
parents:
diff changeset
3668 if (safe
kono
parents:
diff changeset
3669 && (info.bounded || retval[1] < info.objsize)
kono
parents:
diff changeset
3670 && (retval[0] < target_int_max ()
kono
parents:
diff changeset
3671 && retval[1] < target_int_max ()))
kono
parents:
diff changeset
3672 {
kono
parents:
diff changeset
3673 /* If the result is in a valid range bounded by the size of
kono
parents:
diff changeset
3674 the destination set it so that it can be used for subsequent
kono
parents:
diff changeset
3675 optimizations. */
kono
parents:
diff changeset
3676 int prec = TYPE_PRECISION (integer_type_node);
kono
parents:
diff changeset
3677
kono
parents:
diff changeset
3678 wide_int min = wi::shwi (retval[0], prec);
kono
parents:
diff changeset
3679 wide_int max = wi::shwi (retval[1], prec);
kono
parents:
diff changeset
3680 set_range_info (lhs, VR_RANGE, min, max);
kono
parents:
diff changeset
3681
kono
parents:
diff changeset
3682 setrange = true;
kono
parents:
diff changeset
3683 }
kono
parents:
diff changeset
3684
kono
parents:
diff changeset
3685 if (dump_file)
kono
parents:
diff changeset
3686 {
kono
parents:
diff changeset
3687 const char *inbounds
kono
parents:
diff changeset
3688 = (retval[0] < info.objsize
kono
parents:
diff changeset
3689 ? (retval[1] < info.objsize
kono
parents:
diff changeset
3690 ? "in" : "potentially out-of")
kono
parents:
diff changeset
3691 : "out-of");
kono
parents:
diff changeset
3692
kono
parents:
diff changeset
3693 const char *what = setrange ? "Setting" : "Discarding";
kono
parents:
diff changeset
3694 if (retval[0] != retval[1])
kono
parents:
diff changeset
3695 fprintf (dump_file,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3696 " %s %s-bounds return value range ["
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3697 HOST_WIDE_INT_PRINT_UNSIGNED ", "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3698 HOST_WIDE_INT_PRINT_UNSIGNED "].\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3699 what, inbounds, retval[0], retval[1]);
111
kono
parents:
diff changeset
3700 else
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3701 fprintf (dump_file, " %s %s-bounds return value "
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3702 HOST_WIDE_INT_PRINT_UNSIGNED ".\n",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3703 what, inbounds, retval[0]);
111
kono
parents:
diff changeset
3704 }
kono
parents:
diff changeset
3705 }
kono
parents:
diff changeset
3706
kono
parents:
diff changeset
3707 if (dump_file)
kono
parents:
diff changeset
3708 fputc ('\n', dump_file);
kono
parents:
diff changeset
3709
kono
parents:
diff changeset
3710 return removed;
kono
parents:
diff changeset
3711 }
kono
parents:
diff changeset
3712
kono
parents:
diff changeset
3713 /* Try to simplify a s{,n}printf call described by INFO with result
kono
parents:
diff changeset
3714 RES by replacing it with a simpler and presumably more efficient
kono
parents:
diff changeset
3715 call (such as strcpy). */
kono
parents:
diff changeset
3716
kono
parents:
diff changeset
3717 static bool
kono
parents:
diff changeset
3718 try_simplify_call (gimple_stmt_iterator *gsi,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3719 const sprintf_dom_walker::call_info &info,
111
kono
parents:
diff changeset
3720 const format_result &res)
kono
parents:
diff changeset
3721 {
kono
parents:
diff changeset
3722 unsigned HOST_WIDE_INT dummy[2];
kono
parents:
diff changeset
3723 if (!is_call_safe (info, res, info.retval_used (), dummy))
kono
parents:
diff changeset
3724 return false;
kono
parents:
diff changeset
3725
kono
parents:
diff changeset
3726 switch (info.fncode)
kono
parents:
diff changeset
3727 {
kono
parents:
diff changeset
3728 case BUILT_IN_SNPRINTF:
kono
parents:
diff changeset
3729 return gimple_fold_builtin_snprintf (gsi);
kono
parents:
diff changeset
3730
kono
parents:
diff changeset
3731 case BUILT_IN_SPRINTF:
kono
parents:
diff changeset
3732 return gimple_fold_builtin_sprintf (gsi);
kono
parents:
diff changeset
3733
kono
parents:
diff changeset
3734 default:
kono
parents:
diff changeset
3735 ;
kono
parents:
diff changeset
3736 }
kono
parents:
diff changeset
3737
kono
parents:
diff changeset
3738 return false;
kono
parents:
diff changeset
3739 }
kono
parents:
diff changeset
3740
kono
parents:
diff changeset
3741 /* Determine if a GIMPLE CALL is to one of the sprintf-like built-in
kono
parents:
diff changeset
3742 functions and if so, handle it. Return true if the call is removed
kono
parents:
diff changeset
3743 and gsi_next should not be performed in the caller. */
kono
parents:
diff changeset
3744
kono
parents:
diff changeset
3745 bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3746 sprintf_dom_walker::handle_gimple_call (gimple_stmt_iterator *gsi)
111
kono
parents:
diff changeset
3747 {
kono
parents:
diff changeset
3748 call_info info = call_info ();
kono
parents:
diff changeset
3749
kono
parents:
diff changeset
3750 info.callstmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
3751 if (!gimple_call_builtin_p (info.callstmt, BUILT_IN_NORMAL))
kono
parents:
diff changeset
3752 return false;
kono
parents:
diff changeset
3753
kono
parents:
diff changeset
3754 info.func = gimple_call_fndecl (info.callstmt);
kono
parents:
diff changeset
3755 info.fncode = DECL_FUNCTION_CODE (info.func);
kono
parents:
diff changeset
3756
kono
parents:
diff changeset
3757 /* The size of the destination as in snprintf(dest, size, ...). */
kono
parents:
diff changeset
3758 unsigned HOST_WIDE_INT dstsize = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
3759
kono
parents:
diff changeset
3760 /* The size of the destination determined by __builtin_object_size. */
kono
parents:
diff changeset
3761 unsigned HOST_WIDE_INT objsize = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
3762
kono
parents:
diff changeset
3763 /* Buffer size argument number (snprintf and vsnprintf). */
kono
parents:
diff changeset
3764 unsigned HOST_WIDE_INT idx_dstsize = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
3765
kono
parents:
diff changeset
3766 /* Object size argument number (snprintf_chk and vsnprintf_chk). */
kono
parents:
diff changeset
3767 unsigned HOST_WIDE_INT idx_objsize = HOST_WIDE_INT_M1U;
kono
parents:
diff changeset
3768
kono
parents:
diff changeset
3769 /* Format string argument number (valid for all functions). */
kono
parents:
diff changeset
3770 unsigned idx_format;
kono
parents:
diff changeset
3771
kono
parents:
diff changeset
3772 switch (info.fncode)
kono
parents:
diff changeset
3773 {
kono
parents:
diff changeset
3774 case BUILT_IN_SPRINTF:
kono
parents:
diff changeset
3775 // Signature:
kono
parents:
diff changeset
3776 // __builtin_sprintf (dst, format, ...)
kono
parents:
diff changeset
3777 idx_format = 1;
kono
parents:
diff changeset
3778 info.argidx = 2;
kono
parents:
diff changeset
3779 break;
kono
parents:
diff changeset
3780
kono
parents:
diff changeset
3781 case BUILT_IN_SPRINTF_CHK:
kono
parents:
diff changeset
3782 // Signature:
kono
parents:
diff changeset
3783 // __builtin___sprintf_chk (dst, ost, objsize, format, ...)
kono
parents:
diff changeset
3784 idx_objsize = 2;
kono
parents:
diff changeset
3785 idx_format = 3;
kono
parents:
diff changeset
3786 info.argidx = 4;
kono
parents:
diff changeset
3787 break;
kono
parents:
diff changeset
3788
kono
parents:
diff changeset
3789 case BUILT_IN_SNPRINTF:
kono
parents:
diff changeset
3790 // Signature:
kono
parents:
diff changeset
3791 // __builtin_snprintf (dst, size, format, ...)
kono
parents:
diff changeset
3792 idx_dstsize = 1;
kono
parents:
diff changeset
3793 idx_format = 2;
kono
parents:
diff changeset
3794 info.argidx = 3;
kono
parents:
diff changeset
3795 info.bounded = true;
kono
parents:
diff changeset
3796 break;
kono
parents:
diff changeset
3797
kono
parents:
diff changeset
3798 case BUILT_IN_SNPRINTF_CHK:
kono
parents:
diff changeset
3799 // Signature:
kono
parents:
diff changeset
3800 // __builtin___snprintf_chk (dst, size, ost, objsize, format, ...)
kono
parents:
diff changeset
3801 idx_dstsize = 1;
kono
parents:
diff changeset
3802 idx_objsize = 3;
kono
parents:
diff changeset
3803 idx_format = 4;
kono
parents:
diff changeset
3804 info.argidx = 5;
kono
parents:
diff changeset
3805 info.bounded = true;
kono
parents:
diff changeset
3806 break;
kono
parents:
diff changeset
3807
kono
parents:
diff changeset
3808 case BUILT_IN_VSNPRINTF:
kono
parents:
diff changeset
3809 // Signature:
kono
parents:
diff changeset
3810 // __builtin_vsprintf (dst, size, format, va)
kono
parents:
diff changeset
3811 idx_dstsize = 1;
kono
parents:
diff changeset
3812 idx_format = 2;
kono
parents:
diff changeset
3813 info.argidx = -1;
kono
parents:
diff changeset
3814 info.bounded = true;
kono
parents:
diff changeset
3815 break;
kono
parents:
diff changeset
3816
kono
parents:
diff changeset
3817 case BUILT_IN_VSNPRINTF_CHK:
kono
parents:
diff changeset
3818 // Signature:
kono
parents:
diff changeset
3819 // __builtin___vsnprintf_chk (dst, size, ost, objsize, format, va)
kono
parents:
diff changeset
3820 idx_dstsize = 1;
kono
parents:
diff changeset
3821 idx_objsize = 3;
kono
parents:
diff changeset
3822 idx_format = 4;
kono
parents:
diff changeset
3823 info.argidx = -1;
kono
parents:
diff changeset
3824 info.bounded = true;
kono
parents:
diff changeset
3825 break;
kono
parents:
diff changeset
3826
kono
parents:
diff changeset
3827 case BUILT_IN_VSPRINTF:
kono
parents:
diff changeset
3828 // Signature:
kono
parents:
diff changeset
3829 // __builtin_vsprintf (dst, format, va)
kono
parents:
diff changeset
3830 idx_format = 1;
kono
parents:
diff changeset
3831 info.argidx = -1;
kono
parents:
diff changeset
3832 break;
kono
parents:
diff changeset
3833
kono
parents:
diff changeset
3834 case BUILT_IN_VSPRINTF_CHK:
kono
parents:
diff changeset
3835 // Signature:
kono
parents:
diff changeset
3836 // __builtin___vsprintf_chk (dst, ost, objsize, format, va)
kono
parents:
diff changeset
3837 idx_format = 3;
kono
parents:
diff changeset
3838 idx_objsize = 2;
kono
parents:
diff changeset
3839 info.argidx = -1;
kono
parents:
diff changeset
3840 break;
kono
parents:
diff changeset
3841
kono
parents:
diff changeset
3842 default:
kono
parents:
diff changeset
3843 return false;
kono
parents:
diff changeset
3844 }
kono
parents:
diff changeset
3845
kono
parents:
diff changeset
3846 /* Set the global warning level for this function. */
kono
parents:
diff changeset
3847 warn_level = info.bounded ? warn_format_trunc : warn_format_overflow;
kono
parents:
diff changeset
3848
kono
parents:
diff changeset
3849 /* The first argument is a pointer to the destination. */
kono
parents:
diff changeset
3850 tree dstptr = gimple_call_arg (info.callstmt, 0);
kono
parents:
diff changeset
3851
kono
parents:
diff changeset
3852 info.format = gimple_call_arg (info.callstmt, idx_format);
kono
parents:
diff changeset
3853
kono
parents:
diff changeset
3854 /* True when the destination size is constant as opposed to the lower
kono
parents:
diff changeset
3855 or upper bound of a range. */
kono
parents:
diff changeset
3856 bool dstsize_cst_p = true;
kono
parents:
diff changeset
3857
kono
parents:
diff changeset
3858 if (idx_dstsize == HOST_WIDE_INT_M1U)
kono
parents:
diff changeset
3859 {
kono
parents:
diff changeset
3860 /* For non-bounded functions like sprintf, determine the size
kono
parents:
diff changeset
3861 of the destination from the object or pointer passed to it
kono
parents:
diff changeset
3862 as the first argument. */
kono
parents:
diff changeset
3863 dstsize = get_destination_size (dstptr);
kono
parents:
diff changeset
3864 }
kono
parents:
diff changeset
3865 else if (tree size = gimple_call_arg (info.callstmt, idx_dstsize))
kono
parents:
diff changeset
3866 {
kono
parents:
diff changeset
3867 /* For bounded functions try to get the size argument. */
kono
parents:
diff changeset
3868
kono
parents:
diff changeset
3869 if (TREE_CODE (size) == INTEGER_CST)
kono
parents:
diff changeset
3870 {
kono
parents:
diff changeset
3871 dstsize = tree_to_uhwi (size);
kono
parents:
diff changeset
3872 /* No object can be larger than SIZE_MAX bytes (half the address
kono
parents:
diff changeset
3873 space) on the target.
kono
parents:
diff changeset
3874 The functions are defined only for output of at most INT_MAX
kono
parents:
diff changeset
3875 bytes. Specifying a bound in excess of that limit effectively
kono
parents:
diff changeset
3876 defeats the bounds checking (and on some implementations such
kono
parents:
diff changeset
3877 as Solaris cause the function to fail with EINVAL). */
kono
parents:
diff changeset
3878 if (dstsize > target_size_max () / 2)
kono
parents:
diff changeset
3879 {
kono
parents:
diff changeset
3880 /* Avoid warning if -Wstringop-overflow is specified since
kono
parents:
diff changeset
3881 it also warns for the same thing though only for the
kono
parents:
diff changeset
3882 checking built-ins. */
kono
parents:
diff changeset
3883 if ((idx_objsize == HOST_WIDE_INT_M1U
kono
parents:
diff changeset
3884 || !warn_stringop_overflow))
kono
parents:
diff changeset
3885 warning_at (gimple_location (info.callstmt), info.warnopt (),
kono
parents:
diff changeset
3886 "specified bound %wu exceeds maximum object size "
kono
parents:
diff changeset
3887 "%wu",
kono
parents:
diff changeset
3888 dstsize, target_size_max () / 2);
kono
parents:
diff changeset
3889 }
kono
parents:
diff changeset
3890 else if (dstsize > target_int_max ())
kono
parents:
diff changeset
3891 warning_at (gimple_location (info.callstmt), info.warnopt (),
kono
parents:
diff changeset
3892 "specified bound %wu exceeds %<INT_MAX%>",
kono
parents:
diff changeset
3893 dstsize);
kono
parents:
diff changeset
3894 }
kono
parents:
diff changeset
3895 else if (TREE_CODE (size) == SSA_NAME)
kono
parents:
diff changeset
3896 {
kono
parents:
diff changeset
3897 /* Try to determine the range of values of the argument
kono
parents:
diff changeset
3898 and use the greater of the two at level 1 and the smaller
kono
parents:
diff changeset
3899 of them at level 2. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3900 value_range *vr = evrp_range_analyzer.get_value_range (size);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3901 if (range_int_cst_p (vr))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3902 dstsize = (warn_level < 2
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3903 ? TREE_INT_CST_LOW (vr->max ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3904 : TREE_INT_CST_LOW (vr->min ()));
111
kono
parents:
diff changeset
3905
kono
parents:
diff changeset
3906 /* The destination size is not constant. If the function is
kono
parents:
diff changeset
3907 bounded (e.g., snprintf) a lower bound of zero doesn't
kono
parents:
diff changeset
3908 necessarily imply it can be eliminated. */
kono
parents:
diff changeset
3909 dstsize_cst_p = false;
kono
parents:
diff changeset
3910 }
kono
parents:
diff changeset
3911 }
kono
parents:
diff changeset
3912
kono
parents:
diff changeset
3913 if (idx_objsize != HOST_WIDE_INT_M1U)
kono
parents:
diff changeset
3914 if (tree size = gimple_call_arg (info.callstmt, idx_objsize))
kono
parents:
diff changeset
3915 if (tree_fits_uhwi_p (size))
kono
parents:
diff changeset
3916 objsize = tree_to_uhwi (size);
kono
parents:
diff changeset
3917
kono
parents:
diff changeset
3918 if (info.bounded && !dstsize)
kono
parents:
diff changeset
3919 {
kono
parents:
diff changeset
3920 /* As a special case, when the explicitly specified destination
kono
parents:
diff changeset
3921 size argument (to a bounded function like snprintf) is zero
kono
parents:
diff changeset
3922 it is a request to determine the number of bytes on output
kono
parents:
diff changeset
3923 without actually producing any. Pretend the size is
kono
parents:
diff changeset
3924 unlimited in this case. */
kono
parents:
diff changeset
3925 info.objsize = HOST_WIDE_INT_MAX;
kono
parents:
diff changeset
3926 info.nowrite = dstsize_cst_p;
kono
parents:
diff changeset
3927 }
kono
parents:
diff changeset
3928 else
kono
parents:
diff changeset
3929 {
kono
parents:
diff changeset
3930 /* For calls to non-bounded functions or to those of bounded
kono
parents:
diff changeset
3931 functions with a non-zero size, warn if the destination
kono
parents:
diff changeset
3932 pointer is null. */
kono
parents:
diff changeset
3933 if (integer_zerop (dstptr))
kono
parents:
diff changeset
3934 {
kono
parents:
diff changeset
3935 /* This is diagnosed with -Wformat only when the null is a constant
kono
parents:
diff changeset
3936 pointer. The warning here diagnoses instances where the pointer
kono
parents:
diff changeset
3937 is not constant. */
kono
parents:
diff changeset
3938 location_t loc = gimple_location (info.callstmt);
kono
parents:
diff changeset
3939 warning_at (EXPR_LOC_OR_LOC (dstptr, loc),
kono
parents:
diff changeset
3940 info.warnopt (), "null destination pointer");
kono
parents:
diff changeset
3941 return false;
kono
parents:
diff changeset
3942 }
kono
parents:
diff changeset
3943
kono
parents:
diff changeset
3944 /* Set the object size to the smaller of the two arguments
kono
parents:
diff changeset
3945 of both have been specified and they're not equal. */
kono
parents:
diff changeset
3946 info.objsize = dstsize < objsize ? dstsize : objsize;
kono
parents:
diff changeset
3947
kono
parents:
diff changeset
3948 if (info.bounded
kono
parents:
diff changeset
3949 && dstsize < target_size_max () / 2 && objsize < dstsize
kono
parents:
diff changeset
3950 /* Avoid warning if -Wstringop-overflow is specified since
kono
parents:
diff changeset
3951 it also warns for the same thing though only for the
kono
parents:
diff changeset
3952 checking built-ins. */
kono
parents:
diff changeset
3953 && (idx_objsize == HOST_WIDE_INT_M1U
kono
parents:
diff changeset
3954 || !warn_stringop_overflow))
kono
parents:
diff changeset
3955 {
kono
parents:
diff changeset
3956 warning_at (gimple_location (info.callstmt), info.warnopt (),
kono
parents:
diff changeset
3957 "specified bound %wu exceeds the size %wu "
kono
parents:
diff changeset
3958 "of the destination object", dstsize, objsize);
kono
parents:
diff changeset
3959 }
kono
parents:
diff changeset
3960 }
kono
parents:
diff changeset
3961
kono
parents:
diff changeset
3962 if (integer_zerop (info.format))
kono
parents:
diff changeset
3963 {
kono
parents:
diff changeset
3964 /* This is diagnosed with -Wformat only when the null is a constant
kono
parents:
diff changeset
3965 pointer. The warning here diagnoses instances where the pointer
kono
parents:
diff changeset
3966 is not constant. */
kono
parents:
diff changeset
3967 location_t loc = gimple_location (info.callstmt);
kono
parents:
diff changeset
3968 warning_at (EXPR_LOC_OR_LOC (info.format, loc),
kono
parents:
diff changeset
3969 info.warnopt (), "null format string");
kono
parents:
diff changeset
3970 return false;
kono
parents:
diff changeset
3971 }
kono
parents:
diff changeset
3972
kono
parents:
diff changeset
3973 info.fmtstr = get_format_string (info.format, &info.fmtloc);
kono
parents:
diff changeset
3974 if (!info.fmtstr)
kono
parents:
diff changeset
3975 return false;
kono
parents:
diff changeset
3976
kono
parents:
diff changeset
3977 /* The result is the number of bytes output by the formatted function,
kono
parents:
diff changeset
3978 including the terminating NUL. */
kono
parents:
diff changeset
3979 format_result res = format_result ();
kono
parents:
diff changeset
3980
kono
parents:
diff changeset
3981 bool success = compute_format_length (info, &res);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3982 if (res.warned)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
3983 gimple_set_no_warning (info.callstmt, true);
111
kono
parents:
diff changeset
3984
kono
parents:
diff changeset
3985 /* When optimizing and the printf return value optimization is enabled,
kono
parents:
diff changeset
3986 attempt to substitute the computed result for the return value of
kono
parents:
diff changeset
3987 the call. Avoid this optimization when -frounding-math is in effect
kono
parents:
diff changeset
3988 and the format string contains a floating point directive. */
kono
parents:
diff changeset
3989 bool call_removed = false;
kono
parents:
diff changeset
3990 if (success && optimize > 0)
kono
parents:
diff changeset
3991 {
kono
parents:
diff changeset
3992 /* Save a copy of the iterator pointing at the call. The iterator
kono
parents:
diff changeset
3993 may change to point past the call in try_substitute_return_value
kono
parents:
diff changeset
3994 but the original value is needed in try_simplify_call. */
kono
parents:
diff changeset
3995 gimple_stmt_iterator gsi_call = *gsi;
kono
parents:
diff changeset
3996
kono
parents:
diff changeset
3997 if (flag_printf_return_value
kono
parents:
diff changeset
3998 && (!flag_rounding_math || !res.floating))
kono
parents:
diff changeset
3999 call_removed = try_substitute_return_value (gsi, info, res);
kono
parents:
diff changeset
4000
kono
parents:
diff changeset
4001 if (!call_removed)
kono
parents:
diff changeset
4002 try_simplify_call (&gsi_call, info, res);
kono
parents:
diff changeset
4003 }
kono
parents:
diff changeset
4004
kono
parents:
diff changeset
4005 return call_removed;
kono
parents:
diff changeset
4006 }
kono
parents:
diff changeset
4007
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4008 edge
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4009 sprintf_dom_walker::before_dom_children (basic_block bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4010 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4011 evrp_range_analyzer.enter (bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4012 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si); )
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4013 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4014 /* Iterate over statements, looking for function calls. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4015 gimple *stmt = gsi_stmt (si);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4016
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4017 /* First record ranges generated by this statement. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4018 evrp_range_analyzer.record_ranges_from_stmt (stmt, false);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4019
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4020 if (is_gimple_call (stmt) && handle_gimple_call (&si))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4021 /* If handle_gimple_call returns true, the iterator is
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4022 already pointing to the next statement. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4023 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4024
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4025 gsi_next (&si);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4026 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4027 return NULL;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4028 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4029
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4030 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4031 sprintf_dom_walker::after_dom_children (basic_block bb)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4032 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4033 evrp_range_analyzer.leave (bb);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4034 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4035
111
kono
parents:
diff changeset
4036 /* Execute the pass for function FUN. */
kono
parents:
diff changeset
4037
kono
parents:
diff changeset
4038 unsigned int
kono
parents:
diff changeset
4039 pass_sprintf_length::execute (function *fun)
kono
parents:
diff changeset
4040 {
kono
parents:
diff changeset
4041 init_target_to_host_charmap ();
kono
parents:
diff changeset
4042
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4043 calculate_dominance_info (CDI_DOMINATORS);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4044
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4045 sprintf_dom_walker sprintf_dom_walker;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
4046 sprintf_dom_walker.walk (ENTRY_BLOCK_PTR_FOR_FN (fun));
111
kono
parents:
diff changeset
4047
kono
parents:
diff changeset
4048 /* Clean up object size info. */
kono
parents:
diff changeset
4049 fini_object_sizes ();
kono
parents:
diff changeset
4050 return 0;
kono
parents:
diff changeset
4051 }
kono
parents:
diff changeset
4052
kono
parents:
diff changeset
4053 } /* Unnamed namespace. */
kono
parents:
diff changeset
4054
kono
parents:
diff changeset
4055 /* Return a pointer to a pass object newly constructed from the context
kono
parents:
diff changeset
4056 CTXT. */
kono
parents:
diff changeset
4057
kono
parents:
diff changeset
4058 gimple_opt_pass *
kono
parents:
diff changeset
4059 make_pass_sprintf_length (gcc::context *ctxt)
kono
parents:
diff changeset
4060 {
kono
parents:
diff changeset
4061 return new pass_sprintf_length (ctxt);
kono
parents:
diff changeset
4062 }