Implemented the flavor function which returns the flavor of
authorBoris Kolpackov <boris@kolpackov.net>
Thu, 17 Nov 2005 07:27:28 +0000 (07:27 +0000)
committerBoris Kolpackov <boris@kolpackov.net>
Thu, 17 Nov 2005 07:27:28 +0000 (07:27 +0000)
a variable ('simple', 'recursive', or 'undefined').

ChangeLog
NEWS
doc/make.texi
function.c
tests/ChangeLog
tests/scripts/functions/flavor [new file with mode: 0644]

index 6445cc2..2a01c8c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2005-11-17  Boris Kolpackov  <boris@kolpackov.net>
+
+       * function.c (func_flavor): Implement the flavor function which
+       returns the flavor of a variable.
+       * doc/make.texi (Functions for Transforming Text): Document it.
+       * NEWS: Add it to the list of new functions.
+
 2005-11-14  Boris Kolpackov  <boris@kolpackov.net>
 
        * read.c (construct_include_path): Set the .INCLUDE_DIRS special
diff --git a/NEWS b/NEWS
index 01e9016..4b1cfd4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -89,6 +89,7 @@ Version 3.81beta3
      all symbolic links resolved as well.
    - $(info ...) prints its arguments to stdout.  No makefile name or
      line number info, etc. is printed.
+   - $(flavor ...) returns the flavor of a variable.
 
 * Changes made for POSIX compatibility:
    - Only touch targets (under -t) if they have at least one command.
index 62ad82e..ed864f9 100644 (file)
@@ -273,6 +273,7 @@ Functions for Transforming Text
 * Value Function::              Return the un-expanded value of a variable.
 * Eval Function::               Evaluate the arguments as makefile syntax.
 * Origin Function::             Find where a variable got its value.
+* Flavor Function::             Find out the flavor of a variable.
 * Shell Function::              Substitute the output of a shell command.
 * Make Control Functions::      Functions that control how make runs.
 
@@ -6020,6 +6021,7 @@ call, just as a variable might be substituted.
 * Value Function::              Return the un-expanded value of a variable.
 * Eval Function::               Evaluate the arguments as makefile syntax.
 * Origin Function::             Find where a variable got its value.
+* Flavor Function::             Find out the flavor of a variable.
 * Shell Function::              Substitute the output of a shell command.
 * Make Control Functions::      Functions that control how make runs.
 @end menu
@@ -6939,7 +6941,7 @@ clean:
 @end group
 @end example
 
-@node Origin Function, Shell Function, Eval Function, Functions
+@node Origin Function, Flavor Function, Eval Function, Functions
 @section The @code{origin} Function
 @findex origin
 @cindex variables, origin of
@@ -7048,7 +7050,49 @@ Here the redefinition takes place if @samp{$(origin bletch)} returns either
 @samp{environment} or @samp{environment override}.
 @xref{Text Functions, , Functions for String Substitution and Analysis}.
 
-@node Shell Function, Make Control Functions, Origin Function, Functions
+@node Flavor Function, Shell Function, Origin Function, Functions
+@section The @code{flavor} Function
+@findex flavor
+@cindex variables, flavor of
+@cindex flavor of variable
+
+The @code{flavor} function is unlike most other functions (and like
+@code{origin} function) in that it does not operate on the values of
+variables; it tells you something @emph{about} a variable. Specifically,
+it tells you the flavor of a variable
+(@pxref{Flavors, ,The Two Flavors of Variables}).
+
+The syntax of the @code{flavor} function is:
+
+@example
+$(flavor @var{variable})
+@end example
+
+Note that @var{variable} is the @emph{name} of a variable to inquire about;
+not a @emph{reference} to that variable.  Therefore you would not normally
+use a @samp{$} or parentheses when writing it.  (You can, however, use a
+variable reference in the name if you want the name not to be a constant.)
+
+The result of this function is a string that identifies the flavor of the
+variable @var{variable}:
+
+@table @samp
+@item undefined
+
+if @var{variable} was never defined.
+
+@item recursive
+
+if @var{variable} is a recursively expanded variable.
+
+@item simple
+
+if @var{variable} is a simply expanded variable.
+
+@end table
+
+
+@node Shell Function, Make Control Functions, Flavor Function, Functions
 @section The @code{shell} Function
 @findex shell
 @cindex commands, expansion
