blob: 159dbbb07e5b2e3f46a2dff2edf1ac8db6439a97 [file] [log] [blame]
erg@google.com720121a2012-05-11 16:31:47 +00001#!/usr/bin/python
erg@google.com4e00b9a2009-01-12 23:05:11 +00002#
erg@google.com8f91ab22011-09-06 21:04:45 +00003# Copyright (c) 2009 Google Inc. All rights reserved.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004#
erg@google.com969161c2009-06-26 22:06:46 +00005# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
erg@google.com4e00b9a2009-01-12 23:05:11 +00008#
erg@google.com969161c2009-06-26 22:06:46 +00009# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
erg@google.com4e00b9a2009-01-12 23:05:11 +000018#
erg@google.com969161c2009-06-26 22:06:46 +000019# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
erg@google.com4e00b9a2009-01-12 23:05:11 +000030
31# Here are some issues that I've had people identify in my code during reviews,
32# that I think are possible to flag automatically in a lint tool. If these were
33# caught by lint, it would save time both for myself and that of my reviewers.
34# Most likely, some of these are beyond the scope of the current lint framework,
35# but I think it is valuable to retain these wish-list items even if they cannot
36# be immediately implemented.
37#
38# Suggestions
39# -----------
40# - Check for no 'explicit' for multi-arg ctor
41# - Check for boolean assign RHS in parens
42# - Check for ctor initializer-list colon position and spacing
43# - Check that if there's a ctor, there should be a dtor
44# - Check accessors that return non-pointer member variables are
45# declared const
46# - Check accessors that return non-const pointer member vars are
47# *not* declared const
48# - Check for using public includes for testing
49# - Check for spaces between brackets in one-line inline method
50# - Check for no assert()
51# - Check for spaces surrounding operators
52# - Check for 0 in pointer context (should be NULL)
53# - Check for 0 in char context (should be '\0')
54# - Check for camel-case method name conventions for methods
55# that are not simple inline getters and setters
erg@google.com4e00b9a2009-01-12 23:05:11 +000056# - Do not indent namespace contents
57# - Avoid inlining non-trivial constructors in header files
erg@google.com4e00b9a2009-01-12 23:05:11 +000058# - Check for old-school (void) cast for call-sites of functions
59# ignored return value
60# - Check gUnit usage of anonymous namespace
61# - Check for class declaration order (typedefs, consts, enums,
62# ctor(s?), dtor, friend declarations, methods, member vars)
63#
64
65"""Does google-lint on c++ files.
66
67The goal of this script is to identify places in the code that *may*
68be in non-compliance with google style. It does not attempt to fix
69up these problems -- the point is to educate. It does also not
70attempt to find all problems, or to ensure that everything it does
71find is legitimately a problem.
72
73In particular, we can get very confused by /* and // inside strings!
74We do a small hack, which is to ignore //'s with "'s after them on the
75same line, but it is far from perfect (in either direction).
76"""
77
78import codecs
erg@google.comd350fe52013-01-14 17:51:48 +000079import copy
erg@google.com4e00b9a2009-01-12 23:05:11 +000080import getopt
81import math # for log
82import os
83import re
84import sre_compile
85import string
86import sys
87import unicodedata
88
89
90_USAGE = """
91Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
erg@google.coma868d2d2009-10-09 21:18:45 +000092 [--counting=total|toplevel|detailed]
erg@google.com4e00b9a2009-01-12 23:05:11 +000093 <file> [file] ...
94
95 The style guidelines this tries to follow are those in
96 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
97
98 Every problem is given a confidence score from 1-5, with 5 meaning we are
99 certain of the problem, and 1 meaning it could be a legitimate construct.
100 This will miss some errors, and is not a substitute for a code review.
101
erg+personal@google.com05189642010-04-30 20:43:03 +0000102 To suppress false-positive errors of a certain category, add a
103 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
104 suppresses errors of all categories on that line.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000105
106 The files passed in will be linted; at least one file must be provided.
107 Linted extensions are .cc, .cpp, and .h. Other file types will be ignored.
108
109 Flags:
110
111 output=vs7
112 By default, the output is formatted to ease emacs parsing. Visual Studio
113 compatible output (vs7) may also be used. Other formats are unsupported.
114
115 verbose=#
116 Specify a number 0-5 to restrict errors to certain verbosity levels.
117
118 filter=-x,+y,...
119 Specify a comma-separated list of category-filters to apply: only
120 error messages whose category names pass the filters will be printed.
121 (Category names are printed with the message and look like
122 "[whitespace/indent]".) Filters are evaluated left to right.
123 "-FOO" and "FOO" means "do not print categories that start with FOO".
124 "+FOO" means "do print categories that start with FOO".
125
126 Examples: --filter=-whitespace,+whitespace/braces
127 --filter=whitespace,runtime/printf,+runtime/printf_format
128 --filter=-,+build/include_what_you_use
129
130 To see a list of all the categories used in cpplint, pass no arg:
131 --filter=
erg@google.coma868d2d2009-10-09 21:18:45 +0000132
133 counting=total|toplevel|detailed
134 The total number of errors found is always printed. If
135 'toplevel' is provided, then the count of errors in each of
136 the top-level categories like 'build' and 'whitespace' will
137 also be printed. If 'detailed' is provided, then a count
138 is provided for each category like 'build/class'.
erg@google.com4d70a882013-04-16 21:06:32 +0000139
140 root=subdir
141 The root directory used for deriving header guard CPP variable.
142 By default, the header guard CPP variable is calculated as the relative
143 path to the directory that contains .git, .hg, or .svn. When this flag
144 is specified, the relative path is calculated from the specified
145 directory. If the specified directory does not exist, this flag is
146 ignored.
147
148 Examples:
149 Assuing that src/.git exists, the header guard CPP variables for
150 src/chrome/browser/ui/browser.h are:
151
152 No flag => CHROME_BROWSER_UI_BROWSER_H_
153 --root=chrome => BROWSER_UI_BROWSER_H_
154 --root=chrome/browser => UI_BROWSER_H_
erg@google.com4e00b9a2009-01-12 23:05:11 +0000155"""
156
157# We categorize each error message we print. Here are the categories.
158# We want an explicit list so we can list them all in cpplint --filter=.
159# If you add a new error message with a new category, add it to the list
160# here! cpplint_unittest.py should tell you if you forget to do this.
erg@google.coma87abb82009-02-24 01:41:01 +0000161# \ used for clearer layout -- pylint: disable-msg=C6013
erg+personal@google.com05189642010-04-30 20:43:03 +0000162_ERROR_CATEGORIES = [
163 'build/class',
164 'build/deprecated',
165 'build/endif_comment',
erg@google.com8a95ecc2011-09-08 00:45:54 +0000166 'build/explicit_make_pair',
erg+personal@google.com05189642010-04-30 20:43:03 +0000167 'build/forward_decl',
168 'build/header_guard',
169 'build/include',
170 'build/include_alpha',
171 'build/include_order',
172 'build/include_what_you_use',
173 'build/namespaces',
174 'build/printf_format',
175 'build/storage_class',
176 'legal/copyright',
erg@google.comd350fe52013-01-14 17:51:48 +0000177 'readability/alt_tokens',
erg+personal@google.com05189642010-04-30 20:43:03 +0000178 'readability/braces',
179 'readability/casting',
180 'readability/check',
181 'readability/constructors',
182 'readability/fn_size',
183 'readability/function',
184 'readability/multiline_comment',
185 'readability/multiline_string',
erg@google.comd350fe52013-01-14 17:51:48 +0000186 'readability/namespace',
erg+personal@google.com05189642010-04-30 20:43:03 +0000187 'readability/nolint',
188 'readability/streams',
189 'readability/todo',
190 'readability/utf8',
191 'runtime/arrays',
192 'runtime/casting',
193 'runtime/explicit',
194 'runtime/int',
195 'runtime/init',
196 'runtime/invalid_increment',
197 'runtime/member_string_references',
198 'runtime/memset',
199 'runtime/operator',
200 'runtime/printf',
201 'runtime/printf_format',
202 'runtime/references',
203 'runtime/rtti',
204 'runtime/sizeof',
205 'runtime/string',
206 'runtime/threadsafe_fn',
erg+personal@google.com05189642010-04-30 20:43:03 +0000207 'whitespace/blank_line',
208 'whitespace/braces',
209 'whitespace/comma',
210 'whitespace/comments',
erg@google.comd350fe52013-01-14 17:51:48 +0000211 'whitespace/empty_loop_body',
erg+personal@google.com05189642010-04-30 20:43:03 +0000212 'whitespace/end_of_line',
213 'whitespace/ending_newline',
erg@google.comd350fe52013-01-14 17:51:48 +0000214 'whitespace/forcolon',
erg+personal@google.com05189642010-04-30 20:43:03 +0000215 'whitespace/indent',
216 'whitespace/labels',
217 'whitespace/line_length',
218 'whitespace/newline',
219 'whitespace/operators',
220 'whitespace/parens',
221 'whitespace/semicolon',
222 'whitespace/tab',
223 'whitespace/todo'
224 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000225
erg@google.come35f7652009-06-19 20:52:09 +0000226# The default state of the category filter. This is overrided by the --filter=
227# flag. By default all errors are on, so only add here categories that should be
228# off by default (i.e., categories that must be enabled by the --filter= flags).
229# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000230_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000231
erg@google.com4e00b9a2009-01-12 23:05:11 +0000232# We used to check for high-bit characters, but after much discussion we
233# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000234# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000235
236# Headers that we consider STL headers.
237_STL_HEADERS = frozenset([
238 'algobase.h', 'algorithm', 'alloc.h', 'bitset', 'deque', 'exception',
239 'function.h', 'functional', 'hash_map', 'hash_map.h', 'hash_set',
erg+personal@google.com05189642010-04-30 20:43:03 +0000240 'hash_set.h', 'iterator', 'list', 'list.h', 'map', 'memory', 'new',
241 'pair.h', 'pthread_alloc', 'queue', 'set', 'set.h', 'sstream', 'stack',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000242 'stl_alloc.h', 'stl_relops.h', 'type_traits.h',
243 'utility', 'vector', 'vector.h',
244 ])
245
246
247# Non-STL C++ system headers.
248_CPP_HEADERS = frozenset([
249 'algo.h', 'builtinbuf.h', 'bvector.h', 'cassert', 'cctype',
250 'cerrno', 'cfloat', 'ciso646', 'climits', 'clocale', 'cmath',
251 'complex', 'complex.h', 'csetjmp', 'csignal', 'cstdarg', 'cstddef',
252 'cstdio', 'cstdlib', 'cstring', 'ctime', 'cwchar', 'cwctype',
253 'defalloc.h', 'deque.h', 'editbuf.h', 'exception', 'fstream',
254 'fstream.h', 'hashtable.h', 'heap.h', 'indstream.h', 'iomanip',
erg@google.comd7d27472011-09-07 17:36:35 +0000255 'iomanip.h', 'ios', 'iosfwd', 'iostream', 'iostream.h', 'istream',
256 'istream.h', 'iterator.h', 'limits', 'map.h', 'multimap.h', 'multiset.h',
257 'numeric', 'ostream', 'ostream.h', 'parsestream.h', 'pfstream.h',
258 'PlotFile.h', 'procbuf.h', 'pthread_alloc.h', 'rope', 'rope.h',
259 'ropeimpl.h', 'SFile.h', 'slist', 'slist.h', 'stack.h', 'stdexcept',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000260 'stdiostream.h', 'streambuf.h', 'stream.h', 'strfile.h', 'string',
261 'strstream', 'strstream.h', 'tempbuf.h', 'tree.h', 'typeinfo', 'valarray',
262 ])
263
264
265# Assertion macros. These are defined in base/logging.h and
266# testing/base/gunit.h. Note that the _M versions need to come first
267# for substring matching to work.
268_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000269 'DCHECK', 'CHECK',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000270 'EXPECT_TRUE_M', 'EXPECT_TRUE',
271 'ASSERT_TRUE_M', 'ASSERT_TRUE',
272 'EXPECT_FALSE_M', 'EXPECT_FALSE',
273 'ASSERT_FALSE_M', 'ASSERT_FALSE',
274 ]
275
erg@google.come35f7652009-06-19 20:52:09 +0000276# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000277_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
278
279for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
280 ('>=', 'GE'), ('>', 'GT'),
281 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000282 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000283 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
284 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
285 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
286 _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
287 _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
288
289for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
290 ('>=', 'LT'), ('>', 'LE'),
291 ('<=', 'GT'), ('<', 'GE')]:
292 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
293 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
294 _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
295 _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
296
erg@google.comd350fe52013-01-14 17:51:48 +0000297# Alternative tokens and their replacements. For full list, see section 2.5
298# Alternative tokens [lex.digraph] in the C++ standard.
299#
300# Digraphs (such as '%:') are not included here since it's a mess to
301# match those on a word boundary.
302_ALT_TOKEN_REPLACEMENT = {
303 'and': '&&',
304 'bitor': '|',
305 'or': '||',
306 'xor': '^',
307 'compl': '~',
308 'bitand': '&',
309 'and_eq': '&=',
310 'or_eq': '|=',
311 'xor_eq': '^=',
312 'not': '!',
313 'not_eq': '!='
314 }
315
316# Compile regular expression that matches all the above keywords. The "[ =()]"
317# bit is meant to avoid matching these keywords outside of boolean expressions.
318#
319# False positives include C-style multi-line comments (http://go/nsiut )
320# and multi-line strings (http://go/beujw ), but those have always been
321# troublesome for cpplint.
322_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
323 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
324
erg@google.com4e00b9a2009-01-12 23:05:11 +0000325
326# These constants define types of headers for use with
327# _IncludeState.CheckNextIncludeOrder().
328_C_SYS_HEADER = 1
329_CPP_SYS_HEADER = 2
330_LIKELY_MY_HEADER = 3
331_POSSIBLE_MY_HEADER = 4
332_OTHER_HEADER = 5
333
erg@google.comd350fe52013-01-14 17:51:48 +0000334# These constants define the current inline assembly state
335_NO_ASM = 0 # Outside of inline assembly block
336_INSIDE_ASM = 1 # Inside inline assembly block
337_END_ASM = 2 # Last line of inline assembly block
338_BLOCK_ASM = 3 # The whole block is an inline assembly block
339
340# Match start of assembly blocks
341_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
342 r'(?:\s+(volatile|__volatile__))?'
343 r'\s*[{(]')
344
erg@google.com4e00b9a2009-01-12 23:05:11 +0000345
346_regexp_compile_cache = {}
347
erg+personal@google.com05189642010-04-30 20:43:03 +0000348# Finds occurrences of NOLINT or NOLINT(...).
349_RE_SUPPRESSION = re.compile(r'\bNOLINT\b(\([^)]*\))?')
350
351# {str, set(int)}: a map from error categories to sets of linenumbers
352# on which those errors are expected and should be suppressed.
353_error_suppressions = {}
354
erg@google.com4d70a882013-04-16 21:06:32 +0000355# The root directory used for deriving header guard CPP variable.
356# This is set by --root flag.
357_root = None
358
erg+personal@google.com05189642010-04-30 20:43:03 +0000359def ParseNolintSuppressions(filename, raw_line, linenum, error):
360 """Updates the global list of error-suppressions.
361
362 Parses any NOLINT comments on the current line, updating the global
363 error_suppressions store. Reports an error if the NOLINT comment
364 was malformed.
365
366 Args:
367 filename: str, the name of the input file.
368 raw_line: str, the line of input text, with comments.
369 linenum: int, the number of the current line.
370 error: function, an error handler.
371 """
372 # FIXME(adonovan): "NOLINT(" is misparsed as NOLINT(*).
erg@google.com8a95ecc2011-09-08 00:45:54 +0000373 matched = _RE_SUPPRESSION.search(raw_line)
374 if matched:
375 category = matched.group(1)
erg+personal@google.com05189642010-04-30 20:43:03 +0000376 if category in (None, '(*)'): # => "suppress all"
377 _error_suppressions.setdefault(None, set()).add(linenum)
378 else:
379 if category.startswith('(') and category.endswith(')'):
380 category = category[1:-1]
381 if category in _ERROR_CATEGORIES:
382 _error_suppressions.setdefault(category, set()).add(linenum)
383 else:
384 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000385 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000386
387
388def ResetNolintSuppressions():
389 "Resets the set of NOLINT suppressions to empty."
390 _error_suppressions.clear()
391
392
393def IsErrorSuppressedByNolint(category, linenum):
394 """Returns true if the specified error category is suppressed on this line.
395
396 Consults the global error_suppressions map populated by
397 ParseNolintSuppressions/ResetNolintSuppressions.
398
399 Args:
400 category: str, the category of the error.
401 linenum: int, the current line number.
402 Returns:
403 bool, True iff the error should be suppressed due to a NOLINT comment.
404 """
405 return (linenum in _error_suppressions.get(category, set()) or
406 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000407
408def Match(pattern, s):
409 """Matches the string with the pattern, caching the compiled regexp."""
410 # The regexp compilation caching is inlined in both Match and Search for
411 # performance reasons; factoring it out into a separate function turns out
412 # to be noticeably expensive.
413 if not pattern in _regexp_compile_cache:
414 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
415 return _regexp_compile_cache[pattern].match(s)
416
417
418def Search(pattern, s):
419 """Searches the string for the pattern, caching the compiled regexp."""
420 if not pattern in _regexp_compile_cache:
421 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
422 return _regexp_compile_cache[pattern].search(s)
423
424
425class _IncludeState(dict):
426 """Tracks line numbers for includes, and the order in which includes appear.
427
428 As a dict, an _IncludeState object serves as a mapping between include
429 filename and line number on which that file was included.
430
431 Call CheckNextIncludeOrder() once for each header in the file, passing
432 in the type constants defined above. Calls in an illegal order will
433 raise an _IncludeError with an appropriate error message.
434
435 """
436 # self._section will move monotonically through this set. If it ever
437 # needs to move backwards, CheckNextIncludeOrder will raise an error.
438 _INITIAL_SECTION = 0
439 _MY_H_SECTION = 1
440 _C_SECTION = 2
441 _CPP_SECTION = 3
442 _OTHER_H_SECTION = 4
443
444 _TYPE_NAMES = {
445 _C_SYS_HEADER: 'C system header',
446 _CPP_SYS_HEADER: 'C++ system header',
447 _LIKELY_MY_HEADER: 'header this file implements',
448 _POSSIBLE_MY_HEADER: 'header this file may implement',
449 _OTHER_HEADER: 'other header',
450 }
451 _SECTION_NAMES = {
452 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
453 _MY_H_SECTION: 'a header this file implements',
454 _C_SECTION: 'C system header',
455 _CPP_SECTION: 'C++ system header',
456 _OTHER_H_SECTION: 'other header',
457 }
458
459 def __init__(self):
460 dict.__init__(self)
erg@google.coma868d2d2009-10-09 21:18:45 +0000461 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000462 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000463 # The path of last found header.
464 self._last_header = ''
465
466 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000467 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000468
469 - replaces "-" with "_" so they both cmp the same.
470 - removes '-inl' since we don't require them to be after the main header.
471 - lowercase everything, just in case.
472
473 Args:
474 header_path: Path to be canonicalized.
475
476 Returns:
477 Canonicalized path.
478 """
479 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
480
481 def IsInAlphabeticalOrder(self, header_path):
482 """Check if a header is in alphabetical order with the previous header.
483
484 Args:
485 header_path: Header to be checked.
486
487 Returns:
488 Returns true if the header is in alphabetical order.
489 """
490 canonical_header = self.CanonicalizeAlphabeticalOrder(header_path)
491 if self._last_header > canonical_header:
492 return False
493 self._last_header = canonical_header
494 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000495
496 def CheckNextIncludeOrder(self, header_type):
497 """Returns a non-empty error message if the next header is out of order.
498
499 This function also updates the internal state to be ready to check
500 the next include.
501
502 Args:
503 header_type: One of the _XXX_HEADER constants defined above.
504
505 Returns:
506 The empty string if the header is in the right order, or an
507 error message describing what's wrong.
508
509 """
510 error_message = ('Found %s after %s' %
511 (self._TYPE_NAMES[header_type],
512 self._SECTION_NAMES[self._section]))
513
erg@google.coma868d2d2009-10-09 21:18:45 +0000514 last_section = self._section
515
erg@google.com4e00b9a2009-01-12 23:05:11 +0000516 if header_type == _C_SYS_HEADER:
517 if self._section <= self._C_SECTION:
518 self._section = self._C_SECTION
519 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000520 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000521 return error_message
522 elif header_type == _CPP_SYS_HEADER:
523 if self._section <= self._CPP_SECTION:
524 self._section = self._CPP_SECTION
525 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000526 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000527 return error_message
528 elif header_type == _LIKELY_MY_HEADER:
529 if self._section <= self._MY_H_SECTION:
530 self._section = self._MY_H_SECTION
531 else:
532 self._section = self._OTHER_H_SECTION
533 elif header_type == _POSSIBLE_MY_HEADER:
534 if self._section <= self._MY_H_SECTION:
535 self._section = self._MY_H_SECTION
536 else:
537 # This will always be the fallback because we're not sure
538 # enough that the header is associated with this file.
539 self._section = self._OTHER_H_SECTION
540 else:
541 assert header_type == _OTHER_HEADER
542 self._section = self._OTHER_H_SECTION
543
erg@google.coma868d2d2009-10-09 21:18:45 +0000544 if last_section != self._section:
545 self._last_header = ''
546
erg@google.com4e00b9a2009-01-12 23:05:11 +0000547 return ''
548
549
550class _CppLintState(object):
551 """Maintains module-wide state.."""
552
553 def __init__(self):
554 self.verbose_level = 1 # global setting.
555 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000556 # filters to apply when emitting error messages
557 self.filters = _DEFAULT_FILTERS[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000558 self.counting = 'total' # In what way are we counting errors?
559 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000560
561 # output format:
562 # "emacs" - format that emacs can parse (default)
563 # "vs7" - format that Microsoft Visual Studio 7 can parse
564 self.output_format = 'emacs'
565
566 def SetOutputFormat(self, output_format):
567 """Sets the output format for errors."""
568 self.output_format = output_format
569
570 def SetVerboseLevel(self, level):
571 """Sets the module's verbosity, and returns the previous setting."""
572 last_verbose_level = self.verbose_level
573 self.verbose_level = level
574 return last_verbose_level
575
erg@google.coma868d2d2009-10-09 21:18:45 +0000576 def SetCountingStyle(self, counting_style):
577 """Sets the module's counting options."""
578 self.counting = counting_style
579
erg@google.com4e00b9a2009-01-12 23:05:11 +0000580 def SetFilters(self, filters):
581 """Sets the error-message filters.
582
583 These filters are applied when deciding whether to emit a given
584 error message.
585
586 Args:
587 filters: A string of comma-separated filters (eg "+whitespace/indent").
588 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000589
590 Raises:
591 ValueError: The comma-separated filters did not all start with '+' or '-'.
592 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000593 """
erg@google.come35f7652009-06-19 20:52:09 +0000594 # Default filters always have less priority than the flag ones.
595 self.filters = _DEFAULT_FILTERS[:]
596 for filt in filters.split(','):
597 clean_filt = filt.strip()
598 if clean_filt:
599 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000600 for filt in self.filters:
601 if not (filt.startswith('+') or filt.startswith('-')):
602 raise ValueError('Every filter in --filters must start with + or -'
603 ' (%s does not)' % filt)
604
erg@google.coma868d2d2009-10-09 21:18:45 +0000605 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000606 """Sets the module's error statistic back to zero."""
607 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000608 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000609
erg@google.coma868d2d2009-10-09 21:18:45 +0000610 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000611 """Bumps the module's error statistic."""
612 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000613 if self.counting in ('toplevel', 'detailed'):
614 if self.counting != 'detailed':
615 category = category.split('/')[0]
616 if category not in self.errors_by_category:
617 self.errors_by_category[category] = 0
618 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000619
erg@google.coma868d2d2009-10-09 21:18:45 +0000620 def PrintErrorCounts(self):
621 """Print a summary of errors by category, and the total."""
622 for category, count in self.errors_by_category.iteritems():
623 sys.stderr.write('Category \'%s\' errors found: %d\n' %
624 (category, count))
625 sys.stderr.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000626
627_cpplint_state = _CppLintState()
628
629
630def _OutputFormat():
631 """Gets the module's output format."""
632 return _cpplint_state.output_format
633
634
635def _SetOutputFormat(output_format):
636 """Sets the module's output format."""
637 _cpplint_state.SetOutputFormat(output_format)
638
639
640def _VerboseLevel():
641 """Returns the module's verbosity setting."""
642 return _cpplint_state.verbose_level
643
644
645def _SetVerboseLevel(level):
646 """Sets the module's verbosity, and returns the previous setting."""
647 return _cpplint_state.SetVerboseLevel(level)
648
649
erg@google.coma868d2d2009-10-09 21:18:45 +0000650def _SetCountingStyle(level):
651 """Sets the module's counting options."""
652 _cpplint_state.SetCountingStyle(level)
653
654
erg@google.com4e00b9a2009-01-12 23:05:11 +0000655def _Filters():
656 """Returns the module's list of output filters, as a list."""
657 return _cpplint_state.filters
658
659
660def _SetFilters(filters):
661 """Sets the module's error-message filters.
662
663 These filters are applied when deciding whether to emit a given
664 error message.
665
666 Args:
667 filters: A string of comma-separated filters (eg "whitespace/indent").
668 Each filter should start with + or -; else we die.
669 """
670 _cpplint_state.SetFilters(filters)
671
672
673class _FunctionState(object):
674 """Tracks current function name and the number of lines in its body."""
675
676 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
677 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
678
679 def __init__(self):
680 self.in_a_function = False
681 self.lines_in_function = 0
682 self.current_function = ''
683
684 def Begin(self, function_name):
685 """Start analyzing function body.
686
687 Args:
688 function_name: The name of the function being tracked.
689 """
690 self.in_a_function = True
691 self.lines_in_function = 0
692 self.current_function = function_name
693
694 def Count(self):
695 """Count line in current function body."""
696 if self.in_a_function:
697 self.lines_in_function += 1
698
699 def Check(self, error, filename, linenum):
700 """Report if too many lines in function body.
701
702 Args:
703 error: The function to call with any errors found.
704 filename: The name of the current file.
705 linenum: The number of the line to check.
706 """
707 if Match(r'T(EST|est)', self.current_function):
708 base_trigger = self._TEST_TRIGGER
709 else:
710 base_trigger = self._NORMAL_TRIGGER
711 trigger = base_trigger * 2**_VerboseLevel()
712
713 if self.lines_in_function > trigger:
714 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
715 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
716 if error_level > 5:
717 error_level = 5
718 error(filename, linenum, 'readability/fn_size', error_level,
719 'Small and focused functions are preferred:'
720 ' %s has %d non-comment lines'
721 ' (error triggered by exceeding %d lines).' % (
722 self.current_function, self.lines_in_function, trigger))
723
724 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000725 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +0000726 self.in_a_function = False
727
728
729class _IncludeError(Exception):
730 """Indicates a problem with the include order in a file."""
731 pass
732
733
734class FileInfo:
735 """Provides utility functions for filenames.
736
737 FileInfo provides easy access to the components of a file's path
738 relative to the project root.
739 """
740
741 def __init__(self, filename):
742 self._filename = filename
743
744 def FullName(self):
745 """Make Windows paths like Unix."""
746 return os.path.abspath(self._filename).replace('\\', '/')
747
748 def RepositoryName(self):
749 """FullName after removing the local path to the repository.
750
751 If we have a real absolute path name here we can try to do something smart:
752 detecting the root of the checkout and truncating /path/to/checkout from
753 the name so that we get header guards that don't include things like
754 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
755 people on different computers who have checked the source out to different
756 locations won't see bogus errors.
757 """
758 fullname = self.FullName()
759
760 if os.path.exists(fullname):
761 project_dir = os.path.dirname(fullname)
762
763 if os.path.exists(os.path.join(project_dir, ".svn")):
764 # If there's a .svn file in the current directory, we recursively look
765 # up the directory tree for the top of the SVN checkout
766 root_dir = project_dir
767 one_up_dir = os.path.dirname(root_dir)
768 while os.path.exists(os.path.join(one_up_dir, ".svn")):
769 root_dir = os.path.dirname(root_dir)
770 one_up_dir = os.path.dirname(one_up_dir)
771
772 prefix = os.path.commonprefix([root_dir, project_dir])
773 return fullname[len(prefix) + 1:]
774
erg@google.com3dc74262011-11-30 01:12:00 +0000775 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
776 # searching up from the current path.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000777 root_dir = os.path.dirname(fullname)
778 while (root_dir != os.path.dirname(root_dir) and
erg@google.com5e169692010-01-28 20:17:01 +0000779 not os.path.exists(os.path.join(root_dir, ".git")) and
erg@google.com3dc74262011-11-30 01:12:00 +0000780 not os.path.exists(os.path.join(root_dir, ".hg")) and
781 not os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000782 root_dir = os.path.dirname(root_dir)
erg@google.com42e59b02010-10-04 22:18:07 +0000783
784 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +0000785 os.path.exists(os.path.join(root_dir, ".hg")) or
786 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +0000787 prefix = os.path.commonprefix([root_dir, project_dir])
788 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000789
790 # Don't know what to do; header guard warnings may be wrong...
791 return fullname
792
793 def Split(self):
794 """Splits the file into the directory, basename, and extension.
795
796 For 'chrome/browser/browser.cc', Split() would
797 return ('chrome/browser', 'browser', '.cc')
798
799 Returns:
800 A tuple of (directory, basename, extension).
801 """
802
803 googlename = self.RepositoryName()
804 project, rest = os.path.split(googlename)
805 return (project,) + os.path.splitext(rest)
806
807 def BaseName(self):
808 """File base name - text after the final slash, before the final period."""
809 return self.Split()[1]
810
811 def Extension(self):
812 """File extension - text following the final period."""
813 return self.Split()[2]
814
815 def NoExtension(self):
816 """File has no source file extension."""
817 return '/'.join(self.Split()[0:2])
818
819 def IsSource(self):
820 """File has a source file extension."""
821 return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx')
822
823
erg+personal@google.com05189642010-04-30 20:43:03 +0000824def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000825 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000826
827 # There are three ways we might decide not to print an error message:
828 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +0000829 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +0000830 if IsErrorSuppressedByNolint(category, linenum):
831 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +0000832 if confidence < _cpplint_state.verbose_level:
833 return False
834
835 is_filtered = False
836 for one_filter in _Filters():
837 if one_filter.startswith('-'):
838 if category.startswith(one_filter[1:]):
839 is_filtered = True
840 elif one_filter.startswith('+'):
841 if category.startswith(one_filter[1:]):
842 is_filtered = False
843 else:
844 assert False # should have been checked for in SetFilter.
845 if is_filtered:
846 return False
847
848 return True
849
850
851def Error(filename, linenum, category, confidence, message):
852 """Logs the fact we've found a lint error.
853
854 We log where the error was found, and also our confidence in the error,
855 that is, how certain we are this is a legitimate style regression, and
856 not a misidentification or a use that's sometimes justified.
857
erg+personal@google.com05189642010-04-30 20:43:03 +0000858 False positives can be suppressed by the use of
859 "cpplint(category)" comments on the offending line. These are
860 parsed into _error_suppressions.
861
erg@google.com4e00b9a2009-01-12 23:05:11 +0000862 Args:
863 filename: The name of the file containing the error.
864 linenum: The number of the line containing the error.
865 category: A string used to describe the "category" this bug
866 falls under: "whitespace", say, or "runtime". Categories
867 may have a hierarchy separated by slashes: "whitespace/indent".
868 confidence: A number from 1-5 representing a confidence score for
869 the error, with 5 meaning that we are certain of the problem,
870 and 1 meaning that it could be a legitimate construct.
871 message: The error message.
872 """
erg+personal@google.com05189642010-04-30 20:43:03 +0000873 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +0000874 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000875 if _cpplint_state.output_format == 'vs7':
876 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
877 filename, linenum, message, category, confidence))
878 else:
879 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
880 filename, linenum, message, category, confidence))
881
882
883# Matches standard C++ escape esequences per 2.13.2.3 of the C++ standard.
884_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
885 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
886# Matches strings. Escape codes should already be removed by ESCAPES.
887_RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES = re.compile(r'"[^"]*"')
888# Matches characters. Escape codes should already be removed by ESCAPES.
889_RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES = re.compile(r"'.'")
890# Matches multi-line C++ comments.
891# This RE is a little bit more complicated than one might expect, because we
892# have to take care of space removals tools so we can handle comments inside
893# statements better.
894# The current rule is: We only clear spaces from both sides when we're at the
895# end of the line. Otherwise, we try to remove spaces from the right side,
896# if this doesn't work we try on left side but only if there's a non-character
897# on the right.
898_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
899 r"""(\s*/\*.*\*/\s*$|
900 /\*.*\*/\s+|
901 \s+/\*.*\*/(?=\W)|
902 /\*.*\*/)""", re.VERBOSE)
903
904
905def IsCppString(line):
906 """Does line terminate so, that the next symbol is in string constant.
907
908 This function does not consider single-line nor multi-line comments.
909
910 Args:
911 line: is a partial line of code starting from the 0..n.
912
913 Returns:
914 True, if next character appended to 'line' is inside a
915 string constant.
916 """
917
918 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
919 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
920
921
922def FindNextMultiLineCommentStart(lines, lineix):
923 """Find the beginning marker for a multiline comment."""
924 while lineix < len(lines):
925 if lines[lineix].strip().startswith('/*'):
926 # Only return this marker if the comment goes beyond this line
927 if lines[lineix].strip().find('*/', 2) < 0:
928 return lineix
929 lineix += 1
930 return len(lines)
931
932
933def FindNextMultiLineCommentEnd(lines, lineix):
934 """We are inside a comment, find the end marker."""
935 while lineix < len(lines):
936 if lines[lineix].strip().endswith('*/'):
937 return lineix
938 lineix += 1
939 return len(lines)
940
941
942def RemoveMultiLineCommentsFromRange(lines, begin, end):
943 """Clears a range of lines for multi-line comments."""
944 # Having // dummy comments makes the lines non-empty, so we will not get
945 # unnecessary blank line warnings later in the code.
946 for i in range(begin, end):
947 lines[i] = '// dummy'
948
949
950def RemoveMultiLineComments(filename, lines, error):
951 """Removes multiline (c-style) comments from lines."""
952 lineix = 0
953 while lineix < len(lines):
954 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
955 if lineix_begin >= len(lines):
956 return
957 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
958 if lineix_end >= len(lines):
959 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
960 'Could not find end of multi-line comment')
961 return
962 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
963 lineix = lineix_end + 1
964
965
966def CleanseComments(line):
967 """Removes //-comments and single-line C-style /* */ comments.
968
969 Args:
970 line: A line of C++ source.
971
972 Returns:
973 The line with single-line comments removed.
974 """
975 commentpos = line.find('//')
976 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +0000977 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +0000978 # get rid of /* ... */
979 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
980
981
erg@google.coma87abb82009-02-24 01:41:01 +0000982class CleansedLines(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000983 """Holds 3 copies of all lines with different preprocessing applied to them.
984
985 1) elided member contains lines without strings and comments,
986 2) lines member contains lines without comments, and
erg@google.comd350fe52013-01-14 17:51:48 +0000987 3) raw_lines member contains all the lines without processing.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000988 All these three members are of <type 'list'>, and of the same length.
989 """
990
991 def __init__(self, lines):
992 self.elided = []
993 self.lines = []
994 self.raw_lines = lines
995 self.num_lines = len(lines)
996 for linenum in range(len(lines)):
997 self.lines.append(CleanseComments(lines[linenum]))
998 elided = self._CollapseStrings(lines[linenum])
999 self.elided.append(CleanseComments(elided))
1000
1001 def NumLines(self):
1002 """Returns the number of lines represented."""
1003 return self.num_lines
1004
1005 @staticmethod
1006 def _CollapseStrings(elided):
1007 """Collapses strings and chars on a line to simple "" or '' blocks.
1008
1009 We nix strings first so we're not fooled by text like '"http://"'
1010
1011 Args:
1012 elided: The line being processed.
1013
1014 Returns:
1015 The line with collapsed strings.
1016 """
1017 if not _RE_PATTERN_INCLUDE.match(elided):
1018 # Remove escaped characters first to make quote/single quote collapsing
1019 # basic. Things that look like escaped characters shouldn't occur
1020 # outside of strings and chars.
1021 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1022 elided = _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES.sub("''", elided)
1023 elided = _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES.sub('""', elided)
1024 return elided
1025
1026
erg@google.comd350fe52013-01-14 17:51:48 +00001027def FindEndOfExpressionInLine(line, startpos, depth, startchar, endchar):
1028 """Find the position just after the matching endchar.
1029
1030 Args:
1031 line: a CleansedLines line.
1032 startpos: start searching at this position.
1033 depth: nesting level at startpos.
1034 startchar: expression opening character.
1035 endchar: expression closing character.
1036
1037 Returns:
1038 Index just after endchar.
1039 """
1040 for i in xrange(startpos, len(line)):
1041 if line[i] == startchar:
1042 depth += 1
1043 elif line[i] == endchar:
1044 depth -= 1
1045 if depth == 0:
1046 return i + 1
1047 return -1
1048
1049
erg@google.com4e00b9a2009-01-12 23:05:11 +00001050def CloseExpression(clean_lines, linenum, pos):
1051 """If input points to ( or { or [, finds the position that closes it.
1052
erg@google.com8a95ecc2011-09-08 00:45:54 +00001053 If lines[linenum][pos] points to a '(' or '{' or '[', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001054 linenum/pos that correspond to the closing of the expression.
1055
1056 Args:
1057 clean_lines: A CleansedLines instance containing the file.
1058 linenum: The number of the line to check.
1059 pos: A position on the line.
1060
1061 Returns:
1062 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1063 (line, len(lines), -1) if we never find a close. Note we ignore
1064 strings and comments when matching; and the line we return is the
1065 'cleansed' line at linenum.
1066 """
1067
1068 line = clean_lines.elided[linenum]
1069 startchar = line[pos]
1070 if startchar not in '({[':
1071 return (line, clean_lines.NumLines(), -1)
1072 if startchar == '(': endchar = ')'
1073 if startchar == '[': endchar = ']'
1074 if startchar == '{': endchar = '}'
1075
erg@google.comd350fe52013-01-14 17:51:48 +00001076 # Check first line
1077 end_pos = FindEndOfExpressionInLine(line, pos, 0, startchar, endchar)
1078 if end_pos > -1:
1079 return (line, linenum, end_pos)
1080 tail = line[pos:]
1081 num_open = tail.count(startchar) - tail.count(endchar)
1082 while linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001083 linenum += 1
1084 line = clean_lines.elided[linenum]
erg@google.comd350fe52013-01-14 17:51:48 +00001085 delta = line.count(startchar) - line.count(endchar)
1086 if num_open + delta <= 0:
1087 return (line, linenum,
1088 FindEndOfExpressionInLine(line, 0, num_open, startchar, endchar))
1089 num_open += delta
erg@google.com4e00b9a2009-01-12 23:05:11 +00001090
erg@google.comd350fe52013-01-14 17:51:48 +00001091 # Did not find endchar before end of file, give up
1092 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001093
1094def CheckForCopyright(filename, lines, error):
1095 """Logs an error if no Copyright message appears at the top of the file."""
1096
1097 # We'll say it should occur by line 10. Don't forget there's a
1098 # dummy line at the front.
1099 for line in xrange(1, min(len(lines), 11)):
1100 if re.search(r'Copyright', lines[line], re.I): break
1101 else: # means no copyright line was found
1102 error(filename, 0, 'legal/copyright', 5,
1103 'No copyright message found. '
1104 'You should have a line: "Copyright [year] <Copyright Owner>"')
1105
1106
1107def GetHeaderGuardCPPVariable(filename):
1108 """Returns the CPP variable that should be used as a header guard.
1109
1110 Args:
1111 filename: The name of a C++ header file.
1112
1113 Returns:
1114 The CPP variable that should be used as a header guard in the
1115 named file.
1116
1117 """
1118
erg+personal@google.com05189642010-04-30 20:43:03 +00001119 # Restores original filename in case that cpplint is invoked from Emacs's
1120 # flymake.
1121 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001122 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
erg+personal@google.com05189642010-04-30 20:43:03 +00001123
erg@google.com4e00b9a2009-01-12 23:05:11 +00001124 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001125 file_path_from_root = fileinfo.RepositoryName()
1126 if _root:
1127 file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root)
1128 return re.sub(r'[-./\s]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001129
1130
1131def CheckForHeaderGuard(filename, lines, error):
1132 """Checks that the file contains a header guard.
1133
erg@google.coma87abb82009-02-24 01:41:01 +00001134 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001135 headers, checks that the full pathname is used.
1136
1137 Args:
1138 filename: The name of the C++ header file.
1139 lines: An array of strings, each representing a line of the file.
1140 error: The function to call with any errors found.
1141 """
1142
1143 cppvar = GetHeaderGuardCPPVariable(filename)
1144
1145 ifndef = None
1146 ifndef_linenum = 0
1147 define = None
1148 endif = None
1149 endif_linenum = 0
1150 for linenum, line in enumerate(lines):
1151 linesplit = line.split()
1152 if len(linesplit) >= 2:
1153 # find the first occurrence of #ifndef and #define, save arg
1154 if not ifndef and linesplit[0] == '#ifndef':
1155 # set ifndef to the header guard presented on the #ifndef line.
1156 ifndef = linesplit[1]
1157 ifndef_linenum = linenum
1158 if not define and linesplit[0] == '#define':
1159 define = linesplit[1]
1160 # find the last occurrence of #endif, save entire line
1161 if line.startswith('#endif'):
1162 endif = line
1163 endif_linenum = linenum
1164
erg@google.comdc289702012-01-26 20:30:03 +00001165 if not ifndef:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001166 error(filename, 0, 'build/header_guard', 5,
1167 'No #ifndef header guard found, suggested CPP variable is: %s' %
1168 cppvar)
1169 return
1170
erg@google.comdc289702012-01-26 20:30:03 +00001171 if not define:
1172 error(filename, 0, 'build/header_guard', 5,
1173 'No #define header guard found, suggested CPP variable is: %s' %
1174 cppvar)
1175 return
1176
erg@google.com4e00b9a2009-01-12 23:05:11 +00001177 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1178 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001179 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001180 error_level = 0
1181 if ifndef != cppvar + '_':
1182 error_level = 5
1183
erg+personal@google.com05189642010-04-30 20:43:03 +00001184 ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum,
1185 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001186 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1187 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1188
erg@google.comdc289702012-01-26 20:30:03 +00001189 if define != ifndef:
1190 error(filename, 0, 'build/header_guard', 5,
1191 '#ifndef and #define don\'t match, suggested CPP variable is: %s' %
1192 cppvar)
1193 return
1194
erg+personal@google.com05189642010-04-30 20:43:03 +00001195 if endif != ('#endif // %s' % cppvar):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001196 error_level = 0
1197 if endif != ('#endif // %s' % (cppvar + '_')):
1198 error_level = 5
1199
erg+personal@google.com05189642010-04-30 20:43:03 +00001200 ParseNolintSuppressions(filename, lines[endif_linenum], endif_linenum,
1201 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001202 error(filename, endif_linenum, 'build/header_guard', error_level,
1203 '#endif line should be "#endif // %s"' % cppvar)
1204
1205
1206def CheckForUnicodeReplacementCharacters(filename, lines, error):
1207 """Logs an error for each line containing Unicode replacement characters.
1208
1209 These indicate that either the file contained invalid UTF-8 (likely)
1210 or Unicode replacement characters (which it shouldn't). Note that
1211 it's possible for this to throw off line numbering if the invalid
1212 UTF-8 occurred adjacent to a newline.
1213
1214 Args:
1215 filename: The name of the current file.
1216 lines: An array of strings, each representing a line of the file.
1217 error: The function to call with any errors found.
1218 """
1219 for linenum, line in enumerate(lines):
1220 if u'\ufffd' in line:
1221 error(filename, linenum, 'readability/utf8', 5,
1222 'Line contains invalid UTF-8 (or Unicode replacement character).')
1223
1224
1225def CheckForNewlineAtEOF(filename, lines, error):
1226 """Logs an error if there is no newline char at the end of the file.
1227
1228 Args:
1229 filename: The name of the current file.
1230 lines: An array of strings, each representing a line of the file.
1231 error: The function to call with any errors found.
1232 """
1233
1234 # The array lines() was created by adding two newlines to the
1235 # original file (go figure), then splitting on \n.
1236 # To verify that the file ends in \n, we just have to make sure the
1237 # last-but-two element of lines() exists and is empty.
1238 if len(lines) < 3 or lines[-2]:
1239 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1240 'Could not find a newline character at the end of the file.')
1241
1242
1243def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1244 """Logs an error if we see /* ... */ or "..." that extend past one line.
1245
1246 /* ... */ comments are legit inside macros, for one line.
1247 Otherwise, we prefer // comments, so it's ok to warn about the
1248 other. Likewise, it's ok for strings to extend across multiple
1249 lines, as long as a line continuation character (backslash)
1250 terminates each line. Although not currently prohibited by the C++
1251 style guide, it's ugly and unnecessary. We don't do well with either
1252 in this lint program, so we warn about both.
1253
1254 Args:
1255 filename: The name of the current file.
1256 clean_lines: A CleansedLines instance containing the file.
1257 linenum: The number of the line to check.
1258 error: The function to call with any errors found.
1259 """
1260 line = clean_lines.elided[linenum]
1261
1262 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1263 # second (escaped) slash may trigger later \" detection erroneously.
1264 line = line.replace('\\\\', '')
1265
1266 if line.count('/*') > line.count('*/'):
1267 error(filename, linenum, 'readability/multiline_comment', 5,
1268 'Complex multi-line /*...*/-style comment found. '
1269 'Lint may give bogus warnings. '
1270 'Consider replacing these with //-style comments, '
1271 'with #if 0...#endif, '
1272 'or with more clearly structured multi-line comments.')
1273
1274 if (line.count('"') - line.count('\\"')) % 2:
1275 error(filename, linenum, 'readability/multiline_string', 5,
1276 'Multi-line string ("...") found. This lint script doesn\'t '
1277 'do well with such strings, and may give bogus warnings. They\'re '
1278 'ugly and unnecessary, and you should use concatenation instead".')
1279
1280
1281threading_list = (
1282 ('asctime(', 'asctime_r('),
1283 ('ctime(', 'ctime_r('),
1284 ('getgrgid(', 'getgrgid_r('),
1285 ('getgrnam(', 'getgrnam_r('),
1286 ('getlogin(', 'getlogin_r('),
1287 ('getpwnam(', 'getpwnam_r('),
1288 ('getpwuid(', 'getpwuid_r('),
1289 ('gmtime(', 'gmtime_r('),
1290 ('localtime(', 'localtime_r('),
1291 ('rand(', 'rand_r('),
1292 ('readdir(', 'readdir_r('),
1293 ('strtok(', 'strtok_r('),
1294 ('ttyname(', 'ttyname_r('),
1295 )
1296
1297
1298def CheckPosixThreading(filename, clean_lines, linenum, error):
1299 """Checks for calls to thread-unsafe functions.
1300
1301 Much code has been originally written without consideration of
1302 multi-threading. Also, engineers are relying on their old experience;
1303 they have learned posix before threading extensions were added. These
1304 tests guide the engineers to use thread-safe functions (when using
1305 posix directly).
1306
1307 Args:
1308 filename: The name of the current file.
1309 clean_lines: A CleansedLines instance containing the file.
1310 linenum: The number of the line to check.
1311 error: The function to call with any errors found.
1312 """
1313 line = clean_lines.elided[linenum]
1314 for single_thread_function, multithread_safe_function in threading_list:
1315 ix = line.find(single_thread_function)
erg@google.coma87abb82009-02-24 01:41:01 +00001316 # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
erg@google.com4e00b9a2009-01-12 23:05:11 +00001317 if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and
1318 line[ix - 1] not in ('_', '.', '>'))):
1319 error(filename, linenum, 'runtime/threadsafe_fn', 2,
1320 'Consider using ' + multithread_safe_function +
1321 '...) instead of ' + single_thread_function +
1322 '...) for improved thread safety.')
1323
1324
erg@google.coma868d2d2009-10-09 21:18:45 +00001325# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00001326# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00001327_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00001328 r'^\s*\*\w+(\+\+|--);')
1329
1330
1331def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00001332 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00001333
1334 For example following function:
1335 void increment_counter(int* count) {
1336 *count++;
1337 }
1338 is invalid, because it effectively does count++, moving pointer, and should
1339 be replaced with ++*count, (*count)++ or *count += 1.
1340
1341 Args:
1342 filename: The name of the current file.
1343 clean_lines: A CleansedLines instance containing the file.
1344 linenum: The number of the line to check.
1345 error: The function to call with any errors found.
1346 """
1347 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00001348 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00001349 error(filename, linenum, 'runtime/invalid_increment', 5,
1350 'Changing pointer instead of value (or unused value of operator*).')
1351
1352
erg@google.comd350fe52013-01-14 17:51:48 +00001353class _BlockInfo(object):
1354 """Stores information about a generic block of code."""
1355
1356 def __init__(self, seen_open_brace):
1357 self.seen_open_brace = seen_open_brace
1358 self.open_parentheses = 0
1359 self.inline_asm = _NO_ASM
1360
1361 def CheckBegin(self, filename, clean_lines, linenum, error):
1362 """Run checks that applies to text up to the opening brace.
1363
1364 This is mostly for checking the text after the class identifier
1365 and the "{", usually where the base class is specified. For other
1366 blocks, there isn't much to check, so we always pass.
1367
1368 Args:
1369 filename: The name of the current file.
1370 clean_lines: A CleansedLines instance containing the file.
1371 linenum: The number of the line to check.
1372 error: The function to call with any errors found.
1373 """
1374 pass
1375
1376 def CheckEnd(self, filename, clean_lines, linenum, error):
1377 """Run checks that applies to text after the closing brace.
1378
1379 This is mostly used for checking end of namespace comments.
1380
1381 Args:
1382 filename: The name of the current file.
1383 clean_lines: A CleansedLines instance containing the file.
1384 linenum: The number of the line to check.
1385 error: The function to call with any errors found.
1386 """
1387 pass
1388
1389
1390class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001391 """Stores information about a class."""
1392
erg@google.comd350fe52013-01-14 17:51:48 +00001393 def __init__(self, name, class_or_struct, clean_lines, linenum):
1394 _BlockInfo.__init__(self, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001395 self.name = name
erg@google.comd350fe52013-01-14 17:51:48 +00001396 self.starting_linenum = linenum
erg@google.com4e00b9a2009-01-12 23:05:11 +00001397 self.is_derived = False
erg@google.comd350fe52013-01-14 17:51:48 +00001398 if class_or_struct == 'struct':
1399 self.access = 'public'
1400 else:
1401 self.access = 'private'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001402
erg@google.com8a95ecc2011-09-08 00:45:54 +00001403 # Try to find the end of the class. This will be confused by things like:
1404 # class A {
1405 # } *x = { ...
1406 #
1407 # But it's still good enough for CheckSectionSpacing.
1408 self.last_line = 0
1409 depth = 0
1410 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00001411 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00001412 depth += line.count('{') - line.count('}')
1413 if not depth:
1414 self.last_line = i
1415 break
1416
erg@google.comd350fe52013-01-14 17:51:48 +00001417 def CheckBegin(self, filename, clean_lines, linenum, error):
1418 # Look for a bare ':'
1419 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
1420 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00001421
erg@google.com4e00b9a2009-01-12 23:05:11 +00001422
erg@google.comd350fe52013-01-14 17:51:48 +00001423class _NamespaceInfo(_BlockInfo):
1424 """Stores information about a namespace."""
1425
1426 def __init__(self, name, linenum):
1427 _BlockInfo.__init__(self, False)
1428 self.name = name or ''
1429 self.starting_linenum = linenum
1430
1431 def CheckEnd(self, filename, clean_lines, linenum, error):
1432 """Check end of namespace comments."""
1433 line = clean_lines.raw_lines[linenum]
1434
1435 # Check how many lines is enclosed in this namespace. Don't issue
1436 # warning for missing namespace comments if there aren't enough
1437 # lines. However, do apply checks if there is already an end of
1438 # namespace comment and it's incorrect.
1439 #
1440 # TODO(unknown): We always want to check end of namespace comments
1441 # if a namespace is large, but sometimes we also want to apply the
1442 # check if a short namespace contained nontrivial things (something
1443 # other than forward declarations). There is currently no logic on
1444 # deciding what these nontrivial things are, so this check is
1445 # triggered by namespace size only, which works most of the time.
1446 if (linenum - self.starting_linenum < 10
1447 and not Match(r'};*\s*(//|/\*).*\bnamespace\b', line)):
1448 return
1449
1450 # Look for matching comment at end of namespace.
1451 #
1452 # Note that we accept C style "/* */" comments for terminating
1453 # namespaces, so that code that terminate namespaces inside
1454 # preprocessor macros can be cpplint clean. Example: http://go/nxpiz
1455 #
1456 # We also accept stuff like "// end of namespace <name>." with the
1457 # period at the end.
1458 #
1459 # Besides these, we don't accept anything else, otherwise we might
1460 # get false negatives when existing comment is a substring of the
1461 # expected namespace. Example: http://go/ldkdc, http://cl/23548205
1462 if self.name:
1463 # Named namespace
1464 if not Match((r'};*\s*(//|/\*).*\bnamespace\s+' + re.escape(self.name) +
1465 r'[\*/\.\\\s]*$'),
1466 line):
1467 error(filename, linenum, 'readability/namespace', 5,
1468 'Namespace should be terminated with "// namespace %s"' %
1469 self.name)
1470 else:
1471 # Anonymous namespace
1472 if not Match(r'};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
1473 error(filename, linenum, 'readability/namespace', 5,
1474 'Namespace should be terminated with "// namespace"')
1475
1476
1477class _PreprocessorInfo(object):
1478 """Stores checkpoints of nesting stacks when #if/#else is seen."""
1479
1480 def __init__(self, stack_before_if):
1481 # The entire nesting stack before #if
1482 self.stack_before_if = stack_before_if
1483
1484 # The entire nesting stack up to #else
1485 self.stack_before_else = []
1486
1487 # Whether we have already seen #else or #elif
1488 self.seen_else = False
1489
1490
1491class _NestingState(object):
1492 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001493
1494 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00001495 # Stack for tracking all braces. An object is pushed whenever we
1496 # see a "{", and popped when we see a "}". Only 3 types of
1497 # objects are possible:
1498 # - _ClassInfo: a class or struct.
1499 # - _NamespaceInfo: a namespace.
1500 # - _BlockInfo: some other type of block.
1501 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00001502
erg@google.comd350fe52013-01-14 17:51:48 +00001503 # Stack of _PreprocessorInfo objects.
1504 self.pp_stack = []
1505
1506 def SeenOpenBrace(self):
1507 """Check if we have seen the opening brace for the innermost block.
1508
1509 Returns:
1510 True if we have seen the opening brace, False if the innermost
1511 block is still expecting an opening brace.
1512 """
1513 return (not self.stack) or self.stack[-1].seen_open_brace
1514
1515 def InNamespaceBody(self):
1516 """Check if we are currently one level inside a namespace body.
1517
1518 Returns:
1519 True if top of the stack is a namespace block, False otherwise.
1520 """
1521 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
1522
1523 def UpdatePreprocessor(self, line):
1524 """Update preprocessor stack.
1525
1526 We need to handle preprocessors due to classes like this:
1527 #ifdef SWIG
1528 struct ResultDetailsPageElementExtensionPoint {
1529 #else
1530 struct ResultDetailsPageElementExtensionPoint : public Extension {
1531 #endif
1532 (see http://go/qwddn for original example)
1533
1534 We make the following assumptions (good enough for most files):
1535 - Preprocessor condition evaluates to true from #if up to first
1536 #else/#elif/#endif.
1537
1538 - Preprocessor condition evaluates to false from #else/#elif up
1539 to #endif. We still perform lint checks on these lines, but
1540 these do not affect nesting stack.
1541
1542 Args:
1543 line: current line to check.
1544 """
1545 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
1546 # Beginning of #if block, save the nesting stack here. The saved
1547 # stack will allow us to restore the parsing state in the #else case.
1548 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
1549 elif Match(r'^\s*#\s*(else|elif)\b', line):
1550 # Beginning of #else block
1551 if self.pp_stack:
1552 if not self.pp_stack[-1].seen_else:
1553 # This is the first #else or #elif block. Remember the
1554 # whole nesting stack up to this point. This is what we
1555 # keep after the #endif.
1556 self.pp_stack[-1].seen_else = True
1557 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
1558
1559 # Restore the stack to how it was before the #if
1560 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
1561 else:
1562 # TODO(unknown): unexpected #else, issue warning?
1563 pass
1564 elif Match(r'^\s*#\s*endif\b', line):
1565 # End of #if or #else blocks.
1566 if self.pp_stack:
1567 # If we saw an #else, we will need to restore the nesting
1568 # stack to its former state before the #else, otherwise we
1569 # will just continue from where we left off.
1570 if self.pp_stack[-1].seen_else:
1571 # Here we can just use a shallow copy since we are the last
1572 # reference to it.
1573 self.stack = self.pp_stack[-1].stack_before_else
1574 # Drop the corresponding #if
1575 self.pp_stack.pop()
1576 else:
1577 # TODO(unknown): unexpected #endif, issue warning?
1578 pass
1579
1580 def Update(self, filename, clean_lines, linenum, error):
1581 """Update nesting state with current line.
1582
1583 Args:
1584 filename: The name of the current file.
1585 clean_lines: A CleansedLines instance containing the file.
1586 linenum: The number of the line to check.
1587 error: The function to call with any errors found.
1588 """
1589 line = clean_lines.elided[linenum]
1590
1591 # Update pp_stack first
1592 self.UpdatePreprocessor(line)
1593
1594 # Count parentheses. This is to avoid adding struct arguments to
1595 # the nesting stack.
1596 if self.stack:
1597 inner_block = self.stack[-1]
1598 depth_change = line.count('(') - line.count(')')
1599 inner_block.open_parentheses += depth_change
1600
1601 # Also check if we are starting or ending an inline assembly block.
1602 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
1603 if (depth_change != 0 and
1604 inner_block.open_parentheses == 1 and
1605 _MATCH_ASM.match(line)):
1606 # Enter assembly block
1607 inner_block.inline_asm = _INSIDE_ASM
1608 else:
1609 # Not entering assembly block. If previous line was _END_ASM,
1610 # we will now shift to _NO_ASM state.
1611 inner_block.inline_asm = _NO_ASM
1612 elif (inner_block.inline_asm == _INSIDE_ASM and
1613 inner_block.open_parentheses == 0):
1614 # Exit assembly block
1615 inner_block.inline_asm = _END_ASM
1616
1617 # Consume namespace declaration at the beginning of the line. Do
1618 # this in a loop so that we catch same line declarations like this:
1619 # namespace proto2 { namespace bridge { class MessageSet; } }
1620 while True:
1621 # Match start of namespace. The "\b\s*" below catches namespace
1622 # declarations even if it weren't followed by a whitespace, this
1623 # is so that we don't confuse our namespace checker. The
1624 # missing spaces will be flagged by CheckSpacing.
1625 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
1626 if not namespace_decl_match:
1627 break
1628
1629 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
1630 self.stack.append(new_namespace)
1631
1632 line = namespace_decl_match.group(2)
1633 if line.find('{') != -1:
1634 new_namespace.seen_open_brace = True
1635 line = line[line.find('{') + 1:]
1636
1637 # Look for a class declaration in whatever is left of the line
1638 # after parsing namespaces. The regexp accounts for decorated classes
1639 # such as in:
1640 # class LOCKABLE API Object {
1641 # };
1642 #
1643 # Templates with class arguments may confuse the parser, for example:
1644 # template <class T
1645 # class Comparator = less<T>,
1646 # class Vector = vector<T> >
1647 # class HeapQueue {
1648 #
1649 # Because this parser has no nesting state about templates, by the
1650 # time it saw "class Comparator", it may think that it's a new class.
1651 # Nested templates have a similar problem:
1652 # template <
1653 # typename ExportedType,
1654 # typename TupleType,
1655 # template <typename, typename> class ImplTemplate>
1656 #
1657 # To avoid these cases, we ignore classes that are followed by '=' or '>'
1658 class_decl_match = Match(
1659 r'\s*(template\s*<[\w\s<>,:]*>\s*)?'
1660 '(class|struct)\s+([A-Z_]+\s+)*(\w+(?:::\w+)*)'
1661 '(([^=>]|<[^<>]*>)*)$', line)
1662 if (class_decl_match and
1663 (not self.stack or self.stack[-1].open_parentheses == 0)):
1664 self.stack.append(_ClassInfo(
1665 class_decl_match.group(4), class_decl_match.group(2),
1666 clean_lines, linenum))
1667 line = class_decl_match.group(5)
1668
1669 # If we have not yet seen the opening brace for the innermost block,
1670 # run checks here.
1671 if not self.SeenOpenBrace():
1672 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
1673
1674 # Update access control if we are inside a class/struct
1675 if self.stack and isinstance(self.stack[-1], _ClassInfo):
1676 access_match = Match(r'\s*(public|private|protected)\s*:', line)
1677 if access_match:
1678 self.stack[-1].access = access_match.group(1)
1679
1680 # Consume braces or semicolons from what's left of the line
1681 while True:
1682 # Match first brace, semicolon, or closed parenthesis.
1683 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
1684 if not matched:
1685 break
1686
1687 token = matched.group(1)
1688 if token == '{':
1689 # If namespace or class hasn't seen a opening brace yet, mark
1690 # namespace/class head as complete. Push a new block onto the
1691 # stack otherwise.
1692 if not self.SeenOpenBrace():
1693 self.stack[-1].seen_open_brace = True
1694 else:
1695 self.stack.append(_BlockInfo(True))
1696 if _MATCH_ASM.match(line):
1697 self.stack[-1].inline_asm = _BLOCK_ASM
1698 elif token == ';' or token == ')':
1699 # If we haven't seen an opening brace yet, but we already saw
1700 # a semicolon, this is probably a forward declaration. Pop
1701 # the stack for these.
1702 #
1703 # Similarly, if we haven't seen an opening brace yet, but we
1704 # already saw a closing parenthesis, then these are probably
1705 # function arguments with extra "class" or "struct" keywords.
1706 # Also pop these stack for these.
1707 if not self.SeenOpenBrace():
1708 self.stack.pop()
1709 else: # token == '}'
1710 # Perform end of block checks and pop the stack.
1711 if self.stack:
1712 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
1713 self.stack.pop()
1714 line = matched.group(2)
1715
1716 def InnermostClass(self):
1717 """Get class info on the top of the stack.
1718
1719 Returns:
1720 A _ClassInfo object if we are inside a class, or None otherwise.
1721 """
1722 for i in range(len(self.stack), 0, -1):
1723 classinfo = self.stack[i - 1]
1724 if isinstance(classinfo, _ClassInfo):
1725 return classinfo
1726 return None
1727
1728 def CheckClassFinished(self, filename, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001729 """Checks that all classes have been completely parsed.
1730
1731 Call this when all lines in a file have been processed.
1732 Args:
1733 filename: The name of the current file.
1734 error: The function to call with any errors found.
1735 """
erg@google.comd350fe52013-01-14 17:51:48 +00001736 # Note: This test can result in false positives if #ifdef constructs
1737 # get in the way of brace matching. See the testBuildClass test in
1738 # cpplint_unittest.py for an example of this.
1739 for obj in self.stack:
1740 if isinstance(obj, _ClassInfo):
1741 error(filename, obj.starting_linenum, 'build/class', 5,
1742 'Failed to find complete declaration of class %s' %
1743 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001744
1745
1746def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00001747 nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001748 """Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
1749
1750 Complain about several constructs which gcc-2 accepts, but which are
1751 not standard C++. Warning about these in lint is one way to ease the
1752 transition to new compilers.
1753 - put storage class first (e.g. "static const" instead of "const static").
1754 - "%lld" instead of %qd" in printf-type functions.
1755 - "%1$d" is non-standard in printf-type functions.
1756 - "\%" is an undefined character escape sequence.
1757 - text after #endif is not allowed.
1758 - invalid inner-style forward declaration.
1759 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001760
erg@google.coma868d2d2009-10-09 21:18:45 +00001761 Additionally, check for constructor/destructor style violations and reference
1762 members, as it is very convenient to do so while checking for
1763 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001764
1765 Args:
1766 filename: The name of the current file.
1767 clean_lines: A CleansedLines instance containing the file.
1768 linenum: The number of the line to check.
erg@google.comd350fe52013-01-14 17:51:48 +00001769 nesting_state: A _NestingState instance which maintains information about
1770 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001771 error: A callable to which errors are reported, which takes 4 arguments:
1772 filename, line number, error level, and message
1773 """
1774
1775 # Remove comments from the line, but leave in strings for now.
1776 line = clean_lines.lines[linenum]
1777
1778 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
1779 error(filename, linenum, 'runtime/printf_format', 3,
1780 '%q in format strings is deprecated. Use %ll instead.')
1781
1782 if Search(r'printf\s*\(.*".*%\d+\$', line):
1783 error(filename, linenum, 'runtime/printf_format', 2,
1784 '%N$ formats are unconventional. Try rewriting to avoid them.')
1785
1786 # Remove escaped backslashes before looking for undefined escapes.
1787 line = line.replace('\\\\', '')
1788
1789 if Search(r'("|\').*\\(%|\[|\(|{)', line):
1790 error(filename, linenum, 'build/printf_format', 3,
1791 '%, [, (, and { are undefined character escapes. Unescape them.')
1792
1793 # For the rest, work with both comments and strings removed.
1794 line = clean_lines.elided[linenum]
1795
1796 if Search(r'\b(const|volatile|void|char|short|int|long'
1797 r'|float|double|signed|unsigned'
1798 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00001799 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00001800 line):
1801 error(filename, linenum, 'build/storage_class', 5,
1802 'Storage class (static, extern, typedef, etc) should be first.')
1803
1804 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
1805 error(filename, linenum, 'build/endif_comment', 5,
1806 'Uncommented text after #endif is non-standard. Use a comment.')
1807
1808 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
1809 error(filename, linenum, 'build/forward_decl', 5,
1810 'Inner-style forward declarations are invalid. Remove this line.')
1811
1812 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
1813 line):
1814 error(filename, linenum, 'build/deprecated', 3,
1815 '>? and <? (max and min) operators are non-standard and deprecated.')
1816
erg@google.coma868d2d2009-10-09 21:18:45 +00001817 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
1818 # TODO(unknown): Could it be expanded safely to arbitrary references,
1819 # without triggering too many false positives? The first
1820 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
1821 # the restriction.
1822 # Here's the original regexp, for the reference:
1823 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
1824 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
1825 error(filename, linenum, 'runtime/member_string_references', 2,
1826 'const string& members are dangerous. It is much better to use '
1827 'alternatives, such as pointers or simple constants.')
1828
erg@google.comd350fe52013-01-14 17:51:48 +00001829 # Everything else in this function operates on class declarations.
1830 # Return early if the top of the nesting stack is not a class, or if
1831 # the class head is not completed yet.
1832 classinfo = nesting_state.InnermostClass()
1833 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001834 return
1835
erg@google.com4e00b9a2009-01-12 23:05:11 +00001836 # The class may have been declared with namespace or classname qualifiers.
1837 # The constructor and destructor will not have those qualifiers.
1838 base_classname = classinfo.name.split('::')[-1]
1839
1840 # Look for single-argument constructors that aren't marked explicit.
1841 # Technically a valid construct, but against style.
erg@google.com8a95ecc2011-09-08 00:45:54 +00001842 args = Match(r'\s+(?:inline\s+)?%s\s*\(([^,()]+)\)'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001843 % re.escape(base_classname),
1844 line)
1845 if (args and
1846 args.group(1) != 'void' and
erg@google.com8a95ecc2011-09-08 00:45:54 +00001847 not Match(r'(const\s+)?%s\s*(?:<\w+>\s*)?&' % re.escape(base_classname),
erg@google.com4e00b9a2009-01-12 23:05:11 +00001848 args.group(1).strip())):
1849 error(filename, linenum, 'runtime/explicit', 5,
1850 'Single-argument constructors should be marked explicit.')
1851
erg@google.com4e00b9a2009-01-12 23:05:11 +00001852
1853def CheckSpacingForFunctionCall(filename, line, linenum, error):
1854 """Checks for the correctness of various spacing around function calls.
1855
1856 Args:
1857 filename: The name of the current file.
1858 line: The text of the line to check.
1859 linenum: The number of the line to check.
1860 error: The function to call with any errors found.
1861 """
1862
1863 # Since function calls often occur inside if/for/while/switch
1864 # expressions - which have their own, more liberal conventions - we
1865 # first see if we should be looking inside such an expression for a
1866 # function call, to which we can apply more strict standards.
1867 fncall = line # if there's no control flow construct, look at whole line
1868 for pattern in (r'\bif\s*\((.*)\)\s*{',
1869 r'\bfor\s*\((.*)\)\s*{',
1870 r'\bwhile\s*\((.*)\)\s*[{;]',
1871 r'\bswitch\s*\((.*)\)\s*{'):
1872 match = Search(pattern, line)
1873 if match:
1874 fncall = match.group(1) # look inside the parens for function calls
1875 break
1876
1877 # Except in if/for/while/switch, there should never be space
1878 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
1879 # for nested parens ( (a+b) + c ). Likewise, there should never be
1880 # a space before a ( when it's a function argument. I assume it's a
1881 # function argument when the char before the whitespace is legal in
1882 # a function name (alnum + _) and we're not starting a macro. Also ignore
1883 # pointers and references to arrays and functions coz they're too tricky:
1884 # we use a very simple way to recognize these:
1885 # " (something)(maybe-something)" or
1886 # " (something)(maybe-something," or
1887 # " (something)[something]"
1888 # Note that we assume the contents of [] to be short enough that
1889 # they'll never need to wrap.
1890 if ( # Ignore control structures.
1891 not Search(r'\b(if|for|while|switch|return|delete)\b', fncall) and
1892 # Ignore pointers/references to functions.
1893 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
1894 # Ignore pointers/references to arrays.
1895 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00001896 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00001897 error(filename, linenum, 'whitespace/parens', 4,
1898 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00001899 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001900 error(filename, linenum, 'whitespace/parens', 2,
1901 'Extra space after (')
1902 if (Search(r'\w\s+\(', fncall) and
erg@google.comd350fe52013-01-14 17:51:48 +00001903 not Search(r'#\s*define|typedef', fncall) and
1904 not Search(r'\w\s+\((\w+::)?\*\w+\)\(', fncall)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001905 error(filename, linenum, 'whitespace/parens', 4,
1906 'Extra space before ( in function call')
1907 # If the ) is followed only by a newline or a { + newline, assume it's
1908 # part of a control statement (if/while/etc), and don't complain
1909 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001910 # If the closing parenthesis is preceded by only whitespaces,
1911 # try to give a more descriptive error message.
1912 if Search(r'^\s+\)', fncall):
1913 error(filename, linenum, 'whitespace/parens', 2,
1914 'Closing ) should be moved to the previous line')
1915 else:
1916 error(filename, linenum, 'whitespace/parens', 2,
1917 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001918
1919
1920def IsBlankLine(line):
1921 """Returns true if the given line is blank.
1922
1923 We consider a line to be blank if the line is empty or consists of
1924 only white spaces.
1925
1926 Args:
1927 line: A line of a string.
1928
1929 Returns:
1930 True, if the given line is blank.
1931 """
1932 return not line or line.isspace()
1933
1934
1935def CheckForFunctionLengths(filename, clean_lines, linenum,
1936 function_state, error):
1937 """Reports for long function bodies.
1938
1939 For an overview why this is done, see:
1940 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
1941
1942 Uses a simplistic algorithm assuming other style guidelines
1943 (especially spacing) are followed.
1944 Only checks unindented functions, so class members are unchecked.
1945 Trivial bodies are unchecked, so constructors with huge initializer lists
1946 may be missed.
1947 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00001948 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001949 NOLINT *on the last line of a function* disables this check.
1950
1951 Args:
1952 filename: The name of the current file.
1953 clean_lines: A CleansedLines instance containing the file.
1954 linenum: The number of the line to check.
1955 function_state: Current function name and lines in body so far.
1956 error: The function to call with any errors found.
1957 """
1958 lines = clean_lines.lines
1959 line = lines[linenum]
1960 raw = clean_lines.raw_lines
1961 raw_line = raw[linenum]
1962 joined_line = ''
1963
1964 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00001965 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00001966 match_result = Match(regexp, line)
1967 if match_result:
1968 # If the name is all caps and underscores, figure it's a macro and
1969 # ignore it, unless it's TEST or TEST_F.
1970 function_name = match_result.group(1).split()[-1]
1971 if function_name == 'TEST' or function_name == 'TEST_F' or (
1972 not Match(r'[A-Z_]+$', function_name)):
1973 starting_func = True
1974
1975 if starting_func:
1976 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00001977 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001978 start_line = lines[start_linenum]
1979 joined_line += ' ' + start_line.lstrip()
1980 if Search(r'(;|})', start_line): # Declarations and trivial functions
1981 body_found = True
1982 break # ... ignore
1983 elif Search(r'{', start_line):
1984 body_found = True
1985 function = Search(r'((\w|:)*)\(', line).group(1)
1986 if Match(r'TEST', function): # Handle TEST... macros
1987 parameter_regexp = Search(r'(\(.*\))', joined_line)
1988 if parameter_regexp: # Ignore bad syntax
1989 function += parameter_regexp.group(1)
1990 else:
1991 function += '()'
1992 function_state.Begin(function)
1993 break
1994 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00001995 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001996 error(filename, linenum, 'readability/fn_size', 5,
1997 'Lint failed to find start of function body.')
1998 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00001999 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002000 function_state.End()
2001 elif not Match(r'^\s*$', line):
2002 function_state.Count() # Count non-blank/non-comment lines.
2003
2004
2005_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
2006
2007
2008def CheckComment(comment, filename, linenum, error):
2009 """Checks for common mistakes in TODO comments.
2010
2011 Args:
2012 comment: The text of the comment from the line in question.
2013 filename: The name of the current file.
2014 linenum: The number of the line to check.
2015 error: The function to call with any errors found.
2016 """
2017 match = _RE_PATTERN_TODO.match(comment)
2018 if match:
2019 # One whitespace is correct; zero whitespace is handled elsewhere.
2020 leading_whitespace = match.group(1)
2021 if len(leading_whitespace) > 1:
2022 error(filename, linenum, 'whitespace/todo', 2,
2023 'Too many spaces before TODO')
2024
2025 username = match.group(2)
2026 if not username:
2027 error(filename, linenum, 'readability/todo', 2,
2028 'Missing username in TODO; it should look like '
2029 '"// TODO(my_username): Stuff."')
2030
2031 middle_whitespace = match.group(3)
erg@google.coma87abb82009-02-24 01:41:01 +00002032 # Comparisons made explicit for correctness -- pylint: disable-msg=C6403
erg@google.com4e00b9a2009-01-12 23:05:11 +00002033 if middle_whitespace != ' ' and middle_whitespace != '':
2034 error(filename, linenum, 'whitespace/todo', 2,
2035 'TODO(my_username) should be followed by a space')
2036
erg@google.comd350fe52013-01-14 17:51:48 +00002037def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
2038 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002039
erg@google.comd350fe52013-01-14 17:51:48 +00002040 Args:
2041 filename: The name of the current file.
2042 clean_lines: A CleansedLines instance containing the file.
2043 linenum: The number of the line to check.
2044 nesting_state: A _NestingState instance which maintains information about
2045 the current stack of nested blocks being parsed.
2046 error: The function to call with any errors found.
2047 """
2048 line = clean_lines.elided[linenum] # get rid of comments and strings
2049
2050 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
2051 r'DISALLOW_EVIL_CONSTRUCTORS|'
2052 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
2053 if not matched:
2054 return
2055 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
2056 if nesting_state.stack[-1].access != 'private':
2057 error(filename, linenum, 'readability/constructors', 3,
2058 '%s must be in the private: section' % matched.group(1))
2059
2060 else:
2061 # Found DISALLOW* macro outside a class declaration, or perhaps it
2062 # was used inside a function when it should have been part of the
2063 # class declaration. We could issue a warning here, but it
2064 # probably resulted in a compiler error already.
2065 pass
2066
2067
2068def FindNextMatchingAngleBracket(clean_lines, linenum, init_suffix):
2069 """Find the corresponding > to close a template.
2070
2071 Args:
2072 clean_lines: A CleansedLines instance containing the file.
2073 linenum: Current line number.
2074 init_suffix: Remainder of the current line after the initial <.
2075
2076 Returns:
2077 True if a matching bracket exists.
2078 """
2079 line = init_suffix
2080 nesting_stack = ['<']
2081 while True:
2082 # Find the next operator that can tell us whether < is used as an
2083 # opening bracket or as a less-than operator. We only want to
2084 # warn on the latter case.
2085 #
2086 # We could also check all other operators and terminate the search
2087 # early, e.g. if we got something like this "a<b+c", the "<" is
2088 # most likely a less-than operator, but then we will get false
2089 # positives for default arguments (e.g. http://go/prccd) and
2090 # other template expressions (e.g. http://go/oxcjq).
2091 match = Search(r'^[^<>(),;\[\]]*([<>(),;\[\]])(.*)$', line)
2092 if match:
2093 # Found an operator, update nesting stack
2094 operator = match.group(1)
2095 line = match.group(2)
2096
2097 if nesting_stack[-1] == '<':
2098 # Expecting closing angle bracket
2099 if operator in ('<', '(', '['):
2100 nesting_stack.append(operator)
2101 elif operator == '>':
2102 nesting_stack.pop()
2103 if not nesting_stack:
2104 # Found matching angle bracket
2105 return True
2106 elif operator == ',':
2107 # Got a comma after a bracket, this is most likely a template
2108 # argument. We have not seen a closing angle bracket yet, but
2109 # it's probably a few lines later if we look for it, so just
2110 # return early here.
2111 return True
2112 else:
2113 # Got some other operator.
2114 return False
2115
2116 else:
2117 # Expecting closing parenthesis or closing bracket
2118 if operator in ('<', '(', '['):
2119 nesting_stack.append(operator)
2120 elif operator in (')', ']'):
2121 # We don't bother checking for matching () or []. If we got
2122 # something like (] or [), it would have been a syntax error.
2123 nesting_stack.pop()
2124
2125 else:
2126 # Scan the next line
2127 linenum += 1
2128 if linenum >= len(clean_lines.elided):
2129 break
2130 line = clean_lines.elided[linenum]
2131
2132 # Exhausted all remaining lines and still no matching angle bracket.
2133 # Most likely the input was incomplete, otherwise we should have
2134 # seen a semicolon and returned early.
2135 return True
2136
2137
2138def FindPreviousMatchingAngleBracket(clean_lines, linenum, init_prefix):
2139 """Find the corresponding < that started a template.
2140
2141 Args:
2142 clean_lines: A CleansedLines instance containing the file.
2143 linenum: Current line number.
2144 init_prefix: Part of the current line before the initial >.
2145
2146 Returns:
2147 True if a matching bracket exists.
2148 """
2149 line = init_prefix
2150 nesting_stack = ['>']
2151 while True:
2152 # Find the previous operator
2153 match = Search(r'^(.*)([<>(),;\[\]])[^<>(),;\[\]]*$', line)
2154 if match:
2155 # Found an operator, update nesting stack
2156 operator = match.group(2)
2157 line = match.group(1)
2158
2159 if nesting_stack[-1] == '>':
2160 # Expecting opening angle bracket
2161 if operator in ('>', ')', ']'):
2162 nesting_stack.append(operator)
2163 elif operator == '<':
2164 nesting_stack.pop()
2165 if not nesting_stack:
2166 # Found matching angle bracket
2167 return True
2168 elif operator == ',':
2169 # Got a comma before a bracket, this is most likely a
2170 # template argument. The opening angle bracket is probably
2171 # there if we look for it, so just return early here.
2172 return True
2173 else:
2174 # Got some other operator.
2175 return False
2176
2177 else:
2178 # Expecting opening parenthesis or opening bracket
2179 if operator in ('>', ')', ']'):
2180 nesting_stack.append(operator)
2181 elif operator in ('(', '['):
2182 nesting_stack.pop()
2183
2184 else:
2185 # Scan the previous line
2186 linenum -= 1
2187 if linenum < 0:
2188 break
2189 line = clean_lines.elided[linenum]
2190
2191 # Exhausted all earlier lines and still no matching angle bracket.
2192 return False
2193
2194
2195def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002196 """Checks for the correctness of various spacing issues in the code.
2197
2198 Things we check for: spaces around operators, spaces after
2199 if/for/while/switch, no spaces around parens in function calls, two
2200 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00002201 line, don't end a function with a blank line, don't add a blank line
2202 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002203
2204 Args:
2205 filename: The name of the current file.
2206 clean_lines: A CleansedLines instance containing the file.
2207 linenum: The number of the line to check.
erg@google.comd350fe52013-01-14 17:51:48 +00002208 nesting_state: A _NestingState instance which maintains information about
2209 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002210 error: The function to call with any errors found.
2211 """
2212
2213 raw = clean_lines.raw_lines
2214 line = raw[linenum]
2215
2216 # Before nixing comments, check if the line is blank for no good
2217 # reason. This includes the first line after a block is opened, and
2218 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00002219 #
2220 # Skip all the blank line checks if we are immediately inside a
2221 # namespace body. In other words, don't issue blank line warnings
2222 # for this block:
2223 # namespace {
2224 #
2225 # }
2226 #
2227 # A warning about missing end of namespace comments will be issued instead.
2228 if IsBlankLine(line) and not nesting_state.InNamespaceBody():
erg@google.com4e00b9a2009-01-12 23:05:11 +00002229 elided = clean_lines.elided
2230 prev_line = elided[linenum - 1]
2231 prevbrace = prev_line.rfind('{')
2232 # TODO(unknown): Don't complain if line before blank line, and line after,
2233 # both start with alnums and are indented the same amount.
2234 # This ignores whitespace at the start of a namespace block
2235 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00002236 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002237 # OK, we have a blank line at the start of a code block. Before we
2238 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00002239 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00002240 # 4 spaces (because they did not fit in a 80 column line when placed on
2241 # the same line as the function name). We also check for the case where
2242 # the previous line is indented 6 spaces, which may happen when the
2243 # initializers of a constructor do not fit into a 80 column line.
2244 exception = False
2245 if Match(r' {6}\w', prev_line): # Initializer list?
2246 # We are looking for the opening column of initializer list, which
2247 # should be indented 4 spaces to cause 6 space indentation afterwards.
2248 search_position = linenum-2
2249 while (search_position >= 0
2250 and Match(r' {6}\w', elided[search_position])):
2251 search_position -= 1
2252 exception = (search_position >= 0
2253 and elided[search_position][:5] == ' :')
2254 else:
2255 # Search for the function arguments or an initializer list. We use a
2256 # simple heuristic here: If the line is indented 4 spaces; and we have a
2257 # closing paren, without the opening paren, followed by an opening brace
2258 # or colon (for initializer lists) we assume that it is the last line of
2259 # a function header. If we have a colon indented 4 spaces, it is an
2260 # initializer list.
2261 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
2262 prev_line)
2263 or Match(r' {4}:', prev_line))
2264
2265 if not exception:
2266 error(filename, linenum, 'whitespace/blank_line', 2,
2267 'Blank line at the start of a code block. Is this needed?')
erg@google.comd350fe52013-01-14 17:51:48 +00002268 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00002269 # chain, like this:
2270 # if (condition1) {
2271 # // Something followed by a blank line
2272 #
2273 # } else if (condition2) {
2274 # // Something else
2275 # }
2276 if linenum + 1 < clean_lines.NumLines():
2277 next_line = raw[linenum + 1]
2278 if (next_line
2279 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002280 and next_line.find('} else ') == -1):
2281 error(filename, linenum, 'whitespace/blank_line', 3,
2282 'Blank line at the end of a code block. Is this needed?')
2283
erg@google.com8a95ecc2011-09-08 00:45:54 +00002284 matched = Match(r'\s*(public|protected|private):', prev_line)
2285 if matched:
2286 error(filename, linenum, 'whitespace/blank_line', 3,
2287 'Do not leave a blank line after "%s:"' % matched.group(1))
2288
erg@google.com4e00b9a2009-01-12 23:05:11 +00002289 # Next, we complain if there's a comment too near the text
2290 commentpos = line.find('//')
2291 if commentpos != -1:
2292 # Check if the // may be in quotes. If so, ignore it
erg@google.coma87abb82009-02-24 01:41:01 +00002293 # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
erg@google.com4e00b9a2009-01-12 23:05:11 +00002294 if (line.count('"', 0, commentpos) -
2295 line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes
2296 # Allow one space for new scopes, two spaces otherwise:
2297 if (not Match(r'^\s*{ //', line) and
2298 ((commentpos >= 1 and
2299 line[commentpos-1] not in string.whitespace) or
2300 (commentpos >= 2 and
2301 line[commentpos-2] not in string.whitespace))):
2302 error(filename, linenum, 'whitespace/comments', 2,
2303 'At least two spaces is best between code and comments')
2304 # There should always be a space between the // and the comment
2305 commentend = commentpos + 2
2306 if commentend < len(line) and not line[commentend] == ' ':
2307 # but some lines are exceptions -- e.g. if they're big
2308 # comment delimiters like:
2309 # //----------------------------------------------------------
erg@google.coma51c16b2010-11-17 18:09:31 +00002310 # or are an empty C++ style Doxygen comment, like:
2311 # ///
erg@google.come35f7652009-06-19 20:52:09 +00002312 # or they begin with multiple slashes followed by a space:
2313 # //////// Header comment
2314 match = (Search(r'[=/-]{4,}\s*$', line[commentend:]) or
erg@google.coma51c16b2010-11-17 18:09:31 +00002315 Search(r'^/$', line[commentend:]) or
erg@google.come35f7652009-06-19 20:52:09 +00002316 Search(r'^/+ ', line[commentend:]))
erg@google.com4e00b9a2009-01-12 23:05:11 +00002317 if not match:
2318 error(filename, linenum, 'whitespace/comments', 4,
2319 'Should have a space between // and comment')
2320 CheckComment(line[commentpos:], filename, linenum, error)
2321
2322 line = clean_lines.elided[linenum] # get rid of comments and strings
2323
2324 # Don't try to do spacing checks for operator methods
2325 line = re.sub(r'operator(==|!=|<|<<|<=|>=|>>|>)\(', 'operator\(', line)
2326
2327 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
2328 # Otherwise not. Note we only check for non-spaces on *both* sides;
2329 # sometimes people put non-spaces on one side when aligning ='s among
2330 # many lines (not that this is behavior that I approve of...)
2331 if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line):
2332 error(filename, linenum, 'whitespace/operators', 4,
2333 'Missing spaces around =')
2334
2335 # It's ok not to have spaces around binary operators like + - * /, but if
2336 # there's too little whitespace, we get concerned. It's hard to tell,
2337 # though, so we punt on this one for now. TODO.
2338
2339 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00002340 #
2341 # Check <= and >= first to avoid false positives with < and >, then
2342 # check non-include lines for spacing around < and >.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002343 match = Search(r'[^<>=!\s](==|!=|<=|>=)[^<>=!\s]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002344 if match:
2345 error(filename, linenum, 'whitespace/operators', 3,
2346 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00002347 # We allow no-spaces around << when used like this: 10<<20, but
erg@google.com4e00b9a2009-01-12 23:05:11 +00002348 # not otherwise (particularly, not when used as streams)
erg@google.comd350fe52013-01-14 17:51:48 +00002349 match = Search(r'(\S)(?:L|UL|ULL|l|ul|ull)?<<(\S)', line)
2350 if match and not (match.group(1).isdigit() and match.group(2).isdigit()):
2351 error(filename, linenum, 'whitespace/operators', 3,
2352 'Missing spaces around <<')
2353 elif not Match(r'#.*include', line):
2354 # Avoid false positives on ->
2355 reduced_line = line.replace('->', '')
2356
2357 # Look for < that is not surrounded by spaces. This is only
2358 # triggered if both sides are missing spaces, even though
2359 # technically should should flag if at least one side is missing a
2360 # space. This is done to avoid some false positives with shifts.
2361 match = Search(r'[^\s<]<([^\s=<].*)', reduced_line)
2362 if (match and
2363 not FindNextMatchingAngleBracket(clean_lines, linenum, match.group(1))):
2364 error(filename, linenum, 'whitespace/operators', 3,
2365 'Missing spaces around <')
2366
2367 # Look for > that is not surrounded by spaces. Similar to the
2368 # above, we only trigger if both sides are missing spaces to avoid
2369 # false positives with shifts.
2370 match = Search(r'^(.*[^\s>])>[^\s=>]', reduced_line)
2371 if (match and
2372 not FindPreviousMatchingAngleBracket(clean_lines, linenum,
2373 match.group(1))):
2374 error(filename, linenum, 'whitespace/operators', 3,
2375 'Missing spaces around >')
2376
2377 # We allow no-spaces around >> for almost anything. This is because
2378 # C++11 allows ">>" to close nested templates, which accounts for
2379 # most cases when ">>" is not followed by a space.
2380 #
2381 # We still warn on ">>" followed by alpha character, because that is
2382 # likely due to ">>" being used for right shifts, e.g.:
2383 # value >> alpha
2384 #
2385 # When ">>" is used to close templates, the alphanumeric letter that
2386 # follows would be part of an identifier, and there should still be
2387 # a space separating the template type and the identifier.
2388 # type<type<type>> alpha
2389 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002390 if match:
2391 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00002392 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002393
2394 # There shouldn't be space around unary operators
2395 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
2396 if match:
2397 error(filename, linenum, 'whitespace/operators', 4,
2398 'Extra space for operator %s' % match.group(1))
2399
2400 # A pet peeve of mine: no spaces after an if, while, switch, or for
2401 match = Search(r' (if\(|for\(|while\(|switch\()', line)
2402 if match:
2403 error(filename, linenum, 'whitespace/parens', 5,
2404 'Missing space before ( in %s' % match.group(1))
2405
2406 # For if/for/while/switch, the left and right parens should be
2407 # consistent about how many spaces are inside the parens, and
2408 # there should either be zero or one spaces inside the parens.
2409 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00002410 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002411 match = Search(r'\b(if|for|while|switch)\s*'
2412 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
2413 line)
2414 if match:
2415 if len(match.group(2)) != len(match.group(4)):
2416 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00002417 len(match.group(2)) == 1 + len(match.group(4)) or
2418 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002419 error(filename, linenum, 'whitespace/parens', 5,
2420 'Mismatching spaces inside () in %s' % match.group(1))
2421 if not len(match.group(2)) in [0, 1]:
2422 error(filename, linenum, 'whitespace/parens', 5,
2423 'Should have zero or one spaces inside ( and ) in %s' %
2424 match.group(1))
2425
2426 # You should always have a space after a comma (either as fn arg or operator)
2427 if Search(r',[^\s]', line):
2428 error(filename, linenum, 'whitespace/comma', 3,
2429 'Missing space after ,')
2430
erg@google.comd7d27472011-09-07 17:36:35 +00002431 # You should always have a space after a semicolon
2432 # except for few corner cases
2433 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
2434 # space after ;
2435 if Search(r';[^\s};\\)/]', line):
2436 error(filename, linenum, 'whitespace/semicolon', 3,
2437 'Missing space after ;')
2438
erg@google.com4e00b9a2009-01-12 23:05:11 +00002439 # Next we will look for issues with function calls.
2440 CheckSpacingForFunctionCall(filename, line, linenum, error)
2441
erg@google.com8a95ecc2011-09-08 00:45:54 +00002442 # Except after an opening paren, or after another opening brace (in case of
2443 # an initializer list, for instance), you should have spaces before your
2444 # braces. And since you should never have braces at the beginning of a line,
2445 # this is an easy test.
2446 if Search(r'[^ ({]{', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002447 error(filename, linenum, 'whitespace/braces', 5,
2448 'Missing space before {')
2449
2450 # Make sure '} else {' has spaces.
2451 if Search(r'}else', line):
2452 error(filename, linenum, 'whitespace/braces', 5,
2453 'Missing space before else')
2454
2455 # You shouldn't have spaces before your brackets, except maybe after
2456 # 'delete []' or 'new char * []'.
2457 if Search(r'\w\s+\[', line) and not Search(r'delete\s+\[', line):
2458 error(filename, linenum, 'whitespace/braces', 5,
2459 'Extra space before [')
2460
2461 # You shouldn't have a space before a semicolon at the end of the line.
2462 # There's a special case for "for" since the style guide allows space before
2463 # the semicolon there.
2464 if Search(r':\s*;\s*$', line):
2465 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00002466 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002467 elif Search(r'^\s*;\s*$', line):
2468 error(filename, linenum, 'whitespace/semicolon', 5,
2469 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00002470 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002471 elif (Search(r'\s+;\s*$', line) and
2472 not Search(r'\bfor\b', line)):
2473 error(filename, linenum, 'whitespace/semicolon', 5,
2474 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00002475 'statement, use {} instead.')
2476
2477 # In range-based for, we wanted spaces before and after the colon, but
2478 # not around "::" tokens that might appear.
2479 if (Search('for *\(.*[^:]:[^: ]', line) or
2480 Search('for *\(.*[^: ]:[^:]', line)):
2481 error(filename, linenum, 'whitespace/forcolon', 2,
2482 'Missing space around colon in range-based for loop')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002483
2484
erg@google.com8a95ecc2011-09-08 00:45:54 +00002485def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
2486 """Checks for additional blank line issues related to sections.
2487
2488 Currently the only thing checked here is blank line before protected/private.
2489
2490 Args:
2491 filename: The name of the current file.
2492 clean_lines: A CleansedLines instance containing the file.
2493 class_info: A _ClassInfo objects.
2494 linenum: The number of the line to check.
2495 error: The function to call with any errors found.
2496 """
2497 # Skip checks if the class is small, where small means 25 lines or less.
2498 # 25 lines seems like a good cutoff since that's the usual height of
2499 # terminals, and any class that can't fit in one screen can't really
2500 # be considered "small".
2501 #
2502 # Also skip checks if we are on the first line. This accounts for
2503 # classes that look like
2504 # class Foo { public: ... };
2505 #
2506 # If we didn't find the end of the class, last_line would be zero,
2507 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00002508 if (class_info.last_line - class_info.starting_linenum <= 24 or
2509 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002510 return
2511
2512 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
2513 if matched:
2514 # Issue warning if the line before public/protected/private was
2515 # not a blank line, but don't do this if the previous line contains
2516 # "class" or "struct". This can happen two ways:
2517 # - We are at the beginning of the class.
2518 # - We are forward-declaring an inner class that is semantically
2519 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00002520 # Also ignores cases where the previous line ends with a backslash as can be
2521 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00002522 prev_line = clean_lines.lines[linenum - 1]
2523 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00002524 not Search(r'\b(class|struct)\b', prev_line) and
2525 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002526 # Try a bit harder to find the beginning of the class. This is to
2527 # account for multi-line base-specifier lists, e.g.:
2528 # class Derived
2529 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00002530 end_class_head = class_info.starting_linenum
2531 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002532 if Search(r'\{\s*$', clean_lines.lines[i]):
2533 end_class_head = i
2534 break
2535 if end_class_head < linenum - 1:
2536 error(filename, linenum, 'whitespace/blank_line', 3,
2537 '"%s:" should be preceded by a blank line' % matched.group(1))
2538
2539
erg@google.com4e00b9a2009-01-12 23:05:11 +00002540def GetPreviousNonBlankLine(clean_lines, linenum):
2541 """Return the most recent non-blank line and its line number.
2542
2543 Args:
2544 clean_lines: A CleansedLines instance containing the file contents.
2545 linenum: The number of the line to check.
2546
2547 Returns:
2548 A tuple with two elements. The first element is the contents of the last
2549 non-blank line before the current line, or the empty string if this is the
2550 first non-blank line. The second is the line number of that line, or -1
2551 if this is the first non-blank line.
2552 """
2553
2554 prevlinenum = linenum - 1
2555 while prevlinenum >= 0:
2556 prevline = clean_lines.elided[prevlinenum]
2557 if not IsBlankLine(prevline): # if not a blank line...
2558 return (prevline, prevlinenum)
2559 prevlinenum -= 1
2560 return ('', -1)
2561
2562
2563def CheckBraces(filename, clean_lines, linenum, error):
2564 """Looks for misplaced braces (e.g. at the end of line).
2565
2566 Args:
2567 filename: The name of the current file.
2568 clean_lines: A CleansedLines instance containing the file.
2569 linenum: The number of the line to check.
2570 error: The function to call with any errors found.
2571 """
2572
2573 line = clean_lines.elided[linenum] # get rid of comments and strings
2574
2575 if Match(r'\s*{\s*$', line):
2576 # We allow an open brace to start a line in the case where someone
2577 # is using braces in a block to explicitly create a new scope,
2578 # which is commonly used to control the lifetime of
2579 # stack-allocated variables. We don't detect this perfectly: we
2580 # just don't complain if the last non-whitespace character on the
erg@google.comd350fe52013-01-14 17:51:48 +00002581 # previous non-blank line is ';', ':', '{', or '}', or if the previous
2582 # line starts a preprocessor block.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002583 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.comd350fe52013-01-14 17:51:48 +00002584 if (not Search(r'[;:}{]\s*$', prevline) and
2585 not Match(r'\s*#', prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002586 error(filename, linenum, 'whitespace/braces', 4,
2587 '{ should almost always be at the end of the previous line')
2588
2589 # An else clause should be on the same line as the preceding closing brace.
2590 if Match(r'\s*else\s*', line):
2591 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
2592 if Match(r'\s*}\s*$', prevline):
2593 error(filename, linenum, 'whitespace/newline', 4,
2594 'An else should appear on the same line as the preceding }')
2595
2596 # If braces come on one side of an else, they should be on both.
2597 # However, we have to worry about "else if" that spans multiple lines!
2598 if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
2599 if Search(r'}\s*else if([^{]*)$', line): # could be multi-line if
2600 # find the ( after the if
2601 pos = line.find('else if')
2602 pos = line.find('(', pos)
2603 if pos > 0:
2604 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
2605 if endline[endpos:].find('{') == -1: # must be brace after if
2606 error(filename, linenum, 'readability/braces', 5,
2607 'If an else has a brace on one side, it should have it on both')
2608 else: # common case: else not followed by a multi-line if
2609 error(filename, linenum, 'readability/braces', 5,
2610 'If an else has a brace on one side, it should have it on both')
2611
2612 # Likewise, an else should never have the else clause on the same line
2613 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
2614 error(filename, linenum, 'whitespace/newline', 4,
2615 'Else clause should never be on same line as else (use 2 lines)')
2616
2617 # In the same way, a do/while should never be on one line
2618 if Match(r'\s*do [^\s{]', line):
2619 error(filename, linenum, 'whitespace/newline', 4,
2620 'do/while clauses should not be on a single line')
2621
2622 # Braces shouldn't be followed by a ; unless they're defining a struct
2623 # or initializing an array.
2624 # We can't tell in general, but we can for some common cases.
2625 prevlinenum = linenum
2626 while True:
2627 (prevline, prevlinenum) = GetPreviousNonBlankLine(clean_lines, prevlinenum)
2628 if Match(r'\s+{.*}\s*;', line) and not prevline.count(';'):
2629 line = prevline + line
2630 else:
2631 break
2632 if (Search(r'{.*}\s*;', line) and
2633 line.count('{') == line.count('}') and
2634 not Search(r'struct|class|enum|\s*=\s*{', line)):
2635 error(filename, linenum, 'readability/braces', 4,
2636 "You don't need a ; after a }")
2637
2638
erg@google.comd350fe52013-01-14 17:51:48 +00002639def CheckEmptyLoopBody(filename, clean_lines, linenum, error):
2640 """Loop for empty loop body with only a single semicolon.
2641
2642 Args:
2643 filename: The name of the current file.
2644 clean_lines: A CleansedLines instance containing the file.
2645 linenum: The number of the line to check.
2646 error: The function to call with any errors found.
2647 """
2648
2649 # Search for loop keywords at the beginning of the line. Because only
2650 # whitespaces are allowed before the keywords, this will also ignore most
2651 # do-while-loops, since those lines should start with closing brace.
2652 line = clean_lines.elided[linenum]
2653 if Match(r'\s*(for|while)\s*\(', line):
2654 # Find the end of the conditional expression
2655 (end_line, end_linenum, end_pos) = CloseExpression(
2656 clean_lines, linenum, line.find('('))
2657
2658 # Output warning if what follows the condition expression is a semicolon.
2659 # No warning for all other cases, including whitespace or newline, since we
2660 # have a separate check for semicolons preceded by whitespace.
2661 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
2662 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
2663 'Empty loop bodies should use {} or continue')
2664
2665
erg@google.com4e00b9a2009-01-12 23:05:11 +00002666def ReplaceableCheck(operator, macro, line):
2667 """Determine whether a basic CHECK can be replaced with a more specific one.
2668
2669 For example suggest using CHECK_EQ instead of CHECK(a == b) and
2670 similarly for CHECK_GE, CHECK_GT, CHECK_LE, CHECK_LT, CHECK_NE.
2671
2672 Args:
2673 operator: The C++ operator used in the CHECK.
2674 macro: The CHECK or EXPECT macro being called.
2675 line: The current source line.
2676
2677 Returns:
2678 True if the CHECK can be replaced with a more specific one.
2679 """
2680
2681 # This matches decimal and hex integers, strings, and chars (in that order).
2682 match_constant = r'([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')'
2683
2684 # Expression to match two sides of the operator with something that
2685 # looks like a literal, since CHECK(x == iterator) won't compile.
2686 # This means we can't catch all the cases where a more specific
2687 # CHECK is possible, but it's less annoying than dealing with
2688 # extraneous warnings.
2689 match_this = (r'\s*' + macro + r'\((\s*' +
2690 match_constant + r'\s*' + operator + r'[^<>].*|'
2691 r'.*[^<>]' + operator + r'\s*' + match_constant +
2692 r'\s*\))')
2693
2694 # Don't complain about CHECK(x == NULL) or similar because
2695 # CHECK_EQ(x, NULL) won't compile (requires a cast).
2696 # Also, don't complain about more complex boolean expressions
2697 # involving && or || such as CHECK(a == b || c == d).
2698 return Match(match_this, line) and not Search(r'NULL|&&|\|\|', line)
2699
2700
2701def CheckCheck(filename, clean_lines, linenum, error):
2702 """Checks the use of CHECK and EXPECT macros.
2703
2704 Args:
2705 filename: The name of the current file.
2706 clean_lines: A CleansedLines instance containing the file.
2707 linenum: The number of the line to check.
2708 error: The function to call with any errors found.
2709 """
2710
2711 # Decide the set of replacement macros that should be suggested
2712 raw_lines = clean_lines.raw_lines
2713 current_macro = ''
2714 for macro in _CHECK_MACROS:
2715 if raw_lines[linenum].find(macro) >= 0:
2716 current_macro = macro
2717 break
2718 if not current_macro:
2719 # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT'
2720 return
2721
2722 line = clean_lines.elided[linenum] # get rid of comments and strings
2723
2724 # Encourage replacing plain CHECKs with CHECK_EQ/CHECK_NE/etc.
2725 for operator in ['==', '!=', '>=', '>', '<=', '<']:
2726 if ReplaceableCheck(operator, current_macro, line):
2727 error(filename, linenum, 'readability/check', 2,
2728 'Consider using %s instead of %s(a %s b)' % (
2729 _CHECK_REPLACEMENT[current_macro][operator],
2730 current_macro, operator))
2731 break
2732
2733
erg@google.comd350fe52013-01-14 17:51:48 +00002734def CheckAltTokens(filename, clean_lines, linenum, error):
2735 """Check alternative keywords being used in boolean expressions.
2736
2737 Args:
2738 filename: The name of the current file.
2739 clean_lines: A CleansedLines instance containing the file.
2740 linenum: The number of the line to check.
2741 error: The function to call with any errors found.
2742 """
2743 line = clean_lines.elided[linenum]
2744
2745 # Avoid preprocessor lines
2746 if Match(r'^\s*#', line):
2747 return
2748
2749 # Last ditch effort to avoid multi-line comments. This will not help
2750 # if the comment started before the current line or ended after the
2751 # current line, but it catches most of the false positives. At least,
2752 # it provides a way to workaround this warning for people who use
2753 # multi-line comments in preprocessor macros.
2754 #
2755 # TODO(unknown): remove this once cpplint has better support for
2756 # multi-line comments.
2757 if line.find('/*') >= 0 or line.find('*/') >= 0:
2758 return
2759
2760 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
2761 error(filename, linenum, 'readability/alt_tokens', 2,
2762 'Use operator %s instead of %s' % (
2763 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
2764
2765
erg@google.com4e00b9a2009-01-12 23:05:11 +00002766def GetLineWidth(line):
2767 """Determines the width of the line in column positions.
2768
2769 Args:
2770 line: A string, which may be a Unicode string.
2771
2772 Returns:
2773 The width of the line in column positions, accounting for Unicode
2774 combining characters and wide characters.
2775 """
2776 if isinstance(line, unicode):
2777 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00002778 for uc in unicodedata.normalize('NFC', line):
2779 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002780 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00002781 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002782 width += 1
2783 return width
2784 else:
2785 return len(line)
2786
2787
erg@google.comd350fe52013-01-14 17:51:48 +00002788def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00002789 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002790 """Checks rules from the 'C++ style rules' section of cppguide.html.
2791
2792 Most of these rules are hard to test (naming, comment style), but we
2793 do what we can. In particular we check for 2-space indents, line lengths,
2794 tab usage, spaces inside code, etc.
2795
2796 Args:
2797 filename: The name of the current file.
2798 clean_lines: A CleansedLines instance containing the file.
2799 linenum: The number of the line to check.
2800 file_extension: The extension (without the dot) of the filename.
erg@google.comd350fe52013-01-14 17:51:48 +00002801 nesting_state: A _NestingState instance which maintains information about
2802 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002803 error: The function to call with any errors found.
2804 """
2805
2806 raw_lines = clean_lines.raw_lines
2807 line = raw_lines[linenum]
2808
2809 if line.find('\t') != -1:
2810 error(filename, linenum, 'whitespace/tab', 1,
2811 'Tab found; better to use spaces')
2812
2813 # One or three blank spaces at the beginning of the line is weird; it's
2814 # hard to reconcile that with 2-space indents.
2815 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
2816 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
2817 # if(RLENGTH > 20) complain = 0;
2818 # if(match($0, " +(error|private|public|protected):")) complain = 0;
2819 # if(match(prev, "&& *$")) complain = 0;
2820 # if(match(prev, "\\|\\| *$")) complain = 0;
2821 # if(match(prev, "[\",=><] *$")) complain = 0;
2822 # if(match($0, " <<")) complain = 0;
2823 # if(match(prev, " +for \\(")) complain = 0;
2824 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
2825 initial_spaces = 0
2826 cleansed_line = clean_lines.elided[linenum]
2827 while initial_spaces < len(line) and line[initial_spaces] == ' ':
2828 initial_spaces += 1
2829 if line and line[-1].isspace():
2830 error(filename, linenum, 'whitespace/end_of_line', 4,
2831 'Line ends in whitespace. Consider deleting these extra spaces.')
2832 # There are certain situations we allow one space, notably for labels
2833 elif ((initial_spaces == 1 or initial_spaces == 3) and
2834 not Match(r'\s*\w+\s*:\s*$', cleansed_line)):
2835 error(filename, linenum, 'whitespace/indent', 3,
2836 'Weird number of spaces at line-start. '
2837 'Are you using a 2-space indent?')
2838 # Labels should always be indented at least one space.
2839 elif not initial_spaces and line[:2] != '//' and Search(r'[^:]:\s*$',
2840 line):
2841 error(filename, linenum, 'whitespace/labels', 4,
2842 'Labels should always be indented at least one space. '
erg+personal@google.com05189642010-04-30 20:43:03 +00002843 'If this is a member-initializer list in a constructor or '
2844 'the base class list in a class definition, the colon should '
2845 'be on the following line.')
2846
erg@google.com4e00b9a2009-01-12 23:05:11 +00002847
2848 # Check if the line is a header guard.
2849 is_header_guard = False
2850 if file_extension == 'h':
2851 cppvar = GetHeaderGuardCPPVariable(filename)
2852 if (line.startswith('#ifndef %s' % cppvar) or
2853 line.startswith('#define %s' % cppvar) or
2854 line.startswith('#endif // %s' % cppvar)):
2855 is_header_guard = True
2856 # #include lines and header guards can be long, since there's no clean way to
2857 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00002858 #
2859 # URLs can be long too. It's possible to split these, but it makes them
2860 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00002861 #
2862 # The "$Id:...$" comment may also get very long without it being the
2863 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00002864 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00002865 not Match(r'^\s*//.*http(s?)://\S*$', line) and
2866 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002867 line_width = GetLineWidth(line)
2868 if line_width > 100:
2869 error(filename, linenum, 'whitespace/line_length', 4,
2870 'Lines should very rarely be longer than 100 characters')
2871 elif line_width > 80:
2872 error(filename, linenum, 'whitespace/line_length', 2,
2873 'Lines should be <= 80 characters long')
2874
2875 if (cleansed_line.count(';') > 1 and
2876 # for loops are allowed two ;'s (and may run over two lines).
2877 cleansed_line.find('for') == -1 and
2878 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
2879 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
2880 # It's ok to have many commands in a switch case that fits in 1 line
2881 not ((cleansed_line.find('case ') != -1 or
2882 cleansed_line.find('default:') != -1) and
2883 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00002884 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00002885 'More than one command on the same line')
2886
2887 # Some more style checks
2888 CheckBraces(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00002889 CheckEmptyLoopBody(filename, clean_lines, linenum, error)
2890 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
2891 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002892 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00002893 CheckAltTokens(filename, clean_lines, linenum, error)
2894 classinfo = nesting_state.InnermostClass()
2895 if classinfo:
2896 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002897
2898
2899_RE_PATTERN_INCLUDE_NEW_STYLE = re.compile(r'#include +"[^/]+\.h"')
2900_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
2901# Matches the first component of a filename delimited by -s and _s. That is:
2902# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
2903# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
2904# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
2905# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
2906_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
2907
2908
2909def _DropCommonSuffixes(filename):
2910 """Drops common suffixes like _test.cc or -inl.h from filename.
2911
2912 For example:
2913 >>> _DropCommonSuffixes('foo/foo-inl.h')
2914 'foo/foo'
2915 >>> _DropCommonSuffixes('foo/bar/foo.cc')
2916 'foo/bar/foo'
2917 >>> _DropCommonSuffixes('foo/foo_internal.h')
2918 'foo/foo'
2919 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
2920 'foo/foo_unusualinternal'
2921
2922 Args:
2923 filename: The input filename.
2924
2925 Returns:
2926 The filename with the common suffix removed.
2927 """
2928 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
2929 'inl.h', 'impl.h', 'internal.h'):
2930 if (filename.endswith(suffix) and len(filename) > len(suffix) and
2931 filename[-len(suffix) - 1] in ('-', '_')):
2932 return filename[:-len(suffix) - 1]
2933 return os.path.splitext(filename)[0]
2934
2935
2936def _IsTestFilename(filename):
2937 """Determines if the given filename has a suffix that identifies it as a test.
2938
2939 Args:
2940 filename: The input filename.
2941
2942 Returns:
2943 True if 'filename' looks like a test, False otherwise.
2944 """
2945 if (filename.endswith('_test.cc') or
2946 filename.endswith('_unittest.cc') or
2947 filename.endswith('_regtest.cc')):
2948 return True
2949 else:
2950 return False
2951
2952
2953def _ClassifyInclude(fileinfo, include, is_system):
2954 """Figures out what kind of header 'include' is.
2955
2956 Args:
2957 fileinfo: The current file cpplint is running over. A FileInfo instance.
2958 include: The path to a #included file.
2959 is_system: True if the #include used <> rather than "".
2960
2961 Returns:
2962 One of the _XXX_HEADER constants.
2963
2964 For example:
2965 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
2966 _C_SYS_HEADER
2967 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
2968 _CPP_SYS_HEADER
2969 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
2970 _LIKELY_MY_HEADER
2971 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
2972 ... 'bar/foo_other_ext.h', False)
2973 _POSSIBLE_MY_HEADER
2974 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
2975 _OTHER_HEADER
2976 """
2977 # This is a list of all standard c++ header files, except
2978 # those already checked for above.
2979 is_stl_h = include in _STL_HEADERS
2980 is_cpp_h = is_stl_h or include in _CPP_HEADERS
2981
2982 if is_system:
2983 if is_cpp_h:
2984 return _CPP_SYS_HEADER
2985 else:
2986 return _C_SYS_HEADER
2987
2988 # If the target file and the include we're checking share a
2989 # basename when we drop common extensions, and the include
2990 # lives in . , then it's likely to be owned by the target file.
2991 target_dir, target_base = (
2992 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
2993 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
2994 if target_base == include_base and (
2995 include_dir == target_dir or
2996 include_dir == os.path.normpath(target_dir + '/../public')):
2997 return _LIKELY_MY_HEADER
2998
2999 # If the target and include share some initial basename
3000 # component, it's possible the target is implementing the
3001 # include, so it's allowed to be first, but we'll never
3002 # complain if it's not there.
3003 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
3004 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
3005 if (target_first_component and include_first_component and
3006 target_first_component.group(0) ==
3007 include_first_component.group(0)):
3008 return _POSSIBLE_MY_HEADER
3009
3010 return _OTHER_HEADER
3011
3012
erg@google.coma87abb82009-02-24 01:41:01 +00003013
erg@google.come35f7652009-06-19 20:52:09 +00003014def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
3015 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003016
erg@google.come35f7652009-06-19 20:52:09 +00003017 Strings on #include lines are NOT removed from elided line, to make
3018 certain tasks easier. However, to prevent false positives, checks
3019 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003020
3021 Args:
3022 filename: The name of the current file.
3023 clean_lines: A CleansedLines instance containing the file.
3024 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003025 include_state: An _IncludeState instance in which the headers are inserted.
3026 error: The function to call with any errors found.
3027 """
3028 fileinfo = FileInfo(filename)
3029
erg@google.come35f7652009-06-19 20:52:09 +00003030 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003031
3032 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
erg@google.come35f7652009-06-19 20:52:09 +00003033 if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003034 error(filename, linenum, 'build/include', 4,
3035 'Include the directory when naming .h files')
3036
3037 # we shouldn't include a file more than once. actually, there are a
3038 # handful of instances where doing so is okay, but in general it's
3039 # not.
erg@google.come35f7652009-06-19 20:52:09 +00003040 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003041 if match:
3042 include = match.group(2)
3043 is_system = (match.group(1) == '<')
3044 if include in include_state:
3045 error(filename, linenum, 'build/include', 4,
3046 '"%s" already included at %s:%s' %
3047 (include, filename, include_state[include]))
3048 else:
3049 include_state[include] = linenum
3050
3051 # We want to ensure that headers appear in the right order:
3052 # 1) for foo.cc, foo.h (preferred location)
3053 # 2) c system files
3054 # 3) cpp system files
3055 # 4) for foo.cc, foo.h (deprecated location)
3056 # 5) other google headers
3057 #
3058 # We classify each include statement as one of those 5 types
3059 # using a number of techniques. The include_state object keeps
3060 # track of the highest type seen, and complains if we see a
3061 # lower type after that.
3062 error_message = include_state.CheckNextIncludeOrder(
3063 _ClassifyInclude(fileinfo, include, is_system))
3064 if error_message:
3065 error(filename, linenum, 'build/include_order', 4,
3066 '%s. Should be: %s.h, c system, c++ system, other.' %
3067 (error_message, fileinfo.BaseName()))
erg@google.coma868d2d2009-10-09 21:18:45 +00003068 if not include_state.IsInAlphabeticalOrder(include):
3069 error(filename, linenum, 'build/include_alpha', 4,
3070 'Include "%s" not in alphabetical order' % include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003071
erg@google.come35f7652009-06-19 20:52:09 +00003072 # Look for any of the stream classes that are part of standard C++.
3073 match = _RE_PATTERN_INCLUDE.match(line)
3074 if match:
3075 include = match.group(2)
3076 if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
3077 # Many unit tests use cout, so we exempt them.
3078 if not _IsTestFilename(filename):
3079 error(filename, linenum, 'readability/streams', 3,
3080 'Streams are highly discouraged.')
3081
erg@google.com8a95ecc2011-09-08 00:45:54 +00003082
3083def _GetTextInside(text, start_pattern):
3084 """Retrieves all the text between matching open and close parentheses.
3085
3086 Given a string of lines and a regular expression string, retrieve all the text
3087 following the expression and between opening punctuation symbols like
3088 (, [, or {, and the matching close-punctuation symbol. This properly nested
3089 occurrences of the punctuations, so for the text like
3090 printf(a(), b(c()));
3091 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
3092 start_pattern must match string having an open punctuation symbol at the end.
3093
3094 Args:
3095 text: The lines to extract text. Its comments and strings must be elided.
3096 It can be single line and can span multiple lines.
3097 start_pattern: The regexp string indicating where to start extracting
3098 the text.
3099 Returns:
3100 The extracted text.
3101 None if either the opening string or ending punctuation could not be found.
3102 """
3103 # TODO(sugawarayu): Audit cpplint.py to see what places could be profitably
3104 # rewritten to use _GetTextInside (and use inferior regexp matching today).
3105
3106 # Give opening punctuations to get the matching close-punctuations.
3107 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
3108 closing_punctuation = set(matching_punctuation.itervalues())
3109
3110 # Find the position to start extracting text.
3111 match = re.search(start_pattern, text, re.M)
3112 if not match: # start_pattern not found in text.
3113 return None
3114 start_position = match.end(0)
3115
3116 assert start_position > 0, (
3117 'start_pattern must ends with an opening punctuation.')
3118 assert text[start_position - 1] in matching_punctuation, (
3119 'start_pattern must ends with an opening punctuation.')
3120 # Stack of closing punctuations we expect to have in text after position.
3121 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
3122 position = start_position
3123 while punctuation_stack and position < len(text):
3124 if text[position] == punctuation_stack[-1]:
3125 punctuation_stack.pop()
3126 elif text[position] in closing_punctuation:
3127 # A closing punctuation without matching opening punctuations.
3128 return None
3129 elif text[position] in matching_punctuation:
3130 punctuation_stack.append(matching_punctuation[text[position]])
3131 position += 1
3132 if punctuation_stack:
3133 # Opening punctuations left without matching close-punctuations.
3134 return None
3135 # punctuations match.
3136 return text[start_position:position - 1]
3137
3138
erg@google.come35f7652009-06-19 20:52:09 +00003139def CheckLanguage(filename, clean_lines, linenum, file_extension, include_state,
3140 error):
3141 """Checks rules from the 'C++ language rules' section of cppguide.html.
3142
3143 Some of these rules are hard to test (function overloading, using
3144 uint32 inappropriately), but we do the best we can.
3145
3146 Args:
3147 filename: The name of the current file.
3148 clean_lines: A CleansedLines instance containing the file.
3149 linenum: The number of the line to check.
3150 file_extension: The extension (without the dot) of the filename.
3151 include_state: An _IncludeState instance in which the headers are inserted.
3152 error: The function to call with any errors found.
3153 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00003154 # If the line is empty or consists of entirely a comment, no need to
3155 # check it.
3156 line = clean_lines.elided[linenum]
3157 if not line:
3158 return
3159
erg@google.come35f7652009-06-19 20:52:09 +00003160 match = _RE_PATTERN_INCLUDE.search(line)
3161 if match:
3162 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
3163 return
3164
erg@google.com4e00b9a2009-01-12 23:05:11 +00003165 # Create an extended_line, which is the concatenation of the current and
3166 # next lines, for more effective checking of code that may span more than one
3167 # line.
3168 if linenum + 1 < clean_lines.NumLines():
3169 extended_line = line + clean_lines.elided[linenum + 1]
3170 else:
3171 extended_line = line
3172
3173 # Make Windows paths like Unix.
3174 fullname = os.path.abspath(filename).replace('\\', '/')
3175
3176 # TODO(unknown): figure out if they're using default arguments in fn proto.
3177
erg@google.com4e00b9a2009-01-12 23:05:11 +00003178 # Check for non-const references in functions. This is tricky because &
3179 # is also used to take the address of something. We allow <> for templates,
3180 # (ignoring whatever is between the braces) and : for classes.
3181 # These are complicated re's. They try to capture the following:
3182 # paren (for fn-prototype start), typename, &, varname. For the const
3183 # version, we're willing for const to be before typename or after
erg@google.com8a95ecc2011-09-08 00:45:54 +00003184 # Don't check the implementation on same line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003185 fnline = line.split('{', 1)[0]
3186 if (len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) >
3187 len(re.findall(r'\([^()]*\bconst\s+(?:typename\s+)?(?:struct\s+)?'
3188 r'(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) +
3189 len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+\s+const(\s?&|&\s?)[\w]+',
3190 fnline))):
3191
3192 # We allow non-const references in a few standard places, like functions
erg@google.comd350fe52013-01-14 17:51:48 +00003193 # called "swap()" or iostream operators like "<<" or ">>". We also filter
3194 # out for loops, which lint otherwise mistakenly thinks are functions.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003195 if not Search(
erg@google.comd350fe52013-01-14 17:51:48 +00003196 r'(for|swap|Swap|operator[<>][<>])\s*\(\s*'
3197 r'(?:(?:typename\s*)?[\w:]|<.*>)+\s*&',
erg@google.com4e00b9a2009-01-12 23:05:11 +00003198 fnline):
3199 error(filename, linenum, 'runtime/references', 2,
3200 'Is this a non-const reference? '
3201 'If so, make const or use a pointer.')
3202
3203 # Check to see if they're using an conversion function cast.
3204 # I just try to capture the most common basic types, though there are more.
3205 # Parameterless conversion functions, such as bool(), are allowed as they are
3206 # probably a member operator declaration or default constructor.
3207 match = Search(
erg@google.coma868d2d2009-10-09 21:18:45 +00003208 r'(\bnew\s+)?\b' # Grab 'new' operator, if it's there
3209 r'(int|float|double|bool|char|int32|uint32|int64|uint64)\([^)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003210 if match:
3211 # gMock methods are defined using some variant of MOCK_METHODx(name, type)
3212 # where type may be float(), int(string), etc. Without context they are
erg@google.comd7d27472011-09-07 17:36:35 +00003213 # virtually indistinguishable from int(x) casts. Likewise, gMock's
3214 # MockCallback takes a template parameter of the form return_type(arg_type),
3215 # which looks much like the cast we're trying to detect.
erg@google.coma868d2d2009-10-09 21:18:45 +00003216 if (match.group(1) is None and # If new operator, then this isn't a cast
erg@google.comd7d27472011-09-07 17:36:35 +00003217 not (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
3218 Match(r'^\s*MockCallback<.*>', line))):
erg@google.comd350fe52013-01-14 17:51:48 +00003219 # Try a bit harder to catch gmock lines: the only place where
3220 # something looks like an old-style cast is where we declare the
3221 # return type of the mocked method, and the only time when we
3222 # are missing context is if MOCK_METHOD was split across
3223 # multiple lines (for example http://go/hrfhr ), so we only need
3224 # to check the previous line for MOCK_METHOD.
3225 if (linenum == 0 or
3226 not Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(\S+,\s*$',
3227 clean_lines.elided[linenum - 1])):
3228 error(filename, linenum, 'readability/casting', 4,
3229 'Using deprecated casting style. '
3230 'Use static_cast<%s>(...) instead' %
3231 match.group(2))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003232
3233 CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3234 'static_cast',
erg@google.com8a95ecc2011-09-08 00:45:54 +00003235 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
3236
3237 # This doesn't catch all cases. Consider (const char * const)"hello".
3238 #
3239 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
3240 # compile).
3241 if CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3242 'const_cast', r'\((char\s?\*+\s?)\)\s*"', error):
3243 pass
3244 else:
3245 # Check pointer casts for other than string constants
3246 CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3247 'reinterpret_cast', r'\((\w+\s?\*+\s?)\)', error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003248
3249 # In addition, we look for people taking the address of a cast. This
3250 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
3251 # point where you think.
3252 if Search(
3253 r'(&\([^)]+\)[\w(])|(&(static|dynamic|reinterpret)_cast\b)', line):
3254 error(filename, linenum, 'runtime/casting', 4,
3255 ('Are you taking an address of a cast? '
3256 'This is dangerous: could be a temp var. '
3257 'Take the address before doing the cast, rather than after'))
3258
3259 # Check for people declaring static/global STL strings at the top level.
3260 # This is dangerous because the C++ language does not guarantee that
3261 # globals with constructors are initialized before the first access.
3262 match = Match(
3263 r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
3264 line)
3265 # Make sure it's not a function.
3266 # Function template specialization looks like: "string foo<Type>(...".
3267 # Class template definitions look like: "string Foo<Type>::Method(...".
3268 if match and not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)?\s*\(([^"]|$)',
3269 match.group(3)):
3270 error(filename, linenum, 'runtime/string', 4,
3271 'For a static/global string constant, use a C style string instead: '
3272 '"%schar %s[]".' %
3273 (match.group(1), match.group(2)))
3274
3275 # Check that we're not using RTTI outside of testing code.
3276 if Search(r'\bdynamic_cast<', line) and not _IsTestFilename(filename):
3277 error(filename, linenum, 'runtime/rtti', 5,
3278 'Do not use dynamic_cast<>. If you need to cast within a class '
3279 "hierarchy, use static_cast<> to upcast. Google doesn't support "
3280 'RTTI.')
3281
3282 if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
3283 error(filename, linenum, 'runtime/init', 4,
3284 'You seem to be initializing a member variable with itself.')
3285
3286 if file_extension == 'h':
3287 # TODO(unknown): check that 1-arg constructors are explicit.
3288 # How to tell it's a constructor?
3289 # (handled in CheckForNonStandardConstructs for now)
3290 # TODO(unknown): check that classes have DISALLOW_EVIL_CONSTRUCTORS
3291 # (level 1 error)
3292 pass
3293
3294 # Check if people are using the verboten C basic types. The only exception
3295 # we regularly allow is "unsigned short port" for port.
3296 if Search(r'\bshort port\b', line):
3297 if not Search(r'\bunsigned short port\b', line):
3298 error(filename, linenum, 'runtime/int', 4,
3299 'Use "unsigned short" for ports, not "short"')
3300 else:
3301 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
3302 if match:
3303 error(filename, linenum, 'runtime/int', 4,
3304 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
3305
3306 # When snprintf is used, the second argument shouldn't be a literal.
3307 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
erg+personal@google.com05189642010-04-30 20:43:03 +00003308 if match and match.group(2) != '0':
3309 # If 2nd arg is zero, snprintf is used to calculate size.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003310 error(filename, linenum, 'runtime/printf', 3,
3311 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
3312 'to snprintf.' % (match.group(1), match.group(2)))
3313
3314 # Check if some verboten C functions are being used.
3315 if Search(r'\bsprintf\b', line):
3316 error(filename, linenum, 'runtime/printf', 5,
3317 'Never use sprintf. Use snprintf instead.')
3318 match = Search(r'\b(strcpy|strcat)\b', line)
3319 if match:
3320 error(filename, linenum, 'runtime/printf', 4,
3321 'Almost always, snprintf is better than %s' % match.group(1))
3322
3323 if Search(r'\bsscanf\b', line):
3324 error(filename, linenum, 'runtime/printf', 1,
3325 'sscanf can be ok, but is slow and can overflow buffers.')
3326
erg@google.coma868d2d2009-10-09 21:18:45 +00003327 # Check if some verboten operator overloading is going on
3328 # TODO(unknown): catch out-of-line unary operator&:
3329 # class X {};
3330 # int operator&(const X& x) { return 42; } // unary operator&
3331 # The trick is it's hard to tell apart from binary operator&:
3332 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
3333 if Search(r'\boperator\s*&\s*\(\s*\)', line):
3334 error(filename, linenum, 'runtime/operator', 4,
3335 'Unary operator& is dangerous. Do not use it.')
3336
erg@google.com4e00b9a2009-01-12 23:05:11 +00003337 # Check for suspicious usage of "if" like
3338 # } if (a == b) {
3339 if Search(r'\}\s*if\s*\(', line):
3340 error(filename, linenum, 'readability/braces', 4,
3341 'Did you mean "else if"? If not, start a new line for "if".')
3342
3343 # Check for potential format string bugs like printf(foo).
3344 # We constrain the pattern not to pick things like DocidForPrintf(foo).
3345 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
erg@google.com8a95ecc2011-09-08 00:45:54 +00003346 # TODO(sugawarayu): Catch the following case. Need to change the calling
3347 # convention of the whole function to process multiple line to handle it.
3348 # printf(
3349 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
3350 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
3351 if printf_args:
3352 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00003353 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00003354 function_name = re.search(r'\b((?:string)?printf)\s*\(',
3355 line, re.I).group(1)
3356 error(filename, linenum, 'runtime/printf', 4,
3357 'Potential format string bug. Do %s("%%s", %s) instead.'
3358 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003359
3360 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
3361 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
3362 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
3363 error(filename, linenum, 'runtime/memset', 4,
3364 'Did you mean "memset(%s, 0, %s)"?'
3365 % (match.group(1), match.group(2)))
3366
3367 if Search(r'\busing namespace\b', line):
3368 error(filename, linenum, 'build/namespaces', 5,
3369 'Do not use namespace using-directives. '
3370 'Use using-declarations instead.')
3371
3372 # Detect variable-length arrays.
3373 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
3374 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
3375 match.group(3).find(']') == -1):
3376 # Split the size using space and arithmetic operators as delimiters.
3377 # If any of the resulting tokens are not compile time constants then
3378 # report the error.
3379 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
3380 is_const = True
3381 skip_next = False
3382 for tok in tokens:
3383 if skip_next:
3384 skip_next = False
3385 continue
3386
3387 if Search(r'sizeof\(.+\)', tok): continue
3388 if Search(r'arraysize\(\w+\)', tok): continue
3389
3390 tok = tok.lstrip('(')
3391 tok = tok.rstrip(')')
3392 if not tok: continue
3393 if Match(r'\d+', tok): continue
3394 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
3395 if Match(r'k[A-Z0-9]\w*', tok): continue
3396 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
3397 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
3398 # A catch all for tricky sizeof cases, including 'sizeof expression',
3399 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00003400 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003401 if tok.startswith('sizeof'):
3402 skip_next = True
3403 continue
3404 is_const = False
3405 break
3406 if not is_const:
3407 error(filename, linenum, 'runtime/arrays', 1,
3408 'Do not use variable-length arrays. Use an appropriately named '
3409 "('k' followed by CamelCase) compile-time constant for the size.")
3410
3411 # If DISALLOW_EVIL_CONSTRUCTORS, DISALLOW_COPY_AND_ASSIGN, or
3412 # DISALLOW_IMPLICIT_CONSTRUCTORS is present, then it should be the last thing
3413 # in the class declaration.
3414 match = Match(
3415 (r'\s*'
3416 r'(DISALLOW_(EVIL_CONSTRUCTORS|COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))'
3417 r'\(.*\);$'),
3418 line)
3419 if match and linenum + 1 < clean_lines.NumLines():
3420 next_line = clean_lines.elided[linenum + 1]
erg@google.com8a95ecc2011-09-08 00:45:54 +00003421 # We allow some, but not all, declarations of variables to be present
3422 # in the statement that defines the class. The [\w\*,\s]* fragment of
3423 # the regular expression below allows users to declare instances of
3424 # the class or pointers to instances, but not less common types such
3425 # as function pointers or arrays. It's a tradeoff between allowing
3426 # reasonable code and avoiding trying to parse more C++ using regexps.
3427 if not Search(r'^\s*}[\w\*,\s]*;', next_line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003428 error(filename, linenum, 'readability/constructors', 3,
3429 match.group(1) + ' should be the last thing in the class')
3430
3431 # Check for use of unnamed namespaces in header files. Registration
3432 # macros are typically OK, so we allow use of "namespace {" on lines
3433 # that end with backslashes.
3434 if (file_extension == 'h'
3435 and Search(r'\bnamespace\s*{', line)
3436 and line[-1] != '\\'):
3437 error(filename, linenum, 'build/namespaces', 4,
3438 'Do not use unnamed namespaces in header files. See '
3439 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
3440 ' for more information.')
3441
3442
3443def CheckCStyleCast(filename, linenum, line, raw_line, cast_type, pattern,
3444 error):
3445 """Checks for a C-style cast by looking for the pattern.
3446
3447 This also handles sizeof(type) warnings, due to similarity of content.
3448
3449 Args:
3450 filename: The name of the current file.
3451 linenum: The number of the line to check.
3452 line: The line of code to check.
3453 raw_line: The raw line of code to check, with comments.
3454 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00003455 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003456 pattern: The regular expression used to find C-style casts.
3457 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003458
3459 Returns:
3460 True if an error was emitted.
3461 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003462 """
3463 match = Search(pattern, line)
3464 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00003465 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00003466
3467 # e.g., sizeof(int)
3468 sizeof_match = Match(r'.*sizeof\s*$', line[0:match.start(1) - 1])
3469 if sizeof_match:
3470 error(filename, linenum, 'runtime/sizeof', 1,
3471 'Using sizeof(type). Use sizeof(varname) instead if possible')
erg@google.com8a95ecc2011-09-08 00:45:54 +00003472 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +00003473
erg@google.comd350fe52013-01-14 17:51:48 +00003474 # operator++(int) and operator--(int)
3475 if (line[0:match.start(1) - 1].endswith(' operator++') or
3476 line[0:match.start(1) - 1].endswith(' operator--')):
3477 return False
3478
erg@google.com4e00b9a2009-01-12 23:05:11 +00003479 remainder = line[match.end(0):]
3480
3481 # The close paren is for function pointers as arguments to a function.
3482 # eg, void foo(void (*bar)(int));
3483 # The semicolon check is a more basic function check; also possibly a
3484 # function pointer typedef.
3485 # eg, void foo(int); or void foo(int) const;
3486 # The equals check is for function pointer assignment.
3487 # eg, void *(*foo)(int) = ...
erg@google.comd7d27472011-09-07 17:36:35 +00003488 # The > is for MockCallback<...> ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00003489 #
3490 # Right now, this will only catch cases where there's a single argument, and
3491 # it's unnamed. It should probably be expanded to check for multiple
3492 # arguments with some unnamed.
erg@google.comd7d27472011-09-07 17:36:35 +00003493 function_match = Match(r'\s*(\)|=|(const)?\s*(;|\{|throw\(\)|>))', remainder)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003494 if function_match:
3495 if (not function_match.group(3) or
3496 function_match.group(3) == ';' or
erg@google.comd7d27472011-09-07 17:36:35 +00003497 ('MockCallback<' not in raw_line and
3498 '/*' not in raw_line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003499 error(filename, linenum, 'readability/function', 3,
3500 'All parameters should be named in a function')
erg@google.com8a95ecc2011-09-08 00:45:54 +00003501 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +00003502
3503 # At this point, all that should be left is actual casts.
3504 error(filename, linenum, 'readability/casting', 4,
3505 'Using C-style cast. Use %s<%s>(...) instead' %
3506 (cast_type, match.group(1)))
3507
erg@google.com8a95ecc2011-09-08 00:45:54 +00003508 return True
3509
erg@google.com4e00b9a2009-01-12 23:05:11 +00003510
3511_HEADERS_CONTAINING_TEMPLATES = (
3512 ('<deque>', ('deque',)),
3513 ('<functional>', ('unary_function', 'binary_function',
3514 'plus', 'minus', 'multiplies', 'divides', 'modulus',
3515 'negate',
3516 'equal_to', 'not_equal_to', 'greater', 'less',
3517 'greater_equal', 'less_equal',
3518 'logical_and', 'logical_or', 'logical_not',
3519 'unary_negate', 'not1', 'binary_negate', 'not2',
3520 'bind1st', 'bind2nd',
3521 'pointer_to_unary_function',
3522 'pointer_to_binary_function',
3523 'ptr_fun',
3524 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
3525 'mem_fun_ref_t',
3526 'const_mem_fun_t', 'const_mem_fun1_t',
3527 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
3528 'mem_fun_ref',
3529 )),
3530 ('<limits>', ('numeric_limits',)),
3531 ('<list>', ('list',)),
3532 ('<map>', ('map', 'multimap',)),
3533 ('<memory>', ('allocator',)),
3534 ('<queue>', ('queue', 'priority_queue',)),
3535 ('<set>', ('set', 'multiset',)),
3536 ('<stack>', ('stack',)),
3537 ('<string>', ('char_traits', 'basic_string',)),
3538 ('<utility>', ('pair',)),
3539 ('<vector>', ('vector',)),
3540
3541 # gcc extensions.
3542 # Note: std::hash is their hash, ::hash is our hash
3543 ('<hash_map>', ('hash_map', 'hash_multimap',)),
3544 ('<hash_set>', ('hash_set', 'hash_multiset',)),
3545 ('<slist>', ('slist',)),
3546 )
3547
erg@google.com4e00b9a2009-01-12 23:05:11 +00003548_RE_PATTERN_STRING = re.compile(r'\bstring\b')
3549
3550_re_pattern_algorithm_header = []
erg@google.coma87abb82009-02-24 01:41:01 +00003551for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
3552 'transform'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003553 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
3554 # type::max().
3555 _re_pattern_algorithm_header.append(
3556 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
3557 _template,
3558 '<algorithm>'))
3559
3560_re_pattern_templates = []
3561for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
3562 for _template in _templates:
3563 _re_pattern_templates.append(
3564 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
3565 _template + '<>',
3566 _header))
3567
3568
erg@google.come35f7652009-06-19 20:52:09 +00003569def FilesBelongToSameModule(filename_cc, filename_h):
3570 """Check if these two filenames belong to the same module.
3571
3572 The concept of a 'module' here is a as follows:
3573 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
3574 same 'module' if they are in the same directory.
3575 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
3576 to belong to the same module here.
3577
3578 If the filename_cc contains a longer path than the filename_h, for example,
3579 '/absolute/path/to/base/sysinfo.cc', and this file would include
3580 'base/sysinfo.h', this function also produces the prefix needed to open the
3581 header. This is used by the caller of this function to more robustly open the
3582 header file. We don't have access to the real include paths in this context,
3583 so we need this guesswork here.
3584
3585 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
3586 according to this implementation. Because of this, this function gives
3587 some false positives. This should be sufficiently rare in practice.
3588
3589 Args:
3590 filename_cc: is the path for the .cc file
3591 filename_h: is the path for the header path
3592
3593 Returns:
3594 Tuple with a bool and a string:
3595 bool: True if filename_cc and filename_h belong to the same module.
3596 string: the additional prefix needed to open the header file.
3597 """
3598
3599 if not filename_cc.endswith('.cc'):
3600 return (False, '')
3601 filename_cc = filename_cc[:-len('.cc')]
3602 if filename_cc.endswith('_unittest'):
3603 filename_cc = filename_cc[:-len('_unittest')]
3604 elif filename_cc.endswith('_test'):
3605 filename_cc = filename_cc[:-len('_test')]
3606 filename_cc = filename_cc.replace('/public/', '/')
3607 filename_cc = filename_cc.replace('/internal/', '/')
3608
3609 if not filename_h.endswith('.h'):
3610 return (False, '')
3611 filename_h = filename_h[:-len('.h')]
3612 if filename_h.endswith('-inl'):
3613 filename_h = filename_h[:-len('-inl')]
3614 filename_h = filename_h.replace('/public/', '/')
3615 filename_h = filename_h.replace('/internal/', '/')
3616
3617 files_belong_to_same_module = filename_cc.endswith(filename_h)
3618 common_path = ''
3619 if files_belong_to_same_module:
3620 common_path = filename_cc[:-len(filename_h)]
3621 return files_belong_to_same_module, common_path
3622
3623
3624def UpdateIncludeState(filename, include_state, io=codecs):
3625 """Fill up the include_state with new includes found from the file.
3626
3627 Args:
3628 filename: the name of the header to read.
3629 include_state: an _IncludeState instance in which the headers are inserted.
3630 io: The io factory to use to read the file. Provided for testability.
3631
3632 Returns:
3633 True if a header was succesfully added. False otherwise.
3634 """
3635 headerfile = None
3636 try:
3637 headerfile = io.open(filename, 'r', 'utf8', 'replace')
3638 except IOError:
3639 return False
3640 linenum = 0
3641 for line in headerfile:
3642 linenum += 1
3643 clean_line = CleanseComments(line)
3644 match = _RE_PATTERN_INCLUDE.search(clean_line)
3645 if match:
3646 include = match.group(2)
3647 # The value formatting is cute, but not really used right now.
3648 # What matters here is that the key is in include_state.
3649 include_state.setdefault(include, '%s:%d' % (filename, linenum))
3650 return True
3651
3652
3653def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
3654 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003655 """Reports for missing stl includes.
3656
3657 This function will output warnings to make sure you are including the headers
3658 necessary for the stl containers and functions that you use. We only give one
3659 reason to include a header. For example, if you use both equal_to<> and
3660 less<> in a .h file, only one (the latter in the file) of these will be
3661 reported as a reason to include the <functional>.
3662
erg@google.com4e00b9a2009-01-12 23:05:11 +00003663 Args:
3664 filename: The name of the current file.
3665 clean_lines: A CleansedLines instance containing the file.
3666 include_state: An _IncludeState instance.
3667 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00003668 io: The IO factory to use to read the header file. Provided for unittest
3669 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003670 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00003671 required = {} # A map of header name to linenumber and the template entity.
3672 # Example of required: { '<functional>': (1219, 'less<>') }
3673
3674 for linenum in xrange(clean_lines.NumLines()):
3675 line = clean_lines.elided[linenum]
3676 if not line or line[0] == '#':
3677 continue
3678
3679 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003680 matched = _RE_PATTERN_STRING.search(line)
3681 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00003682 # Don't warn about strings in non-STL namespaces:
3683 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00003684 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00003685 if prefix.endswith('std::') or not prefix.endswith('::'):
3686 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003687
3688 for pattern, template, header in _re_pattern_algorithm_header:
3689 if pattern.search(line):
3690 required[header] = (linenum, template)
3691
3692 # The following function is just a speed up, no semantics are changed.
3693 if not '<' in line: # Reduces the cpu time usage by skipping lines.
3694 continue
3695
3696 for pattern, template, header in _re_pattern_templates:
3697 if pattern.search(line):
3698 required[header] = (linenum, template)
3699
erg@google.come35f7652009-06-19 20:52:09 +00003700 # The policy is that if you #include something in foo.h you don't need to
3701 # include it again in foo.cc. Here, we will look at possible includes.
3702 # Let's copy the include_state so it is only messed up within this function.
3703 include_state = include_state.copy()
3704
3705 # Did we find the header for this file (if any) and succesfully load it?
3706 header_found = False
3707
3708 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00003709 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00003710
3711 # For Emacs's flymake.
3712 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
3713 # by flymake and that file name might end with '_flymake.cc'. In that case,
3714 # restore original file name here so that the corresponding header file can be
3715 # found.
3716 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
3717 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00003718 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00003719
3720 # include_state is modified during iteration, so we iterate over a copy of
3721 # the keys.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003722 header_keys = include_state.keys()
3723 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00003724 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
3725 fullpath = common_path + header
3726 if same_module and UpdateIncludeState(fullpath, include_state, io):
3727 header_found = True
3728
3729 # If we can't find the header file for a .cc, assume it's because we don't
3730 # know where to look. In that case we'll give up as we're not sure they
3731 # didn't include it in the .h file.
3732 # TODO(unknown): Do a better job of finding .h files so we are confident that
3733 # not having the .h file means there isn't one.
3734 if filename.endswith('.cc') and not header_found:
3735 return
3736
erg@google.com4e00b9a2009-01-12 23:05:11 +00003737 # All the lines have been processed, report the errors found.
3738 for required_header_unstripped in required:
3739 template = required[required_header_unstripped][1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003740 if required_header_unstripped.strip('<>"') not in include_state:
3741 error(filename, required[required_header_unstripped][0],
3742 'build/include_what_you_use', 4,
3743 'Add #include ' + required_header_unstripped + ' for ' + template)
3744
3745
erg@google.com8a95ecc2011-09-08 00:45:54 +00003746_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
3747
3748
3749def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
3750 """Check that make_pair's template arguments are deduced.
3751
3752 G++ 4.6 in C++0x mode fails badly if make_pair's template arguments are
3753 specified explicitly, and such use isn't intended in any case.
3754
3755 Args:
3756 filename: The name of the current file.
3757 clean_lines: A CleansedLines instance containing the file.
3758 linenum: The number of the line to check.
3759 error: The function to call with any errors found.
3760 """
3761 raw = clean_lines.raw_lines
3762 line = raw[linenum]
3763 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
3764 if match:
3765 error(filename, linenum, 'build/explicit_make_pair',
3766 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00003767 'For C++11-compatibility, omit template arguments from make_pair'
3768 ' OR use pair directly OR if appropriate, construct a pair directly')
erg@google.com8a95ecc2011-09-08 00:45:54 +00003769
3770
erg@google.comd350fe52013-01-14 17:51:48 +00003771def ProcessLine(filename, file_extension, clean_lines, line,
3772 include_state, function_state, nesting_state, error,
3773 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003774 """Processes a single line in the file.
3775
3776 Args:
3777 filename: Filename of the file that is being processed.
3778 file_extension: The extension (dot not included) of the file.
3779 clean_lines: An array of strings, each representing a line of the file,
3780 with comments stripped.
3781 line: Number of line being processed.
3782 include_state: An _IncludeState instance in which the headers are inserted.
3783 function_state: A _FunctionState instance which counts function lines, etc.
erg@google.comd350fe52013-01-14 17:51:48 +00003784 nesting_state: A _NestingState instance which maintains information about
3785 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003786 error: A callable to which errors are reported, which takes 4 arguments:
3787 filename, line number, error level, and message
erg@google.comefeacdf2011-09-07 21:12:16 +00003788 extra_check_functions: An array of additional check functions that will be
3789 run on each source line. Each function takes 4
3790 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00003791 """
3792 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00003793 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00003794 nesting_state.Update(filename, clean_lines, line, error)
3795 if nesting_state.stack and nesting_state.stack[-1].inline_asm != _NO_ASM:
3796 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00003797 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003798 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00003799 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003800 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
3801 error)
3802 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00003803 nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003804 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00003805 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00003806 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
erg@google.comefeacdf2011-09-07 21:12:16 +00003807 for check_fn in extra_check_functions:
3808 check_fn(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003809
erg@google.comefeacdf2011-09-07 21:12:16 +00003810def ProcessFileData(filename, file_extension, lines, error,
3811 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003812 """Performs lint checks and reports any errors to the given error function.
3813
3814 Args:
3815 filename: Filename of the file that is being processed.
3816 file_extension: The extension (dot not included) of the file.
3817 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00003818 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003819 error: A callable to which errors are reported, which takes 4 arguments:
erg@google.comefeacdf2011-09-07 21:12:16 +00003820 filename, line number, error level, and message
3821 extra_check_functions: An array of additional check functions that will be
3822 run on each source line. Each function takes 4
3823 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00003824 """
3825 lines = (['// marker so line numbers and indices both start at 1'] + lines +
3826 ['// marker so line numbers end in a known way'])
3827
3828 include_state = _IncludeState()
3829 function_state = _FunctionState()
erg@google.comd350fe52013-01-14 17:51:48 +00003830 nesting_state = _NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00003831
erg+personal@google.com05189642010-04-30 20:43:03 +00003832 ResetNolintSuppressions()
3833
erg@google.com4e00b9a2009-01-12 23:05:11 +00003834 CheckForCopyright(filename, lines, error)
3835
3836 if file_extension == 'h':
3837 CheckForHeaderGuard(filename, lines, error)
3838
3839 RemoveMultiLineComments(filename, lines, error)
3840 clean_lines = CleansedLines(lines)
3841 for line in xrange(clean_lines.NumLines()):
3842 ProcessLine(filename, file_extension, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00003843 include_state, function_state, nesting_state, error,
erg@google.comefeacdf2011-09-07 21:12:16 +00003844 extra_check_functions)
erg@google.comd350fe52013-01-14 17:51:48 +00003845 nesting_state.CheckClassFinished(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003846
3847 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
3848
3849 # We check here rather than inside ProcessLine so that we see raw
3850 # lines rather than "cleaned" lines.
3851 CheckForUnicodeReplacementCharacters(filename, lines, error)
3852
3853 CheckForNewlineAtEOF(filename, lines, error)
3854
erg@google.comefeacdf2011-09-07 21:12:16 +00003855def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003856 """Does google-lint on a single file.
3857
3858 Args:
3859 filename: The name of the file to parse.
3860
3861 vlevel: The level of errors to report. Every error of confidence
3862 >= verbose_level will be reported. 0 is a good default.
erg@google.comefeacdf2011-09-07 21:12:16 +00003863
3864 extra_check_functions: An array of additional check functions that will be
3865 run on each source line. Each function takes 4
3866 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00003867 """
3868
3869 _SetVerboseLevel(vlevel)
3870
3871 try:
3872 # Support the UNIX convention of using "-" for stdin. Note that
3873 # we are not opening the file with universal newline support
3874 # (which codecs doesn't support anyway), so the resulting lines do
3875 # contain trailing '\r' characters if we are reading a file that
3876 # has CRLF endings.
3877 # If after the split a trailing '\r' is present, it is removed
3878 # below. If it is not expected to be present (i.e. os.linesep !=
3879 # '\r\n' as in Windows), a warning is issued below if this file
3880 # is processed.
3881
3882 if filename == '-':
3883 lines = codecs.StreamReaderWriter(sys.stdin,
3884 codecs.getreader('utf8'),
3885 codecs.getwriter('utf8'),
3886 'replace').read().split('\n')
3887 else:
3888 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
3889
3890 carriage_return_found = False
3891 # Remove trailing '\r'.
3892 for linenum in range(len(lines)):
3893 if lines[linenum].endswith('\r'):
3894 lines[linenum] = lines[linenum].rstrip('\r')
3895 carriage_return_found = True
3896
3897 except IOError:
3898 sys.stderr.write(
3899 "Skipping input '%s': Can't open for reading\n" % filename)
3900 return
3901
3902 # Note, if no dot is found, this will give the entire filename as the ext.
3903 file_extension = filename[filename.rfind('.') + 1:]
3904
3905 # When reading from stdin, the extension is unknown, so no cpplint tests
3906 # should rely on the extension.
3907 if (filename != '-' and file_extension != 'cc' and file_extension != 'h'
3908 and file_extension != 'cpp'):
3909 sys.stderr.write('Ignoring %s; not a .cc or .h file\n' % filename)
3910 else:
erg@google.comefeacdf2011-09-07 21:12:16 +00003911 ProcessFileData(filename, file_extension, lines, Error,
3912 extra_check_functions)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003913 if carriage_return_found and os.linesep != '\r\n':
erg@google.com8a95ecc2011-09-08 00:45:54 +00003914 # Use 0 for linenum since outputting only one error for potentially
erg@google.com4e00b9a2009-01-12 23:05:11 +00003915 # several lines.
3916 Error(filename, 0, 'whitespace/newline', 1,
3917 'One or more unexpected \\r (^M) found;'
3918 'better to use only a \\n')
3919
3920 sys.stderr.write('Done processing %s\n' % filename)
3921
3922
3923def PrintUsage(message):
3924 """Prints a brief usage string and exits, optionally with an error message.
3925
3926 Args:
3927 message: The optional error message.
3928 """
3929 sys.stderr.write(_USAGE)
3930 if message:
3931 sys.exit('\nFATAL ERROR: ' + message)
3932 else:
3933 sys.exit(1)
3934
3935
3936def PrintCategories():
3937 """Prints a list of all the error-categories used by error messages.
3938
3939 These are the categories used to filter messages via --filter.
3940 """
erg+personal@google.com05189642010-04-30 20:43:03 +00003941 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003942 sys.exit(0)
3943
3944
3945def ParseArguments(args):
3946 """Parses the command line arguments.
3947
3948 This may set the output format and verbosity level as side-effects.
3949
3950 Args:
3951 args: The command line arguments:
3952
3953 Returns:
3954 The list of filenames to lint.
3955 """
3956 try:
3957 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00003958 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00003959 'filter=',
3960 'root='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00003961 except getopt.GetoptError:
3962 PrintUsage('Invalid arguments.')
3963
3964 verbosity = _VerboseLevel()
3965 output_format = _OutputFormat()
3966 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00003967 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00003968
3969 for (opt, val) in opts:
3970 if opt == '--help':
3971 PrintUsage(None)
3972 elif opt == '--output':
3973 if not val in ('emacs', 'vs7'):
3974 PrintUsage('The only allowed output formats are emacs and vs7.')
3975 output_format = val
3976 elif opt == '--verbose':
3977 verbosity = int(val)
3978 elif opt == '--filter':
3979 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00003980 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003981 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00003982 elif opt == '--counting':
3983 if val not in ('total', 'toplevel', 'detailed'):
3984 PrintUsage('Valid counting options are total, toplevel, and detailed')
3985 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00003986 elif opt == '--root':
3987 global _root
3988 _root = val
erg@google.com4e00b9a2009-01-12 23:05:11 +00003989
3990 if not filenames:
3991 PrintUsage('No files were specified.')
3992
3993 _SetOutputFormat(output_format)
3994 _SetVerboseLevel(verbosity)
3995 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00003996 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003997
3998 return filenames
3999
4000
4001def main():
4002 filenames = ParseArguments(sys.argv[1:])
4003
4004 # Change stderr to write with replacement characters so we don't die
4005 # if we try to print something containing non-ASCII characters.
4006 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
4007 codecs.getreader('utf8'),
4008 codecs.getwriter('utf8'),
4009 'replace')
4010
erg@google.coma868d2d2009-10-09 21:18:45 +00004011 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004012 for filename in filenames:
4013 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00004014 _cpplint_state.PrintErrorCounts()
4015
erg@google.com4e00b9a2009-01-12 23:05:11 +00004016 sys.exit(_cpplint_state.error_count > 0)
4017
4018
4019if __name__ == '__main__':
4020 main()