Clean up expand.c

Clarify the naming and documentation on functions in src/expand.c:
- variable_expand -> expand_string
- variable_expand_string -> expand_string_buf
- variable_expand_for_file -> expand_string_for_file
- allocated_variable_expand -> allocated_expand_string
- allocated_variable_expand_for_file ->
  allocated_expand_string_for_file
Change all callers to use the new names.

* src/variable.h: Rename the functions and macros.
* src/expand.c: Ditto.
* src/file.c: Use the new function names.
* src/function.c: Ditto.
* src/implicit.c: Ditto.
* src/job.c: Ditto.
* src/loadapi.c: Ditto.
* src/main.c: Ditto.
* src/read.c: Ditto.
* src/remake.c: Ditto.
* src/variable.c: Ditto.
* src/vpath.c: Ditto.
* src/w32/subproc/sub_proc.c: Ditto.
This commit is contained in:
Paul Smith 2023-03-25 17:53:19 -04:00
parent f99d083418
commit caf1d4c28f
14 changed files with 97 additions and 105 deletions

View File

@ -207,7 +207,7 @@ recursively_expand_for_file (struct variable *v, struct file *file)
if (v->append)
value = allocated_variable_append (v);
else
value = allocated_variable_expand (v->value);
value = allocated_expand_string (v->value);
v->expanding = 0;
if (set_reading)
@ -252,16 +252,17 @@ reference_variable (char *o, const char *name, size_t length)
}
/* Scan STRING for variable references and expansion-function calls. Only
LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
a null byte is found.
LENGTH bytes of STRING are actually scanned.
If LENGTH is SIZE_MAX, scan until a null byte is found.
Write the results to LINE, which must point into 'variable_buffer'. If
LINE is NULL, start at the beginning of the buffer.
Return a pointer to LINE, or to the beginning of the buffer if LINE is
Write the results to BUF, which must point into 'variable_buffer'. If
BUF is NULL, start at the beginning of the current 'variable_buffer'.
Return a pointer to BUF, or to the beginning of the new buffer if BUF is
NULL.
*/
char *
variable_expand_string (char *line, const char *string, size_t length)
expand_string_buf (char *buf, const char *string, size_t length)
{
struct variable *v;
const char *p, *p1;
@ -269,10 +270,10 @@ variable_expand_string (char *line, const char *string, size_t length)
char *o;
size_t line_offset;
if (!line)
line = initialize_variable_output ();
o = line;
line_offset = line - variable_buffer;
if (!buf)
buf = initialize_variable_output ();
o = buf;
line_offset = buf - variable_buffer;
if (length == 0)
return variable_buffer;
@ -472,17 +473,7 @@ variable_expand_string (char *line, const char *string, size_t length)
return (variable_buffer + line_offset);
}
/* Scan LINE for variable references and expansion-function calls.
Build in 'variable_buffer' the result of expanding the references and calls.
Return the address of the resulting string, which is null-terminated
and is valid only until the next time this function is called. */
char *
variable_expand (const char *line)
{
return variable_expand_string (NULL, line, SIZE_MAX);
}
/* Expand an argument for an expansion function.
The text starting at STR and ending at END is variable-expanded
into a null-terminated string that is returned as the value.
@ -499,7 +490,7 @@ expand_argument (const char *str, const char *end)
return xstrdup ("");
if (!end || *end == '\0')
return allocated_variable_expand (str);
return allocated_expand_string (str);
if (end - str + 1 > 1000)
tmp = alloc = xmalloc (end - str + 1);
@ -509,25 +500,27 @@ expand_argument (const char *str, const char *end)
memcpy (tmp, str, end - str);
tmp[end - str] = '\0';
r = allocated_variable_expand (tmp);
r = allocated_expand_string (tmp);
free (alloc);
return r;
}
/* Expand LINE for FILE. Error messages refer to the file and line where
FILE's commands were found. Expansion uses FILE's variable set list. */
/* Expand STRING for FILE, into the current variable_buffer.
Error messages refer to the file and line where FILE's commands were found.
Expansion uses FILE's variable set list. */
char *
variable_expand_for_file (const char *line, struct file *file)
expand_string_for_file (const char *string, struct file *file)
{
char *result;
struct variable_set_list *savev;
const floc *savef;
if (file == 0)
return variable_expand (line);
if (!file)
return expand_string (string);
savev = current_variable_set_list;
current_variable_set_list = file->variables;
@ -536,17 +529,32 @@ variable_expand_for_file (const char *line, struct file *file)
if (file->cmds && file->cmds->fileinfo.filenm)
reading_file = &file->cmds->fileinfo;
else
reading_file = 0;
reading_file = NULL;
result = variable_expand (line);
result = expand_string (string);
current_variable_set_list = savev;
reading_file = savef;
return result;
}
/* Like expand_string_for_file, but the returned string is malloc'd. */
char *
allocated_expand_string_for_file (const char *string, struct file *file)
{
char *obuf;
size_t olen;
install_variable_buffer (&obuf, &olen);
expand_string_for_file (string, file);
return swap_variable_buffer (obuf, olen);
}
/* Like allocated_variable_expand, but for += target-specific variables.
/* Like allocated_expand_string, but for += target-specific variables.
First recursively construct the variable value from its appended parts in
any upper variable sets. Then expand the resulting value. */
@ -588,7 +596,7 @@ variable_append (const char *name, size_t length,
if (! v->recursive)
return variable_buffer_output (buf, v->value, strlen (v->value));
buf = variable_expand_string (buf, v->value, strlen (v->value));
buf = expand_string_buf (buf, v->value, strlen (v->value));
return (buf + strlen (buf));
}
@ -606,19 +614,3 @@ allocated_variable_append (const struct variable *v)
return swap_variable_buffer (obuf, olen);
}
/* Like variable_expand_for_file, but the returned string is malloc'd.
This function is called a lot. It wants to be efficient. */
char *
allocated_variable_expand_for_file (const char *line, struct file *file)
{
char *obuf;
size_t olen;
install_variable_buffer (&obuf, &olen);
variable_expand_for_file (line, file);
return swap_variable_buffer (obuf, olen);
}

