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