OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libcpp/] [macro.c] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 730 jeremybenn
/* Part of CPP library.  (Macro and #define handling.)
2
   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
3
   1999, 2000, 2001, 2002, 2003, 2004, 2005,
4
   2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5
   Written by Per Bothner, 1994.
6
   Based on CCCP program by Paul Rubin, June 1986
7
   Adapted to ANSI C, Richard Stallman, Jan 1987
8
 
9
This program is free software; you can redistribute it and/or modify it
10
under the terms of the GNU General Public License as published by the
11
Free Software Foundation; either version 3, or (at your option) any
12
later version.
13
 
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with this program; see the file COPYING3.  If not see
21
<http://www.gnu.org/licenses/>.
22
 
23
 In other words, you are welcome to use, share and improve this program.
24
 You are forbidden to forbid anyone else to use, share and improve
25
 what you give them.   Help stamp out software-hoarding!  */
26
 
27
#include "config.h"
28
#include "system.h"
29
#include "cpplib.h"
30
#include "internal.h"
31
 
32
typedef struct macro_arg macro_arg;
33
/* This structure represents the tokens of a macro argument.  These
34
   tokens can be macro themselves, in which case they can be either
35
   expanded or unexpanded.  When they are expanded, this data
36
   structure keeps both the expanded and unexpanded forms.  */
37
struct macro_arg
38
{
39
  const cpp_token **first;      /* First token in unexpanded argument.  */
40
  const cpp_token **expanded;   /* Macro-expanded argument.  */
41
  const cpp_token *stringified; /* Stringified argument.  */
42
  unsigned int count;           /* # of tokens in argument.  */
43
  unsigned int expanded_count;  /* # of tokens in expanded argument.  */
44
  source_location *virt_locs;   /* Where virtual locations for
45
                                   unexpanded tokens are stored.  */
46
  source_location *expanded_virt_locs; /* Where virtual locations for
47
                                          expanded tokens are
48
                                          stored.  */
49
};
50
 
51
/* The kind of macro tokens which the instance of
52
   macro_arg_token_iter is supposed to iterate over.  */
53
enum macro_arg_token_kind {
54
  MACRO_ARG_TOKEN_NORMAL,
55
  /* This is a macro argument token that got transformed into a string
56
     litteral, e.g. #foo.  */
57
  MACRO_ARG_TOKEN_STRINGIFIED,
58
  /* This is a token resulting from the expansion of a macro
59
     argument that was itself a macro.  */
60
  MACRO_ARG_TOKEN_EXPANDED
61
};
62
 
63
/* An iterator over tokens coming from a function-like macro
64
   argument.  */
65
typedef struct macro_arg_token_iter macro_arg_token_iter;
66
struct macro_arg_token_iter
67
{
68
  /* Whether or not -ftrack-macro-expansion is used.  */
69
  bool track_macro_exp_p;
70
  /* The kind of token over which we are supposed to iterate.  */
71
  enum macro_arg_token_kind kind;
72
  /* A pointer to the current token pointed to by the iterator.  */
73
  const cpp_token **token_ptr;
74
  /* A pointer to the "full" location of the current token.  If
75
     -ftrack-macro-expansion is used this location tracks loci accross
76
     macro expansion.  */
77
  const source_location *location_ptr;
78
#ifdef ENABLE_CHECKING
79
  /* The number of times the iterator went forward. This useful only
80
     when checking is enabled.  */
81
  size_t num_forwards;
82
#endif
83
};
84
 
85
/* Macro expansion.  */
86
 
87
static int enter_macro_context (cpp_reader *, cpp_hashnode *,
88
                                const cpp_token *, source_location);
89
static int builtin_macro (cpp_reader *, cpp_hashnode *);
90
static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
91
                                 const cpp_token **, unsigned int);
92
static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
93
                                          _cpp_buff *, source_location *,
94
                                          const cpp_token **, unsigned int);
95
static _cpp_buff *collect_args (cpp_reader *, const cpp_hashnode *,
96
                                _cpp_buff **, unsigned *);
97
static cpp_context *next_context (cpp_reader *);
98
static const cpp_token *padding_token (cpp_reader *, const cpp_token *);
99
static void expand_arg (cpp_reader *, macro_arg *);
100
static const cpp_token *new_string_token (cpp_reader *, uchar *, unsigned int);
101
static const cpp_token *stringify_arg (cpp_reader *, macro_arg *);
102
static void paste_all_tokens (cpp_reader *, const cpp_token *);
103
static bool paste_tokens (cpp_reader *, const cpp_token **, const cpp_token *);
104
static void alloc_expanded_arg_mem (cpp_reader *, macro_arg *, size_t);
105
static void ensure_expanded_arg_room (cpp_reader *, macro_arg *, size_t, size_t *);
106
static void delete_macro_args (_cpp_buff*, unsigned num_args);
107
static void set_arg_token (macro_arg *, const cpp_token *,
108
                           source_location, size_t,
109
                           enum macro_arg_token_kind,
110
                           bool);
111
static const source_location *get_arg_token_location (const macro_arg *,
112
                                                      enum macro_arg_token_kind);
113
static const cpp_token **arg_token_ptr_at (const macro_arg *,
114
                                           size_t,
115
                                           enum macro_arg_token_kind,
116
                                           source_location **virt_location);
117
 
118
static void macro_arg_token_iter_init (macro_arg_token_iter *, bool,
119
                                       enum macro_arg_token_kind,
120
                                       const macro_arg *,
121
                                       const cpp_token **);
122
static const cpp_token *macro_arg_token_iter_get_token
123
(const macro_arg_token_iter *it);
124
static source_location macro_arg_token_iter_get_location
125
(const macro_arg_token_iter *);
126
static void macro_arg_token_iter_forward (macro_arg_token_iter *);
127
static _cpp_buff *tokens_buff_new (cpp_reader *, size_t,
128
                                   source_location **);
129
static size_t tokens_buff_count (_cpp_buff *);
130
static const cpp_token **tokens_buff_last_token_ptr (_cpp_buff *);
131
static inline const cpp_token **tokens_buff_put_token_to (const cpp_token **,
132
                                                          source_location *,
133
                                                          const cpp_token *,
134
                                                          source_location,
135
                                                          source_location,
136
                                                          const struct line_map *,
137
                                                          unsigned int);
138
 
139
static const cpp_token **tokens_buff_add_token (_cpp_buff *,
140
                                                source_location *,
141
                                                const cpp_token *,
142
                                                source_location,
143
                                                source_location,
144
                                                const struct line_map *,
145
                                                unsigned int);
146
static inline void tokens_buff_remove_last_token (_cpp_buff *);
147
static void replace_args (cpp_reader *, cpp_hashnode *, cpp_macro *,
148
                          macro_arg *, source_location);
149
static _cpp_buff *funlike_invocation_p (cpp_reader *, cpp_hashnode *,
150
                                        _cpp_buff **, unsigned *);
151
static bool create_iso_definition (cpp_reader *, cpp_macro *);
152
 
153
/* #define directive parsing and handling.  */
154
 
155
static cpp_token *alloc_expansion_token (cpp_reader *, cpp_macro *);
156
static cpp_token *lex_expansion_token (cpp_reader *, cpp_macro *);
157
static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *,
158
                                  const cpp_macro *);
159
static bool parse_params (cpp_reader *, cpp_macro *);
160
static void check_trad_stringification (cpp_reader *, const cpp_macro *,
161
                                        const cpp_string *);
162
static bool reached_end_of_context (cpp_context *);
163
static void consume_next_token_from_context (cpp_reader *pfile,
164
                                             const cpp_token **,
165
                                             source_location *);
166
static const cpp_token* cpp_get_token_1 (cpp_reader *, source_location *);
167
 
168
/* Statistical counter tracking the number of macros that got
169
   expanded.  */
170
unsigned num_expanded_macros_counter = 0;
171
/* Statistical counter tracking the total number tokens resulting
172
   from macro expansion.  */
173
unsigned num_macro_tokens_counter = 0;
174
 
175
/* Emits a warning if NODE is a macro defined in the main file that
176
   has not been used.  */
177
int
178
_cpp_warn_if_unused_macro (cpp_reader *pfile, cpp_hashnode *node,
179
                           void *v ATTRIBUTE_UNUSED)
180
{
181
  if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
182
    {
183
      cpp_macro *macro = node->value.macro;
184
 
185
      if (!macro->used
186
          && MAIN_FILE_P (linemap_lookup (pfile->line_table, macro->line)))
187
        cpp_warning_with_line (pfile, CPP_W_UNUSED_MACROS, macro->line, 0,
188
                               "macro \"%s\" is not used", NODE_NAME (node));
189
    }
190
 
191
  return 1;
192
}
193
 
194
/* Allocates and returns a CPP_STRING token, containing TEXT of length
195
   LEN, after null-terminating it.  TEXT must be in permanent storage.  */
196
static const cpp_token *
197
new_string_token (cpp_reader *pfile, unsigned char *text, unsigned int len)
198
{
199
  cpp_token *token = _cpp_temp_token (pfile);
200
 
201
  text[len] = '\0';
202
  token->type = CPP_STRING;
203
  token->val.str.len = len;
204
  token->val.str.text = text;
205
  token->flags = 0;
206
  return token;
207
}
208
 
209
static const char * const monthnames[] =
210
{
211
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
212
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
213
};
214
 
215
/* Helper function for builtin_macro.  Returns the text generated by
216
   a builtin macro. */
217
const uchar *
218
_cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
219
{
220
  const uchar *result = NULL;
221
  linenum_type number = 1;
222
 
223
  switch (node->value.builtin)
224
    {
225
    default:
226
      cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
227
                 NODE_NAME (node));
228
      break;
229
 
230
    case BT_TIMESTAMP:
231
      {
232
        cpp_buffer *pbuffer = cpp_get_buffer (pfile);
233
        if (pbuffer->timestamp == NULL)
234
          {
235
            /* Initialize timestamp value of the assotiated file. */
236
            struct _cpp_file *file = cpp_get_file (pbuffer);
237
            if (file)
238
              {
239
                /* Generate __TIMESTAMP__ string, that represents
240
                   the date and time of the last modification
241
                   of the current source file. The string constant
242
                   looks like "Sun Sep 16 01:03:52 1973".  */
243
                struct tm *tb = NULL;
244
                struct stat *st = _cpp_get_file_stat (file);
245
                if (st)
246
                  tb = localtime (&st->st_mtime);
247
                if (tb)
248
                  {
249
                    char *str = asctime (tb);
250
                    size_t len = strlen (str);
251
                    unsigned char *buf = _cpp_unaligned_alloc (pfile, len + 2);
252
                    buf[0] = '"';
253
                    strcpy ((char *) buf + 1, str);
254
                    buf[len] = '"';
255
                    pbuffer->timestamp = buf;
256
                  }
257
                else
258
                  {
259
                    cpp_errno (pfile, CPP_DL_WARNING,
260
                        "could not determine file timestamp");
261
                    pbuffer->timestamp = UC"\"??? ??? ?? ??:??:?? ????\"";
262
                  }
263
              }
264
          }
265
        result = pbuffer->timestamp;
266
      }
267
      break;
268
    case BT_FILE:
269
    case BT_BASE_FILE:
270
      {
271
        unsigned int len;
272
        const char *name;
273
        uchar *buf;
274
 
275
        if (node->value.builtin == BT_FILE)
276
          name = linemap_get_expansion_filename (pfile->line_table,
277
                                                 pfile->line_table->highest_line);
278
        else
279
          {
280
            name = _cpp_get_file_name (pfile->main_file);
281
            if (!name)
282
              abort ();
283
          }
284
        len = strlen (name);
285
        buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
286
        result = buf;
287
        *buf = '"';
288
        buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
289
        *buf++ = '"';
290
        *buf = '\0';
291
      }
292
      break;
293
 
294
    case BT_INCLUDE_LEVEL:
295
      /* The line map depth counts the primary source as level 1, but
296
         historically __INCLUDE_DEPTH__ has called the primary source
297
         level 0.  */
298
      number = pfile->line_table->depth - 1;
299
      break;
300
 
301
    case BT_SPECLINE:
302
      /* If __LINE__ is embedded in a macro, it must expand to the
303
         line of the macro's invocation, not its definition.
304
         Otherwise things like assert() will not work properly.  */
305
      number = linemap_get_expansion_line (pfile->line_table,
306
                                           CPP_OPTION (pfile, traditional)
307
                                           ? pfile->line_table->highest_line
308
                                           : pfile->cur_token[-1].src_loc);
309
      break;
310
 
311
      /* __STDC__ has the value 1 under normal circumstances.
312
         However, if (a) we are in a system header, (b) the option
313
         stdc_0_in_system_headers is true (set by target config), and
314
         (c) we are not in strictly conforming mode, then it has the
315
         value 0.  (b) and (c) are already checked in cpp_init_builtins.  */
316
    case BT_STDC:
317
      if (cpp_in_system_header (pfile))
318
        number = 0;
319
      else
320
        number = 1;
321
      break;
322
 
323
    case BT_DATE:
324
    case BT_TIME:
325
      if (pfile->date == NULL)
326
        {
327
          /* Allocate __DATE__ and __TIME__ strings from permanent
328
             storage.  We only do this once, and don't generate them
329
             at init time, because time() and localtime() are very
330
             slow on some systems.  */
331
          time_t tt;
332
          struct tm *tb = NULL;
333
 
334
          /* (time_t) -1 is a legitimate value for "number of seconds
335
             since the Epoch", so we have to do a little dance to
336
             distinguish that from a genuine error.  */
337
          errno = 0;
338
          tt = time(NULL);
339
          if (tt != (time_t)-1 || errno == 0)
340
            tb = localtime (&tt);
341
 
342
          if (tb)
343
            {
344
              pfile->date = _cpp_unaligned_alloc (pfile,
345
                                                  sizeof ("\"Oct 11 1347\""));
346
              sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
347
                       monthnames[tb->tm_mon], tb->tm_mday,
348
                       tb->tm_year + 1900);
349
 
350
              pfile->time = _cpp_unaligned_alloc (pfile,
351
                                                  sizeof ("\"12:34:56\""));
352
              sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
353
                       tb->tm_hour, tb->tm_min, tb->tm_sec);
354
            }
355
          else
356
            {
357
              cpp_errno (pfile, CPP_DL_WARNING,
358
                         "could not determine date and time");
359
 
360
              pfile->date = UC"\"??? ?? ????\"";
361
              pfile->time = UC"\"??:??:??\"";
362
            }
363
        }
364
 
365
      if (node->value.builtin == BT_DATE)
366
        result = pfile->date;
367
      else
368
        result = pfile->time;
369
      break;
370
 
371
    case BT_COUNTER:
372
      if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive)
373
        cpp_error (pfile, CPP_DL_ERROR,
374
            "__COUNTER__ expanded inside directive with -fdirectives-only");
375
      number = pfile->counter++;
376
      break;
377
    }
378
 
379
  if (result == NULL)
380
    {
381
      /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers.  */
382
      result = _cpp_unaligned_alloc (pfile, 21);
383
      sprintf ((char *) result, "%u", number);
384
    }
385
 
386
  return result;
387
}
388
 
389
/* Convert builtin macros like __FILE__ to a token and push it on the
390
   context stack.  Also handles _Pragma, for which a new token may not
391
   be created.  Returns 1 if it generates a new token context, 0 to
392
   return the token to the caller.  */
393
static int
394
builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
395
{
396
  const uchar *buf;
397
  size_t len;
398
  char *nbuf;
399
 
400
  if (node->value.builtin == BT_PRAGMA)
401
    {
402
      /* Don't interpret _Pragma within directives.  The standard is
403
         not clear on this, but to me this makes most sense.  */
404
      if (pfile->state.in_directive)
405
        return 0;
406
 
407
      return _cpp_do__Pragma (pfile);
408
    }
409
 
410
  buf = _cpp_builtin_macro_text (pfile, node);
411
  len = ustrlen (buf);
412
  nbuf = (char *) alloca (len + 1);
413
  memcpy (nbuf, buf, len);
414
  nbuf[len]='\n';
415
 
416
  cpp_push_buffer (pfile, (uchar *) nbuf, len, /* from_stage3 */ true);
417
  _cpp_clean_line (pfile);
418
 
419
  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
420
  pfile->cur_token = _cpp_temp_token (pfile);
421
  _cpp_push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
422
  if (pfile->buffer->cur != pfile->buffer->rlimit)
423
    cpp_error (pfile, CPP_DL_ICE, "invalid built-in macro \"%s\"",
424
               NODE_NAME (node));
425
  _cpp_pop_buffer (pfile);
426
 
427
  return 1;
428
}
429
 
