summaryrefslogtreecommitdiffstats
path: root/gcc/cppmacro.c
diff options
context:
space:
mode:
authorneil <neil@138bc75d-0d04-0410-961f-82ee72b054a4>2002-06-19 05:40:08 +0000
committerneil <neil@138bc75d-0d04-0410-961f-82ee72b054a4>2002-06-19 05:40:08 +0000
commit9c343313ea14c543785f41b5f43e10e530f87d07 (patch)
tree3c3d821dfd3b04ed9aaf9b7fbdf7297ca8b32a1f /gcc/cppmacro.c
parent770eb97707cc9f2454a4d2102b79b61ef5bba5c8 (diff)
downloadppe42-gcc-9c343313ea14c543785f41b5f43e10e530f87d07.tar.gz
ppe42-gcc-9c343313ea14c543785f41b5f43e10e530f87d07.zip
* cpphash.h (struct cpp_reader): Make date and time strings.
(_cpp_builtin_macro_text, _cpp_copy_replacement_text, _cpp_replacement_text_len): New. * cppinit.c (cpp_create_reader): Update. (init_builtins): Register appropriate builtins for -traditional-cpp. * cppmacro.c (new_number_token): Remove. (_cpp_builtin_macro_text): New. (builtin_macro): Use it. (cpp_macro_definition): Update to handle traditional macros. * cppmain.c (cb_line_change): Don't do column positioning for traditional output. * cpptrad.c (enum ls): Rename ls_fun_macro to ls_fun_open. New state ls_fun_close. (skip_whitespace): Fix. (maybe_start_funlike): Don't set state.parsing_args. (scan_out_logical_line): Remove duplicate error. Use lex_state rather than state.parsing_args. (push_replacement_text): Handle builtins. (_cpp_replacement_text_len, _cpp_copy_replacement_text): New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@54771 138bc75d-0d04-0410-961f-82ee72b054a4
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