]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpptrad.c
* Makefile.in (unstrap, restrap): New targets.
[thirdparty/gcc.git] / gcc / cpptrad.c
CommitLineData
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
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, 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
198b48a0 24/* The replacement text of a function-like macro is stored as a
c455fac3 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
198b48a0 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
c455fac3 31 easily recognizable as it has an argument index of zero. */
198b48a0 32
33struct 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
06025647 43/* Structure holding information about a function-like macro
44 invocation. */
45struct 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
bab5e68f 66/* Lexing state. It is mostly used to prevent macro expansion. */
67enum ls {ls_none = 0, /* Normal state. */
9c343313 68 ls_fun_open, /* When looking for '('. */
69 ls_fun_close, /* When looking for ')'. */
bab5e68f 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
9a468a1b 76/* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c
77 from recognizing comments and directives during its lexing pass. */
0bb65704 78
79static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *));
80static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *,
81 const uchar *));
9a468a1b 82static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *,
83 int));
0bb65704 84static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
9a468a1b 85static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *, int));
198b48a0 86static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *));
0bb65704 87static void check_output_buffer PARAMS ((cpp_reader *, size_t));
f15f6c8d 88static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *));
198b48a0 89static bool scan_parameters PARAMS ((cpp_reader *, cpp_macro *));
01628c3c 90static bool recursive_macro PARAMS ((cpp_reader *, cpp_hashnode *));
198b48a0 91static void save_replacement_text PARAMS ((cpp_reader *, cpp_macro *,
92 unsigned int));
06025647 93static void maybe_start_funlike PARAMS ((cpp_reader *, cpp_hashnode *,
94 const uchar *, struct fun_macro *));
95static void save_argument PARAMS ((struct fun_macro *, size_t));
96static void replace_args_and_push PARAMS ((cpp_reader *, struct fun_macro *));
3396dc80 97static size_t canonicalize_text PARAMS ((uchar *, const uchar *, size_t,
98 uchar *));
0bb65704 99
100/* Ensures we have N bytes' space in the output buffer, and
101 reallocates it if not. */
102static void
103check_output_buffer (pfile, n)
104 cpp_reader *pfile;
105 size_t n;
106{
c455fac3 107 /* We might need two bytes to terminate an unterminated comment, and
9a468a1b 108 one more to terminate the line with a NUL. */
c455fac3 109 n += 2 + 1;
110
fb83e0d6 111 if (n > (size_t) (pfile->out.limit - pfile->out.cur))
0bb65704 112 {
fb83e0d6 113 size_t size = pfile->out.cur - pfile->out.base;
0bb65704 114 size_t new_size = (size + n) * 3 / 2;
115
fb83e0d6 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;
0bb65704 120 }
121}
122
123/* To be called whenever a newline character is encountered in the
9a468a1b 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. */
0bb65704 128static const uchar *
129handle_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,
9a468a1b 141 and returns the new position.
142
143 Warns if a file buffer ends in an escaped newline. */
0bb65704 144static const uchar *
145skip_escaped_newlines (pfile, cur)
146 cpp_reader *pfile;
147 const uchar *cur;
148{
9a468a1b 149 const uchar *orig_cur = cur;
c455fac3 150
9a468a1b 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");
0bb65704 156
157 return cur;
158}
159
9a468a1b 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.
c455fac3 169
170 Returns a pointer to the first character after the comment in the
171 input buffer. */
0bb65704 172static const uchar *
9a468a1b 173copy_comment (pfile, cur, in_define)
0bb65704 174 cpp_reader *pfile;
175 const uchar *cur;
9a468a1b 176 int in_define;
0bb65704 177{
178 unsigned int from_line = pfile->line;
a854276a 179 const uchar *limit = RLIMIT (pfile->context);
c455fac3 180 uchar *out = pfile->out.cur;
0bb65704 181
9a468a1b 182 do
0bb65704 183 {
c455fac3 184 unsigned int c = *cur++;
185 *out++ = c;
ca52ef60 186
187 if (c == '/')
0bb65704 188 {
c455fac3 189 /* An immediate slash does not terminate the comment. */
9a468a1b 190 if (out[-2] == '*' && out - 2 > pfile->out.cur)
c455fac3 191 goto done;
192
ca52ef60 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");
0bb65704 197 }
198 else if (is_vspace (c))
c455fac3 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 }
0bb65704 207 }
9a468a1b 208 while (cur < limit);
0bb65704 209
c455fac3 210 cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment");
211 *out++ = '*';
212 *out++ = '/';
ca52ef60 213
c455fac3 214 done:
9a468a1b 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
0bb65704 235 return cur;
236}
237
9a468a1b 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. */
f15f6c8d 250static const uchar *
9a468a1b 251skip_whitespace (pfile, cur, skip_comments)
f15f6c8d 252 cpp_reader *pfile;
253 const uchar *cur;
9a468a1b 254 int skip_comments;
f15f6c8d 255{
9a468a1b 256 uchar *out = pfile->out.cur;
f15f6c8d 257
258 for (;;)
259 {
9a468a1b 260 unsigned int c = *cur++;
261 *out++ = c;
f15f6c8d 262
9a468a1b 263 if (is_nvspace (c) && c)
f15f6c8d 264 continue;
265
9c343313 266 if (!c && cur - 1 != RLIMIT (pfile->context))
9a468a1b 267 continue;
f15f6c8d 268
9a468a1b 269 if (*cur == '/' && skip_comments)
f15f6c8d 270 {
9a468a1b 271 const uchar *tmp = skip_escaped_newlines (pfile, cur);
f15f6c8d 272 if (*tmp == '*')
273 {
9a468a1b 274 pfile->out.cur = out;
275 cur = copy_comment (pfile, tmp, false /* in_define */);
276 out = pfile->out.cur;
f15f6c8d 277 continue;
278 }
279 }
280
9a468a1b 281 out--;
282 if (c == '\\' && is_vspace (*cur))
283 {
284 cur = skip_escaped_newlines (pfile, cur);
285 continue;
286 }
287
f15f6c8d 288 break;
289 }
290
9a468a1b 291 pfile->out.cur = out;
292 return cur - 1;
f15f6c8d 293}
294
0bb65704 295/* Lexes and outputs an identifier starting at CUR, which is assumed
296 to point to a valid first character of an identifier. Returns
fb83e0d6 297 the hashnode, and updates out.cur. */
0bb65704 298static cpp_hashnode *
299lex_identifier (pfile, cur)
300 cpp_reader *pfile;
301 const uchar *cur;
302{
303 size_t len;
fb83e0d6 304 uchar *out = pfile->out.cur;
f15f6c8d 305 cpp_hashnode *result;
0bb65704 306
307 do
308 {
309 do
310 *out++ = *cur++;
06025647 311 while (is_numchar (*cur));
0bb65704 312 cur = skip_escaped_newlines (pfile, cur);
313 }
06025647 314 while (is_numchar (*cur));
0bb65704 315
a854276a 316 CUR (pfile->context) = cur;
fb83e0d6 317 len = out - pfile->out.cur;
318 result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
f15f6c8d 319 len, HT_ALLOC);
fb83e0d6 320 pfile->out.cur = out;
f15f6c8d 321 return result;
322}
323
0bb65704 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(). */
327void
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
bab5e68f 335 pfile->overlaid_buffer = buffer;
0bb65704 336 buffer->saved_cur = buffer->cur;
337 buffer->saved_rlimit = buffer->rlimit;
0bb65704 338
339 buffer->cur = start;
0bb65704 340 buffer->rlimit = start + len;
fb83e0d6 341
342 pfile->saved_line = pfile->line;
0bb65704 343}
344
345/* Restores a buffer overlaid by _cpp_overlay_buffer(). */
fb83e0d6 346void
347_cpp_remove_overlay (pfile)
0bb65704 348 cpp_reader *pfile;
349{
bab5e68f 350 cpp_buffer *buffer = pfile->overlaid_buffer;
0bb65704 351
352 buffer->cur = buffer->saved_cur;
353 buffer->rlimit = buffer->saved_rlimit;
fb83e0d6 354
355 pfile->line = pfile->saved_line;
0bb65704 356}
357
358/* Reads a logical line into the output buffer. Returns TRUE if there
359 is more text left in the buffer. */
360bool
fb83e0d6 361_cpp_read_logical_line_trad (pfile)
0bb65704 362 cpp_reader *pfile;
363{
01628c3c 364 cpp_buffer *buffer = pfile->buffer;
0bb65704 365
01628c3c 366 do
0bb65704 367 {
01628c3c 368 if (buffer->cur == buffer->rlimit)
0bb65704 369 {
01628c3c 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);
bab5e68f 377 buffer = pfile->buffer;
01628c3c 378 }
379
380 if (stop)
381 return false;
0bb65704 382 }
383
01628c3c 384 CUR (pfile->context) = buffer->cur;
385 RLIMIT (pfile->context) = buffer->rlimit;
386 scan_out_logical_line (pfile, NULL);
bab5e68f 387 buffer = pfile->buffer;
01628c3c 388 buffer->cur = CUR (pfile->context);
0bb65704 389 }
01628c3c 390 while (pfile->state.skipping);
a854276a 391
0bb65704 392 return true;
393}
394
06025647 395/* Set up state for finding the opening '(' of a function-like
396 macro. */
397static void
398maybe_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;
fb83e0d6 411 macro->offset = start - pfile->out.base;
06025647 412 macro->argc = 0;
06025647 413}
414
415/* Save the OFFSET of the start of the next argument to MACRO. */
416static void
417save_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
0bb65704 426/* Copies the next logical line in the current buffer to the output
427 buffer. The output is guaranteed to terminate with a NUL
198b48a0 428 character.
429
430 If MACRO is non-NULL, then we are scanning the replacement list of
06025647 431 MACRO, and we call save_replacement_text() every time we meet an
198b48a0 432 argument. */
0bb65704 433static void
198b48a0 434scan_out_logical_line (pfile, macro)
0bb65704 435 cpp_reader *pfile;
198b48a0 436 cpp_macro *macro;
0bb65704 437{
f15f6c8d 438 cpp_context *context;
439 const uchar *cur;
0bb65704 440 uchar *out;
06025647 441 struct fun_macro fmacro;
bab5e68f 442 unsigned int c, paren_depth = 0, quote = 0;
443 enum ls lex_state = ls_none;
0bb65704 444
06025647 445 fmacro.buff = NULL;
01628c3c 446
447 start_logical_line:
448 pfile->out.cur = pfile->out.base;
449 pfile->out.first_line = pfile->line;
f15f6c8d 450 new_context:
451 context = pfile->context;
452 cur = CUR (context);
a854276a 453 check_output_buffer (pfile, RLIMIT (context) - cur);
fb83e0d6 454 out = pfile->out.cur;
0bb65704 455
456 for (;;)
457 {
458 c = *cur++;
459 *out++ = c;
460
bab5e68f 461 /* Whitespace should "continue" out of the switch,
462 non-whitespace should "break" out of it. */
0bb65704 463 switch (c)
464 {
bab5e68f 465 case ' ':
466 case '\t':
467 case '\f':
468 case '\v':
469 continue;
470
0bb65704 471 case '\0':
a854276a 472 if (cur - 1 != RLIMIT (context))
bab5e68f 473 continue;
f15f6c8d 474
475 /* If this is a macro's expansion, pop it. */
476 if (context->prev)
477 {
fb83e0d6 478 pfile->out.cur = out - 1;
f15f6c8d 479 _cpp_pop_context (pfile);
480 goto new_context;
481 }
482
483 /* Premature end of file. Fake a new line. */
0bb65704 484 cur--;
a854276a 485 if (!pfile->buffer->from_stage3)
0bb65704 486 cpp_error (pfile, DL_PEDWARN, "no newline at end of file");
487 pfile->line++;
06025647 488 goto done;
a854276a 489
490 case '\r': case '\n':
491 cur = handle_newline (pfile, cur - 1);
9c343313 492 if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
493 && !pfile->state.in_directive)
06025647 494 {
495 /* Newlines in arguments become a space. */
9c343313 496 if (lex_state == ls_fun_close)
497 out[-1] = ' ';
06025647 498 continue;
499 }
500 goto done;
0bb65704 501
bab5e68f 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
0bb65704 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))
bab5e68f 524 {
525 out--;
526 cur = skip_escaped_newlines (pfile, cur - 1);
527 continue;
528 }
0bb65704 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 == '*')
c455fac3 546 {
9a468a1b 547 pfile->out.cur = out;
548 cur = copy_comment (pfile, cur, macro != 0);
549 out = pfile->out.cur;
bab5e68f 550 continue;
c455fac3 551 }
0bb65704 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':
bab5e68f 566 if (!pfile->state.skipping && (quote == 0 || macro))
198b48a0 567 {
568 cpp_hashnode *node;
bab5e68f 569 uchar *out_start = out - 1;
198b48a0 570
bab5e68f 571 pfile->out.cur = out_start;
198b48a0 572 node = lex_identifier (pfile, cur - 1);
bab5e68f 573 out = pfile->out.cur;
574 cur = CUR (context);
198b48a0 575
06025647 576 if (node->type == NT_MACRO
bab5e68f 577 /* Should we expand for ls_answer? */
9c343313 578 && (lex_state == ls_none || lex_state == ls_fun_open)
01628c3c 579 && !pfile->state.prevent_expansion
580 && !recursive_macro (pfile, node))
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;
bab5e68f 589 continue;
590 }
06025647 591 else
592 {
593 /* Remove the object-like macro's name from the
594 output, and push its replacement text. */
bab5e68f 595 pfile->out.cur = out_start;
06025647 596 push_replacement_text (pfile, node);
9c343313 597 lex_state = ls_none;
06025647 598 goto new_context;
599 }
198b48a0 600 }
601 else if (macro && node->arg_index)
602 {
06025647 603 /* Found a parameter in the replacement text of a
604 #define. Remove its name from the output. */
bab5e68f 605 out = pfile->out.cur = out_start;
198b48a0 606 save_replacement_text (pfile, macro, node->arg_index);
607 }
bab5e68f 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 }
198b48a0 619 }
0bb65704 620 break;
621
06025647 622 case '(':
623 if (quote == 0)
624 {
625 paren_depth++;
9c343313 626 if (lex_state == ls_fun_open)
06025647 627 {
9c343313 628 lex_state = ls_fun_close;
bab5e68f 629 paren_depth = 1;
630 out = pfile->out.base + fmacro.offset;
631 fmacro.args[0] = fmacro.offset;
06025647 632 }
bab5e68f 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;
06025647 637 }
638 break;
639
640 case ',':
9c343313 641 if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
fb83e0d6 642 save_argument (&fmacro, out - pfile->out.base);
06025647 643 break;
644
645 case ')':
646 if (quote == 0)
647 {
648 paren_depth--;
9c343313 649 if (lex_state == ls_fun_close && paren_depth == 0)
06025647 650 {
651 cpp_macro *m = fmacro.node->value.macro;
652
9c343313 653 lex_state = ls_none;
fb83e0d6 654 save_argument (&fmacro, out - pfile->out.base);
06025647 655
3396dc80 656 /* A single zero-length argument is no argument. */
657 if (fmacro.argc == 1
658 && m->paramc == 0
bab5e68f 659 && out == pfile->out.base + fmacro.offset + 1)
3396dc80 660 fmacro.argc = 0;
06025647 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. */
fb83e0d6 666 pfile->out.cur = (pfile->out.base
06025647 667 + fmacro.offset);
668 CUR (context) = cur;
669 replace_args_and_push (pfile, &fmacro);
670 goto new_context;
671 }
672 }
bab5e68f 673 else if (lex_state == ls_answer || lex_state == ls_defined_close)
674 lex_state = ls_none;
06025647 675 }
676 break;
677
fb83e0d6 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 */))
01628c3c 686 goto start_logical_line;
fb83e0d6 687 }
bab5e68f 688 if (pfile->state.in_expression)
689 {
690 lex_state = ls_hash;
691 continue;
692 }
fb83e0d6 693 break;
694
0bb65704 695 default:
696 break;
697 }
bab5e68f 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. */
9c343313 704 if (lex_state == ls_fun_open)
705 /* Missing '('. */
706 lex_state = ls_none;
bab5e68f 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 ')'. */
0bb65704 713 }
06025647 714
715 done:
716 out[-1] = '\0';
717 CUR (context) = cur;
fb83e0d6 718 pfile->out.cur = out - 1;
06025647 719 if (fmacro.buff)
720 _cpp_release_buff (pfile, fmacro.buff);
bab5e68f 721
9c343313 722 if (lex_state == ls_fun_close)
bab5e68f 723 cpp_error (pfile, DL_ERROR,
724 "unterminated argument list invoking macro \"%s\"",
725 NODE_NAME (fmacro.node));
0bb65704 726}
f15f6c8d 727
728/* Push a context holding the replacement text of the macro NODE on
06025647 729 the context stack. NODE is either object-like, or a function-like
730 macro with no arguments. */
f15f6c8d 731static void
732push_replacement_text (pfile, node)
733 cpp_reader *pfile;
734 cpp_hashnode *node;
735{
9c343313 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 }
f15f6c8d 750
9c343313 751 _cpp_push_text_context (pfile, node, text, len);
06025647 752}
753
01628c3c 754/* Returns TRUE if traditional macro recursion is detected. */
755static bool
756recursive_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
9c343313 796/* Return the length of the replacement text of a function-like or
797 object-like non-builtin macro. */
798size_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
808 for (exp = macro->exp.text;;)
809 {
810 struct block *b = (struct block *) exp;
811
812 len += b->text_len;
813 if (b->arg_index == 0)
814 break;
815 len += NODE_LEN (macro->params[b->arg_index - 1]);
816 exp += BLOCK_LEN (b->text_len);
817 }
818 }
819 else
820 len = macro->count;
821
822 return len;
823}
824
825/* Copy the replacement text of MACRO to DEST, which must be of
826 sufficient size. It is not NUL-terminated. The next character is
827 returned. */
828uchar *
829_cpp_copy_replacement_text (macro, dest)
830 const cpp_macro *macro;
831 uchar *dest;
832{
833 if (macro->fun_like)
834 {
835 const uchar *exp;
836
837 for (exp = macro->exp.text;;)
838 {
839 struct block *b = (struct block *) exp;
840 cpp_hashnode *param;
841
842 memcpy (dest, b->text, b->text_len);
843 dest += b->text_len;
844 if (b->arg_index == 0)
845 break;
846 param = macro->params[b->arg_index - 1];
847 memcpy (dest, NODE_NAME (param), NODE_LEN (param));
848 dest += NODE_LEN (param);
849 exp += BLOCK_LEN (b->text_len);
850 }
851 }
852 else
853 {
854 memcpy (dest, macro->exp.text, macro->count);
855 dest += macro->count;
856 }
857
858 return dest;
859}
860
06025647 861/* Push a context holding the replacement text of the macro NODE on
862 the context stack. NODE is either object-like, or a function-like
863 macro with no arguments. */
864static void
865replace_args_and_push (pfile, fmacro)
866 cpp_reader *pfile;
867 struct fun_macro *fmacro;
868{
869 cpp_macro *macro = fmacro->node->value.macro;
870
871 if (macro->paramc == 0)
872 push_replacement_text (pfile, fmacro->node);
873 else
874 {
875 const uchar *exp;
876 uchar *p;
877 _cpp_buff *buff;
878 size_t len = 0;
879
880 /* Calculate the length of the argument-replaced text. */
881 for (exp = macro->exp.text;;)
882 {
883 struct block *b = (struct block *) exp;
884
885 len += b->text_len;
886 if (b->arg_index == 0)
887 break;
888 len += (fmacro->args[b->arg_index]
889 - fmacro->args[b->arg_index - 1] - 1);
890 exp += BLOCK_LEN (b->text_len);
891 }
892
893 /* Allocate room for the expansion plus NUL. */
894 buff = _cpp_get_buff (pfile, len + 1);
895
896 /* Copy the expansion and replace arguments. */
897 p = BUFF_FRONT (buff);
898 for (exp = macro->exp.text;;)
899 {
900 struct block *b = (struct block *) exp;
901 size_t arglen;
902
903 memcpy (p, b->text, b->text_len);
904 p += b->text_len;
905 if (b->arg_index == 0)
906 break;
907 arglen = (fmacro->args[b->arg_index]
908 - fmacro->args[b->arg_index - 1] - 1);
fb83e0d6 909 memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
06025647 910 arglen);
911 p += arglen;
912 exp += BLOCK_LEN (b->text_len);
913 }
914
915 /* NUL-terminate. */
916 *p = '\0';
917 _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
918
919 /* So we free buffer allocation when macro is left. */
920 pfile->context->buff = buff;
921 }
f15f6c8d 922}
923
198b48a0 924/* Read and record the parameters, if any, of a function-like macro
fb83e0d6 925 definition. Destroys pfile->out.cur.
198b48a0 926
927 Returns true on success, false on failure (syntax error or a
928 duplicate parameter). On success, CUR (pfile->context) is just
929 past the closing parenthesis. */
930static bool
931scan_parameters (pfile, macro)
932 cpp_reader *pfile;
933 cpp_macro *macro;
934{
935 const uchar *cur = CUR (pfile->context) + 1;
936 bool ok;
937
938 for (;;)
939 {
9a468a1b 940 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
198b48a0 941
06025647 942 if (is_idstart (*cur))
198b48a0 943 {
944 ok = false;
945 if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
946 break;
9a468a1b 947 cur = skip_whitespace (pfile, CUR (pfile->context),
948 true /* skip_comments */);
198b48a0 949 if (*cur == ',')
950 {
951 cur++;
952 continue;
953 }
954 ok = (*cur == ')');
955 break;
956 }
957
958 ok = (*cur == ')' && macro->paramc == 0);
959 break;
960 }
961
962 CUR (pfile->context) = cur + (*cur == ')');
963
964 return ok;
965}
966
fb83e0d6 967/* Save the text from pfile->out.base to pfile->out.cur as
198b48a0 968 the replacement text for the current macro, followed by argument
969 ARG_INDEX, with zero indicating the end of the replacement
970 text. */
971static void
972save_replacement_text (pfile, macro, arg_index)
973 cpp_reader *pfile;
974 cpp_macro *macro;
975 unsigned int arg_index;
976{
fb83e0d6 977 size_t len = pfile->out.cur - pfile->out.base;
198b48a0 978 uchar *exp;
979
980 if (macro->paramc == 0)
981 {
982 /* Object-like and function-like macros without parameters
983 simply store their NUL-terminated replacement text. */
984 exp = _cpp_unaligned_alloc (pfile, len + 1);
fb83e0d6 985 memcpy (exp, pfile->out.base, len);
198b48a0 986 exp[len] = '\0';
987 macro->exp.text = exp;
988 macro->count = len;
989 }
990 else
991 {
992 /* Store the text's length (unsigned int), the argument index
993 (unsigned short, base 1) and then the text. */
994 size_t blen = BLOCK_LEN (len);
995 struct block *block;
996
997 if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
998 _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
999
1000 exp = BUFF_FRONT (pfile->a_buff);
1001 block = (struct block *) (exp + macro->count);
1002 macro->exp.text = exp;
1003
1004 /* Write out the block information. */
1005 block->text_len = len;
1006 block->arg_index = arg_index;
fb83e0d6 1007 memcpy (block->text, pfile->out.base, len);
198b48a0 1008
1009 /* Lex the rest into the start of the output buffer. */
fb83e0d6 1010 pfile->out.cur = pfile->out.base;
198b48a0 1011
06025647 1012 macro->count += blen;
3396dc80 1013
1014 /* If we've finished, commit the memory. */
1015 if (arg_index == 0)
1016 BUFF_FRONT (pfile->a_buff) += macro->count;
198b48a0 1017 }
1018}
1019
1020/* Analyze and save the replacement text of a macro. Returns true on
1021 success. */
f15f6c8d 1022bool
1023_cpp_create_trad_definition (pfile, macro)
1024 cpp_reader *pfile;
1025 cpp_macro *macro;
1026{
198b48a0 1027 const uchar *cur;
1028 uchar *limit;
f15f6c8d 1029
fb83e0d6 1030 CUR (pfile->context) = pfile->buffer->cur;
1031
198b48a0 1032 /* Is this a function-like macro? */
1033 if (* CUR (pfile->context) == '(')
1034 {
06025647 1035 /* Setting macro to NULL indicates an error occurred, and
1036 prevents unnecessary work in scan_out_logical_line. */
198b48a0 1037 if (!scan_parameters (pfile, macro))
1038 macro = NULL;
1039 else
1040 {
1041 /* Success. Commit the parameter array. */
1042 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1043 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1044 macro->fun_like = 1;
1045 }
198b48a0 1046 }
1047
06025647 1048 /* Skip leading whitespace in the replacement text. */
9a468a1b 1049 CUR (pfile->context)
1050 = skip_whitespace (pfile, CUR (pfile->context),
1051 CPP_OPTION (pfile, discard_comments_in_macro_exp));
06025647 1052
198b48a0 1053 pfile->state.prevent_expansion++;
1054 scan_out_logical_line (pfile, macro);
1055 pfile->state.prevent_expansion--;
1056
1057 if (!macro)
1058 return false;
f15f6c8d 1059
1060 /* Skip trailing white space. */
fb83e0d6 1061 cur = pfile->out.base;
1062 limit = pfile->out.cur;
f15f6c8d 1063 while (limit > cur && is_space (limit[-1]))
1064 limit--;
fb83e0d6 1065 pfile->out.cur = limit;
198b48a0 1066 save_replacement_text (pfile, macro, 0);
f15f6c8d 1067
1068 return true;
1069}
1070
3396dc80 1071/* Copy SRC of length LEN to DEST, but convert all contiguous
1072 whitespace to a single space, provided it is not in quotes. The
1073 quote currently in effect is pointed to by PQUOTE, and is updated
1074 by the function. Returns the number of bytes copied. */
1075static size_t
1076canonicalize_text (dest, src, len, pquote)
1077 uchar *dest;
1078 const uchar *src;
1079 size_t len;
1080 uchar *pquote;
1081{
1082 uchar *orig_dest = dest;
1083 uchar quote = *pquote;
1084
1085 while (len)
1086 {
1087 if (is_space (*src) && !quote)
1088 {
1089 do
1090 src++, len--;
1091 while (len && is_space (*src));
1092 *dest++ = ' ';
1093 }
1094 else
1095 {
1096 if (*src == '\'' || *src == '"')
1097 {
1098 if (!quote)
1099 quote = *src;
1100 else if (quote == *src)
1101 quote = 0;
1102 }
1103 *dest++ = *src++, len--;
1104 }
1105 }
1106
1107 *pquote = quote;
1108 return dest - orig_dest;
1109}
1110
1111/* Returns true if MACRO1 and MACRO2 have expansions different other
1112 than in the form of their whitespace. */
1113bool
1114_cpp_expansions_different_trad (macro1, macro2)
fd34c678 1115 const cpp_macro *macro1, *macro2;
3396dc80 1116{
1117 uchar *p1 = xmalloc (macro1->count + macro2->count);
1118 uchar *p2 = p1 + macro1->count;
1119 uchar quote1 = 0, quote2;
1120 bool mismatch;
1121 size_t len1, len2;
1122
1123 if (macro1->paramc > 0)
1124 {
1125 const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1126
1127 mismatch = true;
1128 for (;;)
1129 {
1130 struct block *b1 = (struct block *) exp1;
1131 struct block *b2 = (struct block *) exp2;
1132
1133 if (b1->arg_index != b2->arg_index)
1134 break;
1135
1136 len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
1137 len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
1138 if (len1 != len2 || memcmp (p1, p2, len1))
1139 break;
1140 if (b1->arg_index == 0)
1141 {
1142 mismatch = false;
1143 break;
1144 }
1145 exp1 += BLOCK_LEN (b1->text_len);
1146 exp2 += BLOCK_LEN (b2->text_len);
1147 }
1148 }
1149 else
1150 {
1151 len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
1152 len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
1153 mismatch = (len1 != len2 || memcmp (p1, p2, len1));
1154 }
1155
1156 free (p1);
1157 return mismatch;
1158}
1159
f15f6c8d 1160/* Prepare to be able to scan the current buffer. */
1161void
1162_cpp_set_trad_context (pfile)
1163 cpp_reader *pfile;
1164{
1165 cpp_buffer *buffer = pfile->buffer;
1166 cpp_context *context = pfile->context;
1167
1168 if (pfile->context->prev)
1169 abort ();
1170
fb83e0d6 1171 pfile->out.cur = pfile->out.base;
f15f6c8d 1172 CUR (context) = buffer->cur;
1173 RLIMIT (context) = buffer->rlimit;
1174 check_output_buffer (pfile, RLIMIT (context) - CUR (context));
1175}