Commit 10f04917 by Nathan Sidwell Committed by Nathan Sidwell

[PATCH] Macro body is trailing array

https://gcc.gnu.org/ml/gcc-patches/2018-08/msg01037.html
	* include/cpplib.h (enum cpp_macro_kind): New.
	(struct cpp_macro): Make body trailing array.  Add kind field,
	delete traditional flag.
	* internal.h (_cpp_new_macro): Declare.
	(_cpp_reserve_room): New inline.
	(_cpp_commit_buf): Declare.
	(_cpp_create_trad_definition): Return new macro.
	* lex.c (_cpp_commit_buff): New.
	* macro.c (macro_real_token_count): Count backwards.
	(replace_args): Pointer equality not orderedness.
	(_cpp_save_parameter): Use _cpp_reserve_room.
	(alloc_expansion_token): Delete.
	(lex_expansion_token): Return macro pointer.  Use _cpp_reserve_room.
	(create_iso_definition): Allocate macro itself.  Adjust for
	different allocation ordering.
	(_cpp_new_macro): New.
	(_cpp_create_definition): Adjust for API changes.
	* traditional.c (push_replacement_text): Don't set traditional
	flag.
	(save_replacement_text): Likewise.
	(_cpp_create_trad_definition): Allocate macro itself, Adjust for
	different allocation ordering.

