Home | History | Annotate | Download | only in lint1
History log of /src/usr.bin/xlint/lint1/init.c
RevisionDateAuthorComments
 1.275  11-Jul-2025  rillig lint: fix warning about enum array index

Seen in openssh/fmt_scaled.c, scale_chars.
 1.274  04-May-2025  rillig lint: add more details to message about discarding qualifiers

The name of the function (or, if not available, its type) is useful in
lines that contain more than one function call.
 1.273  12-Apr-2025  rillig lint: reword messages that apply to traditional C
 1.272  12-Apr-2025  rillig lint: s/illegal/invalid/g

Lint does not provide legal advice.
 1.271  13-Nov-2024  rillig lint: add more details to 'statement not reached' message

In lib/libcompat/regexp/regexp.c, the FAIL macro expands to a compound
statement containing a function call statement and a return statement,
and the macro invocation is followed by a semicolon, forming an extra
empty statement. Which of these statements is unreachable now becomes
clear from the diagnostic, without having to inspect the preprocessed
source code.
 1.270  29-Aug-2024  rillig lint: support GCC's __auto_type

Fixes PR toolchain/58654.
 1.269  09-May-2024  rillig branches: 1.269.2;
lint: add details to message about too many initializers
 1.268  30-Mar-2024  rillig lint: do not convert array subscripts from size_t to ptrdiff_t

The C standards do not specify a fixed type for an array subscript, it
just has to be an integer type. Previously, query 4 fired for the
ubiquitous expression 'ptr[sz]' when sz had type 'size_t'.

The test platform_ilp32_long is unaffected by this change, as the
integer constant 0x80000000 has type 'unsigned int', while size_t is
'unsigned long' on those platforms, and even though the types 'unsigned
int' and 'unsigned long' have the same value space, there's still a
conversion, at least for now.
 1.267  30-Mar-2024  rillig lint: reword messages about array subscripts to sound more natural
 1.266  29-Mar-2024  rillig lint: clean up
 1.265  29-Mar-2024  rillig lint: fix wording of warning about bit-field initializer
 1.264  27-Mar-2024  rillig lint: don't use 'long' in diagnostics

The size of 'long' differs between 64-bit and 32-bit platforms.
Eliminate this possible platform-dependency.
 1.263  09-Mar-2024  rillig lint: inline accessor macros for tnode_t
 1.262  09-Mar-2024  rillig lint: inline accessor macros for type_t
 1.261  03-Mar-2024  rillig lint: clean up string parsing and snprintb check
 1.260  01-Mar-2024  rillig lint: fix misleading initializer for string iterator

The field 'start' marks the start of the previous matching character,
not the current iterator position.

No binary change.
 1.259  08-Feb-2024  rillig lint: clean up redundant braces

No functional change.
 1.258  03-Feb-2024  rillig lint: keep strings in their source representation

This allows further analysis depending on whether individual characters are
escaped as octal, hexadecimal or not at all.
 1.257  01-Feb-2024  rillig lint: use standard buffer for storing string values

No functional change.
 1.256  13-Jan-2024  rillig lint: clean up grammar for initializers
 1.255  11-Jan-2024  rillig lint: clean up enum constants for designators

In intializers and offsetof, both struct and union members are handled
in the same way, thus there is no need to distinguish them.
 1.254  09-Jan-2024  rillig lint: allow complex offsetof(type, member-designator)

Both GCC 11 and Clang 8 accept member-designators that are not
identifiers but designator sequences, such as in 'offsetof(struct stat,
st_atim.tv_sec)', so make lint accept them as well.
 1.253  10-Dec-2023  rillig lint: clean up comments

No functional change.
 1.252  03-Dec-2023  rillig lint: re-wrap comments

No functional change.
 1.251  02-Dec-2023  rillig lint: remove redundant empty lines

No functional change.
 1.250  30-Jul-2023  rillig lint: in debug mode, default to indenting the debug log

Only the 'parsing' lines are not indented, as line breaks are
independent from the structure of the code.
 1.249  21-Jul-2023  rillig lint: reword message about non-constant initializer
 1.248  15-Jul-2023  rillig lint: clean up duplicate code for finding struct/union members
 1.247  15-Jul-2023  rillig lint: add detailed logging for finding memory allocation bugs
 1.246  13-Jul-2023  rillig lint: indent copyright lines consistently
 1.245  10-Jul-2023  rillig lint: clean up wchar_t and hash tables
 1.244  01-Jul-2023  rillig lint: fix initialization of unnamed union member
 1.243  30-Jun-2023  rillig lint: fix handling of unnamed struct/union members

The support for unnamed struct/union members that was added in decl.c
1.60 from 2015-10-13 was simple but wrong. It didn't cover initializers
of these structures and computed wrong sizes for structures containing
anonymous unions. At that time, the handling of initializers was broken
as well, it was fixed 6 years later in init.c 1.229 from 2021-12-22.

Real-life examples for code that lint couldn't handle are:

* external/bsd/jemalloc/dist/src/jemalloc.c
* external/mit/xorg/lib/dri.old/Makefile
 1.242  22-May-2023  rillig lint: fix emitted type for arrays of unknown size
 1.241  22-Apr-2023  rillig lint: rename type_t.t_str to t_sou

The name 't_str' could have meant 'string' or 'struct'. Since both
struct and union types have members, rename it to 't_sou'.

No functional change.
 1.240  06-Feb-2023  rillig lint: eliminate unnecessary indirection

No functional change.
 1.239  06-Feb-2023  rillig lint: condense code for handling initializations

Remove the assertion for a non-null initialization, as each code path
immediately dereferences it.

No functional change.
 1.238  13-Jan-2023  rillig lint: remove custom memory allocator

Besides adding complexity, the custom memory allocator didn't invalidate
freed memory, which made it harder to find possible use-after-free bugs.
 1.237  28-Aug-2022  rillig lint: clean up visual clutter

