annotate macro.c @ 200:18b872f36217 release-0.5.2

Release 0.5.2
author David A. Holland
date Sun, 04 Sep 2016 17:16:39 -0400
parents 1d2bad7151f9
children 3a25180d3a5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
1 /*-
99
60184aa42604 add 2013 to copyrights where it seems warranted
David A. Holland
parents: 88
diff changeset
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
30
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
3 * All rights reserved.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
4 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
6 * by David A. Holland.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
7 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
8 * Redistribution and use in source and binary forms, with or without
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
9 * modification, are permitted provided that the following conditions
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
10 * are met:
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
12 * notice, this list of conditions and the following disclaimer.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
15 * documentation and/or other materials provided with the distribution.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
16 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
28 */
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
29
33
a5acb7049e4c need stdint.h for uint16_t
David A. Holland
parents: 30
diff changeset
30 #include <stdint.h>
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
31 #include <stdio.h>
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
32 #include <stdlib.h>
76da41da923f added macro table
David A. Holland
parents:
diff changeset
33 #include <string.h>
76da41da923f added macro table
David A. Holland
parents:
diff changeset
34
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
35 #include "union.h"
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
36 #include "array.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
37 #include "mode.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
38 #include "place.h"
76da41da923f added macro table
David A. Holland
parents:
diff changeset
39 #include "macro.h"
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
40 #include "output.h"
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
41
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
42 struct expansionitem {
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
43 enum { EI_STRING, EI_PARAM, EI_FILE, EI_LINE } itemtype;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
44 union {
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
45 char *ei_string; /* for EI_STRING */
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
46 unsigned ei_param; /* for EI_PARAM */
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
47 } UN;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
48 };
107
33954a07d013 __unused -> UNUSED
David A. Holland
parents: 99
diff changeset
49 DECLARRAY(expansionitem, static UNUSED);
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
50 DEFARRAY(expansionitem, static);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
51
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
52 #ifdef NEED_UNION_ACCESSORS
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
53 #define ei_string un.ei_string
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
54 #define ei_param un.ei_param
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
55 #endif
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
56
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
57
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
58 struct macro {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
59 struct place defplace;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
60 struct place expansionplace;
171
5922e6ca6b80 Revert gratuitous reordering of struct macro contents.
David A. Holland
parents: 170
diff changeset
61 unsigned hash;
5922e6ca6b80 Revert gratuitous reordering of struct macro contents.
David A. Holland
parents: 170
diff changeset
62 char *name;
5922e6ca6b80 Revert gratuitous reordering of struct macro contents.
David A. Holland
parents: 170
diff changeset
63 bool hasparams;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
64 struct stringarray params;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
65 struct expansionitemarray expansion;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
66 bool inuse;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
67 };
107
33954a07d013 __unused -> UNUSED
David A. Holland
parents: 99
diff changeset
68 DECLARRAY(macro, static UNUSED);
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
69 DEFARRAY(macro, static);
107
33954a07d013 __unused -> UNUSED
David A. Holland
parents: 99
diff changeset
70 DECLARRAY(macroarray, static UNUSED);
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 42
diff changeset
71 DEFARRAY(macroarray, static);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
72
76da41da923f added macro table
David A. Holland
parents:
diff changeset
73 static struct macroarrayarray macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
74 static unsigned total_macros;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
75 static unsigned hashmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
76
76da41da923f added macro table
David A. Holland
parents:
diff changeset
77 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
78 // macro structure ops
76da41da923f added macro table
David A. Holland
parents:
diff changeset
79
76da41da923f added macro table
David A. Holland
parents:
diff changeset
80 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
81 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
82 expansionitem_create_string(const char *string)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
83 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
84 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
85
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
86 ei = domalloc(sizeof(*ei));
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
87 ei->itemtype = EI_STRING;
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
88 ei->ei_string = dostrdup(string);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
89 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
90 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
91
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
92 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
93 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
94 expansionitem_create_stringlen(const char *string, size_t len)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
95 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
96 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
97
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
98 ei = domalloc(sizeof(*ei));
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
99 ei->itemtype = EI_STRING;
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
100 ei->ei_string = dostrndup(string, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
101 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
102 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
103
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
104 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
105 struct expansionitem *
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
106 expansionitem_create_param(unsigned param)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
107 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
108 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
109
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
110 ei = domalloc(sizeof(*ei));
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
111 ei->itemtype = EI_PARAM;
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
112 ei->ei_param = param;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
113 return ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
114 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
115
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
116 static
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
117 struct expansionitem *
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
118 expansionitem_create_file(void)
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
119 {
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
120 struct expansionitem *ei;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
121
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
122 ei = domalloc(sizeof(*ei));
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
123 ei->itemtype = EI_FILE;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
124 return ei;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
125 }
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
126
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
127 static
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
128 struct expansionitem *
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
129 expansionitem_create_line(void)
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
130 {
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
131 struct expansionitem *ei;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
132
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
133 ei = domalloc(sizeof(*ei));
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
134 ei->itemtype = EI_LINE;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
135 return ei;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
136 }
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
137
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
138 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
139 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
140 expansionitem_destroy(struct expansionitem *ei)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
141 {
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
142 switch (ei->itemtype) {
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
143 case EI_STRING:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
144 dostrfree(ei->ei_string);
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
145 break;
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
146 case EI_PARAM:
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
147 case EI_FILE:
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
148 case EI_LINE:
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
149 break;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
150 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
151 dofree(ei, sizeof(*ei));
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
152 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
153
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
154 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
155 bool
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
156 expansionitem_eq(const struct expansionitem *ei1,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
157 const struct expansionitem *ei2)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
158 {
170
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
159 if (ei1->itemtype != ei2->itemtype) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
160 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
161 }
170
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
162 switch (ei1->itemtype) {
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
163 case EI_STRING:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
164 if (strcmp(ei1->ei_string, ei2->ei_string) != 0) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
165 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
166 }
170
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
167 break;
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
168 case EI_PARAM:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
169 if (ei1->ei_param != ei2->ei_param) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
170 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
171 }
170
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
172 break;
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
173 case EI_FILE:
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
174 case EI_LINE:
2ee7db420643 Fix joerg's expansionitem_eq(). Was wrong previously.
David A. Holland
parents: 169
diff changeset
175 break;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
176 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
177 return true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
178 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
179
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
180 static
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
181 struct macro *
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
182 macro_create(struct place *p1, const char *name, unsigned hash,
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
183 struct place *p2)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
184 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
185 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
186
76da41da923f added macro table
David A. Holland
parents:
diff changeset
187 m = domalloc(sizeof(*m));
76da41da923f added macro table
David A. Holland
parents:
diff changeset
188 m->defplace = *p1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
189 m->expansionplace = *p2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
190 m->hash = hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
191 m->name = dostrdup(name);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
192 m->hasparams = false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
193 stringarray_init(&m->params);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
194 expansionitemarray_init(&m->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
195 m->inuse = false;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
196 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
197 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
198
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
199 DESTROYALL_ARRAY(expansionitem, );
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
200
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
201 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
202 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
203 macro_destroy(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
204 {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
205 expansionitemarray_destroyall(&m->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
206 expansionitemarray_cleanup(&m->expansion);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
207 dostrfree(m->name);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
208 dofree(m, sizeof(*m));
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
209 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
210
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
211 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
212 bool
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
213 macro_eq(const struct macro *m1, const struct macro *m2)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
214 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
215 unsigned num1, num2, i;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
216 struct expansionitem *ei1, *ei2;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
217 const char *p1, *p2;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
218
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
219 if (strcmp(m1->name, m2->name) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
220 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
221 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
222
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
223 if (m1->hasparams != m2->hasparams) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
224 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
225 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
226
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
227 num1 = expansionitemarray_num(&m1->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
228 num2 = expansionitemarray_num(&m2->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
229 if (num1 != num2) {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
230 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
231 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
232
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
233 for (i=0; i<num1; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
234 ei1 = expansionitemarray_get(&m1->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
235 ei2 = expansionitemarray_get(&m2->expansion, i);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
236 if (!expansionitem_eq(ei1, ei2)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
237 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
238 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
239 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
240
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
241 num1 = stringarray_num(&m1->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
242 num2 = stringarray_num(&m2->params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
243 if (num1 != num2) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
244 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
245 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
246
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
247 for (i=0; i<num1; i++) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
248 p1 = stringarray_get(&m1->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
249 p2 = stringarray_get(&m2->params, i);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
250 if (strcmp(p1, p2) != 0) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
251 return false;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
252 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
253 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
254 return true;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
255 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
256
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
257 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
258 // macro table
76da41da923f added macro table
David A. Holland
parents:
diff changeset
259
76da41da923f added macro table
David A. Holland
parents:
diff changeset
260 /*
76da41da923f added macro table
David A. Holland
parents:
diff changeset
261 * Unless I've screwed up, this is something called Fletcher's Checksum
76da41da923f added macro table
David A. Holland
parents:
diff changeset
262 * that showed up in Dr. Dobbs in, according to my notes, May 1992. The
76da41da923f added macro table
David A. Holland
parents:
diff changeset
263 * implementation is new.
76da41da923f added macro table
David A. Holland
parents:
diff changeset
264 */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
265 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
266 unsigned
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
267 hashfunc(const char *s, size_t len)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
268 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
269 uint16_t x1, x2, a;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
270 size_t i;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
271
76da41da923f added macro table
David A. Holland
parents:
diff changeset
272 x1 = (uint16_t) (len >> 16);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
273 x2 = (uint16_t) (len);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
274 if (x1==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
275 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
276 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
277 if (x2==0) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
278 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
279 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
280
76da41da923f added macro table
David A. Holland
parents:
diff changeset
281 for (i=0; i<len; i+=2) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
282 if (i==len-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
283 a = (unsigned char)s[i];
76da41da923f added macro table
David A. Holland
parents:
diff changeset
284 /* don't run off the end of the array */
76da41da923f added macro table
David A. Holland
parents:
diff changeset
285 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
286 else {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
287 a = (unsigned char)s[i] +
76da41da923f added macro table
David A. Holland
parents:
diff changeset
288 ((uint16_t)(unsigned char)s[i+1] << 8);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
289 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
290 x1 += a;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
291 if (x1 < a) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
292 x1++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
293 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
294 x2 += x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
295 if (x2 < x1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
296 x2++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
297 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
298 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
299
76da41da923f added macro table
David A. Holland
parents:
diff changeset
300 x1 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
301 x2 ^= 0xffff;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
302 return ((uint32_t)x2)*65535U + x1;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
303 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
304
76da41da923f added macro table
David A. Holland
parents:
diff changeset
305 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
306 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
307 macrotable_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
308 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
309 unsigned i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
310
76da41da923f added macro table
David A. Holland
parents:
diff changeset
311 macroarrayarray_init(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
312 macroarrayarray_setsize(&macros, 4);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
313 for (i=0; i<4; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
314 macroarrayarray_set(&macros, i, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
315 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
316 total_macros = 0;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
317 hashmask = 0x3;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
318 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
319
76da41da923f added macro table
David A. Holland
parents:
diff changeset
320 DESTROYALL_ARRAY(macro, );
76da41da923f added macro table
David A. Holland
parents:
diff changeset
321
76da41da923f added macro table
David A. Holland
parents:
diff changeset
322 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
323 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
324 macrotable_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
325 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
326 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
327 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
328
76da41da923f added macro table
David A. Holland
parents:
diff changeset
329 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
330 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
331 bucket = macroarrayarray_get(&macros, i);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
332 if (bucket != NULL) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
333 macroarray_destroyall(bucket);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
334 macroarray_destroy(bucket);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
335 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
336 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
337 macroarrayarray_setsize(&macros, 0);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
338 macroarrayarray_cleanup(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
339 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
340
76da41da923f added macro table
David A. Holland
parents:
diff changeset
341 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
342 struct macro *
191
c8d4ab9aeff0 don't use "remove" as a local variable; fails on gcc4.1
David A. Holland
parents: 184
diff changeset
343 macrotable_findlen(const char *name, size_t len, bool remove_it)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
344 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
345 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
346 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
347 struct macro *m, *m2;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
348 unsigned i, num;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
349 size_t mlen;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
350
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
351 hash = hashfunc(name, len);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
352 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
353 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
354 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
355 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
356 num = macroarray_num(bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
357 for (i=0; i<num; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
358 m = macroarray_get(bucket, i);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
359 if (hash != m->hash) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
360 continue;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
361 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
362 mlen = strlen(m->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
363 if (len == mlen && !memcmp(name, m->name, len)) {
191
c8d4ab9aeff0 don't use "remove" as a local variable; fails on gcc4.1
David A. Holland
parents: 184
diff changeset
364 if (remove_it) {
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
365 if (i < num-1) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
366 m2 = macroarray_get(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
367 macroarray_set(bucket, i, m2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
368 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
369 macroarray_setsize(bucket, num-1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
370 total_macros--;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
371 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
372 return m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
373 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
374 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
375 return NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
376 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
377
76da41da923f added macro table
David A. Holland
parents:
diff changeset
378 static
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
379 struct macro *
191
c8d4ab9aeff0 don't use "remove" as a local variable; fails on gcc4.1
David A. Holland
parents: 184
diff changeset
380 macrotable_find(const char *name, bool remove_it)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
381 {
191
c8d4ab9aeff0 don't use "remove" as a local variable; fails on gcc4.1
David A. Holland
parents: 184
diff changeset
382 return macrotable_findlen(name, strlen(name), remove_it);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
383 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
384
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
385 static
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
386 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
387 macrotable_rehash(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
388 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
389 struct macroarray *newbucket, *oldbucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
390 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
391 unsigned newmask, tossbit;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
392 unsigned numbuckets, i;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
393 unsigned oldnum, j, k;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
394
76da41da923f added macro table
David A. Holland
parents:
diff changeset
395 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
396 macroarrayarray_setsize(&macros, numbuckets*2);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
397
76da41da923f added macro table
David A. Holland
parents:
diff changeset
398 assert(hashmask == numbuckets - 1);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
399 newmask = (hashmask << 1) | 1U;
74
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
400 tossbit = newmask & ~hashmask;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
401 hashmask = newmask;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
402
76da41da923f added macro table
David A. Holland
parents:
diff changeset
403 for (i=0; i<numbuckets; i++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
404 newbucket = NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
405 oldbucket = macroarrayarray_get(&macros, i);
74
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
406 if (oldbucket == NULL) {
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
407 macroarrayarray_set(&macros, numbuckets + i, NULL);
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
408 continue;
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
409 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
410 oldnum = macroarray_num(oldbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
411 for (j=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
412 m = macroarray_get(oldbucket, j);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
413 if (m->hash & tossbit) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
414 if (newbucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
415 newbucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
416 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
417 macroarray_set(oldbucket, j, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
418 macroarray_add(newbucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
419 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
420 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
421 for (j=k=0; j<oldnum; j++) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
422 m = macroarray_get(oldbucket, j);
74
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
423 if (m != NULL) {
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
424 if (k < j) {
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
425 macroarray_set(oldbucket, k, m);
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
426 }
bbbf71859a21 fix stupid bugs
David A. Holland
parents: 47
diff changeset
427 k++;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
428 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
429 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
430 macroarray_setsize(oldbucket, k);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
431 macroarrayarray_set(&macros, numbuckets + i, newbucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
432 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
433 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
434
76da41da923f added macro table
David A. Holland
parents:
diff changeset
435 static
76da41da923f added macro table
David A. Holland
parents:
diff changeset
436 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
437 macrotable_add(struct macro *m)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
438 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
439 unsigned hash;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
440 struct macroarray *bucket;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
441 unsigned numbuckets;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
442
76da41da923f added macro table
David A. Holland
parents:
diff changeset
443 numbuckets = macroarrayarray_num(&macros);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
444 if (total_macros > 0 && total_macros / numbuckets > 9) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
445 macrotable_rehash();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
446 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
447
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
448 hash = hashfunc(m->name, strlen(m->name));
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
449 bucket = macroarrayarray_get(&macros, hash & hashmask);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
450 if (bucket == NULL) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
451 bucket = macroarray_create();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
452 macroarrayarray_set(&macros, hash & hashmask, bucket);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
453 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
454 macroarray_add(bucket, m, NULL);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
455 total_macros++;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
456 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
457
76da41da923f added macro table
David A. Holland
parents:
diff changeset
458 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
459 // external macro definition interface
76da41da923f added macro table
David A. Holland
parents:
diff changeset
460
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
461 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
462 struct macro *
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
463 macro_define_common_start(struct place *p1, const char *macro,
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
464 struct place *p2)
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
465 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
466 struct macro *m;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
467 unsigned hash;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
468
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
469 if (!is_identifier(macro)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
470 complain(p1, "Invalid macro name %s", macro);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
471 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
472 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
473
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
474 hash = hashfunc(macro, strlen(macro));
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
475 m = macro_create(p1, macro, hash, p2);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
476 return m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
477 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
478
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
479 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
480 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
481 macro_define_common_end(struct macro *m)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
482 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
483 struct macro *oldm;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
484 bool ok;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
485
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
486 oldm = macrotable_find(m->name, false);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
487 if (oldm != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
488 ok = macro_eq(m, oldm);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
489 if (ok) {
84
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
490 /* in traditional cpp this is silent */
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
491 //complain(&m->defplace,
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
492 // "Warning: redefinition of %s", m->name);
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
493 //complain(&oldm->defplace,
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
494 // "Previous definition was here");
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
495 //if (mode.werror) {
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
496 // complain_fail();
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
497 //}
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
498 } else {
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
499 complain(&m->defplace,
84
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
500 "Warning: non-identical redefinition of %s",
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
501 m->name);
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
502 complain(&oldm->defplace,
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
503 "Previous definition was here");
7e4723d34248 Complain only about non-identical redefinitions of macros.
David A. Holland
parents: 74
diff changeset
504 /* in traditional cpp this is not fatal */
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
505 if (mode.werror) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
506 complain_fail();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
507 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
508 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
509 macro_destroy(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
510 return;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
511 }
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
512 macrotable_add(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
513 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
514
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
515 static
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
516 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
517 macro_parse_parameters(struct macro *m, struct place *p, const char *params)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
518 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
519 size_t len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
520 const char *s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
521 char *param;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
522
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
523 while (params != NULL) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
524 len = strspn(params, ws);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
525 params += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
526 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
527 s = strchr(params, ',');
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
528 if (s) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
529 len = s-params;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
530 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
531 s++;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
532 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
533 len = strlen(params);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
534 param = dostrndup(params, len);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
535 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
536 notrailingws(param, strlen(param));
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
537 if (!is_identifier(param)) {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
538 complain(p, "Invalid macro parameter name %s", param);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
539 complain_fail();
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
540 } else {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
541 stringarray_add(&m->params, param, NULL);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
542 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
543 params = s;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
544 p->column += len;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
545 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
546 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
547
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
548 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
549 bool
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
550 isparam(struct macro *m, const char *name, size_t len, unsigned *num_ret)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
551 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
552 unsigned num, i;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
553 const char *param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
554
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
555 num = stringarray_num(&m->params);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
556 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
557 param = stringarray_get(&m->params, i);
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 25
diff changeset
558 if (strlen(param) == len && !memcmp(name, param, len)) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
559 *num_ret = i;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
560 return true;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
561 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
562 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
563 return false;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
564 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
565
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
566 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
567 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
568 macro_parse_expansion(struct macro *m, const char *buf)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
569 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
570 size_t blockstart, wordstart, pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
571 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
572 unsigned param;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
573
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
574 pos = blockstart = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
575 while (buf[pos] != '\0') {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
576 pos += strspn(buf+pos, ws);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
577 if (strchr(alnum, buf[pos])) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
578 wordstart = pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
579 pos += strspn(buf+pos, alnum);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
580 if (isparam(m, buf+wordstart, pos-wordstart, &param)) {
144
7ab3d0c09cd8 fix very minor bug
David A. Holland
parents: 129
diff changeset
581 if (wordstart > blockstart) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
582 ei = expansionitem_create_stringlen(
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
583 buf + blockstart,
27
01c3a2088ab4 fix some more bugs
David A. Holland
parents: 25
diff changeset
584 wordstart - blockstart);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
585 expansionitemarray_add(&m->expansion,
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
586 ei, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
587 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
588 ei = expansionitem_create_param(param);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
589 expansionitemarray_add(&m->expansion, ei,NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
590 blockstart = pos;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
591 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
592 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
593 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
594 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
595 pos++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
596 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
597 if (pos > blockstart) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
598 ei = expansionitem_create_stringlen(buf + blockstart,
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
599 pos - blockstart);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
600 expansionitemarray_add(&m->expansion, ei, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
601 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
602 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
603
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
604 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
605 macro_define_plain(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
606 struct place *p2, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
607 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
608 struct macro *m;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
609 struct expansionitem *ei;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
610
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
611 m = macro_define_common_start(p1, macro, p2);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
612 ei = expansionitem_create_string(expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
613 expansionitemarray_add(&m->expansion, ei, NULL);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
614 macro_define_common_end(m);
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
615 }
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
616
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
617 void
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
618 macro_define_params(struct place *p1, const char *macro,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
619 struct place *p2, const char *params,
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
620 struct place *p3, const char *expansion)
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
621 {
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
622 struct macro *m;
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
623
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
624 m = macro_define_common_start(p1, macro, p3);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
625 m->hasparams = true;
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
626 macro_parse_parameters(m, p2, params);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
627 macro_parse_expansion(m, expansion);
18
c08a947d8f30 deal with macro parameters
David A. Holland
parents: 17
diff changeset
628 macro_define_common_end(m);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
629 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
630
76da41da923f added macro table
David A. Holland
parents:
diff changeset
631 void
172
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
632 macro_define_magic(struct place *p, const char *macro)
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
633 {
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
634 struct macro *m;
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
635 struct expansionitem *ei;
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
636
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
637 m = macro_define_common_start(p, macro, p);
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
638 if (!strcmp(macro, "__FILE__")) {
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
639 ei = expansionitem_create_file();
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
640 }
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
641 else {
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
642 assert(!strcmp(macro, "__LINE__"));
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
643 ei = expansionitem_create_line();
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
644 }
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
645 expansionitemarray_add(&m->expansion, ei, NULL);
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
646 macro_define_common_end(m);
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
647 }
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
648
3e7e696fe558 Clean up the mess Joerg made of apply_builtin_macros().
David A. Holland
parents: 171
diff changeset
649 void
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
650 macro_undef(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
651 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
652 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
653
76da41da923f added macro table
David A. Holland
parents:
diff changeset
654 m = macrotable_find(macro, true);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
655 if (m) {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
656 macro_destroy(m);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
657 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
658 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
659
76da41da923f added macro table
David A. Holland
parents:
diff changeset
660 bool
76da41da923f added macro table
David A. Holland
parents:
diff changeset
661 macro_isdefined(const char *macro)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
662 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
663 struct macro *m;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
664
76da41da923f added macro table
David A. Holland
parents:
diff changeset
665 m = macrotable_find(macro, false);
76da41da923f added macro table
David A. Holland
parents:
diff changeset
666 return m != NULL;
76da41da923f added macro table
David A. Holland
parents:
diff changeset
667 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
668
76da41da923f added macro table
David A. Holland
parents:
diff changeset
669 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
670 // macro expansion
76da41da923f added macro table
David A. Holland
parents:
diff changeset
671
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
672 struct expstate {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
673 bool honordefined;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
674 enum { ES_NORMAL, ES_WANTLPAREN, ES_NOARG, ES_HAVEARG } state;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
675 struct macro *curmacro;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
676 struct stringarray args;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
677 unsigned argparens;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
678
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
679 bool tobuf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
680 char *buf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
681 size_t bufpos, bufmax;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
682 };
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
683
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
684 static struct expstate mainstate;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
685
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
686 static void doexpand(struct expstate *es, struct place *p,
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
687 const char *buf, size_t len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
688
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
689 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
690 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
691 expstate_init(struct expstate *es, bool tobuf, bool honordefined)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
692 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
693 es->honordefined = honordefined;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
694 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
695 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
696 stringarray_init(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
697 es->argparens = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
698 es->tobuf = tobuf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
699 es->buf = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
700 es->bufpos = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
701 es->bufmax = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
702 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
703
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
704 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
705 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
706 expstate_cleanup(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
707 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
708 assert(es->state == ES_NORMAL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
709 stringarray_cleanup(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
710 if (es->buf) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
711 dofree(es->buf, es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
712 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
713 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
714
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
715 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
716 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
717 expstate_destroyargs(struct expstate *es)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
718 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
719 unsigned i, num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
720
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
721 num = stringarray_num(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
722 for (i=0; i<num; i++) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
723 dostrfree(stringarray_get(&es->args, i));
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
724 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
725 stringarray_setsize(&es->args, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
726 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
727
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
728 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
729 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
730 expand_send(struct expstate *es, struct place *p, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
731 {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
732 size_t oldmax;
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
733
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
734 if (es->tobuf) {
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 33
diff changeset
735 assert(es->bufpos <= es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
736 if (es->bufpos + len > es->bufmax) {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
737 oldmax = es->bufmax;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
738 if (es->bufmax == 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
739 es->bufmax = 64;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
740 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
741 while (es->bufpos + len > es->bufmax) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
742 es->bufmax *= 2;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
743 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
744 es->buf = dorealloc(es->buf, oldmax, es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
745 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
746 memcpy(es->buf + es->bufpos, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
747 es->bufpos += len;
38
b156910b59b2 Wrap free() in dofree() to allow instrumenting it for debugging.
David A. Holland
parents: 33
diff changeset
748 assert(es->bufpos <= es->bufmax);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
749 } else {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
750 output(p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
751 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
752 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
753
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
754 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
755 void
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
756 expand_send_eof(struct expstate *es, struct place *p)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
757 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
758 if (es->tobuf) {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
759 expand_send(es, p, "", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
760 es->bufpos--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
761 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
762 output_eof();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
763 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
764 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
765
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
766 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
767 void
161
4a4b3d5c41fa Use more const.
Joerg Sonnenberger <joerg@bec.de>
parents: 159
diff changeset
768 expand_newarg(struct expstate *es, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
769 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
770 char *text;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
771
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
772 text = dostrndup(buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
773 stringarray_add(&es->args, text, NULL);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
774 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
775
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
776 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
777 void
161
4a4b3d5c41fa Use more const.
Joerg Sonnenberger <joerg@bec.de>
parents: 159
diff changeset
778 expand_appendarg(struct expstate *es, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
779 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
780 unsigned num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
781 char *text;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
782 size_t oldlen;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
783
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
784 num = stringarray_num(&es->args);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
785 assert(num > 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
786
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
787 text = stringarray_get(&es->args, num - 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
788 oldlen = strlen(text);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
789 text = dorealloc(text, oldlen + 1, oldlen + len + 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
790 memcpy(text + oldlen, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
791 text[oldlen+len] = '\0';
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
792 stringarray_set(&es->args, num - 1, text);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
793 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
794
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
795 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
796 char *
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
797 expand_substitute(struct place *p, struct expstate *es)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
798 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
799 struct expansionitem *ei;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
800 unsigned i, num;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
801 size_t len;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
802 char *arg;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
803 char *ret;
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
804 unsigned numargs, numparams;
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
805 char numbuf[64];
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
806
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
807 numargs = stringarray_num(&es->args);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
808 numparams = stringarray_num(&es->curmacro->params);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
809
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
810 if (numargs == 0 && numparams == 1) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
811 /* no arguments <=> one empty argument */
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
812 stringarray_add(&es->args, dostrdup(""), NULL);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
813 numargs++;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
814 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
815 if (numargs != numparams) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
816 complain(p, "Wrong number of arguments for macro %s; "
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
817 "found %u, expected %u",
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
818 es->curmacro->name, numargs, numparams);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
819 complain_fail();
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
820 while (numargs < numparams) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
821 stringarray_add(&es->args, dostrdup(""), NULL);
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
822 numargs++;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
823 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
824 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
825
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
826 len = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
827 num = expansionitemarray_num(&es->curmacro->expansion);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
828 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
829 ei = expansionitemarray_get(&es->curmacro->expansion, i);
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
830 switch (ei->itemtype) {
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
831 case EI_STRING:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
832 len += strlen(ei->ei_string);
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
833 break;
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
834 case EI_PARAM:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
835 arg = stringarray_get(&es->args, ei->ei_param);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
836 len += strlen(arg);
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
837 break;
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
838 case EI_FILE:
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
839 len += strlen(place_getname(p)) + 2;
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
840 break;
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
841 case EI_LINE:
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
842 len += snprintf(numbuf, sizeof(numbuf), "%u", p->line);
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
843 break;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
844 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
845 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
846
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
847 ret = domalloc(len+1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
848 *ret = '\0';
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
849 for (i=0; i<num; i++) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
850 ei = expansionitemarray_get(&es->curmacro->expansion, i);
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
851 switch (ei->itemtype) {
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
852 case EI_STRING:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
853 strcat(ret, ei->ei_string);
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
854 break;
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
855 case EI_PARAM:
184
d359d9b86327 Don't rely on anonymous unions.
David A. Holland
parents: 177
diff changeset
856 arg = stringarray_get(&es->args, ei->ei_param);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
857 strcat(ret, arg);
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
858 break;
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
859 case EI_FILE:
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
860 strcat(ret, "\"");
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
861 strcat(ret, place_getname(p));
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
862 strcat(ret, "\"");
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
863 break;
174
09cfad6772de Some minor cosmetic changes relating to ei->itemtype.
David A. Holland
parents: 172
diff changeset
864 case EI_LINE:
159
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
865 snprintf(numbuf, sizeof(numbuf), "%u", p->line);
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
866 strcat(ret, numbuf);
8cef6d7227a8 Expand __FILE__ and __LINE__.
Joerg Sonnenberger <joerg@bec.de>
parents: 158
diff changeset
867 break;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
868 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
869 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
870
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
871 return ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
872 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
873
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
874 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
875 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
876 expand_domacro(struct expstate *es, struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
877 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
878 struct macro *m;
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
879 const char *name, *val;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
880 char *newbuf, *newbuf2;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
881
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
882 if (es->curmacro == NULL) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
883 /* defined() */
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
884 if (stringarray_num(&es->args) != 1) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
885 complain(p, "Too many arguments for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
886 complain_fail();
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
887 expand_send(es, p, "0", 1);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
888 return;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
889 }
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
890 name = stringarray_get(&es->args, 0);
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
891 m = macrotable_find(name, false);
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
892 val = (m != NULL) ? "1" : "0";
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
893 debuglog(p, "defined(%s): %s", name, val);
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
894 expand_send(es, p, val, 1);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
895 expstate_destroyargs(es);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
896 return;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
897 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
898
192
255db24bc29b Fix output corruption; see CHANGES entry.
David A. Holland
parents: 191
diff changeset
899 m = es->curmacro;
255db24bc29b Fix output corruption; see CHANGES entry.
David A. Holland
parents: 191
diff changeset
900 assert(m->inuse == false);
255db24bc29b Fix output corruption; see CHANGES entry.
David A. Holland
parents: 191
diff changeset
901 m->inuse = true;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
902
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
903 debuglog(p, "Expanding macro %s", m->name);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
904 newbuf = expand_substitute(p, es);
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
905 debuglog(p, "Substituting for %s: %s", m->name, newbuf);
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
906
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
907 newbuf2 = macroexpand(p, newbuf, strlen(newbuf), false);
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
908 dostrfree(newbuf);
25
18681e5ae6e4 fix some more bugs
David A. Holland
parents: 21
diff changeset
909 expstate_destroyargs(es);
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
910 debuglog(p, "Complete expansion for %s: %s", m->name, newbuf2);
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 192
diff changeset
911
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
912 doexpand(es, p, newbuf2, strlen(newbuf2));
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
913 dostrfree(newbuf2);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
914
192
255db24bc29b Fix output corruption; see CHANGES entry.
David A. Holland
parents: 191
diff changeset
915 m->inuse = false;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
916 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
917
87
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
918 /*
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
919 * The traditional behavior if a function-like macro appears without
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
920 * arguments is to pretend it isn't a macro; that is, just emit its
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
921 * name.
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
922 */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
923 static
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
924 void
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
925 expand_missingargs(struct expstate *es, struct place *p, bool needspace)
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
926 {
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
927 if (es->curmacro == NULL) {
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
928 /* defined */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
929 expand_send(es, p, "defined", 7);
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
930 return;
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
931 }
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
932 expand_send(es, p, es->curmacro->name, strlen(es->curmacro->name));
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
933 /* send a space in case we ate whitespace after the macro name */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
934 if (needspace) {
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
935 expand_send(es, p, " ", 1);
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
936 }
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
937 }
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
938
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
939 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
940 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
941 expand_got_ws(struct expstate *es, struct place *p,
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
942 const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
943 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
944 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
945 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
946 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
947 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
948 case ES_WANTLPAREN:
152
e787bd67bf88 add commented-out note for later
David A. Holland
parents: 145
diff changeset
949 /* XXX notyet */
e787bd67bf88 add commented-out note for later
David A. Holland
parents: 145
diff changeset
950 //expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
951 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
952 case ES_NOARG:
118
c13f36775fe8 Preserve leading whitespace in macro arguments.
David A. Holland
parents: 117
diff changeset
953 expand_newarg(es, buf, len);
c13f36775fe8 Preserve leading whitespace in macro arguments.
David A. Holland
parents: 117
diff changeset
954 es->state = ES_HAVEARG;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
955 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
956 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
957 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
958 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
959 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
960 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
961
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
962 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
963 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
964 expand_got_word(struct expstate *es, struct place *p,
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
965 const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
966 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
967 struct macro *m;
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
968
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
969 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
970 case ES_NORMAL:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
971 if (es->honordefined &&
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
972 len == 7 && !memcmp(buf, "defined", 7)) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
973 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
974 es->state = ES_WANTLPAREN;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
975 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
976 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
977 m = macrotable_findlen(buf, len, false);
42
ad7763329eba Don't crash if a macro tries to expand itself recursively.
David A. Holland
parents: 40
diff changeset
978 if (m == NULL || m->inuse) {
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
979 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
980 } else if (!m->hasparams) {
155
e6eb15635a48 Don't shortcut macro expansion of non-parameter macros.
David A. Holland
parents: 152
diff changeset
981 es->curmacro = m;
e6eb15635a48 Don't shortcut macro expansion of non-parameter macros.
David A. Holland
parents: 152
diff changeset
982 expand_domacro(es, p);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
983 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
984 es->curmacro = m;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
985 es->state = ES_WANTLPAREN;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
986 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
987 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
988 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
989 if (es->curmacro != NULL) {
87
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
990 expand_missingargs(es, p, true);
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
991 es->state = ES_NORMAL;
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
992 /* try again */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
993 expand_got_word(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
994 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
995 /* "defined foo" means "defined(foo)" */
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
996 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
997 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
998 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
999 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1000 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1001 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1002 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1003 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1004 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1005 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1006 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1007 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1008 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1009 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1010
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1011 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1012 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1013 expand_got_lparen(struct expstate *es, struct place *p,
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1014 const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1015 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1016 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1017 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1018 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1019 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1020 case ES_WANTLPAREN:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1021 es->state = ES_NOARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1022 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1023 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1024 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1025 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1026 es->argparens++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1027 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1028 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1029 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1030 es->argparens++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1031 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1032 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1033 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1034
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1035 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1036 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1037 expand_got_rparen(struct expstate *es, struct place *p,
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1038 const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1039 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1040 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1041 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1042 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1043 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1044 case ES_WANTLPAREN:
87
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1045 expand_missingargs(es, p, false);
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1046 es->state = ES_NORMAL;
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1047 /* try again */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1048 expand_got_rparen(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1049 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1050 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1051 assert(es->argparens == 0);
117
c46959e2d9ef Handle empty arguments properly when there's more than one argument.
David A. Holland
parents: 107
diff changeset
1052 if (stringarray_num(&es->args) > 0) {
c46959e2d9ef Handle empty arguments properly when there's more than one argument.
David A. Holland
parents: 107
diff changeset
1053 /* we are after a comma; enter an empty argument */
c46959e2d9ef Handle empty arguments properly when there's more than one argument.
David A. Holland
parents: 107
diff changeset
1054 expand_newarg(es, buf, 0);
c46959e2d9ef Handle empty arguments properly when there's more than one argument.
David A. Holland
parents: 107
diff changeset
1055 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1056 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1057 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1058 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1059 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1060 if (es->argparens > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1061 es->argparens--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1062 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1063 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1064 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1065 expand_domacro(es, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1066 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1067 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1068 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1069 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1070
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1071 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1072 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1073 expand_got_comma(struct expstate *es, struct place *p,
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1074 const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1075 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1076 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1077 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1078 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1079 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1080 case ES_WANTLPAREN:
87
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1081 expand_missingargs(es, p, false);
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1082 es->state = ES_NORMAL;
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1083 /* try again */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1084 expand_got_comma(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1085 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1086 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1087 assert(es->argparens == 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1088 expand_newarg(es, buf, 0);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1089 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1090 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1091 if (es->argparens > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1092 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1093 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1094 es->state = ES_NOARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1095 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1096 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1097 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1098 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1099
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1100 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1101 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1102 expand_got_other(struct expstate *es, struct place *p,
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1103 const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1104 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1105 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1106 case ES_NORMAL:
21
e3fab8f1b52c strip comments.
David A. Holland
parents: 19
diff changeset
1107 expand_send(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1108 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1109 case ES_WANTLPAREN:
87
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1110 expand_missingargs(es, p, false);
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1111 es->state = ES_NORMAL;
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1112 /* try again */
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1113 expand_got_other(es, p, buf, len);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1114 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1115 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1116 expand_newarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1117 es->state = ES_HAVEARG;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1118 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1119 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1120 expand_appendarg(es, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1121 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1122 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1123 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1124
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1125 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1126 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1127 expand_got_eof(struct expstate *es, struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1128 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1129 switch (es->state) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1130 case ES_NORMAL:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1131 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1132 case ES_WANTLPAREN:
87
2b153df78214 Don't bomb out if a function-like macro is given no arguments.
David A. Holland
parents: 85
diff changeset
1133 expand_missingargs(es, p, false);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1134 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1135 case ES_NOARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1136 case ES_HAVEARG:
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1137 if (es->curmacro) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1138 complain(p, "Unclosed argument list for macro %s",
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1139 es->curmacro->name);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1140 } else {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1141 complain(p, "Unclosed argument list for defined()");
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1142 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1143 complain_fail();
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1144 expstate_destroyargs(es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1145 break;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1146 }
88
36066289e31a Always send the expansion state an EOF.
David A. Holland
parents: 87
diff changeset
1147 expand_send_eof(es, p);
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1148 es->state = ES_NORMAL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1149 es->curmacro = NULL;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1150 es->argparens = 0;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1151 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1152
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1153 static
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1154 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1155 doexpand(struct expstate *es, struct place *p, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1156 {
85
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1157 char *s;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1158 size_t x;
85
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1159 bool inquote = false;
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1160 char quote = '\0';
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1161
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1162 while (len > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1163 x = strspn(buf, ws);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1164 if (x > len) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1165 /* XXX gross, need strnspn */
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1166 x = len;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1167 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1168
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1169 if (x > 0) {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1170 expand_got_ws(es, p, buf, x);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1171 buf += x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1172 len -= x;
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1173 continue;
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1174 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1175
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1176 x = strspn(buf, alnum);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1177 if (x > len) {
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1178 /* XXX gross, need strnspn */
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1179 x = len;
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1180 }
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 27
diff changeset
1181
145
a403605d3166 Fix handling of macros in quoted strings.
David A. Holland
parents: 144
diff changeset
1182 if (!inquote && x > 0) {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1183 expand_got_word(es, p, buf, x);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1184 buf += x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1185 len -= x;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1186 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1187 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1188
85
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1189 if (!inquote && len > 1 && buf[0] == '/' && buf[1] == '*') {
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1190 s = strstr(buf, "*/");
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1191 if (s) {
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1192 x = s - buf;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1193 } else {
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1194 x = len;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1195 }
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1196 expand_got_ws(es, p, buf, x);
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1197 buf += x;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1198 len -= x;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1199 continue;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1200 }
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1201
129
2e1496dd96c4 Don't recognize macro argument parens or commas within quotes.
David A. Holland
parents: 128
diff changeset
1202 if (!inquote && buf[0] == '(') {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1203 expand_got_lparen(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1204 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1205 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1206 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1207 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1208
129
2e1496dd96c4 Don't recognize macro argument parens or commas within quotes.
David A. Holland
parents: 128
diff changeset
1209 if (!inquote && buf[0] == ')') {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1210 expand_got_rparen(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1211 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1212 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1213 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1214 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1215
129
2e1496dd96c4 Don't recognize macro argument parens or commas within quotes.
David A. Holland
parents: 128
diff changeset
1216 if (!inquote && buf[0] == ',') {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1217 expand_got_comma(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1218 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1219 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1220 continue;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1221 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1222
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1223 if (len > 1 && buf[0] == '\\' &&
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1224 (buf[1] == '"' || buf[1] == '\'')) {
85
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1225 expand_got_other(es, p, buf, 2);
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1226 buf += 2;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1227 len -= 2;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1228 continue;
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1229 }
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1230 if (!inquote && (buf[0] == '"' || buf[0] == '\'')) {
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1231 inquote = true;
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1232 quote = buf[0];
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1233 } else if (inquote && buf[0] == quote) {
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 118
diff changeset
1234 inquote = false;
85
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1235 }
c91dc1315745 Don't expand macros inside comments.
David A. Holland
parents: 84
diff changeset
1236
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1237 expand_got_other(es, p, buf, 1);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1238 buf++;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1239 len--;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1240 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1241 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1242
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1243 char *
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1244 macroexpand(struct place *p, const char *buf, size_t len, bool honordefined)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1245 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1246 struct expstate es;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1247 char *ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1248
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1249 expstate_init(&es, true, honordefined);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1250 doexpand(&es, p, buf, len);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1251 expand_got_eof(&es, p);
40
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1252
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1253 /* trim to fit, so the malloc debugging won't complain */
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1254 es.buf = dorealloc(es.buf, es.bufmax, strlen(es.buf) + 1);
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1255
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1256 ret = es.buf;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1257 es.buf = NULL;
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
1258 es.bufpos = es.bufmax = 0;
40
291fefe664f2 Oops, fix previous.
David A. Holland
parents: 39
diff changeset
1259
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1260 expstate_cleanup(&es);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1261
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1262 return ret;
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1263 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1264
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1265 void
177
6119608a9817 Finish joerg's halfassedly partial const changes.
David A. Holland
parents: 175
diff changeset
1266 macro_sendline(struct place *p, const char *buf, size_t len)
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1267 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1268 doexpand(&mainstate, p, buf, len);
162
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1269 switch (mainstate.state) {
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1270 case ES_NORMAL:
175
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1271 /*
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1272 * If we were sent a blank line, don't emit a newline
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1273 * for it. This matches the prior behavior of tradcpp.
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1274 */
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1275 if (len > 0) {
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1276 output(p, "\n", 1);
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1277 }
162
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1278 break;
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1279 case ES_WANTLPAREN:
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1280 case ES_NOARG:
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1281 case ES_HAVEARG:
175
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1282 /*
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1283 * Apparently to match gcc's -traditional behavior we
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1284 * need to emit a space for each newline that appears
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1285 * while processing macro args.
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 174
diff changeset
1286 */
162
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1287 expand_got_ws(&mainstate, p, " ", 1);
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1288 break;
82cc6fa54b01 newline during a macro invocation counts as space. Behavior differs from
Joerg Sonnenberger <joerg@bec.de>
parents: 161
diff changeset
1289 }
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1290 }
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1291
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1292 void
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1293 macro_sendeof(struct place *p)
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1294 {
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1295 expand_got_eof(&mainstate, p);
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1296 }
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1297
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1298 ////////////////////////////////////////////////////////////
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1299 // module initialization
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1300
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1301 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1302 macros_init(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1303 {
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1304 macrotable_init();
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1305 expstate_init(&mainstate, false, false);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1306 }
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1307
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1308 void
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1309 macros_cleanup(void)
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1310 {
19
f9792a9ec704 macro expansion.
David A. Holland
parents: 18
diff changeset
1311 expstate_cleanup(&mainstate);
17
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1312 macrotable_cleanup();
76da41da923f added macro table
David A. Holland
parents:
diff changeset
1313 }