Missed one in last change.
[official-gcc.git] / gcc / fixinc / fixfixes.c
blob4c558c550ff1de39b172462c8dcf442cb6cba807
2 /*
4 Test to see if a particular fix should be applied to a header file.
6 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003
7 Free Software Foundation, Inc.
9 = = = = = = = = = = = = = = = = = = = = = = = = =
11 NOTE TO DEVELOPERS
13 The routines you write here must work closely with fixincl.c.
15 Here are the rules:
17 1. Every test procedure name must be suffixed with "_fix".
18 These routines will be referenced from inclhack.def, sans the suffix.
20 2. Use the "FIX_PROC_HEAD()" macro _with_ the "_fix" suffix
21 (I cannot use the ## magic from ANSI C) for defining your entry point.
23 3. Put your test name into the FIXUP_TABLE.
25 4. Do not read anything from stdin. It is closed.
27 5. Write to stderr only in the event of a reportable error
28 In such an event, call "exit (EXIT_FAILURE)".
30 6. You have access to the fixDescList entry for the fix in question.
31 This may be useful, for example, if there are interesting strings
32 or pre-compiled regular expressions stored there.
34 = = = = = = = = = = = = = = = = = = = = = = = = =
36 This file is part of GCC.
38 GCC is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2, or (at your option)
41 any later version.
43 GCC is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
48 You should have received a copy of the GNU General Public License
49 along with GCC; see the file COPYING. If not, write to
50 the Free Software Foundation, 59 Temple Place - Suite 330,
51 Boston, MA 02111-1307, USA. */
53 #include "fixlib.h"
54 #define GTYPE_SE_CT 1
56 #ifdef SEPARATE_FIX_PROC
57 #include "fixincl.x"
58 #endif
60 tSCC zNeedsArg[] = "fixincl error: `%s' needs %s argument (c_fix_arg[%d])\n";
62 typedef void t_fix_proc PARAMS ((const char *, const char *, tFixDesc *));
63 typedef struct {
64 const char* fix_name;
65 t_fix_proc* fix_proc;
66 } fix_entry_t;
68 #define FIXUP_TABLE \
69 _FT_( "char_macro_def", char_macro_def_fix ) \
70 _FT_( "char_macro_use", char_macro_use_fix ) \
71 _FT_( "format", format_fix ) \
72 _FT_( "machine_name", machine_name_fix ) \
73 _FT_( "wrap", wrap_fix ) \
74 _FT_( "gnu_type", gnu_type_fix )
77 #define FIX_PROC_HEAD( fix ) \
78 static void fix PARAMS ((const char *, const char *, tFixDesc *)); \
79 static void fix ( filname, text, p_fixd ) \
80 const char* filname ATTRIBUTE_UNUSED; \
81 const char* text ATTRIBUTE_UNUSED; \
82 tFixDesc* p_fixd ATTRIBUTE_UNUSED;
84 #ifdef NEED_PRINT_QUOTE
86 * Skip over a quoted string. Single quote strings may
87 * contain multiple characters if the first character is
88 * a backslash. Especially a backslash followed by octal digits.
89 * We are not doing a correctness syntax check here.
91 static char*
92 print_quote( q, text )
93 char q;
94 char* text;
96 fputc( q, stdout );
98 for (;;)
100 char ch = *(text++);
101 fputc( ch, stdout );
103 switch (ch)
105 case '\\':
106 if (*text == NUL)
107 goto quote_done;
109 fputc( *(text++), stdout );
110 break;
112 case '"':
113 case '\'':
114 if (ch != q)
115 break;
116 /*FALLTHROUGH*/
118 case '\n':
119 case NUL:
120 goto quote_done;
122 } quote_done:;
124 return text;
126 #endif /* NEED_PRINT_QUOTE */
130 * Emit the GNU standard type wrapped up in such a way that
131 * this thing can be encountered countless times during a compile
132 * and not cause even a warning.
134 static const char *emit_gnu_type PARAMS ((const char *, regmatch_t *));
135 static const char*
136 emit_gnu_type ( text, rm )
137 const char* text;
138 regmatch_t* rm;
140 char z_TYPE[ 64 ];
141 char z_type[ 64 ];
143 fwrite (text, rm[0].rm_so, 1, stdout);
146 const char* ps = text + rm[1].rm_so;
147 const char* pe = text + rm[1].rm_eo;
148 char* pd = z_type;
149 char* pD = z_TYPE;
151 while (ps < pe)
152 *(pD++) = TOUPPER( *(pd++) = *(ps++) );
154 *pD = *pd = NUL;
158 * Now print out the reformed typedef,
159 * with a C++ guard for WCHAR
162 tSCC z_fmt[] = "\
163 #if !defined(_GCC_%s_T)%s\n\
164 #define _GCC_%s_T\n\
165 typedef __%s_TYPE__ %s_t;\n\
166 #endif\n";
168 const char *const pz_guard = (strcmp (z_type, "wchar") == 0)
169 ? " && ! defined(__cplusplus)" : "";
171 printf (z_fmt, z_TYPE, pz_guard, z_TYPE, z_TYPE, z_type);
174 return text += rm[0].rm_eo;
179 * Copy the `format' string to std out, replacing `%n' expressions
180 * with the matched text from a regular expression evaluation.
181 * Doubled '%' characters will be replaced with a single copy.
182 * '%' characters in other contexts and all other characters are
183 * copied out verbatim.
185 static void format_write PARAMS ((tCC *, tCC *, regmatch_t[]));
186 static void
187 format_write (format, text, av)
188 tCC* format;
189 tCC* text;
190 regmatch_t av[];
192 int c;
194 while ((c = (unsigned)*(format++)) != NUL) {
196 if (c != '%')
198 putchar(c);
199 continue;
202 c = (unsigned)*(format++);
205 * IF the character following a '%' is not a digit,
206 * THEN we will always emit a '%' and we may or may
207 * not emit the following character. We will end on
208 * a NUL and we will emit only one of a pair of '%'.
210 if (! ISDIGIT ( c ))
212 putchar( '%' );
213 switch (c) {
214 case NUL:
215 return;
216 case '%':
217 break;
218 default:
219 putchar(c);
224 * Emit the matched subexpression numbered 'c'.
225 * IF, of course, there was such a match...
227 else {
228 regmatch_t* pRM = av + (c - (unsigned)'0');
229 size_t len;
231 if (pRM->rm_so < 0)
232 continue;
234 len = pRM->rm_eo - pRM->rm_so;
235 if (len > 0)
236 fwrite(text + pRM->rm_so, len, 1, stdout);
243 * Search for multiple copies of a regular expression. Each block
244 * of matched text is replaced with the format string, as described
245 * above in `format_write'.
247 FIX_PROC_HEAD( format_fix )
249 tCC* pz_pat = p_fixd->patch_args[2];
250 tCC* pz_fmt = p_fixd->patch_args[1];
251 regex_t re;
252 regmatch_t rm[10];
253 IGNORE_ARG(filname);
256 * We must have a format
258 if (pz_fmt == (tCC*)NULL)
260 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
261 exit (EXIT_BROKEN);
265 * IF we don't have a search text, then go find the first
266 * regular expression among the tests.
268 if (pz_pat == (tCC*)NULL)
270 tTestDesc* pTD = p_fixd->p_test_desc;
271 int ct = p_fixd->test_ct;
272 for (;;)
274 if (ct-- <= 0)
276 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
277 exit (EXIT_BROKEN);
280 if (pTD->type == TT_EGREP)
282 pz_pat = pTD->pz_test_text;
283 break;
286 pTD++;
291 * Replace every copy of the text we find
293 compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
294 while (regexec (&re, text, 10, rm, 0) == 0)
296 fwrite( text, rm[0].rm_so, 1, stdout );
297 format_write( pz_fmt, text, rm );
298 text += rm[0].rm_eo;
302 * Dump out the rest of the file
304 fputs (text, stdout);
308 /* Scan the input file for all occurrences of text like this:
310 #define TIOCCONS _IO(T, 12)
312 and change them to read like this:
314 #define TIOCCONS _IO('T', 12)
316 which is the required syntax per the C standard. (The definition of
317 _IO also has to be tweaked - see below.) 'IO' is actually whatever you
318 provide as the `c_fix_arg' argument. */
320 FIX_PROC_HEAD( char_macro_use_fix )
322 /* This regexp looks for a traditional-syntax #define (# in column 1)
323 of an object-like macro. */
324 static const char pat[] =
325 "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
326 static regex_t re;
328 const char* str = p_fixd->patch_args[1];
329 regmatch_t rm[1];
330 const char *p, *limit;
331 size_t len;
332 IGNORE_ARG(filname);
334 if (str == NULL)
336 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
337 exit (EXIT_BROKEN);
340 len = strlen (str);
341 compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
343 for (p = text;
344 regexec (&re, p, 1, rm, 0) == 0;
345 p = limit + 1)
347 /* p + rm[0].rm_eo is the first character of the macro replacement.
348 Find the end of the macro replacement, and the STR we were
349 sent to look for within the replacement. */
350 p += rm[0].rm_eo;
351 limit = p - 1;
354 limit = strchr (limit + 1, '\n');
355 if (!limit)
356 goto done;
358 while (limit[-1] == '\\');
362 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
363 goto found;
365 while (++p < limit - len);
366 /* Hit end of line. */
367 continue;
369 found:
370 /* Found STR on this line. If the macro needs fixing,
371 the next few chars will be whitespace or uppercase,
372 then an open paren, then a single letter. */
373 while ((ISSPACE (*p) || ISUPPER (*p)) && p < limit) p++;
374 if (*p++ != '(')
375 continue;
376 if (!ISALPHA (*p))
377 continue;
378 if (ISIDNUM (p[1]))
379 continue;
381 /* Splat all preceding text into the output buffer,
382 quote the character at p, then proceed. */
383 fwrite (text, 1, p - text, stdout);
384 putchar ('\'');
385 putchar (*p);
386 putchar ('\'');
387 text = p + 1;
389 done:
390 fputs (text, stdout);
394 /* Scan the input file for all occurrences of text like this:
396 #define xxxIOxx(x, y) (....'x'<<16....)
398 and change them to read like this:
400 #define xxxIOxx(x, y) (....x<<16....)
402 which is the required syntax per the C standard. (The uses of _IO
403 also has to be tweaked - see above.) 'IO' is actually whatever
404 you provide as the `c_fix_arg' argument. */
405 FIX_PROC_HEAD( char_macro_def_fix )
407 /* This regexp looks for any traditional-syntax #define (# in column 1). */
408 static const char pat[] =
409 "^#[ \t]*define[ \t]+";
410 static regex_t re;
412 const char* str = p_fixd->patch_args[1];
413 regmatch_t rm[1];
414 const char *p, *limit;
415 char arg;
416 size_t len;
417 IGNORE_ARG(filname);
419 if (str == NULL)
421 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
422 exit (EXIT_BROKEN);
425 len = strlen (str);
426 compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
428 for (p = text;
429 regexec (&re, p, 1, rm, 0) == 0;
430 p = limit + 1)
432 /* p + rm[0].rm_eo is the first character of the macro name.
433 Find the end of the macro replacement, and the STR we were
434 sent to look for within the name. */
435 p += rm[0].rm_eo;
436 limit = p - 1;
439 limit = strchr (limit + 1, '\n');
440 if (!limit)
441 goto done;
443 while (limit[-1] == '\\');
447 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
448 goto found;
449 p++;
451 while (ISIDNUM (*p));
452 /* Hit end of macro name without finding the string. */
453 continue;
455 found:
456 /* Found STR in this macro name. If the macro needs fixing,
457 there may be a few uppercase letters, then there will be an
458 open paren with _no_ intervening whitespace, and then a
459 single letter. */
460 while (ISUPPER (*p) && p < limit) p++;
461 if (*p++ != '(')
462 continue;
463 if (!ISALPHA (*p))
464 continue;
465 if (ISIDNUM (p[1]))
466 continue;
468 /* The character at P is the one to look for in the following
469 text. */
470 arg = *p;
471 p += 2;
473 while (p < limit)
475 if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
477 /* Remove the quotes from this use of ARG. */
478 p--;
479 fwrite (text, 1, p - text, stdout);
480 putchar (arg);
481 p += 3;
482 text = p;
484 else
485 p++;
488 done:
489 fputs (text, stdout);
492 /* Fix for machine name #ifdefs that are not in the namespace reserved
493 by the C standard. They won't be defined if compiling with -ansi,
494 and the headers will break. We go to some trouble to only change
495 #ifdefs where the macro is defined by GCC in non-ansi mode; this
496 minimizes the number of headers touched. */
498 #define SCRATCHSZ 64 /* hopefully long enough */
500 FIX_PROC_HEAD( machine_name_fix )
502 #ifndef MN_NAME_PAT
503 fputs( "The target machine has no needed machine name fixes\n", stderr );
504 #else
505 regmatch_t match[2];
506 const char *line, *base, *limit, *p, *q;
507 regex_t *label_re, *name_re;
508 char scratch[SCRATCHSZ];
509 size_t len;
510 IGNORE_ARG(filname);
511 IGNORE_ARG(p_fixd);
513 mn_get_regexps (&label_re, &name_re, "machine_name_fix");
515 scratch[0] = '_';
516 scratch[1] = '_';
518 for (base = text;
519 regexec (label_re, base, 2, match, 0) == 0;
520 base = limit)
522 base += match[0].rm_eo;
523 /* We're looking at an #if or #ifdef. Scan forward for the
524 next non-escaped newline. */
525 line = limit = base;
528 limit++;
529 limit = strchr (limit, '\n');
530 if (!limit)
531 goto done;
533 while (limit[-1] == '\\');
535 /* If the 'name_pat' matches in between base and limit, we have
536 a bogon. It is not worth the hassle of excluding comments
537 because comments on #if/#ifdef lines are rare, and strings on
538 such lines are illegal.
540 REG_NOTBOL means 'base' is not at the beginning of a line, which
541 shouldn't matter since the name_re has no ^ anchor, but let's
542 be accurate anyway. */
544 for (;;)
546 again:
547 if (base == limit)
548 break;
550 if (regexec (name_re, base, 1, match, REG_NOTBOL))
551 goto done; /* No remaining match in this file */
553 /* Match; is it on the line? */
554 if (match[0].rm_eo > limit - base)
555 break;
557 p = base + match[0].rm_so;
558 base += match[0].rm_eo;
560 /* One more test: if on the same line we have the same string
561 with the appropriate underscores, then leave it alone.
562 We want exactly two leading and trailing underscores. */
563 if (*p == '_')
565 len = base - p - ((*base == '_') ? 2 : 1);
566 q = p + 1;
568 else
570 len = base - p - ((*base == '_') ? 1 : 0);
571 q = p;
573 if (len + 4 > SCRATCHSZ)
574 abort ();
575 memcpy (&scratch[2], q, len);
576 len += 2;
577 scratch[len++] = '_';
578 scratch[len++] = '_';
580 for (q = line; q <= limit - len; q++)
581 if (*q == '_' && !strncmp (q, scratch, len))
582 goto again;
584 fwrite (text, 1, p - text, stdout);
585 fwrite (scratch, 1, len, stdout);
587 text = base;
590 done:
591 #endif
592 fputs (text, stdout);
596 FIX_PROC_HEAD( wrap_fix )
598 tSCC z_no_wrap_pat[] = "^#if.*__need_";
599 static regex_t no_wrapping_re; /* assume zeroed data */
601 tCC* pz_name = NULL;
603 if (no_wrapping_re.allocated == 0)
604 compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
605 "wrap-fix" );
608 * IF we do *not* match the no-wrap re, then we have a double negative.
609 * A double negative means YES.
611 if (regexec( &no_wrapping_re, text, 0, NULL, 0 ) != 0)
614 * A single file can get wrapped more than once by different fixes.
615 * A single fix can wrap multiple files. Therefore, guard with
616 * *both* the fix name and the file name.
618 size_t ln = strlen( filname ) + strlen( p_fixd->fix_name ) + 14;
619 char* pz = xmalloc( ln );
620 pz_name = pz;
621 sprintf( pz, "FIXINC_WRAP_%s-%s", filname, p_fixd->fix_name );
623 for (pz += 12; 1; pz++) {
624 char ch = *pz;
626 if (ch == NUL)
627 break;
629 if (! ISALNUM( ch )) {
630 *pz = '_';
632 else {
633 *pz = TOUPPER( ch );
637 printf( "#ifndef %s\n", pz_name );
638 printf( "#define %s 1\n\n", pz_name );
641 if (p_fixd->patch_args[1] == (tCC*)NULL)
642 fputs( text, stdout );
644 else {
645 fputs( p_fixd->patch_args[1], stdout );
646 fputs( text, stdout );
647 if (p_fixd->patch_args[2] != (tCC*)NULL)
648 fputs( p_fixd->patch_args[2], stdout );
651 if (pz_name != NULL) {
652 printf( "\n#endif /* %s */\n", pz_name );
653 free( (void*)pz_name );
659 * Search for multiple copies of a regular expression. Each block
660 * of matched text is replaced with the format string, as described
661 * above in `format_write'.
663 FIX_PROC_HEAD( gnu_type_fix )
665 const char* pz_pat;
666 regex_t re;
667 regmatch_t rm[GTYPE_SE_CT+1];
668 IGNORE_ARG(filname);
671 tTestDesc* pTD = p_fixd->p_test_desc;
672 int ct = p_fixd->test_ct;
673 for (;;)
675 if (ct-- <= 0)
677 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
678 exit (EXIT_BROKEN);
681 if (pTD->type == TT_EGREP)
683 pz_pat = pTD->pz_test_text;
684 break;
687 pTD++;
691 compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
693 while (regexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
695 text = emit_gnu_type (text, rm);
699 * Dump out the rest of the file
701 fputs (text, stdout);
705 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
707 test for fix selector
709 THIS IS THE ONLY EXPORTED ROUTINE
712 void
713 apply_fix( p_fixd, filname )
714 tFixDesc* p_fixd;
715 tCC* filname;
717 #define _FT_(n,p) { n, p },
718 static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
719 #undef _FT_
720 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
722 tCC* fixname = p_fixd->patch_args[0];
723 char* buf;
724 int ct = FIX_TABLE_CT;
725 fix_entry_t* pfe = fix_table;
727 for (;;)
729 if (strcmp (pfe->fix_name, fixname) == 0)
730 break;
731 if (--ct <= 0)
733 fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
734 fixname );
735 exit (EXIT_BROKEN);
737 pfe++;
740 buf = load_file_data (stdin);
741 (*pfe->fix_proc)( filname, buf, p_fixd );
744 #ifdef SEPARATE_FIX_PROC
745 tSCC z_usage[] =
746 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
747 tSCC z_reopen[] =
748 "FS error %d (%s) reopening %s as std%s\n";
751 main( argc, argv )
752 int argc;
753 char** argv;
755 tFixDesc* pFix;
756 char* pz_tmptmp;
757 char* pz_tmp_base;
758 char* pz_tmp_dot;
760 if (argc != 5)
762 usage_failure:
763 fputs (z_usage, stderr);
764 return EXIT_FAILURE;
768 char* pz = argv[1];
769 long idx;
771 if (! ISDIGIT ( *pz ))
772 goto usage_failure;
774 idx = strtol (pz, &pz, 10);
775 if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
776 goto usage_failure;
777 pFix = fixDescList + idx;
780 if (freopen (argv[3], "r", stdin) != stdin)
782 fprintf (stderr, z_reopen, errno, strerror( errno ), argv[3], "in");
783 return EXIT_FAILURE;
786 pz_tmptmp = (char*)xmalloc( strlen( argv[4] ) + 5 );
787 strcpy( pz_tmptmp, argv[4] );
789 /* Don't lose because "12345678" and "12345678X" map to the same
790 file under DOS restricted 8+3 file namespace. Note that DOS
791 doesn't allow more than one dot in the trunk of a file name. */
792 pz_tmp_base = basename( pz_tmptmp );
793 pz_tmp_dot = strchr( pz_tmp_base, '.' );
794 if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
795 && pz_tmp_dot != (char*)NULL)
796 strcpy (pz_tmp_dot+1, "X"); /* nuke the original extension */
797 else
798 strcat (pz_tmptmp, ".X");
799 if (freopen (pz_tmptmp, "w", stdout) != stdout)
801 fprintf (stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out");
802 return EXIT_FAILURE;
805 apply_fix (pFix, argv[1]);
806 fclose (stdout);
807 fclose (stdin);
808 unlink (argv[4]);
809 if (rename (pz_tmptmp, argv[4]) != 0)
811 fprintf (stderr, "error %d (%s) renaming %s to %s\n", errno,
812 strerror( errno ), pz_tmptmp, argv[4]);
813 return EXIT_FAILURE;
816 return EXIT_SUCCESS;
818 #endif