* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / cpptrad.c
blobf8923ae841200f4576b24f4f933de1b21a8c15a5
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
8 later version.
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. */
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "cpplib.h"
24 #include "cpphash.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. */
35 struct block
37 unsigned int text_len;
38 unsigned short arg_index;
39 uchar text[1];
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
46 invocation. */
47 struct fun_macro
49 /* Memory buffer holding the trad_arg array. */
50 _cpp_buff *buff;
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. */
56 size_t *args;
58 /* The hashnode of the macro. */
59 cpp_hashnode *node;
61 /* The offset of the macro name in the output buffer. */
62 size_t offset;
64 /* The line the macro name appeared on. */
65 unsigned int line;
67 /* Zero-based index of argument being currently lexed. */
68 unsigned int argc;
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 *skip_whitespace PARAMS ((cpp_reader *, const uchar *,
85 int));
86 static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
87 static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *, int));
88 static void check_output_buffer PARAMS ((cpp_reader *, size_t));
89 static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *));
90 static bool scan_parameters PARAMS ((cpp_reader *, cpp_macro *));
91 static bool recursive_macro PARAMS ((cpp_reader *, cpp_hashnode *));
92 static void save_replacement_text PARAMS ((cpp_reader *, cpp_macro *,
93 unsigned int));
94 static void maybe_start_funlike PARAMS ((cpp_reader *, cpp_hashnode *,
95 const uchar *, struct fun_macro *));
96 static void save_argument PARAMS ((struct fun_macro *, size_t));
97 static void replace_args_and_push PARAMS ((cpp_reader *, struct fun_macro *));
98 static size_t canonicalize_text PARAMS ((uchar *, const uchar *, size_t,
99 uchar *));
101 /* Ensures we have N bytes' space in the output buffer, and
102 reallocates it if not. */
103 static void
104 check_output_buffer (pfile, n)
105 cpp_reader *pfile;
106 size_t n;
108 /* We might need two bytes to terminate an unterminated comment, and
109 one more to terminate the line with a NUL. */
110 n += 2 + 1;
112 if (n > (size_t) (pfile->out.limit - pfile->out.cur))
114 size_t size = pfile->out.cur - pfile->out.base;
115 size_t new_size = (size + n) * 3 / 2;
117 pfile->out.base
118 = (uchar *) xrealloc (pfile->out.base, new_size);
119 pfile->out.limit = pfile->out.base + new_size;
120 pfile->out.cur = pfile->out.base + size;
124 /* CUR points to the asterisk introducing a comment in the current
125 context. IN_DEFINE is true if we are in the replacement text of a
126 macro.
128 The asterisk and following comment is copied to the buffer pointed
129 to by pfile->out.cur, which must be of sufficient size.
130 Unterminated comments are diagnosed, and correctly terminated in
131 the output. pfile->out.cur is updated depending upon IN_DEFINE,
132 -C, -CC and pfile->state.in_directive.
134 Returns a pointer to the first character after the comment in the
135 input buffer. */
136 static const uchar *
137 copy_comment (pfile, cur, in_define)
138 cpp_reader *pfile;
139 const uchar *cur;
140 int in_define;
142 bool unterminated, copy = false;
143 unsigned int from_line = pfile->line;
144 cpp_buffer *buffer = pfile->buffer;
146 buffer->cur = cur;
147 unterminated = _cpp_skip_block_comment (pfile);
148 if (unterminated)
149 cpp_error_with_line (pfile, DL_ERROR, from_line, 0,
150 "unterminated comment");
152 /* Comments in directives become spaces so that tokens are properly
153 separated when the ISO preprocessor re-lexes the line. The
154 exception is #define. */
155 if (pfile->state.in_directive)
157 if (in_define)
159 if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
160 pfile->out.cur--;
161 else
162 copy = true;
164 else
165 pfile->out.cur[-1] = ' ';
167 else if (CPP_OPTION (pfile, discard_comments))
168 pfile->out.cur--;
169 else
170 copy = true;
172 if (copy)
174 size_t len = (size_t) (buffer->cur - cur);
175 memcpy (pfile->out.cur, cur, len);
176 pfile->out.cur += len;
177 if (unterminated)
179 *pfile->out.cur++ = '*';
180 *pfile->out.cur++ = '/';
184 return buffer->cur;
187 /* CUR points to any character in the input buffer. Skips over all
188 contiguous horizontal white space and NULs, including comments if
189 SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
190 character or the end of the current context. Escaped newlines are
191 removed.
193 The whitespace is copied verbatim to the output buffer, except that
194 comments are handled as described in copy_comment().
195 pfile->out.cur is updated.
197 Returns a pointer to the first character after the whitespace in
198 the input buffer. */
199 static const uchar *
200 skip_whitespace (pfile, cur, skip_comments)
201 cpp_reader *pfile;
202 const uchar *cur;
203 int skip_comments;
205 uchar *out = pfile->out.cur;
207 for (;;)
209 unsigned int c = *cur++;
210 *out++ = c;
212 if (is_nvspace (c))
213 continue;
215 if (c == '/' && *cur == '*' && skip_comments)
217 pfile->out.cur = out;
218 cur = copy_comment (pfile, cur, false /* in_define */);
219 out = pfile->out.cur;
220 continue;
223 out--;
224 break;
227 pfile->out.cur = out;
228 return cur - 1;
231 /* Lexes and outputs an identifier starting at CUR, which is assumed
232 to point to a valid first character of an identifier. Returns
233 the hashnode, and updates out.cur. */
234 static cpp_hashnode *
235 lex_identifier (pfile, cur)
236 cpp_reader *pfile;
237 const uchar *cur;
239 size_t len;
240 uchar *out = pfile->out.cur;
241 cpp_hashnode *result;
244 *out++ = *cur++;
245 while (is_numchar (*cur));
247 CUR (pfile->context) = cur;
248 len = out - pfile->out.cur;
249 result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
250 len, HT_ALLOC);
251 pfile->out.cur = out;
252 return result;
255 /* Overlays the true file buffer temporarily with text of length LEN
256 starting at START. The true buffer is restored upon calling
257 restore_buff(). */
258 void
259 _cpp_overlay_buffer (pfile, start, len)
260 cpp_reader *pfile;
261 const uchar *start;
262 size_t len;
264 cpp_buffer *buffer = pfile->buffer;
266 pfile->overlaid_buffer = buffer;
267 buffer->saved_cur = buffer->cur;
268 buffer->saved_rlimit = buffer->rlimit;
269 /* Prevent the ISO lexer from scanning a fresh line. */
270 pfile->saved_line = pfile->line--;
271 buffer->need_line = false;
273 buffer->cur = start;
274 buffer->rlimit = start + len;
277 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
278 void
279 _cpp_remove_overlay (pfile)
280 cpp_reader *pfile;
282 cpp_buffer *buffer = pfile->overlaid_buffer;
284 buffer->cur = buffer->saved_cur;
285 buffer->rlimit = buffer->saved_rlimit;
286 buffer->need_line = true;
288 pfile->overlaid_buffer = NULL;
289 pfile->line = pfile->saved_line;
292 /* Reads a logical line into the output buffer. Returns TRUE if there
293 is more text left in the buffer. */
294 bool
295 _cpp_read_logical_line_trad (pfile)
296 cpp_reader *pfile;
300 if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile))
301 return false;
303 while (!scan_out_logical_line (pfile, NULL) || pfile->state.skipping);
305 return true;
308 /* Set up state for finding the opening '(' of a function-like
309 macro. */
310 static void
311 maybe_start_funlike (pfile, node, start, macro)
312 cpp_reader *pfile;
313 cpp_hashnode *node;
314 const uchar *start;
315 struct fun_macro *macro;
317 unsigned int n = node->value.macro->paramc + 1;
319 if (macro->buff)
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);
323 macro->node = node;
324 macro->offset = start - pfile->out.base;
325 macro->argc = 0;
328 /* Save the OFFSET of the start of the next argument to MACRO. */
329 static void
330 save_argument (macro, offset)
331 struct fun_macro *macro;
332 size_t offset;
334 macro->argc++;
335 if (macro->argc <= macro->node->value.macro->paramc)
336 macro->args[macro->argc] = offset;
339 /* Copies the next logical line in the current buffer (starting at
340 buffer->cur) to the output buffer. The output is guaranteed to
341 terminate with a NUL character. buffer->cur is updated.
343 If MACRO is non-NULL, then we are scanning the replacement list of
344 MACRO, and we call save_replacement_text() every time we meet an
345 argument. */
346 bool
347 scan_out_logical_line (pfile, macro)
348 cpp_reader *pfile;
349 cpp_macro *macro;
351 bool result = true;
352 cpp_context *context;
353 const uchar *cur;
354 uchar *out;
355 struct fun_macro fmacro;
356 unsigned int c, paren_depth = 0, quote;
357 enum ls lex_state = ls_none;
358 bool header_ok;
360 fmacro.buff = NULL;
362 quote = 0;
363 header_ok = pfile->state.angled_headers;
364 CUR (pfile->context) = pfile->buffer->cur;
365 RLIMIT (pfile->context) = pfile->buffer->rlimit;
366 pfile->out.cur = pfile->out.base;
367 pfile->out.first_line = pfile->line;
368 new_context:
369 context = pfile->context;
370 cur = CUR (context);
371 check_output_buffer (pfile, RLIMIT (context) - cur);
372 out = pfile->out.cur;
374 for (;;)
376 if (!context->prev
377 && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
379 pfile->buffer->cur = cur;
380 _cpp_process_line_notes (pfile, false);
382 c = *cur++;
383 *out++ = c;
385 /* Whitespace should "continue" out of the switch,
386 non-whitespace should "break" out of it. */
387 switch (c)
389 case ' ':
390 case '\t':
391 case '\f':
392 case '\v':
393 case '\0':
394 continue;
396 case '\n':
397 /* If this is a macro's expansion, pop it. */
398 if (context->prev)
400 pfile->out.cur = out - 1;
401 _cpp_pop_context (pfile);
402 goto new_context;
405 /* Omit the newline from the output buffer. */
406 pfile->out.cur = out - 1;
407 pfile->buffer->cur = cur;
408 pfile->buffer->need_line = true;
409 pfile->line++;
411 if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
412 && !pfile->state.in_directive
413 && _cpp_get_fresh_line (pfile))
415 /* Newlines in arguments become a space, but we don't
416 clear any in-progress quote. */
417 if (lex_state == ls_fun_close)
418 out[-1] = ' ';
419 cur = pfile->buffer->cur;
420 continue;
422 goto done;
424 case '<':
425 if (header_ok)
426 quote = '>';
427 break;
428 case '>':
429 if (c == quote)
430 quote = 0;
431 break;
433 case '"':
434 case '\'':
435 if (c == quote)
436 quote = 0;
437 else if (!quote)
438 quote = c;
439 break;
441 case '\\':
442 /* Skip escaped quotes here, it's easier than above. */
443 if (*cur == '\\' || *cur == '"' || *cur == '\'')
444 *out++ = *cur++;
445 break;
447 case '/':
448 /* Traditional CPP does not recognize comments within
449 literals. */
450 if (!quote && *cur == '*')
452 pfile->out.cur = out;
453 cur = copy_comment (pfile, cur, macro != 0);
454 out = pfile->out.cur;
455 continue;
457 break;
459 case '_':
460 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
461 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
462 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
463 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
464 case 'y': case 'z':
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':
469 case 'Y': case 'Z':
470 if (!pfile->state.skipping && (quote == 0 || macro))
472 cpp_hashnode *node;
473 uchar *out_start = out - 1;
475 pfile->out.cur = out_start;
476 node = lex_identifier (pfile, cur - 1);
477 out = pfile->out.cur;
478 cur = CUR (context);
480 if (node->type == NT_MACRO
481 /* Should we expand for ls_answer? */
482 && (lex_state == ls_none || lex_state == ls_fun_open)
483 && !pfile->state.prevent_expansion)
485 /* Macros invalidate MI optimization. */
486 pfile->mi_valid = false;
487 if (! (node->flags & NODE_BUILTIN)
488 && node->value.macro->fun_like)
490 maybe_start_funlike (pfile, node, out_start, &fmacro);
491 lex_state = ls_fun_open;
492 fmacro.line = pfile->line;
493 continue;
495 else if (!recursive_macro (pfile, node))
497 /* Remove the object-like macro's name from the
498 output, and push its replacement text. */
499 pfile->out.cur = out_start;
500 push_replacement_text (pfile, node);
501 lex_state = ls_none;
502 goto new_context;
505 else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
507 /* Found a parameter in the replacement text of a
508 #define. Remove its name from the output. */
509 pfile->out.cur = out_start;
510 save_replacement_text (pfile, macro, node->value.arg_index);
511 out = pfile->out.base;
513 else if (lex_state == ls_hash)
515 lex_state = ls_predicate;
516 continue;
518 else if (pfile->state.in_expression
519 && node == pfile->spec_nodes.n_defined)
521 lex_state = ls_defined;
522 continue;
525 break;
527 case '(':
528 if (quote == 0)
530 paren_depth++;
531 if (lex_state == ls_fun_open)
533 if (recursive_macro (pfile, fmacro.node))
534 lex_state = ls_none;
535 else
537 lex_state = ls_fun_close;
538 paren_depth = 1;
539 out = pfile->out.base + fmacro.offset;
540 fmacro.args[0] = fmacro.offset;
543 else if (lex_state == ls_predicate)
544 lex_state = ls_answer;
545 else if (lex_state == ls_defined)
546 lex_state = ls_defined_close;
548 break;
550 case ',':
551 if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
552 save_argument (&fmacro, out - pfile->out.base);
553 break;
555 case ')':
556 if (quote == 0)
558 paren_depth--;
559 if (lex_state == ls_fun_close && paren_depth == 0)
561 cpp_macro *m = fmacro.node->value.macro;
563 m->used = 1;
564 lex_state = ls_none;
565 save_argument (&fmacro, out - pfile->out.base);
567 /* A single zero-length argument is no argument. */
568 if (fmacro.argc == 1
569 && m->paramc == 0
570 && out == pfile->out.base + fmacro.offset + 1)
571 fmacro.argc = 0;
573 if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
575 /* Remove the macro's invocation from the
576 output, and push its replacement text. */
577 pfile->out.cur = (pfile->out.base
578 + fmacro.offset);
579 CUR (context) = cur;
580 replace_args_and_push (pfile, &fmacro);
581 goto new_context;
584 else if (lex_state == ls_answer || lex_state == ls_defined_close)
585 lex_state = ls_none;
587 break;
589 case '#':
590 if (out - 1 == pfile->out.base
591 /* A '#' from a macro doesn't start a directive. */
592 && !pfile->context->prev
593 && !pfile->state.in_directive)
595 /* A directive. With the way _cpp_handle_directive
596 currently works, we only want to call it if either we
597 know the directive is OK, or we want it to fail and
598 be removed from the output. If we want it to be
599 passed through (the assembler case) then we must not
600 call _cpp_handle_directive. */
601 pfile->out.cur = out;
602 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
603 out = pfile->out.cur;
605 if (*cur == '\n')
607 /* Null directive. Ignore it and don't invalidate
608 the MI optimization. */
609 pfile->buffer->need_line = true;
610 pfile->line++;
611 result = false;
612 goto done;
614 else
616 bool do_it = false;
618 if (is_numstart (*cur)
619 && CPP_OPTION (pfile, lang) != CLK_ASM)
620 do_it = true;
621 else if (is_idstart (*cur))
622 /* Check whether we know this directive, but don't
623 advance. */
624 do_it = lex_identifier (pfile, cur)->is_directive;
626 if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
628 /* This is a kludge. We want to have the ISO
629 preprocessor lex the next token. */
630 pfile->buffer->cur = cur;
631 _cpp_handle_directive (pfile, false /* indented */);
632 result = false;
633 goto done;
638 if (pfile->state.in_expression)
640 lex_state = ls_hash;
641 continue;
643 break;
645 default:
646 break;
649 /* Non-whitespace disables MI optimization and stops treating
650 '<' as a quote in #include. */
651 header_ok = false;
652 if (!pfile->state.in_directive)
653 pfile->mi_valid = false;
655 if (lex_state == ls_none)
656 continue;
658 /* Some of these transitions of state are syntax errors. The
659 ISO preprocessor will issue errors later. */
660 if (lex_state == ls_fun_open)
661 /* Missing '('. */
662 lex_state = ls_none;
663 else if (lex_state == ls_hash
664 || lex_state == ls_predicate
665 || lex_state == ls_defined)
666 lex_state = ls_none;
668 /* ls_answer and ls_defined_close keep going until ')'. */
671 done:
672 if (fmacro.buff)
673 _cpp_release_buff (pfile, fmacro.buff);
675 if (lex_state == ls_fun_close)
676 cpp_error_with_line (pfile, DL_ERROR, fmacro.line, 0,
677 "unterminated argument list invoking macro \"%s\"",
678 NODE_NAME (fmacro.node));
679 return result;
682 /* Push a context holding the replacement text of the macro NODE on
683 the context stack. NODE is either object-like, or a function-like
684 macro with no arguments. */
685 static void
686 push_replacement_text (pfile, node)
687 cpp_reader *pfile;
688 cpp_hashnode *node;
690 size_t len;
691 const uchar *text;
692 uchar *buf;
694 if (node->flags & NODE_BUILTIN)
696 text = _cpp_builtin_macro_text (pfile, node);
697 len = ustrlen (text);
698 buf = _cpp_unaligned_alloc (pfile, len + 1);
699 memcpy (buf, text, len);
700 buf[len]='\n';
701 text = buf;
703 else
705 cpp_macro *macro = node->value.macro;
706 macro->used = 1;
707 text = macro->exp.text;
708 len = macro->count;
711 _cpp_push_text_context (pfile, node, text, len);
714 /* Returns TRUE if traditional macro recursion is detected. */
715 static bool
716 recursive_macro (pfile, node)
717 cpp_reader *pfile;
718 cpp_hashnode *node;
720 bool recursing = !!(node->flags & NODE_DISABLED);
722 /* Object-like macros that are already expanding are necessarily
723 recursive.
725 However, it is possible to have traditional function-like macros
726 that are not infinitely recursive but recurse to any given depth.
727 Further, it is easy to construct examples that get ever longer
728 until the point they stop recursing. So there is no easy way to
729 detect true recursion; instead we assume any expansion more than
730 20 deep since the first invocation of this macro must be
731 recursing. */
732 if (recursing && node->value.macro->fun_like)
734 size_t depth = 0;
735 cpp_context *context = pfile->context;
739 depth++;
740 if (context->macro == node && depth > 20)
741 break;
742 context = context->prev;
744 while (context);
745 recursing = context != NULL;
748 if (recursing)
749 cpp_error (pfile, DL_ERROR,
750 "detected recursion whilst expanding macro \"%s\"",
751 NODE_NAME (node));
753 return recursing;
756 /* Return the length of the replacement text of a function-like or
757 object-like non-builtin macro. */
758 size_t
759 _cpp_replacement_text_len (macro)
760 const cpp_macro *macro;
762 size_t len;
764 if (macro->fun_like && (macro->paramc != 0))
766 const uchar *exp;
768 len = 0;
769 for (exp = macro->exp.text;;)
771 struct block *b = (struct block *) exp;
773 len += b->text_len;
774 if (b->arg_index == 0)
775 break;
776 len += NODE_LEN (macro->params[b->arg_index - 1]);
777 exp += BLOCK_LEN (b->text_len);
780 else
781 len = macro->count;
783 return len;
786 /* Copy the replacement text of MACRO to DEST, which must be of
787 sufficient size. It is not NUL-terminated. The next character is
788 returned. */
789 uchar *
790 _cpp_copy_replacement_text (macro, dest)
791 const cpp_macro *macro;
792 uchar *dest;
794 if (macro->fun_like && (macro->paramc != 0))
796 const uchar *exp;
798 for (exp = macro->exp.text;;)
800 struct block *b = (struct block *) exp;
801 cpp_hashnode *param;
803 memcpy (dest, b->text, b->text_len);
804 dest += b->text_len;
805 if (b->arg_index == 0)
806 break;
807 param = macro->params[b->arg_index - 1];
808 memcpy (dest, NODE_NAME (param), NODE_LEN (param));
809 dest += NODE_LEN (param);
810 exp += BLOCK_LEN (b->text_len);
813 else
815 memcpy (dest, macro->exp.text, macro->count);
816 dest += macro->count;
819 return dest;
822 /* Push a context holding the replacement text of the macro NODE on
823 the context stack. NODE is either object-like, or a function-like
824 macro with no arguments. */
825 static void
826 replace_args_and_push (pfile, fmacro)
827 cpp_reader *pfile;
828 struct fun_macro *fmacro;
830 cpp_macro *macro = fmacro->node->value.macro;
832 if (macro->paramc == 0)
833 push_replacement_text (pfile, fmacro->node);
834 else
836 const uchar *exp;
837 uchar *p;
838 _cpp_buff *buff;
839 size_t len = 0;
841 /* Calculate the length of the argument-replaced text. */
842 for (exp = macro->exp.text;;)
844 struct block *b = (struct block *) exp;
846 len += b->text_len;
847 if (b->arg_index == 0)
848 break;
849 len += (fmacro->args[b->arg_index]
850 - fmacro->args[b->arg_index - 1] - 1);
851 exp += BLOCK_LEN (b->text_len);
854 /* Allocate room for the expansion plus \n. */
855 buff = _cpp_get_buff (pfile, len + 1);
857 /* Copy the expansion and replace arguments. */
858 p = BUFF_FRONT (buff);
859 for (exp = macro->exp.text;;)
861 struct block *b = (struct block *) exp;
862 size_t arglen;
864 memcpy (p, b->text, b->text_len);
865 p += b->text_len;
866 if (b->arg_index == 0)
867 break;
868 arglen = (fmacro->args[b->arg_index]
869 - fmacro->args[b->arg_index - 1] - 1);
870 memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
871 arglen);
872 p += arglen;
873 exp += BLOCK_LEN (b->text_len);
876 /* \n-terminate. */
877 *p = '\n';
878 _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
880 /* So we free buffer allocation when macro is left. */
881 pfile->context->buff = buff;
885 /* Read and record the parameters, if any, of a function-like macro
886 definition. Destroys pfile->out.cur.
888 Returns true on success, false on failure (syntax error or a
889 duplicate parameter). On success, CUR (pfile->context) is just
890 past the closing parenthesis. */
891 static bool
892 scan_parameters (pfile, macro)
893 cpp_reader *pfile;
894 cpp_macro *macro;
896 const uchar *cur = CUR (pfile->context) + 1;
897 bool ok;
899 for (;;)
901 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
903 if (is_idstart (*cur))
905 ok = false;
906 if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
907 break;
908 cur = skip_whitespace (pfile, CUR (pfile->context),
909 true /* skip_comments */);
910 if (*cur == ',')
912 cur++;
913 continue;
915 ok = (*cur == ')');
916 break;
919 ok = (*cur == ')' && macro->paramc == 0);
920 break;
923 CUR (pfile->context) = cur + (*cur == ')');
925 return ok;
928 /* Save the text from pfile->out.base to pfile->out.cur as
929 the replacement text for the current macro, followed by argument
930 ARG_INDEX, with zero indicating the end of the replacement
931 text. */
932 static void
933 save_replacement_text (pfile, macro, arg_index)
934 cpp_reader *pfile;
935 cpp_macro *macro;
936 unsigned int arg_index;
938 size_t len = pfile->out.cur - pfile->out.base;
939 uchar *exp;
941 if (macro->paramc == 0)
943 /* Object-like and function-like macros without parameters
944 simply store their \n-terminated replacement text. */
945 exp = _cpp_unaligned_alloc (pfile, len + 1);
946 memcpy (exp, pfile->out.base, len);
947 exp[len] = '\n';
948 macro->exp.text = exp;
949 macro->count = len;
951 else
953 /* Store the text's length (unsigned int), the argument index
954 (unsigned short, base 1) and then the text. */
955 size_t blen = BLOCK_LEN (len);
956 struct block *block;
958 if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
959 _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
961 exp = BUFF_FRONT (pfile->a_buff);
962 block = (struct block *) (exp + macro->count);
963 macro->exp.text = exp;
965 /* Write out the block information. */
966 block->text_len = len;
967 block->arg_index = arg_index;
968 memcpy (block->text, pfile->out.base, len);
970 /* Lex the rest into the start of the output buffer. */
971 pfile->out.cur = pfile->out.base;
973 macro->count += blen;
975 /* If we've finished, commit the memory. */
976 if (arg_index == 0)
977 BUFF_FRONT (pfile->a_buff) += macro->count;
981 /* Analyze and save the replacement text of a macro. Returns true on
982 success. */
983 bool
984 _cpp_create_trad_definition (pfile, macro)
985 cpp_reader *pfile;
986 cpp_macro *macro;
988 const uchar *cur;
989 uchar *limit;
990 cpp_context *context = pfile->context;
992 /* The context has not been set up for command line defines, and CUR
993 has not been updated for the macro name for in-file defines. */
994 pfile->out.cur = pfile->out.base;
995 CUR (context) = pfile->buffer->cur;
996 RLIMIT (context) = pfile->buffer->rlimit;
997 check_output_buffer (pfile, RLIMIT (context) - CUR (context));
999 /* Is this a function-like macro? */
1000 if (* CUR (context) == '(')
1002 /* Setting macro to NULL indicates an error occurred, and
1003 prevents unnecessary work in scan_out_logical_line. */
1004 if (!scan_parameters (pfile, macro))
1005 macro = NULL;
1006 else
1008 /* Success. Commit the parameter array. */
1009 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
1010 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
1011 macro->fun_like = 1;
1015 /* Skip leading whitespace in the replacement text. */
1016 pfile->buffer->cur
1017 = skip_whitespace (pfile, CUR (context),
1018 CPP_OPTION (pfile, discard_comments_in_macro_exp));
1020 pfile->state.prevent_expansion++;
1021 scan_out_logical_line (pfile, macro);
1022 pfile->state.prevent_expansion--;
1024 if (!macro)
1025 return false;
1027 /* Skip trailing white space. */
1028 cur = pfile->out.base;
1029 limit = pfile->out.cur;
1030 while (limit > cur && is_space (limit[-1]))
1031 limit--;
1032 pfile->out.cur = limit;
1033 save_replacement_text (pfile, macro, 0);
1035 return true;
1038 /* Copy SRC of length LEN to DEST, but convert all contiguous
1039 whitespace to a single space, provided it is not in quotes. The
1040 quote currently in effect is pointed to by PQUOTE, and is updated
1041 by the function. Returns the number of bytes copied. */
1042 static size_t
1043 canonicalize_text (dest, src, len, pquote)
1044 uchar *dest;
1045 const uchar *src;
1046 size_t len;
1047 uchar *pquote;
1049 uchar *orig_dest = dest;
1050 uchar quote = *pquote;
1052 while (len)
1054 if (is_space (*src) && !quote)
1057 src++, len--;
1058 while (len && is_space (*src));
1059 *dest++ = ' ';
1061 else
1063 if (*src == '\'' || *src == '"')
1065 if (!quote)
1066 quote = *src;
1067 else if (quote == *src)
1068 quote = 0;
1070 *dest++ = *src++, len--;
1074 *pquote = quote;
1075 return dest - orig_dest;
1078 /* Returns true if MACRO1 and MACRO2 have expansions different other
1079 than in the form of their whitespace. */
1080 bool
1081 _cpp_expansions_different_trad (macro1, macro2)
1082 const cpp_macro *macro1, *macro2;
1084 uchar *p1 = xmalloc (macro1->count + macro2->count);
1085 uchar *p2 = p1 + macro1->count;
1086 uchar quote1 = 0, quote2 = 0;
1087 bool mismatch;
1088 size_t len1, len2;
1090 if (macro1->paramc > 0)
1092 const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1094 mismatch = true;
1095 for (;;)
1097 struct block *b1 = (struct block *) exp1;
1098 struct block *b2 = (struct block *) exp2;
1100 if (b1->arg_index != b2->arg_index)
1101 break;
1103 len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
1104 len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
1105 if (len1 != len2 || memcmp (p1, p2, len1))
1106 break;
1107 if (b1->arg_index == 0)
1109 mismatch = false;
1110 break;
1112 exp1 += BLOCK_LEN (b1->text_len);
1113 exp2 += BLOCK_LEN (b2->text_len);
1116 else
1118 len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
1119 len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
1120 mismatch = (len1 != len2 || memcmp (p1, p2, len1));
1123 free (p1);
1124 return mismatch;