Skip to content

Setup clang-format code beautification#657

Merged
stevengj merged 6 commits intomasterfrom
clang-format
Jan 10, 2019
Merged

Setup clang-format code beautification#657
stevengj merged 6 commits intomasterfrom
clang-format

Conversation

@stevengj
Copy link
Collaborator

@stevengj stevengj commented Jan 8, 2019

The best tool I can find for automatic beautification (indenting, brace placement, etcetera) of C++ code is clang-format, which seems to be the most modern and well-maintained open-source tool for this right now

This PR doesn't actually run clang-format yet (which will cause conflicts on every other PR), but it prepares the way, via:

  • A .clang-format file in the top-level directory sets the default format when you run clang-format.
    The clang-format defaults (LLVM style, I think) seem pretty reasonable, with a couple of tweaks.

  • For it to format our LOOP_IN_FOO and FOR_FOO looping macros correctly, the loop bodies must be enclosed in braces. That is the cause of most of the changes in this PR.

  • We don't want to run clang-format on the SWIG-generated files (mainly because it is very slow), so I renamed them to *.cxx to easily distinguish them from hand-written code

You can just run find . -name '*.*pp' | xargs clang-format -i in the source tree to automatically fix all of the code spacing.

Once we actually run clang-format and check into master, any existing PR will get lots of conflicts. You might be able to do git rebase -Xignore-all-space master to mostly fix this. Alternatively, you could run clang-format on your files and rebase with the reformatted files. Could be a little bit of a pain for large PRs.

cc @ChristopherHogan

@stevengj
Copy link
Collaborator Author

stevengj commented Jan 8, 2019

See also this article on rebasing branches after running clang-format: https://engineering.mongodb.com/post/succeeding-with-clangformat-part-3-persisting-the-change … they have a suggested procedure and even a Python script, so hopefully this won't be too difficult.

@stevengj
Copy link
Collaborator Author

Looks like I misplaced a brace in boundaries.cpp.

@stevengj stevengj merged commit 9813c6e into master Jan 10, 2019
@stevengj stevengj deleted the clang-format branch January 10, 2019 18:29
bencbartlett pushed a commit to bencbartlett/meep that referenced this pull request Sep 9, 2021
* clang-format configuration

* clang-format needs braces after LOOP macros to format them nicely

* whoops

* rename SWIG-generated files to *.cxx

* explicitly specify LLVM style

* fix brace placement
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.

1 participant