1 /* CPP Library - traditional lexical analysis and macro expansion.
2 Copyright (C) 2002, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Neil Booth, May 2002
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
25 /* The replacement text of a function-like macro is stored as a
26 contiguous sequence of aligned blocks, each representing the text
27 between subsequent parameters.
29 Each block comprises the text between its surrounding parameters,
30 the length of that text, and the one-based index of the following
31 parameter. The final block in the replacement text is easily
32 recognizable as it has an argument index of zero. */
36 unsigned int text_len;
37 unsigned short arg_index;
41 #define BLOCK_HEADER_LEN offsetof (struct block, text)
42 #define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN))
44 /* Structure holding information about a function-like macro
48 /* Memory buffer holding the trad_arg array. */
51 /* An array of size the number of macro parameters + 1, containing
52 the offsets of the start of each macro argument in the output
53 buffer. The argument continues until the character before the
54 start of the next one. */
57 /* The hashnode of the macro. */
60 /* The offset of the macro name in the output buffer. */
63 /* The line the macro name appeared on. */
66 /* Zero-based index of argument being currently lexed. */
70 /* Lexing state. It is mostly used to prevent macro expansion. */
71 enum ls {ls_none = 0, /* Normal state. */
72 ls_fun_open, /* When looking for '('. */
73 ls_fun_close, /* When looking for ')'. */
74 ls_defined, /* After defined. */
75 ls_defined_close, /* Looking for ')' of defined(). */
76 ls_hash, /* After # in preprocessor conditional. */
77 ls_predicate, /* After the predicate, maybe paren? */
78 ls_answer}; /* In answer to predicate. */
80 /* Lexing TODO: Maybe handle space in escaped newlines. Stop lex.c
81 from recognizing comments and directives during its lexing pass. */
83 static const uchar *skip_whitespace (cpp_reader *, const uchar *, int);
84 static cpp_hashnode *lex_identifier (cpp_reader *, const uchar *);
85 static const uchar *copy_comment (cpp_reader *, const uchar *, int);
86 static void check_output_buffer (cpp_reader *, size_t);
87 static void push_replacement_text (cpp_reader *, cpp_hashnode *);
88 static bool scan_parameters (cpp_reader *, cpp_macro *);
89 static bool recursive_macro (cpp_reader *, cpp_hashnode *);
90 static void save_replacement_text (cpp_reader *, cpp_macro *, unsigned int);
91 static void maybe_start_funlike (cpp_reader *, cpp_hashnode *, const uchar *,
93 static void save_argument (struct fun_macro *, size_t);
94 static void replace_args_and_push (cpp_reader *, struct fun_macro *);
95 static size_t canonicalize_text (uchar *, const uchar *, size_t, uchar *);
97 /* Ensures we have N bytes' space in the output buffer, and
98 reallocates it if not. */
100 check_output_buffer (cpp_reader *pfile, size_t n)
102 /* We might need two bytes to terminate an unterminated comment, and
103 one more to terminate the line with a NUL. */
106 if (n > (size_t) (pfile->out.limit - pfile->out.cur))
108 size_t size = pfile->out.cur - pfile->out.base;
109 size_t new_size = (size + n) * 3 / 2;
111 pfile->out.base = XRESIZEVEC (unsigned char, pfile->out.base, new_size);
112 pfile->out.limit = pfile->out.base + new_size;
113 pfile->out.cur = pfile->out.base + size;
117 /* Skip a C-style block comment in a macro as a result of -CC.
118 Buffer->cur points to the initial asterisk of the comment. */
120 skip_macro_block_comment (cpp_reader *pfile)
122 const uchar *cur = pfile->buffer->cur;
128 /* People like decorating comments with '*', so check for '/'
129 instead for efficiency. */
130 while(! (*cur++ == '/' && cur[-2] == '*') )
133 pfile->buffer->cur = cur;
136 /* CUR points to the asterisk introducing a comment in the current
137 context. IN_DEFINE is true if we are in the replacement text of a
140 The asterisk and following comment is copied to the buffer pointed
141 to by pfile->out.cur, which must be of sufficient size.
142 Unterminated comments are diagnosed, and correctly terminated in
143 the output. pfile->out.cur is updated depending upon IN_DEFINE,
144 -C, -CC and pfile->state.in_directive.
146 Returns a pointer to the first character after the comment in the
149 copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
151 bool unterminated, copy = false;
152 source_location src_loc = pfile->line_table->highest_line;
153 cpp_buffer *buffer = pfile->buffer;
156 if (pfile->context->prev)
157 unterminated = false, skip_macro_block_comment (pfile);
159 unterminated = _cpp_skip_block_comment (pfile);
162 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
163 "unterminated comment");
165 /* Comments in directives become spaces so that tokens are properly
166 separated when the ISO preprocessor re-lexes the line. The
167 exception is #define. */
168 if (pfile->state.in_directive)
172 if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
178 pfile->out.cur[-1] = ' ';
180 else if (CPP_OPTION (pfile, discard_comments))
187 size_t len = (size_t) (buffer->cur - cur);
188 memcpy (pfile->out.cur, cur, len);
189 pfile->out.cur += len;
192 *pfile->out.cur++ = '*';
193 *pfile->out.cur++ = '/';
200 /* CUR points to any character in the input buffer. Skips over all
201 contiguous horizontal white space and NULs, including comments if
202 SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
203 character or the end of the current context. Escaped newlines are
206 The whitespace is copied verbatim to the output buffer, except that
207 comments are handled as described in copy_comment().
208 pfile->out.cur is updated.
210 Returns a pointer to the first character after the whitespace in
213 skip_whitespace (cpp_reader *pfile, const uchar *cur, int skip_comments)
215 uchar *out = pfile->out.cur;
219 unsigned int c = *cur++;
225 if (c == '/' && *cur == '*' && skip_comments)
227 pfile->out.cur = out;
228 cur = copy_comment (pfile, cur, false /* in_define */);
229 out = pfile->out.cur;
237 pfile->out.cur = out;
241 /* Lexes and outputs an identifier starting at CUR, which is assumed
242 to point to a valid first character of an identifier. Returns
243 the hashnode, and updates out.cur. */
244 static cpp_hashnode *
245 lex_identifier (cpp_reader *pfile, const uchar *cur)
248 uchar *out = pfile->out.cur;
249 cpp_hashnode *result;
253 while (is_numchar (*cur));
255 CUR (pfile->context) = cur;
256 len = out - pfile->out.cur;
257 result = CPP_HASHNODE (ht_lookup (pfile->hash_table, pfile->out.cur,
259 pfile->out.cur = out;
263 /* Overlays the true file buffer temporarily with text of length LEN
264 starting at START. The true buffer is restored upon calling
267 _cpp_overlay_buffer (cpp_reader *pfile, const uchar *start, size_t len)
269 cpp_buffer *buffer = pfile->buffer;
271 pfile->overlaid_buffer = buffer;
272 pfile->saved_cur = buffer->cur;
273 pfile->saved_rlimit = buffer->rlimit;
274 pfile->saved_line_base = buffer->next_line;
275 buffer->need_line = false;
278 buffer->line_base = start;
279 buffer->rlimit = start + len;
282 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
284 _cpp_remove_overlay (cpp_reader *pfile)
286 cpp_buffer *buffer = pfile->overlaid_buffer;
288 buffer->cur = pfile->saved_cur;
289 buffer->rlimit = pfile->saved_rlimit;
290 buffer->line_base = pfile->saved_line_base;
291 buffer->need_line = true;
293 pfile->overlaid_buffer = NULL;
296 /* Reads a logical line into the output buffer. Returns TRUE if there
297 is more text left in the buffer. */
299 _cpp_read_logical_line_trad (cpp_reader *pfile)
303 if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile))
306 while (!_cpp_scan_out_logical_line (pfile, NULL) || pfile->state.skipping);
308 return pfile->buffer != NULL;
311 /* Set up state for finding the opening '(' of a function-like
314 maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start, struct fun_macro *macro)
316 unsigned int n = node->value.macro->paramc + 1;
319 _cpp_release_buff (pfile, macro->buff);
320 macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t));
321 macro->args = (size_t *) BUFF_FRONT (macro->buff);
323 macro->offset = start - pfile->out.base;
327 /* Save the OFFSET of the start of the next argument to MACRO. */
329 save_argument (struct fun_macro *macro, size_t offset)
332 if (macro->argc <= macro->node->value.macro->paramc)
333 macro->args[macro->argc] = offset;
336 /* Copies the next logical line in the current buffer (starting at
337 buffer->cur) to the output buffer. The output is guaranteed to
338 terminate with a NUL character. buffer->cur is updated.
340 If MACRO is non-NULL, then we are scanning the replacement list of
341 MACRO, and we call save_replacement_text() every time we meet an
344 _cpp_scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
347 cpp_context *context;
350 struct fun_macro fmacro;
351 unsigned int c, paren_depth = 0, quote;
352 enum ls lex_state = ls_none;
354 const uchar *start_of_input_line;
364 header_ok = pfile->state.angled_headers;
365 CUR (pfile->context) = pfile->buffer->cur;
366 RLIMIT (pfile->context) = pfile->buffer->rlimit;
367 pfile->out.cur = pfile->out.base;
368 pfile->out.first_line = pfile->line_table->highest_line;
369 /* start_of_input_line is needed to make sure that directives really,
370 really start at the first character of the line. */
371 start_of_input_line = pfile->buffer->cur;
373 context = pfile->context;
375 check_output_buffer (pfile, RLIMIT (context) - cur);
376 out = pfile->out.cur;
381 && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
383 pfile->buffer->cur = cur;
384 _cpp_process_line_notes (pfile, false);
389 /* Whitespace should "continue" out of the switch,
390 non-whitespace should "break" out of it. */
401 /* If this is a macro's expansion, pop it. */
404 pfile->out.cur = out - 1;
405 _cpp_pop_context (pfile);
409 /* Omit the newline from the output buffer. */
410 pfile->out.cur = out - 1;
411 pfile->buffer->cur = cur;
412 pfile->buffer->need_line = true;
413 CPP_INCREMENT_LINE (pfile, 0);
415 if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
416 && !pfile->state.in_directive
417 && _cpp_get_fresh_line (pfile))
419 /* Newlines in arguments become a space, but we don't
420 clear any in-progress quote. */
421 if (lex_state == ls_fun_close)
423 cur = pfile->buffer->cur;
446 /* Skip escaped quotes here, it's easier than above. */
447 if (*cur == '\\' || *cur == '"' || *cur == '\'')
452 /* Traditional CPP does not recognize comments within
454 if (!quote && *cur == '*')
456 pfile->out.cur = out;
457 cur = copy_comment (pfile, cur, macro != 0);
458 out = pfile->out.cur;
464 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
465 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
466 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
467 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
469 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
470 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
471 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
472 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
474 if (!pfile->state.skipping && (quote == 0 || macro))
477 uchar *out_start = out - 1;
479 pfile->out.cur = out_start;
480 node = lex_identifier (pfile, cur - 1);
481 out = pfile->out.cur;
484 if (node->type == NT_MACRO
485 /* Should we expand for ls_answer? */
486 && (lex_state == ls_none || lex_state == ls_fun_open)
487 && !pfile->state.prevent_expansion)
489 /* Macros invalidate MI optimization. */
490 pfile->mi_valid = false;
491 if (! (node->flags & NODE_BUILTIN)
492 && node->value.macro->fun_like)
494 maybe_start_funlike (pfile, node, out_start, &fmacro);
495 lex_state = ls_fun_open;
496 fmacro.line = pfile->line_table->highest_line;
499 else if (!recursive_macro (pfile, node))
501 /* Remove the object-like macro's name from the
502 output, and push its replacement text. */
503 pfile->out.cur = out_start;
504 push_replacement_text (pfile, node);
509 else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
511 /* Found a parameter in the replacement text of a
512 #define. Remove its name from the output. */
513 pfile->out.cur = out_start;
514 save_replacement_text (pfile, macro, node->value.arg_index);
515 out = pfile->out.base;
517 else if (lex_state == ls_hash)
519 lex_state = ls_predicate;
522 else if (pfile->state.in_expression
523 && node == pfile->spec_nodes.n_defined)
525 lex_state = ls_defined;
535 if (lex_state == ls_fun_open)
537 if (recursive_macro (pfile, fmacro.node))
541 lex_state = ls_fun_close;
543 out = pfile->out.base + fmacro.offset;
544 fmacro.args[0] = fmacro.offset;
547 else if (lex_state == ls_predicate)
548 lex_state = ls_answer;
549 else if (lex_state == ls_defined)
550 lex_state = ls_defined_close;
555 if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
556 save_argument (&fmacro, out - pfile->out.base);
563 if (lex_state == ls_fun_close && paren_depth == 0)
565 cpp_macro *m = fmacro.node->value.macro;
569 save_argument (&fmacro, out - pfile->out.base);
571 /* A single zero-length argument is no argument. */
574 && out == pfile->out.base + fmacro.offset + 1)
577 if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
579 /* Remove the macro's invocation from the
580 output, and push its replacement text. */
581 pfile->out.cur = (pfile->out.base
584 replace_args_and_push (pfile, &fmacro);
588 else if (lex_state == ls_answer || lex_state == ls_defined_close)
594 if (cur - 1 == start_of_input_line
595 /* A '#' from a macro doesn't start a directive. */
596 && !pfile->context->prev
597 && !pfile->state.in_directive)
599 /* A directive. With the way _cpp_handle_directive
600 currently works, we only want to call it if either we
601 know the directive is OK, or we want it to fail and
602 be removed from the output. If we want it to be
603 passed through (the assembler case) then we must not
604 call _cpp_handle_directive. */
605 pfile->out.cur = out;
606 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
607 out = pfile->out.cur;
611 /* Null directive. Ignore it and don't invalidate
612 the MI optimization. */
613 pfile->buffer->need_line = true;
614 CPP_INCREMENT_LINE (pfile, 0);
622 if (is_numstart (*cur)
623 && CPP_OPTION (pfile, lang) != CLK_ASM)
625 else if (is_idstart (*cur))
626 /* Check whether we know this directive, but don't
628 do_it = lex_identifier (pfile, cur)->is_directive;
630 if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
632 /* This is a kludge. We want to have the ISO
633 preprocessor lex the next token. */
634 pfile->buffer->cur = cur;
635 _cpp_handle_directive (pfile, false /* indented */);
642 if (pfile->state.in_expression)
653 /* Non-whitespace disables MI optimization and stops treating
654 '<' as a quote in #include. */
656 if (!pfile->state.in_directive)
657 pfile->mi_valid = false;
659 if (lex_state == ls_none)
662 /* Some of these transitions of state are syntax errors. The
663 ISO preprocessor will issue errors later. */
664 if (lex_state == ls_fun_open)
667 else if (lex_state == ls_hash
668 || lex_state == ls_predicate
669 || lex_state == ls_defined)
672 /* ls_answer and ls_defined_close keep going until ')'. */
677 _cpp_release_buff (pfile, fmacro.buff);
679 if (lex_state == ls_fun_close)
680 cpp_error_with_line (pfile, CPP_DL_ERROR, fmacro.line, 0,
681 "unterminated argument list invoking macro \"%s\"",
682 NODE_NAME (fmacro.node));
686 /* Push a context holding the replacement text of the macro NODE on
687 the context stack. NODE is either object-like, or a function-like
688 macro with no arguments. */
690 push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
696 if (node->flags & NODE_BUILTIN)
698 text = _cpp_builtin_macro_text (pfile, node);
699 len = ustrlen (text);
700 buf = _cpp_unaligned_alloc (pfile, len + 1);
701 memcpy (buf, text, len);
707 cpp_macro *macro = node->value.macro;
709 text = macro->exp.text;
710 macro->traditional = 1;
714 _cpp_push_text_context (pfile, node, text, len);
717 /* Returns TRUE if traditional macro recursion is detected. */
719 recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
721 bool recursing = !!(node->flags & NODE_DISABLED);
723 /* Object-like macros that are already expanding are necessarily
726 However, it is possible to have traditional function-like macros
727 that are not infinitely recursive but recurse to any given depth.
728 Further, it is easy to construct examples that get ever longer
729 until the point they stop recursing. So there is no easy way to
730 detect true recursion; instead we assume any expansion more than
731 20 deep since the first invocation of this macro must be
733 if (recursing && node->value.macro->fun_like)
736 cpp_context *context = pfile->context;
741 if (context->macro == node && depth > 20)
743 context = context->prev;
746 recursing = context != NULL;
750 cpp_error (pfile, CPP_DL_ERROR,
751 "detected recursion whilst expanding macro \"%s\"",
757 /* Return the length of the replacement text of a function-like or
758 object-like non-builtin macro. */
760 _cpp_replacement_text_len (const cpp_macro *macro)
764 if (macro->fun_like && (macro->paramc != 0))
769 for (exp = macro->exp.text;;)
771 struct block *b = (struct block *) exp;
774 if (b->arg_index == 0)
776 len += NODE_LEN (macro->params[b->arg_index - 1]);
777 exp += BLOCK_LEN (b->text_len);
786 /* Copy the replacement text of MACRO to DEST, which must be of
787 sufficient size. It is not NUL-terminated. The next character is
790 _cpp_copy_replacement_text (const cpp_macro *macro, uchar *dest)
792 if (macro->fun_like && (macro->paramc != 0))
796 for (exp = macro->exp.text;;)
798 struct block *b = (struct block *) exp;
801 memcpy (dest, b->text, b->text_len);
803 if (b->arg_index == 0)
805 param = macro->params[b->arg_index - 1];
806 memcpy (dest, NODE_NAME (param), NODE_LEN (param));
807 dest += NODE_LEN (param);
808 exp += BLOCK_LEN (b->text_len);
813 memcpy (dest, macro->exp.text, macro->count);
814 dest += macro->count;
820 /* Push a context holding the replacement text of the macro NODE on
821 the context stack. NODE is either object-like, or a function-like
822 macro with no arguments. */
824 replace_args_and_push (cpp_reader *pfile, struct fun_macro *fmacro)
826 cpp_macro *macro = fmacro->node->value.macro;
828 if (macro->paramc == 0)
829 push_replacement_text (pfile, fmacro->node);
838 /* Get an estimate of the length of the argument-replaced text.
839 This is a worst case estimate, assuming that every replacement
840 text character needs quoting. */
841 for (exp = macro->exp.text;;)
843 struct block *b = (struct block *) exp;
846 if (b->arg_index == 0)
848 len += 2 * (fmacro->args[b->arg_index]
849 - fmacro->args[b->arg_index - 1] - 1);
850 exp += BLOCK_LEN (b->text_len);
853 /* Allocate room for the expansion plus \n. */
854 buff = _cpp_get_buff (pfile, len + 1);
856 /* Copy the expansion and replace arguments. */
857 /* Accumulate actual length, including quoting as necessary */
858 p = BUFF_FRONT (buff);
860 for (exp = macro->exp.text;;)
862 struct block *b = (struct block *) exp;
869 /* Copy the non-argument text literally, keeping
870 track of whether matching quotes have been seen. */
871 for (arglen = b->text_len, in = b->text; arglen > 0; arglen--)
874 cxtquote = ! cxtquote;
877 /* Done if no more arguments */
878 if (b->arg_index == 0)
880 arglen = (fmacro->args[b->arg_index]
881 - fmacro->args[b->arg_index - 1] - 1);
882 base = pfile->out.base + fmacro->args[b->arg_index - 1];
885 /* Skip leading whitespace in the text for the argument to
886 be substituted. To be compatible with gcc 2.95, we would
887 also need to trim trailing whitespace. Gcc 2.95 trims
888 leading and trailing whitespace, which may be a bug. The
889 current gcc testsuite explicitly checks that this leading
890 and trailing whitespace in actual arguments is
892 while (arglen > 0 && is_space (*in))
898 for (argquote = 0; arglen > 0; arglen--)
900 if (cxtquote && *in == '"')
902 if (in > base && *(in-1) != '\\')
903 argquote = ! argquote;
904 /* Always add backslash before double quote if argument
905 is expanded in a quoted context */
909 else if (cxtquote && argquote && *in == '\\')
911 /* Always add backslash before a backslash in an argument
912 that is expanded in a quoted context and also in the
913 range of a quoted context in the argument itself. */
920 exp += BLOCK_LEN (b->text_len);
925 _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
927 /* So we free buffer allocation when macro is left. */
928 pfile->context->buff = buff;
932 /* Read and record the parameters, if any, of a function-like macro
933 definition. Destroys pfile->out.cur.
935 Returns true on success, false on failure (syntax error or a
936 duplicate parameter). On success, CUR (pfile->context) is just
937 past the closing parenthesis. */
939 scan_parameters (cpp_reader *pfile, cpp_macro *macro)
941 const uchar *cur = CUR (pfile->context) + 1;
946 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
948 if (is_idstart (*cur))
951 if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
953 cur = skip_whitespace (pfile, CUR (pfile->context),
954 true /* skip_comments */);
964 ok = (*cur == ')' && macro->paramc == 0);
969 cpp_error (pfile, CPP_DL_ERROR, "syntax error in macro parameter list");
971 CUR (pfile->context) = cur + (*cur == ')');
976 /* Save the text from pfile->out.base to pfile->out.cur as
977 the replacement text for the current macro, followed by argument
978 ARG_INDEX, with zero indicating the end of the replacement
981 save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
982 unsigned int arg_index)
984 size_t len = pfile->out.cur - pfile->out.base;
987 if (macro->paramc == 0)
989 /* Object-like and function-like macros without parameters
990 simply store their \n-terminated replacement text. */
991 exp = _cpp_unaligned_alloc (pfile, len + 1);
992 memcpy (exp, pfile->out.base, len);
994 macro->exp.text = exp;
995 macro->traditional = 1;
1000 /* Store the text's length (unsigned int), the argument index
1001 (unsigned short, base 1) and then the text. */
1002 size_t blen = BLOCK_LEN (len);
1003 struct block *block;
1005 if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
1006 _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
1008 exp = BUFF_FRONT (pfile->a_buff);
1009 block = (struct block *) (exp + macro->count);
1010 macro->exp.text = exp;
1011 macro->traditional = 1;
1013 /* Write out the block information. */
1014 block->text_len = len;
1015 block->arg_index = arg_index;
1016 memcpy (block->text, pfile->out.base, len);
1018 /* Lex the rest into the start of the output buffer. */
1019 pfile->out.cur = pfile->out.base;
1021 macro->count += blen;
1023 /* If we've finished, commit the memory. */
1025 BUFF_FRONT (pfile->a_buff) += macro->count;
1029 /* Analyze and save the replacement text of a macro. Returns true on
1032 _cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
1036 cpp_context *context = pfile->context;
1038 /* The context has not been set up for command line defines, and CUR
1039 has not been updated for the macro name for in-file defines. */
1040 pfile->out.cur = pfile->out.base;
1041 CUR (context) = pfile->buffer->cur;
1042 RLIMIT (context) = pfile->buffer->rlimit;
1043 check_output_buffer (pfile, RLIMIT (context) - CUR (context));
1045 /* Is this a function-like macro? */
1046 if (* CUR (context) == '(')
1048 bool ok = scan_parameters (pfile, macro);
1050 /* Remember the params so we can clear NODE_MACRO_ARG flags. */
1051 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1053 /* Setting macro to NULL indicates an error occurred, and
1054 prevents unnecessary work in _cpp_scan_out_logical_line. */
1059 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1060 macro->fun_like = 1;
1064 /* Skip leading whitespace in the replacement text. */
1066 = skip_whitespace (pfile, CUR (context),
1067 CPP_OPTION (pfile, discard_comments_in_macro_exp));
1069 pfile->state.prevent_expansion++;
1070 _cpp_scan_out_logical_line (pfile, macro);
1071 pfile->state.prevent_expansion--;
1076 /* Skip trailing white space. */
1077 cur = pfile->out.base;
1078 limit = pfile->out.cur;
1079 while (limit > cur && is_space (limit[-1]))
1081 pfile->out.cur = limit;
1082 save_replacement_text (pfile, macro, 0);
1087 /* Copy SRC of length LEN to DEST, but convert all contiguous
1088 whitespace to a single space, provided it is not in quotes. The
1089 quote currently in effect is pointed to by PQUOTE, and is updated
1090 by the function. Returns the number of bytes copied. */
1092 canonicalize_text (uchar *dest, const uchar *src, size_t len, uchar *pquote)
1094 uchar *orig_dest = dest;
1095 uchar quote = *pquote;
1099 if (is_space (*src) && !quote)
1103 while (len && is_space (*src));
1108 if (*src == '\'' || *src == '"')
1112 else if (quote == *src)
1115 *dest++ = *src++, len--;
1120 return dest - orig_dest;
1123 /* Returns true if MACRO1 and MACRO2 have expansions different other
1124 than in the form of their whitespace. */
1126 _cpp_expansions_different_trad (const cpp_macro *macro1,
1127 const cpp_macro *macro2)
1129 uchar *p1 = XNEWVEC (uchar, macro1->count + macro2->count);
1130 uchar *p2 = p1 + macro1->count;
1131 uchar quote1 = 0, quote2 = 0;
1135 if (macro1->paramc > 0)
1137 const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1142 struct block *b1 = (struct block *) exp1;
1143 struct block *b2 = (struct block *) exp2;
1145 if (b1->arg_index != b2->arg_index)
1148 len1 = canonicalize_text (p1, b1->text, b1->text_len, "e1);
1149 len2 = canonicalize_text (p2, b2->text, b2->text_len, "e2);
1150 if (len1 != len2 || memcmp (p1, p2, len1))
1152 if (b1->arg_index == 0)
1157 exp1 += BLOCK_LEN (b1->text_len);
1158 exp2 += BLOCK_LEN (b2->text_len);
1163 len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, "e1);
1164 len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, "e2);
1165 mismatch = (len1 != len2 || memcmp (p1, p2, len1));