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