Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ament_clang_tidy/ament_clang_tidy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import argparse
from collections import defaultdict
import copy
import glob
import json
from multiprocessing.pool import ThreadPool
import os
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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,)))
Expand Down
9 changes: 6 additions & 3 deletions ament_clang_tidy/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<paths>`` is a directory, it will be recursively searched for
"compile_commands.json" files (this is usually the ``build`` directory of a
Expand Down Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion ament_cmake_clang_tidy/cmake/ament_clang_tidy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()