OSDN Git Service

Obsolete test result
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / fixfixes.c
1
2 /*
3
4    Test to see if a particular fix should be applied to a header file.
5
6    Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
7
8 = = = = = = = = = = = = = = = = = = = = = = = = =
9
10 NOTE TO DEVELOPERS
11
12 The routines you write here must work closely with fixincl.c.
13
14 Here are the rules:
15
16 1.  Every test procedure name must be suffixed with "_fix".
17     These routines will be referenced from inclhack.def, sans the suffix.
18
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.
21
22 3.  Put your test name into the FIXUP_TABLE.
23
24 4.  Do not read anything from stdin.  It is closed.
25
26 5.  Write to stderr only in the event of a reportable error
27     In such an event, call "exit (EXIT_FAILURE)".
28
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.
32
33     It is also possible to access fix descriptions by using the
34     index of a known fix, "my_fix_name" for example:
35
36         tFixDesc*  p_desc  = fixDescList + MY_FIX_NAME_FIXIDX;
37         tTestDesc* p_tlist = p_desc->p_test_desc;
38
39         regexec (p_tlist->p_test_regex, ...)
40
41 = = = = = = = = = = = = = = = = = = = = = = = = =
42
43 This file is part of GNU CC.
44
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)
48 any later version.
49
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.
54
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.  */
59
60 #include "fixlib.h"
61 #define    GTYPE_SE_CT 1
62
63 tSCC zNeedsArg[] = "fixincl error:  `%s' needs %s argument (c_fix_arg[%d])\n";
64
65 typedef struct {
66     const char*  fix_name;
67     void (*fix_proc)();
68 } fix_entry_t;
69
70 #define FIXUP_TABLE \
71   _FT_( "char_macro_def",   char_macro_def_fix ) \
72   _FT_( "char_macro_use",   char_macro_use_fix ) \
73   _FT_( "format",           format_fix )         \
74   _FT_( "machine_name",     machine_name_fix )   \
75   _FT_( "wrap",             wrap_fix )           \
76   _FT_( "gnu_type",         gnu_type_fix )
77
78
79 #define FIX_PROC_HEAD( fix ) \
80 static void fix ( filname, text, p_fixd ) \
81     const char* filname; \
82     const char* text; \
83     tFixDesc* p_fixd;
84
85
86 /*
87  *  Skip over a quoted string.  Single quote strings may
88  *  contain multiple characters if the first character is
89  *  a backslash.  Especially a backslash followed by octal digits.
90  *  We are not doing a correctness syntax check here.
91  */
92 static char*
93 print_quote( q, text )
94   char  q;
95   char* text;
96 {
97   fputc( q, stdout );
98
99   for (;;)
100     {
101       char ch = *(text++);
102       fputc( ch, stdout );
103
104       switch (ch)
105         {
106         case '\\':
107           if (*text == NUL)
108             goto quote_done;
109
110           fputc( *(text++), stdout );
111           break;
112
113         case '"':
114         case '\'':
115           if (ch != q)
116             break;
117           /*FALLTHROUGH*/
118
119         case '\n':
120         case NUL:
121           goto quote_done;
122         }
123     } quote_done:;
124
125   return text;
126 }
127
128
129 /*
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.
133  */
134 static const char*
135 emit_gnu_type ( text, rm )
136   const char* text;
137   regmatch_t* rm;
138 {
139   extern t_gnu_type_map gnu_type_map[];
140   extern int gnu_type_map_ct;
141
142   const char*     pzt  = text + rm[GTYPE_SE_CT].rm_so;
143   t_gnu_type_map* p_tm = gnu_type_map;
144   int   ct = gnu_type_map_ct;
145
146   fwrite (text, rm[0].rm_so, 1, stdout);
147   text += rm[0].rm_eo;
148
149   for (;;)
150     {
151       if (strncmp (pzt, p_tm->pz_type, p_tm->type_name_len) == 0)
152         break;
153
154 #ifdef DEBUG
155       if (--ct <= 0)
156         return (const char*)NULL;
157 #else
158       if (--ct <= 0)
159         return text;
160 #endif
161       p_tm++;
162     }
163
164   /*
165    *  Now print out the reformed typedef
166    */
167   printf ("#ifndef __%s_TYPE__\n"
168           "#define __%s_TYPE__ %s\n"
169           "#endif\n",
170           p_tm->pz_TYPE, p_tm->pz_TYPE, p_tm->pz_gtype );
171
172   printf ("#if !defined(_GCC_%s_T)%s\n"
173           "#define _GCC_%s_T\n"
174           "typedef __%s_TYPE__ %s_t;\n"
175           "#endif\n",
176           p_tm->pz_TYPE, p_tm->pz_cxx_guard,
177           p_tm->pz_TYPE, p_tm->pz_TYPE, p_tm->pz_type);
178
179   return text;
180 }
181
182
183 /*
184  *  Copy the `format' string to std out, replacing `%n' expressions
185  *  with the matched text from a regular expression evaluation.
186  *  Doubled '%' characters will be replaced with a single copy.
187  *  '%' characters in other contexts and all other characters are
188  *  copied out verbatim.
189  */
190 static void
191 format_write (format, text, av)
192      tCC* format;
193      tCC* text;
194      regmatch_t av[];
195 {
196   int c;
197
198   while ((c = (unsigned)*(format++)) != NUL) {
199
200     if (c != '%')
201       {
202         putchar(c);
203         continue;
204       }
205
206     c = (unsigned)*(format++);
207
208     /*
209      *  IF the character following a '%' is not a digit,
210      *  THEN we will always emit a '%' and we may or may
211      *  not emit the following character.  We will end on
212      *  a NUL and we will emit only one of a pair of '%'.
213      */
214     if (! isdigit( c ))
215       {
216         putchar( '%' );
217         switch (c) {
218         case NUL:
219           return;
220         case '%':
221           break;
222         default:
223           putchar(c);
224         }
225       }
226
227     /*
228      *  Emit the matched subexpression numbered 'c'.
229      *  IF, of course, there was such a match...
230      */
231     else {
232       regmatch_t*  pRM = av + (c - (unsigned)'0');
233       size_t len;
234
235       if (pRM->rm_so < 0)
236         continue;
237
238       len = pRM->rm_eo - pRM->rm_so;
239       if (len > 0)
240         fwrite(text + pRM->rm_so, len, 1, stdout);
241     }
242   }
243 }
244
245
246 /*
247  *  Search for multiple copies of a regular expression.  Each block
248  *  of matched text is replaced with the format string, as described
249  *  above in `format_write'.
250  */
251 FIX_PROC_HEAD( format_fix )
252 {
253   tCC*  pz_pat = p_fixd->patch_args[2];
254   tCC*  pz_fmt = p_fixd->patch_args[1];
255   const char *p;
256   regex_t re;
257   regmatch_t rm[10];
258
259   /*
260    *  We must have a format
261    */
262   if (pz_fmt == (tCC*)NULL)
263     {
264       fprintf( stderr, zNeedsArg, p_fixd->fix_name, "replacement format", 0 );
265       exit (EXIT_BROKEN);
266     }
267
268   /*
269    *  IF we don't have a search text, then go find the first
270    *  regular expression among the tests.
271    */
272   if (pz_pat == (tCC*)NULL)
273     {
274       tTestDesc* pTD = p_fixd->p_test_desc;
275       int        ct  = p_fixd->test_ct;
276       for (;;)
277         {
278           if (ct-- <= 0)
279             {
280               fprintf( stderr, zNeedsArg, p_fixd->fix_name, "search text", 1 );
281               exit (EXIT_BROKEN);
282             }
283
284           if (pTD->type == TT_EGREP)
285             {
286               pz_pat = pTD->pz_test_text;
287               break;
288             }
289
290           pTD++;
291         }
292     }
293
294   /*
295    *  Replace every copy of the text we find
296    */
297   compile_re (pz_pat, &re, 1, "format search-text", "format_fix" );
298   while (regexec (&re, text, 10, rm, 0) == 0)
299     {
300       char* apz[10];
301       int   i;
302
303       fwrite( text, rm[0].rm_so, 1, stdout );
304       format_write( pz_fmt, text, rm );
305       text += rm[0].rm_eo;
306     }
307
308   /*
309    *  Dump out the rest of the file
310    */
311   fputs (text, stdout);
312 }
313
314
315 /* Scan the input file for all occurrences of text like this:
316
317    #define TIOCCONS _IO(T, 12)
318
319    and change them to read like this:
320
321    #define TIOCCONS _IO('T', 12)
322
323    which is the required syntax per the C standard.  (The definition of
324    _IO also has to be tweaked - see below.)  'IO' is actually whatever you
325    provide as the `c_fix_arg' argument.  */
326
327 FIX_PROC_HEAD( char_macro_use_fix )
328 {
329   /* This regexp looks for a traditional-syntax #define (# in column 1)
330      of an object-like macro.  */
331   static const char pat[] =
332     "^#[ \t]*define[ \t]+[_A-Za-z][_A-Za-z0-9]*[ \t]+";
333   static regex_t re;
334
335   const char* str = p_fixd->patch_args[1];
336   regmatch_t rm[1];
337   const char *p, *limit;
338   size_t len;
339
340   if (str == NULL)
341     {
342       fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
343       exit (EXIT_BROKEN);
344     }
345
346   len = strlen (str);
347   compile_re (pat, &re, 1, "macro pattern", "char_macro_use_fix");
348
349   for (p = text;
350        regexec (&re, p, 1, rm, 0) == 0;
351        p = limit + 1)
352     {
353       /* p + rm[0].rm_eo is the first character of the macro replacement.
354          Find the end of the macro replacement, and the STR we were
355          sent to look for within the replacement.  */
356       p += rm[0].rm_eo;
357       limit = p - 1;
358       do
359         {
360           limit = strchr (limit + 1, '\n');
361           if (!limit)
362             goto done;
363         }
364       while (limit[-1] == '\\');
365
366       do
367         {
368           if (*p == str[0] && !strncmp (p+1, str+1, len-1))
369             goto found;
370         }
371       while (++p < limit - len);
372       /* Hit end of line.  */
373       continue;
374
375     found:
376       /* Found STR on this line.  If the macro needs fixing,
377          the next few chars will be whitespace or uppercase,
378          then an open paren, then a single letter.  */
379       while ((isspace (*p) || isupper (*p)) && p < limit) p++;
380       if (*p++ != '(')
381         continue;
382       if (!isalpha (*p))
383         continue;
384       if (isalnum (p[1]) || p[1] == '_')
385         continue;
386
387       /* Splat all preceding text into the output buffer,
388          quote the character at p, then proceed.  */
389       fwrite (text, 1, p - text, stdout);
390       putchar ('\'');
391       putchar (*p);
392       putchar ('\'');
393       text = p + 1;
394     }
395  done:
396   fputs (text, stdout);
397 }
398
399
400 /* Scan the input file for all occurrences of text like this:
401
402    #define xxxIOxx(x, y) (....'x'<<16....)
403
404    and change them to read like this:
405
406    #define xxxIOxx(x, y) (....x<<16....)
407
408    which is the required syntax per the C standard.  (The uses of _IO
409    also has to be tweaked - see above.)  'IO' is actually whatever
410    you provide as the `c_fix_arg' argument.  */
411 FIX_PROC_HEAD( char_macro_def_fix )
412 {
413   /* This regexp looks for any traditional-syntax #define (# in column 1).  */
414   static const char pat[] =
415     "^#[ \t]*define[ \t]+";
416   static regex_t re;
417
418   const char* str = p_fixd->patch_args[1];
419   regmatch_t rm[1];
420   const char *p, *limit;
421   char arg;
422   size_t len;
423
424   if (str == NULL)
425     {
426       fprintf (stderr, zNeedsArg, p_fixd->fix_name, "ioctl type", 0);
427       exit (EXIT_BROKEN);
428     }
429
430   len = strlen (str);
431   compile_re (pat, &re, 1, "macro pattern", "fix_char_macro_defines");
432
433   for (p = text;
434        regexec (&re, p, 1, rm, 0) == 0;
435        p = limit + 1)
436     {
437       /* p + rm[0].rm_eo is the first character of the macro name.
438          Find the end of the macro replacement, and the STR we were
439          sent to look for within the name.  */
440       p += rm[0].rm_eo;
441       limit = p - 1;
442       do
443         {
444           limit = strchr (limit + 1, '\n');
445           if (!limit)
446             goto done;
447         }
448       while (limit[-1] == '\\');
449
450       do
451         {
452           if (*p == str[0] && !strncmp (p+1, str+1, len-1))
453             goto found;
454           p++;
455         }
456       while (isalpha (*p) || isalnum (*p) || *p == '_');
457       /* Hit end of macro name without finding the string.  */
458       continue;
459
460     found:
461       /* Found STR in this macro name.  If the macro needs fixing,
462          there may be a few uppercase letters, then there will be an
463          open paren with _no_ intervening whitespace, and then a
464          single letter.  */
465       while (isupper (*p) && p < limit) p++;
466       if (*p++ != '(')
467         continue;
468       if (!isalpha (*p))
469         continue;
470       if (isalnum (p[1]) || p[1] == '_')
471         continue;
472
473       /* The character at P is the one to look for in the following
474          text.  */
475       arg = *p;
476       p += 2;
477
478       while (p < limit)
479         {
480           if (p[-1] == '\'' && p[0] == arg && p[1] == '\'')
481             {
482               /* Remove the quotes from this use of ARG.  */
483               p--;
484               fwrite (text, 1, p - text, stdout);
485               putchar (arg);
486               p += 3;
487               text = p;
488             }
489           else
490             p++;
491         }
492     }
493  done:
494   fputs (text, stdout);
495 }
496
497 /* Fix for machine name #ifdefs that are not in the namespace reserved
498    by the C standard.  They won't be defined if compiling with -ansi,
499    and the headers will break.  We go to some trouble to only change
500    #ifdefs where the macro is defined by GCC in non-ansi mode; this
501    minimizes the number of headers touched.  */
502
503 #define SCRATCHSZ 64   /* hopefully long enough */
504
505 FIX_PROC_HEAD( machine_name_fix )
506 {
507 #ifndef MN_NAME_PAT
508   fputs( "The target machine has no needed machine name fixes\n", stderr );
509 #else
510   regmatch_t match[2];
511   const char *line, *base, *limit, *p, *q;
512   regex_t *label_re, *name_re;
513   char scratch[SCRATCHSZ];
514   size_t len;
515
516   mn_get_regexps (&label_re, &name_re, "machine_name_fix");
517
518   scratch[0] = '_';
519   scratch[1] = '_';
520
521   for (base = text;
522        regexec (label_re, base, 2, match, 0) == 0;
523        base = limit)
524     {
525       base += match[0].rm_eo;
526       /* We're looking at an #if or #ifdef.  Scan forward for the
527          next non-escaped newline.  */
528       line = limit = base;
529       do
530         {
531           limit++;
532           limit = strchr (limit, '\n');
533           if (!limit)
534             goto done;
535         }
536       while (limit[-1] == '\\');
537
538       /* If the 'name_pat' matches in between base and limit, we have
539          a bogon.  It is not worth the hassle of excluding comments
540          because comments on #if/#ifdef lines are rare, and strings on
541          such lines are illegal.
542
543          REG_NOTBOL means 'base' is not at the beginning of a line, which
544          shouldn't matter since the name_re has no ^ anchor, but let's
545          be accurate anyway.  */
546
547       for (;;)
548         {
549         again:
550           if (base == limit)
551             break;
552
553           if (regexec (name_re, base, 1, match, REG_NOTBOL))
554             goto done;  /* No remaining match in this file */
555
556           /* Match; is it on the line?  */
557           if (match[0].rm_eo > limit - base)
558             break;
559
560           p = base + match[0].rm_so;
561           base += match[0].rm_eo;
562
563           /* One more test: if on the same line we have the same string
564              with the appropriate underscores, then leave it alone.
565              We want exactly two leading and trailing underscores.  */
566           if (*p == '_')
567             {
568               len = base - p - ((*base == '_') ? 2 : 1);
569               q = p + 1;
570             }
571           else
572             {
573               len = base - p - ((*base == '_') ? 1 : 0);
574               q = p;
575             }
576           if (len + 4 > SCRATCHSZ)
577             abort ();
578           memcpy (&scratch[2], q, len);
579           len += 2;
580           scratch[len++] = '_';
581           scratch[len++] = '_';
582
583           for (q = line; q <= limit - len; q++)
584             if (*q == '_' && !strncmp (q, scratch, len))
585               goto again;
586           
587           fwrite (text, 1, p - text, stdout);
588           fwrite (scratch, 1, len, stdout);
589
590           text = base;
591         }
592     }
593  done:
594 #endif
595   fputs (text, stdout);
596 }
597
598
599 FIX_PROC_HEAD( wrap_fix )
600 {
601   char   z_fixname[ 64 ];
602   tCC*   pz_src  = p_fixd->fix_name;
603   tCC*   pz_name = z_fixname;
604   char*  pz_dst  = z_fixname;
605   size_t len     = 0;
606
607   for (;;) {
608     char ch = *(pz_src++);
609
610     if (islower(ch))
611       *(pz_dst++) = toupper( ch );
612
613     else if (isalnum( ch ))
614       *(pz_dst++) = ch;
615
616     else if (ch == NUL) {
617       *(pz_dst++) = ch;
618       break;
619     }
620     else
621       *(pz_dst++) = '_';
622
623     if (++len >= sizeof( z_fixname )) {
624       void* p = xmalloc( len + strlen( pz_src ) + 1 );
625       memcpy( p, (void*)z_fixname, len );
626       pz_name = (tCC*)p;
627       pz_dst  = (char*)pz_name + len;
628     }
629   }
630
631   printf( "#ifndef FIXINC_%s_CHECK\n", pz_name );
632   printf( "#define FIXINC_%s_CHECK 1\n\n", pz_name );
633
634   if (p_fixd->patch_args[1] == (tCC*)NULL)
635     fputs( text, stdout );
636
637   else {
638     fputs( p_fixd->patch_args[1], stdout );
639     fputs( text, stdout );
640     if (p_fixd->patch_args[2] != (tCC*)NULL)
641       fputs( p_fixd->patch_args[2], stdout );
642   }
643
644   printf( "\n#endif  /* FIXINC_%s_CHECK */\n", pz_name );
645   if (pz_name != z_fixname)
646     free( (void*)pz_name );
647 }
648
649
650 /*
651  *  Search for multiple copies of a regular expression.  Each block
652  *  of matched text is replaced with the format string, as described
653  *  above in `format_write'.
654  */
655 FIX_PROC_HEAD( gnu_type_fix )
656 {
657   const char* pz_pat;
658   regex_t    re;
659   regmatch_t rm[GTYPE_SE_CT+1];
660
661   {
662     tTestDesc* pTD = p_fixd->p_test_desc;
663     int        ct  = p_fixd->test_ct;
664     for (;;)
665       {
666         if (ct-- <= 0)
667           {
668             fprintf (stderr, zNeedsArg, p_fixd->fix_name, "search text", 1);
669             exit (EXIT_BROKEN);
670           }
671
672         if (pTD->type == TT_EGREP)
673           {
674             pz_pat = pTD->pz_test_text;
675             break;
676           }
677
678         pTD++;
679       }
680   }
681
682   compile_re (pz_pat, &re, 1, "gnu type typedef", "gnu_type_fix");
683
684   while (regexec (&re, text, GTYPE_SE_CT+1, rm, 0) == 0)
685     {
686 #ifndef DEBUG
687       text = emit_gnu_type (text, rm);
688 #else
689       tSCC z_mismatch[] = "``%s'' mismatched:\n";
690
691       /*
692        *  Make sure we matched *all* subexpressions
693        */
694       if (rm[GTYPE_SE_CT].rm_so == -1)
695         {
696           int i;
697
698           fprintf (stderr, z_mismatch, pz_pat);
699
700           for (i=0; i <= GTYPE_SE_CT; i++)
701             {
702               if (rm[i].rm_so != -1)
703                 {
704                   fprintf( stderr, "%4d:  ``", i );
705                   fwrite( text + rm[i].rm_so, rm[i].rm_eo - rm[i].rm_so,
706                           1, stderr );
707                   fputs( "''\n", stderr );
708                 }
709               else
710                 {
711                   fprintf( stderr, "%4d:  BROKEN\n", i );
712                 }
713             }
714           exit (EXIT_BROKEN);
715         }
716
717       text = emit_gnu_type (text, rm);
718       if (text == NULL)
719         {
720           fprintf (stderr, z_mismatch, pz_pat);
721           exit (EXIT_BROKEN);
722         }
723 #endif
724     }
725
726   /*
727    *  Dump out the rest of the file
728    */
729   fputs (text, stdout);
730 }
731
732
733 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
734
735      test for fix selector
736
737      THIS IS THE ONLY EXPORTED ROUTINE
738
739 */
740 void
741 apply_fix( p_fixd, filname )
742   tFixDesc* p_fixd;
743   tCC* filname;
744 {
745 #define _FT_(n,p) { n, p },
746   static fix_entry_t fix_table[] = { FIXUP_TABLE { NULL, NULL }};
747 #undef _FT_
748 #define FIX_TABLE_CT ((sizeof(fix_table)/sizeof(fix_table[0]))-1)
749
750   tCC* fixname = p_fixd->patch_args[0];
751   char* buf;
752   int ct = FIX_TABLE_CT;
753   fix_entry_t* pfe = fix_table;
754
755   for (;;)
756     {
757       if (strcmp (pfe->fix_name, fixname) == 0)
758         break;
759       if (--ct <= 0)
760         {
761           fprintf (stderr, "fixincl error:  the `%s' fix is unknown\n",
762                    fixname );
763           exit (EXIT_BROKEN);
764         }
765       pfe++;
766     }
767
768   buf = load_file_data (stdin);
769   (*pfe->fix_proc)( filname, buf, p_fixd );
770 }