Skip to content

Commit

Permalink
Make clang-tidy to use dmlc embedded gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
trams committed Oct 7, 2019
1 parent 018280b commit 91d53fa
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def ClangTidy() {
def docker_binary = "docker"
def dockerArgs = "--build-arg CUDA_VERSION=9.2"
sh """
${dockerRun} ${container_type} ${docker_binary} ${dockerArgs} tests/ci_build/clang_tidy.sh
${dockerRun} ${container_type} ${docker_binary} ${dockerArgs} python3 tests/ci_build/tidy.py
"""
deleteDir()
}
Expand Down
8 changes: 3 additions & 5 deletions doc/contrib/coding_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,19 @@ To run this check locally, run the following command from the top level source t
.. code-block:: bash
cd /path/to/xgboost/
python3 tests/ci_build/tidy.py --gtest-path=/path/to/google-test
where ``--gtest-path`` option specifies the full path of Google Test library.
python3 tests/ci_build/tidy.py
Also, the script accepts two optional integer arguments, namely ``--cpp`` and ``--cuda``. By default they are both set to 1, meaning that both C++ and CUDA code will be checked. If the CUDA toolkit is not installed on your machine, you'll encounter an error. To exclude CUDA source from linting, use:

.. code-block:: bash
cd /path/to/xgboost/
python3 tests/ci_build/tidy.py --cuda=0 --gtest-path=/path/to/google-test
python3 tests/ci_build/tidy.py --cuda=0
Similarly, if you want to exclude C++ source from linting:

.. code-block:: bash
cd /path/to/xgboost/
python3 tests/ci_build/tidy.py --cpp=0 --gtest-path=/path/to/google-test
python3 tests/ci_build/tidy.py --cpp=0
18 changes: 0 additions & 18 deletions tests/ci_build/clang_tidy.sh

This file was deleted.

15 changes: 6 additions & 9 deletions tests/ci_build/tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ class ClangTidy(object):
'''
clang tidy wrapper.
Args:
gtest_path: Full path of Google Test library.
cpp_lint: Run linter on C++ source code.
cuda_lint: Run linter on CUDA source code.
'''
def __init__(self, gtest_path, cpp_lint, cuda_lint):
self.gtest_path = gtest_path
def __init__(self, cpp_lint, cuda_lint):
self.cpp_lint = cpp_lint
self.cuda_lint = cuda_lint
print('Using Google Test from {}'.format(self.gtest_path))
print('Run linter on CUDA: ', self.cuda_lint)
print('Run linter on C++:', self.cpp_lint)
if not self.cpp_lint and not self.cuda_lint:
Expand All @@ -61,8 +58,7 @@ def _generate_cdb(self):
os.mkdir(self.cdb_path)
os.chdir(self.cdb_path)
cmake_args = ['cmake', '..', '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON',
'-DGOOGLE_TEST=ON', '-DGTEST_ROOT={}'.format(
self.gtest_path)]
'-DGOOGLE_TEST=ON', '-DUSE_DMLC_GTEST=ON']
if self.cuda_lint:
cmake_args.extend(['-DUSE_CUDA=ON', '-DUSE_NCCL=ON'])
subprocess.run(cmake_args)
Expand Down Expand Up @@ -108,6 +104,8 @@ def convert_nvcc_command_to_clang(self, command):
'--cuda-gpu-arch=sm_' + capability)
elif components[i].find('--std=c++11') != -1:
converted_components.append('-std=c++11')
elif components[i].startswith('-isystem='):
converted_components.extend(components[i].split('='))
else:
converted_components.append(components[i])

Expand Down Expand Up @@ -156,6 +154,7 @@ def should_lint(path):
return False
isxgb = path.find('rabit') == -1
isxgb = isxgb and path.find('dmlc-core') == -1
isxgb = isxgb and (not path.startswith(self.cdb_path))
if isxgb:
return True

Expand Down Expand Up @@ -235,13 +234,11 @@ def test_tidy():
parser = argparse.ArgumentParser(description='Run clang-tidy.')
parser.add_argument('--cpp', type=int, default=1)
parser.add_argument('--cuda', type=int, default=1)
parser.add_argument('--gtest-path', required=True,
help='Full path of Google Test library directory')
args = parser.parse_args()

test_tidy()

with ClangTidy(args.gtest_path, args.cpp, args.cuda) as linter:
with ClangTidy(args.cpp, args.cuda) as linter:
passed = linter.run()
if not passed:
sys.exit(1)
2 changes: 1 addition & 1 deletion tests/cpp/common/test_enum_class_param.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST(EnumClassParam, Basic) {
{"foo", "frog"}, {"bar", "10"}
};
// try initializing
param.Init(kwargs);
param.Init(kwargs); // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult)
ASSERT_EQ(param.foo, Foo::kFrog);
ASSERT_EQ(param.bar, 10);

Expand Down

0 comments on commit 91d53fa

Please sign in to comment.