No functional change.
 1.236  22-Jun-2022  rillig lint: add quotes around placeholders for the remaining messages

Reword some of the messages slightly, exchanging brevity for clarity.

Message 138 is kept as-is, as it is not yet covered by any tests.

Message 240 is kep as-is, as it is unreachable.
 1.235  20-May-2022  rillig lint: use __RCSID in lint mode as well

Since 1995-10-02, lint supports __asm statements and __asm modifiers.

No binary change.
 1.234  30-Apr-2022  rillig lint: inline macro 'tflag'

The definition of the macro tested both allow_trad and allow_c90, but
there is only a single mode in which allow_c90 is false, therefore it
suffices to test only that.

While double-checking each occurrence of tflag individually, I learned
why lint performs lookups of struct members only by name, independently
of the struct in which they are declared. See typeok_arrow for details.

No functional change.
 1.233  02-Apr-2022  rillig lint: clean up style

Remove outdated ARGSUSED (the one in tree.c had been wrong since 1995),
remove unused argument.

No functional change.
 1.232  27-Feb-2022  rillig lint: encode lifetime of allocated memory in function names

No functional change.
 1.231  27-Feb-2022  rillig lint: encode lifetime of allocated memory in the function names

No functional change.
 1.230  22-Dec-2021  rillig lint: clean up initialization

No functional change.
 1.229  22-Dec-2021  rillig lint: fix handling of initializations

The implementation from March 2021 added proper support for designators
but didn't model the brace levels correctly. In particular, it could
not handle additional braces or omitted braces. In such a case, lint
skipped the remaining initializers from the initialization. Due to
this, type errors in the remaining initializers went unnoticed. Another
effect was that arrays of unknown size were wrongly reported as having
size 0.

Both GCC and Clang recommend placing braces around each sub-type that is
initialized, such as a struct, union or array. Postfix does not follow
these recommendations, therefore lint had to be disabled in
external/ibm-public/postfix/Makefile.inc. This commit fixes the bugs
mentioned there.
 1.228  21-Dec-2021  rillig lint: reword message 187 about too long string literal for initializer

The previous message was imprecise in that it didn't distinguish between
non-terminating and terminating null bytes.
 1.227  21-Dec-2021  rillig lint: treat incomplete union in the same way as incomplete struct

The newly added tests triggered the assertion in begin_designation since
for incomplete types the initialization is stopped before handling the
first brace.
 1.226  21-Dec-2021  rillig lint: rename debug_indent to debug_print_indent

The previous name could be mistaken to mean "increase the indentation of
the debug output". Instead, the function prints the current indentation.

In externs1.h, the macro definition was a duplicate, the macros were
sorted differently than the functions a few lines above.

No binary change.
 1.225  21-Dec-2021  rillig lint: make function names a bit more concise

No binary change.
 1.224  21-Dec-2021  rillig lint: rename parameters in check_init_expr

An initialization has a left-hand and a right-hand side, reflect this in
the parameter names to avoid confusion.

No binary change.
 1.223  20-Dec-2021  rillig lint: add grammar rule for the beginning of a designation

This will be necessary to properly implement handling of initializers
and braced initializer-lists.

No functional change for now since the designation is already reset
after each expression and '}'. To handle initializations properly, the
designation must not be reset after each expression, it must advance to
the next member instead.
 1.222  19-Dec-2021  rillig lint: split local variable in initialization_end_brace_level

No functional change.
 1.221  19-Dec-2021  rillig lint: add typedef for types in initializations

No binary change except for line numbers in assertions.
 1.220  18-Dec-2021  rillig lint: document wrong data structures for modelling initializations

No functional change.
 1.219  18-Dec-2021  rillig lint: move maximum seen subscript from brace level to initialization

There is no need to store this information at every brace level since in
any translation unit that survives a conforming C99 compiler, an array
of unknown size is only possible once per initialization, not once per
brace level.
 1.218  18-Dec-2021  rillig lint: use vector instead of list for designation in initialization

This change is a prerequisite for fixing the current bugs in handling
initializations. Part of the fix will be designation_pop, which is
costly for a singly linked list.

As a side benefit, memory management becomes simpler and needs fewer
malloc calls.

No functional change.
 1.217  17-Dec-2021  rillig lint: reduce code for beginning an initialization

No functional change.
 1.216  17-Dec-2021  rillig lint: inline sub-cases of brace_level_sub_type

Having the code in separate functions did not add to the clarity of the
code. The additional information from the function names can be grasped
as easily from the case labels.

No functional change.
 1.215  17-Dec-2021  rillig lint: fix initialization with few braces from function

Seen in Postfix, smtp_proto.c.
 1.214  17-Dec-2021  rillig lint: fix initialization of array with fewer braces

Seen in Postfix, cleanup_strerror.c.
 1.213  17-Dec-2021  rillig lint: work around lint bug in initialization with few braces

Needs a proper fix later, but for now, this workaround allows to run
lint on Postfix again.
 1.212  17-Dec-2021  rillig lint: rename is_string_array to is_character_array

The old name was too confusing.

No functional change.
 1.211  16-Dec-2021  rillig lint: narrow down parameter of build_name

Passing an arbitrary tokenizer symbol left too much freedom and
uncertainty to the caller, and 0 was a magic number in this context.

No functional change.
 1.210  16-Nov-2021  rillig lint: fix check for function calls in strict bool mode

Previously, if a function call occurred in the controlling expression,
its return type could be any scalar, not just bool. This was against
the goal of strict bool mode, which makes bool a separate and
incompabile type to all other types. For example, it would allow
controlling expressions like 'strcmp(a, b)' without the usual '!= 0',
but only if at least one of 'a' and 'b' came from a macro definition
from a system header.

The fix is that the decision of whether the type of the controlling
expression may be scalar is no longer based on the operand types but on
the main operator of the controlling expression.
 1.209  04-Sep-2021  rillig lint: spell out abbreviations in comments

