blob: f0416f20b18c3e50c7665175433e263e5215b409 [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
144
145 "set noparent" option prevents cpplint from traversing directory tree
146 upwards looking for more .cfg files in parent directories. This option
147 is usually placed in the top-level project directory.
148
149 The "filter" option is similar in function to --filter flag. It specifies
150 message filters in addition to the |_DEFAULT_FILTERS| and those specified
151 through --filter command-line flag.
152
153 "exclude_files" allows to specify a regular expression to be matched against
154 a file name. If the expression matches, the file is skipped and not run
155 through liner.
156
157 CPPLINT.cfg has an effect on files in the same directory and all
158 sub-directories, unless overridden by a nested configuration file.
159
160 Example file:
161 filter=-build/include_order,+build/include_alpha
162 exclude_files=.*\.cc
163
164 The above example disables build/include_order warning and enables
165 build/include_alpha as well as excludes all .cc from being
166 processed by linter, in the current directory (where the .cfg
167 file is located) and all sub-directories.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000168"""
169
170# We categorize each error message we print. Here are the categories.
171# We want an explicit list so we can list them all in cpplint --filter=.
172# If you add a new error message with a new category, add it to the list
173# here! cpplint_unittest.py should tell you if you forget to do this.
erg@google.com35589e62010-11-17 18:58:16 +0000174_ERROR_CATEGORIES = [
175 'build/class',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000176 'build/c++11',
erg@google.com35589e62010-11-17 18:58:16 +0000177 'build/deprecated',
178 'build/endif_comment',
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000179 'build/explicit_make_pair',
erg@google.com35589e62010-11-17 18:58:16 +0000180 'build/forward_decl',
181 'build/header_guard',
182 'build/include',
183 'build/include_alpha',
184 'build/include_order',
185 'build/include_what_you_use',
186 'build/namespaces',
187 'build/printf_format',
188 'build/storage_class',
189 'legal/copyright',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000190 'readability/alt_tokens',
erg@google.com35589e62010-11-17 18:58:16 +0000191 'readability/braces',
192 'readability/casting',
193 'readability/check',
194 'readability/constructors',
195 'readability/fn_size',
196 'readability/function',
avakulenko@google.com59146752014-08-11 20:20:55 +0000197 'readability/inheritance',
erg@google.com35589e62010-11-17 18:58:16 +0000198 'readability/multiline_comment',
199 'readability/multiline_string',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000200 'readability/namespace',
erg@google.com35589e62010-11-17 18:58:16 +0000201 'readability/nolint',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000202 'readability/nul',
erg@google.com35589e62010-11-17 18:58:16 +0000203 'readability/streams',
204 'readability/todo',
205 'readability/utf8',
206 'runtime/arrays',
207 'runtime/casting',
208 'runtime/explicit',
209 'runtime/int',
210 'runtime/init',
211 'runtime/invalid_increment',
212 'runtime/member_string_references',
213 'runtime/memset',
avakulenko@google.com59146752014-08-11 20:20:55 +0000214 'runtime/indentation_namespace',
erg@google.com35589e62010-11-17 18:58:16 +0000215 'runtime/operator',
216 'runtime/printf',
217 'runtime/printf_format',
218 'runtime/references',
erg@google.com35589e62010-11-17 18:58:16 +0000219 'runtime/string',
220 'runtime/threadsafe_fn',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000221 'runtime/vlog',
erg@google.com35589e62010-11-17 18:58:16 +0000222 'whitespace/blank_line',
223 'whitespace/braces',
224 'whitespace/comma',
225 'whitespace/comments',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000226 'whitespace/empty_conditional_body',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000227 'whitespace/empty_loop_body',
erg@google.com35589e62010-11-17 18:58:16 +0000228 'whitespace/end_of_line',
229 'whitespace/ending_newline',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000230 'whitespace/forcolon',
erg@google.com35589e62010-11-17 18:58:16 +0000231 'whitespace/indent',
erg@google.com35589e62010-11-17 18:58:16 +0000232 'whitespace/line_length',
233 'whitespace/newline',
234 'whitespace/operators',
235 'whitespace/parens',
236 'whitespace/semicolon',
237 'whitespace/tab',
238 'whitespace/todo'
239 ]
erg@google.com6317a9c2009-06-25 00:28:19 +0000240
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000241# The default state of the category filter. This is overridden by the --filter=
erg@google.com6317a9c2009-06-25 00:28:19 +0000242# flag. By default all errors are on, so only add here categories that should be
243# off by default (i.e., categories that must be enabled by the --filter= flags).
244# All entries here should start with a '-' or '+', as in the --filter= flag.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000245_DEFAULT_FILTERS = ['-build/include_alpha']
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000246
247# We used to check for high-bit characters, but after much discussion we
248# decided those were OK, as long as they were in UTF-8 and didn't represent
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000249# hard-coded international strings, which belong in a separate i18n file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000250
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000251# C++ headers
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000252_CPP_HEADERS = frozenset([
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000253 # Legacy
254 'algobase.h',
255 'algo.h',
256 'alloc.h',
257 'builtinbuf.h',
258 'bvector.h',
259 'complex.h',
260 'defalloc.h',
261 'deque.h',
262 'editbuf.h',
263 'fstream.h',
264 'function.h',
265 'hash_map',
266 'hash_map.h',
267 'hash_set',
268 'hash_set.h',
269 'hashtable.h',
270 'heap.h',
271 'indstream.h',
272 'iomanip.h',
273 'iostream.h',
274 'istream.h',
275 'iterator.h',
276 'list.h',
277 'map.h',
278 'multimap.h',
279 'multiset.h',
280 'ostream.h',
281 'pair.h',
282 'parsestream.h',
283 'pfstream.h',
284 'procbuf.h',
285 'pthread_alloc',
286 'pthread_alloc.h',
287 'rope',
288 'rope.h',
289 'ropeimpl.h',
290 'set.h',
291 'slist',
292 'slist.h',
293 'stack.h',
294 'stdiostream.h',
295 'stl_alloc.h',
296 'stl_relops.h',
297 'streambuf.h',
298 'stream.h',
299 'strfile.h',
300 'strstream.h',
301 'tempbuf.h',
302 'tree.h',
303 'type_traits.h',
304 'vector.h',
305 # 17.6.1.2 C++ library headers
306 'algorithm',
307 'array',
308 'atomic',
309 'bitset',
310 'chrono',
311 'codecvt',
312 'complex',
313 'condition_variable',
314 'deque',
315 'exception',
316 'forward_list',
317 'fstream',
318 'functional',
319 'future',
320 'initializer_list',
321 'iomanip',
322 'ios',
323 'iosfwd',
324 'iostream',
325 'istream',
326 'iterator',
327 'limits',
328 'list',
329 'locale',
330 'map',
331 'memory',
332 'mutex',
333 'new',
334 'numeric',
335 'ostream',
336 'queue',
337 'random',
338 'ratio',
339 'regex',
340 'set',
341 'sstream',
342 'stack',
343 'stdexcept',
344 'streambuf',
345 'string',
346 'strstream',
347 'system_error',
348 'thread',
349 'tuple',
350 'typeindex',
351 'typeinfo',
352 'type_traits',
353 'unordered_map',
354 'unordered_set',
355 'utility',
356 'valarray',
357 'vector',
358 # 17.6.1.2 C++ headers for C library facilities
359 'cassert',
360 'ccomplex',
361 'cctype',
362 'cerrno',
363 'cfenv',
364 'cfloat',
365 'cinttypes',
366 'ciso646',
367 'climits',
368 'clocale',
369 'cmath',
370 'csetjmp',
371 'csignal',
372 'cstdalign',
373 'cstdarg',
374 'cstdbool',
375 'cstddef',
376 'cstdint',
377 'cstdio',
378 'cstdlib',
379 'cstring',
380 'ctgmath',
381 'ctime',
382 'cuchar',
383 'cwchar',
384 'cwctype',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000385 ])
386
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000387
avakulenko@google.com59146752014-08-11 20:20:55 +0000388# These headers are excluded from [build/include] and [build/include_order]
389# checks:
390# - Anything not following google file name conventions (containing an
391# uppercase character, such as Python.h or nsStringAPI.h, for example).
392# - Lua headers.
393_THIRD_PARTY_HEADERS_PATTERN = re.compile(
394 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
395
396
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000397# Assertion macros. These are defined in base/logging.h and
398# testing/base/gunit.h. Note that the _M versions need to come first
399# for substring matching to work.
400_CHECK_MACROS = [
erg@google.com6317a9c2009-06-25 00:28:19 +0000401 'DCHECK', 'CHECK',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000402 'EXPECT_TRUE_M', 'EXPECT_TRUE',
403 'ASSERT_TRUE_M', 'ASSERT_TRUE',
404 'EXPECT_FALSE_M', 'EXPECT_FALSE',
405 'ASSERT_FALSE_M', 'ASSERT_FALSE',
406 ]
407
erg@google.com6317a9c2009-06-25 00:28:19 +0000408# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000409_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
410
411for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
412 ('>=', 'GE'), ('>', 'GT'),
413 ('<=', 'LE'), ('<', 'LT')]:
erg@google.com6317a9c2009-06-25 00:28:19 +0000414 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000415 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
416 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
417 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
418 _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
419 _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
420
421for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
422 ('>=', 'LT'), ('>', 'LE'),
423 ('<=', 'GT'), ('<', 'GE')]:
424 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
425 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
426 _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
427 _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
428
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000429# Alternative tokens and their replacements. For full list, see section 2.5
430# Alternative tokens [lex.digraph] in the C++ standard.
431#
432# Digraphs (such as '%:') are not included here since it's a mess to
433# match those on a word boundary.
434_ALT_TOKEN_REPLACEMENT = {
435 'and': '&&',
436 'bitor': '|',
437 'or': '||',
438 'xor': '^',
439 'compl': '~',
440 'bitand': '&',
441 'and_eq': '&=',
442 'or_eq': '|=',
443 'xor_eq': '^=',
444 'not': '!',
445 'not_eq': '!='
446 }
447
448# Compile regular expression that matches all the above keywords. The "[ =()]"
449# bit is meant to avoid matching these keywords outside of boolean expressions.
450#
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000451# False positives include C-style multi-line comments and multi-line strings
452# but those have always been troublesome for cpplint.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000453_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
454 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
455
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000456
457# These constants define types of headers for use with
458# _IncludeState.CheckNextIncludeOrder().
459_C_SYS_HEADER = 1
460_CPP_SYS_HEADER = 2
461_LIKELY_MY_HEADER = 3
462_POSSIBLE_MY_HEADER = 4
463_OTHER_HEADER = 5
464
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000465# These constants define the current inline assembly state
466_NO_ASM = 0 # Outside of inline assembly block
467_INSIDE_ASM = 1 # Inside inline assembly block
468_END_ASM = 2 # Last line of inline assembly block
469_BLOCK_ASM = 3 # The whole block is an inline assembly block
470
471# Match start of assembly blocks
472_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
473 r'(?:\s+(volatile|__volatile__))?'
474 r'\s*[{(]')
475
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000476
477_regexp_compile_cache = {}
478
erg@google.com35589e62010-11-17 18:58:16 +0000479# {str, set(int)}: a map from error categories to sets of linenumbers
480# on which those errors are expected and should be suppressed.
481_error_suppressions = {}
482
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000483# The root directory used for deriving header guard CPP variable.
484# This is set by --root flag.
485_root = None
486
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000487# The allowed line length of files.
488# This is set by --linelength flag.
489_line_length = 80
490
491# The allowed extensions for file names
492# This is set by --extensions flag.
493_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
494
erg@google.com35589e62010-11-17 18:58:16 +0000495def ParseNolintSuppressions(filename, raw_line, linenum, error):
496 """Updates the global list of error-suppressions.
497
498 Parses any NOLINT comments on the current line, updating the global
499 error_suppressions store. Reports an error if the NOLINT comment
500 was malformed.
501
502 Args:
503 filename: str, the name of the input file.
504 raw_line: str, the line of input text, with comments.
505 linenum: int, the number of the current line.
506 error: function, an error handler.
507 """
avakulenko@google.com59146752014-08-11 20:20:55 +0000508 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000509 if matched:
avakulenko@google.com59146752014-08-11 20:20:55 +0000510 if matched.group(1):
511 suppressed_line = linenum + 1
512 else:
513 suppressed_line = linenum
514 category = matched.group(2)
erg@google.com35589e62010-11-17 18:58:16 +0000515 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.com59146752014-08-11 20:20:55 +0000516 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg@google.com35589e62010-11-17 18:58:16 +0000517 else:
518 if category.startswith('(') and category.endswith(')'):
519 category = category[1:-1]
520 if category in _ERROR_CATEGORIES:
avakulenko@google.com59146752014-08-11 20:20:55 +0000521 _error_suppressions.setdefault(category, set()).add(suppressed_line)
erg@google.com35589e62010-11-17 18:58:16 +0000522 else:
523 error(filename, linenum, 'readability/nolint', 5,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000524 'Unknown NOLINT error category: %s' % category)
erg@google.com35589e62010-11-17 18:58:16 +0000525
526
527def ResetNolintSuppressions():
avakulenko@google.com59146752014-08-11 20:20:55 +0000528 """Resets the set of NOLINT suppressions to empty."""
erg@google.com35589e62010-11-17 18:58:16 +0000529 _error_suppressions.clear()
530
531
532def IsErrorSuppressedByNolint(category, linenum):
533 """Returns true if the specified error category is suppressed on this line.
534
535 Consults the global error_suppressions map populated by
536 ParseNolintSuppressions/ResetNolintSuppressions.
537
538 Args:
539 category: str, the category of the error.
540 linenum: int, the current line number.
541 Returns:
542 bool, True iff the error should be suppressed due to a NOLINT comment.
543 """
544 return (linenum in _error_suppressions.get(category, set()) or
545 linenum in _error_suppressions.get(None, set()))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000546
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000547
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000548def Match(pattern, s):
549 """Matches the string with the pattern, caching the compiled regexp."""
550 # The regexp compilation caching is inlined in both Match and Search for
551 # performance reasons; factoring it out into a separate function turns out
552 # to be noticeably expensive.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000553 if pattern not in _regexp_compile_cache:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000554 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
555 return _regexp_compile_cache[pattern].match(s)
556
557
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000558def ReplaceAll(pattern, rep, s):
559 """Replaces instances of pattern in a string with a replacement.
560
561 The compiled regex is kept in a cache shared by Match and Search.
562
563 Args:
564 pattern: regex pattern
565 rep: replacement text
566 s: search string
567
568 Returns:
569 string with replacements made (or original string if no replacements)
570 """
571 if pattern not in _regexp_compile_cache:
572 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
573 return _regexp_compile_cache[pattern].sub(rep, s)
574
575
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000576def Search(pattern, s):
577 """Searches the string for the pattern, caching the compiled regexp."""
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000578 if pattern not in _regexp_compile_cache:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000579 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
580 return _regexp_compile_cache[pattern].search(s)
581
582
avakulenko@google.com59146752014-08-11 20:20:55 +0000583class _IncludeState(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000584 """Tracks line numbers for includes, and the order in which includes appear.
585
avakulenko@google.com59146752014-08-11 20:20:55 +0000586 include_list contains list of lists of (header, line number) pairs.
587 It's a lists of lists rather than just one flat list to make it
588 easier to update across preprocessor boundaries.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000589
590 Call CheckNextIncludeOrder() once for each header in the file, passing
591 in the type constants defined above. Calls in an illegal order will
592 raise an _IncludeError with an appropriate error message.
593
594 """
595 # self._section will move monotonically through this set. If it ever
596 # needs to move backwards, CheckNextIncludeOrder will raise an error.
597 _INITIAL_SECTION = 0
598 _MY_H_SECTION = 1
599 _C_SECTION = 2
600 _CPP_SECTION = 3
601 _OTHER_H_SECTION = 4
602
603 _TYPE_NAMES = {
604 _C_SYS_HEADER: 'C system header',
605 _CPP_SYS_HEADER: 'C++ system header',
606 _LIKELY_MY_HEADER: 'header this file implements',
607 _POSSIBLE_MY_HEADER: 'header this file may implement',
608 _OTHER_HEADER: 'other header',
609 }
610 _SECTION_NAMES = {
611 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
612 _MY_H_SECTION: 'a header this file implements',
613 _C_SECTION: 'C system header',
614 _CPP_SECTION: 'C++ system header',
615 _OTHER_H_SECTION: 'other header',
616 }
617
618 def __init__(self):
avakulenko@google.com59146752014-08-11 20:20:55 +0000619 self.include_list = [[]]
620 self.ResetSection('')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000621
avakulenko@google.com59146752014-08-11 20:20:55 +0000622 def FindHeader(self, header):
623 """Check if a header has already been included.
624
625 Args:
626 header: header to check.
627 Returns:
628 Line number of previous occurrence, or -1 if the header has not
629 been seen before.
630 """
631 for section_list in self.include_list:
632 for f in section_list:
633 if f[0] == header:
634 return f[1]
635 return -1
636
637 def ResetSection(self, directive):
638 """Reset section checking for preprocessor directive.
639
640 Args:
641 directive: preprocessor directive (e.g. "if", "else").
642 """
erg@google.com26970fa2009-11-17 18:07:32 +0000643 # The name of the current section.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000644 self._section = self._INITIAL_SECTION
erg@google.com26970fa2009-11-17 18:07:32 +0000645 # The path of last found header.
646 self._last_header = ''
647
avakulenko@google.com59146752014-08-11 20:20:55 +0000648 # Update list of includes. Note that we never pop from the
649 # include list.
650 if directive in ('if', 'ifdef', 'ifndef'):
651 self.include_list.append([])
652 elif directive in ('else', 'elif'):
653 self.include_list[-1] = []
654
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000655 def SetLastHeader(self, header_path):
656 self._last_header = header_path
657
erg@google.com26970fa2009-11-17 18:07:32 +0000658 def CanonicalizeAlphabeticalOrder(self, header_path):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000659 """Returns a path canonicalized for alphabetical comparison.
erg@google.com26970fa2009-11-17 18:07:32 +0000660
661 - replaces "-" with "_" so they both cmp the same.
662 - removes '-inl' since we don't require them to be after the main header.
663 - lowercase everything, just in case.
664
665 Args:
666 header_path: Path to be canonicalized.
667
668 Returns:
669 Canonicalized path.
670 """
671 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
672
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000673 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.com26970fa2009-11-17 18:07:32 +0000674 """Check if a header is in alphabetical order with the previous header.
675
676 Args:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000677 clean_lines: A CleansedLines instance containing the file.
678 linenum: The number of the line to check.
679 header_path: Canonicalized header to be checked.
erg@google.com26970fa2009-11-17 18:07:32 +0000680
681 Returns:
682 Returns true if the header is in alphabetical order.
683 """
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000684 # If previous section is different from current section, _last_header will
685 # be reset to empty string, so it's always less than current header.
686 #
687 # If previous line was a blank line, assume that the headers are
688 # intentionally sorted the way they are.
689 if (self._last_header > header_path and
690 not Match(r'^\s*$', clean_lines.elided[linenum - 1])):
erg@google.com26970fa2009-11-17 18:07:32 +0000691 return False
erg@google.com26970fa2009-11-17 18:07:32 +0000692 return True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000693
694 def CheckNextIncludeOrder(self, header_type):
695 """Returns a non-empty error message if the next header is out of order.
696
697 This function also updates the internal state to be ready to check
698 the next include.
699
700 Args:
701 header_type: One of the _XXX_HEADER constants defined above.
702
703 Returns:
704 The empty string if the header is in the right order, or an
705 error message describing what's wrong.
706
707 """
708 error_message = ('Found %s after %s' %
709 (self._TYPE_NAMES[header_type],
710 self._SECTION_NAMES[self._section]))
711
erg@google.com26970fa2009-11-17 18:07:32 +0000712 last_section = self._section
713
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000714 if header_type == _C_SYS_HEADER:
715 if self._section <= self._C_SECTION:
716 self._section = self._C_SECTION
717 else:
erg@google.com26970fa2009-11-17 18:07:32 +0000718 self._last_header = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000719 return error_message
720 elif header_type == _CPP_SYS_HEADER:
721 if self._section <= self._CPP_SECTION:
722 self._section = self._CPP_SECTION
723 else:
erg@google.com26970fa2009-11-17 18:07:32 +0000724 self._last_header = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000725 return error_message
726 elif header_type == _LIKELY_MY_HEADER:
727 if self._section <= self._MY_H_SECTION:
728 self._section = self._MY_H_SECTION
729 else:
730 self._section = self._OTHER_H_SECTION
731 elif header_type == _POSSIBLE_MY_HEADER:
732 if self._section <= self._MY_H_SECTION:
733 self._section = self._MY_H_SECTION
734 else:
735 # This will always be the fallback because we're not sure
736 # enough that the header is associated with this file.
737 self._section = self._OTHER_H_SECTION
738 else:
739 assert header_type == _OTHER_HEADER
740 self._section = self._OTHER_H_SECTION
741
erg@google.com26970fa2009-11-17 18:07:32 +0000742 if last_section != self._section:
743 self._last_header = ''
744
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000745 return ''
746
747
748class _CppLintState(object):
749 """Maintains module-wide state.."""
750
751 def __init__(self):
752 self.verbose_level = 1 # global setting.
753 self.error_count = 0 # global count of reported errors
erg@google.com6317a9c2009-06-25 00:28:19 +0000754 # filters to apply when emitting error messages
755 self.filters = _DEFAULT_FILTERS[:]
avakulenko@google.com17449932014-07-28 22:13:33 +0000756 # backup of filter list. Used to restore the state after each file.
757 self._filters_backup = self.filters[:]
erg@google.com26970fa2009-11-17 18:07:32 +0000758 self.counting = 'total' # In what way are we counting errors?
759 self.errors_by_category = {} # string to int dict storing error counts
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000760
761 # output format:
762 # "emacs" - format that emacs can parse (default)
763 # "vs7" - format that Microsoft Visual Studio 7 can parse
764 self.output_format = 'emacs'
765
766 def SetOutputFormat(self, output_format):
767 """Sets the output format for errors."""
768 self.output_format = output_format
769
770 def SetVerboseLevel(self, level):
771 """Sets the module's verbosity, and returns the previous setting."""
772 last_verbose_level = self.verbose_level
773 self.verbose_level = level
774 return last_verbose_level
775
erg@google.com26970fa2009-11-17 18:07:32 +0000776 def SetCountingStyle(self, counting_style):
777 """Sets the module's counting options."""
778 self.counting = counting_style
779
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000780 def SetFilters(self, filters):
781 """Sets the error-message filters.
782
783 These filters are applied when deciding whether to emit a given
784 error message.
785
786 Args:
787 filters: A string of comma-separated filters (eg "+whitespace/indent").
788 Each filter should start with + or -; else we die.
erg@google.com6317a9c2009-06-25 00:28:19 +0000789
790 Raises:
791 ValueError: The comma-separated filters did not all start with '+' or '-'.
792 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000793 """
erg@google.com6317a9c2009-06-25 00:28:19 +0000794 # Default filters always have less priority than the flag ones.
795 self.filters = _DEFAULT_FILTERS[:]
avakulenko@google.com17449932014-07-28 22:13:33 +0000796 self.AddFilters(filters)
797
798 def AddFilters(self, filters):
799 """ Adds more filters to the existing list of error-message filters. """
erg@google.com6317a9c2009-06-25 00:28:19 +0000800 for filt in filters.split(','):
801 clean_filt = filt.strip()
802 if clean_filt:
803 self.filters.append(clean_filt)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000804 for filt in self.filters:
805 if not (filt.startswith('+') or filt.startswith('-')):
806 raise ValueError('Every filter in --filters must start with + or -'
807 ' (%s does not)' % filt)
808
avakulenko@google.com17449932014-07-28 22:13:33 +0000809 def BackupFilters(self):
810 """ Saves the current filter list to backup storage."""
811 self._filters_backup = self.filters[:]
812
813 def RestoreFilters(self):
814 """ Restores filters previously backed up."""
815 self.filters = self._filters_backup[:]
816
erg@google.com26970fa2009-11-17 18:07:32 +0000817 def ResetErrorCounts(self):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000818 """Sets the module's error statistic back to zero."""
819 self.error_count = 0
erg@google.com26970fa2009-11-17 18:07:32 +0000820 self.errors_by_category = {}
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000821
erg@google.com26970fa2009-11-17 18:07:32 +0000822 def IncrementErrorCount(self, category):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000823 """Bumps the module's error statistic."""
824 self.error_count += 1
erg@google.com26970fa2009-11-17 18:07:32 +0000825 if self.counting in ('toplevel', 'detailed'):
826 if self.counting != 'detailed':
827 category = category.split('/')[0]
828 if category not in self.errors_by_category:
829 self.errors_by_category[category] = 0
830 self.errors_by_category[category] += 1
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000831
erg@google.com26970fa2009-11-17 18:07:32 +0000832 def PrintErrorCounts(self):
833 """Print a summary of errors by category, and the total."""
834 for category, count in self.errors_by_category.iteritems():
835 sys.stderr.write('Category \'%s\' errors found: %d\n' %
836 (category, count))
837 sys.stderr.write('Total errors found: %d\n' % self.error_count)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000838
839_cpplint_state = _CppLintState()
840
841
842def _OutputFormat():
843 """Gets the module's output format."""
844 return _cpplint_state.output_format
845
846
847def _SetOutputFormat(output_format):
848 """Sets the module's output format."""
849 _cpplint_state.SetOutputFormat(output_format)
850
851
852def _VerboseLevel():
853 """Returns the module's verbosity setting."""
854 return _cpplint_state.verbose_level
855
856
857def _SetVerboseLevel(level):
858 """Sets the module's verbosity, and returns the previous setting."""
859 return _cpplint_state.SetVerboseLevel(level)
860
861
erg@google.com26970fa2009-11-17 18:07:32 +0000862def _SetCountingStyle(level):
863 """Sets the module's counting options."""
864 _cpplint_state.SetCountingStyle(level)
865
866
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000867def _Filters():
868 """Returns the module's list of output filters, as a list."""
869 return _cpplint_state.filters
870
871
872def _SetFilters(filters):
873 """Sets the module's error-message filters.
874
875 These filters are applied when deciding whether to emit a given
876 error message.
877
878 Args:
879 filters: A string of comma-separated filters (eg "whitespace/indent").
880 Each filter should start with + or -; else we die.
881 """
882 _cpplint_state.SetFilters(filters)
883
avakulenko@google.com17449932014-07-28 22:13:33 +0000884def _AddFilters(filters):
885 """Adds more filter overrides.
avakulenko@google.com59146752014-08-11 20:20:55 +0000886
avakulenko@google.com17449932014-07-28 22:13:33 +0000887 Unlike _SetFilters, this function does not reset the current list of filters
888 available.
889
890 Args:
891 filters: A string of comma-separated filters (eg "whitespace/indent").
892 Each filter should start with + or -; else we die.
893 """
894 _cpplint_state.AddFilters(filters)
895
896def _BackupFilters():
897 """ Saves the current filter list to backup storage."""
898 _cpplint_state.BackupFilters()
899
900def _RestoreFilters():
901 """ Restores filters previously backed up."""
902 _cpplint_state.RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000903
904class _FunctionState(object):
905 """Tracks current function name and the number of lines in its body."""
906
907 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
908 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
909
910 def __init__(self):
911 self.in_a_function = False
912 self.lines_in_function = 0
913 self.current_function = ''
914
915 def Begin(self, function_name):
916 """Start analyzing function body.
917
918 Args:
919 function_name: The name of the function being tracked.
920 """
921 self.in_a_function = True
922 self.lines_in_function = 0
923 self.current_function = function_name
924
925 def Count(self):
926 """Count line in current function body."""
927 if self.in_a_function:
928 self.lines_in_function += 1
929
930 def Check(self, error, filename, linenum):
931 """Report if too many lines in function body.
932
933 Args:
934 error: The function to call with any errors found.
935 filename: The name of the current file.
936 linenum: The number of the line to check.
937 """
938 if Match(r'T(EST|est)', self.current_function):
939 base_trigger = self._TEST_TRIGGER
940 else:
941 base_trigger = self._NORMAL_TRIGGER
942 trigger = base_trigger * 2**_VerboseLevel()
943
944 if self.lines_in_function > trigger:
945 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
946 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
947 if error_level > 5:
948 error_level = 5
949 error(filename, linenum, 'readability/fn_size', error_level,
950 'Small and focused functions are preferred:'
951 ' %s has %d non-comment lines'
952 ' (error triggered by exceeding %d lines).' % (
953 self.current_function, self.lines_in_function, trigger))
954
955 def End(self):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000956 """Stop analyzing function body."""
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000957 self.in_a_function = False
958
959
960class _IncludeError(Exception):
961 """Indicates a problem with the include order in a file."""
962 pass
963
964
avakulenko@google.com59146752014-08-11 20:20:55 +0000965class FileInfo(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000966 """Provides utility functions for filenames.
967
968 FileInfo provides easy access to the components of a file's path
969 relative to the project root.
970 """
971
972 def __init__(self, filename):
973 self._filename = filename
974
975 def FullName(self):
976 """Make Windows paths like Unix."""
977 return os.path.abspath(self._filename).replace('\\', '/')
978
979 def RepositoryName(self):
980 """FullName after removing the local path to the repository.
981
982 If we have a real absolute path name here we can try to do something smart:
983 detecting the root of the checkout and truncating /path/to/checkout from
984 the name so that we get header guards that don't include things like
985 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
986 people on different computers who have checked the source out to different
987 locations won't see bogus errors.
988 """
989 fullname = self.FullName()
990
991 if os.path.exists(fullname):
992 project_dir = os.path.dirname(fullname)
993
994 if os.path.exists(os.path.join(project_dir, ".svn")):
995 # If there's a .svn file in the current directory, we recursively look
996 # up the directory tree for the top of the SVN checkout
997 root_dir = project_dir
998 one_up_dir = os.path.dirname(root_dir)
999 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1000 root_dir = os.path.dirname(root_dir)
1001 one_up_dir = os.path.dirname(one_up_dir)
1002
1003 prefix = os.path.commonprefix([root_dir, project_dir])
1004 return fullname[len(prefix) + 1:]
1005
erg@chromium.org7956a872011-11-30 01:44:03 +00001006 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1007 # searching up from the current path.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001008 root_dir = os.path.dirname(fullname)
1009 while (root_dir != os.path.dirname(root_dir) and
erg@google.com35589e62010-11-17 18:58:16 +00001010 not os.path.exists(os.path.join(root_dir, ".git")) and
erg@chromium.org7956a872011-11-30 01:44:03 +00001011 not os.path.exists(os.path.join(root_dir, ".hg")) and
1012 not os.path.exists(os.path.join(root_dir, ".svn"))):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001013 root_dir = os.path.dirname(root_dir)
erg@google.com35589e62010-11-17 18:58:16 +00001014
1015 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@chromium.org7956a872011-11-30 01:44:03 +00001016 os.path.exists(os.path.join(root_dir, ".hg")) or
1017 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com35589e62010-11-17 18:58:16 +00001018 prefix = os.path.commonprefix([root_dir, project_dir])
1019 return fullname[len(prefix) + 1:]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001020
1021 # Don't know what to do; header guard warnings may be wrong...
1022 return fullname
1023
1024 def Split(self):
1025 """Splits the file into the directory, basename, and extension.
1026
1027 For 'chrome/browser/browser.cc', Split() would
1028 return ('chrome/browser', 'browser', '.cc')
1029
1030 Returns:
1031 A tuple of (directory, basename, extension).
1032 """
1033
1034 googlename = self.RepositoryName()
1035 project, rest = os.path.split(googlename)
1036 return (project,) + os.path.splitext(rest)
1037
1038 def BaseName(self):
1039 """File base name - text after the final slash, before the final period."""
1040 return self.Split()[1]
1041
1042 def Extension(self):
1043 """File extension - text following the final period."""
1044 return self.Split()[2]
1045
1046 def NoExtension(self):
1047 """File has no source file extension."""
1048 return '/'.join(self.Split()[0:2])
1049
1050 def IsSource(self):
1051 """File has a source file extension."""
1052 return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx')
1053
1054
erg@google.com35589e62010-11-17 18:58:16 +00001055def _ShouldPrintError(category, confidence, linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001056 """If confidence >= verbose, category passes filter and is not suppressed."""
erg@google.com35589e62010-11-17 18:58:16 +00001057
1058 # There are three ways we might decide not to print an error message:
1059 # a "NOLINT(category)" comment appears in the source,
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001060 # the verbosity level isn't high enough, or the filters filter it out.
erg@google.com35589e62010-11-17 18:58:16 +00001061 if IsErrorSuppressedByNolint(category, linenum):
1062 return False
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001063
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001064 if confidence < _cpplint_state.verbose_level:
1065 return False
1066
1067 is_filtered = False
1068 for one_filter in _Filters():
1069 if one_filter.startswith('-'):
1070 if category.startswith(one_filter[1:]):
1071 is_filtered = True
1072 elif one_filter.startswith('+'):
1073 if category.startswith(one_filter[1:]):
1074 is_filtered = False
1075 else:
1076 assert False # should have been checked for in SetFilter.
1077 if is_filtered:
1078 return False
1079
1080 return True
1081
1082
1083def Error(filename, linenum, category, confidence, message):
1084 """Logs the fact we've found a lint error.
1085
1086 We log where the error was found, and also our confidence in the error,
1087 that is, how certain we are this is a legitimate style regression, and
1088 not a misidentification or a use that's sometimes justified.
1089
erg@google.com35589e62010-11-17 18:58:16 +00001090 False positives can be suppressed by the use of
1091 "cpplint(category)" comments on the offending line. These are
1092 parsed into _error_suppressions.
1093
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001094 Args:
1095 filename: The name of the file containing the error.
1096 linenum: The number of the line containing the error.
1097 category: A string used to describe the "category" this bug
1098 falls under: "whitespace", say, or "runtime". Categories
1099 may have a hierarchy separated by slashes: "whitespace/indent".
1100 confidence: A number from 1-5 representing a confidence score for
1101 the error, with 5 meaning that we are certain of the problem,
1102 and 1 meaning that it could be a legitimate construct.
1103 message: The error message.
1104 """
erg@google.com35589e62010-11-17 18:58:16 +00001105 if _ShouldPrintError(category, confidence, linenum):
erg@google.com26970fa2009-11-17 18:07:32 +00001106 _cpplint_state.IncrementErrorCount(category)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001107 if _cpplint_state.output_format == 'vs7':
1108 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
1109 filename, linenum, message, category, confidence))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001110 elif _cpplint_state.output_format == 'eclipse':
1111 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1112 filename, linenum, message, category, confidence))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001113 else:
1114 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1115 filename, linenum, message, category, confidence))
1116
1117
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001118# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001119_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1120 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001121# Match a single C style comment on the same line.
1122_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1123# Matches multi-line C style comments.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001124# This RE is a little bit more complicated than one might expect, because we
1125# have to take care of space removals tools so we can handle comments inside
1126# statements better.
1127# The current rule is: We only clear spaces from both sides when we're at the
1128# end of the line. Otherwise, we try to remove spaces from the right side,
1129# if this doesn't work we try on left side but only if there's a non-character
1130# on the right.
1131_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001132 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1133 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1134 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1135 _RE_PATTERN_C_COMMENTS + r')')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001136
1137
1138def IsCppString(line):
1139 """Does line terminate so, that the next symbol is in string constant.
1140
1141 This function does not consider single-line nor multi-line comments.
1142
1143 Args:
1144 line: is a partial line of code starting from the 0..n.
1145
1146 Returns:
1147 True, if next character appended to 'line' is inside a
1148 string constant.
1149 """
1150
1151 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1152 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1153
1154
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001155def CleanseRawStrings(raw_lines):
1156 """Removes C++11 raw strings from lines.
1157
1158 Before:
1159 static const char kData[] = R"(
1160 multi-line string
1161 )";
1162
1163 After:
1164 static const char kData[] = ""
1165 (replaced by blank line)
1166 "";
1167
1168 Args:
1169 raw_lines: list of raw lines.
1170
1171 Returns:
1172 list of lines with C++11 raw strings replaced by empty strings.
1173 """
1174
1175 delimiter = None
1176 lines_without_raw_strings = []
1177 for line in raw_lines:
1178 if delimiter:
1179 # Inside a raw string, look for the end
1180 end = line.find(delimiter)
1181 if end >= 0:
1182 # Found the end of the string, match leading space for this
1183 # line and resume copying the original lines, and also insert
1184 # a "" on the last line.
1185 leading_space = Match(r'^(\s*)\S', line)
1186 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1187 delimiter = None
1188 else:
1189 # Haven't found the end yet, append a blank line.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001190 line = '""'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001191
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001192 # Look for beginning of a raw string, and replace them with
1193 # empty strings. This is done in a loop to handle multiple raw
1194 # strings on the same line.
1195 while delimiter is None:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001196 # Look for beginning of a raw string.
1197 # See 2.14.15 [lex.string] for syntax.
1198 matched = Match(r'^(.*)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1199 if matched:
1200 delimiter = ')' + matched.group(2) + '"'
1201
1202 end = matched.group(3).find(delimiter)
1203 if end >= 0:
1204 # Raw string ended on same line
1205 line = (matched.group(1) + '""' +
1206 matched.group(3)[end + len(delimiter):])
1207 delimiter = None
1208 else:
1209 # Start of a multi-line raw string
1210 line = matched.group(1) + '""'
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001211 else:
1212 break
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001213
1214 lines_without_raw_strings.append(line)
1215
1216 # TODO(unknown): if delimiter is not None here, we might want to
1217 # emit a warning for unterminated string.
1218 return lines_without_raw_strings
1219
1220
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001221def FindNextMultiLineCommentStart(lines, lineix):
1222 """Find the beginning marker for a multiline comment."""
1223 while lineix < len(lines):
1224 if lines[lineix].strip().startswith('/*'):
1225 # Only return this marker if the comment goes beyond this line
1226 if lines[lineix].strip().find('*/', 2) < 0:
1227 return lineix
1228 lineix += 1
1229 return len(lines)
1230
1231
1232def FindNextMultiLineCommentEnd(lines, lineix):
1233 """We are inside a comment, find the end marker."""
1234 while lineix < len(lines):
1235 if lines[lineix].strip().endswith('*/'):
1236 return lineix
1237 lineix += 1
1238 return len(lines)
1239
1240
1241def RemoveMultiLineCommentsFromRange(lines, begin, end):
1242 """Clears a range of lines for multi-line comments."""
1243 # Having // dummy comments makes the lines non-empty, so we will not get
1244 # unnecessary blank line warnings later in the code.
1245 for i in range(begin, end):
1246 lines[i] = '// dummy'
1247
1248
1249def RemoveMultiLineComments(filename, lines, error):
1250 """Removes multiline (c-style) comments from lines."""
1251 lineix = 0
1252 while lineix < len(lines):
1253 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1254 if lineix_begin >= len(lines):
1255 return
1256 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1257 if lineix_end >= len(lines):
1258 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1259 'Could not find end of multi-line comment')
1260 return
1261 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1262 lineix = lineix_end + 1
1263
1264
1265def CleanseComments(line):
1266 """Removes //-comments and single-line C-style /* */ comments.
1267
1268 Args:
1269 line: A line of C++ source.
1270
1271 Returns:
1272 The line with single-line comments removed.
1273 """
1274 commentpos = line.find('//')
1275 if commentpos != -1 and not IsCppString(line[:commentpos]):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001276 line = line[:commentpos].rstrip()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001277 # get rid of /* ... */
1278 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1279
1280
erg@google.com6317a9c2009-06-25 00:28:19 +00001281class CleansedLines(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001282 """Holds 3 copies of all lines with different preprocessing applied to them.
1283
1284 1) elided member contains lines without strings and comments,
1285 2) lines member contains lines without comments, and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001286 3) raw_lines member contains all the lines without processing.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001287 All these three members are of <type 'list'>, and of the same length.
1288 """
1289
1290 def __init__(self, lines):
1291 self.elided = []
1292 self.lines = []
1293 self.raw_lines = lines
1294 self.num_lines = len(lines)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001295 self.lines_without_raw_strings = CleanseRawStrings(lines)
1296 for linenum in range(len(self.lines_without_raw_strings)):
1297 self.lines.append(CleanseComments(
1298 self.lines_without_raw_strings[linenum]))
1299 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001300 self.elided.append(CleanseComments(elided))
1301
1302 def NumLines(self):
1303 """Returns the number of lines represented."""
1304 return self.num_lines
1305
1306 @staticmethod
1307 def _CollapseStrings(elided):
1308 """Collapses strings and chars on a line to simple "" or '' blocks.
1309
1310 We nix strings first so we're not fooled by text like '"http://"'
1311
1312 Args:
1313 elided: The line being processed.
1314
1315 Returns:
1316 The line with collapsed strings.
1317 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001318 if _RE_PATTERN_INCLUDE.match(elided):
1319 return elided
1320
1321 # Remove escaped characters first to make quote/single quote collapsing
1322 # basic. Things that look like escaped characters shouldn't occur
1323 # outside of strings and chars.
1324 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1325
1326 # Replace quoted strings and digit separators. Both single quotes
1327 # and double quotes are processed in the same loop, otherwise
1328 # nested quotes wouldn't work.
1329 collapsed = ''
1330 while True:
1331 # Find the first quote character
1332 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1333 if not match:
1334 collapsed += elided
1335 break
1336 head, quote, tail = match.groups()
1337
1338 if quote == '"':
1339 # Collapse double quoted strings
1340 second_quote = tail.find('"')
1341 if second_quote >= 0:
1342 collapsed += head + '""'
1343 elided = tail[second_quote + 1:]
1344 else:
1345 # Unmatched double quote, don't bother processing the rest
1346 # of the line since this is probably a multiline string.
1347 collapsed += elided
1348 break
1349 else:
1350 # Found single quote, check nearby text to eliminate digit separators.
1351 #
1352 # There is no special handling for floating point here, because
1353 # the integer/fractional/exponent parts would all be parsed
1354 # correctly as long as there are digits on both sides of the
1355 # separator. So we are fine as long as we don't see something
1356 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1357 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1358 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1359 collapsed += head + match_literal.group(1).replace("'", '')
1360 elided = match_literal.group(2)
1361 else:
1362 second_quote = tail.find('\'')
1363 if second_quote >= 0:
1364 collapsed += head + "''"
1365 elided = tail[second_quote + 1:]
1366 else:
1367 # Unmatched single quote
1368 collapsed += elided
1369 break
1370
1371 return collapsed
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001372
1373
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001374def FindEndOfExpressionInLine(line, startpos, stack):
1375 """Find the position just after the end of current parenthesized expression.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001376
1377 Args:
1378 line: a CleansedLines line.
1379 startpos: start searching at this position.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001380 stack: nesting stack at startpos.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001381
1382 Returns:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001383 On finding matching end: (index just after matching end, None)
1384 On finding an unclosed expression: (-1, None)
1385 Otherwise: (-1, new stack at end of this line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001386 """
1387 for i in xrange(startpos, len(line)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001388 char = line[i]
1389 if char in '([{':
1390 # Found start of parenthesized expression, push to expression stack
1391 stack.append(char)
1392 elif char == '<':
1393 # Found potential start of template argument list
1394 if i > 0 and line[i - 1] == '<':
1395 # Left shift operator
1396 if stack and stack[-1] == '<':
1397 stack.pop()
1398 if not stack:
1399 return (-1, None)
1400 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1401 # operator<, don't add to stack
1402 continue
1403 else:
1404 # Tentative start of template argument list
1405 stack.append('<')
1406 elif char in ')]}':
1407 # Found end of parenthesized expression.
1408 #
1409 # If we are currently expecting a matching '>', the pending '<'
1410 # must have been an operator. Remove them from expression stack.
1411 while stack and stack[-1] == '<':
1412 stack.pop()
1413 if not stack:
1414 return (-1, None)
1415 if ((stack[-1] == '(' and char == ')') or
1416 (stack[-1] == '[' and char == ']') or
1417 (stack[-1] == '{' and char == '}')):
1418 stack.pop()
1419 if not stack:
1420 return (i + 1, None)
1421 else:
1422 # Mismatched parentheses
1423 return (-1, None)
1424 elif char == '>':
1425 # Found potential end of template argument list.
1426
1427 # Ignore "->" and operator functions
1428 if (i > 0 and
1429 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1430 continue
1431
1432 # Pop the stack if there is a matching '<'. Otherwise, ignore
1433 # this '>' since it must be an operator.
1434 if stack:
1435 if stack[-1] == '<':
1436 stack.pop()
1437 if not stack:
1438 return (i + 1, None)
1439 elif char == ';':
1440 # Found something that look like end of statements. If we are currently
1441 # expecting a '>', the matching '<' must have been an operator, since
1442 # template argument list should not contain statements.
1443 while stack and stack[-1] == '<':
1444 stack.pop()
1445 if not stack:
1446 return (-1, None)
1447
1448 # Did not find end of expression or unbalanced parentheses on this line
1449 return (-1, stack)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001450
1451
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001452def CloseExpression(clean_lines, linenum, pos):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001453 """If input points to ( or { or [ or <, finds the position that closes it.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001454
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001455 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001456 linenum/pos that correspond to the closing of the expression.
1457
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001458 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1459 Ideally we would want to index all opening and closing parentheses once
1460 and have CloseExpression be just a simple lookup, but due to preprocessor
1461 tricks, this is not so easy.
1462
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001463 Args:
1464 clean_lines: A CleansedLines instance containing the file.
1465 linenum: The number of the line to check.
1466 pos: A position on the line.
1467
1468 Returns:
1469 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1470 (line, len(lines), -1) if we never find a close. Note we ignore
1471 strings and comments when matching; and the line we return is the
1472 'cleansed' line at linenum.
1473 """
1474
1475 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001476 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001477 return (line, clean_lines.NumLines(), -1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001478
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001479 # Check first line
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001480 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001481 if end_pos > -1:
1482 return (line, linenum, end_pos)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001483
1484 # Continue scanning forward
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001485 while stack and linenum < clean_lines.NumLines() - 1:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001486 linenum += 1
1487 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001488 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001489 if end_pos > -1:
1490 return (line, linenum, end_pos)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001491
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001492 # Did not find end of expression before end of file, give up
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001493 return (line, clean_lines.NumLines(), -1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001494
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001495
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001496def FindStartOfExpressionInLine(line, endpos, stack):
1497 """Find position at the matching start of current expression.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001498
1499 This is almost the reverse of FindEndOfExpressionInLine, but note
1500 that the input position and returned position differs by 1.
1501
1502 Args:
1503 line: a CleansedLines line.
1504 endpos: start searching at this position.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001505 stack: nesting stack at endpos.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001506
1507 Returns:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001508 On finding matching start: (index at matching start, None)
1509 On finding an unclosed expression: (-1, None)
1510 Otherwise: (-1, new stack at beginning of this line)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001511 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001512 i = endpos
1513 while i >= 0:
1514 char = line[i]
1515 if char in ')]}':
1516 # Found end of expression, push to expression stack
1517 stack.append(char)
1518 elif char == '>':
1519 # Found potential end of template argument list.
1520 #
1521 # Ignore it if it's a "->" or ">=" or "operator>"
1522 if (i > 0 and
1523 (line[i - 1] == '-' or
1524 Match(r'\s>=\s', line[i - 1:]) or
1525 Search(r'\boperator\s*$', line[0:i]))):
1526 i -= 1
1527 else:
1528 stack.append('>')
1529 elif char == '<':
1530 # Found potential start of template argument list
1531 if i > 0 and line[i - 1] == '<':
1532 # Left shift operator
1533 i -= 1
1534 else:
1535 # If there is a matching '>', we can pop the expression stack.
1536 # Otherwise, ignore this '<' since it must be an operator.
1537 if stack and stack[-1] == '>':
1538 stack.pop()
1539 if not stack:
1540 return (i, None)
1541 elif char in '([{':
1542 # Found start of expression.
1543 #
1544 # If there are any unmatched '>' on the stack, they must be
1545 # operators. Remove those.
1546 while stack and stack[-1] == '>':
1547 stack.pop()
1548 if not stack:
1549 return (-1, None)
1550 if ((char == '(' and stack[-1] == ')') or
1551 (char == '[' and stack[-1] == ']') or
1552 (char == '{' and stack[-1] == '}')):
1553 stack.pop()
1554 if not stack:
1555 return (i, None)
1556 else:
1557 # Mismatched parentheses
1558 return (-1, None)
1559 elif char == ';':
1560 # Found something that look like end of statements. If we are currently
1561 # expecting a '<', the matching '>' must have been an operator, since
1562 # template argument list should not contain statements.
1563 while stack and stack[-1] == '>':
1564 stack.pop()
1565 if not stack:
1566 return (-1, None)
1567
1568 i -= 1
1569
1570 return (-1, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001571
1572
1573def ReverseCloseExpression(clean_lines, linenum, pos):
1574 """If input points to ) or } or ] or >, finds the position that opens it.
1575
1576 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1577 linenum/pos that correspond to the opening of the expression.
1578
1579 Args:
1580 clean_lines: A CleansedLines instance containing the file.
1581 linenum: The number of the line to check.
1582 pos: A position on the line.
1583
1584 Returns:
1585 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1586 (line, 0, -1) if we never find the matching opening brace. Note
1587 we ignore strings and comments when matching; and the line we
1588 return is the 'cleansed' line at linenum.
1589 """
1590 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001591 if line[pos] not in ')}]>':
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001592 return (line, 0, -1)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001593
1594 # Check last line
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001595 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001596 if start_pos > -1:
1597 return (line, linenum, start_pos)
1598
1599 # Continue scanning backward
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001600 while stack and linenum > 0:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001601 linenum -= 1
1602 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001603 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001604 if start_pos > -1:
1605 return (line, linenum, start_pos)
1606
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001607 # Did not find start of expression before beginning of file, give up
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001608 return (line, 0, -1)
1609
1610
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001611def CheckForCopyright(filename, lines, error):
1612 """Logs an error if no Copyright message appears at the top of the file."""
1613
1614 # We'll say it should occur by line 10. Don't forget there's a
1615 # dummy line at the front.
1616 for line in xrange(1, min(len(lines), 11)):
1617 if re.search(r'Copyright', lines[line], re.I): break
1618 else: # means no copyright line was found
1619 error(filename, 0, 'legal/copyright', 5,
1620 'No copyright message found. '
1621 'You should have a line: "Copyright [year] <Copyright Owner>"')
1622
1623
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001624def GetIndentLevel(line):
1625 """Return the number of leading spaces in line.
1626
1627 Args:
1628 line: A string to check.
1629
1630 Returns:
1631 An integer count of leading spaces, possibly zero.
1632 """
1633 indent = Match(r'^( *)\S', line)
1634 if indent:
1635 return len(indent.group(1))
1636 else:
1637 return 0
1638
1639
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001640def GetHeaderGuardCPPVariable(filename):
1641 """Returns the CPP variable that should be used as a header guard.
1642
1643 Args:
1644 filename: The name of a C++ header file.
1645
1646 Returns:
1647 The CPP variable that should be used as a header guard in the
1648 named file.
1649
1650 """
1651
erg@google.com35589e62010-11-17 18:58:16 +00001652 # Restores original filename in case that cpplint is invoked from Emacs's
1653 # flymake.
1654 filename = re.sub(r'_flymake\.h$', '.h', filename)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001655 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
erg@google.com35589e62010-11-17 18:58:16 +00001656
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001657 fileinfo = FileInfo(filename)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001658 file_path_from_root = fileinfo.RepositoryName()
1659 if _root:
1660 file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root)
1661 return re.sub(r'[-./\s]', '_', file_path_from_root).upper() + '_'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001662
1663
1664def CheckForHeaderGuard(filename, lines, error):
1665 """Checks that the file contains a header guard.
1666
erg@google.com6317a9c2009-06-25 00:28:19 +00001667 Logs an error if no #ifndef header guard is present. For other
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001668 headers, checks that the full pathname is used.
1669
1670 Args:
1671 filename: The name of the C++ header file.
1672 lines: An array of strings, each representing a line of the file.
1673 error: The function to call with any errors found.
1674 """
1675
avakulenko@google.com59146752014-08-11 20:20:55 +00001676 # Don't check for header guards if there are error suppression
1677 # comments somewhere in this file.
1678 #
1679 # Because this is silencing a warning for a nonexistent line, we
1680 # only support the very specific NOLINT(build/header_guard) syntax,
1681 # and not the general NOLINT or NOLINT(*) syntax.
1682 for i in lines:
1683 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1684 return
1685
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001686 cppvar = GetHeaderGuardCPPVariable(filename)
1687
1688 ifndef = None
1689 ifndef_linenum = 0
1690 define = None
1691 endif = None
1692 endif_linenum = 0
1693 for linenum, line in enumerate(lines):
1694 linesplit = line.split()
1695 if len(linesplit) >= 2:
1696 # find the first occurrence of #ifndef and #define, save arg
1697 if not ifndef and linesplit[0] == '#ifndef':
1698 # set ifndef to the header guard presented on the #ifndef line.
1699 ifndef = linesplit[1]
1700 ifndef_linenum = linenum
1701 if not define and linesplit[0] == '#define':
1702 define = linesplit[1]
1703 # find the last occurrence of #endif, save entire line
1704 if line.startswith('#endif'):
1705 endif = line
1706 endif_linenum = linenum
1707
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001708 if not ifndef:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001709 error(filename, 0, 'build/header_guard', 5,
1710 'No #ifndef header guard found, suggested CPP variable is: %s' %
1711 cppvar)
1712 return
1713
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001714 if not define:
1715 error(filename, 0, 'build/header_guard', 5,
1716 'No #define header guard found, suggested CPP variable is: %s' %
1717 cppvar)
1718 return
1719
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001720 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1721 # for backward compatibility.
erg@google.com35589e62010-11-17 18:58:16 +00001722 if ifndef != cppvar:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001723 error_level = 0
1724 if ifndef != cppvar + '_':
1725 error_level = 5
1726
erg@google.com35589e62010-11-17 18:58:16 +00001727 ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum,
1728 error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001729 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1730 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1731
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001732 if define != ifndef:
1733 error(filename, 0, 'build/header_guard', 5,
1734 '#ifndef and #define don\'t match, suggested CPP variable is: %s' %
1735 cppvar)
1736 return
1737
erg@google.com35589e62010-11-17 18:58:16 +00001738 if endif != ('#endif // %s' % cppvar):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001739 error_level = 0
1740 if endif != ('#endif // %s' % (cppvar + '_')):
1741 error_level = 5
1742
erg@google.com35589e62010-11-17 18:58:16 +00001743 ParseNolintSuppressions(filename, lines[endif_linenum], endif_linenum,
1744 error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001745 error(filename, endif_linenum, 'build/header_guard', error_level,
1746 '#endif line should be "#endif // %s"' % cppvar)
1747
1748
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001749def CheckForBadCharacters(filename, lines, error):
1750 """Logs an error for each line containing bad characters.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001751
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001752 Two kinds of bad characters:
1753
1754 1. Unicode replacement characters: These indicate that either the file
1755 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1756 it shouldn't). Note that it's possible for this to throw off line
1757 numbering if the invalid UTF-8 occurred adjacent to a newline.
1758
1759 2. NUL bytes. These are problematic for some tools.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001760
1761 Args:
1762 filename: The name of the current file.
1763 lines: An array of strings, each representing a line of the file.
1764 error: The function to call with any errors found.
1765 """
1766 for linenum, line in enumerate(lines):
1767 if u'\ufffd' in line:
1768 error(filename, linenum, 'readability/utf8', 5,
1769 'Line contains invalid UTF-8 (or Unicode replacement character).')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001770 if '\0' in line:
1771 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001772
1773
1774def CheckForNewlineAtEOF(filename, lines, error):
1775 """Logs an error if there is no newline char at the end of the file.
1776
1777 Args:
1778 filename: The name of the current file.
1779 lines: An array of strings, each representing a line of the file.
1780 error: The function to call with any errors found.
1781 """
1782
1783 # The array lines() was created by adding two newlines to the
1784 # original file (go figure), then splitting on \n.
1785 # To verify that the file ends in \n, we just have to make sure the
1786 # last-but-two element of lines() exists and is empty.
1787 if len(lines) < 3 or lines[-2]:
1788 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1789 'Could not find a newline character at the end of the file.')
1790
1791
1792def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1793 """Logs an error if we see /* ... */ or "..." that extend past one line.
1794
1795 /* ... */ comments are legit inside macros, for one line.
1796 Otherwise, we prefer // comments, so it's ok to warn about the
1797 other. Likewise, it's ok for strings to extend across multiple
1798 lines, as long as a line continuation character (backslash)
1799 terminates each line. Although not currently prohibited by the C++
1800 style guide, it's ugly and unnecessary. We don't do well with either
1801 in this lint program, so we warn about both.
1802
1803 Args:
1804 filename: The name of the current file.
1805 clean_lines: A CleansedLines instance containing the file.
1806 linenum: The number of the line to check.
1807 error: The function to call with any errors found.
1808 """
1809 line = clean_lines.elided[linenum]
1810
1811 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1812 # second (escaped) slash may trigger later \" detection erroneously.
1813 line = line.replace('\\\\', '')
1814
1815 if line.count('/*') > line.count('*/'):
1816 error(filename, linenum, 'readability/multiline_comment', 5,
1817 'Complex multi-line /*...*/-style comment found. '
1818 'Lint may give bogus warnings. '
1819 'Consider replacing these with //-style comments, '
1820 'with #if 0...#endif, '
1821 'or with more clearly structured multi-line comments.')
1822
1823 if (line.count('"') - line.count('\\"')) % 2:
1824 error(filename, linenum, 'readability/multiline_string', 5,
1825 'Multi-line string ("...") found. This lint script doesn\'t '
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001826 'do well with such strings, and may give bogus warnings. '
1827 'Use C++11 raw strings or concatenation instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001828
1829
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001830# (non-threadsafe name, thread-safe alternative, validation pattern)
1831#
1832# The validation pattern is used to eliminate false positives such as:
1833# _rand(); // false positive due to substring match.
1834# ->rand(); // some member function rand().
1835# ACMRandom rand(seed); // some variable named rand.
1836# ISAACRandom rand(); // another variable named rand.
1837#
1838# Basically we require the return value of these functions to be used
1839# in some expression context on the same line by matching on some
1840# operator before the function name. This eliminates constructors and
1841# member function calls.
1842_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
1843_THREADING_LIST = (
1844 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
1845 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
1846 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
1847 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
1848 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
1849 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
1850 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
1851 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
1852 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
1853 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
1854 ('strtok(', 'strtok_r(',
1855 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
1856 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001857 )
1858
1859
1860def CheckPosixThreading(filename, clean_lines, linenum, error):
1861 """Checks for calls to thread-unsafe functions.
1862
1863 Much code has been originally written without consideration of
1864 multi-threading. Also, engineers are relying on their old experience;
1865 they have learned posix before threading extensions were added. These
1866 tests guide the engineers to use thread-safe functions (when using
1867 posix directly).
1868
1869 Args:
1870 filename: The name of the current file.
1871 clean_lines: A CleansedLines instance containing the file.
1872 linenum: The number of the line to check.
1873 error: The function to call with any errors found.
1874 """
1875 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001876 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
1877 # Additional pattern matching check to confirm that this is the
1878 # function we are looking for
1879 if Search(pattern, line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001880 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001881 'Consider using ' + multithread_safe_func +
1882 '...) instead of ' + single_thread_func +
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001883 '...) for improved thread safety.')
1884
1885
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001886def CheckVlogArguments(filename, clean_lines, linenum, error):
1887 """Checks that VLOG() is only used for defining a logging level.
1888
1889 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
1890 VLOG(FATAL) are not.
1891
1892 Args:
1893 filename: The name of the current file.
1894 clean_lines: A CleansedLines instance containing the file.
1895 linenum: The number of the line to check.
1896 error: The function to call with any errors found.
1897 """
1898 line = clean_lines.elided[linenum]
1899 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
1900 error(filename, linenum, 'runtime/vlog', 5,
1901 'VLOG() should be used with numeric verbosity level. '
1902 'Use LOG() if you want symbolic severity levels.')
1903
erg@google.com26970fa2009-11-17 18:07:32 +00001904# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com6317a9c2009-06-25 00:28:19 +00001905# incrementing a value.
erg@google.com26970fa2009-11-17 18:07:32 +00001906_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com6317a9c2009-06-25 00:28:19 +00001907 r'^\s*\*\w+(\+\+|--);')
1908
1909
1910def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.com26970fa2009-11-17 18:07:32 +00001911 """Checks for invalid increment *count++.
erg@google.com6317a9c2009-06-25 00:28:19 +00001912
1913 For example following function:
1914 void increment_counter(int* count) {
1915 *count++;
1916 }
1917 is invalid, because it effectively does count++, moving pointer, and should
1918 be replaced with ++*count, (*count)++ or *count += 1.
1919
1920 Args:
1921 filename: The name of the current file.
1922 clean_lines: A CleansedLines instance containing the file.
1923 linenum: The number of the line to check.
1924 error: The function to call with any errors found.
1925 """
1926 line = clean_lines.elided[linenum]
erg@google.com26970fa2009-11-17 18:07:32 +00001927 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com6317a9c2009-06-25 00:28:19 +00001928 error(filename, linenum, 'runtime/invalid_increment', 5,
1929 'Changing pointer instead of value (or unused value of operator*).')
1930
1931
avakulenko@google.com59146752014-08-11 20:20:55 +00001932def IsMacroDefinition(clean_lines, linenum):
1933 if Search(r'^#define', clean_lines[linenum]):
1934 return True
1935
1936 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
1937 return True
1938
1939 return False
1940
1941
1942def IsForwardClassDeclaration(clean_lines, linenum):
1943 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
1944
1945
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001946class _BlockInfo(object):
1947 """Stores information about a generic block of code."""
1948
1949 def __init__(self, seen_open_brace):
1950 self.seen_open_brace = seen_open_brace
1951 self.open_parentheses = 0
1952 self.inline_asm = _NO_ASM
avakulenko@google.com59146752014-08-11 20:20:55 +00001953 self.check_namespace_indentation = False
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001954
1955 def CheckBegin(self, filename, clean_lines, linenum, error):
1956 """Run checks that applies to text up to the opening brace.
1957
1958 This is mostly for checking the text after the class identifier
1959 and the "{", usually where the base class is specified. For other
1960 blocks, there isn't much to check, so we always pass.
1961
1962 Args:
1963 filename: The name of the current file.
1964 clean_lines: A CleansedLines instance containing the file.
1965 linenum: The number of the line to check.
1966 error: The function to call with any errors found.
1967 """
1968 pass
1969
1970 def CheckEnd(self, filename, clean_lines, linenum, error):
1971 """Run checks that applies to text after the closing brace.
1972
1973 This is mostly used for checking end of namespace comments.
1974
1975 Args:
1976 filename: The name of the current file.
1977 clean_lines: A CleansedLines instance containing the file.
1978 linenum: The number of the line to check.
1979 error: The function to call with any errors found.
1980 """
1981 pass
1982
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001983 def IsBlockInfo(self):
1984 """Returns true if this block is a _BlockInfo.
1985
1986 This is convenient for verifying that an object is an instance of
1987 a _BlockInfo, but not an instance of any of the derived classes.
1988
1989 Returns:
1990 True for this class, False for derived classes.
1991 """
1992 return self.__class__ == _BlockInfo
1993
1994
1995class _ExternCInfo(_BlockInfo):
1996 """Stores information about an 'extern "C"' block."""
1997
1998 def __init__(self):
1999 _BlockInfo.__init__(self, True)
2000
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002001
2002class _ClassInfo(_BlockInfo):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002003 """Stores information about a class."""
2004
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002005 def __init__(self, name, class_or_struct, clean_lines, linenum):
2006 _BlockInfo.__init__(self, False)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002007 self.name = name
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002008 self.starting_linenum = linenum
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002009 self.is_derived = False
avakulenko@google.com59146752014-08-11 20:20:55 +00002010 self.check_namespace_indentation = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002011 if class_or_struct == 'struct':
2012 self.access = 'public'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002013 self.is_struct = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002014 else:
2015 self.access = 'private'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002016 self.is_struct = False
2017
2018 # Remember initial indentation level for this class. Using raw_lines here
2019 # instead of elided to account for leading comments.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002020 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002021
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002022 # Try to find the end of the class. This will be confused by things like:
2023 # class A {
2024 # } *x = { ...
2025 #
2026 # But it's still good enough for CheckSectionSpacing.
2027 self.last_line = 0
2028 depth = 0
2029 for i in range(linenum, clean_lines.NumLines()):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002030 line = clean_lines.elided[i]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002031 depth += line.count('{') - line.count('}')
2032 if not depth:
2033 self.last_line = i
2034 break
2035
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002036 def CheckBegin(self, filename, clean_lines, linenum, error):
2037 # Look for a bare ':'
2038 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2039 self.is_derived = True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002040
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002041 def CheckEnd(self, filename, clean_lines, linenum, error):
2042 # Check that closing brace is aligned with beginning of the class.
2043 # Only do this if the closing brace is indented by only whitespaces.
2044 # This means we will not check single-line class definitions.
2045 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2046 if indent and len(indent.group(1)) != self.class_indent:
2047 if self.is_struct:
2048 parent = 'struct ' + self.name
2049 else:
2050 parent = 'class ' + self.name
2051 error(filename, linenum, 'whitespace/indent', 3,
2052 'Closing brace should be aligned with beginning of %s' % parent)
2053
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002054
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002055class _NamespaceInfo(_BlockInfo):
2056 """Stores information about a namespace."""
2057
2058 def __init__(self, name, linenum):
2059 _BlockInfo.__init__(self, False)
2060 self.name = name or ''
2061 self.starting_linenum = linenum
avakulenko@google.com59146752014-08-11 20:20:55 +00002062 self.check_namespace_indentation = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002063
2064 def CheckEnd(self, filename, clean_lines, linenum, error):
2065 """Check end of namespace comments."""
2066 line = clean_lines.raw_lines[linenum]
2067
2068 # Check how many lines is enclosed in this namespace. Don't issue
2069 # warning for missing namespace comments if there aren't enough
2070 # lines. However, do apply checks if there is already an end of
2071 # namespace comment and it's incorrect.
2072 #
2073 # TODO(unknown): We always want to check end of namespace comments
2074 # if a namespace is large, but sometimes we also want to apply the
2075 # check if a short namespace contained nontrivial things (something
2076 # other than forward declarations). There is currently no logic on
2077 # deciding what these nontrivial things are, so this check is
2078 # triggered by namespace size only, which works most of the time.
2079 if (linenum - self.starting_linenum < 10
2080 and not Match(r'};*\s*(//|/\*).*\bnamespace\b', line)):
2081 return
2082
2083 # Look for matching comment at end of namespace.
2084 #
2085 # Note that we accept C style "/* */" comments for terminating
2086 # namespaces, so that code that terminate namespaces inside
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002087 # preprocessor macros can be cpplint clean.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002088 #
2089 # We also accept stuff like "// end of namespace <name>." with the
2090 # period at the end.
2091 #
2092 # Besides these, we don't accept anything else, otherwise we might
2093 # get false negatives when existing comment is a substring of the
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002094 # expected namespace.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002095 if self.name:
2096 # Named namespace
2097 if not Match((r'};*\s*(//|/\*).*\bnamespace\s+' + re.escape(self.name) +
2098 r'[\*/\.\\\s]*$'),
2099 line):
2100 error(filename, linenum, 'readability/namespace', 5,
2101 'Namespace should be terminated with "// namespace %s"' %
2102 self.name)
2103 else:
2104 # Anonymous namespace
2105 if not Match(r'};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002106 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2107 # mention "// anonymous namespace" as an acceptable form
2108 if Match(r'}.*\b(namespace anonymous|anonymous namespace)\b', line):
2109 error(filename, linenum, 'readability/namespace', 5,
2110 'Anonymous namespace should be terminated with "// namespace"'
2111 ' or "// anonymous namespace"')
2112 else:
2113 error(filename, linenum, 'readability/namespace', 5,
2114 'Anonymous namespace should be terminated with "// namespace"')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002115
2116
2117class _PreprocessorInfo(object):
2118 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2119
2120 def __init__(self, stack_before_if):
2121 # The entire nesting stack before #if
2122 self.stack_before_if = stack_before_if
2123
2124 # The entire nesting stack up to #else
2125 self.stack_before_else = []
2126
2127 # Whether we have already seen #else or #elif
2128 self.seen_else = False
2129
2130
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002131class NestingState(object):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002132 """Holds states related to parsing braces."""
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002133
2134 def __init__(self):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002135 # Stack for tracking all braces. An object is pushed whenever we
2136 # see a "{", and popped when we see a "}". Only 3 types of
2137 # objects are possible:
2138 # - _ClassInfo: a class or struct.
2139 # - _NamespaceInfo: a namespace.
2140 # - _BlockInfo: some other type of block.
2141 self.stack = []
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002142
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002143 # Top of the previous stack before each Update().
2144 #
2145 # Because the nesting_stack is updated at the end of each line, we
2146 # had to do some convoluted checks to find out what is the current
2147 # scope at the beginning of the line. This check is simplified by
2148 # saving the previous top of nesting stack.
2149 #
2150 # We could save the full stack, but we only need the top. Copying
2151 # the full nesting stack would slow down cpplint by ~10%.
2152 self.previous_stack_top = []
2153
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002154 # Stack of _PreprocessorInfo objects.
2155 self.pp_stack = []
2156
2157 def SeenOpenBrace(self):
2158 """Check if we have seen the opening brace for the innermost block.
2159
2160 Returns:
2161 True if we have seen the opening brace, False if the innermost
2162 block is still expecting an opening brace.
2163 """
2164 return (not self.stack) or self.stack[-1].seen_open_brace
2165
2166 def InNamespaceBody(self):
2167 """Check if we are currently one level inside a namespace body.
2168
2169 Returns:
2170 True if top of the stack is a namespace block, False otherwise.
2171 """
2172 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2173
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002174 def InExternC(self):
2175 """Check if we are currently one level inside an 'extern "C"' block.
2176
2177 Returns:
2178 True if top of the stack is an extern block, False otherwise.
2179 """
2180 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2181
2182 def InClassDeclaration(self):
2183 """Check if we are currently one level inside a class or struct declaration.
2184
2185 Returns:
2186 True if top of the stack is a class/struct, False otherwise.
2187 """
2188 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2189
2190 def InAsmBlock(self):
2191 """Check if we are currently one level inside an inline ASM block.
2192
2193 Returns:
2194 True if the top of the stack is a block containing inline ASM.
2195 """
2196 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2197
2198 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2199 """Check if current position is inside template argument list.
2200
2201 Args:
2202 clean_lines: A CleansedLines instance containing the file.
2203 linenum: The number of the line to check.
2204 pos: position just after the suspected template argument.
2205 Returns:
2206 True if (linenum, pos) is inside template arguments.
2207 """
2208 while linenum < clean_lines.NumLines():
2209 # Find the earliest character that might indicate a template argument
2210 line = clean_lines.elided[linenum]
2211 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2212 if not match:
2213 linenum += 1
2214 pos = 0
2215 continue
2216 token = match.group(1)
2217 pos += len(match.group(0))
2218
2219 # These things do not look like template argument list:
2220 # class Suspect {
2221 # class Suspect x; }
2222 if token in ('{', '}', ';'): return False
2223
2224 # These things look like template argument list:
2225 # template <class Suspect>
2226 # template <class Suspect = default_value>
2227 # template <class Suspect[]>
2228 # template <class Suspect...>
2229 if token in ('>', '=', '[', ']', '.'): return True
2230
2231 # Check if token is an unmatched '<'.
2232 # If not, move on to the next character.
2233 if token != '<':
2234 pos += 1
2235 if pos >= len(line):
2236 linenum += 1
2237 pos = 0
2238 continue
2239
2240 # We can't be sure if we just find a single '<', and need to
2241 # find the matching '>'.
2242 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2243 if end_pos < 0:
2244 # Not sure if template argument list or syntax error in file
2245 return False
2246 linenum = end_line
2247 pos = end_pos
2248 return False
2249
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002250 def UpdatePreprocessor(self, line):
2251 """Update preprocessor stack.
2252
2253 We need to handle preprocessors due to classes like this:
2254 #ifdef SWIG
2255 struct ResultDetailsPageElementExtensionPoint {
2256 #else
2257 struct ResultDetailsPageElementExtensionPoint : public Extension {
2258 #endif
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002259
2260 We make the following assumptions (good enough for most files):
2261 - Preprocessor condition evaluates to true from #if up to first
2262 #else/#elif/#endif.
2263
2264 - Preprocessor condition evaluates to false from #else/#elif up
2265 to #endif. We still perform lint checks on these lines, but
2266 these do not affect nesting stack.
2267
2268 Args:
2269 line: current line to check.
2270 """
2271 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2272 # Beginning of #if block, save the nesting stack here. The saved
2273 # stack will allow us to restore the parsing state in the #else case.
2274 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2275 elif Match(r'^\s*#\s*(else|elif)\b', line):
2276 # Beginning of #else block
2277 if self.pp_stack:
2278 if not self.pp_stack[-1].seen_else:
2279 # This is the first #else or #elif block. Remember the
2280 # whole nesting stack up to this point. This is what we
2281 # keep after the #endif.
2282 self.pp_stack[-1].seen_else = True
2283 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2284
2285 # Restore the stack to how it was before the #if
2286 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2287 else:
2288 # TODO(unknown): unexpected #else, issue warning?
2289 pass
2290 elif Match(r'^\s*#\s*endif\b', line):
2291 # End of #if or #else blocks.
2292 if self.pp_stack:
2293 # If we saw an #else, we will need to restore the nesting
2294 # stack to its former state before the #else, otherwise we
2295 # will just continue from where we left off.
2296 if self.pp_stack[-1].seen_else:
2297 # Here we can just use a shallow copy since we are the last
2298 # reference to it.
2299 self.stack = self.pp_stack[-1].stack_before_else
2300 # Drop the corresponding #if
2301 self.pp_stack.pop()
2302 else:
2303 # TODO(unknown): unexpected #endif, issue warning?
2304 pass
2305
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002306 # TODO(unknown): Update() is too long, but we will refactor later.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002307 def Update(self, filename, clean_lines, linenum, error):
2308 """Update nesting state with current line.
2309
2310 Args:
2311 filename: The name of the current file.
2312 clean_lines: A CleansedLines instance containing the file.
2313 linenum: The number of the line to check.
2314 error: The function to call with any errors found.
2315 """
2316 line = clean_lines.elided[linenum]
2317
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002318 # Remember top of the previous nesting stack.
2319 #
2320 # The stack is always pushed/popped and not modified in place, so
2321 # we can just do a shallow copy instead of copy.deepcopy. Using
2322 # deepcopy would slow down cpplint by ~28%.
2323 if self.stack:
2324 self.previous_stack_top = self.stack[-1]
2325 else:
2326 self.previous_stack_top = None
2327
2328 # Update pp_stack
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002329 self.UpdatePreprocessor(line)
2330
2331 # Count parentheses. This is to avoid adding struct arguments to
2332 # the nesting stack.
2333 if self.stack:
2334 inner_block = self.stack[-1]
2335 depth_change = line.count('(') - line.count(')')
2336 inner_block.open_parentheses += depth_change
2337
2338 # Also check if we are starting or ending an inline assembly block.
2339 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2340 if (depth_change != 0 and
2341 inner_block.open_parentheses == 1 and
2342 _MATCH_ASM.match(line)):
2343 # Enter assembly block
2344 inner_block.inline_asm = _INSIDE_ASM
2345 else:
2346 # Not entering assembly block. If previous line was _END_ASM,
2347 # we will now shift to _NO_ASM state.
2348 inner_block.inline_asm = _NO_ASM
2349 elif (inner_block.inline_asm == _INSIDE_ASM and
2350 inner_block.open_parentheses == 0):
2351 # Exit assembly block
2352 inner_block.inline_asm = _END_ASM
2353
2354 # Consume namespace declaration at the beginning of the line. Do
2355 # this in a loop so that we catch same line declarations like this:
2356 # namespace proto2 { namespace bridge { class MessageSet; } }
2357 while True:
2358 # Match start of namespace. The "\b\s*" below catches namespace
2359 # declarations even if it weren't followed by a whitespace, this
2360 # is so that we don't confuse our namespace checker. The
2361 # missing spaces will be flagged by CheckSpacing.
2362 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2363 if not namespace_decl_match:
2364 break
2365
2366 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2367 self.stack.append(new_namespace)
2368
2369 line = namespace_decl_match.group(2)
2370 if line.find('{') != -1:
2371 new_namespace.seen_open_brace = True
2372 line = line[line.find('{') + 1:]
2373
2374 # Look for a class declaration in whatever is left of the line
2375 # after parsing namespaces. The regexp accounts for decorated classes
2376 # such as in:
2377 # class LOCKABLE API Object {
2378 # };
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002379 class_decl_match = Match(
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002380 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2381 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2382 r'(.*)$', line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002383 if (class_decl_match and
2384 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002385 # We do not want to accept classes that are actually template arguments:
2386 # template <class Ignore1,
2387 # class Ignore2 = Default<Args>,
2388 # template <Args> class Ignore3>
2389 # void Function() {};
2390 #
2391 # To avoid template argument cases, we scan forward and look for
2392 # an unmatched '>'. If we see one, assume we are inside a
2393 # template argument list.
2394 end_declaration = len(class_decl_match.group(1))
2395 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2396 self.stack.append(_ClassInfo(
2397 class_decl_match.group(3), class_decl_match.group(2),
2398 clean_lines, linenum))
2399 line = class_decl_match.group(4)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002400
2401 # If we have not yet seen the opening brace for the innermost block,
2402 # run checks here.
2403 if not self.SeenOpenBrace():
2404 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2405
2406 # Update access control if we are inside a class/struct
2407 if self.stack and isinstance(self.stack[-1], _ClassInfo):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002408 classinfo = self.stack[-1]
2409 access_match = Match(
2410 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2411 r':(?:[^:]|$)',
2412 line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002413 if access_match:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002414 classinfo.access = access_match.group(2)
2415
2416 # Check that access keywords are indented +1 space. Skip this
2417 # check if the keywords are not preceded by whitespaces.
2418 indent = access_match.group(1)
2419 if (len(indent) != classinfo.class_indent + 1 and
2420 Match(r'^\s*$', indent)):
2421 if classinfo.is_struct:
2422 parent = 'struct ' + classinfo.name
2423 else:
2424 parent = 'class ' + classinfo.name
2425 slots = ''
2426 if access_match.group(3):
2427 slots = access_match.group(3)
2428 error(filename, linenum, 'whitespace/indent', 3,
2429 '%s%s: should be indented +1 space inside %s' % (
2430 access_match.group(2), slots, parent))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002431
2432 # Consume braces or semicolons from what's left of the line
2433 while True:
2434 # Match first brace, semicolon, or closed parenthesis.
2435 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2436 if not matched:
2437 break
2438
2439 token = matched.group(1)
2440 if token == '{':
2441 # If namespace or class hasn't seen a opening brace yet, mark
2442 # namespace/class head as complete. Push a new block onto the
2443 # stack otherwise.
2444 if not self.SeenOpenBrace():
2445 self.stack[-1].seen_open_brace = True
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002446 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
2447 self.stack.append(_ExternCInfo())
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002448 else:
2449 self.stack.append(_BlockInfo(True))
2450 if _MATCH_ASM.match(line):
2451 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002452
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002453 elif token == ';' or token == ')':
2454 # If we haven't seen an opening brace yet, but we already saw
2455 # a semicolon, this is probably a forward declaration. Pop
2456 # the stack for these.
2457 #
2458 # Similarly, if we haven't seen an opening brace yet, but we
2459 # already saw a closing parenthesis, then these are probably
2460 # function arguments with extra "class" or "struct" keywords.
2461 # Also pop these stack for these.
2462 if not self.SeenOpenBrace():
2463 self.stack.pop()
2464 else: # token == '}'
2465 # Perform end of block checks and pop the stack.
2466 if self.stack:
2467 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2468 self.stack.pop()
2469 line = matched.group(2)
2470
2471 def InnermostClass(self):
2472 """Get class info on the top of the stack.
2473
2474 Returns:
2475 A _ClassInfo object if we are inside a class, or None otherwise.
2476 """
2477 for i in range(len(self.stack), 0, -1):
2478 classinfo = self.stack[i - 1]
2479 if isinstance(classinfo, _ClassInfo):
2480 return classinfo
2481 return None
2482
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002483 def CheckCompletedBlocks(self, filename, error):
2484 """Checks that all classes and namespaces have been completely parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002485
2486 Call this when all lines in a file have been processed.
2487 Args:
2488 filename: The name of the current file.
2489 error: The function to call with any errors found.
2490 """
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002491 # Note: This test can result in false positives if #ifdef constructs
2492 # get in the way of brace matching. See the testBuildClass test in
2493 # cpplint_unittest.py for an example of this.
2494 for obj in self.stack:
2495 if isinstance(obj, _ClassInfo):
2496 error(filename, obj.starting_linenum, 'build/class', 5,
2497 'Failed to find complete declaration of class %s' %
2498 obj.name)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002499 elif isinstance(obj, _NamespaceInfo):
2500 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2501 'Failed to find complete declaration of namespace %s' %
2502 obj.name)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002503
2504
2505def CheckForNonStandardConstructs(filename, clean_lines, linenum,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002506 nesting_state, error):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002507 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002508
2509 Complain about several constructs which gcc-2 accepts, but which are
2510 not standard C++. Warning about these in lint is one way to ease the
2511 transition to new compilers.
2512 - put storage class first (e.g. "static const" instead of "const static").
2513 - "%lld" instead of %qd" in printf-type functions.
2514 - "%1$d" is non-standard in printf-type functions.
2515 - "\%" is an undefined character escape sequence.
2516 - text after #endif is not allowed.
2517 - invalid inner-style forward declaration.
2518 - >? and <? operators, and their >?= and <?= cousins.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002519
erg@google.com26970fa2009-11-17 18:07:32 +00002520 Additionally, check for constructor/destructor style violations and reference
2521 members, as it is very convenient to do so while checking for
2522 gcc-2 compliance.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002523
2524 Args:
2525 filename: The name of the current file.
2526 clean_lines: A CleansedLines instance containing the file.
2527 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002528 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002529 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002530 error: A callable to which errors are reported, which takes 4 arguments:
2531 filename, line number, error level, and message
2532 """
2533
2534 # Remove comments from the line, but leave in strings for now.
2535 line = clean_lines.lines[linenum]
2536
2537 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2538 error(filename, linenum, 'runtime/printf_format', 3,
2539 '%q in format strings is deprecated. Use %ll instead.')
2540
2541 if Search(r'printf\s*\(.*".*%\d+\$', line):
2542 error(filename, linenum, 'runtime/printf_format', 2,
2543 '%N$ formats are unconventional. Try rewriting to avoid them.')
2544
2545 # Remove escaped backslashes before looking for undefined escapes.
2546 line = line.replace('\\\\', '')
2547
2548 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2549 error(filename, linenum, 'build/printf_format', 3,
2550 '%, [, (, and { are undefined character escapes. Unescape them.')
2551
2552 # For the rest, work with both comments and strings removed.
2553 line = clean_lines.elided[linenum]
2554
2555 if Search(r'\b(const|volatile|void|char|short|int|long'
2556 r'|float|double|signed|unsigned'
2557 r'|schar|u?int8|u?int16|u?int32|u?int64)'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002558 r'\s+(register|static|extern|typedef)\b',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002559 line):
2560 error(filename, linenum, 'build/storage_class', 5,
2561 'Storage class (static, extern, typedef, etc) should be first.')
2562
2563 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2564 error(filename, linenum, 'build/endif_comment', 5,
2565 'Uncommented text after #endif is non-standard. Use a comment.')
2566
2567 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2568 error(filename, linenum, 'build/forward_decl', 5,
2569 'Inner-style forward declarations are invalid. Remove this line.')
2570
2571 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2572 line):
2573 error(filename, linenum, 'build/deprecated', 3,
2574 '>? and <? (max and min) operators are non-standard and deprecated.')
2575
erg@google.com26970fa2009-11-17 18:07:32 +00002576 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2577 # TODO(unknown): Could it be expanded safely to arbitrary references,
2578 # without triggering too many false positives? The first
2579 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2580 # the restriction.
2581 # Here's the original regexp, for the reference:
2582 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2583 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2584 error(filename, linenum, 'runtime/member_string_references', 2,
2585 'const string& members are dangerous. It is much better to use '
2586 'alternatives, such as pointers or simple constants.')
2587
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002588 # Everything else in this function operates on class declarations.
2589 # Return early if the top of the nesting stack is not a class, or if
2590 # the class head is not completed yet.
2591 classinfo = nesting_state.InnermostClass()
2592 if not classinfo or not classinfo.seen_open_brace:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002593 return
2594
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002595 # The class may have been declared with namespace or classname qualifiers.
2596 # The constructor and destructor will not have those qualifiers.
2597 base_classname = classinfo.name.split('::')[-1]
2598
2599 # Look for single-argument constructors that aren't marked explicit.
avakulenko@google.com59146752014-08-11 20:20:55 +00002600 # Technically a valid construct, but against style. Also look for
2601 # non-single-argument constructors which are also technically valid, but
2602 # strongly suggest something is wrong.
2603 explicit_constructor_match = Match(
2604 r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*'
2605 r'\(((?:[^()]|\([^()]*\))*)\)'
2606 % re.escape(base_classname),
2607 line)
2608
2609 if explicit_constructor_match:
2610 is_marked_explicit = explicit_constructor_match.group(1)
2611
2612 if not explicit_constructor_match.group(2):
2613 constructor_args = []
2614 else:
2615 constructor_args = explicit_constructor_match.group(2).split(',')
2616
2617 # collapse arguments so that commas in template parameter lists and function
2618 # argument parameter lists don't split arguments in two
2619 i = 0
2620 while i < len(constructor_args):
2621 constructor_arg = constructor_args[i]
2622 while (constructor_arg.count('<') > constructor_arg.count('>') or
2623 constructor_arg.count('(') > constructor_arg.count(')')):
2624 constructor_arg += ',' + constructor_args[i + 1]
2625 del constructor_args[i + 1]
2626 constructor_args[i] = constructor_arg
2627 i += 1
2628
2629 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2630 noarg_constructor = (not constructor_args or # empty arg list
2631 # 'void' arg specifier
2632 (len(constructor_args) == 1 and
2633 constructor_args[0].strip() == 'void'))
2634 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2635 not noarg_constructor) or
2636 # all but at most one arg defaulted
2637 (len(constructor_args) >= 1 and
2638 not noarg_constructor and
2639 len(defaulted_args) >= len(constructor_args) - 1))
2640 initializer_list_constructor = bool(
2641 onearg_constructor and
2642 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2643 copy_constructor = bool(
2644 onearg_constructor and
2645 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2646 % re.escape(base_classname), constructor_args[0].strip()))
2647
2648 if (not is_marked_explicit and
2649 onearg_constructor and
2650 not initializer_list_constructor and
2651 not copy_constructor):
2652 if defaulted_args:
2653 error(filename, linenum, 'runtime/explicit', 5,
2654 'Constructors callable with one argument '
2655 'should be marked explicit.')
2656 else:
2657 error(filename, linenum, 'runtime/explicit', 5,
2658 'Single-parameter constructors should be marked explicit.')
2659 elif is_marked_explicit and not onearg_constructor:
2660 if noarg_constructor:
2661 error(filename, linenum, 'runtime/explicit', 5,
2662 'Zero-parameter constructors should not be marked explicit.')
2663 else:
2664 error(filename, linenum, 'runtime/explicit', 0,
2665 'Constructors that require multiple arguments '
2666 'should not be marked explicit.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002667
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002668
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002669def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002670 """Checks for the correctness of various spacing around function calls.
2671
2672 Args:
2673 filename: The name of the current file.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002674 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002675 linenum: The number of the line to check.
2676 error: The function to call with any errors found.
2677 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002678 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002679
2680 # Since function calls often occur inside if/for/while/switch
2681 # expressions - which have their own, more liberal conventions - we
2682 # first see if we should be looking inside such an expression for a
2683 # function call, to which we can apply more strict standards.
2684 fncall = line # if there's no control flow construct, look at whole line
2685 for pattern in (r'\bif\s*\((.*)\)\s*{',
2686 r'\bfor\s*\((.*)\)\s*{',
2687 r'\bwhile\s*\((.*)\)\s*[{;]',
2688 r'\bswitch\s*\((.*)\)\s*{'):
2689 match = Search(pattern, line)
2690 if match:
2691 fncall = match.group(1) # look inside the parens for function calls
2692 break
2693
2694 # Except in if/for/while/switch, there should never be space
2695 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2696 # for nested parens ( (a+b) + c ). Likewise, there should never be
2697 # a space before a ( when it's a function argument. I assume it's a
2698 # function argument when the char before the whitespace is legal in
2699 # a function name (alnum + _) and we're not starting a macro. Also ignore
2700 # pointers and references to arrays and functions coz they're too tricky:
2701 # we use a very simple way to recognize these:
2702 # " (something)(maybe-something)" or
2703 # " (something)(maybe-something," or
2704 # " (something)[something]"
2705 # Note that we assume the contents of [] to be short enough that
2706 # they'll never need to wrap.
2707 if ( # Ignore control structures.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002708 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
2709 fncall) and
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002710 # Ignore pointers/references to functions.
2711 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2712 # Ignore pointers/references to arrays.
2713 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com6317a9c2009-06-25 00:28:19 +00002714 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002715 error(filename, linenum, 'whitespace/parens', 4,
2716 'Extra space after ( in function call')
erg@google.com6317a9c2009-06-25 00:28:19 +00002717 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002718 error(filename, linenum, 'whitespace/parens', 2,
2719 'Extra space after (')
2720 if (Search(r'\w\s+\(', fncall) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002721 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002722 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002723 # TODO(unknown): Space after an operator function seem to be a common
2724 # error, silence those for now by restricting them to highest verbosity.
2725 if Search(r'\boperator_*\b', line):
2726 error(filename, linenum, 'whitespace/parens', 0,
2727 'Extra space before ( in function call')
2728 else:
2729 error(filename, linenum, 'whitespace/parens', 4,
2730 'Extra space before ( in function call')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002731 # If the ) is followed only by a newline or a { + newline, assume it's
2732 # part of a control statement (if/while/etc), and don't complain
2733 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002734 # If the closing parenthesis is preceded by only whitespaces,
2735 # try to give a more descriptive error message.
2736 if Search(r'^\s+\)', fncall):
2737 error(filename, linenum, 'whitespace/parens', 2,
2738 'Closing ) should be moved to the previous line')
2739 else:
2740 error(filename, linenum, 'whitespace/parens', 2,
2741 'Extra space before )')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002742
2743
2744def IsBlankLine(line):
2745 """Returns true if the given line is blank.
2746
2747 We consider a line to be blank if the line is empty or consists of
2748 only white spaces.
2749
2750 Args:
2751 line: A line of a string.
2752
2753 Returns:
2754 True, if the given line is blank.
2755 """
2756 return not line or line.isspace()
2757
2758
avakulenko@google.com59146752014-08-11 20:20:55 +00002759def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
2760 error):
2761 is_namespace_indent_item = (
2762 len(nesting_state.stack) > 1 and
2763 nesting_state.stack[-1].check_namespace_indentation and
2764 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
2765 nesting_state.previous_stack_top == nesting_state.stack[-2])
2766
2767 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
2768 clean_lines.elided, line):
2769 CheckItemIndentationInNamespace(filename, clean_lines.elided,
2770 line, error)
2771
2772
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002773def CheckForFunctionLengths(filename, clean_lines, linenum,
2774 function_state, error):
2775 """Reports for long function bodies.
2776
2777 For an overview why this is done, see:
2778 http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
2779
2780 Uses a simplistic algorithm assuming other style guidelines
2781 (especially spacing) are followed.
2782 Only checks unindented functions, so class members are unchecked.
2783 Trivial bodies are unchecked, so constructors with huge initializer lists
2784 may be missed.
2785 Blank/comment lines are not counted so as to avoid encouraging the removal
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002786 of vertical space and comments just to get through a lint check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002787 NOLINT *on the last line of a function* disables this check.
2788
2789 Args:
2790 filename: The name of the current file.
2791 clean_lines: A CleansedLines instance containing the file.
2792 linenum: The number of the line to check.
2793 function_state: Current function name and lines in body so far.
2794 error: The function to call with any errors found.
2795 """
2796 lines = clean_lines.lines
2797 line = lines[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002798 joined_line = ''
2799
2800 starting_func = False
erg@google.com6317a9c2009-06-25 00:28:19 +00002801 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002802 match_result = Match(regexp, line)
2803 if match_result:
2804 # If the name is all caps and underscores, figure it's a macro and
2805 # ignore it, unless it's TEST or TEST_F.
2806 function_name = match_result.group(1).split()[-1]
2807 if function_name == 'TEST' or function_name == 'TEST_F' or (
2808 not Match(r'[A-Z_]+$', function_name)):
2809 starting_func = True
2810
2811 if starting_func:
2812 body_found = False
erg@google.com6317a9c2009-06-25 00:28:19 +00002813 for start_linenum in xrange(linenum, clean_lines.NumLines()):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002814 start_line = lines[start_linenum]
2815 joined_line += ' ' + start_line.lstrip()
2816 if Search(r'(;|})', start_line): # Declarations and trivial functions
2817 body_found = True
2818 break # ... ignore
2819 elif Search(r'{', start_line):
2820 body_found = True
2821 function = Search(r'((\w|:)*)\(', line).group(1)
2822 if Match(r'TEST', function): # Handle TEST... macros
2823 parameter_regexp = Search(r'(\(.*\))', joined_line)
2824 if parameter_regexp: # Ignore bad syntax
2825 function += parameter_regexp.group(1)
2826 else:
2827 function += '()'
2828 function_state.Begin(function)
2829 break
2830 if not body_found:
erg@google.com6317a9c2009-06-25 00:28:19 +00002831 # No body for the function (or evidence of a non-function) was found.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002832 error(filename, linenum, 'readability/fn_size', 5,
2833 'Lint failed to find start of function body.')
2834 elif Match(r'^\}\s*$', line): # function end
erg@google.com35589e62010-11-17 18:58:16 +00002835 function_state.Check(error, filename, linenum)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002836 function_state.End()
2837 elif not Match(r'^\s*$', line):
2838 function_state.Count() # Count non-blank/non-comment lines.
2839
2840
2841_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
2842
2843
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002844def CheckComment(line, filename, linenum, next_line_start, error):
2845 """Checks for common mistakes in comments.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002846
2847 Args:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002848 line: The line in question.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002849 filename: The name of the current file.
2850 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002851 next_line_start: The first non-whitespace column of the next line.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002852 error: The function to call with any errors found.
2853 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002854 commentpos = line.find('//')
2855 if commentpos != -1:
2856 # Check if the // may be in quotes. If so, ignore it
2857 # Comparisons made explicit for clarity -- pylint: disable=g-explicit-bool-comparison
2858 if (line.count('"', 0, commentpos) -
2859 line.count('\\"', 0, commentpos)) % 2 == 0: # not in quotes
2860 # Allow one space for new scopes, two spaces otherwise:
2861 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
2862 ((commentpos >= 1 and
2863 line[commentpos-1] not in string.whitespace) or
2864 (commentpos >= 2 and
2865 line[commentpos-2] not in string.whitespace))):
2866 error(filename, linenum, 'whitespace/comments', 2,
2867 'At least two spaces is best between code and comments')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002868
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002869 # Checks for common mistakes in TODO comments.
2870 comment = line[commentpos:]
2871 match = _RE_PATTERN_TODO.match(comment)
2872 if match:
2873 # One whitespace is correct; zero whitespace is handled elsewhere.
2874 leading_whitespace = match.group(1)
2875 if len(leading_whitespace) > 1:
2876 error(filename, linenum, 'whitespace/todo', 2,
2877 'Too many spaces before TODO')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002878
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002879 username = match.group(2)
2880 if not username:
2881 error(filename, linenum, 'readability/todo', 2,
2882 'Missing username in TODO; it should look like '
2883 '"// TODO(my_username): Stuff."')
2884
2885 middle_whitespace = match.group(3)
2886 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
2887 if middle_whitespace != ' ' and middle_whitespace != '':
2888 error(filename, linenum, 'whitespace/todo', 2,
2889 'TODO(my_username) should be followed by a space')
2890
2891 # If the comment contains an alphanumeric character, there
2892 # should be a space somewhere between it and the //.
2893 if Match(r'//[^ ]*\w', comment):
2894 error(filename, linenum, 'whitespace/comments', 4,
2895 'Should have a space between // and comment')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002896
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002897def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
2898 """Checks for improper use of DISALLOW* macros.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002899
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002900 Args:
2901 filename: The name of the current file.
2902 clean_lines: A CleansedLines instance containing the file.
2903 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002904 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002905 the current stack of nested blocks being parsed.
2906 error: The function to call with any errors found.
2907 """
2908 line = clean_lines.elided[linenum] # get rid of comments and strings
2909
2910 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002911 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
2912 if not matched:
2913 return
2914 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
2915 if nesting_state.stack[-1].access != 'private':
2916 error(filename, linenum, 'readability/constructors', 3,
2917 '%s must be in the private: section' % matched.group(1))
2918
2919 else:
2920 # Found DISALLOW* macro outside a class declaration, or perhaps it
2921 # was used inside a function when it should have been part of the
2922 # class declaration. We could issue a warning here, but it
2923 # probably resulted in a compiler error already.
2924 pass
2925
2926
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002927def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002928 """Checks for the correctness of various spacing issues in the code.
2929
2930 Things we check for: spaces around operators, spaces after
2931 if/for/while/switch, no spaces around parens in function calls, two
2932 spaces between code and comment, don't start a block with a blank
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002933 line, don't end a function with a blank line, don't add a blank line
2934 after public/protected/private, don't have too many blank lines in a row.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002935
2936 Args:
2937 filename: The name of the current file.
2938 clean_lines: A CleansedLines instance containing the file.
2939 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002940 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002941 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002942 error: The function to call with any errors found.
2943 """
2944
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002945 # Don't use "elided" lines here, otherwise we can't check commented lines.
2946 # Don't want to use "raw" either, because we don't want to check inside C++11
2947 # raw strings,
2948 raw = clean_lines.lines_without_raw_strings
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002949 line = raw[linenum]
2950
2951 # Before nixing comments, check if the line is blank for no good
2952 # reason. This includes the first line after a block is opened, and
2953 # blank lines at the end of a function (ie, right before a line like '}'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002954 #
2955 # Skip all the blank line checks if we are immediately inside a
2956 # namespace body. In other words, don't issue blank line warnings
2957 # for this block:
2958 # namespace {
2959 #
2960 # }
2961 #
2962 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002963 #
2964 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
2965 # like namespaces.
2966 if (IsBlankLine(line) and
2967 not nesting_state.InNamespaceBody() and
2968 not nesting_state.InExternC()):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002969 elided = clean_lines.elided
2970 prev_line = elided[linenum - 1]
2971 prevbrace = prev_line.rfind('{')
2972 # TODO(unknown): Don't complain if line before blank line, and line after,
2973 # both start with alnums and are indented the same amount.
2974 # This ignores whitespace at the start of a namespace block
2975 # because those are not usually indented.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002976 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002977 # OK, we have a blank line at the start of a code block. Before we
2978 # complain, we check if it is an exception to the rule: The previous
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002979 # non-empty line has the parameters of a function header that are indented
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002980 # 4 spaces (because they did not fit in a 80 column line when placed on
2981 # the same line as the function name). We also check for the case where
2982 # the previous line is indented 6 spaces, which may happen when the
2983 # initializers of a constructor do not fit into a 80 column line.
2984 exception = False
2985 if Match(r' {6}\w', prev_line): # Initializer list?
2986 # We are looking for the opening column of initializer list, which
2987 # should be indented 4 spaces to cause 6 space indentation afterwards.
2988 search_position = linenum-2
2989 while (search_position >= 0
2990 and Match(r' {6}\w', elided[search_position])):
2991 search_position -= 1
2992 exception = (search_position >= 0
2993 and elided[search_position][:5] == ' :')
2994 else:
2995 # Search for the function arguments or an initializer list. We use a
2996 # simple heuristic here: If the line is indented 4 spaces; and we have a
2997 # closing paren, without the opening paren, followed by an opening brace
2998 # or colon (for initializer lists) we assume that it is the last line of
2999 # a function header. If we have a colon indented 4 spaces, it is an
3000 # initializer list.
3001 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3002 prev_line)
3003 or Match(r' {4}:', prev_line))
3004
3005 if not exception:
3006 error(filename, linenum, 'whitespace/blank_line', 2,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003007 'Redundant blank line at the start of a code block '
3008 'should be deleted.')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003009 # Ignore blank lines at the end of a block in a long if-else
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003010 # chain, like this:
3011 # if (condition1) {
3012 # // Something followed by a blank line
3013 #
3014 # } else if (condition2) {
3015 # // Something else
3016 # }
3017 if linenum + 1 < clean_lines.NumLines():
3018 next_line = raw[linenum + 1]
3019 if (next_line
3020 and Match(r'\s*}', next_line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003021 and next_line.find('} else ') == -1):
3022 error(filename, linenum, 'whitespace/blank_line', 3,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003023 'Redundant blank line at the end of a code block '
3024 'should be deleted.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003025
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003026 matched = Match(r'\s*(public|protected|private):', prev_line)
3027 if matched:
3028 error(filename, linenum, 'whitespace/blank_line', 3,
3029 'Do not leave a blank line after "%s:"' % matched.group(1))
3030
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003031 # Next, check comments
3032 next_line_start = 0
3033 if linenum + 1 < clean_lines.NumLines():
3034 next_line = raw[linenum + 1]
3035 next_line_start = len(next_line) - len(next_line.lstrip())
3036 CheckComment(line, filename, linenum, next_line_start, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003037
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003038 # get rid of comments and strings
3039 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003040
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003041 # You shouldn't have spaces before your brackets, except maybe after
3042 # 'delete []' or 'return []() {};'
3043 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3044 error(filename, linenum, 'whitespace/braces', 5,
3045 'Extra space before [')
3046
3047 # In range-based for, we wanted spaces before and after the colon, but
3048 # not around "::" tokens that might appear.
3049 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3050 Search(r'for *\(.*[^: ]:[^:]', line)):
3051 error(filename, linenum, 'whitespace/forcolon', 2,
3052 'Missing space around colon in range-based for loop')
3053
3054
3055def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3056 """Checks for horizontal spacing around operators.
3057
3058 Args:
3059 filename: The name of the current file.
3060 clean_lines: A CleansedLines instance containing the file.
3061 linenum: The number of the line to check.
3062 error: The function to call with any errors found.
3063 """
3064 line = clean_lines.elided[linenum]
3065
3066 # Don't try to do spacing checks for operator methods. Do this by
3067 # replacing the troublesome characters with something else,
3068 # preserving column position for all other characters.
3069 #
3070 # The replacement is done repeatedly to avoid false positives from
3071 # operators that call operators.
3072 while True:
3073 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3074 if match:
3075 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3076 else:
3077 break
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003078
3079 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3080 # Otherwise not. Note we only check for non-spaces on *both* sides;
3081 # sometimes people put non-spaces on one side when aligning ='s among
3082 # many lines (not that this is behavior that I approve of...)
3083 if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line):
3084 error(filename, linenum, 'whitespace/operators', 4,
3085 'Missing spaces around =')
3086
3087 # It's ok not to have spaces around binary operators like + - * /, but if
3088 # there's too little whitespace, we get concerned. It's hard to tell,
3089 # though, so we punt on this one for now. TODO.
3090
3091 # You should always have whitespace around binary operators.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003092 #
3093 # Check <= and >= first to avoid false positives with < and >, then
3094 # check non-include lines for spacing around < and >.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003095 #
3096 # If the operator is followed by a comma, assume it's be used in a
3097 # macro context and don't do any checks. This avoids false
3098 # positives.
3099 #
3100 # Note that && is not included here. Those are checked separately
3101 # in CheckRValueReference
3102 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003103 if match:
3104 error(filename, linenum, 'whitespace/operators', 3,
3105 'Missing spaces around %s' % match.group(1))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003106 elif not Match(r'#.*include', line):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003107 # Look for < that is not surrounded by spaces. This is only
3108 # triggered if both sides are missing spaces, even though
3109 # technically should should flag if at least one side is missing a
3110 # space. This is done to avoid some false positives with shifts.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003111 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3112 if match:
3113 (_, _, end_pos) = CloseExpression(
3114 clean_lines, linenum, len(match.group(1)))
3115 if end_pos <= -1:
3116 error(filename, linenum, 'whitespace/operators', 3,
3117 'Missing spaces around <')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003118
3119 # Look for > that is not surrounded by spaces. Similar to the
3120 # above, we only trigger if both sides are missing spaces to avoid
3121 # false positives with shifts.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003122 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3123 if match:
3124 (_, _, start_pos) = ReverseCloseExpression(
3125 clean_lines, linenum, len(match.group(1)))
3126 if start_pos <= -1:
3127 error(filename, linenum, 'whitespace/operators', 3,
3128 'Missing spaces around >')
3129
3130 # We allow no-spaces around << when used like this: 10<<20, but
3131 # not otherwise (particularly, not when used as streams)
avakulenko@google.com59146752014-08-11 20:20:55 +00003132 #
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003133 # We also allow operators following an opening parenthesis, since
3134 # those tend to be macros that deal with operators.
3135 match = Search(r'(operator|\S)(?:L|UL|ULL|l|ul|ull)?<<([^\s,=])', line)
3136 if (match and match.group(1) != '(' and
3137 not (match.group(1).isdigit() and match.group(2).isdigit()) and
3138 not (match.group(1) == 'operator' and match.group(2) == ';')):
3139 error(filename, linenum, 'whitespace/operators', 3,
3140 'Missing spaces around <<')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003141
3142 # We allow no-spaces around >> for almost anything. This is because
3143 # C++11 allows ">>" to close nested templates, which accounts for
3144 # most cases when ">>" is not followed by a space.
3145 #
3146 # We still warn on ">>" followed by alpha character, because that is
3147 # likely due to ">>" being used for right shifts, e.g.:
3148 # value >> alpha
3149 #
3150 # When ">>" is used to close templates, the alphanumeric letter that
3151 # follows would be part of an identifier, and there should still be
3152 # a space separating the template type and the identifier.
3153 # type<type<type>> alpha
3154 match = Search(r'>>[a-zA-Z_]', line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003155 if match:
3156 error(filename, linenum, 'whitespace/operators', 3,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003157 'Missing spaces around >>')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003158
3159 # There shouldn't be space around unary operators
3160 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3161 if match:
3162 error(filename, linenum, 'whitespace/operators', 4,
3163 'Extra space for operator %s' % match.group(1))
3164
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003165
3166def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3167 """Checks for horizontal spacing around parentheses.
3168
3169 Args:
3170 filename: The name of the current file.
3171 clean_lines: A CleansedLines instance containing the file.
3172 linenum: The number of the line to check.
3173 error: The function to call with any errors found.
3174 """
3175 line = clean_lines.elided[linenum]
3176
3177 # No spaces after an if, while, switch, or for
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003178 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3179 if match:
3180 error(filename, linenum, 'whitespace/parens', 5,
3181 'Missing space before ( in %s' % match.group(1))
3182
3183 # For if/for/while/switch, the left and right parens should be
3184 # consistent about how many spaces are inside the parens, and
3185 # there should either be zero or one spaces inside the parens.
3186 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.com6317a9c2009-06-25 00:28:19 +00003187 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003188 match = Search(r'\b(if|for|while|switch)\s*'
3189 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3190 line)
3191 if match:
3192 if len(match.group(2)) != len(match.group(4)):
3193 if not (match.group(3) == ';' and
erg@google.com6317a9c2009-06-25 00:28:19 +00003194 len(match.group(2)) == 1 + len(match.group(4)) or
3195 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003196 error(filename, linenum, 'whitespace/parens', 5,
3197 'Mismatching spaces inside () in %s' % match.group(1))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003198 if len(match.group(2)) not in [0, 1]:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003199 error(filename, linenum, 'whitespace/parens', 5,
3200 'Should have zero or one spaces inside ( and ) in %s' %
3201 match.group(1))
3202
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003203
3204def CheckCommaSpacing(filename, clean_lines, linenum, error):
3205 """Checks for horizontal spacing near commas and semicolons.
3206
3207 Args:
3208 filename: The name of the current file.
3209 clean_lines: A CleansedLines instance containing the file.
3210 linenum: The number of the line to check.
3211 error: The function to call with any errors found.
3212 """
3213 raw = clean_lines.lines_without_raw_strings
3214 line = clean_lines.elided[linenum]
3215
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003216 # 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 +00003217 #
3218 # This does not apply when the non-space character following the
3219 # comma is another comma, since the only time when that happens is
3220 # for empty macro arguments.
3221 #
3222 # We run this check in two passes: first pass on elided lines to
3223 # verify that lines contain missing whitespaces, second pass on raw
3224 # lines to confirm that those missing whitespaces are not due to
3225 # elided comments.
avakulenko@google.com59146752014-08-11 20:20:55 +00003226 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3227 Search(r',[^,\s]', raw[linenum])):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003228 error(filename, linenum, 'whitespace/comma', 3,
3229 'Missing space after ,')
3230
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003231 # You should always have a space after a semicolon
3232 # except for few corner cases
3233 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3234 # space after ;
3235 if Search(r';[^\s};\\)/]', line):
3236 error(filename, linenum, 'whitespace/semicolon', 3,
3237 'Missing space after ;')
3238
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003239
3240def CheckBracesSpacing(filename, clean_lines, linenum, error):
3241 """Checks for horizontal spacing near commas.
3242
3243 Args:
3244 filename: The name of the current file.
3245 clean_lines: A CleansedLines instance containing the file.
3246 linenum: The number of the line to check.
3247 error: The function to call with any errors found.
3248 """
3249 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003250
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003251 # Except after an opening paren, or after another opening brace (in case of
3252 # an initializer list, for instance), you should have spaces before your
3253 # braces. And since you should never have braces at the beginning of a line,
3254 # this is an easy test.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003255 match = Match(r'^(.*[^ ({]){', line)
3256 if match:
3257 # Try a bit harder to check for brace initialization. This
3258 # happens in one of the following forms:
3259 # Constructor() : initializer_list_{} { ... }
3260 # Constructor{}.MemberFunction()
3261 # Type variable{};
3262 # FunctionCall(type{}, ...);
3263 # LastArgument(..., type{});
3264 # LOG(INFO) << type{} << " ...";
3265 # map_of_type[{...}] = ...;
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003266 # ternary = expr ? new type{} : nullptr;
3267 # OuterTemplate<InnerTemplateConstructor<Type>{}>
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003268 #
3269 # We check for the character following the closing brace, and
3270 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003271 # "{.;,)<>]:".
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003272 #
3273 # To account for nested initializer list, we allow any number of
3274 # closing braces up to "{;,)<". We can't simply silence the
3275 # warning on first sight of closing brace, because that would
3276 # cause false negatives for things that are not initializer lists.
3277 # Silence this: But not this:
3278 # Outer{ if (...) {
3279 # Inner{...} if (...){ // Missing space before {
3280 # }; }
3281 #
3282 # There is a false negative with this approach if people inserted
3283 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3284 # spurious semicolon with a separate check.
3285 (endline, endlinenum, endpos) = CloseExpression(
3286 clean_lines, linenum, len(match.group(1)))
3287 trailing_text = ''
3288 if endpos > -1:
3289 trailing_text = endline[endpos:]
3290 for offset in xrange(endlinenum + 1,
3291 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3292 trailing_text += clean_lines.elided[offset]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003293 if not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003294 error(filename, linenum, 'whitespace/braces', 5,
3295 'Missing space before {')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003296
3297 # Make sure '} else {' has spaces.
3298 if Search(r'}else', line):
3299 error(filename, linenum, 'whitespace/braces', 5,
3300 'Missing space before else')
3301
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003302 # You shouldn't have a space before a semicolon at the end of the line.
3303 # There's a special case for "for" since the style guide allows space before
3304 # the semicolon there.
3305 if Search(r':\s*;\s*$', line):
3306 error(filename, linenum, 'whitespace/semicolon', 5,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003307 'Semicolon defining empty statement. Use {} instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003308 elif Search(r'^\s*;\s*$', line):
3309 error(filename, linenum, 'whitespace/semicolon', 5,
3310 'Line contains only semicolon. If this should be an empty statement, '
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003311 'use {} instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003312 elif (Search(r'\s+;\s*$', line) and
3313 not Search(r'\bfor\b', line)):
3314 error(filename, linenum, 'whitespace/semicolon', 5,
3315 'Extra space before last semicolon. If this should be an empty '
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003316 'statement, use {} instead.')
3317
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003318
3319def IsDecltype(clean_lines, linenum, column):
3320 """Check if the token ending on (linenum, column) is decltype().
3321
3322 Args:
3323 clean_lines: A CleansedLines instance containing the file.
3324 linenum: the number of the line to check.
3325 column: end column of the token to check.
3326 Returns:
3327 True if this token is decltype() expression, False otherwise.
3328 """
3329 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3330 if start_col < 0:
3331 return False
3332 if Search(r'\bdecltype\s*$', text[0:start_col]):
3333 return True
3334 return False
3335
3336
3337def IsTemplateParameterList(clean_lines, linenum, column):
3338 """Check if the token ending on (linenum, column) is the end of template<>.
3339
3340 Args:
3341 clean_lines: A CleansedLines instance containing the file.
3342 linenum: the number of the line to check.
3343 column: end column of the token to check.
3344 Returns:
3345 True if this token is end of a template parameter list, False otherwise.
3346 """
3347 (_, startline, startpos) = ReverseCloseExpression(
3348 clean_lines, linenum, column)
3349 if (startpos > -1 and
3350 Search(r'\btemplate\s*$', clean_lines.elided[startline][0:startpos])):
3351 return True
3352 return False
3353
3354
3355def IsRValueType(clean_lines, nesting_state, linenum, column):
3356 """Check if the token ending on (linenum, column) is a type.
3357
3358 Assumes that text to the right of the column is "&&" or a function
3359 name.
3360
3361 Args:
3362 clean_lines: A CleansedLines instance containing the file.
3363 nesting_state: A NestingState instance which maintains information about
3364 the current stack of nested blocks being parsed.
3365 linenum: the number of the line to check.
3366 column: end column of the token to check.
3367 Returns:
3368 True if this token is a type, False if we are not sure.
3369 """
3370 prefix = clean_lines.elided[linenum][0:column]
3371
3372 # Get one word to the left. If we failed to do so, this is most
3373 # likely not a type, since it's unlikely that the type name and "&&"
3374 # would be split across multiple lines.
3375 match = Match(r'^(.*)(\b\w+|[>*)&])\s*$', prefix)
3376 if not match:
3377 return False
3378
3379 # Check text following the token. If it's "&&>" or "&&," or "&&...", it's
3380 # most likely a rvalue reference used inside a template.
3381 suffix = clean_lines.elided[linenum][column:]
3382 if Match(r'&&\s*(?:[>,]|\.\.\.)', suffix):
3383 return True
3384
3385 # Check for simple type and end of templates:
3386 # int&& variable
3387 # vector<int>&& variable
3388 #
3389 # Because this function is called recursively, we also need to
3390 # recognize pointer and reference types:
3391 # int* Function()
3392 # int& Function()
3393 if match.group(2) in ['char', 'char16_t', 'char32_t', 'wchar_t', 'bool',
3394 'short', 'int', 'long', 'signed', 'unsigned',
3395 'float', 'double', 'void', 'auto', '>', '*', '&']:
3396 return True
3397
3398 # If we see a close parenthesis, look for decltype on the other side.
3399 # decltype would unambiguously identify a type, anything else is
3400 # probably a parenthesized expression and not a type.
3401 if match.group(2) == ')':
3402 return IsDecltype(
3403 clean_lines, linenum, len(match.group(1)) + len(match.group(2)) - 1)
3404
3405 # Check for casts and cv-qualifiers.
3406 # match.group(1) remainder
3407 # -------------- ---------
3408 # const_cast< type&&
3409 # const type&&
3410 # type const&&
3411 if Search(r'\b(?:const_cast\s*<|static_cast\s*<|dynamic_cast\s*<|'
3412 r'reinterpret_cast\s*<|\w+\s)\s*$',
3413 match.group(1)):
3414 return True
3415
3416 # Look for a preceding symbol that might help differentiate the context.
3417 # These are the cases that would be ambiguous:
3418 # match.group(1) remainder
3419 # -------------- ---------
3420 # Call ( expression &&
3421 # Declaration ( type&&
3422 # sizeof ( type&&
3423 # if ( expression &&
3424 # while ( expression &&
3425 # for ( type&&
3426 # for( ; expression &&
3427 # statement ; type&&
3428 # block { type&&
3429 # constructor { expression &&
3430 start = linenum
3431 line = match.group(1)
3432 match_symbol = None
3433 while start >= 0:
3434 # We want to skip over identifiers and commas to get to a symbol.
3435 # Commas are skipped so that we can find the opening parenthesis
3436 # for function parameter lists.
3437 match_symbol = Match(r'^(.*)([^\w\s,])[\w\s,]*$', line)
3438 if match_symbol:
3439 break
3440 start -= 1
3441 line = clean_lines.elided[start]
3442
3443 if not match_symbol:
3444 # Probably the first statement in the file is an rvalue reference
3445 return True
3446
3447 if match_symbol.group(2) == '}':
3448 # Found closing brace, probably an indicate of this:
3449 # block{} type&&
3450 return True
3451
3452 if match_symbol.group(2) == ';':
3453 # Found semicolon, probably one of these:
3454 # for(; expression &&
3455 # statement; type&&
3456
3457 # Look for the previous 'for(' in the previous lines.
3458 before_text = match_symbol.group(1)
3459 for i in xrange(start - 1, max(start - 6, 0), -1):
3460 before_text = clean_lines.elided[i] + before_text
3461 if Search(r'for\s*\([^{};]*$', before_text):
3462 # This is the condition inside a for-loop
3463 return False
3464
3465 # Did not find a for-init-statement before this semicolon, so this
3466 # is probably a new statement and not a condition.
3467 return True
3468
3469 if match_symbol.group(2) == '{':
3470 # Found opening brace, probably one of these:
3471 # block{ type&& = ... ; }
3472 # constructor{ expression && expression }
3473
3474 # Look for a closing brace or a semicolon. If we see a semicolon
3475 # first, this is probably a rvalue reference.
3476 line = clean_lines.elided[start][0:len(match_symbol.group(1)) + 1]
3477 end = start
3478 depth = 1
3479 while True:
3480 for ch in line:
3481 if ch == ';':
3482 return True
3483 elif ch == '{':
3484 depth += 1
3485 elif ch == '}':
3486 depth -= 1
3487 if depth == 0:
3488 return False
3489 end += 1
3490 if end >= clean_lines.NumLines():
3491 break
3492 line = clean_lines.elided[end]
3493 # Incomplete program?
3494 return False
3495
3496 if match_symbol.group(2) == '(':
3497 # Opening parenthesis. Need to check what's to the left of the
3498 # parenthesis. Look back one extra line for additional context.
3499 before_text = match_symbol.group(1)
3500 if linenum > 1:
3501 before_text = clean_lines.elided[linenum - 1] + before_text
3502 before_text = match_symbol.group(1)
3503
3504 # Patterns that are likely to be types:
3505 # [](type&&
3506 # for (type&&
3507 # sizeof(type&&
3508 # operator=(type&&
3509 #
3510 if Search(r'(?:\]|\bfor|\bsizeof|\boperator\s*\S+\s*)\s*$', before_text):
3511 return True
3512
3513 # Patterns that are likely to be expressions:
3514 # if (expression &&
3515 # while (expression &&
3516 # : initializer(expression &&
3517 # , initializer(expression &&
3518 # ( FunctionCall(expression &&
3519 # + FunctionCall(expression &&
3520 # + (expression &&
3521 #
3522 # The last '+' represents operators such as '+' and '-'.
3523 if Search(r'(?:\bif|\bwhile|[-+=%^(<!?:,&*]\s*)$', before_text):
3524 return False
3525
3526 # Something else. Check that tokens to the left look like
3527 # return_type function_name
3528 match_func = Match(r'^(.*)\s+\w(?:\w|::)*(?:<[^<>]*>)?\s*$',
3529 match_symbol.group(1))
3530 if match_func:
3531 # Check for constructors, which don't have return types.
avakulenko@google.com59146752014-08-11 20:20:55 +00003532 if Search(r'\b(?:explicit|inline)$', match_func.group(1)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003533 return True
3534 implicit_constructor = Match(r'\s*(\w+)\((?:const\s+)?(\w+)', prefix)
3535 if (implicit_constructor and
3536 implicit_constructor.group(1) == implicit_constructor.group(2)):
3537 return True
3538 return IsRValueType(clean_lines, nesting_state, linenum,
3539 len(match_func.group(1)))
3540
3541 # Nothing before the function name. If this is inside a block scope,
3542 # this is probably a function call.
3543 return not (nesting_state.previous_stack_top and
3544 nesting_state.previous_stack_top.IsBlockInfo())
3545
3546 if match_symbol.group(2) == '>':
3547 # Possibly a closing bracket, check that what's on the other side
3548 # looks like the start of a template.
3549 return IsTemplateParameterList(
3550 clean_lines, start, len(match_symbol.group(1)))
3551
3552 # Some other symbol, usually something like "a=b&&c". This is most
3553 # likely not a type.
3554 return False
3555
3556
avakulenko@google.com59146752014-08-11 20:20:55 +00003557def IsDeletedOrDefault(clean_lines, linenum):
3558 """Check if current constructor or operator is deleted or default.
3559
3560 Args:
3561 clean_lines: A CleansedLines instance containing the file.
3562 linenum: The number of the line to check.
3563 Returns:
3564 True if this is a deleted or default constructor.
3565 """
3566 open_paren = clean_lines.elided[linenum].find('(')
3567 if open_paren < 0:
3568 return False
3569 (close_line, _, close_paren) = CloseExpression(
3570 clean_lines, linenum, open_paren)
3571 if close_paren < 0:
3572 return False
3573 return Match(r'\s*=\s*(?:delete|default)\b', close_line[close_paren:])
3574
3575
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003576def IsRValueAllowed(clean_lines, linenum):
avakulenko@google.com59146752014-08-11 20:20:55 +00003577 """Check if RValue reference is allowed on a particular line.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003578
3579 Args:
3580 clean_lines: A CleansedLines instance containing the file.
3581 linenum: The number of the line to check.
3582 Returns:
3583 True if line is within the region where RValue references are allowed.
3584 """
avakulenko@google.com59146752014-08-11 20:20:55 +00003585 # Allow region marked by PUSH/POP macros
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003586 for i in xrange(linenum, 0, -1):
3587 line = clean_lines.elided[i]
3588 if Match(r'GOOGLE_ALLOW_RVALUE_REFERENCES_(?:PUSH|POP)', line):
3589 if not line.endswith('PUSH'):
3590 return False
3591 for j in xrange(linenum, clean_lines.NumLines(), 1):
3592 line = clean_lines.elided[j]
3593 if Match(r'GOOGLE_ALLOW_RVALUE_REFERENCES_(?:PUSH|POP)', line):
3594 return line.endswith('POP')
avakulenko@google.com59146752014-08-11 20:20:55 +00003595
3596 # Allow operator=
3597 line = clean_lines.elided[linenum]
3598 if Search(r'\boperator\s*=\s*\(', line):
3599 return IsDeletedOrDefault(clean_lines, linenum)
3600
3601 # Allow constructors
3602 match = Match(r'\s*([\w<>]+)\s*::\s*([\w<>]+)\s*\(', line)
3603 if match and match.group(1) == match.group(2):
3604 return IsDeletedOrDefault(clean_lines, linenum)
3605 if Search(r'\b(?:explicit|inline)\s+[\w<>]+\s*\(', line):
3606 return IsDeletedOrDefault(clean_lines, linenum)
3607
3608 if Match(r'\s*[\w<>]+\s*\(', line):
3609 previous_line = 'ReturnType'
3610 if linenum > 0:
3611 previous_line = clean_lines.elided[linenum - 1]
3612 if Match(r'^\s*$', previous_line) or Search(r'[{}:;]\s*$', previous_line):
3613 return IsDeletedOrDefault(clean_lines, linenum)
3614
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003615 return False
3616
3617
3618def CheckRValueReference(filename, clean_lines, linenum, nesting_state, error):
3619 """Check for rvalue references.
3620
3621 Args:
3622 filename: The name of the current file.
3623 clean_lines: A CleansedLines instance containing the file.
3624 linenum: The number of the line to check.
3625 nesting_state: A NestingState instance which maintains information about
3626 the current stack of nested blocks being parsed.
3627 error: The function to call with any errors found.
3628 """
3629 # Find lines missing spaces around &&.
3630 # TODO(unknown): currently we don't check for rvalue references
3631 # with spaces surrounding the && to avoid false positives with
3632 # boolean expressions.
3633 line = clean_lines.elided[linenum]
3634 match = Match(r'^(.*\S)&&', line)
3635 if not match:
3636 match = Match(r'(.*)&&\S', line)
3637 if (not match) or '(&&)' in line or Search(r'\boperator\s*$', match.group(1)):
3638 return
3639
3640 # Either poorly formed && or an rvalue reference, check the context
3641 # to get a more accurate error message. Mostly we want to determine
3642 # if what's to the left of "&&" is a type or not.
3643 and_pos = len(match.group(1))
3644 if IsRValueType(clean_lines, nesting_state, linenum, and_pos):
3645 if not IsRValueAllowed(clean_lines, linenum):
3646 error(filename, linenum, 'build/c++11', 3,
3647 'RValue references are an unapproved C++ feature.')
3648 else:
3649 error(filename, linenum, 'whitespace/operators', 3,
3650 'Missing spaces around &&')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003651
3652
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003653def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3654 """Checks for additional blank line issues related to sections.
3655
3656 Currently the only thing checked here is blank line before protected/private.
3657
3658 Args:
3659 filename: The name of the current file.
3660 clean_lines: A CleansedLines instance containing the file.
3661 class_info: A _ClassInfo objects.
3662 linenum: The number of the line to check.
3663 error: The function to call with any errors found.
3664 """
3665 # Skip checks if the class is small, where small means 25 lines or less.
3666 # 25 lines seems like a good cutoff since that's the usual height of
3667 # terminals, and any class that can't fit in one screen can't really
3668 # be considered "small".
3669 #
3670 # Also skip checks if we are on the first line. This accounts for
3671 # classes that look like
3672 # class Foo { public: ... };
3673 #
3674 # If we didn't find the end of the class, last_line would be zero,
3675 # and the check will be skipped by the first condition.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003676 if (class_info.last_line - class_info.starting_linenum <= 24 or
3677 linenum <= class_info.starting_linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003678 return
3679
3680 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3681 if matched:
3682 # Issue warning if the line before public/protected/private was
3683 # not a blank line, but don't do this if the previous line contains
3684 # "class" or "struct". This can happen two ways:
3685 # - We are at the beginning of the class.
3686 # - We are forward-declaring an inner class that is semantically
3687 # private, but needed to be public for implementation reasons.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003688 # Also ignores cases where the previous line ends with a backslash as can be
3689 # common when defining classes in C macros.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003690 prev_line = clean_lines.lines[linenum - 1]
3691 if (not IsBlankLine(prev_line) and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003692 not Search(r'\b(class|struct)\b', prev_line) and
3693 not Search(r'\\$', prev_line)):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003694 # Try a bit harder to find the beginning of the class. This is to
3695 # account for multi-line base-specifier lists, e.g.:
3696 # class Derived
3697 # : public Base {
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003698 end_class_head = class_info.starting_linenum
3699 for i in range(class_info.starting_linenum, linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003700 if Search(r'\{\s*$', clean_lines.lines[i]):
3701 end_class_head = i
3702 break
3703 if end_class_head < linenum - 1:
3704 error(filename, linenum, 'whitespace/blank_line', 3,
3705 '"%s:" should be preceded by a blank line' % matched.group(1))
3706
3707
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003708def GetPreviousNonBlankLine(clean_lines, linenum):
3709 """Return the most recent non-blank line and its line number.
3710
3711 Args:
3712 clean_lines: A CleansedLines instance containing the file contents.
3713 linenum: The number of the line to check.
3714
3715 Returns:
3716 A tuple with two elements. The first element is the contents of the last
3717 non-blank line before the current line, or the empty string if this is the
3718 first non-blank line. The second is the line number of that line, or -1
3719 if this is the first non-blank line.
3720 """
3721
3722 prevlinenum = linenum - 1
3723 while prevlinenum >= 0:
3724 prevline = clean_lines.elided[prevlinenum]
3725 if not IsBlankLine(prevline): # if not a blank line...
3726 return (prevline, prevlinenum)
3727 prevlinenum -= 1
3728 return ('', -1)
3729
3730
3731def CheckBraces(filename, clean_lines, linenum, error):
3732 """Looks for misplaced braces (e.g. at the end of line).
3733
3734 Args:
3735 filename: The name of the current file.
3736 clean_lines: A CleansedLines instance containing the file.
3737 linenum: The number of the line to check.
3738 error: The function to call with any errors found.
3739 """
3740
3741 line = clean_lines.elided[linenum] # get rid of comments and strings
3742
3743 if Match(r'\s*{\s*$', line):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003744 # We allow an open brace to start a line in the case where someone is using
3745 # braces in a block to explicitly create a new scope, which is commonly used
3746 # to control the lifetime of stack-allocated variables. Braces are also
3747 # used for brace initializers inside function calls. We don't detect this
3748 # perfectly: we just don't complain if the last non-whitespace character on
3749 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
3750 # previous line starts a preprocessor block.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003751 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003752 if (not Search(r'[,;:}{(]\s*$', prevline) and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003753 not Match(r'\s*#', prevline)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003754 error(filename, linenum, 'whitespace/braces', 4,
3755 '{ should almost always be at the end of the previous line')
3756
3757 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003758 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003759 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3760 if Match(r'\s*}\s*$', prevline):
3761 error(filename, linenum, 'whitespace/newline', 4,
3762 'An else should appear on the same line as the preceding }')
3763
3764 # If braces come on one side of an else, they should be on both.
3765 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003766 if Search(r'else if\s*\(', line): # could be multi-line if
3767 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3768 # find the ( after the if
3769 pos = line.find('else if')
3770 pos = line.find('(', pos)
3771 if pos > 0:
3772 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3773 brace_on_right = endline[endpos:].find('{') != -1
3774 if brace_on_left != brace_on_right: # must be brace after if
3775 error(filename, linenum, 'readability/braces', 5,
3776 'If an else has a brace on one side, it should have it on both')
3777 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3778 error(filename, linenum, 'readability/braces', 5,
3779 'If an else has a brace on one side, it should have it on both')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003780
3781 # Likewise, an else should never have the else clause on the same line
3782 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3783 error(filename, linenum, 'whitespace/newline', 4,
3784 'Else clause should never be on same line as else (use 2 lines)')
3785
3786 # In the same way, a do/while should never be on one line
3787 if Match(r'\s*do [^\s{]', line):
3788 error(filename, linenum, 'whitespace/newline', 4,
3789 'do/while clauses should not be on a single line')
3790
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003791 # Check single-line if/else bodies. The style guide says 'curly braces are not
3792 # required for single-line statements'. We additionally allow multi-line,
3793 # single statements, but we reject anything with more than one semicolon in
3794 # it. This means that the first semicolon after the if should be at the end of
3795 # its line, and the line after that should have an indent level equal to or
3796 # lower than the if. We also check for ambiguous if/else nesting without
3797 # braces.
3798 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3799 if if_else_match and not Match(r'\s*#', line):
3800 if_indent = GetIndentLevel(line)
3801 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3802 if_match = Search(r'\bif\s*\(', line)
3803 if if_match:
3804 # This could be a multiline if condition, so find the end first.
3805 pos = if_match.end() - 1
3806 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3807 # Check for an opening brace, either directly after the if or on the next
3808 # line. If found, this isn't a single-statement conditional.
3809 if (not Match(r'\s*{', endline[endpos:])
3810 and not (Match(r'\s*$', endline[endpos:])
3811 and endlinenum < (len(clean_lines.elided) - 1)
3812 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3813 while (endlinenum < len(clean_lines.elided)
3814 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3815 endlinenum += 1
3816 endpos = 0
3817 if endlinenum < len(clean_lines.elided):
3818 endline = clean_lines.elided[endlinenum]
3819 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3820 # methods) and a single \ after the semicolon (for macros)
3821 endpos = endline.find(';')
3822 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.com59146752014-08-11 20:20:55 +00003823 # Semicolon isn't the last character, there's something trailing.
3824 # Output a warning if the semicolon is not contained inside
3825 # a lambda expression.
3826 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3827 endline):
3828 error(filename, linenum, 'readability/braces', 4,
3829 'If/else bodies with multiple statements require braces')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003830 elif endlinenum < len(clean_lines.elided) - 1:
3831 # Make sure the next line is dedented
3832 next_line = clean_lines.elided[endlinenum + 1]
3833 next_indent = GetIndentLevel(next_line)
3834 # With ambiguous nested if statements, this will error out on the
3835 # if that *doesn't* match the else, regardless of whether it's the
3836 # inner one or outer one.
3837 if (if_match and Match(r'\s*else\b', next_line)
3838 and next_indent != if_indent):
3839 error(filename, linenum, 'readability/braces', 4,
3840 'Else clause should be indented at the same level as if. '
3841 'Ambiguous nested if/else chains require braces.')
3842 elif next_indent > if_indent:
3843 error(filename, linenum, 'readability/braces', 4,
3844 'If/else bodies with multiple statements require braces')
3845
3846
3847def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3848 """Looks for redundant trailing semicolon.
3849
3850 Args:
3851 filename: The name of the current file.
3852 clean_lines: A CleansedLines instance containing the file.
3853 linenum: The number of the line to check.
3854 error: The function to call with any errors found.
3855 """
3856
3857 line = clean_lines.elided[linenum]
3858
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003859 # Block bodies should not be followed by a semicolon. Due to C++11
3860 # brace initialization, there are more places where semicolons are
3861 # required than not, so we use a whitelist approach to check these
3862 # rather than a blacklist. These are the places where "};" should
3863 # be replaced by just "}":
3864 # 1. Some flavor of block following closing parenthesis:
3865 # for (;;) {};
3866 # while (...) {};
3867 # switch (...) {};
3868 # Function(...) {};
3869 # if (...) {};
3870 # if (...) else if (...) {};
3871 #
3872 # 2. else block:
3873 # if (...) else {};
3874 #
3875 # 3. const member function:
3876 # Function(...) const {};
3877 #
3878 # 4. Block following some statement:
3879 # x = 42;
3880 # {};
3881 #
3882 # 5. Block at the beginning of a function:
3883 # Function(...) {
3884 # {};
3885 # }
3886 #
3887 # Note that naively checking for the preceding "{" will also match
3888 # braces inside multi-dimensional arrays, but this is fine since
3889 # that expression will not contain semicolons.
3890 #
3891 # 6. Block following another block:
3892 # while (true) {}
3893 # {};
3894 #
3895 # 7. End of namespaces:
3896 # namespace {};
3897 #
3898 # These semicolons seems far more common than other kinds of
3899 # redundant semicolons, possibly due to people converting classes
3900 # to namespaces. For now we do not warn for this case.
3901 #
3902 # Try matching case 1 first.
3903 match = Match(r'^(.*\)\s*)\{', line)
3904 if match:
3905 # Matched closing parenthesis (case 1). Check the token before the
3906 # matching opening parenthesis, and don't warn if it looks like a
3907 # macro. This avoids these false positives:
3908 # - macro that defines a base class
3909 # - multi-line macro that defines a base class
3910 # - macro that defines the whole class-head
3911 #
3912 # But we still issue warnings for macros that we know are safe to
3913 # warn, specifically:
3914 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3915 # - TYPED_TEST
3916 # - INTERFACE_DEF
3917 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3918 #
3919 # We implement a whitelist of safe macros instead of a blacklist of
3920 # unsafe macros, even though the latter appears less frequently in
3921 # google code and would have been easier to implement. This is because
3922 # the downside for getting the whitelist wrong means some extra
3923 # semicolons, while the downside for getting the blacklist wrong
3924 # would result in compile errors.
3925 #
3926 # In addition to macros, we also don't want to warn on compound
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003927 # literals and lambdas.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003928 closing_brace_pos = match.group(1).rfind(')')
3929 opening_parenthesis = ReverseCloseExpression(
3930 clean_lines, linenum, closing_brace_pos)
3931 if opening_parenthesis[2] > -1:
3932 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
3933 macro = Search(r'\b([A-Z_]+)\s*$', line_prefix)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003934 func = Match(r'^(.*\])\s*$', line_prefix)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003935 if ((macro and
3936 macro.group(1) not in (
3937 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3938 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3939 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003940 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003941 Search(r'\s+=\s*$', line_prefix)):
3942 match = None
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003943 if (match and
3944 opening_parenthesis[1] > 1 and
3945 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3946 # Multi-line lambda-expression
3947 match = None
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003948
3949 else:
3950 # Try matching cases 2-3.
3951 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3952 if not match:
3953 # Try matching cases 4-6. These are always matched on separate lines.
3954 #
3955 # Note that we can't simply concatenate the previous line to the
3956 # current line and do a single match, otherwise we may output
3957 # duplicate warnings for the blank line case:
3958 # if (cond) {
3959 # // blank line
3960 # }
3961 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3962 if prevline and Search(r'[;{}]\s*$', prevline):
3963 match = Match(r'^(\s*)\{', line)
3964
3965 # Check matching closing brace
3966 if match:
3967 (endline, endlinenum, endpos) = CloseExpression(
3968 clean_lines, linenum, len(match.group(1)))
3969 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3970 # Current {} pair is eligible for semicolon check, and we have found
3971 # the redundant semicolon, output warning here.
3972 #
3973 # Note: because we are scanning forward for opening braces, and
3974 # outputting warnings for the matching closing brace, if there are
3975 # nested blocks with trailing semicolons, we will get the error
3976 # messages in reversed order.
3977 error(filename, endlinenum, 'readability/braces', 4,
3978 "You don't need a ; after a }")
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003979
3980
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003981def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3982 """Look for empty loop/conditional body with only a single semicolon.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003983
3984 Args:
3985 filename: The name of the current file.
3986 clean_lines: A CleansedLines instance containing the file.
3987 linenum: The number of the line to check.
3988 error: The function to call with any errors found.
3989 """
3990
3991 # Search for loop keywords at the beginning of the line. Because only
3992 # whitespaces are allowed before the keywords, this will also ignore most
3993 # do-while-loops, since those lines should start with closing brace.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003994 #
3995 # We also check "if" blocks here, since an empty conditional block
3996 # is likely an error.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003997 line = clean_lines.elided[linenum]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003998 matched = Match(r'\s*(for|while|if)\s*\(', line)
3999 if matched:
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004000 # Find the end of the conditional expression
4001 (end_line, end_linenum, end_pos) = CloseExpression(
4002 clean_lines, linenum, line.find('('))
4003
4004 # Output warning if what follows the condition expression is a semicolon.
4005 # No warning for all other cases, including whitespace or newline, since we
4006 # have a separate check for semicolons preceded by whitespace.
4007 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004008 if matched.group(1) == 'if':
4009 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
4010 'Empty conditional bodies should use {}')
4011 else:
4012 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
4013 'Empty loop bodies should use {} or continue')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004014
4015
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004016def FindCheckMacro(line):
4017 """Find a replaceable CHECK-like macro.
4018
4019 Args:
4020 line: line to search on.
4021 Returns:
4022 (macro name, start position), or (None, -1) if no replaceable
4023 macro is found.
4024 """
4025 for macro in _CHECK_MACROS:
4026 i = line.find(macro)
4027 if i >= 0:
4028 # Find opening parenthesis. Do a regular expression match here
4029 # to make sure that we are matching the expected CHECK macro, as
4030 # opposed to some other macro that happens to contain the CHECK
4031 # substring.
4032 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4033 if not matched:
4034 continue
4035 return (macro, len(matched.group(1)))
4036 return (None, -1)
4037
4038
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004039def CheckCheck(filename, clean_lines, linenum, error):
4040 """Checks the use of CHECK and EXPECT macros.
4041
4042 Args:
4043 filename: The name of the current file.
4044 clean_lines: A CleansedLines instance containing the file.
4045 linenum: The number of the line to check.
4046 error: The function to call with any errors found.
4047 """
4048
4049 # Decide the set of replacement macros that should be suggested
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004050 lines = clean_lines.elided
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004051 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4052 if not check_macro:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004053 return
4054
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004055 # Find end of the boolean expression by matching parentheses
4056 (last_line, end_line, end_pos) = CloseExpression(
4057 clean_lines, linenum, start_pos)
4058 if end_pos < 0:
4059 return
avakulenko@google.com59146752014-08-11 20:20:55 +00004060
4061 # If the check macro is followed by something other than a
4062 # semicolon, assume users will log their own custom error messages
4063 # and don't suggest any replacements.
4064 if not Match(r'\s*;', last_line[end_pos:]):
4065 return
4066
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004067 if linenum == end_line:
4068 expression = lines[linenum][start_pos + 1:end_pos - 1]
4069 else:
4070 expression = lines[linenum][start_pos + 1:]
4071 for i in xrange(linenum + 1, end_line):
4072 expression += lines[i]
4073 expression += last_line[0:end_pos - 1]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004074
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004075 # Parse expression so that we can take parentheses into account.
4076 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4077 # which is not replaceable by CHECK_LE.
4078 lhs = ''
4079 rhs = ''
4080 operator = None
4081 while expression:
4082 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4083 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4084 if matched:
4085 token = matched.group(1)
4086 if token == '(':
4087 # Parenthesized operand
4088 expression = matched.group(2)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004089 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004090 if end < 0:
4091 return # Unmatched parenthesis
4092 lhs += '(' + expression[0:end]
4093 expression = expression[end:]
4094 elif token in ('&&', '||'):
4095 # Logical and/or operators. This means the expression
4096 # contains more than one term, for example:
4097 # CHECK(42 < a && a < b);
4098 #
4099 # These are not replaceable with CHECK_LE, so bail out early.
4100 return
4101 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4102 # Non-relational operator
4103 lhs += token
4104 expression = matched.group(2)
4105 else:
4106 # Relational operator
4107 operator = token
4108 rhs = matched.group(2)
4109 break
4110 else:
4111 # Unparenthesized operand. Instead of appending to lhs one character
4112 # at a time, we do another regular expression match to consume several
4113 # characters at once if possible. Trivial benchmark shows that this
4114 # is more efficient when the operands are longer than a single
4115 # character, which is generally the case.
4116 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4117 if not matched:
4118 matched = Match(r'^(\s*\S)(.*)$', expression)
4119 if not matched:
4120 break
4121 lhs += matched.group(1)
4122 expression = matched.group(2)
4123
4124 # Only apply checks if we got all parts of the boolean expression
4125 if not (lhs and operator and rhs):
4126 return
4127
4128 # Check that rhs do not contain logical operators. We already know
4129 # that lhs is fine since the loop above parses out && and ||.
4130 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4131 return
4132
4133 # At least one of the operands must be a constant literal. This is
4134 # to avoid suggesting replacements for unprintable things like
4135 # CHECK(variable != iterator)
4136 #
4137 # The following pattern matches decimal, hex integers, strings, and
4138 # characters (in that order).
4139 lhs = lhs.strip()
4140 rhs = rhs.strip()
4141 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4142 if Match(match_constant, lhs) or Match(match_constant, rhs):
4143 # Note: since we know both lhs and rhs, we can provide a more
4144 # descriptive error message like:
4145 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4146 # Instead of:
4147 # Consider using CHECK_EQ instead of CHECK(a == b)
4148 #
4149 # We are still keeping the less descriptive message because if lhs
4150 # or rhs gets long, the error message might become unreadable.
4151 error(filename, linenum, 'readability/check', 2,
4152 'Consider using %s instead of %s(a %s b)' % (
4153 _CHECK_REPLACEMENT[check_macro][operator],
4154 check_macro, operator))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004155
4156
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004157def CheckAltTokens(filename, clean_lines, linenum, error):
4158 """Check alternative keywords being used in boolean expressions.
4159
4160 Args:
4161 filename: The name of the current file.
4162 clean_lines: A CleansedLines instance containing the file.
4163 linenum: The number of the line to check.
4164 error: The function to call with any errors found.
4165 """
4166 line = clean_lines.elided[linenum]
4167
4168 # Avoid preprocessor lines
4169 if Match(r'^\s*#', line):
4170 return
4171
4172 # Last ditch effort to avoid multi-line comments. This will not help
4173 # if the comment started before the current line or ended after the
4174 # current line, but it catches most of the false positives. At least,
4175 # it provides a way to workaround this warning for people who use
4176 # multi-line comments in preprocessor macros.
4177 #
4178 # TODO(unknown): remove this once cpplint has better support for
4179 # multi-line comments.
4180 if line.find('/*') >= 0 or line.find('*/') >= 0:
4181 return
4182
4183 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4184 error(filename, linenum, 'readability/alt_tokens', 2,
4185 'Use operator %s instead of %s' % (
4186 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4187
4188
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004189def GetLineWidth(line):
4190 """Determines the width of the line in column positions.
4191
4192 Args:
4193 line: A string, which may be a Unicode string.
4194
4195 Returns:
4196 The width of the line in column positions, accounting for Unicode
4197 combining characters and wide characters.
4198 """
4199 if isinstance(line, unicode):
4200 width = 0
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004201 for uc in unicodedata.normalize('NFC', line):
4202 if unicodedata.east_asian_width(uc) in ('W', 'F'):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004203 width += 2
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004204 elif not unicodedata.combining(uc):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004205 width += 1
4206 return width
4207 else:
4208 return len(line)
4209
4210
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004211def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004212 error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004213 """Checks rules from the 'C++ style rules' section of cppguide.html.
4214
4215 Most of these rules are hard to test (naming, comment style), but we
4216 do what we can. In particular we check for 2-space indents, line lengths,
4217 tab usage, spaces inside code, etc.
4218
4219 Args:
4220 filename: The name of the current file.
4221 clean_lines: A CleansedLines instance containing the file.
4222 linenum: The number of the line to check.
4223 file_extension: The extension (without the dot) of the filename.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004224 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004225 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004226 error: The function to call with any errors found.
4227 """
4228
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004229 # Don't use "elided" lines here, otherwise we can't check commented lines.
4230 # Don't want to use "raw" either, because we don't want to check inside C++11
4231 # raw strings,
4232 raw_lines = clean_lines.lines_without_raw_strings
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004233 line = raw_lines[linenum]
4234
4235 if line.find('\t') != -1:
4236 error(filename, linenum, 'whitespace/tab', 1,
4237 'Tab found; better to use spaces')
4238
4239 # One or three blank spaces at the beginning of the line is weird; it's
4240 # hard to reconcile that with 2-space indents.
4241 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4242 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4243 # if(RLENGTH > 20) complain = 0;
4244 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4245 # if(match(prev, "&& *$")) complain = 0;
4246 # if(match(prev, "\\|\\| *$")) complain = 0;
4247 # if(match(prev, "[\",=><] *$")) complain = 0;
4248 # if(match($0, " <<")) complain = 0;
4249 # if(match(prev, " +for \\(")) complain = 0;
4250 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004251 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4252 classinfo = nesting_state.InnermostClass()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004253 initial_spaces = 0
4254 cleansed_line = clean_lines.elided[linenum]
4255 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4256 initial_spaces += 1
4257 if line and line[-1].isspace():
4258 error(filename, linenum, 'whitespace/end_of_line', 4,
4259 'Line ends in whitespace. Consider deleting these extra spaces.')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004260 # There are certain situations we allow one space, notably for
4261 # section labels, and also lines containing multi-line raw strings.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004262 elif ((initial_spaces == 1 or initial_spaces == 3) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004263 not Match(scope_or_label_pattern, cleansed_line) and
4264 not (clean_lines.raw_lines[linenum] != line and
4265 Match(r'^\s*""', line))):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004266 error(filename, linenum, 'whitespace/indent', 3,
4267 'Weird number of spaces at line-start. '
4268 'Are you using a 2-space indent?')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004269
4270 # Check if the line is a header guard.
4271 is_header_guard = False
4272 if file_extension == 'h':
4273 cppvar = GetHeaderGuardCPPVariable(filename)
4274 if (line.startswith('#ifndef %s' % cppvar) or
4275 line.startswith('#define %s' % cppvar) or
4276 line.startswith('#endif // %s' % cppvar)):
4277 is_header_guard = True
4278 # #include lines and header guards can be long, since there's no clean way to
4279 # split them.
erg@google.com6317a9c2009-06-25 00:28:19 +00004280 #
4281 # URLs can be long too. It's possible to split these, but it makes them
4282 # harder to cut&paste.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004283 #
4284 # The "$Id:...$" comment may also get very long without it being the
4285 # developers fault.
erg@google.com6317a9c2009-06-25 00:28:19 +00004286 if (not line.startswith('#include') and not is_header_guard and
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004287 not Match(r'^\s*//.*http(s?)://\S*$', line) and
4288 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004289 line_width = GetLineWidth(line)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004290 extended_length = int((_line_length * 1.25))
4291 if line_width > extended_length:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004292 error(filename, linenum, 'whitespace/line_length', 4,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004293 'Lines should very rarely be longer than %i characters' %
4294 extended_length)
4295 elif line_width > _line_length:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004296 error(filename, linenum, 'whitespace/line_length', 2,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004297 'Lines should be <= %i characters long' % _line_length)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004298
4299 if (cleansed_line.count(';') > 1 and
4300 # for loops are allowed two ;'s (and may run over two lines).
4301 cleansed_line.find('for') == -1 and
4302 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4303 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4304 # It's ok to have many commands in a switch case that fits in 1 line
4305 not ((cleansed_line.find('case ') != -1 or
4306 cleansed_line.find('default:') != -1) and
4307 cleansed_line.find('break;') != -1)):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004308 error(filename, linenum, 'whitespace/newline', 0,
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004309 'More than one command on the same line')
4310
4311 # Some more style checks
4312 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004313 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004314 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004315 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
4316 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004317 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4318 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4319 CheckCommaSpacing(filename, clean_lines, linenum, error)
4320 CheckBracesSpacing(filename, clean_lines, linenum, error)
4321 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
4322 CheckRValueReference(filename, clean_lines, linenum, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004323 CheckCheck(filename, clean_lines, linenum, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004324 CheckAltTokens(filename, clean_lines, linenum, error)
4325 classinfo = nesting_state.InnermostClass()
4326 if classinfo:
4327 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004328
4329
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004330_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4331# Matches the first component of a filename delimited by -s and _s. That is:
4332# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4333# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4334# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4335# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4336_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4337
4338
4339def _DropCommonSuffixes(filename):
4340 """Drops common suffixes like _test.cc or -inl.h from filename.
4341
4342 For example:
4343 >>> _DropCommonSuffixes('foo/foo-inl.h')
4344 'foo/foo'
4345 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4346 'foo/bar/foo'
4347 >>> _DropCommonSuffixes('foo/foo_internal.h')
4348 'foo/foo'
4349 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4350 'foo/foo_unusualinternal'
4351
4352 Args:
4353 filename: The input filename.
4354
4355 Returns:
4356 The filename with the common suffix removed.
4357 """
4358 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4359 'inl.h', 'impl.h', 'internal.h'):
4360 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4361 filename[-len(suffix) - 1] in ('-', '_')):
4362 return filename[:-len(suffix) - 1]
4363 return os.path.splitext(filename)[0]
4364
4365
4366def _IsTestFilename(filename):
4367 """Determines if the given filename has a suffix that identifies it as a test.
4368
4369 Args:
4370 filename: The input filename.
4371
4372 Returns:
4373 True if 'filename' looks like a test, False otherwise.
4374 """
4375 if (filename.endswith('_test.cc') or
4376 filename.endswith('_unittest.cc') or
4377 filename.endswith('_regtest.cc')):
4378 return True
4379 else:
4380 return False
4381
4382
4383def _ClassifyInclude(fileinfo, include, is_system):
4384 """Figures out what kind of header 'include' is.
4385
4386 Args:
4387 fileinfo: The current file cpplint is running over. A FileInfo instance.
4388 include: The path to a #included file.
4389 is_system: True if the #include used <> rather than "".
4390
4391 Returns:
4392 One of the _XXX_HEADER constants.
4393
4394 For example:
4395 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4396 _C_SYS_HEADER
4397 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4398 _CPP_SYS_HEADER
4399 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4400 _LIKELY_MY_HEADER
4401 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4402 ... 'bar/foo_other_ext.h', False)
4403 _POSSIBLE_MY_HEADER
4404 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4405 _OTHER_HEADER
4406 """
4407 # This is a list of all standard c++ header files, except
4408 # those already checked for above.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004409 is_cpp_h = include in _CPP_HEADERS
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004410
4411 if is_system:
4412 if is_cpp_h:
4413 return _CPP_SYS_HEADER
4414 else:
4415 return _C_SYS_HEADER
4416
4417 # If the target file and the include we're checking share a
4418 # basename when we drop common extensions, and the include
4419 # lives in . , then it's likely to be owned by the target file.
4420 target_dir, target_base = (
4421 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4422 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4423 if target_base == include_base and (
4424 include_dir == target_dir or
4425 include_dir == os.path.normpath(target_dir + '/../public')):
4426 return _LIKELY_MY_HEADER
4427
4428 # If the target and include share some initial basename
4429 # component, it's possible the target is implementing the
4430 # include, so it's allowed to be first, but we'll never
4431 # complain if it's not there.
4432 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4433 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4434 if (target_first_component and include_first_component and
4435 target_first_component.group(0) ==
4436 include_first_component.group(0)):
4437 return _POSSIBLE_MY_HEADER
4438
4439 return _OTHER_HEADER
4440
4441
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004442
erg@google.com6317a9c2009-06-25 00:28:19 +00004443def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4444 """Check rules that are applicable to #include lines.
4445
4446 Strings on #include lines are NOT removed from elided line, to make
4447 certain tasks easier. However, to prevent false positives, checks
4448 applicable to #include lines in CheckLanguage must be put here.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004449
4450 Args:
4451 filename: The name of the current file.
4452 clean_lines: A CleansedLines instance containing the file.
4453 linenum: The number of the line to check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004454 include_state: An _IncludeState instance in which the headers are inserted.
4455 error: The function to call with any errors found.
4456 """
4457 fileinfo = FileInfo(filename)
erg@google.com6317a9c2009-06-25 00:28:19 +00004458 line = clean_lines.lines[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004459
4460 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.com59146752014-08-11 20:20:55 +00004461 # Only do this check if the included header follows google naming
4462 # conventions. If not, assume that it's a 3rd party API that
4463 # requires special include conventions.
4464 #
4465 # We also make an exception for Lua headers, which follow google
4466 # naming convention but not the include convention.
4467 match = Match(r'#include\s*"([^/]+\.h)"', line)
4468 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004469 error(filename, linenum, 'build/include', 4,
4470 'Include the directory when naming .h files')
4471
4472 # we shouldn't include a file more than once. actually, there are a
4473 # handful of instances where doing so is okay, but in general it's
4474 # not.
erg@google.com6317a9c2009-06-25 00:28:19 +00004475 match = _RE_PATTERN_INCLUDE.search(line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004476 if match:
4477 include = match.group(2)
4478 is_system = (match.group(1) == '<')
avakulenko@google.com59146752014-08-11 20:20:55 +00004479 duplicate_line = include_state.FindHeader(include)
4480 if duplicate_line >= 0:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004481 error(filename, linenum, 'build/include', 4,
4482 '"%s" already included at %s:%s' %
avakulenko@google.com59146752014-08-11 20:20:55 +00004483 (include, filename, duplicate_line))
4484 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4485 include_state.include_list[-1].append((include, linenum))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004486
4487 # We want to ensure that headers appear in the right order:
4488 # 1) for foo.cc, foo.h (preferred location)
4489 # 2) c system files
4490 # 3) cpp system files
4491 # 4) for foo.cc, foo.h (deprecated location)
4492 # 5) other google headers
4493 #
4494 # We classify each include statement as one of those 5 types
4495 # using a number of techniques. The include_state object keeps
4496 # track of the highest type seen, and complains if we see a
4497 # lower type after that.
4498 error_message = include_state.CheckNextIncludeOrder(
4499 _ClassifyInclude(fileinfo, include, is_system))
4500 if error_message:
4501 error(filename, linenum, 'build/include_order', 4,
4502 '%s. Should be: %s.h, c system, c++ system, other.' %
4503 (error_message, fileinfo.BaseName()))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004504 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4505 if not include_state.IsInAlphabeticalOrder(
4506 clean_lines, linenum, canonical_include):
erg@google.com26970fa2009-11-17 18:07:32 +00004507 error(filename, linenum, 'build/include_alpha', 4,
4508 'Include "%s" not in alphabetical order' % include)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004509 include_state.SetLastHeader(canonical_include)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004510
erg@google.com6317a9c2009-06-25 00:28:19 +00004511 # Look for any of the stream classes that are part of standard C++.
4512 match = _RE_PATTERN_INCLUDE.match(line)
4513 if match:
4514 include = match.group(2)
4515 if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
4516 # Many unit tests use cout, so we exempt them.
4517 if not _IsTestFilename(filename):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004518 # Suggest a different header for ostream
4519 if include == 'ostream':
4520 error(filename, linenum, 'readability/streams', 3,
4521 'For logging, include "base/logging.h" instead of <ostream>.')
4522 else:
4523 error(filename, linenum, 'readability/streams', 3,
4524 'Streams are highly discouraged.')
erg@google.com6317a9c2009-06-25 00:28:19 +00004525
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004526
4527def _GetTextInside(text, start_pattern):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004528 r"""Retrieves all the text between matching open and close parentheses.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004529
4530 Given a string of lines and a regular expression string, retrieve all the text
4531 following the expression and between opening punctuation symbols like
4532 (, [, or {, and the matching close-punctuation symbol. This properly nested
4533 occurrences of the punctuations, so for the text like
4534 printf(a(), b(c()));
4535 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4536 start_pattern must match string having an open punctuation symbol at the end.
4537
4538 Args:
4539 text: The lines to extract text. Its comments and strings must be elided.
4540 It can be single line and can span multiple lines.
4541 start_pattern: The regexp string indicating where to start extracting
4542 the text.
4543 Returns:
4544 The extracted text.
4545 None if either the opening string or ending punctuation could not be found.
4546 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004547 # TODO(unknown): Audit cpplint.py to see what places could be profitably
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004548 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4549
4550 # Give opening punctuations to get the matching close-punctuations.
4551 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4552 closing_punctuation = set(matching_punctuation.itervalues())
4553
4554 # Find the position to start extracting text.
4555 match = re.search(start_pattern, text, re.M)
4556 if not match: # start_pattern not found in text.
4557 return None
4558 start_position = match.end(0)
4559
4560 assert start_position > 0, (
4561 'start_pattern must ends with an opening punctuation.')
4562 assert text[start_position - 1] in matching_punctuation, (
4563 'start_pattern must ends with an opening punctuation.')
4564 # Stack of closing punctuations we expect to have in text after position.
4565 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4566 position = start_position
4567 while punctuation_stack and position < len(text):
4568 if text[position] == punctuation_stack[-1]:
4569 punctuation_stack.pop()
4570 elif text[position] in closing_punctuation:
4571 # A closing punctuation without matching opening punctuations.
4572 return None
4573 elif text[position] in matching_punctuation:
4574 punctuation_stack.append(matching_punctuation[text[position]])
4575 position += 1
4576 if punctuation_stack:
4577 # Opening punctuations left without matching close-punctuations.
4578 return None
4579 # punctuations match.
4580 return text[start_position:position - 1]
4581
4582
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004583# Patterns for matching call-by-reference parameters.
4584#
4585# Supports nested templates up to 2 levels deep using this messy pattern:
4586# < (?: < (?: < [^<>]*
4587# >
4588# | [^<>] )*
4589# >
4590# | [^<>] )*
4591# >
4592_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4593_RE_PATTERN_TYPE = (
4594 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
4595 r'(?:\w|'
4596 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4597 r'::)+')
4598# A call-by-reference parameter ends with '& identifier'.
4599_RE_PATTERN_REF_PARAM = re.compile(
4600 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4601 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4602# A call-by-const-reference parameter either ends with 'const& identifier'
4603# or looks like 'const type& identifier' when 'type' is atomic.
4604_RE_PATTERN_CONST_REF_PARAM = (
4605 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4606 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
4607
4608
4609def CheckLanguage(filename, clean_lines, linenum, file_extension,
4610 include_state, nesting_state, error):
erg@google.com6317a9c2009-06-25 00:28:19 +00004611 """Checks rules from the 'C++ language rules' section of cppguide.html.
4612
4613 Some of these rules are hard to test (function overloading, using
4614 uint32 inappropriately), but we do the best we can.
4615
4616 Args:
4617 filename: The name of the current file.
4618 clean_lines: A CleansedLines instance containing the file.
4619 linenum: The number of the line to check.
4620 file_extension: The extension (without the dot) of the filename.
4621 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004622 nesting_state: A NestingState instance which maintains information about
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004623 the current stack of nested blocks being parsed.
erg@google.com6317a9c2009-06-25 00:28:19 +00004624 error: The function to call with any errors found.
4625 """
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004626 # If the line is empty or consists of entirely a comment, no need to
4627 # check it.
4628 line = clean_lines.elided[linenum]
4629 if not line:
4630 return
4631
erg@google.com6317a9c2009-06-25 00:28:19 +00004632 match = _RE_PATTERN_INCLUDE.search(line)
4633 if match:
4634 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4635 return
4636
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004637 # Reset include state across preprocessor directives. This is meant
4638 # to silence warnings for conditional includes.
avakulenko@google.com59146752014-08-11 20:20:55 +00004639 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4640 if match:
4641 include_state.ResetSection(match.group(1))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004642
4643 # Make Windows paths like Unix.
4644 fullname = os.path.abspath(filename).replace('\\', '/')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004645
4646 # Perform other checks now that we are sure that this is not an include line
4647 CheckCasts(filename, clean_lines, linenum, error)
4648 CheckGlobalStatic(filename, clean_lines, linenum, error)
4649 CheckPrintf(filename, clean_lines, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004650
4651 if file_extension == 'h':
4652 # TODO(unknown): check that 1-arg constructors are explicit.
4653 # How to tell it's a constructor?
4654 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.com59146752014-08-11 20:20:55 +00004655 # TODO(unknown): check that classes declare or disable copy/assign
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004656 # (level 1 error)
4657 pass
4658
4659 # Check if people are using the verboten C basic types. The only exception
4660 # we regularly allow is "unsigned short port" for port.
4661 if Search(r'\bshort port\b', line):
4662 if not Search(r'\bunsigned short port\b', line):
4663 error(filename, linenum, 'runtime/int', 4,
4664 'Use "unsigned short" for ports, not "short"')
4665 else:
4666 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4667 if match:
4668 error(filename, linenum, 'runtime/int', 4,
4669 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4670
erg@google.com26970fa2009-11-17 18:07:32 +00004671 # Check if some verboten operator overloading is going on
4672 # TODO(unknown): catch out-of-line unary operator&:
4673 # class X {};
4674 # int operator&(const X& x) { return 42; } // unary operator&
4675 # The trick is it's hard to tell apart from binary operator&:
4676 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4677 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4678 error(filename, linenum, 'runtime/operator', 4,
4679 'Unary operator& is dangerous. Do not use it.')
4680
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004681 # Check for suspicious usage of "if" like
4682 # } if (a == b) {
4683 if Search(r'\}\s*if\s*\(', line):
4684 error(filename, linenum, 'readability/braces', 4,
4685 'Did you mean "else if"? If not, start a new line for "if".')
4686
4687 # Check for potential format string bugs like printf(foo).
4688 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4689 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004690 # TODO(unknown): Catch the following case. Need to change the calling
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004691 # convention of the whole function to process multiple line to handle it.
4692 # printf(
4693 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4694 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4695 if printf_args:
4696 match = Match(r'([\w.\->()]+)$', printf_args)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004697 if match and match.group(1) != '__VA_ARGS__':
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004698 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4699 line, re.I).group(1)
4700 error(filename, linenum, 'runtime/printf', 4,
4701 'Potential format string bug. Do %s("%%s", %s) instead.'
4702 % (function_name, match.group(1)))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004703
4704 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4705 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4706 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4707 error(filename, linenum, 'runtime/memset', 4,
4708 'Did you mean "memset(%s, 0, %s)"?'
4709 % (match.group(1), match.group(2)))
4710
4711 if Search(r'\busing namespace\b', line):
4712 error(filename, linenum, 'build/namespaces', 5,
4713 'Do not use namespace using-directives. '
4714 'Use using-declarations instead.')
4715
4716 # Detect variable-length arrays.
4717 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4718 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4719 match.group(3).find(']') == -1):
4720 # Split the size using space and arithmetic operators as delimiters.
4721 # If any of the resulting tokens are not compile time constants then
4722 # report the error.
4723 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4724 is_const = True
4725 skip_next = False
4726 for tok in tokens:
4727 if skip_next:
4728 skip_next = False
4729 continue
4730
4731 if Search(r'sizeof\(.+\)', tok): continue
4732 if Search(r'arraysize\(\w+\)', tok): continue
4733
4734 tok = tok.lstrip('(')
4735 tok = tok.rstrip(')')
4736 if not tok: continue
4737 if Match(r'\d+', tok): continue
4738 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4739 if Match(r'k[A-Z0-9]\w*', tok): continue
4740 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4741 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4742 # A catch all for tricky sizeof cases, including 'sizeof expression',
4743 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004744 # requires skipping the next token because we split on ' ' and '*'.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004745 if tok.startswith('sizeof'):
4746 skip_next = True
4747 continue
4748 is_const = False
4749 break
4750 if not is_const:
4751 error(filename, linenum, 'runtime/arrays', 1,
4752 'Do not use variable-length arrays. Use an appropriately named '
4753 "('k' followed by CamelCase) compile-time constant for the size.")
4754
avakulenko@google.com59146752014-08-11 20:20:55 +00004755 # If DISALLOW_COPY_AND_ASSIGN DISALLOW_IMPLICIT_CONSTRUCTORS is present,
4756 # then it should be the last thing in the class declaration.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004757 match = Match(
4758 (r'\s*'
avakulenko@google.com59146752014-08-11 20:20:55 +00004759 r'(DISALLOW_(COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004760 r'\(.*\);$'),
4761 line)
4762 if match and linenum + 1 < clean_lines.NumLines():
4763 next_line = clean_lines.elided[linenum + 1]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004764 # We allow some, but not all, declarations of variables to be present
4765 # in the statement that defines the class. The [\w\*,\s]* fragment of
4766 # the regular expression below allows users to declare instances of
4767 # the class or pointers to instances, but not less common types such
4768 # as function pointers or arrays. It's a tradeoff between allowing
4769 # reasonable code and avoiding trying to parse more C++ using regexps.
4770 if not Search(r'^\s*}[\w\*,\s]*;', next_line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004771 error(filename, linenum, 'readability/constructors', 3,
4772 match.group(1) + ' should be the last thing in the class')
4773
4774 # Check for use of unnamed namespaces in header files. Registration
4775 # macros are typically OK, so we allow use of "namespace {" on lines
4776 # that end with backslashes.
4777 if (file_extension == 'h'
4778 and Search(r'\bnamespace\s*{', line)
4779 and line[-1] != '\\'):
4780 error(filename, linenum, 'build/namespaces', 4,
4781 'Do not use unnamed namespaces in header files. See '
4782 'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
4783 ' for more information.')
4784
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004785
4786def CheckGlobalStatic(filename, clean_lines, linenum, error):
4787 """Check for unsafe global or static objects.
4788
4789 Args:
4790 filename: The name of the current file.
4791 clean_lines: A CleansedLines instance containing the file.
4792 linenum: The number of the line to check.
4793 error: The function to call with any errors found.
4794 """
4795 line = clean_lines.elided[linenum]
4796
avakulenko@google.com59146752014-08-11 20:20:55 +00004797 # Match two lines at a time to support multiline declarations
4798 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4799 line += clean_lines.elided[linenum + 1].strip()
4800
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004801 # Check for people declaring static/global STL strings at the top level.
4802 # This is dangerous because the C++ language does not guarantee that
4803 # globals with constructors are initialized before the first access.
4804 match = Match(
4805 r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
4806 line)
avakulenko@google.com59146752014-08-11 20:20:55 +00004807
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004808 # Remove false positives:
4809 # - String pointers (as opposed to values).
4810 # string *pointer
4811 # const string *pointer
4812 # string const *pointer
4813 # string *const pointer
4814 #
4815 # - Functions and template specializations.
4816 # string Function<Type>(...
4817 # string Class<Type>::Method(...
4818 #
4819 # - Operators. These are matched separately because operator names
4820 # cross non-word boundaries, and trying to match both operators
4821 # and functions at the same time would decrease accuracy of
4822 # matching identifiers.
4823 # string Class::operator*()
4824 if (match and
4825 not Search(r'\bstring\b(\s+const)?\s*\*\s*(const\s+)?\w', line) and
4826 not Search(r'\boperator\W', line) and
avakulenko@google.com59146752014-08-11 20:20:55 +00004827 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(3))):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004828 error(filename, linenum, 'runtime/string', 4,
4829 'For a static/global string constant, use a C style string instead: '
4830 '"%schar %s[]".' %
4831 (match.group(1), match.group(2)))
4832
4833 if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
4834 error(filename, linenum, 'runtime/init', 4,
4835 'You seem to be initializing a member variable with itself.')
4836
4837
4838def CheckPrintf(filename, clean_lines, linenum, error):
4839 """Check for printf related issues.
4840
4841 Args:
4842 filename: The name of the current file.
4843 clean_lines: A CleansedLines instance containing the file.
4844 linenum: The number of the line to check.
4845 error: The function to call with any errors found.
4846 """
4847 line = clean_lines.elided[linenum]
4848
4849 # When snprintf is used, the second argument shouldn't be a literal.
4850 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4851 if match and match.group(2) != '0':
4852 # If 2nd arg is zero, snprintf is used to calculate size.
4853 error(filename, linenum, 'runtime/printf', 3,
4854 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4855 'to snprintf.' % (match.group(1), match.group(2)))
4856
4857 # Check if some verboten C functions are being used.
avakulenko@google.com59146752014-08-11 20:20:55 +00004858 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004859 error(filename, linenum, 'runtime/printf', 5,
4860 'Never use sprintf. Use snprintf instead.')
avakulenko@google.com59146752014-08-11 20:20:55 +00004861 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004862 if match:
4863 error(filename, linenum, 'runtime/printf', 4,
4864 'Almost always, snprintf is better than %s' % match.group(1))
4865
4866
4867def IsDerivedFunction(clean_lines, linenum):
4868 """Check if current line contains an inherited function.
4869
4870 Args:
4871 clean_lines: A CleansedLines instance containing the file.
4872 linenum: The number of the line to check.
4873 Returns:
4874 True if current line contains a function with "override"
4875 virt-specifier.
4876 """
avakulenko@google.com59146752014-08-11 20:20:55 +00004877 # Scan back a few lines for start of current function
4878 for i in xrange(linenum, max(-1, linenum - 10), -1):
4879 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4880 if match:
4881 # Look for "override" after the matching closing parenthesis
4882 line, _, closing_paren = CloseExpression(
4883 clean_lines, i, len(match.group(1)))
4884 return (closing_paren >= 0 and
4885 Search(r'\boverride\b', line[closing_paren:]))
4886 return False
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004887
4888
4889def IsInitializerList(clean_lines, linenum):
4890 """Check if current line is inside constructor initializer list.
4891
4892 Args:
4893 clean_lines: A CleansedLines instance containing the file.
4894 linenum: The number of the line to check.
4895 Returns:
4896 True if current line appears to be inside constructor initializer
4897 list, False otherwise.
4898 """
4899 for i in xrange(linenum, 1, -1):
4900 line = clean_lines.elided[i]
4901 if i == linenum:
4902 remove_function_body = Match(r'^(.*)\{\s*$', line)
4903 if remove_function_body:
4904 line = remove_function_body.group(1)
4905
4906 if Search(r'\s:\s*\w+[({]', line):
4907 # A lone colon tend to indicate the start of a constructor
4908 # initializer list. It could also be a ternary operator, which
4909 # also tend to appear in constructor initializer lists as
4910 # opposed to parameter lists.
4911 return True
4912 if Search(r'\}\s*,\s*$', line):
4913 # A closing brace followed by a comma is probably the end of a
4914 # brace-initialized member in constructor initializer list.
4915 return True
4916 if Search(r'[{};]\s*$', line):
4917 # Found one of the following:
4918 # - A closing brace or semicolon, probably the end of the previous
4919 # function.
4920 # - An opening brace, probably the start of current class or namespace.
4921 #
4922 # Current line is probably not inside an initializer list since
4923 # we saw one of those things without seeing the starting colon.
4924 return False
4925
4926 # Got to the beginning of the file without seeing the start of
4927 # constructor initializer list.
4928 return False
4929
4930
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004931def CheckForNonConstReference(filename, clean_lines, linenum,
4932 nesting_state, error):
4933 """Check for non-const references.
4934
4935 Separate from CheckLanguage since it scans backwards from current
4936 line, instead of scanning forward.
4937
4938 Args:
4939 filename: The name of the current file.
4940 clean_lines: A CleansedLines instance containing the file.
4941 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004942 nesting_state: A NestingState instance which maintains information about
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004943 the current stack of nested blocks being parsed.
4944 error: The function to call with any errors found.
4945 """
4946 # Do nothing if there is no '&' on current line.
4947 line = clean_lines.elided[linenum]
4948 if '&' not in line:
4949 return
4950
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004951 # If a function is inherited, current function doesn't have much of
4952 # a choice, so any non-const references should not be blamed on
4953 # derived function.
4954 if IsDerivedFunction(clean_lines, linenum):
4955 return
4956
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004957 # Long type names may be broken across multiple lines, usually in one
4958 # of these forms:
4959 # LongType
4960 # ::LongTypeContinued &identifier
4961 # LongType::
4962 # LongTypeContinued &identifier
4963 # LongType<
4964 # ...>::LongTypeContinued &identifier
4965 #
4966 # If we detected a type split across two lines, join the previous
4967 # line to current line so that we can match const references
4968 # accordingly.
4969 #
4970 # Note that this only scans back one line, since scanning back
4971 # arbitrary number of lines would be expensive. If you have a type
4972 # that spans more than 2 lines, please use a typedef.
4973 if linenum > 1:
4974 previous = None
4975 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
4976 # previous_line\n + ::current_line
4977 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
4978 clean_lines.elided[linenum - 1])
4979 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
4980 # previous_line::\n + current_line
4981 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
4982 clean_lines.elided[linenum - 1])
4983 if previous:
4984 line = previous.group(1) + line.lstrip()
4985 else:
4986 # Check for templated parameter that is split across multiple lines
4987 endpos = line.rfind('>')
4988 if endpos > -1:
4989 (_, startline, startpos) = ReverseCloseExpression(
4990 clean_lines, linenum, endpos)
4991 if startpos > -1 and startline < linenum:
4992 # Found the matching < on an earlier line, collect all
4993 # pieces up to current line.
4994 line = ''
4995 for i in xrange(startline, linenum + 1):
4996 line += clean_lines.elided[i].strip()
4997
4998 # Check for non-const references in function parameters. A single '&' may
4999 # found in the following places:
5000 # inside expression: binary & for bitwise AND
5001 # inside expression: unary & for taking the address of something
5002 # inside declarators: reference parameter
5003 # We will exclude the first two cases by checking that we are not inside a
5004 # function body, including one that was just introduced by a trailing '{'.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005005 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005006 if (nesting_state.previous_stack_top and
5007 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
5008 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
5009 # Not at toplevel, not within a class, and not within a namespace
5010 return
5011
avakulenko@google.com59146752014-08-11 20:20:55 +00005012 # Avoid initializer lists. We only need to scan back from the
5013 # current line for something that starts with ':'.
5014 #
5015 # We don't need to check the current line, since the '&' would
5016 # appear inside the second set of parentheses on the current line as
5017 # opposed to the first set.
5018 if linenum > 0:
5019 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5020 previous_line = clean_lines.elided[i]
5021 if not Search(r'[),]\s*$', previous_line):
5022 break
5023 if Match(r'^\s*:\s+\S', previous_line):
5024 return
5025
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005026 # Avoid preprocessors
5027 if Search(r'\\\s*$', line):
5028 return
5029
5030 # Avoid constructor initializer lists
5031 if IsInitializerList(clean_lines, linenum):
5032 return
5033
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005034 # We allow non-const references in a few standard places, like functions
5035 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5036 # those function parameters.
5037 #
5038 # We also accept & in static_assert, which looks like a function but
5039 # it's actually a declaration expression.
5040 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5041 r'operator\s*[<>][<>]|'
5042 r'static_assert|COMPILE_ASSERT'
5043 r')\s*\(')
5044 if Search(whitelisted_functions, line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005045 return
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005046 elif not Search(r'\S+\([^)]*$', line):
5047 # Don't see a whitelisted function on this line. Actually we
5048 # didn't see any function name on this line, so this is likely a
5049 # multi-line parameter list. Try a bit harder to catch this case.
5050 for i in xrange(2):
5051 if (linenum > i and
5052 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005053 return
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005054
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005055 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5056 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
5057 if not Match(_RE_PATTERN_CONST_REF_PARAM, parameter):
5058 error(filename, linenum, 'runtime/references', 2,
5059 'Is this a non-const reference? '
5060 'If so, make const or use a pointer: ' +
5061 ReplaceAll(' *<', '<', parameter))
5062
5063
5064def CheckCasts(filename, clean_lines, linenum, error):
5065 """Various cast related checks.
5066
5067 Args:
5068 filename: The name of the current file.
5069 clean_lines: A CleansedLines instance containing the file.
5070 linenum: The number of the line to check.
5071 error: The function to call with any errors found.
5072 """
5073 line = clean_lines.elided[linenum]
5074
5075 # Check to see if they're using an conversion function cast.
5076 # I just try to capture the most common basic types, though there are more.
5077 # Parameterless conversion functions, such as bool(), are allowed as they are
5078 # probably a member operator declaration or default constructor.
5079 match = Search(
5080 r'(\bnew\s+|\S<\s*(?:const\s+)?)?\b'
5081 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5082 r'(\([^)].*)', line)
5083 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5084 if match and not expecting_function:
5085 matched_type = match.group(2)
5086
5087 # matched_new_or_template is used to silence two false positives:
5088 # - New operators
5089 # - Template arguments with function types
5090 #
5091 # For template arguments, we match on types immediately following
5092 # an opening bracket without any spaces. This is a fast way to
5093 # silence the common case where the function type is the first
5094 # template argument. False negative with less-than comparison is
5095 # avoided because those operators are usually followed by a space.
5096 #
5097 # function<double(double)> // bracket + no space = false positive
5098 # value < double(42) // bracket + space = true positive
5099 matched_new_or_template = match.group(1)
5100
avakulenko@google.com59146752014-08-11 20:20:55 +00005101 # Avoid arrays by looking for brackets that come after the closing
5102 # parenthesis.
5103 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5104 return
5105
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005106 # Other things to ignore:
5107 # - Function pointers
5108 # - Casts to pointer types
5109 # - Placement new
5110 # - Alias declarations
5111 matched_funcptr = match.group(3)
5112 if (matched_new_or_template is None and
5113 not (matched_funcptr and
5114 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5115 matched_funcptr) or
5116 matched_funcptr.startswith('(*)'))) and
5117 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5118 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5119 error(filename, linenum, 'readability/casting', 4,
5120 'Using deprecated casting style. '
5121 'Use static_cast<%s>(...) instead' %
5122 matched_type)
5123
5124 if not expecting_function:
avakulenko@google.com59146752014-08-11 20:20:55 +00005125 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005126 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5127
5128 # This doesn't catch all cases. Consider (const char * const)"hello".
5129 #
5130 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5131 # compile).
avakulenko@google.com59146752014-08-11 20:20:55 +00005132 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5133 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005134 pass
5135 else:
5136 # Check pointer casts for other than string constants
avakulenko@google.com59146752014-08-11 20:20:55 +00005137 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5138 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005139
5140 # In addition, we look for people taking the address of a cast. This
5141 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5142 # point where you think.
avakulenko@google.com59146752014-08-11 20:20:55 +00005143 #
5144 # Some non-identifier character is required before the '&' for the
5145 # expression to be recognized as a cast. These are casts:
5146 # expression = &static_cast<int*>(temporary());
5147 # function(&(int*)(temporary()));
5148 #
5149 # This is not a cast:
5150 # reference_type&(int* function_param);
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005151 match = Search(
avakulenko@google.com59146752014-08-11 20:20:55 +00005152 r'(?:[^\w]&\(([^)]+)\)[\w(])|'
5153 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005154 if match and match.group(1) != '*':
5155 # Try a better error message when the & is bound to something
5156 # dereferenced by the casted pointer, as opposed to the casted
5157 # pointer itself.
5158 parenthesis_error = False
5159 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5160 if match:
5161 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5162 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5163 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5164 if x2 >= 0:
5165 extended_line = clean_lines.elided[y2][x2:]
5166 if y2 < clean_lines.NumLines() - 1:
5167 extended_line += clean_lines.elided[y2 + 1]
5168 if Match(r'\s*(?:->|\[)', extended_line):
5169 parenthesis_error = True
5170
5171 if parenthesis_error:
5172 error(filename, linenum, 'readability/casting', 4,
5173 ('Are you taking an address of something dereferenced '
5174 'from a cast? Wrapping the dereferenced expression in '
5175 'parentheses will make the binding more obvious'))
5176 else:
5177 error(filename, linenum, 'runtime/casting', 4,
5178 ('Are you taking an address of a cast? '
5179 'This is dangerous: could be a temp var. '
5180 'Take the address before doing the cast, rather than after'))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005181
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005182
avakulenko@google.com59146752014-08-11 20:20:55 +00005183def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005184 """Checks for a C-style cast by looking for the pattern.
5185
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005186 Args:
5187 filename: The name of the current file.
avakulenko@google.com59146752014-08-11 20:20:55 +00005188 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005189 linenum: The number of the line to check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005190 cast_type: The string for the C++ cast to recommend. This is either
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005191 reinterpret_cast, static_cast, or const_cast, depending.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005192 pattern: The regular expression used to find C-style casts.
5193 error: The function to call with any errors found.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005194
5195 Returns:
5196 True if an error was emitted.
5197 False otherwise.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005198 """
avakulenko@google.com59146752014-08-11 20:20:55 +00005199 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005200 match = Search(pattern, line)
5201 if not match:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005202 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005203
avakulenko@google.com59146752014-08-11 20:20:55 +00005204 # Exclude lines with keywords that tend to look like casts
5205 context = line[0:match.start(1) - 1]
5206 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5207 return False
5208
5209 # Try expanding current context to see if we one level of
5210 # parentheses inside a macro.
5211 if linenum > 0:
5212 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5213 context = clean_lines.elided[i] + context
5214 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005215 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005216
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005217 # operator++(int) and operator--(int)
avakulenko@google.com59146752014-08-11 20:20:55 +00005218 if context.endswith(' operator++') or context.endswith(' operator--'):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005219 return False
5220
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005221 # A single unnamed argument for a function tends to look like old
5222 # style cast. If we see those, don't issue warnings for deprecated
5223 # casts, instead issue warnings for unnamed arguments where
5224 # appropriate.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005225 #
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005226 # These are things that we want warnings for, since the style guide
5227 # explicitly require all parameters to be named:
5228 # Function(int);
5229 # Function(int) {
5230 # ConstMember(int) const;
5231 # ConstMember(int) const {
5232 # ExceptionMember(int) throw (...);
5233 # ExceptionMember(int) throw (...) {
5234 # PureVirtual(int) = 0;
5235 #
5236 # These are functions of some sort, where the compiler would be fine
5237 # if they had named parameters, but people often omit those
5238 # identifiers to reduce clutter:
5239 # (FunctionPointer)(int);
5240 # (FunctionPointer)(int) = value;
5241 # Function((function_pointer_arg)(int))
avakulenko@google.com59146752014-08-11 20:20:55 +00005242 # Function((function_pointer_arg)(int), int param)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005243 # <TemplateArgument(int)>;
5244 # <(FunctionPointerTemplateArgument)(int)>;
5245 remainder = line[match.end(0):]
avakulenko@google.com59146752014-08-11 20:20:55 +00005246 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),])',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005247 remainder):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005248 # Looks like an unnamed parameter.
5249
5250 # Don't warn on any kind of template arguments.
5251 if Match(r'^\s*>', remainder):
5252 return False
5253
5254 # Don't warn on assignments to function pointers, but keep warnings for
5255 # unnamed parameters to pure virtual functions. Note that this pattern
5256 # will also pass on assignments of "0" to function pointers, but the
5257 # preferred values for those would be "nullptr" or "NULL".
5258 matched_zero = Match(r'^\s=\s*(\S+)\s*;', remainder)
5259 if matched_zero and matched_zero.group(1) != '0':
5260 return False
5261
5262 # Don't warn on function pointer declarations. For this we need
5263 # to check what came before the "(type)" string.
5264 if Match(r'.*\)\s*$', line[0:match.start(0)]):
5265 return False
5266
5267 # Don't warn if the parameter is named with block comments, e.g.:
5268 # Function(int /*unused_param*/);
avakulenko@google.com59146752014-08-11 20:20:55 +00005269 raw_line = clean_lines.raw_lines[linenum]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005270 if '/*' in raw_line:
5271 return False
5272
5273 # Passed all filters, issue warning here.
5274 error(filename, linenum, 'readability/function', 3,
5275 'All parameters should be named in a function')
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005276 return True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005277
5278 # At this point, all that should be left is actual casts.
5279 error(filename, linenum, 'readability/casting', 4,
5280 'Using C-style cast. Use %s<%s>(...) instead' %
5281 (cast_type, match.group(1)))
5282
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005283 return True
5284
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005285
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005286def ExpectingFunctionArgs(clean_lines, linenum):
5287 """Checks whether where function type arguments are expected.
5288
5289 Args:
5290 clean_lines: A CleansedLines instance containing the file.
5291 linenum: The number of the line to check.
5292
5293 Returns:
5294 True if the line at 'linenum' is inside something that expects arguments
5295 of function types.
5296 """
5297 line = clean_lines.elided[linenum]
5298 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5299 (linenum >= 2 and
5300 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5301 clean_lines.elided[linenum - 1]) or
5302 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5303 clean_lines.elided[linenum - 2]) or
5304 Search(r'\bstd::m?function\s*\<\s*$',
5305 clean_lines.elided[linenum - 1]))))
5306
5307
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005308_HEADERS_CONTAINING_TEMPLATES = (
5309 ('<deque>', ('deque',)),
5310 ('<functional>', ('unary_function', 'binary_function',
5311 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5312 'negate',
5313 'equal_to', 'not_equal_to', 'greater', 'less',
5314 'greater_equal', 'less_equal',
5315 'logical_and', 'logical_or', 'logical_not',
5316 'unary_negate', 'not1', 'binary_negate', 'not2',
5317 'bind1st', 'bind2nd',
5318 'pointer_to_unary_function',
5319 'pointer_to_binary_function',
5320 'ptr_fun',
5321 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5322 'mem_fun_ref_t',
5323 'const_mem_fun_t', 'const_mem_fun1_t',
5324 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5325 'mem_fun_ref',
5326 )),
5327 ('<limits>', ('numeric_limits',)),
5328 ('<list>', ('list',)),
5329 ('<map>', ('map', 'multimap',)),
5330 ('<memory>', ('allocator',)),
5331 ('<queue>', ('queue', 'priority_queue',)),
5332 ('<set>', ('set', 'multiset',)),
5333 ('<stack>', ('stack',)),
5334 ('<string>', ('char_traits', 'basic_string',)),
5335 ('<utility>', ('pair',)),
5336 ('<vector>', ('vector',)),
5337
5338 # gcc extensions.
5339 # Note: std::hash is their hash, ::hash is our hash
5340 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5341 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5342 ('<slist>', ('slist',)),
5343 )
5344
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005345_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5346
5347_re_pattern_algorithm_header = []
erg@google.com6317a9c2009-06-25 00:28:19 +00005348for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
5349 'transform'):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005350 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5351 # type::max().
5352 _re_pattern_algorithm_header.append(
5353 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5354 _template,
5355 '<algorithm>'))
5356
5357_re_pattern_templates = []
5358for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5359 for _template in _templates:
5360 _re_pattern_templates.append(
5361 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5362 _template + '<>',
5363 _header))
5364
5365
erg@google.com6317a9c2009-06-25 00:28:19 +00005366def FilesBelongToSameModule(filename_cc, filename_h):
5367 """Check if these two filenames belong to the same module.
5368
5369 The concept of a 'module' here is a as follows:
5370 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5371 same 'module' if they are in the same directory.
5372 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5373 to belong to the same module here.
5374
5375 If the filename_cc contains a longer path than the filename_h, for example,
5376 '/absolute/path/to/base/sysinfo.cc', and this file would include
5377 'base/sysinfo.h', this function also produces the prefix needed to open the
5378 header. This is used by the caller of this function to more robustly open the
5379 header file. We don't have access to the real include paths in this context,
5380 so we need this guesswork here.
5381
5382 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5383 according to this implementation. Because of this, this function gives
5384 some false positives. This should be sufficiently rare in practice.
5385
5386 Args:
5387 filename_cc: is the path for the .cc file
5388 filename_h: is the path for the header path
5389
5390 Returns:
5391 Tuple with a bool and a string:
5392 bool: True if filename_cc and filename_h belong to the same module.
5393 string: the additional prefix needed to open the header file.
5394 """
5395
5396 if not filename_cc.endswith('.cc'):
5397 return (False, '')
5398 filename_cc = filename_cc[:-len('.cc')]
5399 if filename_cc.endswith('_unittest'):
5400 filename_cc = filename_cc[:-len('_unittest')]
5401 elif filename_cc.endswith('_test'):
5402 filename_cc = filename_cc[:-len('_test')]
5403 filename_cc = filename_cc.replace('/public/', '/')
5404 filename_cc = filename_cc.replace('/internal/', '/')
5405
5406 if not filename_h.endswith('.h'):
5407 return (False, '')
5408 filename_h = filename_h[:-len('.h')]
5409 if filename_h.endswith('-inl'):
5410 filename_h = filename_h[:-len('-inl')]
5411 filename_h = filename_h.replace('/public/', '/')
5412 filename_h = filename_h.replace('/internal/', '/')
5413
5414 files_belong_to_same_module = filename_cc.endswith(filename_h)
5415 common_path = ''
5416 if files_belong_to_same_module:
5417 common_path = filename_cc[:-len(filename_h)]
5418 return files_belong_to_same_module, common_path
5419
5420
avakulenko@google.com59146752014-08-11 20:20:55 +00005421def UpdateIncludeState(filename, include_dict, io=codecs):
5422 """Fill up the include_dict with new includes found from the file.
erg@google.com6317a9c2009-06-25 00:28:19 +00005423
5424 Args:
5425 filename: the name of the header to read.
avakulenko@google.com59146752014-08-11 20:20:55 +00005426 include_dict: a dictionary in which the headers are inserted.
erg@google.com6317a9c2009-06-25 00:28:19 +00005427 io: The io factory to use to read the file. Provided for testability.
5428
5429 Returns:
avakulenko@google.com59146752014-08-11 20:20:55 +00005430 True if a header was successfully added. False otherwise.
erg@google.com6317a9c2009-06-25 00:28:19 +00005431 """
5432 headerfile = None
5433 try:
5434 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5435 except IOError:
5436 return False
5437 linenum = 0
5438 for line in headerfile:
5439 linenum += 1
5440 clean_line = CleanseComments(line)
5441 match = _RE_PATTERN_INCLUDE.search(clean_line)
5442 if match:
5443 include = match.group(2)
avakulenko@google.com59146752014-08-11 20:20:55 +00005444 include_dict.setdefault(include, linenum)
erg@google.com6317a9c2009-06-25 00:28:19 +00005445 return True
5446
5447
5448def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5449 io=codecs):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005450 """Reports for missing stl includes.
5451
5452 This function will output warnings to make sure you are including the headers
5453 necessary for the stl containers and functions that you use. We only give one
5454 reason to include a header. For example, if you use both equal_to<> and
5455 less<> in a .h file, only one (the latter in the file) of these will be
5456 reported as a reason to include the <functional>.
5457
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005458 Args:
5459 filename: The name of the current file.
5460 clean_lines: A CleansedLines instance containing the file.
5461 include_state: An _IncludeState instance.
5462 error: The function to call with any errors found.
erg@google.com6317a9c2009-06-25 00:28:19 +00005463 io: The IO factory to use to read the header file. Provided for unittest
5464 injection.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005465 """
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005466 required = {} # A map of header name to linenumber and the template entity.
5467 # Example of required: { '<functional>': (1219, 'less<>') }
5468
5469 for linenum in xrange(clean_lines.NumLines()):
5470 line = clean_lines.elided[linenum]
5471 if not line or line[0] == '#':
5472 continue
5473
5474 # String is special -- it is a non-templatized type in STL.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005475 matched = _RE_PATTERN_STRING.search(line)
5476 if matched:
erg@google.com35589e62010-11-17 18:58:16 +00005477 # Don't warn about strings in non-STL namespaces:
5478 # (We check only the first match per line; good enough.)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005479 prefix = line[:matched.start()]
erg@google.com35589e62010-11-17 18:58:16 +00005480 if prefix.endswith('std::') or not prefix.endswith('::'):
5481 required['<string>'] = (linenum, 'string')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005482
5483 for pattern, template, header in _re_pattern_algorithm_header:
5484 if pattern.search(line):
5485 required[header] = (linenum, template)
5486
5487 # The following function is just a speed up, no semantics are changed.
5488 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5489 continue
5490
5491 for pattern, template, header in _re_pattern_templates:
5492 if pattern.search(line):
5493 required[header] = (linenum, template)
5494
erg@google.com6317a9c2009-06-25 00:28:19 +00005495 # The policy is that if you #include something in foo.h you don't need to
5496 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.com59146752014-08-11 20:20:55 +00005497 # Let's flatten the include_state include_list and copy it into a dictionary.
5498 include_dict = dict([item for sublist in include_state.include_list
5499 for item in sublist])
erg@google.com6317a9c2009-06-25 00:28:19 +00005500
avakulenko@google.com59146752014-08-11 20:20:55 +00005501 # Did we find the header for this file (if any) and successfully load it?
erg@google.com6317a9c2009-06-25 00:28:19 +00005502 header_found = False
5503
5504 # Use the absolute path so that matching works properly.
erg@chromium.org8f927562012-01-30 19:51:28 +00005505 abs_filename = FileInfo(filename).FullName()
erg@google.com6317a9c2009-06-25 00:28:19 +00005506
5507 # For Emacs's flymake.
5508 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5509 # by flymake and that file name might end with '_flymake.cc'. In that case,
5510 # restore original file name here so that the corresponding header file can be
5511 # found.
5512 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5513 # instead of 'foo_flymake.h'
erg@google.com35589e62010-11-17 18:58:16 +00005514 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.com6317a9c2009-06-25 00:28:19 +00005515
avakulenko@google.com59146752014-08-11 20:20:55 +00005516 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.com6317a9c2009-06-25 00:28:19 +00005517 # the keys.
avakulenko@google.com59146752014-08-11 20:20:55 +00005518 header_keys = include_dict.keys()
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005519 for header in header_keys:
erg@google.com6317a9c2009-06-25 00:28:19 +00005520 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5521 fullpath = common_path + header
avakulenko@google.com59146752014-08-11 20:20:55 +00005522 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.com6317a9c2009-06-25 00:28:19 +00005523 header_found = True
5524
5525 # If we can't find the header file for a .cc, assume it's because we don't
5526 # know where to look. In that case we'll give up as we're not sure they
5527 # didn't include it in the .h file.
5528 # TODO(unknown): Do a better job of finding .h files so we are confident that
5529 # not having the .h file means there isn't one.
5530 if filename.endswith('.cc') and not header_found:
5531 return
5532
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005533 # All the lines have been processed, report the errors found.
5534 for required_header_unstripped in required:
5535 template = required[required_header_unstripped][1]
avakulenko@google.com59146752014-08-11 20:20:55 +00005536 if required_header_unstripped.strip('<>"') not in include_dict:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005537 error(filename, required[required_header_unstripped][0],
5538 'build/include_what_you_use', 4,
5539 'Add #include ' + required_header_unstripped + ' for ' + template)
5540
5541
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005542_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5543
5544
5545def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5546 """Check that make_pair's template arguments are deduced.
5547
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005548 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005549 specified explicitly, and such use isn't intended in any case.
5550
5551 Args:
5552 filename: The name of the current file.
5553 clean_lines: A CleansedLines instance containing the file.
5554 linenum: The number of the line to check.
5555 error: The function to call with any errors found.
5556 """
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005557 line = clean_lines.elided[linenum]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005558 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5559 if match:
5560 error(filename, linenum, 'build/explicit_make_pair',
5561 4, # 4 = high confidence
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005562 'For C++11-compatibility, omit template arguments from make_pair'
5563 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.com59146752014-08-11 20:20:55 +00005564
5565
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005566def CheckDefaultLambdaCaptures(filename, clean_lines, linenum, error):
5567 """Check that default lambda captures are not used.
5568
5569 Args:
5570 filename: The name of the current file.
5571 clean_lines: A CleansedLines instance containing the file.
5572 linenum: The number of the line to check.
5573 error: The function to call with any errors found.
5574 """
5575 line = clean_lines.elided[linenum]
5576
5577 # A lambda introducer specifies a default capture if it starts with "[="
5578 # or if it starts with "[&" _not_ followed by an identifier.
5579 match = Match(r'^(.*)\[\s*(?:=|&[^\w])', line)
5580 if match:
5581 # Found a potential error, check what comes after the lambda-introducer.
5582 # If it's not open parenthesis (for lambda-declarator) or open brace
5583 # (for compound-statement), it's not a lambda.
5584 line, _, pos = CloseExpression(clean_lines, linenum, len(match.group(1)))
5585 if pos >= 0 and Match(r'^\s*[{(]', line[pos:]):
5586 error(filename, linenum, 'build/c++11',
5587 4, # 4 = high confidence
5588 'Default lambda captures are an unapproved C++ feature.')
5589
5590
avakulenko@google.com59146752014-08-11 20:20:55 +00005591def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5592 """Check if line contains a redundant "virtual" function-specifier.
5593
5594 Args:
5595 filename: The name of the current file.
5596 clean_lines: A CleansedLines instance containing the file.
5597 linenum: The number of the line to check.
5598 error: The function to call with any errors found.
5599 """
5600 # Look for "virtual" on current line.
5601 line = clean_lines.elided[linenum]
5602 virtual = Match(r'^(.*\bvirtual\b)', line)
5603 if not virtual: return
5604
5605 # Look for the next opening parenthesis. This is the start of the
5606 # parameter list (possibly on the next line shortly after virtual).
5607 # TODO(unknown): doesn't work if there are virtual functions with
5608 # decltype() or other things that use parentheses, but csearch suggests
5609 # that this is rare.
5610 end_col = -1
5611 end_line = -1
5612 start_col = len(virtual.group(1))
5613 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5614 line = clean_lines.elided[start_line][start_col:]
5615 parameter_list = Match(r'^([^(]*)\(', line)
5616 if parameter_list:
5617 # Match parentheses to find the end of the parameter list
5618 (_, end_line, end_col) = CloseExpression(
5619 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5620 break
5621 start_col = 0
5622
5623 if end_col < 0:
5624 return # Couldn't find end of parameter list, give up
5625
5626 # Look for "override" or "final" after the parameter list
5627 # (possibly on the next few lines).
5628 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5629 line = clean_lines.elided[i][end_col:]
5630 match = Search(r'\b(override|final)\b', line)
5631 if match:
5632 error(filename, linenum, 'readability/inheritance', 4,
5633 ('"virtual" is redundant since function is '
5634 'already declared as "%s"' % match.group(1)))
5635
5636 # Set end_col to check whole lines after we are done with the
5637 # first line.
5638 end_col = 0
5639 if Search(r'[^\w]\s*$', line):
5640 break
5641
5642
5643def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5644 """Check if line contains a redundant "override" or "final" virt-specifier.
5645
5646 Args:
5647 filename: The name of the current file.
5648 clean_lines: A CleansedLines instance containing the file.
5649 linenum: The number of the line to check.
5650 error: The function to call with any errors found.
5651 """
5652 # Check that at most one of "override" or "final" is present, not both
5653 line = clean_lines.elided[linenum]
5654 if Search(r'\boverride\b', line) and Search(r'\bfinal\b', line):
5655 error(filename, linenum, 'readability/inheritance', 4,
5656 ('"override" is redundant since function is '
5657 'already declared as "final"'))
5658
5659
5660
5661
5662# Returns true if we are at a new block, and it is directly
5663# inside of a namespace.
5664def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5665 """Checks that the new block is directly in a namespace.
5666
5667 Args:
5668 nesting_state: The _NestingState object that contains info about our state.
5669 is_forward_declaration: If the class is a forward declared class.
5670 Returns:
5671 Whether or not the new block is directly in a namespace.
5672 """
5673 if is_forward_declaration:
5674 if len(nesting_state.stack) >= 1 and (
5675 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5676 return True
5677 else:
5678 return False
5679
5680 return (len(nesting_state.stack) > 1 and
5681 nesting_state.stack[-1].check_namespace_indentation and
5682 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5683
5684
5685def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5686 raw_lines_no_comments, linenum):
5687 """This method determines if we should apply our namespace indentation check.
5688
5689 Args:
5690 nesting_state: The current nesting state.
5691 is_namespace_indent_item: If we just put a new class on the stack, True.
5692 If the top of the stack is not a class, or we did not recently
5693 add the class, False.
5694 raw_lines_no_comments: The lines without the comments.
5695 linenum: The current line number we are processing.
5696
5697 Returns:
5698 True if we should apply our namespace indentation check. Currently, it
5699 only works for classes and namespaces inside of a namespace.
5700 """
5701
5702 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5703 linenum)
5704
5705 if not (is_namespace_indent_item or is_forward_declaration):
5706 return False
5707
5708 # If we are in a macro, we do not want to check the namespace indentation.
5709 if IsMacroDefinition(raw_lines_no_comments, linenum):
5710 return False
5711
5712 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5713
5714
5715# Call this method if the line is directly inside of a namespace.
5716# If the line above is blank (excluding comments) or the start of
5717# an inner namespace, it cannot be indented.
5718def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5719 error):
5720 line = raw_lines_no_comments[linenum]
5721 if Match(r'^\s+', line):
5722 error(filename, linenum, 'runtime/indentation_namespace', 4,
5723 'Do not indent within a namespace')
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005724
5725
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005726def ProcessLine(filename, file_extension, clean_lines, line,
5727 include_state, function_state, nesting_state, error,
5728 extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005729 """Processes a single line in the file.
5730
5731 Args:
5732 filename: Filename of the file that is being processed.
5733 file_extension: The extension (dot not included) of the file.
5734 clean_lines: An array of strings, each representing a line of the file,
5735 with comments stripped.
5736 line: Number of line being processed.
5737 include_state: An _IncludeState instance in which the headers are inserted.
5738 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005739 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005740 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005741 error: A callable to which errors are reported, which takes 4 arguments:
5742 filename, line number, error level, and message
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005743 extra_check_functions: An array of additional check functions that will be
5744 run on each source line. Each function takes 4
5745 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005746 """
5747 raw_lines = clean_lines.raw_lines
erg@google.com35589e62010-11-17 18:58:16 +00005748 ParseNolintSuppressions(filename, raw_lines[line], line, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005749 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.com59146752014-08-11 20:20:55 +00005750 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5751 error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005752 if nesting_state.InAsmBlock(): return
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005753 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005754 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005755 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005756 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005757 nesting_state, error)
5758 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005759 CheckForNonStandardConstructs(filename, clean_lines, line,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005760 nesting_state, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005761 CheckVlogArguments(filename, clean_lines, line, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005762 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com6317a9c2009-06-25 00:28:19 +00005763 CheckInvalidIncrement(filename, clean_lines, line, error)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005764 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005765 CheckDefaultLambdaCaptures(filename, clean_lines, line, error)
avakulenko@google.com59146752014-08-11 20:20:55 +00005766 CheckRedundantVirtual(filename, clean_lines, line, error)
5767 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005768 for check_fn in extra_check_functions:
5769 check_fn(filename, clean_lines, line, error)
avakulenko@google.com17449932014-07-28 22:13:33 +00005770
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005771def FlagCxx11Features(filename, clean_lines, linenum, error):
5772 """Flag those c++11 features that we only allow in certain places.
5773
5774 Args:
5775 filename: The name of the current file.
5776 clean_lines: A CleansedLines instance containing the file.
5777 linenum: The number of the line to check.
5778 error: The function to call with any errors found.
5779 """
5780 line = clean_lines.elided[linenum]
5781
5782 # Flag unapproved C++11 headers.
5783 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5784 if include and include.group(1) in ('cfenv',
5785 'condition_variable',
5786 'fenv.h',
5787 'future',
5788 'mutex',
5789 'thread',
5790 'chrono',
5791 'ratio',
5792 'regex',
5793 'system_error',
5794 ):
5795 error(filename, linenum, 'build/c++11', 5,
5796 ('<%s> is an unapproved C++11 header.') % include.group(1))
5797
5798 # The only place where we need to worry about C++11 keywords and library
5799 # features in preprocessor directives is in macro definitions.
5800 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5801
5802 # These are classes and free functions. The classes are always
5803 # mentioned as std::*, but we only catch the free functions if
5804 # they're not found by ADL. They're alphabetical by header.
5805 for top_name in (
5806 # type_traits
5807 'alignment_of',
5808 'aligned_union',
5809
5810 # utility
5811 'forward',
5812 ):
5813 if Search(r'\bstd::%s\b' % top_name, line):
5814 error(filename, linenum, 'build/c++11', 5,
5815 ('std::%s is an unapproved C++11 class or function. Send c-style '
5816 'an example of where it would make your code more readable, and '
5817 'they may let you use it.') % top_name)
5818
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005819
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005820def ProcessFileData(filename, file_extension, lines, error,
5821 extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005822 """Performs lint checks and reports any errors to the given error function.
5823
5824 Args:
5825 filename: Filename of the file that is being processed.
5826 file_extension: The extension (dot not included) of the file.
5827 lines: An array of strings, each representing a line of the file, with the
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005828 last element being empty if the file is terminated with a newline.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005829 error: A callable to which errors are reported, which takes 4 arguments:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005830 filename, line number, error level, and message
5831 extra_check_functions: An array of additional check functions that will be
5832 run on each source line. Each function takes 4
5833 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005834 """
5835 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5836 ['// marker so line numbers end in a known way'])
5837
5838 include_state = _IncludeState()
5839 function_state = _FunctionState()
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005840 nesting_state = NestingState()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005841
erg@google.com35589e62010-11-17 18:58:16 +00005842 ResetNolintSuppressions()
5843
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005844 CheckForCopyright(filename, lines, error)
5845
5846 if file_extension == 'h':
5847 CheckForHeaderGuard(filename, lines, error)
5848
5849 RemoveMultiLineComments(filename, lines, error)
5850 clean_lines = CleansedLines(lines)
5851 for line in xrange(clean_lines.NumLines()):
5852 ProcessLine(filename, file_extension, clean_lines, line,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005853 include_state, function_state, nesting_state, error,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005854 extra_check_functions)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005855 FlagCxx11Features(filename, clean_lines, line, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005856 nesting_state.CheckCompletedBlocks(filename, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005857
5858 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
5859
5860 # We check here rather than inside ProcessLine so that we see raw
5861 # lines rather than "cleaned" lines.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005862 CheckForBadCharacters(filename, lines, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005863
5864 CheckForNewlineAtEOF(filename, lines, error)
5865
avakulenko@google.com17449932014-07-28 22:13:33 +00005866def ProcessConfigOverrides(filename):
5867 """ Loads the configuration files and processes the config overrides.
5868
5869 Args:
5870 filename: The name of the file being processed by the linter.
5871
5872 Returns:
5873 False if the current |filename| should not be processed further.
5874 """
5875
5876 abs_filename = os.path.abspath(filename)
5877 cfg_filters = []
5878 keep_looking = True
5879 while keep_looking:
5880 abs_path, base_name = os.path.split(abs_filename)
5881 if not base_name:
5882 break # Reached the root directory.
5883
5884 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5885 abs_filename = abs_path
5886 if not os.path.isfile(cfg_file):
5887 continue
5888
5889 try:
5890 with open(cfg_file) as file_handle:
5891 for line in file_handle:
5892 line, _, _ = line.partition('#') # Remove comments.
5893 if not line.strip():
5894 continue
5895
5896 name, _, val = line.partition('=')
5897 name = name.strip()
5898 val = val.strip()
5899 if name == 'set noparent':
5900 keep_looking = False
5901 elif name == 'filter':
5902 cfg_filters.append(val)
5903 elif name == 'exclude_files':
5904 # When matching exclude_files pattern, use the base_name of
5905 # the current file name or the directory name we are processing.
5906 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5907 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5908 # file's "exclude_files" filter is meant to be checked against "bar"
5909 # and not "baz" nor "bar/baz.cc".
5910 if base_name:
5911 pattern = re.compile(val)
5912 if pattern.match(base_name):
5913 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5914 'File path component "%s" matches '
5915 'pattern "%s"\n' %
5916 (filename, cfg_file, base_name, val))
5917 return False
5918 else:
5919 sys.stderr.write(
5920 'Invalid configuration option (%s) in file %s\n' %
5921 (name, cfg_file))
5922
5923 except IOError:
5924 sys.stderr.write(
5925 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5926 keep_looking = False
5927
5928 # Apply all the accumulated filters in reverse order (top-level directory
5929 # config options having the least priority).
5930 for filter in reversed(cfg_filters):
5931 _AddFilters(filter)
5932
5933 return True
5934
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005935
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005936def ProcessFile(filename, vlevel, extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005937 """Does google-lint on a single file.
5938
5939 Args:
5940 filename: The name of the file to parse.
5941
5942 vlevel: The level of errors to report. Every error of confidence
5943 >= verbose_level will be reported. 0 is a good default.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005944
5945 extra_check_functions: An array of additional check functions that will be
5946 run on each source line. Each function takes 4
5947 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005948 """
5949
5950 _SetVerboseLevel(vlevel)
avakulenko@google.com17449932014-07-28 22:13:33 +00005951 _BackupFilters()
5952
5953 if not ProcessConfigOverrides(filename):
5954 _RestoreFilters()
5955 return
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005956
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005957 lf_lines = []
5958 crlf_lines = []
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005959 try:
5960 # Support the UNIX convention of using "-" for stdin. Note that
5961 # we are not opening the file with universal newline support
5962 # (which codecs doesn't support anyway), so the resulting lines do
5963 # contain trailing '\r' characters if we are reading a file that
5964 # has CRLF endings.
5965 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005966 # below.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005967 if filename == '-':
5968 lines = codecs.StreamReaderWriter(sys.stdin,
5969 codecs.getreader('utf8'),
5970 codecs.getwriter('utf8'),
5971 'replace').read().split('\n')
5972 else:
5973 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
5974
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005975 # Remove trailing '\r'.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005976 # The -1 accounts for the extra trailing blank line we get from split()
5977 for linenum in range(len(lines) - 1):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005978 if lines[linenum].endswith('\r'):
5979 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005980 crlf_lines.append(linenum + 1)
5981 else:
5982 lf_lines.append(linenum + 1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005983
5984 except IOError:
5985 sys.stderr.write(
5986 "Skipping input '%s': Can't open for reading\n" % filename)
avakulenko@google.com17449932014-07-28 22:13:33 +00005987 _RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005988 return
5989
5990 # Note, if no dot is found, this will give the entire filename as the ext.
5991 file_extension = filename[filename.rfind('.') + 1:]
5992
5993 # When reading from stdin, the extension is unknown, so no cpplint tests
5994 # should rely on the extension.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005995 if filename != '-' and file_extension not in _valid_extensions:
5996 sys.stderr.write('Ignoring %s; not a valid file name '
5997 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005998 else:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005999 ProcessFileData(filename, file_extension, lines, Error,
6000 extra_check_functions)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006001
6002 # If end-of-line sequences are a mix of LF and CR-LF, issue
6003 # warnings on the lines with CR.
6004 #
6005 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
6006 # since critique can handle these just fine, and the style guide
6007 # doesn't dictate a particular end of line sequence.
6008 #
6009 # We can't depend on os.linesep to determine what the desired
6010 # end-of-line sequence should be, since that will return the
6011 # server-side end-of-line sequence.
6012 if lf_lines and crlf_lines:
6013 # Warn on every line with CR. An alternative approach might be to
6014 # check whether the file is mostly CRLF or just LF, and warn on the
6015 # minority, we bias toward LF here since most tools prefer LF.
6016 for linenum in crlf_lines:
6017 Error(filename, linenum, 'whitespace/newline', 1,
6018 'Unexpected \\r (^M) found; better to use only \\n')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006019
6020 sys.stderr.write('Done processing %s\n' % filename)
avakulenko@google.com17449932014-07-28 22:13:33 +00006021 _RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006022
6023
6024def PrintUsage(message):
6025 """Prints a brief usage string and exits, optionally with an error message.
6026
6027 Args:
6028 message: The optional error message.
6029 """
6030 sys.stderr.write(_USAGE)
6031 if message:
6032 sys.exit('\nFATAL ERROR: ' + message)
6033 else:
6034 sys.exit(1)
6035
6036
6037def PrintCategories():
6038 """Prints a list of all the error-categories used by error messages.
6039
6040 These are the categories used to filter messages via --filter.
6041 """
erg@google.com35589e62010-11-17 18:58:16 +00006042 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006043 sys.exit(0)
6044
6045
6046def ParseArguments(args):
6047 """Parses the command line arguments.
6048
6049 This may set the output format and verbosity level as side-effects.
6050
6051 Args:
6052 args: The command line arguments:
6053
6054 Returns:
6055 The list of filenames to lint.
6056 """
6057 try:
6058 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.com26970fa2009-11-17 18:07:32 +00006059 'counting=',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006060 'filter=',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006061 'root=',
6062 'linelength=',
6063 'extensions='])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006064 except getopt.GetoptError:
6065 PrintUsage('Invalid arguments.')
6066
6067 verbosity = _VerboseLevel()
6068 output_format = _OutputFormat()
6069 filters = ''
erg@google.com26970fa2009-11-17 18:07:32 +00006070 counting_style = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006071
6072 for (opt, val) in opts:
6073 if opt == '--help':
6074 PrintUsage(None)
6075 elif opt == '--output':
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006076 if val not in ('emacs', 'vs7', 'eclipse'):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006077 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006078 output_format = val
6079 elif opt == '--verbose':
6080 verbosity = int(val)
6081 elif opt == '--filter':
6082 filters = val
erg@google.com6317a9c2009-06-25 00:28:19 +00006083 if not filters:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006084 PrintCategories()
erg@google.com26970fa2009-11-17 18:07:32 +00006085 elif opt == '--counting':
6086 if val not in ('total', 'toplevel', 'detailed'):
6087 PrintUsage('Valid counting options are total, toplevel, and detailed')
6088 counting_style = val
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006089 elif opt == '--root':
6090 global _root
6091 _root = val
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006092 elif opt == '--linelength':
6093 global _line_length
6094 try:
6095 _line_length = int(val)
6096 except ValueError:
6097 PrintUsage('Line length must be digits.')
6098 elif opt == '--extensions':
6099 global _valid_extensions
6100 try:
6101 _valid_extensions = set(val.split(','))
6102 except ValueError:
6103 PrintUsage('Extensions must be comma seperated list.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006104
6105 if not filenames:
6106 PrintUsage('No files were specified.')
6107
6108 _SetOutputFormat(output_format)
6109 _SetVerboseLevel(verbosity)
6110 _SetFilters(filters)
erg@google.com26970fa2009-11-17 18:07:32 +00006111 _SetCountingStyle(counting_style)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006112
6113 return filenames
6114
6115
6116def main():
6117 filenames = ParseArguments(sys.argv[1:])
6118
6119 # Change stderr to write with replacement characters so we don't die
6120 # if we try to print something containing non-ASCII characters.
6121 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6122 codecs.getreader('utf8'),
6123 codecs.getwriter('utf8'),
6124 'replace')
6125
erg@google.com26970fa2009-11-17 18:07:32 +00006126 _cpplint_state.ResetErrorCounts()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006127 for filename in filenames:
6128 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.com26970fa2009-11-17 18:07:32 +00006129 _cpplint_state.PrintErrorCounts()
6130
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006131 sys.exit(_cpplint_state.error_count > 0)
6132
6133
6134if __name__ == '__main__':
6135 main()