annotate macro.c @ 176:a2f047301c15

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