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 = = = = = = = = = = = = = = = = = = = = = = = = =
12 The routines you write here must work closely with fixincl.c.
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 = = = = = = = = = = = = = = = = = = = = = = = = =
35 This file is part of GNU CC.
37 GNU CC is free software; you can redistribute it and/or modify
38 it under the terms of the GNU General Public License as published by
39 the Free Software Foundation; either version 2, or (at your option)
42 GNU CC is distributed in the hope that it will be useful,
43 but WITHOUT ANY WARRANTY; without even the implied warranty of
44 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 GNU General Public License for more details.
47 You should have received a copy of the GNU General Public License
48 along with GNU CC; see the file COPYING. If not, write to
49 the Free Software Foundation, 59 Temple Place - Suite 330,
50 Boston, MA 02111-1307, USA. */
55 #ifdef SEPARATE_FIX_PROC
59 tSCC zNeedsArg[] = "fixincl error: `%s' needs %s argument (c_fix_arg[%d])\n";
61 typedef void t_fix_proc PARAMS ((const char *, const char *, tFixDesc *));
68 _FT_( "char_macro_def", char_macro_def_fix ) \
69 _FT_( "char_macro_use", char_macro_use_fix ) \
70 _FT_( "format", format_fix ) \
71 _FT_( "machine_name", machine_name_fix ) \
72 _FT_( "wrap", wrap_fix ) \
73 _FT_( "gnu_type", gnu_type_fix )
76 #define FIX_PROC_HEAD( fix ) \
77 static void fix PARAMS ((const char *, const char *, tFixDesc *)); /* avoid warning */ \
78 static void fix ( filname, text, p_fixd ) \
79 const char* filname; \
83 #ifdef NEED_PRINT_QUOTE
85 * Skip over a quoted string. Single quote strings may
86 * contain multiple characters if the first character is
87 * a backslash. Especially a backslash followed by octal digits.
88 * We are not doing a correctness syntax check here.
91 print_quote( q, text )
108 fputc( *(text++), stdout );
125 #endif /* NEED_PRINT_QUOTE */
129 * Emit the GNU standard type wrapped up in such a way that
130 * this thing can be encountered countless times during a compile
131 * and not cause even a warning.
133 static const char *emit_gnu_type PARAMS ((const char *, regmatch_t *));
135 emit_gnu_type ( text, rm )
142 fwrite (text, rm[0].rm_so, 1, stdout);
145 const char* ps = text + rm[1].rm_so;
146 const char* pe = text + rm[1].rm_eo;
151 *(pD++) = TOUPPER( *(pd++) = *(ps++) );
157 * Now print out the reformed typedef,
158 * with a C++ guard for WCHAR
162 #if !defined(_GCC_%s_T)%s\n\
164 typedef __%s_TYPE__ %s_t;\n\
167 const char *const pz_guard = (strcmp (z_type, "wchar") == 0)
168 ? " && ! defined(__cplusplus)" : "";
170 printf (z_fmt, z_TYPE, pz_guard, z_TYPE, z_TYPE, z_type);
173 return text += rm[0].rm_eo;
178 * Copy the `format' string to std out, replacing `%n' expressions
179 * with the matched text from a regular expression evaluation.
180 * Doubled '%' characters will be replaced with a single copy.
181 * '%' characters in other contexts and all other characters are
182 * copied out verbatim.
184 static void format_write PARAMS ((tCC *, tCC *, regmatch_t[]));
186 format_write (format, text, av)
193 while ((c = (unsigned)*(format++)) != NUL) {
201 c = (unsigned)*(format++);
204 * IF the character following a '%' is not a digit,
205 * THEN we will always emit a '%' and we may or may
206 * not emit the following character. We will end on
207 * a NUL and we will emit only one of a pair of '%'.
223 * Emit the matched subexpression numbered 'c'.
224 * IF, of course, there was such a match...
227 regmatch_t* pRM = av + (c - (unsigned)'0');
233 len = pRM->rm_eo - pRM->rm_so;
235 fwrite(text + pRM->rm_so, len, 1, stdout);
242 * Search for multiple copies of a regular expression. Each block
243 * of matched text is replaced with the format string, as described
244 * above in `format_write'.
246 FIX_PROC_HEAD( format_fix )
248 tCC* pz_pat = p_fixd->patch_args[2];
249 tCC* pz_fmt = p_fixd->patch_args[1];
255 * We must have a format
257 if (pz_fmt == (tCC*)NULL)
259 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
264 * IF we don't have a search text, then go find the first
265 * regular expression among the tests.
267 if (pz_pat == (tCC*)NULL)
269 tTestDesc* pTD = p_fixd->p_test_desc;
270 int ct = p_fixd->test_ct;
275 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
279 if (pTD->type == TT_EGREP)
281 pz_pat = pTD->pz_test_text;
290 * Replace every copy of the text we find
292 compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
293 while (regexec (&re, text, 10, rm, 0) == 0)
295 fwrite( text, rm[0].rm_so, 1, stdout );
296 format_write( pz_fmt, text, rm );
301 * Dump out the rest of the file
303 fputs (text, stdout);
307 /* Scan the input file for all occurrences of text like this:
309 #define TIOCCONS _IO(T, 12)
311 and change them to read like this:
313 #define TIOCCONS _IO('T', 12)
315 which is the required syntax per the C standard. (The definition of
316 _IO also has to be tweaked - see below.) 'IO' is actually whatever you
317 provide as the `c_fix_arg' argument. */
319 FIX_PROC_HEAD( char_macro_use_fix )
321 /* This regexp looks for a traditional-syntax #define (# in column 1)
322 of an object-like macro. */
323 static const char pat[] =
324 "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
327 const char* str = p_fixd->patch_args[1];
329 const char *p, *limit;
335 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
340 compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
343 regexec (&re, p, 1, rm, 0) == 0;
346 /* p + rm[0].rm_eo is the first character of the macro replacement.
347 Find the end of the macro replacement, and the STR we were
348 sent to look for within the replacement. */
353 limit = strchr (limit + 1, '\n');
357 while (limit[-1] == '\\');
361 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
364 while (++p < limit - len);
365 /* Hit end of line. */
369 /* Found STR on this line. If the macro needs fixing,
370 the next few chars will be whitespace or uppercase,
371 then an open paren, then a single letter. */
372 while ((ISSPACE (*p) || ISUPPER (*p)) && p < limit) p++;
380 /* Splat all preceding text into the output buffer,
381 quote the character at p, then proceed. */
382 fwrite (text, 1, p - text, stdout);
389 fputs (text, stdout);
393 /* Scan the input file for all occurrences of text like this:
395 #define xxxIOxx(x, y) (....'x'<<16....)
397 and change them to read like this:
399 #define xxxIOxx(x, y) (....x<<16....)
401 which is the required syntax per the C standard. (The uses of _IO
402 also has to be tweaked - see above.) 'IO' is actually whatever
403 you provide as the `c_fix_arg' argument. */
404 FIX_PROC_HEAD( char_macro_def_fix )
406 /* This regexp looks for any traditional-syntax #define (# in column 1). */
407 static const char pat[] =
408 "^#[ \t]*define[ \t]+";
411 const char* str = p_fixd->patch_args[1];
413 const char *p, *limit;
420 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
425 compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
428 regexec (&re, p, 1, rm, 0) == 0;
431 /* p + rm[0].rm_eo is the first character of the macro name.
432 Find the end of the macro replacement, and the STR we were
433 sent to look for within the name. */
438 limit = strchr (limit + 1, '\n');
442 while (limit[-1] == '\\');
446 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
450 while (ISIDNUM (*p));
451 /* Hit end of macro name without finding the string. */
455 /* Found STR in this macro name. If the macro needs fixing,
456 there may be a few uppercase letters, then there will be an
457 open paren with _no_ intervening whitespace, and then a
459 while (ISUPPER (*p) && p < limit) p++;
467 /* The character at P is the one to look for in the following
474 if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
476 /* Remove the quotes from this use of ARG. */
478 fwrite (text, 1, p - text, stdout);
488 fputs (text, stdout);
491 /* Fix for machine name #ifdefs that are not in the namespace reserved
492 by the C standard. They won't be defined if compiling with -ansi,
493 and the headers will break. We go to some trouble to only change
494 #ifdefs where the macro is defined by GCC in non-ansi mode; this
495 minimizes the number of headers touched. */
497 #define SCRATCHSZ 64 /* hopefully long enough */
499 FIX_PROC_HEAD( machine_name_fix )
502 fputs( "The target machine has no needed machine name fixes\n", stderr );
505 const char *line, *base, *limit, *p, *q;
506 regex_t *label_re, *name_re;
507 char scratch[SCRATCHSZ];
512 mn_get_regexps (&label_re, &name_re, "machine_name_fix");
518 regexec (label_re, base, 2, match, 0) == 0;
521 base += match[0].rm_eo;
522 /* We're looking at an #if or #ifdef. Scan forward for the
523 next non-escaped newline. */
528 limit = strchr (limit, '\n');
532 while (limit[-1] == '\\');
534 /* If the 'name_pat' matches in between base and limit, we have
535 a bogon. It is not worth the hassle of excluding comments
536 because comments on #if/#ifdef lines are rare, and strings on
537 such lines are illegal.
539 REG_NOTBOL means 'base' is not at the beginning of a line, which
540 shouldn't matter since the name_re has no ^ anchor, but let's
541 be accurate anyway. */
549 if (regexec (name_re, base, 1, match, REG_NOTBOL))
550 goto done; /* No remaining match in this file */
552 /* Match; is it on the line? */
553 if (match[0].rm_eo > limit - base)
556 p = base + match[0].rm_so;
557 base += match[0].rm_eo;
559 /* One more test: if on the same line we have the same string
560 with the appropriate underscores, then leave it alone.
561 We want exactly two leading and trailing underscores. */
564 len = base - p - ((*base == '_') ? 2 : 1);
569 len = base - p - ((*base == '_') ? 1 : 0);
572 if (len + 4 > SCRATCHSZ)
574 memcpy (&scratch[2], q, len);
576 scratch[len++] = '_';
577 scratch[len++] = '_';
579 for (q = line; q <= limit - len; q++)
580 if (*q == '_' && !strncmp (q, scratch, len))
583 fwrite (text, 1, p - text, stdout);
584 fwrite (scratch, 1, len, stdout);
591 fputs (text, stdout);
595 FIX_PROC_HEAD( wrap_fix )
597 tSCC z_no_wrap_pat[] = "^#if.*__need_";
598 static regex_t no_wrapping_re; /* assume zeroed data */
600 char z_fixname[ 64 ];
601 tCC* pz_src = p_fixd->fix_name;
602 tCC* pz_name = z_fixname;
603 char* pz_dst = z_fixname;
608 if (no_wrapping_re.allocated == 0)
609 compile_re( z_no_wrap_pat, &no_wrapping_re, 0, "no-wrap pattern",
618 } else if (! ISALNUM (ch)) {
621 *pz_dst++ = TOUPPER (ch);
624 if (++len >= sizeof( z_fixname )) {
625 void* p = xmalloc( len + strlen( pz_src ) + 1 );
626 memcpy( p, (void*)z_fixname, len );
628 pz_dst = (char*)pz_name + len;
633 * IF we do *not* match the no-wrap re, then we have a double negative.
634 * A double negative means YES.
636 if (regexec (&no_wrapping_re, text, 0, NULL, 0) != 0)
638 printf( "#ifndef FIXINC_%s_CHECK\n", pz_name );
639 printf( "#define FIXINC_%s_CHECK 1\n\n", pz_name );
643 if (p_fixd->patch_args[1] == (tCC*)NULL)
644 fputs( text, stdout );
647 fputs( p_fixd->patch_args[1], stdout );
648 fputs( text, stdout );
649 if (p_fixd->patch_args[2] != (tCC*)NULL)
650 fputs( p_fixd->patch_args[2], stdout );
654 printf( "\n#endif /* FIXINC_%s_CHECK */\n", pz_name );
656 if (pz_name != z_fixname)
657 free( (void*)pz_name );
662 * Search for multiple copies of a regular expression. Each block
663 * of matched text is replaced with the format string, as described
664 * above in `format_write'.
666 FIX_PROC_HEAD( gnu_type_fix )
670 regmatch_t rm[GTYPE_SE_CT+1];
674 tTestDesc* pTD = p_fixd->p_test_desc;
675 int ct = p_fixd->test_ct;
680 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
684 if (pTD->type == TT_EGREP)
686 pz_pat = pTD->pz_test_text;
694 compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
696 while (regexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
698 text = emit_gnu_type (text, rm);
702 * Dump out the rest of the file
704 fputs (text, stdout);
708 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
710 test for fix selector
712 THIS IS THE ONLY EXPORTED ROUTINE
716 apply_fix( p_fixd, filname )
720 #define _FT_(n,p) { n, p },
721 static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
723 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
725 tCC* fixname = p_fixd->patch_args[0];
727 int ct = FIX_TABLE_CT;
728 fix_entry_t* pfe = fix_table;
732 if (strcmp (pfe->fix_name, fixname) == 0)
736 fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
743 buf = load_file_data (stdin);
744 (*pfe->fix_proc)( filname, buf, p_fixd );
747 #ifdef SEPARATE_FIX_PROC
749 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
751 "FS error %d (%s) reopening %s as std%s\n";
766 fputs (z_usage, stderr);
774 if (! ISDIGIT ( *pz ))
777 idx = strtol (pz, &pz, 10);
778 if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
780 pFix = fixDescList + idx;
783 if (freopen (argv[3], "r", stdin) != stdin)
785 fprintf (stderr, z_reopen, errno, strerror( errno ), argv[3], "in");
789 pz_tmptmp = (char*)xmalloc( strlen( argv[4] ) + 5 );
790 strcpy( pz_tmptmp, argv[4] );
792 /* Don't lose because "12345678" and "12345678X" map to the same
793 file under DOS restricted 8+3 file namespace. Note that DOS
794 doesn't allow more than one dot in the trunk of a file name. */
795 pz_tmp_base = basename( pz_tmptmp );
796 pz_tmp_dot = strchr( pz_tmp_base, '.' );
797 if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
798 && pz_tmp_dot != (char*)NULL)
799 strcpy (pz_tmp_dot+1, "X"); /* nuke the original extension */
801 strcat (pz_tmptmp, ".X");
802 if (freopen (pz_tmptmp, "w", stdout) != stdout)
804 fprintf (stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out");
808 apply_fix (pFix, argv[1]);
812 if (rename (pz_tmptmp, argv[4]) != 0)
814 fprintf (stderr, "error %d (%s) renaming %s to %s\n", errno,
815 strerror( errno ), pz_tmptmp, argv[4]);