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 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)
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. */
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 *));
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 t_fix_proc fix; /* avoid warning */ \
86 static void fix ( filname, text, p_fixd ) \
87 const char* filname; \
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.
99 print_quote( q, text )
116 fputc( *(text++), stdout );
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.
142 emit_gnu_type ( text, rm )
149 fwrite (text, rm[0].rm_so, 1, stdout);
152 const char* ps = text + rm[1].rm_so;
153 const char* pe = text + rm[1].rm_eo;
158 *(pD++) = toupper( *(pd++) = *(ps++) );
164 * Now print out the reformed typedef,
165 * with a C++ guard for WCHAR
169 #if !defined(_GCC_%s_T)%s\n\
171 typedef __%s_TYPE__ %s_t;\n\
174 const char* pz_guard = (strcmp (z_type, "wchar") == 0)
175 ? " && ! defined(__cplusplus)" : "";
177 printf (z_fmt, z_TYPE, pz_guard, z_TYPE, z_TYPE, z_type);
180 return text += rm[0].rm_eo;
185 * Copy the `format' string to std out, replacing `%n' expressions
186 * with the matched text from a regular expression evaluation.
187 * Doubled '%' characters will be replaced with a single copy.
188 * '%' characters in other contexts and all other characters are
189 * copied out verbatim.
192 format_write (format, text, av)
199 while ((c = (unsigned)*(format++)) != NUL) {
207 c = (unsigned)*(format++);
210 * IF the character following a '%' is not a digit,
211 * THEN we will always emit a '%' and we may or may
212 * not emit the following character. We will end on
213 * a NUL and we will emit only one of a pair of '%'.
229 * Emit the matched subexpression numbered 'c'.
230 * IF, of course, there was such a match...
233 regmatch_t* pRM = av + (c - (unsigned)'0');
239 len = pRM->rm_eo - pRM->rm_so;
241 fwrite(text + pRM->rm_so, len, 1, stdout);
248 * Search for multiple copies of a regular expression. Each block
249 * of matched text is replaced with the format string, as described
250 * above in `format_write'.
252 FIX_PROC_HEAD( format_fix )
254 tCC* pz_pat = p_fixd->patch_args[2];
255 tCC* pz_fmt = p_fixd->patch_args[1];
260 * We must have a format
262 if (pz_fmt == (tCC*)NULL)
264 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
269 * IF we don't have a search text, then go find the first
270 * regular expression among the tests.
272 if (pz_pat == (tCC*)NULL)
274 tTestDesc* pTD = p_fixd->p_test_desc;
275 int ct = p_fixd->test_ct;
280 fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
284 if (pTD->type == TT_EGREP)
286 pz_pat = pTD->pz_test_text;
295 * Replace every copy of the text we find
297 compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
298 while (regexec (&re, text, 10, rm, 0) == 0)
300 fwrite( text, rm[0].rm_so, 1, stdout );
301 format_write( pz_fmt, text, rm );
306 * Dump out the rest of the file
308 fputs (text, stdout);
312 /* Scan the input file for all occurrences of text like this:
314 #define TIOCCONS _IO(T, 12)
316 and change them to read like this:
318 #define TIOCCONS _IO('T', 12)
320 which is the required syntax per the C standard. (The definition of
321 _IO also has to be tweaked - see below.) 'IO' is actually whatever you
322 provide as the `c_fix_arg' argument. */
324 FIX_PROC_HEAD( char_macro_use_fix )
326 /* This regexp looks for a traditional-syntax #define (# in column 1)
327 of an object-like macro. */
328 static const char pat[] =
329 "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
332 const char* str = p_fixd->patch_args[1];
334 const char *p, *limit;
339 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
344 compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
347 regexec (&re, p, 1, rm, 0) == 0;
350 /* p + rm[0].rm_eo is the first character of the macro replacement.
351 Find the end of the macro replacement, and the STR we were
352 sent to look for within the replacement. */
357 limit = strchr (limit + 1, '\n');
361 while (limit[-1] == '\\');
365 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
368 while (++p < limit - len);
369 /* Hit end of line. */
373 /* Found STR on this line. If the macro needs fixing,
374 the next few chars will be whitespace or uppercase,
375 then an open paren, then a single letter. */
376 while ((isspace (*p) || isupper (*p)) && p < limit) p++;
381 if (isalnum (p[1]) || p[1] == '_')
384 /* Splat all preceding text into the output buffer,
385 quote the character at p, then proceed. */
386 fwrite (text, 1, p - text, stdout);
393 fputs (text, stdout);
397 /* Scan the input file for all occurrences of text like this:
399 #define xxxIOxx(x, y) (....'x'<<16....)
401 and change them to read like this:
403 #define xxxIOxx(x, y) (....x<<16....)
405 which is the required syntax per the C standard. (The uses of _IO
406 also has to be tweaked - see above.) 'IO' is actually whatever
407 you provide as the `c_fix_arg' argument. */
408 FIX_PROC_HEAD( char_macro_def_fix )
410 /* This regexp looks for any traditional-syntax #define (# in column 1). */
411 static const char pat[] =
412 "^#[ \t]*define[ \t]+";
415 const char* str = p_fixd->patch_args[1];
417 const char *p, *limit;
423 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
428 compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
431 regexec (&re, p, 1, rm, 0) == 0;
434 /* p + rm[0].rm_eo is the first character of the macro name.
435 Find the end of the macro replacement, and the STR we were
436 sent to look for within the name. */
441 limit = strchr (limit + 1, '\n');
445 while (limit[-1] == '\\');
449 if (*p == str[0] && !strncmp (p+1, str+1, len-1))
453 while (isalpha (*p) || isalnum (*p) || *p == '_');
454 /* Hit end of macro name without finding the string. */
458 /* Found STR in this macro name. If the macro needs fixing,
459 there may be a few uppercase letters, then there will be an
460 open paren with _no_ intervening whitespace, and then a
462 while (isupper (*p) && p < limit) p++;
467 if (isalnum (p[1]) || p[1] == '_')
470 /* The character at P is the one to look for in the following
477 if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
479 /* Remove the quotes from this use of ARG. */
481 fwrite (text, 1, p - text, stdout);
491 fputs (text, stdout);
494 /* Fix for machine name #ifdefs that are not in the namespace reserved
495 by the C standard. They won't be defined if compiling with -ansi,
496 and the headers will break. We go to some trouble to only change
497 #ifdefs where the macro is defined by GCC in non-ansi mode; this
498 minimizes the number of headers touched. */
500 #define SCRATCHSZ 64 /* hopefully long enough */
502 FIX_PROC_HEAD( machine_name_fix )
505 fputs( "The target machine has no needed machine name fixes\n", stderr );
508 const char *line, *base, *limit, *p, *q;
509 regex_t *label_re, *name_re;
510 char scratch[SCRATCHSZ];
513 mn_get_regexps (&label_re, &name_re, "machine_name_fix");
519 regexec (label_re, base, 2, match, 0) == 0;
522 base += match[0].rm_eo;
523 /* We're looking at an #if or #ifdef. Scan forward for the
524 next non-escaped newline. */
529 limit = strchr (limit, '\n');
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. */
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)
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. */
565 len = base - p - ((*base == '_') ? 2 : 1);
570 len = base - p - ((*base == '_') ? 1 : 0);
573 if (len + 4 > SCRATCHSZ)
575 memcpy (&scratch[2], q, len);
577 scratch[len++] = '_';
578 scratch[len++] = '_';
580 for (q = line; q <= limit - len; q++)
581 if (*q == '_' && !strncmp (q, scratch, len))
584 fwrite (text, 1, p - text, stdout);
585 fwrite (scratch, 1, len, stdout);
592 fputs (text, stdout);
596 FIX_PROC_HEAD( wrap_fix )
598 char z_fixname[ 64 ];
599 tCC* pz_src = p_fixd->fix_name;
600 tCC* pz_name = z_fixname;
601 char* pz_dst = z_fixname;
605 char ch = *(pz_src++);
608 *(pz_dst++) = toupper( ch );
610 else if (isalnum( ch ))
613 else if (ch == NUL) {
620 if (++len >= sizeof( z_fixname )) {
621 void* p = xmalloc( len + strlen( pz_src ) + 1 );
622 memcpy( p, (void*)z_fixname, len );
624 pz_dst = (char*)pz_name + len;
628 printf( "#ifndef FIXINC_%s_CHECK\n", pz_name );
629 printf( "#define FIXINC_%s_CHECK 1\n\n", pz_name );
631 if (p_fixd->patch_args[1] == (tCC*)NULL)
632 fputs( text, stdout );
635 fputs( p_fixd->patch_args[1], stdout );
636 fputs( text, stdout );
637 if (p_fixd->patch_args[2] != (tCC*)NULL)
638 fputs( p_fixd->patch_args[2], stdout );
641 printf( "\n#endif /* FIXINC_%s_CHECK */\n", pz_name );
642 if (pz_name != z_fixname)
643 free( (void*)pz_name );
648 * Search for multiple copies of a regular expression. Each block
649 * of matched text is replaced with the format string, as described
650 * above in `format_write'.
652 FIX_PROC_HEAD( gnu_type_fix )
656 regmatch_t rm[GTYPE_SE_CT+1];
659 tTestDesc* pTD = p_fixd->p_test_desc;
660 int ct = p_fixd->test_ct;
665 fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
669 if (pTD->type == TT_EGREP)
671 pz_pat = pTD->pz_test_text;
679 compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
681 while (regexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
683 text = emit_gnu_type (text, rm);
687 * Dump out the rest of the file
689 fputs (text, stdout);
693 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
695 test for fix selector
697 THIS IS THE ONLY EXPORTED ROUTINE
701 apply_fix( p_fixd, filname )
705 #define _FT_(n,p) { n, p },
706 static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
708 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
710 tCC* fixname = p_fixd->patch_args[0];
712 int ct = FIX_TABLE_CT;
713 fix_entry_t* pfe = fix_table;
717 if (strcmp (pfe->fix_name, fixname) == 0)
721 fprintf (stderr, "fixincl error: the `%s' fix is unknown\n",
728 buf = load_file_data (stdin);
729 (*pfe->fix_proc)( filname, buf, p_fixd );
734 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
736 "FS error %d (%s) reopening %s as std%s\n";
751 fputs( z_usage, stderr );
759 if (! isdigit( *pz ))
762 idx = strtol( pz, &pz, 10 );
763 if ((*pz != NUL) || ((unsigned)idx >= FIX_COUNT))
765 pFix = fixDescList + idx;
768 if (freopen( argv[3], "r", stdin ) != stdin)
770 fprintf( stderr, z_reopen, errno, strerror( errno ), argv[3], "in" );
774 pz_tmptmp = (char*)xmalloc( strlen( argv[4] ) + 5 );
775 strcpy( pz_tmptmp, argv[4] );
777 /* Don't lose because "12345678" and "12345678X" map to the same
778 file under DOS restricted 8+3 file namespace. Note that DOS
779 doesn't allow more than one dot in the trunk of a file name. */
780 pz_tmp_base = basename( pz_tmptmp );
781 pz_tmp_dot = strchr( pz_tmp_base, '.' );
782 if (pathconf( pz_tmptmp, _PC_NAME_MAX ) <= 12 /* is this DOS or Windows9X? */
783 && pz_tmp_dot != (char*)NULL)
784 strcpy( pz_tmp_dot+1, "X" ); /* nuke the original extension */
786 strcat( pz_tmptmp, ".X" );
787 if (freopen( pz_tmptmp, "w", stdout ) != stdout)
789 fprintf( stderr, z_reopen, errno, strerror( errno ), pz_tmptmp, "out" );
793 apply_fix( pFix, argv[1] );
794 close( STDOUT_FILENO );
795 close( STDIN_FILENO );
797 if (rename( pz_tmptmp, argv[4] ) != 0)
799 fprintf( stderr, "error %d (%s) renaming %s to %s\n", errno,
800 strerror( errno ), pz_tmptmp, argv[4] );