blob: f919562d2d49d73df39ecfb01c3dd8729e10b7ed [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
agablef39c3332016-09-26 09:35:42 -070031# pylint: skip-file
32
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000033"""Does google-lint on c++ files.
34
35The goal of this script is to identify places in the code that *may*
36be in non-compliance with google style. It does not attempt to fix
37up these problems -- the point is to educate. It does also not
38attempt to find all problems, or to ensure that everything it does
39find is legitimately a problem.
40
41In particular, we can get very confused by /* and // inside strings!
42We do a small hack, which is to ignore //'s with "'s after them on the
43same line, but it is far from perfect (in either direction).
44"""
45
46import codecs
mazda@chromium.org3fffcec2013-06-07 01:04:53 +000047import copy
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000048import getopt
49import math # for log
50import os
51import re
52import sre_compile
53import string
54import sys
55import unicodedata
Amin Hassanif7e1e102018-06-21 20:13:30 +000056import sysconfig
57
58try:
59 xrange # Python 2
60except NameError:
61 xrange = range # Python 3
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000062
63
64_USAGE = """
65Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +000066 [--counting=total|toplevel|detailed] [--root=subdir]
Amin Hassanif7e1e102018-06-21 20:13:30 +000067 [--linelength=digits] [--headers=x,y,...]
68 [--quiet]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000069 <file> [file] ...
70
71 The style guidelines this tries to follow are those in
Alexandr Ilinff294c32017-04-27 15:57:40 +020072 https://google.github.io/styleguide/cppguide.html
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000073
74 Every problem is given a confidence score from 1-5, with 5 meaning we are
75 certain of the problem, and 1 meaning it could be a legitimate construct.
76 This will miss some errors, and is not a substitute for a code review.
77
erg@google.com35589e62010-11-17 18:58:16 +000078 To suppress false-positive errors of a certain category, add a
79 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
80 suppresses errors of all categories on that line.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000081
82 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 +000083 Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the
84 extensions with the --extensions flag.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000085
86 Flags:
87
88 output=vs7
89 By default, the output is formatted to ease emacs parsing. Visual Studio
90 compatible output (vs7) may also be used. Other formats are unsupported.
91
92 verbose=#
93 Specify a number 0-5 to restrict errors to certain verbosity levels.
94
Amin Hassanif7e1e102018-06-21 20:13:30 +000095 quiet
96 Don't print anything if no errors are found.
97
maruel@google.comfb2b8eb2009-04-23 21:03:42 +000098 filter=-x,+y,...
99 Specify a comma-separated list of category-filters to apply: only
100 error messages whose category names pass the filters will be printed.
101 (Category names are printed with the message and look like
102 "[whitespace/indent]".) Filters are evaluated left to right.
103 "-FOO" and "FOO" means "do not print categories that start with FOO".
104 "+FOO" means "do print categories that start with FOO".
105
106 Examples: --filter=-whitespace,+whitespace/braces
107 --filter=whitespace,runtime/printf,+runtime/printf_format
108 --filter=-,+build/include_what_you_use
109
110 To see a list of all the categories used in cpplint, pass no arg:
111 --filter=
erg@google.com26970fa2009-11-17 18:07:32 +0000112
113 counting=total|toplevel|detailed
114 The total number of errors found is always printed. If
115 'toplevel' is provided, then the count of errors in each of
116 the top-level categories like 'build' and 'whitespace' will
117 also be printed. If 'detailed' is provided, then a count
118 is provided for each category like 'build/class'.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000119
120 root=subdir
121 The root directory used for deriving header guard CPP variable.
122 By default, the header guard CPP variable is calculated as the relative
123 path to the directory that contains .git, .hg, or .svn. When this flag
124 is specified, the relative path is calculated from the specified
125 directory. If the specified directory does not exist, this flag is
126 ignored.
127
128 Examples:
Amin Hassanif7e1e102018-06-21 20:13:30 +0000129 Assuming that top/src/.git exists (and cwd=top/src), the header guard
130 CPP variables for top/src/chrome/browser/ui/browser.h are:
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000131
132 No flag => CHROME_BROWSER_UI_BROWSER_H_
133 --root=chrome => BROWSER_UI_BROWSER_H_
134 --root=chrome/browser => UI_BROWSER_H_
Amin Hassanif7e1e102018-06-21 20:13:30 +0000135 --root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000136
137 linelength=digits
138 This is the allowed line length for the project. The default value is
139 80 characters.
140
141 Examples:
142 --linelength=120
143
144 extensions=extension,extension,...
145 The allowed file extensions that cpplint will check
146
147 Examples:
148 --extensions=hpp,cpp
avakulenko@google.com17449932014-07-28 22:13:33 +0000149
Amin Hassanif7e1e102018-06-21 20:13:30 +0000150 headers=x,y,...
151 The header extensions that cpplint will treat as .h in checks. Values are
152 automatically added to --extensions list.
153
154 Examples:
155 --headers=hpp,hxx
156 --headers=hpp
157
avakulenko@google.com17449932014-07-28 22:13:33 +0000158 cpplint.py supports per-directory configurations specified in CPPLINT.cfg
159 files. CPPLINT.cfg file can contain a number of key=value pairs.
160 Currently the following options are supported:
161
162 set noparent
163 filter=+filter1,-filter2,...
164 exclude_files=regex
avakulenko@google.com68a4fa62014-08-25 16:26:18 +0000165 linelength=80
Amin Hassanif7e1e102018-06-21 20:13:30 +0000166 root=subdir
167 headers=x,y,...
avakulenko@google.com17449932014-07-28 22:13:33 +0000168
169 "set noparent" option prevents cpplint from traversing directory tree
170 upwards looking for more .cfg files in parent directories. This option
171 is usually placed in the top-level project directory.
172
173 The "filter" option is similar in function to --filter flag. It specifies
174 message filters in addition to the |_DEFAULT_FILTERS| and those specified
175 through --filter command-line flag.
176
177 "exclude_files" allows to specify a regular expression to be matched against
178 a file name. If the expression matches, the file is skipped and not run
179 through liner.
180
avakulenko@google.com68a4fa62014-08-25 16:26:18 +0000181 "linelength" allows to specify the allowed line length for the project.
182
Amin Hassanif7e1e102018-06-21 20:13:30 +0000183 The "root" option is similar in function to the --root flag (see example
184 above). Paths are relative to the directory of the CPPLINT.cfg.
185
186 The "headers" option is similar in function to the --headers flag
187 (see example above).
188
avakulenko@google.com17449932014-07-28 22:13:33 +0000189 CPPLINT.cfg has an effect on files in the same directory and all
190 sub-directories, unless overridden by a nested configuration file.
191
192 Example file:
193 filter=-build/include_order,+build/include_alpha
194 exclude_files=.*\.cc
195
196 The above example disables build/include_order warning and enables
197 build/include_alpha as well as excludes all .cc from being
198 processed by linter, in the current directory (where the .cfg
199 file is located) and all sub-directories.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000200"""
201
202# We categorize each error message we print. Here are the categories.
203# We want an explicit list so we can list them all in cpplint --filter=.
204# If you add a new error message with a new category, add it to the list
205# here! cpplint_unittest.py should tell you if you forget to do this.
erg@google.com35589e62010-11-17 18:58:16 +0000206_ERROR_CATEGORIES = [
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000207 'build/class',
208 'build/c++11',
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000209 'build/c++14',
210 'build/c++tr1',
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000211 'build/deprecated',
212 'build/endif_comment',
213 'build/explicit_make_pair',
214 'build/forward_decl',
215 'build/header_guard',
216 'build/include',
217 'build/include_alpha',
218 'build/include_order',
219 'build/include_what_you_use',
220 'build/namespaces',
221 'build/printf_format',
222 'build/storage_class',
223 'legal/copyright',
224 'readability/alt_tokens',
225 'readability/braces',
226 'readability/casting',
227 'readability/check',
228 'readability/constructors',
229 'readability/fn_size',
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000230 'readability/inheritance',
231 'readability/multiline_comment',
232 'readability/multiline_string',
233 'readability/namespace',
234 'readability/nolint',
235 'readability/nul',
236 'readability/strings',
237 'readability/todo',
238 'readability/utf8',
239 'runtime/arrays',
240 'runtime/casting',
241 'runtime/explicit',
242 'runtime/int',
243 'runtime/init',
244 'runtime/invalid_increment',
245 'runtime/member_string_references',
246 'runtime/memset',
247 'runtime/indentation_namespace',
248 'runtime/operator',
249 'runtime/printf',
250 'runtime/printf_format',
251 'runtime/references',
252 'runtime/string',
253 'runtime/threadsafe_fn',
254 'runtime/vlog',
255 'whitespace/blank_line',
256 'whitespace/braces',
257 'whitespace/comma',
258 'whitespace/comments',
259 'whitespace/empty_conditional_body',
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000260 'whitespace/empty_if_body',
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000261 'whitespace/empty_loop_body',
262 'whitespace/end_of_line',
263 'whitespace/ending_newline',
264 'whitespace/forcolon',
265 'whitespace/indent',
266 'whitespace/line_length',
267 'whitespace/newline',
268 'whitespace/operators',
269 'whitespace/parens',
270 'whitespace/semicolon',
271 'whitespace/tab',
272 'whitespace/todo',
273 ]
274
275# These error categories are no longer enforced by cpplint, but for backwards-
276# compatibility they may still appear in NOLINT comments.
277_LEGACY_ERROR_CATEGORIES = [
278 'readability/streams',
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000279 'readability/function',
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000280 ]
erg@google.com6317a9c2009-06-25 00:28:19 +0000281
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000282# The default state of the category filter. This is overridden by the --filter=
erg@google.com6317a9c2009-06-25 00:28:19 +0000283# flag. By default all errors are on, so only add here categories that should be
284# off by default (i.e., categories that must be enabled by the --filter= flags).
285# All entries here should start with a '-' or '+', as in the --filter= flag.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000286_DEFAULT_FILTERS = ['-build/include_alpha']
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000287
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000288# The default list of categories suppressed for C (not C++) files.
289_DEFAULT_C_SUPPRESSED_CATEGORIES = [
290 'readability/casting',
291 ]
292
293# The default list of categories suppressed for Linux Kernel files.
294_DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [
295 'whitespace/tab',
296 ]
297
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000298# We used to check for high-bit characters, but after much discussion we
299# decided those were OK, as long as they were in UTF-8 and didn't represent
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000300# hard-coded international strings, which belong in a separate i18n file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000301
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000302# C++ headers
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000303_CPP_HEADERS = frozenset([
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000304 # Legacy
305 'algobase.h',
306 'algo.h',
307 'alloc.h',
308 'builtinbuf.h',
309 'bvector.h',
310 'complex.h',
311 'defalloc.h',
312 'deque.h',
313 'editbuf.h',
314 'fstream.h',
315 'function.h',
316 'hash_map',
317 'hash_map.h',
318 'hash_set',
319 'hash_set.h',
320 'hashtable.h',
321 'heap.h',
322 'indstream.h',
323 'iomanip.h',
324 'iostream.h',
325 'istream.h',
326 'iterator.h',
327 'list.h',
328 'map.h',
329 'multimap.h',
330 'multiset.h',
331 'ostream.h',
332 'pair.h',
333 'parsestream.h',
334 'pfstream.h',
335 'procbuf.h',
336 'pthread_alloc',
337 'pthread_alloc.h',
338 'rope',
339 'rope.h',
340 'ropeimpl.h',
341 'set.h',
342 'slist',
343 'slist.h',
344 'stack.h',
345 'stdiostream.h',
346 'stl_alloc.h',
347 'stl_relops.h',
348 'streambuf.h',
349 'stream.h',
350 'strfile.h',
351 'strstream.h',
352 'tempbuf.h',
353 'tree.h',
354 'type_traits.h',
355 'vector.h',
356 # 17.6.1.2 C++ library headers
357 'algorithm',
358 'array',
359 'atomic',
360 'bitset',
361 'chrono',
362 'codecvt',
363 'complex',
364 'condition_variable',
365 'deque',
366 'exception',
367 'forward_list',
368 'fstream',
369 'functional',
370 'future',
371 'initializer_list',
372 'iomanip',
373 'ios',
374 'iosfwd',
375 'iostream',
376 'istream',
377 'iterator',
378 'limits',
379 'list',
380 'locale',
381 'map',
382 'memory',
383 'mutex',
384 'new',
385 'numeric',
386 'ostream',
387 'queue',
388 'random',
389 'ratio',
390 'regex',
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000391 'scoped_allocator',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000392 'set',
393 'sstream',
394 'stack',
395 'stdexcept',
396 'streambuf',
397 'string',
398 'strstream',
399 'system_error',
400 'thread',
401 'tuple',
402 'typeindex',
403 'typeinfo',
404 'type_traits',
405 'unordered_map',
406 'unordered_set',
407 'utility',
408 'valarray',
409 'vector',
410 # 17.6.1.2 C++ headers for C library facilities
411 'cassert',
412 'ccomplex',
413 'cctype',
414 'cerrno',
415 'cfenv',
416 'cfloat',
417 'cinttypes',
418 'ciso646',
419 'climits',
420 'clocale',
421 'cmath',
422 'csetjmp',
423 'csignal',
424 'cstdalign',
425 'cstdarg',
426 'cstdbool',
427 'cstddef',
428 'cstdint',
429 'cstdio',
430 'cstdlib',
431 'cstring',
432 'ctgmath',
433 'ctime',
434 'cuchar',
435 'cwchar',
436 'cwctype',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000437 ])
438
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000439# Type names
440_TYPES = re.compile(
441 r'^(?:'
442 # [dcl.type.simple]
443 r'(char(16_t|32_t)?)|wchar_t|'
444 r'bool|short|int|long|signed|unsigned|float|double|'
445 # [support.types]
446 r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|'
447 # [cstdint.syn]
448 r'(u?int(_fast|_least)?(8|16|32|64)_t)|'
449 r'(u?int(max|ptr)_t)|'
450 r')$')
451
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000452
avakulenko@google.com59146752014-08-11 20:20:55 +0000453# These headers are excluded from [build/include] and [build/include_order]
454# checks:
455# - Anything not following google file name conventions (containing an
456# uppercase character, such as Python.h or nsStringAPI.h, for example).
457# - Lua headers.
458_THIRD_PARTY_HEADERS_PATTERN = re.compile(
459 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
460
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000461# Pattern for matching FileInfo.BaseName() against test file name
462_TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$'
463
464# Pattern that matches only complete whitespace, possibly across multiple lines.
465_EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL)
avakulenko@google.com59146752014-08-11 20:20:55 +0000466
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000467# Assertion macros. These are defined in base/logging.h and
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000468# testing/base/public/gunit.h.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000469_CHECK_MACROS = [
erg@google.com6317a9c2009-06-25 00:28:19 +0000470 'DCHECK', 'CHECK',
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000471 'EXPECT_TRUE', 'ASSERT_TRUE',
472 'EXPECT_FALSE', 'ASSERT_FALSE',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000473 ]
474
erg@google.com6317a9c2009-06-25 00:28:19 +0000475# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000476_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
477
478for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
479 ('>=', 'GE'), ('>', 'GT'),
480 ('<=', 'LE'), ('<', 'LT')]:
erg@google.com6317a9c2009-06-25 00:28:19 +0000481 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000482 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
483 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
484 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000485
486for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
487 ('>=', 'LT'), ('>', 'LE'),
488 ('<=', 'GT'), ('<', 'GE')]:
489 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
490 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000491
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000492# Alternative tokens and their replacements. For full list, see section 2.5
493# Alternative tokens [lex.digraph] in the C++ standard.
494#
495# Digraphs (such as '%:') are not included here since it's a mess to
496# match those on a word boundary.
497_ALT_TOKEN_REPLACEMENT = {
498 'and': '&&',
499 'bitor': '|',
500 'or': '||',
501 'xor': '^',
502 'compl': '~',
503 'bitand': '&',
504 'and_eq': '&=',
505 'or_eq': '|=',
506 'xor_eq': '^=',
507 'not': '!',
508 'not_eq': '!='
509 }
510
511# Compile regular expression that matches all the above keywords. The "[ =()]"
512# bit is meant to avoid matching these keywords outside of boolean expressions.
513#
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000514# False positives include C-style multi-line comments and multi-line strings
515# but those have always been troublesome for cpplint.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000516_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
517 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
518
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000519
520# These constants define types of headers for use with
521# _IncludeState.CheckNextIncludeOrder().
522_C_SYS_HEADER = 1
523_CPP_SYS_HEADER = 2
524_LIKELY_MY_HEADER = 3
525_POSSIBLE_MY_HEADER = 4
526_OTHER_HEADER = 5
527
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000528# These constants define the current inline assembly state
529_NO_ASM = 0 # Outside of inline assembly block
530_INSIDE_ASM = 1 # Inside inline assembly block
531_END_ASM = 2 # Last line of inline assembly block
532_BLOCK_ASM = 3 # The whole block is an inline assembly block
533
534# Match start of assembly blocks
535_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
536 r'(?:\s+(volatile|__volatile__))?'
537 r'\s*[{(]')
538
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000539# Match strings that indicate we're working on a C (not C++) file.
540_SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|'
541 r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))')
542
543# Match string that indicates we're working on a Linux Kernel file.
544_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000545
546_regexp_compile_cache = {}
547
erg@google.com35589e62010-11-17 18:58:16 +0000548# {str, set(int)}: a map from error categories to sets of linenumbers
549# on which those errors are expected and should be suppressed.
550_error_suppressions = {}
551
mazda@chromium.org3fffcec2013-06-07 01:04:53 +0000552# The root directory used for deriving header guard CPP variable.
553# This is set by --root flag.
554_root = None
Amin Hassanif7e1e102018-06-21 20:13:30 +0000555_root_debug = False
sdefresne263e9282016-07-19 02:14:22 -0700556
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000557# The allowed line length of files.
558# This is set by --linelength flag.
559_line_length = 80
560
561# The allowed extensions for file names
562# This is set by --extensions flag.
563_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
564
Amin Hassanif7e1e102018-06-21 20:13:30 +0000565# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
566# This is set by --headers flag.
567_hpp_headers = set(['h'])
568
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000569# {str, bool}: a map from error categories to booleans which indicate if the
570# category should be suppressed for every line.
571_global_error_suppressions = {}
572
Amin Hassanif7e1e102018-06-21 20:13:30 +0000573def ProcessHppHeadersOption(val):
574 global _hpp_headers
575 try:
576 _hpp_headers = set(val.split(','))
577 # Automatically append to extensions list so it does not have to be set 2 times
578 _valid_extensions.update(_hpp_headers)
579 except ValueError:
580 PrintUsage('Header extensions must be comma separated list.')
581
582def IsHeaderExtension(file_extension):
583 return file_extension in _hpp_headers
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000584
erg@google.com35589e62010-11-17 18:58:16 +0000585def ParseNolintSuppressions(filename, raw_line, linenum, error):
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000586 """Updates the global list of line error-suppressions.
erg@google.com35589e62010-11-17 18:58:16 +0000587
588 Parses any NOLINT comments on the current line, updating the global
589 error_suppressions store. Reports an error if the NOLINT comment
590 was malformed.
591
592 Args:
593 filename: str, the name of the input file.
594 raw_line: str, the line of input text, with comments.
595 linenum: int, the number of the current line.
596 error: function, an error handler.
597 """
avakulenko@google.com59146752014-08-11 20:20:55 +0000598 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000599 if matched:
avakulenko@google.com59146752014-08-11 20:20:55 +0000600 if matched.group(1):
601 suppressed_line = linenum + 1
602 else:
603 suppressed_line = linenum
604 category = matched.group(2)
erg@google.com35589e62010-11-17 18:58:16 +0000605 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.com59146752014-08-11 20:20:55 +0000606 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg@google.com35589e62010-11-17 18:58:16 +0000607 else:
608 if category.startswith('(') and category.endswith(')'):
609 category = category[1:-1]
610 if category in _ERROR_CATEGORIES:
avakulenko@google.com59146752014-08-11 20:20:55 +0000611 _error_suppressions.setdefault(category, set()).add(suppressed_line)
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000612 elif category not in _LEGACY_ERROR_CATEGORIES:
erg@google.com35589e62010-11-17 18:58:16 +0000613 error(filename, linenum, 'readability/nolint', 5,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000614 'Unknown NOLINT error category: %s' % category)
erg@google.com35589e62010-11-17 18:58:16 +0000615
616
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000617def ProcessGlobalSuppresions(lines):
618 """Updates the list of global error suppressions.
619
620 Parses any lint directives in the file that have global effect.
621
622 Args:
623 lines: An array of strings, each representing a line of the file, with the
624 last element being empty if the file is terminated with a newline.
625 """
626 for line in lines:
627 if _SEARCH_C_FILE.search(line):
628 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
629 _global_error_suppressions[category] = True
630 if _SEARCH_KERNEL_FILE.search(line):
631 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES:
632 _global_error_suppressions[category] = True
633
634
erg@google.com35589e62010-11-17 18:58:16 +0000635def ResetNolintSuppressions():
avakulenko@google.com59146752014-08-11 20:20:55 +0000636 """Resets the set of NOLINT suppressions to empty."""
erg@google.com35589e62010-11-17 18:58:16 +0000637 _error_suppressions.clear()
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000638 _global_error_suppressions.clear()
erg@google.com35589e62010-11-17 18:58:16 +0000639
640
641def IsErrorSuppressedByNolint(category, linenum):
642 """Returns true if the specified error category is suppressed on this line.
643
644 Consults the global error_suppressions map populated by
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000645 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions.
erg@google.com35589e62010-11-17 18:58:16 +0000646
647 Args:
648 category: str, the category of the error.
649 linenum: int, the current line number.
650 Returns:
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000651 bool, True iff the error should be suppressed due to a NOLINT comment or
652 global suppression.
erg@google.com35589e62010-11-17 18:58:16 +0000653 """
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000654 return (_global_error_suppressions.get(category, False) or
655 linenum in _error_suppressions.get(category, set()) or
erg@google.com35589e62010-11-17 18:58:16 +0000656 linenum in _error_suppressions.get(None, set()))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000657
avakulenko@google.comd39bbb52014-06-04 22:55:20 +0000658
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000659def Match(pattern, s):
660 """Matches the string with the pattern, caching the compiled regexp."""
661 # The regexp compilation caching is inlined in both Match and Search for
662 # performance reasons; factoring it out into a separate function turns out
663 # to be noticeably expensive.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000664 if pattern not in _regexp_compile_cache:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000665 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
666 return _regexp_compile_cache[pattern].match(s)
667
668
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000669def ReplaceAll(pattern, rep, s):
670 """Replaces instances of pattern in a string with a replacement.
671
672 The compiled regex is kept in a cache shared by Match and Search.
673
674 Args:
675 pattern: regex pattern
676 rep: replacement text
677 s: search string
678
679 Returns:
680 string with replacements made (or original string if no replacements)
681 """
682 if pattern not in _regexp_compile_cache:
683 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
684 return _regexp_compile_cache[pattern].sub(rep, s)
685
686
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000687def Search(pattern, s):
688 """Searches the string for the pattern, caching the compiled regexp."""
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000689 if pattern not in _regexp_compile_cache:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000690 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
691 return _regexp_compile_cache[pattern].search(s)
692
693
avakulenko@chromium.org764ce712016-05-06 23:03:41 +0000694def _IsSourceExtension(s):
695 """File extension (excluding dot) matches a source file extension."""
696 return s in ('c', 'cc', 'cpp', 'cxx')
697
698
avakulenko@google.com59146752014-08-11 20:20:55 +0000699class _IncludeState(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000700 """Tracks line numbers for includes, and the order in which includes appear.
701
avakulenko@google.com59146752014-08-11 20:20:55 +0000702 include_list contains list of lists of (header, line number) pairs.
703 It's a lists of lists rather than just one flat list to make it
704 easier to update across preprocessor boundaries.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000705
706 Call CheckNextIncludeOrder() once for each header in the file, passing
707 in the type constants defined above. Calls in an illegal order will
708 raise an _IncludeError with an appropriate error message.
709
710 """
711 # self._section will move monotonically through this set. If it ever
712 # needs to move backwards, CheckNextIncludeOrder will raise an error.
713 _INITIAL_SECTION = 0
714 _MY_H_SECTION = 1
715 _C_SECTION = 2
716 _CPP_SECTION = 3
717 _OTHER_H_SECTION = 4
718
719 _TYPE_NAMES = {
720 _C_SYS_HEADER: 'C system header',
721 _CPP_SYS_HEADER: 'C++ system header',
722 _LIKELY_MY_HEADER: 'header this file implements',
723 _POSSIBLE_MY_HEADER: 'header this file may implement',
724 _OTHER_HEADER: 'other header',
725 }
726 _SECTION_NAMES = {
727 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
728 _MY_H_SECTION: 'a header this file implements',
729 _C_SECTION: 'C system header',
730 _CPP_SECTION: 'C++ system header',
731 _OTHER_H_SECTION: 'other header',
732 }
733
734 def __init__(self):
avakulenko@google.com59146752014-08-11 20:20:55 +0000735 self.include_list = [[]]
736 self.ResetSection('')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000737
avakulenko@google.com59146752014-08-11 20:20:55 +0000738 def FindHeader(self, header):
739 """Check if a header has already been included.
740
741 Args:
742 header: header to check.
743 Returns:
744 Line number of previous occurrence, or -1 if the header has not
745 been seen before.
746 """
747 for section_list in self.include_list:
748 for f in section_list:
749 if f[0] == header:
750 return f[1]
751 return -1
752
753 def ResetSection(self, directive):
754 """Reset section checking for preprocessor directive.
755
756 Args:
757 directive: preprocessor directive (e.g. "if", "else").
758 """
erg@google.com26970fa2009-11-17 18:07:32 +0000759 # The name of the current section.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000760 self._section = self._INITIAL_SECTION
erg@google.com26970fa2009-11-17 18:07:32 +0000761 # The path of last found header.
762 self._last_header = ''
763
avakulenko@google.com59146752014-08-11 20:20:55 +0000764 # Update list of includes. Note that we never pop from the
765 # include list.
766 if directive in ('if', 'ifdef', 'ifndef'):
767 self.include_list.append([])
768 elif directive in ('else', 'elif'):
769 self.include_list[-1] = []
770
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000771 def SetLastHeader(self, header_path):
772 self._last_header = header_path
773
erg@google.com26970fa2009-11-17 18:07:32 +0000774 def CanonicalizeAlphabeticalOrder(self, header_path):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +0000775 """Returns a path canonicalized for alphabetical comparison.
erg@google.com26970fa2009-11-17 18:07:32 +0000776
777 - replaces "-" with "_" so they both cmp the same.
778 - removes '-inl' since we don't require them to be after the main header.
779 - lowercase everything, just in case.
780
781 Args:
782 header_path: Path to be canonicalized.
783
784 Returns:
785 Canonicalized path.
786 """
787 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
788
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000789 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.com26970fa2009-11-17 18:07:32 +0000790 """Check if a header is in alphabetical order with the previous header.
791
792 Args:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000793 clean_lines: A CleansedLines instance containing the file.
794 linenum: The number of the line to check.
795 header_path: Canonicalized header to be checked.
erg@google.com26970fa2009-11-17 18:07:32 +0000796
797 Returns:
798 Returns true if the header is in alphabetical order.
799 """
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +0000800 # If previous section is different from current section, _last_header will
801 # be reset to empty string, so it's always less than current header.
802 #
803 # If previous line was a blank line, assume that the headers are
804 # intentionally sorted the way they are.
805 if (self._last_header > header_path and
avakulenko@google.com255f2be2014-12-05 22:19:55 +0000806 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])):
erg@google.com26970fa2009-11-17 18:07:32 +0000807 return False
erg@google.com26970fa2009-11-17 18:07:32 +0000808 return True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000809
810 def CheckNextIncludeOrder(self, header_type):
811 """Returns a non-empty error message if the next header is out of order.
812
813 This function also updates the internal state to be ready to check
814 the next include.
815
816 Args:
817 header_type: One of the _XXX_HEADER constants defined above.
818
819 Returns:
820 The empty string if the header is in the right order, or an
821 error message describing what's wrong.
822
823 """
824 error_message = ('Found %s after %s' %
825 (self._TYPE_NAMES[header_type],
826 self._SECTION_NAMES[self._section]))
827
erg@google.com26970fa2009-11-17 18:07:32 +0000828 last_section = self._section
829
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000830 if header_type == _C_SYS_HEADER:
831 if self._section <= self._C_SECTION:
832 self._section = self._C_SECTION
833 else:
erg@google.com26970fa2009-11-17 18:07:32 +0000834 self._last_header = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000835 return error_message
836 elif header_type == _CPP_SYS_HEADER:
837 if self._section <= self._CPP_SECTION:
838 self._section = self._CPP_SECTION
839 else:
erg@google.com26970fa2009-11-17 18:07:32 +0000840 self._last_header = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000841 return error_message
842 elif header_type == _LIKELY_MY_HEADER:
843 if self._section <= self._MY_H_SECTION:
844 self._section = self._MY_H_SECTION
845 else:
846 self._section = self._OTHER_H_SECTION
847 elif header_type == _POSSIBLE_MY_HEADER:
848 if self._section <= self._MY_H_SECTION:
849 self._section = self._MY_H_SECTION
850 else:
851 # This will always be the fallback because we're not sure
852 # enough that the header is associated with this file.
853 self._section = self._OTHER_H_SECTION
854 else:
855 assert header_type == _OTHER_HEADER
856 self._section = self._OTHER_H_SECTION
857
erg@google.com26970fa2009-11-17 18:07:32 +0000858 if last_section != self._section:
859 self._last_header = ''
860
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000861 return ''
862
863
864class _CppLintState(object):
865 """Maintains module-wide state.."""
866
867 def __init__(self):
868 self.verbose_level = 1 # global setting.
869 self.error_count = 0 # global count of reported errors
erg@google.com6317a9c2009-06-25 00:28:19 +0000870 # filters to apply when emitting error messages
871 self.filters = _DEFAULT_FILTERS[:]
avakulenko@google.com17449932014-07-28 22:13:33 +0000872 # backup of filter list. Used to restore the state after each file.
873 self._filters_backup = self.filters[:]
erg@google.com26970fa2009-11-17 18:07:32 +0000874 self.counting = 'total' # In what way are we counting errors?
875 self.errors_by_category = {} # string to int dict storing error counts
Amin Hassanif7e1e102018-06-21 20:13:30 +0000876 self.quiet = False # Suppress non-error messagess?
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000877
878 # output format:
879 # "emacs" - format that emacs can parse (default)
880 # "vs7" - format that Microsoft Visual Studio 7 can parse
881 self.output_format = 'emacs'
882
883 def SetOutputFormat(self, output_format):
884 """Sets the output format for errors."""
885 self.output_format = output_format
886
Amin Hassanif7e1e102018-06-21 20:13:30 +0000887 def SetQuiet(self, quiet):
888 """Sets the module's quiet settings, and returns the previous setting."""
889 last_quiet = self.quiet
890 self.quiet = quiet
891 return last_quiet
892
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000893 def SetVerboseLevel(self, level):
894 """Sets the module's verbosity, and returns the previous setting."""
895 last_verbose_level = self.verbose_level
896 self.verbose_level = level
897 return last_verbose_level
898
erg@google.com26970fa2009-11-17 18:07:32 +0000899 def SetCountingStyle(self, counting_style):
900 """Sets the module's counting options."""
901 self.counting = counting_style
902
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000903 def SetFilters(self, filters):
904 """Sets the error-message filters.
905
906 These filters are applied when deciding whether to emit a given
907 error message.
908
909 Args:
910 filters: A string of comma-separated filters (eg "+whitespace/indent").
911 Each filter should start with + or -; else we die.
erg@google.com6317a9c2009-06-25 00:28:19 +0000912
913 Raises:
914 ValueError: The comma-separated filters did not all start with '+' or '-'.
915 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000916 """
erg@google.com6317a9c2009-06-25 00:28:19 +0000917 # Default filters always have less priority than the flag ones.
918 self.filters = _DEFAULT_FILTERS[:]
avakulenko@google.com17449932014-07-28 22:13:33 +0000919 self.AddFilters(filters)
920
921 def AddFilters(self, filters):
922 """ Adds more filters to the existing list of error-message filters. """
erg@google.com6317a9c2009-06-25 00:28:19 +0000923 for filt in filters.split(','):
924 clean_filt = filt.strip()
925 if clean_filt:
926 self.filters.append(clean_filt)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000927 for filt in self.filters:
928 if not (filt.startswith('+') or filt.startswith('-')):
929 raise ValueError('Every filter in --filters must start with + or -'
930 ' (%s does not)' % filt)
931
avakulenko@google.com17449932014-07-28 22:13:33 +0000932 def BackupFilters(self):
933 """ Saves the current filter list to backup storage."""
934 self._filters_backup = self.filters[:]
935
936 def RestoreFilters(self):
937 """ Restores filters previously backed up."""
938 self.filters = self._filters_backup[:]
939
erg@google.com26970fa2009-11-17 18:07:32 +0000940 def ResetErrorCounts(self):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000941 """Sets the module's error statistic back to zero."""
942 self.error_count = 0
erg@google.com26970fa2009-11-17 18:07:32 +0000943 self.errors_by_category = {}
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000944
erg@google.com26970fa2009-11-17 18:07:32 +0000945 def IncrementErrorCount(self, category):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000946 """Bumps the module's error statistic."""
947 self.error_count += 1
erg@google.com26970fa2009-11-17 18:07:32 +0000948 if self.counting in ('toplevel', 'detailed'):
949 if self.counting != 'detailed':
950 category = category.split('/')[0]
951 if category not in self.errors_by_category:
952 self.errors_by_category[category] = 0
953 self.errors_by_category[category] += 1
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000954
erg@google.com26970fa2009-11-17 18:07:32 +0000955 def PrintErrorCounts(self):
956 """Print a summary of errors by category, and the total."""
957 for category, count in self.errors_by_category.iteritems():
958 sys.stderr.write('Category \'%s\' errors found: %d\n' %
959 (category, count))
Amin Hassanif7e1e102018-06-21 20:13:30 +0000960 sys.stdout.write('Total errors found: %d\n' % self.error_count)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000961
962_cpplint_state = _CppLintState()
963
964
965def _OutputFormat():
966 """Gets the module's output format."""
967 return _cpplint_state.output_format
968
969
970def _SetOutputFormat(output_format):
971 """Sets the module's output format."""
972 _cpplint_state.SetOutputFormat(output_format)
973
Amin Hassanif7e1e102018-06-21 20:13:30 +0000974def _Quiet():
975 """Return's the module's quiet setting."""
976 return _cpplint_state.quiet
977
978def _SetQuiet(quiet):
979 """Set the module's quiet status, and return previous setting."""
980 return _cpplint_state.SetQuiet(quiet)
981
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000982
983def _VerboseLevel():
984 """Returns the module's verbosity setting."""
985 return _cpplint_state.verbose_level
986
987
988def _SetVerboseLevel(level):
989 """Sets the module's verbosity, and returns the previous setting."""
990 return _cpplint_state.SetVerboseLevel(level)
991
992
erg@google.com26970fa2009-11-17 18:07:32 +0000993def _SetCountingStyle(level):
994 """Sets the module's counting options."""
995 _cpplint_state.SetCountingStyle(level)
996
997
maruel@google.comfb2b8eb2009-04-23 21:03:42 +0000998def _Filters():
999 """Returns the module's list of output filters, as a list."""
1000 return _cpplint_state.filters
1001
1002
1003def _SetFilters(filters):
1004 """Sets the module's error-message filters.
1005
1006 These filters are applied when deciding whether to emit a given
1007 error message.
1008
1009 Args:
1010 filters: A string of comma-separated filters (eg "whitespace/indent").
1011 Each filter should start with + or -; else we die.
1012 """
1013 _cpplint_state.SetFilters(filters)
1014
avakulenko@google.com17449932014-07-28 22:13:33 +00001015def _AddFilters(filters):
1016 """Adds more filter overrides.
avakulenko@google.com59146752014-08-11 20:20:55 +00001017
avakulenko@google.com17449932014-07-28 22:13:33 +00001018 Unlike _SetFilters, this function does not reset the current list of filters
1019 available.
1020
1021 Args:
1022 filters: A string of comma-separated filters (eg "whitespace/indent").
1023 Each filter should start with + or -; else we die.
1024 """
1025 _cpplint_state.AddFilters(filters)
1026
1027def _BackupFilters():
1028 """ Saves the current filter list to backup storage."""
1029 _cpplint_state.BackupFilters()
1030
1031def _RestoreFilters():
1032 """ Restores filters previously backed up."""
1033 _cpplint_state.RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001034
1035class _FunctionState(object):
1036 """Tracks current function name and the number of lines in its body."""
1037
1038 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
1039 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
1040
1041 def __init__(self):
1042 self.in_a_function = False
1043 self.lines_in_function = 0
1044 self.current_function = ''
1045
1046 def Begin(self, function_name):
1047 """Start analyzing function body.
1048
1049 Args:
1050 function_name: The name of the function being tracked.
1051 """
1052 self.in_a_function = True
1053 self.lines_in_function = 0
1054 self.current_function = function_name
1055
1056 def Count(self):
1057 """Count line in current function body."""
1058 if self.in_a_function:
1059 self.lines_in_function += 1
1060
1061 def Check(self, error, filename, linenum):
1062 """Report if too many lines in function body.
1063
1064 Args:
1065 error: The function to call with any errors found.
1066 filename: The name of the current file.
1067 linenum: The number of the line to check.
1068 """
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00001069 if not self.in_a_function:
1070 return
1071
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001072 if Match(r'T(EST|est)', self.current_function):
1073 base_trigger = self._TEST_TRIGGER
1074 else:
1075 base_trigger = self._NORMAL_TRIGGER
1076 trigger = base_trigger * 2**_VerboseLevel()
1077
1078 if self.lines_in_function > trigger:
1079 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
1080 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1081 if error_level > 5:
1082 error_level = 5
1083 error(filename, linenum, 'readability/fn_size', error_level,
1084 'Small and focused functions are preferred:'
1085 ' %s has %d non-comment lines'
1086 ' (error triggered by exceeding %d lines).' % (
1087 self.current_function, self.lines_in_function, trigger))
1088
1089 def End(self):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001090 """Stop analyzing function body."""
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001091 self.in_a_function = False
1092
1093
1094class _IncludeError(Exception):
1095 """Indicates a problem with the include order in a file."""
1096 pass
1097
1098
avakulenko@google.com59146752014-08-11 20:20:55 +00001099class FileInfo(object):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001100 """Provides utility functions for filenames.
1101
1102 FileInfo provides easy access to the components of a file's path
1103 relative to the project root.
1104 """
1105
1106 def __init__(self, filename):
1107 self._filename = filename
1108
1109 def FullName(self):
1110 """Make Windows paths like Unix."""
1111 return os.path.abspath(self._filename).replace('\\', '/')
1112
1113 def RepositoryName(self):
1114 """FullName after removing the local path to the repository.
1115
1116 If we have a real absolute path name here we can try to do something smart:
1117 detecting the root of the checkout and truncating /path/to/checkout from
1118 the name so that we get header guards that don't include things like
1119 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
1120 people on different computers who have checked the source out to different
1121 locations won't see bogus errors.
1122 """
1123 fullname = self.FullName()
1124
1125 if os.path.exists(fullname):
1126 project_dir = os.path.dirname(fullname)
1127
1128 if os.path.exists(os.path.join(project_dir, ".svn")):
1129 # If there's a .svn file in the current directory, we recursively look
1130 # up the directory tree for the top of the SVN checkout
1131 root_dir = project_dir
1132 one_up_dir = os.path.dirname(root_dir)
1133 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1134 root_dir = os.path.dirname(root_dir)
1135 one_up_dir = os.path.dirname(one_up_dir)
1136
1137 prefix = os.path.commonprefix([root_dir, project_dir])
1138 return fullname[len(prefix) + 1:]
1139
erg@chromium.org7956a872011-11-30 01:44:03 +00001140 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1141 # searching up from the current path.
Amin Hassanif7e1e102018-06-21 20:13:30 +00001142 root_dir = current_dir = os.path.dirname(fullname)
1143 while current_dir != os.path.dirname(current_dir):
1144 if (os.path.exists(os.path.join(current_dir, ".git")) or
1145 os.path.exists(os.path.join(current_dir, ".hg")) or
1146 os.path.exists(os.path.join(current_dir, ".svn"))):
1147 root_dir = current_dir
1148 current_dir = os.path.dirname(current_dir)
erg@google.com35589e62010-11-17 18:58:16 +00001149
1150 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@chromium.org7956a872011-11-30 01:44:03 +00001151 os.path.exists(os.path.join(root_dir, ".hg")) or
1152 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com35589e62010-11-17 18:58:16 +00001153 prefix = os.path.commonprefix([root_dir, project_dir])
1154 return fullname[len(prefix) + 1:]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001155
1156 # Don't know what to do; header guard warnings may be wrong...
1157 return fullname
1158
1159 def Split(self):
1160 """Splits the file into the directory, basename, and extension.
1161
1162 For 'chrome/browser/browser.cc', Split() would
1163 return ('chrome/browser', 'browser', '.cc')
1164
1165 Returns:
1166 A tuple of (directory, basename, extension).
1167 """
1168
1169 googlename = self.RepositoryName()
1170 project, rest = os.path.split(googlename)
1171 return (project,) + os.path.splitext(rest)
1172
1173 def BaseName(self):
1174 """File base name - text after the final slash, before the final period."""
1175 return self.Split()[1]
1176
1177 def Extension(self):
1178 """File extension - text following the final period."""
1179 return self.Split()[2]
1180
1181 def NoExtension(self):
1182 """File has no source file extension."""
1183 return '/'.join(self.Split()[0:2])
1184
1185 def IsSource(self):
1186 """File has a source file extension."""
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00001187 return _IsSourceExtension(self.Extension()[1:])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001188
1189
erg@google.com35589e62010-11-17 18:58:16 +00001190def _ShouldPrintError(category, confidence, linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001191 """If confidence >= verbose, category passes filter and is not suppressed."""
erg@google.com35589e62010-11-17 18:58:16 +00001192
1193 # There are three ways we might decide not to print an error message:
1194 # a "NOLINT(category)" comment appears in the source,
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001195 # the verbosity level isn't high enough, or the filters filter it out.
erg@google.com35589e62010-11-17 18:58:16 +00001196 if IsErrorSuppressedByNolint(category, linenum):
1197 return False
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001198
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001199 if confidence < _cpplint_state.verbose_level:
1200 return False
1201
1202 is_filtered = False
1203 for one_filter in _Filters():
1204 if one_filter.startswith('-'):
1205 if category.startswith(one_filter[1:]):
1206 is_filtered = True
1207 elif one_filter.startswith('+'):
1208 if category.startswith(one_filter[1:]):
1209 is_filtered = False
1210 else:
1211 assert False # should have been checked for in SetFilter.
1212 if is_filtered:
1213 return False
1214
1215 return True
1216
1217
1218def Error(filename, linenum, category, confidence, message):
1219 """Logs the fact we've found a lint error.
1220
1221 We log where the error was found, and also our confidence in the error,
1222 that is, how certain we are this is a legitimate style regression, and
1223 not a misidentification or a use that's sometimes justified.
1224
erg@google.com35589e62010-11-17 18:58:16 +00001225 False positives can be suppressed by the use of
1226 "cpplint(category)" comments on the offending line. These are
1227 parsed into _error_suppressions.
1228
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001229 Args:
1230 filename: The name of the file containing the error.
1231 linenum: The number of the line containing the error.
1232 category: A string used to describe the "category" this bug
1233 falls under: "whitespace", say, or "runtime". Categories
1234 may have a hierarchy separated by slashes: "whitespace/indent".
1235 confidence: A number from 1-5 representing a confidence score for
1236 the error, with 5 meaning that we are certain of the problem,
1237 and 1 meaning that it could be a legitimate construct.
1238 message: The error message.
1239 """
erg@google.com35589e62010-11-17 18:58:16 +00001240 if _ShouldPrintError(category, confidence, linenum):
erg@google.com26970fa2009-11-17 18:07:32 +00001241 _cpplint_state.IncrementErrorCount(category)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001242 if _cpplint_state.output_format == 'vs7':
Amin Hassanif7e1e102018-06-21 20:13:30 +00001243 sys.stderr.write('%s(%s): error cpplint: [%s] %s [%d]\n' % (
1244 filename, linenum, category, message, confidence))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001245 elif _cpplint_state.output_format == 'eclipse':
1246 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1247 filename, linenum, message, category, confidence))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001248 else:
1249 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1250 filename, linenum, message, category, confidence))
1251
1252
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001253# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001254_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1255 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001256# Match a single C style comment on the same line.
1257_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1258# Matches multi-line C style comments.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001259# This RE is a little bit more complicated than one might expect, because we
1260# have to take care of space removals tools so we can handle comments inside
1261# statements better.
1262# The current rule is: We only clear spaces from both sides when we're at the
1263# end of the line. Otherwise, we try to remove spaces from the right side,
1264# if this doesn't work we try on left side but only if there's a non-character
1265# on the right.
1266_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001267 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1268 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1269 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1270 _RE_PATTERN_C_COMMENTS + r')')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001271
1272
1273def IsCppString(line):
1274 """Does line terminate so, that the next symbol is in string constant.
1275
1276 This function does not consider single-line nor multi-line comments.
1277
1278 Args:
1279 line: is a partial line of code starting from the 0..n.
1280
1281 Returns:
1282 True, if next character appended to 'line' is inside a
1283 string constant.
1284 """
1285
1286 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1287 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1288
1289
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001290def CleanseRawStrings(raw_lines):
1291 """Removes C++11 raw strings from lines.
1292
1293 Before:
1294 static const char kData[] = R"(
1295 multi-line string
1296 )";
1297
1298 After:
1299 static const char kData[] = ""
1300 (replaced by blank line)
1301 "";
1302
1303 Args:
1304 raw_lines: list of raw lines.
1305
1306 Returns:
1307 list of lines with C++11 raw strings replaced by empty strings.
1308 """
1309
1310 delimiter = None
1311 lines_without_raw_strings = []
1312 for line in raw_lines:
1313 if delimiter:
1314 # Inside a raw string, look for the end
1315 end = line.find(delimiter)
1316 if end >= 0:
1317 # Found the end of the string, match leading space for this
1318 # line and resume copying the original lines, and also insert
1319 # a "" on the last line.
1320 leading_space = Match(r'^(\s*)\S', line)
1321 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1322 delimiter = None
1323 else:
1324 # Haven't found the end yet, append a blank line.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001325 line = '""'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001326
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001327 # Look for beginning of a raw string, and replace them with
1328 # empty strings. This is done in a loop to handle multiple raw
1329 # strings on the same line.
1330 while delimiter is None:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001331 # Look for beginning of a raw string.
1332 # See 2.14.15 [lex.string] for syntax.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00001333 #
1334 # Once we have matched a raw string, we check the prefix of the
1335 # line to make sure that the line is not part of a single line
1336 # comment. It's done this way because we remove raw strings
1337 # before removing comments as opposed to removing comments
1338 # before removing raw strings. This is because there are some
1339 # cpplint checks that requires the comments to be preserved, but
1340 # we don't want to check comments that are inside raw strings.
1341 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1342 if (matched and
1343 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
1344 matched.group(1))):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001345 delimiter = ')' + matched.group(2) + '"'
1346
1347 end = matched.group(3).find(delimiter)
1348 if end >= 0:
1349 # Raw string ended on same line
1350 line = (matched.group(1) + '""' +
1351 matched.group(3)[end + len(delimiter):])
1352 delimiter = None
1353 else:
1354 # Start of a multi-line raw string
1355 line = matched.group(1) + '""'
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001356 else:
1357 break
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001358
1359 lines_without_raw_strings.append(line)
1360
1361 # TODO(unknown): if delimiter is not None here, we might want to
1362 # emit a warning for unterminated string.
1363 return lines_without_raw_strings
1364
1365
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001366def FindNextMultiLineCommentStart(lines, lineix):
1367 """Find the beginning marker for a multiline comment."""
1368 while lineix < len(lines):
1369 if lines[lineix].strip().startswith('/*'):
1370 # Only return this marker if the comment goes beyond this line
1371 if lines[lineix].strip().find('*/', 2) < 0:
1372 return lineix
1373 lineix += 1
1374 return len(lines)
1375
1376
1377def FindNextMultiLineCommentEnd(lines, lineix):
1378 """We are inside a comment, find the end marker."""
1379 while lineix < len(lines):
1380 if lines[lineix].strip().endswith('*/'):
1381 return lineix
1382 lineix += 1
1383 return len(lines)
1384
1385
1386def RemoveMultiLineCommentsFromRange(lines, begin, end):
1387 """Clears a range of lines for multi-line comments."""
1388 # Having // dummy comments makes the lines non-empty, so we will not get
1389 # unnecessary blank line warnings later in the code.
1390 for i in range(begin, end):
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001391 lines[i] = '/**/'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001392
1393
1394def RemoveMultiLineComments(filename, lines, error):
1395 """Removes multiline (c-style) comments from lines."""
1396 lineix = 0
1397 while lineix < len(lines):
1398 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1399 if lineix_begin >= len(lines):
1400 return
1401 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1402 if lineix_end >= len(lines):
1403 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1404 'Could not find end of multi-line comment')
1405 return
1406 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1407 lineix = lineix_end + 1
1408
1409
1410def CleanseComments(line):
1411 """Removes //-comments and single-line C-style /* */ comments.
1412
1413 Args:
1414 line: A line of C++ source.
1415
1416 Returns:
1417 The line with single-line comments removed.
1418 """
1419 commentpos = line.find('//')
1420 if commentpos != -1 and not IsCppString(line[:commentpos]):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00001421 line = line[:commentpos].rstrip()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001422 # get rid of /* ... */
1423 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1424
1425
erg@google.com6317a9c2009-06-25 00:28:19 +00001426class CleansedLines(object):
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001427 """Holds 4 copies of all lines with different preprocessing applied to them.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001428
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001429 1) elided member contains lines without strings and comments.
1430 2) lines member contains lines without comments.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001431 3) raw_lines member contains all the lines without processing.
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001432 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw
1433 strings removed.
1434 All these members are of <type 'list'>, and of the same length.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001435 """
1436
1437 def __init__(self, lines):
1438 self.elided = []
1439 self.lines = []
1440 self.raw_lines = lines
1441 self.num_lines = len(lines)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001442 self.lines_without_raw_strings = CleanseRawStrings(lines)
1443 for linenum in range(len(self.lines_without_raw_strings)):
1444 self.lines.append(CleanseComments(
1445 self.lines_without_raw_strings[linenum]))
1446 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001447 self.elided.append(CleanseComments(elided))
1448
1449 def NumLines(self):
1450 """Returns the number of lines represented."""
1451 return self.num_lines
1452
1453 @staticmethod
1454 def _CollapseStrings(elided):
1455 """Collapses strings and chars on a line to simple "" or '' blocks.
1456
1457 We nix strings first so we're not fooled by text like '"http://"'
1458
1459 Args:
1460 elided: The line being processed.
1461
1462 Returns:
1463 The line with collapsed strings.
1464 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001465 if _RE_PATTERN_INCLUDE.match(elided):
1466 return elided
1467
1468 # Remove escaped characters first to make quote/single quote collapsing
1469 # basic. Things that look like escaped characters shouldn't occur
1470 # outside of strings and chars.
1471 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1472
1473 # Replace quoted strings and digit separators. Both single quotes
1474 # and double quotes are processed in the same loop, otherwise
1475 # nested quotes wouldn't work.
1476 collapsed = ''
1477 while True:
1478 # Find the first quote character
1479 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1480 if not match:
1481 collapsed += elided
1482 break
1483 head, quote, tail = match.groups()
1484
1485 if quote == '"':
1486 # Collapse double quoted strings
1487 second_quote = tail.find('"')
1488 if second_quote >= 0:
1489 collapsed += head + '""'
1490 elided = tail[second_quote + 1:]
1491 else:
1492 # Unmatched double quote, don't bother processing the rest
1493 # of the line since this is probably a multiline string.
1494 collapsed += elided
1495 break
1496 else:
1497 # Found single quote, check nearby text to eliminate digit separators.
1498 #
1499 # There is no special handling for floating point here, because
1500 # the integer/fractional/exponent parts would all be parsed
1501 # correctly as long as there are digits on both sides of the
1502 # separator. So we are fine as long as we don't see something
1503 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1504 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1505 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1506 collapsed += head + match_literal.group(1).replace("'", '')
1507 elided = match_literal.group(2)
1508 else:
1509 second_quote = tail.find('\'')
1510 if second_quote >= 0:
1511 collapsed += head + "''"
1512 elided = tail[second_quote + 1:]
1513 else:
1514 # Unmatched single quote
1515 collapsed += elided
1516 break
1517
1518 return collapsed
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001519
1520
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001521def FindEndOfExpressionInLine(line, startpos, stack):
1522 """Find the position just after the end of current parenthesized expression.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001523
1524 Args:
1525 line: a CleansedLines line.
1526 startpos: start searching at this position.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001527 stack: nesting stack at startpos.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001528
1529 Returns:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001530 On finding matching end: (index just after matching end, None)
1531 On finding an unclosed expression: (-1, None)
1532 Otherwise: (-1, new stack at end of this line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001533 """
1534 for i in xrange(startpos, len(line)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001535 char = line[i]
1536 if char in '([{':
1537 # Found start of parenthesized expression, push to expression stack
1538 stack.append(char)
1539 elif char == '<':
1540 # Found potential start of template argument list
1541 if i > 0 and line[i - 1] == '<':
1542 # Left shift operator
1543 if stack and stack[-1] == '<':
1544 stack.pop()
1545 if not stack:
1546 return (-1, None)
1547 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1548 # operator<, don't add to stack
1549 continue
1550 else:
1551 # Tentative start of template argument list
1552 stack.append('<')
1553 elif char in ')]}':
1554 # Found end of parenthesized expression.
1555 #
1556 # If we are currently expecting a matching '>', the pending '<'
1557 # must have been an operator. Remove them from expression stack.
1558 while stack and stack[-1] == '<':
1559 stack.pop()
1560 if not stack:
1561 return (-1, None)
1562 if ((stack[-1] == '(' and char == ')') or
1563 (stack[-1] == '[' and char == ']') or
1564 (stack[-1] == '{' and char == '}')):
1565 stack.pop()
1566 if not stack:
1567 return (i + 1, None)
1568 else:
1569 # Mismatched parentheses
1570 return (-1, None)
1571 elif char == '>':
1572 # Found potential end of template argument list.
1573
1574 # Ignore "->" and operator functions
1575 if (i > 0 and
1576 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1577 continue
1578
1579 # Pop the stack if there is a matching '<'. Otherwise, ignore
1580 # this '>' since it must be an operator.
1581 if stack:
1582 if stack[-1] == '<':
1583 stack.pop()
1584 if not stack:
1585 return (i + 1, None)
1586 elif char == ';':
1587 # Found something that look like end of statements. If we are currently
1588 # expecting a '>', the matching '<' must have been an operator, since
1589 # template argument list should not contain statements.
1590 while stack and stack[-1] == '<':
1591 stack.pop()
1592 if not stack:
1593 return (-1, None)
1594
1595 # Did not find end of expression or unbalanced parentheses on this line
1596 return (-1, stack)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001597
1598
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001599def CloseExpression(clean_lines, linenum, pos):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001600 """If input points to ( or { or [ or <, finds the position that closes it.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001601
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001602 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001603 linenum/pos that correspond to the closing of the expression.
1604
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001605 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1606 Ideally we would want to index all opening and closing parentheses once
1607 and have CloseExpression be just a simple lookup, but due to preprocessor
1608 tricks, this is not so easy.
1609
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001610 Args:
1611 clean_lines: A CleansedLines instance containing the file.
1612 linenum: The number of the line to check.
1613 pos: A position on the line.
1614
1615 Returns:
1616 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1617 (line, len(lines), -1) if we never find a close. Note we ignore
1618 strings and comments when matching; and the line we return is the
1619 'cleansed' line at linenum.
1620 """
1621
1622 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001623 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001624 return (line, clean_lines.NumLines(), -1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001625
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001626 # Check first line
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001627 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001628 if end_pos > -1:
1629 return (line, linenum, end_pos)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001630
1631 # Continue scanning forward
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001632 while stack and linenum < clean_lines.NumLines() - 1:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001633 linenum += 1
1634 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001635 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001636 if end_pos > -1:
1637 return (line, linenum, end_pos)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001638
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001639 # Did not find end of expression before end of file, give up
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001640 return (line, clean_lines.NumLines(), -1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001641
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001642
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001643def FindStartOfExpressionInLine(line, endpos, stack):
1644 """Find position at the matching start of current expression.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001645
1646 This is almost the reverse of FindEndOfExpressionInLine, but note
1647 that the input position and returned position differs by 1.
1648
1649 Args:
1650 line: a CleansedLines line.
1651 endpos: start searching at this position.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001652 stack: nesting stack at endpos.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001653
1654 Returns:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001655 On finding matching start: (index at matching start, None)
1656 On finding an unclosed expression: (-1, None)
1657 Otherwise: (-1, new stack at beginning of this line)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001658 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001659 i = endpos
1660 while i >= 0:
1661 char = line[i]
1662 if char in ')]}':
1663 # Found end of expression, push to expression stack
1664 stack.append(char)
1665 elif char == '>':
1666 # Found potential end of template argument list.
1667 #
1668 # Ignore it if it's a "->" or ">=" or "operator>"
1669 if (i > 0 and
1670 (line[i - 1] == '-' or
1671 Match(r'\s>=\s', line[i - 1:]) or
1672 Search(r'\boperator\s*$', line[0:i]))):
1673 i -= 1
1674 else:
1675 stack.append('>')
1676 elif char == '<':
1677 # Found potential start of template argument list
1678 if i > 0 and line[i - 1] == '<':
1679 # Left shift operator
1680 i -= 1
1681 else:
1682 # If there is a matching '>', we can pop the expression stack.
1683 # Otherwise, ignore this '<' since it must be an operator.
1684 if stack and stack[-1] == '>':
1685 stack.pop()
1686 if not stack:
1687 return (i, None)
1688 elif char in '([{':
1689 # Found start of expression.
1690 #
1691 # If there are any unmatched '>' on the stack, they must be
1692 # operators. Remove those.
1693 while stack and stack[-1] == '>':
1694 stack.pop()
1695 if not stack:
1696 return (-1, None)
1697 if ((char == '(' and stack[-1] == ')') or
1698 (char == '[' and stack[-1] == ']') or
1699 (char == '{' and stack[-1] == '}')):
1700 stack.pop()
1701 if not stack:
1702 return (i, None)
1703 else:
1704 # Mismatched parentheses
1705 return (-1, None)
1706 elif char == ';':
1707 # Found something that look like end of statements. If we are currently
1708 # expecting a '<', the matching '>' must have been an operator, since
1709 # template argument list should not contain statements.
1710 while stack and stack[-1] == '>':
1711 stack.pop()
1712 if not stack:
1713 return (-1, None)
1714
1715 i -= 1
1716
1717 return (-1, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001718
1719
1720def ReverseCloseExpression(clean_lines, linenum, pos):
1721 """If input points to ) or } or ] or >, finds the position that opens it.
1722
1723 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1724 linenum/pos that correspond to the opening of the expression.
1725
1726 Args:
1727 clean_lines: A CleansedLines instance containing the file.
1728 linenum: The number of the line to check.
1729 pos: A position on the line.
1730
1731 Returns:
1732 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1733 (line, 0, -1) if we never find the matching opening brace. Note
1734 we ignore strings and comments when matching; and the line we
1735 return is the 'cleansed' line at linenum.
1736 """
1737 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001738 if line[pos] not in ')}]>':
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001739 return (line, 0, -1)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001740
1741 # Check last line
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001742 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001743 if start_pos > -1:
1744 return (line, linenum, start_pos)
1745
1746 # Continue scanning backward
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001747 while stack and linenum > 0:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001748 linenum -= 1
1749 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001750 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001751 if start_pos > -1:
1752 return (line, linenum, start_pos)
1753
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001754 # Did not find start of expression before beginning of file, give up
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00001755 return (line, 0, -1)
1756
1757
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001758def CheckForCopyright(filename, lines, error):
1759 """Logs an error if no Copyright message appears at the top of the file."""
1760
1761 # We'll say it should occur by line 10. Don't forget there's a
1762 # dummy line at the front.
1763 for line in xrange(1, min(len(lines), 11)):
1764 if re.search(r'Copyright', lines[line], re.I): break
1765 else: # means no copyright line was found
1766 error(filename, 0, 'legal/copyright', 5,
1767 'No copyright message found. '
1768 'You should have a line: "Copyright [year] <Copyright Owner>"')
1769
1770
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001771def GetIndentLevel(line):
1772 """Return the number of leading spaces in line.
1773
1774 Args:
1775 line: A string to check.
1776
1777 Returns:
1778 An integer count of leading spaces, possibly zero.
1779 """
1780 indent = Match(r'^( *)\S', line)
1781 if indent:
1782 return len(indent.group(1))
1783 else:
1784 return 0
1785
Amin Hassanif7e1e102018-06-21 20:13:30 +00001786def PathSplitToList(path):
1787 """Returns the path split into a list by the separator.
1788
1789 Args:
1790 path: An absolute or relative path (e.g. '/a/b/c/' or '../a')
1791
1792 Returns:
1793 A list of path components (e.g. ['a', 'b', 'c]).
1794 """
1795 lst = []
1796 while True:
1797 (head, tail) = os.path.split(path)
1798 if head == path: # absolute paths end
1799 lst.append(head)
1800 break
1801 if tail == path: # relative paths end
1802 lst.append(tail)
1803 break
1804
1805 path = head
1806 lst.append(tail)
1807
1808 lst.reverse()
1809 return lst
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00001810
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001811def GetHeaderGuardCPPVariable(filename):
1812 """Returns the CPP variable that should be used as a header guard.
1813
1814 Args:
1815 filename: The name of a C++ header file.
1816
1817 Returns:
1818 The CPP variable that should be used as a header guard in the
1819 named file.
1820
1821 """
1822
erg@google.com35589e62010-11-17 18:58:16 +00001823 # Restores original filename in case that cpplint is invoked from Emacs's
1824 # flymake.
1825 filename = re.sub(r'_flymake\.h$', '.h', filename)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001826 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001827 # Replace 'c++' with 'cpp'.
1828 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')
skym@chromium.org3990c412016-02-05 20:55:12 +00001829
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001830 fileinfo = FileInfo(filename)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00001831 file_path_from_root = fileinfo.RepositoryName()
Amin Hassanif7e1e102018-06-21 20:13:30 +00001832
1833 def FixupPathFromRoot():
1834 if _root_debug:
1835 sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n"
1836 %(_root, fileinfo.RepositoryName()))
1837
1838 # Process the file path with the --root flag if it was set.
1839 if not _root:
1840 if _root_debug:
1841 sys.stderr.write("_root unspecified\n")
1842 return file_path_from_root
1843
1844 def StripListPrefix(lst, prefix):
1845 # f(['x', 'y'], ['w, z']) -> None (not a valid prefix)
1846 if lst[:len(prefix)] != prefix:
1847 return None
1848 # f(['a, 'b', 'c', 'd'], ['a', 'b']) -> ['c', 'd']
1849 return lst[(len(prefix)):]
1850
1851 # root behavior:
1852 # --root=subdir , lstrips subdir from the header guard
1853 maybe_path = StripListPrefix(PathSplitToList(file_path_from_root),
1854 PathSplitToList(_root))
1855
1856 if _root_debug:
1857 sys.stderr.write(("_root lstrip (maybe_path=%s, file_path_from_root=%s," +
1858 " _root=%s)\n") %(maybe_path, file_path_from_root, _root))
1859
1860 if maybe_path:
1861 return os.path.join(*maybe_path)
1862
1863 # --root=.. , will prepend the outer directory to the header guard
1864 full_path = fileinfo.FullName()
1865 root_abspath = os.path.abspath(_root)
1866
1867 maybe_path = StripListPrefix(PathSplitToList(full_path),
1868 PathSplitToList(root_abspath))
1869
1870 if _root_debug:
1871 sys.stderr.write(("_root prepend (maybe_path=%s, full_path=%s, " +
1872 "root_abspath=%s)\n") %(maybe_path, full_path, root_abspath))
1873
1874 if maybe_path:
1875 return os.path.join(*maybe_path)
1876
1877 if _root_debug:
1878 sys.stderr.write("_root ignore, returning %s\n" %(file_path_from_root))
1879
1880 # --root=FAKE_DIR is ignored
1881 return file_path_from_root
1882
1883 file_path_from_root = FixupPathFromRoot()
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001884 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001885
1886
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001887def CheckForHeaderGuard(filename, clean_lines, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001888 """Checks that the file contains a header guard.
1889
erg@google.com6317a9c2009-06-25 00:28:19 +00001890 Logs an error if no #ifndef header guard is present. For other
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001891 headers, checks that the full pathname is used.
1892
1893 Args:
1894 filename: The name of the C++ header file.
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001895 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001896 error: The function to call with any errors found.
1897 """
1898
avakulenko@google.com59146752014-08-11 20:20:55 +00001899 # Don't check for header guards if there are error suppression
1900 # comments somewhere in this file.
1901 #
1902 # Because this is silencing a warning for a nonexistent line, we
1903 # only support the very specific NOLINT(build/header_guard) syntax,
1904 # and not the general NOLINT or NOLINT(*) syntax.
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001905 raw_lines = clean_lines.lines_without_raw_strings
1906 for i in raw_lines:
avakulenko@google.com59146752014-08-11 20:20:55 +00001907 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1908 return
1909
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001910 cppvar = GetHeaderGuardCPPVariable(filename)
1911
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001912 ifndef = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001913 ifndef_linenum = 0
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001914 define = ''
1915 endif = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001916 endif_linenum = 0
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001917 for linenum, line in enumerate(raw_lines):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001918 linesplit = line.split()
1919 if len(linesplit) >= 2:
1920 # find the first occurrence of #ifndef and #define, save arg
1921 if not ifndef and linesplit[0] == '#ifndef':
1922 # set ifndef to the header guard presented on the #ifndef line.
1923 ifndef = linesplit[1]
1924 ifndef_linenum = linenum
1925 if not define and linesplit[0] == '#define':
1926 define = linesplit[1]
1927 # find the last occurrence of #endif, save entire line
1928 if line.startswith('#endif'):
1929 endif = line
1930 endif_linenum = linenum
1931
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001932 if not ifndef or not define or ifndef != define:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001933 error(filename, 0, 'build/header_guard', 5,
1934 'No #ifndef header guard found, suggested CPP variable is: %s' %
1935 cppvar)
1936 return
1937
1938 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1939 # for backward compatibility.
erg@google.com35589e62010-11-17 18:58:16 +00001940 if ifndef != cppvar:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001941 error_level = 0
1942 if ifndef != cppvar + '_':
1943 error_level = 5
1944
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001945 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum,
erg@google.com35589e62010-11-17 18:58:16 +00001946 error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001947 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1948 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1949
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001950 # Check for "//" comments on endif line.
1951 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum,
1952 error)
1953 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif)
1954 if match:
1955 if match.group(1) == '_':
1956 # Issue low severity warning for deprecated double trailing underscore
1957 error(filename, endif_linenum, 'build/header_guard', 0,
1958 '#endif line should be "#endif // %s"' % cppvar)
erg@chromium.orgc452fea2012-01-26 21:10:45 +00001959 return
1960
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001961 # Didn't find the corresponding "//" comment. If this file does not
1962 # contain any "//" comments at all, it could be that the compiler
1963 # only wants "/**/" comments, look for those instead.
1964 no_single_line_comments = True
1965 for i in xrange(1, len(raw_lines) - 1):
1966 line = raw_lines[i]
1967 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
1968 no_single_line_comments = False
1969 break
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00001970
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001971 if no_single_line_comments:
1972 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif)
1973 if match:
1974 if match.group(1) == '_':
1975 # Low severity warning for double trailing underscore
1976 error(filename, endif_linenum, 'build/header_guard', 0,
1977 '#endif line should be "#endif /* %s */"' % cppvar)
1978 return
1979
1980 # Didn't find anything
1981 error(filename, endif_linenum, 'build/header_guard', 5,
1982 '#endif line should be "#endif // %s"' % cppvar)
1983
1984
1985def CheckHeaderFileIncluded(filename, include_state, error):
1986 """Logs an error if a .cc file does not include its header."""
1987
1988 # Do not check test files
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00001989 fileinfo = FileInfo(filename)
1990 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()):
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001991 return
1992
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00001993 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h'
avakulenko@google.com255f2be2014-12-05 22:19:55 +00001994 if not os.path.exists(headerfile):
1995 return
1996 headername = FileInfo(headerfile).RepositoryName()
1997 first_include = 0
1998 for section_list in include_state.include_list:
1999 for f in section_list:
2000 if headername in f[0] or f[0] in headername:
2001 return
2002 if not first_include:
2003 first_include = f[1]
2004
2005 error(filename, first_include, 'build/include', 5,
2006 '%s should include its header file %s' % (fileinfo.RepositoryName(),
2007 headername))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002008
2009
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002010def CheckForBadCharacters(filename, lines, error):
2011 """Logs an error for each line containing bad characters.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002012
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002013 Two kinds of bad characters:
2014
2015 1. Unicode replacement characters: These indicate that either the file
2016 contained invalid UTF-8 (likely) or Unicode replacement characters (which
2017 it shouldn't). Note that it's possible for this to throw off line
2018 numbering if the invalid UTF-8 occurred adjacent to a newline.
2019
2020 2. NUL bytes. These are problematic for some tools.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002021
2022 Args:
2023 filename: The name of the current file.
2024 lines: An array of strings, each representing a line of the file.
2025 error: The function to call with any errors found.
2026 """
2027 for linenum, line in enumerate(lines):
2028 if u'\ufffd' in line:
2029 error(filename, linenum, 'readability/utf8', 5,
2030 'Line contains invalid UTF-8 (or Unicode replacement character).')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002031 if '\0' in line:
2032 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002033
2034
2035def CheckForNewlineAtEOF(filename, lines, error):
2036 """Logs an error if there is no newline char at the end of the file.
2037
2038 Args:
2039 filename: The name of the current file.
2040 lines: An array of strings, each representing a line of the file.
2041 error: The function to call with any errors found.
2042 """
2043
2044 # The array lines() was created by adding two newlines to the
2045 # original file (go figure), then splitting on \n.
2046 # To verify that the file ends in \n, we just have to make sure the
2047 # last-but-two element of lines() exists and is empty.
2048 if len(lines) < 3 or lines[-2]:
2049 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
2050 'Could not find a newline character at the end of the file.')
2051
2052
2053def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
2054 """Logs an error if we see /* ... */ or "..." that extend past one line.
2055
2056 /* ... */ comments are legit inside macros, for one line.
2057 Otherwise, we prefer // comments, so it's ok to warn about the
2058 other. Likewise, it's ok for strings to extend across multiple
2059 lines, as long as a line continuation character (backslash)
2060 terminates each line. Although not currently prohibited by the C++
2061 style guide, it's ugly and unnecessary. We don't do well with either
2062 in this lint program, so we warn about both.
2063
2064 Args:
2065 filename: The name of the current file.
2066 clean_lines: A CleansedLines instance containing the file.
2067 linenum: The number of the line to check.
2068 error: The function to call with any errors found.
2069 """
2070 line = clean_lines.elided[linenum]
2071
2072 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
2073 # second (escaped) slash may trigger later \" detection erroneously.
2074 line = line.replace('\\\\', '')
2075
2076 if line.count('/*') > line.count('*/'):
2077 error(filename, linenum, 'readability/multiline_comment', 5,
2078 'Complex multi-line /*...*/-style comment found. '
2079 'Lint may give bogus warnings. '
2080 'Consider replacing these with //-style comments, '
2081 'with #if 0...#endif, '
2082 'or with more clearly structured multi-line comments.')
2083
2084 if (line.count('"') - line.count('\\"')) % 2:
2085 error(filename, linenum, 'readability/multiline_string', 5,
2086 'Multi-line string ("...") found. This lint script doesn\'t '
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002087 'do well with such strings, and may give bogus warnings. '
2088 'Use C++11 raw strings or concatenation instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002089
2090
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002091# (non-threadsafe name, thread-safe alternative, validation pattern)
2092#
2093# The validation pattern is used to eliminate false positives such as:
2094# _rand(); // false positive due to substring match.
2095# ->rand(); // some member function rand().
2096# ACMRandom rand(seed); // some variable named rand.
2097# ISAACRandom rand(); // another variable named rand.
2098#
2099# Basically we require the return value of these functions to be used
2100# in some expression context on the same line by matching on some
2101# operator before the function name. This eliminates constructors and
2102# member function calls.
2103_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
2104_THREADING_LIST = (
2105 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
2106 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
2107 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
2108 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
2109 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
2110 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
2111 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
2112 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
2113 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
2114 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
2115 ('strtok(', 'strtok_r(',
2116 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
2117 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002118 )
2119
2120
2121def CheckPosixThreading(filename, clean_lines, linenum, error):
2122 """Checks for calls to thread-unsafe functions.
2123
2124 Much code has been originally written without consideration of
2125 multi-threading. Also, engineers are relying on their old experience;
2126 they have learned posix before threading extensions were added. These
2127 tests guide the engineers to use thread-safe functions (when using
2128 posix directly).
2129
2130 Args:
2131 filename: The name of the current file.
2132 clean_lines: A CleansedLines instance containing the file.
2133 linenum: The number of the line to check.
2134 error: The function to call with any errors found.
2135 """
2136 line = clean_lines.elided[linenum]
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002137 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
2138 # Additional pattern matching check to confirm that this is the
2139 # function we are looking for
2140 if Search(pattern, line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002141 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002142 'Consider using ' + multithread_safe_func +
2143 '...) instead of ' + single_thread_func +
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002144 '...) for improved thread safety.')
2145
2146
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002147def CheckVlogArguments(filename, clean_lines, linenum, error):
2148 """Checks that VLOG() is only used for defining a logging level.
2149
2150 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
2151 VLOG(FATAL) are not.
2152
2153 Args:
2154 filename: The name of the current file.
2155 clean_lines: A CleansedLines instance containing the file.
2156 linenum: The number of the line to check.
2157 error: The function to call with any errors found.
2158 """
2159 line = clean_lines.elided[linenum]
2160 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
2161 error(filename, linenum, 'runtime/vlog', 5,
2162 'VLOG() should be used with numeric verbosity level. '
2163 'Use LOG() if you want symbolic severity levels.')
2164
erg@google.com26970fa2009-11-17 18:07:32 +00002165# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com6317a9c2009-06-25 00:28:19 +00002166# incrementing a value.
erg@google.com26970fa2009-11-17 18:07:32 +00002167_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com6317a9c2009-06-25 00:28:19 +00002168 r'^\s*\*\w+(\+\+|--);')
2169
2170
2171def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.com26970fa2009-11-17 18:07:32 +00002172 """Checks for invalid increment *count++.
erg@google.com6317a9c2009-06-25 00:28:19 +00002173
2174 For example following function:
2175 void increment_counter(int* count) {
2176 *count++;
2177 }
2178 is invalid, because it effectively does count++, moving pointer, and should
2179 be replaced with ++*count, (*count)++ or *count += 1.
2180
2181 Args:
2182 filename: The name of the current file.
2183 clean_lines: A CleansedLines instance containing the file.
2184 linenum: The number of the line to check.
2185 error: The function to call with any errors found.
2186 """
2187 line = clean_lines.elided[linenum]
erg@google.com26970fa2009-11-17 18:07:32 +00002188 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com6317a9c2009-06-25 00:28:19 +00002189 error(filename, linenum, 'runtime/invalid_increment', 5,
2190 'Changing pointer instead of value (or unused value of operator*).')
2191
2192
avakulenko@google.com59146752014-08-11 20:20:55 +00002193def IsMacroDefinition(clean_lines, linenum):
2194 if Search(r'^#define', clean_lines[linenum]):
2195 return True
2196
2197 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
2198 return True
2199
2200 return False
2201
2202
2203def IsForwardClassDeclaration(clean_lines, linenum):
2204 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
2205
2206
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002207class _BlockInfo(object):
2208 """Stores information about a generic block of code."""
2209
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002210 def __init__(self, linenum, seen_open_brace):
2211 self.starting_linenum = linenum
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002212 self.seen_open_brace = seen_open_brace
2213 self.open_parentheses = 0
2214 self.inline_asm = _NO_ASM
avakulenko@google.com59146752014-08-11 20:20:55 +00002215 self.check_namespace_indentation = False
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002216
2217 def CheckBegin(self, filename, clean_lines, linenum, error):
2218 """Run checks that applies to text up to the opening brace.
2219
2220 This is mostly for checking the text after the class identifier
2221 and the "{", usually where the base class is specified. For other
2222 blocks, there isn't much to check, so we always pass.
2223
2224 Args:
2225 filename: The name of the current file.
2226 clean_lines: A CleansedLines instance containing the file.
2227 linenum: The number of the line to check.
2228 error: The function to call with any errors found.
2229 """
2230 pass
2231
2232 def CheckEnd(self, filename, clean_lines, linenum, error):
2233 """Run checks that applies to text after the closing brace.
2234
2235 This is mostly used for checking end of namespace comments.
2236
2237 Args:
2238 filename: The name of the current file.
2239 clean_lines: A CleansedLines instance containing the file.
2240 linenum: The number of the line to check.
2241 error: The function to call with any errors found.
2242 """
2243 pass
2244
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002245 def IsBlockInfo(self):
2246 """Returns true if this block is a _BlockInfo.
2247
2248 This is convenient for verifying that an object is an instance of
2249 a _BlockInfo, but not an instance of any of the derived classes.
2250
2251 Returns:
2252 True for this class, False for derived classes.
2253 """
2254 return self.__class__ == _BlockInfo
2255
2256
2257class _ExternCInfo(_BlockInfo):
2258 """Stores information about an 'extern "C"' block."""
2259
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002260 def __init__(self, linenum):
2261 _BlockInfo.__init__(self, linenum, True)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002262
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002263
2264class _ClassInfo(_BlockInfo):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002265 """Stores information about a class."""
2266
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002267 def __init__(self, name, class_or_struct, clean_lines, linenum):
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002268 _BlockInfo.__init__(self, linenum, False)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002269 self.name = name
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002270 self.is_derived = False
avakulenko@google.com59146752014-08-11 20:20:55 +00002271 self.check_namespace_indentation = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002272 if class_or_struct == 'struct':
2273 self.access = 'public'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002274 self.is_struct = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002275 else:
2276 self.access = 'private'
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002277 self.is_struct = False
2278
2279 # Remember initial indentation level for this class. Using raw_lines here
2280 # instead of elided to account for leading comments.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002281 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002282
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002283 # Try to find the end of the class. This will be confused by things like:
2284 # class A {
2285 # } *x = { ...
2286 #
2287 # But it's still good enough for CheckSectionSpacing.
2288 self.last_line = 0
2289 depth = 0
2290 for i in range(linenum, clean_lines.NumLines()):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002291 line = clean_lines.elided[i]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00002292 depth += line.count('{') - line.count('}')
2293 if not depth:
2294 self.last_line = i
2295 break
2296
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002297 def CheckBegin(self, filename, clean_lines, linenum, error):
2298 # Look for a bare ':'
2299 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2300 self.is_derived = True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002301
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002302 def CheckEnd(self, filename, clean_lines, linenum, error):
avakulenko@google.com255f2be2014-12-05 22:19:55 +00002303 # If there is a DISALLOW macro, it should appear near the end of
2304 # the class.
2305 seen_last_thing_in_class = False
2306 for i in xrange(linenum - 1, self.starting_linenum, -1):
2307 match = Search(
2308 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
2309 self.name + r'\)',
2310 clean_lines.elided[i])
2311 if match:
2312 if seen_last_thing_in_class:
2313 error(filename, i, 'readability/constructors', 3,
2314 match.group(1) + ' should be the last thing in the class')
2315 break
2316
2317 if not Match(r'^\s*$', clean_lines.elided[i]):
2318 seen_last_thing_in_class = True
2319
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002320 # Check that closing brace is aligned with beginning of the class.
2321 # Only do this if the closing brace is indented by only whitespaces.
2322 # This means we will not check single-line class definitions.
2323 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2324 if indent and len(indent.group(1)) != self.class_indent:
2325 if self.is_struct:
2326 parent = 'struct ' + self.name
2327 else:
2328 parent = 'class ' + self.name
2329 error(filename, linenum, 'whitespace/indent', 3,
2330 'Closing brace should be aligned with beginning of %s' % parent)
2331
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002332
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002333class _NamespaceInfo(_BlockInfo):
2334 """Stores information about a namespace."""
2335
2336 def __init__(self, name, linenum):
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002337 _BlockInfo.__init__(self, linenum, False)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002338 self.name = name or ''
avakulenko@google.com59146752014-08-11 20:20:55 +00002339 self.check_namespace_indentation = True
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002340
2341 def CheckEnd(self, filename, clean_lines, linenum, error):
2342 """Check end of namespace comments."""
2343 line = clean_lines.raw_lines[linenum]
2344
2345 # Check how many lines is enclosed in this namespace. Don't issue
2346 # warning for missing namespace comments if there aren't enough
2347 # lines. However, do apply checks if there is already an end of
2348 # namespace comment and it's incorrect.
2349 #
2350 # TODO(unknown): We always want to check end of namespace comments
2351 # if a namespace is large, but sometimes we also want to apply the
2352 # check if a short namespace contained nontrivial things (something
2353 # other than forward declarations). There is currently no logic on
2354 # deciding what these nontrivial things are, so this check is
2355 # triggered by namespace size only, which works most of the time.
2356 if (linenum - self.starting_linenum < 10
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002357 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002358 return
2359
2360 # Look for matching comment at end of namespace.
2361 #
2362 # Note that we accept C style "/* */" comments for terminating
2363 # namespaces, so that code that terminate namespaces inside
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002364 # preprocessor macros can be cpplint clean.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002365 #
2366 # We also accept stuff like "// end of namespace <name>." with the
2367 # period at the end.
2368 #
2369 # Besides these, we don't accept anything else, otherwise we might
2370 # get false negatives when existing comment is a substring of the
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002371 # expected namespace.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002372 if self.name:
2373 # Named namespace
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002374 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
2375 re.escape(self.name) + r'[\*/\.\\\s]*$'),
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002376 line):
2377 error(filename, linenum, 'readability/namespace', 5,
2378 'Namespace should be terminated with "// namespace %s"' %
2379 self.name)
2380 else:
2381 # Anonymous namespace
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002382 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002383 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2384 # mention "// anonymous namespace" as an acceptable form
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002385 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002386 error(filename, linenum, 'readability/namespace', 5,
2387 'Anonymous namespace should be terminated with "// namespace"'
2388 ' or "// anonymous namespace"')
2389 else:
2390 error(filename, linenum, 'readability/namespace', 5,
2391 'Anonymous namespace should be terminated with "// namespace"')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002392
2393
2394class _PreprocessorInfo(object):
2395 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2396
2397 def __init__(self, stack_before_if):
2398 # The entire nesting stack before #if
2399 self.stack_before_if = stack_before_if
2400
2401 # The entire nesting stack up to #else
2402 self.stack_before_else = []
2403
2404 # Whether we have already seen #else or #elif
2405 self.seen_else = False
2406
2407
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002408class NestingState(object):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002409 """Holds states related to parsing braces."""
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002410
2411 def __init__(self):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002412 # Stack for tracking all braces. An object is pushed whenever we
2413 # see a "{", and popped when we see a "}". Only 3 types of
2414 # objects are possible:
2415 # - _ClassInfo: a class or struct.
2416 # - _NamespaceInfo: a namespace.
2417 # - _BlockInfo: some other type of block.
2418 self.stack = []
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002419
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002420 # Top of the previous stack before each Update().
2421 #
2422 # Because the nesting_stack is updated at the end of each line, we
2423 # had to do some convoluted checks to find out what is the current
2424 # scope at the beginning of the line. This check is simplified by
2425 # saving the previous top of nesting stack.
2426 #
2427 # We could save the full stack, but we only need the top. Copying
2428 # the full nesting stack would slow down cpplint by ~10%.
2429 self.previous_stack_top = []
2430
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002431 # Stack of _PreprocessorInfo objects.
2432 self.pp_stack = []
2433
2434 def SeenOpenBrace(self):
2435 """Check if we have seen the opening brace for the innermost block.
2436
2437 Returns:
2438 True if we have seen the opening brace, False if the innermost
2439 block is still expecting an opening brace.
2440 """
2441 return (not self.stack) or self.stack[-1].seen_open_brace
2442
2443 def InNamespaceBody(self):
2444 """Check if we are currently one level inside a namespace body.
2445
2446 Returns:
2447 True if top of the stack is a namespace block, False otherwise.
2448 """
2449 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2450
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002451 def InExternC(self):
2452 """Check if we are currently one level inside an 'extern "C"' block.
2453
2454 Returns:
2455 True if top of the stack is an extern block, False otherwise.
2456 """
2457 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2458
2459 def InClassDeclaration(self):
2460 """Check if we are currently one level inside a class or struct declaration.
2461
2462 Returns:
2463 True if top of the stack is a class/struct, False otherwise.
2464 """
2465 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2466
2467 def InAsmBlock(self):
2468 """Check if we are currently one level inside an inline ASM block.
2469
2470 Returns:
2471 True if the top of the stack is a block containing inline ASM.
2472 """
2473 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2474
2475 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2476 """Check if current position is inside template argument list.
2477
2478 Args:
2479 clean_lines: A CleansedLines instance containing the file.
2480 linenum: The number of the line to check.
2481 pos: position just after the suspected template argument.
2482 Returns:
2483 True if (linenum, pos) is inside template arguments.
2484 """
2485 while linenum < clean_lines.NumLines():
2486 # Find the earliest character that might indicate a template argument
2487 line = clean_lines.elided[linenum]
2488 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2489 if not match:
2490 linenum += 1
2491 pos = 0
2492 continue
2493 token = match.group(1)
2494 pos += len(match.group(0))
2495
2496 # These things do not look like template argument list:
2497 # class Suspect {
2498 # class Suspect x; }
2499 if token in ('{', '}', ';'): return False
2500
2501 # These things look like template argument list:
2502 # template <class Suspect>
2503 # template <class Suspect = default_value>
2504 # template <class Suspect[]>
2505 # template <class Suspect...>
2506 if token in ('>', '=', '[', ']', '.'): return True
2507
2508 # Check if token is an unmatched '<'.
2509 # If not, move on to the next character.
2510 if token != '<':
2511 pos += 1
2512 if pos >= len(line):
2513 linenum += 1
2514 pos = 0
2515 continue
2516
2517 # We can't be sure if we just find a single '<', and need to
2518 # find the matching '>'.
2519 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2520 if end_pos < 0:
2521 # Not sure if template argument list or syntax error in file
2522 return False
2523 linenum = end_line
2524 pos = end_pos
2525 return False
2526
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002527 def UpdatePreprocessor(self, line):
2528 """Update preprocessor stack.
2529
2530 We need to handle preprocessors due to classes like this:
2531 #ifdef SWIG
2532 struct ResultDetailsPageElementExtensionPoint {
2533 #else
2534 struct ResultDetailsPageElementExtensionPoint : public Extension {
2535 #endif
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002536
2537 We make the following assumptions (good enough for most files):
2538 - Preprocessor condition evaluates to true from #if up to first
2539 #else/#elif/#endif.
2540
2541 - Preprocessor condition evaluates to false from #else/#elif up
2542 to #endif. We still perform lint checks on these lines, but
2543 these do not affect nesting stack.
2544
2545 Args:
2546 line: current line to check.
2547 """
2548 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2549 # Beginning of #if block, save the nesting stack here. The saved
2550 # stack will allow us to restore the parsing state in the #else case.
2551 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2552 elif Match(r'^\s*#\s*(else|elif)\b', line):
2553 # Beginning of #else block
2554 if self.pp_stack:
2555 if not self.pp_stack[-1].seen_else:
2556 # This is the first #else or #elif block. Remember the
2557 # whole nesting stack up to this point. This is what we
2558 # keep after the #endif.
2559 self.pp_stack[-1].seen_else = True
2560 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2561
2562 # Restore the stack to how it was before the #if
2563 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2564 else:
2565 # TODO(unknown): unexpected #else, issue warning?
2566 pass
2567 elif Match(r'^\s*#\s*endif\b', line):
2568 # End of #if or #else blocks.
2569 if self.pp_stack:
2570 # If we saw an #else, we will need to restore the nesting
2571 # stack to its former state before the #else, otherwise we
2572 # will just continue from where we left off.
2573 if self.pp_stack[-1].seen_else:
2574 # Here we can just use a shallow copy since we are the last
2575 # reference to it.
2576 self.stack = self.pp_stack[-1].stack_before_else
2577 # Drop the corresponding #if
2578 self.pp_stack.pop()
2579 else:
2580 # TODO(unknown): unexpected #endif, issue warning?
2581 pass
2582
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002583 # TODO(unknown): Update() is too long, but we will refactor later.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002584 def Update(self, filename, clean_lines, linenum, error):
2585 """Update nesting state with current line.
2586
2587 Args:
2588 filename: The name of the current file.
2589 clean_lines: A CleansedLines instance containing the file.
2590 linenum: The number of the line to check.
2591 error: The function to call with any errors found.
2592 """
2593 line = clean_lines.elided[linenum]
2594
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002595 # Remember top of the previous nesting stack.
2596 #
2597 # The stack is always pushed/popped and not modified in place, so
2598 # we can just do a shallow copy instead of copy.deepcopy. Using
2599 # deepcopy would slow down cpplint by ~28%.
2600 if self.stack:
2601 self.previous_stack_top = self.stack[-1]
2602 else:
2603 self.previous_stack_top = None
2604
2605 # Update pp_stack
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002606 self.UpdatePreprocessor(line)
2607
2608 # Count parentheses. This is to avoid adding struct arguments to
2609 # the nesting stack.
2610 if self.stack:
2611 inner_block = self.stack[-1]
2612 depth_change = line.count('(') - line.count(')')
2613 inner_block.open_parentheses += depth_change
2614
2615 # Also check if we are starting or ending an inline assembly block.
2616 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2617 if (depth_change != 0 and
2618 inner_block.open_parentheses == 1 and
2619 _MATCH_ASM.match(line)):
2620 # Enter assembly block
2621 inner_block.inline_asm = _INSIDE_ASM
2622 else:
2623 # Not entering assembly block. If previous line was _END_ASM,
2624 # we will now shift to _NO_ASM state.
2625 inner_block.inline_asm = _NO_ASM
2626 elif (inner_block.inline_asm == _INSIDE_ASM and
2627 inner_block.open_parentheses == 0):
2628 # Exit assembly block
2629 inner_block.inline_asm = _END_ASM
2630
2631 # Consume namespace declaration at the beginning of the line. Do
2632 # this in a loop so that we catch same line declarations like this:
2633 # namespace proto2 { namespace bridge { class MessageSet; } }
2634 while True:
2635 # Match start of namespace. The "\b\s*" below catches namespace
2636 # declarations even if it weren't followed by a whitespace, this
2637 # is so that we don't confuse our namespace checker. The
2638 # missing spaces will be flagged by CheckSpacing.
2639 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2640 if not namespace_decl_match:
2641 break
2642
2643 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2644 self.stack.append(new_namespace)
2645
2646 line = namespace_decl_match.group(2)
2647 if line.find('{') != -1:
2648 new_namespace.seen_open_brace = True
2649 line = line[line.find('{') + 1:]
2650
2651 # Look for a class declaration in whatever is left of the line
2652 # after parsing namespaces. The regexp accounts for decorated classes
2653 # such as in:
2654 # class LOCKABLE API Object {
2655 # };
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002656 class_decl_match = Match(
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002657 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2658 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2659 r'(.*)$', line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002660 if (class_decl_match and
2661 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002662 # We do not want to accept classes that are actually template arguments:
2663 # template <class Ignore1,
2664 # class Ignore2 = Default<Args>,
2665 # template <Args> class Ignore3>
2666 # void Function() {};
2667 #
2668 # To avoid template argument cases, we scan forward and look for
2669 # an unmatched '>'. If we see one, assume we are inside a
2670 # template argument list.
2671 end_declaration = len(class_decl_match.group(1))
2672 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2673 self.stack.append(_ClassInfo(
2674 class_decl_match.group(3), class_decl_match.group(2),
2675 clean_lines, linenum))
2676 line = class_decl_match.group(4)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002677
2678 # If we have not yet seen the opening brace for the innermost block,
2679 # run checks here.
2680 if not self.SeenOpenBrace():
2681 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2682
2683 # Update access control if we are inside a class/struct
2684 if self.stack and isinstance(self.stack[-1], _ClassInfo):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002685 classinfo = self.stack[-1]
2686 access_match = Match(
2687 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2688 r':(?:[^:]|$)',
2689 line)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002690 if access_match:
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002691 classinfo.access = access_match.group(2)
2692
2693 # Check that access keywords are indented +1 space. Skip this
2694 # check if the keywords are not preceded by whitespaces.
2695 indent = access_match.group(1)
2696 if (len(indent) != classinfo.class_indent + 1 and
2697 Match(r'^\s*$', indent)):
2698 if classinfo.is_struct:
2699 parent = 'struct ' + classinfo.name
2700 else:
2701 parent = 'class ' + classinfo.name
2702 slots = ''
2703 if access_match.group(3):
2704 slots = access_match.group(3)
2705 error(filename, linenum, 'whitespace/indent', 3,
2706 '%s%s: should be indented +1 space inside %s' % (
2707 access_match.group(2), slots, parent))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002708
2709 # Consume braces or semicolons from what's left of the line
2710 while True:
2711 # Match first brace, semicolon, or closed parenthesis.
2712 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2713 if not matched:
2714 break
2715
2716 token = matched.group(1)
2717 if token == '{':
2718 # If namespace or class hasn't seen a opening brace yet, mark
2719 # namespace/class head as complete. Push a new block onto the
2720 # stack otherwise.
2721 if not self.SeenOpenBrace():
2722 self.stack[-1].seen_open_brace = True
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002723 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002724 self.stack.append(_ExternCInfo(linenum))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002725 else:
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002726 self.stack.append(_BlockInfo(linenum, True))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002727 if _MATCH_ASM.match(line):
2728 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002729
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002730 elif token == ';' or token == ')':
2731 # If we haven't seen an opening brace yet, but we already saw
2732 # a semicolon, this is probably a forward declaration. Pop
2733 # the stack for these.
2734 #
2735 # Similarly, if we haven't seen an opening brace yet, but we
2736 # already saw a closing parenthesis, then these are probably
2737 # function arguments with extra "class" or "struct" keywords.
2738 # Also pop these stack for these.
2739 if not self.SeenOpenBrace():
2740 self.stack.pop()
2741 else: # token == '}'
2742 # Perform end of block checks and pop the stack.
2743 if self.stack:
2744 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2745 self.stack.pop()
2746 line = matched.group(2)
2747
2748 def InnermostClass(self):
2749 """Get class info on the top of the stack.
2750
2751 Returns:
2752 A _ClassInfo object if we are inside a class, or None otherwise.
2753 """
2754 for i in range(len(self.stack), 0, -1):
2755 classinfo = self.stack[i - 1]
2756 if isinstance(classinfo, _ClassInfo):
2757 return classinfo
2758 return None
2759
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002760 def CheckCompletedBlocks(self, filename, error):
2761 """Checks that all classes and namespaces have been completely parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002762
2763 Call this when all lines in a file have been processed.
2764 Args:
2765 filename: The name of the current file.
2766 error: The function to call with any errors found.
2767 """
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002768 # Note: This test can result in false positives if #ifdef constructs
2769 # get in the way of brace matching. See the testBuildClass test in
2770 # cpplint_unittest.py for an example of this.
2771 for obj in self.stack:
2772 if isinstance(obj, _ClassInfo):
2773 error(filename, obj.starting_linenum, 'build/class', 5,
2774 'Failed to find complete declaration of class %s' %
2775 obj.name)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002776 elif isinstance(obj, _NamespaceInfo):
2777 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2778 'Failed to find complete declaration of namespace %s' %
2779 obj.name)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002780
2781
2782def CheckForNonStandardConstructs(filename, clean_lines, linenum,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002783 nesting_state, error):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002784 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002785
2786 Complain about several constructs which gcc-2 accepts, but which are
2787 not standard C++. Warning about these in lint is one way to ease the
2788 transition to new compilers.
2789 - put storage class first (e.g. "static const" instead of "const static").
2790 - "%lld" instead of %qd" in printf-type functions.
2791 - "%1$d" is non-standard in printf-type functions.
2792 - "\%" is an undefined character escape sequence.
2793 - text after #endif is not allowed.
2794 - invalid inner-style forward declaration.
2795 - >? and <? operators, and their >?= and <?= cousins.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002796
erg@google.com26970fa2009-11-17 18:07:32 +00002797 Additionally, check for constructor/destructor style violations and reference
2798 members, as it is very convenient to do so while checking for
2799 gcc-2 compliance.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002800
2801 Args:
2802 filename: The name of the current file.
2803 clean_lines: A CleansedLines instance containing the file.
2804 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002805 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002806 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002807 error: A callable to which errors are reported, which takes 4 arguments:
2808 filename, line number, error level, and message
2809 """
2810
2811 # Remove comments from the line, but leave in strings for now.
2812 line = clean_lines.lines[linenum]
2813
2814 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2815 error(filename, linenum, 'runtime/printf_format', 3,
2816 '%q in format strings is deprecated. Use %ll instead.')
2817
2818 if Search(r'printf\s*\(.*".*%\d+\$', line):
2819 error(filename, linenum, 'runtime/printf_format', 2,
2820 '%N$ formats are unconventional. Try rewriting to avoid them.')
2821
2822 # Remove escaped backslashes before looking for undefined escapes.
2823 line = line.replace('\\\\', '')
2824
2825 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2826 error(filename, linenum, 'build/printf_format', 3,
2827 '%, [, (, and { are undefined character escapes. Unescape them.')
2828
2829 # For the rest, work with both comments and strings removed.
2830 line = clean_lines.elided[linenum]
2831
2832 if Search(r'\b(const|volatile|void|char|short|int|long'
2833 r'|float|double|signed|unsigned'
2834 r'|schar|u?int8|u?int16|u?int32|u?int64)'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002835 r'\s+(register|static|extern|typedef)\b',
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002836 line):
2837 error(filename, linenum, 'build/storage_class', 5,
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002838 'Storage-class specifier (static, extern, typedef, etc) should be '
2839 'at the beginning of the declaration.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002840
2841 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2842 error(filename, linenum, 'build/endif_comment', 5,
2843 'Uncommented text after #endif is non-standard. Use a comment.')
2844
2845 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2846 error(filename, linenum, 'build/forward_decl', 5,
2847 'Inner-style forward declarations are invalid. Remove this line.')
2848
2849 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2850 line):
2851 error(filename, linenum, 'build/deprecated', 3,
2852 '>? and <? (max and min) operators are non-standard and deprecated.')
2853
erg@google.com26970fa2009-11-17 18:07:32 +00002854 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2855 # TODO(unknown): Could it be expanded safely to arbitrary references,
2856 # without triggering too many false positives? The first
2857 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2858 # the restriction.
2859 # Here's the original regexp, for the reference:
2860 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2861 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2862 error(filename, linenum, 'runtime/member_string_references', 2,
2863 'const string& members are dangerous. It is much better to use '
2864 'alternatives, such as pointers or simple constants.')
2865
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00002866 # Everything else in this function operates on class declarations.
2867 # Return early if the top of the nesting stack is not a class, or if
2868 # the class head is not completed yet.
2869 classinfo = nesting_state.InnermostClass()
2870 if not classinfo or not classinfo.seen_open_brace:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002871 return
2872
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002873 # The class may have been declared with namespace or classname qualifiers.
2874 # The constructor and destructor will not have those qualifiers.
2875 base_classname = classinfo.name.split('::')[-1]
2876
2877 # Look for single-argument constructors that aren't marked explicit.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002878 # Technically a valid construct, but against style.
avakulenko@google.com59146752014-08-11 20:20:55 +00002879 explicit_constructor_match = Match(
danakjd7f56752017-02-22 11:45:06 -05002880 r'\s+(?:(?:inline|constexpr)\s+)*(explicit\s+)?'
2881 r'(?:(?:inline|constexpr)\s+)*%s\s*'
avakulenko@google.com59146752014-08-11 20:20:55 +00002882 r'\(((?:[^()]|\([^()]*\))*)\)'
2883 % re.escape(base_classname),
2884 line)
2885
2886 if explicit_constructor_match:
2887 is_marked_explicit = explicit_constructor_match.group(1)
2888
2889 if not explicit_constructor_match.group(2):
2890 constructor_args = []
2891 else:
2892 constructor_args = explicit_constructor_match.group(2).split(',')
2893
2894 # collapse arguments so that commas in template parameter lists and function
2895 # argument parameter lists don't split arguments in two
2896 i = 0
2897 while i < len(constructor_args):
2898 constructor_arg = constructor_args[i]
2899 while (constructor_arg.count('<') > constructor_arg.count('>') or
2900 constructor_arg.count('(') > constructor_arg.count(')')):
2901 constructor_arg += ',' + constructor_args[i + 1]
2902 del constructor_args[i + 1]
2903 constructor_args[i] = constructor_arg
2904 i += 1
2905
2906 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2907 noarg_constructor = (not constructor_args or # empty arg list
2908 # 'void' arg specifier
2909 (len(constructor_args) == 1 and
2910 constructor_args[0].strip() == 'void'))
2911 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2912 not noarg_constructor) or
2913 # all but at most one arg defaulted
2914 (len(constructor_args) >= 1 and
2915 not noarg_constructor and
2916 len(defaulted_args) >= len(constructor_args) - 1))
2917 initializer_list_constructor = bool(
2918 onearg_constructor and
2919 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2920 copy_constructor = bool(
2921 onearg_constructor and
2922 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2923 % re.escape(base_classname), constructor_args[0].strip()))
2924
2925 if (not is_marked_explicit and
2926 onearg_constructor and
2927 not initializer_list_constructor and
2928 not copy_constructor):
2929 if defaulted_args:
2930 error(filename, linenum, 'runtime/explicit', 5,
2931 'Constructors callable with one argument '
2932 'should be marked explicit.')
2933 else:
2934 error(filename, linenum, 'runtime/explicit', 5,
2935 'Single-parameter constructors should be marked explicit.')
2936 elif is_marked_explicit and not onearg_constructor:
2937 if noarg_constructor:
2938 error(filename, linenum, 'runtime/explicit', 5,
2939 'Zero-parameter constructors should not be marked explicit.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002940
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002941
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002942def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002943 """Checks for the correctness of various spacing around function calls.
2944
2945 Args:
2946 filename: The name of the current file.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002947 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002948 linenum: The number of the line to check.
2949 error: The function to call with any errors found.
2950 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002951 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002952
2953 # Since function calls often occur inside if/for/while/switch
2954 # expressions - which have their own, more liberal conventions - we
2955 # first see if we should be looking inside such an expression for a
2956 # function call, to which we can apply more strict standards.
2957 fncall = line # if there's no control flow construct, look at whole line
2958 for pattern in (r'\bif\s*\((.*)\)\s*{',
2959 r'\bfor\s*\((.*)\)\s*{',
2960 r'\bwhile\s*\((.*)\)\s*[{;]',
2961 r'\bswitch\s*\((.*)\)\s*{'):
2962 match = Search(pattern, line)
2963 if match:
2964 fncall = match.group(1) # look inside the parens for function calls
2965 break
2966
2967 # Except in if/for/while/switch, there should never be space
2968 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2969 # for nested parens ( (a+b) + c ). Likewise, there should never be
2970 # a space before a ( when it's a function argument. I assume it's a
2971 # function argument when the char before the whitespace is legal in
2972 # a function name (alnum + _) and we're not starting a macro. Also ignore
2973 # pointers and references to arrays and functions coz they're too tricky:
2974 # we use a very simple way to recognize these:
2975 # " (something)(maybe-something)" or
2976 # " (something)(maybe-something," or
2977 # " (something)[something]"
2978 # Note that we assume the contents of [] to be short enough that
2979 # they'll never need to wrap.
2980 if ( # Ignore control structures.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00002981 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
2982 fncall) and
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002983 # Ignore pointers/references to functions.
2984 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2985 # Ignore pointers/references to arrays.
2986 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com6317a9c2009-06-25 00:28:19 +00002987 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002988 error(filename, linenum, 'whitespace/parens', 4,
2989 'Extra space after ( in function call')
erg@google.com6317a9c2009-06-25 00:28:19 +00002990 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00002991 error(filename, linenum, 'whitespace/parens', 2,
2992 'Extra space after (')
2993 if (Search(r'\w\s+\(', fncall) and
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00002994 not Search(r'_{0,2}asm_{0,2}\s+_{0,2}volatile_{0,2}\s+\(', fncall) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002995 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
avakulenko@google.com255f2be2014-12-05 22:19:55 +00002996 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and
2997 not Search(r'\bcase\s+\(', fncall)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00002998 # TODO(unknown): Space after an operator function seem to be a common
2999 # error, silence those for now by restricting them to highest verbosity.
3000 if Search(r'\boperator_*\b', line):
3001 error(filename, linenum, 'whitespace/parens', 0,
3002 'Extra space before ( in function call')
3003 else:
3004 error(filename, linenum, 'whitespace/parens', 4,
3005 'Extra space before ( in function call')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003006 # If the ) is followed only by a newline or a { + newline, assume it's
3007 # part of a control statement (if/while/etc), and don't complain
3008 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003009 # If the closing parenthesis is preceded by only whitespaces,
3010 # try to give a more descriptive error message.
3011 if Search(r'^\s+\)', fncall):
3012 error(filename, linenum, 'whitespace/parens', 2,
3013 'Closing ) should be moved to the previous line')
3014 else:
3015 error(filename, linenum, 'whitespace/parens', 2,
3016 'Extra space before )')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003017
3018
3019def IsBlankLine(line):
3020 """Returns true if the given line is blank.
3021
3022 We consider a line to be blank if the line is empty or consists of
3023 only white spaces.
3024
3025 Args:
3026 line: A line of a string.
3027
3028 Returns:
3029 True, if the given line is blank.
3030 """
3031 return not line or line.isspace()
3032
3033
avakulenko@google.com59146752014-08-11 20:20:55 +00003034def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
3035 error):
3036 is_namespace_indent_item = (
3037 len(nesting_state.stack) > 1 and
3038 nesting_state.stack[-1].check_namespace_indentation and
3039 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
3040 nesting_state.previous_stack_top == nesting_state.stack[-2])
3041
3042 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
3043 clean_lines.elided, line):
3044 CheckItemIndentationInNamespace(filename, clean_lines.elided,
3045 line, error)
3046
3047
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003048def CheckForFunctionLengths(filename, clean_lines, linenum,
3049 function_state, error):
3050 """Reports for long function bodies.
3051
3052 For an overview why this is done, see:
Alexandr Ilinff294c32017-04-27 15:57:40 +02003053 https://google.github.io/styleguide/cppguide.html#Write_Short_Functions
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003054
3055 Uses a simplistic algorithm assuming other style guidelines
3056 (especially spacing) are followed.
3057 Only checks unindented functions, so class members are unchecked.
3058 Trivial bodies are unchecked, so constructors with huge initializer lists
3059 may be missed.
3060 Blank/comment lines are not counted so as to avoid encouraging the removal
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003061 of vertical space and comments just to get through a lint check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003062 NOLINT *on the last line of a function* disables this check.
3063
3064 Args:
3065 filename: The name of the current file.
3066 clean_lines: A CleansedLines instance containing the file.
3067 linenum: The number of the line to check.
3068 function_state: Current function name and lines in body so far.
3069 error: The function to call with any errors found.
3070 """
3071 lines = clean_lines.lines
3072 line = lines[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003073 joined_line = ''
3074
3075 starting_func = False
erg@google.com6317a9c2009-06-25 00:28:19 +00003076 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003077 match_result = Match(regexp, line)
3078 if match_result:
3079 # If the name is all caps and underscores, figure it's a macro and
3080 # ignore it, unless it's TEST or TEST_F.
3081 function_name = match_result.group(1).split()[-1]
3082 if function_name == 'TEST' or function_name == 'TEST_F' or (
3083 not Match(r'[A-Z_]+$', function_name)):
3084 starting_func = True
3085
3086 if starting_func:
3087 body_found = False
erg@google.com6317a9c2009-06-25 00:28:19 +00003088 for start_linenum in xrange(linenum, clean_lines.NumLines()):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003089 start_line = lines[start_linenum]
3090 joined_line += ' ' + start_line.lstrip()
3091 if Search(r'(;|})', start_line): # Declarations and trivial functions
3092 body_found = True
3093 break # ... ignore
3094 elif Search(r'{', start_line):
3095 body_found = True
3096 function = Search(r'((\w|:)*)\(', line).group(1)
3097 if Match(r'TEST', function): # Handle TEST... macros
3098 parameter_regexp = Search(r'(\(.*\))', joined_line)
3099 if parameter_regexp: # Ignore bad syntax
3100 function += parameter_regexp.group(1)
3101 else:
3102 function += '()'
3103 function_state.Begin(function)
3104 break
3105 if not body_found:
erg@google.com6317a9c2009-06-25 00:28:19 +00003106 # No body for the function (or evidence of a non-function) was found.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003107 error(filename, linenum, 'readability/fn_size', 5,
3108 'Lint failed to find start of function body.')
3109 elif Match(r'^\}\s*$', line): # function end
erg@google.com35589e62010-11-17 18:58:16 +00003110 function_state.Check(error, filename, linenum)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003111 function_state.End()
3112 elif not Match(r'^\s*$', line):
3113 function_state.Count() # Count non-blank/non-comment lines.
3114
3115
3116_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
3117
3118
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003119def CheckComment(line, filename, linenum, next_line_start, error):
3120 """Checks for common mistakes in comments.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003121
3122 Args:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003123 line: The line in question.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003124 filename: The name of the current file.
3125 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003126 next_line_start: The first non-whitespace column of the next line.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003127 error: The function to call with any errors found.
3128 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003129 commentpos = line.find('//')
3130 if commentpos != -1:
3131 # Check if the // may be in quotes. If so, ignore it
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003132 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003133 # Allow one space for new scopes, two spaces otherwise:
3134 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
3135 ((commentpos >= 1 and
3136 line[commentpos-1] not in string.whitespace) or
3137 (commentpos >= 2 and
3138 line[commentpos-2] not in string.whitespace))):
3139 error(filename, linenum, 'whitespace/comments', 2,
3140 'At least two spaces is best between code and comments')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003141
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003142 # Checks for common mistakes in TODO comments.
3143 comment = line[commentpos:]
3144 match = _RE_PATTERN_TODO.match(comment)
3145 if match:
3146 # One whitespace is correct; zero whitespace is handled elsewhere.
3147 leading_whitespace = match.group(1)
3148 if len(leading_whitespace) > 1:
3149 error(filename, linenum, 'whitespace/todo', 2,
3150 'Too many spaces before TODO')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003151
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003152 username = match.group(2)
3153 if not username:
3154 error(filename, linenum, 'readability/todo', 2,
3155 'Missing username in TODO; it should look like '
3156 '"// TODO(my_username): Stuff."')
3157
3158 middle_whitespace = match.group(3)
3159 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
3160 if middle_whitespace != ' ' and middle_whitespace != '':
3161 error(filename, linenum, 'whitespace/todo', 2,
3162 'TODO(my_username) should be followed by a space')
3163
3164 # If the comment contains an alphanumeric character, there
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003165 # should be a space somewhere between it and the // unless
3166 # it's a /// or //! Doxygen comment.
3167 if (Match(r'//[^ ]*\w', comment) and
3168 not Match(r'(///|//\!)(\s+|$)', comment)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003169 error(filename, linenum, 'whitespace/comments', 4,
3170 'Should have a space between // and comment')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003171
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003172
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003173def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003174 """Checks for the correctness of various spacing issues in the code.
3175
3176 Things we check for: spaces around operators, spaces after
3177 if/for/while/switch, no spaces around parens in function calls, two
3178 spaces between code and comment, don't start a block with a blank
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003179 line, don't end a function with a blank line, don't add a blank line
3180 after public/protected/private, don't have too many blank lines in a row.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003181
3182 Args:
3183 filename: The name of the current file.
3184 clean_lines: A CleansedLines instance containing the file.
3185 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003186 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003187 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003188 error: The function to call with any errors found.
3189 """
3190
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003191 # Don't use "elided" lines here, otherwise we can't check commented lines.
3192 # Don't want to use "raw" either, because we don't want to check inside C++11
3193 # raw strings,
3194 raw = clean_lines.lines_without_raw_strings
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003195 line = raw[linenum]
3196
3197 # Before nixing comments, check if the line is blank for no good
3198 # reason. This includes the first line after a block is opened, and
3199 # blank lines at the end of a function (ie, right before a line like '}'
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003200 #
3201 # Skip all the blank line checks if we are immediately inside a
3202 # namespace body. In other words, don't issue blank line warnings
3203 # for this block:
3204 # namespace {
3205 #
3206 # }
3207 #
3208 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003209 #
3210 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
3211 # like namespaces.
3212 if (IsBlankLine(line) and
3213 not nesting_state.InNamespaceBody() and
3214 not nesting_state.InExternC()):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003215 elided = clean_lines.elided
3216 prev_line = elided[linenum - 1]
3217 prevbrace = prev_line.rfind('{')
3218 # TODO(unknown): Don't complain if line before blank line, and line after,
3219 # both start with alnums and are indented the same amount.
3220 # This ignores whitespace at the start of a namespace block
3221 # because those are not usually indented.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003222 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003223 # OK, we have a blank line at the start of a code block. Before we
3224 # complain, we check if it is an exception to the rule: The previous
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003225 # non-empty line has the parameters of a function header that are indented
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003226 # 4 spaces (because they did not fit in a 80 column line when placed on
3227 # the same line as the function name). We also check for the case where
3228 # the previous line is indented 6 spaces, which may happen when the
3229 # initializers of a constructor do not fit into a 80 column line.
3230 exception = False
3231 if Match(r' {6}\w', prev_line): # Initializer list?
3232 # We are looking for the opening column of initializer list, which
3233 # should be indented 4 spaces to cause 6 space indentation afterwards.
3234 search_position = linenum-2
3235 while (search_position >= 0
3236 and Match(r' {6}\w', elided[search_position])):
3237 search_position -= 1
3238 exception = (search_position >= 0
3239 and elided[search_position][:5] == ' :')
3240 else:
3241 # Search for the function arguments or an initializer list. We use a
3242 # simple heuristic here: If the line is indented 4 spaces; and we have a
3243 # closing paren, without the opening paren, followed by an opening brace
3244 # or colon (for initializer lists) we assume that it is the last line of
3245 # a function header. If we have a colon indented 4 spaces, it is an
3246 # initializer list.
3247 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3248 prev_line)
3249 or Match(r' {4}:', prev_line))
3250
3251 if not exception:
3252 error(filename, linenum, 'whitespace/blank_line', 2,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003253 'Redundant blank line at the start of a code block '
3254 'should be deleted.')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003255 # Ignore blank lines at the end of a block in a long if-else
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003256 # chain, like this:
3257 # if (condition1) {
3258 # // Something followed by a blank line
3259 #
3260 # } else if (condition2) {
3261 # // Something else
3262 # }
3263 if linenum + 1 < clean_lines.NumLines():
3264 next_line = raw[linenum + 1]
3265 if (next_line
3266 and Match(r'\s*}', next_line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003267 and next_line.find('} else ') == -1):
3268 error(filename, linenum, 'whitespace/blank_line', 3,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003269 'Redundant blank line at the end of a code block '
3270 'should be deleted.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003271
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003272 matched = Match(r'\s*(public|protected|private):', prev_line)
3273 if matched:
3274 error(filename, linenum, 'whitespace/blank_line', 3,
3275 'Do not leave a blank line after "%s:"' % matched.group(1))
3276
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003277 # Next, check comments
3278 next_line_start = 0
3279 if linenum + 1 < clean_lines.NumLines():
3280 next_line = raw[linenum + 1]
3281 next_line_start = len(next_line) - len(next_line.lstrip())
3282 CheckComment(line, filename, linenum, next_line_start, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003283
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003284 # get rid of comments and strings
3285 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003286
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003287 # You shouldn't have spaces before your brackets, except maybe after
3288 # 'delete []' or 'return []() {};'
3289 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3290 error(filename, linenum, 'whitespace/braces', 5,
3291 'Extra space before [')
3292
3293 # In range-based for, we wanted spaces before and after the colon, but
3294 # not around "::" tokens that might appear.
3295 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3296 Search(r'for *\(.*[^: ]:[^:]', line)):
3297 error(filename, linenum, 'whitespace/forcolon', 2,
3298 'Missing space around colon in range-based for loop')
3299
3300
3301def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3302 """Checks for horizontal spacing around operators.
3303
3304 Args:
3305 filename: The name of the current file.
3306 clean_lines: A CleansedLines instance containing the file.
3307 linenum: The number of the line to check.
3308 error: The function to call with any errors found.
3309 """
3310 line = clean_lines.elided[linenum]
3311
3312 # Don't try to do spacing checks for operator methods. Do this by
3313 # replacing the troublesome characters with something else,
3314 # preserving column position for all other characters.
3315 #
3316 # The replacement is done repeatedly to avoid false positives from
3317 # operators that call operators.
3318 while True:
3319 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3320 if match:
3321 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3322 else:
3323 break
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003324
3325 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3326 # Otherwise not. Note we only check for non-spaces on *both* sides;
3327 # sometimes people put non-spaces on one side when aligning ='s among
3328 # many lines (not that this is behavior that I approve of...)
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003329 if ((Search(r'[\w.]=', line) or
3330 Search(r'=[\w.]', line))
3331 and not Search(r'\b(if|while|for) ', line)
3332 # Operators taken from [lex.operators] in C++11 standard.
3333 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3334 and not Search(r'operator=', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003335 error(filename, linenum, 'whitespace/operators', 4,
3336 'Missing spaces around =')
3337
3338 # It's ok not to have spaces around binary operators like + - * /, but if
3339 # there's too little whitespace, we get concerned. It's hard to tell,
3340 # though, so we punt on this one for now. TODO.
3341
3342 # You should always have whitespace around binary operators.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003343 #
3344 # Check <= and >= first to avoid false positives with < and >, then
3345 # check non-include lines for spacing around < and >.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003346 #
3347 # If the operator is followed by a comma, assume it's be used in a
3348 # macro context and don't do any checks. This avoids false
3349 # positives.
3350 #
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003351 # Note that && is not included here. This is because there are too
3352 # many false positives due to RValue references.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003353 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003354 if match:
3355 error(filename, linenum, 'whitespace/operators', 3,
3356 'Missing spaces around %s' % match.group(1))
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003357 elif not Match(r'#.*include', line):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003358 # Look for < that is not surrounded by spaces. This is only
3359 # triggered if both sides are missing spaces, even though
3360 # technically should should flag if at least one side is missing a
3361 # space. This is done to avoid some false positives with shifts.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003362 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3363 if match:
3364 (_, _, end_pos) = CloseExpression(
3365 clean_lines, linenum, len(match.group(1)))
3366 if end_pos <= -1:
3367 error(filename, linenum, 'whitespace/operators', 3,
3368 'Missing spaces around <')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003369
3370 # Look for > that is not surrounded by spaces. Similar to the
3371 # above, we only trigger if both sides are missing spaces to avoid
3372 # false positives with shifts.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003373 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3374 if match:
3375 (_, _, start_pos) = ReverseCloseExpression(
3376 clean_lines, linenum, len(match.group(1)))
3377 if start_pos <= -1:
3378 error(filename, linenum, 'whitespace/operators', 3,
3379 'Missing spaces around >')
3380
3381 # We allow no-spaces around << when used like this: 10<<20, but
3382 # not otherwise (particularly, not when used as streams)
avakulenko@google.com59146752014-08-11 20:20:55 +00003383 #
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003384 # We also allow operators following an opening parenthesis, since
3385 # those tend to be macros that deal with operators.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003386 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line)
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003387 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003388 not (match.group(1) == 'operator' and match.group(2) == ';')):
3389 error(filename, linenum, 'whitespace/operators', 3,
3390 'Missing spaces around <<')
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003391
3392 # We allow no-spaces around >> for almost anything. This is because
3393 # C++11 allows ">>" to close nested templates, which accounts for
3394 # most cases when ">>" is not followed by a space.
3395 #
3396 # We still warn on ">>" followed by alpha character, because that is
3397 # likely due to ">>" being used for right shifts, e.g.:
3398 # value >> alpha
3399 #
3400 # When ">>" is used to close templates, the alphanumeric letter that
3401 # follows would be part of an identifier, and there should still be
3402 # a space separating the template type and the identifier.
3403 # type<type<type>> alpha
3404 match = Search(r'>>[a-zA-Z_]', line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003405 if match:
3406 error(filename, linenum, 'whitespace/operators', 3,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003407 'Missing spaces around >>')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003408
3409 # There shouldn't be space around unary operators
3410 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3411 if match:
3412 error(filename, linenum, 'whitespace/operators', 4,
3413 'Extra space for operator %s' % match.group(1))
3414
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003415
3416def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3417 """Checks for horizontal spacing around parentheses.
3418
3419 Args:
3420 filename: The name of the current file.
3421 clean_lines: A CleansedLines instance containing the file.
3422 linenum: The number of the line to check.
3423 error: The function to call with any errors found.
3424 """
3425 line = clean_lines.elided[linenum]
3426
3427 # No spaces after an if, while, switch, or for
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003428 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3429 if match:
3430 error(filename, linenum, 'whitespace/parens', 5,
3431 'Missing space before ( in %s' % match.group(1))
3432
3433 # For if/for/while/switch, the left and right parens should be
3434 # consistent about how many spaces are inside the parens, and
3435 # there should either be zero or one spaces inside the parens.
3436 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.com6317a9c2009-06-25 00:28:19 +00003437 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003438 match = Search(r'\b(if|for|while|switch)\s*'
3439 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3440 line)
3441 if match:
3442 if len(match.group(2)) != len(match.group(4)):
3443 if not (match.group(3) == ';' and
erg@google.com6317a9c2009-06-25 00:28:19 +00003444 len(match.group(2)) == 1 + len(match.group(4)) or
3445 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003446 error(filename, linenum, 'whitespace/parens', 5,
3447 'Mismatching spaces inside () in %s' % match.group(1))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003448 if len(match.group(2)) not in [0, 1]:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003449 error(filename, linenum, 'whitespace/parens', 5,
3450 'Should have zero or one spaces inside ( and ) in %s' %
3451 match.group(1))
3452
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003453
3454def CheckCommaSpacing(filename, clean_lines, linenum, error):
3455 """Checks for horizontal spacing near commas and semicolons.
3456
3457 Args:
3458 filename: The name of the current file.
3459 clean_lines: A CleansedLines instance containing the file.
3460 linenum: The number of the line to check.
3461 error: The function to call with any errors found.
3462 """
3463 raw = clean_lines.lines_without_raw_strings
3464 line = clean_lines.elided[linenum]
3465
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003466 # 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 +00003467 #
3468 # This does not apply when the non-space character following the
3469 # comma is another comma, since the only time when that happens is
3470 # for empty macro arguments.
3471 #
3472 # We run this check in two passes: first pass on elided lines to
3473 # verify that lines contain missing whitespaces, second pass on raw
3474 # lines to confirm that those missing whitespaces are not due to
3475 # elided comments.
avakulenko@google.com59146752014-08-11 20:20:55 +00003476 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3477 Search(r',[^,\s]', raw[linenum])):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003478 error(filename, linenum, 'whitespace/comma', 3,
3479 'Missing space after ,')
3480
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003481 # You should always have a space after a semicolon
3482 # except for few corner cases
3483 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3484 # space after ;
3485 if Search(r';[^\s};\\)/]', line):
3486 error(filename, linenum, 'whitespace/semicolon', 3,
3487 'Missing space after ;')
3488
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003489
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003490def _IsType(clean_lines, nesting_state, expr):
3491 """Check if expression looks like a type name, returns true if so.
3492
3493 Args:
3494 clean_lines: A CleansedLines instance containing the file.
3495 nesting_state: A NestingState instance which maintains information about
3496 the current stack of nested blocks being parsed.
3497 expr: The expression to check.
3498 Returns:
3499 True, if token looks like a type.
3500 """
3501 # Keep only the last token in the expression
3502 last_word = Match(r'^.*(\b\S+)$', expr)
3503 if last_word:
3504 token = last_word.group(1)
3505 else:
3506 token = expr
3507
3508 # Match native types and stdint types
3509 if _TYPES.match(token):
3510 return True
3511
3512 # Try a bit harder to match templated types. Walk up the nesting
3513 # stack until we find something that resembles a typename
3514 # declaration for what we are looking for.
3515 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) +
3516 r'\b')
3517 block_index = len(nesting_state.stack) - 1
3518 while block_index >= 0:
3519 if isinstance(nesting_state.stack[block_index], _NamespaceInfo):
3520 return False
3521
3522 # Found where the opening brace is. We want to scan from this
3523 # line up to the beginning of the function, minus a few lines.
3524 # template <typename Type1, // stop scanning here
3525 # ...>
3526 # class C
3527 # : public ... { // start scanning here
3528 last_line = nesting_state.stack[block_index].starting_linenum
3529
3530 next_block_start = 0
3531 if block_index > 0:
3532 next_block_start = nesting_state.stack[block_index - 1].starting_linenum
3533 first_line = last_line
3534 while first_line >= next_block_start:
3535 if clean_lines.elided[first_line].find('template') >= 0:
3536 break
3537 first_line -= 1
3538 if first_line < next_block_start:
3539 # Didn't find any "template" keyword before reaching the next block,
3540 # there are probably no template things to check for this block
3541 block_index -= 1
3542 continue
3543
3544 # Look for typename in the specified range
3545 for i in xrange(first_line, last_line + 1, 1):
3546 if Search(typename_pattern, clean_lines.elided[i]):
3547 return True
3548 block_index -= 1
3549
3550 return False
3551
3552
3553def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003554 """Checks for horizontal spacing near commas.
3555
3556 Args:
3557 filename: The name of the current file.
3558 clean_lines: A CleansedLines instance containing the file.
3559 linenum: The number of the line to check.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003560 nesting_state: A NestingState instance which maintains information about
3561 the current stack of nested blocks being parsed.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003562 error: The function to call with any errors found.
3563 """
3564 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003565
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003566 # Except after an opening paren, or after another opening brace (in case of
3567 # an initializer list, for instance), you should have spaces before your
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003568 # braces when they are delimiting blocks, classes, namespaces etc.
3569 # And since you should never have braces at the beginning of a line,
3570 # this is an easy test. Except that braces used for initialization don't
3571 # follow the same rule; we often don't want spaces before those.
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003572 match = Match(r'^(.*[^ ({>]){', line)
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003573
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003574 if match:
3575 # Try a bit harder to check for brace initialization. This
3576 # happens in one of the following forms:
3577 # Constructor() : initializer_list_{} { ... }
3578 # Constructor{}.MemberFunction()
3579 # Type variable{};
3580 # FunctionCall(type{}, ...);
3581 # LastArgument(..., type{});
3582 # LOG(INFO) << type{} << " ...";
3583 # map_of_type[{...}] = ...;
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003584 # ternary = expr ? new type{} : nullptr;
3585 # OuterTemplate<InnerTemplateConstructor<Type>{}>
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003586 #
3587 # We check for the character following the closing brace, and
3588 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003589 # "{.;,)<>]:".
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003590 #
3591 # To account for nested initializer list, we allow any number of
3592 # closing braces up to "{;,)<". We can't simply silence the
3593 # warning on first sight of closing brace, because that would
3594 # cause false negatives for things that are not initializer lists.
3595 # Silence this: But not this:
3596 # Outer{ if (...) {
3597 # Inner{...} if (...){ // Missing space before {
3598 # }; }
3599 #
3600 # There is a false negative with this approach if people inserted
3601 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3602 # spurious semicolon with a separate check.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003603 leading_text = match.group(1)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003604 (endline, endlinenum, endpos) = CloseExpression(
3605 clean_lines, linenum, len(match.group(1)))
3606 trailing_text = ''
3607 if endpos > -1:
3608 trailing_text = endline[endpos:]
3609 for offset in xrange(endlinenum + 1,
3610 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3611 trailing_text += clean_lines.elided[offset]
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003612 # We also suppress warnings for `uint64_t{expression}` etc., as the style
3613 # guide recommends brace initialization for integral types to avoid
3614 # overflow/truncation.
3615 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text)
3616 and not _IsType(clean_lines, nesting_state, leading_text)):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003617 error(filename, linenum, 'whitespace/braces', 5,
3618 'Missing space before {')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003619
3620 # Make sure '} else {' has spaces.
3621 if Search(r'}else', line):
3622 error(filename, linenum, 'whitespace/braces', 5,
3623 'Missing space before else')
3624
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003625 # You shouldn't have a space before a semicolon at the end of the line.
3626 # There's a special case for "for" since the style guide allows space before
3627 # the semicolon there.
3628 if Search(r':\s*;\s*$', line):
3629 error(filename, linenum, 'whitespace/semicolon', 5,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003630 'Semicolon defining empty statement. Use {} instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003631 elif Search(r'^\s*;\s*$', line):
3632 error(filename, linenum, 'whitespace/semicolon', 5,
3633 'Line contains only semicolon. If this should be an empty statement, '
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003634 'use {} instead.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003635 elif (Search(r'\s+;\s*$', line) and
3636 not Search(r'\bfor\b', line)):
3637 error(filename, linenum, 'whitespace/semicolon', 5,
3638 'Extra space before last semicolon. If this should be an empty '
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003639 'statement, use {} instead.')
3640
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003641
3642def IsDecltype(clean_lines, linenum, column):
3643 """Check if the token ending on (linenum, column) is decltype().
3644
3645 Args:
3646 clean_lines: A CleansedLines instance containing the file.
3647 linenum: the number of the line to check.
3648 column: end column of the token to check.
3649 Returns:
3650 True if this token is decltype() expression, False otherwise.
3651 """
3652 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3653 if start_col < 0:
3654 return False
3655 if Search(r'\bdecltype\s*$', text[0:start_col]):
3656 return True
3657 return False
3658
3659
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003660def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3661 """Checks for additional blank line issues related to sections.
3662
3663 Currently the only thing checked here is blank line before protected/private.
3664
3665 Args:
3666 filename: The name of the current file.
3667 clean_lines: A CleansedLines instance containing the file.
3668 class_info: A _ClassInfo objects.
3669 linenum: The number of the line to check.
3670 error: The function to call with any errors found.
3671 """
3672 # Skip checks if the class is small, where small means 25 lines or less.
3673 # 25 lines seems like a good cutoff since that's the usual height of
3674 # terminals, and any class that can't fit in one screen can't really
3675 # be considered "small".
3676 #
3677 # Also skip checks if we are on the first line. This accounts for
3678 # classes that look like
3679 # class Foo { public: ... };
3680 #
3681 # If we didn't find the end of the class, last_line would be zero,
3682 # and the check will be skipped by the first condition.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003683 if (class_info.last_line - class_info.starting_linenum <= 24 or
3684 linenum <= class_info.starting_linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003685 return
3686
3687 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3688 if matched:
3689 # Issue warning if the line before public/protected/private was
3690 # not a blank line, but don't do this if the previous line contains
3691 # "class" or "struct". This can happen two ways:
3692 # - We are at the beginning of the class.
3693 # - We are forward-declaring an inner class that is semantically
3694 # private, but needed to be public for implementation reasons.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003695 # Also ignores cases where the previous line ends with a backslash as can be
3696 # common when defining classes in C macros.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003697 prev_line = clean_lines.lines[linenum - 1]
3698 if (not IsBlankLine(prev_line) and
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003699 not Search(r'\b(class|struct)\b', prev_line) and
3700 not Search(r'\\$', prev_line)):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003701 # Try a bit harder to find the beginning of the class. This is to
3702 # account for multi-line base-specifier lists, e.g.:
3703 # class Derived
3704 # : public Base {
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00003705 end_class_head = class_info.starting_linenum
3706 for i in range(class_info.starting_linenum, linenum):
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00003707 if Search(r'\{\s*$', clean_lines.lines[i]):
3708 end_class_head = i
3709 break
3710 if end_class_head < linenum - 1:
3711 error(filename, linenum, 'whitespace/blank_line', 3,
3712 '"%s:" should be preceded by a blank line' % matched.group(1))
3713
3714
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003715def GetPreviousNonBlankLine(clean_lines, linenum):
3716 """Return the most recent non-blank line and its line number.
3717
3718 Args:
3719 clean_lines: A CleansedLines instance containing the file contents.
3720 linenum: The number of the line to check.
3721
3722 Returns:
3723 A tuple with two elements. The first element is the contents of the last
3724 non-blank line before the current line, or the empty string if this is the
3725 first non-blank line. The second is the line number of that line, or -1
3726 if this is the first non-blank line.
3727 """
3728
3729 prevlinenum = linenum - 1
3730 while prevlinenum >= 0:
3731 prevline = clean_lines.elided[prevlinenum]
3732 if not IsBlankLine(prevline): # if not a blank line...
3733 return (prevline, prevlinenum)
3734 prevlinenum -= 1
3735 return ('', -1)
3736
3737
3738def CheckBraces(filename, clean_lines, linenum, error):
3739 """Looks for misplaced braces (e.g. at the end of line).
3740
3741 Args:
3742 filename: The name of the current file.
3743 clean_lines: A CleansedLines instance containing the file.
3744 linenum: The number of the line to check.
3745 error: The function to call with any errors found.
3746 """
3747
3748 line = clean_lines.elided[linenum] # get rid of comments and strings
3749
3750 if Match(r'\s*{\s*$', line):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003751 # We allow an open brace to start a line in the case where someone is using
3752 # braces in a block to explicitly create a new scope, which is commonly used
3753 # to control the lifetime of stack-allocated variables. Braces are also
3754 # used for brace initializers inside function calls. We don't detect this
3755 # perfectly: we just don't complain if the last non-whitespace character on
3756 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003757 # previous line starts a preprocessor block. We also allow a brace on the
3758 # following line if it is part of an array initialization and would not fit
3759 # within the 80 character limit of the preceding line.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003760 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003761 if (not Search(r'[,;:}{(]\s*$', prevline) and
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003762 not Match(r'\s*#', prevline) and
3763 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003764 error(filename, linenum, 'whitespace/braces', 4,
3765 '{ should almost always be at the end of the previous line')
3766
3767 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003768 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003769 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3770 if Match(r'\s*}\s*$', prevline):
3771 error(filename, linenum, 'whitespace/newline', 4,
3772 'An else should appear on the same line as the preceding }')
3773
3774 # If braces come on one side of an else, they should be on both.
3775 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003776 if Search(r'else if\s*\(', line): # could be multi-line if
3777 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3778 # find the ( after the if
3779 pos = line.find('else if')
3780 pos = line.find('(', pos)
3781 if pos > 0:
3782 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3783 brace_on_right = endline[endpos:].find('{') != -1
3784 if brace_on_left != brace_on_right: # must be brace after if
3785 error(filename, linenum, 'readability/braces', 5,
3786 'If an else has a brace on one side, it should have it on both')
3787 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3788 error(filename, linenum, 'readability/braces', 5,
3789 'If an else has a brace on one side, it should have it on both')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00003790
3791 # Likewise, an else should never have the else clause on the same line
3792 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3793 error(filename, linenum, 'whitespace/newline', 4,
3794 'Else clause should never be on same line as else (use 2 lines)')
3795
3796 # In the same way, a do/while should never be on one line
3797 if Match(r'\s*do [^\s{]', line):
3798 error(filename, linenum, 'whitespace/newline', 4,
3799 'do/while clauses should not be on a single line')
3800
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003801 # Check single-line if/else bodies. The style guide says 'curly braces are not
3802 # required for single-line statements'. We additionally allow multi-line,
3803 # single statements, but we reject anything with more than one semicolon in
3804 # it. This means that the first semicolon after the if should be at the end of
3805 # its line, and the line after that should have an indent level equal to or
3806 # lower than the if. We also check for ambiguous if/else nesting without
3807 # braces.
3808 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3809 if if_else_match and not Match(r'\s*#', line):
3810 if_indent = GetIndentLevel(line)
3811 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3812 if_match = Search(r'\bif\s*\(', line)
3813 if if_match:
3814 # This could be a multiline if condition, so find the end first.
3815 pos = if_match.end() - 1
3816 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3817 # Check for an opening brace, either directly after the if or on the next
3818 # line. If found, this isn't a single-statement conditional.
3819 if (not Match(r'\s*{', endline[endpos:])
3820 and not (Match(r'\s*$', endline[endpos:])
3821 and endlinenum < (len(clean_lines.elided) - 1)
3822 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3823 while (endlinenum < len(clean_lines.elided)
3824 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3825 endlinenum += 1
3826 endpos = 0
3827 if endlinenum < len(clean_lines.elided):
3828 endline = clean_lines.elided[endlinenum]
3829 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3830 # methods) and a single \ after the semicolon (for macros)
3831 endpos = endline.find(';')
3832 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.com59146752014-08-11 20:20:55 +00003833 # Semicolon isn't the last character, there's something trailing.
3834 # Output a warning if the semicolon is not contained inside
3835 # a lambda expression.
3836 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3837 endline):
3838 error(filename, linenum, 'readability/braces', 4,
3839 'If/else bodies with multiple statements require braces')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003840 elif endlinenum < len(clean_lines.elided) - 1:
3841 # Make sure the next line is dedented
3842 next_line = clean_lines.elided[endlinenum + 1]
3843 next_indent = GetIndentLevel(next_line)
3844 # With ambiguous nested if statements, this will error out on the
3845 # if that *doesn't* match the else, regardless of whether it's the
3846 # inner one or outer one.
3847 if (if_match and Match(r'\s*else\b', next_line)
3848 and next_indent != if_indent):
3849 error(filename, linenum, 'readability/braces', 4,
3850 'Else clause should be indented at the same level as if. '
3851 'Ambiguous nested if/else chains require braces.')
3852 elif next_indent > if_indent:
3853 error(filename, linenum, 'readability/braces', 4,
3854 'If/else bodies with multiple statements require braces')
3855
3856
3857def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3858 """Looks for redundant trailing semicolon.
3859
3860 Args:
3861 filename: The name of the current file.
3862 clean_lines: A CleansedLines instance containing the file.
3863 linenum: The number of the line to check.
3864 error: The function to call with any errors found.
3865 """
3866
3867 line = clean_lines.elided[linenum]
3868
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003869 # Block bodies should not be followed by a semicolon. Due to C++11
3870 # brace initialization, there are more places where semicolons are
3871 # required than not, so we use a whitelist approach to check these
3872 # rather than a blacklist. These are the places where "};" should
3873 # be replaced by just "}":
3874 # 1. Some flavor of block following closing parenthesis:
3875 # for (;;) {};
3876 # while (...) {};
3877 # switch (...) {};
3878 # Function(...) {};
3879 # if (...) {};
3880 # if (...) else if (...) {};
3881 #
3882 # 2. else block:
3883 # if (...) else {};
3884 #
3885 # 3. const member function:
3886 # Function(...) const {};
3887 #
3888 # 4. Block following some statement:
3889 # x = 42;
3890 # {};
3891 #
3892 # 5. Block at the beginning of a function:
3893 # Function(...) {
3894 # {};
3895 # }
3896 #
3897 # Note that naively checking for the preceding "{" will also match
3898 # braces inside multi-dimensional arrays, but this is fine since
3899 # that expression will not contain semicolons.
3900 #
3901 # 6. Block following another block:
3902 # while (true) {}
3903 # {};
3904 #
3905 # 7. End of namespaces:
3906 # namespace {};
3907 #
3908 # These semicolons seems far more common than other kinds of
3909 # redundant semicolons, possibly due to people converting classes
3910 # to namespaces. For now we do not warn for this case.
3911 #
3912 # Try matching case 1 first.
3913 match = Match(r'^(.*\)\s*)\{', line)
3914 if match:
3915 # Matched closing parenthesis (case 1). Check the token before the
3916 # matching opening parenthesis, and don't warn if it looks like a
3917 # macro. This avoids these false positives:
3918 # - macro that defines a base class
3919 # - multi-line macro that defines a base class
3920 # - macro that defines the whole class-head
3921 #
3922 # But we still issue warnings for macros that we know are safe to
3923 # warn, specifically:
3924 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3925 # - TYPED_TEST
3926 # - INTERFACE_DEF
3927 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3928 #
3929 # We implement a whitelist of safe macros instead of a blacklist of
3930 # unsafe macros, even though the latter appears less frequently in
3931 # google code and would have been easier to implement. This is because
3932 # the downside for getting the whitelist wrong means some extra
3933 # semicolons, while the downside for getting the blacklist wrong
3934 # would result in compile errors.
3935 #
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003936 # In addition to macros, we also don't want to warn on
3937 # - Compound literals
3938 # - Lambdas
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003939 # - alignas specifier with anonymous structs
3940 # - decltype
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003941 closing_brace_pos = match.group(1).rfind(')')
3942 opening_parenthesis = ReverseCloseExpression(
3943 clean_lines, linenum, closing_brace_pos)
3944 if opening_parenthesis[2] > -1:
3945 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003946 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003947 func = Match(r'^(.*\])\s*$', line_prefix)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003948 if ((macro and
3949 macro.group(1) not in (
3950 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3951 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3952 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003953 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
avakulenko@google.com255f2be2014-12-05 22:19:55 +00003954 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00003955 Search(r'\bdecltype$', line_prefix) or
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003956 Search(r'\s+=\s*$', line_prefix)):
3957 match = None
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00003958 if (match and
3959 opening_parenthesis[1] > 1 and
3960 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3961 # Multi-line lambda-expression
3962 match = None
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00003963
3964 else:
3965 # Try matching cases 2-3.
3966 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3967 if not match:
3968 # Try matching cases 4-6. These are always matched on separate lines.
3969 #
3970 # Note that we can't simply concatenate the previous line to the
3971 # current line and do a single match, otherwise we may output
3972 # duplicate warnings for the blank line case:
3973 # if (cond) {
3974 # // blank line
3975 # }
3976 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3977 if prevline and Search(r'[;{}]\s*$', prevline):
3978 match = Match(r'^(\s*)\{', line)
3979
3980 # Check matching closing brace
3981 if match:
3982 (endline, endlinenum, endpos) = CloseExpression(
3983 clean_lines, linenum, len(match.group(1)))
3984 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3985 # Current {} pair is eligible for semicolon check, and we have found
3986 # the redundant semicolon, output warning here.
3987 #
3988 # Note: because we are scanning forward for opening braces, and
3989 # outputting warnings for the matching closing brace, if there are
3990 # nested blocks with trailing semicolons, we will get the error
3991 # messages in reversed order.
Amin Hassanif7e1e102018-06-21 20:13:30 +00003992
3993 # We need to check the line forward for NOLINT
3994 raw_lines = clean_lines.raw_lines
3995 ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1,
3996 error)
3997 ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum,
3998 error)
3999
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004000 error(filename, endlinenum, 'readability/braces', 4,
4001 "You don't need a ; after a }")
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004002
4003
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004004def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
4005 """Look for empty loop/conditional body with only a single semicolon.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004006
4007 Args:
4008 filename: The name of the current file.
4009 clean_lines: A CleansedLines instance containing the file.
4010 linenum: The number of the line to check.
4011 error: The function to call with any errors found.
4012 """
4013
4014 # Search for loop keywords at the beginning of the line. Because only
4015 # whitespaces are allowed before the keywords, this will also ignore most
4016 # do-while-loops, since those lines should start with closing brace.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004017 #
4018 # We also check "if" blocks here, since an empty conditional block
4019 # is likely an error.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004020 line = clean_lines.elided[linenum]
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004021 matched = Match(r'\s*(for|while|if)\s*\(', line)
4022 if matched:
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004023 # Find the end of the conditional expression.
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004024 (end_line, end_linenum, end_pos) = CloseExpression(
4025 clean_lines, linenum, line.find('('))
4026
4027 # Output warning if what follows the condition expression is a semicolon.
4028 # No warning for all other cases, including whitespace or newline, since we
4029 # have a separate check for semicolons preceded by whitespace.
4030 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004031 if matched.group(1) == 'if':
4032 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
4033 'Empty conditional bodies should use {}')
4034 else:
4035 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
4036 'Empty loop bodies should use {} or continue')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004037
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004038 # Check for if statements that have completely empty bodies (no comments)
4039 # and no else clauses.
4040 if end_pos >= 0 and matched.group(1) == 'if':
4041 # Find the position of the opening { for the if statement.
4042 # Return without logging an error if it has no brackets.
4043 opening_linenum = end_linenum
4044 opening_line_fragment = end_line[end_pos:]
4045 # Loop until EOF or find anything that's not whitespace or opening {.
4046 while not Search(r'^\s*\{', opening_line_fragment):
4047 if Search(r'^(?!\s*$)', opening_line_fragment):
4048 # Conditional has no brackets.
4049 return
4050 opening_linenum += 1
4051 if opening_linenum == len(clean_lines.elided):
4052 # Couldn't find conditional's opening { or any code before EOF.
4053 return
4054 opening_line_fragment = clean_lines.elided[opening_linenum]
4055 # Set opening_line (opening_line_fragment may not be entire opening line).
4056 opening_line = clean_lines.elided[opening_linenum]
4057
4058 # Find the position of the closing }.
4059 opening_pos = opening_line_fragment.find('{')
4060 if opening_linenum == end_linenum:
4061 # We need to make opening_pos relative to the start of the entire line.
4062 opening_pos += end_pos
4063 (closing_line, closing_linenum, closing_pos) = CloseExpression(
4064 clean_lines, opening_linenum, opening_pos)
4065 if closing_pos < 0:
4066 return
4067
4068 # Now construct the body of the conditional. This consists of the portion
4069 # of the opening line after the {, all lines until the closing line,
4070 # and the portion of the closing line before the }.
4071 if (clean_lines.raw_lines[opening_linenum] !=
4072 CleanseComments(clean_lines.raw_lines[opening_linenum])):
4073 # Opening line ends with a comment, so conditional isn't empty.
4074 return
4075 if closing_linenum > opening_linenum:
4076 # Opening line after the {. Ignore comments here since we checked above.
4077 body = list(opening_line[opening_pos+1:])
4078 # All lines until closing line, excluding closing line, with comments.
4079 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum])
4080 # Closing line before the }. Won't (and can't) have comments.
4081 body.append(clean_lines.elided[closing_linenum][:closing_pos-1])
4082 body = '\n'.join(body)
4083 else:
4084 # If statement has brackets and fits on a single line.
4085 body = opening_line[opening_pos+1:closing_pos-1]
4086
4087 # Check if the body is empty
4088 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body):
4089 return
4090 # The body is empty. Now make sure there's not an else clause.
4091 current_linenum = closing_linenum
4092 current_line_fragment = closing_line[closing_pos:]
4093 # Loop until EOF or find anything that's not whitespace or else clause.
4094 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment):
4095 if Search(r'^(?=\s*else)', current_line_fragment):
4096 # Found an else clause, so don't log an error.
4097 return
4098 current_linenum += 1
4099 if current_linenum == len(clean_lines.elided):
4100 break
4101 current_line_fragment = clean_lines.elided[current_linenum]
4102
4103 # The body is empty and there's no else clause until EOF or other code.
4104 error(filename, end_linenum, 'whitespace/empty_if_body', 4,
4105 ('If statement had no body and no else clause'))
4106
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004107
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004108def FindCheckMacro(line):
4109 """Find a replaceable CHECK-like macro.
4110
4111 Args:
4112 line: line to search on.
4113 Returns:
4114 (macro name, start position), or (None, -1) if no replaceable
4115 macro is found.
4116 """
4117 for macro in _CHECK_MACROS:
4118 i = line.find(macro)
4119 if i >= 0:
4120 # Find opening parenthesis. Do a regular expression match here
4121 # to make sure that we are matching the expected CHECK macro, as
4122 # opposed to some other macro that happens to contain the CHECK
4123 # substring.
4124 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4125 if not matched:
4126 continue
4127 return (macro, len(matched.group(1)))
4128 return (None, -1)
4129
4130
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004131def CheckCheck(filename, clean_lines, linenum, error):
4132 """Checks the use of CHECK and EXPECT macros.
4133
4134 Args:
4135 filename: The name of the current file.
4136 clean_lines: A CleansedLines instance containing the file.
4137 linenum: The number of the line to check.
4138 error: The function to call with any errors found.
4139 """
4140
4141 # Decide the set of replacement macros that should be suggested
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004142 lines = clean_lines.elided
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004143 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4144 if not check_macro:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004145 return
4146
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004147 # Find end of the boolean expression by matching parentheses
4148 (last_line, end_line, end_pos) = CloseExpression(
4149 clean_lines, linenum, start_pos)
4150 if end_pos < 0:
4151 return
avakulenko@google.com59146752014-08-11 20:20:55 +00004152
4153 # If the check macro is followed by something other than a
4154 # semicolon, assume users will log their own custom error messages
4155 # and don't suggest any replacements.
4156 if not Match(r'\s*;', last_line[end_pos:]):
4157 return
4158
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004159 if linenum == end_line:
4160 expression = lines[linenum][start_pos + 1:end_pos - 1]
4161 else:
4162 expression = lines[linenum][start_pos + 1:]
4163 for i in xrange(linenum + 1, end_line):
4164 expression += lines[i]
4165 expression += last_line[0:end_pos - 1]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004166
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004167 # Parse expression so that we can take parentheses into account.
4168 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4169 # which is not replaceable by CHECK_LE.
4170 lhs = ''
4171 rhs = ''
4172 operator = None
4173 while expression:
4174 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4175 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4176 if matched:
4177 token = matched.group(1)
4178 if token == '(':
4179 # Parenthesized operand
4180 expression = matched.group(2)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004181 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004182 if end < 0:
4183 return # Unmatched parenthesis
4184 lhs += '(' + expression[0:end]
4185 expression = expression[end:]
4186 elif token in ('&&', '||'):
4187 # Logical and/or operators. This means the expression
4188 # contains more than one term, for example:
4189 # CHECK(42 < a && a < b);
4190 #
4191 # These are not replaceable with CHECK_LE, so bail out early.
4192 return
4193 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4194 # Non-relational operator
4195 lhs += token
4196 expression = matched.group(2)
4197 else:
4198 # Relational operator
4199 operator = token
4200 rhs = matched.group(2)
4201 break
4202 else:
4203 # Unparenthesized operand. Instead of appending to lhs one character
4204 # at a time, we do another regular expression match to consume several
4205 # characters at once if possible. Trivial benchmark shows that this
4206 # is more efficient when the operands are longer than a single
4207 # character, which is generally the case.
4208 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4209 if not matched:
4210 matched = Match(r'^(\s*\S)(.*)$', expression)
4211 if not matched:
4212 break
4213 lhs += matched.group(1)
4214 expression = matched.group(2)
4215
4216 # Only apply checks if we got all parts of the boolean expression
4217 if not (lhs and operator and rhs):
4218 return
4219
4220 # Check that rhs do not contain logical operators. We already know
4221 # that lhs is fine since the loop above parses out && and ||.
4222 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4223 return
4224
4225 # At least one of the operands must be a constant literal. This is
4226 # to avoid suggesting replacements for unprintable things like
4227 # CHECK(variable != iterator)
4228 #
4229 # The following pattern matches decimal, hex integers, strings, and
4230 # characters (in that order).
4231 lhs = lhs.strip()
4232 rhs = rhs.strip()
4233 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4234 if Match(match_constant, lhs) or Match(match_constant, rhs):
4235 # Note: since we know both lhs and rhs, we can provide a more
4236 # descriptive error message like:
4237 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4238 # Instead of:
4239 # Consider using CHECK_EQ instead of CHECK(a == b)
4240 #
4241 # We are still keeping the less descriptive message because if lhs
4242 # or rhs gets long, the error message might become unreadable.
4243 error(filename, linenum, 'readability/check', 2,
4244 'Consider using %s instead of %s(a %s b)' % (
4245 _CHECK_REPLACEMENT[check_macro][operator],
4246 check_macro, operator))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004247
4248
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004249def CheckAltTokens(filename, clean_lines, linenum, error):
4250 """Check alternative keywords being used in boolean expressions.
4251
4252 Args:
4253 filename: The name of the current file.
4254 clean_lines: A CleansedLines instance containing the file.
4255 linenum: The number of the line to check.
4256 error: The function to call with any errors found.
4257 """
4258 line = clean_lines.elided[linenum]
4259
4260 # Avoid preprocessor lines
4261 if Match(r'^\s*#', line):
4262 return
4263
4264 # Last ditch effort to avoid multi-line comments. This will not help
4265 # if the comment started before the current line or ended after the
4266 # current line, but it catches most of the false positives. At least,
4267 # it provides a way to workaround this warning for people who use
4268 # multi-line comments in preprocessor macros.
4269 #
4270 # TODO(unknown): remove this once cpplint has better support for
4271 # multi-line comments.
4272 if line.find('/*') >= 0 or line.find('*/') >= 0:
4273 return
4274
4275 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4276 error(filename, linenum, 'readability/alt_tokens', 2,
4277 'Use operator %s instead of %s' % (
4278 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4279
4280
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004281def GetLineWidth(line):
4282 """Determines the width of the line in column positions.
4283
4284 Args:
4285 line: A string, which may be a Unicode string.
4286
4287 Returns:
4288 The width of the line in column positions, accounting for Unicode
4289 combining characters and wide characters.
4290 """
4291 if isinstance(line, unicode):
4292 width = 0
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004293 for uc in unicodedata.normalize('NFC', line):
4294 if unicodedata.east_asian_width(uc) in ('W', 'F'):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004295 width += 2
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004296 elif not unicodedata.combining(uc):
Amin Hassanif7e1e102018-06-21 20:13:30 +00004297 # Issue 337
4298 # https://mail.python.org/pipermail/python-list/2012-August/628809.html
4299 if (sys.version_info.major, sys.version_info.minor) <= (3, 2):
4300 # https://github.com/python/cpython/blob/2.7/Include/unicodeobject.h#L81
4301 is_wide_build = sysconfig.get_config_var("Py_UNICODE_SIZE") >= 4
4302 # https://github.com/python/cpython/blob/2.7/Objects/unicodeobject.c#L564
4303 is_low_surrogate = 0xDC00 <= ord(uc) <= 0xDFFF
4304 if not is_wide_build and is_low_surrogate:
4305 width -= 1
4306
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004307 width += 1
4308 return width
4309 else:
4310 return len(line)
4311
4312
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004313def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004314 error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004315 """Checks rules from the 'C++ style rules' section of cppguide.html.
4316
4317 Most of these rules are hard to test (naming, comment style), but we
4318 do what we can. In particular we check for 2-space indents, line lengths,
4319 tab usage, spaces inside code, etc.
4320
4321 Args:
4322 filename: The name of the current file.
4323 clean_lines: A CleansedLines instance containing the file.
4324 linenum: The number of the line to check.
4325 file_extension: The extension (without the dot) of the filename.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004326 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004327 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004328 error: The function to call with any errors found.
4329 """
4330
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004331 # Don't use "elided" lines here, otherwise we can't check commented lines.
4332 # Don't want to use "raw" either, because we don't want to check inside C++11
4333 # raw strings,
4334 raw_lines = clean_lines.lines_without_raw_strings
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004335 line = raw_lines[linenum]
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004336 prev = raw_lines[linenum - 1] if linenum > 0 else ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004337
4338 if line.find('\t') != -1:
4339 error(filename, linenum, 'whitespace/tab', 1,
4340 'Tab found; better to use spaces')
4341
4342 # One or three blank spaces at the beginning of the line is weird; it's
4343 # hard to reconcile that with 2-space indents.
4344 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4345 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4346 # if(RLENGTH > 20) complain = 0;
4347 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4348 # if(match(prev, "&& *$")) complain = 0;
4349 # if(match(prev, "\\|\\| *$")) complain = 0;
4350 # if(match(prev, "[\",=><] *$")) complain = 0;
4351 # if(match($0, " <<")) complain = 0;
4352 # if(match(prev, " +for \\(")) complain = 0;
4353 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004354 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4355 classinfo = nesting_state.InnermostClass()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004356 initial_spaces = 0
4357 cleansed_line = clean_lines.elided[linenum]
4358 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4359 initial_spaces += 1
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004360 # There are certain situations we allow one space, notably for
4361 # section labels, and also lines containing multi-line raw strings.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004362 # We also don't check for lines that look like continuation lines
4363 # (of lines ending in double quotes, commas, equals, or angle brackets)
4364 # because the rules for how to indent those are non-trivial.
4365 if (not Search(r'[",=><] *$', prev) and
4366 (initial_spaces == 1 or initial_spaces == 3) and
4367 not Match(scope_or_label_pattern, cleansed_line) and
4368 not (clean_lines.raw_lines[linenum] != line and
4369 Match(r'^\s*""', line))):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004370 error(filename, linenum, 'whitespace/indent', 3,
4371 'Weird number of spaces at line-start. '
4372 'Are you using a 2-space indent?')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004373
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004374 if line and line[-1].isspace():
4375 error(filename, linenum, 'whitespace/end_of_line', 4,
4376 'Line ends in whitespace. Consider deleting these extra spaces.')
4377
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004378 # Check if the line is a header guard.
4379 is_header_guard = False
Amin Hassanif7e1e102018-06-21 20:13:30 +00004380 if IsHeaderExtension(file_extension):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004381 cppvar = GetHeaderGuardCPPVariable(filename)
4382 if (line.startswith('#ifndef %s' % cppvar) or
4383 line.startswith('#define %s' % cppvar) or
4384 line.startswith('#endif // %s' % cppvar)):
4385 is_header_guard = True
4386 # #include lines and header guards can be long, since there's no clean way to
4387 # split them.
erg@google.com6317a9c2009-06-25 00:28:19 +00004388 #
4389 # URLs can be long too. It's possible to split these, but it makes them
4390 # harder to cut&paste.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004391 #
4392 # The "$Id:...$" comment may also get very long without it being the
4393 # developers fault.
erg@google.com6317a9c2009-06-25 00:28:19 +00004394 if (not line.startswith('#include') and not is_header_guard and
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004395 not Match(r'^\s*//.*http(s?)://\S*$', line) and
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004396 not Match(r'^\s*//\s*[^\s]*$', line) and
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004397 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004398 line_width = GetLineWidth(line)
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004399 if line_width > _line_length:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004400 error(filename, linenum, 'whitespace/line_length', 2,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004401 'Lines should be <= %i characters long' % _line_length)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004402
4403 if (cleansed_line.count(';') > 1 and
4404 # for loops are allowed two ;'s (and may run over two lines).
4405 cleansed_line.find('for') == -1 and
4406 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4407 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4408 # It's ok to have many commands in a switch case that fits in 1 line
4409 not ((cleansed_line.find('case ') != -1 or
4410 cleansed_line.find('default:') != -1) and
4411 cleansed_line.find('break;') != -1)):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004412 error(filename, linenum, 'whitespace/newline', 0,
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004413 'More than one command on the same line')
4414
4415 # Some more style checks
4416 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004417 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004418 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004419 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004420 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4421 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4422 CheckCommaSpacing(filename, clean_lines, linenum, error)
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004423 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004424 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004425 CheckCheck(filename, clean_lines, linenum, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004426 CheckAltTokens(filename, clean_lines, linenum, error)
4427 classinfo = nesting_state.InnermostClass()
4428 if classinfo:
4429 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004430
4431
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004432_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4433# Matches the first component of a filename delimited by -s and _s. That is:
4434# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4435# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4436# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4437# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4438_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4439
4440
4441def _DropCommonSuffixes(filename):
4442 """Drops common suffixes like _test.cc or -inl.h from filename.
4443
4444 For example:
4445 >>> _DropCommonSuffixes('foo/foo-inl.h')
4446 'foo/foo'
4447 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4448 'foo/bar/foo'
4449 >>> _DropCommonSuffixes('foo/foo_internal.h')
4450 'foo/foo'
4451 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4452 'foo/foo_unusualinternal'
4453
4454 Args:
4455 filename: The input filename.
4456
4457 Returns:
4458 The filename with the common suffix removed.
4459 """
4460 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4461 'inl.h', 'impl.h', 'internal.h'):
4462 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4463 filename[-len(suffix) - 1] in ('-', '_')):
4464 return filename[:-len(suffix) - 1]
4465 return os.path.splitext(filename)[0]
4466
4467
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004468def _ClassifyInclude(fileinfo, include, is_system):
4469 """Figures out what kind of header 'include' is.
4470
4471 Args:
4472 fileinfo: The current file cpplint is running over. A FileInfo instance.
4473 include: The path to a #included file.
4474 is_system: True if the #include used <> rather than "".
4475
4476 Returns:
4477 One of the _XXX_HEADER constants.
4478
4479 For example:
4480 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4481 _C_SYS_HEADER
4482 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4483 _CPP_SYS_HEADER
4484 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4485 _LIKELY_MY_HEADER
4486 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4487 ... 'bar/foo_other_ext.h', False)
4488 _POSSIBLE_MY_HEADER
4489 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4490 _OTHER_HEADER
4491 """
4492 # This is a list of all standard c++ header files, except
4493 # those already checked for above.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004494 is_cpp_h = include in _CPP_HEADERS
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004495
4496 if is_system:
4497 if is_cpp_h:
4498 return _CPP_SYS_HEADER
4499 else:
4500 return _C_SYS_HEADER
4501
4502 # If the target file and the include we're checking share a
4503 # basename when we drop common extensions, and the include
4504 # lives in . , then it's likely to be owned by the target file.
4505 target_dir, target_base = (
4506 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4507 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4508 if target_base == include_base and (
4509 include_dir == target_dir or
4510 include_dir == os.path.normpath(target_dir + '/../public')):
4511 return _LIKELY_MY_HEADER
4512
4513 # If the target and include share some initial basename
4514 # component, it's possible the target is implementing the
4515 # include, so it's allowed to be first, but we'll never
4516 # complain if it's not there.
4517 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4518 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4519 if (target_first_component and include_first_component and
4520 target_first_component.group(0) ==
4521 include_first_component.group(0)):
4522 return _POSSIBLE_MY_HEADER
4523
4524 return _OTHER_HEADER
4525
4526
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004527
erg@google.com6317a9c2009-06-25 00:28:19 +00004528def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4529 """Check rules that are applicable to #include lines.
4530
4531 Strings on #include lines are NOT removed from elided line, to make
4532 certain tasks easier. However, to prevent false positives, checks
4533 applicable to #include lines in CheckLanguage must be put here.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004534
4535 Args:
4536 filename: The name of the current file.
4537 clean_lines: A CleansedLines instance containing the file.
4538 linenum: The number of the line to check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004539 include_state: An _IncludeState instance in which the headers are inserted.
4540 error: The function to call with any errors found.
4541 """
4542 fileinfo = FileInfo(filename)
erg@google.com6317a9c2009-06-25 00:28:19 +00004543 line = clean_lines.lines[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004544
4545 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.com59146752014-08-11 20:20:55 +00004546 # Only do this check if the included header follows google naming
4547 # conventions. If not, assume that it's a 3rd party API that
4548 # requires special include conventions.
4549 #
4550 # We also make an exception for Lua headers, which follow google
4551 # naming convention but not the include convention.
4552 match = Match(r'#include\s*"([^/]+\.h)"', line)
4553 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004554 error(filename, linenum, 'build/include', 4,
4555 'Include the directory when naming .h files')
4556
4557 # we shouldn't include a file more than once. actually, there are a
4558 # handful of instances where doing so is okay, but in general it's
4559 # not.
erg@google.com6317a9c2009-06-25 00:28:19 +00004560 match = _RE_PATTERN_INCLUDE.search(line)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004561 if match:
4562 include = match.group(2)
4563 is_system = (match.group(1) == '<')
avakulenko@google.com59146752014-08-11 20:20:55 +00004564 duplicate_line = include_state.FindHeader(include)
4565 if duplicate_line >= 0:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004566 error(filename, linenum, 'build/include', 4,
4567 '"%s" already included at %s:%s' %
avakulenko@google.com59146752014-08-11 20:20:55 +00004568 (include, filename, duplicate_line))
avakulenko@google.com255f2be2014-12-05 22:19:55 +00004569 elif (include.endswith('.cc') and
4570 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)):
4571 error(filename, linenum, 'build/include', 4,
4572 'Do not include .cc files from other packages')
avakulenko@google.com59146752014-08-11 20:20:55 +00004573 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4574 include_state.include_list[-1].append((include, linenum))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004575
4576 # We want to ensure that headers appear in the right order:
4577 # 1) for foo.cc, foo.h (preferred location)
4578 # 2) c system files
4579 # 3) cpp system files
4580 # 4) for foo.cc, foo.h (deprecated location)
4581 # 5) other google headers
4582 #
4583 # We classify each include statement as one of those 5 types
4584 # using a number of techniques. The include_state object keeps
4585 # track of the highest type seen, and complains if we see a
4586 # lower type after that.
4587 error_message = include_state.CheckNextIncludeOrder(
4588 _ClassifyInclude(fileinfo, include, is_system))
4589 if error_message:
4590 error(filename, linenum, 'build/include_order', 4,
4591 '%s. Should be: %s.h, c system, c++ system, other.' %
4592 (error_message, fileinfo.BaseName()))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004593 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4594 if not include_state.IsInAlphabeticalOrder(
4595 clean_lines, linenum, canonical_include):
erg@google.com26970fa2009-11-17 18:07:32 +00004596 error(filename, linenum, 'build/include_alpha', 4,
4597 'Include "%s" not in alphabetical order' % include)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004598 include_state.SetLastHeader(canonical_include)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004599
erg@google.com6317a9c2009-06-25 00:28:19 +00004600
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004601
4602def _GetTextInside(text, start_pattern):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004603 r"""Retrieves all the text between matching open and close parentheses.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004604
4605 Given a string of lines and a regular expression string, retrieve all the text
4606 following the expression and between opening punctuation symbols like
4607 (, [, or {, and the matching close-punctuation symbol. This properly nested
4608 occurrences of the punctuations, so for the text like
4609 printf(a(), b(c()));
4610 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4611 start_pattern must match string having an open punctuation symbol at the end.
4612
4613 Args:
4614 text: The lines to extract text. Its comments and strings must be elided.
4615 It can be single line and can span multiple lines.
4616 start_pattern: The regexp string indicating where to start extracting
4617 the text.
4618 Returns:
4619 The extracted text.
4620 None if either the opening string or ending punctuation could not be found.
4621 """
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004622 # TODO(unknown): Audit cpplint.py to see what places could be profitably
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004623 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4624
4625 # Give opening punctuations to get the matching close-punctuations.
4626 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4627 closing_punctuation = set(matching_punctuation.itervalues())
4628
4629 # Find the position to start extracting text.
4630 match = re.search(start_pattern, text, re.M)
4631 if not match: # start_pattern not found in text.
4632 return None
4633 start_position = match.end(0)
4634
4635 assert start_position > 0, (
4636 'start_pattern must ends with an opening punctuation.')
4637 assert text[start_position - 1] in matching_punctuation, (
4638 'start_pattern must ends with an opening punctuation.')
4639 # Stack of closing punctuations we expect to have in text after position.
4640 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4641 position = start_position
4642 while punctuation_stack and position < len(text):
4643 if text[position] == punctuation_stack[-1]:
4644 punctuation_stack.pop()
4645 elif text[position] in closing_punctuation:
4646 # A closing punctuation without matching opening punctuations.
4647 return None
4648 elif text[position] in matching_punctuation:
4649 punctuation_stack.append(matching_punctuation[text[position]])
4650 position += 1
4651 if punctuation_stack:
4652 # Opening punctuations left without matching close-punctuations.
4653 return None
4654 # punctuations match.
4655 return text[start_position:position - 1]
4656
4657
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004658# Patterns for matching call-by-reference parameters.
4659#
4660# Supports nested templates up to 2 levels deep using this messy pattern:
4661# < (?: < (?: < [^<>]*
4662# >
4663# | [^<>] )*
4664# >
4665# | [^<>] )*
4666# >
4667_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4668_RE_PATTERN_TYPE = (
4669 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
4670 r'(?:\w|'
4671 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4672 r'::)+')
4673# A call-by-reference parameter ends with '& identifier'.
4674_RE_PATTERN_REF_PARAM = re.compile(
4675 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4676 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4677# A call-by-const-reference parameter either ends with 'const& identifier'
4678# or looks like 'const type& identifier' when 'type' is atomic.
4679_RE_PATTERN_CONST_REF_PARAM = (
4680 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4681 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004682# Stream types.
4683_RE_PATTERN_REF_STREAM_PARAM = (
4684 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')')
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004685
4686
4687def CheckLanguage(filename, clean_lines, linenum, file_extension,
4688 include_state, nesting_state, error):
erg@google.com6317a9c2009-06-25 00:28:19 +00004689 """Checks rules from the 'C++ language rules' section of cppguide.html.
4690
4691 Some of these rules are hard to test (function overloading, using
4692 uint32 inappropriately), but we do the best we can.
4693
4694 Args:
4695 filename: The name of the current file.
4696 clean_lines: A CleansedLines instance containing the file.
4697 linenum: The number of the line to check.
4698 file_extension: The extension (without the dot) of the filename.
4699 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004700 nesting_state: A NestingState instance which maintains information about
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004701 the current stack of nested blocks being parsed.
erg@google.com6317a9c2009-06-25 00:28:19 +00004702 error: The function to call with any errors found.
4703 """
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004704 # If the line is empty or consists of entirely a comment, no need to
4705 # check it.
4706 line = clean_lines.elided[linenum]
4707 if not line:
4708 return
4709
erg@google.com6317a9c2009-06-25 00:28:19 +00004710 match = _RE_PATTERN_INCLUDE.search(line)
4711 if match:
4712 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4713 return
4714
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00004715 # Reset include state across preprocessor directives. This is meant
4716 # to silence warnings for conditional includes.
avakulenko@google.com59146752014-08-11 20:20:55 +00004717 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4718 if match:
4719 include_state.ResetSection(match.group(1))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004720
4721 # Make Windows paths like Unix.
4722 fullname = os.path.abspath(filename).replace('\\', '/')
skym@chromium.org3990c412016-02-05 20:55:12 +00004723
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004724 # Perform other checks now that we are sure that this is not an include line
4725 CheckCasts(filename, clean_lines, linenum, error)
4726 CheckGlobalStatic(filename, clean_lines, linenum, error)
4727 CheckPrintf(filename, clean_lines, linenum, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004728
Amin Hassanif7e1e102018-06-21 20:13:30 +00004729 if IsHeaderExtension(file_extension):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004730 # TODO(unknown): check that 1-arg constructors are explicit.
4731 # How to tell it's a constructor?
4732 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.com59146752014-08-11 20:20:55 +00004733 # TODO(unknown): check that classes declare or disable copy/assign
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004734 # (level 1 error)
4735 pass
4736
4737 # Check if people are using the verboten C basic types. The only exception
4738 # we regularly allow is "unsigned short port" for port.
4739 if Search(r'\bshort port\b', line):
4740 if not Search(r'\bunsigned short port\b', line):
4741 error(filename, linenum, 'runtime/int', 4,
4742 'Use "unsigned short" for ports, not "short"')
4743 else:
4744 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4745 if match:
4746 error(filename, linenum, 'runtime/int', 4,
4747 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4748
erg@google.com26970fa2009-11-17 18:07:32 +00004749 # Check if some verboten operator overloading is going on
4750 # TODO(unknown): catch out-of-line unary operator&:
4751 # class X {};
4752 # int operator&(const X& x) { return 42; } // unary operator&
4753 # The trick is it's hard to tell apart from binary operator&:
4754 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4755 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4756 error(filename, linenum, 'runtime/operator', 4,
4757 'Unary operator& is dangerous. Do not use it.')
4758
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004759 # Check for suspicious usage of "if" like
4760 # } if (a == b) {
4761 if Search(r'\}\s*if\s*\(', line):
4762 error(filename, linenum, 'readability/braces', 4,
4763 'Did you mean "else if"? If not, start a new line for "if".')
4764
4765 # Check for potential format string bugs like printf(foo).
4766 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4767 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004768 # TODO(unknown): Catch the following case. Need to change the calling
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004769 # convention of the whole function to process multiple line to handle it.
4770 # printf(
4771 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4772 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4773 if printf_args:
4774 match = Match(r'([\w.\->()]+)$', printf_args)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00004775 if match and match.group(1) != '__VA_ARGS__':
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004776 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4777 line, re.I).group(1)
4778 error(filename, linenum, 'runtime/printf', 4,
4779 'Potential format string bug. Do %s("%%s", %s) instead.'
4780 % (function_name, match.group(1)))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004781
4782 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4783 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4784 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4785 error(filename, linenum, 'runtime/memset', 4,
4786 'Did you mean "memset(%s, 0, %s)"?'
4787 % (match.group(1), match.group(2)))
4788
4789 if Search(r'\busing namespace\b', line):
4790 error(filename, linenum, 'build/namespaces', 5,
4791 'Do not use namespace using-directives. '
4792 'Use using-declarations instead.')
4793
4794 # Detect variable-length arrays.
4795 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4796 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4797 match.group(3).find(']') == -1):
4798 # Split the size using space and arithmetic operators as delimiters.
4799 # If any of the resulting tokens are not compile time constants then
4800 # report the error.
4801 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4802 is_const = True
4803 skip_next = False
4804 for tok in tokens:
4805 if skip_next:
4806 skip_next = False
4807 continue
4808
4809 if Search(r'sizeof\(.+\)', tok): continue
4810 if Search(r'arraysize\(\w+\)', tok): continue
4811
4812 tok = tok.lstrip('(')
4813 tok = tok.rstrip(')')
4814 if not tok: continue
4815 if Match(r'\d+', tok): continue
4816 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4817 if Match(r'k[A-Z0-9]\w*', tok): continue
4818 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4819 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4820 # A catch all for tricky sizeof cases, including 'sizeof expression',
4821 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00004822 # requires skipping the next token because we split on ' ' and '*'.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004823 if tok.startswith('sizeof'):
4824 skip_next = True
4825 continue
4826 is_const = False
4827 break
4828 if not is_const:
4829 error(filename, linenum, 'runtime/arrays', 1,
4830 'Do not use variable-length arrays. Use an appropriately named '
4831 "('k' followed by CamelCase) compile-time constant for the size.")
4832
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004833 # Check for use of unnamed namespaces in header files. Registration
4834 # macros are typically OK, so we allow use of "namespace {" on lines
4835 # that end with backslashes.
Amin Hassanif7e1e102018-06-21 20:13:30 +00004836 if (IsHeaderExtension(file_extension)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004837 and Search(r'\bnamespace\s*{', line)
4838 and line[-1] != '\\'):
4839 error(filename, linenum, 'build/namespaces', 4,
4840 'Do not use unnamed namespaces in header files. See '
Amin Hassanif7e1e102018-06-21 20:13:30 +00004841 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00004842 ' for more information.')
4843
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004844
4845def CheckGlobalStatic(filename, clean_lines, linenum, error):
4846 """Check for unsafe global or static objects.
4847
4848 Args:
4849 filename: The name of the current file.
4850 clean_lines: A CleansedLines instance containing the file.
4851 linenum: The number of the line to check.
4852 error: The function to call with any errors found.
4853 """
4854 line = clean_lines.elided[linenum]
4855
avakulenko@google.com59146752014-08-11 20:20:55 +00004856 # Match two lines at a time to support multiline declarations
4857 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4858 line += clean_lines.elided[linenum + 1].strip()
4859
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004860 # Check for people declaring static/global STL strings at the top level.
4861 # This is dangerous because the C++ language does not guarantee that
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004862 # globals with constructors are initialized before the first access, and
4863 # also because globals can be destroyed when some threads are still running.
4864 # TODO(unknown): Generalize this to also find static unique_ptr instances.
4865 # TODO(unknown): File bugs for clang-tidy to find these.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004866 match = Match(
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004867 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +'
4868 r'([a-zA-Z0-9_:]+)\b(.*)',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004869 line)
avakulenko@google.com59146752014-08-11 20:20:55 +00004870
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004871 # Remove false positives:
4872 # - String pointers (as opposed to values).
4873 # string *pointer
4874 # const string *pointer
4875 # string const *pointer
4876 # string *const pointer
4877 #
4878 # - Functions and template specializations.
4879 # string Function<Type>(...
4880 # string Class<Type>::Method(...
4881 #
4882 # - Operators. These are matched separately because operator names
4883 # cross non-word boundaries, and trying to match both operators
4884 # and functions at the same time would decrease accuracy of
4885 # matching identifiers.
4886 # string Class::operator*()
4887 if (match and
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004888 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004889 not Search(r'\boperator\W', line) and
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004890 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))):
4891 if Search(r'\bconst\b', line):
4892 error(filename, linenum, 'runtime/string', 4,
4893 'For a static/global string constant, use a C style string '
4894 'instead: "%schar%s %s[]".' %
4895 (match.group(1), match.group(2) or '', match.group(3)))
4896 else:
4897 error(filename, linenum, 'runtime/string', 4,
4898 'Static/global string variables are not permitted.')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004899
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00004900 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or
4901 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004902 error(filename, linenum, 'runtime/init', 4,
4903 'You seem to be initializing a member variable with itself.')
4904
4905
4906def CheckPrintf(filename, clean_lines, linenum, error):
4907 """Check for printf related issues.
4908
4909 Args:
4910 filename: The name of the current file.
4911 clean_lines: A CleansedLines instance containing the file.
4912 linenum: The number of the line to check.
4913 error: The function to call with any errors found.
4914 """
4915 line = clean_lines.elided[linenum]
4916
4917 # When snprintf is used, the second argument shouldn't be a literal.
4918 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4919 if match and match.group(2) != '0':
4920 # If 2nd arg is zero, snprintf is used to calculate size.
4921 error(filename, linenum, 'runtime/printf', 3,
4922 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4923 'to snprintf.' % (match.group(1), match.group(2)))
4924
4925 # Check if some verboten C functions are being used.
avakulenko@google.com59146752014-08-11 20:20:55 +00004926 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004927 error(filename, linenum, 'runtime/printf', 5,
4928 'Never use sprintf. Use snprintf instead.')
avakulenko@google.com59146752014-08-11 20:20:55 +00004929 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004930 if match:
4931 error(filename, linenum, 'runtime/printf', 4,
4932 'Almost always, snprintf is better than %s' % match.group(1))
4933
4934
4935def IsDerivedFunction(clean_lines, linenum):
4936 """Check if current line contains an inherited function.
4937
4938 Args:
4939 clean_lines: A CleansedLines instance containing the file.
4940 linenum: The number of the line to check.
4941 Returns:
4942 True if current line contains a function with "override"
4943 virt-specifier.
4944 """
avakulenko@google.com59146752014-08-11 20:20:55 +00004945 # Scan back a few lines for start of current function
4946 for i in xrange(linenum, max(-1, linenum - 10), -1):
4947 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4948 if match:
4949 # Look for "override" after the matching closing parenthesis
4950 line, _, closing_paren = CloseExpression(
4951 clean_lines, i, len(match.group(1)))
4952 return (closing_paren >= 0 and
4953 Search(r'\boverride\b', line[closing_paren:]))
4954 return False
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004955
4956
avakulenko@google.com255f2be2014-12-05 22:19:55 +00004957def IsOutOfLineMethodDefinition(clean_lines, linenum):
4958 """Check if current line contains an out-of-line method definition.
4959
4960 Args:
4961 clean_lines: A CleansedLines instance containing the file.
4962 linenum: The number of the line to check.
4963 Returns:
4964 True if current line contains an out-of-line method definition.
4965 """
4966 # Scan back a few lines for start of current function
4967 for i in xrange(linenum, max(-1, linenum - 10), -1):
4968 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
4969 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
4970 return False
4971
4972
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00004973def IsInitializerList(clean_lines, linenum):
4974 """Check if current line is inside constructor initializer list.
4975
4976 Args:
4977 clean_lines: A CleansedLines instance containing the file.
4978 linenum: The number of the line to check.
4979 Returns:
4980 True if current line appears to be inside constructor initializer
4981 list, False otherwise.
4982 """
4983 for i in xrange(linenum, 1, -1):
4984 line = clean_lines.elided[i]
4985 if i == linenum:
4986 remove_function_body = Match(r'^(.*)\{\s*$', line)
4987 if remove_function_body:
4988 line = remove_function_body.group(1)
4989
4990 if Search(r'\s:\s*\w+[({]', line):
4991 # A lone colon tend to indicate the start of a constructor
4992 # initializer list. It could also be a ternary operator, which
4993 # also tend to appear in constructor initializer lists as
4994 # opposed to parameter lists.
4995 return True
4996 if Search(r'\}\s*,\s*$', line):
4997 # A closing brace followed by a comma is probably the end of a
4998 # brace-initialized member in constructor initializer list.
4999 return True
5000 if Search(r'[{};]\s*$', line):
5001 # Found one of the following:
5002 # - A closing brace or semicolon, probably the end of the previous
5003 # function.
5004 # - An opening brace, probably the start of current class or namespace.
5005 #
5006 # Current line is probably not inside an initializer list since
5007 # we saw one of those things without seeing the starting colon.
5008 return False
5009
5010 # Got to the beginning of the file without seeing the start of
5011 # constructor initializer list.
5012 return False
5013
5014
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005015def CheckForNonConstReference(filename, clean_lines, linenum,
5016 nesting_state, error):
5017 """Check for non-const references.
5018
5019 Separate from CheckLanguage since it scans backwards from current
5020 line, instead of scanning forward.
5021
5022 Args:
5023 filename: The name of the current file.
5024 clean_lines: A CleansedLines instance containing the file.
5025 linenum: The number of the line to check.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005026 nesting_state: A NestingState instance which maintains information about
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005027 the current stack of nested blocks being parsed.
5028 error: The function to call with any errors found.
5029 """
5030 # Do nothing if there is no '&' on current line.
5031 line = clean_lines.elided[linenum]
5032 if '&' not in line:
5033 return
5034
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005035 # If a function is inherited, current function doesn't have much of
5036 # a choice, so any non-const references should not be blamed on
5037 # derived function.
5038 if IsDerivedFunction(clean_lines, linenum):
5039 return
5040
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005041 # Don't warn on out-of-line method definitions, as we would warn on the
5042 # in-line declaration, if it isn't marked with 'override'.
5043 if IsOutOfLineMethodDefinition(clean_lines, linenum):
5044 return
5045
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005046 # Long type names may be broken across multiple lines, usually in one
5047 # of these forms:
5048 # LongType
5049 # ::LongTypeContinued &identifier
5050 # LongType::
5051 # LongTypeContinued &identifier
5052 # LongType<
5053 # ...>::LongTypeContinued &identifier
5054 #
5055 # If we detected a type split across two lines, join the previous
5056 # line to current line so that we can match const references
5057 # accordingly.
5058 #
5059 # Note that this only scans back one line, since scanning back
5060 # arbitrary number of lines would be expensive. If you have a type
5061 # that spans more than 2 lines, please use a typedef.
5062 if linenum > 1:
5063 previous = None
5064 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
5065 # previous_line\n + ::current_line
5066 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
5067 clean_lines.elided[linenum - 1])
5068 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
5069 # previous_line::\n + current_line
5070 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
5071 clean_lines.elided[linenum - 1])
5072 if previous:
5073 line = previous.group(1) + line.lstrip()
5074 else:
5075 # Check for templated parameter that is split across multiple lines
5076 endpos = line.rfind('>')
5077 if endpos > -1:
5078 (_, startline, startpos) = ReverseCloseExpression(
5079 clean_lines, linenum, endpos)
5080 if startpos > -1 and startline < linenum:
5081 # Found the matching < on an earlier line, collect all
5082 # pieces up to current line.
5083 line = ''
5084 for i in xrange(startline, linenum + 1):
5085 line += clean_lines.elided[i].strip()
5086
5087 # Check for non-const references in function parameters. A single '&' may
5088 # found in the following places:
5089 # inside expression: binary & for bitwise AND
5090 # inside expression: unary & for taking the address of something
5091 # inside declarators: reference parameter
5092 # We will exclude the first two cases by checking that we are not inside a
5093 # function body, including one that was just introduced by a trailing '{'.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005094 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005095 if (nesting_state.previous_stack_top and
5096 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
5097 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
5098 # Not at toplevel, not within a class, and not within a namespace
5099 return
5100
avakulenko@google.com59146752014-08-11 20:20:55 +00005101 # Avoid initializer lists. We only need to scan back from the
5102 # current line for something that starts with ':'.
5103 #
5104 # We don't need to check the current line, since the '&' would
5105 # appear inside the second set of parentheses on the current line as
5106 # opposed to the first set.
5107 if linenum > 0:
5108 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5109 previous_line = clean_lines.elided[i]
5110 if not Search(r'[),]\s*$', previous_line):
5111 break
5112 if Match(r'^\s*:\s+\S', previous_line):
5113 return
5114
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005115 # Avoid preprocessors
5116 if Search(r'\\\s*$', line):
5117 return
5118
5119 # Avoid constructor initializer lists
5120 if IsInitializerList(clean_lines, linenum):
5121 return
5122
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005123 # We allow non-const references in a few standard places, like functions
5124 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5125 # those function parameters.
5126 #
5127 # We also accept & in static_assert, which looks like a function but
5128 # it's actually a declaration expression.
5129 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5130 r'operator\s*[<>][<>]|'
5131 r'static_assert|COMPILE_ASSERT'
5132 r')\s*\(')
5133 if Search(whitelisted_functions, line):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005134 return
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005135 elif not Search(r'\S+\([^)]*$', line):
5136 # Don't see a whitelisted function on this line. Actually we
5137 # didn't see any function name on this line, so this is likely a
5138 # multi-line parameter list. Try a bit harder to catch this case.
5139 for i in xrange(2):
5140 if (linenum > i and
5141 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005142 return
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005143
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005144 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5145 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005146 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and
5147 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005148 error(filename, linenum, 'runtime/references', 2,
5149 'Is this a non-const reference? '
5150 'If so, make const or use a pointer: ' +
5151 ReplaceAll(' *<', '<', parameter))
5152
5153
5154def CheckCasts(filename, clean_lines, linenum, error):
5155 """Various cast related checks.
5156
5157 Args:
5158 filename: The name of the current file.
5159 clean_lines: A CleansedLines instance containing the file.
5160 linenum: The number of the line to check.
5161 error: The function to call with any errors found.
5162 """
5163 line = clean_lines.elided[linenum]
5164
5165 # Check to see if they're using an conversion function cast.
5166 # I just try to capture the most common basic types, though there are more.
5167 # Parameterless conversion functions, such as bool(), are allowed as they are
5168 # probably a member operator declaration or default constructor.
5169 match = Search(
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005170 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005171 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5172 r'(\([^)].*)', line)
5173 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5174 if match and not expecting_function:
5175 matched_type = match.group(2)
5176
5177 # matched_new_or_template is used to silence two false positives:
5178 # - New operators
5179 # - Template arguments with function types
5180 #
5181 # For template arguments, we match on types immediately following
5182 # an opening bracket without any spaces. This is a fast way to
5183 # silence the common case where the function type is the first
5184 # template argument. False negative with less-than comparison is
5185 # avoided because those operators are usually followed by a space.
5186 #
5187 # function<double(double)> // bracket + no space = false positive
5188 # value < double(42) // bracket + space = true positive
5189 matched_new_or_template = match.group(1)
5190
avakulenko@google.com59146752014-08-11 20:20:55 +00005191 # Avoid arrays by looking for brackets that come after the closing
5192 # parenthesis.
5193 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5194 return
5195
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005196 # Other things to ignore:
5197 # - Function pointers
5198 # - Casts to pointer types
5199 # - Placement new
5200 # - Alias declarations
5201 matched_funcptr = match.group(3)
5202 if (matched_new_or_template is None and
5203 not (matched_funcptr and
5204 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5205 matched_funcptr) or
5206 matched_funcptr.startswith('(*)'))) and
5207 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5208 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5209 error(filename, linenum, 'readability/casting', 4,
5210 'Using deprecated casting style. '
5211 'Use static_cast<%s>(...) instead' %
5212 matched_type)
5213
5214 if not expecting_function:
avakulenko@google.com59146752014-08-11 20:20:55 +00005215 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005216 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5217
5218 # This doesn't catch all cases. Consider (const char * const)"hello".
5219 #
5220 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5221 # compile).
avakulenko@google.com59146752014-08-11 20:20:55 +00005222 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5223 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005224 pass
5225 else:
5226 # Check pointer casts for other than string constants
avakulenko@google.com59146752014-08-11 20:20:55 +00005227 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5228 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005229
5230 # In addition, we look for people taking the address of a cast. This
5231 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5232 # point where you think.
avakulenko@google.com59146752014-08-11 20:20:55 +00005233 #
5234 # Some non-identifier character is required before the '&' for the
5235 # expression to be recognized as a cast. These are casts:
5236 # expression = &static_cast<int*>(temporary());
5237 # function(&(int*)(temporary()));
5238 #
5239 # This is not a cast:
5240 # reference_type&(int* function_param);
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005241 match = Search(
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005242 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|'
avakulenko@google.com59146752014-08-11 20:20:55 +00005243 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005244 if match:
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005245 # Try a better error message when the & is bound to something
5246 # dereferenced by the casted pointer, as opposed to the casted
5247 # pointer itself.
5248 parenthesis_error = False
5249 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5250 if match:
5251 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5252 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5253 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5254 if x2 >= 0:
5255 extended_line = clean_lines.elided[y2][x2:]
5256 if y2 < clean_lines.NumLines() - 1:
5257 extended_line += clean_lines.elided[y2 + 1]
5258 if Match(r'\s*(?:->|\[)', extended_line):
5259 parenthesis_error = True
5260
5261 if parenthesis_error:
5262 error(filename, linenum, 'readability/casting', 4,
5263 ('Are you taking an address of something dereferenced '
5264 'from a cast? Wrapping the dereferenced expression in '
5265 'parentheses will make the binding more obvious'))
5266 else:
5267 error(filename, linenum, 'runtime/casting', 4,
5268 ('Are you taking an address of a cast? '
5269 'This is dangerous: could be a temp var. '
5270 'Take the address before doing the cast, rather than after'))
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005271
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005272
avakulenko@google.com59146752014-08-11 20:20:55 +00005273def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005274 """Checks for a C-style cast by looking for the pattern.
5275
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005276 Args:
5277 filename: The name of the current file.
avakulenko@google.com59146752014-08-11 20:20:55 +00005278 clean_lines: A CleansedLines instance containing the file.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005279 linenum: The number of the line to check.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005280 cast_type: The string for the C++ cast to recommend. This is either
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005281 reinterpret_cast, static_cast, or const_cast, depending.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005282 pattern: The regular expression used to find C-style casts.
5283 error: The function to call with any errors found.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005284
5285 Returns:
5286 True if an error was emitted.
5287 False otherwise.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005288 """
avakulenko@google.com59146752014-08-11 20:20:55 +00005289 line = clean_lines.elided[linenum]
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005290 match = Search(pattern, line)
5291 if not match:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005292 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005293
avakulenko@google.com59146752014-08-11 20:20:55 +00005294 # Exclude lines with keywords that tend to look like casts
5295 context = line[0:match.start(1) - 1]
5296 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5297 return False
5298
5299 # Try expanding current context to see if we one level of
5300 # parentheses inside a macro.
5301 if linenum > 0:
5302 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5303 context = clean_lines.elided[i] + context
5304 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005305 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005306
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005307 # operator++(int) and operator--(int)
avakulenko@google.com59146752014-08-11 20:20:55 +00005308 if context.endswith(' operator++') or context.endswith(' operator--'):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005309 return False
5310
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005311 # A single unnamed argument for a function tends to look like old style cast.
5312 # If we see those, don't issue warnings for deprecated casts.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005313 remainder = line[match.end(0):]
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005314 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005315 remainder):
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005316 return False
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005317
5318 # At this point, all that should be left is actual casts.
5319 error(filename, linenum, 'readability/casting', 4,
5320 'Using C-style cast. Use %s<%s>(...) instead' %
5321 (cast_type, match.group(1)))
5322
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005323 return True
5324
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005325
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005326def ExpectingFunctionArgs(clean_lines, linenum):
5327 """Checks whether where function type arguments are expected.
5328
5329 Args:
5330 clean_lines: A CleansedLines instance containing the file.
5331 linenum: The number of the line to check.
5332
5333 Returns:
5334 True if the line at 'linenum' is inside something that expects arguments
5335 of function types.
5336 """
5337 line = clean_lines.elided[linenum]
5338 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5339 (linenum >= 2 and
5340 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5341 clean_lines.elided[linenum - 1]) or
5342 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5343 clean_lines.elided[linenum - 2]) or
5344 Search(r'\bstd::m?function\s*\<\s*$',
5345 clean_lines.elided[linenum - 1]))))
5346
5347
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005348_HEADERS_CONTAINING_TEMPLATES = (
5349 ('<deque>', ('deque',)),
5350 ('<functional>', ('unary_function', 'binary_function',
5351 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5352 'negate',
5353 'equal_to', 'not_equal_to', 'greater', 'less',
5354 'greater_equal', 'less_equal',
5355 'logical_and', 'logical_or', 'logical_not',
5356 'unary_negate', 'not1', 'binary_negate', 'not2',
5357 'bind1st', 'bind2nd',
5358 'pointer_to_unary_function',
5359 'pointer_to_binary_function',
5360 'ptr_fun',
5361 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5362 'mem_fun_ref_t',
5363 'const_mem_fun_t', 'const_mem_fun1_t',
5364 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5365 'mem_fun_ref',
5366 )),
5367 ('<limits>', ('numeric_limits',)),
5368 ('<list>', ('list',)),
5369 ('<map>', ('map', 'multimap',)),
lhchavez2d1b6da2016-07-13 10:40:01 -07005370 ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr',
5371 'unique_ptr', 'weak_ptr')),
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005372 ('<queue>', ('queue', 'priority_queue',)),
5373 ('<set>', ('set', 'multiset',)),
5374 ('<stack>', ('stack',)),
5375 ('<string>', ('char_traits', 'basic_string',)),
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005376 ('<tuple>', ('tuple',)),
lhchavez2d1b6da2016-07-13 10:40:01 -07005377 ('<unordered_map>', ('unordered_map', 'unordered_multimap')),
5378 ('<unordered_set>', ('unordered_set', 'unordered_multiset')),
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005379 ('<utility>', ('pair',)),
5380 ('<vector>', ('vector',)),
5381
5382 # gcc extensions.
5383 # Note: std::hash is their hash, ::hash is our hash
5384 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5385 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5386 ('<slist>', ('slist',)),
5387 )
5388
skym@chromium.org3990c412016-02-05 20:55:12 +00005389_HEADERS_MAYBE_TEMPLATES = (
5390 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort',
5391 'transform',
5392 )),
lhchavez2d1b6da2016-07-13 10:40:01 -07005393 ('<utility>', ('forward', 'make_pair', 'move', 'swap')),
skym@chromium.org3990c412016-02-05 20:55:12 +00005394 )
5395
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005396_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5397
skym@chromium.org3990c412016-02-05 20:55:12 +00005398_re_pattern_headers_maybe_templates = []
5399for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
5400 for _template in _templates:
5401 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5402 # type::max().
5403 _re_pattern_headers_maybe_templates.append(
5404 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5405 _template,
5406 _header))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005407
skym@chromium.org3990c412016-02-05 20:55:12 +00005408# Other scripts may reach in and modify this pattern.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005409_re_pattern_templates = []
5410for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5411 for _template in _templates:
5412 _re_pattern_templates.append(
5413 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5414 _template + '<>',
5415 _header))
5416
5417
erg@google.com6317a9c2009-06-25 00:28:19 +00005418def FilesBelongToSameModule(filename_cc, filename_h):
5419 """Check if these two filenames belong to the same module.
5420
5421 The concept of a 'module' here is a as follows:
5422 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5423 same 'module' if they are in the same directory.
5424 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5425 to belong to the same module here.
5426
5427 If the filename_cc contains a longer path than the filename_h, for example,
5428 '/absolute/path/to/base/sysinfo.cc', and this file would include
5429 'base/sysinfo.h', this function also produces the prefix needed to open the
5430 header. This is used by the caller of this function to more robustly open the
5431 header file. We don't have access to the real include paths in this context,
5432 so we need this guesswork here.
5433
5434 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5435 according to this implementation. Because of this, this function gives
5436 some false positives. This should be sufficiently rare in practice.
5437
5438 Args:
5439 filename_cc: is the path for the .cc file
5440 filename_h: is the path for the header path
5441
5442 Returns:
5443 Tuple with a bool and a string:
5444 bool: True if filename_cc and filename_h belong to the same module.
5445 string: the additional prefix needed to open the header file.
5446 """
5447
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005448 fileinfo = FileInfo(filename_cc)
5449 if not fileinfo.IsSource():
erg@google.com6317a9c2009-06-25 00:28:19 +00005450 return (False, '')
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005451 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5452 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5453 if matched_test_suffix:
5454 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
erg@google.com6317a9c2009-06-25 00:28:19 +00005455 filename_cc = filename_cc.replace('/public/', '/')
5456 filename_cc = filename_cc.replace('/internal/', '/')
5457
5458 if not filename_h.endswith('.h'):
5459 return (False, '')
5460 filename_h = filename_h[:-len('.h')]
5461 if filename_h.endswith('-inl'):
5462 filename_h = filename_h[:-len('-inl')]
5463 filename_h = filename_h.replace('/public/', '/')
5464 filename_h = filename_h.replace('/internal/', '/')
5465
5466 files_belong_to_same_module = filename_cc.endswith(filename_h)
5467 common_path = ''
5468 if files_belong_to_same_module:
5469 common_path = filename_cc[:-len(filename_h)]
5470 return files_belong_to_same_module, common_path
5471
5472
avakulenko@google.com59146752014-08-11 20:20:55 +00005473def UpdateIncludeState(filename, include_dict, io=codecs):
5474 """Fill up the include_dict with new includes found from the file.
erg@google.com6317a9c2009-06-25 00:28:19 +00005475
5476 Args:
5477 filename: the name of the header to read.
avakulenko@google.com59146752014-08-11 20:20:55 +00005478 include_dict: a dictionary in which the headers are inserted.
erg@google.com6317a9c2009-06-25 00:28:19 +00005479 io: The io factory to use to read the file. Provided for testability.
5480
5481 Returns:
avakulenko@google.com59146752014-08-11 20:20:55 +00005482 True if a header was successfully added. False otherwise.
erg@google.com6317a9c2009-06-25 00:28:19 +00005483 """
5484 headerfile = None
5485 try:
5486 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5487 except IOError:
5488 return False
5489 linenum = 0
5490 for line in headerfile:
5491 linenum += 1
5492 clean_line = CleanseComments(line)
5493 match = _RE_PATTERN_INCLUDE.search(clean_line)
5494 if match:
5495 include = match.group(2)
avakulenko@google.com59146752014-08-11 20:20:55 +00005496 include_dict.setdefault(include, linenum)
erg@google.com6317a9c2009-06-25 00:28:19 +00005497 return True
5498
5499
5500def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5501 io=codecs):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005502 """Reports for missing stl includes.
5503
5504 This function will output warnings to make sure you are including the headers
5505 necessary for the stl containers and functions that you use. We only give one
5506 reason to include a header. For example, if you use both equal_to<> and
5507 less<> in a .h file, only one (the latter in the file) of these will be
5508 reported as a reason to include the <functional>.
5509
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005510 Args:
5511 filename: The name of the current file.
5512 clean_lines: A CleansedLines instance containing the file.
5513 include_state: An _IncludeState instance.
5514 error: The function to call with any errors found.
erg@google.com6317a9c2009-06-25 00:28:19 +00005515 io: The IO factory to use to read the header file. Provided for unittest
5516 injection.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005517 """
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005518 required = {} # A map of header name to linenumber and the template entity.
5519 # Example of required: { '<functional>': (1219, 'less<>') }
5520
5521 for linenum in xrange(clean_lines.NumLines()):
5522 line = clean_lines.elided[linenum]
5523 if not line or line[0] == '#':
5524 continue
5525
5526 # String is special -- it is a non-templatized type in STL.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005527 matched = _RE_PATTERN_STRING.search(line)
5528 if matched:
erg@google.com35589e62010-11-17 18:58:16 +00005529 # Don't warn about strings in non-STL namespaces:
5530 # (We check only the first match per line; good enough.)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005531 prefix = line[:matched.start()]
erg@google.com35589e62010-11-17 18:58:16 +00005532 if prefix.endswith('std::') or not prefix.endswith('::'):
5533 required['<string>'] = (linenum, 'string')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005534
skym@chromium.org3990c412016-02-05 20:55:12 +00005535 for pattern, template, header in _re_pattern_headers_maybe_templates:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005536 if pattern.search(line):
5537 required[header] = (linenum, template)
5538
5539 # The following function is just a speed up, no semantics are changed.
5540 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5541 continue
5542
5543 for pattern, template, header in _re_pattern_templates:
lhchavez9b2173c2016-07-13 10:20:07 -07005544 matched = pattern.search(line)
5545 if matched:
5546 # Don't warn about IWYU in non-STL namespaces:
5547 # (We check only the first match per line; good enough.)
5548 prefix = line[:matched.start()]
5549 if prefix.endswith('std::') or not prefix.endswith('::'):
5550 required[header] = (linenum, template)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005551
erg@google.com6317a9c2009-06-25 00:28:19 +00005552 # The policy is that if you #include something in foo.h you don't need to
5553 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.com59146752014-08-11 20:20:55 +00005554 # Let's flatten the include_state include_list and copy it into a dictionary.
5555 include_dict = dict([item for sublist in include_state.include_list
5556 for item in sublist])
erg@google.com6317a9c2009-06-25 00:28:19 +00005557
avakulenko@google.com59146752014-08-11 20:20:55 +00005558 # Did we find the header for this file (if any) and successfully load it?
erg@google.com6317a9c2009-06-25 00:28:19 +00005559 header_found = False
5560
5561 # Use the absolute path so that matching works properly.
erg@chromium.org8f927562012-01-30 19:51:28 +00005562 abs_filename = FileInfo(filename).FullName()
erg@google.com6317a9c2009-06-25 00:28:19 +00005563
5564 # For Emacs's flymake.
5565 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5566 # by flymake and that file name might end with '_flymake.cc'. In that case,
5567 # restore original file name here so that the corresponding header file can be
5568 # found.
5569 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5570 # instead of 'foo_flymake.h'
erg@google.com35589e62010-11-17 18:58:16 +00005571 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.com6317a9c2009-06-25 00:28:19 +00005572
avakulenko@google.com59146752014-08-11 20:20:55 +00005573 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.com6317a9c2009-06-25 00:28:19 +00005574 # the keys.
avakulenko@google.com59146752014-08-11 20:20:55 +00005575 header_keys = include_dict.keys()
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005576 for header in header_keys:
erg@google.com6317a9c2009-06-25 00:28:19 +00005577 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5578 fullpath = common_path + header
avakulenko@google.com59146752014-08-11 20:20:55 +00005579 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.com6317a9c2009-06-25 00:28:19 +00005580 header_found = True
5581
5582 # If we can't find the header file for a .cc, assume it's because we don't
5583 # know where to look. In that case we'll give up as we're not sure they
5584 # didn't include it in the .h file.
5585 # TODO(unknown): Do a better job of finding .h files so we are confident that
5586 # not having the .h file means there isn't one.
5587 if filename.endswith('.cc') and not header_found:
5588 return
5589
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005590 # All the lines have been processed, report the errors found.
5591 for required_header_unstripped in required:
5592 template = required[required_header_unstripped][1]
avakulenko@google.com59146752014-08-11 20:20:55 +00005593 if required_header_unstripped.strip('<>"') not in include_dict:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005594 error(filename, required[required_header_unstripped][0],
5595 'build/include_what_you_use', 4,
5596 'Add #include ' + required_header_unstripped + ' for ' + template)
5597
5598
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005599_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5600
5601
5602def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5603 """Check that make_pair's template arguments are deduced.
5604
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005605 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005606 specified explicitly, and such use isn't intended in any case.
5607
5608 Args:
5609 filename: The name of the current file.
5610 clean_lines: A CleansedLines instance containing the file.
5611 linenum: The number of the line to check.
5612 error: The function to call with any errors found.
5613 """
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005614 line = clean_lines.elided[linenum]
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005615 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5616 if match:
5617 error(filename, linenum, 'build/explicit_make_pair',
5618 4, # 4 = high confidence
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005619 'For C++11-compatibility, omit template arguments from make_pair'
5620 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.com59146752014-08-11 20:20:55 +00005621
5622
avakulenko@google.com59146752014-08-11 20:20:55 +00005623def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5624 """Check if line contains a redundant "virtual" function-specifier.
5625
5626 Args:
5627 filename: The name of the current file.
5628 clean_lines: A CleansedLines instance containing the file.
5629 linenum: The number of the line to check.
5630 error: The function to call with any errors found.
5631 """
5632 # Look for "virtual" on current line.
5633 line = clean_lines.elided[linenum]
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005634 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line)
avakulenko@google.com59146752014-08-11 20:20:55 +00005635 if not virtual: return
5636
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005637 # Ignore "virtual" keywords that are near access-specifiers. These
5638 # are only used in class base-specifier and do not apply to member
5639 # functions.
5640 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or
5641 Match(r'^\s+(public|protected|private)\b', virtual.group(3))):
5642 return
5643
5644 # Ignore the "virtual" keyword from virtual base classes. Usually
5645 # there is a column on the same line in these cases (virtual base
5646 # classes are rare in google3 because multiple inheritance is rare).
5647 if Match(r'^.*[^:]:[^:].*$', line): return
5648
avakulenko@google.com59146752014-08-11 20:20:55 +00005649 # Look for the next opening parenthesis. This is the start of the
5650 # parameter list (possibly on the next line shortly after virtual).
5651 # TODO(unknown): doesn't work if there are virtual functions with
5652 # decltype() or other things that use parentheses, but csearch suggests
5653 # that this is rare.
5654 end_col = -1
5655 end_line = -1
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005656 start_col = len(virtual.group(2))
avakulenko@google.com59146752014-08-11 20:20:55 +00005657 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5658 line = clean_lines.elided[start_line][start_col:]
5659 parameter_list = Match(r'^([^(]*)\(', line)
5660 if parameter_list:
5661 # Match parentheses to find the end of the parameter list
5662 (_, end_line, end_col) = CloseExpression(
5663 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5664 break
5665 start_col = 0
5666
5667 if end_col < 0:
5668 return # Couldn't find end of parameter list, give up
5669
5670 # Look for "override" or "final" after the parameter list
5671 # (possibly on the next few lines).
5672 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5673 line = clean_lines.elided[i][end_col:]
5674 match = Search(r'\b(override|final)\b', line)
5675 if match:
5676 error(filename, linenum, 'readability/inheritance', 4,
5677 ('"virtual" is redundant since function is '
5678 'already declared as "%s"' % match.group(1)))
5679
5680 # Set end_col to check whole lines after we are done with the
5681 # first line.
5682 end_col = 0
5683 if Search(r'[^\w]\s*$', line):
5684 break
5685
5686
5687def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5688 """Check if line contains a redundant "override" or "final" virt-specifier.
5689
5690 Args:
5691 filename: The name of the current file.
5692 clean_lines: A CleansedLines instance containing the file.
5693 linenum: The number of the line to check.
5694 error: The function to call with any errors found.
5695 """
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005696 # Look for closing parenthesis nearby. We need one to confirm where
5697 # the declarator ends and where the virt-specifier starts to avoid
5698 # false positives.
avakulenko@google.com59146752014-08-11 20:20:55 +00005699 line = clean_lines.elided[linenum]
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005700 declarator_end = line.rfind(')')
5701 if declarator_end >= 0:
5702 fragment = line[declarator_end:]
5703 else:
5704 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0:
5705 fragment = line
5706 else:
5707 return
5708
5709 # Check that at most one of "override" or "final" is present, not both
5710 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment):
avakulenko@google.com59146752014-08-11 20:20:55 +00005711 error(filename, linenum, 'readability/inheritance', 4,
5712 ('"override" is redundant since function is '
5713 'already declared as "final"'))
5714
5715
5716
5717
5718# Returns true if we are at a new block, and it is directly
5719# inside of a namespace.
5720def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5721 """Checks that the new block is directly in a namespace.
5722
5723 Args:
5724 nesting_state: The _NestingState object that contains info about our state.
5725 is_forward_declaration: If the class is a forward declared class.
5726 Returns:
5727 Whether or not the new block is directly in a namespace.
5728 """
5729 if is_forward_declaration:
5730 if len(nesting_state.stack) >= 1 and (
5731 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5732 return True
5733 else:
5734 return False
5735
5736 return (len(nesting_state.stack) > 1 and
5737 nesting_state.stack[-1].check_namespace_indentation and
5738 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5739
5740
5741def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5742 raw_lines_no_comments, linenum):
5743 """This method determines if we should apply our namespace indentation check.
5744
5745 Args:
5746 nesting_state: The current nesting state.
5747 is_namespace_indent_item: If we just put a new class on the stack, True.
5748 If the top of the stack is not a class, or we did not recently
5749 add the class, False.
5750 raw_lines_no_comments: The lines without the comments.
5751 linenum: The current line number we are processing.
5752
5753 Returns:
5754 True if we should apply our namespace indentation check. Currently, it
5755 only works for classes and namespaces inside of a namespace.
5756 """
5757
5758 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5759 linenum)
5760
5761 if not (is_namespace_indent_item or is_forward_declaration):
5762 return False
5763
5764 # If we are in a macro, we do not want to check the namespace indentation.
5765 if IsMacroDefinition(raw_lines_no_comments, linenum):
5766 return False
5767
5768 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5769
5770
5771# Call this method if the line is directly inside of a namespace.
5772# If the line above is blank (excluding comments) or the start of
5773# an inner namespace, it cannot be indented.
5774def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5775 error):
5776 line = raw_lines_no_comments[linenum]
5777 if Match(r'^\s+', line):
5778 error(filename, linenum, 'runtime/indentation_namespace', 4,
5779 'Do not indent within a namespace')
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005780
5781
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005782def ProcessLine(filename, file_extension, clean_lines, line,
5783 include_state, function_state, nesting_state, error,
5784 extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005785 """Processes a single line in the file.
5786
5787 Args:
5788 filename: Filename of the file that is being processed.
5789 file_extension: The extension (dot not included) of the file.
5790 clean_lines: An array of strings, each representing a line of the file,
5791 with comments stripped.
5792 line: Number of line being processed.
5793 include_state: An _IncludeState instance in which the headers are inserted.
5794 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005795 nesting_state: A NestingState instance which maintains information about
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005796 the current stack of nested blocks being parsed.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005797 error: A callable to which errors are reported, which takes 4 arguments:
5798 filename, line number, error level, and message
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005799 extra_check_functions: An array of additional check functions that will be
5800 run on each source line. Each function takes 4
5801 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005802 """
5803 raw_lines = clean_lines.raw_lines
erg@google.com35589e62010-11-17 18:58:16 +00005804 ParseNolintSuppressions(filename, raw_lines[line], line, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005805 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.com59146752014-08-11 20:20:55 +00005806 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5807 error)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005808 if nesting_state.InAsmBlock(): return
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005809 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005810 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005811 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005812 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005813 nesting_state, error)
5814 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005815 CheckForNonStandardConstructs(filename, clean_lines, line,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005816 nesting_state, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005817 CheckVlogArguments(filename, clean_lines, line, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005818 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com6317a9c2009-06-25 00:28:19 +00005819 CheckInvalidIncrement(filename, clean_lines, line, error)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005820 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.com59146752014-08-11 20:20:55 +00005821 CheckRedundantVirtual(filename, clean_lines, line, error)
5822 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005823 for check_fn in extra_check_functions:
5824 check_fn(filename, clean_lines, line, error)
avakulenko@google.com17449932014-07-28 22:13:33 +00005825
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005826def FlagCxx11Features(filename, clean_lines, linenum, error):
5827 """Flag those c++11 features that we only allow in certain places.
5828
5829 Args:
5830 filename: The name of the current file.
5831 clean_lines: A CleansedLines instance containing the file.
5832 linenum: The number of the line to check.
5833 error: The function to call with any errors found.
5834 """
5835 line = clean_lines.elided[linenum]
5836
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005837 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005838
5839 # Flag unapproved C++ TR1 headers.
5840 if include and include.group(1).startswith('tr1/'):
5841 error(filename, linenum, 'build/c++tr1', 5,
5842 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1))
5843
5844 # Flag unapproved C++11 headers.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005845 if include and include.group(1) in ('cfenv',
5846 'condition_variable',
5847 'fenv.h',
5848 'future',
5849 'mutex',
5850 'thread',
5851 'chrono',
5852 'ratio',
5853 'regex',
5854 'system_error',
5855 ):
5856 error(filename, linenum, 'build/c++11', 5,
5857 ('<%s> is an unapproved C++11 header.') % include.group(1))
5858
5859 # The only place where we need to worry about C++11 keywords and library
5860 # features in preprocessor directives is in macro definitions.
5861 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5862
5863 # These are classes and free functions. The classes are always
5864 # mentioned as std::*, but we only catch the free functions if
5865 # they're not found by ADL. They're alphabetical by header.
5866 for top_name in (
5867 # type_traits
5868 'alignment_of',
5869 'aligned_union',
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005870 ):
5871 if Search(r'\bstd::%s\b' % top_name, line):
5872 error(filename, linenum, 'build/c++11', 5,
5873 ('std::%s is an unapproved C++11 class or function. Send c-style '
5874 'an example of where it would make your code more readable, and '
5875 'they may let you use it.') % top_name)
5876
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005877
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005878def FlagCxx14Features(filename, clean_lines, linenum, error):
5879 """Flag those C++14 features that we restrict.
5880
5881 Args:
5882 filename: The name of the current file.
5883 clean_lines: A CleansedLines instance containing the file.
5884 linenum: The number of the line to check.
5885 error: The function to call with any errors found.
5886 """
5887 line = clean_lines.elided[linenum]
5888
5889 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5890
5891 # Flag unapproved C++14 headers.
5892 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):
5893 error(filename, linenum, 'build/c++14', 5,
5894 ('<%s> is an unapproved C++14 header.') % include.group(1))
5895
5896
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005897def ProcessFileData(filename, file_extension, lines, error,
5898 extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005899 """Performs lint checks and reports any errors to the given error function.
5900
5901 Args:
5902 filename: Filename of the file that is being processed.
5903 file_extension: The extension (dot not included) of the file.
5904 lines: An array of strings, each representing a line of the file, with the
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005905 last element being empty if the file is terminated with a newline.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005906 error: A callable to which errors are reported, which takes 4 arguments:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005907 filename, line number, error level, and message
5908 extra_check_functions: An array of additional check functions that will be
5909 run on each source line. Each function takes 4
5910 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005911 """
5912 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5913 ['// marker so line numbers end in a known way'])
5914
5915 include_state = _IncludeState()
5916 function_state = _FunctionState()
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005917 nesting_state = NestingState()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005918
erg@google.com35589e62010-11-17 18:58:16 +00005919 ResetNolintSuppressions()
5920
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005921 CheckForCopyright(filename, lines, error)
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005922 ProcessGlobalSuppresions(lines)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005923 RemoveMultiLineComments(filename, lines, error)
5924 clean_lines = CleansedLines(lines)
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005925
Amin Hassanif7e1e102018-06-21 20:13:30 +00005926 if IsHeaderExtension(file_extension):
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005927 CheckForHeaderGuard(filename, clean_lines, error)
5928
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005929 for line in xrange(clean_lines.NumLines()):
5930 ProcessLine(filename, file_extension, clean_lines, line,
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00005931 include_state, function_state, nesting_state, error,
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00005932 extra_check_functions)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00005933 FlagCxx11Features(filename, clean_lines, line, error)
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005934 nesting_state.CheckCompletedBlocks(filename, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005935
5936 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
skym@chromium.org3990c412016-02-05 20:55:12 +00005937
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005938 # Check that the .cc file has included its header if it exists.
avakulenko@chromium.org764ce712016-05-06 23:03:41 +00005939 if _IsSourceExtension(file_extension):
avakulenko@google.com255f2be2014-12-05 22:19:55 +00005940 CheckHeaderFileIncluded(filename, include_state, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005941
5942 # We check here rather than inside ProcessLine so that we see raw
5943 # lines rather than "cleaned" lines.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00005944 CheckForBadCharacters(filename, lines, error)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00005945
5946 CheckForNewlineAtEOF(filename, lines, error)
5947
avakulenko@google.com17449932014-07-28 22:13:33 +00005948def ProcessConfigOverrides(filename):
5949 """ Loads the configuration files and processes the config overrides.
5950
5951 Args:
5952 filename: The name of the file being processed by the linter.
5953
5954 Returns:
5955 False if the current |filename| should not be processed further.
5956 """
5957
5958 abs_filename = os.path.abspath(filename)
5959 cfg_filters = []
5960 keep_looking = True
5961 while keep_looking:
5962 abs_path, base_name = os.path.split(abs_filename)
5963 if not base_name:
5964 break # Reached the root directory.
5965
5966 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5967 abs_filename = abs_path
5968 if not os.path.isfile(cfg_file):
5969 continue
5970
5971 try:
5972 with open(cfg_file) as file_handle:
5973 for line in file_handle:
5974 line, _, _ = line.partition('#') # Remove comments.
5975 if not line.strip():
5976 continue
5977
5978 name, _, val = line.partition('=')
5979 name = name.strip()
5980 val = val.strip()
5981 if name == 'set noparent':
5982 keep_looking = False
5983 elif name == 'filter':
5984 cfg_filters.append(val)
5985 elif name == 'exclude_files':
5986 # When matching exclude_files pattern, use the base_name of
5987 # the current file name or the directory name we are processing.
5988 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5989 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5990 # file's "exclude_files" filter is meant to be checked against "bar"
5991 # and not "baz" nor "bar/baz.cc".
5992 if base_name:
5993 pattern = re.compile(val)
5994 if pattern.match(base_name):
Amin Hassanif7e1e102018-06-21 20:13:30 +00005995 if _cpplint_state.quiet:
5996 # Suppress "Ignoring file" warning when using --quiet.
5997 return False
avakulenko@google.com17449932014-07-28 22:13:33 +00005998 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5999 'File path component "%s" matches '
6000 'pattern "%s"\n' %
6001 (filename, cfg_file, base_name, val))
6002 return False
avakulenko@google.com68a4fa62014-08-25 16:26:18 +00006003 elif name == 'linelength':
6004 global _line_length
6005 try:
6006 _line_length = int(val)
6007 except ValueError:
6008 sys.stderr.write('Line length must be numeric.')
Amin Hassanif7e1e102018-06-21 20:13:30 +00006009 elif name == 'root':
6010 global _root
6011 # root directories are specified relative to CPPLINT.cfg dir.
6012 _root = os.path.join(os.path.dirname(cfg_file), val)
6013 elif name == 'headers':
6014 ProcessHppHeadersOption(val)
avakulenko@google.com17449932014-07-28 22:13:33 +00006015 else:
6016 sys.stderr.write(
6017 'Invalid configuration option (%s) in file %s\n' %
6018 (name, cfg_file))
6019
6020 except IOError:
6021 sys.stderr.write(
6022 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
6023 keep_looking = False
6024
6025 # Apply all the accumulated filters in reverse order (top-level directory
6026 # config options having the least priority).
6027 for filter in reversed(cfg_filters):
6028 _AddFilters(filter)
6029
6030 return True
6031
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006032
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00006033def ProcessFile(filename, vlevel, extra_check_functions=[]):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006034 """Does google-lint on a single file.
6035
6036 Args:
6037 filename: The name of the file to parse.
6038
6039 vlevel: The level of errors to report. Every error of confidence
6040 >= verbose_level will be reported. 0 is a good default.
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00006041
6042 extra_check_functions: An array of additional check functions that will be
6043 run on each source line. Each function takes 4
6044 arguments: filename, clean_lines, line, error
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006045 """
6046
6047 _SetVerboseLevel(vlevel)
avakulenko@google.com17449932014-07-28 22:13:33 +00006048 _BackupFilters()
Amin Hassanif7e1e102018-06-21 20:13:30 +00006049 old_errors = _cpplint_state.error_count
avakulenko@google.com17449932014-07-28 22:13:33 +00006050
6051 if not ProcessConfigOverrides(filename):
6052 _RestoreFilters()
6053 return
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006054
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006055 lf_lines = []
6056 crlf_lines = []
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006057 try:
6058 # Support the UNIX convention of using "-" for stdin. Note that
6059 # we are not opening the file with universal newline support
6060 # (which codecs doesn't support anyway), so the resulting lines do
6061 # contain trailing '\r' characters if we are reading a file that
6062 # has CRLF endings.
6063 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006064 # below.
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006065 if filename == '-':
6066 lines = codecs.StreamReaderWriter(sys.stdin,
6067 codecs.getreader('utf8'),
6068 codecs.getwriter('utf8'),
6069 'replace').read().split('\n')
6070 else:
6071 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
6072
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006073 # Remove trailing '\r'.
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006074 # The -1 accounts for the extra trailing blank line we get from split()
6075 for linenum in range(len(lines) - 1):
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006076 if lines[linenum].endswith('\r'):
6077 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006078 crlf_lines.append(linenum + 1)
6079 else:
6080 lf_lines.append(linenum + 1)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006081
6082 except IOError:
6083 sys.stderr.write(
6084 "Skipping input '%s': Can't open for reading\n" % filename)
avakulenko@google.com17449932014-07-28 22:13:33 +00006085 _RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006086 return
6087
6088 # Note, if no dot is found, this will give the entire filename as the ext.
6089 file_extension = filename[filename.rfind('.') + 1:]
6090
6091 # When reading from stdin, the extension is unknown, so no cpplint tests
6092 # should rely on the extension.
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006093 if filename != '-' and file_extension not in _valid_extensions:
6094 sys.stderr.write('Ignoring %s; not a valid file name '
6095 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006096 else:
asvitkine@chromium.org8b8d8be2011-09-08 15:34:45 +00006097 ProcessFileData(filename, file_extension, lines, Error,
6098 extra_check_functions)
avakulenko@google.comd39bbb52014-06-04 22:55:20 +00006099
6100 # If end-of-line sequences are a mix of LF and CR-LF, issue
6101 # warnings on the lines with CR.
6102 #
6103 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
6104 # since critique can handle these just fine, and the style guide
6105 # doesn't dictate a particular end of line sequence.
6106 #
6107 # We can't depend on os.linesep to determine what the desired
6108 # end-of-line sequence should be, since that will return the
6109 # server-side end-of-line sequence.
6110 if lf_lines and crlf_lines:
6111 # Warn on every line with CR. An alternative approach might be to
6112 # check whether the file is mostly CRLF or just LF, and warn on the
6113 # minority, we bias toward LF here since most tools prefer LF.
6114 for linenum in crlf_lines:
6115 Error(filename, linenum, 'whitespace/newline', 1,
6116 'Unexpected \\r (^M) found; better to use only \\n')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006117
Amin Hassanif7e1e102018-06-21 20:13:30 +00006118 # Suppress printing anything if --quiet was passed unless the error
6119 # count has increased after processing this file.
6120 if not _cpplint_state.quiet or old_errors != _cpplint_state.error_count:
6121 sys.stdout.write('Done processing %s\n' % filename)
avakulenko@google.com17449932014-07-28 22:13:33 +00006122 _RestoreFilters()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006123
6124
6125def PrintUsage(message):
6126 """Prints a brief usage string and exits, optionally with an error message.
6127
6128 Args:
6129 message: The optional error message.
6130 """
6131 sys.stderr.write(_USAGE)
6132 if message:
6133 sys.exit('\nFATAL ERROR: ' + message)
6134 else:
6135 sys.exit(1)
6136
6137
6138def PrintCategories():
6139 """Prints a list of all the error-categories used by error messages.
6140
6141 These are the categories used to filter messages via --filter.
6142 """
erg@google.com35589e62010-11-17 18:58:16 +00006143 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006144 sys.exit(0)
6145
6146
6147def ParseArguments(args):
6148 """Parses the command line arguments.
6149
6150 This may set the output format and verbosity level as side-effects.
6151
6152 Args:
6153 args: The command line arguments:
6154
6155 Returns:
6156 The list of filenames to lint.
6157 """
6158 try:
6159 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.com26970fa2009-11-17 18:07:32 +00006160 'counting=',
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006161 'filter=',
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006162 'root=',
6163 'linelength=',
sdefresne263e9282016-07-19 02:14:22 -07006164 'extensions=',
Amin Hassanif7e1e102018-06-21 20:13:30 +00006165 'headers=',
6166 'quiet'])
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006167 except getopt.GetoptError:
6168 PrintUsage('Invalid arguments.')
6169
6170 verbosity = _VerboseLevel()
6171 output_format = _OutputFormat()
6172 filters = ''
Amin Hassanif7e1e102018-06-21 20:13:30 +00006173 quiet = _Quiet()
erg@google.com26970fa2009-11-17 18:07:32 +00006174 counting_style = ''
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006175
6176 for (opt, val) in opts:
6177 if opt == '--help':
6178 PrintUsage(None)
6179 elif opt == '--output':
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006180 if val not in ('emacs', 'vs7', 'eclipse'):
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006181 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006182 output_format = val
Amin Hassanif7e1e102018-06-21 20:13:30 +00006183 elif opt == '--quiet':
6184 quiet = True
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006185 elif opt == '--verbose':
6186 verbosity = int(val)
6187 elif opt == '--filter':
6188 filters = val
erg@google.com6317a9c2009-06-25 00:28:19 +00006189 if not filters:
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006190 PrintCategories()
erg@google.com26970fa2009-11-17 18:07:32 +00006191 elif opt == '--counting':
6192 if val not in ('total', 'toplevel', 'detailed'):
6193 PrintUsage('Valid counting options are total, toplevel, and detailed')
6194 counting_style = val
mazda@chromium.org3fffcec2013-06-07 01:04:53 +00006195 elif opt == '--root':
6196 global _root
6197 _root = val
raphael.kubo.da.costa@intel.com331fbc42014-05-09 08:48:20 +00006198 elif opt == '--linelength':
6199 global _line_length
6200 try:
6201 _line_length = int(val)
6202 except ValueError:
6203 PrintUsage('Line length must be digits.')
6204 elif opt == '--extensions':
6205 global _valid_extensions
6206 try:
6207 _valid_extensions = set(val.split(','))
6208 except ValueError:
qyearsley12fa6ff2016-08-24 09:18:40 -07006209 PrintUsage('Extensions must be comma separated list.')
Amin Hassanif7e1e102018-06-21 20:13:30 +00006210 elif opt == '--headers':
6211 ProcessHppHeadersOption(val)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006212
6213 if not filenames:
6214 PrintUsage('No files were specified.')
6215
6216 _SetOutputFormat(output_format)
Amin Hassanif7e1e102018-06-21 20:13:30 +00006217 _SetQuiet(quiet)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006218 _SetVerboseLevel(verbosity)
6219 _SetFilters(filters)
erg@google.com26970fa2009-11-17 18:07:32 +00006220 _SetCountingStyle(counting_style)
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006221
6222 return filenames
6223
6224
6225def main():
6226 filenames = ParseArguments(sys.argv[1:])
6227
6228 # Change stderr to write with replacement characters so we don't die
6229 # if we try to print something containing non-ASCII characters.
6230 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6231 codecs.getreader('utf8'),
6232 codecs.getwriter('utf8'),
6233 'replace')
6234
erg@google.com26970fa2009-11-17 18:07:32 +00006235 _cpplint_state.ResetErrorCounts()
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006236 for filename in filenames:
6237 ProcessFile(filename, _cpplint_state.verbose_level)
Amin Hassanif7e1e102018-06-21 20:13:30 +00006238 # If --quiet is passed, suppress printing error count unless there are errors.
6239 if not _cpplint_state.quiet or _cpplint_state.error_count > 0:
6240 _cpplint_state.PrintErrorCounts()
erg@google.com26970fa2009-11-17 18:07:32 +00006241
maruel@google.comfb2b8eb2009-04-23 21:03:42 +00006242 sys.exit(_cpplint_state.error_count > 0)
6243
6244
6245if __name__ == '__main__':
6246 main()