Commit Graph

327 Commits

Author SHA1 Message Date
rillig
bb2ba6a0e5 make(1): rename LstNode functions to match their type 2020-08-29 10:41:12 +00:00
rillig
f6a7d0bc31 make(1): rename Lst_FindB back to Lst_Find
The migration from "comparison function" to "match function" is done,
the "B" in the names is no longer needed.
2020-08-29 10:12:06 +00:00
rillig
d1c1d9370a make(1): start replacing Lst_Find with Lst_FindB
Lst_Find is called with a "comparison" function that returns the integer
0 if the desired node is found.  This leads to confusion since there are
so many different return value conventions for int, such as 0/1 for
mimicking false/true, -1/0 as in close(2), and the sign as in strcmp(3).
This API is much easier to understand if the "comparison" function is
not called a comparison function (since that is too close to strcmp),
but a "match" function that just returns a boolean.

In Lst_FindFromB, the node argument may be null.  This deviates from the
other Lst functions, which require Lst and LstNode to generally be
non-null.  In this case it is useful though to make the calling code
simpler.

In arch.c, this makes a lot of the previous documentation redundant.

In cond.c, the documentation is reduced a little bit since it had
already been cleaned up before.  It also removes the strange negation
from CondFindStrMatch.

In dir.c, the documentation collapses as well.

In main.c, separating the ReadMakefile function from the callbacks for
Lst_FindB allows the former to get back its natural function signature,
with proper types and no unused parameters.

To catch any accidental mistakes during the migration from Lst_Find to
Lst_FindB, the code can be compiled with -DUSE_DOUBLE_BOOLEAN, which
will complain about incompatible function pointer types.
2020-08-29 09:30:10 +00:00
rillig
793f3a81dd make(1): fix null pointer dereference when sys.mk is not found
This is quite hard to trigger in a real-life scenario since it requires
precise timing.

Instead, I created /tmp/sys.mk and ran "./make -m /tmp -f /dev/null" in
the debugger, after setting a breakpoint in this line:

ln = Lst_Find(sysMkPath, ReadMakefile, NULL);

Once this line was reached, I removed /tmp/sys.mk to make ReadMakefile
fail.  Just adding a few parse errors won't help since ReadMakefile only
fails if the file cannot be found.
2020-08-29 08:59:08 +00:00
rillig
5783a5d404 make(1): allow for strict type checking for Boolean
Having Boolean aliased to int creates ambiguities since int is widely
used.  Allow to occasionally compile make with -DUSE_DOUBLE_BOOLEAN to
check that the type definitions still agree.
2020-08-29 07:52:55 +00:00
rillig
7c1ae9cea7 make(1): clean up code in make.c
Var_Subst never returns NULL.

In Main_ExportMAKEFLAGS, don't compare ints with booleans.

In MainParseArgs, use char for the current character.  First, that's
more precise and correct, and second, it makes debugging easier for
those who don't know the ASCII table by heart.
2020-08-29 07:13:17 +00:00
rillig
da6468d597 make(1): rename confusing function ReadAllMakefiles
The old name implied that the function would read multiple files, which
was not the case.

The comment above that function was highly confusing.  It's not that the
function returns a boolean, but rather 0 or non-zero, and 0 means that
Lst_Find should stop searching.

One of the next refactorings will be to make Lst_Find return the first
list node for which the function returns TRUE.  This will reduce the
confusion about the several functions called SomethingP in suff.c.  The
P suffix means to return TRUE or FALSE, not 0 or non-zero.
2020-08-29 07:05:12 +00:00
rillig
942d06c278 make(1): remove trailing 'S' from names of Lst functions
The migration from null-passing Lst functions to argument-checking Lst
functions is completed.

There were 2 surprises: The targets list may be NULL, and in Dir_AddDir,
the path may be NULL.  The latter case is especially surprising since
that function turns into an almost-nop in that case.  This is another
case where probably 2 independent functions have been squeezed into a
single function.  This may be improved in a follow-up commit.

All other lists were fine.  They were always defined and thus didn't
need much work.
2020-08-28 04:48:56 +00:00
rillig
4acfd85d5e make(1): migrate Lst_Find to Lst_FindS 2020-08-28 04:28:45 +00:00
rillig
4b307a6103 make(1): migrate Lst_First to Lst_FirstS 2020-08-28 04:14:31 +00:00
rillig
74fd7ff4cd make(1): migrate Lst_IsEmpty to Lst_IsEmptyS 2020-08-27 19:15:35 +00:00
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
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
1c84dcceb3 make(1): fix obvious bugs in -DCLEANUP mode
The outdated type name FreeProc had been renamed to LstFreeProc.
Casting the function free to it is not necessary since the type of this
function is already exactly the correct type.  Anything else would be
undefined behavior anyway.

