Newer
Older
Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
1999, 2000, 2001 Free Software Foundation, Inc.
Written by Per Bothner, 1994.
Based on CCCP program by Paul Rubin, June 1986
Adapted to ANSI C, Richard Stallman, Jan 1987
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding! */
#include "config.h"
#include "system.h"
cpp_hashnode **params; /* Parameters, if any. */
cpp_token *expansion; /* First token of replacement list. */
unsigned int line; /* Starting line number. */
unsigned int count; /* Number of tokens in expansion. */
unsigned short paramc; /* Number of parameters. */
unsigned int fun_like : 1; /* If a function-like macro. */
unsigned int variadic : 1; /* If a variadic macro. */
Neil Booth
committed
unsigned int syshdr : 1; /* If macro defined in system header. */
};
typedef struct macro_arg macro_arg;
struct macro_arg
{
const cpp_token **first; /* First token in unexpanded argument. */
const cpp_token **expanded; /* Macro-expanded argument. */
const cpp_token *stringified; /* Stringified argument. */
unsigned int count; /* # of tokens in argument. */
unsigned int expanded_count; /* # of tokens in expanded argument. */
Neil Booth
committed
static int enter_macro_context PARAMS ((cpp_reader *, cpp_hashnode *));
static const cpp_token *builtin_macro PARAMS ((cpp_reader *, cpp_hashnode *));
static void push_token_context
PARAMS ((cpp_reader *, cpp_macro *, const cpp_token *, unsigned int));
static void push_ptoken_context
PARAMS ((cpp_reader *, cpp_macro *, _cpp_buff *,
const cpp_token **, unsigned int));
static _cpp_buff *collect_args PARAMS ((cpp_reader *, const cpp_hashnode *));
static cpp_context *next_context PARAMS ((cpp_reader *));
static const cpp_token *padding_token
PARAMS ((cpp_reader *, const cpp_token *));
static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
static unsigned char *quote_string PARAMS ((unsigned char *,
const unsigned char *,
unsigned int));
static const cpp_token *new_string_token PARAMS ((cpp_reader *, U_CHAR *,
unsigned int));
static const cpp_token *new_number_token PARAMS ((cpp_reader *, int));
static const cpp_token *stringify_arg PARAMS ((cpp_reader *, macro_arg *));
static void paste_all_tokens PARAMS ((cpp_reader *, const cpp_token *));
static bool paste_tokens PARAMS ((cpp_reader *, const cpp_token **,
const cpp_token *));
static int funlike_invocation_p PARAMS ((cpp_reader *, const cpp_hashnode *));
static void replace_args PARAMS ((cpp_reader *, cpp_macro *, macro_arg *));
static cpp_token *alloc_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
static cpp_token *lex_expansion_token PARAMS ((cpp_reader *, cpp_macro *));
static int warn_of_redefinition PARAMS ((cpp_reader *, const cpp_hashnode *,
const cpp_macro *));
static int save_parameter PARAMS ((cpp_reader *, cpp_macro *, cpp_hashnode *));
static int parse_params PARAMS ((cpp_reader *, cpp_macro *));
static void check_trad_stringification PARAMS ((cpp_reader *,
const cpp_string *));
/* Allocates and returns a CPP_STRING token, containing TEXT of length
LEN, after null-terminating it. TEXT must be in permanent storage. */
static const cpp_token *
new_string_token (pfile, text, len)
cpp_reader *pfile;
unsigned char *text;
cpp_token *token = _cpp_temp_token (pfile);
token->val.str.len = len;
token->val.str.text = text;
/* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
static const cpp_token *
new_number_token (pfile, number)
cpp_token *token = _cpp_temp_token (pfile);
Neil Booth
committed
unsigned char *buf = _cpp_pool_alloc (&pfile->ident_pool, 20);
sprintf ((char *) buf, "%d", number);
token->type = CPP_NUMBER;
token->val.str.text = buf;
token->val.str.len = ustrlen (buf);
token->flags = 0;
}
static const char * const monthnames[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/* Handle builtin macros like __FILE__. */
static const cpp_token *
builtin_macro (pfile, node)
default:
cpp_ice (pfile, "invalid builtin macro \"%s\"", NODE_NAME (node));
return new_number_token (pfile, 1);
const struct line_map *map = pfile->map;
if (node->value.builtin == BT_BASE_FILE)
while (! MAIN_FILE_P (map))
map = INCLUDED_FROM (&pfile->line_maps, map);
name = map->to_file;
len = strlen (name);
buf = _cpp_pool_alloc (&pfile->ident_pool, len * 4 + 1);
len = quote_string (buf, (const unsigned char *) name, len) - buf;
return new_string_token (pfile, buf, len);
/* The line map depth counts the primary source as level 1, but
historically __INCLUDE_DEPTH__ has called the primary source
level 0. */
return new_number_token (pfile, pfile->line_maps.depth - 1);
case BT_SPECLINE:
/* If __LINE__ is embedded in a macro, it must expand to the
line of the macro's invocation, not its definition.
Otherwise things like assert() will not work properly. */
return new_number_token (pfile, SOURCE_LINE (pfile->map,
pfile->cur_token[-1].line));
int stdc = (!CPP_IN_SYSTEM_HEADER (pfile)
|| pfile->spec_nodes.n__STRICT_ANSI__->type != NT_VOID);
case BT_DATE:
case BT_TIME:
if (pfile->date.type == CPP_EOF)
{
/* Allocate __DATE__ and __TIME__ strings from permanent
storage. We only do this once, and don't generate them
at init time, because time() and localtime() are very
slow on some systems. */
time_t tt = time (NULL);
struct tm *tb = localtime (&tt);
pfile->date.val.str.text =
_cpp_pool_alloc (&pfile->ident_pool, sizeof ("Oct 11 1347"));
pfile->date.val.str.len = sizeof ("Oct 11 1347") - 1;
pfile->date.type = CPP_STRING;
pfile->date.flags = 0;
sprintf ((char *) pfile->date.val.str.text, "%s %2d %4d",
monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
pfile->time.val.str.text =
_cpp_pool_alloc (&pfile->ident_pool, sizeof ("12:34:56"));
pfile->time.val.str.len = sizeof ("12:34:56") - 1;
pfile->time.type = CPP_STRING;
pfile->time.flags = 0;
sprintf ((char *) pfile->time.val.str.text, "%02d:%02d:%02d",
tb->tm_hour, tb->tm_min, tb->tm_sec);
}
return node->value.builtin == BT_DATE ? &pfile->date: &pfile->time;
/* Adds backslashes before all backslashes and double quotes appearing
in strings. Non-printable characters are converted to octal. */
static U_CHAR *
quote_string (dest, src, len)
U_CHAR *dest;
const U_CHAR *src;
unsigned int len;
{
while (len--)
{
U_CHAR c = *src++;
if (c == '\\' || c == '"')
{
*dest++ = '\\';
*dest++ = c;
}
else
{
if (ISPRINT (c))
*dest++ = c;
else
/* Convert a token sequence to a single string token according to the
rules of the ISO C #-operator. */
stringify_arg (pfile, arg)
cpp_reader *pfile;
macro_arg *arg;
{
Neil Booth
committed
cpp_pool *pool = &pfile->ident_pool;
unsigned char *start = POOL_FRONT (pool);
unsigned int i, escape_it, total_len = 0, backslash_count = 0;
/* Loop, reading in the argument's tokens. */
for (i = 0; i < arg->count; i++)
{
unsigned char *dest;
const cpp_token *token = arg->first[i];
unsigned int len;
if (token->type == CPP_PADDING)
{
if (source == NULL)
source = token->val.source;
continue;
}
escape_it = (token->type == CPP_STRING || token->type == CPP_WSTRING
|| token->type == CPP_CHAR || token->type == CPP_WCHAR);
if (escape_it)
/* Worst case is each char is octal. */
len *= 4;
len += 2; /* Room for initial space and final NUL. */
dest = &start[total_len];
if (dest + len > POOL_LIMIT (pool))
{
_cpp_next_chunk (pool, len, (unsigned char **) &start);
dest = &start[total_len];
}
/* Leading white space? */
if (total_len)
{
if (source == NULL)
source = token;
if (source->flags & PREV_WHITE)
*dest++ = ' ';
}
source = NULL;
if (escape_it)
{
unsigned char *buf = (unsigned char *) xmalloc (len);
len = cpp_spell_token (pfile, token, buf) - buf;
dest = quote_string (dest, buf, len);
free (buf);
}
else
dest = cpp_spell_token (pfile, token, dest);
total_len = dest - start;
Neil Booth
committed
if (token->type == CPP_OTHER && token->val.c == '\\')
backslash_count++;
else
backslash_count = 0;
}
/* Ignore the final \ of invalid string literals. */
if (backslash_count & 1)
{
cpp_warning (pfile, "invalid string literal, ignoring final '\\'");
total_len--;
}
/* Commit the memory, including NUL, and return the token. */
POOL_COMMIT (pool, total_len + 1);
return new_string_token (pfile, start, total_len);
/* Try to paste two tokens. On success, return non-zero. In any
case, PLHS is updated to point to the pasted token, which is
guaranteed to not have the PASTE_LEFT flag set. */
static bool
paste_tokens (pfile, plhs, rhs)
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
unsigned char *buf, *end;
const cpp_token *lhs;
unsigned int len;
bool valid;
lhs = *plhs;
len = cpp_token_len (lhs) + cpp_token_len (rhs) + 1;
buf = (unsigned char *) alloca (len);
end = cpp_spell_token (pfile, lhs, buf);
/* Avoid comment headers, since they are still processed in stage 3.
It is simpler to insert a space here, rather than modifying the
lexer to ignore comments in some circumstances. Simply returning
false doesn't work, since we want to clear the PASTE_LEFT flag. */
if (lhs->type == CPP_DIV
&& (rhs->type == CPP_MULT || rhs->type == CPP_DIV))
*end++ = ' ';
end = cpp_spell_token (pfile, rhs, end);
cpp_push_buffer (pfile, buf, end - buf, /* from_stage3 */ true, 1);
/* Tweak the column number the lexer will report. */
pfile->buffer->col_adjust = pfile->cur_token[-1].col - 1;
/* We don't want a leading # to be interpreted as a directive. */
pfile->buffer->saved_flags = 0;
/* Set pfile->cur_token as required by _cpp_lex_direct. */
pfile->cur_token = _cpp_temp_token (pfile);
*plhs = _cpp_lex_direct (pfile);
valid = (pfile->buffer->cur == pfile->buffer->rlimit
&& pfile->buffer->read_ahead == EOF);
_cpp_pop_buffer (pfile);
return valid;
/* Handles an arbitrarily long sequence of ## operators. This
implementation is left-associative, non-recursive, and finishes a
paste before handling succeeding ones. If the paste fails, we back
up a token to just after the ## operator, with the effect that it
appears in the output stream normally. */
static void
paste_all_tokens (pfile, lhs)
cpp_reader *pfile;
const cpp_token *rhs;
cpp_context *context = pfile->context;
do
{
/* Take the token directly from the current context. We can do
this, because we are in the replacement list of either an
object-like macro, or a function-like macro with arguments
inserted. In either case, the constraints to #define
guarantee we have at least one more token. */
if (context->direct_p)
rhs = context->first.token++;
else
rhs = *context->first.ptoken++;
if (rhs->type == CPP_PADDING)
abort ();
/* Mandatory warning for all apart from assembler. */
if (CPP_OPTION (pfile, lang) != CLK_ASM)
cpp_warning (pfile,
"pasting \"%s\" and \"%s\" does not give a valid preprocessing token",
cpp_token_as_text (pfile, lhs),
cpp_token_as_text (pfile, rhs));
/* Put the resulting token in its own context. */
push_token_context (pfile, NULL, lhs, 1);
/* Reads and returns the arguments to a function-like macro invocation.
Assumes the opening parenthesis has been processed. If there is an
error, emits an appropriate diagnostic and returns NULL. */
static _cpp_buff *
collect_args (pfile, node)
_cpp_buff *buff, *base_buff;
cpp_macro *macro;
macro_arg *args, *arg;
const cpp_token *token;
unsigned int argc;
bool error = false;
macro = node->value.macro;
if (macro->paramc)
argc = macro->paramc;
else
argc = 1;
buff = _cpp_get_buff (pfile, argc * (50 * sizeof (cpp_token *)
+ sizeof (macro_arg)));
base_buff = buff;
args = (macro_arg *) buff->base;
memset (args, 0, argc * sizeof (macro_arg));
buff->cur = (char *) &args[argc];
arg = args, argc = 0;
/* Collect the tokens making up each argument. We don't yet know
how many arguments have been supplied, whether too many or too
few. Hence the slightly bizarre usage of "argc" and "arg". */
do
unsigned int paren_depth = 0;
unsigned int ntokens = 0;
argc++;
arg->first = (const cpp_token **) buff->cur;
for (;;)
{
/* Require space for 2 new tokens (including a CPP_EOF). */
if ((char *) &arg->first[ntokens + 2] > buff->limit)
{
buff = _cpp_extend_buff (pfile, buff,
1000 * sizeof (cpp_token *));
arg->first = (const cpp_token **) buff->cur;
}
if (token->type == CPP_PADDING)
{
/* Drop leading padding. */
if (ntokens == 0)
continue;
}
else if (token->type == CPP_OPEN_PAREN)
paren_depth++;
else if (token->type == CPP_CLOSE_PAREN)
{
if (paren_depth-- == 0)
break;
}
else if (token->type == CPP_COMMA)
{
/* A comma does not terminate an argument within
parentheses or as part of a variable argument. */
if (paren_depth == 0
&& ! (macro->variadic && argc == macro->paramc))
break;
}
else if (token->type == CPP_EOF
|| (token->type == CPP_HASH && token->flags & BOL))
break;
arg->first[ntokens++] = token;
}
/* Drop trailing padding. */
while (ntokens > 0 && arg->first[ntokens - 1]->type == CPP_PADDING)
ntokens--;
arg->count = ntokens;
arg->first[ntokens] = &pfile->eof;
/* Terminate the argument. Excess arguments loop back and
overwrite the final legitimate argument, before failing. */
if (argc <= macro->paramc)
{
buff->cur = (char *) &arg->first[ntokens + 1];
if (argc != macro->paramc)
arg++;
}
while (token->type != CPP_CLOSE_PAREN
&& token->type != CPP_EOF
&& token->type != CPP_HASH);
if (token->type == CPP_EOF || token->type == CPP_HASH)
bool step_back = false;
/* 6.10.3 paragraph 11: If there are sequences of preprocessing
tokens within the list of arguments that would otherwise act
as preprocessing directives, the behavior is undefined.
This implementation will report a hard error, terminate the
macro invocation, and proceed to process the directive. */
if (token->type == CPP_HASH)
{
cpp_error (pfile,
"directives may not be used inside a macro argument");
step_back = true;
}
else
/* We still need the CPP_EOF to end directives, and to end
pre-expansion of a macro argument. */
step_back = (pfile->context->prev || pfile->state.in_directive);
if (step_back)
_cpp_backup_tokens (pfile, 1);
cpp_error (pfile, "unterminated argument list invoking macro \"%s\"",
}
else if (argc < macro->paramc)
{
/* As an extension, a rest argument is allowed to not appear in
the invocation at all.
e.g. #define debug(format, args...) something
debug("string");
This is exactly the same as if there had been an empty rest
argument - debug("string", ). */
if (argc + 1 == macro->paramc && macro->variadic)
Neil Booth
committed
if (CPP_PEDANTIC (pfile) && ! macro->syshdr)
cpp_pedwarn (pfile, "ISO C99 requires rest arguments to be used");
}
else
{
cpp_error (pfile,
"macro \"%s\" requires %u arguments, but only %u given",
NODE_NAME (node), macro->paramc, argc);
/* Empty argument to a macro taking no arguments is OK. */
{
cpp_error (pfile,
"macro \"%s\" passed %u arguments, but takes just %u",
NODE_NAME (node), argc, macro->paramc);
if (!error)
return base_buff;
_cpp_release_buff (pfile, base_buff);
return NULL;
pfile->state.parsing_args = 1;
do
maybe_paren = cpp_get_token (pfile);
while (maybe_paren->type == CPP_PADDING);
Neil Booth
committed
pfile->state.parsing_args = 2;
buff = collect_args (pfile, node);
else
{
_cpp_backup_tokens (pfile, 1);
if (CPP_WTRADITIONAL (pfile) && ! node->value.macro->syshdr)
cpp_warning (pfile,
"function-like macro \"%s\" must be used with arguments in traditional C",
NODE_NAME (node));
}
replace_args (pfile, node->value.macro, (macro_arg *) buff->base);
_cpp_release_buff (pfile, buff);
}
/* Push the context of a macro onto the context stack. TOKEN is the
macro name. If we can successfully start expanding the macro,
TOKEN is replaced with the first token of the expansion, and we
return non-zero. */
Neil Booth
committed
enter_macro_context (pfile, node)
Neil Booth
committed
cpp_hashnode *node;
if (node->flags & NODE_BUILTIN)
push_token_context (pfile, NULL, builtin_macro (pfile, node), 1);
else
if (macro->fun_like && !funlike_invocation_p (pfile, node))
/* Disable the macro within its expansion. */
macro->disabled = 1;
if (macro->paramc == 0)
push_token_context (pfile, macro, macro->expansion, macro->count);
/* Take the expansion of a function-like MACRO, replacing parameters
with the actual arguments. Each argument is macro-expanded before
replacement, unless operated upon by the # or ## operators. */
cpp_reader *pfile;
cpp_macro *macro;
macro_arg *args;
{
unsigned int i, total;
const cpp_token *src, *limit;
/* First, fully macro-expand arguments, calculating the number of
tokens in the final expansion as we go. The ordering of the if
statements below is subtle; we must handle stringification before
pasting. */
total = macro->count;
limit = macro->expansion + macro->count;
for (src = macro->expansion; src < limit; src++)
/* Leading and trailing padding tokens. */
total += 2;
/* We have an argument. If it is not being stringified or
pasted it is macro-replaced before insertion. */
Neil Booth
committed
arg = &args[src->val.arg_no - 1];
if (src->flags & STRINGIFY_ARG)
{
if (!arg->stringified)
arg->stringified = stringify_arg (pfile, arg);
}
else if ((src->flags & PASTE_LEFT)
|| (src > macro->expansion && (src[-1].flags & PASTE_LEFT)))
total += arg->count - 1;
else
{
if (!arg->expanded)
/* Now allocate space for the expansion, copy the tokens and replace
the arguments. */
buff = _cpp_get_buff (pfile, total * sizeof (cpp_token *));
first = (const cpp_token **) buff->base;
{
unsigned int count;
const cpp_token **from, **paste_flag;
if (src->type != CPP_MACRO_ARG)
{
*dest++ = src;
continue;
}
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
paste_flag = 0;
arg = &args[src->val.arg_no - 1];
if (src->flags & STRINGIFY_ARG)
count = 1, from = &arg->stringified;
else if (src->flags & PASTE_LEFT)
count = arg->count, from = arg->first;
else if (src != macro->expansion && (src[-1].flags & PASTE_LEFT))
{
count = arg->count, from = arg->first;
if (dest != first)
{
/* GCC has special semantics for , ## b where b is a
varargs parameter: the comma disappears if b was
given no actual arguments (not merely if b is an
empty argument); otherwise the paste flag is removed. */
if (dest[-1]->type == CPP_COMMA
&& macro->variadic
&& src->val.arg_no == macro->paramc)
{
if (count == 0)
dest--;
else
paste_flag = dest - 1;
}
/* Remove the paste flag if the RHS is a placemarker. */
else if (count == 0)
paste_flag = dest - 1;
}
}
else
count = arg->expanded_count, from = arg->expanded;
/* Padding on the left of an argument (unless RHS of ##). */
if (!pfile->state.in_directive
&& src != macro->expansion && !(src[-1].flags & PASTE_LEFT))
*dest++ = padding_token (pfile, src);
if (count)
{
memcpy (dest, from, count * sizeof (cpp_token *));
dest += count;
/* With a non-empty argument on the LHS of ##, the last
token should be flagged PASTE_LEFT. */
if (src->flags & PASTE_LEFT)
paste_flag = dest - 1;
}
/* Avoid paste on RHS (even case count == 0). */
if (!pfile->state.in_directive && !(src->flags & PASTE_LEFT))
*dest++ = &pfile->avoid_paste;
/* Add a new paste flag, or remove an unwanted one. */
if (paste_flag)
{
cpp_token *token = _cpp_temp_token (pfile);
token->type = (*paste_flag)->type;
token->val.str = (*paste_flag)->val.str;
if (src->flags & PASTE_LEFT)
token->flags = (*paste_flag)->flags | PASTE_LEFT;
else
token->flags = (*paste_flag)->flags & ~PASTE_LEFT;
*paste_flag = token;
}
}
/* Free the expanded arguments. */
for (i = 0; i < macro->paramc; i++)
if (args[i].expanded)
free (args[i].expanded);
push_ptoken_context (pfile, macro, buff, first, dest - first);
}
/* Return a special padding token, with padding inherited from SOURCE. */
static const cpp_token *
padding_token (pfile, source)
cpp_reader *pfile;
const cpp_token *source;
{
cpp_token *result = _cpp_temp_token (pfile);
result->type = CPP_PADDING;
result->val.source = source;
result->flags = 0;
return result;
}
/* Move to the next context. Create one if there is none. */
static cpp_context *
next_context (pfile)
cpp_reader *pfile;
{
cpp_context *result = pfile->context->next;
if (result == 0)
result = xnew (cpp_context);
result->prev = pfile->context;
result->next = 0;
pfile->context->next = result;
pfile->context = result;
return result;
/* Push a list of pointers to tokens. */
static void
push_ptoken_context (pfile, macro, buff, first, count)
const cpp_token **first;
unsigned int count;
context->direct_p = false;
context->macro = macro;
context->buff = buff;
context->first.ptoken = first;
context->last.ptoken = first + count;
}
/* Push a list of tokens. */
static void
push_token_context (pfile, macro, first, count)
cpp_reader *pfile;
cpp_macro *macro;
const cpp_token *first;
unsigned int count;
{
cpp_context *context = next_context (pfile);
context->direct_p = true;
context->macro = macro;
context->buff = NULL;
context->first.token = first;
context->last.token = first + count;
}
static void
expand_arg (pfile, arg)
cpp_reader *pfile;
macro_arg *arg;
{
unsigned int capacity;
if (arg->count == 0)
return;
capacity = 256;
arg->expanded = (const cpp_token **)
xmalloc (capacity * sizeof (cpp_token *));
push_ptoken_context (pfile, NULL, NULL, arg->first, arg->count + 1);
const cpp_token *token;
if (arg->expanded_count + 1 >= capacity)
arg->expanded = (const cpp_token **)
xrealloc (arg->expanded, capacity * sizeof (cpp_token *));
if (token->type == CPP_EOF)
break;
arg->expanded[arg->expanded_count++] = token;
}
_cpp_pop_context (pfile);
}
void
_cpp_pop_context (pfile)
cpp_reader *pfile;
{
cpp_context *context = pfile->context;
/* Re-enable a macro when leaving its expansion. */
if (context->macro)
context->macro->disabled = 0;
if (context->buff)
_cpp_release_buff (pfile, context->buff);
pfile->context = context->prev;
/* Eternal routine to get a token. Also used nearly everywhere
internally, except for places where we know we can safely call
the lexer directly, such as lexing a directive name.
Macro expansions and directives are transparently handled,
including entering included files. Thus tokens are post-macro
expansion, and after any intervening directives. External callers
see CPP_EOF only at EOF. Internal callers also see it when meeting
a directive inside a macro call, when at the end of a directive and
state.in_directive is still 1, and at the end of argument
pre-expansion. */
const cpp_token *
cpp_get_token (pfile)
Neil Booth
committed
for (;;)
cpp_context *context = pfile->context;
/* Context->prev == 0 <=> base context. */
result = _cpp_lex_token (pfile);
else if (context->first.token != context->last.token)
Neil Booth
committed
{
if (context->direct_p)
result = context->first.token++;
else
result = *context->first.ptoken++;
if (result->flags & PASTE_LEFT)
paste_all_tokens (pfile, result);
if (pfile->state.in_directive)
continue;
return padding_token (pfile, result);
Neil Booth
committed
}
if (pfile->state.in_directive)
continue;
return &pfile->avoid_paste;
/* Handle macros and the _Pragma operator. */
if (node->type == NT_MACRO && !(result->flags & NO_EXPAND))
/* Macros invalidate controlling macros. */
pfile->mi_valid = false;
if (!(node->flags & NODE_BUILTIN) && node->value.macro->disabled)
/* Flag this token as always unexpandable. */
cpp_token *t = _cpp_temp_token (pfile);
t->type = result->type;
t->flags = result->flags | NO_EXPAND;
t->val.str = result->val.str;
result = t;
else if (!pfile->state.prevent_expansion
&& enter_macro_context (pfile, node))
if (pfile->state.in_directive)
continue;
return padding_token (pfile, result);