]>
Commit | Line | Data |
---|---|---|
38f8b050 JR |
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, 2010 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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 <string.h> | |
23 | #include "hashtab.h" | |
24 | #include "errors.h" | |
25 | ||
26 | struct hook_desc { const char *doc, *type, *name, *param, *init; }; | |
27 | static struct hook_desc hook_array[] = { | |
28 | #define HOOK_VECTOR_1(NAME, FRAGMENT) \ | |
29 | { 0, 0, #NAME, 0, 0 }, | |
30 | #define DEFHOOKPOD(NAME, DOC, TYPE, INIT) \ | |
31 | { DOC, #TYPE, HOOK_PREFIX #NAME, 0, #INIT }, | |
32 | #define DEFHOOK(NAME, DOC, TYPE, PARAMS, INIT) \ | |
33 | { DOC, #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT }, | |
34 | #define DEFHOOK_UNDOC(NAME, DOC, TYPE, PARAMS, INIT) \ | |
35 | { "*", #TYPE, HOOK_PREFIX #NAME, #PARAMS, #INIT }, | |
36 | #include "target.def" | |
37 | #undef DEFHOOK | |
38 | }; | |
39 | ||
40 | /* For each @Fcode in the 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) | |
45 | { | |
46 | const char *end = strstr (doc, "\n\n"); | |
47 | const char *fcode; | |
48 | ||
49 | while ((fcode = strstr (doc, "@Fcode{")) && (!end || fcode < end)) | |
50 | { | |
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", doc - fcode, fcode); | |
56 | doc = fcode; | |
57 | } | |
58 | } | |
59 | ||
60 | /* Return an upper-case copy of IN. */ | |
61 | static char * | |
62 | upstrdup (const char *in) | |
63 | { | |
64 | char *p, *ret = xstrdup (in); | |
65 | for (p = ret; *p; p++) | |
66 | *p = TOUPPER (*p); | |
67 | return ret; | |
68 | } | |
69 | ||
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 | |
73 | { | |
74 | char *name; | |
75 | int pos; | |
76 | }; | |
77 | ||
78 | static hashval_t | |
79 | s_hook_hash (const void *p) | |
80 | { | |
81 | const struct s_hook *s_hook = (const struct s_hook *)p; | |
82 | return htab_hash_string (s_hook->name); | |
83 | } | |
84 | ||
85 | static int | |
86 | s_hook_eq_p (const void *p1, const void *p2) | |
87 | { | |
88 | return (strcmp (((const struct s_hook *) p1)->name, | |
89 | ((const struct s_hook *) p2)->name) == 0); | |
90 | } | |
91 | ||
92 | /* Read the documentation file with name IN_FNAME, perform substitutions | |
93 | to incorporate informtion 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) | |
117 | { | |
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; | |
122 | bool found_start = false; | |
123 | ||
124 | /* Enter all the start hooks in start_hooks. */ | |
125 | f = fopen (in_fname, "r"); | |
126 | if (!f) | |
127 | { | |
128 | perror (""); | |
129 | fatal ("Couldn't open input file"); | |
130 | } | |
131 | while (fscanf (f, "%*[^@]"), buf[0] = '\0', | |
132 | fscanf (f, "@%5[^ \n]", buf) != EOF) | |
133 | { | |
134 | void **p; | |
135 | struct s_hook *shp; | |
136 | ||
137 | if (strcmp (buf, "hook") != 0) | |
138 | continue; | |
139 | buf[0] = '\0'; | |
140 | fscanf (f, "%999s", buf); | |
141 | shp = XNEW (struct s_hook); | |
142 | shp->name = upstrdup (buf); | |
143 | shp->pos = -1; | |
144 | p = htab_find_slot (start_hooks, shp, INSERT); | |
145 | if (*p != HTAB_EMPTY_ENTRY) | |
146 | fatal ("Duplicate placement for hook %s\n", shp->name); | |
147 | *(struct s_hook **) p = shp; | |
148 | } | |
149 | fclose (f); | |
150 | /* For each hook in hook_array, if it is a start hook, store its position. */ | |
151 | for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++) | |
152 | { | |
153 | struct s_hook sh, *shp; | |
154 | void *p; | |
155 | ||
156 | if (!hook_array[i].doc || strcmp (hook_array[i].doc, "*") == 0) | |
157 | continue; | |
158 | sh.name = upstrdup (hook_array[i].name); | |
159 | p = htab_find (start_hooks, &sh); | |
160 | if (p) | |
161 | { | |
162 | shp = (struct s_hook *) p; | |
163 | if (shp->pos >= 0) | |
164 | fatal ("Duplicate hook %s\n", sh.name); | |
165 | shp->pos = i; | |
166 | found_start = true; | |
167 | } | |
168 | else if (!found_start) | |
169 | fatal ("No place specified to document hook %s\n", sh.name); | |
170 | free (sh.name); | |
171 | } | |
172 | /* Copy input file to stdout, substituting @hook directives with the | |
173 | corresponding hook documentation sequences. */ | |
174 | f = fopen (in_fname, "r"); | |
175 | if (!f) | |
176 | { | |
177 | perror (""); | |
178 | fatal ("Couldn't open input file"); | |
179 | } | |
180 | for (;;) | |
181 | { | |
182 | struct s_hook sh, *shp; | |
183 | int c = getc (f); | |
184 | char *name; | |
185 | ||
186 | if (c == EOF) | |
187 | break; | |
188 | if (c != '@') | |
189 | { | |
190 | putchar (c); | |
191 | continue; | |
192 | } | |
193 | buf[0] = '\0'; | |
194 | fscanf (f, "%5[^ \n]", buf); | |
195 | if (strcmp (buf, "hook") != 0) | |
196 | { | |
197 | printf ("@%s", buf); | |
198 | continue; | |
199 | } | |
200 | fscanf (f, "%999s", buf); | |
201 | sh.name = name = upstrdup (buf); | |
202 | shp = (struct s_hook *) htab_find (start_hooks, &sh); | |
203 | if (!shp || shp->pos < 0) | |
204 | fatal ("No documentation for hook %s\n", sh.name); | |
205 | i = shp->pos; | |
206 | do | |
207 | { | |
208 | const char *q, *e; | |
209 | const char *deftype; | |
210 | const char *doc, *fcode, *p_end; | |
211 | ||
212 | /* A leading '*' means to output the documentation string without | |
213 | further processing. */ | |
214 | if (*hook_array[i].doc == '*') | |
215 | printf ("%s", hook_array[i].doc + 1); | |
216 | else | |
217 | { | |
218 | if (i != shp->pos) | |
219 | printf ("\n\n"); | |
220 | emit_findices (hook_array[i].doc, name); | |
221 | ||
222 | /* Print header. Function-valued hooks have a parameter list, | |
223 | unlike POD-valued ones. */ | |
224 | deftype = hook_array[i].param ? "deftypefn" : "deftypevr"; | |
225 | printf ("@%s {Target Hook} ", deftype); | |
226 | if (strchr (hook_array[i].type, ' ')) | |
227 | printf ("{%s}", hook_array[i].type); | |
228 | else | |
229 | printf ("%s", hook_array[i].type); | |
230 | printf (" %s", name); | |
231 | if (hook_array[i].param) | |
232 | { | |
233 | /* Print the parameter list, with the parameter names | |
234 | enclosed in @var{}. */ | |
235 | printf (" "); | |
236 | for (q = hook_array[i].param; (e = strpbrk (q, " *,)")); | |
237 | q = e + 1) | |
238 | /* Type names like 'int' are followed by a space, sometimes | |
239 | also by '*'. 'void' should appear only in "(void)". */ | |
240 | if (*e == ' ' || *e == '*' || *q == '(') | |
241 | printf ("%.*s", e - q + 1, q); | |
242 | else | |
243 | printf ("@var{%.*s}%c", e - q, q, *e); | |
244 | } | |
245 | /* POD-valued hooks sometimes come in groups with common | |
246 | documentation.*/ | |
247 | for (j = i + 1; | |
248 | j < (int) (sizeof hook_array / sizeof hook_array[0]) | |
249 | && hook_array[j].doc == 0 && hook_array[j].type; j++) | |
250 | { | |
251 | char *namex = upstrdup (hook_array[j].name); | |
252 | ||
253 | printf ("\n@%sx {Target Hook} {%s} %s", | |
254 | deftype, hook_array[j].type, namex); | |
255 | } | |
256 | if (hook_array[i].doc[0]) | |
257 | { | |
258 | printf ("\n"); | |
259 | /* Print each documentation paragraph in turn. */ | |
260 | for (doc = hook_array[i].doc; *doc; doc = p_end) | |
261 | { | |
262 | /* Find paragraph end. */ | |
263 | p_end = strstr (doc, "\n\n"); | |
264 | p_end = (p_end ? p_end + 2 : doc + strlen (doc)); | |
265 | /* Print paragraph, emitting @Fcode as @code. */ | |
266 | for (; (fcode = strstr (doc, "@Fcode{")) && fcode < p_end; | |
267 | doc = fcode + 2) | |
268 | printf ("%.*s@", fcode - doc, doc); | |
269 | printf ("%.*s", p_end - doc, doc); | |
270 | /* Emit function indices for next paragraph. */ | |
271 | emit_findices (p_end, name); | |
272 | } | |
273 | printf ("\n@end %s", deftype); | |
274 | } | |
275 | } | |
276 | if (++i >= (int) (sizeof hook_array / sizeof hook_array[0]) | |
277 | || !hook_array[i].doc) | |
278 | break; | |
279 | free (name); | |
280 | sh.name = name = upstrdup (hook_array[i].name); | |
281 | } | |
282 | while (!htab_find (start_hooks, &sh)); | |
283 | free (name); | |
284 | } | |
285 | } | |
286 | ||
287 | /* Emit #defines to stdout (this will be redirected to generate | |
288 | target-hook-def.h) which set target hooks initializer macros | |
289 | to their default values. */ | |
290 | static void | |
291 | emit_init_macros (void) | |
292 | { | |
293 | int i; | |
294 | const int MAX_NEST = 2; | |
295 | int print_nest, nest = 0; | |
296 | ||
297 | for (print_nest = 0; print_nest <= MAX_NEST; print_nest++) | |
298 | { | |
299 | for (i = 0; i < (int) (sizeof hook_array / sizeof hook_array[0]); i++) | |
300 | { | |
301 | char *name = upstrdup (hook_array[i].name); | |
302 | ||
303 | if (!hook_array[i].type) | |
304 | { | |
305 | if (*name) | |
306 | { | |
307 | if (nest && nest == print_nest) | |
308 | printf (" %s, \\\n", name); | |
309 | nest++; | |
310 | if (nest > MAX_NEST) | |
311 | fatal ("Unexpected nesting of %s\n", name); | |
312 | if (nest == print_nest) | |
313 | printf ("\n#define %s \\\n { \\\n", name); | |
314 | } | |
315 | else | |
316 | { | |
317 | if (nest == print_nest) | |
318 | printf (" }\n"); | |
319 | nest--; | |
320 | } | |
321 | continue; | |
322 | } | |
323 | if (0 == print_nest) | |
324 | { | |
325 | /* Output default definitions of target hooks. */ | |
326 | printf ("#ifndef %s\n#define %s %s\n#endif\n", | |
327 | name, name, hook_array[i].init); | |
328 | } | |
329 | if (nest == print_nest) | |
330 | printf (" %s, \\\n", name); | |
331 | } | |
332 | } | |
333 | } | |
334 | ||
335 | int | |
336 | main (int argc, char **argv) | |
337 | { | |
338 | if (argc >= 2) | |
339 | emit_documentation (argv[1]); | |
340 | else | |
341 | emit_init_macros (); | |
342 | return 0; | |
343 | } |