Skip to content

Commit 8bdc389

Browse files
authored
style: clang-tidy checks (#455)
* style: clang-tidy checks * style: a few more checks * style: casting
1 parent 7c34fb0 commit 8bdc389

File tree

12 files changed

+91
-23
lines changed

12 files changed

+91
-23
lines changed

.clang-tidy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FormatStyle: file
2+
3+
Checks: '
4+
modernize-*,
5+
-modernize-use-trailing-return-type,
6+
-modernize-avoid-c-arrays,
7+
llvm-namespace-comment,
8+
readability-container-size-empty,
9+
google-readability-casting,
10+
'
11+
12+
HeaderFilterRegex: 'bh_python/.*hpp'

.github/workflows/tests.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ jobs:
2121
with:
2222
extra_args: --hook-stage manual
2323

24+
clang-tidy:
25+
name: Clang-Tidy
26+
runs-on: ubuntu-latest
27+
container: silkeh/clang:10
28+
29+
steps:
30+
- uses: actions/checkout@v1
31+
with:
32+
submodules: true
33+
34+
- name: Install requirements
35+
run: apt-get update && apt-get install -y python3-dev python3-pip
36+
37+
- name: Install extra requirements
38+
run: python3 -m pip install setuptools_scm
39+
40+
- name: Configure
41+
run: cmake -S . -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);--warnings-as-errors=*"
42+
43+
- name: Build
44+
run: cmake --build build -j 2
45+
46+
2447
cmake:
2548
runs-on: ubuntu-latest
2649
strategy:

CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,15 @@ option(BOOST_HISTOGRAM_ERRORS "Make warnings errors (for CI mostly)")
105105

106106
# Adding warnings
107107
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
108-
target_compile_options(_core PRIVATE -Wall -Wextra -pedantic-errors -Wconversion
109-
-Wsign-conversion -Wsign-compare)
108+
target_compile_options(
109+
_core
110+
PRIVATE -Wall
111+
-Wextra
112+
-pedantic-errors
113+
-Wconversion
114+
-Wsign-conversion
115+
-Wsign-compare
116+
-Wno-unused-value)
110117
if(BOOST_HISTOGRAM_ERRORS)
111118
target_compile_options(_core PRIVATE -Werror)
112119
endif()
@@ -182,7 +189,7 @@ function(python_import)
182189
if(FAILED_NAMES)
183190
list(JOIN FAILED_NAMES " " FAILED_NAMES)
184191
message(
185-
FATAL_ERROR
192+
WARNING
186193
"Dependencies missing, try installing:\n ${PYTHON_EXECUTABLE} -m pip install ${FAILED_NAMES}\n"
187194
)
188195
endif()

CONTRIBUTING.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,30 @@ pre-commit autoupdate
176176
> when running pre-commit, and instead run `clang-format -style=file -i
177177
> <files>` yourself.
178178
179+
## Clang-Tidy
180+
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.
183+
184+
```bash
185+
docker run --rm -v $PWD:/pybind11 -it silkeh/clang:10
186+
apt-get update && apt-get install python3-dev
187+
cmake -S pybind11/ -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);-fix"
188+
cmake --build build
189+
```
190+
191+
Remember to build single-threaded if applying fixes!
192+
193+
## Include what you use
194+
195+
To run include what you use, install (`brew install include-what-you-use` on
196+
macOS), then run:
197+
198+
```bash
199+
cmake -S . -B build-iwyu -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=$(which include-what-you-use)
200+
cmake --build build
201+
```
202+
179203
## Common tasks
180204

181205

