-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add clang-format style for project #2821
base: master
Are you sure you want to change the base?
Conversation
9377535
to
eafa8a3
Compare
This is great! Just a question: is the goal to establish a new coding style or to match the old one as best as possible (both would be fine by me)? We might also want to add We probably want to revise (or remove) our coding_standard_c.md, too. |
@AndreasFuchsTPM On Ubuntu 22.04 I got the following error ?
|
@JuergenReppSIT You will need clang-format version 15 to support that (use apt install clang-format-15). @AndreasFuchsTPM Perhaps consider adding |
Our fapi Macros:
- statecase(VAR, STATE)=case 0:;
- general_failure(VAR)=default:;
- statecasedefault(VAR)=default:;
- statecasedefault_error(VAR, r, label)=default:; To try this out (even on Ubuntu), you can use a dockerized clang-format: docker run -u 1000 -v $PWD:$PWD xianpengshen/clang-tools:17 clang-format -i $PWD/**/*.c $PWD/**/*.h This results in the following formatting. What I could not get working is formatting comments after the case correctly (but I would be able to live with the result): switch (context->io_state) {
statecase(context->io_state, IO_INIT)
/* Prepare the loading of the object. */
r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
return_if_error2(r, "Could not open: %s", path);
fallthrough; Depending on our preferences, we can set To measure the changes: git diff --shortstat | grep -oP '(?<=changed, )\d*' |
I'd like to request a change when it comes to function declarations/definitions. My suggestion would change a lot of lines of code, but is IMO most in line with what we do currently in the fapi. The following applies to declarations and definitions equally. Currently# AlignAfterOpenBracket: Align # default Align
# AlignConsecutiveDeclarations: false # default false
# BinPackParameters: true # default true
# AllowAllParametersOfDeclarationOnNextLine: true # default true TSS2_RC Fapi_CreateSeal_Async(FAPI_CONTEXT *context, char const *path, char const *type,
size_t size, char const *policyPath, char const *authValue,
uint8_t const *data); Better:AlignAfterOpenBracket: AlwaysBreak # default Align
AlignConsecutiveDeclarations: true
BinPackParameters: false
AllowAllParametersOfDeclarationOnNextLine: false TSS2_RC Fapi_CreateSeal_Async(
FAPI_CONTEXT *context,
char const *path,
char const *type,
size_t size,
char const *policyPath,
char const *authValue,
uint8_t const *data); |
The statecase macro now looks better with @johol's suggestion e.g.:
Only superfluous semicolons are moved to the next line.
@joholl Can you compile tss after executing clang-format-17?
|
@JuergenReppSIT I also get these compile errors. That is because clang-format sorts the includes alphabetically and we do not enforce include correctness (see also #2666). Btw: cmocka assumes four includes, so we would also need to add something like this. /* the following includes are needed by cmocka.h */
// clang-format off
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
// clang-format on Let me try to create a commit for that. |
You might wanna wait for #2823 |
It looks like this issue is caused by the appended semicolon when these macros are used. There are code with the semicolon:
Also code that doesnt:
Perhaps we should clean up the semicolons following these macros in the code for consistency. |
The semicolon issue is fixed with the following in Macros:
- statecase(VAR, STATE)=case 0:fn()
- general_failure(VAR)=default:fn()
- statecasedefault(VAR)=default:fn()
- statecasedefault_error(VAR, r, label)=default:fn() |
@joholl Actually, that will trigger another issue:
It seems like this resolves the issue where the line extends behind the comment:
|
This adds a .clang-format file for the project. Requires clang-format-17: docker run -u 1000 -v $PWD:$PWD xianpengshen/clang-tools:17 \ clang-format -i $(find -name '*.c' | xargs realpath) Signed-off-by: Andreas Fuchs <andreas.fuchs@infineon.com>
eafa8a3
to
d4f0551
Compare
I did add the things we talked about. You can use this for testing (thanks to johol):
If y'all are ok, I will go ahead, let it run and then check it in using some smaller chunks... |
I would like to make the complete codebase of this project consistent in terms of style.
Thus I hereby propose our new coding style.
You can check the result by grabbing the .clang-format file and running
and comment on the resulting git diff...
If everyone is fine, I'd like to apply the complete changes to the code base and then also add a CI job that checks for this style.
Also adding a mention in "CONTRIBUTING.md".
What do y'all think ?
Edit: I added a
MacroBlockBegin
statement to account for "statecase" in FAPI.This will now indent all new states, but this actually makes it weirdly readable...