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