Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CMake linter line length configurable #235

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion ament_cmake_lint_cmake/cmake/ament_lint_cmake.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#
# :param TESTNAME: the name of the test, default: "lint_cmake"
# :type TESTNAME: string
# :param MAX_LINE_LENGTH: override the maximum line length,
# the default is defined in ament_lint_cmake
# :type MAX_LINE_LENGTH: integer
# :param ARGN: the files or directories to check
# :type ARGN: list of strings
#
# @public
#
function(ament_lint_cmake)
cmake_parse_arguments(ARG "" "TESTNAME" "" ${ARGN})
cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN})
if(NOT ARG_TESTNAME)
set(ARG_TESTNAME "lint_cmake")
endif()
Expand All @@ -35,6 +38,9 @@ function(ament_lint_cmake)

set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml")
set(cmd "${ament_lint_cmake_BIN}" "--xunit-file" "${result_file}")
if(DEFINED ARG_MAX_LINE_LENGTH)
list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}")
endif()
list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS})

file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_lint_cmake")
Expand Down
14 changes: 12 additions & 2 deletions ament_lint_cmake/ament_lint_cmake/cmakelint.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self):
self.config = 0
self.errors = 0
self.spaces = 2
self.linelength = 80
self.allowed_categories = _ERROR_CATEGORIES.split()

def SetFilters(self, filters):
Expand All @@ -110,6 +111,9 @@ def SetFilters(self, filters):
def SetSpaces(self, spaces):
self.spaces = int(spaces.strip())

def SetLineLength(self, linelength):
self.linelength = int(linelength)

class _CMakePackageState(object):
def __init__(self):
self.sets = []
Expand Down Expand Up @@ -198,7 +202,7 @@ def CheckLineLength(filename, linenumber, clean_lines, errors):
Check for lines longer than the recommended length
"""
line = clean_lines.raw_lines[linenumber]
if len(line) > 80:
if len(line) > _lint_state.linelength:
if _RE_STRING.match(line):
lineno = linenumber
while lineno > 0:
Expand All @@ -215,7 +219,8 @@ def CheckLineLength(filename, linenumber, clean_lines, errors):
filename,
linenumber,
'linelength',
'Lines should be <= 80 characters long')
'Lines should be <= %d characters long' %
(_lint_state.linelength))

def ContainsCommand(line):
return _RE_COMMAND.match(line)
Expand Down Expand Up @@ -448,6 +453,7 @@ def PrintCategories():
def ParseOptionFile(contents, ignore_space):
filters = None
spaces = None
linelength = None
for line in contents:
line = line.strip()
if not line or line.startswith('#'):
Expand All @@ -456,9 +462,13 @@ def ParseOptionFile(contents, ignore_space):
filters = line.replace('filter=', '')
if line.startswith('spaces='):
spaces = line.replace('spaces=', '')
if line.startswith('linelength='):
linelength = line.replace('linelength=', '')
_lint_state.SetFilters(filters)
if spaces and not ignore_space:
_lint_state.SetSpaces(spaces)
if linelength is not None:
_lint_state.SetLineLength(linelength)

def ParseArgs(argv):
try:
Expand Down
5 changes: 5 additions & 0 deletions ament_lint_cmake/ament_lint_cmake/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def main(argv=sys.argv[1:]):
default='',
help='Filters for lint_cmake, for a list of filters see: '
'https://github.com/richq/cmake-lint/blob/master/README.md#usage')
parser.add_argument(
'--linelength', metavar='N', type=int,
help='The maximum line length')
# not using a file handle directly
# in order to prevent leaving an empty file when something fails early
parser.add_argument(
Expand All @@ -70,6 +73,8 @@ def main(argv=sys.argv[1:]):
# invoke cmake lint
cmakelint._lint_state.config = cmakelint._DEFAULT_CMAKELINTRC
cmakelint._lint_state.SetFilters(args.filters)
if args.linelength is not None:
cmakelint._lint_state.SetLineLength(str(args.linelength))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of strange that we're casting from int to str and then back to int, but it should be fine.

for filename in files:
# hook into error reporting
errors = []
Expand Down