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. */
21 #include "coretypes.h"
26 /* The replacement text of a function-like macro is stored as a
27 contiguous sequence of aligned blocks, each representing the text
28 between subsequent parameters.
30 Each block comprises the text between its surrounding parameters,
31 the length of that text, and the one-based index of the following
32 parameter. The final block in the replacement text is easily
33 recognizable as it has an argument index of zero. */
37 unsigned int text_len
;
38 unsigned short arg_index
;
42 #define BLOCK_HEADER_LEN offsetof (struct block, text)
43 #define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN))
45 /* Structure holding information about a function-like macro
49 /* Memory buffer holding the trad_arg array. */
52 /* An array of size the number of macro parameters + 1, containing
53 the offsets of the start of each macro argument in the output
54 buffer. The argument continues until the character before the
55 start of the next one. */
58 /* The hashnode of the macro. */
61 /* The offset of the macro name in the output buffer. */
64 /* The line the macro name appeared on. */
67 /* Zero-based index of argument being currently lexed. */
71 /* Lexing state. It is mostly used to prevent macro expansion. */
72 enum ls
{ls_none
= 0, /* Normal state. */
73 ls_fun_open
, /* When looking for '('. */
74 ls_fun_close
, /* When looking for ')'. */
75 ls_defined
, /* After defined. */
76 ls_defined_close
, /* Looking for ')' of defined(). */
77 ls_hash
, /* After # in preprocessor conditional. */
78 ls_predicate
, /* After the predicate, maybe paren? */
79 ls_answer
}; /* In answer to predicate. */
81 /* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c
82 from recognizing comments and directives during its lexing pass. */
84 static const uchar
*handle_newline
PARAMS ((cpp_reader
*, const uchar
*));
85 static const uchar
*skip_escaped_newlines
PARAMS ((cpp_reader
*,
87 static const uchar
*skip_whitespace
PARAMS ((cpp_reader
*, const uchar
*,
89 static cpp_hashnode
*lex_identifier
PARAMS ((cpp_reader
*, const uchar
*));
90 static const uchar
*copy_comment
PARAMS ((cpp_reader
*, const uchar
*, int));
91 static void scan_out_logical_line
PARAMS ((cpp_reader
*pfile
, cpp_macro
*));
92 static void check_output_buffer
PARAMS ((cpp_reader
*, size_t));
93 static void push_replacement_text
PARAMS ((cpp_reader
*, cpp_hashnode
*));
94 static bool scan_parameters
PARAMS ((cpp_reader
*, cpp_macro
*));
95 static bool recursive_macro
PARAMS ((cpp_reader
*, cpp_hashnode
*));
96 static void save_replacement_text
PARAMS ((cpp_reader
*, cpp_macro
*,
98 static void maybe_start_funlike
PARAMS ((cpp_reader
*, cpp_hashnode
*,
99 const uchar
*, struct fun_macro
*));
100 static void save_argument
PARAMS ((struct fun_macro
*, size_t));
101 static void replace_args_and_push
PARAMS ((cpp_reader
*, struct fun_macro
*));
102 static size_t canonicalize_text
PARAMS ((uchar
*, const uchar
*, size_t,
105 /* Ensures we have N bytes' space in the output buffer, and
106 reallocates it if not. */
108 check_output_buffer (pfile
, n
)
112 /* We might need two bytes to terminate an unterminated comment, and
113 one more to terminate the line with a NUL. */
116 if (n
> (size_t) (pfile
->out
.limit
- pfile
->out
.cur
))
118 size_t size
= pfile
->out
.cur
- pfile
->out
.base
;
119 size_t new_size
= (size
+ n
) * 3 / 2;
122 = (uchar
*) xrealloc (pfile
->out
.base
, new_size
);
123 pfile
->out
.limit
= pfile
->out
.base
+ new_size
;
124 pfile
->out
.cur
= pfile
->out
.base
+ size
;
128 /* To be called whenever a newline character is encountered in the
129 input file, at CUR. Handles DOS, Mac and Unix ends of line, and
130 increments pfile->line.
132 Returns a pointer the character after the newline sequence. */
134 handle_newline (pfile
, cur
)
139 if (cur
[0] + cur
[1] == '\r' + '\n')
144 /* CUR points to any character in the current context, not necessarily
145 a backslash. Advances CUR until all escaped newlines are skipped,
146 and returns the new position without updating the context.
148 Warns if a file buffer ends in an escaped newline. */
150 skip_escaped_newlines (pfile
, cur
)
154 const uchar
*orig_cur
= cur
;
156 while (*cur
== '\\' && is_vspace (cur
[1]))
157 cur
= handle_newline (pfile
, cur
+ 1);
159 if (cur
!= orig_cur
&& cur
== RLIMIT (pfile
->context
) && pfile
->buffer
->inc
)
160 cpp_error (pfile
, DL_PEDWARN
, "backslash-newline at end of file");
165 /* CUR points to the asterisk introducing a comment in the current
166 context. IN_DEFINE is true if we are in the replacement text of a
169 The asterisk and following comment is copied to the buffer pointed
170 to by pfile->out.cur, which must be of sufficient size.
171 Unterminated comments are diagnosed, and correctly terminated in
172 the output. pfile->out.cur is updated depending upon IN_DEFINE,
173 -C, -CC and pfile->state.in_directive.
175 Returns a pointer to the first character after the comment in the
178 copy_comment (pfile
, cur
, in_define
)
183 unsigned int from_line
= pfile
->line
;
184 const uchar
*limit
= RLIMIT (pfile
->context
);
185 uchar
*out
= pfile
->out
.cur
;
189 unsigned int c
= *cur
++;
194 /* An immediate slash does not terminate the comment. */
195 if (out
[-2] == '*' && out
- 2 > pfile
->out
.cur
)
198 if (*cur
== '*' && cur
[1] != '/'
199 && CPP_OPTION (pfile
, warn_comments
))
200 cpp_error_with_line (pfile
, DL_WARNING
, pfile
->line
, 0,
201 "\"/*\" within comment");
203 else if (is_vspace (c
))
205 cur
= handle_newline (pfile
, cur
- 1);
206 /* Canonicalize newline sequences and skip escaped ones. */
215 cpp_error_with_line (pfile
, DL_ERROR
, from_line
, 0, "unterminated comment");
220 /* Comments in directives become spaces so that tokens are properly
221 separated when the ISO preprocessor re-lexes the line. The
222 exception is #define. */
223 if (pfile
->state
.in_directive
)
227 if (CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
230 pfile
->out
.cur
= out
;
233 pfile
->out
.cur
[-1] = ' ';
235 else if (CPP_OPTION (pfile
, discard_comments
))
238 pfile
->out
.cur
= out
;
243 /* CUR points to any character in the input buffer. Skips over all
244 contiguous horizontal white space and NULs, including comments if
245 SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
246 character or the end of the current context. Escaped newlines are
249 The whitespace is copied verbatim to the output buffer, except that
250 comments are handled as described in copy_comment().
251 pfile->out.cur is updated.
253 Returns a pointer to the first character after the whitespace in
256 skip_whitespace (pfile
, cur
, skip_comments
)
261 uchar
*out
= pfile
->out
.cur
;
265 unsigned int c
= *cur
++;
268 if (is_nvspace (c
) && c
)
271 if (!c
&& cur
- 1 != RLIMIT (pfile
->context
))
274 if (c
== '/' && skip_comments
)
276 const uchar
*tmp
= skip_escaped_newlines (pfile
, cur
);
279 pfile
->out
.cur
= out
;
280 cur
= copy_comment (pfile
, tmp
, false /* in_define */);
281 out
= pfile
->out
.cur
;
287 if (c
== '\\' && is_vspace (*cur
))
289 cur
= skip_escaped_newlines (pfile
, cur
- 1);
296 pfile
->out
.cur
= out
;
300 /* Lexes and outputs an identifier starting at CUR, which is assumed
301 to point to a valid first character of an identifier. Returns
302 the hashnode, and updates out.cur. */
303 static cpp_hashnode
*
304 lex_identifier (pfile
, cur
)
309 uchar
*out
= pfile
->out
.cur
;
310 cpp_hashnode
*result
;
316 while (is_numchar (*cur
));
317 cur
= skip_escaped_newlines (pfile
, cur
);
319 while (is_numchar (*cur
));
321 CUR (pfile
->context
) = cur
;
322 len
= out
- pfile
->out
.cur
;
323 result
= (cpp_hashnode
*) ht_lookup (pfile
->hash_table
, pfile
->out
.cur
,
325 pfile
->out
.cur
= out
;
329 /* Overlays the true file buffer temporarily with text of length LEN
330 starting at START. The true buffer is restored upon calling
333 _cpp_overlay_buffer (pfile
, start
, len
)
338 cpp_buffer
*buffer
= pfile
->buffer
;
340 pfile
->overlaid_buffer
= buffer
;
341 buffer
->saved_cur
= buffer
->cur
;
342 buffer
->saved_rlimit
= buffer
->rlimit
;
345 buffer
->rlimit
= start
+ len
;
347 pfile
->saved_line
= pfile
->line
;
350 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
352 _cpp_remove_overlay (pfile
)
355 cpp_buffer
*buffer
= pfile
->overlaid_buffer
;
357 buffer
->cur
= buffer
->saved_cur
;
358 buffer
->rlimit
= buffer
->saved_rlimit
;
360 pfile
->line
= pfile
->saved_line
;
363 /* Reads a logical line into the output buffer. Returns TRUE if there
364 is more text left in the buffer. */
366 _cpp_read_logical_line_trad (pfile
)
371 if (pfile
->buffer
->cur
== pfile
->buffer
->rlimit
)
375 /* Don't pop the last buffer. */
376 if (pfile
->buffer
->prev
)
378 stop
= pfile
->buffer
->return_at_eof
;
379 _cpp_pop_buffer (pfile
);
386 scan_out_logical_line (pfile
, NULL
);
388 while (pfile
->state
.skipping
);
393 /* Set up state for finding the opening '(' of a function-like
396 maybe_start_funlike (pfile
, node
, start
, macro
)
400 struct fun_macro
*macro
;
402 unsigned int n
= node
->value
.macro
->paramc
+ 1;
405 _cpp_release_buff (pfile
, macro
->buff
);
406 macro
->buff
= _cpp_get_buff (pfile
, n
* sizeof (size_t));
407 macro
->args
= (size_t *) BUFF_FRONT (macro
->buff
);
409 macro
->offset
= start
- pfile
->out
.base
;
413 /* Save the OFFSET of the start of the next argument to MACRO. */
415 save_argument (macro
, offset
)
416 struct fun_macro
*macro
;
420 if (macro
->argc
<= macro
->node
->value
.macro
->paramc
)
421 macro
->args
[macro
->argc
] = offset
;
424 /* Copies the next logical line in the current buffer (starting at
425 buffer->cur) to the output buffer. The output is guaranteed to
426 terminate with a NUL character. buffer->cur is updated.
428 If MACRO is non-NULL, then we are scanning the replacement list of
429 MACRO, and we call save_replacement_text() every time we meet an
432 scan_out_logical_line (pfile
, macro
)
436 cpp_context
*context
;
439 struct fun_macro fmacro
;
440 unsigned int c
, paren_depth
= 0, quote
;
441 enum ls lex_state
= ls_none
;
448 header_ok
= pfile
->state
.angled_headers
;
449 CUR (pfile
->context
) = pfile
->buffer
->cur
;
450 RLIMIT (pfile
->context
) = pfile
->buffer
->rlimit
;
451 pfile
->out
.cur
= pfile
->out
.base
;
452 pfile
->out
.first_line
= pfile
->line
;
454 context
= pfile
->context
;
456 check_output_buffer (pfile
, RLIMIT (context
) - cur
);
457 out
= pfile
->out
.cur
;
464 /* Whitespace should "continue" out of the switch,
465 non-whitespace should "break" out of it. */
475 if (cur
- 1 != RLIMIT (context
))
478 /* If this is a macro's expansion, pop it. */
481 pfile
->out
.cur
= out
- 1;
482 _cpp_pop_context (pfile
);
486 /* Premature end of file. Fake a new line. */
488 if (!pfile
->buffer
->from_stage3
)
489 cpp_error (pfile
, DL_PEDWARN
, "no newline at end of file");
493 case '\r': case '\n':
494 cur
= handle_newline (pfile
, cur
- 1);
495 if ((lex_state
== ls_fun_open
|| lex_state
== ls_fun_close
)
496 && !pfile
->state
.in_directive
)
498 /* Newlines in arguments become a space, but we don't
499 clear any in-progress quote. */
500 if (lex_state
== ls_fun_close
)
524 if (is_vspace (*cur
))
527 cur
= skip_escaped_newlines (pfile
, cur
- 1);
532 /* Skip escaped quotes here, it's easier than above, but
533 take care to first skip escaped newlines. */
534 cur
= skip_escaped_newlines (pfile
, cur
);
535 if (*cur
== '\\' || *cur
== '"' || *cur
== '\'')
541 /* Traditional CPP does not recognize comments within
545 cur
= skip_escaped_newlines (pfile
, cur
);
548 pfile
->out
.cur
= out
;
549 cur
= copy_comment (pfile
, cur
, macro
!= 0);
550 out
= pfile
->out
.cur
;
557 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
558 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
559 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
560 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
562 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
563 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
564 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
565 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
567 if (!pfile
->state
.skipping
&& (quote
== 0 || macro
))
570 uchar
*out_start
= out
- 1;
572 pfile
->out
.cur
= out_start
;
573 node
= lex_identifier (pfile
, cur
- 1);
574 out
= pfile
->out
.cur
;
577 if (node
->type
== NT_MACRO
578 /* Should we expand for ls_answer? */
579 && (lex_state
== ls_none
|| lex_state
== ls_fun_open
)
580 && !pfile
->state
.prevent_expansion
)
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
;
589 fmacro
.line
= pfile
->line
;
592 else if (!recursive_macro (pfile
, node
))
594 /* Remove the object-like macro's name from the
595 output, and push its replacement text. */
596 pfile
->out
.cur
= out_start
;
597 push_replacement_text (pfile
, node
);
602 else if (macro
&& (node
->flags
& NODE_MACRO_ARG
) != 0)
604 /* Found a parameter in the replacement text of a
605 #define. Remove its name from the output. */
606 pfile
->out
.cur
= out_start
;
607 save_replacement_text (pfile
, macro
, node
->value
.arg_index
);
608 out
= pfile
->out
.base
;
610 else if (lex_state
== ls_hash
)
612 lex_state
= ls_predicate
;
615 else if (pfile
->state
.in_expression
616 && node
== pfile
->spec_nodes
.n_defined
)
618 lex_state
= ls_defined
;
628 if (lex_state
== ls_fun_open
)
630 if (recursive_macro (pfile
, fmacro
.node
))
634 lex_state
= ls_fun_close
;
636 out
= pfile
->out
.base
+ fmacro
.offset
;
637 fmacro
.args
[0] = fmacro
.offset
;
640 else if (lex_state
== ls_predicate
)
641 lex_state
= ls_answer
;
642 else if (lex_state
== ls_defined
)
643 lex_state
= ls_defined_close
;
648 if (quote
== 0 && lex_state
== ls_fun_close
&& paren_depth
== 1)
649 save_argument (&fmacro
, out
- pfile
->out
.base
);
656 if (lex_state
== ls_fun_close
&& paren_depth
== 0)
658 cpp_macro
*m
= fmacro
.node
->value
.macro
;
662 save_argument (&fmacro
, out
- pfile
->out
.base
);
664 /* A single zero-length argument is no argument. */
667 && out
== pfile
->out
.base
+ fmacro
.offset
+ 1)
670 if (_cpp_arguments_ok (pfile
, m
, fmacro
.node
, fmacro
.argc
))
672 /* Remove the macro's invocation from the
673 output, and push its replacement text. */
674 pfile
->out
.cur
= (pfile
->out
.base
677 replace_args_and_push (pfile
, &fmacro
);
681 else if (lex_state
== ls_answer
|| lex_state
== ls_defined_close
)
687 if (out
- 1 == pfile
->out
.base
688 /* A '#' from a macro doesn't start a directive. */
689 && !pfile
->context
->prev
690 && !pfile
->state
.in_directive
)
692 /* A directive. With the way _cpp_handle_directive
693 currently works, we only want to call it if either we
694 know the directive is OK, or we want it to fail and
695 be removed from the output. If we want it to be
696 passed through (the assembler case) then we must not
697 call _cpp_handle_directive. */
698 pfile
->out
.cur
= out
;
699 cur
= skip_whitespace (pfile
, cur
, true /* skip_comments */);
700 out
= pfile
->out
.cur
;
702 if (is_vspace (*cur
))
704 /* Null directive. Ignore it and don't invalidate
705 the MI optimization. */
706 out
= pfile
->out
.base
;
713 if (is_numstart (*cur
)
714 && CPP_OPTION (pfile
, lang
) != CLK_ASM
)
716 else if (is_idstart (*cur
))
717 /* Check whether we know this directive, but don't
719 do_it
= lex_identifier (pfile
, cur
)->is_directive
;
721 if (do_it
|| CPP_OPTION (pfile
, lang
) != CLK_ASM
)
723 /* This is a kludge. We want to have the ISO
724 preprocessor lex the next token. */
725 pfile
->buffer
->cur
= cur
;
726 _cpp_handle_directive (pfile
, false /* indented */);
727 /* #include changes pfile->buffer so we need to
728 update the limits of the current context. */
729 goto start_logical_line
;
734 if (pfile
->state
.in_expression
)
745 /* Non-whitespace disables MI optimization and stops treating
746 '<' as a quote in #include. */
748 if (!pfile
->state
.in_directive
)
749 pfile
->mi_valid
= false;
751 if (lex_state
== ls_none
)
754 /* Some of these transitions of state are syntax errors. The
755 ISO preprocessor will issue errors later. */
756 if (lex_state
== ls_fun_open
)
759 else if (lex_state
== ls_hash
760 || lex_state
== ls_predicate
761 || lex_state
== ls_defined
)
764 /* ls_answer and ls_defined_close keep going until ')'. */
769 pfile
->buffer
->cur
= cur
;
770 pfile
->out
.cur
= out
- 1;
772 _cpp_release_buff (pfile
, fmacro
.buff
);
774 if (lex_state
== ls_fun_close
)
775 cpp_error_with_line (pfile
, DL_ERROR
, fmacro
.line
, 0,
776 "unterminated argument list invoking macro \"%s\"",
777 NODE_NAME (fmacro
.node
));
780 /* Push a context holding the replacement text of the macro NODE on
781 the context stack. NODE is either object-like, or a function-like
782 macro with no arguments. */
784 push_replacement_text (pfile
, node
)
791 if (node
->flags
& NODE_BUILTIN
)
793 text
= _cpp_builtin_macro_text (pfile
, node
);
794 len
= ustrlen (text
);
798 cpp_macro
*macro
= node
->value
.macro
;
800 text
= macro
->exp
.text
;
804 _cpp_push_text_context (pfile
, node
, text
, len
);
807 /* Returns TRUE if traditional macro recursion is detected. */
809 recursive_macro (pfile
, node
)
813 bool recursing
= !!(node
->flags
& NODE_DISABLED
);
815 /* Object-like macros that are already expanding are necessarily
818 However, it is possible to have traditional function-like macros
819 that are not infinitely recursive but recurse to any given depth.
820 Further, it is easy to construct examples that get ever longer
821 until the point they stop recursing. So there is no easy way to
822 detect true recursion; instead we assume any expansion more than
823 20 deep since the first invocation of this macro must be
825 if (recursing
&& node
->value
.macro
->fun_like
)
828 cpp_context
*context
= pfile
->context
;
833 if (context
->macro
== node
&& depth
> 20)
835 context
= context
->prev
;
838 recursing
= context
!= NULL
;
842 cpp_error (pfile
, DL_ERROR
,
843 "detected recursion whilst expanding macro \"%s\"",
849 /* Return the length of the replacement text of a function-like or
850 object-like non-builtin macro. */
852 _cpp_replacement_text_len (macro
)
853 const cpp_macro
*macro
;
862 for (exp
= macro
->exp
.text
;;)
864 struct block
*b
= (struct block
*) exp
;
867 if (b
->arg_index
== 0)
869 len
+= NODE_LEN (macro
->params
[b
->arg_index
- 1]);
870 exp
+= BLOCK_LEN (b
->text_len
);
879 /* Copy the replacement text of MACRO to DEST, which must be of
880 sufficient size. It is not NUL-terminated. The next character is
883 _cpp_copy_replacement_text (macro
, dest
)
884 const cpp_macro
*macro
;
891 for (exp
= macro
->exp
.text
;;)
893 struct block
*b
= (struct block
*) exp
;
896 memcpy (dest
, b
->text
, b
->text_len
);
898 if (b
->arg_index
== 0)
900 param
= macro
->params
[b
->arg_index
- 1];
901 memcpy (dest
, NODE_NAME (param
), NODE_LEN (param
));
902 dest
+= NODE_LEN (param
);
903 exp
+= BLOCK_LEN (b
->text_len
);
908 memcpy (dest
, macro
->exp
.text
, macro
->count
);
909 dest
+= macro
->count
;
915 /* Push a context holding the replacement text of the macro NODE on
916 the context stack. NODE is either object-like, or a function-like
917 macro with no arguments. */
919 replace_args_and_push (pfile
, fmacro
)
921 struct fun_macro
*fmacro
;
923 cpp_macro
*macro
= fmacro
->node
->value
.macro
;
925 if (macro
->paramc
== 0)
926 push_replacement_text (pfile
, fmacro
->node
);
934 /* Calculate the length of the argument-replaced text. */
935 for (exp
= macro
->exp
.text
;;)
937 struct block
*b
= (struct block
*) exp
;
940 if (b
->arg_index
== 0)
942 len
+= (fmacro
->args
[b
->arg_index
]
943 - fmacro
->args
[b
->arg_index
- 1] - 1);
944 exp
+= BLOCK_LEN (b
->text_len
);
947 /* Allocate room for the expansion plus NUL. */
948 buff
= _cpp_get_buff (pfile
, len
+ 1);
950 /* Copy the expansion and replace arguments. */
951 p
= BUFF_FRONT (buff
);
952 for (exp
= macro
->exp
.text
;;)
954 struct block
*b
= (struct block
*) exp
;
957 memcpy (p
, b
->text
, b
->text_len
);
959 if (b
->arg_index
== 0)
961 arglen
= (fmacro
->args
[b
->arg_index
]
962 - fmacro
->args
[b
->arg_index
- 1] - 1);
963 memcpy (p
, pfile
->out
.base
+ fmacro
->args
[b
->arg_index
- 1],
966 exp
+= BLOCK_LEN (b
->text_len
);
971 _cpp_push_text_context (pfile
, fmacro
->node
, BUFF_FRONT (buff
), len
);
973 /* So we free buffer allocation when macro is left. */
974 pfile
->context
->buff
= buff
;
978 /* Read and record the parameters, if any, of a function-like macro
979 definition. Destroys pfile->out.cur.
981 Returns true on success, false on failure (syntax error or a
982 duplicate parameter). On success, CUR (pfile->context) is just
983 past the closing parenthesis. */
985 scan_parameters (pfile
, macro
)
989 const uchar
*cur
= CUR (pfile
->context
) + 1;
994 cur
= skip_whitespace (pfile
, cur
, true /* skip_comments */);
996 if (is_idstart (*cur
))
999 if (_cpp_save_parameter (pfile
, macro
, lex_identifier (pfile
, cur
)))
1001 cur
= skip_whitespace (pfile
, CUR (pfile
->context
),
1002 true /* skip_comments */);
1012 ok
= (*cur
== ')' && macro
->paramc
== 0);
1016 CUR (pfile
->context
) = cur
+ (*cur
== ')');
1021 /* Save the text from pfile->out.base to pfile->out.cur as
1022 the replacement text for the current macro, followed by argument
1023 ARG_INDEX, with zero indicating the end of the replacement
1026 save_replacement_text (pfile
, macro
, arg_index
)
1029 unsigned int arg_index
;
1031 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
1034 if (macro
->paramc
== 0)
1036 /* Object-like and function-like macros without parameters
1037 simply store their NUL-terminated replacement text. */
1038 exp
= _cpp_unaligned_alloc (pfile
, len
+ 1);
1039 memcpy (exp
, pfile
->out
.base
, len
);
1041 macro
->exp
.text
= exp
;
1046 /* Store the text's length (unsigned int), the argument index
1047 (unsigned short, base 1) and then the text. */
1048 size_t blen
= BLOCK_LEN (len
);
1049 struct block
*block
;
1051 if (macro
->count
+ blen
> BUFF_ROOM (pfile
->a_buff
))
1052 _cpp_extend_buff (pfile
, &pfile
->a_buff
, macro
->count
+ blen
);
1054 exp
= BUFF_FRONT (pfile
->a_buff
);
1055 block
= (struct block
*) (exp
+ macro
->count
);
1056 macro
->exp
.text
= exp
;
1058 /* Write out the block information. */
1059 block
->text_len
= len
;
1060 block
->arg_index
= arg_index
;
1061 memcpy (block
->text
, pfile
->out
.base
, len
);
1063 /* Lex the rest into the start of the output buffer. */
1064 pfile
->out
.cur
= pfile
->out
.base
;
1066 macro
->count
+= blen
;
1068 /* If we've finished, commit the memory. */
1070 BUFF_FRONT (pfile
->a_buff
) += macro
->count
;
1074 /* Analyze and save the replacement text of a macro. Returns true on
1077 _cpp_create_trad_definition (pfile
, macro
)
1083 cpp_context
*context
= pfile
->context
;
1085 /* The context has not been set up for command line defines, and CUR
1086 has not been updated for the macro name for in-file defines. */
1087 pfile
->out
.cur
= pfile
->out
.base
;
1088 CUR (context
) = pfile
->buffer
->cur
;
1089 RLIMIT (context
) = pfile
->buffer
->rlimit
;
1090 check_output_buffer (pfile
, RLIMIT (context
) - CUR (context
));
1092 /* Is this a function-like macro? */
1093 if (* CUR (context
) == '(')
1095 /* Setting macro to NULL indicates an error occurred, and
1096 prevents unnecessary work in scan_out_logical_line. */
1097 if (!scan_parameters (pfile
, macro
))
1101 /* Success. Commit the parameter array. */
1102 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
1103 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
1104 macro
->fun_like
= 1;
1108 /* Skip leading whitespace in the replacement text. */
1110 = skip_whitespace (pfile
, CUR (context
),
1111 CPP_OPTION (pfile
, discard_comments_in_macro_exp
));
1113 pfile
->state
.prevent_expansion
++;
1114 scan_out_logical_line (pfile
, macro
);
1115 pfile
->state
.prevent_expansion
--;
1120 /* Skip trailing white space. */
1121 cur
= pfile
->out
.base
;
1122 limit
= pfile
->out
.cur
;
1123 while (limit
> cur
&& is_space (limit
[-1]))
1125 pfile
->out
.cur
= limit
;
1126 save_replacement_text (pfile
, macro
, 0);
1131 /* Copy SRC of length LEN to DEST, but convert all contiguous
1132 whitespace to a single space, provided it is not in quotes. The
1133 quote currently in effect is pointed to by PQUOTE, and is updated
1134 by the function. Returns the number of bytes copied. */
1136 canonicalize_text (dest
, src
, len
, pquote
)
1142 uchar
*orig_dest
= dest
;
1143 uchar quote
= *pquote
;
1147 if (is_space (*src
) && !quote
)
1151 while (len
&& is_space (*src
));
1156 if (*src
== '\'' || *src
== '"')
1160 else if (quote
== *src
)
1163 *dest
++ = *src
++, len
--;
1168 return dest
- orig_dest
;
1171 /* Returns true if MACRO1 and MACRO2 have expansions different other
1172 than in the form of their whitespace. */
1174 _cpp_expansions_different_trad (macro1
, macro2
)
1175 const cpp_macro
*macro1
, *macro2
;
1177 uchar
*p1
= xmalloc (macro1
->count
+ macro2
->count
);
1178 uchar
*p2
= p1
+ macro1
->count
;
1179 uchar quote1
= 0, quote2
= 0;
1183 if (macro1
->paramc
> 0)
1185 const uchar
*exp1
= macro1
->exp
.text
, *exp2
= macro2
->exp
.text
;
1190 struct block
*b1
= (struct block
*) exp1
;
1191 struct block
*b2
= (struct block
*) exp2
;
1193 if (b1
->arg_index
!= b2
->arg_index
)
1196 len1
= canonicalize_text (p1
, b1
->text
, b1
->text_len
, "e1
);
1197 len2
= canonicalize_text (p2
, b2
->text
, b2
->text_len
, "e2
);
1198 if (len1
!= len2
|| memcmp (p1
, p2
, len1
))
1200 if (b1
->arg_index
== 0)
1205 exp1
+= BLOCK_LEN (b1
->text_len
);
1206 exp2
+= BLOCK_LEN (b2
->text_len
);
1211 len1
= canonicalize_text (p1
, macro1
->exp
.text
, macro1
->count
, "e1
);
1212 len2
= canonicalize_text (p2
, macro2
->exp
.text
, macro2
->count
, "e2
);
1213 mismatch
= (len1
!= len2
|| memcmp (p1
, p2
, len1
));