Commit Graph

114 Commits

Author SHA1 Message Date
rillig
b855b10134 make(1): migrate Lst_Succ to Lst_SuccS 2020-08-27 07:00:29 +00:00
rillig
6c196ebe9c make(1): migrate Lst_ForEach to Lst_ForEachS
Most lists are always valid.  Only the "targets" variable may be null in
some cases, probably.
2020-08-27 06:53:57 +00:00
rillig
760b7512c6 make(1): migrate remaining code from Lst_Open to Lst_OpenS 2020-08-27 06:28:44 +00:00
rillig
4862a68b32 make(1): add stricter variants for remaining Lst functions
In most cases the Lst functions are only called when the arguments are
indeed valid.  It's not guaranteed though, therefore each function call
needs to be analyzed and converted individually.

While here, remove a few statements that were only useful when the Lst
functions handled circular lists.
2020-08-26 22:55:46 +00:00
rillig
27d66938d5 make(1): reverse order of the Lst_Find parameters
The other callbacks all have (function, param), only the Lst_Find had
(param, function), which was inconsistent.
2020-08-23 16:58:02 +00:00
rillig
43c60c497e make(1): migrate Lst_AtFront to Lst_PrependS
This makes Lst_AtFront unused, as well as LstInsertBefore.
2020-08-22 23:06:51 +00:00
rillig
e288063ccd make(1): replace Lst_Duplicate with Lst_CopyS
Lst_Duplicate would have passed through any null pointer, which was not
needed for make.  It was the last function that used Lst_AtEnd, which in
turn was the last function that used LstInsertAfter.  As a result, these
two functions have been removed.
2020-08-22 22:57:53 +00:00
rillig
c145566082 make(1): use Lst_OpenS in Dir_SetPATH
Since dirSearchPath is initialized in Dir_Init, opening the list can
never fail.
2020-08-22 19:57:43 +00:00
rillig
885e5a1b8a make(1): fix indentation 2020-08-22 17:34:25 +00:00
rillig
68f9487da7 make(1): extract percentage calculation out of Dir_PrintDirectories 2020-08-22 15:55:22 +00:00
rillig
f724405abb make(1): require argument of Lst_Member to be non-null
Since the lists don't contain null pointers, it doesn't make sense to
search for a null pointer.  All calls but one already had obviously
non-null arguments.  The one remaining call using targ->suff has been
guarded for now.

The code for Lst_Member became much simpler than before.  Partly because
the old code had an extra condition for circular lists, which are not
used by make.
2020-08-22 15:43:32 +00:00
rillig
4ea2d96bef make(1): replace Lst_Datum with non-null guaranteeing Lst_DatumS 2020-08-22 15:17:09 +00:00
rillig
182c9e03db make(1): convert Lst_Enqueue and Lst_Dequeue to nonnull variants
Except for once instance in parse.c, the usage pattern for Lst_Dequeue
was to first test whether the list is empty.  This pattern allowed the
implementation of Lst_Dequeue to become simpler since the null check is
not needed anymore.

The calls to Lst_Enqueue never pass an invalid list or a null pointer,
therefore making them strict was trivial.
2020-08-22 14:39:12 +00:00
rillig
bbc184419a make(1): remove wrong comment for Dir_Init
Neither the list nor the hash table modules have anything to do with
opening directories.
2020-08-22 14:04:22 +00:00
rillig
e997d2617b make(1): replace "(void)Lst_AtEnd" with stricter "Lst_AppendS"
This change ensures that there is actually something added to the list.
Lst_AtEnd had silently skipped the addition if the list was invalid
(null pointer), which was not intended in these cases.  The "(void)" is
assumed to mean "I know that this cannot fail", while it could also mean
"I don't care whether something actually happened".

Running "./build.sh -j6 tools" still succeeds after this change,
therefore chances are very low that this change breaks anything.  If
there is any change, it's an obvious assertion failure.  There is no
silent change in behavior though.
2020-08-22 11:35:00 +00:00
rillig
a4890d871b make(1): add Lst_Append to add an item at the end of the list
The previous variant of using a special case of Lst_InsertAfter was
unnecessarily complicated.  Linked lists are a very basic data
structure, and there is no need to overcomplicate things by introducing
unnecessary conditions and branches.
2020-08-22 09:40:18 +00:00
rillig
76aca411e7 make(1): remove unused return value for DirMatchFiles 2020-08-22 09:03:53 +00:00
rillig
956e4defda make(1): split Dir_Init into two functions
There's just no point in having a function consisting of a big
if-then-else.
2020-08-22 00:48:02 +00:00
rillig
82a6520aed make(1): use stricter list API for sequential access
In several places, it just doesn't make sense to have a null pointer
when a list is expected.