430
/* Copies SRC, of length LEN, to DEST, adding backslashes before all
431
   backslashes and double quotes. DEST must be of sufficient size.
432
   Returns a pointer to the end of the string.  */
433
uchar *
434
cpp_quote_string (uchar *dest, const uchar *src, unsigned int len)
435
{
436
  while (len--)
437
    {
438
      uchar c = *src++;
439
 
440
      if (c == '\\' || c == '"')
441
        {
442
          *dest++ = '\\';
443
          *dest++ = c;
444
        }
445
      else
446
          *dest++ = c;
447
    }
448
 
449
  return dest;
450
}
451
 
452
/* Convert a token sequence ARG to a single string token according to
453
   the rules of the ISO C #-operator.  */
454
static const cpp_token *
455
stringify_arg (cpp_reader *pfile, macro_arg *arg)
456
{
457
  unsigned char *dest;
458
  unsigned int i, escape_it, backslash_count = 0;
459
  const cpp_token *source = NULL;
460
  size_t len;
461
 
462
  if (BUFF_ROOM (pfile->u_buff) < 3)
463
    _cpp_extend_buff (pfile, &pfile->u_buff, 3);
464
  dest = BUFF_FRONT (pfile->u_buff);
465
  *dest++ = '"';
466
 
467
  /* Loop, reading in the argument's tokens.  */
468
  for (i = 0; i < arg->count; i++)
469
    {
470
      const cpp_token *token = arg->first[i];
471
 
472
      if (token->type == CPP_PADDING)
473
        {
474
          if (source == NULL
475
              || (!(source->flags & PREV_WHITE)
476
                  && token->val.source == NULL))
477
            source = token->val.source;
478
          continue;
479
        }
480
 
481
      escape_it = (token->type == CPP_STRING || token->type == CPP_CHAR
482
                   || token->type == CPP_WSTRING || token->type == CPP_WCHAR
483
                   || token->type == CPP_STRING32 || token->type == CPP_CHAR32
484
                   || token->type == CPP_STRING16 || token->type == CPP_CHAR16
485
                   || token->type == CPP_UTF8STRING);
486
 
487
      /* Room for each char being written in octal, initial space and
488
         final quote and NUL.  */
489
      len = cpp_token_len (token);
490
      if (escape_it)
491
        len *= 4;
492
      len += 3;
493
 
494
      if ((size_t) (BUFF_LIMIT (pfile->u_buff) - dest) < len)
495
        {
496
          size_t len_so_far = dest - BUFF_FRONT (pfile->u_buff);
497
          _cpp_extend_buff (pfile, &pfile->u_buff, len);
498
          dest = BUFF_FRONT (pfile->u_buff) + len_so_far;
499
        }
500
 
501
      /* Leading white space?  */
502
      if (dest - 1 != BUFF_FRONT (pfile->u_buff))
503
        {
504
          if (source == NULL)
505
            source = token;
506
          if (source->flags & PREV_WHITE)
507
            *dest++ = ' ';
508
        }
509
      source = NULL;
510
 
511
      if (escape_it)
512
        {
513
          _cpp_buff *buff = _cpp_get_buff (pfile, len);
514
          unsigned char *buf = BUFF_FRONT (buff);
515
          len = cpp_spell_token (pfile, token, buf, true) - buf;
516
          dest = cpp_quote_string (dest, buf, len);
517
          _cpp_release_buff (pfile, buff);
518
        }
519
      else
520
        dest = cpp_spell_token (pfile, token, dest, true);
521
 
522
      if (token->type == CPP_OTHER && token->val.str.text[0] == '\\')
523
        backslash_count++;
524
      else
525
        backslash_count = 0;
526
    }
527
 
528
  /* Ignore the final \ of invalid string literals.  */
529
  if (backslash_count & 1)
530
    {
531
      cpp_error (pfile, CPP_DL_WARNING,
532
                 "invalid string literal, ignoring final '\\'");
533
      dest--;
534
    }
535
 
536
  /* Commit the memory, including NUL, and return the token.  */
537
  *dest++ = '"';
538
  len = dest - BUFF_FRONT (pfile->u_buff);
539
  BUFF_FRONT (pfile->u_buff) = dest + 1;
540
  return new_string_token (pfile, dest - len, len);
541
}
542
 
543
/* Try to paste two tokens.  On success, return nonzero.  In any
544
   case, PLHS is updated to point to the pasted token, which is
545
   guaranteed to not have the PASTE_LEFT flag set.  */
546
static bool
547
paste_tokens (cpp_reader *pfile, const cpp_token **plhs, const cpp_token *rhs)
548
{
549
  unsigned char *buf, *end, *lhsend;
550
  cpp_token *lhs;
551
  unsigned int len;
552
 
553
  len = cpp_token_len (*plhs) + cpp_token_len (rhs) + 1;
554
  buf = (unsigned char *) alloca (len);
555
  end = lhsend = cpp_spell_token (pfile, *plhs, buf, false);
556
 
557
  /* Avoid comment headers, since they are still processed in stage 3.
558
     It is simpler to insert a space here, rather than modifying the
559
     lexer to ignore comments in some circumstances.  Simply returning
560
     false doesn't work, since we want to clear the PASTE_LEFT flag.  */
561
  if ((*plhs)->type == CPP_DIV && rhs->type != CPP_EQ)
562
    *end++ = ' ';
563
  /* In one obscure case we might see padding here.  */
564
  if (rhs->type != CPP_PADDING)
565
    end = cpp_spell_token (pfile, rhs, end, false);
566
  *end = '\n';
567
 
568
  cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true);
569
  _cpp_clean_line (pfile);
570
 
571
  /* Set pfile->cur_token as required by _cpp_lex_direct.  */
572
  pfile->cur_token = _cpp_temp_token (pfile);
573
  lhs = _cpp_lex_direct (pfile);
574
  if (pfile->buffer->cur != pfile->buffer->rlimit)
575
    {
576
      source_location saved_loc = lhs->src_loc;
577
 
578
      _cpp_pop_buffer (pfile);
579
      _cpp_backup_tokens (pfile, 1);
580
      *lhsend = '\0';
581
 
582
      /* We have to remove the PASTE_LEFT flag from the old lhs, but
583
         we want to keep the new location.  */
584
      *lhs = **plhs;
585
      *plhs = lhs;
586
      lhs->src_loc = saved_loc;
587
      lhs->flags &= ~PASTE_LEFT;
588
 
589
      /* Mandatory error for all apart from assembler.  */
590
      if (CPP_OPTION (pfile, lang) != CLK_ASM)
591
        cpp_error (pfile, CPP_DL_ERROR,
592
         "pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
593
                   buf, cpp_token_as_text (pfile, rhs));
594
      return false;
595
    }
596
 
597
  *plhs = lhs;
598
  _cpp_pop_buffer (pfile);
599
  return true;
600
}
601
 
602
/* Handles an arbitrarily long sequence of ## operators, with initial
603
   operand LHS.  This implementation is left-associative,
604
   non-recursive, and finishes a paste before handling succeeding
605
   ones.  If a paste fails, we back up to the RHS of the failing ##
606
   operator before pushing the context containing the result of prior
607
   successful pastes, with the effect that the RHS appears in the
608
   output stream after the pasted LHS normally.  */
609
static void
610
paste_all_tokens (cpp_reader *pfile, const cpp_token *lhs)
611
{
612
  const cpp_token *rhs = NULL;
613
  cpp_context *context = pfile->context;
614
 
615
  do
616
    {
617
      /* Take the token directly from the current context.  We can do
618
         this, because we are in the replacement list of either an
619
         object-like macro, or a function-like macro with arguments
620
         inserted.  In either case, the constraints to #define
621
         guarantee we have at least one more token.  */
622
      if (context->tokens_kind == TOKENS_KIND_DIRECT)
623
        rhs = FIRST (context).token++;
624
      else if (context->tokens_kind == TOKENS_KIND_INDIRECT)
625
        rhs = *FIRST (context).ptoken++;
626
      else if (context->tokens_kind == TOKENS_KIND_EXTENDED)
627
        {
628
          /* So we are in presence of an extended token context, which
629
             means that each token in this context has a virtual
630
             location attached to it.  So let's not forget to update
631
             the pointer to the current virtual location of the
632
             current token when we update the pointer to the current
633
             token */
634
 
635
          rhs = *FIRST (context).ptoken++;
636
          /* context->c.mc must be non-null, as if we were not in a
637
             macro context, context->tokens_kind could not be equal to
638
             TOKENS_KIND_EXTENDED.  */
639
          context->c.mc->cur_virt_loc++;
640
        }
641
 
642
      if (rhs->type == CPP_PADDING)
643
        {
644
          if (rhs->flags & PASTE_LEFT)
645
            abort ();
646
        }
647
      if (!paste_tokens (pfile, &lhs, rhs))
648
        break;
649
    }
650
  while (rhs->flags & PASTE_LEFT);
651
 
652
  /* Put the resulting token in its own context.  */
653
  _cpp_push_token_context (pfile, NULL, lhs, 1);
654
}
655
 
656
/* Returns TRUE if the number of arguments ARGC supplied in an
657
   invocation of the MACRO referenced by NODE is valid.  An empty
658
   invocation to a macro with no parameters should pass ARGC as zero.
659
 
660
   Note that MACRO cannot necessarily be deduced from NODE, in case
661
   NODE was redefined whilst collecting arguments.  */
662
bool
663
_cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node, unsigned int argc)
664
{
665
  if (argc == macro->paramc)
666
    return true;
667
 
668
  if (argc < macro->paramc)
669
    {
670
      /* As an extension, a rest argument is allowed to not appear in
671
         the invocation at all.
672
         e.g. #define debug(format, args...) something
673
         debug("string");
674
 
675
         This is exactly the same as if there had been an empty rest
676
         argument - debug("string", ).  */
677
 
678
      if (argc + 1 == macro->paramc && macro->variadic)
679
        {
680
          if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
681
            cpp_error (pfile, CPP_DL_PEDWARN,
682
                       "ISO C99 requires rest arguments to be used");
683
          return true;
684
        }
685
 
686
      cpp_error (pfile, CPP_DL_ERROR,
687
                 "macro \"%s\" requires %u arguments, but only %u given",
688
                 NODE_NAME (node), macro->paramc, argc);
689
    }
690
  else
691
    cpp_error (pfile, CPP_DL_ERROR,
692
               "macro \"%s\" passed %u arguments, but takes just %u",
693
               NODE_NAME (node), argc, macro->paramc);
694
 
695
  return false;
696
}
697
 
698
/* Reads and returns the arguments to a function-like macro
699
   invocation.  Assumes the opening parenthesis has been processed.
700
   If there is an error, emits an appropriate diagnostic and returns
701
   NULL.  Each argument is terminated by a CPP_EOF token, for the
702
   future benefit of expand_arg().  If there are any deferred
703
   #pragma directives among macro arguments, store pointers to the
704
   CPP_PRAGMA ... CPP_PRAGMA_EOL tokens into *PRAGMA_BUFF buffer.
705
 
706
   What is returned is the buffer that contains the memory allocated
707
   to hold the macro arguments.  NODE is the name of the macro this
708
   function is dealing with.  If NUM_ARGS is non-NULL, *NUM_ARGS is
709
   set to the actual number of macro arguments allocated in the
710
   returned buffer.  */
711
static _cpp_buff *
712
collect_args (cpp_reader *pfile, const cpp_hashnode *node,
713
              _cpp_buff **pragma_buff, unsigned *num_args)
714
{
715
  _cpp_buff *buff, *base_buff;
716
  cpp_macro *macro;
717
  macro_arg *args, *arg;
718
  const cpp_token *token;
719
  unsigned int argc;
720
  source_location virt_loc;
721
  bool track_macro_expansion_p = CPP_OPTION (pfile, track_macro_expansion);
722
  unsigned num_args_alloced = 0;
723
 
724
  macro = node->value.macro;
725
  if (macro->paramc)
726
    argc = macro->paramc;
727
  else
728
    argc = 1;
729
 
730
#define DEFAULT_NUM_TOKENS_PER_MACRO_ARG 50
731
#define ARG_TOKENS_EXTENT 1000
732
 
733
  buff = _cpp_get_buff (pfile, argc * (DEFAULT_NUM_TOKENS_PER_MACRO_ARG
734
                                       * sizeof (cpp_token *)
735
                                       + sizeof (macro_arg)));
736
  base_buff = buff;
737
  args = (macro_arg *) buff->base;
738
  memset (args, 0, argc * sizeof (macro_arg));
739
  buff->cur = (unsigned char *) &args[argc];
740
  arg = args, argc = 0;
741
 
742
  /* Collect the tokens making up each argument.  We don't yet know
743
     how many arguments have been supplied, whether too many or too
744
     few.  Hence the slightly bizarre usage of "argc" and "arg".  */
745
  do
746
    {
747
      unsigned int paren_depth = 0;
748
      unsigned int ntokens = 0;
749
      unsigned virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
750
      num_args_alloced++;
751
 
752
      argc++;
753
      arg->first = (const cpp_token **) buff->cur;
754
      if (track_macro_expansion_p)
755
        {
756
          virt_locs_capacity = DEFAULT_NUM_TOKENS_PER_MACRO_ARG;
757
          arg->virt_locs = XNEWVEC (source_location,
758
                                    virt_locs_capacity);
759
        }
760
 
761
      for (;;)
762
        {
763
          /* Require space for 2 new tokens (including a CPP_EOF).  */
764
          if ((unsigned char *) &arg->first[ntokens + 2] > buff->limit)
765
            {
766
              buff = _cpp_append_extend_buff (pfile, buff,
767
                                              ARG_TOKENS_EXTENT
768
                                              * sizeof (cpp_token *));
769
              arg->first = (const cpp_token **) buff->cur;
770
            }
771
          if (track_macro_expansion_p
772
              && (ntokens + 2 > virt_locs_capacity))
773
            {
774
              virt_locs_capacity += ARG_TOKENS_EXTENT;
775
              arg->virt_locs = XRESIZEVEC (source_location,
776
                                           arg->virt_locs,
777
                                           virt_locs_capacity);
778
            }
779
 
780
          token = cpp_get_token_1 (pfile, &virt_loc);
781
 
782
          if (token->type == CPP_PADDING)
783
            {
784
              /* Drop leading padding.  */
785
              if (ntokens == 0)
786
                continue;
787
            }
788
          else if (token->type == CPP_OPEN_PAREN)
789
            paren_depth++;
790
          else if (token->type == CPP_CLOSE_PAREN)
791
            {
792
              if (paren_depth-- == 0)
793
                break;
794
            }
795
          else if (token->type == CPP_COMMA)
796
            {
797
              /* A comma does not terminate an argument within
798
                 parentheses or as part of a variable argument.  */
799
              if (paren_depth == 0
800
                  && ! (macro->variadic && argc == macro->paramc))
801
                break;
802
            }
803
          else if (token->type == CPP_EOF
804
                   || (token->type == CPP_HASH && token->flags & BOL))
805
            break;
806
          else if (token->type == CPP_PRAGMA)
807
            {
808
              cpp_token *newtok = _cpp_temp_token (pfile);
809
 
810
              /* CPP_PRAGMA token lives in directive_result, which will
811
                 be overwritten on the next directive.  */
812
              *newtok = *token;
813
              token = newtok;
814
              do
815
                {
816
                  if (*pragma_buff == NULL
817
                      || BUFF_ROOM (*pragma_buff) < sizeof (cpp_token *))
818
                    {
819
                      _cpp_buff *next;
820
                      if (*pragma_buff == NULL)
821
                        *pragma_buff
822
                          = _cpp_get_buff (pfile, 32 * sizeof (cpp_token *));
823
                      else
824
                        {
825
                          next = *pragma_buff;
826
                          *pragma_buff
827
                            = _cpp_get_buff (pfile,
828
                                             (BUFF_FRONT (*pragma_buff)
829
                                              - (*pragma_buff)->base) * 2);
830
                          (*pragma_buff)->next = next;
831
                        }
832
                    }
833
                  *(const cpp_token **) BUFF_FRONT (*pragma_buff) = token;
834
                  BUFF_FRONT (*pragma_buff) += sizeof (cpp_token *);
835
                  if (token->type == CPP_PRAGMA_EOL)
836
                    break;
837
                  token = cpp_get_token_1 (pfile, &virt_loc);
838
                }
839
              while (token->type != CPP_EOF);
840
 
841
              /* In deferred pragmas parsing_args and prevent_expansion
842
                 had been changed, reset it.  */
843
              pfile->state.parsing_args = 2;
844
              pfile->state.prevent_expansion = 1;
845
 
846
              if (token->type == CPP_EOF)
847
                break;
848
              else
849
                continue;
850
            }
851
          set_arg_token (arg, token, virt_loc,
852
                         ntokens, MACRO_ARG_TOKEN_NORMAL,
853
                         CPP_OPTION (pfile, track_macro_expansion));
854
          ntokens++;
855
        }
856
 
857
      /* Drop trailing padding.  */
858
      while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
859
        ntokens--;
860
 
861
      arg->count = ntokens;
862
      set_arg_token (arg, &pfile->eof, pfile->eof.src_loc,
863
                     ntokens, MACRO_ARG_TOKEN_NORMAL,
864
                     CPP_OPTION (pfile, track_macro_expansion));
865
 
866
      /* Terminate the argument.  Excess arguments loop back and
867
         overwrite the final legitimate argument, before failing.  */
868
      if (argc <= macro->paramc)
869
        {
870
          buff->cur = (unsigned char *) &arg->first[ntokens + 1];
871
          if (argc != macro->paramc)
872
            arg++;
873
        }
874
    }
875
  while (token->type != CPP_CLOSE_PAREN && token->type != CPP_EOF);
876
 
877
  if (token->type == CPP_EOF)
878
    {
879
      /* We still need the CPP_EOF to end directives, and to end
880
         pre-expansion of a macro argument.  Step back is not
881
         unconditional, since we don't want to return a CPP_EOF to our
882
         callers at the end of an -include-d file.  */
883
      if (pfile->context->prev || pfile->state.in_directive)
884
        _cpp_backup_tokens (pfile, 1);
885
      cpp_error (pfile, CPP_DL_ERROR,
886
                 "unterminated argument list invoking macro \"%s\"",
887
                 NODE_NAME (node));
888
    }
889
  else
890
    {
891
      /* A single empty argument is counted as no argument.  */
892
      if (argc == 1 && macro->paramc == 0 && args[0].count == 0)
893
        argc = 0;
894
      if (_cpp_arguments_ok (pfile, macro, node, argc))
895
        {
896
          /* GCC has special semantics for , ## b where b is a varargs
897
             parameter: we remove the comma if b was omitted entirely.
898
             If b was merely an empty argument, the comma is retained.
899
             If the macro takes just one (varargs) parameter, then we
900
             retain the comma only if we are standards conforming.
901
 
902
             If FIRST is NULL replace_args () swallows the comma.  */
903
          if (macro->variadic && (argc < macro->paramc
904
                                  || (argc == 1 && args[0].count == 0
905
                                      && !CPP_OPTION (pfile, std))))
906
            args[macro->paramc - 1].first = NULL;
907
          if (num_args)
908
            *num_args = num_args_alloced;
909
          return base_buff;
910
        }
911
    }
912
 
913
  /* An error occurred.  */
914
  _cpp_release_buff (pfile, base_buff);
915
  return NULL;
916
}
917
 