No functional change.
 1.208  14-Aug-2021  rillig lint: allow initialization of struct with constant member

The operator INIT, just like RETURN and FARG, initializes an object with
an expression. The target object of such an initialization may be a
struct with constant members.

The operator ASSIGN, on the other hand, is entirely different. It
overwrites the existing value of the object, and this is not allowed for
structs that have a constant member. Therefore it was wrong to use the
operator ASSIGN for initialization.
 1.207  10-Aug-2021  rillig lint: fix 3 of the 4 wrong messages about lvalue in initial assignment
 1.206  31-Jul-2021  rillig lint: clean up debug logging

The calls to debug_step, unlike printf, don't need a trailing newline.

Remove the debug_step0 macro and its relatives since lint already uses
enough other features from C99 that it essentially requires this
standard, which supports varargs macro arguments. Among these features
are __func__ and printf("%zu").

In non-debug mode, do not evaluate the arguments of debug_step.
Evaluating the arguments had caused an internal error when running the
test op_shl_lp64. This is indeed a bug since initdecl should have
initialized the type table for __uint128_t. This had been forgotten
when support for __uint128_t was added in decl.c 1.69 from 2018-09-07.

No functional change.
 1.205  31-Jul-2021  rillig lint: extract debug logging to separate file

Lint currently has several different kinds of debug log:

* The -DDEBUG log is controlled at compile time.
* The -d command line options enables some other debug logging.
* The -DYYDEBUG log for parsing is controlled at compile time.
* The -y command line option only has an effect in -DYYDEBUG mode.

Extracting the logging into a separate file is a first step towards
unifying these logs and making the code for debug logging stand out less
than the current #ifdef DEBUG.

No functional change.
 1.204  31-Jul-2021  rillig lint: merge duplicate code for generating unqualified type

This is a preparation for fixing the wrong warnings in msg_115.c.

No functional change.
 1.203  20-Jul-2021  rillig lint: use consistent naming scheme for functions that build nodes

No functional change.
 1.202  20-Jul-2021  rillig lint: split 'build' into build_binary and build_unary

No functional change.
 1.201  02-Jul-2021  rillig lint: fix initialization of array of unknown size

The size of the resulting array was computed wrong.
 1.200  29-Jun-2021  rillig lint: do not modify tnode_t->tn_type in check_init_expr

This is a very small step towards having all shared type_t objects only
referenced via const pointers. Since the types may be shared, it is a
bad idea to try to modify them, so better let the compiler check this.
It's a long way to reach this goal, but this small step is already
possible.

No functional change.
 1.199  19-Jun-2021  rillig lint: replace undefined behavior with assertion failure

Triggered by this malformed code:

struct{int;
 1.198  18-Apr-2021  rillig lint: extend documentation about handling initializations
 1.197  18-Apr-2021  rillig lint: remove redundant test for tflag in initialization handling
 1.196  18-Apr-2021  rillig lint: fix storage class of compound literal in initializer

A compound literal that occurs at block_level 0 does not have storage
class AUTO. Instead, its storage class is either EXTERN or STATIC.

While removing the temporary objects from the symbol table had prevented
the assertion, it did not properly fix the underlying problem, which was
that since C99 the initializers can contain references to unnamed
objects that are created on-the-fly. For C90 it was correct to always
use AUTO as the storage class of a temporary symbol.
 1.195  17-Apr-2021  rillig lint: fix assertion failure for temporary objects in initialization
 1.194  09-Apr-2021  rillig lint: fix initialization with brace-enclosed string literal

C99 allows this form in 6.7.8p14 and p15.

The previous lint tests did not cover the case of an array at the top
level of the object to be initialized, they only covered the error cases
(d_c99_init.c, variables 'prefixed_message' and 'message_with_suffix').

Lint is now more generous than strictly required by C99, but since GCC
and Clang already cover the case of 'message_with_suffix', this is ok.

The test d_init_array_using_string.c was wrong before in rejecting the
initializer for 'extra_braces'. I had tested that Clang generated a
warning for this, but I had not inspected its warning carefully enough.
Clang had not warned about the extra braces but only about a type
mismatch since I tested on a platform where wchar_t was 16 bit.
 1.193  02-Apr-2021  rillig lint: reorder struct members to be in comprehension order

No functional change.
 1.192  02-Apr-2021  rillig lint: rename members of brace_level

The terms 'member' and 'subscript' are distinctive enough.

No functional change.
 1.191  02-Apr-2021  rillig lint: remove unused brace_level.bl_array_of_unknown_size

Since init.c 1.177 from 2021-03-29, the type of the object to be
initialized is no longer modified in the middle of the initialization,
as required by C99 6.7.8p22. Therefore it is no longer necessary to
keep this redundant information around.

No functional change.
 1.190  02-Apr-2021  rillig lint: add parentheses after sizeof, as required by share/misc/style

No functional change.
 1.189  02-Apr-2021  rillig lint: rename functions for duplicating types

No functional change.
 1.188  02-Apr-2021  rillig lint: name memory allocation functions consistently

No functional change.
 1.187  02-Apr-2021  rillig lint: rename mbl to memory_block

No functional change.
 1.186  02-Apr-2021  rillig lint: make debug logging compatible with C90

The previous code used the GCC-style varargs macros, which did not even
conform to C99.

No functional change.
 1.185  01-Apr-2021  rillig lint: do not error out of a struct is initialized without braces

This allows to process lib/libc/gen/sysctl.c 1.38 from 2021-03-30, as
well as its precedessor 1.37, which had a workaround just for lint.

While unusual, C99 allows these.
 1.184  30-Mar-2021  rillig lint: clean up duplicate code in handling of initialization

No functional change.
 1.183  30-Mar-2021  rillig lint: add error about out-of-bounds array subscripts

This check is not strictly necessary since any C99 compiler must
diagnose them as well, it is rather meant for demonstrating how to do
the check in lint, and for symmetry with the 'unknown member' error
message. These provide insight into how the data structures in init.c
are meant to be accessed.
 1.182  30-Mar-2021  rillig lint: add type information for message about unknown member name
 1.181  30-Mar-2021  rillig lint: reword message for very unlikely .member in array initialization
 1.180  30-Mar-2021  rillig lint: add type information to message 175
 1.179  30-Mar-2021  rillig lint: rewrite handling of initializations, fixing several bugs

The previous implementation had a wrong model of how initialization
happens in C99, its assertions failed in all kind of edge cases and it
was not possible to fix the remaining bugs one at a time without running
into even more obscure assertion failures.

The debug logging was detailed but did not help to clarify the
situation. After about 20 failed attempts at fixing the small details I
decided to start all over and rewrite the initialization code from
scratch. I left the low-level parts of handling designators, the code
that is independent of brace_level and the high-level parts of how the
parser calls into this module. Everything else is completely new.

The concept of a brace level stays since that is how C99 describes
initialization. The previous code could not handle multi-level
designations (see d_init_pop_member.c). There are no more assertion
failures in the initialization code.

Some TODO comments have been left in the tests to keep the line numbers
the same in this commit. These will be cleaned up in a follow-up
commit.

The new implementation does not handle initialization with "missing"
braces. This is an edge case that both GCC and Clang warn about, so it
is not widely used. If necessary, it may be added later.

The new implementation does not use any global variables in the vast
majority of the functions, to make all dependencies and possible
modifications obvious.
 1.178  29-Mar-2021  rillig lint: rename variable 'level' to 'bl'

For consistency with its type prefix and the other variables. This
variable is used so often that it makes sense to abbreviate it.

No functional change.
 1.177  29-Mar-2021  rillig lint: add helper functions

No functional change.
 1.176  29-Mar-2021  rillig lint: rename struct members in init.c

No functional change.
 1.175  29-Mar-2021  rillig lint: remove outdated comments, clean up style

No functional change.
 1.174  28-Mar-2021  rillig lint: add assertions for aggregate initialization

No functional change.
 1.173  28-Mar-2021  rillig lint: add test for assertion failure in initialization

The 'cnt = level->bl_type->t_tspec == STRUCT ? 2 : 1;' in
initialization_push_struct_or_union is obviously wrong since not every
struct has exactly 1 remaining member after the first member that has an
initializer with designation.

This bug started its life in init.c 1.12 from 2002-10-21, a little over
18 years ago.
 1.172  28-Mar-2021  rillig lint: split complicated function for looking up struct members

No functional change.
 1.171  28-Mar-2021  rillig lint: inline variable in initialization_init_array_using_string

No functional change.
 1.170  28-Mar-2021  rillig lint: clean up initialization_init_array_using_string

No functional change.
 1.169  28-Mar-2021  rillig lint: clean up initialization_expr

No functional change.
 1.168  28-Mar-2021  rillig lint: extract duplicate code to is_string_array

No functional change.
 1.167  28-Mar-2021  rillig lint: add is_struct_or_union

No functional change.
 1.166  28-Mar-2021  rillig lint: extract brace_level_subtype

No functional change.
 1.165  28-Mar-2021  rillig lint: extract initialization_end_brace_level

No functional change.
 1.164  28-Mar-2021  rillig lint: extract check_no_auto_aggregate

No functional change.
 1.163  28-Mar-2021  rillig lint: clean up type handling in initialization

No functional change.
 1.162  28-Mar-2021  rillig lint: remove wrong warning about wrong initializer type

The following code is valid:

int valid = {{{ 3 }}};

C90 3.5.7 and C99 6.7.8 both say that the "initializer for a scalar
shall be a single expression, optionally enclosed in braces". They
don't put any upper bound on the amount of braces, not even in the
"Translation limits" section.
 1.161  28-Mar-2021  rillig lint: sprinkle const on function declarations

No functional change.
 1.160  28-Mar-2021  rillig lint: move code for extending an array of unknown size further up

No functional change.
 1.159  28-Mar-2021  rillig lint: move code for checking init expressions further up

It did not make sense to have this code between the code for the
designation and the brace level. Since it is independent of all these
types, move it to the top.

No functional change.
 1.158  28-Mar-2021  rillig lint: group functions according to their main object

No functional change.
 1.157  28-Mar-2021  rillig lint: rename public designator functions

Their previous names were too similar to the actual implementation
functions, which was confusing.

No functional change.
 1.156  28-Mar-2021  rillig lint: inline initsym

No functional change.
 1.155  28-Mar-2021  rillig lint: replace initstack_push_array with brace_level_push_array

The designator is no longer logged at this point because it is
irrelevant.

No functional change.
 1.154  28-Mar-2021  rillig lint: clean up debug logging, use consistent variable names

No functional change.
 1.153  28-Mar-2021  rillig lint: omit unnecessary calls to current_init

No functional change.
 1.152  28-Mar-2021  rillig lint: move brace level functions further up

No functional change.
 1.151  28-Mar-2021  rillig lint: clean up debug logging for initialization

No functional change.
 1.150  28-Mar-2021  rillig lint: inline initerr in code for handling initializations

No functional change.
 1.149  28-Mar-2021  rillig lint: inline brace level in initialization

No functional change.
 1.148  28-Mar-2021  rillig lint: replace global variables with function parameters

No functional change.
 1.147  28-Mar-2021  rillig lint: move more functions to the global-variables-free section

No functional change.
 1.146  28-Mar-2021  rillig lint: reorder code for handling initializations

First the primitives for debug logging, then the functions that do not
access any global variables, finally everything else.

No functional change.
 1.145  28-Mar-2021  rillig lint: move debug primitives to the top of the code

No functional change.
 1.144  27-Mar-2021  rillig lint: extract brace_level_next_member from initstack_pop_item_unnamed

No functional change.
 1.143  27-Mar-2021  rillig lint: extract brace level code into separate functions

No functional change.
 1.142  27-Mar-2021  rillig lint: prepare support for adding array designators

No functional change.
 1.141  27-Mar-2021  rillig lint: add function for setting the initialization error

No functional change.
 1.140  27-Mar-2021  rillig lint: remove unnecessary typedefs

No functional change.
 1.139  27-Mar-2021  rillig lint: rename initstack_element to brace_level

No functional change.
 1.138  27-Mar-2021  rillig lint: extract look_up_member from initstack_push_struct_or_union

No functional change.
 1.137  27-Mar-2021  rillig lint: clean up initstack_pop_item_named_member

Previously, the code accessed the global variable for the designator
several times, even though the designator cannot change during this part
of the code. Make this obvious by passing this designator as a
parameter instead.

No functional change.
 1.136  27-Mar-2021  rillig lint: fix and update comments about initialization
 1.135  27-Mar-2021  rillig lint: merge duplicate code in initialization

No functional change.
 1.134  26-Mar-2021  rillig lint: in malloc calls, use 'sizeof *ptr' instead of 'sizeof(type)'

No functional change.
 1.133  25-Mar-2021  rillig lint: fix initialization for arrays with designators

From the previous commit, there was an off-by-one error left, which was
due to the interaction between designation_add_subscript and
extend_if_array_of_unknown_size.

The other crucial point was to call initstack_pop_nobrace before
accessing the "current initialization stack element". Without this
call, in msg_168.c the "current element" would point to the initializer
level for 'const char *' instead of the one for 'array of const char *'.

One more step towards supporting C99.
 1.132  25-Mar-2021  rillig lint: improve initialization of arrays with designators

Initialization is still buggy but better than before. The remaining bug
is that only the first designator determines the array size, and after
that, the array is no longer considered of unknown size. This
contradicts C99. More improvements to come.
 1.131  25-Mar-2021  rillig lint: fix names of functions dealing with designations

These functions modify the whole designation, not only a single
designator.

No functional change.
 1.130  25-Mar-2021  rillig lint: free the designator as soon as it is no longer needed

One of the latest "refactorings" introduced a small and practically
unimportant memory leak. If the last initializer in an initialization
had a designator, that designator was not freed.

When the "current initialization" was still a global variable with
unlimited lifetime, it was freed at the beginning of the next
initialization. After the refactorings, this "next initialization"
could no longer see anything from the previous initialization since all
references have been cleaned up at that point. Freeing the memory so
late and in an almost totally unrelated place was a bad idea anyway.
 1.129  25-Mar-2021  rillig lint: distinguish between read and write access to initstk

There are far too many places that modify the top element of the
initializer stack. Each of these places should get its own logging, as
long as the code is still complicated. These places are now clearly
marked with initstk_lvalue.

No functional change.
 1.128  25-Mar-2021  rillig lint: replace namlist with designation and designator

This makes it possible to accurately model C99 initializers, including
their optional designators. Previously, array subscripts had not been
modeled at all.

In the previous commit, debug_designation crashed immediately since I
had not run the code in debug mode even once. The condition 'name !=
head' was a left-over from the old times where the designator was still
a circular list.

No functional change outside debug mode.
 1.127  25-Mar-2021  rillig lint: remove over-engineered doubly-linked circular list

The designation only needs to be navigated from head to tail, not the
other way round.

No functional change.
 1.126  25-Mar-2021  rillig lint: rename debug_named_member to debug_designation

No functional change outside debug mode.
 1.125  25-Mar-2021  rillig lint: rename i_current_object to i_next_member

No functional change outside debug mode.
 1.124  25-Mar-2021  rillig lint: rename the few remaining instances of 'namedmem'

No functional change.
 1.123  25-Mar-2021  rillig lint: split current_namedmem into lvalue and rvalue form, rename them

This is a small step towards using the terminology from C99.

No functional change.
 1.122  25-Mar-2021  rillig lint: inline macro 'namedmem' for C99 designators

No functional change.
 1.121  25-Mar-2021  rillig lint: free memory at the end of an initialization

No functional change, just more clarity in the code.
 1.120  25-Mar-2021  rillig lint: condense debug output for initializations

This way it does not look more impressive than it really is.

No functional change outside debug mode.
 1.119  25-Mar-2021  rillig lint: add comments about things left to do, from code review

No functional change.
 1.118  25-Mar-2021  rillig lint: fix C99 initialization with expression of type 'struct'

This has been a long-standing limitation of lint. Now it is almost
ready for C99, see the list of "major changes" in the foreword of C99.

One known remaining bug in the area of initialization is designators
with several levels, such as '.member[2].member.member'. Oh, and
designators for arrays are only supported in the parser but not in the
type checker. There's still some work to do.
 1.117  25-Mar-2021  rillig lint: remove outdated comment from init_using_expr

Since C99, an aggregate type can be initialized without braces.

No functional change.
 1.116  25-Mar-2021  rillig lint: extract check_init_expr from init_using_expr

No functional change.
 1.115  23-Mar-2021  rillig lint: fix wrong warning about initialization using string literal

Missing braces after 'if', since init.c 1.68 from 2021-02-20.

GCC 10 doesn't complain about this even with -Wmisleading-indentation
since at least one of the involved lines is a macro invocation (in this
case both lines). GCC 11 will warn about this.

Clang warns about this, but the regular Clang build currently fails for
other reasons, so this problem didn't show up there either.
 1.114  23-Mar-2021  rillig lint: extract init_using_assign from init_using_expr

No functional change.
 1.113  23-Mar-2021  rillig lint: merge duplicate code in init_using_expr

No functional change.
 1.112  23-Mar-2021  rillig lint: fix documentation about optional braces in initializers
 1.111  23-Mar-2021  rillig lint: fix wrong error message about type mismatch in compound literal

Now that the code contains explicit markers for starting and ending an
initialization, and having the guarantee that an assertion fails
whenever some code accesses the state of the "current initialization"
even though there is no ongoing initialization gives me much more
confidence in the correctness of the code. The calls to
begin_initialization and end_initialization always appear in pairs,
enclosing the minimal amount of code necessary for initialization.

In a nutshell, global modifiable state is error-prone and hard to
understand.

A nice side effect is that the grammar no longer needs a special rule
for the outermost initializer since the functions for the debug logging
are now called explicitly.

The code that misuses the initialization state just because it needs to
temporarily store a sym_t somewhere is now clearly marked as such. A
GCC statement expression can appear anywhere and is therefore
independent of the initialization. Most probably the code can simply
refer to the local variable in the grammar rule itself, or this variable
needs to be encoded in the grammar %union. For sure there is a better
way to handle this.

There is no longer a need that the function 'declare' initializes the
initialization state, it was just the wrong place to do this.
 1.110  23-Mar-2021  rillig lint: add indirection for accessing the current initialization

This indirection will be needed to handle nested initializations, which
are a new feature of C99. These are currently not handled correctly,
see msg_171.c.

No functional change.
 1.109  22-Mar-2021  rillig lint: reduce visibility of variables for initializations

No functional change.
 1.108  20-Mar-2021  rillig lint: fix assertion failure after error in designation

In d_c99_init.c, the initialization of array_with_designator failed.
The designator '.member' from that initialization was not cleaned up
before starting the next initialization.
 1.107  20-Mar-2021  rillig lint: replace segmentation fault with assertion failure
 1.106  19-Mar-2021  rillig lint: improve debug loggin for initialization
 1.105  19-Mar-2021  rillig lint: rename designator_pop_name to designator_shift_name

The entries are removed from the beginning, not from the end.

No functional change.
 1.104  19-Mar-2021  rillig lint: replace assertion in initialization with proper error message
 1.103  19-Mar-2021  rillig lint: rename push_member and pop_member

These two functions are supposed to model the designator that is used
for initializing structs and arrays. The implementation is still buggy
and does not work at all for C99 designators with multiple names, see
d_init_pop_member.c.

For now, just rename the functions to head in the right direction.

No functional change.
 1.102  19-Mar-2021  rillig lint: extend documentation about initialization

No functional change.
 1.101  19-Mar-2021  rillig lint: split initstack_pop_item into separate functions

No functional change.
 1.100  19-Mar-2021  rillig lint: improve debug logging in initstack_push

No functional change outside debug mode.
 1.99  18-Mar-2021  rillig lint: split initstack_push into smaller functions

No functional change.
 1.98  18-Mar-2021  rillig lint: clean up control flow in initstack_push

No functional change.
 1.97  18-Mar-2021  rillig lint: extract extend_if_array_of_unknown_size from initstack_push

No functional change.
 1.96  18-Mar-2021  rillig lint: replace undefined behavior during initialization with assertion

This only affects code that is already rejected by the compiler.
 1.95  18-Mar-2021  rillig lint: improve debug logging during initialization

No functional change outside debug mode.
 1.94  18-Mar-2021  rillig lint: reduce debug logging for initialization, update documentation

No functional change outside debug mode.
 1.93  18-Mar-2021  rillig lint: document how initialization works, improve debug logging

No functional change outside debug mode.
 1.92  18-Mar-2021  rillig lint: document the initialization of an object in more detail

This will help fixing the bugs that are currently demonstrated in
msg_168.c and d_struct_init_nested.c.
 1.91  17-Mar-2021  rillig lint: add debug logging for C99-style initialization of arrays

No functional change outside debug mode.
 1.90  17-Mar-2021  rillig lint: move debug logging for the designator to the top of the code

The debugging code is needed by the soon-to-be-added proper handling of
array subscript initializers such as '.member[123].member = 12345'.

No functional change.
 1.89  22-Feb-2021  rillig lint: change spelling of initialisation to initialization

That's the wording from the ISO C99 standard.
 1.88  22-Feb-2021  rillig lint: improve debug message and comment
 1.87  21-Feb-2021  rillig lint: extract check_non_constant_initializer from init_using_expr

No functional change.
 1.86  21-Feb-2021  rillig lint: extract check_bit_field_init from init_using_expr

No functional change.
 1.85  21-Feb-2021  rillig lint: add debug logging for initializing an array of unknown size

It is possible that the type name 'array[unknown_size]' may spill into
the user-visible diagnostics. The current test suite does not cover
such a case. Anyway, saying 'array[unknown_size]' is still better than
saying 'array[0]', which would be misleading.
 1.84  21-Feb-2021  rillig lint: rename and condense initstack_check_too_many

No functional change.
 1.83  21-Feb-2021  rillig lint: remove redundant debug logging

In initstack_pop_nobrace, if anything happens to the initstack, it will
be logged by initstack_pop_item.

In init_using_expr, the address of the node is irrelevant, the node's
contents has already been logged above.
 1.82  21-Feb-2021  rillig lint: document i_brace, add comments, rename initstack_string

No functional change outside debug mode.
 1.81  21-Feb-2021  rillig lint: always initialize return values of constant_addr

Before, the caller was responsible for initializing the return values
from the function. This was an unexpected burden.

Ensure that in each branch that returns true, both return values are
properly set.

Strangely, the only caller of that function, init_using_expr, uses
neither of the return values. It just tests whether the expression is
constant or not.

No functional change.
 1.80  21-Feb-2021  rillig lint: clean up debug logging in initstack_pop_item

The debug logging contained much redundant information and was
misleading in a few places. For example, "pop" did not actually pop an
item, plus there are several things that could be popped, so that didn't
help either.

Sprinkle some comments in places where the code needs to become clearer.

No functional change outside debug mode. The condition
'istk->i_remaining >= 0' was redundant due to the assertion directly
above it.
 1.79  21-Feb-2021  rillig lint: rename istk to initstack_element

The longer name is more expressive and more correct. The previous name
called each stack element a stack itself, which was unnecessarily
confusing.

No functional change.
 1.78  21-Feb-2021  rillig lint: reduce amount of debug output during initialization
 1.77  21-Feb-2021  rillig lint: rename members of struct istk to be more expressive

No functional change outside debug mode.
 1.76  21-Feb-2021  rillig lint: indent node details in debug mode
 1.75  20-Feb-2021  rillig lint: fix lint warning 'expression has null effect'
 1.74  20-Feb-2021  rillig lint: fix lint warnings

No functional change.
 1.73  20-Feb-2021  rillig lint: in debug mode, print the initialization stack

This is the central data structure of the initializations, it keeps
track of the objects that still need to be initialized. Seeing its
contents in debug mode helps in finding and understanding the still
incomplete C99 support.
 1.72  20-Feb-2021  rillig lint: define debug_named_member only in debug mode

In non-debug mode it was an expensive no-op.
 1.71  20-Feb-2021  rillig lint: document an assumption that has turned wrong with C99
 1.70  20-Feb-2021  rillig lint: extend debugging for initializing objects

No functional change outside debug mode.
 1.69  20-Feb-2021  rillig lint: rename mkinit to init_using_expr

No functional change outside debug mode.
 1.68  20-Feb-2021  rillig lint: add hierarchical debug logging for initializations

No functional change for default mode.
 1.67  19-Feb-2021  rillig lint: rename str_t and its members to be more expressive

No functional change.
 1.66  31-Jan-2021  rillig lint: don't warn about constant condition in 'do { } while (0)'
 1.65  30-Jan-2021  rillig lint: rename incompl to is_incomplete

No functional change.
 1.64  17-Jan-2021  rillig lint: fix return type of conaddr
 1.63  16-Jan-2021  rillig lint: replace integer constant expressions with true and false

LINTFLAGS=-gST make lint, with manual review.

The error messages from lint are all correct, they are not complete
though. The return value of a function returning bool may still be
compared to the integer 0.
 1.62  16-Jan-2021  rillig lint: replace 0 and 1 with false and true, where appropriate

Change in behavior: Passing the option -h exactly 4294967296 times or
any multiple thereof is no longer equivalent to passing it never at all,
it is now equivalent to passing it once. See main2.c, hflag++ for the
actual change.

Other than that, no functional change intended.

A very large portion of the code already conformed to the requirements
of the strict bool mode. The only missing thing was using the constant
literals false and true instead of 0 and 1. For sure there are some
integer literals left that can be converted. For now, all literals that
appeared in the form " = 0" or " = 1" have been replaced.
 1.61  10-Jan-2021  rillig lint: rename type classification macros

The previous names tspec_is_int and tspec_is_uint were confusing because
there are actually tspec_t constants called INT and UINT, these
classification macros return true for other integer types as well,
though.

While here, remove the prefix "tspec_" from these macros. It wasn't as
helpful as intended, in many cases it was obviously redundant, when it
was called as tspec_is_integer(tn->tn_type->t_tspec).

No functional change.
 1.60  03-Jan-2021  rillig lint: rename functions that create nodes
 1.59  03-Jan-2021  rillig lint: rename type.t_isfield to t_bitfield
 1.58  03-Jan-2021  rillig lint: let gnuism and c99ism return void instead of int

The return value was only used in a single case. Duplicating the
condition for printing a message is ok in that case, since it makes all
other places in the code simpler.

The occasional "(void)" or "msg = " before the function call had hidden
the calls from check-msgs.lua, which didn't check the message texts in
such cases.
 1.57  02-Jan-2021  rillig lint: fix buffer truncation for type names

Previously, most type names had been cut off after 63 significant
characters. In some cases, 127 characters survived, or 255. And for
the debugging messages, sometimes even 1023. This inconsistency was
useless.

It was wrong in the first place to make the caller of the function
tyname responsible for handling the buffer. That's not something a
caller of such a simple function should do. These callers have better
things to do.

The API of the new function type_name is as simple as possible.

In the implementation, the name of the type is generated anew each time.
I just didn't know whether the type details could change, once the type
is initialized, and I didn't want to find out. To be on the safe side,
the resulting type name is cached, independently of the type it was
generated for. Using a trivial, unbalanced binary tree should be good
enough for now.

All this work is necessary to support adding new debug logging, without
being distracted by irrelevant implementation details such as these
buffer sizes. Adding new debug messages should be fun and easy; up to
now, it was overly bureaucratic.
 1.56  02-Jan-2021  rillig lint: use bool instead of u_int:1 in structures

Better late than never.
 1.55  01-Jan-2021  rillig lint: add debug logging for initialization using named members
 1.54  01-Jan-2021  rillig lint: document that C99-style initialization is necessarily buggy
 1.53  01-Jan-2021  rillig lint: un-export struct istk
 1.52  01-Jan-2021  rillig lint: fix wrong warning about bitfield in C99 structure initialization

The variable namemem is supposed to be a circular list, which is
"documented" implicitly in push_member.

The implementation was buggy though. In pop_member, the circular list
was destroyed though. Given the list (capital, major, favorite_color,
green), removing capital made major point to itself in the forward
direction, even though it should not have been modified at all.

In the test, I had been too optimistic to quickly understand the code
around variable initialization. I was wrong though, so I had to adjust
the comments there to reality.
 1.51  01-Jan-2021  rillig lint: demonstrate bug in handling of nested C9X struct initializers
 1.50  01-Jan-2021  rillig lint: add missing redundant messages in source code
 1.49  01-Jan-2021  rillig lint: align comments in code with actual messages

Redundancy is bad. Especially in this case, separating the format
strings from the actual arguments prevents the compiler from
cross-checking them.
 1.48  30-Dec-2020  rillig lint: un-abbreviate s_field, s_keyw and s_xsym
 1.47  30-Dec-2020  rillig lint: un-abbreviate parenthesized and _strg
 1.46  30-Dec-2020  rillig lint: rename more _nxt members to _next
 1.45  30-Dec-2020  rillig lint: rename s_nxt to s_next
 1.44  30-Dec-2020  rillig lint: reduce verbosity of assertions

Having 2 lines of source code per assertion is too much, especially
since most of this code is redundant anyway. Extract the common code
and the additional negation into a simple function instead.
 1.43  29-Dec-2020  rillig lint: rename istk_t.i_cnt to i_remaining
 1.42  29-Dec-2020  rillig lint: untangle conditions in initstack_next_nobrace
 1.41  29-Dec-2020  rillig lint: clean up debug logging for initializations
 1.40  29-Dec-2020  rillig lint: make debug output for initializations more uniform
 1.39  29-Dec-2020  rillig lint: remove redundant function prototypes
 1.38  29-Dec-2020  rillig lint: improve debug output for initializing structs

Still trying to find out where the wrong warning in d_struct_init_nested
comes from.
 1.37  29-Dec-2020  rillig lint: split initstack_next into separate functions
 1.36  29-Dec-2020  rillig lint: split initstack_pop into separate functions
 1.35  29-Dec-2020  rillig lint: rename functions for handling the initialization stack
 1.34  29-Dec-2020  rillig lint: rename functions with very short names
 1.33  29-Dec-2020  rillig lint: remove redundant parentheses around return value
 1.32  28-Dec-2020  rillig lint: rename confusing function setcompl

The previous function name suggested that it would set the complete flag
of the type, but it was the exact opposite. To reduce confusion, negate
the meaning of the parameter.
 1.31  28-Dec-2020  rillig lint: sort includes
 1.30  28-Dec-2020  rillig lint: realign code
 1.29  28-Dec-2020  rillig lint: rename tspec macros
 1.28  28-Dec-2020  rillig lint1: remove trailing whitespace
 1.27  28-Jul-2015  christos fix member list deallocation; remove #if 0'ed code.
 1.26  20-Nov-2014  christos Always set i_brace in the struct case.
Cleanup some debugging.
 1.25  17-Apr-2014  christos Don't free memory; it is used later.
 1.24  02-Oct-2009  christos branches: 1.24.6; 1.24.12; 1.24.22;
change initialization from non const of regular variables to c99 instead of
gnu since c99 now has it.
 1.23  26-Apr-2008  christos fix "long double" type recognition which broke with the complex changes.
 1.22  20-Jan-2007  ad branches: 1.22.10;
pushinit(): fix a use-after-free bug.
 1.21  15-Oct-2006  christos branches: 1.21.2; 1.21.6;
in the same way that we need an extra level for arrays, do the same for struct.
 1.20  15-Oct-2006  christos previous fix broke array initializers.
 1.19  14-Oct-2006  christos Fix c99 initialization issues. Now the regression tests work.
 1.18  20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.17  06-Dec-2002  thorpej branches: 1.17.2;
Include <string.h> for prototypes.
 1.16  13-Nov-2002  christos PR/18896: Jason Thorpe: C99 structure member initializers broken for arrays.
XXX: The checking done does not work, but the code passes.
 1.15  23-Oct-2002  christos forgot to commit those.
 1.14  22-Oct-2002  christos handle free-ing of temp symbols properly. Don't segv on bad node types.
 1.13  22-Oct-2002  christos fix spelling in comment.
 1.12  21-Oct-2002  christos support for c99 style and gnu style structure and union named initializers.
 1.11  13-Sep-2002  christos Minimize diffs with my C99 capable version [this commit does not include
C99 support.

- turn lerror() into a macro so that the filename and the line number of the
error are printed before we abort.
- recurse in type printing to provide the proper type name.
 1.10  31-Jan-2002  tv Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.9  18-Sep-2001  wiz Give initiali[sz]e all the "i"s it deserves.
 1.8  16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.7  28-May-2001  lukem cleanup (prior to more adding more features):
- convert to ANSI KNF
- remove trailing whitespace
- translate some comments from german into english

code compiles and runs clean, and tested by running "make lint" against
xlint source using previous and this lint produces same results.
 1.6  24-Feb-2001  cgd fix broken NetBSD RCS id tags
 1.5  22-Feb-1998  christos WARNSify
 1.4  02-Oct-1995  jpo prototypes override old style function definitions
this is a gnu extension to ansi c
 1.3  02-Oct-1995  jpo added inline keywords
"inline" is enabled by -g, "__inline" and "__inline__" are always available
 1.2  03-Jul-1995  cgd RCS id cleanup
 1.1  03-Jul-1995  cgd branches: 1.1.1;
Initial revision
 1.1.1.1  03-Jul-1995  cgd lint(1) implementation, by Jochen Pohl. named 'xlint' for a similar
reason to why 'install' is named 'xinstall'.
 1.17.2.1  22-Jun-2004  tron Pull up revision 1.18 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.21.6.1  18-Aug-2009  bouyer Pull up following revision(s) (requested by msaitoh in ticket #1353):
usr.bin/xlint/lint1/init.c: revision 1.22
pushinit(): fix a use-after-free bug.
 1.21.2.1  18-Aug-2009  bouyer Pull up following revision(s) (requested by msaitoh in ticket #1353):
usr.bin/xlint/lint1/init.c: revision 1.22
pushinit(): fix a use-after-free bug.
 1.22.10.1  18-May-2008  yamt sync with head.
 1.24.22.1  10-Aug-2014  tls Rebase.
 1.24.12.1  20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.24.6.1  22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.269.2.1  02-Aug-2025  perseant Sync with HEAD

RSS XML Feed