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.
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
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. */
27 static void add_hash
PARAMS ((const char *));
28 static int parse_fn_proto
PARAMS ((char *, char *, struct fn_decl
*));
30 #define HASH_SIZE 2503 /* a prime */
31 int hash_tab
[HASH_SIZE
];
41 /* NOTE: If you edit this, also edit lookup_std_proto in fix-header.c !! */
42 i
= hashstr (fname
, strlen (fname
)) % HASH_SIZE
;
49 i
= (i
+1) % HASH_SIZE
;
56 hash_tab
[i
] = next_index
;
61 /* Given a function prototype, fill in the fields of FN.
62 The result is a boolean indicating if a function prototype was found.
64 The input string is modified (trailing NULs are inserted).
65 The fields of FN point to the input string. */
68 parse_fn_proto (start
, end
, fn
)
73 int param_nesting
= 1;
74 char *param_start
, *param_end
, *decl_start
, *name_start
, *name_end
;
77 while (*ptr
== ' ' || *ptr
== '\t') ptr
--;
80 fprintf (stderr
, "Funny input line: %s\n", start
);
83 while (*ptr
== ' ' || *ptr
== '\t') ptr
--;
86 fprintf (stderr
, "Funny input line: %s\n", start
);
93 if (c
== '(' && --param_nesting
== 0)
101 while (*ptr
== ' ' || *ptr
== '\t') ptr
--;
103 if (!ISALNUM ((unsigned char)*ptr
))
106 fprintf (stderr
, "%s: Can't handle this complex prototype: %s\n",
112 while (ISALNUM ((unsigned char)*ptr
) || *ptr
== '_') --ptr
;
114 while (*ptr
== ' ' || *ptr
== '\t') ptr
--;
120 if (strncmp (decl_start
, "typedef ", 8) == 0)
122 if (strncmp (decl_start
, "extern ", 7) == 0)
125 fn
->fname
= name_start
;
126 fn
->rtype
= decl_start
;
127 fn
->params
= param_start
;
131 extern int main
PARAMS ((int, char **));
135 int argc ATTRIBUTE_UNUSED
;
142 struct fn_decl fn_decl
;
144 i
= strlen (argv
[0]);
145 while (i
> 0 && argv
[0][i
-1] != '/') --i
;
146 progname
= &argv
[0][i
];
148 INIT_SSTRING (&linebuf
);
150 fprintf (outf
, "struct fn_decl std_protos[] = {\n");
152 /* A hash table entry of 0 means "unused" so reserve it. */
153 fprintf (outf
, " {\"\", \"\", \"\", 0},\n");
158 int c
= skip_spaces (inf
, ' ');
162 linebuf
.ptr
= linebuf
.base
;
164 c
= read_upto (inf
, &linebuf
, '\n');
165 if (linebuf
.base
[0] == '#') /* skip cpp command */
167 if (linebuf
.base
[0] == '\0') /* skip empty line */
170 if (! parse_fn_proto (linebuf
.base
, linebuf
.ptr
, &fn_decl
))
173 add_hash (fn_decl
.fname
);
175 fprintf (outf
, " {\"%s\", \"%s\", \"%s\", 0},\n",
176 fn_decl
.fname
, fn_decl
.rtype
, fn_decl
.params
);
181 fprintf (outf
, " {0, 0, 0, 0}\n};\n");
184 fprintf (outf
, "#define HASH_SIZE %d\n", HASH_SIZE
);
185 fprintf (outf
, "short hash_tab[HASH_SIZE] = {\n");
186 for (i
= 0; i
< HASH_SIZE
; i
++)
187 fprintf (outf
, " %d,\n", hash_tab
[i
]);
188 fprintf (outf
, "};\n");
190 fprintf (stderr
, "gen-protos: %d entries %d collisions\n",
191 next_index
, collisions
);