configure.ac: GCC_NO_EXECUTABLES was supposed to be commented in the patch from 3...
[official-gcc.git] / gcc / gen-protos.c
blobee8f0a17cd18ed6ebf2e841b7d73a462fcebdf40
1 /* gen-protos.c - massages a list of prototypes, for use by fixproto.
2 Copyright (C) 1993, 1994, 1995, 1996, 1998,
3 1999, 2003 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #include "bconfig.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "scan.h"
24 #undef abort
26 int verbose = 0;
27 const char *progname;
29 static void add_hash (const char *);
30 static int parse_fn_proto (char *, char *, struct fn_decl *);
32 #define HASH_SIZE 2503 /* a prime */
33 int hash_tab[HASH_SIZE];
34 int next_index;
35 int collisions;
37 static void
38 add_hash (const char *fname)
40 int i, i0;
42 /* NOTE: If you edit this, also edit lookup_std_proto in fix-header.c !! */
43 i = hashstr (fname, strlen (fname)) % HASH_SIZE;
44 i0 = i;
45 if (hash_tab[i] != 0)
47 collisions++;
48 for (;;)
50 i = (i+1) % HASH_SIZE;
51 if (i == i0)
52 abort ();
53 if (hash_tab[i] == 0)
54 break;
57 hash_tab[i] = next_index;
59 next_index++;
62 /* Given a function prototype, fill in the fields of FN.
63 The result is a boolean indicating if a function prototype was found.
65 The input string is modified (trailing NULs are inserted).
66 The fields of FN point to the input string. */
68 static int
69 parse_fn_proto (char *start, char *end, struct fn_decl *fn)
71 char *ptr;
72 int param_nesting = 1;
73 char *param_start, *param_end, *decl_start, *name_start, *name_end;
75 ptr = end - 1;
76 while (*ptr == ' ' || *ptr == '\t') ptr--;
77 if (*ptr-- != ';')
79 fprintf (stderr, "Funny input line: %s\n", start);
80 return 0;
82 while (*ptr == ' ' || *ptr == '\t') ptr--;
83 if (*ptr != ')')
85 fprintf (stderr, "Funny input line: %s\n", start);
86 return 0;
88 param_end = ptr;
89 for (;;)
91 int c = *--ptr;
92 if (c == '(' && --param_nesting == 0)
93 break;
94 else if (c == ')')
95 param_nesting++;
97 param_start = ptr+1;
99 ptr--;
100 while (*ptr == ' ' || *ptr == '\t') ptr--;
102 if (!ISALNUM ((unsigned char)*ptr))
104 if (verbose)
105 fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
106 progname, start);
107 return 0;
109 name_end = ptr+1;
111 while (ISIDNUM (*ptr))
112 --ptr;
113 name_start = ptr+1;
114 while (*ptr == ' ' || *ptr == '\t') ptr--;
115 ptr[1] = 0;
116 *param_end = 0;
117 *name_end = 0;
119 decl_start = start;
120 if (strncmp (decl_start, "typedef ", 8) == 0)
121 return 0;
122 if (strncmp (decl_start, "extern ", 7) == 0)
123 decl_start += 7;
125 fn->fname = name_start;
126 fn->rtype = decl_start;
127 fn->params = param_start;
128 return 1;
132 main (int argc ATTRIBUTE_UNUSED, char **argv)
134 FILE *inf = stdin;
135 FILE *outf = stdout;
136 int i;
137 sstring linebuf;
138 struct fn_decl fn_decl;
140 i = strlen (argv[0]);
141 while (i > 0 && argv[0][i-1] != '/') --i;
142 progname = &argv[0][i];
144 INIT_SSTRING (&linebuf);
146 fprintf (outf, "struct fn_decl std_protos[] = {\n");
148 /* A hash table entry of 0 means "unused" so reserve it. */
149 fprintf (outf, " {\"\", \"\", \"\", 0},\n");
150 next_index = 1;
152 for (;;)
154 int c = skip_spaces (inf, ' ');
156 if (c == EOF)
157 break;
158 linebuf.ptr = linebuf.base;
159 ungetc (c, inf);
160 c = read_upto (inf, &linebuf, '\n');
161 if (linebuf.base[0] == '#') /* skip cpp command */
162 continue;
163 if (linebuf.base[0] == '\0') /* skip empty line */
164 continue;
166 if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl))
167 continue;
169 add_hash (fn_decl.fname);
171 fprintf (outf, " {\"%s\", \"%s\", \"%s\", 0},\n",
172 fn_decl.fname, fn_decl.rtype, fn_decl.params);
174 if (c == EOF)
175 break;
177 fprintf (outf, " {0, 0, 0, 0}\n};\n");
180 fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE);
181 fprintf (outf, "short hash_tab[HASH_SIZE] = {\n");
182 for (i = 0; i < HASH_SIZE; i++)
183 fprintf (outf, " %d,\n", hash_tab[i]);
184 fprintf (outf, "};\n");
186 fprintf (stderr, "gen-protos: %d entries %d collisions\n",
187 next_index, collisions);
189 return 0;