* oacc-parallel.c (GOACC_parallel): Move variadic handling into
[official-gcc.git] / gcc / genhooks.c
blobb191fb5a0e257e71d3c8d0f738549c19769d1f20
1 /* Process target.def to create initialization macros definition in
2 target-hooks-def.h and documentation in target-hooks.texi.
3 Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20 #include "bconfig.h"
21 #include "system.h"
22 #include "errors.h"
24 struct hook_desc { const char *doc, *type, *name, *param, *init, *docname; };
25 static struct hook_desc hook_array[] = {
26 #define HOOK_VECTOR_1(NAME, FRAGMENT) \
27 { 0, 0, #NAME, 0, 0, HOOK_TYPE },
28 #define DEFHOOKPOD(NAME, DOC, TYPE, INIT) \
29 { DOC, #TYPE, HOOK_PREFIX #NAME, 0, #INIT, HOOK_TYPE },
30 #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) \
31 { DOC, #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT, HOOK_TYPE },
32 #define DEFHOOK_UNDOC(NAME, DOC, TYPE, PARAMS, INIT) \
33 { "*", #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT, HOOK_TYPE },
34 #include "target.def"
35 #include "c-family/c-target.def"
36 #include "common/common-target.def"
37 #undef DEFHOOK
40 /* For each @Fcode in the first paragraph of the documentation string DOC,
41 print an @findex directive. HOOK_NAME is the name of the hook this bit of
42 documentation pertains to. */
43 static void
44 emit_findices (const char *doc, const char *hook_name)
46 const char *end = strstr (doc, "\n\n");
47 const char *fcode;
49 while ((fcode = strstr (doc, "@Fcode{")) && (!end || fcode < end))
51 fcode += strlen ("@Fcode{");
52 doc = strchr (fcode, '}');
53 if (!doc)
54 fatal ("Malformed @Fcode for hook %s\n", hook_name);
55 printf ("@findex %.*s\n", (int) (doc - fcode), fcode);
56 doc = fcode;
60 /* Return an upper-case copy of IN. */
61 static char *
62 upstrdup (const char *in)
64 char *p, *ret = xstrdup (in);
65 for (p = ret; *p; p++)
66 *p = TOUPPER (*p);
67 return ret;
70 /* Struct for 'start hooks' which start a sequence of consecutive hooks
71 that are defined in target.def and to be documented in tm.texi. */
72 struct s_hook
74 char *name;
75 int pos;
78 static hashval_t
79 s_hook_hash (const void *p)
81 const struct s_hook *s_hook = (const struct s_hook *)p;
82 return htab_hash_string (s_hook->name);
85 static int
86 s_hook_eq_p (const void *p1, const void *p2)
88 return (strcmp (((const struct s_hook *) p1)->name,
89 ((const struct s_hook *) p2)->name) == 0);
92 /* Read the documentation file with name IN_FNAME, perform substitutions
93 to incorporate information from hook_array, and emit the result on stdout.
94 Hooks defined with DEFHOOK / DEFHOOKPOD are emitted at the place of a
95 matching @hook in the input file; if there is no matching @hook, the
96 hook is emitted after the hook that precedes it in target.def .
97 Usually, the emitted hook documentation starts with the hook
98 signature, followed by the string from the doc field.
99 The documentation is bracketed in @deftypefn / @deftypevr and a matching
100 @end.
101 While emitting the doc field, @Fcode is translated to @code, and an
102 @findex entry is added to the affected paragraph.
103 If the doc field starts with '*', the leading '*' is stripped, and the doc
104 field is otherwise emitted unaltered; no function signature/
105 @deftypefn/deftypevr/@end is emitted.
106 In particular, a doc field of "*" means not to emit any ocumentation for
107 this target.def / hook_array entry at all (there might be documentation
108 for this hook in the file named IN_FNAME, though).
109 A doc field of 0 is used to append the hook signature after the previous
110 hook's signture, so that one description can be used for a group of hooks.
111 When the doc field is "", @deftypefn/@deftypevr and the hook signature
112 is emitted, but not the matching @end. This allows all the free-form
113 documentation to be placed in IN_FNAME, to work around GPL/GFDL
114 licensing incompatibility issues. */
115 static void
116 emit_documentation (const char *in_fname)
118 int i, j;
119 char buf[1000];
120 htab_t start_hooks = htab_create (99, s_hook_hash, s_hook_eq_p, (htab_del) 0);
121 FILE *f;
123 /* Enter all the start hooks in start_hooks. */
124 f = fopen (in_fname, "r");
125 if (!f)
127 perror ("");
128 fatal ("Couldn't open input file");
130 while (fscanf (f, "%*[^@]"), buf[0] = '\0',
131 fscanf (f, "@%5[^ \n]", buf) != EOF)
133 void **p;
134 struct s_hook *shp;
136 if (strcmp (buf, "hook") != 0)
137 continue;
138 buf[0] = '\0';
139 fscanf (f, "%999s", buf);
140 shp = XNEW (struct s_hook);
141 shp->name = upstrdup (buf);
142 shp->pos = -1;
143 p = htab_find_slot (start_hooks, shp, INSERT);
144 if (*p != HTAB_EMPTY_ENTRY)
145 fatal ("Duplicate placement for hook %s\n", shp->name);
146 *(struct s_hook **) p = shp;
148 fclose (f);
149 /* For each hook in hook_array, if it is a start hook, store its position. */
150 for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++)
152 struct s_hook sh, *shp;
153 void *p;
155 if (!hook_array[i].doc || strcmp (hook_array[i].doc, "*") == 0)
156 continue;
157 sh.name = upstrdup (hook_array[i].name);
158 p = htab_find (start_hooks, &sh);
159 if (p)
161 shp = (struct s_hook *) p;
162 if (shp->pos >= 0)
163 fatal ("Duplicate hook %s\n", sh.name);
164 shp->pos = i;
166 else
167 fatal ("No place specified to document hook %s\n", sh.name);
168 free (sh.name);
170 /* Copy input file to stdout, substituting @hook directives with the
171 corresponding hook documentation sequences. */
172 f = fopen (in_fname, "r");
173 if (!f)
175 perror ("");
176 fatal ("Couldn't open input file");
178 for (;;)
180 struct s_hook sh, *shp;
181 int c = getc (f);
182 char *name;
184 if (c == EOF)
185 break;
186 if (c != '@')
188 putchar (c);
189 continue;
191 buf[0] = '\0';
192 fscanf (f, "%5[^ \n]", buf);
193 if (strcmp (buf, "hook") != 0)
195 printf ("@%s", buf);
196 continue;
198 fscanf (f, "%999s", buf);
199 sh.name = name = upstrdup (buf);
200 shp = (struct s_hook *) htab_find (start_hooks, &sh);
201 if (!shp || shp->pos < 0)
202 fatal ("No documentation for hook %s\n", sh.name);
203 i = shp->pos;
206 const char *q, *e;
207 const char *deftype;
208 const char *doc, *fcode, *p_end;
210 /* A leading '*' means to output the documentation string without
211 further processing. */
212 if (*hook_array[i].doc == '*')
213 printf ("%s", hook_array[i].doc + 1);
214 else
216 if (i != shp->pos)
217 printf ("\n\n");
218 emit_findices (hook_array[i].doc, name);
220 /* Print header. Function-valued hooks have a parameter list,
221 unlike POD-valued ones. */
222 deftype = hook_array[i].param ? "deftypefn" : "deftypevr";
223 printf ("@%s {%s} ", deftype, hook_array[i].docname);
224 if (strchr (hook_array[i].type, ' '))
225 printf ("{%s}", hook_array[i].type);
226 else
227 printf ("%s", hook_array[i].type);
228 printf (" %s", name);
229 if (hook_array[i].param)
231 /* Print the parameter list, with the parameter names
232 enclosed in @var{}. */
233 printf (" ");
234 for (q = hook_array[i].param; (e = strpbrk (q, " *,)"));
235 q = e + 1)
236 /* Type names like 'int' are followed by a space, sometimes
237 also by '*'. 'void' should appear only in "(void)". */
238 if (*e == ' ' || *e == '*' || *q == '(')
239 printf ("%.*s", (int) (e - q + 1), q);
240 else
241 printf ("@var{%.*s}%c", (int) (e - q), q, *e);
243 /* POD-valued hooks sometimes come in groups with common
244 documentation.*/
245 for (j = i + 1;
246 j < (int) (sizeof hook_array / sizeof hook_array[0])
247 && hook_array[j].doc == 0 && hook_array[j].type; j++)
249 char *namex = upstrdup (hook_array[j].name);
251 printf ("\n@%sx {%s} {%s} %s",
252 deftype, hook_array[j].docname,
253 hook_array[j].type, namex);
255 if (hook_array[i].doc[0])
257 printf ("\n");
258 /* Print each documentation paragraph in turn. */
259 for (doc = hook_array[i].doc; *doc; doc = p_end)
261 /* Find paragraph end. */
262 p_end = strstr (doc, "\n\n");
263 p_end = (p_end ? p_end + 2 : doc + strlen (doc));
264 /* Print paragraph, emitting @Fcode as @code. */
265 for (; (fcode = strstr (doc, "@Fcode{")) && fcode < p_end;
266 doc = fcode + 2)
267 printf ("%.*s@", (int) (fcode - doc), doc);
268 printf ("%.*s", (int) (p_end - doc), doc);
269 /* Emit function indices for next paragraph. */
270 emit_findices (p_end, name);
272 printf ("\n@end %s", deftype);
275 if (++i >= (int) (sizeof hook_array / sizeof hook_array[0])
276 || !hook_array[i].doc)
277 break;
278 free (name);
279 sh.name = name = upstrdup (hook_array[i].name);
281 while (!htab_find (start_hooks, &sh));
282 free (name);
286 /* Emit #defines to stdout (this will be redirected to generate
287 target-hook-def.h) which set target hooks initializer macros
288 to their default values. These should only be emitted for hooks
289 whose type is given by DOCNAME. */
290 static void
291 emit_init_macros (const char *docname)
293 int i;
294 const int MAX_NEST = 2;
295 int print_nest, nest = 0;
297 for (print_nest = 0; print_nest <= MAX_NEST; print_nest++)
299 for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++)
301 char *name = upstrdup (hook_array[i].name);
303 if (strcmp (hook_array[i].docname, docname) != 0)
304 continue;
306 if (!hook_array[i].type)
308 if (*name)
310 if (nest && nest == print_nest)
311 printf (" %s, \\\n", name);
312 nest++;
313 if (nest > MAX_NEST)
314 fatal ("Unexpected nesting of %s\n", name);
315 if (nest == print_nest)
316 printf ("\n#define %s \\\n { \\\n", name);
318 else
320 if (nest == print_nest)
321 printf (" }\n");
322 nest--;
324 continue;
326 if (0 == print_nest)
328 /* Output default definitions of target hooks. */
329 printf ("#ifndef %s\n#define %s %s\n#endif\n",
330 name, name, hook_array[i].init);
332 if (nest == print_nest)
333 printf (" %s, \\\n", name);
339 main (int argc, char **argv)
341 progname = "genhooks";
343 if (argc >= 3)
344 emit_documentation (argv[2]);
345 else
346 emit_init_macros (argv[1]);
347 return 0;