918
/* Search for an opening parenthesis to the macro of NODE, in such a
919
   way that, if none is found, we don't lose the information in any
920
   intervening padding tokens.  If we find the parenthesis, collect
921
   the arguments and return the buffer containing them.  PRAGMA_BUFF
922
   argument is the same as in collect_args.  If NUM_ARGS is non-NULL,
923
   *NUM_ARGS is set to the number of arguments contained in the
924
   returned buffer.  */
925
static _cpp_buff *
926
funlike_invocation_p (cpp_reader *pfile, cpp_hashnode *node,
927
                      _cpp_buff **pragma_buff, unsigned *num_args)
928
{
929
  const cpp_token *token, *padding = NULL;
930
 
931
  for (;;)
932
    {
933
      token = cpp_get_token (pfile);
934
      if (token->type != CPP_PADDING)
935
        break;
936
      if (padding == NULL
937
          || (!(padding->flags & PREV_WHITE) && token->val.source == NULL))
938
        padding = token;
939
    }
940
 
941
  if (token->type == CPP_OPEN_PAREN)
942
    {
943
      pfile->state.parsing_args = 2;
944
      return collect_args (pfile, node, pragma_buff, num_args);
945
    }
946
 
947
  /* CPP_EOF can be the end of macro arguments, or the end of the
948
     file.  We mustn't back up over the latter.  Ugh.  */
949
  if (token->type != CPP_EOF || token == &pfile->eof)
950
    {
951
      /* Back up.  We may have skipped padding, in which case backing
952
         up more than one token when expanding macros is in general
953
         too difficult.  We re-insert it in its own context.  */
954
      _cpp_backup_tokens (pfile, 1);
955
      if (padding)
956
        _cpp_push_token_context (pfile, NULL, padding, 1);
957
    }
958
 
959
  return NULL;
960
}
961
 
962
/* Return the real number of tokens in the expansion of MACRO.  */
963
static inline unsigned int
964
macro_real_token_count (const cpp_macro *macro)
965
{
966
  unsigned int i;
967
  if (__builtin_expect (!macro->extra_tokens, true))
968
    return macro->count;
969
  for (i = 0; i < macro->count; i++)
970
    if (macro->exp.tokens[i].type == CPP_PASTE)
971
      return i;
972
  abort ();
973
}
974
 
975
/* Push the context of a macro with hash entry NODE onto the context
976
   stack.  If we can successfully expand the macro, we push a context
977
   containing its yet-to-be-rescanned replacement list and return one.
978
   If there were additionally any unexpanded deferred #pragma
979
   directives among macro arguments, push another context containing
980
   the pragma tokens before the yet-to-be-rescanned replacement list
981
   and return two.  Otherwise, we don't push a context and return
982
   zero. LOCATION is the location of the expansion point of the
983
   macro.  */
984
static int
985
enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
986
                     const cpp_token *result, source_location location)
987
{
988
  /* The presence of a macro invalidates a file's controlling macro.  */
989
  pfile->mi_valid = false;
990
 
991
  pfile->state.angled_headers = false;
992
 
993
  if ((node->flags & NODE_BUILTIN) && !(node->flags & NODE_USED))
994
    {
995
      node->flags |= NODE_USED;
996
      if ((!pfile->cb.user_builtin_macro
997
           || !pfile->cb.user_builtin_macro (pfile, node))
998
          && pfile->cb.used_define)
999
        pfile->cb.used_define (pfile, pfile->directive_line, node);
1000
    }
1001
 
1002
  /* Handle standard macros.  */
1003
  if (! (node->flags & NODE_BUILTIN))
1004
    {
1005
      cpp_macro *macro = node->value.macro;
1006
      _cpp_buff *pragma_buff = NULL;
1007
 
1008
      if (macro->fun_like)
1009
        {
1010
          _cpp_buff *buff;
1011
          unsigned num_args = 0;
1012
 
1013
          pfile->state.prevent_expansion++;
1014
          pfile->keep_tokens++;
1015
          pfile->state.parsing_args = 1;
1016
          buff = funlike_invocation_p (pfile, node, &pragma_buff,
1017
                                       &num_args);
1018
          pfile->state.parsing_args = 0;
1019
          pfile->keep_tokens--;
1020
          pfile->state.prevent_expansion--;
1021
 
1022
          if (buff == NULL)
1023
            {
1024
              if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
1025
                cpp_warning (pfile, CPP_W_TRADITIONAL,
1026
 "function-like macro \"%s\" must be used with arguments in traditional C",
1027
                             NODE_NAME (node));
1028
 
1029
              if (pragma_buff)
1030
                _cpp_release_buff (pfile, pragma_buff);
1031
 
1032
              return 0;
1033
            }
1034
 
1035
          if (macro->paramc > 0)
1036
            replace_args (pfile, node, macro,
1037
                          (macro_arg *) buff->base,
1038
                          location);
1039
          /* Free the memory used by the arguments of this
1040
             function-like macro.  This memory has been allocated by
1041
             funlike_invocation_p and by replace_args.  */
1042
          delete_macro_args (buff, num_args);
1043
        }
1044
 
1045
      /* Disable the macro within its expansion.  */
1046
      node->flags |= NODE_DISABLED;
1047
 
1048
      if (!(node->flags & NODE_USED))
1049
        {
1050
          node->flags |= NODE_USED;
1051
          if (pfile->cb.used_define)
1052
            pfile->cb.used_define (pfile, pfile->directive_line, node);
1053
        }
1054
 
1055
      if (pfile->cb.used)
1056
        pfile->cb.used (pfile, location, node);
1057
 
1058
      macro->used = 1;
1059
 
1060
      if (macro->paramc == 0)
1061
        {
1062
          if (CPP_OPTION (pfile, track_macro_expansion))
1063
            {
1064
              unsigned int i, count = macro->count;
1065
              const cpp_token *src = macro->exp.tokens;
1066
              const struct line_map *map;
1067
              source_location *virt_locs = NULL;
1068
              _cpp_buff *macro_tokens =
1069
                tokens_buff_new (pfile, count, &virt_locs);
1070
 
1071
              /* Create a macro map to record the locations of the
1072
                 tokens that are involved in the expansion. LOCATION
1073
                 is the location of the macro expansion point.  */
1074
              map  = linemap_enter_macro (pfile->line_table,
1075
                                          node, location, count);
1076
              for (i = 0; i < count; ++i)
1077
                {
1078
                  tokens_buff_add_token (macro_tokens, virt_locs,
1079
                                         src, src->src_loc,
1080
                                         src->src_loc, map, i);
1081
                  ++src;
1082
                }
1083
              push_extended_tokens_context (pfile, node,
1084
                                            macro_tokens,
1085
                                            virt_locs,
1086
                                            (const cpp_token **)
1087
                                            macro_tokens->base,
1088
                                            count);
1089
              num_macro_tokens_counter += count;
1090
            }
1091
          else
1092
            {
1093
              unsigned tokens_count = macro_real_token_count (macro);
1094
              _cpp_push_token_context (pfile, node, macro->exp.tokens,
1095
                                       tokens_count);
1096
              num_macro_tokens_counter += tokens_count;
1097
            }
1098
        }
1099
 
1100
      if (pragma_buff)
1101
        {
1102
          if (!pfile->state.in_directive)
1103
            _cpp_push_token_context (pfile, NULL,
1104
                                     padding_token (pfile, result), 1);
1105
          do
1106
            {
1107
              unsigned tokens_count;
1108
              _cpp_buff *tail = pragma_buff->next;
1109
              pragma_buff->next = NULL;
1110
              tokens_count = ((const cpp_token **) BUFF_FRONT (pragma_buff)
1111
                              - (const cpp_token **) pragma_buff->base);
1112
              push_ptoken_context (pfile, NULL, pragma_buff,
1113
                                   (const cpp_token **) pragma_buff->base,
1114
                                   tokens_count);
1115
              pragma_buff = tail;
1116
              if (!CPP_OPTION (pfile, track_macro_expansion))
1117
                num_macro_tokens_counter += tokens_count;
1118
 
1119
            }
1120
          while (pragma_buff != NULL);
1121
          return 2;
1122
        }
1123
 
1124
      return 1;
1125
    }
1126
 
1127
  /* Handle built-in macros and the _Pragma operator.  */
1128
  return builtin_macro (pfile, node);
1129
}
1130
 
1131
/* De-allocate the memory used by BUFF which is an array of instances
1132
   of macro_arg.  NUM_ARGS is the number of instances of macro_arg
1133
   present in BUFF.  */
1134
static void
1135
delete_macro_args (_cpp_buff *buff, unsigned num_args)
1136
{
1137
  macro_arg *macro_args;
1138
  unsigned i;
1139
 
1140
  if (buff == NULL)
1141
    return;
1142
 
1143
  macro_args = (macro_arg *) buff->base;
1144
 
1145
  /* Walk instances of macro_arg to free their expanded tokens as well
1146
     as their macro_arg::virt_locs members.  */
1147
  for (i = 0; i < num_args; ++i)
1148
    {
1149
      if (macro_args[i].expanded)
1150
        {
1151
          free (macro_args[i].expanded);
1152
          macro_args[i].expanded = NULL;
1153
        }
1154
      if (macro_args[i].virt_locs)
1155
        {
1156
          free (macro_args[i].virt_locs);
1157
          macro_args[i].virt_locs = NULL;
1158
        }
1159
      if (macro_args[i].expanded_virt_locs)
1160
        {
1161
          free (macro_args[i].expanded_virt_locs);
1162
          macro_args[i].expanded_virt_locs = NULL;
1163
        }
1164
    }
1165
  _cpp_free_buff (buff);
1166
}
1167
 
1168
/* Set the INDEXth token of the macro argument ARG. TOKEN is the token
1169
   to set, LOCATION is its virtual location.  "Virtual" location means
1170
   the location that encodes loci accross macro expansion. Otherwise
1171
   it has to be TOKEN->SRC_LOC.  KIND is the kind of tokens the
1172
   argument ARG is supposed to contain.  Note that ARG must be
1173
   tailored so that it has enough room to contain INDEX + 1 numbers of
1174
   tokens, at least.  */
1175
static void
1176
set_arg_token (macro_arg *arg, const cpp_token *token,
1177
               source_location location, size_t index,
1178
               enum macro_arg_token_kind kind,
1179
               bool track_macro_exp_p)
1180
{
1181
  const cpp_token **token_ptr;
1182
  source_location *loc = NULL;
1183
 
1184
  token_ptr =
1185
    arg_token_ptr_at (arg, index, kind,
1186
                      track_macro_exp_p ? &loc : NULL);
1187
  *token_ptr = token;
1188
 
1189
  if (loc != NULL)
1190
    {
1191
#ifdef ENABLE_CHECKING
1192
      if (kind == MACRO_ARG_TOKEN_STRINGIFIED
1193
          || !track_macro_exp_p)
1194
        /* We can't set the location of a stringified argument
1195
           token and we can't set any location if we aren't tracking
1196
           macro expansion locations.   */
1197
        abort ();
1198
#endif
1199
      *loc = location;
1200
    }
1201
}
1202
 
1203
/* Get the pointer to the location of the argument token of the
1204
   function-like macro argument ARG.  This function must be called
1205
   only when we -ftrack-macro-expansion is on.  */
1206
static const source_location *
1207
get_arg_token_location (const macro_arg *arg,
1208
                        enum macro_arg_token_kind kind)
1209
{
1210
  const source_location *loc = NULL;
1211
  const cpp_token **token_ptr =
1212
    arg_token_ptr_at (arg, 0, kind, (source_location **) &loc);
1213
 
1214
  if (token_ptr == NULL)
1215
    return NULL;
1216
 
1217
  return loc;
1218
}
1219
 
1220
/* Return the pointer to the INDEXth token of the macro argument ARG.
1221
   KIND specifies the kind of token the macro argument ARG contains.
1222
   If VIRT_LOCATION is non NULL, *VIRT_LOCATION is set to the address
1223
   of the virtual location of the returned token if the
1224
   -ftrack-macro-expansion flag is on; otherwise, it's set to the
1225
   spelling location of the returned token.  */
