Add clang-tidy target. Closes #2383.

pull/5988/head
Jussi Pakkanen 6 years ago
parent 5a52983f16
commit d3b389f349
  1. 8
      docs/markdown/snippets/clangtidy.md
  2. 8
      mesonbuild/backend/ninjabackend.py
  3. 38
      mesonbuild/scripts/clangtidy.py
  4. 13
      run_unittests.py
  5. 1
      test cases/unit/70 clang-tidy/.clang-tidy
  6. 7
      test cases/unit/70 clang-tidy/cttest.cpp
  7. 3
      test cases/unit/70 clang-tidy/meson.build

@ -0,0 +1,8 @@
## Clang-tidy target
If `clang-tidy` is installed and the project's source root contains a
`.clang-tidy` (or `_clang-tidy`) file, Meson will automatically define
a `clang-tidy` target that runs Clang-Tidy on all source files.
If you have defined your own `clang-tidy` target, Meson will not
generate its own target.

@ -2657,7 +2657,6 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
self.create_target_alias('meson-scan-build')
def generate_clangtool(self, name):
import shutil
target_name = 'clang-' + name
if not os.path.exists(os.path.join(self.environment.source_dir, '.clang-' + name)) and \
not os.path.exists(os.path.join(self.environment.source_dir, '_clang-' + name)):
@ -2677,10 +2676,17 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
return
self.generate_clangtool('format')
def generate_clangtidy(self):
import shutil
if not shutil.which('clang-tidy'):
return
self.generate_clangtool('tidy')
# For things like scan-build and other helper tools we might have.
def generate_utils(self):
self.generate_scanbuild()
self.generate_clangformat()
self.generate_clangtidy()
cmd = self.environment.get_build_command() + ['--internal', 'uninstall']
elem = NinjaBuildElement(self.all_outputs, 'meson-uninstall', 'CUSTOM_COMMAND', 'PHONY')
elem.add_item('COMMAND', cmd)

@ -0,0 +1,38 @@
# Copyright 2019 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import subprocess
from concurrent.futures import ThreadPoolExecutor
from ..compilers import lang_suffixes
def clangformat(srcdir_name, builddir_name):
srcdir = pathlib.Path(srcdir_name)
suffixes = set(lang_suffixes['c']).union(set(lang_suffixes['cpp']))
suffixes.add('h')
futures = []
with ThreadPoolExecutor() as e:
for f in (x for suff in suffixes for x in srcdir.glob('**/*.' + suff)):
strf = str(f)
if strf.startswith(builddir_name):
continue
futures.append(e.submit(subprocess.check_call, ['clang-tidy', '-p', builddir_name, strf]))
[x.result() for x in futures]
return 0
def run(args):
srcdir_name = args[0]
builddir_name = args[1]
return clangformat(srcdir_name, builddir_name)

@ -3659,6 +3659,19 @@ recommended as it is not supported on some platforms''')
if os.path.exists(testheader):
os.unlink(testheader)
@skipIfNoExecutable('clang-tidy')
def test_clang_tidy(self):
if self.backend is not Backend.ninja:
raise unittest.SkipTest('Clang-tidy is for now only supported on Ninja, not {}'.format(self.backend.name))
if shutil.which('c++') is None:
raise unittest.SkipTest('Clang-tidy breaks when ccache is used and "c++" not in path.')
if is_osx():
raise unittest.SkipTest('Apple ships a broken clang-tidy that chokes on -pipe.')
testdir = os.path.join(self.unit_test_dir, '70 clang-tidy')
self.init(testdir, override_envvars={'CXX': 'c++'})
out = self.run_target('clang-tidy')
self.assertIn('cttest.cpp:4:20', out)
def test_introspect_buildoptions_without_configured_build(self):
testdir = os.path.join(self.unit_test_dir, '59 introspect buildoptions')
testfile = os.path.join(testdir, 'meson.build')

@ -0,0 +1 @@
Checks: '-*,modernize-use-bool-literals'

@ -0,0 +1,7 @@
#include<cstdio>
int main(int, char**) {
bool intbool = 1;
printf("Intbool is %d\n", (int)intbool);
return 0;
}

@ -0,0 +1,3 @@
project('clangtidytest', 'cpp', default_options: 'cpp_std=c++14')
executable('cttest', 'cttest.cpp')
Loading…
Cancel
Save