OSDN Git Service

* cpphash.h (_cpp_lex_identifier_trad): Remove.
[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 in that text.
27
28    Each block comprises the length of text contained therein, the
29    one-based index of the argument that immediately follows that text,
30    and the text itself.  The final block in the macro expansion is
31    easily 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   /* Zero-based index of argument being currently lexed.  */
63   unsigned int argc;
64 };
65
66 /* Lexing TODO: Maybe handle -CC and space in escaped newlines.  Stop
67    cpplex.c from recognizing comments and directives during its lexing
68    pass.  Get rid of line_base usage - seems pointless?  */
69
70 static const uchar *handle_newline PARAMS ((cpp_reader *, const uchar *));
71 static const uchar *skip_escaped_newlines PARAMS ((cpp_reader *,
72                                                    const uchar *));
73 static const uchar *skip_whitespace PARAMS ((cpp_reader *, const uchar *));
74 static cpp_hashnode *lex_identifier PARAMS ((cpp_reader *, const uchar *));
75 static const uchar *copy_comment PARAMS ((cpp_reader *, const uchar *));
76 static void scan_out_logical_line PARAMS ((cpp_reader *pfile, cpp_macro *));
77 static void check_output_buffer PARAMS ((cpp_reader *, size_t));
78 static void push_replacement_text PARAMS ((cpp_reader *, cpp_hashnode *));
79 static bool scan_parameters PARAMS ((cpp_reader *, cpp_macro *));
80 static bool recursive_macro PARAMS ((cpp_reader *, cpp_hashnode *));
81 static void save_replacement_text PARAMS ((cpp_reader *, cpp_macro *,
82                                            unsigned int));
83 static void maybe_start_funlike PARAMS ((cpp_reader *, cpp_hashnode *,
84                                          const uchar *, struct fun_macro *));
85 static void save_argument PARAMS ((struct fun_macro *, size_t));
86 static void replace_args_and_push PARAMS ((cpp_reader *, struct fun_macro *));
87 static size_t canonicalize_text PARAMS ((uchar *, const uchar *, size_t,
88                                          uchar *));
89
90 /* Ensures we have N bytes' space in the output buffer, and
91    reallocates it if not.  */
92 static void
93 check_output_buffer (pfile, n)
94      cpp_reader *pfile;
95      size_t n;
96 {
97   /* We might need two bytes to terminate an unterminated comment, and
98      one more to terminate with a NUL.  */
99   n += 2 + 1;
100
101   if (n > (size_t) (pfile->out.limit - pfile->out.cur))
102     {
103       size_t size = pfile->out.cur - pfile->out.base;
104       size_t new_size = (size + n) * 3 / 2;
105
106       pfile->out.base
107         = (uchar *) xrealloc (pfile->out.base, new_size);
108       pfile->out.limit = pfile->out.base + new_size;
109       pfile->out.cur = pfile->out.base + size;
110     }
111 }
112
113 /* To be called whenever a newline character is encountered in the
114    input file, at CUR.  Handles DOS, MAC and Unix ends of line, and
115    returns the character after the newline sequence.  */
116 static const uchar *
117 handle_newline (pfile, cur)
118      cpp_reader *pfile;
119      const uchar *cur;
120 {
121   pfile->line++;
122   if (cur[0] + cur[1] == '\r' + '\n')
123     cur++;
124   pfile->buffer->line_base = cur + 1;
125   return cur + 1;
126 }
127
128 /* CUR points to any character in the buffer, not necessarily a
129    backslash.  Advances CUR until all escaped newlines are skipped,
130    and returns the new position.  */
131 static const uchar *
132 skip_escaped_newlines (pfile, cur)
133      cpp_reader *pfile;
134      const uchar *cur;
135 {
136   if (*cur == '\\' && is_vspace (cur[1]))
137     {
138       do
139         cur = handle_newline (pfile, cur + 1);
140       while (*cur == '\\' && is_vspace (cur[1]));
141
142       if (cur == RLIMIT (pfile->context))
143         cpp_error (pfile, DL_PEDWARN,
144                    "backslash-newline at end of file");
145     }
146
147   return cur;
148 }
149
150 /* CUR points to the character after the asterisk introducing a
151    comment in the input buffer.  The remaining comment is copied to
152    the buffer pointed to by pfile->out.cur, which must be of
153    sufficient size, and pfile->out.cur is updated.  Unterminated
154    comments are diagnosed, and correctly terminated in the output.
155
156    Returns a pointer to the first character after the comment in the
157    input buffer.  */
158 static const uchar *
159 copy_comment (pfile, cur)
160      cpp_reader *pfile;
161      const uchar *cur;
162 {
163   unsigned int from_line = pfile->line;
164   const uchar *limit = RLIMIT (pfile->context);
165   uchar *out = pfile->out.cur;
166
167   while (cur < limit)
168     {
169       unsigned int c = *cur++;
170       *out++ = c;
171
172       if (c == '/')
173         {
174           /* An immediate slash does not terminate the comment.  */
175           if (out[-2] == '*' && out > pfile->out.cur + 1)
176             goto done;
177
178           if (*cur == '*' && cur[1] != '/'
179               && CPP_OPTION (pfile, warn_comments))
180             cpp_error_with_line (pfile, DL_WARNING, pfile->line, 0,
181                                  "\"/*\" within comment");
182         }
183       else if (is_vspace (c))
184         {
185           cur = handle_newline (pfile, cur - 1);
186           /* Canonicalize newline sequences and skip escaped ones.  */
187           if (out[-2] == '\\')
188             out -= 2;
189           else
190             out[-1] = '\n';
191         }
192     }
193
194   cpp_error_with_line (pfile, DL_ERROR, from_line, 0, "unterminated comment");
195   *out++ = '*';
196   *out++ = '/';
197
198  done:
199   pfile->out.cur = out;
200   return cur;
201 }
202
203 /* Skip any horizontal whitespace and comments beginning at CUR,
204    returning the following character.  */
205 static const uchar *
206 skip_whitespace (pfile, cur)
207      cpp_reader *pfile;
208      const uchar *cur;
209 {
210   const uchar *tmp;
211
212   for (;;)
213     {
214       while (is_nvspace (*cur) && *cur != 0)
215         cur++;
216
217       if (*cur == '\0' && cur != RLIMIT (pfile->context))
218         continue;
219
220       if (*cur == '\\')
221         {
222           tmp = cur;
223           cur = skip_escaped_newlines (pfile, cur);
224           if (tmp != cur)
225             continue;
226         }
227
228       if (*cur == '/')
229         {
230           tmp = skip_escaped_newlines (pfile, cur + 1);
231           if (*tmp == '*')
232             {
233               cur = copy_comment (pfile, tmp + 1);
234               continue;
235             }
236         }
237
238       break;
239     }
240
241   return cur;
242 }
243
244 /* Lexes and outputs an identifier starting at CUR, which is assumed
245    to point to a valid first character of an identifier.  Returns
246    the hashnode, and updates out.cur.  */
247 static cpp_hashnode *
248 lex_identifier (pfile, cur)
249      cpp_reader *pfile;
250      const uchar *cur;
251 {
252   size_t len;
253   uchar *out = pfile->out.cur;
254   cpp_hashnode *result;
255
256   do
257     {
258       do
259         *out++ = *cur++;
260       while (is_numchar (*cur));
261       cur = skip_escaped_newlines (pfile, cur);
262     }
263   while (is_numchar (*cur));
264
265   CUR (pfile->context) = cur;
266   len = out - pfile->out.cur;
267   result = (cpp_hashnode *) ht_lookup (pfile->hash_table, pfile->out.cur,
268                                        len, HT_ALLOC);
269   pfile->out.cur = out;
270   return result;
271 }
272
273 /* Overlays the true file buffer temporarily with text of length LEN
274    starting at START.  The true buffer is restored upon calling
275    restore_buff().  */
276 void
277 _cpp_overlay_buffer (pfile, start, len)
278      cpp_reader *pfile;
279      const uchar *start;
280      size_t len;
281 {
282   cpp_buffer *buffer = pfile->buffer;
283
284   buffer->saved_cur = buffer->cur;
285   buffer->saved_rlimit = buffer->rlimit;
286   buffer->saved_line_base = buffer->line_base;
287
288   buffer->cur = start;
289   buffer->line_base = start;
290   buffer->rlimit = start + len;
291
292   pfile->saved_line = pfile->line;
293 }
294
295 /* Restores a buffer overlaid by _cpp_overlay_buffer().  */
296 void
297 _cpp_remove_overlay (pfile)
298      cpp_reader *pfile;
299 {
300   cpp_buffer *buffer = pfile->buffer;
301
302   buffer->cur = buffer->saved_cur;
303   buffer->rlimit = buffer->saved_rlimit;
304   buffer->line_base = buffer->saved_line_base;
305
306   pfile->line = pfile->saved_line;
307 }
308
309 /* Reads a logical line into the output buffer.  Returns TRUE if there
310    is more text left in the buffer.  */
311 bool
312 _cpp_read_logical_line_trad (pfile)
313      cpp_reader *pfile;
314 {
315   cpp_buffer *buffer = pfile->buffer;
316
317   do
318     {
319       if (buffer->cur == buffer->rlimit)
320         {
321           bool stop = true;
322
323           /* Don't pop the last buffer.  */
324           if (buffer->prev)
325             {
326               stop = buffer->return_at_eof;
327               _cpp_pop_buffer (pfile);
328             }
329
330           if (stop)
331             return false;
332         }
333
334       CUR (pfile->context) = buffer->cur;
335       RLIMIT (pfile->context) = buffer->rlimit;
336       scan_out_logical_line (pfile, NULL);
337       buffer->cur = CUR (pfile->context);
338     }
339   while (pfile->state.skipping);
340
341   return true;
342 }
343
344 /* Set up state for finding the opening '(' of a function-like
345    macro.  */
346 static void
347 maybe_start_funlike (pfile, node, start, macro)
348      cpp_reader *pfile;
349      cpp_hashnode *node;
350      const uchar *start;
351      struct fun_macro *macro;
352 {
353   unsigned int n = node->value.macro->paramc + 1;
354
355   if (macro->buff)
356     _cpp_release_buff (pfile, macro->buff);
357   macro->buff = _cpp_get_buff (pfile, n * sizeof (size_t));
358   macro->args = (size_t *) BUFF_FRONT (macro->buff);
359   macro->node = node;
360   macro->offset = start - pfile->out.base;
361   macro->argc = 0;
362
363   pfile->state.parsing_args = 1;
364 }
365
366 /* Save the OFFSET of the start of the next argument to MACRO.  */
367 static void
368 save_argument (macro, offset)
369      struct fun_macro *macro;
370      size_t offset;
371 {
372   macro->argc++;
373   if (macro->argc <= macro->node->value.macro->paramc)
374     macro->args[macro->argc] = offset;
375 }
376
377 /* Copies the next logical line in the current buffer to the output
378    buffer.  The output is guaranteed to terminate with a NUL
379    character.
380
381    If MACRO is non-NULL, then we are scanning the replacement list of
382    MACRO, and we call save_replacement_text() every time we meet an
383    argument.  */
384 static void
385 scan_out_logical_line (pfile, macro)
386      cpp_reader *pfile;
387      cpp_macro *macro;
388 {
389   cpp_context *context;
390   const uchar *cur;
391   unsigned int c, paren_depth = 0, quote = 0;
392   uchar *out;
393   struct fun_macro fmacro;
394
395   fmacro.buff = NULL;
396
397  start_logical_line:
398   pfile->out.cur = pfile->out.base;
399   pfile->out.first_line = pfile->line;
400  new_context:
401   context = pfile->context;
402   cur = CUR (context);
403   check_output_buffer (pfile, RLIMIT (context) - cur);
404   out = pfile->out.cur;
405
406   for (;;)
407     {
408       c = *cur++;
409       *out++ = c;
410
411       /* There are only a few entities we need to catch: comments,
412          identifiers, newlines, escaped newlines, # and '\0'.  */
413       switch (c)
414         {
415         case '\0':
416           if (cur - 1 != RLIMIT (context))
417             break;
418
419           /* If this is a macro's expansion, pop it.  */
420           if (context->prev)
421             {
422               pfile->out.cur = out - 1;
423               _cpp_pop_context (pfile);
424               goto new_context;
425             }
426
427           /* Premature end of file.  Fake a new line.  */
428           cur--;
429           if (!pfile->buffer->from_stage3)
430             cpp_error (pfile, DL_PEDWARN, "no newline at end of file");
431           if (pfile->state.parsing_args == 2)
432             cpp_error (pfile, DL_ERROR,
433                        "unterminated argument list invoking macro \"%s\"",
434                        NODE_NAME (fmacro.node));
435           pfile->line++;
436           goto done;
437
438         case '\r': case '\n':
439           cur = handle_newline (pfile, cur - 1);
440           if (pfile->state.parsing_args == 2)
441             {
442               /* Newlines in arguments become a space.  */
443               out[-1] = ' ';
444               continue;
445             }
446           goto done;
447
448         case '"':
449         case '\'':
450           if (c == quote)
451             quote = 0;
452           else if (!quote)
453             quote = c;
454           break;
455
456         case '\\':
457           if (is_vspace (*cur))
458             out--, cur = skip_escaped_newlines (pfile, cur - 1);
459           else
460             {
461               /* Skip escaped quotes here, it's easier than above, but
462                  take care to first skip escaped newlines.  */
463               cur = skip_escaped_newlines (pfile, cur);
464               if (*cur == '\\' || *cur == '"' || *cur == '\'')
465                 *out++ = *cur++;
466             }
467           break;
468
469         case '/':
470           /* Traditional CPP does not recognize comments within
471              literals.  */
472           if (!quote)
473             {
474               cur = skip_escaped_newlines (pfile, cur);
475               if (*cur == '*')
476                 {
477                   *out = '*';
478                   pfile->out.cur = out + 1;
479                   cur = copy_comment (pfile, cur + 1);
480
481                   /* Comments in directives become spaces so that
482                      tokens are properly separated when the ISO
483                      preprocessor re-lexes the line.  The exception
484                      is #define.  */
485                   if (pfile->state.in_directive && !macro)
486                     out[-1] = ' ';
487                   else if (CPP_OPTION (pfile, discard_comments))
488                     out -= 1;
489                   else
490                     out = pfile->out.cur;
491                 }
492             }
493           break;
494
495         case '_':
496         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
497         case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
498         case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
499         case 's': case 't': case 'u': case 'v': case 'w': case 'x':
500         case 'y': case 'z':
501         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
502         case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
503         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
504         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
505         case 'Y': case 'Z':
506           if (quote == 0 || macro)
507             {
508               cpp_hashnode *node;
509
510               pfile->out.cur = --out;
511               node = lex_identifier (pfile, cur - 1);
512
513               if (node->type == NT_MACRO
514                   && !pfile->state.skipping
515                   && pfile->state.parsing_args != 2
516                   && !pfile->state.prevent_expansion
517                   && !recursive_macro (pfile, node))
518                 {
519                   if (node->value.macro->fun_like)
520                     maybe_start_funlike (pfile, node, out, &fmacro);
521                   else
522                     {
523                       /* Remove the object-like macro's name from the
524                          output, and push its replacement text.  */
525                       pfile->out.cur = out;
526                       push_replacement_text (pfile, node);
527                       goto new_context;
528                     }
529                 }
530               else if (macro && node->arg_index)
531                 {
532                   /* Found a parameter in the replacement text of a
533                      #define.  Remove its name from the output.  */
534                   pfile->out.cur = out;
535                   save_replacement_text (pfile, macro, node->arg_index);
536                 }
537
538               out = pfile->out.cur;
539               cur = CUR (context);
540             }
541           break;
542
543         case '(':
544           if (quote == 0)
545             {
546               paren_depth++;
547               if (pfile->state.parsing_args == 1)
548                 {
549                   const uchar *p = pfile->out.base + fmacro.offset;
550
551                   /* Invoke a prior function-like macro if there is only
552                      white space in-between.  */
553                   while (is_numchar (*p))
554                     p++;
555                   while (is_space (*p))
556                     p++;
557
558                   if (p == out - 1)
559                     {
560                       pfile->state.parsing_args = 2;
561                       paren_depth = 1;
562                       out = pfile->out.base + fmacro.offset;
563                       fmacro.args[0] = fmacro.offset;
564                     }
565                   else
566                     pfile->state.parsing_args = 0;
567                 }
568             }
569           break;
570
571         case ',':
572           if (quote == 0 && pfile->state.parsing_args == 2 && paren_depth == 1)
573             save_argument (&fmacro, out - pfile->out.base);
574           break;
575
576         case ')':
577           if (quote == 0)
578             {
579               paren_depth--;
580               if (pfile->state.parsing_args == 2 && paren_depth == 0)
581                 {
582                   cpp_macro *m = fmacro.node->value.macro;
583
584                   pfile->state.parsing_args = 0;
585                   save_argument (&fmacro, out - pfile->out.base);
586
587                   /* A single zero-length argument is no argument.  */
588                   if (fmacro.argc == 1
589                       && m->paramc == 0
590                       && out == pfile->out.base + 1)
591                     fmacro.argc = 0;
592
593                   if (_cpp_arguments_ok (pfile, m, fmacro.node, fmacro.argc))
594                     {
595                       /* Remove the macro's invocation from the
596                          output, and push its replacement text.  */
597                       pfile->out.cur = (pfile->out.base
598                                              + fmacro.offset);
599                       CUR (context) = cur;
600                       replace_args_and_push (pfile, &fmacro);
601                       goto new_context;
602                     }
603                 }
604             }
605           break;
606
607         case '#':
608           /* At start of a line it's a directive.  */
609           if (out - 1 == pfile->out.base && !pfile->state.in_directive)
610             {
611               /* This is a kludge.  We want to have the ISO
612                  preprocessor lex the next token.  */
613               pfile->buffer->cur = cur;
614               if (_cpp_handle_directive (pfile, false /* indented */))
615                 goto start_logical_line;
616             }
617           break;
618
619         default:
620           break;
621         }
622     }
623
624  done:
625   out[-1] = '\0';
626   CUR (context) = cur;
627   pfile->out.cur = out - 1;
628   if (fmacro.buff)
629     _cpp_release_buff (pfile, fmacro.buff);
630 }
631
632 /* Push a context holding the replacement text of the macro NODE on
633    the context stack.  NODE is either object-like, or a function-like
634    macro with no arguments.  */
635 static void
636 push_replacement_text (pfile, node)
637      cpp_reader *pfile;
638      cpp_hashnode *node;
639 {
640   cpp_macro *macro = node->value.macro;
641
642   _cpp_push_text_context (pfile, node, macro->exp.text, macro->count);
643 }
644
645 /* Returns TRUE if traditional macro recursion is detected.  */
646 static bool
647 recursive_macro (pfile, node)
648      cpp_reader *pfile;
649      cpp_hashnode *node;
650 {
651   bool recursing = node->flags & NODE_DISABLED;
652
653   /* Object-like macros that are already expanding are necessarily
654      recursive.
655
656      However, it is possible to have traditional function-like macros
657      that are not infinitely recursive but recurse to any given depth.
658      Further, it is easy to construct examples that get ever longer
659      until the point they stop recursing.  So there is no easy way to
660      detect true recursion; instead we assume any expansion more than
661      20 deep since the first invocation of this macro must be
662      recursing.  */
663   if (recursing && node->value.macro->fun_like)
664     {
665       size_t depth = 0;
666       cpp_context *context = pfile->context;
667
668       do
669         {
670           depth++;
671           if (context->macro == node && depth > 20)
672             break;
673           context = context->prev;
674         }
675       while (context);
676       recursing = context != NULL;
677     }
678
679   if (recursing)
680     cpp_error (pfile, DL_ERROR,
681                "detected recursion whilst expanding macro \"%s\"",
682                NODE_NAME (node));
683
684   return recursing;
685 }
686
687 /* Push a context holding the replacement text of the macro NODE on
688    the context stack.  NODE is either object-like, or a function-like
689    macro with no arguments.  */
690 static void
691 replace_args_and_push (pfile, fmacro)
692      cpp_reader *pfile;
693      struct fun_macro *fmacro;
694 {
695   cpp_macro *macro = fmacro->node->value.macro;
696
697   if (macro->paramc == 0)
698     push_replacement_text (pfile, fmacro->node);
699   else
700     {
701       const uchar *exp;
702       uchar *p;
703       _cpp_buff *buff;
704       size_t len = 0;
705
706       /* Calculate the length of the argument-replaced text.  */
707       for (exp = macro->exp.text;;)
708         {
709           struct block *b = (struct block *) exp;
710
711           len += b->text_len;
712           if (b->arg_index == 0)
713             break;
714           len += (fmacro->args[b->arg_index]
715                   - fmacro->args[b->arg_index - 1] - 1);
716           exp += BLOCK_LEN (b->text_len);
717         }
718
719       /* Allocate room for the expansion plus NUL.  */
720       buff = _cpp_get_buff (pfile, len + 1);
721
722       /* Copy the expansion and replace arguments.  */
723       p = BUFF_FRONT (buff);
724       for (exp = macro->exp.text;;)
725         {
726           struct block *b = (struct block *) exp;
727           size_t arglen;
728
729           memcpy (p, b->text, b->text_len);
730           p += b->text_len;
731           if (b->arg_index == 0)
732             break;
733           arglen = (fmacro->args[b->arg_index]
734                     - fmacro->args[b->arg_index - 1] - 1);
735           memcpy (p, pfile->out.base + fmacro->args[b->arg_index - 1],
736                   arglen);
737           p += arglen;
738           exp += BLOCK_LEN (b->text_len);
739         }
740
741       /* NUL-terminate.  */
742       *p = '\0';
743       _cpp_push_text_context (pfile, fmacro->node, BUFF_FRONT (buff), len);
744
745       /* So we free buffer allocation when macro is left.  */
746       pfile->context->buff = buff;
747     }
748 }
749
750 /* Read and record the parameters, if any, of a function-like macro
751    definition.  Destroys pfile->out.cur.
752
753    Returns true on success, false on failure (syntax error or a
754    duplicate parameter).  On success, CUR (pfile->context) is just
755    past the closing parenthesis.  */
756 static bool
757 scan_parameters (pfile, macro)
758      cpp_reader *pfile;
759      cpp_macro *macro;
760 {
761   const uchar *cur = CUR (pfile->context) + 1;
762   bool ok;
763
764   for (;;)
765     {
766       cur = skip_whitespace (pfile, cur);
767
768       if (is_idstart (*cur))
769         {
770           ok = false;
771           if (_cpp_save_parameter (pfile, macro, lex_identifier (pfile, cur)))
772             break;
773           cur = skip_whitespace (pfile, CUR (pfile->context));
774           if (*cur == ',')
775             {
776               cur++;
777               continue;
778             }
779           ok = (*cur == ')');
780           break;
781         }
782
783       ok = (*cur == ')' && macro->paramc == 0);
784       break;
785     }
786
787   CUR (pfile->context) = cur + (*cur == ')');
788
789   return ok;
790 }
791
792 /* Save the text from pfile->out.base to pfile->out.cur as
793    the replacement text for the current macro, followed by argument
794    ARG_INDEX, with zero indicating the end of the replacement
795    text.  */
796 static void
797 save_replacement_text (pfile, macro, arg_index)
798      cpp_reader *pfile;
799      cpp_macro *macro;
800      unsigned int arg_index;
801 {
802   size_t len = pfile->out.cur - pfile->out.base;
803   uchar *exp;
804
805   if (macro->paramc == 0)
806     {
807       /* Object-like and function-like macros without parameters
808          simply store their NUL-terminated replacement text.  */
809       exp = _cpp_unaligned_alloc (pfile, len + 1);
810       memcpy (exp, pfile->out.base, len);
811       exp[len] = '\0';
812       macro->exp.text = exp;
813       macro->count = len;
814     }
815   else
816     {
817       /* Store the text's length (unsigned int), the argument index
818          (unsigned short, base 1) and then the text.  */
819       size_t blen = BLOCK_LEN (len);
820       struct block *block;
821
822       if (macro->count + blen > BUFF_ROOM (pfile->a_buff))
823         _cpp_extend_buff (pfile, &pfile->a_buff, macro->count + blen);
824
825       exp = BUFF_FRONT (pfile->a_buff);
826       block = (struct block *) (exp + macro->count);
827       macro->exp.text = exp;
828
829       /* Write out the block information.  */
830       block->text_len = len;
831       block->arg_index = arg_index;
832       memcpy (block->text, pfile->out.base, len);
833
834       /* Lex the rest into the start of the output buffer.  */
835       pfile->out.cur = pfile->out.base;
836
837       macro->count += blen;
838
839       /* If we've finished, commit the memory.  */
840       if (arg_index == 0)
841         BUFF_FRONT (pfile->a_buff) += macro->count;
842     }
843 }
844
845 /* Analyze and save the replacement text of a macro.  Returns true on
846    success.  */
847 bool
848 _cpp_create_trad_definition (pfile, macro)
849      cpp_reader *pfile;
850      cpp_macro *macro;
851 {
852   const uchar *cur;
853   uchar *limit;
854
855   CUR (pfile->context) = pfile->buffer->cur;
856
857   /* Is this a function-like macro?  */
858   if (* CUR (pfile->context) == '(')
859     {
860       /* Setting macro to NULL indicates an error occurred, and
861          prevents unnecessary work in scan_out_logical_line.  */
862       if (!scan_parameters (pfile, macro))
863         macro = NULL;
864       else
865         {
866           /* Success.  Commit the parameter array.  */
867           macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
868           BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
869           macro->fun_like = 1;
870         }
871     }
872
873   /* Skip leading whitespace in the replacement text.  */
874   CUR (pfile->context) = skip_whitespace (pfile, CUR (pfile->context));
875
876   pfile->state.prevent_expansion++;
877   scan_out_logical_line (pfile, macro);
878   pfile->state.prevent_expansion--;
879
880   if (!macro)
881     return false;
882
883   /* Skip trailing white space.  */
884   cur = pfile->out.base;
885   limit = pfile->out.cur;
886   while (limit > cur && is_space (limit[-1]))
887     limit--;
888   pfile->out.cur = limit;
889   save_replacement_text (pfile, macro, 0);
890
891   return true;
892 }
893
894 /* Copy SRC of length LEN to DEST, but convert all contiguous
895    whitespace to a single space, provided it is not in quotes.  The
896    quote currently in effect is pointed to by PQUOTE, and is updated
897    by the function.  Returns the number of bytes copied.  */
898 static size_t
899 canonicalize_text (dest, src, len, pquote)
900      uchar *dest;
901      const uchar *src;
902      size_t len;
903      uchar *pquote;
904 {
905   uchar *orig_dest = dest;
906   uchar quote = *pquote;
907
908   while (len)
909     {
910       if (is_space (*src) && !quote)
911         {
912           do
913             src++, len--;
914           while (len && is_space (*src));
915           *dest++ = ' ';
916         }
917       else
918         {
919           if (*src == '\'' || *src == '"')
920             {
921               if (!quote)
922                 quote = *src;
923               else if (quote == *src)
924                 quote = 0;
925             }
926           *dest++ = *src++, len--;
927         }
928     }
929
930   *pquote = quote;
931   return dest - orig_dest;
932 }
933
934 /* Returns true if MACRO1 and MACRO2 have expansions different other
935    than in the form of their whitespace.  */
936 bool
937 _cpp_expansions_different_trad (macro1, macro2)
938      const cpp_macro *macro1, *macro2;
939 {
940   uchar *p1 = xmalloc (macro1->count + macro2->count);
941   uchar *p2 = p1 + macro1->count;
942   uchar quote1 = 0, quote2;
943   bool mismatch;
944   size_t len1, len2;
945
946   if (macro1->paramc > 0)
947     {
948       const uchar *exp1 = macro1->exp.text, *exp2 = macro2->exp.text;
949
950       mismatch = true;
951       for (;;)
952         {
953           struct block *b1 = (struct block *) exp1;
954           struct block *b2 = (struct block *) exp2;
955
956           if (b1->arg_index != b2->arg_index)
957             break;
958
959           len1 = canonicalize_text (p1, b1->text, b1->text_len, &quote1);
960           len2 = canonicalize_text (p2, b2->text, b2->text_len, &quote2);
961           if (len1 != len2 || memcmp (p1, p2, len1))
962             break;
963           if (b1->arg_index == 0)
964             {
965               mismatch = false;
966               break;
967             }
968           exp1 += BLOCK_LEN (b1->text_len);
969           exp2 += BLOCK_LEN (b2->text_len);
970         }
971     }
972   else
973     {
974       len1 = canonicalize_text (p1, macro1->exp.text, macro1->count, &quote1);
975       len2 = canonicalize_text (p2, macro2->exp.text, macro2->count, &quote2);
976       mismatch = (len1 != len2 || memcmp (p1, p2, len1));
977     }
978
979   free (p1);
980   return mismatch;
981 }
982
983 /* Prepare to be able to scan the current buffer.  */
984 void
985 _cpp_set_trad_context (pfile)
986      cpp_reader *pfile;
987 {
988   cpp_buffer *buffer = pfile->buffer;
989   cpp_context *context = pfile->context;
990
991   if (pfile->context->prev)
992     abort ();
993
994   pfile->out.cur = pfile->out.base;
995   CUR (context) = buffer->cur;
996   RLIMIT (context) = buffer->rlimit;
997   check_output_buffer (pfile, RLIMIT (context) - CUR (context));
998 }