-
Notifications
You must be signed in to change notification settings - Fork 3
/
c-common.c
2255 lines (2004 loc) · 66.8 KB
/
c-common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Subroutines shared by all languages that are variants of C.
Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include "config.h"
#include "tree.h"
#include "c-lex.h"
#include "c-tree.h"
#include "flags.h"
#include "obstack.h"
#include <stdio.h>
#include <ctype.h>
extern struct obstack permanent_obstack;
enum attrs {A_PACKED, A_NOCOMMON, A_NORETURN, A_CONST, A_T_UNION,
A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
A_UNUSED, A_FORMAT, A_WEAK, A_ALIAS};
static void declare_hidden_char_array PROTO((char *, char *));
static void add_attribute PROTO((enum attrs, char *,
int, int, int));
static void init_attributes PROTO((void));
/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
void
declare_function_name ()
{
char *name, *printable_name;
if (current_function_decl == NULL)
{
name = "";
printable_name = "top level";
}
else
{
char *kind = "function";
if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
kind = "method";
/* Allow functions to be nameless (such as artificial ones). */
if (DECL_NAME (current_function_decl))
name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
else
name = "";
printable_name = (*decl_printable_name) (current_function_decl, &kind);
}
declare_hidden_char_array ("__FUNCTION__", name);
declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
}
static void
declare_hidden_char_array (name, value)
char *name, *value;
{
tree decl, type, init;
int vlen;
/* If the default size of char arrays isn't big enough for the name,
or if we want to give warnings for large objects, make a bigger one. */
vlen = strlen (value) + 1;
type = char_array_type_node;
if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
|| warn_larger_than)
type = build_array_type (char_type_node,
build_index_type (build_int_2 (vlen, 0)));
push_obstacks_nochange ();
decl = build_decl (VAR_DECL, get_identifier (name), type);
TREE_STATIC (decl) = 1;
TREE_READONLY (decl) = 1;
TREE_ASM_WRITTEN (decl) = 1;
DECL_SOURCE_LINE (decl) = 0;
DECL_ARTIFICIAL (decl) = 1;
DECL_IN_SYSTEM_HEADER (decl) = 1;
DECL_IGNORED_P (decl) = 1;
init = build_string (vlen, value);
TREE_TYPE (init) = type;
DECL_INITIAL (decl) = init;
finish_decl (pushdecl (decl), init, NULL_TREE);
}
/* Given a chain of STRING_CST nodes,
concatenate them into one STRING_CST
and give it a suitable array-of-chars data type. */
tree
combine_strings (strings)
tree strings;
{
register tree value, t;
register int length = 1;
int wide_length = 0;
int wide_flag = 0;
int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
int nchars;
if (TREE_CHAIN (strings))
{
/* More than one in the chain, so concatenate. */
register char *p, *q;
/* Don't include the \0 at the end of each substring,
except for the last one.
Count wide strings and ordinary strings separately. */
for (t = strings; t; t = TREE_CHAIN (t))
{
if (TREE_TYPE (t) == wchar_array_type_node)
{
wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
wide_flag = 1;
}
else
length += (TREE_STRING_LENGTH (t) - 1);
}
/* If anything is wide, the non-wides will be converted,
which makes them take more space. */
if (wide_flag)
length = length * wchar_bytes + wide_length;
p = savealloc (length);
/* Copy the individual strings into the new combined string.
If the combined string is wide, convert the chars to ints
for any individual strings that are not wide. */
q = p;
for (t = strings; t; t = TREE_CHAIN (t))
{
int len = (TREE_STRING_LENGTH (t)
- ((TREE_TYPE (t) == wchar_array_type_node)
? wchar_bytes : 1));
if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
{
bcopy (TREE_STRING_POINTER (t), q, len);
q += len;
}
else
{
int i;
for (i = 0; i < len; i++)
((int *) q)[i] = TREE_STRING_POINTER (t)[i];
q += len * wchar_bytes;
}
}
if (wide_flag)
{
int i;
for (i = 0; i < wchar_bytes; i++)
*q++ = 0;
}
else
*q = 0;
value = make_node (STRING_CST);
TREE_STRING_POINTER (value) = p;
TREE_STRING_LENGTH (value) = length;
TREE_CONSTANT (value) = 1;
}
else
{
value = strings;
length = TREE_STRING_LENGTH (value);
if (TREE_TYPE (value) == wchar_array_type_node)
wide_flag = 1;
}
/* Compute the number of elements, for the array type. */
nchars = wide_flag ? length / wchar_bytes : length;
/* Create the array type for the string constant.
-Wwrite-strings says make the string constant an array of const char
so that copying it to a non-const pointer will get a warning. */
if (warn_write_strings
&& (! flag_traditional && ! flag_writable_strings))
{
tree elements
= build_type_variant (wide_flag ? wchar_type_node : char_type_node,
1, 0);
TREE_TYPE (value)
= build_array_type (elements,
build_index_type (build_int_2 (nchars - 1, 0)));
}
else
TREE_TYPE (value)
= build_array_type (wide_flag ? wchar_type_node : char_type_node,
build_index_type (build_int_2 (nchars - 1, 0)));
TREE_CONSTANT (value) = 1;
TREE_STATIC (value) = 1;
return value;
}
/* To speed up processing of attributes, we maintain an array of
IDENTIFIER_NODES and the corresponding attribute types. */
/* Array to hold attribute information. */
static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
static int attrtab_idx = 0;
/* Add an entry to the attribute table above. */
static void
add_attribute (id, string, min_len, max_len, decl_req)
enum attrs id;
char *string;
int min_len, max_len;
int decl_req;
{
char buf[100];
attrtab[attrtab_idx].id = id;
attrtab[attrtab_idx].name = get_identifier (string);
attrtab[attrtab_idx].min = min_len;
attrtab[attrtab_idx].max = max_len;
attrtab[attrtab_idx++].decl_req = decl_req;
sprintf (buf, "__%s__", string);
attrtab[attrtab_idx].id = id;
attrtab[attrtab_idx].name = get_identifier (buf);
attrtab[attrtab_idx].min = min_len;
attrtab[attrtab_idx].max = max_len;
attrtab[attrtab_idx++].decl_req = decl_req;
}
/* Initialize attribute table. */
static void
init_attributes ()
{
add_attribute (A_PACKED, "packed", 0, 0, 0);
add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
add_attribute (A_NORETURN, "volatile", 0, 0, 1);
add_attribute (A_UNUSED, "unused", 0, 0, 1);
add_attribute (A_CONST, "const", 0, 0, 1);
add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
add_attribute (A_MODE, "mode", 1, 1, 1);
add_attribute (A_SECTION, "section", 1, 1, 1);
add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
add_attribute (A_FORMAT, "format", 3, 3, 1);
add_attribute (A_WEAK, "weak", 0, 0, 1);
add_attribute (A_ALIAS, "alias", 1, 1, 1);
}
/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
and install them in NODE, which is either a DECL (including a TYPE_DECL)
or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
and declaration modifiers but before the declaration proper. */
void
decl_attributes (node, attributes, prefix_attributes)
tree node, attributes, prefix_attributes;
{
tree decl = 0, type;
int is_type;
tree a;
if (attrtab_idx == 0)
init_attributes ();
if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
{
decl = node;
type = TREE_TYPE (decl);
is_type = TREE_CODE (node) == TYPE_DECL;
}
else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
type = node, is_type = 1;
attributes = chainon (prefix_attributes, attributes);
for (a = attributes; a; a = TREE_CHAIN (a))
{
tree name = TREE_PURPOSE (a);
tree args = TREE_VALUE (a);
int i;
enum attrs id;
for (i = 0; i < attrtab_idx; i++)
if (attrtab[i].name == name)
break;
if (i == attrtab_idx)
{
if (! valid_machine_attribute (name, args, decl, type))
warning ("`%s' attribute directive ignored",
IDENTIFIER_POINTER (name));
continue;
}
else if (attrtab[i].decl_req && decl == 0)
{
warning ("`%s' attribute does not apply to types",
IDENTIFIER_POINTER (name));
continue;
}
else if (list_length (args) < attrtab[i].min
|| list_length (args) > attrtab[i].max)
{
error ("wrong number of arguments specified for `%s' attribute",
IDENTIFIER_POINTER (name));
continue;
}
id = attrtab[i].id;
switch (id)
{
case A_PACKED:
if (is_type)
TYPE_PACKED (type) = 1;
else if (TREE_CODE (decl) == FIELD_DECL)
DECL_PACKED (decl) = 1;
/* We can't set DECL_PACKED for a VAR_DECL, because the bit is
used for DECL_REGISTER. It wouldn't mean anything anyway. */
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_NOCOMMON:
if (TREE_CODE (decl) == VAR_DECL)
DECL_COMMON (decl) = 0;
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_NORETURN:
if (TREE_CODE (decl) == FUNCTION_DECL)
TREE_THIS_VOLATILE (decl) = 1;
else if (TREE_CODE (type) == POINTER_TYPE
&& TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
TREE_TYPE (decl) = type
= build_pointer_type
(build_type_variant (TREE_TYPE (type),
TREE_READONLY (TREE_TYPE (type)), 1));
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_UNUSED:
if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL
|| TREE_CODE (decl) == FUNCTION_DECL)
TREE_USED (decl) = 1;
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_CONST:
if (TREE_CODE (decl) == FUNCTION_DECL)
TREE_READONLY (decl) = 1;
else if (TREE_CODE (type) == POINTER_TYPE
&& TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
TREE_TYPE (decl) = type
= build_pointer_type
(build_type_variant (TREE_TYPE (type), 1,
TREE_THIS_VOLATILE (TREE_TYPE (type))));
else
warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_T_UNION:
if (is_type
&& TREE_CODE (type) == UNION_TYPE
&& (decl == 0
|| TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type))))
TYPE_TRANSPARENT_UNION (type) = 1;
else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
&& TREE_CODE (type) == UNION_TYPE
&& TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
DECL_TRANSPARENT_UNION (decl) = 1;
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_CONSTRUCTOR:
if (TREE_CODE (decl) == FUNCTION_DECL
&& TREE_CODE (type) == FUNCTION_TYPE
&& decl_function_context (decl) == 0)
{
DECL_STATIC_CONSTRUCTOR (decl) = 1;
TREE_USED (decl) = 1;
}
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_DESTRUCTOR:
if (TREE_CODE (decl) == FUNCTION_DECL
&& TREE_CODE (type) == FUNCTION_TYPE
&& decl_function_context (decl) == 0)
{
DECL_STATIC_DESTRUCTOR (decl) = 1;
TREE_USED (decl) = 1;
}
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
case A_MODE:
if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
else
{
int j;
char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
int len = strlen (p);
enum machine_mode mode = VOIDmode;
tree typefm;
if (len > 4 && p[0] == '_' && p[1] == '_'
&& p[len - 1] == '_' && p[len - 2] == '_')
{
char *newp = (char *) alloca (len - 1);
strcpy (newp, &p[2]);
newp[len - 4] = '\0';
p = newp;
}
/* Give this decl a type with the specified mode.
First check for the special modes. */
if (! strcmp (p, "byte"))
mode = byte_mode;
else if (!strcmp (p, "word"))
mode = word_mode;
else if (! strcmp (p, "pointer"))
mode = ptr_mode;
else
for (j = 0; j < NUM_MACHINE_MODES; j++)
if (!strcmp (p, GET_MODE_NAME (j)))
mode = (enum machine_mode) j;
if (mode == VOIDmode)
error ("unknown machine mode `%s'", p);
else if (0 == (typefm = type_for_mode (mode,
TREE_UNSIGNED (type))))
error ("no data type for mode `%s'", p);
else
{
TREE_TYPE (decl) = type = typefm;
DECL_SIZE (decl) = 0;
layout_decl (decl, 0);
}
}
break;
case A_SECTION:
#ifdef ASM_OUTPUT_SECTION_NAME
if ((TREE_CODE (decl) == FUNCTION_DECL
|| TREE_CODE (decl) == VAR_DECL)
&& TREE_CODE (TREE_VALUE (args)) == STRING_CST)
{
if (TREE_CODE (decl) == VAR_DECL
&& current_function_decl != NULL_TREE)
error_with_decl (decl,
"section attribute cannot be specified for local variables");
/* The decl may have already been given a section attribute from
a previous declaration. Ensure they match. */
else if (DECL_SECTION_NAME (decl) != NULL_TREE
&& strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
error_with_decl (node,
"section of `%s' conflicts with previous declaration");
else
DECL_SECTION_NAME (decl) = TREE_VALUE (args);
}
else
error_with_decl (node,
"section attribute not allowed for `%s'");
#else
error_with_decl (node,
"section attributes are not supported for this target");
#endif
break;
case A_ALIGNED:
{
tree align_expr
= args ? TREE_VALUE (args) : size_int (BIGGEST_ALIGNMENT);
int align;
/* Strip any NOPs of any kind. */
while (TREE_CODE (align_expr) == NOP_EXPR
|| TREE_CODE (align_expr) == CONVERT_EXPR
|| TREE_CODE (align_expr) == NON_LVALUE_EXPR)
align_expr = TREE_OPERAND (align_expr, 0);
if (TREE_CODE (align_expr) != INTEGER_CST)
{
error ("requested alignment is not a constant");
continue;
}
align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
if (exact_log2 (align) == -1)
error ("requested alignment is not a power of 2");
else if (is_type)
TYPE_ALIGN (type) = align;
else if (TREE_CODE (decl) != VAR_DECL
&& TREE_CODE (decl) != FIELD_DECL)
error_with_decl (decl,
"alignment may not be specified for `%s'");
else
DECL_ALIGN (decl) = align;
}
break;
case A_FORMAT:
{
tree format_type = TREE_VALUE (args);
tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
tree first_arg_num_expr
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
int format_num;
int first_arg_num;
int is_scan;
tree argument;
int arg_num;
if (TREE_CODE (decl) != FUNCTION_DECL)
{
error_with_decl (decl,
"argument format specified for non-function `%s'");
continue;
}
if (TREE_CODE (format_type) == IDENTIFIER_NODE
&& (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
|| !strcmp (IDENTIFIER_POINTER (format_type),
"__printf__")))
is_scan = 0;
else if (TREE_CODE (format_type) == IDENTIFIER_NODE
&& (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
|| !strcmp (IDENTIFIER_POINTER (format_type),
"__scanf__")))
is_scan = 1;
else
{
error ("unrecognized format specifier for `%s'");
continue;
}
/* Strip any conversions from the string index and first arg number
and verify they are constants. */
while (TREE_CODE (format_num_expr) == NOP_EXPR
|| TREE_CODE (format_num_expr) == CONVERT_EXPR
|| TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
format_num_expr = TREE_OPERAND (format_num_expr, 0);
while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
|| TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
|| TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
if (TREE_CODE (format_num_expr) != INTEGER_CST
|| TREE_CODE (first_arg_num_expr) != INTEGER_CST)
{
error ("format string has non-constant operand number");
continue;
}
format_num = TREE_INT_CST_LOW (format_num_expr);
first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
if (first_arg_num != 0 && first_arg_num <= format_num)
{
error ("format string arg follows the args to be formatted");
continue;
}
/* If a parameter list is specified, verify that the format_num
argument is actually a string, in case the format attribute
is in error. */
argument = TYPE_ARG_TYPES (type);
if (argument)
{
for (arg_num = 1; ; ++arg_num)
{
if (argument == 0 || arg_num == format_num)
break;
argument = TREE_CHAIN (argument);
}
if (! argument
|| TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
|| (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
!= char_type_node))
{
error ("format string arg not a string type");
continue;
}
if (first_arg_num != 0)
{
/* Verify that first_arg_num points to the last arg,
the ... */
while (argument)
arg_num++, argument = TREE_CHAIN (argument);
if (arg_num != first_arg_num)
{
error ("args to be formatted is not ...");
continue;
}
}
}
record_function_format (DECL_NAME (decl),
DECL_ASSEMBLER_NAME (decl),
is_scan, format_num, first_arg_num);
break;
}
case A_WEAK:
declare_weak (decl);
break;
case A_ALIAS:
if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
|| TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl))
error_with_decl (decl,
"`%s' defined both normally and as an alias");
else if (decl_function_context (decl) == 0)
{
tree id = get_identifier (TREE_STRING_POINTER
(TREE_VALUE (args)));
if (TREE_CODE (decl) == FUNCTION_DECL)
DECL_INITIAL (decl) = error_mark_node;
else
DECL_EXTERNAL (decl) = 0;
assemble_alias (decl, id);
}
else
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
break;
}
}
}
/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
a parameter list. */
#define T_I &integer_type_node
#define T_L &long_integer_type_node
#define T_LL &long_long_integer_type_node
#define T_S &short_integer_type_node
#define T_UI &unsigned_type_node
#define T_UL &long_unsigned_type_node
#define T_ULL &long_long_unsigned_type_node
#define T_US &short_unsigned_type_node
#define T_F &float_type_node
#define T_D &double_type_node
#define T_LD &long_double_type_node
#define T_C &char_type_node
#define T_V &void_type_node
#define T_W &wchar_type_node
#define T_ST &sizetype
typedef struct {
char *format_chars;
int pointer_count;
/* Type of argument if no length modifier is used. */
tree *nolen;
/* Type of argument if length modifier for shortening is used.
If NULL, then this modifier is not allowed. */
tree *hlen;
/* Type of argument if length modifier `l' is used.
If NULL, then this modifier is not allowed. */
tree *llen;
/* Type of argument if length modifier `q' or `ll' is used.
If NULL, then this modifier is not allowed. */
tree *qlen;
/* Type of argument if length modifier `L' is used.
If NULL, then this modifier is not allowed. */
tree *bigllen;
/* List of other modifier characters allowed with these options. */
char *flag_chars;
} format_char_info;
static format_char_info print_char_table[] = {
{ "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
{ "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
{ "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
/* Two GNU extensions. */
{ "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
{ "m", 0, T_V, NULL, NULL, NULL, NULL, "-wp" },
{ "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
{ "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
{ "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
{ "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
{ "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
{ "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
{ "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
{ NULL }
};
static format_char_info scan_char_table[] = {
{ "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
{ "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
{ "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
{ "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
{ "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
{ "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
{ "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
{ "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
{ "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
{ NULL }
};
typedef struct function_format_info {
struct function_format_info *next; /* next structure on the list */
tree name; /* identifier such as "printf" */
tree assembler_name; /* optional mangled identifier (for C++) */
int is_scan; /* TRUE if *scanf */
int format_num; /* number of format argument */
int first_arg_num; /* number of first arg (zero for varargs) */
} function_format_info;
static function_format_info *function_format_list = NULL;
static void check_format_info PROTO((function_format_info *, tree));
/* Initialize the table of functions to perform format checking on.
The ANSI functions are always checked (whether <stdio.h> is
included or not), since it is common to call printf without
including <stdio.h>. There shouldn't be a problem with this,
since ANSI reserves these function names whether you include the
header file or not. In any case, the checking is harmless. */
void
init_function_format_info ()
{
record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
}
/* Record information for argument format checking. FUNCTION_IDENT is
the identifier node for the name of the function to check (its decl
need not exist yet). IS_SCAN is true for scanf-type format checking;
false indicates printf-style format checking. FORMAT_NUM is the number
of the argument which is the format control string (starting from 1).
FIRST_ARG_NUM is the number of the first actual argument to check
against teh format string, or zero if no checking is not be done
(e.g. for varargs such as vfprintf). */
void
record_function_format (name, assembler_name, is_scan,
format_num, first_arg_num)
tree name;
tree assembler_name;
int is_scan;
int format_num;
int first_arg_num;
{
function_format_info *info;
/* Re-use existing structure if it's there. */
for (info = function_format_list; info; info = info->next)
{
if (info->name == name && info->assembler_name == assembler_name)
break;
}
if (! info)
{
info = (function_format_info *) xmalloc (sizeof (function_format_info));
info->next = function_format_list;
function_format_list = info;
info->name = name;
info->assembler_name = assembler_name;
}
info->is_scan = is_scan;
info->format_num = format_num;
info->first_arg_num = first_arg_num;
}
static char tfaff[] = "too few arguments for format";
/* Check the argument list of a call to printf, scanf, etc.
NAME is the function identifier.
ASSEMBLER_NAME is the function's assembler identifier.
(Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
PARAMS is the list of argument values. */
void
check_function_format (name, assembler_name, params)
tree name;
tree assembler_name;
tree params;
{
function_format_info *info;
/* See if this function is a format function. */
for (info = function_format_list; info; info = info->next)
{
if (info->assembler_name
? (info->assembler_name == assembler_name)
: (info->name == name))
{
/* Yup; check it. */
check_format_info (info, params);
break;
}
}
}
/* Check the argument list of a call to printf, scanf, etc.
INFO points to the function_format_info structure.
PARAMS is the list of argument values. */
static void
check_format_info (info, params)
function_format_info *info;
tree params;
{
int i;
int arg_num;
int suppressed, wide, precise;
int length_char;
int format_char;
int format_length;
tree format_tree;
tree cur_param;
tree cur_type;
tree wanted_type;
tree first_fillin_param;
char *format_chars;
format_char_info *fci;
static char message[132];
char flag_chars[8];
int has_operand_number = 0;
/* Skip to format argument. If the argument isn't available, there's
no work for us to do; prototype checking will catch the problem. */
for (arg_num = 1; ; ++arg_num)
{
if (params == 0)
return;
if (arg_num == info->format_num)
break;
params = TREE_CHAIN (params);
}
format_tree = TREE_VALUE (params);
params = TREE_CHAIN (params);
if (format_tree == 0)
return;
/* We can only check the format if it's a string constant. */
while (TREE_CODE (format_tree) == NOP_EXPR)
format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
if (integer_zerop (format_tree))
{
warning ("null format string");
return;
}
if (TREE_CODE (format_tree) != ADDR_EXPR)
return;
format_tree = TREE_OPERAND (format_tree, 0);
if (TREE_CODE (format_tree) != STRING_CST)
return;
format_chars = TREE_STRING_POINTER (format_tree);
format_length = TREE_STRING_LENGTH (format_tree);
if (format_length <= 1)
warning ("zero-length format string");
if (format_chars[--format_length] != 0)
{
warning ("unterminated format string");
return;
}
/* Skip to first argument to check. */
while (arg_num + 1 < info->first_arg_num)
{
if (params == 0)
return;
params = TREE_CHAIN (params);
++arg_num;
}
first_fillin_param = params;
while (1)
{
int aflag;
if (*format_chars == 0)
{
if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
warning ("embedded `\\0' in format");
if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
warning ("too many arguments for format");
return;
}
if (*format_chars++ != '%')
continue;
if (*format_chars == 0)
{
warning ("spurious trailing `%%' in format");
continue;
}
if (*format_chars == '%')
{
++format_chars;
continue;
}
flag_chars[0] = 0;
suppressed = wide = precise = FALSE;
if (info->is_scan)
{
suppressed = *format_chars == '*';
if (suppressed)
++format_chars;
while (isdigit (*format_chars))
++format_chars;
}
else
{
/* See if we have a number followed by a dollar sign. If we do,
it is an operand number, so set PARAMS to that operand. */
if (*format_chars >= '0' && *format_chars <= '9')
{
char *p = format_chars;
while (*p >= '0' && *p++ <= '9')
;
if (*p == '$')
{
int opnum = atoi (format_chars);
params = first_fillin_param;
format_chars = p + 1;
has_operand_number = 1;
for (i = 1; i < opnum && params != 0; i++)
params = TREE_CHAIN (params);
if (opnum == 0 || params == 0)
{
warning ("operand number out of range in format");
return;
}
}
}
while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
{
if (index (flag_chars, *format_chars) != 0)
{
sprintf (message, "repeated `%c' flag in format",
*format_chars);
warning (message);
}
i = strlen (flag_chars);
flag_chars[i++] = *format_chars++;
flag_chars[i] = 0;
}
/* "If the space and + flags both appear,
the space flag will be ignored." */
if (index (flag_chars, ' ') != 0
&& index (flag_chars, '+') != 0)
warning ("use of both ` ' and `+' flags in format");
/* "If the 0 and - flags both appear,
the 0 flag will be ignored." */
if (index (flag_chars, '0') != 0
&& index (flag_chars, '-') != 0)
warning ("use of both `0' and `-' flags in format");
if (*format_chars == '*')
{
wide = TRUE;
/* "...a field width...may be indicated by an asterisk.
In this case, an int argument supplies the field width..." */
++format_chars;
if (params == 0)
{
warning (tfaff);
return;