blob: a9815124f0ac814e6bef06b1f63a597442924bf0 [file] [log] [blame]
avakulenko@google.com554223d2014-12-04 22:00:20 +00001#!/usr/bin/env python
erg@google.com4e00b9a2009-01-12 23:05:11 +00002#
erg@google.com8f91ab22011-09-06 21:04:45 +00003# Copyright (c) 2009 Google Inc. All rights reserved.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004#
erg@google.com969161c2009-06-26 22:06:46 +00005# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
erg@google.com4e00b9a2009-01-12 23:05:11 +00008#
erg@google.com969161c2009-06-26 22:06:46 +00009# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
erg@google.com4e00b9a2009-01-12 23:05:11 +000018#
erg@google.com969161c2009-06-26 22:06:46 +000019# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
erg@google.com4e00b9a2009-01-12 23:05:11 +000030
erg@google.com4e00b9a2009-01-12 23:05:11 +000031"""Does google-lint on c++ files.
32
33The goal of this script is to identify places in the code that *may*
34be in non-compliance with google style. It does not attempt to fix
35up these problems -- the point is to educate. It does also not
36attempt to find all problems, or to ensure that everything it does
37find is legitimately a problem.
38
39In particular, we can get very confused by /* and // inside strings!
40We do a small hack, which is to ignore //'s with "'s after them on the
41same line, but it is far from perfect (in either direction).
42"""
43
44import codecs
erg@google.comd350fe52013-01-14 17:51:48 +000045import copy
erg@google.com4e00b9a2009-01-12 23:05:11 +000046import getopt
47import math # for log
48import os
49import re
50import sre_compile
51import string
52import sys
53import unicodedata
54
55
56_USAGE = """
57Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
erg@google.comab53edf2013-11-05 22:23:37 +000058 [--counting=total|toplevel|detailed] [--root=subdir]
LukeCz7197a242016-09-24 13:27:35 -050059 [--linelength=digits] [--headers=x,y,...]
erg@google.com4e00b9a2009-01-12 23:05:11 +000060 <file> [file] ...
61
62 The style guidelines this tries to follow are those in
Ackermann Yuriy79692902016-04-01 21:41:34 +130063 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
erg@google.com4e00b9a2009-01-12 23:05:11 +000064
65 Every problem is given a confidence score from 1-5, with 5 meaning we are
66 certain of the problem, and 1 meaning it could be a legitimate construct.
67 This will miss some errors, and is not a substitute for a code review.
68
erg+personal@google.com05189642010-04-30 20:43:03 +000069 To suppress false-positive errors of a certain category, add a
70 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
71 suppresses errors of all categories on that line.
erg@google.com4e00b9a2009-01-12 23:05:11 +000072
73 The files passed in will be linted; at least one file must be provided.
erg@google.com19680272013-12-16 22:48:54 +000074 Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the
75 extensions with the --extensions flag.
erg@google.com4e00b9a2009-01-12 23:05:11 +000076
77 Flags:
78
79 output=vs7
80 By default, the output is formatted to ease emacs parsing. Visual Studio
81 compatible output (vs7) may also be used. Other formats are unsupported.
82
83 verbose=#
84 Specify a number 0-5 to restrict errors to certain verbosity levels.
85
86 filter=-x,+y,...
87 Specify a comma-separated list of category-filters to apply: only
88 error messages whose category names pass the filters will be printed.
89 (Category names are printed with the message and look like
90 "[whitespace/indent]".) Filters are evaluated left to right.
91 "-FOO" and "FOO" means "do not print categories that start with FOO".
92 "+FOO" means "do print categories that start with FOO".
93
94 Examples: --filter=-whitespace,+whitespace/braces
95 --filter=whitespace,runtime/printf,+runtime/printf_format
96 --filter=-,+build/include_what_you_use
97
98 To see a list of all the categories used in cpplint, pass no arg:
99 --filter=
erg@google.coma868d2d2009-10-09 21:18:45 +0000100
101 counting=total|toplevel|detailed
102 The total number of errors found is always printed. If
103 'toplevel' is provided, then the count of errors in each of
104 the top-level categories like 'build' and 'whitespace' will
105 also be printed. If 'detailed' is provided, then a count
106 is provided for each category like 'build/class'.
erg@google.com4d70a882013-04-16 21:06:32 +0000107
108 root=subdir
109 The root directory used for deriving header guard CPP variable.
110 By default, the header guard CPP variable is calculated as the relative
111 path to the directory that contains .git, .hg, or .svn. When this flag
112 is specified, the relative path is calculated from the specified
113 directory. If the specified directory does not exist, this flag is
114 ignored.
115
116 Examples:
Igor Murashkin8a87a462017-11-09 13:48:29 -0800117 Assuming that top/src/.git exists (and cwd=top/src), the header guard
118 CPP variables for top/src/chrome/browser/ui/browser.h are:
erg@google.com4d70a882013-04-16 21:06:32 +0000119
120 No flag => CHROME_BROWSER_UI_BROWSER_H_
121 --root=chrome => BROWSER_UI_BROWSER_H_
122 --root=chrome/browser => UI_BROWSER_H_
Igor Murashkin8a87a462017-11-09 13:48:29 -0800123 --root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_
erg@google.comab53edf2013-11-05 22:23:37 +0000124
125 linelength=digits
126 This is the allowed line length for the project. The default value is
127 80 characters.
128
129 Examples:
130 --linelength=120
erg@google.com19680272013-12-16 22:48:54 +0000131
132 extensions=extension,extension,...
133 The allowed file extensions that cpplint will check
134
135 Examples:
136 --extensions=hpp,cpp
erg@google.com7430eef2014-07-28 22:33:46 +0000137
LukeCz7197a242016-09-24 13:27:35 -0500138 headers=x,y,...
139 The header extensions that cpplint will treat as .h in checks. Values are
140 automatically added to --extensions list.
141
142 Examples:
143 --headers=hpp,hxx
144 --headers=hpp
145
erg@google.com7430eef2014-07-28 22:33:46 +0000146 cpplint.py supports per-directory configurations specified in CPPLINT.cfg
147 files. CPPLINT.cfg file can contain a number of key=value pairs.
148 Currently the following options are supported:
149
150 set noparent
151 filter=+filter1,-filter2,...
152 exclude_files=regex
avakulenko@google.com310681b2014-08-22 19:38:55 +0000153 linelength=80
Fabian Guera2322e4f2016-05-01 17:36:30 +0200154 root=subdir
LukeCz7197a242016-09-24 13:27:35 -0500155 headers=x,y,...
erg@google.com7430eef2014-07-28 22:33:46 +0000156
157 "set noparent" option prevents cpplint from traversing directory tree
158 upwards looking for more .cfg files in parent directories. This option
159 is usually placed in the top-level project directory.
160
161 The "filter" option is similar in function to --filter flag. It specifies
162 message filters in addition to the |_DEFAULT_FILTERS| and those specified
163 through --filter command-line flag.
164
165 "exclude_files" allows to specify a regular expression to be matched against
166 a file name. If the expression matches, the file is skipped and not run
167 through liner.
168
avakulenko@google.com310681b2014-08-22 19:38:55 +0000169 "linelength" allows to specify the allowed line length for the project.
170
Fabian Guera2322e4f2016-05-01 17:36:30 +0200171 The "root" option is similar in function to the --root flag (see example
Igor Murashkin8a87a462017-11-09 13:48:29 -0800172 above). Paths are relative to the directory of the CPPLINT.cfg.
173
174 The "headers" option is similar in function to the --headers flag
LukeCz7197a242016-09-24 13:27:35 -0500175 (see example above).
Fabian Guera2322e4f2016-05-01 17:36:30 +0200176
erg@google.com7430eef2014-07-28 22:33:46 +0000177 CPPLINT.cfg has an effect on files in the same directory and all
178 sub-directories, unless overridden by a nested configuration file.
179
180 Example file:
181 filter=-build/include_order,+build/include_alpha
182 exclude_files=.*\.cc
183
184 The above example disables build/include_order warning and enables
185 build/include_alpha as well as excludes all .cc from being
186 processed by linter, in the current directory (where the .cfg
187 file is located) and all sub-directories.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000188"""
189
190# We categorize each error message we print. Here are the categories.
191# We want an explicit list so we can list them all in cpplint --filter=.
192# If you add a new error message with a new category, add it to the list
193# here! cpplint_unittest.py should tell you if you forget to do this.
erg+personal@google.com05189642010-04-30 20:43:03 +0000194_ERROR_CATEGORIES = [
avakulenko@google.com554223d2014-12-04 22:00:20 +0000195 'build/class',
196 'build/c++11',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700197 'build/c++14',
198 'build/c++tr1',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000199 'build/deprecated',
200 'build/endif_comment',
201 'build/explicit_make_pair',
202 'build/forward_decl',
203 'build/header_guard',
204 'build/include',
205 'build/include_alpha',
206 'build/include_order',
207 'build/include_what_you_use',
208 'build/namespaces',
209 'build/printf_format',
210 'build/storage_class',
211 'legal/copyright',
212 'readability/alt_tokens',
213 'readability/braces',
214 'readability/casting',
215 'readability/check',
216 'readability/constructors',
217 'readability/fn_size',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000218 'readability/inheritance',
219 'readability/multiline_comment',
220 'readability/multiline_string',
221 'readability/namespace',
222 'readability/nolint',
223 'readability/nul',
224 'readability/strings',
225 'readability/todo',
226 'readability/utf8',
227 'runtime/arrays',
228 'runtime/casting',
229 'runtime/explicit',
230 'runtime/int',
231 'runtime/init',
232 'runtime/invalid_increment',
233 'runtime/member_string_references',
234 'runtime/memset',
235 'runtime/indentation_namespace',
236 'runtime/operator',
237 'runtime/printf',
238 'runtime/printf_format',
239 'runtime/references',
240 'runtime/string',
241 'runtime/threadsafe_fn',
242 'runtime/vlog',
243 'whitespace/blank_line',
244 'whitespace/braces',
245 'whitespace/comma',
246 'whitespace/comments',
247 'whitespace/empty_conditional_body',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700248 'whitespace/empty_if_body',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000249 'whitespace/empty_loop_body',
250 'whitespace/end_of_line',
251 'whitespace/ending_newline',
252 'whitespace/forcolon',
253 'whitespace/indent',
254 'whitespace/line_length',
255 'whitespace/newline',
256 'whitespace/operators',
257 'whitespace/parens',
258 'whitespace/semicolon',
259 'whitespace/tab',
260 'whitespace/todo',
261 ]
262
263# These error categories are no longer enforced by cpplint, but for backwards-
264# compatibility they may still appear in NOLINT comments.
265_LEGACY_ERROR_CATEGORIES = [
266 'readability/streams',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700267 'readability/function',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000268 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000269
avakulenko@google.com02af6282014-06-04 18:53:25 +0000270# The default state of the category filter. This is overridden by the --filter=
erg@google.come35f7652009-06-19 20:52:09 +0000271# flag. By default all errors are on, so only add here categories that should be
272# off by default (i.e., categories that must be enabled by the --filter= flags).
273# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000274_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000275
Alex Vakulenko01e47232016-05-06 13:54:15 -0700276# The default list of categories suppressed for C (not C++) files.
277_DEFAULT_C_SUPPRESSED_CATEGORIES = [
278 'readability/casting',
279 ]
280
281# The default list of categories suppressed for Linux Kernel files.
282_DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [
283 'whitespace/tab',
284 ]
285
erg@google.com4e00b9a2009-01-12 23:05:11 +0000286# We used to check for high-bit characters, but after much discussion we
287# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000288# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000289
erg@google.comfd5da632013-10-25 17:39:45 +0000290# C++ headers
erg@google.com4e00b9a2009-01-12 23:05:11 +0000291_CPP_HEADERS = frozenset([
erg@google.comfd5da632013-10-25 17:39:45 +0000292 # Legacy
293 'algobase.h',
294 'algo.h',
295 'alloc.h',
296 'builtinbuf.h',
297 'bvector.h',
298 'complex.h',
299 'defalloc.h',
300 'deque.h',
301 'editbuf.h',
302 'fstream.h',
303 'function.h',
304 'hash_map',
305 'hash_map.h',
306 'hash_set',
307 'hash_set.h',
308 'hashtable.h',
309 'heap.h',
310 'indstream.h',
311 'iomanip.h',
312 'iostream.h',
313 'istream.h',
314 'iterator.h',
315 'list.h',
316 'map.h',
317 'multimap.h',
318 'multiset.h',
319 'ostream.h',
320 'pair.h',
321 'parsestream.h',
322 'pfstream.h',
323 'procbuf.h',
324 'pthread_alloc',
325 'pthread_alloc.h',
326 'rope',
327 'rope.h',
328 'ropeimpl.h',
329 'set.h',
330 'slist',
331 'slist.h',
332 'stack.h',
333 'stdiostream.h',
334 'stl_alloc.h',
335 'stl_relops.h',
336 'streambuf.h',
337 'stream.h',
338 'strfile.h',
339 'strstream.h',
340 'tempbuf.h',
341 'tree.h',
342 'type_traits.h',
343 'vector.h',
344 # 17.6.1.2 C++ library headers
345 'algorithm',
346 'array',
347 'atomic',
348 'bitset',
349 'chrono',
350 'codecvt',
351 'complex',
352 'condition_variable',
353 'deque',
354 'exception',
355 'forward_list',
356 'fstream',
357 'functional',
358 'future',
359 'initializer_list',
360 'iomanip',
361 'ios',
362 'iosfwd',
363 'iostream',
364 'istream',
365 'iterator',
366 'limits',
367 'list',
368 'locale',
369 'map',
370 'memory',
371 'mutex',
372 'new',
373 'numeric',
374 'ostream',
375 'queue',
376 'random',
377 'ratio',
378 'regex',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700379 'scoped_allocator',
erg@google.comfd5da632013-10-25 17:39:45 +0000380 'set',
381 'sstream',
382 'stack',
383 'stdexcept',
384 'streambuf',
385 'string',
386 'strstream',
387 'system_error',
388 'thread',
389 'tuple',
390 'typeindex',
391 'typeinfo',
392 'type_traits',
393 'unordered_map',
394 'unordered_set',
395 'utility',
erg@google.com5d00c562013-07-12 19:57:05 +0000396 'valarray',
erg@google.comfd5da632013-10-25 17:39:45 +0000397 'vector',
398 # 17.6.1.2 C++ headers for C library facilities
399 'cassert',
400 'ccomplex',
401 'cctype',
402 'cerrno',
403 'cfenv',
404 'cfloat',
405 'cinttypes',
406 'ciso646',
407 'climits',
408 'clocale',
409 'cmath',
410 'csetjmp',
411 'csignal',
412 'cstdalign',
413 'cstdarg',
414 'cstdbool',
415 'cstddef',
416 'cstdint',
417 'cstdio',
418 'cstdlib',
419 'cstring',
420 'ctgmath',
421 'ctime',
422 'cuchar',
423 'cwchar',
424 'cwctype',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000425 ])
426
Alex Vakulenko01e47232016-05-06 13:54:15 -0700427# Type names
428_TYPES = re.compile(
429 r'^(?:'
430 # [dcl.type.simple]
431 r'(char(16_t|32_t)?)|wchar_t|'
432 r'bool|short|int|long|signed|unsigned|float|double|'
433 # [support.types]
434 r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|'
435 # [cstdint.syn]
436 r'(u?int(_fast|_least)?(8|16|32|64)_t)|'
437 r'(u?int(max|ptr)_t)|'
438 r')$')
439
avakulenko@google.com02af6282014-06-04 18:53:25 +0000440
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000441# These headers are excluded from [build/include] and [build/include_order]
442# checks:
443# - Anything not following google file name conventions (containing an
444# uppercase character, such as Python.h or nsStringAPI.h, for example).
445# - Lua headers.
446_THIRD_PARTY_HEADERS_PATTERN = re.compile(
447 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
448
Alex Vakulenko01e47232016-05-06 13:54:15 -0700449# Pattern for matching FileInfo.BaseName() against test file name
450_TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$'
451
452# Pattern that matches only complete whitespace, possibly across multiple lines.
453_EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000454
erg@google.com4e00b9a2009-01-12 23:05:11 +0000455# Assertion macros. These are defined in base/logging.h and
Alex Vakulenko01e47232016-05-06 13:54:15 -0700456# testing/base/public/gunit.h.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000457_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000458 'DCHECK', 'CHECK',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700459 'EXPECT_TRUE', 'ASSERT_TRUE',
460 'EXPECT_FALSE', 'ASSERT_FALSE',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000461 ]
462
erg@google.come35f7652009-06-19 20:52:09 +0000463# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000464_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
465
466for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
467 ('>=', 'GE'), ('>', 'GT'),
468 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000469 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000470 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
471 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
472 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000473
474for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
475 ('>=', 'LT'), ('>', 'LE'),
476 ('<=', 'GT'), ('<', 'GE')]:
477 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
478 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000479
erg@google.comd350fe52013-01-14 17:51:48 +0000480# Alternative tokens and their replacements. For full list, see section 2.5
481# Alternative tokens [lex.digraph] in the C++ standard.
482#
483# Digraphs (such as '%:') are not included here since it's a mess to
484# match those on a word boundary.
485_ALT_TOKEN_REPLACEMENT = {
486 'and': '&&',
487 'bitor': '|',
488 'or': '||',
489 'xor': '^',
490 'compl': '~',
491 'bitand': '&',
492 'and_eq': '&=',
493 'or_eq': '|=',
494 'xor_eq': '^=',
495 'not': '!',
496 'not_eq': '!='
497 }
498
499# Compile regular expression that matches all the above keywords. The "[ =()]"
500# bit is meant to avoid matching these keywords outside of boolean expressions.
501#
erg@google.comc6671232013-10-25 21:44:03 +0000502# False positives include C-style multi-line comments and multi-line strings
503# but those have always been troublesome for cpplint.
erg@google.comd350fe52013-01-14 17:51:48 +0000504_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
505 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
506
erg@google.com4e00b9a2009-01-12 23:05:11 +0000507
508# These constants define types of headers for use with
509# _IncludeState.CheckNextIncludeOrder().
510_C_SYS_HEADER = 1
511_CPP_SYS_HEADER = 2
512_LIKELY_MY_HEADER = 3
513_POSSIBLE_MY_HEADER = 4
514_OTHER_HEADER = 5
515
erg@google.comd350fe52013-01-14 17:51:48 +0000516# These constants define the current inline assembly state
517_NO_ASM = 0 # Outside of inline assembly block
518_INSIDE_ASM = 1 # Inside inline assembly block
519_END_ASM = 2 # Last line of inline assembly block
520_BLOCK_ASM = 3 # The whole block is an inline assembly block
521
522# Match start of assembly blocks
523_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
524 r'(?:\s+(volatile|__volatile__))?'
525 r'\s*[{(]')
526
Alex Vakulenko01e47232016-05-06 13:54:15 -0700527# Match strings that indicate we're working on a C (not C++) file.
528_SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|'
529 r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))')
530
531# Match string that indicates we're working on a Linux Kernel file.
532_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
erg@google.com4e00b9a2009-01-12 23:05:11 +0000533
534_regexp_compile_cache = {}
535
erg+personal@google.com05189642010-04-30 20:43:03 +0000536# {str, set(int)}: a map from error categories to sets of linenumbers
537# on which those errors are expected and should be suppressed.
538_error_suppressions = {}
539
erg@google.com4d70a882013-04-16 21:06:32 +0000540# The root directory used for deriving header guard CPP variable.
541# This is set by --root flag.
542_root = None
Igor Murashkin8a87a462017-11-09 13:48:29 -0800543_root_debug = False
erg@google.com4d70a882013-04-16 21:06:32 +0000544
erg@google.comab53edf2013-11-05 22:23:37 +0000545# The allowed line length of files.
546# This is set by --linelength flag.
547_line_length = 80
548
erg@google.com19680272013-12-16 22:48:54 +0000549# The allowed extensions for file names
550# This is set by --extensions flag.
551_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
552
LukeCz7197a242016-09-24 13:27:35 -0500553# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
554# This is set by --headers flag.
LukeCz8920b132016-09-26 19:40:47 -0500555_hpp_headers = set(['h'])
LukeCz7197a242016-09-24 13:27:35 -0500556
Alex Vakulenko01e47232016-05-06 13:54:15 -0700557# {str, bool}: a map from error categories to booleans which indicate if the
558# category should be suppressed for every line.
559_global_error_suppressions = {}
560
LukeCz7197a242016-09-24 13:27:35 -0500561def ProcessHppHeadersOption(val):
562 global _hpp_headers
563 try:
564 _hpp_headers = set(val.split(','))
565 # Automatically append to extensions list so it does not have to be set 2 times
566 _valid_extensions.update(_hpp_headers)
567 except ValueError:
568 PrintUsage('Header extensions must be comma seperated list.')
569
570def IsHeaderExtension(file_extension):
LukeCz8920b132016-09-26 19:40:47 -0500571 return file_extension in _hpp_headers
Alex Vakulenko01e47232016-05-06 13:54:15 -0700572
erg+personal@google.com05189642010-04-30 20:43:03 +0000573def ParseNolintSuppressions(filename, raw_line, linenum, error):
Alex Vakulenko01e47232016-05-06 13:54:15 -0700574 """Updates the global list of line error-suppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000575
576 Parses any NOLINT comments on the current line, updating the global
577 error_suppressions store. Reports an error if the NOLINT comment
578 was malformed.
579
580 Args:
581 filename: str, the name of the input file.
582 raw_line: str, the line of input text, with comments.
583 linenum: int, the number of the current line.
584 error: function, an error handler.
585 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000586 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
erg@google.com8a95ecc2011-09-08 00:45:54 +0000587 if matched:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000588 if matched.group(1):
589 suppressed_line = linenum + 1
590 else:
591 suppressed_line = linenum
592 category = matched.group(2)
erg+personal@google.com05189642010-04-30 20:43:03 +0000593 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000594 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg+personal@google.com05189642010-04-30 20:43:03 +0000595 else:
596 if category.startswith('(') and category.endswith(')'):
597 category = category[1:-1]
598 if category in _ERROR_CATEGORIES:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000599 _error_suppressions.setdefault(category, set()).add(suppressed_line)
avakulenko@google.com554223d2014-12-04 22:00:20 +0000600 elif category not in _LEGACY_ERROR_CATEGORIES:
erg+personal@google.com05189642010-04-30 20:43:03 +0000601 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000602 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000603
604
Alex Vakulenko01e47232016-05-06 13:54:15 -0700605def ProcessGlobalSuppresions(lines):
606 """Updates the list of global error suppressions.
607
608 Parses any lint directives in the file that have global effect.
609
610 Args:
611 lines: An array of strings, each representing a line of the file, with the
612 last element being empty if the file is terminated with a newline.
613 """
614 for line in lines:
615 if _SEARCH_C_FILE.search(line):
616 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
617 _global_error_suppressions[category] = True
618 if _SEARCH_KERNEL_FILE.search(line):
619 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES:
620 _global_error_suppressions[category] = True
621
622
erg+personal@google.com05189642010-04-30 20:43:03 +0000623def ResetNolintSuppressions():
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000624 """Resets the set of NOLINT suppressions to empty."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000625 _error_suppressions.clear()
Alex Vakulenko01e47232016-05-06 13:54:15 -0700626 _global_error_suppressions.clear()
erg+personal@google.com05189642010-04-30 20:43:03 +0000627
628
629def IsErrorSuppressedByNolint(category, linenum):
630 """Returns true if the specified error category is suppressed on this line.
631
632 Consults the global error_suppressions map populated by
Alex Vakulenko01e47232016-05-06 13:54:15 -0700633 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000634
635 Args:
636 category: str, the category of the error.
637 linenum: int, the current line number.
638 Returns:
Alex Vakulenko01e47232016-05-06 13:54:15 -0700639 bool, True iff the error should be suppressed due to a NOLINT comment or
640 global suppression.
erg+personal@google.com05189642010-04-30 20:43:03 +0000641 """
Alex Vakulenko01e47232016-05-06 13:54:15 -0700642 return (_global_error_suppressions.get(category, False) or
643 linenum in _error_suppressions.get(category, set()) or
erg+personal@google.com05189642010-04-30 20:43:03 +0000644 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000645
avakulenko@google.com02af6282014-06-04 18:53:25 +0000646
erg@google.com4e00b9a2009-01-12 23:05:11 +0000647def Match(pattern, s):
648 """Matches the string with the pattern, caching the compiled regexp."""
649 # The regexp compilation caching is inlined in both Match and Search for
650 # performance reasons; factoring it out into a separate function turns out
651 # to be noticeably expensive.
erg@google.comc6671232013-10-25 21:44:03 +0000652 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000653 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
654 return _regexp_compile_cache[pattern].match(s)
655
656
erg@google.comfd5da632013-10-25 17:39:45 +0000657def ReplaceAll(pattern, rep, s):
658 """Replaces instances of pattern in a string with a replacement.
659
660 The compiled regex is kept in a cache shared by Match and Search.
661
662 Args:
663 pattern: regex pattern
664 rep: replacement text
665 s: search string
666
667 Returns:
668 string with replacements made (or original string if no replacements)
669 """
670 if pattern not in _regexp_compile_cache:
671 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
672 return _regexp_compile_cache[pattern].sub(rep, s)
673
674
erg@google.com4e00b9a2009-01-12 23:05:11 +0000675def Search(pattern, s):
676 """Searches the string for the pattern, caching the compiled regexp."""
erg@google.comc6671232013-10-25 21:44:03 +0000677 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000678 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
679 return _regexp_compile_cache[pattern].search(s)
680
681
Alex Vakulenko01e47232016-05-06 13:54:15 -0700682def _IsSourceExtension(s):
683 """File extension (excluding dot) matches a source file extension."""
684 return s in ('c', 'cc', 'cpp', 'cxx')
685
686
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000687class _IncludeState(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000688 """Tracks line numbers for includes, and the order in which includes appear.
689
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000690 include_list contains list of lists of (header, line number) pairs.
691 It's a lists of lists rather than just one flat list to make it
692 easier to update across preprocessor boundaries.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000693
694 Call CheckNextIncludeOrder() once for each header in the file, passing
695 in the type constants defined above. Calls in an illegal order will
696 raise an _IncludeError with an appropriate error message.
697
698 """
699 # self._section will move monotonically through this set. If it ever
700 # needs to move backwards, CheckNextIncludeOrder will raise an error.
701 _INITIAL_SECTION = 0
702 _MY_H_SECTION = 1
703 _C_SECTION = 2
704 _CPP_SECTION = 3
705 _OTHER_H_SECTION = 4
706
707 _TYPE_NAMES = {
708 _C_SYS_HEADER: 'C system header',
709 _CPP_SYS_HEADER: 'C++ system header',
710 _LIKELY_MY_HEADER: 'header this file implements',
711 _POSSIBLE_MY_HEADER: 'header this file may implement',
712 _OTHER_HEADER: 'other header',
713 }
714 _SECTION_NAMES = {
715 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
716 _MY_H_SECTION: 'a header this file implements',
717 _C_SECTION: 'C system header',
718 _CPP_SECTION: 'C++ system header',
719 _OTHER_H_SECTION: 'other header',
720 }
721
722 def __init__(self):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000723 self.include_list = [[]]
724 self.ResetSection('')
erg@google.com2aa59982013-10-28 19:09:25 +0000725
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000726 def FindHeader(self, header):
727 """Check if a header has already been included.
728
729 Args:
730 header: header to check.
731 Returns:
732 Line number of previous occurrence, or -1 if the header has not
733 been seen before.
734 """
735 for section_list in self.include_list:
736 for f in section_list:
737 if f[0] == header:
738 return f[1]
739 return -1
740
741 def ResetSection(self, directive):
742 """Reset section checking for preprocessor directive.
743
744 Args:
745 directive: preprocessor directive (e.g. "if", "else").
746 """
erg@google.coma868d2d2009-10-09 21:18:45 +0000747 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000748 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000749 # The path of last found header.
750 self._last_header = ''
751
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000752 # Update list of includes. Note that we never pop from the
753 # include list.
754 if directive in ('if', 'ifdef', 'ifndef'):
755 self.include_list.append([])
756 elif directive in ('else', 'elif'):
757 self.include_list[-1] = []
758
erg@google.comfd5da632013-10-25 17:39:45 +0000759 def SetLastHeader(self, header_path):
760 self._last_header = header_path
761
erg@google.coma868d2d2009-10-09 21:18:45 +0000762 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000763 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000764
765 - replaces "-" with "_" so they both cmp the same.
766 - removes '-inl' since we don't require them to be after the main header.
767 - lowercase everything, just in case.
768
769 Args:
770 header_path: Path to be canonicalized.
771
772 Returns:
773 Canonicalized path.
774 """
775 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
776
erg@google.comfd5da632013-10-25 17:39:45 +0000777 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.coma868d2d2009-10-09 21:18:45 +0000778 """Check if a header is in alphabetical order with the previous header.
779
780 Args:
erg@google.comfd5da632013-10-25 17:39:45 +0000781 clean_lines: A CleansedLines instance containing the file.
782 linenum: The number of the line to check.
783 header_path: Canonicalized header to be checked.
erg@google.coma868d2d2009-10-09 21:18:45 +0000784
785 Returns:
786 Returns true if the header is in alphabetical order.
787 """
erg@google.comfd5da632013-10-25 17:39:45 +0000788 # If previous section is different from current section, _last_header will
789 # be reset to empty string, so it's always less than current header.
790 #
791 # If previous line was a blank line, assume that the headers are
792 # intentionally sorted the way they are.
793 if (self._last_header > header_path and
avakulenko@google.com554223d2014-12-04 22:00:20 +0000794 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])):
erg@google.coma868d2d2009-10-09 21:18:45 +0000795 return False
erg@google.coma868d2d2009-10-09 21:18:45 +0000796 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000797
798 def CheckNextIncludeOrder(self, header_type):
799 """Returns a non-empty error message if the next header is out of order.
800
801 This function also updates the internal state to be ready to check
802 the next include.
803
804 Args:
805 header_type: One of the _XXX_HEADER constants defined above.
806
807 Returns:
808 The empty string if the header is in the right order, or an
809 error message describing what's wrong.
810
811 """
812 error_message = ('Found %s after %s' %
813 (self._TYPE_NAMES[header_type],
814 self._SECTION_NAMES[self._section]))
815
erg@google.coma868d2d2009-10-09 21:18:45 +0000816 last_section = self._section
817
erg@google.com4e00b9a2009-01-12 23:05:11 +0000818 if header_type == _C_SYS_HEADER:
819 if self._section <= self._C_SECTION:
820 self._section = self._C_SECTION
821 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000822 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000823 return error_message
824 elif header_type == _CPP_SYS_HEADER:
825 if self._section <= self._CPP_SECTION:
826 self._section = self._CPP_SECTION
827 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000828 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000829 return error_message
830 elif header_type == _LIKELY_MY_HEADER:
831 if self._section <= self._MY_H_SECTION:
832 self._section = self._MY_H_SECTION
833 else:
834 self._section = self._OTHER_H_SECTION
835 elif header_type == _POSSIBLE_MY_HEADER:
836 if self._section <= self._MY_H_SECTION:
837 self._section = self._MY_H_SECTION
838 else:
839 # This will always be the fallback because we're not sure
840 # enough that the header is associated with this file.
841 self._section = self._OTHER_H_SECTION
842 else:
843 assert header_type == _OTHER_HEADER
844 self._section = self._OTHER_H_SECTION
845
erg@google.coma868d2d2009-10-09 21:18:45 +0000846 if last_section != self._section:
847 self._last_header = ''
848
erg@google.com4e00b9a2009-01-12 23:05:11 +0000849 return ''
850
851
852class _CppLintState(object):
853 """Maintains module-wide state.."""
854
855 def __init__(self):
856 self.verbose_level = 1 # global setting.
857 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000858 # filters to apply when emitting error messages
859 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000860 # backup of filter list. Used to restore the state after each file.
861 self._filters_backup = self.filters[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000862 self.counting = 'total' # In what way are we counting errors?
863 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000864
865 # output format:
866 # "emacs" - format that emacs can parse (default)
867 # "vs7" - format that Microsoft Visual Studio 7 can parse
868 self.output_format = 'emacs'
869
870 def SetOutputFormat(self, output_format):
871 """Sets the output format for errors."""
872 self.output_format = output_format
873
874 def SetVerboseLevel(self, level):
875 """Sets the module's verbosity, and returns the previous setting."""
876 last_verbose_level = self.verbose_level
877 self.verbose_level = level
878 return last_verbose_level
879
erg@google.coma868d2d2009-10-09 21:18:45 +0000880 def SetCountingStyle(self, counting_style):
881 """Sets the module's counting options."""
882 self.counting = counting_style
883
erg@google.com4e00b9a2009-01-12 23:05:11 +0000884 def SetFilters(self, filters):
885 """Sets the error-message filters.
886
887 These filters are applied when deciding whether to emit a given
888 error message.
889
890 Args:
891 filters: A string of comma-separated filters (eg "+whitespace/indent").
892 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000893
894 Raises:
895 ValueError: The comma-separated filters did not all start with '+' or '-'.
896 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000897 """
erg@google.come35f7652009-06-19 20:52:09 +0000898 # Default filters always have less priority than the flag ones.
899 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000900 self.AddFilters(filters)
901
902 def AddFilters(self, filters):
903 """ Adds more filters to the existing list of error-message filters. """
erg@google.come35f7652009-06-19 20:52:09 +0000904 for filt in filters.split(','):
905 clean_filt = filt.strip()
906 if clean_filt:
907 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000908 for filt in self.filters:
909 if not (filt.startswith('+') or filt.startswith('-')):
910 raise ValueError('Every filter in --filters must start with + or -'
911 ' (%s does not)' % filt)
912
erg@google.com7430eef2014-07-28 22:33:46 +0000913 def BackupFilters(self):
914 """ Saves the current filter list to backup storage."""
915 self._filters_backup = self.filters[:]
916
917 def RestoreFilters(self):
918 """ Restores filters previously backed up."""
919 self.filters = self._filters_backup[:]
920
erg@google.coma868d2d2009-10-09 21:18:45 +0000921 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000922 """Sets the module's error statistic back to zero."""
923 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000924 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000925
erg@google.coma868d2d2009-10-09 21:18:45 +0000926 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000927 """Bumps the module's error statistic."""
928 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000929 if self.counting in ('toplevel', 'detailed'):
930 if self.counting != 'detailed':
931 category = category.split('/')[0]
932 if category not in self.errors_by_category:
933 self.errors_by_category[category] = 0
934 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000935
erg@google.coma868d2d2009-10-09 21:18:45 +0000936 def PrintErrorCounts(self):
937 """Print a summary of errors by category, and the total."""
938 for category, count in self.errors_by_category.iteritems():
939 sys.stderr.write('Category \'%s\' errors found: %d\n' %
940 (category, count))
LukeCze09f4782016-09-28 19:13:37 -0500941 sys.stdout.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000942
943_cpplint_state = _CppLintState()
944
945
946def _OutputFormat():
947 """Gets the module's output format."""
948 return _cpplint_state.output_format
949
950
951def _SetOutputFormat(output_format):
952 """Sets the module's output format."""
953 _cpplint_state.SetOutputFormat(output_format)
954
955
956def _VerboseLevel():
957 """Returns the module's verbosity setting."""
958 return _cpplint_state.verbose_level
959
960
961def _SetVerboseLevel(level):
962 """Sets the module's verbosity, and returns the previous setting."""
963 return _cpplint_state.SetVerboseLevel(level)
964
965
erg@google.coma868d2d2009-10-09 21:18:45 +0000966def _SetCountingStyle(level):
967 """Sets the module's counting options."""
968 _cpplint_state.SetCountingStyle(level)
969
970
erg@google.com4e00b9a2009-01-12 23:05:11 +0000971def _Filters():
972 """Returns the module's list of output filters, as a list."""
973 return _cpplint_state.filters
974
975
976def _SetFilters(filters):
977 """Sets the module's error-message filters.
978
979 These filters are applied when deciding whether to emit a given
980 error message.
981
982 Args:
983 filters: A string of comma-separated filters (eg "whitespace/indent").
984 Each filter should start with + or -; else we die.
985 """
986 _cpplint_state.SetFilters(filters)
987
erg@google.com7430eef2014-07-28 22:33:46 +0000988def _AddFilters(filters):
989 """Adds more filter overrides.
990
991 Unlike _SetFilters, this function does not reset the current list of filters
992 available.
993
994 Args:
995 filters: A string of comma-separated filters (eg "whitespace/indent").
996 Each filter should start with + or -; else we die.
997 """
998 _cpplint_state.AddFilters(filters)
999
1000def _BackupFilters():
1001 """ Saves the current filter list to backup storage."""
1002 _cpplint_state.BackupFilters()
1003
1004def _RestoreFilters():
1005 """ Restores filters previously backed up."""
1006 _cpplint_state.RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001007
1008class _FunctionState(object):
1009 """Tracks current function name and the number of lines in its body."""
1010
1011 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
1012 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
1013
1014 def __init__(self):
1015 self.in_a_function = False
1016 self.lines_in_function = 0
1017 self.current_function = ''
1018
1019 def Begin(self, function_name):
1020 """Start analyzing function body.
1021
1022 Args:
1023 function_name: The name of the function being tracked.
1024 """
1025 self.in_a_function = True
1026 self.lines_in_function = 0
1027 self.current_function = function_name
1028
1029 def Count(self):
1030 """Count line in current function body."""
1031 if self.in_a_function:
1032 self.lines_in_function += 1
1033
1034 def Check(self, error, filename, linenum):
1035 """Report if too many lines in function body.
1036
1037 Args:
1038 error: The function to call with any errors found.
1039 filename: The name of the current file.
1040 linenum: The number of the line to check.
1041 """
Alex Vakulenko01e47232016-05-06 13:54:15 -07001042 if not self.in_a_function:
1043 return
1044
erg@google.com4e00b9a2009-01-12 23:05:11 +00001045 if Match(r'T(EST|est)', self.current_function):
1046 base_trigger = self._TEST_TRIGGER
1047 else:
1048 base_trigger = self._NORMAL_TRIGGER
1049 trigger = base_trigger * 2**_VerboseLevel()
1050
1051 if self.lines_in_function > trigger:
1052 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
1053 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1054 if error_level > 5:
1055 error_level = 5
1056 error(filename, linenum, 'readability/fn_size', error_level,
1057 'Small and focused functions are preferred:'
1058 ' %s has %d non-comment lines'
1059 ' (error triggered by exceeding %d lines).' % (
1060 self.current_function, self.lines_in_function, trigger))
1061
1062 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001063 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001064 self.in_a_function = False
1065
1066
1067class _IncludeError(Exception):
1068 """Indicates a problem with the include order in a file."""
1069 pass
1070
1071
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001072class FileInfo(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001073 """Provides utility functions for filenames.
1074
1075 FileInfo provides easy access to the components of a file's path
1076 relative to the project root.
1077 """
1078
1079 def __init__(self, filename):
1080 self._filename = filename
1081
1082 def FullName(self):
1083 """Make Windows paths like Unix."""
1084 return os.path.abspath(self._filename).replace('\\', '/')
1085
1086 def RepositoryName(self):
1087 """FullName after removing the local path to the repository.
1088
1089 If we have a real absolute path name here we can try to do something smart:
1090 detecting the root of the checkout and truncating /path/to/checkout from
1091 the name so that we get header guards that don't include things like
1092 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
1093 people on different computers who have checked the source out to different
1094 locations won't see bogus errors.
1095 """
1096 fullname = self.FullName()
1097
1098 if os.path.exists(fullname):
1099 project_dir = os.path.dirname(fullname)
1100
1101 if os.path.exists(os.path.join(project_dir, ".svn")):
1102 # If there's a .svn file in the current directory, we recursively look
1103 # up the directory tree for the top of the SVN checkout
1104 root_dir = project_dir
1105 one_up_dir = os.path.dirname(root_dir)
1106 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1107 root_dir = os.path.dirname(root_dir)
1108 one_up_dir = os.path.dirname(one_up_dir)
1109
1110 prefix = os.path.commonprefix([root_dir, project_dir])
1111 return fullname[len(prefix) + 1:]
1112
erg@google.com3dc74262011-11-30 01:12:00 +00001113 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1114 # searching up from the current path.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001115 root_dir = current_dir = os.path.dirname(fullname)
1116 while current_dir != os.path.dirname(current_dir):
1117 if (os.path.exists(os.path.join(current_dir, ".git")) or
1118 os.path.exists(os.path.join(current_dir, ".hg")) or
1119 os.path.exists(os.path.join(current_dir, ".svn"))):
1120 root_dir = current_dir
1121 current_dir = os.path.dirname(current_dir)
erg@google.com42e59b02010-10-04 22:18:07 +00001122
1123 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +00001124 os.path.exists(os.path.join(root_dir, ".hg")) or
1125 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +00001126 prefix = os.path.commonprefix([root_dir, project_dir])
1127 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +00001128
1129 # Don't know what to do; header guard warnings may be wrong...
1130 return fullname
1131
1132 def Split(self):
1133 """Splits the file into the directory, basename, and extension.
1134
1135 For 'chrome/browser/browser.cc', Split() would
1136 return ('chrome/browser', 'browser', '.cc')
1137
1138 Returns:
1139 A tuple of (directory, basename, extension).
1140 """
1141
1142 googlename = self.RepositoryName()
1143 project, rest = os.path.split(googlename)
1144 return (project,) + os.path.splitext(rest)
1145
1146 def BaseName(self):
1147 """File base name - text after the final slash, before the final period."""
1148 return self.Split()[1]
1149
1150 def Extension(self):
1151 """File extension - text following the final period."""
1152 return self.Split()[2]
1153
1154 def NoExtension(self):
1155 """File has no source file extension."""
1156 return '/'.join(self.Split()[0:2])
1157
1158 def IsSource(self):
1159 """File has a source file extension."""
Alex Vakulenko01e47232016-05-06 13:54:15 -07001160 return _IsSourceExtension(self.Extension()[1:])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001161
1162
erg+personal@google.com05189642010-04-30 20:43:03 +00001163def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001164 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +00001165
1166 # There are three ways we might decide not to print an error message:
1167 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +00001168 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +00001169 if IsErrorSuppressedByNolint(category, linenum):
1170 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00001171
erg@google.com4e00b9a2009-01-12 23:05:11 +00001172 if confidence < _cpplint_state.verbose_level:
1173 return False
1174
1175 is_filtered = False
1176 for one_filter in _Filters():
1177 if one_filter.startswith('-'):
1178 if category.startswith(one_filter[1:]):
1179 is_filtered = True
1180 elif one_filter.startswith('+'):
1181 if category.startswith(one_filter[1:]):
1182 is_filtered = False
1183 else:
1184 assert False # should have been checked for in SetFilter.
1185 if is_filtered:
1186 return False
1187
1188 return True
1189
1190
1191def Error(filename, linenum, category, confidence, message):
1192 """Logs the fact we've found a lint error.
1193
1194 We log where the error was found, and also our confidence in the error,
1195 that is, how certain we are this is a legitimate style regression, and
1196 not a misidentification or a use that's sometimes justified.
1197
erg+personal@google.com05189642010-04-30 20:43:03 +00001198 False positives can be suppressed by the use of
1199 "cpplint(category)" comments on the offending line. These are
1200 parsed into _error_suppressions.
1201
erg@google.com4e00b9a2009-01-12 23:05:11 +00001202 Args:
1203 filename: The name of the file containing the error.
1204 linenum: The number of the line containing the error.
1205 category: A string used to describe the "category" this bug
1206 falls under: "whitespace", say, or "runtime". Categories
1207 may have a hierarchy separated by slashes: "whitespace/indent".
1208 confidence: A number from 1-5 representing a confidence score for
1209 the error, with 5 meaning that we are certain of the problem,
1210 and 1 meaning that it could be a legitimate construct.
1211 message: The error message.
1212 """
erg+personal@google.com05189642010-04-30 20:43:03 +00001213 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +00001214 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001215 if _cpplint_state.output_format == 'vs7':
schoetbi819c5722017-05-03 10:09:12 +02001216 sys.stderr.write('%s(%s): error cpplint: [%s] %s [%d]\n' % (
1217 filename, linenum, category, message, confidence))
erg@google.com02c27fd2013-05-28 21:34:34 +00001218 elif _cpplint_state.output_format == 'eclipse':
1219 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1220 filename, linenum, message, category, confidence))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001221 else:
1222 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1223 filename, linenum, message, category, confidence))
1224
1225
erg@google.com2aa59982013-10-28 19:09:25 +00001226# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001227_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1228 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.com02af6282014-06-04 18:53:25 +00001229# Match a single C style comment on the same line.
1230_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1231# Matches multi-line C style comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001232# This RE is a little bit more complicated than one might expect, because we
1233# have to take care of space removals tools so we can handle comments inside
1234# statements better.
1235# The current rule is: We only clear spaces from both sides when we're at the
1236# end of the line. Otherwise, we try to remove spaces from the right side,
1237# if this doesn't work we try on left side but only if there's a non-character
1238# on the right.
1239_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.com02af6282014-06-04 18:53:25 +00001240 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1241 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1242 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1243 _RE_PATTERN_C_COMMENTS + r')')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001244
1245
1246def IsCppString(line):
1247 """Does line terminate so, that the next symbol is in string constant.
1248
1249 This function does not consider single-line nor multi-line comments.
1250
1251 Args:
1252 line: is a partial line of code starting from the 0..n.
1253
1254 Returns:
1255 True, if next character appended to 'line' is inside a
1256 string constant.
1257 """
1258
1259 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1260 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1261
1262
erg@google.com2aa59982013-10-28 19:09:25 +00001263def CleanseRawStrings(raw_lines):
1264 """Removes C++11 raw strings from lines.
1265
1266 Before:
1267 static const char kData[] = R"(
1268 multi-line string
1269 )";
1270
1271 After:
1272 static const char kData[] = ""
1273 (replaced by blank line)
1274 "";
1275
1276 Args:
1277 raw_lines: list of raw lines.
1278
1279 Returns:
1280 list of lines with C++11 raw strings replaced by empty strings.
1281 """
1282
1283 delimiter = None
1284 lines_without_raw_strings = []
1285 for line in raw_lines:
1286 if delimiter:
1287 # Inside a raw string, look for the end
1288 end = line.find(delimiter)
1289 if end >= 0:
1290 # Found the end of the string, match leading space for this
1291 # line and resume copying the original lines, and also insert
1292 # a "" on the last line.
1293 leading_space = Match(r'^(\s*)\S', line)
1294 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1295 delimiter = None
1296 else:
1297 # Haven't found the end yet, append a blank line.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001298 line = '""'
erg@google.com2aa59982013-10-28 19:09:25 +00001299
avakulenko@google.com02af6282014-06-04 18:53:25 +00001300 # Look for beginning of a raw string, and replace them with
1301 # empty strings. This is done in a loop to handle multiple raw
1302 # strings on the same line.
1303 while delimiter is None:
erg@google.com2aa59982013-10-28 19:09:25 +00001304 # Look for beginning of a raw string.
1305 # See 2.14.15 [lex.string] for syntax.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001306 #
1307 # Once we have matched a raw string, we check the prefix of the
1308 # line to make sure that the line is not part of a single line
1309 # comment. It's done this way because we remove raw strings
1310 # before removing comments as opposed to removing comments
1311 # before removing raw strings. This is because there are some
1312 # cpplint checks that requires the comments to be preserved, but
1313 # we don't want to check comments that are inside raw strings.
1314 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1315 if (matched and
1316 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
1317 matched.group(1))):
erg@google.com2aa59982013-10-28 19:09:25 +00001318 delimiter = ')' + matched.group(2) + '"'
1319
1320 end = matched.group(3).find(delimiter)
1321 if end >= 0:
1322 # Raw string ended on same line
1323 line = (matched.group(1) + '""' +
1324 matched.group(3)[end + len(delimiter):])
1325 delimiter = None
1326 else:
1327 # Start of a multi-line raw string
1328 line = matched.group(1) + '""'
avakulenko@google.com02af6282014-06-04 18:53:25 +00001329 else:
1330 break
erg@google.com2aa59982013-10-28 19:09:25 +00001331
1332 lines_without_raw_strings.append(line)
1333
1334 # TODO(unknown): if delimiter is not None here, we might want to
1335 # emit a warning for unterminated string.
1336 return lines_without_raw_strings
1337
1338
erg@google.com4e00b9a2009-01-12 23:05:11 +00001339def FindNextMultiLineCommentStart(lines, lineix):
1340 """Find the beginning marker for a multiline comment."""
1341 while lineix < len(lines):
1342 if lines[lineix].strip().startswith('/*'):
1343 # Only return this marker if the comment goes beyond this line
1344 if lines[lineix].strip().find('*/', 2) < 0:
1345 return lineix
1346 lineix += 1
1347 return len(lines)
1348
1349
1350def FindNextMultiLineCommentEnd(lines, lineix):
1351 """We are inside a comment, find the end marker."""
1352 while lineix < len(lines):
1353 if lines[lineix].strip().endswith('*/'):
1354 return lineix
1355 lineix += 1
1356 return len(lines)
1357
1358
1359def RemoveMultiLineCommentsFromRange(lines, begin, end):
1360 """Clears a range of lines for multi-line comments."""
1361 # Having // dummy comments makes the lines non-empty, so we will not get
1362 # unnecessary blank line warnings later in the code.
1363 for i in range(begin, end):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001364 lines[i] = '/**/'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001365
1366
1367def RemoveMultiLineComments(filename, lines, error):
1368 """Removes multiline (c-style) comments from lines."""
1369 lineix = 0
1370 while lineix < len(lines):
1371 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1372 if lineix_begin >= len(lines):
1373 return
1374 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1375 if lineix_end >= len(lines):
1376 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1377 'Could not find end of multi-line comment')
1378 return
1379 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1380 lineix = lineix_end + 1
1381
1382
1383def CleanseComments(line):
1384 """Removes //-comments and single-line C-style /* */ comments.
1385
1386 Args:
1387 line: A line of C++ source.
1388
1389 Returns:
1390 The line with single-line comments removed.
1391 """
1392 commentpos = line.find('//')
1393 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +00001394 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001395 # get rid of /* ... */
1396 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1397
1398
erg@google.coma87abb82009-02-24 01:41:01 +00001399class CleansedLines(object):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001400 """Holds 4 copies of all lines with different preprocessing applied to them.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001401
avakulenko@google.com554223d2014-12-04 22:00:20 +00001402 1) elided member contains lines without strings and comments.
1403 2) lines member contains lines without comments.
erg@google.comd350fe52013-01-14 17:51:48 +00001404 3) raw_lines member contains all the lines without processing.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001405 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw
1406 strings removed.
1407 All these members are of <type 'list'>, and of the same length.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001408 """
1409
1410 def __init__(self, lines):
1411 self.elided = []
1412 self.lines = []
1413 self.raw_lines = lines
1414 self.num_lines = len(lines)
erg@google.com2aa59982013-10-28 19:09:25 +00001415 self.lines_without_raw_strings = CleanseRawStrings(lines)
1416 for linenum in range(len(self.lines_without_raw_strings)):
1417 self.lines.append(CleanseComments(
1418 self.lines_without_raw_strings[linenum]))
1419 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001420 self.elided.append(CleanseComments(elided))
1421
1422 def NumLines(self):
1423 """Returns the number of lines represented."""
1424 return self.num_lines
1425
1426 @staticmethod
1427 def _CollapseStrings(elided):
1428 """Collapses strings and chars on a line to simple "" or '' blocks.
1429
1430 We nix strings first so we're not fooled by text like '"http://"'
1431
1432 Args:
1433 elided: The line being processed.
1434
1435 Returns:
1436 The line with collapsed strings.
1437 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001438 if _RE_PATTERN_INCLUDE.match(elided):
1439 return elided
1440
1441 # Remove escaped characters first to make quote/single quote collapsing
1442 # basic. Things that look like escaped characters shouldn't occur
1443 # outside of strings and chars.
1444 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1445
1446 # Replace quoted strings and digit separators. Both single quotes
1447 # and double quotes are processed in the same loop, otherwise
1448 # nested quotes wouldn't work.
1449 collapsed = ''
1450 while True:
1451 # Find the first quote character
1452 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1453 if not match:
1454 collapsed += elided
1455 break
1456 head, quote, tail = match.groups()
1457
1458 if quote == '"':
1459 # Collapse double quoted strings
1460 second_quote = tail.find('"')
1461 if second_quote >= 0:
1462 collapsed += head + '""'
1463 elided = tail[second_quote + 1:]
1464 else:
1465 # Unmatched double quote, don't bother processing the rest
1466 # of the line since this is probably a multiline string.
1467 collapsed += elided
1468 break
1469 else:
1470 # Found single quote, check nearby text to eliminate digit separators.
1471 #
1472 # There is no special handling for floating point here, because
1473 # the integer/fractional/exponent parts would all be parsed
1474 # correctly as long as there are digits on both sides of the
1475 # separator. So we are fine as long as we don't see something
1476 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1477 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1478 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1479 collapsed += head + match_literal.group(1).replace("'", '')
1480 elided = match_literal.group(2)
1481 else:
1482 second_quote = tail.find('\'')
1483 if second_quote >= 0:
1484 collapsed += head + "''"
1485 elided = tail[second_quote + 1:]
1486 else:
1487 # Unmatched single quote
1488 collapsed += elided
1489 break
1490
1491 return collapsed
erg@google.com4e00b9a2009-01-12 23:05:11 +00001492
1493
avakulenko@google.com02af6282014-06-04 18:53:25 +00001494def FindEndOfExpressionInLine(line, startpos, stack):
1495 """Find the position just after the end of current parenthesized expression.
erg@google.comd350fe52013-01-14 17:51:48 +00001496
1497 Args:
1498 line: a CleansedLines line.
1499 startpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001500 stack: nesting stack at startpos.
erg@google.comd350fe52013-01-14 17:51:48 +00001501
1502 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001503 On finding matching end: (index just after matching end, None)
1504 On finding an unclosed expression: (-1, None)
1505 Otherwise: (-1, new stack at end of this line)
erg@google.comd350fe52013-01-14 17:51:48 +00001506 """
1507 for i in xrange(startpos, len(line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00001508 char = line[i]
1509 if char in '([{':
1510 # Found start of parenthesized expression, push to expression stack
1511 stack.append(char)
1512 elif char == '<':
1513 # Found potential start of template argument list
1514 if i > 0 and line[i - 1] == '<':
1515 # Left shift operator
1516 if stack and stack[-1] == '<':
1517 stack.pop()
1518 if not stack:
1519 return (-1, None)
1520 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1521 # operator<, don't add to stack
1522 continue
1523 else:
1524 # Tentative start of template argument list
1525 stack.append('<')
1526 elif char in ')]}':
1527 # Found end of parenthesized expression.
1528 #
1529 # If we are currently expecting a matching '>', the pending '<'
1530 # must have been an operator. Remove them from expression stack.
1531 while stack and stack[-1] == '<':
1532 stack.pop()
1533 if not stack:
1534 return (-1, None)
1535 if ((stack[-1] == '(' and char == ')') or
1536 (stack[-1] == '[' and char == ']') or
1537 (stack[-1] == '{' and char == '}')):
1538 stack.pop()
1539 if not stack:
1540 return (i + 1, None)
1541 else:
1542 # Mismatched parentheses
1543 return (-1, None)
1544 elif char == '>':
1545 # Found potential end of template argument list.
1546
1547 # Ignore "->" and operator functions
1548 if (i > 0 and
1549 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1550 continue
1551
1552 # Pop the stack if there is a matching '<'. Otherwise, ignore
1553 # this '>' since it must be an operator.
1554 if stack:
1555 if stack[-1] == '<':
1556 stack.pop()
1557 if not stack:
1558 return (i + 1, None)
1559 elif char == ';':
1560 # Found something that look like end of statements. If we are currently
1561 # expecting a '>', the matching '<' must have been an operator, since
1562 # template argument list should not contain statements.
1563 while stack and stack[-1] == '<':
1564 stack.pop()
1565 if not stack:
1566 return (-1, None)
1567
1568 # Did not find end of expression or unbalanced parentheses on this line
1569 return (-1, stack)
erg@google.comd350fe52013-01-14 17:51:48 +00001570
1571
erg@google.com4e00b9a2009-01-12 23:05:11 +00001572def CloseExpression(clean_lines, linenum, pos):
erg@google.com2aa59982013-10-28 19:09:25 +00001573 """If input points to ( or { or [ or <, finds the position that closes it.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001574
erg@google.com2aa59982013-10-28 19:09:25 +00001575 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001576 linenum/pos that correspond to the closing of the expression.
1577
avakulenko@google.com02af6282014-06-04 18:53:25 +00001578 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1579 Ideally we would want to index all opening and closing parentheses once
1580 and have CloseExpression be just a simple lookup, but due to preprocessor
1581 tricks, this is not so easy.
1582
erg@google.com4e00b9a2009-01-12 23:05:11 +00001583 Args:
1584 clean_lines: A CleansedLines instance containing the file.
1585 linenum: The number of the line to check.
1586 pos: A position on the line.
1587
1588 Returns:
1589 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1590 (line, len(lines), -1) if we never find a close. Note we ignore
1591 strings and comments when matching; and the line we return is the
1592 'cleansed' line at linenum.
1593 """
1594
1595 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001596 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001597 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001598
erg@google.comd350fe52013-01-14 17:51:48 +00001599 # Check first line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001600 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
erg@google.comd350fe52013-01-14 17:51:48 +00001601 if end_pos > -1:
1602 return (line, linenum, end_pos)
erg@google.com2aa59982013-10-28 19:09:25 +00001603
1604 # Continue scanning forward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001605 while stack and linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001606 linenum += 1
1607 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001608 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001609 if end_pos > -1:
1610 return (line, linenum, end_pos)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001611
avakulenko@google.com02af6282014-06-04 18:53:25 +00001612 # Did not find end of expression before end of file, give up
erg@google.comd350fe52013-01-14 17:51:48 +00001613 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001614
erg@google.com2aa59982013-10-28 19:09:25 +00001615
avakulenko@google.com02af6282014-06-04 18:53:25 +00001616def FindStartOfExpressionInLine(line, endpos, stack):
1617 """Find position at the matching start of current expression.
erg@google.com2aa59982013-10-28 19:09:25 +00001618
1619 This is almost the reverse of FindEndOfExpressionInLine, but note
1620 that the input position and returned position differs by 1.
1621
1622 Args:
1623 line: a CleansedLines line.
1624 endpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001625 stack: nesting stack at endpos.
erg@google.com2aa59982013-10-28 19:09:25 +00001626
1627 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001628 On finding matching start: (index at matching start, None)
1629 On finding an unclosed expression: (-1, None)
1630 Otherwise: (-1, new stack at beginning of this line)
erg@google.com2aa59982013-10-28 19:09:25 +00001631 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001632 i = endpos
1633 while i >= 0:
1634 char = line[i]
1635 if char in ')]}':
1636 # Found end of expression, push to expression stack
1637 stack.append(char)
1638 elif char == '>':
1639 # Found potential end of template argument list.
1640 #
1641 # Ignore it if it's a "->" or ">=" or "operator>"
1642 if (i > 0 and
1643 (line[i - 1] == '-' or
1644 Match(r'\s>=\s', line[i - 1:]) or
1645 Search(r'\boperator\s*$', line[0:i]))):
1646 i -= 1
1647 else:
1648 stack.append('>')
1649 elif char == '<':
1650 # Found potential start of template argument list
1651 if i > 0 and line[i - 1] == '<':
1652 # Left shift operator
1653 i -= 1
1654 else:
1655 # If there is a matching '>', we can pop the expression stack.
1656 # Otherwise, ignore this '<' since it must be an operator.
1657 if stack and stack[-1] == '>':
1658 stack.pop()
1659 if not stack:
1660 return (i, None)
1661 elif char in '([{':
1662 # Found start of expression.
1663 #
1664 # If there are any unmatched '>' on the stack, they must be
1665 # operators. Remove those.
1666 while stack and stack[-1] == '>':
1667 stack.pop()
1668 if not stack:
1669 return (-1, None)
1670 if ((char == '(' and stack[-1] == ')') or
1671 (char == '[' and stack[-1] == ']') or
1672 (char == '{' and stack[-1] == '}')):
1673 stack.pop()
1674 if not stack:
1675 return (i, None)
1676 else:
1677 # Mismatched parentheses
1678 return (-1, None)
1679 elif char == ';':
1680 # Found something that look like end of statements. If we are currently
1681 # expecting a '<', the matching '>' must have been an operator, since
1682 # template argument list should not contain statements.
1683 while stack and stack[-1] == '>':
1684 stack.pop()
1685 if not stack:
1686 return (-1, None)
1687
1688 i -= 1
1689
1690 return (-1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001691
1692
1693def ReverseCloseExpression(clean_lines, linenum, pos):
1694 """If input points to ) or } or ] or >, finds the position that opens it.
1695
1696 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1697 linenum/pos that correspond to the opening of the expression.
1698
1699 Args:
1700 clean_lines: A CleansedLines instance containing the file.
1701 linenum: The number of the line to check.
1702 pos: A position on the line.
1703
1704 Returns:
1705 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1706 (line, 0, -1) if we never find the matching opening brace. Note
1707 we ignore strings and comments when matching; and the line we
1708 return is the 'cleansed' line at linenum.
1709 """
1710 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001711 if line[pos] not in ')}]>':
erg@google.com2aa59982013-10-28 19:09:25 +00001712 return (line, 0, -1)
erg@google.com2aa59982013-10-28 19:09:25 +00001713
1714 # Check last line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001715 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
erg@google.com2aa59982013-10-28 19:09:25 +00001716 if start_pos > -1:
1717 return (line, linenum, start_pos)
1718
1719 # Continue scanning backward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001720 while stack and linenum > 0:
erg@google.com2aa59982013-10-28 19:09:25 +00001721 linenum -= 1
1722 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001723 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001724 if start_pos > -1:
1725 return (line, linenum, start_pos)
1726
avakulenko@google.com02af6282014-06-04 18:53:25 +00001727 # Did not find start of expression before beginning of file, give up
erg@google.com2aa59982013-10-28 19:09:25 +00001728 return (line, 0, -1)
1729
1730
erg@google.com4e00b9a2009-01-12 23:05:11 +00001731def CheckForCopyright(filename, lines, error):
1732 """Logs an error if no Copyright message appears at the top of the file."""
1733
1734 # We'll say it should occur by line 10. Don't forget there's a
1735 # dummy line at the front.
1736 for line in xrange(1, min(len(lines), 11)):
1737 if re.search(r'Copyright', lines[line], re.I): break
1738 else: # means no copyright line was found
1739 error(filename, 0, 'legal/copyright', 5,
1740 'No copyright message found. '
1741 'You should have a line: "Copyright [year] <Copyright Owner>"')
1742
1743
avakulenko@google.com02af6282014-06-04 18:53:25 +00001744def GetIndentLevel(line):
1745 """Return the number of leading spaces in line.
1746
1747 Args:
1748 line: A string to check.
1749
1750 Returns:
1751 An integer count of leading spaces, possibly zero.
1752 """
1753 indent = Match(r'^( *)\S', line)
1754 if indent:
1755 return len(indent.group(1))
1756 else:
1757 return 0
1758
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001759def PathSplitToList(path):
1760 """Returns the path split into a list by the separator.
1761
1762 Args:
1763 path: An absolute or relative path (e.g. '/a/b/c/' or '../a')
1764
1765 Returns:
1766 A list of path components (e.g. ['a', 'b', 'c]).
1767 """
1768 lst = []
1769 while True:
1770 (head, tail) = os.path.split(path)
1771 if head == path: # absolute paths end
1772 lst.append(head)
1773 break
1774 if tail == path: # relative paths end
1775 lst.append(tail)
1776 break
1777
1778 path = head
1779 lst.append(tail)
1780
1781 lst.reverse()
1782 return lst
avakulenko@google.com02af6282014-06-04 18:53:25 +00001783
erg@google.com4e00b9a2009-01-12 23:05:11 +00001784def GetHeaderGuardCPPVariable(filename):
1785 """Returns the CPP variable that should be used as a header guard.
1786
1787 Args:
1788 filename: The name of a C++ header file.
1789
1790 Returns:
1791 The CPP variable that should be used as a header guard in the
1792 named file.
1793
1794 """
1795
erg+personal@google.com05189642010-04-30 20:43:03 +00001796 # Restores original filename in case that cpplint is invoked from Emacs's
1797 # flymake.
1798 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001799 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001800 # Replace 'c++' with 'cpp'.
1801 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')
Alex Vakulenko01e47232016-05-06 13:54:15 -07001802
erg@google.com4e00b9a2009-01-12 23:05:11 +00001803 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001804 file_path_from_root = fileinfo.RepositoryName()
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001805
1806 def FixupPathFromRoot():
Igor Murashkin8a87a462017-11-09 13:48:29 -08001807 if _root_debug:
1808 sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n"
1809 %(_root, fileinfo.RepositoryName()))
1810
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001811 # Process the file path with the --root flag if it was set.
1812 if not _root:
Igor Murashkin8a87a462017-11-09 13:48:29 -08001813 if _root_debug:
1814 sys.stderr.write("_root unspecified\n")
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001815 return file_path_from_root
1816
1817 def StripListPrefix(lst, prefix):
1818 # f(['x', 'y'], ['w, z']) -> None (not a valid prefix)
1819 if lst[:len(prefix)] != prefix:
1820 return None
1821 # f(['a, 'b', 'c', 'd'], ['a', 'b']) -> ['c', 'd']
1822 return lst[(len(prefix)):]
1823
1824 # root behavior:
1825 # --root=subdir , lstrips subdir from the header guard
1826 maybe_path = StripListPrefix(PathSplitToList(file_path_from_root),
1827 PathSplitToList(_root))
Igor Murashkin8a87a462017-11-09 13:48:29 -08001828
1829 if _root_debug:
1830 sys.stderr.write("_root lstrip (maybe_path=%s, file_path_from_root=%s," +
1831 " _root=%s)\n" %(maybe_path, file_path_from_root, _root))
1832
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001833 if maybe_path:
1834 return os.path.join(*maybe_path)
1835
1836 # --root=.. , will prepend the outer directory to the header guard
1837 full_path = fileinfo.FullName()
1838 root_abspath = os.path.abspath(_root)
1839
1840 maybe_path = StripListPrefix(PathSplitToList(full_path),
1841 PathSplitToList(root_abspath))
Igor Murashkin8a87a462017-11-09 13:48:29 -08001842
1843 if _root_debug:
1844 sys.stderr.write("_root prepend (maybe_path=%s, full_path=%s, " +
1845 "root_abspath=%s)\n" %(maybe_path, full_path, root_abspath))
1846
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001847 if maybe_path:
1848 return os.path.join(*maybe_path)
1849
Igor Murashkin8a87a462017-11-09 13:48:29 -08001850 if _root_debug:
1851 sys.stderr.write("_root ignore, returning %s\n" %(file_path_from_root))
1852
Igor Murashkine7ddd2a2017-11-06 15:00:05 -08001853 # --root=FAKE_DIR is ignored
1854 return file_path_from_root
1855
1856 file_path_from_root = FixupPathFromRoot()
avakulenko@google.com554223d2014-12-04 22:00:20 +00001857 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001858
1859
avakulenko@google.com554223d2014-12-04 22:00:20 +00001860def CheckForHeaderGuard(filename, clean_lines, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001861 """Checks that the file contains a header guard.
1862
erg@google.coma87abb82009-02-24 01:41:01 +00001863 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001864 headers, checks that the full pathname is used.
1865
1866 Args:
1867 filename: The name of the C++ header file.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001868 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001869 error: The function to call with any errors found.
1870 """
1871
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001872 # Don't check for header guards if there are error suppression
1873 # comments somewhere in this file.
1874 #
1875 # Because this is silencing a warning for a nonexistent line, we
1876 # only support the very specific NOLINT(build/header_guard) syntax,
1877 # and not the general NOLINT or NOLINT(*) syntax.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001878 raw_lines = clean_lines.lines_without_raw_strings
1879 for i in raw_lines:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001880 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1881 return
1882
erg@google.com4e00b9a2009-01-12 23:05:11 +00001883 cppvar = GetHeaderGuardCPPVariable(filename)
1884
avakulenko@google.com554223d2014-12-04 22:00:20 +00001885 ifndef = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001886 ifndef_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001887 define = ''
1888 endif = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001889 endif_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001890 for linenum, line in enumerate(raw_lines):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001891 linesplit = line.split()
1892 if len(linesplit) >= 2:
1893 # find the first occurrence of #ifndef and #define, save arg
1894 if not ifndef and linesplit[0] == '#ifndef':
1895 # set ifndef to the header guard presented on the #ifndef line.
1896 ifndef = linesplit[1]
1897 ifndef_linenum = linenum
1898 if not define and linesplit[0] == '#define':
1899 define = linesplit[1]
1900 # find the last occurrence of #endif, save entire line
1901 if line.startswith('#endif'):
1902 endif = line
1903 endif_linenum = linenum
1904
avakulenko@google.com554223d2014-12-04 22:00:20 +00001905 if not ifndef or not define or ifndef != define:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001906 error(filename, 0, 'build/header_guard', 5,
1907 'No #ifndef header guard found, suggested CPP variable is: %s' %
1908 cppvar)
1909 return
1910
1911 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1912 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001913 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001914 error_level = 0
1915 if ifndef != cppvar + '_':
1916 error_level = 5
1917
avakulenko@google.com554223d2014-12-04 22:00:20 +00001918 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum,
erg+personal@google.com05189642010-04-30 20:43:03 +00001919 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001920 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1921 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1922
avakulenko@google.com554223d2014-12-04 22:00:20 +00001923 # Check for "//" comments on endif line.
1924 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum,
1925 error)
1926 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif)
1927 if match:
1928 if match.group(1) == '_':
1929 # Issue low severity warning for deprecated double trailing underscore
1930 error(filename, endif_linenum, 'build/header_guard', 0,
1931 '#endif line should be "#endif // %s"' % cppvar)
erg@google.comdc289702012-01-26 20:30:03 +00001932 return
1933
avakulenko@google.com554223d2014-12-04 22:00:20 +00001934 # Didn't find the corresponding "//" comment. If this file does not
1935 # contain any "//" comments at all, it could be that the compiler
1936 # only wants "/**/" comments, look for those instead.
1937 no_single_line_comments = True
1938 for i in xrange(1, len(raw_lines) - 1):
1939 line = raw_lines[i]
1940 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
1941 no_single_line_comments = False
1942 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00001943
avakulenko@google.com554223d2014-12-04 22:00:20 +00001944 if no_single_line_comments:
1945 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif)
1946 if match:
1947 if match.group(1) == '_':
1948 # Low severity warning for double trailing underscore
1949 error(filename, endif_linenum, 'build/header_guard', 0,
1950 '#endif line should be "#endif /* %s */"' % cppvar)
1951 return
1952
1953 # Didn't find anything
1954 error(filename, endif_linenum, 'build/header_guard', 5,
1955 '#endif line should be "#endif // %s"' % cppvar)
1956
1957
1958def CheckHeaderFileIncluded(filename, include_state, error):
1959 """Logs an error if a .cc file does not include its header."""
1960
1961 # Do not check test files
Alex Vakulenko01e47232016-05-06 13:54:15 -07001962 fileinfo = FileInfo(filename)
1963 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001964 return
1965
Alex Vakulenko01e47232016-05-06 13:54:15 -07001966 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h'
avakulenko@google.com554223d2014-12-04 22:00:20 +00001967 if not os.path.exists(headerfile):
1968 return
1969 headername = FileInfo(headerfile).RepositoryName()
1970 first_include = 0
1971 for section_list in include_state.include_list:
1972 for f in section_list:
1973 if headername in f[0] or f[0] in headername:
1974 return
1975 if not first_include:
1976 first_include = f[1]
1977
1978 error(filename, first_include, 'build/include', 5,
1979 '%s should include its header file %s' % (fileinfo.RepositoryName(),
1980 headername))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001981
1982
erg@google.com2aa59982013-10-28 19:09:25 +00001983def CheckForBadCharacters(filename, lines, error):
1984 """Logs an error for each line containing bad characters.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001985
erg@google.com2aa59982013-10-28 19:09:25 +00001986 Two kinds of bad characters:
1987
1988 1. Unicode replacement characters: These indicate that either the file
1989 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1990 it shouldn't). Note that it's possible for this to throw off line
1991 numbering if the invalid UTF-8 occurred adjacent to a newline.
1992
1993 2. NUL bytes. These are problematic for some tools.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001994
1995 Args:
1996 filename: The name of the current file.
1997 lines: An array of strings, each representing a line of the file.
1998 error: The function to call with any errors found.
1999 """
2000 for linenum, line in enumerate(lines):
2001 if u'\ufffd' in line:
2002 error(filename, linenum, 'readability/utf8', 5,
2003 'Line contains invalid UTF-8 (or Unicode replacement character).')
erg@google.com2aa59982013-10-28 19:09:25 +00002004 if '\0' in line:
2005 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002006
2007
2008def CheckForNewlineAtEOF(filename, lines, error):
2009 """Logs an error if there is no newline char at the end of the file.
2010
2011 Args:
2012 filename: The name of the current file.
2013 lines: An array of strings, each representing a line of the file.
2014 error: The function to call with any errors found.
2015 """
2016
2017 # The array lines() was created by adding two newlines to the
2018 # original file (go figure), then splitting on \n.
2019 # To verify that the file ends in \n, we just have to make sure the
2020 # last-but-two element of lines() exists and is empty.
2021 if len(lines) < 3 or lines[-2]:
2022 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
2023 'Could not find a newline character at the end of the file.')
2024
2025
2026def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
2027 """Logs an error if we see /* ... */ or "..." that extend past one line.
2028
2029 /* ... */ comments are legit inside macros, for one line.
2030 Otherwise, we prefer // comments, so it's ok to warn about the
2031 other. Likewise, it's ok for strings to extend across multiple
2032 lines, as long as a line continuation character (backslash)
2033 terminates each line. Although not currently prohibited by the C++
2034 style guide, it's ugly and unnecessary. We don't do well with either
2035 in this lint program, so we warn about both.
2036
2037 Args:
2038 filename: The name of the current file.
2039 clean_lines: A CleansedLines instance containing the file.
2040 linenum: The number of the line to check.
2041 error: The function to call with any errors found.
2042 """
2043 line = clean_lines.elided[linenum]
2044
2045 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
2046 # second (escaped) slash may trigger later \" detection erroneously.
2047 line = line.replace('\\\\', '')
2048
2049 if line.count('/*') > line.count('*/'):
2050 error(filename, linenum, 'readability/multiline_comment', 5,
2051 'Complex multi-line /*...*/-style comment found. '
2052 'Lint may give bogus warnings. '
2053 'Consider replacing these with //-style comments, '
2054 'with #if 0...#endif, '
2055 'or with more clearly structured multi-line comments.')
2056
2057 if (line.count('"') - line.count('\\"')) % 2:
2058 error(filename, linenum, 'readability/multiline_string', 5,
2059 'Multi-line string ("...") found. This lint script doesn\'t '
erg@google.com2aa59982013-10-28 19:09:25 +00002060 'do well with such strings, and may give bogus warnings. '
2061 'Use C++11 raw strings or concatenation instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002062
2063
avakulenko@google.com02af6282014-06-04 18:53:25 +00002064# (non-threadsafe name, thread-safe alternative, validation pattern)
2065#
2066# The validation pattern is used to eliminate false positives such as:
2067# _rand(); // false positive due to substring match.
2068# ->rand(); // some member function rand().
2069# ACMRandom rand(seed); // some variable named rand.
2070# ISAACRandom rand(); // another variable named rand.
2071#
2072# Basically we require the return value of these functions to be used
2073# in some expression context on the same line by matching on some
2074# operator before the function name. This eliminates constructors and
2075# member function calls.
2076_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
2077_THREADING_LIST = (
2078 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
2079 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
2080 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
2081 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
2082 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
2083 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
2084 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
2085 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
2086 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
2087 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
2088 ('strtok(', 'strtok_r(',
2089 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
2090 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
erg@google.com4e00b9a2009-01-12 23:05:11 +00002091 )
2092
2093
2094def CheckPosixThreading(filename, clean_lines, linenum, error):
2095 """Checks for calls to thread-unsafe functions.
2096
2097 Much code has been originally written without consideration of
2098 multi-threading. Also, engineers are relying on their old experience;
2099 they have learned posix before threading extensions were added. These
2100 tests guide the engineers to use thread-safe functions (when using
2101 posix directly).
2102
2103 Args:
2104 filename: The name of the current file.
2105 clean_lines: A CleansedLines instance containing the file.
2106 linenum: The number of the line to check.
2107 error: The function to call with any errors found.
2108 """
2109 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00002110 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
2111 # Additional pattern matching check to confirm that this is the
2112 # function we are looking for
2113 if Search(pattern, line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002114 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.com02af6282014-06-04 18:53:25 +00002115 'Consider using ' + multithread_safe_func +
2116 '...) instead of ' + single_thread_func +
erg@google.com4e00b9a2009-01-12 23:05:11 +00002117 '...) for improved thread safety.')
2118
2119
erg@google.com2aa59982013-10-28 19:09:25 +00002120def CheckVlogArguments(filename, clean_lines, linenum, error):
2121 """Checks that VLOG() is only used for defining a logging level.
2122
2123 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
2124 VLOG(FATAL) are not.
2125
2126 Args:
2127 filename: The name of the current file.
2128 clean_lines: A CleansedLines instance containing the file.
2129 linenum: The number of the line to check.
2130 error: The function to call with any errors found.
2131 """
2132 line = clean_lines.elided[linenum]
2133 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
2134 error(filename, linenum, 'runtime/vlog', 5,
2135 'VLOG() should be used with numeric verbosity level. '
2136 'Use LOG() if you want symbolic severity levels.')
2137
erg@google.coma868d2d2009-10-09 21:18:45 +00002138# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00002139# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00002140_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00002141 r'^\s*\*\w+(\+\+|--);')
2142
2143
2144def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00002145 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00002146
2147 For example following function:
2148 void increment_counter(int* count) {
2149 *count++;
2150 }
2151 is invalid, because it effectively does count++, moving pointer, and should
2152 be replaced with ++*count, (*count)++ or *count += 1.
2153
2154 Args:
2155 filename: The name of the current file.
2156 clean_lines: A CleansedLines instance containing the file.
2157 linenum: The number of the line to check.
2158 error: The function to call with any errors found.
2159 """
2160 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00002161 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00002162 error(filename, linenum, 'runtime/invalid_increment', 5,
2163 'Changing pointer instead of value (or unused value of operator*).')
2164
2165
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002166def IsMacroDefinition(clean_lines, linenum):
2167 if Search(r'^#define', clean_lines[linenum]):
2168 return True
2169
2170 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
2171 return True
2172
2173 return False
2174
2175
2176def IsForwardClassDeclaration(clean_lines, linenum):
2177 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
2178
2179
erg@google.comd350fe52013-01-14 17:51:48 +00002180class _BlockInfo(object):
2181 """Stores information about a generic block of code."""
2182
Alex Vakulenko01e47232016-05-06 13:54:15 -07002183 def __init__(self, linenum, seen_open_brace):
2184 self.starting_linenum = linenum
erg@google.comd350fe52013-01-14 17:51:48 +00002185 self.seen_open_brace = seen_open_brace
2186 self.open_parentheses = 0
2187 self.inline_asm = _NO_ASM
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002188 self.check_namespace_indentation = False
erg@google.comd350fe52013-01-14 17:51:48 +00002189
2190 def CheckBegin(self, filename, clean_lines, linenum, error):
2191 """Run checks that applies to text up to the opening brace.
2192
2193 This is mostly for checking the text after the class identifier
2194 and the "{", usually where the base class is specified. For other
2195 blocks, there isn't much to check, so we always pass.
2196
2197 Args:
2198 filename: The name of the current file.
2199 clean_lines: A CleansedLines instance containing the file.
2200 linenum: The number of the line to check.
2201 error: The function to call with any errors found.
2202 """
2203 pass
2204
2205 def CheckEnd(self, filename, clean_lines, linenum, error):
2206 """Run checks that applies to text after the closing brace.
2207
2208 This is mostly used for checking end of namespace comments.
2209
2210 Args:
2211 filename: The name of the current file.
2212 clean_lines: A CleansedLines instance containing the file.
2213 linenum: The number of the line to check.
2214 error: The function to call with any errors found.
2215 """
2216 pass
2217
avakulenko@google.com02af6282014-06-04 18:53:25 +00002218 def IsBlockInfo(self):
2219 """Returns true if this block is a _BlockInfo.
2220
2221 This is convenient for verifying that an object is an instance of
2222 a _BlockInfo, but not an instance of any of the derived classes.
2223
2224 Returns:
2225 True for this class, False for derived classes.
2226 """
2227 return self.__class__ == _BlockInfo
2228
2229
2230class _ExternCInfo(_BlockInfo):
2231 """Stores information about an 'extern "C"' block."""
2232
Alex Vakulenko01e47232016-05-06 13:54:15 -07002233 def __init__(self, linenum):
2234 _BlockInfo.__init__(self, linenum, True)
avakulenko@google.com02af6282014-06-04 18:53:25 +00002235
erg@google.comd350fe52013-01-14 17:51:48 +00002236
2237class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002238 """Stores information about a class."""
2239
erg@google.comd350fe52013-01-14 17:51:48 +00002240 def __init__(self, name, class_or_struct, clean_lines, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002241 _BlockInfo.__init__(self, linenum, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002242 self.name = name
erg@google.com4e00b9a2009-01-12 23:05:11 +00002243 self.is_derived = False
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002244 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002245 if class_or_struct == 'struct':
2246 self.access = 'public'
erg@google.comfd5da632013-10-25 17:39:45 +00002247 self.is_struct = True
erg@google.comd350fe52013-01-14 17:51:48 +00002248 else:
2249 self.access = 'private'
erg@google.comfd5da632013-10-25 17:39:45 +00002250 self.is_struct = False
2251
2252 # Remember initial indentation level for this class. Using raw_lines here
erg@google.comc6671232013-10-25 21:44:03 +00002253 # instead of elided to account for leading comments.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002254 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00002255
erg@google.com8a95ecc2011-09-08 00:45:54 +00002256 # Try to find the end of the class. This will be confused by things like:
2257 # class A {
2258 # } *x = { ...
2259 #
2260 # But it's still good enough for CheckSectionSpacing.
2261 self.last_line = 0
2262 depth = 0
2263 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00002264 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00002265 depth += line.count('{') - line.count('}')
2266 if not depth:
2267 self.last_line = i
2268 break
2269
erg@google.comd350fe52013-01-14 17:51:48 +00002270 def CheckBegin(self, filename, clean_lines, linenum, error):
2271 # Look for a bare ':'
2272 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2273 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00002274
erg@google.comfd5da632013-10-25 17:39:45 +00002275 def CheckEnd(self, filename, clean_lines, linenum, error):
avakulenko@google.com554223d2014-12-04 22:00:20 +00002276 # If there is a DISALLOW macro, it should appear near the end of
2277 # the class.
2278 seen_last_thing_in_class = False
2279 for i in xrange(linenum - 1, self.starting_linenum, -1):
2280 match = Search(
2281 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
2282 self.name + r'\)',
2283 clean_lines.elided[i])
2284 if match:
2285 if seen_last_thing_in_class:
2286 error(filename, i, 'readability/constructors', 3,
2287 match.group(1) + ' should be the last thing in the class')
2288 break
2289
2290 if not Match(r'^\s*$', clean_lines.elided[i]):
2291 seen_last_thing_in_class = True
2292
erg@google.comfd5da632013-10-25 17:39:45 +00002293 # Check that closing brace is aligned with beginning of the class.
2294 # Only do this if the closing brace is indented by only whitespaces.
2295 # This means we will not check single-line class definitions.
2296 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2297 if indent and len(indent.group(1)) != self.class_indent:
2298 if self.is_struct:
2299 parent = 'struct ' + self.name
2300 else:
2301 parent = 'class ' + self.name
2302 error(filename, linenum, 'whitespace/indent', 3,
2303 'Closing brace should be aligned with beginning of %s' % parent)
2304
erg@google.com4e00b9a2009-01-12 23:05:11 +00002305
erg@google.comd350fe52013-01-14 17:51:48 +00002306class _NamespaceInfo(_BlockInfo):
2307 """Stores information about a namespace."""
2308
2309 def __init__(self, name, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002310 _BlockInfo.__init__(self, linenum, False)
erg@google.comd350fe52013-01-14 17:51:48 +00002311 self.name = name or ''
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002312 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002313
2314 def CheckEnd(self, filename, clean_lines, linenum, error):
2315 """Check end of namespace comments."""
2316 line = clean_lines.raw_lines[linenum]
2317
2318 # Check how many lines is enclosed in this namespace. Don't issue
2319 # warning for missing namespace comments if there aren't enough
2320 # lines. However, do apply checks if there is already an end of
2321 # namespace comment and it's incorrect.
2322 #
2323 # TODO(unknown): We always want to check end of namespace comments
2324 # if a namespace is large, but sometimes we also want to apply the
2325 # check if a short namespace contained nontrivial things (something
2326 # other than forward declarations). There is currently no logic on
2327 # deciding what these nontrivial things are, so this check is
2328 # triggered by namespace size only, which works most of the time.
2329 if (linenum - self.starting_linenum < 10
Alex Vakulenko01e47232016-05-06 13:54:15 -07002330 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)):
erg@google.comd350fe52013-01-14 17:51:48 +00002331 return
2332
2333 # Look for matching comment at end of namespace.
2334 #
2335 # Note that we accept C style "/* */" comments for terminating
2336 # namespaces, so that code that terminate namespaces inside
erg@google.comc6671232013-10-25 21:44:03 +00002337 # preprocessor macros can be cpplint clean.
erg@google.comd350fe52013-01-14 17:51:48 +00002338 #
2339 # We also accept stuff like "// end of namespace <name>." with the
2340 # period at the end.
2341 #
2342 # Besides these, we don't accept anything else, otherwise we might
2343 # get false negatives when existing comment is a substring of the
erg@google.comc6671232013-10-25 21:44:03 +00002344 # expected namespace.
erg@google.comd350fe52013-01-14 17:51:48 +00002345 if self.name:
2346 # Named namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002347 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
2348 re.escape(self.name) + r'[\*/\.\\\s]*$'),
erg@google.comd350fe52013-01-14 17:51:48 +00002349 line):
2350 error(filename, linenum, 'readability/namespace', 5,
2351 'Namespace should be terminated with "// namespace %s"' %
2352 self.name)
2353 else:
2354 # Anonymous namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002355 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002356 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2357 # mention "// anonymous namespace" as an acceptable form
Alex Vakulenko01e47232016-05-06 13:54:15 -07002358 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002359 error(filename, linenum, 'readability/namespace', 5,
2360 'Anonymous namespace should be terminated with "// namespace"'
2361 ' or "// anonymous namespace"')
2362 else:
2363 error(filename, linenum, 'readability/namespace', 5,
2364 'Anonymous namespace should be terminated with "// namespace"')
erg@google.comd350fe52013-01-14 17:51:48 +00002365
2366
2367class _PreprocessorInfo(object):
2368 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2369
2370 def __init__(self, stack_before_if):
2371 # The entire nesting stack before #if
2372 self.stack_before_if = stack_before_if
2373
2374 # The entire nesting stack up to #else
2375 self.stack_before_else = []
2376
2377 # Whether we have already seen #else or #elif
2378 self.seen_else = False
2379
2380
avakulenko@google.com02af6282014-06-04 18:53:25 +00002381class NestingState(object):
erg@google.comd350fe52013-01-14 17:51:48 +00002382 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00002383
2384 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00002385 # Stack for tracking all braces. An object is pushed whenever we
2386 # see a "{", and popped when we see a "}". Only 3 types of
2387 # objects are possible:
2388 # - _ClassInfo: a class or struct.
2389 # - _NamespaceInfo: a namespace.
2390 # - _BlockInfo: some other type of block.
2391 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00002392
avakulenko@google.com02af6282014-06-04 18:53:25 +00002393 # Top of the previous stack before each Update().
2394 #
2395 # Because the nesting_stack is updated at the end of each line, we
2396 # had to do some convoluted checks to find out what is the current
2397 # scope at the beginning of the line. This check is simplified by
2398 # saving the previous top of nesting stack.
2399 #
2400 # We could save the full stack, but we only need the top. Copying
2401 # the full nesting stack would slow down cpplint by ~10%.
2402 self.previous_stack_top = []
2403
erg@google.comd350fe52013-01-14 17:51:48 +00002404 # Stack of _PreprocessorInfo objects.
2405 self.pp_stack = []
2406
2407 def SeenOpenBrace(self):
2408 """Check if we have seen the opening brace for the innermost block.
2409
2410 Returns:
2411 True if we have seen the opening brace, False if the innermost
2412 block is still expecting an opening brace.
2413 """
2414 return (not self.stack) or self.stack[-1].seen_open_brace
2415
2416 def InNamespaceBody(self):
2417 """Check if we are currently one level inside a namespace body.
2418
2419 Returns:
2420 True if top of the stack is a namespace block, False otherwise.
2421 """
2422 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2423
avakulenko@google.com02af6282014-06-04 18:53:25 +00002424 def InExternC(self):
2425 """Check if we are currently one level inside an 'extern "C"' block.
2426
2427 Returns:
2428 True if top of the stack is an extern block, False otherwise.
2429 """
2430 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2431
2432 def InClassDeclaration(self):
2433 """Check if we are currently one level inside a class or struct declaration.
2434
2435 Returns:
2436 True if top of the stack is a class/struct, False otherwise.
2437 """
2438 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2439
2440 def InAsmBlock(self):
2441 """Check if we are currently one level inside an inline ASM block.
2442
2443 Returns:
2444 True if the top of the stack is a block containing inline ASM.
2445 """
2446 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2447
2448 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2449 """Check if current position is inside template argument list.
2450
2451 Args:
2452 clean_lines: A CleansedLines instance containing the file.
2453 linenum: The number of the line to check.
2454 pos: position just after the suspected template argument.
2455 Returns:
2456 True if (linenum, pos) is inside template arguments.
2457 """
2458 while linenum < clean_lines.NumLines():
2459 # Find the earliest character that might indicate a template argument
2460 line = clean_lines.elided[linenum]
2461 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2462 if not match:
2463 linenum += 1
2464 pos = 0
2465 continue
2466 token = match.group(1)
2467 pos += len(match.group(0))
2468
2469 # These things do not look like template argument list:
2470 # class Suspect {
2471 # class Suspect x; }
2472 if token in ('{', '}', ';'): return False
2473
2474 # These things look like template argument list:
2475 # template <class Suspect>
2476 # template <class Suspect = default_value>
2477 # template <class Suspect[]>
2478 # template <class Suspect...>
2479 if token in ('>', '=', '[', ']', '.'): return True
2480
2481 # Check if token is an unmatched '<'.
2482 # If not, move on to the next character.
2483 if token != '<':
2484 pos += 1
2485 if pos >= len(line):
2486 linenum += 1
2487 pos = 0
2488 continue
2489
2490 # We can't be sure if we just find a single '<', and need to
2491 # find the matching '>'.
2492 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2493 if end_pos < 0:
2494 # Not sure if template argument list or syntax error in file
2495 return False
2496 linenum = end_line
2497 pos = end_pos
2498 return False
2499
erg@google.comd350fe52013-01-14 17:51:48 +00002500 def UpdatePreprocessor(self, line):
2501 """Update preprocessor stack.
2502
2503 We need to handle preprocessors due to classes like this:
2504 #ifdef SWIG
2505 struct ResultDetailsPageElementExtensionPoint {
2506 #else
2507 struct ResultDetailsPageElementExtensionPoint : public Extension {
2508 #endif
erg@google.comd350fe52013-01-14 17:51:48 +00002509
2510 We make the following assumptions (good enough for most files):
2511 - Preprocessor condition evaluates to true from #if up to first
2512 #else/#elif/#endif.
2513
2514 - Preprocessor condition evaluates to false from #else/#elif up
2515 to #endif. We still perform lint checks on these lines, but
2516 these do not affect nesting stack.
2517
2518 Args:
2519 line: current line to check.
2520 """
2521 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2522 # Beginning of #if block, save the nesting stack here. The saved
2523 # stack will allow us to restore the parsing state in the #else case.
2524 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2525 elif Match(r'^\s*#\s*(else|elif)\b', line):
2526 # Beginning of #else block
2527 if self.pp_stack:
2528 if not self.pp_stack[-1].seen_else:
2529 # This is the first #else or #elif block. Remember the
2530 # whole nesting stack up to this point. This is what we
2531 # keep after the #endif.
2532 self.pp_stack[-1].seen_else = True
2533 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2534
2535 # Restore the stack to how it was before the #if
2536 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2537 else:
2538 # TODO(unknown): unexpected #else, issue warning?
2539 pass
2540 elif Match(r'^\s*#\s*endif\b', line):
2541 # End of #if or #else blocks.
2542 if self.pp_stack:
2543 # If we saw an #else, we will need to restore the nesting
2544 # stack to its former state before the #else, otherwise we
2545 # will just continue from where we left off.
2546 if self.pp_stack[-1].seen_else:
2547 # Here we can just use a shallow copy since we are the last
2548 # reference to it.
2549 self.stack = self.pp_stack[-1].stack_before_else
2550 # Drop the corresponding #if
2551 self.pp_stack.pop()
2552 else:
2553 # TODO(unknown): unexpected #endif, issue warning?
2554 pass
2555
avakulenko@google.com02af6282014-06-04 18:53:25 +00002556 # TODO(unknown): Update() is too long, but we will refactor later.
erg@google.comd350fe52013-01-14 17:51:48 +00002557 def Update(self, filename, clean_lines, linenum, error):
2558 """Update nesting state with current line.
2559
2560 Args:
2561 filename: The name of the current file.
2562 clean_lines: A CleansedLines instance containing the file.
2563 linenum: The number of the line to check.
2564 error: The function to call with any errors found.
2565 """
2566 line = clean_lines.elided[linenum]
2567
avakulenko@google.com02af6282014-06-04 18:53:25 +00002568 # Remember top of the previous nesting stack.
2569 #
2570 # The stack is always pushed/popped and not modified in place, so
2571 # we can just do a shallow copy instead of copy.deepcopy. Using
2572 # deepcopy would slow down cpplint by ~28%.
2573 if self.stack:
2574 self.previous_stack_top = self.stack[-1]
2575 else:
2576 self.previous_stack_top = None
2577
2578 # Update pp_stack
erg@google.comd350fe52013-01-14 17:51:48 +00002579 self.UpdatePreprocessor(line)
2580
2581 # Count parentheses. This is to avoid adding struct arguments to
2582 # the nesting stack.
2583 if self.stack:
2584 inner_block = self.stack[-1]
2585 depth_change = line.count('(') - line.count(')')
2586 inner_block.open_parentheses += depth_change
2587
2588 # Also check if we are starting or ending an inline assembly block.
2589 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2590 if (depth_change != 0 and
2591 inner_block.open_parentheses == 1 and
2592 _MATCH_ASM.match(line)):
2593 # Enter assembly block
2594 inner_block.inline_asm = _INSIDE_ASM
2595 else:
2596 # Not entering assembly block. If previous line was _END_ASM,
2597 # we will now shift to _NO_ASM state.
2598 inner_block.inline_asm = _NO_ASM
2599 elif (inner_block.inline_asm == _INSIDE_ASM and
2600 inner_block.open_parentheses == 0):
2601 # Exit assembly block
2602 inner_block.inline_asm = _END_ASM
2603
2604 # Consume namespace declaration at the beginning of the line. Do
2605 # this in a loop so that we catch same line declarations like this:
2606 # namespace proto2 { namespace bridge { class MessageSet; } }
2607 while True:
2608 # Match start of namespace. The "\b\s*" below catches namespace
2609 # declarations even if it weren't followed by a whitespace, this
2610 # is so that we don't confuse our namespace checker. The
2611 # missing spaces will be flagged by CheckSpacing.
2612 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2613 if not namespace_decl_match:
2614 break
2615
2616 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2617 self.stack.append(new_namespace)
2618
2619 line = namespace_decl_match.group(2)
2620 if line.find('{') != -1:
2621 new_namespace.seen_open_brace = True
2622 line = line[line.find('{') + 1:]
2623
2624 # Look for a class declaration in whatever is left of the line
2625 # after parsing namespaces. The regexp accounts for decorated classes
2626 # such as in:
2627 # class LOCKABLE API Object {
2628 # };
erg@google.comd350fe52013-01-14 17:51:48 +00002629 class_decl_match = Match(
avakulenko@google.com02af6282014-06-04 18:53:25 +00002630 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2631 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2632 r'(.*)$', line)
erg@google.comd350fe52013-01-14 17:51:48 +00002633 if (class_decl_match and
2634 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002635 # We do not want to accept classes that are actually template arguments:
2636 # template <class Ignore1,
2637 # class Ignore2 = Default<Args>,
2638 # template <Args> class Ignore3>
2639 # void Function() {};
2640 #
2641 # To avoid template argument cases, we scan forward and look for
2642 # an unmatched '>'. If we see one, assume we are inside a
2643 # template argument list.
2644 end_declaration = len(class_decl_match.group(1))
2645 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2646 self.stack.append(_ClassInfo(
2647 class_decl_match.group(3), class_decl_match.group(2),
2648 clean_lines, linenum))
2649 line = class_decl_match.group(4)
erg@google.comd350fe52013-01-14 17:51:48 +00002650
2651 # If we have not yet seen the opening brace for the innermost block,
2652 # run checks here.
2653 if not self.SeenOpenBrace():
2654 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2655
2656 # Update access control if we are inside a class/struct
2657 if self.stack and isinstance(self.stack[-1], _ClassInfo):
erg@google.comfd5da632013-10-25 17:39:45 +00002658 classinfo = self.stack[-1]
2659 access_match = Match(
2660 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2661 r':(?:[^:]|$)',
2662 line)
erg@google.comd350fe52013-01-14 17:51:48 +00002663 if access_match:
erg@google.comfd5da632013-10-25 17:39:45 +00002664 classinfo.access = access_match.group(2)
2665
2666 # Check that access keywords are indented +1 space. Skip this
erg@google.comc6671232013-10-25 21:44:03 +00002667 # check if the keywords are not preceded by whitespaces.
erg@google.comfd5da632013-10-25 17:39:45 +00002668 indent = access_match.group(1)
2669 if (len(indent) != classinfo.class_indent + 1 and
2670 Match(r'^\s*$', indent)):
2671 if classinfo.is_struct:
2672 parent = 'struct ' + classinfo.name
2673 else:
2674 parent = 'class ' + classinfo.name
2675 slots = ''
2676 if access_match.group(3):
2677 slots = access_match.group(3)
2678 error(filename, linenum, 'whitespace/indent', 3,
2679 '%s%s: should be indented +1 space inside %s' % (
2680 access_match.group(2), slots, parent))
erg@google.comd350fe52013-01-14 17:51:48 +00002681
2682 # Consume braces or semicolons from what's left of the line
2683 while True:
2684 # Match first brace, semicolon, or closed parenthesis.
2685 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2686 if not matched:
2687 break
2688
2689 token = matched.group(1)
2690 if token == '{':
2691 # If namespace or class hasn't seen a opening brace yet, mark
2692 # namespace/class head as complete. Push a new block onto the
2693 # stack otherwise.
2694 if not self.SeenOpenBrace():
2695 self.stack[-1].seen_open_brace = True
avakulenko@google.com02af6282014-06-04 18:53:25 +00002696 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002697 self.stack.append(_ExternCInfo(linenum))
erg@google.comd350fe52013-01-14 17:51:48 +00002698 else:
Alex Vakulenko01e47232016-05-06 13:54:15 -07002699 self.stack.append(_BlockInfo(linenum, True))
erg@google.comd350fe52013-01-14 17:51:48 +00002700 if _MATCH_ASM.match(line):
2701 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.com02af6282014-06-04 18:53:25 +00002702
erg@google.comd350fe52013-01-14 17:51:48 +00002703 elif token == ';' or token == ')':
2704 # If we haven't seen an opening brace yet, but we already saw
2705 # a semicolon, this is probably a forward declaration. Pop
2706 # the stack for these.
2707 #
2708 # Similarly, if we haven't seen an opening brace yet, but we
2709 # already saw a closing parenthesis, then these are probably
2710 # function arguments with extra "class" or "struct" keywords.
2711 # Also pop these stack for these.
2712 if not self.SeenOpenBrace():
2713 self.stack.pop()
2714 else: # token == '}'
2715 # Perform end of block checks and pop the stack.
2716 if self.stack:
2717 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2718 self.stack.pop()
2719 line = matched.group(2)
2720
2721 def InnermostClass(self):
2722 """Get class info on the top of the stack.
2723
2724 Returns:
2725 A _ClassInfo object if we are inside a class, or None otherwise.
2726 """
2727 for i in range(len(self.stack), 0, -1):
2728 classinfo = self.stack[i - 1]
2729 if isinstance(classinfo, _ClassInfo):
2730 return classinfo
2731 return None
2732
erg@google.com2aa59982013-10-28 19:09:25 +00002733 def CheckCompletedBlocks(self, filename, error):
2734 """Checks that all classes and namespaces have been completely parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002735
2736 Call this when all lines in a file have been processed.
2737 Args:
2738 filename: The name of the current file.
2739 error: The function to call with any errors found.
2740 """
erg@google.comd350fe52013-01-14 17:51:48 +00002741 # Note: This test can result in false positives if #ifdef constructs
2742 # get in the way of brace matching. See the testBuildClass test in
2743 # cpplint_unittest.py for an example of this.
2744 for obj in self.stack:
2745 if isinstance(obj, _ClassInfo):
2746 error(filename, obj.starting_linenum, 'build/class', 5,
2747 'Failed to find complete declaration of class %s' %
2748 obj.name)
erg@google.com2aa59982013-10-28 19:09:25 +00002749 elif isinstance(obj, _NamespaceInfo):
2750 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2751 'Failed to find complete declaration of namespace %s' %
2752 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002753
2754
2755def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00002756 nesting_state, error):
erg@google.com2aa59982013-10-28 19:09:25 +00002757 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002758
2759 Complain about several constructs which gcc-2 accepts, but which are
2760 not standard C++. Warning about these in lint is one way to ease the
2761 transition to new compilers.
2762 - put storage class first (e.g. "static const" instead of "const static").
2763 - "%lld" instead of %qd" in printf-type functions.
2764 - "%1$d" is non-standard in printf-type functions.
2765 - "\%" is an undefined character escape sequence.
2766 - text after #endif is not allowed.
2767 - invalid inner-style forward declaration.
2768 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002769
erg@google.coma868d2d2009-10-09 21:18:45 +00002770 Additionally, check for constructor/destructor style violations and reference
2771 members, as it is very convenient to do so while checking for
2772 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002773
2774 Args:
2775 filename: The name of the current file.
2776 clean_lines: A CleansedLines instance containing the file.
2777 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002778 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00002779 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002780 error: A callable to which errors are reported, which takes 4 arguments:
2781 filename, line number, error level, and message
2782 """
2783
2784 # Remove comments from the line, but leave in strings for now.
2785 line = clean_lines.lines[linenum]
2786
2787 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2788 error(filename, linenum, 'runtime/printf_format', 3,
2789 '%q in format strings is deprecated. Use %ll instead.')
2790
2791 if Search(r'printf\s*\(.*".*%\d+\$', line):
2792 error(filename, linenum, 'runtime/printf_format', 2,
2793 '%N$ formats are unconventional. Try rewriting to avoid them.')
2794
2795 # Remove escaped backslashes before looking for undefined escapes.
2796 line = line.replace('\\\\', '')
2797
2798 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2799 error(filename, linenum, 'build/printf_format', 3,
2800 '%, [, (, and { are undefined character escapes. Unescape them.')
2801
2802 # For the rest, work with both comments and strings removed.
2803 line = clean_lines.elided[linenum]
2804
2805 if Search(r'\b(const|volatile|void|char|short|int|long'
2806 r'|float|double|signed|unsigned'
2807 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00002808 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00002809 line):
2810 error(filename, linenum, 'build/storage_class', 5,
Alex Vakulenko01e47232016-05-06 13:54:15 -07002811 'Storage-class specifier (static, extern, typedef, etc) should be '
2812 'at the beginning of the declaration.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002813
Elliot Glaysherae118112016-09-30 15:34:26 -07002814 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2815 error(filename, linenum, 'build/endif_comment', 5,
2816 'Uncommented text after #endif is non-standard. Use a comment.')
2817
erg@google.com4e00b9a2009-01-12 23:05:11 +00002818 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2819 error(filename, linenum, 'build/forward_decl', 5,
2820 'Inner-style forward declarations are invalid. Remove this line.')
2821
2822 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2823 line):
2824 error(filename, linenum, 'build/deprecated', 3,
2825 '>? and <? (max and min) operators are non-standard and deprecated.')
2826
erg@google.coma868d2d2009-10-09 21:18:45 +00002827 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2828 # TODO(unknown): Could it be expanded safely to arbitrary references,
2829 # without triggering too many false positives? The first
2830 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2831 # the restriction.
2832 # Here's the original regexp, for the reference:
2833 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2834 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2835 error(filename, linenum, 'runtime/member_string_references', 2,
2836 'const string& members are dangerous. It is much better to use '
2837 'alternatives, such as pointers or simple constants.')
2838
erg@google.comd350fe52013-01-14 17:51:48 +00002839 # Everything else in this function operates on class declarations.
2840 # Return early if the top of the nesting stack is not a class, or if
2841 # the class head is not completed yet.
2842 classinfo = nesting_state.InnermostClass()
2843 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002844 return
2845
erg@google.com4e00b9a2009-01-12 23:05:11 +00002846 # The class may have been declared with namespace or classname qualifiers.
2847 # The constructor and destructor will not have those qualifiers.
2848 base_classname = classinfo.name.split('::')[-1]
2849
2850 # Look for single-argument constructors that aren't marked explicit.
Alex Vakulenko01e47232016-05-06 13:54:15 -07002851 # Technically a valid construct, but against style.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002852 explicit_constructor_match = Match(
Dana Jansenscf4071c2017-02-22 12:02:39 -05002853 r'\s+(?:(?:inline|constexpr)\s+)*(explicit\s+)?'
2854 r'(?:(?:inline|constexpr)\s+)*%s\s*'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002855 r'\(((?:[^()]|\([^()]*\))*)\)'
2856 % re.escape(base_classname),
2857 line)
2858
2859 if explicit_constructor_match:
2860 is_marked_explicit = explicit_constructor_match.group(1)
2861
2862 if not explicit_constructor_match.group(2):
2863 constructor_args = []
2864 else:
2865 constructor_args = explicit_constructor_match.group(2).split(',')
2866
2867 # collapse arguments so that commas in template parameter lists and function
2868 # argument parameter lists don't split arguments in two
2869 i = 0
2870 while i < len(constructor_args):
2871 constructor_arg = constructor_args[i]
2872 while (constructor_arg.count('<') > constructor_arg.count('>') or
2873 constructor_arg.count('(') > constructor_arg.count(')')):
2874 constructor_arg += ',' + constructor_args[i + 1]
2875 del constructor_args[i + 1]
2876 constructor_args[i] = constructor_arg
2877 i += 1
2878
2879 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2880 noarg_constructor = (not constructor_args or # empty arg list
2881 # 'void' arg specifier
2882 (len(constructor_args) == 1 and
2883 constructor_args[0].strip() == 'void'))
2884 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2885 not noarg_constructor) or
2886 # all but at most one arg defaulted
2887 (len(constructor_args) >= 1 and
2888 not noarg_constructor and
2889 len(defaulted_args) >= len(constructor_args) - 1))
2890 initializer_list_constructor = bool(
2891 onearg_constructor and
2892 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2893 copy_constructor = bool(
2894 onearg_constructor and
2895 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2896 % re.escape(base_classname), constructor_args[0].strip()))
2897
2898 if (not is_marked_explicit and
2899 onearg_constructor and
2900 not initializer_list_constructor and
2901 not copy_constructor):
2902 if defaulted_args:
2903 error(filename, linenum, 'runtime/explicit', 5,
2904 'Constructors callable with one argument '
2905 'should be marked explicit.')
2906 else:
2907 error(filename, linenum, 'runtime/explicit', 5,
2908 'Single-parameter constructors should be marked explicit.')
2909 elif is_marked_explicit and not onearg_constructor:
2910 if noarg_constructor:
2911 error(filename, linenum, 'runtime/explicit', 5,
2912 'Zero-parameter constructors should not be marked explicit.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002913
erg@google.com4e00b9a2009-01-12 23:05:11 +00002914
avakulenko@google.com02af6282014-06-04 18:53:25 +00002915def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002916 """Checks for the correctness of various spacing around function calls.
2917
2918 Args:
2919 filename: The name of the current file.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002920 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002921 linenum: The number of the line to check.
2922 error: The function to call with any errors found.
2923 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00002924 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002925
2926 # Since function calls often occur inside if/for/while/switch
2927 # expressions - which have their own, more liberal conventions - we
2928 # first see if we should be looking inside such an expression for a
2929 # function call, to which we can apply more strict standards.
2930 fncall = line # if there's no control flow construct, look at whole line
2931 for pattern in (r'\bif\s*\((.*)\)\s*{',
2932 r'\bfor\s*\((.*)\)\s*{',
2933 r'\bwhile\s*\((.*)\)\s*[{;]',
2934 r'\bswitch\s*\((.*)\)\s*{'):
2935 match = Search(pattern, line)
2936 if match:
2937 fncall = match.group(1) # look inside the parens for function calls
2938 break
2939
2940 # Except in if/for/while/switch, there should never be space
2941 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2942 # for nested parens ( (a+b) + c ). Likewise, there should never be
2943 # a space before a ( when it's a function argument. I assume it's a
2944 # function argument when the char before the whitespace is legal in
2945 # a function name (alnum + _) and we're not starting a macro. Also ignore
2946 # pointers and references to arrays and functions coz they're too tricky:
2947 # we use a very simple way to recognize these:
2948 # " (something)(maybe-something)" or
2949 # " (something)(maybe-something," or
2950 # " (something)[something]"
2951 # Note that we assume the contents of [] to be short enough that
2952 # they'll never need to wrap.
2953 if ( # Ignore control structures.
erg@google.com2aa59982013-10-28 19:09:25 +00002954 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
erg@google.comc6671232013-10-25 21:44:03 +00002955 fncall) and
erg@google.com4e00b9a2009-01-12 23:05:11 +00002956 # Ignore pointers/references to functions.
2957 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2958 # Ignore pointers/references to arrays.
2959 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00002960 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00002961 error(filename, linenum, 'whitespace/parens', 4,
2962 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00002963 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002964 error(filename, linenum, 'whitespace/parens', 2,
2965 'Extra space after (')
2966 if (Search(r'\w\s+\(', fncall) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07002967 not Search(r'_{0,2}asm_{0,2}\s+_{0,2}volatile_{0,2}\s+\(', fncall) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00002968 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
avakulenko@google.com554223d2014-12-04 22:00:20 +00002969 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and
2970 not Search(r'\bcase\s+\(', fncall)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002971 # TODO(unknown): Space after an operator function seem to be a common
2972 # error, silence those for now by restricting them to highest verbosity.
2973 if Search(r'\boperator_*\b', line):
2974 error(filename, linenum, 'whitespace/parens', 0,
2975 'Extra space before ( in function call')
2976 else:
2977 error(filename, linenum, 'whitespace/parens', 4,
2978 'Extra space before ( in function call')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002979 # If the ) is followed only by a newline or a { + newline, assume it's
2980 # part of a control statement (if/while/etc), and don't complain
2981 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002982 # If the closing parenthesis is preceded by only whitespaces,
2983 # try to give a more descriptive error message.
2984 if Search(r'^\s+\)', fncall):
2985 error(filename, linenum, 'whitespace/parens', 2,
2986 'Closing ) should be moved to the previous line')
2987 else:
2988 error(filename, linenum, 'whitespace/parens', 2,
2989 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002990
2991
2992def IsBlankLine(line):
2993 """Returns true if the given line is blank.
2994
2995 We consider a line to be blank if the line is empty or consists of
2996 only white spaces.
2997
2998 Args:
2999 line: A line of a string.
3000
3001 Returns:
3002 True, if the given line is blank.
3003 """
3004 return not line or line.isspace()
3005
3006
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003007def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
3008 error):
3009 is_namespace_indent_item = (
3010 len(nesting_state.stack) > 1 and
3011 nesting_state.stack[-1].check_namespace_indentation and
3012 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
3013 nesting_state.previous_stack_top == nesting_state.stack[-2])
3014
3015 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
3016 clean_lines.elided, line):
3017 CheckItemIndentationInNamespace(filename, clean_lines.elided,
3018 line, error)
3019
3020
erg@google.com4e00b9a2009-01-12 23:05:11 +00003021def CheckForFunctionLengths(filename, clean_lines, linenum,
3022 function_state, error):
3023 """Reports for long function bodies.
3024
3025 For an overview why this is done, see:
Ackermann Yuriy79692902016-04-01 21:41:34 +13003026 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
erg@google.com4e00b9a2009-01-12 23:05:11 +00003027
3028 Uses a simplistic algorithm assuming other style guidelines
3029 (especially spacing) are followed.
3030 Only checks unindented functions, so class members are unchecked.
3031 Trivial bodies are unchecked, so constructors with huge initializer lists
3032 may be missed.
3033 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00003034 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003035 NOLINT *on the last line of a function* disables this check.
3036
3037 Args:
3038 filename: The name of the current file.
3039 clean_lines: A CleansedLines instance containing the file.
3040 linenum: The number of the line to check.
3041 function_state: Current function name and lines in body so far.
3042 error: The function to call with any errors found.
3043 """
3044 lines = clean_lines.lines
3045 line = lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003046 joined_line = ''
3047
3048 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00003049 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00003050 match_result = Match(regexp, line)
3051 if match_result:
3052 # If the name is all caps and underscores, figure it's a macro and
3053 # ignore it, unless it's TEST or TEST_F.
3054 function_name = match_result.group(1).split()[-1]
3055 if function_name == 'TEST' or function_name == 'TEST_F' or (
3056 not Match(r'[A-Z_]+$', function_name)):
3057 starting_func = True
3058
3059 if starting_func:
3060 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00003061 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003062 start_line = lines[start_linenum]
3063 joined_line += ' ' + start_line.lstrip()
3064 if Search(r'(;|})', start_line): # Declarations and trivial functions
3065 body_found = True
3066 break # ... ignore
3067 elif Search(r'{', start_line):
3068 body_found = True
3069 function = Search(r'((\w|:)*)\(', line).group(1)
3070 if Match(r'TEST', function): # Handle TEST... macros
3071 parameter_regexp = Search(r'(\(.*\))', joined_line)
3072 if parameter_regexp: # Ignore bad syntax
3073 function += parameter_regexp.group(1)
3074 else:
3075 function += '()'
3076 function_state.Begin(function)
3077 break
3078 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00003079 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003080 error(filename, linenum, 'readability/fn_size', 5,
3081 'Lint failed to find start of function body.')
3082 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00003083 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003084 function_state.End()
3085 elif not Match(r'^\s*$', line):
3086 function_state.Count() # Count non-blank/non-comment lines.
3087
3088
3089_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
3090
3091
avakulenko@google.com02af6282014-06-04 18:53:25 +00003092def CheckComment(line, filename, linenum, next_line_start, error):
3093 """Checks for common mistakes in comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003094
3095 Args:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003096 line: The line in question.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003097 filename: The name of the current file.
3098 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003099 next_line_start: The first non-whitespace column of the next line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003100 error: The function to call with any errors found.
3101 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00003102 commentpos = line.find('//')
3103 if commentpos != -1:
3104 # Check if the // may be in quotes. If so, ignore it
Alex Vakulenko01e47232016-05-06 13:54:15 -07003105 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003106 # Allow one space for new scopes, two spaces otherwise:
3107 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
3108 ((commentpos >= 1 and
3109 line[commentpos-1] not in string.whitespace) or
3110 (commentpos >= 2 and
3111 line[commentpos-2] not in string.whitespace))):
3112 error(filename, linenum, 'whitespace/comments', 2,
3113 'At least two spaces is best between code and comments')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003114
avakulenko@google.com02af6282014-06-04 18:53:25 +00003115 # Checks for common mistakes in TODO comments.
3116 comment = line[commentpos:]
3117 match = _RE_PATTERN_TODO.match(comment)
3118 if match:
3119 # One whitespace is correct; zero whitespace is handled elsewhere.
3120 leading_whitespace = match.group(1)
3121 if len(leading_whitespace) > 1:
3122 error(filename, linenum, 'whitespace/todo', 2,
3123 'Too many spaces before TODO')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003124
avakulenko@google.com02af6282014-06-04 18:53:25 +00003125 username = match.group(2)
3126 if not username:
3127 error(filename, linenum, 'readability/todo', 2,
3128 'Missing username in TODO; it should look like '
3129 '"// TODO(my_username): Stuff."')
3130
3131 middle_whitespace = match.group(3)
3132 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
3133 if middle_whitespace != ' ' and middle_whitespace != '':
3134 error(filename, linenum, 'whitespace/todo', 2,
3135 'TODO(my_username) should be followed by a space')
3136
3137 # If the comment contains an alphanumeric character, there
avakulenko@google.com554223d2014-12-04 22:00:20 +00003138 # should be a space somewhere between it and the // unless
3139 # it's a /// or //! Doxygen comment.
3140 if (Match(r'//[^ ]*\w', comment) and
3141 not Match(r'(///|//\!)(\s+|$)', comment)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003142 error(filename, linenum, 'whitespace/comments', 4,
3143 'Should have a space between // and comment')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003144
avakulenko@google.com554223d2014-12-04 22:00:20 +00003145
erg@google.comd350fe52013-01-14 17:51:48 +00003146def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003147 """Checks for the correctness of various spacing issues in the code.
3148
3149 Things we check for: spaces around operators, spaces after
3150 if/for/while/switch, no spaces around parens in function calls, two
3151 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00003152 line, don't end a function with a blank line, don't add a blank line
3153 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003154
3155 Args:
3156 filename: The name of the current file.
3157 clean_lines: A CleansedLines instance containing the file.
3158 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003159 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003160 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003161 error: The function to call with any errors found.
3162 """
3163
erg@google.com2aa59982013-10-28 19:09:25 +00003164 # Don't use "elided" lines here, otherwise we can't check commented lines.
3165 # Don't want to use "raw" either, because we don't want to check inside C++11
3166 # raw strings,
3167 raw = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00003168 line = raw[linenum]
3169
3170 # Before nixing comments, check if the line is blank for no good
3171 # reason. This includes the first line after a block is opened, and
3172 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00003173 #
3174 # Skip all the blank line checks if we are immediately inside a
3175 # namespace body. In other words, don't issue blank line warnings
3176 # for this block:
3177 # namespace {
3178 #
3179 # }
3180 #
3181 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003182 #
3183 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
3184 # like namespaces.
3185 if (IsBlankLine(line) and
3186 not nesting_state.InNamespaceBody() and
3187 not nesting_state.InExternC()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003188 elided = clean_lines.elided
3189 prev_line = elided[linenum - 1]
3190 prevbrace = prev_line.rfind('{')
3191 # TODO(unknown): Don't complain if line before blank line, and line after,
3192 # both start with alnums and are indented the same amount.
3193 # This ignores whitespace at the start of a namespace block
3194 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00003195 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003196 # OK, we have a blank line at the start of a code block. Before we
3197 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00003198 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00003199 # 4 spaces (because they did not fit in a 80 column line when placed on
3200 # the same line as the function name). We also check for the case where
3201 # the previous line is indented 6 spaces, which may happen when the
3202 # initializers of a constructor do not fit into a 80 column line.
3203 exception = False
3204 if Match(r' {6}\w', prev_line): # Initializer list?
3205 # We are looking for the opening column of initializer list, which
3206 # should be indented 4 spaces to cause 6 space indentation afterwards.
3207 search_position = linenum-2
3208 while (search_position >= 0
3209 and Match(r' {6}\w', elided[search_position])):
3210 search_position -= 1
3211 exception = (search_position >= 0
3212 and elided[search_position][:5] == ' :')
3213 else:
3214 # Search for the function arguments or an initializer list. We use a
3215 # simple heuristic here: If the line is indented 4 spaces; and we have a
3216 # closing paren, without the opening paren, followed by an opening brace
3217 # or colon (for initializer lists) we assume that it is the last line of
3218 # a function header. If we have a colon indented 4 spaces, it is an
3219 # initializer list.
3220 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3221 prev_line)
3222 or Match(r' {4}:', prev_line))
3223
3224 if not exception:
3225 error(filename, linenum, 'whitespace/blank_line', 2,
erg@google.com2aa59982013-10-28 19:09:25 +00003226 'Redundant blank line at the start of a code block '
3227 'should be deleted.')
erg@google.comd350fe52013-01-14 17:51:48 +00003228 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00003229 # chain, like this:
3230 # if (condition1) {
3231 # // Something followed by a blank line
3232 #
3233 # } else if (condition2) {
3234 # // Something else
3235 # }
3236 if linenum + 1 < clean_lines.NumLines():
3237 next_line = raw[linenum + 1]
3238 if (next_line
3239 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003240 and next_line.find('} else ') == -1):
3241 error(filename, linenum, 'whitespace/blank_line', 3,
erg@google.com2aa59982013-10-28 19:09:25 +00003242 'Redundant blank line at the end of a code block '
3243 'should be deleted.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003244
erg@google.com8a95ecc2011-09-08 00:45:54 +00003245 matched = Match(r'\s*(public|protected|private):', prev_line)
3246 if matched:
3247 error(filename, linenum, 'whitespace/blank_line', 3,
3248 'Do not leave a blank line after "%s:"' % matched.group(1))
3249
avakulenko@google.com02af6282014-06-04 18:53:25 +00003250 # Next, check comments
3251 next_line_start = 0
3252 if linenum + 1 < clean_lines.NumLines():
3253 next_line = raw[linenum + 1]
3254 next_line_start = len(next_line) - len(next_line.lstrip())
3255 CheckComment(line, filename, linenum, next_line_start, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003256
avakulenko@google.com02af6282014-06-04 18:53:25 +00003257 # get rid of comments and strings
3258 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003259
avakulenko@google.com02af6282014-06-04 18:53:25 +00003260 # You shouldn't have spaces before your brackets, except maybe after
3261 # 'delete []' or 'return []() {};'
3262 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3263 error(filename, linenum, 'whitespace/braces', 5,
3264 'Extra space before [')
3265
3266 # In range-based for, we wanted spaces before and after the colon, but
3267 # not around "::" tokens that might appear.
3268 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3269 Search(r'for *\(.*[^: ]:[^:]', line)):
3270 error(filename, linenum, 'whitespace/forcolon', 2,
3271 'Missing space around colon in range-based for loop')
3272
3273
3274def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3275 """Checks for horizontal spacing around operators.
3276
3277 Args:
3278 filename: The name of the current file.
3279 clean_lines: A CleansedLines instance containing the file.
3280 linenum: The number of the line to check.
3281 error: The function to call with any errors found.
3282 """
3283 line = clean_lines.elided[linenum]
3284
3285 # Don't try to do spacing checks for operator methods. Do this by
3286 # replacing the troublesome characters with something else,
3287 # preserving column position for all other characters.
3288 #
3289 # The replacement is done repeatedly to avoid false positives from
3290 # operators that call operators.
3291 while True:
3292 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3293 if match:
3294 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3295 else:
3296 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00003297
3298 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3299 # Otherwise not. Note we only check for non-spaces on *both* sides;
3300 # sometimes people put non-spaces on one side when aligning ='s among
3301 # many lines (not that this is behavior that I approve of...)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003302 if ((Search(r'[\w.]=', line) or
3303 Search(r'=[\w.]', line))
3304 and not Search(r'\b(if|while|for) ', line)
3305 # Operators taken from [lex.operators] in C++11 standard.
3306 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3307 and not Search(r'operator=', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003308 error(filename, linenum, 'whitespace/operators', 4,
3309 'Missing spaces around =')
3310
3311 # It's ok not to have spaces around binary operators like + - * /, but if
3312 # there's too little whitespace, we get concerned. It's hard to tell,
3313 # though, so we punt on this one for now. TODO.
3314
3315 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00003316 #
3317 # Check <= and >= first to avoid false positives with < and >, then
3318 # check non-include lines for spacing around < and >.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003319 #
3320 # If the operator is followed by a comma, assume it's be used in a
3321 # macro context and don't do any checks. This avoids false
3322 # positives.
3323 #
Alex Vakulenko01e47232016-05-06 13:54:15 -07003324 # Note that && is not included here. This is because there are too
3325 # many false positives due to RValue references.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003326 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003327 if match:
3328 error(filename, linenum, 'whitespace/operators', 3,
3329 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00003330 elif not Match(r'#.*include', line):
erg@google.comd350fe52013-01-14 17:51:48 +00003331 # Look for < that is not surrounded by spaces. This is only
3332 # triggered if both sides are missing spaces, even though
3333 # technically should should flag if at least one side is missing a
3334 # space. This is done to avoid some false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003335 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3336 if match:
3337 (_, _, end_pos) = CloseExpression(
3338 clean_lines, linenum, len(match.group(1)))
3339 if end_pos <= -1:
3340 error(filename, linenum, 'whitespace/operators', 3,
3341 'Missing spaces around <')
erg@google.comd350fe52013-01-14 17:51:48 +00003342
3343 # Look for > that is not surrounded by spaces. Similar to the
3344 # above, we only trigger if both sides are missing spaces to avoid
3345 # false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003346 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3347 if match:
3348 (_, _, start_pos) = ReverseCloseExpression(
3349 clean_lines, linenum, len(match.group(1)))
3350 if start_pos <= -1:
3351 error(filename, linenum, 'whitespace/operators', 3,
3352 'Missing spaces around >')
3353
3354 # We allow no-spaces around << when used like this: 10<<20, but
3355 # not otherwise (particularly, not when used as streams)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003356 #
avakulenko@google.com02af6282014-06-04 18:53:25 +00003357 # We also allow operators following an opening parenthesis, since
3358 # those tend to be macros that deal with operators.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003359 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003360 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00003361 not (match.group(1) == 'operator' and match.group(2) == ';')):
3362 error(filename, linenum, 'whitespace/operators', 3,
3363 'Missing spaces around <<')
erg@google.comd350fe52013-01-14 17:51:48 +00003364
3365 # We allow no-spaces around >> for almost anything. This is because
3366 # C++11 allows ">>" to close nested templates, which accounts for
3367 # most cases when ">>" is not followed by a space.
3368 #
3369 # We still warn on ">>" followed by alpha character, because that is
3370 # likely due to ">>" being used for right shifts, e.g.:
3371 # value >> alpha
3372 #
3373 # When ">>" is used to close templates, the alphanumeric letter that
3374 # follows would be part of an identifier, and there should still be
3375 # a space separating the template type and the identifier.
3376 # type<type<type>> alpha
3377 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003378 if match:
3379 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00003380 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003381
3382 # There shouldn't be space around unary operators
3383 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3384 if match:
3385 error(filename, linenum, 'whitespace/operators', 4,
3386 'Extra space for operator %s' % match.group(1))
3387
avakulenko@google.com02af6282014-06-04 18:53:25 +00003388
3389def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3390 """Checks for horizontal spacing around parentheses.
3391
3392 Args:
3393 filename: The name of the current file.
3394 clean_lines: A CleansedLines instance containing the file.
3395 linenum: The number of the line to check.
3396 error: The function to call with any errors found.
3397 """
3398 line = clean_lines.elided[linenum]
3399
3400 # No spaces after an if, while, switch, or for
erg@google.com4e00b9a2009-01-12 23:05:11 +00003401 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3402 if match:
3403 error(filename, linenum, 'whitespace/parens', 5,
3404 'Missing space before ( in %s' % match.group(1))
3405
3406 # For if/for/while/switch, the left and right parens should be
3407 # consistent about how many spaces are inside the parens, and
3408 # there should either be zero or one spaces inside the parens.
3409 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00003410 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003411 match = Search(r'\b(if|for|while|switch)\s*'
3412 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3413 line)
3414 if match:
3415 if len(match.group(2)) != len(match.group(4)):
3416 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00003417 len(match.group(2)) == 1 + len(match.group(4)) or
3418 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003419 error(filename, linenum, 'whitespace/parens', 5,
3420 'Mismatching spaces inside () in %s' % match.group(1))
erg@google.comc6671232013-10-25 21:44:03 +00003421 if len(match.group(2)) not in [0, 1]:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003422 error(filename, linenum, 'whitespace/parens', 5,
3423 'Should have zero or one spaces inside ( and ) in %s' %
3424 match.group(1))
3425
avakulenko@google.com02af6282014-06-04 18:53:25 +00003426
3427def CheckCommaSpacing(filename, clean_lines, linenum, error):
3428 """Checks for horizontal spacing near commas and semicolons.
3429
3430 Args:
3431 filename: The name of the current file.
3432 clean_lines: A CleansedLines instance containing the file.
3433 linenum: The number of the line to check.
3434 error: The function to call with any errors found.
3435 """
3436 raw = clean_lines.lines_without_raw_strings
3437 line = clean_lines.elided[linenum]
3438
erg@google.com4e00b9a2009-01-12 23:05:11 +00003439 # You should always have a space after a comma (either as fn arg or operator)
erg@google.comc6671232013-10-25 21:44:03 +00003440 #
3441 # This does not apply when the non-space character following the
3442 # comma is another comma, since the only time when that happens is
3443 # for empty macro arguments.
erg@google.com2aa59982013-10-28 19:09:25 +00003444 #
3445 # We run this check in two passes: first pass on elided lines to
3446 # verify that lines contain missing whitespaces, second pass on raw
3447 # lines to confirm that those missing whitespaces are not due to
3448 # elided comments.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003449 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3450 Search(r',[^,\s]', raw[linenum])):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003451 error(filename, linenum, 'whitespace/comma', 3,
3452 'Missing space after ,')
3453
erg@google.comd7d27472011-09-07 17:36:35 +00003454 # You should always have a space after a semicolon
3455 # except for few corner cases
3456 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3457 # space after ;
3458 if Search(r';[^\s};\\)/]', line):
3459 error(filename, linenum, 'whitespace/semicolon', 3,
3460 'Missing space after ;')
3461
avakulenko@google.com02af6282014-06-04 18:53:25 +00003462
Alex Vakulenko01e47232016-05-06 13:54:15 -07003463def _IsType(clean_lines, nesting_state, expr):
3464 """Check if expression looks like a type name, returns true if so.
3465
3466 Args:
3467 clean_lines: A CleansedLines instance containing the file.
3468 nesting_state: A NestingState instance which maintains information about
3469 the current stack of nested blocks being parsed.
3470 expr: The expression to check.
3471 Returns:
3472 True, if token looks like a type.
3473 """
3474 # Keep only the last token in the expression
3475 last_word = Match(r'^.*(\b\S+)$', expr)
3476 if last_word:
3477 token = last_word.group(1)
3478 else:
3479 token = expr
3480
3481 # Match native types and stdint types
3482 if _TYPES.match(token):
3483 return True
3484
3485 # Try a bit harder to match templated types. Walk up the nesting
3486 # stack until we find something that resembles a typename
3487 # declaration for what we are looking for.
3488 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) +
3489 r'\b')
3490 block_index = len(nesting_state.stack) - 1
3491 while block_index >= 0:
3492 if isinstance(nesting_state.stack[block_index], _NamespaceInfo):
3493 return False
3494
3495 # Found where the opening brace is. We want to scan from this
3496 # line up to the beginning of the function, minus a few lines.
3497 # template <typename Type1, // stop scanning here
3498 # ...>
3499 # class C
3500 # : public ... { // start scanning here
3501 last_line = nesting_state.stack[block_index].starting_linenum
3502
3503 next_block_start = 0
3504 if block_index > 0:
3505 next_block_start = nesting_state.stack[block_index - 1].starting_linenum
3506 first_line = last_line
3507 while first_line >= next_block_start:
3508 if clean_lines.elided[first_line].find('template') >= 0:
3509 break
3510 first_line -= 1
3511 if first_line < next_block_start:
3512 # Didn't find any "template" keyword before reaching the next block,
3513 # there are probably no template things to check for this block
3514 block_index -= 1
3515 continue
3516
3517 # Look for typename in the specified range
3518 for i in xrange(first_line, last_line + 1, 1):
3519 if Search(typename_pattern, clean_lines.elided[i]):
3520 return True
3521 block_index -= 1
3522
3523 return False
3524
3525
3526def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003527 """Checks for horizontal spacing near commas.
3528
3529 Args:
3530 filename: The name of the current file.
3531 clean_lines: A CleansedLines instance containing the file.
3532 linenum: The number of the line to check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003533 nesting_state: A NestingState instance which maintains information about
3534 the current stack of nested blocks being parsed.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003535 error: The function to call with any errors found.
3536 """
3537 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003538
erg@google.com8a95ecc2011-09-08 00:45:54 +00003539 # Except after an opening paren, or after another opening brace (in case of
3540 # an initializer list, for instance), you should have spaces before your
Alex Vakulenko01e47232016-05-06 13:54:15 -07003541 # braces when they are delimiting blocks, classes, namespaces etc.
3542 # And since you should never have braces at the beginning of a line,
3543 # this is an easy test. Except that braces used for initialization don't
3544 # follow the same rule; we often don't want spaces before those.
avakulenko@google.com554223d2014-12-04 22:00:20 +00003545 match = Match(r'^(.*[^ ({>]){', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07003546
erg@google.com2aa59982013-10-28 19:09:25 +00003547 if match:
3548 # Try a bit harder to check for brace initialization. This
3549 # happens in one of the following forms:
3550 # Constructor() : initializer_list_{} { ... }
3551 # Constructor{}.MemberFunction()
3552 # Type variable{};
3553 # FunctionCall(type{}, ...);
3554 # LastArgument(..., type{});
3555 # LOG(INFO) << type{} << " ...";
3556 # map_of_type[{...}] = ...;
avakulenko@google.com02af6282014-06-04 18:53:25 +00003557 # ternary = expr ? new type{} : nullptr;
3558 # OuterTemplate<InnerTemplateConstructor<Type>{}>
erg@google.com2aa59982013-10-28 19:09:25 +00003559 #
3560 # We check for the character following the closing brace, and
3561 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003562 # "{.;,)<>]:".
erg@google.com2aa59982013-10-28 19:09:25 +00003563 #
3564 # To account for nested initializer list, we allow any number of
3565 # closing braces up to "{;,)<". We can't simply silence the
3566 # warning on first sight of closing brace, because that would
3567 # cause false negatives for things that are not initializer lists.
3568 # Silence this: But not this:
3569 # Outer{ if (...) {
3570 # Inner{...} if (...){ // Missing space before {
3571 # }; }
3572 #
3573 # There is a false negative with this approach if people inserted
3574 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3575 # spurious semicolon with a separate check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003576 leading_text = match.group(1)
erg@google.com2aa59982013-10-28 19:09:25 +00003577 (endline, endlinenum, endpos) = CloseExpression(
3578 clean_lines, linenum, len(match.group(1)))
3579 trailing_text = ''
3580 if endpos > -1:
3581 trailing_text = endline[endpos:]
3582 for offset in xrange(endlinenum + 1,
3583 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3584 trailing_text += clean_lines.elided[offset]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003585 # We also suppress warnings for `uint64_t{expression}` etc., as the style
3586 # guide recommends brace initialization for integral types to avoid
3587 # overflow/truncation.
3588 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text)
3589 and not _IsType(clean_lines, nesting_state, leading_text)):
erg@google.com2aa59982013-10-28 19:09:25 +00003590 error(filename, linenum, 'whitespace/braces', 5,
3591 'Missing space before {')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003592
3593 # Make sure '} else {' has spaces.
3594 if Search(r'}else', line):
3595 error(filename, linenum, 'whitespace/braces', 5,
3596 'Missing space before else')
3597
erg@google.com4e00b9a2009-01-12 23:05:11 +00003598 # You shouldn't have a space before a semicolon at the end of the line.
3599 # There's a special case for "for" since the style guide allows space before
3600 # the semicolon there.
3601 if Search(r':\s*;\s*$', line):
3602 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00003603 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003604 elif Search(r'^\s*;\s*$', line):
3605 error(filename, linenum, 'whitespace/semicolon', 5,
3606 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00003607 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003608 elif (Search(r'\s+;\s*$', line) and
3609 not Search(r'\bfor\b', line)):
3610 error(filename, linenum, 'whitespace/semicolon', 5,
3611 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00003612 'statement, use {} instead.')
3613
avakulenko@google.com02af6282014-06-04 18:53:25 +00003614
3615def IsDecltype(clean_lines, linenum, column):
3616 """Check if the token ending on (linenum, column) is decltype().
3617
3618 Args:
3619 clean_lines: A CleansedLines instance containing the file.
3620 linenum: the number of the line to check.
3621 column: end column of the token to check.
3622 Returns:
3623 True if this token is decltype() expression, False otherwise.
3624 """
3625 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3626 if start_col < 0:
3627 return False
3628 if Search(r'\bdecltype\s*$', text[0:start_col]):
3629 return True
3630 return False
3631
3632
erg@google.com8a95ecc2011-09-08 00:45:54 +00003633def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3634 """Checks for additional blank line issues related to sections.
3635
3636 Currently the only thing checked here is blank line before protected/private.
3637
3638 Args:
3639 filename: The name of the current file.
3640 clean_lines: A CleansedLines instance containing the file.
3641 class_info: A _ClassInfo objects.
3642 linenum: The number of the line to check.
3643 error: The function to call with any errors found.
3644 """
3645 # Skip checks if the class is small, where small means 25 lines or less.
3646 # 25 lines seems like a good cutoff since that's the usual height of
3647 # terminals, and any class that can't fit in one screen can't really
3648 # be considered "small".
3649 #
3650 # Also skip checks if we are on the first line. This accounts for
3651 # classes that look like
3652 # class Foo { public: ... };
3653 #
3654 # If we didn't find the end of the class, last_line would be zero,
3655 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00003656 if (class_info.last_line - class_info.starting_linenum <= 24 or
3657 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003658 return
3659
3660 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3661 if matched:
3662 # Issue warning if the line before public/protected/private was
3663 # not a blank line, but don't do this if the previous line contains
3664 # "class" or "struct". This can happen two ways:
3665 # - We are at the beginning of the class.
3666 # - We are forward-declaring an inner class that is semantically
3667 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00003668 # Also ignores cases where the previous line ends with a backslash as can be
3669 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003670 prev_line = clean_lines.lines[linenum - 1]
3671 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00003672 not Search(r'\b(class|struct)\b', prev_line) and
3673 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003674 # Try a bit harder to find the beginning of the class. This is to
3675 # account for multi-line base-specifier lists, e.g.:
3676 # class Derived
3677 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00003678 end_class_head = class_info.starting_linenum
3679 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003680 if Search(r'\{\s*$', clean_lines.lines[i]):
3681 end_class_head = i
3682 break
3683 if end_class_head < linenum - 1:
3684 error(filename, linenum, 'whitespace/blank_line', 3,
3685 '"%s:" should be preceded by a blank line' % matched.group(1))
3686
3687
erg@google.com4e00b9a2009-01-12 23:05:11 +00003688def GetPreviousNonBlankLine(clean_lines, linenum):
3689 """Return the most recent non-blank line and its line number.
3690
3691 Args:
3692 clean_lines: A CleansedLines instance containing the file contents.
3693 linenum: The number of the line to check.
3694
3695 Returns:
3696 A tuple with two elements. The first element is the contents of the last
3697 non-blank line before the current line, or the empty string if this is the
3698 first non-blank line. The second is the line number of that line, or -1
3699 if this is the first non-blank line.
3700 """
3701
3702 prevlinenum = linenum - 1
3703 while prevlinenum >= 0:
3704 prevline = clean_lines.elided[prevlinenum]
3705 if not IsBlankLine(prevline): # if not a blank line...
3706 return (prevline, prevlinenum)
3707 prevlinenum -= 1
3708 return ('', -1)
3709
3710
3711def CheckBraces(filename, clean_lines, linenum, error):
3712 """Looks for misplaced braces (e.g. at the end of line).
3713
3714 Args:
3715 filename: The name of the current file.
3716 clean_lines: A CleansedLines instance containing the file.
3717 linenum: The number of the line to check.
3718 error: The function to call with any errors found.
3719 """
3720
3721 line = clean_lines.elided[linenum] # get rid of comments and strings
3722
3723 if Match(r'\s*{\s*$', line):
erg@google.com2aa59982013-10-28 19:09:25 +00003724 # We allow an open brace to start a line in the case where someone is using
3725 # braces in a block to explicitly create a new scope, which is commonly used
3726 # to control the lifetime of stack-allocated variables. Braces are also
3727 # used for brace initializers inside function calls. We don't detect this
3728 # perfectly: we just don't complain if the last non-whitespace character on
3729 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
Alex Vakulenko01e47232016-05-06 13:54:15 -07003730 # previous line starts a preprocessor block. We also allow a brace on the
3731 # following line if it is part of an array initialization and would not fit
3732 # within the 80 character limit of the preceding line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003733 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.com2aa59982013-10-28 19:09:25 +00003734 if (not Search(r'[,;:}{(]\s*$', prevline) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07003735 not Match(r'\s*#', prevline) and
3736 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003737 error(filename, linenum, 'whitespace/braces', 4,
3738 '{ should almost always be at the end of the previous line')
3739
3740 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003741 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003742 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3743 if Match(r'\s*}\s*$', prevline):
3744 error(filename, linenum, 'whitespace/newline', 4,
3745 'An else should appear on the same line as the preceding }')
3746
3747 # If braces come on one side of an else, they should be on both.
3748 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.com02af6282014-06-04 18:53:25 +00003749 if Search(r'else if\s*\(', line): # could be multi-line if
3750 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3751 # find the ( after the if
3752 pos = line.find('else if')
3753 pos = line.find('(', pos)
3754 if pos > 0:
3755 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3756 brace_on_right = endline[endpos:].find('{') != -1
3757 if brace_on_left != brace_on_right: # must be brace after if
3758 error(filename, linenum, 'readability/braces', 5,
3759 'If an else has a brace on one side, it should have it on both')
3760 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3761 error(filename, linenum, 'readability/braces', 5,
3762 'If an else has a brace on one side, it should have it on both')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003763
3764 # Likewise, an else should never have the else clause on the same line
3765 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3766 error(filename, linenum, 'whitespace/newline', 4,
3767 'Else clause should never be on same line as else (use 2 lines)')
3768
3769 # In the same way, a do/while should never be on one line
3770 if Match(r'\s*do [^\s{]', line):
3771 error(filename, linenum, 'whitespace/newline', 4,
3772 'do/while clauses should not be on a single line')
3773
avakulenko@google.com02af6282014-06-04 18:53:25 +00003774 # Check single-line if/else bodies. The style guide says 'curly braces are not
3775 # required for single-line statements'. We additionally allow multi-line,
3776 # single statements, but we reject anything with more than one semicolon in
3777 # it. This means that the first semicolon after the if should be at the end of
3778 # its line, and the line after that should have an indent level equal to or
3779 # lower than the if. We also check for ambiguous if/else nesting without
3780 # braces.
3781 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3782 if if_else_match and not Match(r'\s*#', line):
3783 if_indent = GetIndentLevel(line)
3784 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3785 if_match = Search(r'\bif\s*\(', line)
3786 if if_match:
3787 # This could be a multiline if condition, so find the end first.
3788 pos = if_match.end() - 1
3789 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3790 # Check for an opening brace, either directly after the if or on the next
3791 # line. If found, this isn't a single-statement conditional.
3792 if (not Match(r'\s*{', endline[endpos:])
3793 and not (Match(r'\s*$', endline[endpos:])
3794 and endlinenum < (len(clean_lines.elided) - 1)
3795 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3796 while (endlinenum < len(clean_lines.elided)
3797 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3798 endlinenum += 1
3799 endpos = 0
3800 if endlinenum < len(clean_lines.elided):
3801 endline = clean_lines.elided[endlinenum]
3802 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3803 # methods) and a single \ after the semicolon (for macros)
3804 endpos = endline.find(';')
3805 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003806 # Semicolon isn't the last character, there's something trailing.
3807 # Output a warning if the semicolon is not contained inside
3808 # a lambda expression.
3809 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3810 endline):
3811 error(filename, linenum, 'readability/braces', 4,
3812 'If/else bodies with multiple statements require braces')
avakulenko@google.com02af6282014-06-04 18:53:25 +00003813 elif endlinenum < len(clean_lines.elided) - 1:
3814 # Make sure the next line is dedented
3815 next_line = clean_lines.elided[endlinenum + 1]
3816 next_indent = GetIndentLevel(next_line)
3817 # With ambiguous nested if statements, this will error out on the
3818 # if that *doesn't* match the else, regardless of whether it's the
3819 # inner one or outer one.
3820 if (if_match and Match(r'\s*else\b', next_line)
3821 and next_indent != if_indent):
3822 error(filename, linenum, 'readability/braces', 4,
3823 'Else clause should be indented at the same level as if. '
3824 'Ambiguous nested if/else chains require braces.')
3825 elif next_indent > if_indent:
3826 error(filename, linenum, 'readability/braces', 4,
3827 'If/else bodies with multiple statements require braces')
3828
3829
3830def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3831 """Looks for redundant trailing semicolon.
3832
3833 Args:
3834 filename: The name of the current file.
3835 clean_lines: A CleansedLines instance containing the file.
3836 linenum: The number of the line to check.
3837 error: The function to call with any errors found.
3838 """
3839
3840 line = clean_lines.elided[linenum]
3841
erg@google.com2aa59982013-10-28 19:09:25 +00003842 # Block bodies should not be followed by a semicolon. Due to C++11
3843 # brace initialization, there are more places where semicolons are
3844 # required than not, so we use a whitelist approach to check these
3845 # rather than a blacklist. These are the places where "};" should
3846 # be replaced by just "}":
3847 # 1. Some flavor of block following closing parenthesis:
3848 # for (;;) {};
3849 # while (...) {};
3850 # switch (...) {};
3851 # Function(...) {};
3852 # if (...) {};
3853 # if (...) else if (...) {};
3854 #
3855 # 2. else block:
3856 # if (...) else {};
3857 #
3858 # 3. const member function:
3859 # Function(...) const {};
3860 #
3861 # 4. Block following some statement:
3862 # x = 42;
3863 # {};
3864 #
3865 # 5. Block at the beginning of a function:
3866 # Function(...) {
3867 # {};
3868 # }
3869 #
3870 # Note that naively checking for the preceding "{" will also match
3871 # braces inside multi-dimensional arrays, but this is fine since
3872 # that expression will not contain semicolons.
3873 #
3874 # 6. Block following another block:
3875 # while (true) {}
3876 # {};
3877 #
3878 # 7. End of namespaces:
3879 # namespace {};
3880 #
3881 # These semicolons seems far more common than other kinds of
3882 # redundant semicolons, possibly due to people converting classes
3883 # to namespaces. For now we do not warn for this case.
3884 #
3885 # Try matching case 1 first.
3886 match = Match(r'^(.*\)\s*)\{', line)
3887 if match:
3888 # Matched closing parenthesis (case 1). Check the token before the
3889 # matching opening parenthesis, and don't warn if it looks like a
3890 # macro. This avoids these false positives:
3891 # - macro that defines a base class
3892 # - multi-line macro that defines a base class
3893 # - macro that defines the whole class-head
3894 #
3895 # But we still issue warnings for macros that we know are safe to
3896 # warn, specifically:
3897 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3898 # - TYPED_TEST
3899 # - INTERFACE_DEF
3900 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3901 #
3902 # We implement a whitelist of safe macros instead of a blacklist of
3903 # unsafe macros, even though the latter appears less frequently in
3904 # google code and would have been easier to implement. This is because
3905 # the downside for getting the whitelist wrong means some extra
3906 # semicolons, while the downside for getting the blacklist wrong
3907 # would result in compile errors.
3908 #
avakulenko@google.com554223d2014-12-04 22:00:20 +00003909 # In addition to macros, we also don't want to warn on
3910 # - Compound literals
3911 # - Lambdas
Alex Vakulenko01e47232016-05-06 13:54:15 -07003912 # - alignas specifier with anonymous structs
3913 # - decltype
erg@google.com2aa59982013-10-28 19:09:25 +00003914 closing_brace_pos = match.group(1).rfind(')')
3915 opening_parenthesis = ReverseCloseExpression(
3916 clean_lines, linenum, closing_brace_pos)
3917 if opening_parenthesis[2] > -1:
3918 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003919 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix)
avakulenko@google.com02af6282014-06-04 18:53:25 +00003920 func = Match(r'^(.*\])\s*$', line_prefix)
erg@google.com2aa59982013-10-28 19:09:25 +00003921 if ((macro and
3922 macro.group(1) not in (
3923 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3924 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3925 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.com02af6282014-06-04 18:53:25 +00003926 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
avakulenko@google.com554223d2014-12-04 22:00:20 +00003927 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or
Alex Vakulenko01e47232016-05-06 13:54:15 -07003928 Search(r'\bdecltype$', line_prefix) or
erg@google.com2aa59982013-10-28 19:09:25 +00003929 Search(r'\s+=\s*$', line_prefix)):
3930 match = None
avakulenko@google.com02af6282014-06-04 18:53:25 +00003931 if (match and
3932 opening_parenthesis[1] > 1 and
3933 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3934 # Multi-line lambda-expression
3935 match = None
erg@google.com2aa59982013-10-28 19:09:25 +00003936
3937 else:
3938 # Try matching cases 2-3.
3939 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3940 if not match:
3941 # Try matching cases 4-6. These are always matched on separate lines.
3942 #
3943 # Note that we can't simply concatenate the previous line to the
3944 # current line and do a single match, otherwise we may output
3945 # duplicate warnings for the blank line case:
3946 # if (cond) {
3947 # // blank line
3948 # }
3949 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3950 if prevline and Search(r'[;{}]\s*$', prevline):
3951 match = Match(r'^(\s*)\{', line)
3952
3953 # Check matching closing brace
3954 if match:
3955 (endline, endlinenum, endpos) = CloseExpression(
3956 clean_lines, linenum, len(match.group(1)))
3957 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3958 # Current {} pair is eligible for semicolon check, and we have found
3959 # the redundant semicolon, output warning here.
3960 #
3961 # Note: because we are scanning forward for opening braces, and
3962 # outputting warnings for the matching closing brace, if there are
3963 # nested blocks with trailing semicolons, we will get the error
3964 # messages in reversed order.
Piotr Semenovb7e2ef62016-05-20 18:39:34 +03003965
3966 # We need to check the line forward for NOLINT
3967 raw_lines = clean_lines.raw_lines
3968 ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1,
3969 error)
3970 ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum,
3971 error)
3972
erg@google.com2aa59982013-10-28 19:09:25 +00003973 error(filename, endlinenum, 'readability/braces', 4,
3974 "You don't need a ; after a }")
erg@google.com4e00b9a2009-01-12 23:05:11 +00003975
3976
erg@google.comc6671232013-10-25 21:44:03 +00003977def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3978 """Look for empty loop/conditional body with only a single semicolon.
erg@google.comd350fe52013-01-14 17:51:48 +00003979
3980 Args:
3981 filename: The name of the current file.
3982 clean_lines: A CleansedLines instance containing the file.
3983 linenum: The number of the line to check.
3984 error: The function to call with any errors found.
3985 """
3986
3987 # Search for loop keywords at the beginning of the line. Because only
3988 # whitespaces are allowed before the keywords, this will also ignore most
3989 # do-while-loops, since those lines should start with closing brace.
erg@google.comc6671232013-10-25 21:44:03 +00003990 #
3991 # We also check "if" blocks here, since an empty conditional block
3992 # is likely an error.
erg@google.comd350fe52013-01-14 17:51:48 +00003993 line = clean_lines.elided[linenum]
erg@google.comc6671232013-10-25 21:44:03 +00003994 matched = Match(r'\s*(for|while|if)\s*\(', line)
3995 if matched:
Alex Vakulenko01e47232016-05-06 13:54:15 -07003996 # Find the end of the conditional expression.
erg@google.comd350fe52013-01-14 17:51:48 +00003997 (end_line, end_linenum, end_pos) = CloseExpression(
3998 clean_lines, linenum, line.find('('))
3999
4000 # Output warning if what follows the condition expression is a semicolon.
4001 # No warning for all other cases, including whitespace or newline, since we
4002 # have a separate check for semicolons preceded by whitespace.
4003 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
erg@google.comc6671232013-10-25 21:44:03 +00004004 if matched.group(1) == 'if':
4005 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
4006 'Empty conditional bodies should use {}')
4007 else:
4008 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
4009 'Empty loop bodies should use {} or continue')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004010
Alex Vakulenko01e47232016-05-06 13:54:15 -07004011 # Check for if statements that have completely empty bodies (no comments)
4012 # and no else clauses.
4013 if end_pos >= 0 and matched.group(1) == 'if':
4014 # Find the position of the opening { for the if statement.
4015 # Return without logging an error if it has no brackets.
4016 opening_linenum = end_linenum
4017 opening_line_fragment = end_line[end_pos:]
4018 # Loop until EOF or find anything that's not whitespace or opening {.
4019 while not Search(r'^\s*\{', opening_line_fragment):
4020 if Search(r'^(?!\s*$)', opening_line_fragment):
4021 # Conditional has no brackets.
4022 return
4023 opening_linenum += 1
4024 if opening_linenum == len(clean_lines.elided):
4025 # Couldn't find conditional's opening { or any code before EOF.
4026 return
4027 opening_line_fragment = clean_lines.elided[opening_linenum]
4028 # Set opening_line (opening_line_fragment may not be entire opening line).
4029 opening_line = clean_lines.elided[opening_linenum]
4030
4031 # Find the position of the closing }.
4032 opening_pos = opening_line_fragment.find('{')
4033 if opening_linenum == end_linenum:
4034 # We need to make opening_pos relative to the start of the entire line.
4035 opening_pos += end_pos
4036 (closing_line, closing_linenum, closing_pos) = CloseExpression(
4037 clean_lines, opening_linenum, opening_pos)
4038 if closing_pos < 0:
4039 return
4040
4041 # Now construct the body of the conditional. This consists of the portion
4042 # of the opening line after the {, all lines until the closing line,
4043 # and the portion of the closing line before the }.
4044 if (clean_lines.raw_lines[opening_linenum] !=
4045 CleanseComments(clean_lines.raw_lines[opening_linenum])):
4046 # Opening line ends with a comment, so conditional isn't empty.
4047 return
4048 if closing_linenum > opening_linenum:
4049 # Opening line after the {. Ignore comments here since we checked above.
4050 body = list(opening_line[opening_pos+1:])
4051 # All lines until closing line, excluding closing line, with comments.
4052 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum])
4053 # Closing line before the }. Won't (and can't) have comments.
4054 body.append(clean_lines.elided[closing_linenum][:closing_pos-1])
4055 body = '\n'.join(body)
4056 else:
4057 # If statement has brackets and fits on a single line.
4058 body = opening_line[opening_pos+1:closing_pos-1]
4059
4060 # Check if the body is empty
4061 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body):
4062 return
4063 # The body is empty. Now make sure there's not an else clause.
4064 current_linenum = closing_linenum
4065 current_line_fragment = closing_line[closing_pos:]
4066 # Loop until EOF or find anything that's not whitespace or else clause.
4067 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment):
4068 if Search(r'^(?=\s*else)', current_line_fragment):
4069 # Found an else clause, so don't log an error.
4070 return
4071 current_linenum += 1
4072 if current_linenum == len(clean_lines.elided):
4073 break
4074 current_line_fragment = clean_lines.elided[current_linenum]
4075
4076 # The body is empty and there's no else clause until EOF or other code.
4077 error(filename, end_linenum, 'whitespace/empty_if_body', 4,
4078 ('If statement had no body and no else clause'))
4079
erg@google.com4e00b9a2009-01-12 23:05:11 +00004080
avakulenko@google.com02af6282014-06-04 18:53:25 +00004081def FindCheckMacro(line):
4082 """Find a replaceable CHECK-like macro.
4083
4084 Args:
4085 line: line to search on.
4086 Returns:
4087 (macro name, start position), or (None, -1) if no replaceable
4088 macro is found.
4089 """
4090 for macro in _CHECK_MACROS:
4091 i = line.find(macro)
4092 if i >= 0:
4093 # Find opening parenthesis. Do a regular expression match here
4094 # to make sure that we are matching the expected CHECK macro, as
4095 # opposed to some other macro that happens to contain the CHECK
4096 # substring.
4097 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4098 if not matched:
4099 continue
4100 return (macro, len(matched.group(1)))
4101 return (None, -1)
4102
4103
erg@google.com4e00b9a2009-01-12 23:05:11 +00004104def CheckCheck(filename, clean_lines, linenum, error):
4105 """Checks the use of CHECK and EXPECT macros.
4106
4107 Args:
4108 filename: The name of the current file.
4109 clean_lines: A CleansedLines instance containing the file.
4110 linenum: The number of the line to check.
4111 error: The function to call with any errors found.
4112 """
4113
4114 # Decide the set of replacement macros that should be suggested
erg@google.comc6671232013-10-25 21:44:03 +00004115 lines = clean_lines.elided
avakulenko@google.com02af6282014-06-04 18:53:25 +00004116 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4117 if not check_macro:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004118 return
4119
erg@google.comc6671232013-10-25 21:44:03 +00004120 # Find end of the boolean expression by matching parentheses
4121 (last_line, end_line, end_pos) = CloseExpression(
4122 clean_lines, linenum, start_pos)
4123 if end_pos < 0:
4124 return
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004125
4126 # If the check macro is followed by something other than a
4127 # semicolon, assume users will log their own custom error messages
4128 # and don't suggest any replacements.
4129 if not Match(r'\s*;', last_line[end_pos:]):
4130 return
4131
erg@google.comc6671232013-10-25 21:44:03 +00004132 if linenum == end_line:
4133 expression = lines[linenum][start_pos + 1:end_pos - 1]
4134 else:
4135 expression = lines[linenum][start_pos + 1:]
4136 for i in xrange(linenum + 1, end_line):
4137 expression += lines[i]
4138 expression += last_line[0:end_pos - 1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004139
erg@google.comc6671232013-10-25 21:44:03 +00004140 # Parse expression so that we can take parentheses into account.
4141 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4142 # which is not replaceable by CHECK_LE.
4143 lhs = ''
4144 rhs = ''
4145 operator = None
4146 while expression:
4147 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4148 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4149 if matched:
4150 token = matched.group(1)
4151 if token == '(':
4152 # Parenthesized operand
4153 expression = matched.group(2)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004154 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
erg@google.comc6671232013-10-25 21:44:03 +00004155 if end < 0:
4156 return # Unmatched parenthesis
4157 lhs += '(' + expression[0:end]
4158 expression = expression[end:]
4159 elif token in ('&&', '||'):
4160 # Logical and/or operators. This means the expression
4161 # contains more than one term, for example:
4162 # CHECK(42 < a && a < b);
4163 #
4164 # These are not replaceable with CHECK_LE, so bail out early.
4165 return
4166 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4167 # Non-relational operator
4168 lhs += token
4169 expression = matched.group(2)
4170 else:
4171 # Relational operator
4172 operator = token
4173 rhs = matched.group(2)
4174 break
4175 else:
4176 # Unparenthesized operand. Instead of appending to lhs one character
4177 # at a time, we do another regular expression match to consume several
4178 # characters at once if possible. Trivial benchmark shows that this
4179 # is more efficient when the operands are longer than a single
4180 # character, which is generally the case.
4181 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4182 if not matched:
4183 matched = Match(r'^(\s*\S)(.*)$', expression)
4184 if not matched:
4185 break
4186 lhs += matched.group(1)
4187 expression = matched.group(2)
4188
4189 # Only apply checks if we got all parts of the boolean expression
4190 if not (lhs and operator and rhs):
4191 return
4192
4193 # Check that rhs do not contain logical operators. We already know
4194 # that lhs is fine since the loop above parses out && and ||.
4195 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4196 return
4197
4198 # At least one of the operands must be a constant literal. This is
4199 # to avoid suggesting replacements for unprintable things like
4200 # CHECK(variable != iterator)
4201 #
4202 # The following pattern matches decimal, hex integers, strings, and
4203 # characters (in that order).
4204 lhs = lhs.strip()
4205 rhs = rhs.strip()
4206 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4207 if Match(match_constant, lhs) or Match(match_constant, rhs):
4208 # Note: since we know both lhs and rhs, we can provide a more
4209 # descriptive error message like:
4210 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4211 # Instead of:
4212 # Consider using CHECK_EQ instead of CHECK(a == b)
4213 #
4214 # We are still keeping the less descriptive message because if lhs
4215 # or rhs gets long, the error message might become unreadable.
4216 error(filename, linenum, 'readability/check', 2,
4217 'Consider using %s instead of %s(a %s b)' % (
4218 _CHECK_REPLACEMENT[check_macro][operator],
4219 check_macro, operator))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004220
4221
erg@google.comd350fe52013-01-14 17:51:48 +00004222def CheckAltTokens(filename, clean_lines, linenum, error):
4223 """Check alternative keywords being used in boolean expressions.
4224
4225 Args:
4226 filename: The name of the current file.
4227 clean_lines: A CleansedLines instance containing the file.
4228 linenum: The number of the line to check.
4229 error: The function to call with any errors found.
4230 """
4231 line = clean_lines.elided[linenum]
4232
4233 # Avoid preprocessor lines
4234 if Match(r'^\s*#', line):
4235 return
4236
4237 # Last ditch effort to avoid multi-line comments. This will not help
4238 # if the comment started before the current line or ended after the
4239 # current line, but it catches most of the false positives. At least,
4240 # it provides a way to workaround this warning for people who use
4241 # multi-line comments in preprocessor macros.
4242 #
4243 # TODO(unknown): remove this once cpplint has better support for
4244 # multi-line comments.
4245 if line.find('/*') >= 0 or line.find('*/') >= 0:
4246 return
4247
4248 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4249 error(filename, linenum, 'readability/alt_tokens', 2,
4250 'Use operator %s instead of %s' % (
4251 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4252
4253
erg@google.com4e00b9a2009-01-12 23:05:11 +00004254def GetLineWidth(line):
4255 """Determines the width of the line in column positions.
4256
4257 Args:
4258 line: A string, which may be a Unicode string.
4259
4260 Returns:
4261 The width of the line in column positions, accounting for Unicode
4262 combining characters and wide characters.
4263 """
4264 if isinstance(line, unicode):
4265 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00004266 for uc in unicodedata.normalize('NFC', line):
4267 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004268 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00004269 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004270 width += 1
4271 return width
4272 else:
4273 return len(line)
4274
4275
erg@google.comd350fe52013-01-14 17:51:48 +00004276def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00004277 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004278 """Checks rules from the 'C++ style rules' section of cppguide.html.
4279
4280 Most of these rules are hard to test (naming, comment style), but we
4281 do what we can. In particular we check for 2-space indents, line lengths,
4282 tab usage, spaces inside code, etc.
4283
4284 Args:
4285 filename: The name of the current file.
4286 clean_lines: A CleansedLines instance containing the file.
4287 linenum: The number of the line to check.
4288 file_extension: The extension (without the dot) of the filename.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004289 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00004290 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004291 error: The function to call with any errors found.
4292 """
4293
erg@google.com2aa59982013-10-28 19:09:25 +00004294 # Don't use "elided" lines here, otherwise we can't check commented lines.
4295 # Don't want to use "raw" either, because we don't want to check inside C++11
4296 # raw strings,
4297 raw_lines = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00004298 line = raw_lines[linenum]
Alex Vakulenko01e47232016-05-06 13:54:15 -07004299 prev = raw_lines[linenum - 1] if linenum > 0 else ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00004300
4301 if line.find('\t') != -1:
4302 error(filename, linenum, 'whitespace/tab', 1,
4303 'Tab found; better to use spaces')
4304
4305 # One or three blank spaces at the beginning of the line is weird; it's
4306 # hard to reconcile that with 2-space indents.
4307 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4308 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4309 # if(RLENGTH > 20) complain = 0;
4310 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4311 # if(match(prev, "&& *$")) complain = 0;
4312 # if(match(prev, "\\|\\| *$")) complain = 0;
4313 # if(match(prev, "[\",=><] *$")) complain = 0;
4314 # if(match($0, " <<")) complain = 0;
4315 # if(match(prev, " +for \\(")) complain = 0;
4316 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.com02af6282014-06-04 18:53:25 +00004317 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4318 classinfo = nesting_state.InnermostClass()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004319 initial_spaces = 0
4320 cleansed_line = clean_lines.elided[linenum]
4321 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4322 initial_spaces += 1
avakulenko@google.com02af6282014-06-04 18:53:25 +00004323 # There are certain situations we allow one space, notably for
4324 # section labels, and also lines containing multi-line raw strings.
Alex Vakulenko01e47232016-05-06 13:54:15 -07004325 # We also don't check for lines that look like continuation lines
4326 # (of lines ending in double quotes, commas, equals, or angle brackets)
4327 # because the rules for how to indent those are non-trivial.
4328 if (not Search(r'[",=><] *$', prev) and
4329 (initial_spaces == 1 or initial_spaces == 3) and
4330 not Match(scope_or_label_pattern, cleansed_line) and
4331 not (clean_lines.raw_lines[linenum] != line and
4332 Match(r'^\s*""', line))):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004333 error(filename, linenum, 'whitespace/indent', 3,
4334 'Weird number of spaces at line-start. '
4335 'Are you using a 2-space indent?')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004336
Alex Vakulenko01e47232016-05-06 13:54:15 -07004337 if line and line[-1].isspace():
4338 error(filename, linenum, 'whitespace/end_of_line', 4,
4339 'Line ends in whitespace. Consider deleting these extra spaces.')
4340
erg@google.com4e00b9a2009-01-12 23:05:11 +00004341 # Check if the line is a header guard.
4342 is_header_guard = False
LukeCz7197a242016-09-24 13:27:35 -05004343 if IsHeaderExtension(file_extension):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004344 cppvar = GetHeaderGuardCPPVariable(filename)
4345 if (line.startswith('#ifndef %s' % cppvar) or
4346 line.startswith('#define %s' % cppvar) or
4347 line.startswith('#endif // %s' % cppvar)):
4348 is_header_guard = True
4349 # #include lines and header guards can be long, since there's no clean way to
4350 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00004351 #
4352 # URLs can be long too. It's possible to split these, but it makes them
4353 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00004354 #
4355 # The "$Id:...$" comment may also get very long without it being the
4356 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00004357 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00004358 not Match(r'^\s*//.*http(s?)://\S*$', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004359 not Match(r'^\s*//\s*[^\s]*$', line) and
erg@google.comd7d27472011-09-07 17:36:35 +00004360 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004361 line_width = GetLineWidth(line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004362 if line_width > _line_length:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004363 error(filename, linenum, 'whitespace/line_length', 2,
erg@google.comab53edf2013-11-05 22:23:37 +00004364 'Lines should be <= %i characters long' % _line_length)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004365
4366 if (cleansed_line.count(';') > 1 and
4367 # for loops are allowed two ;'s (and may run over two lines).
4368 cleansed_line.find('for') == -1 and
4369 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4370 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4371 # It's ok to have many commands in a switch case that fits in 1 line
4372 not ((cleansed_line.find('case ') != -1 or
4373 cleansed_line.find('default:') != -1) and
4374 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00004375 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00004376 'More than one command on the same line')
4377
4378 # Some more style checks
4379 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004380 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
erg@google.comc6671232013-10-25 21:44:03 +00004381 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004382 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004383 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4384 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4385 CheckCommaSpacing(filename, clean_lines, linenum, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004386 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004387 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004388 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004389 CheckAltTokens(filename, clean_lines, linenum, error)
4390 classinfo = nesting_state.InnermostClass()
4391 if classinfo:
4392 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004393
4394
erg@google.com4e00b9a2009-01-12 23:05:11 +00004395_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4396# Matches the first component of a filename delimited by -s and _s. That is:
4397# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4398# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4399# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4400# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4401_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4402
4403
4404def _DropCommonSuffixes(filename):
4405 """Drops common suffixes like _test.cc or -inl.h from filename.
4406
4407 For example:
4408 >>> _DropCommonSuffixes('foo/foo-inl.h')
4409 'foo/foo'
4410 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4411 'foo/bar/foo'
4412 >>> _DropCommonSuffixes('foo/foo_internal.h')
4413 'foo/foo'
4414 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4415 'foo/foo_unusualinternal'
4416
4417 Args:
4418 filename: The input filename.
4419
4420 Returns:
4421 The filename with the common suffix removed.
4422 """
4423 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4424 'inl.h', 'impl.h', 'internal.h'):
4425 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4426 filename[-len(suffix) - 1] in ('-', '_')):
4427 return filename[:-len(suffix) - 1]
4428 return os.path.splitext(filename)[0]
4429
4430
erg@google.com4e00b9a2009-01-12 23:05:11 +00004431def _ClassifyInclude(fileinfo, include, is_system):
4432 """Figures out what kind of header 'include' is.
4433
4434 Args:
4435 fileinfo: The current file cpplint is running over. A FileInfo instance.
4436 include: The path to a #included file.
4437 is_system: True if the #include used <> rather than "".
4438
4439 Returns:
4440 One of the _XXX_HEADER constants.
4441
4442 For example:
4443 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4444 _C_SYS_HEADER
4445 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4446 _CPP_SYS_HEADER
4447 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4448 _LIKELY_MY_HEADER
4449 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4450 ... 'bar/foo_other_ext.h', False)
4451 _POSSIBLE_MY_HEADER
4452 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4453 _OTHER_HEADER
4454 """
4455 # This is a list of all standard c++ header files, except
4456 # those already checked for above.
erg@google.comfd5da632013-10-25 17:39:45 +00004457 is_cpp_h = include in _CPP_HEADERS
erg@google.com4e00b9a2009-01-12 23:05:11 +00004458
4459 if is_system:
4460 if is_cpp_h:
4461 return _CPP_SYS_HEADER
4462 else:
4463 return _C_SYS_HEADER
4464
4465 # If the target file and the include we're checking share a
4466 # basename when we drop common extensions, and the include
4467 # lives in . , then it's likely to be owned by the target file.
4468 target_dir, target_base = (
4469 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4470 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4471 if target_base == include_base and (
4472 include_dir == target_dir or
4473 include_dir == os.path.normpath(target_dir + '/../public')):
4474 return _LIKELY_MY_HEADER
4475
4476 # If the target and include share some initial basename
4477 # component, it's possible the target is implementing the
4478 # include, so it's allowed to be first, but we'll never
4479 # complain if it's not there.
4480 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4481 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4482 if (target_first_component and include_first_component and
4483 target_first_component.group(0) ==
4484 include_first_component.group(0)):
4485 return _POSSIBLE_MY_HEADER
4486
4487 return _OTHER_HEADER
4488
4489
erg@google.coma87abb82009-02-24 01:41:01 +00004490
erg@google.come35f7652009-06-19 20:52:09 +00004491def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4492 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004493
erg@google.come35f7652009-06-19 20:52:09 +00004494 Strings on #include lines are NOT removed from elided line, to make
4495 certain tasks easier. However, to prevent false positives, checks
4496 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004497
4498 Args:
4499 filename: The name of the current file.
4500 clean_lines: A CleansedLines instance containing the file.
4501 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004502 include_state: An _IncludeState instance in which the headers are inserted.
4503 error: The function to call with any errors found.
4504 """
4505 fileinfo = FileInfo(filename)
erg@google.come35f7652009-06-19 20:52:09 +00004506 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004507
4508 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004509 # Only do this check if the included header follows google naming
4510 # conventions. If not, assume that it's a 3rd party API that
4511 # requires special include conventions.
4512 #
4513 # We also make an exception for Lua headers, which follow google
4514 # naming convention but not the include convention.
4515 match = Match(r'#include\s*"([^/]+\.h)"', line)
4516 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004517 error(filename, linenum, 'build/include', 4,
4518 'Include the directory when naming .h files')
4519
4520 # we shouldn't include a file more than once. actually, there are a
4521 # handful of instances where doing so is okay, but in general it's
4522 # not.
erg@google.come35f7652009-06-19 20:52:09 +00004523 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004524 if match:
4525 include = match.group(2)
4526 is_system = (match.group(1) == '<')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004527 duplicate_line = include_state.FindHeader(include)
4528 if duplicate_line >= 0:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004529 error(filename, linenum, 'build/include', 4,
4530 '"%s" already included at %s:%s' %
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004531 (include, filename, duplicate_line))
avakulenko@google.com554223d2014-12-04 22:00:20 +00004532 elif (include.endswith('.cc') and
4533 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)):
4534 error(filename, linenum, 'build/include', 4,
4535 'Do not include .cc files from other packages')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004536 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4537 include_state.include_list[-1].append((include, linenum))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004538
4539 # We want to ensure that headers appear in the right order:
4540 # 1) for foo.cc, foo.h (preferred location)
4541 # 2) c system files
4542 # 3) cpp system files
4543 # 4) for foo.cc, foo.h (deprecated location)
4544 # 5) other google headers
4545 #
4546 # We classify each include statement as one of those 5 types
4547 # using a number of techniques. The include_state object keeps
4548 # track of the highest type seen, and complains if we see a
4549 # lower type after that.
4550 error_message = include_state.CheckNextIncludeOrder(
4551 _ClassifyInclude(fileinfo, include, is_system))
4552 if error_message:
4553 error(filename, linenum, 'build/include_order', 4,
4554 '%s. Should be: %s.h, c system, c++ system, other.' %
4555 (error_message, fileinfo.BaseName()))
erg@google.comfd5da632013-10-25 17:39:45 +00004556 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4557 if not include_state.IsInAlphabeticalOrder(
4558 clean_lines, linenum, canonical_include):
erg@google.coma868d2d2009-10-09 21:18:45 +00004559 error(filename, linenum, 'build/include_alpha', 4,
4560 'Include "%s" not in alphabetical order' % include)
erg@google.comfd5da632013-10-25 17:39:45 +00004561 include_state.SetLastHeader(canonical_include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004562
erg@google.come35f7652009-06-19 20:52:09 +00004563
erg@google.com8a95ecc2011-09-08 00:45:54 +00004564
4565def _GetTextInside(text, start_pattern):
erg@google.com2aa59982013-10-28 19:09:25 +00004566 r"""Retrieves all the text between matching open and close parentheses.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004567
4568 Given a string of lines and a regular expression string, retrieve all the text
4569 following the expression and between opening punctuation symbols like
4570 (, [, or {, and the matching close-punctuation symbol. This properly nested
4571 occurrences of the punctuations, so for the text like
4572 printf(a(), b(c()));
4573 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4574 start_pattern must match string having an open punctuation symbol at the end.
4575
4576 Args:
4577 text: The lines to extract text. Its comments and strings must be elided.
4578 It can be single line and can span multiple lines.
4579 start_pattern: The regexp string indicating where to start extracting
4580 the text.
4581 Returns:
4582 The extracted text.
4583 None if either the opening string or ending punctuation could not be found.
4584 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00004585 # TODO(unknown): Audit cpplint.py to see what places could be profitably
erg@google.com8a95ecc2011-09-08 00:45:54 +00004586 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4587
4588 # Give opening punctuations to get the matching close-punctuations.
4589 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4590 closing_punctuation = set(matching_punctuation.itervalues())
4591
4592 # Find the position to start extracting text.
4593 match = re.search(start_pattern, text, re.M)
4594 if not match: # start_pattern not found in text.
4595 return None
4596 start_position = match.end(0)
4597
4598 assert start_position > 0, (
4599 'start_pattern must ends with an opening punctuation.')
4600 assert text[start_position - 1] in matching_punctuation, (
4601 'start_pattern must ends with an opening punctuation.')
4602 # Stack of closing punctuations we expect to have in text after position.
4603 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4604 position = start_position
4605 while punctuation_stack and position < len(text):
4606 if text[position] == punctuation_stack[-1]:
4607 punctuation_stack.pop()
4608 elif text[position] in closing_punctuation:
4609 # A closing punctuation without matching opening punctuations.
4610 return None
4611 elif text[position] in matching_punctuation:
4612 punctuation_stack.append(matching_punctuation[text[position]])
4613 position += 1
4614 if punctuation_stack:
4615 # Opening punctuations left without matching close-punctuations.
4616 return None
4617 # punctuations match.
4618 return text[start_position:position - 1]
4619
4620
erg@google.comfd5da632013-10-25 17:39:45 +00004621# Patterns for matching call-by-reference parameters.
erg@google.com2aa59982013-10-28 19:09:25 +00004622#
4623# Supports nested templates up to 2 levels deep using this messy pattern:
4624# < (?: < (?: < [^<>]*
4625# >
4626# | [^<>] )*
4627# >
4628# | [^<>] )*
4629# >
erg@google.comfd5da632013-10-25 17:39:45 +00004630_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4631_RE_PATTERN_TYPE = (
4632 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
erg@google.com2aa59982013-10-28 19:09:25 +00004633 r'(?:\w|'
4634 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4635 r'::)+')
erg@google.comfd5da632013-10-25 17:39:45 +00004636# A call-by-reference parameter ends with '& identifier'.
4637_RE_PATTERN_REF_PARAM = re.compile(
4638 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4639 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4640# A call-by-const-reference parameter either ends with 'const& identifier'
4641# or looks like 'const type& identifier' when 'type' is atomic.
4642_RE_PATTERN_CONST_REF_PARAM = (
4643 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4644 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004645# Stream types.
4646_RE_PATTERN_REF_STREAM_PARAM = (
4647 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')')
erg@google.comfd5da632013-10-25 17:39:45 +00004648
4649
4650def CheckLanguage(filename, clean_lines, linenum, file_extension,
4651 include_state, nesting_state, error):
erg@google.come35f7652009-06-19 20:52:09 +00004652 """Checks rules from the 'C++ language rules' section of cppguide.html.
4653
4654 Some of these rules are hard to test (function overloading, using
4655 uint32 inappropriately), but we do the best we can.
4656
4657 Args:
4658 filename: The name of the current file.
4659 clean_lines: A CleansedLines instance containing the file.
4660 linenum: The number of the line to check.
4661 file_extension: The extension (without the dot) of the filename.
4662 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004663 nesting_state: A NestingState instance which maintains information about
erg@google.comfd5da632013-10-25 17:39:45 +00004664 the current stack of nested blocks being parsed.
erg@google.come35f7652009-06-19 20:52:09 +00004665 error: The function to call with any errors found.
4666 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00004667 # If the line is empty or consists of entirely a comment, no need to
4668 # check it.
4669 line = clean_lines.elided[linenum]
4670 if not line:
4671 return
4672
erg@google.come35f7652009-06-19 20:52:09 +00004673 match = _RE_PATTERN_INCLUDE.search(line)
4674 if match:
4675 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4676 return
4677
erg@google.com2aa59982013-10-28 19:09:25 +00004678 # Reset include state across preprocessor directives. This is meant
4679 # to silence warnings for conditional includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004680 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4681 if match:
4682 include_state.ResetSection(match.group(1))
erg@google.com2aa59982013-10-28 19:09:25 +00004683
erg@google.com4e00b9a2009-01-12 23:05:11 +00004684 # Make Windows paths like Unix.
4685 fullname = os.path.abspath(filename).replace('\\', '/')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004686
avakulenko@google.com02af6282014-06-04 18:53:25 +00004687 # Perform other checks now that we are sure that this is not an include line
4688 CheckCasts(filename, clean_lines, linenum, error)
4689 CheckGlobalStatic(filename, clean_lines, linenum, error)
4690 CheckPrintf(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004691
LukeCz7197a242016-09-24 13:27:35 -05004692 if IsHeaderExtension(file_extension):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004693 # TODO(unknown): check that 1-arg constructors are explicit.
4694 # How to tell it's a constructor?
4695 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004696 # TODO(unknown): check that classes declare or disable copy/assign
erg@google.com4e00b9a2009-01-12 23:05:11 +00004697 # (level 1 error)
4698 pass
4699
4700 # Check if people are using the verboten C basic types. The only exception
4701 # we regularly allow is "unsigned short port" for port.
4702 if Search(r'\bshort port\b', line):
4703 if not Search(r'\bunsigned short port\b', line):
4704 error(filename, linenum, 'runtime/int', 4,
4705 'Use "unsigned short" for ports, not "short"')
4706 else:
4707 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4708 if match:
4709 error(filename, linenum, 'runtime/int', 4,
4710 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4711
erg@google.coma868d2d2009-10-09 21:18:45 +00004712 # Check if some verboten operator overloading is going on
4713 # TODO(unknown): catch out-of-line unary operator&:
4714 # class X {};
4715 # int operator&(const X& x) { return 42; } // unary operator&
4716 # The trick is it's hard to tell apart from binary operator&:
4717 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4718 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4719 error(filename, linenum, 'runtime/operator', 4,
4720 'Unary operator& is dangerous. Do not use it.')
4721
erg@google.com4e00b9a2009-01-12 23:05:11 +00004722 # Check for suspicious usage of "if" like
4723 # } if (a == b) {
4724 if Search(r'\}\s*if\s*\(', line):
4725 error(filename, linenum, 'readability/braces', 4,
4726 'Did you mean "else if"? If not, start a new line for "if".')
4727
4728 # Check for potential format string bugs like printf(foo).
4729 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4730 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.com02af6282014-06-04 18:53:25 +00004731 # TODO(unknown): Catch the following case. Need to change the calling
erg@google.com8a95ecc2011-09-08 00:45:54 +00004732 # convention of the whole function to process multiple line to handle it.
4733 # printf(
4734 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4735 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4736 if printf_args:
4737 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00004738 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00004739 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4740 line, re.I).group(1)
4741 error(filename, linenum, 'runtime/printf', 4,
4742 'Potential format string bug. Do %s("%%s", %s) instead.'
4743 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004744
4745 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4746 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4747 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4748 error(filename, linenum, 'runtime/memset', 4,
4749 'Did you mean "memset(%s, 0, %s)"?'
4750 % (match.group(1), match.group(2)))
4751
4752 if Search(r'\busing namespace\b', line):
4753 error(filename, linenum, 'build/namespaces', 5,
4754 'Do not use namespace using-directives. '
4755 'Use using-declarations instead.')
4756
4757 # Detect variable-length arrays.
4758 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4759 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4760 match.group(3).find(']') == -1):
4761 # Split the size using space and arithmetic operators as delimiters.
4762 # If any of the resulting tokens are not compile time constants then
4763 # report the error.
4764 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4765 is_const = True
4766 skip_next = False
4767 for tok in tokens:
4768 if skip_next:
4769 skip_next = False
4770 continue
4771
4772 if Search(r'sizeof\(.+\)', tok): continue
4773 if Search(r'arraysize\(\w+\)', tok): continue
4774
4775 tok = tok.lstrip('(')
4776 tok = tok.rstrip(')')
4777 if not tok: continue
4778 if Match(r'\d+', tok): continue
4779 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4780 if Match(r'k[A-Z0-9]\w*', tok): continue
4781 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4782 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4783 # A catch all for tricky sizeof cases, including 'sizeof expression',
4784 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00004785 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004786 if tok.startswith('sizeof'):
4787 skip_next = True
4788 continue
4789 is_const = False
4790 break
4791 if not is_const:
4792 error(filename, linenum, 'runtime/arrays', 1,
4793 'Do not use variable-length arrays. Use an appropriately named '
4794 "('k' followed by CamelCase) compile-time constant for the size.")
4795
erg@google.com4e00b9a2009-01-12 23:05:11 +00004796 # Check for use of unnamed namespaces in header files. Registration
4797 # macros are typically OK, so we allow use of "namespace {" on lines
4798 # that end with backslashes.
LukeCz7197a242016-09-24 13:27:35 -05004799 if (IsHeaderExtension(file_extension)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004800 and Search(r'\bnamespace\s*{', line)
4801 and line[-1] != '\\'):
4802 error(filename, linenum, 'build/namespaces', 4,
4803 'Do not use unnamed namespaces in header files. See '
Ackermann Yuriy79692902016-04-01 21:41:34 +13004804 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
erg@google.com4e00b9a2009-01-12 23:05:11 +00004805 ' for more information.')
4806
avakulenko@google.com02af6282014-06-04 18:53:25 +00004807
4808def CheckGlobalStatic(filename, clean_lines, linenum, error):
4809 """Check for unsafe global or static objects.
4810
4811 Args:
4812 filename: The name of the current file.
4813 clean_lines: A CleansedLines instance containing the file.
4814 linenum: The number of the line to check.
4815 error: The function to call with any errors found.
4816 """
4817 line = clean_lines.elided[linenum]
4818
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004819 # Match two lines at a time to support multiline declarations
4820 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4821 line += clean_lines.elided[linenum + 1].strip()
4822
avakulenko@google.com02af6282014-06-04 18:53:25 +00004823 # Check for people declaring static/global STL strings at the top level.
4824 # This is dangerous because the C++ language does not guarantee that
Alex Vakulenko01e47232016-05-06 13:54:15 -07004825 # globals with constructors are initialized before the first access, and
4826 # also because globals can be destroyed when some threads are still running.
4827 # TODO(unknown): Generalize this to also find static unique_ptr instances.
4828 # TODO(unknown): File bugs for clang-tidy to find these.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004829 match = Match(
Alex Vakulenko01e47232016-05-06 13:54:15 -07004830 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +'
4831 r'([a-zA-Z0-9_:]+)\b(.*)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00004832 line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004833
avakulenko@google.com02af6282014-06-04 18:53:25 +00004834 # Remove false positives:
4835 # - String pointers (as opposed to values).
4836 # string *pointer
4837 # const string *pointer
4838 # string const *pointer
4839 # string *const pointer
4840 #
4841 # - Functions and template specializations.
4842 # string Function<Type>(...
4843 # string Class<Type>::Method(...
4844 #
4845 # - Operators. These are matched separately because operator names
4846 # cross non-word boundaries, and trying to match both operators
4847 # and functions at the same time would decrease accuracy of
4848 # matching identifiers.
4849 # string Class::operator*()
4850 if (match and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004851 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00004852 not Search(r'\boperator\W', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004853 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))):
4854 if Search(r'\bconst\b', line):
4855 error(filename, linenum, 'runtime/string', 4,
4856 'For a static/global string constant, use a C style string '
4857 'instead: "%schar%s %s[]".' %
4858 (match.group(1), match.group(2) or '', match.group(3)))
4859 else:
4860 error(filename, linenum, 'runtime/string', 4,
4861 'Static/global string variables are not permitted.')
avakulenko@google.com02af6282014-06-04 18:53:25 +00004862
Alex Vakulenko01e47232016-05-06 13:54:15 -07004863 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or
4864 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004865 error(filename, linenum, 'runtime/init', 4,
4866 'You seem to be initializing a member variable with itself.')
4867
4868
4869def CheckPrintf(filename, clean_lines, linenum, error):
4870 """Check for printf related issues.
4871
4872 Args:
4873 filename: The name of the current file.
4874 clean_lines: A CleansedLines instance containing the file.
4875 linenum: The number of the line to check.
4876 error: The function to call with any errors found.
4877 """
4878 line = clean_lines.elided[linenum]
4879
4880 # When snprintf is used, the second argument shouldn't be a literal.
4881 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4882 if match and match.group(2) != '0':
4883 # If 2nd arg is zero, snprintf is used to calculate size.
4884 error(filename, linenum, 'runtime/printf', 3,
4885 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4886 'to snprintf.' % (match.group(1), match.group(2)))
4887
4888 # Check if some verboten C functions are being used.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004889 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004890 error(filename, linenum, 'runtime/printf', 5,
4891 'Never use sprintf. Use snprintf instead.')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004892 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004893 if match:
4894 error(filename, linenum, 'runtime/printf', 4,
4895 'Almost always, snprintf is better than %s' % match.group(1))
4896
4897
4898def IsDerivedFunction(clean_lines, linenum):
4899 """Check if current line contains an inherited function.
4900
4901 Args:
4902 clean_lines: A CleansedLines instance containing the file.
4903 linenum: The number of the line to check.
4904 Returns:
4905 True if current line contains a function with "override"
4906 virt-specifier.
4907 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004908 # Scan back a few lines for start of current function
4909 for i in xrange(linenum, max(-1, linenum - 10), -1):
4910 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4911 if match:
4912 # Look for "override" after the matching closing parenthesis
4913 line, _, closing_paren = CloseExpression(
4914 clean_lines, i, len(match.group(1)))
4915 return (closing_paren >= 0 and
4916 Search(r'\boverride\b', line[closing_paren:]))
4917 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00004918
4919
avakulenko@google.com554223d2014-12-04 22:00:20 +00004920def IsOutOfLineMethodDefinition(clean_lines, linenum):
4921 """Check if current line contains an out-of-line method definition.
4922
4923 Args:
4924 clean_lines: A CleansedLines instance containing the file.
4925 linenum: The number of the line to check.
4926 Returns:
4927 True if current line contains an out-of-line method definition.
4928 """
4929 # Scan back a few lines for start of current function
4930 for i in xrange(linenum, max(-1, linenum - 10), -1):
4931 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
4932 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
4933 return False
4934
4935
avakulenko@google.com02af6282014-06-04 18:53:25 +00004936def IsInitializerList(clean_lines, linenum):
4937 """Check if current line is inside constructor initializer list.
4938
4939 Args:
4940 clean_lines: A CleansedLines instance containing the file.
4941 linenum: The number of the line to check.
4942 Returns:
4943 True if current line appears to be inside constructor initializer
4944 list, False otherwise.
4945 """
4946 for i in xrange(linenum, 1, -1):
4947 line = clean_lines.elided[i]
4948 if i == linenum:
4949 remove_function_body = Match(r'^(.*)\{\s*$', line)
4950 if remove_function_body:
4951 line = remove_function_body.group(1)
4952
4953 if Search(r'\s:\s*\w+[({]', line):
4954 # A lone colon tend to indicate the start of a constructor
4955 # initializer list. It could also be a ternary operator, which
4956 # also tend to appear in constructor initializer lists as
4957 # opposed to parameter lists.
4958 return True
4959 if Search(r'\}\s*,\s*$', line):
4960 # A closing brace followed by a comma is probably the end of a
4961 # brace-initialized member in constructor initializer list.
4962 return True
4963 if Search(r'[{};]\s*$', line):
4964 # Found one of the following:
4965 # - A closing brace or semicolon, probably the end of the previous
4966 # function.
4967 # - An opening brace, probably the start of current class or namespace.
4968 #
4969 # Current line is probably not inside an initializer list since
4970 # we saw one of those things without seeing the starting colon.
4971 return False
4972
4973 # Got to the beginning of the file without seeing the start of
4974 # constructor initializer list.
4975 return False
4976
4977
erg@google.comc6671232013-10-25 21:44:03 +00004978def CheckForNonConstReference(filename, clean_lines, linenum,
4979 nesting_state, error):
4980 """Check for non-const references.
4981
4982 Separate from CheckLanguage since it scans backwards from current
4983 line, instead of scanning forward.
4984
4985 Args:
4986 filename: The name of the current file.
4987 clean_lines: A CleansedLines instance containing the file.
4988 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004989 nesting_state: A NestingState instance which maintains information about
erg@google.comc6671232013-10-25 21:44:03 +00004990 the current stack of nested blocks being parsed.
4991 error: The function to call with any errors found.
4992 """
4993 # Do nothing if there is no '&' on current line.
4994 line = clean_lines.elided[linenum]
4995 if '&' not in line:
4996 return
4997
avakulenko@google.com02af6282014-06-04 18:53:25 +00004998 # If a function is inherited, current function doesn't have much of
4999 # a choice, so any non-const references should not be blamed on
5000 # derived function.
5001 if IsDerivedFunction(clean_lines, linenum):
5002 return
5003
avakulenko@google.com554223d2014-12-04 22:00:20 +00005004 # Don't warn on out-of-line method definitions, as we would warn on the
5005 # in-line declaration, if it isn't marked with 'override'.
5006 if IsOutOfLineMethodDefinition(clean_lines, linenum):
5007 return
5008
erg@google.com2aa59982013-10-28 19:09:25 +00005009 # Long type names may be broken across multiple lines, usually in one
5010 # of these forms:
5011 # LongType
5012 # ::LongTypeContinued &identifier
5013 # LongType::
5014 # LongTypeContinued &identifier
5015 # LongType<
5016 # ...>::LongTypeContinued &identifier
5017 #
5018 # If we detected a type split across two lines, join the previous
5019 # line to current line so that we can match const references
5020 # accordingly.
erg@google.comc6671232013-10-25 21:44:03 +00005021 #
5022 # Note that this only scans back one line, since scanning back
5023 # arbitrary number of lines would be expensive. If you have a type
5024 # that spans more than 2 lines, please use a typedef.
5025 if linenum > 1:
5026 previous = None
erg@google.com2aa59982013-10-28 19:09:25 +00005027 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00005028 # previous_line\n + ::current_line
erg@google.com2aa59982013-10-28 19:09:25 +00005029 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00005030 clean_lines.elided[linenum - 1])
erg@google.com2aa59982013-10-28 19:09:25 +00005031 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00005032 # previous_line::\n + current_line
erg@google.com2aa59982013-10-28 19:09:25 +00005033 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00005034 clean_lines.elided[linenum - 1])
5035 if previous:
5036 line = previous.group(1) + line.lstrip()
erg@google.com2aa59982013-10-28 19:09:25 +00005037 else:
5038 # Check for templated parameter that is split across multiple lines
5039 endpos = line.rfind('>')
5040 if endpos > -1:
5041 (_, startline, startpos) = ReverseCloseExpression(
5042 clean_lines, linenum, endpos)
5043 if startpos > -1 and startline < linenum:
5044 # Found the matching < on an earlier line, collect all
5045 # pieces up to current line.
5046 line = ''
5047 for i in xrange(startline, linenum + 1):
5048 line += clean_lines.elided[i].strip()
erg@google.comc6671232013-10-25 21:44:03 +00005049
5050 # Check for non-const references in function parameters. A single '&' may
5051 # found in the following places:
5052 # inside expression: binary & for bitwise AND
5053 # inside expression: unary & for taking the address of something
5054 # inside declarators: reference parameter
5055 # We will exclude the first two cases by checking that we are not inside a
5056 # function body, including one that was just introduced by a trailing '{'.
erg@google.comc6671232013-10-25 21:44:03 +00005057 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.com02af6282014-06-04 18:53:25 +00005058 if (nesting_state.previous_stack_top and
5059 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
5060 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
5061 # Not at toplevel, not within a class, and not within a namespace
5062 return
5063
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005064 # Avoid initializer lists. We only need to scan back from the
5065 # current line for something that starts with ':'.
5066 #
5067 # We don't need to check the current line, since the '&' would
5068 # appear inside the second set of parentheses on the current line as
5069 # opposed to the first set.
5070 if linenum > 0:
5071 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5072 previous_line = clean_lines.elided[i]
5073 if not Search(r'[),]\s*$', previous_line):
5074 break
5075 if Match(r'^\s*:\s+\S', previous_line):
5076 return
5077
avakulenko@google.com02af6282014-06-04 18:53:25 +00005078 # Avoid preprocessors
5079 if Search(r'\\\s*$', line):
5080 return
5081
5082 # Avoid constructor initializer lists
5083 if IsInitializerList(clean_lines, linenum):
5084 return
5085
erg@google.comc6671232013-10-25 21:44:03 +00005086 # We allow non-const references in a few standard places, like functions
5087 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5088 # those function parameters.
5089 #
5090 # We also accept & in static_assert, which looks like a function but
5091 # it's actually a declaration expression.
5092 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5093 r'operator\s*[<>][<>]|'
5094 r'static_assert|COMPILE_ASSERT'
5095 r')\s*\(')
5096 if Search(whitelisted_functions, line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005097 return
erg@google.comc6671232013-10-25 21:44:03 +00005098 elif not Search(r'\S+\([^)]*$', line):
5099 # Don't see a whitelisted function on this line. Actually we
5100 # didn't see any function name on this line, so this is likely a
5101 # multi-line parameter list. Try a bit harder to catch this case.
5102 for i in xrange(2):
5103 if (linenum > i and
5104 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005105 return
erg@google.comc6671232013-10-25 21:44:03 +00005106
avakulenko@google.com02af6282014-06-04 18:53:25 +00005107 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5108 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005109 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and
5110 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005111 error(filename, linenum, 'runtime/references', 2,
5112 'Is this a non-const reference? '
5113 'If so, make const or use a pointer: ' +
5114 ReplaceAll(' *<', '<', parameter))
5115
5116
5117def CheckCasts(filename, clean_lines, linenum, error):
5118 """Various cast related checks.
5119
5120 Args:
5121 filename: The name of the current file.
5122 clean_lines: A CleansedLines instance containing the file.
5123 linenum: The number of the line to check.
5124 error: The function to call with any errors found.
5125 """
5126 line = clean_lines.elided[linenum]
5127
5128 # Check to see if they're using an conversion function cast.
5129 # I just try to capture the most common basic types, though there are more.
5130 # Parameterless conversion functions, such as bool(), are allowed as they are
5131 # probably a member operator declaration or default constructor.
5132 match = Search(
Alex Vakulenko01e47232016-05-06 13:54:15 -07005133 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
avakulenko@google.com02af6282014-06-04 18:53:25 +00005134 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5135 r'(\([^)].*)', line)
5136 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5137 if match and not expecting_function:
5138 matched_type = match.group(2)
5139
5140 # matched_new_or_template is used to silence two false positives:
5141 # - New operators
5142 # - Template arguments with function types
5143 #
5144 # For template arguments, we match on types immediately following
5145 # an opening bracket without any spaces. This is a fast way to
5146 # silence the common case where the function type is the first
5147 # template argument. False negative with less-than comparison is
5148 # avoided because those operators are usually followed by a space.
5149 #
5150 # function<double(double)> // bracket + no space = false positive
5151 # value < double(42) // bracket + space = true positive
5152 matched_new_or_template = match.group(1)
5153
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005154 # Avoid arrays by looking for brackets that come after the closing
5155 # parenthesis.
5156 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5157 return
5158
avakulenko@google.com02af6282014-06-04 18:53:25 +00005159 # Other things to ignore:
5160 # - Function pointers
5161 # - Casts to pointer types
5162 # - Placement new
5163 # - Alias declarations
5164 matched_funcptr = match.group(3)
5165 if (matched_new_or_template is None and
5166 not (matched_funcptr and
5167 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5168 matched_funcptr) or
5169 matched_funcptr.startswith('(*)'))) and
5170 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5171 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5172 error(filename, linenum, 'readability/casting', 4,
5173 'Using deprecated casting style. '
5174 'Use static_cast<%s>(...) instead' %
5175 matched_type)
5176
5177 if not expecting_function:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005178 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005179 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5180
5181 # This doesn't catch all cases. Consider (const char * const)"hello".
5182 #
5183 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5184 # compile).
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005185 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5186 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005187 pass
5188 else:
5189 # Check pointer casts for other than string constants
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005190 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5191 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005192
5193 # In addition, we look for people taking the address of a cast. This
5194 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5195 # point where you think.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005196 #
5197 # Some non-identifier character is required before the '&' for the
5198 # expression to be recognized as a cast. These are casts:
5199 # expression = &static_cast<int*>(temporary());
5200 # function(&(int*)(temporary()));
5201 #
5202 # This is not a cast:
5203 # reference_type&(int* function_param);
avakulenko@google.com02af6282014-06-04 18:53:25 +00005204 match = Search(
avakulenko@google.com554223d2014-12-04 22:00:20 +00005205 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005206 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005207 if match:
avakulenko@google.com02af6282014-06-04 18:53:25 +00005208 # Try a better error message when the & is bound to something
5209 # dereferenced by the casted pointer, as opposed to the casted
5210 # pointer itself.
5211 parenthesis_error = False
5212 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5213 if match:
5214 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5215 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5216 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5217 if x2 >= 0:
5218 extended_line = clean_lines.elided[y2][x2:]
5219 if y2 < clean_lines.NumLines() - 1:
5220 extended_line += clean_lines.elided[y2 + 1]
5221 if Match(r'\s*(?:->|\[)', extended_line):
5222 parenthesis_error = True
5223
5224 if parenthesis_error:
5225 error(filename, linenum, 'readability/casting', 4,
5226 ('Are you taking an address of something dereferenced '
5227 'from a cast? Wrapping the dereferenced expression in '
5228 'parentheses will make the binding more obvious'))
5229 else:
5230 error(filename, linenum, 'runtime/casting', 4,
5231 ('Are you taking an address of a cast? '
5232 'This is dangerous: could be a temp var. '
5233 'Take the address before doing the cast, rather than after'))
erg@google.comc6671232013-10-25 21:44:03 +00005234
erg@google.com4e00b9a2009-01-12 23:05:11 +00005235
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005236def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005237 """Checks for a C-style cast by looking for the pattern.
5238
erg@google.com4e00b9a2009-01-12 23:05:11 +00005239 Args:
5240 filename: The name of the current file.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005241 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005242 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005243 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00005244 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005245 pattern: The regular expression used to find C-style casts.
5246 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005247
5248 Returns:
5249 True if an error was emitted.
5250 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005251 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005252 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00005253 match = Search(pattern, line)
5254 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00005255 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005256
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005257 # Exclude lines with keywords that tend to look like casts
5258 context = line[0:match.start(1) - 1]
5259 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5260 return False
5261
5262 # Try expanding current context to see if we one level of
5263 # parentheses inside a macro.
5264 if linenum > 0:
5265 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5266 context = clean_lines.elided[i] + context
5267 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
erg@google.comfd5da632013-10-25 17:39:45 +00005268 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005269
erg@google.comd350fe52013-01-14 17:51:48 +00005270 # operator++(int) and operator--(int)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005271 if context.endswith(' operator++') or context.endswith(' operator--'):
erg@google.comd350fe52013-01-14 17:51:48 +00005272 return False
5273
Alex Vakulenko01e47232016-05-06 13:54:15 -07005274 # A single unnamed argument for a function tends to look like old style cast.
5275 # If we see those, don't issue warnings for deprecated casts.
erg@google.comc6671232013-10-25 21:44:03 +00005276 remainder = line[match.end(0):]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005277 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005278 remainder):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005279 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005280
5281 # At this point, all that should be left is actual casts.
5282 error(filename, linenum, 'readability/casting', 4,
5283 'Using C-style cast. Use %s<%s>(...) instead' %
5284 (cast_type, match.group(1)))
5285
erg@google.com8a95ecc2011-09-08 00:45:54 +00005286 return True
5287
erg@google.com4e00b9a2009-01-12 23:05:11 +00005288
avakulenko@google.com02af6282014-06-04 18:53:25 +00005289def ExpectingFunctionArgs(clean_lines, linenum):
5290 """Checks whether where function type arguments are expected.
5291
5292 Args:
5293 clean_lines: A CleansedLines instance containing the file.
5294 linenum: The number of the line to check.
5295
5296 Returns:
5297 True if the line at 'linenum' is inside something that expects arguments
5298 of function types.
5299 """
5300 line = clean_lines.elided[linenum]
5301 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5302 (linenum >= 2 and
5303 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5304 clean_lines.elided[linenum - 1]) or
5305 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5306 clean_lines.elided[linenum - 2]) or
5307 Search(r'\bstd::m?function\s*\<\s*$',
5308 clean_lines.elided[linenum - 1]))))
5309
5310
erg@google.com4e00b9a2009-01-12 23:05:11 +00005311_HEADERS_CONTAINING_TEMPLATES = (
5312 ('<deque>', ('deque',)),
5313 ('<functional>', ('unary_function', 'binary_function',
5314 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5315 'negate',
5316 'equal_to', 'not_equal_to', 'greater', 'less',
5317 'greater_equal', 'less_equal',
5318 'logical_and', 'logical_or', 'logical_not',
5319 'unary_negate', 'not1', 'binary_negate', 'not2',
5320 'bind1st', 'bind2nd',
5321 'pointer_to_unary_function',
5322 'pointer_to_binary_function',
5323 'ptr_fun',
5324 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5325 'mem_fun_ref_t',
5326 'const_mem_fun_t', 'const_mem_fun1_t',
5327 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5328 'mem_fun_ref',
5329 )),
5330 ('<limits>', ('numeric_limits',)),
5331 ('<list>', ('list',)),
5332 ('<map>', ('map', 'multimap',)),
lhchavez2890dff2016-07-11 19:37:29 -07005333 ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr',
5334 'unique_ptr', 'weak_ptr')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005335 ('<queue>', ('queue', 'priority_queue',)),
5336 ('<set>', ('set', 'multiset',)),
5337 ('<stack>', ('stack',)),
5338 ('<string>', ('char_traits', 'basic_string',)),
avakulenko@google.com554223d2014-12-04 22:00:20 +00005339 ('<tuple>', ('tuple',)),
lhchavez2890dff2016-07-11 19:37:29 -07005340 ('<unordered_map>', ('unordered_map', 'unordered_multimap')),
5341 ('<unordered_set>', ('unordered_set', 'unordered_multiset')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005342 ('<utility>', ('pair',)),
5343 ('<vector>', ('vector',)),
5344
5345 # gcc extensions.
5346 # Note: std::hash is their hash, ::hash is our hash
5347 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5348 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5349 ('<slist>', ('slist',)),
5350 )
5351
Alex Vakulenko01e47232016-05-06 13:54:15 -07005352_HEADERS_MAYBE_TEMPLATES = (
5353 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort',
5354 'transform',
5355 )),
lhchavez2890dff2016-07-11 19:37:29 -07005356 ('<utility>', ('forward', 'make_pair', 'move', 'swap')),
Alex Vakulenko01e47232016-05-06 13:54:15 -07005357 )
5358
erg@google.com4e00b9a2009-01-12 23:05:11 +00005359_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5360
Alex Vakulenko01e47232016-05-06 13:54:15 -07005361_re_pattern_headers_maybe_templates = []
5362for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
5363 for _template in _templates:
5364 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5365 # type::max().
5366 _re_pattern_headers_maybe_templates.append(
5367 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5368 _template,
5369 _header))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005370
Alex Vakulenko01e47232016-05-06 13:54:15 -07005371# Other scripts may reach in and modify this pattern.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005372_re_pattern_templates = []
5373for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5374 for _template in _templates:
5375 _re_pattern_templates.append(
5376 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5377 _template + '<>',
5378 _header))
5379
5380
erg@google.come35f7652009-06-19 20:52:09 +00005381def FilesBelongToSameModule(filename_cc, filename_h):
5382 """Check if these two filenames belong to the same module.
5383
5384 The concept of a 'module' here is a as follows:
5385 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5386 same 'module' if they are in the same directory.
5387 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5388 to belong to the same module here.
5389
5390 If the filename_cc contains a longer path than the filename_h, for example,
5391 '/absolute/path/to/base/sysinfo.cc', and this file would include
5392 'base/sysinfo.h', this function also produces the prefix needed to open the
5393 header. This is used by the caller of this function to more robustly open the
5394 header file. We don't have access to the real include paths in this context,
5395 so we need this guesswork here.
5396
5397 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5398 according to this implementation. Because of this, this function gives
5399 some false positives. This should be sufficiently rare in practice.
5400
5401 Args:
5402 filename_cc: is the path for the .cc file
5403 filename_h: is the path for the header path
5404
5405 Returns:
5406 Tuple with a bool and a string:
5407 bool: True if filename_cc and filename_h belong to the same module.
5408 string: the additional prefix needed to open the header file.
5409 """
5410
Alex Vakulenko01e47232016-05-06 13:54:15 -07005411 fileinfo = FileInfo(filename_cc)
5412 if not fileinfo.IsSource():
erg@google.come35f7652009-06-19 20:52:09 +00005413 return (False, '')
Alex Vakulenko01e47232016-05-06 13:54:15 -07005414 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5415 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5416 if matched_test_suffix:
5417 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
erg@google.come35f7652009-06-19 20:52:09 +00005418 filename_cc = filename_cc.replace('/public/', '/')
5419 filename_cc = filename_cc.replace('/internal/', '/')
5420
5421 if not filename_h.endswith('.h'):
5422 return (False, '')
5423 filename_h = filename_h[:-len('.h')]
5424 if filename_h.endswith('-inl'):
5425 filename_h = filename_h[:-len('-inl')]
5426 filename_h = filename_h.replace('/public/', '/')
5427 filename_h = filename_h.replace('/internal/', '/')
5428
5429 files_belong_to_same_module = filename_cc.endswith(filename_h)
5430 common_path = ''
5431 if files_belong_to_same_module:
5432 common_path = filename_cc[:-len(filename_h)]
5433 return files_belong_to_same_module, common_path
5434
5435
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005436def UpdateIncludeState(filename, include_dict, io=codecs):
5437 """Fill up the include_dict with new includes found from the file.
erg@google.come35f7652009-06-19 20:52:09 +00005438
5439 Args:
5440 filename: the name of the header to read.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005441 include_dict: a dictionary in which the headers are inserted.
erg@google.come35f7652009-06-19 20:52:09 +00005442 io: The io factory to use to read the file. Provided for testability.
5443
5444 Returns:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005445 True if a header was successfully added. False otherwise.
erg@google.come35f7652009-06-19 20:52:09 +00005446 """
5447 headerfile = None
5448 try:
5449 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5450 except IOError:
5451 return False
5452 linenum = 0
5453 for line in headerfile:
5454 linenum += 1
5455 clean_line = CleanseComments(line)
5456 match = _RE_PATTERN_INCLUDE.search(clean_line)
5457 if match:
5458 include = match.group(2)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005459 include_dict.setdefault(include, linenum)
erg@google.come35f7652009-06-19 20:52:09 +00005460 return True
5461
5462
5463def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5464 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005465 """Reports for missing stl includes.
5466
5467 This function will output warnings to make sure you are including the headers
5468 necessary for the stl containers and functions that you use. We only give one
5469 reason to include a header. For example, if you use both equal_to<> and
5470 less<> in a .h file, only one (the latter in the file) of these will be
5471 reported as a reason to include the <functional>.
5472
erg@google.com4e00b9a2009-01-12 23:05:11 +00005473 Args:
5474 filename: The name of the current file.
5475 clean_lines: A CleansedLines instance containing the file.
5476 include_state: An _IncludeState instance.
5477 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00005478 io: The IO factory to use to read the header file. Provided for unittest
5479 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005480 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00005481 required = {} # A map of header name to linenumber and the template entity.
5482 # Example of required: { '<functional>': (1219, 'less<>') }
5483
5484 for linenum in xrange(clean_lines.NumLines()):
5485 line = clean_lines.elided[linenum]
5486 if not line or line[0] == '#':
5487 continue
5488
5489 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005490 matched = _RE_PATTERN_STRING.search(line)
5491 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00005492 # Don't warn about strings in non-STL namespaces:
5493 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005494 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00005495 if prefix.endswith('std::') or not prefix.endswith('::'):
5496 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00005497
Alex Vakulenko01e47232016-05-06 13:54:15 -07005498 for pattern, template, header in _re_pattern_headers_maybe_templates:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005499 if pattern.search(line):
5500 required[header] = (linenum, template)
5501
5502 # The following function is just a speed up, no semantics are changed.
5503 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5504 continue
5505
5506 for pattern, template, header in _re_pattern_templates:
lhchavez3ae81f12016-07-11 19:00:34 -07005507 matched = pattern.search(line)
5508 if matched:
5509 # Don't warn about IWYU in non-STL namespaces:
5510 # (We check only the first match per line; good enough.)
5511 prefix = line[:matched.start()]
5512 if prefix.endswith('std::') or not prefix.endswith('::'):
5513 required[header] = (linenum, template)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005514
erg@google.come35f7652009-06-19 20:52:09 +00005515 # The policy is that if you #include something in foo.h you don't need to
5516 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005517 # Let's flatten the include_state include_list and copy it into a dictionary.
5518 include_dict = dict([item for sublist in include_state.include_list
5519 for item in sublist])
erg@google.come35f7652009-06-19 20:52:09 +00005520
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005521 # Did we find the header for this file (if any) and successfully load it?
erg@google.come35f7652009-06-19 20:52:09 +00005522 header_found = False
5523
5524 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00005525 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00005526
5527 # For Emacs's flymake.
5528 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5529 # by flymake and that file name might end with '_flymake.cc'. In that case,
5530 # restore original file name here so that the corresponding header file can be
5531 # found.
5532 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5533 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00005534 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00005535
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005536 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.come35f7652009-06-19 20:52:09 +00005537 # the keys.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005538 header_keys = include_dict.keys()
erg@google.com8a95ecc2011-09-08 00:45:54 +00005539 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00005540 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5541 fullpath = common_path + header
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005542 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.come35f7652009-06-19 20:52:09 +00005543 header_found = True
5544
5545 # If we can't find the header file for a .cc, assume it's because we don't
5546 # know where to look. In that case we'll give up as we're not sure they
5547 # didn't include it in the .h file.
5548 # TODO(unknown): Do a better job of finding .h files so we are confident that
5549 # not having the .h file means there isn't one.
5550 if filename.endswith('.cc') and not header_found:
5551 return
5552
erg@google.com4e00b9a2009-01-12 23:05:11 +00005553 # All the lines have been processed, report the errors found.
5554 for required_header_unstripped in required:
5555 template = required[required_header_unstripped][1]
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005556 if required_header_unstripped.strip('<>"') not in include_dict:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005557 error(filename, required[required_header_unstripped][0],
5558 'build/include_what_you_use', 4,
5559 'Add #include ' + required_header_unstripped + ' for ' + template)
5560
5561
erg@google.com8a95ecc2011-09-08 00:45:54 +00005562_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5563
5564
5565def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5566 """Check that make_pair's template arguments are deduced.
5567
avakulenko@google.com02af6282014-06-04 18:53:25 +00005568 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
erg@google.com8a95ecc2011-09-08 00:45:54 +00005569 specified explicitly, and such use isn't intended in any case.
5570
5571 Args:
5572 filename: The name of the current file.
5573 clean_lines: A CleansedLines instance containing the file.
5574 linenum: The number of the line to check.
5575 error: The function to call with any errors found.
5576 """
erg@google.com2aa59982013-10-28 19:09:25 +00005577 line = clean_lines.elided[linenum]
erg@google.com8a95ecc2011-09-08 00:45:54 +00005578 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5579 if match:
5580 error(filename, linenum, 'build/explicit_make_pair',
5581 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00005582 'For C++11-compatibility, omit template arguments from make_pair'
5583 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005584
5585
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005586def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5587 """Check if line contains a redundant "virtual" function-specifier.
5588
5589 Args:
5590 filename: The name of the current file.
5591 clean_lines: A CleansedLines instance containing the file.
5592 linenum: The number of the line to check.
5593 error: The function to call with any errors found.
5594 """
5595 # Look for "virtual" on current line.
5596 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005597 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005598 if not virtual: return
5599
avakulenko@google.com554223d2014-12-04 22:00:20 +00005600 # Ignore "virtual" keywords that are near access-specifiers. These
5601 # are only used in class base-specifier and do not apply to member
5602 # functions.
5603 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or
5604 Match(r'^\s+(public|protected|private)\b', virtual.group(3))):
5605 return
5606
5607 # Ignore the "virtual" keyword from virtual base classes. Usually
5608 # there is a column on the same line in these cases (virtual base
5609 # classes are rare in google3 because multiple inheritance is rare).
5610 if Match(r'^.*[^:]:[^:].*$', line): return
5611
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005612 # Look for the next opening parenthesis. This is the start of the
5613 # parameter list (possibly on the next line shortly after virtual).
5614 # TODO(unknown): doesn't work if there are virtual functions with
5615 # decltype() or other things that use parentheses, but csearch suggests
5616 # that this is rare.
5617 end_col = -1
5618 end_line = -1
avakulenko@google.com554223d2014-12-04 22:00:20 +00005619 start_col = len(virtual.group(2))
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005620 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5621 line = clean_lines.elided[start_line][start_col:]
5622 parameter_list = Match(r'^([^(]*)\(', line)
5623 if parameter_list:
5624 # Match parentheses to find the end of the parameter list
5625 (_, end_line, end_col) = CloseExpression(
5626 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5627 break
5628 start_col = 0
5629
5630 if end_col < 0:
5631 return # Couldn't find end of parameter list, give up
5632
5633 # Look for "override" or "final" after the parameter list
5634 # (possibly on the next few lines).
5635 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5636 line = clean_lines.elided[i][end_col:]
5637 match = Search(r'\b(override|final)\b', line)
5638 if match:
5639 error(filename, linenum, 'readability/inheritance', 4,
5640 ('"virtual" is redundant since function is '
5641 'already declared as "%s"' % match.group(1)))
5642
5643 # Set end_col to check whole lines after we are done with the
5644 # first line.
5645 end_col = 0
5646 if Search(r'[^\w]\s*$', line):
5647 break
5648
5649
5650def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5651 """Check if line contains a redundant "override" or "final" virt-specifier.
5652
5653 Args:
5654 filename: The name of the current file.
5655 clean_lines: A CleansedLines instance containing the file.
5656 linenum: The number of the line to check.
5657 error: The function to call with any errors found.
5658 """
avakulenko@google.com554223d2014-12-04 22:00:20 +00005659 # Look for closing parenthesis nearby. We need one to confirm where
5660 # the declarator ends and where the virt-specifier starts to avoid
5661 # false positives.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005662 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005663 declarator_end = line.rfind(')')
5664 if declarator_end >= 0:
5665 fragment = line[declarator_end:]
5666 else:
5667 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0:
5668 fragment = line
5669 else:
5670 return
5671
5672 # Check that at most one of "override" or "final" is present, not both
5673 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005674 error(filename, linenum, 'readability/inheritance', 4,
5675 ('"override" is redundant since function is '
5676 'already declared as "final"'))
5677
5678
5679
5680
5681# Returns true if we are at a new block, and it is directly
5682# inside of a namespace.
5683def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5684 """Checks that the new block is directly in a namespace.
5685
5686 Args:
5687 nesting_state: The _NestingState object that contains info about our state.
5688 is_forward_declaration: If the class is a forward declared class.
5689 Returns:
5690 Whether or not the new block is directly in a namespace.
5691 """
5692 if is_forward_declaration:
5693 if len(nesting_state.stack) >= 1 and (
5694 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5695 return True
5696 else:
5697 return False
5698
5699 return (len(nesting_state.stack) > 1 and
5700 nesting_state.stack[-1].check_namespace_indentation and
5701 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5702
5703
5704def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5705 raw_lines_no_comments, linenum):
5706 """This method determines if we should apply our namespace indentation check.
5707
5708 Args:
5709 nesting_state: The current nesting state.
5710 is_namespace_indent_item: If we just put a new class on the stack, True.
5711 If the top of the stack is not a class, or we did not recently
5712 add the class, False.
5713 raw_lines_no_comments: The lines without the comments.
5714 linenum: The current line number we are processing.
5715
5716 Returns:
5717 True if we should apply our namespace indentation check. Currently, it
5718 only works for classes and namespaces inside of a namespace.
5719 """
5720
5721 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5722 linenum)
5723
5724 if not (is_namespace_indent_item or is_forward_declaration):
5725 return False
5726
5727 # If we are in a macro, we do not want to check the namespace indentation.
5728 if IsMacroDefinition(raw_lines_no_comments, linenum):
5729 return False
5730
5731 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5732
5733
5734# Call this method if the line is directly inside of a namespace.
5735# If the line above is blank (excluding comments) or the start of
5736# an inner namespace, it cannot be indented.
5737def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5738 error):
5739 line = raw_lines_no_comments[linenum]
5740 if Match(r'^\s+', line):
5741 error(filename, linenum, 'runtime/indentation_namespace', 4,
5742 'Do not indent within a namespace')
erg@google.com8a95ecc2011-09-08 00:45:54 +00005743
5744
erg@google.comd350fe52013-01-14 17:51:48 +00005745def ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005746 include_state, function_state, nesting_state, error,
5747 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005748 """Processes a single line in the file.
5749
5750 Args:
5751 filename: Filename of the file that is being processed.
5752 file_extension: The extension (dot not included) of the file.
5753 clean_lines: An array of strings, each representing a line of the file,
5754 with comments stripped.
5755 line: Number of line being processed.
5756 include_state: An _IncludeState instance in which the headers are inserted.
5757 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005758 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00005759 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005760 error: A callable to which errors are reported, which takes 4 arguments:
5761 filename, line number, error level, and message
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005762 extra_check_functions: An array of additional check functions that will be
5763 run on each source line. Each function takes 4
5764 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005765 """
5766 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00005767 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005768 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005769 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5770 error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005771 if nesting_state.InAsmBlock(): return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005772 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005773 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005774 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005775 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
erg@google.comfd5da632013-10-25 17:39:45 +00005776 nesting_state, error)
erg@google.comc6671232013-10-25 21:44:03 +00005777 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005778 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00005779 nesting_state, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005780 CheckVlogArguments(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005781 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00005782 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005783 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005784 CheckRedundantVirtual(filename, clean_lines, line, error)
5785 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005786 for check_fn in extra_check_functions:
5787 check_fn(filename, clean_lines, line, error)
erg@google.com7430eef2014-07-28 22:33:46 +00005788
avakulenko@google.com02af6282014-06-04 18:53:25 +00005789def FlagCxx11Features(filename, clean_lines, linenum, error):
5790 """Flag those c++11 features that we only allow in certain places.
5791
5792 Args:
5793 filename: The name of the current file.
5794 clean_lines: A CleansedLines instance containing the file.
5795 linenum: The number of the line to check.
5796 error: The function to call with any errors found.
5797 """
5798 line = clean_lines.elided[linenum]
5799
avakulenko@google.com02af6282014-06-04 18:53:25 +00005800 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005801
5802 # Flag unapproved C++ TR1 headers.
5803 if include and include.group(1).startswith('tr1/'):
5804 error(filename, linenum, 'build/c++tr1', 5,
5805 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1))
5806
5807 # Flag unapproved C++11 headers.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005808 if include and include.group(1) in ('cfenv',
5809 'condition_variable',
5810 'fenv.h',
5811 'future',
5812 'mutex',
5813 'thread',
5814 'chrono',
5815 'ratio',
5816 'regex',
5817 'system_error',
5818 ):
5819 error(filename, linenum, 'build/c++11', 5,
5820 ('<%s> is an unapproved C++11 header.') % include.group(1))
5821
5822 # The only place where we need to worry about C++11 keywords and library
5823 # features in preprocessor directives is in macro definitions.
5824 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5825
5826 # These are classes and free functions. The classes are always
5827 # mentioned as std::*, but we only catch the free functions if
5828 # they're not found by ADL. They're alphabetical by header.
5829 for top_name in (
5830 # type_traits
5831 'alignment_of',
5832 'aligned_union',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005833 ):
5834 if Search(r'\bstd::%s\b' % top_name, line):
5835 error(filename, linenum, 'build/c++11', 5,
5836 ('std::%s is an unapproved C++11 class or function. Send c-style '
5837 'an example of where it would make your code more readable, and '
5838 'they may let you use it.') % top_name)
5839
5840
Alex Vakulenko01e47232016-05-06 13:54:15 -07005841def FlagCxx14Features(filename, clean_lines, linenum, error):
5842 """Flag those C++14 features that we restrict.
5843
5844 Args:
5845 filename: The name of the current file.
5846 clean_lines: A CleansedLines instance containing the file.
5847 linenum: The number of the line to check.
5848 error: The function to call with any errors found.
5849 """
5850 line = clean_lines.elided[linenum]
5851
5852 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5853
5854 # Flag unapproved C++14 headers.
5855 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):
5856 error(filename, linenum, 'build/c++14', 5,
5857 ('<%s> is an unapproved C++14 header.') % include.group(1))
5858
5859
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005860def ProcessFileData(filename, file_extension, lines, error,
5861 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005862 """Performs lint checks and reports any errors to the given error function.
5863
5864 Args:
5865 filename: Filename of the file that is being processed.
5866 file_extension: The extension (dot not included) of the file.
5867 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00005868 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005869 error: A callable to which errors are reported, which takes 4 arguments:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005870 filename, line number, error level, and message
5871 extra_check_functions: An array of additional check functions that will be
5872 run on each source line. Each function takes 4
5873 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005874 """
5875 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5876 ['// marker so line numbers end in a known way'])
5877
5878 include_state = _IncludeState()
5879 function_state = _FunctionState()
avakulenko@google.com02af6282014-06-04 18:53:25 +00005880 nesting_state = NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005881
erg+personal@google.com05189642010-04-30 20:43:03 +00005882 ResetNolintSuppressions()
5883
erg@google.com4e00b9a2009-01-12 23:05:11 +00005884 CheckForCopyright(filename, lines, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005885 ProcessGlobalSuppresions(lines)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005886 RemoveMultiLineComments(filename, lines, error)
5887 clean_lines = CleansedLines(lines)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005888
LukeCz7197a242016-09-24 13:27:35 -05005889 if IsHeaderExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005890 CheckForHeaderGuard(filename, clean_lines, error)
5891
erg@google.com4e00b9a2009-01-12 23:05:11 +00005892 for line in xrange(clean_lines.NumLines()):
5893 ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005894 include_state, function_state, nesting_state, error,
5895 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005896 FlagCxx11Features(filename, clean_lines, line, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005897 nesting_state.CheckCompletedBlocks(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005898
5899 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005900
avakulenko@google.com554223d2014-12-04 22:00:20 +00005901 # Check that the .cc file has included its header if it exists.
Alex Vakulenko01e47232016-05-06 13:54:15 -07005902 if _IsSourceExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005903 CheckHeaderFileIncluded(filename, include_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005904
5905 # We check here rather than inside ProcessLine so that we see raw
5906 # lines rather than "cleaned" lines.
erg@google.com2aa59982013-10-28 19:09:25 +00005907 CheckForBadCharacters(filename, lines, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005908
5909 CheckForNewlineAtEOF(filename, lines, error)
5910
erg@google.com7430eef2014-07-28 22:33:46 +00005911def ProcessConfigOverrides(filename):
5912 """ Loads the configuration files and processes the config overrides.
5913
5914 Args:
5915 filename: The name of the file being processed by the linter.
5916
5917 Returns:
5918 False if the current |filename| should not be processed further.
5919 """
5920
5921 abs_filename = os.path.abspath(filename)
5922 cfg_filters = []
5923 keep_looking = True
5924 while keep_looking:
5925 abs_path, base_name = os.path.split(abs_filename)
5926 if not base_name:
5927 break # Reached the root directory.
5928
5929 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5930 abs_filename = abs_path
5931 if not os.path.isfile(cfg_file):
5932 continue
5933
5934 try:
5935 with open(cfg_file) as file_handle:
5936 for line in file_handle:
5937 line, _, _ = line.partition('#') # Remove comments.
5938 if not line.strip():
5939 continue
5940
5941 name, _, val = line.partition('=')
5942 name = name.strip()
5943 val = val.strip()
5944 if name == 'set noparent':
5945 keep_looking = False
5946 elif name == 'filter':
5947 cfg_filters.append(val)
5948 elif name == 'exclude_files':
5949 # When matching exclude_files pattern, use the base_name of
5950 # the current file name or the directory name we are processing.
5951 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5952 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5953 # file's "exclude_files" filter is meant to be checked against "bar"
5954 # and not "baz" nor "bar/baz.cc".
5955 if base_name:
5956 pattern = re.compile(val)
5957 if pattern.match(base_name):
5958 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5959 'File path component "%s" matches '
5960 'pattern "%s"\n' %
5961 (filename, cfg_file, base_name, val))
5962 return False
avakulenko@google.com310681b2014-08-22 19:38:55 +00005963 elif name == 'linelength':
5964 global _line_length
5965 try:
5966 _line_length = int(val)
5967 except ValueError:
5968 sys.stderr.write('Line length must be numeric.')
Fabian Guera2322e4f2016-05-01 17:36:30 +02005969 elif name == 'root':
5970 global _root
Igor Murashkin8a87a462017-11-09 13:48:29 -08005971 # root directories are specified relative to CPPLINT.cfg dir.
5972 _root = os.path.join(os.path.dirname(cfg_file), val)
LukeCz7197a242016-09-24 13:27:35 -05005973 elif name == 'headers':
5974 ProcessHppHeadersOption(val)
erg@google.com7430eef2014-07-28 22:33:46 +00005975 else:
5976 sys.stderr.write(
5977 'Invalid configuration option (%s) in file %s\n' %
5978 (name, cfg_file))
5979
5980 except IOError:
5981 sys.stderr.write(
5982 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5983 keep_looking = False
5984
5985 # Apply all the accumulated filters in reverse order (top-level directory
5986 # config options having the least priority).
5987 for filter in reversed(cfg_filters):
5988 _AddFilters(filter)
5989
5990 return True
5991
avakulenko@google.com02af6282014-06-04 18:53:25 +00005992
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005993def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005994 """Does google-lint on a single file.
5995
5996 Args:
5997 filename: The name of the file to parse.
5998
5999 vlevel: The level of errors to report. Every error of confidence
6000 >= verbose_level will be reported. 0 is a good default.
avakulenko@google.com4b957b22014-06-04 22:48:14 +00006001
6002 extra_check_functions: An array of additional check functions that will be
6003 run on each source line. Each function takes 4
6004 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00006005 """
6006
6007 _SetVerboseLevel(vlevel)
erg@google.com7430eef2014-07-28 22:33:46 +00006008 _BackupFilters()
6009
6010 if not ProcessConfigOverrides(filename):
6011 _RestoreFilters()
6012 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00006013
avakulenko@google.com02af6282014-06-04 18:53:25 +00006014 lf_lines = []
6015 crlf_lines = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00006016 try:
6017 # Support the UNIX convention of using "-" for stdin. Note that
6018 # we are not opening the file with universal newline support
6019 # (which codecs doesn't support anyway), so the resulting lines do
6020 # contain trailing '\r' characters if we are reading a file that
6021 # has CRLF endings.
6022 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.com02af6282014-06-04 18:53:25 +00006023 # below.
erg@google.com4e00b9a2009-01-12 23:05:11 +00006024 if filename == '-':
6025 lines = codecs.StreamReaderWriter(sys.stdin,
6026 codecs.getreader('utf8'),
6027 codecs.getwriter('utf8'),
6028 'replace').read().split('\n')
6029 else:
6030 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
6031
erg@google.com4e00b9a2009-01-12 23:05:11 +00006032 # Remove trailing '\r'.
avakulenko@google.com02af6282014-06-04 18:53:25 +00006033 # The -1 accounts for the extra trailing blank line we get from split()
6034 for linenum in range(len(lines) - 1):
erg@google.com4e00b9a2009-01-12 23:05:11 +00006035 if lines[linenum].endswith('\r'):
6036 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.com02af6282014-06-04 18:53:25 +00006037 crlf_lines.append(linenum + 1)
6038 else:
6039 lf_lines.append(linenum + 1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006040
6041 except IOError:
6042 sys.stderr.write(
6043 "Skipping input '%s': Can't open for reading\n" % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006044 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006045 return
6046
6047 # Note, if no dot is found, this will give the entire filename as the ext.
6048 file_extension = filename[filename.rfind('.') + 1:]
6049
6050 # When reading from stdin, the extension is unknown, so no cpplint tests
6051 # should rely on the extension.
erg@google.com19680272013-12-16 22:48:54 +00006052 if filename != '-' and file_extension not in _valid_extensions:
erg@google.com2aa59982013-10-28 19:09:25 +00006053 sys.stderr.write('Ignoring %s; not a valid file name '
erg@google.com19680272013-12-16 22:48:54 +00006054 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006055 else:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00006056 ProcessFileData(filename, file_extension, lines, Error,
6057 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00006058
6059 # If end-of-line sequences are a mix of LF and CR-LF, issue
6060 # warnings on the lines with CR.
6061 #
6062 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
6063 # since critique can handle these just fine, and the style guide
6064 # doesn't dictate a particular end of line sequence.
6065 #
6066 # We can't depend on os.linesep to determine what the desired
6067 # end-of-line sequence should be, since that will return the
6068 # server-side end-of-line sequence.
6069 if lf_lines and crlf_lines:
6070 # Warn on every line with CR. An alternative approach might be to
6071 # check whether the file is mostly CRLF or just LF, and warn on the
6072 # minority, we bias toward LF here since most tools prefer LF.
6073 for linenum in crlf_lines:
6074 Error(filename, linenum, 'whitespace/newline', 1,
6075 'Unexpected \\r (^M) found; better to use only \\n')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006076
LukeCze09f4782016-09-28 19:13:37 -05006077 sys.stdout.write('Done processing %s\n' % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006078 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006079
6080
6081def PrintUsage(message):
6082 """Prints a brief usage string and exits, optionally with an error message.
6083
6084 Args:
6085 message: The optional error message.
6086 """
6087 sys.stderr.write(_USAGE)
6088 if message:
6089 sys.exit('\nFATAL ERROR: ' + message)
6090 else:
6091 sys.exit(1)
6092
6093
6094def PrintCategories():
6095 """Prints a list of all the error-categories used by error messages.
6096
6097 These are the categories used to filter messages via --filter.
6098 """
erg+personal@google.com05189642010-04-30 20:43:03 +00006099 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006100 sys.exit(0)
6101
6102
6103def ParseArguments(args):
6104 """Parses the command line arguments.
6105
6106 This may set the output format and verbosity level as side-effects.
6107
6108 Args:
6109 args: The command line arguments:
6110
6111 Returns:
6112 The list of filenames to lint.
6113 """
6114 try:
6115 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00006116 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00006117 'filter=',
erg@google.comab53edf2013-11-05 22:23:37 +00006118 'root=',
erg@google.com19680272013-12-16 22:48:54 +00006119 'linelength=',
LukeCz7197a242016-09-24 13:27:35 -05006120 'extensions=',
6121 'headers='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00006122 except getopt.GetoptError:
6123 PrintUsage('Invalid arguments.')
6124
6125 verbosity = _VerboseLevel()
6126 output_format = _OutputFormat()
6127 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00006128 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00006129
6130 for (opt, val) in opts:
6131 if opt == '--help':
6132 PrintUsage(None)
6133 elif opt == '--output':
erg@google.comc6671232013-10-25 21:44:03 +00006134 if val not in ('emacs', 'vs7', 'eclipse'):
erg@google.com02c27fd2013-05-28 21:34:34 +00006135 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006136 output_format = val
6137 elif opt == '--verbose':
6138 verbosity = int(val)
6139 elif opt == '--filter':
6140 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00006141 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00006142 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00006143 elif opt == '--counting':
6144 if val not in ('total', 'toplevel', 'detailed'):
6145 PrintUsage('Valid counting options are total, toplevel, and detailed')
6146 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00006147 elif opt == '--root':
6148 global _root
6149 _root = val
erg@google.comab53edf2013-11-05 22:23:37 +00006150 elif opt == '--linelength':
6151 global _line_length
6152 try:
6153 _line_length = int(val)
6154 except ValueError:
6155 PrintUsage('Line length must be digits.')
erg@google.com19680272013-12-16 22:48:54 +00006156 elif opt == '--extensions':
6157 global _valid_extensions
6158 try:
6159 _valid_extensions = set(val.split(','))
6160 except ValueError:
6161 PrintUsage('Extensions must be comma seperated list.')
LukeCz7197a242016-09-24 13:27:35 -05006162 elif opt == '--headers':
6163 ProcessHppHeadersOption(val)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006164
6165 if not filenames:
6166 PrintUsage('No files were specified.')
6167
6168 _SetOutputFormat(output_format)
6169 _SetVerboseLevel(verbosity)
6170 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00006171 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006172
6173 return filenames
6174
6175
6176def main():
6177 filenames = ParseArguments(sys.argv[1:])
6178
6179 # Change stderr to write with replacement characters so we don't die
6180 # if we try to print something containing non-ASCII characters.
6181 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6182 codecs.getreader('utf8'),
6183 codecs.getwriter('utf8'),
6184 'replace')
6185
erg@google.coma868d2d2009-10-09 21:18:45 +00006186 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006187 for filename in filenames:
6188 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00006189 _cpplint_state.PrintErrorCounts()
6190
erg@google.com4e00b9a2009-01-12 23:05:11 +00006191 sys.exit(_cpplint_state.error_count > 0)
6192
6193
6194if __name__ == '__main__':
6195 main()