1226
static const cpp_token **
1227
arg_token_ptr_at (const macro_arg *arg, size_t index,
1228
                  enum macro_arg_token_kind kind,
1229
                  source_location **virt_location)
1230
{
1231
  const cpp_token **tokens_ptr = NULL;
1232
 
1233
  switch (kind)
1234
    {
1235
    case MACRO_ARG_TOKEN_NORMAL:
1236
      tokens_ptr = arg->first;
1237
      break;
1238
    case MACRO_ARG_TOKEN_STRINGIFIED:
1239
      tokens_ptr = (const cpp_token **) &arg->stringified;
1240
      break;
1241
    case MACRO_ARG_TOKEN_EXPANDED:
1242
        tokens_ptr = arg->expanded;
1243
      break;
1244
    }
1245
 
1246
  if (tokens_ptr == NULL)
1247
    /* This can happen for e.g, an empty token argument to a
1248
       funtion-like macro.  */
1249
    return tokens_ptr;
1250
 
1251
  if (virt_location)
1252
    {
1253
      if (kind == MACRO_ARG_TOKEN_NORMAL)
1254
        *virt_location = &arg->virt_locs[index];
1255
      else if (kind == MACRO_ARG_TOKEN_EXPANDED)
1256
        *virt_location = &arg->expanded_virt_locs[index];
1257
      else if (kind == MACRO_ARG_TOKEN_STRINGIFIED)
1258
        *virt_location =
1259
          (source_location *) &tokens_ptr[index]->src_loc;
1260
    }
1261
  return &tokens_ptr[index];
1262
}
1263
 
1264
/* Initialize an iterator so that it iterates over the tokens of a
1265
   function-like macro argument.  KIND is the kind of tokens we want
1266
   ITER to iterate over. TOKEN_PTR points the first token ITER will
1267
   iterate over.  */
1268
static void
1269
macro_arg_token_iter_init (macro_arg_token_iter *iter,
1270
                           bool track_macro_exp_p,
1271
                           enum macro_arg_token_kind kind,
1272
                           const macro_arg *arg,
1273
                           const cpp_token **token_ptr)
1274
{
1275
  iter->track_macro_exp_p = track_macro_exp_p;
1276
  iter->kind = kind;
1277
  iter->token_ptr = token_ptr;
1278
  /* Unconditionally initialize this so that the compiler doesn't warn
1279
     about iter->location_ptr being possibly uninitialized later after
1280
     this code has been inlined somewhere.  */
1281
  iter->location_ptr = NULL;
1282
  if (track_macro_exp_p)
1283
    iter->location_ptr = get_arg_token_location (arg, kind);
1284
#ifdef ENABLE_CHECKING
1285
  iter->num_forwards = 0;
1286
  if (track_macro_exp_p
1287
      && token_ptr != NULL
1288
      && iter->location_ptr == NULL)
1289
    abort ();
1290
#endif
1291
}
1292
 
1293
/* Move the iterator one token forward. Note that if IT was
1294
   initialized on an argument that has a stringified token, moving it
1295
   foward doesn't make sense as a stringified token is essentially one
1296
   string.  */
1297
static void
1298
macro_arg_token_iter_forward (macro_arg_token_iter *it)
1299
{
1300
  switch (it->kind)
1301
    {
1302
    case MACRO_ARG_TOKEN_NORMAL:
1303
    case MACRO_ARG_TOKEN_EXPANDED:
1304
      it->token_ptr++;
1305
      if (it->track_macro_exp_p)
1306
        it->location_ptr++;
1307
      break;
1308
    case MACRO_ARG_TOKEN_STRINGIFIED:
1309
#ifdef ENABLE_CHECKING
1310
      if (it->num_forwards > 0)
1311
        abort ();
1312
#endif
1313
      break;
1314
    }
1315
 
1316
#ifdef ENABLE_CHECKING
1317
  it->num_forwards++;
1318
#endif
1319
}
1320
 
1321
/* Return the token pointed to by the iterator.  */
1322
static const cpp_token *
1323
macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
1324
{
1325
#ifdef ENABLE_CHECKING
1326
  if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1327
      && it->num_forwards > 0)
1328
    abort ();
1329
#endif
1330
  if (it->token_ptr == NULL)
1331
    return NULL;
1332
  return *it->token_ptr;
1333
}
1334
 
1335
/* Return the location of the token pointed to by the iterator.*/
1336
static source_location
1337
macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
1338
{
1339
#ifdef ENABLE_CHECKING
1340
  if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
1341
      && it->num_forwards > 0)
1342
    abort ();
1343
#endif
1344
  if (it->track_macro_exp_p)
1345
    return *it->location_ptr;
1346
  else
1347
    return (*it->token_ptr)->src_loc;
1348
}
1349
 
1350
/* Return the index of a token [resulting from macro expansion] inside
1351
   the total list of tokens resulting from a given macro
1352
   expansion. The index can be different depending on whether if we
1353
   want each tokens resulting from function-like macro arguments
1354
   expansion to have a different location or not.
1355
 
1356
   E.g, consider this function-like macro:
1357
 
1358
        #define M(x) x - 3
1359
 
1360
   Then consider us "calling" it (and thus expanding it) like:
1361
 
1362
       M(1+4)
1363
 
1364
   It will be expanded into:
1365
 
1366
       1+4-3
1367
 
1368
   Let's consider the case of the token '4'.
1369
 
1370
   Its index can be 2 (it's the third token of the set of tokens
1371
   resulting from the expansion) or it can be 0 if we consider that
1372
   all tokens resulting from the expansion of the argument "1+2" have
1373
   the same index, which is 0. In this later case, the index of token
1374
   '-' would then be 1 and the index of token '3' would be 2.
1375
 
1376
   The later case is useful to use less memory e.g, for the case of
1377
   the user using the option -ftrack-macro-expansion=1.
1378
 
1379
   ABSOLUTE_TOKEN_INDEX is the index of the macro argument token we
1380
   are interested in.  CUR_REPLACEMENT_TOKEN is the token of the macro
1381
   parameter (inside the macro replacement list) that corresponds to
1382
   the macro argument for which ABSOLUTE_TOKEN_INDEX is a token index
1383
   of.
1384
 
1385
   If we refer to the example above, for the '4' argument token,
1386
   ABSOLUTE_TOKEN_INDEX would be set to 2, and CUR_REPLACEMENT_TOKEN
1387
   would be set to the token 'x', in the replacement list "x - 3" of
1388
   macro M.
1389
 
1390
   This is a subroutine of replace_args.  */
1391
inline static unsigned
1392
expanded_token_index (cpp_reader *pfile, cpp_macro *macro,
1393
                      const cpp_token *cur_replacement_token,
1394
                      unsigned absolute_token_index)
1395
{
1396
  if (CPP_OPTION (pfile, track_macro_expansion) > 1)
1397
    return absolute_token_index;
1398
  return cur_replacement_token - macro->exp.tokens;
1399
}
1400
 
1401
/* Replace the parameters in a function-like macro of NODE with the
1402
   actual ARGS, and place the result in a newly pushed token context.
1403
   Expand each argument before replacing, unless it is operated upon
1404
   by the # or ## operators. EXPANSION_POINT_LOC is the location of
1405
   the expansion point of the macro. E.g, the location of the
1406
   function-like macro invocation.  */
1407
static void
1408
replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
1409
              macro_arg *args, source_location expansion_point_loc)
1410
{
1411
  unsigned int i, total;
1412
  const cpp_token *src, *limit;
1413
  const cpp_token **first = NULL;
1414
  macro_arg *arg;
1415
  _cpp_buff *buff = NULL;
1416
  source_location *virt_locs = NULL;
1417
  unsigned int exp_count;
1418
  const struct line_map *map = NULL;
1419
  int track_macro_exp;
1420
 
1421
  /* First, fully macro-expand arguments, calculating the number of
1422
     tokens in the final expansion as we go.  The ordering of the if
1423
     statements below is subtle; we must handle stringification before
1424
     pasting.  */
1425
 
1426
  /* EXP_COUNT is the number of tokens in the macro replacement
1427
     list.  TOTAL is the number of tokens /after/ macro parameters
1428
     have been replaced by their arguments.   */
1429
  exp_count = macro_real_token_count (macro);
1430
  total = exp_count;
1431
  limit = macro->exp.tokens + exp_count;
1432
 
1433
  for (src = macro->exp.tokens; src < limit; src++)
1434
    if (src->type == CPP_MACRO_ARG)
1435
      {
1436
        /* Leading and trailing padding tokens.  */
1437
        total += 2;
1438
        /* Account for leading and padding tokens in exp_count too.
1439
           This is going to be important later down this function,
1440
           when we want to handle the case of (track_macro_exp <
1441
           2).  */
1442
        exp_count += 2;
1443
 
1444
        /* We have an argument.  If it is not being stringified or
1445
           pasted it is macro-replaced before insertion.  */
1446
        arg = &args[src->val.macro_arg.arg_no - 1];
1447
 
1448
        if (src->flags & STRINGIFY_ARG)
1449
          {
1450
            if (!arg->stringified)
1451
              arg->stringified = stringify_arg (pfile, arg);
1452
          }
1453
        else if ((src->flags & PASTE_LEFT)
1454
                 || (src > macro->exp.tokens && (src[-1].flags & PASTE_LEFT)))
1455
          total += arg->count - 1;
1456
        else
1457
          {
1458
            if (!arg->expanded)
1459
              expand_arg (pfile, arg);
1460
            total += arg->expanded_count - 1;
1461
          }
1462
      }
1463
 
1464
  /* When the compiler is called with the -ftrack-macro-expansion
1465
     flag, we need to keep track of the location of each token that
1466
     results from macro expansion.
1467
 
1468
     A token resulting from macro expansion is not a new token. It is
1469
     simply the same token as the token coming from the macro
1470
     definition.  The new things that are allocated are the buffer
1471
     that holds the tokens resulting from macro expansion and a new
1472
     location that records many things like the locus of the expansion
1473
     point as well as the original locus inside the definition of the
1474
     macro.  This location is called a virtual location.
1475
 
1476
     So the buffer BUFF holds a set of cpp_token*, and the buffer
1477
     VIRT_LOCS holds the virtual locations of the tokens held by BUFF.
1478
 
1479
     Both of these two buffers are going to be hung off of the macro
1480
     context, when the latter is pushed.  The memory allocated to
1481
     store the tokens and their locations is going to be freed once
1482
     the context of macro expansion is popped.
1483
 
1484
     As far as tokens are concerned, the memory overhead of
1485
     -ftrack-macro-expansion is proportional to the number of
1486
     macros that get expanded multiplied by sizeof (source_location).
1487
     The good news is that extra memory gets freed when the macro
1488
     context is freed, i.e shortly after the macro got expanded.  */
1489
 
1490
  /* Is the -ftrack-macro-expansion flag in effect?  */
1491
  track_macro_exp = CPP_OPTION (pfile, track_macro_expansion);
1492
 
1493
  /* Now allocate memory space for tokens and locations resulting from
1494
     the macro expansion, copy the tokens and replace the arguments.
1495
     This memory must be freed when the context of the macro MACRO is
1496
     popped.  */
1497
  buff = tokens_buff_new (pfile, total, track_macro_exp ? &virt_locs : NULL);
1498
 
1499
  first = (const cpp_token **) buff->base;
1500
 
1501
  /* Create a macro map to record the locations of the tokens that are
1502
     involved in the expansion.  Note that the expansion point is set
1503
     to the location of the closing parenthesis.  Otherwise, the
1504
     subsequent map created for the first token that comes after the
1505
     macro map might have a wrong line number.  That would lead to
1506
     tokens with wrong line numbers after the macro expansion.  This
1507
     adds up to the memory overhead of the -ftrack-macro-expansion
1508
     flag; for every macro that is expanded, a "macro map" is
1509
     created.  */
1510
  if (track_macro_exp)
1511
    {
1512
      int num_macro_tokens = total;
1513
      if (track_macro_exp < 2)
1514
        /* Then the number of macro tokens won't take in account the
1515
           fact that function-like macro arguments can expand to
1516
           multiple tokens. This is to save memory at the expense of
1517
           accuracy.
1518
 
1519
           Suppose we have #define SQARE(A) A * A
1520
 
1521
           And then we do SQARE(2+3)
1522
 
1523
           Then the tokens 2, +, 3, will have the same location,
1524
           saying they come from the expansion of the argument A.  */
1525
        num_macro_tokens = exp_count;
1526
      map = linemap_enter_macro (pfile->line_table, node,
1527
                                 expansion_point_loc,
1528
                                 num_macro_tokens);
1529
    }
1530
  i = 0;
1531
  for (src = macro->exp.tokens; src < limit; src++)
1532
    {
1533
      unsigned int arg_tokens_count;
1534
      macro_arg_token_iter from;
1535
      const cpp_token **paste_flag = NULL;
1536
      const cpp_token **tmp_token_ptr;
1537
 
1538
      if (src->type != CPP_MACRO_ARG)
1539
        {
1540
          /* Allocate a virtual location for token SRC, and add that
1541
             token and its virtual location into the buffers BUFF and
1542
             VIRT_LOCS.  */
1543
          unsigned index = expanded_token_index (pfile, macro, src, i);
1544
          tokens_buff_add_token (buff, virt_locs, src,
1545
                                 src->src_loc, src->src_loc,
1546
                                 map, index);
1547
          i += 1;
1548
          continue;
1549
        }
1550
 
1551
      paste_flag = 0;
1552
      arg = &args[src->val.macro_arg.arg_no - 1];
1553
      /* SRC is a macro parameter that we need to replace with its
1554
         corresponding argument.  So at some point we'll need to
1555
         iterate over the tokens of the macro argument and copy them
1556
         into the "place" now holding the correspondig macro
1557
         parameter.  We are going to use the iterator type
1558
         macro_argo_token_iter to handle that iterating.  The 'if'
1559
         below is to initialize the iterator depending on the type of
1560
         tokens the macro argument has.  It also does some adjustment
1561
         related to padding tokens and some pasting corner cases.  */
1562
      if (src->flags & STRINGIFY_ARG)
1563
        {
1564
          arg_tokens_count = 1;
1565
          macro_arg_token_iter_init (&from,
1566
                                     CPP_OPTION (pfile,
1567
                                                 track_macro_expansion),
1568
                                     MACRO_ARG_TOKEN_STRINGIFIED,
1569
                                     arg, &arg->stringified);
1570
        }
1571
      else if (src->flags & PASTE_LEFT)
1572
        {
1573
          arg_tokens_count = arg->count;
1574
          macro_arg_token_iter_init (&from,
1575
                                     CPP_OPTION (pfile,
1576
                                                 track_macro_expansion),
1577
                                     MACRO_ARG_TOKEN_NORMAL,
1578
                                     arg, arg->first);
1579
        }
1580
      else if (src != macro->exp.tokens && (src[-1].flags & PASTE_LEFT))
1581
        {
1582
          int num_toks;
1583
          arg_tokens_count = arg->count;
1584
          macro_arg_token_iter_init (&from,
1585
                                     CPP_OPTION (pfile,
1586
                                                 track_macro_expansion),
1587
                                     MACRO_ARG_TOKEN_NORMAL,
1588
                                     arg, arg->first);
1589
 
1590
          num_toks = tokens_buff_count (buff);
1591
 
1592
          if (num_toks != 0)
1593
            {
1594
              /* So the current parameter token is pasted to the previous
1595
                 token in the replacement list.  Let's look at what
1596
                 we have as previous and current arguments.  */
1597
 
1598
              /* This is the previous argument's token ...  */
1599
              tmp_token_ptr = tokens_buff_last_token_ptr (buff);
1600
 
1601
              if ((*tmp_token_ptr)->type == CPP_COMMA
1602
                  && macro->variadic
1603
                  && src->val.macro_arg.arg_no == macro->paramc)
1604
                {
1605
                  /* ... which is a comma; and the current parameter
1606
                     is the last parameter of a variadic function-like
1607
                     macro.  If the argument to the current last
1608
                     parameter is NULL, then swallow the comma,
1609
                     otherwise drop the paste flag.  */
1610
                  if (macro_arg_token_iter_get_token (&from) == NULL)
1611
                    tokens_buff_remove_last_token (buff);
1612
                  else
1613
                    paste_flag = tmp_token_ptr;
1614
                }
1615
              /* Remove the paste flag if the RHS is a placemarker.  */
1616
              else if (arg_tokens_count == 0)
1617
                paste_flag = tmp_token_ptr;
1618
            }
1619
        }
1620
      else
1621
        {
1622
          arg_tokens_count = arg->expanded_count;
1623
          macro_arg_token_iter_init (&from,
1624
                                     CPP_OPTION (pfile,
1625
                                                 track_macro_expansion),
1626
                                     MACRO_ARG_TOKEN_EXPANDED,
1627
                                     arg, arg->expanded);
1628
        }
1629
 
1630
      /* Padding on the left of an argument (unless RHS of ##).  */
1631
      if ((!pfile->state.in_directive || pfile->state.directive_wants_padding)
1632
          && src != macro->exp.tokens && !(src[-1].flags & PASTE_LEFT))
1633
        {
1634
          const cpp_token *t = padding_token (pfile, src);
1635
          unsigned index = expanded_token_index (pfile, macro, src, i);
1636
          /* Allocate a virtual location for the padding token and
1637
             append the token and its location to BUFF and
1638
             VIRT_LOCS.   */
1639
          tokens_buff_add_token (buff, virt_locs, t,
1640
                                 t->src_loc, t->src_loc,
1641
                                 map, index);
1642
        }
1643
 
1644
      if (arg_tokens_count)
1645
        {
1646
          /* So now we've got the number of tokens that make up the
1647
             argument that is going to replace the current parameter
1648
             in the macro's replacement list.  */
1649
          unsigned int j;
1650
          for (j = 0; j < arg_tokens_count; ++j)
1651
            {
1652
              /* So if track_macro_exp is < 2, the user wants to
1653
                 save extra memory while tracking macro expansion
1654
                 locations.  So in that case here is what we do:
1655
 
1656
                 Suppose we have #define SQARE(A) A * A
1657
 
1658
                 And then we do SQARE(2+3)
1659
 
1660
                 Then the tokens 2, +, 3, will have the same location,
1661
                 saying they come from the expansion of the argument
1662
                 A.
1663
 
1664
              So that means we are going to ignore the COUNT tokens
1665
              resulting from the expansion of the current macro
1666
              arugment. In other words all the ARG_TOKENS_COUNT tokens
1667
              resulting from the expansion of the macro argument will
1668
              have the index I. Normally, each of those token should
1669
              have index I+J.  */
1670
              unsigned token_index = i;
1671
              unsigned index;
1672
              if (track_macro_exp > 1)
1673
                token_index += j;
1674
 
1675
              index = expanded_token_index (pfile, macro, src, token_index);
1676
              tokens_buff_add_token (buff, virt_locs,
1677
                                     macro_arg_token_iter_get_token (&from),
1678
                                     macro_arg_token_iter_get_location (&from),
1679
                                     src->src_loc, map, index);
1680
              macro_arg_token_iter_forward (&from);
1681
            }
1682
 
1683
          /* With a non-empty argument on the LHS of ##, the last
1684
             token should be flagged PASTE_LEFT.  */
1685
          if (src->flags & PASTE_LEFT)
1686
            paste_flag =
1687
              (const cpp_token **) tokens_buff_last_token_ptr (buff);
1688
        }
1689
      else if (CPP_PEDANTIC (pfile) && ! macro->syshdr
1690
               && ! CPP_OPTION (pfile, c99)
1691
               && ! cpp_in_system_header (pfile))
1692
        {
1693
          cpp_error (pfile, CPP_DL_PEDWARN,
1694
                     "invoking macro %s argument %d: "
1695
                     "empty macro arguments are undefined"
1696
                     " in ISO C90 and ISO C++98",
1697
                     NODE_NAME (node),
1698
                     src->val.macro_arg.arg_no);
1699
        }
1700
 
1701
      /* Avoid paste on RHS (even case count == 0).  */
1702
      if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
1703
        {
1704
          const cpp_token *t = &pfile->avoid_paste;
1705
          tokens_buff_add_token (buff, virt_locs,
1706
                                 t, t->src_loc, t->src_loc,
1707
                                 NULL, 0);
1708
        }
1709
 
1710
      /* Add a new paste flag, or remove an unwanted one.  */
1711
      if (paste_flag)
1712
        {
1713
          cpp_token *token = _cpp_temp_token (pfile);
1714
          token->type = (*paste_flag)->type;
1715
          token->val = (*paste_flag)->val;
1716
          if (src->flags & PASTE_LEFT)
1717
            token->flags = (*paste_flag)->flags | PASTE_LEFT;
1718
          else
1719
            token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
1720
          *paste_flag = token;
1721
        }
1722
 
1723
      i += arg_tokens_count;
1724
    }
1725
 
1726
  if (track_macro_exp)
1727
    push_extended_tokens_context (pfile, node, buff, virt_locs, first,
1728
                                  tokens_buff_count (buff));
1729
  else
1730
    push_ptoken_context (pfile, node, buff, first,
1731
                         tokens_buff_count (buff));
1732
 
1733
  num_macro_tokens_counter += tokens_buff_count (buff);
1734
}
1735
 
