Ignore:
Timestamp:
May 23, 2007, 7:31:19 AM (18 years ago)
Author:
bird
Message:

Merged with the 2007-05-23 CVS. Added rsort and fixed a couple of windows build issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmakenew/rule.c

    r503 r903  
    1818
    1919#include "make.h"
     20
     21#include <assert.h>
     22
    2023#include "dep.h"
    2124#include "filedef.h"
     
    2528#include "rule.h"
    2629
    27 static void freerule PARAMS ((struct rule *rule, struct rule *lastrule));
     30static void freerule (struct rule *rule, struct rule *lastrule);
    2831
    2932
     
    7275  char *name;
    7376  int namelen;
    74   register struct rule *rule, *lastrule;
     77  struct rule *rule, *lastrule;
    7578
    7679  num_pattern_rules = max_pattern_targets = max_pattern_deps = 0;
     
    8487    {
    8588      unsigned int ndeps = 0;
    86       register struct dep *dep;
     89      struct dep *dep;
    8790      struct rule *next = rule->next;
    88       unsigned int ntargets;
    8991
    9092      ++num_pattern_rules;
    9193
    92       ntargets = 0;
    93       while (rule->targets[ntargets] != 0)
    94         ++ntargets;
    95 
    96       if (ntargets > max_pattern_targets)
    97         max_pattern_targets = ntargets;
     94      if (rule->num > max_pattern_targets)
     95        max_pattern_targets = rule->num;
    9896
    9997      for (dep = rule->deps; dep != 0; dep = dep->next)
     
    102100
    103101#ifdef VMS
    104           char *p = strrchr (dep->name, ']');
    105           char *p2;
     102          const char *p = strrchr (dep->name, ']');
     103          const char *p2;
    106104          if (p == 0)
    107105            p = strrchr (dep->name, ':');
    108106          p2 = p != 0 ? strchr (dep->name, '%') : 0;
    109107#else
    110           char *p = strrchr (dep->name, '/');
    111           char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
     108          const char *p = strrchr (dep->name, '/');
     109          const char *p2 = p != 0 ? strchr (dep->name, '%') : 0;
    112110#endif
    113111          ndeps++;
     
    124122              if (p - dep->name > namelen)
    125123                {
    126                   if (name != 0)
    127                     free (name);
    128124                  namelen = p - dep->name;
    129                   name = (char *) xmalloc (namelen + 1);
     125                  name = xrealloc (name, namelen + 1);
    130126                }
    131               bcopy (dep->name, name, p - dep->name);
     127              memcpy (name, dep->name, p - dep->name);
    132128              name[p - dep->name] = '\0';
    133129
     
    162158
    163159static void
    164 convert_suffix_rule (char *target, char *source, struct commands *cmds)
    165 {
    166   char *targname, *targpercent, *depname;
    167   char **names, **percents;
     160convert_suffix_rule (const char *target, const char *source,
     161                     struct commands *cmds)
     162{
     163  const char **names, **percents;
    168164  struct dep *deps;
    169   unsigned int len;
     165
     166  names = xmalloc (sizeof (const char *));
     167  percents = xmalloc (sizeof (const char *));
    170168
    171169  if (target == 0)
    172     /* Special case: TARGET being nil means we are defining a
    173        `.X.a' suffix rule; the target pattern is always `(%.o)'.  */
    174     {
     170    {
     171      /* Special case: TARGET being nil means we are defining a `.X.a' suffix
     172         rule; the target pattern is always `(%.o)'.  */
    175173#ifdef VMS
    176       targname = savestring ("(%.obj)", 7);
     174      *names = strcache_add_len ("(%.obj)", 7);
    177175#else
    178       targname = savestring ("(%.o)", 5);
     176      *names = strcache_add_len ("(%.o)", 5);
    179177#endif
    180       targpercent = targname + 1;
     178      *percents = *names + 1;
    181179    }
    182180  else
    183181    {
    184182      /* Construct the target name.  */
    185       len = strlen (target);
    186       targname = xmalloc (1 + len + 1);
    187       targname[0] = '%';
    188       bcopy (target, targname + 1, len + 1);
    189       targpercent = targname;
    190     }
    191 
    192   names = (char **) xmalloc (2 * sizeof (char *));
    193   percents = (char **) alloca (2 * sizeof (char *));
    194   names[0] = targname;
    195   percents[0] = targpercent;
    196   names[1] = percents[1] = 0;
     183      unsigned int len = strlen (target);
     184      char *p = alloca (1 + len + 1);
     185      p[0] = '%';
     186      memcpy (p + 1, target, len + 1);
     187      *names = strcache_add_len (p, len + 1);
     188      *percents = *names;
     189    }
    197190
    198191  if (source == 0)
     
    201194    {
    202195      /* Construct the dependency name.  */
    203       len = strlen (source);
    204       depname = xmalloc (1 + len + 1);
    205       depname[0] = '%';
    206       bcopy (source, depname + 1, len + 1);
     196      unsigned int len = strlen (source);
     197      char *p = alloca (1 + len + 1);
     198      p[0] = '%';
     199      memcpy (p + 1, source, len + 1);
    207200      deps = alloc_dep ();
    208       deps->name = depname;
    209     }
    210 
    211   create_pattern_rule (names, percents, 0, deps, cmds, 0);
     201      deps->name = strcache_add_len (p, len + 1);
     202    }
     203
     204  create_pattern_rule (names, percents, 1, 0, deps, cmds, 0);
    212205}
    213206
    214207/* Convert old-style suffix rules to pattern rules.
    215    All rules for the suffixes on the .SUFFIXES list
    216    are converted and added to the chain of pattern rules.  */
     208   All rules for the suffixes on the .SUFFIXES list are converted and added to
     209   the chain of pattern rules.  */
    217210
    218211void
    219212convert_to_pattern (void)
    220213{
    221   register struct dep *d, *d2;
    222   register struct file *f;
    223   register char *rulename;
    224   register unsigned int slen, s2len;
    225 
    226   /* Compute maximum length of all the suffixes.  */
     214  struct dep *d, *d2;
     215  char *rulename;
     216
     217  /* We will compute every potential suffix rule (.x.y) from the list of
     218     suffixes in the .SUFFIXES target's dependencies and see if it exists.
     219     First find the longest of the suffixes.  */
    227220
    228221  maxsuffix = 0;
    229222  for (d = suffix_file->deps; d != 0; d = d->next)
    230223    {
    231       register unsigned int namelen = strlen (dep_name (d));
    232       if (namelen > maxsuffix)
    233         maxsuffix = namelen;
    234     }
    235 
    236   rulename = (char *) alloca ((maxsuffix * 2) + 1);
     224      unsigned int l = strlen (dep_name (d));
     225      if (l > maxsuffix)
     226        maxsuffix = l;
     227    }
     228
     229  /* Space to construct the suffix rule target name.  */
     230  rulename = alloca ((maxsuffix * 2) + 1);
    237231
    238232  for (d = suffix_file->deps; d != 0; d = d->next)
    239233    {
     234      unsigned int slen;
     235
    240236      /* Make a rule that is just the suffix, with no deps or commands.
    241237         This rule exists solely to disqualify match-anything rules.  */
    242       convert_suffix_rule (dep_name (d), (char *) 0, (struct commands *) 0);
    243 
    244       f = d->file;
    245       if (f->cmds != 0)
     238      convert_suffix_rule (dep_name (d), 0, 0);
     239
     240      if (d->file->cmds != 0)
    246241        /* Record a pattern for this suffix's null-suffix rule.  */
    247         convert_suffix_rule ("", dep_name (d), f->cmds);
    248 
    249       /* Record a pattern for each of this suffix's two-suffix rules.  */
     242        convert_suffix_rule ("", dep_name (d), d->file->cmds);
     243
     244      /* Add every other suffix to this one and see if it exists as a
     245         two-suffix rule.  */
    250246      slen = strlen (dep_name (d));
    251       bcopy (dep_name (d), rulename, slen);
     247      memcpy (rulename, dep_name (d), slen);
     248
    252249      for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
    253250        {
     251          struct file *f;
     252          unsigned int s2len;
     253
    254254          s2len = strlen (dep_name (d2));
    255255
     256          /* Can't build something from itself.  */
    256257          if (slen == s2len && streq (dep_name (d), dep_name (d2)))
    257258            continue;
    258259
    259           bcopy (dep_name (d2), rulename + slen, s2len + 1);
     260          memcpy (rulename + slen, dep_name (d2), s2len + 1);
    260261          f = lookup_file (rulename);
    261262          if (f == 0 || f->cmds == 0)
     
    265266            /* A suffix rule `.X.a:' generates the pattern rule `(%.o): %.X'.
    266267               It also generates a normal `%.a: %.X' rule below.  */
    267             convert_suffix_rule ((char *) 0, /* Indicates `(%.o)'.  */
     268            convert_suffix_rule (NULL, /* Indicates `(%.o)'.  */
    268269                                 dep_name (d),
    269270                                 f->cmds);
     
    277278
    278279
    279 /* Install the pattern rule RULE (whose fields have been filled in)
    280    at the end of the list (so that any rules previously defined
    281    will take precedence).  If this rule duplicates a previous one
    282    (identical target and dependencies), the old one is replaced
    283    if OVERRIDE is nonzero, otherwise this new one is thrown out.
    284    When an old rule is replaced, the new one is put at the end of the
    285    list.  Return nonzero if RULE is used; zero if not.  */
    286 
    287 int
     280/* Install the pattern rule RULE (whose fields have been filled in) at the end
     281   of the list (so that any rules previously defined will take precedence).
     282   If this rule duplicates a previous one (identical target and dependencies),
     283   the old one is replaced if OVERRIDE is nonzero, otherwise this new one is
     284   thrown out.  When an old rule is replaced, the new one is put at the end of
     285   the list.  Return nonzero if RULE is used; zero if not.  */
     286
     287static int
    288288new_pattern_rule (struct rule *rule, int override)
    289289{
    290   register struct rule *r, *lastrule;
    291   register unsigned int i, j;
     290  struct rule *r, *lastrule;
     291  unsigned int i, j;
    292292
    293293  rule->in_use = 0;
     
    299299  lastrule = 0;
    300300  for (r = pattern_rules; r != 0; lastrule = r, r = r->next)
    301     for (i = 0; rule->targets[i] != 0; ++i)
     301    for (i = 0; i < rule->num; ++i)
    302302      {
    303         for (j = 0; r->targets[j] != 0; ++j)
     303        for (j = 0; j < r->num; ++j)
    304304          if (!streq (rule->targets[i], r->targets[j]))
    305305            break;
    306         if (r->targets[j] == 0)
    307           /* All the targets matched.  */
     306        /* If all the targets matched...  */
     307        if (j == r->num)
    308308          {
    309             register struct dep *d, *d2;
     309            struct dep *d, *d2;
    310310            for (d = rule->deps, d2 = r->deps;
    311311                 d != 0 && d2 != 0; d = d->next, d2 = d2->next)
     
    363363install_pattern_rule (struct pspec *p, int terminal)
    364364{
    365   register struct rule *r;
     365  struct rule *r;
    366366  char *ptr;
    367367
    368   r = (struct rule *) xmalloc (sizeof (struct rule));
    369 
    370   r->targets = (char **) xmalloc (2 * sizeof (char *));
    371   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
    372   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
    373 
    374   r->targets[1] = 0;
    375   r->suffixes[1] = 0;
    376   r->lens[1] = 0;
     368  r = xmalloc (sizeof (struct rule));
     369
     370  r->num = 1;
     371  r->targets = xmalloc (sizeof (const char *));
     372  r->suffixes = xmalloc (sizeof (const char *));
     373  r->lens = xmalloc (sizeof (unsigned int));
    377374
    378375  r->lens[0] = strlen (p->target);
    379   /* These will all be string literals, but we malloc space for
    380      them anyway because somebody might want to free them later on.  */
    381   r->targets[0] = savestring (p->target, r->lens[0]);
    382   r->suffixes[0] = find_percent (r->targets[0]);
    383   if (r->suffixes[0] == 0)
    384     /* Programmer-out-to-lunch error.  */
    385     abort ();
    386   else
    387     ++r->suffixes[0];
     376  r->targets[0] = p->target;
     377  r->suffixes[0] = find_percent_cached (&r->targets[0]);
     378  assert (r->suffixes[0] != NULL);
     379  ++r->suffixes[0];
    388380
    389381  ptr = p->dep;
     
    395387    {
    396388      r->terminal = terminal;
    397       r->cmds = (struct commands *) xmalloc (sizeof (struct commands));
     389      r->cmds = xmalloc (sizeof (struct commands));
    398390      r->cmds->fileinfo.filenm = 0;
    399391      r->cmds->fileinfo.lineno = 0;
     
    414406{
    415407  struct rule *next = rule->next;
    416   register unsigned int i;
    417   register struct dep *dep;
    418 
    419   for (i = 0; rule->targets[i] != 0; ++i)
    420     free (rule->targets[i]);
     408  struct dep *dep;
    421409
    422410  dep = rule->deps;
    423411  while (dep)
    424412    {
    425       struct dep *t;
    426 
    427       t = dep->next;
    428       /* We might leak dep->name here, but I'm not sure how to fix this: I
    429          think that pointer might be shared (e.g., in the file hash?)  */
    430       dep->name = 0; /* Make sure free_dep does not free name.  */
     413      struct dep *t = dep->next;
    431414      free_dep (dep);
    432415      dep = t;
    433416    }
    434417
    435   free ((char *) rule->targets);
    436   free ((char *) rule->suffixes);
    437   free ((char *) rule->lens);
     418  free (rule->targets);
     419  free (rule->suffixes);
     420  free (rule->lens);
    438421
    439422  /* We can't free the storage for the commands because there
     
    448431       pointer from the `struct file' for the suffix rule.  */
    449432
    450   free ((char *) rule);
     433  free (rule);
    451434
    452435  if (pattern_rules == rule)
     
    462445
    463446
    464 /* Create a new pattern rule with the targets in the nil-terminated
    465    array TARGETS.  If TARGET_PERCENTS is not nil, it is an array of
    466    pointers into the elements of TARGETS, where the `%'s are.
    467    The new rule has dependencies DEPS and commands from COMMANDS.
     447/* Create a new pattern rule with the targets in the nil-terminated array
     448   TARGETS.  TARGET_PERCENTS is an array of pointers to the % in each element
     449   of TARGETS.  N is the number of items in the array (not counting the nil
     450   element).  The new rule has dependencies DEPS and commands from COMMANDS.
    468451   It is a terminal rule if TERMINAL is nonzero.  This rule overrides
    469452   identical rules with different commands if OVERRIDE is nonzero.
    470453
    471    The storage for TARGETS and its elements is used and must not be freed
    472    until the rule is destroyed.  The storage for TARGET_PERCENTS is not used;
    473    it may be freed.  */
     454   The storage for TARGETS and its elements and TARGET_PERCENTS is used and
     455   must not be freed until the rule is destroyed.  */
    474456
    475457void
    476 create_pattern_rule (char **targets, char **target_percents,
    477                     int terminal, struct dep *deps,
     458create_pattern_rule (const char **targets, const char **target_percents,
     459                     unsigned int n, int terminal, struct dep *deps,
    478460                     struct commands *commands, int override)
    479461{
    480   unsigned int max_targets, i;
    481   struct rule *r = (struct rule *) xmalloc (sizeof (struct rule));
    482 
     462  unsigned int i;
     463  struct rule *r = xmalloc (sizeof (struct rule));
     464
     465  r->num = n;
    483466  r->cmds = commands;
    484467  r->deps = deps;
    485468  r->targets = targets;
    486 
    487   max_targets = 2;
    488   r->lens = (unsigned int *) xmalloc (2 * sizeof (unsigned int));
    489   r->suffixes = (char **) xmalloc (2 * sizeof (char *));
    490   for (i = 0; targets[i] != 0; ++i)
    491     {
    492       if (i == max_targets - 1)
    493         {
    494           max_targets += 5;
    495           r->lens = (unsigned int *)
    496             xrealloc ((char *) r->lens, max_targets * sizeof (unsigned int));
    497           r->suffixes = (char **)
    498             xrealloc ((char *) r->suffixes, max_targets * sizeof (char *));
    499         }
     469  r->suffixes = target_percents;
     470  r->lens = xmalloc (n * sizeof (unsigned int));
     471
     472  for (i = 0; i < n; ++i)
     473    {
    500474      r->lens[i] = strlen (targets[i]);
    501       r->suffixes[i] = (target_percents == 0 ? find_percent (targets[i])
    502                         : target_percents[i]) + 1;
    503       if (r->suffixes[i] == 0)
    504         abort ();
    505     }
    506 
    507   if (i < max_targets - 1)
    508     {
    509       r->lens = (unsigned int *) xrealloc ((char *) r->lens,
    510                                            (i + 1) * sizeof (unsigned int));
    511       r->suffixes = (char **) xrealloc ((char *) r->suffixes,
    512                                         (i + 1) * sizeof (char *));
     475      assert (r->suffixes[i] != NULL);
     476      ++r->suffixes[i];
    513477    }
    514478
     
    523487print_rule (struct rule *r)
    524488{
    525   register unsigned int i;
    526   register struct dep *d;
    527 
    528   for (i = 0; r->targets[i] != 0; ++i)
     489  unsigned int i;
     490  struct dep *d;
     491
     492  for (i = 0; i < r->num; ++i)
    529493    {
    530494      fputs (r->targets[i], stdout);
    531       if (r->targets[i + 1] != 0)
    532         putchar (' ');
    533       else
    534         putchar (':');
     495      putchar ((i + 1 == r->num) ? ':' : ' ');
    535496    }
    536497  if (r->terminal)
     
    548509print_rule_data_base (void)
    549510{
    550   register unsigned int rules, terminal;
    551   register struct rule *r;
     511  unsigned int rules, terminal;
     512  struct rule *r;
    552513
    553514  puts (_("\n# Implicit Rules"));
Note: See TracChangeset for help on using the changeset viewer.