4 Test to see if a particular fix should be applied to a header file.
6 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2009
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 3, 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 COPYING3. If not see
50 <http://www.gnu.org/licenses/>. */
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 (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 (const char* filname ATTRIBUTE_UNUSED , \
78 const char* text ATTRIBUTE_UNUSED , \
79 tFixDesc* p_fixd ATTRIBUTE_UNUSED )
81 #ifdef NEED_PRINT_QUOTE
83 * Skip over a quoted string. Single quote strings may
84 * contain multiple characters if the first character is
85 * a backslash. Especially a backslash followed by octal digits.
86 * We are not doing a correctness syntax check here.
89 print_quote(char q
, char* text
)
104 fputc( *(text
++), stdout
);
121 #endif /* NEED_PRINT_QUOTE */
125 * Emit the GNU standard type wrapped up in such a way that
126 * this thing can be encountered countless times during a compile
127 * and not cause even a warning.
130 emit_gnu_type (const char* text
, regmatch_t
* rm
)
135 fwrite (text
, rm
[0].rm_so
, 1, stdout
);
138 const char* ps
= text
+ rm
[1].rm_so
;
139 const char* pe
= text
+ rm
[1].rm_eo
;
144 *(pD
++) = TOUPPER( *(pd
++) = *(ps
++) );
150 * Now print out the reformed typedef,
151 * with a C++ guard for WCHAR
155 #if !defined(_GCC_%s_T)%s\n\
157 typedef __%s_TYPE__ %s_t;\n\
160 const char *const pz_guard
= (strcmp (z_type
, "wchar") == 0)
161 ? " && ! defined(__cplusplus)" : "";
163 printf (z_fmt
, z_TYPE
, pz_guard
, z_TYPE
, z_TYPE
, z_type
);
166 return text
+= rm
[0].rm_eo
;
171 * Copy the `format' string to std out, replacing `%n' expressions
172 * with the matched text from a regular expression evaluation.
173 * Doubled '%' characters will be replaced with a single copy.
174 * '%' characters in other contexts and all other characters are
175 * copied out verbatim.
178 format_write (tCC
* format
, tCC
* text
, regmatch_t av
[] )
182 while ((c
= (unsigned)*(format
++)) != NUL
) {
190 c
= (unsigned)*(format
++);
193 * IF the character following a '%' is not a digit,
194 * THEN we will always emit a '%' and we may or may
195 * not emit the following character. We will end on
196 * a NUL and we will emit only one of a pair of '%'.
212 * Emit the matched subexpression numbered 'c'.
213 * IF, of course, there was such a match...
216 regmatch_t
* pRM
= av
+ (c
- (unsigned)'0');
222 len
= pRM
->rm_eo
- pRM
->rm_so
;
224 fwrite(text
+ pRM
->rm_so
, len
, 1, stdout
);
231 * Search for multiple copies of a regular expression. Each block
232 * of matched text is replaced with the format string, as described
233 * above in `format_write'.
235 FIX_PROC_HEAD( format_fix
)
237 tCC
* pz_pat
= p_fixd
->patch_args
[2];
238 tCC
* pz_fmt
= p_fixd
->patch_args
[1];
244 * We must have a format
246 if (pz_fmt
== (tCC
*)NULL
)
248 fprintf( stderr
, zNeedsArg
, p_fixd
->fix_name
, "replacement format", 0 );
253 * IF we don't have a search text, then go find the first
254 * regular expression among the tests.
256 if (pz_pat
== (tCC
*)NULL
)
258 tTestDesc
* pTD
= p_fixd
->p_test_desc
;
259 int ct
= p_fixd
->test_ct
;
264 fprintf( stderr
, zNeedsArg
, p_fixd
->fix_name
, "search text", 1 );
268 if (pTD
->type
== TT_EGREP
)
270 pz_pat
= pTD
->pz_test_text
;
279 * Replace every copy of the text we find
281 compile_re (pz_pat
, &re
, 1, "format search-text", "format_fix" );
282 while (xregexec (&re
, text
, 10, rm
, 0) == 0)
284 fwrite( text
, rm
[0].rm_so
, 1, stdout
);
285 format_write( pz_fmt
, text
, rm
);
290 * Dump out the rest of the file
292 fputs (text
, stdout
);
296 /* Scan the input file for all occurrences of text like this:
298 #define TIOCCONS _IO(T, 12)
300 and change them to read like this:
302 #define TIOCCONS _IO('T', 12)
304 which is the required syntax per the C standard. (The definition of
305 _IO also has to be tweaked - see below.) 'IO' is actually whatever you
306 provide as the `c_fix_arg' argument. */
308 FIX_PROC_HEAD( char_macro_use_fix
)
310 /* This regexp looks for a traditional-syntax #define (# in column 1)
311 of an object-like macro. */
312 static const char pat
[] =
313 "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
316 const char* str
= p_fixd
->patch_args
[1];
318 const char *p
, *limit
;
324 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "ioctl type", 0);
329 compile_re (pat
, &re
, 1, "macro pattern", "char_macro_use_fix");
332 xregexec (&re
, p
, 1, rm
, 0) == 0;
335 /* p + rm[0].rm_eo is the first character of the macro replacement.
336 Find the end of the macro replacement, and the STR we were
337 sent to look for within the replacement. */
342 limit
= strchr (limit
+ 1, '\n');
346 while (limit
[-1] == '\\');
350 if (*p
== str
[0] && !strncmp (p
+1, str
+1, len
-1))
353 while (++p
< limit
- len
);
354 /* Hit end of line. */
358 /* Found STR on this line. If the macro needs fixing,
359 the next few chars will be whitespace or uppercase,
360 then an open paren, then a single letter. */
361 while ((ISSPACE (*p
) || ISUPPER (*p
)) && p
< limit
) p
++;
369 /* Splat all preceding text into the output buffer,
370 quote the character at p, then proceed. */
371 fwrite (text
, 1, p
- text
, stdout
);
378 fputs (text
, stdout
);
382 /* Scan the input file for all occurrences of text like this:
384 #define xxxIOxx(x, y) (....'x'<<16....)
386 and change them to read like this:
388 #define xxxIOxx(x, y) (....x<<16....)
390 which is the required syntax per the C standard. (The uses of _IO
391 also has to be tweaked - see above.) 'IO' is actually whatever
392 you provide as the `c_fix_arg' argument. */
393 FIX_PROC_HEAD( char_macro_def_fix
)
395 /* This regexp looks for any traditional-syntax #define (# in column 1). */
396 static const char pat
[] =
397 "^#[ \t]*define[ \t]+";
400 const char* str
= p_fixd
->patch_args
[1];
402 const char *p
, *limit
;
409 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "ioctl type", 0);
414 compile_re (pat
, &re
, 1, "macro pattern", "fix_char_macro_defines");
417 xregexec (&re
, p
, 1, rm
, 0) == 0;
420 /* p + rm[0].rm_eo is the first character of the macro name.
421 Find the end of the macro replacement, and the STR we were
422 sent to look for within the name. */
427 limit
= strchr (limit
+ 1, '\n');
431 while (limit
[-1] == '\\');
435 if (*p
== str
[0] && !strncmp (p
+1, str
+1, len
-1))
439 while (ISIDNUM (*p
));
440 /* Hit end of macro name without finding the string. */
444 /* Found STR in this macro name. If the macro needs fixing,
445 there may be a few uppercase letters, then there will be an
446 open paren with _no_ intervening whitespace, and then a
448 while (ISUPPER (*p
) && p
< limit
) p
++;
456 /* The character at P is the one to look for in the following
463 if (p
[-1] == '\'' && p
[0] == arg
&& p
[1] == '\'')
465 /* Remove the quotes from this use of ARG. */
467 fwrite (text
, 1, p
- text
, stdout
);
477 fputs (text
, stdout
);
480 /* Check if the pattern at pos is actually in a "__has_include(...)"
481 directive. Return the pointer to the ')' of this
482 "__has_include(...)" if it is, NULL otherwise. */
484 check_has_inc (const char *begin
, const char *pos
, const char *end
)
486 static const char has_inc
[] = "__has_include";
487 const size_t has_inc_len
= sizeof (has_inc
) - 1;
490 for (p
= memmem (begin
, pos
- begin
, has_inc
, has_inc_len
);
492 p
= memmem (p
, pos
- p
, has_inc
, has_inc_len
))
495 while (p
< end
&& ISSPACE (*p
))
498 /* "__has_include" may appear as "defined(__has_include)",
499 search for the next appearance then. */
503 /* To avoid too much complexity, just hope there is never a
504 ')' in a header name. */
505 p
= memchr (p
, ')', end
- p
);
506 if (p
== NULL
|| p
> pos
)
513 /* Fix for machine name #ifdefs that are not in the namespace reserved
514 by the C standard. They won't be defined if compiling with -ansi,
515 and the headers will break. We go to some trouble to only change
516 #ifdefs where the macro is defined by GCC in non-ansi mode; this
517 minimizes the number of headers touched. */
519 #define SCRATCHSZ 64 /* hopefully long enough */
521 FIX_PROC_HEAD( machine_name_fix
)
524 const char *line
, *base
, *limit
, *p
, *q
;
525 regex_t
*label_re
, *name_re
;
526 char scratch
[SCRATCHSZ
];
531 if (!mn_get_regexps (&label_re
, &name_re
, "machine_name_fix"))
533 fputs( "The target machine has no needed machine name fixes\n", stderr
);
541 xregexec (label_re
, base
, 2, match
, 0) == 0;
544 base
+= match
[0].rm_eo
;
545 /* We're looking at an #if or #ifdef. Scan forward for the
546 next non-escaped newline. */
551 limit
= strchr (limit
, '\n');
555 while (limit
[-1] == '\\');
557 /* If the 'name_pat' matches in between base and limit, we have
558 a bogon. It is not worth the hassle of excluding comments
559 because comments on #if/#ifdef lines are rare, and strings on
560 such lines are only legal in a "__has_include" directive.
562 REG_NOTBOL means 'base' is not at the beginning of a line, which
563 shouldn't matter since the name_re has no ^ anchor, but let's
564 be accurate anyway. */
572 if (xregexec (name_re
, base
, 1, match
, REG_NOTBOL
))
573 goto done
; /* No remaining match in this file */
575 /* Match; is it on the line? */
576 if (match
[0].rm_eo
> limit
- base
)
579 p
= base
+ match
[0].rm_so
;
581 /* Check if the match is in __has_include(...) (PR 91085). */
582 q
= check_has_inc (base
, p
, limit
);
589 base
+= match
[0].rm_eo
;
590 /* One more test: if on the same line we have the same string
591 with the appropriate underscores, then leave it alone.
592 We want exactly two leading and trailing underscores. */
595 len
= base
- p
- ((*base
== '_') ? 2 : 1);
600 len
= base
- p
- ((*base
== '_') ? 1 : 0);
603 if (len
+ 4 > SCRATCHSZ
)
605 memcpy (&scratch
[2], q
, len
);
607 scratch
[len
++] = '_';
608 scratch
[len
++] = '_';
610 for (q
= line
; q
<= limit
- len
; q
++)
611 if (*q
== '_' && !strncmp (q
, scratch
, len
))
614 fwrite (text
, 1, p
- text
, stdout
);
615 fwrite (scratch
, 1, len
, stdout
);
621 fputs (text
, stdout
);
625 FIX_PROC_HEAD( wrap_fix
)
627 tSCC z_no_wrap_pat
[] = "^#if.*__need_";
628 static regex_t no_wrapping_re
; /* assume zeroed data */
632 if (no_wrapping_re
.allocated
== 0)
633 compile_re( z_no_wrap_pat
, &no_wrapping_re
, 0, "no-wrap pattern",
637 * IF we do *not* match the no-wrap re, then we have a double negative.
638 * A double negative means YES.
640 if (xregexec( &no_wrapping_re
, text
, 0, NULL
, 0 ) != 0)
643 * A single file can get wrapped more than once by different fixes.
644 * A single fix can wrap multiple files. Therefore, guard with
645 * *both* the fix name and the file name.
647 size_t ln
= strlen( filname
) + strlen( p_fixd
->fix_name
) + 14;
648 char* pz
= XNEWVEC (char, ln
);
650 sprintf( pz
, "FIXINC_WRAP_%s-%s", filname
, p_fixd
->fix_name
);
652 for (pz
+= 12; 1; pz
++) {
658 if (! ISALNUM( ch
)) {
666 printf( "#ifndef %s\n", pz_name
);
667 printf( "#define %s 1\n\n", pz_name
);
670 if (p_fixd
->patch_args
[1] == (tCC
*)NULL
)
671 fputs( text
, stdout
);
674 fputs( p_fixd
->patch_args
[1], stdout
);
675 fputs( text
, stdout
);
676 if (p_fixd
->patch_args
[2] != (tCC
*)NULL
)
677 fputs( p_fixd
->patch_args
[2], stdout
);
680 if (pz_name
!= NULL
) {
681 printf( "\n#endif /* %s */\n", pz_name
);
682 free( (void*)pz_name
);
688 * Search for multiple copies of a regular expression. Each block
689 * of matched text is replaced with the format string, as described
690 * above in `format_write'.
692 FIX_PROC_HEAD( gnu_type_fix
)
696 regmatch_t rm
[GTYPE_SE_CT
+1];
700 tTestDesc
* pTD
= p_fixd
->p_test_desc
;
701 int ct
= p_fixd
->test_ct
;
706 fprintf (stderr
, zNeedsArg
, p_fixd
->fix_name
, "search text", 1);
710 if (pTD
->type
== TT_EGREP
)
712 pz_pat
= pTD
->pz_test_text
;
720 compile_re (pz_pat
, &re
, 1, "gnu type typedef", "gnu_type_fix");
722 while (xregexec (&re
, text
, GTYPE_SE_CT
+1, rm
, 0) == 0)
724 text
= emit_gnu_type (text
, rm
);
728 * Dump out the rest of the file
730 fputs (text
, stdout
);
734 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
736 test for fix selector
738 THIS IS THE ONLY EXPORTED ROUTINE
742 apply_fix( tFixDesc
* p_fixd
, tCC
* filname
)
744 #define _FT_(n,p) { n, p },
745 static fix_entry_t fix_table
[] = { FIXUP_TABLE
{ NULL
, NULL
}};
747 #define FIX_TABLE_CT (ARRAY_SIZE (fix_table)-1)
749 tCC
* fixname
= p_fixd
->patch_args
[0];
751 int ct
= FIX_TABLE_CT
;
752 fix_entry_t
* pfe
= fix_table
;
756 if (strcmp (pfe
->fix_name
, fixname
) == 0)
760 fprintf (stderr
, "fixincl error: the `%s' fix is unknown\n",
767 buf
= load_file_data (stdin
);
768 (*pfe
->fix_proc
)( filname
, buf
, p_fixd
);
771 #ifdef SEPARATE_FIX_PROC
773 "USAGE: applyfix <fix-name> <file-to-fix> <file-source> <file-destination>\n";
775 "FS error %d (%s) reopening %s as std%s\n";
778 main( int argc
, char** argv
)
790 fputs (z_usage
, stderr
);
800 if (! ISDIGIT ( *pz
))
803 idx
= strtol (pz
, &pz
, 10);
804 if ((*pz
!= NUL
) || ((unsigned)idx
>= FIX_COUNT
))
806 pFix
= fixDescList
+ idx
;
809 if (freopen (argv
[3], "r", stdin
) != stdin
)
811 fprintf (stderr
, z_reopen
, errno
, strerror( errno
), argv
[3], "in");
815 pz_tmptmp
= XNEWVEC (char, strlen (argv
[4]) + 5);
816 strcpy( pz_tmptmp
, argv
[4] );
819 /* Don't lose because "12345678" and "12345678X" map to the same
820 file under DOS restricted 8+3 file namespace. Note that DOS
821 doesn't allow more than one dot in the trunk of a file name. */
822 pz_tmp_base
= basename( pz_tmptmp
);
823 pz_tmp_dot
= strchr( pz_tmp_base
, '.' );
824 if (pathconf( pz_tmptmp
, _PC_NAME_MAX
) <= 12 /* is this DOS or Windows9X? */
825 && pz_tmp_dot
!= (char*)NULL
)
826 strcpy (pz_tmp_dot
+1, "X"); /* nuke the original extension */
828 #endif /* _PC_NAME_MAX */
829 strcat (pz_tmptmp
, ".X");
830 if (freopen (pz_tmptmp
, "w", stdout
) != stdout
)
832 fprintf (stderr
, z_reopen
, errno
, strerror( errno
), pz_tmptmp
, "out");
836 /* Second parameter of apply_fix is file name */
837 apply_fix (pFix
, argv
[2]);
841 if (rename (pz_tmptmp
, argv
[4]) != 0)
843 fprintf (stderr
, "error %d (%s) renaming %s to %s\n", errno
,
844 strerror( errno
), pz_tmptmp
, argv
[4]);