1736
/* Return a special padding token, with padding inherited from SOURCE.  */
1737
static const cpp_token *
1738
padding_token (cpp_reader *pfile, const cpp_token *source)
1739
{
1740
  cpp_token *result = _cpp_temp_token (pfile);
1741
 
1742
  result->type = CPP_PADDING;
1743
 
1744
  /* Data in GCed data structures cannot be made const so far, so we
1745
     need a cast here.  */
1746
  result->val.source = (cpp_token *) source;
1747
  result->flags = 0;
1748
  return result;
1749
}
1750
 
1751
/* Get a new uninitialized context.  Create a new one if we cannot
1752
   re-use an old one.  */
1753
static cpp_context *
1754
next_context (cpp_reader *pfile)
1755
{
1756
  cpp_context *result = pfile->context->next;
1757
 
1758
  if (result == 0)
1759
    {
1760
      result = XNEW (cpp_context);
1761
      memset (result, 0, sizeof (cpp_context));
1762
      result->prev = pfile->context;
1763
      result->next = 0;
1764
      pfile->context->next = result;
1765
    }
1766
 
1767
  pfile->context = result;
1768
  return result;
1769
}
1770
 
1771
/* Push a list of pointers to tokens.  */
1772
static void
1773
push_ptoken_context (cpp_reader *pfile, cpp_hashnode *macro, _cpp_buff *buff,
1774
                     const cpp_token **first, unsigned int count)
1775
{
1776
  cpp_context *context = next_context (pfile);
1777
 
1778
  context->tokens_kind = TOKENS_KIND_INDIRECT;
1779
  context->c.macro = macro;
1780
  context->buff = buff;
1781
  FIRST (context).ptoken = first;
1782
  LAST (context).ptoken = first + count;
1783
}
1784
 
1785
/* Push a list of tokens.  */
1786
void
1787
_cpp_push_token_context (cpp_reader *pfile, cpp_hashnode *macro,
1788
                         const cpp_token *first, unsigned int count)
1789
{
1790
   cpp_context *context = next_context (pfile);
1791
 
1792
   context->tokens_kind = TOKENS_KIND_DIRECT;
1793
   context->c.macro = macro;
1794
   context->buff = NULL;
1795
  FIRST (context).token = first;
1796
  LAST (context).token = first + count;
1797
}
1798
 
1799
/* Build a context containing a list of tokens as well as their
1800
   virtual locations and push it.  TOKENS_BUFF is the buffer that
1801
   contains the tokens pointed to by FIRST.  If TOKENS_BUFF is
1802
   non-NULL, it means that the context owns it, meaning that
1803
   _cpp_pop_context will free it as well as VIRT_LOCS_BUFF that
1804
   contains the virtual locations.  */
1805
static void
1806
push_extended_tokens_context (cpp_reader *pfile,
1807
                              cpp_hashnode *macro,
1808
                              _cpp_buff *token_buff,
1809
                              source_location *virt_locs,
1810
                              const cpp_token **first,
1811
                              unsigned int count)
1812
{
1813
  cpp_context *context = next_context (pfile);
1814
  macro_context *m;
1815
 
1816
  context->tokens_kind = TOKENS_KIND_EXTENDED;
1817
  context->buff = token_buff;
1818
 
1819
  m = XNEW (macro_context);
1820
  m->macro_node = macro;
1821
  m->virt_locs = virt_locs;
1822
  m->cur_virt_loc = virt_locs;
1823
  context->c.mc = m;
1824
  FIRST (context).ptoken = first;
1825
  LAST (context).ptoken = first + count;
1826
}
1827
 
1828
/* Push a traditional macro's replacement text.  */
1829
void
1830
_cpp_push_text_context (cpp_reader *pfile, cpp_hashnode *macro,
1831
                        const uchar *start, size_t len)
1832
{
1833
  cpp_context *context = next_context (pfile);
1834
 
1835
  context->tokens_kind = TOKENS_KIND_DIRECT;
1836
  context->c.macro = macro;
1837
  context->buff = NULL;
1838
  CUR (context) = start;
1839
  RLIMIT (context) = start + len;
1840
  macro->flags |= NODE_DISABLED;
1841
}
1842
 
1843
/* Creates a buffer that holds tokens a.k.a "token buffer", usually
1844
   for the purpose of storing them on a cpp_context. If VIRT_LOCS is
1845
   non-null (which means that -ftrack-macro-expansion is on),
1846
   *VIRT_LOCS is set to a newly allocated buffer that is supposed to
1847
   hold the virtual locations of the tokens resulting from macro
1848
   expansion.  */
1849
static _cpp_buff*
1850
tokens_buff_new (cpp_reader *pfile, size_t len,
1851
                 source_location **virt_locs)
1852
{
1853
  size_t tokens_size = len * sizeof (cpp_token *);
1854
  size_t locs_size = len * sizeof (source_location);
1855
 
1856
  if (virt_locs != NULL)
1857
    *virt_locs = XNEWVEC (source_location, locs_size);
1858
  return _cpp_get_buff (pfile, tokens_size);
1859
}
1860
 
1861
/* Returns the number of tokens contained in a token buffer.  The
1862
   buffer holds a set of cpp_token*.  */
1863
static size_t
1864
tokens_buff_count (_cpp_buff *buff)
1865
{
1866
  return (BUFF_FRONT (buff) - buff->base) / sizeof (cpp_token *);
1867
}
1868
 
1869
/* Return a pointer to the last token contained in the token buffer
1870
   BUFF.  */
1871
static const cpp_token **
1872
tokens_buff_last_token_ptr (_cpp_buff *buff)
1873
{
1874
  return &((const cpp_token **) BUFF_FRONT (buff))[-1];
1875
}
1876
 
1877
/* Remove the last token contained in the token buffer TOKENS_BUFF.
1878
   If VIRT_LOCS_BUFF is non-NULL,  it should point at the buffer
1879
   containing the virtual locations of the tokens in TOKENS_BUFF; in
1880
   which case the function updates that buffer as well.   */
1881
static inline void
1882
tokens_buff_remove_last_token (_cpp_buff *tokens_buff)
1883
 
1884
{
1885
  if (BUFF_FRONT (tokens_buff) > tokens_buff->base)
1886
    BUFF_FRONT (tokens_buff) =
1887
      (unsigned char *) &((cpp_token **) BUFF_FRONT (tokens_buff))[-1];
1888
}
1889
 
1890
/* Insert a token into the token buffer at the position pointed to by
1891
   DEST.  Note that the buffer is not enlarged so the previous token
1892
   that was at *DEST is overwritten.  VIRT_LOC_DEST, if non-null,
1893
   means -ftrack-macro-expansion is effect; it then points to where to
1894
   insert the virtual location of TOKEN.  TOKEN is the token to
1895
   insert.  VIRT_LOC is the virtual location of the token, i.e, the
1896
   location possibly encoding its locus accross macro expansion.  If
1897
   TOKEN is an argument of a function-like macro (inside a macro
1898
   replacement list), PARM_DEF_LOC is the spelling location of the
1899
   macro parameter that TOKEN is replacing, in the replacement list of
1900
   the macro.  If TOKEN is not an argument of a function-like macro or
1901
   if it doesn't come from a macro expansion, then VIRT_LOC can just
1902
   be set to the same value as PARM_DEF_LOC.  If MAP is non null, it
1903
   means TOKEN comes from a macro expansion and MAP is the macro map
1904
   associated to the macro.  MACRO_TOKEN_INDEX points to the index of
1905
   the token in the macro map; it is not considered if MAP is NULL.
1906
 
1907
   Upon successful completion this function returns the a pointer to
1908
   the position of the token coming right after the insertion
1909
   point.  */
1910
static inline const cpp_token **
1911
tokens_buff_put_token_to (const cpp_token **dest,
1912
                          source_location *virt_loc_dest,
1913
                          const cpp_token *token,
1914
                          source_location virt_loc,
1915
                          source_location parm_def_loc,
1916
                          const struct line_map *map,
1917
                          unsigned int macro_token_index)
1918
{
1919
  source_location macro_loc = virt_loc;
1920
  const cpp_token **result;
1921
 
1922
  if (virt_loc_dest)
1923
    {
1924
      /* -ftrack-macro-expansion is on.  */
1925
      if (map)
1926
        macro_loc = linemap_add_macro_token (map, macro_token_index,
1927
                                             virt_loc, parm_def_loc);
1928
      *virt_loc_dest = macro_loc;
1929
    }
1930
  *dest = token;
1931
  result = &dest[1];
1932
 
1933
  return result;
1934
}
1935
 
1936
/* Adds a token at the end of the tokens contained in BUFFER.  Note
1937
   that this function doesn't enlarge BUFFER when the number of tokens
1938
   reaches BUFFER's size; it aborts in that situation.
1939
 
1940
   TOKEN is the token to append. VIRT_LOC is the virtual location of
1941
   the token, i.e, the location possibly encoding its locus accross
1942
   macro expansion. If TOKEN is an argument of a function-like macro
1943
   (inside a macro replacement list), PARM_DEF_LOC is the location of
1944
   the macro parameter that TOKEN is replacing.  If TOKEN doesn't come
1945
   from a macro expansion, then VIRT_LOC can just be set to the same
1946
   value as PARM_DEF_LOC.  If MAP is non null, it means TOKEN comes
1947
   from a macro expansion and MAP is the macro map associated to the
1948
   macro.  MACRO_TOKEN_INDEX points to the index of the token in the
1949
   macro map; It is not considered if MAP is NULL.  If VIRT_LOCS is
1950
   non-null, it means -ftrack-macro-expansion is on; in which case
1951
   this function adds the virtual location DEF_LOC to the VIRT_LOCS
1952
   array, at the same index as the one of TOKEN in BUFFER.  Upon
1953
   successful completion this function returns the a pointer to the
1954
   position of the token coming right after the insertion point.  */
1955
static const cpp_token **
1956
tokens_buff_add_token (_cpp_buff *buffer,
1957
                       source_location *virt_locs,
1958
                       const cpp_token *token,
1959
                       source_location virt_loc,
1960
                       source_location parm_def_loc,
1961
                       const struct line_map *map,
1962
                       unsigned int macro_token_index)
1963
{
1964
  const cpp_token **result;
1965
  source_location *virt_loc_dest = NULL;
1966
  unsigned token_index =
1967
    (BUFF_FRONT (buffer) - buffer->base) / sizeof (cpp_token *);
1968
 
1969
  /* Abort if we pass the end the buffer.  */
1970
  if (BUFF_FRONT (buffer) > BUFF_LIMIT (buffer))
1971
    abort ();
1972
 
1973
  if (virt_locs != NULL)
1974
    virt_loc_dest = &virt_locs[token_index];
1975
 
1976
  result =
1977
    tokens_buff_put_token_to ((const cpp_token **) BUFF_FRONT (buffer),
1978
                              virt_loc_dest, token, virt_loc, parm_def_loc,
1979
                              map, macro_token_index);
1980
 
1981
  BUFF_FRONT (buffer) = (unsigned char *) result;
1982
  return result;
1983
}
1984
 