View File

@ -636,7 +636,7 @@ expand_deps (struct file *f)
set_file_variables (f, d->stem ? d->stem : f->stem);
/* Perform second expansion. */
p = variable_expand_for_file (d->name, f);
p = expand_string_for_file (d->name, f);
/* Free the un-expanded name. */
free ((char*)d->name);
@ -688,7 +688,7 @@ struct dep *
expand_extra_prereqs (const struct variable *extra)
{
struct dep *d;
struct dep *prereqs = extra ? split_prereqs (variable_expand (extra->value)) : NULL;
struct dep *prereqs = extra ? split_prereqs (expand_string (extra->value)) : NULL;
for (d = prereqs; d; d = d->next)
{

View File

@ -885,7 +885,7 @@ func_foreach (char *o, char **argv, const char *funcname UNUSED)
free (var->value);
var->value = xstrndup (p, len);
result = allocated_variable_expand (body);
result = allocated_expand_string (body);
o = variable_buffer_output (o, result, strlen (result));
o = variable_buffer_output (o, " ", 1);
@ -945,7 +945,7 @@ func_let (char *o, char **argv, const char *funcname UNUSED)
/* Expand the body in the context of the arguments, adding the result to
the variable buffer. */
o = variable_expand_string (o, body, SIZE_MAX);
o = expand_string_buf (o, body, SIZE_MAX);
pop_variable_scope ();
free (varnames);
@ -2690,7 +2690,7 @@ func_call (char *o, char **argv, const char *funcname UNUSED)
saved_args = max_args;
max_args = i;
o = variable_expand_string (o, body, flen+3);
o = expand_string_buf (o, body, flen+3);
max_args = saved_args;
v->exp_count = 0;

View File

@ -706,7 +706,7 @@ pattern_search (struct file *file, int archive,
}
/* Perform the 2nd expansion. */
p = variable_expand_for_file (depname, file);
p = expand_string_for_file (depname, file);
dptr = &dl;
/* Parse the results into a deps list. */

View File

@ -1782,8 +1782,8 @@ new_job (struct file *file)
/* Finally, expand the line. */
cmds->fileinfo.offset = i;
lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
file);
lines[i] = allocated_expand_string_for_file (cmds->command_lines[i],
file);
}
cmds->fileinfo.offset = 0;
@ -1882,7 +1882,7 @@ new_job (struct file *file)
nm, c->file->name);
else
{
char *newer = allocated_variable_expand_for_file ("$?", c->file);
char *newer = allocated_expand_string_for_file ("$?", c->file);
if (newer[0] != '\0')
{
OSSS (message, 0, _("%s: update target '%s' due to: %s"),
@ -3637,7 +3637,7 @@ construct_command_argv (char *line, char **restp, struct file *file,
int save = warn_undefined_variables_flag;
warn_undefined_variables_flag = 0;
shell = allocated_variable_expand_for_file ("$(SHELL)", file);
shell = allocated_expand_string_for_file ("$(SHELL)", file);
#if MK_OS_W32
/*
* Convert to forward slashes so that construct_command_argv_internal()
@ -3700,9 +3700,9 @@ construct_command_argv (char *line, char **restp, struct file *file,
/* In POSIX mode we default to -ec, unless we're ignoring errors. */
shellflags = xstrdup (ANY_SET (cmd_flags, COMMANDS_NOERROR) ? "-c" : "-ec");
else
shellflags = allocated_variable_expand_for_file (var->value, file);
shellflags = allocated_expand_string_for_file (var->value, file);
ifs = allocated_variable_expand_for_file ("$(IFS)", file);
ifs = allocated_expand_string_for_file ("$(IFS)", file);
warn_undefined_variables_flag = save;
}

View File

@ -70,7 +70,7 @@ gmk_eval (const char *buffer, const gmk_floc *gfloc)
char *
gmk_expand (const char *ref)
{
return allocated_variable_expand (ref);
return allocated_expand_string (ref);
}
/* Register a function to be called from makefiles. */

View File

@ -179,6 +179,10 @@ int question_flag = 0;
int no_builtin_rules_flag = 0;
int no_builtin_variables_flag = 0;
/* Nonzero means all variables are automatically exported. */
int export_all_variables = 0;
/* Nonzero means keep going even if remaking some file fails (-k). */
int keep_going_flag;
@ -2797,7 +2801,7 @@ main (int argc, char **argv, char **envp)
char *p;
if (default_goal_var->recursive)
p = variable_expand (default_goal_var->value);
p = expand_string (default_goal_var->value);
else
{
p = variable_buffer_output (variable_buffer, default_goal_var->value,
@ -3336,7 +3340,7 @@ decode_env_switches (const char *envar, size_t len, enum variable_origin origin)
p = mempcpy (p, envar, len);
*(p++) = ')';
*p = '\0';
value = variable_expand (varref);
value = expand_string (varref);
/* Skip whitespace, and check for an empty value. */
NEXT_TOKEN (value);

View File

@ -738,7 +738,7 @@ extern unsigned short stopchar_map[];
extern int just_print_flag, run_silent, ignore_errors_flag, keep_going_flag;
extern int print_data_base_flag, question_flag, touch_flag, always_make_flag;
extern int env_overrides, no_builtin_rules_flag, no_builtin_variables_flag;
extern int print_version_flag, check_symlink_flag;
extern int print_version_flag, check_symlink_flag, export_all_variables;
extern int warn_undefined_variables_flag, posix_pedantic;
extern int not_parallel, second_expansion, clock_skew_detected;
extern int rebuilding_makefiles, one_shell, output_sync, verify_flag;
@ -752,6 +752,9 @@ extern int batch_mode_shell;
#define GNUMAKEFLAGS_NAME "GNUMAKEFLAGS"
#define MAKEFLAGS_NAME "MAKEFLAGS"
#define MAKELEVEL_NAME "MAKELEVEL"
#define MAKELEVEL_LENGTH (CSTRLEN (MAKELEVEL_NAME))
/* Resetting the command script introduction prefix character. */
#define RECIPEPREFIX_NAME ".RECIPEPREFIX"
#define RECIPEPREFIX_DEFAULT '\t'

View File

@ -186,7 +186,7 @@ read_all_makefiles (const char **makefiles)
char *name, *p;
size_t length;
value = allocated_variable_expand ("$(MAKEFILES)");
value = allocated_expand_string ("$(MAKEFILES)");
/* Set NAME to the start of next token and LENGTH to its length.
MAKEFILES is updated for finding remaining tokens. */
@ -807,7 +807,7 @@ eval (struct ebuffer *ebuf, int set_default)
/* Expand the line so we can use indirect and constructed
variable names in an (un)export command. */
cp = ap = allocated_variable_expand (p2);
cp = ap = allocated_expand_string (p2);
for (p = find_next_token (&cp, &l); p != 0;
p = find_next_token (&cp, &l))
@ -833,7 +833,7 @@ eval (struct ebuffer *ebuf, int set_default)
/* vpath ends the previous rule. */
record_waiting_files ();
cp = variable_expand (p2);
cp = expand_string (p2);
p = find_next_token (&cp, &l);
if (p != 0)
{
@ -866,7 +866,7 @@ eval (struct ebuffer *ebuf, int set_default)
/* Include ends the previous rule. */
record_waiting_files ();
p = allocated_variable_expand (p2);
p = allocated_expand_string (p2);
/* If no filenames, it's a no-op. */
if (*p == '\0')
@ -920,7 +920,7 @@ eval (struct ebuffer *ebuf, int set_default)
/* Load ends the previous rule. */
record_waiting_files ();
p = allocated_variable_expand (p2);
p = allocated_expand_string (p2);
/* If no filenames, it's a no-op. */
if (*p == '\0')
@ -1052,7 +1052,7 @@ eval (struct ebuffer *ebuf, int set_default)
break;
}
p2 = variable_expand_string (NULL, lb_next, wlen);
p2 = expand_string_buf (NULL, lb_next, wlen);
while (1)
{
@ -1080,7 +1080,7 @@ eval (struct ebuffer *ebuf, int set_default)
entirely consistent, since we do an unconditional
expand below once we know we don't have a
target-specific variable. */
variable_expand_string (pend, lb_next, SIZE_MAX);
expand_string_buf (pend, lb_next, SIZE_MAX);
lb_next += strlen (lb_next);
p2 = variable_buffer + p2_off;
cmdleft = variable_buffer + cmd_off + 1;
@ -1116,7 +1116,7 @@ eval (struct ebuffer *ebuf, int set_default)
p2 += strlen (p2);
*(p2++) = ' ';
p2 = variable_expand_string (p2, lb_next, wlen);
p2 = expand_string_buf (p2, lb_next, wlen);
/* We don't need to worry about cmdleft here, because if it was
found in the variable_buffer the entire buffer has already
been expanded... we'll never get here. */
@ -1228,7 +1228,7 @@ eval (struct ebuffer *ebuf, int set_default)
if (*lb_next != '\0')
{
size_t l = p2 - variable_buffer;
variable_expand_string (p2 + plen, lb_next, SIZE_MAX);
expand_string_buf (p2 + plen, lb_next, SIZE_MAX);
p2 = variable_buffer + l;
/* Look for a semicolon in the expanded line. */
@ -1359,7 +1359,7 @@ do_undefine (char *name, enum variable_origin origin, struct ebuffer *ebuf)
char *p, *var;
/* Expand the variable name and find the beginning (NAME) and end. */
var = allocated_variable_expand (name);
var = allocated_expand_string (name);
name = next_token (var);
if (*name == '\0')
O (fatal, &ebuf->floc, _("empty variable name"));
@ -1404,7 +1404,7 @@ do_define (char *name, enum variable_origin origin, struct ebuffer *ebuf)
}
/* Expand the variable name and find the beginning (NAME) and end. */
n = allocated_variable_expand (name);
n = allocated_expand_string (name);
name = next_token (n);
if (name[0] == '\0')
O (fatal, &defstart, _("empty variable name"));
@ -1637,7 +1637,7 @@ conditional_line (char *line, size_t len, const floc *flocp)
/* Expand the thing we're looking up, so we can use indirect and
constructed variable names. */
var = allocated_variable_expand (line);
var = allocated_expand_string (line);
/* Make sure there's only one variable name to test. */
p = end_of_token (var);
@ -1695,9 +1695,9 @@ conditional_line (char *line, size_t len, const floc *flocp)
else
*line++ = '\0';
s2 = variable_expand (s1);
s2 = expand_string (s1);
/* We must allocate a new copy of the expanded string because
variable_expand re-uses the same buffer. */
expand_string re-uses the same buffer. */
l = strlen (s2);
s1 = alloca (l + 1);
memcpy (s1, s2, l + 1);
@ -1744,7 +1744,7 @@ conditional_line (char *line, size_t len, const floc *flocp)
if (*line != '\0')
EXTRATEXT ();
s2 = variable_expand (s2);
s2 = expand_string (s2);
conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
}
@ -1804,7 +1804,7 @@ record_target_var (struct nameseq *filenames, char *defn,
v->origin = origin;
if (v->flavor == f_simple)
v->value = allocated_variable_expand (v->value);
v->value = allocated_expand_string (v->value);
else
v->value = xstrdup (v->value);
}
@ -3071,7 +3071,7 @@ tilde_expand (const char *name)
int save = warn_undefined_variables_flag;
warn_undefined_variables_flag = 0;
home_dir = allocated_variable_expand ("$(HOME)");
home_dir = allocated_expand_string ("$(HOME)");
warn_undefined_variables_flag = save;
}

View File

@ -1717,7 +1717,7 @@ library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
const char **dp;
libpatterns = xstrdup (variable_expand ("$(.LIBPATTERNS)"));
libpatterns = xstrdup (expand_string ("$(.LIBPATTERNS)"));
/* Skip the '-l'. */
lib += 2;

View File

@ -1005,7 +1005,6 @@ define_automatic_variables (void)
define_variable_cname ("+F", "$(notdir $+)", o_automatic, 1);
}
int export_all_variables;
static int
should_export (const struct variable *v)
@ -1305,14 +1304,14 @@ do_variable_definition (const floc *flocp, const char *varname,
We have to allocate memory since otherwise it'll clobber the
variable buffer, and we may still need that if we're looking at a
target-specific variable. */
newval = alloc_value = allocated_variable_expand (value);
newval = alloc_value = allocated_expand_string (value);
break;
case f_expand:
{
/* A POSIX "var :::= value" assignment. Expand the value, then it
becomes a recursive variable. After expansion convert all '$'
tokens to '$$' to resolve to '$' when recursively expanded. */
char *t = allocated_variable_expand (value);
char *t = allocated_expand_string (value);
char *np = alloc_value = xmalloc (strlen (t) * 2 + 1);
char *op = t;
while (op[0] != '\0')
@ -1330,7 +1329,7 @@ do_variable_definition (const floc *flocp, const char *varname,
{
/* A shell definition "var != value". Expand value, pass it to
the shell, and store the result in recursively-expanded var. */
char *q = allocated_variable_expand (value);
char *q = allocated_expand_string (value);
alloc_value = shell_result (q);
free (q);
flavor = f_recursive;
@ -1398,7 +1397,7 @@ do_variable_definition (const floc *flocp, const char *varname,
when it was set; and from the expanded new value. Allocate
memory for the expansion as we may still need the rest of the
buffer if we're looking at a target-specific variable. */
val = tp = allocated_variable_expand (val);
val = tp = allocated_expand_string (val);
/* If the new value is empty, nothing to do. */
vallen = strlen (val);
@ -1544,7 +1543,7 @@ do_variable_definition (const floc *flocp, const char *varname,
{
char *tp = alloc_value;
alloc_value = allocated_variable_expand (newval);
alloc_value = allocated_expand_string (newval);
if (find_and_set_default_shell (alloc_value))
{
@ -1767,7 +1766,7 @@ assign_variable_definition (struct variable *v, const char *line)
name = alloca (v->length + 1);
memcpy (name, v->name, v->length);
name[v->length] = '\0';
v->name = allocated_variable_expand (name);
v->name = allocated_expand_string (name);
if (v->name[0] == '\0')
O (fatal, &v->fileinfo, _("empty variable name"));
@ -2020,7 +2019,7 @@ sync_Path_environment ()
{
static char *environ_path = NULL;
char *oldpath = environ_path;
char *path = allocated_variable_expand ("PATH=$(PATH)");
char *path = allocated_expand_string ("PATH=$(PATH)");
if (!path)
return;

View File

@ -127,15 +127,14 @@ void install_variable_buffer (char **bufp, size_t *lenp);
void restore_variable_buffer (char *buf, size_t len);
char *swap_variable_buffer (char *buf, size_t len);
char *variable_expand_string (char *line, const char *string, size_t length);
char *variable_expand (const char *line);
char *variable_expand_for_file (const char *line, struct file *file);
char *allocated_variable_expand_for_file (const char *line, struct file *file);
#define allocated_variable_expand(line) \
allocated_variable_expand_for_file (line, (struct file *) 0)
char *expand_string_buf (char *buf, const char *string, size_t length);
#define expand_string(s) expand_string_buf (NULL, (s), SIZE_MAX)
char *expand_string_for_file (const char *string, struct file *file);
char *allocated_expand_string_for_file (const char *line, struct file *file);
#define allocated_expand_string(s) allocated_expand_string_for_file ((s), NULL)
char *expand_argument (const char *str, const char *end);
char *recursively_expand_for_file (struct variable *v, struct file *file);
#define recursively_expand(v) recursively_expand_for_file (v, NULL)
#define recursively_expand(v) recursively_expand_for_file ((v), NULL)
/* function.c */
int handle_function (char **op, const char **stringp);
@ -232,8 +231,3 @@ char **target_environment (struct file *file, int recursive);
struct pattern_var *create_pattern_var (const char *target,
const char *suffix);
extern int export_all_variables;
#define MAKELEVEL_NAME "MAKELEVEL"
#define MAKELEVEL_LENGTH (CSTRLEN (MAKELEVEL_NAME))

View File

@ -71,7 +71,7 @@ build_vpath_lists (void)
/* If there is a VPATH variable with a nonnull expanded value, construct the
general VPATH list from it. */
p = variable_expand ("$(strip $(VPATH))");
p = expand_string ("$(strip $(VPATH))");
if (*p != '\0')
{
@ -95,7 +95,7 @@ build_vpath_lists (void)
/* If there is a GPATH variable with a nonnull expanded value, construct the
GPATH list from it. */
p = variable_expand ("$(strip $(GPATH))");
p = expand_string ("$(strip $(GPATH))");
if (*p != '\0')
{

View File

@ -615,7 +615,7 @@ process_begin(
char **argvi = argv;
size_t arglen = 0;
strcpy(buf, variable_expand ("$(SHELL)"));
strcpy(buf, expand_string ("$(SHELL)"));
shell_name = &buf[0];
strcpy(exec_fname, "-c");
/* Construct a single command string in argv[0]. */