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