expr.c (emit_single_push_insn): If padding is needed downward...
[gcc.git] / gcc / cpptrad.c
1 /* CPP Library - traditional lexical analysis and macro expansion.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Contributed by Neil Booth, May 2002
4
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.
9
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.
14
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. */
18
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "tm.h"
23 #include "cpplib.h"
24 #include "cpphash.h"
25
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.
29
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. */
34
35 struct block
36 {
37 unsigned int text_len;
38 unsigned short arg_index;
39 uchar text[1];
40 };
41
42 #define BLOCK_HEADER_LEN offsetof (struct block, text)
43 #define BLOCK_LEN(TEXT_LEN) CPP_ALIGN (BLOCK_HEADER_LEN + (TEXT_LEN))
44
45 /* Structure holding information about a function-like macro
46 invocation. */
47 struct fun_macro
48 {
49 /* Memory buffer holding the trad_arg array. */
50 _cpp_buff *buff;
51
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;
57
58 /* The hashnode of the macro. */
59 cpp_hashnode *node;
60
61 /* The offset of the macro name in the output buffer. */
62 size_t offset;
63
64 /* The line the macro name appeared on. */
65 unsigned int line;
66
67 /* Zero-based index of argument being currently lexed. */
68 unsigned int argc;
69 };
70
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. */
80
81 /* Lexing TODO: Maybe handle space in escaped newlines. Stop cpplex.c
82 from recognizing comments and directives during its lexing pass. */
83
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 *,
93 struct fun_macro *);
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 *);
97
98 /* Ensures we have N bytes' space in the output buffer, and
99 reallocates it if not. */
100 static void
101 check_output_buffer (cpp_reader *pfile, size_t n)
102 {
103 /* We might need two bytes to terminate an unterminated comment, and
104 one more to terminate the line with a NUL. */
105 n += 2 + 1;
106
107 if (n > (size_t) (pfile->out.limit - pfile->out.cur))
108 {
109 size_t size = pfile->out.cur - pfile->out.base;
110 size_t new_size = (size + n) * 3 / 2;
111
112 pfile->out.base
113 = (uchar *) xrealloc (pfile->out.base, new_size);
114 pfile->out.limit = pfile->out.base + new_size;
115 pfile->out.cur = pfile->out.base + size;
116 }
117 }
118
119 /* Skip a C-style block comment in a macro as a result of -CC.
120 Buffer->cur points to the initial asterisk of the comment. */
121 static void
122 skip_macro_block_comment (cpp_reader *pfile)
123 {
124 const uchar *cur = pfile->buffer->cur;
125
126 cur++;
127 if (*cur == '/')
128 cur++;
129
130 /* People like decorating comments with '*', so check for '/'
131 instead for efficiency. */
132 while(! (*cur++ == '/' && cur[-2] == '*') )
133 ;
134
135 pfile->buffer->cur = cur;
136 }
137
138 /* CUR points to the asterisk introducing a comment in the current
139 context. IN_DEFINE is true if we are in the replacement text of a
140 macro.
141
142 The asterisk and following comment is copied to the buffer pointed
143 to by pfile->out.cur, which must be of sufficient size.
144 Unterminated comments are diagnosed, and correctly terminated in
145 the output. pfile->out.cur is updated depending upon IN_DEFINE,
146 -C, -CC and pfile->state.in_directive.
147
148 Returns a pointer to the first character after the comment in the
149 input buffer. */
150 static const uchar *
151 copy_comment (cpp_reader *pfile, const uchar *cur, int in_define)
152 {
153 bool unterminated, copy = false;
154 unsigned int from_line = pfile->line;
155 cpp_buffer *buffer = pfile->buffer;
156
157 buffer->cur = cur;
158 if (pfile->context->prev)
159 unterminated = false, skip_macro_block_comment (pfile);
160 else
161 unterminated = _cpp_skip_block_comment (pfile);
162
163 if (unterminated)
164 cpp_error_with_line (pfile, DL_ERROR, from_line, 0,
165 "unterminated comment");
166
167 /* Comments in directives become spaces so that tokens are properly
168 separated when the ISO preprocessor re-lexes the line. The
169 exception is #define. */
170 if (pfile->state.in_directive)
171 {
172 if (in_define)
173 {
174 if (CPP_OPTION (pfile, discard_comments_in_macro_exp))
175 pfile->out.cur--;
176 else
177 copy = true;
178 }
179 else
180 pfile->out.cur[-1] = ' ';
181 }
182 else if (CPP_OPTION (pfile, discard_comments))
183 pfile->out.cur--;
184 else
185 copy = true;
186
187 if (copy)
188 {
189 size_t len = (size_t) (buffer->cur - cur);
190 memcpy (pfile->out.cur, cur, len);
191 pfile->out.cur += len;
192 if (unterminated)
193 {
194 *pfile->out.cur++ = '*';
195 *pfile->out.cur++ = '/';
196 }
197 }
198
199 return buffer->cur;
200 }
201
202 /* CUR points to any character in the input buffer. Skips over all
203 contiguous horizontal white space and NULs, including comments if
204 SKIP_COMMENTS, until reaching the first non-horizontal-whitespace
205 character or the end of the current context. Escaped newlines are
206 removed.
207
208 The whitespace is copied verbatim to the output buffer, except that
209 comments are handled as described in copy_comment().
210 pfile->out.cur is updated.
211
212 Returns a pointer to the first character after the whitespace in
213 the input buffer. */
214 static const uchar *
215 skip_whitespace (cpp_reader *pfile, const uchar *cur, int skip_comments)
216 {
217 uchar *out = pfile->out.cur;
218
219 for (;;)
220 {
221 unsigned int c = *cur++;
222 *out++ = c;
223
224 if (is_nvspace (c))
225 continue;
226
227 if (c == '/' && *cur == '*' && skip_comments)
228 {
229 pfile->out.cur = out;
230 cur = copy_comment (pfile, cur, false /* in_define */);
231 out = pfile->out.cur;
232 continue;
233 }
234
235 out--;
236 break;
237 }
238
239 pfile->out.cur = out;
240 return cur - 1;
241 }
242
243 /* Lexes and outputs an identifier starting at CUR, which is assumed
244 to point to a valid first character of an identifier. Returns
245 the hashnode, and updates out.cur. */
246 static cpp_hashnode *
247 lex_identifier (cpp_reader *pfile, const uchar *cur)
248 {
249 size_t len;
250 uchar *out = pfile->out.cur;
251 cpp_hashnode *result;
252
253 do
254 *out++ = *cur++;
255 while (is_numchar (*cur));
256
257 CUR (pfile->context) = cur;
258 len = out - pfile->out.cur;
259 result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
260 len, HT_ALLOC);
261 pfile->out.cur = out;
262 return result;
263 }
264
265 /* Overlays the true file buffer temporarily with text of length LEN
266 starting at START. The true buffer is restored upon calling
267 restore_buff(). */
268 void
269 _cpp_overlay_buffer (cpp_reader *pfile, const uchar *start, size_t len)
270 {
271 cpp_buffer *buffer = pfile->buffer;
272
273 pfile->overlaid_buffer = buffer;
274 buffer->saved_cur = buffer->cur;
275 buffer->saved_rlimit = buffer->rlimit;
276 /* Prevent the ISO lexer from scanning a fresh line. */
277 pfile->saved_line = pfile->line--;
278 buffer->need_line = false;
279
280 buffer->cur = start;
281 buffer->rlimit = start + len;
282 }
283
284 /* Restores a buffer overlaid by _cpp_overlay_buffer(). */
285 void
286 _cpp_remove_overlay (cpp_reader *pfile)
287 {
288 cpp_buffer *buffer = pfile->overlaid_buffer;
289
290 buffer->cur = buffer->saved_cur;
291 buffer->rlimit = buffer->saved_rlimit;
292 buffer->need_line = true;
293
294 pfile->overlaid_buffer = NULL;
295 pfile->line = pfile->saved_line;
296 }
297
298 /* Reads a logical line into the output buffer. Returns TRUE if there
299 is more text left in the buffer. */
300 bool
301 _cpp_read_logical_line_trad (cpp_reader *pfile)
302 {
303 do
304 {
305 if (pfile->buffer->need_line && !_cpp_get_fresh_line (pfile))
306 return false;
307 }
308 while (!scan_out_logical_line (pfile, NULL) || pfile->state.skipping);
309
310 return true;
311 }
312
313 /* Set up state for finding the opening '(' of a function-like
314 macro. */
315 static void
316 maybe_start_funlike (cpp_reader *pfile, cpp_hashnode *node, const uchar *start, struct fun_macro *macro)
317 {
318 unsigned int n = node->value.macro->paramc + 1;
319
320 if (macro->buff)
321 _cpp_release_buff (pfile, macro->buff);
322 macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t));
323 macro->args = (size_t *) BUFF_FRONT (macro->buff);
324 macro->node = node;
325 macro->offset = start - pfile->out.base;
326 macro->argc = 0;
327 }
328
329 /* Save the OFFSET of the start of the next argument to MACRO. */
330 static void
331 save_argument (struct fun_macro *macro, size_t offset)
332 {
333 macro->argc++;
334 if (macro->argc <= macro->node->value.macro->paramc)
335 macro->args[macro->argc] = offset;
336 }
337
338 /* Copies the next logical line in the current buffer (starting at
339 buffer->cur) to the output buffer. The output is guaranteed to
340 terminate with a NUL character. buffer->cur is updated.
341
342 If MACRO is non-NULL, then we are scanning the replacement list of
343 MACRO, and we call save_replacement_text() every time we meet an
344 argument. */
345 bool
346 scan_out_logical_line (cpp_reader *pfile, cpp_macro *macro)
347 {
348 bool result = true;
349 cpp_context *context;
350 const uchar *cur;
351 uchar *out;
352 struct fun_macro fmacro;
353 unsigned int c, paren_depth = 0, quote;
354 enum ls lex_state = ls_none;
355 bool header_ok;
356
357 fmacro.buff = NULL;
358
359 quote = 0;
360 header_ok = pfile->state.angled_headers;
361 CUR (pfile->context) = pfile->buffer->cur;
362 RLIMIT (pfile->context) = pfile->buffer->rlimit;
363 pfile->out.cur = pfile->out.base;
364 pfile->out.first_line = pfile->line;
365 new_context:
366 context = pfile->context;
367 cur = CUR (context);
368 check_output_buffer (pfile, RLIMIT (context) - cur);
369 out = pfile->out.cur;
370
371 for (;;)
372 {
373 if (!context->prev
374 && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
375 {
376 pfile->buffer->cur = cur;
377 _cpp_process_line_notes (pfile, false);
378 }
379 c = *cur++;
380 *out++ = c;
381
382 /* Whitespace should "continue" out of the switch,
383 non-whitespace should "break" out of it. */
384 switch (c)
385 {
386 case ' ':
387 case '\t':
388 case '\f':
389 case '\v':
390 case '\0':
391 continue;
392
393 case '\n':
394 /* If this is a macro's expansion, pop it. */
395 if (context->prev)
396 {
397 pfile->out.cur = out - 1;
398 _cpp_pop_context (pfile);
399 goto new_context;
400 }
401
402 /* Omit the newline from the output buffer. */
403 pfile->out.cur = out - 1;
404 pfile->buffer->cur = cur;
405 pfile->buffer->need_line = true;
406 pfile->line++;
407
408 if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
409 && !pfile->state.in_directive
410 && _cpp_get_fresh_line (pfile))
411 {
412 /* Newlines in arguments become a space, but we don't
413 clear any in-progress quote. */
414 if (lex_state == ls_fun_close)
415 out[-1] = ' ';
416 cur = pfile->buffer->cur;
417 continue;
418 }
419 goto done;
420
421 case '<':
422 if (header_ok)
423 quote = '>';
424 break;
425 case '>':
426 if (c == quote)
427 quote = 0;
428 break;
429
430 case '"':
431 case '\'':
432 if (c == quote)
433 quote = 0;
434 else if (!quote)
435 quote = c;
436 break;
437
438 case '\\':
439 /* Skip escaped quotes here, it's easier than above. */
440 if (*cur == '\\' || *cur == '"' || *cur == '\'')
441 *out++ = *cur++;
442 break;
443
444 case '/':
445 /* Traditional CPP does not recognize comments within
446 literals. */
447 if (!quote && *cur == '*')
448 {
449 pfile->out.cur = out;
450 cur = copy_comment (pfile, cur, macro != 0);
451 out = pfile->out.cur;
452 continue;
453 }
454 break;
455
456 case '_':
457 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
458 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
459 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
460 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
461 case 'y': case 'z':
462 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
463 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
464 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
465 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
466 case 'Y': case 'Z':
467 if (!pfile->state.skipping && (quote == 0 || macro))
468 {
469 cpp_hashnode *node;
470 uchar *out_start = out - 1;
471
472 pfile->out.cur = out_start;
473 node = lex_identifier (pfile, cur - 1);
474 out = pfile->out.cur;
475 cur = CUR (context);
476
477 if (node->type == NT_MACRO
478 /* Should we expand for ls_answer? */
479 && (lex_state == ls_none || lex_state == ls_fun_open)
480 && !pfile->state.prevent_expansion)
481 {
482 /* Macros invalidate MI optimization. */
483 pfile->mi_valid = false;
484 if (! (node->flags & NODE_BUILTIN)
485 && node->value.macro->fun_like)
486 {
487 maybe_start_funlike (pfile, node, out_start, &fmacro);
488 lex_state = ls_fun_open;
489 fmacro.line = pfile->line;
490 continue;
491 }
492 else if (!recursive_macro (pfile, node))
493 {
494 /* Remove the object-like macro's name from the
495 output, and push its replacement text. */
496 pfile->out.cur = out_start;
497 push_replacement_text (pfile, node);
498 lex_state = ls_none;
499 goto new_context;
500 }
501 }
502 else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
503 {
504 /* Found a parameter in the replacement text of a
505 #define. Remove its name from the output. */
506 pfile->out.cur = out_start;
507 save_replacement_text (pfile, macro, node->value.arg_index);
508 out = pfile->out.base;
509 }
510 else if (lex_state == ls_hash)
511 {
512 lex_state = ls_predicate;
513 continue;
514 }
515 else if (pfile->state.in_expression
516 && node == pfile->spec_nodes.n_defined)
517 {
518 lex_state = ls_defined;
519 continue;
520 }
521 }
522 break;
523
524 case '(':
525 if (quote == 0)
526 {
527 paren_depth++;
528 if (lex_state == ls_fun_open)
529 {
530 if (recursive_macro (pfile, fmacro.node))
531 lex_state = ls_none;
532 else
533 {
534 lex_state = ls_fun_close;
535 paren_depth = 1;
536 out = pfile->out.base + fmacro.offset;
537 fmacro.args[0] = fmacro.offset;
538 }
539 }
540 else if (lex_state == ls_predicate)
541 lex_state = ls_answer;
542 else if (lex_state == ls_defined)
543 lex_state = ls_defined_close;
544 }
545 break;
546
547 case ',':
548 if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
549 save_argument (&fmacro, out - pfile->out.base);
550 break;
551
552 case ')':
553 if (quote == 0)
554 {
555 paren_depth--;
556 if (lex_state == ls_fun_close && paren_depth == 0)
557 {
558 cpp_macro *m = fmacro.node->value.macro;
559
560 m->used = 1;
561 lex_state = ls_none;
562 save_argument (&fmacro, out - pfile->out.base);
563
564 /* A single zero-length argument is no argument. */
565 if (fmacro.argc == 1
566 && m->paramc == 0
567 && out == pfile->out.base + fmacro.offset + 1)
568 fmacro.argc = 0;
569
570 if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
571 {
572 /* Remove the macro's invocation from the
573 output, and push its replacement text. */
574 pfile->out.cur = (pfile->out.base
575 + fmacro.offset);
576 CUR (context) = cur;
577 replace_args_and_push (pfile, &fmacro);
578 goto new_context;
579 }
580 }
581 else if (lex_state == ls_answer || lex_state == ls_defined_close)
582 lex_state = ls_none;
583 }
584 break;
585
586 case '#':
587 if (out - 1 == pfile->out.base
588 /* A '#' from a macro doesn't start a directive. */
589 && !pfile->context->prev
590 && !pfile->state.in_directive)
591 {
592 /* A directive. With the way _cpp_handle_directive
593 currently works, we only want to call it if either we
594 know the directive is OK, or we want it to fail and
595 be removed from the output. If we want it to be
596 passed through (the assembler case) then we must not
597 call _cpp_handle_directive. */
598 pfile->out.cur = out;
599 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
600 out = pfile->out.cur;
601
602 if (*cur == '\n')
603 {
604 /* Null directive. Ignore it and don't invalidate
605 the MI optimization. */
606 pfile->buffer->need_line = true;
607 pfile->line++;
608 result = false;
609 goto done;
610 }
611 else
612 {
613 bool do_it = false;
614
615 if (is_numstart (*cur)
616 && CPP_OPTION (pfile, lang) != CLK_ASM)
617 do_it = true;
618 else if (is_idstart (*cur))
619 /* Check whether we know this directive, but don't
620 advance. */
621 do_it = lex_identifier (pfile, cur)->is_directive;
622
623 if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
624 {
625 /* This is a kludge. We want to have the ISO
626 preprocessor lex the next token. */
627 pfile->buffer->cur = cur;
628 _cpp_handle_directive (pfile, false /* indented */);
629 result = false;
630 goto done;
631 }
632 }
633 }
634
635 if (pfile->state.in_expression)
636 {
637 lex_state = ls_hash;
638 continue;
639 }
640 break;
641
642 default:
643 break;
644 }
645
646 /* Non-whitespace disables MI optimization and stops treating
647 '<' as a quote in #include. */
648 header_ok = false;
649 if (!pfile->state.in_directive)
650 pfile->mi_valid = false;
651
652 if (lex_state == ls_none)
653 continue;
654
655 /* Some of these transitions of state are syntax errors. The
656 ISO preprocessor will issue errors later. */
657 if (lex_state == ls_fun_open)
658 /* Missing '('. */
659 lex_state = ls_none;
660 else if (lex_state == ls_hash
661 || lex_state == ls_predicate
662 || lex_state == ls_defined)
663 lex_state = ls_none;
664
665 /* ls_answer and ls_defined_close keep going until ')'. */
666 }
667
668 done:
669 if (fmacro.buff)
670 _cpp_release_buff (pfile, fmacro.buff);
671
672 if (lex_state == ls_fun_close)
673 cpp_error_with_line (pfile, DL_ERROR, fmacro.line, 0,
674 "unterminated argument list invoking macro \"%s\"",
675 NODE_NAME (fmacro.node));
676 return result;
677 }
678
679 /* Push a context holding the replacement text of the macro NODE on
680 the context stack. NODE is either object-like, or a function-like
681 macro with no arguments. */
682 static void
683 push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
684 {
685 size_t len;
686 const uchar *text;
687 uchar *buf;
688
689 if (node->flags & NODE_BUILTIN)
690 {
691 text = _cpp_builtin_macro_text (pfile, node);
692 len = ustrlen (text);
693 buf = _cpp_unaligned_alloc (pfile, len + 1);
694 memcpy (buf, text, len);
695 buf[len]='\n';
696 text = buf;
697 }
698 else
699 {
700 cpp_macro *macro = node->value.macro;
701 macro->used = 1;
702 text = macro->exp.text;
703 len = macro->count;
704 }
705
706 _cpp_push_text_context (pfile, node, text, len);
707 }
708
709 /* Returns TRUE if traditional macro recursion is detected. */
710 static bool
711 recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
712 {
713 bool recursing = !!(node->flags & NODE_DISABLED);
714
715 /* Object-like macros that are already expanding are necessarily
716 recursive.
717
718 However, it is possible to have traditional function-like macros
719 that are not infinitely recursive but recurse to any given depth.
720 Further, it is easy to construct examples that get ever longer
721 until the point they stop recursing. So there is no easy way to
722 detect true recursion; instead we assume any expansion more than
723 20 deep since the first invocation of this macro must be
724 recursing. */
725 if (recursing && node->value.macro->fun_like)
726 {
727 size_t depth = 0;
728 cpp_context *context = pfile->context;
729
730 do
731 {
732 depth++;
733 if (context->macro == node && depth > 20)
734 break;
735 context = context->prev;
736 }
737 while (context);
738 recursing = context != NULL;
739 }
740
741 if (recursing)
742 cpp_error (pfile, DL_ERROR,
743 "detected recursion whilst expanding macro \"%s\"",
744 NODE_NAME (node));
745
746 return recursing;
747 }
748
749 /* Return the length of the replacement text of a function-like or
750 object-like non-builtin macro. */
751 size_t
752 _cpp_replacement_text_len (const cpp_macro *macro)
753 {
754 size_t len;
755
756 if (macro->fun_like && (macro->paramc != 0))
757 {
758 const uchar *exp;
759
760 len = 0;
761 for (exp = macro->exp.text;;)
762 {
763 struct block *b = (struct block *) exp;
764
765 len += b->text_len;
766 if (b->arg_index == 0)
767 break;
768 len += NODE_LEN (macro->params[b->arg_index - 1]);
769 exp += BLOCK_LEN (b->text_len);
770 }
771 }
772 else
773 len = macro->count;
774
775 return len;
776 }
777
778 /* Copy the replacement text of MACRO to DEST, which must be of
779 sufficient size. It is not NUL-terminated. The next character is
780 returned. */
781 uchar *
782 _cpp_copy_replacement_text (const cpp_macro *macro, uchar *dest)
783 {
784 if (macro->fun_like && (macro->paramc != 0))
785 {
786 const uchar *exp;
787
788 for (exp = macro->exp.text;;)
789 {
790 struct block *b = (struct block *) exp;
791 cpp_hashnode *param;
792
793 memcpy (dest, b->text, b->text_len);
794 dest += b->text_len;
795 if (b->arg_index == 0)
796 break;
797 param = macro->params[b->arg_index - 1];
798 memcpy (dest, NODE_NAME (param), NODE_LEN (param));
799 dest += NODE_LEN (param);
800 exp += BLOCK_LEN (b->text_len);
801 }
802 }
803 else
804 {
805 memcpy (dest, macro->exp.text, macro->count);
806 dest += macro->count;
807 }
808
809 return dest;
810 }
811
812 /* Push a context holding the replacement text of the macro NODE on
813 the context stack. NODE is either object-like, or a function-like
814 macro with no arguments. */
815 static void
816 replace_args_and_push (cpp_reader *pfile, struct fun_macro *fmacro)
817 {
818 cpp_macro *macro = fmacro->node->value.macro;
819
820 if (macro->paramc == 0)
821 push_replacement_text (pfile, fmacro->node);
822 else
823 {
824 const uchar *exp;
825 uchar *p;
826 _cpp_buff *buff;
827 size_t len = 0;
828
829 /* Calculate the length of the argument-replaced text. */
830 for (exp = macro->exp.text;;)
831 {
832 struct block *b = (struct block *) exp;
833
834 len += b->text_len;
835 if (b->arg_index == 0)
836 break;
837 len += (fmacro->args[b->arg_index]
838 - fmacro->args[b->arg_index - 1] - 1);
839 exp += BLOCK_LEN (b->text_len);
840 }
841
842 /* Allocate room for the expansion plus \n. */
843 buff = _cpp_get_buff (pfile, len + 1);
844
845 /* Copy the expansion and replace arguments. */
846 p = BUFF_FRONT (buff);
847 for (exp = macro->exp.text;;)
848 {
849 struct block *b = (struct block *) exp;
850 size_t arglen;
851
852 memcpy (p, b->text, b->text_len);
853 p += b->text_len;
854 if (b->arg_index == 0)
855 break;
856 arglen = (fmacro->args[b->arg_index]
857 - fmacro->args[b->arg_index - 1] - 1);
858 memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
859 arglen);
860 p += arglen;
861 exp += BLOCK_LEN (b->text_len);
862 }
863
864 /* \n-terminate. */
865 *p = '\n';
866 _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
867
868 /* So we free buffer allocation when macro is left. */
869 pfile->context->buff = buff;
870 }
871 }
872
873 /* Read and record the parameters, if any, of a function-like macro
874 definition. Destroys pfile->out.cur.
875
876 Returns true on success, false on failure (syntax error or a
877 duplicate parameter). On success, CUR (pfile->context) is just
878 past the closing parenthesis. */
879 static bool
880 scan_parameters (cpp_reader *pfile, cpp_macro *macro)
881 {
882 const uchar *cur = CUR (pfile->context) + 1;
883 bool ok;
884
885 for (;;)
886 {
887 cur = skip_whitespace (pfile, cur, true /* skip_comments */);
888
889 if (is_idstart (*cur))
890 {
891 ok = false;
892 if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
893 break;
894 cur = skip_whitespace (pfile, CUR (pfile->context),
895 true /* skip_comments */);
896 if (*cur == ',')
897 {
898 cur++;
899 continue;
900 }
901 ok = (*cur == ')');
902 break;
903 }
904
905 ok = (*cur == ')' && macro->paramc == 0);
906 break;
907 }
908
909 CUR (pfile->context) = cur + (*cur == ')');
910
911 return ok;
912 }
913
914 /* Save the text from pfile->out.base to pfile->out.cur as
915 the replacement text for the current macro, followed by argument
916 ARG_INDEX, with zero indicating the end of the replacement
917 text. */
918 static void
919 save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
920 unsigned int arg_index)
921 {
922 size_t len = pfile->out.cur - pfile->out.base;
923 uchar *exp;
924
925 if (macro->paramc == 0)
926 {
927 /* Object-like and function-like macros without parameters
928 simply store their \n-terminated replacement text. */
929 exp = _cpp_unaligned_alloc (pfile, len + 1);
930 memcpy (exp, pfile->out.base, len);
931 exp[len] = '\n';
932 macro->exp.text = exp;
933 macro->count = len;
934 }
935 else
936 {
937 /* Store the text's length (unsigned int), the argument index
938 (unsigned short, base 1) and then the text. */
939 size_t blen = BLOCK_LEN (len);
940 struct block *block;
941
942 if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
943 _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
944
945 exp = BUFF_FRONT (pfile->a_buff);
946 block = (struct block *) (exp + macro->count);
947 macro->exp.text = exp;
948
949 /* Write out the block information. */
950 block->text_len = len;
951 block->arg_index = arg_index;
952 memcpy (block->text, pfile->out.base, len);
953
954 /* Lex the rest into the start of the output buffer. */
955 pfile->out.cur = pfile->out.base;
956
957 macro->count += blen;
958
959 /* If we've finished, commit the memory. */
960 if (arg_index == 0)
961 BUFF_FRONT (pfile->a_buff) += macro->count;
962 }
963 }
964
965 /* Analyze and save the replacement text of a macro. Returns true on
966 success. */
967 bool
968 _cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
969 {
970 const uchar *cur;
971 uchar *limit;
972 cpp_context *context = pfile->context;
973
974 /* The context has not been set up for command line defines, and CUR
975 has not been updated for the macro name for in-file defines. */
976 pfile->out.cur = pfile->out.base;
977 CUR (context) = pfile->buffer->cur;
978 RLIMIT (context) = pfile->buffer->rlimit;
979 check_output_buffer (pfile, RLIMIT (context) - CUR (context));
980
981 /* Is this a function-like macro? */
982 if (* CUR (context) == '(')
983 {
984 /* Setting macro to NULL indicates an error occurred, and
985 prevents unnecessary work in scan_out_logical_line. */
986 if (!scan_parameters (pfile, macro))
987 macro = NULL;
988 else
989 {
990 /* Success. Commit the parameter array. */
991 macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
992 BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
993 macro->fun_like = 1;
994 }
995 }
996
997 /* Skip leading whitespace in the replacement text. */
998 pfile->buffer->cur
999 = skip_whitespace (pfile, CUR (context),
1000 CPP_OPTION (pfile, discard_comments_in_macro_exp));
1001
1002 pfile->state.prevent_expansion++;
1003 scan_out_logical_line (pfile, macro);
1004 pfile->state.prevent_expansion--;
1005
1006 if (!macro)
1007 return false;
1008
1009 /* Skip trailing white space. */
1010 cur = pfile->out.base;
1011 limit = pfile->out.cur;
1012 while (limit > cur && is_space (limit[-1]))
1013 limit--;
1014 pfile->out.cur = limit;
1015 save_replacement_text (pfile, macro, 0);
1016
1017 return true;
1018 }
1019
1020 /* Copy SRC of length LEN to DEST, but convert all contiguous
1021 whitespace to a single space, provided it is not in quotes. The
1022 quote currently in effect is pointed to by PQUOTE, and is updated
1023 by the function. Returns the number of bytes copied. */
1024 static size_t
1025 canonicalize_text (uchar *dest, const uchar *src, size_t len, uchar *pquote)
1026 {
1027 uchar *orig_dest = dest;
1028 uchar quote = *pquote;
1029
1030 while (len)
1031 {
1032 if (is_space (*src) && !quote)
1033 {
1034 do
1035 src++, len--;
1036 while (len && is_space (*src));
1037 *dest++ = ' ';
1038 }
1039 else
1040 {
1041 if (*src == '\'' || *src == '"')
1042 {
1043 if (!quote)
1044 quote = *src;
1045 else if (quote == *src)
1046 quote = 0;
1047 }
1048 *dest++ = *src++, len--;
1049 }
1050 }
1051
1052 *pquote = quote;
1053 return dest - orig_dest;
1054 }
1055
1056 /* Returns true if MACRO1 and MACRO2 have expansions different other
1057 than in the form of their whitespace. */
1058 bool
1059 _cpp_expansions_different_trad (const cpp_macro *macro1,
1060 const cpp_macro *macro2)
1061 {
1062 uchar *p1 = xmalloc (macro1->count + macro2->count);
1063 uchar *p2 = p1 + macro1->count;
1064 uchar quote1 = 0, quote2 = 0;
1065 bool mismatch;
1066 size_t len1, len2;
1067
1068 if (macro1->paramc > 0)
1069 {
1070 const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1071
1072 mismatch = true;
1073 for (;;)
1074 {
1075 struct block *b1 = (struct block *) exp1;
1076 struct block *b2 = (struct block *) exp2;
1077
1078 if (b1->arg_index != b2->arg_index)
1079 break;
1080
1081 len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
1082 len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
1083 if (len1 != len2 || memcmp (p1, p2, len1))
1084 break;
1085 if (b1->arg_index == 0)
1086 {
1087 mismatch = false;
1088 break;
1089 }
1090 exp1 += BLOCK_LEN (b1->text_len);
1091 exp2 += BLOCK_LEN (b2->text_len);
1092 }
1093 }
1094 else
1095 {
1096 len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
1097 len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
1098 mismatch = (len1 != len2 || memcmp (p1, p2, len1));
1099 }
1100
1101 free (p1);
1102 return mismatch;
1103 }