The uninitialized sufflist in Suff_ClearSuffixes was ok until now
because the Lst functions had silently skipped any calls with invalid
arguments.  This silent skipping is a good argument to have strict
argument validation since it detects these unintended control flows.
2020-08-25 16:50:02 +00:00
rillig
38885a7509 make(1): make brk_string return size_t for the number of words 2020-08-23 18:26:35 +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
812ca946e8 make(1): remove unused variable jobServer 2020-08-22 18:47:31 +00:00
rillig
885e5a1b8a make(1): fix indentation 2020-08-22 17:34:25 +00:00
rillig
4ea2d96bef make(1): replace Lst_Datum with non-null guaranteeing Lst_DatumS 2020-08-22 15:17:09 +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
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
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
f271e31bca make(1): replace snprintf/malloc in ReadMakefile with str_concat3 2020-08-11 18:52:03 +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
0eefc20b94 make(1): clean up indentation, includes, add documentation 2020-08-09 09:44:14 +00:00
rillig
667ca31b65 make(1): remove outdated comment 2020-08-09 09:27:44 +00:00
rillig
935ed8f6f3 make(1): avoid undefined behavior in Cmd_Exec
Iterating the command output backwards was dangerous since at the end,
the pointer cp pointed outside of the array.  Even without dereferencing
this pointer, this already invokes undefined behavior (C11, 6.5.6p8).
Don't risk anything.  Iterating forwards is probably faster anyway, since
it is more common.
2020-08-09 09:26:21 +00:00
rillig
088c26ed6d make(1): remove redundant assignment from Cmd_Exec
A Buffer is always null-terminated.
2020-08-09 09:07:54 +00:00
rillig
ef3f5c5505 make(1): split local variable in Cmd_Exec into two
This avoids a mismatch between signed and unsigned types.
2020-08-09 09:01:29 +00:00
rillig
26e41a6045 make(1): remove trailing Z from buffer functions
This Z had been useful during the migration from int to size_t.  This
migration is finished, at least for the Buffer type, so the Z is no
longer necessary.
2020-08-08 18:54:04 +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
7f7a68cac6 make(1): replace one instance of strncpy with snprintf
GCC 9 incorrectly claims that the string might not be null-terminated.
Since objdir is a global variable, it is initialized to zero, and the +1
in the size guarantees that this byte is always 0.

Still, using strncpy to initialize a string is a waste of memory access,
since it is enough if only the actual data is copied, without zeroing
out all the remaining bytes.
2020-08-02 08:06:35 +00:00
rillig
45b10be806 make(1): switch Buffer size from int to size_t
This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.

The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.

All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len.  These changes have been reviewed
thoroughly and manually, like all the others in this commit.

Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten.  They will be renamed back in a follow-up commit.

As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.

The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.
2020-08-01 21:40:49 +00:00
rillig
a99e4a0c7c make(1): fix parameter name in Cmd_Exec
A format string is not a number.
2020-08-01 17:45:32 +00:00
rillig
2de1dde5a2 make(1): use ordinary string concatenation in usage text 2020-08-01 14:52:14 +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
ad6695e0a6 make(1): avoid calls to free(3) in the common case of a NULL pointer 2020-08-01 09:55:00 +00:00
rillig
ab2bfed61f make(1): let Var_Value return a const char *
The return value must not be modified anyway, so let the compiler check
this for free.
2020-08-01 09:25:36 +00:00
rillig
dbccf55bb9 make(1): make Main_SetVarObjdir const-correct and use separate variables
The return value of Var_Value should be const char *, but changing that
now would be too large a change.
2020-08-01 09:08:17 +00:00
rillig
9998f9a6ae make(1): merge duplicate code for concatenating strings 2020-08-01 08:55:28 +00:00
rillig
5b6403d328 make(1): eliminate unsatisfiable condition in is_relpath 2020-08-01 08:49:47 +00:00
sjg
a826e8b769 Add -dL for LINT
When parsing variable assignments other than := and if
value contains '$' attempt Var_Subst the same as for :=,
if the value does not parse correctly, we get a fatal error
including file an line number.

This can greatly help with finding the cause of problems.

Reviewed by: christos
2020-07-31 20:22:10 +00:00
rillig
5ca4f23a1e make(1): remove dead code from Var_Subst
The first parameter from Var_Subst had been a literal NULL in all cases.
These have been fixed using this command:

sed -i 's|Var_Subst(NULL, |Var_Subst(|' *.c

The one remaining case was not found because the "NULL," was followed by
a line break instead of a space.

The removed code probably wouldn't have worked as expected anyway.
Expanding a single variable to a literal string would have led to
unexpected behavior for cases like ${VAR:M${pattern}}, in case pattern
would contain an unescaped ':' itself.
2020-07-28 16:42:22 +00:00
sjg
bfa35b9119 Make DEBUG_HASH less of a fire-hose.
Reporting keys on every lookup is overkill unless
playing with a new HASH, so wrap in #ifdef DEBUG_HASH_LOOKUP
Also add some stats at the end so we can see
final size and max chain length - maxchain is a better
variable name than maxlen.
2020-07-20 18:12:48 +00:00
rillig
e60b891d45 make(1): clean up unnecessary snprintf and multi-line function calls 2020-07-19 12:35:30 +00:00
rillig
2b74fdb213 make(1): rename Varf_Flags to VarEvalFlags
In var.c there are lots of different flag types.  To make any accidental
mixture obvious, each flag group gets its own prefix.

The only flag group that is visible outside of var.c is concerned with
evaluating variables, therefore the "e", which replaces the former "f"
that probably just meant "flag".
2020-07-19 12:26:17 +00:00
sjg
6625c883c8 Add -dh for DEBUG_HASH
Allow tracking of max chain length, to see how well the hash
tables are working.
Pull the actual hash operation into a marco so it can be
easily changed - for experimenting.

The current hash, is pretty good.

Reviewed by: christos
2020-07-18 21:37:38 +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