blob: 30113453fe86b1af4400f65820ff4eb1e84b466b [file] [log] [blame]
erg@chromium.orgd528f8b2012-05-11 17:31:08 +00001#!/usr/bin/env python
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002#
erg@google.com26970fa2009-11-17 18:07:32 +00003# Copyright (c) 2009 Google Inc. All rights reserved.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004#
erg@google.com26970fa2009-11-17 18:07:32 +00005# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00008#
erg@google.com26970fa2009-11-17 18:07:32 +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.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000018#
erg@google.com26970fa2009-11-17 18:07:32 +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.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000030
maruel@google.comfb2b8eb2009-04-23 21:03:42 +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
mazda@chromium.org3fffcec2013-06-07 01:04:53 +000045import copy
maruel@google.comfb2b8eb2009-04-23 21:03:42 +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,...]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +000058 [--counting=total|toplevel|detailed] [--root=subdir]
59 [--linelength=digits]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000060 <file> [file] ...
61
62 The style guidelines this tries to follow are those in
63 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
64
65 Every problem is given a confidence score from 1-5, with 5 meaning we are
66 certain of the problem, and 1 meaning it could be a legitimate construct.
67 This will miss some errors, and is not a substitute for a code review.
68
erg@google.com35589e62010-11-17 18:58:16 +000069 To suppress false-positive errors of a certain category, add a
70 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
71 suppresses errors of all categories on that line.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000072
73 The files passed in will be linted; at least one file must be provided.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +000074 Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the
75 extensions with the --extensions flag.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000076
77 Flags:
78
79 output=vs7
80 By default, the output is formatted to ease emacs parsing. Visual Studio
81 compatible output (vs7) may also be used. Other formats are unsupported.
82
83 verbose=#
84 Specify a number 0-5 to restrict errors to certain verbosity levels.
85
86 filter=-x,+y,...
87 Specify a comma-separated list of category-filters to apply: only
88 error messages whose category names pass the filters will be printed.
89 (Category names are printed with the message and look like
90 "[whitespace/indent]".) Filters are evaluated left to right.
91 "-FOO" and "FOO" means "do not print categories that start with FOO".
92 "+FOO" means "do print categories that start with FOO".
93
94 Examples: --filter=-whitespace,+whitespace/braces
95 --filter=whitespace,runtime/printf,+runtime/printf_format
96 --filter=-,+build/include_what_you_use
97
98 To see a list of all the categories used in cpplint, pass no arg:
99 --filter=
erg@google.com26970fa2009-11-17 18:07:32 +0000100
101 counting=total|toplevel|detailed
102 The total number of errors found is always printed. If
103 'toplevel' is provided, then the count of errors in each of
104 the top-level categories like 'build' and 'whitespace' will
105 also be printed. If 'detailed' is provided, then a count
106 is provided for each category like 'build/class'.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000107
108 root=subdir
109 The root directory used for deriving header guard CPP variable.
110 By default, the header guard CPP variable is calculated as the relative
111 path to the directory that contains .git, .hg, or .svn. When this flag
112 is specified, the relative path is calculated from the specified
113 directory. If the specified directory does not exist, this flag is
114 ignored.
115
116 Examples:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000117 Assuming that src/.git exists, the header guard CPP variables for
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000118 src/chrome/browser/ui/browser.h are:
119
120 No flag => CHROME_BROWSER_UI_BROWSER_H_
121 --root=chrome => BROWSER_UI_BROWSER_H_
122 --root=chrome/browser => UI_BROWSER_H_
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000123
124 linelength=digits
125 This is the allowed line length for the project. The default value is
126 80 characters.
127
128 Examples:
129 --linelength=120
130
131 extensions=extension,extension,...
132 The allowed file extensions that cpplint will check
133
134 Examples:
135 --extensions=hpp,cpp
avakulenko@google.com17449932014-07-28 22:13:33 +0000136
137 cpplint.py supports per-directory configurations specified in CPPLINT.cfg
138 files. CPPLINT.cfg file can contain a number of key=value pairs.
139 Currently the following options are supported:
140
141 set noparent
142 filter=+filter1,-filter2,...
143 exclude_files=regex
avakulenko@google.com68a4fa62014-08-25 16:26:18 +0000144 linelength=80
avakulenko@google.com17449932014-07-28 22:13:33 +0000145
146 "set noparent" option prevents cpplint from traversing directory tree
147 upwards looking for more .cfg files in parent directories. This option
148 is usually placed in the top-level project directory.
149
150 The "filter" option is similar in function to --filter flag. It specifies
151 message filters in addition to the |_DEFAULT_FILTERS| and those specified
152 through --filter command-line flag.
153
154 "exclude_files" allows to specify a regular expression to be matched against
155 a file name. If the expression matches, the file is skipped and not run
156 through liner.
157
avakulenko@google.com68a4fa62014-08-25 16:26:18 +0000158 "linelength" allows to specify the allowed line length for the project.
159
avakulenko@google.com17449932014-07-28 22:13:33 +0000160 CPPLINT.cfg has an effect on files in the same directory and all
161 sub-directories, unless overridden by a nested configuration file.
162
163 Example file:
164 filter=-build/include_order,+build/include_alpha
165 exclude_files=.*\.cc
166
167 The above example disables build/include_order warning and enables
168 build/include_alpha as well as excludes all .cc from being
169 processed by linter, in the current directory (where the .cfg
170 file is located) and all sub-directories.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000171"""
172
173# We categorize each error message we print. Here are the categories.
174# We want an explicit list so we can list them all in cpplint --filter=.
175# If you add a new error message with a new category, add it to the list
176# here! cpplint_unittest.py should tell you if you forget to do this.
erg@google.com35589e62010-11-17 18:58:16 +0000177_ERROR_CATEGORIES = [
178 'build/class',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000179 'build/c++11',
erg@google.com35589e62010-11-17 18:58:16 +0000180 'build/deprecated',
181 'build/endif_comment',
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000182 'build/explicit_make_pair',
erg@google.com35589e62010-11-17 18:58:16 +0000183 'build/forward_decl',
184 'build/header_guard',
185 'build/include',
186 'build/include_alpha',
187 'build/include_order',
188 'build/include_what_you_use',
189 'build/namespaces',
190 'build/printf_format',
191 'build/storage_class',
192 'legal/copyright',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000193 'readability/alt_tokens',
erg@google.com35589e62010-11-17 18:58:16 +0000194 'readability/braces',
195 'readability/casting',
196 'readability/check',
197 'readability/constructors',
198 'readability/fn_size',
199 'readability/function',
avakulenko@google.com59146752014-08-11 20:20:55 +0000200 'readability/inheritance',
erg@google.com35589e62010-11-17 18:58:16 +0000201 'readability/multiline_comment',
202 'readability/multiline_string',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000203 'readability/namespace',
erg@google.com35589e62010-11-17 18:58:16 +0000204 'readability/nolint',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000205 'readability/nul',
erg@google.com35589e62010-11-17 18:58:16 +0000206 'readability/streams',
207 'readability/todo',
208 'readability/utf8',
209 'runtime/arrays',
210 'runtime/casting',
211 'runtime/explicit',
212 'runtime/int',
213 'runtime/init',
214 'runtime/invalid_increment',
215 'runtime/member_string_references',
216 'runtime/memset',
avakulenko@google.com59146752014-08-11 20:20:55 +0000217 'runtime/indentation_namespace',
erg@google.com35589e62010-11-17 18:58:16 +0000218 'runtime/operator',
219 'runtime/printf',
220 'runtime/printf_format',
221 'runtime/references',
erg@google.com35589e62010-11-17 18:58:16 +0000222 'runtime/string',
223 'runtime/threadsafe_fn',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000224 'runtime/vlog',
erg@google.com35589e62010-11-17 18:58:16 +0000225 'whitespace/blank_line',
226 'whitespace/braces',
227 'whitespace/comma',
228 'whitespace/comments',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000229 'whitespace/empty_conditional_body',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000230 'whitespace/empty_loop_body',
erg@google.com35589e62010-11-17 18:58:16 +0000231 'whitespace/end_of_line',
232 'whitespace/ending_newline',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000233 'whitespace/forcolon',
erg@google.com35589e62010-11-17 18:58:16 +0000234 'whitespace/indent',
erg@google.com35589e62010-11-17 18:58:16 +0000235 'whitespace/line_length',
236 'whitespace/newline',
237 'whitespace/operators',
238 'whitespace/parens',
239 'whitespace/semicolon',
240 'whitespace/tab',
241 'whitespace/todo'
242 ]
erg@google.com6317a9c2009-06-25 00:28:19 +0000243
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000244# The default state of the category filter. This is overridden by the --filter=
erg@google.com6317a9c2009-06-25 00:28:19 +0000245# flag. By default all errors are on, so only add here categories that should be
246# off by default (i.e., categories that must be enabled by the --filter= flags).
247# All entries here should start with a '-' or '+', as in the --filter= flag.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000248_DEFAULT_FILTERS = ['-build/include_alpha']
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000249
250# We used to check for high-bit characters, but after much discussion we
251# decided those were OK, as long as they were in UTF-8 and didn't represent
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000252# hard-coded international strings, which belong in a separate i18n file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000253
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000254# C++ headers
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000255_CPP_HEADERS = frozenset([
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000256 # Legacy
257 'algobase.h',
258 'algo.h',
259 'alloc.h',
260 'builtinbuf.h',
261 'bvector.h',
262 'complex.h',
263 'defalloc.h',
264 'deque.h',
265 'editbuf.h',
266 'fstream.h',
267 'function.h',
268 'hash_map',
269 'hash_map.h',
270 'hash_set',
271 'hash_set.h',
272 'hashtable.h',
273 'heap.h',
274 'indstream.h',
275 'iomanip.h',
276 'iostream.h',
277 'istream.h',
278 'iterator.h',
279 'list.h',
280 'map.h',
281 'multimap.h',
282 'multiset.h',
283 'ostream.h',
284 'pair.h',
285 'parsestream.h',
286 'pfstream.h',
287 'procbuf.h',
288 'pthread_alloc',
289 'pthread_alloc.h',
290 'rope',
291 'rope.h',
292 'ropeimpl.h',
293 'set.h',
294 'slist',
295 'slist.h',
296 'stack.h',
297 'stdiostream.h',
298 'stl_alloc.h',
299 'stl_relops.h',
300 'streambuf.h',
301 'stream.h',
302 'strfile.h',
303 'strstream.h',
304 'tempbuf.h',
305 'tree.h',
306 'type_traits.h',
307 'vector.h',
308 # 17.6.1.2 C++ library headers
309 'algorithm',
310 'array',
311 'atomic',
312 'bitset',
313 'chrono',
314 'codecvt',
315 'complex',
316 'condition_variable',
317 'deque',
318 'exception',
319 'forward_list',
320 'fstream',
321 'functional',
322 'future',
323 'initializer_list',
324 'iomanip',
325 'ios',
326 'iosfwd',
327 'iostream',
328 'istream',
329 'iterator',
330 'limits',
331 'list',
332 'locale',
333 'map',
334 'memory',
335 'mutex',
336 'new',
337 'numeric',
338 'ostream',
339 'queue',
340 'random',
341 'ratio',
342 'regex',
343 'set',
344 'sstream',
345 'stack',
346 'stdexcept',
347 'streambuf',
348 'string',
349 'strstream',
350 'system_error',
351 'thread',
352 'tuple',
353 'typeindex',
354 'typeinfo',
355 'type_traits',
356 'unordered_map',
357 'unordered_set',
358 'utility',
359 'valarray',
360 'vector',
361 # 17.6.1.2 C++ headers for C library facilities
362 'cassert',
363 'ccomplex',
364 'cctype',
365 'cerrno',
366 'cfenv',
367 'cfloat',
368 'cinttypes',
369 'ciso646',
370 'climits',
371 'clocale',
372 'cmath',
373 'csetjmp',
374 'csignal',
375 'cstdalign',
376 'cstdarg',
377 'cstdbool',
378 'cstddef',
379 'cstdint',
380 'cstdio',
381 'cstdlib',
382 'cstring',
383 'ctgmath',
384 'ctime',
385 'cuchar',
386 'cwchar',
387 'cwctype',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000388 ])
389
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000390
avakulenko@google.com59146752014-08-11 20:20:55 +0000391# These headers are excluded from [build/include] and [build/include_order]
392# checks:
393# - Anything not following google file name conventions (containing an
394# uppercase character, such as Python.h or nsStringAPI.h, for example).
395# - Lua headers.
396_THIRD_PARTY_HEADERS_PATTERN = re.compile(
397 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
398
399
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000400# Assertion macros. These are defined in base/logging.h and
401# testing/base/gunit.h. Note that the _M versions need to come first
402# for substring matching to work.
403_CHECK_MACROS = [
erg@google.com6317a9c2009-06-25 00:28:19 +0000404 'DCHECK', 'CHECK',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000405 'EXPECT_TRUE_M', 'EXPECT_TRUE',
406 'ASSERT_TRUE_M', 'ASSERT_TRUE',
407 'EXPECT_FALSE_M', 'EXPECT_FALSE',
408 'ASSERT_FALSE_M', 'ASSERT_FALSE',
409 ]
410
erg@google.com6317a9c2009-06-25 00:28:19 +0000411# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000412_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
413
414for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
415 ('>=', 'GE'), ('>', 'GT'),
416 ('<=', 'LE'), ('<', 'LT')]:
erg@google.com6317a9c2009-06-25 00:28:19 +0000417 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000418 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
419 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
420 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
421 _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
422 _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
423
424for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
425 ('>=', 'LT'), ('>', 'LE'),
426 ('<=', 'GT'), ('<', 'GE')]:
427 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
428 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
429 _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
430 _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
431
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000432# Alternative tokens and their replacements. For full list, see section 2.5
433# Alternative tokens [lex.digraph] in the C++ standard.
434#
435# Digraphs (such as '%:') are not included here since it's a mess to
436# match those on a word boundary.
437_ALT_TOKEN_REPLACEMENT = {
438 'and': '&&',
439 'bitor': '|',
440 'or': '||',
441 'xor': '^',
442 'compl': '~',
443 'bitand': '&',
444 'and_eq': '&=',
445 'or_eq': '|=',
446 'xor_eq': '^=',
447 'not': '!',
448 'not_eq': '!='
449 }
450
451# Compile regular expression that matches all the above keywords. The "[ =()]"
452# bit is meant to avoid matching these keywords outside of boolean expressions.
453#
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000454# False positives include C-style multi-line comments and multi-line strings
455# but those have always been troublesome for cpplint.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000456_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
457 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
458
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000459
460# These constants define types of headers for use with
461# _IncludeState.CheckNextIncludeOrder().
462_C_SYS_HEADER = 1
463_CPP_SYS_HEADER = 2
464_LIKELY_MY_HEADER = 3
465_POSSIBLE_MY_HEADER = 4
466_OTHER_HEADER = 5
467
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000468# These constants define the current inline assembly state
469_NO_ASM = 0 # Outside of inline assembly block
470_INSIDE_ASM = 1 # Inside inline assembly block
471_END_ASM = 2 # Last line of inline assembly block
472_BLOCK_ASM = 3 # The whole block is an inline assembly block
473
474# Match start of assembly blocks
475_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
476 r'(?:\s+(volatile|__volatile__))?'
477 r'\s*[{(]')
478
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000479
480_regexp_compile_cache = {}
481
erg@google.com35589e62010-11-17 18:58:16 +0000482# {str, set(int)}: a map from error categories to sets of linenumbers
483# on which those errors are expected and should be suppressed.
484_error_suppressions = {}
485
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000486# The root directory used for deriving header guard CPP variable.
487# This is set by --root flag.
488_root = None
489
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000490# The allowed line length of files.
491# This is set by --linelength flag.
492_line_length = 80
493
494# The allowed extensions for file names
495# This is set by --extensions flag.
496_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
497
erg@google.com35589e62010-11-17 18:58:16 +0000498def ParseNolintSuppressions(filename, raw_line, linenum, error):
499 """Updates the global list of error-suppressions.
500
501 Parses any NOLINT comments on the current line, updating the global
502 error_suppressions store. Reports an error if the NOLINT comment
503 was malformed.
504
505 Args:
506 filename: str, the name of the input file.
507 raw_line: str, the line of input text, with comments.
508 linenum: int, the number of the current line.
509 error: function, an error handler.
510 """
avakulenko@google.com59146752014-08-11 20:20:55 +0000511 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000512 if matched:
avakulenko@google.com59146752014-08-11 20:20:55 +0000513 if matched.group(1):
514 suppressed_line = linenum + 1
515 else:
516 suppressed_line = linenum
517 category = matched.group(2)
erg@google.com35589e62010-11-17 18:58:16 +0000518 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.com59146752014-08-11 20:20:55 +0000519 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg@google.com35589e62010-11-17 18:58:16 +0000520 else:
521 if category.startswith('(') and category.endswith(')'):
522 category = category[1:-1]
523 if category in _ERROR_CATEGORIES:
avakulenko@google.com59146752014-08-11 20:20:55 +0000524 _error_suppressions.setdefault(category, set()).add(suppressed_line)
erg@google.com35589e62010-11-17 18:58:16 +0000525 else:
526 error(filename, linenum, 'readability/nolint', 5,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000527 'Unknown NOLINT error category: %s' % category)
erg@google.com35589e62010-11-17 18:58:16 +0000528
529
530def ResetNolintSuppressions():
avakulenko@google.com59146752014-08-11 20:20:55 +0000531 """Resets the set of NOLINT suppressions to empty."""
erg@google.com35589e62010-11-17 18:58:16 +0000532 _error_suppressions.clear()
533
534
535def IsErrorSuppressedByNolint(category, linenum):
536 """Returns true if the specified error category is suppressed on this line.
537
538 Consults the global error_suppressions map populated by
539 ParseNolintSuppressions/ResetNolintSuppressions.
540
541 Args:
542 category: str, the category of the error.
543 linenum: int, the current line number.
544 Returns:
545 bool, True iff the error should be suppressed due to a NOLINT comment.
546 """
547 return (linenum in _error_suppressions.get(category, set()) or
548 linenum in _error_suppressions.get(None, set()))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000549
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000550
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000551def Match(pattern, s):
552 """Matches the string with the pattern, caching the compiled regexp."""
553 # The regexp compilation caching is inlined in both Match and Search for
554 # performance reasons; factoring it out into a separate function turns out
555 # to be noticeably expensive.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000556 if pattern not in _regexp_compile_cache:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000557 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
558 return _regexp_compile_cache[pattern].match(s)
559
560
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000561def ReplaceAll(pattern, rep, s):
562 """Replaces instances of pattern in a string with a replacement.
563
564 The compiled regex is kept in a cache shared by Match and Search.
565
566 Args:
567 pattern: regex pattern
568 rep: replacement text
569 s: search string
570
571 Returns:
572 string with replacements made (or original string if no replacements)
573 """
574 if pattern not in _regexp_compile_cache:
575 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
576 return _regexp_compile_cache[pattern].sub(rep, s)
577
578
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000579def Search(pattern, s):
580 """Searches the string for the pattern, caching the compiled regexp."""
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000581 if pattern not in _regexp_compile_cache:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000582 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
583 return _regexp_compile_cache[pattern].search(s)
584
585
avakulenko@google.com59146752014-08-11 20:20:55 +0000586class _IncludeState(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000587 """Tracks line numbers for includes, and the order in which includes appear.
588
avakulenko@google.com59146752014-08-11 20:20:55 +0000589 include_list contains list of lists of (header, line number) pairs.
590 It's a lists of lists rather than just one flat list to make it
591 easier to update across preprocessor boundaries.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000592
593 Call CheckNextIncludeOrder() once for each header in the file, passing
594 in the type constants defined above. Calls in an illegal order will
595 raise an _IncludeError with an appropriate error message.
596
597 """
598 # self._section will move monotonically through this set. If it ever
599 # needs to move backwards, CheckNextIncludeOrder will raise an error.
600 _INITIAL_SECTION = 0
601 _MY_H_SECTION = 1
602 _C_SECTION = 2
603 _CPP_SECTION = 3
604 _OTHER_H_SECTION = 4
605
606 _TYPE_NAMES = {
607 _C_SYS_HEADER: 'C system header',
608 _CPP_SYS_HEADER: 'C++ system header',
609 _LIKELY_MY_HEADER: 'header this file implements',
610 _POSSIBLE_MY_HEADER: 'header this file may implement',
611 _OTHER_HEADER: 'other header',
612 }
613 _SECTION_NAMES = {
614 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
615 _MY_H_SECTION: 'a header this file implements',
616 _C_SECTION: 'C system header',
617 _CPP_SECTION: 'C++ system header',
618 _OTHER_H_SECTION: 'other header',
619 }
620
621 def __init__(self):
avakulenko@google.com59146752014-08-11 20:20:55 +0000622 self.include_list = [[]]
623 self.ResetSection('')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000624
avakulenko@google.com59146752014-08-11 20:20:55 +0000625 def FindHeader(self, header):
626 """Check if a header has already been included.
627
628 Args:
629 header: header to check.
630 Returns:
631 Line number of previous occurrence, or -1 if the header has not
632 been seen before.
633 """
634 for section_list in self.include_list:
635 for f in section_list:
636 if f[0] == header:
637 return f[1]
638 return -1
639
640 def ResetSection(self, directive):
641 """Reset section checking for preprocessor directive.
642
643 Args:
644 directive: preprocessor directive (e.g. "if", "else").
645 """
erg@google.com26970fa2009-11-17 18:07:32 +0000646 # The name of the current section.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000647 self._section = self._INITIAL_SECTION
erg@google.com26970fa2009-11-17 18:07:32 +0000648 # The path of last found header.
649 self._last_header = ''
650
avakulenko@google.com59146752014-08-11 20:20:55 +0000651 # Update list of includes. Note that we never pop from the
652 # include list.
653 if directive in ('if', 'ifdef', 'ifndef'):
654 self.include_list.append([])
655 elif directive in ('else', 'elif'):
656 self.include_list[-1] = []
657
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000658 def SetLastHeader(self, header_path):
659 self._last_header = header_path
660
erg@google.com26970fa2009-11-17 18:07:32 +0000661 def CanonicalizeAlphabeticalOrder(self, header_path):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000662 """Returns a path canonicalized for alphabetical comparison.
erg@google.com26970fa2009-11-17 18:07:32 +0000663
664 - replaces "-" with "_" so they both cmp the same.
665 - removes '-inl' since we don't require them to be after the main header.
666 - lowercase everything, just in case.
667
668 Args:
669 header_path: Path to be canonicalized.
670
671 Returns:
672 Canonicalized path.
673 """
674 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
675
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000676 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.com26970fa2009-11-17 18:07:32 +0000677 """Check if a header is in alphabetical order with the previous header.
678
679 Args:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000680 clean_lines: A CleansedLines instance containing the file.
681 linenum: The number of the line to check.
682 header_path: Canonicalized header to be checked.
erg@google.com26970fa2009-11-17 18:07:32 +0000683
684 Returns:
685 Returns true if the header is in alphabetical order.
686 """
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000687 # If previous section is different from current section, _last_header will
688 # be reset to empty string, so it's always less than current header.
689 #
690 # If previous line was a blank line, assume that the headers are
691 # intentionally sorted the way they are.
692 if (self._last_header > header_path and
693 not Match(r'^\s*$', clean_lines.elided[linenum - 1])):
erg@google.com26970fa2009-11-17 18:07:32 +0000694 return False
erg@google.com26970fa2009-11-17 18:07:32 +0000695 return True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000696
697 def CheckNextIncludeOrder(self, header_type):
698 """Returns a non-empty error message if the next header is out of order.
699
700 This function also updates the internal state to be ready to check
701 the next include.
702
703 Args:
704 header_type: One of the _XXX_HEADER constants defined above.
705
706 Returns:
707 The empty string if the header is in the right order, or an
708 error message describing what's wrong.
709
710 """
711 error_message = ('Found %s after %s' %
712 (self._TYPE_NAMES[header_type],
713 self._SECTION_NAMES[self._section]))
714
erg@google.com26970fa2009-11-17 18:07:32 +0000715 last_section = self._section
716
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000717 if header_type == _C_SYS_HEADER:
718 if self._section <= self._C_SECTION:
719 self._section = self._C_SECTION
720 else:
erg@google.com26970fa2009-11-17 18:07:32 +0000721 self._last_header = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000722 return error_message
723 elif header_type == _CPP_SYS_HEADER:
724 if self._section <= self._CPP_SECTION:
725 self._section = self._CPP_SECTION
726 else:
erg@google.com26970fa2009-11-17 18:07:32 +0000727 self._last_header = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000728 return error_message
729 elif header_type == _LIKELY_MY_HEADER:
730 if self._section <= self._MY_H_SECTION:
731 self._section = self._MY_H_SECTION
732 else:
733 self._section = self._OTHER_H_SECTION
734 elif header_type == _POSSIBLE_MY_HEADER:
735 if self._section <= self._MY_H_SECTION:
736 self._section = self._MY_H_SECTION
737 else:
738 # This will always be the fallback because we're not sure
739 # enough that the header is associated with this file.
740 self._section = self._OTHER_H_SECTION
741 else:
742 assert header_type == _OTHER_HEADER
743 self._section = self._OTHER_H_SECTION
744
erg@google.com26970fa2009-11-17 18:07:32 +0000745 if last_section != self._section:
746 self._last_header = ''
747
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000748 return ''
749
750
751class _CppLintState(object):
752 """Maintains module-wide state.."""
753
754 def __init__(self):
755 self.verbose_level = 1 # global setting.
756 self.error_count = 0 # global count of reported errors
erg@google.com6317a9c2009-06-25 00:28:19 +0000757 # filters to apply when emitting error messages
758 self.filters = _DEFAULT_FILTERS[:]
avakulenko@google.com17449932014-07-28 22:13:33 +0000759 # backup of filter list. Used to restore the state after each file.
760 self._filters_backup = self.filters[:]
erg@google.com26970fa2009-11-17 18:07:32 +0000761 self.counting = 'total' # In what way are we counting errors?
762 self.errors_by_category = {} # string to int dict storing error counts
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000763
764 # output format:
765 # "emacs" - format that emacs can parse (default)
766 # "vs7" - format that Microsoft Visual Studio 7 can parse
767 self.output_format = 'emacs'
768
769 def SetOutputFormat(self, output_format):
770 """Sets the output format for errors."""
771 self.output_format = output_format
772
773 def SetVerboseLevel(self, level):
774 """Sets the module's verbosity, and returns the previous setting."""
775 last_verbose_level = self.verbose_level
776 self.verbose_level = level
777 return last_verbose_level
778
erg@google.com26970fa2009-11-17 18:07:32 +0000779 def SetCountingStyle(self, counting_style):
780 """Sets the module's counting options."""
781 self.counting = counting_style
782
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000783 def SetFilters(self, filters):
784 """Sets the error-message filters.
785
786 These filters are applied when deciding whether to emit a given
787 error message.
788
789 Args:
790 filters: A string of comma-separated filters (eg "+whitespace/indent").
791 Each filter should start with + or -; else we die.
erg@google.com6317a9c2009-06-25 00:28:19 +0000792
793 Raises:
794 ValueError: The comma-separated filters did not all start with '+' or '-'.
795 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000796 """
erg@google.com6317a9c2009-06-25 00:28:19 +0000797 # Default filters always have less priority than the flag ones.
798 self.filters = _DEFAULT_FILTERS[:]
avakulenko@google.com17449932014-07-28 22:13:33 +0000799 self.AddFilters(filters)
800
801 def AddFilters(self, filters):
802 """ Adds more filters to the existing list of error-message filters. """
erg@google.com6317a9c2009-06-25 00:28:19 +0000803 for filt in filters.split(','):
804 clean_filt = filt.strip()
805 if clean_filt:
806 self.filters.append(clean_filt)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000807 for filt in self.filters:
808 if not (filt.startswith('+') or filt.startswith('-')):
809 raise ValueError('Every filter in --filters must start with + or -'
810 ' (%s does not)' % filt)
811
avakulenko@google.com17449932014-07-28 22:13:33 +0000812 def BackupFilters(self):
813 """ Saves the current filter list to backup storage."""
814 self._filters_backup = self.filters[:]
815
816 def RestoreFilters(self):
817 """ Restores filters previously backed up."""
818 self.filters = self._filters_backup[:]
819
erg@google.com26970fa2009-11-17 18:07:32 +0000820 def ResetErrorCounts(self):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000821 """Sets the module's error statistic back to zero."""
822 self.error_count = 0
erg@google.com26970fa2009-11-17 18:07:32 +0000823 self.errors_by_category = {}
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000824
erg@google.com26970fa2009-11-17 18:07:32 +0000825 def IncrementErrorCount(self, category):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000826 """Bumps the module's error statistic."""
827 self.error_count += 1
erg@google.com26970fa2009-11-17 18:07:32 +0000828 if self.counting in ('toplevel', 'detailed'):
829 if self.counting != 'detailed':
830 category = category.split('/')[0]
831 if category not in self.errors_by_category:
832 self.errors_by_category[category] = 0
833 self.errors_by_category[category] += 1
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000834
erg@google.com26970fa2009-11-17 18:07:32 +0000835 def PrintErrorCounts(self):
836 """Print a summary of errors by category, and the total."""
837 for category, count in self.errors_by_category.iteritems():
838 sys.stderr.write('Category \'%s\' errors found: %d\n' %
839 (category, count))
840 sys.stderr.write('Total errors found: %d\n' % self.error_count)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000841
842_cpplint_state = _CppLintState()
843
844
845def _OutputFormat():
846 """Gets the module's output format."""
847 return _cpplint_state.output_format
848
849
850def _SetOutputFormat(output_format):
851 """Sets the module's output format."""
852 _cpplint_state.SetOutputFormat(output_format)
853
854
855def _VerboseLevel():
856 """Returns the module's verbosity setting."""
857 return _cpplint_state.verbose_level
858
859
860def _SetVerboseLevel(level):
861 """Sets the module's verbosity, and returns the previous setting."""
862 return _cpplint_state.SetVerboseLevel(level)
863
864
erg@google.com26970fa2009-11-17 18:07:32 +0000865def _SetCountingStyle(level):
866 """Sets the module's counting options."""
867 _cpplint_state.SetCountingStyle(level)
868
869
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000870def _Filters():
871 """Returns the module's list of output filters, as a list."""
872 return _cpplint_state.filters
873
874
875def _SetFilters(filters):
876 """Sets the module's error-message filters.
877
878 These filters are applied when deciding whether to emit a given
879 error message.
880
881 Args:
882 filters: A string of comma-separated filters (eg "whitespace/indent").
883 Each filter should start with + or -; else we die.
884 """
885 _cpplint_state.SetFilters(filters)
886
avakulenko@google.com17449932014-07-28 22:13:33 +0000887def _AddFilters(filters):
888 """Adds more filter overrides.
avakulenko@google.com59146752014-08-11 20:20:55 +0000889
avakulenko@google.com17449932014-07-28 22:13:33 +0000890 Unlike _SetFilters, this function does not reset the current list of filters
891 available.
892
893 Args:
894 filters: A string of comma-separated filters (eg "whitespace/indent").
895 Each filter should start with + or -; else we die.
896 """
897 _cpplint_state.AddFilters(filters)
898
899def _BackupFilters():
900 """ Saves the current filter list to backup storage."""
901 _cpplint_state.BackupFilters()
902
903def _RestoreFilters():
904 """ Restores filters previously backed up."""
905 _cpplint_state.RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000906
907class _FunctionState(object):
908 """Tracks current function name and the number of lines in its body."""
909
910 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
911 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
912
913 def __init__(self):
914 self.in_a_function = False
915 self.lines_in_function = 0
916 self.current_function = ''
917
918 def Begin(self, function_name):
919 """Start analyzing function body.
920
921 Args:
922 function_name: The name of the function being tracked.
923 """
924 self.in_a_function = True
925 self.lines_in_function = 0
926 self.current_function = function_name
927
928 def Count(self):
929 """Count line in current function body."""
930 if self.in_a_function:
931 self.lines_in_function += 1
932
933 def Check(self, error, filename, linenum):
934 """Report if too many lines in function body.
935
936 Args:
937 error: The function to call with any errors found.
938 filename: The name of the current file.
939 linenum: The number of the line to check.
940 """
941 if Match(r'T(EST|est)', self.current_function):
942 base_trigger = self._TEST_TRIGGER
943 else:
944 base_trigger = self._NORMAL_TRIGGER
945 trigger = base_trigger * 2**_VerboseLevel()
946
947 if self.lines_in_function > trigger:
948 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
949 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
950 if error_level > 5:
951 error_level = 5
952 error(filename, linenum, 'readability/fn_size', error_level,
953 'Small and focused functions are preferred:'
954 ' %s has %d non-comment lines'
955 ' (error triggered by exceeding %d lines).' % (
956 self.current_function, self.lines_in_function, trigger))
957
958 def End(self):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000959 """Stop analyzing function body."""
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000960 self.in_a_function = False
961
962
963class _IncludeError(Exception):
964 """Indicates a problem with the include order in a file."""
965 pass
966
967
avakulenko@google.com59146752014-08-11 20:20:55 +0000968class FileInfo(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000969 """Provides utility functions for filenames.
970
971 FileInfo provides easy access to the components of a file's path
972 relative to the project root.
973 """
974
975 def __init__(self, filename):
976 self._filename = filename
977
978 def FullName(self):
979 """Make Windows paths like Unix."""
980 return os.path.abspath(self._filename).replace('\\', '/')
981
982 def RepositoryName(self):
983 """FullName after removing the local path to the repository.
984
985 If we have a real absolute path name here we can try to do something smart:
986 detecting the root of the checkout and truncating /path/to/checkout from
987 the name so that we get header guards that don't include things like
988 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
989 people on different computers who have checked the source out to different
990 locations won't see bogus errors.
991 """
992 fullname = self.FullName()
993
994 if os.path.exists(fullname):
995 project_dir = os.path.dirname(fullname)
996
997 if os.path.exists(os.path.join(project_dir, ".svn")):
998 # If there's a .svn file in the current directory, we recursively look
999 # up the directory tree for the top of the SVN checkout
1000 root_dir = project_dir
1001 one_up_dir = os.path.dirname(root_dir)
1002 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1003 root_dir = os.path.dirname(root_dir)
1004 one_up_dir = os.path.dirname(one_up_dir)
1005
1006 prefix = os.path.commonprefix([root_dir, project_dir])
1007 return fullname[len(prefix) + 1:]
1008
erg@chromium.org7956a872011-11-30 01:44:03 +00001009 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1010 # searching up from the current path.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001011 root_dir = os.path.dirname(fullname)
1012 while (root_dir != os.path.dirname(root_dir) and
erg@google.com35589e62010-11-17 18:58:16 +00001013 not os.path.exists(os.path.join(root_dir, ".git")) and
erg@chromium.org7956a872011-11-30 01:44:03 +00001014 not os.path.exists(os.path.join(root_dir, ".hg")) and
1015 not os.path.exists(os.path.join(root_dir, ".svn"))):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001016 root_dir = os.path.dirname(root_dir)
erg@google.com35589e62010-11-17 18:58:16 +00001017
1018 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@chromium.org7956a872011-11-30 01:44:03 +00001019 os.path.exists(os.path.join(root_dir, ".hg")) or
1020 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com35589e62010-11-17 18:58:16 +00001021 prefix = os.path.commonprefix([root_dir, project_dir])
1022 return fullname[len(prefix) + 1:]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001023
1024 # Don't know what to do; header guard warnings may be wrong...
1025 return fullname
1026
1027 def Split(self):
1028 """Splits the file into the directory, basename, and extension.
1029
1030 For 'chrome/browser/browser.cc', Split() would
1031 return ('chrome/browser', 'browser', '.cc')
1032
1033 Returns:
1034 A tuple of (directory, basename, extension).
1035 """
1036
1037 googlename = self.RepositoryName()
1038 project, rest = os.path.split(googlename)
1039 return (project,) + os.path.splitext(rest)
1040
1041 def BaseName(self):
1042 """File base name - text after the final slash, before the final period."""
1043 return self.Split()[1]
1044
1045 def Extension(self):
1046 """File extension - text following the final period."""
1047 return self.Split()[2]
1048
1049 def NoExtension(self):
1050 """File has no source file extension."""
1051 return '/'.join(self.Split()[0:2])
1052
1053 def IsSource(self):
1054 """File has a source file extension."""
1055 return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx')
1056
1057
erg@google.com35589e62010-11-17 18:58:16 +00001058def _ShouldPrintError(category, confidence, linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001059 """If confidence >= verbose, category passes filter and is not suppressed."""
erg@google.com35589e62010-11-17 18:58:16 +00001060
1061 # There are three ways we might decide not to print an error message:
1062 # a "NOLINT(category)" comment appears in the source,
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001063 # the verbosity level isn't high enough, or the filters filter it out.
erg@google.com35589e62010-11-17 18:58:16 +00001064 if IsErrorSuppressedByNolint(category, linenum):
1065 return False
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001066
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001067 if confidence < _cpplint_state.verbose_level:
1068 return False
1069
1070 is_filtered = False
1071 for one_filter in _Filters():
1072 if one_filter.startswith('-'):
1073 if category.startswith(one_filter[1:]):
1074 is_filtered = True
1075 elif one_filter.startswith('+'):
1076 if category.startswith(one_filter[1:]):
1077 is_filtered = False
1078 else:
1079 assert False # should have been checked for in SetFilter.
1080 if is_filtered:
1081 return False
1082
1083 return True
1084
1085
1086def Error(filename, linenum, category, confidence, message):
1087 """Logs the fact we've found a lint error.
1088
1089 We log where the error was found, and also our confidence in the error,
1090 that is, how certain we are this is a legitimate style regression, and
1091 not a misidentification or a use that's sometimes justified.
1092
erg@google.com35589e62010-11-17 18:58:16 +00001093 False positives can be suppressed by the use of
1094 "cpplint(category)" comments on the offending line. These are
1095 parsed into _error_suppressions.
1096
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001097 Args:
1098 filename: The name of the file containing the error.
1099 linenum: The number of the line containing the error.
1100 category: A string used to describe the "category" this bug
1101 falls under: "whitespace", say, or "runtime". Categories
1102 may have a hierarchy separated by slashes: "whitespace/indent".
1103 confidence: A number from 1-5 representing a confidence score for
1104 the error, with 5 meaning that we are certain of the problem,
1105 and 1 meaning that it could be a legitimate construct.
1106 message: The error message.
1107 """
erg@google.com35589e62010-11-17 18:58:16 +00001108 if _ShouldPrintError(category, confidence, linenum):
erg@google.com26970fa2009-11-17 18:07:32 +00001109 _cpplint_state.IncrementErrorCount(category)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001110 if _cpplint_state.output_format == 'vs7':
1111 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
1112 filename, linenum, message, category, confidence))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001113 elif _cpplint_state.output_format == 'eclipse':
1114 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1115 filename, linenum, message, category, confidence))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001116 else:
1117 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1118 filename, linenum, message, category, confidence))
1119
1120
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001121# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001122_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1123 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001124# Match a single C style comment on the same line.
1125_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1126# Matches multi-line C style comments.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001127# This RE is a little bit more complicated than one might expect, because we
1128# have to take care of space removals tools so we can handle comments inside
1129# statements better.
1130# The current rule is: We only clear spaces from both sides when we're at the
1131# end of the line. Otherwise, we try to remove spaces from the right side,
1132# if this doesn't work we try on left side but only if there's a non-character
1133# on the right.
1134_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001135 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1136 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1137 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1138 _RE_PATTERN_C_COMMENTS + r')')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001139
1140
1141def IsCppString(line):
1142 """Does line terminate so, that the next symbol is in string constant.
1143
1144 This function does not consider single-line nor multi-line comments.
1145
1146 Args:
1147 line: is a partial line of code starting from the 0..n.
1148
1149 Returns:
1150 True, if next character appended to 'line' is inside a
1151 string constant.
1152 """
1153
1154 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1155 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1156
1157
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001158def CleanseRawStrings(raw_lines):
1159 """Removes C++11 raw strings from lines.
1160
1161 Before:
1162 static const char kData[] = R"(
1163 multi-line string
1164 )";
1165
1166 After:
1167 static const char kData[] = ""
1168 (replaced by blank line)
1169 "";
1170
1171 Args:
1172 raw_lines: list of raw lines.
1173
1174 Returns:
1175 list of lines with C++11 raw strings replaced by empty strings.
1176 """
1177
1178 delimiter = None
1179 lines_without_raw_strings = []
1180 for line in raw_lines:
1181 if delimiter:
1182 # Inside a raw string, look for the end
1183 end = line.find(delimiter)
1184 if end >= 0:
1185 # Found the end of the string, match leading space for this
1186 # line and resume copying the original lines, and also insert
1187 # a "" on the last line.
1188 leading_space = Match(r'^(\s*)\S', line)
1189 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1190 delimiter = None
1191 else:
1192 # Haven't found the end yet, append a blank line.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001193 line = '""'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001194
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001195 # Look for beginning of a raw string, and replace them with
1196 # empty strings. This is done in a loop to handle multiple raw
1197 # strings on the same line.
1198 while delimiter is None:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001199 # Look for beginning of a raw string.
1200 # See 2.14.15 [lex.string] for syntax.
1201 matched = Match(r'^(.*)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1202 if matched:
1203 delimiter = ')' + matched.group(2) + '"'
1204
1205 end = matched.group(3).find(delimiter)
1206 if end >= 0:
1207 # Raw string ended on same line
1208 line = (matched.group(1) + '""' +
1209 matched.group(3)[end + len(delimiter):])
1210 delimiter = None
1211 else:
1212 # Start of a multi-line raw string
1213 line = matched.group(1) + '""'
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001214 else:
1215 break
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001216
1217 lines_without_raw_strings.append(line)
1218
1219 # TODO(unknown): if delimiter is not None here, we might want to
1220 # emit a warning for unterminated string.
1221 return lines_without_raw_strings
1222
1223
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001224def FindNextMultiLineCommentStart(lines, lineix):
1225 """Find the beginning marker for a multiline comment."""
1226 while lineix < len(lines):
1227 if lines[lineix].strip().startswith('/*'):
1228 # Only return this marker if the comment goes beyond this line
1229 if lines[lineix].strip().find('*/', 2) < 0:
1230 return lineix
1231 lineix += 1
1232 return len(lines)
1233
1234
1235def FindNextMultiLineCommentEnd(lines, lineix):
1236 """We are inside a comment, find the end marker."""
1237 while lineix < len(lines):
1238 if lines[lineix].strip().endswith('*/'):
1239 return lineix
1240 lineix += 1
1241 return len(lines)
1242
1243
1244def RemoveMultiLineCommentsFromRange(lines, begin, end):
1245 """Clears a range of lines for multi-line comments."""
1246 # Having // dummy comments makes the lines non-empty, so we will not get
1247 # unnecessary blank line warnings later in the code.
1248 for i in range(begin, end):
1249 lines[i] = '// dummy'
1250
1251
1252def RemoveMultiLineComments(filename, lines, error):
1253 """Removes multiline (c-style) comments from lines."""
1254 lineix = 0
1255 while lineix < len(lines):
1256 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1257 if lineix_begin >= len(lines):
1258 return
1259 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1260 if lineix_end >= len(lines):
1261 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1262 'Could not find end of multi-line comment')
1263 return
1264 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1265 lineix = lineix_end + 1
1266
1267
1268def CleanseComments(line):
1269 """Removes //-comments and single-line C-style /* */ comments.
1270
1271 Args:
1272 line: A line of C++ source.
1273
1274 Returns:
1275 The line with single-line comments removed.
1276 """
1277 commentpos = line.find('//')
1278 if commentpos != -1 and not IsCppString(line[:commentpos]):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001279 line = line[:commentpos].rstrip()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001280 # get rid of /* ... */
1281 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1282
1283
erg@google.com6317a9c2009-06-25 00:28:19 +00001284class CleansedLines(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001285 """Holds 3 copies of all lines with different preprocessing applied to them.
1286
1287 1) elided member contains lines without strings and comments,
1288 2) lines member contains lines without comments, and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001289 3) raw_lines member contains all the lines without processing.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001290 All these three members are of <type 'list'>, and of the same length.
1291 """
1292
1293 def __init__(self, lines):
1294 self.elided = []
1295 self.lines = []
1296 self.raw_lines = lines
1297 self.num_lines = len(lines)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001298 self.lines_without_raw_strings = CleanseRawStrings(lines)
1299 for linenum in range(len(self.lines_without_raw_strings)):
1300 self.lines.append(CleanseComments(
1301 self.lines_without_raw_strings[linenum]))
1302 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001303 self.elided.append(CleanseComments(elided))
1304
1305 def NumLines(self):
1306 """Returns the number of lines represented."""
1307 return self.num_lines
1308
1309 @staticmethod
1310 def _CollapseStrings(elided):
1311 """Collapses strings and chars on a line to simple "" or '' blocks.
1312
1313 We nix strings first so we're not fooled by text like '"http://"'
1314
1315 Args:
1316 elided: The line being processed.
1317
1318 Returns:
1319 The line with collapsed strings.
1320 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001321 if _RE_PATTERN_INCLUDE.match(elided):
1322 return elided
1323
1324 # Remove escaped characters first to make quote/single quote collapsing
1325 # basic. Things that look like escaped characters shouldn't occur
1326 # outside of strings and chars.
1327 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1328
1329 # Replace quoted strings and digit separators. Both single quotes
1330 # and double quotes are processed in the same loop, otherwise
1331 # nested quotes wouldn't work.
1332 collapsed = ''
1333 while True:
1334 # Find the first quote character
1335 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1336 if not match:
1337 collapsed += elided
1338 break
1339 head, quote, tail = match.groups()
1340
1341 if quote == '"':
1342 # Collapse double quoted strings
1343 second_quote = tail.find('"')
1344 if second_quote >= 0:
1345 collapsed += head + '""'
1346 elided = tail[second_quote + 1:]
1347 else:
1348 # Unmatched double quote, don't bother processing the rest
1349 # of the line since this is probably a multiline string.
1350 collapsed += elided
1351 break
1352 else:
1353 # Found single quote, check nearby text to eliminate digit separators.
1354 #
1355 # There is no special handling for floating point here, because
1356 # the integer/fractional/exponent parts would all be parsed
1357 # correctly as long as there are digits on both sides of the
1358 # separator. So we are fine as long as we don't see something
1359 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1360 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1361 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1362 collapsed += head + match_literal.group(1).replace("'", '')
1363 elided = match_literal.group(2)
1364 else:
1365 second_quote = tail.find('\'')
1366 if second_quote >= 0:
1367 collapsed += head + "''"
1368 elided = tail[second_quote + 1:]
1369 else:
1370 # Unmatched single quote
1371 collapsed += elided
1372 break
1373
1374 return collapsed
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001375
1376
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001377def FindEndOfExpressionInLine(line, startpos, stack):
1378 """Find the position just after the end of current parenthesized expression.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001379
1380 Args:
1381 line: a CleansedLines line.
1382 startpos: start searching at this position.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001383 stack: nesting stack at startpos.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001384
1385 Returns:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001386 On finding matching end: (index just after matching end, None)
1387 On finding an unclosed expression: (-1, None)
1388 Otherwise: (-1, new stack at end of this line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001389 """
1390 for i in xrange(startpos, len(line)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001391 char = line[i]
1392 if char in '([{':
1393 # Found start of parenthesized expression, push to expression stack
1394 stack.append(char)
1395 elif char == '<':
1396 # Found potential start of template argument list
1397 if i > 0 and line[i - 1] == '<':
1398 # Left shift operator
1399 if stack and stack[-1] == '<':
1400 stack.pop()
1401 if not stack:
1402 return (-1, None)
1403 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1404 # operator<, don't add to stack
1405 continue
1406 else:
1407 # Tentative start of template argument list
1408 stack.append('<')
1409 elif char in ')]}':
1410 # Found end of parenthesized expression.
1411 #
1412 # If we are currently expecting a matching '>', the pending '<'
1413 # must have been an operator. Remove them from expression stack.
1414 while stack and stack[-1] == '<':
1415 stack.pop()
1416 if not stack:
1417 return (-1, None)
1418 if ((stack[-1] == '(' and char == ')') or
1419 (stack[-1] == '[' and char == ']') or
1420 (stack[-1] == '{' and char == '}')):
1421 stack.pop()
1422 if not stack:
1423 return (i + 1, None)
1424 else:
1425 # Mismatched parentheses
1426 return (-1, None)
1427 elif char == '>':
1428 # Found potential end of template argument list.
1429
1430 # Ignore "->" and operator functions
1431 if (i > 0 and
1432 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1433 continue
1434
1435 # Pop the stack if there is a matching '<'. Otherwise, ignore
1436 # this '>' since it must be an operator.
1437 if stack:
1438 if stack[-1] == '<':
1439 stack.pop()
1440 if not stack:
1441 return (i + 1, None)
1442 elif char == ';':
1443 # Found something that look like end of statements. If we are currently
1444 # expecting a '>', the matching '<' must have been an operator, since
1445 # template argument list should not contain statements.
1446 while stack and stack[-1] == '<':
1447 stack.pop()
1448 if not stack:
1449 return (-1, None)
1450
1451 # Did not find end of expression or unbalanced parentheses on this line
1452 return (-1, stack)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001453
1454
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001455def CloseExpression(clean_lines, linenum, pos):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001456 """If input points to ( or { or [ or <, finds the position that closes it.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001457
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001458 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001459 linenum/pos that correspond to the closing of the expression.
1460
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001461 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1462 Ideally we would want to index all opening and closing parentheses once
1463 and have CloseExpression be just a simple lookup, but due to preprocessor
1464 tricks, this is not so easy.
1465
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001466 Args:
1467 clean_lines: A CleansedLines instance containing the file.
1468 linenum: The number of the line to check.
1469 pos: A position on the line.
1470
1471 Returns:
1472 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1473 (line, len(lines), -1) if we never find a close. Note we ignore
1474 strings and comments when matching; and the line we return is the
1475 'cleansed' line at linenum.
1476 """
1477
1478 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001479 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001480 return (line, clean_lines.NumLines(), -1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001481
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001482 # Check first line
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001483 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001484 if end_pos > -1:
1485 return (line, linenum, end_pos)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001486
1487 # Continue scanning forward
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001488 while stack and linenum < clean_lines.NumLines() - 1:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001489 linenum += 1
1490 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001491 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001492 if end_pos > -1:
1493 return (line, linenum, end_pos)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001494
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001495 # Did not find end of expression before end of file, give up
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001496 return (line, clean_lines.NumLines(), -1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001497
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001498
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001499def FindStartOfExpressionInLine(line, endpos, stack):
1500 """Find position at the matching start of current expression.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001501
1502 This is almost the reverse of FindEndOfExpressionInLine, but note
1503 that the input position and returned position differs by 1.
1504
1505 Args:
1506 line: a CleansedLines line.
1507 endpos: start searching at this position.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001508 stack: nesting stack at endpos.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001509
1510 Returns:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001511 On finding matching start: (index at matching start, None)
1512 On finding an unclosed expression: (-1, None)
1513 Otherwise: (-1, new stack at beginning of this line)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001514 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001515 i = endpos
1516 while i >= 0:
1517 char = line[i]
1518 if char in ')]}':
1519 # Found end of expression, push to expression stack
1520 stack.append(char)
1521 elif char == '>':
1522 # Found potential end of template argument list.
1523 #
1524 # Ignore it if it's a "->" or ">=" or "operator>"
1525 if (i > 0 and
1526 (line[i - 1] == '-' or
1527 Match(r'\s>=\s', line[i - 1:]) or
1528 Search(r'\boperator\s*$', line[0:i]))):
1529 i -= 1
1530 else:
1531 stack.append('>')
1532 elif char == '<':
1533 # Found potential start of template argument list
1534 if i > 0 and line[i - 1] == '<':
1535 # Left shift operator
1536 i -= 1
1537 else:
1538 # If there is a matching '>', we can pop the expression stack.
1539 # Otherwise, ignore this '<' since it must be an operator.
1540 if stack and stack[-1] == '>':
1541 stack.pop()
1542 if not stack:
1543 return (i, None)
1544 elif char in '([{':
1545 # Found start of expression.
1546 #
1547 # If there are any unmatched '>' on the stack, they must be
1548 # operators. Remove those.
1549 while stack and stack[-1] == '>':
1550 stack.pop()
1551 if not stack:
1552 return (-1, None)
1553 if ((char == '(' and stack[-1] == ')') or
1554 (char == '[' and stack[-1] == ']') or
1555 (char == '{' and stack[-1] == '}')):
1556 stack.pop()
1557 if not stack:
1558 return (i, None)
1559 else:
1560 # Mismatched parentheses
1561 return (-1, None)
1562 elif char == ';':
1563 # Found something that look like end of statements. If we are currently
1564 # expecting a '<', the matching '>' must have been an operator, since
1565 # template argument list should not contain statements.
1566 while stack and stack[-1] == '>':
1567 stack.pop()
1568 if not stack:
1569 return (-1, None)
1570
1571 i -= 1
1572
1573 return (-1, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001574
1575
1576def ReverseCloseExpression(clean_lines, linenum, pos):
1577 """If input points to ) or } or ] or >, finds the position that opens it.
1578
1579 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1580 linenum/pos that correspond to the opening of the expression.
1581
1582 Args:
1583 clean_lines: A CleansedLines instance containing the file.
1584 linenum: The number of the line to check.
1585 pos: A position on the line.
1586
1587 Returns:
1588 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1589 (line, 0, -1) if we never find the matching opening brace. Note
1590 we ignore strings and comments when matching; and the line we
1591 return is the 'cleansed' line at linenum.
1592 """
1593 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001594 if line[pos] not in ')}]>':
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001595 return (line, 0, -1)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001596
1597 # Check last line
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001598 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001599 if start_pos > -1:
1600 return (line, linenum, start_pos)
1601
1602 # Continue scanning backward
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001603 while stack and linenum > 0:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001604 linenum -= 1
1605 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001606 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001607 if start_pos > -1:
1608 return (line, linenum, start_pos)
1609
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001610 # Did not find start of expression before beginning of file, give up
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001611 return (line, 0, -1)
1612
1613
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001614def CheckForCopyright(filename, lines, error):
1615 """Logs an error if no Copyright message appears at the top of the file."""
1616
1617 # We'll say it should occur by line 10. Don't forget there's a
1618 # dummy line at the front.
1619 for line in xrange(1, min(len(lines), 11)):
1620 if re.search(r'Copyright', lines[line], re.I): break
1621 else: # means no copyright line was found
1622 error(filename, 0, 'legal/copyright', 5,
1623 'No copyright message found. '
1624 'You should have a line: "Copyright [year] <Copyright Owner>"')
1625
1626
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001627def GetIndentLevel(line):
1628 """Return the number of leading spaces in line.
1629
1630 Args:
1631 line: A string to check.
1632
1633 Returns:
1634 An integer count of leading spaces, possibly zero.
1635 """
1636 indent = Match(r'^( *)\S', line)
1637 if indent:
1638 return len(indent.group(1))
1639 else:
1640 return 0
1641
1642
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001643def GetHeaderGuardCPPVariable(filename):
1644 """Returns the CPP variable that should be used as a header guard.
1645
1646 Args:
1647 filename: The name of a C++ header file.
1648
1649 Returns:
1650 The CPP variable that should be used as a header guard in the
1651 named file.
1652
1653 """
1654
erg@google.com35589e62010-11-17 18:58:16 +00001655 # Restores original filename in case that cpplint is invoked from Emacs's
1656 # flymake.
1657 filename = re.sub(r'_flymake\.h$', '.h', filename)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001658 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
erg@google.com35589e62010-11-17 18:58:16 +00001659
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001660 fileinfo = FileInfo(filename)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001661 file_path_from_root = fileinfo.RepositoryName()
1662 if _root:
1663 file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root)
1664 return re.sub(r'[-./\s]', '_', file_path_from_root).upper() + '_'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001665
1666
1667def CheckForHeaderGuard(filename, lines, error):
1668 """Checks that the file contains a header guard.
1669
erg@google.com6317a9c2009-06-25 00:28:19 +00001670 Logs an error if no #ifndef header guard is present. For other
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001671 headers, checks that the full pathname is used.
1672
1673 Args:
1674 filename: The name of the C++ header file.
1675 lines: An array of strings, each representing a line of the file.
1676 error: The function to call with any errors found.
1677 """
1678
avakulenko@google.com59146752014-08-11 20:20:55 +00001679 # Don't check for header guards if there are error suppression
1680 # comments somewhere in this file.
1681 #
1682 # Because this is silencing a warning for a nonexistent line, we
1683 # only support the very specific NOLINT(build/header_guard) syntax,
1684 # and not the general NOLINT or NOLINT(*) syntax.
1685 for i in lines:
1686 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1687 return
1688
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001689 cppvar = GetHeaderGuardCPPVariable(filename)
1690
1691 ifndef = None
1692 ifndef_linenum = 0
1693 define = None
1694 endif = None
1695 endif_linenum = 0
1696 for linenum, line in enumerate(lines):
1697 linesplit = line.split()
1698 if len(linesplit) >= 2:
1699 # find the first occurrence of #ifndef and #define, save arg
1700 if not ifndef and linesplit[0] == '#ifndef':
1701 # set ifndef to the header guard presented on the #ifndef line.
1702 ifndef = linesplit[1]
1703 ifndef_linenum = linenum
1704 if not define and linesplit[0] == '#define':
1705 define = linesplit[1]
1706 # find the last occurrence of #endif, save entire line
1707 if line.startswith('#endif'):
1708 endif = line
1709 endif_linenum = linenum
1710
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001711 if not ifndef:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001712 error(filename, 0, 'build/header_guard', 5,
1713 'No #ifndef header guard found, suggested CPP variable is: %s' %
1714 cppvar)
1715 return
1716
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001717 if not define:
1718 error(filename, 0, 'build/header_guard', 5,
1719 'No #define header guard found, suggested CPP variable is: %s' %
1720 cppvar)
1721 return
1722
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001723 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1724 # for backward compatibility.
erg@google.com35589e62010-11-17 18:58:16 +00001725 if ifndef != cppvar:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001726 error_level = 0
1727 if ifndef != cppvar + '_':
1728 error_level = 5
1729
erg@google.com35589e62010-11-17 18:58:16 +00001730 ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum,
1731 error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001732 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1733 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1734
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001735 if define != ifndef:
1736 error(filename, 0, 'build/header_guard', 5,
1737 '#ifndef and #define don\'t match, suggested CPP variable is: %s' %
1738 cppvar)
1739 return
1740
erg@google.com35589e62010-11-17 18:58:16 +00001741 if endif != ('#endif // %s' % cppvar):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001742 error_level = 0
1743 if endif != ('#endif // %s' % (cppvar + '_')):
1744 error_level = 5
1745
erg@google.com35589e62010-11-17 18:58:16 +00001746 ParseNolintSuppressions(filename, lines[endif_linenum], endif_linenum,
1747 error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001748 error(filename, endif_linenum, 'build/header_guard', error_level,
1749 '#endif line should be "#endif // %s"' % cppvar)
1750
1751
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001752def CheckForBadCharacters(filename, lines, error):
1753 """Logs an error for each line containing bad characters.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001754
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001755 Two kinds of bad characters:
1756
1757 1. Unicode replacement characters: These indicate that either the file
1758 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1759 it shouldn't). Note that it's possible for this to throw off line
1760 numbering if the invalid UTF-8 occurred adjacent to a newline.
1761
1762 2. NUL bytes. These are problematic for some tools.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001763
1764 Args:
1765 filename: The name of the current file.
1766 lines: An array of strings, each representing a line of the file.
1767 error: The function to call with any errors found.
1768 """
1769 for linenum, line in enumerate(lines):
1770 if u'\ufffd' in line:
1771 error(filename, linenum, 'readability/utf8', 5,
1772 'Line contains invalid UTF-8 (or Unicode replacement character).')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001773 if '\0' in line:
1774 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001775
1776
1777def CheckForNewlineAtEOF(filename, lines, error):
1778 """Logs an error if there is no newline char at the end of the file.
1779
1780 Args:
1781 filename: The name of the current file.
1782 lines: An array of strings, each representing a line of the file.
1783 error: The function to call with any errors found.
1784 """
1785
1786 # The array lines() was created by adding two newlines to the
1787 # original file (go figure), then splitting on \n.
1788 # To verify that the file ends in \n, we just have to make sure the
1789 # last-but-two element of lines() exists and is empty.
1790 if len(lines) < 3 or lines[-2]:
1791 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1792 'Could not find a newline character at the end of the file.')
1793
1794
1795def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1796 """Logs an error if we see /* ... */ or "..." that extend past one line.
1797
1798 /* ... */ comments are legit inside macros, for one line.
1799 Otherwise, we prefer // comments, so it's ok to warn about the
1800 other. Likewise, it's ok for strings to extend across multiple
1801 lines, as long as a line continuation character (backslash)
1802 terminates each line. Although not currently prohibited by the C++
1803 style guide, it's ugly and unnecessary. We don't do well with either
1804 in this lint program, so we warn about both.
1805
1806 Args:
1807 filename: The name of the current file.
1808 clean_lines: A CleansedLines instance containing the file.
1809 linenum: The number of the line to check.
1810 error: The function to call with any errors found.
1811 """
1812 line = clean_lines.elided[linenum]
1813
1814 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1815 # second (escaped) slash may trigger later \" detection erroneously.
1816 line = line.replace('\\\\', '')
1817
1818 if line.count('/*') > line.count('*/'):
1819 error(filename, linenum, 'readability/multiline_comment', 5,
1820 'Complex multi-line /*...*/-style comment found. '
1821 'Lint may give bogus warnings. '
1822 'Consider replacing these with //-style comments, '
1823 'with #if 0...#endif, '
1824 'or with more clearly structured multi-line comments.')
1825
1826 if (line.count('"') - line.count('\\"')) % 2:
1827 error(filename, linenum, 'readability/multiline_string', 5,
1828 'Multi-line string ("...") found. This lint script doesn\'t '
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001829 'do well with such strings, and may give bogus warnings. '
1830 'Use C++11 raw strings or concatenation instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001831
1832
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001833# (non-threadsafe name, thread-safe alternative, validation pattern)
1834#
1835# The validation pattern is used to eliminate false positives such as:
1836# _rand(); // false positive due to substring match.
1837# ->rand(); // some member function rand().
1838# ACMRandom rand(seed); // some variable named rand.
1839# ISAACRandom rand(); // another variable named rand.
1840#
1841# Basically we require the return value of these functions to be used
1842# in some expression context on the same line by matching on some
1843# operator before the function name. This eliminates constructors and
1844# member function calls.
1845_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
1846_THREADING_LIST = (
1847 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
1848 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
1849 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
1850 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
1851 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
1852 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
1853 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
1854 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
1855 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
1856 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
1857 ('strtok(', 'strtok_r(',
1858 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
1859 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001860 )
1861
1862
1863def CheckPosixThreading(filename, clean_lines, linenum, error):
1864 """Checks for calls to thread-unsafe functions.
1865
1866 Much code has been originally written without consideration of
1867 multi-threading. Also, engineers are relying on their old experience;
1868 they have learned posix before threading extensions were added. These
1869 tests guide the engineers to use thread-safe functions (when using
1870 posix directly).
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]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001879 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
1880 # Additional pattern matching check to confirm that this is the
1881 # function we are looking for
1882 if Search(pattern, line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001883 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001884 'Consider using ' + multithread_safe_func +
1885 '...) instead of ' + single_thread_func +
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001886 '...) for improved thread safety.')
1887
1888
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001889def CheckVlogArguments(filename, clean_lines, linenum, error):
1890 """Checks that VLOG() is only used for defining a logging level.
1891
1892 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
1893 VLOG(FATAL) are not.
1894
1895 Args:
1896 filename: The name of the current file.
1897 clean_lines: A CleansedLines instance containing the file.
1898 linenum: The number of the line to check.
1899 error: The function to call with any errors found.
1900 """
1901 line = clean_lines.elided[linenum]
1902 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
1903 error(filename, linenum, 'runtime/vlog', 5,
1904 'VLOG() should be used with numeric verbosity level. '
1905 'Use LOG() if you want symbolic severity levels.')
1906
erg@google.com26970fa2009-11-17 18:07:32 +00001907# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com6317a9c2009-06-25 00:28:19 +00001908# incrementing a value.
erg@google.com26970fa2009-11-17 18:07:32 +00001909_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com6317a9c2009-06-25 00:28:19 +00001910 r'^\s*\*\w+(\+\+|--);')
1911
1912
1913def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.com26970fa2009-11-17 18:07:32 +00001914 """Checks for invalid increment *count++.
erg@google.com6317a9c2009-06-25 00:28:19 +00001915
1916 For example following function:
1917 void increment_counter(int* count) {
1918 *count++;
1919 }
1920 is invalid, because it effectively does count++, moving pointer, and should
1921 be replaced with ++*count, (*count)++ or *count += 1.
1922
1923 Args:
1924 filename: The name of the current file.
1925 clean_lines: A CleansedLines instance containing the file.
1926 linenum: The number of the line to check.
1927 error: The function to call with any errors found.
1928 """
1929 line = clean_lines.elided[linenum]
erg@google.com26970fa2009-11-17 18:07:32 +00001930 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com6317a9c2009-06-25 00:28:19 +00001931 error(filename, linenum, 'runtime/invalid_increment', 5,
1932 'Changing pointer instead of value (or unused value of operator*).')
1933
1934
avakulenko@google.com59146752014-08-11 20:20:55 +00001935def IsMacroDefinition(clean_lines, linenum):
1936 if Search(r'^#define', clean_lines[linenum]):
1937 return True
1938
1939 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
1940 return True
1941
1942 return False
1943
1944
1945def IsForwardClassDeclaration(clean_lines, linenum):
1946 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
1947
1948
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001949class _BlockInfo(object):
1950 """Stores information about a generic block of code."""
1951
1952 def __init__(self, seen_open_brace):
1953 self.seen_open_brace = seen_open_brace
1954 self.open_parentheses = 0
1955 self.inline_asm = _NO_ASM
avakulenko@google.com59146752014-08-11 20:20:55 +00001956 self.check_namespace_indentation = False
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001957
1958 def CheckBegin(self, filename, clean_lines, linenum, error):
1959 """Run checks that applies to text up to the opening brace.
1960
1961 This is mostly for checking the text after the class identifier
1962 and the "{", usually where the base class is specified. For other
1963 blocks, there isn't much to check, so we always pass.
1964
1965 Args:
1966 filename: The name of the current file.
1967 clean_lines: A CleansedLines instance containing the file.
1968 linenum: The number of the line to check.
1969 error: The function to call with any errors found.
1970 """
1971 pass
1972
1973 def CheckEnd(self, filename, clean_lines, linenum, error):
1974 """Run checks that applies to text after the closing brace.
1975
1976 This is mostly used for checking end of namespace comments.
1977
1978 Args:
1979 filename: The name of the current file.
1980 clean_lines: A CleansedLines instance containing the file.
1981 linenum: The number of the line to check.
1982 error: The function to call with any errors found.
1983 """
1984 pass
1985
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001986 def IsBlockInfo(self):
1987 """Returns true if this block is a _BlockInfo.
1988
1989 This is convenient for verifying that an object is an instance of
1990 a _BlockInfo, but not an instance of any of the derived classes.
1991
1992 Returns:
1993 True for this class, False for derived classes.
1994 """
1995 return self.__class__ == _BlockInfo
1996
1997
1998class _ExternCInfo(_BlockInfo):
1999 """Stores information about an 'extern "C"' block."""
2000
2001 def __init__(self):
2002 _BlockInfo.__init__(self, True)
2003
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002004
2005class _ClassInfo(_BlockInfo):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002006 """Stores information about a class."""
2007
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002008 def __init__(self, name, class_or_struct, clean_lines, linenum):
2009 _BlockInfo.__init__(self, False)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002010 self.name = name
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002011 self.starting_linenum = linenum
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002012 self.is_derived = False
avakulenko@google.com59146752014-08-11 20:20:55 +00002013 self.check_namespace_indentation = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002014 if class_or_struct == 'struct':
2015 self.access = 'public'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002016 self.is_struct = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002017 else:
2018 self.access = 'private'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002019 self.is_struct = False
2020
2021 # Remember initial indentation level for this class. Using raw_lines here
2022 # instead of elided to account for leading comments.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002023 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002024
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002025 # Try to find the end of the class. This will be confused by things like:
2026 # class A {
2027 # } *x = { ...
2028 #
2029 # But it's still good enough for CheckSectionSpacing.
2030 self.last_line = 0
2031 depth = 0
2032 for i in range(linenum, clean_lines.NumLines()):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002033 line = clean_lines.elided[i]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002034 depth += line.count('{') - line.count('}')
2035 if not depth:
2036 self.last_line = i
2037 break
2038
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002039 def CheckBegin(self, filename, clean_lines, linenum, error):
2040 # Look for a bare ':'
2041 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2042 self.is_derived = True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002043
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002044 def CheckEnd(self, filename, clean_lines, linenum, error):
2045 # Check that closing brace is aligned with beginning of the class.
2046 # Only do this if the closing brace is indented by only whitespaces.
2047 # This means we will not check single-line class definitions.
2048 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2049 if indent and len(indent.group(1)) != self.class_indent:
2050 if self.is_struct:
2051 parent = 'struct ' + self.name
2052 else:
2053 parent = 'class ' + self.name
2054 error(filename, linenum, 'whitespace/indent', 3,
2055 'Closing brace should be aligned with beginning of %s' % parent)
2056
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002057
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002058class _NamespaceInfo(_BlockInfo):
2059 """Stores information about a namespace."""
2060
2061 def __init__(self, name, linenum):
2062 _BlockInfo.__init__(self, False)
2063 self.name = name or ''
2064 self.starting_linenum = linenum
avakulenko@google.com59146752014-08-11 20:20:55 +00002065 self.check_namespace_indentation = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002066
2067 def CheckEnd(self, filename, clean_lines, linenum, error):
2068 """Check end of namespace comments."""
2069 line = clean_lines.raw_lines[linenum]
2070
2071 # Check how many lines is enclosed in this namespace. Don't issue
2072 # warning for missing namespace comments if there aren't enough
2073 # lines. However, do apply checks if there is already an end of
2074 # namespace comment and it's incorrect.
2075 #
2076 # TODO(unknown): We always want to check end of namespace comments
2077 # if a namespace is large, but sometimes we also want to apply the
2078 # check if a short namespace contained nontrivial things (something
2079 # other than forward declarations). There is currently no logic on
2080 # deciding what these nontrivial things are, so this check is
2081 # triggered by namespace size only, which works most of the time.
2082 if (linenum - self.starting_linenum < 10
2083 and not Match(r'};*\s*(//|/\*).*\bnamespace\b', line)):
2084 return
2085
2086 # Look for matching comment at end of namespace.
2087 #
2088 # Note that we accept C style "/* */" comments for terminating
2089 # namespaces, so that code that terminate namespaces inside
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002090 # preprocessor macros can be cpplint clean.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002091 #
2092 # We also accept stuff like "// end of namespace <name>." with the
2093 # period at the end.
2094 #
2095 # Besides these, we don't accept anything else, otherwise we might
2096 # get false negatives when existing comment is a substring of the
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002097 # expected namespace.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002098 if self.name:
2099 # Named namespace
2100 if not Match((r'};*\s*(//|/\*).*\bnamespace\s+' + re.escape(self.name) +
2101 r'[\*/\.\\\s]*$'),
2102 line):
2103 error(filename, linenum, 'readability/namespace', 5,
2104 'Namespace should be terminated with "// namespace %s"' %
2105 self.name)
2106 else:
2107 # Anonymous namespace
2108 if not Match(r'};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002109 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2110 # mention "// anonymous namespace" as an acceptable form
2111 if Match(r'}.*\b(namespace anonymous|anonymous namespace)\b', line):
2112 error(filename, linenum, 'readability/namespace', 5,
2113 'Anonymous namespace should be terminated with "// namespace"'
2114 ' or "// anonymous namespace"')
2115 else:
2116 error(filename, linenum, 'readability/namespace', 5,
2117 'Anonymous namespace should be terminated with "// namespace"')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002118
2119
2120class _PreprocessorInfo(object):
2121 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2122
2123 def __init__(self, stack_before_if):
2124 # The entire nesting stack before #if
2125 self.stack_before_if = stack_before_if
2126
2127 # The entire nesting stack up to #else
2128 self.stack_before_else = []
2129
2130 # Whether we have already seen #else or #elif
2131 self.seen_else = False
2132
2133
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002134class NestingState(object):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002135 """Holds states related to parsing braces."""
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002136
2137 def __init__(self):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002138 # Stack for tracking all braces. An object is pushed whenever we
2139 # see a "{", and popped when we see a "}". Only 3 types of
2140 # objects are possible:
2141 # - _ClassInfo: a class or struct.
2142 # - _NamespaceInfo: a namespace.
2143 # - _BlockInfo: some other type of block.
2144 self.stack = []
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002145
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002146 # Top of the previous stack before each Update().
2147 #
2148 # Because the nesting_stack is updated at the end of each line, we
2149 # had to do some convoluted checks to find out what is the current
2150 # scope at the beginning of the line. This check is simplified by
2151 # saving the previous top of nesting stack.
2152 #
2153 # We could save the full stack, but we only need the top. Copying
2154 # the full nesting stack would slow down cpplint by ~10%.
2155 self.previous_stack_top = []
2156
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002157 # Stack of _PreprocessorInfo objects.
2158 self.pp_stack = []
2159
2160 def SeenOpenBrace(self):
2161 """Check if we have seen the opening brace for the innermost block.
2162
2163 Returns:
2164 True if we have seen the opening brace, False if the innermost
2165 block is still expecting an opening brace.
2166 """
2167 return (not self.stack) or self.stack[-1].seen_open_brace
2168
2169 def InNamespaceBody(self):
2170 """Check if we are currently one level inside a namespace body.
2171
2172 Returns:
2173 True if top of the stack is a namespace block, False otherwise.
2174 """
2175 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2176
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002177 def InExternC(self):
2178 """Check if we are currently one level inside an 'extern "C"' block.
2179
2180 Returns:
2181 True if top of the stack is an extern block, False otherwise.
2182 """
2183 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2184
2185 def InClassDeclaration(self):
2186 """Check if we are currently one level inside a class or struct declaration.
2187
2188 Returns:
2189 True if top of the stack is a class/struct, False otherwise.
2190 """
2191 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2192
2193 def InAsmBlock(self):
2194 """Check if we are currently one level inside an inline ASM block.
2195
2196 Returns:
2197 True if the top of the stack is a block containing inline ASM.
2198 """
2199 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2200
2201 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2202 """Check if current position is inside template argument list.
2203
2204 Args:
2205 clean_lines: A CleansedLines instance containing the file.
2206 linenum: The number of the line to check.
2207 pos: position just after the suspected template argument.
2208 Returns:
2209 True if (linenum, pos) is inside template arguments.
2210 """
2211 while linenum < clean_lines.NumLines():
2212 # Find the earliest character that might indicate a template argument
2213 line = clean_lines.elided[linenum]
2214 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2215 if not match:
2216 linenum += 1
2217 pos = 0
2218 continue
2219 token = match.group(1)
2220 pos += len(match.group(0))
2221
2222 # These things do not look like template argument list:
2223 # class Suspect {
2224 # class Suspect x; }
2225 if token in ('{', '}', ';'): return False
2226
2227 # These things look like template argument list:
2228 # template <class Suspect>
2229 # template <class Suspect = default_value>
2230 # template <class Suspect[]>
2231 # template <class Suspect...>
2232 if token in ('>', '=', '[', ']', '.'): return True
2233
2234 # Check if token is an unmatched '<'.
2235 # If not, move on to the next character.
2236 if token != '<':
2237 pos += 1
2238 if pos >= len(line):
2239 linenum += 1
2240 pos = 0
2241 continue
2242
2243 # We can't be sure if we just find a single '<', and need to
2244 # find the matching '>'.
2245 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2246 if end_pos < 0:
2247 # Not sure if template argument list or syntax error in file
2248 return False
2249 linenum = end_line
2250 pos = end_pos
2251 return False
2252
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002253 def UpdatePreprocessor(self, line):
2254 """Update preprocessor stack.
2255
2256 We need to handle preprocessors due to classes like this:
2257 #ifdef SWIG
2258 struct ResultDetailsPageElementExtensionPoint {
2259 #else
2260 struct ResultDetailsPageElementExtensionPoint : public Extension {
2261 #endif
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002262
2263 We make the following assumptions (good enough for most files):
2264 - Preprocessor condition evaluates to true from #if up to first
2265 #else/#elif/#endif.
2266
2267 - Preprocessor condition evaluates to false from #else/#elif up
2268 to #endif. We still perform lint checks on these lines, but
2269 these do not affect nesting stack.
2270
2271 Args:
2272 line: current line to check.
2273 """
2274 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2275 # Beginning of #if block, save the nesting stack here. The saved
2276 # stack will allow us to restore the parsing state in the #else case.
2277 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2278 elif Match(r'^\s*#\s*(else|elif)\b', line):
2279 # Beginning of #else block
2280 if self.pp_stack:
2281 if not self.pp_stack[-1].seen_else:
2282 # This is the first #else or #elif block. Remember the
2283 # whole nesting stack up to this point. This is what we
2284 # keep after the #endif.
2285 self.pp_stack[-1].seen_else = True
2286 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2287
2288 # Restore the stack to how it was before the #if
2289 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2290 else:
2291 # TODO(unknown): unexpected #else, issue warning?
2292 pass
2293 elif Match(r'^\s*#\s*endif\b', line):
2294 # End of #if or #else blocks.
2295 if self.pp_stack:
2296 # If we saw an #else, we will need to restore the nesting
2297 # stack to its former state before the #else, otherwise we
2298 # will just continue from where we left off.
2299 if self.pp_stack[-1].seen_else:
2300 # Here we can just use a shallow copy since we are the last
2301 # reference to it.
2302 self.stack = self.pp_stack[-1].stack_before_else
2303 # Drop the corresponding #if
2304 self.pp_stack.pop()
2305 else:
2306 # TODO(unknown): unexpected #endif, issue warning?
2307 pass
2308
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002309 # TODO(unknown): Update() is too long, but we will refactor later.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002310 def Update(self, filename, clean_lines, linenum, error):
2311 """Update nesting state with current line.
2312
2313 Args:
2314 filename: The name of the current file.
2315 clean_lines: A CleansedLines instance containing the file.
2316 linenum: The number of the line to check.
2317 error: The function to call with any errors found.
2318 """
2319 line = clean_lines.elided[linenum]
2320
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002321 # Remember top of the previous nesting stack.
2322 #
2323 # The stack is always pushed/popped and not modified in place, so
2324 # we can just do a shallow copy instead of copy.deepcopy. Using
2325 # deepcopy would slow down cpplint by ~28%.
2326 if self.stack:
2327 self.previous_stack_top = self.stack[-1]
2328 else:
2329 self.previous_stack_top = None
2330
2331 # Update pp_stack
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002332 self.UpdatePreprocessor(line)
2333
2334 # Count parentheses. This is to avoid adding struct arguments to
2335 # the nesting stack.
2336 if self.stack:
2337 inner_block = self.stack[-1]
2338 depth_change = line.count('(') - line.count(')')
2339 inner_block.open_parentheses += depth_change
2340
2341 # Also check if we are starting or ending an inline assembly block.
2342 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2343 if (depth_change != 0 and
2344 inner_block.open_parentheses == 1 and
2345 _MATCH_ASM.match(line)):
2346 # Enter assembly block
2347 inner_block.inline_asm = _INSIDE_ASM
2348 else:
2349 # Not entering assembly block. If previous line was _END_ASM,
2350 # we will now shift to _NO_ASM state.
2351 inner_block.inline_asm = _NO_ASM
2352 elif (inner_block.inline_asm == _INSIDE_ASM and
2353 inner_block.open_parentheses == 0):
2354 # Exit assembly block
2355 inner_block.inline_asm = _END_ASM
2356
2357 # Consume namespace declaration at the beginning of the line. Do
2358 # this in a loop so that we catch same line declarations like this:
2359 # namespace proto2 { namespace bridge { class MessageSet; } }
2360 while True:
2361 # Match start of namespace. The "\b\s*" below catches namespace
2362 # declarations even if it weren't followed by a whitespace, this
2363 # is so that we don't confuse our namespace checker. The
2364 # missing spaces will be flagged by CheckSpacing.
2365 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2366 if not namespace_decl_match:
2367 break
2368
2369 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2370 self.stack.append(new_namespace)
2371
2372 line = namespace_decl_match.group(2)
2373 if line.find('{') != -1:
2374 new_namespace.seen_open_brace = True
2375 line = line[line.find('{') + 1:]
2376
2377 # Look for a class declaration in whatever is left of the line
2378 # after parsing namespaces. The regexp accounts for decorated classes
2379 # such as in:
2380 # class LOCKABLE API Object {
2381 # };
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002382 class_decl_match = Match(
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002383 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2384 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2385 r'(.*)$', line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002386 if (class_decl_match and
2387 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002388 # We do not want to accept classes that are actually template arguments:
2389 # template <class Ignore1,
2390 # class Ignore2 = Default<Args>,
2391 # template <Args> class Ignore3>
2392 # void Function() {};
2393 #
2394 # To avoid template argument cases, we scan forward and look for
2395 # an unmatched '>'. If we see one, assume we are inside a
2396 # template argument list.
2397 end_declaration = len(class_decl_match.group(1))
2398 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2399 self.stack.append(_ClassInfo(
2400 class_decl_match.group(3), class_decl_match.group(2),
2401 clean_lines, linenum))
2402 line = class_decl_match.group(4)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002403
2404 # If we have not yet seen the opening brace for the innermost block,
2405 # run checks here.
2406 if not self.SeenOpenBrace():
2407 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2408
2409 # Update access control if we are inside a class/struct
2410 if self.stack and isinstance(self.stack[-1], _ClassInfo):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002411 classinfo = self.stack[-1]
2412 access_match = Match(
2413 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2414 r':(?:[^:]|$)',
2415 line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002416 if access_match:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002417 classinfo.access = access_match.group(2)
2418
2419 # Check that access keywords are indented +1 space. Skip this
2420 # check if the keywords are not preceded by whitespaces.
2421 indent = access_match.group(1)
2422 if (len(indent) != classinfo.class_indent + 1 and
2423 Match(r'^\s*$', indent)):
2424 if classinfo.is_struct:
2425 parent = 'struct ' + classinfo.name
2426 else:
2427 parent = 'class ' + classinfo.name
2428 slots = ''
2429 if access_match.group(3):
2430 slots = access_match.group(3)
2431 error(filename, linenum, 'whitespace/indent', 3,
2432 '%s%s: should be indented +1 space inside %s' % (
2433 access_match.group(2), slots, parent))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002434
2435 # Consume braces or semicolons from what's left of the line
2436 while True:
2437 # Match first brace, semicolon, or closed parenthesis.
2438 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2439 if not matched:
2440 break
2441
2442 token = matched.group(1)
2443 if token == '{':
2444 # If namespace or class hasn't seen a opening brace yet, mark
2445 # namespace/class head as complete. Push a new block onto the
2446 # stack otherwise.
2447 if not self.SeenOpenBrace():
2448 self.stack[-1].seen_open_brace = True
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002449 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
2450 self.stack.append(_ExternCInfo())
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002451 else:
2452 self.stack.append(_BlockInfo(True))
2453 if _MATCH_ASM.match(line):
2454 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002455
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002456 elif token == ';' or token == ')':
2457 # If we haven't seen an opening brace yet, but we already saw
2458 # a semicolon, this is probably a forward declaration. Pop
2459 # the stack for these.
2460 #
2461 # Similarly, if we haven't seen an opening brace yet, but we
2462 # already saw a closing parenthesis, then these are probably
2463 # function arguments with extra "class" or "struct" keywords.
2464 # Also pop these stack for these.
2465 if not self.SeenOpenBrace():
2466 self.stack.pop()
2467 else: # token == '}'
2468 # Perform end of block checks and pop the stack.
2469 if self.stack:
2470 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2471 self.stack.pop()
2472 line = matched.group(2)
2473
2474 def InnermostClass(self):
2475 """Get class info on the top of the stack.
2476
2477 Returns:
2478 A _ClassInfo object if we are inside a class, or None otherwise.
2479 """
2480 for i in range(len(self.stack), 0, -1):
2481 classinfo = self.stack[i - 1]
2482 if isinstance(classinfo, _ClassInfo):
2483 return classinfo
2484 return None
2485
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002486 def CheckCompletedBlocks(self, filename, error):
2487 """Checks that all classes and namespaces have been completely parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002488
2489 Call this when all lines in a file have been processed.
2490 Args:
2491 filename: The name of the current file.
2492 error: The function to call with any errors found.
2493 """
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002494 # Note: This test can result in false positives if #ifdef constructs
2495 # get in the way of brace matching. See the testBuildClass test in
2496 # cpplint_unittest.py for an example of this.
2497 for obj in self.stack:
2498 if isinstance(obj, _ClassInfo):
2499 error(filename, obj.starting_linenum, 'build/class', 5,
2500 'Failed to find complete declaration of class %s' %
2501 obj.name)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002502 elif isinstance(obj, _NamespaceInfo):
2503 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2504 'Failed to find complete declaration of namespace %s' %
2505 obj.name)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002506
2507
2508def CheckForNonStandardConstructs(filename, clean_lines, linenum,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002509 nesting_state, error):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002510 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002511
2512 Complain about several constructs which gcc-2 accepts, but which are
2513 not standard C++. Warning about these in lint is one way to ease the
2514 transition to new compilers.
2515 - put storage class first (e.g. "static const" instead of "const static").
2516 - "%lld" instead of %qd" in printf-type functions.
2517 - "%1$d" is non-standard in printf-type functions.
2518 - "\%" is an undefined character escape sequence.
2519 - text after #endif is not allowed.
2520 - invalid inner-style forward declaration.
2521 - >? and <? operators, and their >?= and <?= cousins.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002522
erg@google.com26970fa2009-11-17 18:07:32 +00002523 Additionally, check for constructor/destructor style violations and reference
2524 members, as it is very convenient to do so while checking for
2525 gcc-2 compliance.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002526
2527 Args:
2528 filename: The name of the current file.
2529 clean_lines: A CleansedLines instance containing the file.
2530 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002531 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002532 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002533 error: A callable to which errors are reported, which takes 4 arguments:
2534 filename, line number, error level, and message
2535 """
2536
2537 # Remove comments from the line, but leave in strings for now.
2538 line = clean_lines.lines[linenum]
2539
2540 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2541 error(filename, linenum, 'runtime/printf_format', 3,
2542 '%q in format strings is deprecated. Use %ll instead.')
2543
2544 if Search(r'printf\s*\(.*".*%\d+\$', line):
2545 error(filename, linenum, 'runtime/printf_format', 2,
2546 '%N$ formats are unconventional. Try rewriting to avoid them.')
2547
2548 # Remove escaped backslashes before looking for undefined escapes.
2549 line = line.replace('\\\\', '')
2550
2551 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2552 error(filename, linenum, 'build/printf_format', 3,
2553 '%, [, (, and { are undefined character escapes. Unescape them.')
2554
2555 # For the rest, work with both comments and strings removed.
2556 line = clean_lines.elided[linenum]
2557
2558 if Search(r'\b(const|volatile|void|char|short|int|long'
2559 r'|float|double|signed|unsigned'
2560 r'|schar|u?int8|u?int16|u?int32|u?int64)'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002561 r'\s+(register|static|extern|typedef)\b',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002562 line):
2563 error(filename, linenum, 'build/storage_class', 5,
2564 'Storage class (static, extern, typedef, etc) should be first.')
2565
2566 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2567 error(filename, linenum, 'build/endif_comment', 5,
2568 'Uncommented text after #endif is non-standard. Use a comment.')
2569
2570 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2571 error(filename, linenum, 'build/forward_decl', 5,
2572 'Inner-style forward declarations are invalid. Remove this line.')
2573
2574 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2575 line):
2576 error(filename, linenum, 'build/deprecated', 3,
2577 '>? and <? (max and min) operators are non-standard and deprecated.')
2578
erg@google.com26970fa2009-11-17 18:07:32 +00002579 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2580 # TODO(unknown): Could it be expanded safely to arbitrary references,
2581 # without triggering too many false positives? The first
2582 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2583 # the restriction.
2584 # Here's the original regexp, for the reference:
2585 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2586 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2587 error(filename, linenum, 'runtime/member_string_references', 2,
2588 'const string& members are dangerous. It is much better to use '
2589 'alternatives, such as pointers or simple constants.')
2590
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002591 # Everything else in this function operates on class declarations.
2592 # Return early if the top of the nesting stack is not a class, or if
2593 # the class head is not completed yet.
2594 classinfo = nesting_state.InnermostClass()
2595 if not classinfo or not classinfo.seen_open_brace:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002596 return
2597
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002598 # The class may have been declared with namespace or classname qualifiers.
2599 # The constructor and destructor will not have those qualifiers.
2600 base_classname = classinfo.name.split('::')[-1]
2601
2602 # Look for single-argument constructors that aren't marked explicit.
avakulenko@google.com59146752014-08-11 20:20:55 +00002603 # Technically a valid construct, but against style. Also look for
2604 # non-single-argument constructors which are also technically valid, but
2605 # strongly suggest something is wrong.
2606 explicit_constructor_match = Match(
2607 r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*'
2608 r'\(((?:[^()]|\([^()]*\))*)\)'
2609 % re.escape(base_classname),
2610 line)
2611
2612 if explicit_constructor_match:
2613 is_marked_explicit = explicit_constructor_match.group(1)
2614
2615 if not explicit_constructor_match.group(2):
2616 constructor_args = []
2617 else:
2618 constructor_args = explicit_constructor_match.group(2).split(',')
2619
2620 # collapse arguments so that commas in template parameter lists and function
2621 # argument parameter lists don't split arguments in two
2622 i = 0
2623 while i < len(constructor_args):
2624 constructor_arg = constructor_args[i]
2625 while (constructor_arg.count('<') > constructor_arg.count('>') or
2626 constructor_arg.count('(') > constructor_arg.count(')')):
2627 constructor_arg += ',' + constructor_args[i + 1]
2628 del constructor_args[i + 1]
2629 constructor_args[i] = constructor_arg
2630 i += 1
2631
2632 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2633 noarg_constructor = (not constructor_args or # empty arg list
2634 # 'void' arg specifier
2635 (len(constructor_args) == 1 and
2636 constructor_args[0].strip() == 'void'))
2637 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2638 not noarg_constructor) or
2639 # all but at most one arg defaulted
2640 (len(constructor_args) >= 1 and
2641 not noarg_constructor and
2642 len(defaulted_args) >= len(constructor_args) - 1))
2643 initializer_list_constructor = bool(
2644 onearg_constructor and
2645 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2646 copy_constructor = bool(
2647 onearg_constructor and
2648 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2649 % re.escape(base_classname), constructor_args[0].strip()))
2650
2651 if (not is_marked_explicit and
2652 onearg_constructor and
2653 not initializer_list_constructor and
2654 not copy_constructor):
2655 if defaulted_args:
2656 error(filename, linenum, 'runtime/explicit', 5,
2657 'Constructors callable with one argument '
2658 'should be marked explicit.')
2659 else:
2660 error(filename, linenum, 'runtime/explicit', 5,
2661 'Single-parameter constructors should be marked explicit.')
2662 elif is_marked_explicit and not onearg_constructor:
2663 if noarg_constructor:
2664 error(filename, linenum, 'runtime/explicit', 5,
2665 'Zero-parameter constructors should not be marked explicit.')
2666 else:
2667 error(filename, linenum, 'runtime/explicit', 0,
2668 'Constructors that require multiple arguments '
2669 'should not be marked explicit.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002670
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002671
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002672def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002673 """Checks for the correctness of various spacing around function calls.
2674
2675 Args:
2676 filename: The name of the current file.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002677 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002678 linenum: The number of the line to check.
2679 error: The function to call with any errors found.
2680 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002681 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002682
2683 # Since function calls often occur inside if/for/while/switch
2684 # expressions - which have their own, more liberal conventions - we
2685 # first see if we should be looking inside such an expression for a
2686 # function call, to which we can apply more strict standards.
2687 fncall = line # if there's no control flow construct, look at whole line
2688 for pattern in (r'\bif\s*\((.*)\)\s*{',
2689 r'\bfor\s*\((.*)\)\s*{',
2690 r'\bwhile\s*\((.*)\)\s*[{;]',
2691 r'\bswitch\s*\((.*)\)\s*{'):
2692 match = Search(pattern, line)
2693 if match:
2694 fncall = match.group(1) # look inside the parens for function calls
2695 break
2696
2697 # Except in if/for/while/switch, there should never be space
2698 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2699 # for nested parens ( (a+b) + c ). Likewise, there should never be
2700 # a space before a ( when it's a function argument. I assume it's a
2701 # function argument when the char before the whitespace is legal in
2702 # a function name (alnum + _) and we're not starting a macro. Also ignore
2703 # pointers and references to arrays and functions coz they're too tricky:
2704 # we use a very simple way to recognize these:
2705 # " (something)(maybe-something)" or
2706 # " (something)(maybe-something," or
2707 # " (something)[something]"
2708 # Note that we assume the contents of [] to be short enough that
2709 # they'll never need to wrap.
2710 if ( # Ignore control structures.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002711 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
2712 fncall) and
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002713 # Ignore pointers/references to functions.
2714 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2715 # Ignore pointers/references to arrays.
2716 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com6317a9c2009-06-25 00:28:19 +00002717 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002718 error(filename, linenum, 'whitespace/parens', 4,
2719 'Extra space after ( in function call')
erg@google.com6317a9c2009-06-25 00:28:19 +00002720 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002721 error(filename, linenum, 'whitespace/parens', 2,
2722 'Extra space after (')
2723 if (Search(r'\w\s+\(', fncall) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002724 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002725 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002726 # TODO(unknown): Space after an operator function seem to be a common
2727 # error, silence those for now by restricting them to highest verbosity.
2728 if Search(r'\boperator_*\b', line):
2729 error(filename, linenum, 'whitespace/parens', 0,
2730 'Extra space before ( in function call')
2731 else:
2732 error(filename, linenum, 'whitespace/parens', 4,
2733 'Extra space before ( in function call')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002734 # If the ) is followed only by a newline or a { + newline, assume it's
2735 # part of a control statement (if/while/etc), and don't complain
2736 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002737 # If the closing parenthesis is preceded by only whitespaces,
2738 # try to give a more descriptive error message.
2739 if Search(r'^\s+\)', fncall):
2740 error(filename, linenum, 'whitespace/parens', 2,
2741 'Closing ) should be moved to the previous line')
2742 else:
2743 error(filename, linenum, 'whitespace/parens', 2,
2744 'Extra space before )')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002745
2746
2747def IsBlankLine(line):
2748 """Returns true if the given line is blank.
2749
2750 We consider a line to be blank if the line is empty or consists of
2751 only white spaces.
2752
2753 Args:
2754 line: A line of a string.
2755
2756 Returns:
2757 True, if the given line is blank.
2758 """
2759 return not line or line.isspace()
2760
2761
avakulenko@google.com59146752014-08-11 20:20:55 +00002762def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
2763 error):
2764 is_namespace_indent_item = (
2765 len(nesting_state.stack) > 1 and
2766 nesting_state.stack[-1].check_namespace_indentation and
2767 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
2768 nesting_state.previous_stack_top == nesting_state.stack[-2])
2769
2770 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
2771 clean_lines.elided, line):
2772 CheckItemIndentationInNamespace(filename, clean_lines.elided,
2773 line, error)
2774
2775
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002776def CheckForFunctionLengths(filename, clean_lines, linenum,
2777 function_state, error):
2778 """Reports for long function bodies.
2779
2780 For an overview why this is done, see:
2781 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
2782
2783 Uses a simplistic algorithm assuming other style guidelines
2784 (especially spacing) are followed.
2785 Only checks unindented functions, so class members are unchecked.
2786 Trivial bodies are unchecked, so constructors with huge initializer lists
2787 may be missed.
2788 Blank/comment lines are not counted so as to avoid encouraging the removal
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002789 of vertical space and comments just to get through a lint check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002790 NOLINT *on the last line of a function* disables this check.
2791
2792 Args:
2793 filename: The name of the current file.
2794 clean_lines: A CleansedLines instance containing the file.
2795 linenum: The number of the line to check.
2796 function_state: Current function name and lines in body so far.
2797 error: The function to call with any errors found.
2798 """
2799 lines = clean_lines.lines
2800 line = lines[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002801 joined_line = ''
2802
2803 starting_func = False
erg@google.com6317a9c2009-06-25 00:28:19 +00002804 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002805 match_result = Match(regexp, line)
2806 if match_result:
2807 # If the name is all caps and underscores, figure it's a macro and
2808 # ignore it, unless it's TEST or TEST_F.
2809 function_name = match_result.group(1).split()[-1]
2810 if function_name == 'TEST' or function_name == 'TEST_F' or (
2811 not Match(r'[A-Z_]+$', function_name)):
2812 starting_func = True
2813
2814 if starting_func:
2815 body_found = False
erg@google.com6317a9c2009-06-25 00:28:19 +00002816 for start_linenum in xrange(linenum, clean_lines.NumLines()):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002817 start_line = lines[start_linenum]
2818 joined_line += ' ' + start_line.lstrip()
2819 if Search(r'(;|})', start_line): # Declarations and trivial functions
2820 body_found = True
2821 break # ... ignore
2822 elif Search(r'{', start_line):
2823 body_found = True
2824 function = Search(r'((\w|:)*)\(', line).group(1)
2825 if Match(r'TEST', function): # Handle TEST... macros
2826 parameter_regexp = Search(r'(\(.*\))', joined_line)
2827 if parameter_regexp: # Ignore bad syntax
2828 function += parameter_regexp.group(1)
2829 else:
2830 function += '()'
2831 function_state.Begin(function)
2832 break
2833 if not body_found:
erg@google.com6317a9c2009-06-25 00:28:19 +00002834 # No body for the function (or evidence of a non-function) was found.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002835 error(filename, linenum, 'readability/fn_size', 5,
2836 'Lint failed to find start of function body.')
2837 elif Match(r'^\}\s*$', line): # function end
erg@google.com35589e62010-11-17 18:58:16 +00002838 function_state.Check(error, filename, linenum)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002839 function_state.End()
2840 elif not Match(r'^\s*$', line):
2841 function_state.Count() # Count non-blank/non-comment lines.
2842
2843
2844_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
2845
2846
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002847def CheckComment(line, filename, linenum, next_line_start, error):
2848 """Checks for common mistakes in comments.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002849
2850 Args:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002851 line: The line in question.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002852 filename: The name of the current file.
2853 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002854 next_line_start: The first non-whitespace column of the next line.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002855 error: The function to call with any errors found.
2856 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002857 commentpos = line.find('//')
2858 if commentpos != -1:
2859 # Check if the // may be in quotes. If so, ignore it
2860 # Comparisons made explicit for clarity -- pylint: disable=g-explicit-bool-comparison
2861 if (line.count('"', 0, commentpos) -
2862 line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes
2863 # Allow one space for new scopes, two spaces otherwise:
2864 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
2865 ((commentpos >= 1 and
2866 line[commentpos-1] not in string.whitespace) or
2867 (commentpos >= 2 and
2868 line[commentpos-2] not in string.whitespace))):
2869 error(filename, linenum, 'whitespace/comments', 2,
2870 'At least two spaces is best between code and comments')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002871
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002872 # Checks for common mistakes in TODO comments.
2873 comment = line[commentpos:]
2874 match = _RE_PATTERN_TODO.match(comment)
2875 if match:
2876 # One whitespace is correct; zero whitespace is handled elsewhere.
2877 leading_whitespace = match.group(1)
2878 if len(leading_whitespace) > 1:
2879 error(filename, linenum, 'whitespace/todo', 2,
2880 'Too many spaces before TODO')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002881
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002882 username = match.group(2)
2883 if not username:
2884 error(filename, linenum, 'readability/todo', 2,
2885 'Missing username in TODO; it should look like '
2886 '"// TODO(my_username): Stuff."')
2887
2888 middle_whitespace = match.group(3)
2889 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
2890 if middle_whitespace != ' ' and middle_whitespace != '':
2891 error(filename, linenum, 'whitespace/todo', 2,
2892 'TODO(my_username) should be followed by a space')
2893
2894 # If the comment contains an alphanumeric character, there
2895 # should be a space somewhere between it and the //.
2896 if Match(r'//[^ ]*\w', comment):
2897 error(filename, linenum, 'whitespace/comments', 4,
2898 'Should have a space between // and comment')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002899
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002900def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
2901 """Checks for improper use of DISALLOW* macros.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002902
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002903 Args:
2904 filename: The name of the current file.
2905 clean_lines: A CleansedLines instance containing the file.
2906 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002907 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002908 the current stack of nested blocks being parsed.
2909 error: The function to call with any errors found.
2910 """
2911 line = clean_lines.elided[linenum] # get rid of comments and strings
2912
2913 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002914 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
2915 if not matched:
2916 return
2917 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
2918 if nesting_state.stack[-1].access != 'private':
2919 error(filename, linenum, 'readability/constructors', 3,
2920 '%s must be in the private: section' % matched.group(1))
2921
2922 else:
2923 # Found DISALLOW* macro outside a class declaration, or perhaps it
2924 # was used inside a function when it should have been part of the
2925 # class declaration. We could issue a warning here, but it
2926 # probably resulted in a compiler error already.
2927 pass
2928
2929
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002930def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002931 """Checks for the correctness of various spacing issues in the code.
2932
2933 Things we check for: spaces around operators, spaces after
2934 if/for/while/switch, no spaces around parens in function calls, two
2935 spaces between code and comment, don't start a block with a blank
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002936 line, don't end a function with a blank line, don't add a blank line
2937 after public/protected/private, don't have too many blank lines in a row.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002938
2939 Args:
2940 filename: The name of the current file.
2941 clean_lines: A CleansedLines instance containing the file.
2942 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002943 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002944 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002945 error: The function to call with any errors found.
2946 """
2947
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002948 # Don't use "elided" lines here, otherwise we can't check commented lines.
2949 # Don't want to use "raw" either, because we don't want to check inside C++11
2950 # raw strings,
2951 raw = clean_lines.lines_without_raw_strings
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002952 line = raw[linenum]
2953
2954 # Before nixing comments, check if the line is blank for no good
2955 # reason. This includes the first line after a block is opened, and
2956 # blank lines at the end of a function (ie, right before a line like '}'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002957 #
2958 # Skip all the blank line checks if we are immediately inside a
2959 # namespace body. In other words, don't issue blank line warnings
2960 # for this block:
2961 # namespace {
2962 #
2963 # }
2964 #
2965 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002966 #
2967 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
2968 # like namespaces.
2969 if (IsBlankLine(line) and
2970 not nesting_state.InNamespaceBody() and
2971 not nesting_state.InExternC()):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002972 elided = clean_lines.elided
2973 prev_line = elided[linenum - 1]
2974 prevbrace = prev_line.rfind('{')
2975 # TODO(unknown): Don't complain if line before blank line, and line after,
2976 # both start with alnums and are indented the same amount.
2977 # This ignores whitespace at the start of a namespace block
2978 # because those are not usually indented.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002979 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002980 # OK, we have a blank line at the start of a code block. Before we
2981 # complain, we check if it is an exception to the rule: The previous
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002982 # non-empty line has the parameters of a function header that are indented
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002983 # 4 spaces (because they did not fit in a 80 column line when placed on
2984 # the same line as the function name). We also check for the case where
2985 # the previous line is indented 6 spaces, which may happen when the
2986 # initializers of a constructor do not fit into a 80 column line.
2987 exception = False
2988 if Match(r' {6}\w', prev_line): # Initializer list?
2989 # We are looking for the opening column of initializer list, which
2990 # should be indented 4 spaces to cause 6 space indentation afterwards.
2991 search_position = linenum-2
2992 while (search_position >= 0
2993 and Match(r' {6}\w', elided[search_position])):
2994 search_position -= 1
2995 exception = (search_position >= 0
2996 and elided[search_position][:5] == ' :')
2997 else:
2998 # Search for the function arguments or an initializer list. We use a
2999 # simple heuristic here: If the line is indented 4 spaces; and we have a
3000 # closing paren, without the opening paren, followed by an opening brace
3001 # or colon (for initializer lists) we assume that it is the last line of
3002 # a function header. If we have a colon indented 4 spaces, it is an
3003 # initializer list.
3004 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3005 prev_line)
3006 or Match(r' {4}:', prev_line))
3007
3008 if not exception:
3009 error(filename, linenum, 'whitespace/blank_line', 2,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003010 'Redundant blank line at the start of a code block '
3011 'should be deleted.')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003012 # Ignore blank lines at the end of a block in a long if-else
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003013 # chain, like this:
3014 # if (condition1) {
3015 # // Something followed by a blank line
3016 #
3017 # } else if (condition2) {
3018 # // Something else
3019 # }
3020 if linenum + 1 < clean_lines.NumLines():
3021 next_line = raw[linenum + 1]
3022 if (next_line
3023 and Match(r'\s*}', next_line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003024 and next_line.find('} else ') == -1):
3025 error(filename, linenum, 'whitespace/blank_line', 3,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003026 'Redundant blank line at the end of a code block '
3027 'should be deleted.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003028
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003029 matched = Match(r'\s*(public|protected|private):', prev_line)
3030 if matched:
3031 error(filename, linenum, 'whitespace/blank_line', 3,
3032 'Do not leave a blank line after "%s:"' % matched.group(1))
3033
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003034 # Next, check comments
3035 next_line_start = 0
3036 if linenum + 1 < clean_lines.NumLines():
3037 next_line = raw[linenum + 1]
3038 next_line_start = len(next_line) - len(next_line.lstrip())
3039 CheckComment(line, filename, linenum, next_line_start, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003040
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003041 # get rid of comments and strings
3042 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003043
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003044 # You shouldn't have spaces before your brackets, except maybe after
3045 # 'delete []' or 'return []() {};'
3046 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3047 error(filename, linenum, 'whitespace/braces', 5,
3048 'Extra space before [')
3049
3050 # In range-based for, we wanted spaces before and after the colon, but
3051 # not around "::" tokens that might appear.
3052 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3053 Search(r'for *\(.*[^: ]:[^:]', line)):
3054 error(filename, linenum, 'whitespace/forcolon', 2,
3055 'Missing space around colon in range-based for loop')
3056
3057
3058def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3059 """Checks for horizontal spacing around operators.
3060
3061 Args:
3062 filename: The name of the current file.
3063 clean_lines: A CleansedLines instance containing the file.
3064 linenum: The number of the line to check.
3065 error: The function to call with any errors found.
3066 """
3067 line = clean_lines.elided[linenum]
3068
3069 # Don't try to do spacing checks for operator methods. Do this by
3070 # replacing the troublesome characters with something else,
3071 # preserving column position for all other characters.
3072 #
3073 # The replacement is done repeatedly to avoid false positives from
3074 # operators that call operators.
3075 while True:
3076 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3077 if match:
3078 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3079 else:
3080 break
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003081
3082 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3083 # Otherwise not. Note we only check for non-spaces on *both* sides;
3084 # sometimes people put non-spaces on one side when aligning ='s among
3085 # many lines (not that this is behavior that I approve of...)
3086 if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line):
3087 error(filename, linenum, 'whitespace/operators', 4,
3088 'Missing spaces around =')
3089
3090 # It's ok not to have spaces around binary operators like + - * /, but if
3091 # there's too little whitespace, we get concerned. It's hard to tell,
3092 # though, so we punt on this one for now. TODO.
3093
3094 # You should always have whitespace around binary operators.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003095 #
3096 # Check <= and >= first to avoid false positives with < and >, then
3097 # check non-include lines for spacing around < and >.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003098 #
3099 # If the operator is followed by a comma, assume it's be used in a
3100 # macro context and don't do any checks. This avoids false
3101 # positives.
3102 #
3103 # Note that && is not included here. Those are checked separately
3104 # in CheckRValueReference
3105 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003106 if match:
3107 error(filename, linenum, 'whitespace/operators', 3,
3108 'Missing spaces around %s' % match.group(1))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003109 elif not Match(r'#.*include', line):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003110 # Look for < that is not surrounded by spaces. This is only
3111 # triggered if both sides are missing spaces, even though
3112 # technically should should flag if at least one side is missing a
3113 # space. This is done to avoid some false positives with shifts.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003114 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3115 if match:
3116 (_, _, end_pos) = CloseExpression(
3117 clean_lines, linenum, len(match.group(1)))
3118 if end_pos <= -1:
3119 error(filename, linenum, 'whitespace/operators', 3,
3120 'Missing spaces around <')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003121
3122 # Look for > that is not surrounded by spaces. Similar to the
3123 # above, we only trigger if both sides are missing spaces to avoid
3124 # false positives with shifts.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003125 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3126 if match:
3127 (_, _, start_pos) = ReverseCloseExpression(
3128 clean_lines, linenum, len(match.group(1)))
3129 if start_pos <= -1:
3130 error(filename, linenum, 'whitespace/operators', 3,
3131 'Missing spaces around >')
3132
3133 # We allow no-spaces around << when used like this: 10<<20, but
3134 # not otherwise (particularly, not when used as streams)
avakulenko@google.com59146752014-08-11 20:20:55 +00003135 #
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003136 # We also allow operators following an opening parenthesis, since
3137 # those tend to be macros that deal with operators.
3138 match = Search(r'(operator|\S)(?:L|UL|ULL|l|ul|ull)?<<([^\s,=])', line)
3139 if (match and match.group(1) != '(' and
3140 not (match.group(1).isdigit() and match.group(2).isdigit()) and
3141 not (match.group(1) == 'operator' and match.group(2) == ';')):
3142 error(filename, linenum, 'whitespace/operators', 3,
3143 'Missing spaces around <<')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003144
3145 # We allow no-spaces around >> for almost anything. This is because
3146 # C++11 allows ">>" to close nested templates, which accounts for
3147 # most cases when ">>" is not followed by a space.
3148 #
3149 # We still warn on ">>" followed by alpha character, because that is
3150 # likely due to ">>" being used for right shifts, e.g.:
3151 # value >> alpha
3152 #
3153 # When ">>" is used to close templates, the alphanumeric letter that
3154 # follows would be part of an identifier, and there should still be
3155 # a space separating the template type and the identifier.
3156 # type<type<type>> alpha
3157 match = Search(r'>>[a-zA-Z_]', line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003158 if match:
3159 error(filename, linenum, 'whitespace/operators', 3,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003160 'Missing spaces around >>')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003161
3162 # There shouldn't be space around unary operators
3163 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3164 if match:
3165 error(filename, linenum, 'whitespace/operators', 4,
3166 'Extra space for operator %s' % match.group(1))
3167
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003168
3169def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3170 """Checks for horizontal spacing around parentheses.
3171
3172 Args:
3173 filename: The name of the current file.
3174 clean_lines: A CleansedLines instance containing the file.
3175 linenum: The number of the line to check.
3176 error: The function to call with any errors found.
3177 """
3178 line = clean_lines.elided[linenum]
3179
3180 # No spaces after an if, while, switch, or for
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003181 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3182 if match:
3183 error(filename, linenum, 'whitespace/parens', 5,
3184 'Missing space before ( in %s' % match.group(1))
3185
3186 # For if/for/while/switch, the left and right parens should be
3187 # consistent about how many spaces are inside the parens, and
3188 # there should either be zero or one spaces inside the parens.
3189 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.com6317a9c2009-06-25 00:28:19 +00003190 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003191 match = Search(r'\b(if|for|while|switch)\s*'
3192 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3193 line)
3194 if match:
3195 if len(match.group(2)) != len(match.group(4)):
3196 if not (match.group(3) == ';' and
erg@google.com6317a9c2009-06-25 00:28:19 +00003197 len(match.group(2)) == 1 + len(match.group(4)) or
3198 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003199 error(filename, linenum, 'whitespace/parens', 5,
3200 'Mismatching spaces inside () in %s' % match.group(1))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003201 if len(match.group(2)) not in [0, 1]:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003202 error(filename, linenum, 'whitespace/parens', 5,
3203 'Should have zero or one spaces inside ( and ) in %s' %
3204 match.group(1))
3205
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003206
3207def CheckCommaSpacing(filename, clean_lines, linenum, error):
3208 """Checks for horizontal spacing near commas and semicolons.
3209
3210 Args:
3211 filename: The name of the current file.
3212 clean_lines: A CleansedLines instance containing the file.
3213 linenum: The number of the line to check.
3214 error: The function to call with any errors found.
3215 """
3216 raw = clean_lines.lines_without_raw_strings
3217 line = clean_lines.elided[linenum]
3218
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003219 # You should always have a space after a comma (either as fn arg or operator)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003220 #
3221 # This does not apply when the non-space character following the
3222 # comma is another comma, since the only time when that happens is
3223 # for empty macro arguments.
3224 #
3225 # We run this check in two passes: first pass on elided lines to
3226 # verify that lines contain missing whitespaces, second pass on raw
3227 # lines to confirm that those missing whitespaces are not due to
3228 # elided comments.
avakulenko@google.com59146752014-08-11 20:20:55 +00003229 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3230 Search(r',[^,\s]', raw[linenum])):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003231 error(filename, linenum, 'whitespace/comma', 3,
3232 'Missing space after ,')
3233
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003234 # You should always have a space after a semicolon
3235 # except for few corner cases
3236 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3237 # space after ;
3238 if Search(r';[^\s};\\)/]', line):
3239 error(filename, linenum, 'whitespace/semicolon', 3,
3240 'Missing space after ;')
3241
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003242
3243def CheckBracesSpacing(filename, clean_lines, linenum, error):
3244 """Checks for horizontal spacing near commas.
3245
3246 Args:
3247 filename: The name of the current file.
3248 clean_lines: A CleansedLines instance containing the file.
3249 linenum: The number of the line to check.
3250 error: The function to call with any errors found.
3251 """
3252 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003253
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003254 # Except after an opening paren, or after another opening brace (in case of
3255 # an initializer list, for instance), you should have spaces before your
3256 # braces. And since you should never have braces at the beginning of a line,
3257 # this is an easy test.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003258 match = Match(r'^(.*[^ ({]){', line)
3259 if match:
3260 # Try a bit harder to check for brace initialization. This
3261 # happens in one of the following forms:
3262 # Constructor() : initializer_list_{} { ... }
3263 # Constructor{}.MemberFunction()
3264 # Type variable{};
3265 # FunctionCall(type{}, ...);
3266 # LastArgument(..., type{});
3267 # LOG(INFO) << type{} << " ...";
3268 # map_of_type[{...}] = ...;
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003269 # ternary = expr ? new type{} : nullptr;
3270 # OuterTemplate<InnerTemplateConstructor<Type>{}>
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003271 #
3272 # We check for the character following the closing brace, and
3273 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003274 # "{.;,)<>]:".
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003275 #
3276 # To account for nested initializer list, we allow any number of
3277 # closing braces up to "{;,)<". We can't simply silence the
3278 # warning on first sight of closing brace, because that would
3279 # cause false negatives for things that are not initializer lists.
3280 # Silence this: But not this:
3281 # Outer{ if (...) {
3282 # Inner{...} if (...){ // Missing space before {
3283 # }; }
3284 #
3285 # There is a false negative with this approach if people inserted
3286 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3287 # spurious semicolon with a separate check.
3288 (endline, endlinenum, endpos) = CloseExpression(
3289 clean_lines, linenum, len(match.group(1)))
3290 trailing_text = ''
3291 if endpos > -1:
3292 trailing_text = endline[endpos:]
3293 for offset in xrange(endlinenum + 1,
3294 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3295 trailing_text += clean_lines.elided[offset]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003296 if not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003297 error(filename, linenum, 'whitespace/braces', 5,
3298 'Missing space before {')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003299
3300 # Make sure '} else {' has spaces.
3301 if Search(r'}else', line):
3302 error(filename, linenum, 'whitespace/braces', 5,
3303 'Missing space before else')
3304
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003305 # You shouldn't have a space before a semicolon at the end of the line.
3306 # There's a special case for "for" since the style guide allows space before
3307 # the semicolon there.
3308 if Search(r':\s*;\s*$', line):
3309 error(filename, linenum, 'whitespace/semicolon', 5,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003310 'Semicolon defining empty statement. Use {} instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003311 elif Search(r'^\s*;\s*$', line):
3312 error(filename, linenum, 'whitespace/semicolon', 5,
3313 'Line contains only semicolon. If this should be an empty statement, '
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003314 'use {} instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003315 elif (Search(r'\s+;\s*$', line) and
3316 not Search(r'\bfor\b', line)):
3317 error(filename, linenum, 'whitespace/semicolon', 5,
3318 'Extra space before last semicolon. If this should be an empty '
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003319 'statement, use {} instead.')
3320
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003321
3322def IsDecltype(clean_lines, linenum, column):
3323 """Check if the token ending on (linenum, column) is decltype().
3324
3325 Args:
3326 clean_lines: A CleansedLines instance containing the file.
3327 linenum: the number of the line to check.
3328 column: end column of the token to check.
3329 Returns:
3330 True if this token is decltype() expression, False otherwise.
3331 """
3332 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3333 if start_col < 0:
3334 return False
3335 if Search(r'\bdecltype\s*$', text[0:start_col]):
3336 return True
3337 return False
3338
3339
3340def IsTemplateParameterList(clean_lines, linenum, column):
3341 """Check if the token ending on (linenum, column) is the end of template<>.
3342
3343 Args:
3344 clean_lines: A CleansedLines instance containing the file.
3345 linenum: the number of the line to check.
3346 column: end column of the token to check.
3347 Returns:
3348 True if this token is end of a template parameter list, False otherwise.
3349 """
3350 (_, startline, startpos) = ReverseCloseExpression(
3351 clean_lines, linenum, column)
3352 if (startpos > -1 and
3353 Search(r'\btemplate\s*$', clean_lines.elided[startline][0:startpos])):
3354 return True
3355 return False
3356
3357
3358def IsRValueType(clean_lines, nesting_state, linenum, column):
3359 """Check if the token ending on (linenum, column) is a type.
3360
3361 Assumes that text to the right of the column is "&&" or a function
3362 name.
3363
3364 Args:
3365 clean_lines: A CleansedLines instance containing the file.
3366 nesting_state: A NestingState instance which maintains information about
3367 the current stack of nested blocks being parsed.
3368 linenum: the number of the line to check.
3369 column: end column of the token to check.
3370 Returns:
3371 True if this token is a type, False if we are not sure.
3372 """
3373 prefix = clean_lines.elided[linenum][0:column]
3374
3375 # Get one word to the left. If we failed to do so, this is most
3376 # likely not a type, since it's unlikely that the type name and "&&"
3377 # would be split across multiple lines.
3378 match = Match(r'^(.*)(\b\w+|[>*)&])\s*$', prefix)
3379 if not match:
3380 return False
3381
3382 # Check text following the token. If it's "&&>" or "&&," or "&&...", it's
3383 # most likely a rvalue reference used inside a template.
3384 suffix = clean_lines.elided[linenum][column:]
3385 if Match(r'&&\s*(?:[>,]|\.\.\.)', suffix):
3386 return True
3387
3388 # Check for simple type and end of templates:
3389 # int&& variable
3390 # vector<int>&& variable
3391 #
3392 # Because this function is called recursively, we also need to
3393 # recognize pointer and reference types:
3394 # int* Function()
3395 # int& Function()
3396 if match.group(2) in ['char', 'char16_t', 'char32_t', 'wchar_t', 'bool',
3397 'short', 'int', 'long', 'signed', 'unsigned',
3398 'float', 'double', 'void', 'auto', '>', '*', '&']:
3399 return True
3400
3401 # If we see a close parenthesis, look for decltype on the other side.
3402 # decltype would unambiguously identify a type, anything else is
3403 # probably a parenthesized expression and not a type.
3404 if match.group(2) == ')':
3405 return IsDecltype(
3406 clean_lines, linenum, len(match.group(1)) + len(match.group(2)) - 1)
3407
3408 # Check for casts and cv-qualifiers.
3409 # match.group(1) remainder
3410 # -------------- ---------
3411 # const_cast< type&&
3412 # const type&&
3413 # type const&&
3414 if Search(r'\b(?:const_cast\s*<|static_cast\s*<|dynamic_cast\s*<|'
3415 r'reinterpret_cast\s*<|\w+\s)\s*$',
3416 match.group(1)):
3417 return True
3418
3419 # Look for a preceding symbol that might help differentiate the context.
3420 # These are the cases that would be ambiguous:
3421 # match.group(1) remainder
3422 # -------------- ---------
3423 # Call ( expression &&
3424 # Declaration ( type&&
3425 # sizeof ( type&&
3426 # if ( expression &&
3427 # while ( expression &&
3428 # for ( type&&
3429 # for( ; expression &&
3430 # statement ; type&&
3431 # block { type&&
3432 # constructor { expression &&
3433 start = linenum
3434 line = match.group(1)
3435 match_symbol = None
3436 while start >= 0:
3437 # We want to skip over identifiers and commas to get to a symbol.
3438 # Commas are skipped so that we can find the opening parenthesis
3439 # for function parameter lists.
3440 match_symbol = Match(r'^(.*)([^\w\s,])[\w\s,]*$', line)
3441 if match_symbol:
3442 break
3443 start -= 1
3444 line = clean_lines.elided[start]
3445
3446 if not match_symbol:
3447 # Probably the first statement in the file is an rvalue reference
3448 return True
3449
3450 if match_symbol.group(2) == '}':
3451 # Found closing brace, probably an indicate of this:
3452 # block{} type&&
3453 return True
3454
3455 if match_symbol.group(2) == ';':
3456 # Found semicolon, probably one of these:
3457 # for(; expression &&
3458 # statement; type&&
3459
3460 # Look for the previous 'for(' in the previous lines.
3461 before_text = match_symbol.group(1)
3462 for i in xrange(start - 1, max(start - 6, 0), -1):
3463 before_text = clean_lines.elided[i] + before_text
3464 if Search(r'for\s*\([^{};]*$', before_text):
3465 # This is the condition inside a for-loop
3466 return False
3467
3468 # Did not find a for-init-statement before this semicolon, so this
3469 # is probably a new statement and not a condition.
3470 return True
3471
3472 if match_symbol.group(2) == '{':
3473 # Found opening brace, probably one of these:
3474 # block{ type&& = ... ; }
3475 # constructor{ expression && expression }
3476
3477 # Look for a closing brace or a semicolon. If we see a semicolon
3478 # first, this is probably a rvalue reference.
3479 line = clean_lines.elided[start][0:len(match_symbol.group(1)) + 1]
3480 end = start
3481 depth = 1
3482 while True:
3483 for ch in line:
3484 if ch == ';':
3485 return True
3486 elif ch == '{':
3487 depth += 1
3488 elif ch == '}':
3489 depth -= 1
3490 if depth == 0:
3491 return False
3492 end += 1
3493 if end >= clean_lines.NumLines():
3494 break
3495 line = clean_lines.elided[end]
3496 # Incomplete program?
3497 return False
3498
3499 if match_symbol.group(2) == '(':
3500 # Opening parenthesis. Need to check what's to the left of the
3501 # parenthesis. Look back one extra line for additional context.
3502 before_text = match_symbol.group(1)
3503 if linenum > 1:
3504 before_text = clean_lines.elided[linenum - 1] + before_text
3505 before_text = match_symbol.group(1)
3506
3507 # Patterns that are likely to be types:
3508 # [](type&&
3509 # for (type&&
3510 # sizeof(type&&
3511 # operator=(type&&
3512 #
3513 if Search(r'(?:\]|\bfor|\bsizeof|\boperator\s*\S+\s*)\s*$', before_text):
3514 return True
3515
3516 # Patterns that are likely to be expressions:
3517 # if (expression &&
3518 # while (expression &&
3519 # : initializer(expression &&
3520 # , initializer(expression &&
3521 # ( FunctionCall(expression &&
3522 # + FunctionCall(expression &&
3523 # + (expression &&
3524 #
3525 # The last '+' represents operators such as '+' and '-'.
3526 if Search(r'(?:\bif|\bwhile|[-+=%^(<!?:,&*]\s*)$', before_text):
3527 return False
3528
3529 # Something else. Check that tokens to the left look like
3530 # return_type function_name
3531 match_func = Match(r'^(.*)\s+\w(?:\w|::)*(?:<[^<>]*>)?\s*$',
3532 match_symbol.group(1))
3533 if match_func:
3534 # Check for constructors, which don't have return types.
avakulenko@google.com59146752014-08-11 20:20:55 +00003535 if Search(r'\b(?:explicit|inline)$', match_func.group(1)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003536 return True
3537 implicit_constructor = Match(r'\s*(\w+)\((?:const\s+)?(\w+)', prefix)
3538 if (implicit_constructor and
3539 implicit_constructor.group(1) == implicit_constructor.group(2)):
3540 return True
3541 return IsRValueType(clean_lines, nesting_state, linenum,
3542 len(match_func.group(1)))
3543
3544 # Nothing before the function name. If this is inside a block scope,
3545 # this is probably a function call.
3546 return not (nesting_state.previous_stack_top and
3547 nesting_state.previous_stack_top.IsBlockInfo())
3548
3549 if match_symbol.group(2) == '>':
3550 # Possibly a closing bracket, check that what's on the other side
3551 # looks like the start of a template.
3552 return IsTemplateParameterList(
3553 clean_lines, start, len(match_symbol.group(1)))
3554
3555 # Some other symbol, usually something like "a=b&&c". This is most
3556 # likely not a type.
3557 return False
3558
3559
avakulenko@google.com59146752014-08-11 20:20:55 +00003560def IsDeletedOrDefault(clean_lines, linenum):
3561 """Check if current constructor or operator is deleted or default.
3562
3563 Args:
3564 clean_lines: A CleansedLines instance containing the file.
3565 linenum: The number of the line to check.
3566 Returns:
3567 True if this is a deleted or default constructor.
3568 """
3569 open_paren = clean_lines.elided[linenum].find('(')
3570 if open_paren < 0:
3571 return False
3572 (close_line, _, close_paren) = CloseExpression(
3573 clean_lines, linenum, open_paren)
3574 if close_paren < 0:
3575 return False
3576 return Match(r'\s*=\s*(?:delete|default)\b', close_line[close_paren:])
3577
3578
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003579def IsRValueAllowed(clean_lines, linenum):
avakulenko@google.com59146752014-08-11 20:20:55 +00003580 """Check if RValue reference is allowed on a particular line.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003581
3582 Args:
3583 clean_lines: A CleansedLines instance containing the file.
3584 linenum: The number of the line to check.
3585 Returns:
3586 True if line is within the region where RValue references are allowed.
3587 """
avakulenko@google.com59146752014-08-11 20:20:55 +00003588 # Allow region marked by PUSH/POP macros
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003589 for i in xrange(linenum, 0, -1):
3590 line = clean_lines.elided[i]
3591 if Match(r'GOOGLE_ALLOW_RVALUE_REFERENCES_(?:PUSH|POP)', line):
3592 if not line.endswith('PUSH'):
3593 return False
3594 for j in xrange(linenum, clean_lines.NumLines(), 1):
3595 line = clean_lines.elided[j]
3596 if Match(r'GOOGLE_ALLOW_RVALUE_REFERENCES_(?:PUSH|POP)', line):
3597 return line.endswith('POP')
avakulenko@google.com59146752014-08-11 20:20:55 +00003598
3599 # Allow operator=
3600 line = clean_lines.elided[linenum]
3601 if Search(r'\boperator\s*=\s*\(', line):
3602 return IsDeletedOrDefault(clean_lines, linenum)
3603
3604 # Allow constructors
3605 match = Match(r'\s*([\w<>]+)\s*::\s*([\w<>]+)\s*\(', line)
3606 if match and match.group(1) == match.group(2):
3607 return IsDeletedOrDefault(clean_lines, linenum)
3608 if Search(r'\b(?:explicit|inline)\s+[\w<>]+\s*\(', line):
3609 return IsDeletedOrDefault(clean_lines, linenum)
3610
3611 if Match(r'\s*[\w<>]+\s*\(', line):
3612 previous_line = 'ReturnType'
3613 if linenum > 0:
3614 previous_line = clean_lines.elided[linenum - 1]
3615 if Match(r'^\s*$', previous_line) or Search(r'[{}:;]\s*$', previous_line):
3616 return IsDeletedOrDefault(clean_lines, linenum)
3617
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003618 return False
3619
3620
3621def CheckRValueReference(filename, clean_lines, linenum, nesting_state, error):
3622 """Check for rvalue references.
3623
3624 Args:
3625 filename: The name of the current file.
3626 clean_lines: A CleansedLines instance containing the file.
3627 linenum: The number of the line to check.
3628 nesting_state: A NestingState instance which maintains information about
3629 the current stack of nested blocks being parsed.
3630 error: The function to call with any errors found.
3631 """
3632 # Find lines missing spaces around &&.
3633 # TODO(unknown): currently we don't check for rvalue references
3634 # with spaces surrounding the && to avoid false positives with
3635 # boolean expressions.
3636 line = clean_lines.elided[linenum]
3637 match = Match(r'^(.*\S)&&', line)
3638 if not match:
3639 match = Match(r'(.*)&&\S', line)
3640 if (not match) or '(&&)' in line or Search(r'\boperator\s*$', match.group(1)):
3641 return
3642
3643 # Either poorly formed && or an rvalue reference, check the context
3644 # to get a more accurate error message. Mostly we want to determine
3645 # if what's to the left of "&&" is a type or not.
3646 and_pos = len(match.group(1))
3647 if IsRValueType(clean_lines, nesting_state, linenum, and_pos):
3648 if not IsRValueAllowed(clean_lines, linenum):
3649 error(filename, linenum, 'build/c++11', 3,
3650 'RValue references are an unapproved C++ feature.')
3651 else:
3652 error(filename, linenum, 'whitespace/operators', 3,
3653 'Missing spaces around &&')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003654
3655
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003656def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3657 """Checks for additional blank line issues related to sections.
3658
3659 Currently the only thing checked here is blank line before protected/private.
3660
3661 Args:
3662 filename: The name of the current file.
3663 clean_lines: A CleansedLines instance containing the file.
3664 class_info: A _ClassInfo objects.
3665 linenum: The number of the line to check.
3666 error: The function to call with any errors found.
3667 """
3668 # Skip checks if the class is small, where small means 25 lines or less.
3669 # 25 lines seems like a good cutoff since that's the usual height of
3670 # terminals, and any class that can't fit in one screen can't really
3671 # be considered "small".
3672 #
3673 # Also skip checks if we are on the first line. This accounts for
3674 # classes that look like
3675 # class Foo { public: ... };
3676 #
3677 # If we didn't find the end of the class, last_line would be zero,
3678 # and the check will be skipped by the first condition.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003679 if (class_info.last_line - class_info.starting_linenum <= 24 or
3680 linenum <= class_info.starting_linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003681 return
3682
3683 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3684 if matched:
3685 # Issue warning if the line before public/protected/private was
3686 # not a blank line, but don't do this if the previous line contains
3687 # "class" or "struct". This can happen two ways:
3688 # - We are at the beginning of the class.
3689 # - We are forward-declaring an inner class that is semantically
3690 # private, but needed to be public for implementation reasons.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003691 # Also ignores cases where the previous line ends with a backslash as can be
3692 # common when defining classes in C macros.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003693 prev_line = clean_lines.lines[linenum - 1]
3694 if (not IsBlankLine(prev_line) and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003695 not Search(r'\b(class|struct)\b', prev_line) and
3696 not Search(r'\\$', prev_line)):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003697 # Try a bit harder to find the beginning of the class. This is to
3698 # account for multi-line base-specifier lists, e.g.:
3699 # class Derived
3700 # : public Base {
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003701 end_class_head = class_info.starting_linenum
3702 for i in range(class_info.starting_linenum, linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003703 if Search(r'\{\s*$', clean_lines.lines[i]):
3704 end_class_head = i
3705 break
3706 if end_class_head < linenum - 1:
3707 error(filename, linenum, 'whitespace/blank_line', 3,
3708 '"%s:" should be preceded by a blank line' % matched.group(1))
3709
3710
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003711def GetPreviousNonBlankLine(clean_lines, linenum):
3712 """Return the most recent non-blank line and its line number.
3713
3714 Args:
3715 clean_lines: A CleansedLines instance containing the file contents.
3716 linenum: The number of the line to check.
3717
3718 Returns:
3719 A tuple with two elements. The first element is the contents of the last
3720 non-blank line before the current line, or the empty string if this is the
3721 first non-blank line. The second is the line number of that line, or -1
3722 if this is the first non-blank line.
3723 """
3724
3725 prevlinenum = linenum - 1
3726 while prevlinenum >= 0:
3727 prevline = clean_lines.elided[prevlinenum]
3728 if not IsBlankLine(prevline): # if not a blank line...
3729 return (prevline, prevlinenum)
3730 prevlinenum -= 1
3731 return ('', -1)
3732
3733
3734def CheckBraces(filename, clean_lines, linenum, error):
3735 """Looks for misplaced braces (e.g. at the end of line).
3736
3737 Args:
3738 filename: The name of the current file.
3739 clean_lines: A CleansedLines instance containing the file.
3740 linenum: The number of the line to check.
3741 error: The function to call with any errors found.
3742 """
3743
3744 line = clean_lines.elided[linenum] # get rid of comments and strings
3745
3746 if Match(r'\s*{\s*$', line):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003747 # We allow an open brace to start a line in the case where someone is using
3748 # braces in a block to explicitly create a new scope, which is commonly used
3749 # to control the lifetime of stack-allocated variables. Braces are also
3750 # used for brace initializers inside function calls. We don't detect this
3751 # perfectly: we just don't complain if the last non-whitespace character on
3752 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
3753 # previous line starts a preprocessor block.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003754 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003755 if (not Search(r'[,;:}{(]\s*$', prevline) and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003756 not Match(r'\s*#', prevline)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003757 error(filename, linenum, 'whitespace/braces', 4,
3758 '{ should almost always be at the end of the previous line')
3759
3760 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003761 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003762 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3763 if Match(r'\s*}\s*$', prevline):
3764 error(filename, linenum, 'whitespace/newline', 4,
3765 'An else should appear on the same line as the preceding }')
3766
3767 # If braces come on one side of an else, they should be on both.
3768 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003769 if Search(r'else if\s*\(', line): # could be multi-line if
3770 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3771 # find the ( after the if
3772 pos = line.find('else if')
3773 pos = line.find('(', pos)
3774 if pos > 0:
3775 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3776 brace_on_right = endline[endpos:].find('{') != -1
3777 if brace_on_left != brace_on_right: # must be brace after if
3778 error(filename, linenum, 'readability/braces', 5,
3779 'If an else has a brace on one side, it should have it on both')
3780 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3781 error(filename, linenum, 'readability/braces', 5,
3782 'If an else has a brace on one side, it should have it on both')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003783
3784 # Likewise, an else should never have the else clause on the same line
3785 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3786 error(filename, linenum, 'whitespace/newline', 4,
3787 'Else clause should never be on same line as else (use 2 lines)')
3788
3789 # In the same way, a do/while should never be on one line
3790 if Match(r'\s*do [^\s{]', line):
3791 error(filename, linenum, 'whitespace/newline', 4,
3792 'do/while clauses should not be on a single line')
3793
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003794 # Check single-line if/else bodies. The style guide says 'curly braces are not
3795 # required for single-line statements'. We additionally allow multi-line,
3796 # single statements, but we reject anything with more than one semicolon in
3797 # it. This means that the first semicolon after the if should be at the end of
3798 # its line, and the line after that should have an indent level equal to or
3799 # lower than the if. We also check for ambiguous if/else nesting without
3800 # braces.
3801 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3802 if if_else_match and not Match(r'\s*#', line):
3803 if_indent = GetIndentLevel(line)
3804 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3805 if_match = Search(r'\bif\s*\(', line)
3806 if if_match:
3807 # This could be a multiline if condition, so find the end first.
3808 pos = if_match.end() - 1
3809 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3810 # Check for an opening brace, either directly after the if or on the next
3811 # line. If found, this isn't a single-statement conditional.
3812 if (not Match(r'\s*{', endline[endpos:])
3813 and not (Match(r'\s*$', endline[endpos:])
3814 and endlinenum < (len(clean_lines.elided) - 1)
3815 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3816 while (endlinenum < len(clean_lines.elided)
3817 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3818 endlinenum += 1
3819 endpos = 0
3820 if endlinenum < len(clean_lines.elided):
3821 endline = clean_lines.elided[endlinenum]
3822 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3823 # methods) and a single \ after the semicolon (for macros)
3824 endpos = endline.find(';')
3825 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.com59146752014-08-11 20:20:55 +00003826 # Semicolon isn't the last character, there's something trailing.
3827 # Output a warning if the semicolon is not contained inside
3828 # a lambda expression.
3829 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3830 endline):
3831 error(filename, linenum, 'readability/braces', 4,
3832 'If/else bodies with multiple statements require braces')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003833 elif endlinenum < len(clean_lines.elided) - 1:
3834 # Make sure the next line is dedented
3835 next_line = clean_lines.elided[endlinenum + 1]
3836 next_indent = GetIndentLevel(next_line)
3837 # With ambiguous nested if statements, this will error out on the
3838 # if that *doesn't* match the else, regardless of whether it's the
3839 # inner one or outer one.
3840 if (if_match and Match(r'\s*else\b', next_line)
3841 and next_indent != if_indent):
3842 error(filename, linenum, 'readability/braces', 4,
3843 'Else clause should be indented at the same level as if. '
3844 'Ambiguous nested if/else chains require braces.')
3845 elif next_indent > if_indent:
3846 error(filename, linenum, 'readability/braces', 4,
3847 'If/else bodies with multiple statements require braces')
3848
3849
3850def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3851 """Looks for redundant trailing semicolon.
3852
3853 Args:
3854 filename: The name of the current file.
3855 clean_lines: A CleansedLines instance containing the file.
3856 linenum: The number of the line to check.
3857 error: The function to call with any errors found.
3858 """
3859
3860 line = clean_lines.elided[linenum]
3861
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003862 # Block bodies should not be followed by a semicolon. Due to C++11
3863 # brace initialization, there are more places where semicolons are
3864 # required than not, so we use a whitelist approach to check these
3865 # rather than a blacklist. These are the places where "};" should
3866 # be replaced by just "}":
3867 # 1. Some flavor of block following closing parenthesis:
3868 # for (;;) {};
3869 # while (...) {};
3870 # switch (...) {};
3871 # Function(...) {};
3872 # if (...) {};
3873 # if (...) else if (...) {};
3874 #
3875 # 2. else block:
3876 # if (...) else {};
3877 #
3878 # 3. const member function:
3879 # Function(...) const {};
3880 #
3881 # 4. Block following some statement:
3882 # x = 42;
3883 # {};
3884 #
3885 # 5. Block at the beginning of a function:
3886 # Function(...) {
3887 # {};
3888 # }
3889 #
3890 # Note that naively checking for the preceding "{" will also match
3891 # braces inside multi-dimensional arrays, but this is fine since
3892 # that expression will not contain semicolons.
3893 #
3894 # 6. Block following another block:
3895 # while (true) {}
3896 # {};
3897 #
3898 # 7. End of namespaces:
3899 # namespace {};
3900 #
3901 # These semicolons seems far more common than other kinds of
3902 # redundant semicolons, possibly due to people converting classes
3903 # to namespaces. For now we do not warn for this case.
3904 #
3905 # Try matching case 1 first.
3906 match = Match(r'^(.*\)\s*)\{', line)
3907 if match:
3908 # Matched closing parenthesis (case 1). Check the token before the
3909 # matching opening parenthesis, and don't warn if it looks like a
3910 # macro. This avoids these false positives:
3911 # - macro that defines a base class
3912 # - multi-line macro that defines a base class
3913 # - macro that defines the whole class-head
3914 #
3915 # But we still issue warnings for macros that we know are safe to
3916 # warn, specifically:
3917 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3918 # - TYPED_TEST
3919 # - INTERFACE_DEF
3920 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3921 #
3922 # We implement a whitelist of safe macros instead of a blacklist of
3923 # unsafe macros, even though the latter appears less frequently in
3924 # google code and would have been easier to implement. This is because
3925 # the downside for getting the whitelist wrong means some extra
3926 # semicolons, while the downside for getting the blacklist wrong
3927 # would result in compile errors.
3928 #
3929 # In addition to macros, we also don't want to warn on compound
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003930 # literals and lambdas.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003931 closing_brace_pos = match.group(1).rfind(')')
3932 opening_parenthesis = ReverseCloseExpression(
3933 clean_lines, linenum, closing_brace_pos)
3934 if opening_parenthesis[2] > -1:
3935 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
3936 macro = Search(r'\b([A-Z_]+)\s*$', line_prefix)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003937 func = Match(r'^(.*\])\s*$', line_prefix)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003938 if ((macro and
3939 macro.group(1) not in (
3940 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3941 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3942 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003943 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003944 Search(r'\s+=\s*$', line_prefix)):
3945 match = None
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003946 if (match and
3947 opening_parenthesis[1] > 1 and
3948 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3949 # Multi-line lambda-expression
3950 match = None
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003951
3952 else:
3953 # Try matching cases 2-3.
3954 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3955 if not match:
3956 # Try matching cases 4-6. These are always matched on separate lines.
3957 #
3958 # Note that we can't simply concatenate the previous line to the
3959 # current line and do a single match, otherwise we may output
3960 # duplicate warnings for the blank line case:
3961 # if (cond) {
3962 # // blank line
3963 # }
3964 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3965 if prevline and Search(r'[;{}]\s*$', prevline):
3966 match = Match(r'^(\s*)\{', line)
3967
3968 # Check matching closing brace
3969 if match:
3970 (endline, endlinenum, endpos) = CloseExpression(
3971 clean_lines, linenum, len(match.group(1)))
3972 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3973 # Current {} pair is eligible for semicolon check, and we have found
3974 # the redundant semicolon, output warning here.
3975 #
3976 # Note: because we are scanning forward for opening braces, and
3977 # outputting warnings for the matching closing brace, if there are
3978 # nested blocks with trailing semicolons, we will get the error
3979 # messages in reversed order.
3980 error(filename, endlinenum, 'readability/braces', 4,
3981 "You don't need a ; after a }")
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003982
3983
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003984def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3985 """Look for empty loop/conditional body with only a single semicolon.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003986
3987 Args:
3988 filename: The name of the current file.
3989 clean_lines: A CleansedLines instance containing the file.
3990 linenum: The number of the line to check.
3991 error: The function to call with any errors found.
3992 """
3993
3994 # Search for loop keywords at the beginning of the line. Because only
3995 # whitespaces are allowed before the keywords, this will also ignore most
3996 # do-while-loops, since those lines should start with closing brace.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003997 #
3998 # We also check "if" blocks here, since an empty conditional block
3999 # is likely an error.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004000 line = clean_lines.elided[linenum]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004001 matched = Match(r'\s*(for|while|if)\s*\(', line)
4002 if matched:
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004003 # Find the end of the conditional expression
4004 (end_line, end_linenum, end_pos) = CloseExpression(
4005 clean_lines, linenum, line.find('('))
4006
4007 # Output warning if what follows the condition expression is a semicolon.
4008 # No warning for all other cases, including whitespace or newline, since we
4009 # have a separate check for semicolons preceded by whitespace.
4010 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004011 if matched.group(1) == 'if':
4012 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
4013 'Empty conditional bodies should use {}')
4014 else:
4015 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
4016 'Empty loop bodies should use {} or continue')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004017
4018
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004019def FindCheckMacro(line):
4020 """Find a replaceable CHECK-like macro.
4021
4022 Args:
4023 line: line to search on.
4024 Returns:
4025 (macro name, start position), or (None, -1) if no replaceable
4026 macro is found.
4027 """
4028 for macro in _CHECK_MACROS:
4029 i = line.find(macro)
4030 if i >= 0:
4031 # Find opening parenthesis. Do a regular expression match here
4032 # to make sure that we are matching the expected CHECK macro, as
4033 # opposed to some other macro that happens to contain the CHECK
4034 # substring.
4035 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4036 if not matched:
4037 continue
4038 return (macro, len(matched.group(1)))
4039 return (None, -1)
4040
4041
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004042def CheckCheck(filename, clean_lines, linenum, error):
4043 """Checks the use of CHECK and EXPECT macros.
4044
4045 Args:
4046 filename: The name of the current file.
4047 clean_lines: A CleansedLines instance containing the file.
4048 linenum: The number of the line to check.
4049 error: The function to call with any errors found.
4050 """
4051
4052 # Decide the set of replacement macros that should be suggested
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004053 lines = clean_lines.elided
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004054 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4055 if not check_macro:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004056 return
4057
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004058 # Find end of the boolean expression by matching parentheses
4059 (last_line, end_line, end_pos) = CloseExpression(
4060 clean_lines, linenum, start_pos)
4061 if end_pos < 0:
4062 return
avakulenko@google.com59146752014-08-11 20:20:55 +00004063
4064 # If the check macro is followed by something other than a
4065 # semicolon, assume users will log their own custom error messages
4066 # and don't suggest any replacements.
4067 if not Match(r'\s*;', last_line[end_pos:]):
4068 return
4069
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004070 if linenum == end_line:
4071 expression = lines[linenum][start_pos + 1:end_pos - 1]
4072 else:
4073 expression = lines[linenum][start_pos + 1:]
4074 for i in xrange(linenum + 1, end_line):
4075 expression += lines[i]
4076 expression += last_line[0:end_pos - 1]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004077
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004078 # Parse expression so that we can take parentheses into account.
4079 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4080 # which is not replaceable by CHECK_LE.
4081 lhs = ''
4082 rhs = ''
4083 operator = None
4084 while expression:
4085 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4086 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4087 if matched:
4088 token = matched.group(1)
4089 if token == '(':
4090 # Parenthesized operand
4091 expression = matched.group(2)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004092 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004093 if end < 0:
4094 return # Unmatched parenthesis
4095 lhs += '(' + expression[0:end]
4096 expression = expression[end:]
4097 elif token in ('&&', '||'):
4098 # Logical and/or operators. This means the expression
4099 # contains more than one term, for example:
4100 # CHECK(42 < a && a < b);
4101 #
4102 # These are not replaceable with CHECK_LE, so bail out early.
4103 return
4104 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4105 # Non-relational operator
4106 lhs += token
4107 expression = matched.group(2)
4108 else:
4109 # Relational operator
4110 operator = token
4111 rhs = matched.group(2)
4112 break
4113 else:
4114 # Unparenthesized operand. Instead of appending to lhs one character
4115 # at a time, we do another regular expression match to consume several
4116 # characters at once if possible. Trivial benchmark shows that this
4117 # is more efficient when the operands are longer than a single
4118 # character, which is generally the case.
4119 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4120 if not matched:
4121 matched = Match(r'^(\s*\S)(.*)$', expression)
4122 if not matched:
4123 break
4124 lhs += matched.group(1)
4125 expression = matched.group(2)
4126
4127 # Only apply checks if we got all parts of the boolean expression
4128 if not (lhs and operator and rhs):
4129 return
4130
4131 # Check that rhs do not contain logical operators. We already know
4132 # that lhs is fine since the loop above parses out && and ||.
4133 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4134 return
4135
4136 # At least one of the operands must be a constant literal. This is
4137 # to avoid suggesting replacements for unprintable things like
4138 # CHECK(variable != iterator)
4139 #
4140 # The following pattern matches decimal, hex integers, strings, and
4141 # characters (in that order).
4142 lhs = lhs.strip()
4143 rhs = rhs.strip()
4144 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4145 if Match(match_constant, lhs) or Match(match_constant, rhs):
4146 # Note: since we know both lhs and rhs, we can provide a more
4147 # descriptive error message like:
4148 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4149 # Instead of:
4150 # Consider using CHECK_EQ instead of CHECK(a == b)
4151 #
4152 # We are still keeping the less descriptive message because if lhs
4153 # or rhs gets long, the error message might become unreadable.
4154 error(filename, linenum, 'readability/check', 2,
4155 'Consider using %s instead of %s(a %s b)' % (
4156 _CHECK_REPLACEMENT[check_macro][operator],
4157 check_macro, operator))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004158
4159
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004160def CheckAltTokens(filename, clean_lines, linenum, error):
4161 """Check alternative keywords being used in boolean expressions.
4162
4163 Args:
4164 filename: The name of the current file.
4165 clean_lines: A CleansedLines instance containing the file.
4166 linenum: The number of the line to check.
4167 error: The function to call with any errors found.
4168 """
4169 line = clean_lines.elided[linenum]
4170
4171 # Avoid preprocessor lines
4172 if Match(r'^\s*#', line):
4173 return
4174
4175 # Last ditch effort to avoid multi-line comments. This will not help
4176 # if the comment started before the current line or ended after the
4177 # current line, but it catches most of the false positives. At least,
4178 # it provides a way to workaround this warning for people who use
4179 # multi-line comments in preprocessor macros.
4180 #
4181 # TODO(unknown): remove this once cpplint has better support for
4182 # multi-line comments.
4183 if line.find('/*') >= 0 or line.find('*/') >= 0:
4184 return
4185
4186 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4187 error(filename, linenum, 'readability/alt_tokens', 2,
4188 'Use operator %s instead of %s' % (
4189 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4190
4191
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004192def GetLineWidth(line):
4193 """Determines the width of the line in column positions.
4194
4195 Args:
4196 line: A string, which may be a Unicode string.
4197
4198 Returns:
4199 The width of the line in column positions, accounting for Unicode
4200 combining characters and wide characters.
4201 """
4202 if isinstance(line, unicode):
4203 width = 0
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004204 for uc in unicodedata.normalize('NFC', line):
4205 if unicodedata.east_asian_width(uc) in ('W', 'F'):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004206 width += 2
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004207 elif not unicodedata.combining(uc):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004208 width += 1
4209 return width
4210 else:
4211 return len(line)
4212
4213
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004214def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004215 error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004216 """Checks rules from the 'C++ style rules' section of cppguide.html.
4217
4218 Most of these rules are hard to test (naming, comment style), but we
4219 do what we can. In particular we check for 2-space indents, line lengths,
4220 tab usage, spaces inside code, etc.
4221
4222 Args:
4223 filename: The name of the current file.
4224 clean_lines: A CleansedLines instance containing the file.
4225 linenum: The number of the line to check.
4226 file_extension: The extension (without the dot) of the filename.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004227 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004228 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004229 error: The function to call with any errors found.
4230 """
4231
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004232 # Don't use "elided" lines here, otherwise we can't check commented lines.
4233 # Don't want to use "raw" either, because we don't want to check inside C++11
4234 # raw strings,
4235 raw_lines = clean_lines.lines_without_raw_strings
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004236 line = raw_lines[linenum]
4237
4238 if line.find('\t') != -1:
4239 error(filename, linenum, 'whitespace/tab', 1,
4240 'Tab found; better to use spaces')
4241
4242 # One or three blank spaces at the beginning of the line is weird; it's
4243 # hard to reconcile that with 2-space indents.
4244 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4245 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4246 # if(RLENGTH > 20) complain = 0;
4247 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4248 # if(match(prev, "&& *$")) complain = 0;
4249 # if(match(prev, "\\|\\| *$")) complain = 0;
4250 # if(match(prev, "[\",=><] *$")) complain = 0;
4251 # if(match($0, " <<")) complain = 0;
4252 # if(match(prev, " +for \\(")) complain = 0;
4253 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004254 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4255 classinfo = nesting_state.InnermostClass()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004256 initial_spaces = 0
4257 cleansed_line = clean_lines.elided[linenum]
4258 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4259 initial_spaces += 1
4260 if line and line[-1].isspace():
4261 error(filename, linenum, 'whitespace/end_of_line', 4,
4262 'Line ends in whitespace. Consider deleting these extra spaces.')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004263 # There are certain situations we allow one space, notably for
4264 # section labels, and also lines containing multi-line raw strings.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004265 elif ((initial_spaces == 1 or initial_spaces == 3) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004266 not Match(scope_or_label_pattern, cleansed_line) and
4267 not (clean_lines.raw_lines[linenum] != line and
4268 Match(r'^\s*""', line))):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004269 error(filename, linenum, 'whitespace/indent', 3,
4270 'Weird number of spaces at line-start. '
4271 'Are you using a 2-space indent?')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004272
4273 # Check if the line is a header guard.
4274 is_header_guard = False
4275 if file_extension == 'h':
4276 cppvar = GetHeaderGuardCPPVariable(filename)
4277 if (line.startswith('#ifndef %s' % cppvar) or
4278 line.startswith('#define %s' % cppvar) or
4279 line.startswith('#endif // %s' % cppvar)):
4280 is_header_guard = True
4281 # #include lines and header guards can be long, since there's no clean way to
4282 # split them.
erg@google.com6317a9c2009-06-25 00:28:19 +00004283 #
4284 # URLs can be long too. It's possible to split these, but it makes them
4285 # harder to cut&paste.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004286 #
4287 # The "$Id:...$" comment may also get very long without it being the
4288 # developers fault.
erg@google.com6317a9c2009-06-25 00:28:19 +00004289 if (not line.startswith('#include') and not is_header_guard and
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004290 not Match(r'^\s*//.*http(s?)://\S*$', line) and
4291 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004292 line_width = GetLineWidth(line)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004293 extended_length = int((_line_length * 1.25))
4294 if line_width > extended_length:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004295 error(filename, linenum, 'whitespace/line_length', 4,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004296 'Lines should very rarely be longer than %i characters' %
4297 extended_length)
4298 elif line_width > _line_length:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004299 error(filename, linenum, 'whitespace/line_length', 2,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004300 'Lines should be <= %i characters long' % _line_length)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004301
4302 if (cleansed_line.count(';') > 1 and
4303 # for loops are allowed two ;'s (and may run over two lines).
4304 cleansed_line.find('for') == -1 and
4305 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4306 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4307 # It's ok to have many commands in a switch case that fits in 1 line
4308 not ((cleansed_line.find('case ') != -1 or
4309 cleansed_line.find('default:') != -1) and
4310 cleansed_line.find('break;') != -1)):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004311 error(filename, linenum, 'whitespace/newline', 0,
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004312 'More than one command on the same line')
4313
4314 # Some more style checks
4315 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004316 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004317 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004318 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
4319 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004320 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4321 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4322 CheckCommaSpacing(filename, clean_lines, linenum, error)
4323 CheckBracesSpacing(filename, clean_lines, linenum, error)
4324 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
4325 CheckRValueReference(filename, clean_lines, linenum, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004326 CheckCheck(filename, clean_lines, linenum, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004327 CheckAltTokens(filename, clean_lines, linenum, error)
4328 classinfo = nesting_state.InnermostClass()
4329 if classinfo:
4330 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004331
4332
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004333_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4334# Matches the first component of a filename delimited by -s and _s. That is:
4335# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4336# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4337# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4338# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4339_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4340
4341
4342def _DropCommonSuffixes(filename):
4343 """Drops common suffixes like _test.cc or -inl.h from filename.
4344
4345 For example:
4346 >>> _DropCommonSuffixes('foo/foo-inl.h')
4347 'foo/foo'
4348 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4349 'foo/bar/foo'
4350 >>> _DropCommonSuffixes('foo/foo_internal.h')
4351 'foo/foo'
4352 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4353 'foo/foo_unusualinternal'
4354
4355 Args:
4356 filename: The input filename.
4357
4358 Returns:
4359 The filename with the common suffix removed.
4360 """
4361 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4362 'inl.h', 'impl.h', 'internal.h'):
4363 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4364 filename[-len(suffix) - 1] in ('-', '_')):
4365 return filename[:-len(suffix) - 1]
4366 return os.path.splitext(filename)[0]
4367
4368
4369def _IsTestFilename(filename):
4370 """Determines if the given filename has a suffix that identifies it as a test.
4371
4372 Args:
4373 filename: The input filename.
4374
4375 Returns:
4376 True if 'filename' looks like a test, False otherwise.
4377 """
4378 if (filename.endswith('_test.cc') or
4379 filename.endswith('_unittest.cc') or
4380 filename.endswith('_regtest.cc')):
4381 return True
4382 else:
4383 return False
4384
4385
4386def _ClassifyInclude(fileinfo, include, is_system):
4387 """Figures out what kind of header 'include' is.
4388
4389 Args:
4390 fileinfo: The current file cpplint is running over. A FileInfo instance.
4391 include: The path to a #included file.
4392 is_system: True if the #include used <> rather than "".
4393
4394 Returns:
4395 One of the _XXX_HEADER constants.
4396
4397 For example:
4398 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4399 _C_SYS_HEADER
4400 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4401 _CPP_SYS_HEADER
4402 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4403 _LIKELY_MY_HEADER
4404 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4405 ... 'bar/foo_other_ext.h', False)
4406 _POSSIBLE_MY_HEADER
4407 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4408 _OTHER_HEADER
4409 """
4410 # This is a list of all standard c++ header files, except
4411 # those already checked for above.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004412 is_cpp_h = include in _CPP_HEADERS
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004413
4414 if is_system:
4415 if is_cpp_h:
4416 return _CPP_SYS_HEADER
4417 else:
4418 return _C_SYS_HEADER
4419
4420 # If the target file and the include we're checking share a
4421 # basename when we drop common extensions, and the include
4422 # lives in . , then it's likely to be owned by the target file.
4423 target_dir, target_base = (
4424 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4425 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4426 if target_base == include_base and (
4427 include_dir == target_dir or
4428 include_dir == os.path.normpath(target_dir + '/../public')):
4429 return _LIKELY_MY_HEADER
4430
4431 # If the target and include share some initial basename
4432 # component, it's possible the target is implementing the
4433 # include, so it's allowed to be first, but we'll never
4434 # complain if it's not there.
4435 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4436 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4437 if (target_first_component and include_first_component and
4438 target_first_component.group(0) ==
4439 include_first_component.group(0)):
4440 return _POSSIBLE_MY_HEADER
4441
4442 return _OTHER_HEADER
4443
4444
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004445
erg@google.com6317a9c2009-06-25 00:28:19 +00004446def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4447 """Check rules that are applicable to #include lines.
4448
4449 Strings on #include lines are NOT removed from elided line, to make
4450 certain tasks easier. However, to prevent false positives, checks
4451 applicable to #include lines in CheckLanguage must be put here.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004452
4453 Args:
4454 filename: The name of the current file.
4455 clean_lines: A CleansedLines instance containing the file.
4456 linenum: The number of the line to check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004457 include_state: An _IncludeState instance in which the headers are inserted.
4458 error: The function to call with any errors found.
4459 """
4460 fileinfo = FileInfo(filename)
erg@google.com6317a9c2009-06-25 00:28:19 +00004461 line = clean_lines.lines[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004462
4463 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.com59146752014-08-11 20:20:55 +00004464 # Only do this check if the included header follows google naming
4465 # conventions. If not, assume that it's a 3rd party API that
4466 # requires special include conventions.
4467 #
4468 # We also make an exception for Lua headers, which follow google
4469 # naming convention but not the include convention.
4470 match = Match(r'#include\s*"([^/]+\.h)"', line)
4471 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004472 error(filename, linenum, 'build/include', 4,
4473 'Include the directory when naming .h files')
4474
4475 # we shouldn't include a file more than once. actually, there are a
4476 # handful of instances where doing so is okay, but in general it's
4477 # not.
erg@google.com6317a9c2009-06-25 00:28:19 +00004478 match = _RE_PATTERN_INCLUDE.search(line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004479 if match:
4480 include = match.group(2)
4481 is_system = (match.group(1) == '<')
avakulenko@google.com59146752014-08-11 20:20:55 +00004482 duplicate_line = include_state.FindHeader(include)
4483 if duplicate_line >= 0:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004484 error(filename, linenum, 'build/include', 4,
4485 '"%s" already included at %s:%s' %
avakulenko@google.com59146752014-08-11 20:20:55 +00004486 (include, filename, duplicate_line))
4487 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4488 include_state.include_list[-1].append((include, linenum))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004489
4490 # We want to ensure that headers appear in the right order:
4491 # 1) for foo.cc, foo.h (preferred location)
4492 # 2) c system files
4493 # 3) cpp system files
4494 # 4) for foo.cc, foo.h (deprecated location)
4495 # 5) other google headers
4496 #
4497 # We classify each include statement as one of those 5 types
4498 # using a number of techniques. The include_state object keeps
4499 # track of the highest type seen, and complains if we see a
4500 # lower type after that.
4501 error_message = include_state.CheckNextIncludeOrder(
4502 _ClassifyInclude(fileinfo, include, is_system))
4503 if error_message:
4504 error(filename, linenum, 'build/include_order', 4,
4505 '%s. Should be: %s.h, c system, c++ system, other.' %
4506 (error_message, fileinfo.BaseName()))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004507 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4508 if not include_state.IsInAlphabeticalOrder(
4509 clean_lines, linenum, canonical_include):
erg@google.com26970fa2009-11-17 18:07:32 +00004510 error(filename, linenum, 'build/include_alpha', 4,
4511 'Include "%s" not in alphabetical order' % include)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004512 include_state.SetLastHeader(canonical_include)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004513
erg@google.com6317a9c2009-06-25 00:28:19 +00004514 # Look for any of the stream classes that are part of standard C++.
4515 match = _RE_PATTERN_INCLUDE.match(line)
4516 if match:
4517 include = match.group(2)
4518 if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
4519 # Many unit tests use cout, so we exempt them.
4520 if not _IsTestFilename(filename):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004521 # Suggest a different header for ostream
4522 if include == 'ostream':
4523 error(filename, linenum, 'readability/streams', 3,
4524 'For logging, include "base/logging.h" instead of <ostream>.')
4525 else:
4526 error(filename, linenum, 'readability/streams', 3,
4527 'Streams are highly discouraged.')
erg@google.com6317a9c2009-06-25 00:28:19 +00004528
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004529
4530def _GetTextInside(text, start_pattern):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004531 r"""Retrieves all the text between matching open and close parentheses.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004532
4533 Given a string of lines and a regular expression string, retrieve all the text
4534 following the expression and between opening punctuation symbols like
4535 (, [, or {, and the matching close-punctuation symbol. This properly nested
4536 occurrences of the punctuations, so for the text like
4537 printf(a(), b(c()));
4538 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4539 start_pattern must match string having an open punctuation symbol at the end.
4540
4541 Args:
4542 text: The lines to extract text. Its comments and strings must be elided.
4543 It can be single line and can span multiple lines.
4544 start_pattern: The regexp string indicating where to start extracting
4545 the text.
4546 Returns:
4547 The extracted text.
4548 None if either the opening string or ending punctuation could not be found.
4549 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004550 # TODO(unknown): Audit cpplint.py to see what places could be profitably
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004551 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4552
4553 # Give opening punctuations to get the matching close-punctuations.
4554 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4555 closing_punctuation = set(matching_punctuation.itervalues())
4556
4557 # Find the position to start extracting text.
4558 match = re.search(start_pattern, text, re.M)
4559 if not match: # start_pattern not found in text.
4560 return None
4561 start_position = match.end(0)
4562
4563 assert start_position > 0, (
4564 'start_pattern must ends with an opening punctuation.')
4565 assert text[start_position - 1] in matching_punctuation, (
4566 'start_pattern must ends with an opening punctuation.')
4567 # Stack of closing punctuations we expect to have in text after position.
4568 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4569 position = start_position
4570 while punctuation_stack and position < len(text):
4571 if text[position] == punctuation_stack[-1]:
4572 punctuation_stack.pop()
4573 elif text[position] in closing_punctuation:
4574 # A closing punctuation without matching opening punctuations.
4575 return None
4576 elif text[position] in matching_punctuation:
4577 punctuation_stack.append(matching_punctuation[text[position]])
4578 position += 1
4579 if punctuation_stack:
4580 # Opening punctuations left without matching close-punctuations.
4581 return None
4582 # punctuations match.
4583 return text[start_position:position - 1]
4584
4585
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004586# Patterns for matching call-by-reference parameters.
4587#
4588# Supports nested templates up to 2 levels deep using this messy pattern:
4589# < (?: < (?: < [^<>]*
4590# >
4591# | [^<>] )*
4592# >
4593# | [^<>] )*
4594# >
4595_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4596_RE_PATTERN_TYPE = (
4597 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
4598 r'(?:\w|'
4599 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4600 r'::)+')
4601# A call-by-reference parameter ends with '& identifier'.
4602_RE_PATTERN_REF_PARAM = re.compile(
4603 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4604 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4605# A call-by-const-reference parameter either ends with 'const& identifier'
4606# or looks like 'const type& identifier' when 'type' is atomic.
4607_RE_PATTERN_CONST_REF_PARAM = (
4608 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4609 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
4610
4611
4612def CheckLanguage(filename, clean_lines, linenum, file_extension,
4613 include_state, nesting_state, error):
erg@google.com6317a9c2009-06-25 00:28:19 +00004614 """Checks rules from the 'C++ language rules' section of cppguide.html.
4615
4616 Some of these rules are hard to test (function overloading, using
4617 uint32 inappropriately), but we do the best we can.
4618
4619 Args:
4620 filename: The name of the current file.
4621 clean_lines: A CleansedLines instance containing the file.
4622 linenum: The number of the line to check.
4623 file_extension: The extension (without the dot) of the filename.
4624 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004625 nesting_state: A NestingState instance which maintains information about
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004626 the current stack of nested blocks being parsed.
erg@google.com6317a9c2009-06-25 00:28:19 +00004627 error: The function to call with any errors found.
4628 """
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004629 # If the line is empty or consists of entirely a comment, no need to
4630 # check it.
4631 line = clean_lines.elided[linenum]
4632 if not line:
4633 return
4634
erg@google.com6317a9c2009-06-25 00:28:19 +00004635 match = _RE_PATTERN_INCLUDE.search(line)
4636 if match:
4637 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4638 return
4639
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004640 # Reset include state across preprocessor directives. This is meant
4641 # to silence warnings for conditional includes.
avakulenko@google.com59146752014-08-11 20:20:55 +00004642 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4643 if match:
4644 include_state.ResetSection(match.group(1))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004645
4646 # Make Windows paths like Unix.
4647 fullname = os.path.abspath(filename).replace('\\', '/')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004648
4649 # Perform other checks now that we are sure that this is not an include line
4650 CheckCasts(filename, clean_lines, linenum, error)
4651 CheckGlobalStatic(filename, clean_lines, linenum, error)
4652 CheckPrintf(filename, clean_lines, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004653
4654 if file_extension == 'h':
4655 # TODO(unknown): check that 1-arg constructors are explicit.
4656 # How to tell it's a constructor?
4657 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.com59146752014-08-11 20:20:55 +00004658 # TODO(unknown): check that classes declare or disable copy/assign
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004659 # (level 1 error)
4660 pass
4661
4662 # Check if people are using the verboten C basic types. The only exception
4663 # we regularly allow is "unsigned short port" for port.
4664 if Search(r'\bshort port\b', line):
4665 if not Search(r'\bunsigned short port\b', line):
4666 error(filename, linenum, 'runtime/int', 4,
4667 'Use "unsigned short" for ports, not "short"')
4668 else:
4669 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4670 if match:
4671 error(filename, linenum, 'runtime/int', 4,
4672 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4673
erg@google.com26970fa2009-11-17 18:07:32 +00004674 # Check if some verboten operator overloading is going on
4675 # TODO(unknown): catch out-of-line unary operator&:
4676 # class X {};
4677 # int operator&(const X& x) { return 42; } // unary operator&
4678 # The trick is it's hard to tell apart from binary operator&:
4679 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4680 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4681 error(filename, linenum, 'runtime/operator', 4,
4682 'Unary operator& is dangerous. Do not use it.')
4683
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004684 # Check for suspicious usage of "if" like
4685 # } if (a == b) {
4686 if Search(r'\}\s*if\s*\(', line):
4687 error(filename, linenum, 'readability/braces', 4,
4688 'Did you mean "else if"? If not, start a new line for "if".')
4689
4690 # Check for potential format string bugs like printf(foo).
4691 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4692 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004693 # TODO(unknown): Catch the following case. Need to change the calling
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004694 # convention of the whole function to process multiple line to handle it.
4695 # printf(
4696 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4697 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4698 if printf_args:
4699 match = Match(r'([\w.\->()]+)$', printf_args)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004700 if match and match.group(1) != '__VA_ARGS__':
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004701 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4702 line, re.I).group(1)
4703 error(filename, linenum, 'runtime/printf', 4,
4704 'Potential format string bug. Do %s("%%s", %s) instead.'
4705 % (function_name, match.group(1)))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004706
4707 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4708 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4709 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4710 error(filename, linenum, 'runtime/memset', 4,
4711 'Did you mean "memset(%s, 0, %s)"?'
4712 % (match.group(1), match.group(2)))
4713
4714 if Search(r'\busing namespace\b', line):
4715 error(filename, linenum, 'build/namespaces', 5,
4716 'Do not use namespace using-directives. '
4717 'Use using-declarations instead.')
4718
4719 # Detect variable-length arrays.
4720 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4721 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4722 match.group(3).find(']') == -1):
4723 # Split the size using space and arithmetic operators as delimiters.
4724 # If any of the resulting tokens are not compile time constants then
4725 # report the error.
4726 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4727 is_const = True
4728 skip_next = False
4729 for tok in tokens:
4730 if skip_next:
4731 skip_next = False
4732 continue
4733
4734 if Search(r'sizeof\(.+\)', tok): continue
4735 if Search(r'arraysize\(\w+\)', tok): continue
4736
4737 tok = tok.lstrip('(')
4738 tok = tok.rstrip(')')
4739 if not tok: continue
4740 if Match(r'\d+', tok): continue
4741 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4742 if Match(r'k[A-Z0-9]\w*', tok): continue
4743 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4744 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4745 # A catch all for tricky sizeof cases, including 'sizeof expression',
4746 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004747 # requires skipping the next token because we split on ' ' and '*'.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004748 if tok.startswith('sizeof'):
4749 skip_next = True
4750 continue
4751 is_const = False
4752 break
4753 if not is_const:
4754 error(filename, linenum, 'runtime/arrays', 1,
4755 'Do not use variable-length arrays. Use an appropriately named '
4756 "('k' followed by CamelCase) compile-time constant for the size.")
4757
avakulenko@google.com59146752014-08-11 20:20:55 +00004758 # If DISALLOW_COPY_AND_ASSIGN DISALLOW_IMPLICIT_CONSTRUCTORS is present,
4759 # then it should be the last thing in the class declaration.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004760 match = Match(
4761 (r'\s*'
avakulenko@google.com59146752014-08-11 20:20:55 +00004762 r'(DISALLOW_(COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004763 r'\(.*\);$'),
4764 line)
4765 if match and linenum + 1 < clean_lines.NumLines():
4766 next_line = clean_lines.elided[linenum + 1]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004767 # We allow some, but not all, declarations of variables to be present
4768 # in the statement that defines the class. The [\w\*,\s]* fragment of
4769 # the regular expression below allows users to declare instances of
4770 # the class or pointers to instances, but not less common types such
4771 # as function pointers or arrays. It's a tradeoff between allowing
4772 # reasonable code and avoiding trying to parse more C++ using regexps.
4773 if not Search(r'^\s*}[\w\*,\s]*;', next_line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004774 error(filename, linenum, 'readability/constructors', 3,
4775 match.group(1) + ' should be the last thing in the class')
4776
4777 # Check for use of unnamed namespaces in header files. Registration
4778 # macros are typically OK, so we allow use of "namespace {" on lines
4779 # that end with backslashes.
4780 if (file_extension == 'h'
4781 and Search(r'\bnamespace\s*{', line)
4782 and line[-1] != '\\'):
4783 error(filename, linenum, 'build/namespaces', 4,
4784 'Do not use unnamed namespaces in header files. See '
4785 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
4786 ' for more information.')
4787
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004788
4789def CheckGlobalStatic(filename, clean_lines, linenum, error):
4790 """Check for unsafe global or static objects.
4791
4792 Args:
4793 filename: The name of the current file.
4794 clean_lines: A CleansedLines instance containing the file.
4795 linenum: The number of the line to check.
4796 error: The function to call with any errors found.
4797 """
4798 line = clean_lines.elided[linenum]
4799
avakulenko@google.com59146752014-08-11 20:20:55 +00004800 # Match two lines at a time to support multiline declarations
4801 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4802 line += clean_lines.elided[linenum + 1].strip()
4803
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004804 # Check for people declaring static/global STL strings at the top level.
4805 # This is dangerous because the C++ language does not guarantee that
4806 # globals with constructors are initialized before the first access.
4807 match = Match(
4808 r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
4809 line)
avakulenko@google.com59146752014-08-11 20:20:55 +00004810
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004811 # Remove false positives:
4812 # - String pointers (as opposed to values).
4813 # string *pointer
4814 # const string *pointer
4815 # string const *pointer
4816 # string *const pointer
4817 #
4818 # - Functions and template specializations.
4819 # string Function<Type>(...
4820 # string Class<Type>::Method(...
4821 #
4822 # - Operators. These are matched separately because operator names
4823 # cross non-word boundaries, and trying to match both operators
4824 # and functions at the same time would decrease accuracy of
4825 # matching identifiers.
4826 # string Class::operator*()
4827 if (match and
4828 not Search(r'\bstring\b(\s+const)?\s*\*\s*(const\s+)?\w', line) and
4829 not Search(r'\boperator\W', line) and
avakulenko@google.com59146752014-08-11 20:20:55 +00004830 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(3))):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004831 error(filename, linenum, 'runtime/string', 4,
4832 'For a static/global string constant, use a C style string instead: '
4833 '"%schar %s[]".' %
4834 (match.group(1), match.group(2)))
4835
4836 if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
4837 error(filename, linenum, 'runtime/init', 4,
4838 'You seem to be initializing a member variable with itself.')
4839
4840
4841def CheckPrintf(filename, clean_lines, linenum, error):
4842 """Check for printf related issues.
4843
4844 Args:
4845 filename: The name of the current file.
4846 clean_lines: A CleansedLines instance containing the file.
4847 linenum: The number of the line to check.
4848 error: The function to call with any errors found.
4849 """
4850 line = clean_lines.elided[linenum]
4851
4852 # When snprintf is used, the second argument shouldn't be a literal.
4853 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4854 if match and match.group(2) != '0':
4855 # If 2nd arg is zero, snprintf is used to calculate size.
4856 error(filename, linenum, 'runtime/printf', 3,
4857 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4858 'to snprintf.' % (match.group(1), match.group(2)))
4859
4860 # Check if some verboten C functions are being used.
avakulenko@google.com59146752014-08-11 20:20:55 +00004861 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004862 error(filename, linenum, 'runtime/printf', 5,
4863 'Never use sprintf. Use snprintf instead.')
avakulenko@google.com59146752014-08-11 20:20:55 +00004864 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004865 if match:
4866 error(filename, linenum, 'runtime/printf', 4,
4867 'Almost always, snprintf is better than %s' % match.group(1))
4868
4869
4870def IsDerivedFunction(clean_lines, linenum):
4871 """Check if current line contains an inherited function.
4872
4873 Args:
4874 clean_lines: A CleansedLines instance containing the file.
4875 linenum: The number of the line to check.
4876 Returns:
4877 True if current line contains a function with "override"
4878 virt-specifier.
4879 """
avakulenko@google.com59146752014-08-11 20:20:55 +00004880 # Scan back a few lines for start of current function
4881 for i in xrange(linenum, max(-1, linenum - 10), -1):
4882 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4883 if match:
4884 # Look for "override" after the matching closing parenthesis
4885 line, _, closing_paren = CloseExpression(
4886 clean_lines, i, len(match.group(1)))
4887 return (closing_paren >= 0 and
4888 Search(r'\boverride\b', line[closing_paren:]))
4889 return False
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004890
4891
4892def IsInitializerList(clean_lines, linenum):
4893 """Check if current line is inside constructor initializer list.
4894
4895 Args:
4896 clean_lines: A CleansedLines instance containing the file.
4897 linenum: The number of the line to check.
4898 Returns:
4899 True if current line appears to be inside constructor initializer
4900 list, False otherwise.
4901 """
4902 for i in xrange(linenum, 1, -1):
4903 line = clean_lines.elided[i]
4904 if i == linenum:
4905 remove_function_body = Match(r'^(.*)\{\s*$', line)
4906 if remove_function_body:
4907 line = remove_function_body.group(1)
4908
4909 if Search(r'\s:\s*\w+[({]', line):
4910 # A lone colon tend to indicate the start of a constructor
4911 # initializer list. It could also be a ternary operator, which
4912 # also tend to appear in constructor initializer lists as
4913 # opposed to parameter lists.
4914 return True
4915 if Search(r'\}\s*,\s*$', line):
4916 # A closing brace followed by a comma is probably the end of a
4917 # brace-initialized member in constructor initializer list.
4918 return True
4919 if Search(r'[{};]\s*$', line):
4920 # Found one of the following:
4921 # - A closing brace or semicolon, probably the end of the previous
4922 # function.
4923 # - An opening brace, probably the start of current class or namespace.
4924 #
4925 # Current line is probably not inside an initializer list since
4926 # we saw one of those things without seeing the starting colon.
4927 return False
4928
4929 # Got to the beginning of the file without seeing the start of
4930 # constructor initializer list.
4931 return False
4932
4933
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004934def CheckForNonConstReference(filename, clean_lines, linenum,
4935 nesting_state, error):
4936 """Check for non-const references.
4937
4938 Separate from CheckLanguage since it scans backwards from current
4939 line, instead of scanning forward.
4940
4941 Args:
4942 filename: The name of the current file.
4943 clean_lines: A CleansedLines instance containing the file.
4944 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004945 nesting_state: A NestingState instance which maintains information about
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004946 the current stack of nested blocks being parsed.
4947 error: The function to call with any errors found.
4948 """
4949 # Do nothing if there is no '&' on current line.
4950 line = clean_lines.elided[linenum]
4951 if '&' not in line:
4952 return
4953
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004954 # If a function is inherited, current function doesn't have much of
4955 # a choice, so any non-const references should not be blamed on
4956 # derived function.
4957 if IsDerivedFunction(clean_lines, linenum):
4958 return
4959
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004960 # Long type names may be broken across multiple lines, usually in one
4961 # of these forms:
4962 # LongType
4963 # ::LongTypeContinued &identifier
4964 # LongType::
4965 # LongTypeContinued &identifier
4966 # LongType<
4967 # ...>::LongTypeContinued &identifier
4968 #
4969 # If we detected a type split across two lines, join the previous
4970 # line to current line so that we can match const references
4971 # accordingly.
4972 #
4973 # Note that this only scans back one line, since scanning back
4974 # arbitrary number of lines would be expensive. If you have a type
4975 # that spans more than 2 lines, please use a typedef.
4976 if linenum > 1:
4977 previous = None
4978 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
4979 # previous_line\n + ::current_line
4980 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
4981 clean_lines.elided[linenum - 1])
4982 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
4983 # previous_line::\n + current_line
4984 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
4985 clean_lines.elided[linenum - 1])
4986 if previous:
4987 line = previous.group(1) + line.lstrip()
4988 else:
4989 # Check for templated parameter that is split across multiple lines
4990 endpos = line.rfind('>')
4991 if endpos > -1:
4992 (_, startline, startpos) = ReverseCloseExpression(
4993 clean_lines, linenum, endpos)
4994 if startpos > -1 and startline < linenum:
4995 # Found the matching < on an earlier line, collect all
4996 # pieces up to current line.
4997 line = ''
4998 for i in xrange(startline, linenum + 1):
4999 line += clean_lines.elided[i].strip()
5000
5001 # Check for non-const references in function parameters. A single '&' may
5002 # found in the following places:
5003 # inside expression: binary & for bitwise AND
5004 # inside expression: unary & for taking the address of something
5005 # inside declarators: reference parameter
5006 # We will exclude the first two cases by checking that we are not inside a
5007 # function body, including one that was just introduced by a trailing '{'.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005008 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005009 if (nesting_state.previous_stack_top and
5010 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
5011 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
5012 # Not at toplevel, not within a class, and not within a namespace
5013 return
5014
avakulenko@google.com59146752014-08-11 20:20:55 +00005015 # Avoid initializer lists. We only need to scan back from the
5016 # current line for something that starts with ':'.
5017 #
5018 # We don't need to check the current line, since the '&' would
5019 # appear inside the second set of parentheses on the current line as
5020 # opposed to the first set.
5021 if linenum > 0:
5022 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5023 previous_line = clean_lines.elided[i]
5024 if not Search(r'[),]\s*$', previous_line):
5025 break
5026 if Match(r'^\s*:\s+\S', previous_line):
5027 return
5028
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005029 # Avoid preprocessors
5030 if Search(r'\\\s*$', line):
5031 return
5032
5033 # Avoid constructor initializer lists
5034 if IsInitializerList(clean_lines, linenum):
5035 return
5036
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005037 # We allow non-const references in a few standard places, like functions
5038 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5039 # those function parameters.
5040 #
5041 # We also accept & in static_assert, which looks like a function but
5042 # it's actually a declaration expression.
5043 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5044 r'operator\s*[<>][<>]|'
5045 r'static_assert|COMPILE_ASSERT'
5046 r')\s*\(')
5047 if Search(whitelisted_functions, line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005048 return
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005049 elif not Search(r'\S+\([^)]*$', line):
5050 # Don't see a whitelisted function on this line. Actually we
5051 # didn't see any function name on this line, so this is likely a
5052 # multi-line parameter list. Try a bit harder to catch this case.
5053 for i in xrange(2):
5054 if (linenum > i and
5055 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005056 return
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005057
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005058 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5059 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
5060 if not Match(_RE_PATTERN_CONST_REF_PARAM, parameter):
5061 error(filename, linenum, 'runtime/references', 2,
5062 'Is this a non-const reference? '
5063 'If so, make const or use a pointer: ' +
5064 ReplaceAll(' *<', '<', parameter))
5065
5066
5067def CheckCasts(filename, clean_lines, linenum, error):
5068 """Various cast related checks.
5069
5070 Args:
5071 filename: The name of the current file.
5072 clean_lines: A CleansedLines instance containing the file.
5073 linenum: The number of the line to check.
5074 error: The function to call with any errors found.
5075 """
5076 line = clean_lines.elided[linenum]
5077
5078 # Check to see if they're using an conversion function cast.
5079 # I just try to capture the most common basic types, though there are more.
5080 # Parameterless conversion functions, such as bool(), are allowed as they are
5081 # probably a member operator declaration or default constructor.
5082 match = Search(
5083 r'(\bnew\s+|\S<\s*(?:const\s+)?)?\b'
5084 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5085 r'(\([^)].*)', line)
5086 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5087 if match and not expecting_function:
5088 matched_type = match.group(2)
5089
5090 # matched_new_or_template is used to silence two false positives:
5091 # - New operators
5092 # - Template arguments with function types
5093 #
5094 # For template arguments, we match on types immediately following
5095 # an opening bracket without any spaces. This is a fast way to
5096 # silence the common case where the function type is the first
5097 # template argument. False negative with less-than comparison is
5098 # avoided because those operators are usually followed by a space.
5099 #
5100 # function<double(double)> // bracket + no space = false positive
5101 # value < double(42) // bracket + space = true positive
5102 matched_new_or_template = match.group(1)
5103
avakulenko@google.com59146752014-08-11 20:20:55 +00005104 # Avoid arrays by looking for brackets that come after the closing
5105 # parenthesis.
5106 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5107 return
5108
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005109 # Other things to ignore:
5110 # - Function pointers
5111 # - Casts to pointer types
5112 # - Placement new
5113 # - Alias declarations
5114 matched_funcptr = match.group(3)
5115 if (matched_new_or_template is None and
5116 not (matched_funcptr and
5117 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5118 matched_funcptr) or
5119 matched_funcptr.startswith('(*)'))) and
5120 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5121 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5122 error(filename, linenum, 'readability/casting', 4,
5123 'Using deprecated casting style. '
5124 'Use static_cast<%s>(...) instead' %
5125 matched_type)
5126
5127 if not expecting_function:
avakulenko@google.com59146752014-08-11 20:20:55 +00005128 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005129 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5130
5131 # This doesn't catch all cases. Consider (const char * const)"hello".
5132 #
5133 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5134 # compile).
avakulenko@google.com59146752014-08-11 20:20:55 +00005135 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5136 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005137 pass
5138 else:
5139 # Check pointer casts for other than string constants
avakulenko@google.com59146752014-08-11 20:20:55 +00005140 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5141 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005142
5143 # In addition, we look for people taking the address of a cast. This
5144 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5145 # point where you think.
avakulenko@google.com59146752014-08-11 20:20:55 +00005146 #
5147 # Some non-identifier character is required before the '&' for the
5148 # expression to be recognized as a cast. These are casts:
5149 # expression = &static_cast<int*>(temporary());
5150 # function(&(int*)(temporary()));
5151 #
5152 # This is not a cast:
5153 # reference_type&(int* function_param);
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005154 match = Search(
avakulenko@google.com59146752014-08-11 20:20:55 +00005155 r'(?:[^\w]&\(([^)]+)\)[\w(])|'
5156 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005157 if match and match.group(1) != '*':
5158 # Try a better error message when the & is bound to something
5159 # dereferenced by the casted pointer, as opposed to the casted
5160 # pointer itself.
5161 parenthesis_error = False
5162 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5163 if match:
5164 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5165 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5166 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5167 if x2 >= 0:
5168 extended_line = clean_lines.elided[y2][x2:]
5169 if y2 < clean_lines.NumLines() - 1:
5170 extended_line += clean_lines.elided[y2 + 1]
5171 if Match(r'\s*(?:->|\[)', extended_line):
5172 parenthesis_error = True
5173
5174 if parenthesis_error:
5175 error(filename, linenum, 'readability/casting', 4,
5176 ('Are you taking an address of something dereferenced '
5177 'from a cast? Wrapping the dereferenced expression in '
5178 'parentheses will make the binding more obvious'))
5179 else:
5180 error(filename, linenum, 'runtime/casting', 4,
5181 ('Are you taking an address of a cast? '
5182 'This is dangerous: could be a temp var. '
5183 'Take the address before doing the cast, rather than after'))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005184
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005185
avakulenko@google.com59146752014-08-11 20:20:55 +00005186def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005187 """Checks for a C-style cast by looking for the pattern.
5188
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005189 Args:
5190 filename: The name of the current file.
avakulenko@google.com59146752014-08-11 20:20:55 +00005191 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005192 linenum: The number of the line to check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005193 cast_type: The string for the C++ cast to recommend. This is either
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005194 reinterpret_cast, static_cast, or const_cast, depending.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005195 pattern: The regular expression used to find C-style casts.
5196 error: The function to call with any errors found.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005197
5198 Returns:
5199 True if an error was emitted.
5200 False otherwise.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005201 """
avakulenko@google.com59146752014-08-11 20:20:55 +00005202 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005203 match = Search(pattern, line)
5204 if not match:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005205 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005206
avakulenko@google.com59146752014-08-11 20:20:55 +00005207 # Exclude lines with keywords that tend to look like casts
5208 context = line[0:match.start(1) - 1]
5209 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5210 return False
5211
5212 # Try expanding current context to see if we one level of
5213 # parentheses inside a macro.
5214 if linenum > 0:
5215 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5216 context = clean_lines.elided[i] + context
5217 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005218 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005219
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005220 # operator++(int) and operator--(int)
avakulenko@google.com59146752014-08-11 20:20:55 +00005221 if context.endswith(' operator++') or context.endswith(' operator--'):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005222 return False
5223
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005224 # A single unnamed argument for a function tends to look like old
5225 # style cast. If we see those, don't issue warnings for deprecated
5226 # casts, instead issue warnings for unnamed arguments where
5227 # appropriate.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005228 #
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005229 # These are things that we want warnings for, since the style guide
5230 # explicitly require all parameters to be named:
5231 # Function(int);
5232 # Function(int) {
5233 # ConstMember(int) const;
5234 # ConstMember(int) const {
5235 # ExceptionMember(int) throw (...);
5236 # ExceptionMember(int) throw (...) {
5237 # PureVirtual(int) = 0;
5238 #
5239 # These are functions of some sort, where the compiler would be fine
5240 # if they had named parameters, but people often omit those
5241 # identifiers to reduce clutter:
5242 # (FunctionPointer)(int);
5243 # (FunctionPointer)(int) = value;
5244 # Function((function_pointer_arg)(int))
avakulenko@google.com59146752014-08-11 20:20:55 +00005245 # Function((function_pointer_arg)(int), int param)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005246 # <TemplateArgument(int)>;
5247 # <(FunctionPointerTemplateArgument)(int)>;
5248 remainder = line[match.end(0):]
avakulenko@google.com59146752014-08-11 20:20:55 +00005249 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),])',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005250 remainder):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005251 # Looks like an unnamed parameter.
5252
5253 # Don't warn on any kind of template arguments.
5254 if Match(r'^\s*>', remainder):
5255 return False
5256
5257 # Don't warn on assignments to function pointers, but keep warnings for
5258 # unnamed parameters to pure virtual functions. Note that this pattern
5259 # will also pass on assignments of "0" to function pointers, but the
5260 # preferred values for those would be "nullptr" or "NULL".
5261 matched_zero = Match(r'^\s=\s*(\S+)\s*;', remainder)
5262 if matched_zero and matched_zero.group(1) != '0':
5263 return False
5264
5265 # Don't warn on function pointer declarations. For this we need
5266 # to check what came before the "(type)" string.
5267 if Match(r'.*\)\s*$', line[0:match.start(0)]):
5268 return False
5269
5270 # Don't warn if the parameter is named with block comments, e.g.:
5271 # Function(int /*unused_param*/);
avakulenko@google.com59146752014-08-11 20:20:55 +00005272 raw_line = clean_lines.raw_lines[linenum]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005273 if '/*' in raw_line:
5274 return False
5275
5276 # Passed all filters, issue warning here.
5277 error(filename, linenum, 'readability/function', 3,
5278 'All parameters should be named in a function')
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005279 return True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005280
5281 # At this point, all that should be left is actual casts.
5282 error(filename, linenum, 'readability/casting', 4,
5283 'Using C-style cast. Use %s<%s>(...) instead' %
5284 (cast_type, match.group(1)))
5285
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005286 return True
5287
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005288
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005289def ExpectingFunctionArgs(clean_lines, linenum):
5290 """Checks whether where function type arguments are expected.
5291
5292 Args:
5293 clean_lines: A CleansedLines instance containing the file.
5294 linenum: The number of the line to check.
5295
5296 Returns:
5297 True if the line at 'linenum' is inside something that expects arguments
5298 of function types.
5299 """
5300 line = clean_lines.elided[linenum]
5301 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5302 (linenum >= 2 and
5303 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5304 clean_lines.elided[linenum - 1]) or
5305 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5306 clean_lines.elided[linenum - 2]) or
5307 Search(r'\bstd::m?function\s*\<\s*$',
5308 clean_lines.elided[linenum - 1]))))
5309
5310
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005311_HEADERS_CONTAINING_TEMPLATES = (
5312 ('<deque>', ('deque',)),
5313 ('<functional>', ('unary_function', 'binary_function',
5314 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5315 'negate',
5316 'equal_to', 'not_equal_to', 'greater', 'less',
5317 'greater_equal', 'less_equal',
5318 'logical_and', 'logical_or', 'logical_not',
5319 'unary_negate', 'not1', 'binary_negate', 'not2',
5320 'bind1st', 'bind2nd',
5321 'pointer_to_unary_function',
5322 'pointer_to_binary_function',
5323 'ptr_fun',
5324 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5325 'mem_fun_ref_t',
5326 'const_mem_fun_t', 'const_mem_fun1_t',
5327 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5328 'mem_fun_ref',
5329 )),
5330 ('<limits>', ('numeric_limits',)),
5331 ('<list>', ('list',)),
5332 ('<map>', ('map', 'multimap',)),
5333 ('<memory>', ('allocator',)),
5334 ('<queue>', ('queue', 'priority_queue',)),
5335 ('<set>', ('set', 'multiset',)),
5336 ('<stack>', ('stack',)),
5337 ('<string>', ('char_traits', 'basic_string',)),
5338 ('<utility>', ('pair',)),
5339 ('<vector>', ('vector',)),
5340
5341 # gcc extensions.
5342 # Note: std::hash is their hash, ::hash is our hash
5343 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5344 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5345 ('<slist>', ('slist',)),
5346 )
5347
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005348_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5349
5350_re_pattern_algorithm_header = []
erg@google.com6317a9c2009-06-25 00:28:19 +00005351for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
5352 'transform'):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005353 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5354 # type::max().
5355 _re_pattern_algorithm_header.append(
5356 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5357 _template,
5358 '<algorithm>'))
5359
5360_re_pattern_templates = []
5361for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5362 for _template in _templates:
5363 _re_pattern_templates.append(
5364 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5365 _template + '<>',
5366 _header))
5367
5368
erg@google.com6317a9c2009-06-25 00:28:19 +00005369def FilesBelongToSameModule(filename_cc, filename_h):
5370 """Check if these two filenames belong to the same module.
5371
5372 The concept of a 'module' here is a as follows:
5373 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5374 same 'module' if they are in the same directory.
5375 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5376 to belong to the same module here.
5377
5378 If the filename_cc contains a longer path than the filename_h, for example,
5379 '/absolute/path/to/base/sysinfo.cc', and this file would include
5380 'base/sysinfo.h', this function also produces the prefix needed to open the
5381 header. This is used by the caller of this function to more robustly open the
5382 header file. We don't have access to the real include paths in this context,
5383 so we need this guesswork here.
5384
5385 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5386 according to this implementation. Because of this, this function gives
5387 some false positives. This should be sufficiently rare in practice.
5388
5389 Args:
5390 filename_cc: is the path for the .cc file
5391 filename_h: is the path for the header path
5392
5393 Returns:
5394 Tuple with a bool and a string:
5395 bool: True if filename_cc and filename_h belong to the same module.
5396 string: the additional prefix needed to open the header file.
5397 """
5398
5399 if not filename_cc.endswith('.cc'):
5400 return (False, '')
5401 filename_cc = filename_cc[:-len('.cc')]
5402 if filename_cc.endswith('_unittest'):
5403 filename_cc = filename_cc[:-len('_unittest')]
5404 elif filename_cc.endswith('_test'):
5405 filename_cc = filename_cc[:-len('_test')]
5406 filename_cc = filename_cc.replace('/public/', '/')
5407 filename_cc = filename_cc.replace('/internal/', '/')
5408
5409 if not filename_h.endswith('.h'):
5410 return (False, '')
5411 filename_h = filename_h[:-len('.h')]
5412 if filename_h.endswith('-inl'):
5413 filename_h = filename_h[:-len('-inl')]
5414 filename_h = filename_h.replace('/public/', '/')
5415 filename_h = filename_h.replace('/internal/', '/')
5416
5417 files_belong_to_same_module = filename_cc.endswith(filename_h)
5418 common_path = ''
5419 if files_belong_to_same_module:
5420 common_path = filename_cc[:-len(filename_h)]
5421 return files_belong_to_same_module, common_path
5422
5423
avakulenko@google.com59146752014-08-11 20:20:55 +00005424def UpdateIncludeState(filename, include_dict, io=codecs):
5425 """Fill up the include_dict with new includes found from the file.
erg@google.com6317a9c2009-06-25 00:28:19 +00005426
5427 Args:
5428 filename: the name of the header to read.
avakulenko@google.com59146752014-08-11 20:20:55 +00005429 include_dict: a dictionary in which the headers are inserted.
erg@google.com6317a9c2009-06-25 00:28:19 +00005430 io: The io factory to use to read the file. Provided for testability.
5431
5432 Returns:
avakulenko@google.com59146752014-08-11 20:20:55 +00005433 True if a header was successfully added. False otherwise.
erg@google.com6317a9c2009-06-25 00:28:19 +00005434 """
5435 headerfile = None
5436 try:
5437 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5438 except IOError:
5439 return False
5440 linenum = 0
5441 for line in headerfile:
5442 linenum += 1
5443 clean_line = CleanseComments(line)
5444 match = _RE_PATTERN_INCLUDE.search(clean_line)
5445 if match:
5446 include = match.group(2)
avakulenko@google.com59146752014-08-11 20:20:55 +00005447 include_dict.setdefault(include, linenum)
erg@google.com6317a9c2009-06-25 00:28:19 +00005448 return True
5449
5450
5451def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5452 io=codecs):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005453 """Reports for missing stl includes.
5454
5455 This function will output warnings to make sure you are including the headers
5456 necessary for the stl containers and functions that you use. We only give one
5457 reason to include a header. For example, if you use both equal_to<> and
5458 less<> in a .h file, only one (the latter in the file) of these will be
5459 reported as a reason to include the <functional>.
5460
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005461 Args:
5462 filename: The name of the current file.
5463 clean_lines: A CleansedLines instance containing the file.
5464 include_state: An _IncludeState instance.
5465 error: The function to call with any errors found.
erg@google.com6317a9c2009-06-25 00:28:19 +00005466 io: The IO factory to use to read the header file. Provided for unittest
5467 injection.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005468 """
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005469 required = {} # A map of header name to linenumber and the template entity.
5470 # Example of required: { '<functional>': (1219, 'less<>') }
5471
5472 for linenum in xrange(clean_lines.NumLines()):
5473 line = clean_lines.elided[linenum]
5474 if not line or line[0] == '#':
5475 continue
5476
5477 # String is special -- it is a non-templatized type in STL.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005478 matched = _RE_PATTERN_STRING.search(line)
5479 if matched:
erg@google.com35589e62010-11-17 18:58:16 +00005480 # Don't warn about strings in non-STL namespaces:
5481 # (We check only the first match per line; good enough.)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005482 prefix = line[:matched.start()]
erg@google.com35589e62010-11-17 18:58:16 +00005483 if prefix.endswith('std::') or not prefix.endswith('::'):
5484 required['<string>'] = (linenum, 'string')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005485
5486 for pattern, template, header in _re_pattern_algorithm_header:
5487 if pattern.search(line):
5488 required[header] = (linenum, template)
5489
5490 # The following function is just a speed up, no semantics are changed.
5491 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5492 continue
5493
5494 for pattern, template, header in _re_pattern_templates:
5495 if pattern.search(line):
5496 required[header] = (linenum, template)
5497
erg@google.com6317a9c2009-06-25 00:28:19 +00005498 # The policy is that if you #include something in foo.h you don't need to
5499 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.com59146752014-08-11 20:20:55 +00005500 # Let's flatten the include_state include_list and copy it into a dictionary.
5501 include_dict = dict([item for sublist in include_state.include_list
5502 for item in sublist])
erg@google.com6317a9c2009-06-25 00:28:19 +00005503
avakulenko@google.com59146752014-08-11 20:20:55 +00005504 # Did we find the header for this file (if any) and successfully load it?
erg@google.com6317a9c2009-06-25 00:28:19 +00005505 header_found = False
5506
5507 # Use the absolute path so that matching works properly.
erg@chromium.org8f927562012-01-30 19:51:28 +00005508 abs_filename = FileInfo(filename).FullName()
erg@google.com6317a9c2009-06-25 00:28:19 +00005509
5510 # For Emacs's flymake.
5511 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5512 # by flymake and that file name might end with '_flymake.cc'. In that case,
5513 # restore original file name here so that the corresponding header file can be
5514 # found.
5515 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5516 # instead of 'foo_flymake.h'
erg@google.com35589e62010-11-17 18:58:16 +00005517 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.com6317a9c2009-06-25 00:28:19 +00005518
avakulenko@google.com59146752014-08-11 20:20:55 +00005519 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.com6317a9c2009-06-25 00:28:19 +00005520 # the keys.
avakulenko@google.com59146752014-08-11 20:20:55 +00005521 header_keys = include_dict.keys()
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005522 for header in header_keys:
erg@google.com6317a9c2009-06-25 00:28:19 +00005523 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5524 fullpath = common_path + header
avakulenko@google.com59146752014-08-11 20:20:55 +00005525 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.com6317a9c2009-06-25 00:28:19 +00005526 header_found = True
5527
5528 # If we can't find the header file for a .cc, assume it's because we don't
5529 # know where to look. In that case we'll give up as we're not sure they
5530 # didn't include it in the .h file.
5531 # TODO(unknown): Do a better job of finding .h files so we are confident that
5532 # not having the .h file means there isn't one.
5533 if filename.endswith('.cc') and not header_found:
5534 return
5535
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005536 # All the lines have been processed, report the errors found.
5537 for required_header_unstripped in required:
5538 template = required[required_header_unstripped][1]
avakulenko@google.com59146752014-08-11 20:20:55 +00005539 if required_header_unstripped.strip('<>"') not in include_dict:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005540 error(filename, required[required_header_unstripped][0],
5541 'build/include_what_you_use', 4,
5542 'Add #include ' + required_header_unstripped + ' for ' + template)
5543
5544
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005545_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5546
5547
5548def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5549 """Check that make_pair's template arguments are deduced.
5550
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005551 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005552 specified explicitly, and such use isn't intended in any case.
5553
5554 Args:
5555 filename: The name of the current file.
5556 clean_lines: A CleansedLines instance containing the file.
5557 linenum: The number of the line to check.
5558 error: The function to call with any errors found.
5559 """
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005560 line = clean_lines.elided[linenum]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005561 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5562 if match:
5563 error(filename, linenum, 'build/explicit_make_pair',
5564 4, # 4 = high confidence
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005565 'For C++11-compatibility, omit template arguments from make_pair'
5566 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.com59146752014-08-11 20:20:55 +00005567
5568
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005569def CheckDefaultLambdaCaptures(filename, clean_lines, linenum, error):
5570 """Check that default lambda captures are not used.
5571
5572 Args:
5573 filename: The name of the current file.
5574 clean_lines: A CleansedLines instance containing the file.
5575 linenum: The number of the line to check.
5576 error: The function to call with any errors found.
5577 """
5578 line = clean_lines.elided[linenum]
5579
5580 # A lambda introducer specifies a default capture if it starts with "[="
5581 # or if it starts with "[&" _not_ followed by an identifier.
5582 match = Match(r'^(.*)\[\s*(?:=|&[^\w])', line)
5583 if match:
5584 # Found a potential error, check what comes after the lambda-introducer.
5585 # If it's not open parenthesis (for lambda-declarator) or open brace
5586 # (for compound-statement), it's not a lambda.
5587 line, _, pos = CloseExpression(clean_lines, linenum, len(match.group(1)))
5588 if pos >= 0 and Match(r'^\s*[{(]', line[pos:]):
5589 error(filename, linenum, 'build/c++11',
5590 4, # 4 = high confidence
5591 'Default lambda captures are an unapproved C++ feature.')
5592
5593
avakulenko@google.com59146752014-08-11 20:20:55 +00005594def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5595 """Check if line contains a redundant "virtual" function-specifier.
5596
5597 Args:
5598 filename: The name of the current file.
5599 clean_lines: A CleansedLines instance containing the file.
5600 linenum: The number of the line to check.
5601 error: The function to call with any errors found.
5602 """
5603 # Look for "virtual" on current line.
5604 line = clean_lines.elided[linenum]
5605 virtual = Match(r'^(.*\bvirtual\b)', line)
5606 if not virtual: return
5607
5608 # Look for the next opening parenthesis. This is the start of the
5609 # parameter list (possibly on the next line shortly after virtual).
5610 # TODO(unknown): doesn't work if there are virtual functions with
5611 # decltype() or other things that use parentheses, but csearch suggests
5612 # that this is rare.
5613 end_col = -1
5614 end_line = -1
5615 start_col = len(virtual.group(1))
5616 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5617 line = clean_lines.elided[start_line][start_col:]
5618 parameter_list = Match(r'^([^(]*)\(', line)
5619 if parameter_list:
5620 # Match parentheses to find the end of the parameter list
5621 (_, end_line, end_col) = CloseExpression(
5622 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5623 break
5624 start_col = 0
5625
5626 if end_col < 0:
5627 return # Couldn't find end of parameter list, give up
5628
5629 # Look for "override" or "final" after the parameter list
5630 # (possibly on the next few lines).
5631 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5632 line = clean_lines.elided[i][end_col:]
5633 match = Search(r'\b(override|final)\b', line)
5634 if match:
5635 error(filename, linenum, 'readability/inheritance', 4,
5636 ('"virtual" is redundant since function is '
5637 'already declared as "%s"' % match.group(1)))
5638
5639 # Set end_col to check whole lines after we are done with the
5640 # first line.
5641 end_col = 0
5642 if Search(r'[^\w]\s*$', line):
5643 break
5644
5645
5646def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5647 """Check if line contains a redundant "override" or "final" virt-specifier.
5648
5649 Args:
5650 filename: The name of the current file.
5651 clean_lines: A CleansedLines instance containing the file.
5652 linenum: The number of the line to check.
5653 error: The function to call with any errors found.
5654 """
5655 # Check that at most one of "override" or "final" is present, not both
5656 line = clean_lines.elided[linenum]
5657 if Search(r'\boverride\b', line) and Search(r'\bfinal\b', line):
5658 error(filename, linenum, 'readability/inheritance', 4,
5659 ('"override" is redundant since function is '
5660 'already declared as "final"'))
5661
5662
5663
5664
5665# Returns true if we are at a new block, and it is directly
5666# inside of a namespace.
5667def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5668 """Checks that the new block is directly in a namespace.
5669
5670 Args:
5671 nesting_state: The _NestingState object that contains info about our state.
5672 is_forward_declaration: If the class is a forward declared class.
5673 Returns:
5674 Whether or not the new block is directly in a namespace.
5675 """
5676 if is_forward_declaration:
5677 if len(nesting_state.stack) >= 1 and (
5678 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5679 return True
5680 else:
5681 return False
5682
5683 return (len(nesting_state.stack) > 1 and
5684 nesting_state.stack[-1].check_namespace_indentation and
5685 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5686
5687
5688def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5689 raw_lines_no_comments, linenum):
5690 """This method determines if we should apply our namespace indentation check.
5691
5692 Args:
5693 nesting_state: The current nesting state.
5694 is_namespace_indent_item: If we just put a new class on the stack, True.
5695 If the top of the stack is not a class, or we did not recently
5696 add the class, False.
5697 raw_lines_no_comments: The lines without the comments.
5698 linenum: The current line number we are processing.
5699
5700 Returns:
5701 True if we should apply our namespace indentation check. Currently, it
5702 only works for classes and namespaces inside of a namespace.
5703 """
5704
5705 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5706 linenum)
5707
5708 if not (is_namespace_indent_item or is_forward_declaration):
5709 return False
5710
5711 # If we are in a macro, we do not want to check the namespace indentation.
5712 if IsMacroDefinition(raw_lines_no_comments, linenum):
5713 return False
5714
5715 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5716
5717
5718# Call this method if the line is directly inside of a namespace.
5719# If the line above is blank (excluding comments) or the start of
5720# an inner namespace, it cannot be indented.
5721def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5722 error):
5723 line = raw_lines_no_comments[linenum]
5724 if Match(r'^\s+', line):
5725 error(filename, linenum, 'runtime/indentation_namespace', 4,
5726 'Do not indent within a namespace')
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005727
5728
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005729def ProcessLine(filename, file_extension, clean_lines, line,
5730 include_state, function_state, nesting_state, error,
5731 extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005732 """Processes a single line in the file.
5733
5734 Args:
5735 filename: Filename of the file that is being processed.
5736 file_extension: The extension (dot not included) of the file.
5737 clean_lines: An array of strings, each representing a line of the file,
5738 with comments stripped.
5739 line: Number of line being processed.
5740 include_state: An _IncludeState instance in which the headers are inserted.
5741 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005742 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005743 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005744 error: A callable to which errors are reported, which takes 4 arguments:
5745 filename, line number, error level, and message
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005746 extra_check_functions: An array of additional check functions that will be
5747 run on each source line. Each function takes 4
5748 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005749 """
5750 raw_lines = clean_lines.raw_lines
erg@google.com35589e62010-11-17 18:58:16 +00005751 ParseNolintSuppressions(filename, raw_lines[line], line, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005752 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.com59146752014-08-11 20:20:55 +00005753 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5754 error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005755 if nesting_state.InAsmBlock(): return
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005756 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005757 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005758 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005759 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005760 nesting_state, error)
5761 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005762 CheckForNonStandardConstructs(filename, clean_lines, line,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005763 nesting_state, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005764 CheckVlogArguments(filename, clean_lines, line, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005765 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com6317a9c2009-06-25 00:28:19 +00005766 CheckInvalidIncrement(filename, clean_lines, line, error)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005767 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005768 CheckDefaultLambdaCaptures(filename, clean_lines, line, error)
avakulenko@google.com59146752014-08-11 20:20:55 +00005769 CheckRedundantVirtual(filename, clean_lines, line, error)
5770 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005771 for check_fn in extra_check_functions:
5772 check_fn(filename, clean_lines, line, error)
avakulenko@google.com17449932014-07-28 22:13:33 +00005773
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005774def FlagCxx11Features(filename, clean_lines, linenum, error):
5775 """Flag those c++11 features that we only allow in certain places.
5776
5777 Args:
5778 filename: The name of the current file.
5779 clean_lines: A CleansedLines instance containing the file.
5780 linenum: The number of the line to check.
5781 error: The function to call with any errors found.
5782 """
5783 line = clean_lines.elided[linenum]
5784
5785 # Flag unapproved C++11 headers.
5786 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5787 if include and include.group(1) in ('cfenv',
5788 'condition_variable',
5789 'fenv.h',
5790 'future',
5791 'mutex',
5792 'thread',
5793 'chrono',
5794 'ratio',
5795 'regex',
5796 'system_error',
5797 ):
5798 error(filename, linenum, 'build/c++11', 5,
5799 ('<%s> is an unapproved C++11 header.') % include.group(1))
5800
5801 # The only place where we need to worry about C++11 keywords and library
5802 # features in preprocessor directives is in macro definitions.
5803 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5804
5805 # These are classes and free functions. The classes are always
5806 # mentioned as std::*, but we only catch the free functions if
5807 # they're not found by ADL. They're alphabetical by header.
5808 for top_name in (
5809 # type_traits
5810 'alignment_of',
5811 'aligned_union',
5812
5813 # utility
5814 'forward',
5815 ):
5816 if Search(r'\bstd::%s\b' % top_name, line):
5817 error(filename, linenum, 'build/c++11', 5,
5818 ('std::%s is an unapproved C++11 class or function. Send c-style '
5819 'an example of where it would make your code more readable, and '
5820 'they may let you use it.') % top_name)
5821
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005822
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005823def ProcessFileData(filename, file_extension, lines, error,
5824 extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005825 """Performs lint checks and reports any errors to the given error function.
5826
5827 Args:
5828 filename: Filename of the file that is being processed.
5829 file_extension: The extension (dot not included) of the file.
5830 lines: An array of strings, each representing a line of the file, with the
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005831 last element being empty if the file is terminated with a newline.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005832 error: A callable to which errors are reported, which takes 4 arguments:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005833 filename, line number, error level, and message
5834 extra_check_functions: An array of additional check functions that will be
5835 run on each source line. Each function takes 4
5836 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005837 """
5838 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5839 ['// marker so line numbers end in a known way'])
5840
5841 include_state = _IncludeState()
5842 function_state = _FunctionState()
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005843 nesting_state = NestingState()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005844
erg@google.com35589e62010-11-17 18:58:16 +00005845 ResetNolintSuppressions()
5846
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005847 CheckForCopyright(filename, lines, error)
5848
5849 if file_extension == 'h':
5850 CheckForHeaderGuard(filename, lines, error)
5851
5852 RemoveMultiLineComments(filename, lines, error)
5853 clean_lines = CleansedLines(lines)
5854 for line in xrange(clean_lines.NumLines()):
5855 ProcessLine(filename, file_extension, clean_lines, line,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005856 include_state, function_state, nesting_state, error,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005857 extra_check_functions)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005858 FlagCxx11Features(filename, clean_lines, line, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005859 nesting_state.CheckCompletedBlocks(filename, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005860
5861 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
5862
5863 # We check here rather than inside ProcessLine so that we see raw
5864 # lines rather than "cleaned" lines.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005865 CheckForBadCharacters(filename, lines, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005866
5867 CheckForNewlineAtEOF(filename, lines, error)
5868
avakulenko@google.com17449932014-07-28 22:13:33 +00005869def ProcessConfigOverrides(filename):
5870 """ Loads the configuration files and processes the config overrides.
5871
5872 Args:
5873 filename: The name of the file being processed by the linter.
5874
5875 Returns:
5876 False if the current |filename| should not be processed further.
5877 """
5878
5879 abs_filename = os.path.abspath(filename)
5880 cfg_filters = []
5881 keep_looking = True
5882 while keep_looking:
5883 abs_path, base_name = os.path.split(abs_filename)
5884 if not base_name:
5885 break # Reached the root directory.
5886
5887 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5888 abs_filename = abs_path
5889 if not os.path.isfile(cfg_file):
5890 continue
5891
5892 try:
5893 with open(cfg_file) as file_handle:
5894 for line in file_handle:
5895 line, _, _ = line.partition('#') # Remove comments.
5896 if not line.strip():
5897 continue
5898
5899 name, _, val = line.partition('=')
5900 name = name.strip()
5901 val = val.strip()
5902 if name == 'set noparent':
5903 keep_looking = False
5904 elif name == 'filter':
5905 cfg_filters.append(val)
5906 elif name == 'exclude_files':
5907 # When matching exclude_files pattern, use the base_name of
5908 # the current file name or the directory name we are processing.
5909 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5910 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5911 # file's "exclude_files" filter is meant to be checked against "bar"
5912 # and not "baz" nor "bar/baz.cc".
5913 if base_name:
5914 pattern = re.compile(val)
5915 if pattern.match(base_name):
5916 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5917 'File path component "%s" matches '
5918 'pattern "%s"\n' %
5919 (filename, cfg_file, base_name, val))
5920 return False
avakulenko@google.com68a4fa62014-08-25 16:26:18 +00005921 elif name == 'linelength':
5922 global _line_length
5923 try:
5924 _line_length = int(val)
5925 except ValueError:
5926 sys.stderr.write('Line length must be numeric.')
avakulenko@google.com17449932014-07-28 22:13:33 +00005927 else:
5928 sys.stderr.write(
5929 'Invalid configuration option (%s) in file %s\n' %
5930 (name, cfg_file))
5931
5932 except IOError:
5933 sys.stderr.write(
5934 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5935 keep_looking = False
5936
5937 # Apply all the accumulated filters in reverse order (top-level directory
5938 # config options having the least priority).
5939 for filter in reversed(cfg_filters):
5940 _AddFilters(filter)
5941
5942 return True
5943
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005944
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005945def ProcessFile(filename, vlevel, extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005946 """Does google-lint on a single file.
5947
5948 Args:
5949 filename: The name of the file to parse.
5950
5951 vlevel: The level of errors to report. Every error of confidence
5952 >= verbose_level will be reported. 0 is a good default.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005953
5954 extra_check_functions: An array of additional check functions that will be
5955 run on each source line. Each function takes 4
5956 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005957 """
5958
5959 _SetVerboseLevel(vlevel)
avakulenko@google.com17449932014-07-28 22:13:33 +00005960 _BackupFilters()
5961
5962 if not ProcessConfigOverrides(filename):
5963 _RestoreFilters()
5964 return
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005965
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005966 lf_lines = []
5967 crlf_lines = []
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005968 try:
5969 # Support the UNIX convention of using "-" for stdin. Note that
5970 # we are not opening the file with universal newline support
5971 # (which codecs doesn't support anyway), so the resulting lines do
5972 # contain trailing '\r' characters if we are reading a file that
5973 # has CRLF endings.
5974 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005975 # below.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005976 if filename == '-':
5977 lines = codecs.StreamReaderWriter(sys.stdin,
5978 codecs.getreader('utf8'),
5979 codecs.getwriter('utf8'),
5980 'replace').read().split('\n')
5981 else:
5982 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
5983
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005984 # Remove trailing '\r'.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005985 # The -1 accounts for the extra trailing blank line we get from split()
5986 for linenum in range(len(lines) - 1):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005987 if lines[linenum].endswith('\r'):
5988 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005989 crlf_lines.append(linenum + 1)
5990 else:
5991 lf_lines.append(linenum + 1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005992
5993 except IOError:
5994 sys.stderr.write(
5995 "Skipping input '%s': Can't open for reading\n" % filename)
avakulenko@google.com17449932014-07-28 22:13:33 +00005996 _RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005997 return
5998
5999 # Note, if no dot is found, this will give the entire filename as the ext.
6000 file_extension = filename[filename.rfind('.') + 1:]
6001
6002 # When reading from stdin, the extension is unknown, so no cpplint tests
6003 # should rely on the extension.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006004 if filename != '-' and file_extension not in _valid_extensions:
6005 sys.stderr.write('Ignoring %s; not a valid file name '
6006 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006007 else:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00006008 ProcessFileData(filename, file_extension, lines, Error,
6009 extra_check_functions)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006010
6011 # If end-of-line sequences are a mix of LF and CR-LF, issue
6012 # warnings on the lines with CR.
6013 #
6014 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
6015 # since critique can handle these just fine, and the style guide
6016 # doesn't dictate a particular end of line sequence.
6017 #
6018 # We can't depend on os.linesep to determine what the desired
6019 # end-of-line sequence should be, since that will return the
6020 # server-side end-of-line sequence.
6021 if lf_lines and crlf_lines:
6022 # Warn on every line with CR. An alternative approach might be to
6023 # check whether the file is mostly CRLF or just LF, and warn on the
6024 # minority, we bias toward LF here since most tools prefer LF.
6025 for linenum in crlf_lines:
6026 Error(filename, linenum, 'whitespace/newline', 1,
6027 'Unexpected \\r (^M) found; better to use only \\n')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006028
6029 sys.stderr.write('Done processing %s\n' % filename)
avakulenko@google.com17449932014-07-28 22:13:33 +00006030 _RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006031
6032
6033def PrintUsage(message):
6034 """Prints a brief usage string and exits, optionally with an error message.
6035
6036 Args:
6037 message: The optional error message.
6038 """
6039 sys.stderr.write(_USAGE)
6040 if message:
6041 sys.exit('\nFATAL ERROR: ' + message)
6042 else:
6043 sys.exit(1)
6044
6045
6046def PrintCategories():
6047 """Prints a list of all the error-categories used by error messages.
6048
6049 These are the categories used to filter messages via --filter.
6050 """
erg@google.com35589e62010-11-17 18:58:16 +00006051 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006052 sys.exit(0)
6053
6054
6055def ParseArguments(args):
6056 """Parses the command line arguments.
6057
6058 This may set the output format and verbosity level as side-effects.
6059
6060 Args:
6061 args: The command line arguments:
6062
6063 Returns:
6064 The list of filenames to lint.
6065 """
6066 try:
6067 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.com26970fa2009-11-17 18:07:32 +00006068 'counting=',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006069 'filter=',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006070 'root=',
6071 'linelength=',
6072 'extensions='])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006073 except getopt.GetoptError:
6074 PrintUsage('Invalid arguments.')
6075
6076 verbosity = _VerboseLevel()
6077 output_format = _OutputFormat()
6078 filters = ''
erg@google.com26970fa2009-11-17 18:07:32 +00006079 counting_style = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006080
6081 for (opt, val) in opts:
6082 if opt == '--help':
6083 PrintUsage(None)
6084 elif opt == '--output':
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006085 if val not in ('emacs', 'vs7', 'eclipse'):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006086 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006087 output_format = val
6088 elif opt == '--verbose':
6089 verbosity = int(val)
6090 elif opt == '--filter':
6091 filters = val
erg@google.com6317a9c2009-06-25 00:28:19 +00006092 if not filters:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006093 PrintCategories()
erg@google.com26970fa2009-11-17 18:07:32 +00006094 elif opt == '--counting':
6095 if val not in ('total', 'toplevel', 'detailed'):
6096 PrintUsage('Valid counting options are total, toplevel, and detailed')
6097 counting_style = val
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006098 elif opt == '--root':
6099 global _root
6100 _root = val
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006101 elif opt == '--linelength':
6102 global _line_length
6103 try:
6104 _line_length = int(val)
6105 except ValueError:
6106 PrintUsage('Line length must be digits.')
6107 elif opt == '--extensions':
6108 global _valid_extensions
6109 try:
6110 _valid_extensions = set(val.split(','))
6111 except ValueError:
6112 PrintUsage('Extensions must be comma seperated list.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006113
6114 if not filenames:
6115 PrintUsage('No files were specified.')
6116
6117 _SetOutputFormat(output_format)
6118 _SetVerboseLevel(verbosity)
6119 _SetFilters(filters)
erg@google.com26970fa2009-11-17 18:07:32 +00006120 _SetCountingStyle(counting_style)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006121
6122 return filenames
6123
6124
6125def main():
6126 filenames = ParseArguments(sys.argv[1:])
6127
6128 # Change stderr to write with replacement characters so we don't die
6129 # if we try to print something containing non-ASCII characters.
6130 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6131 codecs.getreader('utf8'),
6132 codecs.getwriter('utf8'),
6133 'replace')
6134
erg@google.com26970fa2009-11-17 18:07:32 +00006135 _cpplint_state.ResetErrorCounts()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006136 for filename in filenames:
6137 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.com26970fa2009-11-17 18:07:32 +00006138 _cpplint_state.PrintErrorCounts()
6139
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006140 sys.exit(_cpplint_state.error_count > 0)
6141
6142
6143if __name__ == '__main__':
6144 main()