comparison directive.c @ 18:c08a947d8f30

deal with macro parameters
author David A. Holland
date Mon, 20 Dec 2010 01:51:47 -0500
parents 9dda765ee85c
children 76c114899f63
comparison
equal deleted inserted replaced
17:76da41da923f 18:c08a947d8f30
169 169
170 static 170 static
171 void 171 void
172 d_define(struct place *p, struct place *p2, char *line, size_t len) 172 d_define(struct place *p, struct place *p2, char *line, size_t len)
173 { 173 {
174 size_t pos; 174 size_t pos, argpos;
175 struct place p3; 175 struct place p3, p4;
176 176
177 /* 177 /*
178 * line may be: 178 * line may be:
179 * macro expansion 179 * macro expansion
180 * macro(arg, arg, ...) expansion 180 * macro(arg, arg, ...) expansion
181 */ 181 */
182 182
183 pos = strcspn(line, " \t\f\v("); 183 pos = strcspn(line, " \t\f\v(");
184 if (line[pos] == '(') { 184 if (line[pos] == '(') {
185 pos++; 185 line[pos++] = '\0';
186 argpos = pos;
186 pos = pos + strcspn(line+pos, "()"); 187 pos = pos + strcspn(line+pos, "()");
187 if (line[pos] == '(') { 188 if (line[pos] == '(') {
188 p2->column += pos; 189 p2->column += pos;
189 complain(p2, "Left parenthesis in macro parameters"); 190 complain(p2, "Left parenthesis in macro parameters");
190 complain_fail(); 191 complain_fail();
194 p2->column += pos; 195 p2->column += pos;
195 complain(p2, "Unclosed macro parameter list"); 196 complain(p2, "Unclosed macro parameter list");
196 complain_fail(); 197 complain_fail();
197 return; 198 return;
198 } 199 }
199 pos++; 200 line[pos++] = '\0';
200 if (!strchr(ws, line[pos])) { 201 if (!strchr(ws, line[pos])) {
201 p2->column += pos; 202 p2->column += pos;
202 complain(p2, "Trash after macro parameter list"); 203 complain(p2, "Trash after macro parameter list");
203 complain_fail(); 204 complain_fail();
204 return; 205 return;
205 } 206 }
206 line[pos++] = '\0';
207 } else if (line[pos] == '\0') { 207 } else if (line[pos] == '\0') {
208 /* nothing */ 208 argpos = 0;
209 } else { 209 } else {
210 line[pos++] = '\0'; 210 line[pos++] = '\0';
211 argpos = 0;
211 } 212 }
212 213
213 pos += strspn(line+pos, ws); 214 pos += strspn(line+pos, ws);
214 215
215 p3 = *p2; 216 p3 = *p2;
216 p3.column += pos; 217 p3.column += argpos;
217 macro_define(p2, line, &p3, line + pos); 218
219 p4 = *p2;
220 p4.column += pos;
221
222 if (argpos) {
223 macro_define_params(p2, line, &p3,
224 line + argpos, &p4,
225 line + pos);
226 } else {
227 macro_define_plain(p2, line, &p4, line + pos);
228 }
218 } 229 }
219 230
220 static 231 static
221 void 232 void
222 d_undef(struct place *p, struct place *p2, char *line, size_t len) 233 d_undef(struct place *p, struct place *p2, char *line, size_t len)
335 { "warning", true, d_warning }, 346 { "warning", true, d_warning },
336 }; 347 };
337 static const unsigned numdirectives = HOWMANY(directives); 348 static const unsigned numdirectives = HOWMANY(directives);
338 349
339 static 350 static
340 size_t
341 notrailingws(char *buf, size_t len)
342 {
343 while (len > 0 && strchr(ws, buf[len-1])) {
344 buf[--len] = '\0';
345 }
346 return len;
347 }
348
349 static
350 void 351 void
351 directive_gotdirective(struct place *p, char *line, size_t linelen) 352 directive_gotdirective(struct place *p, char *line, size_t linelen)
352 { 353 {
353 struct place p2; 354 struct place p2;
354 size_t len, skip; 355 size_t len, skip;