OSDN Git Service

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