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