From e20ec3f9f94afa4eb7a1906da57ebff0b0c6f48d Mon Sep 17 00:00:00 2001 From: Kevin Peters Date: Sat, 20 Oct 2018 11:14:25 -0500 Subject: [PATCH] Add clang-tidy to buildsystem checks Fixes #1060 --- buildsystem/codecompliance/__main__.py | 14 ++++++++++++- buildsystem/codecompliance/clangtidy.py | 27 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 buildsystem/codecompliance/clangtidy.py diff --git a/buildsystem/codecompliance/__main__.py b/buildsystem/codecompliance/__main__.py index cea666509f..4191c98b06 100644 --- a/buildsystem/codecompliance/__main__.py +++ b/buildsystem/codecompliance/__main__.py @@ -45,6 +45,8 @@ def parse_args(): cli.add_argument("--test-git-change-years", action="store_true", help=("when doing legal checks, test whether the " "copyright year matches the git history.")) + cli.add_argument("--clangtidy", action="store_true", + help="check the cpp code with clang-tidy") cli.add_argument("--fix", action="store_true", help=("try to automatically fix the found issues")) @@ -79,10 +81,11 @@ def process_args(args, error): args.pystyle = True args.pylint = True args.test_git_change_years = True + args.clangtidy = True if not any((args.headerguards, args.legal, args.authors, args.pystyle, args.cppstyle, args.test_git_change_years, args.pylint, - args.filemodes, args.textfiles)): + args.filemodes, args.textfiles, args.clangtidy)): error("no checks were specified") has_git = bool(shutil.which('git')) @@ -115,6 +118,11 @@ def process_args(args, error): if not importlib.util.find_spec('pylint'): error("pylint python module required for linting") + if args.clangtidy: + has_clangtidy = bool(shutil.which('clang-tidy')) + if not has_clangtidy: + error("--clang-tidy requires clang-tidy") + def get_changed_files(gitref): """ @@ -248,6 +256,10 @@ def find_all_issues(args, check_files=None): yield from find_issues(check_files, ('openage', 'buildsystem', 'libopenage')) + if args.clangtidy: + from .clangtidy import find_issues + yield from find_issues(check_files, ('libopenage',)) + if __name__ == '__main__': if main(parse_args()): diff --git a/buildsystem/codecompliance/clangtidy.py b/buildsystem/codecompliance/clangtidy.py new file mode 100644 index 0000000000..987d4d2dd3 --- /dev/null +++ b/buildsystem/codecompliance/clangtidy.py @@ -0,0 +1,27 @@ +# Copyright 2015-2018 the openage authors. See copying.md for legal info. + +import subprocess + +from .cppstyle import filter_file_list +from .util import findfiles + + +def find_issues(check_files, dirnames): + """ Invokes the external clang-tidy tool. """ + + invocation = ['clang-tidy', '-checks=-*,readability-*'] + + if check_files is not None: + filenames = filter_file_list(check_files, dirnames) + else: + filenames = filter_file_list(findfiles(dirnames), dirnames) + + invocation.extend(filenames) + + try: + retcode = subprocess.check_call(invocation) + except subprocess.CalledProcessError as exc: + retcode = exc.returncode + + if retcode: + yield ("clang-tidy issue", f"clang-tidy exited with return code {retcode}", None)