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
Add kernel.org's .clang-format
for editor-agnostic C formatting hints
#25488
Conversation
91b5d84
to
6a90bc2
Compare
Thanks for this! I know we've discussed using For my part, I'd be happy to defer formatting to
Out of curiosity, do you have a reference for this? My understanding was that the config file was here to help, but the style is not enforced and I don't think it's even considered as an absolute reference, more like a best-effort config? If anything,
What does it do? Does it format the new code accordingly, or does it reformat the entire file on save? The former sounds good, the latter not that much - the current state of Cilium's code formatting is too far from what
Yes, this intent is good.
So a few more questions:
TL;DR: Happy with the idea of using Cc @aditighag @christarazi who were involved in previous discussions, if I remember correctly. |
So I'm not proposing we replace
It works like It would be opt-in/only apply if a manual reformat was triggered. I find it helpful since I already use LSP-based extensions and largely don't want to spend time on manual formatting. Someone would have to manually reformat every C file in the repo for this to apply globally, and that's certainly not the intent.
Not on all files, but yeah I did notice some stylistic differences. Figured I'd raise this PR and see what people thought about it.
TY, I'll look into these and see if they can be explicitly configured.
Turn off your editor's This is purely opt-in locally for everybody, except for people that might have already been using a plugin that makes use of
Yeah as mentioned above - don't wanna do that until kernel tree does the same. In the meantime this is purely a local dev aid to help avoid roundtrips against
Oh nice I missed that, will take a look.
Thanks for the feedback! I figured that would be the case and it'd be worth discussing. I'll see if I can address those concerns. |
OK, agreed
Which is good, OK thanks for clarifying
👍
Agreed. I guess whether we prefer to keep the current style or adopt
Yes please, this would be great!
OK. I meant more about turning it off for a portion of code if we ever adopt the tool in CI, and can't encode the style we want with clang-format options. I see it should be doable if needed: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code: int formatted_code;
// clang-format off
void unformatted_code ;
// clang-format on
void formatted_code_again;
Yes, we've discussed it before and although we never got a consensus, I think it would be a good thing overall to agree on a style definition and get the Please do take a look at the Tetragon changes, and at whether it seems doable to address the style changes mentioned above, if you have a moment. Would be great to have something as close to the existing style as possible. Then it will be good to go as far as I'm concerned, although I'll try to mention it at the next community meeting (Wednesday), for awareness and to make sure nobody has major concerns about it (I don't expect there will be). |
I agree with Quentins comments; we should discuss this in the community meeting. In any case I think this deserves a more experienced pair of eyes from contributing, I'll rerequest review. EDIT: heh - I guess given @qmonnet's review GitHub won't add another person. 🤷 let's see what the community meeting says. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Various comments and observations. I experimented a little, not something exhaustive.
(Additions or updates to the values we got from the kernel's file, if any, should likely go into a separate commit.)
IncludeIsMainRegex: '(Test)?$' | ||
IndentCaseLabels: false | ||
IndentGotoLabels: false | ||
IndentPPDirectives: None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use this:
IndentPPDirectives: None | |
IndentPPDirectives: AfterHash |
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#indentppdirectives
Although this indents directives everywhere, including where we're not currently using them. Meaning that if, for example, we remove one level of nested #ifdef
s, we have to update the indent for inner directives.
If using the above we'd also need to set PPIndentWidth
, because it seems to default to IndentWidth
but we don't want that:
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#ppindentwidth
My fedora defaults to clang-format 15 - some of the options you added are in 16 only. I'm inclined to track (current version - 2) just to keep things simple - that seems to track with Ubuntu (and presumably other) distros as well. |
Commit c5c90cc8be87287d61faa1051405dd91d4433091 does not contain "Signed-off-by". Please follow instructions provided in https://docs.cilium.io/en/stable/contributing/development/contributing_guide/#developer-s-certificate-of-origin |
Commit c5c90cc8be87287d61faa1051405dd91d4433091 does not contain "Signed-off-by". Please follow instructions provided in https://docs.cilium.io/en/stable/contributing/development/contributing_guide/#developer-s-certificate-of-origin |
a955604
to
3641b03
Compare
Commit c5c90cc8be87287d61faa1051405dd91d4433091 does not contain "Signed-off-by". Please follow instructions provided in https://docs.cilium.io/en/stable/contributing/development/contributing_guide/#developer-s-certificate-of-origin |
Makes sense. I didn't pay too much attention to the required version when looking at these options, so indeed it makes sense to leave them aside if they require something too recent. Thanks! |
Note: If we accept the file, this PR will need a related entry in I'm not sure why the bot fails to detect your sign-off in the commit description, I can see it. Let's see how it goes on the next PR update. |
3641b03
to
6195964
Compare
6195964
to
20f0466
Compare
I've bumped the CODEOWNERS as requested, and rolled in most of the changes requested - feel like it's in a good spot and we can tweak further once it's in place if everyone is good with this as a starting point. |
Thanks! Regarding Other than that, agreed, it's in a good shape and we can tweak further later if necessary. One last thing before we go ahead: Could you please split the commit in two, one with the initial config file copied from the kernel (but without the macro allow-list, so just like in your first version), and then a second one that brings the changes we've agreed on? This way it will be easier to track down the changes we've added for Cilium. I'd also like to have the kernel commit (at which you picked the file) referenced in the description of the commit adding the initial file, so we can track changes on kernel's |
👍
Ah good call, that all makes sense, will do. |
Hmm actually trying this on e.g. |
d8829b7
to
5730131
Compare
@qmonnet Commits re-organized as you asked, please let me know about |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks all good, thanks a lot!
please let me know about
IndentPPDirectives: AfterHash
OK let's leave it out for now, or let's see if others chime in. Hard to tell what option would be the best here anyway.
The checkpatch workflow reports that your commit subject is too long on your first commit, I think it's because you're just missing the empty line between the subject and the description (the link).
Note to other reviewers: This PR was discussed at the community meeting last Wednesday (2023-05-24) and the consensus was to add .clang-format
, nobody raised any particular objections.
https://github.com/torvalds/linux/blob/7acc1372113083fa281ba426021801e2402caca1/.clang-format Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>
Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>
Signed-off-by: Benjamin Leggett <benjamin.leggett@solo.io>
5730131
to
03eb751
Compare
Ah, missed that, fixed, ty! |
clang-format
is an editor-agnostic means of describing C formatting prefs for code editors.Since upstream kernel uses this to define an explicit contract for C formatting, and our
BPF code follows the kernel style, this just brings in a copy of the kernel
.clang-format
dotfile.This will get picked up by any editor that supports it (VSCode w/ C extension, emacs with
lsp
, vims, etc).This will declare a specific contract for contributors around C style, as well as help this project explicitly declare any deviations from that style.
This is a straight copy of the kernel.org file, with a kernel-specific macro allowlist removed.