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 = = = = = = = = = = = = = = = = = = = = = = = = =
13 The routines you write here must work closely with fixincl.c.
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)
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. */
56 #ifdef SEPARATE_FIX_PROC
60 tSCC zNeedsArg
[] = "fixincl error: `%s' needs %s argument (c_fix_arg[%d])\n";
62 typedef void t_fix_proc (const char *, const char *, tFixDesc
*) ;
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 (const char* filname ATTRIBUTE_UNUSED , \
79 const char* text ATTRIBUTE_UNUSED , \
80 tFixDesc* p_fixd ATTRIBUTE_UNUSED )
82 #ifdef NEED_PRINT_QUOTE
84 * Skip over a quoted string. Single quote strings may
85 * contain multiple characters if the first character is
86 * a backslash. Especially a backslash followed by octal digits.
87 * We are not doing a correctness syntax check here.
90 print_quote(char q
, char* text
)
105 fputc( *(text
++), stdout
);
122 #endif /* NEED_PRINT_QUOTE */
126 * Emit the GNU standard type wrapped up in such a way that
127 * this thing can be encountered countless times during a compile
128 * and not cause even a warning.
131 emit_gnu_type (const char* text
, regmatch_t
* rm
)
136 fwrite (text
, rm
[0].rm_so
, 1, stdout
);
139 const char* ps
= text
+ rm
[1].rm_so
;
140 const char* pe
= text
+ rm
[1].rm_eo
;
145 *(pD
++) = TOUPPER( *(pd
++) = *(ps
++) );
151 * Now print out the reformed typedef,
152 * with a C++ guard for WCHAR
156 #if !defined(_GCC_%s_T)%s\n\
158 typedef __%s_TYPE__ %s_t;\n\
161 const char *const pz_guard
= (strcmp (z_type
, "wchar") == 0)
162 ? " && ! defined(__cplusplus)" : "";
164 printf (z_fmt
, z_TYPE
, pz_guard
, z_TYPE
, z_TYPE
, z_type
);
167 return text
+= rm
[0].rm_eo
;
172 * Copy the `format' string to std out, replacing `%n' expressions
173 * with the matched text from a regular expression evaluation.
174 * Doubled '%' characters will be replaced with a single copy.
175 * '%' characters in other contexts and all other characters are
176 * copied out verbatim.
179 format_write (tCC
* format
, tCC
* text
, regmatch_t av
[] )
183 while ((c
= (unsigned)*(format
++)) != NUL
) {
191 c
= (unsigned)*(format
++);
194 * IF the character following a '%' is not a digit,
195 * THEN we will always emit a '%' and we may or may
196 * not emit the following character. We will end on
197 * a NUL and we will emit only one of a pair of '%'.
213 * Emit the matched subexpression numbered 'c'.
214 * IF, of course, there was such a match...
217 regmatch_t
* pRM
= av
+ (c
- (unsigned)'0');
223 len
= pRM
->rm_eo
- pRM
->rm_so
;
225 fwrite(text
+ pRM
->rm_so
, len
, 1, stdout
);
232 * Search for multiple copies of a regular expression. Each block
233 * of matched text is replaced with the format string, as described
234 * above in `format_write'.
236 FIX_PROC_HEAD( format_fix
)
238 tCC
* pz_pat
= p_fixd
->patch_args
[2];
239 tCC
* pz_fmt
= p_fixd
->patch_args
[1];
245 * We must have a format
247 if (pz_fmt
== (tCC
*)NULL
)
249 fprintf( stderr
, zNeedsArg
, p_fixd
->fix_name
, "replacement format", 0 );
254 * IF we don't have a search text, then go find the first
255 * regular expression among the tests.
257 if (pz_pat
== (tCC
*)NULL
)
259 tTestDesc
* pTD
= p_fixd
->p_test_desc
;
260 int ct
= p_fixd
->test_ct
;
265 fprintf( stderr
, zNeedsArg
, p_fixd
->fix_name
, "search text", 1 );
269 if (pTD
->type
== TT_EGREP
)
271 pz_pat
= pTD
->pz_test_text
;
280 * Replace every copy of the text we find
282 compile_re (pz_pat
, &re
, 1, "format search-text", "format_fix" );
283 while (xregexec (&re
, text
, 10, rm
, 0) == 0)
285 fwrite( text
, rm
[0].rm_so
, 1, stdout
);
286 format_write( pz_fmt
, text
, rm
);
291 * Dump out the rest of the file
293 fputs (text
, stdout
);
297 /* Scan the input file for all occurrences of text like this:
299 #define TIOCCONS _IO(T, 12)
301 and change them to read like this:
303 #define TIOCCONS _IO('T', 12)
305 which is the required syntax per the C standard. (The definition of
306 _IO also has to be tweaked - see below.) 'IO' is actually whatever you
307 provide as the `c_fix_arg' argument. */
309 FIX_PROC_HEAD( char_macro_use_fix
)
311 /* This regexp looks for a traditional-syntax #define (# in column 1)
312 of an object-like macro. */
313 static const char pat
[] =
314 "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
317 const char* str
= p_fixd
->patch_args
[1];
319 const char *p
, *limit
;
325 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "ioctl type", 0);
330 compile_re (pat
, &re
, 1, "macro pattern", "char_macro_use_fix");
333 xregexec (&re
, p
, 1, rm
, 0) == 0;
336 /* p + rm[0].rm_eo is the first character of the macro replacement.
337 Find the end of the macro replacement, and the STR we were
338 sent to look for within the replacement. */
343 limit
= strchr (limit
+ 1, '\n');
347 while (limit
[-1] == '\\');
351 if (*p
== str
[0] && !strncmp (p
+1, str
+1, len
-1))
354 while (++p
< limit
- len
);
355 /* Hit end of line. */
359 /* Found STR on this line. If the macro needs fixing,
360 the next few chars will be whitespace or uppercase,
361 then an open paren, then a single letter. */
362 while ((ISSPACE (*p
) || ISUPPER (*p
)) && p
< limit
) p
++;
370 /* Splat all preceding text into the output buffer,
371 quote the character at p, then proceed. */
372 fwrite (text
, 1, p
- text
, stdout
);
379 fputs (text
, stdout
);
383 /* Scan the input file for all occurrences of text like this:
385 #define xxxIOxx(x, y) (....'x'<<16....)
387 and change them to read like this:
389 #define xxxIOxx(x, y) (....x<<16....)
391 which is the required syntax per the C standard. (The uses of _IO
392 also has to be tweaked - see above.) 'IO' is actually whatever
393 you provide as the `c_fix_arg' argument. */
394 FIX_PROC_HEAD( char_macro_def_fix
)
396 /* This regexp looks for any traditional-syntax #define (# in column 1). */
397 static const char pat
[] =
398 "^#[ \t]*define[ \t]+";
401 const char* str
= p_fixd
->patch_args
[1];
403 const char *p
, *limit
;
410 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "ioctl type", 0);
415 compile_re (pat
, &re
, 1, "macro pattern", "fix_char_macro_defines");
418 xregexec (&re
, p
, 1, rm
, 0) == 0;
421 /* p + rm[0].rm_eo is the first character of the macro name.
422 Find the end of the macro replacement, and the STR we were
423 sent to look for within the name. */
428 limit
= strchr (limit
+ 1, '\n');
432 while (limit
[-1] == '\\');
436 if (*p
== str
[0] && !strncmp (p
+1, str
+1, len
-1))
440 while (ISIDNUM (*p
));
441 /* Hit end of macro name without finding the string. */
445 /* Found STR in this macro name. If the macro needs fixing,
446 there may be a few uppercase letters, then there will be an
447 open paren with _no_ intervening whitespace, and then a
449 while (ISUPPER (*p
) && p
< limit
) p
++;
457 /* The character at P is the one to look for in the following
464 if (p
[-1] == '\'' && p
[0] == arg
&& p
[1] == '\'')
466 /* Remove the quotes from this use of ARG. */
468 fwrite (text
, 1, p
- text
, stdout
);
478 fputs (text
, stdout
);
481 /* Fix for machine name #ifdefs that are not in the namespace reserved
482 by the C standard. They won't be defined if compiling with -ansi,
483 and the headers will break. We go to some trouble to only change
484 #ifdefs where the macro is defined by GCC in non-ansi mode; this
485 minimizes the number of headers touched. */
487 #define SCRATCHSZ 64 /* hopefully long enough */
489 FIX_PROC_HEAD( machine_name_fix
)
492 const char *line
, *base
, *limit
, *p
, *q
;
493 regex_t
*label_re
, *name_re
;
494 char scratch
[SCRATCHSZ
];
499 if (!mn_get_regexps (&label_re
, &name_re
, "machine_name_fix"))
501 fputs( "The target machine has no needed machine name fixes\n", stderr
);
509 xregexec (label_re
, base
, 2, match
, 0) == 0;
512 base
+= match
[0].rm_eo
;
513 /* We're looking at an #if or #ifdef. Scan forward for the
514 next non-escaped newline. */
519 limit
= strchr (limit
, '\n');
523 while (limit
[-1] == '\\');
525 /* If the 'name_pat' matches in between base and limit, we have
526 a bogon. It is not worth the hassle of excluding comments
527 because comments on #if/#ifdef lines are rare, and strings on
528 such lines are illegal.
530 REG_NOTBOL means 'base' is not at the beginning of a line, which
531 shouldn't matter since the name_re has no ^ anchor, but let's
532 be accurate anyway. */
540 if (xregexec (name_re
, base
, 1, match
, REG_NOTBOL
))
541 goto done
; /* No remaining match in this file */
543 /* Match; is it on the line? */
544 if (match
[0].rm_eo
> limit
- base
)
547 p
= base
+ match
[0].rm_so
;
548 base
+= match
[0].rm_eo
;
550 /* One more test: if on the same line we have the same string
551 with the appropriate underscores, then leave it alone.
552 We want exactly two leading and trailing underscores. */
555 len
= base
- p
- ((*base
== '_') ? 2 : 1);
560 len
= base
- p
- ((*base
== '_') ? 1 : 0);
563 if (len
+ 4 > SCRATCHSZ
)
565 memcpy (&scratch
[2], q
, len
);
567 scratch
[len
++] = '_';
568 scratch
[len
++] = '_';
570 for (q
= line
; q
<= limit
- len
; q
++)
571 if (*q
== '_' && !strncmp (q
, scratch
, len
))
574 fwrite (text
, 1, p
- text
, stdout
);
575 fwrite (scratch
, 1, len
, stdout
);
581 fputs (text
, stdout
);
585 FIX_PROC_HEAD( wrap_fix
)
587 tSCC z_no_wrap_pat
[] = "^#if.*__need_";
588 static regex_t no_wrapping_re
; /* assume zeroed data */
592 if (no_wrapping_re
.allocated
== 0)
593 compile_re( z_no_wrap_pat
, &no_wrapping_re
, 0, "no-wrap pattern",
597 * IF we do *not* match the no-wrap re, then we have a double negative.
598 * A double negative means YES.
600 if (xregexec( &no_wrapping_re
, text
, 0, NULL
, 0 ) != 0)
603 * A single file can get wrapped more than once by different fixes.
604 * A single fix can wrap multiple files. Therefore, guard with
605 * *both* the fix name and the file name.
607 size_t ln
= strlen( filname
) + strlen( p_fixd
->fix_name
) + 14;
608 char* pz
= xmalloc( ln
);
610 sprintf( pz
, "FIXINC_WRAP_%s-%s", filname
, p_fixd
->fix_name
);
612 for (pz
+= 12; 1; pz
++) {
618 if (! ISALNUM( ch
)) {
626 printf( "#ifndef %s\n", pz_name
);
627 printf( "#define %s 1\n\n", pz_name
);
630 if (p_fixd
->patch_args
[1] == (tCC
*)NULL
)
631 fputs( text
, stdout
);
634 fputs( p_fixd
->patch_args
[1], stdout
);
635 fputs( text
, stdout
);
636 if (p_fixd
->patch_args
[2] != (tCC
*)NULL
)
637 fputs( p_fixd
->patch_args
[2], stdout
);
640 if (pz_name
!= NULL
) {
641 printf( "\n#endif /* %s */\n", pz_name
);
642 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];
660 tTestDesc
* pTD
= p_fixd
->p_test_desc
;
661 int ct
= p_fixd
->test_ct
;
666 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "search text", 1);
670 if (pTD
->type
== TT_EGREP
)
672 pz_pat
= pTD
->pz_test_text
;
680 compile_re (pz_pat
, &re
, 1, "gnu type typedef", "gnu_type_fix");
682 while (xregexec (&re
, text
, GTYPE_SE_CT
+1, rm
, 0) == 0)
684 text
= emit_gnu_type (text
, rm
);
688 * Dump out the rest of the file
690 fputs (text
, stdout
);
694 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
696 test for fix selector
698 THIS IS THE ONLY EXPORTED ROUTINE
702 apply_fix( tFixDesc
* p_fixd
, tCC
* filname
)
704 #define _FT_(n,p) { n, p },
705 static fix_entry_t fix_table
[] = { FIXUP_TABLE
{ NULL
, NULL
}};
707 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
709 tCC
* fixname
= p_fixd
->patch_args
[0];
711 int ct
= FIX_TABLE_CT
;
712 fix_entry_t
* pfe
= fix_table
;
716 if (strcmp (pfe
->fix_name
, fixname
) == 0)
720 fprintf (stderr
, "fixincl error: the `%s' fix is unknown\n",
727 buf
= load_file_data (stdin
);
728 (*pfe
->fix_proc
)( filname
, buf
, p_fixd
);
731 #ifdef SEPARATE_FIX_PROC
733 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
735 "FS error %d (%s) reopening %s as std%s\n";
738 main( int argc
, char** argv
)
748 fputs (z_usage
, stderr
);
756 if (! ISDIGIT ( *pz
))
759 idx
= strtol (pz
, &pz
, 10);
760 if ((*pz
!= NUL
) || ((unsigned)idx
>= FIX_COUNT
))
762 pFix
= fixDescList
+ idx
;
765 if (freopen (argv
[3], "r", stdin
) != stdin
)
767 fprintf (stderr
, z_reopen
, errno
, strerror( errno
), argv
[3], "in");
771 pz_tmptmp
= xmalloc (strlen (argv
[4]) + 5);
772 strcpy( pz_tmptmp
, argv
[4] );
774 /* Don't lose because "12345678" and "12345678X" map to the same
775 file under DOS restricted 8+3 file namespace. Note that DOS
776 doesn't allow more than one dot in the trunk of a file name. */
777 pz_tmp_base
= basename( pz_tmptmp
);
778 pz_tmp_dot
= strchr( pz_tmp_base
, '.' );
779 if (pathconf( pz_tmptmp
, _PC_NAME_MAX
) <= 12 /* is this DOS or Windows9X? */
780 && pz_tmp_dot
!= (char*)NULL
)
781 strcpy (pz_tmp_dot
+1, "X"); /* nuke the original extension */
783 strcat (pz_tmptmp
, ".X");
784 if (freopen (pz_tmptmp
, "w", stdout
) != stdout
)
786 fprintf (stderr
, z_reopen
, errno
, strerror( errno
), pz_tmptmp
, "out");
790 apply_fix (pFix
, argv
[1]);
794 if (rename (pz_tmptmp
, argv
[4]) != 0)
796 fprintf (stderr
, "error %d (%s) renaming %s to %s\n", errno
,
797 strerror( errno
), pz_tmptmp
, argv
[4]);