@@ -10217,6 +10261,12 @@ Return a string describing how the @code{make} variable @var{variable} was
 defined.@*
 @xref{Origin Function, , The @code{origin} Function}.
 
+@item $(flavor @var{variable})
+
+Return a string describing the flavor of the @code{make} variable
+@var{variable}.@*
+@xref{Flavor Function, , The @code{flavor} Function}.
+
 @item $(foreach @var{var},@var{words},@var{text})
 
 Evaluate @var{text} with @var{var} bound to each word in @var{words},
index 47cdafa..95872f0 100644 (file)
@@ -489,6 +489,22 @@ func_origin (char *o, char **argv, const char *funcname UNUSED)
   return o;
 }
 
+static char *
+func_flavor (char *o, char **argv, const char *funcname UNUSED)
+{
+  register struct variable *v = lookup_variable (argv[0], strlen (argv[0]));
+
+  if (v == 0)
+    o = variable_buffer_output (o, "undefined", 9);
+  else
+    if (v->recursive)
+      o = variable_buffer_output (o, "recursive", 9);
+    else
+      o = variable_buffer_output (o, "simple", 6);
+
+  return o;
+}
+
 #ifdef VMS
 # define IS_PATHSEP(c) ((c) == ']')
 #else
@@ -1942,6 +1958,7 @@ static struct function_table_entry function_table_init[] =
   { STRING_SIZE_TUPLE("filter-out"),    2,  2,  1,  func_filter_filterout},
   { STRING_SIZE_TUPLE("findstring"),    2,  2,  1,  func_findstring},
   { STRING_SIZE_TUPLE("firstword"),     0,  1,  1,  func_firstword},
+  { STRING_SIZE_TUPLE("flavor"),        0,  1,  1,  func_flavor},
   { STRING_SIZE_TUPLE("join"),          2,  2,  1,  func_join},
   { STRING_SIZE_TUPLE("lastword"),      0,  1,  1,  func_lastword},
   { STRING_SIZE_TUPLE("patsubst"),      3,  3,  1,  func_patsubst},
index d30f0c9..01aecc6 100644 (file)
@@ -1,3 +1,7 @@
+2005-11-17  Boris Kolpackov  <boris@kolpackov.net>
+
+       * scripts/functions/flavor: Add a test for the flavor function.
+
 2005-11-14  Boris Kolpackov  <boris@kolpackov.net>
 
        * scripts/variables/INCLUDE_DIRS: Add a test for the .INCLUDE_DIRS
diff --git a/tests/scripts/functions/flavor b/tests/scripts/functions/flavor
new file mode 100644 (file)
index 0000000..80d6be7
--- /dev/null
@@ -0,0 +1,44 @@
+#                                                                    -*-perl-*-
+$description = "Test the flavor function.";
+
+$details = "";
+
+
+# Test #1: Test general logic.
+#
+run_make_test('
+s := s
+r = r
+
+$(info u $(flavor u))
+$(info s $(flavor s))
+$(info r $(flavor r))
+
+ra += ra
+rc ?= rc
+
+$(info ra $(flavor ra))
+$(info rc $(flavor rc))
+
+s += s
+r += r
+
+$(info s $(flavor s))
+$(info r $(flavor r))
+
+
+.PHONY: all
+all:;@:
+',
+'',
+'u undefined
+s simple
+r recursive
+ra recursive
+rc recursive
+s simple
+r recursive');
+
+
+# This tells the test driver that the perl test script executed properly.
+1;