xref: /openbsd/gnu/usr.bin/gcc/gcc/gen-protos.c (revision 09467b48)
1 /* gen-protos.c - massages a list of prototypes, for use by fixproto.
2    Copyright (C) 1993, 1994, 1995, 1996, 1998,
3    1999 Free Software Foundation, Inc.
4 
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.
9 
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.
14 
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.  */
18 
19 #include "hconfig.h"
20 #include "system.h"
21 #include "scan.h"
22 #undef abort
23 
24 int verbose = 0;
25 const char *progname;
26 
27 static void add_hash		PARAMS ((const char *));
28 static int parse_fn_proto	PARAMS ((char *, char *, struct fn_decl *));
29 
30 #define HASH_SIZE 2503 /* a prime */
31 int hash_tab[HASH_SIZE];
32 int next_index;
33 int collisions;
34 
35 static void
36 add_hash (fname)
37      const char *fname;
38 {
39   int i, i0;
40 
41   /* NOTE:  If you edit this, also edit lookup_std_proto in fix-header.c !! */
42   i = hashstr (fname, strlen (fname)) % HASH_SIZE;
43   i0 = i;
44   if (hash_tab[i] != 0)
45     {
46       collisions++;
47       for (;;)
48 	{
49 	  i = (i+1) % HASH_SIZE;
50 	  if (i == i0)
51 	    abort ();
52 	  if (hash_tab[i] == 0)
53 	    break;
54 	}
55     }
56   hash_tab[i] = next_index;
57 
58   next_index++;
59 }
60 
61 /* Given a function prototype, fill in the fields of FN.
62    The result is a boolean indicating if a function prototype was found.
63 
64    The input string is modified (trailing NULs are inserted).
65    The fields of FN point to the input string.  */
66 
67 static int
68 parse_fn_proto (start, end, fn)
69      char *start, *end;
70      struct fn_decl *fn;
71 {
72   char *ptr;
73   int param_nesting = 1;
74   char *param_start, *param_end, *decl_start, *name_start, *name_end;
75 
76   ptr = end - 1;
77   while (*ptr == ' ' || *ptr == '\t') ptr--;
78   if (*ptr-- != ';')
79     {
80       fprintf (stderr, "Funny input line: %s\n", start);
81       return 0;
82     }
83   while (*ptr == ' ' || *ptr == '\t') ptr--;
84   if (*ptr != ')')
85     {
86       fprintf (stderr, "Funny input line: %s\n", start);
87       return 0;
88     }
89   param_end = ptr;
90   for (;;)
91     {
92       int c = *--ptr;
93       if (c == '(' && --param_nesting == 0)
94 	break;
95       else if (c == ')')
96 	param_nesting++;
97     }
98   param_start = ptr+1;
99 
100   ptr--;
101   while (*ptr == ' ' || *ptr == '\t') ptr--;
102 
103   if (!ISALNUM ((unsigned char)*ptr))
104     {
105       if (verbose)
106 	fprintf (stderr, "%s: Can't handle this complex prototype: %s\n",
107 		 progname, start);
108       return 0;
109     }
110   name_end = ptr+1;
111 
112   while (ISIDNUM (*ptr))
113     --ptr;
114   name_start = ptr+1;
115   while (*ptr == ' ' || *ptr == '\t') ptr--;
116   ptr[1] = 0;
117   *param_end = 0;
118   *name_end = 0;
119 
120   decl_start = start;
121   if (strncmp (decl_start, "typedef ", 8) == 0)
122     return 0;
123   if (strncmp (decl_start, "extern ", 7) == 0)
124     decl_start += 7;
125 
126   fn->fname = name_start;
127   fn->rtype = decl_start;
128   fn->params = param_start;
129   return 1;
130 }
131 
132 extern int main PARAMS ((int, char **));
133 
134 int
135 main (argc, argv)
136      int argc ATTRIBUTE_UNUSED;
137      char **argv;
138 {
139   FILE *inf = stdin;
140   FILE *outf = stdout;
141   int i;
142   sstring linebuf;
143   struct fn_decl fn_decl;
144 
145   i = strlen (argv[0]);
146   while (i > 0 && argv[0][i-1] != '/') --i;
147   progname = &argv[0][i];
148 
149   INIT_SSTRING (&linebuf);
150 
151   fprintf (outf, "struct fn_decl std_protos[] = {\n");
152 
153   /* A hash table entry of 0 means "unused" so reserve it.  */
154   fprintf (outf, "  {\"\", \"\", \"\", 0},\n");
155   next_index = 1;
156 
157   for (;;)
158     {
159       int c = skip_spaces (inf, ' ');
160 
161       if (c == EOF)
162 	break;
163       linebuf.ptr = linebuf.base;
164       ungetc (c, inf);
165       c = read_upto (inf, &linebuf, '\n');
166       if (linebuf.base[0] == '#') /* skip cpp command */
167 	continue;
168       if (linebuf.base[0] == '\0') /* skip empty line */
169 	continue;
170 
171       if (! parse_fn_proto (linebuf.base, linebuf.ptr, &fn_decl))
172 	continue;
173 
174       add_hash (fn_decl.fname);
175 
176       fprintf (outf, "  {\"%s\", \"%s\", \"%s\", 0},\n",
177 	       fn_decl.fname, fn_decl.rtype, fn_decl.params);
178 
179       if (c == EOF)
180 	break;
181     }
182   fprintf (outf, "  {0, 0, 0, 0}\n};\n");
183 
184 
185   fprintf (outf, "#define HASH_SIZE %d\n", HASH_SIZE);
186   fprintf (outf, "short hash_tab[HASH_SIZE] = {\n");
187   for (i = 0; i < HASH_SIZE; i++)
188     fprintf (outf, "  %d,\n", hash_tab[i]);
189   fprintf (outf, "};\n");
190 
191   fprintf (stderr, "gen-protos: %d entries %d collisions\n",
192 	   next_index, collisions);
193 
194   return 0;
195 }
196