RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

tracking: replacing header guards with #pragma once

Open N11cc00 opened this issue 8 months ago • 15 comments

Description

~~Clang-format requires the header guard #ifndef #define HEADER_GUARD_H to be outside of the doxygen documentation, that means outside the @{ ... @} block. If this is not done, then clang-format will indent all preprocessor directives after the #ifndef. The problem is that the majority of RIOT's header files don't adhere to this order. They either have the header guard inside the doxygen block or the header guard starts correctly outside the doxygen block but the #endif directive is still inside the doxygen documentation. To fix this inconsistency, I have written a python script that recursively scans all header files in a directory and rearranges the parts, so that the correct order is established.~~

Previously this issue tracked the effort to bring the header guards into the correct order to work with clang-format. Recently it was decided that from now on, RIOT would use the #pragma once macro instead. This means that some of the previous pull request are outdated and every part of the RIOT repo needs to be updated.

Fixed directories

  • [x] drivers #21496, OLD: ~~#21325~~
  • [x] sys: #21497 OLD: ~~#21340~~
  • [x] cpu: #21501
  • [x] core: #21405
  • [x] boards: #21504
  • [x] examples: #21517
  • [ ] pkg
  • [ ] tests

Testing procedure

Use my python script and change the path variable 'RIOT_PATH' in the script to whatever directory you want to recursively fix the header files. Warning: the script isn't fool-proof but fixes 95% of all header files that have this issue. There are individual issues with the formatting that require manual intervention.

The script can be found here

Application

This is based on the fix in https://github.com/RIOT-OS/RIOT/pull/20905 addressed by @mguetschow but with the added benefit of keeping the blocks balanced.

N11cc00 avatar Mar 28 '25 17:03 N11cc00

The order should also be fixed in https://github.com/aabadie/riot-generator

mguetschow avatar Mar 28 '25 17:03 mguetschow

I wonder if your Python script could be adapted and added to the static tests, so that for future contributions, the include guards are checked as well before merging 🤔

crasbe avatar Mar 31 '25 15:03 crasbe

I wonder if your Python script could be adapted and added to the static tests, so that for future contributions, the include guards are checked as well before merging 🤔

This seem like a very reasonable addition. I have yet to look into CI stuff as I have never worked with it.

N11cc00 avatar Mar 31 '25 15:03 N11cc00

There is some documentation in the dist/tools/ci folder https://github.com/RIOT-OS/RIOT/tree/master/dist/tools/ci and you can look at how the check-scripts are called by dist/tools/ci/static_tests.sh and how they operate.

You don't necessarily have to look into CI for that, the script is executed when you call make static-test and that's exactly what the CI system does as well.

crasbe avatar Mar 31 '25 15:03 crasbe

Should we just move to #pragma once instead?

benpicco avatar Apr 03 '25 13:04 benpicco

@benpicco Yes!!

carl-tud avatar Apr 03 '25 13:04 carl-tud

That would be fine by me.

Enoch247 avatar Apr 03 '25 23:04 Enoch247

We had that discussion at VMA 2024.11 and had general consensus on switching to #pragma once, while having some reservation about the necessary migration work to keep in-tree consistency. Since we are now anyways touching a lot of header files, there was consensus at today's maintainer weekly that we can just as well use that work to switch over to #pragma once.

mguetschow avatar Apr 04 '25 08:04 mguetschow

Awesome! I have updated #21344 to make use of the #pragma once.

Enoch247 avatar Apr 06 '25 17:04 Enoch247

For #pragma once, the dist/tools/headerguards/headerguards.py script has to be modified, otherwise the static tests fail:

Image

crasbe avatar Apr 06 '25 17:04 crasbe

Hello, I am new to the RIOT community and would like to help fix the headers. I can start in the core folder if no one is working on that already. Also, I made a modified version of @KSKNico's script to replace the header guard entirely with #pragma once which may be helpful.

JerelJr avatar Apr 11 '25 02:04 JerelJr

@mguetschow @crasbe Should vendor specific header files also be modified to use #pragma once?

N11cc00 avatar May 20 '25 11:05 N11cc00

If those are vendor-provided (aka just copied to the RIOT tree from somewhere else) then I wouldn't change them. But all other header files belonging to RIOT which happen to be for a specific vendor should be modified of course.

mguetschow avatar May 20 '25 11:05 mguetschow

@KSKNico could you extend your script to check that if there is an empty line before and after the #ifndef...#define...#endif that one of these lines is also removed? That would avoid introducing empty line static errors.

See https://github.com/RIOT-OS/RIOT/pull/21501#issuecomment-2899145625 and #21502.

crasbe avatar May 21 '25 21:05 crasbe

@crasbe I updated the script to account for that. Should I open another pull request?

N11cc00 avatar May 22 '25 15:05 N11cc00

When #21701 is merged, does that conclude the migration?

crasbe avatar Oct 17 '25 14:10 crasbe

When #21701 is merged, does that conclude the migration?

Yes, I think this can be closed.

N11cc00 avatar Oct 17 '25 15:10 N11cc00

Thanks everyone for pushing and getting this through! 🎉

mguetschow avatar Oct 19 '25 14:10 mguetschow