Initial revision
[official-gcc.git] / gcc / fix-header.c
blob0908fa70a5900cd5b3d90adb013b319c2328c18a
1 /* patch-header.c - Make C header file suitable for C++.
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* This program massages a system include file (such as stdio.h),
19 into a form more conformant with ANSI/POSIX, and more suitable for C++:
21 * extern "C" { ... } braces are added (inside #ifndef __cplusplus),
22 if they seem to be needed. These prevcnt C++ compilers from name
23 mangling the functions inside the braces.
25 * If an old-style incomplete function declaration is seen (without
26 an argument list), and it is a "standard" function listed in
27 the file sys-protos.h (and with a non-empty argument list), then
28 the declaration is converted to a complete prototype by replacing
29 the empty parameter list with the argument lust from sys-protos.h.
31 * The program can be given a list of (names of) required standard
32 functions (such as fclose for stdio.h). If a reqquired function
33 is not seen in the input, then a prototype for it will be
34 written to the output.
36 * If all of the non-comment code of the original file is protected
37 against multiple inclusion:
38 #ifndef FOO
39 #define FOO
40 <body of include file>
41 #endif
42 then extra matter added to the include file is placed inside the <body>.
44 * If the input file is OK (nothing needs to be done);
45 the output file is not written (nor removed if it exists).
47 There are also some special actions that are done for certain
48 well-known standard include files:
50 * If argv[1] is "sys/stat.h", the Posix.1 macros
51 S_ISBLK, S_ISCHR, S_ISDIR, S_ISFIFO, S_ISLNK, S_ISREG are added if
52 they were missing, and the corresponding "traditional" S_IFxxx
53 macros were defined.
55 * If argv[1] is "errno.h", errno is declared if it was missing.
57 * TODO: The input file should be read complete into memory, because:
58 a) it needs to be scanned twice anyway, and
59 b) it would be nice to allow update in place.
61 Usage:
62 patch-header FOO.H INFILE.H OUTFILE.H REQUIRED_FUNCS <SCAN-FILE
63 where:
64 * FOO.H is the relative file name of the include file,
65 as it would be #include'd by a C file. (E.g. stdio.h)
66 * INFILE.H is a full pathname for the input file (e.g. /usr/include/stdio.h)
67 * OUTFILE.H is the full pathname for where to write the output file,
68 if anything needs to be done. (e.g. ./include/stdio.h)
69 * SCAN-FILE is the output of the scan-decls program.
70 * REQUIRED_FUNCS is a list of required function (e.g. fclose for stdio.h).
72 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
74 #include <stdio.h>
75 #include <ctype.h>
76 #include "obstack.h"
77 #include "scan.h"
79 extern char *strcpy();
80 sstring buf;
81 int verbose = 0;
82 int partial_count = 0;
83 int missing_extern_C_count = 0;
84 int missing_extra_stuff = 0;
86 #include "xsys-protos.h"
88 /* Certain standard files get extra treatment */
90 enum special_file
92 no_special,
93 errno_special,
94 sys_stat_special
97 enum special_file special_file_handling = no_special;
99 /* The following are only used when handling sys/stat.h */
100 /* They are set if the corresponding macro has been seen. */
101 int seen_S_IFBLK = 0, seen_S_ISBLK = 0;
102 int seen_S_IFCHR = 0, seen_S_ISCHR = 0;
103 int seen_S_IFDIR = 0, seen_S_ISDIR = 0;
104 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
105 int seen_S_IFLNK = 0, seen_S_ISLNK = 0;
106 int seen_S_IFREG = 0, seen_S_ISREG = 0;
108 /* The following are only used when handling errno.h */
109 int seen_errno = 0;
111 /* Wrapper around free, to avoid prototype clashes. */
113 void xfree (ptr)
114 char *ptr;
116 free(ptr);
119 #define obstack_chunk_alloc xmalloc
120 #define obstack_chunk_free xfree
121 struct obstack scan_file_obstack;
123 /* NOTE: If you edit this, also edit gen-protos.c !! */
124 struct fn_decl *
125 lookup_std_proto (name)
126 char *name;
128 int i = hash(name) % HASH_SIZE;
129 int i0 = i;
130 for (;;)
132 struct fn_decl *fn;
133 if (hash_tab[i] == 0)
134 return NULL;
135 fn = &std_protos[hash_tab[i]];
136 if (strcmp (fn->fname, name) == 0)
137 return fn;
138 i = (i+1) % HASH_SIZE;
139 if (i == i0)
140 abort();
144 char *inc_filename;
145 int inc_filename_length;
146 char *progname = "patch-header";
147 FILE *outf;
148 sstring buf;
149 sstring line;
151 int lbrac_line, rbrac_line;
153 char **required_functions;
154 int required_unseen_count;
156 int
157 write_lbrac ()
159 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
161 if (partial_count)
163 fprintf (outf, "#ifndef _PARAMS\n");
164 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
165 fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
166 fprintf (outf, "#else\n");
167 fprintf (outf, "#define _PARAMS(ARGS) ()\n");
168 fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
172 struct partial_proto
174 struct partial_proto *next;
175 char *fname; /* name of function */
176 char *rtype; /* return type */
177 struct fn_decl *fn;
178 int line_seen;
181 struct partial_proto *partial_proto_list = NULL;
183 struct partial_proto required_dummy_proto;
184 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
185 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
186 #define CLEAR_REQUIRED(FN) ((FN)->partial = 0)
188 void
189 read_scan_file (scan_file)
190 FILE *scan_file;
192 char **rptr;
193 int i;
194 obstack_init(&scan_file_obstack);
196 for (;;)
198 struct partial_proto *partial;
199 struct fn_decl *fn;
200 int ch;
201 char *ptr, *fname, *kind, *rtype, *args, *file_seen, *line_seen;
202 line.ptr = line.base;
203 ch = read_upto (scan_file, &line, '\n');
204 if (ch == EOF)
205 break;
207 fname = line.base;
208 for (ptr = fname; *ptr != ';'; ) ptr++;
209 *ptr = 0;
210 kind = ptr + 1;
211 for (ptr = kind; *ptr != ';'; ) ptr++;
212 *ptr = 0;
214 if (*kind == 'X')
216 switch (special_file_handling)
218 case errno_special:
219 if (strcmp (fname, "errno") == 0) seen_errno++;
220 break;
222 continue;
225 if (*kind == 'M')
227 /* The original include file defines fname as a macro. */
228 fn = lookup_std_proto (fname);
230 /* Since fname is a macro, don't require a prototype for it. */
231 if (fn && REQUIRED (fn))
233 CLEAR_REQUIRED(fn);
234 required_unseen_count--;
237 switch (special_file_handling)
239 case errno_special:
240 if (strcmp (fname, "errno") == 0) seen_errno++;
241 break;
242 case sys_stat_special:
243 if (fname[0] == 'S' && fname[1] == '_')
245 if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
246 else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
247 else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
248 else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
249 else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
250 else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
251 else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
252 else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
253 else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
254 else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
255 else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
256 else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
258 break;
260 continue;
263 rtype = ptr + 1;
264 for (ptr = rtype; *ptr != ';'; ) ptr++;
265 *ptr = 0;
266 args = ptr + 1;
267 for (ptr = args; *ptr != ';'; ) ptr++;
268 *ptr = 0;
269 file_seen = ptr + 1;
270 for (ptr = file_seen; *ptr != ';'; ) ptr++;
271 *ptr = 0;
272 line_seen = ptr + 1;
273 for (ptr = line_seen; *ptr != ';'; ) ptr++;
274 *ptr = 0;
276 if (kind[0] == 'f')
277 missing_extern_C_count++;
279 fn = lookup_std_proto (fname);
281 /* Remove the function from the list of required function. */
282 if (fn && REQUIRED (fn))
284 CLEAR_REQUIRED(fn);
285 required_unseen_count--;
288 /* If we have a full prototype, we're done. */
289 if (args[0] != '\0')
290 continue;
292 if (kind[0] == 'I') /* don't edit inline function */
293 continue;
295 /* If the partial prototype was included from some other file,
296 we don't need to patch it up (in this run). */
297 i = strlen (file_seen);
298 if (i < inc_filename_length
299 || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
300 continue;
302 if (fn == NULL)
303 continue;
304 if (fn->params[0] == '\0' || strcmp(fn->params, "void") == 0)
305 continue;
307 /* We only have a partial function declaration,
308 so remember that we have to add a complete prototype. */
309 partial_count++;
310 partial = (struct partial_proto*)
311 obstack_alloc (&scan_file_obstack, sizeof(struct partial_proto));
312 partial->fname = obstack_alloc (&scan_file_obstack, strlen(fname) + 1);
313 strcpy (partial->fname, fname);
314 partial->rtype = obstack_alloc (&scan_file_obstack, strlen(rtype) + 1);
315 strcpy (partial->rtype, rtype);
316 partial->line_seen = atoi(line_seen);
317 partial->fn = fn;
318 fn->partial = partial;
319 partial->next = partial_proto_list;
320 partial_proto_list = partial;
321 if (verbose)
323 fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
324 inc_filename, fname);
328 if (missing_extern_C_count + required_unseen_count + partial_count
329 + missing_extra_stuff == 0)
331 if (verbose)
332 fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
333 exit (0);
335 if (!verbose)
336 fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
337 else
339 if (required_unseen_count)
340 fprintf (stderr, "%s: %d missing function declarations.\n",
341 inc_filename, required_unseen_count);
342 if (partial_count)
343 fprintf (stderr, "%s: %d non-prototype function declarations.\n",
344 inc_filename, partial_count);
345 if (missing_extern_C_count)
346 fprintf (stderr,
347 "%s: %d declarations not protected by extern \"C\".\n",
348 inc_filename, missing_extern_C_count);
352 write_rbrac ()
354 struct fn_decl *fn;
355 char **rptr;
356 register struct partial_proto *partial;
358 if (required_unseen_count)
359 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
361 /* Now we print out prototypes for those functions that we haven't seen. */
362 for (rptr = required_functions; *rptr; rptr++)
364 fn = lookup_std_proto (*rptr);
365 if (fn == NULL || !REQUIRED (fn))
366 continue;
367 fprintf (outf, "extern %s %s (%s);\n",
368 fn->rtype, fn->fname, fn->params);
370 if (required_unseen_count)
371 fprintf (outf,
372 "#endif /* defined(__STDC__) || defined(__cplusplus) */\n");
374 switch (special_file_handling)
376 case errno_special:
377 if (!seen_errno)
378 fprintf (outf, "extern int errno;\n");
379 break;
380 case sys_stat_special:
381 if (!seen_S_ISBLK && seen_S_IFBLK)
382 fprintf (outf,
383 "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
384 if (!seen_S_ISCHR && seen_S_IFCHR)
385 fprintf (outf,
386 "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
387 if (!seen_S_ISDIR && seen_S_IFDIR)
388 fprintf (outf,
389 "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
390 if (!seen_S_ISFIFO && seen_S_IFIFO)
391 fprintf (outf,
392 "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
393 if (!seen_S_ISLNK && seen_S_IFLNK)
394 fprintf (outf,
395 "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
396 if (!seen_S_ISREG && seen_S_IFREG)
397 fprintf (outf,
398 "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
399 break;
403 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
406 char *
407 strdup (str)
408 char *str;
410 return strcpy((char*)malloc (strlen (str) + 1), str);
413 /* Returns 1 iff the file is properly protected from multiple inclusion:
414 #ifndef PROTECT_NAME
415 #define PROTECT_NAME
416 #endif
421 check_protection (inf, ifndef_line, endif_line)
422 FILE *inf;
423 int *ifndef_line, *endif_line;
425 int c;
426 int if_nesting = 1; /* Level of nesting of #if's */
427 char *protect_name = NULL; /* Identifier following initial #ifndef */
428 int define_seen = 0;
430 /* Skip initial white space (including comments). */
431 for (;; lineno++)
433 c = skip_spaces (inf, ' ');
434 if (c == EOF)
435 return 0;
436 if (c != '\n')
437 break;
439 if (c != '#')
440 return 0;
441 c = scan_ident (inf, &buf, skip_spaces (inf, ' '));
442 if (SSTRING_LENGTH(&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
443 return 0;
445 /* So far so good: We've seen an initial #ifndef. */
446 *ifndef_line = lineno;
447 c = scan_ident (inf, &buf, skip_spaces (inf, c));
448 if (SSTRING_LENGTH(&buf) == 0 || c == EOF)
449 return 0;
450 protect_name = strdup (buf.base);
452 ungetc (c, inf);
453 c = read_upto (inf, &buf, '\n');
454 if (c == EOF)
455 return 0;
456 lineno++;
458 for (;;)
460 c = skip_spaces(inf, ' ');
461 if (c == EOF)
462 return 0;
463 if (c == '\n')
465 lineno++;
466 continue;
468 if (c != '#')
469 goto skip_to_eol;
470 c = scan_ident (inf, &buf, skip_spaces (inf, ' '));
471 if (SSTRING_LENGTH(&buf) == 0)
473 else if (!strcmp (buf.base, "ifndef")
474 || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
476 if_nesting++;
478 else if (!strcmp (buf.base, "endif"))
480 if_nesting--;
481 if (if_nesting == 0)
482 break;
484 else if (!strcmp (buf.base, "else"))
486 if (if_nesting == 1)
487 return 0;
489 else if (!strcmp (buf.base, "define"))
491 if (if_nesting != 1)
492 goto skip_to_eol;
493 c = skip_spaces (inf, c);
494 c = scan_ident (inf, &buf, c);
495 if (buf.base[0] > 0 && strcmp(buf.base, protect_name) == 0)
496 define_seen = 1;
498 skip_to_eol:
499 for (;;)
501 if (c == '\n' || c == EOF)
502 break;
503 c = getc (inf);
505 if (c == EOF)
506 return 0;
507 lineno++;
510 if (!define_seen)
511 return 0;
512 *endif_line = lineno;
513 /* Skip final white space (including comments). */
514 for (;;)
516 c = skip_spaces (inf, ' ');
517 if (c == EOF)
518 break;
519 if (c != '\n')
520 return 0;
523 return 1;
527 main(argc, argv)
528 int argc;
529 char **argv;
531 FILE *inf;
532 int c;
533 int i, done;
534 char *cptr, *cptr0, **pptr;
535 int ifndef_line;
536 int endif_line;;
539 if (argv[0] && argv[0][0])
540 progname = argv[0];
542 if (argc < 4)
544 fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h req_funcs <scan-file-name\n",
545 progname);
546 exit (-1);
549 inc_filename = argv[1];
550 inc_filename_length = strlen (inc_filename);
551 if (strcmp (inc_filename, "sys/stat.h") == 0)
552 special_file_handling = sys_stat_special;
553 else if (strcmp (inc_filename, "errno.h") == 0)
554 special_file_handling = errno_special, missing_extra_stuff++;
556 /* Calculate an upper bound of the number of function names in argv[4] */
557 for (i = 1, cptr = argv[4]; *cptr; cptr++)
558 if (*cptr == ' ') i++;
559 /* Find the list of prototypes required for this include file. */
560 required_functions = (char**)xmalloc((i+1) * sizeof(char*));
561 for (cptr = argv[4], cptr0 = cptr, pptr = required_functions, done = 0;
562 !done; cptr++)
564 done = *cptr == '\0';
565 if (*cptr == ' ' || done)
567 *cptr = '\0';
568 if (cptr > cptr0)
570 struct fn_decl *fn = lookup_std_proto(cptr0);
571 *pptr++ = cptr0;
572 if (fn == NULL)
573 fprintf (stderr, "Internal error: No prototype for %s\n",
574 cptr0);
575 else
576 SET_REQUIRED(fn);
578 cptr0 = cptr + 1;
581 required_unseen_count = pptr - required_functions;
582 *pptr = 0;
584 read_scan_file (stdin);
586 inf = fopen (argv[2], "r");
587 if (inf == NULL)
589 fprintf (stderr, "%s: Cannot open '%s' for reading -",
590 progname, argv[2]);
591 perror (NULL);
592 exit (-1);
595 outf = fopen (argv[3], "w");
596 if (outf == NULL)
598 fprintf (stderr, "%s: Cannot open '%s' for writing -",
599 progname, argv[3]);
600 perror (NULL);
601 exit (-1);
604 if (check_protection (inf, &ifndef_line, &endif_line))
606 #if 0
607 fprintf(stderr, "#ifndef %s on line %d; #endif on line %d\n",
608 protect_name, ifndef_line, endif_line);
609 #endif
610 lbrac_line = ifndef_line+1;
611 rbrac_line = endif_line;
613 else
615 lbrac_line = 1;
616 rbrac_line = -1;
619 fseek(inf, 0, 0);
620 lineno = 1;
622 for (;;)
624 if (lineno == lbrac_line)
625 write_lbrac ();
626 if (lineno == rbrac_line)
627 write_rbrac ();
628 for (;;)
630 struct fn_decl *fn;
631 c = getc (inf);
632 if (c == EOF)
633 break;
634 if (isalpha (c) || c == '_')
636 struct partial_proto *partial;
637 ungetc (c, inf);
638 if (get_token (inf, &buf) != IDENTIFIER_TOKEN)
639 abort ();
640 fputs (buf.base, outf);
641 fn = lookup_std_proto (buf.base);
642 /* We only want to edit the declaration matching the one
643 seen by scan-decls, as there can be multiple
644 declarations, selected by #ifdef __STDC__ or whatever. */
645 if (fn && fn->partial && fn->partial->line_seen == lineno)
647 c = skip_spaces (inf, ' ');
648 if (c == EOF)
649 break;
650 if (c == '(')
652 c = skip_spaces (inf, ' ');
653 if (c == ')')
655 fprintf (outf, " _PARAMS((%s))", fn->params);
657 else
659 putc ('(', outf);
660 ungetc (c, inf);
663 else
664 fprintf (outf, " %c", c);
667 else
668 putc (c, outf);
669 if (c == '\n')
670 break;
672 if (c == EOF)
673 break;
674 lineno++;
676 if (rbrac_line < 0)
677 write_rbrac ();
679 fclose (inf);
680 fclose (outf);
682 return 0;