include/bh_python/array_like.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
template <class T>
1515
py::array_t<T> array_like(py::object obj) {
1616
if(!py::isinstance<py::array>(obj)) {
17-
ssize_t shape[1] = {0}; // if scalar
17+
py::ssize_t shape[1] = {0}; // if scalar
1818
if(py::isinstance<py::sequence>(obj) && !py::isinstance<py::str>(obj)) {
1919
// if sequence
2020
auto seq = py::cast<py::sequence>(obj);
21-
shape[0] = static_cast<ssize_t>(seq.size());
21+
shape[0] = static_cast<py::ssize_t>(seq.size());
2222
}
2323
return py::array_t<T>(shape);
2424
}

include/bh_python/axis.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ class boolean : public bh::axis::integer<int, metadata_t, option::none_t> {
117117
bh::axis::index_type end,
118118
unsigned merge)
119119
: integer(src, begin, end, merge) {}
120-
boolean(const boolean& other)
121-
: integer(other) {}
120+
boolean(const boolean& other) = default;
122121

123122
bh::axis::index_type index(int x) const noexcept {
124123
return integer::index(x == 0 ? 0 : 1);
@@ -206,11 +205,9 @@ decltype(auto) unchecked_bin(const A& ax, bh::axis::index_type i) {
206205
template <class A>
207206
py::array_t<double> edges(const A& ax, bool flow = false, bool numpy_upper = false) {
208207
auto continuous = [flow, numpy_upper](const auto& ax) {
209-
using AX = std::decay_t<decltype(ax)>;
210-
using index_type
211-
= std::conditional_t<bh::axis::traits::is_continuous<AX>::value,
212-
bh::axis::real_index_type,
213-
bh::axis::index_type>;
208+
using AX = std::decay_t<decltype(ax)>;
209+
using index_type = bh::axis::index_type;
210+
214211
const index_type underflow
215212
= flow && (bh::axis::traits::get_options<AX>::test(option::underflow));
216213
const index_type overflow

include/bh_python/histogram.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ py::buffer_info make_buffer(bh::histogram<A, bh::unlimited_storage<Allocator>>&
101101
template <class F, int Opt>
102102
const F& pyarray_at(const py::array_t<F, Opt>& input,
103103
std::vector<py::ssize_t> indexes) {
104-
const py::ssize_t rank = static_cast<py::ssize_t>(indexes.size());
104+
const auto rank = static_cast<py::ssize_t>(indexes.size());
105105
if(input.ndim() != rank)
106106
throw py::value_error(
107107
"The input array dimensions must match the index dimensions");

include/bh_python/kwargs.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ inline void none_only_arg(py::kwargs& kwargs, const char* name) {
4747

4848
/// Run this last; it will provide an error if other keyword are still present
4949
inline void finalize_args(const py::kwargs& kwargs) {
50-
if(kwargs.size() > 0) {
50+
if(!kwargs.empty()) {
5151
auto keys = py::str(", ").attr("join")(kwargs.attr("keys")());
5252
throw py::type_error(py::str("Keyword(s) {0} not expected").format(keys));
5353
}

include/bh_python/register_histogram.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ auto register_histogram(py::module& m, const char* name, const char* desc) {
4949
.def("__copy__", [](const histogram_t& self) { return histogram_t(self); })
5050
.def("__deepcopy__",
5151
[](const histogram_t& self, py::object memo) {
52-
histogram_t* a = new histogram_t(self);
52+
auto* a = new histogram_t(self);
5353
py::module copy = py::module::import("copy");
5454
for(unsigned i = 0; i < a->rank(); i++) {
5555
bh::unsafe_access::axis(*a, i).metadata()
@@ -135,7 +135,8 @@ auto register_histogram(py::module& m, const char* name, const char* desc) {
135135
.def(
136136
"axis",
137137
[](const histogram_t& self, int i) -> py::object {
138-
unsigned ii = i < 0 ? self.rank() - (unsigned)std::abs(i) : (unsigned)i;
138+
unsigned ii = i < 0 ? self.rank() - static_cast<unsigned>(std::abs(i))
139+
: static_cast<unsigned>(i);
139140

140141
if(ii < self.rank()) {
141142
const axis_variant& var = self.axis(ii);

include/bh_python/regular_numpy.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ namespace axis {
1717
/// Mimics the numpy behavoir exactly.
1818
class regular_numpy : public bh::axis::regular<double, bh::use_default, metadata_t> {
1919
using value_type = double;
20-
double stop_;
20+
double stop_{0};
2121

2222
public:
2323
regular_numpy(unsigned n, value_type start, value_type stop, metadata_t meta = {})
2424
: regular(n, start, stop, meta)
2525
, stop_(stop) {}
2626

2727
regular_numpy()
28-
: regular()
29-
, stop_(0) {}
28+
: regular() {}
3029

3130
bh::axis::index_type index(value_type v) const {
3231
return v <= stop_ ? std::min(regular::index(v), size() - 1) : regular::index(v);

0 commit comments

Comments
 (0)