OSDN Git Service

4e45dc1dd54afc52185d8fd4c10f3fe1ed279f10
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / fixtests.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 "_test".
17     These routines will be referenced from inclhack.def, sans the suffix.
18
19 2.  Use the "TEST_FOR_FIX_PROC_HEAD()" macro _with_ the "_test" suffix
20     (I cannot use the ## magic from ANSI C) for defining your entry point.
21
22 3.  Put your test name into the FIX_TEST_TABLE
23
24 4.  Do not write anything to stdout.  It may be closed.
25
26 5.  Write to stderr only in the event of a reportable error
27     In such an event, call "exit(1)".
28
29 = = = = = = = = = = = = = = = = = = = = = = = = =
30
31 This file is part of GNU CC.
32
33 GNU CC is free software; you can redistribute it and/or modify
34 it under the terms of the GNU General Public License as published by
35 the Free Software Foundation; either version 2, or (at your option)
36 any later version.
37
38 GNU CC is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41 GNU General Public License for more details.
42
43 You should have received a copy of the GNU General Public License
44 along with GNU CC; see the file COPYING.  If not, write to
45 the Free Software Foundation, 59 Temple Place - Suite 330,
46 Boston, MA 02111-1307, USA.  */
47
48 #include "fixlib.h"
49
50 typedef apply_fix_p_t t_test_proc PARAMS(( tCC* file, tCC* text ));
51
52 typedef struct {
53     tCC*         test_name;
54     t_test_proc* test_proc;
55 } test_entry_t;
56
57 #define FIX_TEST_TABLE \
58   _FT_( "machine_name",     machine_name_test )
59
60 #define TEST_FOR_FIX_PROC_HEAD( test )          \
61 apply_fix_p_t test PARAMS(( tCC* file, tCC* text ));  /* avoid warning */          \
62 static apply_fix_p_t test ( fname, text )       \
63     tCC* fname;                                 \
64     tCC* text;
65
66
67 TEST_FOR_FIX_PROC_HEAD( machine_name_test )
68 {
69 #ifndef MN_NAME_PAT
70   return SKIP_FIX;
71 #else
72   regex_t *label_re, *name_re;
73   regmatch_t match[2];
74   tCC *base, *limit;
75
76   mn_get_regexps(&label_re, &name_re, "machine_name_test");
77
78   for (base = text;
79        regexec (label_re, base, 2, match, 0) == 0;
80        base = limit)
81     {
82       base += match[0].rm_eo;
83       /* We're looking at an #if or #ifdef.  Scan forward for the
84          next non-escaped newline.  */
85       limit = base;
86       do
87         {
88           limit++;
89           limit = strchr (limit, '\n');
90           if (!limit)
91             return SKIP_FIX;
92         }
93       while (limit[-1] == '\\');
94
95       /* If the 'name_pat' matches in between base and limit, we have
96          a bogon.  It is not worth the hassle of excluding comments,
97          because comments on #if/#ifdef/#ifndef lines are rare,
98          and strings on such lines are illegal.
99
100          REG_NOTBOL means 'base' is not at the beginning of a line, which
101          shouldn't matter since the name_re has no ^ anchor, but let's
102          be accurate anyway.  */
103
104       if (regexec (name_re, base, 1, match, REG_NOTBOL))
105         return SKIP_FIX;  /* No match in file - no fix needed */
106
107       /* Match; is it on the line?  */
108       if (match[0].rm_eo <= limit - base)
109         return APPLY_FIX;  /* Yup */
110
111       /* Otherwise, keep looking... */
112     }
113   return SKIP_FIX;
114 #endif
115 }
116
117
118 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
119
120      test for fix selector
121
122      THIS IS THE ONLY EXPORTED ROUTINE
123
124 */
125 apply_fix_p_t
126 run_test( tname, fname, text )
127   tCC* tname;
128   tCC* fname;
129   tCC* text;
130 {
131 #define _FT_(n,p) { n, p },
132   static test_entry_t test_table[] = { FIX_TEST_TABLE { NULL, NULL }};
133 #undef _FT_
134 #define TEST_TABLE_CT (ARRAY_SIZE (test_table)-1)
135
136   int ct = TEST_TABLE_CT;
137   test_entry_t* pte = test_table;
138
139   do
140     {
141       if (strcmp( pte->test_name, tname ) == 0)
142         return (*pte->test_proc)( fname, text );
143       pte++;
144     } while (--ct > 0);
145   fprintf( stderr, "fixincludes error:  the `%s' fix test is unknown\n",
146            tname );
147   exit( 3 );
148 }