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