1 /* CPP Library - traditional lexical analysis and macro expansion.
2 Copyright (C) 2002-2015 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 3, 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; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
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.
28 Each block comprises the text between its surrounding parameters,
29 the length of that text, and the one-based index of the following
30 parameter. The final block in the replacement text is easily
31 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 /* The line the macro name appeared on. */
65 /* Zero-based index of argument being currently lexed. */
69 /* Lexing state. It is mostly used to prevent macro expansion. */
70 enum ls
{ls_none
= 0, /* Normal state. */
71 ls_fun_open
, /* When looking for '('. */
72 ls_fun_close
, /* When looking for ')'. */
73 ls_defined
, /* After defined. */
74 ls_defined_close
, /* Looking for ')' of defined(). */
75 ls_hash
, /* After # in preprocessor conditional. */
76 ls_predicate
, /* After the predicate, maybe paren? */
77 ls_answer
, /* In answer to predicate. */
78 ls_has_include
, /* After __has_include__. */
79 ls_has_include_close
}; /* Looking for ')' of __has_include__. */
81 /* Lexing TODO: Maybe handle space in escaped newlines. Stop lex.c
82 from recognizing comments and directives during its lexing pass. */
84 static const uchar
*skip_whitespace (cpp_reader
*, const uchar
*, int);
85 static cpp_hashnode
*lex_identifier (cpp_reader
*, const uchar
*);
86 static const uchar
*copy_comment (cpp_reader
*, const uchar
*, int);
87 static void check_output_buffer (cpp_reader
*, size_t);
88 static void push_replacement_text (cpp_reader
*, cpp_hashnode
*);
89 static bool scan_parameters (cpp_reader
*, cpp_macro
*);
90 static bool recursive_macro (cpp_reader
*, cpp_hashnode
*);
91 static void save_replacement_text (cpp_reader
*, cpp_macro
*, unsigned int);
92 static void maybe_start_funlike (cpp_reader
*, cpp_hashnode
*, const uchar
*,
94 static void save_argument (struct fun_macro
*, size_t);
95 static void replace_args_and_push (cpp_reader
*, struct fun_macro
*);
96 static size_t canonicalize_text (uchar
*, const uchar
*, size_t, uchar
*);
98 /* Ensures we have N bytes' space in the output buffer, and
99 reallocates it if not. */
101 check_output_buffer (cpp_reader
*pfile
, size_t n
)
103 /* We might need two bytes to terminate an unterminated comment, and
104 one more to terminate the line with a NUL. */
107 if (n
> (size_t) (pfile
->out
.limit
- pfile
->out
.cur
))
109 size_t size
= pfile
->out
.cur
- pfile
->out
.base
;
110 size_t new_size
= (size
+ n
) * 3 / 2;
112 pfile
->out
.base
= XRESIZEVEC (unsigned char, pfile
->out
.base
, new_size
);
113 pfile
->out
.limit
= pfile
->out
.base
+ new_size
;
114 pfile
->out
.cur
= pfile
->out
.base
+ size
;
118 /* Skip a C-style block comment in a macro as a result of -CC.
119 Buffer->cur points to the initial asterisk of the comment. */
121 skip_macro_block_comment (cpp_reader
*pfile
)
123 const uchar
*cur
= pfile
->buffer
->cur
;
129 /* People like decorating comments with '*', so check for '/'
130 instead for efficiency. */
131 while(! (*cur
++ == '/' && cur
[-2] == '*') )
134 pfile
->buffer
->cur
= cur
;
137 /* CUR points to the asterisk introducing a comment in the current
138 context. IN_DEFINE is true if we are in the replacement text of a
141 The asterisk and following comment is copied to the buffer pointed
142 to by pfile->out.cur, which must be of sufficient size.
143 Unterminated comments are diagnosed, and correctly terminated in
144 the output. pfile->out.cur is updated depending upon IN_DEFINE,
145 -C, -CC and pfile->state.in_directive.
147 Returns a pointer to the first character after the comment in the
150 copy_comment (cpp_reader
*pfile
, const uchar
*cur
, int in_define
)
152 bool unterminated
, copy
= false;
153 source_location src_loc
= pfile
->line_table
->highest_line
;
154 cpp_buffer
*buffer
= pfile
->buffer
;
157 if (pfile
->context
->prev
)
158 unterminated
= false, skip_macro_block_comment (pfile
);
160 unterminated
= _cpp_skip_block_comment (pfile
);
163 cpp_error_with_line (pfile
, CPP_DL_ERROR
, src_loc
, 0,
164 "unterminated comment");
166 /* Comments in directives become spaces so that tokens are properly
167 separated when the ISO preprocessor re-lexes the line. The
168 exception is #define. */
169 if (pfile
->state
.in_directive
)
173 if (CPP_OPTION (pfile
, discard_comments_in_macro_exp
))
179 pfile
->out
.cur
[-1] = ' ';
181 else if (CPP_OPTION (pfile
, discard_comments
))
188 size_t len
= (size_t) (buffer
->cur
- cur
);
189 memcpy (pfile
->out
.cur
, cur
, len
);
190 pfile
->out
.cur
+= len
;
193 *pfile
->out
.cur
++ = '*';
194 *pfile
->out
.cur
++ = '/';
201 /* CUR points to any character in the input buffer. Skips over all
202 contiguous horizontal white space and NULs, including comments if
203 SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
204 character or the end of the current context. Escaped newlines are
207 The whitespace is copied verbatim to the output buffer, except that
208 comments are handled as described in copy_comment().
209 pfile->out.cur is updated.
211 Returns a pointer to the first character after the whitespace in
214 skip_whitespace (cpp_reader
*pfile
, const uchar
*cur
, int skip_comments
)
216 uchar
*out
= pfile
->out
.cur
;
220 unsigned int c
= *cur
++;
226 if (c
== '/' && *cur
== '*' && skip_comments
)
228 pfile
->out
.cur
= out
;
229 cur
= copy_comment (pfile
, cur
, false /* in_define */);
230 out
= pfile
->out
.cur
;
238 pfile
->out
.cur
= out
;
242 /* Lexes and outputs an identifier starting at CUR, which is assumed
243 to point to a valid first character of an identifier. Returns
244 the hashnode, and updates out.cur. */
245 static cpp_hashnode
*
246 lex_identifier (cpp_reader
*pfile
, const uchar
*cur
)
249 uchar
*out
= pfile
->out
.cur
;
250 cpp_hashnode
*result
;
254 while (is_numchar (*cur
));
256 CUR (pfile
->context
) = cur
;
257 len
= out
- pfile
->out
.cur
;
258 result
= CPP_HASHNODE (ht_lookup (pfile
->hash_table
, pfile
->out
.cur
,
260 pfile
->out
.cur
= out
;
264 /* Overlays the true file buffer temporarily with text of length LEN
265 starting at START. The true buffer is restored upon calling
268 _cpp_overlay_buffer (cpp_reader
*pfile
, const uchar
*start
, size_t len
)
270 cpp_buffer
*buffer
= pfile
->buffer
;
272 pfile
->overlaid_buffer
= buffer
;
273 pfile
->saved_cur
= buffer
->cur
;
274 pfile
->saved_rlimit
= buffer
->rlimit
;
275 pfile
->saved_line_base
= buffer
->next_line
;
276 buffer
->need_line
= false;
279 buffer
->line_base
= start
;
280 buffer
->rlimit
= start
+ len
;
283 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
285 _cpp_remove_overlay (cpp_reader
*pfile
)
287 cpp_buffer
*buffer
= pfile
->overlaid_buffer
;
289 buffer
->cur
= pfile
->saved_cur
;
290 buffer
->rlimit
= pfile
->saved_rlimit
;
291 buffer
->line_base
= pfile
->saved_line_base
;
292 buffer
->need_line
= true;
294 pfile
->overlaid_buffer
= NULL
;
297 /* Reads a logical line into the output buffer. Returns TRUE if there
298 is more text left in the buffer. */
300 _cpp_read_logical_line_trad (cpp_reader
*pfile
)
304 if (pfile
->buffer
->need_line
&& !_cpp_get_fresh_line (pfile
))
307 while (!_cpp_scan_out_logical_line (pfile
, NULL
) || pfile
->state
.skipping
);
309 return pfile
->buffer
!= NULL
;
312 /* Set up state for finding the opening '(' of a function-like
315 maybe_start_funlike (cpp_reader
*pfile
, cpp_hashnode
*node
, const uchar
*start
, struct fun_macro
*macro
)
317 unsigned int n
= node
->value
.macro
->paramc
+ 1;
320 _cpp_release_buff (pfile
, macro
->buff
);
321 macro
->buff
= _cpp_get_buff (pfile
, n
* sizeof (size_t));
322 macro
->args
= (size_t *) BUFF_FRONT (macro
->buff
);
324 macro
->offset
= start
- pfile
->out
.base
;
328 /* Save the OFFSET of the start of the next argument to MACRO. */
330 save_argument (struct fun_macro
*macro
, size_t offset
)
333 if (macro
->argc
<= macro
->node
->value
.macro
->paramc
)
334 macro
->args
[macro
->argc
] = offset
;
337 /* Copies the next logical line in the current buffer (starting at
338 buffer->cur) to the output buffer. The output is guaranteed to
339 terminate with a NUL character. buffer->cur is updated.
341 If MACRO is non-NULL, then we are scanning the replacement list of
342 MACRO, and we call save_replacement_text() every time we meet an
345 _cpp_scan_out_logical_line (cpp_reader
*pfile
, cpp_macro
*macro
)
348 cpp_context
*context
;
351 struct fun_macro fmacro
;
352 unsigned int c
, paren_depth
= 0, quote
;
353 enum ls lex_state
= ls_none
;
355 const uchar
*start_of_input_line
;
365 header_ok
= pfile
->state
.angled_headers
;
366 CUR (pfile
->context
) = pfile
->buffer
->cur
;
367 RLIMIT (pfile
->context
) = pfile
->buffer
->rlimit
;
368 pfile
->out
.cur
= pfile
->out
.base
;
369 pfile
->out
.first_line
= pfile
->line_table
->highest_line
;
370 /* start_of_input_line is needed to make sure that directives really,
371 really start at the first character of the line. */
372 start_of_input_line
= pfile
->buffer
->cur
;
374 context
= pfile
->context
;
376 check_output_buffer (pfile
, RLIMIT (context
) - cur
);
377 out
= pfile
->out
.cur
;
382 && cur
>= pfile
->buffer
->notes
[pfile
->buffer
->cur_note
].pos
)
384 pfile
->buffer
->cur
= cur
;
385 _cpp_process_line_notes (pfile
, false);
390 /* Whitespace should "continue" out of the switch,
391 non-whitespace should "break" out of it. */
402 /* If this is a macro's expansion, pop it. */
405 pfile
->out
.cur
= out
- 1;
406 _cpp_pop_context (pfile
);
410 /* Omit the newline from the output buffer. */
411 pfile
->out
.cur
= out
- 1;
412 pfile
->buffer
->cur
= cur
;
413 pfile
->buffer
->need_line
= true;
414 CPP_INCREMENT_LINE (pfile
, 0);
416 if ((lex_state
== ls_fun_open
|| lex_state
== ls_fun_close
)
417 && !pfile
->state
.in_directive
418 && _cpp_get_fresh_line (pfile
))
420 /* Newlines in arguments become a space, but we don't
421 clear any in-progress quote. */
422 if (lex_state
== ls_fun_close
)
424 cur
= pfile
->buffer
->cur
;
447 /* Skip escaped quotes here, it's easier than above. */
448 if (*cur
== '\\' || *cur
== '"' || *cur
== '\'')
453 /* Traditional CPP does not recognize comments within
455 if (!quote
&& *cur
== '*')
457 pfile
->out
.cur
= out
;
458 cur
= copy_comment (pfile
, cur
, macro
!= 0);
459 out
= pfile
->out
.cur
;
465 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
466 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
467 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
468 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
470 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
471 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
472 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
473 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
475 if (!pfile
->state
.skipping
&& (quote
== 0 || macro
))
478 uchar
*out_start
= out
- 1;
480 pfile
->out
.cur
= out_start
;
481 node
= lex_identifier (pfile
, cur
- 1);
482 out
= pfile
->out
.cur
;
485 if (node
->type
== NT_MACRO
486 /* Should we expand for ls_answer? */
487 && (lex_state
== ls_none
|| lex_state
== ls_fun_open
)
488 && !pfile
->state
.prevent_expansion
)
490 /* Macros invalidate MI optimization. */
491 pfile
->mi_valid
= false;
492 if (! (node
->flags
& NODE_BUILTIN
)
493 && node
->value
.macro
->fun_like
)
495 maybe_start_funlike (pfile
, node
, out_start
, &fmacro
);
496 lex_state
= ls_fun_open
;
497 fmacro
.line
= pfile
->line_table
->highest_line
;
500 else if (!recursive_macro (pfile
, node
))
502 /* Remove the object-like macro's name from the
503 output, and push its replacement text. */
504 pfile
->out
.cur
= out_start
;
505 push_replacement_text (pfile
, node
);
510 else if (macro
&& (node
->flags
& NODE_MACRO_ARG
) != 0)
512 /* Found a parameter in the replacement text of a
513 #define. Remove its name from the output. */
514 pfile
->out
.cur
= out_start
;
515 save_replacement_text (pfile
, macro
, node
->value
.arg_index
);
516 out
= pfile
->out
.base
;
518 else if (lex_state
== ls_hash
)
520 lex_state
= ls_predicate
;
523 else if (pfile
->state
.in_expression
524 && node
== pfile
->spec_nodes
.n_defined
)
526 lex_state
= ls_defined
;
529 else if (pfile
->state
.in_expression
530 && (node
== pfile
->spec_nodes
.n__has_include__
531 || node
== pfile
->spec_nodes
.n__has_include_next__
))
533 lex_state
= ls_has_include
;
543 if (lex_state
== ls_fun_open
)
545 if (recursive_macro (pfile
, fmacro
.node
))
549 lex_state
= ls_fun_close
;
551 out
= pfile
->out
.base
+ fmacro
.offset
;
552 fmacro
.args
[0] = fmacro
.offset
;
555 else if (lex_state
== ls_predicate
)
556 lex_state
= ls_answer
;
557 else if (lex_state
== ls_defined
)
558 lex_state
= ls_defined_close
;
559 else if (lex_state
== ls_has_include
)
560 lex_state
= ls_has_include_close
;
565 if (quote
== 0 && lex_state
== ls_fun_close
&& paren_depth
== 1)
566 save_argument (&fmacro
, out
- pfile
->out
.base
);
573 if (lex_state
== ls_fun_close
&& paren_depth
== 0)
575 cpp_macro
*m
= fmacro
.node
->value
.macro
;
579 save_argument (&fmacro
, out
- pfile
->out
.base
);
581 /* A single zero-length argument is no argument. */
584 && out
== pfile
->out
.base
+ fmacro
.offset
+ 1)
587 if (_cpp_arguments_ok (pfile
, m
, fmacro
.node
, fmacro
.argc
))
589 /* Remove the macro's invocation from the
590 output, and push its replacement text. */
591 pfile
->out
.cur
= (pfile
->out
.base
594 replace_args_and_push (pfile
, &fmacro
);
598 else if (lex_state
== ls_answer
|| lex_state
== ls_defined_close
599 || lex_state
== ls_has_include_close
)
605 if (cur
- 1 == start_of_input_line
606 /* A '#' from a macro doesn't start a directive. */
607 && !pfile
->context
->prev
608 && !pfile
->state
.in_directive
)
610 /* A directive. With the way _cpp_handle_directive
611 currently works, we only want to call it if either we
612 know the directive is OK, or we want it to fail and
613 be removed from the output. If we want it to be
614 passed through (the assembler case) then we must not
615 call _cpp_handle_directive. */
616 pfile
->out
.cur
= out
;
617 cur
= skip_whitespace (pfile
, cur
, true /* skip_comments */);
618 out
= pfile
->out
.cur
;
622 /* Null directive. Ignore it and don't invalidate
623 the MI optimization. */
624 pfile
->buffer
->need_line
= true;
625 CPP_INCREMENT_LINE (pfile
, 0);
633 if (is_numstart (*cur
)
634 && CPP_OPTION (pfile
, lang
) != CLK_ASM
)
636 else if (is_idstart (*cur
))
637 /* Check whether we know this directive, but don't
639 do_it
= lex_identifier (pfile
, cur
)->is_directive
;
641 if (do_it
|| CPP_OPTION (pfile
, lang
) != CLK_ASM
)
643 /* This is a kludge. We want to have the ISO
644 preprocessor lex the next token. */
645 pfile
->buffer
->cur
= cur
;
646 _cpp_handle_directive (pfile
, false /* indented */);
653 if (pfile
->state
.in_expression
)
664 /* Non-whitespace disables MI optimization and stops treating
665 '<' as a quote in #include. */
667 if (!pfile
->state
.in_directive
)
668 pfile
->mi_valid
= false;
670 if (lex_state
== ls_none
)
673 /* Some of these transitions of state are syntax errors. The
674 ISO preprocessor will issue errors later. */
675 if (lex_state
== ls_fun_open
)
678 else if (lex_state
== ls_hash
679 || lex_state
== ls_predicate
680 || lex_state
== ls_defined
681 || lex_state
== ls_has_include
)
684 /* ls_answer and ls_defined_close keep going until ')'. */
689 _cpp_release_buff (pfile
, fmacro
.buff
);
691 if (lex_state
== ls_fun_close
)
692 cpp_error_with_line (pfile
, CPP_DL_ERROR
, fmacro
.line
, 0,
693 "unterminated argument list invoking macro \"%s\"",
694 NODE_NAME (fmacro
.node
));
698 /* Push a context holding the replacement text of the macro NODE on
699 the context stack. NODE is either object-like, or a function-like
700 macro with no arguments. */
702 push_replacement_text (cpp_reader
*pfile
, cpp_hashnode
*node
)
708 if (node
->flags
& NODE_BUILTIN
)
710 text
= _cpp_builtin_macro_text (pfile
, node
);
711 len
= ustrlen (text
);
712 buf
= _cpp_unaligned_alloc (pfile
, len
+ 1);
713 memcpy (buf
, text
, len
);
719 cpp_macro
*macro
= node
->value
.macro
;
721 text
= macro
->exp
.text
;
722 macro
->traditional
= 1;
726 _cpp_push_text_context (pfile
, node
, text
, len
);
729 /* Returns TRUE if traditional macro recursion is detected. */
731 recursive_macro (cpp_reader
*pfile
, cpp_hashnode
*node
)
733 bool recursing
= !!(node
->flags
& NODE_DISABLED
);
735 /* Object-like macros that are already expanding are necessarily
738 However, it is possible to have traditional function-like macros
739 that are not infinitely recursive but recurse to any given depth.
740 Further, it is easy to construct examples that get ever longer
741 until the point they stop recursing. So there is no easy way to
742 detect true recursion; instead we assume any expansion more than
743 20 deep since the first invocation of this macro must be
745 if (recursing
&& node
->value
.macro
->fun_like
)
748 cpp_context
*context
= pfile
->context
;
753 if (context
->c
.macro
== node
&& depth
> 20)
755 context
= context
->prev
;
758 recursing
= context
!= NULL
;
762 cpp_error (pfile
, CPP_DL_ERROR
,
763 "detected recursion whilst expanding macro \"%s\"",
769 /* Return the length of the replacement text of a function-like or
770 object-like non-builtin macro. */
772 _cpp_replacement_text_len (const cpp_macro
*macro
)
776 if (macro
->fun_like
&& (macro
->paramc
!= 0))
781 for (exp
= macro
->exp
.text
;;)
783 struct block
*b
= (struct block
*) exp
;
786 if (b
->arg_index
== 0)
788 len
+= NODE_LEN (macro
->params
[b
->arg_index
- 1]);
789 exp
+= BLOCK_LEN (b
->text_len
);
798 /* Copy the replacement text of MACRO to DEST, which must be of
799 sufficient size. It is not NUL-terminated. The next character is
802 _cpp_copy_replacement_text (const cpp_macro
*macro
, uchar
*dest
)
804 if (macro
->fun_like
&& (macro
->paramc
!= 0))
808 for (exp
= macro
->exp
.text
;;)
810 struct block
*b
= (struct block
*) exp
;
813 memcpy (dest
, b
->text
, b
->text_len
);
815 if (b
->arg_index
== 0)
817 param
= macro
->params
[b
->arg_index
- 1];
818 memcpy (dest
, NODE_NAME (param
), NODE_LEN (param
));
819 dest
+= NODE_LEN (param
);
820 exp
+= BLOCK_LEN (b
->text_len
);
825 memcpy (dest
, macro
->exp
.text
, macro
->count
);
826 dest
+= macro
->count
;
832 /* Push a context holding the replacement text of the macro NODE on
833 the context stack. NODE is either object-like, or a function-like
834 macro with no arguments. */
836 replace_args_and_push (cpp_reader
*pfile
, struct fun_macro
*fmacro
)
838 cpp_macro
*macro
= fmacro
->node
->value
.macro
;
840 if (macro
->paramc
== 0)
841 push_replacement_text (pfile
, fmacro
->node
);
850 /* Get an estimate of the length of the argument-replaced text.
851 This is a worst case estimate, assuming that every replacement
852 text character needs quoting. */
853 for (exp
= macro
->exp
.text
;;)
855 struct block
*b
= (struct block
*) exp
;
858 if (b
->arg_index
== 0)
860 len
+= 2 * (fmacro
->args
[b
->arg_index
]
861 - fmacro
->args
[b
->arg_index
- 1] - 1);
862 exp
+= BLOCK_LEN (b
->text_len
);
865 /* Allocate room for the expansion plus \n. */
866 buff
= _cpp_get_buff (pfile
, len
+ 1);
868 /* Copy the expansion and replace arguments. */
869 /* Accumulate actual length, including quoting as necessary */
870 p
= BUFF_FRONT (buff
);
872 for (exp
= macro
->exp
.text
;;)
874 struct block
*b
= (struct block
*) exp
;
881 /* Copy the non-argument text literally, keeping
882 track of whether matching quotes have been seen. */
883 for (arglen
= b
->text_len
, in
= b
->text
; arglen
> 0; arglen
--)
886 cxtquote
= ! cxtquote
;
889 /* Done if no more arguments */
890 if (b
->arg_index
== 0)
892 arglen
= (fmacro
->args
[b
->arg_index
]
893 - fmacro
->args
[b
->arg_index
- 1] - 1);
894 base
= pfile
->out
.base
+ fmacro
->args
[b
->arg_index
- 1];
897 /* Skip leading whitespace in the text for the argument to
898 be substituted. To be compatible with gcc 2.95, we would
899 also need to trim trailing whitespace. Gcc 2.95 trims
900 leading and trailing whitespace, which may be a bug. The
901 current gcc testsuite explicitly checks that this leading
902 and trailing whitespace in actual arguments is
904 while (arglen
> 0 && is_space (*in
))
910 for (argquote
= 0; arglen
> 0; arglen
--)
912 if (cxtquote
&& *in
== '"')
914 if (in
> base
&& *(in
-1) != '\\')
915 argquote
= ! argquote
;
916 /* Always add backslash before double quote if argument
917 is expanded in a quoted context */
921 else if (cxtquote
&& argquote
&& *in
== '\\')
923 /* Always add backslash before a backslash in an argument
924 that is expanded in a quoted context and also in the
925 range of a quoted context in the argument itself. */
932 exp
+= BLOCK_LEN (b
->text_len
);
937 _cpp_push_text_context (pfile
, fmacro
->node
, BUFF_FRONT (buff
), len
);
939 /* So we free buffer allocation when macro is left. */
940 pfile
->context
->buff
= buff
;
944 /* Read and record the parameters, if any, of a function-like macro
945 definition. Destroys pfile->out.cur.
947 Returns true on success, false on failure (syntax error or a
948 duplicate parameter). On success, CUR (pfile->context) is just
949 past the closing parenthesis. */
951 scan_parameters (cpp_reader
*pfile
, cpp_macro
*macro
)
953 const uchar
*cur
= CUR (pfile
->context
) + 1;
958 cur
= skip_whitespace (pfile
, cur
, true /* skip_comments */);
960 if (is_idstart (*cur
))
962 struct cpp_hashnode
*id
= lex_identifier (pfile
, cur
);
964 if (_cpp_save_parameter (pfile
, macro
, id
, id
))
966 cur
= skip_whitespace (pfile
, CUR (pfile
->context
),
967 true /* skip_comments */);
977 ok
= (*cur
== ')' && macro
->paramc
== 0);
982 cpp_error (pfile
, CPP_DL_ERROR
, "syntax error in macro parameter list");
984 CUR (pfile
->context
) = cur
+ (*cur
== ')');
989 /* Save the text from pfile->out.base to pfile->out.cur as
990 the replacement text for the current macro, followed by argument
991 ARG_INDEX, with zero indicating the end of the replacement
994 save_replacement_text (cpp_reader
*pfile
, cpp_macro
*macro
,
995 unsigned int arg_index
)
997 size_t len
= pfile
->out
.cur
- pfile
->out
.base
;
1000 if (macro
->paramc
== 0)
1002 /* Object-like and function-like macros without parameters
1003 simply store their \n-terminated replacement text. */
1004 exp
= _cpp_unaligned_alloc (pfile
, len
+ 1);
1005 memcpy (exp
, pfile
->out
.base
, len
);
1007 macro
->exp
.text
= exp
;
1008 macro
->traditional
= 1;
1013 /* Store the text's length (unsigned int), the argument index
1014 (unsigned short, base 1) and then the text. */
1015 size_t blen
= BLOCK_LEN (len
);
1016 struct block
*block
;
1018 if (macro
->count
+ blen
> BUFF_ROOM (pfile
->a_buff
))
1019 _cpp_extend_buff (pfile
, &pfile
->a_buff
, macro
->count
+ blen
);
1021 exp
= BUFF_FRONT (pfile
->a_buff
);
1022 block
= (struct block
*) (exp
+ macro
->count
);
1023 macro
->exp
.text
= exp
;
1024 macro
->traditional
= 1;
1026 /* Write out the block information. */
1027 block
->text_len
= len
;
1028 block
->arg_index
= arg_index
;
1029 memcpy (block
->text
, pfile
->out
.base
, len
);
1031 /* Lex the rest into the start of the output buffer. */
1032 pfile
->out
.cur
= pfile
->out
.base
;
1034 macro
->count
+= blen
;
1036 /* If we've finished, commit the memory. */
1038 BUFF_FRONT (pfile
->a_buff
) += macro
->count
;
1042 /* Analyze and save the replacement text of a macro. Returns true on
1045 _cpp_create_trad_definition (cpp_reader
*pfile
, cpp_macro
*macro
)
1049 cpp_context
*context
= pfile
->context
;
1051 /* The context has not been set up for command line defines, and CUR
1052 has not been updated for the macro name for in-file defines. */
1053 pfile
->out
.cur
= pfile
->out
.base
;
1054 CUR (context
) = pfile
->buffer
->cur
;
1055 RLIMIT (context
) = pfile
->buffer
->rlimit
;
1056 check_output_buffer (pfile
, RLIMIT (context
) - CUR (context
));
1058 /* Is this a function-like macro? */
1059 if (* CUR (context
) == '(')
1061 bool ok
= scan_parameters (pfile
, macro
);
1063 /* Remember the params so we can clear NODE_MACRO_ARG flags. */
1064 macro
->params
= (cpp_hashnode
**) BUFF_FRONT (pfile
->a_buff
);
1066 /* Setting macro to NULL indicates an error occurred, and
1067 prevents unnecessary work in _cpp_scan_out_logical_line. */
1072 BUFF_FRONT (pfile
->a_buff
) = (uchar
*) ¯o
->params
[macro
->paramc
];
1073 macro
->fun_like
= 1;
1077 /* Skip leading whitespace in the replacement text. */
1079 = skip_whitespace (pfile
, CUR (context
),
1080 CPP_OPTION (pfile
, discard_comments_in_macro_exp
));
1082 pfile
->state
.prevent_expansion
++;
1083 _cpp_scan_out_logical_line (pfile
, macro
);
1084 pfile
->state
.prevent_expansion
--;
1089 /* Skip trailing white space. */
1090 cur
= pfile
->out
.base
;
1091 limit
= pfile
->out
.cur
;
1092 while (limit
> cur
&& is_space (limit
[-1]))
1094 pfile
->out
.cur
= limit
;
1095 save_replacement_text (pfile
, macro
, 0);
1100 /* Copy SRC of length LEN to DEST, but convert all contiguous
1101 whitespace to a single space, provided it is not in quotes. The
1102 quote currently in effect is pointed to by PQUOTE, and is updated
1103 by the function. Returns the number of bytes copied. */
1105 canonicalize_text (uchar
*dest
, const uchar
*src
, size_t len
, uchar
*pquote
)
1107 uchar
*orig_dest
= dest
;
1108 uchar quote
= *pquote
;
1112 if (is_space (*src
) && !quote
)
1116 while (len
&& is_space (*src
));
1121 if (*src
== '\'' || *src
== '"')
1125 else if (quote
== *src
)
1128 *dest
++ = *src
++, len
--;
1133 return dest
- orig_dest
;
1136 /* Returns true if MACRO1 and MACRO2 have expansions different other
1137 than in the form of their whitespace. */
1139 _cpp_expansions_different_trad (const cpp_macro
*macro1
,
1140 const cpp_macro
*macro2
)
1142 uchar
*p1
= XNEWVEC (uchar
, macro1
->count
+ macro2
->count
);
1143 uchar
*p2
= p1
+ macro1
->count
;
1144 uchar quote1
= 0, quote2
= 0;
1148 if (macro1
->paramc
> 0)
1150 const uchar
*exp1
= macro1
->exp
.text
, *exp2
= macro2
->exp
.text
;
1155 struct block
*b1
= (struct block
*) exp1
;
1156 struct block
*b2
= (struct block
*) exp2
;
1158 if (b1
->arg_index
!= b2
->arg_index
)
1161 len1
= canonicalize_text (p1
, b1
->text
, b1
->text_len
, "e1
);
1162 len2
= canonicalize_text (p2
, b2
->text
, b2
->text_len
, "e2
);
1163 if (len1
!= len2
|| memcmp (p1
, p2
, len1
))
1165 if (b1
->arg_index
== 0)
1170 exp1
+= BLOCK_LEN (b1
->text_len
);
1171 exp2
+= BLOCK_LEN (b2
->text_len
);
1176 len1
= canonicalize_text (p1
, macro1
->exp
.text
, macro1
->count
, "e1
);
1177 len2
= canonicalize_text (p2
, macro2
->exp
.text
, macro2
->count
, "e2
);
1178 mismatch
= (len1
!= len2
|| memcmp (p1
, p2
, len1
));