In the existing unit tests, the list passed to Lst_Open is always valid,
but that's not a guarantee for real-world usage.  Therefore, Lst_Open
has been left for now, and Lst_OpenS is only the preferred alternative
to it.
2020-08-21 04:42:02 +00:00
rillig
2825a464f9 make(1): assert correct usage of the Lst_Open API
All calls to Lst_Next are properly protected by Lst_Open, so there is no
possible assertion failure here.
2020-08-21 04:09:12 +00:00
rillig
7b0da2fa03 make(1): make list library code stricter
Up to now, the list library didn't distinguish between programming
mistakes (violations of invariants, illegal parameter values) and
actually interesting situations like "element not found in list".

The current code contains many branches for conditions that are neither
exercised by the unit tests nor by real-world usage.  There is no point
in keeping this unnecessary code.

The list functions will be migrated from their lenient variants to the
stricter variants in small parts, each function getting the S suffix
when it is made strict, to avoid any confusion about how strict a
particular function is.  When all functions have been migrated, they
will be renamed back to their original names.

While here, the comments of the functions are cleaned up since they
mention irrelevant implementation details in the API comments, as well
as "side effects" that are really main effects.
2020-08-21 03:36:03 +00:00
rillig
7f136885e3 make(1): remove unused code for circular lists
The list library had probably been imported from a general-purpose
library that also supported circular lists.  These are not used by make
though.

After replacing Lst_Init(FALSE) with Lst_Init(), only a single call to
Lst_Init remained with a non-constant argument, and that was in
Lst_Concat, which was to be expected.
2020-08-21 02:20:47 +00:00
rillig
d4d42bc70a make(1): clean up debug logging in dir.c
In C90, the variadic macro argument __VA_ARGS__ is not known, therefore
fall back to the forms listing the number of actual printf arguments.
2020-08-13 03:33:56 +00:00
rillig
4ff1f1ac37 make(1): avoid negated conditions in DirExpandCurly 2020-08-13 03:07:49 +00:00
rillig
ef9eb65a77 make(1): clean up DirExpandCurly
Now that nested curly braces work as expected and are covered by unit
tests, the debug log is no longer necessary.
2020-08-13 03:00:44 +00:00
rillig
022d5c4ff0 make(1): use enum instead of #define for cached_stats 2020-08-13 02:53:15 +00:00
rillig
6abaf3bbe5 make(1): make Dir_MakeFlags simpler
This avoids unnecessary string allocations, especially for long lists.

There is no unit test to cover this code.  Since this is an obscure,
undocumented part of make, I wasn't able to come up with a unit test.
Therefore I resorted to write a separate testing program to verify the
expected results.

$ cat <<'EOF' >dir_test.c
#include <stdio.h>

#include "make.h"
#include "dir.h"

int main(int argc, char **argv)
{
    int i;
    Lst lst;
    char *flags;

    lst = Lst_Init(FALSE);
    for (i = 1; i < argc; i++)
    	Dir_AddDir(lst, argv[i]);
    flags = Dir_MakeFlags("-I", lst);

    printf("%s\n", flags);
    free(flags);
    return 0;
}
EOF

# Needs some trivial patches to Makefile and main.c
$ make PROG=dir_test EXTRA_SRCS=dir_test.c EXTRA_CPPFLAGS=-DNO_MAIN \
       COPTS.main.c=-Wno-unused-function NOMAN=1

$ ./dir_test a b c

$ mkdir a b c
$ ./dir_test a b c
 -Ia -Ib -Ic
$ rmdir a b c
2020-08-12 03:05:57 +00:00
rillig
644e5ad76b make(1): replace str_concat with str_concat2 and str_concat3
The new functions have a simpler interface, and str_concat3 is even more
general-purpose, since the middle string is no longer required to be
exactly of length 1.
2020-08-10 19:53:19 +00:00
rillig
21107e4e40 make(1): fix parameter name of str_concat
The previous documentation mentioned Str_Concat, but str_concat has been
written in lowercase for years.  The "flags" are not flags since they
cannot be combined, not even when they are written in hex.
2020-08-10 19:30:30 +00:00
rillig
173c978875 make(1): format the source code consistently, at least per file
Some files use 4 spaces per indentation level, others use 8.  At least
for the few files from this commit, they use a consistent style
throughout each file now.

In Cond_Eval, the #define has changed into an enum since the identifiers
need not be visible to the C preprocessor.
2020-08-09 19:51:02 +00:00
rillig
86abf69ed1 make(1): no declaration-after-statement anymore
NetBSD make is intended to be maximally portable, therefore it uses only
C89.  This was not declared in the Makefile before.

