blob: 84cc1cd525273166860b3b24c4e2452818c0ffc3 [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
erg@google.com4e00b9a2009-01-12 23:05:11 +000031"""Does google-lint on c++ files.
32
33The goal of this script is to identify places in the code that *may*
34be in non-compliance with google style. It does not attempt to fix
35up these problems -- the point is to educate. It does also not
36attempt to find all problems, or to ensure that everything it does
37find is legitimately a problem.
38
39In particular, we can get very confused by /* and // inside strings!
40We do a small hack, which is to ignore //'s with "'s after them on the
41same line, but it is far from perfect (in either direction).
42"""
43
44import codecs
erg@google.comd350fe52013-01-14 17:51:48 +000045import copy
erg@google.com4e00b9a2009-01-12 23:05:11 +000046import getopt
47import math # for log
48import os
49import re
50import sre_compile
51import string
52import sys
53import unicodedata
54
55
56_USAGE = """
57Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
erg@google.coma868d2d2009-10-09 21:18:45 +000058 [--counting=total|toplevel|detailed]
erg@google.com4e00b9a2009-01-12 23:05:11 +000059 <file> [file] ...
60
61 The style guidelines this tries to follow are those in
62 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
63
64 Every problem is given a confidence score from 1-5, with 5 meaning we are
65 certain of the problem, and 1 meaning it could be a legitimate construct.
66 This will miss some errors, and is not a substitute for a code review.
67
erg+personal@google.com05189642010-04-30 20:43:03 +000068 To suppress false-positive errors of a certain category, add a
69 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
70 suppresses errors of all categories on that line.
erg@google.com4e00b9a2009-01-12 23:05:11 +000071
72 The files passed in will be linted; at least one file must be provided.
73 Linted extensions are .cc, .cpp, and .h. Other file types will be ignored.
74
75 Flags:
76
77 output=vs7
78 By default, the output is formatted to ease emacs parsing. Visual Studio
79 compatible output (vs7) may also be used. Other formats are unsupported.
80
81 verbose=#
82 Specify a number 0-5 to restrict errors to certain verbosity levels.
83
84 filter=-x,+y,...
85 Specify a comma-separated list of category-filters to apply: only
86 error messages whose category names pass the filters will be printed.
87 (Category names are printed with the message and look like
88 "[whitespace/indent]".) Filters are evaluated left to right.
89 "-FOO" and "FOO" means "do not print categories that start with FOO".
90 "+FOO" means "do print categories that start with FOO".
91
92 Examples: --filter=-whitespace,+whitespace/braces
93 --filter=whitespace,runtime/printf,+runtime/printf_format
94 --filter=-,+build/include_what_you_use
95
96 To see a list of all the categories used in cpplint, pass no arg:
97 --filter=
erg@google.coma868d2d2009-10-09 21:18:45 +000098
99 counting=total|toplevel|detailed
100 The total number of errors found is always printed. If
101 'toplevel' is provided, then the count of errors in each of
102 the top-level categories like 'build' and 'whitespace' will
103 also be printed. If 'detailed' is provided, then a count
104 is provided for each category like 'build/class'.
erg@google.com4d70a882013-04-16 21:06:32 +0000105
106 root=subdir
107 The root directory used for deriving header guard CPP variable.
108 By default, the header guard CPP variable is calculated as the relative
109 path to the directory that contains .git, .hg, or .svn. When this flag
110 is specified, the relative path is calculated from the specified
111 directory. If the specified directory does not exist, this flag is
112 ignored.
113
114 Examples:
115 Assuing that src/.git exists, the header guard CPP variables for
116 src/chrome/browser/ui/browser.h are:
117
118 No flag => CHROME_BROWSER_UI_BROWSER_H_
119 --root=chrome => BROWSER_UI_BROWSER_H_
120 --root=chrome/browser => UI_BROWSER_H_
erg@google.com4e00b9a2009-01-12 23:05:11 +0000121"""
122
123# We categorize each error message we print. Here are the categories.
124# We want an explicit list so we can list them all in cpplint --filter=.
125# If you add a new error message with a new category, add it to the list
126# here! cpplint_unittest.py should tell you if you forget to do this.
erg+personal@google.com05189642010-04-30 20:43:03 +0000127_ERROR_CATEGORIES = [
128 'build/class',
129 'build/deprecated',
130 'build/endif_comment',
erg@google.com8a95ecc2011-09-08 00:45:54 +0000131 'build/explicit_make_pair',
erg+personal@google.com05189642010-04-30 20:43:03 +0000132 'build/forward_decl',
133 'build/header_guard',
134 'build/include',
135 'build/include_alpha',
136 'build/include_order',
137 'build/include_what_you_use',
138 'build/namespaces',
139 'build/printf_format',
140 'build/storage_class',
141 'legal/copyright',
erg@google.comd350fe52013-01-14 17:51:48 +0000142 'readability/alt_tokens',
erg+personal@google.com05189642010-04-30 20:43:03 +0000143 'readability/braces',
144 'readability/casting',
145 'readability/check',
146 'readability/constructors',
147 'readability/fn_size',
148 'readability/function',
149 'readability/multiline_comment',
150 'readability/multiline_string',
erg@google.comd350fe52013-01-14 17:51:48 +0000151 'readability/namespace',
erg+personal@google.com05189642010-04-30 20:43:03 +0000152 'readability/nolint',
erg@google.com2aa59982013-10-28 19:09:25 +0000153 'readability/nul',
erg+personal@google.com05189642010-04-30 20:43:03 +0000154 'readability/streams',
155 'readability/todo',
156 'readability/utf8',
157 'runtime/arrays',
158 'runtime/casting',
159 'runtime/explicit',
160 'runtime/int',
161 'runtime/init',
162 'runtime/invalid_increment',
163 'runtime/member_string_references',
164 'runtime/memset',
165 'runtime/operator',
166 'runtime/printf',
167 'runtime/printf_format',
168 'runtime/references',
erg+personal@google.com05189642010-04-30 20:43:03 +0000169 'runtime/string',
170 'runtime/threadsafe_fn',
erg@google.com2aa59982013-10-28 19:09:25 +0000171 'runtime/vlog',
172 'whitespace/blank_line',
173 'whitespace/braces',
174 'whitespace/comma',
175 'whitespace/comments',
176 'whitespace/empty_conditional_body',
177 'whitespace/empty_loop_body',
178 'whitespace/end_of_line',
179 'whitespace/ending_newline',
180 'whitespace/forcolon',
erg+personal@google.com05189642010-04-30 20:43:03 +0000181 'whitespace/indent',
erg+personal@google.com05189642010-04-30 20:43:03 +0000182 'whitespace/line_length',
183 'whitespace/newline',
184 'whitespace/operators',
185 'whitespace/parens',
186 'whitespace/semicolon',
187 'whitespace/tab',
188 'whitespace/todo'
189 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000190
erg@google.come35f7652009-06-19 20:52:09 +0000191# The default state of the category filter. This is overrided by the --filter=
192# flag. By default all errors are on, so only add here categories that should be
193# off by default (i.e., categories that must be enabled by the --filter= flags).
194# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000195_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000196
erg@google.com4e00b9a2009-01-12 23:05:11 +0000197# We used to check for high-bit characters, but after much discussion we
198# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000199# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000200
erg@google.com4e00b9a2009-01-12 23:05:11 +0000201
erg@google.comfd5da632013-10-25 17:39:45 +0000202# C++ headers
erg@google.com4e00b9a2009-01-12 23:05:11 +0000203_CPP_HEADERS = frozenset([
erg@google.comfd5da632013-10-25 17:39:45 +0000204 # Legacy
205 'algobase.h',
206 'algo.h',
207 'alloc.h',
208 'builtinbuf.h',
209 'bvector.h',
210 'complex.h',
211 'defalloc.h',
212 'deque.h',
213 'editbuf.h',
214 'fstream.h',
215 'function.h',
216 'hash_map',
217 'hash_map.h',
218 'hash_set',
219 'hash_set.h',
220 'hashtable.h',
221 'heap.h',
222 'indstream.h',
223 'iomanip.h',
224 'iostream.h',
225 'istream.h',
226 'iterator.h',
227 'list.h',
228 'map.h',
229 'multimap.h',
230 'multiset.h',
231 'ostream.h',
232 'pair.h',
233 'parsestream.h',
234 'pfstream.h',
235 'procbuf.h',
236 'pthread_alloc',
237 'pthread_alloc.h',
238 'rope',
239 'rope.h',
240 'ropeimpl.h',
241 'set.h',
242 'slist',
243 'slist.h',
244 'stack.h',
245 'stdiostream.h',
246 'stl_alloc.h',
247 'stl_relops.h',
248 'streambuf.h',
249 'stream.h',
250 'strfile.h',
251 'strstream.h',
252 'tempbuf.h',
253 'tree.h',
254 'type_traits.h',
255 'vector.h',
256 # 17.6.1.2 C++ library headers
257 'algorithm',
258 'array',
259 'atomic',
260 'bitset',
261 'chrono',
262 'codecvt',
263 'complex',
264 'condition_variable',
265 'deque',
266 'exception',
267 'forward_list',
268 'fstream',
269 'functional',
270 'future',
271 'initializer_list',
272 'iomanip',
273 'ios',
274 'iosfwd',
275 'iostream',
276 'istream',
277 'iterator',
278 'limits',
279 'list',
280 'locale',
281 'map',
282 'memory',
283 'mutex',
284 'new',
285 'numeric',
286 'ostream',
287 'queue',
288 'random',
289 'ratio',
290 'regex',
291 'set',
292 'sstream',
293 'stack',
294 'stdexcept',
295 'streambuf',
296 'string',
297 'strstream',
298 'system_error',
299 'thread',
300 'tuple',
301 'typeindex',
302 'typeinfo',
303 'type_traits',
304 'unordered_map',
305 'unordered_set',
306 'utility',
erg@google.com5d00c562013-07-12 19:57:05 +0000307 'valarray',
erg@google.comfd5da632013-10-25 17:39:45 +0000308 'vector',
309 # 17.6.1.2 C++ headers for C library facilities
310 'cassert',
311 'ccomplex',
312 'cctype',
313 'cerrno',
314 'cfenv',
315 'cfloat',
316 'cinttypes',
317 'ciso646',
318 'climits',
319 'clocale',
320 'cmath',
321 'csetjmp',
322 'csignal',
323 'cstdalign',
324 'cstdarg',
325 'cstdbool',
326 'cstddef',
327 'cstdint',
328 'cstdio',
329 'cstdlib',
330 'cstring',
331 'ctgmath',
332 'ctime',
333 'cuchar',
334 'cwchar',
335 'cwctype',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000336 ])
337
erg@google.com4e00b9a2009-01-12 23:05:11 +0000338# Assertion macros. These are defined in base/logging.h and
339# testing/base/gunit.h. Note that the _M versions need to come first
340# for substring matching to work.
341_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000342 'DCHECK', 'CHECK',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000343 'EXPECT_TRUE_M', 'EXPECT_TRUE',
344 'ASSERT_TRUE_M', 'ASSERT_TRUE',
345 'EXPECT_FALSE_M', 'EXPECT_FALSE',
346 'ASSERT_FALSE_M', 'ASSERT_FALSE',
347 ]
348
erg@google.come35f7652009-06-19 20:52:09 +0000349# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000350_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
351
352for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
353 ('>=', 'GE'), ('>', 'GT'),
354 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000355 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000356 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
357 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
358 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
359 _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
360 _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
361
362for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
363 ('>=', 'LT'), ('>', 'LE'),
364 ('<=', 'GT'), ('<', 'GE')]:
365 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
366 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
367 _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
368 _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
369
erg@google.comd350fe52013-01-14 17:51:48 +0000370# Alternative tokens and their replacements. For full list, see section 2.5
371# Alternative tokens [lex.digraph] in the C++ standard.
372#
373# Digraphs (such as '%:') are not included here since it's a mess to
374# match those on a word boundary.
375_ALT_TOKEN_REPLACEMENT = {
376 'and': '&&',
377 'bitor': '|',
378 'or': '||',
379 'xor': '^',
380 'compl': '~',
381 'bitand': '&',
382 'and_eq': '&=',
383 'or_eq': '|=',
384 'xor_eq': '^=',
385 'not': '!',
386 'not_eq': '!='
387 }
388
389# Compile regular expression that matches all the above keywords. The "[ =()]"
390# bit is meant to avoid matching these keywords outside of boolean expressions.
391#
erg@google.comc6671232013-10-25 21:44:03 +0000392# False positives include C-style multi-line comments and multi-line strings
393# but those have always been troublesome for cpplint.
erg@google.comd350fe52013-01-14 17:51:48 +0000394_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
395 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
396
erg@google.com4e00b9a2009-01-12 23:05:11 +0000397
398# These constants define types of headers for use with
399# _IncludeState.CheckNextIncludeOrder().
400_C_SYS_HEADER = 1
401_CPP_SYS_HEADER = 2
402_LIKELY_MY_HEADER = 3
403_POSSIBLE_MY_HEADER = 4
404_OTHER_HEADER = 5
405
erg@google.comd350fe52013-01-14 17:51:48 +0000406# These constants define the current inline assembly state
407_NO_ASM = 0 # Outside of inline assembly block
408_INSIDE_ASM = 1 # Inside inline assembly block
409_END_ASM = 2 # Last line of inline assembly block
410_BLOCK_ASM = 3 # The whole block is an inline assembly block
411
412# Match start of assembly blocks
413_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
414 r'(?:\s+(volatile|__volatile__))?'
415 r'\s*[{(]')
416
erg@google.com4e00b9a2009-01-12 23:05:11 +0000417
418_regexp_compile_cache = {}
419
erg+personal@google.com05189642010-04-30 20:43:03 +0000420# Finds occurrences of NOLINT or NOLINT(...).
421_RE_SUPPRESSION = re.compile(r'\bNOLINT\b(\([^)]*\))?')
422
423# {str, set(int)}: a map from error categories to sets of linenumbers
424# on which those errors are expected and should be suppressed.
425_error_suppressions = {}
426
erg@google.com4d70a882013-04-16 21:06:32 +0000427# The root directory used for deriving header guard CPP variable.
428# This is set by --root flag.
429_root = None
430
erg+personal@google.com05189642010-04-30 20:43:03 +0000431def ParseNolintSuppressions(filename, raw_line, linenum, error):
432 """Updates the global list of error-suppressions.
433
434 Parses any NOLINT comments on the current line, updating the global
435 error_suppressions store. Reports an error if the NOLINT comment
436 was malformed.
437
438 Args:
439 filename: str, the name of the input file.
440 raw_line: str, the line of input text, with comments.
441 linenum: int, the number of the current line.
442 error: function, an error handler.
443 """
444 # FIXME(adonovan): "NOLINT(" is misparsed as NOLINT(*).
erg@google.com8a95ecc2011-09-08 00:45:54 +0000445 matched = _RE_SUPPRESSION.search(raw_line)
446 if matched:
447 category = matched.group(1)
erg+personal@google.com05189642010-04-30 20:43:03 +0000448 if category in (None, '(*)'): # => "suppress all"
449 _error_suppressions.setdefault(None, set()).add(linenum)
450 else:
451 if category.startswith('(') and category.endswith(')'):
452 category = category[1:-1]
453 if category in _ERROR_CATEGORIES:
454 _error_suppressions.setdefault(category, set()).add(linenum)
455 else:
456 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000457 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000458
459
460def ResetNolintSuppressions():
461 "Resets the set of NOLINT suppressions to empty."
462 _error_suppressions.clear()
463
464
465def IsErrorSuppressedByNolint(category, linenum):
466 """Returns true if the specified error category is suppressed on this line.
467
468 Consults the global error_suppressions map populated by
469 ParseNolintSuppressions/ResetNolintSuppressions.
470
471 Args:
472 category: str, the category of the error.
473 linenum: int, the current line number.
474 Returns:
475 bool, True iff the error should be suppressed due to a NOLINT comment.
476 """
477 return (linenum in _error_suppressions.get(category, set()) or
478 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000479
480def Match(pattern, s):
481 """Matches the string with the pattern, caching the compiled regexp."""
482 # The regexp compilation caching is inlined in both Match and Search for
483 # performance reasons; factoring it out into a separate function turns out
484 # to be noticeably expensive.
erg@google.comc6671232013-10-25 21:44:03 +0000485 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000486 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
487 return _regexp_compile_cache[pattern].match(s)
488
489
erg@google.comfd5da632013-10-25 17:39:45 +0000490def ReplaceAll(pattern, rep, s):
491 """Replaces instances of pattern in a string with a replacement.
492
493 The compiled regex is kept in a cache shared by Match and Search.
494
495 Args:
496 pattern: regex pattern
497 rep: replacement text
498 s: search string
499
500 Returns:
501 string with replacements made (or original string if no replacements)
502 """
503 if pattern not in _regexp_compile_cache:
504 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
505 return _regexp_compile_cache[pattern].sub(rep, s)
506
507
erg@google.com4e00b9a2009-01-12 23:05:11 +0000508def Search(pattern, s):
509 """Searches the string for the pattern, caching the compiled regexp."""
erg@google.comc6671232013-10-25 21:44:03 +0000510 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000511 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
512 return _regexp_compile_cache[pattern].search(s)
513
514
515class _IncludeState(dict):
516 """Tracks line numbers for includes, and the order in which includes appear.
517
518 As a dict, an _IncludeState object serves as a mapping between include
519 filename and line number on which that file was included.
520
521 Call CheckNextIncludeOrder() once for each header in the file, passing
522 in the type constants defined above. Calls in an illegal order will
523 raise an _IncludeError with an appropriate error message.
524
525 """
526 # self._section will move monotonically through this set. If it ever
527 # needs to move backwards, CheckNextIncludeOrder will raise an error.
528 _INITIAL_SECTION = 0
529 _MY_H_SECTION = 1
530 _C_SECTION = 2
531 _CPP_SECTION = 3
532 _OTHER_H_SECTION = 4
533
534 _TYPE_NAMES = {
535 _C_SYS_HEADER: 'C system header',
536 _CPP_SYS_HEADER: 'C++ system header',
537 _LIKELY_MY_HEADER: 'header this file implements',
538 _POSSIBLE_MY_HEADER: 'header this file may implement',
539 _OTHER_HEADER: 'other header',
540 }
541 _SECTION_NAMES = {
542 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
543 _MY_H_SECTION: 'a header this file implements',
544 _C_SECTION: 'C system header',
545 _CPP_SECTION: 'C++ system header',
546 _OTHER_H_SECTION: 'other header',
547 }
548
549 def __init__(self):
550 dict.__init__(self)
erg@google.com2aa59982013-10-28 19:09:25 +0000551 self.ResetSection()
552
553 def ResetSection(self):
erg@google.coma868d2d2009-10-09 21:18:45 +0000554 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000555 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000556 # The path of last found header.
557 self._last_header = ''
558
erg@google.comfd5da632013-10-25 17:39:45 +0000559 def SetLastHeader(self, header_path):
560 self._last_header = header_path
561
erg@google.coma868d2d2009-10-09 21:18:45 +0000562 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000563 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000564
565 - replaces "-" with "_" so they both cmp the same.
566 - removes '-inl' since we don't require them to be after the main header.
567 - lowercase everything, just in case.
568
569 Args:
570 header_path: Path to be canonicalized.
571
572 Returns:
573 Canonicalized path.
574 """
575 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
576
erg@google.comfd5da632013-10-25 17:39:45 +0000577 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.coma868d2d2009-10-09 21:18:45 +0000578 """Check if a header is in alphabetical order with the previous header.
579
580 Args:
erg@google.comfd5da632013-10-25 17:39:45 +0000581 clean_lines: A CleansedLines instance containing the file.
582 linenum: The number of the line to check.
583 header_path: Canonicalized header to be checked.
erg@google.coma868d2d2009-10-09 21:18:45 +0000584
585 Returns:
586 Returns true if the header is in alphabetical order.
587 """
erg@google.comfd5da632013-10-25 17:39:45 +0000588 # If previous section is different from current section, _last_header will
589 # be reset to empty string, so it's always less than current header.
590 #
591 # If previous line was a blank line, assume that the headers are
592 # intentionally sorted the way they are.
593 if (self._last_header > header_path and
594 not Match(r'^\s*$', clean_lines.elided[linenum - 1])):
erg@google.coma868d2d2009-10-09 21:18:45 +0000595 return False
erg@google.coma868d2d2009-10-09 21:18:45 +0000596 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000597
598 def CheckNextIncludeOrder(self, header_type):
599 """Returns a non-empty error message if the next header is out of order.
600
601 This function also updates the internal state to be ready to check
602 the next include.
603
604 Args:
605 header_type: One of the _XXX_HEADER constants defined above.
606
607 Returns:
608 The empty string if the header is in the right order, or an
609 error message describing what's wrong.
610
611 """
612 error_message = ('Found %s after %s' %
613 (self._TYPE_NAMES[header_type],
614 self._SECTION_NAMES[self._section]))
615
erg@google.coma868d2d2009-10-09 21:18:45 +0000616 last_section = self._section
617
erg@google.com4e00b9a2009-01-12 23:05:11 +0000618 if header_type == _C_SYS_HEADER:
619 if self._section <= self._C_SECTION:
620 self._section = self._C_SECTION
621 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000622 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000623 return error_message
624 elif header_type == _CPP_SYS_HEADER:
625 if self._section <= self._CPP_SECTION:
626 self._section = self._CPP_SECTION
627 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000628 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000629 return error_message
630 elif header_type == _LIKELY_MY_HEADER:
631 if self._section <= self._MY_H_SECTION:
632 self._section = self._MY_H_SECTION
633 else:
634 self._section = self._OTHER_H_SECTION
635 elif header_type == _POSSIBLE_MY_HEADER:
636 if self._section <= self._MY_H_SECTION:
637 self._section = self._MY_H_SECTION
638 else:
639 # This will always be the fallback because we're not sure
640 # enough that the header is associated with this file.
641 self._section = self._OTHER_H_SECTION
642 else:
643 assert header_type == _OTHER_HEADER
644 self._section = self._OTHER_H_SECTION
645
erg@google.coma868d2d2009-10-09 21:18:45 +0000646 if last_section != self._section:
647 self._last_header = ''
648
erg@google.com4e00b9a2009-01-12 23:05:11 +0000649 return ''
650
651
652class _CppLintState(object):
653 """Maintains module-wide state.."""
654
655 def __init__(self):
656 self.verbose_level = 1 # global setting.
657 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000658 # filters to apply when emitting error messages
659 self.filters = _DEFAULT_FILTERS[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000660 self.counting = 'total' # In what way are we counting errors?
661 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000662
663 # output format:
664 # "emacs" - format that emacs can parse (default)
665 # "vs7" - format that Microsoft Visual Studio 7 can parse
666 self.output_format = 'emacs'
667
668 def SetOutputFormat(self, output_format):
669 """Sets the output format for errors."""
670 self.output_format = output_format
671
672 def SetVerboseLevel(self, level):
673 """Sets the module's verbosity, and returns the previous setting."""
674 last_verbose_level = self.verbose_level
675 self.verbose_level = level
676 return last_verbose_level
677
erg@google.coma868d2d2009-10-09 21:18:45 +0000678 def SetCountingStyle(self, counting_style):
679 """Sets the module's counting options."""
680 self.counting = counting_style
681
erg@google.com4e00b9a2009-01-12 23:05:11 +0000682 def SetFilters(self, filters):
683 """Sets the error-message filters.
684
685 These filters are applied when deciding whether to emit a given
686 error message.
687
688 Args:
689 filters: A string of comma-separated filters (eg "+whitespace/indent").
690 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000691
692 Raises:
693 ValueError: The comma-separated filters did not all start with '+' or '-'.
694 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000695 """
erg@google.come35f7652009-06-19 20:52:09 +0000696 # Default filters always have less priority than the flag ones.
697 self.filters = _DEFAULT_FILTERS[:]
698 for filt in filters.split(','):
699 clean_filt = filt.strip()
700 if clean_filt:
701 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000702 for filt in self.filters:
703 if not (filt.startswith('+') or filt.startswith('-')):
704 raise ValueError('Every filter in --filters must start with + or -'
705 ' (%s does not)' % filt)
706
erg@google.coma868d2d2009-10-09 21:18:45 +0000707 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000708 """Sets the module's error statistic back to zero."""
709 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000710 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000711
erg@google.coma868d2d2009-10-09 21:18:45 +0000712 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000713 """Bumps the module's error statistic."""
714 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000715 if self.counting in ('toplevel', 'detailed'):
716 if self.counting != 'detailed':
717 category = category.split('/')[0]
718 if category not in self.errors_by_category:
719 self.errors_by_category[category] = 0
720 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000721
erg@google.coma868d2d2009-10-09 21:18:45 +0000722 def PrintErrorCounts(self):
723 """Print a summary of errors by category, and the total."""
724 for category, count in self.errors_by_category.iteritems():
725 sys.stderr.write('Category \'%s\' errors found: %d\n' %
726 (category, count))
727 sys.stderr.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000728
729_cpplint_state = _CppLintState()
730
731
732def _OutputFormat():
733 """Gets the module's output format."""
734 return _cpplint_state.output_format
735
736
737def _SetOutputFormat(output_format):
738 """Sets the module's output format."""
739 _cpplint_state.SetOutputFormat(output_format)
740
741
742def _VerboseLevel():
743 """Returns the module's verbosity setting."""
744 return _cpplint_state.verbose_level
745
746
747def _SetVerboseLevel(level):
748 """Sets the module's verbosity, and returns the previous setting."""
749 return _cpplint_state.SetVerboseLevel(level)
750
751
erg@google.coma868d2d2009-10-09 21:18:45 +0000752def _SetCountingStyle(level):
753 """Sets the module's counting options."""
754 _cpplint_state.SetCountingStyle(level)
755
756
erg@google.com4e00b9a2009-01-12 23:05:11 +0000757def _Filters():
758 """Returns the module's list of output filters, as a list."""
759 return _cpplint_state.filters
760
761
762def _SetFilters(filters):
763 """Sets the module's error-message filters.
764
765 These filters are applied when deciding whether to emit a given
766 error message.
767
768 Args:
769 filters: A string of comma-separated filters (eg "whitespace/indent").
770 Each filter should start with + or -; else we die.
771 """
772 _cpplint_state.SetFilters(filters)
773
774
775class _FunctionState(object):
776 """Tracks current function name and the number of lines in its body."""
777
778 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
779 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
780
781 def __init__(self):
782 self.in_a_function = False
783 self.lines_in_function = 0
784 self.current_function = ''
785
786 def Begin(self, function_name):
787 """Start analyzing function body.
788
789 Args:
790 function_name: The name of the function being tracked.
791 """
792 self.in_a_function = True
793 self.lines_in_function = 0
794 self.current_function = function_name
795
796 def Count(self):
797 """Count line in current function body."""
798 if self.in_a_function:
799 self.lines_in_function += 1
800
801 def Check(self, error, filename, linenum):
802 """Report if too many lines in function body.
803
804 Args:
805 error: The function to call with any errors found.
806 filename: The name of the current file.
807 linenum: The number of the line to check.
808 """
809 if Match(r'T(EST|est)', self.current_function):
810 base_trigger = self._TEST_TRIGGER
811 else:
812 base_trigger = self._NORMAL_TRIGGER
813 trigger = base_trigger * 2**_VerboseLevel()
814
815 if self.lines_in_function > trigger:
816 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
817 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
818 if error_level > 5:
819 error_level = 5
820 error(filename, linenum, 'readability/fn_size', error_level,
821 'Small and focused functions are preferred:'
822 ' %s has %d non-comment lines'
823 ' (error triggered by exceeding %d lines).' % (
824 self.current_function, self.lines_in_function, trigger))
825
826 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000827 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +0000828 self.in_a_function = False
829
830
831class _IncludeError(Exception):
832 """Indicates a problem with the include order in a file."""
833 pass
834
835
836class FileInfo:
837 """Provides utility functions for filenames.
838
839 FileInfo provides easy access to the components of a file's path
840 relative to the project root.
841 """
842
843 def __init__(self, filename):
844 self._filename = filename
845
846 def FullName(self):
847 """Make Windows paths like Unix."""
848 return os.path.abspath(self._filename).replace('\\', '/')
849
850 def RepositoryName(self):
851 """FullName after removing the local path to the repository.
852
853 If we have a real absolute path name here we can try to do something smart:
854 detecting the root of the checkout and truncating /path/to/checkout from
855 the name so that we get header guards that don't include things like
856 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
857 people on different computers who have checked the source out to different
858 locations won't see bogus errors.
859 """
860 fullname = self.FullName()
861
862 if os.path.exists(fullname):
863 project_dir = os.path.dirname(fullname)
864
865 if os.path.exists(os.path.join(project_dir, ".svn")):
866 # If there's a .svn file in the current directory, we recursively look
867 # up the directory tree for the top of the SVN checkout
868 root_dir = project_dir
869 one_up_dir = os.path.dirname(root_dir)
870 while os.path.exists(os.path.join(one_up_dir, ".svn")):
871 root_dir = os.path.dirname(root_dir)
872 one_up_dir = os.path.dirname(one_up_dir)
873
874 prefix = os.path.commonprefix([root_dir, project_dir])
875 return fullname[len(prefix) + 1:]
876
erg@google.com3dc74262011-11-30 01:12:00 +0000877 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
878 # searching up from the current path.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000879 root_dir = os.path.dirname(fullname)
880 while (root_dir != os.path.dirname(root_dir) and
erg@google.com5e169692010-01-28 20:17:01 +0000881 not os.path.exists(os.path.join(root_dir, ".git")) and
erg@google.com3dc74262011-11-30 01:12:00 +0000882 not os.path.exists(os.path.join(root_dir, ".hg")) and
883 not os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000884 root_dir = os.path.dirname(root_dir)
erg@google.com42e59b02010-10-04 22:18:07 +0000885
886 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +0000887 os.path.exists(os.path.join(root_dir, ".hg")) or
888 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +0000889 prefix = os.path.commonprefix([root_dir, project_dir])
890 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000891
892 # Don't know what to do; header guard warnings may be wrong...
893 return fullname
894
895 def Split(self):
896 """Splits the file into the directory, basename, and extension.
897
898 For 'chrome/browser/browser.cc', Split() would
899 return ('chrome/browser', 'browser', '.cc')
900
901 Returns:
902 A tuple of (directory, basename, extension).
903 """
904
905 googlename = self.RepositoryName()
906 project, rest = os.path.split(googlename)
907 return (project,) + os.path.splitext(rest)
908
909 def BaseName(self):
910 """File base name - text after the final slash, before the final period."""
911 return self.Split()[1]
912
913 def Extension(self):
914 """File extension - text following the final period."""
915 return self.Split()[2]
916
917 def NoExtension(self):
918 """File has no source file extension."""
919 return '/'.join(self.Split()[0:2])
920
921 def IsSource(self):
922 """File has a source file extension."""
923 return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx')
924
925
erg+personal@google.com05189642010-04-30 20:43:03 +0000926def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000927 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000928
929 # There are three ways we might decide not to print an error message:
930 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +0000931 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +0000932 if IsErrorSuppressedByNolint(category, linenum):
933 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +0000934 if confidence < _cpplint_state.verbose_level:
935 return False
936
937 is_filtered = False
938 for one_filter in _Filters():
939 if one_filter.startswith('-'):
940 if category.startswith(one_filter[1:]):
941 is_filtered = True
942 elif one_filter.startswith('+'):
943 if category.startswith(one_filter[1:]):
944 is_filtered = False
945 else:
946 assert False # should have been checked for in SetFilter.
947 if is_filtered:
948 return False
949
950 return True
951
952
953def Error(filename, linenum, category, confidence, message):
954 """Logs the fact we've found a lint error.
955
956 We log where the error was found, and also our confidence in the error,
957 that is, how certain we are this is a legitimate style regression, and
958 not a misidentification or a use that's sometimes justified.
959
erg+personal@google.com05189642010-04-30 20:43:03 +0000960 False positives can be suppressed by the use of
961 "cpplint(category)" comments on the offending line. These are
962 parsed into _error_suppressions.
963
erg@google.com4e00b9a2009-01-12 23:05:11 +0000964 Args:
965 filename: The name of the file containing the error.
966 linenum: The number of the line containing the error.
967 category: A string used to describe the "category" this bug
968 falls under: "whitespace", say, or "runtime". Categories
969 may have a hierarchy separated by slashes: "whitespace/indent".
970 confidence: A number from 1-5 representing a confidence score for
971 the error, with 5 meaning that we are certain of the problem,
972 and 1 meaning that it could be a legitimate construct.
973 message: The error message.
974 """
erg+personal@google.com05189642010-04-30 20:43:03 +0000975 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +0000976 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000977 if _cpplint_state.output_format == 'vs7':
978 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
979 filename, linenum, message, category, confidence))
erg@google.com02c27fd2013-05-28 21:34:34 +0000980 elif _cpplint_state.output_format == 'eclipse':
981 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
982 filename, linenum, message, category, confidence))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000983 else:
984 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
985 filename, linenum, message, category, confidence))
986
987
erg@google.com2aa59982013-10-28 19:09:25 +0000988# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000989_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
990 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
991# Matches strings. Escape codes should already be removed by ESCAPES.
992_RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES = re.compile(r'"[^"]*"')
993# Matches characters. Escape codes should already be removed by ESCAPES.
994_RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES = re.compile(r"'.'")
995# Matches multi-line C++ comments.
996# This RE is a little bit more complicated than one might expect, because we
997# have to take care of space removals tools so we can handle comments inside
998# statements better.
999# The current rule is: We only clear spaces from both sides when we're at the
1000# end of the line. Otherwise, we try to remove spaces from the right side,
1001# if this doesn't work we try on left side but only if there's a non-character
1002# on the right.
1003_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
1004 r"""(\s*/\*.*\*/\s*$|
1005 /\*.*\*/\s+|
1006 \s+/\*.*\*/(?=\W)|
1007 /\*.*\*/)""", re.VERBOSE)
1008
1009
1010def IsCppString(line):
1011 """Does line terminate so, that the next symbol is in string constant.
1012
1013 This function does not consider single-line nor multi-line comments.
1014
1015 Args:
1016 line: is a partial line of code starting from the 0..n.
1017
1018 Returns:
1019 True, if next character appended to 'line' is inside a
1020 string constant.
1021 """
1022
1023 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1024 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1025
1026
erg@google.com2aa59982013-10-28 19:09:25 +00001027def CleanseRawStrings(raw_lines):
1028 """Removes C++11 raw strings from lines.
1029
1030 Before:
1031 static const char kData[] = R"(
1032 multi-line string
1033 )";
1034
1035 After:
1036 static const char kData[] = ""
1037 (replaced by blank line)
1038 "";
1039
1040 Args:
1041 raw_lines: list of raw lines.
1042
1043 Returns:
1044 list of lines with C++11 raw strings replaced by empty strings.
1045 """
1046
1047 delimiter = None
1048 lines_without_raw_strings = []
1049 for line in raw_lines:
1050 if delimiter:
1051 # Inside a raw string, look for the end
1052 end = line.find(delimiter)
1053 if end >= 0:
1054 # Found the end of the string, match leading space for this
1055 # line and resume copying the original lines, and also insert
1056 # a "" on the last line.
1057 leading_space = Match(r'^(\s*)\S', line)
1058 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1059 delimiter = None
1060 else:
1061 # Haven't found the end yet, append a blank line.
1062 line = ''
1063
1064 else:
1065 # Look for beginning of a raw string.
1066 # See 2.14.15 [lex.string] for syntax.
1067 matched = Match(r'^(.*)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1068 if matched:
1069 delimiter = ')' + matched.group(2) + '"'
1070
1071 end = matched.group(3).find(delimiter)
1072 if end >= 0:
1073 # Raw string ended on same line
1074 line = (matched.group(1) + '""' +
1075 matched.group(3)[end + len(delimiter):])
1076 delimiter = None
1077 else:
1078 # Start of a multi-line raw string
1079 line = matched.group(1) + '""'
1080
1081 lines_without_raw_strings.append(line)
1082
1083 # TODO(unknown): if delimiter is not None here, we might want to
1084 # emit a warning for unterminated string.
1085 return lines_without_raw_strings
1086
1087
erg@google.com4e00b9a2009-01-12 23:05:11 +00001088def FindNextMultiLineCommentStart(lines, lineix):
1089 """Find the beginning marker for a multiline comment."""
1090 while lineix < len(lines):
1091 if lines[lineix].strip().startswith('/*'):
1092 # Only return this marker if the comment goes beyond this line
1093 if lines[lineix].strip().find('*/', 2) < 0:
1094 return lineix
1095 lineix += 1
1096 return len(lines)
1097
1098
1099def FindNextMultiLineCommentEnd(lines, lineix):
1100 """We are inside a comment, find the end marker."""
1101 while lineix < len(lines):
1102 if lines[lineix].strip().endswith('*/'):
1103 return lineix
1104 lineix += 1
1105 return len(lines)
1106
1107
1108def RemoveMultiLineCommentsFromRange(lines, begin, end):
1109 """Clears a range of lines for multi-line comments."""
1110 # Having // dummy comments makes the lines non-empty, so we will not get
1111 # unnecessary blank line warnings later in the code.
1112 for i in range(begin, end):
1113 lines[i] = '// dummy'
1114
1115
1116def RemoveMultiLineComments(filename, lines, error):
1117 """Removes multiline (c-style) comments from lines."""
1118 lineix = 0
1119 while lineix < len(lines):
1120 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1121 if lineix_begin >= len(lines):
1122 return
1123 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1124 if lineix_end >= len(lines):
1125 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1126 'Could not find end of multi-line comment')
1127 return
1128 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1129 lineix = lineix_end + 1
1130
1131
1132def CleanseComments(line):
1133 """Removes //-comments and single-line C-style /* */ comments.
1134
1135 Args:
1136 line: A line of C++ source.
1137
1138 Returns:
1139 The line with single-line comments removed.
1140 """
1141 commentpos = line.find('//')
1142 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +00001143 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001144 # get rid of /* ... */
1145 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1146
1147
erg@google.coma87abb82009-02-24 01:41:01 +00001148class CleansedLines(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001149 """Holds 3 copies of all lines with different preprocessing applied to them.
1150
1151 1) elided member contains lines without strings and comments,
1152 2) lines member contains lines without comments, and
erg@google.comd350fe52013-01-14 17:51:48 +00001153 3) raw_lines member contains all the lines without processing.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001154 All these three members are of <type 'list'>, and of the same length.
1155 """
1156
1157 def __init__(self, lines):
1158 self.elided = []
1159 self.lines = []
1160 self.raw_lines = lines
1161 self.num_lines = len(lines)
erg@google.com2aa59982013-10-28 19:09:25 +00001162 self.lines_without_raw_strings = CleanseRawStrings(lines)
1163 for linenum in range(len(self.lines_without_raw_strings)):
1164 self.lines.append(CleanseComments(
1165 self.lines_without_raw_strings[linenum]))
1166 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001167 self.elided.append(CleanseComments(elided))
1168
1169 def NumLines(self):
1170 """Returns the number of lines represented."""
1171 return self.num_lines
1172
1173 @staticmethod
1174 def _CollapseStrings(elided):
1175 """Collapses strings and chars on a line to simple "" or '' blocks.
1176
1177 We nix strings first so we're not fooled by text like '"http://"'
1178
1179 Args:
1180 elided: The line being processed.
1181
1182 Returns:
1183 The line with collapsed strings.
1184 """
1185 if not _RE_PATTERN_INCLUDE.match(elided):
1186 # Remove escaped characters first to make quote/single quote collapsing
1187 # basic. Things that look like escaped characters shouldn't occur
1188 # outside of strings and chars.
1189 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1190 elided = _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES.sub("''", elided)
1191 elided = _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES.sub('""', elided)
1192 return elided
1193
1194
erg@google.comd350fe52013-01-14 17:51:48 +00001195def FindEndOfExpressionInLine(line, startpos, depth, startchar, endchar):
1196 """Find the position just after the matching endchar.
1197
1198 Args:
1199 line: a CleansedLines line.
1200 startpos: start searching at this position.
1201 depth: nesting level at startpos.
1202 startchar: expression opening character.
1203 endchar: expression closing character.
1204
1205 Returns:
erg@google.com2aa59982013-10-28 19:09:25 +00001206 On finding matching endchar: (index just after matching endchar, 0)
1207 Otherwise: (-1, new depth at end of this line)
erg@google.comd350fe52013-01-14 17:51:48 +00001208 """
1209 for i in xrange(startpos, len(line)):
1210 if line[i] == startchar:
1211 depth += 1
1212 elif line[i] == endchar:
1213 depth -= 1
1214 if depth == 0:
erg@google.com2aa59982013-10-28 19:09:25 +00001215 return (i + 1, 0)
1216 return (-1, depth)
erg@google.comd350fe52013-01-14 17:51:48 +00001217
1218
erg@google.com4e00b9a2009-01-12 23:05:11 +00001219def CloseExpression(clean_lines, linenum, pos):
erg@google.com2aa59982013-10-28 19:09:25 +00001220 """If input points to ( or { or [ or <, finds the position that closes it.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001221
erg@google.com2aa59982013-10-28 19:09:25 +00001222 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001223 linenum/pos that correspond to the closing of the expression.
1224
1225 Args:
1226 clean_lines: A CleansedLines instance containing the file.
1227 linenum: The number of the line to check.
1228 pos: A position on the line.
1229
1230 Returns:
1231 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1232 (line, len(lines), -1) if we never find a close. Note we ignore
1233 strings and comments when matching; and the line we return is the
1234 'cleansed' line at linenum.
1235 """
1236
1237 line = clean_lines.elided[linenum]
1238 startchar = line[pos]
erg@google.com2aa59982013-10-28 19:09:25 +00001239 if startchar not in '({[<':
erg@google.com4e00b9a2009-01-12 23:05:11 +00001240 return (line, clean_lines.NumLines(), -1)
1241 if startchar == '(': endchar = ')'
1242 if startchar == '[': endchar = ']'
1243 if startchar == '{': endchar = '}'
erg@google.com2aa59982013-10-28 19:09:25 +00001244 if startchar == '<': endchar = '>'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001245
erg@google.comd350fe52013-01-14 17:51:48 +00001246 # Check first line
erg@google.com2aa59982013-10-28 19:09:25 +00001247 (end_pos, num_open) = FindEndOfExpressionInLine(
1248 line, pos, 0, startchar, endchar)
erg@google.comd350fe52013-01-14 17:51:48 +00001249 if end_pos > -1:
1250 return (line, linenum, end_pos)
erg@google.com2aa59982013-10-28 19:09:25 +00001251
1252 # Continue scanning forward
erg@google.comd350fe52013-01-14 17:51:48 +00001253 while linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001254 linenum += 1
1255 line = clean_lines.elided[linenum]
erg@google.com2aa59982013-10-28 19:09:25 +00001256 (end_pos, num_open) = FindEndOfExpressionInLine(
1257 line, 0, num_open, startchar, endchar)
1258 if end_pos > -1:
1259 return (line, linenum, end_pos)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001260
erg@google.comd350fe52013-01-14 17:51:48 +00001261 # Did not find endchar before end of file, give up
1262 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001263
erg@google.com2aa59982013-10-28 19:09:25 +00001264
1265def FindStartOfExpressionInLine(line, endpos, depth, startchar, endchar):
1266 """Find position at the matching startchar.
1267
1268 This is almost the reverse of FindEndOfExpressionInLine, but note
1269 that the input position and returned position differs by 1.
1270
1271 Args:
1272 line: a CleansedLines line.
1273 endpos: start searching at this position.
1274 depth: nesting level at endpos.
1275 startchar: expression opening character.
1276 endchar: expression closing character.
1277
1278 Returns:
1279 On finding matching startchar: (index at matching startchar, 0)
1280 Otherwise: (-1, new depth at beginning of this line)
1281 """
1282 for i in xrange(endpos, -1, -1):
1283 if line[i] == endchar:
1284 depth += 1
1285 elif line[i] == startchar:
1286 depth -= 1
1287 if depth == 0:
1288 return (i, 0)
1289 return (-1, depth)
1290
1291
1292def ReverseCloseExpression(clean_lines, linenum, pos):
1293 """If input points to ) or } or ] or >, finds the position that opens it.
1294
1295 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1296 linenum/pos that correspond to the opening of the expression.
1297
1298 Args:
1299 clean_lines: A CleansedLines instance containing the file.
1300 linenum: The number of the line to check.
1301 pos: A position on the line.
1302
1303 Returns:
1304 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1305 (line, 0, -1) if we never find the matching opening brace. Note
1306 we ignore strings and comments when matching; and the line we
1307 return is the 'cleansed' line at linenum.
1308 """
1309 line = clean_lines.elided[linenum]
1310 endchar = line[pos]
1311 if endchar not in ')}]>':
1312 return (line, 0, -1)
1313 if endchar == ')': startchar = '('
1314 if endchar == ']': startchar = '['
1315 if endchar == '}': startchar = '{'
1316 if endchar == '>': startchar = '<'
1317
1318 # Check last line
1319 (start_pos, num_open) = FindStartOfExpressionInLine(
1320 line, pos, 0, startchar, endchar)
1321 if start_pos > -1:
1322 return (line, linenum, start_pos)
1323
1324 # Continue scanning backward
1325 while linenum > 0:
1326 linenum -= 1
1327 line = clean_lines.elided[linenum]
1328 (start_pos, num_open) = FindStartOfExpressionInLine(
1329 line, len(line) - 1, num_open, startchar, endchar)
1330 if start_pos > -1:
1331 return (line, linenum, start_pos)
1332
1333 # Did not find startchar before beginning of file, give up
1334 return (line, 0, -1)
1335
1336
erg@google.com4e00b9a2009-01-12 23:05:11 +00001337def CheckForCopyright(filename, lines, error):
1338 """Logs an error if no Copyright message appears at the top of the file."""
1339
1340 # We'll say it should occur by line 10. Don't forget there's a
1341 # dummy line at the front.
1342 for line in xrange(1, min(len(lines), 11)):
1343 if re.search(r'Copyright', lines[line], re.I): break
1344 else: # means no copyright line was found
1345 error(filename, 0, 'legal/copyright', 5,
1346 'No copyright message found. '
1347 'You should have a line: "Copyright [year] <Copyright Owner>"')
1348
1349
1350def GetHeaderGuardCPPVariable(filename):
1351 """Returns the CPP variable that should be used as a header guard.
1352
1353 Args:
1354 filename: The name of a C++ header file.
1355
1356 Returns:
1357 The CPP variable that should be used as a header guard in the
1358 named file.
1359
1360 """
1361
erg+personal@google.com05189642010-04-30 20:43:03 +00001362 # Restores original filename in case that cpplint is invoked from Emacs's
1363 # flymake.
1364 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001365 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
erg+personal@google.com05189642010-04-30 20:43:03 +00001366
erg@google.com4e00b9a2009-01-12 23:05:11 +00001367 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001368 file_path_from_root = fileinfo.RepositoryName()
1369 if _root:
1370 file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root)
1371 return re.sub(r'[-./\s]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001372
1373
1374def CheckForHeaderGuard(filename, lines, error):
1375 """Checks that the file contains a header guard.
1376
erg@google.coma87abb82009-02-24 01:41:01 +00001377 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001378 headers, checks that the full pathname is used.
1379
1380 Args:
1381 filename: The name of the C++ header file.
1382 lines: An array of strings, each representing a line of the file.
1383 error: The function to call with any errors found.
1384 """
1385
1386 cppvar = GetHeaderGuardCPPVariable(filename)
1387
1388 ifndef = None
1389 ifndef_linenum = 0
1390 define = None
1391 endif = None
1392 endif_linenum = 0
1393 for linenum, line in enumerate(lines):
1394 linesplit = line.split()
1395 if len(linesplit) >= 2:
1396 # find the first occurrence of #ifndef and #define, save arg
1397 if not ifndef and linesplit[0] == '#ifndef':
1398 # set ifndef to the header guard presented on the #ifndef line.
1399 ifndef = linesplit[1]
1400 ifndef_linenum = linenum
1401 if not define and linesplit[0] == '#define':
1402 define = linesplit[1]
1403 # find the last occurrence of #endif, save entire line
1404 if line.startswith('#endif'):
1405 endif = line
1406 endif_linenum = linenum
1407
erg@google.comdc289702012-01-26 20:30:03 +00001408 if not ifndef:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001409 error(filename, 0, 'build/header_guard', 5,
1410 'No #ifndef header guard found, suggested CPP variable is: %s' %
1411 cppvar)
1412 return
1413
erg@google.comdc289702012-01-26 20:30:03 +00001414 if not define:
1415 error(filename, 0, 'build/header_guard', 5,
1416 'No #define header guard found, suggested CPP variable is: %s' %
1417 cppvar)
1418 return
1419
erg@google.com4e00b9a2009-01-12 23:05:11 +00001420 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1421 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001422 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001423 error_level = 0
1424 if ifndef != cppvar + '_':
1425 error_level = 5
1426
erg+personal@google.com05189642010-04-30 20:43:03 +00001427 ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum,
1428 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001429 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1430 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1431
erg@google.comdc289702012-01-26 20:30:03 +00001432 if define != ifndef:
1433 error(filename, 0, 'build/header_guard', 5,
1434 '#ifndef and #define don\'t match, suggested CPP variable is: %s' %
1435 cppvar)
1436 return
1437
erg+personal@google.com05189642010-04-30 20:43:03 +00001438 if endif != ('#endif // %s' % cppvar):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001439 error_level = 0
1440 if endif != ('#endif // %s' % (cppvar + '_')):
1441 error_level = 5
1442
erg+personal@google.com05189642010-04-30 20:43:03 +00001443 ParseNolintSuppressions(filename, lines[endif_linenum], endif_linenum,
1444 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001445 error(filename, endif_linenum, 'build/header_guard', error_level,
1446 '#endif line should be "#endif // %s"' % cppvar)
1447
1448
erg@google.com2aa59982013-10-28 19:09:25 +00001449def CheckForBadCharacters(filename, lines, error):
1450 """Logs an error for each line containing bad characters.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001451
erg@google.com2aa59982013-10-28 19:09:25 +00001452 Two kinds of bad characters:
1453
1454 1. Unicode replacement characters: These indicate that either the file
1455 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1456 it shouldn't). Note that it's possible for this to throw off line
1457 numbering if the invalid UTF-8 occurred adjacent to a newline.
1458
1459 2. NUL bytes. These are problematic for some tools.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001460
1461 Args:
1462 filename: The name of the current file.
1463 lines: An array of strings, each representing a line of the file.
1464 error: The function to call with any errors found.
1465 """
1466 for linenum, line in enumerate(lines):
1467 if u'\ufffd' in line:
1468 error(filename, linenum, 'readability/utf8', 5,
1469 'Line contains invalid UTF-8 (or Unicode replacement character).')
erg@google.com2aa59982013-10-28 19:09:25 +00001470 if '\0' in line:
1471 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001472
1473
1474def CheckForNewlineAtEOF(filename, lines, error):
1475 """Logs an error if there is no newline char at the end of the file.
1476
1477 Args:
1478 filename: The name of the current file.
1479 lines: An array of strings, each representing a line of the file.
1480 error: The function to call with any errors found.
1481 """
1482
1483 # The array lines() was created by adding two newlines to the
1484 # original file (go figure), then splitting on \n.
1485 # To verify that the file ends in \n, we just have to make sure the
1486 # last-but-two element of lines() exists and is empty.
1487 if len(lines) < 3 or lines[-2]:
1488 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1489 'Could not find a newline character at the end of the file.')
1490
1491
1492def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1493 """Logs an error if we see /* ... */ or "..." that extend past one line.
1494
1495 /* ... */ comments are legit inside macros, for one line.
1496 Otherwise, we prefer // comments, so it's ok to warn about the
1497 other. Likewise, it's ok for strings to extend across multiple
1498 lines, as long as a line continuation character (backslash)
1499 terminates each line. Although not currently prohibited by the C++
1500 style guide, it's ugly and unnecessary. We don't do well with either
1501 in this lint program, so we warn about both.
1502
1503 Args:
1504 filename: The name of the current file.
1505 clean_lines: A CleansedLines instance containing the file.
1506 linenum: The number of the line to check.
1507 error: The function to call with any errors found.
1508 """
1509 line = clean_lines.elided[linenum]
1510
1511 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1512 # second (escaped) slash may trigger later \" detection erroneously.
1513 line = line.replace('\\\\', '')
1514
1515 if line.count('/*') > line.count('*/'):
1516 error(filename, linenum, 'readability/multiline_comment', 5,
1517 'Complex multi-line /*...*/-style comment found. '
1518 'Lint may give bogus warnings. '
1519 'Consider replacing these with //-style comments, '
1520 'with #if 0...#endif, '
1521 'or with more clearly structured multi-line comments.')
1522
1523 if (line.count('"') - line.count('\\"')) % 2:
1524 error(filename, linenum, 'readability/multiline_string', 5,
1525 'Multi-line string ("...") found. This lint script doesn\'t '
erg@google.com2aa59982013-10-28 19:09:25 +00001526 'do well with such strings, and may give bogus warnings. '
1527 'Use C++11 raw strings or concatenation instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001528
1529
1530threading_list = (
1531 ('asctime(', 'asctime_r('),
1532 ('ctime(', 'ctime_r('),
1533 ('getgrgid(', 'getgrgid_r('),
1534 ('getgrnam(', 'getgrnam_r('),
1535 ('getlogin(', 'getlogin_r('),
1536 ('getpwnam(', 'getpwnam_r('),
1537 ('getpwuid(', 'getpwuid_r('),
1538 ('gmtime(', 'gmtime_r('),
1539 ('localtime(', 'localtime_r('),
1540 ('rand(', 'rand_r('),
erg@google.com4e00b9a2009-01-12 23:05:11 +00001541 ('strtok(', 'strtok_r('),
1542 ('ttyname(', 'ttyname_r('),
1543 )
1544
1545
1546def CheckPosixThreading(filename, clean_lines, linenum, error):
1547 """Checks for calls to thread-unsafe functions.
1548
1549 Much code has been originally written without consideration of
1550 multi-threading. Also, engineers are relying on their old experience;
1551 they have learned posix before threading extensions were added. These
1552 tests guide the engineers to use thread-safe functions (when using
1553 posix directly).
1554
1555 Args:
1556 filename: The name of the current file.
1557 clean_lines: A CleansedLines instance containing the file.
1558 linenum: The number of the line to check.
1559 error: The function to call with any errors found.
1560 """
1561 line = clean_lines.elided[linenum]
1562 for single_thread_function, multithread_safe_function in threading_list:
1563 ix = line.find(single_thread_function)
erg@google.com2aa59982013-10-28 19:09:25 +00001564 # Comparisons made explicit for clarity -- pylint: disable=g-explicit-bool-comparison
erg@google.com4e00b9a2009-01-12 23:05:11 +00001565 if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and
1566 line[ix - 1] not in ('_', '.', '>'))):
1567 error(filename, linenum, 'runtime/threadsafe_fn', 2,
1568 'Consider using ' + multithread_safe_function +
1569 '...) instead of ' + single_thread_function +
1570 '...) for improved thread safety.')
1571
1572
erg@google.com2aa59982013-10-28 19:09:25 +00001573def CheckVlogArguments(filename, clean_lines, linenum, error):
1574 """Checks that VLOG() is only used for defining a logging level.
1575
1576 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
1577 VLOG(FATAL) are not.
1578
1579 Args:
1580 filename: The name of the current file.
1581 clean_lines: A CleansedLines instance containing the file.
1582 linenum: The number of the line to check.
1583 error: The function to call with any errors found.
1584 """
1585 line = clean_lines.elided[linenum]
1586 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
1587 error(filename, linenum, 'runtime/vlog', 5,
1588 'VLOG() should be used with numeric verbosity level. '
1589 'Use LOG() if you want symbolic severity levels.')
1590
1591
erg@google.coma868d2d2009-10-09 21:18:45 +00001592# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00001593# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00001594_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00001595 r'^\s*\*\w+(\+\+|--);')
1596
1597
1598def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00001599 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00001600
1601 For example following function:
1602 void increment_counter(int* count) {
1603 *count++;
1604 }
1605 is invalid, because it effectively does count++, moving pointer, and should
1606 be replaced with ++*count, (*count)++ or *count += 1.
1607
1608 Args:
1609 filename: The name of the current file.
1610 clean_lines: A CleansedLines instance containing the file.
1611 linenum: The number of the line to check.
1612 error: The function to call with any errors found.
1613 """
1614 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00001615 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00001616 error(filename, linenum, 'runtime/invalid_increment', 5,
1617 'Changing pointer instead of value (or unused value of operator*).')
1618
1619
erg@google.comd350fe52013-01-14 17:51:48 +00001620class _BlockInfo(object):
1621 """Stores information about a generic block of code."""
1622
1623 def __init__(self, seen_open_brace):
1624 self.seen_open_brace = seen_open_brace
1625 self.open_parentheses = 0
1626 self.inline_asm = _NO_ASM
1627
1628 def CheckBegin(self, filename, clean_lines, linenum, error):
1629 """Run checks that applies to text up to the opening brace.
1630
1631 This is mostly for checking the text after the class identifier
1632 and the "{", usually where the base class is specified. For other
1633 blocks, there isn't much to check, so we always pass.
1634
1635 Args:
1636 filename: The name of the current file.
1637 clean_lines: A CleansedLines instance containing the file.
1638 linenum: The number of the line to check.
1639 error: The function to call with any errors found.
1640 """
1641 pass
1642
1643 def CheckEnd(self, filename, clean_lines, linenum, error):
1644 """Run checks that applies to text after the closing brace.
1645
1646 This is mostly used for checking end of namespace comments.
1647
1648 Args:
1649 filename: The name of the current file.
1650 clean_lines: A CleansedLines instance containing the file.
1651 linenum: The number of the line to check.
1652 error: The function to call with any errors found.
1653 """
1654 pass
1655
1656
1657class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001658 """Stores information about a class."""
1659
erg@google.comd350fe52013-01-14 17:51:48 +00001660 def __init__(self, name, class_or_struct, clean_lines, linenum):
1661 _BlockInfo.__init__(self, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001662 self.name = name
erg@google.comd350fe52013-01-14 17:51:48 +00001663 self.starting_linenum = linenum
erg@google.com4e00b9a2009-01-12 23:05:11 +00001664 self.is_derived = False
erg@google.comd350fe52013-01-14 17:51:48 +00001665 if class_or_struct == 'struct':
1666 self.access = 'public'
erg@google.comfd5da632013-10-25 17:39:45 +00001667 self.is_struct = True
erg@google.comd350fe52013-01-14 17:51:48 +00001668 else:
1669 self.access = 'private'
erg@google.comfd5da632013-10-25 17:39:45 +00001670 self.is_struct = False
1671
1672 # Remember initial indentation level for this class. Using raw_lines here
erg@google.comc6671232013-10-25 21:44:03 +00001673 # instead of elided to account for leading comments.
erg@google.comfd5da632013-10-25 17:39:45 +00001674 initial_indent = Match(r'^( *)\S', clean_lines.raw_lines[linenum])
1675 if initial_indent:
1676 self.class_indent = len(initial_indent.group(1))
1677 else:
1678 self.class_indent = 0
erg@google.com4e00b9a2009-01-12 23:05:11 +00001679
erg@google.com8a95ecc2011-09-08 00:45:54 +00001680 # Try to find the end of the class. This will be confused by things like:
1681 # class A {
1682 # } *x = { ...
1683 #
1684 # But it's still good enough for CheckSectionSpacing.
1685 self.last_line = 0
1686 depth = 0
1687 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00001688 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00001689 depth += line.count('{') - line.count('}')
1690 if not depth:
1691 self.last_line = i
1692 break
1693
erg@google.comd350fe52013-01-14 17:51:48 +00001694 def CheckBegin(self, filename, clean_lines, linenum, error):
1695 # Look for a bare ':'
1696 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
1697 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00001698
erg@google.comfd5da632013-10-25 17:39:45 +00001699 def CheckEnd(self, filename, clean_lines, linenum, error):
1700 # Check that closing brace is aligned with beginning of the class.
1701 # Only do this if the closing brace is indented by only whitespaces.
1702 # This means we will not check single-line class definitions.
1703 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
1704 if indent and len(indent.group(1)) != self.class_indent:
1705 if self.is_struct:
1706 parent = 'struct ' + self.name
1707 else:
1708 parent = 'class ' + self.name
1709 error(filename, linenum, 'whitespace/indent', 3,
1710 'Closing brace should be aligned with beginning of %s' % parent)
1711
erg@google.com4e00b9a2009-01-12 23:05:11 +00001712
erg@google.comd350fe52013-01-14 17:51:48 +00001713class _NamespaceInfo(_BlockInfo):
1714 """Stores information about a namespace."""
1715
1716 def __init__(self, name, linenum):
1717 _BlockInfo.__init__(self, False)
1718 self.name = name or ''
1719 self.starting_linenum = linenum
1720
1721 def CheckEnd(self, filename, clean_lines, linenum, error):
1722 """Check end of namespace comments."""
1723 line = clean_lines.raw_lines[linenum]
1724
1725 # Check how many lines is enclosed in this namespace. Don't issue
1726 # warning for missing namespace comments if there aren't enough
1727 # lines. However, do apply checks if there is already an end of
1728 # namespace comment and it's incorrect.
1729 #
1730 # TODO(unknown): We always want to check end of namespace comments
1731 # if a namespace is large, but sometimes we also want to apply the
1732 # check if a short namespace contained nontrivial things (something
1733 # other than forward declarations). There is currently no logic on
1734 # deciding what these nontrivial things are, so this check is
1735 # triggered by namespace size only, which works most of the time.
1736 if (linenum - self.starting_linenum < 10
1737 and not Match(r'};*\s*(//|/\*).*\bnamespace\b', line)):
1738 return
1739
1740 # Look for matching comment at end of namespace.
1741 #
1742 # Note that we accept C style "/* */" comments for terminating
1743 # namespaces, so that code that terminate namespaces inside
erg@google.comc6671232013-10-25 21:44:03 +00001744 # preprocessor macros can be cpplint clean.
erg@google.comd350fe52013-01-14 17:51:48 +00001745 #
1746 # We also accept stuff like "// end of namespace <name>." with the
1747 # period at the end.
1748 #
1749 # Besides these, we don't accept anything else, otherwise we might
1750 # get false negatives when existing comment is a substring of the
erg@google.comc6671232013-10-25 21:44:03 +00001751 # expected namespace.
erg@google.comd350fe52013-01-14 17:51:48 +00001752 if self.name:
1753 # Named namespace
1754 if not Match((r'};*\s*(//|/\*).*\bnamespace\s+' + re.escape(self.name) +
1755 r'[\*/\.\\\s]*$'),
1756 line):
1757 error(filename, linenum, 'readability/namespace', 5,
1758 'Namespace should be terminated with "// namespace %s"' %
1759 self.name)
1760 else:
1761 # Anonymous namespace
1762 if not Match(r'};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
1763 error(filename, linenum, 'readability/namespace', 5,
1764 'Namespace should be terminated with "// namespace"')
1765
1766
1767class _PreprocessorInfo(object):
1768 """Stores checkpoints of nesting stacks when #if/#else is seen."""
1769
1770 def __init__(self, stack_before_if):
1771 # The entire nesting stack before #if
1772 self.stack_before_if = stack_before_if
1773
1774 # The entire nesting stack up to #else
1775 self.stack_before_else = []
1776
1777 # Whether we have already seen #else or #elif
1778 self.seen_else = False
1779
1780
1781class _NestingState(object):
1782 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001783
1784 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00001785 # Stack for tracking all braces. An object is pushed whenever we
1786 # see a "{", and popped when we see a "}". Only 3 types of
1787 # objects are possible:
1788 # - _ClassInfo: a class or struct.
1789 # - _NamespaceInfo: a namespace.
1790 # - _BlockInfo: some other type of block.
1791 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00001792
erg@google.comd350fe52013-01-14 17:51:48 +00001793 # Stack of _PreprocessorInfo objects.
1794 self.pp_stack = []
1795
1796 def SeenOpenBrace(self):
1797 """Check if we have seen the opening brace for the innermost block.
1798
1799 Returns:
1800 True if we have seen the opening brace, False if the innermost
1801 block is still expecting an opening brace.
1802 """
1803 return (not self.stack) or self.stack[-1].seen_open_brace
1804
1805 def InNamespaceBody(self):
1806 """Check if we are currently one level inside a namespace body.
1807
1808 Returns:
1809 True if top of the stack is a namespace block, False otherwise.
1810 """
1811 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
1812
1813 def UpdatePreprocessor(self, line):
1814 """Update preprocessor stack.
1815
1816 We need to handle preprocessors due to classes like this:
1817 #ifdef SWIG
1818 struct ResultDetailsPageElementExtensionPoint {
1819 #else
1820 struct ResultDetailsPageElementExtensionPoint : public Extension {
1821 #endif
erg@google.comd350fe52013-01-14 17:51:48 +00001822
1823 We make the following assumptions (good enough for most files):
1824 - Preprocessor condition evaluates to true from #if up to first
1825 #else/#elif/#endif.
1826
1827 - Preprocessor condition evaluates to false from #else/#elif up
1828 to #endif. We still perform lint checks on these lines, but
1829 these do not affect nesting stack.
1830
1831 Args:
1832 line: current line to check.
1833 """
1834 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
1835 # Beginning of #if block, save the nesting stack here. The saved
1836 # stack will allow us to restore the parsing state in the #else case.
1837 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
1838 elif Match(r'^\s*#\s*(else|elif)\b', line):
1839 # Beginning of #else block
1840 if self.pp_stack:
1841 if not self.pp_stack[-1].seen_else:
1842 # This is the first #else or #elif block. Remember the
1843 # whole nesting stack up to this point. This is what we
1844 # keep after the #endif.
1845 self.pp_stack[-1].seen_else = True
1846 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
1847
1848 # Restore the stack to how it was before the #if
1849 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
1850 else:
1851 # TODO(unknown): unexpected #else, issue warning?
1852 pass
1853 elif Match(r'^\s*#\s*endif\b', line):
1854 # End of #if or #else blocks.
1855 if self.pp_stack:
1856 # If we saw an #else, we will need to restore the nesting
1857 # stack to its former state before the #else, otherwise we
1858 # will just continue from where we left off.
1859 if self.pp_stack[-1].seen_else:
1860 # Here we can just use a shallow copy since we are the last
1861 # reference to it.
1862 self.stack = self.pp_stack[-1].stack_before_else
1863 # Drop the corresponding #if
1864 self.pp_stack.pop()
1865 else:
1866 # TODO(unknown): unexpected #endif, issue warning?
1867 pass
1868
1869 def Update(self, filename, clean_lines, linenum, error):
1870 """Update nesting state with current line.
1871
1872 Args:
1873 filename: The name of the current file.
1874 clean_lines: A CleansedLines instance containing the file.
1875 linenum: The number of the line to check.
1876 error: The function to call with any errors found.
1877 """
1878 line = clean_lines.elided[linenum]
1879
1880 # Update pp_stack first
1881 self.UpdatePreprocessor(line)
1882
1883 # Count parentheses. This is to avoid adding struct arguments to
1884 # the nesting stack.
1885 if self.stack:
1886 inner_block = self.stack[-1]
1887 depth_change = line.count('(') - line.count(')')
1888 inner_block.open_parentheses += depth_change
1889
1890 # Also check if we are starting or ending an inline assembly block.
1891 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
1892 if (depth_change != 0 and
1893 inner_block.open_parentheses == 1 and
1894 _MATCH_ASM.match(line)):
1895 # Enter assembly block
1896 inner_block.inline_asm = _INSIDE_ASM
1897 else:
1898 # Not entering assembly block. If previous line was _END_ASM,
1899 # we will now shift to _NO_ASM state.
1900 inner_block.inline_asm = _NO_ASM
1901 elif (inner_block.inline_asm == _INSIDE_ASM and
1902 inner_block.open_parentheses == 0):
1903 # Exit assembly block
1904 inner_block.inline_asm = _END_ASM
1905
1906 # Consume namespace declaration at the beginning of the line. Do
1907 # this in a loop so that we catch same line declarations like this:
1908 # namespace proto2 { namespace bridge { class MessageSet; } }
1909 while True:
1910 # Match start of namespace. The "\b\s*" below catches namespace
1911 # declarations even if it weren't followed by a whitespace, this
1912 # is so that we don't confuse our namespace checker. The
1913 # missing spaces will be flagged by CheckSpacing.
1914 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
1915 if not namespace_decl_match:
1916 break
1917
1918 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
1919 self.stack.append(new_namespace)
1920
1921 line = namespace_decl_match.group(2)
1922 if line.find('{') != -1:
1923 new_namespace.seen_open_brace = True
1924 line = line[line.find('{') + 1:]
1925
1926 # Look for a class declaration in whatever is left of the line
1927 # after parsing namespaces. The regexp accounts for decorated classes
1928 # such as in:
1929 # class LOCKABLE API Object {
1930 # };
1931 #
1932 # Templates with class arguments may confuse the parser, for example:
1933 # template <class T
1934 # class Comparator = less<T>,
1935 # class Vector = vector<T> >
1936 # class HeapQueue {
1937 #
1938 # Because this parser has no nesting state about templates, by the
1939 # time it saw "class Comparator", it may think that it's a new class.
1940 # Nested templates have a similar problem:
1941 # template <
1942 # typename ExportedType,
1943 # typename TupleType,
1944 # template <typename, typename> class ImplTemplate>
1945 #
1946 # To avoid these cases, we ignore classes that are followed by '=' or '>'
1947 class_decl_match = Match(
1948 r'\s*(template\s*<[\w\s<>,:]*>\s*)?'
erg@google.comfd5da632013-10-25 17:39:45 +00001949 r'(class|struct)\s+([A-Z_]+\s+)*(\w+(?:::\w+)*)'
1950 r'(([^=>]|<[^<>]*>|<[^<>]*<[^<>]*>\s*>)*)$', line)
erg@google.comd350fe52013-01-14 17:51:48 +00001951 if (class_decl_match and
1952 (not self.stack or self.stack[-1].open_parentheses == 0)):
1953 self.stack.append(_ClassInfo(
1954 class_decl_match.group(4), class_decl_match.group(2),
1955 clean_lines, linenum))
1956 line = class_decl_match.group(5)
1957
1958 # If we have not yet seen the opening brace for the innermost block,
1959 # run checks here.
1960 if not self.SeenOpenBrace():
1961 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
1962
1963 # Update access control if we are inside a class/struct
1964 if self.stack and isinstance(self.stack[-1], _ClassInfo):
erg@google.comfd5da632013-10-25 17:39:45 +00001965 classinfo = self.stack[-1]
1966 access_match = Match(
1967 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
1968 r':(?:[^:]|$)',
1969 line)
erg@google.comd350fe52013-01-14 17:51:48 +00001970 if access_match:
erg@google.comfd5da632013-10-25 17:39:45 +00001971 classinfo.access = access_match.group(2)
1972
1973 # Check that access keywords are indented +1 space. Skip this
erg@google.comc6671232013-10-25 21:44:03 +00001974 # check if the keywords are not preceded by whitespaces.
erg@google.comfd5da632013-10-25 17:39:45 +00001975 indent = access_match.group(1)
1976 if (len(indent) != classinfo.class_indent + 1 and
1977 Match(r'^\s*$', indent)):
1978 if classinfo.is_struct:
1979 parent = 'struct ' + classinfo.name
1980 else:
1981 parent = 'class ' + classinfo.name
1982 slots = ''
1983 if access_match.group(3):
1984 slots = access_match.group(3)
1985 error(filename, linenum, 'whitespace/indent', 3,
1986 '%s%s: should be indented +1 space inside %s' % (
1987 access_match.group(2), slots, parent))
erg@google.comd350fe52013-01-14 17:51:48 +00001988
1989 # Consume braces or semicolons from what's left of the line
1990 while True:
1991 # Match first brace, semicolon, or closed parenthesis.
1992 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
1993 if not matched:
1994 break
1995
1996 token = matched.group(1)
1997 if token == '{':
1998 # If namespace or class hasn't seen a opening brace yet, mark
1999 # namespace/class head as complete. Push a new block onto the
2000 # stack otherwise.
2001 if not self.SeenOpenBrace():
2002 self.stack[-1].seen_open_brace = True
2003 else:
2004 self.stack.append(_BlockInfo(True))
2005 if _MATCH_ASM.match(line):
2006 self.stack[-1].inline_asm = _BLOCK_ASM
2007 elif token == ';' or token == ')':
2008 # If we haven't seen an opening brace yet, but we already saw
2009 # a semicolon, this is probably a forward declaration. Pop
2010 # the stack for these.
2011 #
2012 # Similarly, if we haven't seen an opening brace yet, but we
2013 # already saw a closing parenthesis, then these are probably
2014 # function arguments with extra "class" or "struct" keywords.
2015 # Also pop these stack for these.
2016 if not self.SeenOpenBrace():
2017 self.stack.pop()
2018 else: # token == '}'
2019 # Perform end of block checks and pop the stack.
2020 if self.stack:
2021 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2022 self.stack.pop()
2023 line = matched.group(2)
2024
2025 def InnermostClass(self):
2026 """Get class info on the top of the stack.
2027
2028 Returns:
2029 A _ClassInfo object if we are inside a class, or None otherwise.
2030 """
2031 for i in range(len(self.stack), 0, -1):
2032 classinfo = self.stack[i - 1]
2033 if isinstance(classinfo, _ClassInfo):
2034 return classinfo
2035 return None
2036
erg@google.com2aa59982013-10-28 19:09:25 +00002037 def CheckCompletedBlocks(self, filename, error):
2038 """Checks that all classes and namespaces have been completely parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002039
2040 Call this when all lines in a file have been processed.
2041 Args:
2042 filename: The name of the current file.
2043 error: The function to call with any errors found.
2044 """
erg@google.comd350fe52013-01-14 17:51:48 +00002045 # Note: This test can result in false positives if #ifdef constructs
2046 # get in the way of brace matching. See the testBuildClass test in
2047 # cpplint_unittest.py for an example of this.
2048 for obj in self.stack:
2049 if isinstance(obj, _ClassInfo):
2050 error(filename, obj.starting_linenum, 'build/class', 5,
2051 'Failed to find complete declaration of class %s' %
2052 obj.name)
erg@google.com2aa59982013-10-28 19:09:25 +00002053 elif isinstance(obj, _NamespaceInfo):
2054 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2055 'Failed to find complete declaration of namespace %s' %
2056 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002057
2058
2059def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00002060 nesting_state, error):
erg@google.com2aa59982013-10-28 19:09:25 +00002061 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002062
2063 Complain about several constructs which gcc-2 accepts, but which are
2064 not standard C++. Warning about these in lint is one way to ease the
2065 transition to new compilers.
2066 - put storage class first (e.g. "static const" instead of "const static").
2067 - "%lld" instead of %qd" in printf-type functions.
2068 - "%1$d" is non-standard in printf-type functions.
2069 - "\%" is an undefined character escape sequence.
2070 - text after #endif is not allowed.
2071 - invalid inner-style forward declaration.
2072 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002073
erg@google.coma868d2d2009-10-09 21:18:45 +00002074 Additionally, check for constructor/destructor style violations and reference
2075 members, as it is very convenient to do so while checking for
2076 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002077
2078 Args:
2079 filename: The name of the current file.
2080 clean_lines: A CleansedLines instance containing the file.
2081 linenum: The number of the line to check.
erg@google.comd350fe52013-01-14 17:51:48 +00002082 nesting_state: A _NestingState instance which maintains information about
2083 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002084 error: A callable to which errors are reported, which takes 4 arguments:
2085 filename, line number, error level, and message
2086 """
2087
2088 # Remove comments from the line, but leave in strings for now.
2089 line = clean_lines.lines[linenum]
2090
2091 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2092 error(filename, linenum, 'runtime/printf_format', 3,
2093 '%q in format strings is deprecated. Use %ll instead.')
2094
2095 if Search(r'printf\s*\(.*".*%\d+\$', line):
2096 error(filename, linenum, 'runtime/printf_format', 2,
2097 '%N$ formats are unconventional. Try rewriting to avoid them.')
2098
2099 # Remove escaped backslashes before looking for undefined escapes.
2100 line = line.replace('\\\\', '')
2101
2102 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2103 error(filename, linenum, 'build/printf_format', 3,
2104 '%, [, (, and { are undefined character escapes. Unescape them.')
2105
2106 # For the rest, work with both comments and strings removed.
2107 line = clean_lines.elided[linenum]
2108
2109 if Search(r'\b(const|volatile|void|char|short|int|long'
2110 r'|float|double|signed|unsigned'
2111 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00002112 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00002113 line):
2114 error(filename, linenum, 'build/storage_class', 5,
2115 'Storage class (static, extern, typedef, etc) should be first.')
2116
2117 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2118 error(filename, linenum, 'build/endif_comment', 5,
2119 'Uncommented text after #endif is non-standard. Use a comment.')
2120
2121 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2122 error(filename, linenum, 'build/forward_decl', 5,
2123 'Inner-style forward declarations are invalid. Remove this line.')
2124
2125 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2126 line):
2127 error(filename, linenum, 'build/deprecated', 3,
2128 '>? and <? (max and min) operators are non-standard and deprecated.')
2129
erg@google.coma868d2d2009-10-09 21:18:45 +00002130 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2131 # TODO(unknown): Could it be expanded safely to arbitrary references,
2132 # without triggering too many false positives? The first
2133 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2134 # the restriction.
2135 # Here's the original regexp, for the reference:
2136 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2137 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2138 error(filename, linenum, 'runtime/member_string_references', 2,
2139 'const string& members are dangerous. It is much better to use '
2140 'alternatives, such as pointers or simple constants.')
2141
erg@google.comd350fe52013-01-14 17:51:48 +00002142 # Everything else in this function operates on class declarations.
2143 # Return early if the top of the nesting stack is not a class, or if
2144 # the class head is not completed yet.
2145 classinfo = nesting_state.InnermostClass()
2146 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002147 return
2148
erg@google.com4e00b9a2009-01-12 23:05:11 +00002149 # The class may have been declared with namespace or classname qualifiers.
2150 # The constructor and destructor will not have those qualifiers.
2151 base_classname = classinfo.name.split('::')[-1]
2152
2153 # Look for single-argument constructors that aren't marked explicit.
2154 # Technically a valid construct, but against style.
erg@google.com8a95ecc2011-09-08 00:45:54 +00002155 args = Match(r'\s+(?:inline\s+)?%s\s*\(([^,()]+)\)'
erg@google.com4e00b9a2009-01-12 23:05:11 +00002156 % re.escape(base_classname),
2157 line)
2158 if (args and
2159 args.group(1) != 'void' and
erg@google.comfd5da632013-10-25 17:39:45 +00002160 not Match(r'(const\s+)?%s(\s+const)?\s*(?:<\w+>\s*)?&'
2161 % re.escape(base_classname), args.group(1).strip())):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002162 error(filename, linenum, 'runtime/explicit', 5,
2163 'Single-argument constructors should be marked explicit.')
2164
erg@google.com4e00b9a2009-01-12 23:05:11 +00002165
2166def CheckSpacingForFunctionCall(filename, line, linenum, error):
2167 """Checks for the correctness of various spacing around function calls.
2168
2169 Args:
2170 filename: The name of the current file.
2171 line: The text of the line to check.
2172 linenum: The number of the line to check.
2173 error: The function to call with any errors found.
2174 """
2175
2176 # Since function calls often occur inside if/for/while/switch
2177 # expressions - which have their own, more liberal conventions - we
2178 # first see if we should be looking inside such an expression for a
2179 # function call, to which we can apply more strict standards.
2180 fncall = line # if there's no control flow construct, look at whole line
2181 for pattern in (r'\bif\s*\((.*)\)\s*{',
2182 r'\bfor\s*\((.*)\)\s*{',
2183 r'\bwhile\s*\((.*)\)\s*[{;]',
2184 r'\bswitch\s*\((.*)\)\s*{'):
2185 match = Search(pattern, line)
2186 if match:
2187 fncall = match.group(1) # look inside the parens for function calls
2188 break
2189
2190 # Except in if/for/while/switch, there should never be space
2191 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2192 # for nested parens ( (a+b) + c ). Likewise, there should never be
2193 # a space before a ( when it's a function argument. I assume it's a
2194 # function argument when the char before the whitespace is legal in
2195 # a function name (alnum + _) and we're not starting a macro. Also ignore
2196 # pointers and references to arrays and functions coz they're too tricky:
2197 # we use a very simple way to recognize these:
2198 # " (something)(maybe-something)" or
2199 # " (something)(maybe-something," or
2200 # " (something)[something]"
2201 # Note that we assume the contents of [] to be short enough that
2202 # they'll never need to wrap.
2203 if ( # Ignore control structures.
erg@google.com2aa59982013-10-28 19:09:25 +00002204 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
erg@google.comc6671232013-10-25 21:44:03 +00002205 fncall) and
erg@google.com4e00b9a2009-01-12 23:05:11 +00002206 # Ignore pointers/references to functions.
2207 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2208 # Ignore pointers/references to arrays.
2209 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00002210 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00002211 error(filename, linenum, 'whitespace/parens', 4,
2212 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00002213 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002214 error(filename, linenum, 'whitespace/parens', 2,
2215 'Extra space after (')
2216 if (Search(r'\w\s+\(', fncall) and
erg@google.comd350fe52013-01-14 17:51:48 +00002217 not Search(r'#\s*define|typedef', fncall) and
erg@google.com2aa59982013-10-28 19:09:25 +00002218 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002219 error(filename, linenum, 'whitespace/parens', 4,
2220 'Extra space before ( in function call')
2221 # If the ) is followed only by a newline or a { + newline, assume it's
2222 # part of a control statement (if/while/etc), and don't complain
2223 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002224 # If the closing parenthesis is preceded by only whitespaces,
2225 # try to give a more descriptive error message.
2226 if Search(r'^\s+\)', fncall):
2227 error(filename, linenum, 'whitespace/parens', 2,
2228 'Closing ) should be moved to the previous line')
2229 else:
2230 error(filename, linenum, 'whitespace/parens', 2,
2231 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002232
2233
2234def IsBlankLine(line):
2235 """Returns true if the given line is blank.
2236
2237 We consider a line to be blank if the line is empty or consists of
2238 only white spaces.
2239
2240 Args:
2241 line: A line of a string.
2242
2243 Returns:
2244 True, if the given line is blank.
2245 """
2246 return not line or line.isspace()
2247
2248
2249def CheckForFunctionLengths(filename, clean_lines, linenum,
2250 function_state, error):
2251 """Reports for long function bodies.
2252
2253 For an overview why this is done, see:
2254 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
2255
2256 Uses a simplistic algorithm assuming other style guidelines
2257 (especially spacing) are followed.
2258 Only checks unindented functions, so class members are unchecked.
2259 Trivial bodies are unchecked, so constructors with huge initializer lists
2260 may be missed.
2261 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00002262 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002263 NOLINT *on the last line of a function* disables this check.
2264
2265 Args:
2266 filename: The name of the current file.
2267 clean_lines: A CleansedLines instance containing the file.
2268 linenum: The number of the line to check.
2269 function_state: Current function name and lines in body so far.
2270 error: The function to call with any errors found.
2271 """
2272 lines = clean_lines.lines
2273 line = lines[linenum]
2274 raw = clean_lines.raw_lines
2275 raw_line = raw[linenum]
2276 joined_line = ''
2277
2278 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00002279 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00002280 match_result = Match(regexp, line)
2281 if match_result:
2282 # If the name is all caps and underscores, figure it's a macro and
2283 # ignore it, unless it's TEST or TEST_F.
2284 function_name = match_result.group(1).split()[-1]
2285 if function_name == 'TEST' or function_name == 'TEST_F' or (
2286 not Match(r'[A-Z_]+$', function_name)):
2287 starting_func = True
2288
2289 if starting_func:
2290 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00002291 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002292 start_line = lines[start_linenum]
2293 joined_line += ' ' + start_line.lstrip()
2294 if Search(r'(;|})', start_line): # Declarations and trivial functions
2295 body_found = True
2296 break # ... ignore
2297 elif Search(r'{', start_line):
2298 body_found = True
2299 function = Search(r'((\w|:)*)\(', line).group(1)
2300 if Match(r'TEST', function): # Handle TEST... macros
2301 parameter_regexp = Search(r'(\(.*\))', joined_line)
2302 if parameter_regexp: # Ignore bad syntax
2303 function += parameter_regexp.group(1)
2304 else:
2305 function += '()'
2306 function_state.Begin(function)
2307 break
2308 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00002309 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002310 error(filename, linenum, 'readability/fn_size', 5,
2311 'Lint failed to find start of function body.')
2312 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00002313 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002314 function_state.End()
2315 elif not Match(r'^\s*$', line):
2316 function_state.Count() # Count non-blank/non-comment lines.
2317
2318
2319_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
2320
2321
2322def CheckComment(comment, filename, linenum, error):
2323 """Checks for common mistakes in TODO comments.
2324
2325 Args:
2326 comment: The text of the comment from the line in question.
2327 filename: The name of the current file.
2328 linenum: The number of the line to check.
2329 error: The function to call with any errors found.
2330 """
2331 match = _RE_PATTERN_TODO.match(comment)
2332 if match:
2333 # One whitespace is correct; zero whitespace is handled elsewhere.
2334 leading_whitespace = match.group(1)
2335 if len(leading_whitespace) > 1:
2336 error(filename, linenum, 'whitespace/todo', 2,
2337 'Too many spaces before TODO')
2338
2339 username = match.group(2)
2340 if not username:
2341 error(filename, linenum, 'readability/todo', 2,
2342 'Missing username in TODO; it should look like '
2343 '"// TODO(my_username): Stuff."')
2344
2345 middle_whitespace = match.group(3)
erg@google.com2aa59982013-10-28 19:09:25 +00002346 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
erg@google.com4e00b9a2009-01-12 23:05:11 +00002347 if middle_whitespace != ' ' and middle_whitespace != '':
2348 error(filename, linenum, 'whitespace/todo', 2,
2349 'TODO(my_username) should be followed by a space')
2350
erg@google.comd350fe52013-01-14 17:51:48 +00002351def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
2352 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002353
erg@google.comd350fe52013-01-14 17:51:48 +00002354 Args:
2355 filename: The name of the current file.
2356 clean_lines: A CleansedLines instance containing the file.
2357 linenum: The number of the line to check.
2358 nesting_state: A _NestingState instance which maintains information about
2359 the current stack of nested blocks being parsed.
2360 error: The function to call with any errors found.
2361 """
2362 line = clean_lines.elided[linenum] # get rid of comments and strings
2363
2364 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
2365 r'DISALLOW_EVIL_CONSTRUCTORS|'
2366 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
2367 if not matched:
2368 return
2369 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
2370 if nesting_state.stack[-1].access != 'private':
2371 error(filename, linenum, 'readability/constructors', 3,
2372 '%s must be in the private: section' % matched.group(1))
2373
2374 else:
2375 # Found DISALLOW* macro outside a class declaration, or perhaps it
2376 # was used inside a function when it should have been part of the
2377 # class declaration. We could issue a warning here, but it
2378 # probably resulted in a compiler error already.
2379 pass
2380
2381
2382def FindNextMatchingAngleBracket(clean_lines, linenum, init_suffix):
2383 """Find the corresponding > to close a template.
2384
2385 Args:
2386 clean_lines: A CleansedLines instance containing the file.
2387 linenum: Current line number.
2388 init_suffix: Remainder of the current line after the initial <.
2389
2390 Returns:
2391 True if a matching bracket exists.
2392 """
2393 line = init_suffix
2394 nesting_stack = ['<']
2395 while True:
2396 # Find the next operator that can tell us whether < is used as an
2397 # opening bracket or as a less-than operator. We only want to
2398 # warn on the latter case.
2399 #
2400 # We could also check all other operators and terminate the search
2401 # early, e.g. if we got something like this "a<b+c", the "<" is
2402 # most likely a less-than operator, but then we will get false
erg@google.comc6671232013-10-25 21:44:03 +00002403 # positives for default arguments and other template expressions.
erg@google.comd350fe52013-01-14 17:51:48 +00002404 match = Search(r'^[^<>(),;\[\]]*([<>(),;\[\]])(.*)$', line)
2405 if match:
2406 # Found an operator, update nesting stack
2407 operator = match.group(1)
2408 line = match.group(2)
2409
2410 if nesting_stack[-1] == '<':
2411 # Expecting closing angle bracket
2412 if operator in ('<', '(', '['):
2413 nesting_stack.append(operator)
2414 elif operator == '>':
2415 nesting_stack.pop()
2416 if not nesting_stack:
2417 # Found matching angle bracket
2418 return True
2419 elif operator == ',':
2420 # Got a comma after a bracket, this is most likely a template
2421 # argument. We have not seen a closing angle bracket yet, but
2422 # it's probably a few lines later if we look for it, so just
2423 # return early here.
2424 return True
2425 else:
2426 # Got some other operator.
2427 return False
2428
2429 else:
2430 # Expecting closing parenthesis or closing bracket
2431 if operator in ('<', '(', '['):
2432 nesting_stack.append(operator)
2433 elif operator in (')', ']'):
2434 # We don't bother checking for matching () or []. If we got
2435 # something like (] or [), it would have been a syntax error.
2436 nesting_stack.pop()
2437
2438 else:
2439 # Scan the next line
2440 linenum += 1
2441 if linenum >= len(clean_lines.elided):
2442 break
2443 line = clean_lines.elided[linenum]
2444
2445 # Exhausted all remaining lines and still no matching angle bracket.
2446 # Most likely the input was incomplete, otherwise we should have
2447 # seen a semicolon and returned early.
2448 return True
2449
2450
2451def FindPreviousMatchingAngleBracket(clean_lines, linenum, init_prefix):
2452 """Find the corresponding < that started a template.
2453
2454 Args:
2455 clean_lines: A CleansedLines instance containing the file.
2456 linenum: Current line number.
2457 init_prefix: Part of the current line before the initial >.
2458
2459 Returns:
2460 True if a matching bracket exists.
2461 """
2462 line = init_prefix
2463 nesting_stack = ['>']
2464 while True:
2465 # Find the previous operator
2466 match = Search(r'^(.*)([<>(),;\[\]])[^<>(),;\[\]]*$', line)
2467 if match:
2468 # Found an operator, update nesting stack
2469 operator = match.group(2)
2470 line = match.group(1)
2471
2472 if nesting_stack[-1] == '>':
2473 # Expecting opening angle bracket
2474 if operator in ('>', ')', ']'):
2475 nesting_stack.append(operator)
2476 elif operator == '<':
2477 nesting_stack.pop()
2478 if not nesting_stack:
2479 # Found matching angle bracket
2480 return True
2481 elif operator == ',':
2482 # Got a comma before a bracket, this is most likely a
2483 # template argument. The opening angle bracket is probably
2484 # there if we look for it, so just return early here.
2485 return True
2486 else:
2487 # Got some other operator.
2488 return False
2489
2490 else:
2491 # Expecting opening parenthesis or opening bracket
2492 if operator in ('>', ')', ']'):
2493 nesting_stack.append(operator)
2494 elif operator in ('(', '['):
2495 nesting_stack.pop()
2496
2497 else:
2498 # Scan the previous line
2499 linenum -= 1
2500 if linenum < 0:
2501 break
2502 line = clean_lines.elided[linenum]
2503
2504 # Exhausted all earlier lines and still no matching angle bracket.
2505 return False
2506
2507
2508def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002509 """Checks for the correctness of various spacing issues in the code.
2510
2511 Things we check for: spaces around operators, spaces after
2512 if/for/while/switch, no spaces around parens in function calls, two
2513 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00002514 line, don't end a function with a blank line, don't add a blank line
2515 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002516
2517 Args:
2518 filename: The name of the current file.
2519 clean_lines: A CleansedLines instance containing the file.
2520 linenum: The number of the line to check.
erg@google.comd350fe52013-01-14 17:51:48 +00002521 nesting_state: A _NestingState instance which maintains information about
2522 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002523 error: The function to call with any errors found.
2524 """
2525
erg@google.com2aa59982013-10-28 19:09:25 +00002526 # Don't use "elided" lines here, otherwise we can't check commented lines.
2527 # Don't want to use "raw" either, because we don't want to check inside C++11
2528 # raw strings,
2529 raw = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00002530 line = raw[linenum]
2531
2532 # Before nixing comments, check if the line is blank for no good
2533 # reason. This includes the first line after a block is opened, and
2534 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00002535 #
2536 # Skip all the blank line checks if we are immediately inside a
2537 # namespace body. In other words, don't issue blank line warnings
2538 # for this block:
2539 # namespace {
2540 #
2541 # }
2542 #
2543 # A warning about missing end of namespace comments will be issued instead.
2544 if IsBlankLine(line) and not nesting_state.InNamespaceBody():
erg@google.com4e00b9a2009-01-12 23:05:11 +00002545 elided = clean_lines.elided
2546 prev_line = elided[linenum - 1]
2547 prevbrace = prev_line.rfind('{')
2548 # TODO(unknown): Don't complain if line before blank line, and line after,
2549 # both start with alnums and are indented the same amount.
2550 # This ignores whitespace at the start of a namespace block
2551 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00002552 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002553 # OK, we have a blank line at the start of a code block. Before we
2554 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00002555 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00002556 # 4 spaces (because they did not fit in a 80 column line when placed on
2557 # the same line as the function name). We also check for the case where
2558 # the previous line is indented 6 spaces, which may happen when the
2559 # initializers of a constructor do not fit into a 80 column line.
2560 exception = False
2561 if Match(r' {6}\w', prev_line): # Initializer list?
2562 # We are looking for the opening column of initializer list, which
2563 # should be indented 4 spaces to cause 6 space indentation afterwards.
2564 search_position = linenum-2
2565 while (search_position >= 0
2566 and Match(r' {6}\w', elided[search_position])):
2567 search_position -= 1
2568 exception = (search_position >= 0
2569 and elided[search_position][:5] == ' :')
2570 else:
2571 # Search for the function arguments or an initializer list. We use a
2572 # simple heuristic here: If the line is indented 4 spaces; and we have a
2573 # closing paren, without the opening paren, followed by an opening brace
2574 # or colon (for initializer lists) we assume that it is the last line of
2575 # a function header. If we have a colon indented 4 spaces, it is an
2576 # initializer list.
2577 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
2578 prev_line)
2579 or Match(r' {4}:', prev_line))
2580
2581 if not exception:
2582 error(filename, linenum, 'whitespace/blank_line', 2,
erg@google.com2aa59982013-10-28 19:09:25 +00002583 'Redundant blank line at the start of a code block '
2584 'should be deleted.')
erg@google.comd350fe52013-01-14 17:51:48 +00002585 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00002586 # chain, like this:
2587 # if (condition1) {
2588 # // Something followed by a blank line
2589 #
2590 # } else if (condition2) {
2591 # // Something else
2592 # }
2593 if linenum + 1 < clean_lines.NumLines():
2594 next_line = raw[linenum + 1]
2595 if (next_line
2596 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002597 and next_line.find('} else ') == -1):
2598 error(filename, linenum, 'whitespace/blank_line', 3,
erg@google.com2aa59982013-10-28 19:09:25 +00002599 'Redundant blank line at the end of a code block '
2600 'should be deleted.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002601
erg@google.com8a95ecc2011-09-08 00:45:54 +00002602 matched = Match(r'\s*(public|protected|private):', prev_line)
2603 if matched:
2604 error(filename, linenum, 'whitespace/blank_line', 3,
2605 'Do not leave a blank line after "%s:"' % matched.group(1))
2606
erg@google.com4e00b9a2009-01-12 23:05:11 +00002607 # Next, we complain if there's a comment too near the text
2608 commentpos = line.find('//')
2609 if commentpos != -1:
2610 # Check if the // may be in quotes. If so, ignore it
erg@google.com2aa59982013-10-28 19:09:25 +00002611 # Comparisons made explicit for clarity -- pylint: disable=g-explicit-bool-comparison
erg@google.com4e00b9a2009-01-12 23:05:11 +00002612 if (line.count('"', 0, commentpos) -
2613 line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes
2614 # Allow one space for new scopes, two spaces otherwise:
2615 if (not Match(r'^\s*{ //', line) and
2616 ((commentpos >= 1 and
2617 line[commentpos-1] not in string.whitespace) or
2618 (commentpos >= 2 and
2619 line[commentpos-2] not in string.whitespace))):
2620 error(filename, linenum, 'whitespace/comments', 2,
2621 'At least two spaces is best between code and comments')
2622 # There should always be a space between the // and the comment
2623 commentend = commentpos + 2
2624 if commentend < len(line) and not line[commentend] == ' ':
2625 # but some lines are exceptions -- e.g. if they're big
2626 # comment delimiters like:
2627 # //----------------------------------------------------------
erg@google.coma51c16b2010-11-17 18:09:31 +00002628 # or are an empty C++ style Doxygen comment, like:
2629 # ///
erg@google.com6d8d9832013-10-31 19:46:18 +00002630 # or C++ style Doxygen comments placed after the variable:
2631 # ///< Header comment
2632 # //!< Header comment
erg@google.come35f7652009-06-19 20:52:09 +00002633 # or they begin with multiple slashes followed by a space:
2634 # //////// Header comment
2635 match = (Search(r'[=/-]{4,}\s*$', line[commentend:]) or
erg@google.coma51c16b2010-11-17 18:09:31 +00002636 Search(r'^/$', line[commentend:]) or
erg@google.com6d8d9832013-10-31 19:46:18 +00002637 Search(r'^!< ', line[commentend:]) or
2638 Search(r'^/< ', line[commentend:]) or
erg@google.come35f7652009-06-19 20:52:09 +00002639 Search(r'^/+ ', line[commentend:]))
erg@google.com4e00b9a2009-01-12 23:05:11 +00002640 if not match:
2641 error(filename, linenum, 'whitespace/comments', 4,
2642 'Should have a space between // and comment')
2643 CheckComment(line[commentpos:], filename, linenum, error)
2644
2645 line = clean_lines.elided[linenum] # get rid of comments and strings
2646
2647 # Don't try to do spacing checks for operator methods
2648 line = re.sub(r'operator(==|!=|<|<<|<=|>=|>>|>)\(', 'operator\(', line)
2649
2650 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
2651 # Otherwise not. Note we only check for non-spaces on *both* sides;
2652 # sometimes people put non-spaces on one side when aligning ='s among
2653 # many lines (not that this is behavior that I approve of...)
2654 if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line):
2655 error(filename, linenum, 'whitespace/operators', 4,
2656 'Missing spaces around =')
2657
2658 # It's ok not to have spaces around binary operators like + - * /, but if
2659 # there's too little whitespace, we get concerned. It's hard to tell,
2660 # though, so we punt on this one for now. TODO.
2661
2662 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00002663 #
2664 # Check <= and >= first to avoid false positives with < and >, then
2665 # check non-include lines for spacing around < and >.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002666 match = Search(r'[^<>=!\s](==|!=|<=|>=)[^<>=!\s]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002667 if match:
2668 error(filename, linenum, 'whitespace/operators', 3,
2669 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00002670 # We allow no-spaces around << when used like this: 10<<20, but
erg@google.com4e00b9a2009-01-12 23:05:11 +00002671 # not otherwise (particularly, not when used as streams)
erg@google.comd350fe52013-01-14 17:51:48 +00002672 match = Search(r'(\S)(?:L|UL|ULL|l|ul|ull)?<<(\S)', line)
2673 if match and not (match.group(1).isdigit() and match.group(2).isdigit()):
2674 error(filename, linenum, 'whitespace/operators', 3,
2675 'Missing spaces around <<')
2676 elif not Match(r'#.*include', line):
2677 # Avoid false positives on ->
2678 reduced_line = line.replace('->', '')
2679
2680 # Look for < that is not surrounded by spaces. This is only
2681 # triggered if both sides are missing spaces, even though
2682 # technically should should flag if at least one side is missing a
2683 # space. This is done to avoid some false positives with shifts.
2684 match = Search(r'[^\s<]<([^\s=<].*)', reduced_line)
2685 if (match and
2686 not FindNextMatchingAngleBracket(clean_lines, linenum, match.group(1))):
2687 error(filename, linenum, 'whitespace/operators', 3,
2688 'Missing spaces around <')
2689
2690 # Look for > that is not surrounded by spaces. Similar to the
2691 # above, we only trigger if both sides are missing spaces to avoid
2692 # false positives with shifts.
2693 match = Search(r'^(.*[^\s>])>[^\s=>]', reduced_line)
2694 if (match and
2695 not FindPreviousMatchingAngleBracket(clean_lines, linenum,
2696 match.group(1))):
2697 error(filename, linenum, 'whitespace/operators', 3,
2698 'Missing spaces around >')
2699
2700 # We allow no-spaces around >> for almost anything. This is because
2701 # C++11 allows ">>" to close nested templates, which accounts for
2702 # most cases when ">>" is not followed by a space.
2703 #
2704 # We still warn on ">>" followed by alpha character, because that is
2705 # likely due to ">>" being used for right shifts, e.g.:
2706 # value >> alpha
2707 #
2708 # When ">>" is used to close templates, the alphanumeric letter that
2709 # follows would be part of an identifier, and there should still be
2710 # a space separating the template type and the identifier.
2711 # type<type<type>> alpha
2712 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002713 if match:
2714 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00002715 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002716
2717 # There shouldn't be space around unary operators
2718 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
2719 if match:
2720 error(filename, linenum, 'whitespace/operators', 4,
2721 'Extra space for operator %s' % match.group(1))
2722
2723 # A pet peeve of mine: no spaces after an if, while, switch, or for
2724 match = Search(r' (if\(|for\(|while\(|switch\()', line)
2725 if match:
2726 error(filename, linenum, 'whitespace/parens', 5,
2727 'Missing space before ( in %s' % match.group(1))
2728
2729 # For if/for/while/switch, the left and right parens should be
2730 # consistent about how many spaces are inside the parens, and
2731 # there should either be zero or one spaces inside the parens.
2732 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00002733 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002734 match = Search(r'\b(if|for|while|switch)\s*'
2735 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
2736 line)
2737 if match:
2738 if len(match.group(2)) != len(match.group(4)):
2739 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00002740 len(match.group(2)) == 1 + len(match.group(4)) or
2741 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002742 error(filename, linenum, 'whitespace/parens', 5,
2743 'Mismatching spaces inside () in %s' % match.group(1))
erg@google.comc6671232013-10-25 21:44:03 +00002744 if len(match.group(2)) not in [0, 1]:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002745 error(filename, linenum, 'whitespace/parens', 5,
2746 'Should have zero or one spaces inside ( and ) in %s' %
2747 match.group(1))
2748
2749 # You should always have a space after a comma (either as fn arg or operator)
erg@google.comc6671232013-10-25 21:44:03 +00002750 #
2751 # This does not apply when the non-space character following the
2752 # comma is another comma, since the only time when that happens is
2753 # for empty macro arguments.
erg@google.com2aa59982013-10-28 19:09:25 +00002754 #
2755 # We run this check in two passes: first pass on elided lines to
2756 # verify that lines contain missing whitespaces, second pass on raw
2757 # lines to confirm that those missing whitespaces are not due to
2758 # elided comments.
2759 if Search(r',[^,\s]', line) and Search(r',[^,\s]', raw[linenum]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002760 error(filename, linenum, 'whitespace/comma', 3,
2761 'Missing space after ,')
2762
erg@google.comd7d27472011-09-07 17:36:35 +00002763 # You should always have a space after a semicolon
2764 # except for few corner cases
2765 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
2766 # space after ;
2767 if Search(r';[^\s};\\)/]', line):
2768 error(filename, linenum, 'whitespace/semicolon', 3,
2769 'Missing space after ;')
2770
erg@google.com4e00b9a2009-01-12 23:05:11 +00002771 # Next we will look for issues with function calls.
2772 CheckSpacingForFunctionCall(filename, line, linenum, error)
2773
erg@google.com8a95ecc2011-09-08 00:45:54 +00002774 # Except after an opening paren, or after another opening brace (in case of
2775 # an initializer list, for instance), you should have spaces before your
2776 # braces. And since you should never have braces at the beginning of a line,
2777 # this is an easy test.
erg@google.com2aa59982013-10-28 19:09:25 +00002778 match = Match(r'^(.*[^ ({]){', line)
2779 if match:
2780 # Try a bit harder to check for brace initialization. This
2781 # happens in one of the following forms:
2782 # Constructor() : initializer_list_{} { ... }
2783 # Constructor{}.MemberFunction()
2784 # Type variable{};
2785 # FunctionCall(type{}, ...);
2786 # LastArgument(..., type{});
2787 # LOG(INFO) << type{} << " ...";
2788 # map_of_type[{...}] = ...;
2789 #
2790 # We check for the character following the closing brace, and
2791 # silence the warning if it's one of those listed above, i.e.
2792 # "{.;,)<]".
2793 #
2794 # To account for nested initializer list, we allow any number of
2795 # closing braces up to "{;,)<". We can't simply silence the
2796 # warning on first sight of closing brace, because that would
2797 # cause false negatives for things that are not initializer lists.
2798 # Silence this: But not this:
2799 # Outer{ if (...) {
2800 # Inner{...} if (...){ // Missing space before {
2801 # }; }
2802 #
2803 # There is a false negative with this approach if people inserted
2804 # spurious semicolons, e.g. "if (cond){};", but we will catch the
2805 # spurious semicolon with a separate check.
2806 (endline, endlinenum, endpos) = CloseExpression(
2807 clean_lines, linenum, len(match.group(1)))
2808 trailing_text = ''
2809 if endpos > -1:
2810 trailing_text = endline[endpos:]
2811 for offset in xrange(endlinenum + 1,
2812 min(endlinenum + 3, clean_lines.NumLines() - 1)):
2813 trailing_text += clean_lines.elided[offset]
2814 if not Match(r'^[\s}]*[{.;,)<\]]', trailing_text):
2815 error(filename, linenum, 'whitespace/braces', 5,
2816 'Missing space before {')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002817
2818 # Make sure '} else {' has spaces.
2819 if Search(r'}else', line):
2820 error(filename, linenum, 'whitespace/braces', 5,
2821 'Missing space before else')
2822
2823 # You shouldn't have spaces before your brackets, except maybe after
2824 # 'delete []' or 'new char * []'.
2825 if Search(r'\w\s+\[', line) and not Search(r'delete\s+\[', line):
2826 error(filename, linenum, 'whitespace/braces', 5,
2827 'Extra space before [')
2828
2829 # You shouldn't have a space before a semicolon at the end of the line.
2830 # There's a special case for "for" since the style guide allows space before
2831 # the semicolon there.
2832 if Search(r':\s*;\s*$', line):
2833 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00002834 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002835 elif Search(r'^\s*;\s*$', line):
2836 error(filename, linenum, 'whitespace/semicolon', 5,
2837 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00002838 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002839 elif (Search(r'\s+;\s*$', line) and
2840 not Search(r'\bfor\b', line)):
2841 error(filename, linenum, 'whitespace/semicolon', 5,
2842 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00002843 'statement, use {} instead.')
2844
2845 # In range-based for, we wanted spaces before and after the colon, but
2846 # not around "::" tokens that might appear.
2847 if (Search('for *\(.*[^:]:[^: ]', line) or
2848 Search('for *\(.*[^: ]:[^:]', line)):
2849 error(filename, linenum, 'whitespace/forcolon', 2,
2850 'Missing space around colon in range-based for loop')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002851
2852
erg@google.com8a95ecc2011-09-08 00:45:54 +00002853def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
2854 """Checks for additional blank line issues related to sections.
2855
2856 Currently the only thing checked here is blank line before protected/private.
2857
2858 Args:
2859 filename: The name of the current file.
2860 clean_lines: A CleansedLines instance containing the file.
2861 class_info: A _ClassInfo objects.
2862 linenum: The number of the line to check.
2863 error: The function to call with any errors found.
2864 """
2865 # Skip checks if the class is small, where small means 25 lines or less.
2866 # 25 lines seems like a good cutoff since that's the usual height of
2867 # terminals, and any class that can't fit in one screen can't really
2868 # be considered "small".
2869 #
2870 # Also skip checks if we are on the first line. This accounts for
2871 # classes that look like
2872 # class Foo { public: ... };
2873 #
2874 # If we didn't find the end of the class, last_line would be zero,
2875 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00002876 if (class_info.last_line - class_info.starting_linenum <= 24 or
2877 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002878 return
2879
2880 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
2881 if matched:
2882 # Issue warning if the line before public/protected/private was
2883 # not a blank line, but don't do this if the previous line contains
2884 # "class" or "struct". This can happen two ways:
2885 # - We are at the beginning of the class.
2886 # - We are forward-declaring an inner class that is semantically
2887 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00002888 # Also ignores cases where the previous line ends with a backslash as can be
2889 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00002890 prev_line = clean_lines.lines[linenum - 1]
2891 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00002892 not Search(r'\b(class|struct)\b', prev_line) and
2893 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002894 # Try a bit harder to find the beginning of the class. This is to
2895 # account for multi-line base-specifier lists, e.g.:
2896 # class Derived
2897 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00002898 end_class_head = class_info.starting_linenum
2899 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002900 if Search(r'\{\s*$', clean_lines.lines[i]):
2901 end_class_head = i
2902 break
2903 if end_class_head < linenum - 1:
2904 error(filename, linenum, 'whitespace/blank_line', 3,
2905 '"%s:" should be preceded by a blank line' % matched.group(1))
2906
2907
erg@google.com4e00b9a2009-01-12 23:05:11 +00002908def GetPreviousNonBlankLine(clean_lines, linenum):
2909 """Return the most recent non-blank line and its line number.
2910
2911 Args:
2912 clean_lines: A CleansedLines instance containing the file contents.
2913 linenum: The number of the line to check.
2914
2915 Returns:
2916 A tuple with two elements. The first element is the contents of the last
2917 non-blank line before the current line, or the empty string if this is the
2918 first non-blank line. The second is the line number of that line, or -1
2919 if this is the first non-blank line.
2920 """
2921
2922 prevlinenum = linenum - 1
2923 while prevlinenum >= 0:
2924 prevline = clean_lines.elided[prevlinenum]
2925 if not IsBlankLine(prevline): # if not a blank line...
2926 return (prevline, prevlinenum)
2927 prevlinenum -= 1
2928 return ('', -1)
2929
2930
2931def CheckBraces(filename, clean_lines, linenum, error):
2932 """Looks for misplaced braces (e.g. at the end of line).
2933
2934 Args:
2935 filename: The name of the current file.
2936 clean_lines: A CleansedLines instance containing the file.
2937 linenum: The number of the line to check.
2938 error: The function to call with any errors found.
2939 """
2940
2941 line = clean_lines.elided[linenum] # get rid of comments and strings
2942
2943 if Match(r'\s*{\s*$', line):
erg@google.com2aa59982013-10-28 19:09:25 +00002944 # We allow an open brace to start a line in the case where someone is using
2945 # braces in a block to explicitly create a new scope, which is commonly used
2946 # to control the lifetime of stack-allocated variables. Braces are also
2947 # used for brace initializers inside function calls. We don't detect this
2948 # perfectly: we just don't complain if the last non-whitespace character on
2949 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
erg@google.comc6671232013-10-25 21:44:03 +00002950 # previous line starts a preprocessor block.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002951 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.com2aa59982013-10-28 19:09:25 +00002952 if (not Search(r'[,;:}{(]\s*$', prevline) and
erg@google.comd350fe52013-01-14 17:51:48 +00002953 not Match(r'\s*#', prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002954 error(filename, linenum, 'whitespace/braces', 4,
2955 '{ should almost always be at the end of the previous line')
2956
2957 # An else clause should be on the same line as the preceding closing brace.
2958 if Match(r'\s*else\s*', line):
2959 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
2960 if Match(r'\s*}\s*$', prevline):
2961 error(filename, linenum, 'whitespace/newline', 4,
2962 'An else should appear on the same line as the preceding }')
2963
2964 # If braces come on one side of an else, they should be on both.
2965 # However, we have to worry about "else if" that spans multiple lines!
2966 if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
2967 if Search(r'}\s*else if([^{]*)$', line): # could be multi-line if
2968 # find the ( after the if
2969 pos = line.find('else if')
2970 pos = line.find('(', pos)
2971 if pos > 0:
2972 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
2973 if endline[endpos:].find('{') == -1: # must be brace after if
2974 error(filename, linenum, 'readability/braces', 5,
2975 'If an else has a brace on one side, it should have it on both')
2976 else: # common case: else not followed by a multi-line if
2977 error(filename, linenum, 'readability/braces', 5,
2978 'If an else has a brace on one side, it should have it on both')
2979
2980 # Likewise, an else should never have the else clause on the same line
2981 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
2982 error(filename, linenum, 'whitespace/newline', 4,
2983 'Else clause should never be on same line as else (use 2 lines)')
2984
2985 # In the same way, a do/while should never be on one line
2986 if Match(r'\s*do [^\s{]', line):
2987 error(filename, linenum, 'whitespace/newline', 4,
2988 'do/while clauses should not be on a single line')
2989
erg@google.com2aa59982013-10-28 19:09:25 +00002990 # Block bodies should not be followed by a semicolon. Due to C++11
2991 # brace initialization, there are more places where semicolons are
2992 # required than not, so we use a whitelist approach to check these
2993 # rather than a blacklist. These are the places where "};" should
2994 # be replaced by just "}":
2995 # 1. Some flavor of block following closing parenthesis:
2996 # for (;;) {};
2997 # while (...) {};
2998 # switch (...) {};
2999 # Function(...) {};
3000 # if (...) {};
3001 # if (...) else if (...) {};
3002 #
3003 # 2. else block:
3004 # if (...) else {};
3005 #
3006 # 3. const member function:
3007 # Function(...) const {};
3008 #
3009 # 4. Block following some statement:
3010 # x = 42;
3011 # {};
3012 #
3013 # 5. Block at the beginning of a function:
3014 # Function(...) {
3015 # {};
3016 # }
3017 #
3018 # Note that naively checking for the preceding "{" will also match
3019 # braces inside multi-dimensional arrays, but this is fine since
3020 # that expression will not contain semicolons.
3021 #
3022 # 6. Block following another block:
3023 # while (true) {}
3024 # {};
3025 #
3026 # 7. End of namespaces:
3027 # namespace {};
3028 #
3029 # These semicolons seems far more common than other kinds of
3030 # redundant semicolons, possibly due to people converting classes
3031 # to namespaces. For now we do not warn for this case.
3032 #
3033 # Try matching case 1 first.
3034 match = Match(r'^(.*\)\s*)\{', line)
3035 if match:
3036 # Matched closing parenthesis (case 1). Check the token before the
3037 # matching opening parenthesis, and don't warn if it looks like a
3038 # macro. This avoids these false positives:
3039 # - macro that defines a base class
3040 # - multi-line macro that defines a base class
3041 # - macro that defines the whole class-head
3042 #
3043 # But we still issue warnings for macros that we know are safe to
3044 # warn, specifically:
3045 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3046 # - TYPED_TEST
3047 # - INTERFACE_DEF
3048 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3049 #
3050 # We implement a whitelist of safe macros instead of a blacklist of
3051 # unsafe macros, even though the latter appears less frequently in
3052 # google code and would have been easier to implement. This is because
3053 # the downside for getting the whitelist wrong means some extra
3054 # semicolons, while the downside for getting the blacklist wrong
3055 # would result in compile errors.
3056 #
3057 # In addition to macros, we also don't want to warn on compound
3058 # literals.
3059 closing_brace_pos = match.group(1).rfind(')')
3060 opening_parenthesis = ReverseCloseExpression(
3061 clean_lines, linenum, closing_brace_pos)
3062 if opening_parenthesis[2] > -1:
3063 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
3064 macro = Search(r'\b([A-Z_]+)\s*$', line_prefix)
3065 if ((macro and
3066 macro.group(1) not in (
3067 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3068 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3069 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
3070 Search(r'\s+=\s*$', line_prefix)):
3071 match = None
3072
3073 else:
3074 # Try matching cases 2-3.
3075 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3076 if not match:
3077 # Try matching cases 4-6. These are always matched on separate lines.
3078 #
3079 # Note that we can't simply concatenate the previous line to the
3080 # current line and do a single match, otherwise we may output
3081 # duplicate warnings for the blank line case:
3082 # if (cond) {
3083 # // blank line
3084 # }
3085 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3086 if prevline and Search(r'[;{}]\s*$', prevline):
3087 match = Match(r'^(\s*)\{', line)
3088
3089 # Check matching closing brace
3090 if match:
3091 (endline, endlinenum, endpos) = CloseExpression(
3092 clean_lines, linenum, len(match.group(1)))
3093 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3094 # Current {} pair is eligible for semicolon check, and we have found
3095 # the redundant semicolon, output warning here.
3096 #
3097 # Note: because we are scanning forward for opening braces, and
3098 # outputting warnings for the matching closing brace, if there are
3099 # nested blocks with trailing semicolons, we will get the error
3100 # messages in reversed order.
3101 error(filename, endlinenum, 'readability/braces', 4,
3102 "You don't need a ; after a }")
erg@google.com4e00b9a2009-01-12 23:05:11 +00003103
3104
erg@google.comc6671232013-10-25 21:44:03 +00003105def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3106 """Look for empty loop/conditional body with only a single semicolon.
erg@google.comd350fe52013-01-14 17:51:48 +00003107
3108 Args:
3109 filename: The name of the current file.
3110 clean_lines: A CleansedLines instance containing the file.
3111 linenum: The number of the line to check.
3112 error: The function to call with any errors found.
3113 """
3114
3115 # Search for loop keywords at the beginning of the line. Because only
3116 # whitespaces are allowed before the keywords, this will also ignore most
3117 # do-while-loops, since those lines should start with closing brace.
erg@google.comc6671232013-10-25 21:44:03 +00003118 #
3119 # We also check "if" blocks here, since an empty conditional block
3120 # is likely an error.
erg@google.comd350fe52013-01-14 17:51:48 +00003121 line = clean_lines.elided[linenum]
erg@google.comc6671232013-10-25 21:44:03 +00003122 matched = Match(r'\s*(for|while|if)\s*\(', line)
3123 if matched:
erg@google.comd350fe52013-01-14 17:51:48 +00003124 # Find the end of the conditional expression
3125 (end_line, end_linenum, end_pos) = CloseExpression(
3126 clean_lines, linenum, line.find('('))
3127
3128 # Output warning if what follows the condition expression is a semicolon.
3129 # No warning for all other cases, including whitespace or newline, since we
3130 # have a separate check for semicolons preceded by whitespace.
3131 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
erg@google.comc6671232013-10-25 21:44:03 +00003132 if matched.group(1) == 'if':
3133 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
3134 'Empty conditional bodies should use {}')
3135 else:
3136 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
3137 'Empty loop bodies should use {} or continue')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003138
3139
3140def CheckCheck(filename, clean_lines, linenum, error):
3141 """Checks the use of CHECK and EXPECT macros.
3142
3143 Args:
3144 filename: The name of the current file.
3145 clean_lines: A CleansedLines instance containing the file.
3146 linenum: The number of the line to check.
3147 error: The function to call with any errors found.
3148 """
3149
3150 # Decide the set of replacement macros that should be suggested
erg@google.comc6671232013-10-25 21:44:03 +00003151 lines = clean_lines.elided
3152 check_macro = None
3153 start_pos = -1
erg@google.com4e00b9a2009-01-12 23:05:11 +00003154 for macro in _CHECK_MACROS:
erg@google.comc6671232013-10-25 21:44:03 +00003155 i = lines[linenum].find(macro)
3156 if i >= 0:
3157 check_macro = macro
3158
3159 # Find opening parenthesis. Do a regular expression match here
3160 # to make sure that we are matching the expected CHECK macro, as
3161 # opposed to some other macro that happens to contain the CHECK
3162 # substring.
3163 matched = Match(r'^(.*\b' + check_macro + r'\s*)\(', lines[linenum])
3164 if not matched:
3165 continue
3166 start_pos = len(matched.group(1))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003167 break
erg@google.comc6671232013-10-25 21:44:03 +00003168 if not check_macro or start_pos < 0:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003169 # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT'
3170 return
3171
erg@google.comc6671232013-10-25 21:44:03 +00003172 # Find end of the boolean expression by matching parentheses
3173 (last_line, end_line, end_pos) = CloseExpression(
3174 clean_lines, linenum, start_pos)
3175 if end_pos < 0:
3176 return
3177 if linenum == end_line:
3178 expression = lines[linenum][start_pos + 1:end_pos - 1]
3179 else:
3180 expression = lines[linenum][start_pos + 1:]
3181 for i in xrange(linenum + 1, end_line):
3182 expression += lines[i]
3183 expression += last_line[0:end_pos - 1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003184
erg@google.comc6671232013-10-25 21:44:03 +00003185 # Parse expression so that we can take parentheses into account.
3186 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
3187 # which is not replaceable by CHECK_LE.
3188 lhs = ''
3189 rhs = ''
3190 operator = None
3191 while expression:
3192 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
3193 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
3194 if matched:
3195 token = matched.group(1)
3196 if token == '(':
3197 # Parenthesized operand
3198 expression = matched.group(2)
erg@google.com2aa59982013-10-28 19:09:25 +00003199 (end, _) = FindEndOfExpressionInLine(expression, 0, 1, '(', ')')
erg@google.comc6671232013-10-25 21:44:03 +00003200 if end < 0:
3201 return # Unmatched parenthesis
3202 lhs += '(' + expression[0:end]
3203 expression = expression[end:]
3204 elif token in ('&&', '||'):
3205 # Logical and/or operators. This means the expression
3206 # contains more than one term, for example:
3207 # CHECK(42 < a && a < b);
3208 #
3209 # These are not replaceable with CHECK_LE, so bail out early.
3210 return
3211 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
3212 # Non-relational operator
3213 lhs += token
3214 expression = matched.group(2)
3215 else:
3216 # Relational operator
3217 operator = token
3218 rhs = matched.group(2)
3219 break
3220 else:
3221 # Unparenthesized operand. Instead of appending to lhs one character
3222 # at a time, we do another regular expression match to consume several
3223 # characters at once if possible. Trivial benchmark shows that this
3224 # is more efficient when the operands are longer than a single
3225 # character, which is generally the case.
3226 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
3227 if not matched:
3228 matched = Match(r'^(\s*\S)(.*)$', expression)
3229 if not matched:
3230 break
3231 lhs += matched.group(1)
3232 expression = matched.group(2)
3233
3234 # Only apply checks if we got all parts of the boolean expression
3235 if not (lhs and operator and rhs):
3236 return
3237
3238 # Check that rhs do not contain logical operators. We already know
3239 # that lhs is fine since the loop above parses out && and ||.
3240 if rhs.find('&&') > -1 or rhs.find('||') > -1:
3241 return
3242
3243 # At least one of the operands must be a constant literal. This is
3244 # to avoid suggesting replacements for unprintable things like
3245 # CHECK(variable != iterator)
3246 #
3247 # The following pattern matches decimal, hex integers, strings, and
3248 # characters (in that order).
3249 lhs = lhs.strip()
3250 rhs = rhs.strip()
3251 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
3252 if Match(match_constant, lhs) or Match(match_constant, rhs):
3253 # Note: since we know both lhs and rhs, we can provide a more
3254 # descriptive error message like:
3255 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
3256 # Instead of:
3257 # Consider using CHECK_EQ instead of CHECK(a == b)
3258 #
3259 # We are still keeping the less descriptive message because if lhs
3260 # or rhs gets long, the error message might become unreadable.
3261 error(filename, linenum, 'readability/check', 2,
3262 'Consider using %s instead of %s(a %s b)' % (
3263 _CHECK_REPLACEMENT[check_macro][operator],
3264 check_macro, operator))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003265
3266
erg@google.comd350fe52013-01-14 17:51:48 +00003267def CheckAltTokens(filename, clean_lines, linenum, error):
3268 """Check alternative keywords being used in boolean expressions.
3269
3270 Args:
3271 filename: The name of the current file.
3272 clean_lines: A CleansedLines instance containing the file.
3273 linenum: The number of the line to check.
3274 error: The function to call with any errors found.
3275 """
3276 line = clean_lines.elided[linenum]
3277
3278 # Avoid preprocessor lines
3279 if Match(r'^\s*#', line):
3280 return
3281
3282 # Last ditch effort to avoid multi-line comments. This will not help
3283 # if the comment started before the current line or ended after the
3284 # current line, but it catches most of the false positives. At least,
3285 # it provides a way to workaround this warning for people who use
3286 # multi-line comments in preprocessor macros.
3287 #
3288 # TODO(unknown): remove this once cpplint has better support for
3289 # multi-line comments.
3290 if line.find('/*') >= 0 or line.find('*/') >= 0:
3291 return
3292
3293 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
3294 error(filename, linenum, 'readability/alt_tokens', 2,
3295 'Use operator %s instead of %s' % (
3296 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
3297
3298
erg@google.com4e00b9a2009-01-12 23:05:11 +00003299def GetLineWidth(line):
3300 """Determines the width of the line in column positions.
3301
3302 Args:
3303 line: A string, which may be a Unicode string.
3304
3305 Returns:
3306 The width of the line in column positions, accounting for Unicode
3307 combining characters and wide characters.
3308 """
3309 if isinstance(line, unicode):
3310 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00003311 for uc in unicodedata.normalize('NFC', line):
3312 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003313 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00003314 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003315 width += 1
3316 return width
3317 else:
3318 return len(line)
3319
3320
erg@google.comd350fe52013-01-14 17:51:48 +00003321def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00003322 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003323 """Checks rules from the 'C++ style rules' section of cppguide.html.
3324
3325 Most of these rules are hard to test (naming, comment style), but we
3326 do what we can. In particular we check for 2-space indents, line lengths,
3327 tab usage, spaces inside code, etc.
3328
3329 Args:
3330 filename: The name of the current file.
3331 clean_lines: A CleansedLines instance containing the file.
3332 linenum: The number of the line to check.
3333 file_extension: The extension (without the dot) of the filename.
erg@google.comd350fe52013-01-14 17:51:48 +00003334 nesting_state: A _NestingState instance which maintains information about
3335 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003336 error: The function to call with any errors found.
3337 """
3338
erg@google.com2aa59982013-10-28 19:09:25 +00003339 # Don't use "elided" lines here, otherwise we can't check commented lines.
3340 # Don't want to use "raw" either, because we don't want to check inside C++11
3341 # raw strings,
3342 raw_lines = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00003343 line = raw_lines[linenum]
3344
3345 if line.find('\t') != -1:
3346 error(filename, linenum, 'whitespace/tab', 1,
3347 'Tab found; better to use spaces')
3348
3349 # One or three blank spaces at the beginning of the line is weird; it's
3350 # hard to reconcile that with 2-space indents.
3351 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
3352 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
3353 # if(RLENGTH > 20) complain = 0;
3354 # if(match($0, " +(error|private|public|protected):")) complain = 0;
3355 # if(match(prev, "&& *$")) complain = 0;
3356 # if(match(prev, "\\|\\| *$")) complain = 0;
3357 # if(match(prev, "[\",=><] *$")) complain = 0;
3358 # if(match($0, " <<")) complain = 0;
3359 # if(match(prev, " +for \\(")) complain = 0;
3360 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
3361 initial_spaces = 0
3362 cleansed_line = clean_lines.elided[linenum]
3363 while initial_spaces < len(line) and line[initial_spaces] == ' ':
3364 initial_spaces += 1
3365 if line and line[-1].isspace():
3366 error(filename, linenum, 'whitespace/end_of_line', 4,
3367 'Line ends in whitespace. Consider deleting these extra spaces.')
erg@google.comfd5da632013-10-25 17:39:45 +00003368 # There are certain situations we allow one space, notably for section labels
erg@google.com4e00b9a2009-01-12 23:05:11 +00003369 elif ((initial_spaces == 1 or initial_spaces == 3) and
3370 not Match(r'\s*\w+\s*:\s*$', cleansed_line)):
3371 error(filename, linenum, 'whitespace/indent', 3,
3372 'Weird number of spaces at line-start. '
3373 'Are you using a 2-space indent?')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003374
3375 # Check if the line is a header guard.
3376 is_header_guard = False
3377 if file_extension == 'h':
3378 cppvar = GetHeaderGuardCPPVariable(filename)
3379 if (line.startswith('#ifndef %s' % cppvar) or
3380 line.startswith('#define %s' % cppvar) or
3381 line.startswith('#endif // %s' % cppvar)):
3382 is_header_guard = True
3383 # #include lines and header guards can be long, since there's no clean way to
3384 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00003385 #
3386 # URLs can be long too. It's possible to split these, but it makes them
3387 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00003388 #
3389 # The "$Id:...$" comment may also get very long without it being the
3390 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00003391 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00003392 not Match(r'^\s*//.*http(s?)://\S*$', line) and
3393 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003394 line_width = GetLineWidth(line)
3395 if line_width > 100:
3396 error(filename, linenum, 'whitespace/line_length', 4,
3397 'Lines should very rarely be longer than 100 characters')
3398 elif line_width > 80:
3399 error(filename, linenum, 'whitespace/line_length', 2,
3400 'Lines should be <= 80 characters long')
3401
3402 if (cleansed_line.count(';') > 1 and
3403 # for loops are allowed two ;'s (and may run over two lines).
3404 cleansed_line.find('for') == -1 and
3405 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
3406 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
3407 # It's ok to have many commands in a switch case that fits in 1 line
3408 not ((cleansed_line.find('case ') != -1 or
3409 cleansed_line.find('default:') != -1) and
3410 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00003411 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00003412 'More than one command on the same line')
3413
3414 # Some more style checks
3415 CheckBraces(filename, clean_lines, linenum, error)
erg@google.comc6671232013-10-25 21:44:03 +00003416 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00003417 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
3418 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003419 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00003420 CheckAltTokens(filename, clean_lines, linenum, error)
3421 classinfo = nesting_state.InnermostClass()
3422 if classinfo:
3423 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003424
3425
3426_RE_PATTERN_INCLUDE_NEW_STYLE = re.compile(r'#include +"[^/]+\.h"')
3427_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
3428# Matches the first component of a filename delimited by -s and _s. That is:
3429# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
3430# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
3431# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
3432# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
3433_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
3434
3435
3436def _DropCommonSuffixes(filename):
3437 """Drops common suffixes like _test.cc or -inl.h from filename.
3438
3439 For example:
3440 >>> _DropCommonSuffixes('foo/foo-inl.h')
3441 'foo/foo'
3442 >>> _DropCommonSuffixes('foo/bar/foo.cc')
3443 'foo/bar/foo'
3444 >>> _DropCommonSuffixes('foo/foo_internal.h')
3445 'foo/foo'
3446 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
3447 'foo/foo_unusualinternal'
3448
3449 Args:
3450 filename: The input filename.
3451
3452 Returns:
3453 The filename with the common suffix removed.
3454 """
3455 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
3456 'inl.h', 'impl.h', 'internal.h'):
3457 if (filename.endswith(suffix) and len(filename) > len(suffix) and
3458 filename[-len(suffix) - 1] in ('-', '_')):
3459 return filename[:-len(suffix) - 1]
3460 return os.path.splitext(filename)[0]
3461
3462
3463def _IsTestFilename(filename):
3464 """Determines if the given filename has a suffix that identifies it as a test.
3465
3466 Args:
3467 filename: The input filename.
3468
3469 Returns:
3470 True if 'filename' looks like a test, False otherwise.
3471 """
3472 if (filename.endswith('_test.cc') or
3473 filename.endswith('_unittest.cc') or
3474 filename.endswith('_regtest.cc')):
3475 return True
3476 else:
3477 return False
3478
3479
3480def _ClassifyInclude(fileinfo, include, is_system):
3481 """Figures out what kind of header 'include' is.
3482
3483 Args:
3484 fileinfo: The current file cpplint is running over. A FileInfo instance.
3485 include: The path to a #included file.
3486 is_system: True if the #include used <> rather than "".
3487
3488 Returns:
3489 One of the _XXX_HEADER constants.
3490
3491 For example:
3492 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
3493 _C_SYS_HEADER
3494 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
3495 _CPP_SYS_HEADER
3496 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
3497 _LIKELY_MY_HEADER
3498 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
3499 ... 'bar/foo_other_ext.h', False)
3500 _POSSIBLE_MY_HEADER
3501 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
3502 _OTHER_HEADER
3503 """
3504 # This is a list of all standard c++ header files, except
3505 # those already checked for above.
erg@google.comfd5da632013-10-25 17:39:45 +00003506 is_cpp_h = include in _CPP_HEADERS
erg@google.com4e00b9a2009-01-12 23:05:11 +00003507
3508 if is_system:
3509 if is_cpp_h:
3510 return _CPP_SYS_HEADER
3511 else:
3512 return _C_SYS_HEADER
3513
3514 # If the target file and the include we're checking share a
3515 # basename when we drop common extensions, and the include
3516 # lives in . , then it's likely to be owned by the target file.
3517 target_dir, target_base = (
3518 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
3519 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
3520 if target_base == include_base and (
3521 include_dir == target_dir or
3522 include_dir == os.path.normpath(target_dir + '/../public')):
3523 return _LIKELY_MY_HEADER
3524
3525 # If the target and include share some initial basename
3526 # component, it's possible the target is implementing the
3527 # include, so it's allowed to be first, but we'll never
3528 # complain if it's not there.
3529 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
3530 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
3531 if (target_first_component and include_first_component and
3532 target_first_component.group(0) ==
3533 include_first_component.group(0)):
3534 return _POSSIBLE_MY_HEADER
3535
3536 return _OTHER_HEADER
3537
3538
erg@google.coma87abb82009-02-24 01:41:01 +00003539
erg@google.come35f7652009-06-19 20:52:09 +00003540def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
3541 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003542
erg@google.come35f7652009-06-19 20:52:09 +00003543 Strings on #include lines are NOT removed from elided line, to make
3544 certain tasks easier. However, to prevent false positives, checks
3545 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003546
3547 Args:
3548 filename: The name of the current file.
3549 clean_lines: A CleansedLines instance containing the file.
3550 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003551 include_state: An _IncludeState instance in which the headers are inserted.
3552 error: The function to call with any errors found.
3553 """
3554 fileinfo = FileInfo(filename)
3555
erg@google.come35f7652009-06-19 20:52:09 +00003556 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003557
3558 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
erg@google.come35f7652009-06-19 20:52:09 +00003559 if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003560 error(filename, linenum, 'build/include', 4,
3561 'Include the directory when naming .h files')
3562
3563 # we shouldn't include a file more than once. actually, there are a
3564 # handful of instances where doing so is okay, but in general it's
3565 # not.
erg@google.come35f7652009-06-19 20:52:09 +00003566 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003567 if match:
3568 include = match.group(2)
3569 is_system = (match.group(1) == '<')
3570 if include in include_state:
3571 error(filename, linenum, 'build/include', 4,
3572 '"%s" already included at %s:%s' %
3573 (include, filename, include_state[include]))
3574 else:
3575 include_state[include] = linenum
3576
3577 # We want to ensure that headers appear in the right order:
3578 # 1) for foo.cc, foo.h (preferred location)
3579 # 2) c system files
3580 # 3) cpp system files
3581 # 4) for foo.cc, foo.h (deprecated location)
3582 # 5) other google headers
3583 #
3584 # We classify each include statement as one of those 5 types
3585 # using a number of techniques. The include_state object keeps
3586 # track of the highest type seen, and complains if we see a
3587 # lower type after that.
3588 error_message = include_state.CheckNextIncludeOrder(
3589 _ClassifyInclude(fileinfo, include, is_system))
3590 if error_message:
3591 error(filename, linenum, 'build/include_order', 4,
3592 '%s. Should be: %s.h, c system, c++ system, other.' %
3593 (error_message, fileinfo.BaseName()))
erg@google.comfd5da632013-10-25 17:39:45 +00003594 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
3595 if not include_state.IsInAlphabeticalOrder(
3596 clean_lines, linenum, canonical_include):
erg@google.coma868d2d2009-10-09 21:18:45 +00003597 error(filename, linenum, 'build/include_alpha', 4,
3598 'Include "%s" not in alphabetical order' % include)
erg@google.comfd5da632013-10-25 17:39:45 +00003599 include_state.SetLastHeader(canonical_include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003600
erg@google.come35f7652009-06-19 20:52:09 +00003601 # Look for any of the stream classes that are part of standard C++.
3602 match = _RE_PATTERN_INCLUDE.match(line)
3603 if match:
3604 include = match.group(2)
3605 if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
3606 # Many unit tests use cout, so we exempt them.
3607 if not _IsTestFilename(filename):
3608 error(filename, linenum, 'readability/streams', 3,
3609 'Streams are highly discouraged.')
3610
erg@google.com8a95ecc2011-09-08 00:45:54 +00003611
3612def _GetTextInside(text, start_pattern):
erg@google.com2aa59982013-10-28 19:09:25 +00003613 r"""Retrieves all the text between matching open and close parentheses.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003614
3615 Given a string of lines and a regular expression string, retrieve all the text
3616 following the expression and between opening punctuation symbols like
3617 (, [, or {, and the matching close-punctuation symbol. This properly nested
3618 occurrences of the punctuations, so for the text like
3619 printf(a(), b(c()));
3620 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
3621 start_pattern must match string having an open punctuation symbol at the end.
3622
3623 Args:
3624 text: The lines to extract text. Its comments and strings must be elided.
3625 It can be single line and can span multiple lines.
3626 start_pattern: The regexp string indicating where to start extracting
3627 the text.
3628 Returns:
3629 The extracted text.
3630 None if either the opening string or ending punctuation could not be found.
3631 """
3632 # TODO(sugawarayu): Audit cpplint.py to see what places could be profitably
3633 # rewritten to use _GetTextInside (and use inferior regexp matching today).
3634
3635 # Give opening punctuations to get the matching close-punctuations.
3636 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
3637 closing_punctuation = set(matching_punctuation.itervalues())
3638
3639 # Find the position to start extracting text.
3640 match = re.search(start_pattern, text, re.M)
3641 if not match: # start_pattern not found in text.
3642 return None
3643 start_position = match.end(0)
3644
3645 assert start_position > 0, (
3646 'start_pattern must ends with an opening punctuation.')
3647 assert text[start_position - 1] in matching_punctuation, (
3648 'start_pattern must ends with an opening punctuation.')
3649 # Stack of closing punctuations we expect to have in text after position.
3650 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
3651 position = start_position
3652 while punctuation_stack and position < len(text):
3653 if text[position] == punctuation_stack[-1]:
3654 punctuation_stack.pop()
3655 elif text[position] in closing_punctuation:
3656 # A closing punctuation without matching opening punctuations.
3657 return None
3658 elif text[position] in matching_punctuation:
3659 punctuation_stack.append(matching_punctuation[text[position]])
3660 position += 1
3661 if punctuation_stack:
3662 # Opening punctuations left without matching close-punctuations.
3663 return None
3664 # punctuations match.
3665 return text[start_position:position - 1]
3666
3667
erg@google.comfd5da632013-10-25 17:39:45 +00003668# Patterns for matching call-by-reference parameters.
erg@google.com2aa59982013-10-28 19:09:25 +00003669#
3670# Supports nested templates up to 2 levels deep using this messy pattern:
3671# < (?: < (?: < [^<>]*
3672# >
3673# | [^<>] )*
3674# >
3675# | [^<>] )*
3676# >
erg@google.comfd5da632013-10-25 17:39:45 +00003677_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
3678_RE_PATTERN_TYPE = (
3679 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
erg@google.com2aa59982013-10-28 19:09:25 +00003680 r'(?:\w|'
3681 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
3682 r'::)+')
erg@google.comfd5da632013-10-25 17:39:45 +00003683# A call-by-reference parameter ends with '& identifier'.
3684_RE_PATTERN_REF_PARAM = re.compile(
3685 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
3686 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
3687# A call-by-const-reference parameter either ends with 'const& identifier'
3688# or looks like 'const type& identifier' when 'type' is atomic.
3689_RE_PATTERN_CONST_REF_PARAM = (
3690 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
3691 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
3692
3693
3694def CheckLanguage(filename, clean_lines, linenum, file_extension,
3695 include_state, nesting_state, error):
erg@google.come35f7652009-06-19 20:52:09 +00003696 """Checks rules from the 'C++ language rules' section of cppguide.html.
3697
3698 Some of these rules are hard to test (function overloading, using
3699 uint32 inappropriately), but we do the best we can.
3700
3701 Args:
3702 filename: The name of the current file.
3703 clean_lines: A CleansedLines instance containing the file.
3704 linenum: The number of the line to check.
3705 file_extension: The extension (without the dot) of the filename.
3706 include_state: An _IncludeState instance in which the headers are inserted.
erg@google.comfd5da632013-10-25 17:39:45 +00003707 nesting_state: A _NestingState instance which maintains information about
3708 the current stack of nested blocks being parsed.
erg@google.come35f7652009-06-19 20:52:09 +00003709 error: The function to call with any errors found.
3710 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00003711 # If the line is empty or consists of entirely a comment, no need to
3712 # check it.
3713 line = clean_lines.elided[linenum]
3714 if not line:
3715 return
3716
erg@google.come35f7652009-06-19 20:52:09 +00003717 match = _RE_PATTERN_INCLUDE.search(line)
3718 if match:
3719 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
3720 return
3721
erg@google.com2aa59982013-10-28 19:09:25 +00003722 # Reset include state across preprocessor directives. This is meant
3723 # to silence warnings for conditional includes.
3724 if Match(r'^\s*#\s*(?:ifdef|elif|else|endif)\b', line):
3725 include_state.ResetSection()
3726
erg@google.com4e00b9a2009-01-12 23:05:11 +00003727 # Make Windows paths like Unix.
3728 fullname = os.path.abspath(filename).replace('\\', '/')
3729
3730 # TODO(unknown): figure out if they're using default arguments in fn proto.
3731
erg@google.com4e00b9a2009-01-12 23:05:11 +00003732 # Check to see if they're using an conversion function cast.
3733 # I just try to capture the most common basic types, though there are more.
3734 # Parameterless conversion functions, such as bool(), are allowed as they are
3735 # probably a member operator declaration or default constructor.
3736 match = Search(
erg@google.coma868d2d2009-10-09 21:18:45 +00003737 r'(\bnew\s+)?\b' # Grab 'new' operator, if it's there
erg@google.comc6671232013-10-25 21:44:03 +00003738 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
3739 r'(\([^)].*)', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003740 if match:
erg@google.comc6671232013-10-25 21:44:03 +00003741 matched_new = match.group(1)
3742 matched_type = match.group(2)
3743 matched_funcptr = match.group(3)
3744
erg@google.com4e00b9a2009-01-12 23:05:11 +00003745 # gMock methods are defined using some variant of MOCK_METHODx(name, type)
3746 # where type may be float(), int(string), etc. Without context they are
erg@google.comd7d27472011-09-07 17:36:35 +00003747 # virtually indistinguishable from int(x) casts. Likewise, gMock's
3748 # MockCallback takes a template parameter of the form return_type(arg_type),
3749 # which looks much like the cast we're trying to detect.
erg@google.comc6671232013-10-25 21:44:03 +00003750 #
3751 # std::function<> wrapper has a similar problem.
3752 #
3753 # Return types for function pointers also look like casts if they
3754 # don't have an extra space.
3755 if (matched_new is None and # If new operator, then this isn't a cast
erg@google.comd7d27472011-09-07 17:36:35 +00003756 not (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
erg@google.comc6671232013-10-25 21:44:03 +00003757 Search(r'\bMockCallback<.*>', line) or
3758 Search(r'\bstd::function<.*>', line)) and
3759 not (matched_funcptr and
3760 Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
3761 matched_funcptr))):
erg@google.comd350fe52013-01-14 17:51:48 +00003762 # Try a bit harder to catch gmock lines: the only place where
3763 # something looks like an old-style cast is where we declare the
3764 # return type of the mocked method, and the only time when we
3765 # are missing context is if MOCK_METHOD was split across
erg@google.comc6671232013-10-25 21:44:03 +00003766 # multiple lines. The missing MOCK_METHOD is usually one or two
3767 # lines back, so scan back one or two lines.
3768 #
3769 # It's not possible for gmock macros to appear in the first 2
3770 # lines, since the class head + section name takes up 2 lines.
3771 if (linenum < 2 or
3772 not (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
3773 clean_lines.elided[linenum - 1]) or
3774 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
3775 clean_lines.elided[linenum - 2]))):
erg@google.comd350fe52013-01-14 17:51:48 +00003776 error(filename, linenum, 'readability/casting', 4,
3777 'Using deprecated casting style. '
3778 'Use static_cast<%s>(...) instead' %
erg@google.comc6671232013-10-25 21:44:03 +00003779 matched_type)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003780
3781 CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3782 'static_cast',
erg@google.com8a95ecc2011-09-08 00:45:54 +00003783 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
3784
3785 # This doesn't catch all cases. Consider (const char * const)"hello".
3786 #
3787 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
3788 # compile).
3789 if CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3790 'const_cast', r'\((char\s?\*+\s?)\)\s*"', error):
3791 pass
3792 else:
3793 # Check pointer casts for other than string constants
3794 CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
3795 'reinterpret_cast', r'\((\w+\s?\*+\s?)\)', error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003796
3797 # In addition, we look for people taking the address of a cast. This
3798 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
3799 # point where you think.
erg@google.comc6671232013-10-25 21:44:03 +00003800 match = Search(
3801 r'(?:&\(([^)]+)\)[\w(])|'
3802 r'(?:&(static|dynamic|down|reinterpret)_cast\b)', line)
3803 if match and match.group(1) != '*':
erg@google.com4e00b9a2009-01-12 23:05:11 +00003804 error(filename, linenum, 'runtime/casting', 4,
3805 ('Are you taking an address of a cast? '
3806 'This is dangerous: could be a temp var. '
3807 'Take the address before doing the cast, rather than after'))
3808
erg@google.comc6671232013-10-25 21:44:03 +00003809 # Create an extended_line, which is the concatenation of the current and
3810 # next lines, for more effective checking of code that may span more than one
3811 # line.
3812 if linenum + 1 < clean_lines.NumLines():
3813 extended_line = line + clean_lines.elided[linenum + 1]
3814 else:
3815 extended_line = line
3816
erg@google.com4e00b9a2009-01-12 23:05:11 +00003817 # Check for people declaring static/global STL strings at the top level.
3818 # This is dangerous because the C++ language does not guarantee that
3819 # globals with constructors are initialized before the first access.
3820 match = Match(
3821 r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
3822 line)
3823 # Make sure it's not a function.
3824 # Function template specialization looks like: "string foo<Type>(...".
3825 # Class template definitions look like: "string Foo<Type>::Method(...".
erg@google.com2aa59982013-10-28 19:09:25 +00003826 #
3827 # Also ignore things that look like operators. These are matched separately
3828 # because operator names cross non-word boundaries. If we change the pattern
3829 # above, we would decrease the accuracy of matching identifiers.
3830 if (match and
3831 not Search(r'\boperator\W', line) and
3832 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)?\s*\(([^"]|$)', match.group(3))):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003833 error(filename, linenum, 'runtime/string', 4,
3834 'For a static/global string constant, use a C style string instead: '
3835 '"%schar %s[]".' %
3836 (match.group(1), match.group(2)))
3837
erg@google.com4e00b9a2009-01-12 23:05:11 +00003838 if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
3839 error(filename, linenum, 'runtime/init', 4,
3840 'You seem to be initializing a member variable with itself.')
3841
3842 if file_extension == 'h':
3843 # TODO(unknown): check that 1-arg constructors are explicit.
3844 # How to tell it's a constructor?
3845 # (handled in CheckForNonStandardConstructs for now)
3846 # TODO(unknown): check that classes have DISALLOW_EVIL_CONSTRUCTORS
3847 # (level 1 error)
3848 pass
3849
3850 # Check if people are using the verboten C basic types. The only exception
3851 # we regularly allow is "unsigned short port" for port.
3852 if Search(r'\bshort port\b', line):
3853 if not Search(r'\bunsigned short port\b', line):
3854 error(filename, linenum, 'runtime/int', 4,
3855 'Use "unsigned short" for ports, not "short"')
3856 else:
3857 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
3858 if match:
3859 error(filename, linenum, 'runtime/int', 4,
3860 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
3861
3862 # When snprintf is used, the second argument shouldn't be a literal.
3863 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
erg+personal@google.com05189642010-04-30 20:43:03 +00003864 if match and match.group(2) != '0':
3865 # If 2nd arg is zero, snprintf is used to calculate size.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003866 error(filename, linenum, 'runtime/printf', 3,
3867 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
3868 'to snprintf.' % (match.group(1), match.group(2)))
3869
3870 # Check if some verboten C functions are being used.
3871 if Search(r'\bsprintf\b', line):
3872 error(filename, linenum, 'runtime/printf', 5,
3873 'Never use sprintf. Use snprintf instead.')
3874 match = Search(r'\b(strcpy|strcat)\b', line)
3875 if match:
3876 error(filename, linenum, 'runtime/printf', 4,
3877 'Almost always, snprintf is better than %s' % match.group(1))
3878
erg@google.coma868d2d2009-10-09 21:18:45 +00003879 # Check if some verboten operator overloading is going on
3880 # TODO(unknown): catch out-of-line unary operator&:
3881 # class X {};
3882 # int operator&(const X& x) { return 42; } // unary operator&
3883 # The trick is it's hard to tell apart from binary operator&:
3884 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
3885 if Search(r'\boperator\s*&\s*\(\s*\)', line):
3886 error(filename, linenum, 'runtime/operator', 4,
3887 'Unary operator& is dangerous. Do not use it.')
3888
erg@google.com4e00b9a2009-01-12 23:05:11 +00003889 # Check for suspicious usage of "if" like
3890 # } if (a == b) {
3891 if Search(r'\}\s*if\s*\(', line):
3892 error(filename, linenum, 'readability/braces', 4,
3893 'Did you mean "else if"? If not, start a new line for "if".')
3894
3895 # Check for potential format string bugs like printf(foo).
3896 # We constrain the pattern not to pick things like DocidForPrintf(foo).
3897 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
erg@google.com8a95ecc2011-09-08 00:45:54 +00003898 # TODO(sugawarayu): Catch the following case. Need to change the calling
3899 # convention of the whole function to process multiple line to handle it.
3900 # printf(
3901 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
3902 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
3903 if printf_args:
3904 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00003905 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00003906 function_name = re.search(r'\b((?:string)?printf)\s*\(',
3907 line, re.I).group(1)
3908 error(filename, linenum, 'runtime/printf', 4,
3909 'Potential format string bug. Do %s("%%s", %s) instead.'
3910 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00003911
3912 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
3913 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
3914 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
3915 error(filename, linenum, 'runtime/memset', 4,
3916 'Did you mean "memset(%s, 0, %s)"?'
3917 % (match.group(1), match.group(2)))
3918
3919 if Search(r'\busing namespace\b', line):
3920 error(filename, linenum, 'build/namespaces', 5,
3921 'Do not use namespace using-directives. '
3922 'Use using-declarations instead.')
3923
3924 # Detect variable-length arrays.
3925 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
3926 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
3927 match.group(3).find(']') == -1):
3928 # Split the size using space and arithmetic operators as delimiters.
3929 # If any of the resulting tokens are not compile time constants then
3930 # report the error.
3931 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
3932 is_const = True
3933 skip_next = False
3934 for tok in tokens:
3935 if skip_next:
3936 skip_next = False
3937 continue
3938
3939 if Search(r'sizeof\(.+\)', tok): continue
3940 if Search(r'arraysize\(\w+\)', tok): continue
3941
3942 tok = tok.lstrip('(')
3943 tok = tok.rstrip(')')
3944 if not tok: continue
3945 if Match(r'\d+', tok): continue
3946 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
3947 if Match(r'k[A-Z0-9]\w*', tok): continue
3948 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
3949 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
3950 # A catch all for tricky sizeof cases, including 'sizeof expression',
3951 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00003952 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003953 if tok.startswith('sizeof'):
3954 skip_next = True
3955 continue
3956 is_const = False
3957 break
3958 if not is_const:
3959 error(filename, linenum, 'runtime/arrays', 1,
3960 'Do not use variable-length arrays. Use an appropriately named '
3961 "('k' followed by CamelCase) compile-time constant for the size.")
3962
3963 # If DISALLOW_EVIL_CONSTRUCTORS, DISALLOW_COPY_AND_ASSIGN, or
3964 # DISALLOW_IMPLICIT_CONSTRUCTORS is present, then it should be the last thing
3965 # in the class declaration.
3966 match = Match(
3967 (r'\s*'
3968 r'(DISALLOW_(EVIL_CONSTRUCTORS|COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))'
3969 r'\(.*\);$'),
3970 line)
3971 if match and linenum + 1 < clean_lines.NumLines():
3972 next_line = clean_lines.elided[linenum + 1]
erg@google.com8a95ecc2011-09-08 00:45:54 +00003973 # We allow some, but not all, declarations of variables to be present
3974 # in the statement that defines the class. The [\w\*,\s]* fragment of
3975 # the regular expression below allows users to declare instances of
3976 # the class or pointers to instances, but not less common types such
3977 # as function pointers or arrays. It's a tradeoff between allowing
3978 # reasonable code and avoiding trying to parse more C++ using regexps.
3979 if not Search(r'^\s*}[\w\*,\s]*;', next_line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003980 error(filename, linenum, 'readability/constructors', 3,
3981 match.group(1) + ' should be the last thing in the class')
3982
3983 # Check for use of unnamed namespaces in header files. Registration
3984 # macros are typically OK, so we allow use of "namespace {" on lines
3985 # that end with backslashes.
3986 if (file_extension == 'h'
3987 and Search(r'\bnamespace\s*{', line)
3988 and line[-1] != '\\'):
3989 error(filename, linenum, 'build/namespaces', 4,
3990 'Do not use unnamed namespaces in header files. See '
3991 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
3992 ' for more information.')
3993
erg@google.comc6671232013-10-25 21:44:03 +00003994def CheckForNonConstReference(filename, clean_lines, linenum,
3995 nesting_state, error):
3996 """Check for non-const references.
3997
3998 Separate from CheckLanguage since it scans backwards from current
3999 line, instead of scanning forward.
4000
4001 Args:
4002 filename: The name of the current file.
4003 clean_lines: A CleansedLines instance containing the file.
4004 linenum: The number of the line to check.
4005 nesting_state: A _NestingState instance which maintains information about
4006 the current stack of nested blocks being parsed.
4007 error: The function to call with any errors found.
4008 """
4009 # Do nothing if there is no '&' on current line.
4010 line = clean_lines.elided[linenum]
4011 if '&' not in line:
4012 return
4013
erg@google.com2aa59982013-10-28 19:09:25 +00004014 # Long type names may be broken across multiple lines, usually in one
4015 # of these forms:
4016 # LongType
4017 # ::LongTypeContinued &identifier
4018 # LongType::
4019 # LongTypeContinued &identifier
4020 # LongType<
4021 # ...>::LongTypeContinued &identifier
4022 #
4023 # If we detected a type split across two lines, join the previous
4024 # line to current line so that we can match const references
4025 # accordingly.
erg@google.comc6671232013-10-25 21:44:03 +00004026 #
4027 # Note that this only scans back one line, since scanning back
4028 # arbitrary number of lines would be expensive. If you have a type
4029 # that spans more than 2 lines, please use a typedef.
4030 if linenum > 1:
4031 previous = None
erg@google.com2aa59982013-10-28 19:09:25 +00004032 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004033 # previous_line\n + ::current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004034 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004035 clean_lines.elided[linenum - 1])
erg@google.com2aa59982013-10-28 19:09:25 +00004036 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004037 # previous_line::\n + current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004038 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004039 clean_lines.elided[linenum - 1])
4040 if previous:
4041 line = previous.group(1) + line.lstrip()
erg@google.com2aa59982013-10-28 19:09:25 +00004042 else:
4043 # Check for templated parameter that is split across multiple lines
4044 endpos = line.rfind('>')
4045 if endpos > -1:
4046 (_, startline, startpos) = ReverseCloseExpression(
4047 clean_lines, linenum, endpos)
4048 if startpos > -1 and startline < linenum:
4049 # Found the matching < on an earlier line, collect all
4050 # pieces up to current line.
4051 line = ''
4052 for i in xrange(startline, linenum + 1):
4053 line += clean_lines.elided[i].strip()
erg@google.comc6671232013-10-25 21:44:03 +00004054
4055 # Check for non-const references in function parameters. A single '&' may
4056 # found in the following places:
4057 # inside expression: binary & for bitwise AND
4058 # inside expression: unary & for taking the address of something
4059 # inside declarators: reference parameter
4060 # We will exclude the first two cases by checking that we are not inside a
4061 # function body, including one that was just introduced by a trailing '{'.
4062 # TODO(unknwon): Doesn't account for preprocessor directives.
4063 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
4064 check_params = False
4065 if not nesting_state.stack:
4066 check_params = True # top level
4067 elif (isinstance(nesting_state.stack[-1], _ClassInfo) or
4068 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
4069 check_params = True # within class or namespace
4070 elif Match(r'.*{\s*$', line):
4071 if (len(nesting_state.stack) == 1 or
4072 isinstance(nesting_state.stack[-2], _ClassInfo) or
4073 isinstance(nesting_state.stack[-2], _NamespaceInfo)):
4074 check_params = True # just opened global/class/namespace block
4075 # We allow non-const references in a few standard places, like functions
4076 # called "swap()" or iostream operators like "<<" or ">>". Do not check
4077 # those function parameters.
4078 #
4079 # We also accept & in static_assert, which looks like a function but
4080 # it's actually a declaration expression.
4081 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
4082 r'operator\s*[<>][<>]|'
4083 r'static_assert|COMPILE_ASSERT'
4084 r')\s*\(')
4085 if Search(whitelisted_functions, line):
4086 check_params = False
4087 elif not Search(r'\S+\([^)]*$', line):
4088 # Don't see a whitelisted function on this line. Actually we
4089 # didn't see any function name on this line, so this is likely a
4090 # multi-line parameter list. Try a bit harder to catch this case.
4091 for i in xrange(2):
4092 if (linenum > i and
4093 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
4094 check_params = False
4095 break
4096
4097 if check_params:
4098 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
4099 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
4100 if not Match(_RE_PATTERN_CONST_REF_PARAM, parameter):
4101 error(filename, linenum, 'runtime/references', 2,
4102 'Is this a non-const reference? '
erg@google.com2aa59982013-10-28 19:09:25 +00004103 'If so, make const or use a pointer: ' +
4104 ReplaceAll(' *<', '<', parameter))
erg@google.comc6671232013-10-25 21:44:03 +00004105
erg@google.com4e00b9a2009-01-12 23:05:11 +00004106
4107def CheckCStyleCast(filename, linenum, line, raw_line, cast_type, pattern,
4108 error):
4109 """Checks for a C-style cast by looking for the pattern.
4110
erg@google.com4e00b9a2009-01-12 23:05:11 +00004111 Args:
4112 filename: The name of the current file.
4113 linenum: The number of the line to check.
4114 line: The line of code to check.
4115 raw_line: The raw line of code to check, with comments.
4116 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00004117 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004118 pattern: The regular expression used to find C-style casts.
4119 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004120
4121 Returns:
4122 True if an error was emitted.
4123 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004124 """
4125 match = Search(pattern, line)
4126 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00004127 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00004128
erg@google.comfd5da632013-10-25 17:39:45 +00004129 # Exclude lines with sizeof, since sizeof looks like a cast.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004130 sizeof_match = Match(r'.*sizeof\s*$', line[0:match.start(1) - 1])
4131 if sizeof_match:
erg@google.comfd5da632013-10-25 17:39:45 +00004132 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00004133
erg@google.comd350fe52013-01-14 17:51:48 +00004134 # operator++(int) and operator--(int)
4135 if (line[0:match.start(1) - 1].endswith(' operator++') or
4136 line[0:match.start(1) - 1].endswith(' operator--')):
4137 return False
4138
erg@google.comc6671232013-10-25 21:44:03 +00004139 # A single unnamed argument for a function tends to look like old
4140 # style cast. If we see those, don't issue warnings for deprecated
4141 # casts, instead issue warnings for unnamed arguments where
4142 # appropriate.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004143 #
erg@google.comc6671232013-10-25 21:44:03 +00004144 # These are things that we want warnings for, since the style guide
4145 # explicitly require all parameters to be named:
4146 # Function(int);
4147 # Function(int) {
4148 # ConstMember(int) const;
4149 # ConstMember(int) const {
4150 # ExceptionMember(int) throw (...);
4151 # ExceptionMember(int) throw (...) {
4152 # PureVirtual(int) = 0;
4153 #
4154 # These are functions of some sort, where the compiler would be fine
4155 # if they had named parameters, but people often omit those
4156 # identifiers to reduce clutter:
4157 # (FunctionPointer)(int);
4158 # (FunctionPointer)(int) = value;
4159 # Function((function_pointer_arg)(int))
4160 # <TemplateArgument(int)>;
4161 # <(FunctionPointerTemplateArgument)(int)>;
4162 remainder = line[match.end(0):]
4163 if Match(r'^\s*(?:;|const\b|throw\b|=|>|\{|\))', remainder):
4164 # Looks like an unnamed parameter.
4165
4166 # Don't warn on any kind of template arguments.
4167 if Match(r'^\s*>', remainder):
4168 return False
4169
4170 # Don't warn on assignments to function pointers, but keep warnings for
4171 # unnamed parameters to pure virtual functions. Note that this pattern
4172 # will also pass on assignments of "0" to function pointers, but the
4173 # preferred values for those would be "nullptr" or "NULL".
4174 matched_zero = Match(r'^\s=\s*(\S+)\s*;', remainder)
4175 if matched_zero and matched_zero.group(1) != '0':
4176 return False
4177
4178 # Don't warn on function pointer declarations. For this we need
4179 # to check what came before the "(type)" string.
4180 if Match(r'.*\)\s*$', line[0:match.start(0)]):
4181 return False
4182
4183 # Don't warn if the parameter is named with block comments, e.g.:
4184 # Function(int /*unused_param*/);
4185 if '/*' in raw_line:
4186 return False
4187
4188 # Passed all filters, issue warning here.
4189 error(filename, linenum, 'readability/function', 3,
4190 'All parameters should be named in a function')
erg@google.com8a95ecc2011-09-08 00:45:54 +00004191 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +00004192
4193 # At this point, all that should be left is actual casts.
4194 error(filename, linenum, 'readability/casting', 4,
4195 'Using C-style cast. Use %s<%s>(...) instead' %
4196 (cast_type, match.group(1)))
4197
erg@google.com8a95ecc2011-09-08 00:45:54 +00004198 return True
4199
erg@google.com4e00b9a2009-01-12 23:05:11 +00004200
4201_HEADERS_CONTAINING_TEMPLATES = (
4202 ('<deque>', ('deque',)),
4203 ('<functional>', ('unary_function', 'binary_function',
4204 'plus', 'minus', 'multiplies', 'divides', 'modulus',
4205 'negate',
4206 'equal_to', 'not_equal_to', 'greater', 'less',
4207 'greater_equal', 'less_equal',
4208 'logical_and', 'logical_or', 'logical_not',
4209 'unary_negate', 'not1', 'binary_negate', 'not2',
4210 'bind1st', 'bind2nd',
4211 'pointer_to_unary_function',
4212 'pointer_to_binary_function',
4213 'ptr_fun',
4214 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
4215 'mem_fun_ref_t',
4216 'const_mem_fun_t', 'const_mem_fun1_t',
4217 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
4218 'mem_fun_ref',
4219 )),
4220 ('<limits>', ('numeric_limits',)),
4221 ('<list>', ('list',)),
4222 ('<map>', ('map', 'multimap',)),
4223 ('<memory>', ('allocator',)),
4224 ('<queue>', ('queue', 'priority_queue',)),
4225 ('<set>', ('set', 'multiset',)),
4226 ('<stack>', ('stack',)),
4227 ('<string>', ('char_traits', 'basic_string',)),
4228 ('<utility>', ('pair',)),
4229 ('<vector>', ('vector',)),
4230
4231 # gcc extensions.
4232 # Note: std::hash is their hash, ::hash is our hash
4233 ('<hash_map>', ('hash_map', 'hash_multimap',)),
4234 ('<hash_set>', ('hash_set', 'hash_multiset',)),
4235 ('<slist>', ('slist',)),
4236 )
4237
erg@google.com4e00b9a2009-01-12 23:05:11 +00004238_RE_PATTERN_STRING = re.compile(r'\bstring\b')
4239
4240_re_pattern_algorithm_header = []
erg@google.coma87abb82009-02-24 01:41:01 +00004241for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
4242 'transform'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004243 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
4244 # type::max().
4245 _re_pattern_algorithm_header.append(
4246 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
4247 _template,
4248 '<algorithm>'))
4249
4250_re_pattern_templates = []
4251for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
4252 for _template in _templates:
4253 _re_pattern_templates.append(
4254 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
4255 _template + '<>',
4256 _header))
4257
4258
erg@google.come35f7652009-06-19 20:52:09 +00004259def FilesBelongToSameModule(filename_cc, filename_h):
4260 """Check if these two filenames belong to the same module.
4261
4262 The concept of a 'module' here is a as follows:
4263 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
4264 same 'module' if they are in the same directory.
4265 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
4266 to belong to the same module here.
4267
4268 If the filename_cc contains a longer path than the filename_h, for example,
4269 '/absolute/path/to/base/sysinfo.cc', and this file would include
4270 'base/sysinfo.h', this function also produces the prefix needed to open the
4271 header. This is used by the caller of this function to more robustly open the
4272 header file. We don't have access to the real include paths in this context,
4273 so we need this guesswork here.
4274
4275 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
4276 according to this implementation. Because of this, this function gives
4277 some false positives. This should be sufficiently rare in practice.
4278
4279 Args:
4280 filename_cc: is the path for the .cc file
4281 filename_h: is the path for the header path
4282
4283 Returns:
4284 Tuple with a bool and a string:
4285 bool: True if filename_cc and filename_h belong to the same module.
4286 string: the additional prefix needed to open the header file.
4287 """
4288
4289 if not filename_cc.endswith('.cc'):
4290 return (False, '')
4291 filename_cc = filename_cc[:-len('.cc')]
4292 if filename_cc.endswith('_unittest'):
4293 filename_cc = filename_cc[:-len('_unittest')]
4294 elif filename_cc.endswith('_test'):
4295 filename_cc = filename_cc[:-len('_test')]
4296 filename_cc = filename_cc.replace('/public/', '/')
4297 filename_cc = filename_cc.replace('/internal/', '/')
4298
4299 if not filename_h.endswith('.h'):
4300 return (False, '')
4301 filename_h = filename_h[:-len('.h')]
4302 if filename_h.endswith('-inl'):
4303 filename_h = filename_h[:-len('-inl')]
4304 filename_h = filename_h.replace('/public/', '/')
4305 filename_h = filename_h.replace('/internal/', '/')
4306
4307 files_belong_to_same_module = filename_cc.endswith(filename_h)
4308 common_path = ''
4309 if files_belong_to_same_module:
4310 common_path = filename_cc[:-len(filename_h)]
4311 return files_belong_to_same_module, common_path
4312
4313
4314def UpdateIncludeState(filename, include_state, io=codecs):
4315 """Fill up the include_state with new includes found from the file.
4316
4317 Args:
4318 filename: the name of the header to read.
4319 include_state: an _IncludeState instance in which the headers are inserted.
4320 io: The io factory to use to read the file. Provided for testability.
4321
4322 Returns:
4323 True if a header was succesfully added. False otherwise.
4324 """
4325 headerfile = None
4326 try:
4327 headerfile = io.open(filename, 'r', 'utf8', 'replace')
4328 except IOError:
4329 return False
4330 linenum = 0
4331 for line in headerfile:
4332 linenum += 1
4333 clean_line = CleanseComments(line)
4334 match = _RE_PATTERN_INCLUDE.search(clean_line)
4335 if match:
4336 include = match.group(2)
4337 # The value formatting is cute, but not really used right now.
4338 # What matters here is that the key is in include_state.
4339 include_state.setdefault(include, '%s:%d' % (filename, linenum))
4340 return True
4341
4342
4343def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
4344 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004345 """Reports for missing stl includes.
4346
4347 This function will output warnings to make sure you are including the headers
4348 necessary for the stl containers and functions that you use. We only give one
4349 reason to include a header. For example, if you use both equal_to<> and
4350 less<> in a .h file, only one (the latter in the file) of these will be
4351 reported as a reason to include the <functional>.
4352
erg@google.com4e00b9a2009-01-12 23:05:11 +00004353 Args:
4354 filename: The name of the current file.
4355 clean_lines: A CleansedLines instance containing the file.
4356 include_state: An _IncludeState instance.
4357 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00004358 io: The IO factory to use to read the header file. Provided for unittest
4359 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004360 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00004361 required = {} # A map of header name to linenumber and the template entity.
4362 # Example of required: { '<functional>': (1219, 'less<>') }
4363
4364 for linenum in xrange(clean_lines.NumLines()):
4365 line = clean_lines.elided[linenum]
4366 if not line or line[0] == '#':
4367 continue
4368
4369 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004370 matched = _RE_PATTERN_STRING.search(line)
4371 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00004372 # Don't warn about strings in non-STL namespaces:
4373 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00004374 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00004375 if prefix.endswith('std::') or not prefix.endswith('::'):
4376 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004377
4378 for pattern, template, header in _re_pattern_algorithm_header:
4379 if pattern.search(line):
4380 required[header] = (linenum, template)
4381
4382 # The following function is just a speed up, no semantics are changed.
4383 if not '<' in line: # Reduces the cpu time usage by skipping lines.
4384 continue
4385
4386 for pattern, template, header in _re_pattern_templates:
4387 if pattern.search(line):
4388 required[header] = (linenum, template)
4389
erg@google.come35f7652009-06-19 20:52:09 +00004390 # The policy is that if you #include something in foo.h you don't need to
4391 # include it again in foo.cc. Here, we will look at possible includes.
4392 # Let's copy the include_state so it is only messed up within this function.
4393 include_state = include_state.copy()
4394
4395 # Did we find the header for this file (if any) and succesfully load it?
4396 header_found = False
4397
4398 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00004399 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00004400
4401 # For Emacs's flymake.
4402 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
4403 # by flymake and that file name might end with '_flymake.cc'. In that case,
4404 # restore original file name here so that the corresponding header file can be
4405 # found.
4406 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
4407 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00004408 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00004409
4410 # include_state is modified during iteration, so we iterate over a copy of
4411 # the keys.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004412 header_keys = include_state.keys()
4413 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00004414 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
4415 fullpath = common_path + header
4416 if same_module and UpdateIncludeState(fullpath, include_state, io):
4417 header_found = True
4418
4419 # If we can't find the header file for a .cc, assume it's because we don't
4420 # know where to look. In that case we'll give up as we're not sure they
4421 # didn't include it in the .h file.
4422 # TODO(unknown): Do a better job of finding .h files so we are confident that
4423 # not having the .h file means there isn't one.
4424 if filename.endswith('.cc') and not header_found:
4425 return
4426
erg@google.com4e00b9a2009-01-12 23:05:11 +00004427 # All the lines have been processed, report the errors found.
4428 for required_header_unstripped in required:
4429 template = required[required_header_unstripped][1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004430 if required_header_unstripped.strip('<>"') not in include_state:
4431 error(filename, required[required_header_unstripped][0],
4432 'build/include_what_you_use', 4,
4433 'Add #include ' + required_header_unstripped + ' for ' + template)
4434
4435
erg@google.com8a95ecc2011-09-08 00:45:54 +00004436_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
4437
4438
4439def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
4440 """Check that make_pair's template arguments are deduced.
4441
4442 G++ 4.6 in C++0x mode fails badly if make_pair's template arguments are
4443 specified explicitly, and such use isn't intended in any case.
4444
4445 Args:
4446 filename: The name of the current file.
4447 clean_lines: A CleansedLines instance containing the file.
4448 linenum: The number of the line to check.
4449 error: The function to call with any errors found.
4450 """
erg@google.com2aa59982013-10-28 19:09:25 +00004451 line = clean_lines.elided[linenum]
erg@google.com8a95ecc2011-09-08 00:45:54 +00004452 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
4453 if match:
4454 error(filename, linenum, 'build/explicit_make_pair',
4455 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00004456 'For C++11-compatibility, omit template arguments from make_pair'
4457 ' OR use pair directly OR if appropriate, construct a pair directly')
erg@google.com8a95ecc2011-09-08 00:45:54 +00004458
4459
erg@google.comd350fe52013-01-14 17:51:48 +00004460def ProcessLine(filename, file_extension, clean_lines, line,
4461 include_state, function_state, nesting_state, error,
4462 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004463 """Processes a single line in the file.
4464
4465 Args:
4466 filename: Filename of the file that is being processed.
4467 file_extension: The extension (dot not included) of the file.
4468 clean_lines: An array of strings, each representing a line of the file,
4469 with comments stripped.
4470 line: Number of line being processed.
4471 include_state: An _IncludeState instance in which the headers are inserted.
4472 function_state: A _FunctionState instance which counts function lines, etc.
erg@google.comd350fe52013-01-14 17:51:48 +00004473 nesting_state: A _NestingState instance which maintains information about
4474 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004475 error: A callable to which errors are reported, which takes 4 arguments:
4476 filename, line number, error level, and message
erg@google.comefeacdf2011-09-07 21:12:16 +00004477 extra_check_functions: An array of additional check functions that will be
4478 run on each source line. Each function takes 4
4479 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00004480 """
4481 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00004482 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004483 nesting_state.Update(filename, clean_lines, line, error)
4484 if nesting_state.stack and nesting_state.stack[-1].inline_asm != _NO_ASM:
4485 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00004486 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004487 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004488 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004489 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
erg@google.comfd5da632013-10-25 17:39:45 +00004490 nesting_state, error)
erg@google.comc6671232013-10-25 21:44:03 +00004491 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004492 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00004493 nesting_state, error)
erg@google.com2aa59982013-10-28 19:09:25 +00004494 CheckVlogArguments(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004495 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00004496 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00004497 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
erg@google.comefeacdf2011-09-07 21:12:16 +00004498 for check_fn in extra_check_functions:
4499 check_fn(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004500
erg@google.comefeacdf2011-09-07 21:12:16 +00004501def ProcessFileData(filename, file_extension, lines, error,
4502 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004503 """Performs lint checks and reports any errors to the given error function.
4504
4505 Args:
4506 filename: Filename of the file that is being processed.
4507 file_extension: The extension (dot not included) of the file.
4508 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00004509 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004510 error: A callable to which errors are reported, which takes 4 arguments:
erg@google.comefeacdf2011-09-07 21:12:16 +00004511 filename, line number, error level, and message
4512 extra_check_functions: An array of additional check functions that will be
4513 run on each source line. Each function takes 4
4514 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00004515 """
4516 lines = (['// marker so line numbers and indices both start at 1'] + lines +
4517 ['// marker so line numbers end in a known way'])
4518
4519 include_state = _IncludeState()
4520 function_state = _FunctionState()
erg@google.comd350fe52013-01-14 17:51:48 +00004521 nesting_state = _NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004522
erg+personal@google.com05189642010-04-30 20:43:03 +00004523 ResetNolintSuppressions()
4524
erg@google.com4e00b9a2009-01-12 23:05:11 +00004525 CheckForCopyright(filename, lines, error)
4526
4527 if file_extension == 'h':
4528 CheckForHeaderGuard(filename, lines, error)
4529
4530 RemoveMultiLineComments(filename, lines, error)
4531 clean_lines = CleansedLines(lines)
4532 for line in xrange(clean_lines.NumLines()):
4533 ProcessLine(filename, file_extension, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00004534 include_state, function_state, nesting_state, error,
erg@google.comefeacdf2011-09-07 21:12:16 +00004535 extra_check_functions)
erg@google.com2aa59982013-10-28 19:09:25 +00004536 nesting_state.CheckCompletedBlocks(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004537
4538 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
4539
4540 # We check here rather than inside ProcessLine so that we see raw
4541 # lines rather than "cleaned" lines.
erg@google.com2aa59982013-10-28 19:09:25 +00004542 CheckForBadCharacters(filename, lines, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004543
4544 CheckForNewlineAtEOF(filename, lines, error)
4545
erg@google.comefeacdf2011-09-07 21:12:16 +00004546def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004547 """Does google-lint on a single file.
4548
4549 Args:
4550 filename: The name of the file to parse.
4551
4552 vlevel: The level of errors to report. Every error of confidence
4553 >= verbose_level will be reported. 0 is a good default.
erg@google.comefeacdf2011-09-07 21:12:16 +00004554
4555 extra_check_functions: An array of additional check functions that will be
4556 run on each source line. Each function takes 4
4557 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00004558 """
4559
4560 _SetVerboseLevel(vlevel)
4561
4562 try:
4563 # Support the UNIX convention of using "-" for stdin. Note that
4564 # we are not opening the file with universal newline support
4565 # (which codecs doesn't support anyway), so the resulting lines do
4566 # contain trailing '\r' characters if we are reading a file that
4567 # has CRLF endings.
4568 # If after the split a trailing '\r' is present, it is removed
4569 # below. If it is not expected to be present (i.e. os.linesep !=
4570 # '\r\n' as in Windows), a warning is issued below if this file
4571 # is processed.
4572
4573 if filename == '-':
4574 lines = codecs.StreamReaderWriter(sys.stdin,
4575 codecs.getreader('utf8'),
4576 codecs.getwriter('utf8'),
4577 'replace').read().split('\n')
4578 else:
4579 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
4580
4581 carriage_return_found = False
4582 # Remove trailing '\r'.
4583 for linenum in range(len(lines)):
4584 if lines[linenum].endswith('\r'):
4585 lines[linenum] = lines[linenum].rstrip('\r')
4586 carriage_return_found = True
4587
4588 except IOError:
4589 sys.stderr.write(
4590 "Skipping input '%s': Can't open for reading\n" % filename)
4591 return
4592
4593 # Note, if no dot is found, this will give the entire filename as the ext.
4594 file_extension = filename[filename.rfind('.') + 1:]
4595
4596 # When reading from stdin, the extension is unknown, so no cpplint tests
4597 # should rely on the extension.
erg@google.com2aa59982013-10-28 19:09:25 +00004598 valid_extensions = ['cc', 'h', 'cpp', 'cu', 'cuh']
4599 if filename != '-' and file_extension not in valid_extensions:
4600 sys.stderr.write('Ignoring %s; not a valid file name '
4601 '(.cc, .h, .cpp, .cu, .cuh)\n' % filename)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004602 else:
erg@google.comefeacdf2011-09-07 21:12:16 +00004603 ProcessFileData(filename, file_extension, lines, Error,
4604 extra_check_functions)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004605 if carriage_return_found and os.linesep != '\r\n':
erg@google.com8a95ecc2011-09-08 00:45:54 +00004606 # Use 0 for linenum since outputting only one error for potentially
erg@google.com4e00b9a2009-01-12 23:05:11 +00004607 # several lines.
4608 Error(filename, 0, 'whitespace/newline', 1,
4609 'One or more unexpected \\r (^M) found;'
4610 'better to use only a \\n')
4611
4612 sys.stderr.write('Done processing %s\n' % filename)
4613
4614
4615def PrintUsage(message):
4616 """Prints a brief usage string and exits, optionally with an error message.
4617
4618 Args:
4619 message: The optional error message.
4620 """
4621 sys.stderr.write(_USAGE)
4622 if message:
4623 sys.exit('\nFATAL ERROR: ' + message)
4624 else:
4625 sys.exit(1)
4626
4627
4628def PrintCategories():
4629 """Prints a list of all the error-categories used by error messages.
4630
4631 These are the categories used to filter messages via --filter.
4632 """
erg+personal@google.com05189642010-04-30 20:43:03 +00004633 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004634 sys.exit(0)
4635
4636
4637def ParseArguments(args):
4638 """Parses the command line arguments.
4639
4640 This may set the output format and verbosity level as side-effects.
4641
4642 Args:
4643 args: The command line arguments:
4644
4645 Returns:
4646 The list of filenames to lint.
4647 """
4648 try:
4649 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00004650 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00004651 'filter=',
4652 'root='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00004653 except getopt.GetoptError:
4654 PrintUsage('Invalid arguments.')
4655
4656 verbosity = _VerboseLevel()
4657 output_format = _OutputFormat()
4658 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00004659 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00004660
4661 for (opt, val) in opts:
4662 if opt == '--help':
4663 PrintUsage(None)
4664 elif opt == '--output':
erg@google.comc6671232013-10-25 21:44:03 +00004665 if val not in ('emacs', 'vs7', 'eclipse'):
erg@google.com02c27fd2013-05-28 21:34:34 +00004666 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004667 output_format = val
4668 elif opt == '--verbose':
4669 verbosity = int(val)
4670 elif opt == '--filter':
4671 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00004672 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004673 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00004674 elif opt == '--counting':
4675 if val not in ('total', 'toplevel', 'detailed'):
4676 PrintUsage('Valid counting options are total, toplevel, and detailed')
4677 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00004678 elif opt == '--root':
4679 global _root
4680 _root = val
erg@google.com4e00b9a2009-01-12 23:05:11 +00004681
4682 if not filenames:
4683 PrintUsage('No files were specified.')
4684
4685 _SetOutputFormat(output_format)
4686 _SetVerboseLevel(verbosity)
4687 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00004688 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004689
4690 return filenames
4691
4692
4693def main():
4694 filenames = ParseArguments(sys.argv[1:])
4695
4696 # Change stderr to write with replacement characters so we don't die
4697 # if we try to print something containing non-ASCII characters.
4698 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
4699 codecs.getreader('utf8'),
4700 codecs.getwriter('utf8'),
4701 'replace')
4702
erg@google.coma868d2d2009-10-09 21:18:45 +00004703 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004704 for filename in filenames:
4705 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00004706 _cpplint_state.PrintErrorCounts()
4707
erg@google.com4e00b9a2009-01-12 23:05:11 +00004708 sys.exit(_cpplint_state.error_count > 0)
4709
4710
4711if __name__ == '__main__':
4712 main()