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