1985
/* Allocate space for the function-like macro argument ARG to store
1986
   the tokens resulting from the macro-expansion of the tokens that
1987
   make up ARG itself. That space is allocated in ARG->expanded and
1988
   needs to be freed using free.  */
1989
static void
1990
alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
1991
{
1992
#ifdef ENABLE_CHECKING
1993
  if (arg->expanded != NULL
1994
      || arg->expanded_virt_locs != NULL)
1995
    abort ();
1996
#endif
1997
  arg->expanded = XNEWVEC (const cpp_token *, capacity);
1998
  if (CPP_OPTION (pfile, track_macro_expansion))
1999
    arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
2000
 
2001
}
2002
 
2003
/* If necessary, enlarge ARG->expanded to so that it can contain SIZE
2004
   tokens.  */
2005
static void
2006
ensure_expanded_arg_room (cpp_reader *pfile, macro_arg *arg,
2007
                          size_t size, size_t *expanded_capacity)
2008
{
2009
  if (size <= *expanded_capacity)
2010
    return;
2011
 
2012
  size *= 2;
2013
 
2014
  arg->expanded =
2015
    XRESIZEVEC (const cpp_token *, arg->expanded, size);
2016
  *expanded_capacity = size;
2017
 
2018
  if (CPP_OPTION (pfile, track_macro_expansion))
2019
    {
2020
      if (arg->expanded_virt_locs == NULL)
2021
        arg->expanded_virt_locs = XNEWVEC (source_location, size);
2022
      else
2023
        arg->expanded_virt_locs = XRESIZEVEC (source_location,
2024
                                              arg->expanded_virt_locs,
2025
                                              size);
2026
    }
2027
}
2028
 
2029
/* Expand an argument ARG before replacing parameters in a
2030
   function-like macro.  This works by pushing a context with the
2031
   argument's tokens, and then expanding that into a temporary buffer
2032
   as if it were a normal part of the token stream.  collect_args()
2033
   has terminated the argument's tokens with a CPP_EOF so that we know
2034
   when we have fully expanded the argument.  */
2035
static void
2036
expand_arg (cpp_reader *pfile, macro_arg *arg)
2037
{
2038
  size_t capacity;
2039
  bool saved_warn_trad;
2040
  bool track_macro_exp_p = CPP_OPTION (pfile, track_macro_expansion);
2041
 
2042
  if (arg->count == 0
2043
      || arg->expanded != NULL)
2044
    return;
2045
 
2046
  /* Don't warn about funlike macros when pre-expanding.  */
2047
  saved_warn_trad = CPP_WTRADITIONAL (pfile);
2048
  CPP_WTRADITIONAL (pfile) = 0;
2049
 
2050
  /* Loop, reading in the tokens of the argument.  */
2051
  capacity = 256;
2052
  alloc_expanded_arg_mem (pfile, arg, capacity);
2053
 
2054
  if (track_macro_exp_p)
2055
    push_extended_tokens_context (pfile, NULL, NULL,
2056
                                  arg->virt_locs,
2057
                                  arg->first,
2058
                                  arg->count + 1);
2059
  else
2060
    push_ptoken_context (pfile, NULL, NULL,
2061
                         arg->first, arg->count + 1);
2062
 
2063
  for (;;)
2064
    {
2065
      const cpp_token *token;
2066
      source_location location;
2067
 
2068
      ensure_expanded_arg_room (pfile, arg, arg->expanded_count + 1,
2069
                                &capacity);
2070
 
2071
      token = cpp_get_token_1 (pfile, &location);
2072
 
2073
      if (token->type == CPP_EOF)
2074
        break;
2075
 
2076
      set_arg_token (arg, token, location,
2077
                     arg->expanded_count, MACRO_ARG_TOKEN_EXPANDED,
2078
                     CPP_OPTION (pfile, track_macro_expansion));
2079
      arg->expanded_count++;
2080
    }
2081
 
2082
  _cpp_pop_context (pfile);
2083
 
2084
  CPP_WTRADITIONAL (pfile) = saved_warn_trad;
2085
}
2086
 
2087
/* Pop the current context off the stack, re-enabling the macro if the
2088
   context represented a macro's replacement list.  Initially the
2089
   context structure was not freed so that we can re-use it later, but
2090
   now we do free it to reduce peak memory consumption.  */
2091
void
2092
_cpp_pop_context (cpp_reader *pfile)
2093
{
2094
  cpp_context *context = pfile->context;
2095
 
2096
  if (context->c.macro)
2097
    {
2098
      cpp_hashnode *macro;
2099
      if (context->tokens_kind == TOKENS_KIND_EXTENDED)
2100
        {
2101
          macro_context *mc = context->c.mc;
2102
          macro = mc->macro_node;
2103
          /* If context->buff is set, it means the life time of tokens
2104
             is bound to the life time of this context; so we must
2105
             free the tokens; that means we must free the virtual
2106
             locations of these tokens too.  */
2107
          if (context->buff && mc->virt_locs)
2108
            {
2109
              free (mc->virt_locs);
2110
              mc->virt_locs = NULL;
2111
            }
2112
          free (mc);
2113
          context->c.mc = NULL;
2114
        }
2115
      else
2116
        macro = context->c.macro;
2117
 
2118
      /* Beware that MACRO can be NULL in cases like when we are
2119
         called from expand_arg.  In those cases, a dummy context with
2120
         tokens is pushed just for the purpose of walking them using
2121
         cpp_get_token_1.  In that case, no 'macro' field is set into
2122
         the dummy context.  */
2123
      if (macro != NULL)
2124
        macro->flags &= ~NODE_DISABLED;
2125
    }
2126
 
2127
  if (context->buff)
2128
    {
2129
      /* Decrease memory peak consumption by freeing the memory used
2130
         by the context.  */
2131
      _cpp_free_buff (context->buff);
2132
    }
2133
 
2134
  pfile->context = context->prev;
2135
  /* decrease peak memory consumption by feeing the context.  */
2136
  pfile->context->next = NULL;
2137
  free (context);
2138
}
2139
 
2140
/* Return TRUE if we reached the end of the set of tokens stored in
2141
   CONTEXT, FALSE otherwise.  */
2142
static inline bool
2143
reached_end_of_context (cpp_context *context)
2144
{
2145
  if (context->tokens_kind == TOKENS_KIND_DIRECT)
2146
      return FIRST (context).token == LAST (context).token;
2147
  else if (context->tokens_kind == TOKENS_KIND_INDIRECT
2148
           || context->tokens_kind == TOKENS_KIND_EXTENDED)
2149
    return FIRST (context).ptoken == LAST (context).ptoken;
2150
  else
2151
    abort ();
2152
}
2153
 
2154
/* Consume the next token contained in the current context of PFILE,
2155
   and return it in *TOKEN. It's "full location" is returned in
2156
   *LOCATION. If -ftrack-macro-location is in effeect, fFull location"
2157
   means the location encoding the locus of the token accross macro
2158
   expansion; otherwise it's just is the "normal" location of the
2159
   token which (*TOKEN)->src_loc.  */
2160
static inline void
2161
consume_next_token_from_context (cpp_reader *pfile,
2162
                                 const cpp_token ** token,
2163
                                 source_location *location)
2164
{
2165
  cpp_context *c = pfile->context;
2166
 
2167
  if ((c)->tokens_kind == TOKENS_KIND_DIRECT)
2168
    {
2169
      *token = FIRST (c).token;
2170
      *location = (*token)->src_loc;
2171
      FIRST (c).token++;
2172
    }
2173
  else if ((c)->tokens_kind == TOKENS_KIND_INDIRECT)
2174
    {
2175
      *token = *FIRST (c).ptoken;
2176
      *location = (*token)->src_loc;
2177
      FIRST (c).ptoken++;
2178
    }
2179
  else if ((c)->tokens_kind == TOKENS_KIND_EXTENDED)
2180
    {
2181
      macro_context *m = c->c.mc;
2182
      *token = *FIRST (c).ptoken;
2183
      if (m->virt_locs)
2184
        {
2185
          *location = *m->cur_virt_loc;
2186
          m->cur_virt_loc++;
2187
        }
2188
      else
2189
        *location = (*token)->src_loc;
2190
      FIRST (c).ptoken++;
2191
    }
2192
  else
2193
    abort ();
2194
}
2195
 
2196
/* In the traditional mode of the preprocessor, if we are currently in
2197
   a directive, the location of a token must be the location of the
2198
   start of the directive line.  This function returns the proper
2199
   location if we are in the traditional mode, and just returns
2200
   LOCATION otherwise.  */
2201
 
2202
static inline source_location
2203
maybe_adjust_loc_for_trad_cpp (cpp_reader *pfile, source_location location)
2204
{
2205
  if (CPP_OPTION (pfile, traditional))
2206
    {
2207
      if (pfile->state.in_directive)
2208
        return pfile->directive_line;
2209
    }
2210
  return location;
2211
}
2212
 
2213
/* Routine to get a token as well as its location.
2214
 
2215
   Macro expansions and directives are transparently handled,
2216
   including entering included files.  Thus tokens are post-macro
2217
   expansion, and after any intervening directives.  External callers
2218
   see CPP_EOF only at EOF.  Internal callers also see it when meeting
2219
   a directive inside a macro call, when at the end of a directive and
2220
   state.in_directive is still 1, and at the end of argument
2221
   pre-expansion.
2222
 
2223
   LOC is an out parameter; *LOC is set to the location "as expected
2224
   by the user".  Please read the comment of
2225
   cpp_get_token_with_location to learn more about the meaning of this
2226
   location.  */
2227
static const cpp_token*
2228
cpp_get_token_1 (cpp_reader *pfile, source_location *location)
2229
{
2230
  const cpp_token *result;
2231
  bool can_set = pfile->set_invocation_location;
2232
  /* This token is a virtual token that either encodes a location
2233
     related to macro expansion or a spelling location.  */
2234
  source_location virt_loc = 0;
2235
  pfile->set_invocation_location = false;
2236
 
2237
  for (;;)
2238
    {
2239
      cpp_hashnode *node;
2240
      cpp_context *context = pfile->context;
2241
 
2242
      /* Context->prev == 0 <=> base context.  */
2243
      if (!context->prev)
2244
        {
2245
          result = _cpp_lex_token (pfile);
2246
          virt_loc = result->src_loc;
2247
        }
2248
      else if (!reached_end_of_context (context))
2249
        {
2250
          consume_next_token_from_context (pfile, &result,
2251
                                           &virt_loc);
2252
          if (result->flags & PASTE_LEFT)
2253
            {
2254
              paste_all_tokens (pfile, result);
2255
              if (pfile->state.in_directive)
2256
                continue;
2257
              result = padding_token (pfile, result);
2258
              goto out;
2259
            }
2260
        }
2261
      else
2262
        {
2263
          if (pfile->context->c.macro)
2264
            ++num_expanded_macros_counter;
2265
          _cpp_pop_context (pfile);
2266
          if (pfile->state.in_directive)
2267
            continue;
2268
          result = &pfile->avoid_paste;
2269
          goto out;
2270
        }
2271
 
2272
      if (pfile->state.in_directive && result->type == CPP_COMMENT)
2273
        continue;
2274
 
2275
      if (result->type != CPP_NAME)
2276
        break;
2277
 
2278
      node = result->val.node.node;
2279
 
2280
      if (node->type != NT_MACRO || (result->flags & NO_EXPAND))
2281
        break;
2282
 
2283
      if (!(node->flags & NODE_DISABLED))
2284
        {
2285
          int ret = 0;
2286
          /* If not in a macro context, and we're going to start an
2287
             expansion, record the location.  */
2288
          if (can_set && !context->c.macro)
2289
            pfile->invocation_location = result->src_loc;
2290
          if (pfile->state.prevent_expansion)
2291
            break;
2292
 
2293
          /* Conditional macros require that a predicate be evaluated
2294
             first.  */
2295
          if ((node->flags & NODE_CONDITIONAL) != 0)
2296
            {
2297
              if (pfile->cb.macro_to_expand)
2298
                {
2299
                  bool whitespace_after;
2300
                  const cpp_token *peek_tok = cpp_peek_token (pfile, 0);
2301
 
2302
                  whitespace_after = (peek_tok->type == CPP_PADDING
2303
                                      || (peek_tok->flags & PREV_WHITE));
2304
                  node = pfile->cb.macro_to_expand (pfile, result);
2305
                  if (node)
2306
                    ret = enter_macro_context (pfile, node, result,
2307
                                               virt_loc);
2308
                  else if (whitespace_after)
2309
                    {
2310
                      /* If macro_to_expand hook returned NULL and it
2311
                         ate some tokens, see if we don't need to add
2312
                         a padding token in between this and the
2313
                         next token.  */
2314
                      peek_tok = cpp_peek_token (pfile, 0);
2315
                      if (peek_tok->type != CPP_PADDING
2316
                          && (peek_tok->flags & PREV_WHITE) == 0)
2317
                        _cpp_push_token_context (pfile, NULL,
2318
                                                 padding_token (pfile,
2319
                                                                peek_tok), 1);
2320
                    }
2321
                }
2322
            }
2323
          else
2324
            ret = enter_macro_context (pfile, node, result,
2325
                                       virt_loc);
2326
          if (ret)
2327
            {
2328
              if (pfile->state.in_directive || ret == 2)
2329
                continue;
2330
              result = padding_token (pfile, result);
2331
              goto out;
2332
            }
2333
        }
2334
      else
2335
        {
2336
          /* Flag this token as always unexpandable.  FIXME: move this
2337
             to collect_args()?.  */
2338
          cpp_token *t = _cpp_temp_token (pfile);
2339
          t->type = result->type;
2340
          t->flags = result->flags | NO_EXPAND;
2341
          t->val = result->val;
2342
          result = t;
2343
        }
2344
 
2345
      break;
2346
    }
2347
 
2348
 out:
2349
  if (location != NULL)
2350
    {
2351
      if (virt_loc == 0)
2352
        virt_loc = result->src_loc;
2353
      *location = virt_loc;
2354
 
2355
      if (!CPP_OPTION (pfile, track_macro_expansion)
2356
          && can_set
2357
          && pfile->context->c.macro != NULL)
2358
        /* We are in a macro expansion context, are not tracking
2359
           virtual location, but were asked to report the location
2360
           of the expansion point of the macro being expanded.  */
2361
        *location = pfile->invocation_location;
2362
 
2363
      *location = maybe_adjust_loc_for_trad_cpp (pfile, *location);
2364
    }
2365
  return result;
2366
}
2367
 
2368
/* External routine to get a token.  Also used nearly everywhere
2369
   internally, except for places where we know we can safely call
2370
   _cpp_lex_token directly, such as lexing a directive name.
2371
 
2372
   Macro expansions and directives are transparently handled,
2373
   including entering included files.  Thus tokens are post-macro
2374
   expansion, and after any intervening directives.  External callers
2375
   see CPP_EOF only at EOF.  Internal callers also see it when meeting
2376
   a directive inside a macro call, when at the end of a directive and
2377
   state.in_directive is still 1, and at the end of argument
2378
   pre-expansion.  */
2379
const cpp_token *
2380
cpp_get_token (cpp_reader *pfile)
2381
{
2382
  return cpp_get_token_1 (pfile, NULL);
2383
}
2384
 
