tomwalters@0: #!/usr/bin/python tomwalters@0: # tomwalters@0: # Copyright (c) 2009 Google Inc. All rights reserved. tomwalters@0: # tomwalters@0: # Redistribution and use in source and binary forms, with or without tomwalters@0: # modification, are permitted provided that the following conditions are tomwalters@0: # met: tomwalters@0: # tomwalters@0: # * Redistributions of source code must retain the above copyright tomwalters@0: # notice, this list of conditions and the following disclaimer. tomwalters@0: # * Redistributions in binary form must reproduce the above tomwalters@0: # copyright notice, this list of conditions and the following disclaimer tomwalters@0: # in the documentation and/or other materials provided with the tomwalters@0: # distribution. tomwalters@0: # * Neither the name of Google Inc. nor the names of its tomwalters@0: # contributors may be used to endorse or promote products derived from tomwalters@0: # this software without specific prior written permission. tomwalters@0: # tomwalters@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS tomwalters@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT tomwalters@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR tomwalters@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT tomwalters@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, tomwalters@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT tomwalters@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, tomwalters@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY tomwalters@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT tomwalters@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE tomwalters@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. tomwalters@0: tomwalters@0: # Here are some issues that I've had people identify in my code during reviews, tomwalters@0: # that I think are possible to flag automatically in a lint tool. If these were tomwalters@0: # caught by lint, it would save time both for myself and that of my reviewers. tomwalters@0: # Most likely, some of these are beyond the scope of the current lint framework, tomwalters@0: # but I think it is valuable to retain these wish-list items even if they cannot tomwalters@0: # be immediately implemented. tomwalters@0: # tomwalters@0: # Suggestions tomwalters@0: # ----------- tomwalters@0: # - Check for no 'explicit' for multi-arg ctor tomwalters@0: # - Check for boolean assign RHS in parens tomwalters@0: # - Check for ctor initializer-list colon position and spacing tomwalters@0: # - Check that if there's a ctor, there should be a dtor tomwalters@0: # - Check accessors that return non-pointer member variables are tomwalters@0: # declared const tomwalters@0: # - Check accessors that return non-const pointer member vars are tomwalters@0: # *not* declared const tomwalters@0: # - Check for using public includes for testing tomwalters@0: # - Check for spaces between brackets in one-line inline method tomwalters@0: # - Check for no assert() tomwalters@0: # - Check for spaces surrounding operators tomwalters@0: # - Check for 0 in pointer context (should be NULL) tomwalters@0: # - Check for 0 in char context (should be '\0') tomwalters@0: # - Check for camel-case method name conventions for methods tomwalters@0: # that are not simple inline getters and setters tomwalters@0: # - Check that base classes have virtual destructors tomwalters@0: # put " // namespace" after } that closes a namespace, with tomwalters@0: # namespace's name after 'namespace' if it is named. tomwalters@0: # - Do not indent namespace contents tomwalters@0: # - Avoid inlining non-trivial constructors in header files tomwalters@0: # include base/basictypes.h if DISALLOW_EVIL_CONSTRUCTORS is used tomwalters@0: # - Check for old-school (void) cast for call-sites of functions tomwalters@0: # ignored return value tomwalters@0: # - Check gUnit usage of anonymous namespace tomwalters@0: # - Check for class declaration order (typedefs, consts, enums, tomwalters@0: # ctor(s?), dtor, friend declarations, methods, member vars) tomwalters@0: # tomwalters@0: tomwalters@0: """Does google-lint on c++ files. tomwalters@0: tomwalters@0: The goal of this script is to identify places in the code that *may* tomwalters@0: be in non-compliance with google style. It does not attempt to fix tomwalters@0: up these problems -- the point is to educate. It does also not tomwalters@0: attempt to find all problems, or to ensure that everything it does tomwalters@0: find is legitimately a problem. tomwalters@0: tomwalters@0: In particular, we can get very confused by /* and // inside strings! tomwalters@0: We do a small hack, which is to ignore //'s with "'s after them on the tomwalters@0: same line, but it is far from perfect (in either direction). tomwalters@0: """ tomwalters@0: tomwalters@0: import codecs tomwalters@0: import getopt tomwalters@0: import math # for log tomwalters@0: import os tomwalters@0: import re tomwalters@0: import sre_compile tomwalters@0: import string tomwalters@0: import sys tomwalters@0: import unicodedata tomwalters@0: tomwalters@0: tomwalters@0: _USAGE = """ tomwalters@0: Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] tomwalters@0: [--counting=total|toplevel|detailed] tomwalters@0: [file] ... tomwalters@0: tomwalters@0: The style guidelines this tries to follow are those in tomwalters@0: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml tomwalters@0: tomwalters@0: Every problem is given a confidence score from 1-5, with 5 meaning we are tomwalters@0: certain of the problem, and 1 meaning it could be a legitimate construct. tomwalters@0: This will miss some errors, and is not a substitute for a code review. tomwalters@0: tomwalters@0: To prevent specific lines from being linted, add a '// NOLINT' comment to the tomwalters@0: end of the line. tomwalters@0: tomwalters@0: The files passed in will be linted; at least one file must be provided. tomwalters@0: Linted extensions are .cc, .cpp, and .h. Other file types will be ignored. tomwalters@0: tomwalters@0: Flags: tomwalters@0: tomwalters@0: output=vs7 tomwalters@0: By default, the output is formatted to ease emacs parsing. Visual Studio tomwalters@0: compatible output (vs7) may also be used. Other formats are unsupported. tomwalters@0: tomwalters@0: verbose=# tomwalters@0: Specify a number 0-5 to restrict errors to certain verbosity levels. tomwalters@0: tomwalters@0: filter=-x,+y,... tomwalters@0: Specify a comma-separated list of category-filters to apply: only tomwalters@0: error messages whose category names pass the filters will be printed. tomwalters@0: (Category names are printed with the message and look like tomwalters@0: "[whitespace/indent]".) Filters are evaluated left to right. tomwalters@0: "-FOO" and "FOO" means "do not print categories that start with FOO". tomwalters@0: "+FOO" means "do print categories that start with FOO". tomwalters@0: tomwalters@0: Examples: --filter=-whitespace,+whitespace/braces tomwalters@0: --filter=whitespace,runtime/printf,+runtime/printf_format tomwalters@0: --filter=-,+build/include_what_you_use tomwalters@0: tomwalters@0: To see a list of all the categories used in cpplint, pass no arg: tomwalters@0: --filter= tomwalters@0: tomwalters@0: counting=total|toplevel|detailed tomwalters@0: The total number of errors found is always printed. If tomwalters@0: 'toplevel' is provided, then the count of errors in each of tomwalters@0: the top-level categories like 'build' and 'whitespace' will tomwalters@0: also be printed. If 'detailed' is provided, then a count tomwalters@0: is provided for each category like 'build/class'. tomwalters@0: """ tomwalters@0: tomwalters@0: # We categorize each error message we print. Here are the categories. tomwalters@0: # We want an explicit list so we can list them all in cpplint --filter=. tomwalters@0: # If you add a new error message with a new category, add it to the list tomwalters@0: # here! cpplint_unittest.py should tell you if you forget to do this. tomwalters@0: # \ used for clearer layout -- pylint: disable-msg=C6013 tomwalters@0: _ERROR_CATEGORIES = '''\ tomwalters@0: build/class tomwalters@0: build/deprecated tomwalters@0: build/endif_comment tomwalters@0: build/forward_decl tomwalters@0: build/header_guard tomwalters@0: build/include tomwalters@0: build/include_alpha tomwalters@0: build/include_order tomwalters@0: build/include_what_you_use tomwalters@0: build/namespaces tomwalters@0: build/printf_format tomwalters@0: build/storage_class tomwalters@0: legal/copyright tomwalters@0: readability/braces tomwalters@0: readability/casting tomwalters@0: readability/check tomwalters@0: readability/constructors tomwalters@0: readability/fn_size tomwalters@0: readability/function tomwalters@0: readability/multiline_comment tomwalters@0: readability/multiline_string tomwalters@0: readability/streams tomwalters@0: readability/todo tomwalters@0: readability/utf8 tomwalters@0: runtime/arrays tomwalters@0: runtime/casting tomwalters@0: runtime/explicit tomwalters@0: runtime/int tomwalters@0: runtime/init tomwalters@0: runtime/invalid_increment tomwalters@0: runtime/member_string_references tomwalters@0: runtime/memset tomwalters@0: runtime/operator tomwalters@0: runtime/printf tomwalters@0: runtime/printf_format tomwalters@0: runtime/references tomwalters@0: runtime/rtti tomwalters@0: runtime/sizeof tomwalters@0: runtime/string tomwalters@0: runtime/threadsafe_fn tomwalters@0: runtime/virtual tomwalters@0: whitespace/blank_line tomwalters@0: whitespace/braces tomwalters@0: whitespace/comma tomwalters@0: whitespace/comments tomwalters@0: whitespace/end_of_line tomwalters@0: whitespace/ending_newline tomwalters@0: whitespace/indent tomwalters@0: whitespace/labels tomwalters@0: whitespace/line_length tomwalters@0: whitespace/newline tomwalters@0: whitespace/operators tomwalters@0: whitespace/parens tomwalters@0: whitespace/semicolon tomwalters@0: whitespace/tab tomwalters@0: whitespace/todo tomwalters@0: ''' tomwalters@0: tomwalters@0: # The default state of the category filter. This is overrided by the --filter= tomwalters@0: # flag. By default all errors are on, so only add here categories that should be tomwalters@0: # off by default (i.e., categories that must be enabled by the --filter= flags). tomwalters@0: # All entries here should start with a '-' or '+', as in the --filter= flag. tomwalters@0: _DEFAULT_FILTERS = [ '-build/include_alpha' ] tomwalters@0: tomwalters@0: # We used to check for high-bit characters, but after much discussion we tomwalters@0: # decided those were OK, as long as they were in UTF-8 and didn't represent tomwalters@0: # hard-coded international strings, which belong in a seperate i18n file. tomwalters@0: tomwalters@0: # Headers that we consider STL headers. tomwalters@0: _STL_HEADERS = frozenset([ tomwalters@0: 'algobase.h', 'algorithm', 'alloc.h', 'bitset', 'deque', 'exception', tomwalters@0: 'function.h', 'functional', 'hash_map', 'hash_map.h', 'hash_set', tomwalters@0: 'hash_set.h', 'iterator', 'list', 'list.h', 'map', 'memory', 'pair.h', tomwalters@0: 'pthread_alloc', 'queue', 'set', 'set.h', 'sstream', 'stack', tomwalters@0: 'stl_alloc.h', 'stl_relops.h', 'type_traits.h', tomwalters@0: 'utility', 'vector', 'vector.h', tomwalters@0: ]) tomwalters@0: tomwalters@0: tomwalters@0: # Non-STL C++ system headers. tomwalters@0: _CPP_HEADERS = frozenset([ tomwalters@0: 'algo.h', 'builtinbuf.h', 'bvector.h', 'cassert', 'cctype', tomwalters@0: 'cerrno', 'cfloat', 'ciso646', 'climits', 'clocale', 'cmath', tomwalters@0: 'complex', 'complex.h', 'csetjmp', 'csignal', 'cstdarg', 'cstddef', tomwalters@0: 'cstdio', 'cstdlib', 'cstring', 'ctime', 'cwchar', 'cwctype', tomwalters@0: 'defalloc.h', 'deque.h', 'editbuf.h', 'exception', 'fstream', tomwalters@0: 'fstream.h', 'hashtable.h', 'heap.h', 'indstream.h', 'iomanip', tomwalters@0: 'iomanip.h', 'ios', 'iosfwd', 'iostream', 'iostream.h', 'istream.h', tomwalters@0: 'iterator.h', 'limits', 'map.h', 'multimap.h', 'multiset.h', tomwalters@0: 'numeric', 'ostream.h', 'parsestream.h', 'pfstream.h', 'PlotFile.h', tomwalters@0: 'procbuf.h', 'pthread_alloc.h', 'rope', 'rope.h', 'ropeimpl.h', tomwalters@0: 'SFile.h', 'slist', 'slist.h', 'stack.h', 'stdexcept', tomwalters@0: 'stdiostream.h', 'streambuf.h', 'stream.h', 'strfile.h', 'string', tomwalters@0: 'strstream', 'strstream.h', 'tempbuf.h', 'tree.h', 'typeinfo', 'valarray', tomwalters@0: ]) tomwalters@0: tomwalters@0: tomwalters@0: # Assertion macros. These are defined in base/logging.h and tomwalters@0: # testing/base/gunit.h. Note that the _M versions need to come first tomwalters@0: # for substring matching to work. tomwalters@0: _CHECK_MACROS = [ tomwalters@0: 'DCHECK', 'CHECK', tomwalters@0: 'EXPECT_TRUE_M', 'EXPECT_TRUE', tomwalters@0: 'ASSERT_TRUE_M', 'ASSERT_TRUE', tomwalters@0: 'EXPECT_FALSE_M', 'EXPECT_FALSE', tomwalters@0: 'ASSERT_FALSE_M', 'ASSERT_FALSE', tomwalters@0: ] tomwalters@0: tomwalters@0: # Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE tomwalters@0: _CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS]) tomwalters@0: tomwalters@0: for op, replacement in [('==', 'EQ'), ('!=', 'NE'), tomwalters@0: ('>=', 'GE'), ('>', 'GT'), tomwalters@0: ('<=', 'LE'), ('<', 'LT')]: tomwalters@0: _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement tomwalters@0: _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement tomwalters@0: _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement tomwalters@0: _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement tomwalters@0: _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement tomwalters@0: _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement tomwalters@0: tomwalters@0: for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'), tomwalters@0: ('>=', 'LT'), ('>', 'LE'), tomwalters@0: ('<=', 'GT'), ('<', 'GE')]: tomwalters@0: _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement tomwalters@0: _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement tomwalters@0: _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement tomwalters@0: _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement tomwalters@0: tomwalters@0: tomwalters@0: # These constants define types of headers for use with tomwalters@0: # _IncludeState.CheckNextIncludeOrder(). tomwalters@0: _C_SYS_HEADER = 1 tomwalters@0: _CPP_SYS_HEADER = 2 tomwalters@0: _LIKELY_MY_HEADER = 3 tomwalters@0: _POSSIBLE_MY_HEADER = 4 tomwalters@0: _OTHER_HEADER = 5 tomwalters@0: tomwalters@0: tomwalters@0: _regexp_compile_cache = {} tomwalters@0: tomwalters@0: tomwalters@0: def Match(pattern, s): tomwalters@0: """Matches the string with the pattern, caching the compiled regexp.""" tomwalters@0: # The regexp compilation caching is inlined in both Match and Search for tomwalters@0: # performance reasons; factoring it out into a separate function turns out tomwalters@0: # to be noticeably expensive. tomwalters@0: if not pattern in _regexp_compile_cache: tomwalters@0: _regexp_compile_cache[pattern] = sre_compile.compile(pattern) tomwalters@0: return _regexp_compile_cache[pattern].match(s) tomwalters@0: tomwalters@0: tomwalters@0: def Search(pattern, s): tomwalters@0: """Searches the string for the pattern, caching the compiled regexp.""" tomwalters@0: if not pattern in _regexp_compile_cache: tomwalters@0: _regexp_compile_cache[pattern] = sre_compile.compile(pattern) tomwalters@0: return _regexp_compile_cache[pattern].search(s) tomwalters@0: tomwalters@0: tomwalters@0: class _IncludeState(dict): tomwalters@0: """Tracks line numbers for includes, and the order in which includes appear. tomwalters@0: tomwalters@0: As a dict, an _IncludeState object serves as a mapping between include tomwalters@0: filename and line number on which that file was included. tomwalters@0: tomwalters@0: Call CheckNextIncludeOrder() once for each header in the file, passing tomwalters@0: in the type constants defined above. Calls in an illegal order will tomwalters@0: raise an _IncludeError with an appropriate error message. tomwalters@0: tomwalters@0: """ tomwalters@0: # self._section will move monotonically through this set. If it ever tomwalters@0: # needs to move backwards, CheckNextIncludeOrder will raise an error. tomwalters@0: _INITIAL_SECTION = 0 tomwalters@0: _MY_H_SECTION = 1 tomwalters@0: _C_SECTION = 2 tomwalters@0: _CPP_SECTION = 3 tomwalters@0: _OTHER_H_SECTION = 4 tomwalters@0: tomwalters@0: _TYPE_NAMES = { tomwalters@0: _C_SYS_HEADER: 'C system header', tomwalters@0: _CPP_SYS_HEADER: 'C++ system header', tomwalters@0: _LIKELY_MY_HEADER: 'header this file implements', tomwalters@0: _POSSIBLE_MY_HEADER: 'header this file may implement', tomwalters@0: _OTHER_HEADER: 'other header', tomwalters@0: } tomwalters@0: _SECTION_NAMES = { tomwalters@0: _INITIAL_SECTION: "... nothing. (This can't be an error.)", tomwalters@0: _MY_H_SECTION: 'a header this file implements', tomwalters@0: _C_SECTION: 'C system header', tomwalters@0: _CPP_SECTION: 'C++ system header', tomwalters@0: _OTHER_H_SECTION: 'other header', tomwalters@0: } tomwalters@0: tomwalters@0: def __init__(self): tomwalters@0: dict.__init__(self) tomwalters@0: # The name of the current section. tomwalters@0: self._section = self._INITIAL_SECTION tomwalters@0: # The path of last found header. tomwalters@0: self._last_header = '' tomwalters@0: tomwalters@0: def CanonicalizeAlphabeticalOrder(self, header_path): tomwalters@0: """Returns a path canonicalized for alphabetical comparisson. tomwalters@0: tomwalters@0: - replaces "-" with "_" so they both cmp the same. tomwalters@0: - removes '-inl' since we don't require them to be after the main header. tomwalters@0: - lowercase everything, just in case. tomwalters@0: tomwalters@0: Args: tomwalters@0: header_path: Path to be canonicalized. tomwalters@0: tomwalters@0: Returns: tomwalters@0: Canonicalized path. tomwalters@0: """ tomwalters@0: return header_path.replace('-inl.h', '.h').replace('-', '_').lower() tomwalters@0: tomwalters@0: def IsInAlphabeticalOrder(self, header_path): tomwalters@0: """Check if a header is in alphabetical order with the previous header. tomwalters@0: tomwalters@0: Args: tomwalters@0: header_path: Header to be checked. tomwalters@0: tomwalters@0: Returns: tomwalters@0: Returns true if the header is in alphabetical order. tomwalters@0: """ tomwalters@0: canonical_header = self.CanonicalizeAlphabeticalOrder(header_path) tomwalters@0: if self._last_header > canonical_header: tomwalters@0: return False tomwalters@0: self._last_header = canonical_header tomwalters@0: return True tomwalters@0: tomwalters@0: def CheckNextIncludeOrder(self, header_type): tomwalters@0: """Returns a non-empty error message if the next header is out of order. tomwalters@0: tomwalters@0: This function also updates the internal state to be ready to check tomwalters@0: the next include. tomwalters@0: tomwalters@0: Args: tomwalters@0: header_type: One of the _XXX_HEADER constants defined above. tomwalters@0: tomwalters@0: Returns: tomwalters@0: The empty string if the header is in the right order, or an tomwalters@0: error message describing what's wrong. tomwalters@0: tomwalters@0: """ tomwalters@0: error_message = ('Found %s after %s' % tomwalters@0: (self._TYPE_NAMES[header_type], tomwalters@0: self._SECTION_NAMES[self._section])) tomwalters@0: tomwalters@0: last_section = self._section tomwalters@0: tomwalters@0: if header_type == _C_SYS_HEADER: tomwalters@0: if self._section <= self._C_SECTION: tomwalters@0: self._section = self._C_SECTION tomwalters@0: else: tomwalters@0: self._last_header = '' tomwalters@0: return error_message tomwalters@0: elif header_type == _CPP_SYS_HEADER: tomwalters@0: if self._section <= self._CPP_SECTION: tomwalters@0: self._section = self._CPP_SECTION tomwalters@0: else: tomwalters@0: self._last_header = '' tomwalters@0: return error_message tomwalters@0: elif header_type == _LIKELY_MY_HEADER: tomwalters@0: if self._section <= self._MY_H_SECTION: tomwalters@0: self._section = self._MY_H_SECTION tomwalters@0: else: tomwalters@0: self._section = self._OTHER_H_SECTION tomwalters@0: elif header_type == _POSSIBLE_MY_HEADER: tomwalters@0: if self._section <= self._MY_H_SECTION: tomwalters@0: self._section = self._MY_H_SECTION tomwalters@0: else: tomwalters@0: # This will always be the fallback because we're not sure tomwalters@0: # enough that the header is associated with this file. tomwalters@0: self._section = self._OTHER_H_SECTION tomwalters@0: else: tomwalters@0: assert header_type == _OTHER_HEADER tomwalters@0: self._section = self._OTHER_H_SECTION tomwalters@0: tomwalters@0: if last_section != self._section: tomwalters@0: self._last_header = '' tomwalters@0: tomwalters@0: return '' tomwalters@0: tomwalters@0: tomwalters@0: class _CppLintState(object): tomwalters@0: """Maintains module-wide state..""" tomwalters@0: tomwalters@0: def __init__(self): tomwalters@0: self.verbose_level = 1 # global setting. tomwalters@0: self.error_count = 0 # global count of reported errors tomwalters@0: # filters to apply when emitting error messages tomwalters@0: self.filters = _DEFAULT_FILTERS[:] tomwalters@0: self.counting = 'total' # In what way are we counting errors? tomwalters@0: self.errors_by_category = {} # string to int dict storing error counts tomwalters@0: tomwalters@0: # output format: tomwalters@0: # "emacs" - format that emacs can parse (default) tomwalters@0: # "vs7" - format that Microsoft Visual Studio 7 can parse tomwalters@0: self.output_format = 'emacs' tomwalters@0: tomwalters@0: def SetOutputFormat(self, output_format): tomwalters@0: """Sets the output format for errors.""" tomwalters@0: self.output_format = output_format tomwalters@0: tomwalters@0: def SetVerboseLevel(self, level): tomwalters@0: """Sets the module's verbosity, and returns the previous setting.""" tomwalters@0: last_verbose_level = self.verbose_level tomwalters@0: self.verbose_level = level tomwalters@0: return last_verbose_level tomwalters@0: tomwalters@0: def SetCountingStyle(self, counting_style): tomwalters@0: """Sets the module's counting options.""" tomwalters@0: self.counting = counting_style tomwalters@0: tomwalters@0: def SetFilters(self, filters): tomwalters@0: """Sets the error-message filters. tomwalters@0: tomwalters@0: These filters are applied when deciding whether to emit a given tomwalters@0: error message. tomwalters@0: tomwalters@0: Args: tomwalters@0: filters: A string of comma-separated filters (eg "+whitespace/indent"). tomwalters@0: Each filter should start with + or -; else we die. tomwalters@0: tomwalters@0: Raises: tomwalters@0: ValueError: The comma-separated filters did not all start with '+' or '-'. tomwalters@0: E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter" tomwalters@0: """ tomwalters@0: # Default filters always have less priority than the flag ones. tomwalters@0: self.filters = _DEFAULT_FILTERS[:] tomwalters@0: for filt in filters.split(','): tomwalters@0: clean_filt = filt.strip() tomwalters@0: if clean_filt: tomwalters@0: self.filters.append(clean_filt) tomwalters@0: for filt in self.filters: tomwalters@0: if not (filt.startswith('+') or filt.startswith('-')): tomwalters@0: raise ValueError('Every filter in --filters must start with + or -' tomwalters@0: ' (%s does not)' % filt) tomwalters@0: tomwalters@0: def ResetErrorCounts(self): tomwalters@0: """Sets the module's error statistic back to zero.""" tomwalters@0: self.error_count = 0 tomwalters@0: self.errors_by_category = {} tomwalters@0: tomwalters@0: def IncrementErrorCount(self, category): tomwalters@0: """Bumps the module's error statistic.""" tomwalters@0: self.error_count += 1 tomwalters@0: if self.counting in ('toplevel', 'detailed'): tomwalters@0: if self.counting != 'detailed': tomwalters@0: category = category.split('/')[0] tomwalters@0: if category not in self.errors_by_category: tomwalters@0: self.errors_by_category[category] = 0 tomwalters@0: self.errors_by_category[category] += 1 tomwalters@0: tomwalters@0: def PrintErrorCounts(self): tomwalters@0: """Print a summary of errors by category, and the total.""" tomwalters@0: for category, count in self.errors_by_category.iteritems(): tomwalters@0: sys.stderr.write('Category \'%s\' errors found: %d\n' % tomwalters@0: (category, count)) tomwalters@0: sys.stderr.write('Total errors found: %d\n' % self.error_count) tomwalters@0: tomwalters@0: _cpplint_state = _CppLintState() tomwalters@0: tomwalters@0: tomwalters@0: def _OutputFormat(): tomwalters@0: """Gets the module's output format.""" tomwalters@0: return _cpplint_state.output_format tomwalters@0: tomwalters@0: tomwalters@0: def _SetOutputFormat(output_format): tomwalters@0: """Sets the module's output format.""" tomwalters@0: _cpplint_state.SetOutputFormat(output_format) tomwalters@0: tomwalters@0: tomwalters@0: def _VerboseLevel(): tomwalters@0: """Returns the module's verbosity setting.""" tomwalters@0: return _cpplint_state.verbose_level tomwalters@0: tomwalters@0: tomwalters@0: def _SetVerboseLevel(level): tomwalters@0: """Sets the module's verbosity, and returns the previous setting.""" tomwalters@0: return _cpplint_state.SetVerboseLevel(level) tomwalters@0: tomwalters@0: tomwalters@0: def _SetCountingStyle(level): tomwalters@0: """Sets the module's counting options.""" tomwalters@0: _cpplint_state.SetCountingStyle(level) tomwalters@0: tomwalters@0: tomwalters@0: def _Filters(): tomwalters@0: """Returns the module's list of output filters, as a list.""" tomwalters@0: return _cpplint_state.filters tomwalters@0: tomwalters@0: tomwalters@0: def _SetFilters(filters): tomwalters@0: """Sets the module's error-message filters. tomwalters@0: tomwalters@0: These filters are applied when deciding whether to emit a given tomwalters@0: error message. tomwalters@0: tomwalters@0: Args: tomwalters@0: filters: A string of comma-separated filters (eg "whitespace/indent"). tomwalters@0: Each filter should start with + or -; else we die. tomwalters@0: """ tomwalters@0: _cpplint_state.SetFilters(filters) tomwalters@0: tomwalters@0: tomwalters@0: class _FunctionState(object): tomwalters@0: """Tracks current function name and the number of lines in its body.""" tomwalters@0: tomwalters@0: _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. tomwalters@0: _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. tomwalters@0: tomwalters@0: def __init__(self): tomwalters@0: self.in_a_function = False tomwalters@0: self.lines_in_function = 0 tomwalters@0: self.current_function = '' tomwalters@0: tomwalters@0: def Begin(self, function_name): tomwalters@0: """Start analyzing function body. tomwalters@0: tomwalters@0: Args: tomwalters@0: function_name: The name of the function being tracked. tomwalters@0: """ tomwalters@0: self.in_a_function = True tomwalters@0: self.lines_in_function = 0 tomwalters@0: self.current_function = function_name tomwalters@0: tomwalters@0: def Count(self): tomwalters@0: """Count line in current function body.""" tomwalters@0: if self.in_a_function: tomwalters@0: self.lines_in_function += 1 tomwalters@0: tomwalters@0: def Check(self, error, filename, linenum): tomwalters@0: """Report if too many lines in function body. tomwalters@0: tomwalters@0: Args: tomwalters@0: error: The function to call with any errors found. tomwalters@0: filename: The name of the current file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: """ tomwalters@0: if Match(r'T(EST|est)', self.current_function): tomwalters@0: base_trigger = self._TEST_TRIGGER tomwalters@0: else: tomwalters@0: base_trigger = self._NORMAL_TRIGGER tomwalters@0: trigger = base_trigger * 2**_VerboseLevel() tomwalters@0: tomwalters@0: if self.lines_in_function > trigger: tomwalters@0: error_level = int(math.log(self.lines_in_function / base_trigger, 2)) tomwalters@0: # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... tomwalters@0: if error_level > 5: tomwalters@0: error_level = 5 tomwalters@0: error(filename, linenum, 'readability/fn_size', error_level, tomwalters@0: 'Small and focused functions are preferred:' tomwalters@0: ' %s has %d non-comment lines' tomwalters@0: ' (error triggered by exceeding %d lines).' % ( tomwalters@0: self.current_function, self.lines_in_function, trigger)) tomwalters@0: tomwalters@0: def End(self): tomwalters@0: """Stop analizing function body.""" tomwalters@0: self.in_a_function = False tomwalters@0: tomwalters@0: tomwalters@0: class _IncludeError(Exception): tomwalters@0: """Indicates a problem with the include order in a file.""" tomwalters@0: pass tomwalters@0: tomwalters@0: tomwalters@0: class FileInfo: tomwalters@0: """Provides utility functions for filenames. tomwalters@0: tomwalters@0: FileInfo provides easy access to the components of a file's path tomwalters@0: relative to the project root. tomwalters@0: """ tomwalters@0: tomwalters@0: def __init__(self, filename): tomwalters@0: self._filename = filename tomwalters@0: tomwalters@0: def FullName(self): tomwalters@0: """Make Windows paths like Unix.""" tomwalters@0: return os.path.abspath(self._filename).replace('\\', '/') tomwalters@0: tomwalters@0: def RepositoryName(self): tomwalters@0: """FullName after removing the local path to the repository. tomwalters@0: tomwalters@0: If we have a real absolute path name here we can try to do something smart: tomwalters@0: detecting the root of the checkout and truncating /path/to/checkout from tomwalters@0: the name so that we get header guards that don't include things like tomwalters@0: "C:\Documents and Settings\..." or "/home/username/..." in them and thus tomwalters@0: people on different computers who have checked the source out to different tomwalters@0: locations won't see bogus errors. tomwalters@0: """ tomwalters@0: fullname = self.FullName() tomwalters@0: tomwalters@0: if os.path.exists(fullname): tomwalters@0: project_dir = os.path.dirname(fullname) tomwalters@0: tomwalters@0: if os.path.exists(os.path.join(project_dir, ".svn")): tomwalters@0: # If there's a .svn file in the current directory, we recursively look tomwalters@0: # up the directory tree for the top of the SVN checkout tomwalters@0: root_dir = project_dir tomwalters@0: one_up_dir = os.path.dirname(root_dir) tomwalters@0: while os.path.exists(os.path.join(one_up_dir, ".svn")): tomwalters@0: root_dir = os.path.dirname(root_dir) tomwalters@0: one_up_dir = os.path.dirname(one_up_dir) tomwalters@0: tomwalters@0: prefix = os.path.commonprefix([root_dir, project_dir]) tomwalters@0: return fullname[len(prefix) + 1:] tomwalters@0: tomwalters@0: # Not SVN? Try to find a git or hg top level directory by searching up tomwalters@0: # from the current path. tomwalters@0: root_dir = os.path.dirname(fullname) tomwalters@0: while (root_dir != os.path.dirname(root_dir) and tomwalters@0: not os.path.exists(os.path.join(root_dir, ".git")) and tomwalters@0: not os.path.exists(os.path.join(root_dir, ".hg"))): tomwalters@0: root_dir = os.path.dirname(root_dir) tomwalters@0: if (os.path.exists(os.path.join(root_dir, ".git")) or tomwalters@0: os.path.exists(os.path.join(root_dir, ".hg"))): tomwalters@0: prefix = os.path.commonprefix([root_dir, project_dir]) tomwalters@0: return fullname[len(prefix) + 1:] tomwalters@0: tomwalters@0: # Don't know what to do; header guard warnings may be wrong... tomwalters@0: return fullname tomwalters@0: tomwalters@0: def Split(self): tomwalters@0: """Splits the file into the directory, basename, and extension. tomwalters@0: tomwalters@0: For 'chrome/browser/browser.cc', Split() would tomwalters@0: return ('chrome/browser', 'browser', '.cc') tomwalters@0: tomwalters@0: Returns: tomwalters@0: A tuple of (directory, basename, extension). tomwalters@0: """ tomwalters@0: tomwalters@0: googlename = self.RepositoryName() tomwalters@0: project, rest = os.path.split(googlename) tomwalters@0: return (project,) + os.path.splitext(rest) tomwalters@0: tomwalters@0: def BaseName(self): tomwalters@0: """File base name - text after the final slash, before the final period.""" tomwalters@0: return self.Split()[1] tomwalters@0: tomwalters@0: def Extension(self): tomwalters@0: """File extension - text following the final period.""" tomwalters@0: return self.Split()[2] tomwalters@0: tomwalters@0: def NoExtension(self): tomwalters@0: """File has no source file extension.""" tomwalters@0: return '/'.join(self.Split()[0:2]) tomwalters@0: tomwalters@0: def IsSource(self): tomwalters@0: """File has a source file extension.""" tomwalters@0: return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx') tomwalters@0: tomwalters@0: tomwalters@0: def _ShouldPrintError(category, confidence): tomwalters@0: """Returns true iff confidence >= verbose, and category passes filter.""" tomwalters@0: # There are two ways we might decide not to print an error message: tomwalters@0: # the verbosity level isn't high enough, or the filters filter it out. tomwalters@0: if confidence < _cpplint_state.verbose_level: tomwalters@0: return False tomwalters@0: tomwalters@0: is_filtered = False tomwalters@0: for one_filter in _Filters(): tomwalters@0: if one_filter.startswith('-'): tomwalters@0: if category.startswith(one_filter[1:]): tomwalters@0: is_filtered = True tomwalters@0: elif one_filter.startswith('+'): tomwalters@0: if category.startswith(one_filter[1:]): tomwalters@0: is_filtered = False tomwalters@0: else: tomwalters@0: assert False # should have been checked for in SetFilter. tomwalters@0: if is_filtered: tomwalters@0: return False tomwalters@0: tomwalters@0: return True tomwalters@0: tomwalters@0: tomwalters@0: def Error(filename, linenum, category, confidence, message): tomwalters@0: """Logs the fact we've found a lint error. tomwalters@0: tomwalters@0: We log where the error was found, and also our confidence in the error, tomwalters@0: that is, how certain we are this is a legitimate style regression, and tomwalters@0: not a misidentification or a use that's sometimes justified. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the file containing the error. tomwalters@0: linenum: The number of the line containing the error. tomwalters@0: category: A string used to describe the "category" this bug tomwalters@0: falls under: "whitespace", say, or "runtime". Categories tomwalters@0: may have a hierarchy separated by slashes: "whitespace/indent". tomwalters@0: confidence: A number from 1-5 representing a confidence score for tomwalters@0: the error, with 5 meaning that we are certain of the problem, tomwalters@0: and 1 meaning that it could be a legitimate construct. tomwalters@0: message: The error message. tomwalters@0: """ tomwalters@0: # There are two ways we might decide not to print an error message: tomwalters@0: # the verbosity level isn't high enough, or the filters filter it out. tomwalters@0: if _ShouldPrintError(category, confidence): tomwalters@0: _cpplint_state.IncrementErrorCount(category) tomwalters@0: if _cpplint_state.output_format == 'vs7': tomwalters@0: sys.stderr.write('%s(%s): %s [%s] [%d]\n' % ( tomwalters@0: filename, linenum, message, category, confidence)) tomwalters@0: else: tomwalters@0: sys.stderr.write('%s:%s: %s [%s] [%d]\n' % ( tomwalters@0: filename, linenum, message, category, confidence)) tomwalters@0: tomwalters@0: tomwalters@0: # Matches standard C++ escape esequences per 2.13.2.3 of the C++ standard. tomwalters@0: _RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile( tomwalters@0: r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)') tomwalters@0: # Matches strings. Escape codes should already be removed by ESCAPES. tomwalters@0: _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES = re.compile(r'"[^"]*"') tomwalters@0: # Matches characters. Escape codes should already be removed by ESCAPES. tomwalters@0: _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES = re.compile(r"'.'") tomwalters@0: # Matches multi-line C++ comments. tomwalters@0: # This RE is a little bit more complicated than one might expect, because we tomwalters@0: # have to take care of space removals tools so we can handle comments inside tomwalters@0: # statements better. tomwalters@0: # The current rule is: We only clear spaces from both sides when we're at the tomwalters@0: # end of the line. Otherwise, we try to remove spaces from the right side, tomwalters@0: # if this doesn't work we try on left side but only if there's a non-character tomwalters@0: # on the right. tomwalters@0: _RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile( tomwalters@0: r"""(\s*/\*.*\*/\s*$| tomwalters@0: /\*.*\*/\s+| tomwalters@0: \s+/\*.*\*/(?=\W)| tomwalters@0: /\*.*\*/)""", re.VERBOSE) tomwalters@0: tomwalters@0: tomwalters@0: def IsCppString(line): tomwalters@0: """Does line terminate so, that the next symbol is in string constant. tomwalters@0: tomwalters@0: This function does not consider single-line nor multi-line comments. tomwalters@0: tomwalters@0: Args: tomwalters@0: line: is a partial line of code starting from the 0..n. tomwalters@0: tomwalters@0: Returns: tomwalters@0: True, if next character appended to 'line' is inside a tomwalters@0: string constant. tomwalters@0: """ tomwalters@0: tomwalters@0: line = line.replace(r'\\', 'XX') # after this, \\" does not match to \" tomwalters@0: return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1 tomwalters@0: tomwalters@0: tomwalters@0: def FindNextMultiLineCommentStart(lines, lineix): tomwalters@0: """Find the beginning marker for a multiline comment.""" tomwalters@0: while lineix < len(lines): tomwalters@0: if lines[lineix].strip().startswith('/*'): tomwalters@0: # Only return this marker if the comment goes beyond this line tomwalters@0: if lines[lineix].strip().find('*/', 2) < 0: tomwalters@0: return lineix tomwalters@0: lineix += 1 tomwalters@0: return len(lines) tomwalters@0: tomwalters@0: tomwalters@0: def FindNextMultiLineCommentEnd(lines, lineix): tomwalters@0: """We are inside a comment, find the end marker.""" tomwalters@0: while lineix < len(lines): tomwalters@0: if lines[lineix].strip().endswith('*/'): tomwalters@0: return lineix tomwalters@0: lineix += 1 tomwalters@0: return len(lines) tomwalters@0: tomwalters@0: tomwalters@0: def RemoveMultiLineCommentsFromRange(lines, begin, end): tomwalters@0: """Clears a range of lines for multi-line comments.""" tomwalters@0: # Having // dummy comments makes the lines non-empty, so we will not get tomwalters@0: # unnecessary blank line warnings later in the code. tomwalters@0: for i in range(begin, end): tomwalters@0: lines[i] = '// dummy' tomwalters@0: tomwalters@0: tomwalters@0: def RemoveMultiLineComments(filename, lines, error): tomwalters@0: """Removes multiline (c-style) comments from lines.""" tomwalters@0: lineix = 0 tomwalters@0: while lineix < len(lines): tomwalters@0: lineix_begin = FindNextMultiLineCommentStart(lines, lineix) tomwalters@0: if lineix_begin >= len(lines): tomwalters@0: return tomwalters@0: lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin) tomwalters@0: if lineix_end >= len(lines): tomwalters@0: error(filename, lineix_begin + 1, 'readability/multiline_comment', 5, tomwalters@0: 'Could not find end of multi-line comment') tomwalters@0: return tomwalters@0: RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1) tomwalters@0: lineix = lineix_end + 1 tomwalters@0: tomwalters@0: tomwalters@0: def CleanseComments(line): tomwalters@0: """Removes //-comments and single-line C-style /* */ comments. tomwalters@0: tomwalters@0: Args: tomwalters@0: line: A line of C++ source. tomwalters@0: tomwalters@0: Returns: tomwalters@0: The line with single-line comments removed. tomwalters@0: """ tomwalters@0: commentpos = line.find('//') tomwalters@0: if commentpos != -1 and not IsCppString(line[:commentpos]): tomwalters@0: line = line[:commentpos] tomwalters@0: # get rid of /* ... */ tomwalters@0: return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line) tomwalters@0: tomwalters@0: tomwalters@0: class CleansedLines(object): tomwalters@0: """Holds 3 copies of all lines with different preprocessing applied to them. tomwalters@0: tomwalters@0: 1) elided member contains lines without strings and comments, tomwalters@0: 2) lines member contains lines without comments, and tomwalters@0: 3) raw member contains all the lines without processing. tomwalters@0: All these three members are of , and of the same length. tomwalters@0: """ tomwalters@0: tomwalters@0: def __init__(self, lines): tomwalters@0: self.elided = [] tomwalters@0: self.lines = [] tomwalters@0: self.raw_lines = lines tomwalters@0: self.num_lines = len(lines) tomwalters@0: for linenum in range(len(lines)): tomwalters@0: self.lines.append(CleanseComments(lines[linenum])) tomwalters@0: elided = self._CollapseStrings(lines[linenum]) tomwalters@0: self.elided.append(CleanseComments(elided)) tomwalters@0: tomwalters@0: def NumLines(self): tomwalters@0: """Returns the number of lines represented.""" tomwalters@0: return self.num_lines tomwalters@0: tomwalters@0: @staticmethod tomwalters@0: def _CollapseStrings(elided): tomwalters@0: """Collapses strings and chars on a line to simple "" or '' blocks. tomwalters@0: tomwalters@0: We nix strings first so we're not fooled by text like '"http://"' tomwalters@0: tomwalters@0: Args: tomwalters@0: elided: The line being processed. tomwalters@0: tomwalters@0: Returns: tomwalters@0: The line with collapsed strings. tomwalters@0: """ tomwalters@0: if not _RE_PATTERN_INCLUDE.match(elided): tomwalters@0: # Remove escaped characters first to make quote/single quote collapsing tomwalters@0: # basic. Things that look like escaped characters shouldn't occur tomwalters@0: # outside of strings and chars. tomwalters@0: elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided) tomwalters@0: elided = _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES.sub("''", elided) tomwalters@0: elided = _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES.sub('""', elided) tomwalters@0: return elided tomwalters@0: tomwalters@0: tomwalters@0: def CloseExpression(clean_lines, linenum, pos): tomwalters@0: """If input points to ( or { or [, finds the position that closes it. tomwalters@0: tomwalters@0: If lines[linenum][pos] points to a '(' or '{' or '[', finds the the tomwalters@0: linenum/pos that correspond to the closing of the expression. tomwalters@0: tomwalters@0: Args: tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: pos: A position on the line. tomwalters@0: tomwalters@0: Returns: tomwalters@0: A tuple (line, linenum, pos) pointer *past* the closing brace, or tomwalters@0: (line, len(lines), -1) if we never find a close. Note we ignore tomwalters@0: strings and comments when matching; and the line we return is the tomwalters@0: 'cleansed' line at linenum. tomwalters@0: """ tomwalters@0: tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: startchar = line[pos] tomwalters@0: if startchar not in '({[': tomwalters@0: return (line, clean_lines.NumLines(), -1) tomwalters@0: if startchar == '(': endchar = ')' tomwalters@0: if startchar == '[': endchar = ']' tomwalters@0: if startchar == '{': endchar = '}' tomwalters@0: tomwalters@0: num_open = line.count(startchar) - line.count(endchar) tomwalters@0: while linenum < clean_lines.NumLines() and num_open > 0: tomwalters@0: linenum += 1 tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: num_open += line.count(startchar) - line.count(endchar) tomwalters@0: # OK, now find the endchar that actually got us back to even tomwalters@0: endpos = len(line) tomwalters@0: while num_open >= 0: tomwalters@0: endpos = line.rfind(')', 0, endpos) tomwalters@0: num_open -= 1 # chopped off another ) tomwalters@0: return (line, linenum, endpos + 1) tomwalters@0: tomwalters@0: tomwalters@0: def CheckForCopyright(filename, lines, error): tomwalters@0: """Logs an error if no Copyright message appears at the top of the file.""" tomwalters@0: tomwalters@0: # We'll say it should occur by line 10. Don't forget there's a tomwalters@0: # dummy line at the front. tomwalters@0: for line in xrange(1, min(len(lines), 11)): tomwalters@0: if re.search(r'Copyright', lines[line], re.I): break tomwalters@0: else: # means no copyright line was found tomwalters@0: error(filename, 0, 'legal/copyright', 5, tomwalters@0: 'No copyright message found. ' tomwalters@0: 'You should have a line: "Copyright [year] "') tomwalters@0: tomwalters@0: tomwalters@0: def GetHeaderGuardCPPVariable(filename): tomwalters@0: """Returns the CPP variable that should be used as a header guard. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of a C++ header file. tomwalters@0: tomwalters@0: Returns: tomwalters@0: The CPP variable that should be used as a header guard in the tomwalters@0: named file. tomwalters@0: tomwalters@0: """ tomwalters@0: tomwalters@0: fileinfo = FileInfo(filename) tomwalters@0: return re.sub(r'[-./\s]', '_', fileinfo.RepositoryName()).upper() + '_' tomwalters@0: tomwalters@0: tomwalters@0: def CheckForHeaderGuard(filename, lines, error): tomwalters@0: """Checks that the file contains a header guard. tomwalters@0: tomwalters@0: Logs an error if no #ifndef header guard is present. For other tomwalters@0: headers, checks that the full pathname is used. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the C++ header file. tomwalters@0: lines: An array of strings, each representing a line of the file. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: tomwalters@0: cppvar = GetHeaderGuardCPPVariable(filename) tomwalters@0: tomwalters@0: ifndef = None tomwalters@0: ifndef_linenum = 0 tomwalters@0: define = None tomwalters@0: endif = None tomwalters@0: endif_linenum = 0 tomwalters@0: for linenum, line in enumerate(lines): tomwalters@0: linesplit = line.split() tomwalters@0: if len(linesplit) >= 2: tomwalters@0: # find the first occurrence of #ifndef and #define, save arg tomwalters@0: if not ifndef and linesplit[0] == '#ifndef': tomwalters@0: # set ifndef to the header guard presented on the #ifndef line. tomwalters@0: ifndef = linesplit[1] tomwalters@0: ifndef_linenum = linenum tomwalters@0: if not define and linesplit[0] == '#define': tomwalters@0: define = linesplit[1] tomwalters@0: # find the last occurrence of #endif, save entire line tomwalters@0: if line.startswith('#endif'): tomwalters@0: endif = line tomwalters@0: endif_linenum = linenum tomwalters@0: tomwalters@0: if not ifndef or not define or ifndef != define: tomwalters@0: error(filename, 0, 'build/header_guard', 5, tomwalters@0: 'No #ifndef header guard found, suggested CPP variable is: %s' % tomwalters@0: cppvar) tomwalters@0: return tomwalters@0: tomwalters@0: # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ tomwalters@0: # for backward compatibility. tomwalters@0: if ifndef != cppvar and not Search(r'\bNOLINT\b', lines[ifndef_linenum]): tomwalters@0: error_level = 0 tomwalters@0: if ifndef != cppvar + '_': tomwalters@0: error_level = 5 tomwalters@0: tomwalters@0: error(filename, ifndef_linenum, 'build/header_guard', error_level, tomwalters@0: '#ifndef header guard has wrong style, please use: %s' % cppvar) tomwalters@0: tomwalters@0: if (endif != ('#endif // %s' % cppvar) and tomwalters@0: not Search(r'\bNOLINT\b', lines[endif_linenum])): tomwalters@0: error_level = 0 tomwalters@0: if endif != ('#endif // %s' % (cppvar + '_')): tomwalters@0: error_level = 5 tomwalters@0: tomwalters@0: error(filename, endif_linenum, 'build/header_guard', error_level, tomwalters@0: '#endif line should be "#endif // %s"' % cppvar) tomwalters@0: tomwalters@0: tomwalters@0: def CheckForUnicodeReplacementCharacters(filename, lines, error): tomwalters@0: """Logs an error for each line containing Unicode replacement characters. tomwalters@0: tomwalters@0: These indicate that either the file contained invalid UTF-8 (likely) tomwalters@0: or Unicode replacement characters (which it shouldn't). Note that tomwalters@0: it's possible for this to throw off line numbering if the invalid tomwalters@0: UTF-8 occurred adjacent to a newline. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: lines: An array of strings, each representing a line of the file. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: for linenum, line in enumerate(lines): tomwalters@0: if u'\ufffd' in line: tomwalters@0: error(filename, linenum, 'readability/utf8', 5, tomwalters@0: 'Line contains invalid UTF-8 (or Unicode replacement character).') tomwalters@0: tomwalters@0: tomwalters@0: def CheckForNewlineAtEOF(filename, lines, error): tomwalters@0: """Logs an error if there is no newline char at the end of the file. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: lines: An array of strings, each representing a line of the file. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: tomwalters@0: # The array lines() was created by adding two newlines to the tomwalters@0: # original file (go figure), then splitting on \n. tomwalters@0: # To verify that the file ends in \n, we just have to make sure the tomwalters@0: # last-but-two element of lines() exists and is empty. tomwalters@0: if len(lines) < 3 or lines[-2]: tomwalters@0: error(filename, len(lines) - 2, 'whitespace/ending_newline', 5, tomwalters@0: 'Could not find a newline character at the end of the file.') tomwalters@0: tomwalters@0: tomwalters@0: def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error): tomwalters@0: """Logs an error if we see /* ... */ or "..." that extend past one line. tomwalters@0: tomwalters@0: /* ... */ comments are legit inside macros, for one line. tomwalters@0: Otherwise, we prefer // comments, so it's ok to warn about the tomwalters@0: other. Likewise, it's ok for strings to extend across multiple tomwalters@0: lines, as long as a line continuation character (backslash) tomwalters@0: terminates each line. Although not currently prohibited by the C++ tomwalters@0: style guide, it's ugly and unnecessary. We don't do well with either tomwalters@0: in this lint program, so we warn about both. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: tomwalters@0: # Remove all \\ (escaped backslashes) from the line. They are OK, and the tomwalters@0: # second (escaped) slash may trigger later \" detection erroneously. tomwalters@0: line = line.replace('\\\\', '') tomwalters@0: tomwalters@0: if line.count('/*') > line.count('*/'): tomwalters@0: error(filename, linenum, 'readability/multiline_comment', 5, tomwalters@0: 'Complex multi-line /*...*/-style comment found. ' tomwalters@0: 'Lint may give bogus warnings. ' tomwalters@0: 'Consider replacing these with //-style comments, ' tomwalters@0: 'with #if 0...#endif, ' tomwalters@0: 'or with more clearly structured multi-line comments.') tomwalters@0: tomwalters@0: if (line.count('"') - line.count('\\"')) % 2: tomwalters@0: error(filename, linenum, 'readability/multiline_string', 5, tomwalters@0: 'Multi-line string ("...") found. This lint script doesn\'t ' tomwalters@0: 'do well with such strings, and may give bogus warnings. They\'re ' tomwalters@0: 'ugly and unnecessary, and you should use concatenation instead".') tomwalters@0: tomwalters@0: tomwalters@0: threading_list = ( tomwalters@0: ('asctime(', 'asctime_r('), tomwalters@0: ('ctime(', 'ctime_r('), tomwalters@0: ('getgrgid(', 'getgrgid_r('), tomwalters@0: ('getgrnam(', 'getgrnam_r('), tomwalters@0: ('getlogin(', 'getlogin_r('), tomwalters@0: ('getpwnam(', 'getpwnam_r('), tomwalters@0: ('getpwuid(', 'getpwuid_r('), tomwalters@0: ('gmtime(', 'gmtime_r('), tomwalters@0: ('localtime(', 'localtime_r('), tomwalters@0: ('rand(', 'rand_r('), tomwalters@0: ('readdir(', 'readdir_r('), tomwalters@0: ('strtok(', 'strtok_r('), tomwalters@0: ('ttyname(', 'ttyname_r('), tomwalters@0: ) tomwalters@0: tomwalters@0: tomwalters@0: def CheckPosixThreading(filename, clean_lines, linenum, error): tomwalters@0: """Checks for calls to thread-unsafe functions. tomwalters@0: tomwalters@0: Much code has been originally written without consideration of tomwalters@0: multi-threading. Also, engineers are relying on their old experience; tomwalters@0: they have learned posix before threading extensions were added. These tomwalters@0: tests guide the engineers to use thread-safe functions (when using tomwalters@0: posix directly). tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: for single_thread_function, multithread_safe_function in threading_list: tomwalters@0: ix = line.find(single_thread_function) tomwalters@0: # Comparisons made explicit for clarity -- pylint: disable-msg=C6403 tomwalters@0: if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and tomwalters@0: line[ix - 1] not in ('_', '.', '>'))): tomwalters@0: error(filename, linenum, 'runtime/threadsafe_fn', 2, tomwalters@0: 'Consider using ' + multithread_safe_function + tomwalters@0: '...) instead of ' + single_thread_function + tomwalters@0: '...) for improved thread safety.') tomwalters@0: tomwalters@0: tomwalters@0: # Matches invalid increment: *count++, which moves pointer instead of tomwalters@0: # incrementing a value. tomwalters@0: _RE_PATTERN_INVALID_INCREMENT = re.compile( tomwalters@0: r'^\s*\*\w+(\+\+|--);') tomwalters@0: tomwalters@0: tomwalters@0: def CheckInvalidIncrement(filename, clean_lines, linenum, error): tomwalters@0: """Checks for invalid increment *count++. tomwalters@0: tomwalters@0: For example following function: tomwalters@0: void increment_counter(int* count) { tomwalters@0: *count++; tomwalters@0: } tomwalters@0: is invalid, because it effectively does count++, moving pointer, and should tomwalters@0: be replaced with ++*count, (*count)++ or *count += 1. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: if _RE_PATTERN_INVALID_INCREMENT.match(line): tomwalters@0: error(filename, linenum, 'runtime/invalid_increment', 5, tomwalters@0: 'Changing pointer instead of value (or unused value of operator*).') tomwalters@0: tomwalters@0: tomwalters@0: class _ClassInfo(object): tomwalters@0: """Stores information about a class.""" tomwalters@0: tomwalters@0: def __init__(self, name, linenum): tomwalters@0: self.name = name tomwalters@0: self.linenum = linenum tomwalters@0: self.seen_open_brace = False tomwalters@0: self.is_derived = False tomwalters@0: self.virtual_method_linenumber = None tomwalters@0: self.has_virtual_destructor = False tomwalters@0: self.brace_depth = 0 tomwalters@0: tomwalters@0: tomwalters@0: class _ClassState(object): tomwalters@0: """Holds the current state of the parse relating to class declarations. tomwalters@0: tomwalters@0: It maintains a stack of _ClassInfos representing the parser's guess tomwalters@0: as to the current nesting of class declarations. The innermost class tomwalters@0: is at the top (back) of the stack. Typically, the stack will either tomwalters@0: be empty or have exactly one entry. tomwalters@0: """ tomwalters@0: tomwalters@0: def __init__(self): tomwalters@0: self.classinfo_stack = [] tomwalters@0: tomwalters@0: def CheckFinished(self, filename, error): tomwalters@0: """Checks that all classes have been completely parsed. tomwalters@0: tomwalters@0: Call this when all lines in a file have been processed. tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: if self.classinfo_stack: tomwalters@0: # Note: This test can result in false positives if #ifdef constructs tomwalters@0: # get in the way of brace matching. See the testBuildClass test in tomwalters@0: # cpplint_unittest.py for an example of this. tomwalters@0: error(filename, self.classinfo_stack[0].linenum, 'build/class', 5, tomwalters@0: 'Failed to find complete declaration of class %s' % tomwalters@0: self.classinfo_stack[0].name) tomwalters@0: tomwalters@0: tomwalters@0: def CheckForNonStandardConstructs(filename, clean_lines, linenum, tomwalters@0: class_state, error): tomwalters@0: """Logs an error if we see certain non-ANSI constructs ignored by gcc-2. tomwalters@0: tomwalters@0: Complain about several constructs which gcc-2 accepts, but which are tomwalters@0: not standard C++. Warning about these in lint is one way to ease the tomwalters@0: transition to new compilers. tomwalters@0: - put storage class first (e.g. "static const" instead of "const static"). tomwalters@0: - "%lld" instead of %qd" in printf-type functions. tomwalters@0: - "%1$d" is non-standard in printf-type functions. tomwalters@0: - "\%" is an undefined character escape sequence. tomwalters@0: - text after #endif is not allowed. tomwalters@0: - invalid inner-style forward declaration. tomwalters@0: - >? and ?= and )\?=?\s*(\w+|[+-]?\d+)(\.\d*)?', tomwalters@0: line): tomwalters@0: error(filename, linenum, 'build/deprecated', 3, tomwalters@0: '>? and ))?' tomwalters@0: # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;' tomwalters@0: error(filename, linenum, 'runtime/member_string_references', 2, tomwalters@0: 'const string& members are dangerous. It is much better to use ' tomwalters@0: 'alternatives, such as pointers or simple constants.') tomwalters@0: tomwalters@0: # Track class entry and exit, and attempt to find cases within the tomwalters@0: # class declaration that don't meet the C++ style tomwalters@0: # guidelines. Tracking is very dependent on the code matching Google tomwalters@0: # style guidelines, but it seems to perform well enough in testing tomwalters@0: # to be a worthwhile addition to the checks. tomwalters@0: classinfo_stack = class_state.classinfo_stack tomwalters@0: # Look for a class declaration tomwalters@0: class_decl_match = Match( tomwalters@0: r'\s*(template\s*<[\w\s<>,:]*>\s*)?(class|struct)\s+(\w+(::\w+)*)', line) tomwalters@0: if class_decl_match: tomwalters@0: classinfo_stack.append(_ClassInfo(class_decl_match.group(3), linenum)) tomwalters@0: tomwalters@0: # Everything else in this function uses the top of the stack if it's tomwalters@0: # not empty. tomwalters@0: if not classinfo_stack: tomwalters@0: return tomwalters@0: tomwalters@0: classinfo = classinfo_stack[-1] tomwalters@0: tomwalters@0: # If the opening brace hasn't been seen look for it and also tomwalters@0: # parent class declarations. tomwalters@0: if not classinfo.seen_open_brace: tomwalters@0: # If the line has a ';' in it, assume it's a forward declaration or tomwalters@0: # a single-line class declaration, which we won't process. tomwalters@0: if line.find(';') != -1: tomwalters@0: classinfo_stack.pop() tomwalters@0: return tomwalters@0: classinfo.seen_open_brace = (line.find('{') != -1) tomwalters@0: # Look for a bare ':' tomwalters@0: if Search('(^|[^:]):($|[^:])', line): tomwalters@0: classinfo.is_derived = True tomwalters@0: if not classinfo.seen_open_brace: tomwalters@0: return # Everything else in this function is for after open brace tomwalters@0: tomwalters@0: # The class may have been declared with namespace or classname qualifiers. tomwalters@0: # The constructor and destructor will not have those qualifiers. tomwalters@0: base_classname = classinfo.name.split('::')[-1] tomwalters@0: tomwalters@0: # Look for single-argument constructors that aren't marked explicit. tomwalters@0: # Technically a valid construct, but against style. tomwalters@0: args = Match(r'(? 1: tomwalters@0: error(filename, linenum, 'whitespace/todo', 2, tomwalters@0: 'Too many spaces before TODO') tomwalters@0: tomwalters@0: username = match.group(2) tomwalters@0: if not username: tomwalters@0: error(filename, linenum, 'readability/todo', 2, tomwalters@0: 'Missing username in TODO; it should look like ' tomwalters@0: '"// TODO(my_username): Stuff."') tomwalters@0: tomwalters@0: middle_whitespace = match.group(3) tomwalters@0: # Comparisons made explicit for correctness -- pylint: disable-msg=C6403 tomwalters@0: if middle_whitespace != ' ' and middle_whitespace != '': tomwalters@0: error(filename, linenum, 'whitespace/todo', 2, tomwalters@0: 'TODO(my_username) should be followed by a space') tomwalters@0: tomwalters@0: tomwalters@0: def CheckSpacing(filename, clean_lines, linenum, error): tomwalters@0: """Checks for the correctness of various spacing issues in the code. tomwalters@0: tomwalters@0: Things we check for: spaces around operators, spaces after tomwalters@0: if/for/while/switch, no spaces around parens in function calls, two tomwalters@0: spaces between code and comment, don't start a block with a blank tomwalters@0: line, don't end a function with a blank line, don't have too many tomwalters@0: blank lines in a row. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: tomwalters@0: raw = clean_lines.raw_lines tomwalters@0: line = raw[linenum] tomwalters@0: tomwalters@0: # Before nixing comments, check if the line is blank for no good tomwalters@0: # reason. This includes the first line after a block is opened, and tomwalters@0: # blank lines at the end of a function (ie, right before a line like '}' tomwalters@0: if IsBlankLine(line): tomwalters@0: elided = clean_lines.elided tomwalters@0: prev_line = elided[linenum - 1] tomwalters@0: prevbrace = prev_line.rfind('{') tomwalters@0: # TODO(unknown): Don't complain if line before blank line, and line after, tomwalters@0: # both start with alnums and are indented the same amount. tomwalters@0: # This ignores whitespace at the start of a namespace block tomwalters@0: # because those are not usually indented. tomwalters@0: if (prevbrace != -1 and prev_line[prevbrace:].find('}') == -1 tomwalters@0: and prev_line[:prevbrace].find('namespace') == -1): tomwalters@0: # OK, we have a blank line at the start of a code block. Before we tomwalters@0: # complain, we check if it is an exception to the rule: The previous tomwalters@0: # non-empty line has the paramters of a function header that are indented tomwalters@0: # 4 spaces (because they did not fit in a 80 column line when placed on tomwalters@0: # the same line as the function name). We also check for the case where tomwalters@0: # the previous line is indented 6 spaces, which may happen when the tomwalters@0: # initializers of a constructor do not fit into a 80 column line. tomwalters@0: exception = False tomwalters@0: if Match(r' {6}\w', prev_line): # Initializer list? tomwalters@0: # We are looking for the opening column of initializer list, which tomwalters@0: # should be indented 4 spaces to cause 6 space indentation afterwards. tomwalters@0: search_position = linenum-2 tomwalters@0: while (search_position >= 0 tomwalters@0: and Match(r' {6}\w', elided[search_position])): tomwalters@0: search_position -= 1 tomwalters@0: exception = (search_position >= 0 tomwalters@0: and elided[search_position][:5] == ' :') tomwalters@0: else: tomwalters@0: # Search for the function arguments or an initializer list. We use a tomwalters@0: # simple heuristic here: If the line is indented 4 spaces; and we have a tomwalters@0: # closing paren, without the opening paren, followed by an opening brace tomwalters@0: # or colon (for initializer lists) we assume that it is the last line of tomwalters@0: # a function header. If we have a colon indented 4 spaces, it is an tomwalters@0: # initializer list. tomwalters@0: exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)', tomwalters@0: prev_line) tomwalters@0: or Match(r' {4}:', prev_line)) tomwalters@0: tomwalters@0: if not exception: tomwalters@0: error(filename, linenum, 'whitespace/blank_line', 2, tomwalters@0: 'Blank line at the start of a code block. Is this needed?') tomwalters@0: # This doesn't ignore whitespace at the end of a namespace block tomwalters@0: # because that is too hard without pairing open/close braces; tomwalters@0: # however, a special exception is made for namespace closing tomwalters@0: # brackets which have a comment containing "namespace". tomwalters@0: # tomwalters@0: # Also, ignore blank lines at the end of a block in a long if-else tomwalters@0: # chain, like this: tomwalters@0: # if (condition1) { tomwalters@0: # // Something followed by a blank line tomwalters@0: # tomwalters@0: # } else if (condition2) { tomwalters@0: # // Something else tomwalters@0: # } tomwalters@0: if linenum + 1 < clean_lines.NumLines(): tomwalters@0: next_line = raw[linenum + 1] tomwalters@0: if (next_line tomwalters@0: and Match(r'\s*}', next_line) tomwalters@0: and next_line.find('namespace') == -1 tomwalters@0: and next_line.find('} else ') == -1): tomwalters@0: error(filename, linenum, 'whitespace/blank_line', 3, tomwalters@0: 'Blank line at the end of a code block. Is this needed?') tomwalters@0: tomwalters@0: # Next, we complain if there's a comment too near the text tomwalters@0: commentpos = line.find('//') tomwalters@0: if commentpos != -1: tomwalters@0: # Check if the // may be in quotes. If so, ignore it tomwalters@0: # Comparisons made explicit for clarity -- pylint: disable-msg=C6403 tomwalters@0: if (line.count('"', 0, commentpos) - tomwalters@0: line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes tomwalters@0: # Allow one space for new scopes, two spaces otherwise: tomwalters@0: if (not Match(r'^\s*{ //', line) and tomwalters@0: ((commentpos >= 1 and tomwalters@0: line[commentpos-1] not in string.whitespace) or tomwalters@0: (commentpos >= 2 and tomwalters@0: line[commentpos-2] not in string.whitespace))): tomwalters@0: error(filename, linenum, 'whitespace/comments', 2, tomwalters@0: 'At least two spaces is best between code and comments') tomwalters@0: # There should always be a space between the // and the comment tomwalters@0: commentend = commentpos + 2 tomwalters@0: if commentend < len(line) and not line[commentend] == ' ': tomwalters@0: # but some lines are exceptions -- e.g. if they're big tomwalters@0: # comment delimiters like: tomwalters@0: # //---------------------------------------------------------- tomwalters@0: # or they begin with multiple slashes followed by a space: tomwalters@0: # //////// Header comment tomwalters@0: match = (Search(r'[=/-]{4,}\s*$', line[commentend:]) or tomwalters@0: Search(r'^/+ ', line[commentend:])) tomwalters@0: if not match: tomwalters@0: error(filename, linenum, 'whitespace/comments', 4, tomwalters@0: 'Should have a space between // and comment') tomwalters@0: CheckComment(line[commentpos:], filename, linenum, error) tomwalters@0: tomwalters@0: line = clean_lines.elided[linenum] # get rid of comments and strings tomwalters@0: tomwalters@0: # Don't try to do spacing checks for operator methods tomwalters@0: line = re.sub(r'operator(==|!=|<|<<|<=|>=|>>|>)\(', 'operator\(', line) tomwalters@0: tomwalters@0: # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". tomwalters@0: # Otherwise not. Note we only check for non-spaces on *both* sides; tomwalters@0: # sometimes people put non-spaces on one side when aligning ='s among tomwalters@0: # many lines (not that this is behavior that I approve of...) tomwalters@0: if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line): tomwalters@0: error(filename, linenum, 'whitespace/operators', 4, tomwalters@0: 'Missing spaces around =') tomwalters@0: tomwalters@0: # It's ok not to have spaces around binary operators like + - * /, but if tomwalters@0: # there's too little whitespace, we get concerned. It's hard to tell, tomwalters@0: # though, so we punt on this one for now. TODO. tomwalters@0: tomwalters@0: # You should always have whitespace around binary operators. tomwalters@0: # Alas, we can't test < or > because they're legitimately used sans spaces tomwalters@0: # (a->b, vector a). The only time we can tell is a < with no >, and tomwalters@0: # only if it's not template params list spilling into the next line. tomwalters@0: match = Search(r'[^<>=!\s](==|!=|<=|>=)[^<>=!\s]', line) tomwalters@0: if not match: tomwalters@0: # Note that while it seems that the '<[^<]*' term in the following tomwalters@0: # regexp could be simplified to '<.*', which would indeed match tomwalters@0: # the same class of strings, the [^<] means that searching for the tomwalters@0: # regexp takes linear rather than quadratic time. tomwalters@0: if not Search(r'<[^<]*,\s*$', line): # template params spill tomwalters@0: match = Search(r'[^<>=!\s](<)[^<>=!\s]([^>]|->)*$', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'whitespace/operators', 3, tomwalters@0: 'Missing spaces around %s' % match.group(1)) tomwalters@0: # We allow no-spaces around << and >> when used like this: 10<<20, but tomwalters@0: # not otherwise (particularly, not when used as streams) tomwalters@0: match = Search(r'[^0-9\s](<<|>>)[^0-9\s]', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'whitespace/operators', 3, tomwalters@0: 'Missing spaces around %s' % match.group(1)) tomwalters@0: tomwalters@0: # There shouldn't be space around unary operators tomwalters@0: match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'whitespace/operators', 4, tomwalters@0: 'Extra space for operator %s' % match.group(1)) tomwalters@0: tomwalters@0: # A pet peeve of mine: no spaces after an if, while, switch, or for tomwalters@0: match = Search(r' (if\(|for\(|while\(|switch\()', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'whitespace/parens', 5, tomwalters@0: 'Missing space before ( in %s' % match.group(1)) tomwalters@0: tomwalters@0: # For if/for/while/switch, the left and right parens should be tomwalters@0: # consistent about how many spaces are inside the parens, and tomwalters@0: # there should either be zero or one spaces inside the parens. tomwalters@0: # We don't want: "if ( foo)" or "if ( foo )". tomwalters@0: # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed. tomwalters@0: match = Search(r'\b(if|for|while|switch)\s*' tomwalters@0: r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$', tomwalters@0: line) tomwalters@0: if match: tomwalters@0: if len(match.group(2)) != len(match.group(4)): tomwalters@0: if not (match.group(3) == ';' and tomwalters@0: len(match.group(2)) == 1 + len(match.group(4)) or tomwalters@0: not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)): tomwalters@0: error(filename, linenum, 'whitespace/parens', 5, tomwalters@0: 'Mismatching spaces inside () in %s' % match.group(1)) tomwalters@0: if not len(match.group(2)) in [0, 1]: tomwalters@0: error(filename, linenum, 'whitespace/parens', 5, tomwalters@0: 'Should have zero or one spaces inside ( and ) in %s' % tomwalters@0: match.group(1)) tomwalters@0: tomwalters@0: # You should always have a space after a comma (either as fn arg or operator) tomwalters@0: if Search(r',[^\s]', line): tomwalters@0: error(filename, linenum, 'whitespace/comma', 3, tomwalters@0: 'Missing space after ,') tomwalters@0: tomwalters@0: # Next we will look for issues with function calls. tomwalters@0: CheckSpacingForFunctionCall(filename, line, linenum, error) tomwalters@0: tomwalters@0: # Except after an opening paren, you should have spaces before your braces. tomwalters@0: # And since you should never have braces at the beginning of a line, this is tomwalters@0: # an easy test. tomwalters@0: if Search(r'[^ (]{', line): tomwalters@0: error(filename, linenum, 'whitespace/braces', 5, tomwalters@0: 'Missing space before {') tomwalters@0: tomwalters@0: # Make sure '} else {' has spaces. tomwalters@0: if Search(r'}else', line): tomwalters@0: error(filename, linenum, 'whitespace/braces', 5, tomwalters@0: 'Missing space before else') tomwalters@0: tomwalters@0: # You shouldn't have spaces before your brackets, except maybe after tomwalters@0: # 'delete []' or 'new char * []'. tomwalters@0: if Search(r'\w\s+\[', line) and not Search(r'delete\s+\[', line): tomwalters@0: error(filename, linenum, 'whitespace/braces', 5, tomwalters@0: 'Extra space before [') tomwalters@0: tomwalters@0: # You shouldn't have a space before a semicolon at the end of the line. tomwalters@0: # There's a special case for "for" since the style guide allows space before tomwalters@0: # the semicolon there. tomwalters@0: if Search(r':\s*;\s*$', line): tomwalters@0: error(filename, linenum, 'whitespace/semicolon', 5, tomwalters@0: 'Semicolon defining empty statement. Use { } instead.') tomwalters@0: elif Search(r'^\s*;\s*$', line): tomwalters@0: error(filename, linenum, 'whitespace/semicolon', 5, tomwalters@0: 'Line contains only semicolon. If this should be an empty statement, ' tomwalters@0: 'use { } instead.') tomwalters@0: elif (Search(r'\s+;\s*$', line) and tomwalters@0: not Search(r'\bfor\b', line)): tomwalters@0: error(filename, linenum, 'whitespace/semicolon', 5, tomwalters@0: 'Extra space before last semicolon. If this should be an empty ' tomwalters@0: 'statement, use { } instead.') tomwalters@0: tomwalters@0: tomwalters@0: def GetPreviousNonBlankLine(clean_lines, linenum): tomwalters@0: """Return the most recent non-blank line and its line number. tomwalters@0: tomwalters@0: Args: tomwalters@0: clean_lines: A CleansedLines instance containing the file contents. tomwalters@0: linenum: The number of the line to check. tomwalters@0: tomwalters@0: Returns: tomwalters@0: A tuple with two elements. The first element is the contents of the last tomwalters@0: non-blank line before the current line, or the empty string if this is the tomwalters@0: first non-blank line. The second is the line number of that line, or -1 tomwalters@0: if this is the first non-blank line. tomwalters@0: """ tomwalters@0: tomwalters@0: prevlinenum = linenum - 1 tomwalters@0: while prevlinenum >= 0: tomwalters@0: prevline = clean_lines.elided[prevlinenum] tomwalters@0: if not IsBlankLine(prevline): # if not a blank line... tomwalters@0: return (prevline, prevlinenum) tomwalters@0: prevlinenum -= 1 tomwalters@0: return ('', -1) tomwalters@0: tomwalters@0: tomwalters@0: def CheckBraces(filename, clean_lines, linenum, error): tomwalters@0: """Looks for misplaced braces (e.g. at the end of line). tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: tomwalters@0: line = clean_lines.elided[linenum] # get rid of comments and strings tomwalters@0: tomwalters@0: if Match(r'\s*{\s*$', line): tomwalters@0: # We allow an open brace to start a line in the case where someone tomwalters@0: # is using braces in a block to explicitly create a new scope, tomwalters@0: # which is commonly used to control the lifetime of tomwalters@0: # stack-allocated variables. We don't detect this perfectly: we tomwalters@0: # just don't complain if the last non-whitespace character on the tomwalters@0: # previous non-blank line is ';', ':', '{', or '}'. tomwalters@0: prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] tomwalters@0: if not Search(r'[;:}{]\s*$', prevline): tomwalters@0: error(filename, linenum, 'whitespace/braces', 4, tomwalters@0: '{ should almost always be at the end of the previous line') tomwalters@0: tomwalters@0: # An else clause should be on the same line as the preceding closing brace. tomwalters@0: if Match(r'\s*else\s*', line): tomwalters@0: prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] tomwalters@0: if Match(r'\s*}\s*$', prevline): tomwalters@0: error(filename, linenum, 'whitespace/newline', 4, tomwalters@0: 'An else should appear on the same line as the preceding }') tomwalters@0: tomwalters@0: # If braces come on one side of an else, they should be on both. tomwalters@0: # However, we have to worry about "else if" that spans multiple lines! tomwalters@0: if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line): tomwalters@0: if Search(r'}\s*else if([^{]*)$', line): # could be multi-line if tomwalters@0: # find the ( after the if tomwalters@0: pos = line.find('else if') tomwalters@0: pos = line.find('(', pos) tomwalters@0: if pos > 0: tomwalters@0: (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos) tomwalters@0: if endline[endpos:].find('{') == -1: # must be brace after if tomwalters@0: error(filename, linenum, 'readability/braces', 5, tomwalters@0: 'If an else has a brace on one side, it should have it on both') tomwalters@0: else: # common case: else not followed by a multi-line if tomwalters@0: error(filename, linenum, 'readability/braces', 5, tomwalters@0: 'If an else has a brace on one side, it should have it on both') tomwalters@0: tomwalters@0: # Likewise, an else should never have the else clause on the same line tomwalters@0: if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line): tomwalters@0: error(filename, linenum, 'whitespace/newline', 4, tomwalters@0: 'Else clause should never be on same line as else (use 2 lines)') tomwalters@0: tomwalters@0: # In the same way, a do/while should never be on one line tomwalters@0: if Match(r'\s*do [^\s{]', line): tomwalters@0: error(filename, linenum, 'whitespace/newline', 4, tomwalters@0: 'do/while clauses should not be on a single line') tomwalters@0: tomwalters@0: # Braces shouldn't be followed by a ; unless they're defining a struct tomwalters@0: # or initializing an array. tomwalters@0: # We can't tell in general, but we can for some common cases. tomwalters@0: prevlinenum = linenum tomwalters@0: while True: tomwalters@0: (prevline, prevlinenum) = GetPreviousNonBlankLine(clean_lines, prevlinenum) tomwalters@0: if Match(r'\s+{.*}\s*;', line) and not prevline.count(';'): tomwalters@0: line = prevline + line tomwalters@0: else: tomwalters@0: break tomwalters@0: if (Search(r'{.*}\s*;', line) and tomwalters@0: line.count('{') == line.count('}') and tomwalters@0: not Search(r'struct|class|enum|\s*=\s*{', line)): tomwalters@0: error(filename, linenum, 'readability/braces', 4, tomwalters@0: "You don't need a ; after a }") tomwalters@0: tomwalters@0: tomwalters@0: def ReplaceableCheck(operator, macro, line): tomwalters@0: """Determine whether a basic CHECK can be replaced with a more specific one. tomwalters@0: tomwalters@0: For example suggest using CHECK_EQ instead of CHECK(a == b) and tomwalters@0: similarly for CHECK_GE, CHECK_GT, CHECK_LE, CHECK_LT, CHECK_NE. tomwalters@0: tomwalters@0: Args: tomwalters@0: operator: The C++ operator used in the CHECK. tomwalters@0: macro: The CHECK or EXPECT macro being called. tomwalters@0: line: The current source line. tomwalters@0: tomwalters@0: Returns: tomwalters@0: True if the CHECK can be replaced with a more specific one. tomwalters@0: """ tomwalters@0: tomwalters@0: # This matches decimal and hex integers, strings, and chars (in that order). tomwalters@0: match_constant = r'([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')' tomwalters@0: tomwalters@0: # Expression to match two sides of the operator with something that tomwalters@0: # looks like a literal, since CHECK(x == iterator) won't compile. tomwalters@0: # This means we can't catch all the cases where a more specific tomwalters@0: # CHECK is possible, but it's less annoying than dealing with tomwalters@0: # extraneous warnings. tomwalters@0: match_this = (r'\s*' + macro + r'\((\s*' + tomwalters@0: match_constant + r'\s*' + operator + r'[^<>].*|' tomwalters@0: r'.*[^<>]' + operator + r'\s*' + match_constant + tomwalters@0: r'\s*\))') tomwalters@0: tomwalters@0: # Don't complain about CHECK(x == NULL) or similar because tomwalters@0: # CHECK_EQ(x, NULL) won't compile (requires a cast). tomwalters@0: # Also, don't complain about more complex boolean expressions tomwalters@0: # involving && or || such as CHECK(a == b || c == d). tomwalters@0: return Match(match_this, line) and not Search(r'NULL|&&|\|\|', line) tomwalters@0: tomwalters@0: tomwalters@0: def CheckCheck(filename, clean_lines, linenum, error): tomwalters@0: """Checks the use of CHECK and EXPECT macros. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: tomwalters@0: # Decide the set of replacement macros that should be suggested tomwalters@0: raw_lines = clean_lines.raw_lines tomwalters@0: current_macro = '' tomwalters@0: for macro in _CHECK_MACROS: tomwalters@0: if raw_lines[linenum].find(macro) >= 0: tomwalters@0: current_macro = macro tomwalters@0: break tomwalters@0: if not current_macro: tomwalters@0: # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT' tomwalters@0: return tomwalters@0: tomwalters@0: line = clean_lines.elided[linenum] # get rid of comments and strings tomwalters@0: tomwalters@0: # Encourage replacing plain CHECKs with CHECK_EQ/CHECK_NE/etc. tomwalters@0: for operator in ['==', '!=', '>=', '>', '<=', '<']: tomwalters@0: if ReplaceableCheck(operator, current_macro, line): tomwalters@0: error(filename, linenum, 'readability/check', 2, tomwalters@0: 'Consider using %s instead of %s(a %s b)' % ( tomwalters@0: _CHECK_REPLACEMENT[current_macro][operator], tomwalters@0: current_macro, operator)) tomwalters@0: break tomwalters@0: tomwalters@0: tomwalters@0: def GetLineWidth(line): tomwalters@0: """Determines the width of the line in column positions. tomwalters@0: tomwalters@0: Args: tomwalters@0: line: A string, which may be a Unicode string. tomwalters@0: tomwalters@0: Returns: tomwalters@0: The width of the line in column positions, accounting for Unicode tomwalters@0: combining characters and wide characters. tomwalters@0: """ tomwalters@0: if isinstance(line, unicode): tomwalters@0: width = 0 tomwalters@0: for c in unicodedata.normalize('NFC', line): tomwalters@0: if unicodedata.east_asian_width(c) in ('W', 'F'): tomwalters@0: width += 2 tomwalters@0: elif not unicodedata.combining(c): tomwalters@0: width += 1 tomwalters@0: return width tomwalters@0: else: tomwalters@0: return len(line) tomwalters@0: tomwalters@0: tomwalters@0: def CheckStyle(filename, clean_lines, linenum, file_extension, error): tomwalters@0: """Checks rules from the 'C++ style rules' section of cppguide.html. tomwalters@0: tomwalters@0: Most of these rules are hard to test (naming, comment style), but we tomwalters@0: do what we can. In particular we check for 2-space indents, line lengths, tomwalters@0: tab usage, spaces inside code, etc. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: file_extension: The extension (without the dot) of the filename. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: tomwalters@0: raw_lines = clean_lines.raw_lines tomwalters@0: line = raw_lines[linenum] tomwalters@0: tomwalters@0: if line.find('\t') != -1: tomwalters@0: error(filename, linenum, 'whitespace/tab', 1, tomwalters@0: 'Tab found; better to use spaces') tomwalters@0: tomwalters@0: # One or three blank spaces at the beginning of the line is weird; it's tomwalters@0: # hard to reconcile that with 2-space indents. tomwalters@0: # NOTE: here are the conditions rob pike used for his tests. Mine aren't tomwalters@0: # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces tomwalters@0: # if(RLENGTH > 20) complain = 0; tomwalters@0: # if(match($0, " +(error|private|public|protected):")) complain = 0; tomwalters@0: # if(match(prev, "&& *$")) complain = 0; tomwalters@0: # if(match(prev, "\\|\\| *$")) complain = 0; tomwalters@0: # if(match(prev, "[\",=><] *$")) complain = 0; tomwalters@0: # if(match($0, " <<")) complain = 0; tomwalters@0: # if(match(prev, " +for \\(")) complain = 0; tomwalters@0: # if(prevodd && match(prevprev, " +for \\(")) complain = 0; tomwalters@0: initial_spaces = 0 tomwalters@0: cleansed_line = clean_lines.elided[linenum] tomwalters@0: while initial_spaces < len(line) and line[initial_spaces] == ' ': tomwalters@0: initial_spaces += 1 tomwalters@0: if line and line[-1].isspace(): tomwalters@0: error(filename, linenum, 'whitespace/end_of_line', 4, tomwalters@0: 'Line ends in whitespace. Consider deleting these extra spaces.') tomwalters@0: # There are certain situations we allow one space, notably for labels tomwalters@0: elif ((initial_spaces == 1 or initial_spaces == 3) and tomwalters@0: not Match(r'\s*\w+\s*:\s*$', cleansed_line)): tomwalters@0: error(filename, linenum, 'whitespace/indent', 3, tomwalters@0: 'Weird number of spaces at line-start. ' tomwalters@0: 'Are you using a 2-space indent?') tomwalters@0: # Labels should always be indented at least one space. tomwalters@0: elif not initial_spaces and line[:2] != '//' and Search(r'[^:]:\s*$', tomwalters@0: line): tomwalters@0: error(filename, linenum, 'whitespace/labels', 4, tomwalters@0: 'Labels should always be indented at least one space. ' tomwalters@0: 'If this is a member-initializer list in a constructor, ' tomwalters@0: 'the colon should be on the line after the definition header.') tomwalters@0: tomwalters@0: # Check if the line is a header guard. tomwalters@0: is_header_guard = False tomwalters@0: if file_extension == 'h': tomwalters@0: cppvar = GetHeaderGuardCPPVariable(filename) tomwalters@0: if (line.startswith('#ifndef %s' % cppvar) or tomwalters@0: line.startswith('#define %s' % cppvar) or tomwalters@0: line.startswith('#endif // %s' % cppvar)): tomwalters@0: is_header_guard = True tomwalters@0: # #include lines and header guards can be long, since there's no clean way to tomwalters@0: # split them. tomwalters@0: # tomwalters@0: # URLs can be long too. It's possible to split these, but it makes them tomwalters@0: # harder to cut&paste. tomwalters@0: if (not line.startswith('#include') and not is_header_guard and tomwalters@0: not Match(r'^\s*//.*http(s?)://\S*$', line)): tomwalters@0: line_width = GetLineWidth(line) tomwalters@0: if line_width > 100: tomwalters@0: error(filename, linenum, 'whitespace/line_length', 4, tomwalters@0: 'Lines should very rarely be longer than 100 characters') tomwalters@0: elif line_width > 80: tomwalters@0: error(filename, linenum, 'whitespace/line_length', 2, tomwalters@0: 'Lines should be <= 80 characters long') tomwalters@0: tomwalters@0: if (cleansed_line.count(';') > 1 and tomwalters@0: # for loops are allowed two ;'s (and may run over two lines). tomwalters@0: cleansed_line.find('for') == -1 and tomwalters@0: (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or tomwalters@0: GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and tomwalters@0: # It's ok to have many commands in a switch case that fits in 1 line tomwalters@0: not ((cleansed_line.find('case ') != -1 or tomwalters@0: cleansed_line.find('default:') != -1) and tomwalters@0: cleansed_line.find('break;') != -1)): tomwalters@0: error(filename, linenum, 'whitespace/newline', 4, tomwalters@0: 'More than one command on the same line') tomwalters@0: tomwalters@0: # Some more style checks tomwalters@0: CheckBraces(filename, clean_lines, linenum, error) tomwalters@0: CheckSpacing(filename, clean_lines, linenum, error) tomwalters@0: CheckCheck(filename, clean_lines, linenum, error) tomwalters@0: tomwalters@0: tomwalters@0: _RE_PATTERN_INCLUDE_NEW_STYLE = re.compile(r'#include +"[^/]+\.h"') tomwalters@0: _RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$') tomwalters@0: # Matches the first component of a filename delimited by -s and _s. That is: tomwalters@0: # _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo' tomwalters@0: # _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo' tomwalters@0: # _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo' tomwalters@0: # _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo' tomwalters@0: _RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+') tomwalters@0: tomwalters@0: tomwalters@0: def _DropCommonSuffixes(filename): tomwalters@0: """Drops common suffixes like _test.cc or -inl.h from filename. tomwalters@0: tomwalters@0: For example: tomwalters@0: >>> _DropCommonSuffixes('foo/foo-inl.h') tomwalters@0: 'foo/foo' tomwalters@0: >>> _DropCommonSuffixes('foo/bar/foo.cc') tomwalters@0: 'foo/bar/foo' tomwalters@0: >>> _DropCommonSuffixes('foo/foo_internal.h') tomwalters@0: 'foo/foo' tomwalters@0: >>> _DropCommonSuffixes('foo/foo_unusualinternal.h') tomwalters@0: 'foo/foo_unusualinternal' tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The input filename. tomwalters@0: tomwalters@0: Returns: tomwalters@0: The filename with the common suffix removed. tomwalters@0: """ tomwalters@0: for suffix in ('test.cc', 'regtest.cc', 'unittest.cc', tomwalters@0: 'inl.h', 'impl.h', 'internal.h'): tomwalters@0: if (filename.endswith(suffix) and len(filename) > len(suffix) and tomwalters@0: filename[-len(suffix) - 1] in ('-', '_')): tomwalters@0: return filename[:-len(suffix) - 1] tomwalters@0: return os.path.splitext(filename)[0] tomwalters@0: tomwalters@0: tomwalters@0: def _IsTestFilename(filename): tomwalters@0: """Determines if the given filename has a suffix that identifies it as a test. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The input filename. tomwalters@0: tomwalters@0: Returns: tomwalters@0: True if 'filename' looks like a test, False otherwise. tomwalters@0: """ tomwalters@0: if (filename.endswith('_test.cc') or tomwalters@0: filename.endswith('_unittest.cc') or tomwalters@0: filename.endswith('_regtest.cc')): tomwalters@0: return True tomwalters@0: else: tomwalters@0: return False tomwalters@0: tomwalters@0: tomwalters@0: def _ClassifyInclude(fileinfo, include, is_system): tomwalters@0: """Figures out what kind of header 'include' is. tomwalters@0: tomwalters@0: Args: tomwalters@0: fileinfo: The current file cpplint is running over. A FileInfo instance. tomwalters@0: include: The path to a #included file. tomwalters@0: is_system: True if the #include used <> rather than "". tomwalters@0: tomwalters@0: Returns: tomwalters@0: One of the _XXX_HEADER constants. tomwalters@0: tomwalters@0: For example: tomwalters@0: >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True) tomwalters@0: _C_SYS_HEADER tomwalters@0: >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True) tomwalters@0: _CPP_SYS_HEADER tomwalters@0: >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False) tomwalters@0: _LIKELY_MY_HEADER tomwalters@0: >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'), tomwalters@0: ... 'bar/foo_other_ext.h', False) tomwalters@0: _POSSIBLE_MY_HEADER tomwalters@0: >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False) tomwalters@0: _OTHER_HEADER tomwalters@0: """ tomwalters@0: # This is a list of all standard c++ header files, except tomwalters@0: # those already checked for above. tomwalters@0: is_stl_h = include in _STL_HEADERS tomwalters@0: is_cpp_h = is_stl_h or include in _CPP_HEADERS tomwalters@0: tomwalters@0: if is_system: tomwalters@0: if is_cpp_h: tomwalters@0: return _CPP_SYS_HEADER tomwalters@0: else: tomwalters@0: return _C_SYS_HEADER tomwalters@0: tomwalters@0: # If the target file and the include we're checking share a tomwalters@0: # basename when we drop common extensions, and the include tomwalters@0: # lives in . , then it's likely to be owned by the target file. tomwalters@0: target_dir, target_base = ( tomwalters@0: os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName()))) tomwalters@0: include_dir, include_base = os.path.split(_DropCommonSuffixes(include)) tomwalters@0: if target_base == include_base and ( tomwalters@0: include_dir == target_dir or tomwalters@0: include_dir == os.path.normpath(target_dir + '/../public')): tomwalters@0: return _LIKELY_MY_HEADER tomwalters@0: tomwalters@0: # If the target and include share some initial basename tomwalters@0: # component, it's possible the target is implementing the tomwalters@0: # include, so it's allowed to be first, but we'll never tomwalters@0: # complain if it's not there. tomwalters@0: target_first_component = _RE_FIRST_COMPONENT.match(target_base) tomwalters@0: include_first_component = _RE_FIRST_COMPONENT.match(include_base) tomwalters@0: if (target_first_component and include_first_component and tomwalters@0: target_first_component.group(0) == tomwalters@0: include_first_component.group(0)): tomwalters@0: return _POSSIBLE_MY_HEADER tomwalters@0: tomwalters@0: return _OTHER_HEADER tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): tomwalters@0: """Check rules that are applicable to #include lines. tomwalters@0: tomwalters@0: Strings on #include lines are NOT removed from elided line, to make tomwalters@0: certain tasks easier. However, to prevent false positives, checks tomwalters@0: applicable to #include lines in CheckLanguage must be put here. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: include_state: An _IncludeState instance in which the headers are inserted. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: fileinfo = FileInfo(filename) tomwalters@0: tomwalters@0: line = clean_lines.lines[linenum] tomwalters@0: tomwalters@0: # "include" should use the new style "foo/bar.h" instead of just "bar.h" tomwalters@0: if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line): tomwalters@0: error(filename, linenum, 'build/include', 4, tomwalters@0: 'Include the directory when naming .h files') tomwalters@0: tomwalters@0: # we shouldn't include a file more than once. actually, there are a tomwalters@0: # handful of instances where doing so is okay, but in general it's tomwalters@0: # not. tomwalters@0: match = _RE_PATTERN_INCLUDE.search(line) tomwalters@0: if match: tomwalters@0: include = match.group(2) tomwalters@0: is_system = (match.group(1) == '<') tomwalters@0: if include in include_state: tomwalters@0: error(filename, linenum, 'build/include', 4, tomwalters@0: '"%s" already included at %s:%s' % tomwalters@0: (include, filename, include_state[include])) tomwalters@0: else: tomwalters@0: include_state[include] = linenum tomwalters@0: tomwalters@0: # We want to ensure that headers appear in the right order: tomwalters@0: # 1) for foo.cc, foo.h (preferred location) tomwalters@0: # 2) c system files tomwalters@0: # 3) cpp system files tomwalters@0: # 4) for foo.cc, foo.h (deprecated location) tomwalters@0: # 5) other google headers tomwalters@0: # tomwalters@0: # We classify each include statement as one of those 5 types tomwalters@0: # using a number of techniques. The include_state object keeps tomwalters@0: # track of the highest type seen, and complains if we see a tomwalters@0: # lower type after that. tomwalters@0: error_message = include_state.CheckNextIncludeOrder( tomwalters@0: _ClassifyInclude(fileinfo, include, is_system)) tomwalters@0: if error_message: tomwalters@0: error(filename, linenum, 'build/include_order', 4, tomwalters@0: '%s. Should be: %s.h, c system, c++ system, other.' % tomwalters@0: (error_message, fileinfo.BaseName())) tomwalters@0: if not include_state.IsInAlphabeticalOrder(include): tomwalters@0: error(filename, linenum, 'build/include_alpha', 4, tomwalters@0: 'Include "%s" not in alphabetical order' % include) tomwalters@0: tomwalters@0: # Look for any of the stream classes that are part of standard C++. tomwalters@0: match = _RE_PATTERN_INCLUDE.match(line) tomwalters@0: if match: tomwalters@0: include = match.group(2) tomwalters@0: if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include): tomwalters@0: # Many unit tests use cout, so we exempt them. tomwalters@0: if not _IsTestFilename(filename): tomwalters@0: error(filename, linenum, 'readability/streams', 3, tomwalters@0: 'Streams are highly discouraged.') tomwalters@0: tomwalters@0: def CheckLanguage(filename, clean_lines, linenum, file_extension, include_state, tomwalters@0: error): tomwalters@0: """Checks rules from the 'C++ language rules' section of cppguide.html. tomwalters@0: tomwalters@0: Some of these rules are hard to test (function overloading, using tomwalters@0: uint32 inappropriately), but we do the best we can. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: file_extension: The extension (without the dot) of the filename. tomwalters@0: include_state: An _IncludeState instance in which the headers are inserted. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: # If the line is empty or consists of entirely a comment, no need to tomwalters@0: # check it. tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: if not line: tomwalters@0: return tomwalters@0: tomwalters@0: match = _RE_PATTERN_INCLUDE.search(line) tomwalters@0: if match: tomwalters@0: CheckIncludeLine(filename, clean_lines, linenum, include_state, error) tomwalters@0: return tomwalters@0: tomwalters@0: # Create an extended_line, which is the concatenation of the current and tomwalters@0: # next lines, for more effective checking of code that may span more than one tomwalters@0: # line. tomwalters@0: if linenum + 1 < clean_lines.NumLines(): tomwalters@0: extended_line = line + clean_lines.elided[linenum + 1] tomwalters@0: else: tomwalters@0: extended_line = line tomwalters@0: tomwalters@0: # Make Windows paths like Unix. tomwalters@0: fullname = os.path.abspath(filename).replace('\\', '/') tomwalters@0: tomwalters@0: # TODO(unknown): figure out if they're using default arguments in fn proto. tomwalters@0: tomwalters@0: # Check for non-const references in functions. This is tricky because & tomwalters@0: # is also used to take the address of something. We allow <> for templates, tomwalters@0: # (ignoring whatever is between the braces) and : for classes. tomwalters@0: # These are complicated re's. They try to capture the following: tomwalters@0: # paren (for fn-prototype start), typename, &, varname. For the const tomwalters@0: # version, we're willing for const to be before typename or after tomwalters@0: # Don't check the implemention on same line. tomwalters@0: fnline = line.split('{', 1)[0] tomwalters@0: if (len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) > tomwalters@0: len(re.findall(r'\([^()]*\bconst\s+(?:typename\s+)?(?:struct\s+)?' tomwalters@0: r'(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) + tomwalters@0: len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+\s+const(\s?&|&\s?)[\w]+', tomwalters@0: fnline))): tomwalters@0: tomwalters@0: # We allow non-const references in a few standard places, like functions tomwalters@0: # called "swap()" or iostream operators like "<<" or ">>". tomwalters@0: if not Search( tomwalters@0: r'(swap|Swap|operator[<>][<>])\s*\(\s*(?:[\w:]|<.*>)+\s*&', tomwalters@0: fnline): tomwalters@0: error(filename, linenum, 'runtime/references', 2, tomwalters@0: 'Is this a non-const reference? ' tomwalters@0: 'If so, make const or use a pointer.') tomwalters@0: tomwalters@0: # Check to see if they're using an conversion function cast. tomwalters@0: # I just try to capture the most common basic types, though there are more. tomwalters@0: # Parameterless conversion functions, such as bool(), are allowed as they are tomwalters@0: # probably a member operator declaration or default constructor. tomwalters@0: match = Search( tomwalters@0: r'(\bnew\s+)?\b' # Grab 'new' operator, if it's there tomwalters@0: r'(int|float|double|bool|char|int32|uint32|int64|uint64)\([^)]', line) tomwalters@0: if match: tomwalters@0: # gMock methods are defined using some variant of MOCK_METHODx(name, type) tomwalters@0: # where type may be float(), int(string), etc. Without context they are tomwalters@0: # virtually indistinguishable from int(x) casts. tomwalters@0: if (match.group(1) is None and # If new operator, then this isn't a cast tomwalters@0: not Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line)): tomwalters@0: error(filename, linenum, 'readability/casting', 4, tomwalters@0: 'Using deprecated casting style. ' tomwalters@0: 'Use static_cast<%s>(...) instead' % tomwalters@0: match.group(2)) tomwalters@0: tomwalters@0: CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum], tomwalters@0: 'static_cast', tomwalters@0: r'\((int|float|double|bool|char|u?int(16|32|64))\)', tomwalters@0: error) tomwalters@0: # This doesn't catch all cases. Consider (const char * const)"hello". tomwalters@0: CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum], tomwalters@0: 'reinterpret_cast', r'\((\w+\s?\*+\s?)\)', error) tomwalters@0: tomwalters@0: # In addition, we look for people taking the address of a cast. This tomwalters@0: # is dangerous -- casts can assign to temporaries, so the pointer doesn't tomwalters@0: # point where you think. tomwalters@0: if Search( tomwalters@0: r'(&\([^)]+\)[\w(])|(&(static|dynamic|reinterpret)_cast\b)', line): tomwalters@0: error(filename, linenum, 'runtime/casting', 4, tomwalters@0: ('Are you taking an address of a cast? ' tomwalters@0: 'This is dangerous: could be a temp var. ' tomwalters@0: 'Take the address before doing the cast, rather than after')) tomwalters@0: tomwalters@0: # Check for people declaring static/global STL strings at the top level. tomwalters@0: # This is dangerous because the C++ language does not guarantee that tomwalters@0: # globals with constructors are initialized before the first access. tomwalters@0: match = Match( tomwalters@0: r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)', tomwalters@0: line) tomwalters@0: # Make sure it's not a function. tomwalters@0: # Function template specialization looks like: "string foo(...". tomwalters@0: # Class template definitions look like: "string Foo::Method(...". tomwalters@0: if match and not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)?\s*\(([^"]|$)', tomwalters@0: match.group(3)): tomwalters@0: error(filename, linenum, 'runtime/string', 4, tomwalters@0: 'For a static/global string constant, use a C style string instead: ' tomwalters@0: '"%schar %s[]".' % tomwalters@0: (match.group(1), match.group(2))) tomwalters@0: tomwalters@0: # Check that we're not using RTTI outside of testing code. tomwalters@0: if Search(r'\bdynamic_cast<', line) and not _IsTestFilename(filename): tomwalters@0: error(filename, linenum, 'runtime/rtti', 5, tomwalters@0: 'Do not use dynamic_cast<>. If you need to cast within a class ' tomwalters@0: "hierarchy, use static_cast<> to upcast. Google doesn't support " tomwalters@0: 'RTTI.') tomwalters@0: tomwalters@0: if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line): tomwalters@0: error(filename, linenum, 'runtime/init', 4, tomwalters@0: 'You seem to be initializing a member variable with itself.') tomwalters@0: tomwalters@0: if file_extension == 'h': tomwalters@0: # TODO(unknown): check that 1-arg constructors are explicit. tomwalters@0: # How to tell it's a constructor? tomwalters@0: # (handled in CheckForNonStandardConstructs for now) tomwalters@0: # TODO(unknown): check that classes have DISALLOW_EVIL_CONSTRUCTORS tomwalters@0: # (level 1 error) tomwalters@0: pass tomwalters@0: tomwalters@0: # Check if people are using the verboten C basic types. The only exception tomwalters@0: # we regularly allow is "unsigned short port" for port. tomwalters@0: if Search(r'\bshort port\b', line): tomwalters@0: if not Search(r'\bunsigned short port\b', line): tomwalters@0: error(filename, linenum, 'runtime/int', 4, tomwalters@0: 'Use "unsigned short" for ports, not "short"') tomwalters@0: else: tomwalters@0: match = Search(r'\b(short|long(?! +double)|long long)\b', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'runtime/int', 4, tomwalters@0: 'Use int16/int64/etc, rather than the C type %s' % match.group(1)) tomwalters@0: tomwalters@0: # When snprintf is used, the second argument shouldn't be a literal. tomwalters@0: match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'runtime/printf', 3, tomwalters@0: 'If you can, use sizeof(%s) instead of %s as the 2nd arg ' tomwalters@0: 'to snprintf.' % (match.group(1), match.group(2))) tomwalters@0: tomwalters@0: # Check if some verboten C functions are being used. tomwalters@0: if Search(r'\bsprintf\b', line): tomwalters@0: error(filename, linenum, 'runtime/printf', 5, tomwalters@0: 'Never use sprintf. Use snprintf instead.') tomwalters@0: match = Search(r'\b(strcpy|strcat)\b', line) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'runtime/printf', 4, tomwalters@0: 'Almost always, snprintf is better than %s' % match.group(1)) tomwalters@0: tomwalters@0: if Search(r'\bsscanf\b', line): tomwalters@0: error(filename, linenum, 'runtime/printf', 1, tomwalters@0: 'sscanf can be ok, but is slow and can overflow buffers.') tomwalters@0: tomwalters@0: # Check if some verboten operator overloading is going on tomwalters@0: # TODO(unknown): catch out-of-line unary operator&: tomwalters@0: # class X {}; tomwalters@0: # int operator&(const X& x) { return 42; } // unary operator& tomwalters@0: # The trick is it's hard to tell apart from binary operator&: tomwalters@0: # class Y { int operator&(const Y& x) { return 23; } }; // binary operator& tomwalters@0: if Search(r'\boperator\s*&\s*\(\s*\)', line): tomwalters@0: error(filename, linenum, 'runtime/operator', 4, tomwalters@0: 'Unary operator& is dangerous. Do not use it.') tomwalters@0: tomwalters@0: # Check for suspicious usage of "if" like tomwalters@0: # } if (a == b) { tomwalters@0: if Search(r'\}\s*if\s*\(', line): tomwalters@0: error(filename, linenum, 'readability/braces', 4, tomwalters@0: 'Did you mean "else if"? If not, start a new line for "if".') tomwalters@0: tomwalters@0: # Check for potential format string bugs like printf(foo). tomwalters@0: # We constrain the pattern not to pick things like DocidForPrintf(foo). tomwalters@0: # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str()) tomwalters@0: match = re.search(r'\b((?:string)?printf)\s*\(([\w.\->()]+)\)', line, re.I) tomwalters@0: if match: tomwalters@0: error(filename, linenum, 'runtime/printf', 4, tomwalters@0: 'Potential format string bug. Do %s("%%s", %s) instead.' tomwalters@0: % (match.group(1), match.group(2))) tomwalters@0: tomwalters@0: # Check for potential memset bugs like memset(buf, sizeof(buf), 0). tomwalters@0: match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line) tomwalters@0: if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)): tomwalters@0: error(filename, linenum, 'runtime/memset', 4, tomwalters@0: 'Did you mean "memset(%s, 0, %s)"?' tomwalters@0: % (match.group(1), match.group(2))) tomwalters@0: tomwalters@0: if Search(r'\busing namespace\b', line): tomwalters@0: error(filename, linenum, 'build/namespaces', 5, tomwalters@0: 'Do not use namespace using-directives. ' tomwalters@0: 'Use using-declarations instead.') tomwalters@0: tomwalters@0: # Detect variable-length arrays. tomwalters@0: match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line) tomwalters@0: if (match and match.group(2) != 'return' and match.group(2) != 'delete' and tomwalters@0: match.group(3).find(']') == -1): tomwalters@0: # Split the size using space and arithmetic operators as delimiters. tomwalters@0: # If any of the resulting tokens are not compile time constants then tomwalters@0: # report the error. tomwalters@0: tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3)) tomwalters@0: is_const = True tomwalters@0: skip_next = False tomwalters@0: for tok in tokens: tomwalters@0: if skip_next: tomwalters@0: skip_next = False tomwalters@0: continue tomwalters@0: tomwalters@0: if Search(r'sizeof\(.+\)', tok): continue tomwalters@0: if Search(r'arraysize\(\w+\)', tok): continue tomwalters@0: tomwalters@0: tok = tok.lstrip('(') tomwalters@0: tok = tok.rstrip(')') tomwalters@0: if not tok: continue tomwalters@0: if Match(r'\d+', tok): continue tomwalters@0: if Match(r'0[xX][0-9a-fA-F]+', tok): continue tomwalters@0: if Match(r'k[A-Z0-9]\w*', tok): continue tomwalters@0: if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue tomwalters@0: if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue tomwalters@0: # A catch all for tricky sizeof cases, including 'sizeof expression', tomwalters@0: # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)' tomwalters@0: # requires skipping the next token becasue we split on ' ' and '*'. tomwalters@0: if tok.startswith('sizeof'): tomwalters@0: skip_next = True tomwalters@0: continue tomwalters@0: is_const = False tomwalters@0: break tomwalters@0: if not is_const: tomwalters@0: error(filename, linenum, 'runtime/arrays', 1, tomwalters@0: 'Do not use variable-length arrays. Use an appropriately named ' tomwalters@0: "('k' followed by CamelCase) compile-time constant for the size.") tomwalters@0: tomwalters@0: # If DISALLOW_EVIL_CONSTRUCTORS, DISALLOW_COPY_AND_ASSIGN, or tomwalters@0: # DISALLOW_IMPLICIT_CONSTRUCTORS is present, then it should be the last thing tomwalters@0: # in the class declaration. tomwalters@0: match = Match( tomwalters@0: (r'\s*' tomwalters@0: r'(DISALLOW_(EVIL_CONSTRUCTORS|COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))' tomwalters@0: r'\(.*\);$'), tomwalters@0: line) tomwalters@0: if match and linenum + 1 < clean_lines.NumLines(): tomwalters@0: next_line = clean_lines.elided[linenum + 1] tomwalters@0: if not Search(r'^\s*};', next_line): tomwalters@0: error(filename, linenum, 'readability/constructors', 3, tomwalters@0: match.group(1) + ' should be the last thing in the class') tomwalters@0: tomwalters@0: # Check for use of unnamed namespaces in header files. Registration tomwalters@0: # macros are typically OK, so we allow use of "namespace {" on lines tomwalters@0: # that end with backslashes. tomwalters@0: if (file_extension == 'h' tomwalters@0: and Search(r'\bnamespace\s*{', line) tomwalters@0: and line[-1] != '\\'): tomwalters@0: error(filename, linenum, 'build/namespaces', 4, tomwalters@0: 'Do not use unnamed namespaces in header files. See ' tomwalters@0: 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces' tomwalters@0: ' for more information.') tomwalters@0: tomwalters@0: tomwalters@0: def CheckCStyleCast(filename, linenum, line, raw_line, cast_type, pattern, tomwalters@0: error): tomwalters@0: """Checks for a C-style cast by looking for the pattern. tomwalters@0: tomwalters@0: This also handles sizeof(type) warnings, due to similarity of content. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: linenum: The number of the line to check. tomwalters@0: line: The line of code to check. tomwalters@0: raw_line: The raw line of code to check, with comments. tomwalters@0: cast_type: The string for the C++ cast to recommend. This is either tomwalters@0: reinterpret_cast or static_cast, depending. tomwalters@0: pattern: The regular expression used to find C-style casts. tomwalters@0: error: The function to call with any errors found. tomwalters@0: """ tomwalters@0: match = Search(pattern, line) tomwalters@0: if not match: tomwalters@0: return tomwalters@0: tomwalters@0: # e.g., sizeof(int) tomwalters@0: sizeof_match = Match(r'.*sizeof\s*$', line[0:match.start(1) - 1]) tomwalters@0: if sizeof_match: tomwalters@0: error(filename, linenum, 'runtime/sizeof', 1, tomwalters@0: 'Using sizeof(type). Use sizeof(varname) instead if possible') tomwalters@0: return tomwalters@0: tomwalters@0: remainder = line[match.end(0):] tomwalters@0: tomwalters@0: # The close paren is for function pointers as arguments to a function. tomwalters@0: # eg, void foo(void (*bar)(int)); tomwalters@0: # The semicolon check is a more basic function check; also possibly a tomwalters@0: # function pointer typedef. tomwalters@0: # eg, void foo(int); or void foo(int) const; tomwalters@0: # The equals check is for function pointer assignment. tomwalters@0: # eg, void *(*foo)(int) = ... tomwalters@0: # tomwalters@0: # Right now, this will only catch cases where there's a single argument, and tomwalters@0: # it's unnamed. It should probably be expanded to check for multiple tomwalters@0: # arguments with some unnamed. tomwalters@0: function_match = Match(r'\s*(\)|=|(const)?\s*(;|\{|throw\(\)))', remainder) tomwalters@0: if function_match: tomwalters@0: if (not function_match.group(3) or tomwalters@0: function_match.group(3) == ';' or tomwalters@0: raw_line.find('/*') < 0): tomwalters@0: error(filename, linenum, 'readability/function', 3, tomwalters@0: 'All parameters should be named in a function') tomwalters@0: return tomwalters@0: tomwalters@0: # At this point, all that should be left is actual casts. tomwalters@0: error(filename, linenum, 'readability/casting', 4, tomwalters@0: 'Using C-style cast. Use %s<%s>(...) instead' % tomwalters@0: (cast_type, match.group(1))) tomwalters@0: tomwalters@0: tomwalters@0: _HEADERS_CONTAINING_TEMPLATES = ( tomwalters@0: ('', ('deque',)), tomwalters@0: ('', ('unary_function', 'binary_function', tomwalters@0: 'plus', 'minus', 'multiplies', 'divides', 'modulus', tomwalters@0: 'negate', tomwalters@0: 'equal_to', 'not_equal_to', 'greater', 'less', tomwalters@0: 'greater_equal', 'less_equal', tomwalters@0: 'logical_and', 'logical_or', 'logical_not', tomwalters@0: 'unary_negate', 'not1', 'binary_negate', 'not2', tomwalters@0: 'bind1st', 'bind2nd', tomwalters@0: 'pointer_to_unary_function', tomwalters@0: 'pointer_to_binary_function', tomwalters@0: 'ptr_fun', tomwalters@0: 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t', tomwalters@0: 'mem_fun_ref_t', tomwalters@0: 'const_mem_fun_t', 'const_mem_fun1_t', tomwalters@0: 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t', tomwalters@0: 'mem_fun_ref', tomwalters@0: )), tomwalters@0: ('', ('numeric_limits',)), tomwalters@0: ('', ('list',)), tomwalters@0: ('', ('map', 'multimap',)), tomwalters@0: ('', ('allocator',)), tomwalters@0: ('', ('queue', 'priority_queue',)), tomwalters@0: ('', ('set', 'multiset',)), tomwalters@0: ('', ('stack',)), tomwalters@0: ('', ('char_traits', 'basic_string',)), tomwalters@0: ('', ('pair',)), tomwalters@0: ('', ('vector',)), tomwalters@0: tomwalters@0: # gcc extensions. tomwalters@0: # Note: std::hash is their hash, ::hash is our hash tomwalters@0: ('', ('hash_map', 'hash_multimap',)), tomwalters@0: ('', ('hash_set', 'hash_multiset',)), tomwalters@0: ('', ('slist',)), tomwalters@0: ) tomwalters@0: tomwalters@0: _HEADERS_ACCEPTED_BUT_NOT_PROMOTED = { tomwalters@0: # We can trust with reasonable confidence that map gives us pair<>, too. tomwalters@0: 'pair<>': ('map', 'multimap', 'hash_map', 'hash_multimap') tomwalters@0: } tomwalters@0: tomwalters@0: _RE_PATTERN_STRING = re.compile(r'\bstring\b') tomwalters@0: tomwalters@0: _re_pattern_algorithm_header = [] tomwalters@0: for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap', tomwalters@0: 'transform'): tomwalters@0: # Match max(..., ...), max(..., ...), but not foo->max, foo.max or tomwalters@0: # type::max(). tomwalters@0: _re_pattern_algorithm_header.append( tomwalters@0: (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'), tomwalters@0: _template, tomwalters@0: '')) tomwalters@0: tomwalters@0: _re_pattern_templates = [] tomwalters@0: for _header, _templates in _HEADERS_CONTAINING_TEMPLATES: tomwalters@0: for _template in _templates: tomwalters@0: _re_pattern_templates.append( tomwalters@0: (re.compile(r'(\<|\b)' + _template + r'\s*\<'), tomwalters@0: _template + '<>', tomwalters@0: _header)) tomwalters@0: tomwalters@0: tomwalters@0: def FilesBelongToSameModule(filename_cc, filename_h): tomwalters@0: """Check if these two filenames belong to the same module. tomwalters@0: tomwalters@0: The concept of a 'module' here is a as follows: tomwalters@0: foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the tomwalters@0: same 'module' if they are in the same directory. tomwalters@0: some/path/public/xyzzy and some/path/internal/xyzzy are also considered tomwalters@0: to belong to the same module here. tomwalters@0: tomwalters@0: If the filename_cc contains a longer path than the filename_h, for example, tomwalters@0: '/absolute/path/to/base/sysinfo.cc', and this file would include tomwalters@0: 'base/sysinfo.h', this function also produces the prefix needed to open the tomwalters@0: header. This is used by the caller of this function to more robustly open the tomwalters@0: header file. We don't have access to the real include paths in this context, tomwalters@0: so we need this guesswork here. tomwalters@0: tomwalters@0: Known bugs: tools/base/bar.cc and base/bar.h belong to the same module tomwalters@0: according to this implementation. Because of this, this function gives tomwalters@0: some false positives. This should be sufficiently rare in practice. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename_cc: is the path for the .cc file tomwalters@0: filename_h: is the path for the header path tomwalters@0: tomwalters@0: Returns: tomwalters@0: Tuple with a bool and a string: tomwalters@0: bool: True if filename_cc and filename_h belong to the same module. tomwalters@0: string: the additional prefix needed to open the header file. tomwalters@0: """ tomwalters@0: tomwalters@0: if not filename_cc.endswith('.cc'): tomwalters@0: return (False, '') tomwalters@0: filename_cc = filename_cc[:-len('.cc')] tomwalters@0: if filename_cc.endswith('_unittest'): tomwalters@0: filename_cc = filename_cc[:-len('_unittest')] tomwalters@0: elif filename_cc.endswith('_test'): tomwalters@0: filename_cc = filename_cc[:-len('_test')] tomwalters@0: filename_cc = filename_cc.replace('/public/', '/') tomwalters@0: filename_cc = filename_cc.replace('/internal/', '/') tomwalters@0: tomwalters@0: if not filename_h.endswith('.h'): tomwalters@0: return (False, '') tomwalters@0: filename_h = filename_h[:-len('.h')] tomwalters@0: if filename_h.endswith('-inl'): tomwalters@0: filename_h = filename_h[:-len('-inl')] tomwalters@0: filename_h = filename_h.replace('/public/', '/') tomwalters@0: filename_h = filename_h.replace('/internal/', '/') tomwalters@0: tomwalters@0: files_belong_to_same_module = filename_cc.endswith(filename_h) tomwalters@0: common_path = '' tomwalters@0: if files_belong_to_same_module: tomwalters@0: common_path = filename_cc[:-len(filename_h)] tomwalters@0: return files_belong_to_same_module, common_path tomwalters@0: tomwalters@0: tomwalters@0: def UpdateIncludeState(filename, include_state, io=codecs): tomwalters@0: """Fill up the include_state with new includes found from the file. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: the name of the header to read. tomwalters@0: include_state: an _IncludeState instance in which the headers are inserted. tomwalters@0: io: The io factory to use to read the file. Provided for testability. tomwalters@0: tomwalters@0: Returns: tomwalters@0: True if a header was succesfully added. False otherwise. tomwalters@0: """ tomwalters@0: headerfile = None tomwalters@0: try: tomwalters@0: headerfile = io.open(filename, 'r', 'utf8', 'replace') tomwalters@0: except IOError: tomwalters@0: return False tomwalters@0: linenum = 0 tomwalters@0: for line in headerfile: tomwalters@0: linenum += 1 tomwalters@0: clean_line = CleanseComments(line) tomwalters@0: match = _RE_PATTERN_INCLUDE.search(clean_line) tomwalters@0: if match: tomwalters@0: include = match.group(2) tomwalters@0: # The value formatting is cute, but not really used right now. tomwalters@0: # What matters here is that the key is in include_state. tomwalters@0: include_state.setdefault(include, '%s:%d' % (filename, linenum)) tomwalters@0: return True tomwalters@0: tomwalters@0: tomwalters@0: def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, tomwalters@0: io=codecs): tomwalters@0: """Reports for missing stl includes. tomwalters@0: tomwalters@0: This function will output warnings to make sure you are including the headers tomwalters@0: necessary for the stl containers and functions that you use. We only give one tomwalters@0: reason to include a header. For example, if you use both equal_to<> and tomwalters@0: less<> in a .h file, only one (the latter in the file) of these will be tomwalters@0: reported as a reason to include the . tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the current file. tomwalters@0: clean_lines: A CleansedLines instance containing the file. tomwalters@0: include_state: An _IncludeState instance. tomwalters@0: error: The function to call with any errors found. tomwalters@0: io: The IO factory to use to read the header file. Provided for unittest tomwalters@0: injection. tomwalters@0: """ tomwalters@0: required = {} # A map of header name to linenumber and the template entity. tomwalters@0: # Example of required: { '': (1219, 'less<>') } tomwalters@0: tomwalters@0: for linenum in xrange(clean_lines.NumLines()): tomwalters@0: line = clean_lines.elided[linenum] tomwalters@0: if not line or line[0] == '#': tomwalters@0: continue tomwalters@0: tomwalters@0: # String is special -- it is a non-templatized type in STL. tomwalters@0: if _RE_PATTERN_STRING.search(line): tomwalters@0: required[''] = (linenum, 'string') tomwalters@0: tomwalters@0: for pattern, template, header in _re_pattern_algorithm_header: tomwalters@0: if pattern.search(line): tomwalters@0: required[header] = (linenum, template) tomwalters@0: tomwalters@0: # The following function is just a speed up, no semantics are changed. tomwalters@0: if not '<' in line: # Reduces the cpu time usage by skipping lines. tomwalters@0: continue tomwalters@0: tomwalters@0: for pattern, template, header in _re_pattern_templates: tomwalters@0: if pattern.search(line): tomwalters@0: required[header] = (linenum, template) tomwalters@0: tomwalters@0: # The policy is that if you #include something in foo.h you don't need to tomwalters@0: # include it again in foo.cc. Here, we will look at possible includes. tomwalters@0: # Let's copy the include_state so it is only messed up within this function. tomwalters@0: include_state = include_state.copy() tomwalters@0: tomwalters@0: # Did we find the header for this file (if any) and succesfully load it? tomwalters@0: header_found = False tomwalters@0: tomwalters@0: # Use the absolute path so that matching works properly. tomwalters@0: abs_filename = os.path.abspath(filename) tomwalters@0: tomwalters@0: # For Emacs's flymake. tomwalters@0: # If cpplint is invoked from Emacs's flymake, a temporary file is generated tomwalters@0: # by flymake and that file name might end with '_flymake.cc'. In that case, tomwalters@0: # restore original file name here so that the corresponding header file can be tomwalters@0: # found. tomwalters@0: # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h' tomwalters@0: # instead of 'foo_flymake.h' tomwalters@0: emacs_flymake_suffix = '_flymake.cc' tomwalters@0: if abs_filename.endswith(emacs_flymake_suffix): tomwalters@0: abs_filename = abs_filename[:-len(emacs_flymake_suffix)] + '.cc' tomwalters@0: tomwalters@0: # include_state is modified during iteration, so we iterate over a copy of tomwalters@0: # the keys. tomwalters@0: for header in include_state.keys(): #NOLINT tomwalters@0: (same_module, common_path) = FilesBelongToSameModule(abs_filename, header) tomwalters@0: fullpath = common_path + header tomwalters@0: if same_module and UpdateIncludeState(fullpath, include_state, io): tomwalters@0: header_found = True tomwalters@0: tomwalters@0: # If we can't find the header file for a .cc, assume it's because we don't tomwalters@0: # know where to look. In that case we'll give up as we're not sure they tomwalters@0: # didn't include it in the .h file. tomwalters@0: # TODO(unknown): Do a better job of finding .h files so we are confident that tomwalters@0: # not having the .h file means there isn't one. tomwalters@0: if filename.endswith('.cc') and not header_found: tomwalters@0: return tomwalters@0: tomwalters@0: # All the lines have been processed, report the errors found. tomwalters@0: for required_header_unstripped in required: tomwalters@0: template = required[required_header_unstripped][1] tomwalters@0: if template in _HEADERS_ACCEPTED_BUT_NOT_PROMOTED: tomwalters@0: headers = _HEADERS_ACCEPTED_BUT_NOT_PROMOTED[template] tomwalters@0: if [True for header in headers if header in include_state]: tomwalters@0: continue tomwalters@0: if required_header_unstripped.strip('<>"') not in include_state: tomwalters@0: error(filename, required[required_header_unstripped][0], tomwalters@0: 'build/include_what_you_use', 4, tomwalters@0: 'Add #include ' + required_header_unstripped + ' for ' + template) tomwalters@0: tomwalters@0: tomwalters@0: def ProcessLine(filename, file_extension, tomwalters@0: clean_lines, line, include_state, function_state, tomwalters@0: class_state, error): tomwalters@0: """Processes a single line in the file. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: Filename of the file that is being processed. tomwalters@0: file_extension: The extension (dot not included) of the file. tomwalters@0: clean_lines: An array of strings, each representing a line of the file, tomwalters@0: with comments stripped. tomwalters@0: line: Number of line being processed. tomwalters@0: include_state: An _IncludeState instance in which the headers are inserted. tomwalters@0: function_state: A _FunctionState instance which counts function lines, etc. tomwalters@0: class_state: A _ClassState instance which maintains information about tomwalters@0: the current stack of nested class declarations being parsed. tomwalters@0: error: A callable to which errors are reported, which takes 4 arguments: tomwalters@0: filename, line number, error level, and message tomwalters@0: tomwalters@0: """ tomwalters@0: raw_lines = clean_lines.raw_lines tomwalters@0: CheckForFunctionLengths(filename, clean_lines, line, function_state, error) tomwalters@0: if Search(r'\bNOLINT\b', raw_lines[line]): # ignore nolint lines tomwalters@0: return tomwalters@0: CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error) tomwalters@0: CheckStyle(filename, clean_lines, line, file_extension, error) tomwalters@0: CheckLanguage(filename, clean_lines, line, file_extension, include_state, tomwalters@0: error) tomwalters@0: CheckForNonStandardConstructs(filename, clean_lines, line, tomwalters@0: class_state, error) tomwalters@0: CheckPosixThreading(filename, clean_lines, line, error) tomwalters@0: CheckInvalidIncrement(filename, clean_lines, line, error) tomwalters@0: tomwalters@0: tomwalters@0: def ProcessFileData(filename, file_extension, lines, error): tomwalters@0: """Performs lint checks and reports any errors to the given error function. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: Filename of the file that is being processed. tomwalters@0: file_extension: The extension (dot not included) of the file. tomwalters@0: lines: An array of strings, each representing a line of the file, with the tomwalters@0: last element being empty if the file is termined with a newline. tomwalters@0: error: A callable to which errors are reported, which takes 4 arguments: tomwalters@0: """ tomwalters@0: lines = (['// marker so line numbers and indices both start at 1'] + lines + tomwalters@0: ['// marker so line numbers end in a known way']) tomwalters@0: tomwalters@0: include_state = _IncludeState() tomwalters@0: function_state = _FunctionState() tomwalters@0: class_state = _ClassState() tomwalters@0: tomwalters@0: CheckForCopyright(filename, lines, error) tomwalters@0: tomwalters@0: if file_extension == 'h': tomwalters@0: CheckForHeaderGuard(filename, lines, error) tomwalters@0: tomwalters@0: RemoveMultiLineComments(filename, lines, error) tomwalters@0: clean_lines = CleansedLines(lines) tomwalters@0: for line in xrange(clean_lines.NumLines()): tomwalters@0: ProcessLine(filename, file_extension, clean_lines, line, tomwalters@0: include_state, function_state, class_state, error) tomwalters@0: class_state.CheckFinished(filename, error) tomwalters@0: tomwalters@0: CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) tomwalters@0: tomwalters@0: # We check here rather than inside ProcessLine so that we see raw tomwalters@0: # lines rather than "cleaned" lines. tomwalters@0: CheckForUnicodeReplacementCharacters(filename, lines, error) tomwalters@0: tomwalters@0: CheckForNewlineAtEOF(filename, lines, error) tomwalters@0: tomwalters@0: tomwalters@0: def ProcessFile(filename, vlevel): tomwalters@0: """Does google-lint on a single file. tomwalters@0: tomwalters@0: Args: tomwalters@0: filename: The name of the file to parse. tomwalters@0: tomwalters@0: vlevel: The level of errors to report. Every error of confidence tomwalters@0: >= verbose_level will be reported. 0 is a good default. tomwalters@0: """ tomwalters@0: tomwalters@0: _SetVerboseLevel(vlevel) tomwalters@0: tomwalters@0: try: tomwalters@0: # Support the UNIX convention of using "-" for stdin. Note that tomwalters@0: # we are not opening the file with universal newline support tomwalters@0: # (which codecs doesn't support anyway), so the resulting lines do tomwalters@0: # contain trailing '\r' characters if we are reading a file that tomwalters@0: # has CRLF endings. tomwalters@0: # If after the split a trailing '\r' is present, it is removed tomwalters@0: # below. If it is not expected to be present (i.e. os.linesep != tomwalters@0: # '\r\n' as in Windows), a warning is issued below if this file tomwalters@0: # is processed. tomwalters@0: tomwalters@0: if filename == '-': tomwalters@0: lines = codecs.StreamReaderWriter(sys.stdin, tomwalters@0: codecs.getreader('utf8'), tomwalters@0: codecs.getwriter('utf8'), tomwalters@0: 'replace').read().split('\n') tomwalters@0: else: tomwalters@0: lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n') tomwalters@0: tomwalters@0: carriage_return_found = False tomwalters@0: # Remove trailing '\r'. tomwalters@0: for linenum in range(len(lines)): tomwalters@0: if lines[linenum].endswith('\r'): tomwalters@0: lines[linenum] = lines[linenum].rstrip('\r') tomwalters@0: carriage_return_found = True tomwalters@0: tomwalters@0: except IOError: tomwalters@0: sys.stderr.write( tomwalters@0: "Skipping input '%s': Can't open for reading\n" % filename) tomwalters@0: return tomwalters@0: tomwalters@0: # Note, if no dot is found, this will give the entire filename as the ext. tomwalters@0: file_extension = filename[filename.rfind('.') + 1:] tomwalters@0: tomwalters@0: # When reading from stdin, the extension is unknown, so no cpplint tests tomwalters@0: # should rely on the extension. tomwalters@0: if (filename != '-' and file_extension != 'cc' and file_extension != 'h' tomwalters@0: and file_extension != 'cpp'): tomwalters@0: sys.stderr.write('Ignoring %s; not a .cc or .h file\n' % filename) tomwalters@0: else: tomwalters@0: ProcessFileData(filename, file_extension, lines, Error) tomwalters@0: if carriage_return_found and os.linesep != '\r\n': tomwalters@0: # Use 0 for linenum since outputing only one error for potentially tomwalters@0: # several lines. tomwalters@0: Error(filename, 0, 'whitespace/newline', 1, tomwalters@0: 'One or more unexpected \\r (^M) found;' tomwalters@0: 'better to use only a \\n') tomwalters@0: tomwalters@0: sys.stderr.write('Done processing %s\n' % filename) tomwalters@0: tomwalters@0: tomwalters@0: def PrintUsage(message): tomwalters@0: """Prints a brief usage string and exits, optionally with an error message. tomwalters@0: tomwalters@0: Args: tomwalters@0: message: The optional error message. tomwalters@0: """ tomwalters@0: sys.stderr.write(_USAGE) tomwalters@0: if message: tomwalters@0: sys.exit('\nFATAL ERROR: ' + message) tomwalters@0: else: tomwalters@0: sys.exit(1) tomwalters@0: tomwalters@0: tomwalters@0: def PrintCategories(): tomwalters@0: """Prints a list of all the error-categories used by error messages. tomwalters@0: tomwalters@0: These are the categories used to filter messages via --filter. tomwalters@0: """ tomwalters@0: sys.stderr.write(_ERROR_CATEGORIES) tomwalters@0: sys.exit(0) tomwalters@0: tomwalters@0: tomwalters@0: def ParseArguments(args): tomwalters@0: """Parses the command line arguments. tomwalters@0: tomwalters@0: This may set the output format and verbosity level as side-effects. tomwalters@0: tomwalters@0: Args: tomwalters@0: args: The command line arguments: tomwalters@0: tomwalters@0: Returns: tomwalters@0: The list of filenames to lint. tomwalters@0: """ tomwalters@0: try: tomwalters@0: (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=', tomwalters@0: 'counting=', tomwalters@0: 'filter=']) tomwalters@0: except getopt.GetoptError: tomwalters@0: PrintUsage('Invalid arguments.') tomwalters@0: tomwalters@0: verbosity = _VerboseLevel() tomwalters@0: output_format = _OutputFormat() tomwalters@0: filters = '' tomwalters@0: counting_style = '' tomwalters@0: tomwalters@0: for (opt, val) in opts: tomwalters@0: if opt == '--help': tomwalters@0: PrintUsage(None) tomwalters@0: elif opt == '--output': tomwalters@0: if not val in ('emacs', 'vs7'): tomwalters@0: PrintUsage('The only allowed output formats are emacs and vs7.') tomwalters@0: output_format = val tomwalters@0: elif opt == '--verbose': tomwalters@0: verbosity = int(val) tomwalters@0: elif opt == '--filter': tomwalters@0: filters = val tomwalters@0: if not filters: tomwalters@0: PrintCategories() tomwalters@0: elif opt == '--counting': tomwalters@0: if val not in ('total', 'toplevel', 'detailed'): tomwalters@0: PrintUsage('Valid counting options are total, toplevel, and detailed') tomwalters@0: counting_style = val tomwalters@0: tomwalters@0: if not filenames: tomwalters@0: PrintUsage('No files were specified.') tomwalters@0: tomwalters@0: _SetOutputFormat(output_format) tomwalters@0: _SetVerboseLevel(verbosity) tomwalters@0: _SetFilters(filters) tomwalters@0: _SetCountingStyle(counting_style) tomwalters@0: tomwalters@0: return filenames tomwalters@0: tomwalters@0: tomwalters@0: def main(): tomwalters@0: filenames = ParseArguments(sys.argv[1:]) tomwalters@0: tomwalters@0: # Change stderr to write with replacement characters so we don't die tomwalters@0: # if we try to print something containing non-ASCII characters. tomwalters@0: sys.stderr = codecs.StreamReaderWriter(sys.stderr, tomwalters@0: codecs.getreader('utf8'), tomwalters@0: codecs.getwriter('utf8'), tomwalters@0: 'replace') tomwalters@0: tomwalters@0: _cpplint_state.ResetErrorCounts() tomwalters@0: for filename in filenames: tomwalters@0: ProcessFile(filename, _cpplint_state.verbose_level) tomwalters@0: _cpplint_state.PrintErrorCounts() tomwalters@0: tomwalters@0: sys.exit(_cpplint_state.error_count > 0) tomwalters@0: tomwalters@0: tomwalters@0: if __name__ == '__main__': tomwalters@0: main()