diff --git a/ament_clang_tidy/ament_clang_tidy/main.py b/ament_clang_tidy/ament_clang_tidy/main.py index f2b7eeab6..f0e6c6419 100755 --- a/ament_clang_tidy/ament_clang_tidy/main.py +++ b/ament_clang_tidy/ament_clang_tidy/main.py @@ -17,6 +17,7 @@ import argparse from collections import defaultdict import copy +import glob import json from multiprocessing.pool import ThreadPool import os @@ -84,11 +85,18 @@ def main(argv=sys.argv[1:]): parser.add_argument( '--packages-select', nargs='*', metavar='PKG_NAME', help='Only process a subset of packages') + parser.add_argument( + '--exclude', default=[], nargs='*', help='Filenames to exclude') parser.add_argument( '--xunit-file', help='Generate a xunit compliant XML file') args = parser.parse_args(argv) + excludes = [] + for exclude_pattern in args.exclude: + excludes.extend(glob.glob(exclude_pattern)) + excludes = {os.path.realpath(x) for x in excludes} + if args.config_file is not None and not os.path.exists(args.config_file): print("Could not find config file '%s'" % args.config_file, file=sys.stderr) @@ -162,6 +170,14 @@ def is_gtest_source(file_name): def is_unittest_source(package, file_path): return ('%s/test/' % package) in file_path + def is_protobuf_source(file_name): + if(file_name.endswith('.pb.cc') or file_name.endswith('.pb.h')): + return True + return False + + def is_excluded(file_path): + return file_path in excludes + def start_subprocess(full_cmd): output = '' try: @@ -188,6 +204,14 @@ def start_subprocess(full_cmd): if is_unittest_source(package_name, item['file']): continue + # exclude auto-generated protobuf sources from being checked by clang-tidy + if is_protobuf_source(os.path.basename(item['file'])): + continue + + # exclude files that are explicitly excluded + if is_excluded(item['file']): + continue + files.append(item['file']) full_cmd = cmd + [item['file']] async_outputs.append(pool.apply_async(start_subprocess, (full_cmd,))) diff --git a/ament_clang_tidy/doc/index.rst b/ament_clang_tidy/doc/index.rst index d8255f9a1..c2bdd6283 100644 --- a/ament_clang_tidy/doc/index.rst +++ b/ament_clang_tidy/doc/index.rst @@ -19,10 +19,11 @@ have already been installed. ``compile_commands.json`` files should have already ament_clang_tidy [-h] [--config path] [--explain-config] [--export-fixes EXPORT_FIXES] [--fix-errors] [--header-filter HEADER_FILTER] [--quiet] - [--system-headers] [--jobs N] - [--packages-select [PKG_NAME [PKG_NAME ...]]] + [--system-headers] [--jobs JOBS] + [--packages-select [PKG_NAME ...]] + [--exclude [EXCLUDE ...]] [--xunit-file XUNIT_FILE] - [paths [paths ...]] + [paths ...] If ```` is a directory, it will be recursively searched for "compile_commands.json" files (this is usually the ``build`` directory of a @@ -56,6 +57,8 @@ parallel. The ``--packages-select`` option will filter the "compile_commands.json" files to just those generated by specific ROS packages. +The ``--exclude`` option will exclude the specified patterns. + The ``--xunit-file`` option will generate a xunit compliant XML file when supplied with a file name. diff --git a/ament_cmake_clang_tidy/cmake/ament_clang_tidy.cmake b/ament_cmake_clang_tidy/cmake/ament_clang_tidy.cmake index 95df6abe6..0927723a9 100644 --- a/ament_cmake_clang_tidy/cmake/ament_clang_tidy.cmake +++ b/ament_cmake_clang_tidy/cmake/ament_clang_tidy.cmake @@ -25,6 +25,8 @@ # # :param TESTNAME: the name of the test, default: "clang_tidy" # :type TESTNAME: string +# :param EXCLUDE: an optional list of exclude directories or files for flake8 +# :type EXCLUDE: list # :param CONFIG_FILE: the path of the configuration file for clang-tidy to consider # :type CONFIG_FILE: string # :param ARGN: the files or directories to check @@ -35,7 +37,7 @@ # @public # function(ament_clang_tidy) - cmake_parse_arguments(ARG "" "TESTNAME;CONFIG_FILE;TIMEOUT;HEADER_FILTER;JOBS" "" ${ARGN}) + cmake_parse_arguments(ARG "" "TESTNAME;EXCLUDE;CONFIG_FILE;TIMEOUT;HEADER_FILTER;JOBS" "" ${ARGN}) if(NOT ARG_TESTNAME) set(ARG_TESTNAME "clang_tidy") endif() @@ -49,6 +51,10 @@ function(ament_clang_tidy) set(cmd "${ament_clang_tidy_BIN}" "--xunit-file" "${result_file}") list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS}) + if(ARG_EXCLUDE) + list(APPEND cmd "--exclude" "${ARG_EXCLUDE}") + endif() + if(ARG_CONFIG_FILE) list(APPEND cmd "--config" "${ARG_CONFIG_FILE}") elseif(DEFINED ament_cmake_clang_tidy_CONFIG_FILE) diff --git a/ament_cmake_clang_tidy/cmake/ament_cmake_clang_tidy_lint_hook.cmake b/ament_cmake_clang_tidy/cmake/ament_cmake_clang_tidy_lint_hook.cmake index 06b08bace..c9d7da719 100644 --- a/ament_cmake_clang_tidy/cmake/ament_cmake_clang_tidy_lint_hook.cmake +++ b/ament_cmake_clang_tidy/cmake/ament_cmake_clang_tidy_lint_hook.cmake @@ -24,5 +24,5 @@ file(GLOB_RECURSE _source_files FOLLOW_SYMLINKS ) if(_source_files) message(STATUS "Added test 'clang_tidy' to check C / C++ code style") - ament_clang_tidy("${CMAKE_CURRENT_BINARY_DIR}") + ament_clang_tidy(EXCLUDE ${AMENT_LINT_AUTO_FILE_EXCLUDE} "${CMAKE_CURRENT_BINARY_DIR}") endif()