2385
/* Like cpp_get_token, but also returns a virtual token location
2386
   separate from the spelling location carried by the returned token.
2387
 
2388
   LOC is an out parameter; *LOC is set to the location "as expected
2389
   by the user".  This matters when a token results from macro
2390
   expansion; in that case the token's spelling location indicates the
2391
   locus of the token in the definition of the macro but *LOC
2392
   virtually encodes all the other meaningful locuses associated to
2393
   the token.
2394
 
2395
   What? virtual location? Yes, virtual location.
2396
 
2397
   If the token results from macro expansion and if macro expansion
2398
   location tracking is enabled its virtual location encodes (at the
2399
   same time):
2400
 
2401
   - the spelling location of the token
2402
 
2403
   - the locus of the macro expansion point
2404
 
2405
   - the locus of the point where the token got instantiated as part
2406
     of the macro expansion process.
2407
 
2408
   You have to use the linemap API to get the locus you are interested
2409
   in from a given virtual location.
2410
 
2411
   Note however that virtual locations are not necessarily ordered for
2412
   relations '<' and '>'.  One must use the function
2413
   linemap_location_before_p instead of using the relational operator
2414
   '<'.
2415
 
2416
   If macro expansion tracking is off and if the token results from
2417
   macro expansion the virtual location is the expansion point of the
2418
   macro that got expanded.
2419
 
2420
   When the token doesn't result from macro expansion, the virtual
2421
   location is just the same thing as its spelling location.  */
2422
 
2423
const cpp_token *
2424
cpp_get_token_with_location (cpp_reader *pfile, source_location *loc)
2425
{
2426
  const cpp_token *result;
2427
 
2428
  pfile->set_invocation_location = true;
2429
  result = cpp_get_token_1 (pfile, loc);
2430
  return result;
2431
}
2432
 
2433
/* Returns true if we're expanding an object-like macro that was
2434
   defined in a system header.  Just checks the macro at the top of
2435
   the stack.  Used for diagnostic suppression.  */
2436
int
2437
cpp_sys_macro_p (cpp_reader *pfile)
2438
{
2439
  cpp_hashnode *node = pfile->context->c.macro;
2440
 
2441
  return node && node->value.macro && node->value.macro->syshdr;
2442
}
2443
 
2444
/* Read each token in, until end of the current file.  Directives are
2445
   transparently processed.  */
2446
void
2447
cpp_scan_nooutput (cpp_reader *pfile)
2448
{
2449
  /* Request a CPP_EOF token at the end of this file, rather than
2450
     transparently continuing with the including file.  */
2451
  pfile->buffer->return_at_eof = true;
2452
 
2453
  pfile->state.discarding_output++;
2454
  pfile->state.prevent_expansion++;
2455
 
2456
  if (CPP_OPTION (pfile, traditional))
2457
    while (_cpp_read_logical_line_trad (pfile))
2458
      ;
2459
  else
2460
    while (cpp_get_token (pfile)->type != CPP_EOF)
2461
      ;
2462
 
2463
  pfile->state.discarding_output--;
2464
  pfile->state.prevent_expansion--;
2465
}
2466
 
2467
/* Step back one or more tokens obtained from the lexer.  */
2468
void
2469
_cpp_backup_tokens_direct (cpp_reader *pfile, unsigned int count)
2470
{
2471
  pfile->lookaheads += count;
2472
  while (count--)
2473
    {
2474
      pfile->cur_token--;
2475
      if (pfile->cur_token == pfile->cur_run->base
2476
          /* Possible with -fpreprocessed and no leading #line.  */
2477
          && pfile->cur_run->prev != NULL)
2478
        {
2479
          pfile->cur_run = pfile->cur_run->prev;
2480
          pfile->cur_token = pfile->cur_run->limit;
2481
        }
2482
    }
2483
}
2484
 
2485
/* Step back one (or more) tokens.  Can only step back more than 1 if
2486
   they are from the lexer, and not from macro expansion.  */
2487
void
2488
_cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
2489
{
2490
  if (pfile->context->prev == NULL)
2491
    _cpp_backup_tokens_direct (pfile, count);
2492
  else
2493
    {
2494
      if (count != 1)
2495
        abort ();
2496
      if (pfile->context->tokens_kind == TOKENS_KIND_DIRECT)
2497
        FIRST (pfile->context).token--;
2498
      else if (pfile->context->tokens_kind == TOKENS_KIND_INDIRECT)
2499
        FIRST (pfile->context).ptoken--;
2500
      else if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
2501
        {
2502
          FIRST (pfile->context).ptoken--;
2503
          if (pfile->context->c.macro)
2504
            {
2505
              macro_context *m = pfile->context->c.mc;
2506
              m->cur_virt_loc--;
2507
#ifdef ENABLE_CHECKING
2508
              if (m->cur_virt_loc < m->virt_locs)
2509
                abort ();
2510
#endif
2511
            }
2512
          else
2513
            abort ();
2514
        }
2515
      else
2516
        abort ();
2517
    }
2518
}
2519
 
2520
/* #define directive parsing and handling.  */
2521
 
2522
/* Returns nonzero if a macro redefinition warning is required.  */
2523
static bool
2524
warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node,
2525
                      const cpp_macro *macro2)
2526
{
2527
  const cpp_macro *macro1;
2528
  unsigned int i;
2529
 
2530
  /* Some redefinitions need to be warned about regardless.  */
2531
  if (node->flags & NODE_WARN)
2532
    return true;
2533
 
2534
  /* Suppress warnings for builtins that lack the NODE_WARN flag.  */
2535
  if (node->flags & NODE_BUILTIN)
2536
    {
2537
      if (!pfile->cb.user_builtin_macro
2538
          || !pfile->cb.user_builtin_macro (pfile, node))
2539
        return false;
2540
    }
2541
 
2542
  /* Redefinitions of conditional (context-sensitive) macros, on
2543
     the other hand, must be allowed silently.  */
2544
  if (node->flags & NODE_CONDITIONAL)
2545
    return false;
2546
 
2547
  /* Redefinition of a macro is allowed if and only if the old and new
2548
     definitions are the same.  (6.10.3 paragraph 2).  */
2549
  macro1 = node->value.macro;
2550
 
2551
  /* Don't check count here as it can be different in valid
2552
     traditional redefinitions with just whitespace differences.  */
2553
  if (macro1->paramc != macro2->paramc
2554
      || macro1->fun_like != macro2->fun_like
2555
      || macro1->variadic != macro2->variadic)
2556
    return true;
2557
 
2558
  /* Check parameter spellings.  */
2559
  for (i = 0; i < macro1->paramc; i++)
2560
    if (macro1->params[i] != macro2->params[i])
2561
      return true;
2562
 
2563
  /* Check the replacement text or tokens.  */
2564
  if (CPP_OPTION (pfile, traditional))
2565
    return _cpp_expansions_different_trad (macro1, macro2);
2566
 
2567
  if (macro1->count != macro2->count)
2568
    return true;
2569
 
2570
  for (i = 0; i < macro1->count; i++)
2571
    if (!_cpp_equiv_tokens (&macro1->exp.tokens[i], &macro2->exp.tokens[i]))
2572
      return true;
2573
 
2574
  return false;
2575
}
2576
 
2577
/* Free the definition of hashnode H.  */
2578
void
2579
_cpp_free_definition (cpp_hashnode *h)
2580
{
2581
  /* Macros and assertions no longer have anything to free.  */
2582
  h->type = NT_VOID;
2583
  /* Clear builtin flag in case of redefinition.  */
2584
  h->flags &= ~(NODE_BUILTIN | NODE_DISABLED | NODE_USED);
2585
}
2586
 
2587
/* Save parameter NODE to the parameter list of macro MACRO.  Returns
2588
   zero on success, nonzero if the parameter is a duplicate.  */
2589
bool
2590
_cpp_save_parameter (cpp_reader *pfile, cpp_macro *macro, cpp_hashnode *node)
2591
{
2592
  unsigned int len;
2593
  /* Constraint 6.10.3.6 - duplicate parameter names.  */
2594
  if (node->flags & NODE_MACRO_ARG)
2595
    {
2596
      cpp_error (pfile, CPP_DL_ERROR, "duplicate macro parameter \"%s\"",
2597
                 NODE_NAME (node));
2598
      return true;
2599
    }
2600
 
2601
  if (BUFF_ROOM (pfile->a_buff)
2602
      < (macro->paramc + 1) * sizeof (cpp_hashnode *))
2603
    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_hashnode *));
2604
 
2605
  ((cpp_hashnode **) BUFF_FRONT (pfile->a_buff))[macro->paramc++] = node;
2606
  node->flags |= NODE_MACRO_ARG;
2607
  len = macro->paramc * sizeof (union _cpp_hashnode_value);
2608
  if (len > pfile->macro_buffer_len)
2609
    {
2610
      pfile->macro_buffer = XRESIZEVEC (unsigned char, pfile->macro_buffer,
2611
                                        len);
2612
      pfile->macro_buffer_len = len;
2613
    }
2614
  ((union _cpp_hashnode_value *) pfile->macro_buffer)[macro->paramc - 1]
2615
    = node->value;
2616
 
2617
  node->value.arg_index  = macro->paramc;
2618
  return false;
2619
}
2620
 
2621
/* Check the syntax of the parameters in a MACRO definition.  Returns
2622
   false if an error occurs.  */
2623
static bool
2624
parse_params (cpp_reader *pfile, cpp_macro *macro)
2625
{
2626
  unsigned int prev_ident = 0;
2627
 
2628
  for (;;)
2629
    {
2630
      const cpp_token *token = _cpp_lex_token (pfile);
2631
 
2632
      switch (token->type)
2633
        {
2634
        default:
2635
          /* Allow/ignore comments in parameter lists if we are
2636
             preserving comments in macro expansions.  */
2637
          if (token->type == CPP_COMMENT
2638
              && ! CPP_OPTION (pfile, discard_comments_in_macro_exp))
2639
            continue;
2640
 
2641
          cpp_error (pfile, CPP_DL_ERROR,
2642
                     "\"%s\" may not appear in macro parameter list",
2643
                     cpp_token_as_text (pfile, token));
2644
          return false;
2645
 
2646
        case CPP_NAME:
2647
          if (prev_ident)
2648
            {
2649
              cpp_error (pfile, CPP_DL_ERROR,
2650
                         "macro parameters must be comma-separated");
2651
              return false;
2652
            }
2653
          prev_ident = 1;
2654
 
2655
          if (_cpp_save_parameter (pfile, macro, token->val.node.node))
2656
            return false;
2657
          continue;
2658
 
2659
        case CPP_CLOSE_PAREN:
2660
          if (prev_ident || macro->paramc == 0)
2661
            return true;
2662
 
2663
          /* Fall through to pick up the error.  */
2664
        case CPP_COMMA:
2665
          if (!prev_ident)
2666
            {
2667
              cpp_error (pfile, CPP_DL_ERROR, "parameter name missing");
2668
              return false;
2669
            }
2670
          prev_ident = 0;
2671
          continue;
2672
 
2673
        case CPP_ELLIPSIS:
2674
          macro->variadic = 1;
2675
          if (!prev_ident)
2676
            {
2677
              _cpp_save_parameter (pfile, macro,
2678
                                   pfile->spec_nodes.n__VA_ARGS__);
2679
              pfile->state.va_args_ok = 1;
2680
              if (! CPP_OPTION (pfile, c99)
2681
                  && CPP_OPTION (pfile, cpp_pedantic)
2682
                  && CPP_OPTION (pfile, warn_variadic_macros))
2683
                cpp_pedwarning
2684
                  (pfile, CPP_W_VARIADIC_MACROS,
2685
                   "anonymous variadic macros were introduced in C99");
2686
            }
2687
          else if (CPP_OPTION (pfile, cpp_pedantic)
2688
                   && CPP_OPTION (pfile, warn_variadic_macros))
2689
            cpp_pedwarning (pfile, CPP_W_VARIADIC_MACROS,
2690
                            "ISO C does not permit named variadic macros");
2691
 
2692
          /* We're at the end, and just expect a closing parenthesis.  */
2693
          token = _cpp_lex_token (pfile);
2694
          if (token->type == CPP_CLOSE_PAREN)
2695
            return true;
2696
          /* Fall through.  */
2697
 
2698
        case CPP_EOF:
2699
          cpp_error (pfile, CPP_DL_ERROR, "missing ')' in macro parameter list");
2700
          return false;
2701
        }
2702
    }
2703
}
2704
 
2705
/* Allocate room for a token from a macro's replacement list.  */
2706
static cpp_token *
2707
alloc_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2708
{
2709
  if (BUFF_ROOM (pfile->a_buff) < (macro->count + 1) * sizeof (cpp_token))
2710
    _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (cpp_token));
2711
 
2712
  return &((cpp_token *) BUFF_FRONT (pfile->a_buff))[macro->count++];
2713
}
2714
 
2715
/* Lex a token from the expansion of MACRO, but mark parameters as we
2716
   find them and warn of traditional stringification.  */
2717
static cpp_token *
2718
lex_expansion_token (cpp_reader *pfile, cpp_macro *macro)
2719
{
2720
  cpp_token *token, *saved_cur_token;
2721
 
2722
  saved_cur_token = pfile->cur_token;
2723
  pfile->cur_token = alloc_expansion_token (pfile, macro);
2724
  token = _cpp_lex_direct (pfile);
2725
  pfile->cur_token = saved_cur_token;
2726
 
2727
  /* Is this a parameter?  */
2728
  if (token->type == CPP_NAME
2729
      && (token->val.node.node->flags & NODE_MACRO_ARG) != 0)
2730
    {
2731
      token->type = CPP_MACRO_ARG;
2732
      token->val.macro_arg.arg_no = token->val.node.node->value.arg_index;
2733
    }
2734
  else if (CPP_WTRADITIONAL (pfile) && macro->paramc > 0
2735
           && (token->type == CPP_STRING || token->type == CPP_CHAR))
2736
    check_trad_stringification (pfile, macro, &token->val.str);
2737
 
2738
  return token;
2739
}
2740
 
