2001-04-09 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / fixinc / fixfixes.c
blob56b9f5493c7fee91716594d14b45f0c91ef1ba13
2 /*
4 Test to see if a particular fix should be applied to a header file.
6 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
8 = = = = = = = = = = = = = = = = = = = = = = = = =
10 NOTE TO DEVELOPERS
12 The routines you write here must work closely with fixincl.c.
14 Here are the rules:
16 1. Every test procedure name must be suffixed with "_fix".
17 These routines will be referenced from inclhack.def, sans the suffix.
19 2. Use the "FIX_PROC_HEAD()" macro _with_ the "_fix" suffix
20 (I cannot use the ## magic from ANSI C) for defining your entry point.
22 3. Put your test name into the FIXUP_TABLE.
24 4. Do not read anything from stdin. It is closed.
26 5. Write to stderr only in the event of a reportable error
27 In such an event, call "exit (EXIT_FAILURE)".
29 6. You have access to the fixDescList entry for the fix in question.
30 This may be useful, for example, if there are interesting strings
31 or pre-compiled regular expressions stored there.
33 It is also possible to access fix descriptions by using the
34 index of a known fix, "my_fix_name" for example:
36 tFixDesc* p_desc = fixDescList + MY_FIX_NAME_FIXIDX;
37 tTestDesc* p_tlist = p_desc->p_test_desc;
39 regexec (p_tlist->p_test_regex, ...)
41 = = = = = = = = = = = = = = = = = = = = = = = = =
43 This file is part of GNU CC.
45 GNU CC is free software; you can redistribute it and/or modify
46 it under the terms of the GNU General Public License as published by
47 the Free Software Foundation; either version 2, or (at your option)
48 any later version.
50 GNU CC is distributed in the hope that it will be useful,
51 but WITHOUT ANY WARRANTY; without even the implied warranty of
52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 GNU General Public License for more details.
55 You should have received a copy of the GNU General Public License
56 along with GNU CC; see the file COPYING. If not, write to
57 the Free Software Foundation, 59 Temple Place - Suite 330,
58 Boston, MA 02111-1307, USA. */
60 #include "fixlib.h"
61 #define GTYPE_SE_CT 1
63 #ifdef SEPARATE_FIX_PROC
64 #include "fixincl.x"
65 #endif
67 tSCC zNeedsArg[] = "fixincl error: `%s' needs %s argument (c_fix_arg[%d])\n";
69 typedef void t_fix_proc PARAMS ((const char *, const char *, tFixDesc *));
70 typedef struct {
71 const char* fix_name;
72 t_fix_proc* fix_proc;
73 } fix_entry_t;
75 #define FIXUP_TABLE \
76 _FT_( "char_macro_def", char_macro_def_fix ) \
77 _FT_( "char_macro_use", char_macro_use_fix ) \
78 _FT_( "format", format_fix ) \
79 _FT_( "machine_name", machine_name_fix ) \
80 _FT_( "wrap", wrap_fix ) \
81 _FT_( "gnu_type", gnu_type_fix )
84 #define FIX_PROC_HEAD( fix ) \
85 static void fix PARAMS ((const char *, const char *, tFixDesc *)); /* avoid warning */ \
86 static void fix ( filname, text, p_fixd ) \
87 const char* filname; \
88 const char* text; \
89 tFixDesc* p_fixd;
91 #ifdef NEED_PRINT_QUOTE
93 * Skip over a quoted string. Single quote strings may
94 * contain multiple characters if the first character is
95 * a backslash. Especially a backslash followed by octal digits.
96 * We are not doing a correctness syntax check here.
98 static char*
99 print_quote( q, text )
100 char q;
101 char* text;
103 fputc( q, stdout );
105 for (;;)
107 char ch = *(text++);
108 fputc( ch, stdout );
110 switch (ch)
112 case '\\':
113 if (*text == NUL)
114 goto quote_done;
116 fputc( *(text++), stdout );
117 break;
119 case '"':
120 case '\'':
121 if (ch != q)
122 break;
123 /*FALLTHROUGH*/
125 case '\n':
126 case NUL:
127 goto quote_done;
129 } quote_done:;
131 return text;
133 #endif /* NEED_PRINT_QUOTE */
137 * Emit the GNU standard type wrapped up in such a way that
138 * this thing can be encountered countless times during a compile
139 * and not cause even a warning.
141 static const char *emit_gnu_type PARAMS ((const char *, regmatch_t *));
142 static const char*
143 emit_gnu_type ( text, rm )
144 const char* text;
145 regmatch_t* rm;
147 char z_TYPE[ 64 ];
148 char z_type[ 64 ];
150 fwrite (text, rm[0].rm_so, 1, stdout);
153 const char* ps = text + rm[1].rm_so;
154 const char* pe = text + rm[1].rm_eo;
155 char* pd = z_type;
156 char* pD = z_TYPE;
158 while (ps < pe)
159 *(pD++) = TOUPPER( *(pd++) = *(ps++) );
161 *pD = *pd = NUL;
165 * Now print out the reformed typedef,
166 * with a C++ guard for WCHAR
169 tSCC z_fmt[] = "\
170 #if !defined(_GCC_%s_T)%s\n\
171 #define _GCC_%s_T\n\
172 typedef __%s_TYPE__ %s_t;\n\
173 #endif\n";
175 const char* pz_guard = (strcmp (z_type, "wchar") == 0)
176 ? " && ! defined(__cplusplus)" : "";
178 printf (z_fmt, z_TYPE, pz_guard, z_TYPE, z_TYPE, z_type);
181 return text += rm[0].rm_eo;
186 * Copy the `format' string to std out, replacing `%n' expressions
187 * with the matched text from a regular expression evaluation.
188 * Doubled '%' characters will be replaced with a single copy.
189 * '%' characters in other contexts and all other characters are
190 * copied out verbatim.
192 static void format_write PARAMS ((tCC *, tCC *, regmatch_t[]));
193 static void
194 format_write (format, text, av)
195 tCC* format;
196 tCC* text;
197 regmatch_t av[];
199 int c;
201 while ((c = (unsigned)*(format++)) != NUL) {
203 if (c != '%')
205 putchar(c);
206 continue;
209 c = (unsigned)*(format++);
212 * IF the character following a '%' is not a digit,
213 * THEN we will always emit a '%' and we may or may
214 * not emit the following character. We will end on
215 * a NUL and we will emit only one of a pair of '%'.
217 if (! ISDIGIT ( c ))
219 putchar( '%' );
220 switch (c) {
221 case NUL:
222 return;
223 case '%':
224 break;
225 default:
226 putchar(c);
231 * Emit the matched subexpression numbered 'c'.
232 * IF, of course, there was such a match...
234 else {
235 regmatch_t* pRM = av + (c - (unsigned)'0');
236 size_t len;
238 if (pRM->rm_so < 0)
239 continue;
241 len = pRM->rm_eo - pRM->rm_so;
242 if (len > 0)
243 fwrite(text + pRM->rm_so, len, 1, stdout);
250 * Search for multiple copies of a regular expression. Each block
251 * of matched text is replaced with the format string, as described
252 * above in `format_write'.
254 FIX_PROC_HEAD( format_fix )
256 tCC* pz_pat = p_fixd->patch_args[2];
257 tCC* pz_fmt = p_fixd->patch_args[1];
258 regex_t re;
259 regmatch_t rm[10];
260 IGNORE_ARG(filname);
263 * We must have a format
265 if (pz_fmt == (tCC*)NULL)
267 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
268 exit (EXIT_BROKEN);
272 * IF we don't have a search text, then go find the first
273 * regular expression among the tests.
275 if (pz_pat == (tCC*)NULL)
277 tTestDesc* pTD = p_fixd->p_test_desc;
278 int ct = p_fixd->test_ct;
279 for (;;)
281 if (ct-- <= 0)
283 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
284 exit (EXIT_BROKEN);
287 if (pTD->type == TT_EGREP)
289 pz_pat = pTD->pz_test_text;
290 break;
293 pTD++;
298 * Replace every copy of the text we find
300 compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
301 while (regexec (&re, text, 10, rm, 0) == 0)
303 fwrite( text, rm[0].rm_so, 1, stdout );
304 format_write( pz_fmt, text, rm );
305 text += rm[0].rm_eo;
309 * Dump out the rest of the file
311 fputs (text, stdout);
315 /* Scan the input file for all occurrences of text like this:
317 #define TIOCCONS _IO(T, 12)
319 and change them to read like this:
321 #define TIOCCONS _IO('T', 12)
323 which is the required syntax per the C standard. (The definition of
324 _IO also has to be tweaked - see below.) 'IO' is actually whatever you
325 provide as the `c_fix_arg' argument. */
327 FIX_PROC_HEAD( char_macro_use_fix )
329 /* This regexp looks for a traditional-syntax #define (# in column 1)
330 of an object-like macro. */
331 static const char pat[] =
332 "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
333 static regex_t re;
335 const char* str = p_fixd->patch_args[1];
336 regmatch_t rm[1];
337 const char *p, *limit;
338 size_t len;
339 IGNORE_ARG(filname);
341 if (str == NULL)
343 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
344 exit (EXIT_BROKEN);
347 len = strlen (str);
348 compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
350 for (p = text;
351 regexec (&re, p, 1, rm, 0) == 0;
352 p = limit + 1)
354 /* p + rm[0].rm_eo is the first character of the macro replacement.
355 Find the end of the macro replacement, and the STR we were
356 sent to look for within the replacement. */
357 p += rm[0].rm_eo;
358 limit = p - 1;
361 limit = strchr (limit + 1, '\n');
362 if (!limit)
363 goto done;
365 while (limit[-1] == '\\');
369 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
370 goto found;
372 while (++p < limit - len);
373 /* Hit end of line. */
374 continue;
376 found:
377 /* Found STR on this line. If the macro needs fixing,
378 the next few chars will be whitespace or uppercase,
379 then an open paren, then a single letter. */
380 while ((ISSPACE (*p) || ISUPPER (*p)) && p < limit) p++;
381 if (*p++ != '(')
382 continue;
383 if (!ISALPHA (*p))
384 continue;
385 if (ISALNUM (p[1]) || p[1] == '_')
386 continue;
388 /* Splat all preceding text into the output buffer,
389 quote the character at p, then proceed. */
390 fwrite (text, 1, p - text, stdout);
391 putchar ('\'');
392 putchar (*p);
393 putchar ('\'');
394 text = p + 1;
396 done:
397 fputs (text, stdout);
401 /* Scan the input file for all occurrences of text like this:
403 #define xxxIOxx(x, y) (....'x'<<16....)
405 and change them to read like this:
407 #define xxxIOxx(x, y) (....x<<16....)
409 which is the required syntax per the C standard. (The uses of _IO
410 also has to be tweaked - see above.) 'IO' is actually whatever
411 you provide as the `c_fix_arg' argument. */
412 FIX_PROC_HEAD( char_macro_def_fix )
414 /* This regexp looks for any traditional-syntax #define (# in column 1). */
415 static const char pat[] =
416 "^#[ \t]*define[ \t]+";
417 static regex_t re;
419 const char* str = p_fixd->patch_args[1];
420 regmatch_t rm[1];
421 const char *p, *limit;
422 char arg;
423 size_t len;
424 IGNORE_ARG(filname);
426 if (str == NULL)
428 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
429 exit (EXIT_BROKEN);
432 len = strlen (str);
433 compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
435 for (p = text;
436 regexec (&re, p, 1, rm, 0) == 0;
437 p = limit + 1)
439 /* p + rm[0].rm_eo is the first character of the macro name.
440 Find the end of the macro replacement, and the STR we were
441 sent to look for within the name. */
442 p += rm[0].rm_eo;
443 limit = p - 1;
446 limit = strchr (limit + 1, '\n');
447 if (!limit)
448 goto done;
450 while (limit[-1] == '\\');
454 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
455 goto found;
456 p++;
458 while (ISALPHA (*p) || ISALNUM (*p) || *p == '_');
459 /* Hit end of macro name without finding the string. */
460 continue;
462 found:
463 /* Found STR in this macro name. If the macro needs fixing,
464 there may be a few uppercase letters, then there will be an
465 open paren with _no_ intervening whitespace, and then a
466 single letter. */
467 while (ISUPPER (*p) && p < limit) p++;
468 if (*p++ != '(')
469 continue;
470 if (!ISALPHA (*p))
471 continue;
472 if (ISALNUM (p[1]) || p[1] == '_')
473 continue;
475 /* The character at P is the one to look for in the following
476 text. */
477 arg = *p;
478 p += 2;
480 while (p < limit)
482 if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
484 /* Remove the quotes from this use of ARG. */
485 p--;
486 fwrite (text, 1, p - text, stdout);
487 putchar (arg);
488 p += 3;
489 text = p;
491 else
492 p++;
495 done:
496 fputs (text, stdout);
499 /* Fix for machine name #ifdefs that are not in the namespace reserved
500 by the C standard. They won't be defined if compiling with -ansi,
501 and the headers will break. We go to some trouble to only change
502 #ifdefs where the macro is defined by GCC in non-ansi mode; this
503 minimizes the number of headers touched. */
505 #define SCRATCHSZ 64 /* hopefully long enough */
507 FIX_PROC_HEAD( machine_name_fix )
509 #ifndef MN_NAME_PAT
510 fputs( "The target machine has no needed machine name fixes\n", stderr );
511 #else
512 regmatch_t match[2];
513 const char *line, *base, *limit, *p, *q;
514 regex_t *label_re, *name_re;
515 char scratch[SCRATCHSZ];
516 size_t len;
517 IGNORE_ARG(filname);
518 IGNORE_ARG(p_fixd);
520 mn_get_regexps (&label_re, &name_re, "machine_name_fix");
522 scratch[0] = '_';
523 scratch[1] = '_';
525 for (base = text;
526 regexec (label_re, base, 2, match, 0) == 0;
527 base = limit)
529 base += match[0].rm_eo;
530 /* We're looking at an #if or #ifdef. Scan forward for the
531 next non-escaped newline. */
532 line = limit = base;
535 limit++;
536 limit = strchr (limit, '\n');
537 if (!limit)
538 goto done;
540 while (limit[-1] == '\\');
542 /* If the 'name_pat' matches in between base and limit, we have
543 a bogon. It is not worth the hassle of excluding comments
544 because comments on #if/#ifdef lines are rare, and strings on
545 such lines are illegal.
547 REG_NOTBOL means 'base' is not at the beginning of a line, which
548 shouldn't matter since the name_re has no ^ anchor, but let's
549 be accurate anyway. */
551 for (;;)
553 again:
554 if (base == limit)
555 break;
557 if (regexec (name_re, base, 1, match, REG_NOTBOL))
558 goto done; /* No remaining match in this file */
560 /* Match; is it on the line? */
561 if (match[0].rm_eo > limit - base)
562 break;
564 p = base + match[0].rm_so;
565 base += match[0].rm_eo;
567 /* One more test: if on the same line we have the same string
568 with the appropriate underscores, then leave it alone.
569 We want exactly two leading and trailing underscores. */
570 if (*p == '_')
572 len = base - p - ((*base == '_') ? 2 : 1);
573 q = p + 1;
575 else
577 len = base - p - ((*base == '_') ? 1 : 0);
578 q = p;
580 if (len + 4 > SCRATCHSZ)
581 abort ();
582 memcpy (&scratch[2], q, len);
583 len += 2;
584 scratch[len++] = '_';
585 scratch[len++] = '_';
587 for (q = line; q <= limit - len; q++)
588 if (*q == '_' && !strncmp (q, scratch, len))
589 goto again;
591 fwrite (text, 1, p - text, stdout);
592 fwrite (scratch, 1, len, stdout);
594 text = base;
597 done:
598 #endif
599 fputs (text, stdout);
603 FIX_PROC_HEAD( wrap_fix )
605 tSCC z_no_wrap_pat[] = "^#if.*__need_";
606 static regex_t no_wrapping_re; /* assume zeroed data */
608 char z_fixname[ 64 ];
609 tCC* pz_src = p_fixd->fix_name;
610 tCC* pz_name = z_fixname;
611 char* pz_dst = z_fixname;
612 int do_end = 0;
613 size_t len = 0;
614 IGNORE_ARG(filname);
616 if (no_wrapping_re.allocated == 0)
617 compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
618 "wrap-fix" );
620 for (;;) {
621 char ch = *pz_src++;
623 if (ch == NUL) {
624 *pz_dst++ = ch;
625 break;
626 } else if (! ISALNUM (ch)) {
627 *pz_dst++ = '_';
628 } else {
629 *pz_dst++ = TOUPPER (ch);
632 if (++len >= sizeof( z_fixname )) {
633 void* p = xmalloc( len + strlen( pz_src ) + 1 );
634 memcpy( p, (void*)z_fixname, len );
635 pz_name = (tCC*)p;
636 pz_dst = (char*)pz_name + len;
641 * IF we do *not* match the no-wrap re, then we have a double negative.
642 * A double negative means YES.
644 if (regexec (&no_wrapping_re, text, 0, NULL, 0) != 0)
646 printf( "#ifndef FIXINC_%s_CHECK\n", pz_name );
647 printf( "#define FIXINC_%s_CHECK 1\n\n", pz_name );
648 do_end = 1;
651 if (p_fixd->patch_args[1] == (tCC*)NULL)
652 fputs( text, stdout );
654 else {
655 fputs( p_fixd->patch_args[1], stdout );
656 fputs( text, stdout );
657 if (p_fixd->patch_args[2] != (tCC*)NULL)
658 fputs( p_fixd->patch_args[2], stdout );
661 if (do_end != 0)
662 printf( "\n#endif /* FIXINC_%s_CHECK */\n", pz_name );
664 if (pz_name != z_fixname)
665 free( (void*)pz_name );
670 * Search for multiple copies of a regular expression. Each block
671 * of matched text is replaced with the format string, as described
672 * above in `format_write'.
674 FIX_PROC_HEAD( gnu_type_fix )
676 const char* pz_pat;
677 regex_t re;
678 regmatch_t rm[GTYPE_SE_CT+1];
679 IGNORE_ARG(filname);
682 tTestDesc* pTD = p_fixd->p_test_desc;
683 int ct = p_fixd->test_ct;
684 for (;;)
686 if (ct-- <= 0)
688 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
689 exit (EXIT_BROKEN);
692 if (pTD->type == TT_EGREP)
694 pz_pat = pTD->pz_test_text;
695 break;
698 pTD++;
702 compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
704 while (regexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
706 text = emit_gnu_type (text, rm);
710 * Dump out the rest of the file
712 fputs (text, stdout);
716 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
718 test for fix selector
720 THIS IS THE ONLY EXPORTED ROUTINE
723 void
724 apply_fix( p_fixd, filname )
725 tFixDesc* p_fixd;
726 tCC* filname;
728 #define _FT_(n,p) { n, p },
729 static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
730 #undef _FT_
731 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
733 tCC* fixname = p_fixd->patch_args[0];
734 char* buf;
735 int ct = FIX_TABLE_CT;
736 fix_entry_t* pfe = fix_table;
738 for (;;)
740 if (strcmp (pfe->fix_name, fixname) == 0)
741 break;
742 if (--ct <= 0)
744 fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
745 fixname );
746 exit (EXIT_BROKEN);
748 pfe++;
751 buf = load_file_data (stdin);
752 (*pfe->fix_proc)( filname, buf, p_fixd );
755 #ifdef SEPARATE_FIX_PROC
756 tSCC z_usage[] =
757 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
758 tSCC z_reopen[] =
759 "FS error %d (%s) reopening %s as std%s\n";
762 main( argc, argv )
763 int argc;
764 char** argv;
766 tFixDesc* pFix;
767 char* pz_tmptmp;
768 char* pz_tmp_base;
769 char* pz_tmp_dot;
771 if (argc != 5)
773 usage_failure:
774 fputs (z_usage, stderr);
775 return EXIT_FAILURE;
779 char* pz = argv[1];
780 long idx;
782 if (! ISDIGIT ( *pz ))
783 goto usage_failure;
785 idx = strtol (pz, &pz, 10);
786 if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
787 goto usage_failure;
788 pFix = fixDescList + idx;
791 if (freopen (argv[3], "r", stdin) != stdin)
793 fprintf (stderr, z_reopen, errno, strerror( errno ), argv[3], "in");
794 return EXIT_FAILURE;
797 pz_tmptmp = (char*)xmalloc( strlen( argv[4] ) + 5 );
798 strcpy( pz_tmptmp, argv[4] );
800 /* Don't lose because "12345678" and "12345678X" map to the same
801 file under DOS restricted 8+3 file namespace. Note that DOS
802 doesn't allow more than one dot in the trunk of a file name. */
803 pz_tmp_base = basename( pz_tmptmp );
804 pz_tmp_dot = strchr( pz_tmp_base, '.' );
805 if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
806 && pz_tmp_dot != (char*)NULL)
807 strcpy (pz_tmp_dot+1, "X"); /* nuke the original extension */
808 else
809 strcat (pz_tmptmp, ".X");
810 if (freopen (pz_tmptmp, "w", stdout) != stdout)
812 fprintf (stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out");
813 return EXIT_FAILURE;
816 apply_fix (pFix, argv[1]);
817 fclose (stdout);
818 fclose (stdin);
819 unlink (argv[4]);
820 if (rename (pz_tmptmp, argv[4]) != 0)
822 fprintf (stderr, "error %d (%s) renaming %s to %s\n", errno,
823 strerror( errno ), pz_tmptmp, argv[4]);
824 return EXIT_FAILURE;
827 return EXIT_SUCCESS;
829 #endif