I want to generate asserts at compile time, checking whether each element within a struct is initialized or not. I was wondering how I could generate a for loop of such static assert statements at compile time given that the assert is not a preprocessor command.
The issue was that uninitialized elements within such a struct of constants was causing undefined behavior in the system.
Tried modifying GCC compiler flags -Wuninitialized or -Wmaybe-uninitialized do not detect when an element within an array of structs is not filled.
Followed the following thread Static assert in C
_Static_assert(fl_Button_List_SA[0].Button_constants_S.Button_Pressed_output != unintializedVariable.Button_constants_S.Button_Pressed_output ,"Struct is uninitialized.");
the fl_Button_List_SA is the array of structs. the Button_constants_S is a struct containing constants only. Button_pressed_output is an element defined as constant containing the output when the button is pressed.
unintializedVariable is a struct of the same type which was kept uninitialized.
The above assertion is currently returning the following error ' error: expression in static assertion is not constant ' but I will manage to get past this. However for more info the button struct inside the array is consisting of a struct containing two structs one containing only constants and the other containing only variables. And given that the structs themselves aren't constants this must be why this issue is coming up.
I would just like to generate code that goes through all the elements of each struct inside the array and checks that the elements are initialized.
As an example of an initialization:
//struct definition
typedef struct Button_properties_t
{
Button_constants Button_constants_S;
Button_variables Button_variables_S;
} Button_properties;
Button_properties Button_list[numberOfButtons] =
{
[0].Button_constants_S.pressed_output=2u,
[0].Button_constants_S.DTC_number=First_Button, //Enum
...
...
}
What I am expecting is a rust like compile time error that if for example during initialization of button number [100] the programmer fails to define a DTC number for that button a compile time error is raised.
[100].Button_constants_S.pressed_output = 4u,
//[100].Button_constants_S.DTC_number uninitialized
...
...
[101].Button_constants_S.pressed_output = 5u,
[101].Button_constants_S.DTC_number = Fifth_Button, //Enum
...