Skip to content
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 C++ code Formatting #420

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

mabruzzo
Copy link
Contributor

@mabruzzo mabruzzo commented Jul 18, 2024

Pull request summary

This PR adds a support for using clang-format with Enzo-E.
I have added extensive documentation.

Detailed Description

This is something we spoken at length about in the past and there is consensus enforcing standard formatting for C/C++ code would be beneficial.

I feel like this is something we could delay indefinitely. So I made 2 particular choices to make this easier to merge:

  1. To avoid merge-conflict I have excluded just about all existing files from clang-format.
    • If we don't do this we will be trapped in a cycle of waiting for PRs to be closed (which never ends)
    • I think there's value in applying formatting new files and "ripping the band-aid off" later for the older files
  2. I decided to "just make a decision" about code-formatting.
    • This is something that we could endlessly bikeshed, and I think it would be beneficial to choose now.
    • I basically choose the Google C++ style and made a few very minor adjustments based on a few different choices made in the LLVM style (the two styles are extremely similar). I have put forth a similar style in Grackle.
  • I have reformatted a small subset of existing files to show what the style looks like. I'm happy to make changes if you see something egregious.
  • For the most part, I think we should leave iteration on minor details to a later date. That's the beauty of code-formatting.

Relationship with pre-commit

The main challenge of using clang-format is that it is not forward/backward compatible (picking an established style should mitigate some of these issues). - In other words, you need to apply a consistent version of clang-format everywhere.

  • Since clang-format is part of the LLVM project, this can be somewhat non-trivial to enforce (it could be very inconvenient to start requiring people to use).
  • If we didn't take any steps to address this problem, it could be a large inconvenience.

SOLUTION: To address this challenge, this PR configures the pre-commit software and pre-commit.ci continuous integration service (introduced in #419) to invoke clang-format.

  • Essentially, you will be able to modify the formatting of proposed changes in a PR without locally installing any software by leaving a comment that says "pre-commit.ci autofix" to a PR (then a commit will be added to your branch to fix the problem).
  • To easily run clang-format locally, I recommend installing the pre-commit software on your machine and invoking that software. The pre-commit software will download its own personal copy of clang-format to use for formatting (it won't affect the rest of your system)
  • I have added extensive documentation about how this works (and how to do it).

I implemented a similar solution in Cholla and it works well.

This is a major change or addition (that needs 2 reviewers): unknown

Depends on #419

PR Checklist

  • New features are documented with narrative docs.

@mabruzzo
Copy link
Contributor Author

At the last Enzo-E call, it was suggested that I try to format everything and check if it works.

  • A branch of the repository where everything is formatted can be found here.
  • I confirmed that everything definitely compiles when we do that

@jobordner jobordner self-assigned this Oct 17, 2024
@mabruzzo
Copy link
Contributor Author

mabruzzo commented Nov 21, 2024

pre-commit.ci run

EDIT: For posterity, this comment was used to trigger the new pre-commit.ci continutous integration (this was necessary since I introduced precommit.ci after this PR was created and I didn't want to push a new commit)

@mabruzzo
Copy link
Contributor Author

pre-commit.ci autofix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file included in this PR at all? it doesn't seem necessary to do it. Is the goal to demonstrate how the formatting tools work, is it some unrelated cleanup work, or was it included by accident?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(@bwoshea I'm guessing this is one of the "I have reformatted a small subset of existing files" mentioned in the PR description.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as for EnzoComputeCoolingTime.cpp - why is this file included in the PR? (Also, to be clear, I don't see anything wrong with the changes in either of these files, I'm just not clear on why they're included.)

3. the "pre-commit" `git hook <https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks>__`.
This is one of multiple different "hooks" offered by git.
The pre-commit software is named after this hook because it was originally designed to be used with this hook.
While developers are free to use the pre-commit software with the pre-commit hook, that is **NOT** (and is not discussed on this page)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing. "that is NOT" what?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is NOT recommended

(I'll fix that)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe swap the "not recommended" and "recommended" items so the recommended one is first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixed

I replaced

While developers are free to use the pre-commit software with the pre-commit hook, that is NOT (and is not discussed on this page)"
with
We do NOT generally recommend using the pre-commit software with the pre-commit hook.

@jobordner (I think I effectively addressed your comment here)

As an aside: I don't really care whether people are configuring git's hooks to trigger pre-commit hooks, but I think that is a personal choice after they understand the choice (if people don't understand what they are "signing up for", they will be surprised)


To run the checks locally, we encourage you need to install the pre-commit software.
This software is written in python and can be installed with ``pip``.
The `installation instructions <https://pre-commit.com/#installation>`__ also mention an alternative approach where you can run download and run pre-commit without fully installing it (as a "zipapp").
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any known conflicts with the requirements for pre-commit and those of other things Enzo-E developers are likely to use, like yt? (I don't know if that's something that has to be explicitly stated unless there's something that is likely to trip up a lot of developers.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. I imagine the answer is "no" since yt also uses pre-commit


* files that are formatted this way will generally have far fewer merge conflicts.

* Sometimes, you may need to disable clang-format to disable formatting for individual pieces of code.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you comment on why somebody might want to disable clang-format?

Copy link
Contributor Author

@mabruzzo mabruzzo Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm just going to remove that bullet. Off the top of my head, I don't really have a good reason.

I'm sure there are good reasons, but there's no need to invite it

* Sometimes, you may need to disable clang-format to disable formatting for individual pieces of code.
You can do that with a pair of special comments, `described here <https://releases.llvm.org/18.1.8/tools/clang/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code>`__.

* Unless you have a very good reason, [#f1]_ **all** newly introduced C/C++ files will be formatted by ``clang-format``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add something to the effect of "adherence to the formatting guidelines enforced by clang-format is a requirement for acceptance of pull requests, and deviation from that will require you to explicitly justify why your code is not adhering to that format."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this a lot! Here's the replacement text:

Adherence to the formatting guidelines enforced by clang-format is a requirement for acceptance of pull requests, and deviation from that will require you to explicitly justify why your code is not adhering to that format. [#f1]_ We have temporarily disabled clang-format from applying to most older files (listed in .clang-format-ignore), that existed before we adopted clang-format in order to minimize merge conflicts (we plan to enable clang-format for these files in the future).

.. [#f1] A scenario where it might be reasonable to disable clang-format is for a source code file generated by external tools like flex/bison.


* Unless you have a very good reason, [#f1]_ **all** newly introduced C/C++ files will be formatted by ``clang-format``.
With that said, we have temporarily disabled clang-format from applying to most older files (listed in ``.clang-format-ignore``, that existed before we adopted clang-format in order to minimize merge conflicts (we plan to enable ``clang-format`` for these files in the future).
We plan to enable formatting on these files in the future.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line (148) is redundant with the previous sentence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed!

@bwoshea
Copy link
Contributor

bwoshea commented Jan 16, 2025

@mabruzzo I looked at all of the files in this PR and things look just fine. However (as I commented at the top of a couple of the files) it's not clear to me why the files in Enzo/chemistry are included in this PR. It looks like they've had the new formatting applied to them, so I suspect that they're an example of a formatted version of the code. Can you comment on that? Otherwise, I am happy to approve this PR.

@mabruzzo
Copy link
Contributor Author

Those files are indeed an example of a formatted version of the code.

Copy link
Contributor

@bwoshea bwoshea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving given that @mabruzzo answered my question about the chemistry code. @jobordner needs to look at this next!

Copy link
Contributor

@jobordner jobordner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, thanks for working on this!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(@bwoshea I'm guessing this is one of the "I have reformatted a small subset of existing files" mentioned in the PR description.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants