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