Skip to content

Commit 2db0264

Browse files
henryiiirwgk
andauthored
style: add clang-format file (#2310)
* style: adding clang-format as manual hook * docs: adding a Clang-Format section. (#2803) Co-authored-by: Ralf W. Grosse-Kunstleve <[email protected]>
1 parent 130c995 commit 2db0264

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

.clang-format

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
# See all possible options and defaults with:
3+
# clang-format --style=llvm --dump-config
4+
BasedOnStyle: LLVM
5+
AccessModifierOffset: -4
6+
AlignConsecutiveAssignments: true
7+
AlwaysBreakTemplateDeclarations: Yes
8+
BinPackArguments: false
9+
BinPackParameters: false
10+
BreakBeforeBinaryOperators: All
11+
BreakConstructorInitializers: BeforeColon
12+
ColumnLimit: 99
13+
IndentCaseLabels: true
14+
IndentPPDirectives: AfterHash
15+
IndentWidth: 4
16+
Language: Cpp
17+
SpaceAfterCStyleCast: true
18+
# SpaceInEmptyBlock: true # too new
19+
Standard: Cpp11
20+
TabWidth: 4
21+
...

.github/CONTRIBUTING.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,35 @@ name, pre-commit):
176176
pre-commit install
177177
```
178178

179+
### Clang-Format
180+
181+
As of v2.6.2, pybind11 ships with a [`clang-format`][clang-format]
182+
configuration file at the top level of the repo (the filename is
183+
`.clang-format`). Currently, formatting is NOT applied automatically, but
184+
manually using `clang-format` for newly developed files is highly encouraged.
185+
To check if a file needs formatting:
186+
187+
```bash
188+
clang-format -style=file --dry-run some.cpp
189+
```
190+
191+
The output will show things to be fixed, if any. To actually format the file:
192+
193+
```bash
194+
clang-format -style=file -i some.cpp
195+
```
196+
197+
Note that the `-style-file` option searches the parent directories for the
198+
`.clang-format` file, i.e. the commands above can be run in any subdirectory
199+
of the pybind11 repo.
200+
179201
### Clang-Tidy
180202

181-
To run Clang tidy, the following recipe should work. Files will be modified in
182-
place, so you can use git to monitor the changes.
203+
[`clang-tidy`][clang-tidy] performs deeper static code analyses and is
204+
more complex to run, compared to `clang-format`, but support for `clang-tidy`
205+
is built into the pybind11 CMake configuration. To run `clang-tidy`, the
206+
following recipe should work. Files will be modified in place, so you can
207+
use git to monitor the changes.
183208

184209
```bash
185210
docker run --rm -v $PWD:/pybind11 -it silkeh/clang:10
@@ -198,7 +223,7 @@ cmake -S . -B build-iwyu -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=$(which include-what-y
198223
cmake --build build
199224
```
200225

201-
The report is sent to stderr; you can pip it into a file if you wish.
226+
The report is sent to stderr; you can pipe it into a file if you wish.
202227

203228
### Build recipes
204229

@@ -325,6 +350,8 @@ if you really want to.
325350

326351

327352
[pre-commit]: https://pre-commit.com
353+
[clang-format]: https://clang.llvm.org/docs/ClangFormat.html
354+
[clang-tidy]: https://clang.llvm.org/extra/clang-tidy/
328355
[pybind11.readthedocs.org]: http://pybind11.readthedocs.org/en/latest
329356
[issue tracker]: https://github.com/pybind/pybind11/issues
330357
[gitter]: https://gitter.im/pybind/Lobby

include/pybind11/iostream.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ PYBIND11_NAMESPACE_END(detail)
104104
.. code-block:: cpp
105105
106106
{
107-
py::scoped_ostream_redirect output{std::cerr, py::module_::import("sys").attr("stderr")};
108-
std::cerr << "Hello, World!";
107+
py::scoped_ostream_redirect output{
108+
std::cerr,
109+
py::module::import("sys").attr("stderr")
110+
};
109111
}
110112
\endrst */
111113
class scoped_ostream_redirect {

setup.cfg

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ ignore =
4343
docs/**
4444
tools/**
4545
include/**
46-
.appveyor.yml
47-
.cmake-format.yaml
48-
.gitmodules
49-
.pre-commit-config.yaml
50-
.readthedocs.yml
51-
.clang-tidy
46+
.*
5247
pybind11/include/**
5348
pybind11/share/**
5449
CMakeLists.txt

0 commit comments

Comments
 (0)