34 from .cpplint
import Match, IsBlankLine, main
35 from functools
import partial
42 cpplint._line_length = 120
46 """ Decorator to easily allow wrapping/overriding of the Check* functions in cpplint. Should
47 decorate a function which matches the signature of the function it replaces expect with
48 the addition of a fn parameter, which is a pass-through of the replaced function, in case
49 the replacement would like call through to the original functionality. """
50 def wrap(override_fn):
51 original_fn = getattr(original_module, override_fn.__name__)
52 setattr(original_module, override_fn.__name__, partial(override_fn, original_fn))
59 def makeErrorFn(original_fn, suppress_categories, suppress_message_matches):
60 """ Create a return a wrapped version of the error-report function which suppresses specific
62 def newError(filename, linenum, category, confidence, message):
63 if category
in suppress_categories:
65 if True in [bool(
Match(r, message))
for r
in suppress_message_matches]:
67 original_fn(filename, linenum, category, confidence, message)
73 """ Replacement for the function which determines the header guard variable, to pick one which
74 matches ROS C++ Style. """
78 head, tail = os.path.split(head)
79 var_parts.insert(0, tail)
80 if head.endswith(
'include')
or os.path.exists(os.path.join(head,
"package.xml"))
or tail ==
"":
82 return re.sub(
r'[-./\s]',
'_',
"_".join(var_parts)).upper()
87 """ Complete replacement for cpplint.CheckBraces, since the brace rules for ROS C++ Style
88 are completely different from the Google style guide ones. """
89 line = clean_lines.elided[linenum]
90 if Match(
r'^(.*){(.*)}.*$', line):
97 m =
Match(
r'^(.*){(.*)$', line)
100 if "=" in line
and Match(
r'\)( *){$', line):
104 error(filename, linenum,
'whitespace/braces', 4,
105 'when starting a new scope, { should be on a line by itself')
106 m =
Match(
r'^(.*)}(.*)$', line)
108 if m.group(2) !=
";":
109 error(filename, linenum,
'whitespace/braces', 4,
110 '} should be on a line by itself')
116 """ Run the function to get include state, but suppress all the errors, since
117 ROS C++ Style is silent on include order, and contains no prohibition on use of streams. """
118 fn(filename, clean_lines, linenum, include_state,
119 makeErrorFn(error, [
'build/include_order',
'build/include_alpha',
'readability/streams'], []))
123 def CheckSpacing(fn, filename, clean_lines, linenum, nesting_state, error):
124 """ Do most of the original Spacing checks, but suppress the ones related to braces, since
125 the ROS C++ Style rules are different. """
126 fn(filename, clean_lines, linenum, nesting_state,
127 makeErrorFn(error, [
'readability/braces',
'whitespace/braces'], []))
132 include_state, function_state, nesting_state, error,
133 extra_check_functions=[]):
134 """ Squelch the error about access control indents. """
135 fn(filename, file_extension, clean_lines, line,
136 include_state, function_state, nesting_state,
137 makeErrorFn(error, [], [
r'(.*)should be indented \+1 space inside(.*)']),
138 extra_check_functions=[])
143 """ Look for empty loop/conditional body with only a single semicolon,
144 but allow ros-style do while loops. """
153 line = clean_lines.elided[linenum]
154 matched =
Match(
r'\s*(for|while|if)\s*\(', line)
158 clean_lines, linenum, line.find(
'('))
164 if end_pos >= 0
and Match(
r';', end_line[end_pos:]):
165 if matched.group(1) ==
'if':
166 error(filename, end_linenum,
167 'whitespace/empty_conditional_body', 5,
168 'Empty conditional bodies should use {}')
169 elif matched.group(1) ==
'while' and linenum != 0 \
170 and "}" in clean_lines.elided[linenum-1]:
177 error(filename, end_linenum,
'whitespace/empty_loop_body', 5,
178 'Empty loop bodies should use {} or continue')
181 if __name__ ==
'__main__':
182 sys.argv.insert(1,
"--filter=-runtime/references")