Ignore:
Timestamp:
Mar 14, 2018, 10:28:10 PM (7 years ago)
Author:
bird
Message:

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

Location:
trunk/src/kmk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk

  • trunk/src/kmk/expand.c

    r2771 r3140  
    11/* Variable expansion functions for GNU Make.
    2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
    4 2010 Free Software Foundation, Inc.
     2Copyright (C) 1988-2016 Free Software Foundation, Inc.
    53This file is part of GNU Make.
    64
     
    1715this program.  If not, see <http://www.gnu.org/licenses/>.  */
    1816
    19 #include "make.h"
     17#include "makeint.h"
    2018
    2119#include <assert.h>
     
    3230/* Initially, any errors reported when expanding strings will be reported
    3331   against the file where the error appears.  */
    34 const struct floc **expanding_var = &reading_file;
     32const floc **expanding_var = &reading_file;
    3533
    3634/* The next two describe the variable output buffer.
     
    8482      unsigned int offset = ptr - variable_buffer;
    8583      variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
    86                                 ? newlen + 100
    87                                 : 2 * variable_buffer_length);
     84                                ? newlen + 100
     85                                : 2 * variable_buffer_length);
    8886      variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
    8987      ptr = variable_buffer + offset;
     
    145143{
    146144  char *value;
    147   const struct floc *this_var;
    148   const struct floc **saved_varp;
     145  const floc *this_var;
     146  const floc **saved_varp;
    149147  struct variable_set_list *save = 0;
    150148  int set_reading = 0;
     
    170168      if (!v->exp_count)
    171169        /* Expanding V causes infinite recursion.  Lose.  */
    172         fatal (*expanding_var,
    173                _("Recursive variable `%s' references itself (eventually)"),
    174                v->name);
     170        OS (fatal, *expanding_var,
     171            _("Recursive variable '%s' references itself (eventually)"),
     172            v->name);
    175173      --v->exp_count;
    176174    }
     
    231229reference_recursive_variable (char *o, struct variable *v)
    232230{
    233   const struct floc *this_var;
    234   const struct floc **saved_varp;
     231  const floc *this_var;
     232  const floc **saved_varp;
    235233  int set_reading = 0;
    236234
     
    255253      if (!v->exp_count)
    256254        /* Expanding V causes infinite recursion.  Lose.  */
    257         fatal (*expanding_var,
    258                _("Recursive variable `%s' references itself (eventually)"),
    259                v->name);
     255        OS (fatal, *expanding_var,
     256            _("Recursive variable `%s' references itself (eventually)"),
     257            v->name);
    260258      --v->exp_count;
    261259    }
     
    346344   a null byte is found.
    347345
    348    Write the results to LINE, which must point into `variable_buffer'.  If
     346   Write the results to LINE, which must point into 'variable_buffer'.  If
    349347   LINE is NULL, start at the beginning of the buffer.
    350348   Return a pointer to LINE, or to the beginning of the buffer if LINE is
     
    356354  struct variable *v;
    357355  const char *p, *p1;
    358   char *abuf = NULL;
     356  char *save;
    359357  char *o;
    360358  unsigned int line_offset;
    361359
    362360  if (!line)
    363     line = initialize_variable_output();
     361    line = initialize_variable_output ();
    364362  o = line;
    365363  line_offset = line - variable_buffer;
     
    371369    }
    372370
    373   /* If we want a subset of the string, allocate a temporary buffer for it.
    374      Most of the functions we use here don't work with length limits.  */
    375   if (length > 0 && string[length] != '\0')
    376     {
    377       abuf = xmalloc(length+1);
    378       memcpy(abuf, string, length);
    379       abuf[length] = '\0';
    380       string = abuf;
    381     }
    382   p = string;
     371  /* We need a copy of STRING: due to eval, it's possible that it will get
     372     freed as we process it (it might be the value of a variable that's reset
     373     for example).  Also having a nil-terminated string is handy.  */
     374  save = length < 0 ? xstrdup (string) : xstrndup (string, length);
     375  p = save;
    383376
    384377  while (1)
     
    386379      /* Copy all following uninteresting chars all at once to the
    387380         variable output buffer, and skip them.  Uninteresting chars end
    388         at the next $ or the end of the input.  */
     381        at the next $ or the end of the input.  */
    389382
    390383      p1 = strchr (p, '$');
     
    393386
    394387      if (p1 == 0)
    395         break;
     388        break;
    396389      p = p1 + 1;
    397390
     
    399392
    400393      switch (*p)
    401         {
    402         case '$':
    403           /* $$ seen means output one $ to the variable output buffer.  */
    404           o = variable_buffer_output (o, p, 1);
    405           break;
    406 
    407         case '(':
    408         case '{':
    409           /* $(...) or ${...} is the general case of substitution.  */
    410           {
    411             char openparen = *p;
    412             char closeparen = (openparen == '(') ? ')' : '}';
     394        {
     395        case '$':
     396        case '\0':
     397          /* $$ or $ at the end of the string means output one $ to the
     398             variable output buffer.  */
     399          o = variable_buffer_output (o, p1, 1);
     400          break;
     401
     402        case '(':
     403        case '{':
     404          /* $(...) or ${...} is the general case of substitution.  */
     405          {
     406            char openparen = *p;
     407            char closeparen = (openparen == '(') ? ')' : '}';
    413408            const char *begp;
    414             const char *beg = p + 1;
    415             char *op;
     409            const char *beg = p + 1;
     410            char *op;
    416411            char *abeg = NULL;
    417             const char *end, *colon;
    418 
    419             op = o;
    420             begp = p;
    421             if (handle_function (&op, &begp))
    422               {
    423                 o = op;
    424                 p = begp;
    425                 break;
    426               }
    427 
    428             /* Is there a variable reference inside the parens or braces?
    429                If so, expand it before expanding the entire reference.  */
    430 
    431             end = strchr (beg, closeparen);
    432             if (end == 0)
     412            const char *end, *colon;
     413
     414            op = o;
     415            begp = p;
     416            if (handle_function (&op, &begp))
     417              {
     418                o = op;
     419                p = begp;
     420                break;
     421              }
     422
     423            /* Is there a variable reference inside the parens or braces?
     424               If so, expand it before expanding the entire reference.  */
     425
     426            end = strchr (beg, closeparen);
     427            if (end == 0)
    433428              /* Unterminated variable reference.  */
    434               fatal (*expanding_var, _("unterminated variable reference"));
    435             p1 = lindex (beg, end, '$');
    436             if (p1 != 0)
    437               {
    438                 /* BEG now points past the opening paren or brace.
    439                    Count parens or braces until it is matched.  */
    440                 int count = 0;
    441                 for (p = beg; *p != '\0'; ++p)
    442                   {
    443                     if (*p == openparen)
    444                       ++count;
    445                     else if (*p == closeparen && --count < 0)
    446                       break;
    447                   }
    448                 /* If COUNT is >= 0, there were unmatched opening parens
    449                    or braces, so we go to the simple case of a variable name
    450                    such as `$($(a)'.  */
    451                 if (count < 0)
    452                   {
    453                     abeg = expand_argument (beg, p); /* Expand the name.  */
    454                     beg = abeg;
    455                     end = strchr (beg, '\0');
    456                   }
    457               }
    458             else
    459               /* Advance P to the end of this reference.  After we are
     429              O (fatal, *expanding_var, _("unterminated variable reference"));
     430            p1 = lindex (beg, end, '$');
     431            if (p1 != 0)
     432              {
     433                /* BEG now points past the opening paren or brace.
     434                   Count parens or braces until it is matched.  */
     435                int count = 0;
     436                for (p = beg; *p != '\0'; ++p)
     437                  {
     438                    if (*p == openparen)
     439                      ++count;
     440                    else if (*p == closeparen && --count < 0)
     441                      break;
     442                  }
     443                /* If COUNT is >= 0, there were unmatched opening parens
     444                   or braces, so we go to the simple case of a variable name
     445                   such as '$($(a)'.  */
     446                if (count < 0)
     447                  {
     448                    abeg = expand_argument (beg, p); /* Expand the name.  */
     449                    beg = abeg;
     450                    end = strchr (beg, '\0');
     451                  }
     452              }
     453            else
     454              /* Advance P to the end of this reference.  After we are
    460455                 finished expanding this one, P will be incremented to
    461456                 continue the scan.  */
    462               p = end;
    463 
    464             /* This is not a reference to a built-in function and
    465                any variable references inside are now expanded.
    466                Is the resultant text a substitution reference?  */
    467 
    468             colon = lindex (beg, end, ':');
    469             if (colon)
    470               {
    471                 /* This looks like a substitution reference: $(FOO:A=B).  */
    472                 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
    473 
    474                 subst_beg = colon + 1;
    475                 subst_end = lindex (subst_beg, end, '=');
    476                 if (subst_end == 0)
    477                   /* There is no = in sight.  Punt on the substitution
    478                      reference and treat this as a variable name containing
    479                      a colon, in the code below.  */
    480                   colon = 0;
    481                 else
    482                   {
    483                     replace_beg = subst_end + 1;
    484                     replace_end = end;
    485 
    486                     /* Extract the variable name before the colon
    487                        and look up that variable.  */
    488                     v = lookup_variable (beg, colon - beg);
    489                     if (v == 0)
    490                       warn_undefined (beg, colon - beg);
     457              p = end;
     458
     459            /* This is not a reference to a built-in function and
     460               any variable references inside are now expanded.
     461               Is the resultant text a substitution reference?  */
     462
     463            colon = lindex (beg, end, ':');
     464            if (colon)
     465              {
     466                /* This looks like a substitution reference: $(FOO:A=B).  */
     467                const char *subst_beg = colon + 1;
     468                const char *subst_end = lindex (subst_beg, end, '=');
     469                if (subst_end == 0)
     470                  /* There is no = in sight.  Punt on the substitution
     471                     reference and treat this as a variable name containing
     472                     a colon, in the code below.  */
     473                  colon = 0;
     474                else
     475                  {
     476                    const char *replace_beg = subst_end + 1;
     477                    const char *replace_end = end;
     478
     479                    /* Extract the variable name before the colon
     480                       and look up that variable.  */
     481                    v = lookup_variable (beg, colon - beg);
     482                    if (v == 0)
     483                      warn_undefined (beg, colon - beg);
    491484
    492485                    /* If the variable is not empty, perform the
    493486                       substitution.  */
    494                     if (v != 0 && *v->value != '\0')
    495                       {
    496                         char *pattern, *replace, *ppercent, *rpercent;
    497                         char *value = (v->recursive
     487                    if (v != 0 && *v->value != '\0')
     488                      {
     489                        char *pattern, *replace, *ppercent, *rpercent;
     490                        char *value = (v->recursive
    498491                                       ? recursively_expand (v)
    499                                        : v->value);
     492                                       : v->value);
    500493
    501494                        /* Copy the pattern and the replacement.  Add in an
     
    515508                        /* Look for %.  Set the percent pointers properly
    516509                           based on whether we find one or not.  */
    517                         ppercent = find_percent (pattern);
    518                         if (ppercent)
     510                        ppercent = find_percent (pattern);
     511                        if (ppercent)
    519512                          {
    520513                            ++ppercent;
     
    523516                              ++rpercent;
    524517                          }
    525                         else
     518                        else
    526519                          {
    527520                            ppercent = pattern;
     
    534527                                                 ppercent, rpercent);
    535528
    536                         if (v->recursive)
    537                           free (value);
    538                       }
    539                   }
    540               }
    541 
    542             if (colon == 0)
    543               /* This is an ordinary variable reference.
    544                  Look up the value of the variable.  */
    545                 o = reference_variable (o, beg, end - beg);
    546 
    547           if (abeg)
    548             free (abeg);
    549           }
    550           break;
    551 
    552         case '\0':
    553           break;
    554 
    555         default:
    556           if (isblank ((unsigned char)p[-1]))
    557             break;
    558 
    559           /* A $ followed by a random char is a variable reference:
    560              $a is equivalent to $(a).  */
     529                        if (v->recursive)
     530                          free (value);
     531                      }
     532                  }
     533              }
     534
     535            if (colon == 0)
     536              /* This is an ordinary variable reference.
     537                 Look up the value of the variable.  */
     538                o = reference_variable (o, beg, end - beg);
     539
     540            free (abeg);
     541          }
     542          break;
     543
     544        default:
     545          if (ISSPACE (p[-1]))
     546            break;
     547
     548          /* A $ followed by a random char is a variable reference:
     549             $a is equivalent to $(a).  */
    561550          o = reference_variable (o, p, 1);
    562551
    563           break;
    564         }
     552          break;
     553        }
    565554
    566555      if (*p == '\0')
    567         break;
     556        break;
    568557
    569558      ++p;
    570559    }
    571560
    572   if (abuf)
    573     free (abuf);
     561  free (save);
    574562
    575563  variable_buffer_output (o, "", 1);
     
    682670            if (end == 0)
    683671              /* Unterminated variable reference.  */
    684               fatal (*expanding_var, _("unterminated variable reference"));
     672              O (fatal, *expanding_var, _("unterminated variable reference"));
    685673            p1 = lindex (beg, end, '$');
    686674            if (p1 != 0)
     
    812800
    813801        default:
    814           if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
     802          if (ISBLANK (p[-1])) /* XXX: This looks incorrect, previous is '$' */
    815803            break;
    816804
     
    839827
    840828/* Scan LINE for variable references and expansion-function calls.
    841    Build in `variable_buffer' the result of expanding the references and calls.
     829   Build in 'variable_buffer' the result of expanding the references and calls.
    842830   Return the address of the resulting string, which is null-terminated
    843831   and is valid only until the next time this function is called.  */
     
    847835{
    848836#ifndef CONFIG_WITH_VALUE_LENGTH
    849   return variable_expand_string(NULL, line, (long)-1);
     837  return variable_expand_string (NULL, line, (long)-1);
    850838#else  /* CONFIG_WITH_VALUE_LENGTH */
    851839  char *s;
     
    865853   The text starting at STR and ending at END is variable-expanded
    866854   into a null-terminated string that is returned as the value.
    867    This is done without clobbering `variable_buffer' or the current
     855   This is done without clobbering 'variable_buffer' or the current
    868856   variable-expansion that is in progress.  */
    869857
     
    877865
    878866  if (str == end)
    879     return xstrdup("");
     867    return xstrdup ("");
    880868
    881869#ifndef CONFIG_WITH_VALUE_LENGTH
     
    893881  r = allocated_variable_expand (tmp);
    894882
    895   if (alloc)
    896     free (alloc);
     883  free (alloc);
    897884
    898885  return r;
     
    913900  char *result;
    914901  struct variable_set_list *savev;
    915   const struct floc *savef;
     902  const floc *savef;
    916903
    917904  if (file == 0)
     
    950937  char *result;
    951938  struct variable_set_list *savev;
    952   const struct floc *savef;
     939  const floc *savef;
    953940  long len = length == ~0U ? (long)-1 : (long)length;
    954941  char *eol;
     
    990977static char *
    991978variable_append (const char *name, unsigned int length,
    992                  const struct variable_set_list *set)
     979                 const struct variable_set_list *set, int local)
    993980{
    994981  const struct variable *v;
    995982  char *buf = 0;
     983  /* If this set is local and the next is not a parent, then next is local.  */
     984  int nextlocal = local && set->next_is_parent == 0;
    996985
    997986  /* If there's nothing left to check, return the empty buffer.  */
     
    1002991  v = lookup_variable_in_set (name, length, set->set);
    1003992
    1004   /* If there isn't one, look to see if there's one in a set above us.  */
    1005   if (!v)
    1006     return variable_append (name, length, set->next);
     993  /* If there isn't one, or this one is private, try the set above us.  */
     994  if (!v || (!local && v->private_var))
     995    return variable_append (name, length, set->next, nextlocal);
    1007996
    1008997  /* If this variable type is append, first get any upper values.
    1009998     If not, initialize the buffer.  */
    1010999  if (v->append)
    1011     buf = variable_append (name, length, set->next);
     1000    buf = variable_append (name, length, set->next, nextlocal);
    10121001  else
    10131002    buf = initialize_variable_output ();
     
    11151104  variable_buffer = 0;
    11161105
    1117   assert ((unsigned int)v->length == strlen (v->name)); /* bird */
    1118   val = variable_append (v->name, strlen (v->name), current_variable_set_list);
     1106  assert ((unsigned int)v->length == strlen (v->name)); /* bird */
     1107  val = variable_append (v->name, strlen (v->name), /** @todo optimize by using v->length! */
     1108                         current_variable_set_list, 1);
    11191109  variable_buffer_output (val, "", 1);
    11201110  val = variable_buffer;
Note: See TracChangeset for help on using the changeset viewer.