-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 clang-format file #5092
Add clang-format file #5092
Conversation
clang-format allows to auto-format C code. The settings here are set up to follow the code style, see https://redmine.openinfosecfoundation.org/projects/suricata/wiki/Coding_Style. Some bike shedding should still happen. Includes sample formatted code. One can set up .clang-format as "differential setting based on an existing project" or specify all settings. Includes both for people to compare. DO NOT MERGE AS IS.
@@ -0,0 +1,562 @@ | |||
// Formatted using |
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.
Yes, I know these are C++ style comments. This is not meant to be committed, imho. I can always fix the commend style up if some form of this should be committed.
|
||
//--- Line Length --- | ||
// https://redmine.openinfosecfoundation.org/projects/suricata/wiki/Coding_Style#Line-length | ||
// ColumnLimit: 80 |
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.
ColumnLimit is the config that impacts the formatting here. I typically mentioned most of the important configs throughout the file.
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.
I'm in favor of looking at relaxing the 80 chars. I think we've so far called this a soft limit and ~100 a hard limit. I'm okay with wider, where the main consideration should probably be the width of the review layout of github and gitlab.
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.
For what it's worth, Linux recently switched over to 100 chars.
Anyhow, clang-format only has a hard limit. I.e. whatever the number, that'll be the limit. Downside of making it bigger than the mostly used 80 chars is that it'll reformat to 100 chars some of the code that exceeded 80 chars.
struct bla { | ||
// AlignConsecutiveDeclarations | ||
// AlignTrailingComments | ||
int a; /* comment */ |
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.
Some of the code aligns the struct fields, some not. Which one is preferred.
Small sidenote: there's a bug/feature in clang-format that it does not correctly indent the * if pointer is right-aligned and fields are aligned,
E.g.
struct bla {
int a; /* comment */
unsigned bb; /* comment */
int * ccc; /* comment */
};
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.
I think I prefer the non-aligned for members, but aligned for the comments. But not a strong opinion.
// pointers | ||
// DerivePointerAlignment | ||
// PointerAlignment | ||
void *ptr; |
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.
I saw mostly right-aligned pointers so that's what I set up.
DerivePointerAlignment could be useful for whole-file formatting to "follow the prevalent left/centre/right aligned style". I disabled that.
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.
yeah right aligned it is
|
||
struct Bitfields { | ||
// AlignConsecutiveBitFields | ||
int aaaa : 1; |
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.
There will be spaces around the colon for bitfields which is different than used in e.g. app-layer-dnp3-objects.h
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.
Is the comment // AlignConsecutiveBitFields
required here? What would happen if left off?
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.
No, sorry. It's my poor way of tracking that "this is the clang-format setting that impacts this". None of the // comments are required, just FYI. These settings are only set in the .clang-format file.
I am currently updating the code_style.rst where it should be easier to follow.
I was trying to avoid including a "this vs that" comparison for each setting, but it probably would be helpful for the bikeshedding discussion on the format. ;)
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.
AlignConsecutiveBitFields (bool)
If true, aligns consecutive bitfield members.
This will align the bitfield separators of consecutive lines. This will result in formattings like
int aaaa : 1;
int b : 12;
int ccc : 8;
Oh, this is only supported as of clang 11 which is not yet out. I don't think this should be set at all then. Explains why you won't find this setting in clang-format.full.do_not_merge. I've used clang 9 which is recent enough yet old enough to be supported by different dists.
//--- comment wrapping --- | ||
void multi_line_comments() | ||
{ | ||
// ReflowComments: false does not trim comments to ColumnLimit chars |
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.
I disabled limiting comments to 80 chars in the config. Hence, clang-format does not reformat comments.
While it's great to use in my experience, changing an existing code base can be messy. It requires manual fixing up of comments as we devs suck at formatting comments to a length.
Having said that, it probably would be great to enable it so that new code could take advantage of it.
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.
I really like it when an IDE reflows my comments to 80 chars. So I'd like to see this on by default, if it doesn't get in the way too often.
Thanks for this proposition. |
Cool, just learnt that there's a draft in github.
Don't know github actions myself, but there should be a way. We could probably add a new make target as well that could be used in the github action. From what I can tell, these are the different potential steps:
Reformatting all code shall be out-of-scope for this PR. |
Thanks Roland, this is great work. The |
Rebases of unmerged branches can be handled with git filter-branch. I will add some make targets to help with that use case. I fully understand the worries about massive reformatting. Been there, done that. ;) No easy solution, really. For git bisect driven workflows, we clang-format the old version as well and then do diffs that way in case one version is formatted, and one not. Not ideal, but workable. The pain is if you quickly want to diff across the "reformatting boundary". Pre-reformat and post-reformat diffs are the albeit somewhat mundane solution. Key is to have "pure formatting commits" to indicate these boundaries. |
Continued in #5147 |
clang-format allows to auto-format C code. The settings here are set up to follow the code style, see https://redmine.openinfosecfoundation.org/projects/suricata/wiki/Coding_Style.
Some bike shedding should still happen. Includes sample formatted code.
DO NOT MERGE AS IS.
This is not about reformatting all source code. That would be another discussion.
Make sure these boxes are signed before submitting your Pull Request -- thank you.
Link to redmine ticket:
https://redmine.openinfosecfoundation.org/issues/3736
Describe changes
Discussion and decisions
Requires
How to use it?
Other Notes
PRScript output (if applicable):
n/a