diff --git a/docs/markdown/snippets/clangtidy.md b/docs/markdown/snippets/clangtidy.md new file mode 100644 index 000000000..816d45fc4 --- /dev/null +++ b/docs/markdown/snippets/clangtidy.md @@ -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. diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 1f3fb7777..fe1eee6d0 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -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) diff --git a/mesonbuild/scripts/clangtidy.py b/mesonbuild/scripts/clangtidy.py new file mode 100644 index 000000000..11c737ff1 --- /dev/null +++ b/mesonbuild/scripts/clangtidy.py @@ -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) diff --git a/run_unittests.py b/run_unittests.py index a2b083fb0..2df8bcb6e 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -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') diff --git a/test cases/unit/70 clang-tidy/.clang-tidy b/test cases/unit/70 clang-tidy/.clang-tidy new file mode 100644 index 000000000..393529479 --- /dev/null +++ b/test cases/unit/70 clang-tidy/.clang-tidy @@ -0,0 +1 @@ +Checks: '-*,modernize-use-bool-literals' diff --git a/test cases/unit/70 clang-tidy/cttest.cpp b/test cases/unit/70 clang-tidy/cttest.cpp new file mode 100644 index 000000000..07b35a666 --- /dev/null +++ b/test cases/unit/70 clang-tidy/cttest.cpp @@ -0,0 +1,7 @@ +#include + +int main(int, char**) { + bool intbool = 1; + printf("Intbool is %d\n", (int)intbool); + return 0; +} diff --git a/test cases/unit/70 clang-tidy/meson.build b/test cases/unit/70 clang-tidy/meson.build new file mode 100644 index 000000000..737474b02 --- /dev/null +++ b/test cases/unit/70 clang-tidy/meson.build @@ -0,0 +1,3 @@ +project('clangtidytest', 'cpp', default_options: 'cpp_std=c++14') + +executable('cttest', 'cttest.cpp')