-
Notifications
You must be signed in to change notification settings - Fork 901
Add a .clang_format file for Open MPI #8551
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 a .clang_format file for Open MPI #8551
Conversation
If this seems like a good idea lets iterate until the style is what we want. Then I would recommend running clang-format on the entire code base. |
Should mention that this only is used if you run |
2bc5868
to
0d35f5c
Compare
This looks like indent on steroids. Nice ! |
There is also a git tool for this:
It can fix fix, show formatting fixes, etc for currently edited files or a commit. Example:
|
@rhc54 Ok. I can re-enable that. I added a sort priority order but we can tweak it:
You can force sorting header with |
Haven't figured out how yet but we might also want to disallow bare includes (includes without a full path). For example: allow "opal/mca/btl/btl/base/base.h" but disallow "base.h" from the files in that directory. Then we could also sort opal, then ompi, then oshmem headers (or some variation on that). Bare header includes make that difficult. |
It looks like you changed it, but Open MPI style was supposed to not have a space between function name and the open paren. So I think your initial commit message was wrong. |
@bwbarrett True. Will fix the commit message on squish and will fix the PR description now. |
Per discussion on the 9 March 2021, we'd like to get this in before we branch for v5.0. Unless there are objections, let's do this:
|
@gpaulsen and I are OK with adding this to the 5.0 post branch allow list if it needs extra time. |
9b213bc
to
858e80e
Compare
Added some details to the top of .clang-format, squished everything, and updated the commit message. |
Any interest in this feature:
This style is used in a couple of places and does help readability. It turns:
into
or we can use:
which indents before the hash. |
How is this working with our [BEGIN | END]_C_DECLS ? If it increases the indentation for everything in the file (similar to emacs) then it is not good. |
Let me check. |
|
I just reformatted the whole code base as a test now to double check. |
bot:ompi:retest |
I applied the format to some files I've been working on recently and like most of what the current configuration currently produces. However, I noticed a few things that we might want to reconsider:
- index = (int)(uintptr_t)pvar->ctx; /* Convert from MPI_T pvar index to SPC index */
+ index = (int) (uintptr_t) pvar->ctx; /* Convert from MPI_T pvar index to SPC index */
- if ((assert & MPI_MODE_NOCHECK) == 0) {
+ if ((assert &MPI_MODE_NOCHECK) == 0) { -> Why the change to
- if (module->epoch_type.access != NONE_EPOCH &&
- module->epoch_type.access != FENCE_EPOCH) {
+ if (module->epoch_type.access != NONE_EPOCH && module->epoch_type.access != FENCE_EPOCH) {
- ompi_osc_ucx_handle_incoming_post(module, &(module->state.post_state[j]), NULL, 0);
+ ompi_osc_ucx_handle_incoming_post(module, &(module->state.post_state[j]), NULL,
+ 0); I'd rather have the lines balanced: - ompi_osc_ucx_handle_incoming_post(module, &(module->state.post_state[j]), NULL, 0);
+ ompi_osc_ucx_handle_incoming_post(module, &(module->state.post_state[j]),
+ NULL, 0); That also applies to cases where lines are balanced at the moment and the formatter reshuffles them to maximize the first lines.
- if (OMPI_SUCCESS != ret) return ret;
+ if (OMPI_SUCCESS != ret)
+ return ret;
- ret = opal_common_ucx_wpmem_fetch_nb(module->mem, opcode, value, target,
- output_addr, origin_dt_bytes, remote_addr,
- user_req_cb, user_req_ptr);
+ ret =
+ opal_common_ucx_wpmem_fetch_nb(module->mem, opcode, value, target, output_addr,
+ origin_dt_bytes, remote_addr, user_req_cb, user_req_ptr); Is there a way to avoid the line break after the assignment?
module->epoch_type.exposure = NONE_EPOCH;
- *flag = 1;
+ *flag = 1; Maybe constrain this alignment to variable definition blocks? |
@devreal Thanks for the feedback. Let me take a look now. The starting point was the LLVM format (default) tweaked for Open MPI. Likely are a couple of things we need to adjust. |
I had to go reread https://github.com/open-mpi/ompi/wiki/CodingStyle, because I swear we used to have a line about there being a space between cast and variable. It looks like I'm wrong, but it is certainly the common behavior in OMPI. I have no strong opinion, as long as we're consistent. |
I usually don't put spaces after casts - but I don't have strong feelings over it. |
We definitely need to fix the curly braces missing around an added line break for a conditional. That's one of the only things in our coding style :). |
Braces around single line conditionals can't be added by clang-format. Looks like that is something clang-tidy can do. It is a bit more heavyweight but it worth discussing separate from the clang-format discussion. I am used to having both but clang-format is the first step. |
For the assignment alignment. The recommendation is if you want to break assignment alignment that there be a new line. I usually do that. We can also turn off aligning assignment if it is not desired. Example: module.foo = bar;
baz = bar; is how it will align it with module.foo = bar;
baz = bar; to prevent the alignment. I can go either way. Sometimes I like assignment alignmed, sometimes I don't. |
I'd rather disable the assignment alignment check; we've never done that in the wide in OMPI. |
Maybe do the same for the cast spacing? People have different tastes and it's not really critical. And reordering function arguments only to avoid overly long lines will allow people to pick an order that makes sense in the specific instance. |
For cast spacing. Let me get a sense of how widly it is used. If more than half the code base doesn't use it I will remove it otherwise I will leave it. |
And the result is....
|
As for the bit-wise and spacing. Looks like it thinks that we are taking the address of MPI_MODE_NOCHECK. Looking at how to address that one. |
Yup. That is something unique to the & bit-wise operator. Looks like a bug in clang-format as it should be able to tell the difference between taking the address and bit-wise & here. |
Definitely a bug. Happens with '*' as well. It is deciding it is a unary not a binary operator. I found the relevant code in clang and will see if I can fix it. I already have one PR up on clang-format (https://reviews.llvm.org/D98429). edit: Interesting. It only happens if the & happens inside parenthesis. This formats correctly: Ok. I see the problem. assert is treated differently. If you change the variable name from assert it works correctly. We really shouldn't be naming things like that! |
Example:
So, lets fix by renaming all |
Added a couple to tweaks that give slightly better formatting. |
f2457df
to
84640d6
Compare
@hjelmn any update on this? Can you squash down so we can get this in when you have a chance. thanks |
This file works with clang-format --style=file to reformat code to match the style used in Open MPI. This type includes: - No tabs. They are not recommended for Open MPI and can often screw up the formatting. - Tab depth: 4. This is what is used throughout the Open MPI code base. - Max column width: 100. There currently is no standard for Open MPI but we should aim to use something reasonable. - Braces following function definitions occur un-indented on the following line. - Braces following other control statements occur on the same line as the control statement. - Spaces before open parentheses for control statements. No spaces otherwise. This is common accross the code base but not consistent. - Sort headers. Put _config headers before any other header. Followed by internal ("" headers), then system headers (<> headers). - Align consecutive macro definitions. Signed-off-by: Nathan Hjelm <[email protected]>
84640d6
to
c352fba
Compare
Squashed and good to go. |
Just did a trial run on ompi and opal and it builds fine with one small exception (PR already up). Spot checked it and looks good. We can always tweak the format as we go. |
bot:aws:retest |
Thanks @hjelmn can you bring this back to 5.0.x? |
This file works with clang-format --style=file to reformat code to match the style used in PMIx. This type includes: No tabs. They are not recommended for PMIx and can often screw up the formatting. Tab depth: 4. This is what is used throughout the PMIx code base. Max column width: 100. There currently is no standard for PMIx but we should aim to use something reasonable. Braces following function definitions occur un-indented on the following line. Braces following other control statements occur on the same line as the control statement. Spaces always before open parentheses for control statements (if, while, do, etc). This is common across the code base but not consistent. No spaces after function names, and function or macro calls. Align consecutive macro definitions. Tracks open-mpi/ompi#8551 Signed-off-by: Ralph Castain <[email protected]>
This file works with clang-format --style=file to reformat code to match the style used in PRRTE. This type includes: No tabs. They are not recommended for PRRTE and can often screw up the formatting. Tab depth: 4. This is what is used throughout the PRRTE code base. Max column width: 100. There currently is no standard for PRRTE but we should aim to use something reasonable. Braces following function definitions occur un-indented on the following line. Braces following other control statements occur on the same line as the control statement. Spaces always before open parentheses for control statements (if, while, do, etc). This is common across the code base but not consistent. No spaces after function names, and function or macro calls. Align consecutive macro definitions. Tracks open-mpi/ompi#8551 Signed-off-by: Ralph Castain <[email protected]>
This file works with clang-format --style=file to reformat code to match the style used in PMIx. This type includes: No tabs. They are not recommended for PMIx and can often screw up the formatting. Tab depth: 4. This is what is used throughout the PMIx code base. Max column width: 100. There currently is no standard for PMIx but we should aim to use something reasonable. Braces following function definitions occur un-indented on the following line. Braces following other control statements occur on the same line as the control statement. Spaces always before open parentheses for control statements (if, while, do, etc). This is common across the code base but not consistent. No spaces after function names, and function or macro calls. Align consecutive macro definitions. Tracks open-mpi/ompi#8551 Signed-off-by: Ralph Castain <[email protected]> (cherry picked from commit 57a3969)
This file works with clang-format --style=file to reformat code to match the style used in PMIx. This type includes: No tabs. They are not recommended for PMIx and can often screw up the formatting. Tab depth: 4. This is what is used throughout the PMIx code base. Max column width: 100. There currently is no standard for PMIx but we should aim to use something reasonable. Braces following function definitions occur un-indented on the following line. Braces following other control statements occur on the same line as the control statement. Spaces always before open parentheses for control statements (if, while, do, etc). This is common across the code base but not consistent. No spaces after function names, and function or macro calls. Align consecutive macro definitions. Tracks open-mpi/ompi#8551 Signed-off-by: Ralph Castain <[email protected]> (cherry picked from commit 57a3969) (cherry picked from commit 467f88e)
This file works with clang-format --style=file to reformat code to match the
style used in Open MPI. This type includes:
No tabs. They are not recommended for Open MPI and can often screw up the
formatting.
Tab depth: 4. This is what is used throughout the Open MPI code base.
Max column width: 100. There currently is no standard for Open MPI but we
should aim to use something reasonable.
Braces following function definitions occur un-indented on the following
line.
Braces following other control statements occur on the same line as the
control statement.
Spaces always before open parentheses for control statements (if, while, do,
etc). This is common across the code base but not consistent. No spaces
after function names, and function or macro calls.
Align consecutive macro definitions.
Signed-off-by: Nathan Hjelm [email protected]