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
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. */
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. */
35 unsigned int text_len;
36 unsigned short arg_index;
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
47 /* Memory buffer holding the trad_arg array. */
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. */
56 /* The hashnode of the macro. */
59 /* The offset of the macro name in the output buffer. */
62 /* Zero-based index of argument being currently lexed. */
66 /* Lexing state. It is mostly used to prevent macro expansion. */
67 enum ls {ls_none = 0, /* Normal state. */
68 ls_fun_open, /* When looking for '('. */
69 ls_fun_close, /* When looking for ')'. */
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. */
76 /* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c
77 from recognizing comments and directives during its lexing pass. */
79 static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *));
80 static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *,
82 static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *,
84 static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
85 static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *, int));
86 static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *));
87 static void check_output_buffer PARAMS ((cpp_reader *, size_t));
88 static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *));
89 static bool scan_parameters PARAMS ((cpp_reader *, cpp_macro *));
90 static bool recursive_macro PARAMS ((cpp_reader *, cpp_hashnode *));
91 static void save_replacement_text PARAMS ((cpp_reader *, cpp_macro *,
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 *));
97 static size_t canonicalize_text PARAMS ((uchar *, const uchar *, size_t,
100 /* Ensures we have N bytes' space in the output buffer, and
101 reallocates it if not. */
103 check_output_buffer (pfile, n)
107 /* We might need two bytes to terminate an unterminated comment, and
108 one more to terminate the line with a NUL. */
111 if (n > (size_t) (pfile->out.limit - pfile->out.cur))
113 size_t size = pfile->out.cur - pfile->out.base;
114 size_t new_size = (size + n) * 3 / 2;
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;
123 /* To be called whenever a newline character is encountered in the
124 input file, at CUR. Handles DOS, Mac and Unix ends of line, and
125 increments pfile->line.
127 Returns a pointer the character after the newline sequence. */
129 handle_newline (pfile, cur)
134 if (cur[0] + cur[1] == '\r' + '\n')
139 /* CUR points to any character in the buffer, not necessarily a
140 backslash. Advances CUR until all escaped newlines are skipped,
141 and returns the new position.
143 Warns if a file buffer ends in an escaped newline. */
145 skip_escaped_newlines (pfile, cur)
149 const uchar *orig_cur = cur;
151 while (*cur == '\\' && is_vspace (cur[1]))
152 cur = handle_newline (pfile, cur + 1);
154 if (cur != orig_cur && cur == RLIMIT (pfile->context) && pfile->buffer->inc)
155 cpp_error (pfile, DL_PEDWARN, "backslash-newline at end of file");
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
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.
170 Returns a pointer to the first character after the comment in the
173 copy_comment (pfile, cur, in_define)
178 unsigned int from_line = pfile->line;
179 const uchar *limit = RLIMIT (pfile->context);
180 uchar *out = pfile->out.cur;
184 unsigned int c = *cur++;
189 /* An immediate slash does not terminate the comment. */
190 if (out[-2] == '*' && out - 2 > pfile->out.cur)
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");
198 else if (is_vspace (c))
200 cur = handle_newline (pfile, cur - 1);
201 /* Canonicalize newline sequences and skip escaped ones. */
210 cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment");
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)
222 if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
225 pfile->out.cur = out;
228 pfile->out.cur[-1] = ' ';
230 else if (CPP_OPTION (pfile, discard_comments))
233 pfile->out.cur = out;
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
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.
248 Returns a pointer to the first character after the whitespace in
251 skip_whitespace (pfile, cur, skip_comments)
256 uchar *out = pfile->out.cur;
260 unsigned int c = *cur++;
263 if (is_nvspace (c) && c)
266 if (!c && cur - 1 != RLIMIT (pfile->context))
269 if (*cur == '/' && skip_comments)
271 const uchar *tmp = skip_escaped_newlines (pfile, cur);
274 pfile->out.cur = out;
275 cur = copy_comment (pfile, tmp, false /* in_define */);
276 out = pfile->out.cur;
282 if (c == '\\' && is_vspace (*cur))
284 cur = skip_escaped_newlines (pfile, cur);
291 pfile->out.cur = out;
295 /* Lexes and outputs an identifier starting at CUR, which is assumed
296 to point to a valid first character of an identifier. Returns
297 the hashnode, and updates out.cur. */
298 static cpp_hashnode *
299 lex_identifier (pfile, cur)
304 uchar *out = pfile->out.cur;
305 cpp_hashnode *result;
311 while (is_numchar (*cur));
312 cur = skip_escaped_newlines (pfile, cur);
314 while (is_numchar (*cur));
316 CUR (pfile->context) = cur;
317 len = out - pfile->out.cur;
318 result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
320 pfile->out.cur = out;
324 /* Overlays the true file buffer temporarily with text of length LEN
325 starting at START. The true buffer is restored upon calling
328 _cpp_overlay_buffer (pfile, start, len)
333 cpp_buffer *buffer = pfile->buffer;
335 pfile->overlaid_buffer = buffer;
336 buffer->saved_cur = buffer->cur;
337 buffer->saved_rlimit = buffer->rlimit;
340 buffer->rlimit = start + len;
342 pfile->saved_line = pfile->line;
345 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
347 _cpp_remove_overlay (pfile)
350 cpp_buffer *buffer = pfile->overlaid_buffer;
352 buffer->cur = buffer->saved_cur;
353 buffer->rlimit = buffer->saved_rlimit;
355 pfile->line = pfile->saved_line;
358 /* Reads a logical line into the output buffer. Returns TRUE if there
359 is more text left in the buffer. */
361 _cpp_read_logical_line_trad (pfile)
364 cpp_buffer *buffer = pfile->buffer;
368 if (buffer->cur == buffer->rlimit)
372 /* Don't pop the last buffer. */
375 stop = buffer->return_at_eof;
376 _cpp_pop_buffer (pfile);
377 buffer = pfile->buffer;
384 CUR (pfile->context) = buffer->cur;
385 RLIMIT (pfile->context) = buffer->rlimit;
386 scan_out_logical_line (pfile, NULL);
387 buffer = pfile->buffer;
388 buffer->cur = CUR (pfile->context);
390 while (pfile->state.skipping);
395 /* Set up state for finding the opening '(' of a function-like
398 maybe_start_funlike (pfile, node, start, macro)
402 struct fun_macro *macro;
404 unsigned int n = node->value.macro->paramc + 1;
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);
411 macro->offset = start - pfile->out.base;
415 /* Save the OFFSET of the start of the next argument to MACRO. */
417 save_argument (macro, offset)
418 struct fun_macro *macro;
422 if (macro->argc <= macro->node->value.macro->paramc)
423 macro->args[macro->argc] = offset;
426 /* Copies the next logical line in the current buffer to the output
427 buffer. The output is guaranteed to terminate with a NUL
430 If MACRO is non-NULL, then we are scanning the replacement list of
431 MACRO, and we call save_replacement_text() every time we meet an
434 scan_out_logical_line (pfile, macro)
438 cpp_context *context;
441 struct fun_macro fmacro;
442 unsigned int c, paren_depth = 0, quote = 0;
443 enum ls lex_state = ls_none;
448 pfile->out.cur = pfile->out.base;
449 pfile->out.first_line = pfile->line;
451 context = pfile->context;
453 check_output_buffer (pfile, RLIMIT (context) - cur);
454 out = pfile->out.cur;
461 /* Whitespace should "continue" out of the switch,
462 non-whitespace should "break" out of it. */
472 if (cur - 1 != RLIMIT (context))
475 /* If this is a macro's expansion, pop it. */
478 pfile->out.cur = out - 1;
479 _cpp_pop_context (pfile);
483 /* Premature end of file. Fake a new line. */
485 if (!pfile->buffer->from_stage3)
486 cpp_error (pfile, DL_PEDWARN, "no newline at end of file");
490 case '\r': case '\n':
491 cur = handle_newline (pfile, cur - 1);
492 if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
493 && !pfile->state.in_directive)
495 /* Newlines in arguments become a space. */
496 if (lex_state == ls_fun_close)
503 if (pfile->state.angled_headers && !quote)
507 if (pfile->state.angled_headers && c == quote)
509 pfile->state.angled_headers = false;
523 if (is_vspace (*cur))
526 cur = skip_escaped_newlines (pfile, cur - 1);
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 == '\'')
540 /* Traditional CPP does not recognize comments within
544 cur = skip_escaped_newlines (pfile, cur);
547 pfile->out.cur = out;
548 cur = copy_comment (pfile, cur, macro != 0);
549 out = pfile->out.cur;
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':
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':
566 if (!pfile->state.skipping && (quote == 0 || macro))
569 uchar *out_start = out - 1;
571 pfile->out.cur = out_start;
572 node = lex_identifier (pfile, cur - 1);
573 out = pfile->out.cur;
576 if (node->type == NT_MACRO
577 /* Should we expand for ls_answer? */
578 && (lex_state == ls_none || lex_state == ls_fun_open)
579 && !pfile->state.prevent_expansion
580 && !recursive_macro (pfile, node))
582 /* Macros invalidate MI optimization. */
583 pfile->mi_valid = false;
584 if (! (node->flags & NODE_BUILTIN)
585 && node->value.macro->fun_like)
587 maybe_start_funlike (pfile, node, out_start, &fmacro);
588 lex_state = ls_fun_open;
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);
601 else if (macro && node->arg_index)
603 /* Found a parameter in the replacement text of a
604 #define. Remove its name from the output. */
605 pfile->out.cur = out_start;
606 save_replacement_text (pfile, macro, node->arg_index);
607 out = pfile->out.base;
609 else if (lex_state == ls_hash)
611 lex_state = ls_predicate;
614 else if (pfile->state.in_expression
615 && node == pfile->spec_nodes.n_defined)
617 lex_state = ls_defined;
627 if (lex_state == ls_fun_open)
629 lex_state = ls_fun_close;
631 out = pfile->out.base + fmacro.offset;
632 fmacro.args[0] = fmacro.offset;
634 else if (lex_state == ls_predicate)
635 lex_state = ls_answer;
636 else if (lex_state == ls_defined)
637 lex_state = ls_defined_close;
642 if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
643 save_argument (&fmacro, out - pfile->out.base);
650 if (lex_state == ls_fun_close && paren_depth == 0)
652 cpp_macro *m = fmacro.node->value.macro;
655 save_argument (&fmacro, out - pfile->out.base);
657 /* A single zero-length argument is no argument. */
660 && out == pfile->out.base + fmacro.offset + 1)
663 if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
665 /* Remove the macro's invocation from the
666 output, and push its replacement text. */
667 pfile->out.cur = (pfile->out.base
670 replace_args_and_push (pfile, &fmacro);
674 else if (lex_state == ls_answer || lex_state == ls_defined_close)
680 /* At start of a line it's a directive. */
681 if (out - 1 == pfile->out.base && !pfile->state.in_directive)
683 /* This is a kludge. We want to have the ISO
684 preprocessor lex the next token. */
685 pfile->buffer->cur = cur;
686 if (_cpp_handle_directive (pfile, false /* indented */))
687 goto start_logical_line;
689 if (pfile->state.in_expression)
700 if (lex_state == ls_none)
703 /* Some of these transitions of state are syntax errors. The
704 ISO preprocessor will issue errors later. */
705 if (lex_state == ls_fun_open)
708 else if (lex_state == ls_hash
709 || lex_state == ls_predicate
710 || lex_state == ls_defined)
713 /* ls_answer and ls_defined_close keep going until ')'. */
719 pfile->out.cur = out - 1;
721 _cpp_release_buff (pfile, fmacro.buff);
723 if (lex_state == ls_fun_close)
724 cpp_error (pfile, DL_ERROR,
725 "unterminated argument list invoking macro \"%s\"",
726 NODE_NAME (fmacro.node));
729 /* Push a context holding the replacement text of the macro NODE on
730 the context stack. NODE is either object-like, or a function-like
731 macro with no arguments. */
733 push_replacement_text (pfile, node)
740 if (node->flags & NODE_BUILTIN)
742 text = _cpp_builtin_macro_text (pfile, node);
743 len = ustrlen (text);
747 cpp_macro *macro = node->value.macro;
748 text = macro->exp.text;
752 _cpp_push_text_context (pfile, node, text, len);
755 /* Returns TRUE if traditional macro recursion is detected. */
757 recursive_macro (pfile, node)
761 bool recursing = node->flags & NODE_DISABLED;
763 /* Object-like macros that are already expanding are necessarily
766 However, it is possible to have traditional function-like macros
767 that are not infinitely recursive but recurse to any given depth.
768 Further, it is easy to construct examples that get ever longer
769 until the point they stop recursing. So there is no easy way to
770 detect true recursion; instead we assume any expansion more than
771 20 deep since the first invocation of this macro must be
773 if (recursing && node->value.macro->fun_like)
776 cpp_context *context = pfile->context;
781 if (context->macro == node && depth > 20)
783 context = context->prev;
786 recursing = context != NULL;
790 cpp_error (pfile, DL_ERROR,
791 "detected recursion whilst expanding macro \"%s\"",
797 /* Return the length of the replacement text of a function-like or
798 object-like non-builtin macro. */
800 _cpp_replacement_text_len (macro)
801 const cpp_macro *macro;
810 for (exp = macro->exp.text;;)
812 struct block *b = (struct block *) exp;
815 if (b->arg_index == 0)
817 len += NODE_LEN (macro->params[b->arg_index - 1]);
818 exp += BLOCK_LEN (b->text_len);
827 /* Copy the replacement text of MACRO to DEST, which must be of
828 sufficient size. It is not NUL-terminated. The next character is
831 _cpp_copy_replacement_text (macro, dest)
832 const cpp_macro *macro;
839 for (exp = macro->exp.text;;)
841 struct block *b = (struct block *) exp;
844 memcpy (dest, b->text, b->text_len);
846 if (b->arg_index == 0)
848 param = macro->params[b->arg_index - 1];
849 memcpy (dest, NODE_NAME (param), NODE_LEN (param));
850 dest += NODE_LEN (param);
851 exp += BLOCK_LEN (b->text_len);
856 memcpy (dest, macro->exp.text, macro->count);
857 dest += macro->count;
863 /* Push a context holding the replacement text of the macro NODE on
864 the context stack. NODE is either object-like, or a function-like
865 macro with no arguments. */
867 replace_args_and_push (pfile, fmacro)
869 struct fun_macro *fmacro;
871 cpp_macro *macro = fmacro->node->value.macro;
873 if (macro->paramc == 0)
874 push_replacement_text (pfile, fmacro->node);
882 /* Calculate the length of the argument-replaced text. */
883 for (exp = macro->exp.text;;)
885 struct block *b = (struct block *) exp;
888 if (b->arg_index == 0)
890 len += (fmacro->args[b->arg_index]
891 - fmacro->args[b->arg_index - 1] - 1);
892 exp += BLOCK_LEN (b->text_len);
895 /* Allocate room for the expansion plus NUL. */
896 buff = _cpp_get_buff (pfile, len + 1);
898 /* Copy the expansion and replace arguments. */
899 p = BUFF_FRONT (buff);
900 for (exp = macro->exp.text;;)
902 struct block *b = (struct block *) exp;
905 memcpy (p, b->text, b->text_len);
907 if (b->arg_index == 0)
909 arglen = (fmacro->args[b->arg_index]
910 - fmacro->args[b->arg_index - 1] - 1);
911 memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
914 exp += BLOCK_LEN (b->text_len);
919 _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
921 /* So we free buffer allocation when macro is left. */
922 pfile->context->buff = buff;
926 /* Read and record the parameters, if any, of a function-like macro
927 definition. Destroys pfile->out.cur.
929 Returns true on success, false on failure (syntax error or a
930 duplicate parameter). On success, CUR (pfile->context) is just
931 past the closing parenthesis. */
933 scan_parameters (pfile, macro)
937 const uchar *cur = CUR (pfile->context) + 1;
942 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
944 if (is_idstart (*cur))
947 if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
949 cur = skip_whitespace (pfile, CUR (pfile->context),
950 true /* skip_comments */);
960 ok = (*cur == ')' && macro->paramc == 0);
964 CUR (pfile->context) = cur + (*cur == ')');
969 /* Save the text from pfile->out.base to pfile->out.cur as
970 the replacement text for the current macro, followed by argument
971 ARG_INDEX, with zero indicating the end of the replacement
974 save_replacement_text (pfile, macro, arg_index)
977 unsigned int arg_index;
979 size_t len = pfile->out.cur - pfile->out.base;
982 if (macro->paramc == 0)
984 /* Object-like and function-like macros without parameters
985 simply store their NUL-terminated replacement text. */
986 exp = _cpp_unaligned_alloc (pfile, len + 1);
987 memcpy (exp, pfile->out.base, len);
989 macro->exp.text = exp;
994 /* Store the text's length (unsigned int), the argument index
995 (unsigned short, base 1) and then the text. */
996 size_t blen = BLOCK_LEN (len);
999 if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
1000 _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
1002 exp = BUFF_FRONT (pfile->a_buff);
1003 block = (struct block *) (exp + macro->count);
1004 macro->exp.text = exp;
1006 /* Write out the block information. */
1007 block->text_len = len;
1008 block->arg_index = arg_index;
1009 memcpy (block->text, pfile->out.base, len);
1011 /* Lex the rest into the start of the output buffer. */
1012 pfile->out.cur = pfile->out.base;
1014 macro->count += blen;
1016 /* If we've finished, commit the memory. */
1018 BUFF_FRONT (pfile->a_buff) += macro->count;
1022 /* Analyze and save the replacement text of a macro. Returns true on
1025 _cpp_create_trad_definition (pfile, macro)
1032 CUR (pfile->context) = pfile->buffer->cur;
1034 /* Is this a function-like macro? */
1035 if (* CUR (pfile->context) == '(')
1037 /* Setting macro to NULL indicates an error occurred, and
1038 prevents unnecessary work in scan_out_logical_line. */
1039 if (!scan_parameters (pfile, macro))
1043 /* Success. Commit the parameter array. */
1044 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1045 BUFF_FRONT (pfile->a_buff) = (uchar *) ¯o->params[macro->paramc];
1046 macro->fun_like = 1;
1050 /* Skip leading whitespace in the replacement text. */
1051 CUR (pfile->context)
1052 = skip_whitespace (pfile, CUR (pfile->context),
1053 CPP_OPTION (pfile, discard_comments_in_macro_exp));
1055 pfile->state.prevent_expansion++;
1056 scan_out_logical_line (pfile, macro);
1057 pfile->state.prevent_expansion--;
1062 /* Skip trailing white space. */
1063 cur = pfile->out.base;
1064 limit = pfile->out.cur;
1065 while (limit > cur && is_space (limit[-1]))
1067 pfile->out.cur = limit;
1068 save_replacement_text (pfile, macro, 0);
1073 /* Copy SRC of length LEN to DEST, but convert all contiguous
1074 whitespace to a single space, provided it is not in quotes. The
1075 quote currently in effect is pointed to by PQUOTE, and is updated
1076 by the function. Returns the number of bytes copied. */
1078 canonicalize_text (dest, src, len, pquote)
1084 uchar *orig_dest = dest;
1085 uchar quote = *pquote;
1089 if (is_space (*src) && !quote)
1093 while (len && is_space (*src));
1098 if (*src == '\'' || *src == '"')
1102 else if (quote == *src)
1105 *dest++ = *src++, len--;
1110 return dest - orig_dest;
1113 /* Returns true if MACRO1 and MACRO2 have expansions different other
1114 than in the form of their whitespace. */
1116 _cpp_expansions_different_trad (macro1, macro2)
1117 const cpp_macro *macro1, *macro2;
1119 uchar *p1 = xmalloc (macro1->count + macro2->count);
1120 uchar *p2 = p1 + macro1->count;
1121 uchar quote1 = 0, quote2;
1125 if (macro1->paramc > 0)
1127 const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1132 struct block *b1 = (struct block *) exp1;
1133 struct block *b2 = (struct block *) exp2;
1135 if (b1->arg_index != b2->arg_index)
1138 len1 = canonicalize_text (p1, b1->text, b1->text_len, "e1);
1139 len2 = canonicalize_text (p2, b2->text, b2->text_len, "e2);
1140 if (len1 != len2 || memcmp (p1, p2, len1))
1142 if (b1->arg_index == 0)
1147 exp1 += BLOCK_LEN (b1->text_len);
1148 exp2 += BLOCK_LEN (b2->text_len);
1153 len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, "e1);
1154 len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, "e2);
1155 mismatch = (len1 != len2 || memcmp (p1, p2, len1));
1162 /* Prepare to be able to scan the current buffer. */
1164 _cpp_set_trad_context (pfile)
1167 cpp_buffer *buffer = pfile->buffer;
1168 cpp_context *context = pfile->context;
1170 if (pfile->context->prev)
1173 pfile->out.cur = pfile->out.base;
1174 CUR (context) = buffer->cur;
1175 RLIMIT (context) = buffer->rlimit;
1176 check_output_buffer (pfile, RLIMIT (context) - CUR (context));