blob: 0445454f60f8fd74673882f8574fa0ecd8fa9076 [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.com4e00b9a2009-01-12 23:05:11 +0000139"""
140
141# We categorize each error message we print. Here are the categories.
142# We want an explicit list so we can list them all in cpplint --filter=.
143# If you add a new error message with a new category, add it to the list
144# here! cpplint_unittest.py should tell you if you forget to do this.
erg@google.coma87abb82009-02-24 01:41:01 +0000145# \ used for clearer layout -- pylint: disable-msg=C6013
erg+personal@google.com05189642010-04-30 20:43:03 +0000146_ERROR_CATEGORIES = [
147 'build/class',
148 'build/deprecated',
149 'build/endif_comment',
erg@google.com8a95ecc2011-09-08 00:45:54 +0000150 'build/explicit_make_pair',
erg+personal@google.com05189642010-04-30 20:43:03 +0000151 'build/forward_decl',
152 'build/header_guard',
153 'build/include',
154 'build/include_alpha',
155 'build/include_order',
156 'build/include_what_you_use',
157 'build/namespaces',
158 'build/printf_format',
159 'build/storage_class',
160 'legal/copyright',
erg@google.comd350fe52013-01-14 17:51:48 +0000161 'readability/alt_tokens',
erg+personal@google.com05189642010-04-30 20:43:03 +0000162 'readability/braces',
163 'readability/casting',
164 'readability/check',
165 'readability/constructors',
166 'readability/fn_size',
167 'readability/function',
168 'readability/multiline_comment',
169 'readability/multiline_string',
erg@google.comd350fe52013-01-14 17:51:48 +0000170 'readability/namespace',
erg+personal@google.com05189642010-04-30 20:43:03 +0000171 'readability/nolint',
172 'readability/streams',
173 'readability/todo',
174 'readability/utf8',
175 'runtime/arrays',
176 'runtime/casting',
177 'runtime/explicit',
178 'runtime/int',
179 'runtime/init',
180 'runtime/invalid_increment',
181 'runtime/member_string_references',
182 'runtime/memset',
183 'runtime/operator',
184 'runtime/printf',
185 'runtime/printf_format',
186 'runtime/references',
187 'runtime/rtti',
188 'runtime/sizeof',
189 'runtime/string',
190 'runtime/threadsafe_fn',
erg+personal@google.com05189642010-04-30 20:43:03 +0000191 'whitespace/blank_line',
192 'whitespace/braces',
193 'whitespace/comma',
194 'whitespace/comments',
erg@google.comd350fe52013-01-14 17:51:48 +0000195 'whitespace/empty_loop_body',
erg+personal@google.com05189642010-04-30 20:43:03 +0000196 'whitespace/end_of_line',
197 'whitespace/ending_newline',
erg@google.comd350fe52013-01-14 17:51:48 +0000198 'whitespace/forcolon',
erg+personal@google.com05189642010-04-30 20:43:03 +0000199 'whitespace/indent',
200 'whitespace/labels',
201 'whitespace/line_length',
202 'whitespace/newline',
203 'whitespace/operators',
204 'whitespace/parens',
205 'whitespace/semicolon',
206 'whitespace/tab',
207 'whitespace/todo'
208 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000209
erg@google.come35f7652009-06-19 20:52:09 +0000210# The default state of the category filter. This is overrided by the --filter=
211# flag. By default all errors are on, so only add here categories that should be
212# off by default (i.e., categories that must be enabled by the --filter= flags).
213# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000214_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000215
erg@google.com4e00b9a2009-01-12 23:05:11 +0000216# We used to check for high-bit characters, but after much discussion we
217# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000218# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000219
220# Headers that we consider STL headers.
221_STL_HEADERS = frozenset([
222 'algobase.h', 'algorithm', 'alloc.h', 'bitset', 'deque', 'exception',
223 'function.h', 'functional', 'hash_map', 'hash_map.h', 'hash_set',
erg+personal@google.com05189642010-04-30 20:43:03 +0000224 'hash_set.h', 'iterator', 'list', 'list.h', 'map', 'memory', 'new',
225 'pair.h', 'pthread_alloc', 'queue', 'set', 'set.h', 'sstream', 'stack',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000226 'stl_alloc.h', 'stl_relops.h', 'type_traits.h',
227 'utility', 'vector', 'vector.h',
228 ])
229
230
231# Non-STL C++ system headers.
232_CPP_HEADERS = frozenset([
233 'algo.h', 'builtinbuf.h', 'bvector.h', 'cassert', 'cctype',
234 'cerrno', 'cfloat', 'ciso646', 'climits', 'clocale', 'cmath',
235 'complex', 'complex.h', 'csetjmp', 'csignal', 'cstdarg', 'cstddef',
236 'cstdio', 'cstdlib', 'cstring', 'ctime', 'cwchar', 'cwctype',
237 'defalloc.h', 'deque.h', 'editbuf.h', 'exception', 'fstream',
238 'fstream.h', 'hashtable.h', 'heap.h', 'indstream.h', 'iomanip',
erg@google.comd7d27472011-09-07 17:36:35 +0000239 'iomanip.h', 'ios', 'iosfwd', 'iostream', 'iostream.h', 'istream',
240 'istream.h', 'iterator.h', 'limits', 'map.h', 'multimap.h', 'multiset.h',
241 'numeric', 'ostream', 'ostream.h', 'parsestream.h', 'pfstream.h',
242 'PlotFile.h', 'procbuf.h', 'pthread_alloc.h', 'rope', 'rope.h',
243 'ropeimpl.h', 'SFile.h', 'slist', 'slist.h', 'stack.h', 'stdexcept',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000244 'stdiostream.h', 'streambuf.h', 'stream.h', 'strfile.h', 'string',
245 'strstream', 'strstream.h', 'tempbuf.h', 'tree.h', 'typeinfo', 'valarray',
246 ])
247
248
249# Assertion macros. These are defined in base/logging.h and
250# testing/base/gunit.h. Note that the _M versions need to come first
251# for substring matching to work.
252_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000253 'DCHECK', 'CHECK',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000254 'EXPECT_TRUE_M', 'EXPECT_TRUE',
255 'ASSERT_TRUE_M', 'ASSERT_TRUE',
256 'EXPECT_FALSE_M', 'EXPECT_FALSE',
257 'ASSERT_FALSE_M', 'ASSERT_FALSE',
258 ]
259
erg@google.come35f7652009-06-19 20:52:09 +0000260# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000261_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
262
263for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
264 ('>=', 'GE'), ('>', 'GT'),
265 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000266 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000267 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
268 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
269 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
270 _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
271 _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
272
273for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
274 ('>=', 'LT'), ('>', 'LE'),
275 ('<=', 'GT'), ('<', 'GE')]:
276 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
277 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
278 _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
279 _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
280
erg@google.comd350fe52013-01-14 17:51:48 +0000281# Alternative tokens and their replacements. For full list, see section 2.5
282# Alternative tokens [lex.digraph] in the C++ standard.
283#
284# Digraphs (such as '%:') are not included here since it's a mess to
285# match those on a word boundary.
286_ALT_TOKEN_REPLACEMENT = {
287 'and': '&&',
288 'bitor': '|',
289 'or': '||',
290 'xor': '^',
291 'compl': '~',
292 'bitand': '&',
293 'and_eq': '&=',
294 'or_eq': '|=',
295 'xor_eq': '^=',
296 'not': '!',
297 'not_eq': '!='
298 }
299
300# Compile regular expression that matches all the above keywords. The "[ =()]"
301# bit is meant to avoid matching these keywords outside of boolean expressions.
302#
303# False positives include C-style multi-line comments (http://go/nsiut )
304# and multi-line strings (http://go/beujw ), but those have always been
305# troublesome for cpplint.
306_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
307 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
308
erg@google.com4e00b9a2009-01-12 23:05:11 +0000309
310# These constants define types of headers for use with
311# _IncludeState.CheckNextIncludeOrder().
312_C_SYS_HEADER = 1
313_CPP_SYS_HEADER = 2
314_LIKELY_MY_HEADER = 3
315_POSSIBLE_MY_HEADER = 4
316_OTHER_HEADER = 5
317
erg@google.comd350fe52013-01-14 17:51:48 +0000318# These constants define the current inline assembly state
319_NO_ASM = 0 # Outside of inline assembly block
320_INSIDE_ASM = 1 # Inside inline assembly block
321_END_ASM = 2 # Last line of inline assembly block
322_BLOCK_ASM = 3 # The whole block is an inline assembly block
323
324# Match start of assembly blocks
325_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
326 r'(?:\s+(volatile|__volatile__))?'
327 r'\s*[{(]')
328
erg@google.com4e00b9a2009-01-12 23:05:11 +0000329
330_regexp_compile_cache = {}
331
erg+personal@google.com05189642010-04-30 20:43:03 +0000332# Finds occurrences of NOLINT or NOLINT(...).
333_RE_SUPPRESSION = re.compile(r'\bNOLINT\b(\([^)]*\))?')
334
335# {str, set(int)}: a map from error categories to sets of linenumbers
336# on which those errors are expected and should be suppressed.
337_error_suppressions = {}
338
339def ParseNolintSuppressions(filename, raw_line, linenum, error):
340 """Updates the global list of error-suppressions.
341
342 Parses any NOLINT comments on the current line, updating the global
343 error_suppressions store. Reports an error if the NOLINT comment
344 was malformed.
345
346 Args:
347 filename: str, the name of the input file.
348 raw_line: str, the line of input text, with comments.
349 linenum: int, the number of the current line.
350 error: function, an error handler.
351 """
352 # FIXME(adonovan): "NOLINT(" is misparsed as NOLINT(*).
erg@google.com8a95ecc2011-09-08 00:45:54 +0000353 matched = _RE_SUPPRESSION.search(raw_line)
354 if matched:
355 category = matched.group(1)
erg+personal@google.com05189642010-04-30 20:43:03 +0000356 if category in (None, '(*)'): # => "suppress all"
357 _error_suppressions.setdefault(None, set()).add(linenum)
358 else:
359 if category.startswith('(') and category.endswith(')'):
360 category = category[1:-1]
361 if category in _ERROR_CATEGORIES:
362 _error_suppressions.setdefault(category, set()).add(linenum)
363 else:
364 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000365 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000366
367
368def ResetNolintSuppressions():
369 "Resets the set of NOLINT suppressions to empty."
370 _error_suppressions.clear()
371
372
373def IsErrorSuppressedByNolint(category, linenum):
374 """Returns true if the specified error category is suppressed on this line.
375
376 Consults the global error_suppressions map populated by
377 ParseNolintSuppressions/ResetNolintSuppressions.
378
379 Args:
380 category: str, the category of the error.
381 linenum: int, the current line number.
382 Returns:
383 bool, True iff the error should be suppressed due to a NOLINT comment.
384 """
385 return (linenum in _error_suppressions.get(category, set()) or
386 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000387
388def Match(pattern, s):
389 """Matches the string with the pattern, caching the compiled regexp."""
390 # The regexp compilation caching is inlined in both Match and Search for
391 # performance reasons; factoring it out into a separate function turns out
392 # to be noticeably expensive.
393 if not pattern in _regexp_compile_cache:
394 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
395 return _regexp_compile_cache[pattern].match(s)
396
397
398def Search(pattern, s):
399 """Searches the string for the pattern, caching the compiled regexp."""
400 if not pattern in _regexp_compile_cache:
401 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
402 return _regexp_compile_cache[pattern].search(s)
403
404
405class _IncludeState(dict):
406 """Tracks line numbers for includes, and the order in which includes appear.
407
408 As a dict, an _IncludeState object serves as a mapping between include
409 filename and line number on which that file was included.
410
411 Call CheckNextIncludeOrder() once for each header in the file, passing
412 in the type constants defined above. Calls in an illegal order will
413 raise an _IncludeError with an appropriate error message.
414
415 """
416 # self._section will move monotonically through this set. If it ever
417 # needs to move backwards, CheckNextIncludeOrder will raise an error.
418 _INITIAL_SECTION = 0
419 _MY_H_SECTION = 1
420 _C_SECTION = 2
421 _CPP_SECTION = 3
422 _OTHER_H_SECTION = 4
423
424 _TYPE_NAMES = {
425 _C_SYS_HEADER: 'C system header',
426 _CPP_SYS_HEADER: 'C++ system header',
427 _LIKELY_MY_HEADER: 'header this file implements',
428 _POSSIBLE_MY_HEADER: 'header this file may implement',
429 _OTHER_HEADER: 'other header',
430 }
431 _SECTION_NAMES = {
432 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
433 _MY_H_SECTION: 'a header this file implements',
434 _C_SECTION: 'C system header',
435 _CPP_SECTION: 'C++ system header',
436 _OTHER_H_SECTION: 'other header',
437 }
438
439 def __init__(self):
440 dict.__init__(self)
erg@google.coma868d2d2009-10-09 21:18:45 +0000441 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000442 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000443 # The path of last found header.
444 self._last_header = ''
445
446 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000447 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000448
449 - replaces "-" with "_" so they both cmp the same.
450 - removes '-inl' since we don't require them to be after the main header.
451 - lowercase everything, just in case.
452
453 Args:
454 header_path: Path to be canonicalized.
455
456 Returns:
457 Canonicalized path.
458 """
459 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
460
461 def IsInAlphabeticalOrder(self, header_path):
462 """Check if a header is in alphabetical order with the previous header.
463
464 Args:
465 header_path: Header to be checked.
466
467 Returns:
468 Returns true if the header is in alphabetical order.
469 """
470 canonical_header = self.CanonicalizeAlphabeticalOrder(header_path)
471 if self._last_header > canonical_header:
472 return False
473 self._last_header = canonical_header
474 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000475
476 def CheckNextIncludeOrder(self, header_type):
477 """Returns a non-empty error message if the next header is out of order.
478
479 This function also updates the internal state to be ready to check
480 the next include.
481
482 Args:
483 header_type: One of the _XXX_HEADER constants defined above.
484
485 Returns:
486 The empty string if the header is in the right order, or an
487 error message describing what's wrong.
488
489 """
490 error_message = ('Found %s after %s' %
491 (self._TYPE_NAMES[header_type],
492 self._SECTION_NAMES[self._section]))
493
erg@google.coma868d2d2009-10-09 21:18:45 +0000494 last_section = self._section
495
erg@google.com4e00b9a2009-01-12 23:05:11 +0000496 if header_type == _C_SYS_HEADER:
497 if self._section <= self._C_SECTION:
498 self._section = self._C_SECTION
499 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000500 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000501 return error_message
502 elif header_type == _CPP_SYS_HEADER:
503 if self._section <= self._CPP_SECTION:
504 self._section = self._CPP_SECTION
505 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000506 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000507 return error_message
508 elif header_type == _LIKELY_MY_HEADER:
509 if self._section <= self._MY_H_SECTION:
510 self._section = self._MY_H_SECTION
511 else:
512 self._section = self._OTHER_H_SECTION
513 elif header_type == _POSSIBLE_MY_HEADER:
514 if self._section <= self._MY_H_SECTION:
515 self._section = self._MY_H_SECTION
516 else:
517 # This will always be the fallback because we're not sure
518 # enough that the header is associated with this file.
519 self._section = self._OTHER_H_SECTION
520 else:
521 assert header_type == _OTHER_HEADER
522 self._section = self._OTHER_H_SECTION
523
erg@google.coma868d2d2009-10-09 21:18:45 +0000524 if last_section != self._section:
525 self._last_header = ''
526
erg@google.com4e00b9a2009-01-12 23:05:11 +0000527 return ''
528
529
530class _CppLintState(object):
531 """Maintains module-wide state.."""
532
533 def __init__(self):
534 self.verbose_level = 1 # global setting.
535 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000536 # filters to apply when emitting error messages
537 self.filters = _DEFAULT_FILTERS[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000538 self.counting = 'total' # In what way are we counting errors?
539 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000540
541 # output format:
542 # "emacs" - format that emacs can parse (default)
543 # "vs7" - format that Microsoft Visual Studio 7 can parse
544 self.output_format = 'emacs'
545
546 def SetOutputFormat(self, output_format):
547 """Sets the output format for errors."""
548 self.output_format = output_format
549
550 def SetVerboseLevel(self, level):
551 """Sets the module's verbosity, and returns the previous setting."""
552 last_verbose_level = self.verbose_level
553 self.verbose_level = level
554 return last_verbose_level
555
erg@google.coma868d2d2009-10-09 21:18:45 +0000556 def SetCountingStyle(self, counting_style):
557 """Sets the module's counting options."""
558 self.counting = counting_style
559
erg@google.com4e00b9a2009-01-12 23:05:11 +0000560 def SetFilters(self, filters):
561 """Sets the error-message filters.
562
563 These filters are applied when deciding whether to emit a given
564 error message.
565
566 Args:
567 filters: A string of comma-separated filters (eg "+whitespace/indent").
568 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000569
570 Raises:
571 ValueError: The comma-separated filters did not all start with '+' or '-'.
572 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000573 """
erg@google.come35f7652009-06-19 20:52:09 +0000574 # Default filters always have less priority than the flag ones.
575 self.filters = _DEFAULT_FILTERS[:]
576 for filt in filters.split(','):
577 clean_filt = filt.strip()
578 if clean_filt:
579 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000580 for filt in self.filters:
581 if not (filt.startswith('+') or filt.startswith('-')):
582 raise ValueError('Every filter in --filters must start with + or -'
583 ' (%s does not)' % filt)
584
erg@google.coma868d2d2009-10-09 21:18:45 +0000585 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000586 """Sets the module's error statistic back to zero."""
587 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000588 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000589
erg@google.coma868d2d2009-10-09 21:18:45 +0000590 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000591 """Bumps the module's error statistic."""
592 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000593 if self.counting in ('toplevel', 'detailed'):
594 if self.counting != 'detailed':
595 category = category.split('/')[0]
596 if category not in self.errors_by_category:
597 self.errors_by_category[category] = 0
598 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000599
erg@google.coma868d2d2009-10-09 21:18:45 +0000600 def PrintErrorCounts(self):
601 """Print a summary of errors by category, and the total."""
602 for category, count in self.errors_by_category.iteritems():
603 sys.stderr.write('Category \'%s\' errors found: %d\n' %
604 (category, count))
605 sys.stderr.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000606
607_cpplint_state = _CppLintState()
608
609
610def _OutputFormat():
611 """Gets the module's output format."""
612 return _cpplint_state.output_format
613
614
615def _SetOutputFormat(output_format):
616 """Sets the module's output format."""
617 _cpplint_state.SetOutputFormat(output_format)
618
619
620def _VerboseLevel():
621 """Returns the module's verbosity setting."""
622 return _cpplint_state.verbose_level
623
624
625def _SetVerboseLevel(level):
626 """Sets the module's verbosity, and returns the previous setting."""
627 return _cpplint_state.SetVerboseLevel(level)
628
629
erg@google.coma868d2d2009-10-09 21:18:45 +0000630def _SetCountingStyle(level):
631 """Sets the module's counting options."""
632 _cpplint_state.SetCountingStyle(level)
633
634
erg@google.com4e00b9a2009-01-12 23:05:11 +0000635def _Filters():
636 """Returns the module's list of output filters, as a list."""
637 return _cpplint_state.filters
638
639
640def _SetFilters(filters):
641 """Sets the module's error-message filters.
642
643 These filters are applied when deciding whether to emit a given
644 error message.
645
646 Args:
647 filters: A string of comma-separated filters (eg "whitespace/indent").
648 Each filter should start with + or -; else we die.
649 """
650 _cpplint_state.SetFilters(filters)
651
652
653class _FunctionState(object):
654 """Tracks current function name and the number of lines in its body."""
655
656 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
657 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
658
659 def __init__(self):
660 self.in_a_function = False
661 self.lines_in_function = 0
662 self.current_function = ''
663
664 def Begin(self, function_name):
665 """Start analyzing function body.
666
667 Args:
668 function_name: The name of the function being tracked.
669 """
670 self.in_a_function = True
671 self.lines_in_function = 0
672 self.current_function = function_name
673
674 def Count(self):
675 """Count line in current function body."""
676 if self.in_a_function:
677 self.lines_in_function += 1
678
679 def Check(self, error, filename, linenum):
680 """Report if too many lines in function body.
681
682 Args:
683 error: The function to call with any errors found.
684 filename: The name of the current file.
685 linenum: The number of the line to check.
686 """
687 if Match(r'T(EST|est)', self.current_function):
688 base_trigger = self._TEST_TRIGGER
689 else:
690 base_trigger = self._NORMAL_TRIGGER
691 trigger = base_trigger * 2**_VerboseLevel()
692
693 if self.lines_in_function > trigger:
694 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
695 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
696 if error_level > 5:
697 error_level = 5
698 error(filename, linenum, 'readability/fn_size', error_level,
699 'Small and focused functions are preferred:'
700 ' %s has %d non-comment lines'
701 ' (error triggered by exceeding %d lines).' % (
702 self.current_function, self.lines_in_function, trigger))
703
704 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000705 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +0000706 self.in_a_function = False
707
708
709class _IncludeError(Exception):
710 """Indicates a problem with the include order in a file."""
711 pass
712
713
714class FileInfo:
715 """Provides utility functions for filenames.
716
717 FileInfo provides easy access to the components of a file's path
718 relative to the project root.
719 """
720
721 def __init__(self, filename):
722 self._filename = filename
723
724 def FullName(self):
725 """Make Windows paths like Unix."""
726 return os.path.abspath(self._filename).replace('\\', '/')
727
728 def RepositoryName(self):
729 """FullName after removing the local path to the repository.
730
731 If we have a real absolute path name here we can try to do something smart:
732 detecting the root of the checkout and truncating /path/to/checkout from
733 the name so that we get header guards that don't include things like
734 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
735 people on different computers who have checked the source out to different
736 locations won't see bogus errors.
737 """
738 fullname = self.FullName()
739
740 if os.path.exists(fullname):
741 project_dir = os.path.dirname(fullname)
742
743 if os.path.exists(os.path.join(project_dir, ".svn")):
744 # If there's a .svn file in the current directory, we recursively look
745 # up the directory tree for the top of the SVN checkout
746 root_dir = project_dir
747 one_up_dir = os.path.dirname(root_dir)
748 while os.path.exists(os.path.join(one_up_dir, ".svn")):
749 root_dir = os.path.dirname(root_dir)
750 one_up_dir = os.path.dirname(one_up_dir)
751
752 prefix = os.path.commonprefix([root_dir, project_dir])
753 return fullname[len(prefix) + 1:]
754
erg@google.com3dc74262011-11-30 01:12:00 +0000755 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
756 # searching up from the current path.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000757 root_dir = os.path.dirname(fullname)
758 while (root_dir != os.path.dirname(root_dir) and
erg@google.com5e169692010-01-28 20:17:01 +0000759 not os.path.exists(os.path.join(root_dir, ".git")) and
erg@google.com3dc74262011-11-30 01:12:00 +0000760 not os.path.exists(os.path.join(root_dir, ".hg")) and
761 not os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000762 root_dir = os.path.dirname(root_dir)
erg@google.com42e59b02010-10-04 22:18:07 +0000763
764 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +0000765 os.path.exists(os.path.join(root_dir, ".hg")) or
766 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +0000767 prefix = os.path.commonprefix([root_dir, project_dir])
768 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000769
770 # Don't know what to do; header guard warnings may be wrong...
771 return fullname
772
773 def Split(self):
774 """Splits the file into the directory, basename, and extension.
775
776 For 'chrome/browser/browser.cc', Split() would
777 return ('chrome/browser', 'browser', '.cc')
778
779 Returns:
780 A tuple of (directory, basename, extension).
781 """
782
783 googlename = self.RepositoryName()
784 project, rest = os.path.split(googlename)
785 return (project,) + os.path.splitext(rest)
786
787 def BaseName(self):
788 """File base name - text after the final slash, before the final period."""
789 return self.Split()[1]
790
791 def Extension(self):
792 """File extension - text following the final period."""
793 return self.Split()[2]
794
795 def NoExtension(self):
796 """File has no source file extension."""
797 return '/'.join(self.Split()[0:2])
798
799 def IsSource(self):
800 """File has a source file extension."""
801 return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx')
802
803
erg+personal@google.com05189642010-04-30 20:43:03 +0000804def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000805 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000806
807 # There are three ways we might decide not to print an error message:
808 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +0000809 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +0000810 if IsErrorSuppressedByNolint(category, linenum):
811 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +0000812 if confidence < _cpplint_state.verbose_level:
813 return False
814
815 is_filtered = False
816 for one_filter in _Filters():
817 if one_filter.startswith('-'):
818 if category.startswith(one_filter[1:]):
819 is_filtered = True
820 elif one_filter.startswith('+'):
821 if category.startswith(one_filter[1:]):
822 is_filtered = False
823 else:
824 assert False # should have been checked for in SetFilter.
825 if is_filtered:
826 return False
827
828 return True
829
830
831def Error(filename, linenum, category, confidence, message):
832 """Logs the fact we've found a lint error.
833
834 We log where the error was found, and also our confidence in the error,
835 that is, how certain we are this is a legitimate style regression, and
836 not a misidentification or a use that's sometimes justified.
837
erg+personal@google.com05189642010-04-30 20:43:03 +0000838 False positives can be suppressed by the use of
839 "cpplint(category)" comments on the offending line. These are
840 parsed into _error_suppressions.
841
erg@google.com4e00b9a2009-01-12 23:05:11 +0000842 Args:
843 filename: The name of the file containing the error.
844 linenum: The number of the line containing the error.
845 category: A string used to describe the "category" this bug
846 falls under: "whitespace", say, or "runtime". Categories
847 may have a hierarchy separated by slashes: "whitespace/indent".
848 confidence: A number from 1-5 representing a confidence score for
849 the error, with 5 meaning that we are certain of the problem,
850 and 1 meaning that it could be a legitimate construct.
851 message: The error message.
852 """
erg+personal@google.com05189642010-04-30 20:43:03 +0000853 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +0000854 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000855 if _cpplint_state.output_format == 'vs7':
856 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
857 filename, linenum, message, category, confidence))
858 else:
859 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
860 filename, linenum, message, category, confidence))
861
862
863# Matches standard C++ escape esequences per 2.13.2.3 of the C++ standard.
864_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
865 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
866# Matches strings. Escape codes should already be removed by ESCAPES.
867_RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES = re.compile(r'"[^"]*"')
868# Matches characters. Escape codes should already be removed by ESCAPES.
869_RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES = re.compile(r"'.'")
870# Matches multi-line C++ comments.
871# This RE is a little bit more complicated than one might expect, because we
872# have to take care of space removals tools so we can handle comments inside
873# statements better.
874# The current rule is: We only clear spaces from both sides when we're at the
875# end of the line. Otherwise, we try to remove spaces from the right side,
876# if this doesn't work we try on left side but only if there's a non-character
877# on the right.
878_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
879 r"""(\s*/\*.*\*/\s*$|
880 /\*.*\*/\s+|
881 \s+/\*.*\*/(?=\W)|
882 /\*.*\*/)""", re.VERBOSE)
883
884
885def IsCppString(line):
886 """Does line terminate so, that the next symbol is in string constant.
887
888 This function does not consider single-line nor multi-line comments.
889
890 Args:
891 line: is a partial line of code starting from the 0..n.
892
893 Returns:
894 True, if next character appended to 'line' is inside a
895 string constant.
896 """
897
898 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
899 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
900
901
902def FindNextMultiLineCommentStart(lines, lineix):
903 """Find the beginning marker for a multiline comment."""
904 while lineix < len(lines):
905 if lines[lineix].strip().startswith('/*'):
906 # Only return this marker if the comment goes beyond this line
907 if lines[lineix].strip().find('*/', 2) < 0:
908 return lineix
909 lineix += 1
910 return len(lines)
911
912
913def FindNextMultiLineCommentEnd(lines, lineix):
914 """We are inside a comment, find the end marker."""
915 while lineix < len(lines):
916 if lines[lineix].strip().endswith('*/'):
917 return lineix
918 lineix += 1
919 return len(lines)
920
921
922def RemoveMultiLineCommentsFromRange(lines, begin, end):
923 """Clears a range of lines for multi-line comments."""
924 # Having // dummy comments makes the lines non-empty, so we will not get
925 # unnecessary blank line warnings later in the code.
926 for i in range(begin, end):
927 lines[i] = '// dummy'
928
929
930def RemoveMultiLineComments(filename, lines, error):
931 """Removes multiline (c-style) comments from lines."""
932 lineix = 0
933 while lineix < len(lines):
934 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
935 if lineix_begin >= len(lines):
936 return
937 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
938 if lineix_end >= len(lines):
939 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
940 'Could not find end of multi-line comment')
941 return
942 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
943 lineix = lineix_end + 1
944
945
946def CleanseComments(line):
947 """Removes //-comments and single-line C-style /* */ comments.
948
949 Args:
950 line: A line of C++ source.
951
952 Returns:
953 The line with single-line comments removed.
954 """
955 commentpos = line.find('//')
956 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +0000957 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +0000958 # get rid of /* ... */
959 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
960
961
erg@google.coma87abb82009-02-24 01:41:01 +0000962class CleansedLines(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000963 """Holds 3 copies of all lines with different preprocessing applied to them.
964
965 1) elided member contains lines without strings and comments,
966 2) lines member contains lines without comments, and
erg@google.comd350fe52013-01-14 17:51:48 +0000967 3) raw_lines member contains all the lines without processing.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000968 All these three members are of <type 'list'>, and of the same length.
969 """
970
971 def __init__(self, lines):
972 self.elided = []
973 self.lines = []
974 self.raw_lines = lines
975 self.num_lines = len(lines)
976 for linenum in range(len(lines)):
977 self.lines.append(CleanseComments(lines[linenum]))
978 elided = self._CollapseStrings(lines[linenum])
979 self.elided.append(CleanseComments(elided))
980
981 def NumLines(self):
982 """Returns the number of lines represented."""
983 return self.num_lines
984
985 @staticmethod
986 def _CollapseStrings(elided):
987 """Collapses strings and chars on a line to simple "" or '' blocks.
988
989 We nix strings first so we're not fooled by text like '"http://"'
990
991 Args:
992 elided: The line being processed.
993
994 Returns:
995 The line with collapsed strings.
996 """
997 if not _RE_PATTERN_INCLUDE.match(elided):
998 # Remove escaped characters first to make quote/single quote collapsing
999 # basic. Things that look like escaped characters shouldn't occur
1000 # outside of strings and chars.
1001 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1002 elided = _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES.sub("''", elided)
1003 elided = _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES.sub('""', elided)
1004 return elided
1005
1006
erg@google.comd350fe52013-01-14 17:51:48 +00001007def FindEndOfExpressionInLine(line, startpos, depth, startchar, endchar):
1008 """Find the position just after the matching endchar.
1009
1010 Args:
1011 line: a CleansedLines line.
1012 startpos: start searching at this position.
1013 depth: nesting level at startpos.
1014 startchar: expression opening character.
1015 endchar: expression closing character.
1016
1017 Returns:
1018 Index just after endchar.
1019 """
1020 for i in xrange(startpos, len(line)):
1021 if line[i] == startchar:
1022 depth += 1
1023 elif line[i] == endchar:
1024 depth -= 1
1025 if depth == 0:
1026 return i + 1
1027 return -1
1028
1029
erg@google.com4e00b9a2009-01-12 23:05:11 +00001030def CloseExpression(clean_lines, linenum, pos):
1031 """If input points to ( or { or [, finds the position that closes it.
1032
erg@google.com8a95ecc2011-09-08 00:45:54 +00001033 If lines[linenum][pos] points to a '(' or '{' or '[', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001034 linenum/pos that correspond to the closing of the expression.
1035
1036 Args:
1037 clean_lines: A CleansedLines instance containing the file.
1038 linenum: The number of the line to check.
1039 pos: A position on the line.
1040
1041 Returns:
1042 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1043 (line, len(lines), -1) if we never find a close. Note we ignore
1044 strings and comments when matching; and the line we return is the
1045 'cleansed' line at linenum.
1046 """
1047
1048 line = clean_lines.elided[linenum]
1049 startchar = line[pos]
1050 if startchar not in '({[':
1051 return (line, clean_lines.NumLines(), -1)
1052 if startchar == '(': endchar = ')'
1053 if startchar == '[': endchar = ']'
1054 if startchar == '{': endchar = '}'
1055
erg@google.comd350fe52013-01-14 17:51:48 +00001056 # Check first line
1057 end_pos = FindEndOfExpressionInLine(line, pos, 0, startchar, endchar)
1058 if end_pos > -1:
1059 return (line, linenum, end_pos)
1060 tail = line[pos:]
1061 num_open = tail.count(startchar) - tail.count(endchar)
1062 while linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001063 linenum += 1
1064 line = clean_lines.elided[linenum]
erg@google.comd350fe52013-01-14 17:51:48 +00001065 delta = line.count(startchar) - line.count(endchar)
1066 if num_open + delta <= 0:
1067 return (line, linenum,
1068 FindEndOfExpressionInLine(line, 0, num_open, startchar, endchar))
1069 num_open += delta
erg@google.com4e00b9a2009-01-12 23:05:11 +00001070
erg@google.comd350fe52013-01-14 17:51:48 +00001071 # Did not find endchar before end of file, give up
1072 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001073
1074def CheckForCopyright(filename, lines, error):
1075 """Logs an error if no Copyright message appears at the top of the file."""
1076
1077 # We'll say it should occur by line 10. Don't forget there's a
1078 # dummy line at the front.
1079 for line in xrange(1, min(len(lines), 11)):
1080 if re.search(r'Copyright', lines[line], re.I): break
1081 else: # means no copyright line was found
1082 error(filename, 0, 'legal/copyright', 5,
1083 'No copyright message found. '
1084 'You should have a line: "Copyright [year] <Copyright Owner>"')
1085
1086
1087def GetHeaderGuardCPPVariable(filename):
1088 """Returns the CPP variable that should be used as a header guard.
1089
1090 Args:
1091 filename: The name of a C++ header file.
1092
1093 Returns:
1094 The CPP variable that should be used as a header guard in the
1095 named file.
1096
1097 """
1098
erg+personal@google.com05189642010-04-30 20:43:03 +00001099 # Restores original filename in case that cpplint is invoked from Emacs's
1100 # flymake.
1101 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001102 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
erg+personal@google.com05189642010-04-30 20:43:03 +00001103
erg@google.com4e00b9a2009-01-12 23:05:11 +00001104 fileinfo = FileInfo(filename)
1105 return re.sub(r'[-./\s]', '_', fileinfo.RepositoryName()).upper() + '_'
1106
1107
1108def CheckForHeaderGuard(filename, lines, error):
1109 """Checks that the file contains a header guard.
1110
erg@google.coma87abb82009-02-24 01:41:01 +00001111 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001112 headers, checks that the full pathname is used.
1113
1114 Args:
1115 filename: The name of the C++ header file.
1116 lines: An array of strings, each representing a line of the file.
1117 error: The function to call with any errors found.
1118 """
1119
1120 cppvar = GetHeaderGuardCPPVariable(filename)
1121
1122 ifndef = None
1123 ifndef_linenum = 0
1124 define = None
1125 endif = None
1126 endif_linenum = 0
1127 for linenum, line in enumerate(lines):
1128 linesplit = line.split()
1129 if len(linesplit) >= 2:
1130 # find the first occurrence of #ifndef and #define, save arg
1131 if not ifndef and linesplit[0] == '#ifndef':
1132 # set ifndef to the header guard presented on the #ifndef line.
1133 ifndef = linesplit[1]
1134 ifndef_linenum = linenum
1135 if not define and linesplit[0] == '#define':
1136 define = linesplit[1]
1137 # find the last occurrence of #endif, save entire line
1138 if line.startswith('#endif'):
1139 endif = line
1140 endif_linenum = linenum
1141
erg@google.comdc289702012-01-26 20:30:03 +00001142 if not ifndef:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001143 error(filename, 0, 'build/header_guard', 5,
1144 'No #ifndef header guard found, suggested CPP variable is: %s' %
1145 cppvar)
1146 return
1147
erg@google.comdc289702012-01-26 20:30:03 +00001148 if not define:
1149 error(filename, 0, 'build/header_guard', 5,
1150 'No #define header guard found, suggested CPP variable is: %s' %
1151 cppvar)
1152 return
1153
erg@google.com4e00b9a2009-01-12 23:05:11 +00001154 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1155 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001156 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001157 error_level = 0
1158 if ifndef != cppvar + '_':
1159 error_level = 5
1160
erg+personal@google.com05189642010-04-30 20:43:03 +00001161 ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum,
1162 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001163 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1164 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1165
erg@google.comdc289702012-01-26 20:30:03 +00001166 if define != ifndef:
1167 error(filename, 0, 'build/header_guard', 5,
1168 '#ifndef and #define don\'t match, suggested CPP variable is: %s' %
1169 cppvar)
1170 return
1171
erg+personal@google.com05189642010-04-30 20:43:03 +00001172 if endif != ('#endif // %s' % cppvar):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001173 error_level = 0
1174 if endif != ('#endif // %s' % (cppvar + '_')):
1175 error_level = 5
1176
erg+personal@google.com05189642010-04-30 20:43:03 +00001177 ParseNolintSuppressions(filename, lines[endif_linenum], endif_linenum,
1178 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001179 error(filename, endif_linenum, 'build/header_guard', error_level,
1180 '#endif line should be "#endif // %s"' % cppvar)
1181
1182
1183def CheckForUnicodeReplacementCharacters(filename, lines, error):
1184 """Logs an error for each line containing Unicode replacement characters.
1185
1186 These indicate that either the file contained invalid UTF-8 (likely)
1187 or Unicode replacement characters (which it shouldn't). Note that
1188 it's possible for this to throw off line numbering if the invalid
1189 UTF-8 occurred adjacent to a newline.
1190
1191 Args:
1192 filename: The name of the current file.
1193 lines: An array of strings, each representing a line of the file.
1194 error: The function to call with any errors found.
1195 """
1196 for linenum, line in enumerate(lines):
1197 if u'\ufffd' in line:
1198 error(filename, linenum, 'readability/utf8', 5,
1199 'Line contains invalid UTF-8 (or Unicode replacement character).')
1200
1201
1202def CheckForNewlineAtEOF(filename, lines, error):
1203 """Logs an error if there is no newline char at the end of the file.
1204
1205 Args:
1206 filename: The name of the current file.
1207 lines: An array of strings, each representing a line of the file.
1208 error: The function to call with any errors found.
1209 """
1210
1211 # The array lines() was created by adding two newlines to the
1212 # original file (go figure), then splitting on \n.
1213 # To verify that the file ends in \n, we just have to make sure the
1214 # last-but-two element of lines() exists and is empty.
1215 if len(lines) < 3 or lines[-2]:
1216 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1217 'Could not find a newline character at the end of the file.')
1218
1219
1220def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1221 """Logs an error if we see /* ... */ or "..." that extend past one line.
1222
1223 /* ... */ comments are legit inside macros, for one line.
1224 Otherwise, we prefer // comments, so it's ok to warn about the
1225 other. Likewise, it's ok for strings to extend across multiple
1226 lines, as long as a line continuation character (backslash)
1227 terminates each line. Although not currently prohibited by the C++
1228 style guide, it's ugly and unnecessary. We don't do well with either
1229 in this lint program, so we warn about both.
1230
1231 Args:
1232 filename: The name of the current file.
1233 clean_lines: A CleansedLines instance containing the file.
1234 linenum: The number of the line to check.
1235 error: The function to call with any errors found.
1236 """
1237 line = clean_lines.elided[linenum]
1238
1239 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1240 # second (escaped) slash may trigger later \" detection erroneously.
1241 line = line.replace('\\\\', '')
1242
1243 if line.count('/*') > line.count('*/'):
1244 error(filename, linenum, 'readability/multiline_comment', 5,
1245 'Complex multi-line /*...*/-style comment found. '
1246 'Lint may give bogus warnings. '
1247 'Consider replacing these with //-style comments, '
1248 'with #if 0...#endif, '
1249 'or with more clearly structured multi-line comments.')
1250
1251 if (line.count('"') - line.count('\\"')) % 2:
1252 error(filename, linenum, 'readability/multiline_string', 5,
1253 'Multi-line string ("...") found. This lint script doesn\'t '
1254 'do well with such strings, and may give bogus warnings. They\'re '
1255 'ugly and unnecessary, and you should use concatenation instead".')
1256
1257
1258threading_list = (
1259 ('asctime(', 'asctime_r('),
1260 ('ctime(', 'ctime_r('),
1261 ('getgrgid(', 'getgrgid_r('),
1262 ('getgrnam(', 'getgrnam_r('),
1263 ('getlogin(', 'getlogin_r('),
1264 ('getpwnam(', 'getpwnam_r('),
1265 ('getpwuid(', 'getpwuid_r('),
1266 ('gmtime(', 'gmtime_r('),
1267 ('localtime(', 'localtime_r('),
1268 ('rand(', 'rand_r('),
1269 ('readdir(', 'readdir_r('),
1270 ('strtok(', 'strtok_r('),
1271 ('ttyname(', 'ttyname_r('),
1272 )
1273
1274
1275def CheckPosixThreading(filename, clean_lines, linenum, error):
1276 """Checks for calls to thread-unsafe functions.
1277
1278 Much code has been originally written without consideration of
1279 multi-threading. Also, engineers are relying on their old experience;
1280 they have learned posix before threading extensions were added. These
1281 tests guide the engineers to use thread-safe functions (when using
1282 posix directly).
1283
1284 Args:
1285 filename: The name of the current file.
1286 clean_lines: A CleansedLines instance containing the file.
1287 linenum: The number of the line to check.
1288 error: The function to call with any errors found.
1289 """
1290 line = clean_lines.elided[linenum]
1291 for single_thread_function, multithread_safe_function in threading_list:
1292 ix = line.find(single_thread_function)
erg@google.coma87abb82009-02-24 01:41:01 +00001293 # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
erg@google.com4e00b9a2009-01-12 23:05:11 +00001294 if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and
1295 line[ix - 1] not in ('_', '.', '>'))):
1296 error(filename, linenum, 'runtime/threadsafe_fn', 2,
1297 'Consider using ' + multithread_safe_function +
1298 '...) instead of ' + single_thread_function +
1299 '...) for improved thread safety.')
1300
1301
erg@google.coma868d2d2009-10-09 21:18:45 +00001302# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00001303# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00001304_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00001305 r'^\s*\*\w+(\+\+|--);')
1306
1307
1308def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00001309 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00001310
1311 For example following function:
1312 void increment_counter(int* count) {
1313 *count++;
1314 }
1315 is invalid, because it effectively does count++, moving pointer, and should
1316 be replaced with ++*count, (*count)++ or *count += 1.
1317
1318 Args:
1319 filename: The name of the current file.
1320 clean_lines: A CleansedLines instance containing the file.
1321 linenum: The number of the line to check.
1322 error: The function to call with any errors found.
1323 """
1324 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00001325 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00001326 error(filename, linenum, 'runtime/invalid_increment', 5,
1327 'Changing pointer instead of value (or unused value of operator*).')
1328
1329
erg@google.comd350fe52013-01-14 17:51:48 +00001330class _BlockInfo(object):
1331 """Stores information about a generic block of code."""
1332
1333 def __init__(self, seen_open_brace):
1334 self.seen_open_brace = seen_open_brace
1335 self.open_parentheses = 0
1336 self.inline_asm = _NO_ASM
1337
1338 def CheckBegin(self, filename, clean_lines, linenum, error):
1339 """Run checks that applies to text up to the opening brace.
1340
1341 This is mostly for checking the text after the class identifier
1342 and the "{", usually where the base class is specified. For other
1343 blocks, there isn't much to check, so we always pass.
1344
1345 Args:
1346 filename: The name of the current file.
1347 clean_lines: A CleansedLines instance containing the file.
1348 linenum: The number of the line to check.
1349 error: The function to call with any errors found.
1350 """
1351 pass
1352
1353 def CheckEnd(self, filename, clean_lines, linenum, error):
1354 """Run checks that applies to text after the closing brace.
1355
1356 This is mostly used for checking end of namespace comments.
1357
1358 Args:
1359 filename: The name of the current file.
1360 clean_lines: A CleansedLines instance containing the file.
1361 linenum: The number of the line to check.
1362 error: The function to call with any errors found.
1363 """
1364 pass
1365
1366
1367class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001368 """Stores information about a class."""
1369
erg@google.comd350fe52013-01-14 17:51:48 +00001370 def __init__(self, name, class_or_struct, clean_lines, linenum):
1371 _BlockInfo.__init__(self, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001372 self.name = name
erg@google.comd350fe52013-01-14 17:51:48 +00001373 self.starting_linenum = linenum
erg@google.com4e00b9a2009-01-12 23:05:11 +00001374 self.is_derived = False
erg@google.comd350fe52013-01-14 17:51:48 +00001375 if class_or_struct == 'struct':
1376 self.access = 'public'
1377 else:
1378 self.access = 'private'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001379
erg@google.com8a95ecc2011-09-08 00:45:54 +00001380 # Try to find the end of the class. This will be confused by things like:
1381 # class A {
1382 # } *x = { ...
1383 #
1384 # But it's still good enough for CheckSectionSpacing.
1385 self.last_line = 0
1386 depth = 0
1387 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00001388 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00001389 depth += line.count('{') - line.count('}')
1390 if not depth:
1391 self.last_line = i
1392 break
1393
erg@google.comd350fe52013-01-14 17:51:48 +00001394 def CheckBegin(self, filename, clean_lines, linenum, error):
1395 # Look for a bare ':'
1396 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
1397 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00001398
erg@google.com4e00b9a2009-01-12 23:05:11 +00001399
erg@google.comd350fe52013-01-14 17:51:48 +00001400class _NamespaceInfo(_BlockInfo):
1401 """Stores information about a namespace."""
1402
1403 def __init__(self, name, linenum):
1404 _BlockInfo.__init__(self, False)
1405 self.name = name or ''
1406 self.starting_linenum = linenum
1407
1408 def CheckEnd(self, filename, clean_lines, linenum, error):
1409 """Check end of namespace comments."""
1410 line = clean_lines.raw_lines[linenum]
1411
1412 # Check how many lines is enclosed in this namespace. Don't issue
1413 # warning for missing namespace comments if there aren't enough
1414 # lines. However, do apply checks if there is already an end of
1415 # namespace comment and it's incorrect.
1416 #
1417 # TODO(unknown): We always want to check end of namespace comments
1418 # if a namespace is large, but sometimes we also want to apply the
1419 # check if a short namespace contained nontrivial things (something
1420 # other than forward declarations). There is currently no logic on
1421 # deciding what these nontrivial things are, so this check is
1422 # triggered by namespace size only, which works most of the time.
1423 if (linenum - self.starting_linenum < 10
1424 and not Match(r'};*\s*(//|/\*).*\bnamespace\b', line)):
1425 return
1426
1427 # Look for matching comment at end of namespace.
1428 #
1429 # Note that we accept C style "/* */" comments for terminating
1430 # namespaces, so that code that terminate namespaces inside
1431 # preprocessor macros can be cpplint clean. Example: http://go/nxpiz
1432 #
1433 # We also accept stuff like "// end of namespace <name>." with the
1434 # period at the end.
1435 #
1436 # Besides these, we don't accept anything else, otherwise we might
1437 # get false negatives when existing comment is a substring of the
1438 # expected namespace. Example: http://go/ldkdc, http://cl/23548205
1439 if self.name:
1440 # Named namespace
1441 if not Match((r'};*\s*(//|/\*).*\bnamespace\s+' + re.escape(self.name) +
1442 r'[\*/\.\\\s]*$'),
1443 line):
1444 error(filename, linenum, 'readability/namespace', 5,
1445 'Namespace should be terminated with "// namespace %s"' %
1446 self.name)
1447 else:
1448 # Anonymous namespace
1449 if not Match(r'};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
1450 error(filename, linenum, 'readability/namespace', 5,
1451 'Namespace should be terminated with "// namespace"')
1452
1453
1454class _PreprocessorInfo(object):
1455 """Stores checkpoints of nesting stacks when #if/#else is seen."""
1456
1457 def __init__(self, stack_before_if):
1458 # The entire nesting stack before #if
1459 self.stack_before_if = stack_before_if
1460
1461 # The entire nesting stack up to #else
1462 self.stack_before_else = []
1463
1464 # Whether we have already seen #else or #elif
1465 self.seen_else = False
1466
1467
1468class _NestingState(object):
1469 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001470
1471 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00001472 # Stack for tracking all braces. An object is pushed whenever we
1473 # see a "{", and popped when we see a "}". Only 3 types of
1474 # objects are possible:
1475 # - _ClassInfo: a class or struct.
1476 # - _NamespaceInfo: a namespace.
1477 # - _BlockInfo: some other type of block.
1478 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00001479
erg@google.comd350fe52013-01-14 17:51:48 +00001480 # Stack of _PreprocessorInfo objects.
1481 self.pp_stack = []
1482
1483 def SeenOpenBrace(self):
1484 """Check if we have seen the opening brace for the innermost block.
1485
1486 Returns:
1487 True if we have seen the opening brace, False if the innermost
1488 block is still expecting an opening brace.
1489 """
1490 return (not self.stack) or self.stack[-1].seen_open_brace
1491
1492 def InNamespaceBody(self):
1493 """Check if we are currently one level inside a namespace body.
1494
1495 Returns:
1496 True if top of the stack is a namespace block, False otherwise.
1497 """
1498 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
1499
1500 def UpdatePreprocessor(self, line):
1501 """Update preprocessor stack.
1502
1503 We need to handle preprocessors due to classes like this:
1504 #ifdef SWIG
1505 struct ResultDetailsPageElementExtensionPoint {
1506 #else
1507 struct ResultDetailsPageElementExtensionPoint : public Extension {
1508 #endif
1509 (see http://go/qwddn for original example)
1510
1511 We make the following assumptions (good enough for most files):
1512 - Preprocessor condition evaluates to true from #if up to first
1513 #else/#elif/#endif.
1514
1515 - Preprocessor condition evaluates to false from #else/#elif up
1516 to #endif. We still perform lint checks on these lines, but
1517 these do not affect nesting stack.
1518
1519 Args:
1520 line: current line to check.
1521 """
1522 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
1523 # Beginning of #if block, save the nesting stack here. The saved
1524 # stack will allow us to restore the parsing state in the #else case.
1525 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
1526 elif Match(r'^\s*#\s*(else|elif)\b', line):
1527 # Beginning of #else block
1528 if self.pp_stack:
1529 if not self.pp_stack[-1].seen_else:
1530 # This is the first #else or #elif block. Remember the
1531 # whole nesting stack up to this point. This is what we
1532 # keep after the #endif.
1533 self.pp_stack[-1].seen_else = True
1534 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
1535
1536 # Restore the stack to how it was before the #if
1537 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
1538 else:
1539 # TODO(unknown): unexpected #else, issue warning?
1540 pass
1541 elif Match(r'^\s*#\s*endif\b', line):
1542 # End of #if or #else blocks.
1543 if self.pp_stack:
1544 # If we saw an #else, we will need to restore the nesting
1545 # stack to its former state before the #else, otherwise we
1546 # will just continue from where we left off.
1547 if self.pp_stack[-1].seen_else:
1548 # Here we can just use a shallow copy since we are the last
1549 # reference to it.
1550 self.stack = self.pp_stack[-1].stack_before_else
1551 # Drop the corresponding #if
1552 self.pp_stack.pop()
1553 else:
1554 # TODO(unknown): unexpected #endif, issue warning?
1555 pass
1556
1557 def Update(self, filename, clean_lines, linenum, error):
1558 """Update nesting state with current line.
1559
1560 Args:
1561 filename: The name of the current file.
1562 clean_lines: A CleansedLines instance containing the file.
1563 linenum: The number of the line to check.
1564 error: The function to call with any errors found.
1565 """
1566 line = clean_lines.elided[linenum]
1567
1568 # Update pp_stack first
1569 self.UpdatePreprocessor(line)
1570
1571 # Count parentheses. This is to avoid adding struct arguments to
1572 # the nesting stack.
1573 if self.stack:
1574 inner_block = self.stack[-1]
1575 depth_change = line.count('(') - line.count(')')
1576 inner_block.open_parentheses += depth_change
1577
1578 # Also check if we are starting or ending an inline assembly block.
1579 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
1580 if (depth_change != 0 and
1581 inner_block.open_parentheses == 1 and
1582 _MATCH_ASM.match(line)):
1583 # Enter assembly block
1584 inner_block.inline_asm = _INSIDE_ASM
1585 else:
1586 # Not entering assembly block. If previous line was _END_ASM,
1587 # we will now shift to _NO_ASM state.
1588 inner_block.inline_asm = _NO_ASM
1589 elif (inner_block.inline_asm == _INSIDE_ASM and
1590 inner_block.open_parentheses == 0):
1591 # Exit assembly block
1592 inner_block.inline_asm = _END_ASM
1593
1594 # Consume namespace declaration at the beginning of the line. Do
1595 # this in a loop so that we catch same line declarations like this:
1596 # namespace proto2 { namespace bridge { class MessageSet; } }
1597 while True:
1598 # Match start of namespace. The "\b\s*" below catches namespace
1599 # declarations even if it weren't followed by a whitespace, this
1600 # is so that we don't confuse our namespace checker. The
1601 # missing spaces will be flagged by CheckSpacing.
1602 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
1603 if not namespace_decl_match:
1604 break
1605
1606 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
1607 self.stack.append(new_namespace)
1608
1609 line = namespace_decl_match.group(2)
1610 if line.find('{') != -1:
1611 new_namespace.seen_open_brace = True
1612 line = line[line.find('{') + 1:]
1613
1614 # Look for a class declaration in whatever is left of the line
1615 # after parsing namespaces. The regexp accounts for decorated classes
1616 # such as in:
1617 # class LOCKABLE API Object {
1618 # };
1619 #
1620 # Templates with class arguments may confuse the parser, for example:
1621 # template <class T
1622 # class Comparator = less<T>,
1623 # class Vector = vector<T> >
1624 # class HeapQueue {
1625 #
1626 # Because this parser has no nesting state about templates, by the
1627 # time it saw "class Comparator", it may think that it's a new class.
1628 # Nested templates have a similar problem:
1629 # template <
1630 # typename ExportedType,
1631 # typename TupleType,
1632 # template <typename, typename> class ImplTemplate>
1633 #
1634 # To avoid these cases, we ignore classes that are followed by '=' or '>'
1635 class_decl_match = Match(
1636 r'\s*(template\s*<[\w\s<>,:]*>\s*)?'
1637 '(class|struct)\s+([A-Z_]+\s+)*(\w+(?:::\w+)*)'
1638 '(([^=>]|<[^<>]*>)*)$', line)
1639 if (class_decl_match and
1640 (not self.stack or self.stack[-1].open_parentheses == 0)):
1641 self.stack.append(_ClassInfo(
1642 class_decl_match.group(4), class_decl_match.group(2),
1643 clean_lines, linenum))
1644 line = class_decl_match.group(5)
1645
1646 # If we have not yet seen the opening brace for the innermost block,
1647 # run checks here.
1648 if not self.SeenOpenBrace():
1649 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
1650
1651 # Update access control if we are inside a class/struct
1652 if self.stack and isinstance(self.stack[-1], _ClassInfo):
1653 access_match = Match(r'\s*(public|private|protected)\s*:', line)
1654 if access_match:
1655 self.stack[-1].access = access_match.group(1)
1656
1657 # Consume braces or semicolons from what's left of the line
1658 while True:
1659 # Match first brace, semicolon, or closed parenthesis.
1660 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
1661 if not matched:
1662 break
1663
1664 token = matched.group(1)
1665 if token == '{':
1666 # If namespace or class hasn't seen a opening brace yet, mark
1667 # namespace/class head as complete. Push a new block onto the
1668 # stack otherwise.
1669 if not self.SeenOpenBrace():
1670 self.stack[-1].seen_open_brace = True
1671 else:
1672 self.stack.append(_BlockInfo(True))
1673 if _MATCH_ASM.match(line):
1674 self.stack[-1].inline_asm = _BLOCK_ASM
1675 elif token == ';' or token == ')':
1676 # If we haven't seen an opening brace yet, but we already saw
1677 # a semicolon, this is probably a forward declaration. Pop
1678 # the stack for these.
1679 #
1680 # Similarly, if we haven't seen an opening brace yet, but we
1681 # already saw a closing parenthesis, then these are probably
1682 # function arguments with extra "class" or "struct" keywords.
1683 # Also pop these stack for these.
1684 if not self.SeenOpenBrace():
1685 self.stack.pop()
1686 else: # token == '}'
1687 # Perform end of block checks and pop the stack.
1688 if self.stack:
1689 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
1690 self.stack.pop()
1691 line = matched.group(2)
1692
1693 def InnermostClass(self):
1694 """Get class info on the top of the stack.
1695
1696 Returns:
1697 A _ClassInfo object if we are inside a class, or None otherwise.
1698 """
1699 for i in range(len(self.stack), 0, -1):
1700 classinfo = self.stack[i - 1]
1701 if isinstance(classinfo, _ClassInfo):
1702 return classinfo
1703 return None
1704
1705 def CheckClassFinished(self, filename, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001706 """Checks that all classes have been completely parsed.
1707
1708 Call this when all lines in a file have been processed.
1709 Args:
1710 filename: The name of the current file.
1711 error: The function to call with any errors found.
1712 """
erg@google.comd350fe52013-01-14 17:51:48 +00001713 # Note: This test can result in false positives if #ifdef constructs
1714 # get in the way of brace matching. See the testBuildClass test in
1715 # cpplint_unittest.py for an example of this.
1716 for obj in self.stack:
1717 if isinstance(obj, _ClassInfo):
1718 error(filename, obj.starting_linenum, 'build/class', 5,
1719 'Failed to find complete declaration of class %s' %
1720 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001721
1722
1723def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00001724 nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001725 """Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
1726
1727 Complain about several constructs which gcc-2 accepts, but which are
1728 not standard C++. Warning about these in lint is one way to ease the
1729 transition to new compilers.
1730 - put storage class first (e.g. "static const" instead of "const static").
1731 - "%lld" instead of %qd" in printf-type functions.
1732 - "%1$d" is non-standard in printf-type functions.
1733 - "\%" is an undefined character escape sequence.
1734 - text after #endif is not allowed.
1735 - invalid inner-style forward declaration.
1736 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001737
erg@google.coma868d2d2009-10-09 21:18:45 +00001738 Additionally, check for constructor/destructor style violations and reference
1739 members, as it is very convenient to do so while checking for
1740 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001741
1742 Args:
1743 filename: The name of the current file.
1744 clean_lines: A CleansedLines instance containing the file.
1745 linenum: The number of the line to check.
erg@google.comd350fe52013-01-14 17:51:48 +00001746 nesting_state: A _NestingState instance which maintains information about
1747 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001748 error: A callable to which errors are reported, which takes 4 arguments:
1749 filename, line number, error level, and message
1750 """
1751
1752 # Remove comments from the line, but leave in strings for now.
1753 line = clean_lines.lines[linenum]
1754
1755 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
1756 error(filename, linenum, 'runtime/printf_format', 3,
1757 '%q in format strings is deprecated. Use %ll instead.')
1758
1759 if Search(r'printf\s*\(.*".*%\d+\$', line):
1760 error(filename, linenum, 'runtime/printf_format', 2,
1761 '%N$ formats are unconventional. Try rewriting to avoid them.')
1762
1763 # Remove escaped backslashes before looking for undefined escapes.
1764 line = line.replace('\\\\', '')
1765
1766 if Search(r'("|\').*\\(%|\[|\(|{)', line):
1767 error(filename, linenum, 'build/printf_format', 3,
1768 '%, [, (, and { are undefined character escapes. Unescape them.')
1769
1770 # For the rest, work with both comments and strings removed.
1771 line = clean_lines.elided[linenum]
1772
1773 if Search(r'\b(const|volatile|void|char|short|int|long'
1774 r'|float|double|signed|unsigned'
1775 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00001776 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00001777 line):
1778 error(filename, linenum, 'build/storage_class', 5,
1779 'Storage class (static, extern, typedef, etc) should be first.')
1780
1781 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
1782 error(filename, linenum, 'build/endif_comment', 5,
1783 'Uncommented text after #endif is non-standard. Use a comment.')
1784
1785 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
1786 error(filename, linenum, 'build/forward_decl', 5,
1787 'Inner-style forward declarations are invalid. Remove this line.')
1788
1789 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
1790 line):
1791 error(filename, linenum, 'build/deprecated', 3,
1792 '>? and <? (max and min) operators are non-standard and deprecated.')
1793
erg@google.coma868d2d2009-10-09 21:18:45 +00001794 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
1795 # TODO(unknown): Could it be expanded safely to arbitrary references,
1796 # without triggering too many false positives? The first
1797 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
1798 # the restriction.
1799 # Here's the original regexp, for the reference:
1800 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
1801 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
1802 error(filename, linenum, 'runtime/member_string_references', 2,
1803 'const string& members are dangerous. It is much better to use '
1804 'alternatives, such as pointers or simple constants.')
1805
erg@google.comd350fe52013-01-14 17:51:48 +00001806 # Everything else in this function operates on class declarations.
1807 # Return early if the top of the nesting stack is not a class, or if
1808 # the class head is not completed yet.
1809 classinfo = nesting_state.InnermostClass()
1810 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001811 return
1812
erg@google.com4e00b9a2009-01-12 23:05:11 +00001813 # The class may have been declared with namespace or classname qualifiers.
1814 # The constructor and destructor will not have those qualifiers.
1815 base_classname = classinfo.name.split('::')[-1]
1816
1817 # Look for single-argument constructors that aren't marked explicit.
1818 # Technically a valid construct, but against style.
erg@google.com8a95ecc2011-09-08 00:45:54 +00001819 args = Match(r'\s+(?:inline\s+)?%s\s*\(([^,()]+)\)'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001820 % re.escape(base_classname),
1821 line)
1822 if (args and
1823 args.group(1) != 'void' and
erg@google.com8a95ecc2011-09-08 00:45:54 +00001824 not Match(r'(const\s+)?%s\s*(?:<\w+>\s*)?&' % re.escape(base_classname),
erg@google.com4e00b9a2009-01-12 23:05:11 +00001825 args.group(1).strip())):
1826 error(filename, linenum, 'runtime/explicit', 5,
1827 'Single-argument constructors should be marked explicit.')
1828
erg@google.com4e00b9a2009-01-12 23:05:11 +00001829
1830def CheckSpacingForFunctionCall(filename, line, linenum, error):
1831 """Checks for the correctness of various spacing around function calls.
1832
1833 Args:
1834 filename: The name of the current file.
1835 line: The text of the line to check.
1836 linenum: The number of the line to check.
1837 error: The function to call with any errors found.
1838 """
1839
1840 # Since function calls often occur inside if/for/while/switch
1841 # expressions - which have their own, more liberal conventions - we
1842 # first see if we should be looking inside such an expression for a
1843 # function call, to which we can apply more strict standards.
1844 fncall = line # if there's no control flow construct, look at whole line
1845 for pattern in (r'\bif\s*\((.*)\)\s*{',
1846 r'\bfor\s*\((.*)\)\s*{',
1847 r'\bwhile\s*\((.*)\)\s*[{;]',
1848 r'\bswitch\s*\((.*)\)\s*{'):
1849 match = Search(pattern, line)
1850 if match:
1851 fncall = match.group(1) # look inside the parens for function calls
1852 break
1853
1854 # Except in if/for/while/switch, there should never be space
1855 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
1856 # for nested parens ( (a+b) + c ). Likewise, there should never be
1857 # a space before a ( when it's a function argument. I assume it's a
1858 # function argument when the char before the whitespace is legal in
1859 # a function name (alnum + _) and we're not starting a macro. Also ignore
1860 # pointers and references to arrays and functions coz they're too tricky:
1861 # we use a very simple way to recognize these:
1862 # " (something)(maybe-something)" or
1863 # " (something)(maybe-something," or
1864 # " (something)[something]"
1865 # Note that we assume the contents of [] to be short enough that
1866 # they'll never need to wrap.
1867 if ( # Ignore control structures.
1868 not Search(r'\b(if|for|while|switch|return|delete)\b', fncall) and
1869 # Ignore pointers/references to functions.
1870 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
1871 # Ignore pointers/references to arrays.
1872 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00001873 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00001874 error(filename, linenum, 'whitespace/parens', 4,
1875 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00001876 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001877 error(filename, linenum, 'whitespace/parens', 2,
1878 'Extra space after (')
1879 if (Search(r'\w\s+\(', fncall) and
erg@google.comd350fe52013-01-14 17:51:48 +00001880 not Search(r'#\s*define|typedef', fncall) and
1881 not Search(r'\w\s+\((\w+::)?\*\w+\)\(', fncall)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001882 error(filename, linenum, 'whitespace/parens', 4,
1883 'Extra space before ( in function call')
1884 # If the ) is followed only by a newline or a { + newline, assume it's
1885 # part of a control statement (if/while/etc), and don't complain
1886 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001887 # If the closing parenthesis is preceded by only whitespaces,
1888 # try to give a more descriptive error message.
1889 if Search(r'^\s+\)', fncall):
1890 error(filename, linenum, 'whitespace/parens', 2,
1891 'Closing ) should be moved to the previous line')
1892 else:
1893 error(filename, linenum, 'whitespace/parens', 2,
1894 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001895
1896
1897def IsBlankLine(line):
1898 """Returns true if the given line is blank.
1899
1900 We consider a line to be blank if the line is empty or consists of
1901 only white spaces.
1902
1903 Args:
1904 line: A line of a string.
1905
1906 Returns:
1907 True, if the given line is blank.
1908 """
1909 return not line or line.isspace()
1910
1911
1912def CheckForFunctionLengths(filename, clean_lines, linenum,
1913 function_state, error):
1914 """Reports for long function bodies.
1915
1916 For an overview why this is done, see:
1917 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
1918
1919 Uses a simplistic algorithm assuming other style guidelines
1920 (especially spacing) are followed.
1921 Only checks unindented functions, so class members are unchecked.
1922 Trivial bodies are unchecked, so constructors with huge initializer lists
1923 may be missed.
1924 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00001925 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001926 NOLINT *on the last line of a function* disables this check.
1927
1928 Args:
1929 filename: The name of the current file.
1930 clean_lines: A CleansedLines instance containing the file.
1931 linenum: The number of the line to check.
1932 function_state: Current function name and lines in body so far.
1933 error: The function to call with any errors found.
1934 """
1935 lines = clean_lines.lines
1936 line = lines[linenum]
1937 raw = clean_lines.raw_lines
1938 raw_line = raw[linenum]
1939 joined_line = ''
1940
1941 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00001942 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00001943 match_result = Match(regexp, line)
1944 if match_result:
1945 # If the name is all caps and underscores, figure it's a macro and
1946 # ignore it, unless it's TEST or TEST_F.
1947 function_name = match_result.group(1).split()[-1]
1948 if function_name == 'TEST' or function_name == 'TEST_F' or (
1949 not Match(r'[A-Z_]+$', function_name)):
1950 starting_func = True
1951
1952 if starting_func:
1953 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00001954 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001955 start_line = lines[start_linenum]
1956 joined_line += ' ' + start_line.lstrip()
1957 if Search(r'(;|})', start_line): # Declarations and trivial functions
1958 body_found = True
1959 break # ... ignore
1960 elif Search(r'{', start_line):
1961 body_found = True
1962 function = Search(r'((\w|:)*)\(', line).group(1)
1963 if Match(r'TEST', function): # Handle TEST... macros
1964 parameter_regexp = Search(r'(\(.*\))', joined_line)
1965 if parameter_regexp: # Ignore bad syntax
1966 function += parameter_regexp.group(1)
1967 else:
1968 function += '()'
1969 function_state.Begin(function)
1970 break
1971 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00001972 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001973 error(filename, linenum, 'readability/fn_size', 5,
1974 'Lint failed to find start of function body.')
1975 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00001976 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001977 function_state.End()
1978 elif not Match(r'^\s*$', line):
1979 function_state.Count() # Count non-blank/non-comment lines.
1980
1981
1982_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
1983
1984
1985def CheckComment(comment, filename, linenum, error):
1986 """Checks for common mistakes in TODO comments.
1987
1988 Args:
1989 comment: The text of the comment from the line in question.
1990 filename: The name of the current file.
1991 linenum: The number of the line to check.
1992 error: The function to call with any errors found.
1993 """
1994 match = _RE_PATTERN_TODO.match(comment)
1995 if match:
1996 # One whitespace is correct; zero whitespace is handled elsewhere.
1997 leading_whitespace = match.group(1)
1998 if len(leading_whitespace) > 1:
1999 error(filename, linenum, 'whitespace/todo', 2,
2000 'Too many spaces before TODO')
2001
2002 username = match.group(2)
2003 if not username:
2004 error(filename, linenum, 'readability/todo', 2,
2005 'Missing username in TODO; it should look like '
2006 '"// TODO(my_username): Stuff."')
2007
2008 middle_whitespace = match.group(3)
erg@google.coma87abb82009-02-24 01:41:01 +00002009 # Comparisons made explicit for correctness -- pylint: disable-msg=C6403
erg@google.com4e00b9a2009-01-12 23:05:11 +00002010 if middle_whitespace != ' ' and middle_whitespace != '':
2011 error(filename, linenum, 'whitespace/todo', 2,
2012 'TODO(my_username) should be followed by a space')
2013
erg@google.comd350fe52013-01-14 17:51:48 +00002014def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
2015 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002016
erg@google.comd350fe52013-01-14 17:51:48 +00002017 Args:
2018 filename: The name of the current file.
2019 clean_lines: A CleansedLines instance containing the file.
2020 linenum: The number of the line to check.
2021 nesting_state: A _NestingState instance which maintains information about
2022 the current stack of nested blocks being parsed.
2023 error: The function to call with any errors found.
2024 """
2025 line = clean_lines.elided[linenum] # get rid of comments and strings
2026
2027 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
2028 r'DISALLOW_EVIL_CONSTRUCTORS|'
2029 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
2030 if not matched:
2031 return
2032 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
2033 if nesting_state.stack[-1].access != 'private':
2034 error(filename, linenum, 'readability/constructors', 3,
2035 '%s must be in the private: section' % matched.group(1))
2036
2037 else:
2038 # Found DISALLOW* macro outside a class declaration, or perhaps it
2039 # was used inside a function when it should have been part of the
2040 # class declaration. We could issue a warning here, but it
2041 # probably resulted in a compiler error already.
2042 pass
2043
2044
2045def FindNextMatchingAngleBracket(clean_lines, linenum, init_suffix):
2046 """Find the corresponding > to close a template.
2047
2048 Args:
2049 clean_lines: A CleansedLines instance containing the file.
2050 linenum: Current line number.
2051 init_suffix: Remainder of the current line after the initial <.
2052
2053 Returns:
2054 True if a matching bracket exists.
2055 """
2056 line = init_suffix
2057 nesting_stack = ['<']
2058 while True:
2059 # Find the next operator that can tell us whether < is used as an
2060 # opening bracket or as a less-than operator. We only want to
2061 # warn on the latter case.
2062 #
2063 # We could also check all other operators and terminate the search
2064 # early, e.g. if we got something like this "a<b+c", the "<" is
2065 # most likely a less-than operator, but then we will get false
2066 # positives for default arguments (e.g. http://go/prccd) and
2067 # other template expressions (e.g. http://go/oxcjq).
2068 match = Search(r'^[^<>(),;\[\]]*([<>(),;\[\]])(.*)$', line)
2069 if match:
2070 # Found an operator, update nesting stack
2071 operator = match.group(1)
2072 line = match.group(2)
2073
2074 if nesting_stack[-1] == '<':
2075 # Expecting closing angle bracket
2076 if operator in ('<', '(', '['):
2077 nesting_stack.append(operator)
2078 elif operator == '>':
2079 nesting_stack.pop()
2080 if not nesting_stack:
2081 # Found matching angle bracket
2082 return True
2083 elif operator == ',':
2084 # Got a comma after a bracket, this is most likely a template
2085 # argument. We have not seen a closing angle bracket yet, but
2086 # it's probably a few lines later if we look for it, so just
2087 # return early here.
2088 return True
2089 else:
2090 # Got some other operator.
2091 return False
2092
2093 else:
2094 # Expecting closing parenthesis or closing bracket
2095 if operator in ('<', '(', '['):
2096 nesting_stack.append(operator)
2097 elif operator in (')', ']'):
2098 # We don't bother checking for matching () or []. If we got
2099 # something like (] or [), it would have been a syntax error.
2100 nesting_stack.pop()
2101
2102 else:
2103 # Scan the next line
2104 linenum += 1
2105 if linenum >= len(clean_lines.elided):
2106 break
2107 line = clean_lines.elided[linenum]
2108
2109 # Exhausted all remaining lines and still no matching angle bracket.
2110 # Most likely the input was incomplete, otherwise we should have
2111 # seen a semicolon and returned early.
2112 return True
2113
2114
2115def FindPreviousMatchingAngleBracket(clean_lines, linenum, init_prefix):
2116 """Find the corresponding < that started a template.
2117
2118 Args:
2119 clean_lines: A CleansedLines instance containing the file.
2120 linenum: Current line number.
2121 init_prefix: Part of the current line before the initial >.
2122
2123 Returns:
2124 True if a matching bracket exists.
2125 """
2126 line = init_prefix
2127 nesting_stack = ['>']
2128 while True:
2129 # Find the previous operator
2130 match = Search(r'^(.*)([<>(),;\[\]])[^<>(),;\[\]]*$', line)
2131 if match:
2132 # Found an operator, update nesting stack
2133 operator = match.group(2)
2134 line = match.group(1)
2135
2136 if nesting_stack[-1] == '>':
2137 # Expecting opening angle bracket
2138 if operator in ('>', ')', ']'):
2139 nesting_stack.append(operator)
2140 elif operator == '<':
2141 nesting_stack.pop()
2142 if not nesting_stack:
2143 # Found matching angle bracket
2144 return True
2145 elif operator == ',':
2146 # Got a comma before a bracket, this is most likely a
2147 # template argument. The opening angle bracket is probably
2148 # there if we look for it, so just return early here.
2149 return True
2150 else:
2151 # Got some other operator.
2152 return False
2153
2154 else:
2155 # Expecting opening parenthesis or opening bracket
2156 if operator in ('>', ')', ']'):
2157 nesting_stack.append(operator)
2158 elif operator in ('(', '['):
2159 nesting_stack.pop()
2160
2161 else:
2162 # Scan the previous line
2163 linenum -= 1
2164 if linenum < 0:
2165 break
2166 line = clean_lines.elided[linenum]
2167
2168 # Exhausted all earlier lines and still no matching angle bracket.
2169 return False
2170
2171
2172def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002173 """Checks for the correctness of various spacing issues in the code.
2174
2175 Things we check for: spaces around operators, spaces after
2176 if/for/while/switch, no spaces around parens in function calls, two
2177 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00002178 line, don't end a function with a blank line, don't add a blank line
2179 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002180
2181 Args:
2182 filename: The name of the current file.
2183 clean_lines: A CleansedLines instance containing the file.
2184 linenum: The number of the line to check.
erg@google.comd350fe52013-01-14 17:51:48 +00002185 nesting_state: A _NestingState instance which maintains information about
2186 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002187 error: The function to call with any errors found.
2188 """
2189
2190 raw = clean_lines.raw_lines
2191 line = raw[linenum]
2192
2193 # Before nixing comments, check if the line is blank for no good
2194 # reason. This includes the first line after a block is opened, and
2195 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00002196 #
2197 # Skip all the blank line checks if we are immediately inside a
2198 # namespace body. In other words, don't issue blank line warnings
2199 # for this block:
2200 # namespace {
2201 #
2202 # }
2203 #
2204 # A warning about missing end of namespace comments will be issued instead.
2205 if IsBlankLine(line) and not nesting_state.InNamespaceBody():
erg@google.com4e00b9a2009-01-12 23:05:11 +00002206 elided = clean_lines.elided
2207 prev_line = elided[linenum - 1]
2208 prevbrace = prev_line.rfind('{')
2209 # TODO(unknown): Don't complain if line before blank line, and line after,
2210 # both start with alnums and are indented the same amount.
2211 # This ignores whitespace at the start of a namespace block
2212 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00002213 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002214 # OK, we have a blank line at the start of a code block. Before we
2215 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00002216 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00002217 # 4 spaces (because they did not fit in a 80 column line when placed on
2218 # the same line as the function name). We also check for the case where
2219 # the previous line is indented 6 spaces, which may happen when the
2220 # initializers of a constructor do not fit into a 80 column line.
2221 exception = False
2222 if Match(r' {6}\w', prev_line): # Initializer list?
2223 # We are looking for the opening column of initializer list, which
2224 # should be indented 4 spaces to cause 6 space indentation afterwards.
2225 search_position = linenum-2
2226 while (search_position >= 0
2227 and Match(r' {6}\w', elided[search_position])):
2228 search_position -= 1
2229 exception = (search_position >= 0
2230 and elided[search_position][:5] == ' :')
2231 else:
2232 # Search for the function arguments or an initializer list. We use a
2233 # simple heuristic here: If the line is indented 4 spaces; and we have a
2234 # closing paren, without the opening paren, followed by an opening brace
2235 # or colon (for initializer lists) we assume that it is the last line of
2236 # a function header. If we have a colon indented 4 spaces, it is an
2237 # initializer list.
2238 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
2239 prev_line)
2240 or Match(r' {4}:', prev_line))
2241
2242 if not exception:
2243 error(filename, linenum, 'whitespace/blank_line', 2,
2244 'Blank line at the start of a code block. Is this needed?')
erg@google.comd350fe52013-01-14 17:51:48 +00002245 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00002246 # chain, like this:
2247 # if (condition1) {
2248 # // Something followed by a blank line
2249 #
2250 # } else if (condition2) {
2251 # // Something else
2252 # }
2253 if linenum + 1 < clean_lines.NumLines():
2254 next_line = raw[linenum + 1]
2255 if (next_line
2256 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002257 and next_line.find('} else ') == -1):
2258 error(filename, linenum, 'whitespace/blank_line', 3,
2259 'Blank line at the end of a code block. Is this needed?')
2260
erg@google.com8a95ecc2011-09-08 00:45:54 +00002261 matched = Match(r'\s*(public|protected|private):', prev_line)
2262 if matched:
2263 error(filename, linenum, 'whitespace/blank_line', 3,
2264 'Do not leave a blank line after "%s:"' % matched.group(1))
2265
erg@google.com4e00b9a2009-01-12 23:05:11 +00002266 # Next, we complain if there's a comment too near the text
2267 commentpos = line.find('//')
2268 if commentpos != -1:
2269 # Check if the // may be in quotes. If so, ignore it
erg@google.coma87abb82009-02-24 01:41:01 +00002270 # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
erg@google.com4e00b9a2009-01-12 23:05:11 +00002271 if (line.count('"', 0, commentpos) -
2272 line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes
2273 # Allow one space for new scopes, two spaces otherwise:
2274 if (not Match(r'^\s*{ //', line) and
2275 ((commentpos >= 1 and
2276 line[commentpos-1] not in string.whitespace) or
2277 (commentpos >= 2 and
2278 line[commentpos-2] not in string.whitespace))):
2279 error(filename, linenum, 'whitespace/comments', 2,
2280 'At least two spaces is best between code and comments')
2281 # There should always be a space between the // and the comment
2282 commentend = commentpos + 2
2283 if commentend < len(line) and not line[commentend] == ' ':
2284 # but some lines are exceptions -- e.g. if they're big
2285 # comment delimiters like:
2286 # //----------------------------------------------------------
erg@google.coma51c16b2010-11-17 18:09:31 +00002287 # or are an empty C++ style Doxygen comment, like:
2288 # ///
erg@google.come35f7652009-06-19 20:52:09 +00002289 # or they begin with multiple slashes followed by a space:
2290 # //////// Header comment
2291 match = (Search(r'[=/-]{4,}\s*$', line[commentend:]) or
erg@google.coma51c16b2010-11-17 18:09:31 +00002292 Search(r'^/$', line[commentend:]) or
erg@google.come35f7652009-06-19 20:52:09 +00002293 Search(r'^/+ ', line[commentend:]))
erg@google.com4e00b9a2009-01-12 23:05:11 +00002294 if not match:
2295 error(filename, linenum, 'whitespace/comments', 4,
2296 'Should have a space between // and comment')
2297 CheckComment(line[commentpos:], filename, linenum, error)
2298
2299 line = clean_lines.elided[linenum] # get rid of comments and strings
2300
2301 # Don't try to do spacing checks for operator methods
2302 line = re.sub(r'operator(==|!=|<|<<|<=|>=|>>|>)\(', 'operator\(', line)
2303
2304 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
2305 # Otherwise not. Note we only check for non-spaces on *both* sides;
2306 # sometimes people put non-spaces on one side when aligning ='s among
2307 # many lines (not that this is behavior that I approve of...)
2308 if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line):
2309 error(filename, linenum, 'whitespace/operators', 4,
2310 'Missing spaces around =')
2311
2312 # It's ok not to have spaces around binary operators like + - * /, but if
2313 # there's too little whitespace, we get concerned. It's hard to tell,
2314 # though, so we punt on this one for now. TODO.
2315
2316 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00002317 #
2318 # Check <= and >= first to avoid false positives with < and >, then
2319 # check non-include lines for spacing around < and >.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002320 match = Search(r'[^<>=!\s](==|!=|<=|>=)[^<>=!\s]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002321 if match:
2322 error(filename, linenum, 'whitespace/operators', 3,
2323 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00002324 # We allow no-spaces around << when used like this: 10<<20, but
erg@google.com4e00b9a2009-01-12 23:05:11 +00002325 # not otherwise (particularly, not when used as streams)
erg@google.comd350fe52013-01-14 17:51:48 +00002326 match = Search(r'(\S)(?:L|UL|ULL|l|ul|ull)?<<(\S)', line)
2327 if match and not (match.group(1).isdigit() and match.group(2).isdigit()):
2328 error(filename, linenum, 'whitespace/operators', 3,
2329 'Missing spaces around <<')
2330 elif not Match(r'#.*include', line):
2331 # Avoid false positives on ->
2332 reduced_line = line.replace('->', '')
2333
2334 # Look for < that is not surrounded by spaces. This is only
2335 # triggered if both sides are missing spaces, even though
2336 # technically should should flag if at least one side is missing a
2337 # space. This is done to avoid some false positives with shifts.
2338 match = Search(r'[^\s<]<([^\s=<].*)', reduced_line)
2339 if (match and
2340 not FindNextMatchingAngleBracket(clean_lines, linenum, match.group(1))):
2341 error(filename, linenum, 'whitespace/operators', 3,
2342 'Missing spaces around <')
2343
2344 # Look for > that is not surrounded by spaces. Similar to the
2345 # above, we only trigger if both sides are missing spaces to avoid
2346 # false positives with shifts.
2347 match = Search(r'^(.*[^\s>])>[^\s=>]', reduced_line)
2348 if (match and
2349 not FindPreviousMatchingAngleBracket(clean_lines, linenum,
2350 match.group(1))):
2351 error(filename, linenum, 'whitespace/operators', 3,
2352 'Missing spaces around >')
2353
2354 # We allow no-spaces around >> for almost anything. This is because
2355 # C++11 allows ">>" to close nested templates, which accounts for
2356 # most cases when ">>" is not followed by a space.
2357 #
2358 # We still warn on ">>" followed by alpha character, because that is
2359 # likely due to ">>" being used for right shifts, e.g.:
2360 # value >> alpha
2361 #
2362 # When ">>" is used to close templates, the alphanumeric letter that
2363 # follows would be part of an identifier, and there should still be
2364 # a space separating the template type and the identifier.
2365 # type<type<type>> alpha
2366 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002367 if match:
2368 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00002369 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002370
2371 # There shouldn't be space around unary operators
2372 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
2373 if match:
2374 error(filename, linenum, 'whitespace/operators', 4,
2375 'Extra space for operator %s' % match.group(1))
2376
2377 # A pet peeve of mine: no spaces after an if, while, switch, or for
2378 match = Search(r' (if\(|for\(|while\(|switch\()', line)
2379 if match:
2380 error(filename, linenum, 'whitespace/parens', 5,
2381 'Missing space before ( in %s' % match.group(1))
2382
2383 # For if/for/while/switch, the left and right parens should be
2384 # consistent about how many spaces are inside the parens, and
2385 # there should either be zero or one spaces inside the parens.
2386 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00002387 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002388 match = Search(r'\b(if|for|while|switch)\s*'
2389 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
2390 line)
2391 if match:
2392 if len(match.group(2)) != len(match.group(4)):
2393 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00002394 len(match.group(2)) == 1 + len(match.group(4)) or
2395 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002396 error(filename, linenum, 'whitespace/parens', 5,
2397 'Mismatching spaces inside () in %s' % match.group(1))
2398 if not len(match.group(2)) in [0, 1]:
2399 error(filename, linenum, 'whitespace/parens', 5,
2400 'Should have zero or one spaces inside ( and ) in %s' %
2401 match.group(1))
2402
2403 # You should always have a space after a comma (either as fn arg or operator)
2404 if Search(r',[^\s]', line):
2405 error(filename, linenum, 'whitespace/comma', 3,
2406 'Missing space after ,')
2407
erg@google.comd7d27472011-09-07 17:36:35 +00002408 # You should always have a space after a semicolon
2409 # except for few corner cases
2410 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
2411 # space after ;
2412 if Search(r';[^\s};\\)/]', line):
2413 error(filename, linenum, 'whitespace/semicolon', 3,
2414 'Missing space after ;')
2415
erg@google.com4e00b9a2009-01-12 23:05:11 +00002416 # Next we will look for issues with function calls.
2417 CheckSpacingForFunctionCall(filename, line, linenum, error)
2418
erg@google.com8a95ecc2011-09-08 00:45:54 +00002419 # Except after an opening paren, or after another opening brace (in case of
2420 # an initializer list, for instance), you should have spaces before your
2421 # braces. And since you should never have braces at the beginning of a line,
2422 # this is an easy test.
2423 if Search(r'[^ ({]{', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002424 error(filename, linenum, 'whitespace/braces', 5,
2425 'Missing space before {')
2426
2427 # Make sure '} else {' has spaces.
2428 if Search(r'}else', line):
2429 error(filename, linenum, 'whitespace/braces', 5,
2430 'Missing space before else')
2431
2432 # You shouldn't have spaces before your brackets, except maybe after
2433 # 'delete []' or 'new char * []'.
2434 if Search(r'\w\s+\[', line) and not Search(r'delete\s+\[', line):
2435 error(filename, linenum, 'whitespace/braces', 5,
2436 'Extra space before [')
2437
2438 # You shouldn't have a space before a semicolon at the end of the line.
2439 # There's a special case for "for" since the style guide allows space before
2440 # the semicolon there.
2441 if Search(r':\s*;\s*$', line):
2442 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00002443 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002444 elif Search(r'^\s*;\s*$', line):
2445 error(filename, linenum, 'whitespace/semicolon', 5,
2446 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00002447 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002448 elif (Search(r'\s+;\s*$', line) and
2449 not Search(r'\bfor\b', line)):
2450 error(filename, linenum, 'whitespace/semicolon', 5,
2451 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00002452 'statement, use {} instead.')
2453
2454 # In range-based for, we wanted spaces before and after the colon, but
2455 # not around "::" tokens that might appear.
2456 if (Search('for *\(.*[^:]:[^: ]', line) or
2457 Search('for *\(.*[^: ]:[^:]', line)):
2458 error(filename, linenum, 'whitespace/forcolon', 2,
2459 'Missing space around colon in range-based for loop')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002460
2461
erg@google.com8a95ecc2011-09-08 00:45:54 +00002462def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
2463 """Checks for additional blank line issues related to sections.
2464
2465 Currently the only thing checked here is blank line before protected/private.
2466
2467 Args:
2468 filename: The name of the current file.
2469 clean_lines: A CleansedLines instance containing the file.
2470 class_info: A _ClassInfo objects.
2471 linenum: The number of the line to check.
2472 error: The function to call with any errors found.
2473 """
2474 # Skip checks if the class is small, where small means 25 lines or less.
2475 # 25 lines seems like a good cutoff since that's the usual height of
2476 # terminals, and any class that can't fit in one screen can't really
2477 # be considered "small".
2478 #
2479 # Also skip checks if we are on the first line. This accounts for
2480 # classes that look like
2481 # class Foo { public: ... };
2482 #
2483 # If we didn't find the end of the class, last_line would be zero,
2484 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00002485 if (class_info.last_line - class_info.starting_linenum <= 24 or
2486 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002487 return
2488
2489 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
2490 if matched:
2491 # Issue warning if the line before public/protected/private was
2492 # not a blank line, but don't do this if the previous line contains
2493 # "class" or "struct". This can happen two ways:
2494 # - We are at the beginning of the class.
2495 # - We are forward-declaring an inner class that is semantically
2496 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00002497 # Also ignores cases where the previous line ends with a backslash as can be
2498 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00002499 prev_line = clean_lines.lines[linenum - 1]
2500 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00002501 not Search(r'\b(class|struct)\b', prev_line) and
2502 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002503 # Try a bit harder to find the beginning of the class. This is to
2504 # account for multi-line base-specifier lists, e.g.:
2505 # class Derived
2506 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00002507 end_class_head = class_info.starting_linenum
2508 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002509 if Search(r'\{\s*$', clean_lines.lines[i]):
2510 end_class_head = i
2511 break
2512 if end_class_head < linenum - 1:
2513 error(filename, linenum, 'whitespace/blank_line', 3,
2514 '"%s:" should be preceded by a blank line' % matched.group(1))
2515
2516
erg@google.com4e00b9a2009-01-12 23:05:11 +00002517def GetPreviousNonBlankLine(clean_lines, linenum):
2518 """Return the most recent non-blank line and its line number.
2519
2520 Args:
2521 clean_lines: A CleansedLines instance containing the file contents.
2522 linenum: The number of the line to check.
2523
2524 Returns:
2525 A tuple with two elements. The first element is the contents of the last
2526 non-blank line before the current line, or the empty string if this is the
2527 first non-blank line. The second is the line number of that line, or -1
2528 if this is the first non-blank line.
2529 """
2530
2531 prevlinenum = linenum - 1
2532 while prevlinenum >= 0:
2533 prevline = clean_lines.elided[prevlinenum]
2534 if not IsBlankLine(prevline): # if not a blank line...
2535 return (prevline, prevlinenum)
2536 prevlinenum -= 1
2537 return ('', -1)
2538
2539
2540def CheckBraces(filename, clean_lines, linenum, error):
2541 """Looks for misplaced braces (e.g. at the end of line).
2542
2543 Args:
2544 filename: The name of the current file.
2545 clean_lines: A CleansedLines instance containing the file.
2546 linenum: The number of the line to check.
2547 error: The function to call with any errors found.
2548 """
2549
2550 line = clean_lines.elided[linenum] # get rid of comments and strings
2551
2552 if Match(r'\s*{\s*$', line):
2553 # We allow an open brace to start a line in the case where someone
2554 # is using braces in a block to explicitly create a new scope,
2555 # which is commonly used to control the lifetime of
2556 # stack-allocated variables. We don't detect this perfectly: we
2557 # just don't complain if the last non-whitespace character on the
erg@google.comd350fe52013-01-14 17:51:48 +00002558 # previous non-blank line is ';', ':', '{', or '}', or if the previous
2559 # line starts a preprocessor block.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002560 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.comd350fe52013-01-14 17:51:48 +00002561 if (not Search(r'[;:}{]\s*$', prevline) and
2562 not Match(r'\s*#', prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002563 error(filename, linenum, 'whitespace/braces', 4,
2564 '{ should almost always be at the end of the previous line')
2565
2566 # An else clause should be on the same line as the preceding closing brace.
2567 if Match(r'\s*else\s*', line):
2568 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
2569 if Match(r'\s*}\s*$', prevline):
2570 error(filename, linenum, 'whitespace/newline', 4,
2571 'An else should appear on the same line as the preceding }')
2572
2573 # If braces come on one side of an else, they should be on both.
2574 # However, we have to worry about "else if" that spans multiple lines!
2575 if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
2576 if Search(r'}\s*else if([^{]*)$', line): # could be multi-line if
2577 # find the ( after the if
2578 pos = line.find('else if')
2579 pos = line.find('(', pos)
2580 if pos > 0:
2581 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
2582 if endline[endpos:].find('{') == -1: # must be brace after if
2583 error(filename, linenum, 'readability/braces', 5,
2584 'If an else has a brace on one side, it should have it on both')
2585 else: # common case: else not followed by a multi-line if
2586 error(filename, linenum, 'readability/braces', 5,
2587 'If an else has a brace on one side, it should have it on both')
2588
2589 # Likewise, an else should never have the else clause on the same line
2590 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
2591 error(filename, linenum, 'whitespace/newline', 4,
2592 'Else clause should never be on same line as else (use 2 lines)')
2593
2594 # In the same way, a do/while should never be on one line
2595 if Match(r'\s*do [^\s{]', line):
2596 error(filename, linenum, 'whitespace/newline', 4,
2597 'do/while clauses should not be on a single line')
2598
2599 # Braces shouldn't be followed by a ; unless they're defining a struct
2600 # or initializing an array.
2601 # We can't tell in general, but we can for some common cases.
2602 prevlinenum = linenum
2603 while True:
2604 (prevline, prevlinenum) = GetPreviousNonBlankLine(clean_lines, prevlinenum)
2605 if Match(r'\s+{.*}\s*;', line) and not prevline.count(';'):
2606 line = prevline + line
2607 else:
2608 break
2609 if (Search(r'{.*}\s*;', line) and
2610 line.count('{') == line.count('}') and
2611 not Search(r'struct|class|enum|\s*=\s*{', line)):
2612 error(filename, linenum, 'readability/braces', 4,
2613 "You don't need a ; after a }")
2614
2615
erg@google.comd350fe52013-01-14 17:51:48 +00002616def CheckEmptyLoopBody(filename, clean_lines, linenum, error):
2617 """Loop for empty loop body with only a single semicolon.
2618
2619 Args:
2620 filename: The name of the current file.
2621 clean_lines: A CleansedLines instance containing the file.
2622 linenum: The number of the line to check.
2623 error: The function to call with any errors found.
2624 """
2625
2626 # Search for loop keywords at the beginning of the line. Because only
2627 # whitespaces are allowed before the keywords, this will also ignore most
2628 # do-while-loops, since those lines should start with closing brace.
2629 line = clean_lines.elided[linenum]
2630 if Match(r'\s*(for|while)\s*\(', line):
2631 # Find the end of the conditional expression
2632 (end_line, end_linenum, end_pos) = CloseExpression(
2633 clean_lines, linenum, line.find('('))
2634
2635 # Output warning if what follows the condition expression is a semicolon.
2636 # No warning for all other cases, including whitespace or newline, since we
2637 # have a separate check for semicolons preceded by whitespace.
2638 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
2639 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
2640 'Empty loop bodies should use {} or continue')
2641
2642
erg@google.com4e00b9a2009-01-12 23:05:11 +00002643def ReplaceableCheck(operator, macro, line):
2644 """Determine whether a basic CHECK can be replaced with a more specific one.
2645
2646 For example suggest using CHECK_EQ instead of CHECK(a == b) and
2647 similarly for CHECK_GE, CHECK_GT, CHECK_LE, CHECK_LT, CHECK_NE.
2648
2649 Args:
2650 operator: The C++ operator used in the CHECK.
2651 macro: The CHECK or EXPECT macro being called.
2652 line: The current source line.
2653
2654 Returns:
2655 True if the CHECK can be replaced with a more specific one.
2656 """
2657
2658 # This matches decimal and hex integers, strings, and chars (in that order).
2659 match_constant = r'([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')'
2660
2661 # Expression to match two sides of the operator with something that
2662 # looks like a literal, since CHECK(x == iterator) won't compile.
2663 # This means we can't catch all the cases where a more specific
2664 # CHECK is possible, but it's less annoying than dealing with
2665 # extraneous warnings.
2666 match_this = (r'\s*' + macro + r'\((\s*' +
2667 match_constant + r'\s*' + operator + r'[^<>].*|'
2668 r'.*[^<>]' + operator + r'\s*' + match_constant +
2669 r'\s*\))')
2670
2671 # Don't complain about CHECK(x == NULL) or similar because
2672 # CHECK_EQ(x, NULL) won't compile (requires a cast).
2673 # Also, don't complain about more complex boolean expressions
2674 # involving && or || such as CHECK(a == b || c == d).
2675 return Match(match_this, line) and not Search(r'NULL|&&|\|\|', line)
2676
2677
2678def CheckCheck(filename, clean_lines, linenum, error):
2679 """Checks the use of CHECK and EXPECT macros.
2680
2681 Args:
2682 filename: The name of the current file.
2683 clean_lines: A CleansedLines instance containing the file.
2684 linenum: The number of the line to check.
2685 error: The function to call with any errors found.
2686 """
2687
2688 # Decide the set of replacement macros that should be suggested
2689 raw_lines = clean_lines.raw_lines
2690 current_macro = ''
2691 for macro in _CHECK_MACROS:
2692 if raw_lines[linenum].find(macro) >= 0:
2693 current_macro = macro
2694 break
2695 if not current_macro:
2696 # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT'
2697 return
2698
2699 line = clean_lines.elided[linenum] # get rid of comments and strings
2700
2701 # Encourage replacing plain CHECKs with CHECK_EQ/CHECK_NE/etc.
2702 for operator in ['==', '!=', '>=', '>', '<=', '<']:
2703 if ReplaceableCheck(operator, current_macro, line):
2704 error(filename, linenum, 'readability/check', 2,
2705 'Consider using %s instead of %s(a %s b)' % (
2706 _CHECK_REPLACEMENT[current_macro][operator],
2707 current_macro, operator))
2708 break
2709
2710
erg@google.comd350fe52013-01-14 17:51:48 +00002711def CheckAltTokens(filename, clean_lines, linenum, error):
2712 """Check alternative keywords being used in boolean expressions.
2713
2714 Args:
2715 filename: The name of the current file.
2716 clean_lines: A CleansedLines instance containing the file.
2717 linenum: The number of the line to check.
2718 error: The function to call with any errors found.
2719 """
2720 line = clean_lines.elided[linenum]
2721
2722 # Avoid preprocessor lines
2723 if Match(r'^\s*#', line):
2724 return
2725
2726 # Last ditch effort to avoid multi-line comments. This will not help
2727 # if the comment started before the current line or ended after the
2728 # current line, but it catches most of the false positives. At least,
2729 # it provides a way to workaround this warning for people who use
2730 # multi-line comments in preprocessor macros.
2731 #
2732 # TODO(unknown): remove this once cpplint has better support for
2733 # multi-line comments.
2734 if line.find('/*') >= 0 or line.find('*/') >= 0:
2735 return
2736
2737 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
2738 error(filename, linenum, 'readability/alt_tokens', 2,
2739 'Use operator %s instead of %s' % (
2740 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
2741
2742
erg@google.com4e00b9a2009-01-12 23:05:11 +00002743def GetLineWidth(line):
2744 """Determines the width of the line in column positions.
2745
2746 Args:
2747 line: A string, which may be a Unicode string.
2748
2749 Returns:
2750 The width of the line in column positions, accounting for Unicode
2751 combining characters and wide characters.
2752 """
2753 if isinstance(line, unicode):
2754 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00002755 for uc in unicodedata.normalize('NFC', line):
2756 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002757 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00002758 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002759 width += 1
2760 return width
2761 else:
2762 return len(line)
2763
2764
erg@google.comd350fe52013-01-14 17:51:48 +00002765def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00002766 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002767 """Checks rules from the 'C++ style rules' section of cppguide.html.
2768
2769 Most of these rules are hard to test (naming, comment style), but we
2770 do what we can. In particular we check for 2-space indents, line lengths,
2771 tab usage, spaces inside code, etc.
2772
2773 Args:
2774 filename: The name of the current file.
2775 clean_lines: A CleansedLines instance containing the file.
2776 linenum: The number of the line to check.
2777 file_extension: The extension (without the dot) of the filename.
erg@google.comd350fe52013-01-14 17:51:48 +00002778 nesting_state: A _NestingState instance which maintains information about
2779 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002780 error: The function to call with any errors found.
2781 """
2782
2783 raw_lines = clean_lines.raw_lines
2784 line = raw_lines[linenum]
2785
2786 if line.find('\t') != -1:
2787 error(filename, linenum, 'whitespace/tab', 1,
2788 'Tab found; better to use spaces')
2789
2790 # One or three blank spaces at the beginning of the line is weird; it's
2791 # hard to reconcile that with 2-space indents.
2792 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
2793 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
2794 # if(RLENGTH > 20) complain = 0;
2795 # if(match($0, " +(error|private|public|protected):")) complain = 0;
2796 # if(match(prev, "&& *$")) complain = 0;
2797 # if(match(prev, "\\|\\| *$")) complain = 0;
2798 # if(match(prev, "[\",=><] *$")) complain = 0;
2799 # if(match($0, " <<")) complain = 0;
2800 # if(match(prev, " +for \\(")) complain = 0;
2801 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
2802 initial_spaces = 0
2803 cleansed_line = clean_lines.elided[linenum]
2804 while initial_spaces < len(line) and line[initial_spaces] == ' ':
2805 initial_spaces += 1
2806 if line and line[-1].isspace():
2807 error(filename, linenum, 'whitespace/end_of_line', 4,
2808 'Line ends in whitespace. Consider deleting these extra spaces.')
2809 # There are certain situations we allow one space, notably for labels
2810 elif ((initial_spaces == 1 or initial_spaces == 3) and
2811 not Match(r'\s*\w+\s*:\s*$', cleansed_line)):
2812 error(filename, linenum, 'whitespace/indent', 3,
2813 'Weird number of spaces at line-start. '
2814 'Are you using a 2-space indent?')
2815 # Labels should always be indented at least one space.
2816 elif not initial_spaces and line[:2] != '//' and Search(r'[^:]:\s*$',
2817 line):
2818 error(filename, linenum, 'whitespace/labels', 4,
2819 'Labels should always be indented at least one space. '
erg+personal@google.com05189642010-04-30 20:43:03 +00002820 'If this is a member-initializer list in a constructor or '
2821 'the base class list in a class definition, the colon should '
2822 'be on the following line.')
2823
erg@google.com4e00b9a2009-01-12 23:05:11 +00002824
2825 # Check if the line is a header guard.
2826 is_header_guard = False
2827 if file_extension == 'h':
2828 cppvar = GetHeaderGuardCPPVariable(filename)
2829 if (line.startswith('#ifndef %s' % cppvar) or
2830 line.startswith('#define %s' % cppvar) or
2831 line.startswith('#endif // %s' % cppvar)):
2832 is_header_guard = True
2833 # #include lines and header guards can be long, since there's no clean way to
2834 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00002835 #
2836 # URLs can be long too. It's possible to split these, but it makes them
2837 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00002838 #
2839 # The "$Id:...$" comment may also get very long without it being the
2840 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00002841 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00002842 not Match(r'^\s*//.*http(s?)://\S*$', line) and
2843 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002844 line_width = GetLineWidth(line)
2845 if line_width > 100:
2846 error(filename, linenum, 'whitespace/line_length', 4,
2847 'Lines should very rarely be longer than 100 characters')
2848 elif line_width > 80:
2849 error(filename, linenum, 'whitespace/line_length', 2,
2850 'Lines should be <= 80 characters long')
2851
2852 if (cleansed_line.count(';') > 1 and
2853 # for loops are allowed two ;'s (and may run over two lines).
2854 cleansed_line.find('for') == -1 and
2855 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
2856 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
2857 # It's ok to have many commands in a switch case that fits in 1 line
2858 not ((cleansed_line.find('case ') != -1 or
2859 cleansed_line.find('default:') != -1) and
2860 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00002861 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00002862 'More than one command on the same line')
2863
2864 # Some more style checks
2865 CheckBraces(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00002866 CheckEmptyLoopBody(filename, clean_lines, linenum, error)
2867 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
2868 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002869 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00002870 CheckAltTokens(filename, clean_lines, linenum, error)
2871 classinfo = nesting_state.InnermostClass()
2872 if classinfo:
2873 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002874
2875
2876_RE_PATTERN_INCLUDE_NEW_STYLE = re.compile(r'#include +"[^/]+\.h"')
2877_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
2878# Matches the first component of a filename delimited by -s and _s. That is:
2879# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
2880# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
2881# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
2882# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
2883_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
2884
2885
2886def _DropCommonSuffixes(filename):
2887 """Drops common suffixes like _test.cc or -inl.h from filename.
2888
2889 For example:
2890 >>> _DropCommonSuffixes('foo/foo-inl.h')
2891 'foo/foo'
2892 >>> _DropCommonSuffixes('foo/bar/foo.cc')
2893 'foo/bar/foo'
2894 >>> _DropCommonSuffixes('foo/foo_internal.h')
2895 'foo/foo'
2896 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
2897 'foo/foo_unusualinternal'
2898
2899 Args:
2900 filename: The input filename.
2901
2902 Returns:
2903 The filename with the common suffix removed.
2904 """
2905 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
2906 'inl.h', 'impl.h', 'internal.h'):
2907 if (filename.endswith(suffix) and len(filename) > len(suffix) and
2908 filename[-len(suffix) - 1] in ('-', '_')):
2909 return filename[:-len(suffix) - 1]
2910 return os.path.splitext(filename)[0]
2911
2912
2913def _IsTestFilename(filename):
2914 """Determines if the given filename has a suffix that identifies it as a test.
2915
2916 Args:
2917 filename: The input filename.
2918
2919 Returns:
2920 True if 'filename' looks like a test, False otherwise.
2921 """
2922 if (filename.endswith('_test.cc') or
2923 filename.endswith('_unittest.cc') or
2924 filename.endswith('_regtest.cc')):
2925 return True
2926 else:
2927 return False
2928
2929
2930def _ClassifyInclude(fileinfo, include, is_system):
2931 """Figures out what kind of header 'include' is.
2932
2933 Args:
2934 fileinfo: The current file cpplint is running over. A FileInfo instance.
2935 include: The path to a #included file.
2936 is_system: True if the #include used <> rather than "".
2937
2938 Returns:
2939 One of the _XXX_HEADER constants.
2940
2941 For example:
2942 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
2943 _C_SYS_HEADER
2944 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
2945 _CPP_SYS_HEADER
2946 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
2947 _LIKELY_MY_HEADER
2948 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
2949 ... 'bar/foo_other_ext.h', False)
2950 _POSSIBLE_MY_HEADER
2951 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
2952 _OTHER_HEADER
2953 """
2954 # This is a list of all standard c++ header files, except
2955 # those already checked for above.
2956 is_stl_h = include in _STL_HEADERS
2957 is_cpp_h = is_stl_h or include in _CPP_HEADERS
2958
2959 if is_system:
2960 if is_cpp_h:
2961 return _CPP_SYS_HEADER
2962 else:
2963 return _C_SYS_HEADER
2964
2965 # If the target file and the include we're checking share a
2966 # basename when we drop common extensions, and the include
2967 # lives in . , then it's likely to be owned by the target file.
2968 target_dir, target_base = (
2969 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
2970 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
2971 if target_base == include_base and (
2972 include_dir == target_dir or
2973 include_dir == os.path.normpath(target_dir + '/../public')):
2974 return _LIKELY_MY_HEADER
2975
2976 # If the target and include share some initial basename
2977 # component, it's possible the target is implementing the
2978 # include, so it's allowed to be first, but we'll never
2979 # complain if it's not there.
2980 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
2981 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
2982 if (target_first_component and include_first_component and
2983 target_first_component.group(0) ==
2984 include_first_component.group(0)):
2985 return _POSSIBLE_MY_HEADER
2986
2987 return _OTHER_HEADER
2988
2989
erg@google.coma87abb82009-02-24 01:41:01 +00002990
erg@google.come35f7652009-06-19 20:52:09 +00002991def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
2992 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002993
erg@google.come35f7652009-06-19 20:52:09 +00002994 Strings on #include lines are NOT removed from elided line, to make
2995 certain tasks easier. However, to prevent false positives, checks
2996 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002997
2998 Args:
2999 filename: The name of the current file.
3000 clean_lines: A CleansedLines instance containing the file.
3001 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003002 include_state: An _IncludeState instance in which the headers are inserted.
3003 error: The function to call with any errors found.
3004 """
3005 fileinfo = FileInfo(filename)
3006
erg@google.come35f7652009-06-19 20:52:09 +00003007 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003008
3009 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
erg@google.come35f7652009-06-19 20:52:09 +00003010 if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003011 error(filename, linenum, 'build/include', 4,
3012 'Include the directory when naming .h files')
3013
3014 # we shouldn't include a file more than once. actually, there are a
3015 # handful of instances where doing so is okay, but in general it's
3016 # not.
erg@google.come35f7652009-06-19 20:52:09 +00003017 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003018 if match:
3019 include = match.group(2)
3020 is_system = (match.group(1) == '<')
3021 if include in include_state:
3022 error(filename, linenum, 'build/include', 4,
3023 '"%s" already included at %s:%s' %
3024 (include, filename, include_state[include]))
3025 else:
3026 include_state[include] = linenum
3027
3028 # We want to ensure that headers appear in the right order:
3029 # 1) for foo.cc, foo.h (preferred location)
3030 # 2) c system files
3031 # 3) cpp system files
3032 # 4) for foo.cc, foo.h (deprecated location)
3033 # 5) other google headers
3034 #
3035 # We classify each include statement as one of those 5 types
3036 # using a number of techniques. The include_state object keeps
3037 # track of the highest type seen, and complains if we see a
3038 # lower type after that.
3039 error_message = include_state.CheckNextIncludeOrder(
3040 _ClassifyInclude(fileinfo, include, is_system))
3041 if error_message:
3042 error(filename, linenum, 'build/include_order', 4,
3043 '%s. Should be: %s.h, c system, c++ system, other.' %
3044 (error_message, fileinfo.BaseName()))
erg@google.coma868d2d2009-10-09 21:18:45 +00003045 if not include_state.IsInAlphabeticalOrder(include):
3046 error(filename, linenum, 'build/include_alpha', 4,
3047 'Include "%s" not in alphabetical order' % include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003048
erg@google.come35f7652009-06-19 20:52:09 +00003049 # Look for any of the stream classes that are part of standard C++.
3050 match = _RE_PATTERN_INCLUDE.match(line)
3051 if match:
3052 include = match.group(2)
3053 if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
3054 # Many unit tests use cout, so we exempt them.
3055 if not _IsTestFilename(filename):
3056 error(filename, linenum, 'readability/streams', 3,
3057 'Streams are highly discouraged.')
3058
erg@google.com8a95ecc2011-09-08 00:45:54 +00003059
3060def _GetTextInside(text, start_pattern):
3061 """Retrieves all the text between matching open and close parentheses.
3062
3063 Given a string of lines and a regular expression string, retrieve all the text
3064 following the expression and between opening punctuation symbols like
3065 (, [, or {, and the matching close-punctuation symbol. This properly nested
3066 occurrences of the punctuations, so for the text like
3067 printf(a(), b(c()));
3068 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
3069 start_pattern must match string having an open punctuation symbol at the end.
3070
3071 Args:
3072 text: The lines to extract text. Its comments and strings must be elided.
3073 It can be single line and can span multiple lines.
3074 start_pattern: The regexp string indicating where to start extracting
3075 the text.
3076 Returns:
3077 The extracted text.
3078 None if either the opening string or ending punctuation could not be found.
3079 """
3080 # TODO(sugawarayu): Audit cpplint.py to see what places could be profitably
3081 # rewritten to use _GetTextInside (and use inferior regexp matching today).
3082
3083 # Give opening punctuations to get the matching close-punctuations.
3084 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
3085 closing_punctuation = set(matching_punctuation.itervalues())
3086
3087 # Find the position to start extracting text.
3088 match = re.search(start_pattern, text, re.M)
3089 if not match: # start_pattern not found in text.
3090 return None
3091 start_position = match.end(0)
3092
3093 assert start_position > 0, (
3094 'start_pattern must ends with an opening punctuation.')
3095 assert text[start_position - 1] in matching_punctuation, (
3096 'start_pattern must ends with an opening punctuation.')
3097 # Stack of closing punctuations we expect to have in text after position.
3098 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
3099 position = start_position
3100 while punctuation_stack and position < len(text):
3101 if text[position] == punctuation_stack[-1]:
3102 punctuation_stack.pop()
3103 elif text[position] in closing_punctuation:
3104 # A closing punctuation without matching opening punctuations.
3105 return None
3106 elif text[position] in matching_punctuation:
3107 punctuation_stack.append(matching_punctuation[text[position]])
3108 position += 1
3109 if punctuation_stack:
3110 # Opening punctuations left without matching close-punctuations.
3111 return None
3112 # punctuations match.
3113 return text[start_position:position - 1]
3114
3115
erg@google.come35f7652009-06-19 20:52:09 +00003116def CheckLanguage(filename, clean_lines, linenum, file_extension, include_state,
3117 error):
3118 """Checks rules from the 'C++ language rules' section of cppguide.html.
3119
3120 Some of these rules are hard to test (function overloading, using
3121 uint32 inappropriately), but we do the best we can.
3122
3123 Args:
3124 filename: The name of the current file.
3125 clean_lines: A CleansedLines instance containing the file.
3126 linenum: The number of the line to check.
3127 file_extension: The extension (without the dot) of the filename.
3128 include_state: An _IncludeState instance in which the headers are inserted.
3129 error: The function to call with any errors found.
3130 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00003131 # If the line is empty or consists of entirely a comment, no need to
3132 # check it.
3133 line = clean_lines.elided[linenum]
3134 if not line:
3135 return
3136
erg@google.come35f7652009-06-19 20:52:09 +00003137 match = _RE_PATTERN_INCLUDE.search(line)
3138 if match:
3139 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
3140 return
3141
erg@google.com4e00b9a2009-01-12 23:05:11 +00003142 # Create an extended_line, which is the concatenation of the current and
3143 # next lines, for more effective checking of code that may span more than one
3144 # line.
3145 if linenum + 1 < clean_lines.NumLines():
3146 extended_line = line + clean_lines.elided[linenum + 1]
3147 else:
3148 extended_line = line
3149
3150 # Make Windows paths like Unix.
3151 fullname = os.path.abspath(filename).replace('\\', '/')
3152
3153 # TODO(unknown): figure out if they're using default arguments in fn proto.
3154
erg@google.com4e00b9a2009-01-12 23:05:11 +00003155 # Check for non-const references in functions. This is tricky because &
3156 # is also used to take the address of something. We allow <> for templates,
3157 # (ignoring whatever is between the braces) and : for classes.
3158 # These are complicated re's. They try to capture the following:
3159 # paren (for fn-prototype start), typename, &, varname. For the const
3160 # version, we're willing for const to be before typename or after
erg@google.com8a95ecc2011-09-08 00:45:54 +00003161 # Don't check the implementation on same line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003162 fnline = line.split('{', 1)[0]
3163 if (len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) >
3164 len(re.findall(r'\([^()]*\bconst\s+(?:typename\s+)?(?:struct\s+)?'
3165 r'(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) +
3166 len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+\s+const(\s?&|&\s?)[\w]+',
3167 fnline))):
3168
3169 # We allow non-const references in a few standard places, like functions
erg@google.comd350fe52013-01-14 17:51:48 +00003170 # called "swap()" or iostream operators like "<<" or ">>". We also filter
3171 # out for loops, which lint otherwise mistakenly thinks are functions.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003172 if not Search(
erg@google.comd350fe52013-01-14 17:51:48 +00003173 r'(for|swap|Swap|operator[<>][<>])\s*\(\s*'
3174 r'(?:(?:typename\s*)?[\w:]|<.*>)+\s*&',
erg@google.com4e00b9a2009-01-12 23:05:11 +00003175 fnline):
3176 error(filename, linenum, 'runtime/references', 2,
3177 'Is this a non-const reference? '
3178 'If so, make const or use a pointer.')
3179
3180 # Check to see if they're using an conversion function cast.
3181 # I just try to capture the most common basic types, though there are more.
3182 # Parameterless conversion functions, such as bool(), are allowed as they are
3183 # probably a member operator declaration or default constructor.
3184 match = Search(
erg@google.coma868d2d2009-10-09 21:18:45 +00003185 r'(\bnew\s+)?\b' # Grab 'new' operator, if it's there
3186 r'(int|float|double|bool|char|int32|uint32|int64|uint64)\([^)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003187 if match:
3188 # gMock methods are defined using some variant of MOCK_METHODx(name, type)
3189 # where type may be float(), int(string), etc. Without context they are
erg@google.comd7d27472011-09-07 17:36:35 +00003190 # virtually indistinguishable from int(x) casts. Likewise, gMock's
3191 # MockCallback takes a template parameter of the form return_type(arg_type),
3192 # which looks much like the cast we're trying to detect.
erg@google.coma868d2d2009-10-09 21:18:45 +00003193 if (match.group(1) is None and # If new operator, then this isn't a cast
erg@google.comd7d27472011-09-07 17:36:35 +00003194 not (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
3195 Match(r'^\s*MockCallback<.*>', line))):
erg@google.comd350fe52013-01-14 17:51:48 +00003196 # Try a bit harder to catch gmock lines: the only place where
3197 # something looks like an old-style cast is where we declare the
3198 # return type of the mocked method, and the only time when we
3199 # are missing context is if MOCK_METHOD was split across
3200 # multiple lines (for example http://go/hrfhr ), so we only need
3201 # to check the previous line for MOCK_METHOD.
3202 if (linenum == 0 or
3203 not Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(\S+,\s*$',
3204 clean_lines.elided[linenum - 1])):
3205 error(filename, linenum, 'readability/casting', 4,
3206 'Using deprecated casting style. '
3207 'Use static_cast<%s>(...) instead' %
3208 match.group(2))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003209
3210 CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3211 'static_cast',
erg@google.com8a95ecc2011-09-08 00:45:54 +00003212 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
3213
3214 # This doesn't catch all cases. Consider (const char * const)"hello".
3215 #
3216 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
3217 # compile).
3218 if CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3219 'const_cast', r'\((char\s?\*+\s?)\)\s*"', error):
3220 pass
3221 else:
3222 # Check pointer casts for other than string constants
3223 CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3224 'reinterpret_cast', r'\((\w+\s?\*+\s?)\)', error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003225
3226 # In addition, we look for people taking the address of a cast. This
3227 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
3228 # point where you think.
3229 if Search(
3230 r'(&\([^)]+\)[\w(])|(&(static|dynamic|reinterpret)_cast\b)', line):
3231 error(filename, linenum, 'runtime/casting', 4,
3232 ('Are you taking an address of a cast? '
3233 'This is dangerous: could be a temp var. '
3234 'Take the address before doing the cast, rather than after'))
3235
3236 # Check for people declaring static/global STL strings at the top level.
3237 # This is dangerous because the C++ language does not guarantee that
3238 # globals with constructors are initialized before the first access.
3239 match = Match(
3240 r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
3241 line)
3242 # Make sure it's not a function.
3243 # Function template specialization looks like: "string foo<Type>(...".
3244 # Class template definitions look like: "string Foo<Type>::Method(...".
3245 if match and not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)?\s*\(([^"]|$)',
3246 match.group(3)):
3247 error(filename, linenum, 'runtime/string', 4,
3248 'For a static/global string constant, use a C style string instead: '
3249 '"%schar %s[]".' %
3250 (match.group(1), match.group(2)))
3251
3252 # Check that we're not using RTTI outside of testing code.
3253 if Search(r'\bdynamic_cast<', line) and not _IsTestFilename(filename):
3254 error(filename, linenum, 'runtime/rtti', 5,
3255 'Do not use dynamic_cast<>. If you need to cast within a class '
3256 "hierarchy, use static_cast<> to upcast. Google doesn't support "
3257 'RTTI.')
3258
3259 if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
3260 error(filename, linenum, 'runtime/init', 4,
3261 'You seem to be initializing a member variable with itself.')
3262
3263 if file_extension == 'h':
3264 # TODO(unknown): check that 1-arg constructors are explicit.
3265 # How to tell it's a constructor?
3266 # (handled in CheckForNonStandardConstructs for now)
3267 # TODO(unknown): check that classes have DISALLOW_EVIL_CONSTRUCTORS
3268 # (level 1 error)
3269 pass
3270
3271 # Check if people are using the verboten C basic types. The only exception
3272 # we regularly allow is "unsigned short port" for port.
3273 if Search(r'\bshort port\b', line):
3274 if not Search(r'\bunsigned short port\b', line):
3275 error(filename, linenum, 'runtime/int', 4,
3276 'Use "unsigned short" for ports, not "short"')
3277 else:
3278 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
3279 if match:
3280 error(filename, linenum, 'runtime/int', 4,
3281 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
3282
3283 # When snprintf is used, the second argument shouldn't be a literal.
3284 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
erg+personal@google.com05189642010-04-30 20:43:03 +00003285 if match and match.group(2) != '0':
3286 # If 2nd arg is zero, snprintf is used to calculate size.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003287 error(filename, linenum, 'runtime/printf', 3,
3288 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
3289 'to snprintf.' % (match.group(1), match.group(2)))
3290
3291 # Check if some verboten C functions are being used.
3292 if Search(r'\bsprintf\b', line):
3293 error(filename, linenum, 'runtime/printf', 5,
3294 'Never use sprintf. Use snprintf instead.')
3295 match = Search(r'\b(strcpy|strcat)\b', line)
3296 if match:
3297 error(filename, linenum, 'runtime/printf', 4,
3298 'Almost always, snprintf is better than %s' % match.group(1))
3299
3300 if Search(r'\bsscanf\b', line):
3301 error(filename, linenum, 'runtime/printf', 1,
3302 'sscanf can be ok, but is slow and can overflow buffers.')
3303
erg@google.coma868d2d2009-10-09 21:18:45 +00003304 # Check if some verboten operator overloading is going on
3305 # TODO(unknown): catch out-of-line unary operator&:
3306 # class X {};
3307 # int operator&(const X& x) { return 42; } // unary operator&
3308 # The trick is it's hard to tell apart from binary operator&:
3309 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
3310 if Search(r'\boperator\s*&\s*\(\s*\)', line):
3311 error(filename, linenum, 'runtime/operator', 4,
3312 'Unary operator& is dangerous. Do not use it.')
3313
erg@google.com4e00b9a2009-01-12 23:05:11 +00003314 # Check for suspicious usage of "if" like
3315 # } if (a == b) {
3316 if Search(r'\}\s*if\s*\(', line):
3317 error(filename, linenum, 'readability/braces', 4,
3318 'Did you mean "else if"? If not, start a new line for "if".')
3319
3320 # Check for potential format string bugs like printf(foo).
3321 # We constrain the pattern not to pick things like DocidForPrintf(foo).
3322 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
erg@google.com8a95ecc2011-09-08 00:45:54 +00003323 # TODO(sugawarayu): Catch the following case. Need to change the calling
3324 # convention of the whole function to process multiple line to handle it.
3325 # printf(
3326 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
3327 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
3328 if printf_args:
3329 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00003330 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00003331 function_name = re.search(r'\b((?:string)?printf)\s*\(',
3332 line, re.I).group(1)
3333 error(filename, linenum, 'runtime/printf', 4,
3334 'Potential format string bug. Do %s("%%s", %s) instead.'
3335 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003336
3337 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
3338 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
3339 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
3340 error(filename, linenum, 'runtime/memset', 4,
3341 'Did you mean "memset(%s, 0, %s)"?'
3342 % (match.group(1), match.group(2)))
3343
3344 if Search(r'\busing namespace\b', line):
3345 error(filename, linenum, 'build/namespaces', 5,
3346 'Do not use namespace using-directives. '
3347 'Use using-declarations instead.')
3348
3349 # Detect variable-length arrays.
3350 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
3351 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
3352 match.group(3).find(']') == -1):
3353 # Split the size using space and arithmetic operators as delimiters.
3354 # If any of the resulting tokens are not compile time constants then
3355 # report the error.
3356 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
3357 is_const = True
3358 skip_next = False
3359 for tok in tokens:
3360 if skip_next:
3361 skip_next = False
3362 continue
3363
3364 if Search(r'sizeof\(.+\)', tok): continue
3365 if Search(r'arraysize\(\w+\)', tok): continue
3366
3367 tok = tok.lstrip('(')
3368 tok = tok.rstrip(')')
3369 if not tok: continue
3370 if Match(r'\d+', tok): continue
3371 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
3372 if Match(r'k[A-Z0-9]\w*', tok): continue
3373 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
3374 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
3375 # A catch all for tricky sizeof cases, including 'sizeof expression',
3376 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00003377 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003378 if tok.startswith('sizeof'):
3379 skip_next = True
3380 continue
3381 is_const = False
3382 break
3383 if not is_const:
3384 error(filename, linenum, 'runtime/arrays', 1,
3385 'Do not use variable-length arrays. Use an appropriately named '
3386 "('k' followed by CamelCase) compile-time constant for the size.")
3387
3388 # If DISALLOW_EVIL_CONSTRUCTORS, DISALLOW_COPY_AND_ASSIGN, or
3389 # DISALLOW_IMPLICIT_CONSTRUCTORS is present, then it should be the last thing
3390 # in the class declaration.
3391 match = Match(
3392 (r'\s*'
3393 r'(DISALLOW_(EVIL_CONSTRUCTORS|COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))'
3394 r'\(.*\);$'),
3395 line)
3396 if match and linenum + 1 < clean_lines.NumLines():
3397 next_line = clean_lines.elided[linenum + 1]
erg@google.com8a95ecc2011-09-08 00:45:54 +00003398 # We allow some, but not all, declarations of variables to be present
3399 # in the statement that defines the class. The [\w\*,\s]* fragment of
3400 # the regular expression below allows users to declare instances of
3401 # the class or pointers to instances, but not less common types such
3402 # as function pointers or arrays. It's a tradeoff between allowing
3403 # reasonable code and avoiding trying to parse more C++ using regexps.
3404 if not Search(r'^\s*}[\w\*,\s]*;', next_line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003405 error(filename, linenum, 'readability/constructors', 3,
3406 match.group(1) + ' should be the last thing in the class')
3407
3408 # Check for use of unnamed namespaces in header files. Registration
3409 # macros are typically OK, so we allow use of "namespace {" on lines
3410 # that end with backslashes.
3411 if (file_extension == 'h'
3412 and Search(r'\bnamespace\s*{', line)
3413 and line[-1] != '\\'):
3414 error(filename, linenum, 'build/namespaces', 4,
3415 'Do not use unnamed namespaces in header files. See '
3416 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
3417 ' for more information.')
3418
3419
3420def CheckCStyleCast(filename, linenum, line, raw_line, cast_type, pattern,
3421 error):
3422 """Checks for a C-style cast by looking for the pattern.
3423
3424 This also handles sizeof(type) warnings, due to similarity of content.
3425
3426 Args:
3427 filename: The name of the current file.
3428 linenum: The number of the line to check.
3429 line: The line of code to check.
3430 raw_line: The raw line of code to check, with comments.
3431 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00003432 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003433 pattern: The regular expression used to find C-style casts.
3434 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003435
3436 Returns:
3437 True if an error was emitted.
3438 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003439 """
3440 match = Search(pattern, line)
3441 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00003442 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00003443
3444 # e.g., sizeof(int)
3445 sizeof_match = Match(r'.*sizeof\s*$', line[0:match.start(1) - 1])
3446 if sizeof_match:
3447 error(filename, linenum, 'runtime/sizeof', 1,
3448 'Using sizeof(type). Use sizeof(varname) instead if possible')
erg@google.com8a95ecc2011-09-08 00:45:54 +00003449 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +00003450
erg@google.comd350fe52013-01-14 17:51:48 +00003451 # operator++(int) and operator--(int)
3452 if (line[0:match.start(1) - 1].endswith(' operator++') or
3453 line[0:match.start(1) - 1].endswith(' operator--')):
3454 return False
3455
erg@google.com4e00b9a2009-01-12 23:05:11 +00003456 remainder = line[match.end(0):]
3457
3458 # The close paren is for function pointers as arguments to a function.
3459 # eg, void foo(void (*bar)(int));
3460 # The semicolon check is a more basic function check; also possibly a
3461 # function pointer typedef.
3462 # eg, void foo(int); or void foo(int) const;
3463 # The equals check is for function pointer assignment.
3464 # eg, void *(*foo)(int) = ...
erg@google.comd7d27472011-09-07 17:36:35 +00003465 # The > is for MockCallback<...> ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00003466 #
3467 # Right now, this will only catch cases where there's a single argument, and
3468 # it's unnamed. It should probably be expanded to check for multiple
3469 # arguments with some unnamed.
erg@google.comd7d27472011-09-07 17:36:35 +00003470 function_match = Match(r'\s*(\)|=|(const)?\s*(;|\{|throw\(\)|>))', remainder)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003471 if function_match:
3472 if (not function_match.group(3) or
3473 function_match.group(3) == ';' or
erg@google.comd7d27472011-09-07 17:36:35 +00003474 ('MockCallback<' not in raw_line and
3475 '/*' not in raw_line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003476 error(filename, linenum, 'readability/function', 3,
3477 'All parameters should be named in a function')
erg@google.com8a95ecc2011-09-08 00:45:54 +00003478 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +00003479
3480 # At this point, all that should be left is actual casts.
3481 error(filename, linenum, 'readability/casting', 4,
3482 'Using C-style cast. Use %s<%s>(...) instead' %
3483 (cast_type, match.group(1)))
3484
erg@google.com8a95ecc2011-09-08 00:45:54 +00003485 return True
3486
erg@google.com4e00b9a2009-01-12 23:05:11 +00003487
3488_HEADERS_CONTAINING_TEMPLATES = (
3489 ('<deque>', ('deque',)),
3490 ('<functional>', ('unary_function', 'binary_function',
3491 'plus', 'minus', 'multiplies', 'divides', 'modulus',
3492 'negate',
3493 'equal_to', 'not_equal_to', 'greater', 'less',
3494 'greater_equal', 'less_equal',
3495 'logical_and', 'logical_or', 'logical_not',
3496 'unary_negate', 'not1', 'binary_negate', 'not2',
3497 'bind1st', 'bind2nd',
3498 'pointer_to_unary_function',
3499 'pointer_to_binary_function',
3500 'ptr_fun',
3501 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
3502 'mem_fun_ref_t',
3503 'const_mem_fun_t', 'const_mem_fun1_t',
3504 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
3505 'mem_fun_ref',
3506 )),
3507 ('<limits>', ('numeric_limits',)),
3508 ('<list>', ('list',)),
3509 ('<map>', ('map', 'multimap',)),
3510 ('<memory>', ('allocator',)),
3511 ('<queue>', ('queue', 'priority_queue',)),
3512 ('<set>', ('set', 'multiset',)),
3513 ('<stack>', ('stack',)),
3514 ('<string>', ('char_traits', 'basic_string',)),
3515 ('<utility>', ('pair',)),
3516 ('<vector>', ('vector',)),
3517
3518 # gcc extensions.
3519 # Note: std::hash is their hash, ::hash is our hash
3520 ('<hash_map>', ('hash_map', 'hash_multimap',)),
3521 ('<hash_set>', ('hash_set', 'hash_multiset',)),
3522 ('<slist>', ('slist',)),
3523 )
3524
erg@google.com4e00b9a2009-01-12 23:05:11 +00003525_RE_PATTERN_STRING = re.compile(r'\bstring\b')
3526
3527_re_pattern_algorithm_header = []
erg@google.coma87abb82009-02-24 01:41:01 +00003528for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
3529 'transform'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003530 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
3531 # type::max().
3532 _re_pattern_algorithm_header.append(
3533 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
3534 _template,
3535 '<algorithm>'))
3536
3537_re_pattern_templates = []
3538for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
3539 for _template in _templates:
3540 _re_pattern_templates.append(
3541 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
3542 _template + '<>',
3543 _header))
3544
3545
erg@google.come35f7652009-06-19 20:52:09 +00003546def FilesBelongToSameModule(filename_cc, filename_h):
3547 """Check if these two filenames belong to the same module.
3548
3549 The concept of a 'module' here is a as follows:
3550 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
3551 same 'module' if they are in the same directory.
3552 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
3553 to belong to the same module here.
3554
3555 If the filename_cc contains a longer path than the filename_h, for example,
3556 '/absolute/path/to/base/sysinfo.cc', and this file would include
3557 'base/sysinfo.h', this function also produces the prefix needed to open the
3558 header. This is used by the caller of this function to more robustly open the
3559 header file. We don't have access to the real include paths in this context,
3560 so we need this guesswork here.
3561
3562 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
3563 according to this implementation. Because of this, this function gives
3564 some false positives. This should be sufficiently rare in practice.
3565
3566 Args:
3567 filename_cc: is the path for the .cc file
3568 filename_h: is the path for the header path
3569
3570 Returns:
3571 Tuple with a bool and a string:
3572 bool: True if filename_cc and filename_h belong to the same module.
3573 string: the additional prefix needed to open the header file.
3574 """
3575
3576 if not filename_cc.endswith('.cc'):
3577 return (False, '')
3578 filename_cc = filename_cc[:-len('.cc')]
3579 if filename_cc.endswith('_unittest'):
3580 filename_cc = filename_cc[:-len('_unittest')]
3581 elif filename_cc.endswith('_test'):
3582 filename_cc = filename_cc[:-len('_test')]
3583 filename_cc = filename_cc.replace('/public/', '/')
3584 filename_cc = filename_cc.replace('/internal/', '/')
3585
3586 if not filename_h.endswith('.h'):
3587 return (False, '')
3588 filename_h = filename_h[:-len('.h')]
3589 if filename_h.endswith('-inl'):
3590 filename_h = filename_h[:-len('-inl')]
3591 filename_h = filename_h.replace('/public/', '/')
3592 filename_h = filename_h.replace('/internal/', '/')
3593
3594 files_belong_to_same_module = filename_cc.endswith(filename_h)
3595 common_path = ''
3596 if files_belong_to_same_module:
3597 common_path = filename_cc[:-len(filename_h)]
3598 return files_belong_to_same_module, common_path
3599
3600
3601def UpdateIncludeState(filename, include_state, io=codecs):
3602 """Fill up the include_state with new includes found from the file.
3603
3604 Args:
3605 filename: the name of the header to read.
3606 include_state: an _IncludeState instance in which the headers are inserted.
3607 io: The io factory to use to read the file. Provided for testability.
3608
3609 Returns:
3610 True if a header was succesfully added. False otherwise.
3611 """
3612 headerfile = None
3613 try:
3614 headerfile = io.open(filename, 'r', 'utf8', 'replace')
3615 except IOError:
3616 return False
3617 linenum = 0
3618 for line in headerfile:
3619 linenum += 1
3620 clean_line = CleanseComments(line)
3621 match = _RE_PATTERN_INCLUDE.search(clean_line)
3622 if match:
3623 include = match.group(2)
3624 # The value formatting is cute, but not really used right now.
3625 # What matters here is that the key is in include_state.
3626 include_state.setdefault(include, '%s:%d' % (filename, linenum))
3627 return True
3628
3629
3630def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
3631 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003632 """Reports for missing stl includes.
3633
3634 This function will output warnings to make sure you are including the headers
3635 necessary for the stl containers and functions that you use. We only give one
3636 reason to include a header. For example, if you use both equal_to<> and
3637 less<> in a .h file, only one (the latter in the file) of these will be
3638 reported as a reason to include the <functional>.
3639
erg@google.com4e00b9a2009-01-12 23:05:11 +00003640 Args:
3641 filename: The name of the current file.
3642 clean_lines: A CleansedLines instance containing the file.
3643 include_state: An _IncludeState instance.
3644 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00003645 io: The IO factory to use to read the header file. Provided for unittest
3646 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003647 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00003648 required = {} # A map of header name to linenumber and the template entity.
3649 # Example of required: { '<functional>': (1219, 'less<>') }
3650
3651 for linenum in xrange(clean_lines.NumLines()):
3652 line = clean_lines.elided[linenum]
3653 if not line or line[0] == '#':
3654 continue
3655
3656 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003657 matched = _RE_PATTERN_STRING.search(line)
3658 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00003659 # Don't warn about strings in non-STL namespaces:
3660 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00003661 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00003662 if prefix.endswith('std::') or not prefix.endswith('::'):
3663 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003664
3665 for pattern, template, header in _re_pattern_algorithm_header:
3666 if pattern.search(line):
3667 required[header] = (linenum, template)
3668
3669 # The following function is just a speed up, no semantics are changed.
3670 if not '<' in line: # Reduces the cpu time usage by skipping lines.
3671 continue
3672
3673 for pattern, template, header in _re_pattern_templates:
3674 if pattern.search(line):
3675 required[header] = (linenum, template)
3676
erg@google.come35f7652009-06-19 20:52:09 +00003677 # The policy is that if you #include something in foo.h you don't need to
3678 # include it again in foo.cc. Here, we will look at possible includes.
3679 # Let's copy the include_state so it is only messed up within this function.
3680 include_state = include_state.copy()
3681
3682 # Did we find the header for this file (if any) and succesfully load it?
3683 header_found = False
3684
3685 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00003686 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00003687
3688 # For Emacs's flymake.
3689 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
3690 # by flymake and that file name might end with '_flymake.cc'. In that case,
3691 # restore original file name here so that the corresponding header file can be
3692 # found.
3693 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
3694 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00003695 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00003696
3697 # include_state is modified during iteration, so we iterate over a copy of
3698 # the keys.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003699 header_keys = include_state.keys()
3700 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00003701 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
3702 fullpath = common_path + header
3703 if same_module and UpdateIncludeState(fullpath, include_state, io):
3704 header_found = True
3705
3706 # If we can't find the header file for a .cc, assume it's because we don't
3707 # know where to look. In that case we'll give up as we're not sure they
3708 # didn't include it in the .h file.
3709 # TODO(unknown): Do a better job of finding .h files so we are confident that
3710 # not having the .h file means there isn't one.
3711 if filename.endswith('.cc') and not header_found:
3712 return
3713
erg@google.com4e00b9a2009-01-12 23:05:11 +00003714 # All the lines have been processed, report the errors found.
3715 for required_header_unstripped in required:
3716 template = required[required_header_unstripped][1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003717 if required_header_unstripped.strip('<>"') not in include_state:
3718 error(filename, required[required_header_unstripped][0],
3719 'build/include_what_you_use', 4,
3720 'Add #include ' + required_header_unstripped + ' for ' + template)
3721
3722
erg@google.com8a95ecc2011-09-08 00:45:54 +00003723_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
3724
3725
3726def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
3727 """Check that make_pair's template arguments are deduced.
3728
3729 G++ 4.6 in C++0x mode fails badly if make_pair's template arguments are
3730 specified explicitly, and such use isn't intended in any case.
3731
3732 Args:
3733 filename: The name of the current file.
3734 clean_lines: A CleansedLines instance containing the file.
3735 linenum: The number of the line to check.
3736 error: The function to call with any errors found.
3737 """
3738 raw = clean_lines.raw_lines
3739 line = raw[linenum]
3740 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
3741 if match:
3742 error(filename, linenum, 'build/explicit_make_pair',
3743 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00003744 'For C++11-compatibility, omit template arguments from make_pair'
3745 ' OR use pair directly OR if appropriate, construct a pair directly')
erg@google.com8a95ecc2011-09-08 00:45:54 +00003746
3747
erg@google.comd350fe52013-01-14 17:51:48 +00003748def ProcessLine(filename, file_extension, clean_lines, line,
3749 include_state, function_state, nesting_state, error,
3750 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003751 """Processes a single line in the file.
3752
3753 Args:
3754 filename: Filename of the file that is being processed.
3755 file_extension: The extension (dot not included) of the file.
3756 clean_lines: An array of strings, each representing a line of the file,
3757 with comments stripped.
3758 line: Number of line being processed.
3759 include_state: An _IncludeState instance in which the headers are inserted.
3760 function_state: A _FunctionState instance which counts function lines, etc.
erg@google.comd350fe52013-01-14 17:51:48 +00003761 nesting_state: A _NestingState instance which maintains information about
3762 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003763 error: A callable to which errors are reported, which takes 4 arguments:
3764 filename, line number, error level, and message
erg@google.comefeacdf2011-09-07 21:12:16 +00003765 extra_check_functions: An array of additional check functions that will be
3766 run on each source line. Each function takes 4
3767 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00003768 """
3769 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00003770 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00003771 nesting_state.Update(filename, clean_lines, line, error)
3772 if nesting_state.stack and nesting_state.stack[-1].inline_asm != _NO_ASM:
3773 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00003774 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003775 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00003776 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003777 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
3778 error)
3779 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00003780 nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003781 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00003782 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00003783 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
erg@google.comefeacdf2011-09-07 21:12:16 +00003784 for check_fn in extra_check_functions:
3785 check_fn(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003786
erg@google.comefeacdf2011-09-07 21:12:16 +00003787def ProcessFileData(filename, file_extension, lines, error,
3788 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003789 """Performs lint checks and reports any errors to the given error function.
3790
3791 Args:
3792 filename: Filename of the file that is being processed.
3793 file_extension: The extension (dot not included) of the file.
3794 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00003795 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003796 error: A callable to which errors are reported, which takes 4 arguments:
erg@google.comefeacdf2011-09-07 21:12:16 +00003797 filename, line number, error level, and message
3798 extra_check_functions: An array of additional check functions that will be
3799 run on each source line. Each function takes 4
3800 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00003801 """
3802 lines = (['// marker so line numbers and indices both start at 1'] + lines +
3803 ['// marker so line numbers end in a known way'])
3804
3805 include_state = _IncludeState()
3806 function_state = _FunctionState()
erg@google.comd350fe52013-01-14 17:51:48 +00003807 nesting_state = _NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00003808
erg+personal@google.com05189642010-04-30 20:43:03 +00003809 ResetNolintSuppressions()
3810
erg@google.com4e00b9a2009-01-12 23:05:11 +00003811 CheckForCopyright(filename, lines, error)
3812
3813 if file_extension == 'h':
3814 CheckForHeaderGuard(filename, lines, error)
3815
3816 RemoveMultiLineComments(filename, lines, error)
3817 clean_lines = CleansedLines(lines)
3818 for line in xrange(clean_lines.NumLines()):
3819 ProcessLine(filename, file_extension, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00003820 include_state, function_state, nesting_state, error,
erg@google.comefeacdf2011-09-07 21:12:16 +00003821 extra_check_functions)
erg@google.comd350fe52013-01-14 17:51:48 +00003822 nesting_state.CheckClassFinished(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003823
3824 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
3825
3826 # We check here rather than inside ProcessLine so that we see raw
3827 # lines rather than "cleaned" lines.
3828 CheckForUnicodeReplacementCharacters(filename, lines, error)
3829
3830 CheckForNewlineAtEOF(filename, lines, error)
3831
erg@google.comefeacdf2011-09-07 21:12:16 +00003832def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003833 """Does google-lint on a single file.
3834
3835 Args:
3836 filename: The name of the file to parse.
3837
3838 vlevel: The level of errors to report. Every error of confidence
3839 >= verbose_level will be reported. 0 is a good default.
erg@google.comefeacdf2011-09-07 21:12:16 +00003840
3841 extra_check_functions: An array of additional check functions that will be
3842 run on each source line. Each function takes 4
3843 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00003844 """
3845
3846 _SetVerboseLevel(vlevel)
3847
3848 try:
3849 # Support the UNIX convention of using "-" for stdin. Note that
3850 # we are not opening the file with universal newline support
3851 # (which codecs doesn't support anyway), so the resulting lines do
3852 # contain trailing '\r' characters if we are reading a file that
3853 # has CRLF endings.
3854 # If after the split a trailing '\r' is present, it is removed
3855 # below. If it is not expected to be present (i.e. os.linesep !=
3856 # '\r\n' as in Windows), a warning is issued below if this file
3857 # is processed.
3858
3859 if filename == '-':
3860 lines = codecs.StreamReaderWriter(sys.stdin,
3861 codecs.getreader('utf8'),
3862 codecs.getwriter('utf8'),
3863 'replace').read().split('\n')
3864 else:
3865 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
3866
3867 carriage_return_found = False
3868 # Remove trailing '\r'.
3869 for linenum in range(len(lines)):
3870 if lines[linenum].endswith('\r'):
3871 lines[linenum] = lines[linenum].rstrip('\r')
3872 carriage_return_found = True
3873
3874 except IOError:
3875 sys.stderr.write(
3876 "Skipping input '%s': Can't open for reading\n" % filename)
3877 return
3878
3879 # Note, if no dot is found, this will give the entire filename as the ext.
3880 file_extension = filename[filename.rfind('.') + 1:]
3881
3882 # When reading from stdin, the extension is unknown, so no cpplint tests
3883 # should rely on the extension.
3884 if (filename != '-' and file_extension != 'cc' and file_extension != 'h'
3885 and file_extension != 'cpp'):
3886 sys.stderr.write('Ignoring %s; not a .cc or .h file\n' % filename)
3887 else:
erg@google.comefeacdf2011-09-07 21:12:16 +00003888 ProcessFileData(filename, file_extension, lines, Error,
3889 extra_check_functions)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003890 if carriage_return_found and os.linesep != '\r\n':
erg@google.com8a95ecc2011-09-08 00:45:54 +00003891 # Use 0 for linenum since outputting only one error for potentially
erg@google.com4e00b9a2009-01-12 23:05:11 +00003892 # several lines.
3893 Error(filename, 0, 'whitespace/newline', 1,
3894 'One or more unexpected \\r (^M) found;'
3895 'better to use only a \\n')
3896
3897 sys.stderr.write('Done processing %s\n' % filename)
3898
3899
3900def PrintUsage(message):
3901 """Prints a brief usage string and exits, optionally with an error message.
3902
3903 Args:
3904 message: The optional error message.
3905 """
3906 sys.stderr.write(_USAGE)
3907 if message:
3908 sys.exit('\nFATAL ERROR: ' + message)
3909 else:
3910 sys.exit(1)
3911
3912
3913def PrintCategories():
3914 """Prints a list of all the error-categories used by error messages.
3915
3916 These are the categories used to filter messages via --filter.
3917 """
erg+personal@google.com05189642010-04-30 20:43:03 +00003918 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003919 sys.exit(0)
3920
3921
3922def ParseArguments(args):
3923 """Parses the command line arguments.
3924
3925 This may set the output format and verbosity level as side-effects.
3926
3927 Args:
3928 args: The command line arguments:
3929
3930 Returns:
3931 The list of filenames to lint.
3932 """
3933 try:
3934 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00003935 'counting=',
erg@google.com4e00b9a2009-01-12 23:05:11 +00003936 'filter='])
3937 except getopt.GetoptError:
3938 PrintUsage('Invalid arguments.')
3939
3940 verbosity = _VerboseLevel()
3941 output_format = _OutputFormat()
3942 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00003943 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00003944
3945 for (opt, val) in opts:
3946 if opt == '--help':
3947 PrintUsage(None)
3948 elif opt == '--output':
3949 if not val in ('emacs', 'vs7'):
3950 PrintUsage('The only allowed output formats are emacs and vs7.')
3951 output_format = val
3952 elif opt == '--verbose':
3953 verbosity = int(val)
3954 elif opt == '--filter':
3955 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00003956 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003957 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00003958 elif opt == '--counting':
3959 if val not in ('total', 'toplevel', 'detailed'):
3960 PrintUsage('Valid counting options are total, toplevel, and detailed')
3961 counting_style = val
erg@google.com4e00b9a2009-01-12 23:05:11 +00003962
3963 if not filenames:
3964 PrintUsage('No files were specified.')
3965
3966 _SetOutputFormat(output_format)
3967 _SetVerboseLevel(verbosity)
3968 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00003969 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003970
3971 return filenames
3972
3973
3974def main():
3975 filenames = ParseArguments(sys.argv[1:])
3976
3977 # Change stderr to write with replacement characters so we don't die
3978 # if we try to print something containing non-ASCII characters.
3979 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
3980 codecs.getreader('utf8'),
3981 codecs.getwriter('utf8'),
3982 'replace')
3983
erg@google.coma868d2d2009-10-09 21:18:45 +00003984 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00003985 for filename in filenames:
3986 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00003987 _cpplint_state.PrintErrorCounts()
3988
erg@google.com4e00b9a2009-01-12 23:05:11 +00003989 sys.exit(_cpplint_state.error_count > 0)
3990
3991
3992if __name__ == '__main__':
3993 main()