]>
Commit | Line | Data |
---|---|---|
b30f223b | 1 | /* Subroutines shared by all languages that are variants of C. |
09f89307 | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 |
06ceef4e | 3 | Free Software Foundation, Inc. |
b30f223b RS |
4 | |
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
940d9d63 RK |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
b30f223b RS |
21 | |
22 | #include "config.h" | |
670ee920 | 23 | #include "system.h" |
b30f223b | 24 | #include "tree.h" |
b30f223b | 25 | #include "flags.h" |
5f6da302 | 26 | #include "toplev.h" |
d6f4ec51 | 27 | #include "output.h" |
e2af664c | 28 | #include "c-pragma.h" |
3932261a | 29 | #include "rtl.h" |
1526a060 | 30 | #include "ggc.h" |
c6991660 | 31 | #include "expr.h" |
8f17b5c5 | 32 | #include "c-common.h" |
5fd8e536 | 33 | #include "defaults.h" |
7bdb32b9 | 34 | #include "tm_p.h" |
6a357625 | 35 | #include "intl.h" |
3f80e6ac | 36 | #include "diagnostic.h" |
235cfbc4 | 37 | #include "obstack.h" |
c8724862 | 38 | #include "cpplib.h" |
9ba2e1ef | 39 | cpp_reader *parse_in; /* Declared in c-lex.h. */ |
c8724862 | 40 | |
12a39b12 JM |
41 | #undef WCHAR_TYPE_SIZE |
42 | #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node) | |
fef610be | 43 | |
eaa7c03f JM |
44 | /* We let tm.h override the types used here, to handle trivial differences |
45 | such as the choice of unsigned int or long unsigned int for size_t. | |
46 | When machines start needing nontrivial differences in the size type, | |
47 | it would be best to do something here to figure out automatically | |
48 | from other information what type to use. */ | |
49 | ||
50 | #ifndef SIZE_TYPE | |
51 | #define SIZE_TYPE "long unsigned int" | |
52 | #endif | |
53 | ||
54 | #ifndef WCHAR_TYPE | |
55 | #define WCHAR_TYPE "int" | |
56 | #endif | |
57 | ||
0884b60c BS |
58 | #ifndef PTRDIFF_TYPE |
59 | #define PTRDIFF_TYPE "long int" | |
60 | #endif | |
61 | ||
5fd8e536 JM |
62 | #ifndef WINT_TYPE |
63 | #define WINT_TYPE "unsigned int" | |
64 | #endif | |
65 | ||
66 | #ifndef INTMAX_TYPE | |
67 | #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
68 | ? "int" \ | |
69 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
70 | ? "long int" \ | |
71 | : "long long int")) | |
72 | #endif | |
73 | ||
74 | #ifndef UINTMAX_TYPE | |
75 | #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
76 | ? "unsigned int" \ | |
77 | : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \ | |
78 | ? "long unsigned int" \ | |
79 | : "long long unsigned int")) | |
80 | #endif | |
81 | ||
7f4edbcb | 82 | /* The following symbols are subsumed in the c_global_trees array, and |
d125d268 | 83 | listed here individually for documentation purposes. |
7f4edbcb BS |
84 | |
85 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
86 | ||
87 | tree short_integer_type_node; | |
88 | tree long_integer_type_node; | |
89 | tree long_long_integer_type_node; | |
90 | ||
91 | tree short_unsigned_type_node; | |
92 | tree long_unsigned_type_node; | |
93 | tree long_long_unsigned_type_node; | |
94 | ||
95 | tree boolean_type_node; | |
96 | tree boolean_false_node; | |
97 | tree boolean_true_node; | |
98 | ||
99 | tree ptrdiff_type_node; | |
100 | ||
101 | tree unsigned_char_type_node; | |
102 | tree signed_char_type_node; | |
103 | tree wchar_type_node; | |
104 | tree signed_wchar_type_node; | |
105 | tree unsigned_wchar_type_node; | |
106 | ||
107 | tree float_type_node; | |
108 | tree double_type_node; | |
109 | tree long_double_type_node; | |
110 | ||
111 | tree complex_integer_type_node; | |
112 | tree complex_float_type_node; | |
113 | tree complex_double_type_node; | |
114 | tree complex_long_double_type_node; | |
115 | ||
116 | tree intQI_type_node; | |
117 | tree intHI_type_node; | |
118 | tree intSI_type_node; | |
119 | tree intDI_type_node; | |
120 | tree intTI_type_node; | |
121 | ||
122 | tree unsigned_intQI_type_node; | |
123 | tree unsigned_intHI_type_node; | |
124 | tree unsigned_intSI_type_node; | |
125 | tree unsigned_intDI_type_node; | |
126 | tree unsigned_intTI_type_node; | |
127 | ||
128 | tree widest_integer_literal_type_node; | |
129 | tree widest_unsigned_literal_type_node; | |
130 | ||
131 | Nodes for types `void *' and `const void *'. | |
132 | ||
133 | tree ptr_type_node, const_ptr_type_node; | |
134 | ||
135 | Nodes for types `char *' and `const char *'. | |
136 | ||
137 | tree string_type_node, const_string_type_node; | |
138 | ||
139 | Type `char[SOMENUMBER]'. | |
140 | Used when an array of char is needed and the size is irrelevant. | |
141 | ||
142 | tree char_array_type_node; | |
143 | ||
144 | Type `int[SOMENUMBER]' or something like it. | |
145 | Used when an array of int needed and the size is irrelevant. | |
146 | ||
147 | tree int_array_type_node; | |
148 | ||
149 | Type `wchar_t[SOMENUMBER]' or something like it. | |
150 | Used when a wide string literal is created. | |
151 | ||
152 | tree wchar_array_type_node; | |
153 | ||
154 | Type `int ()' -- used for implicit declaration of functions. | |
155 | ||
156 | tree default_function_type; | |
157 | ||
158 | Function types `int (int)', etc. | |
159 | ||
160 | tree int_ftype_int; | |
161 | tree void_ftype; | |
162 | tree void_ftype_ptr; | |
163 | tree int_ftype_int; | |
164 | tree ptr_ftype_sizetype; | |
165 | ||
166 | A VOID_TYPE node, packaged in a TREE_LIST. | |
167 | ||
168 | tree void_list_node; | |
169 | ||
63ad61ed ZW |
170 | The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__. |
171 | ||
172 | tree function_id_node; | |
173 | tree pretty_function_id_node; | |
174 | tree func_id_node; | |
175 | ||
7f4edbcb BS |
176 | */ |
177 | ||
178 | tree c_global_trees[CTI_MAX]; | |
0b73773c | 179 | |
6bcedb4e MM |
180 | /* Nonzero means don't recognize the non-ANSI builtin functions. */ |
181 | ||
182 | int flag_no_builtin; | |
183 | ||
184 | /* Nonzero means don't recognize the non-ANSI builtin functions. | |
185 | -ansi sets this. */ | |
186 | ||
187 | int flag_no_nonansi_builtin; | |
188 | ||
eaa7c03f JM |
189 | /* Nonzero means give `double' the same size as `float'. */ |
190 | ||
191 | int flag_short_double; | |
192 | ||
193 | /* Nonzero means give `wchar_t' the same size as `short'. */ | |
194 | ||
195 | int flag_short_wchar; | |
196 | ||
0a7394bc MM |
197 | /* If non-NULL, dump the tree structure for the entire translation |
198 | unit to this file. */ | |
199 | ||
200 | const char *flag_dump_translation_unit; | |
201 | ||
4d808927 JM |
202 | /* Warn about *printf or *scanf format/argument anomalies. */ |
203 | ||
204 | int warn_format; | |
205 | ||
206 | /* Warn about Y2K problems with strftime formats. */ | |
207 | ||
208 | int warn_format_y2k; | |
209 | ||
210 | /* Warn about excess arguments to formats. */ | |
211 | ||
212 | int warn_format_extra_args; | |
213 | ||
214 | /* Warn about non-literal format arguments. */ | |
215 | ||
216 | int warn_format_nonliteral; | |
217 | ||
c907e684 JM |
218 | /* Warn about possible security problems with calls to format functions. */ |
219 | ||
220 | int warn_format_security; | |
221 | ||
2683ed8d BS |
222 | /* Nonzero means warn about possible violations of sequence point rules. */ |
223 | ||
224 | int warn_sequence_point; | |
225 | ||
f09f1de5 MM |
226 | /* The elements of `ridpointers' are identifier nodes for the reserved |
227 | type names and storage classes. It is indexed by a RID_... value. */ | |
228 | tree *ridpointers; | |
229 | ||
2ce07e2d NS |
230 | tree (*make_fname_decl) PARAMS ((tree, const char *, int)); |
231 | ||
ae499cce MM |
232 | /* If non-NULL, the address of a language-specific function that |
233 | returns 1 for language-specific statement codes. */ | |
234 | int (*lang_statement_code_p) PARAMS ((enum tree_code)); | |
235 | ||
8f17b5c5 MM |
236 | /* If non-NULL, the address of a language-specific function that takes |
237 | any action required right before expand_function_end is called. */ | |
238 | void (*lang_expand_function_end) PARAMS ((void)); | |
239 | ||
67673f5c MM |
240 | /* If this variable is defined to a non-NULL value, it will be called |
241 | after the file has been completely parsed. */ | |
242 | void (*back_end_hook) PARAMS ((tree)); | |
243 | ||
e78a3b42 RK |
244 | /* Nonzero means the expression being parsed will never be evaluated. |
245 | This is a count, since unevaluated expressions can nest. */ | |
246 | int skip_evaluation; | |
247 | ||
2786cbad | 248 | enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION, |
7d384cc0 | 249 | A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION, |
bbb1ae01 | 250 | A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED, |
a157febd | 251 | A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC, |
2a8f6b90 | 252 | A_NO_LIMIT_STACK, A_PURE}; |
53065596 | 253 | |
bb72a084 | 254 | enum format_type { printf_format_type, scanf_format_type, |
26f6672d | 255 | strftime_format_type, strfmon_format_type }; |
bb72a084 | 256 | |
6e090c76 KG |
257 | static void add_attribute PARAMS ((enum attrs, const char *, |
258 | int, int, int)); | |
259 | static void init_attributes PARAMS ((void)); | |
260 | static void record_function_format PARAMS ((tree, tree, enum format_type, | |
261 | int, int)); | |
262 | static void record_international_format PARAMS ((tree, tree, int)); | |
6e090c76 | 263 | static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree)); |
4724b3de | 264 | |
0a7ed33c BS |
265 | /* Keep a stack of if statements. We record the number of compound |
266 | statements seen up to the if keyword, as well as the line number | |
267 | and file of the if. If a potentially ambiguous else is seen, that | |
268 | fact is recorded; the warning is issued when we can be sure that | |
269 | the enclosing if statement does not have an else branch. */ | |
270 | typedef struct | |
271 | { | |
272 | int compstmt_count; | |
273 | int line; | |
dff01034 | 274 | const char *file; |
0a7ed33c | 275 | int needs_warning; |
8f17b5c5 | 276 | tree if_stmt; |
0a7ed33c BS |
277 | } if_elt; |
278 | ||
279 | static if_elt *if_stack; | |
6d819282 MK |
280 | |
281 | /* Amount of space in the if statement stack. */ | |
282 | static int if_stack_space = 0; | |
283 | ||
284 | /* Stack pointer. */ | |
285 | static int if_stack_pointer = 0; | |
286 | ||
8f17b5c5 | 287 | /* Record the start of an if-then, and record the start of it |
0a7ed33c BS |
288 | for ambiguous else detection. */ |
289 | ||
6d819282 | 290 | void |
8f17b5c5 | 291 | c_expand_start_cond (cond, compstmt_count) |
6d819282 | 292 | tree cond; |
6d819282 MK |
293 | int compstmt_count; |
294 | { | |
8f17b5c5 MM |
295 | tree if_stmt; |
296 | ||
6d819282 MK |
297 | /* Make sure there is enough space on the stack. */ |
298 | if (if_stack_space == 0) | |
299 | { | |
300 | if_stack_space = 10; | |
0a7ed33c | 301 | if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt)); |
6d819282 MK |
302 | } |
303 | else if (if_stack_space == if_stack_pointer) | |
304 | { | |
305 | if_stack_space += 10; | |
0a7ed33c | 306 | if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt)); |
6d819282 | 307 | } |
0a7ed33c | 308 | |
8f17b5c5 MM |
309 | if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE); |
310 | IF_COND (if_stmt) = cond; | |
311 | add_stmt (if_stmt); | |
312 | ||
6d819282 | 313 | /* Record this if statement. */ |
0a7ed33c BS |
314 | if_stack[if_stack_pointer].compstmt_count = compstmt_count; |
315 | if_stack[if_stack_pointer].file = input_filename; | |
316 | if_stack[if_stack_pointer].line = lineno; | |
317 | if_stack[if_stack_pointer].needs_warning = 0; | |
8f17b5c5 | 318 | if_stack[if_stack_pointer].if_stmt = if_stmt; |
0a7ed33c | 319 | if_stack_pointer++; |
8f17b5c5 | 320 | } |
6d819282 | 321 | |
8f17b5c5 MM |
322 | /* Called after the then-clause for an if-statement is processed. */ |
323 | ||
324 | void | |
325 | c_finish_then () | |
326 | { | |
327 | tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt; | |
328 | RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt)); | |
6d819282 MK |
329 | } |
330 | ||
8f17b5c5 | 331 | /* Record the end of an if-then. Optionally warn if a nested |
0a7ed33c BS |
332 | if statement had an ambiguous else clause. */ |
333 | ||
6d819282 MK |
334 | void |
335 | c_expand_end_cond () | |
336 | { | |
337 | if_stack_pointer--; | |
0a7ed33c BS |
338 | if (if_stack[if_stack_pointer].needs_warning) |
339 | warning_with_file_and_line (if_stack[if_stack_pointer].file, | |
340 | if_stack[if_stack_pointer].line, | |
341 | "suggest explicit braces to avoid ambiguous `else'"); | |
8f17b5c5 | 342 | last_expr_type = NULL_TREE; |
6d819282 MK |
343 | } |
344 | ||
8f17b5c5 | 345 | /* Called between the then-clause and the else-clause |
0a7ed33c BS |
346 | of an if-then-else. */ |
347 | ||
6d819282 MK |
348 | void |
349 | c_expand_start_else () | |
350 | { | |
0a7ed33c BS |
351 | /* An ambiguous else warning must be generated for the enclosing if |
352 | statement, unless we see an else branch for that one, too. */ | |
6d819282 MK |
353 | if (warn_parentheses |
354 | && if_stack_pointer > 1 | |
0a7ed33c BS |
355 | && (if_stack[if_stack_pointer - 1].compstmt_count |
356 | == if_stack[if_stack_pointer - 2].compstmt_count)) | |
357 | if_stack[if_stack_pointer - 2].needs_warning = 1; | |
358 | ||
359 | /* Even if a nested if statement had an else branch, it can't be | |
360 | ambiguous if this one also has an else. So don't warn in that | |
361 | case. Also don't warn for any if statements nested in this else. */ | |
362 | if_stack[if_stack_pointer - 1].needs_warning = 0; | |
363 | if_stack[if_stack_pointer - 1].compstmt_count--; | |
8f17b5c5 MM |
364 | } |
365 | ||
366 | /* Called after the else-clause for an if-statement is processed. */ | |
6d819282 | 367 | |
8f17b5c5 MM |
368 | void |
369 | c_finish_else () | |
370 | { | |
371 | tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt; | |
372 | RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt)); | |
6d819282 MK |
373 | } |
374 | ||
6f4d7222 | 375 | /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */ |
7da551a2 RS |
376 | |
377 | void | |
378 | declare_function_name () | |
379 | { | |
dff01034 | 380 | const char *name, *printable_name; |
7da551a2 RS |
381 | |
382 | if (current_function_decl == NULL) | |
383 | { | |
384 | name = ""; | |
385 | printable_name = "top level"; | |
386 | } | |
387 | else | |
388 | { | |
6152f876 RS |
389 | /* Allow functions to be nameless (such as artificial ones). */ |
390 | if (DECL_NAME (current_function_decl)) | |
391 | name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl)); | |
392 | else | |
393 | name = ""; | |
a1d7ffe3 | 394 | printable_name = (*decl_printable_name) (current_function_decl, 2); |
63ad61ed ZW |
395 | |
396 | /* ISO C99 defines __func__, which is a variable, not a string | |
397 | constant, and which is not a defined symbol at file scope. */ | |
398 | (*make_fname_decl) (func_id_node, name, 0); | |
7da551a2 | 399 | } |
2ce07e2d | 400 | |
63ad61ed ZW |
401 | (*make_fname_decl) (function_id_node, name, 0); |
402 | (*make_fname_decl) (pretty_function_id_node, printable_name, 1); | |
7da551a2 RS |
403 | } |
404 | ||
b30f223b RS |
405 | /* Given a chain of STRING_CST nodes, |
406 | concatenate them into one STRING_CST | |
407 | and give it a suitable array-of-chars data type. */ | |
408 | ||
409 | tree | |
410 | combine_strings (strings) | |
411 | tree strings; | |
412 | { | |
413 | register tree value, t; | |
414 | register int length = 1; | |
415 | int wide_length = 0; | |
416 | int wide_flag = 0; | |
417 | int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT; | |
418 | int nchars; | |
1326a48b | 419 | const int nchars_max = flag_isoc99 ? 4095 : 509; |
b30f223b RS |
420 | |
421 | if (TREE_CHAIN (strings)) | |
422 | { | |
423 | /* More than one in the chain, so concatenate. */ | |
424 | register char *p, *q; | |
425 | ||
426 | /* Don't include the \0 at the end of each substring, | |
427 | except for the last one. | |
428 | Count wide strings and ordinary strings separately. */ | |
429 | for (t = strings; t; t = TREE_CHAIN (t)) | |
430 | { | |
431 | if (TREE_TYPE (t) == wchar_array_type_node) | |
432 | { | |
433 | wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes); | |
434 | wide_flag = 1; | |
435 | } | |
436 | else | |
437 | length += (TREE_STRING_LENGTH (t) - 1); | |
438 | } | |
439 | ||
440 | /* If anything is wide, the non-wides will be converted, | |
441 | which makes them take more space. */ | |
442 | if (wide_flag) | |
443 | length = length * wchar_bytes + wide_length; | |
444 | ||
520a57c8 | 445 | p = alloca (length); |
b30f223b RS |
446 | |
447 | /* Copy the individual strings into the new combined string. | |
448 | If the combined string is wide, convert the chars to ints | |
449 | for any individual strings that are not wide. */ | |
450 | ||
451 | q = p; | |
452 | for (t = strings; t; t = TREE_CHAIN (t)) | |
453 | { | |
454 | int len = (TREE_STRING_LENGTH (t) | |
455 | - ((TREE_TYPE (t) == wchar_array_type_node) | |
456 | ? wchar_bytes : 1)); | |
457 | if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag) | |
458 | { | |
7e2231e7 | 459 | memcpy (q, TREE_STRING_POINTER (t), len); |
b30f223b RS |
460 | q += len; |
461 | } | |
462 | else | |
463 | { | |
464 | int i; | |
465 | for (i = 0; i < len; i++) | |
41bbd14e JW |
466 | { |
467 | if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT) | |
468 | ((short *) q)[i] = TREE_STRING_POINTER (t)[i]; | |
469 | else | |
470 | ((int *) q)[i] = TREE_STRING_POINTER (t)[i]; | |
471 | } | |
b30f223b RS |
472 | q += len * wchar_bytes; |
473 | } | |
474 | } | |
475 | if (wide_flag) | |
476 | { | |
477 | int i; | |
478 | for (i = 0; i < wchar_bytes; i++) | |
479 | *q++ = 0; | |
480 | } | |
481 | else | |
482 | *q = 0; | |
483 | ||
520a57c8 | 484 | value = build_string (length, p); |
b30f223b RS |
485 | } |
486 | else | |
487 | { | |
488 | value = strings; | |
489 | length = TREE_STRING_LENGTH (value); | |
490 | if (TREE_TYPE (value) == wchar_array_type_node) | |
491 | wide_flag = 1; | |
492 | } | |
493 | ||
b57062ca | 494 | /* Compute the number of elements, for the array type. */ |
b30f223b RS |
495 | nchars = wide_flag ? length / wchar_bytes : length; |
496 | ||
690c96c8 | 497 | if (pedantic && nchars - 1 > nchars_max && c_language == clk_c) |
89abf8d1 | 498 | pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support", |
690c96c8 | 499 | nchars - 1, nchars_max, flag_isoc99 ? 99 : 89); |
1326a48b | 500 | |
b30f223b RS |
501 | /* Create the array type for the string constant. |
502 | -Wwrite-strings says make the string constant an array of const char | |
d9cf7c82 JM |
503 | so that copying it to a non-const pointer will get a warning. |
504 | For C++, this is the standard behavior. */ | |
505 | if (flag_const_strings | |
b30f223b RS |
506 | && (! flag_traditional && ! flag_writable_strings)) |
507 | { | |
508 | tree elements | |
509 | = build_type_variant (wide_flag ? wchar_type_node : char_type_node, | |
510 | 1, 0); | |
511 | TREE_TYPE (value) | |
512 | = build_array_type (elements, | |
513 | build_index_type (build_int_2 (nchars - 1, 0))); | |
514 | } | |
515 | else | |
516 | TREE_TYPE (value) | |
517 | = build_array_type (wide_flag ? wchar_type_node : char_type_node, | |
518 | build_index_type (build_int_2 (nchars - 1, 0))); | |
d9cf7c82 | 519 | |
ccd4c832 JM |
520 | TREE_CONSTANT (value) = 1; |
521 | TREE_READONLY (value) = ! flag_writable_strings; | |
b30f223b RS |
522 | TREE_STATIC (value) = 1; |
523 | return value; | |
524 | } | |
525 | \f | |
53065596 RK |
526 | /* To speed up processing of attributes, we maintain an array of |
527 | IDENTIFIER_NODES and the corresponding attribute types. */ | |
528 | ||
529 | /* Array to hold attribute information. */ | |
530 | ||
531 | static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50]; | |
532 | ||
533 | static int attrtab_idx = 0; | |
534 | ||
535 | /* Add an entry to the attribute table above. */ | |
536 | ||
537 | static void | |
538 | add_attribute (id, string, min_len, max_len, decl_req) | |
539 | enum attrs id; | |
dff01034 | 540 | const char *string; |
53065596 RK |
541 | int min_len, max_len; |
542 | int decl_req; | |
543 | { | |
544 | char buf[100]; | |
545 | ||
546 | attrtab[attrtab_idx].id = id; | |
547 | attrtab[attrtab_idx].name = get_identifier (string); | |
548 | attrtab[attrtab_idx].min = min_len; | |
549 | attrtab[attrtab_idx].max = max_len; | |
550 | attrtab[attrtab_idx++].decl_req = decl_req; | |
551 | ||
552 | sprintf (buf, "__%s__", string); | |
553 | ||
554 | attrtab[attrtab_idx].id = id; | |
555 | attrtab[attrtab_idx].name = get_identifier (buf); | |
556 | attrtab[attrtab_idx].min = min_len; | |
557 | attrtab[attrtab_idx].max = max_len; | |
558 | attrtab[attrtab_idx++].decl_req = decl_req; | |
559 | } | |
560 | ||
561 | /* Initialize attribute table. */ | |
562 | ||
563 | static void | |
564 | init_attributes () | |
565 | { | |
a89ca5c6 | 566 | add_attribute (A_PACKED, "packed", 0, 0, 0); |
bbb1ae01 | 567 | add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1); |
2786cbad | 568 | add_attribute (A_COMMON, "common", 0, 0, 1); |
53065596 RK |
569 | add_attribute (A_NORETURN, "noreturn", 0, 0, 1); |
570 | add_attribute (A_NORETURN, "volatile", 0, 0, 1); | |
6d819282 | 571 | add_attribute (A_UNUSED, "unused", 0, 0, 0); |
53065596 RK |
572 | add_attribute (A_CONST, "const", 0, 0, 1); |
573 | add_attribute (A_T_UNION, "transparent_union", 0, 0, 0); | |
574 | add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1); | |
575 | add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1); | |
576 | add_attribute (A_MODE, "mode", 1, 1, 1); | |
577 | add_attribute (A_SECTION, "section", 1, 1, 1); | |
578 | add_attribute (A_ALIGNED, "aligned", 0, 1, 0); | |
d161fae4 | 579 | add_attribute (A_FORMAT, "format", 3, 3, 1); |
0161e8da | 580 | add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1); |
4b8af8d9 JM |
581 | add_attribute (A_WEAK, "weak", 0, 0, 1); |
582 | add_attribute (A_ALIAS, "alias", 1, 1, 1); | |
07417085 | 583 | add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1); |
7d384cc0 | 584 | add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1); |
140592a0 | 585 | add_attribute (A_MALLOC, "malloc", 0, 0, 1); |
a157febd | 586 | add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1); |
2a8f6b90 | 587 | add_attribute (A_PURE, "pure", 0, 0, 1); |
53065596 RK |
588 | } |
589 | \f | |
adfaf194 JM |
590 | /* Default implementation of valid_lang_attribute, below. By default, there |
591 | are no language-specific attributes. */ | |
592 | ||
593 | static int | |
594 | default_valid_lang_attribute (attr_name, attr_args, decl, type) | |
595 | tree attr_name ATTRIBUTE_UNUSED; | |
596 | tree attr_args ATTRIBUTE_UNUSED; | |
597 | tree decl ATTRIBUTE_UNUSED; | |
598 | tree type ATTRIBUTE_UNUSED; | |
599 | { | |
600 | return 0; | |
601 | } | |
602 | ||
603 | /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific | |
604 | attribute for either declaration DECL or type TYPE and 0 otherwise. */ | |
605 | ||
6e090c76 | 606 | int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree)) |
adfaf194 JM |
607 | = default_valid_lang_attribute; |
608 | ||
1228e2a6 | 609 | /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES |
53065596 RK |
610 | and install them in NODE, which is either a DECL (including a TYPE_DECL) |
611 | or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers | |
0f41302f | 612 | and declaration modifiers but before the declaration proper. */ |
b30f223b RS |
613 | |
614 | void | |
53065596 RK |
615 | decl_attributes (node, attributes, prefix_attributes) |
616 | tree node, attributes, prefix_attributes; | |
b30f223b | 617 | { |
a16b4c9c JW |
618 | tree decl = 0, type = 0; |
619 | int is_type = 0; | |
53065596 RK |
620 | tree a; |
621 | ||
622 | if (attrtab_idx == 0) | |
623 | init_attributes (); | |
624 | ||
2f939d94 | 625 | if (DECL_P (node)) |
53065596 RK |
626 | { |
627 | decl = node; | |
628 | type = TREE_TYPE (decl); | |
629 | is_type = TREE_CODE (node) == TYPE_DECL; | |
630 | } | |
2f939d94 | 631 | else if (TYPE_P (node)) |
53065596 | 632 | type = node, is_type = 1; |
a2cd7b45 | 633 | |
e2af664c NC |
634 | #ifdef PRAGMA_INSERT_ATTRIBUTES |
635 | /* If the code in c-pragma.c wants to insert some attributes then | |
636 | allow it to do so. Do this before allowing machine back ends to | |
637 | insert attributes, so that they have the opportunity to override | |
638 | anything done here. */ | |
639 | PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes); | |
640 | #endif | |
d125d268 | 641 | |
f09db6e0 NC |
642 | #ifdef INSERT_ATTRIBUTES |
643 | INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes); | |
644 | #endif | |
d125d268 | 645 | |
eed32833 | 646 | attributes = chainon (prefix_attributes, attributes); |
5289b665 | 647 | |
b30f223b | 648 | for (a = attributes; a; a = TREE_CHAIN (a)) |
53065596 RK |
649 | { |
650 | tree name = TREE_PURPOSE (a); | |
651 | tree args = TREE_VALUE (a); | |
652 | int i; | |
653 | enum attrs id; | |
b57062ca | 654 | |
53065596 RK |
655 | for (i = 0; i < attrtab_idx; i++) |
656 | if (attrtab[i].name == name) | |
657 | break; | |
658 | ||
6eaba4a7 | 659 | if (i == attrtab_idx) |
53065596 | 660 | { |
adfaf194 JM |
661 | if (! valid_machine_attribute (name, args, decl, type) |
662 | && ! (* valid_lang_attribute) (name, args, decl, type)) | |
6eaba4a7 DE |
663 | warning ("`%s' attribute directive ignored", |
664 | IDENTIFIER_POINTER (name)); | |
17c1a44f SC |
665 | else if (decl != 0) |
666 | type = TREE_TYPE (decl); | |
53065596 RK |
667 | continue; |
668 | } | |
669 | else if (attrtab[i].decl_req && decl == 0) | |
670 | { | |
671 | warning ("`%s' attribute does not apply to types", | |
672 | IDENTIFIER_POINTER (name)); | |
673 | continue; | |
674 | } | |
675 | else if (list_length (args) < attrtab[i].min | |
676 | || list_length (args) > attrtab[i].max) | |
677 | { | |
678 | error ("wrong number of arguments specified for `%s' attribute", | |
679 | IDENTIFIER_POINTER (name)); | |
680 | continue; | |
681 | } | |
682 | ||
683 | id = attrtab[i].id; | |
684 | switch (id) | |
685 | { | |
686 | case A_PACKED: | |
1bcf5b08 | 687 | if (is_type) |
a89ca5c6 RK |
688 | TYPE_PACKED (type) = 1; |
689 | else if (TREE_CODE (decl) == FIELD_DECL) | |
53065596 RK |
690 | DECL_PACKED (decl) = 1; |
691 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is | |
692 | used for DECL_REGISTER. It wouldn't mean anything anyway. */ | |
693 | else | |
694 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
695 | break; | |
696 | ||
bbb1ae01 RK |
697 | case A_NOCOMMON: |
698 | if (TREE_CODE (decl) == VAR_DECL) | |
699 | DECL_COMMON (decl) = 0; | |
700 | else | |
701 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
702 | break; | |
703 | ||
2786cbad JM |
704 | case A_COMMON: |
705 | if (TREE_CODE (decl) == VAR_DECL) | |
706 | DECL_COMMON (decl) = 1; | |
707 | else | |
708 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
709 | break; | |
710 | ||
53065596 RK |
711 | case A_NORETURN: |
712 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
713 | TREE_THIS_VOLATILE (decl) = 1; | |
714 | else if (TREE_CODE (type) == POINTER_TYPE | |
715 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
b57062ca | 716 | TREE_TYPE (decl) = type |
53065596 RK |
717 | = build_pointer_type |
718 | (build_type_variant (TREE_TYPE (type), | |
719 | TREE_READONLY (TREE_TYPE (type)), 1)); | |
720 | else | |
721 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
722 | break; | |
723 | ||
140592a0 AG |
724 | case A_MALLOC: |
725 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
726 | DECL_IS_MALLOC (decl) = 1; | |
2a8f6b90 | 727 | /* ??? TODO: Support types. */ |
140592a0 AG |
728 | else |
729 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
730 | break; | |
731 | ||
1c4fadec | 732 | case A_UNUSED: |
6d819282 | 733 | if (is_type) |
d20a70b4 CM |
734 | if (decl) |
735 | TREE_USED (decl) = 1; | |
736 | else | |
737 | TREE_USED (type) = 1; | |
b57062ca | 738 | else if (TREE_CODE (decl) == PARM_DECL |
6d819282 | 739 | || TREE_CODE (decl) == VAR_DECL |
736b02fd KG |
740 | || TREE_CODE (decl) == FUNCTION_DECL |
741 | || TREE_CODE (decl) == LABEL_DECL) | |
1c4fadec RK |
742 | TREE_USED (decl) = 1; |
743 | else | |
744 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
745 | break; | |
746 | ||
53065596 RK |
747 | case A_CONST: |
748 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
749 | TREE_READONLY (decl) = 1; | |
750 | else if (TREE_CODE (type) == POINTER_TYPE | |
751 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
752 | TREE_TYPE (decl) = type | |
753 | = build_pointer_type | |
754 | (build_type_variant (TREE_TYPE (type), 1, | |
755 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
756 | else | |
757 | warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
758 | break; | |
759 | ||
2a8f6b90 JH |
760 | case A_PURE: |
761 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
762 | DECL_IS_PURE (decl) = 1; | |
763 | /* ??? TODO: Support types. */ | |
764 | else | |
765 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
766 | break; | |
767 | ||
768 | ||
53065596 | 769 | case A_T_UNION: |
1bcf5b08 | 770 | if (is_type |
53065596 | 771 | && TREE_CODE (type) == UNION_TYPE |
1bcf5b08 | 772 | && (decl == 0 |
62b1077c RK |
773 | || (TYPE_FIELDS (type) != 0 |
774 | && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))))) | |
1bcf5b08 RK |
775 | TYPE_TRANSPARENT_UNION (type) = 1; |
776 | else if (decl != 0 && TREE_CODE (decl) == PARM_DECL | |
53065596 RK |
777 | && TREE_CODE (type) == UNION_TYPE |
778 | && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))) | |
1bcf5b08 | 779 | DECL_TRANSPARENT_UNION (decl) = 1; |
53065596 RK |
780 | else |
781 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
782 | break; | |
783 | ||
784 | case A_CONSTRUCTOR: | |
785 | if (TREE_CODE (decl) == FUNCTION_DECL | |
786 | && TREE_CODE (type) == FUNCTION_TYPE | |
787 | && decl_function_context (decl) == 0) | |
1a16a053 RK |
788 | { |
789 | DECL_STATIC_CONSTRUCTOR (decl) = 1; | |
790 | TREE_USED (decl) = 1; | |
791 | } | |
53065596 RK |
792 | else |
793 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
794 | break; | |
795 | ||
796 | case A_DESTRUCTOR: | |
797 | if (TREE_CODE (decl) == FUNCTION_DECL | |
798 | && TREE_CODE (type) == FUNCTION_TYPE | |
799 | && decl_function_context (decl) == 0) | |
1a16a053 RK |
800 | { |
801 | DECL_STATIC_DESTRUCTOR (decl) = 1; | |
802 | TREE_USED (decl) = 1; | |
803 | } | |
53065596 RK |
804 | else |
805 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
806 | break; | |
807 | ||
808 | case A_MODE: | |
809 | if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE) | |
810 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
811 | else | |
812 | { | |
813 | int j; | |
dff01034 | 814 | const char *p = IDENTIFIER_POINTER (TREE_VALUE (args)); |
53065596 RK |
815 | int len = strlen (p); |
816 | enum machine_mode mode = VOIDmode; | |
817 | tree typefm; | |
818 | ||
819 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
820 | && p[len - 1] == '_' && p[len - 2] == '_') | |
821 | { | |
3438dff9 | 822 | char *newp = (char *) alloca (len - 1); |
53065596 RK |
823 | |
824 | strcpy (newp, &p[2]); | |
825 | newp[len - 4] = '\0'; | |
826 | p = newp; | |
827 | } | |
828 | ||
829 | /* Give this decl a type with the specified mode. | |
830 | First check for the special modes. */ | |
831 | if (! strcmp (p, "byte")) | |
832 | mode = byte_mode; | |
833 | else if (!strcmp (p, "word")) | |
834 | mode = word_mode; | |
835 | else if (! strcmp (p, "pointer")) | |
836 | mode = ptr_mode; | |
837 | else | |
838 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
839 | if (!strcmp (p, GET_MODE_NAME (j))) | |
840 | mode = (enum machine_mode) j; | |
841 | ||
842 | if (mode == VOIDmode) | |
843 | error ("unknown machine mode `%s'", p); | |
844 | else if (0 == (typefm = type_for_mode (mode, | |
845 | TREE_UNSIGNED (type)))) | |
846 | error ("no data type for mode `%s'", p); | |
847 | else | |
848 | { | |
b15ad712 JM |
849 | if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type) |
850 | ? TYPE_PRECISION(uintmax_type_node) | |
851 | : TYPE_PRECISION(intmax_type_node)) | |
852 | && pedantic) | |
853 | pedwarn ("type with more precision than %s", | |
854 | TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t"); | |
53065596 | 855 | TREE_TYPE (decl) = type = typefm; |
06ceef4e | 856 | DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0; |
53065596 RK |
857 | layout_decl (decl, 0); |
858 | } | |
859 | } | |
860 | break; | |
861 | ||
862 | case A_SECTION: | |
5289b665 | 863 | #ifdef ASM_OUTPUT_SECTION_NAME |
53065596 RK |
864 | if ((TREE_CODE (decl) == FUNCTION_DECL |
865 | || TREE_CODE (decl) == VAR_DECL) | |
866 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
867 | { | |
b57062ca | 868 | if (TREE_CODE (decl) == VAR_DECL |
f4ca236c RK |
869 | && current_function_decl != NULL_TREE |
870 | && ! TREE_STATIC (decl)) | |
53065596 RK |
871 | error_with_decl (decl, |
872 | "section attribute cannot be specified for local variables"); | |
873 | /* The decl may have already been given a section attribute from | |
874 | a previous declaration. Ensure they match. */ | |
875 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
876 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
877 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
878 | error_with_decl (node, | |
879 | "section of `%s' conflicts with previous declaration"); | |
880 | else | |
881 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
882 | } | |
883 | else | |
884 | error_with_decl (node, | |
5289b665 DE |
885 | "section attribute not allowed for `%s'"); |
886 | #else | |
53065596 RK |
887 | error_with_decl (node, |
888 | "section attributes are not supported for this target"); | |
5289b665 | 889 | #endif |
53065596 RK |
890 | break; |
891 | ||
892 | case A_ALIGNED: | |
6f38f669 | 893 | { |
53065596 | 894 | tree align_expr |
54630035 RK |
895 | = (args ? TREE_VALUE (args) |
896 | : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT)); | |
05bccae2 | 897 | int i; |
d125d268 | 898 | |
53065596 RK |
899 | /* Strip any NOPs of any kind. */ |
900 | while (TREE_CODE (align_expr) == NOP_EXPR | |
901 | || TREE_CODE (align_expr) == CONVERT_EXPR | |
902 | || TREE_CODE (align_expr) == NON_LVALUE_EXPR) | |
903 | align_expr = TREE_OPERAND (align_expr, 0); | |
b57062ca | 904 | |
53065596 RK |
905 | if (TREE_CODE (align_expr) != INTEGER_CST) |
906 | { | |
907 | error ("requested alignment is not a constant"); | |
908 | continue; | |
909 | } | |
910 | ||
05bccae2 | 911 | if ((i = tree_log2 (align_expr)) == -1) |
53065596 | 912 | error ("requested alignment is not a power of 2"); |
05bccae2 RK |
913 | else if (i > HOST_BITS_PER_INT - 2) |
914 | error ("requested alignment is too large"); | |
53065596 | 915 | else if (is_type) |
11cf4d18 | 916 | { |
aee3c6b0 JW |
917 | /* If we have a TYPE_DECL, then copy the type, so that we |
918 | don't accidentally modify a builtin type. See pushdecl. */ | |
919 | if (decl && TREE_TYPE (decl) != error_mark_node | |
920 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
af80d489 | 921 | { |
aee3c6b0 JW |
922 | tree tt = TREE_TYPE (decl); |
923 | DECL_ORIGINAL_TYPE (decl) = tt; | |
924 | tt = build_type_copy (tt); | |
925 | TYPE_NAME (tt) = decl; | |
926 | TREE_USED (tt) = TREE_USED (decl); | |
927 | TREE_TYPE (decl) = tt; | |
928 | type = tt; | |
af80d489 | 929 | } |
aee3c6b0 JW |
930 | |
931 | TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT; | |
932 | TYPE_USER_ALIGN (type) = 1; | |
11cf4d18 | 933 | } |
53065596 RK |
934 | else if (TREE_CODE (decl) != VAR_DECL |
935 | && TREE_CODE (decl) != FIELD_DECL) | |
936 | error_with_decl (decl, | |
937 | "alignment may not be specified for `%s'"); | |
938 | else | |
11cf4d18 JJ |
939 | { |
940 | DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT; | |
941 | DECL_USER_ALIGN (decl) = 1; | |
942 | } | |
6f38f669 | 943 | } |
53065596 | 944 | break; |
6f38f669 | 945 | |
53065596 | 946 | case A_FORMAT: |
b30f223b | 947 | { |
bb72a084 | 948 | tree format_type_id = TREE_VALUE (args); |
53065596 RK |
949 | tree format_num_expr = TREE_VALUE (TREE_CHAIN (args)); |
950 | tree first_arg_num_expr | |
951 | = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args))); | |
05bccae2 | 952 | unsigned HOST_WIDE_INT format_num, first_arg_num; |
bb72a084 | 953 | enum format_type format_type; |
53065596 | 954 | tree argument; |
05bccae2 | 955 | unsigned int arg_num; |
b57062ca | 956 | |
53065596 RK |
957 | if (TREE_CODE (decl) != FUNCTION_DECL) |
958 | { | |
959 | error_with_decl (decl, | |
960 | "argument format specified for non-function `%s'"); | |
961 | continue; | |
962 | } | |
d125d268 | 963 | |
bb72a084 | 964 | if (TREE_CODE (format_type_id) != IDENTIFIER_NODE) |
0161e8da | 965 | { |
bb72a084 | 966 | error ("unrecognized format specifier"); |
0161e8da RK |
967 | continue; |
968 | } | |
53065596 RK |
969 | else |
970 | { | |
dff01034 | 971 | const char *p = IDENTIFIER_POINTER (format_type_id); |
d125d268 | 972 | |
bb72a084 PE |
973 | if (!strcmp (p, "printf") || !strcmp (p, "__printf__")) |
974 | format_type = printf_format_type; | |
975 | else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__")) | |
976 | format_type = scanf_format_type; | |
977 | else if (!strcmp (p, "strftime") | |
978 | || !strcmp (p, "__strftime__")) | |
979 | format_type = strftime_format_type; | |
26f6672d JM |
980 | else if (!strcmp (p, "strfmon") |
981 | || !strcmp (p, "__strfmon__")) | |
982 | format_type = strfmon_format_type; | |
bb72a084 PE |
983 | else |
984 | { | |
b27d2bd5 | 985 | warning ("`%s' is an unrecognized format function type", p); |
bb72a084 PE |
986 | continue; |
987 | } | |
53065596 | 988 | } |
6f38f669 | 989 | |
53065596 RK |
990 | /* Strip any conversions from the string index and first arg number |
991 | and verify they are constants. */ | |
992 | while (TREE_CODE (format_num_expr) == NOP_EXPR | |
993 | || TREE_CODE (format_num_expr) == CONVERT_EXPR | |
994 | || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR) | |
995 | format_num_expr = TREE_OPERAND (format_num_expr, 0); | |
677ff441 | 996 | |
53065596 RK |
997 | while (TREE_CODE (first_arg_num_expr) == NOP_EXPR |
998 | || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR | |
999 | || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR) | |
1000 | first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0); | |
1001 | ||
1002 | if (TREE_CODE (format_num_expr) != INTEGER_CST | |
05bccae2 RK |
1003 | || TREE_INT_CST_HIGH (format_num_expr) != 0 |
1004 | || TREE_CODE (first_arg_num_expr) != INTEGER_CST | |
1005 | || TREE_INT_CST_HIGH (first_arg_num_expr) != 0) | |
09e3dd72 | 1006 | { |
05bccae2 | 1007 | error ("format string has invalid operand number"); |
53065596 | 1008 | continue; |
09e3dd72 | 1009 | } |
53065596 RK |
1010 | |
1011 | format_num = TREE_INT_CST_LOW (format_num_expr); | |
1012 | first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr); | |
1013 | if (first_arg_num != 0 && first_arg_num <= format_num) | |
68a91d8d | 1014 | { |
53065596 | 1015 | error ("format string arg follows the args to be formatted"); |
6f38f669 | 1016 | continue; |
68a91d8d | 1017 | } |
53065596 RK |
1018 | |
1019 | /* If a parameter list is specified, verify that the format_num | |
1020 | argument is actually a string, in case the format attribute | |
1021 | is in error. */ | |
1022 | argument = TYPE_ARG_TYPES (type); | |
1023 | if (argument) | |
09e3dd72 | 1024 | { |
05bccae2 RK |
1025 | for (arg_num = 1; argument != 0 && arg_num != format_num; |
1026 | ++arg_num, argument = TREE_CHAIN (argument)) | |
1027 | ; | |
1028 | ||
53065596 RK |
1029 | if (! argument |
1030 | || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE | |
1031 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument))) | |
1032 | != char_type_node)) | |
1033 | { | |
1034 | error ("format string arg not a string type"); | |
09e3dd72 RK |
1035 | continue; |
1036 | } | |
05bccae2 RK |
1037 | |
1038 | else if (first_arg_num != 0) | |
53065596 RK |
1039 | { |
1040 | /* Verify that first_arg_num points to the last arg, | |
0f41302f | 1041 | the ... */ |
53065596 RK |
1042 | while (argument) |
1043 | arg_num++, argument = TREE_CHAIN (argument); | |
05bccae2 RK |
1044 | |
1045 | if (arg_num != first_arg_num) | |
1046 | { | |
1047 | error ("args to be formatted is not '...'"); | |
1048 | continue; | |
1049 | } | |
53065596 | 1050 | } |
09e3dd72 | 1051 | } |
a2cd7b45 | 1052 | |
9fa8f75d JM |
1053 | if (format_type == strftime_format_type && first_arg_num != 0) |
1054 | { | |
1055 | error ("strftime formats cannot format arguments"); | |
1056 | continue; | |
1057 | } | |
1058 | ||
53065596 RK |
1059 | record_function_format (DECL_NAME (decl), |
1060 | DECL_ASSEMBLER_NAME (decl), | |
bb72a084 | 1061 | format_type, format_num, first_arg_num); |
53065596 RK |
1062 | break; |
1063 | } | |
4b8af8d9 | 1064 | |
0161e8da RK |
1065 | case A_FORMAT_ARG: |
1066 | { | |
1067 | tree format_num_expr = TREE_VALUE (args); | |
05bccae2 RK |
1068 | unsigned HOST_WIDE_INT format_num; |
1069 | unsigned int arg_num; | |
0161e8da | 1070 | tree argument; |
b57062ca | 1071 | |
0161e8da RK |
1072 | if (TREE_CODE (decl) != FUNCTION_DECL) |
1073 | { | |
1074 | error_with_decl (decl, | |
1075 | "argument format specified for non-function `%s'"); | |
1076 | continue; | |
1077 | } | |
b57062ca | 1078 | |
0161e8da RK |
1079 | /* Strip any conversions from the first arg number and verify it |
1080 | is a constant. */ | |
1081 | while (TREE_CODE (format_num_expr) == NOP_EXPR | |
1082 | || TREE_CODE (format_num_expr) == CONVERT_EXPR | |
1083 | || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR) | |
1084 | format_num_expr = TREE_OPERAND (format_num_expr, 0); | |
1085 | ||
05bccae2 RK |
1086 | if (TREE_CODE (format_num_expr) != INTEGER_CST |
1087 | || TREE_INT_CST_HIGH (format_num_expr) != 0) | |
0161e8da | 1088 | { |
05bccae2 | 1089 | error ("format string has invalid operand number"); |
0161e8da RK |
1090 | continue; |
1091 | } | |
1092 | ||
1093 | format_num = TREE_INT_CST_LOW (format_num_expr); | |
1094 | ||
1095 | /* If a parameter list is specified, verify that the format_num | |
1096 | argument is actually a string, in case the format attribute | |
1097 | is in error. */ | |
1098 | argument = TYPE_ARG_TYPES (type); | |
1099 | if (argument) | |
1100 | { | |
05bccae2 RK |
1101 | for (arg_num = 1; argument != 0 && arg_num != format_num; |
1102 | ++arg_num, argument = TREE_CHAIN (argument)) | |
1103 | ; | |
1104 | ||
0161e8da RK |
1105 | if (! argument |
1106 | || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE | |
1107 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument))) | |
1108 | != char_type_node)) | |
1109 | { | |
1110 | error ("format string arg not a string type"); | |
1111 | continue; | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE | |
1116 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl)))) | |
1117 | != char_type_node)) | |
1118 | { | |
1119 | error ("function does not return string type"); | |
1120 | continue; | |
1121 | } | |
1122 | ||
1123 | record_international_format (DECL_NAME (decl), | |
1124 | DECL_ASSEMBLER_NAME (decl), | |
1125 | format_num); | |
1126 | break; | |
1127 | } | |
1128 | ||
4b8af8d9 JM |
1129 | case A_WEAK: |
1130 | declare_weak (decl); | |
1131 | break; | |
1132 | ||
1133 | case A_ALIAS: | |
1134 | if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
5d9dd5a5 | 1135 | || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl))) |
4b8af8d9 JM |
1136 | error_with_decl (decl, |
1137 | "`%s' defined both normally and as an alias"); | |
1138 | else if (decl_function_context (decl) == 0) | |
1139 | { | |
20045452 RH |
1140 | tree id; |
1141 | ||
1142 | id = TREE_VALUE (args); | |
1143 | if (TREE_CODE (id) != STRING_CST) | |
1144 | { | |
1145 | error ("alias arg not a string"); | |
1146 | break; | |
1147 | } | |
1148 | id = get_identifier (TREE_STRING_POINTER (id)); | |
06a50fff ZW |
1149 | /* This counts as a use of the object pointed to. */ |
1150 | TREE_USED (id) = 1; | |
20045452 | 1151 | |
4b8af8d9 JM |
1152 | if (TREE_CODE (decl) == FUNCTION_DECL) |
1153 | DECL_INITIAL (decl) = error_mark_node; | |
1154 | else | |
1155 | DECL_EXTERNAL (decl) = 0; | |
1156 | assemble_alias (decl, id); | |
1157 | } | |
1158 | else | |
1159 | warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); | |
1160 | break; | |
07417085 | 1161 | |
7d384cc0 KR |
1162 | case A_NO_CHECK_MEMORY_USAGE: |
1163 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
1164 | { | |
1165 | error_with_decl (decl, | |
1166 | "`%s' attribute applies only to functions", | |
1167 | IDENTIFIER_POINTER (name)); | |
1168 | } | |
1169 | else if (DECL_INITIAL (decl)) | |
1170 | { | |
1171 | error_with_decl (decl, | |
1172 | "can't set `%s' attribute after definition", | |
1173 | IDENTIFIER_POINTER (name)); | |
1174 | } | |
1175 | else | |
1176 | DECL_NO_CHECK_MEMORY_USAGE (decl) = 1; | |
1177 | break; | |
1178 | ||
07417085 KR |
1179 | case A_NO_INSTRUMENT_FUNCTION: |
1180 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
1181 | { | |
1182 | error_with_decl (decl, | |
1183 | "`%s' attribute applies only to functions", | |
1184 | IDENTIFIER_POINTER (name)); | |
1185 | } | |
1186 | else if (DECL_INITIAL (decl)) | |
1187 | { | |
1188 | error_with_decl (decl, | |
1189 | "can't set `%s' attribute after definition", | |
1190 | IDENTIFIER_POINTER (name)); | |
1191 | } | |
1192 | else | |
1193 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
1194 | break; | |
a157febd GK |
1195 | |
1196 | case A_NO_LIMIT_STACK: | |
1197 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
1198 | { | |
1199 | error_with_decl (decl, | |
1200 | "`%s' attribute applies only to functions", | |
1201 | IDENTIFIER_POINTER (name)); | |
1202 | } | |
1203 | else if (DECL_INITIAL (decl)) | |
1204 | { | |
1205 | error_with_decl (decl, | |
1206 | "can't set `%s' attribute after definition", | |
1207 | IDENTIFIER_POINTER (name)); | |
1208 | } | |
1209 | else | |
1210 | DECL_NO_LIMIT_STACK (decl) = 1; | |
1211 | break; | |
53065596 RK |
1212 | } |
1213 | } | |
b30f223b | 1214 | } |
800f4153 RK |
1215 | |
1216 | /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two | |
1217 | lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE). | |
1218 | ||
1219 | The head of the declspec list is stored in DECLSPECS. | |
1220 | The head of the attribute list is stored in PREFIX_ATTRIBUTES. | |
1221 | ||
1222 | Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of | |
1223 | the list elements. We drop the containing TREE_LIST nodes and link the | |
1224 | resulting attributes together the way decl_attributes expects them. */ | |
1225 | ||
1226 | void | |
1227 | split_specs_attrs (specs_attrs, declspecs, prefix_attributes) | |
1228 | tree specs_attrs; | |
1229 | tree *declspecs, *prefix_attributes; | |
1230 | { | |
1231 | tree t, s, a, next, specs, attrs; | |
1232 | ||
101e59f4 ML |
1233 | /* This can happen after an __extension__ in pedantic mode. */ |
1234 | if (specs_attrs != NULL_TREE | |
1235 | && TREE_CODE (specs_attrs) == INTEGER_CST) | |
1236 | { | |
1237 | *declspecs = NULL_TREE; | |
1238 | *prefix_attributes = NULL_TREE; | |
1239 | return; | |
1240 | } | |
1241 | ||
800f4153 RK |
1242 | /* This can happen in c++ (eg: decl: typespec initdecls ';'). */ |
1243 | if (specs_attrs != NULL_TREE | |
1244 | && TREE_CODE (specs_attrs) != TREE_LIST) | |
1245 | { | |
1246 | *declspecs = specs_attrs; | |
1247 | *prefix_attributes = NULL_TREE; | |
1248 | return; | |
1249 | } | |
1250 | ||
1251 | /* Remember to keep the lists in the same order, element-wise. */ | |
1252 | ||
1253 | specs = s = NULL_TREE; | |
1254 | attrs = a = NULL_TREE; | |
1255 | for (t = specs_attrs; t; t = next) | |
1256 | { | |
1257 | next = TREE_CHAIN (t); | |
1258 | /* Declspecs have a non-NULL TREE_VALUE. */ | |
1259 | if (TREE_VALUE (t) != NULL_TREE) | |
1260 | { | |
1261 | if (specs == NULL_TREE) | |
1262 | specs = s = t; | |
1263 | else | |
1264 | { | |
1265 | TREE_CHAIN (s) = t; | |
1266 | s = t; | |
1267 | } | |
1268 | } | |
1269 | else | |
1270 | { | |
1271 | if (attrs == NULL_TREE) | |
1272 | attrs = a = TREE_PURPOSE (t); | |
1273 | else | |
1274 | { | |
1275 | TREE_CHAIN (a) = TREE_PURPOSE (t); | |
1276 | a = TREE_PURPOSE (t); | |
1277 | } | |
1278 | /* More attrs can be linked here, move A to the end. */ | |
1279 | while (TREE_CHAIN (a) != NULL_TREE) | |
1280 | a = TREE_CHAIN (a); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | /* Terminate the lists. */ | |
1285 | if (s != NULL_TREE) | |
1286 | TREE_CHAIN (s) = NULL_TREE; | |
1287 | if (a != NULL_TREE) | |
1288 | TREE_CHAIN (a) = NULL_TREE; | |
1289 | ||
1290 | /* All done. */ | |
1291 | *declspecs = specs; | |
1292 | *prefix_attributes = attrs; | |
1293 | } | |
d9525bec BK |
1294 | |
1295 | /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes. | |
1296 | This function is used by the parser when a rule will accept attributes | |
1297 | in a particular position, but we don't want to support that just yet. | |
1298 | ||
1299 | A warning is issued for every ignored attribute. */ | |
1300 | ||
1301 | tree | |
1302 | strip_attrs (specs_attrs) | |
1303 | tree specs_attrs; | |
1304 | { | |
1305 | tree specs, attrs; | |
1306 | ||
1307 | split_specs_attrs (specs_attrs, &specs, &attrs); | |
1308 | ||
1309 | while (attrs) | |
1310 | { | |
1311 | warning ("`%s' attribute ignored", | |
1312 | IDENTIFIER_POINTER (TREE_PURPOSE (attrs))); | |
1313 | attrs = TREE_CHAIN (attrs); | |
1314 | } | |
1315 | ||
1316 | return specs; | |
1317 | } | |
b30f223b | 1318 | \f |
1ccf251f RK |
1319 | /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against |
1320 | a parameter list. */ | |
1321 | ||
3c786c69 JM |
1322 | /* The meaningfully distinct length modifiers for format checking recognised |
1323 | by GCC. */ | |
1324 | enum format_lengths | |
1325 | { | |
1326 | FMT_LEN_none, | |
1327 | FMT_LEN_hh, | |
1328 | FMT_LEN_h, | |
1329 | FMT_LEN_l, | |
1330 | FMT_LEN_ll, | |
1331 | FMT_LEN_L, | |
1332 | FMT_LEN_z, | |
1333 | FMT_LEN_t, | |
1334 | FMT_LEN_j, | |
1335 | FMT_LEN_MAX | |
1336 | }; | |
1337 | ||
1338 | ||
1339 | /* The standard versions in which various format features appeared. */ | |
1340 | enum format_std_version | |
1341 | { | |
1342 | STD_C89, | |
1343 | STD_C94, | |
20d380b1 | 1344 | STD_C9L, /* C99, but treat as C89 if -Wno-long-long. */ |
3c786c69 JM |
1345 | STD_C99, |
1346 | STD_EXT | |
1347 | }; | |
1348 | ||
ade7d3cc JM |
1349 | /* The C standard version C++ is treated as equivalent to |
1350 | or inheriting from, for the purpose of format features supported. */ | |
1351 | #define CPLUSPLUS_STD_VER STD_C89 | |
1352 | /* The C standard version we are checking formats against when pedantic. */ | |
1353 | #define C_STD_VER (c_language == clk_cplusplus \ | |
1354 | ? CPLUSPLUS_STD_VER \ | |
1355 | : (flag_isoc99 \ | |
1356 | ? STD_C99 \ | |
1357 | : (flag_isoc94 ? STD_C94 : STD_C89))) | |
1358 | /* The name to give to the standard version we are warning about when | |
1359 | pedantic. FEATURE_VER is the version in which the feature warned out | |
1360 | appeared, which is higher than C_STD_VER. */ | |
1361 | #define C_STD_NAME(FEATURE_VER) (c_language == clk_cplusplus \ | |
1362 | ? "ISO C++" \ | |
1363 | : ((FEATURE_VER) == STD_EXT \ | |
1364 | ? "ISO C" \ | |
1365 | : "ISO C89")) | |
20d380b1 JM |
1366 | /* Adjust a C standard version, which may be STD_C9L, to account for |
1367 | -Wno-long-long. Returns other standard versions unchanged. */ | |
1368 | #define ADJ_STD(VER) ((int)((VER) == STD_C9L \ | |
1369 | ? (warn_long_long ? STD_C99 : STD_C89) \ | |
1370 | : (VER))) | |
3c786c69 | 1371 | |
2588e44e JM |
1372 | /* Flags that may apply to a particular kind of format checked by GCC. */ |
1373 | enum | |
1374 | { | |
1375 | /* This format converts arguments of types determined by the | |
1376 | format string. */ | |
1377 | FMT_FLAG_ARG_CONVERT = 1, | |
1378 | /* The scanf allocation 'a' kludge applies to this format kind. */ | |
1379 | FMT_FLAG_SCANF_A_KLUDGE = 2, | |
1380 | /* A % during parsing a specifier is allowed to be a modified % rather | |
1381 | that indicating the format is broken and we are out-of-sync. */ | |
b691cb00 JM |
1382 | FMT_FLAG_FANCY_PERCENT_OK = 4, |
1383 | /* With $ operand numbers, it is OK to reference the same argument more | |
1384 | than once. */ | |
26f6672d JM |
1385 | FMT_FLAG_DOLLAR_MULTIPLE = 8, |
1386 | /* This format type uses $ operand numbers (strfmon doesn't). */ | |
1387 | FMT_FLAG_USE_DOLLAR = 16, | |
1388 | /* Zero width is bad in this type of format (scanf). */ | |
1389 | FMT_FLAG_ZERO_WIDTH_BAD = 32, | |
1390 | /* Empty precision specification is OK in this type of format (printf). */ | |
1391 | FMT_FLAG_EMPTY_PREC_OK = 64 | |
2588e44e JM |
1392 | /* Not included here: details of whether width or precision may occur |
1393 | (controlled by width_char and precision_char); details of whether | |
1394 | '*' can be used for these (width_type and precision_type); details | |
26f6672d | 1395 | of whether length modifiers can occur (length_char_specs). */ |
2588e44e JM |
1396 | }; |
1397 | ||
1398 | ||
3c786c69 JM |
1399 | /* Structure describing a length modifier supported in format checking, and |
1400 | possibly a doubled version such as "hh". */ | |
1401 | typedef struct | |
1402 | { | |
1403 | /* Name of the single-character length modifier. */ | |
1404 | const char *name; | |
1405 | /* Index into a format_char_info.types array. */ | |
1406 | enum format_lengths index; | |
1407 | /* Standard version this length appears in. */ | |
1408 | enum format_std_version std; | |
1409 | /* Same, if the modifier can be repeated, or NULL if it can't. */ | |
1410 | const char *double_name; | |
1411 | enum format_lengths double_index; | |
1412 | enum format_std_version double_std; | |
1413 | } format_length_info; | |
1414 | ||
1415 | ||
1416 | /* Structure desribing the combination of a conversion specifier | |
1417 | (or a set of specifiers which act identically) and a length modifier. */ | |
1418 | typedef struct | |
1419 | { | |
1420 | /* The standard version this combination of length and type appeared in. | |
1421 | This is only relevant if greater than those for length and type | |
1422 | individually; otherwise it is ignored. */ | |
1423 | enum format_std_version std; | |
1424 | /* The name to use for the type, if different from that generated internally | |
1425 | (e.g., "signed size_t"). */ | |
1426 | const char *name; | |
1427 | /* The type itself. */ | |
1428 | tree *type; | |
1429 | } format_type_detail; | |
1430 | ||
1431 | ||
1432 | /* Macros to fill out tables of these. */ | |
1433 | #define BADLEN { 0, NULL, NULL } | |
1434 | #define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN } | |
1435 | ||
1436 | ||
1437 | /* Structure desribing a format conversion specifier (or a set of specifiers | |
1438 | which act identically), and the length modifiers used with it. */ | |
1439 | typedef struct | |
1440 | { | |
1441 | const char *format_chars; | |
1442 | int pointer_count; | |
1443 | enum format_std_version std; | |
1444 | /* Types accepted for each length modifier. */ | |
1445 | format_type_detail types[FMT_LEN_MAX]; | |
0e00b68b | 1446 | /* List of other modifier characters allowed with these specifiers. |
26f6672d JM |
1447 | This lists flags, and additionally "w" for width, "p" for precision |
1448 | (right precision, for strfmon), "#" for left precision (strfmon), | |
0e00b68b JM |
1449 | "a" for scanf "a" allocation extension (not applicable in C99 mode), |
1450 | "*" for scanf suppression, and "E" and "O" for those strftime | |
1451 | modifiers. */ | |
3c786c69 | 1452 | const char *flag_chars; |
0e00b68b JM |
1453 | /* List of additional flags describing these conversion specifiers. |
1454 | "c" for generic character pointers being allowed, "2" for strftime | |
1455 | two digit year formats, "3" for strftime formats giving two digit | |
2588e44e JM |
1456 | years in some locales, "4" for "2" which becomes "3" with an "E" modifier, |
1457 | "o" if use of strftime "O" is a GNU extension beyond C99, | |
1458 | "W" if the argument is a pointer which is dereferenced and written into, | |
014e7f1d | 1459 | "R" if the argument is a pointer which is dereferenced and read from, |
2588e44e JM |
1460 | "i" for printf integer formats where the '0' flag is ignored with |
1461 | precision, and "[" for the starting character of a scanf scanset. */ | |
0e00b68b | 1462 | const char *flags2; |
3c786c69 JM |
1463 | } format_char_info; |
1464 | ||
1465 | ||
2588e44e JM |
1466 | /* Structure describing a flag accepted by some kind of format. */ |
1467 | typedef struct | |
1468 | { | |
1469 | /* The flag character in question (0 for end of array). */ | |
1470 | int flag_char; | |
1471 | /* Zero if this entry describes the flag character in general, or a | |
1472 | non-zero character that may be found in flags2 if it describes the | |
1473 | flag when used with certain formats only. If the latter, only | |
1474 | the first such entry found that applies to the current conversion | |
1475 | specifier is used; the values of `name' and `long_name' it supplies | |
1476 | will be used, if non-NULL and the standard version is higher than | |
1477 | the unpredicated one, for any pedantic warning. For example, 'o' | |
1478 | for strftime formats (meaning 'O' is an extension over C99). */ | |
1479 | int predicate; | |
26f6672d JM |
1480 | /* Nonzero if the next character after this flag in the format should |
1481 | be skipped ('=' in strfmon), zero otherwise. */ | |
1482 | int skip_next_char; | |
2588e44e JM |
1483 | /* The name to use for this flag in diagnostic messages. For example, |
1484 | N_("`0' flag"), N_("field width"). */ | |
1485 | const char *name; | |
1486 | /* Long name for this flag in diagnostic messages; currently only used for | |
1487 | "ISO C does not support ...". For example, N_("the `I' printf flag"). */ | |
1488 | const char *long_name; | |
1489 | /* The standard version in which it appeared. */ | |
1490 | enum format_std_version std; | |
1491 | } format_flag_spec; | |
1492 | ||
1493 | ||
1494 | /* Structure describing a combination of flags that is bad for some kind | |
1495 | of format. */ | |
1496 | typedef struct | |
1497 | { | |
1498 | /* The first flag character in question (0 for end of array). */ | |
1499 | int flag_char1; | |
1500 | /* The second flag character. */ | |
1501 | int flag_char2; | |
1502 | /* Non-zero if the message should say that the first flag is ignored with | |
1503 | the second, zero if the combination should simply be objected to. */ | |
1504 | int ignored; | |
1505 | /* Zero if this entry applies whenever this flag combination occurs, | |
1506 | a non-zero character from flags2 if it only applies in some | |
1507 | circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */ | |
1508 | int predicate; | |
1509 | } format_flag_pair; | |
1510 | ||
1511 | ||
3c786c69 JM |
1512 | /* Structure describing a particular kind of format processed by GCC. */ |
1513 | typedef struct | |
1514 | { | |
1515 | /* The name of this kind of format, for use in diagnostics. */ | |
1516 | const char *name; | |
1517 | /* Specifications of the length modifiers accepted; possibly NULL. */ | |
1518 | const format_length_info *length_char_specs; | |
1519 | /* Details of the conversion specification characters accepted. */ | |
1520 | const format_char_info *conversion_specs; | |
2588e44e JM |
1521 | /* String listing the flag characters that are accepted. */ |
1522 | const char *flag_chars; | |
1523 | /* String listing modifier characters (strftime) accepted. May be NULL. */ | |
1524 | const char *modifier_chars; | |
1525 | /* Details of the flag characters, including pseudo-flags. */ | |
1526 | const format_flag_spec *flag_specs; | |
1527 | /* Details of bad combinations of flags. */ | |
1528 | const format_flag_pair *bad_flag_pairs; | |
1529 | /* Flags applicable to this kind of format. */ | |
1530 | int flags; | |
1531 | /* Flag character to treat a width as, or 0 if width not used. */ | |
1532 | int width_char; | |
26f6672d JM |
1533 | /* Flag character to treat a left precision (strfmon) as, |
1534 | or 0 if left precision not used. */ | |
1535 | int left_precision_char; | |
1536 | /* Flag character to treat a precision (for strfmon, right precision) as, | |
1537 | or 0 if precision not used. */ | |
2588e44e JM |
1538 | int precision_char; |
1539 | /* If a flag character has the effect of suppressing the conversion of | |
1540 | an argument ('*' in scanf), that flag character, otherwise 0. */ | |
1541 | int suppression_char; | |
1542 | /* Flag character to treat a length modifier as (ignored if length | |
1543 | modifiers not used). Need not be placed in flag_chars for conversion | |
1544 | specifiers, but is used to check for bad combinations such as length | |
1545 | modifier with assignment suppression in scanf. */ | |
1546 | int length_code_char; | |
1547 | /* Pointer to type of argument expected if '*' is used for a width, | |
1548 | or NULL if '*' not used for widths. */ | |
1549 | tree *width_type; | |
1550 | /* Pointer to type of argument expected if '*' is used for a precision, | |
1551 | or NULL if '*' not used for precisions. */ | |
1552 | tree *precision_type; | |
3c786c69 JM |
1553 | } format_kind_info; |
1554 | ||
1555 | ||
6a357625 JM |
1556 | /* Structure describing details of a type expected in format checking, |
1557 | and the type to check against it. */ | |
1558 | typedef struct format_wanted_type | |
1559 | { | |
1560 | /* The type wanted. */ | |
1561 | tree wanted_type; | |
1562 | /* The name of this type to use in diagnostics. */ | |
1563 | const char *wanted_type_name; | |
1564 | /* The level of indirection through pointers at which this type occurs. */ | |
1565 | int pointer_count; | |
1566 | /* Whether, when pointer_count is 1, to allow any character type when | |
1567 | pedantic, rather than just the character or void type specified. */ | |
1568 | int char_lenient_flag; | |
1569 | /* Whether the argument, dereferenced once, is written into and so the | |
1570 | argument must not be a pointer to a const-qualified type. */ | |
1571 | int writing_in_flag; | |
014e7f1d JM |
1572 | /* Whether the argument, dereferenced once, is read from and so |
1573 | must not be a NULL pointer. */ | |
1574 | int reading_from_flag; | |
6a357625 JM |
1575 | /* If warnings should be of the form "field precision is not type int", |
1576 | the name to use (in this case "field precision"), otherwise NULL, | |
1577 | for "%s format, %s arg" type messages. If (in an extension), this | |
1578 | is a pointer type, wanted_type_name should be set to include the | |
1579 | terminating '*' characters of the type name to give a correct | |
1580 | message. */ | |
1581 | const char *name; | |
1582 | /* The actual parameter to check against the wanted type. */ | |
1583 | tree param; | |
1584 | /* The argument number of that parameter. */ | |
1585 | int arg_num; | |
1586 | /* The next type to check for this format conversion, or NULL if none. */ | |
1587 | struct format_wanted_type *next; | |
1588 | } format_wanted_type; | |
1589 | ||
1590 | ||
3c786c69 JM |
1591 | static const format_length_info printf_length_specs[] = |
1592 | { | |
1593 | { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 }, | |
20d380b1 | 1594 | { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L }, |
3c786c69 JM |
1595 | { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 }, |
1596 | { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 }, | |
1597 | { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 }, | |
1598 | { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 }, | |
1599 | { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 }, | |
1600 | { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 }, | |
1601 | { NULL, 0, 0, NULL, 0, 0 } | |
1602 | }; | |
1603 | ||
1604 | ||
1605 | /* This differs from printf_length_specs only in that "Z" is not accepted. */ | |
1606 | static const format_length_info scanf_length_specs[] = | |
1607 | { | |
1608 | { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 }, | |
20d380b1 | 1609 | { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C9L }, |
3c786c69 JM |
1610 | { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 }, |
1611 | { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 }, | |
1612 | { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 }, | |
1613 | { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 }, | |
1614 | { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 }, | |
1615 | { NULL, 0, 0, NULL, 0, 0 } | |
1616 | }; | |
1617 | ||
1618 | ||
26f6672d JM |
1619 | /* All tables for strfmon use STD_C89 everywhere, since -pedantic warnings |
1620 | make no sense for a format type not part of any C standard version. */ | |
1621 | static const format_length_info strfmon_length_specs[] = | |
1622 | { | |
1623 | /* A GNU extension. */ | |
1624 | { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 }, | |
1625 | { NULL, 0, 0, NULL, 0, 0 } | |
1626 | }; | |
1627 | ||
2588e44e JM |
1628 | static const format_flag_spec printf_flag_specs[] = |
1629 | { | |
26f6672d JM |
1630 | { ' ', 0, 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 }, |
1631 | { '+', 0, 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 }, | |
1632 | { '#', 0, 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 }, | |
1633 | { '0', 0, 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 }, | |
1634 | { '-', 0, 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 }, | |
1635 | { '\'', 0, 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT }, | |
1636 | { 'I', 0, 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT }, | |
1637 | { 'w', 0, 0, N_("field width"), N_("field width in printf format"), STD_C89 }, | |
1638 | { 'p', 0, 0, N_("precision"), N_("precision in printf format"), STD_C89 }, | |
1639 | { 'L', 0, 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 }, | |
1640 | { 0, 0, 0, NULL, NULL, 0 } | |
2588e44e JM |
1641 | }; |
1642 | ||
1643 | ||
1644 | static const format_flag_pair printf_flag_pairs[] = | |
1645 | { | |
1646 | { ' ', '+', 1, 0 }, | |
1647 | { '0', '-', 1, 0 }, | |
1648 | { '0', 'p', 1, 'i' }, | |
1649 | { 0, 0, 0, 0 } | |
1650 | }; | |
1651 | ||
1652 | ||
1653 | static const format_flag_spec scanf_flag_specs[] = | |
1654 | { | |
26f6672d JM |
1655 | { '*', 0, 0, N_("assignment suppression"), N_("assignment suppression"), STD_C89 }, |
1656 | { 'a', 0, 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT }, | |
1657 | { 'w', 0, 0, N_("field width"), N_("field width in scanf format"), STD_C89 }, | |
1658 | { 'L', 0, 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 }, | |
1659 | { '\'', 0, 0, N_("`'' flag"), N_("the `'' scanf flag"), STD_EXT }, | |
1660 | { 'I', 0, 0, N_("`I' flag"), N_("the `I' scanf flag"), STD_EXT }, | |
1661 | { 0, 0, 0, NULL, NULL, 0 } | |
2588e44e JM |
1662 | }; |
1663 | ||
1664 | ||
1665 | static const format_flag_pair scanf_flag_pairs[] = | |
1666 | { | |
1667 | { '*', 'L', 0, 0 }, | |
1668 | { 0, 0, 0, 0 } | |
1669 | }; | |
1670 | ||
1671 | ||
1672 | static const format_flag_spec strftime_flag_specs[] = | |
1673 | { | |
26f6672d JM |
1674 | { '_', 0, 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT }, |
1675 | { '-', 0, 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT }, | |
1676 | { '0', 0, 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT }, | |
1677 | { '^', 0, 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT }, | |
1678 | { '#', 0, 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT }, | |
1679 | { 'w', 0, 0, N_("field width"), N_("field width in strftime format"), STD_EXT }, | |
1680 | { 'E', 0, 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 }, | |
1681 | { 'O', 0, 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 }, | |
1682 | { 'O', 'o', 0, NULL, N_("the `O' modifier"), STD_EXT }, | |
1683 | { 0, 0, 0, NULL, NULL, 0 } | |
2588e44e JM |
1684 | }; |
1685 | ||
1686 | ||
1687 | static const format_flag_pair strftime_flag_pairs[] = | |
1688 | { | |
1689 | { 'E', 'O', 0, 0 }, | |
4831bc84 JM |
1690 | { '_', '-', 0, 0 }, |
1691 | { '_', '0', 0, 0 }, | |
1692 | { '-', '0', 0, 0 }, | |
1693 | { '^', '#', 0, 0 }, | |
2588e44e JM |
1694 | { 0, 0, 0, 0 } |
1695 | }; | |
1696 | ||
1697 | ||
26f6672d JM |
1698 | static const format_flag_spec strfmon_flag_specs[] = |
1699 | { | |
1700 | { '=', 0, 1, N_("fill character"), N_("fill character in strfmon format"), STD_C89 }, | |
1701 | { '^', 0, 0, N_("`^' flag"), N_("the `^' strfmon flag"), STD_C89 }, | |
1702 | { '+', 0, 0, N_("`+' flag"), N_("the `+' strfmon flag"), STD_C89 }, | |
1703 | { '(', 0, 0, N_("`(' flag"), N_("the `(' strfmon flag"), STD_C89 }, | |
1704 | { '!', 0, 0, N_("`!' flag"), N_("the `!' strfmon flag"), STD_C89 }, | |
1705 | { '-', 0, 0, N_("`-' flag"), N_("the `-' strfmon flag"), STD_C89 }, | |
1706 | { 'w', 0, 0, N_("field width"), N_("field width in strfmon format"), STD_C89 }, | |
1707 | { '#', 0, 0, N_("left precision"), N_("left precision in strfmon format"), STD_C89 }, | |
1708 | { 'p', 0, 0, N_("right precision"), N_("right precision in strfmon format"), STD_C89 }, | |
1709 | { 'L', 0, 0, N_("length modifier"), N_("length modifier in strfmon format"), STD_C89 }, | |
1710 | { 0, 0, 0, NULL, NULL, 0 } | |
1711 | }; | |
1712 | ||
1713 | static const format_flag_pair strfmon_flag_pairs[] = | |
1714 | { | |
1715 | { '+', '(', 0, 0 }, | |
1716 | { 0, 0, 0, 0 } | |
1717 | }; | |
1718 | ||
1719 | ||
1ccf251f | 1720 | #define T_I &integer_type_node |
3c786c69 JM |
1721 | #define T89_I { STD_C89, NULL, T_I } |
1722 | #define T99_I { STD_C99, NULL, T_I } | |
1ccf251f | 1723 | #define T_L &long_integer_type_node |
3c786c69 | 1724 | #define T89_L { STD_C89, NULL, T_L } |
2314fb70 | 1725 | #define T_LL &long_long_integer_type_node |
20d380b1 | 1726 | #define T9L_LL { STD_C9L, NULL, T_LL } |
3c786c69 | 1727 | #define TEX_LL { STD_EXT, NULL, T_LL } |
1ccf251f | 1728 | #define T_S &short_integer_type_node |
3c786c69 | 1729 | #define T89_S { STD_C89, NULL, T_S } |
1ccf251f | 1730 | #define T_UI &unsigned_type_node |
3c786c69 JM |
1731 | #define T89_UI { STD_C89, NULL, T_UI } |
1732 | #define T99_UI { STD_C99, NULL, T_UI } | |
1ccf251f | 1733 | #define T_UL &long_unsigned_type_node |
3c786c69 | 1734 | #define T89_UL { STD_C89, NULL, T_UL } |
2314fb70 | 1735 | #define T_ULL &long_long_unsigned_type_node |
20d380b1 | 1736 | #define T9L_ULL { STD_C9L, NULL, T_ULL } |
3c786c69 | 1737 | #define TEX_ULL { STD_EXT, NULL, T_ULL } |
1ccf251f | 1738 | #define T_US &short_unsigned_type_node |
3c786c69 | 1739 | #define T89_US { STD_C89, NULL, T_US } |
1ccf251f | 1740 | #define T_F &float_type_node |
3c786c69 JM |
1741 | #define T89_F { STD_C89, NULL, T_F } |
1742 | #define T99_F { STD_C99, NULL, T_F } | |
1ccf251f | 1743 | #define T_D &double_type_node |
3c786c69 JM |
1744 | #define T89_D { STD_C89, NULL, T_D } |
1745 | #define T99_D { STD_C99, NULL, T_D } | |
1ccf251f | 1746 | #define T_LD &long_double_type_node |
3c786c69 JM |
1747 | #define T89_LD { STD_C89, NULL, T_LD } |
1748 | #define T99_LD { STD_C99, NULL, T_LD } | |
1ccf251f | 1749 | #define T_C &char_type_node |
3c786c69 | 1750 | #define T89_C { STD_C89, NULL, T_C } |
cd732418 | 1751 | #define T_SC &signed_char_type_node |
3c786c69 | 1752 | #define T99_SC { STD_C99, NULL, T_SC } |
b57062ca | 1753 | #define T_UC &unsigned_char_type_node |
3c786c69 | 1754 | #define T99_UC { STD_C99, NULL, T_UC } |
1ccf251f | 1755 | #define T_V &void_type_node |
3c786c69 | 1756 | #define T89_V { STD_C89, NULL, T_V } |
1ccf251f | 1757 | #define T_W &wchar_type_node |
3c786c69 JM |
1758 | #define T94_W { STD_C94, "wchar_t", T_W } |
1759 | #define TEX_W { STD_EXT, "wchar_t", T_W } | |
c5ab7f91 | 1760 | #define T_WI &wint_type_node |
3c786c69 JM |
1761 | #define T94_WI { STD_C94, "wint_t", T_WI } |
1762 | #define TEX_WI { STD_EXT, "wint_t", T_WI } | |
1763 | #define T_ST &c_size_type_node | |
1764 | #define T99_ST { STD_C99, "size_t", T_ST } | |
cd732418 | 1765 | #define T_SST &signed_size_type_node |
3c786c69 | 1766 | #define T99_SST { STD_C99, "signed size_t", T_SST } |
b8458e3e | 1767 | #define T_PD &ptrdiff_type_node |
3c786c69 | 1768 | #define T99_PD { STD_C99, "ptrdiff_t", T_PD } |
cd732418 | 1769 | #define T_UPD &unsigned_ptrdiff_type_node |
3c786c69 | 1770 | #define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD } |
b15ad712 | 1771 | #define T_IM &intmax_type_node |
3c786c69 | 1772 | #define T99_IM { STD_C99, "intmax_t", T_IM } |
b15ad712 | 1773 | #define T_UIM &uintmax_type_node |
3c786c69 | 1774 | #define T99_UIM { STD_C99, "uintmax_t", T_UIM } |
1ccf251f | 1775 | |
3c786c69 JM |
1776 | static const format_char_info print_char_table[] = |
1777 | { | |
1778 | /* C89 conversion specifiers. */ | |
20d380b1 JM |
1779 | { "di", 0, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i" }, |
1780 | { "oxX", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i" }, | |
1781 | { "u", 0, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i" }, | |
014e7f1d JM |
1782 | { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" }, |
1783 | { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" }, | |
1784 | { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" }, | |
1785 | { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "cR" }, | |
1786 | { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c" }, | |
20d380b1 | 1787 | { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" }, |
3c786c69 | 1788 | /* C99 conversion specifiers. */ |
014e7f1d JM |
1789 | { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" }, |
1790 | { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" }, | |
3c786c69 | 1791 | /* X/Open conversion specifiers. */ |
014e7f1d JM |
1792 | { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" }, |
1793 | { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "R" }, | |
3c786c69 | 1794 | /* GNU conversion specifiers. */ |
014e7f1d | 1795 | { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" }, |
0e00b68b | 1796 | { NULL, 0, 0, NOLENGTHS, NULL, NULL } |
3c786c69 | 1797 | }; |
1ccf251f | 1798 | |
3c786c69 JM |
1799 | static const format_char_info scan_char_table[] = |
1800 | { | |
1801 | /* C89 conversion specifiers. */ | |
20d380b1 JM |
1802 | { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w'I", "W" }, |
1803 | { "u", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w'I", "W" }, | |
1804 | { "oxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T9L_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W" }, | |
4831bc84 JM |
1805 | { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" }, |
1806 | { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW" }, | |
1807 | { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW" }, | |
1808 | { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[" }, | |
1809 | { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" }, | |
20d380b1 | 1810 | { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T9L_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" }, |
3c786c69 | 1811 | /* C99 conversion specifiers. */ |
4831bc84 | 1812 | { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w'", "W" }, |
3c786c69 | 1813 | /* X/Open conversion specifiers. */ |
4831bc84 JM |
1814 | { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" }, |
1815 | { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W" }, | |
0e00b68b | 1816 | { NULL, 0, 0, NOLENGTHS, NULL, NULL } |
1ccf251f RK |
1817 | }; |
1818 | ||
26f6672d | 1819 | static const format_char_info time_char_table[] = |
3c786c69 JM |
1820 | { |
1821 | /* C89 conversion specifiers. */ | |
0e00b68b JM |
1822 | { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "" }, |
1823 | { "cx", 0, STD_C89, NOLENGTHS, "E", "3" }, | |
1824 | { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "" }, | |
1825 | { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o" }, | |
1826 | { "p", 0, STD_C89, NOLENGTHS, "#", "" }, | |
1827 | { "X", 0, STD_C89, NOLENGTHS, "E", "" }, | |
2588e44e | 1828 | { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4" }, |
0e00b68b JM |
1829 | { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o" }, |
1830 | { "%", 0, STD_C89, NOLENGTHS, "", "" }, | |
3c786c69 | 1831 | /* C99 conversion specifiers. */ |
0e00b68b JM |
1832 | { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o" }, |
1833 | { "D", 0, STD_C99, NOLENGTHS, "", "2" }, | |
1834 | { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "" }, | |
1835 | { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "" }, | |
1836 | { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o" }, | |
1837 | { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o" }, | |
1838 | { "h", 0, STD_C99, NOLENGTHS, "^#", "" }, | |
1839 | { "z", 0, STD_C99, NOLENGTHS, "O", "o" }, | |
3c786c69 | 1840 | /* GNU conversion specifiers. */ |
0e00b68b JM |
1841 | { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "" }, |
1842 | { "P", 0, STD_EXT, NOLENGTHS, "", "" }, | |
1843 | { NULL, 0, 0, NOLENGTHS, NULL, NULL } | |
1ccf251f RK |
1844 | }; |
1845 | ||
26f6672d JM |
1846 | static const format_char_info monetary_char_table[] = |
1847 | { | |
1848 | { "in", 0, STD_C89, { T89_D, BADLEN, BADLEN, BADLEN, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "=^+(!-w#p", "" }, | |
1849 | { NULL, 0, 0, NOLENGTHS, NULL, NULL } | |
1850 | }; | |
1851 | ||
3c786c69 JM |
1852 | |
1853 | /* This must be in the same order as enum format_type. */ | |
1854 | static const format_kind_info format_types[] = | |
1855 | { | |
26f6672d | 1856 | { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL, |
2588e44e | 1857 | printf_flag_specs, printf_flag_pairs, |
26f6672d JM |
1858 | FMT_FLAG_ARG_CONVERT|FMT_FLAG_DOLLAR_MULTIPLE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_EMPTY_PREC_OK, |
1859 | 'w', 0, 'p', 0, 'L', | |
2588e44e JM |
1860 | &integer_type_node, &integer_type_node |
1861 | }, | |
26f6672d | 1862 | { "scanf", scanf_length_specs, scan_char_table, "*'I", NULL, |
2588e44e | 1863 | scanf_flag_specs, scanf_flag_pairs, |
26f6672d JM |
1864 | FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE|FMT_FLAG_USE_DOLLAR|FMT_FLAG_ZERO_WIDTH_BAD, |
1865 | 'w', 0, 0, '*', 'L', | |
2588e44e JM |
1866 | NULL, NULL |
1867 | }, | |
26f6672d | 1868 | { "strftime", NULL, time_char_table, "_-0^#", "EO", |
2588e44e | 1869 | strftime_flag_specs, strftime_flag_pairs, |
26f6672d JM |
1870 | FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0, 0, |
1871 | NULL, NULL | |
1872 | }, | |
1873 | { "strfmon", strfmon_length_specs, monetary_char_table, "=^+(!-", NULL, | |
1874 | strfmon_flag_specs, strfmon_flag_pairs, | |
1875 | FMT_FLAG_ARG_CONVERT, 'w', '#', 'p', 0, 'L', | |
2588e44e JM |
1876 | NULL, NULL |
1877 | } | |
bb72a084 PE |
1878 | }; |
1879 | ||
3c786c69 | 1880 | |
0161e8da RK |
1881 | typedef struct function_format_info |
1882 | { | |
1ccf251f RK |
1883 | struct function_format_info *next; /* next structure on the list */ |
1884 | tree name; /* identifier such as "printf" */ | |
1885 | tree assembler_name; /* optional mangled identifier (for C++) */ | |
bb72a084 | 1886 | enum format_type format_type; /* type of format (printf, scanf, etc.) */ |
1ccf251f RK |
1887 | int format_num; /* number of format argument */ |
1888 | int first_arg_num; /* number of first arg (zero for varargs) */ | |
1889 | } function_format_info; | |
1890 | ||
1891 | static function_format_info *function_format_list = NULL; | |
1892 | ||
0161e8da RK |
1893 | typedef struct international_format_info |
1894 | { | |
1895 | struct international_format_info *next; /* next structure on the list */ | |
1896 | tree name; /* identifier such as "gettext" */ | |
1897 | tree assembler_name; /* optional mangled identifier (for C++) */ | |
1898 | int format_num; /* number of format argument */ | |
1899 | } international_format_info; | |
1900 | ||
1901 | static international_format_info *international_format_list = NULL; | |
1902 | ||
6d2e1454 JM |
1903 | /* Structure detailing the results of checking a format function call |
1904 | where the format expression may be a conditional expression with | |
1905 | many leaves resulting from nested conditional expressions. */ | |
1906 | typedef struct | |
1907 | { | |
1908 | /* Number of leaves of the format argument that could not be checked | |
1909 | as they were not string literals. */ | |
1910 | int number_non_literal; | |
1911 | /* Number of leaves of the format argument that were null pointers or | |
1912 | string literals, but had extra format arguments. */ | |
1913 | int number_extra_args; | |
1914 | /* Number of leaves of the format argument that were null pointers or | |
1915 | string literals, but had extra format arguments and used $ operand | |
1916 | numbers. */ | |
1917 | int number_dollar_extra_args; | |
1918 | /* Number of leaves of the format argument that were wide string | |
1919 | literals. */ | |
1920 | int number_wide; | |
1921 | /* Number of leaves of the format argument that were empty strings. */ | |
1922 | int number_empty; | |
1923 | /* Number of leaves of the format argument that were unterminated | |
1924 | strings. */ | |
1925 | int number_unterminated; | |
1926 | /* Number of leaves of the format argument that were not counted above. */ | |
1927 | int number_other; | |
1928 | } format_check_results; | |
1929 | ||
3f80e6ac | 1930 | static void check_format_info PARAMS ((int *, function_format_info *, tree)); |
6d2e1454 JM |
1931 | static void check_format_info_recurse PARAMS ((int *, format_check_results *, |
1932 | function_format_info *, tree, | |
1933 | tree, int)); | |
1934 | static void check_format_info_main PARAMS ((int *, format_check_results *, | |
1935 | function_format_info *, | |
1936 | const char *, int, tree, int)); | |
3f80e6ac KG |
1937 | static void status_warning PARAMS ((int *, const char *, ...)) |
1938 | ATTRIBUTE_PRINTF_2; | |
1ccf251f | 1939 | |
5804a754 | 1940 | static void init_dollar_format_checking PARAMS ((int, tree)); |
3f80e6ac | 1941 | static int maybe_read_dollar_number PARAMS ((int *, const char **, int, |
b691cb00 JM |
1942 | tree, tree *, |
1943 | const format_kind_info *)); | |
6d2e1454 | 1944 | static void finish_dollar_format_checking PARAMS ((int *, format_check_results *)); |
5804a754 | 1945 | |
2588e44e JM |
1946 | static const format_flag_spec *get_flag_spec PARAMS ((const format_flag_spec *, |
1947 | int, const char *)); | |
1948 | ||
3f80e6ac | 1949 | static void check_format_types PARAMS ((int *, format_wanted_type *)); |
c70eaeaf | 1950 | static int is_valid_printf_arglist PARAMS ((tree)); |
f58e0b0c | 1951 | static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier)); |
c70eaeaf KG |
1952 | static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode, |
1953 | enum expand_modifier, int)); | |
6a357625 | 1954 | |
1ccf251f | 1955 | /* Initialize the table of functions to perform format checking on. |
93e2382f | 1956 | The ISO C functions are always checked (whether <stdio.h> is |
1ccf251f RK |
1957 | included or not), since it is common to call printf without |
1958 | including <stdio.h>. There shouldn't be a problem with this, | |
93e2382f JM |
1959 | since ISO C reserves these function names whether you include the |
1960 | header file or not. In any case, the checking is harmless. With | |
1961 | -ffreestanding, these default attributes are disabled, and must be | |
1962 | specified manually if desired. | |
0161e8da RK |
1963 | |
1964 | Also initialize the name of function that modify the format string for | |
1965 | internationalization purposes. */ | |
1ccf251f RK |
1966 | |
1967 | void | |
1968 | init_function_format_info () | |
1969 | { | |
93e2382f JM |
1970 | if (flag_hosted) |
1971 | { | |
1972 | /* Functions from ISO/IEC 9899:1990. */ | |
1973 | record_function_format (get_identifier ("printf"), NULL_TREE, | |
1974 | printf_format_type, 1, 2); | |
ad3fd36f KG |
1975 | record_function_format (get_identifier ("__builtin_printf"), NULL_TREE, |
1976 | printf_format_type, 1, 2); | |
93e2382f JM |
1977 | record_function_format (get_identifier ("fprintf"), NULL_TREE, |
1978 | printf_format_type, 2, 3); | |
1979 | record_function_format (get_identifier ("sprintf"), NULL_TREE, | |
1980 | printf_format_type, 2, 3); | |
1981 | record_function_format (get_identifier ("scanf"), NULL_TREE, | |
1982 | scanf_format_type, 1, 2); | |
1983 | record_function_format (get_identifier ("fscanf"), NULL_TREE, | |
1984 | scanf_format_type, 2, 3); | |
1985 | record_function_format (get_identifier ("sscanf"), NULL_TREE, | |
1986 | scanf_format_type, 2, 3); | |
1987 | record_function_format (get_identifier ("vprintf"), NULL_TREE, | |
1988 | printf_format_type, 1, 0); | |
1989 | record_function_format (get_identifier ("vfprintf"), NULL_TREE, | |
1990 | printf_format_type, 2, 0); | |
1991 | record_function_format (get_identifier ("vsprintf"), NULL_TREE, | |
1992 | printf_format_type, 2, 0); | |
1993 | record_function_format (get_identifier ("strftime"), NULL_TREE, | |
1994 | strftime_format_type, 3, 0); | |
1995 | } | |
1996 | ||
1997 | if (flag_hosted && flag_isoc99) | |
980d86e9 JM |
1998 | { |
1999 | /* ISO C99 adds the snprintf and vscanf family functions. */ | |
2000 | record_function_format (get_identifier ("snprintf"), NULL_TREE, | |
2001 | printf_format_type, 3, 4); | |
2002 | record_function_format (get_identifier ("vsnprintf"), NULL_TREE, | |
2003 | printf_format_type, 3, 0); | |
2004 | record_function_format (get_identifier ("vscanf"), NULL_TREE, | |
2005 | scanf_format_type, 1, 0); | |
2006 | record_function_format (get_identifier ("vfscanf"), NULL_TREE, | |
2007 | scanf_format_type, 2, 0); | |
2008 | record_function_format (get_identifier ("vsscanf"), NULL_TREE, | |
2009 | scanf_format_type, 2, 0); | |
2010 | } | |
2011 | ||
93e2382f JM |
2012 | if (flag_hosted && flag_noniso_default_format_attributes) |
2013 | { | |
2014 | /* Uniforum/GNU gettext functions, not in ISO C. */ | |
2015 | record_international_format (get_identifier ("gettext"), NULL_TREE, 1); | |
2016 | record_international_format (get_identifier ("dgettext"), NULL_TREE, 2); | |
2017 | record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2); | |
26f6672d JM |
2018 | /* X/Open strfmon function. */ |
2019 | record_function_format (get_identifier ("strfmon"), NULL_TREE, | |
2020 | strfmon_format_type, 3, 4); | |
93e2382f | 2021 | } |
1ccf251f RK |
2022 | } |
2023 | ||
2024 | /* Record information for argument format checking. FUNCTION_IDENT is | |
2025 | the identifier node for the name of the function to check (its decl | |
bb72a084 PE |
2026 | need not exist yet). |
2027 | FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number | |
1ccf251f RK |
2028 | of the argument which is the format control string (starting from 1). |
2029 | FIRST_ARG_NUM is the number of the first actual argument to check | |
0161e8da | 2030 | against the format string, or zero if no checking is not be done |
1ccf251f RK |
2031 | (e.g. for varargs such as vfprintf). */ |
2032 | ||
bb72a084 PE |
2033 | static void |
2034 | record_function_format (name, assembler_name, format_type, | |
1ccf251f RK |
2035 | format_num, first_arg_num) |
2036 | tree name; | |
2037 | tree assembler_name; | |
bb72a084 | 2038 | enum format_type format_type; |
1ccf251f RK |
2039 | int format_num; |
2040 | int first_arg_num; | |
2041 | { | |
2042 | function_format_info *info; | |
2043 | ||
2044 | /* Re-use existing structure if it's there. */ | |
2045 | ||
2046 | for (info = function_format_list; info; info = info->next) | |
2047 | { | |
2048 | if (info->name == name && info->assembler_name == assembler_name) | |
2049 | break; | |
2050 | } | |
2051 | if (! info) | |
2052 | { | |
2053 | info = (function_format_info *) xmalloc (sizeof (function_format_info)); | |
2054 | info->next = function_format_list; | |
2055 | function_format_list = info; | |
2056 | ||
2057 | info->name = name; | |
2058 | info->assembler_name = assembler_name; | |
2059 | } | |
2060 | ||
bb72a084 | 2061 | info->format_type = format_type; |
1ccf251f RK |
2062 | info->format_num = format_num; |
2063 | info->first_arg_num = first_arg_num; | |
2064 | } | |
2065 | ||
0161e8da RK |
2066 | /* Record information for the names of function that modify the format |
2067 | argument to format functions. FUNCTION_IDENT is the identifier node for | |
2068 | the name of the function (its decl need not exist yet) and FORMAT_NUM is | |
2069 | the number of the argument which is the format control string (starting | |
2070 | from 1). */ | |
2071 | ||
31837ce2 | 2072 | static void |
0161e8da RK |
2073 | record_international_format (name, assembler_name, format_num) |
2074 | tree name; | |
2075 | tree assembler_name; | |
2076 | int format_num; | |
2077 | { | |
2078 | international_format_info *info; | |
2079 | ||
2080 | /* Re-use existing structure if it's there. */ | |
2081 | ||
2082 | for (info = international_format_list; info; info = info->next) | |
2083 | { | |
2084 | if (info->name == name && info->assembler_name == assembler_name) | |
2085 | break; | |
2086 | } | |
2087 | ||
2088 | if (! info) | |
2089 | { | |
2090 | info | |
2091 | = (international_format_info *) | |
2092 | xmalloc (sizeof (international_format_info)); | |
2093 | info->next = international_format_list; | |
2094 | international_format_list = info; | |
2095 | ||
2096 | info->name = name; | |
2097 | info->assembler_name = assembler_name; | |
2098 | } | |
2099 | ||
2100 | info->format_num = format_num; | |
2101 | } | |
1ccf251f RK |
2102 | \f |
2103 | /* Check the argument list of a call to printf, scanf, etc. | |
2104 | NAME is the function identifier. | |
2105 | ASSEMBLER_NAME is the function's assembler identifier. | |
2106 | (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.) | |
74ff4629 JM |
2107 | PARAMS is the list of argument values. Also, if -Wmissing-format-attribute, |
2108 | warn for calls to vprintf or vscanf in functions with no such format | |
2109 | attribute themselves. */ | |
1ccf251f RK |
2110 | |
2111 | void | |
3f80e6ac KG |
2112 | check_function_format (status, name, assembler_name, params) |
2113 | int *status; | |
1ccf251f RK |
2114 | tree name; |
2115 | tree assembler_name; | |
2116 | tree params; | |
2117 | { | |
2118 | function_format_info *info; | |
2119 | ||
2120 | /* See if this function is a format function. */ | |
2121 | for (info = function_format_list; info; info = info->next) | |
2122 | { | |
95216dec | 2123 | if (info->assembler_name |
1ccf251f RK |
2124 | ? (info->assembler_name == assembler_name) |
2125 | : (info->name == name)) | |
2126 | { | |
2127 | /* Yup; check it. */ | |
3f80e6ac | 2128 | check_format_info (status, info, params); |
74ff4629 JM |
2129 | if (warn_missing_format_attribute && info->first_arg_num == 0 |
2130 | && (format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT)) | |
2131 | { | |
2132 | function_format_info *info2; | |
2133 | for (info2 = function_format_list; info2; info2 = info2->next) | |
2134 | if ((info2->assembler_name | |
2135 | ? (info2->assembler_name == DECL_ASSEMBLER_NAME (current_function_decl)) | |
2136 | : (info2->name == DECL_NAME (current_function_decl))) | |
2137 | && info2->format_type == info->format_type) | |
2138 | break; | |
2139 | if (info2 == NULL) | |
e45046ae JM |
2140 | { |
2141 | /* Check if the current function has a parameter to which | |
2142 | the format attribute could be attached; if not, it | |
2143 | can't be a candidate for a format attribute, despite | |
2144 | the vprintf-like or vscanf-like call. */ | |
2145 | tree args; | |
2146 | for (args = DECL_ARGUMENTS (current_function_decl); | |
2147 | args != 0; | |
2148 | args = TREE_CHAIN (args)) | |
2149 | { | |
2150 | if (TREE_CODE (TREE_TYPE (args)) == POINTER_TYPE | |
2151 | && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (args))) | |
2152 | == char_type_node)) | |
2153 | break; | |
2154 | } | |
2155 | if (args != 0) | |
2156 | warning ("function might be possible candidate for `%s' format attribute", | |
2157 | format_types[info->format_type].name); | |
2158 | } | |
74ff4629 | 2159 | } |
1ccf251f RK |
2160 | break; |
2161 | } | |
2162 | } | |
2163 | } | |
2164 | ||
3f80e6ac KG |
2165 | /* This function replaces `warning' inside the printf format checking |
2166 | functions. If the `status' parameter is non-NULL, then it is | |
2167 | dereferenced and set to 1 whenever a warning is caught. Otherwise | |
2168 | it warns as usual by replicating the innards of the warning | |
2169 | function from diagnostic.c. */ | |
2170 | static void | |
2171 | status_warning VPARAMS ((int *status, const char *msgid, ...)) | |
2172 | { | |
2173 | #ifndef ANSI_PROTOTYPES | |
2174 | int *status; | |
2175 | const char *msgid; | |
2176 | #endif | |
2177 | va_list ap; | |
2178 | diagnostic_context dc; | |
2179 | ||
29ced803 | 2180 | VA_START (ap, msgid); |
3f80e6ac KG |
2181 | |
2182 | #ifndef ANSI_PROTOTYPES | |
29ced803 AO |
2183 | status = va_arg (ap, int *); |
2184 | msgid = va_arg (ap, const char *); | |
3f80e6ac KG |
2185 | #endif |
2186 | ||
29ced803 AO |
2187 | if (status) |
2188 | *status = 1; | |
2189 | else | |
2190 | { | |
3f80e6ac KG |
2191 | /* This duplicates the warning function behavior. */ |
2192 | set_diagnostic_context | |
2193 | (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1); | |
2194 | report_diagnostic (&dc); | |
3f80e6ac | 2195 | } |
29ced803 AO |
2196 | |
2197 | va_end (ap); | |
3f80e6ac | 2198 | } |
5804a754 JM |
2199 | |
2200 | /* Variables used by the checking of $ operand number formats. */ | |
2201 | static char *dollar_arguments_used = NULL; | |
2202 | static int dollar_arguments_alloc = 0; | |
2203 | static int dollar_arguments_count; | |
2204 | static int dollar_first_arg_num; | |
2205 | static int dollar_max_arg_used; | |
2206 | static int dollar_format_warned; | |
2207 | ||
2208 | /* Initialize the checking for a format string that may contain $ | |
2209 | parameter number specifications; we will need to keep track of whether | |
2210 | each parameter has been used. FIRST_ARG_NUM is the number of the first | |
2211 | argument that is a parameter to the format, or 0 for a vprintf-style | |
2212 | function; PARAMS is the list of arguments starting at this argument. */ | |
2213 | ||
2214 | static void | |
2215 | init_dollar_format_checking (first_arg_num, params) | |
2216 | int first_arg_num; | |
2217 | tree params; | |
2218 | { | |
2219 | dollar_first_arg_num = first_arg_num; | |
2220 | dollar_arguments_count = 0; | |
2221 | dollar_max_arg_used = 0; | |
2222 | dollar_format_warned = 0; | |
2223 | if (first_arg_num > 0) | |
2224 | { | |
2225 | while (params) | |
2226 | { | |
2227 | dollar_arguments_count++; | |
2228 | params = TREE_CHAIN (params); | |
2229 | } | |
2230 | } | |
2231 | if (dollar_arguments_alloc < dollar_arguments_count) | |
2232 | { | |
2233 | if (dollar_arguments_used) | |
2234 | free (dollar_arguments_used); | |
2235 | dollar_arguments_alloc = dollar_arguments_count; | |
2236 | dollar_arguments_used = xmalloc (dollar_arguments_alloc); | |
2237 | } | |
2238 | if (dollar_arguments_alloc) | |
2239 | memset (dollar_arguments_used, 0, dollar_arguments_alloc); | |
2240 | } | |
2241 | ||
2242 | ||
2243 | /* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED | |
2244 | is set, it is an error if one is not found; otherwise, it is OK. If | |
2245 | such a number is found, check whether it is within range and mark that | |
2246 | numbered operand as being used for later checking. Returns the operand | |
2247 | number if found and within range, zero if no such number was found and | |
2248 | this is OK, or -1 on error. PARAMS points to the first operand of the | |
2249 | format; PARAM_PTR is made to point to the parameter referred to. If | |
2250 | a $ format is found, *FORMAT is updated to point just after it. */ | |
2251 | ||
2252 | static int | |
b691cb00 JM |
2253 | maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr, |
2254 | fki) | |
3f80e6ac | 2255 | int *status; |
5804a754 JM |
2256 | const char **format; |
2257 | int dollar_needed; | |
2258 | tree params; | |
2259 | tree *param_ptr; | |
b691cb00 | 2260 | const format_kind_info *fki; |
5804a754 JM |
2261 | { |
2262 | int argnum; | |
2263 | int overflow_flag; | |
2264 | const char *fcp = *format; | |
2265 | if (*fcp < '0' || *fcp > '9') | |
2266 | { | |
2267 | if (dollar_needed) | |
2268 | { | |
3f80e6ac | 2269 | status_warning (status, "missing $ operand number in format"); |
5804a754 JM |
2270 | return -1; |
2271 | } | |
2272 | else | |
2273 | return 0; | |
2274 | } | |
2275 | argnum = 0; | |
2276 | overflow_flag = 0; | |
2277 | while (*fcp >= '0' && *fcp <= '9') | |
2278 | { | |
2279 | int nargnum; | |
2280 | nargnum = 10 * argnum + (*fcp - '0'); | |
2281 | if (nargnum < 0 || nargnum / 10 != argnum) | |
2282 | overflow_flag = 1; | |
2283 | argnum = nargnum; | |
2284 | fcp++; | |
2285 | } | |
2286 | if (*fcp != '$') | |
2287 | { | |
2288 | if (dollar_needed) | |
2289 | { | |
3f80e6ac | 2290 | status_warning (status, "missing $ operand number in format"); |
5804a754 JM |
2291 | return -1; |
2292 | } | |
2293 | else | |
2294 | return 0; | |
2295 | } | |
2296 | *format = fcp + 1; | |
2297 | if (pedantic && !dollar_format_warned) | |
2298 | { | |
ade7d3cc JM |
2299 | status_warning (status, |
2300 | "%s does not support %%n$ operand number formats", | |
2301 | C_STD_NAME (STD_EXT)); | |
5804a754 JM |
2302 | dollar_format_warned = 1; |
2303 | } | |
2304 | if (overflow_flag || argnum == 0 | |
2305 | || (dollar_first_arg_num && argnum > dollar_arguments_count)) | |
2306 | { | |
3f80e6ac | 2307 | status_warning (status, "operand number out of range in format"); |
5804a754 JM |
2308 | return -1; |
2309 | } | |
2310 | if (argnum > dollar_max_arg_used) | |
2311 | dollar_max_arg_used = argnum; | |
2312 | /* For vprintf-style functions we may need to allocate more memory to | |
2313 | track which arguments are used. */ | |
2314 | while (dollar_arguments_alloc < dollar_max_arg_used) | |
2315 | { | |
2316 | int nalloc; | |
2317 | nalloc = 2 * dollar_arguments_alloc + 16; | |
2318 | dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc); | |
2319 | memset (dollar_arguments_used + dollar_arguments_alloc, 0, | |
2320 | nalloc - dollar_arguments_alloc); | |
2321 | dollar_arguments_alloc = nalloc; | |
2322 | } | |
b691cb00 JM |
2323 | if (!(fki->flags & FMT_FLAG_DOLLAR_MULTIPLE) |
2324 | && dollar_arguments_used[argnum - 1] == 1) | |
2325 | { | |
2326 | dollar_arguments_used[argnum - 1] = 2; | |
2327 | status_warning (status, | |
2328 | "format argument %d used more than once in %s format", | |
2329 | argnum, fki->name); | |
2330 | } | |
2331 | else | |
2332 | dollar_arguments_used[argnum - 1] = 1; | |
5804a754 JM |
2333 | if (dollar_first_arg_num) |
2334 | { | |
2335 | int i; | |
2336 | *param_ptr = params; | |
2337 | for (i = 1; i < argnum && *param_ptr != 0; i++) | |
2338 | *param_ptr = TREE_CHAIN (*param_ptr); | |
2339 | ||
2340 | if (*param_ptr == 0) | |
2341 | { | |
2342 | /* This case shouldn't be caught here. */ | |
2343 | abort (); | |
2344 | } | |
2345 | } | |
2346 | else | |
2347 | *param_ptr = 0; | |
2348 | return argnum; | |
2349 | } | |
2350 | ||
2351 | ||
2352 | /* Finish the checking for a format string that used $ operand number formats | |
2353 | instead of non-$ formats. We check for unused operands before used ones | |
2354 | (a serious error, since the implementation of the format function | |
2355 | can't know what types to pass to va_arg to find the later arguments). | |
2356 | and for unused operands at the end of the format (if we know how many | |
2357 | arguments the format had, so not for vprintf). If there were operand | |
2358 | numbers out of range on a non-vprintf-style format, we won't have reached | |
2359 | here. */ | |
2360 | ||
2361 | static void | |
6d2e1454 | 2362 | finish_dollar_format_checking (status, res) |
3f80e6ac | 2363 | int *status; |
6d2e1454 | 2364 | format_check_results *res; |
5804a754 JM |
2365 | { |
2366 | int i; | |
2367 | for (i = 0; i < dollar_max_arg_used; i++) | |
2368 | { | |
2369 | if (!dollar_arguments_used[i]) | |
3f80e6ac | 2370 | status_warning (status, "format argument %d unused before used argument %d in $-style format", |
5804a754 JM |
2371 | i + 1, dollar_max_arg_used); |
2372 | } | |
2373 | if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count) | |
6d2e1454 JM |
2374 | { |
2375 | res->number_other--; | |
2376 | res->number_dollar_extra_args++; | |
2377 | } | |
5804a754 JM |
2378 | } |
2379 | ||
2380 | ||
2588e44e JM |
2381 | /* Retrieve the specification for a format flag. SPEC contains the |
2382 | specifications for format flags for the applicable kind of format. | |
2383 | FLAG is the flag in question. If PREDICATES is NULL, the basic | |
2384 | spec for that flag must be retrieved and this function aborts if | |
2385 | it cannot be found. If PREDICATES is not NULL, it is a string listing | |
2386 | possible predicates for the spec entry; if an entry predicated on any | |
2387 | of these is found, it is returned, otherwise NULL is returned. */ | |
2388 | ||
2389 | static const format_flag_spec * | |
2390 | get_flag_spec (spec, flag, predicates) | |
2391 | const format_flag_spec *spec; | |
2392 | int flag; | |
2393 | const char *predicates; | |
2394 | { | |
2395 | int i; | |
2396 | for (i = 0; spec[i].flag_char != 0; i++) | |
2397 | { | |
2398 | if (spec[i].flag_char != flag) | |
2399 | continue; | |
2400 | if (predicates != NULL) | |
2401 | { | |
2402 | if (spec[i].predicate != 0 | |
0304bbad | 2403 | && strchr (predicates, spec[i].predicate) != 0) |
2588e44e JM |
2404 | return &spec[i]; |
2405 | } | |
2406 | else if (spec[i].predicate == 0) | |
2407 | return &spec[i]; | |
2408 | } | |
2409 | if (predicates == NULL) | |
2410 | abort (); | |
2411 | else | |
2412 | return NULL; | |
2413 | } | |
2414 | ||
2415 | ||
1ccf251f RK |
2416 | /* Check the argument list of a call to printf, scanf, etc. |
2417 | INFO points to the function_format_info structure. | |
2418 | PARAMS is the list of argument values. */ | |
2419 | ||
2420 | static void | |
3f80e6ac KG |
2421 | check_format_info (status, info, params) |
2422 | int *status; | |
1ccf251f RK |
2423 | function_format_info *info; |
2424 | tree params; | |
2425 | { | |
1ccf251f | 2426 | int arg_num; |
1ccf251f | 2427 | tree format_tree; |
6d2e1454 | 2428 | format_check_results res; |
1ccf251f RK |
2429 | /* Skip to format argument. If the argument isn't available, there's |
2430 | no work for us to do; prototype checking will catch the problem. */ | |
2431 | for (arg_num = 1; ; ++arg_num) | |
2432 | { | |
2433 | if (params == 0) | |
2434 | return; | |
2435 | if (arg_num == info->format_num) | |
2436 | break; | |
2437 | params = TREE_CHAIN (params); | |
2438 | } | |
2439 | format_tree = TREE_VALUE (params); | |
2440 | params = TREE_CHAIN (params); | |
2441 | if (format_tree == 0) | |
2442 | return; | |
0161e8da | 2443 | |
6d2e1454 JM |
2444 | res.number_non_literal = 0; |
2445 | res.number_extra_args = 0; | |
2446 | res.number_dollar_extra_args = 0; | |
2447 | res.number_wide = 0; | |
2448 | res.number_empty = 0; | |
2449 | res.number_unterminated = 0; | |
2450 | res.number_other = 0; | |
2451 | ||
2452 | check_format_info_recurse (status, &res, info, format_tree, params, arg_num); | |
2453 | ||
2454 | if (res.number_non_literal > 0) | |
2455 | { | |
2456 | /* Functions taking a va_list normally pass a non-literal format | |
2457 | string. These functions typically are declared with | |
2458 | first_arg_num == 0, so avoid warning in those cases. */ | |
1c248308 JM |
2459 | if (!(format_types[info->format_type].flags & FMT_FLAG_ARG_CONVERT)) |
2460 | { | |
2461 | /* For strftime-like formats, warn for not checking the format | |
2462 | string; but there are no arguments to check. */ | |
2463 | if (warn_format_nonliteral) | |
2464 | status_warning (status, "format not a string literal, format string not checked"); | |
2465 | } | |
2466 | else if (info->first_arg_num != 0) | |
2467 | { | |
2468 | /* If there are no arguments for the format at all, we may have | |
2469 | printf (foo) which is likely to be a security hole. */ | |
2470 | while (arg_num + 1 < info->first_arg_num) | |
2471 | { | |
2472 | if (params == 0) | |
2473 | break; | |
2474 | params = TREE_CHAIN (params); | |
2475 | ++arg_num; | |
2476 | } | |
c907e684 | 2477 | if (params == 0 && (warn_format_nonliteral || warn_format_security)) |
1c248308 JM |
2478 | status_warning (status, "format not a string literal and no format arguments"); |
2479 | else if (warn_format_nonliteral) | |
2480 | status_warning (status, "format not a string literal, argument types not checked"); | |
2481 | } | |
6d2e1454 JM |
2482 | } |
2483 | ||
2484 | /* If there were extra arguments to the format, normally warn. However, | |
2485 | the standard does say extra arguments are ignored, so in the specific | |
2486 | case where we have multiple leaves (conditional expressions or | |
2487 | ngettext) allow extra arguments if at least one leaf didn't have extra | |
2488 | arguments, but was otherwise OK (either non-literal or checked OK). | |
2489 | If the format is an empty string, this should be counted similarly to the | |
2490 | case of extra format arguments. */ | |
2491 | if (res.number_extra_args > 0 && res.number_non_literal == 0 | |
4d808927 | 2492 | && res.number_other == 0 && warn_format_extra_args) |
6d2e1454 JM |
2493 | status_warning (status, "too many arguments for format"); |
2494 | if (res.number_dollar_extra_args > 0 && res.number_non_literal == 0 | |
4d808927 | 2495 | && res.number_other == 0 && warn_format_extra_args) |
6d2e1454 JM |
2496 | status_warning (status, "unused arguments in $-style format"); |
2497 | if (res.number_empty > 0 && res.number_non_literal == 0 | |
2498 | && res.number_other == 0) | |
2499 | status_warning (status, "zero-length format string"); | |
2500 | ||
2501 | if (res.number_wide > 0) | |
2502 | status_warning (status, "format is a wide character string"); | |
2503 | ||
2504 | if (res.number_unterminated > 0) | |
2505 | status_warning (status, "unterminated format string"); | |
2506 | } | |
2507 | ||
2508 | ||
2509 | /* Recursively check a call to a format function. FORMAT_TREE is the | |
2510 | format parameter, which may be a conditional expression in which | |
2511 | both halves should be checked. ARG_NUM is the number of the | |
2512 | format argument; PARAMS points just after it in the argument list. */ | |
2513 | ||
2514 | static void | |
2515 | check_format_info_recurse (status, res, info, format_tree, params, arg_num) | |
2516 | int *status; | |
2517 | format_check_results *res; | |
2518 | function_format_info *info; | |
2519 | tree format_tree; | |
2520 | tree params; | |
2521 | int arg_num; | |
2522 | { | |
2523 | int format_length; | |
2524 | const char *format_chars; | |
2f74f7e9 JM |
2525 | tree array_size = 0; |
2526 | tree array_init; | |
6d2e1454 JM |
2527 | |
2528 | if (TREE_CODE (format_tree) == NOP_EXPR) | |
2529 | { | |
2530 | /* Strip coercion. */ | |
2531 | check_format_info_recurse (status, res, info, | |
2532 | TREE_OPERAND (format_tree, 0), params, | |
2533 | arg_num); | |
2534 | return; | |
2535 | } | |
0161e8da RK |
2536 | |
2537 | if (TREE_CODE (format_tree) == CALL_EXPR | |
2538 | && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR | |
2539 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0)) | |
2540 | == FUNCTION_DECL)) | |
2541 | { | |
2542 | tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0); | |
2543 | ||
2544 | /* See if this is a call to a known internationalization function | |
2545 | that modifies the format arg. */ | |
6d2e1454 | 2546 | international_format_info *iinfo; |
0161e8da | 2547 | |
6d2e1454 JM |
2548 | for (iinfo = international_format_list; iinfo; iinfo = iinfo->next) |
2549 | if (iinfo->assembler_name | |
2550 | ? (iinfo->assembler_name == DECL_ASSEMBLER_NAME (function)) | |
2551 | : (iinfo->name == DECL_NAME (function))) | |
0161e8da RK |
2552 | { |
2553 | tree inner_args; | |
2554 | int i; | |
2555 | ||
2556 | for (inner_args = TREE_OPERAND (format_tree, 1), i = 1; | |
2557 | inner_args != 0; | |
2558 | inner_args = TREE_CHAIN (inner_args), i++) | |
6d2e1454 | 2559 | if (i == iinfo->format_num) |
0161e8da | 2560 | { |
6d2e1454 JM |
2561 | /* FIXME: with Marc Espie's __attribute__((nonnull)) |
2562 | patch in GCC, we will have chained attributes, | |
2563 | and be able to handle functions like ngettext | |
2564 | with multiple format_arg attributes properly. */ | |
2565 | check_format_info_recurse (status, res, info, | |
2566 | TREE_VALUE (inner_args), params, | |
2567 | arg_num); | |
2568 | return; | |
0161e8da RK |
2569 | } |
2570 | } | |
2571 | } | |
2572 | ||
6d2e1454 JM |
2573 | if (TREE_CODE (format_tree) == COND_EXPR) |
2574 | { | |
2575 | /* Check both halves of the conditional expression. */ | |
2576 | check_format_info_recurse (status, res, info, | |
2577 | TREE_OPERAND (format_tree, 1), params, | |
2578 | arg_num); | |
2579 | check_format_info_recurse (status, res, info, | |
2580 | TREE_OPERAND (format_tree, 2), params, | |
2581 | arg_num); | |
2582 | return; | |
2583 | } | |
2584 | ||
39b751ce | 2585 | if (integer_zerop (format_tree)) |
1ccf251f | 2586 | { |
6d2e1454 JM |
2587 | /* FIXME: this warning should go away once Marc Espie's |
2588 | __attribute__((nonnull)) patch is in. Instead, checking for | |
2589 | nonnull attributes should probably change this function to act | |
2590 | specially if info == NULL and add a res->number_null entry for | |
2591 | that case, or maybe add a function pointer to be called at | |
2592 | the end instead of hardcoding check_format_info_main. */ | |
3f80e6ac | 2593 | status_warning (status, "null format string"); |
6d2e1454 JM |
2594 | |
2595 | /* Skip to first argument to check, so we can see if this format | |
2596 | has any arguments (it shouldn't). */ | |
2597 | while (arg_num + 1 < info->first_arg_num) | |
2598 | { | |
2599 | if (params == 0) | |
2600 | return; | |
2601 | params = TREE_CHAIN (params); | |
2602 | ++arg_num; | |
2603 | } | |
2604 | ||
2605 | if (params == 0) | |
2606 | res->number_other++; | |
2607 | else | |
2608 | res->number_extra_args++; | |
2609 | ||
1ccf251f RK |
2610 | return; |
2611 | } | |
6d2e1454 | 2612 | |
1ccf251f | 2613 | if (TREE_CODE (format_tree) != ADDR_EXPR) |
ba806745 | 2614 | { |
6d2e1454 | 2615 | res->number_non_literal++; |
ba806745 KR |
2616 | return; |
2617 | } | |
1ccf251f | 2618 | format_tree = TREE_OPERAND (format_tree, 0); |
2f74f7e9 JM |
2619 | if (TREE_CODE (format_tree) == VAR_DECL |
2620 | && TREE_CODE (TREE_TYPE (format_tree)) == ARRAY_TYPE | |
2621 | && (array_init = decl_constant_value (format_tree)) != format_tree | |
2622 | && TREE_CODE (array_init) == STRING_CST) | |
2623 | { | |
2624 | /* Extract the string constant initializer. Note that this may include | |
2625 | a trailing NUL character that is not in the array (e.g. | |
2626 | const char a[3] = "foo";). */ | |
2627 | array_size = DECL_SIZE_UNIT (format_tree); | |
2628 | format_tree = array_init; | |
2629 | } | |
1ccf251f | 2630 | if (TREE_CODE (format_tree) != STRING_CST) |
ba806745 | 2631 | { |
6d2e1454 | 2632 | res->number_non_literal++; |
ba806745 KR |
2633 | return; |
2634 | } | |
c5412f06 JM |
2635 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node) |
2636 | { | |
6d2e1454 | 2637 | res->number_wide++; |
c5412f06 JM |
2638 | return; |
2639 | } | |
1ccf251f RK |
2640 | format_chars = TREE_STRING_POINTER (format_tree); |
2641 | format_length = TREE_STRING_LENGTH (format_tree); | |
2f74f7e9 JM |
2642 | if (array_size != 0) |
2643 | { | |
2644 | /* Variable length arrays can't be initialized. */ | |
2645 | if (TREE_CODE (array_size) != INTEGER_CST) | |
2646 | abort (); | |
2647 | if (host_integerp (array_size, 0)) | |
2648 | { | |
2649 | HOST_WIDE_INT array_size_value = TREE_INT_CST_LOW (array_size); | |
2650 | if (array_size_value > 0 | |
2651 | && array_size_value == (int) array_size_value | |
2652 | && format_length > array_size_value) | |
2653 | format_length = array_size_value; | |
2654 | } | |
2655 | } | |
6d2e1454 JM |
2656 | if (format_length < 1) |
2657 | { | |
2658 | res->number_unterminated++; | |
2659 | return; | |
2660 | } | |
2661 | if (format_length == 1) | |
2662 | { | |
2663 | res->number_empty++; | |
2664 | return; | |
2665 | } | |
1ccf251f RK |
2666 | if (format_chars[--format_length] != 0) |
2667 | { | |
6d2e1454 | 2668 | res->number_unterminated++; |
1ccf251f RK |
2669 | return; |
2670 | } | |
6d2e1454 | 2671 | |
1ccf251f RK |
2672 | /* Skip to first argument to check. */ |
2673 | while (arg_num + 1 < info->first_arg_num) | |
2674 | { | |
2675 | if (params == 0) | |
2676 | return; | |
2677 | params = TREE_CHAIN (params); | |
2678 | ++arg_num; | |
2679 | } | |
6d2e1454 JM |
2680 | /* Provisionally increment res->number_other; check_format_info_main |
2681 | will decrement it if it finds there are extra arguments, but this way | |
2682 | need not adjust it for every return. */ | |
2683 | res->number_other++; | |
2684 | check_format_info_main (status, res, info, format_chars, format_length, | |
2685 | params, arg_num); | |
2686 | } | |
2687 | ||
2688 | ||
2689 | /* Do the main part of checking a call to a format function. FORMAT_CHARS | |
2690 | is the NUL-terminated format string (which at this point may contain | |
2691 | internal NUL characters); FORMAT_LENGTH is its length (excluding the | |
2692 | terminating NUL character). ARG_NUM is one less than the number of | |
2693 | the first format argument to check; PARAMS points to that format | |
2694 | argument in the list of arguments. */ | |
2695 | ||
2696 | static void | |
2697 | check_format_info_main (status, res, info, format_chars, format_length, | |
2698 | params, arg_num) | |
2699 | int *status; | |
2700 | format_check_results *res; | |
2701 | function_format_info *info; | |
2702 | const char *format_chars; | |
2703 | int format_length; | |
2704 | tree params; | |
2705 | int arg_num; | |
2706 | { | |
6d2e1454 | 2707 | const char *orig_format_chars = format_chars; |
c5251260 JM |
2708 | tree first_fillin_param = params; |
2709 | ||
2710 | const format_kind_info *fki = &format_types[info->format_type]; | |
2711 | const format_flag_spec *flag_specs = fki->flag_specs; | |
2712 | const format_flag_pair *bad_flag_pairs = fki->bad_flag_pairs; | |
2713 | ||
6d2e1454 JM |
2714 | /* -1 if no conversions taking an operand have been found; 0 if one has |
2715 | and it didn't use $; 1 if $ formats are in use. */ | |
2716 | int has_operand_number = -1; | |
9b69f523 | 2717 | |
5804a754 | 2718 | init_dollar_format_checking (info->first_arg_num, first_fillin_param); |
c5251260 | 2719 | |
1ccf251f RK |
2720 | while (1) |
2721 | { | |
c5251260 JM |
2722 | int i; |
2723 | int suppressed = FALSE; | |
2724 | const char *length_chars = NULL; | |
2725 | enum format_lengths length_chars_val = FMT_LEN_none; | |
2726 | enum format_std_version length_chars_std = STD_C89; | |
2727 | int format_char; | |
2728 | tree cur_param; | |
2729 | tree wanted_type; | |
2730 | int main_arg_num = 0; | |
2731 | tree main_arg_params = 0; | |
2732 | enum format_std_version wanted_type_std; | |
2733 | const char *wanted_type_name; | |
2734 | format_wanted_type width_wanted_type; | |
2735 | format_wanted_type precision_wanted_type; | |
2736 | format_wanted_type main_wanted_type; | |
2737 | format_wanted_type *first_wanted_type = NULL; | |
2738 | format_wanted_type *last_wanted_type = NULL; | |
2739 | const format_length_info *fli = NULL; | |
2740 | const format_char_info *fci = NULL; | |
2741 | char flag_chars[256]; | |
2742 | int aflag = 0; | |
1ccf251f RK |
2743 | if (*format_chars == 0) |
2744 | { | |
6d2e1454 | 2745 | if (format_chars - orig_format_chars != format_length) |
3f80e6ac | 2746 | status_warning (status, "embedded `\\0' in format"); |
5804a754 JM |
2747 | if (info->first_arg_num != 0 && params != 0 |
2748 | && has_operand_number <= 0) | |
6d2e1454 JM |
2749 | { |
2750 | res->number_other--; | |
2751 | res->number_extra_args++; | |
2752 | } | |
5804a754 | 2753 | if (has_operand_number > 0) |
6d2e1454 | 2754 | finish_dollar_format_checking (status, res); |
1ccf251f RK |
2755 | return; |
2756 | } | |
2757 | if (*format_chars++ != '%') | |
2758 | continue; | |
2759 | if (*format_chars == 0) | |
2760 | { | |
3f80e6ac | 2761 | status_warning (status, "spurious trailing `%%' in format"); |
1ccf251f RK |
2762 | continue; |
2763 | } | |
2764 | if (*format_chars == '%') | |
2765 | { | |
2766 | ++format_chars; | |
2767 | continue; | |
2768 | } | |
2769 | flag_chars[0] = 0; | |
2588e44e | 2770 | |
26f6672d | 2771 | if ((fki->flags & FMT_FLAG_USE_DOLLAR) && has_operand_number != 0) |
1ccf251f | 2772 | { |
2588e44e JM |
2773 | /* Possibly read a $ operand number at the start of the format. |
2774 | If one was previously used, one is required here. If one | |
2775 | is not used here, we can't immediately conclude this is a | |
2776 | format without them, since it could be printf %m or scanf %*. */ | |
2777 | int opnum; | |
2778 | opnum = maybe_read_dollar_number (status, &format_chars, 0, | |
2779 | first_fillin_param, | |
b691cb00 | 2780 | &main_arg_params, fki); |
2588e44e JM |
2781 | if (opnum == -1) |
2782 | return; | |
2783 | else if (opnum > 0) | |
5804a754 | 2784 | { |
2588e44e JM |
2785 | has_operand_number = 1; |
2786 | main_arg_num = opnum + info->first_arg_num - 1; | |
5804a754 | 2787 | } |
1ccf251f | 2788 | } |
2588e44e JM |
2789 | |
2790 | /* Read any format flags, but do not yet validate them beyond removing | |
2791 | duplicates, since in general validation depends on the rest of | |
2792 | the format. */ | |
0304bbad JM |
2793 | while (*format_chars != 0 |
2794 | && strchr (fki->flag_chars, *format_chars) != 0) | |
2588e44e | 2795 | { |
26f6672d JM |
2796 | const format_flag_spec *s = get_flag_spec (flag_specs, |
2797 | *format_chars, NULL); | |
0304bbad | 2798 | if (strchr (flag_chars, *format_chars) != 0) |
bb72a084 | 2799 | { |
2588e44e | 2800 | status_warning (status, "repeated %s in format", _(s->name)); |
bb72a084 | 2801 | } |
2588e44e | 2802 | else |
bb72a084 PE |
2803 | { |
2804 | i = strlen (flag_chars); | |
2588e44e | 2805 | flag_chars[i++] = *format_chars; |
bb72a084 | 2806 | flag_chars[i] = 0; |
bb72a084 | 2807 | } |
26f6672d JM |
2808 | if (s->skip_next_char) |
2809 | { | |
2810 | ++format_chars; | |
2811 | if (*format_chars == 0) | |
2812 | { | |
2813 | status_warning (status, "missing fill character at end of strfmon format"); | |
2814 | return; | |
2815 | } | |
2816 | } | |
2588e44e | 2817 | ++format_chars; |
bb72a084 | 2818 | } |
9b69f523 | 2819 | |
2588e44e JM |
2820 | /* Read any format width, possibly * or *m$. */ |
2821 | if (fki->width_char != 0) | |
2822 | { | |
2823 | if (fki->width_type != NULL && *format_chars == '*') | |
1ccf251f | 2824 | { |
2588e44e JM |
2825 | i = strlen (flag_chars); |
2826 | flag_chars[i++] = fki->width_char; | |
2827 | flag_chars[i] = 0; | |
1ccf251f RK |
2828 | /* "...a field width...may be indicated by an asterisk. |
2829 | In this case, an int argument supplies the field width..." */ | |
2830 | ++format_chars; | |
2831 | if (params == 0) | |
2832 | { | |
3f80e6ac | 2833 | status_warning (status, "too few arguments for format"); |
1ccf251f RK |
2834 | return; |
2835 | } | |
fc6df7e1 | 2836 | if (has_operand_number != 0) |
5804a754 JM |
2837 | { |
2838 | int opnum; | |
3f80e6ac | 2839 | opnum = maybe_read_dollar_number (status, &format_chars, |
fc6df7e1 | 2840 | has_operand_number == 1, |
5804a754 | 2841 | first_fillin_param, |
b691cb00 | 2842 | ¶ms, fki); |
fc6df7e1 | 2843 | if (opnum == -1) |
5804a754 | 2844 | return; |
fc6df7e1 JM |
2845 | else if (opnum > 0) |
2846 | { | |
2847 | has_operand_number = 1; | |
2848 | arg_num = opnum + info->first_arg_num - 1; | |
2849 | } | |
5804a754 | 2850 | else |
fc6df7e1 | 2851 | has_operand_number = 0; |
5804a754 | 2852 | } |
1ccf251f RK |
2853 | if (info->first_arg_num != 0) |
2854 | { | |
2855 | cur_param = TREE_VALUE (params); | |
5804a754 JM |
2856 | if (has_operand_number <= 0) |
2857 | { | |
2858 | params = TREE_CHAIN (params); | |
2859 | ++arg_num; | |
2860 | } | |
2588e44e | 2861 | width_wanted_type.wanted_type = *fki->width_type; |
6a357625 JM |
2862 | width_wanted_type.wanted_type_name = NULL; |
2863 | width_wanted_type.pointer_count = 0; | |
2864 | width_wanted_type.char_lenient_flag = 0; | |
2865 | width_wanted_type.writing_in_flag = 0; | |
014e7f1d | 2866 | width_wanted_type.reading_from_flag = 0; |
6a357625 JM |
2867 | width_wanted_type.name = _("field width"); |
2868 | width_wanted_type.param = cur_param; | |
2869 | width_wanted_type.arg_num = arg_num; | |
2870 | width_wanted_type.next = NULL; | |
2871 | if (last_wanted_type != 0) | |
2872 | last_wanted_type->next = &width_wanted_type; | |
2873 | if (first_wanted_type == 0) | |
2874 | first_wanted_type = &width_wanted_type; | |
2875 | last_wanted_type = &width_wanted_type; | |
1ccf251f RK |
2876 | } |
2877 | } | |
2878 | else | |
2879 | { | |
2588e44e | 2880 | /* Possibly read a numeric width. If the width is zero, |
26f6672d | 2881 | we complain if appropriate. */ |
2588e44e JM |
2882 | int non_zero_width_char = FALSE; |
2883 | int found_width = FALSE; | |
e9a780ec | 2884 | while (ISDIGIT (*format_chars)) |
1ccf251f | 2885 | { |
2588e44e JM |
2886 | found_width = TRUE; |
2887 | if (*format_chars != '0') | |
2888 | non_zero_width_char = TRUE; | |
1ccf251f RK |
2889 | ++format_chars; |
2890 | } | |
26f6672d JM |
2891 | if (found_width && !non_zero_width_char && |
2892 | (fki->flags & FMT_FLAG_ZERO_WIDTH_BAD)) | |
c5251260 JM |
2893 | status_warning (status, "zero width in %s format", |
2894 | fki->name); | |
2588e44e JM |
2895 | if (found_width) |
2896 | { | |
2897 | i = strlen (flag_chars); | |
2898 | flag_chars[i++] = fki->width_char; | |
2899 | flag_chars[i] = 0; | |
2900 | } | |
1ccf251f | 2901 | } |
2588e44e JM |
2902 | } |
2903 | ||
26f6672d JM |
2904 | /* Read any format left precision (must be a number, not *). */ |
2905 | if (fki->left_precision_char != 0 && *format_chars == '#') | |
2906 | { | |
2907 | ++format_chars; | |
2908 | i = strlen (flag_chars); | |
2909 | flag_chars[i++] = fki->left_precision_char; | |
2910 | flag_chars[i] = 0; | |
2911 | if (!ISDIGIT (*format_chars)) | |
2912 | status_warning (status, "empty left precision in %s format", | |
2913 | fki->name); | |
2914 | while (ISDIGIT (*format_chars)) | |
2915 | ++format_chars; | |
2916 | } | |
2917 | ||
2588e44e JM |
2918 | /* Read any format precision, possibly * or *m$. */ |
2919 | if (fki->precision_char != 0 && *format_chars == '.') | |
2920 | { | |
2921 | ++format_chars; | |
2922 | i = strlen (flag_chars); | |
2923 | flag_chars[i++] = fki->precision_char; | |
2924 | flag_chars[i] = 0; | |
2925 | if (fki->precision_type != NULL && *format_chars == '*') | |
1ccf251f | 2926 | { |
1ccf251f RK |
2927 | /* "...a...precision...may be indicated by an asterisk. |
2928 | In this case, an int argument supplies the...precision." */ | |
2588e44e JM |
2929 | ++format_chars; |
2930 | if (has_operand_number != 0) | |
1ccf251f | 2931 | { |
2588e44e JM |
2932 | int opnum; |
2933 | opnum = maybe_read_dollar_number (status, &format_chars, | |
2934 | has_operand_number == 1, | |
2935 | first_fillin_param, | |
b691cb00 | 2936 | ¶ms, fki); |
2588e44e JM |
2937 | if (opnum == -1) |
2938 | return; | |
2939 | else if (opnum > 0) | |
1ccf251f | 2940 | { |
2588e44e JM |
2941 | has_operand_number = 1; |
2942 | arg_num = opnum + info->first_arg_num - 1; | |
1ccf251f | 2943 | } |
2588e44e JM |
2944 | else |
2945 | has_operand_number = 0; | |
1ccf251f | 2946 | } |
2588e44e | 2947 | if (info->first_arg_num != 0) |
1ccf251f | 2948 | { |
2588e44e JM |
2949 | if (params == 0) |
2950 | { | |
2951 | status_warning (status, "too few arguments for format"); | |
2952 | return; | |
2953 | } | |
2954 | cur_param = TREE_VALUE (params); | |
2955 | if (has_operand_number <= 0) | |
2956 | { | |
2957 | params = TREE_CHAIN (params); | |
2958 | ++arg_num; | |
2959 | } | |
2960 | precision_wanted_type.wanted_type = *fki->precision_type; | |
2961 | precision_wanted_type.wanted_type_name = NULL; | |
2962 | precision_wanted_type.pointer_count = 0; | |
2963 | precision_wanted_type.char_lenient_flag = 0; | |
2964 | precision_wanted_type.writing_in_flag = 0; | |
014e7f1d | 2965 | precision_wanted_type.reading_from_flag = 0; |
2588e44e JM |
2966 | precision_wanted_type.name = _("field precision"); |
2967 | precision_wanted_type.param = cur_param; | |
2968 | precision_wanted_type.arg_num = arg_num; | |
2969 | precision_wanted_type.next = NULL; | |
2970 | if (last_wanted_type != 0) | |
2971 | last_wanted_type->next = &precision_wanted_type; | |
2972 | if (first_wanted_type == 0) | |
2973 | first_wanted_type = &precision_wanted_type; | |
2974 | last_wanted_type = &precision_wanted_type; | |
1ccf251f RK |
2975 | } |
2976 | } | |
2588e44e JM |
2977 | else |
2978 | { | |
26f6672d JM |
2979 | if (!(fki->flags & FMT_FLAG_EMPTY_PREC_OK) |
2980 | && !ISDIGIT (*format_chars)) | |
2981 | status_warning (status, "empty precision in %s format", | |
2982 | fki->name); | |
2588e44e JM |
2983 | while (ISDIGIT (*format_chars)) |
2984 | ++format_chars; | |
2985 | } | |
1ccf251f | 2986 | } |
bb72a084 | 2987 | |
2588e44e | 2988 | /* Read any length modifier, if this kind of format has them. */ |
3c786c69 | 2989 | fli = fki->length_char_specs; |
2588e44e JM |
2990 | length_chars = NULL; |
2991 | length_chars_val = FMT_LEN_none; | |
2992 | length_chars_std = STD_C89; | |
3c786c69 | 2993 | if (fli) |
af3c5588 | 2994 | { |
3c786c69 JM |
2995 | while (fli->name != 0 && fli->name[0] != *format_chars) |
2996 | fli++; | |
2997 | if (fli->name != 0) | |
bb72a084 | 2998 | { |
3c786c69 JM |
2999 | format_chars++; |
3000 | if (fli->double_name != 0 && fli->name[0] == *format_chars) | |
89abf8d1 | 3001 | { |
3c786c69 JM |
3002 | format_chars++; |
3003 | length_chars = fli->double_name; | |
3004 | length_chars_val = fli->double_index; | |
3005 | length_chars_std = fli->double_std; | |
3006 | } | |
3007 | else | |
3008 | { | |
3009 | length_chars = fli->name; | |
3010 | length_chars_val = fli->index; | |
3011 | length_chars_std = fli->std; | |
89abf8d1 | 3012 | } |
2588e44e JM |
3013 | i = strlen (flag_chars); |
3014 | flag_chars[i++] = fki->length_code_char; | |
3015 | flag_chars[i] = 0; | |
bb72a084 | 3016 | } |
3c786c69 | 3017 | if (pedantic) |
bc516719 | 3018 | { |
3c786c69 | 3019 | /* Warn if the length modifier is non-standard. */ |
20d380b1 | 3020 | if (ADJ_STD (length_chars_std) > C_STD_VER) |
ade7d3cc JM |
3021 | status_warning (status, "%s does not support the `%s' %s length modifier", |
3022 | C_STD_NAME (length_chars_std), length_chars, | |
3023 | fki->name); | |
bc516719 | 3024 | } |
2588e44e JM |
3025 | } |
3026 | ||
3027 | /* Read any modifier (strftime E/O). */ | |
3028 | if (fki->modifier_chars != NULL) | |
3029 | { | |
3030 | while (*format_chars != 0 | |
0304bbad | 3031 | && strchr (fki->modifier_chars, *format_chars) != 0) |
2588e44e | 3032 | { |
0304bbad | 3033 | if (strchr (flag_chars, *format_chars) != 0) |
2588e44e JM |
3034 | { |
3035 | const format_flag_spec *s = get_flag_spec (flag_specs, | |
3036 | *format_chars, NULL); | |
3037 | status_warning (status, "repeated %s in format", _(s->name)); | |
3038 | } | |
3039 | else | |
3040 | { | |
3041 | i = strlen (flag_chars); | |
3042 | flag_chars[i++] = *format_chars; | |
3043 | flag_chars[i] = 0; | |
3044 | } | |
3045 | ++format_chars; | |
3046 | } | |
3047 | } | |
3048 | ||
3049 | /* Handle the scanf allocation kludge. */ | |
3050 | if (fki->flags & FMT_FLAG_SCANF_A_KLUDGE) | |
3051 | { | |
3052 | if (*format_chars == 'a' && !flag_isoc99) | |
bb72a084 PE |
3053 | { |
3054 | if (format_chars[1] == 's' || format_chars[1] == 'S' | |
3055 | || format_chars[1] == '[') | |
3056 | { | |
3057 | /* `a' is used as a flag. */ | |
2588e44e JM |
3058 | i = strlen (flag_chars); |
3059 | flag_chars[i++] = 'a'; | |
3060 | flag_chars[i] = 0; | |
bb72a084 PE |
3061 | format_chars++; |
3062 | } | |
3063 | } | |
1ccf251f | 3064 | } |
2588e44e | 3065 | |
1ccf251f | 3066 | format_char = *format_chars; |
bb72a084 | 3067 | if (format_char == 0 |
2588e44e | 3068 | || (!(fki->flags & FMT_FLAG_FANCY_PERCENT_OK) && format_char == '%')) |
1ccf251f | 3069 | { |
3f80e6ac | 3070 | status_warning (status, "conversion lacks type at end of format"); |
1ccf251f RK |
3071 | continue; |
3072 | } | |
3073 | format_chars++; | |
3c786c69 | 3074 | fci = fki->conversion_specs; |
1ccf251f | 3075 | while (fci->format_chars != 0 |
0304bbad | 3076 | && strchr (fci->format_chars, format_char) == 0) |
1ccf251f RK |
3077 | ++fci; |
3078 | if (fci->format_chars == 0) | |
3079 | { | |
f3ad1f9c | 3080 | if (ISGRAPH(format_char)) |
3f80e6ac | 3081 | status_warning (status, "unknown conversion type character `%c' in format", |
1ccf251f RK |
3082 | format_char); |
3083 | else | |
3f80e6ac | 3084 | status_warning (status, "unknown conversion type character 0x%x in format", |
1ccf251f | 3085 | format_char); |
1ccf251f RK |
3086 | continue; |
3087 | } | |
bb72a084 | 3088 | if (pedantic) |
1ccf251f | 3089 | { |
20d380b1 | 3090 | if (ADJ_STD (fci->std) > C_STD_VER) |
ade7d3cc JM |
3091 | status_warning (status, "%s does not support the `%%%c' %s format", |
3092 | C_STD_NAME (fci->std), format_char, fki->name); | |
1ccf251f | 3093 | } |
2588e44e JM |
3094 | |
3095 | /* Validate the individual flags used, removing any that are invalid. */ | |
3096 | { | |
3097 | int d = 0; | |
3098 | for (i = 0; flag_chars[i] != 0; i++) | |
3099 | { | |
3100 | const format_flag_spec *s = get_flag_spec (flag_specs, | |
3101 | flag_chars[i], NULL); | |
3102 | flag_chars[i - d] = flag_chars[i]; | |
3103 | if (flag_chars[i] == fki->length_code_char) | |
3104 | continue; | |
0304bbad | 3105 | if (strchr (fci->flag_chars, flag_chars[i]) == 0) |
2588e44e JM |
3106 | { |
3107 | status_warning (status, "%s used with `%%%c' %s format", | |
3108 | _(s->name), format_char, fki->name); | |
3109 | d++; | |
3110 | continue; | |
3111 | } | |
3112 | if (pedantic) | |
3113 | { | |
3114 | const format_flag_spec *t; | |
20d380b1 | 3115 | if (ADJ_STD (s->std) > C_STD_VER) |
ade7d3cc JM |
3116 | status_warning (status, "%s does not support %s", |
3117 | C_STD_NAME (s->std), _(s->long_name)); | |
2588e44e | 3118 | t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2); |
20d380b1 | 3119 | if (t != NULL && ADJ_STD (t->std) > ADJ_STD (s->std)) |
2588e44e JM |
3120 | { |
3121 | const char *long_name = (t->long_name != NULL | |
3122 | ? t->long_name | |
3123 | : s->long_name); | |
20d380b1 | 3124 | if (ADJ_STD (t->std) > C_STD_VER) |
ade7d3cc JM |
3125 | status_warning (status, "%s does not support %s with the `%%%c' %s format", |
3126 | C_STD_NAME (t->std), _(long_name), | |
3127 | format_char, fki->name); | |
2588e44e JM |
3128 | } |
3129 | } | |
3130 | } | |
3131 | flag_chars[i - d] = 0; | |
3132 | } | |
3133 | ||
2588e44e | 3134 | if ((fki->flags & FMT_FLAG_SCANF_A_KLUDGE) |
0304bbad | 3135 | && strchr (flag_chars, 'a') != 0) |
2588e44e JM |
3136 | aflag = 1; |
3137 | ||
3138 | if (fki->suppression_char | |
0304bbad | 3139 | && strchr (flag_chars, fki->suppression_char) != 0) |
2588e44e JM |
3140 | suppressed = 1; |
3141 | ||
3142 | /* Validate the pairs of flags used. */ | |
3143 | for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++) | |
af3c5588 | 3144 | { |
2588e44e | 3145 | const format_flag_spec *s, *t; |
0304bbad | 3146 | if (strchr (flag_chars, bad_flag_pairs[i].flag_char1) == 0) |
2588e44e | 3147 | continue; |
0304bbad | 3148 | if (strchr (flag_chars, bad_flag_pairs[i].flag_char2) == 0) |
2588e44e JM |
3149 | continue; |
3150 | if (bad_flag_pairs[i].predicate != 0 | |
0304bbad | 3151 | && strchr (fci->flags2, bad_flag_pairs[i].predicate) == 0) |
2588e44e JM |
3152 | continue; |
3153 | s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL); | |
3154 | t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL); | |
3155 | if (bad_flag_pairs[i].ignored) | |
3156 | { | |
3157 | if (bad_flag_pairs[i].predicate != 0) | |
3158 | status_warning (status, "%s ignored with %s and `%%%c' %s format", | |
3159 | _(s->name), _(t->name), format_char, | |
3160 | fki->name); | |
3161 | else | |
3162 | status_warning (status, "%s ignored with %s in %s format", | |
3163 | _(s->name), _(t->name), fki->name); | |
3164 | } | |
3165 | else | |
3166 | { | |
3167 | if (bad_flag_pairs[i].predicate != 0) | |
3168 | status_warning (status, "use of %s and %s together with `%%%c' %s format", | |
3169 | _(s->name), _(t->name), format_char, | |
3170 | fki->name); | |
3171 | else | |
3172 | status_warning (status, "use of %s and %s together in %s format", | |
3173 | _(s->name), _(t->name), fki->name); | |
3174 | } | |
1ccf251f | 3175 | } |
2588e44e JM |
3176 | |
3177 | /* Give Y2K warnings. */ | |
4d808927 JM |
3178 | if (warn_format_y2k) |
3179 | { | |
3180 | int y2k_level = 0; | |
3181 | if (strchr (fci->flags2, '4') != 0) | |
3182 | if (strchr (flag_chars, 'E') != 0) | |
3183 | y2k_level = 3; | |
3184 | else | |
3185 | y2k_level = 2; | |
3186 | else if (strchr (fci->flags2, '3') != 0) | |
2588e44e | 3187 | y2k_level = 3; |
4d808927 | 3188 | else if (strchr (fci->flags2, '2') != 0) |
2588e44e | 3189 | y2k_level = 2; |
4d808927 JM |
3190 | if (y2k_level == 3) |
3191 | status_warning (status, "`%%%c' yields only last 2 digits of year in some locales", | |
3192 | format_char); | |
3193 | else if (y2k_level == 2) | |
3194 | status_warning (status, "`%%%c' yields only last 2 digits of year", format_char); | |
3195 | } | |
2588e44e | 3196 | |
0304bbad | 3197 | if (strchr (fci->flags2, '[') != 0) |
1ccf251f RK |
3198 | { |
3199 | /* Skip over scan set, in case it happens to have '%' in it. */ | |
3200 | if (*format_chars == '^') | |
3201 | ++format_chars; | |
3202 | /* Find closing bracket; if one is hit immediately, then | |
3203 | it's part of the scan set rather than a terminator. */ | |
3204 | if (*format_chars == ']') | |
3205 | ++format_chars; | |
3206 | while (*format_chars && *format_chars != ']') | |
3207 | ++format_chars; | |
3208 | if (*format_chars != ']') | |
bb72a084 | 3209 | /* The end of the format string was reached. */ |
3f80e6ac | 3210 | status_warning (status, "no closing `]' for `%%[' format"); |
1ccf251f | 3211 | } |
2588e44e | 3212 | |
9fa8f75d JM |
3213 | wanted_type = 0; |
3214 | wanted_type_name = 0; | |
2588e44e | 3215 | if (fki->flags & FMT_FLAG_ARG_CONVERT) |
6a357625 | 3216 | { |
2588e44e JM |
3217 | wanted_type = (fci->types[length_chars_val].type |
3218 | ? *fci->types[length_chars_val].type : 0); | |
3219 | wanted_type_name = fci->types[length_chars_val].name; | |
3220 | wanted_type_std = fci->types[length_chars_val].std; | |
3221 | if (wanted_type == 0) | |
3222 | { | |
3223 | status_warning (status, "use of `%s' length modifier with `%c' type character", | |
3224 | length_chars, format_char); | |
3225 | /* Heuristic: skip one argument when an invalid length/type | |
3226 | combination is encountered. */ | |
3227 | arg_num++; | |
3228 | if (params == 0) | |
3229 | { | |
3230 | status_warning (status, "too few arguments for format"); | |
3231 | return; | |
3232 | } | |
3233 | params = TREE_CHAIN (params); | |
3234 | continue; | |
3235 | } | |
3236 | else if (pedantic | |
3237 | /* Warn if non-standard, provided it is more non-standard | |
3238 | than the length and type characters that may already | |
3239 | have been warned for. */ | |
20d380b1 JM |
3240 | && ADJ_STD (wanted_type_std) > ADJ_STD (length_chars_std) |
3241 | && ADJ_STD (wanted_type_std) > ADJ_STD (fci->std)) | |
6a357625 | 3242 | { |
20d380b1 | 3243 | if (ADJ_STD (wanted_type_std) > C_STD_VER) |
ade7d3cc JM |
3244 | status_warning (status, "%s does not support the `%%%s%c' %s format", |
3245 | C_STD_NAME (wanted_type_std), length_chars, | |
3246 | format_char, fki->name); | |
6a357625 | 3247 | } |
1ccf251f | 3248 | } |
1ccf251f | 3249 | |
1ccf251f RK |
3250 | /* Finally. . .check type of argument against desired type! */ |
3251 | if (info->first_arg_num == 0) | |
3252 | continue; | |
2588e44e JM |
3253 | if ((fci->pointer_count == 0 && wanted_type == void_type_node) |
3254 | || suppressed) | |
1ccf251f | 3255 | { |
fc6df7e1 | 3256 | if (main_arg_num != 0) |
2588e44e JM |
3257 | { |
3258 | if (suppressed) | |
3259 | status_warning (status, "operand number specified with suppressed assignment"); | |
3260 | else | |
3261 | status_warning (status, "operand number specified for format taking no argument"); | |
3262 | } | |
fc6df7e1 JM |
3263 | } |
3264 | else | |
3265 | { | |
3266 | if (main_arg_num != 0) | |
6a357625 | 3267 | { |
fc6df7e1 JM |
3268 | arg_num = main_arg_num; |
3269 | params = main_arg_params; | |
3270 | } | |
3271 | else | |
3272 | { | |
3273 | ++arg_num; | |
3274 | if (has_operand_number > 0) | |
3275 | { | |
3f80e6ac | 3276 | status_warning (status, "missing $ operand number in format"); |
fc6df7e1 JM |
3277 | return; |
3278 | } | |
3279 | else | |
3280 | has_operand_number = 0; | |
3281 | if (params == 0) | |
3282 | { | |
3f80e6ac | 3283 | status_warning (status, "too few arguments for format"); |
fc6df7e1 JM |
3284 | return; |
3285 | } | |
6a357625 JM |
3286 | } |
3287 | cur_param = TREE_VALUE (params); | |
3288 | params = TREE_CHAIN (params); | |
6a357625 JM |
3289 | main_wanted_type.wanted_type = wanted_type; |
3290 | main_wanted_type.wanted_type_name = wanted_type_name; | |
3291 | main_wanted_type.pointer_count = fci->pointer_count + aflag; | |
3292 | main_wanted_type.char_lenient_flag = 0; | |
0304bbad | 3293 | if (strchr (fci->flags2, 'c') != 0) |
6a357625 JM |
3294 | main_wanted_type.char_lenient_flag = 1; |
3295 | main_wanted_type.writing_in_flag = 0; | |
014e7f1d JM |
3296 | main_wanted_type.reading_from_flag = 0; |
3297 | if (aflag) | |
6a357625 | 3298 | main_wanted_type.writing_in_flag = 1; |
014e7f1d JM |
3299 | else |
3300 | { | |
3301 | if (strchr (fci->flags2, 'W') != 0) | |
3302 | main_wanted_type.writing_in_flag = 1; | |
3303 | if (strchr (fci->flags2, 'R') != 0) | |
3304 | main_wanted_type.reading_from_flag = 1; | |
3305 | } | |
6a357625 JM |
3306 | main_wanted_type.name = NULL; |
3307 | main_wanted_type.param = cur_param; | |
3308 | main_wanted_type.arg_num = arg_num; | |
3309 | main_wanted_type.next = NULL; | |
3310 | if (last_wanted_type != 0) | |
3311 | last_wanted_type->next = &main_wanted_type; | |
3312 | if (first_wanted_type == 0) | |
3313 | first_wanted_type = &main_wanted_type; | |
3314 | last_wanted_type = &main_wanted_type; | |
1ccf251f | 3315 | } |
6a357625 JM |
3316 | |
3317 | if (first_wanted_type != 0) | |
3f80e6ac | 3318 | check_format_types (status, first_wanted_type); |
6a357625 JM |
3319 | |
3320 | } | |
3321 | } | |
3322 | ||
3323 | ||
3324 | /* Check the argument types from a single format conversion (possibly | |
3325 | including width and precision arguments). */ | |
3326 | static void | |
3f80e6ac KG |
3327 | check_format_types (status, types) |
3328 | int *status; | |
6a357625 JM |
3329 | format_wanted_type *types; |
3330 | { | |
3331 | for (; types != 0; types = types->next) | |
3332 | { | |
3333 | tree cur_param; | |
3334 | tree cur_type; | |
99303ae4 | 3335 | tree orig_cur_type; |
6a357625 | 3336 | tree wanted_type; |
2f7026a0 | 3337 | tree promoted_type; |
6a357625 JM |
3338 | int arg_num; |
3339 | int i; | |
3340 | int char_type_flag; | |
3341 | cur_param = types->param; | |
1ccf251f | 3342 | cur_type = TREE_TYPE (cur_param); |
0304bbad | 3343 | if (cur_type == error_mark_node) |
99303ae4 | 3344 | continue; |
6a357625 JM |
3345 | char_type_flag = 0; |
3346 | wanted_type = types->wanted_type; | |
3347 | arg_num = types->arg_num; | |
1ccf251f | 3348 | |
99303ae4 JM |
3349 | /* The following should not occur here. */ |
3350 | if (wanted_type == 0) | |
3351 | abort (); | |
3352 | if (wanted_type == void_type_node && types->pointer_count == 0) | |
3353 | abort (); | |
3354 | ||
2f7026a0 JM |
3355 | if (types->pointer_count == 0) |
3356 | { | |
3357 | promoted_type = simple_type_promotes_to (wanted_type); | |
3358 | if (promoted_type != NULL_TREE) | |
3359 | wanted_type = promoted_type; | |
3360 | } | |
3361 | ||
6d819282 MK |
3362 | STRIP_NOPS (cur_param); |
3363 | ||
1ccf251f RK |
3364 | /* Check the types of any additional pointer arguments |
3365 | that precede the "real" argument. */ | |
6a357625 | 3366 | for (i = 0; i < types->pointer_count; ++i) |
1ccf251f RK |
3367 | { |
3368 | if (TREE_CODE (cur_type) == POINTER_TYPE) | |
3369 | { | |
3370 | cur_type = TREE_TYPE (cur_type); | |
0304bbad | 3371 | if (cur_type == error_mark_node) |
99303ae4 | 3372 | break; |
6d819282 | 3373 | |
81cf47b2 JM |
3374 | /* Check for writing through a NULL pointer. */ |
3375 | if (types->writing_in_flag | |
3376 | && i == 0 | |
3377 | && cur_param != 0 | |
3378 | && integer_zerop (cur_param)) | |
3379 | status_warning (status, | |
3380 | "writing through null pointer (arg %d)", | |
3381 | arg_num); | |
3382 | ||
014e7f1d JM |
3383 | /* Check for reading through a NULL pointer. */ |
3384 | if (types->reading_from_flag | |
3385 | && i == 0 | |
3386 | && cur_param != 0 | |
3387 | && integer_zerop (cur_param)) | |
3388 | status_warning (status, | |
3389 | "reading through null pointer (arg %d)", | |
3390 | arg_num); | |
3391 | ||
f5963e61 | 3392 | if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR) |
6d819282 MK |
3393 | cur_param = TREE_OPERAND (cur_param, 0); |
3394 | else | |
3395 | cur_param = 0; | |
3396 | ||
f3d360aa JM |
3397 | /* See if this is an attempt to write into a const type with |
3398 | scanf or with printf "%n". Note: the writing in happens | |
3399 | at the first indirection only, if for example | |
3400 | void * const * is passed to scanf %p; passing | |
3401 | const void ** is simply passing an incompatible type. */ | |
6a357625 | 3402 | if (types->writing_in_flag |
f3d360aa | 3403 | && i == 0 |
f3d360aa JM |
3404 | && (TYPE_READONLY (cur_type) |
3405 | || (cur_param != 0 | |
3406 | && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c' | |
3407 | || (DECL_P (cur_param) | |
3408 | && TREE_READONLY (cur_param)))))) | |
3f80e6ac | 3409 | status_warning (status, "writing into constant object (arg %d)", arg_num); |
f3d360aa JM |
3410 | |
3411 | /* If there are extra type qualifiers beyond the first | |
3412 | indirection, then this makes the types technically | |
3413 | incompatible. */ | |
3414 | if (i > 0 | |
3415 | && pedantic | |
f3d360aa JM |
3416 | && (TYPE_READONLY (cur_type) |
3417 | || TYPE_VOLATILE (cur_type) | |
3418 | || TYPE_RESTRICT (cur_type))) | |
3f80e6ac | 3419 | status_warning (status, "extra type qualifiers in format argument (arg %d)", |
f3d360aa JM |
3420 | arg_num); |
3421 | ||
1ccf251f | 3422 | } |
99303ae4 | 3423 | else |
913d0833 | 3424 | { |
6a357625 | 3425 | if (types->pointer_count == 1) |
3f80e6ac | 3426 | status_warning (status, "format argument is not a pointer (arg %d)", arg_num); |
913d0833 | 3427 | else |
3f80e6ac | 3428 | status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num); |
99303ae4 | 3429 | break; |
913d0833 | 3430 | } |
1ccf251f RK |
3431 | } |
3432 | ||
99303ae4 JM |
3433 | if (i < types->pointer_count) |
3434 | continue; | |
3435 | ||
3436 | orig_cur_type = cur_type; | |
3437 | cur_type = TYPE_MAIN_VARIANT (cur_type); | |
3438 | ||
cd732418 JM |
3439 | /* Check whether the argument type is a character type. This leniency |
3440 | only applies to certain formats, flagged with 'c'. | |
3441 | */ | |
99303ae4 JM |
3442 | if (types->char_lenient_flag) |
3443 | char_type_flag = (cur_type == char_type_node | |
3444 | || cur_type == signed_char_type_node | |
3445 | || cur_type == unsigned_char_type_node); | |
b8458e3e | 3446 | |
1ccf251f | 3447 | /* Check the type of the "real" argument, if there's a type we want. */ |
99303ae4 JM |
3448 | if (wanted_type == cur_type) |
3449 | continue; | |
3450 | /* If we want `void *', allow any pointer type. | |
3451 | (Anything else would already have got a warning.) | |
3452 | With -pedantic, only allow pointers to void and to character | |
3453 | types. */ | |
3454 | if (wanted_type == void_type_node | |
3455 | && (!pedantic || (i == 1 && char_type_flag))) | |
3456 | continue; | |
3457 | /* Don't warn about differences merely in signedness, unless | |
3458 | -pedantic. With -pedantic, warn if the type is a pointer | |
3459 | target and not a character type, and for character types at | |
3460 | a second level of indirection. */ | |
3461 | if (TREE_CODE (wanted_type) == INTEGER_TYPE | |
3462 | && TREE_CODE (cur_type) == INTEGER_TYPE | |
3463 | && (! pedantic || i == 0 || (i == 1 && char_type_flag)) | |
3464 | && (TREE_UNSIGNED (wanted_type) | |
3465 | ? wanted_type == unsigned_type (cur_type) | |
3466 | : wanted_type == signed_type (cur_type))) | |
3467 | continue; | |
3468 | /* Likewise, "signed char", "unsigned char" and "char" are | |
3469 | equivalent but the above test won't consider them equivalent. */ | |
3470 | if (wanted_type == char_type_node | |
3471 | && (! pedantic || i < 2) | |
3472 | && char_type_flag) | |
3473 | continue; | |
3474 | /* Now we have a type mismatch. */ | |
3475 | { | |
3476 | register const char *this; | |
3477 | register const char *that; | |
3478 | ||
3479 | this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type))); | |
3480 | that = 0; | |
3481 | if (TYPE_NAME (orig_cur_type) != 0 | |
3482 | && TREE_CODE (orig_cur_type) != INTEGER_TYPE | |
3483 | && !(TREE_CODE (orig_cur_type) == POINTER_TYPE | |
3484 | && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE)) | |
3485 | { | |
3486 | if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL | |
3487 | && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0) | |
3488 | that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type))); | |
3489 | else | |
3490 | that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type)); | |
3491 | } | |
1ccf251f | 3492 | |
99303ae4 JM |
3493 | /* A nameless type can't possibly match what the format wants. |
3494 | So there will be a warning for it. | |
3495 | Make up a string to describe vaguely what it is. */ | |
3496 | if (that == 0) | |
3497 | { | |
3498 | if (TREE_CODE (orig_cur_type) == POINTER_TYPE) | |
3499 | that = "pointer"; | |
3500 | else | |
3501 | that = "different type"; | |
3502 | } | |
1ccf251f | 3503 | |
99303ae4 JM |
3504 | /* Make the warning better in case of mismatch of int vs long. */ |
3505 | if (TREE_CODE (orig_cur_type) == INTEGER_TYPE | |
3506 | && TREE_CODE (wanted_type) == INTEGER_TYPE | |
3507 | && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type) | |
3508 | && TYPE_NAME (orig_cur_type) != 0 | |
3509 | && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL) | |
3510 | that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type))); | |
309ffab6 | 3511 | |
99303ae4 JM |
3512 | if (strcmp (this, that) != 0) |
3513 | { | |
3514 | /* There may be a better name for the format, e.g. size_t, | |
3515 | but we should allow for programs with a perverse typedef | |
3516 | making size_t something other than what the compiler | |
3517 | thinks. */ | |
3518 | if (types->wanted_type_name != 0 | |
3519 | && strcmp (types->wanted_type_name, that) != 0) | |
3520 | this = types->wanted_type_name; | |
3521 | if (types->name != 0) | |
3f80e6ac | 3522 | status_warning (status, "%s is not type %s (arg %d)", types->name, this, |
99303ae4 JM |
3523 | arg_num); |
3524 | else | |
3f80e6ac | 3525 | status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num); |
99303ae4 JM |
3526 | } |
3527 | } | |
1ccf251f RK |
3528 | } |
3529 | } | |
4d808927 JM |
3530 | |
3531 | /* Set format warning options according to a -Wformat=n option. */ | |
3532 | ||
3533 | void | |
3534 | set_Wformat (setting) | |
3535 | int setting; | |
3536 | { | |
3537 | warn_format = setting; | |
3538 | warn_format_y2k = setting; | |
3539 | warn_format_extra_args = setting; | |
3540 | if (setting != 1) | |
c907e684 JM |
3541 | { |
3542 | warn_format_nonliteral = setting; | |
3543 | warn_format_security = setting; | |
3544 | } | |
4d808927 | 3545 | } |
1ccf251f | 3546 | \f |
d74154d5 RS |
3547 | /* Print a warning if a constant expression had overflow in folding. |
3548 | Invoke this function on every expression that the language | |
3549 | requires to be a constant expression. | |
3550 | Note the ANSI C standard says it is erroneous for a | |
3551 | constant expression to overflow. */ | |
96571883 BK |
3552 | |
3553 | void | |
3554 | constant_expression_warning (value) | |
3555 | tree value; | |
3556 | { | |
c05f751c RK |
3557 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
3558 | || TREE_CODE (value) == COMPLEX_CST) | |
3559 | && TREE_CONSTANT_OVERFLOW (value) && pedantic) | |
3560 | pedwarn ("overflow in constant expression"); | |
d74154d5 RS |
3561 | } |
3562 | ||
3563 | /* Print a warning if an expression had overflow in folding. | |
3564 | Invoke this function on every expression that | |
3565 | (1) appears in the source code, and | |
3566 | (2) might be a constant expression that overflowed, and | |
3567 | (3) is not already checked by convert_and_check; | |
3568 | however, do not invoke this function on operands of explicit casts. */ | |
3569 | ||
3570 | void | |
3571 | overflow_warning (value) | |
3572 | tree value; | |
3573 | { | |
c05f751c RK |
3574 | if ((TREE_CODE (value) == INTEGER_CST |
3575 | || (TREE_CODE (value) == COMPLEX_CST | |
3576 | && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)) | |
3577 | && TREE_OVERFLOW (value)) | |
d74154d5 | 3578 | { |
7193bce2 | 3579 | TREE_OVERFLOW (value) = 0; |
e78a3b42 RK |
3580 | if (skip_evaluation == 0) |
3581 | warning ("integer overflow in expression"); | |
d74154d5 | 3582 | } |
c05f751c RK |
3583 | else if ((TREE_CODE (value) == REAL_CST |
3584 | || (TREE_CODE (value) == COMPLEX_CST | |
3585 | && TREE_CODE (TREE_REALPART (value)) == REAL_CST)) | |
3586 | && TREE_OVERFLOW (value)) | |
3587 | { | |
3588 | TREE_OVERFLOW (value) = 0; | |
e78a3b42 RK |
3589 | if (skip_evaluation == 0) |
3590 | warning ("floating point overflow in expression"); | |
c05f751c | 3591 | } |
d74154d5 RS |
3592 | } |
3593 | ||
3594 | /* Print a warning if a large constant is truncated to unsigned, | |
3595 | or if -Wconversion is used and a constant < 0 is converted to unsigned. | |
3596 | Invoke this function on every expression that might be implicitly | |
3597 | converted to an unsigned type. */ | |
3598 | ||
3599 | void | |
3600 | unsigned_conversion_warning (result, operand) | |
3601 | tree result, operand; | |
3602 | { | |
3603 | if (TREE_CODE (operand) == INTEGER_CST | |
3604 | && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE | |
3605 | && TREE_UNSIGNED (TREE_TYPE (result)) | |
e78a3b42 | 3606 | && skip_evaluation == 0 |
d74154d5 RS |
3607 | && !int_fits_type_p (operand, TREE_TYPE (result))) |
3608 | { | |
3609 | if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result)))) | |
3610 | /* This detects cases like converting -129 or 256 to unsigned char. */ | |
90c939d4 | 3611 | warning ("large integer implicitly truncated to unsigned type"); |
d74154d5 | 3612 | else if (warn_conversion) |
90c939d4 | 3613 | warning ("negative integer implicitly converted to unsigned type"); |
d74154d5 RS |
3614 | } |
3615 | } | |
3616 | ||
3617 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
3618 | Invoke this function on every expression that is converted implicitly, | |
3619 | i.e. because of language rules and not because of an explicit cast. */ | |
3620 | ||
3621 | tree | |
3622 | convert_and_check (type, expr) | |
3623 | tree type, expr; | |
3624 | { | |
3625 | tree t = convert (type, expr); | |
3626 | if (TREE_CODE (t) == INTEGER_CST) | |
3627 | { | |
7193bce2 | 3628 | if (TREE_OVERFLOW (t)) |
d74154d5 | 3629 | { |
7193bce2 PE |
3630 | TREE_OVERFLOW (t) = 0; |
3631 | ||
868fc750 RK |
3632 | /* Do not diagnose overflow in a constant expression merely |
3633 | because a conversion overflowed. */ | |
3634 | TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr); | |
3635 | ||
7193bce2 PE |
3636 | /* No warning for converting 0x80000000 to int. */ |
3637 | if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr)) | |
3638 | && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE | |
3639 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr)))) | |
22ba338b RS |
3640 | /* If EXPR fits in the unsigned version of TYPE, |
3641 | don't warn unless pedantic. */ | |
e78a3b42 RK |
3642 | if ((pedantic |
3643 | || TREE_UNSIGNED (type) | |
3644 | || ! int_fits_type_p (expr, unsigned_type (type))) | |
3645 | && skip_evaluation == 0) | |
bb72a084 | 3646 | warning ("overflow in implicit constant conversion"); |
d74154d5 RS |
3647 | } |
3648 | else | |
3649 | unsigned_conversion_warning (t, expr); | |
3650 | } | |
3651 | return t; | |
96571883 BK |
3652 | } |
3653 | \f | |
235cfbc4 BS |
3654 | /* A node in a list that describes references to variables (EXPR), which are |
3655 | either read accesses if WRITER is zero, or write accesses, in which case | |
3656 | WRITER is the parent of EXPR. */ | |
3657 | struct tlist | |
3658 | { | |
3659 | struct tlist *next; | |
3660 | tree expr, writer; | |
3661 | }; | |
3662 | ||
3663 | /* Used to implement a cache the results of a call to verify_tree. We only | |
3664 | use this for SAVE_EXPRs. */ | |
3665 | struct tlist_cache | |
3666 | { | |
3667 | struct tlist_cache *next; | |
3668 | struct tlist *cache_before_sp; | |
3669 | struct tlist *cache_after_sp; | |
3670 | tree expr; | |
2683ed8d BS |
3671 | }; |
3672 | ||
235cfbc4 BS |
3673 | /* Obstack to use when allocating tlist structures, and corresponding |
3674 | firstobj. */ | |
3675 | static struct obstack tlist_obstack; | |
3676 | static char *tlist_firstobj = 0; | |
3677 | ||
3678 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
3679 | warnings. */ | |
3680 | static struct tlist *warned_ids; | |
3681 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
3682 | cache the results. */ | |
3683 | static struct tlist_cache *save_expr_cache; | |
3684 | ||
3685 | static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int)); | |
3686 | static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int)); | |
3687 | static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree)); | |
3688 | static int warning_candidate_p PARAMS ((tree)); | |
3689 | static void warn_for_collisions PARAMS ((struct tlist *)); | |
3690 | static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int)); | |
3691 | static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree)); | |
2683ed8d BS |
3692 | static void verify_sequence_points PARAMS ((tree)); |
3693 | ||
235cfbc4 BS |
3694 | /* Create a new struct tlist and fill in its fields. */ |
3695 | static struct tlist * | |
3696 | new_tlist (next, t, writer) | |
3697 | struct tlist *next; | |
3698 | tree t; | |
3699 | tree writer; | |
3700 | { | |
3701 | struct tlist *l; | |
3702 | l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l); | |
3703 | l->next = next; | |
3704 | l->expr = t; | |
3705 | l->writer = writer; | |
3706 | return l; | |
3707 | } | |
3708 | ||
3709 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
3710 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
3711 | ||
3712 | static void | |
3713 | add_tlist (to, add, exclude_writer, copy) | |
3714 | struct tlist **to; | |
3715 | struct tlist *add; | |
3716 | tree exclude_writer; | |
3717 | int copy; | |
3718 | { | |
3719 | while (add) | |
3720 | { | |
3721 | struct tlist *next = add->next; | |
3722 | if (! copy) | |
3723 | add->next = *to; | |
3724 | if (! exclude_writer || add->writer != exclude_writer) | |
3725 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; | |
3726 | add = next; | |
3727 | } | |
3728 | } | |
3729 | ||
3730 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
3731 | each variable that already exists in TO, no new node is added; however if | |
3732 | there is a write access recorded in ADD, and an occurrence on TO is only | |
3733 | a read access, then the occurrence in TO will be modified to record the | |
3734 | write. */ | |
2683ed8d BS |
3735 | |
3736 | static void | |
235cfbc4 BS |
3737 | merge_tlist (to, add, copy) |
3738 | struct tlist **to; | |
3739 | struct tlist *add; | |
3740 | int copy; | |
3741 | { | |
3742 | struct tlist **end = to; | |
3743 | ||
3744 | while (*end) | |
3745 | end = &(*end)->next; | |
3746 | ||
3747 | while (add) | |
3748 | { | |
3749 | int found = 0; | |
3750 | struct tlist *tmp2; | |
3751 | struct tlist *next = add->next; | |
3752 | ||
3753 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
3754 | if (tmp2->expr == add->expr) | |
3755 | { | |
3756 | found = 1; | |
3757 | if (! tmp2->writer) | |
3758 | tmp2->writer = add->writer; | |
3759 | } | |
3760 | if (! found) | |
3761 | { | |
3762 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
3763 | end = &(*end)->next; | |
3764 | *end = 0; | |
3765 | } | |
3766 | add = next; | |
3767 | } | |
3768 | } | |
3769 | ||
3770 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
3771 | references in list LIST conflict with it, excluding reads if ONLY writers | |
3772 | is nonzero. */ | |
3773 | ||
3774 | static void | |
3775 | warn_for_collisions_1 (written, writer, list, only_writes) | |
3776 | tree written, writer; | |
3777 | struct tlist *list; | |
3778 | int only_writes; | |
3779 | { | |
3780 | struct tlist *tmp; | |
3781 | ||
3782 | /* Avoid duplicate warnings. */ | |
3783 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
3784 | if (tmp->expr == written) | |
3785 | return; | |
3786 | ||
3787 | while (list) | |
3788 | { | |
3789 | if (list->expr == written | |
3790 | && list->writer != writer | |
3791 | && (! only_writes || list->writer)) | |
3792 | { | |
3793 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
3794 | warning ("operation on `%s' may be undefined", | |
3795 | IDENTIFIER_POINTER (DECL_NAME (list->expr))); | |
3796 | } | |
3797 | list = list->next; | |
3798 | } | |
3799 | } | |
3800 | ||
3801 | /* Given a list LIST of references to variables, find whether any of these | |
3802 | can cause conflicts due to missing sequence points. */ | |
3803 | ||
3804 | static void | |
3805 | warn_for_collisions (list) | |
3806 | struct tlist *list; | |
3807 | { | |
3808 | struct tlist *tmp; | |
3809 | ||
3810 | for (tmp = list; tmp; tmp = tmp->next) | |
3811 | { | |
3812 | if (tmp->writer) | |
3813 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
3814 | } | |
3815 | } | |
3816 | ||
3817 | /* Return nonzero if X is a tree that can be verified by the sequence poitn | |
3818 | warnings. */ | |
3819 | static int | |
3820 | warning_candidate_p (x) | |
2683ed8d | 3821 | tree x; |
2683ed8d | 3822 | { |
235cfbc4 BS |
3823 | return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL; |
3824 | } | |
2683ed8d | 3825 | |
235cfbc4 BS |
3826 | /* Walk the tree X, and record accesses to variables. If X is written by the |
3827 | parent tree, WRITER is the parent. | |
3828 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
3829 | expression or its only operand forces a sequence point, then everything up | |
3830 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
3831 | in PNO_SP. | |
3832 | Once we return, we will have emitted warnings if any subexpression before | |
3833 | such a sequence point could be undefined. On a higher level, however, the | |
3834 | sequence point may not be relevant, and we'll merge the two lists. | |
3835 | ||
3836 | Example: (b++, a) + b; | |
3837 | The call that processes the COMPOUND_EXPR will store the increment of B | |
3838 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
3839 | processes the PLUS_EXPR will need to merge the two lists so that | |
3840 | eventually, all accesses end up on the same list (and we'll warn about the | |
3841 | unordered subexpressions b++ and b. | |
3842 | ||
3843 | A note on merging. If we modify the former example so that our expression | |
3844 | becomes | |
3845 | (b++, b) + a | |
3846 | care must be taken not simply to add all three expressions into the final | |
3847 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
3848 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
3849 | way, so that no more than one access to B is recorded. */ | |
2683ed8d | 3850 | |
235cfbc4 BS |
3851 | static void |
3852 | verify_tree (x, pbefore_sp, pno_sp, writer) | |
3853 | tree x; | |
3854 | struct tlist **pbefore_sp, **pno_sp; | |
3855 | tree writer; | |
3856 | { | |
3857 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
3858 | enum tree_code code; | |
3859 | char class; | |
2683ed8d | 3860 | |
235cfbc4 BS |
3861 | restart: |
3862 | code = TREE_CODE (x); | |
3863 | class = TREE_CODE_CLASS (code); | |
2683ed8d | 3864 | |
235cfbc4 | 3865 | if (warning_candidate_p (x)) |
2683ed8d | 3866 | { |
235cfbc4 BS |
3867 | *pno_sp = new_tlist (*pno_sp, x, writer); |
3868 | return; | |
3869 | } | |
3870 | ||
3871 | switch (code) | |
3872 | { | |
52a84e42 BS |
3873 | case CONSTRUCTOR: |
3874 | return; | |
3875 | ||
235cfbc4 BS |
3876 | case COMPOUND_EXPR: |
3877 | case TRUTH_ANDIF_EXPR: | |
3878 | case TRUTH_ORIF_EXPR: | |
3879 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3880 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3881 | warn_for_collisions (tmp_nosp); | |
3882 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3883 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
3884 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
3885 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3886 | return; | |
3887 | ||
3888 | case COND_EXPR: | |
3889 | tmp_before = tmp_list2 = 0; | |
3890 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
3891 | warn_for_collisions (tmp_list2); | |
3892 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3893 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
3894 | ||
3895 | tmp_list3 = tmp_nosp = 0; | |
3896 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
3897 | warn_for_collisions (tmp_nosp); | |
3898 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3899 | ||
3900 | tmp_list3 = tmp_list2 = 0; | |
3901 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
3902 | warn_for_collisions (tmp_list2); | |
3903 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3904 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
3905 | two first, to avoid warning for (a ? b++ : b++). */ | |
3906 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
3907 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3908 | return; | |
3909 | ||
2683ed8d BS |
3910 | case PREDECREMENT_EXPR: |
3911 | case PREINCREMENT_EXPR: | |
3912 | case POSTDECREMENT_EXPR: | |
3913 | case POSTINCREMENT_EXPR: | |
235cfbc4 BS |
3914 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
3915 | return; | |
3916 | ||
3917 | case MODIFY_EXPR: | |
3918 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3919 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
3920 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
3921 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
3922 | in the RHS. Example: | |
3923 | *a = (a++, 2) | |
3924 | Despite the fact that the modification of "a" is in the before_sp | |
3925 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
3926 | We can handle this by adding the contents of tmp_list3 | |
3927 | to those of tmp_before, and redoing the collision warnings for that | |
3928 | list. */ | |
3929 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
3930 | warn_for_collisions (tmp_before); | |
3931 | /* Exclude the LHS itself here; we first have to merge it into the | |
3932 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
3933 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
3934 | as a write. */ | |
3935 | add_tlist (pno_sp, tmp_list3, x, 0); | |
3936 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
3937 | ||
3938 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3939 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
3940 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
3941 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
3942 | return; | |
2683ed8d BS |
3943 | |
3944 | case CALL_EXPR: | |
235cfbc4 BS |
3945 | /* We need to warn about conflicts among arguments and conflicts between |
3946 | args and the function address. Side effects of the function address, | |
3947 | however, are not ordered by the sequence point of the call. */ | |
3948 | tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0; | |
3949 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3950 | if (TREE_OPERAND (x, 1)) | |
3951 | verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE); | |
3952 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
3953 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
3954 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
3955 | warn_for_collisions (tmp_before); | |
3956 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
3957 | return; | |
2683ed8d BS |
3958 | |
3959 | case TREE_LIST: | |
3960 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
3961 | while (x) | |
3962 | { | |
235cfbc4 BS |
3963 | tmp_before = tmp_nosp = 0; |
3964 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
3965 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3966 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2683ed8d BS |
3967 | x = TREE_CHAIN (x); |
3968 | } | |
235cfbc4 | 3969 | return; |
2683ed8d | 3970 | |
235cfbc4 BS |
3971 | case SAVE_EXPR: |
3972 | { | |
3973 | struct tlist_cache *t; | |
3974 | for (t = save_expr_cache; t; t = t->next) | |
3975 | if (t->expr == x) | |
3976 | break; | |
2683ed8d | 3977 | |
235cfbc4 | 3978 | if (! t) |
2683ed8d | 3979 | { |
235cfbc4 BS |
3980 | t = (struct tlist_cache *) obstack_alloc (&tlist_obstack, |
3981 | sizeof *t); | |
3982 | t->next = save_expr_cache; | |
3983 | t->expr = x; | |
3984 | save_expr_cache = t; | |
3985 | ||
3986 | tmp_before = tmp_nosp = 0; | |
3987 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3988 | warn_for_collisions (tmp_nosp); | |
3989 | ||
3990 | tmp_list3 = 0; | |
3991 | while (tmp_nosp) | |
3992 | { | |
3993 | struct tlist *t = tmp_nosp; | |
3994 | tmp_nosp = t->next; | |
3995 | merge_tlist (&tmp_list3, t, 0); | |
3996 | } | |
3997 | t->cache_before_sp = tmp_before; | |
3998 | t->cache_after_sp = tmp_list3; | |
2683ed8d | 3999 | } |
235cfbc4 BS |
4000 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
4001 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
4002 | return; | |
4003 | } | |
4004 | default: | |
2683ed8d BS |
4005 | break; |
4006 | } | |
2683ed8d | 4007 | |
235cfbc4 | 4008 | if (class == '1') |
2683ed8d | 4009 | { |
235cfbc4 BS |
4010 | if (first_rtl_op (code) == 0) |
4011 | return; | |
4012 | x = TREE_OPERAND (x, 0); | |
4013 | writer = 0; | |
4014 | goto restart; | |
2683ed8d BS |
4015 | } |
4016 | ||
235cfbc4 | 4017 | switch (class) |
2683ed8d | 4018 | { |
235cfbc4 BS |
4019 | case 'r': |
4020 | case '<': | |
4021 | case '2': | |
4022 | case 'b': | |
4023 | case 'e': | |
4024 | case 's': | |
4025 | case 'x': | |
4026 | { | |
4027 | int lp; | |
4028 | int max = first_rtl_op (TREE_CODE (x)); | |
4029 | for (lp = 0; lp < max; lp++) | |
4030 | { | |
4031 | tmp_before = tmp_nosp = 0; | |
4032 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE); | |
4033 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
4034 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
4035 | } | |
4036 | break; | |
4037 | } | |
2683ed8d | 4038 | } |
2683ed8d BS |
4039 | } |
4040 | ||
4041 | /* Try to warn for undefined behaviour in EXPR due to missing sequence | |
4042 | points. */ | |
4043 | ||
4044 | static void | |
4045 | verify_sequence_points (expr) | |
4046 | tree expr; | |
4047 | { | |
235cfbc4 | 4048 | struct tlist *before_sp = 0, *after_sp = 0; |
2683ed8d | 4049 | |
235cfbc4 BS |
4050 | warned_ids = 0; |
4051 | save_expr_cache = 0; | |
4052 | if (tlist_firstobj == 0) | |
2683ed8d | 4053 | { |
235cfbc4 BS |
4054 | gcc_obstack_init (&tlist_obstack); |
4055 | tlist_firstobj = obstack_alloc (&tlist_obstack, 0); | |
2683ed8d BS |
4056 | } |
4057 | ||
235cfbc4 BS |
4058 | verify_tree (expr, &before_sp, &after_sp, 0); |
4059 | warn_for_collisions (after_sp); | |
4060 | obstack_free (&tlist_obstack, tlist_firstobj); | |
2683ed8d BS |
4061 | } |
4062 | ||
64094f6a | 4063 | tree |
b30f223b RS |
4064 | c_expand_expr_stmt (expr) |
4065 | tree expr; | |
4066 | { | |
4067 | /* Do default conversion if safe and possibly important, | |
4068 | in case within ({...}). */ | |
4069 | if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr)) | |
4070 | || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE) | |
4071 | expr = default_conversion (expr); | |
4072 | ||
2683ed8d BS |
4073 | if (warn_sequence_point) |
4074 | verify_sequence_points (expr); | |
4075 | ||
b30f223b | 4076 | if (TREE_TYPE (expr) != error_mark_node |
b8de2d02 | 4077 | && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr)) |
b30f223b RS |
4078 | && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE) |
4079 | error ("expression statement has incomplete type"); | |
4080 | ||
8f17b5c5 | 4081 | last_expr_type = TREE_TYPE (expr); |
64094f6a | 4082 | return add_stmt (build_stmt (EXPR_STMT, expr)); |
b30f223b RS |
4083 | } |
4084 | \f | |
4085 | /* Validate the expression after `case' and apply default promotions. */ | |
4086 | ||
4087 | tree | |
4088 | check_case_value (value) | |
4089 | tree value; | |
4090 | { | |
4091 | if (value == NULL_TREE) | |
4092 | return value; | |
4093 | ||
4094 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
8493738b | 4095 | STRIP_TYPE_NOPS (value); |
56cb9733 MM |
4096 | /* In C++, the following is allowed: |
4097 | ||
4098 | const int i = 3; | |
4099 | switch (...) { case i: ... } | |
4100 | ||
4101 | So, we try to reduce the VALUE to a constant that way. */ | |
4102 | if (c_language == clk_cplusplus) | |
4103 | { | |
4104 | value = decl_constant_value (value); | |
4105 | STRIP_TYPE_NOPS (value); | |
4106 | value = fold (value); | |
4107 | } | |
b30f223b RS |
4108 | |
4109 | if (TREE_CODE (value) != INTEGER_CST | |
4110 | && value != error_mark_node) | |
4111 | { | |
4112 | error ("case label does not reduce to an integer constant"); | |
4113 | value = error_mark_node; | |
4114 | } | |
4115 | else | |
4116 | /* Promote char or short to int. */ | |
4117 | value = default_conversion (value); | |
4118 | ||
bc690db1 RS |
4119 | constant_expression_warning (value); |
4120 | ||
b30f223b RS |
4121 | return value; |
4122 | } | |
4123 | \f | |
4124 | /* Return an integer type with BITS bits of precision, | |
4125 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
4126 | ||
4127 | tree | |
4128 | type_for_size (bits, unsignedp) | |
4129 | unsigned bits; | |
4130 | int unsignedp; | |
4131 | { | |
a311b52c JM |
4132 | if (bits == TYPE_PRECISION (integer_type_node)) |
4133 | return unsignedp ? unsigned_type_node : integer_type_node; | |
4134 | ||
3fc7e390 | 4135 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b30f223b RS |
4136 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4137 | ||
3fc7e390 | 4138 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b30f223b RS |
4139 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4140 | ||
3fc7e390 | 4141 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b30f223b RS |
4142 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4143 | ||
3fc7e390 | 4144 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b30f223b RS |
4145 | return (unsignedp ? long_long_unsigned_type_node |
4146 | : long_long_integer_type_node); | |
4147 | ||
835f9b4d GRK |
4148 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
4149 | return (unsignedp ? widest_unsigned_literal_type_node | |
4150 | : widest_integer_literal_type_node); | |
4151 | ||
3fc7e390 RS |
4152 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
4153 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
4154 | ||
4155 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
4156 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
4157 | ||
4158 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
4159 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
4160 | ||
4161 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
4162 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
4163 | ||
b30f223b RS |
4164 | return 0; |
4165 | } | |
4166 | ||
4167 | /* Return a data type that has machine mode MODE. | |
4168 | If the mode is an integer, | |
4169 | then UNSIGNEDP selects between signed and unsigned types. */ | |
4170 | ||
4171 | tree | |
4172 | type_for_mode (mode, unsignedp) | |
4173 | enum machine_mode mode; | |
4174 | int unsignedp; | |
4175 | { | |
a311b52c JM |
4176 | if (mode == TYPE_MODE (integer_type_node)) |
4177 | return unsignedp ? unsigned_type_node : integer_type_node; | |
4178 | ||
b30f223b RS |
4179 | if (mode == TYPE_MODE (signed_char_type_node)) |
4180 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
4181 | ||
4182 | if (mode == TYPE_MODE (short_integer_type_node)) | |
4183 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
4184 | ||
b30f223b RS |
4185 | if (mode == TYPE_MODE (long_integer_type_node)) |
4186 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
4187 | ||
4188 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
4189 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
4190 | ||
835f9b4d | 4191 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
d125d268 | 4192 | return unsignedp ? widest_unsigned_literal_type_node |
835f9b4d GRK |
4193 | : widest_integer_literal_type_node; |
4194 | ||
3fc7e390 RS |
4195 | if (mode == TYPE_MODE (intQI_type_node)) |
4196 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
4197 | ||
4198 | if (mode == TYPE_MODE (intHI_type_node)) | |
4199 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
4200 | ||
4201 | if (mode == TYPE_MODE (intSI_type_node)) | |
4202 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
4203 | ||
4204 | if (mode == TYPE_MODE (intDI_type_node)) | |
4205 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
4206 | ||
21a9616b | 4207 | #if HOST_BITS_PER_WIDE_INT >= 64 |
a6d7e156 JL |
4208 | if (mode == TYPE_MODE (intTI_type_node)) |
4209 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
21a9616b | 4210 | #endif |
a6d7e156 | 4211 | |
b30f223b RS |
4212 | if (mode == TYPE_MODE (float_type_node)) |
4213 | return float_type_node; | |
4214 | ||
4215 | if (mode == TYPE_MODE (double_type_node)) | |
4216 | return double_type_node; | |
4217 | ||
4218 | if (mode == TYPE_MODE (long_double_type_node)) | |
4219 | return long_double_type_node; | |
4220 | ||
4221 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) | |
4222 | return build_pointer_type (char_type_node); | |
4223 | ||
4224 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
4225 | return build_pointer_type (integer_type_node); | |
4226 | ||
4061f623 BS |
4227 | #ifdef VECTOR_MODE_SUPPORTED_P |
4228 | if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
4229 | return V4SF_type_node; | |
4230 | if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
4231 | return V4SI_type_node; | |
4232 | if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
4233 | return V2SI_type_node; | |
4234 | if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
4235 | return V4HI_type_node; | |
4236 | if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode)) | |
4237 | return V8QI_type_node; | |
4238 | #endif | |
4239 | ||
b30f223b RS |
4240 | return 0; |
4241 | } | |
693a6128 GRK |
4242 | |
4243 | /* Return an unsigned type the same as TYPE in other respects. */ | |
4244 | tree | |
4245 | unsigned_type (type) | |
4246 | tree type; | |
4247 | { | |
4248 | tree type1 = TYPE_MAIN_VARIANT (type); | |
4249 | if (type1 == signed_char_type_node || type1 == char_type_node) | |
4250 | return unsigned_char_type_node; | |
4251 | if (type1 == integer_type_node) | |
4252 | return unsigned_type_node; | |
4253 | if (type1 == short_integer_type_node) | |
4254 | return short_unsigned_type_node; | |
4255 | if (type1 == long_integer_type_node) | |
4256 | return long_unsigned_type_node; | |
4257 | if (type1 == long_long_integer_type_node) | |
4258 | return long_long_unsigned_type_node; | |
4259 | if (type1 == widest_integer_literal_type_node) | |
4260 | return widest_unsigned_literal_type_node; | |
4261 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4262 | if (type1 == intTI_type_node) | |
4263 | return unsigned_intTI_type_node; | |
4264 | #endif | |
4265 | if (type1 == intDI_type_node) | |
4266 | return unsigned_intDI_type_node; | |
4267 | if (type1 == intSI_type_node) | |
4268 | return unsigned_intSI_type_node; | |
4269 | if (type1 == intHI_type_node) | |
4270 | return unsigned_intHI_type_node; | |
4271 | if (type1 == intQI_type_node) | |
4272 | return unsigned_intQI_type_node; | |
4273 | ||
4274 | return signed_or_unsigned_type (1, type); | |
4275 | } | |
4276 | ||
4277 | /* Return a signed type the same as TYPE in other respects. */ | |
4278 | ||
4279 | tree | |
4280 | signed_type (type) | |
4281 | tree type; | |
4282 | { | |
4283 | tree type1 = TYPE_MAIN_VARIANT (type); | |
4284 | if (type1 == unsigned_char_type_node || type1 == char_type_node) | |
4285 | return signed_char_type_node; | |
4286 | if (type1 == unsigned_type_node) | |
4287 | return integer_type_node; | |
4288 | if (type1 == short_unsigned_type_node) | |
4289 | return short_integer_type_node; | |
4290 | if (type1 == long_unsigned_type_node) | |
4291 | return long_integer_type_node; | |
4292 | if (type1 == long_long_unsigned_type_node) | |
4293 | return long_long_integer_type_node; | |
4294 | if (type1 == widest_unsigned_literal_type_node) | |
4295 | return widest_integer_literal_type_node; | |
4296 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4297 | if (type1 == unsigned_intTI_type_node) | |
4298 | return intTI_type_node; | |
4299 | #endif | |
4300 | if (type1 == unsigned_intDI_type_node) | |
4301 | return intDI_type_node; | |
4302 | if (type1 == unsigned_intSI_type_node) | |
4303 | return intSI_type_node; | |
4304 | if (type1 == unsigned_intHI_type_node) | |
4305 | return intHI_type_node; | |
4306 | if (type1 == unsigned_intQI_type_node) | |
4307 | return intQI_type_node; | |
4308 | ||
4309 | return signed_or_unsigned_type (0, type); | |
4310 | } | |
4311 | ||
4312 | /* Return a type the same as TYPE except unsigned or | |
4313 | signed according to UNSIGNEDP. */ | |
4314 | ||
4315 | tree | |
4316 | signed_or_unsigned_type (unsignedp, type) | |
4317 | int unsignedp; | |
4318 | tree type; | |
4319 | { | |
4320 | if (! INTEGRAL_TYPE_P (type) | |
4321 | || TREE_UNSIGNED (type) == unsignedp) | |
4322 | return type; | |
4323 | ||
4324 | if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node)) | |
4325 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
d125d268 | 4326 | if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)) |
693a6128 | 4327 | return unsignedp ? unsigned_type_node : integer_type_node; |
d125d268 | 4328 | if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node)) |
693a6128 | 4329 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
d125d268 | 4330 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node)) |
693a6128 | 4331 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
d125d268 | 4332 | if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node)) |
693a6128 GRK |
4333 | return (unsignedp ? long_long_unsigned_type_node |
4334 | : long_long_integer_type_node); | |
d125d268 | 4335 | if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node)) |
693a6128 GRK |
4336 | return (unsignedp ? widest_unsigned_literal_type_node |
4337 | : widest_integer_literal_type_node); | |
4338 | return type; | |
4339 | } | |
b30f223b | 4340 | \f |
6acfe908 JM |
4341 | /* Return the minimum number of bits needed to represent VALUE in a |
4342 | signed or unsigned type, UNSIGNEDP says which. */ | |
4343 | ||
05bccae2 | 4344 | unsigned int |
6acfe908 JM |
4345 | min_precision (value, unsignedp) |
4346 | tree value; | |
4347 | int unsignedp; | |
4348 | { | |
4349 | int log; | |
4350 | ||
4351 | /* If the value is negative, compute its negative minus 1. The latter | |
4352 | adjustment is because the absolute value of the largest negative value | |
4353 | is one larger than the largest positive value. This is equivalent to | |
4354 | a bit-wise negation, so use that operation instead. */ | |
4355 | ||
4356 | if (tree_int_cst_sgn (value) < 0) | |
4357 | value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value)); | |
4358 | ||
4359 | /* Return the number of bits needed, taking into account the fact | |
4360 | that we need one more bit for a signed than unsigned type. */ | |
4361 | ||
4362 | if (integer_zerop (value)) | |
4363 | log = 0; | |
6acfe908 | 4364 | else |
05bccae2 | 4365 | log = tree_floor_log2 (value); |
6acfe908 JM |
4366 | |
4367 | return log + 1 + ! unsignedp; | |
4368 | } | |
4369 | \f | |
b30f223b RS |
4370 | /* Print an error message for invalid operands to arith operation CODE. |
4371 | NOP_EXPR is used as a special case (see truthvalue_conversion). */ | |
4372 | ||
4373 | void | |
4374 | binary_op_error (code) | |
4375 | enum tree_code code; | |
4376 | { | |
dff01034 | 4377 | register const char *opname; |
89c78d7d | 4378 | |
b30f223b RS |
4379 | switch (code) |
4380 | { | |
4381 | case NOP_EXPR: | |
4382 | error ("invalid truth-value expression"); | |
4383 | return; | |
4384 | ||
4385 | case PLUS_EXPR: | |
4386 | opname = "+"; break; | |
4387 | case MINUS_EXPR: | |
4388 | opname = "-"; break; | |
4389 | case MULT_EXPR: | |
4390 | opname = "*"; break; | |
4391 | case MAX_EXPR: | |
4392 | opname = "max"; break; | |
4393 | case MIN_EXPR: | |
4394 | opname = "min"; break; | |
4395 | case EQ_EXPR: | |
4396 | opname = "=="; break; | |
4397 | case NE_EXPR: | |
4398 | opname = "!="; break; | |
4399 | case LE_EXPR: | |
4400 | opname = "<="; break; | |
4401 | case GE_EXPR: | |
4402 | opname = ">="; break; | |
4403 | case LT_EXPR: | |
4404 | opname = "<"; break; | |
4405 | case GT_EXPR: | |
4406 | opname = ">"; break; | |
4407 | case LSHIFT_EXPR: | |
4408 | opname = "<<"; break; | |
4409 | case RSHIFT_EXPR: | |
4410 | opname = ">>"; break; | |
4411 | case TRUNC_MOD_EXPR: | |
047de90b | 4412 | case FLOOR_MOD_EXPR: |
b30f223b RS |
4413 | opname = "%"; break; |
4414 | case TRUNC_DIV_EXPR: | |
047de90b | 4415 | case FLOOR_DIV_EXPR: |
b30f223b RS |
4416 | opname = "/"; break; |
4417 | case BIT_AND_EXPR: | |
4418 | opname = "&"; break; | |
4419 | case BIT_IOR_EXPR: | |
4420 | opname = "|"; break; | |
4421 | case TRUTH_ANDIF_EXPR: | |
4422 | opname = "&&"; break; | |
4423 | case TRUTH_ORIF_EXPR: | |
4424 | opname = "||"; break; | |
4425 | case BIT_XOR_EXPR: | |
4426 | opname = "^"; break; | |
047de90b RS |
4427 | case LROTATE_EXPR: |
4428 | case RROTATE_EXPR: | |
4429 | opname = "rotate"; break; | |
6d819282 MK |
4430 | default: |
4431 | opname = "unknown"; break; | |
b30f223b RS |
4432 | } |
4433 | error ("invalid operands to binary %s", opname); | |
4434 | } | |
4435 | \f | |
4436 | /* Subroutine of build_binary_op, used for comparison operations. | |
4437 | See if the operands have both been converted from subword integer types | |
4438 | and, if so, perhaps change them both back to their original type. | |
94dccd9d RS |
4439 | This function is also responsible for converting the two operands |
4440 | to the proper common type for comparison. | |
b30f223b RS |
4441 | |
4442 | The arguments of this function are all pointers to local variables | |
4443 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
4444 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
4445 | ||
4446 | If this function returns nonzero, it means that the comparison has | |
4447 | a constant value. What this function returns is an expression for | |
4448 | that value. */ | |
4449 | ||
4450 | tree | |
4451 | shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr) | |
4452 | tree *op0_ptr, *op1_ptr; | |
4453 | tree *restype_ptr; | |
4454 | enum tree_code *rescode_ptr; | |
4455 | { | |
4456 | register tree type; | |
4457 | tree op0 = *op0_ptr; | |
4458 | tree op1 = *op1_ptr; | |
4459 | int unsignedp0, unsignedp1; | |
4460 | int real1, real2; | |
4461 | tree primop0, primop1; | |
4462 | enum tree_code code = *rescode_ptr; | |
4463 | ||
4464 | /* Throw away any conversions to wider types | |
4465 | already present in the operands. */ | |
4466 | ||
4467 | primop0 = get_narrower (op0, &unsignedp0); | |
4468 | primop1 = get_narrower (op1, &unsignedp1); | |
4469 | ||
4470 | /* Handle the case that OP0 does not *contain* a conversion | |
4471 | but it *requires* conversion to FINAL_TYPE. */ | |
4472 | ||
4473 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
4474 | unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0)); | |
4475 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) | |
4476 | unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1)); | |
4477 | ||
4478 | /* If one of the operands must be floated, we cannot optimize. */ | |
4479 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
4480 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
4481 | ||
4482 | /* If first arg is constant, swap the args (changing operation | |
5af6001b RK |
4483 | so value is preserved), for canonicalization. Don't do this if |
4484 | the second arg is 0. */ | |
b30f223b | 4485 | |
5af6001b RK |
4486 | if (TREE_CONSTANT (primop0) |
4487 | && ! integer_zerop (primop1) && ! real_zerop (primop1)) | |
b30f223b RS |
4488 | { |
4489 | register tree tem = primop0; | |
4490 | register int temi = unsignedp0; | |
4491 | primop0 = primop1; | |
4492 | primop1 = tem; | |
4493 | tem = op0; | |
4494 | op0 = op1; | |
4495 | op1 = tem; | |
4496 | *op0_ptr = op0; | |
4497 | *op1_ptr = op1; | |
4498 | unsignedp0 = unsignedp1; | |
4499 | unsignedp1 = temi; | |
4500 | temi = real1; | |
4501 | real1 = real2; | |
4502 | real2 = temi; | |
4503 | ||
4504 | switch (code) | |
4505 | { | |
4506 | case LT_EXPR: | |
4507 | code = GT_EXPR; | |
4508 | break; | |
4509 | case GT_EXPR: | |
4510 | code = LT_EXPR; | |
4511 | break; | |
4512 | case LE_EXPR: | |
4513 | code = GE_EXPR; | |
4514 | break; | |
4515 | case GE_EXPR: | |
4516 | code = LE_EXPR; | |
4517 | break; | |
6d819282 MK |
4518 | default: |
4519 | break; | |
b30f223b RS |
4520 | } |
4521 | *rescode_ptr = code; | |
4522 | } | |
4523 | ||
4524 | /* If comparing an integer against a constant more bits wide, | |
4525 | maybe we can deduce a value of 1 or 0 independent of the data. | |
4526 | Or else truncate the constant now | |
4527 | rather than extend the variable at run time. | |
4528 | ||
4529 | This is only interesting if the constant is the wider arg. | |
4530 | Also, it is not safe if the constant is unsigned and the | |
4531 | variable arg is signed, since in this case the variable | |
4532 | would be sign-extended and then regarded as unsigned. | |
4533 | Our technique fails in this case because the lowest/highest | |
4534 | possible unsigned results don't follow naturally from the | |
4535 | lowest/highest possible values of the variable operand. | |
4536 | For just EQ_EXPR and NE_EXPR there is another technique that | |
4537 | could be used: see if the constant can be faithfully represented | |
4538 | in the other operand's type, by truncating it and reextending it | |
4539 | and see if that preserves the constant's value. */ | |
4540 | ||
4541 | if (!real1 && !real2 | |
4542 | && TREE_CODE (primop1) == INTEGER_CST | |
4543 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
4544 | { | |
4545 | int min_gt, max_gt, min_lt, max_lt; | |
4546 | tree maxval, minval; | |
4547 | /* 1 if comparison is nominally unsigned. */ | |
4548 | int unsignedp = TREE_UNSIGNED (*restype_ptr); | |
4549 | tree val; | |
4550 | ||
4551 | type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)); | |
8bbd5685 CW |
4552 | |
4553 | /* If TYPE is an enumeration, then we need to get its min/max | |
4554 | values from it's underlying integral type, not the enumerated | |
4555 | type itself. */ | |
4556 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
4557 | type = type_for_size (TYPE_PRECISION (type), unsignedp0); | |
b30f223b RS |
4558 | |
4559 | maxval = TYPE_MAX_VALUE (type); | |
4560 | minval = TYPE_MIN_VALUE (type); | |
4561 | ||
4562 | if (unsignedp && !unsignedp0) | |
4563 | *restype_ptr = signed_type (*restype_ptr); | |
4564 | ||
4565 | if (TREE_TYPE (primop1) != *restype_ptr) | |
4566 | primop1 = convert (*restype_ptr, primop1); | |
4567 | if (type != *restype_ptr) | |
4568 | { | |
4569 | minval = convert (*restype_ptr, minval); | |
4570 | maxval = convert (*restype_ptr, maxval); | |
4571 | } | |
4572 | ||
4573 | if (unsignedp && unsignedp0) | |
4574 | { | |
4575 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
4576 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
4577 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
4578 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
4579 | } | |
4580 | else | |
4581 | { | |
4582 | min_gt = INT_CST_LT (primop1, minval); | |
4583 | max_gt = INT_CST_LT (primop1, maxval); | |
4584 | min_lt = INT_CST_LT (minval, primop1); | |
4585 | max_lt = INT_CST_LT (maxval, primop1); | |
4586 | } | |
4587 | ||
4588 | val = 0; | |
4589 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
4590 | if (code == NE_EXPR) | |
4591 | { | |
4592 | if (max_lt || min_gt) | |
a360da3a | 4593 | val = boolean_true_node; |
b30f223b RS |
4594 | } |
4595 | else if (code == EQ_EXPR) | |
4596 | { | |
4597 | if (max_lt || min_gt) | |
a360da3a | 4598 | val = boolean_false_node; |
b30f223b RS |
4599 | } |
4600 | else if (code == LT_EXPR) | |
4601 | { | |
4602 | if (max_lt) | |
a360da3a | 4603 | val = boolean_true_node; |
b30f223b | 4604 | if (!min_lt) |
a360da3a | 4605 | val = boolean_false_node; |
b30f223b RS |
4606 | } |
4607 | else if (code == GT_EXPR) | |
4608 | { | |
4609 | if (min_gt) | |
a360da3a | 4610 | val = boolean_true_node; |
b30f223b | 4611 | if (!max_gt) |
a360da3a | 4612 | val = boolean_false_node; |
b30f223b RS |
4613 | } |
4614 | else if (code == LE_EXPR) | |
4615 | { | |
4616 | if (!max_gt) | |
a360da3a | 4617 | val = boolean_true_node; |
b30f223b | 4618 | if (min_gt) |
a360da3a | 4619 | val = boolean_false_node; |
b30f223b RS |
4620 | } |
4621 | else if (code == GE_EXPR) | |
4622 | { | |
4623 | if (!min_lt) | |
a360da3a | 4624 | val = boolean_true_node; |
b30f223b | 4625 | if (max_lt) |
a360da3a | 4626 | val = boolean_false_node; |
b30f223b RS |
4627 | } |
4628 | ||
4629 | /* If primop0 was sign-extended and unsigned comparison specd, | |
4630 | we did a signed comparison above using the signed type bounds. | |
4631 | But the comparison we output must be unsigned. | |
4632 | ||
4633 | Also, for inequalities, VAL is no good; but if the signed | |
4634 | comparison had *any* fixed result, it follows that the | |
4635 | unsigned comparison just tests the sign in reverse | |
4636 | (positive values are LE, negative ones GE). | |
4637 | So we can generate an unsigned comparison | |
4638 | against an extreme value of the signed type. */ | |
4639 | ||
4640 | if (unsignedp && !unsignedp0) | |
4641 | { | |
4642 | if (val != 0) | |
4643 | switch (code) | |
4644 | { | |
4645 | case LT_EXPR: | |
4646 | case GE_EXPR: | |
4647 | primop1 = TYPE_MIN_VALUE (type); | |
4648 | val = 0; | |
4649 | break; | |
4650 | ||
4651 | case LE_EXPR: | |
4652 | case GT_EXPR: | |
4653 | primop1 = TYPE_MAX_VALUE (type); | |
4654 | val = 0; | |
4655 | break; | |
6d819282 MK |
4656 | |
4657 | default: | |
4658 | break; | |
b30f223b RS |
4659 | } |
4660 | type = unsigned_type (type); | |
4661 | } | |
4662 | ||
b7c9c707 | 4663 | if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST) |
b30f223b RS |
4664 | { |
4665 | /* This is the case of (char)x >?< 0x80, which people used to use | |
4666 | expecting old C compilers to change the 0x80 into -0x80. */ | |
a360da3a | 4667 | if (val == boolean_false_node) |
07be2a23 | 4668 | warning ("comparison is always false due to limited range of data type"); |
a360da3a | 4669 | if (val == boolean_true_node) |
07be2a23 | 4670 | warning ("comparison is always true due to limited range of data type"); |
b30f223b RS |
4671 | } |
4672 | ||
b7c9c707 | 4673 | if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST) |
b30f223b | 4674 | { |
1e276c4a | 4675 | /* This is the case of (unsigned char)x >?< -1 or < 0. */ |
a360da3a | 4676 | if (val == boolean_false_node) |
07be2a23 | 4677 | warning ("comparison is always false due to limited range of data type"); |
a360da3a | 4678 | if (val == boolean_true_node) |
07be2a23 | 4679 | warning ("comparison is always true due to limited range of data type"); |
b30f223b RS |
4680 | } |
4681 | ||
4682 | if (val != 0) | |
4683 | { | |
4684 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4685 | if (TREE_SIDE_EFFECTS (primop0)) | |
4686 | return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); | |
4687 | return val; | |
4688 | } | |
4689 | ||
4690 | /* Value is not predetermined, but do the comparison | |
4691 | in the type of the operand that is not constant. | |
4692 | TYPE is already properly set. */ | |
4693 | } | |
4694 | else if (real1 && real2 | |
766f6c30 RS |
4695 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
4696 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b30f223b RS |
4697 | type = TREE_TYPE (primop0); |
4698 | ||
4699 | /* If args' natural types are both narrower than nominal type | |
4700 | and both extend in the same manner, compare them | |
4701 | in the type of the wider arg. | |
4702 | Otherwise must actually extend both to the nominal | |
4703 | common type lest different ways of extending | |
4704 | alter the result. | |
4705 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
4706 | ||
4707 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
4708 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
4709 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
4710 | { | |
4711 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4712 | type = signed_or_unsigned_type (unsignedp0 | |
4713 | || TREE_UNSIGNED (*restype_ptr), | |
4714 | type); | |
4715 | /* Make sure shorter operand is extended the right way | |
4716 | to match the longer operand. */ | |
4717 | primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)), | |
4718 | primop0); | |
4719 | primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)), | |
4720 | primop1); | |
4721 | } | |
4722 | else | |
4723 | { | |
4724 | /* Here we must do the comparison on the nominal type | |
4725 | using the args exactly as we received them. */ | |
4726 | type = *restype_ptr; | |
4727 | primop0 = op0; | |
4728 | primop1 = op1; | |
4729 | ||
4730 | if (!real1 && !real2 && integer_zerop (primop1) | |
597681f6 | 4731 | && TREE_UNSIGNED (*restype_ptr)) |
b30f223b RS |
4732 | { |
4733 | tree value = 0; | |
4734 | switch (code) | |
4735 | { | |
4736 | case GE_EXPR: | |
5af6001b RK |
4737 | /* All unsigned values are >= 0, so we warn if extra warnings |
4738 | are requested. However, if OP0 is a constant that is | |
4739 | >= 0, the signedness of the comparison isn't an issue, | |
4740 | so suppress the warning. */ | |
2c492eef | 4741 | if (extra_warnings && !in_system_header |
5af6001b RK |
4742 | && ! (TREE_CODE (primop0) == INTEGER_CST |
4743 | && ! TREE_OVERFLOW (convert (signed_type (type), | |
4744 | primop0)))) | |
07be2a23 | 4745 | warning ("comparison of unsigned expression >= 0 is always true"); |
a360da3a | 4746 | value = boolean_true_node; |
b30f223b RS |
4747 | break; |
4748 | ||
4749 | case LT_EXPR: | |
2c492eef | 4750 | if (extra_warnings && !in_system_header |
5af6001b RK |
4751 | && ! (TREE_CODE (primop0) == INTEGER_CST |
4752 | && ! TREE_OVERFLOW (convert (signed_type (type), | |
4753 | primop0)))) | |
07be2a23 | 4754 | warning ("comparison of unsigned expression < 0 is always false"); |
a360da3a | 4755 | value = boolean_false_node; |
6d819282 MK |
4756 | break; |
4757 | ||
4758 | default: | |
4759 | break; | |
b30f223b RS |
4760 | } |
4761 | ||
4762 | if (value != 0) | |
4763 | { | |
4764 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4765 | if (TREE_SIDE_EFFECTS (primop0)) | |
4766 | return build (COMPOUND_EXPR, TREE_TYPE (value), | |
4767 | primop0, value); | |
4768 | return value; | |
4769 | } | |
4770 | } | |
4771 | } | |
4772 | ||
4773 | *op0_ptr = convert (type, primop0); | |
4774 | *op1_ptr = convert (type, primop1); | |
4775 | ||
a360da3a | 4776 | *restype_ptr = boolean_type_node; |
b30f223b RS |
4777 | |
4778 | return 0; | |
4779 | } | |
4780 | \f | |
4781 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, | |
4782 | or validate its data type for an `if' or `while' statement or ?..: exp. | |
4783 | ||
4784 | This preparation consists of taking the ordinary | |
4785 | representation of an expression expr and producing a valid tree | |
4786 | boolean expression describing whether expr is nonzero. We could | |
a360da3a | 4787 | simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1), |
b30f223b RS |
4788 | but we optimize comparisons, &&, ||, and !. |
4789 | ||
a360da3a | 4790 | The resulting type should always be `boolean_type_node'. */ |
b30f223b RS |
4791 | |
4792 | tree | |
4793 | truthvalue_conversion (expr) | |
4794 | tree expr; | |
4795 | { | |
257e61ed RS |
4796 | if (TREE_CODE (expr) == ERROR_MARK) |
4797 | return expr; | |
4798 | ||
d7c83727 | 4799 | #if 0 /* This appears to be wrong for C++. */ |
257e61ed RS |
4800 | /* These really should return error_mark_node after 2.4 is stable. |
4801 | But not all callers handle ERROR_MARK properly. */ | |
4802 | switch (TREE_CODE (TREE_TYPE (expr))) | |
4803 | { | |
4804 | case RECORD_TYPE: | |
4805 | error ("struct type value used where scalar is required"); | |
a360da3a | 4806 | return boolean_false_node; |
257e61ed RS |
4807 | |
4808 | case UNION_TYPE: | |
4809 | error ("union type value used where scalar is required"); | |
a360da3a | 4810 | return boolean_false_node; |
257e61ed RS |
4811 | |
4812 | case ARRAY_TYPE: | |
4813 | error ("array type value used where scalar is required"); | |
a360da3a | 4814 | return boolean_false_node; |
257e61ed RS |
4815 | |
4816 | default: | |
4817 | break; | |
4818 | } | |
d7c83727 | 4819 | #endif /* 0 */ |
257e61ed | 4820 | |
b30f223b RS |
4821 | switch (TREE_CODE (expr)) |
4822 | { | |
b30f223b | 4823 | case EQ_EXPR: |
b30f223b RS |
4824 | case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
4825 | case TRUTH_ANDIF_EXPR: | |
4826 | case TRUTH_ORIF_EXPR: | |
4827 | case TRUTH_AND_EXPR: | |
4828 | case TRUTH_OR_EXPR: | |
9379fac9 | 4829 | case TRUTH_XOR_EXPR: |
1180eb10 | 4830 | case TRUTH_NOT_EXPR: |
a360da3a JM |
4831 | TREE_TYPE (expr) = boolean_type_node; |
4832 | return expr; | |
18c0f675 | 4833 | |
b30f223b RS |
4834 | case ERROR_MARK: |
4835 | return expr; | |
4836 | ||
4837 | case INTEGER_CST: | |
a360da3a | 4838 | return integer_zerop (expr) ? boolean_false_node : boolean_true_node; |
b30f223b RS |
4839 | |
4840 | case REAL_CST: | |
a360da3a | 4841 | return real_zerop (expr) ? boolean_false_node : boolean_true_node; |
b30f223b RS |
4842 | |
4843 | case ADDR_EXPR: | |
fc0c675f RK |
4844 | /* If we are taking the address of a external decl, it might be zero |
4845 | if it is weak, so we cannot optimize. */ | |
2f939d94 | 4846 | if (DECL_P (TREE_OPERAND (expr, 0)) |
fc0c675f RK |
4847 | && DECL_EXTERNAL (TREE_OPERAND (expr, 0))) |
4848 | break; | |
4849 | ||
b30f223b | 4850 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))) |
a360da3a JM |
4851 | return build (COMPOUND_EXPR, boolean_type_node, |
4852 | TREE_OPERAND (expr, 0), boolean_true_node); | |
b30f223b | 4853 | else |
a360da3a | 4854 | return boolean_true_node; |
b30f223b | 4855 | |
766f6c30 | 4856 | case COMPLEX_EXPR: |
f0b996c5 | 4857 | return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
b839fb3f | 4858 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
f0b996c5 RS |
4859 | truthvalue_conversion (TREE_OPERAND (expr, 0)), |
4860 | truthvalue_conversion (TREE_OPERAND (expr, 1)), | |
766f6c30 RS |
4861 | 0); |
4862 | ||
b30f223b RS |
4863 | case NEGATE_EXPR: |
4864 | case ABS_EXPR: | |
4865 | case FLOAT_EXPR: | |
4866 | case FFS_EXPR: | |
4867 | /* These don't change whether an object is non-zero or zero. */ | |
4868 | return truthvalue_conversion (TREE_OPERAND (expr, 0)); | |
4869 | ||
4870 | case LROTATE_EXPR: | |
4871 | case RROTATE_EXPR: | |
4872 | /* These don't change whether an object is zero or non-zero, but | |
4873 | we can't ignore them if their second arg has side-effects. */ | |
4874 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
a360da3a | 4875 | return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1), |
b30f223b RS |
4876 | truthvalue_conversion (TREE_OPERAND (expr, 0))); |
4877 | else | |
4878 | return truthvalue_conversion (TREE_OPERAND (expr, 0)); | |
b57062ca | 4879 | |
b30f223b RS |
4880 | case COND_EXPR: |
4881 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a360da3a | 4882 | return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0), |
b30f223b RS |
4883 | truthvalue_conversion (TREE_OPERAND (expr, 1)), |
4884 | truthvalue_conversion (TREE_OPERAND (expr, 2)))); | |
4885 | ||
4886 | case CONVERT_EXPR: | |
4887 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
4888 | since that affects how `default_conversion' will behave. */ | |
4889 | if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE | |
4890 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE) | |
4891 | break; | |
0f41302f | 4892 | /* fall through... */ |
b30f223b RS |
4893 | case NOP_EXPR: |
4894 | /* If this is widening the argument, we can ignore it. */ | |
4895 | if (TYPE_PRECISION (TREE_TYPE (expr)) | |
4896 | >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0)))) | |
4897 | return truthvalue_conversion (TREE_OPERAND (expr, 0)); | |
4898 | break; | |
4899 | ||
b30f223b | 4900 | case MINUS_EXPR: |
f87550e0 JW |
4901 | /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize |
4902 | this case. */ | |
4903 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT | |
4904 | && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE) | |
4905 | break; | |
0f41302f | 4906 | /* fall through... */ |
f87550e0 | 4907 | case BIT_XOR_EXPR: |
d7c83727 | 4908 | /* This and MINUS_EXPR can be changed into a comparison of the |
f87550e0 | 4909 | two objects. */ |
b30f223b RS |
4910 | if (TREE_TYPE (TREE_OPERAND (expr, 0)) |
4911 | == TREE_TYPE (TREE_OPERAND (expr, 1))) | |
4912 | return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0), | |
4913 | TREE_OPERAND (expr, 1), 1); | |
4914 | return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0), | |
4915 | fold (build1 (NOP_EXPR, | |
4916 | TREE_TYPE (TREE_OPERAND (expr, 0)), | |
4917 | TREE_OPERAND (expr, 1))), 1); | |
e2aab13d | 4918 | |
fb48b1f0 | 4919 | case BIT_AND_EXPR: |
58cee643 RK |
4920 | if (integer_onep (TREE_OPERAND (expr, 1)) |
4921 | && TREE_TYPE (expr) != boolean_type_node) | |
4922 | /* Using convert here would cause infinite recursion. */ | |
4923 | return build1 (NOP_EXPR, boolean_type_node, expr); | |
4924 | break; | |
fb48b1f0 | 4925 | |
e2aab13d RS |
4926 | case MODIFY_EXPR: |
4927 | if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR) | |
4928 | warning ("suggest parentheses around assignment used as truth value"); | |
4929 | break; | |
b57062ca | 4930 | |
6d819282 MK |
4931 | default: |
4932 | break; | |
b30f223b RS |
4933 | } |
4934 | ||
f0b996c5 | 4935 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
f0b8d9aa AS |
4936 | { |
4937 | tree tem = save_expr (expr); | |
4938 | return (build_binary_op | |
4939 | ((TREE_SIDE_EFFECTS (expr) | |
4940 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), | |
4941 | truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)), | |
4942 | truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)), | |
4943 | 0)); | |
4944 | } | |
f0b996c5 | 4945 | |
b30f223b RS |
4946 | return build_binary_op (NE_EXPR, expr, integer_zero_node, 1); |
4947 | } | |
4948 | \f | |
fc2aaf30 JM |
4949 | static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree, |
4950 | int, enum built_in_class, int, int, | |
4951 | int)); | |
4952 | ||
0b73773c NH |
4953 | /* Make a variant type in the proper way for C/C++, propagating qualifiers |
4954 | down to the element type of an array. */ | |
4955 | ||
4956 | tree | |
3932261a | 4957 | c_build_qualified_type (type, type_quals) |
0b73773c | 4958 | tree type; |
3932261a | 4959 | int type_quals; |
0b73773c | 4960 | { |
3932261a MM |
4961 | /* A restrict-qualified pointer type must be a pointer to object or |
4962 | incomplete type. Note that the use of POINTER_TYPE_P also allows | |
4963 | REFERENCE_TYPEs, which is appropriate for C++. Unfortunately, | |
4964 | the C++ front-end also use POINTER_TYPE for pointer-to-member | |
4965 | values, so even though it should be illegal to use `restrict' | |
4966 | with such an entity we don't flag that here. Thus, special case | |
4967 | code for that case is required in the C++ front-end. */ | |
4968 | if ((type_quals & TYPE_QUAL_RESTRICT) | |
4969 | && (!POINTER_TYPE_P (type) | |
4970 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))) | |
4971 | { | |
4972 | error ("invalid use of `restrict'"); | |
4973 | type_quals &= ~TYPE_QUAL_RESTRICT; | |
4974 | } | |
4975 | ||
0b73773c | 4976 | if (TREE_CODE (type) == ARRAY_TYPE) |
3932261a MM |
4977 | return build_array_type (c_build_qualified_type (TREE_TYPE (type), |
4978 | type_quals), | |
3ab1999b | 4979 | TYPE_DOMAIN (type)); |
3932261a MM |
4980 | return build_qualified_type (type, type_quals); |
4981 | } | |
4982 | ||
4983 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4984 | ||
4985 | void | |
4986 | c_apply_type_quals_to_decl (type_quals, decl) | |
4987 | int type_quals; | |
4988 | tree decl; | |
4989 | { | |
a6496605 MM |
4990 | if ((type_quals & TYPE_QUAL_CONST) |
4991 | || (TREE_TYPE (decl) | |
4992 | && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)) | |
3932261a MM |
4993 | TREE_READONLY (decl) = 1; |
4994 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4995 | { | |
4996 | TREE_SIDE_EFFECTS (decl) = 1; | |
4997 | TREE_THIS_VOLATILE (decl) = 1; | |
4998 | } | |
6946bc60 | 4999 | if (type_quals & TYPE_QUAL_RESTRICT) |
3932261a | 5000 | { |
6946bc60 MM |
5001 | if (!TREE_TYPE (decl) |
5002 | || !POINTER_TYPE_P (TREE_TYPE (decl)) | |
5003 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl)))) | |
5004 | error ("invalid use of `restrict'"); | |
5005 | else if (flag_strict_aliasing) | |
3932261a | 5006 | { |
6946bc60 MM |
5007 | /* No two restricted pointers can point at the same thing. |
5008 | However, a restricted pointer can point at the same thing | |
5009 | as an unrestricted pointer, if that unrestricted pointer | |
5010 | is based on the restricted pointer. So, we make the | |
5011 | alias set for the restricted pointer a subset of the | |
5012 | alias set for the type pointed to by the type of the | |
5013 | decl. */ | |
5014 | ||
3bdf5ad1 | 5015 | HOST_WIDE_INT pointed_to_alias_set |
6946bc60 | 5016 | = get_alias_set (TREE_TYPE (TREE_TYPE (decl))); |
d125d268 | 5017 | |
3bdf5ad1 | 5018 | if (pointed_to_alias_set == 0) |
6946bc60 MM |
5019 | /* It's not legal to make a subset of alias set zero. */ |
5020 | ; | |
5021 | else | |
5022 | { | |
5023 | DECL_POINTER_ALIAS_SET (decl) = new_alias_set (); | |
5024 | record_alias_subset (pointed_to_alias_set, | |
5025 | DECL_POINTER_ALIAS_SET (decl)); | |
5026 | } | |
3932261a MM |
5027 | } |
5028 | } | |
5029 | } | |
5030 | ||
41472af8 MM |
5031 | |
5032 | /* Return the typed-based alias set for T, which may be an expression | |
3bdf5ad1 | 5033 | or a type. Return -1 if we don't do anything special. */ |
41472af8 | 5034 | |
3bdf5ad1 | 5035 | HOST_WIDE_INT |
2e761e49 | 5036 | lang_get_alias_set (t) |
41472af8 MM |
5037 | tree t; |
5038 | { | |
08bc2431 | 5039 | tree u; |
604bb87d DB |
5040 | |
5041 | /* We know nothing about vector types */ | |
5042 | if (TREE_CODE (t) == VECTOR_TYPE) | |
5043 | return 0; | |
5044 | ||
08bc2431 MM |
5045 | /* Permit type-punning when accessing a union, provided the access |
5046 | is directly through the union. For example, this code does not | |
5047 | permit taking the address of a union member and then storing | |
5048 | through it. Even the type-punning allowed here is a GCC | |
5049 | extension, albeit a common and useful one; the C standard says | |
5050 | that such accesses have implementation-defined behavior. */ | |
5051 | for (u = t; | |
5052 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
5053 | u = TREE_OPERAND (u, 0)) | |
5054 | if (TREE_CODE (u) == COMPONENT_REF | |
5055 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
5056 | return 0; | |
ece32014 | 5057 | |
3bdf5ad1 | 5058 | /* If this is a char *, the ANSI C standard says it can alias |
f824e5c3 RK |
5059 | anything. Note that all references need do this. */ |
5060 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r' | |
5061 | && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE | |
5062 | && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node)) | |
3bdf5ad1 | 5063 | return 0; |
3932261a | 5064 | |
3bdf5ad1 RK |
5065 | /* That's all the expressions we handle specially. */ |
5066 | if (! TYPE_P (t)) | |
5067 | return -1; | |
41472af8 | 5068 | |
f824e5c3 RK |
5069 | /* The C standard specifically allows aliasing between signed and |
5070 | unsigned variants of the same type. We treat the signed | |
5071 | variant as canonical. */ | |
5072 | if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t)) | |
8f215dce JJ |
5073 | { |
5074 | tree t1 = signed_type (t); | |
f824e5c3 | 5075 | |
8f215dce JJ |
5076 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
5077 | if (t1 != t) | |
5078 | return get_alias_set (t1); | |
5079 | } | |
3bdf5ad1 | 5080 | else if (POINTER_TYPE_P (t)) |
02af3af6 | 5081 | { |
3bdf5ad1 | 5082 | tree t1; |
02af3af6 MS |
5083 | |
5084 | /* Unfortunately, there is no canonical form of a pointer type. | |
5085 | In particular, if we have `typedef int I', then `int *', and | |
5086 | `I *' are different types. So, we have to pick a canonical | |
5087 | representative. We do this below. | |
d125d268 | 5088 | |
b61148dd MM |
5089 | Technically, this approach is actually more conservative that |
5090 | it needs to be. In particular, `const int *' and `int *' | |
5091 | chould be in different alias sets, according to the C and C++ | |
5092 | standard, since their types are not the same, and so, | |
5093 | technically, an `int **' and `const int **' cannot point at | |
5094 | the same thing. | |
5095 | ||
5096 | But, the standard is wrong. In particular, this code is | |
5097 | legal C++: | |
5098 | ||
5099 | int *ip; | |
5100 | int **ipp = &ip; | |
5101 | const int* const* cipp = &ip; | |
5102 | ||
5103 | And, it doesn't make sense for that to be legal unless you | |
5104 | can dereference IPP and CIPP. So, we ignore cv-qualifiers on | |
5105 | the pointed-to types. This issue has been reported to the | |
5106 | C++ committee. */ | |
12e1243e | 5107 | t1 = build_type_no_quals (t); |
3bdf5ad1 RK |
5108 | if (t1 != t) |
5109 | return get_alias_set (t1); | |
02af3af6 | 5110 | } |
5b2abab8 MM |
5111 | /* It's not yet safe to use alias sets for classes in C++ because |
5112 | the TYPE_FIELDs list for a class doesn't mention base classes. */ | |
f6f8291a | 5113 | else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t)) |
5b2abab8 | 5114 | return 0; |
ece32014 | 5115 | |
3bdf5ad1 | 5116 | return -1; |
41472af8 | 5117 | } |
7f4edbcb BS |
5118 | |
5119 | /* Build tree nodes and builtin functions common to both C and C++ language | |
6bcedb4e | 5120 | frontends. */ |
3bdf5ad1 | 5121 | |
7f4edbcb | 5122 | void |
6bcedb4e | 5123 | c_common_nodes_and_builtins () |
7f4edbcb | 5124 | { |
eaa7c03f JM |
5125 | int wchar_type_size; |
5126 | tree array_domain_type; | |
7f4edbcb BS |
5127 | tree temp; |
5128 | tree memcpy_ftype, memset_ftype, strlen_ftype; | |
ad3fd36f | 5129 | tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype; |
c2e42aab | 5130 | tree fputs_ftype, fputc_ftype, fwrite_ftype; |
7f4edbcb | 5131 | tree endlink, int_endlink, double_endlink, unsigned_endlink; |
60291474 | 5132 | tree cstring_endlink, sizetype_endlink; |
7f4edbcb | 5133 | tree ptr_ftype, ptr_ftype_unsigned; |
c2e42aab | 5134 | tree void_ftype_any, void_ftype_int, int_ftype_any; |
7f4edbcb BS |
5135 | tree double_ftype_double, double_ftype_double_double; |
5136 | tree float_ftype_float, ldouble_ftype_ldouble; | |
d118937d | 5137 | tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring; |
60291474 KG |
5138 | tree int_ftype_cstring_cstring, string_ftype_string_cstring; |
5139 | tree string_ftype_cstring_int, string_ftype_cstring_cstring; | |
da9e9f08 | 5140 | tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet; |
7f4edbcb | 5141 | tree long_ftype_long; |
1c44c45d | 5142 | tree longlong_ftype_longlong; |
e78f4a97 | 5143 | tree intmax_ftype_intmax; |
7f4edbcb BS |
5144 | /* Either char* or void*. */ |
5145 | tree traditional_ptr_type_node; | |
4b2a62db KG |
5146 | /* Either const char* or const void*. */ |
5147 | tree traditional_cptr_type_node; | |
5148 | tree traditional_len_type_node; | |
5149 | tree traditional_len_endlink; | |
9f720c3e | 5150 | tree va_list_ref_type_node; |
daf68dd7 | 5151 | tree va_list_arg_type_node; |
d3707adb | 5152 | |
eaa7c03f JM |
5153 | /* Define `int' and `char' first so that dbx will output them first. */ |
5154 | record_builtin_type (RID_INT, NULL_PTR, integer_type_node); | |
5155 | record_builtin_type (RID_CHAR, "char", char_type_node); | |
5156 | ||
5157 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
5158 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
5159 | but not C. Are the conditionals here needed? */ | |
5160 | if (c_language == clk_cplusplus) | |
5161 | record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node); | |
5162 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); | |
5163 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
5164 | record_builtin_type (RID_MAX, "long unsigned int", | |
5165 | long_unsigned_type_node); | |
5166 | if (c_language == clk_cplusplus) | |
5167 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); | |
5168 | record_builtin_type (RID_MAX, "long long int", | |
5169 | long_long_integer_type_node); | |
5170 | record_builtin_type (RID_MAX, "long long unsigned int", | |
5171 | long_long_unsigned_type_node); | |
5172 | if (c_language == clk_cplusplus) | |
5173 | record_builtin_type (RID_MAX, "long long unsigned", | |
5174 | long_long_unsigned_type_node); | |
5175 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
5176 | record_builtin_type (RID_MAX, "short unsigned int", | |
5177 | short_unsigned_type_node); | |
5178 | if (c_language == clk_cplusplus) | |
5179 | record_builtin_type (RID_MAX, "unsigned short", | |
5180 | short_unsigned_type_node); | |
5181 | ||
5182 | /* Define both `signed char' and `unsigned char'. */ | |
5183 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
5184 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
5185 | ||
5186 | /* These are types that type_for_size and type_for_mode use. */ | |
5187 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node)); | |
5188 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node)); | |
5189 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node)); | |
5190 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node)); | |
5191 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
5192 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node)); | |
5193 | #endif | |
5194 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node)); | |
5195 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node)); | |
5196 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node)); | |
5197 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node)); | |
5198 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
5199 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node)); | |
5200 | #endif | |
5201 | ||
5202 | /* Create the widest literal types. */ | |
5203 | widest_integer_literal_type_node | |
5204 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
5205 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
5206 | widest_integer_literal_type_node)); | |
5207 | ||
5208 | widest_unsigned_literal_type_node | |
5209 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
5210 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, | |
5211 | widest_unsigned_literal_type_node)); | |
5212 | ||
5213 | /* `unsigned long' is the standard type for sizeof. | |
5214 | Note that stddef.h uses `unsigned long', | |
5215 | and this must agree, even if long and int are the same size. */ | |
5216 | c_size_type_node = | |
5217 | TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE))); | |
5218 | signed_size_type_node = signed_type (c_size_type_node); | |
5219 | if (flag_traditional) | |
5220 | c_size_type_node = signed_size_type_node; | |
5221 | set_sizetype (c_size_type_node); | |
5222 | ||
5223 | build_common_tree_nodes_2 (flag_short_double); | |
5224 | ||
5225 | record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node); | |
5226 | record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node); | |
5227 | record_builtin_type (RID_MAX, "long double", long_double_type_node); | |
5228 | ||
5229 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"), | |
5230 | complex_integer_type_node)); | |
5231 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"), | |
5232 | complex_float_type_node)); | |
5233 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"), | |
5234 | complex_double_type_node)); | |
5235 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"), | |
5236 | complex_long_double_type_node)); | |
5237 | ||
5238 | record_builtin_type (RID_VOID, NULL_PTR, void_type_node); | |
5239 | ||
5240 | void_list_node = build_void_list_node (); | |
5241 | ||
5242 | /* Make a type to be the domain of a few array types | |
5243 | whose domains don't really matter. | |
5244 | 200 is small enough that it always fits in size_t | |
5245 | and large enough that it can hold most function names for the | |
5246 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
5247 | array_domain_type = build_index_type (size_int (200)); | |
5248 | ||
5249 | /* Make a type for arrays of characters. | |
5250 | With luck nothing will ever really depend on the length of this | |
5251 | array type. */ | |
5252 | char_array_type_node | |
5253 | = build_array_type (char_type_node, array_domain_type); | |
5254 | ||
5255 | /* Likewise for arrays of ints. */ | |
5256 | int_array_type_node | |
5257 | = build_array_type (integer_type_node, array_domain_type); | |
5258 | ||
5259 | #ifdef MD_INIT_BUILTINS | |
5260 | MD_INIT_BUILTINS; | |
5261 | #endif | |
5262 | ||
5263 | /* This is special for C++ so functions can be overloaded. */ | |
5264 | wchar_type_node = get_identifier (flag_short_wchar | |
5265 | ? "short unsigned int" | |
5266 | : WCHAR_TYPE); | |
5267 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); | |
5268 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
5269 | if (c_language == clk_cplusplus) | |
5270 | { | |
5271 | if (TREE_UNSIGNED (wchar_type_node)) | |
5272 | wchar_type_node = make_unsigned_type (wchar_type_size); | |
5273 | else | |
5274 | wchar_type_node = make_signed_type (wchar_type_size); | |
5275 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
5276 | } | |
5277 | else | |
5278 | { | |
5279 | signed_wchar_type_node = signed_type (wchar_type_node); | |
5280 | unsigned_wchar_type_node = unsigned_type (wchar_type_node); | |
5281 | } | |
5282 | ||
5283 | /* This is for wide string constants. */ | |
5284 | wchar_array_type_node | |
5285 | = build_array_type (wchar_type_node, array_domain_type); | |
5286 | ||
5fd8e536 JM |
5287 | string_type_node = build_pointer_type (char_type_node); |
5288 | const_string_type_node | |
5289 | = build_pointer_type (build_type_variant (char_type_node, 1, 0)); | |
5290 | ||
5291 | wint_type_node = | |
5292 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
5293 | ||
5294 | intmax_type_node = | |
5295 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
5296 | uintmax_type_node = | |
5297 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
5298 | ||
5299 | default_function_type = build_function_type (integer_type_node, NULL_TREE); | |
5300 | ptrdiff_type_node | |
5301 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
5302 | unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node); | |
5303 | ||
d3707adb RH |
5304 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"), |
5305 | va_list_type_node)); | |
daf68dd7 | 5306 | |
29ae8f10 GRK |
5307 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"), |
5308 | ptrdiff_type_node)); | |
5309 | ||
5310 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"), | |
5311 | sizetype)); | |
5312 | ||
daf68dd7 | 5313 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
9f720c3e GK |
5314 | { |
5315 | va_list_arg_type_node = va_list_ref_type_node = | |
5316 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
5317 | } | |
daf68dd7 | 5318 | else |
9f720c3e GK |
5319 | { |
5320 | va_list_arg_type_node = va_list_type_node; | |
5321 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
5322 | } | |
5323 | ||
7f4edbcb BS |
5324 | endlink = void_list_node; |
5325 | int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink); | |
5326 | double_endlink = tree_cons (NULL_TREE, double_type_node, endlink); | |
5327 | unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink); | |
60291474 | 5328 | cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink); |
7f4edbcb BS |
5329 | |
5330 | ptr_ftype = build_function_type (ptr_type_node, NULL_TREE); | |
5331 | ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink); | |
21318741 | 5332 | sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink); |
7f4edbcb BS |
5333 | /* We realloc here because sizetype could be int or unsigned. S'ok. */ |
5334 | ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink); | |
5335 | ||
4b2a62db | 5336 | int_ftype_any = build_function_type (integer_type_node, NULL_TREE); |
7f4edbcb BS |
5337 | void_ftype_any = build_function_type (void_type_node, NULL_TREE); |
5338 | void_ftype = build_function_type (void_type_node, endlink); | |
5339 | void_ftype_int = build_function_type (void_type_node, int_endlink); | |
5340 | void_ftype_ptr | |
5341 | = build_function_type (void_type_node, | |
5342 | tree_cons (NULL_TREE, ptr_type_node, endlink)); | |
5343 | ||
5344 | float_ftype_float | |
5345 | = build_function_type (float_type_node, | |
5346 | tree_cons (NULL_TREE, float_type_node, endlink)); | |
5347 | ||
5348 | double_ftype_double | |
5349 | = build_function_type (double_type_node, double_endlink); | |
5350 | ||
5351 | ldouble_ftype_ldouble | |
5352 | = build_function_type (long_double_type_node, | |
5353 | tree_cons (NULL_TREE, long_double_type_node, | |
5354 | endlink)); | |
5355 | ||
5356 | double_ftype_double_double | |
5357 | = build_function_type (double_type_node, | |
5358 | tree_cons (NULL_TREE, double_type_node, | |
5359 | double_endlink)); | |
5360 | ||
5361 | int_ftype_int | |
5362 | = build_function_type (integer_type_node, int_endlink); | |
5363 | ||
5364 | long_ftype_long | |
5365 | = build_function_type (long_integer_type_node, | |
5366 | tree_cons (NULL_TREE, long_integer_type_node, | |
5367 | endlink)); | |
5368 | ||
1c44c45d JM |
5369 | longlong_ftype_longlong |
5370 | = build_function_type (long_long_integer_type_node, | |
5371 | tree_cons (NULL_TREE, long_long_integer_type_node, | |
5372 | endlink)); | |
5373 | ||
e78f4a97 JM |
5374 | intmax_ftype_intmax |
5375 | = build_function_type (intmax_type_node, | |
5376 | tree_cons (NULL_TREE, intmax_type_node, | |
5377 | endlink)); | |
5378 | ||
7f4edbcb BS |
5379 | int_ftype_cptr_cptr_sizet |
5380 | = build_function_type (integer_type_node, | |
5381 | tree_cons (NULL_TREE, const_ptr_type_node, | |
60291474 KG |
5382 | tree_cons (NULL_TREE, |
5383 | const_ptr_type_node, | |
5384 | sizetype_endlink))); | |
7f4edbcb | 5385 | |
ae499cce MM |
5386 | void_zero_node = build_int_2 (0, 0); |
5387 | TREE_TYPE (void_zero_node) = void_type_node; | |
5388 | ||
d118937d KG |
5389 | /* Prototype for strcpy/strcat. */ |
5390 | string_ftype_string_cstring | |
7f4edbcb BS |
5391 | = build_function_type (string_type_node, |
5392 | tree_cons (NULL_TREE, string_type_node, | |
60291474 | 5393 | cstring_endlink)); |
7f4edbcb | 5394 | |
d118937d | 5395 | /* Prototype for strncpy/strncat. */ |
da9e9f08 KG |
5396 | string_ftype_string_cstring_sizet |
5397 | = build_function_type (string_type_node, | |
5398 | tree_cons (NULL_TREE, string_type_node, | |
5399 | tree_cons (NULL_TREE, | |
5400 | const_string_type_node, | |
5401 | sizetype_endlink))); | |
5402 | ||
6bcedb4e MM |
5403 | traditional_len_type_node = ((flag_traditional && |
5404 | c_language != clk_cplusplus) | |
4b2a62db KG |
5405 | ? integer_type_node : sizetype); |
5406 | traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node, | |
5407 | endlink); | |
5408 | ||
7f4edbcb | 5409 | /* Prototype for strcmp. */ |
60291474 | 5410 | int_ftype_cstring_cstring |
7f4edbcb BS |
5411 | = build_function_type (integer_type_node, |
5412 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 5413 | cstring_endlink)); |
7f4edbcb | 5414 | |
d118937d KG |
5415 | /* Prototype for strspn/strcspn. */ |
5416 | sizet_ftype_cstring_cstring | |
5417 | = build_function_type (c_size_type_node, | |
5418 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 5419 | cstring_endlink)); |
d118937d | 5420 | |
da9e9f08 KG |
5421 | /* Prototype for strncmp. */ |
5422 | int_ftype_cstring_cstring_sizet | |
5423 | = build_function_type (integer_type_node, | |
5424 | tree_cons (NULL_TREE, const_string_type_node, | |
5425 | tree_cons (NULL_TREE, | |
5426 | const_string_type_node, | |
5427 | sizetype_endlink))); | |
5428 | ||
26a87cab | 5429 | /* Prototype for strstr, strpbrk, etc. */ |
60291474 | 5430 | string_ftype_cstring_cstring |
78e7629e KG |
5431 | = build_function_type (string_type_node, |
5432 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 5433 | cstring_endlink)); |
78e7629e KG |
5434 | |
5435 | /* Prototype for strchr. */ | |
60291474 | 5436 | string_ftype_cstring_int |
78e7629e KG |
5437 | = build_function_type (string_type_node, |
5438 | tree_cons (NULL_TREE, const_string_type_node, | |
60291474 | 5439 | int_endlink)); |
78e7629e | 5440 | |
7f4edbcb BS |
5441 | /* Prototype for strlen. */ |
5442 | strlen_ftype | |
60291474 | 5443 | = build_function_type (traditional_len_type_node, cstring_endlink); |
7f4edbcb | 5444 | |
6bcedb4e MM |
5445 | traditional_ptr_type_node = ((flag_traditional && |
5446 | c_language != clk_cplusplus) | |
7f4edbcb | 5447 | ? string_type_node : ptr_type_node); |
6bcedb4e MM |
5448 | traditional_cptr_type_node = ((flag_traditional && |
5449 | c_language != clk_cplusplus) | |
4b2a62db | 5450 | ? const_string_type_node : const_ptr_type_node); |
7f4edbcb BS |
5451 | |
5452 | /* Prototype for memcpy. */ | |
5453 | memcpy_ftype | |
5454 | = build_function_type (traditional_ptr_type_node, | |
5455 | tree_cons (NULL_TREE, ptr_type_node, | |
5456 | tree_cons (NULL_TREE, const_ptr_type_node, | |
5457 | sizetype_endlink))); | |
5458 | ||
5459 | /* Prototype for memset. */ | |
5460 | memset_ftype | |
5461 | = build_function_type (traditional_ptr_type_node, | |
5462 | tree_cons (NULL_TREE, ptr_type_node, | |
5463 | tree_cons (NULL_TREE, integer_type_node, | |
60291474 | 5464 | sizetype_endlink))); |
7f4edbcb | 5465 | |
e3a709be KG |
5466 | /* Prototype for bzero. */ |
5467 | bzero_ftype | |
5468 | = build_function_type (void_type_node, | |
5469 | tree_cons (NULL_TREE, traditional_ptr_type_node, | |
5470 | traditional_len_endlink)); | |
5471 | ||
4b2a62db KG |
5472 | /* Prototype for bcmp. */ |
5473 | bcmp_ftype | |
5474 | = build_function_type (integer_type_node, | |
5475 | tree_cons (NULL_TREE, traditional_cptr_type_node, | |
5476 | tree_cons (NULL_TREE, | |
5477 | traditional_cptr_type_node, | |
5478 | traditional_len_endlink))); | |
5479 | ||
ad3fd36f KG |
5480 | /* Prototype for puts. */ |
5481 | puts_ftype | |
60291474 | 5482 | = build_function_type (integer_type_node, cstring_endlink); |
ad3fd36f KG |
5483 | |
5484 | /* Prototype for printf. */ | |
5485 | printf_ftype | |
5486 | = build_function_type (integer_type_node, | |
5487 | tree_cons (NULL_TREE, const_string_type_node, | |
5488 | NULL_TREE)); | |
5489 | ||
c2e42aab KG |
5490 | /* These stdio prototypes are declared using void* in place of |
5491 | FILE*. They are only used for __builtin_ style calls, regular | |
5492 | style builtin prototypes omit the arguments and merge those | |
5493 | provided by stdio.h. */ | |
5494 | /* Prototype for fwrite. */ | |
5495 | fwrite_ftype | |
5496 | = build_function_type (c_size_type_node, | |
5497 | tree_cons (NULL_TREE, const_ptr_type_node, | |
5498 | tree_cons (NULL_TREE, c_size_type_node, | |
5499 | tree_cons (NULL_TREE, c_size_type_node, | |
5500 | tree_cons (NULL_TREE, ptr_type_node, endlink))))); | |
5501 | ||
5502 | /* Prototype for fputc. */ | |
5503 | fputc_ftype | |
5504 | = build_function_type (integer_type_node, | |
5505 | tree_cons (NULL_TREE, integer_type_node, | |
5506 | tree_cons (NULL_TREE, ptr_type_node, endlink))); | |
5507 | ||
5508 | /* Prototype for fputs. */ | |
5509 | fputs_ftype | |
5510 | = build_function_type (integer_type_node, | |
5511 | tree_cons (NULL_TREE, const_string_type_node, | |
5512 | tree_cons (NULL_TREE, ptr_type_node, endlink))); | |
5513 | ||
7f4edbcb | 5514 | builtin_function ("__builtin_constant_p", default_function_type, |
26db82d8 | 5515 | BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
5516 | |
5517 | builtin_function ("__builtin_return_address", ptr_ftype_unsigned, | |
26db82d8 | 5518 | BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
5519 | |
5520 | builtin_function ("__builtin_frame_address", ptr_ftype_unsigned, | |
26db82d8 | 5521 | BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
5522 | |
5523 | builtin_function ("__builtin_alloca", ptr_ftype_sizetype, | |
26db82d8 | 5524 | BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca"); |
fc2aaf30 JM |
5525 | builtin_function_2 ("__builtin_ffs", "ffs", |
5526 | int_ftype_int, int_ftype_int, | |
5527 | BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0); | |
5528 | /* Define alloca as builtin, unless SMALL_STACK. */ | |
512b62fb | 5529 | #ifndef SMALL_STACK |
fc2aaf30 JM |
5530 | builtin_function_2 (NULL_PTR, "alloca", NULL_TREE, ptr_ftype_sizetype, |
5531 | BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0); | |
512b62fb | 5532 | #endif |
fc2aaf30 JM |
5533 | /* Declare _exit just to mark it as non-returning. */ |
5534 | builtin_function_2 (NULL_PTR, "_exit", NULL_TREE, void_ftype_int, | |
5535 | 0, NOT_BUILT_IN, 0, 1, 1); | |
5536 | ||
5537 | builtin_function_2 ("__builtin_index", "index", | |
5538 | string_ftype_cstring_int, string_ftype_cstring_int, | |
5539 | BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0); | |
5540 | builtin_function_2 ("__builtin_rindex", "rindex", | |
5541 | string_ftype_cstring_int, string_ftype_cstring_int, | |
5542 | BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0); | |
5543 | ||
5544 | /* The system prototypes for these functions have many | |
5545 | variations, so don't specify parameters to avoid conflicts. | |
5546 | The expand_* functions check the argument types anyway. */ | |
5547 | builtin_function_2 ("__builtin_bzero", "bzero", | |
5548 | bzero_ftype, void_ftype_any, | |
5549 | BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0); | |
5550 | builtin_function_2 ("__builtin_bcmp", "bcmp", | |
5551 | bcmp_ftype, int_ftype_any, | |
5552 | BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0); | |
5553 | ||
5554 | builtin_function_2 ("__builtin_abs", "abs", | |
5555 | int_ftype_int, int_ftype_int, | |
5556 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0); | |
5557 | builtin_function_2 ("__builtin_fabsf", "fabsf", | |
5558 | float_ftype_float, float_ftype_float, | |
5559 | BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0); | |
5560 | builtin_function_2 ("__builtin_fabs", "fabs", | |
5561 | double_ftype_double, double_ftype_double, | |
5562 | BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0); | |
5563 | builtin_function_2 ("__builtin_fabsl", "fabsl", | |
5564 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
5565 | BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0); | |
5566 | builtin_function_2 ("__builtin_labs", "labs", | |
5567 | long_ftype_long, long_ftype_long, | |
29b5ca41 | 5568 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0); |
fc2aaf30 JM |
5569 | builtin_function_2 ("__builtin_llabs", "llabs", |
5570 | longlong_ftype_longlong, longlong_ftype_longlong, | |
29b5ca41 | 5571 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); |
fc2aaf30 JM |
5572 | builtin_function_2 ("__builtin_imaxabs", "imaxabs", |
5573 | intmax_ftype_intmax, intmax_ftype_intmax, | |
29b5ca41 | 5574 | BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0); |
7f4edbcb | 5575 | |
7f4edbcb | 5576 | builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS, |
26db82d8 | 5577 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5578 | builtin_function ("__builtin_classify_type", default_function_type, |
26db82d8 | 5579 | BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5580 | builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG, |
26db82d8 | 5581 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5582 | builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO, |
26db82d8 | 5583 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
5584 | builtin_function ("__builtin_setjmp", |
5585 | build_function_type (integer_type_node, | |
5586 | tree_cons (NULL_TREE, ptr_type_node, | |
5587 | endlink)), | |
26db82d8 | 5588 | BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
5589 | builtin_function ("__builtin_longjmp", |
5590 | build_function_type (void_type_node, | |
5591 | tree_cons (NULL_TREE, ptr_type_node, | |
60291474 | 5592 | int_endlink)), |
26db82d8 BS |
5593 | BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR); |
5594 | builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP, | |
5595 | BUILT_IN_NORMAL, NULL_PTR); | |
7f4edbcb | 5596 | |
1eb8759b RH |
5597 | /* ISO C99 IEEE Unordered compares. */ |
5598 | builtin_function ("__builtin_isgreater", default_function_type, | |
5599 | BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR); | |
5600 | builtin_function ("__builtin_isgreaterequal", default_function_type, | |
5601 | BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR); | |
5602 | builtin_function ("__builtin_isless", default_function_type, | |
5603 | BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR); | |
5604 | builtin_function ("__builtin_islessequal", default_function_type, | |
5605 | BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR); | |
5606 | builtin_function ("__builtin_islessgreater", default_function_type, | |
5607 | BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR); | |
5608 | builtin_function ("__builtin_isunordered", default_function_type, | |
5609 | BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR); | |
5610 | ||
7f4edbcb BS |
5611 | /* Untyped call and return. */ |
5612 | builtin_function ("__builtin_apply_args", ptr_ftype, | |
26db82d8 | 5613 | BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb BS |
5614 | |
5615 | temp = tree_cons (NULL_TREE, | |
5616 | build_pointer_type (build_function_type (void_type_node, | |
5617 | NULL_TREE)), | |
60291474 | 5618 | tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink)); |
7f4edbcb BS |
5619 | builtin_function ("__builtin_apply", |
5620 | build_function_type (ptr_type_node, temp), | |
26db82d8 | 5621 | BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5622 | builtin_function ("__builtin_return", void_ftype_ptr, |
26db82d8 | 5623 | BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5624 | |
d3707adb RH |
5625 | /* Support for varargs.h and stdarg.h. */ |
5626 | builtin_function ("__builtin_varargs_start", | |
5627 | build_function_type (void_type_node, | |
5628 | tree_cons (NULL_TREE, | |
9f720c3e | 5629 | va_list_ref_type_node, |
d3707adb | 5630 | endlink)), |
26db82d8 | 5631 | BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb RH |
5632 | |
5633 | builtin_function ("__builtin_stdarg_start", | |
5634 | build_function_type (void_type_node, | |
5635 | tree_cons (NULL_TREE, | |
9f720c3e | 5636 | va_list_ref_type_node, |
d3707adb | 5637 | NULL_TREE)), |
26db82d8 | 5638 | BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb RH |
5639 | |
5640 | builtin_function ("__builtin_va_end", | |
5641 | build_function_type (void_type_node, | |
5642 | tree_cons (NULL_TREE, | |
9f720c3e | 5643 | va_list_ref_type_node, |
d3707adb | 5644 | endlink)), |
26db82d8 | 5645 | BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb RH |
5646 | |
5647 | builtin_function ("__builtin_va_copy", | |
5648 | build_function_type (void_type_node, | |
5649 | tree_cons (NULL_TREE, | |
9f720c3e | 5650 | va_list_ref_type_node, |
d3707adb | 5651 | tree_cons (NULL_TREE, |
daf68dd7 | 5652 | va_list_arg_type_node, |
d3707adb | 5653 | endlink))), |
26db82d8 | 5654 | BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR); |
d3707adb | 5655 | |
994a57cd RH |
5656 | /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */ |
5657 | builtin_function ("__builtin_expect", | |
5658 | build_function_type (long_integer_type_node, | |
5659 | tree_cons (NULL_TREE, | |
5660 | long_integer_type_node, | |
5661 | tree_cons (NULL_TREE, | |
5662 | long_integer_type_node, | |
5663 | endlink))), | |
5664 | BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR); | |
5665 | ||
7f4edbcb | 5666 | /* Currently under experimentation. */ |
fc2aaf30 JM |
5667 | builtin_function_2 ("__builtin_memcpy", "memcpy", |
5668 | memcpy_ftype, memcpy_ftype, | |
5669 | BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0); | |
5670 | builtin_function_2 ("__builtin_memcmp", "memcmp", | |
5671 | int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet, | |
5672 | BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0); | |
5673 | builtin_function_2 ("__builtin_memset", "memset", | |
5674 | memset_ftype, memset_ftype, | |
5675 | BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0); | |
da9e9f08 | 5676 | built_in_decls[BUILT_IN_STRCMP] = |
fc2aaf30 JM |
5677 | builtin_function_2 ("__builtin_strcmp", "strcmp", |
5678 | int_ftype_cstring_cstring, int_ftype_cstring_cstring, | |
5679 | BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0); | |
5680 | builtin_function_2 ("__builtin_strncmp", "strncmp", | |
5681 | int_ftype_cstring_cstring_sizet, | |
5682 | int_ftype_cstring_cstring_sizet, | |
5683 | BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0); | |
5684 | builtin_function_2 ("__builtin_strstr", "strstr", | |
5685 | string_ftype_cstring_cstring, string_ftype_cstring_cstring, | |
5686 | BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0); | |
5687 | builtin_function_2 ("__builtin_strpbrk", "strpbrk", | |
5688 | string_ftype_cstring_cstring, string_ftype_cstring_cstring, | |
5689 | BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0); | |
78e7629e | 5690 | built_in_decls[BUILT_IN_STRCHR] = |
fc2aaf30 JM |
5691 | builtin_function_2 ("__builtin_strchr", "strchr", |
5692 | string_ftype_cstring_int, string_ftype_cstring_int, | |
5693 | BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0); | |
5694 | builtin_function_2 ("__builtin_strrchr", "strrchr", | |
5695 | string_ftype_cstring_int, string_ftype_cstring_int, | |
5696 | BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0); | |
5697 | builtin_function_2 ("__builtin_strcpy", "strcpy", | |
5698 | string_ftype_string_cstring, string_ftype_string_cstring, | |
5699 | BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0); | |
5700 | builtin_function_2 ("__builtin_strncpy", "strncpy", | |
5701 | string_ftype_string_cstring_sizet, | |
5702 | string_ftype_string_cstring_sizet, | |
5703 | BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0); | |
d118937d | 5704 | built_in_decls[BUILT_IN_STRCAT] = |
fc2aaf30 JM |
5705 | builtin_function_2 ("__builtin_strcat", "strcat", |
5706 | string_ftype_string_cstring, | |
5707 | string_ftype_string_cstring, | |
5708 | BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0); | |
5709 | builtin_function_2 ("__builtin_strncat", "strncat", | |
5710 | string_ftype_string_cstring_sizet, | |
5711 | string_ftype_string_cstring_sizet, | |
5712 | BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0); | |
5713 | builtin_function_2 ("__builtin_strspn", "strspn", | |
5714 | sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring, | |
5715 | BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0); | |
5716 | builtin_function_2 ("__builtin_strcspn", "strcspn", | |
5717 | sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring, | |
5718 | BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0); | |
d118937d | 5719 | built_in_decls[BUILT_IN_STRLEN] = |
fc2aaf30 JM |
5720 | builtin_function_2 ("__builtin_strlen", "strlen", |
5721 | strlen_ftype, strlen_ftype, | |
5722 | BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0); | |
5723 | ||
5724 | builtin_function_2 ("__builtin_sqrtf", "sqrtf", | |
5725 | float_ftype_float, float_ftype_float, | |
5726 | BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0); | |
5727 | builtin_function_2 ("__builtin_fsqrt", "sqrt", | |
5728 | double_ftype_double, double_ftype_double, | |
5729 | BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0); | |
5730 | builtin_function_2 ("__builtin_sqrtl", "sqrtl", | |
5731 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
5732 | BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0); | |
5733 | builtin_function_2 ("__builtin_sinf", "sinf", | |
5734 | float_ftype_float, float_ftype_float, | |
5735 | BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0); | |
5736 | builtin_function_2 ("__builtin_sin", "sin", | |
5737 | double_ftype_double, double_ftype_double, | |
5738 | BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0); | |
5739 | builtin_function_2 ("__builtin_sinl", "sinl", | |
5740 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
5741 | BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0); | |
5742 | builtin_function_2 ("__builtin_cosf", "cosf", | |
5743 | float_ftype_float, float_ftype_float, | |
5744 | BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0); | |
5745 | builtin_function_2 ("__builtin_cos", "cos", | |
5746 | double_ftype_double, double_ftype_double, | |
5747 | BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0); | |
5748 | builtin_function_2 ("__builtin_cosl", "cosl", | |
5749 | ldouble_ftype_ldouble, ldouble_ftype_ldouble, | |
5750 | BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0); | |
ad3fd36f KG |
5751 | built_in_decls[BUILT_IN_PUTCHAR] = |
5752 | builtin_function ("__builtin_putchar", int_ftype_int, | |
5753 | BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar"); | |
5754 | built_in_decls[BUILT_IN_PUTS] = | |
5755 | builtin_function ("__builtin_puts", puts_ftype, | |
5756 | BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts"); | |
fc2aaf30 JM |
5757 | builtin_function_2 ("__builtin_printf", "printf", |
5758 | printf_ftype, printf_ftype, | |
5759 | BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0); | |
07328167 | 5760 | built_in_decls[BUILT_IN_FWRITE] = |
c2e42aab | 5761 | builtin_function ("__builtin_fwrite", fwrite_ftype, |
07328167 | 5762 | BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite"); |
3ff5f682 | 5763 | built_in_decls[BUILT_IN_FPUTC] = |
c2e42aab KG |
5764 | builtin_function ("__builtin_fputc", fputc_ftype, |
5765 | BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc"); | |
5766 | /* Declare the __builtin_ style with arguments and the regular style | |
5767 | without them. We rely on stdio.h to supply the arguments for the | |
5768 | regular style declaration since we had to use void* instead of | |
5769 | FILE* in the __builtin_ prototype supplied here. */ | |
3ff5f682 | 5770 | built_in_decls[BUILT_IN_FPUTS] = |
fc2aaf30 | 5771 | builtin_function_2 ("__builtin_fputs", "fputs", |
c2e42aab | 5772 | fputs_ftype, int_ftype_any, |
fc2aaf30 JM |
5773 | BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0); |
5774 | ||
5775 | /* Declare these functions non-returning | |
5776 | to avoid spurious "control drops through" warnings. */ | |
5777 | builtin_function_2 (NULL_PTR, "abort", | |
5778 | NULL_TREE, ((c_language == clk_cplusplus) | |
5779 | ? void_ftype : void_ftype_any), | |
5780 | 0, NOT_BUILT_IN, 0, 0, 1); | |
5781 | ||
5782 | builtin_function_2 (NULL_PTR, "exit", | |
5783 | NULL_TREE, ((c_language == clk_cplusplus) | |
5784 | ? void_ftype_int : void_ftype_any), | |
5785 | 0, NOT_BUILT_IN, 0, 0, 1); | |
7f4edbcb BS |
5786 | |
5787 | #if 0 | |
5788 | /* Support for these has not been written in either expand_builtin | |
5789 | or build_function_call. */ | |
26db82d8 BS |
5790 | builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, |
5791 | BUILT_IN_NORMAL, NULL_PTR); | |
5792 | builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, | |
5793 | BUILT_IN_NORMAL, NULL_PTR); | |
7f4edbcb | 5794 | builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR, |
26db82d8 | 5795 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5796 | builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL, |
26db82d8 | 5797 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5798 | builtin_function ("__builtin_fmod", double_ftype_double_double, |
26db82d8 | 5799 | BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5800 | builtin_function ("__builtin_frem", double_ftype_double_double, |
26db82d8 | 5801 | BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5802 | builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP, |
26db82d8 | 5803 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5804 | builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN, |
26db82d8 | 5805 | BUILT_IN_NORMAL, NULL_PTR); |
7f4edbcb | 5806 | #endif |
c530479e | 5807 | |
5b47282c GM |
5808 | main_identifier_node = get_identifier ("main"); |
5809 | ||
c530479e RH |
5810 | /* ??? Perhaps there's a better place to do this. But it is related |
5811 | to __builtin_va_arg, so it isn't that off-the-wall. */ | |
5812 | lang_type_promotes_to = simple_type_promotes_to; | |
7f4edbcb | 5813 | } |
d3707adb RH |
5814 | |
5815 | tree | |
5816 | build_va_arg (expr, type) | |
5817 | tree expr, type; | |
5818 | { | |
5819 | return build1 (VA_ARG_EXPR, type, expr); | |
5820 | } | |
fc2aaf30 JM |
5821 | |
5822 | ||
5823 | /* Possibly define a builtin function with one or two names. BUILTIN_NAME | |
5824 | is an __builtin_-prefixed name; NAME is the ordinary name; one or both | |
5825 | of these may be NULL (though both being NULL is useless). | |
5826 | BUILTIN_TYPE is the type of the __builtin_-prefixed function; | |
5827 | TYPE is the type of the function with the ordinary name. These | |
5828 | may differ if the ordinary name is declared with a looser type to avoid | |
5829 | conflicts with headers. FUNCTION_CODE and CLASS are as for | |
5830 | builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as | |
5831 | the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME. | |
5832 | If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if | |
5833 | NORETURN_P is nonzero, the function is marked as non-returning. | |
5834 | Returns the declaration of BUILTIN_NAME, if any, otherwise | |
5835 | the declaration of NAME. Does not declare NAME if flag_no_builtin, | |
5836 | or if NONANSI_P and flag_no_nonansi_builtin. */ | |
5837 | ||
5838 | static tree | |
5839 | builtin_function_2 (builtin_name, name, builtin_type, type, function_code, | |
5840 | class, library_name_p, nonansi_p, noreturn_p) | |
5841 | const char *builtin_name; | |
5842 | const char *name; | |
5843 | tree builtin_type; | |
5844 | tree type; | |
5845 | int function_code; | |
5846 | enum built_in_class class; | |
5847 | int library_name_p; | |
5848 | int nonansi_p; | |
5849 | int noreturn_p; | |
5850 | { | |
5851 | tree bdecl = NULL_TREE; | |
5852 | tree decl = NULL_TREE; | |
5853 | if (builtin_name != 0) | |
5854 | { | |
5855 | bdecl = builtin_function (builtin_name, builtin_type, function_code, | |
5856 | class, library_name_p ? name : NULL_PTR); | |
5857 | if (noreturn_p) | |
5858 | { | |
5859 | TREE_THIS_VOLATILE (bdecl) = 1; | |
5860 | TREE_SIDE_EFFECTS (bdecl) = 1; | |
5861 | } | |
5862 | } | |
5863 | if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin)) | |
5864 | { | |
5865 | decl = builtin_function (name, type, function_code, class, NULL_PTR); | |
5866 | if (nonansi_p) | |
5867 | DECL_BUILT_IN_NONANSI (decl) = 1; | |
5868 | if (noreturn_p) | |
5869 | { | |
5870 | TREE_THIS_VOLATILE (decl) = 1; | |
5871 | TREE_SIDE_EFFECTS (decl) = 1; | |
5872 | } | |
5873 | } | |
5874 | return (bdecl != 0 ? bdecl : decl); | |
5875 | } | |
c530479e RH |
5876 | \f |
5877 | /* Given a type, apply default promotions wrt unnamed function arguments | |
5878 | and return the new type. Return NULL_TREE if no change. */ | |
d125d268 | 5879 | /* ??? There is a function of the same name in the C++ front end that |
c530479e | 5880 | does something similar, but is more thorough and does not return NULL |
d125d268 | 5881 | if no change. We could perhaps share code, but it would make the |
c530479e RH |
5882 | self_promoting_type property harder to identify. */ |
5883 | ||
5884 | tree | |
5885 | simple_type_promotes_to (type) | |
5886 | tree type; | |
5887 | { | |
5888 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5889 | return double_type_node; | |
5890 | ||
5891 | if (C_PROMOTING_INTEGER_TYPE_P (type)) | |
5892 | { | |
5893 | /* Traditionally, unsignedness is preserved in default promotions. | |
5894 | Also preserve unsignedness if not really getting any wider. */ | |
5895 | if (TREE_UNSIGNED (type) | |
5896 | && (flag_traditional | |
5897 | || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))) | |
5898 | return unsigned_type_node; | |
5899 | return integer_type_node; | |
5900 | } | |
5901 | ||
5902 | return NULL_TREE; | |
5903 | } | |
5904 | ||
5905 | /* Return 1 if PARMS specifies a fixed number of parameters | |
5906 | and none of their types is affected by default promotions. */ | |
5907 | ||
5908 | int | |
5909 | self_promoting_args_p (parms) | |
5910 | tree parms; | |
5911 | { | |
5912 | register tree t; | |
5913 | for (t = parms; t; t = TREE_CHAIN (t)) | |
5914 | { | |
5915 | register tree type = TREE_VALUE (t); | |
7e8176d7 | 5916 | |
c530479e RH |
5917 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5918 | return 0; | |
5919 | ||
5920 | if (type == 0) | |
5921 | return 0; | |
5922 | ||
5923 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5924 | return 0; | |
5925 | ||
5926 | if (C_PROMOTING_INTEGER_TYPE_P (type)) | |
5927 | return 0; | |
5928 | } | |
5929 | return 1; | |
5930 | } | |
5eda3d66 | 5931 | |
0a7394bc MM |
5932 | /* Recursively examines the array elements of TYPE, until a non-array |
5933 | element type is found. */ | |
5934 | ||
5935 | tree | |
5936 | strip_array_types (type) | |
5937 | tree type; | |
5938 | { | |
5939 | while (TREE_CODE (type) == ARRAY_TYPE) | |
5940 | type = TREE_TYPE (type); | |
5941 | ||
5942 | return type; | |
5943 | } | |
5944 | ||
5eda3d66 RH |
5945 | /* Recognize certain built-in functions so we can make tree-codes |
5946 | other than CALL_EXPR. We do this when it enables fold-const.c | |
5947 | to do something useful. */ | |
5948 | /* ??? By rights this should go in builtins.c, but only C and C++ | |
5949 | implement build_{binary,unary}_op. Not exactly sure what bits | |
5950 | of functionality are actually needed from those functions, or | |
5951 | where the similar functionality exists in the other front ends. */ | |
5952 | ||
5953 | tree | |
5954 | expand_tree_builtin (function, params, coerced_params) | |
5955 | tree function, params, coerced_params; | |
5956 | { | |
5957 | enum tree_code code; | |
5958 | ||
5959 | if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL) | |
5960 | return NULL_TREE; | |
5961 | ||
5962 | switch (DECL_FUNCTION_CODE (function)) | |
5963 | { | |
5964 | case BUILT_IN_ABS: | |
5eda3d66 RH |
5965 | case BUILT_IN_FABS: |
5966 | if (coerced_params == 0) | |
5967 | return integer_zero_node; | |
5968 | return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0); | |
5969 | ||
5970 | case BUILT_IN_ISGREATER: | |
5971 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
5972 | code = UNLE_EXPR; | |
5973 | else | |
5974 | code = LE_EXPR; | |
5975 | goto unordered_cmp; | |
5976 | ||
5977 | case BUILT_IN_ISGREATEREQUAL: | |
5978 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
5979 | code = UNLT_EXPR; | |
5980 | else | |
5981 | code = LT_EXPR; | |
5982 | goto unordered_cmp; | |
5983 | ||
5984 | case BUILT_IN_ISLESS: | |
5985 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
5986 | code = UNGE_EXPR; | |
5987 | else | |
5988 | code = GE_EXPR; | |
5989 | goto unordered_cmp; | |
5990 | ||
5991 | case BUILT_IN_ISLESSEQUAL: | |
5992 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
5993 | code = UNGT_EXPR; | |
5994 | else | |
5995 | code = GT_EXPR; | |
5996 | goto unordered_cmp; | |
5997 | ||
5998 | case BUILT_IN_ISLESSGREATER: | |
5999 | if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT) | |
6000 | code = UNEQ_EXPR; | |
6001 | else | |
6002 | code = EQ_EXPR; | |
6003 | goto unordered_cmp; | |
6004 | ||
6005 | case BUILT_IN_ISUNORDERED: | |
6006 | if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT) | |
6007 | return integer_zero_node; | |
6008 | code = UNORDERED_EXPR; | |
6009 | goto unordered_cmp; | |
6010 | ||
6011 | unordered_cmp: | |
6012 | { | |
6013 | tree arg0, arg1; | |
6014 | ||
6015 | if (params == 0 | |
6016 | || TREE_CHAIN (params) == 0) | |
6017 | { | |
6018 | error ("too few arguments to function `%s'", | |
6019 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
6020 | return error_mark_node; | |
6021 | } | |
6022 | else if (TREE_CHAIN (TREE_CHAIN (params)) != 0) | |
6023 | { | |
6024 | error ("too many arguments to function `%s'", | |
6025 | IDENTIFIER_POINTER (DECL_NAME (function))); | |
6026 | return error_mark_node; | |
6027 | } | |
6028 | ||
6029 | arg0 = TREE_VALUE (params); | |
6030 | arg1 = TREE_VALUE (TREE_CHAIN (params)); | |
6031 | arg0 = build_binary_op (code, arg0, arg1, 0); | |
6032 | if (code != UNORDERED_EXPR) | |
6033 | arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0); | |
6034 | return arg0; | |
6035 | } | |
6036 | break; | |
6037 | ||
6038 | default: | |
6039 | break; | |
6040 | } | |
6041 | ||
6042 | return NULL_TREE; | |
6043 | } | |
c7d87c0a | 6044 | |
ae499cce MM |
6045 | /* Returns non-zero if CODE is the code for a statement. */ |
6046 | ||
6047 | int | |
6048 | statement_code_p (code) | |
6049 | enum tree_code code; | |
6050 | { | |
6051 | switch (code) | |
6052 | { | |
6053 | case EXPR_STMT: | |
6054 | case COMPOUND_STMT: | |
6055 | case DECL_STMT: | |
6056 | case IF_STMT: | |
6057 | case FOR_STMT: | |
6058 | case WHILE_STMT: | |
6059 | case DO_STMT: | |
6060 | case RETURN_STMT: | |
6061 | case BREAK_STMT: | |
6062 | case CONTINUE_STMT: | |
8f17b5c5 | 6063 | case SCOPE_STMT: |
ae499cce MM |
6064 | case SWITCH_STMT: |
6065 | case GOTO_STMT: | |
6066 | case LABEL_STMT: | |
6067 | case ASM_STMT: | |
6068 | case CASE_LABEL: | |
6069 | return 1; | |
6070 | ||
6071 | default: | |
6072 | if (lang_statement_code_p) | |
6073 | return (*lang_statement_code_p) (code); | |
6074 | return 0; | |
6075 | } | |
6076 | } | |
6077 | ||
6078 | /* Walk the statemen tree, rooted at *tp. Apply FUNC to all the | |
6079 | sub-trees of *TP in a pre-order traversal. FUNC is called with the | |
6080 | DATA and the address of each sub-tree. If FUNC returns a non-NULL | |
6081 | value, the traversal is aborted, and the value returned by FUNC is | |
6082 | returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of | |
6083 | the node being visited are not walked. | |
6084 | ||
6085 | We don't need a without_duplicates variant of this one because the | |
6086 | statement tree is a tree, not a graph. */ | |
6087 | ||
6088 | tree | |
6089 | walk_stmt_tree (tp, func, data) | |
6090 | tree *tp; | |
6091 | walk_tree_fn func; | |
6092 | void *data; | |
6093 | { | |
6094 | enum tree_code code; | |
6095 | int walk_subtrees; | |
6096 | tree result; | |
6097 | int i, len; | |
6098 | ||
6099 | #define WALK_SUBTREE(NODE) \ | |
6100 | do \ | |
6101 | { \ | |
6102 | result = walk_stmt_tree (&(NODE), func, data); \ | |
6103 | if (result) \ | |
6104 | return result; \ | |
6105 | } \ | |
6106 | while (0) | |
6107 | ||
6108 | /* Skip empty subtrees. */ | |
6109 | if (!*tp) | |
6110 | return NULL_TREE; | |
6111 | ||
6112 | /* Skip subtrees below non-statement nodes. */ | |
6113 | if (!statement_code_p (TREE_CODE (*tp))) | |
6114 | return NULL_TREE; | |
6115 | ||
6116 | /* Call the function. */ | |
6117 | walk_subtrees = 1; | |
6118 | result = (*func) (tp, &walk_subtrees, data); | |
6119 | ||
6120 | /* If we found something, return it. */ | |
6121 | if (result) | |
6122 | return result; | |
6123 | ||
6124 | /* Even if we didn't, FUNC may have decided that there was nothing | |
6125 | interesting below this point in the tree. */ | |
6126 | if (!walk_subtrees) | |
6127 | return NULL_TREE; | |
6128 | ||
6129 | /* FUNC may have modified the tree, recheck that we're looking at a | |
6130 | statement node. */ | |
6131 | code = TREE_CODE (*tp); | |
6132 | if (!statement_code_p (code)) | |
6133 | return NULL_TREE; | |
6134 | ||
6135 | /* Walk over all the sub-trees of this operand. Statement nodes never | |
6136 | contain RTL, and we needn't worry about TARGET_EXPRs. */ | |
6137 | len = TREE_CODE_LENGTH (code); | |
6138 | ||
6139 | /* Go through the subtrees. We need to do this in forward order so | |
6140 | that the scope of a FOR_EXPR is handled properly. */ | |
6141 | for (i = 0; i < len; ++i) | |
6142 | WALK_SUBTREE (TREE_OPERAND (*tp, i)); | |
6143 | ||
6144 | /* Finally visit the chain. This can be tail-recursion optimized if | |
6145 | we write it this way. */ | |
6146 | return walk_stmt_tree (&TREE_CHAIN (*tp), func, data); | |
6147 | ||
6148 | #undef WALK_SUBTREE | |
6149 | } | |
6150 | ||
8f17b5c5 MM |
6151 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
6152 | representing case labels, or NULL_TREE for a `default' label. | |
6153 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
6154 | K2, and 0 if K1 and K2 are equal. */ | |
6155 | ||
6156 | int | |
6157 | case_compare (k1, k2) | |
6158 | splay_tree_key k1; | |
6159 | splay_tree_key k2; | |
6160 | { | |
6161 | /* Consider a NULL key (such as arises with a `default' label) to be | |
6162 | smaller than anything else. */ | |
6163 | if (!k1) | |
6164 | return k2 ? -1 : 0; | |
6165 | else if (!k2) | |
6166 | return k1 ? 1 : 0; | |
6167 | ||
6168 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
6169 | } | |
6170 | ||
6171 | /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If | |
6172 | LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is | |
6173 | actually a `default' label. If only HIGH_VALUE is NULL_TREE, then | |
6174 | case label was declared using the usual C/C++ syntax, rather than | |
6175 | the GNU case range extension. CASES is a tree containing all the | |
6176 | case ranges processed so far; COND is the condition for the | |
6177 | switch-statement itself. Returns the CASE_LABEL created, or | |
6178 | ERROR_MARK_NODE if no CASE_LABEL is created. */ | |
6179 | ||
6180 | tree | |
6181 | c_add_case_label (cases, cond, low_value, high_value) | |
6182 | splay_tree cases; | |
6183 | tree cond; | |
6184 | tree low_value; | |
6185 | tree high_value; | |
6186 | { | |
6187 | tree type; | |
6188 | tree label; | |
6189 | tree case_label; | |
6190 | splay_tree_node node; | |
6191 | ||
6192 | /* Create the LABEL_DECL itself. */ | |
6193 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
6194 | DECL_CONTEXT (label) = current_function_decl; | |
6195 | ||
6196 | /* If there was an error processing the switch condition, bail now | |
6197 | before we get more confused. */ | |
6198 | if (!cond || cond == error_mark_node) | |
6199 | { | |
6200 | /* Add a label anyhow so that the back-end doesn't think that | |
6201 | the beginning of the switch is unreachable. */ | |
6202 | if (!cases->root) | |
6203 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
6204 | return error_mark_node; | |
6205 | } | |
6206 | ||
6207 | if ((low_value && TREE_TYPE (low_value) | |
6208 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
6209 | || (high_value && TREE_TYPE (high_value) | |
6210 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
6211 | error ("pointers are not permitted as case values"); | |
6212 | ||
6213 | /* Case ranges are a GNU extension. */ | |
6214 | if (high_value && pedantic) | |
6215 | { | |
6216 | if (c_language == clk_cplusplus) | |
6217 | pedwarn ("ISO C++ forbids range expressions in switch statements"); | |
6218 | else | |
6219 | pedwarn ("ISO C forbids range expressions in switch statements"); | |
6220 | } | |
6221 | ||
6222 | type = TREE_TYPE (cond); | |
6223 | if (low_value) | |
6224 | { | |
6225 | low_value = check_case_value (low_value); | |
6226 | low_value = convert_and_check (type, low_value); | |
6227 | } | |
6228 | if (high_value) | |
6229 | { | |
6230 | high_value = check_case_value (high_value); | |
6231 | high_value = convert_and_check (type, high_value); | |
6232 | } | |
6233 | ||
6234 | /* If an error has occurred, bail out now. */ | |
6235 | if (low_value == error_mark_node || high_value == error_mark_node) | |
6236 | { | |
6237 | if (!cases->root) | |
6238 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
6239 | return error_mark_node; | |
6240 | } | |
6241 | ||
6242 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
6243 | really a case range, even though it was written that way. Remove | |
6244 | the HIGH_VALUE to simplify later processing. */ | |
6245 | if (tree_int_cst_equal (low_value, high_value)) | |
6246 | high_value = NULL_TREE; | |
6247 | if (low_value && high_value | |
6248 | && !tree_int_cst_lt (low_value, high_value)) | |
6249 | warning ("empty range specified"); | |
6250 | ||
6251 | /* Look up the LOW_VALUE in the table of case labels we already | |
6252 | have. */ | |
6253 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
6254 | /* If there was not an exact match, check for overlapping ranges. | |
6255 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
6256 | that's a `default' label and the only overlap is an exact match. */ | |
6257 | if (!node && (low_value || high_value)) | |
6258 | { | |
6259 | splay_tree_node low_bound; | |
6260 | splay_tree_node high_bound; | |
6261 | ||
6262 | /* Even though there wasn't an exact match, there might be an | |
6263 | overlap between this case range and another case range. | |
6264 | Since we've (inductively) not allowed any overlapping case | |
6265 | ranges, we simply need to find the greatest low case label | |
6266 | that is smaller that LOW_VALUE, and the smallest low case | |
6267 | label that is greater than LOW_VALUE. If there is an overlap | |
6268 | it will occur in one of these two ranges. */ | |
6269 | low_bound = splay_tree_predecessor (cases, | |
6270 | (splay_tree_key) low_value); | |
6271 | high_bound = splay_tree_successor (cases, | |
6272 | (splay_tree_key) low_value); | |
6273 | ||
6274 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
6275 | the LOW_VALUE, so there is no need to check unless the | |
6276 | LOW_BOUND is in fact itself a case range. */ | |
6277 | if (low_bound | |
6278 | && CASE_HIGH ((tree) low_bound->value) | |
6279 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
6280 | low_value) >= 0) | |
6281 | node = low_bound; | |
6282 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
6283 | range is bigger than the low end of the current range, so we | |
6284 | are only interested if the current range is a real range, and | |
6285 | not an ordinary case label. */ | |
6286 | else if (high_bound | |
6287 | && high_value | |
6288 | && (tree_int_cst_compare ((tree) high_bound->key, | |
6289 | high_value) | |
6290 | <= 0)) | |
6291 | node = high_bound; | |
6292 | } | |
6293 | /* If there was an overlap, issue an error. */ | |
6294 | if (node) | |
6295 | { | |
6296 | tree duplicate = CASE_LABEL_DECL ((tree) node->value); | |
6297 | ||
6298 | if (high_value) | |
6299 | { | |
6300 | error ("duplicate (or overlapping) case value"); | |
6301 | error_with_decl (duplicate, | |
6302 | "this is the first entry overlapping that value"); | |
6303 | } | |
6304 | else if (low_value) | |
6305 | { | |
6306 | error ("duplicate case value") ; | |
6307 | error_with_decl (duplicate, "previously used here"); | |
6308 | } | |
6309 | else | |
6310 | { | |
6311 | error ("multiple default labels in one switch"); | |
6312 | error_with_decl (duplicate, "this is the first default label"); | |
6313 | } | |
6314 | if (!cases->root) | |
6315 | add_stmt (build_case_label (NULL_TREE, NULL_TREE, label)); | |
6316 | } | |
6317 | ||
6318 | /* Add a CASE_LABEL to the statement-tree. */ | |
6319 | case_label = add_stmt (build_case_label (low_value, high_value, label)); | |
6320 | /* Register this case label in the splay tree. */ | |
6321 | splay_tree_insert (cases, | |
6322 | (splay_tree_key) low_value, | |
6323 | (splay_tree_value) case_label); | |
6324 | ||
6325 | return case_label; | |
6326 | } | |
6327 | ||
6328 | /* Mark P (a stmt_tree) for GC. The use of a `void *' for the | |
6329 | parameter allows this function to be used as a GC-marking | |
6330 | function. */ | |
6331 | ||
6332 | void | |
6333 | mark_stmt_tree (p) | |
6334 | void *p; | |
6335 | { | |
6336 | stmt_tree st = (stmt_tree) p; | |
6337 | ||
6338 | ggc_mark_tree (st->x_last_stmt); | |
6339 | ggc_mark_tree (st->x_last_expr_type); | |
6340 | } | |
6341 | ||
6342 | /* Mark LD for GC. */ | |
6343 | ||
6344 | void | |
6345 | c_mark_lang_decl (c) | |
6346 | struct c_lang_decl *c; | |
6347 | { | |
6348 | ggc_mark_tree (c->saved_tree); | |
6349 | } | |
6350 | ||
6351 | /* Mark F for GC. */ | |
6352 | ||
6353 | void | |
6354 | mark_c_language_function (f) | |
6355 | struct language_function *f; | |
6356 | { | |
6357 | if (!f) | |
6358 | return; | |
6359 | ||
6360 | mark_stmt_tree (&f->x_stmt_tree); | |
6361 | ggc_mark_tree (f->x_scope_stmt_stack); | |
6362 | } | |
6363 | ||
6364 | /* Hook used by expand_expr to expand language-specific tree codes. */ | |
6365 | ||
6366 | rtx | |
6367 | c_expand_expr (exp, target, tmode, modifier) | |
6368 | tree exp; | |
6369 | rtx target; | |
6370 | enum machine_mode tmode; | |
6371 | enum expand_modifier modifier; | |
6372 | { | |
6373 | switch (TREE_CODE (exp)) | |
6374 | { | |
6375 | case STMT_EXPR: | |
6376 | { | |
6377 | tree rtl_expr; | |
6378 | rtx result; | |
6379 | ||
6380 | /* Since expand_expr_stmt calls free_temp_slots after every | |
6381 | expression statement, we must call push_temp_slots here. | |
6382 | Otherwise, any temporaries in use now would be considered | |
6383 | out-of-scope after the first EXPR_STMT from within the | |
6384 | STMT_EXPR. */ | |
6385 | push_temp_slots (); | |
6386 | rtl_expr = expand_start_stmt_expr (); | |
6387 | expand_stmt (STMT_EXPR_STMT (exp)); | |
6388 | expand_end_stmt_expr (rtl_expr); | |
6389 | result = expand_expr (rtl_expr, target, tmode, modifier); | |
6390 | pop_temp_slots (); | |
6391 | return result; | |
6392 | } | |
6393 | break; | |
6394 | ||
c70eaeaf KG |
6395 | case CALL_EXPR: |
6396 | { | |
6397 | if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR | |
6398 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) | |
6399 | == FUNCTION_DECL) | |
6400 | && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) | |
6401 | && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) | |
6402 | == BUILT_IN_FRONTEND)) | |
6403 | return c_expand_builtin (exp, target, tmode, modifier); | |
6404 | else | |
6405 | abort(); | |
6406 | } | |
6407 | break; | |
6408 | ||
8f17b5c5 MM |
6409 | default: |
6410 | abort (); | |
6411 | } | |
6412 | ||
6413 | abort (); | |
6414 | return NULL; | |
6415 | } | |
6416 | ||
6417 | /* Hook used by safe_from_p to handle language-specific tree codes. */ | |
6418 | ||
6419 | int | |
6420 | c_safe_from_p (target, exp) | |
6421 | rtx target; | |
6422 | tree exp; | |
6423 | { | |
6424 | /* We can see statements here when processing the body of a | |
6425 | statement-expression. For a declaration statement declaring a | |
6426 | variable, look at the variable's initializer. */ | |
6427 | if (TREE_CODE (exp) == DECL_STMT) | |
6428 | { | |
6429 | tree decl = DECL_STMT_DECL (exp); | |
6430 | ||
6431 | if (TREE_CODE (decl) == VAR_DECL | |
6432 | && DECL_INITIAL (decl) | |
6433 | && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0)) | |
6434 | return 0; | |
6435 | } | |
6436 | ||
6437 | /* For any statement, we must follow the statement-chain. */ | |
6438 | if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp)) | |
6439 | return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0); | |
6440 | ||
6441 | /* Assume everything else is safe. */ | |
6442 | return 1; | |
6443 | } | |
6444 | ||
3fe30ff6 RH |
6445 | /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */ |
6446 | ||
6447 | int | |
6448 | c_unsafe_for_reeval (exp) | |
6449 | tree exp; | |
6450 | { | |
6451 | /* Statement expressions may not be reevaluated. */ | |
6452 | if (TREE_CODE (exp) == STMT_EXPR) | |
6453 | return 2; | |
6454 | ||
6455 | /* Walk all other expressions. */ | |
6456 | return -1; | |
6457 | } | |
6458 | ||
c7d87c0a BC |
6459 | /* Tree code classes. */ |
6460 | ||
6461 | #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE, | |
6462 | ||
6463 | static char c_tree_code_type[] = { | |
6464 | 'x', | |
6465 | #include "c-common.def" | |
6466 | }; | |
6467 | #undef DEFTREECODE | |
6468 | ||
6469 | /* Table indexed by tree code giving number of expression | |
6470 | operands beyond the fixed part of the node structure. | |
6471 | Not used for types or decls. */ | |
6472 | ||
6473 | #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH, | |
6474 | ||
6475 | static int c_tree_code_length[] = { | |
6476 | 0, | |
6477 | #include "c-common.def" | |
6478 | }; | |
6479 | #undef DEFTREECODE | |
6480 | ||
6481 | /* Names of tree components. | |
6482 | Used for printing out the tree and error messages. */ | |
6483 | #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME, | |
6484 | ||
6485 | static const char *c_tree_code_name[] = { | |
6486 | "@@dummy", | |
6487 | #include "c-common.def" | |
6488 | }; | |
6489 | #undef DEFTREECODE | |
6490 | ||
6491 | /* Adds the tree codes specific to the C front end to the list of all | |
6492 | tree codes. */ | |
6493 | ||
6494 | void | |
5538e30f | 6495 | add_c_tree_codes () |
c7d87c0a BC |
6496 | { |
6497 | memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE, | |
6498 | c_tree_code_type, | |
6499 | (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE); | |
6500 | memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE, | |
6501 | c_tree_code_length, | |
6502 | (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int)); | |
6503 | memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE, | |
6504 | c_tree_code_name, | |
6505 | (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *)); | |
3fe30ff6 | 6506 | lang_unsafe_for_reeval = c_unsafe_for_reeval; |
c7d87c0a | 6507 | } |
c70eaeaf KG |
6508 | |
6509 | #define CALLED_AS_BUILT_IN(NODE) \ | |
6510 | (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10)) | |
6511 | ||
6512 | static rtx | |
6513 | c_expand_builtin (exp, target, tmode, modifier) | |
6514 | tree exp; | |
6515 | rtx target; | |
6516 | enum machine_mode tmode; | |
6517 | enum expand_modifier modifier; | |
6518 | { | |
6519 | tree type = TREE_TYPE (exp); | |
6520 | tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0); | |
6521 | tree arglist = TREE_OPERAND (exp, 1); | |
6522 | enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); | |
6523 | enum tree_code code = TREE_CODE (exp); | |
6524 | const int ignore = (target == const0_rtx | |
6525 | || ((code == NON_LVALUE_EXPR || code == NOP_EXPR | |
6526 | || code == CONVERT_EXPR || code == REFERENCE_EXPR | |
6527 | || code == COND_EXPR) | |
6528 | && TREE_CODE (type) == VOID_TYPE)); | |
6529 | ||
6530 | if (! optimize && ! CALLED_AS_BUILT_IN (fndecl)) | |
6531 | return expand_call (exp, target, ignore); | |
6532 | ||
6533 | switch (fcode) | |
6534 | { | |
6535 | case BUILT_IN_PRINTF: | |
6536 | target = c_expand_builtin_printf (arglist, target, tmode, | |
6537 | modifier, ignore); | |
6538 | if (target) | |
6539 | return target; | |
6540 | break; | |
6541 | ||
6542 | default: /* just do library call, if unknown builtin */ | |
6543 | error ("built-in function `%s' not currently supported", | |
6544 | IDENTIFIER_POINTER (DECL_NAME (fndecl))); | |
6545 | } | |
6546 | ||
6547 | /* The switch statement above can drop through to cause the function | |
6548 | to be called normally. */ | |
6549 | return expand_call (exp, target, ignore); | |
6550 | } | |
6551 | ||
6552 | /* Check an arglist to *printf for problems. The arglist should start | |
6553 | at the format specifier, with the remaining arguments immediately | |
6554 | following it. */ | |
6555 | static int | |
6556 | is_valid_printf_arglist (arglist) | |
6557 | tree arglist; | |
6558 | { | |
6559 | /* Save this value so we can restore it later. */ | |
6560 | const int SAVE_pedantic = pedantic; | |
6561 | int diagnostic_occurred = 0; | |
6562 | ||
6563 | /* Set this to a known value so the user setting won't affect code | |
6564 | generation. */ | |
6565 | pedantic = 1; | |
6566 | /* Check to make sure there are no format specifier errors. */ | |
6567 | check_function_format (&diagnostic_occurred, | |
6568 | maybe_get_identifier("printf"), | |
6569 | NULL_TREE, arglist); | |
6570 | ||
6571 | /* Restore the value of `pedantic'. */ | |
6572 | pedantic = SAVE_pedantic; | |
6573 | ||
6574 | /* If calling `check_function_format_ptr' produces a warning, we | |
6575 | return false, otherwise we return true. */ | |
6576 | return ! diagnostic_occurred; | |
6577 | } | |
6578 | ||
6579 | /* If the arguments passed to printf are suitable for optimizations, | |
6580 | we attempt to transform the call. */ | |
6581 | static rtx | |
6582 | c_expand_builtin_printf (arglist, target, tmode, modifier, ignore) | |
6583 | tree arglist; | |
6584 | rtx target; | |
6585 | enum machine_mode tmode; | |
6586 | enum expand_modifier modifier; | |
6587 | int ignore; | |
6588 | { | |
6589 | tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR], | |
6590 | fn_puts = built_in_decls[BUILT_IN_PUTS]; | |
6591 | tree fn, format_arg, stripped_string; | |
6592 | ||
6593 | /* If the return value is used, or the replacement _DECL isn't | |
6594 | initialized, don't do the transformation. */ | |
6595 | if (!ignore || !fn_putchar || !fn_puts) | |
6596 | return 0; | |
6597 | ||
6598 | /* Verify the required arguments in the original call. */ | |
6599 | if (arglist == 0 | |
6600 | || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)) | |
6601 | return 0; | |
6602 | ||
6603 | /* Check the specifier vs. the parameters. */ | |
6604 | if (!is_valid_printf_arglist (arglist)) | |
6605 | return 0; | |
6606 | ||
6607 | format_arg = TREE_VALUE (arglist); | |
6608 | stripped_string = format_arg; | |
6609 | STRIP_NOPS (stripped_string); | |
6610 | if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR) | |
6611 | stripped_string = TREE_OPERAND (stripped_string, 0); | |
6612 | ||
6613 | /* If the format specifier isn't a STRING_CST, punt. */ | |
6614 | if (TREE_CODE (stripped_string) != STRING_CST) | |
6615 | return 0; | |
6616 | ||
6617 | /* OK! We can attempt optimization. */ | |
6618 | ||
6619 | /* If the format specifier was "%s\n", call __builtin_puts(arg2). */ | |
6620 | if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0) | |
6621 | { | |
6622 | arglist = TREE_CHAIN (arglist); | |
6623 | fn = fn_puts; | |
6624 | } | |
6625 | /* If the format specifier was "%c", call __builtin_putchar (arg2). */ | |
6626 | else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0) | |
6627 | { | |
6628 | arglist = TREE_CHAIN (arglist); | |
6629 | fn = fn_putchar; | |
6630 | } | |
6631 | else | |
6632 | { | |
6633 | /* We can't handle anything else with % args or %% ... yet. */ | |
6634 | if (strchr (TREE_STRING_POINTER (stripped_string), '%')) | |
6635 | return 0; | |
6636 | ||
6637 | /* If the resulting constant string has a length of 1, call | |
6638 | putchar. Note, TREE_STRING_LENGTH includes the terminating | |
6639 | NULL in its count. */ | |
6640 | if (TREE_STRING_LENGTH (stripped_string) == 2) | |
6641 | { | |
6642 | /* Given printf("c"), (where c is any one character,) | |
6643 | convert "c"[0] to an int and pass that to the replacement | |
6644 | function. */ | |
6645 | arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0); | |
6646 | arglist = build_tree_list (NULL_TREE, arglist); | |
6647 | ||
6648 | fn = fn_putchar; | |
6649 | } | |
6650 | /* If the resulting constant was "string\n", call | |
6651 | __builtin_puts("string"). Ensure "string" has at least one | |
6652 | character besides the trailing \n. Note, TREE_STRING_LENGTH | |
6653 | includes the terminating NULL in its count. */ | |
6654 | else if (TREE_STRING_LENGTH (stripped_string) > 2 | |
6655 | && TREE_STRING_POINTER (stripped_string) | |
6656 | [TREE_STRING_LENGTH (stripped_string) - 2] == '\n') | |
6657 | { | |
6658 | /* Create a NULL-terminated string that's one char shorter | |
6659 | than the original, stripping off the trailing '\n'. */ | |
6660 | const int newlen = TREE_STRING_LENGTH (stripped_string) - 1; | |
6661 | char *newstr = (char *) alloca (newlen); | |
6662 | memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1); | |
6663 | newstr[newlen - 1] = 0; | |
6664 | ||
1092710d | 6665 | arglist = combine_strings (build_string (newlen, newstr)); |
c70eaeaf KG |
6666 | arglist = build_tree_list (NULL_TREE, arglist); |
6667 | fn = fn_puts; | |
6668 | } | |
6669 | else | |
6670 | /* We'd like to arrange to call fputs(string) here, but we | |
6671 | need stdout and don't have a way to get it ... yet. */ | |
6672 | return 0; | |
6673 | } | |
6674 | ||
6675 | return expand_expr (build_function_call (fn, arglist), | |
6676 | (ignore ? const0_rtx : target), | |
6677 | tmode, modifier); | |
6678 | } | |
19552aa5 JM |
6679 | \f |
6680 | ||
6681 | /* Given a boolean expression ARG, return a tree representing an increment | |
6682 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
6683 | invalid cases (e.g., decrement in C++). */ | |
6684 | tree | |
6685 | boolean_increment (code, arg) | |
6686 | enum tree_code code; | |
6687 | tree arg; | |
6688 | { | |
6689 | tree val; | |
6690 | tree true_res = (c_language == clk_cplusplus | |
6691 | ? boolean_true_node | |
6692 | : c_bool_true_node); | |
6693 | arg = stabilize_reference (arg); | |
6694 | switch (code) | |
6695 | { | |
6696 | case PREINCREMENT_EXPR: | |
6697 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); | |
6698 | break; | |
6699 | case POSTINCREMENT_EXPR: | |
6700 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); | |
6701 | arg = save_expr (arg); | |
6702 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); | |
6703 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
6704 | break; | |
6705 | case PREDECREMENT_EXPR: | |
6706 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg)); | |
6707 | break; | |
6708 | case POSTDECREMENT_EXPR: | |
6709 | val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg)); | |
6710 | arg = save_expr (arg); | |
6711 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); | |
6712 | val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
6713 | break; | |
6714 | default: | |
6715 | abort (); | |
6716 | } | |
6717 | TREE_SIDE_EFFECTS (val) = 1; | |
6718 | return val; | |
6719 | } | |
03dc0325 JM |
6720 | \f |
6721 | ||
6722 | /* Do the parts of lang_init common to C and C++. */ | |
6723 | void | |
6724 | c_common_lang_init () | |
6725 | { | |
6726 | /* If still "unspecified", make it match -fbounded-pointers. */ | |
6727 | if (flag_bounds_check < 0) | |
6728 | flag_bounds_check = flag_bounded_pointers; | |
6729 | ||
6730 | /* Special format checking options don't work without -Wformat; warn if | |
6731 | they are used. */ | |
6732 | if (warn_format_y2k && !warn_format) | |
6733 | warning ("-Wformat-y2k ignored without -Wformat"); | |
6734 | if (warn_format_extra_args && !warn_format) | |
6735 | warning ("-Wformat-extra-args ignored without -Wformat"); | |
6736 | if (warn_format_nonliteral && !warn_format) | |
6737 | warning ("-Wformat-nonliteral ignored without -Wformat"); | |
6738 | if (warn_format_security && !warn_format) | |
6739 | warning ("-Wformat-security ignored without -Wformat"); | |
6740 | if (warn_missing_format_attribute && !warn_format) | |
6741 | warning ("-Wmissing-format-attribute ignored without -Wformat"); | |
6742 | } |