2741
static bool
2742
create_iso_definition (cpp_reader *pfile, cpp_macro *macro)
2743
{
2744
  cpp_token *token;
2745
  const cpp_token *ctoken;
2746
  bool following_paste_op = false;
2747
  const char *paste_op_error_msg =
2748
    N_("'##' cannot appear at either end of a macro expansion");
2749
  unsigned int num_extra_tokens = 0;
2750
 
2751
  /* Get the first token of the expansion (or the '(' of a
2752
     function-like macro).  */
2753
  ctoken = _cpp_lex_token (pfile);
2754
 
2755
  if (ctoken->type == CPP_OPEN_PAREN && !(ctoken->flags & PREV_WHITE))
2756
    {
2757
      bool ok = parse_params (pfile, macro);
2758
      macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
2759
      if (!ok)
2760
        return false;
2761
 
2762
      /* Success.  Commit or allocate the parameter array.  */
2763
      if (pfile->hash_table->alloc_subobject)
2764
        {
2765
          cpp_hashnode **params =
2766
            (cpp_hashnode **) pfile->hash_table->alloc_subobject
2767
            (sizeof (cpp_hashnode *) * macro->paramc);
2768
          memcpy (params, macro->params,
2769
                  sizeof (cpp_hashnode *) * macro->paramc);
2770
          macro->params = params;
2771
        }
2772
      else
2773
        BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
2774
      macro->fun_like = 1;
2775
    }
2776
  else if (ctoken->type != CPP_EOF && !(ctoken->flags & PREV_WHITE))
2777
    {
2778
      /* While ISO C99 requires whitespace before replacement text
2779
         in a macro definition, ISO C90 with TC1 allows there characters
2780
         from the basic source character set.  */
2781
      if (CPP_OPTION (pfile, c99))
2782
        cpp_error (pfile, CPP_DL_PEDWARN,
2783
                   "ISO C99 requires whitespace after the macro name");
2784
      else
2785
        {
2786
          int warntype = CPP_DL_WARNING;
2787
          switch (ctoken->type)
2788
            {
2789
            case CPP_ATSIGN:
2790
            case CPP_AT_NAME:
2791
            case CPP_OBJC_STRING:
2792
              /* '@' is not in basic character set.  */
2793
              warntype = CPP_DL_PEDWARN;
2794
              break;
2795
            case CPP_OTHER:
2796
              /* Basic character set sans letters, digits and _.  */
2797
              if (strchr ("!\"#%&'()*+,-./:;<=>?[\\]^{|}~",
2798
                          ctoken->val.str.text[0]) == NULL)
2799
                warntype = CPP_DL_PEDWARN;
2800
              break;
2801
            default:
2802
              /* All other tokens start with a character from basic
2803
                 character set.  */
2804
              break;
2805
            }
2806
          cpp_error (pfile, warntype,
2807
                     "missing whitespace after the macro name");
2808
        }
2809
    }
2810
 
2811
  if (macro->fun_like)
2812
    token = lex_expansion_token (pfile, macro);
2813
  else
2814
    {
2815
      token = alloc_expansion_token (pfile, macro);
2816
      *token = *ctoken;
2817
    }
2818
 
2819
  for (;;)
2820
    {
2821
      /* Check the stringifying # constraint 6.10.3.2.1 of
2822
         function-like macros when lexing the subsequent token.  */
2823
      if (macro->count > 1 && token[-1].type == CPP_HASH && macro->fun_like)
2824
        {
2825
          if (token->type == CPP_MACRO_ARG)
2826
            {
2827
              if (token->flags & PREV_WHITE)
2828
                token->flags |= SP_PREV_WHITE;
2829
              if (token[-1].flags & DIGRAPH)
2830
                token->flags |= SP_DIGRAPH;
2831
              token->flags &= ~PREV_WHITE;
2832
              token->flags |= STRINGIFY_ARG;
2833
              token->flags |= token[-1].flags & PREV_WHITE;
2834
              token[-1] = token[0];
2835
              macro->count--;
2836
            }
2837
          /* Let assembler get away with murder.  */
2838
          else if (CPP_OPTION (pfile, lang) != CLK_ASM)
2839
            {
2840
              cpp_error (pfile, CPP_DL_ERROR,
2841
                         "'#' is not followed by a macro parameter");
2842
              return false;
2843
            }
2844
        }
2845
 
2846
      if (token->type == CPP_EOF)
2847
        {
2848
          /* Paste operator constraint 6.10.3.3.1:
2849
             Token-paste ##, can appear in both object-like and
2850
             function-like macros, but not at the end.  */
2851
          if (following_paste_op)
2852
            {
2853
              cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
2854
              return false;
2855
            }
2856
          break;
2857
        }
2858
 
2859
      /* Paste operator constraint 6.10.3.3.1.  */
2860
      if (token->type == CPP_PASTE)
2861
        {
2862
          /* Token-paste ##, can appear in both object-like and
2863
             function-like macros, but not at the beginning.  */
2864
          if (macro->count == 1)
2865
            {
2866
              cpp_error (pfile, CPP_DL_ERROR, paste_op_error_msg);
2867
              return false;
2868
            }
2869
 
2870
          if (token[-1].flags & PASTE_LEFT)
2871
            {
2872
              macro->extra_tokens = 1;
2873
              num_extra_tokens++;
2874
              token->val.token_no = macro->count - 1;
2875
            }
2876
          else
2877
            {
2878
              --macro->count;
2879
              token[-1].flags |= PASTE_LEFT;
2880
              if (token->flags & DIGRAPH)
2881
                token[-1].flags |= SP_DIGRAPH;
2882
              if (token->flags & PREV_WHITE)
2883
                token[-1].flags |= SP_PREV_WHITE;
2884
            }
2885
        }
2886
 
2887
      following_paste_op = (token->type == CPP_PASTE);
2888
      token = lex_expansion_token (pfile, macro);
2889
    }
2890
 
2891
  macro->exp.tokens = (cpp_token *) BUFF_FRONT (pfile->a_buff);
2892
  macro->traditional = 0;
2893
 
2894
  /* Don't count the CPP_EOF.  */
2895
  macro->count--;
2896
 
2897
  /* Clear whitespace on first token for warn_of_redefinition().  */
2898
  if (macro->count)
2899
    macro->exp.tokens[0].flags &= ~PREV_WHITE;
2900
 
2901
  /* Commit or allocate the memory.  */
2902
  if (pfile->hash_table->alloc_subobject)
2903
    {
2904
      cpp_token *tokns =
2905
        (cpp_token *) pfile->hash_table->alloc_subobject (sizeof (cpp_token)
2906
                                                          * macro->count);
2907
      if (num_extra_tokens)
2908
        {
2909
          /* Place second and subsequent ## or %:%: tokens in
2910
             sequences of consecutive such tokens at the end of the
2911
             list to preserve information about where they appear, how
2912
             they are spelt and whether they are preceded by
2913
             whitespace without otherwise interfering with macro
2914
             expansion.  */
2915
          cpp_token *normal_dest = tokns;
2916
          cpp_token *extra_dest = tokns + macro->count - num_extra_tokens;
2917
          unsigned int i;
2918
          for (i = 0; i < macro->count; i++)
2919
            {
2920
              if (macro->exp.tokens[i].type == CPP_PASTE)
2921
                *extra_dest++ = macro->exp.tokens[i];
2922
              else
2923
                *normal_dest++ = macro->exp.tokens[i];
2924
            }
2925
        }
2926
      else
2927
        memcpy (tokns, macro->exp.tokens, sizeof (cpp_token) * macro->count);
2928
      macro->exp.tokens = tokns;
2929
    }
2930
  else
2931
    BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->exp.tokens[macro->count];
2932
 
2933
  return true;
2934
}
2935
 
2936
/* Parse a macro and save its expansion.  Returns nonzero on success.  */
2937
bool
2938
_cpp_create_definition (cpp_reader *pfile, cpp_hashnode *node)
2939
{
2940
  cpp_macro *macro;
2941
  unsigned int i;
2942
  bool ok;
2943
 
2944
  if (pfile->hash_table->alloc_subobject)
2945
    macro = (cpp_macro *) pfile->hash_table->alloc_subobject
2946
      (sizeof (cpp_macro));
2947
  else
2948
    macro = (cpp_macro *) _cpp_aligned_alloc (pfile, sizeof (cpp_macro));
2949
  macro->line = pfile->directive_line;
2950
  macro->params = 0;
2951
  macro->paramc = 0;
2952
  macro->variadic = 0;
2953
  macro->used = !CPP_OPTION (pfile, warn_unused_macros);
2954
  macro->count = 0;
2955
  macro->fun_like = 0;
2956
  macro->extra_tokens = 0;
2957
  /* To suppress some diagnostics.  */
2958
  macro->syshdr = pfile->buffer && pfile->buffer->sysp != 0;
2959
 
2960
  if (CPP_OPTION (pfile, traditional))
2961
    ok = _cpp_create_trad_definition (pfile, macro);
2962
  else
2963
    {
2964
      ok = create_iso_definition (pfile, macro);
2965
 
2966
      /* We set the type for SEEN_EOL() in directives.c.
2967
 
2968
         Longer term we should lex the whole line before coming here,
2969
         and just copy the expansion.  */
2970
 
2971
      /* Stop the lexer accepting __VA_ARGS__.  */
2972
      pfile->state.va_args_ok = 0;
2973
    }
2974
 
2975
  /* Clear the fast argument lookup indices.  */
2976
  for (i = macro->paramc; i-- > 0; )
2977
    {
2978
      struct cpp_hashnode *node = macro->params[i];
2979
      node->flags &= ~ NODE_MACRO_ARG;
2980
      node->value = ((union _cpp_hashnode_value *) pfile->macro_buffer)[i];
2981
    }
2982
 
2983
  if (!ok)
2984
    return ok;
2985
 
2986
  if (node->type == NT_MACRO)
2987
    {
2988
      if (CPP_OPTION (pfile, warn_unused_macros))
2989
        _cpp_warn_if_unused_macro (pfile, node, NULL);
2990
 
2991
      if (warn_of_redefinition (pfile, node, macro))
2992
        {
2993
          const int reason = (node->flags & NODE_BUILTIN)
2994
                             ? CPP_W_BUILTIN_MACRO_REDEFINED : CPP_W_NONE;
2995
          bool warned;
2996
 
2997
          warned = cpp_pedwarning_with_line (pfile, reason,
2998
                                             pfile->directive_line, 0,
2999
                                             "\"%s\" redefined",
3000
                                             NODE_NAME (node));
3001
 
3002
          if (warned && node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
3003
            cpp_error_with_line (pfile, CPP_DL_NOTE,
3004
                                 node->value.macro->line, 0,
3005
                         "this is the location of the previous definition");
3006
        }
3007
    }
3008
 
3009
  if (node->type != NT_VOID)
3010
    _cpp_free_definition (node);
3011
 
3012
  /* Enter definition in hash table.  */
3013
  node->type = NT_MACRO;
3014
  node->value.macro = macro;
3015
  if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_"))
3016
      && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_FORMAT_MACROS")
3017
      /* __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are mentioned
3018
         in the C standard, as something that one must use in C++.
3019
         However DR#593 indicates that these aren't actually mentioned
3020
         in the C++ standard.  We special-case them anyway.  */
3021
      && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_LIMIT_MACROS")
3022
      && ustrcmp (NODE_NAME (node), (const uchar *) "__STDC_CONSTANT_MACROS"))
3023
    node->flags |= NODE_WARN;
3024
 
3025
  /* If user defines one of the conditional macros, remove the
3026
     conditional flag */
3027
  node->flags &= ~NODE_CONDITIONAL;
3028
 
3029
  return ok;
3030
}
3031
 
3032
/* Warn if a token in STRING matches one of a function-like MACRO's
3033
   parameters.  */
3034
static void
3035
check_trad_stringification (cpp_reader *pfile, const cpp_macro *macro,
3036
                            const cpp_string *string)
3037
{
3038
  unsigned int i, len;
3039
  const uchar *p, *q, *limit;
3040
 
3041
  /* Loop over the string.  */
3042
  limit = string->text + string->len - 1;
3043
  for (p = string->text + 1; p < limit; p = q)
3044
    {
3045
      /* Find the start of an identifier.  */
3046
      while (p < limit && !is_idstart (*p))
3047
        p++;
3048
 
3049
      /* Find the end of the identifier.  */
3050
      q = p;
3051
      while (q < limit && is_idchar (*q))
3052
        q++;
3053
 
3054
      len = q - p;
3055
 
3056
      /* Loop over the function macro arguments to see if the
3057
         identifier inside the string matches one of them.  */
3058
      for (i = 0; i < macro->paramc; i++)
3059
        {
3060
          const cpp_hashnode *node = macro->params[i];
3061
 
3062
          if (NODE_LEN (node) == len
3063
              && !memcmp (p, NODE_NAME (node), len))
3064
            {
3065
              cpp_error (pfile, CPP_DL_WARNING,
3066
           "macro argument \"%s\" would be stringified in traditional C",
3067
                         NODE_NAME (node));
3068
              break;
3069
            }
3070
        }
3071
    }
3072
}
3073
 
3074
/* Returns the name, arguments and expansion of a macro, in a format
3075
   suitable to be read back in again, and therefore also for DWARF 2
3076
   debugging info.  e.g. "PASTE(X, Y) X ## Y", or "MACNAME EXPANSION".
3077
   Caller is expected to generate the "#define" bit if needed.  The
3078
   returned text is temporary, and automatically freed later.  */
3079
const unsigned char *
3080
cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node)
3081
{
3082
  unsigned int i, len;
3083
  const cpp_macro *macro;
3084
  unsigned char *buffer;
3085
 
3086
  if (node->type != NT_MACRO || (node->flags & NODE_BUILTIN))
3087
    {
3088
      if (node->type != NT_MACRO
3089
          || !pfile->cb.user_builtin_macro
3090
          || !pfile->cb.user_builtin_macro (pfile, node))
3091
        {
3092
          cpp_error (pfile, CPP_DL_ICE,
3093
                     "invalid hash type %d in cpp_macro_definition",
3094
                     node->type);
3095
          return 0;
3096
        }
3097
    }
3098
 
3099
  macro = node->value.macro;
3100
  /* Calculate length.  */
3101
  len = NODE_LEN (node) + 2;                    /* ' ' and NUL.  */
3102
  if (macro->fun_like)
3103
    {
3104
      len += 4;         /* "()" plus possible final ".." of named
3105
                           varargs (we have + 1 below).  */
3106
      for (i = 0; i < macro->paramc; i++)
3107
        len += NODE_LEN (macro->params[i]) + 1; /* "," */
3108
    }
3109
 
3110
  /* This should match below where we fill in the buffer.  */
3111
  if (CPP_OPTION (pfile, traditional))
3112
    len += _cpp_replacement_text_len (macro);
3113
  else
3114
    {
3115
      unsigned int count = macro_real_token_count (macro);
3116
      for (i = 0; i < count; i++)
3117
        {
3118
          cpp_token *token = &macro->exp.tokens[i];
3119
 
3120
          if (token->type == CPP_MACRO_ARG)
3121
            len += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
3122
          else
3123
            len += cpp_token_len (token);
3124
 
3125
          if (token->flags & STRINGIFY_ARG)
3126
            len++;                      /* "#" */
3127
          if (token->flags & PASTE_LEFT)
3128
            len += 3;           /* " ##" */
3129
          if (token->flags & PREV_WHITE)
3130
            len++;              /* " " */
3131
        }
3132
    }
3133
 
3134
  if (len > pfile->macro_buffer_len)
3135
    {
3136
      pfile->macro_buffer = XRESIZEVEC (unsigned char,
3137
                                        pfile->macro_buffer, len);
3138
      pfile->macro_buffer_len = len;
3139
    }
3140
 
3141
  /* Fill in the buffer.  Start with the macro name.  */
3142
  buffer = pfile->macro_buffer;
3143
  memcpy (buffer, NODE_NAME (node), NODE_LEN (node));
3144
  buffer += NODE_LEN (node);
3145
 
3146
  /* Parameter names.  */
3147
  if (macro->fun_like)
3148
    {
3149
      *buffer++ = '(';
3150
      for (i = 0; i < macro->paramc; i++)
3151
        {
3152
          cpp_hashnode *param = macro->params[i];
3153
 
3154
          if (param != pfile->spec_nodes.n__VA_ARGS__)
3155
            {
3156
              memcpy (buffer, NODE_NAME (param), NODE_LEN (param));
3157
              buffer += NODE_LEN (param);
3158
            }
3159
 
3160
          if (i + 1 < macro->paramc)
3161
            /* Don't emit a space after the comma here; we're trying
3162
               to emit a Dwarf-friendly definition, and the Dwarf spec
3163
               forbids spaces in the argument list.  */
3164
            *buffer++ = ',';
3165
          else if (macro->variadic)
3166
            *buffer++ = '.', *buffer++ = '.', *buffer++ = '.';
3167
        }
3168
      *buffer++ = ')';
3169
    }
3170
 
3171
  /* The Dwarf spec requires a space after the macro name, even if the
3172
     definition is the empty string.  */
3173
  *buffer++ = ' ';
3174
 
3175
  if (CPP_OPTION (pfile, traditional))
3176
    buffer = _cpp_copy_replacement_text (macro, buffer);
3177
  else if (macro->count)
3178
  /* Expansion tokens.  */
3179
    {
3180
      unsigned int count = macro_real_token_count (macro);
3181
      for (i = 0; i < count; i++)
3182
        {
3183
          cpp_token *token = &macro->exp.tokens[i];
3184
 
3185
          if (token->flags & PREV_WHITE)
3186
            *buffer++ = ' ';
3187
          if (token->flags & STRINGIFY_ARG)
3188
            *buffer++ = '#';
3189
 
3190
          if (token->type == CPP_MACRO_ARG)
3191
            {
3192
              memcpy (buffer,
3193
                      NODE_NAME (macro->params[token->val.macro_arg.arg_no - 1]),
3194
                      NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]));
3195
              buffer += NODE_LEN (macro->params[token->val.macro_arg.arg_no - 1]);
3196
            }
3197
          else
3198
            buffer = cpp_spell_token (pfile, token, buffer, false);
3199
 
3200
          if (token->flags & PASTE_LEFT)
3201
            {
3202
              *buffer++ = ' ';
3203
              *buffer++ = '#';
3204
              *buffer++ = '#';
3205
              /* Next has PREV_WHITE; see _cpp_create_definition.  */
3206
            }
3207
        }
3208
    }
3209
 
3210
  *buffer = '\0';
3211
  return pfile->macro_buffer;
3212
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.