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

Global clang-format #3074

Closed
wants to merge 4 commits into from
Closed

Global clang-format #3074

wants to merge 4 commits into from

Conversation

rwgk
Copy link
Collaborator

@rwgk rwgk commented Jul 3, 2021

EXPERIMENTAL integration of clang-format hook into pre-commit.
NOT TO BE MERGED until after the 2.7 release.

At the moment this DRAFT PR is is to see what's involved and what the result looks like.

Using the same docker container as used for clang-tidy (silkeh/clang:12).

@Skylion007
Copy link
Collaborator

@rwgk Could you hold off on this for a sec, there are some readability clang-tidy checks that will also enforce LLVM style more strictly and it would be better to be one massive formatting diff than two.

@rwgk
Copy link
Collaborator Author

rwgk commented Jul 3, 2021

@rwgk Could you hold off on this for a sec, there are some readability clang-tidy checks that will also enforce LLVM style more strictly and it would be better to be one massive formatting diff than two.

Absolutely: see the updated PR description.

@rwgk
Copy link
Collaborator Author

rwgk commented Jul 8, 2021

Note to self: reverting the clang-format changes in these files did NOT resolve the MSVC 2015 failures:

include/pybind11/chrono.h
include/pybind11/pybind11.h
tests/test_chrono.cpp

}

int closeFile = 1;
std::string fname_str = (std::string) fname;
#if PY_VERSION_HEX >= 0x03040000
# if PY_VERSION_HEX >= 0x03040000
Copy link
Collaborator

Choose a reason for hiding this comment

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

Grr, is this the style we really want here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good question.
I was thinking of formatting globally in different ways (multiple PRs), then we can look at the results holistically.

BTW: I'm still hung up on one last (I hope) thing, MSVC 2015 failures after clang-format.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

BTW: I fixed the MSVC 2015 issue.

To your style question, there are only 3 choices: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
PPDIS_None, PPDIS_AfterHash, PPDIS_BeforeHash

What we have (AfterHash) seems like the most OK-ish option to me.
But really, any of these is better than having to mess around with the whitespaces manually.

item_accessor operator[](handle key) const;
/// See above (the only difference is that they key is provided as a string literal)
item_accessor operator[](const char *key) const;

// clang-format off
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To protect the \rst to \endrst block enclosed by the // clang-format off, on.

I added the clang-format meta-comments systematically rather than trying to figure out case by case if they are needed or not. Possibly some could be omitted, but I believe for blocks that are deliberately formatted as rst it's better to systematically exclude them from clang-format.

Note that the sphinx error messages are completely useless if something goes wrong. I spent quite a bit of time figuring out why the Documentation GitHub action was failing. Now I know, but letting clang-format loose on rst is likely to turn into time-consuming surprises for others.

@rwgk rwgk force-pushed the global_clang_format branch 2 times, most recently from fc6ebd0 to 7585337 Compare July 9, 2021 16:30
@rwgk rwgk force-pushed the global_clang_format branch 2 times, most recently from d793a1f to c9610a9 Compare July 13, 2021 20:48
Ralf W. Grosse-Kunstleve added 4 commits July 13, 2021 15:06
…ollowed by tools/check-style.sh).

* Manually moving `// NOLINT` comments so that clang-format does not move them to the wrong places.

* Manually reformatting comments related to `static_assert`s so that clang-format does not need two passes.

* Empty lines between #includes, to prevent clang-format from shuffling the order and thereby confusing MSVC 2015.

* git diff -U0 --no-color HEAD^ | python3 $HOME/clone/llvm-project/clang/tools/clang-format/clang-format-diff.py -p1 -style=file -i
@rwgk
Copy link
Collaborator Author

rwgk commented Aug 7, 2021

This PR is completely stale, even as a demo. The only slightly valuable commit is in the diff below. Closing & deleting branch.

diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 133eb6df4e..6af90465ae 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -12,6 +12,10 @@
 #
 # See https://github.com/pre-commit/pre-commit
 
+ci:
+  skip:
+  - docker-clang-format
+
 repos:
 # Standard hooks
 - repo: https://github.com/pre-commit/pre-commit-hooks
@@ -108,6 +112,19 @@ repos:
     entry: PyBind|Numpy|Cmake|CCache
     exclude: .pre-commit-config.yaml
 
+- repo: local
+  hooks:
+  - id: docker-clang-format
+    name: Docker Clang Format
+    language: docker_image
+    types:
+    - c++
+    entry: silkeh/clang:12
+    args:
+    - clang-format
+    - -style=file
+    - -i
+
 - repo: local
   hooks:
   - id: check-style

@rwgk rwgk closed this Aug 7, 2021
@rwgk rwgk deleted the global_clang_format branch August 7, 2021 15:05
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.

2 participants