summaryrefslogtreecommitdiffstats
path: root/gcc/cppmacro.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/cppmacro.c')
-rw-r--r--gcc/cppmacro.c151
1 files changed, 87 insertions, 64 deletions
diff --git a/gcc/cppmacro.c b/gcc/cppmacro.c
index 20149ec9b33..99cdc19e007 100644
--- a/gcc/cppmacro.c
+++ b/gcc/cppmacro.c
@@ -54,7 +54,6 @@ static const cpp_token *padding_token
static void expand_arg PARAMS ((cpp_reader *, macro_arg *));
static const cpp_token *new_string_token PARAMS ((cpp_reader *, uchar *,
unsigned int));
-static const cpp_token *new_number_token PARAMS ((cpp_reader *, unsigned 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 **,
@@ -93,24 +92,6 @@ new_string_token (pfile, text, len)
return token;
}
-/* Allocates and returns a CPP_NUMBER token evaluating to NUMBER. */
-static const cpp_token *
-new_number_token (pfile, number)
- cpp_reader *pfile;
- unsigned int number;
-{
- cpp_token *token = _cpp_temp_token (pfile);
- /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
- unsigned char *buf = _cpp_unaligned_alloc (pfile, 21);
-
- sprintf ((char *) buf, "%u", number);
- token->type = CPP_NUMBER;
- token->val.str.text = buf;
- token->val.str.len = ustrlen (buf);
- token->flags = 0;
- return token;
-}
-
static const char * const monthnames[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
@@ -121,19 +102,20 @@ static const char * const monthnames[] =
on the context stack. Also handles _Pragma, for which no new token
is created. Returns 1 if it generates a new token context, 0 to
return the token to the caller. */
-static int
-builtin_macro (pfile, node)
+const uchar *
+_cpp_builtin_macro_text (pfile, node)
cpp_reader *pfile;
cpp_hashnode *node;
{
- const cpp_token *result;
+ const uchar *result = NULL;
+ unsigned int number = 1;
switch (node->value.builtin)
{
default:
cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
NODE_NAME (node));
- return 0;
+ break;
case BT_FILE:
case BT_BASE_FILE:
@@ -149,10 +131,12 @@ builtin_macro (pfile, node)
name = map->to_file;
len = strlen (name);
- buf = _cpp_unaligned_alloc (pfile, len * 4 + 1);
- len = cpp_quote_string (buf, (const unsigned char *) name, len) - buf;
-
- result = new_string_token (pfile, buf, len);
+ buf = _cpp_unaligned_alloc (pfile, len * 4 + 3);
+ result = buf;
+ *buf = '"';
+ buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
+ *buf++ = '"';
+ *buf = '\0';
}
break;
@@ -160,16 +144,18 @@ builtin_macro (pfile, node)
/* The line map depth counts the primary source as level 1, but
historically __INCLUDE_DEPTH__ has called the primary source
level 0. */
- result = new_number_token (pfile, pfile->line_maps.depth - 1);
+ number = pfile->line_maps.depth - 1;
break;
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. */
- result = new_number_token (pfile,
- SOURCE_LINE (pfile->map,
- pfile->cur_token[-1].line));
+ if (CPP_OPTION (pfile, traditional))
+ number = pfile->line;
+ else
+ number = pfile->cur_token[-1].line;
+ number = SOURCE_LINE (pfile->map, number);
break;
/* __STDC__ has the value 1 under normal circumstances.
@@ -179,23 +165,20 @@ builtin_macro (pfile, node)
value 0. */
case BT_STDC:
{
- int stdc;
enum c_lang lang = CPP_OPTION (pfile, lang);
if (CPP_IN_SYSTEM_HEADER (pfile)
&& CPP_OPTION (pfile, stdc_0_in_system_headers)
&& !(lang == CLK_STDC89 || lang == CLK_STDC94
|| lang == CLK_STDC99)) /* || lang == CLK_CXX98 ? */
- stdc = 0;
+ number = 0;
else
- stdc = 1;
-
- result = new_number_token (pfile, stdc);
+ number = 1;
}
break;
case BT_DATE:
case BT_TIME:
- if (pfile->date.type == CPP_EOF)
+ if (pfile->date == NULL)
{
/* Allocate __DATE__ and __TIME__ strings from permanent
storage. We only do this once, and don't generate them
@@ -204,30 +187,46 @@ builtin_macro (pfile, node)
time_t tt = time (NULL);
struct tm *tb = localtime (&tt);
- pfile->date.val.str.text =
- _cpp_unaligned_alloc (pfile, 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",
+ pfile->date = _cpp_unaligned_alloc (pfile,
+ sizeof ("\"Oct 11 1347\""));
+ sprintf ((char *) pfile->date, "\"%s %2d %4d\"",
monthnames[tb->tm_mon], tb->tm_mday, tb->tm_year + 1900);
- pfile->time.val.str.text =
- _cpp_unaligned_alloc (pfile, 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",
+ pfile->time = _cpp_unaligned_alloc (pfile, sizeof ("\"12:34:56\""));
+ sprintf ((char *) pfile->time, "\"%02d:%02d:%02d\"",
tb->tm_hour, tb->tm_min, tb->tm_sec);
}
if (node->value.builtin == BT_DATE)
- result = &pfile->date;
+ result = pfile->date;
else
- result = &pfile->time;
+ result = pfile->time;
break;
+ }
+
+ if (result == NULL)
+ {
+ /* 21 bytes holds all NUL-terminated unsigned 64-bit numbers. */
+ result = _cpp_unaligned_alloc (pfile, 21);
+ sprintf ((char *) result, "%u", number);
+ }
+
+ return result;
+}
+
+/* Convert builtin macros like __FILE__ to a token and push it on the
+ context stack. Also handles _Pragma, for which no new token is
+ created. Returns 1 if it generates a new token context, 0 to
+ return the token to the caller. */
+static int
+builtin_macro (pfile, node)
+ cpp_reader *pfile;
+ cpp_hashnode *node;
+{
+ const uchar *buf;
- case BT_PRAGMA:
+ if (node->value.builtin == BT_PRAGMA)
+ {
/* Don't interpret _Pragma within directives. The standard is
not clear on this, but to me this makes most sense. */
if (pfile->state.in_directive)
@@ -237,7 +236,24 @@ builtin_macro (pfile, node)
return 1;
}
- push_token_context (pfile, NULL, result, 1);
+ buf = _cpp_builtin_macro_text (pfile, node);
+
+ cpp_push_buffer (pfile, buf, ustrlen (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);
+ push_token_context (pfile, NULL, _cpp_lex_direct (pfile), 1);
+ if (pfile->buffer->cur != pfile->buffer->rlimit)
+ cpp_error (pfile, DL_ICE, "invalid built-in macro \"%s\"",
+ NODE_NAME (node));
+ _cpp_pop_buffer (pfile);
+
return 1;
}
@@ -1598,18 +1614,23 @@ cpp_macro_definition (pfile, node)
len += NODE_LEN (macro->params[i]) + 1; /* "," */
}
- for (i = 0; i < macro->count; i++)
+ if (CPP_OPTION (pfile, traditional))
+ len += _cpp_replacement_text_len (macro);
+ else
{
- cpp_token *token = &macro->exp.tokens[i];
+ for (i = 0; i < macro->count; i++)
+ {
+ cpp_token *token = &macro->exp.tokens[i];
- if (token->type == CPP_MACRO_ARG)
- len += NODE_LEN (macro->params[token->val.arg_no - 1]);
- else
- len += cpp_token_len (token); /* Includes room for ' '. */
- if (token->flags & STRINGIFY_ARG)
- len++; /* "#" */
- if (token->flags & PASTE_LEFT)
- len += 3; /* " ##" */
+ if (token->type == CPP_MACRO_ARG)
+ len += NODE_LEN (macro->params[token->val.arg_no - 1]);
+ else
+ len += cpp_token_len (token); /* Includes room for ' '. */
+ if (token->flags & STRINGIFY_ARG)
+ len++; /* "#" */
+ if (token->flags & PASTE_LEFT)
+ len += 3; /* " ##" */
+ }
}
if (len > pfile->macro_buffer_len)
@@ -1652,8 +1673,10 @@ cpp_macro_definition (pfile, node)
definition is the empty string. */
*buffer++ = ' ';
+ if (CPP_OPTION (pfile, traditional))
+ buffer = _cpp_copy_replacement_text (macro, buffer);
+ else if (macro->count)
/* Expansion tokens. */
- if (macro->count)
{
for (i = 0; i < macro->count; i++)
{
OpenPOWER on IntegriCloud