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