Commit 04ae435f authored by Alexandre Julliard's avatar Alexandre Julliard

wrc: Use the standard memory allocation wrappers in the preprocessor.

parent b69ce347
......@@ -30,6 +30,7 @@
#include <ctype.h>
#include <string.h>
#include "utils.h"
#include "wpp_private.h"
......@@ -270,27 +271,23 @@ preprocessor
| tMACRO res_arg allmargs tMACROEND opt_mtexts tNL {
pp_add_macro($1, macro_args, nmacro_args, $5);
}
| tLINE tSINT tDQSTRING tNL { if($3) pp_writestring("# %d %s\n", $2 , $3); free($3); }
| tGCCLINE tSINT tDQSTRING tNL { if($3) pp_writestring("# %d %s\n", $2 , $3); free($3); }
| tLINE tSINT tDQSTRING tNL { if($3) fprintf(ppy_out, "# %d %s\n", $2 , $3); free($3); }
| tGCCLINE tSINT tDQSTRING tNL { if($3) fprintf(ppy_out, "# %d %s\n", $2 , $3); free($3); }
| tGCCLINE tSINT tDQSTRING tSINT tNL
{ if($3) pp_writestring("# %d %s %d\n", $2, $3, $4); free($3); }
{ if($3) fprintf(ppy_out, "# %d %s %d\n", $2, $3, $4); free($3); }
| tGCCLINE tSINT tDQSTRING tSINT tSINT tNL
{ if($3) pp_writestring("# %d %s %d %d\n", $2 ,$3, $4, $5); free($3); }
{ if($3) fprintf(ppy_out, "# %d %s %d %d\n", $2 ,$3, $4, $5); free($3); }
| tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tNL
{ if($3) pp_writestring("# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free($3); }
{ if($3) fprintf(ppy_out, "# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free($3); }
| tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tSINT tNL
{ if($3) pp_writestring("# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free($3); }
{ if($3) fprintf(ppy_out, "# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free($3); }
| tGCCLINE tNL /* The null-token */
| tERROR opt_text tNL { ppy_error("#error directive: '%s'", $2); free($2); }
| tWARNING opt_text tNL { ppy_warning("#warning directive: '%s'", $2); free($2); }
| tPRAGMA opt_text tNL { pp_writestring("#pragma %s\n", $2 ? $2 : ""); free($2); }
| tPRAGMA opt_text tNL { fprintf(ppy_out, "#pragma %s\n", $2 ? $2 : ""); free($2); }
| tPPIDENT opt_text tNL { if(pp_status.pedantic) ppy_warning("#ident ignored (arg: '%s')", $2); free($2); }
| tRCINCLUDE tRCINCLUDEPATH {
int nl=strlen($2) +3;
char *fn=pp_xmalloc(nl);
sprintf(fn,"\"%s\"",$2);
pp_do_include(fn,1);
free($2);
pp_do_include(strmake( "\"%s\"", $2 ),1);
}
| tRCINCLUDE tDQSTRING {
pp_do_include($2,1);
......@@ -538,8 +535,8 @@ static int boolean(cval_t *v)
static char *add_new_marg(char *str)
{
char *ma;
macro_args = pp_xrealloc(macro_args, (nmacro_args+1) * sizeof(macro_args[0]));
macro_args[nmacro_args++] = ma = pp_xstrdup(str);
macro_args = xrealloc(macro_args, (nmacro_args+1) * sizeof(macro_args[0]));
macro_args[nmacro_args++] = ma = xstrdup(str);
return ma;
}
......@@ -558,7 +555,7 @@ static int marg_index(char *id)
static mtext_t *new_mtext(char *str, int idx, def_exp_t type)
{
mtext_t *mt = pp_xmalloc(sizeof(mtext_t));
mtext_t *mt = xmalloc(sizeof(mtext_t));
if(str == NULL)
mt->subst.argidx = idx;
......@@ -579,7 +576,7 @@ static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
if(tail->type == exp_text && mtp->type == exp_text)
{
tail->subst.text = pp_xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
tail->subst.text = xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
strcat(tail->subst.text, mtp->subst.text);
free(mtp->subst.text);
free(mtp);
......@@ -645,7 +642,7 @@ static char *merge_text(char *s1, char *s2)
{
int l1 = strlen(s1);
int l2 = strlen(s2);
s1 = pp_xrealloc(s1, l1+l2+1);
s1 = xrealloc(s1, l1+l2+1);
memcpy(s1+l1, s2, l2+1);
free(s2);
return s1;
......
......@@ -34,6 +34,7 @@
# include <unistd.h>
#endif
#include "utils.h"
#include "wpp_private.h"
struct pp_status pp_status;
......@@ -57,42 +58,8 @@ struct define
static struct list cmdline_defines = LIST_INIT( cmdline_defines );
void *pp_xmalloc(size_t size)
{
void *res;
assert(size > 0);
res = malloc(size);
if(res == NULL)
{
fprintf( stderr, "Virtual memory exhausted\n" );
exit(1);
}
return res;
}
void *pp_xrealloc(void *p, size_t size)
{
void *res;
assert(size > 0);
res = realloc(p, size);
if(res == NULL)
{
fprintf( stderr, "Virtual memory exhausted\n" );
exit(1);
}
return res;
}
char *pp_xstrdup(const char *str)
{
int len = strlen(str)+1;
return memcpy(pp_xmalloc(len), str, len);
}
char *wpp_lookup(const char *name, int type, const char *parent_name,
char **include_path, int include_path_count)
static char *wpp_lookup(const char *name, int type, const char *parent_name,
char **include_path, int include_path_count)
{
char *cpy;
char *cptr;
......@@ -100,7 +67,7 @@ char *wpp_lookup(const char *name, int type, const char *parent_name,
const char *ccptr;
int i, fd;
cpy = pp_xmalloc(strlen(name)+1);
cpy = xmalloc(strlen(name)+1);
cptr = cpy;
for(ccptr = name; *ccptr; ccptr++)
......@@ -125,7 +92,7 @@ char *wpp_lookup(const char *name, int type, const char *parent_name,
if ((p = strrchr( parent_name, '/' ))) p++;
else p = parent_name;
path = pp_xmalloc( (p - parent_name) + strlen(cpy) + 1 );
path = xmalloc( (p - parent_name) + strlen(cpy) + 1 );
memcpy( path, parent_name, p - parent_name );
strcpy( path + (p - parent_name), cpy );
fd = open( path, O_RDONLY );
......@@ -140,10 +107,7 @@ char *wpp_lookup(const char *name, int type, const char *parent_name,
/* Search -I path */
for(i = 0; i < include_path_count; i++)
{
path = pp_xmalloc(strlen(include_path[i]) + strlen(cpy) + 2);
strcpy(path, include_path[i]);
strcat(path, "/");
strcat(path, cpy);
path = strmake("%s/%s", include_path[i], cpy);
fd = open( path, O_RDONLY );
if (fd != -1)
{
......@@ -195,7 +159,7 @@ static void free_pp_entry( pp_entry_t *ppp, int idx )
}
/* initialize the define state */
void pp_init_define_state(void)
static void pp_init_define_state(void)
{
int i;
......@@ -203,7 +167,7 @@ void pp_init_define_state(void)
}
/* free the current define state */
void pp_free_define_state(void)
static void pp_free_define_state(void)
{
int i;
pp_entry_t *ppp, *ppp2;
......@@ -255,12 +219,12 @@ pp_entry_t *pp_add_define(const char *def, const char *text)
ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", def, ppp->filename, ppp->linenumber);
pp_del_define(def);
}
ppp = pp_xmalloc(sizeof(pp_entry_t));
ppp = xmalloc(sizeof(pp_entry_t));
memset( ppp, 0, sizeof(*ppp) );
ppp->ident = pp_xstrdup(def);
ppp->ident = xstrdup(def);
ppp->type = def_define;
ppp->subst.text = text ? pp_xstrdup(text) : NULL;
ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
ppp->subst.text = text ? xstrdup(text) : NULL;
ppp->filename = xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
list_add_head( &pp_defines[idx], &ppp->entry );
if(ppp->subst.text)
......@@ -295,14 +259,14 @@ pp_entry_t *pp_add_macro(char *id, char *args[], int nargs, mtext_t *exp)
ppy_warning("Redefinition of %s\n\tPrevious definition: %s:%d", id, ppp->filename, ppp->linenumber);
pp_del_define(id);
}
ppp = pp_xmalloc(sizeof(pp_entry_t));
ppp = xmalloc(sizeof(pp_entry_t));
memset( ppp, 0, sizeof(*ppp) );
ppp->ident = id;
ppp->type = def_macro;
ppp->margs = args;
ppp->nargs = nargs;
ppp->subst.mtext= exp;
ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
ppp->filename = xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
list_add_head( &pp_defines[idx], &ppp->entry );
if(pp_status.debug)
......@@ -349,7 +313,7 @@ static int nincludepath = 0;
void wpp_add_include_path(const char *path)
{
char *tok;
char *cpy = pp_xstrdup(path);
char *cpy = xstrdup(path);
tok = strtok(cpy, INCLUDESEPARATOR);
while(tok)
......@@ -358,7 +322,7 @@ void wpp_add_include_path(const char *path)
char *dir;
char *cptr;
dir = pp_xstrdup(tok);
dir = xstrdup(tok);
for(cptr = dir; *cptr; cptr++)
{
/* Convert to forward slash */
......@@ -370,7 +334,7 @@ void wpp_add_include_path(const char *path)
*cptr = '\0';
/* Add to list */
includepath = pp_xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
includepath = xrealloc(includepath, (nincludepath+1) * sizeof(*includepath));
includepath[nincludepath] = dir;
nincludepath++;
}
......@@ -633,14 +597,14 @@ static void wpp_add_define( const char *name, const char *value )
if (!strcmp( def->name, name ))
{
free( def->value );
def->value = pp_xstrdup(value);
def->value = xstrdup(value);
return;
}
}
def = pp_xmalloc( sizeof(*def) );
def->name = pp_xstrdup(name);
def->value = pp_xstrdup(value);
def = xmalloc( sizeof(*def) );
def->name = xstrdup(name);
def->value = xstrdup(value);
list_add_head( &cmdline_defines, &def->entry );
}
......@@ -666,7 +630,7 @@ void wpp_del_define( const char *name )
void wpp_add_cmdline_define( const char *value )
{
char *p;
char *str = pp_xstrdup(value);
char *str = xstrdup(value);
p = strchr( str, '=' );
if (p) *p++ = 0;
......@@ -708,10 +672,10 @@ int wpp_parse( const char *input, FILE *output )
else if (!(pp_status.file = fopen(input, "rt")))
ppy_error("Could not open %s\n", input);
pp_status.input = input ? pp_xstrdup(input) : NULL;
pp_status.input = input ? xstrdup(input) : NULL;
ppy_out = output;
pp_writestring("# 1 \"%s\" 1\n", input ? input : "");
fprintf(ppy_out, "# 1 \"%s\" 1\n", input ? input : "");
ret = ppy_parse();
......
......@@ -155,12 +155,7 @@ typedef struct cval {
void *pp_xmalloc(size_t);
void *pp_xrealloc(void *, size_t);
char *pp_xstrdup(const char *str);
pp_entry_t *pplookup(const char *ident);
void pp_init_define_state(void);
void pp_free_define_state(void);
pp_entry_t *pp_add_define(const char *def, const char *text);
pp_entry_t *pp_add_macro(char *ident, char *args[], int nargs, mtext_t *exp);
void pp_del_define(const char *name);
......@@ -170,12 +165,6 @@ void pp_next_if_state(int);
pp_if_state_t pp_pop_if(void);
pp_if_state_t pp_if_state(void);
int pp_get_if_depth(void);
char *wpp_lookup(const char *name, int type, const char *parent_name,
char **include_path, int include_path_count);
#ifndef __GNUC__
#define __attribute__(x) /*nothing*/
#endif
int ppy_error(const char *s, ...) __attribute__((format (printf, 1, 2)));
int ppy_warning(const char *s, ...) __attribute__((format (printf, 1, 2)));
......@@ -195,7 +184,6 @@ struct pp_status
extern struct pp_status pp_status;
extern include_state_t pp_incl_state;
extern struct list pp_includelogiclist;
/*
* From ppl.l
......@@ -210,8 +198,6 @@ void pp_do_include(char *fname, int type);
void pp_push_ignore_state(void);
void pp_pop_ignore_state(void);
void pp_writestring(const char *format, ...) __attribute__((format (printf, 1, 2)));
/*
* From ppy.y
*/
......
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