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)
@@ -474,22 +475,28 @@ muscle_percent_variable_update (char const *variable, location variable_loc,
&& STREQ (eq + 1, *value)) && STREQ (eq + 1, *value))
: STREQ (c->obsolete, variable)) : STREQ (c->obsolete, variable))
{ {
char *old = define_directive (c->obsolete, kind, *value); /* Generate the deprecation warning. */
char *upd = define_directive (c->updated, c->kind, *value); {
deprecated_directive (&variable_loc, old, upd); char *old = define_directive (c->obsolete, kind, *value);
free (old); char *upd = define_directive (c->updated, c->kind, *value);
free (upd); deprecated_directive (&variable_loc, old, upd);
char *res = xstrdup (c->updated); free (old);
char *eq2 = strchr (res, '='); free (upd);
if (eq2) }
{ /* Update the variable and its value. */
*eq2 = '\0'; {
*value = eq2 + 1; char *res = xstrdup (c->updated);
} char *eq2 = strchr (res, '=');
return res; if (eq2)
{
*eq2 = '\0';
*value = eq2 + 1;
}
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"