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_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
*,
81 static const uchar
*skip_whitespace
PARAMS ((cpp_reader
*, const uchar
*,
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
*,
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,
99 /* Ensures we have N bytes' space in the output buffer, and
100 reallocates it if not. */
102 check_output_buffer (pfile
, n
)
106 /* We might need two bytes to terminate an unterminated comment, and
107 one more to terminate the line with a NUL. */
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;
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. */
128 handle_newline (pfile
, cur
)
133 if (cur
[0] + cur
[1] == '\r' + '\n')
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. */
144 skip_escaped_newlines (pfile
, 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");
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
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
172 copy_comment (pfile
, cur
, 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
++;
188 /* An immediate slash does not terminate the comment. */
189 if (out
[-2] == '*' && out
- 2 > pfile
->out
.cur
)
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. */
209 cpp_error_with_line (pfile
, DL_ERROR
, from_line
, 0, "unterminated comment");
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
)
221 if (CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
224 pfile
->out
.cur
= out
;
227 pfile
->out
.cur
[-1] = ' ';
229 else if (CPP_OPTION (pfile
, discard_comments
))
232 pfile
->out
.cur
= out
;
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
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
250 skip_whitespace (pfile
, cur
, skip_comments
)
255 uchar
*out
= pfile
->out
.cur
;
259 unsigned int c
= *cur
++;
262 if (is_nvspace (c
) && c
)
265 if (!c
&& cur
!= RLIMIT (pfile
->context
))
268 if (*cur
== '/' && skip_comments
)
270 const uchar
*tmp
= skip_escaped_newlines (pfile
, cur
);
273 pfile
->out
.cur
= out
;
274 cur
= copy_comment (pfile
, tmp
, false /* in_define */);
275 out
= pfile
->out
.cur
;
281 if (c
== '\\' && is_vspace (*cur
))
283 cur
= skip_escaped_newlines (pfile
, cur
);
290 pfile
->out
.cur
= out
;
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
)
303 uchar
*out
= pfile
->out
.cur
;
304 cpp_hashnode
*result
;
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
,
319 pfile
->out
.cur
= out
;
323 /* Overlays the true file buffer temporarily with text of length LEN
324 starting at START. The true buffer is restored upon calling
327 _cpp_overlay_buffer (pfile
, start
, len
)
332 cpp_buffer
*buffer
= pfile
->buffer
;
334 pfile
->overlaid_buffer
= buffer
;
335 buffer
->saved_cur
= buffer
->cur
;
336 buffer
->saved_rlimit
= buffer
->rlimit
;
339 buffer
->rlimit
= start
+ len
;
341 pfile
->saved_line
= pfile
->line
;
344 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
346 _cpp_remove_overlay (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. */
360 _cpp_read_logical_line_trad (pfile
)
363 cpp_buffer
*buffer
= pfile
->buffer
;
367 if (buffer
->cur
== buffer
->rlimit
)
371 /* Don't pop the last buffer. */
374 stop
= buffer
->return_at_eof
;
375 _cpp_pop_buffer (pfile
);
376 buffer
= pfile
->buffer
;
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
);
394 /* Set up state for finding the opening '(' of a function-like
397 maybe_start_funlike (pfile
, node
, start
, macro
)
401 struct fun_macro
*macro
;
403 unsigned int n
= node
->value
.macro
->paramc
+ 1;
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
);
410 macro
->offset
= start
- pfile
->out
.base
;
413 pfile
->state
.parsing_args
= 1;
416 /* Save the OFFSET of the start of the next argument to MACRO. */
418 save_argument (macro
, offset
)
419 struct fun_macro
*macro
;
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
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
435 scan_out_logical_line (pfile
, macro
)
439 cpp_context
*context
;
442 struct fun_macro fmacro
;
443 unsigned int c
, paren_depth
= 0, quote
= 0;
444 enum ls lex_state
= ls_none
;
449 pfile
->out
.cur
= pfile
->out
.base
;
450 pfile
->out
.first_line
= pfile
->line
;
452 context
= pfile
->context
;
454 check_output_buffer (pfile
, RLIMIT (context
) - cur
);
455 out
= pfile
->out
.cur
;
462 /* Whitespace should "continue" out of the switch,
463 non-whitespace should "break" out of it. */
473 if (cur
- 1 != RLIMIT (context
))
476 /* If this is a macro's expansion, pop it. */
479 pfile
->out
.cur
= out
- 1;
480 _cpp_pop_context (pfile
);
484 /* Premature end of file. Fake a new line. */
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
));
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. */
506 if (pfile
->state
.angled_headers
&& !quote
)
510 if (pfile
->state
.angled_headers
&& c
== quote
)
512 pfile
->state
.angled_headers
= false;
526 if (is_vspace (*cur
))
529 cur
= skip_escaped_newlines (pfile
, cur
- 1);
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
== '\'')
543 /* Traditional CPP does not recognize comments within
547 cur
= skip_escaped_newlines (pfile
, cur
);
550 pfile
->out
.cur
= out
;
551 cur
= copy_comment (pfile
, cur
, macro
!= 0);
552 out
= pfile
->out
.cur
;
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':
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':
569 if (!pfile
->state
.skipping
&& (quote
== 0 || macro
))
572 uchar
*out_start
= out
- 1;
574 pfile
->out
.cur
= out_start
;
575 node
= lex_identifier (pfile
, cur
- 1);
576 out
= pfile
->out
.cur
;
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
;
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
);
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
;
612 else if (pfile
->state
.in_expression
613 && node
== pfile
->spec_nodes
.n_defined
)
615 lex_state
= ls_defined
;
625 if (lex_state
== ls_fun_macro
)
628 pfile
->state
.parsing_args
= 2;
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
;
641 if (quote
== 0 && pfile
->state
.parsing_args
== 2 && paren_depth
== 1)
642 save_argument (&fmacro
, out
- pfile
->out
.base
);
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. */
659 && out
== pfile
->out
.base
+ fmacro
.offset
+ 1)
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
669 replace_args_and_push (pfile
, &fmacro
);
673 else if (lex_state
== ls_answer
|| lex_state
== ls_defined_close
)
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
)
699 if (lex_state
== ls_none
)
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
)
708 pfile
->state
.parsing_args
= 0;
710 else if (lex_state
== ls_hash
711 || lex_state
== ls_predicate
712 || lex_state
== ls_defined
)
715 /* ls_answer and ls_defined_close keep going until ')'. */
721 pfile
->out
.cur
= out
- 1;
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. */
736 push_replacement_text (pfile
, 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. */
747 recursive_macro (pfile
, node
)
751 bool recursing
= node
->flags
& NODE_DISABLED
;
753 /* Object-like macros that are already expanding are necessarily
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
763 if (recursing
&& node
->value
.macro
->fun_like
)
766 cpp_context
*context
= pfile
->context
;
771 if (context
->macro
== node
&& depth
> 20)
773 context
= context
->prev
;
776 recursing
= context
!= NULL
;
780 cpp_error (pfile
, DL_ERROR
,
781 "detected recursion whilst expanding macro \"%s\"",
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. */
791 replace_args_and_push (pfile
, fmacro
)
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
);
806 /* Calculate the length of the argument-replaced text. */
807 for (exp
= macro
->exp
.text
;;)
809 struct block
*b
= (struct block
*) exp
;
812 if (b
->arg_index
== 0)
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
;
829 memcpy (p
, b
->text
, b
->text_len
);
831 if (b
->arg_index
== 0)
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],
838 exp
+= BLOCK_LEN (b
->text_len
);
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. */
857 scan_parameters (pfile
, macro
)
861 const uchar
*cur
= CUR (pfile
->context
) + 1;
866 cur
= skip_whitespace (pfile
, cur
, true /* skip_comments */);
868 if (is_idstart (*cur
))
871 if (_cpp_save_parameter (pfile
, macro
, lex_identifier (pfile
, cur
)))
873 cur
= skip_whitespace (pfile
, CUR (pfile
->context
),
874 true /* skip_comments */);
884 ok
= (*cur
== ')' && macro
->paramc
== 0);
888 CUR (pfile
->context
) = cur
+ (*cur
== ')');
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
898 save_replacement_text (pfile
, macro
, arg_index
)
901 unsigned int arg_index
;
903 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
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
);
913 macro
->exp
.text
= exp
;
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
);
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. */
942 BUFF_FRONT (pfile
->a_buff
) += macro
->count
;
946 /* Analyze and save the replacement text of a macro. Returns true on
949 _cpp_create_trad_definition (pfile
, macro
)
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
))
967 /* Success. Commit the parameter array. */
968 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
969 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
974 /* Skip leading whitespace in the replacement text. */
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
--;
986 /* Skip trailing white space. */
987 cur
= pfile
->out
.base
;
988 limit
= pfile
->out
.cur
;
989 while (limit
> cur
&& is_space (limit
[-1]))
991 pfile
->out
.cur
= limit
;
992 save_replacement_text (pfile
, macro
, 0);
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. */
1002 canonicalize_text (dest
, src
, len
, pquote
)
1008 uchar
*orig_dest
= dest
;
1009 uchar quote
= *pquote
;
1013 if (is_space (*src
) && !quote
)
1017 while (len
&& is_space (*src
));
1022 if (*src
== '\'' || *src
== '"')
1026 else if (quote
== *src
)
1029 *dest
++ = *src
++, len
--;
1034 return dest
- orig_dest
;
1037 /* Returns true if MACRO1 and MACRO2 have expansions different other
1038 than in the form of their whitespace. */
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
;
1049 if (macro1
->paramc
> 0)
1051 const uchar
*exp1
= macro1
->exp
.text
, *exp2
= macro2
->exp
.text
;
1056 struct block
*b1
= (struct block
*) exp1
;
1057 struct block
*b2
= (struct block
*) exp2
;
1059 if (b1
->arg_index
!= b2
->arg_index
)
1062 len1
= canonicalize_text (p1
, b1
->text
, b1
->text_len
, "e1
);
1063 len2
= canonicalize_text (p2
, b2
->text
, b2
->text_len
, "e2
);
1064 if (len1
!= len2
|| memcmp (p1
, p2
, len1
))
1066 if (b1
->arg_index
== 0)
1071 exp1
+= BLOCK_LEN (b1
->text_len
);
1072 exp2
+= BLOCK_LEN (b2
->text_len
);
1077 len1
= canonicalize_text (p1
, macro1
->exp
.text
, macro1
->count
, "e1
);
1078 len2
= canonicalize_text (p2
, macro2
->exp
.text
, macro2
->count
, "e2
);
1079 mismatch
= (len1
!= len2
|| memcmp (p1
, p2
, len1
));
1086 /* Prepare to be able to scan the current buffer. */
1088 _cpp_set_trad_context (pfile
)
1091 cpp_buffer
*buffer
= pfile
->buffer
;
1092 cpp_context
*context
= pfile
->context
;
1094 if (pfile
->context
->prev
)
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
));