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