2 # -*- coding: utf-8 -*-
4 # Copyright (C) 2009 Google Inc. All rights reserved.
5 # Copyright (C) 2009 Torch Mobile Inc.
6 # Copyright (C) 2009 Apple Inc. All rights reserved.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following disclaimer
16 # in the documentation and/or other materials provided with the
18 # * Neither the name of Google Inc. nor the names of its
19 # contributors may be used to endorse or promote products derived from
20 # this software without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # This is the modified version of Google's cpplint. The original code is
35 # http://google-styleguide.googlecode.com/svn/trunk/cpplint/cpplint.py
37 """Does WebKit-lint on c++ files.
39 The goal of this script is to identify places in the code that *may*
40 be in non-compliance with WebKit style. It does not attempt to fix
41 up these problems -- the point is to educate. It does also not
42 attempt to find all problems, or to ensure that everything it does
43 find is legitimately a problem.
45 In particular, we can get very confused by /* and // inside strings!
46 We do a small hack, which is to ignore //'s with "'s after them on the
47 same line, but it is far from perfect (in either direction).
63 Syntax: %(program_name)s [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
66 The style guidelines this tries to follow are those in
67 http://webkit.org/coding/coding-style.html
69 Every problem is given a confidence score from 1-5, with 5 meaning we are
70 certain of the problem, and 1 meaning it could be a legitimate construct.
71 This will miss some errors, and is not a substitute for a code review.
73 To prevent specific lines from being linted, add a '// NOLINT' comment to the
76 The files passed in will be linted; at least one file must be provided.
77 Linted extensions are .cpp, .c and .h. Other file types will be ignored.
82 By default, the output is formatted to ease emacs parsing. Visual Studio
83 compatible output (vs7) may also be used. Other formats are unsupported.
86 Specify a number 0-5 to restrict errors to certain verbosity levels.
89 Specify a comma-separated list of category-filters to apply: only
90 error messages whose category names pass the filters will be printed.
91 (Category names are printed with the message and look like
92 "[whitespace/indent]".) Filters are evaluated left to right.
93 "-FOO" and "FOO" means "do not print categories that start with FOO".
94 "+FOO" means "do print categories that start with FOO".
96 Examples: --filter=-whitespace,+whitespace/braces
97 --filter=whitespace,runtime/printf,+runtime/printf_format
98 --filter=-,+build/include_what_you_use
100 To see a list of all the categories used in %(program_name)s, pass no arg:
102 """ % {'program_name': sys.argv[0]}
104 # We categorize each error message we print. Here are the categories.
105 # We want an explicit list so we can list them all in cpp_style --filter=.
106 # If you add a new error message with a new category, add it to the list
107 # here! cpp_style_unittest.py should tell you if you forget to do this.
108 # \ used for clearer layout -- pylint: disable-msg=C6013
109 _ERROR_CATEGORIES = '''\
117 build/include_what_you_use
126 readability/comparison_to_zero
127 readability/constructors
128 readability/control_flow
131 readability/multiline_comment
132 readability/multiline_string
142 runtime/invalid_increment
143 runtime/max_min_macros
146 runtime/printf_format
151 runtime/threadsafe_fn
153 whitespace/blank_line
157 whitespace/declaration
158 whitespace/end_of_line
159 whitespace/ending_newline
162 whitespace/line_length
171 # The default state of the category filter. This is overrided by the --filter=
172 # flag. By default all errors are on, so only add here categories that should be
173 # off by default (i.e., categories that must be enabled by the --filter= flags).
174 # All entries here should start with a '-' or '+', as in the --filter= flag.
175 _DEFAULT_FILTERS = []
177 # Headers that we consider STL headers.
178 _STL_HEADERS = frozenset([
179 'algobase.h', 'algorithm', 'alloc.h', 'bitset', 'deque', 'exception',
180 'function.h', 'functional', 'hash_map', 'hash_map.h', 'hash_set',
181 'hash_set.h', 'iterator', 'list', 'list.h', 'map', 'memory', 'pair.h',
182 'pthread_alloc', 'queue', 'set', 'set.h', 'sstream', 'stack',
183 'stl_alloc.h', 'stl_relops.h', 'type_traits.h',
184 'utility', 'vector', 'vector.h',
188 # Non-STL C++ system headers.
189 _CPP_HEADERS = frozenset([
190 'algo.h', 'builtinbuf.h', 'bvector.h', 'cassert', 'cctype',
191 'cerrno', 'cfloat', 'ciso646', 'climits', 'clocale', 'cmath',
192 'complex', 'complex.h', 'csetjmp', 'csignal', 'cstdarg', 'cstddef',
193 'cstdio', 'cstdlib', 'cstring', 'ctime', 'cwchar', 'cwctype',
194 'defalloc.h', 'deque.h', 'editbuf.h', 'exception', 'fstream',
195 'fstream.h', 'hashtable.h', 'heap.h', 'indstream.h', 'iomanip',
196 'iomanip.h', 'ios', 'iosfwd', 'iostream', 'iostream.h', 'istream.h',
197 'iterator.h', 'limits', 'map.h', 'multimap.h', 'multiset.h',
198 'numeric', 'ostream.h', 'parsestream.h', 'pfstream.h', 'PlotFile.h',
199 'procbuf.h', 'pthread_alloc.h', 'rope', 'rope.h', 'ropeimpl.h',
200 'SFile.h', 'slist', 'slist.h', 'stack.h', 'stdexcept',
201 'stdiostream.h', 'streambuf.h', 'stream.h', 'strfile.h', 'string',
202 'strstream', 'strstream.h', 'tempbuf.h', 'tree.h', 'typeinfo', 'valarray',
206 # Assertion macros. These are defined in base/logging.h and
207 # testing/base/gunit.h. Note that the _M versions need to come first
208 # for substring matching to work.
211 'EXPECT_TRUE_M', 'EXPECT_TRUE',
212 'ASSERT_TRUE_M', 'ASSERT_TRUE',
213 'EXPECT_FALSE_M', 'EXPECT_FALSE',
214 'ASSERT_FALSE_M', 'ASSERT_FALSE',
217 # Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
218 _CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
220 for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
221 ('>=', 'GE'), ('>', 'GT'),
222 ('<=', 'LE'), ('<', 'LT')]:
223 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
224 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
225 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
226 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
227 _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
228 _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
230 for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
231 ('>=', 'LT'), ('>', 'LE'),
232 ('<=', 'GT'), ('<', 'GE')]:
233 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
234 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
235 _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
236 _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
239 # These constants define types of headers for use with
240 # _IncludeState.check_next_include_order().
246 _regexp_compile_cache = {}
249 def match(pattern, s):
250 """Matches the string with the pattern, caching the compiled regexp."""
251 # The regexp compilation caching is inlined in both match and search for
252 # performance reasons; factoring it out into a separate function turns out
253 # to be noticeably expensive.
254 if not pattern in _regexp_compile_cache:
255 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
256 return _regexp_compile_cache[pattern].match(s)
259 def search(pattern, s):
260 """Searches the string for the pattern, caching the compiled regexp."""
261 if not pattern in _regexp_compile_cache:
262 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
263 return _regexp_compile_cache[pattern].search(s)
266 class _IncludeState(dict):
267 """Tracks line numbers for includes, and the order in which includes appear.
269 As a dict, an _IncludeState object serves as a mapping between include
270 filename and line number on which that file was included.
272 Call check_next_include_order() once for each header in the file, passing
273 in the type constants defined above. Calls in an illegal order will
274 raise an _IncludeError with an appropriate error message.
277 # self._section will move monotonically through this set. If it ever
278 # needs to move backwards, check_next_include_order will raise an error.
285 _CONFIG_HEADER: 'WebCore config.h',
286 _PRIMARY_HEADER: 'header this file implements',
287 _OTHER_HEADER: 'other header',
290 _INITIAL_SECTION: "... nothing.",
291 _CONFIG_SECTION: "WebCore config.h.",
292 _PRIMARY_SECTION: 'a header this file implements.',
293 _OTHER_SECTION: 'other header.',
298 self._section = self._INITIAL_SECTION
299 self._visited_primary_section = False
300 self.header_types = dict();
302 def visited_primary_section(self):
303 return self._visited_primary_section
305 def check_next_include_order(self, header_type, file_is_header):
306 """Returns a non-empty error message if the next header is out of order.
308 This function also updates the internal state to be ready to check
312 header_type: One of the _XXX_HEADER constants defined above.
313 file_is_header: Whether the file that owns this _IncludeState is itself a header
316 The empty string if the header is in the right order, or an
317 error message describing what's wrong.
320 if header_type == _CONFIG_HEADER and file_is_header:
321 return 'Header file should not contain WebCore config.h.'
322 if header_type == _PRIMARY_HEADER and file_is_header:
323 return 'Header file should not contain itself.'
326 if self._section != self._OTHER_SECTION:
327 before_error_message = ('Found %s before %s' %
328 (self._TYPE_NAMES[header_type],
329 self._SECTION_NAMES[self._section + 1]))
330 after_error_message = ('Found %s after %s' %
331 (self._TYPE_NAMES[header_type],
332 self._SECTION_NAMES[self._section]))
334 if header_type == _CONFIG_HEADER:
335 if self._section >= self._CONFIG_SECTION:
336 error_message = after_error_message
337 self._section = self._CONFIG_SECTION
338 elif header_type == _PRIMARY_HEADER:
339 if self._section >= self._PRIMARY_SECTION:
340 error_message = after_error_message
341 elif self._section < self._CONFIG_SECTION:
342 error_message = before_error_message
343 self._section = self._PRIMARY_SECTION
344 self._visited_primary_section = True
346 assert header_type == _OTHER_HEADER
347 if not file_is_header and self._section < self._PRIMARY_SECTION:
348 error_message = before_error_message
349 self._section = self._OTHER_SECTION
354 class _CppStyleState(object):
355 """Maintains module-wide state.."""
358 self.verbose_level = 1 # global setting.
359 self.error_count = 0 # global count of reported errors
360 # filters to apply when emitting error messages
361 self.filters = _DEFAULT_FILTERS[:]
364 # "emacs" - format that emacs can parse (default)
365 # "vs7" - format that Microsoft Visual Studio 7 can parse
366 self.output_format = 'emacs'
368 def set_output_format(self, output_format):
369 """Sets the output format for errors."""
370 self.output_format = output_format
372 def set_verbose_level(self, level):
373 """Sets the module's verbosity, and returns the previous setting."""
374 last_verbose_level = self.verbose_level
375 self.verbose_level = level
376 return last_verbose_level
378 def set_filters(self, filters):
379 """Sets the error-message filters.
381 These filters are applied when deciding whether to emit a given
385 filters: A string of comma-separated filters (eg "+whitespace/indent").
386 Each filter should start with + or -; else we die.
389 ValueError: The comma-separated filters did not all start with '+' or '-'.
390 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
392 # Default filters always have less priority than the flag ones.
393 self.filters = _DEFAULT_FILTERS[:]
394 for filter in filters.split(','):
395 clean_filter = filter.strip()
397 self.filters.append(clean_filter)
398 for filter in self.filters:
399 if not (filter.startswith('+') or filter.startswith('-')):
400 raise ValueError('Every filter in --filter must start with '
401 '+ or - (%s does not)' % filter)
403 def reset_error_count(self):
404 """Sets the module's error statistic back to zero."""
407 def increment_error_count(self):
408 """Bumps the module's error statistic."""
409 self.error_count += 1
412 _cpp_style_state = _CppStyleState()
415 def _output_format():
416 """Gets the module's output format."""
417 return _cpp_style_state.output_format
420 def _set_output_format(output_format):
421 """Sets the module's output format."""
422 _cpp_style_state.set_output_format(output_format)
425 def _verbose_level():
426 """Returns the module's verbosity setting."""
427 return _cpp_style_state.verbose_level
430 def _set_verbose_level(level):
431 """Sets the module's verbosity, and returns the previous setting."""
432 return _cpp_style_state.set_verbose_level(level)
436 """Returns the module's list of output filters, as a list."""
437 return _cpp_style_state.filters
440 def _set_filters(filters):
441 """Sets the module's error-message filters.
443 These filters are applied when deciding whether to emit a given
447 filters: A string of comma-separated filters (eg "whitespace/indent").
448 Each filter should start with + or -; else we die.
450 _cpp_style_state.set_filters(filters)
454 """Returns the global count of reported errors."""
455 return _cpp_style_state.error_count
458 class _FunctionState(object):
459 """Tracks current function name and the number of lines in its body."""
461 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
462 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
465 self.in_a_function = False
466 self.lines_in_function = 0
467 self.current_function = ''
469 def begin(self, function_name):
470 """Start analyzing function body.
473 function_name: The name of the function being tracked.
475 self.in_a_function = True
476 self.lines_in_function = 0
477 self.current_function = function_name
480 """Count line in current function body."""
481 if self.in_a_function:
482 self.lines_in_function += 1
484 def check(self, error, filename, line_number):
485 """Report if too many lines in function body.
488 error: The function to call with any errors found.
489 filename: The name of the current file.
490 line_number: The number of the line to check.
492 if match(r'T(EST|est)', self.current_function):
493 base_trigger = self._TEST_TRIGGER
495 base_trigger = self._NORMAL_TRIGGER
496 trigger = base_trigger * 2 ** _verbose_level()
498 if self.lines_in_function > trigger:
499 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
500 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
503 error(filename, line_number, 'readability/fn_size', error_level,
504 'Small and focused functions are preferred:'
505 ' %s has %d non-comment lines'
506 ' (error triggered by exceeding %d lines).' % (
507 self.current_function, self.lines_in_function, trigger))
510 """Stop analizing function body."""
511 self.in_a_function = False
514 class _IncludeError(Exception):
515 """Indicates a problem with the include order in a file."""
520 """Provides utility functions for filenames.
522 FileInfo provides easy access to the components of a file's path
523 relative to the project root.
526 def __init__(self, filename):
527 self._filename = filename
530 """Make Windows paths like Unix."""
531 return os.path.abspath(self._filename).replace('\\', '/')
533 def repository_name(self):
534 """Full name after removing the local path to the repository.
536 If we have a real absolute path name here we can try to do something smart:
537 detecting the root of the checkout and truncating /path/to/checkout from
538 the name so that we get header guards that don't include things like
539 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
540 people on different computers who have checked the source out to different
541 locations won't see bogus errors.
543 fullname = self.full_name()
545 if os.path.exists(fullname):
546 project_dir = os.path.dirname(fullname)
548 if os.path.exists(os.path.join(project_dir, ".svn")):
549 # If there's a .svn file in the current directory, we
550 # recursively look up the directory tree for the top
551 # of the SVN checkout
552 root_dir = project_dir
553 one_up_dir = os.path.dirname(root_dir)
554 while os.path.exists(os.path.join(one_up_dir, ".svn")):
555 root_dir = os.path.dirname(root_dir)
556 one_up_dir = os.path.dirname(one_up_dir)
558 prefix = os.path.commonprefix([root_dir, project_dir])
559 return fullname[len(prefix) + 1:]
561 # Not SVN? Try to find a git top level directory by
562 # searching up from the current path.
563 root_dir = os.path.dirname(fullname)
564 while (root_dir != os.path.dirname(root_dir)
565 and not os.path.exists(os.path.join(root_dir, ".git"))):
566 root_dir = os.path.dirname(root_dir)
567 if os.path.exists(os.path.join(root_dir, ".git")):
568 prefix = os.path.commonprefix([root_dir, project_dir])
569 return fullname[len(prefix) + 1:]
571 # Don't know what to do; header guard warnings may be wrong...
575 """Splits the file into the directory, basename, and extension.
577 For 'chrome/browser/browser.cpp', Split() would
578 return ('chrome/browser', 'browser', '.cpp')
581 A tuple of (directory, basename, extension).
584 googlename = self.repository_name()
585 project, rest = os.path.split(googlename)
586 return (project,) + os.path.splitext(rest)
589 """File base name - text after the final slash, before the final period."""
590 return self.split()[1]
593 """File extension - text following the final period."""
594 return self.split()[2]
596 def no_extension(self):
597 """File has no source file extension."""
598 return '/'.join(self.split()[0:2])
601 """File has a source file extension."""
602 return self.extension()[1:] in ('c', 'cc', 'cpp', 'cxx')
605 def _should_print_error(category, confidence):
606 """Returns true iff confidence >= verbose, and category passes filter."""
607 # There are two ways we might decide not to print an error message:
608 # the verbosity level isn't high enough, or the filters filter it out.
609 if confidence < _cpp_style_state.verbose_level:
613 for one_filter in _filters():
614 if one_filter.startswith('-'):
615 if category.startswith(one_filter[1:]):
617 elif one_filter.startswith('+'):
618 if category.startswith(one_filter[1:]):
621 assert False # should have been checked for in set_filter.
628 def error(filename, line_number, category, confidence, message):
629 """Logs the fact we've found a lint error.
631 We log where the error was found, and also our confidence in the error,
632 that is, how certain we are this is a legitimate style regression, and
633 not a misidentification or a use that's sometimes justified.
636 filename: The name of the file containing the error.
637 line_number: The number of the line containing the error.
638 category: A string used to describe the "category" this bug
639 falls under: "whitespace", say, or "runtime". Categories
640 may have a hierarchy separated by slashes: "whitespace/indent".
641 confidence: A number from 1-5 representing a confidence score for
642 the error, with 5 meaning that we are certain of the problem,
643 and 1 meaning that it could be a legitimate construct.
644 message: The error message.
646 # There are two ways we might decide not to print an error message:
647 # the verbosity level isn't high enough, or the filters filter it out.
648 if _should_print_error(category, confidence):
649 _cpp_style_state.increment_error_count()
650 if _cpp_style_state.output_format == 'vs7':
651 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
652 filename, line_number, message, category, confidence))
654 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
655 filename, line_number, message, category, confidence))
658 # Matches standard C++ escape esequences per 2.13.2.3 of the C++ standard.
659 _RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
660 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
661 # Matches strings. Escape codes should already be removed by ESCAPES.
662 _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES = re.compile(r'"[^"]*"')
663 # Matches characters. Escape codes should already be removed by ESCAPES.
664 _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES = re.compile(r"'.'")
665 # Matches multi-line C++ comments.
666 # This RE is a little bit more complicated than one might expect, because we
667 # have to take care of space removals tools so we can handle comments inside
669 # The current rule is: We only clear spaces from both sides when we're at the
670 # end of the line. Otherwise, we try to remove spaces from the right side,
671 # if this doesn't work we try on left side but only if there's a non-character
673 _RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
674 r"""(\s*/\*.*\*/\s*$|
677 /\*.*\*/)""", re.VERBOSE)
680 def is_cpp_string(line):
681 """Does line terminate so, that the next symbol is in string constant.
683 This function does not consider single-line nor multi-line comments.
686 line: is a partial line of code starting from the 0..n.
689 True, if next character appended to 'line' is inside a
693 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
694 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
697 def find_next_multi_line_comment_start(lines, line_index):
698 """Find the beginning marker for a multiline comment."""
699 while line_index < len(lines):
700 if lines[line_index].strip().startswith('/*'):
701 # Only return this marker if the comment goes beyond this line
702 if lines[line_index].strip().find('*/', 2) < 0:
708 def find_next_multi_line_comment_end(lines, line_index):
709 """We are inside a comment, find the end marker."""
710 while line_index < len(lines):
711 if lines[line_index].strip().endswith('*/'):
717 def remove_multi_line_comments_from_range(lines, begin, end):
718 """Clears a range of lines for multi-line comments."""
719 # Having // dummy comments makes the lines non-empty, so we will not get
720 # unnecessary blank line warnings later in the code.
721 for i in range(begin, end):
722 lines[i] = '// dummy'
725 def remove_multi_line_comments(filename, lines, error):
726 """Removes multiline (c-style) comments from lines."""
728 while line_index < len(lines):
729 line_index_begin = find_next_multi_line_comment_start(lines, line_index)
730 if line_index_begin >= len(lines):
732 line_index_end = find_next_multi_line_comment_end(lines, line_index_begin)
733 if line_index_end >= len(lines):
734 error(filename, line_index_begin + 1, 'readability/multiline_comment', 5,
735 'Could not find end of multi-line comment')
737 remove_multi_line_comments_from_range(lines, line_index_begin, line_index_end + 1)
738 line_index = line_index_end + 1
741 def cleanse_comments(line):
742 """Removes //-comments and single-line C-style /* */ comments.
745 line: A line of C++ source.
748 The line with single-line comments removed.
750 comment_position = line.find('//')
751 if comment_position != -1 and not is_cpp_string(line[:comment_position]):
752 line = line[:comment_position]
753 # get rid of /* ... */
754 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
757 class CleansedLines(object):
758 """Holds 3 copies of all lines with different preprocessing applied to them.
760 1) elided member contains lines without strings and comments,
761 2) lines member contains lines without comments, and
762 3) raw member contains all the lines without processing.
763 All these three members are of <type 'list'>, and of the same length.
766 def __init__(self, lines):
769 self.raw_lines = lines
770 self._num_lines = len(lines)
771 for line_number in range(len(lines)):
772 self.lines.append(cleanse_comments(lines[line_number]))
773 elided = self.collapse_strings(lines[line_number])
774 self.elided.append(cleanse_comments(elided))
777 """Returns the number of lines represented."""
778 return self._num_lines
781 def collapse_strings(elided):
782 """Collapses strings and chars on a line to simple "" or '' blocks.
784 We nix strings first so we're not fooled by text like '"http://"'
787 elided: The line being processed.
790 The line with collapsed strings.
792 if not _RE_PATTERN_INCLUDE.match(elided):
793 # Remove escaped characters first to make quote/single quote collapsing
794 # basic. Things that look like escaped characters shouldn't occur
795 # outside of strings and chars.
796 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
797 elided = _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES.sub("''", elided)
798 elided = _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES.sub('""', elided)
802 def close_expression(clean_lines, line_number, pos):
803 """If input points to ( or { or [, finds the position that closes it.
805 If lines[line_number][pos] points to a '(' or '{' or '[', finds the the
806 line_number/pos that correspond to the closing of the expression.
809 clean_lines: A CleansedLines instance containing the file.
810 line_number: The number of the line to check.
811 pos: A position on the line.
814 A tuple (line, line_number, pos) pointer *past* the closing brace, or
815 (line, len(lines), -1) if we never find a close. Note we ignore
816 strings and comments when matching; and the line we return is the
817 'cleansed' line at line_number.
820 line = clean_lines.elided[line_number]
821 start_character = line[pos]
822 if start_character not in '({[':
823 return (line, clean_lines.num_lines(), -1)
824 if start_character == '(':
826 if start_character == '[':
828 if start_character == '{':
831 num_open = line.count(start_character) - line.count(end_character)
832 while line_number < clean_lines.num_lines() and num_open > 0:
834 line = clean_lines.elided[line_number]
835 num_open += line.count(start_character) - line.count(end_character)
836 # OK, now find the end_character that actually got us back to even
839 endpos = line.rfind(')', 0, endpos)
840 num_open -= 1 # chopped off another )
841 return (line, line_number, endpos + 1)
844 def check_for_copyright(filename, lines, error):
845 """Logs an error if no Copyright message appears at the top of the file."""
847 # We'll say it should occur by line 10. Don't forget there's a
848 # dummy line at the front.
849 for line in xrange(1, min(len(lines), 11)):
850 if re.search(r'Copyright', lines[line], re.I):
852 else: # means no copyright line was found
853 error(filename, 0, 'legal/copyright', 5,
854 'No copyright message found. '
855 'You should have a line: "Copyright [year] <Copyright Owner>"')
858 def get_header_guard_cpp_variable(filename):
859 """Returns the CPP variable that should be used as a header guard.
862 filename: The name of a C++ header file.
865 The CPP variable that should be used as a header guard in the
870 fileinfo = FileInfo(filename)
871 return re.sub(r'[-./\s]', '_', fileinfo.repository_name()).upper() + '_'
874 def check_for_header_guard(filename, lines, error):
875 """Checks that the file contains a header guard.
877 Logs an error if no #ifndef header guard is present. For other
878 headers, checks that the full pathname is used.
881 filename: The name of the C++ header file.
882 lines: An array of strings, each representing a line of the file.
883 error: The function to call with any errors found.
886 cppvar = get_header_guard_cpp_variable(filename)
889 ifndef_line_number = 0
892 endif_line_number = 0
893 for line_number, line in enumerate(lines):
894 line_split = line.split()
895 if len(line_split) >= 2:
896 # find the first occurrence of #ifndef and #define, save arg
897 if not ifndef and line_split[0] == '#ifndef':
898 # set ifndef to the header guard presented on the #ifndef line.
899 ifndef = line_split[1]
900 ifndef_line_number = line_number
901 if not define and line_split[0] == '#define':
902 define = line_split[1]
903 # find the last occurrence of #endif, save entire line
904 if line.startswith('#endif'):
906 endif_line_number = line_number
908 if not ifndef or not define or ifndef != define:
909 error(filename, 0, 'build/header_guard', 5,
910 'No #ifndef header guard found, suggested CPP variable is: %s' %
914 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
915 # for backward compatibility.
918 if ifndef != cppvar + '_':
921 error(filename, ifndef_line_number, 'build/header_guard', error_level,
922 '#ifndef header guard has wrong style, please use: %s' % cppvar)
924 if endif != ('#endif // %s' % cppvar):
926 if endif != ('#endif // %s' % (cppvar + '_')):
929 error(filename, endif_line_number, 'build/header_guard', error_level,
930 '#endif line should be "#endif // %s"' % cppvar)
933 def check_for_unicode_replacement_characters(filename, lines, error):
934 """Logs an error for each line containing Unicode replacement characters.
936 These indicate that either the file contained invalid UTF-8 (likely)
937 or Unicode replacement characters (which it shouldn't). Note that
938 it's possible for this to throw off line numbering if the invalid
939 UTF-8 occurred adjacent to a newline.
942 filename: The name of the current file.
943 lines: An array of strings, each representing a line of the file.
944 error: The function to call with any errors found.
946 for line_number, line in enumerate(lines):
947 if u'\ufffd' in line:
948 error(filename, line_number, 'readability/utf8', 5,
949 'Line contains invalid UTF-8 (or Unicode replacement character).')
952 def check_for_new_line_at_eof(filename, lines, error):
953 """Logs an error if there is no newline char at the end of the file.
956 filename: The name of the current file.
957 lines: An array of strings, each representing a line of the file.
958 error: The function to call with any errors found.
961 # The array lines() was created by adding two newlines to the
962 # original file (go figure), then splitting on \n.
963 # To verify that the file ends in \n, we just have to make sure the
964 # last-but-two element of lines() exists and is empty.
965 if len(lines) < 3 or lines[-2]:
966 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
967 'Could not find a newline character at the end of the file.')
970 def check_for_multiline_comments_and_strings(filename, clean_lines, line_number, error):
971 """Logs an error if we see /* ... */ or "..." that extend past one line.
973 /* ... */ comments are legit inside macros, for one line.
974 Otherwise, we prefer // comments, so it's ok to warn about the
975 other. Likewise, it's ok for strings to extend across multiple
976 lines, as long as a line continuation character (backslash)
977 terminates each line. Although not currently prohibited by the C++
978 style guide, it's ugly and unnecessary. We don't do well with either
979 in this lint program, so we warn about both.
982 filename: The name of the current file.
983 clean_lines: A CleansedLines instance containing the file.
984 line_number: The number of the line to check.
985 error: The function to call with any errors found.
987 line = clean_lines.elided[line_number]
989 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
990 # second (escaped) slash may trigger later \" detection erroneously.
991 line = line.replace('\\\\', '')
993 if line.count('/*') > line.count('*/'):
994 error(filename, line_number, 'readability/multiline_comment', 5,
995 'Complex multi-line /*...*/-style comment found. '
996 'Lint may give bogus warnings. '
997 'Consider replacing these with //-style comments, '
998 'with #if 0...#endif, '
999 'or with more clearly structured multi-line comments.')
1001 if (line.count('"') - line.count('\\"')) % 2:
1002 error(filename, line_number, 'readability/multiline_string', 5,
1003 'Multi-line string ("...") found. This lint script doesn\'t '
1004 'do well with such strings, and may give bogus warnings. They\'re '
1005 'ugly and unnecessary, and you should use concatenation instead".')
1009 ('asctime(', 'asctime_r('),
1010 ('ctime(', 'ctime_r('),
1011 ('getgrgid(', 'getgrgid_r('),
1012 ('getgrnam(', 'getgrnam_r('),
1013 ('getlogin(', 'getlogin_r('),
1014 ('getpwnam(', 'getpwnam_r('),
1015 ('getpwuid(', 'getpwuid_r('),
1016 ('gmtime(', 'gmtime_r('),
1017 ('localtime(', 'localtime_r('),
1018 ('rand(', 'rand_r('),
1019 ('readdir(', 'readdir_r('),
1020 ('strtok(', 'strtok_r('),
1021 ('ttyname(', 'ttyname_r('),
1025 def check_posix_threading(filename, clean_lines, line_number, error):
1026 """Checks for calls to thread-unsafe functions.
1028 Much code has been originally written without consideration of
1029 multi-threading. Also, engineers are relying on their old experience;
1030 they have learned posix before threading extensions were added. These
1031 tests guide the engineers to use thread-safe functions (when using
1035 filename: The name of the current file.
1036 clean_lines: A CleansedLines instance containing the file.
1037 line_number: The number of the line to check.
1038 error: The function to call with any errors found.
1040 line = clean_lines.elided[line_number]
1041 for single_thread_function, multithread_safe_function in _THREADING_LIST:
1042 index = line.find(single_thread_function)
1043 # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
1044 if index >= 0 and (index == 0 or (not line[index - 1].isalnum()
1045 and line[index - 1] not in ('_', '.', '>'))):
1046 error(filename, line_number, 'runtime/threadsafe_fn', 2,
1047 'Consider using ' + multithread_safe_function +
1048 '...) instead of ' + single_thread_function +
1049 '...) for improved thread safety.')
1052 # Matches invalid increment: *count++, which moves pointer instead of
1053 # incrementing a value.
1054 _RE_PATTERN_INVALID_INCREMENT = re.compile(
1055 r'^\s*\*\w+(\+\+|--);')
1058 def check_invalid_increment(filename, clean_lines, line_number, error):
1059 """Checks for invalid increment *count++.
1061 For example following function:
1062 void increment_counter(int* count) {
1065 is invalid, because it effectively does count++, moving pointer, and should
1066 be replaced with ++*count, (*count)++ or *count += 1.
1069 filename: The name of the current file.
1070 clean_lines: A CleansedLines instance containing the file.
1071 line_number: The number of the line to check.
1072 error: The function to call with any errors found.
1074 line = clean_lines.elided[line_number]
1075 if _RE_PATTERN_INVALID_INCREMENT.match(line):
1076 error(filename, line_number, 'runtime/invalid_increment', 5,
1077 'Changing pointer instead of value (or unused value of operator*).')
1080 class _ClassInfo(object):
1081 """Stores information about a class."""
1083 def __init__(self, name, line_number):
1085 self.line_number = line_number
1086 self.seen_open_brace = False
1087 self.is_derived = False
1088 self.virtual_method_line_number = None
1089 self.has_virtual_destructor = False
1090 self.brace_depth = 0
1093 class _ClassState(object):
1094 """Holds the current state of the parse relating to class declarations.
1096 It maintains a stack of _ClassInfos representing the parser's guess
1097 as to the current nesting of class declarations. The innermost class
1098 is at the top (back) of the stack. Typically, the stack will either
1099 be empty or have exactly one entry.
1103 self.classinfo_stack = []
1105 def check_finished(self, filename, error):
1106 """Checks that all classes have been completely parsed.
1108 Call this when all lines in a file have been processed.
1110 filename: The name of the current file.
1111 error: The function to call with any errors found.
1113 if self.classinfo_stack:
1114 # Note: This test can result in false positives if #ifdef constructs
1115 # get in the way of brace matching. See the testBuildClass test in
1116 # cpp_style_unittest.py for an example of this.
1117 error(filename, self.classinfo_stack[0].line_number, 'build/class', 5,
1118 'Failed to find complete declaration of class %s' %
1119 self.classinfo_stack[0].name)
1122 def check_for_non_standard_constructs(filename, clean_lines, line_number,
1123 class_state, error):
1124 """Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
1126 Complain about several constructs which gcc-2 accepts, but which are
1127 not standard C++. Warning about these in lint is one way to ease the
1128 transition to new compilers.
1129 - put storage class first (e.g. "static const" instead of "const static").
1130 - "%lld" instead of %qd" in printf-type functions.
1131 - "%1$d" is non-standard in printf-type functions.
1132 - "\%" is an undefined character escape sequence.
1133 - text after #endif is not allowed.
1134 - invalid inner-style forward declaration.
1135 - >? and <? operators, and their >?= and <?= cousins.
1136 - classes with virtual methods need virtual destructors (compiler warning
1137 available, but not turned on yet.)
1139 Additionally, check for constructor/destructor style violations as it
1140 is very convenient to do so while checking for gcc-2 compliance.
1143 filename: The name of the current file.
1144 clean_lines: A CleansedLines instance containing the file.
1145 line_number: The number of the line to check.
1146 class_state: A _ClassState instance which maintains information about
1147 the current stack of nested class declarations being parsed.
1148 error: A callable to which errors are reported, which takes 4 arguments:
1149 filename, line number, error level, and message
1152 # Remove comments from the line, but leave in strings for now.
1153 line = clean_lines.lines[line_number]
1155 if search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
1156 error(filename, line_number, 'runtime/printf_format', 3,
1157 '%q in format strings is deprecated. Use %ll instead.')
1159 if search(r'printf\s*\(.*".*%\d+\$', line):
1160 error(filename, line_number, 'runtime/printf_format', 2,
1161 '%N$ formats are unconventional. Try rewriting to avoid them.')
1163 # Remove escaped backslashes before looking for undefined escapes.
1164 line = line.replace('\\\\', '')
1166 if search(r'("|\').*\\(%|\[|\(|{)', line):
1167 error(filename, line_number, 'build/printf_format', 3,
1168 '%, [, (, and { are undefined character escapes. Unescape them.')
1170 # For the rest, work with both comments and strings removed.
1171 line = clean_lines.elided[line_number]
1173 if search(r'\b(const|volatile|void|char|short|int|long'
1174 r'|float|double|signed|unsigned'
1175 r'|schar|u?int8|u?int16|u?int32|u?int64)'
1176 r'\s+(auto|register|static|extern|typedef)\b',
1178 error(filename, line_number, 'build/storage_class', 5,
1179 'Storage class (static, extern, typedef, etc) should be first.')
1181 if match(r'\s*#\s*endif\s*[^/\s]+', line):
1182 error(filename, line_number, 'build/endif_comment', 5,
1183 'Uncommented text after #endif is non-standard. Use a comment.')
1185 if match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
1186 error(filename, line_number, 'build/forward_decl', 5,
1187 'Inner-style forward declarations are invalid. Remove this line.')
1189 if search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?', line):
1190 error(filename, line_number, 'build/deprecated', 3,
1191 '>? and <? (max and min) operators are non-standard and deprecated.')
1193 # Track class entry and exit, and attempt to find cases within the
1194 # class declaration that don't meet the C++ style
1195 # guidelines. Tracking is very dependent on the code matching Google
1196 # style guidelines, but it seems to perform well enough in testing
1197 # to be a worthwhile addition to the checks.
1198 classinfo_stack = class_state.classinfo_stack
1199 # Look for a class declaration
1200 class_decl_match = match(
1201 r'\s*(template\s*<[\w\s<>,:]*>\s*)?(class|struct)\s+(\w+(::\w+)*)', line)
1202 if class_decl_match:
1203 classinfo_stack.append(_ClassInfo(class_decl_match.group(3), line_number))
1205 # Everything else in this function uses the top of the stack if it's
1207 if not classinfo_stack:
1210 classinfo = classinfo_stack[-1]
1212 # If the opening brace hasn't been seen look for it and also
1213 # parent class declarations.
1214 if not classinfo.seen_open_brace:
1215 # If the line has a ';' in it, assume it's a forward declaration or
1216 # a single-line class declaration, which we won't process.
1217 if line.find(';') != -1:
1218 classinfo_stack.pop()
1220 classinfo.seen_open_brace = (line.find('{') != -1)
1221 # Look for a bare ':'
1222 if search('(^|[^:]):($|[^:])', line):
1223 classinfo.is_derived = True
1224 if not classinfo.seen_open_brace:
1225 return # Everything else in this function is for after open brace
1227 # The class may have been declared with namespace or classname qualifiers.
1228 # The constructor and destructor will not have those qualifiers.
1229 base_classname = classinfo.name.split('::')[-1]
1231 # Look for single-argument constructors that aren't marked explicit.
1232 # Technically a valid construct, but against style.
1233 args = match(r'(?<!explicit)\s+%s\s*\(([^,()]+)\)'
1234 % re.escape(base_classname),
1237 and args.group(1) != 'void'
1238 and not match(r'(const\s+)?%s\s*&' % re.escape(base_classname),
1239 args.group(1).strip())):
1240 error(filename, line_number, 'runtime/explicit', 5,
1241 'Single-argument constructors should be marked explicit.')
1243 # Look for methods declared virtual.
1244 if search(r'\bvirtual\b', line):
1245 classinfo.virtual_method_line_number = line_number
1246 # Only look for a destructor declaration on the same line. It would
1247 # be extremely unlikely for the destructor declaration to occupy
1248 # more than one line.
1249 if search(r'~%s\s*\(' % base_classname, line):
1250 classinfo.has_virtual_destructor = True
1252 # Look for class end.
1253 brace_depth = classinfo.brace_depth
1254 brace_depth = brace_depth + line.count('{') - line.count('}')
1255 if brace_depth <= 0:
1256 classinfo = classinfo_stack.pop()
1257 # Try to detect missing virtual destructor declarations.
1258 # For now, only warn if a non-derived class with virtual methods lacks
1259 # a virtual destructor. This is to make it less likely that people will
1260 # declare derived virtual destructors without declaring the base
1261 # destructor virtual.
1262 if ((classinfo.virtual_method_line_number is not None)
1263 and (not classinfo.has_virtual_destructor)
1264 and (not classinfo.is_derived)): # Only warn for base classes
1265 error(filename, classinfo.line_number, 'runtime/virtual', 4,
1266 'The class %s probably needs a virtual destructor due to '
1267 'having virtual method(s), one declared at line %d.'
1268 % (classinfo.name, classinfo.virtual_method_line_number))
1270 classinfo.brace_depth = brace_depth
1273 def check_spacing_for_function_call(filename, line, line_number, error):
1274 """Checks for the correctness of various spacing around function calls.
1277 filename: The name of the current file.
1278 line: The text of the line to check.
1279 line_number: The number of the line to check.
1280 error: The function to call with any errors found.
1283 # Since function calls often occur inside if/for/foreach/while/switch
1284 # expressions - which have their own, more liberal conventions - we
1285 # first see if we should be looking inside such an expression for a
1286 # function call, to which we can apply more strict standards.
1287 function_call = line # if there's no control flow construct, look at whole line
1288 for pattern in (r'\bif\s*\((.*)\)\s*{',
1289 r'\bfor\s*\((.*)\)\s*{',
1290 r'\bforeach\s*\((.*)\)\s*{',
1291 r'\bwhile\s*\((.*)\)\s*[{;]',
1292 r'\bswitch\s*\((.*)\)\s*{'):
1293 matched = search(pattern, line)
1295 function_call = matched.group(1) # look inside the parens for function calls
1298 # Except in if/for/foreach/while/switch, there should never be space
1299 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
1300 # for nested parens ( (a+b) + c ). Likewise, there should never be
1301 # a space before a ( when it's a function argument. I assume it's a
1302 # function argument when the char before the whitespace is legal in
1303 # a function name (alnum + _) and we're not starting a macro. Also ignore
1304 # pointers and references to arrays and functions coz they're too tricky:
1305 # we use a very simple way to recognize these:
1306 # " (something)(maybe-something)" or
1307 # " (something)(maybe-something," or
1308 # " (something)[something]"
1309 # Note that we assume the contents of [] to be short enough that
1310 # they'll never need to wrap.
1311 if ( # Ignore control structures.
1312 not search(r'\b(if|for|foreach|while|switch|return|new|delete)\b', function_call)
1313 # Ignore pointers/references to functions.
1314 and not search(r' \([^)]+\)\([^)]*(\)|,$)', function_call)
1315 # Ignore pointers/references to arrays.
1316 and not search(r' \([^)]+\)\[[^\]]+\]', function_call)):
1317 if search(r'\w\s*\([ \t](?!\s*\\$)', function_call): # a ( used for a fn call
1318 error(filename, line_number, 'whitespace/parens', 4,
1319 'Extra space after ( in function call')
1320 elif search(r'\([ \t]+(?!(\s*\\)|\()', function_call):
1321 error(filename, line_number, 'whitespace/parens', 2,
1322 'Extra space after (')
1323 if (search(r'\w\s+\(', function_call)
1324 and not search(r'#\s*define|typedef', function_call)):
1325 error(filename, line_number, 'whitespace/parens', 4,
1326 'Extra space before ( in function call')
1327 # If the ) is followed only by a newline or a { + newline, assume it's
1328 # part of a control statement (if/while/etc), and don't complain
1329 if search(r'[^)\s]\s+\)(?!\s*$|{\s*$)', function_call):
1330 error(filename, line_number, 'whitespace/parens', 2,
1331 'Extra space before )')
1334 def is_blank_line(line):
1335 """Returns true if the given line is blank.
1337 We consider a line to be blank if the line is empty or consists of
1341 line: A line of a string.
1344 True, if the given line is blank.
1346 return not line or line.isspace()
1349 def check_for_function_lengths(filename, clean_lines, line_number,
1350 function_state, error):
1351 """Reports for long function bodies.
1353 For an overview why this is done, see:
1354 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
1356 Uses a simplistic algorithm assuming other style guidelines
1357 (especially spacing) are followed.
1358 Only checks unindented functions, so class members are unchecked.
1359 Trivial bodies are unchecked, so constructors with huge initializer lists
1361 Blank/comment lines are not counted so as to avoid encouraging the removal
1362 of vertical space and commments just to get through a lint check.
1363 NOLINT *on the last line of a function* disables this check.
1366 filename: The name of the current file.
1367 clean_lines: A CleansedLines instance containing the file.
1368 line_number: The number of the line to check.
1369 function_state: Current function name and lines in body so far.
1370 error: The function to call with any errors found.
1372 lines = clean_lines.lines
1373 line = lines[line_number]
1374 raw = clean_lines.raw_lines
1375 raw_line = raw[line_number]
1378 starting_func = False
1379 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
1380 match_result = match(regexp, line)
1382 # If the name is all caps and underscores, figure it's a macro and
1383 # ignore it, unless it's TEST or TEST_F.
1384 function_name = match_result.group(1).split()[-1]
1385 if function_name == 'TEST' or function_name == 'TEST_F' or (not match(r'[A-Z_]+$', function_name)):
1386 starting_func = True
1390 for start_line_number in xrange(line_number, clean_lines.num_lines()):
1391 start_line = lines[start_line_number]
1392 joined_line += ' ' + start_line.lstrip()
1393 if search(r'(;|})', start_line): # Declarations and trivial functions
1396 if search(r'{', start_line):
1398 function = search(r'((\w|:)*)\(', line).group(1)
1399 if match(r'TEST', function): # Handle TEST... macros
1400 parameter_regexp = search(r'(\(.*\))', joined_line)
1401 if parameter_regexp: # Ignore bad syntax
1402 function += parameter_regexp.group(1)
1405 function_state.begin(function)
1408 # No body for the function (or evidence of a non-function) was found.
1409 error(filename, line_number, 'readability/fn_size', 5,
1410 'Lint failed to find start of function body.')
1411 elif match(r'^\}\s*$', line): # function end
1412 if not search(r'\bNOLINT\b', raw_line):
1413 function_state.check(error, filename, line_number)
1414 function_state.end()
1415 elif not match(r'^\s*$', line):
1416 function_state.count() # Count non-blank/non-comment lines.
1419 def check_spacing(filename, clean_lines, line_number, error):
1420 """Checks for the correctness of various spacing issues in the code.
1422 Things we check for: spaces around operators, spaces after
1423 if/for/while/switch, no spaces around parens in function calls, two
1424 spaces between code and comment, don't start a block with a blank
1425 line, don't end a function with a blank line, don't have too many
1426 blank lines in a row.
1429 filename: The name of the current file.
1430 clean_lines: A CleansedLines instance containing the file.
1431 line_number: The number of the line to check.
1432 error: The function to call with any errors found.
1435 raw = clean_lines.raw_lines
1436 line = raw[line_number]
1438 # Before nixing comments, check if the line is blank for no good
1439 # reason. This includes the first line after a block is opened, and
1440 # blank lines at the end of a function (ie, right before a line like '}').
1441 if is_blank_line(line):
1442 elided = clean_lines.elided
1443 previous_line = elided[line_number - 1]
1444 previous_brace = previous_line.rfind('{')
1445 # FIXME: Don't complain if line before blank line, and line after,
1446 # both start with alnums and are indented the same amount.
1447 # This ignores whitespace at the start of a namespace block
1448 # because those are not usually indented.
1449 if (previous_brace != -1 and previous_line[previous_brace:].find('}') == -1
1450 and previous_line[:previous_brace].find('namespace') == -1):
1451 # OK, we have a blank line at the start of a code block. Before we
1452 # complain, we check if it is an exception to the rule: The previous
1453 # non-empty line has the parameters of a function header that are indented
1454 # 4 spaces (because they did not fit in a 80 column line when placed on
1455 # the same line as the function name). We also check for the case where
1456 # the previous line is indented 6 spaces, which may happen when the
1457 # initializers of a constructor do not fit into a 80 column line.
1459 if match(r' {6}\w', previous_line): # Initializer list?
1460 # We are looking for the opening column of initializer list, which
1461 # should be indented 4 spaces to cause 6 space indentation afterwards.
1462 search_position = line_number - 2
1463 while (search_position >= 0
1464 and match(r' {6}\w', elided[search_position])):
1465 search_position -= 1
1466 exception = (search_position >= 0
1467 and elided[search_position][:5] == ' :')
1469 # Search for the function arguments or an initializer list. We use a
1470 # simple heuristic here: If the line is indented 4 spaces; and we have a
1471 # closing paren, without the opening paren, followed by an opening brace
1472 # or colon (for initializer lists) we assume that it is the last line of
1473 # a function header. If we have a colon indented 4 spaces, it is an
1475 exception = (match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
1477 or match(r' {4}:', previous_line))
1480 error(filename, line_number, 'whitespace/blank_line', 2,
1481 'Blank line at the start of a code block. Is this needed?')
1482 # This doesn't ignore whitespace at the end of a namespace block
1483 # because that is too hard without pairing open/close braces;
1484 # however, a special exception is made for namespace closing
1485 # brackets which have a comment containing "namespace".
1487 # Also, ignore blank lines at the end of a block in a long if-else
1490 # // Something followed by a blank line
1492 # } else if (condition2) {
1495 if line_number + 1 < clean_lines.num_lines():
1496 next_line = raw[line_number + 1]
1498 and match(r'\s*}', next_line)
1499 and next_line.find('namespace') == -1
1500 and next_line.find('} else ') == -1):
1501 error(filename, line_number, 'whitespace/blank_line', 3,
1502 'Blank line at the end of a code block. Is this needed?')
1504 # Next, we complain if there's a comment too near the text
1505 comment_position = line.find('//')
1506 if comment_position != -1:
1507 # Check if the // may be in quotes. If so, ignore it
1508 # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
1509 if (line.count('"', 0, comment_position) - line.count('\\"', 0, comment_position)) % 2 == 0: # not in quotes
1510 # Allow one space for new scopes, two spaces otherwise:
1511 if (not match(r'^\s*{ //', line)
1512 and ((comment_position >= 1
1513 and line[comment_position-1] not in string.whitespace)
1514 or (comment_position >= 2
1515 and line[comment_position-2] not in string.whitespace))):
1516 error(filename, line_number, 'whitespace/comments', 2,
1517 'At least two spaces is best between code and comments')
1518 # There should always be a space between the // and the comment
1519 commentend = comment_position + 2
1520 if commentend < len(line) and not line[commentend] == ' ':
1521 # but some lines are exceptions -- e.g. if they're big
1522 # comment delimiters like:
1523 # //----------------------------------------------------------
1524 # or they begin with multiple slashes followed by a space:
1525 # //////// Header comment
1526 matched = (search(r'[=/-]{4,}\s*$', line[commentend:])
1527 or search(r'^/+ ', line[commentend:]))
1529 error(filename, line_number, 'whitespace/comments', 4,
1530 'Should have a space between // and comment')
1532 line = clean_lines.elided[line_number] # get rid of comments and strings
1534 # Don't try to do spacing checks for operator methods
1535 line = re.sub(r'operator(==|!=|<|<<|<=|>=|>>|>)\(', 'operator\(', line)
1536 # Don't try to do spacing checks for #include statements at minimum it
1537 # messes up checks for spacing around /
1538 if match(r'\s*#\s*include', line):
1540 if search(r'[\w.]=[\w.]', line):
1541 error(filename, line_number, 'whitespace/operators', 4,
1542 'Missing spaces around =')
1544 # FIXME: It's not ok to have spaces around binary operators like .
1546 # You should always have whitespace around binary operators.
1547 # Alas, we can't test < or > because they're legitimately used sans spaces
1548 # (a->b, vector<int> a). The only time we can tell is a < with no >, and
1549 # only if it's not template params list spilling into the next line.
1550 matched = search(r'[^<>=!\s](==|!=|\+=|-=|\*=|/=|/|\|=|&=|<<=|>>=|<=|>=|\|\||\||&&|>>|<<)[^<>=!\s]', line)
1552 # Note that while it seems that the '<[^<]*' term in the following
1553 # regexp could be simplified to '<.*', which would indeed match
1554 # the same class of strings, the [^<] means that searching for the
1555 # regexp takes linear rather than quadratic time.
1556 if not search(r'<[^<]*,\s*$', line): # template params spill
1557 matched = search(r'[^<>=!\s](<)[^<>=!\s]([^>]|->)*$', line)
1559 error(filename, line_number, 'whitespace/operators', 3,
1560 'Missing spaces around %s' % matched.group(1))
1562 # There shouldn't be space around unary operators
1563 matched = search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
1565 error(filename, line_number, 'whitespace/operators', 4,
1566 'Extra space for operator %s' % matched.group(1))
1568 # A pet peeve of mine: no spaces after an if, while, switch, or for
1569 matched = search(r' (if\(|for\(|foreach\(|while\(|switch\()', line)
1571 error(filename, line_number, 'whitespace/parens', 5,
1572 'Missing space before ( in %s' % matched.group(1))
1574 # For if/for/foreach/while/switch, the left and right parens should be
1575 # consistent about how many spaces are inside the parens, and
1576 # there should either be zero or one spaces inside the parens.
1577 # We don't want: "if ( foo)" or "if ( foo )".
1578 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
1579 matched = search(r'\b(if|for|foreach|while|switch)\s*\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
1582 if len(matched.group(2)) != len(matched.group(4)):
1583 if not (matched.group(3) == ';'
1584 and len(matched.group(2)) == 1 + len(matched.group(4))
1585 or not matched.group(2) and search(r'\bfor\s*\(.*; \)', line)):
1586 error(filename, line_number, 'whitespace/parens', 5,
1587 'Mismatching spaces inside () in %s' % matched.group(1))
1588 if not len(matched.group(2)) in [0, 1]:
1589 error(filename, line_number, 'whitespace/parens', 5,
1590 'Should have zero or one spaces inside ( and ) in %s' %
1593 # You should always have a space after a comma (either as fn arg or operator)
1594 if search(r',[^\s]', line):
1595 error(filename, line_number, 'whitespace/comma', 3,
1596 'Missing space after ,')
1598 if filename.endswith('.cpp'):
1599 # C++ should have the & or * beside the type not the variable name.
1600 matched = match(r'\s*\w+(?<!\breturn)\s+(?P<pointer_operator>\*|\&)\w+', line)
1602 error(filename, line_number, 'whitespace/declaration', 3,
1603 'Declaration has space between type name and %s in %s' % (matched.group('pointer_operator'), matched.group(0).strip()))
1605 elif filename.endswith('.c'):
1606 # C Pointer declaration should have the * beside the variable not the type name.
1607 matched = search(r'^\s*\w+\*\s+\w+', line)
1609 error(filename, line_number, 'whitespace/declaration', 3,
1610 'Declaration has space between * and variable name in %s' % matched.group(0).strip())
1612 # Next we will look for issues with function calls.
1613 check_spacing_for_function_call(filename, line, line_number, error)
1615 # Except after an opening paren, you should have spaces before your braces.
1616 # And since you should never have braces at the beginning of a line, this is
1618 if search(r'[^ ({]{', line):
1619 error(filename, line_number, 'whitespace/braces', 5,
1620 'Missing space before {')
1622 # Make sure '} else {' has spaces.
1623 if search(r'}else', line):
1624 error(filename, line_number, 'whitespace/braces', 5,
1625 'Missing space before else')
1627 # You shouldn't have spaces before your brackets, except maybe after
1628 # 'delete []' or 'new char * []'.
1629 if search(r'\w\s+\[', line) and not search(r'delete\s+\[', line):
1630 error(filename, line_number, 'whitespace/braces', 5,
1631 'Extra space before [')
1633 # You shouldn't have a space before a semicolon at the end of the line.
1634 # There's a special case for "for" since the style guide allows space before
1635 # the semicolon there.
1636 if search(r':\s*;\s*$', line):
1637 error(filename, line_number, 'whitespace/semicolon', 5,
1638 'Semicolon defining empty statement. Use { } instead.')
1639 elif search(r'^\s*;\s*$', line):
1640 error(filename, line_number, 'whitespace/semicolon', 5,
1641 'Line contains only semicolon. If this should be an empty statement, '
1643 elif (search(r'\s+;\s*$', line) and not search(r'\bfor\b', line)):
1644 error(filename, line_number, 'whitespace/semicolon', 5,
1645 'Extra space before last semicolon. If this should be an empty '
1646 'statement, use { } instead.')
1647 elif (search(r'\b(for|while)\s*\(.*\)\s*;\s*$', line)
1648 and line.count('(') == line.count(')')
1649 # Allow do {} while();
1650 and not search(r'}\s*while', line)):
1651 error(filename, line_number, 'whitespace/semicolon', 5,
1652 'Semicolon defining empty statement for this loop. Use { } instead.')
1655 def get_previous_non_blank_line(clean_lines, line_number):
1656 """Return the most recent non-blank line and its line number.
1659 clean_lines: A CleansedLines instance containing the file contents.
1660 line_number: The number of the line to check.
1663 A tuple with two elements. The first element is the contents of the last
1664 non-blank line before the current line, or the empty string if this is the
1665 first non-blank line. The second is the line number of that line, or -1
1666 if this is the first non-blank line.
1669 previous_line_number = line_number - 1
1670 while previous_line_number >= 0:
1671 previous_line = clean_lines.elided[previous_line_number]
1672 if not is_blank_line(previous_line): # if not a blank line...
1673 return (previous_line, previous_line_number)
1674 previous_line_number -= 1
1678 def check_namespace_indentation(filename, clean_lines, line_number, file_extension, error):
1679 """Looks for indentation errors inside of namespaces.
1682 filename: The name of the current file.
1683 clean_lines: A CleansedLines instance containing the file.
1684 line_number: The number of the line to check.
1685 file_extension: The extension (dot not included) of the file.
1686 error: The function to call with any errors found.
1689 line = clean_lines.elided[line_number] # Get rid of comments and strings.
1691 namespace_match = match(r'(?P<namespace_indentation>\s*)namespace\s+\S+\s*{\s*$', line)
1692 if not namespace_match:
1695 current_indentation_level = len(namespace_match.group('namespace_indentation'))
1696 if current_indentation_level > 0:
1697 error(filename, line_number, 'whitespace/indent', 4,
1698 'namespace should never be indented.')
1700 looking_for_semicolon = False;
1702 in_preprocessor_directive = False;
1703 for current_line in clean_lines.elided[line_number + 1:]:
1705 if not current_line.strip():
1707 if not current_indentation_level:
1708 if not (in_preprocessor_directive or looking_for_semicolon):
1709 if not match(r'\S', current_line):
1710 error(filename, line_number + line_offset, 'whitespace/indent', 4,
1711 'Code inside a namespace should not be indented.')
1712 if in_preprocessor_directive or (current_line.strip()[0] == '#'): # This takes care of preprocessor directive syntax.
1713 in_preprocessor_directive = current_line[-1] == '\\'
1715 looking_for_semicolon = ((current_line.find(';') == -1) and (current_line.strip()[-1] != '}')) or (current_line[-1] == '\\')
1717 looking_for_semicolon = False; # If we have a brace we may not need a semicolon.
1718 current_indentation_level += current_line.count('{') - current_line.count('}')
1719 if current_indentation_level < 0:
1722 def check_using_std(filename, clean_lines, line_number, error):
1723 """Looks for 'using std::foo;' statements which should be replaced with 'using namespace std;'.
1726 filename: The name of the current file.
1727 clean_lines: A CleansedLines instance containing the file.
1728 line_number: The number of the line to check.
1729 error: The function to call with any errors found.
1732 # This check doesn't apply to C or Objective-C implementation files.
1733 if filename.endswith('.c') or filename.endswith('.m'):
1736 line = clean_lines.elided[line_number] # Get rid of comments and strings.
1738 using_std_match = match(r'\s*using\s+std::(?P<method_name>\S+)\s*;\s*$', line)
1739 if not using_std_match:
1742 method_name = using_std_match.group('method_name')
1743 error(filename, line_number, 'build/using_std', 4,
1744 "Use 'using namespace std;' instead of 'using std::%s;'." % method_name)
1747 def check_max_min_macros(filename, clean_lines, line_number, error):
1748 """Looks use of MAX() and MIN() macros that should be replaced with std::max() and std::min().
1751 filename: The name of the current file.
1752 clean_lines: A CleansedLines instance containing the file.
1753 line_number: The number of the line to check.
1754 error: The function to call with any errors found.
1757 # This check doesn't apply to C or Objective-C implementation files.
1758 if filename.endswith('.c') or filename.endswith('.m'):
1761 line = clean_lines.elided[line_number] # Get rid of comments and strings.
1763 max_min_macros_search = search(r'\b(?P<max_min_macro>(MAX|MIN))\s*\(', line)
1764 if not max_min_macros_search:
1767 max_min_macro = max_min_macros_search.group('max_min_macro')
1768 max_min_macro_lower = max_min_macro.lower()
1769 error(filename, line_number, 'runtime/max_min_macros', 4,
1770 'Use std::%s() or std::%s<type>() instead of the %s() macro.'
1771 % (max_min_macro_lower, max_min_macro_lower, max_min_macro))
1774 def check_switch_indentation(filename, clean_lines, line_number, error):
1775 """Looks for indentation errors inside of switch statements.
1778 filename: The name of the current file.
1779 clean_lines: A CleansedLines instance containing the file.
1780 line_number: The number of the line to check.
1781 error: The function to call with any errors found.
1784 line = clean_lines.elided[line_number] # Get rid of comments and strings.
1786 switch_match = match(r'(?P<switch_indentation>\s*)switch\s*\(.+\)\s*{\s*$', line)
1787 if not switch_match:
1790 switch_indentation = switch_match.group('switch_indentation')
1791 inner_indentation = switch_indentation + ' ' * 4
1793 encountered_nested_switch = False
1795 for current_line in clean_lines.elided[line_number + 1:]:
1798 # Skip not only empty lines but also those with preprocessor directives.
1799 if current_line.strip() == '' or current_line.startswith('#'):
1802 if match(r'\s*switch\s*\(.+\)\s*{\s*$', current_line):
1803 # Complexity alarm - another switch statement nested inside the one
1804 # that we're currently testing. We'll need to track the extent of
1805 # that inner switch if the upcoming label tests are still supposed
1806 # to work correctly. Let's not do that; instead, we'll finish
1807 # checking this line, and then leave it like that. Assuming the
1808 # indentation is done consistently (even if incorrectly), this will
1809 # still catch all indentation issues in practice.
1810 encountered_nested_switch = True
1812 current_indentation_match = match(r'(?P<indentation>\s*)(?P<remaining_line>.*)$', current_line);
1813 current_indentation = current_indentation_match.group('indentation')
1814 remaining_line = current_indentation_match.group('remaining_line')
1816 # End the check at the end of the switch statement.
1817 if remaining_line.startswith('}') and current_indentation == switch_indentation:
1819 # Case and default branches should not be indented. The regexp also
1820 # catches single-line cases like "default: break;" but does not trigger
1821 # on stuff like "Document::Foo();".
1822 elif match(r'(default|case\s+.*)\s*:([^:].*)?$', remaining_line):
1823 if current_indentation != switch_indentation:
1824 error(filename, line_number + line_offset, 'whitespace/indent', 4,
1825 'A case label should not be indented, but line up with its switch statement.')
1826 # Don't throw an error for multiple badly indented labels,
1827 # one should be enough to figure out the problem.
1829 # We ignore goto labels at the very beginning of a line.
1830 elif match(r'\w+\s*:\s*$', remaining_line):
1832 # It's not a goto label, so check if it's indented at least as far as
1833 # the switch statement plus one more level of indentation.
1834 elif not current_indentation.startswith(inner_indentation):
1835 error(filename, line_number + line_offset, 'whitespace/indent', 4,
1836 'Non-label code inside switch statements should be indented.')
1837 # Don't throw an error for multiple badly indented statements,
1838 # one should be enough to figure out the problem.
1841 if encountered_nested_switch:
1845 def check_braces(filename, clean_lines, line_number, error):
1846 """Looks for misplaced braces (e.g. at the end of line).
1849 filename: The name of the current file.
1850 clean_lines: A CleansedLines instance containing the file.
1851 line_number: The number of the line to check.
1852 error: The function to call with any errors found.
1855 line = clean_lines.elided[line_number] # Get rid of comments and strings.
1857 if match(r'\s*{\s*$', line):
1858 # We allow an open brace to start a line in the case where someone
1859 # is using braces for function definition or in a block to
1860 # explicitly create a new scope, which is commonly used to control
1861 # the lifetime of stack-allocated variables. We don't detect this
1862 # perfectly: we just don't complain if the last non-whitespace
1863 # character on the previous non-blank line is ';', ':', '{', '}',
1864 # ')', or ') const' and doesn't begin with 'if|for|while|switch|else'.
1865 # We also allow '#' for #endif and '=' for array initialization.
1866 previous_line = get_previous_non_blank_line(clean_lines, line_number)[0]
1867 if ((not search(r'[;:}{)=]\s*$|\)\s*const\s*$', previous_line)
1868 or search(r'\b(if|for|foreach|while|switch|else)\b', previous_line))
1869 and previous_line.find('#') < 0):
1870 error(filename, line_number, 'whitespace/braces', 4,
1871 'This { should be at the end of the previous line')
1872 elif (search(r'\)\s*(const\s*)?{\s*$', line)
1873 and line.count('(') == line.count(')')
1874 and not search(r'\b(if|for|foreach|while|switch)\b', line)):
1875 error(filename, line_number, 'whitespace/braces', 4,
1876 'Place brace on its own line for function definitions.')
1878 if (match(r'\s*}\s*(else\s*({\s*)?)?$', line) and line_number > 1):
1879 # We check if a closed brace has started a line to see if a
1880 # one line control statement was previous.
1881 previous_line = clean_lines.elided[line_number - 2]
1882 if (previous_line.find('{') > 0
1883 and search(r'\b(if|for|foreach|while|else)\b', previous_line)):
1884 error(filename, line_number, 'whitespace/braces', 4,
1885 'One line control clauses should not use braces.')
1887 # An else clause should be on the same line as the preceding closing brace.
1888 if match(r'\s*else\s*', line):
1889 previous_line = get_previous_non_blank_line(clean_lines, line_number)[0]
1890 if match(r'\s*}\s*$', previous_line):
1891 error(filename, line_number, 'whitespace/newline', 4,
1892 'An else should appear on the same line as the preceding }')
1894 # Likewise, an else should never have the else clause on the same line
1895 if search(r'\belse [^\s{]', line) and not search(r'\belse if\b', line):
1896 error(filename, line_number, 'whitespace/newline', 4,
1897 'Else clause should never be on same line as else (use 2 lines)')
1899 # In the same way, a do/while should never be on one line
1900 if match(r'\s*do [^\s{]', line):
1901 error(filename, line_number, 'whitespace/newline', 4,
1902 'do/while clauses should not be on a single line')
1904 # Braces shouldn't be followed by a ; unless they're defining a struct
1905 # or initializing an array.
1906 # We can't tell in general, but we can for some common cases.
1907 previous_line_number = line_number
1909 (previous_line, previous_line_number) = get_previous_non_blank_line(clean_lines, previous_line_number)
1910 if match(r'\s+{.*}\s*;', line) and not previous_line.count(';'):
1911 line = previous_line + line
1914 if (search(r'{.*}\s*;', line)
1915 and line.count('{') == line.count('}')
1916 and not search(r'struct|class|enum|\s*=\s*{', line)):
1917 error(filename, line_number, 'readability/braces', 4,
1918 "You don't need a ; after a }")
1921 def check_exit_statement_simplifications(filename, clean_lines, line_number, error):
1922 """Looks for else or else-if statements that should be written as an
1923 if statement when the prior if concludes with a return, break, continue or
1927 filename: The name of the current file.
1928 clean_lines: A CleansedLines instance containing the file.
1929 line_number: The number of the line to check.
1930 error: The function to call with any errors found.
1933 line = clean_lines.elided[line_number] # Get rid of comments and strings.
1935 else_match = match(r'(?P<else_indentation>\s*)(\}\s*)?else(\s+if\s*\(|(?P<else>\s*(\{\s*)?\Z))', line)
1939 else_indentation = else_match.group('else_indentation')
1940 inner_indentation = else_indentation + ' ' * 4
1942 previous_lines = clean_lines.elided[:line_number]
1943 previous_lines.reverse()
1945 encountered_exit_statement = False
1947 for current_line in previous_lines:
1950 # Skip not only empty lines but also those with preprocessor directives
1952 if current_line.strip() == '' or current_line.startswith('#') or match(r'\w+\s*:\s*$', current_line):
1955 # Skip lines with closing braces on the original indentation level.
1956 # Even though the styleguide says they should be on the same line as
1957 # the "else if" statement, we also want to check for instances where
1958 # the current code does not comply with the coding style. Thus, ignore
1959 # these lines and proceed to the line before that.
1960 if current_line == else_indentation + '}':
1963 current_indentation_match = match(r'(?P<indentation>\s*)(?P<remaining_line>.*)$', current_line);
1964 current_indentation = current_indentation_match.group('indentation')
1965 remaining_line = current_indentation_match.group('remaining_line')
1967 # As we're going up the lines, the first real statement to encounter
1968 # has to be an exit statement (return, break, continue or goto) -
1969 # otherwise, this check doesn't apply.
1970 if not encountered_exit_statement:
1971 # We only want to find exit statements if they are on exactly
1972 # the same level of indentation as expected from the code inside
1973 # the block. If the indentation doesn't strictly match then we
1974 # might have a nested if or something, which must be ignored.
1975 if current_indentation != inner_indentation:
1977 if match(r'(return(\W+.*)|(break|continue)\s*;|goto\s*\w+;)$', remaining_line):
1978 encountered_exit_statement = True
1982 # When code execution reaches this point, we've found an exit statement
1983 # as last statement of the previous block. Now we only need to make
1984 # sure that the block belongs to an "if", then we can throw an error.
1986 # Skip lines with opening braces on the original indentation level,
1987 # similar to the closing braces check above. ("if (condition)\n{")
1988 if current_line == else_indentation + '{':
1991 # Skip everything that's further indented than our "else" or "else if".
1992 if current_indentation.startswith(else_indentation) and current_indentation != else_indentation:
1995 # So we've got a line with same (or less) indentation. Is it an "if"?
1996 # If yes: throw an error. If no: don't throw an error.
1997 # Whatever the outcome, this is the end of our loop.
1998 if match(r'if\s*\(', remaining_line):
1999 if else_match.start('else') != -1:
2000 error(filename, line_number + line_offset, 'readability/control_flow', 4,
2001 'An else statement can be removed when the prior "if" '
2002 'concludes with a return, break, continue or goto statement.')
2004 error(filename, line_number + line_offset, 'readability/control_flow', 4,
2005 'An else if statement should be written as an if statement '
2006 'when the prior "if" concludes with a return, break, '
2007 'continue or goto statement.')
2011 def replaceable_check(operator, macro, line):
2012 """Determine whether a basic CHECK can be replaced with a more specific one.
2014 For example suggest using CHECK_EQ instead of CHECK(a == b) and
2015 similarly for CHECK_GE, CHECK_GT, CHECK_LE, CHECK_LT, CHECK_NE.
2018 operator: The C++ operator used in the CHECK.
2019 macro: The CHECK or EXPECT macro being called.
2020 line: The current source line.
2023 True if the CHECK can be replaced with a more specific one.
2026 # This matches decimal and hex integers, strings, and chars (in that order).
2027 match_constant = r'([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')'
2029 # Expression to match two sides of the operator with something that
2030 # looks like a literal, since CHECK(x == iterator) won't compile.
2031 # This means we can't catch all the cases where a more specific
2032 # CHECK is possible, but it's less annoying than dealing with
2033 # extraneous warnings.
2034 match_this = (r'\s*' + macro + r'\((\s*' +
2035 match_constant + r'\s*' + operator + r'[^<>].*|'
2036 r'.*[^<>]' + operator + r'\s*' + match_constant +
2039 # Don't complain about CHECK(x == NULL) or similar because
2040 # CHECK_EQ(x, NULL) won't compile (requires a cast).
2041 # Also, don't complain about more complex boolean expressions
2042 # involving && or || such as CHECK(a == b || c == d).
2043 return match(match_this, line) and not search(r'NULL|&&|\|\|', line)
2046 def check_check(filename, clean_lines, line_number, error):
2047 """Checks the use of CHECK and EXPECT macros.
2050 filename: The name of the current file.
2051 clean_lines: A CleansedLines instance containing the file.
2052 line_number: The number of the line to check.
2053 error: The function to call with any errors found.
2056 # Decide the set of replacement macros that should be suggested
2057 raw_lines = clean_lines.raw_lines
2059 for macro in _CHECK_MACROS:
2060 if raw_lines[line_number].find(macro) >= 0:
2061 current_macro = macro
2063 if not current_macro:
2064 # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT'
2067 line = clean_lines.elided[line_number] # get rid of comments and strings
2069 # Encourage replacing plain CHECKs with CHECK_EQ/CHECK_NE/etc.
2070 for operator in ['==', '!=', '>=', '>', '<=', '<']:
2071 if replaceable_check(operator, current_macro, line):
2072 error(filename, line_number, 'readability/check', 2,
2073 'Consider using %s instead of %s(a %s b)' % (
2074 _CHECK_REPLACEMENT[current_macro][operator],
2075 current_macro, operator))
2079 def check_for_comparisons_to_zero(filename, clean_lines, line_number, error):
2080 # Get the line without comments and strings.
2081 line = clean_lines.elided[line_number]
2083 # Include NULL here so that users don't have to convert NULL to 0 first and then get this error.
2084 if search(r'[=!]=\s*(NULL|0|true|false)\W', line) or search(r'\W(NULL|0|true|false)\s*[=!]=', line):
2085 error(filename, line_number, 'readability/comparison_to_zero', 5,
2086 'Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons.')
2089 def check_for_null(filename, clean_lines, line_number, error):
2090 # This check doesn't apply to C or Objective-C implementation files.
2091 if filename.endswith('.c') or filename.endswith('.m'):
2094 line = clean_lines.elided[line_number]
2095 if search(r'\bNULL\b', line):
2096 error(filename, line_number, 'readability/null', 5, 'Use 0 instead of NULL.')
2099 line = clean_lines.raw_lines[line_number]
2100 # See if NULL occurs in any comments in the line. If the search for NULL using the raw line
2101 # matches, then do the check with strings collapsed to avoid giving errors for
2102 # NULLs occurring in strings.
2103 if search(r'\bNULL\b', line) and search(r'\bNULL\b', CleansedLines.collapse_strings(line)):
2104 error(filename, line_number, 'readability/null', 4, 'Use 0 instead of NULL.')
2106 def get_line_width(line):
2107 """Determines the width of the line in column positions.
2110 line: A string, which may be a Unicode string.
2113 The width of the line in column positions, accounting for Unicode
2114 combining characters and wide characters.
2116 if isinstance(line, unicode):
2118 for c in unicodedata.normalize('NFC', line):
2119 if unicodedata.east_asian_width(c) in ('W', 'F'):
2121 elif not unicodedata.combining(c):
2127 def check_style(filename, clean_lines, line_number, file_extension, error):
2128 """Checks rules from the 'C++ style rules' section of cppguide.html.
2130 Most of these rules are hard to test (naming, comment style), but we
2131 do what we can. In particular we check for 4-space indents, line lengths,
2132 tab usage, spaces inside code, etc.
2135 filename: The name of the current file.
2136 clean_lines: A CleansedLines instance containing the file.
2137 line_number: The number of the line to check.
2138 file_extension: The extension (without the dot) of the filename.
2139 error: The function to call with any errors found.
2142 raw_lines = clean_lines.raw_lines
2143 line = raw_lines[line_number]
2145 if line.find('\t') != -1:
2146 error(filename, line_number, 'whitespace/tab', 1,
2147 'Tab found; better to use spaces')
2149 # One or three blank spaces at the beginning of the line is weird; it's
2150 # hard to reconcile that with 4-space indents.
2151 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
2152 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
2153 # if(RLENGTH > 20) complain = 0;
2154 # if(match($0, " +(error|private|public|protected):")) complain = 0;
2155 # if(match(prev, "&& *$")) complain = 0;
2156 # if(match(prev, "\\|\\| *$")) complain = 0;
2157 # if(match(prev, "[\",=><] *$")) complain = 0;
2158 # if(match($0, " <<")) complain = 0;
2159 # if(match(prev, " +for \\(")) complain = 0;
2160 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
2162 cleansed_line = clean_lines.elided[line_number]
2163 while initial_spaces < len(line) and line[initial_spaces] == ' ':
2165 if line and line[-1].isspace():
2166 error(filename, line_number, 'whitespace/end_of_line', 4,
2167 'Line ends in whitespace. Consider deleting these extra spaces.')
2168 # There are certain situations we allow one space, notably for labels
2169 elif ((initial_spaces >= 1 and initial_spaces <= 3)
2170 and not match(r'\s*\w+\s*:\s*$', cleansed_line)):
2171 error(filename, line_number, 'whitespace/indent', 3,
2172 'Weird number of spaces at line-start. '
2173 'Are you using a 4-space indent?')
2174 # Labels should always be indented at least one space.
2175 elif not initial_spaces and line[:2] != '//':
2176 label_match = match(r'(?P<label>[^:]+):\s*$', line)
2179 label = label_match.group('label')
2180 # Only throw errors for stuff that is definitely not a goto label,
2181 # because goto labels can in fact occur at the start of the line.
2182 if label in ['public', 'private', 'protected'] or label.find(' ') != -1:
2183 error(filename, line_number, 'whitespace/labels', 4,
2184 'Labels should always be indented at least one space. '
2185 'If this is a member-initializer list in a constructor, '
2186 'the colon should be on the line after the definition header.')
2188 if (cleansed_line.count(';') > 1
2189 # for loops are allowed two ;'s (and may run over two lines).
2190 and cleansed_line.find('for') == -1
2191 and (get_previous_non_blank_line(clean_lines, line_number)[0].find('for') == -1
2192 or get_previous_non_blank_line(clean_lines, line_number)[0].find(';') != -1)
2193 # It's ok to have many commands in a switch case that fits in 1 line
2194 and not ((cleansed_line.find('case ') != -1
2195 or cleansed_line.find('default:') != -1)
2196 and cleansed_line.find('break;') != -1)):
2197 error(filename, line_number, 'whitespace/newline', 4,
2198 'More than one command on the same line')
2200 if cleansed_line.strip().endswith('||') or cleansed_line.strip().endswith('&&'):
2201 error(filename, line_number, 'whitespace/operators', 4,
2202 'Boolean expressions that span multiple lines should have their '
2203 'operators on the left side of the line instead of the right side.')
2205 # Some more style checks
2206 check_namespace_indentation(filename, clean_lines, line_number, file_extension, error)
2207 check_using_std(filename, clean_lines, line_number, error)
2208 check_max_min_macros(filename, clean_lines, line_number, error)
2209 check_switch_indentation(filename, clean_lines, line_number, error)
2210 check_braces(filename, clean_lines, line_number, error)
2211 check_exit_statement_simplifications(filename, clean_lines, line_number, error)
2212 check_spacing(filename, clean_lines, line_number, error)
2213 check_check(filename, clean_lines, line_number, error)
2214 check_for_comparisons_to_zero(filename, clean_lines, line_number, error)
2215 check_for_null(filename, clean_lines, line_number, error)
2218 _RE_PATTERN_INCLUDE_NEW_STYLE = re.compile(r'#include +"[^/]+\.h"')
2219 _RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
2220 # Matches the first component of a filename delimited by -s and _s. That is:
2221 # _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
2222 # _RE_FIRST_COMPONENT.match('foo.cpp').group(0) == 'foo'
2223 # _RE_FIRST_COMPONENT.match('foo-bar_baz.cpp').group(0) == 'foo'
2224 # _RE_FIRST_COMPONENT.match('foo_bar-baz.cpp').group(0) == 'foo'
2225 _RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
2228 def _drop_common_suffixes(filename):
2229 """Drops common suffixes like _test.cpp or -inl.h from filename.
2232 >>> _drop_common_suffixes('foo/foo-inl.h')
2234 >>> _drop_common_suffixes('foo/bar/foo.cpp')
2236 >>> _drop_common_suffixes('foo/foo_internal.h')
2238 >>> _drop_common_suffixes('foo/foo_unusualinternal.h')
2239 'foo/foo_unusualinternal'
2242 filename: The input filename.
2245 The filename with the common suffix removed.
2247 for suffix in ('test.cpp', 'regtest.cpp', 'unittest.cpp',
2248 'inl.h', 'impl.h', 'internal.h'):
2249 if (filename.endswith(suffix) and len(filename) > len(suffix)
2250 and filename[-len(suffix) - 1] in ('-', '_')):
2251 return filename[:-len(suffix) - 1]
2252 return os.path.splitext(filename)[0]
2255 def _is_test_filename(filename):
2256 """Determines if the given filename has a suffix that identifies it as a test.
2259 filename: The input filename.
2262 True if 'filename' looks like a test, False otherwise.
2264 if (filename.endswith('_test.cpp')
2265 or filename.endswith('_unittest.cpp')
2266 or filename.endswith('_regtest.cpp')):
2271 def _classify_include(filename, include, is_system, include_state):
2272 """Figures out what kind of header 'include' is.
2275 filename: The current file cpp_style is running over.
2276 include: The path to a #included file.
2277 is_system: True if the #include used <> rather than "".
2278 include_state: An _IncludeState instance in which the headers are inserted.
2281 One of the _XXX_HEADER constants.
2284 >>> _classify_include('foo.cpp', 'config.h', False)
2286 >>> _classify_include('foo.cpp', 'foo.h', False)
2288 >>> _classify_include('foo.cpp', 'bar.h', False)
2292 # If it is a system header we know it is classified as _OTHER_HEADER.
2294 return _OTHER_HEADER
2296 # If the include is named config.h then this is WebCore/config.h.
2297 if include == "config.h":
2298 return _CONFIG_HEADER
2300 # There cannot be primary includes in header files themselves. Only an
2301 # include exactly matches the header filename will be is flagged as
2302 # primary, so that it triggers the "don't include yourself" check.
2303 if filename.endswith('.h') and filename != include:
2304 return _OTHER_HEADER;
2306 # If the target file basename starts with the include we're checking
2307 # then we consider it the primary header.
2308 target_base = FileInfo(filename).base_name()
2309 include_base = FileInfo(include).base_name()
2311 # If we haven't encountered a primary header, then be lenient in checking.
2312 if not include_state.visited_primary_section() and target_base.startswith(include_base):
2313 return _PRIMARY_HEADER
2314 # If we already encountered a primary header, perform a strict comparison.
2315 # In case the two filename bases are the same then the above lenient check
2316 # probably was a false positive.
2317 elif include_state.visited_primary_section() and target_base == include_base:
2318 return _PRIMARY_HEADER
2320 return _OTHER_HEADER
2324 def check_include_line(filename, clean_lines, line_number, include_state, error):
2325 """Check rules that are applicable to #include lines.
2327 Strings on #include lines are NOT removed from elided line, to make
2328 certain tasks easier. However, to prevent false positives, checks
2329 applicable to #include lines in CheckLanguage must be put here.
2332 filename: The name of the current file.
2333 clean_lines: A CleansedLines instance containing the file.
2334 line_number: The number of the line to check.
2335 include_state: An _IncludeState instance in which the headers are inserted.
2336 error: The function to call with any errors found.
2339 line = clean_lines.lines[line_number]
2341 matched = _RE_PATTERN_INCLUDE.search(line)
2345 include = matched.group(2)
2346 is_system = (matched.group(1) == '<')
2348 # Look for any of the stream classes that are part of standard C++.
2349 if match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
2350 # Many unit tests use cout, so we exempt them.
2351 if not _is_test_filename(filename):
2352 error(filename, line_number, 'readability/streams', 3,
2353 'Streams are highly discouraged.')
2355 # Look for specific includes to fix.
2356 if include.startswith('wtf/') and not is_system:
2357 error(filename, line_number, 'build/include', 4,
2358 'wtf includes should be <wtf/file.h> instead of "wtf/file.h".')
2360 duplicate_header = include in include_state
2361 if duplicate_header:
2362 error(filename, line_number, 'build/include', 4,
2363 '"%s" already included at %s:%s' %
2364 (include, filename, include_state[include]))
2366 include_state[include] = line_number
2368 header_type = _classify_include(filename, include, is_system, include_state)
2369 include_state.header_types[line_number] = header_type
2371 # Only proceed if this isn't a duplicate header.
2372 if duplicate_header:
2375 # We want to ensure that headers appear in the right order:
2376 # 1) for implementation files: config.h, primary header, blank line, alphabetically sorted
2377 # 2) for header files: alphabetically sorted
2378 # The include_state object keeps track of the last type seen
2379 # and complains if the header types are out of order or missing.
2380 error_message = include_state.check_next_include_order(header_type, filename.endswith('.h'))
2382 # Check to make sure we have a blank line after primary header.
2383 if not error_message and header_type == _PRIMARY_HEADER:
2384 next_line = clean_lines.raw_lines[line_number + 1]
2385 if not is_blank_line(next_line):
2386 error(filename, line_number, 'build/include_order', 4,
2387 'You should add a blank line after implementation file\'s own header.')
2389 # Check to make sure all headers besides config.h and the primary header are
2390 # alphabetically sorted.
2391 if not error_message and header_type == _OTHER_HEADER:
2392 previous_line_number = line_number - 1;
2393 previous_line = clean_lines.lines[previous_line_number]
2394 previous_match = _RE_PATTERN_INCLUDE.search(previous_line)
2395 while (not previous_match and previous_line_number > 0
2396 and not search(r'\A(#if|#ifdef|#ifndef|#else|#elif|#endif)', previous_line)):
2397 previous_line_number -= 1;
2398 previous_line = clean_lines.lines[previous_line_number]
2399 previous_match = _RE_PATTERN_INCLUDE.search(previous_line)
2401 previous_header_type = include_state.header_types[previous_line_number]
2402 if previous_header_type == _OTHER_HEADER and previous_line.strip() > line.strip():
2403 error(filename, line_number, 'build/include_order', 4,
2404 'Alphabetical sorting problem.')
2407 if filename.endswith('.h'):
2408 error(filename, line_number, 'build/include_order', 4,
2409 '%s Should be: alphabetically sorted.' %
2412 error(filename, line_number, 'build/include_order', 4,
2413 '%s Should be: config.h, primary header, blank line, and then alphabetically sorted.' %
2417 def check_language(filename, clean_lines, line_number, file_extension, include_state,
2419 """Checks rules from the 'C++ language rules' section of cppguide.html.
2421 Some of these rules are hard to test (function overloading, using
2422 uint32 inappropriately), but we do the best we can.
2425 filename: The name of the current file.
2426 clean_lines: A CleansedLines instance containing the file.
2427 line_number: The number of the line to check.
2428 file_extension: The extension (without the dot) of the filename.
2429 include_state: An _IncludeState instance in which the headers are inserted.
2430 error: The function to call with any errors found.
2432 # If the line is empty or consists of entirely a comment, no need to
2434 line = clean_lines.elided[line_number]
2438 matched = _RE_PATTERN_INCLUDE.search(line)
2440 check_include_line(filename, clean_lines, line_number, include_state, error)
2443 # FIXME: figure out if they're using default arguments in fn proto.
2445 # Check to see if they're using an conversion function cast.
2446 # I just try to capture the most common basic types, though there are more.
2447 # Parameterless conversion functions, such as bool(), are allowed as they are
2448 # probably a member operator declaration or default constructor.
2450 r'\b(int|float|double|bool|char|int32|uint32|int64|uint64)\([^)]', line)
2452 # gMock methods are defined using some variant of MOCK_METHODx(name, type)
2453 # where type may be float(), int(string), etc. Without context they are
2454 # virtually indistinguishable from int(x) casts.
2455 if not match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line):
2456 error(filename, line_number, 'readability/casting', 4,
2457 'Using deprecated casting style. '
2458 'Use static_cast<%s>(...) instead' %
2461 check_c_style_cast(filename, line_number, line, clean_lines.raw_lines[line_number],
2463 r'\((int|float|double|bool|char|u?int(16|32|64))\)',
2465 # This doesn't catch all cases. Consider (const char * const)"hello".
2466 check_c_style_cast(filename, line_number, line, clean_lines.raw_lines[line_number],
2467 'reinterpret_cast', r'\((\w+\s?\*+\s?)\)', error)
2469 # In addition, we look for people taking the address of a cast. This
2470 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
2471 # point where you think.
2473 r'(&\([^)]+\)[\w(])|(&(static|dynamic|reinterpret)_cast\b)', line):
2474 error(filename, line_number, 'runtime/casting', 4,
2475 ('Are you taking an address of a cast? '
2476 'This is dangerous: could be a temp var. '
2477 'Take the address before doing the cast, rather than after'))
2479 # Check for people declaring static/global STL strings at the top level.
2480 # This is dangerous because the C++ language does not guarantee that
2481 # globals with constructors are initialized before the first access.
2483 r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
2485 # Make sure it's not a function.
2486 # Function template specialization looks like: "string foo<Type>(...".
2487 # Class template definitions look like: "string Foo<Type>::Method(...".
2488 if matched and not match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)?\s*\(([^"]|$)',
2490 error(filename, line_number, 'runtime/string', 4,
2491 'For a static/global string constant, use a C style string instead: '
2493 (matched.group(1), matched.group(2)))
2495 # Check that we're not using RTTI outside of testing code.
2496 if search(r'\bdynamic_cast<', line) and not _is_test_filename(filename):
2497 error(filename, line_number, 'runtime/rtti', 5,
2498 'Do not use dynamic_cast<>. If you need to cast within a class '
2499 "hierarchy, use static_cast<> to upcast. Google doesn't support "
2502 if search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
2503 error(filename, line_number, 'runtime/init', 4,
2504 'You seem to be initializing a member variable with itself.')
2506 if file_extension == 'h':
2507 # FIXME: check that 1-arg constructors are explicit.
2508 # How to tell it's a constructor?
2509 # (handled in check_for_non_standard_constructs for now)
2512 # Check if people are using the verboten C basic types. The only exception
2513 # we regularly allow is "unsigned short port" for port.
2514 if search(r'\bshort port\b', line):
2515 if not search(r'\bunsigned short port\b', line):
2516 error(filename, line_number, 'runtime/int', 4,
2517 'Use "unsigned short" for ports, not "short"')
2519 # When snprintf is used, the second argument shouldn't be a literal.
2520 matched = search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
2522 error(filename, line_number, 'runtime/printf', 3,
2523 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
2524 'to snprintf.' % (matched.group(1), matched.group(2)))
2526 # Check if some verboten C functions are being used.
2527 if search(r'\bsprintf\b', line):
2528 error(filename, line_number, 'runtime/printf', 5,
2529 'Never use sprintf. Use snprintf instead.')
2530 matched = search(r'\b(strcpy|strcat)\b', line)
2532 error(filename, line_number, 'runtime/printf', 4,
2533 'Almost always, snprintf is better than %s' % matched.group(1))
2535 if search(r'\bsscanf\b', line):
2536 error(filename, line_number, 'runtime/printf', 1,
2537 'sscanf can be ok, but is slow and can overflow buffers.')
2539 # Check for suspicious usage of "if" like
2541 if search(r'\}\s*if\s*\(', line):
2542 error(filename, line_number, 'readability/braces', 4,
2543 'Did you mean "else if"? If not, start a new line for "if".')
2545 # Check for potential format string bugs like printf(foo).
2546 # We constrain the pattern not to pick things like DocidForPrintf(foo).
2547 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
2548 matched = re.search(r'\b((?:string)?printf)\s*\(([\w.\->()]+)\)', line, re.I)
2550 error(filename, line_number, 'runtime/printf', 4,
2551 'Potential format string bug. Do %s("%%s", %s) instead.'
2552 % (matched.group(1), matched.group(2)))
2554 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
2555 matched = search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
2556 if matched and not match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", matched.group(2)):
2557 error(filename, line_number, 'runtime/memset', 4,
2558 'Did you mean "memset(%s, 0, %s)"?'
2559 % (matched.group(1), matched.group(2)))
2561 # Detect variable-length arrays.
2562 matched = match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
2563 if (matched and matched.group(2) != 'return' and matched.group(2) != 'delete' and
2564 matched.group(3).find(']') == -1):
2565 # Split the size using space and arithmetic operators as delimiters.
2566 # If any of the resulting tokens are not compile time constants then
2568 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', matched.group(3))
2576 if search(r'sizeof\(.+\)', tok):
2578 if search(r'arraysize\(\w+\)', tok):
2581 tok = tok.lstrip('(')
2582 tok = tok.rstrip(')')
2585 if match(r'\d+', tok):
2587 if match(r'0[xX][0-9a-fA-F]+', tok):
2589 if match(r'k[A-Z0-9]\w*', tok):
2591 if match(r'(.+::)?k[A-Z0-9]\w*', tok):
2593 if match(r'(.+::)?[A-Z][A-Z0-9_]*', tok):
2595 # A catch all for tricky sizeof cases, including 'sizeof expression',
2596 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
2597 # requires skipping the next token becasue we split on ' ' and '*'.
2598 if tok.startswith('sizeof'):
2604 error(filename, line_number, 'runtime/arrays', 1,
2605 'Do not use variable-length arrays. Use an appropriately named '
2606 "('k' followed by CamelCase) compile-time constant for the size.")
2608 # Check for use of unnamed namespaces in header files. Registration
2609 # macros are typically OK, so we allow use of "namespace {" on lines
2610 # that end with backslashes.
2611 if (file_extension == 'h'
2612 and search(r'\bnamespace\s*{', line)
2613 and line[-1] != '\\'):
2614 error(filename, line_number, 'build/namespaces', 4,
2615 'Do not use unnamed namespaces in header files. See '
2616 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
2617 ' for more information.')
2620 def check_c_style_cast(filename, line_number, line, raw_line, cast_type, pattern,
2622 """Checks for a C-style cast by looking for the pattern.
2624 This also handles sizeof(type) warnings, due to similarity of content.
2627 filename: The name of the current file.
2628 line_number: The number of the line to check.
2629 line: The line of code to check.
2630 raw_line: The raw line of code to check, with comments.
2631 cast_type: The string for the C++ cast to recommend. This is either
2632 reinterpret_cast or static_cast, depending.
2633 pattern: The regular expression used to find C-style casts.
2634 error: The function to call with any errors found.
2636 matched = search(pattern, line)
2641 sizeof_match = match(r'.*sizeof\s*$', line[0:matched.start(1) - 1])
2643 error(filename, line_number, 'runtime/sizeof', 1,
2644 'Using sizeof(type). Use sizeof(varname) instead if possible')
2647 remainder = line[matched.end(0):]
2649 # The close paren is for function pointers as arguments to a function.
2650 # eg, void foo(void (*bar)(int));
2651 # The semicolon check is a more basic function check; also possibly a
2652 # function pointer typedef.
2653 # eg, void foo(int); or void foo(int) const;
2654 # The equals check is for function pointer assignment.
2655 # eg, void *(*foo)(int) = ...
2657 # Right now, this will only catch cases where there's a single argument, and
2658 # it's unnamed. It should probably be expanded to check for multiple
2659 # arguments with some unnamed.
2660 function_match = match(r'\s*(\)|=|(const)?\s*(;|\{|throw\(\)))', remainder)
2662 if (not function_match.group(3)
2663 or function_match.group(3) == ';'
2664 or raw_line.find('/*') < 0):
2665 error(filename, line_number, 'readability/function', 3,
2666 'All parameters should be named in a function')
2669 # At this point, all that should be left is actual casts.
2670 error(filename, line_number, 'readability/casting', 4,
2671 'Using C-style cast. Use %s<%s>(...) instead' %
2672 (cast_type, matched.group(1)))
2675 _HEADERS_CONTAINING_TEMPLATES = (
2676 ('<deque>', ('deque',)),
2677 ('<functional>', ('unary_function', 'binary_function',
2678 'plus', 'minus', 'multiplies', 'divides', 'modulus',
2680 'equal_to', 'not_equal_to', 'greater', 'less',
2681 'greater_equal', 'less_equal',
2682 'logical_and', 'logical_or', 'logical_not',
2683 'unary_negate', 'not1', 'binary_negate', 'not2',
2684 'bind1st', 'bind2nd',
2685 'pointer_to_unary_function',
2686 'pointer_to_binary_function',
2688 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
2690 'const_mem_fun_t', 'const_mem_fun1_t',
2691 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
2694 ('<limits>', ('numeric_limits',)),
2695 ('<list>', ('list',)),
2696 ('<map>', ('map', 'multimap',)),
2697 ('<memory>', ('allocator',)),
2698 ('<queue>', ('queue', 'priority_queue',)),
2699 ('<set>', ('set', 'multiset',)),
2700 ('<stack>', ('stack',)),
2701 ('<string>', ('char_traits', 'basic_string',)),
2702 ('<utility>', ('pair',)),
2703 ('<vector>', ('vector',)),
2706 # Note: std::hash is their hash, ::hash is our hash
2707 ('<hash_map>', ('hash_map', 'hash_multimap',)),
2708 ('<hash_set>', ('hash_set', 'hash_multiset',)),
2709 ('<slist>', ('slist',)),
2712 _HEADERS_ACCEPTED_BUT_NOT_PROMOTED = {
2713 # We can trust with reasonable confidence that map gives us pair<>, too.
2714 'pair<>': ('map', 'multimap', 'hash_map', 'hash_multimap')
2717 _RE_PATTERN_STRING = re.compile(r'\bstring\b')
2719 _re_pattern_algorithm_header = []
2720 for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
2722 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
2724 _re_pattern_algorithm_header.append(
2725 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
2729 _re_pattern_templates = []
2730 for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
2731 for _template in _templates:
2732 _re_pattern_templates.append(
2733 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
2738 def files_belong_to_same_module(filename_cpp, filename_h):
2739 """Check if these two filenames belong to the same module.
2741 The concept of a 'module' here is a as follows:
2742 foo.h, foo-inl.h, foo.cpp, foo_test.cpp and foo_unittest.cpp belong to the
2743 same 'module' if they are in the same directory.
2744 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
2745 to belong to the same module here.
2747 If the filename_cpp contains a longer path than the filename_h, for example,
2748 '/absolute/path/to/base/sysinfo.cpp', and this file would include
2749 'base/sysinfo.h', this function also produces the prefix needed to open the
2750 header. This is used by the caller of this function to more robustly open the
2751 header file. We don't have access to the real include paths in this context,
2752 so we need this guesswork here.
2754 Known bugs: tools/base/bar.cpp and base/bar.h belong to the same module
2755 according to this implementation. Because of this, this function gives
2756 some false positives. This should be sufficiently rare in practice.
2759 filename_cpp: is the path for the .cpp file
2760 filename_h: is the path for the header path
2763 Tuple with a bool and a string:
2764 bool: True if filename_cpp and filename_h belong to the same module.
2765 string: the additional prefix needed to open the header file.
2768 if not filename_cpp.endswith('.cpp'):
2770 filename_cpp = filename_cpp[:-len('.cpp')]
2771 if filename_cpp.endswith('_unittest'):
2772 filename_cpp = filename_cpp[:-len('_unittest')]
2773 elif filename_cpp.endswith('_test'):
2774 filename_cpp = filename_cpp[:-len('_test')]
2775 filename_cpp = filename_cpp.replace('/public/', '/')
2776 filename_cpp = filename_cpp.replace('/internal/', '/')
2778 if not filename_h.endswith('.h'):
2780 filename_h = filename_h[:-len('.h')]
2781 if filename_h.endswith('-inl'):
2782 filename_h = filename_h[:-len('-inl')]
2783 filename_h = filename_h.replace('/public/', '/')
2784 filename_h = filename_h.replace('/internal/', '/')
2786 files_belong_to_same_module = filename_cpp.endswith(filename_h)
2788 if files_belong_to_same_module:
2789 common_path = filename_cpp[:-len(filename_h)]
2790 return files_belong_to_same_module, common_path
2793 def update_include_state(filename, include_state, io=codecs):
2794 """Fill up the include_state with new includes found from the file.
2797 filename: the name of the header to read.
2798 include_state: an _IncludeState instance in which the headers are inserted.
2799 io: The io factory to use to read the file. Provided for testability.
2802 True if a header was succesfully added. False otherwise.
2806 header_file = io.open(filename, 'r', 'utf8', 'replace')
2810 for line in header_file:
2812 clean_line = cleanse_comments(line)
2813 matched = _RE_PATTERN_INCLUDE.search(clean_line)
2815 include = matched.group(2)
2816 # The value formatting is cute, but not really used right now.
2817 # What matters here is that the key is in include_state.
2818 include_state.setdefault(include, '%s:%d' % (filename, line_number))
2822 def check_for_include_what_you_use(filename, clean_lines, include_state, error,
2824 """Reports for missing stl includes.
2826 This function will output warnings to make sure you are including the headers
2827 necessary for the stl containers and functions that you use. We only give one
2828 reason to include a header. For example, if you use both equal_to<> and
2829 less<> in a .h file, only one (the latter in the file) of these will be
2830 reported as a reason to include the <functional>.
2833 filename: The name of the current file.
2834 clean_lines: A CleansedLines instance containing the file.
2835 include_state: An _IncludeState instance.
2836 error: The function to call with any errors found.
2837 io: The IO factory to use to read the header file. Provided for unittest
2840 required = {} # A map of header name to line_number and the template entity.
2841 # Example of required: { '<functional>': (1219, 'less<>') }
2843 for line_number in xrange(clean_lines.num_lines()):
2844 line = clean_lines.elided[line_number]
2845 if not line or line[0] == '#':
2848 # String is special -- it is a non-templatized type in STL.
2849 if _RE_PATTERN_STRING.search(line):
2850 required['<string>'] = (line_number, 'string')
2852 for pattern, template, header in _re_pattern_algorithm_header:
2853 if pattern.search(line):
2854 required[header] = (line_number, template)
2856 # The following function is just a speed up, no semantics are changed.
2857 if not '<' in line: # Reduces the cpu time usage by skipping lines.
2860 for pattern, template, header in _re_pattern_templates:
2861 if pattern.search(line):
2862 required[header] = (line_number, template)
2864 # The policy is that if you #include something in foo.h you don't need to
2865 # include it again in foo.cpp. Here, we will look at possible includes.
2866 # Let's copy the include_state so it is only messed up within this function.
2867 include_state = include_state.copy()
2869 # Did we find the header for this file (if any) and succesfully load it?
2870 header_found = False
2872 # Use the absolute path so that matching works properly.
2873 abs_filename = os.path.abspath(filename)
2875 # For Emacs's flymake.
2876 # If cpp_style is invoked from Emacs's flymake, a temporary file is generated
2877 # by flymake and that file name might end with '_flymake.cpp'. In that case,
2878 # restore original file name here so that the corresponding header file can be
2880 # e.g. If the file name is 'foo_flymake.cpp', we should search for 'foo.h'
2881 # instead of 'foo_flymake.h'
2882 emacs_flymake_suffix = '_flymake.cpp'
2883 if abs_filename.endswith(emacs_flymake_suffix):
2884 abs_filename = abs_filename[:-len(emacs_flymake_suffix)] + '.cpp'
2886 # include_state is modified during iteration, so we iterate over a copy of
2888 for header in include_state.keys(): #NOLINT
2889 (same_module, common_path) = files_belong_to_same_module(abs_filename, header)
2890 fullpath = common_path + header
2891 if same_module and update_include_state(fullpath, include_state, io):
2894 # If we can't find the header file for a .cpp, assume it's because we don't
2895 # know where to look. In that case we'll give up as we're not sure they
2896 # didn't include it in the .h file.
2897 # FIXME: Do a better job of finding .h files so we are confident that
2898 # not having the .h file means there isn't one.
2899 if filename.endswith('.cpp') and not header_found:
2902 # All the lines have been processed, report the errors found.
2903 for required_header_unstripped in required:
2904 template = required[required_header_unstripped][1]
2905 if template in _HEADERS_ACCEPTED_BUT_NOT_PROMOTED:
2906 headers = _HEADERS_ACCEPTED_BUT_NOT_PROMOTED[template]
2907 if [True for header in headers if header in include_state]:
2909 if required_header_unstripped.strip('<>"') not in include_state:
2910 error(filename, required[required_header_unstripped][0],
2911 'build/include_what_you_use', 4,
2912 'Add #include ' + required_header_unstripped + ' for ' + template)
2915 def process_line(filename, file_extension,
2916 clean_lines, line, include_state, function_state,
2917 class_state, error):
2918 """Processes a single line in the file.
2921 filename: Filename of the file that is being processed.
2922 file_extension: The extension (dot not included) of the file.
2923 clean_lines: An array of strings, each representing a line of the file,
2924 with comments stripped.
2925 line: Number of line being processed.
2926 include_state: An _IncludeState instance in which the headers are inserted.
2927 function_state: A _FunctionState instance which counts function lines, etc.
2928 class_state: A _ClassState instance which maintains information about
2929 the current stack of nested class declarations being parsed.
2930 error: A callable to which errors are reported, which takes 4 arguments:
2931 filename, line number, error level, and message
2934 raw_lines = clean_lines.raw_lines
2935 check_for_function_lengths(filename, clean_lines, line, function_state, error)
2936 if search(r'\bNOLINT\b', raw_lines[line]): # ignore nolint lines
2938 check_for_multiline_comments_and_strings(filename, clean_lines, line, error)
2939 check_style(filename, clean_lines, line, file_extension, error)
2940 check_language(filename, clean_lines, line, file_extension, include_state,
2942 check_for_non_standard_constructs(filename, clean_lines, line,
2944 check_posix_threading(filename, clean_lines, line, error)
2945 check_invalid_increment(filename, clean_lines, line, error)
2948 def process_file_data(filename, file_extension, lines, error):
2949 """Performs lint checks and reports any errors to the given error function.
2952 filename: Filename of the file that is being processed.
2953 file_extension: The extension (dot not included) of the file.
2954 lines: An array of strings, each representing a line of the file, with the
2955 last element being empty if the file is termined with a newline.
2956 error: A callable to which errors are reported, which takes 4 arguments:
2958 lines = (['// marker so line numbers and indices both start at 1'] + lines +
2959 ['// marker so line numbers end in a known way'])
2961 include_state = _IncludeState()
2962 function_state = _FunctionState()
2963 class_state = _ClassState()
2965 check_for_copyright(filename, lines, error)
2967 if file_extension == 'h':
2968 check_for_header_guard(filename, lines, error)
2970 remove_multi_line_comments(filename, lines, error)
2971 clean_lines = CleansedLines(lines)
2972 for line in xrange(clean_lines.num_lines()):
2973 process_line(filename, file_extension, clean_lines, line,
2974 include_state, function_state, class_state, error)
2975 class_state.check_finished(filename, error)
2977 check_for_include_what_you_use(filename, clean_lines, include_state, error)
2979 # We check here rather than inside process_line so that we see raw
2980 # lines rather than "cleaned" lines.
2981 check_for_unicode_replacement_characters(filename, lines, error)
2983 check_for_new_line_at_eof(filename, lines, error)
2986 def process_file(filename, error=error):
2987 """Performs cpp_style on a single file.
2990 filename: The name of the file to parse.
2991 error: The function to call with any errors found.
2994 # Support the UNIX convention of using "-" for stdin. Note that
2995 # we are not opening the file with universal newline support
2996 # (which codecs doesn't support anyway), so the resulting lines do
2997 # contain trailing '\r' characters if we are reading a file that
2999 # If after the split a trailing '\r' is present, it is removed
3000 # below. If it is not expected to be present (i.e. os.linesep !=
3001 # '\r\n' as in Windows), a warning is issued below if this file
3005 lines = codecs.StreamReaderWriter(sys.stdin,
3006 codecs.getreader('utf8'),
3007 codecs.getwriter('utf8'),
3008 'replace').read().split('\n')
3010 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
3012 carriage_return_found = False
3013 # Remove trailing '\r'.
3014 for line_number in range(len(lines)):
3015 if lines[line_number].endswith('\r'):
3016 lines[line_number] = lines[line_number].rstrip('\r')
3017 carriage_return_found = True
3021 "Skipping input '%s': Can't open for reading\n" % filename)
3024 # Note, if no dot is found, this will give the entire filename as the ext.
3025 file_extension = filename[filename.rfind('.') + 1:]
3027 # When reading from stdin, the extension is unknown, so no cpp_style tests
3028 # should rely on the extension.
3029 if (filename != '-' and file_extension != 'h' and file_extension != 'cpp'
3030 and file_extension != 'c'):
3031 sys.stderr.write('Ignoring %s; not a .cpp, .c or .h file\n' % filename)
3033 process_file_data(filename, file_extension, lines, error)
3034 if carriage_return_found and os.linesep != '\r\n':
3035 # Use 0 for line_number since outputing only one error for potentially
3037 error(filename, 0, 'whitespace/newline', 1,
3038 'One or more unexpected \\r (^M) found;'
3039 'better to use only a \\n')
3042 def print_usage(message):
3043 """Prints a brief usage string and exits, optionally with an error message.
3046 message: The optional error message.
3048 sys.stderr.write(_USAGE)
3050 sys.exit('\nFATAL ERROR: ' + message)
3055 def print_categories():
3056 """Prints a list of all the error-categories used by error messages.
3058 These are the categories used to filter messages via --filter.
3060 sys.stderr.write(_ERROR_CATEGORIES)
3064 def parse_arguments(args, additional_flags=[]):
3065 """Parses the command line arguments.
3067 This may set the output format and verbosity level as side-effects.
3070 args: The command line arguments:
3071 additional_flags: A list of strings which specifies flags we allow.
3074 A tuple of (filenames, flags)
3076 filenames: The list of filenames to lint.
3077 flags: The dict of the flag names and the flag values.
3079 flags = ['help', 'output=', 'verbose=', 'filter='] + additional_flags
3080 additional_flag_values = {}
3082 (opts, filenames) = getopt.getopt(args, '', flags)
3083 except getopt.GetoptError:
3084 print_usage('Invalid arguments.')
3086 verbosity = _verbose_level()
3087 output_format = _output_format()
3090 for (opt, val) in opts:
3093 elif opt == '--output':
3094 if not val in ('emacs', 'vs7'):
3095 print_usage('The only allowed output formats are emacs and vs7.')
3097 elif opt == '--verbose':
3098 verbosity = int(val)
3099 elif opt == '--filter':
3104 additional_flag_values[opt] = val
3106 _set_output_format(output_format)
3107 _set_verbose_level(verbosity)
3108 _set_filters(filters)
3110 return (filenames, additional_flag_values)
3113 def use_webkit_styles():
3114 """Disables some features which are not suitable for WebKit."""
3115 # FIXME: For filters we will never want to have, remove them.
3116 # For filters we want to have similar functionalities,
3117 # modify the implementation and enable them.
3118 global _DEFAULT_FILTERS
3119 _DEFAULT_FILTERS = [
3120 '-whitespace/end_of_line',
3121 '-whitespace/comments',
3122 '-whitespace/blank_line',
3123 '-runtime/explicit', # explicit
3124 '-runtime/virtual', # virtual dtor
3126 '-runtime/threadsafe_fn',
3128 '-build/include_what_you_use', # <string> for std::string
3130 '-readability/multiline_comment',
3131 '-readability/braces', # int foo() {};
3132 '-readability/fn_size',
3133 '-build/storage_class', # const static
3134 '-build/endif_comment',
3135 '-whitespace/labels',
3136 '-runtime/arrays', # variable length array
3137 '-build/header_guard',
3138 '-readability/casting',
3139 '-readability/function',
3147 '''********************* WARNING WARNING WARNING *********************
3149 This tool is in the process of development and may give inaccurate
3150 results at present. Please file bugs (and/or patches) for things
3151 that you notice that it flags incorrectly.
3153 ********************* WARNING WARNING WARNING *********************
3159 (filenames, flags) = parse_arguments(sys.argv[1:])
3161 print_usage('No files were specified.')
3163 # Change stderr to write with replacement characters so we don't die
3164 # if we try to print something containing non-ASCII characters.
3165 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
3166 codecs.getreader('utf8'),
3167 codecs.getwriter('utf8'),
3170 _cpp_style_state.reset_error_count()
3171 for filename in filenames:
3172 process_file(filename)
3173 sys.stderr.write('Total errors found: %d\n' % _cpp_style_state.error_count)
3174 sys.exit(_cpp_style_state.error_count > 0)
3177 if __name__ == '__main__':