cancel
Showing results for 
Search instead for 
Did you mean: 

sometimes some unnecessary #include statements automatically added.

t_j_l
Associate II

When writing code, it is common to encounter the automatic inclusion of some unnecessary #include statements. For example, when I input "NULL" and press the Enter key, it will automatically add " #include <cstddef> ".5d4efb0c-42c5-4875-87f1-2af14162023c.png9e383e1b-8f6b-416c-b886-7a4e14d63698.png

8 REPLIES 8
TDK
Super User

This is hard to follow. Where are you inputting NULL? Is this an autocomplete thing?

the IDE shows the file has been modified, but changing the file outside of user code blocks shouldn’t be done.

If you feel a post has answered your question, please click "Accept as Solution".
t_j_l
Associate II

I entered "uint32_t *pi = NULL" in the user code section. When I pressed the Enter key while the code for handling the NULL input was being displayed, it automatically added "#include"

That seems a fairly reasonable thing to do: you've used NULL, so you will need a definition of it - it has just provided that definition for you.

What's the problem with that?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

I think this is unreasonable. Firstly, if it is to be automatically added, it should be added to the user code area instead of the current location (because this would be overwritten when generating the code using STM32CubeMX). Secondly, the automatically added "#include <cstddef>" is also incorrect and cannot be compiled. It should be "#include <stddef.h>".

OK: so it's not the fact that it's added which is the problem; it's the fact that:

  1. it's the wrong #include, and
  2. it's in the wrong place 

Yes?

in that case, I agree with you there.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

My idea is to avoid automatically adding unnecessary #include statements, or it is not intelligent enough. For example, when I input the "osMessageQueuePut" function, it automatically adds #include "cmsis_os2.h", which is unnecessary because there is already #include "cmsis_os.h" in the current code file, and its internal part contains #include "cmsis_os2.h". Of course, if this cannot be solved, it would also be acceptable to address the issues of adding incorrect #include statements and placing them in the wrong location.


@t_j_l wrote:

avoid automatically adding unnecessary #include statements, 


But, in the example you gave, it was not unnecessary!

 


@t_j_l wrote:

it automatically adds #include "cmsis_os2.h", which is unnecessary because there is already #include "cmsis_os.h" in the current code file


OK, so that's another fault with the implementation - but the basic idea remains useful.

Of course, having multiple #includes of the same header is not harmful - we have Include Guards to take care of that.

 

I've not used VSCode widely - is this just an ST thing, or is it generic VSCode behaviour ?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Yes, in my example, the "#include" is indeed necessary. I did this merely to clarify the specific situation.
According to my test, this issue is currently only occurring with the ST plugin.
Finally, I hope that the automatically added "#include" statements can be placed in the user code section, and that the correct "#include" code can be generated.