Skip to content

Commit bcc2a05

Browse files
goldsboroughfacebook-github-bot
authored andcommitted
Enable clang-tidy in CI (pytorch#12213)
Summary: At long last, we will have clang-tidy enabled in CI. For a while I thought I could clean up the project enough to enable clang-tidy with all checks enabled, but I figure it's smarter to set up the minimal checks and at least have those in CI. We can fix more going forward. ezyang apaszke Pull Request resolved: pytorch#12213 Differential Revision: D10183069 Pulled By: goldsborough fbshipit-source-id: 7ecd2d368258f46efe23a2449c0a206d10f3a769
1 parent c9f9df0 commit bcc2a05

13 files changed

+175
-163
lines changed

.clang-tidy

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,11 @@
11
---
22
# NOTE: there must be no spaces before the '-', so put the comma first.
33
Checks: '
4-
*
5-
,clang-analyzer-*
6-
,modernize-*
7-
,-cert-dcl21-cpp
8-
,-cert-err58-cpp
9-
,-cert-err60-cpp
10-
,-clang-diagnostic-*
11-
,-cppcoreguidelines-owning-memory
12-
,-cppcoreguidelines-pro-bounds-array-to-pointer-decay
13-
,-cppcoreguidelines-pro-bounds-constant-array-index
14-
,-cppcoreguidelines-pro-type-member-init
15-
,-cppcoreguidelines-pro-type-static-cast-downcast
16-
,-cppcoreguidelines-pro-type-union-access
17-
,-cppcoreguidelines-pro-type-vararg
18-
,-cppcoreguidelines-special-member-functions
19-
,-fuchsia-*
20-
,-google-build-using-namespace
21-
,-google-default-arguments
22-
,-google-explicit-constructor
23-
,-google-readability-braces-around-statements
24-
,-google-readability-namespace-comments
25-
,-google-readability-todo
26-
,-google-runtime-references
27-
,-google-runtime-references
28-
,-hicpp-braces-around-statements
29-
,-hicpp-explicit-conversions
30-
,-hicpp-member-init
31-
,-hicpp-no-array-decay
32-
,-hicpp-signed-bitwise
33-
,-hicpp-special-member-functions
34-
,-hicpp-vararg
35-
,-llvm-header-guard
36-
,-llvm-include-order
37-
,-llvm-namespace-comment
38-
,-misc-unused-parameters
39-
,-modernize-make-unique
40-
,-modernize-use-default-member-init
41-
,-performance-unnecessary-value-param
42-
,-readability-braces-around-statements
43-
,-readability-else-after-return
44-
,-readability-implicit-bool-conversion
45-
,-readability-named-parameter
4+
-*
5+
,modernize-deprecated-headers
466
'
47-
WarningsAsErrors: ''
48-
HeaderFilterRegex: 'torch/csrc/'
7+
WarningsAsErrors: '*'
8+
HeaderFilterRegex: 'torch/csrc/.*'
499
AnalyzeTemporaryDtors: false
5010
CheckOptions:
5111
...

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ matrix:
2929
- env: CPP_DOC_CHECK
3030
install: sudo apt-get install -y doxygen
3131
script: cd docs/cpp/source && ./check-doxygen.sh
32+
- env: CLANG_TIDY
33+
python: "3.6"
34+
addons:
35+
apt:
36+
sources:
37+
- ubuntu-toolchain-r-test
38+
- llvm-toolchain-trusty-6.0
39+
packages: clang-tidy-6.0
40+
script: tools/run-clang-tidy-in-ci.sh -e "$(which clang-tidy-6.0)"

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ if (NOT MSVC)
1010
set(CMAKE_C_STANDARD 11)
1111
endif()
1212

13+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
14+
1315
# One variable that determines whether the current cmake process is being run
1416
# with the main Caffe2 library. This is useful for building modules - if
1517
# modules are built with the main Caffe2 library then one does not need to do

CONTRIBUTING.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,36 @@ static_assert(std::is_same(A*, decltype(A::singelton()))::value, "hmm");
354354
are too large. Splitting such files into separate files helps.
355355
(Example: `THTensorMath`, `THTensorMoreMath`, `THTensorEvenMoreMath`.)
356356

357+
### Running Clang-Tidy
358+
359+
[Clang-Tidy](https://clang.llvm.org/extra/clang-tidy/index.html) is a C++
360+
linter and static analysis tool based on the clang compiler. We run clang-tidy
361+
in our CI to make sure that new C++ code is safe, sane and efficient. See our
362+
[.travis.yml](https://github.com/pytorch/pytorch/blob/master/.travis.yml) file
363+
for the simple commands we use for this.
364+
365+
To run clang-tidy locally, follow these steps:
366+
367+
1. Install clang-tidy. First, check if you already have clang-tidy by simply
368+
writing `clang-tidy` in your terminal. If you don't yet have clang-tidy, you
369+
should be able to install it easily with your package manager, e.g. by writing
370+
`apt-get install clang-tidy` on Ubuntu. See https://apt.llvm.org for details on
371+
how to install the latest version. Note that newer versions of clang-tidy will
372+
have more checks than older versions. In our CI, we run clang-tidy-6.0.
373+
374+
2. Use our driver script to run clang-tidy over any changes relative to some
375+
git revision (you may want to replace `HEAD~1` with `HEAD` to pick up
376+
uncommitted changes). Changes are picked up based on a `git diff` with the
377+
given revision:
378+
```sh
379+
$ python tools/clang_tidy.py -d build -p torch/csrc -r HEAD~1
380+
```
381+
382+
Above, it is assumed you are in the PyTorch root folder. `path/to/build` should
383+
be the path to where you built PyTorch from source, e.g. `build` in the PyTorch
384+
root folder if you used `setup.py build`. You can use `-c <clang-tidy-binary>`
385+
to change the clang-tidy this script uses.
386+
357387
## Caffe2 notes
358388

359389
In 2018, we merged Caffe2 into the PyTorch source repository. While the

tools/autograd/templates/Functions.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
// NB: Must be at the top of file to avoid including the deprecated "math.h".
2+
// https://stackoverflow.com/questions/6563810/m-pi-works-with-math-h-but-not-with-cmath-in-visual-studio
3+
#ifdef _MSC_VER
4+
#define _USE_MATH_DEFINES
5+
#include <cmath>
6+
#endif
7+
18
#include "Functions.h"
29
#include <ATen/Utils.h>
310
#include <ATen/core/TensorOptions.h>
@@ -6,12 +13,7 @@
613
#include <ATen/ExpandUtils.h>
714
#include <ATen/core/Reduction.h>
815

9-
// define constants like M_PI and C keywords for MSVC
10-
#ifdef _MSC_VER
11-
#define _USE_MATH_DEFINES
1216
#include <ciso646>
13-
#endif
14-
#include <math.h>
1517
#include <algorithm>
1618
#include <numeric>
1719

tools/build_pytorch_libs.bat

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ goto:eof
201201
-DMKLDNN_LIBRARY="%MKLDNN_LIBRARY%" ^
202202
-DATEN_NO_CONTRIB=1 ^
203203
-DCMAKE_INSTALL_PREFIX="%INSTALL_DIR%" ^
204-
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 ^
205204
-DCMAKE_C_FLAGS="%USER_CFLAGS%" ^
206205
-DCMAKE_CXX_FLAGS="/EHa %USER_CFLAGS%" ^
207206
-DCMAKE_EXE_LINKER_FLAGS="%USER_LDFLAGS%" ^

tools/build_pytorch_libs.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function build() {
197197
-DCMAKE_DEBUG_POSTFIX="" \
198198
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
199199
${@:2} \
200-
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 ${CMAKE_ARGS[@]}
200+
${CMAKE_ARGS[@]}
201201
fi
202202
${CMAKE_INSTALL} -j"$MAX_JOBS"
203203
popd
@@ -301,7 +301,6 @@ function build_caffe2() {
301301
-DMKLDNN_LIB_DIR=$MKLDNN_LIB_DIR \
302302
-DMKLDNN_LIBRARY=$MKLDNN_LIBRARY \
303303
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
304-
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
305304
-DCMAKE_C_FLAGS="$USER_CFLAGS" \
306305
-DCMAKE_CXX_FLAGS="$USER_CFLAGS" \
307306
-DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS $USER_LDFLAGS" \

0 commit comments

Comments
 (0)