wpp: Let pp_add_define take the responsibility of copying strings (Valgrind).

This commit is contained in:
Matteo Bruni 2011-02-07 15:15:13 +01:00 committed by Alexandre Julliard
parent e32598b046
commit 46caa0d225
4 changed files with 29 additions and 22 deletions

View File

@ -275,7 +275,7 @@ preprocessor
}
}
| tUNDEF tIDENT tNL { pp_del_define($2); free($2); }
| tDEFINE opt_text tNL { pp_add_define($1, $2); }
| tDEFINE opt_text tNL { pp_add_define($1, $2); free($1); free($2); }
| tMACRO res_arg allmargs tMACROEND opt_mtexts tNL {
pp_add_macro($1, macro_args, nmacro_args, $5);
}

View File

@ -319,7 +319,7 @@ void pp_del_define(const char *name)
printf("Deleted (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name);
}
pp_entry_t *pp_add_define(char *def, char *text)
pp_entry_t *pp_add_define(const char *def, const char *text)
{
int len;
char *cptr;
@ -339,38 +339,45 @@ pp_entry_t *pp_add_define(char *def, char *text)
if(!ppp)
return NULL;
memset( ppp, 0, sizeof(*ppp) );
ppp->ident = def;
ppp->ident = pp_xstrdup(def);
if(!ppp->ident)
goto error;
ppp->type = def_define;
ppp->subst.text = text;
ppp->subst.text = text ? pp_xstrdup(text) : NULL;
if(text && !ppp->subst.text)
goto error;
ppp->filename = pp_xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
if(!ppp->filename)
{
free(ppp);
return NULL;
}
goto error;
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
ppp->next = pp_def_state->defines[idx];
pp_def_state->defines[idx] = ppp;
if(ppp->next)
ppp->next->prev = ppp;
if(text)
if(ppp->subst.text)
{
/* Strip trailing white space from subst text */
len = strlen(text);
while(len && strchr(" \t\r\n", text[len-1]))
len = strlen(ppp->subst.text);
while(len && strchr(" \t\r\n", ppp->subst.text[len-1]))
{
text[--len] = '\0';
ppp->subst.text[--len] = '\0';
}
/* Strip leading white space from subst text */
for(cptr = text; *cptr && strchr(" \t\r", *cptr); cptr++)
for(cptr = ppp->subst.text; *cptr && strchr(" \t\r", *cptr); cptr++)
;
if(text != cptr)
memmove(text, cptr, strlen(cptr)+1);
if(ppp->subst.text != cptr)
memmove(ppp->subst.text, cptr, strlen(cptr)+1);
}
if(pp_status.debug)
printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, text ? text : "(null)");
printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, ppp->subst.text ? ppp->subst.text : "(null)");
return ppp;
error:
free(ppp->ident);
free(ppp->subst.text);
free(ppp);
return NULL;
}
pp_entry_t *pp_add_macro(char *id, marg_t *args[], int nargs, mtext_t *exp)

View File

@ -45,7 +45,7 @@ static void add_cmdline_defines(void)
for (def = cmdline_defines; def; def = def->next)
{
if (def->value) pp_add_define( pp_xstrdup(def->name), pp_xstrdup(def->value) );
if (def->value) pp_add_define( def->name, def->value );
}
}
@ -56,16 +56,16 @@ static void add_special_defines(void)
char buf[32];
strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf) );
pp_add_define( "__DATE__", buf );
strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) );
pp_add_define( "__TIME__", buf );
ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
ppp = pp_add_define( "__FILE__", "" );
if(ppp)
ppp->type = def_special;
ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
ppp = pp_add_define( "__LINE__", "" );
if(ppp)
ppp->type = def_special;
}

View File

@ -204,7 +204,7 @@ char *pp_xstrdup(const char *str);
pp_entry_t *pplookup(const char *ident);
int pp_push_define_state(void);
void pp_pop_define_state(void);
pp_entry_t *pp_add_define(char *def, char *text);
pp_entry_t *pp_add_define(const char *def, const char *text);
pp_entry_t *pp_add_macro(char *ident, marg_t *args[], int nargs, mtext_t *exp);
void pp_del_define(const char *name);
void *pp_open_include(const char *name, const char *parent_name, char **newpath);