Merge from mainline
[official-gcc.git] / gcc / cpptrad.c
blobb29750dd6c5c8701e8faef2f85cefd6ef07da75b
1 /* CPP Library - traditional lexical analysis and macro expansion.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Contributed by Neil Booth, May 2002
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.
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.
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. */
19 #include "config.h"
20 #include "system.h"
21 #include "cpplib.h"
22 #include "cpphash.h"
24 /* The replacement text of a function-like macro is stored as a
25 contiguous sequence of aligned blocks, each representing the text
26 between subsequent parameters in that text.
28 Each block comprises the length of text contained therein, the
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
31 easily recognizable as it has an argument index of zero. */
33 struct block
35 unsigned int text_len;
36 unsigned short arg_index;
37 uchar text[1];
40 #define BLOCK_HEADER_LEN offsetof (struct block, text)
41 #define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + TEXT_LEN)
43 /* Structure holding information about a function-like macro
44 invocation. */
45 struct fun_macro
47 /* Memory buffer holding the trad_arg array. */
48 _cpp_buff *buff;
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;
56 /* The hashnode of the macro. */
57 cpp_hashnode *node;
59 /* The offset of the macro name in the output buffer. */
60 size_t offset;
62 /* Zero-based index of argument being currently lexed. */
63 unsigned int argc;
66 /* Lexing state. It is mostly used to prevent macro expansion. */
67 enum ls {ls_none = 0, /* Normal state. */
68 ls_fun_macro, /* When looking for '('. */
69 ls_defined, /* After defined. */
70 ls_defined_close, /* Looking for ')' of defined(). */
71 ls_hash, /* After # in preprocessor conditional. */
72 ls_predicate, /* After the predicate, maybe paren? */
73 ls_answer}; /* In answer to predicate. */
75 /* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c
76 from recognizing comments and directives during its lexing pass. */
78 static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *));
79 static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *,
80 const uchar *));
81 static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *,
82 int));
83 static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
84 static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *, int));
85 static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *));
86 static void check_output_buffer PARAMS ((cpp_reader *, size_t));
87 static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *));
88 static bool scan_parameters PARAMS ((cpp_reader *, cpp_macro *));
89 static bool recursive_macro PARAMS ((cpp_reader *, cpp_hashnode *));
90 static void save_replacement_text PARAMS ((cpp_reader *, cpp_macro *,
91 unsigned int));
92 static void maybe_start_funlike PARAMS ((cpp_reader *, cpp_hashnode *,
93 const uchar *, struct fun_macro *));
94 static void save_argument PARAMS ((struct fun_macro *, size_t));
95 static void replace_args_and_push PARAMS ((cpp_reader *, struct fun_macro *));
96 static size_t canonicalize_text PARAMS ((uchar *, const uchar *, size_t,
97 uchar *));
99 /* Ensures we have N bytes' space in the output buffer, and
100 reallocates it if not. */
101 static void
102 check_output_buffer (pfile, n)
103 cpp_reader *pfile;
104 size_t n;
106 /* We might need two bytes to terminate an unterminated comment, and
107 one more to terminate the line with a NUL. */
108 n += 2 + 1;
110 if (n > (size_t) (pfile->out.limit - pfile->out.cur))
112 size_t size = pfile->out.cur - pfile->out.base;
113 size_t new_size = (size + n) * 3 / 2;
115 pfile->out.base
116 = (uchar *) xrealloc (pfile->out.base, new_size);
117 pfile->out.limit = pfile->out.base + new_size;
118 pfile->out.cur = pfile->out.base + size;
122 /* To be called whenever a newline character is encountered in the
123 input file, at CUR. Handles DOS, Mac and Unix ends of line, and
124 increments pfile->line.
126 Returns a pointer the character after the newline sequence. */
127 static const uchar *
128 handle_newline (pfile, cur)
129 cpp_reader *pfile;
130 const uchar *cur;
132 pfile->line++;
133 if (cur[0] + cur[1] == '\r' + '\n')
134 cur++;
135 return cur + 1;
138 /* CUR points to any character in the buffer, not necessarily a
139 backslash. Advances CUR until all escaped newlines are skipped,
140 and returns the new position.
142 Warns if a file buffer ends in an escaped newline. */
143 static const uchar *
144 skip_escaped_newlines (pfile, cur)
145 cpp_reader *pfile;
146 const uchar *cur;
148 const uchar *orig_cur = cur;
150 while (*cur == '\\' && is_vspace (cur[1]))
151 cur = handle_newline (pfile, cur + 1);
153 if (cur != orig_cur && cur == RLIMIT (pfile->context) && pfile->buffer->inc)
154 cpp_error (pfile, DL_PEDWARN, "backslash-newline at end of file");
156 return cur;
159 /* CUR points to the asterisk introducing a comment in the input
160 buffer. IN_DEFINE is true if we are in the replacement text
161 of a macro.
163 The asterisk and following comment is copied to the buffer pointed
164 to by pfile->out.cur, which must be of sufficient size.
165 Unterminated comments are diagnosed, and correctly terminated in
166 the output. pfile->out.cur is updated depending upon IN_DEFINE,
167 -C, -CC and pfile->state.in_directive.
169 Returns a pointer to the first character after the comment in the
170 input buffer. */
171 static const uchar *
172 copy_comment (pfile, cur, in_define)
173 cpp_reader *pfile;
174 const uchar *cur;
175 int in_define;
177 unsigned int from_line = pfile->line;
178 const uchar *limit = RLIMIT (pfile->context);
179 uchar *out = pfile->out.cur;
183 unsigned int c = *cur++;
184 *out++ = c;
186 if (c == '/')
188 /* An immediate slash does not terminate the comment. */
189 if (out[-2] == '*' && out - 2 > pfile->out.cur)
190 goto done;
192 if (*cur == '*' && cur[1] != '/'
193 && CPP_OPTION (pfile, warn_comments))
194 cpp_error_with_line (pfile, DL_WARNING, pfile->line, 0,
195 "\"/*\" within comment");
197 else if (is_vspace (c))
199 cur = handle_newline (pfile, cur - 1);
200 /* Canonicalize newline sequences and skip escaped ones. */
201 if (out[-2] == '\\')
202 out -= 2;
203 else
204 out[-1] = '\n';
207 while (cur < limit);
209 cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment");
210 *out++ = '*';
211 *out++ = '/';
213 done:
214 /* Comments in directives become spaces so that tokens are properly
215 separated when the ISO preprocessor re-lexes the line. The
216 exception is #define. */
217 if (pfile->state.in_directive)
219 if (in_define)
221 if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
222 pfile->out.cur--;
223 else
224 pfile->out.cur = out;
226 else
227 pfile->out.cur[-1] = ' ';
229 else if (CPP_OPTION (pfile, discard_comments))
230 pfile->out.cur--;
231 else
232 pfile->out.cur = out;
234 return cur;
237 /* CUR points to any character in the input buffer. Skips over all
238 contiguous horizontal white space and NULs, including comments if
239 SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
240 character or the end of the current context. Escaped newlines are
241 removed.
243 The whitespace is copied verbatim to the output buffer, except that
244 comments are handled as described in copy_comment().
245 pfile->out.cur is updated.
247 Returns a pointer to the first character after the whitespace in
248 the input buffer. */
249 static const uchar *
250 skip_whitespace (pfile, cur, skip_comments)
251 cpp_reader *pfile;
252 const uchar *cur;
253 int skip_comments;
255 uchar *out = pfile->out.cur;
257 for (;;)
259 unsigned int c = *cur++;
260 *out++ = c;
262 if (is_nvspace (c) && c)
263 continue;
265 if (!c && cur != RLIMIT (pfile->context))
266 continue;
268 if (*cur == '/' && skip_comments)
270 const uchar *tmp = skip_escaped_newlines (pfile, cur);
271 if (*tmp == '*')
273 pfile->out.cur = out;
274 cur = copy_comment (pfile, tmp, false /* in_define */);
275 out = pfile->out.cur;
276 continue;
280 out--;
281 if (c == '\\' && is_vspace (*cur))
283 cur = skip_escaped_newlines (pfile, cur);
284 continue;
287 break;
290 pfile->out.cur = out;
291 return cur - 1;
294 /* Lexes and outputs an identifier starting at CUR, which is assumed
295 to point to a valid first character of an identifier. Returns
296 the hashnode, and updates out.cur. */
297 static cpp_hashnode *
298 lex_identifier (pfile, cur)
299 cpp_reader *pfile;
300 const uchar *cur;
302 size_t len;
303 uchar *out = pfile->out.cur;
304 cpp_hashnode *result;
309 *out++ = *cur++;
310 while (is_numchar (*cur));
311 cur = skip_escaped_newlines (pfile, cur);
313 while (is_numchar (*cur));
315 CUR (pfile->context) = cur;
316 len = out - pfile->out.cur;
317 result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
318 len, HT_ALLOC);
319 pfile->out.cur = out;
320 return result;
323 /* Overlays the true file buffer temporarily with text of length LEN
324 starting at START. The true buffer is restored upon calling
325 restore_buff(). */
326 void
327 _cpp_overlay_buffer (pfile, start, len)
328 cpp_reader *pfile;
329 const uchar *start;
330 size_t len;
332 cpp_buffer *buffer = pfile->buffer;
334 pfile->overlaid_buffer = buffer;
335 buffer->saved_cur = buffer->cur;
336 buffer->saved_rlimit = buffer->rlimit;
338 buffer->cur = start;
339 buffer->rlimit = start + len;
341 pfile->saved_line = pfile->line;
344 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
345 void
346 _cpp_remove_overlay (pfile)
347 cpp_reader *pfile;
349 cpp_buffer *buffer = pfile->overlaid_buffer;
351 buffer->cur = buffer->saved_cur;
352 buffer->rlimit = buffer->saved_rlimit;
354 pfile->line = pfile->saved_line;
357 /* Reads a logical line into the output buffer. Returns TRUE if there
358 is more text left in the buffer. */
359 bool
360 _cpp_read_logical_line_trad (pfile)
361 cpp_reader *pfile;
363 cpp_buffer *buffer = pfile->buffer;
367 if (buffer->cur == buffer->rlimit)
369 bool stop = true;
371 /* Don't pop the last buffer. */
372 if (buffer->prev)
374 stop = buffer->return_at_eof;
375 _cpp_pop_buffer (pfile);
376 buffer = pfile->buffer;
379 if (stop)
380 return false;
383 CUR (pfile->context) = buffer->cur;
384 RLIMIT (pfile->context) = buffer->rlimit;
385 scan_out_logical_line (pfile, NULL);
386 buffer = pfile->buffer;
387 buffer->cur = CUR (pfile->context);
389 while (pfile->state.skipping);
391 return true;
394 /* Set up state for finding the opening '(' of a function-like
395 macro. */
396 static void
397 maybe_start_funlike (pfile, node, start, macro)
398 cpp_reader *pfile;
399 cpp_hashnode *node;
400 const uchar *start;
401 struct fun_macro *macro;
403 unsigned int n = node->value.macro->paramc + 1;
405 if (macro->buff)
406 _cpp_release_buff (pfile, macro->buff);
407 macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t));
408 macro->args = (size_t *) BUFF_FRONT (macro->buff);
409 macro->node = node;
410 macro->offset = start - pfile->out.base;
411 macro->argc = 0;
413 pfile->state.parsing_args = 1;
416 /* Save the OFFSET of the start of the next argument to MACRO. */
417 static void
418 save_argument (macro, offset)
419 struct fun_macro *macro;
420 size_t offset;
422 macro->argc++;
423 if (macro->argc <= macro->node->value.macro->paramc)
424 macro->args[macro->argc] = offset;
427 /* Copies the next logical line in the current buffer to the output
428 buffer. The output is guaranteed to terminate with a NUL
429 character.
431 If MACRO is non-NULL, then we are scanning the replacement list of
432 MACRO, and we call save_replacement_text() every time we meet an
433 argument. */
434 static void
435 scan_out_logical_line (pfile, macro)
436 cpp_reader *pfile;
437 cpp_macro *macro;
439 cpp_context *context;
440 const uchar *cur;
441 uchar *out;
442 struct fun_macro fmacro;
443 unsigned int c, paren_depth = 0, quote = 0;
444 enum ls lex_state = ls_none;
446 fmacro.buff = NULL;
448 start_logical_line:
449 pfile->out.cur = pfile->out.base;
450 pfile->out.first_line = pfile->line;
451 new_context:
452 context = pfile->context;
453 cur = CUR (context);
454 check_output_buffer (pfile, RLIMIT (context) - cur);
455 out = pfile->out.cur;
457 for (;;)
459 c = *cur++;
460 *out++ = c;
462 /* Whitespace should "continue" out of the switch,
463 non-whitespace should "break" out of it. */
464 switch (c)
466 case ' ':
467 case '\t':
468 case '\f':
469 case '\v':
470 continue;
472 case '\0':
473 if (cur - 1 != RLIMIT (context))
474 continue;
476 /* If this is a macro's expansion, pop it. */
477 if (context->prev)
479 pfile->out.cur = out - 1;
480 _cpp_pop_context (pfile);
481 goto new_context;
484 /* Premature end of file. Fake a new line. */
485 cur--;
486 if (!pfile->buffer->from_stage3)
487 cpp_error (pfile, DL_PEDWARN, "no newline at end of file");
488 if (pfile->state.parsing_args == 2)
489 cpp_error (pfile, DL_ERROR,
490 "unterminated argument list invoking macro \"%s\"",
491 NODE_NAME (fmacro.node));
492 pfile->line++;
493 goto done;
495 case '\r': case '\n':
496 cur = handle_newline (pfile, cur - 1);
497 if (pfile->state.parsing_args == 2 && !pfile->state.in_directive)
499 /* Newlines in arguments become a space. */
500 out[-1] = ' ';
501 continue;
503 goto done;
505 case '<':
506 if (pfile->state.angled_headers && !quote)
507 quote = '>';
508 break;
509 case '>':
510 if (pfile->state.angled_headers && c == quote)
512 pfile->state.angled_headers = false;
513 quote = 0;
515 break;
517 case '"':
518 case '\'':
519 if (c == quote)
520 quote = 0;
521 else if (!quote)
522 quote = c;
523 break;
525 case '\\':
526 if (is_vspace (*cur))
528 out--;
529 cur = skip_escaped_newlines (pfile, cur - 1);
530 continue;
532 else
534 /* Skip escaped quotes here, it's easier than above, but
535 take care to first skip escaped newlines. */
536 cur = skip_escaped_newlines (pfile, cur);
537 if (*cur == '\\' || *cur == '"' || *cur == '\'')
538 *out++ = *cur++;
540 break;
542 case '/':
543 /* Traditional CPP does not recognize comments within
544 literals. */
545 if (!quote)
547 cur = skip_escaped_newlines (pfile, cur);
548 if (*cur == '*')
550 pfile->out.cur = out;
551 cur = copy_comment (pfile, cur, macro != 0);
552 out = pfile->out.cur;
553 continue;
556 break;
558 case '_':
559 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
560 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
561 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
562 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
563 case 'y': case 'z':
564 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
565 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
566 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
567 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
568 case 'Y': case 'Z':
569 if (!pfile->state.skipping && (quote == 0 || macro))
571 cpp_hashnode *node;
572 uchar *out_start = out - 1;
574 pfile->out.cur = out_start;
575 node = lex_identifier (pfile, cur - 1);
576 out = pfile->out.cur;
577 cur = CUR (context);
579 if (node->type == NT_MACRO
580 /* Should we expand for ls_answer? */
581 && lex_state == ls_none
582 && !pfile->state.prevent_expansion
583 && !recursive_macro (pfile, node))
585 if (node->value.macro->fun_like)
587 maybe_start_funlike (pfile, node, out_start, &fmacro);
588 lex_state = ls_fun_macro;
589 continue;
591 else
593 /* Remove the object-like macro's name from the
594 output, and push its replacement text. */
595 pfile->out.cur = out_start;
596 push_replacement_text (pfile, node);
597 goto new_context;
600 else if (macro && node->arg_index)
602 /* Found a parameter in the replacement text of a
603 #define. Remove its name from the output. */
604 out = pfile->out.cur = out_start;
605 save_replacement_text (pfile, macro, node->arg_index);
607 else if (lex_state == ls_hash)
609 lex_state = ls_predicate;
610 continue;
612 else if (pfile->state.in_expression
613 && node == pfile->spec_nodes.n_defined)
615 lex_state = ls_defined;
616 continue;
619 break;
621 case '(':
622 if (quote == 0)
624 paren_depth++;
625 if (lex_state == ls_fun_macro)
627 lex_state = ls_none;
628 pfile->state.parsing_args = 2;
629 paren_depth = 1;
630 out = pfile->out.base + fmacro.offset;
631 fmacro.args[0] = fmacro.offset;
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;
638 break;
640 case ',':
641 if (quote == 0 && pfile->state.parsing_args == 2 && paren_depth == 1)
642 save_argument (&fmacro, out - pfile->out.base);
643 break;
645 case ')':
646 if (quote == 0)
648 paren_depth--;
649 if (pfile->state.parsing_args == 2 && paren_depth == 0)
651 cpp_macro *m = fmacro.node->value.macro;
653 pfile->state.parsing_args = 0;
654 save_argument (&fmacro, out - pfile->out.base);
656 /* A single zero-length argument is no argument. */
657 if (fmacro.argc == 1
658 && m->paramc == 0
659 && out == pfile->out.base + fmacro.offset + 1)
660 fmacro.argc = 0;
662 if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
664 /* Remove the macro's invocation from the
665 output, and push its replacement text. */
666 pfile->out.cur = (pfile->out.base
667 + fmacro.offset);
668 CUR (context) = cur;
669 replace_args_and_push (pfile, &fmacro);
670 goto new_context;
673 else if (lex_state == ls_answer || lex_state == ls_defined_close)
674 lex_state = ls_none;
676 break;
678 case '#':
679 /* At start of a line it's a directive. */
680 if (out - 1 == pfile->out.base && !pfile->state.in_directive)
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 */))
686 goto start_logical_line;
688 if (pfile->state.in_expression)
690 lex_state = ls_hash;
691 continue;
693 break;
695 default:
696 break;
699 if (lex_state == ls_none)
700 continue;
702 /* Some of these transitions of state are syntax errors. The
703 ISO preprocessor will issue errors later. */
704 if (lex_state == ls_fun_macro)
706 /* Missing '('. */
707 lex_state = ls_none;
708 pfile->state.parsing_args = 0;
710 else if (lex_state == ls_hash
711 || lex_state == ls_predicate
712 || lex_state == ls_defined)
713 lex_state = ls_none;
715 /* ls_answer and ls_defined_close keep going until ')'. */
718 done:
719 out[-1] = '\0';
720 CUR (context) = cur;
721 pfile->out.cur = out - 1;
722 if (fmacro.buff)
723 _cpp_release_buff (pfile, fmacro.buff);
725 if (pfile->state.parsing_args == 2)
726 cpp_error (pfile, DL_ERROR,
727 "unterminated argument list invoking macro \"%s\"",
728 NODE_NAME (fmacro.node));
729 pfile->state.parsing_args = 0;
732 /* Push a context holding the replacement text of the macro NODE on
733 the context stack. NODE is either object-like, or a function-like
734 macro with no arguments. */
735 static void
736 push_replacement_text (pfile, node)
737 cpp_reader *pfile;
738 cpp_hashnode *node;
740 cpp_macro *macro = node->value.macro;
742 _cpp_push_text_context (pfile, node, macro->exp.text, macro->count);
745 /* Returns TRUE if traditional macro recursion is detected. */
746 static bool
747 recursive_macro (pfile, node)
748 cpp_reader *pfile;
749 cpp_hashnode *node;
751 bool recursing = node->flags & NODE_DISABLED;
753 /* Object-like macros that are already expanding are necessarily
754 recursive.
756 However, it is possible to have traditional function-like macros
757 that are not infinitely recursive but recurse to any given depth.
758 Further, it is easy to construct examples that get ever longer
759 until the point they stop recursing. So there is no easy way to
760 detect true recursion; instead we assume any expansion more than
761 20 deep since the first invocation of this macro must be
762 recursing. */
763 if (recursing && node->value.macro->fun_like)
765 size_t depth = 0;
766 cpp_context *context = pfile->context;
770 depth++;
771 if (context->macro == node && depth > 20)
772 break;
773 context = context->prev;
775 while (context);
776 recursing = context != NULL;
779 if (recursing)
780 cpp_error (pfile, DL_ERROR,
781 "detected recursion whilst expanding macro \"%s\"",
782 NODE_NAME (node));
784 return recursing;
787 /* Push a context holding the replacement text of the macro NODE on
788 the context stack. NODE is either object-like, or a function-like
789 macro with no arguments. */
790 static void
791 replace_args_and_push (pfile, fmacro)
792 cpp_reader *pfile;
793 struct fun_macro *fmacro;
795 cpp_macro *macro = fmacro->node->value.macro;
797 if (macro->paramc == 0)
798 push_replacement_text (pfile, fmacro->node);
799 else
801 const uchar *exp;
802 uchar *p;
803 _cpp_buff *buff;
804 size_t len = 0;
806 /* Calculate the length of the argument-replaced text. */
807 for (exp = macro->exp.text;;)
809 struct block *b = (struct block *) exp;
811 len += b->text_len;
812 if (b->arg_index == 0)
813 break;
814 len += (fmacro->args[b->arg_index]
815 - fmacro->args[b->arg_index - 1] - 1);
816 exp += BLOCK_LEN (b->text_len);
819 /* Allocate room for the expansion plus NUL. */
820 buff = _cpp_get_buff (pfile, len + 1);
822 /* Copy the expansion and replace arguments. */
823 p = BUFF_FRONT (buff);
824 for (exp = macro->exp.text;;)
826 struct block *b = (struct block *) exp;
827 size_t arglen;
829 memcpy (p, b->text, b->text_len);
830 p += b->text_len;
831 if (b->arg_index == 0)
832 break;
833 arglen = (fmacro->args[b->arg_index]
834 - fmacro->args[b->arg_index - 1] - 1);
835 memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
836 arglen);
837 p += arglen;
838 exp += BLOCK_LEN (b->text_len);
841 /* NUL-terminate. */
842 *p = '\0';
843 _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
845 /* So we free buffer allocation when macro is left. */
846 pfile->context->buff = buff;
850 /* Read and record the parameters, if any, of a function-like macro
851 definition. Destroys pfile->out.cur.
853 Returns true on success, false on failure (syntax error or a
854 duplicate parameter). On success, CUR (pfile->context) is just
855 past the closing parenthesis. */
856 static bool
857 scan_parameters (pfile, macro)
858 cpp_reader *pfile;
859 cpp_macro *macro;
861 const uchar *cur = CUR (pfile->context) + 1;
862 bool ok;
864 for (;;)
866 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
868 if (is_idstart (*cur))
870 ok = false;
871 if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
872 break;
873 cur = skip_whitespace (pfile, CUR (pfile->context),
874 true /* skip_comments */);
875 if (*cur == ',')
877 cur++;
878 continue;
880 ok = (*cur == ')');
881 break;
884 ok = (*cur == ')' && macro->paramc == 0);
885 break;
888 CUR (pfile->context) = cur + (*cur == ')');
890 return ok;
893 /* Save the text from pfile->out.base to pfile->out.cur as
894 the replacement text for the current macro, followed by argument
895 ARG_INDEX, with zero indicating the end of the replacement
896 text. */
897 static void
898 save_replacement_text (pfile, macro, arg_index)
899 cpp_reader *pfile;
900 cpp_macro *macro;
901 unsigned int arg_index;
903 size_t len = pfile->out.cur - pfile->out.base;
904 uchar *exp;
906 if (macro->paramc == 0)
908 /* Object-like and function-like macros without parameters
909 simply store their NUL-terminated replacement text. */
910 exp = _cpp_unaligned_alloc (pfile, len + 1);
911 memcpy (exp, pfile->out.base, len);
912 exp[len] = '\0';
913 macro->exp.text = exp;
914 macro->count = len;
916 else
918 /* Store the text's length (unsigned int), the argument index
919 (unsigned short, base 1) and then the text. */
920 size_t blen = BLOCK_LEN (len);
921 struct block *block;
923 if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
924 _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
926 exp = BUFF_FRONT (pfile->a_buff);
927 block = (struct block *) (exp + macro->count);
928 macro->exp.text = exp;
930 /* Write out the block information. */
931 block->text_len = len;
932 block->arg_index = arg_index;
933 memcpy (block->text, pfile->out.base, len);
935 /* Lex the rest into the start of the output buffer. */
936 pfile->out.cur = pfile->out.base;
938 macro->count += blen;
940 /* If we've finished, commit the memory. */
941 if (arg_index == 0)
942 BUFF_FRONT (pfile->a_buff) += macro->count;
946 /* Analyze and save the replacement text of a macro. Returns true on
947 success. */
948 bool
949 _cpp_create_trad_definition (pfile, macro)
950 cpp_reader *pfile;
951 cpp_macro *macro;
953 const uchar *cur;
954 uchar *limit;
956 CUR (pfile->context) = pfile->buffer->cur;
958 /* Is this a function-like macro? */
959 if (* CUR (pfile->context) == '(')
961 /* Setting macro to NULL indicates an error occurred, and
962 prevents unnecessary work in scan_out_logical_line. */
963 if (!scan_parameters (pfile, macro))
964 macro = NULL;
965 else
967 /* Success. Commit the parameter array. */
968 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
969 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
970 macro->fun_like = 1;
974 /* Skip leading whitespace in the replacement text. */
975 CUR (pfile->context)
976 = skip_whitespace (pfile, CUR (pfile->context),
977 CPP_OPTION (pfile, discard_comments_in_macro_exp));
979 pfile->state.prevent_expansion++;
980 scan_out_logical_line (pfile, macro);
981 pfile->state.prevent_expansion--;
983 if (!macro)
984 return false;
986 /* Skip trailing white space. */
987 cur = pfile->out.base;
988 limit = pfile->out.cur;
989 while (limit > cur && is_space (limit[-1]))
990 limit--;
991 pfile->out.cur = limit;
992 save_replacement_text (pfile, macro, 0);
994 return true;
997 /* Copy SRC of length LEN to DEST, but convert all contiguous
998 whitespace to a single space, provided it is not in quotes. The
999 quote currently in effect is pointed to by PQUOTE, and is updated
1000 by the function. Returns the number of bytes copied. */
1001 static size_t
1002 canonicalize_text (dest, src, len, pquote)
1003 uchar *dest;
1004 const uchar *src;
1005 size_t len;
1006 uchar *pquote;
1008 uchar *orig_dest = dest;
1009 uchar quote = *pquote;
1011 while (len)
1013 if (is_space (*src) && !quote)
1016 src++, len--;
1017 while (len && is_space (*src));
1018 *dest++ = ' ';
1020 else
1022 if (*src == '\'' || *src == '"')
1024 if (!quote)
1025 quote = *src;
1026 else if (quote == *src)
1027 quote = 0;
1029 *dest++ = *src++, len--;
1033 *pquote = quote;
1034 return dest - orig_dest;
1037 /* Returns true if MACRO1 and MACRO2 have expansions different other
1038 than in the form of their whitespace. */
1039 bool
1040 _cpp_expansions_different_trad (macro1, macro2)
1041 const cpp_macro *macro1, *macro2;
1043 uchar *p1 = xmalloc (macro1->count + macro2->count);
1044 uchar *p2 = p1 + macro1->count;
1045 uchar quote1 = 0, quote2;
1046 bool mismatch;
1047 size_t len1, len2;
1049 if (macro1->paramc > 0)
1051 const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1053 mismatch = true;
1054 for (;;)
1056 struct block *b1 = (struct block *) exp1;
1057 struct block *b2 = (struct block *) exp2;
1059 if (b1->arg_index != b2->arg_index)
1060 break;
1062 len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
1063 len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
1064 if (len1 != len2 || memcmp (p1, p2, len1))
1065 break;
1066 if (b1->arg_index == 0)
1068 mismatch = false;
1069 break;
1071 exp1 += BLOCK_LEN (b1->text_len);
1072 exp2 += BLOCK_LEN (b2->text_len);
1075 else
1077 len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
1078 len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
1079 mismatch = (len1 != len2 || memcmp (p1, p2, len1));
1082 free (p1);
1083 return mismatch;
1086 /* Prepare to be able to scan the current buffer. */
1087 void
1088 _cpp_set_trad_context (pfile)
1089 cpp_reader *pfile;
1091 cpp_buffer *buffer = pfile->buffer;
1092 cpp_context *context = pfile->context;
1094 if (pfile->context->prev)
1095 abort ();
1097 pfile->out.cur = pfile->out.base;
1098 CUR (context) = buffer->cur;
1099 RLIMIT (context) = buffer->rlimit;
1100 check_output_buffer (pfile, RLIMIT (context) - CUR (context));