]> git.ipfire.org Git - thirdparty/make.git/commitdiff
[SV 63439, SV 63452] Don't warn on undefined internal variables
authorPaul Smith <psmith@gnu.org>
Sat, 24 Dec 2022 15:30:13 +0000 (10:30 -0500)
committerPaul Smith <psmith@gnu.org>
Sat, 24 Dec 2022 15:52:49 +0000 (10:52 -0500)
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.

src/read.c
src/variable.c
src/variable.h
src/vpath.c
tests/scripts/options/warn-undefined-variables

index 9f2ed5cfbe3675328501184a3fbf59baaeb1718d..e8ad1234c988a9543f431d98193258eb2a4abaf7 100644 (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.  */
index 1b1cb743ae201dd9004c4357057abe58475cbd38..485933054e00e78056bdbb5177fd8029074d86d3 100644 (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);
+    }
+}
 \f
 /* Print information for variable V, prefixing it with PREFIX.  */
 
index d377a47f5262d116ae05b4bbbb412b37bc4378eb..d0cb775d525fd519f1f0c591ca9be251f0a2395a 100644 (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,
index 9c95441f608d7e795c89f52dcd40f4e591b079e9..9af4213cc175e07ad7ff749dba20fc5a9245f323 100644 (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')
     {
index ce15507d1e0f76302662b98b07944e9584e13e36..9bd408656950499faccec1b837c5ecd44225bab1 100644 (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 =