diagnostics: style: avoid allocating memory when not needed

* src/muscle-tab.c (muscle_percent_variable_update): Avoid allocating
memory when it is not needed, which should be most of the time (when
there's no update to perform).
Adjust callers.
This commit is contained in:
Akim Demaille
2019-01-13 08:57:41 +01:00
parent 3551d51dd9
commit a88f8117b0

View File

@@ -429,9 +429,10 @@ define_directive (char const *assignment,
/** If the \a variable name is obsolete, return the name to use, /** If the \a variable name is obsolete, return the name to use,
* otherwise \a variable. If the \a value is obsolete, update it too. * otherwise \a variable. If the \a value is obsolete, update it too.
* *
* Allocates the returned value. */ * Allocates the returned value if needed, otherwise the returned
* value is exactly \a variable. */
static static
char * char const *
muscle_percent_variable_update (char const *variable, location variable_loc, muscle_percent_variable_update (char const *variable, location variable_loc,
muscle_kind kind, muscle_kind kind,
char const **value) char const **value)
@@ -473,12 +474,17 @@ muscle_percent_variable_update (char const *variable, location variable_loc,
? (!strncmp (c->obsolete, variable, eq - c->obsolete) ? (!strncmp (c->obsolete, variable, eq - c->obsolete)
&& STREQ (eq + 1, *value)) && STREQ (eq + 1, *value))
: STREQ (c->obsolete, variable)) : STREQ (c->obsolete, variable))
{
/* Generate the deprecation warning. */
{ {
char *old = define_directive (c->obsolete, kind, *value); char *old = define_directive (c->obsolete, kind, *value);
char *upd = define_directive (c->updated, c->kind, *value); char *upd = define_directive (c->updated, c->kind, *value);
deprecated_directive (&variable_loc, old, upd); deprecated_directive (&variable_loc, old, upd);
free (old); free (old);
free (upd); free (upd);
}
/* Update the variable and its value. */
{
char *res = xstrdup (c->updated); char *res = xstrdup (c->updated);
char *eq2 = strchr (res, '='); char *eq2 = strchr (res, '=');
if (eq2) if (eq2)
@@ -489,7 +495,8 @@ muscle_percent_variable_update (char const *variable, location variable_loc,
return res; return res;
} }
} }
return xstrdup (variable); }
return variable;
} }
void void
@@ -499,7 +506,7 @@ muscle_percent_define_insert (char const *var, location variable_loc,
muscle_percent_define_how how) muscle_percent_define_how how)
{ {
/* Backward compatibility. */ /* Backward compatibility. */
char *variable char const *variable
= muscle_percent_variable_update (var, variable_loc, kind, &value); = muscle_percent_variable_update (var, variable_loc, kind, &value);
uniqstr name = muscle_name (variable, NULL); uniqstr name = muscle_name (variable, NULL);
uniqstr loc_name = muscle_name (variable, "loc"); uniqstr loc_name = muscle_name (variable, "loc");
@@ -533,7 +540,8 @@ muscle_percent_define_insert (char const *var, location variable_loc,
MUSCLE_INSERT_INT (how_name, how); MUSCLE_INSERT_INT (how_name, how);
MUSCLE_INSERT_STRING (kind_name, muscle_kind_string (kind)); MUSCLE_INSERT_STRING (kind_name, muscle_kind_string (kind));
end: end:
free (variable); if (variable != var)
free ((char *) variable);
} }
/* This is used for backward compatibility, e.g., "%define api.pure" /* This is used for backward compatibility, e.g., "%define api.pure"