1250199Sgrehan
2250199Sgrehan/*
3250199Sgrehan
4250199Sgrehan   Test to see if a particular fix should be applied to a header file.
5250199Sgrehan
6250199Sgrehan   Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
7250199Sgrehan
8250199Sgrehan= = = = = = = = = = = = = = = = = = = = = = = = =
9250199Sgrehan
10250199SgrehanNOTE TO DEVELOPERS
11250199Sgrehan
12250199SgrehanThe routines you write here must work closely with fixincl.c.
13250199Sgrehan
14250199SgrehanHere are the rules:
15250199Sgrehan
16250199Sgrehan1.  Every test procedure name must be suffixed with "_test".
17250199Sgrehan    These routines will be referenced from inclhack.def, sans the suffix.
18250199Sgrehan
19250199Sgrehan2.  Use the "TEST_FOR_FIX_PROC_HEAD()" macro _with_ the "_test" suffix
20250199Sgrehan    (I cannot use the ## magic from ANSI C) for defining your entry point.
21250199Sgrehan
22250199Sgrehan3.  Put your test name into the FIX_TEST_TABLE
23250199Sgrehan
24250199Sgrehan4.  Do not write anything to stdout.  It may be closed.
25250199Sgrehan
26250199Sgrehan5.  Write to stderr only in the event of a reportable error
27250199Sgrehan    In such an event, call "exit(1)".
28250199Sgrehan
29250199Sgrehan= = = = = = = = = = = = = = = = = = = = = = = = =
30250199Sgrehan
31250199SgrehanThis file is part of GCC.
32250199Sgrehan
33250199SgrehanGCC is free software; you can redistribute it and/or modify
34250199Sgrehanit under the terms of the GNU General Public License as published by
35250199Sgrehanthe Free Software Foundation; either version 2, or (at your option)
36250199Sgrehanany later version.
37250199Sgrehan
38250199SgrehanGCC is distributed in the hope that it will be useful,
39250199Sgrehanbut WITHOUT ANY WARRANTY; without even the implied warranty of
40255414SgrehanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41250199SgrehanGNU General Public License for more details.
42250199Sgrehan
43255414SgrehanYou should have received a copy of the GNU General Public License
44250199Sgrehanalong with GCC; see the file COPYING.  If not, write to
45255414Sgrehanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
46255414SgrehanBoston, MA 02110-1301, USA.  */
47255414Sgrehan
48255414Sgrehan#include "fixlib.h"
49250199Sgrehan
50250199Sgrehan#define _ENV_(v,m,n,t)   extern tCC* v;
51250199SgrehanENV_TABLE
52250199Sgrehan#undef _ENV_
53250199Sgrehan
54250199Sgrehantypedef apply_fix_p_t t_test_proc ( tCC* file, tCC* text );
55250199Sgrehan
56250199Sgrehantypedef struct {
57250199Sgrehan    tCC*         test_name;
58250199Sgrehan    t_test_proc* test_proc;
59250199Sgrehan} test_entry_t;
60255414Sgrehan
61250199Sgrehan#define FIX_TEST_TABLE							\
62250199Sgrehan  _FT_( "machine_name",     machine_name_test )				\
63250199Sgrehan  _FT_( "stdc_0_in_system_headers",    stdc_0_in_system_headers_test )
64250199Sgrehan
65250199Sgrehan#define TEST_FOR_FIX_PROC_HEAD( test ) \
66250199Sgrehanstatic apply_fix_p_t test ( tCC* fname ATTRIBUTE_UNUSED, \
67250199Sgrehan                            tCC* text  ATTRIBUTE_UNUSED )
68250199Sgrehan
69250199SgrehanTEST_FOR_FIX_PROC_HEAD( machine_name_test )
70250199Sgrehan{
71250199Sgrehan  regex_t *label_re, *name_re;
72250199Sgrehan  regmatch_t match[2];
73250199Sgrehan  tCC *base, *limit;
74250199Sgrehan  IGNORE_ARG(fname);
75250199Sgrehan
76250199Sgrehan  if (!mn_get_regexps (&label_re, &name_re, "machine_name_test"))
77250199Sgrehan    return SKIP_FIX;
78250199Sgrehan
79250199Sgrehan  for (base = text;
80250199Sgrehan       xregexec (label_re, base, 2, match, 0) == 0;
81250199Sgrehan       base = limit)
82250199Sgrehan    {
83250199Sgrehan      base += match[0].rm_eo;
84250199Sgrehan      /* We're looking at an #if or #ifdef.  Scan forward for the
85250199Sgrehan	 next non-escaped newline.  */
86250199Sgrehan      limit = base;
87255414Sgrehan      do
88255414Sgrehan	{
89250199Sgrehan	  limit++;
90250199Sgrehan	  limit = strchr (limit, '\n');
91255414Sgrehan	  if (!limit)
92250199Sgrehan	    return SKIP_FIX;
93250199Sgrehan	}
94250199Sgrehan      while (limit[-1] == '\\');
95250199Sgrehan
96250199Sgrehan      /* If the 'name_pat' matches in between base and limit, we have
97255414Sgrehan	 a bogon.  It is not worth the hassle of excluding comments,
98250199Sgrehan	 because comments on #if/#ifdef/#ifndef lines are rare,
99250199Sgrehan	 and strings on such lines are illegal.
100255414Sgrehan
101255414Sgrehan	 REG_NOTBOL means 'base' is not at the beginning of a line, which
102250199Sgrehan	 shouldn't matter since the name_re has no ^ anchor, but let's
103255414Sgrehan	 be accurate anyway.  */
104250199Sgrehan
105250199Sgrehan      if (xregexec (name_re, base, 1, match, REG_NOTBOL))
106255414Sgrehan	return SKIP_FIX;  /* No match in file - no fix needed */
107250199Sgrehan
108250199Sgrehan      /* Match; is it on the line?  */
109250199Sgrehan      if (match[0].rm_eo <= limit - base)
110250199Sgrehan	return APPLY_FIX;  /* Yup */
111250199Sgrehan
112250199Sgrehan      /* Otherwise, keep looking... */
113250199Sgrehan    }
114250199Sgrehan  return SKIP_FIX;
115255414Sgrehan}
116250199Sgrehan
117250199Sgrehan
118250199SgrehanTEST_FOR_FIX_PROC_HEAD( stdc_0_in_system_headers_test )
119250199Sgrehan{
120250199Sgrehan#ifdef STDC_0_IN_SYSTEM_HEADERS
121250199Sgrehan  return (pz_machine == NULL) ? APPLY_FIX : SKIP_FIX;
122255414Sgrehan#else
123250199Sgrehan  return APPLY_FIX;
124250199Sgrehan#endif
125250199Sgrehan}
126250199Sgrehan
127250199Sgrehan
128250199Sgrehan/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
129250199Sgrehan
130250199Sgrehan     test for fix selector
131250199Sgrehan
132250199Sgrehan     THIS IS THE ONLY EXPORTED ROUTINE
133250199Sgrehan
134250199Sgrehan*/
135250199Sgrehanapply_fix_p_t
136250199Sgrehanrun_test( tCC* tname, tCC* fname, tCC* text )
137250199Sgrehan{
138250199Sgrehan#define _FT_(n,p) { n, p },
139250199Sgrehan  static test_entry_t test_table[] = { FIX_TEST_TABLE { NULL, NULL }};
140250199Sgrehan#undef _FT_
141250199Sgrehan#define TEST_TABLE_CT (ARRAY_SIZE (test_table)-1)
142250199Sgrehan
143250199Sgrehan  int ct = TEST_TABLE_CT;
144250199Sgrehan  test_entry_t* pte = test_table;
145250199Sgrehan
146255414Sgrehan  do
147255414Sgrehan    {
148255414Sgrehan      if (strcmp( pte->test_name, tname ) == 0)
149255414Sgrehan        return (*pte->test_proc)( fname, text );
150255414Sgrehan      pte++;
151255414Sgrehan    } while (--ct > 0);
152250199Sgrehan  fprintf( stderr, "fixincludes error:  the `%s' fix test is unknown\n",
153250199Sgrehan           tname );
154255414Sgrehan  exit( 3 );
155255414Sgrehan}
156250199Sgrehan