OSDN Git Service

* config/xtensa/xtensa.c (xtensa_multibss_section_type_flags): Add
[pf3gnuchains/gcc-fork.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, 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 == NULL || pfile->buffer->need_line)
303           && !_cpp_get_fresh_line (pfile))
304         return false;
305     }
306   while (!_cpp_scan_out_logical_line (pfile, NULL) || pfile->state.skipping);
307
308   return true;
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
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  new_context:
364   context = pfile->context;
365   cur = CUR (context);
366   check_output_buffer (pfile, RLIMIT (context) - cur);
367   out = pfile->out.cur;
368
369   for (;;)
370     {
371       if (!context->prev
372           && cur >= pfile->buffer->notes[pfile->buffer->cur_note].pos)
373         {
374           pfile->buffer->cur = cur;
375           _cpp_process_line_notes (pfile, false);
376         }
377       c = *cur++;
378       *out++ = c;
379
380       /* Whitespace should "continue" out of the switch,
381          non-whitespace should "break" out of it.  */
382       switch (c)
383         {
384         case ' ':
385         case '\t':
386         case '\f':
387         case '\v':
388         case '\0':
389           continue;
390
391         case '\n':
392           /* If this is a macro's expansion, pop it.  */
393           if (context->prev)
394             {
395               pfile->out.cur = out - 1;
396               _cpp_pop_context (pfile);
397               goto new_context;
398             }
399
400           /* Omit the newline from the output buffer.  */
401           pfile->out.cur = out - 1;
402           pfile->buffer->cur = cur;
403           pfile->buffer->need_line = true;
404           pfile->line++;
405
406           if ((lex_state == ls_fun_open || lex_state == ls_fun_close)
407               && !pfile->state.in_directive
408               && _cpp_get_fresh_line (pfile))
409             {
410               /* Newlines in arguments become a space, but we don't
411                  clear any in-progress quote.  */
412               if (lex_state == ls_fun_close)
413                 out[-1] = ' ';
414               cur = pfile->buffer->cur;
415               continue;
416             }
417           goto done;
418
419         case '<':
420           if (header_ok)
421             quote = '>';
422           break;
423         case '>':
424           if (c == quote)
425             quote = 0;
426           break;
427
428         case '"':
429         case '\'':
430           if (c == quote)
431             quote = 0;
432           else if (!quote)
433             quote = c;
434           break;
435
436         case '\\':
437           /* Skip escaped quotes here, it's easier than above.  */
438           if (*cur == '\\' || *cur == '"' || *cur == '\'')
439             *out++ = *cur++;
440           break;
441
442         case '/':
443           /* Traditional CPP does not recognize comments within
444              literals.  */
445           if (!quote && *cur == '*')
446             {
447               pfile->out.cur = out;
448               cur = copy_comment (pfile, cur, macro != 0);
449               out = pfile->out.cur;
450               continue;
451             }
452           break;
453
454         case '_':
455         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
456         case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
457         case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
458         case 's': case 't': case 'u': case 'v': case 'w': case 'x':
459         case 'y': case 'z':
460         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
461         case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
462         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
463         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
464         case 'Y': case 'Z':
465           if (!pfile->state.skipping && (quote == 0 || macro))
466             {
467               cpp_hashnode *node;
468               uchar *out_start = out - 1;
469
470               pfile->out.cur = out_start;
471               node = lex_identifier (pfile, cur - 1);
472               out = pfile->out.cur;
473               cur = CUR (context);
474
475               if (node->type == NT_MACRO
476                   /* Should we expand for ls_answer?  */
477                   && (lex_state == ls_none || lex_state == ls_fun_open)
478                   && !pfile->state.prevent_expansion)
479                 {
480                   /* Macros invalidate MI optimization.  */
481                   pfile->mi_valid = false;
482                   if (! (node->flags & NODE_BUILTIN)
483                       && node->value.macro->fun_like)
484                     {
485                       maybe_start_funlike (pfile, node, out_start, &fmacro);
486                       lex_state = ls_fun_open;
487                       fmacro.line = pfile->line;
488                       continue;
489                     }
490                   else if (!recursive_macro (pfile, node))
491                     {
492                       /* Remove the object-like macro's name from the
493                          output, and push its replacement text.  */
494                       pfile->out.cur = out_start;
495                       push_replacement_text (pfile, node);
496                       lex_state = ls_none;
497                       goto new_context;
498                     }
499                 }
500               else if (macro && (node->flags & NODE_MACRO_ARG) != 0)
501                 {
502                   /* Found a parameter in the replacement text of a
503                      #define.  Remove its name from the output.  */
504                   pfile->out.cur = out_start;
505                   save_replacement_text (pfile, macro, node->value.arg_index);
506                   out = pfile->out.base;
507                 }
508               else if (lex_state == ls_hash)
509                 {
510                   lex_state = ls_predicate;
511                   continue;
512                 }
513               else if (pfile->state.in_expression
514                        && node == pfile->spec_nodes.n_defined)
515                 {
516                   lex_state = ls_defined;
517                   continue;
518                 }
519             }
520           break;
521
522         case '(':
523           if (quote == 0)
524             {
525               paren_depth++;
526               if (lex_state == ls_fun_open)
527                 {
528                   if (recursive_macro (pfile, fmacro.node))
529                     lex_state = ls_none;
530                   else
531                     {
532                       lex_state = ls_fun_close;
533                       paren_depth = 1;
534                       out = pfile->out.base + fmacro.offset;
535                       fmacro.args[0] = fmacro.offset;
536                     }
537                 }
538               else if (lex_state == ls_predicate)
539                 lex_state = ls_answer;
540               else if (lex_state == ls_defined)
541                 lex_state = ls_defined_close;
542             }
543           break;
544
545         case ',':
546           if (quote == 0 && lex_state == ls_fun_close && paren_depth == 1)
547             save_argument (&fmacro, out - pfile->out.base);
548           break;
549
550         case ')':
551           if (quote == 0)
552             {
553               paren_depth--;
554               if (lex_state == ls_fun_close && paren_depth == 0)
555                 {
556                   cpp_macro *m = fmacro.node->value.macro;
557
558                   m->used = 1;
559                   lex_state = ls_none;
560                   save_argument (&fmacro, out - pfile->out.base);
561
562                   /* A single zero-length argument is no argument.  */
563                   if (fmacro.argc == 1
564                       && m->paramc == 0
565                       && out == pfile->out.base + fmacro.offset + 1)
566                     fmacro.argc = 0;
567
568                   if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
569                     {
570                       /* Remove the macro's invocation from the
571                          output, and push its replacement text.  */
572                       pfile->out.cur = (pfile->out.base
573                                              + fmacro.offset);
574                       CUR (context) = cur;
575                       replace_args_and_push (pfile, &fmacro);
576                       goto new_context;
577                     }
578                 }
579               else if (lex_state == ls_answer || lex_state == ls_defined_close)
580                 lex_state = ls_none;
581             }
582           break;
583
584         case '#':
585           if (out - 1 == pfile->out.base
586               /* A '#' from a macro doesn't start a directive.  */
587               && !pfile->context->prev
588               && !pfile->state.in_directive)
589             {
590               /* A directive.  With the way _cpp_handle_directive
591                  currently works, we only want to call it if either we
592                  know the directive is OK, or we want it to fail and
593                  be removed from the output.  If we want it to be
594                  passed through (the assembler case) then we must not
595                  call _cpp_handle_directive.  */
596               pfile->out.cur = out;
597               cur = skip_whitespace (pfile, cur, true /* skip_comments */);
598               out = pfile->out.cur;
599
600               if (*cur == '\n')
601                 {
602                   /* Null directive.  Ignore it and don't invalidate
603                      the MI optimization.  */
604                   pfile->buffer->need_line = true;
605                   pfile->line++;
606                   result = false;
607                   goto done;
608                 }
609               else
610                 {
611                   bool do_it = false;
612
613                   if (is_numstart (*cur)
614                       && CPP_OPTION (pfile, lang) != CLK_ASM)
615                     do_it = true;
616                   else if (is_idstart (*cur))
617                     /* Check whether we know this directive, but don't
618                        advance.  */
619                     do_it = lex_identifier (pfile, cur)->is_directive;
620
621                   if (do_it || CPP_OPTION (pfile, lang) != CLK_ASM)
622                     {
623                       /* This is a kludge.  We want to have the ISO
624                          preprocessor lex the next token.  */
625                       pfile->buffer->cur = cur;
626                       _cpp_handle_directive (pfile, false /* indented */);
627                       result = false;
628                       goto done;
629                     }
630                 }
631             }
632
633           if (pfile->state.in_expression)
634             {
635               lex_state = ls_hash;
636               continue;
637             }
638           break;
639
640         default:
641           break;
642         }
643
644       /* Non-whitespace disables MI optimization and stops treating
645          '<' as a quote in #include.  */
646       header_ok = false;
647       if (!pfile->state.in_directive)
648         pfile->mi_valid = false;
649
650       if (lex_state == ls_none)
651         continue;
652
653       /* Some of these transitions of state are syntax errors.  The
654          ISO preprocessor will issue errors later.  */
655       if (lex_state == ls_fun_open)
656         /* Missing '('.  */
657         lex_state = ls_none;
658       else if (lex_state == ls_hash
659                || lex_state == ls_predicate
660                || lex_state == ls_defined)
661         lex_state = ls_none;
662
663       /* ls_answer and ls_defined_close keep going until ')'.  */
664     }
665
666  done:
667   if (fmacro.buff)
668     _cpp_release_buff (pfile, fmacro.buff);
669
670   if (lex_state == ls_fun_close)
671     cpp_error_with_line (pfile, DL_ERROR, fmacro.line, 0,
672                          "unterminated argument list invoking macro \"%s\"",
673                          NODE_NAME (fmacro.node));
674   return result;
675 }
676
677 /* Push a context holding the replacement text of the macro NODE on
678    the context stack.  NODE is either object-like, or a function-like
679    macro with no arguments.  */
680 static void
681 push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
682 {
683   size_t len;
684   const uchar *text;
685   uchar *buf;
686
687   if (node->flags & NODE_BUILTIN)
688     {
689       text = _cpp_builtin_macro_text (pfile, node);
690       len = ustrlen (text);
691       buf = _cpp_unaligned_alloc (pfile, len + 1);
692       memcpy (buf, text, len);
693       buf[len]='\n';
694       text = buf;
695     }
696   else
697     {
698       cpp_macro *macro = node->value.macro;
699       macro->used = 1;
700       text = macro->exp.text;
701       len = macro->count;
702     }
703
704   _cpp_push_text_context (pfile, node, text, len);
705 }
706
707 /* Returns TRUE if traditional macro recursion is detected.  */
708 static bool
709 recursive_macro (cpp_reader *pfile, cpp_hashnode *node)
710 {
711   bool recursing = !!(node->flags & NODE_DISABLED);
712
713   /* Object-like macros that are already expanding are necessarily
714      recursive.
715
716      However, it is possible to have traditional function-like macros
717      that are not infinitely recursive but recurse to any given depth.
718      Further, it is easy to construct examples that get ever longer
719      until the point they stop recursing.  So there is no easy way to
720      detect true recursion; instead we assume any expansion more than
721      20 deep since the first invocation of this macro must be
722      recursing.  */
723   if (recursing && node->value.macro->fun_like)
724     {
725       size_t depth = 0;
726       cpp_context *context = pfile->context;
727
728       do
729         {
730           depth++;
731           if (context->macro == node && depth > 20)
732             break;
733           context = context->prev;
734         }
735       while (context);
736       recursing = context != NULL;
737     }
738
739   if (recursing)
740     cpp_error (pfile, DL_ERROR,
741                "detected recursion whilst expanding macro \"%s\"",
742                NODE_NAME (node));
743
744   return recursing;
745 }
746
747 /* Return the length of the replacement text of a function-like or
748    object-like non-builtin macro.  */
749 size_t
750 _cpp_replacement_text_len (const cpp_macro *macro)
751 {
752   size_t len;
753
754   if (macro->fun_like && (macro->paramc != 0))
755     {
756       const uchar *exp;
757
758       len = 0;
759       for (exp = macro->exp.text;;)
760         {
761           struct block *b = (struct block *) exp;
762
763           len += b->text_len;
764           if (b->arg_index == 0)
765             break;
766           len += NODE_LEN (macro->params[b->arg_index - 1]);
767           exp += BLOCK_LEN (b->text_len);
768         }
769     }
770   else
771     len = macro->count;
772   
773   return len;
774 }
775
776 /* Copy the replacement text of MACRO to DEST, which must be of
777    sufficient size.  It is not NUL-terminated.  The next character is
778    returned.  */
779 uchar *
780 _cpp_copy_replacement_text (const cpp_macro *macro, uchar *dest)
781 {
782   if (macro->fun_like && (macro->paramc != 0))
783     {
784       const uchar *exp;
785
786       for (exp = macro->exp.text;;)
787         {
788           struct block *b = (struct block *) exp;
789           cpp_hashnode *param;
790
791           memcpy (dest, b->text, b->text_len);
792           dest += b->text_len;
793           if (b->arg_index == 0)
794             break;
795           param = macro->params[b->arg_index - 1];
796           memcpy (dest, NODE_NAME (param), NODE_LEN (param));
797           dest += NODE_LEN (param);
798           exp += BLOCK_LEN (b->text_len);
799         }
800     }
801   else
802     {
803       memcpy (dest, macro->exp.text, macro->count);
804       dest += macro->count;
805     }
806
807   return dest;
808 }
809
810 /* Push a context holding the replacement text of the macro NODE on
811    the context stack.  NODE is either object-like, or a function-like
812    macro with no arguments.  */
813 static void
814 replace_args_and_push (cpp_reader *pfile, struct fun_macro *fmacro)
815 {
816   cpp_macro *macro = fmacro->node->value.macro;
817
818   if (macro->paramc == 0)
819     push_replacement_text (pfile, fmacro->node);
820   else
821     {
822       const uchar *exp;
823       uchar *p;
824       _cpp_buff *buff;
825       size_t len = 0;
826
827       /* Calculate the length of the argument-replaced text.  */
828       for (exp = macro->exp.text;;)
829         {
830           struct block *b = (struct block *) exp;
831
832           len += b->text_len;
833           if (b->arg_index == 0)
834             break;
835           len += (fmacro->args[b->arg_index]
836                   - fmacro->args[b->arg_index - 1] - 1);
837           exp += BLOCK_LEN (b->text_len);
838         }
839
840       /* Allocate room for the expansion plus \n.  */
841       buff = _cpp_get_buff (pfile, len + 1);
842
843       /* Copy the expansion and replace arguments.  */
844       p = BUFF_FRONT (buff);
845       for (exp = macro->exp.text;;)
846         {
847           struct block *b = (struct block *) exp;
848           size_t arglen;
849
850           memcpy (p, b->text, b->text_len);
851           p += b->text_len;
852           if (b->arg_index == 0)
853             break;
854           arglen = (fmacro->args[b->arg_index]
855                     - fmacro->args[b->arg_index - 1] - 1);
856           memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
857                   arglen);
858           p += arglen;
859           exp += BLOCK_LEN (b->text_len);
860         }
861
862       /* \n-terminate.  */
863       *p = '\n';
864       _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
865
866       /* So we free buffer allocation when macro is left.  */
867       pfile->context->buff = buff;
868     }
869 }
870
871 /* Read and record the parameters, if any, of a function-like macro
872    definition.  Destroys pfile->out.cur.
873
874    Returns true on success, false on failure (syntax error or a
875    duplicate parameter).  On success, CUR (pfile->context) is just
876    past the closing parenthesis.  */
877 static bool
878 scan_parameters (cpp_reader *pfile, cpp_macro *macro)
879 {
880   const uchar *cur = CUR (pfile->context) + 1;
881   bool ok;
882
883   for (;;)
884     {
885       cur = skip_whitespace (pfile, cur, true /* skip_comments */);
886
887       if (is_idstart (*cur))
888         {
889           ok = false;
890           if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
891             break;
892           cur = skip_whitespace (pfile, CUR (pfile->context),
893                                  true /* skip_comments */);
894           if (*cur == ',')
895             {
896               cur++;
897               continue;
898             }
899           ok = (*cur == ')');
900           break;
901         }
902
903       ok = (*cur == ')' && macro->paramc == 0);
904       break;
905     }
906
907   CUR (pfile->context) = cur + (*cur == ')');
908
909   return ok;
910 }
911
912 /* Save the text from pfile->out.base to pfile->out.cur as
913    the replacement text for the current macro, followed by argument
914    ARG_INDEX, with zero indicating the end of the replacement
915    text.  */
916 static void
917 save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
918                        unsigned int arg_index)
919 {
920   size_t len = pfile->out.cur - pfile->out.base;
921   uchar *exp;
922
923   if (macro->paramc == 0)
924     {
925       /* Object-like and function-like macros without parameters
926          simply store their \n-terminated replacement text.  */
927       exp = _cpp_unaligned_alloc (pfile, len + 1);
928       memcpy (exp, pfile->out.base, len);
929       exp[len] = '\n';
930       macro->exp.text = exp;
931       macro->count = len;
932     }
933   else
934     {
935       /* Store the text's length (unsigned int), the argument index
936          (unsigned short, base 1) and then the text.  */
937       size_t blen = BLOCK_LEN (len);
938       struct block *block;
939
940       if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
941         _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
942
943       exp = BUFF_FRONT (pfile->a_buff);
944       block = (struct block *) (exp + macro->count);
945       macro->exp.text = exp;
946
947       /* Write out the block information.  */
948       block->text_len = len;
949       block->arg_index = arg_index;
950       memcpy (block->text, pfile->out.base, len);
951
952       /* Lex the rest into the start of the output buffer.  */
953       pfile->out.cur = pfile->out.base;
954
955       macro->count += blen;
956
957       /* If we've finished, commit the memory.  */
958       if (arg_index == 0)
959         BUFF_FRONT (pfile->a_buff) += macro->count;
960     }
961 }
962
963 /* Analyze and save the replacement text of a macro.  Returns true on
964    success.  */
965 bool
966 _cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
967 {
968   const uchar *cur;
969   uchar *limit;
970   cpp_context *context = pfile->context;
971
972   /* The context has not been set up for command line defines, and CUR
973      has not been updated for the macro name for in-file defines.  */
974   pfile->out.cur = pfile->out.base;
975   CUR (context) = pfile->buffer->cur;
976   RLIMIT (context) = pfile->buffer->rlimit;
977   check_output_buffer (pfile, RLIMIT (context) - CUR (context));
978
979   /* Is this a function-like macro?  */
980   if (* CUR (context) == '(')
981     {
982       /* Setting macro to NULL indicates an error occurred, and
983          prevents unnecessary work in _cpp_scan_out_logical_line.  */
984       if (!scan_parameters (pfile, macro))
985         macro = NULL;
986       else
987         {
988           /* Success.  Commit the parameter array.  */
989           macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
990           BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
991           macro->fun_like = 1;
992         }
993     }
994
995   /* Skip leading whitespace in the replacement text.  */
996   pfile->buffer->cur
997     = skip_whitespace (pfile, CUR (context),
998                        CPP_OPTION (pfile, discard_comments_in_macro_exp));
999
1000   pfile->state.prevent_expansion++;
1001   _cpp_scan_out_logical_line (pfile, macro);
1002   pfile->state.prevent_expansion--;
1003
1004   if (!macro)
1005     return false;
1006
1007   /* Skip trailing white space.  */
1008   cur = pfile->out.base;
1009   limit = pfile->out.cur;
1010   while (limit > cur && is_space (limit[-1]))
1011     limit--;
1012   pfile->out.cur = limit;
1013   save_replacement_text (pfile, macro, 0);
1014
1015   return true;
1016 }
1017
1018 /* Copy SRC of length LEN to DEST, but convert all contiguous
1019    whitespace to a single space, provided it is not in quotes.  The
1020    quote currently in effect is pointed to by PQUOTE, and is updated
1021    by the function.  Returns the number of bytes copied.  */
1022 static size_t
1023 canonicalize_text (uchar *dest, const uchar *src, size_t len, uchar *pquote)
1024 {
1025   uchar *orig_dest = dest;
1026   uchar quote = *pquote;
1027
1028   while (len)
1029     {
1030       if (is_space (*src) && !quote)
1031         {
1032           do
1033             src++, len--;
1034           while (len && is_space (*src));
1035           *dest++ = ' ';
1036         }
1037       else
1038         {
1039           if (*src == '\'' || *src == '"')
1040             {
1041               if (!quote)
1042                 quote = *src;
1043               else if (quote == *src)
1044                 quote = 0;
1045             }
1046           *dest++ = *src++, len--;
1047         }
1048     }
1049
1050   *pquote = quote;
1051   return dest - orig_dest;
1052 }
1053
1054 /* Returns true if MACRO1 and MACRO2 have expansions different other
1055    than in the form of their whitespace.  */
1056 bool
1057 _cpp_expansions_different_trad (const cpp_macro *macro1,
1058                                 const cpp_macro *macro2)
1059 {
1060   uchar *p1 = xmalloc (macro1->count + macro2->count);
1061   uchar *p2 = p1 + macro1->count;
1062   uchar quote1 = 0, quote2 = 0;
1063   bool mismatch;
1064   size_t len1, len2;
1065
1066   if (macro1->paramc > 0)
1067     {
1068       const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
1069
1070       mismatch = true;
1071       for (;;)
1072         {
1073           struct block *b1 = (struct block *) exp1;
1074           struct block *b2 = (struct block *) exp2;
1075
1076           if (b1->arg_index != b2->arg_index)
1077             break;
1078
1079           len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
1080           len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
1081           if (len1 != len2 || memcmp (p1, p2, len1))
1082             break;
1083           if (b1->arg_index == 0)
1084             {
1085               mismatch = false;
1086               break;
1087             }
1088           exp1 += BLOCK_LEN (b1->text_len);
1089           exp2 += BLOCK_LEN (b2->text_len);
1090         }
1091     }
1092   else
1093     {
1094       len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
1095       len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
1096       mismatch = (len1 != len2 || memcmp (p1, p2, len1));
1097     }
1098
1099   free (p1);
1100   return mismatch;
1101 }