From-SVN: r263622
parent c5d725c0
2018-08-17 Nathan Sidwell <nathan@acm.org>
* c-ada-spec.c (macro_length, dump_ada_macros): Constify.
* c-ada-spec.c: Don't #include "cpp-id-data.h"
* c-cppbuiltin.c: Likewise.
......
......@@ -88,7 +88,7 @@ macro_length (const cpp_macro *macro, int *supported, int *buffer_len,
for (j = 0; j < macro->count; j++)
{
cpp_token *token = &macro->exp.tokens[j];
const cpp_token *token = &macro->exp.tokens[j];
if (token->flags & PREV_WHITE)
(*buffer_len)++;
......@@ -274,7 +274,7 @@ dump_ada_macros (pretty_printer *pp, const char* file)
for (i = 0; supported && i < macro->count; i++)
{
cpp_token *token = &macro->exp.tokens[i];
const cpp_token *token = &macro->exp.tokens[i];
int is_one = 0;
if (token->flags & PREV_WHITE)
......
2018-08-17 Nathan Sidwell <nathan@acm.org>
* include/cpplib.h (enum cpp_macro_kind): New.
(struct cpp_macro): Make body trailing array. Add kind field,
delete traditional flag.
* internal.h (_cpp_new_macro): Declare.
(_cpp_reserve_room): New inline.
(_cpp_commit_buf): Declare.
(_cpp_create_trad_definition): Return new macro.
* lex.c (_cpp_commit_buff): New.
* macro.c (macro_real_token_count): Count backwards.
(replace_args): Pointer equality not orderedness.
(_cpp_save_parameter): Use _cpp_reserve_room.
(alloc_expansion_token): Delete.
(lex_expansion_token): Return macro pointer. Use _cpp_reserve_room.
(create_iso_definition): Allocate macro itself. Adjust for
different allocation ordering.
(_cpp_new_macro): New.
(_cpp_create_definition): Adjust for API changes.
* traditional.c (push_replacement_text): Don't set traditional
flag.
(save_replacement_text): Likewise.
(_cpp_create_trad_definition): Allocate macro itself, Adjust for
different allocation ordering.
* cpp-id-data.h (uchar, UC): Move to internal.h
(struct cpp_macro): Move to cpplib.h.
* internal.h (uchar, UC): From cpp-id-data.h.
......
......@@ -671,6 +671,12 @@ struct cpp_dir
dev_t dev;
};
/* The kind of the cpp_macro. */
enum cpp_macro_kind {
cmk_macro, /* An ISO macro (token expansion). */
cmk_traditional, /* A traditional macro (text expansion). */
};
/* Each macro definition is recorded in a cpp_macro structure.
Variadic macros cannot occur with traditional cpp. */
struct GTY(()) cpp_macro {
......@@ -683,15 +689,6 @@ struct GTY(()) cpp_macro {
length ("%h.paramc")))
params;
/* Replacement tokens (ISO) or replacement text (traditional). See
comment at top of cpptrad.c for how traditional function-like
macros are encoded. */
union cpp_macro_u
{
cpp_token * GTY ((tag ("0"), length ("%0.count"))) tokens;
const unsigned char * GTY ((tag ("1"))) text;
} GTY ((desc ("%1.traditional"))) exp;
/* Definition line number. */
source_location line;
......@@ -701,6 +698,9 @@ struct GTY(()) cpp_macro {
/* Number of parameters. */
unsigned short paramc;
/* The kind of this macro (ISO, trad or assert) */
unsigned kind : 2;
/* If a function-like macro. */
unsigned int fun_like : 1;
......@@ -713,13 +713,23 @@ struct GTY(()) cpp_macro {
/* Nonzero if it has been expanded or had its existence tested. */
unsigned int used : 1;
/* Indicate which field of 'exp' is in use. */
unsigned int traditional : 1;
/* Indicate whether the tokens include extra CPP_PASTE tokens at the
end to track invalid redefinitions with consecutive CPP_PASTE
tokens. */
unsigned int extra_tokens : 1;
/* 1 bits spare (32-bit). 33 on 64-bit target. */
union cpp_exp_u
{
/* Trailing array of replacement tokens (ISO), or assertion body value. */
cpp_token GTY ((tag ("false"), length ("%1.count"))) tokens[1];
/* Pointer to replacement text (traditional). See comment at top
of cpptrad.c for how traditional function-like macros are
encoded. */
const unsigned char *GTY ((tag ("true"))) text;
} GTY ((desc ("%1.kind == cmk_traditional"))) exp;
};
/* The structure of a node in the hash table. The hash table has
......
......@@ -633,6 +633,7 @@ inline void _cpp_maybe_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node)
if (!(node->flags & NODE_USED))
_cpp_notify_macro_use (pfile, node);
}
extern cpp_macro *_cpp_new_macro (cpp_reader *, cpp_macro_kind, void *);
extern void _cpp_free_definition (cpp_hashnode *);
extern bool _cpp_create_definition (cpp_reader *, cpp_hashnode *);
extern void _cpp_pop_context (cpp_reader *);
......@@ -697,6 +698,14 @@ extern void _cpp_init_tokenrun (tokenrun *, unsigned int);
extern cpp_hashnode *_cpp_lex_identifier (cpp_reader *, const char *);
extern int _cpp_remaining_tokens_num_in_context (cpp_context *);
extern void _cpp_init_lexer (void);
static inline void *_cpp_reserve_room (cpp_reader *pfile, size_t have,
size_t extra)
{
if (BUFF_ROOM (pfile->a_buff) < (have + extra))
_cpp_extend_buff (pfile, &pfile->a_buff, extra);
return BUFF_FRONT (pfile->a_buff);
}
extern void *_cpp_commit_buff (cpp_reader *pfile, size_t size);
/* In init.c. */
extern void _cpp_maybe_push_include_file (cpp_reader *);
......@@ -733,7 +742,7 @@ extern bool _cpp_read_logical_line_trad (cpp_reader *);
extern void _cpp_overlay_buffer (cpp_reader *pfile, const unsigned char *,
size_t);
extern void _cpp_remove_overlay (cpp_reader *);
extern bool _cpp_create_trad_definition (cpp_reader *, cpp_macro *);
extern cpp_macro *_cpp_create_trad_definition (cpp_reader *);
extern bool _cpp_expansions_different_trad (const cpp_macro *,
const cpp_macro *);
extern unsigned char *_cpp_copy_replacement_text (const cpp_macro *,
......
......@@ -3725,6 +3725,25 @@ _cpp_aligned_alloc (cpp_reader *pfile, size_t len)
return result;
}
/* Commit or allocate storage from a buffer. */
void *
_cpp_commit_buff (cpp_reader *pfile, size_t size)
{
void *ptr = BUFF_FRONT (pfile->a_buff);
if (pfile->hash_table->alloc_subobject)
{
void *copy = pfile->hash_table->alloc_subobject (size);
memcpy (copy, ptr, size);
ptr = copy;
}
else
BUFF_FRONT (pfile->a_buff) += size;
return ptr;
}
/* Say which field of TOK is in use. */
enum cpp_token_fld_kind
......
......@@ -853,7 +853,6 @@ push_replacement_text (cpp_reader *pfile, cpp_hashnode *node)
cpp_macro *macro = node->value.macro;
macro->used = 1;
text = macro->exp.text;
macro->traditional = 1;
len = macro->count;
}
......@@ -1143,7 +1142,6 @@ save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
memcpy (exp, pfile->out.base, len);
exp[len] = '\n';
macro->exp.text = exp;
macro->traditional = 1;
macro->count = len;
}
else
......@@ -1159,7 +1157,6 @@ save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
exp = BUFF_FRONT (pfile->a_buff);
block = (struct block *) (exp + macro->count);
macro->exp.text = exp;
macro->traditional = 1;
/* Write out the block information. */
block->text_len = len;
......@@ -1179,13 +1176,15 @@ save_replacement_text (cpp_reader *pfile, cpp_macro *macro,
/* Analyze and save the replacement text of a macro. Returns true on
success. */
bool
_cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
cpp_macro *
_cpp_create_trad_definition (cpp_reader *pfile)
{
const uchar *cur;
uchar *limit;
cpp_context *context = pfile->context;
unsigned nparms = 0;
int fun_like = 0;
cpp_hashnode **params = NULL;
/* The context has not been set up for command line defines, and CUR
has not been updated for the macro name for in-file defines. */
......@@ -1197,21 +1196,23 @@ _cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
/* Is this a function-like macro? */
if (* CUR (context) == '(')
{
bool ok = scan_parameters (pfile, &nparms);
macro->paramc = nparms;
fun_like = +1;
if (scan_parameters (pfile, &nparms))
params = (cpp_hashnode **)_cpp_commit_buff
(pfile, sizeof (cpp_hashnode *) * nparms);
else
fun_like = -1;
}
/* Remember the params so we can clear NODE_MACRO_ARG flags. */
macro->params = (cpp_hashnode **) BUFF_FRONT (pfile->a_buff);
cpp_macro *macro = NULL;
/* Setting macro to NULL indicates an error occurred, and
prevents unnecessary work in _cpp_scan_out_logical_line. */
if (!ok)
macro = NULL;
else
{
BUFF_FRONT (pfile->a_buff) = (uchar *) &macro->params[macro->paramc];
macro->fun_like = 1;
}
if (fun_like >= 0)
{
macro = _cpp_new_macro (pfile, cmk_traditional,
_cpp_aligned_alloc (pfile, sizeof (cpp_macro)));
macro->params = params;
macro->paramc = nparms;
macro->fun_like = fun_like != 0;
}
/* Skip leading whitespace in the replacement text. */
......@@ -1225,18 +1226,18 @@ _cpp_create_trad_definition (cpp_reader *pfile, cpp_macro *macro)
_cpp_unsave_parameters (pfile, nparms);
if (!macro)
return false;
/* Skip trailing white space. */
cur = pfile->out.base;
limit = pfile->out.cur;
while (limit > cur && is_space (limit[-1]))
limit--;
pfile->out.cur = limit;
save_replacement_text (pfile, macro, 0);
if (macro)
{
/* Skip trailing white space. */
cur = pfile->out.base;
limit = pfile->out.cur;
while (limit > cur && is_space (limit[-1]))
limit--;
pfile->out.cur = limit;
save_replacement_text (pfile, macro, 0);
}
return true;
return macro;
}
/* Copy SRC of length LEN to DEST, but convert all contiguous
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment