]>
Commit | Line | Data |
---|---|---|
0bb65704 | 1 | /* CPP Library - traditional lexical analysis and macro expansion. |
2 | Copyright (C) 2002 Free Software Foundation, Inc. | |
3 | Contributed by Neil Booth, May 2002 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify it | |
6 | under the terms of the GNU General Public License as published by the | |
7 | Free Software Foundation; either version 2, or (at your option) any | |
8 | later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
18 | ||
19 | #include "config.h" | |
20 | #include "system.h" | |
805e22b2 | 21 | #include "coretypes.h" |
22 | #include "tm.h" | |
0bb65704 | 23 | #include "cpplib.h" |
24 | #include "cpphash.h" | |
25 | ||
198b48a0 | 26 | /* The replacement text of a function-like macro is stored as a |
c455fac3 | 27 | contiguous sequence of aligned blocks, each representing the text |
cbe337b6 | 28 | between subsequent parameters. |
c455fac3 | 29 | |
cbe337b6 | 30 | Each block comprises the text between its surrounding parameters, |
31 | the length of that text, and the one-based index of the following | |
32 | parameter. The final block in the replacement text is easily | |
33 | recognizable as it has an argument index of zero. */ | |
198b48a0 | 34 | |
35 | struct block | |
36 | { | |
37 | unsigned int text_len; | |
38 | unsigned short arg_index; | |
39 | uchar text[1]; | |
40 | }; | |
41 | ||
42 | #define BLOCK_HEADER_LEN offsetof (struct block, text) | |
17df7fec | 43 | #define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN)) |
198b48a0 | 44 | |
06025647 | 45 | /* Structure holding information about a function-like macro |
46 | invocation. */ | |
47 | struct fun_macro | |
48 | { | |
49 | /* Memory buffer holding the trad_arg array. */ | |
50 | _cpp_buff *buff; | |
51 | ||
52 | /* An array of size the number of macro parameters + 1, containing | |
53 | the offsets of the start of each macro argument in the output | |
54 | buffer. The argument continues until the character before the | |
55 | start of the next one. */ | |
56 | size_t *args; | |
57 | ||
58 | /* The hashnode of the macro. */ | |
59 | cpp_hashnode *node; | |
60 | ||
61 | /* The offset of the macro name in the output buffer. */ | |
62 | size_t offset; | |
63 | ||
48315635 | 64 | /* The line the macro name appeared on. */ |
65 | unsigned int line; | |
66 | ||
06025647 | 67 | /* Zero-based index of argument being currently lexed. */ |
68 | unsigned int argc; | |
69 | }; | |
70 | ||
bab5e68f | 71 | /* Lexing state. It is mostly used to prevent macro expansion. */ |
72 | enum ls {ls_none = 0, /* Normal state. */ | |
9c343313 | 73 | ls_fun_open, /* When looking for '('. */ |
74 | ls_fun_close, /* When looking for ')'. */ | |
bab5e68f | 75 | ls_defined, /* After defined. */ |
76 | ls_defined_close, /* Looking for ')' of defined(). */ | |
77 | ls_hash, /* After # in preprocessor conditional. */ | |
78 | ls_predicate, /* After the predicate, maybe paren? */ | |
79 | ls_answer}; /* In answer to predicate. */ | |
80 | ||
9a468a1b | 81 | /* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c |
82 | from recognizing comments and directives during its lexing pass. */ | |
0bb65704 | 83 | |
84 | static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *)); | |
85 | static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *, | |
86 | const uchar *)); | |
9a468a1b | 87 | static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *, |
88 | int)); | |
0bb65704 | 89 | static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *)); |
9a468a1b | 90 | static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *, int)); |
198b48a0 | 91 | static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *)); |
0bb65704 | 92 | static void check_output_buffer PARAMS ((cpp_reader *, size_t)); |
f15f6c8d | 93 | static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *)); |
198b48a0 | 94 | static bool scan_parameters PARAMS ((cpp_reader *, cpp_macro *)); |
01628c3c | 95 | static bool recursive_macro PARAMS ((cpp_reader *, cpp_hashnode *)); |
198b48a0 | 96 | static void save_replacement_text PARAMS ((cpp_reader *, cpp_macro *, |
97 | unsigned int)); | |
06025647 | 98 | static void maybe_start_funlike PARAMS ((cpp_reader *, cpp_hashnode *, |
99 | const uchar *, struct fun_macro *)); | |
100 | static void save_argument PARAMS ((struct fun_macro *, size_t)); | |
101 | static void replace_args_and_push PARAMS ((cpp_reader *, struct fun_macro *)); | |
3396dc80 | 102 | static size_t canonicalize_text PARAMS ((uchar *, const uchar *, size_t, |
103 | uchar *)); | |
0bb65704 | 104 | |
105 | /* Ensures we have N bytes' space in the output buffer, and | |
106 | reallocates it if not. */ | |
107 | static void | |
108 | check_output_buffer (pfile, n) | |
109 | cpp_reader *pfile; | |
110 | size_t n; | |
111 | { | |
c455fac3 | 112 | /* We might need two bytes to terminate an unterminated comment, and |
9a468a1b | 113 | one more to terminate the line with a NUL. */ |
c455fac3 | 114 | n += 2 + 1; |
115 | ||
fb83e0d6 | 116 | if (n > (size_t) (pfile->out.limit - pfile->out.cur)) |
0bb65704 | 117 | { |
fb83e0d6 | 118 | size_t size = pfile->out.cur - pfile->out.base; |
0bb65704 | 119 | size_t new_size = (size + n) * 3 / 2; |
120 | ||
fb83e0d6 | 121 | pfile->out.base |
122 | = (uchar *) xrealloc (pfile->out.base, new_size); | |
123 | pfile->out.limit = pfile->out.base + new_size; | |
124 | pfile->out.cur = pfile->out.base + size; | |
0bb65704 | 125 | } |
126 | } | |
127 | ||
128 | /* To be called whenever a newline character is encountered in the | |
9a468a1b | 129 | input file, at CUR. Handles DOS, Mac and Unix ends of line, and |
130 | increments pfile->line. | |
131 | ||
132 | Returns a pointer the character after the newline sequence. */ | |
0bb65704 | 133 | static const uchar * |
134 | handle_newline (pfile, cur) | |
135 | cpp_reader *pfile; | |
136 | const uchar *cur; | |
137 | { | |
138 | pfile->line++; | |
139 | if (cur[0] + cur[1] == '\r' + '\n') | |
140 | cur++; | |
141 | return cur + 1; | |
142 | } | |
143 | ||
cbe337b6 | 144 | /* CUR points to any character in the current context, not necessarily |
145 | a backslash. Advances CUR until all escaped newlines are skipped, | |
146 | and returns the new position without updating the context. | |
9a468a1b | 147 | |
148 | Warns if a file buffer ends in an escaped newline. */ | |
0bb65704 | 149 | static const uchar * |
150 | skip_escaped_newlines (pfile, cur) | |
151 | cpp_reader *pfile; | |
152 | const uchar *cur; | |
153 | { | |
9a468a1b | 154 | const uchar *orig_cur = cur; |
c455fac3 | 155 | |
9a468a1b | 156 | while (*cur == '\\' && is_vspace (cur[1])) |
157 | cur = handle_newline (pfile, cur + 1); | |
158 | ||
159 | if (cur != orig_cur && cur == RLIMIT (pfile->context) && pfile->buffer->inc) | |
160 | cpp_error (pfile, DL_PEDWARN, "backslash-newline at end of file"); | |
0bb65704 | 161 | |
162 | return cur; | |
163 | } | |
164 | ||
cbe337b6 | 165 | /* CUR points to the asterisk introducing a comment in the current |
166 | context. IN_DEFINE is true if we are in the replacement text of a | |
167 | macro. | |
9a468a1b | 168 | |
169 | The asterisk and following comment is copied to the buffer pointed | |
170 | to by pfile->out.cur, which must be of sufficient size. | |
171 | Unterminated comments are diagnosed, and correctly terminated in | |
172 | the output. pfile->out.cur is updated depending upon IN_DEFINE, | |
173 | -C, -CC and pfile->state.in_directive. | |
c455fac3 | 174 | |
175 | Returns a pointer to the first character after the comment in the | |
176 | input buffer. */ | |
0bb65704 | 177 | static const uchar * |
9a468a1b | 178 | copy_comment (pfile, cur, in_define) |
0bb65704 | 179 | cpp_reader *pfile; |
180 | const uchar *cur; | |
9a468a1b | 181 | int in_define; |
0bb65704 | 182 | { |
183 | unsigned int from_line = pfile->line; | |
a854276a | 184 | const uchar *limit = RLIMIT (pfile->context); |
c455fac3 | 185 | uchar *out = pfile->out.cur; |
0bb65704 | 186 | |
9a468a1b | 187 | do |
0bb65704 | 188 | { |
c455fac3 | 189 | unsigned int c = *cur++; |
190 | *out++ = c; | |
ca52ef60 | 191 | |
192 | if (c == '/') | |
0bb65704 | 193 | { |
c455fac3 | 194 | /* An immediate slash does not terminate the comment. */ |
9a468a1b | 195 | if (out[-2] == '*' && out - 2 > pfile->out.cur) |
c455fac3 | 196 | goto done; |
197 | ||
ca52ef60 | 198 | if (*cur == '*' && cur[1] != '/' |
199 | && CPP_OPTION (pfile, warn_comments)) | |
200 | cpp_error_with_line (pfile, DL_WARNING, pfile->line, 0, | |
201 | "\"/*\" within comment"); | |
0bb65704 | 202 | } |
203 | else if (is_vspace (c)) | |
c455fac3 | 204 | { |
205 | cur = handle_newline (pfile, cur - 1); | |
206 | /* Canonicalize newline sequences and skip escaped ones. */ | |
207 | if (out[-2] == '\\') | |
208 | out -= 2; | |
209 | else | |
210 | out[-1] = '\n'; | |
211 | } | |
0bb65704 | 212 | } |
9a468a1b | 213 | while (cur < limit); |
0bb65704 | 214 | |
c455fac3 | 215 | cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment"); |
216 | *out++ = '*'; | |
217 | *out++ = '/'; | |
ca52ef60 | 218 | |
c455fac3 | 219 | done: |
9a468a1b | 220 | /* Comments in directives become spaces so that tokens are properly |
221 | separated when the ISO preprocessor re-lexes the line. The | |
222 | exception is #define. */ | |
223 | if (pfile->state.in_directive) | |
224 | { | |
225 | if (in_define) | |
226 | { | |
227 | if (CPP_OPTION (pfile, discard_comments_in_macro_exp)) | |
228 | pfile->out.cur--; | |
229 | else | |
230 | pfile->out.cur = out; | |
231 | } | |
232 | else | |
233 | pfile->out.cur[-1] = ' '; | |
234 | } | |
235 | else if (CPP_OPTION (pfile, discard_comments)) | |
236 | pfile->out.cur--; | |
237 | else | |
238 | pfile->out.cur = out; | |
239 | ||
0bb65704 | 240 | return cur; |
241 | } | |
242 | ||
9a468a1b | 243 | /* CUR points to any character in the input buffer. Skips over all |
244 | contiguous horizontal white space and NULs, including comments if | |
245 | SKIP_COMMENTS, until reaching the first non-horizontal-whitespace | |
246 | character or the end of the current context. Escaped newlines are | |
247 | removed. | |
248 | ||
249 | The whitespace is copied verbatim to the output buffer, except that | |
250 | comments are handled as described in copy_comment(). | |
251 | pfile->out.cur is updated. | |
252 | ||
253 | Returns a pointer to the first character after the whitespace in | |
254 | the input buffer. */ | |
f15f6c8d | 255 | static const uchar * |
9a468a1b | 256 | skip_whitespace (pfile, cur, skip_comments) |
f15f6c8d | 257 | cpp_reader *pfile; |
258 | const uchar *cur; | |
9a468a1b | 259 | int skip_comments; |
f15f6c8d | 260 | { |
9a468a1b | 261 | uchar *out = pfile->out.cur; |
f15f6c8d | 262 | |
263 | for (;;) | |
264 | { | |
9a468a1b | 265 | unsigned int c = *cur++; |
266 | *out++ = c; | |
f15f6c8d | 267 | |
9a468a1b | 268 | if (is_nvspace (c) && c) |
f15f6c8d | 269 | continue; |
270 | ||
9c343313 | 271 | if (!c && cur - 1 != RLIMIT (pfile->context)) |
9a468a1b | 272 | continue; |
f15f6c8d | 273 | |
aacf5b8e | 274 | if (c == '/' && skip_comments) |
f15f6c8d | 275 | { |
9a468a1b | 276 | const uchar *tmp = skip_escaped_newlines (pfile, cur); |
f15f6c8d | 277 | if (*tmp == '*') |
278 | { | |
9a468a1b | 279 | pfile->out.cur = out; |
280 | cur = copy_comment (pfile, tmp, false /* in_define */); | |
281 | out = pfile->out.cur; | |
f15f6c8d | 282 | continue; |
283 | } | |
284 | } | |
285 | ||
9a468a1b | 286 | out--; |
287 | if (c == '\\' && is_vspace (*cur)) | |
288 | { | |
8874ac02 | 289 | cur = skip_escaped_newlines (pfile, cur - 1); |
9a468a1b | 290 | continue; |
291 | } | |
292 | ||
f15f6c8d | 293 | break; |
294 | } | |
295 | ||
9a468a1b | 296 | pfile->out.cur = out; |
297 | return cur - 1; | |
f15f6c8d | 298 | } |
299 | ||
0bb65704 | 300 | /* Lexes and outputs an identifier starting at CUR, which is assumed |
301 | to point to a valid first character of an identifier. Returns | |
fb83e0d6 | 302 | the hashnode, and updates out.cur. */ |
0bb65704 | 303 | static cpp_hashnode * |
304 | lex_identifier (pfile, cur) | |
305 | cpp_reader *pfile; | |
306 | const uchar *cur; | |
307 | { | |
308 | size_t len; | |
fb83e0d6 | 309 | uchar *out = pfile->out.cur; |
f15f6c8d | 310 | cpp_hashnode *result; |
0bb65704 | 311 | |
312 | do | |
313 | { | |
314 | do | |
315 | *out++ = *cur++; | |
06025647 | 316 | while (is_numchar (*cur)); |
0bb65704 | 317 | cur = skip_escaped_newlines (pfile, cur); |
318 | } | |
06025647 | 319 | while (is_numchar (*cur)); |
0bb65704 | 320 | |
a854276a | 321 | CUR (pfile->context) = cur; |
fb83e0d6 | 322 | len = out - pfile->out.cur; |
323 | result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur, | |
f15f6c8d | 324 | len, HT_ALLOC); |
fb83e0d6 | 325 | pfile->out.cur = out; |
f15f6c8d | 326 | return result; |
327 | } | |
328 | ||
0bb65704 | 329 | /* Overlays the true file buffer temporarily with text of length LEN |
330 | starting at START. The true buffer is restored upon calling | |
331 | restore_buff(). */ | |
332 | void | |
333 | _cpp_overlay_buffer (pfile, start, len) | |
334 | cpp_reader *pfile; | |
335 | const uchar *start; | |
336 | size_t len; | |
337 | { | |
338 | cpp_buffer *buffer = pfile->buffer; | |
339 | ||
bab5e68f | 340 | pfile->overlaid_buffer = buffer; |
0bb65704 | 341 | buffer->saved_cur = buffer->cur; |
342 | buffer->saved_rlimit = buffer->rlimit; | |
0bb65704 | 343 | |
344 | buffer->cur = start; | |
0bb65704 | 345 | buffer->rlimit = start + len; |
fb83e0d6 | 346 | |
347 | pfile->saved_line = pfile->line; | |
0bb65704 | 348 | } |
349 | ||
350 | /* Restores a buffer overlaid by _cpp_overlay_buffer(). */ | |
fb83e0d6 | 351 | void |
352 | _cpp_remove_overlay (pfile) | |
0bb65704 | 353 | cpp_reader *pfile; |
354 | { | |
bab5e68f | 355 | cpp_buffer *buffer = pfile->overlaid_buffer; |
0bb65704 | 356 | |
357 | buffer->cur = buffer->saved_cur; | |
358 | buffer->rlimit = buffer->saved_rlimit; | |
fb83e0d6 | 359 | |
360 | pfile->line = pfile->saved_line; | |
0bb65704 | 361 | } |
362 | ||
363 | /* Reads a logical line into the output buffer. Returns TRUE if there | |
364 | is more text left in the buffer. */ | |
365 | bool | |
fb83e0d6 | 366 | _cpp_read_logical_line_trad (pfile) |
0bb65704 | 367 | cpp_reader *pfile; |
368 | { | |
01628c3c | 369 | do |
0bb65704 | 370 | { |
cbe337b6 | 371 | if (pfile->buffer->cur == pfile->buffer->rlimit) |
0bb65704 | 372 | { |
01628c3c | 373 | bool stop = true; |
374 | ||
375 | /* Don't pop the last buffer. */ | |
cbe337b6 | 376 | if (pfile->buffer->prev) |
01628c3c | 377 | { |
cbe337b6 | 378 | stop = pfile->buffer->return_at_eof; |
01628c3c | 379 | _cpp_pop_buffer (pfile); |
380 | } | |
381 | ||
382 | if (stop) | |
383 | return false; | |
0bb65704 | 384 | } |
385 | ||
01628c3c | 386 | scan_out_logical_line (pfile, NULL); |
0bb65704 | 387 | } |
01628c3c | 388 | while (pfile->state.skipping); |
a854276a | 389 | |
0bb65704 | 390 | return true; |
391 | } | |
392 | ||
06025647 | 393 | /* Set up state for finding the opening '(' of a function-like |
394 | macro. */ | |
395 | static void | |
396 | maybe_start_funlike (pfile, node, start, macro) | |
397 | cpp_reader *pfile; | |
398 | cpp_hashnode *node; | |
399 | const uchar *start; | |
400 | struct fun_macro *macro; | |
401 | { | |
402 | unsigned int n = node->value.macro->paramc + 1; | |
403 | ||
404 | if (macro->buff) | |
405 | _cpp_release_buff (pfile, macro->buff); | |
406 | macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t)); | |
407 | macro->args = (size_t *) BUFF_FRONT (macro->buff); | |
408 | macro->node = node; | |
fb83e0d6 | 409 | macro->offset = start - pfile->out.base; |
06025647 | 410 | macro->argc = 0; |
06025647 | 411 | } |
412 | ||
413 | /* Save the OFFSET of the start of the next argument to MACRO. */ | |
414 | static void | |
415 | save_argument (macro, offset) | |
416 | struct fun_macro *macro; | |
417 | size_t offset; | |
418 | { | |
419 | macro->argc++; | |
420 | if (macro->argc <= macro->node->value.macro->paramc) | |
421 | macro->args[macro->argc] = offset; | |
422 | } | |
423 | ||
cbe337b6 | 424 | /* Copies the next logical line in the current buffer (starting at |
425 | buffer->cur) to the output buffer. The output is guaranteed to | |
426 | terminate with a NUL character. buffer->cur is updated. | |
198b48a0 | 427 | |
428 | If MACRO is non-NULL, then we are scanning the replacement list of | |
06025647 | 429 | MACRO, and we call save_replacement_text() every time we meet an |
198b48a0 | 430 | argument. */ |
0bb65704 | 431 | static void |
198b48a0 | 432 | scan_out_logical_line (pfile, macro) |
0bb65704 | 433 | cpp_reader *pfile; |
198b48a0 | 434 | cpp_macro *macro; |
0bb65704 | 435 | { |
f15f6c8d | 436 | cpp_context *context; |
437 | const uchar *cur; | |
0bb65704 | 438 | uchar *out; |
06025647 | 439 | struct fun_macro fmacro; |
94fed299 | 440 | unsigned int c, paren_depth = 0, quote; |
bab5e68f | 441 | enum ls lex_state = ls_none; |
ecedd82d | 442 | bool header_ok; |
0bb65704 | 443 | |
06025647 | 444 | fmacro.buff = NULL; |
01628c3c | 445 | |
446 | start_logical_line: | |
94fed299 | 447 | quote = 0; |
ecedd82d | 448 | header_ok = pfile->state.angled_headers; |
cbe337b6 | 449 | CUR (pfile->context) = pfile->buffer->cur; |
450 | RLIMIT (pfile->context) = pfile->buffer->rlimit; | |
01628c3c | 451 | pfile->out.cur = pfile->out.base; |
452 | pfile->out.first_line = pfile->line; | |
f15f6c8d | 453 | new_context: |
454 | context = pfile->context; | |
455 | cur = CUR (context); | |
a854276a | 456 | check_output_buffer (pfile, RLIMIT (context) - cur); |
fb83e0d6 | 457 | out = pfile->out.cur; |
0bb65704 | 458 | |
459 | for (;;) | |
460 | { | |
461 | c = *cur++; | |
462 | *out++ = c; | |
463 | ||
bab5e68f | 464 | /* Whitespace should "continue" out of the switch, |
465 | non-whitespace should "break" out of it. */ | |
0bb65704 | 466 | switch (c) |
467 | { | |
bab5e68f | 468 | case ' ': |
469 | case '\t': | |
470 | case '\f': | |
471 | case '\v': | |
472 | continue; | |
473 | ||
0bb65704 | 474 | case '\0': |
a854276a | 475 | if (cur - 1 != RLIMIT (context)) |
bab5e68f | 476 | continue; |
f15f6c8d | 477 | |
478 | /* If this is a macro's expansion, pop it. */ | |
479 | if (context->prev) | |
480 | { | |
fb83e0d6 | 481 | pfile->out.cur = out - 1; |
f15f6c8d | 482 | _cpp_pop_context (pfile); |
483 | goto new_context; | |
484 | } | |
485 | ||
486 | /* Premature end of file. Fake a new line. */ | |
0bb65704 | 487 | cur--; |
a854276a | 488 | if (!pfile->buffer->from_stage3) |
0bb65704 | 489 | cpp_error (pfile, DL_PEDWARN, "no newline at end of file"); |
490 | pfile->line++; | |
06025647 | 491 | goto done; |
a854276a | 492 | |
493 | case '\r': case '\n': | |
494 | cur = handle_newline (pfile, cur - 1); | |
9c343313 | 495 | if ((lex_state == ls_fun_open || lex_state == ls_fun_close) |
496 | && !pfile->state.in_directive) | |
06025647 | 497 | { |
e3905c41 | 498 | /* Newlines in arguments become a space, but we don't |
499 | clear any in-progress quote. */ | |
9c343313 | 500 | if (lex_state == ls_fun_close) |
501 | out[-1] = ' '; | |
06025647 | 502 | continue; |
503 | } | |
504 | goto done; | |
0bb65704 | 505 | |
bab5e68f | 506 | case '<': |
ecedd82d | 507 | if (header_ok) |
bab5e68f | 508 | quote = '>'; |
509 | break; | |
510 | case '>': | |
94fed299 | 511 | if (c == quote) |
ecedd82d | 512 | quote = 0; |
bab5e68f | 513 | break; |
514 | ||
0bb65704 | 515 | case '"': |
516 | case '\'': | |
517 | if (c == quote) | |
518 | quote = 0; | |
519 | else if (!quote) | |
520 | quote = c; | |
521 | break; | |
522 | ||
523 | case '\\': | |
524 | if (is_vspace (*cur)) | |
bab5e68f | 525 | { |
526 | out--; | |
527 | cur = skip_escaped_newlines (pfile, cur - 1); | |
528 | continue; | |
529 | } | |
0bb65704 | 530 | else |
531 | { | |
532 | /* Skip escaped quotes here, it's easier than above, but | |
533 | take care to first skip escaped newlines. */ | |
534 | cur = skip_escaped_newlines (pfile, cur); | |
535 | if (*cur == '\\' || *cur == '"' || *cur == '\'') | |
536 | *out++ = *cur++; | |
537 | } | |
538 | break; | |
539 | ||
540 | case '/': | |
541 | /* Traditional CPP does not recognize comments within | |
542 | literals. */ | |
543 | if (!quote) | |
544 | { | |
545 | cur = skip_escaped_newlines (pfile, cur); | |
546 | if (*cur == '*') | |
c455fac3 | 547 | { |
9a468a1b | 548 | pfile->out.cur = out; |
549 | cur = copy_comment (pfile, cur, macro != 0); | |
550 | out = pfile->out.cur; | |
bab5e68f | 551 | continue; |
c455fac3 | 552 | } |
0bb65704 | 553 | } |
554 | break; | |
555 | ||
556 | case '_': | |
557 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
558 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
559 | case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': | |
560 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': | |
561 | case 'y': case 'z': | |
562 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
563 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': | |
564 | case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': | |
565 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
566 | case 'Y': case 'Z': | |
bab5e68f | 567 | if (!pfile->state.skipping && (quote == 0 || macro)) |
198b48a0 | 568 | { |
569 | cpp_hashnode *node; | |
bab5e68f | 570 | uchar *out_start = out - 1; |
198b48a0 | 571 | |
bab5e68f | 572 | pfile->out.cur = out_start; |
198b48a0 | 573 | node = lex_identifier (pfile, cur - 1); |
bab5e68f | 574 | out = pfile->out.cur; |
575 | cur = CUR (context); | |
198b48a0 | 576 | |
06025647 | 577 | if (node->type == NT_MACRO |
bab5e68f | 578 | /* Should we expand for ls_answer? */ |
9c343313 | 579 | && (lex_state == ls_none || lex_state == ls_fun_open) |
f1bb1590 | 580 | && !pfile->state.prevent_expansion) |
198b48a0 | 581 | { |
9c343313 | 582 | /* Macros invalidate MI optimization. */ |
583 | pfile->mi_valid = false; | |
584 | if (! (node->flags & NODE_BUILTIN) | |
585 | && node->value.macro->fun_like) | |
bab5e68f | 586 | { |
587 | maybe_start_funlike (pfile, node, out_start, &fmacro); | |
9c343313 | 588 | lex_state = ls_fun_open; |
48315635 | 589 | fmacro.line = pfile->line; |
bab5e68f | 590 | continue; |
591 | } | |
f1bb1590 | 592 | else if (!recursive_macro (pfile, node)) |
06025647 | 593 | { |
594 | /* Remove the object-like macro's name from the | |
595 | output, and push its replacement text. */ | |
bab5e68f | 596 | pfile->out.cur = out_start; |
06025647 | 597 | push_replacement_text (pfile, node); |
9c343313 | 598 | lex_state = ls_none; |
06025647 | 599 | goto new_context; |
600 | } | |
198b48a0 | 601 | } |
805e22b2 | 602 | else if (macro && (node->flags & NODE_MACRO_ARG) != 0) |
198b48a0 | 603 | { |
06025647 | 604 | /* Found a parameter in the replacement text of a |
605 | #define. Remove its name from the output. */ | |
17df7fec | 606 | pfile->out.cur = out_start; |
805e22b2 | 607 | save_replacement_text (pfile, macro, node->value.arg_index); |
17df7fec | 608 | out = pfile->out.base; |
198b48a0 | 609 | } |
bab5e68f | 610 | else if (lex_state == ls_hash) |
611 | { | |
612 | lex_state = ls_predicate; | |
613 | continue; | |
614 | } | |
615 | else if (pfile->state.in_expression | |
616 | && node == pfile->spec_nodes.n_defined) | |
617 | { | |
618 | lex_state = ls_defined; | |
619 | continue; | |
620 | } | |
198b48a0 | 621 | } |
0bb65704 | 622 | break; |
623 | ||
06025647 | 624 | case '(': |
625 | if (quote == 0) | |
626 | { | |
627 | paren_depth++; | |
9c343313 | 628 | if (lex_state == ls_fun_open) |
06025647 | 629 | { |
f1bb1590 | 630 | if (recursive_macro (pfile, fmacro.node)) |
631 | lex_state = ls_none; | |
632 | else | |
633 | { | |
634 | lex_state = ls_fun_close; | |
635 | paren_depth = 1; | |
636 | out = pfile->out.base + fmacro.offset; | |
637 | fmacro.args[0] = fmacro.offset; | |
638 | } | |
06025647 | 639 | } |
bab5e68f | 640 | else if (lex_state == ls_predicate) |
641 | lex_state = ls_answer; | |
642 | else if (lex_state == ls_defined) | |
643 | lex_state = ls_defined_close; | |
06025647 | 644 | } |
645 | break; | |
646 | ||
647 | case ',': | |
9c343313 | 648 | if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1) |
fb83e0d6 | 649 | save_argument (&fmacro, out - pfile->out.base); |
06025647 | 650 | break; |
651 | ||
652 | case ')': | |
653 | if (quote == 0) | |
654 | { | |
655 | paren_depth--; | |
9c343313 | 656 | if (lex_state == ls_fun_close && paren_depth == 0) |
06025647 | 657 | { |
658 | cpp_macro *m = fmacro.node->value.macro; | |
659 | ||
71a7c282 | 660 | m->used = 1; |
9c343313 | 661 | lex_state = ls_none; |
fb83e0d6 | 662 | save_argument (&fmacro, out - pfile->out.base); |
06025647 | 663 | |
3396dc80 | 664 | /* A single zero-length argument is no argument. */ |
665 | if (fmacro.argc == 1 | |
666 | && m->paramc == 0 | |
bab5e68f | 667 | && out == pfile->out.base + fmacro.offset + 1) |
3396dc80 | 668 | fmacro.argc = 0; |
06025647 | 669 | |
670 | if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc)) | |
671 | { | |
672 | /* Remove the macro's invocation from the | |
673 | output, and push its replacement text. */ | |
fb83e0d6 | 674 | pfile->out.cur = (pfile->out.base |
06025647 | 675 | + fmacro.offset); |
676 | CUR (context) = cur; | |
677 | replace_args_and_push (pfile, &fmacro); | |
678 | goto new_context; | |
679 | } | |
680 | } | |
bab5e68f | 681 | else if (lex_state == ls_answer || lex_state == ls_defined_close) |
682 | lex_state = ls_none; | |
06025647 | 683 | } |
684 | break; | |
685 | ||
fb83e0d6 | 686 | case '#': |
60757a55 | 687 | if (out - 1 == pfile->out.base |
688 | /* A '#' from a macro doesn't start a directive. */ | |
689 | && !pfile->context->prev | |
690 | && !pfile->state.in_directive) | |
fb83e0d6 | 691 | { |
f1bb1590 | 692 | /* A directive. With the way _cpp_handle_directive |
693 | currently works, we only want to call it if either we | |
694 | know the directive is OK, or we want it to fail and | |
695 | be removed from the output. If we want it to be | |
696 | passed through (the assembler case) then we must not | |
697 | call _cpp_handle_directive. */ | |
698 | pfile->out.cur = out; | |
699 | cur = skip_whitespace (pfile, cur, true /* skip_comments */); | |
700 | out = pfile->out.cur; | |
701 | ||
702 | if (is_vspace (*cur)) | |
6d5a10ca | 703 | { |
704 | /* Null directive. Ignore it and don't invalidate | |
705 | the MI optimization. */ | |
706 | out = pfile->out.base; | |
707 | continue; | |
708 | } | |
f1bb1590 | 709 | else |
710 | { | |
711 | bool do_it = false; | |
712 | ||
60757a55 | 713 | if (is_numstart (*cur) |
714 | && CPP_OPTION (pfile, lang) != CLK_ASM) | |
f1bb1590 | 715 | do_it = true; |
716 | else if (is_idstart (*cur)) | |
717 | /* Check whether we know this directive, but don't | |
718 | advance. */ | |
805e22b2 | 719 | do_it = lex_identifier (pfile, cur)->is_directive; |
f1bb1590 | 720 | |
721 | if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM) | |
722 | { | |
723 | /* This is a kludge. We want to have the ISO | |
724 | preprocessor lex the next token. */ | |
725 | pfile->buffer->cur = cur; | |
726 | _cpp_handle_directive (pfile, false /* indented */); | |
cbe337b6 | 727 | /* #include changes pfile->buffer so we need to |
728 | update the limits of the current context. */ | |
f1bb1590 | 729 | goto start_logical_line; |
730 | } | |
731 | } | |
fb83e0d6 | 732 | } |
f1bb1590 | 733 | |
bab5e68f | 734 | if (pfile->state.in_expression) |
735 | { | |
736 | lex_state = ls_hash; | |
737 | continue; | |
738 | } | |
fb83e0d6 | 739 | break; |
740 | ||
0bb65704 | 741 | default: |
742 | break; | |
743 | } | |
bab5e68f | 744 | |
ecedd82d | 745 | /* Non-whitespace disables MI optimization and stops treating |
746 | '<' as a quote in #include. */ | |
747 | header_ok = false; | |
6d5a10ca | 748 | if (!pfile->state.in_directive) |
749 | pfile->mi_valid = false; | |
750 | ||
bab5e68f | 751 | if (lex_state == ls_none) |
752 | continue; | |
753 | ||
754 | /* Some of these transitions of state are syntax errors. The | |
755 | ISO preprocessor will issue errors later. */ | |
9c343313 | 756 | if (lex_state == ls_fun_open) |
757 | /* Missing '('. */ | |
758 | lex_state = ls_none; | |
bab5e68f | 759 | else if (lex_state == ls_hash |
760 | || lex_state == ls_predicate | |
761 | || lex_state == ls_defined) | |
762 | lex_state = ls_none; | |
763 | ||
764 | /* ls_answer and ls_defined_close keep going until ')'. */ | |
0bb65704 | 765 | } |
06025647 | 766 | |
767 | done: | |
768 | out[-1] = '\0'; | |
cbe337b6 | 769 | pfile->buffer->cur = cur; |
fb83e0d6 | 770 | pfile->out.cur = out - 1; |
06025647 | 771 | if (fmacro.buff) |
772 | _cpp_release_buff (pfile, fmacro.buff); | |
bab5e68f | 773 | |
9c343313 | 774 | if (lex_state == ls_fun_close) |
48315635 | 775 | cpp_error_with_line (pfile, DL_ERROR, fmacro.line, 0, |
776 | "unterminated argument list invoking macro \"%s\"", | |
777 | NODE_NAME (fmacro.node)); | |
0bb65704 | 778 | } |
f15f6c8d | 779 | |
780 | /* Push a context holding the replacement text of the macro NODE on | |
06025647 | 781 | the context stack. NODE is either object-like, or a function-like |
782 | macro with no arguments. */ | |
f15f6c8d | 783 | static void |
784 | push_replacement_text (pfile, node) | |
785 | cpp_reader *pfile; | |
786 | cpp_hashnode *node; | |
787 | { | |
9c343313 | 788 | size_t len; |
789 | const uchar *text; | |
790 | ||
791 | if (node->flags & NODE_BUILTIN) | |
792 | { | |
793 | text = _cpp_builtin_macro_text (pfile, node); | |
794 | len = ustrlen (text); | |
795 | } | |
796 | else | |
797 | { | |
798 | cpp_macro *macro = node->value.macro; | |
71a7c282 | 799 | macro->used = 1; |
9c343313 | 800 | text = macro->exp.text; |
801 | len = macro->count; | |
802 | } | |
f15f6c8d | 803 | |
9c343313 | 804 | _cpp_push_text_context (pfile, node, text, len); |
06025647 | 805 | } |
806 | ||
01628c3c | 807 | /* Returns TRUE if traditional macro recursion is detected. */ |
808 | static bool | |
809 | recursive_macro (pfile, node) | |
810 | cpp_reader *pfile; | |
811 | cpp_hashnode *node; | |
812 | { | |
1ae6ed16 | 813 | bool recursing = !!(node->flags & NODE_DISABLED); |
01628c3c | 814 | |
815 | /* Object-like macros that are already expanding are necessarily | |
816 | recursive. | |
817 | ||
818 | However, it is possible to have traditional function-like macros | |
819 | that are not infinitely recursive but recurse to any given depth. | |
820 | Further, it is easy to construct examples that get ever longer | |
821 | until the point they stop recursing. So there is no easy way to | |
822 | detect true recursion; instead we assume any expansion more than | |
823 | 20 deep since the first invocation of this macro must be | |
824 | recursing. */ | |
825 | if (recursing && node->value.macro->fun_like) | |
826 | { | |
827 | size_t depth = 0; | |
828 | cpp_context *context = pfile->context; | |
829 | ||
830 | do | |
831 | { | |
832 | depth++; | |
833 | if (context->macro == node && depth > 20) | |
834 | break; | |
835 | context = context->prev; | |
836 | } | |
837 | while (context); | |
838 | recursing = context != NULL; | |
839 | } | |
840 | ||
841 | if (recursing) | |
842 | cpp_error (pfile, DL_ERROR, | |
843 | "detected recursion whilst expanding macro \"%s\"", | |
844 | NODE_NAME (node)); | |
845 | ||
846 | return recursing; | |
847 | } | |
848 | ||
9c343313 | 849 | /* Return the length of the replacement text of a function-like or |
850 | object-like non-builtin macro. */ | |
851 | size_t | |
852 | _cpp_replacement_text_len (macro) | |
853 | const cpp_macro *macro; | |
854 | { | |
855 | size_t len; | |
856 | ||
857 | if (macro->fun_like) | |
858 | { | |
859 | const uchar *exp; | |
860 | ||
2620c4b2 | 861 | len = 0; |
9c343313 | 862 | for (exp = macro->exp.text;;) |
863 | { | |
864 | struct block *b = (struct block *) exp; | |
865 | ||
866 | len += b->text_len; | |
867 | if (b->arg_index == 0) | |
868 | break; | |
869 | len += NODE_LEN (macro->params[b->arg_index - 1]); | |
870 | exp += BLOCK_LEN (b->text_len); | |
871 | } | |
872 | } | |
873 | else | |
874 | len = macro->count; | |
875 | ||
876 | return len; | |
877 | } | |
878 | ||
879 | /* Copy the replacement text of MACRO to DEST, which must be of | |
880 | sufficient size. It is not NUL-terminated. The next character is | |
881 | returned. */ | |
882 | uchar * | |
883 | _cpp_copy_replacement_text (macro, dest) | |
884 | const cpp_macro *macro; | |
885 | uchar *dest; | |
886 | { | |
887 | if (macro->fun_like) | |
888 | { | |
889 | const uchar *exp; | |
890 | ||
891 | for (exp = macro->exp.text;;) | |
892 | { | |
893 | struct block *b = (struct block *) exp; | |
894 | cpp_hashnode *param; | |
895 | ||
896 | memcpy (dest, b->text, b->text_len); | |
897 | dest += b->text_len; | |
898 | if (b->arg_index == 0) | |
899 | break; | |
900 | param = macro->params[b->arg_index - 1]; | |
901 | memcpy (dest, NODE_NAME (param), NODE_LEN (param)); | |
902 | dest += NODE_LEN (param); | |
903 | exp += BLOCK_LEN (b->text_len); | |
904 | } | |
905 | } | |
906 | else | |
907 | { | |
908 | memcpy (dest, macro->exp.text, macro->count); | |
909 | dest += macro->count; | |
910 | } | |
911 | ||
912 | return dest; | |
913 | } | |
914 | ||
06025647 | 915 | /* Push a context holding the replacement text of the macro NODE on |
916 | the context stack. NODE is either object-like, or a function-like | |
917 | macro with no arguments. */ | |
918 | static void | |
919 | replace_args_and_push (pfile, fmacro) | |
920 | cpp_reader *pfile; | |
921 | struct fun_macro *fmacro; | |
922 | { | |
923 | cpp_macro *macro = fmacro->node->value.macro; | |
924 | ||
925 | if (macro->paramc == 0) | |
926 | push_replacement_text (pfile, fmacro->node); | |
927 | else | |
928 | { | |
929 | const uchar *exp; | |
930 | uchar *p; | |
931 | _cpp_buff *buff; | |
932 | size_t len = 0; | |
933 | ||
934 | /* Calculate the length of the argument-replaced text. */ | |
935 | for (exp = macro->exp.text;;) | |
936 | { | |
937 | struct block *b = (struct block *) exp; | |
938 | ||
939 | len += b->text_len; | |
940 | if (b->arg_index == 0) | |
941 | break; | |
942 | len += (fmacro->args[b->arg_index] | |
943 | - fmacro->args[b->arg_index - 1] - 1); | |
944 | exp += BLOCK_LEN (b->text_len); | |
945 | } | |
946 | ||
947 | /* Allocate room for the expansion plus NUL. */ | |
948 | buff = _cpp_get_buff (pfile, len + 1); | |
949 | ||
950 | /* Copy the expansion and replace arguments. */ | |
951 | p = BUFF_FRONT (buff); | |
952 | for (exp = macro->exp.text;;) | |
953 | { | |
954 | struct block *b = (struct block *) exp; | |
955 | size_t arglen; | |
956 | ||
957 | memcpy (p, b->text, b->text_len); | |
958 | p += b->text_len; | |
959 | if (b->arg_index == 0) | |
960 | break; | |
961 | arglen = (fmacro->args[b->arg_index] | |
962 | - fmacro->args[b->arg_index - 1] - 1); | |
fb83e0d6 | 963 | memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1], |
06025647 | 964 | arglen); |
965 | p += arglen; | |
966 | exp += BLOCK_LEN (b->text_len); | |
967 | } | |
968 | ||
969 | /* NUL-terminate. */ | |
970 | *p = '\0'; | |
971 | _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len); | |
972 | ||
973 | /* So we free buffer allocation when macro is left. */ | |
974 | pfile->context->buff = buff; | |
975 | } | |
f15f6c8d | 976 | } |
977 | ||
198b48a0 | 978 | /* Read and record the parameters, if any, of a function-like macro |
fb83e0d6 | 979 | definition. Destroys pfile->out.cur. |
198b48a0 | 980 | |
981 | Returns true on success, false on failure (syntax error or a | |
982 | duplicate parameter). On success, CUR (pfile->context) is just | |
983 | past the closing parenthesis. */ | |
984 | static bool | |
985 | scan_parameters (pfile, macro) | |
986 | cpp_reader *pfile; | |
987 | cpp_macro *macro; | |
988 | { | |
989 | const uchar *cur = CUR (pfile->context) + 1; | |
990 | bool ok; | |
991 | ||
992 | for (;;) | |
993 | { | |
9a468a1b | 994 | cur = skip_whitespace (pfile, cur, true /* skip_comments */); |
198b48a0 | 995 | |
06025647 | 996 | if (is_idstart (*cur)) |
198b48a0 | 997 | { |
998 | ok = false; | |
999 | if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur))) | |
1000 | break; | |
9a468a1b | 1001 | cur = skip_whitespace (pfile, CUR (pfile->context), |
1002 | true /* skip_comments */); | |
198b48a0 | 1003 | if (*cur == ',') |
1004 | { | |
1005 | cur++; | |
1006 | continue; | |
1007 | } | |
1008 | ok = (*cur == ')'); | |
1009 | break; | |
1010 | } | |
1011 | ||
1012 | ok = (*cur == ')' && macro->paramc == 0); | |
1013 | break; | |
1014 | } | |
1015 | ||
1016 | CUR (pfile->context) = cur + (*cur == ')'); | |
1017 | ||
1018 | return ok; | |
1019 | } | |
1020 | ||
fb83e0d6 | 1021 | /* Save the text from pfile->out.base to pfile->out.cur as |
198b48a0 | 1022 | the replacement text for the current macro, followed by argument |
1023 | ARG_INDEX, with zero indicating the end of the replacement | |
1024 | text. */ | |
1025 | static void | |
1026 | save_replacement_text (pfile, macro, arg_index) | |
1027 | cpp_reader *pfile; | |
1028 | cpp_macro *macro; | |
1029 | unsigned int arg_index; | |
1030 | { | |
fb83e0d6 | 1031 | size_t len = pfile->out.cur - pfile->out.base; |
198b48a0 | 1032 | uchar *exp; |
1033 | ||
1034 | if (macro->paramc == 0) | |
1035 | { | |
1036 | /* Object-like and function-like macros without parameters | |
1037 | simply store their NUL-terminated replacement text. */ | |
1038 | exp = _cpp_unaligned_alloc (pfile, len + 1); | |
fb83e0d6 | 1039 | memcpy (exp, pfile->out.base, len); |
198b48a0 | 1040 | exp[len] = '\0'; |
1041 | macro->exp.text = exp; | |
1042 | macro->count = len; | |
1043 | } | |
1044 | else | |
1045 | { | |
1046 | /* Store the text's length (unsigned int), the argument index | |
1047 | (unsigned short, base 1) and then the text. */ | |
1048 | size_t blen = BLOCK_LEN (len); | |
1049 | struct block *block; | |
1050 | ||
1051 | if (macro->count + blen > BUFF_ROOM (pfile->a_buff)) | |
1052 | _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen); | |
1053 | ||
1054 | exp = BUFF_FRONT (pfile->a_buff); | |
1055 | block = (struct block *) (exp + macro->count); | |
1056 | macro->exp.text = exp; | |
1057 | ||
1058 | /* Write out the block information. */ | |
1059 | block->text_len = len; | |
1060 | block->arg_index = arg_index; | |
fb83e0d6 | 1061 | memcpy (block->text, pfile->out.base, len); |
198b48a0 | 1062 | |
1063 | /* Lex the rest into the start of the output buffer. */ | |
fb83e0d6 | 1064 | pfile->out.cur = pfile->out.base; |
198b48a0 | 1065 | |
06025647 | 1066 | macro->count += blen; |
3396dc80 | 1067 | |
1068 | /* If we've finished, commit the memory. */ | |
1069 | if (arg_index == 0) | |
1070 | BUFF_FRONT (pfile->a_buff) += macro->count; | |
198b48a0 | 1071 | } |
1072 | } | |
1073 | ||
1074 | /* Analyze and save the replacement text of a macro. Returns true on | |
1075 | success. */ | |
f15f6c8d | 1076 | bool |
1077 | _cpp_create_trad_definition (pfile, macro) | |
1078 | cpp_reader *pfile; | |
1079 | cpp_macro *macro; | |
1080 | { | |
198b48a0 | 1081 | const uchar *cur; |
1082 | uchar *limit; | |
cbe337b6 | 1083 | cpp_context *context = pfile->context; |
f15f6c8d | 1084 | |
cbe337b6 | 1085 | /* The context has not been set up for command line defines, and CUR |
1086 | has not been updated for the macro name for in-file defines. */ | |
1087 | pfile->out.cur = pfile->out.base; | |
1088 | CUR (context) = pfile->buffer->cur; | |
1089 | RLIMIT (context) = pfile->buffer->rlimit; | |
1090 | check_output_buffer (pfile, RLIMIT (context) - CUR (context)); | |
fb83e0d6 | 1091 | |
198b48a0 | 1092 | /* Is this a function-like macro? */ |
cbe337b6 | 1093 | if (* CUR (context) == '(') |
198b48a0 | 1094 | { |
06025647 | 1095 | /* Setting macro to NULL indicates an error occurred, and |
1096 | prevents unnecessary work in scan_out_logical_line. */ | |
198b48a0 | 1097 | if (!scan_parameters (pfile, macro)) |
1098 | macro = NULL; | |
1099 | else | |
1100 | { | |
1101 | /* Success. Commit the parameter array. */ | |
1102 | macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff); | |
1103 | BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc]; | |
1104 | macro->fun_like = 1; | |
1105 | } | |
198b48a0 | 1106 | } |
1107 | ||
06025647 | 1108 | /* Skip leading whitespace in the replacement text. */ |
cbe337b6 | 1109 | pfile->buffer->cur |
1110 | = skip_whitespace (pfile, CUR (context), | |
9a468a1b | 1111 | CPP_OPTION (pfile, discard_comments_in_macro_exp)); |
06025647 | 1112 | |
198b48a0 | 1113 | pfile->state.prevent_expansion++; |
1114 | scan_out_logical_line (pfile, macro); | |
1115 | pfile->state.prevent_expansion--; | |
1116 | ||
1117 | if (!macro) | |
1118 | return false; | |
f15f6c8d | 1119 | |
1120 | /* Skip trailing white space. */ | |
fb83e0d6 | 1121 | cur = pfile->out.base; |
1122 | limit = pfile->out.cur; | |
f15f6c8d | 1123 | while (limit > cur && is_space (limit[-1])) |
1124 | limit--; | |
fb83e0d6 | 1125 | pfile->out.cur = limit; |
198b48a0 | 1126 | save_replacement_text (pfile, macro, 0); |
f15f6c8d | 1127 | |
1128 | return true; | |
1129 | } | |
1130 | ||
3396dc80 | 1131 | /* Copy SRC of length LEN to DEST, but convert all contiguous |
1132 | whitespace to a single space, provided it is not in quotes. The | |
1133 | quote currently in effect is pointed to by PQUOTE, and is updated | |
1134 | by the function. Returns the number of bytes copied. */ | |
1135 | static size_t | |
1136 | canonicalize_text (dest, src, len, pquote) | |
1137 | uchar *dest; | |
1138 | const uchar *src; | |
1139 | size_t len; | |
1140 | uchar *pquote; | |
1141 | { | |
1142 | uchar *orig_dest = dest; | |
1143 | uchar quote = *pquote; | |
1144 | ||
1145 | while (len) | |
1146 | { | |
1147 | if (is_space (*src) && !quote) | |
1148 | { | |
1149 | do | |
1150 | src++, len--; | |
1151 | while (len && is_space (*src)); | |
1152 | *dest++ = ' '; | |
1153 | } | |
1154 | else | |
1155 | { | |
1156 | if (*src == '\'' || *src == '"') | |
1157 | { | |
1158 | if (!quote) | |
1159 | quote = *src; | |
1160 | else if (quote == *src) | |
1161 | quote = 0; | |
1162 | } | |
1163 | *dest++ = *src++, len--; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | *pquote = quote; | |
1168 | return dest - orig_dest; | |
1169 | } | |
1170 | ||
1171 | /* Returns true if MACRO1 and MACRO2 have expansions different other | |
1172 | than in the form of their whitespace. */ | |
1173 | bool | |
1174 | _cpp_expansions_different_trad (macro1, macro2) | |
fd34c678 | 1175 | const cpp_macro *macro1, *macro2; |
3396dc80 | 1176 | { |
1177 | uchar *p1 = xmalloc (macro1->count + macro2->count); | |
1178 | uchar *p2 = p1 + macro1->count; | |
aacf5b8e | 1179 | uchar quote1 = 0, quote2 = 0; |
3396dc80 | 1180 | bool mismatch; |
1181 | size_t len1, len2; | |
1182 | ||
1183 | if (macro1->paramc > 0) | |
1184 | { | |
1185 | const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text; | |
1186 | ||
1187 | mismatch = true; | |
1188 | for (;;) | |
1189 | { | |
1190 | struct block *b1 = (struct block *) exp1; | |
1191 | struct block *b2 = (struct block *) exp2; | |
1192 | ||
1193 | if (b1->arg_index != b2->arg_index) | |
1194 | break; | |
1195 | ||
1196 | len1 = canonicalize_text (p1, b1->text, b1->text_len, "e1); | |
1197 | len2 = canonicalize_text (p2, b2->text, b2->text_len, "e2); | |
1198 | if (len1 != len2 || memcmp (p1, p2, len1)) | |
1199 | break; | |
1200 | if (b1->arg_index == 0) | |
1201 | { | |
1202 | mismatch = false; | |
1203 | break; | |
1204 | } | |
1205 | exp1 += BLOCK_LEN (b1->text_len); | |
1206 | exp2 += BLOCK_LEN (b2->text_len); | |
1207 | } | |
1208 | } | |
1209 | else | |
1210 | { | |
1211 | len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, "e1); | |
1212 | len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, "e2); | |
1213 | mismatch = (len1 != len2 || memcmp (p1, p2, len1)); | |
1214 | } | |
1215 | ||
1216 | free (p1); | |
1217 | return mismatch; | |
1218 | } |