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
PARAMS ((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 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.
92 print_quote( q
, text
)
109 fputc( *(text
++), stdout
);
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
*));
136 emit_gnu_type ( text
, rm
)
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
;
152 *(pD
++) = TOUPPER( *(pd
++) = *(ps
++) );
158 * Now print out the reformed typedef,
159 * with a C++ guard for WCHAR
163 #if !defined(_GCC_%s_T)%s\n\
165 typedef __%s_TYPE__ %s_t;\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
[]));
187 format_write (format
, text
, av
)
194 while ((c
= (unsigned)*(format
++)) != NUL
) {
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 '%'.
224 * Emit the matched subexpression numbered 'c'.
225 * IF, of course, there was such a match...
228 regmatch_t
* pRM
= av
+ (c
- (unsigned)'0');
234 len
= pRM
->rm_eo
- pRM
->rm_so
;
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];
256 * We must have a format
258 if (pz_fmt
== (tCC
*)NULL
)
260 fprintf( stderr
, zNeedsArg
, p_fixd
->fix_name
, "replacement format", 0 );
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
;
276 fprintf( stderr
, zNeedsArg
, p_fixd
->fix_name
, "search text", 1 );
280 if (pTD
->type
== TT_EGREP
)
282 pz_pat
= pTD
->pz_test_text
;
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
);
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]+";
328 const char* str
= p_fixd
->patch_args
[1];
330 const char *p
, *limit
;
336 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "ioctl type", 0);
341 compile_re (pat
, &re
, 1, "macro pattern", "char_macro_use_fix");
344 regexec (&re
, p
, 1, rm
, 0) == 0;
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. */
354 limit
= strchr (limit
+ 1, '\n');
358 while (limit
[-1] == '\\');
362 if (*p
== str
[0] && !strncmp (p
+1, str
+1, len
-1))
365 while (++p
< limit
- len
);
366 /* Hit end of line. */
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
++;
381 /* Splat all preceding text into the output buffer,
382 quote the character at p, then proceed. */
383 fwrite (text
, 1, p
- text
, stdout
);
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]+";
412 const char* str
= p_fixd
->patch_args
[1];
414 const char *p
, *limit
;
421 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "ioctl type", 0);
426 compile_re (pat
, &re
, 1, "macro pattern", "fix_char_macro_defines");
429 regexec (&re
, p
, 1, rm
, 0) == 0;
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. */
439 limit
= strchr (limit
+ 1, '\n');
443 while (limit
[-1] == '\\');
447 if (*p
== str
[0] && !strncmp (p
+1, str
+1, len
-1))
451 while (ISIDNUM (*p
));
452 /* Hit end of macro name without finding the string. */
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
460 while (ISUPPER (*p
) && p
< limit
) p
++;
468 /* The character at P is the one to look for in the following
475 if (p
[-1] == '\'' && p
[0] == arg
&& p
[1] == '\'')
477 /* Remove the quotes from this use of ARG. */
479 fwrite (text
, 1, p
- text
, stdout
);
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
)
503 fputs( "The target machine has no needed machine name fixes\n", stderr
);
506 const char *line
, *base
, *limit
, *p
, *q
;
507 regex_t
*label_re
, *name_re
;
508 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 tSCC z_no_wrap_pat
[] = "^#if.*__need_";
599 static regex_t no_wrapping_re
; /* assume zeroed data */
603 if (no_wrapping_re
.allocated
== 0)
604 compile_re( z_no_wrap_pat
, &no_wrapping_re
, 0, "no-wrap pattern",
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
);
621 sprintf( pz
, "FIXINC_WRAP_%s-%s", filname
, p_fixd
->fix_name
);
623 for (pz
+= 12; 1; pz
++) {
629 if (! ISALNUM( 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
);
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
)
667 regmatch_t rm
[GTYPE_SE_CT
+1];
671 tTestDesc
* pTD
= p_fixd
->p_test_desc
;
672 int ct
= p_fixd
->test_ct
;
677 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "search text", 1);
681 if (pTD
->type
== TT_EGREP
)
683 pz_pat
= pTD
->pz_test_text
;
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
713 apply_fix( p_fixd
, filname
)
717 #define _FT_(n,p) { n, p },
718 static fix_entry_t fix_table
[] = { FIXUP_TABLE
{ NULL
, NULL
}};
720 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
722 tCC
* fixname
= p_fixd
->patch_args
[0];
724 int ct
= FIX_TABLE_CT
;
725 fix_entry_t
* pfe
= fix_table
;
729 if (strcmp (pfe
->fix_name
, fixname
) == 0)
733 fprintf (stderr
, "fixincl error: the `%s' fix is unknown\n",
740 buf
= load_file_data (stdin
);
741 (*pfe
->fix_proc
)( filname
, buf
, p_fixd
);
744 #ifdef SEPARATE_FIX_PROC
746 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
748 "FS error %d (%s) reopening %s as std%s\n";
763 fputs (z_usage
, stderr
);
771 if (! ISDIGIT ( *pz
))
774 idx
= strtol (pz
, &pz
, 10);
775 if ((*pz
!= NUL
) || ((unsigned)idx
>= FIX_COUNT
))
777 pFix
= fixDescList
+ idx
;
780 if (freopen (argv
[3], "r", stdin
) != stdin
)
782 fprintf (stderr
, z_reopen
, errno
, strerror( errno
), argv
[3], "in");
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 */
798 strcat (pz_tmptmp
, ".X");
799 if (freopen (pz_tmptmp
, "w", stdout
) != stdout
)
801 fprintf (stderr
, z_reopen
, errno
, strerror( errno
), pz_tmptmp
, "out");
805 apply_fix (pFix
, argv
[1]);
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]);