[SV 63439, SV 63452] Don't warn on undefined internal variables

Don't generate undefined variable warnings for variables that are
internal / special to make and where the empty string is valid.
Rather than defining them to empty, which could introduce unwanted
behavior, keep a list of variable names which we should never warn
about.

* src/variable.h (warn_undefined): Convert the macro to a function.
* src/variable.c (defined_vars): Always "defined" variable names.
(warn_undefined): Implement as a function and check against the
defined_vars before generating a warning.
* src/read.c (read_all_makefiles): No need to reset warning flag.
* src/vpath.c (build_vpath_lists): Ditto.
* tests/scripts/options/warn-undefined-variables: Expand all the
pre-defined variables to ensure warnings are not generated.
This commit is contained in:
Paul Smith 2022-12-24 10:30:13 -05:00
parent 76d2e5d98d
commit 15dfad96d7
5 changed files with 45 additions and 42 deletions

View File

@ -192,15 +192,7 @@ read_all_makefiles (const char **makefiles)
char *name, *p;
size_t length;
{
/* Turn off --warn-undefined-variables while we expand MAKEFILES. */
int save = warn_undefined_variables_flag;
warn_undefined_variables_flag = 0;
value = allocated_variable_expand ("$(MAKEFILES)");
warn_undefined_variables_flag = save;
}
value = allocated_variable_expand ("$(MAKEFILES)");
/* Set NAME to the start of next token and LENGTH to its length.
MAKEFILES is updated for finding remaining tokens. */

View File

@ -1782,6 +1782,30 @@ try_variable_definition (const floc *flocp, const char *line,
return vp;
}
/* These variables are internal to make, and so considered "defined" for the
purposes of warn_undefined even if they are not really defined. */
static const char *const defined_vars[] = {
"MAKECMDGOALS", "MAKE_RESTARTS", "MAKE_TERMOUT", "MAKE_TERMERR",
"MAKEOVERRIDES", ".DEFAULT", "-*-command-variables-*-", "-*-eval-flags-*-",
"VPATH", "GPATH",
NULL };
void
warn_undefined (const char *name, size_t len)
{
if (warn_undefined_variables_flag)
{
const char *const *cp;
for (cp = defined_vars; *cp != NULL; ++cp)
if (memcmp (*cp, name, len) == 0 && (*cp)[len] == '\0')
return;
error (reading_file, len, _("warning: undefined variable '%.*s'"),
(int)len, name);
}
}
/* Print information for variable V, prefixing it with PREFIX. */

View File

@ -191,6 +191,7 @@ struct variable *define_variable_in_set (const char *name, size_t length,
int recursive,
struct variable_set *set,
const floc *flocp);
void warn_undefined (const char* name, size_t length);
/* Define a variable in the current variable set. */
@ -229,15 +230,6 @@ void undefine_variable_in_set (const char *name, size_t length,
#define undefine_variable_global(n,l,o) \
undefine_variable_in_set((n),(l),(o),NULL)
/* Warn that NAME is an undefined variable. */
#define warn_undefined(n,l) do{\
if (warn_undefined_variables_flag) \
error (reading_file, (l), \
_("warning: undefined variable '%.*s'"), \
(int)(l), (n)); \
}while(0)
char **target_environment (struct file *file, int recursive);
struct pattern_var *create_pattern_var (const char *target,

View File

@ -68,19 +68,10 @@ build_vpath_lists (void)
vpaths = new;
/* If there is a VPATH variable with a nonnull value, construct the
general VPATH list from it. We use variable_expand rather than just
calling lookup_variable so that it will be recursively expanded. */
/* If there is a VPATH variable with a nonnull expanded value, construct the
general VPATH list from it. */
{
/* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
int save = warn_undefined_variables_flag;
warn_undefined_variables_flag = 0;
p = variable_expand ("$(strip $(VPATH))");
warn_undefined_variables_flag = save;
}
p = variable_expand ("$(strip $(VPATH))");
if (*p != '\0')
{
@ -101,19 +92,10 @@ build_vpath_lists (void)
vpaths = save_vpaths;
}
/* If there is a GPATH variable with a nonnull value, construct the
GPATH list from it. We use variable_expand rather than just
calling lookup_variable so that it will be recursively expanded. */
/* If there is a GPATH variable with a nonnull expanded value, construct the
GPATH list from it. */
{
/* Turn off --warn-undefined-variables while we expand SHELL and IFS. */
int save = warn_undefined_variables_flag;
warn_undefined_variables_flag = 0;
p = variable_expand ("$(strip $(GPATH))");
warn_undefined_variables_flag = save;
}
p = variable_expand ("$(strip $(GPATH))");
if (*p != '\0')
{

View File

@ -4,6 +4,19 @@ $description = "Test the --warn-undefined-variables option.";
$details = "Verify that warnings are printed for referencing undefined variables.";
# Verify that make's special variables don't warn even if they're not set
run_make_test(q!
vars := $(.VARIABLES) $(MAKECMDGOALS) $(MAKE_RESTARTS) $(CURDIR)
vars += $(GNUMAKEFLAGS) $(MAKEFLAGS) $(MFLAGS) $(MAKE_COMMAND) $(MAKE)
vars += $(MAKEFILE_LIST) $(MAKEOVERRIDES) $(-*-command-variables-*-)
vars += $(.RECIPEPREFIX) $(.LOADED) $(.FEATURES)
vars += $(SHELL) $(.SHELLFLAGS) $(MAKE_TERMOUT) $(MAKE_TERMERR)
vars += $(.DEFAULT) $(.DEFAULT_GOAL) $(-*-eval-flags-*-) $(SUFFIXES)
vars += $(VPATH) $(GPATH)
all:;
!,
'--warn-undefined-variables', "#MAKE#: 'all' is up to date.");
# Without --warn-undefined-variables, nothing should happen
run_make_test('
EMPTY =