There are still a few places in parse.c and metachar.c that use
end-of-line comments.  These will be fixed in a follow-up commit.
2020-08-03 20:26:09 +00:00
rillig
bdd5655235 make(1): use consistent indentation in source code
Tabs for multiples of 8, then spaces.

The usage string has been kept as-is since the spaces there are
indentional and do influence the output.
2020-08-01 14:47:49 +00:00
rillig
ec9946662a make(1): document another instance of undefined behavior
The UNCONST macro is really terrible.

This segmentation fault can be forced by setting _PATH_DEFSYSMK in
pathnames.h to "./sys*.mk" or any other string that has a slash and a
wildcard to the right of the slash.
2020-07-31 20:57:38 +00:00
rillig
99ecf07de4 make(1): fix regression and add test for empty brace expansion
This had been broken in r1.80 of dir.c, a few minutes ago.
2020-07-31 20:02:44 +00:00
rillig
652ec6da3a make(1): refactor DirExpandCurly
Separating the low-level parts into small functions reduces the need for
summarizing comments between the code lines.

Using a consistent naming scheme for the variables and expressive names
makes the code easier to understand.  The number of variables has
increased from 7 to 11, their clearer names compensate for that, plus
the fact that they come in triples (x, x_end, x_len). Placing the
variables into appropriate registers and eliminating memory access is
left as an exercise to the compiler.
2020-07-31 19:50:44 +00:00
rillig
ac4f09141f make(1): fix parsing of nested braces in dependency lines
Before, make could not parse {{thi,fou}r,fif}teen properly. It did
correctly split up the outer brace into "" + "{thi,fou}r,fif" + "teen",
but then, when expanding the inner braces, it interpreted the first
comma already as a separator, even though this comma was enclosed in
another set of braces.

This resulted in the wrong expansion "{thiteen", which produced the
error message.  The next word "fouteen" was produced since the parser
stopped at the next closing brace.  After this, parsing continued after
the closing brace, producing "rteen".  Finally, the last expansion was
the correct "fifteen".
2020-07-31 19:06:33 +00:00
rillig
4efc2adc09 make(1): trying to understand how DirExpandCurly works 2020-07-31 17:41:35 +00:00
rillig
6dc55cec88 make(1): prefer memcpy over strncpy in DirExpandCurly
strncpy has unnecessary overhead when the source memory is already
guaranteed to contain no '\0'.
2020-07-31 16:59:34 +00:00
rillig
820b5caede make(1): remove trailing whitespace 2020-07-03 08:13:23 +00:00
rillig
44d841d2c5 make(1): remove redundant parentheses around return values 2020-07-03 08:02:55 +00:00
sjg
62b57d3e75 cached_stats: do not conflate stat and lstat
While make uses lstat quite rarely, it does so for reason.
Avoid confusing the results.

Reviewed by: christos
2020-06-05 18:03:59 +00:00
christos
3a280067df Fix previous: cached_stats() returning < 0 means that the file is not found,
not that it was found in the cache, and centralize reporting.
2018-07-12 18:03:31 +00:00
reinoud
38c2b03323 Remove duplicate code in make(1)'s dir.c.
When the cached_stats() code was added, some old logic stayed around that
implements the cached_stats() too.
2018-07-12 17:46:37 +00:00
riastradh
ea5fc7e0b0 Plug memory leaks in Dir_FindFile.
CID 978364
2017-04-16 21:14:47 +00:00
riastradh
9660a1b334 Use, don't kludge, MAKE_ATTR_UNUSED.
CID 1300234
CID 1300237
CID 1300238
CID 1300245
CID 1300255
CID 1300267
CID 1300284
2017-04-16 19:53:58 +00:00
sjg
5f2f8cfca3 Partially initialize Dir before MainParseArgs can be called.
The rest can be done once curdir is finalized.
2017-01-31 06:54:23 +00:00
sjg
e917da0d70 Extend the mtimes cache used by dir.c so it can be used by others.
We store both st_mtime and st_mode, since some callers care about the
later.

Reviewed by: christos
2016-06-07 00:40:00 +00:00
christos
ed82ee44b4 Add a .STALE special target that gets invoked when dependency files contain
stail entries.
2013-03-05 22:01:43 +00:00
christos
a11ae36917 Keep track of the location where a dependency is defined, so we can report
about it.
2013-03-05 02:04:10 +00:00
joerg
b1e1b1d4c2 Replace __dead, __unused and the various printf format attributes
with versions prefixed by MAKE_ATTR_* to avoid modifying the
implementation namespace. Make sure they are available in all places
using nonints.h to fix bootstrap on Linux.
2012-06-12 19:21:50 +00:00