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