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