blob: 2b6c5c8e744b8e308784245d762d9550ca0e1c0f [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]
59 [--linelength=digits]
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:
avakulenko@google.com02af6282014-06-04 18:53:25 +0000117 Assuming that src/.git exists, the header guard CPP variables for
erg@google.com4d70a882013-04-16 21:06:32 +0000118 src/chrome/browser/ui/browser.h are:
119
120 No flag => CHROME_BROWSER_UI_BROWSER_H_
121 --root=chrome => BROWSER_UI_BROWSER_H_
122 --root=chrome/browser => UI_BROWSER_H_
erg@google.comab53edf2013-11-05 22:23:37 +0000123
124 linelength=digits
125 This is the allowed line length for the project. The default value is
126 80 characters.
127
128 Examples:
129 --linelength=120
erg@google.com19680272013-12-16 22:48:54 +0000130
131 extensions=extension,extension,...
132 The allowed file extensions that cpplint will check
133
134 Examples:
135 --extensions=hpp,cpp
erg@google.com7430eef2014-07-28 22:33:46 +0000136
137 cpplint.py supports per-directory configurations specified in CPPLINT.cfg
138 files. CPPLINT.cfg file can contain a number of key=value pairs.
139 Currently the following options are supported:
140
141 set noparent
142 filter=+filter1,-filter2,...
143 exclude_files=regex
avakulenko@google.com310681b2014-08-22 19:38:55 +0000144 linelength=80
Fabian Guera2322e4f2016-05-01 17:36:30 +0200145 root=subdir
erg@google.com7430eef2014-07-28 22:33:46 +0000146
147 "set noparent" option prevents cpplint from traversing directory tree
148 upwards looking for more .cfg files in parent directories. This option
149 is usually placed in the top-level project directory.
150
151 The "filter" option is similar in function to --filter flag. It specifies
152 message filters in addition to the |_DEFAULT_FILTERS| and those specified
153 through --filter command-line flag.
154
155 "exclude_files" allows to specify a regular expression to be matched against
156 a file name. If the expression matches, the file is skipped and not run
157 through liner.
158
avakulenko@google.com310681b2014-08-22 19:38:55 +0000159 "linelength" allows to specify the allowed line length for the project.
160
Fabian Guera2322e4f2016-05-01 17:36:30 +0200161 The "root" option is similar in function to the --root flag (see example
162 above).
163
erg@google.com7430eef2014-07-28 22:33:46 +0000164 CPPLINT.cfg has an effect on files in the same directory and all
165 sub-directories, unless overridden by a nested configuration file.
166
167 Example file:
168 filter=-build/include_order,+build/include_alpha
169 exclude_files=.*\.cc
170
171 The above example disables build/include_order warning and enables
172 build/include_alpha as well as excludes all .cc from being
173 processed by linter, in the current directory (where the .cfg
174 file is located) and all sub-directories.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000175"""
176
177# We categorize each error message we print. Here are the categories.
178# We want an explicit list so we can list them all in cpplint --filter=.
179# If you add a new error message with a new category, add it to the list
180# here! cpplint_unittest.py should tell you if you forget to do this.
erg+personal@google.com05189642010-04-30 20:43:03 +0000181_ERROR_CATEGORIES = [
avakulenko@google.com554223d2014-12-04 22:00:20 +0000182 'build/class',
183 'build/c++11',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700184 'build/c++14',
185 'build/c++tr1',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000186 'build/deprecated',
187 'build/endif_comment',
188 'build/explicit_make_pair',
189 'build/forward_decl',
190 'build/header_guard',
191 'build/include',
192 'build/include_alpha',
193 'build/include_order',
194 'build/include_what_you_use',
195 'build/namespaces',
196 'build/printf_format',
197 'build/storage_class',
198 'legal/copyright',
199 'readability/alt_tokens',
200 'readability/braces',
201 'readability/casting',
202 'readability/check',
203 'readability/constructors',
204 'readability/fn_size',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000205 'readability/inheritance',
206 'readability/multiline_comment',
207 'readability/multiline_string',
208 'readability/namespace',
209 'readability/nolint',
210 'readability/nul',
211 'readability/strings',
212 'readability/todo',
213 'readability/utf8',
214 'runtime/arrays',
215 'runtime/casting',
216 'runtime/explicit',
217 'runtime/int',
218 'runtime/init',
219 'runtime/invalid_increment',
220 'runtime/member_string_references',
221 'runtime/memset',
222 'runtime/indentation_namespace',
223 'runtime/operator',
224 'runtime/printf',
225 'runtime/printf_format',
226 'runtime/references',
227 'runtime/string',
228 'runtime/threadsafe_fn',
229 'runtime/vlog',
230 'whitespace/blank_line',
231 'whitespace/braces',
232 'whitespace/comma',
233 'whitespace/comments',
234 'whitespace/empty_conditional_body',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700235 'whitespace/empty_if_body',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000236 'whitespace/empty_loop_body',
237 'whitespace/end_of_line',
238 'whitespace/ending_newline',
239 'whitespace/forcolon',
240 'whitespace/indent',
241 'whitespace/line_length',
242 'whitespace/newline',
243 'whitespace/operators',
244 'whitespace/parens',
245 'whitespace/semicolon',
246 'whitespace/tab',
247 'whitespace/todo',
248 ]
249
250# These error categories are no longer enforced by cpplint, but for backwards-
251# compatibility they may still appear in NOLINT comments.
252_LEGACY_ERROR_CATEGORIES = [
253 'readability/streams',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700254 'readability/function',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000255 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000256
avakulenko@google.com02af6282014-06-04 18:53:25 +0000257# The default state of the category filter. This is overridden by the --filter=
erg@google.come35f7652009-06-19 20:52:09 +0000258# flag. By default all errors are on, so only add here categories that should be
259# off by default (i.e., categories that must be enabled by the --filter= flags).
260# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000261_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000262
Alex Vakulenko01e47232016-05-06 13:54:15 -0700263# The default list of categories suppressed for C (not C++) files.
264_DEFAULT_C_SUPPRESSED_CATEGORIES = [
265 'readability/casting',
266 ]
267
268# The default list of categories suppressed for Linux Kernel files.
269_DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [
270 'whitespace/tab',
271 ]
272
erg@google.com4e00b9a2009-01-12 23:05:11 +0000273# We used to check for high-bit characters, but after much discussion we
274# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000275# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000276
erg@google.comfd5da632013-10-25 17:39:45 +0000277# C++ headers
erg@google.com4e00b9a2009-01-12 23:05:11 +0000278_CPP_HEADERS = frozenset([
erg@google.comfd5da632013-10-25 17:39:45 +0000279 # Legacy
280 'algobase.h',
281 'algo.h',
282 'alloc.h',
283 'builtinbuf.h',
284 'bvector.h',
285 'complex.h',
286 'defalloc.h',
287 'deque.h',
288 'editbuf.h',
289 'fstream.h',
290 'function.h',
291 'hash_map',
292 'hash_map.h',
293 'hash_set',
294 'hash_set.h',
295 'hashtable.h',
296 'heap.h',
297 'indstream.h',
298 'iomanip.h',
299 'iostream.h',
300 'istream.h',
301 'iterator.h',
302 'list.h',
303 'map.h',
304 'multimap.h',
305 'multiset.h',
306 'ostream.h',
307 'pair.h',
308 'parsestream.h',
309 'pfstream.h',
310 'procbuf.h',
311 'pthread_alloc',
312 'pthread_alloc.h',
313 'rope',
314 'rope.h',
315 'ropeimpl.h',
316 'set.h',
317 'slist',
318 'slist.h',
319 'stack.h',
320 'stdiostream.h',
321 'stl_alloc.h',
322 'stl_relops.h',
323 'streambuf.h',
324 'stream.h',
325 'strfile.h',
326 'strstream.h',
327 'tempbuf.h',
328 'tree.h',
329 'type_traits.h',
330 'vector.h',
331 # 17.6.1.2 C++ library headers
332 'algorithm',
333 'array',
334 'atomic',
335 'bitset',
336 'chrono',
337 'codecvt',
338 'complex',
339 'condition_variable',
340 'deque',
341 'exception',
342 'forward_list',
343 'fstream',
344 'functional',
345 'future',
346 'initializer_list',
347 'iomanip',
348 'ios',
349 'iosfwd',
350 'iostream',
351 'istream',
352 'iterator',
353 'limits',
354 'list',
355 'locale',
356 'map',
357 'memory',
358 'mutex',
359 'new',
360 'numeric',
361 'ostream',
362 'queue',
363 'random',
364 'ratio',
365 'regex',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700366 'scoped_allocator',
erg@google.comfd5da632013-10-25 17:39:45 +0000367 'set',
368 'sstream',
369 'stack',
370 'stdexcept',
371 'streambuf',
372 'string',
373 'strstream',
374 'system_error',
375 'thread',
376 'tuple',
377 'typeindex',
378 'typeinfo',
379 'type_traits',
380 'unordered_map',
381 'unordered_set',
382 'utility',
erg@google.com5d00c562013-07-12 19:57:05 +0000383 'valarray',
erg@google.comfd5da632013-10-25 17:39:45 +0000384 'vector',
385 # 17.6.1.2 C++ headers for C library facilities
386 'cassert',
387 'ccomplex',
388 'cctype',
389 'cerrno',
390 'cfenv',
391 'cfloat',
392 'cinttypes',
393 'ciso646',
394 'climits',
395 'clocale',
396 'cmath',
397 'csetjmp',
398 'csignal',
399 'cstdalign',
400 'cstdarg',
401 'cstdbool',
402 'cstddef',
403 'cstdint',
404 'cstdio',
405 'cstdlib',
406 'cstring',
407 'ctgmath',
408 'ctime',
409 'cuchar',
410 'cwchar',
411 'cwctype',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000412 ])
413
Alex Vakulenko01e47232016-05-06 13:54:15 -0700414# Type names
415_TYPES = re.compile(
416 r'^(?:'
417 # [dcl.type.simple]
418 r'(char(16_t|32_t)?)|wchar_t|'
419 r'bool|short|int|long|signed|unsigned|float|double|'
420 # [support.types]
421 r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|'
422 # [cstdint.syn]
423 r'(u?int(_fast|_least)?(8|16|32|64)_t)|'
424 r'(u?int(max|ptr)_t)|'
425 r')$')
426
avakulenko@google.com02af6282014-06-04 18:53:25 +0000427
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000428# These headers are excluded from [build/include] and [build/include_order]
429# checks:
430# - Anything not following google file name conventions (containing an
431# uppercase character, such as Python.h or nsStringAPI.h, for example).
432# - Lua headers.
433_THIRD_PARTY_HEADERS_PATTERN = re.compile(
434 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
435
Alex Vakulenko01e47232016-05-06 13:54:15 -0700436# Pattern for matching FileInfo.BaseName() against test file name
437_TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$'
438
439# Pattern that matches only complete whitespace, possibly across multiple lines.
440_EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000441
erg@google.com4e00b9a2009-01-12 23:05:11 +0000442# Assertion macros. These are defined in base/logging.h and
Alex Vakulenko01e47232016-05-06 13:54:15 -0700443# testing/base/public/gunit.h.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000444_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000445 'DCHECK', 'CHECK',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700446 'EXPECT_TRUE', 'ASSERT_TRUE',
447 'EXPECT_FALSE', 'ASSERT_FALSE',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000448 ]
449
erg@google.come35f7652009-06-19 20:52:09 +0000450# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000451_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
452
453for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
454 ('>=', 'GE'), ('>', 'GT'),
455 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000456 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000457 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
458 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
459 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000460
461for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
462 ('>=', 'LT'), ('>', 'LE'),
463 ('<=', 'GT'), ('<', 'GE')]:
464 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
465 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000466
erg@google.comd350fe52013-01-14 17:51:48 +0000467# Alternative tokens and their replacements. For full list, see section 2.5
468# Alternative tokens [lex.digraph] in the C++ standard.
469#
470# Digraphs (such as '%:') are not included here since it's a mess to
471# match those on a word boundary.
472_ALT_TOKEN_REPLACEMENT = {
473 'and': '&&',
474 'bitor': '|',
475 'or': '||',
476 'xor': '^',
477 'compl': '~',
478 'bitand': '&',
479 'and_eq': '&=',
480 'or_eq': '|=',
481 'xor_eq': '^=',
482 'not': '!',
483 'not_eq': '!='
484 }
485
486# Compile regular expression that matches all the above keywords. The "[ =()]"
487# bit is meant to avoid matching these keywords outside of boolean expressions.
488#
erg@google.comc6671232013-10-25 21:44:03 +0000489# False positives include C-style multi-line comments and multi-line strings
490# but those have always been troublesome for cpplint.
erg@google.comd350fe52013-01-14 17:51:48 +0000491_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
492 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
493
erg@google.com4e00b9a2009-01-12 23:05:11 +0000494
495# These constants define types of headers for use with
496# _IncludeState.CheckNextIncludeOrder().
497_C_SYS_HEADER = 1
498_CPP_SYS_HEADER = 2
499_LIKELY_MY_HEADER = 3
500_POSSIBLE_MY_HEADER = 4
501_OTHER_HEADER = 5
502
erg@google.comd350fe52013-01-14 17:51:48 +0000503# These constants define the current inline assembly state
504_NO_ASM = 0 # Outside of inline assembly block
505_INSIDE_ASM = 1 # Inside inline assembly block
506_END_ASM = 2 # Last line of inline assembly block
507_BLOCK_ASM = 3 # The whole block is an inline assembly block
508
509# Match start of assembly blocks
510_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
511 r'(?:\s+(volatile|__volatile__))?'
512 r'\s*[{(]')
513
Alex Vakulenko01e47232016-05-06 13:54:15 -0700514# Match strings that indicate we're working on a C (not C++) file.
515_SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|'
516 r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))')
517
518# Match string that indicates we're working on a Linux Kernel file.
519_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
erg@google.com4e00b9a2009-01-12 23:05:11 +0000520
521_regexp_compile_cache = {}
522
erg+personal@google.com05189642010-04-30 20:43:03 +0000523# {str, set(int)}: a map from error categories to sets of linenumbers
524# on which those errors are expected and should be suppressed.
525_error_suppressions = {}
526
erg@google.com4d70a882013-04-16 21:06:32 +0000527# The root directory used for deriving header guard CPP variable.
528# This is set by --root flag.
529_root = None
530
erg@google.comab53edf2013-11-05 22:23:37 +0000531# The allowed line length of files.
532# This is set by --linelength flag.
533_line_length = 80
534
erg@google.com19680272013-12-16 22:48:54 +0000535# The allowed extensions for file names
536# This is set by --extensions flag.
537_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
538
Alex Vakulenko01e47232016-05-06 13:54:15 -0700539# {str, bool}: a map from error categories to booleans which indicate if the
540# category should be suppressed for every line.
541_global_error_suppressions = {}
542
543
erg+personal@google.com05189642010-04-30 20:43:03 +0000544def ParseNolintSuppressions(filename, raw_line, linenum, error):
Alex Vakulenko01e47232016-05-06 13:54:15 -0700545 """Updates the global list of line error-suppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000546
547 Parses any NOLINT comments on the current line, updating the global
548 error_suppressions store. Reports an error if the NOLINT comment
549 was malformed.
550
551 Args:
552 filename: str, the name of the input file.
553 raw_line: str, the line of input text, with comments.
554 linenum: int, the number of the current line.
555 error: function, an error handler.
556 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000557 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
erg@google.com8a95ecc2011-09-08 00:45:54 +0000558 if matched:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000559 if matched.group(1):
560 suppressed_line = linenum + 1
561 else:
562 suppressed_line = linenum
563 category = matched.group(2)
erg+personal@google.com05189642010-04-30 20:43:03 +0000564 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000565 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg+personal@google.com05189642010-04-30 20:43:03 +0000566 else:
567 if category.startswith('(') and category.endswith(')'):
568 category = category[1:-1]
569 if category in _ERROR_CATEGORIES:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000570 _error_suppressions.setdefault(category, set()).add(suppressed_line)
avakulenko@google.com554223d2014-12-04 22:00:20 +0000571 elif category not in _LEGACY_ERROR_CATEGORIES:
erg+personal@google.com05189642010-04-30 20:43:03 +0000572 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000573 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000574
575
Alex Vakulenko01e47232016-05-06 13:54:15 -0700576def ProcessGlobalSuppresions(lines):
577 """Updates the list of global error suppressions.
578
579 Parses any lint directives in the file that have global effect.
580
581 Args:
582 lines: An array of strings, each representing a line of the file, with the
583 last element being empty if the file is terminated with a newline.
584 """
585 for line in lines:
586 if _SEARCH_C_FILE.search(line):
587 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
588 _global_error_suppressions[category] = True
589 if _SEARCH_KERNEL_FILE.search(line):
590 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES:
591 _global_error_suppressions[category] = True
592
593
erg+personal@google.com05189642010-04-30 20:43:03 +0000594def ResetNolintSuppressions():
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000595 """Resets the set of NOLINT suppressions to empty."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000596 _error_suppressions.clear()
Alex Vakulenko01e47232016-05-06 13:54:15 -0700597 _global_error_suppressions.clear()
erg+personal@google.com05189642010-04-30 20:43:03 +0000598
599
600def IsErrorSuppressedByNolint(category, linenum):
601 """Returns true if the specified error category is suppressed on this line.
602
603 Consults the global error_suppressions map populated by
Alex Vakulenko01e47232016-05-06 13:54:15 -0700604 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000605
606 Args:
607 category: str, the category of the error.
608 linenum: int, the current line number.
609 Returns:
Alex Vakulenko01e47232016-05-06 13:54:15 -0700610 bool, True iff the error should be suppressed due to a NOLINT comment or
611 global suppression.
erg+personal@google.com05189642010-04-30 20:43:03 +0000612 """
Alex Vakulenko01e47232016-05-06 13:54:15 -0700613 return (_global_error_suppressions.get(category, False) or
614 linenum in _error_suppressions.get(category, set()) or
erg+personal@google.com05189642010-04-30 20:43:03 +0000615 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000616
avakulenko@google.com02af6282014-06-04 18:53:25 +0000617
erg@google.com4e00b9a2009-01-12 23:05:11 +0000618def Match(pattern, s):
619 """Matches the string with the pattern, caching the compiled regexp."""
620 # The regexp compilation caching is inlined in both Match and Search for
621 # performance reasons; factoring it out into a separate function turns out
622 # to be noticeably expensive.
erg@google.comc6671232013-10-25 21:44:03 +0000623 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000624 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
625 return _regexp_compile_cache[pattern].match(s)
626
627
erg@google.comfd5da632013-10-25 17:39:45 +0000628def ReplaceAll(pattern, rep, s):
629 """Replaces instances of pattern in a string with a replacement.
630
631 The compiled regex is kept in a cache shared by Match and Search.
632
633 Args:
634 pattern: regex pattern
635 rep: replacement text
636 s: search string
637
638 Returns:
639 string with replacements made (or original string if no replacements)
640 """
641 if pattern not in _regexp_compile_cache:
642 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
643 return _regexp_compile_cache[pattern].sub(rep, s)
644
645
erg@google.com4e00b9a2009-01-12 23:05:11 +0000646def Search(pattern, s):
647 """Searches the string for the pattern, caching the compiled regexp."""
erg@google.comc6671232013-10-25 21:44:03 +0000648 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000649 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
650 return _regexp_compile_cache[pattern].search(s)
651
652
Alex Vakulenko01e47232016-05-06 13:54:15 -0700653def _IsSourceExtension(s):
654 """File extension (excluding dot) matches a source file extension."""
655 return s in ('c', 'cc', 'cpp', 'cxx')
656
657
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000658class _IncludeState(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000659 """Tracks line numbers for includes, and the order in which includes appear.
660
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000661 include_list contains list of lists of (header, line number) pairs.
662 It's a lists of lists rather than just one flat list to make it
663 easier to update across preprocessor boundaries.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000664
665 Call CheckNextIncludeOrder() once for each header in the file, passing
666 in the type constants defined above. Calls in an illegal order will
667 raise an _IncludeError with an appropriate error message.
668
669 """
670 # self._section will move monotonically through this set. If it ever
671 # needs to move backwards, CheckNextIncludeOrder will raise an error.
672 _INITIAL_SECTION = 0
673 _MY_H_SECTION = 1
674 _C_SECTION = 2
675 _CPP_SECTION = 3
676 _OTHER_H_SECTION = 4
677
678 _TYPE_NAMES = {
679 _C_SYS_HEADER: 'C system header',
680 _CPP_SYS_HEADER: 'C++ system header',
681 _LIKELY_MY_HEADER: 'header this file implements',
682 _POSSIBLE_MY_HEADER: 'header this file may implement',
683 _OTHER_HEADER: 'other header',
684 }
685 _SECTION_NAMES = {
686 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
687 _MY_H_SECTION: 'a header this file implements',
688 _C_SECTION: 'C system header',
689 _CPP_SECTION: 'C++ system header',
690 _OTHER_H_SECTION: 'other header',
691 }
692
693 def __init__(self):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000694 self.include_list = [[]]
695 self.ResetSection('')
erg@google.com2aa59982013-10-28 19:09:25 +0000696
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000697 def FindHeader(self, header):
698 """Check if a header has already been included.
699
700 Args:
701 header: header to check.
702 Returns:
703 Line number of previous occurrence, or -1 if the header has not
704 been seen before.
705 """
706 for section_list in self.include_list:
707 for f in section_list:
708 if f[0] == header:
709 return f[1]
710 return -1
711
712 def ResetSection(self, directive):
713 """Reset section checking for preprocessor directive.
714
715 Args:
716 directive: preprocessor directive (e.g. "if", "else").
717 """
erg@google.coma868d2d2009-10-09 21:18:45 +0000718 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000719 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000720 # The path of last found header.
721 self._last_header = ''
722
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000723 # Update list of includes. Note that we never pop from the
724 # include list.
725 if directive in ('if', 'ifdef', 'ifndef'):
726 self.include_list.append([])
727 elif directive in ('else', 'elif'):
728 self.include_list[-1] = []
729
erg@google.comfd5da632013-10-25 17:39:45 +0000730 def SetLastHeader(self, header_path):
731 self._last_header = header_path
732
erg@google.coma868d2d2009-10-09 21:18:45 +0000733 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000734 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000735
736 - replaces "-" with "_" so they both cmp the same.
737 - removes '-inl' since we don't require them to be after the main header.
738 - lowercase everything, just in case.
739
740 Args:
741 header_path: Path to be canonicalized.
742
743 Returns:
744 Canonicalized path.
745 """
746 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
747
erg@google.comfd5da632013-10-25 17:39:45 +0000748 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.coma868d2d2009-10-09 21:18:45 +0000749 """Check if a header is in alphabetical order with the previous header.
750
751 Args:
erg@google.comfd5da632013-10-25 17:39:45 +0000752 clean_lines: A CleansedLines instance containing the file.
753 linenum: The number of the line to check.
754 header_path: Canonicalized header to be checked.
erg@google.coma868d2d2009-10-09 21:18:45 +0000755
756 Returns:
757 Returns true if the header is in alphabetical order.
758 """
erg@google.comfd5da632013-10-25 17:39:45 +0000759 # If previous section is different from current section, _last_header will
760 # be reset to empty string, so it's always less than current header.
761 #
762 # If previous line was a blank line, assume that the headers are
763 # intentionally sorted the way they are.
764 if (self._last_header > header_path and
avakulenko@google.com554223d2014-12-04 22:00:20 +0000765 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])):
erg@google.coma868d2d2009-10-09 21:18:45 +0000766 return False
erg@google.coma868d2d2009-10-09 21:18:45 +0000767 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000768
769 def CheckNextIncludeOrder(self, header_type):
770 """Returns a non-empty error message if the next header is out of order.
771
772 This function also updates the internal state to be ready to check
773 the next include.
774
775 Args:
776 header_type: One of the _XXX_HEADER constants defined above.
777
778 Returns:
779 The empty string if the header is in the right order, or an
780 error message describing what's wrong.
781
782 """
783 error_message = ('Found %s after %s' %
784 (self._TYPE_NAMES[header_type],
785 self._SECTION_NAMES[self._section]))
786
erg@google.coma868d2d2009-10-09 21:18:45 +0000787 last_section = self._section
788
erg@google.com4e00b9a2009-01-12 23:05:11 +0000789 if header_type == _C_SYS_HEADER:
790 if self._section <= self._C_SECTION:
791 self._section = self._C_SECTION
792 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000793 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000794 return error_message
795 elif header_type == _CPP_SYS_HEADER:
796 if self._section <= self._CPP_SECTION:
797 self._section = self._CPP_SECTION
798 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000799 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000800 return error_message
801 elif header_type == _LIKELY_MY_HEADER:
802 if self._section <= self._MY_H_SECTION:
803 self._section = self._MY_H_SECTION
804 else:
805 self._section = self._OTHER_H_SECTION
806 elif header_type == _POSSIBLE_MY_HEADER:
807 if self._section <= self._MY_H_SECTION:
808 self._section = self._MY_H_SECTION
809 else:
810 # This will always be the fallback because we're not sure
811 # enough that the header is associated with this file.
812 self._section = self._OTHER_H_SECTION
813 else:
814 assert header_type == _OTHER_HEADER
815 self._section = self._OTHER_H_SECTION
816
erg@google.coma868d2d2009-10-09 21:18:45 +0000817 if last_section != self._section:
818 self._last_header = ''
819
erg@google.com4e00b9a2009-01-12 23:05:11 +0000820 return ''
821
822
823class _CppLintState(object):
824 """Maintains module-wide state.."""
825
826 def __init__(self):
827 self.verbose_level = 1 # global setting.
828 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000829 # filters to apply when emitting error messages
830 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000831 # backup of filter list. Used to restore the state after each file.
832 self._filters_backup = self.filters[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000833 self.counting = 'total' # In what way are we counting errors?
834 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000835
836 # output format:
837 # "emacs" - format that emacs can parse (default)
838 # "vs7" - format that Microsoft Visual Studio 7 can parse
839 self.output_format = 'emacs'
840
841 def SetOutputFormat(self, output_format):
842 """Sets the output format for errors."""
843 self.output_format = output_format
844
845 def SetVerboseLevel(self, level):
846 """Sets the module's verbosity, and returns the previous setting."""
847 last_verbose_level = self.verbose_level
848 self.verbose_level = level
849 return last_verbose_level
850
erg@google.coma868d2d2009-10-09 21:18:45 +0000851 def SetCountingStyle(self, counting_style):
852 """Sets the module's counting options."""
853 self.counting = counting_style
854
erg@google.com4e00b9a2009-01-12 23:05:11 +0000855 def SetFilters(self, filters):
856 """Sets the error-message filters.
857
858 These filters are applied when deciding whether to emit a given
859 error message.
860
861 Args:
862 filters: A string of comma-separated filters (eg "+whitespace/indent").
863 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000864
865 Raises:
866 ValueError: The comma-separated filters did not all start with '+' or '-'.
867 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000868 """
erg@google.come35f7652009-06-19 20:52:09 +0000869 # Default filters always have less priority than the flag ones.
870 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000871 self.AddFilters(filters)
872
873 def AddFilters(self, filters):
874 """ Adds more filters to the existing list of error-message filters. """
erg@google.come35f7652009-06-19 20:52:09 +0000875 for filt in filters.split(','):
876 clean_filt = filt.strip()
877 if clean_filt:
878 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000879 for filt in self.filters:
880 if not (filt.startswith('+') or filt.startswith('-')):
881 raise ValueError('Every filter in --filters must start with + or -'
882 ' (%s does not)' % filt)
883
erg@google.com7430eef2014-07-28 22:33:46 +0000884 def BackupFilters(self):
885 """ Saves the current filter list to backup storage."""
886 self._filters_backup = self.filters[:]
887
888 def RestoreFilters(self):
889 """ Restores filters previously backed up."""
890 self.filters = self._filters_backup[:]
891
erg@google.coma868d2d2009-10-09 21:18:45 +0000892 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000893 """Sets the module's error statistic back to zero."""
894 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000895 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000896
erg@google.coma868d2d2009-10-09 21:18:45 +0000897 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000898 """Bumps the module's error statistic."""
899 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000900 if self.counting in ('toplevel', 'detailed'):
901 if self.counting != 'detailed':
902 category = category.split('/')[0]
903 if category not in self.errors_by_category:
904 self.errors_by_category[category] = 0
905 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000906
erg@google.coma868d2d2009-10-09 21:18:45 +0000907 def PrintErrorCounts(self):
908 """Print a summary of errors by category, and the total."""
909 for category, count in self.errors_by_category.iteritems():
910 sys.stderr.write('Category \'%s\' errors found: %d\n' %
911 (category, count))
912 sys.stderr.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000913
914_cpplint_state = _CppLintState()
915
916
917def _OutputFormat():
918 """Gets the module's output format."""
919 return _cpplint_state.output_format
920
921
922def _SetOutputFormat(output_format):
923 """Sets the module's output format."""
924 _cpplint_state.SetOutputFormat(output_format)
925
926
927def _VerboseLevel():
928 """Returns the module's verbosity setting."""
929 return _cpplint_state.verbose_level
930
931
932def _SetVerboseLevel(level):
933 """Sets the module's verbosity, and returns the previous setting."""
934 return _cpplint_state.SetVerboseLevel(level)
935
936
erg@google.coma868d2d2009-10-09 21:18:45 +0000937def _SetCountingStyle(level):
938 """Sets the module's counting options."""
939 _cpplint_state.SetCountingStyle(level)
940
941
erg@google.com4e00b9a2009-01-12 23:05:11 +0000942def _Filters():
943 """Returns the module's list of output filters, as a list."""
944 return _cpplint_state.filters
945
946
947def _SetFilters(filters):
948 """Sets the module's error-message filters.
949
950 These filters are applied when deciding whether to emit a given
951 error message.
952
953 Args:
954 filters: A string of comma-separated filters (eg "whitespace/indent").
955 Each filter should start with + or -; else we die.
956 """
957 _cpplint_state.SetFilters(filters)
958
erg@google.com7430eef2014-07-28 22:33:46 +0000959def _AddFilters(filters):
960 """Adds more filter overrides.
961
962 Unlike _SetFilters, this function does not reset the current list of filters
963 available.
964
965 Args:
966 filters: A string of comma-separated filters (eg "whitespace/indent").
967 Each filter should start with + or -; else we die.
968 """
969 _cpplint_state.AddFilters(filters)
970
971def _BackupFilters():
972 """ Saves the current filter list to backup storage."""
973 _cpplint_state.BackupFilters()
974
975def _RestoreFilters():
976 """ Restores filters previously backed up."""
977 _cpplint_state.RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +0000978
979class _FunctionState(object):
980 """Tracks current function name and the number of lines in its body."""
981
982 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
983 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
984
985 def __init__(self):
986 self.in_a_function = False
987 self.lines_in_function = 0
988 self.current_function = ''
989
990 def Begin(self, function_name):
991 """Start analyzing function body.
992
993 Args:
994 function_name: The name of the function being tracked.
995 """
996 self.in_a_function = True
997 self.lines_in_function = 0
998 self.current_function = function_name
999
1000 def Count(self):
1001 """Count line in current function body."""
1002 if self.in_a_function:
1003 self.lines_in_function += 1
1004
1005 def Check(self, error, filename, linenum):
1006 """Report if too many lines in function body.
1007
1008 Args:
1009 error: The function to call with any errors found.
1010 filename: The name of the current file.
1011 linenum: The number of the line to check.
1012 """
Alex Vakulenko01e47232016-05-06 13:54:15 -07001013 if not self.in_a_function:
1014 return
1015
erg@google.com4e00b9a2009-01-12 23:05:11 +00001016 if Match(r'T(EST|est)', self.current_function):
1017 base_trigger = self._TEST_TRIGGER
1018 else:
1019 base_trigger = self._NORMAL_TRIGGER
1020 trigger = base_trigger * 2**_VerboseLevel()
1021
1022 if self.lines_in_function > trigger:
1023 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
1024 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1025 if error_level > 5:
1026 error_level = 5
1027 error(filename, linenum, 'readability/fn_size', error_level,
1028 'Small and focused functions are preferred:'
1029 ' %s has %d non-comment lines'
1030 ' (error triggered by exceeding %d lines).' % (
1031 self.current_function, self.lines_in_function, trigger))
1032
1033 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001034 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001035 self.in_a_function = False
1036
1037
1038class _IncludeError(Exception):
1039 """Indicates a problem with the include order in a file."""
1040 pass
1041
1042
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001043class FileInfo(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001044 """Provides utility functions for filenames.
1045
1046 FileInfo provides easy access to the components of a file's path
1047 relative to the project root.
1048 """
1049
1050 def __init__(self, filename):
1051 self._filename = filename
1052
1053 def FullName(self):
1054 """Make Windows paths like Unix."""
1055 return os.path.abspath(self._filename).replace('\\', '/')
1056
1057 def RepositoryName(self):
1058 """FullName after removing the local path to the repository.
1059
1060 If we have a real absolute path name here we can try to do something smart:
1061 detecting the root of the checkout and truncating /path/to/checkout from
1062 the name so that we get header guards that don't include things like
1063 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
1064 people on different computers who have checked the source out to different
1065 locations won't see bogus errors.
1066 """
1067 fullname = self.FullName()
1068
1069 if os.path.exists(fullname):
1070 project_dir = os.path.dirname(fullname)
1071
1072 if os.path.exists(os.path.join(project_dir, ".svn")):
1073 # If there's a .svn file in the current directory, we recursively look
1074 # up the directory tree for the top of the SVN checkout
1075 root_dir = project_dir
1076 one_up_dir = os.path.dirname(root_dir)
1077 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1078 root_dir = os.path.dirname(root_dir)
1079 one_up_dir = os.path.dirname(one_up_dir)
1080
1081 prefix = os.path.commonprefix([root_dir, project_dir])
1082 return fullname[len(prefix) + 1:]
1083
erg@google.com3dc74262011-11-30 01:12:00 +00001084 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1085 # searching up from the current path.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001086 root_dir = current_dir = os.path.dirname(fullname)
1087 while current_dir != os.path.dirname(current_dir):
1088 if (os.path.exists(os.path.join(current_dir, ".git")) or
1089 os.path.exists(os.path.join(current_dir, ".hg")) or
1090 os.path.exists(os.path.join(current_dir, ".svn"))):
1091 root_dir = current_dir
1092 current_dir = os.path.dirname(current_dir)
erg@google.com42e59b02010-10-04 22:18:07 +00001093
1094 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +00001095 os.path.exists(os.path.join(root_dir, ".hg")) or
1096 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +00001097 prefix = os.path.commonprefix([root_dir, project_dir])
1098 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +00001099
1100 # Don't know what to do; header guard warnings may be wrong...
1101 return fullname
1102
1103 def Split(self):
1104 """Splits the file into the directory, basename, and extension.
1105
1106 For 'chrome/browser/browser.cc', Split() would
1107 return ('chrome/browser', 'browser', '.cc')
1108
1109 Returns:
1110 A tuple of (directory, basename, extension).
1111 """
1112
1113 googlename = self.RepositoryName()
1114 project, rest = os.path.split(googlename)
1115 return (project,) + os.path.splitext(rest)
1116
1117 def BaseName(self):
1118 """File base name - text after the final slash, before the final period."""
1119 return self.Split()[1]
1120
1121 def Extension(self):
1122 """File extension - text following the final period."""
1123 return self.Split()[2]
1124
1125 def NoExtension(self):
1126 """File has no source file extension."""
1127 return '/'.join(self.Split()[0:2])
1128
1129 def IsSource(self):
1130 """File has a source file extension."""
Alex Vakulenko01e47232016-05-06 13:54:15 -07001131 return _IsSourceExtension(self.Extension()[1:])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001132
1133
erg+personal@google.com05189642010-04-30 20:43:03 +00001134def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001135 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +00001136
1137 # There are three ways we might decide not to print an error message:
1138 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +00001139 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +00001140 if IsErrorSuppressedByNolint(category, linenum):
1141 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00001142
erg@google.com4e00b9a2009-01-12 23:05:11 +00001143 if confidence < _cpplint_state.verbose_level:
1144 return False
1145
1146 is_filtered = False
1147 for one_filter in _Filters():
1148 if one_filter.startswith('-'):
1149 if category.startswith(one_filter[1:]):
1150 is_filtered = True
1151 elif one_filter.startswith('+'):
1152 if category.startswith(one_filter[1:]):
1153 is_filtered = False
1154 else:
1155 assert False # should have been checked for in SetFilter.
1156 if is_filtered:
1157 return False
1158
1159 return True
1160
1161
1162def Error(filename, linenum, category, confidence, message):
1163 """Logs the fact we've found a lint error.
1164
1165 We log where the error was found, and also our confidence in the error,
1166 that is, how certain we are this is a legitimate style regression, and
1167 not a misidentification or a use that's sometimes justified.
1168
erg+personal@google.com05189642010-04-30 20:43:03 +00001169 False positives can be suppressed by the use of
1170 "cpplint(category)" comments on the offending line. These are
1171 parsed into _error_suppressions.
1172
erg@google.com4e00b9a2009-01-12 23:05:11 +00001173 Args:
1174 filename: The name of the file containing the error.
1175 linenum: The number of the line containing the error.
1176 category: A string used to describe the "category" this bug
1177 falls under: "whitespace", say, or "runtime". Categories
1178 may have a hierarchy separated by slashes: "whitespace/indent".
1179 confidence: A number from 1-5 representing a confidence score for
1180 the error, with 5 meaning that we are certain of the problem,
1181 and 1 meaning that it could be a legitimate construct.
1182 message: The error message.
1183 """
erg+personal@google.com05189642010-04-30 20:43:03 +00001184 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +00001185 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001186 if _cpplint_state.output_format == 'vs7':
1187 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
1188 filename, linenum, message, category, confidence))
erg@google.com02c27fd2013-05-28 21:34:34 +00001189 elif _cpplint_state.output_format == 'eclipse':
1190 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1191 filename, linenum, message, category, confidence))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001192 else:
1193 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1194 filename, linenum, message, category, confidence))
1195
1196
erg@google.com2aa59982013-10-28 19:09:25 +00001197# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001198_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1199 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.com02af6282014-06-04 18:53:25 +00001200# Match a single C style comment on the same line.
1201_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1202# Matches multi-line C style comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001203# This RE is a little bit more complicated than one might expect, because we
1204# have to take care of space removals tools so we can handle comments inside
1205# statements better.
1206# The current rule is: We only clear spaces from both sides when we're at the
1207# end of the line. Otherwise, we try to remove spaces from the right side,
1208# if this doesn't work we try on left side but only if there's a non-character
1209# on the right.
1210_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.com02af6282014-06-04 18:53:25 +00001211 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1212 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1213 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1214 _RE_PATTERN_C_COMMENTS + r')')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001215
1216
1217def IsCppString(line):
1218 """Does line terminate so, that the next symbol is in string constant.
1219
1220 This function does not consider single-line nor multi-line comments.
1221
1222 Args:
1223 line: is a partial line of code starting from the 0..n.
1224
1225 Returns:
1226 True, if next character appended to 'line' is inside a
1227 string constant.
1228 """
1229
1230 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1231 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1232
1233
erg@google.com2aa59982013-10-28 19:09:25 +00001234def CleanseRawStrings(raw_lines):
1235 """Removes C++11 raw strings from lines.
1236
1237 Before:
1238 static const char kData[] = R"(
1239 multi-line string
1240 )";
1241
1242 After:
1243 static const char kData[] = ""
1244 (replaced by blank line)
1245 "";
1246
1247 Args:
1248 raw_lines: list of raw lines.
1249
1250 Returns:
1251 list of lines with C++11 raw strings replaced by empty strings.
1252 """
1253
1254 delimiter = None
1255 lines_without_raw_strings = []
1256 for line in raw_lines:
1257 if delimiter:
1258 # Inside a raw string, look for the end
1259 end = line.find(delimiter)
1260 if end >= 0:
1261 # Found the end of the string, match leading space for this
1262 # line and resume copying the original lines, and also insert
1263 # a "" on the last line.
1264 leading_space = Match(r'^(\s*)\S', line)
1265 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1266 delimiter = None
1267 else:
1268 # Haven't found the end yet, append a blank line.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001269 line = '""'
erg@google.com2aa59982013-10-28 19:09:25 +00001270
avakulenko@google.com02af6282014-06-04 18:53:25 +00001271 # Look for beginning of a raw string, and replace them with
1272 # empty strings. This is done in a loop to handle multiple raw
1273 # strings on the same line.
1274 while delimiter is None:
erg@google.com2aa59982013-10-28 19:09:25 +00001275 # Look for beginning of a raw string.
1276 # See 2.14.15 [lex.string] for syntax.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001277 #
1278 # Once we have matched a raw string, we check the prefix of the
1279 # line to make sure that the line is not part of a single line
1280 # comment. It's done this way because we remove raw strings
1281 # before removing comments as opposed to removing comments
1282 # before removing raw strings. This is because there are some
1283 # cpplint checks that requires the comments to be preserved, but
1284 # we don't want to check comments that are inside raw strings.
1285 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1286 if (matched and
1287 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
1288 matched.group(1))):
erg@google.com2aa59982013-10-28 19:09:25 +00001289 delimiter = ')' + matched.group(2) + '"'
1290
1291 end = matched.group(3).find(delimiter)
1292 if end >= 0:
1293 # Raw string ended on same line
1294 line = (matched.group(1) + '""' +
1295 matched.group(3)[end + len(delimiter):])
1296 delimiter = None
1297 else:
1298 # Start of a multi-line raw string
1299 line = matched.group(1) + '""'
avakulenko@google.com02af6282014-06-04 18:53:25 +00001300 else:
1301 break
erg@google.com2aa59982013-10-28 19:09:25 +00001302
1303 lines_without_raw_strings.append(line)
1304
1305 # TODO(unknown): if delimiter is not None here, we might want to
1306 # emit a warning for unterminated string.
1307 return lines_without_raw_strings
1308
1309
erg@google.com4e00b9a2009-01-12 23:05:11 +00001310def FindNextMultiLineCommentStart(lines, lineix):
1311 """Find the beginning marker for a multiline comment."""
1312 while lineix < len(lines):
1313 if lines[lineix].strip().startswith('/*'):
1314 # Only return this marker if the comment goes beyond this line
1315 if lines[lineix].strip().find('*/', 2) < 0:
1316 return lineix
1317 lineix += 1
1318 return len(lines)
1319
1320
1321def FindNextMultiLineCommentEnd(lines, lineix):
1322 """We are inside a comment, find the end marker."""
1323 while lineix < len(lines):
1324 if lines[lineix].strip().endswith('*/'):
1325 return lineix
1326 lineix += 1
1327 return len(lines)
1328
1329
1330def RemoveMultiLineCommentsFromRange(lines, begin, end):
1331 """Clears a range of lines for multi-line comments."""
1332 # Having // dummy comments makes the lines non-empty, so we will not get
1333 # unnecessary blank line warnings later in the code.
1334 for i in range(begin, end):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001335 lines[i] = '/**/'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001336
1337
1338def RemoveMultiLineComments(filename, lines, error):
1339 """Removes multiline (c-style) comments from lines."""
1340 lineix = 0
1341 while lineix < len(lines):
1342 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1343 if lineix_begin >= len(lines):
1344 return
1345 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1346 if lineix_end >= len(lines):
1347 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1348 'Could not find end of multi-line comment')
1349 return
1350 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1351 lineix = lineix_end + 1
1352
1353
1354def CleanseComments(line):
1355 """Removes //-comments and single-line C-style /* */ comments.
1356
1357 Args:
1358 line: A line of C++ source.
1359
1360 Returns:
1361 The line with single-line comments removed.
1362 """
1363 commentpos = line.find('//')
1364 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +00001365 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001366 # get rid of /* ... */
1367 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1368
1369
erg@google.coma87abb82009-02-24 01:41:01 +00001370class CleansedLines(object):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001371 """Holds 4 copies of all lines with different preprocessing applied to them.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001372
avakulenko@google.com554223d2014-12-04 22:00:20 +00001373 1) elided member contains lines without strings and comments.
1374 2) lines member contains lines without comments.
erg@google.comd350fe52013-01-14 17:51:48 +00001375 3) raw_lines member contains all the lines without processing.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001376 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw
1377 strings removed.
1378 All these members are of <type 'list'>, and of the same length.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001379 """
1380
1381 def __init__(self, lines):
1382 self.elided = []
1383 self.lines = []
1384 self.raw_lines = lines
1385 self.num_lines = len(lines)
erg@google.com2aa59982013-10-28 19:09:25 +00001386 self.lines_without_raw_strings = CleanseRawStrings(lines)
1387 for linenum in range(len(self.lines_without_raw_strings)):
1388 self.lines.append(CleanseComments(
1389 self.lines_without_raw_strings[linenum]))
1390 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001391 self.elided.append(CleanseComments(elided))
1392
1393 def NumLines(self):
1394 """Returns the number of lines represented."""
1395 return self.num_lines
1396
1397 @staticmethod
1398 def _CollapseStrings(elided):
1399 """Collapses strings and chars on a line to simple "" or '' blocks.
1400
1401 We nix strings first so we're not fooled by text like '"http://"'
1402
1403 Args:
1404 elided: The line being processed.
1405
1406 Returns:
1407 The line with collapsed strings.
1408 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001409 if _RE_PATTERN_INCLUDE.match(elided):
1410 return elided
1411
1412 # Remove escaped characters first to make quote/single quote collapsing
1413 # basic. Things that look like escaped characters shouldn't occur
1414 # outside of strings and chars.
1415 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1416
1417 # Replace quoted strings and digit separators. Both single quotes
1418 # and double quotes are processed in the same loop, otherwise
1419 # nested quotes wouldn't work.
1420 collapsed = ''
1421 while True:
1422 # Find the first quote character
1423 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1424 if not match:
1425 collapsed += elided
1426 break
1427 head, quote, tail = match.groups()
1428
1429 if quote == '"':
1430 # Collapse double quoted strings
1431 second_quote = tail.find('"')
1432 if second_quote >= 0:
1433 collapsed += head + '""'
1434 elided = tail[second_quote + 1:]
1435 else:
1436 # Unmatched double quote, don't bother processing the rest
1437 # of the line since this is probably a multiline string.
1438 collapsed += elided
1439 break
1440 else:
1441 # Found single quote, check nearby text to eliminate digit separators.
1442 #
1443 # There is no special handling for floating point here, because
1444 # the integer/fractional/exponent parts would all be parsed
1445 # correctly as long as there are digits on both sides of the
1446 # separator. So we are fine as long as we don't see something
1447 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1448 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1449 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1450 collapsed += head + match_literal.group(1).replace("'", '')
1451 elided = match_literal.group(2)
1452 else:
1453 second_quote = tail.find('\'')
1454 if second_quote >= 0:
1455 collapsed += head + "''"
1456 elided = tail[second_quote + 1:]
1457 else:
1458 # Unmatched single quote
1459 collapsed += elided
1460 break
1461
1462 return collapsed
erg@google.com4e00b9a2009-01-12 23:05:11 +00001463
1464
avakulenko@google.com02af6282014-06-04 18:53:25 +00001465def FindEndOfExpressionInLine(line, startpos, stack):
1466 """Find the position just after the end of current parenthesized expression.
erg@google.comd350fe52013-01-14 17:51:48 +00001467
1468 Args:
1469 line: a CleansedLines line.
1470 startpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001471 stack: nesting stack at startpos.
erg@google.comd350fe52013-01-14 17:51:48 +00001472
1473 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001474 On finding matching end: (index just after matching end, None)
1475 On finding an unclosed expression: (-1, None)
1476 Otherwise: (-1, new stack at end of this line)
erg@google.comd350fe52013-01-14 17:51:48 +00001477 """
1478 for i in xrange(startpos, len(line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00001479 char = line[i]
1480 if char in '([{':
1481 # Found start of parenthesized expression, push to expression stack
1482 stack.append(char)
1483 elif char == '<':
1484 # Found potential start of template argument list
1485 if i > 0 and line[i - 1] == '<':
1486 # Left shift operator
1487 if stack and stack[-1] == '<':
1488 stack.pop()
1489 if not stack:
1490 return (-1, None)
1491 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1492 # operator<, don't add to stack
1493 continue
1494 else:
1495 # Tentative start of template argument list
1496 stack.append('<')
1497 elif char in ')]}':
1498 # Found end of parenthesized expression.
1499 #
1500 # If we are currently expecting a matching '>', the pending '<'
1501 # must have been an operator. Remove them from expression stack.
1502 while stack and stack[-1] == '<':
1503 stack.pop()
1504 if not stack:
1505 return (-1, None)
1506 if ((stack[-1] == '(' and char == ')') or
1507 (stack[-1] == '[' and char == ']') or
1508 (stack[-1] == '{' and char == '}')):
1509 stack.pop()
1510 if not stack:
1511 return (i + 1, None)
1512 else:
1513 # Mismatched parentheses
1514 return (-1, None)
1515 elif char == '>':
1516 # Found potential end of template argument list.
1517
1518 # Ignore "->" and operator functions
1519 if (i > 0 and
1520 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1521 continue
1522
1523 # Pop the stack if there is a matching '<'. Otherwise, ignore
1524 # this '>' since it must be an operator.
1525 if stack:
1526 if stack[-1] == '<':
1527 stack.pop()
1528 if not stack:
1529 return (i + 1, None)
1530 elif char == ';':
1531 # Found something that look like end of statements. If we are currently
1532 # expecting a '>', the matching '<' must have been an operator, since
1533 # template argument list should not contain statements.
1534 while stack and stack[-1] == '<':
1535 stack.pop()
1536 if not stack:
1537 return (-1, None)
1538
1539 # Did not find end of expression or unbalanced parentheses on this line
1540 return (-1, stack)
erg@google.comd350fe52013-01-14 17:51:48 +00001541
1542
erg@google.com4e00b9a2009-01-12 23:05:11 +00001543def CloseExpression(clean_lines, linenum, pos):
erg@google.com2aa59982013-10-28 19:09:25 +00001544 """If input points to ( or { or [ or <, finds the position that closes it.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001545
erg@google.com2aa59982013-10-28 19:09:25 +00001546 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001547 linenum/pos that correspond to the closing of the expression.
1548
avakulenko@google.com02af6282014-06-04 18:53:25 +00001549 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1550 Ideally we would want to index all opening and closing parentheses once
1551 and have CloseExpression be just a simple lookup, but due to preprocessor
1552 tricks, this is not so easy.
1553
erg@google.com4e00b9a2009-01-12 23:05:11 +00001554 Args:
1555 clean_lines: A CleansedLines instance containing the file.
1556 linenum: The number of the line to check.
1557 pos: A position on the line.
1558
1559 Returns:
1560 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1561 (line, len(lines), -1) if we never find a close. Note we ignore
1562 strings and comments when matching; and the line we return is the
1563 'cleansed' line at linenum.
1564 """
1565
1566 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001567 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001568 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001569
erg@google.comd350fe52013-01-14 17:51:48 +00001570 # Check first line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001571 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
erg@google.comd350fe52013-01-14 17:51:48 +00001572 if end_pos > -1:
1573 return (line, linenum, end_pos)
erg@google.com2aa59982013-10-28 19:09:25 +00001574
1575 # Continue scanning forward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001576 while stack and linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001577 linenum += 1
1578 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001579 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001580 if end_pos > -1:
1581 return (line, linenum, end_pos)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001582
avakulenko@google.com02af6282014-06-04 18:53:25 +00001583 # Did not find end of expression before end of file, give up
erg@google.comd350fe52013-01-14 17:51:48 +00001584 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001585
erg@google.com2aa59982013-10-28 19:09:25 +00001586
avakulenko@google.com02af6282014-06-04 18:53:25 +00001587def FindStartOfExpressionInLine(line, endpos, stack):
1588 """Find position at the matching start of current expression.
erg@google.com2aa59982013-10-28 19:09:25 +00001589
1590 This is almost the reverse of FindEndOfExpressionInLine, but note
1591 that the input position and returned position differs by 1.
1592
1593 Args:
1594 line: a CleansedLines line.
1595 endpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001596 stack: nesting stack at endpos.
erg@google.com2aa59982013-10-28 19:09:25 +00001597
1598 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001599 On finding matching start: (index at matching start, None)
1600 On finding an unclosed expression: (-1, None)
1601 Otherwise: (-1, new stack at beginning of this line)
erg@google.com2aa59982013-10-28 19:09:25 +00001602 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001603 i = endpos
1604 while i >= 0:
1605 char = line[i]
1606 if char in ')]}':
1607 # Found end of expression, push to expression stack
1608 stack.append(char)
1609 elif char == '>':
1610 # Found potential end of template argument list.
1611 #
1612 # Ignore it if it's a "->" or ">=" or "operator>"
1613 if (i > 0 and
1614 (line[i - 1] == '-' or
1615 Match(r'\s>=\s', line[i - 1:]) or
1616 Search(r'\boperator\s*$', line[0:i]))):
1617 i -= 1
1618 else:
1619 stack.append('>')
1620 elif char == '<':
1621 # Found potential start of template argument list
1622 if i > 0 and line[i - 1] == '<':
1623 # Left shift operator
1624 i -= 1
1625 else:
1626 # If there is a matching '>', we can pop the expression stack.
1627 # Otherwise, ignore this '<' since it must be an operator.
1628 if stack and stack[-1] == '>':
1629 stack.pop()
1630 if not stack:
1631 return (i, None)
1632 elif char in '([{':
1633 # Found start of expression.
1634 #
1635 # If there are any unmatched '>' on the stack, they must be
1636 # operators. Remove those.
1637 while stack and stack[-1] == '>':
1638 stack.pop()
1639 if not stack:
1640 return (-1, None)
1641 if ((char == '(' and stack[-1] == ')') or
1642 (char == '[' and stack[-1] == ']') or
1643 (char == '{' and stack[-1] == '}')):
1644 stack.pop()
1645 if not stack:
1646 return (i, None)
1647 else:
1648 # Mismatched parentheses
1649 return (-1, None)
1650 elif char == ';':
1651 # Found something that look like end of statements. If we are currently
1652 # expecting a '<', the matching '>' must have been an operator, since
1653 # template argument list should not contain statements.
1654 while stack and stack[-1] == '>':
1655 stack.pop()
1656 if not stack:
1657 return (-1, None)
1658
1659 i -= 1
1660
1661 return (-1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001662
1663
1664def ReverseCloseExpression(clean_lines, linenum, pos):
1665 """If input points to ) or } or ] or >, finds the position that opens it.
1666
1667 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1668 linenum/pos that correspond to the opening of the expression.
1669
1670 Args:
1671 clean_lines: A CleansedLines instance containing the file.
1672 linenum: The number of the line to check.
1673 pos: A position on the line.
1674
1675 Returns:
1676 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1677 (line, 0, -1) if we never find the matching opening brace. Note
1678 we ignore strings and comments when matching; and the line we
1679 return is the 'cleansed' line at linenum.
1680 """
1681 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001682 if line[pos] not in ')}]>':
erg@google.com2aa59982013-10-28 19:09:25 +00001683 return (line, 0, -1)
erg@google.com2aa59982013-10-28 19:09:25 +00001684
1685 # Check last line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001686 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
erg@google.com2aa59982013-10-28 19:09:25 +00001687 if start_pos > -1:
1688 return (line, linenum, start_pos)
1689
1690 # Continue scanning backward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001691 while stack and linenum > 0:
erg@google.com2aa59982013-10-28 19:09:25 +00001692 linenum -= 1
1693 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001694 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001695 if start_pos > -1:
1696 return (line, linenum, start_pos)
1697
avakulenko@google.com02af6282014-06-04 18:53:25 +00001698 # Did not find start of expression before beginning of file, give up
erg@google.com2aa59982013-10-28 19:09:25 +00001699 return (line, 0, -1)
1700
1701
erg@google.com4e00b9a2009-01-12 23:05:11 +00001702def CheckForCopyright(filename, lines, error):
1703 """Logs an error if no Copyright message appears at the top of the file."""
1704
1705 # We'll say it should occur by line 10. Don't forget there's a
1706 # dummy line at the front.
1707 for line in xrange(1, min(len(lines), 11)):
1708 if re.search(r'Copyright', lines[line], re.I): break
1709 else: # means no copyright line was found
1710 error(filename, 0, 'legal/copyright', 5,
1711 'No copyright message found. '
1712 'You should have a line: "Copyright [year] <Copyright Owner>"')
1713
1714
avakulenko@google.com02af6282014-06-04 18:53:25 +00001715def GetIndentLevel(line):
1716 """Return the number of leading spaces in line.
1717
1718 Args:
1719 line: A string to check.
1720
1721 Returns:
1722 An integer count of leading spaces, possibly zero.
1723 """
1724 indent = Match(r'^( *)\S', line)
1725 if indent:
1726 return len(indent.group(1))
1727 else:
1728 return 0
1729
1730
erg@google.com4e00b9a2009-01-12 23:05:11 +00001731def GetHeaderGuardCPPVariable(filename):
1732 """Returns the CPP variable that should be used as a header guard.
1733
1734 Args:
1735 filename: The name of a C++ header file.
1736
1737 Returns:
1738 The CPP variable that should be used as a header guard in the
1739 named file.
1740
1741 """
1742
erg+personal@google.com05189642010-04-30 20:43:03 +00001743 # Restores original filename in case that cpplint is invoked from Emacs's
1744 # flymake.
1745 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001746 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001747 # Replace 'c++' with 'cpp'.
1748 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')
Alex Vakulenko01e47232016-05-06 13:54:15 -07001749
erg@google.com4e00b9a2009-01-12 23:05:11 +00001750 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001751 file_path_from_root = fileinfo.RepositoryName()
1752 if _root:
Sergey Sharybin3b0ea892016-05-31 00:21:14 +02001753 suffix = os.sep
1754 # On Windows using directory separator will leave us with
1755 # "bogus escape error" unless we properly escape regex.
1756 if suffix == '\\':
1757 suffix += '\\'
1758 file_path_from_root = re.sub('^' + _root + suffix, '', file_path_from_root)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001759 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001760
1761
avakulenko@google.com554223d2014-12-04 22:00:20 +00001762def CheckForHeaderGuard(filename, clean_lines, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001763 """Checks that the file contains a header guard.
1764
erg@google.coma87abb82009-02-24 01:41:01 +00001765 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001766 headers, checks that the full pathname is used.
1767
1768 Args:
1769 filename: The name of the C++ header file.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001770 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001771 error: The function to call with any errors found.
1772 """
1773
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001774 # Don't check for header guards if there are error suppression
1775 # comments somewhere in this file.
1776 #
1777 # Because this is silencing a warning for a nonexistent line, we
1778 # only support the very specific NOLINT(build/header_guard) syntax,
1779 # and not the general NOLINT or NOLINT(*) syntax.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001780 raw_lines = clean_lines.lines_without_raw_strings
1781 for i in raw_lines:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001782 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1783 return
1784
erg@google.com4e00b9a2009-01-12 23:05:11 +00001785 cppvar = GetHeaderGuardCPPVariable(filename)
1786
avakulenko@google.com554223d2014-12-04 22:00:20 +00001787 ifndef = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001788 ifndef_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001789 define = ''
1790 endif = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001791 endif_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001792 for linenum, line in enumerate(raw_lines):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001793 linesplit = line.split()
1794 if len(linesplit) >= 2:
1795 # find the first occurrence of #ifndef and #define, save arg
1796 if not ifndef and linesplit[0] == '#ifndef':
1797 # set ifndef to the header guard presented on the #ifndef line.
1798 ifndef = linesplit[1]
1799 ifndef_linenum = linenum
1800 if not define and linesplit[0] == '#define':
1801 define = linesplit[1]
1802 # find the last occurrence of #endif, save entire line
1803 if line.startswith('#endif'):
1804 endif = line
1805 endif_linenum = linenum
1806
avakulenko@google.com554223d2014-12-04 22:00:20 +00001807 if not ifndef or not define or ifndef != define:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001808 error(filename, 0, 'build/header_guard', 5,
1809 'No #ifndef header guard found, suggested CPP variable is: %s' %
1810 cppvar)
1811 return
1812
1813 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1814 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001815 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001816 error_level = 0
1817 if ifndef != cppvar + '_':
1818 error_level = 5
1819
avakulenko@google.com554223d2014-12-04 22:00:20 +00001820 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum,
erg+personal@google.com05189642010-04-30 20:43:03 +00001821 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001822 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1823 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1824
avakulenko@google.com554223d2014-12-04 22:00:20 +00001825 # Check for "//" comments on endif line.
1826 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum,
1827 error)
1828 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif)
1829 if match:
1830 if match.group(1) == '_':
1831 # Issue low severity warning for deprecated double trailing underscore
1832 error(filename, endif_linenum, 'build/header_guard', 0,
1833 '#endif line should be "#endif // %s"' % cppvar)
erg@google.comdc289702012-01-26 20:30:03 +00001834 return
1835
avakulenko@google.com554223d2014-12-04 22:00:20 +00001836 # Didn't find the corresponding "//" comment. If this file does not
1837 # contain any "//" comments at all, it could be that the compiler
1838 # only wants "/**/" comments, look for those instead.
1839 no_single_line_comments = True
1840 for i in xrange(1, len(raw_lines) - 1):
1841 line = raw_lines[i]
1842 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
1843 no_single_line_comments = False
1844 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00001845
avakulenko@google.com554223d2014-12-04 22:00:20 +00001846 if no_single_line_comments:
1847 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif)
1848 if match:
1849 if match.group(1) == '_':
1850 # Low severity warning for double trailing underscore
1851 error(filename, endif_linenum, 'build/header_guard', 0,
1852 '#endif line should be "#endif /* %s */"' % cppvar)
1853 return
1854
1855 # Didn't find anything
1856 error(filename, endif_linenum, 'build/header_guard', 5,
1857 '#endif line should be "#endif // %s"' % cppvar)
1858
1859
1860def CheckHeaderFileIncluded(filename, include_state, error):
1861 """Logs an error if a .cc file does not include its header."""
1862
1863 # Do not check test files
Alex Vakulenko01e47232016-05-06 13:54:15 -07001864 fileinfo = FileInfo(filename)
1865 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001866 return
1867
Alex Vakulenko01e47232016-05-06 13:54:15 -07001868 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h'
avakulenko@google.com554223d2014-12-04 22:00:20 +00001869 if not os.path.exists(headerfile):
1870 return
1871 headername = FileInfo(headerfile).RepositoryName()
1872 first_include = 0
1873 for section_list in include_state.include_list:
1874 for f in section_list:
1875 if headername in f[0] or f[0] in headername:
1876 return
1877 if not first_include:
1878 first_include = f[1]
1879
1880 error(filename, first_include, 'build/include', 5,
1881 '%s should include its header file %s' % (fileinfo.RepositoryName(),
1882 headername))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001883
1884
erg@google.com2aa59982013-10-28 19:09:25 +00001885def CheckForBadCharacters(filename, lines, error):
1886 """Logs an error for each line containing bad characters.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001887
erg@google.com2aa59982013-10-28 19:09:25 +00001888 Two kinds of bad characters:
1889
1890 1. Unicode replacement characters: These indicate that either the file
1891 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1892 it shouldn't). Note that it's possible for this to throw off line
1893 numbering if the invalid UTF-8 occurred adjacent to a newline.
1894
1895 2. NUL bytes. These are problematic for some tools.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001896
1897 Args:
1898 filename: The name of the current file.
1899 lines: An array of strings, each representing a line of the file.
1900 error: The function to call with any errors found.
1901 """
1902 for linenum, line in enumerate(lines):
1903 if u'\ufffd' in line:
1904 error(filename, linenum, 'readability/utf8', 5,
1905 'Line contains invalid UTF-8 (or Unicode replacement character).')
erg@google.com2aa59982013-10-28 19:09:25 +00001906 if '\0' in line:
1907 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001908
1909
1910def CheckForNewlineAtEOF(filename, lines, error):
1911 """Logs an error if there is no newline char at the end of the file.
1912
1913 Args:
1914 filename: The name of the current file.
1915 lines: An array of strings, each representing a line of the file.
1916 error: The function to call with any errors found.
1917 """
1918
1919 # The array lines() was created by adding two newlines to the
1920 # original file (go figure), then splitting on \n.
1921 # To verify that the file ends in \n, we just have to make sure the
1922 # last-but-two element of lines() exists and is empty.
1923 if len(lines) < 3 or lines[-2]:
1924 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1925 'Could not find a newline character at the end of the file.')
1926
1927
1928def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1929 """Logs an error if we see /* ... */ or "..." that extend past one line.
1930
1931 /* ... */ comments are legit inside macros, for one line.
1932 Otherwise, we prefer // comments, so it's ok to warn about the
1933 other. Likewise, it's ok for strings to extend across multiple
1934 lines, as long as a line continuation character (backslash)
1935 terminates each line. Although not currently prohibited by the C++
1936 style guide, it's ugly and unnecessary. We don't do well with either
1937 in this lint program, so we warn about both.
1938
1939 Args:
1940 filename: The name of the current file.
1941 clean_lines: A CleansedLines instance containing the file.
1942 linenum: The number of the line to check.
1943 error: The function to call with any errors found.
1944 """
1945 line = clean_lines.elided[linenum]
1946
1947 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1948 # second (escaped) slash may trigger later \" detection erroneously.
1949 line = line.replace('\\\\', '')
1950
1951 if line.count('/*') > line.count('*/'):
1952 error(filename, linenum, 'readability/multiline_comment', 5,
1953 'Complex multi-line /*...*/-style comment found. '
1954 'Lint may give bogus warnings. '
1955 'Consider replacing these with //-style comments, '
1956 'with #if 0...#endif, '
1957 'or with more clearly structured multi-line comments.')
1958
1959 if (line.count('"') - line.count('\\"')) % 2:
1960 error(filename, linenum, 'readability/multiline_string', 5,
1961 'Multi-line string ("...") found. This lint script doesn\'t '
erg@google.com2aa59982013-10-28 19:09:25 +00001962 'do well with such strings, and may give bogus warnings. '
1963 'Use C++11 raw strings or concatenation instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001964
1965
avakulenko@google.com02af6282014-06-04 18:53:25 +00001966# (non-threadsafe name, thread-safe alternative, validation pattern)
1967#
1968# The validation pattern is used to eliminate false positives such as:
1969# _rand(); // false positive due to substring match.
1970# ->rand(); // some member function rand().
1971# ACMRandom rand(seed); // some variable named rand.
1972# ISAACRandom rand(); // another variable named rand.
1973#
1974# Basically we require the return value of these functions to be used
1975# in some expression context on the same line by matching on some
1976# operator before the function name. This eliminates constructors and
1977# member function calls.
1978_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
1979_THREADING_LIST = (
1980 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
1981 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
1982 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
1983 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
1984 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
1985 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
1986 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
1987 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
1988 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
1989 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
1990 ('strtok(', 'strtok_r(',
1991 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
1992 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
erg@google.com4e00b9a2009-01-12 23:05:11 +00001993 )
1994
1995
1996def CheckPosixThreading(filename, clean_lines, linenum, error):
1997 """Checks for calls to thread-unsafe functions.
1998
1999 Much code has been originally written without consideration of
2000 multi-threading. Also, engineers are relying on their old experience;
2001 they have learned posix before threading extensions were added. These
2002 tests guide the engineers to use thread-safe functions (when using
2003 posix directly).
2004
2005 Args:
2006 filename: The name of the current file.
2007 clean_lines: A CleansedLines instance containing the file.
2008 linenum: The number of the line to check.
2009 error: The function to call with any errors found.
2010 """
2011 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00002012 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
2013 # Additional pattern matching check to confirm that this is the
2014 # function we are looking for
2015 if Search(pattern, line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002016 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.com02af6282014-06-04 18:53:25 +00002017 'Consider using ' + multithread_safe_func +
2018 '...) instead of ' + single_thread_func +
erg@google.com4e00b9a2009-01-12 23:05:11 +00002019 '...) for improved thread safety.')
2020
2021
erg@google.com2aa59982013-10-28 19:09:25 +00002022def CheckVlogArguments(filename, clean_lines, linenum, error):
2023 """Checks that VLOG() is only used for defining a logging level.
2024
2025 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
2026 VLOG(FATAL) are not.
2027
2028 Args:
2029 filename: The name of the current file.
2030 clean_lines: A CleansedLines instance containing the file.
2031 linenum: The number of the line to check.
2032 error: The function to call with any errors found.
2033 """
2034 line = clean_lines.elided[linenum]
2035 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
2036 error(filename, linenum, 'runtime/vlog', 5,
2037 'VLOG() should be used with numeric verbosity level. '
2038 'Use LOG() if you want symbolic severity levels.')
2039
erg@google.coma868d2d2009-10-09 21:18:45 +00002040# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00002041# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00002042_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00002043 r'^\s*\*\w+(\+\+|--);')
2044
2045
2046def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00002047 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00002048
2049 For example following function:
2050 void increment_counter(int* count) {
2051 *count++;
2052 }
2053 is invalid, because it effectively does count++, moving pointer, and should
2054 be replaced with ++*count, (*count)++ or *count += 1.
2055
2056 Args:
2057 filename: The name of the current file.
2058 clean_lines: A CleansedLines instance containing the file.
2059 linenum: The number of the line to check.
2060 error: The function to call with any errors found.
2061 """
2062 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00002063 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00002064 error(filename, linenum, 'runtime/invalid_increment', 5,
2065 'Changing pointer instead of value (or unused value of operator*).')
2066
2067
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002068def IsMacroDefinition(clean_lines, linenum):
2069 if Search(r'^#define', clean_lines[linenum]):
2070 return True
2071
2072 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
2073 return True
2074
2075 return False
2076
2077
2078def IsForwardClassDeclaration(clean_lines, linenum):
2079 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
2080
2081
erg@google.comd350fe52013-01-14 17:51:48 +00002082class _BlockInfo(object):
2083 """Stores information about a generic block of code."""
2084
Alex Vakulenko01e47232016-05-06 13:54:15 -07002085 def __init__(self, linenum, seen_open_brace):
2086 self.starting_linenum = linenum
erg@google.comd350fe52013-01-14 17:51:48 +00002087 self.seen_open_brace = seen_open_brace
2088 self.open_parentheses = 0
2089 self.inline_asm = _NO_ASM
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002090 self.check_namespace_indentation = False
erg@google.comd350fe52013-01-14 17:51:48 +00002091
2092 def CheckBegin(self, filename, clean_lines, linenum, error):
2093 """Run checks that applies to text up to the opening brace.
2094
2095 This is mostly for checking the text after the class identifier
2096 and the "{", usually where the base class is specified. For other
2097 blocks, there isn't much to check, so we always pass.
2098
2099 Args:
2100 filename: The name of the current file.
2101 clean_lines: A CleansedLines instance containing the file.
2102 linenum: The number of the line to check.
2103 error: The function to call with any errors found.
2104 """
2105 pass
2106
2107 def CheckEnd(self, filename, clean_lines, linenum, error):
2108 """Run checks that applies to text after the closing brace.
2109
2110 This is mostly used for checking end of namespace comments.
2111
2112 Args:
2113 filename: The name of the current file.
2114 clean_lines: A CleansedLines instance containing the file.
2115 linenum: The number of the line to check.
2116 error: The function to call with any errors found.
2117 """
2118 pass
2119
avakulenko@google.com02af6282014-06-04 18:53:25 +00002120 def IsBlockInfo(self):
2121 """Returns true if this block is a _BlockInfo.
2122
2123 This is convenient for verifying that an object is an instance of
2124 a _BlockInfo, but not an instance of any of the derived classes.
2125
2126 Returns:
2127 True for this class, False for derived classes.
2128 """
2129 return self.__class__ == _BlockInfo
2130
2131
2132class _ExternCInfo(_BlockInfo):
2133 """Stores information about an 'extern "C"' block."""
2134
Alex Vakulenko01e47232016-05-06 13:54:15 -07002135 def __init__(self, linenum):
2136 _BlockInfo.__init__(self, linenum, True)
avakulenko@google.com02af6282014-06-04 18:53:25 +00002137
erg@google.comd350fe52013-01-14 17:51:48 +00002138
2139class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002140 """Stores information about a class."""
2141
erg@google.comd350fe52013-01-14 17:51:48 +00002142 def __init__(self, name, class_or_struct, clean_lines, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002143 _BlockInfo.__init__(self, linenum, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002144 self.name = name
erg@google.com4e00b9a2009-01-12 23:05:11 +00002145 self.is_derived = False
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002146 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002147 if class_or_struct == 'struct':
2148 self.access = 'public'
erg@google.comfd5da632013-10-25 17:39:45 +00002149 self.is_struct = True
erg@google.comd350fe52013-01-14 17:51:48 +00002150 else:
2151 self.access = 'private'
erg@google.comfd5da632013-10-25 17:39:45 +00002152 self.is_struct = False
2153
2154 # Remember initial indentation level for this class. Using raw_lines here
erg@google.comc6671232013-10-25 21:44:03 +00002155 # instead of elided to account for leading comments.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002156 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00002157
erg@google.com8a95ecc2011-09-08 00:45:54 +00002158 # Try to find the end of the class. This will be confused by things like:
2159 # class A {
2160 # } *x = { ...
2161 #
2162 # But it's still good enough for CheckSectionSpacing.
2163 self.last_line = 0
2164 depth = 0
2165 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00002166 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00002167 depth += line.count('{') - line.count('}')
2168 if not depth:
2169 self.last_line = i
2170 break
2171
erg@google.comd350fe52013-01-14 17:51:48 +00002172 def CheckBegin(self, filename, clean_lines, linenum, error):
2173 # Look for a bare ':'
2174 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2175 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00002176
erg@google.comfd5da632013-10-25 17:39:45 +00002177 def CheckEnd(self, filename, clean_lines, linenum, error):
avakulenko@google.com554223d2014-12-04 22:00:20 +00002178 # If there is a DISALLOW macro, it should appear near the end of
2179 # the class.
2180 seen_last_thing_in_class = False
2181 for i in xrange(linenum - 1, self.starting_linenum, -1):
2182 match = Search(
2183 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
2184 self.name + r'\)',
2185 clean_lines.elided[i])
2186 if match:
2187 if seen_last_thing_in_class:
2188 error(filename, i, 'readability/constructors', 3,
2189 match.group(1) + ' should be the last thing in the class')
2190 break
2191
2192 if not Match(r'^\s*$', clean_lines.elided[i]):
2193 seen_last_thing_in_class = True
2194
erg@google.comfd5da632013-10-25 17:39:45 +00002195 # Check that closing brace is aligned with beginning of the class.
2196 # Only do this if the closing brace is indented by only whitespaces.
2197 # This means we will not check single-line class definitions.
2198 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2199 if indent and len(indent.group(1)) != self.class_indent:
2200 if self.is_struct:
2201 parent = 'struct ' + self.name
2202 else:
2203 parent = 'class ' + self.name
2204 error(filename, linenum, 'whitespace/indent', 3,
2205 'Closing brace should be aligned with beginning of %s' % parent)
2206
erg@google.com4e00b9a2009-01-12 23:05:11 +00002207
erg@google.comd350fe52013-01-14 17:51:48 +00002208class _NamespaceInfo(_BlockInfo):
2209 """Stores information about a namespace."""
2210
2211 def __init__(self, name, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002212 _BlockInfo.__init__(self, linenum, False)
erg@google.comd350fe52013-01-14 17:51:48 +00002213 self.name = name or ''
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002214 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002215
2216 def CheckEnd(self, filename, clean_lines, linenum, error):
2217 """Check end of namespace comments."""
2218 line = clean_lines.raw_lines[linenum]
2219
2220 # Check how many lines is enclosed in this namespace. Don't issue
2221 # warning for missing namespace comments if there aren't enough
2222 # lines. However, do apply checks if there is already an end of
2223 # namespace comment and it's incorrect.
2224 #
2225 # TODO(unknown): We always want to check end of namespace comments
2226 # if a namespace is large, but sometimes we also want to apply the
2227 # check if a short namespace contained nontrivial things (something
2228 # other than forward declarations). There is currently no logic on
2229 # deciding what these nontrivial things are, so this check is
2230 # triggered by namespace size only, which works most of the time.
2231 if (linenum - self.starting_linenum < 10
Alex Vakulenko01e47232016-05-06 13:54:15 -07002232 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)):
erg@google.comd350fe52013-01-14 17:51:48 +00002233 return
2234
2235 # Look for matching comment at end of namespace.
2236 #
2237 # Note that we accept C style "/* */" comments for terminating
2238 # namespaces, so that code that terminate namespaces inside
erg@google.comc6671232013-10-25 21:44:03 +00002239 # preprocessor macros can be cpplint clean.
erg@google.comd350fe52013-01-14 17:51:48 +00002240 #
2241 # We also accept stuff like "// end of namespace <name>." with the
2242 # period at the end.
2243 #
2244 # Besides these, we don't accept anything else, otherwise we might
2245 # get false negatives when existing comment is a substring of the
erg@google.comc6671232013-10-25 21:44:03 +00002246 # expected namespace.
erg@google.comd350fe52013-01-14 17:51:48 +00002247 if self.name:
2248 # Named namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002249 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
2250 re.escape(self.name) + r'[\*/\.\\\s]*$'),
erg@google.comd350fe52013-01-14 17:51:48 +00002251 line):
2252 error(filename, linenum, 'readability/namespace', 5,
2253 'Namespace should be terminated with "// namespace %s"' %
2254 self.name)
2255 else:
2256 # Anonymous namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002257 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002258 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2259 # mention "// anonymous namespace" as an acceptable form
Alex Vakulenko01e47232016-05-06 13:54:15 -07002260 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002261 error(filename, linenum, 'readability/namespace', 5,
2262 'Anonymous namespace should be terminated with "// namespace"'
2263 ' or "// anonymous namespace"')
2264 else:
2265 error(filename, linenum, 'readability/namespace', 5,
2266 'Anonymous namespace should be terminated with "// namespace"')
erg@google.comd350fe52013-01-14 17:51:48 +00002267
2268
2269class _PreprocessorInfo(object):
2270 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2271
2272 def __init__(self, stack_before_if):
2273 # The entire nesting stack before #if
2274 self.stack_before_if = stack_before_if
2275
2276 # The entire nesting stack up to #else
2277 self.stack_before_else = []
2278
2279 # Whether we have already seen #else or #elif
2280 self.seen_else = False
2281
2282
avakulenko@google.com02af6282014-06-04 18:53:25 +00002283class NestingState(object):
erg@google.comd350fe52013-01-14 17:51:48 +00002284 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00002285
2286 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00002287 # Stack for tracking all braces. An object is pushed whenever we
2288 # see a "{", and popped when we see a "}". Only 3 types of
2289 # objects are possible:
2290 # - _ClassInfo: a class or struct.
2291 # - _NamespaceInfo: a namespace.
2292 # - _BlockInfo: some other type of block.
2293 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00002294
avakulenko@google.com02af6282014-06-04 18:53:25 +00002295 # Top of the previous stack before each Update().
2296 #
2297 # Because the nesting_stack is updated at the end of each line, we
2298 # had to do some convoluted checks to find out what is the current
2299 # scope at the beginning of the line. This check is simplified by
2300 # saving the previous top of nesting stack.
2301 #
2302 # We could save the full stack, but we only need the top. Copying
2303 # the full nesting stack would slow down cpplint by ~10%.
2304 self.previous_stack_top = []
2305
erg@google.comd350fe52013-01-14 17:51:48 +00002306 # Stack of _PreprocessorInfo objects.
2307 self.pp_stack = []
2308
2309 def SeenOpenBrace(self):
2310 """Check if we have seen the opening brace for the innermost block.
2311
2312 Returns:
2313 True if we have seen the opening brace, False if the innermost
2314 block is still expecting an opening brace.
2315 """
2316 return (not self.stack) or self.stack[-1].seen_open_brace
2317
2318 def InNamespaceBody(self):
2319 """Check if we are currently one level inside a namespace body.
2320
2321 Returns:
2322 True if top of the stack is a namespace block, False otherwise.
2323 """
2324 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2325
avakulenko@google.com02af6282014-06-04 18:53:25 +00002326 def InExternC(self):
2327 """Check if we are currently one level inside an 'extern "C"' block.
2328
2329 Returns:
2330 True if top of the stack is an extern block, False otherwise.
2331 """
2332 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2333
2334 def InClassDeclaration(self):
2335 """Check if we are currently one level inside a class or struct declaration.
2336
2337 Returns:
2338 True if top of the stack is a class/struct, False otherwise.
2339 """
2340 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2341
2342 def InAsmBlock(self):
2343 """Check if we are currently one level inside an inline ASM block.
2344
2345 Returns:
2346 True if the top of the stack is a block containing inline ASM.
2347 """
2348 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2349
2350 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2351 """Check if current position is inside template argument list.
2352
2353 Args:
2354 clean_lines: A CleansedLines instance containing the file.
2355 linenum: The number of the line to check.
2356 pos: position just after the suspected template argument.
2357 Returns:
2358 True if (linenum, pos) is inside template arguments.
2359 """
2360 while linenum < clean_lines.NumLines():
2361 # Find the earliest character that might indicate a template argument
2362 line = clean_lines.elided[linenum]
2363 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2364 if not match:
2365 linenum += 1
2366 pos = 0
2367 continue
2368 token = match.group(1)
2369 pos += len(match.group(0))
2370
2371 # These things do not look like template argument list:
2372 # class Suspect {
2373 # class Suspect x; }
2374 if token in ('{', '}', ';'): return False
2375
2376 # These things look like template argument list:
2377 # template <class Suspect>
2378 # template <class Suspect = default_value>
2379 # template <class Suspect[]>
2380 # template <class Suspect...>
2381 if token in ('>', '=', '[', ']', '.'): return True
2382
2383 # Check if token is an unmatched '<'.
2384 # If not, move on to the next character.
2385 if token != '<':
2386 pos += 1
2387 if pos >= len(line):
2388 linenum += 1
2389 pos = 0
2390 continue
2391
2392 # We can't be sure if we just find a single '<', and need to
2393 # find the matching '>'.
2394 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2395 if end_pos < 0:
2396 # Not sure if template argument list or syntax error in file
2397 return False
2398 linenum = end_line
2399 pos = end_pos
2400 return False
2401
erg@google.comd350fe52013-01-14 17:51:48 +00002402 def UpdatePreprocessor(self, line):
2403 """Update preprocessor stack.
2404
2405 We need to handle preprocessors due to classes like this:
2406 #ifdef SWIG
2407 struct ResultDetailsPageElementExtensionPoint {
2408 #else
2409 struct ResultDetailsPageElementExtensionPoint : public Extension {
2410 #endif
erg@google.comd350fe52013-01-14 17:51:48 +00002411
2412 We make the following assumptions (good enough for most files):
2413 - Preprocessor condition evaluates to true from #if up to first
2414 #else/#elif/#endif.
2415
2416 - Preprocessor condition evaluates to false from #else/#elif up
2417 to #endif. We still perform lint checks on these lines, but
2418 these do not affect nesting stack.
2419
2420 Args:
2421 line: current line to check.
2422 """
2423 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2424 # Beginning of #if block, save the nesting stack here. The saved
2425 # stack will allow us to restore the parsing state in the #else case.
2426 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2427 elif Match(r'^\s*#\s*(else|elif)\b', line):
2428 # Beginning of #else block
2429 if self.pp_stack:
2430 if not self.pp_stack[-1].seen_else:
2431 # This is the first #else or #elif block. Remember the
2432 # whole nesting stack up to this point. This is what we
2433 # keep after the #endif.
2434 self.pp_stack[-1].seen_else = True
2435 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2436
2437 # Restore the stack to how it was before the #if
2438 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2439 else:
2440 # TODO(unknown): unexpected #else, issue warning?
2441 pass
2442 elif Match(r'^\s*#\s*endif\b', line):
2443 # End of #if or #else blocks.
2444 if self.pp_stack:
2445 # If we saw an #else, we will need to restore the nesting
2446 # stack to its former state before the #else, otherwise we
2447 # will just continue from where we left off.
2448 if self.pp_stack[-1].seen_else:
2449 # Here we can just use a shallow copy since we are the last
2450 # reference to it.
2451 self.stack = self.pp_stack[-1].stack_before_else
2452 # Drop the corresponding #if
2453 self.pp_stack.pop()
2454 else:
2455 # TODO(unknown): unexpected #endif, issue warning?
2456 pass
2457
avakulenko@google.com02af6282014-06-04 18:53:25 +00002458 # TODO(unknown): Update() is too long, but we will refactor later.
erg@google.comd350fe52013-01-14 17:51:48 +00002459 def Update(self, filename, clean_lines, linenum, error):
2460 """Update nesting state with current line.
2461
2462 Args:
2463 filename: The name of the current file.
2464 clean_lines: A CleansedLines instance containing the file.
2465 linenum: The number of the line to check.
2466 error: The function to call with any errors found.
2467 """
2468 line = clean_lines.elided[linenum]
2469
avakulenko@google.com02af6282014-06-04 18:53:25 +00002470 # Remember top of the previous nesting stack.
2471 #
2472 # The stack is always pushed/popped and not modified in place, so
2473 # we can just do a shallow copy instead of copy.deepcopy. Using
2474 # deepcopy would slow down cpplint by ~28%.
2475 if self.stack:
2476 self.previous_stack_top = self.stack[-1]
2477 else:
2478 self.previous_stack_top = None
2479
2480 # Update pp_stack
erg@google.comd350fe52013-01-14 17:51:48 +00002481 self.UpdatePreprocessor(line)
2482
2483 # Count parentheses. This is to avoid adding struct arguments to
2484 # the nesting stack.
2485 if self.stack:
2486 inner_block = self.stack[-1]
2487 depth_change = line.count('(') - line.count(')')
2488 inner_block.open_parentheses += depth_change
2489
2490 # Also check if we are starting or ending an inline assembly block.
2491 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2492 if (depth_change != 0 and
2493 inner_block.open_parentheses == 1 and
2494 _MATCH_ASM.match(line)):
2495 # Enter assembly block
2496 inner_block.inline_asm = _INSIDE_ASM
2497 else:
2498 # Not entering assembly block. If previous line was _END_ASM,
2499 # we will now shift to _NO_ASM state.
2500 inner_block.inline_asm = _NO_ASM
2501 elif (inner_block.inline_asm == _INSIDE_ASM and
2502 inner_block.open_parentheses == 0):
2503 # Exit assembly block
2504 inner_block.inline_asm = _END_ASM
2505
2506 # Consume namespace declaration at the beginning of the line. Do
2507 # this in a loop so that we catch same line declarations like this:
2508 # namespace proto2 { namespace bridge { class MessageSet; } }
2509 while True:
2510 # Match start of namespace. The "\b\s*" below catches namespace
2511 # declarations even if it weren't followed by a whitespace, this
2512 # is so that we don't confuse our namespace checker. The
2513 # missing spaces will be flagged by CheckSpacing.
2514 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2515 if not namespace_decl_match:
2516 break
2517
2518 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2519 self.stack.append(new_namespace)
2520
2521 line = namespace_decl_match.group(2)
2522 if line.find('{') != -1:
2523 new_namespace.seen_open_brace = True
2524 line = line[line.find('{') + 1:]
2525
2526 # Look for a class declaration in whatever is left of the line
2527 # after parsing namespaces. The regexp accounts for decorated classes
2528 # such as in:
2529 # class LOCKABLE API Object {
2530 # };
erg@google.comd350fe52013-01-14 17:51:48 +00002531 class_decl_match = Match(
avakulenko@google.com02af6282014-06-04 18:53:25 +00002532 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2533 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2534 r'(.*)$', line)
erg@google.comd350fe52013-01-14 17:51:48 +00002535 if (class_decl_match and
2536 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002537 # We do not want to accept classes that are actually template arguments:
2538 # template <class Ignore1,
2539 # class Ignore2 = Default<Args>,
2540 # template <Args> class Ignore3>
2541 # void Function() {};
2542 #
2543 # To avoid template argument cases, we scan forward and look for
2544 # an unmatched '>'. If we see one, assume we are inside a
2545 # template argument list.
2546 end_declaration = len(class_decl_match.group(1))
2547 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2548 self.stack.append(_ClassInfo(
2549 class_decl_match.group(3), class_decl_match.group(2),
2550 clean_lines, linenum))
2551 line = class_decl_match.group(4)
erg@google.comd350fe52013-01-14 17:51:48 +00002552
2553 # If we have not yet seen the opening brace for the innermost block,
2554 # run checks here.
2555 if not self.SeenOpenBrace():
2556 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2557
2558 # Update access control if we are inside a class/struct
2559 if self.stack and isinstance(self.stack[-1], _ClassInfo):
erg@google.comfd5da632013-10-25 17:39:45 +00002560 classinfo = self.stack[-1]
2561 access_match = Match(
2562 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2563 r':(?:[^:]|$)',
2564 line)
erg@google.comd350fe52013-01-14 17:51:48 +00002565 if access_match:
erg@google.comfd5da632013-10-25 17:39:45 +00002566 classinfo.access = access_match.group(2)
2567
2568 # Check that access keywords are indented +1 space. Skip this
erg@google.comc6671232013-10-25 21:44:03 +00002569 # check if the keywords are not preceded by whitespaces.
erg@google.comfd5da632013-10-25 17:39:45 +00002570 indent = access_match.group(1)
2571 if (len(indent) != classinfo.class_indent + 1 and
2572 Match(r'^\s*$', indent)):
2573 if classinfo.is_struct:
2574 parent = 'struct ' + classinfo.name
2575 else:
2576 parent = 'class ' + classinfo.name
2577 slots = ''
2578 if access_match.group(3):
2579 slots = access_match.group(3)
2580 error(filename, linenum, 'whitespace/indent', 3,
2581 '%s%s: should be indented +1 space inside %s' % (
2582 access_match.group(2), slots, parent))
erg@google.comd350fe52013-01-14 17:51:48 +00002583
2584 # Consume braces or semicolons from what's left of the line
2585 while True:
2586 # Match first brace, semicolon, or closed parenthesis.
2587 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2588 if not matched:
2589 break
2590
2591 token = matched.group(1)
2592 if token == '{':
2593 # If namespace or class hasn't seen a opening brace yet, mark
2594 # namespace/class head as complete. Push a new block onto the
2595 # stack otherwise.
2596 if not self.SeenOpenBrace():
2597 self.stack[-1].seen_open_brace = True
avakulenko@google.com02af6282014-06-04 18:53:25 +00002598 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002599 self.stack.append(_ExternCInfo(linenum))
erg@google.comd350fe52013-01-14 17:51:48 +00002600 else:
Alex Vakulenko01e47232016-05-06 13:54:15 -07002601 self.stack.append(_BlockInfo(linenum, True))
erg@google.comd350fe52013-01-14 17:51:48 +00002602 if _MATCH_ASM.match(line):
2603 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.com02af6282014-06-04 18:53:25 +00002604
erg@google.comd350fe52013-01-14 17:51:48 +00002605 elif token == ';' or token == ')':
2606 # If we haven't seen an opening brace yet, but we already saw
2607 # a semicolon, this is probably a forward declaration. Pop
2608 # the stack for these.
2609 #
2610 # Similarly, if we haven't seen an opening brace yet, but we
2611 # already saw a closing parenthesis, then these are probably
2612 # function arguments with extra "class" or "struct" keywords.
2613 # Also pop these stack for these.
2614 if not self.SeenOpenBrace():
2615 self.stack.pop()
2616 else: # token == '}'
2617 # Perform end of block checks and pop the stack.
2618 if self.stack:
2619 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2620 self.stack.pop()
2621 line = matched.group(2)
2622
2623 def InnermostClass(self):
2624 """Get class info on the top of the stack.
2625
2626 Returns:
2627 A _ClassInfo object if we are inside a class, or None otherwise.
2628 """
2629 for i in range(len(self.stack), 0, -1):
2630 classinfo = self.stack[i - 1]
2631 if isinstance(classinfo, _ClassInfo):
2632 return classinfo
2633 return None
2634
erg@google.com2aa59982013-10-28 19:09:25 +00002635 def CheckCompletedBlocks(self, filename, error):
2636 """Checks that all classes and namespaces have been completely parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002637
2638 Call this when all lines in a file have been processed.
2639 Args:
2640 filename: The name of the current file.
2641 error: The function to call with any errors found.
2642 """
erg@google.comd350fe52013-01-14 17:51:48 +00002643 # Note: This test can result in false positives if #ifdef constructs
2644 # get in the way of brace matching. See the testBuildClass test in
2645 # cpplint_unittest.py for an example of this.
2646 for obj in self.stack:
2647 if isinstance(obj, _ClassInfo):
2648 error(filename, obj.starting_linenum, 'build/class', 5,
2649 'Failed to find complete declaration of class %s' %
2650 obj.name)
erg@google.com2aa59982013-10-28 19:09:25 +00002651 elif isinstance(obj, _NamespaceInfo):
2652 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2653 'Failed to find complete declaration of namespace %s' %
2654 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002655
2656
2657def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00002658 nesting_state, error):
erg@google.com2aa59982013-10-28 19:09:25 +00002659 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002660
2661 Complain about several constructs which gcc-2 accepts, but which are
2662 not standard C++. Warning about these in lint is one way to ease the
2663 transition to new compilers.
2664 - put storage class first (e.g. "static const" instead of "const static").
2665 - "%lld" instead of %qd" in printf-type functions.
2666 - "%1$d" is non-standard in printf-type functions.
2667 - "\%" is an undefined character escape sequence.
2668 - text after #endif is not allowed.
2669 - invalid inner-style forward declaration.
2670 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002671
erg@google.coma868d2d2009-10-09 21:18:45 +00002672 Additionally, check for constructor/destructor style violations and reference
2673 members, as it is very convenient to do so while checking for
2674 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002675
2676 Args:
2677 filename: The name of the current file.
2678 clean_lines: A CleansedLines instance containing the file.
2679 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002680 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00002681 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002682 error: A callable to which errors are reported, which takes 4 arguments:
2683 filename, line number, error level, and message
2684 """
2685
Alexandre Ramesf5589442016-09-21 11:03:34 +01002686 line = clean_lines.lines_without_raw_strings[linenum]
2687 if Match(r'\s*#\s*endif\s*([^/\s]|/[^/]|$)', line):
2688 error(filename, linenum, 'build/endif_comment', 5,
2689 'Uncommented text after #endif is non-standard. Use a comment.')
2690
erg@google.com4e00b9a2009-01-12 23:05:11 +00002691 # Remove comments from the line, but leave in strings for now.
2692 line = clean_lines.lines[linenum]
2693
2694 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2695 error(filename, linenum, 'runtime/printf_format', 3,
2696 '%q in format strings is deprecated. Use %ll instead.')
2697
2698 if Search(r'printf\s*\(.*".*%\d+\$', line):
2699 error(filename, linenum, 'runtime/printf_format', 2,
2700 '%N$ formats are unconventional. Try rewriting to avoid them.')
2701
2702 # Remove escaped backslashes before looking for undefined escapes.
2703 line = line.replace('\\\\', '')
2704
2705 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2706 error(filename, linenum, 'build/printf_format', 3,
2707 '%, [, (, and { are undefined character escapes. Unescape them.')
2708
2709 # For the rest, work with both comments and strings removed.
2710 line = clean_lines.elided[linenum]
2711
2712 if Search(r'\b(const|volatile|void|char|short|int|long'
2713 r'|float|double|signed|unsigned'
2714 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00002715 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00002716 line):
2717 error(filename, linenum, 'build/storage_class', 5,
Alex Vakulenko01e47232016-05-06 13:54:15 -07002718 'Storage-class specifier (static, extern, typedef, etc) should be '
2719 'at the beginning of the declaration.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002720
erg@google.com4e00b9a2009-01-12 23:05:11 +00002721 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2722 error(filename, linenum, 'build/forward_decl', 5,
2723 'Inner-style forward declarations are invalid. Remove this line.')
2724
2725 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2726 line):
2727 error(filename, linenum, 'build/deprecated', 3,
2728 '>? and <? (max and min) operators are non-standard and deprecated.')
2729
erg@google.coma868d2d2009-10-09 21:18:45 +00002730 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2731 # TODO(unknown): Could it be expanded safely to arbitrary references,
2732 # without triggering too many false positives? The first
2733 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2734 # the restriction.
2735 # Here's the original regexp, for the reference:
2736 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2737 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2738 error(filename, linenum, 'runtime/member_string_references', 2,
2739 'const string& members are dangerous. It is much better to use '
2740 'alternatives, such as pointers or simple constants.')
2741
erg@google.comd350fe52013-01-14 17:51:48 +00002742 # Everything else in this function operates on class declarations.
2743 # Return early if the top of the nesting stack is not a class, or if
2744 # the class head is not completed yet.
2745 classinfo = nesting_state.InnermostClass()
2746 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002747 return
2748
erg@google.com4e00b9a2009-01-12 23:05:11 +00002749 # The class may have been declared with namespace or classname qualifiers.
2750 # The constructor and destructor will not have those qualifiers.
2751 base_classname = classinfo.name.split('::')[-1]
2752
2753 # Look for single-argument constructors that aren't marked explicit.
Alex Vakulenko01e47232016-05-06 13:54:15 -07002754 # Technically a valid construct, but against style.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002755 explicit_constructor_match = Match(
2756 r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*'
2757 r'\(((?:[^()]|\([^()]*\))*)\)'
2758 % re.escape(base_classname),
2759 line)
2760
2761 if explicit_constructor_match:
2762 is_marked_explicit = explicit_constructor_match.group(1)
2763
2764 if not explicit_constructor_match.group(2):
2765 constructor_args = []
2766 else:
2767 constructor_args = explicit_constructor_match.group(2).split(',')
2768
2769 # collapse arguments so that commas in template parameter lists and function
2770 # argument parameter lists don't split arguments in two
2771 i = 0
2772 while i < len(constructor_args):
2773 constructor_arg = constructor_args[i]
2774 while (constructor_arg.count('<') > constructor_arg.count('>') or
2775 constructor_arg.count('(') > constructor_arg.count(')')):
2776 constructor_arg += ',' + constructor_args[i + 1]
2777 del constructor_args[i + 1]
2778 constructor_args[i] = constructor_arg
2779 i += 1
2780
2781 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2782 noarg_constructor = (not constructor_args or # empty arg list
2783 # 'void' arg specifier
2784 (len(constructor_args) == 1 and
2785 constructor_args[0].strip() == 'void'))
2786 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2787 not noarg_constructor) or
2788 # all but at most one arg defaulted
2789 (len(constructor_args) >= 1 and
2790 not noarg_constructor and
2791 len(defaulted_args) >= len(constructor_args) - 1))
2792 initializer_list_constructor = bool(
2793 onearg_constructor and
2794 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2795 copy_constructor = bool(
2796 onearg_constructor and
2797 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2798 % re.escape(base_classname), constructor_args[0].strip()))
2799
2800 if (not is_marked_explicit and
2801 onearg_constructor and
2802 not initializer_list_constructor and
2803 not copy_constructor):
2804 if defaulted_args:
2805 error(filename, linenum, 'runtime/explicit', 5,
2806 'Constructors callable with one argument '
2807 'should be marked explicit.')
2808 else:
2809 error(filename, linenum, 'runtime/explicit', 5,
2810 'Single-parameter constructors should be marked explicit.')
2811 elif is_marked_explicit and not onearg_constructor:
2812 if noarg_constructor:
2813 error(filename, linenum, 'runtime/explicit', 5,
2814 'Zero-parameter constructors should not be marked explicit.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002815
erg@google.com4e00b9a2009-01-12 23:05:11 +00002816
avakulenko@google.com02af6282014-06-04 18:53:25 +00002817def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002818 """Checks for the correctness of various spacing around function calls.
2819
2820 Args:
2821 filename: The name of the current file.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002822 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002823 linenum: The number of the line to check.
2824 error: The function to call with any errors found.
2825 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00002826 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002827
2828 # Since function calls often occur inside if/for/while/switch
2829 # expressions - which have their own, more liberal conventions - we
2830 # first see if we should be looking inside such an expression for a
2831 # function call, to which we can apply more strict standards.
2832 fncall = line # if there's no control flow construct, look at whole line
2833 for pattern in (r'\bif\s*\((.*)\)\s*{',
2834 r'\bfor\s*\((.*)\)\s*{',
2835 r'\bwhile\s*\((.*)\)\s*[{;]',
2836 r'\bswitch\s*\((.*)\)\s*{'):
2837 match = Search(pattern, line)
2838 if match:
2839 fncall = match.group(1) # look inside the parens for function calls
2840 break
2841
2842 # Except in if/for/while/switch, there should never be space
2843 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2844 # for nested parens ( (a+b) + c ). Likewise, there should never be
2845 # a space before a ( when it's a function argument. I assume it's a
2846 # function argument when the char before the whitespace is legal in
2847 # a function name (alnum + _) and we're not starting a macro. Also ignore
2848 # pointers and references to arrays and functions coz they're too tricky:
2849 # we use a very simple way to recognize these:
2850 # " (something)(maybe-something)" or
2851 # " (something)(maybe-something," or
2852 # " (something)[something]"
2853 # Note that we assume the contents of [] to be short enough that
2854 # they'll never need to wrap.
2855 if ( # Ignore control structures.
erg@google.com2aa59982013-10-28 19:09:25 +00002856 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
erg@google.comc6671232013-10-25 21:44:03 +00002857 fncall) and
erg@google.com4e00b9a2009-01-12 23:05:11 +00002858 # Ignore pointers/references to functions.
2859 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2860 # Ignore pointers/references to arrays.
2861 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00002862 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00002863 error(filename, linenum, 'whitespace/parens', 4,
2864 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00002865 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002866 error(filename, linenum, 'whitespace/parens', 2,
2867 'Extra space after (')
2868 if (Search(r'\w\s+\(', fncall) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07002869 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 +00002870 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
avakulenko@google.com554223d2014-12-04 22:00:20 +00002871 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and
2872 not Search(r'\bcase\s+\(', fncall)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002873 # TODO(unknown): Space after an operator function seem to be a common
2874 # error, silence those for now by restricting them to highest verbosity.
2875 if Search(r'\boperator_*\b', line):
2876 error(filename, linenum, 'whitespace/parens', 0,
2877 'Extra space before ( in function call')
2878 else:
2879 error(filename, linenum, 'whitespace/parens', 4,
2880 'Extra space before ( in function call')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002881 # If the ) is followed only by a newline or a { + newline, assume it's
2882 # part of a control statement (if/while/etc), and don't complain
2883 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002884 # If the closing parenthesis is preceded by only whitespaces,
2885 # try to give a more descriptive error message.
2886 if Search(r'^\s+\)', fncall):
2887 error(filename, linenum, 'whitespace/parens', 2,
2888 'Closing ) should be moved to the previous line')
2889 else:
2890 error(filename, linenum, 'whitespace/parens', 2,
2891 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002892
2893
2894def IsBlankLine(line):
2895 """Returns true if the given line is blank.
2896
2897 We consider a line to be blank if the line is empty or consists of
2898 only white spaces.
2899
2900 Args:
2901 line: A line of a string.
2902
2903 Returns:
2904 True, if the given line is blank.
2905 """
2906 return not line or line.isspace()
2907
2908
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002909def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
2910 error):
2911 is_namespace_indent_item = (
2912 len(nesting_state.stack) > 1 and
2913 nesting_state.stack[-1].check_namespace_indentation and
2914 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
2915 nesting_state.previous_stack_top == nesting_state.stack[-2])
2916
2917 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
2918 clean_lines.elided, line):
2919 CheckItemIndentationInNamespace(filename, clean_lines.elided,
2920 line, error)
2921
2922
erg@google.com4e00b9a2009-01-12 23:05:11 +00002923def CheckForFunctionLengths(filename, clean_lines, linenum,
2924 function_state, error):
2925 """Reports for long function bodies.
2926
2927 For an overview why this is done, see:
Ackermann Yuriy79692902016-04-01 21:41:34 +13002928 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
erg@google.com4e00b9a2009-01-12 23:05:11 +00002929
2930 Uses a simplistic algorithm assuming other style guidelines
2931 (especially spacing) are followed.
2932 Only checks unindented functions, so class members are unchecked.
2933 Trivial bodies are unchecked, so constructors with huge initializer lists
2934 may be missed.
2935 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00002936 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002937 NOLINT *on the last line of a function* disables this check.
2938
2939 Args:
2940 filename: The name of the current file.
2941 clean_lines: A CleansedLines instance containing the file.
2942 linenum: The number of the line to check.
2943 function_state: Current function name and lines in body so far.
2944 error: The function to call with any errors found.
2945 """
2946 lines = clean_lines.lines
2947 line = lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002948 joined_line = ''
2949
2950 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00002951 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00002952 match_result = Match(regexp, line)
2953 if match_result:
2954 # If the name is all caps and underscores, figure it's a macro and
2955 # ignore it, unless it's TEST or TEST_F.
2956 function_name = match_result.group(1).split()[-1]
2957 if function_name == 'TEST' or function_name == 'TEST_F' or (
2958 not Match(r'[A-Z_]+$', function_name)):
2959 starting_func = True
2960
2961 if starting_func:
2962 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00002963 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002964 start_line = lines[start_linenum]
2965 joined_line += ' ' + start_line.lstrip()
2966 if Search(r'(;|})', start_line): # Declarations and trivial functions
2967 body_found = True
2968 break # ... ignore
2969 elif Search(r'{', start_line):
2970 body_found = True
2971 function = Search(r'((\w|:)*)\(', line).group(1)
2972 if Match(r'TEST', function): # Handle TEST... macros
2973 parameter_regexp = Search(r'(\(.*\))', joined_line)
2974 if parameter_regexp: # Ignore bad syntax
2975 function += parameter_regexp.group(1)
2976 else:
2977 function += '()'
2978 function_state.Begin(function)
2979 break
2980 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00002981 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002982 error(filename, linenum, 'readability/fn_size', 5,
2983 'Lint failed to find start of function body.')
2984 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00002985 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002986 function_state.End()
2987 elif not Match(r'^\s*$', line):
2988 function_state.Count() # Count non-blank/non-comment lines.
2989
2990
2991_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
2992
2993
avakulenko@google.com02af6282014-06-04 18:53:25 +00002994def CheckComment(line, filename, linenum, next_line_start, error):
2995 """Checks for common mistakes in comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002996
2997 Args:
avakulenko@google.com02af6282014-06-04 18:53:25 +00002998 line: The line in question.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002999 filename: The name of the current file.
3000 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003001 next_line_start: The first non-whitespace column of the next line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003002 error: The function to call with any errors found.
3003 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00003004 commentpos = line.find('//')
3005 if commentpos != -1:
3006 # Check if the // may be in quotes. If so, ignore it
Alex Vakulenko01e47232016-05-06 13:54:15 -07003007 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003008 # Allow one space for new scopes, two spaces otherwise:
3009 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
3010 ((commentpos >= 1 and
3011 line[commentpos-1] not in string.whitespace) or
3012 (commentpos >= 2 and
3013 line[commentpos-2] not in string.whitespace))):
3014 error(filename, linenum, 'whitespace/comments', 2,
3015 'At least two spaces is best between code and comments')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003016
avakulenko@google.com02af6282014-06-04 18:53:25 +00003017 # Checks for common mistakes in TODO comments.
3018 comment = line[commentpos:]
3019 match = _RE_PATTERN_TODO.match(comment)
3020 if match:
3021 # One whitespace is correct; zero whitespace is handled elsewhere.
3022 leading_whitespace = match.group(1)
3023 if len(leading_whitespace) > 1:
3024 error(filename, linenum, 'whitespace/todo', 2,
3025 'Too many spaces before TODO')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003026
avakulenko@google.com02af6282014-06-04 18:53:25 +00003027 username = match.group(2)
3028 if not username:
3029 error(filename, linenum, 'readability/todo', 2,
3030 'Missing username in TODO; it should look like '
3031 '"// TODO(my_username): Stuff."')
3032
3033 middle_whitespace = match.group(3)
3034 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
3035 if middle_whitespace != ' ' and middle_whitespace != '':
3036 error(filename, linenum, 'whitespace/todo', 2,
3037 'TODO(my_username) should be followed by a space')
3038
3039 # If the comment contains an alphanumeric character, there
avakulenko@google.com554223d2014-12-04 22:00:20 +00003040 # should be a space somewhere between it and the // unless
3041 # it's a /// or //! Doxygen comment.
3042 if (Match(r'//[^ ]*\w', comment) and
3043 not Match(r'(///|//\!)(\s+|$)', comment)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003044 error(filename, linenum, 'whitespace/comments', 4,
3045 'Should have a space between // and comment')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003046
avakulenko@google.com554223d2014-12-04 22:00:20 +00003047
erg@google.comd350fe52013-01-14 17:51:48 +00003048def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
3049 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003050
erg@google.comd350fe52013-01-14 17:51:48 +00003051 Args:
3052 filename: The name of the current file.
3053 clean_lines: A CleansedLines instance containing the file.
3054 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003055 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003056 the current stack of nested blocks being parsed.
3057 error: The function to call with any errors found.
3058 """
3059 line = clean_lines.elided[linenum] # get rid of comments and strings
3060
3061 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
erg@google.comd350fe52013-01-14 17:51:48 +00003062 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
3063 if not matched:
3064 return
3065 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
3066 if nesting_state.stack[-1].access != 'private':
3067 error(filename, linenum, 'readability/constructors', 3,
3068 '%s must be in the private: section' % matched.group(1))
3069
3070 else:
3071 # Found DISALLOW* macro outside a class declaration, or perhaps it
3072 # was used inside a function when it should have been part of the
3073 # class declaration. We could issue a warning here, but it
3074 # probably resulted in a compiler error already.
3075 pass
3076
3077
erg@google.comd350fe52013-01-14 17:51:48 +00003078def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003079 """Checks for the correctness of various spacing issues in the code.
3080
3081 Things we check for: spaces around operators, spaces after
3082 if/for/while/switch, no spaces around parens in function calls, two
3083 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00003084 line, don't end a function with a blank line, don't add a blank line
3085 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003086
3087 Args:
3088 filename: The name of the current file.
3089 clean_lines: A CleansedLines instance containing the file.
3090 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003091 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003092 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003093 error: The function to call with any errors found.
3094 """
3095
erg@google.com2aa59982013-10-28 19:09:25 +00003096 # Don't use "elided" lines here, otherwise we can't check commented lines.
3097 # Don't want to use "raw" either, because we don't want to check inside C++11
3098 # raw strings,
3099 raw = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00003100 line = raw[linenum]
3101
3102 # Before nixing comments, check if the line is blank for no good
3103 # reason. This includes the first line after a block is opened, and
3104 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00003105 #
3106 # Skip all the blank line checks if we are immediately inside a
3107 # namespace body. In other words, don't issue blank line warnings
3108 # for this block:
3109 # namespace {
3110 #
3111 # }
3112 #
3113 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003114 #
3115 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
3116 # like namespaces.
3117 if (IsBlankLine(line) and
3118 not nesting_state.InNamespaceBody() and
3119 not nesting_state.InExternC()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003120 elided = clean_lines.elided
3121 prev_line = elided[linenum - 1]
3122 prevbrace = prev_line.rfind('{')
3123 # TODO(unknown): Don't complain if line before blank line, and line after,
3124 # both start with alnums and are indented the same amount.
3125 # This ignores whitespace at the start of a namespace block
3126 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00003127 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003128 # OK, we have a blank line at the start of a code block. Before we
3129 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00003130 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00003131 # 4 spaces (because they did not fit in a 80 column line when placed on
3132 # the same line as the function name). We also check for the case where
3133 # the previous line is indented 6 spaces, which may happen when the
3134 # initializers of a constructor do not fit into a 80 column line.
3135 exception = False
3136 if Match(r' {6}\w', prev_line): # Initializer list?
3137 # We are looking for the opening column of initializer list, which
3138 # should be indented 4 spaces to cause 6 space indentation afterwards.
3139 search_position = linenum-2
3140 while (search_position >= 0
3141 and Match(r' {6}\w', elided[search_position])):
3142 search_position -= 1
3143 exception = (search_position >= 0
3144 and elided[search_position][:5] == ' :')
3145 else:
3146 # Search for the function arguments or an initializer list. We use a
3147 # simple heuristic here: If the line is indented 4 spaces; and we have a
3148 # closing paren, without the opening paren, followed by an opening brace
3149 # or colon (for initializer lists) we assume that it is the last line of
3150 # a function header. If we have a colon indented 4 spaces, it is an
3151 # initializer list.
3152 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3153 prev_line)
3154 or Match(r' {4}:', prev_line))
3155
3156 if not exception:
3157 error(filename, linenum, 'whitespace/blank_line', 2,
erg@google.com2aa59982013-10-28 19:09:25 +00003158 'Redundant blank line at the start of a code block '
3159 'should be deleted.')
erg@google.comd350fe52013-01-14 17:51:48 +00003160 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00003161 # chain, like this:
3162 # if (condition1) {
3163 # // Something followed by a blank line
3164 #
3165 # } else if (condition2) {
3166 # // Something else
3167 # }
3168 if linenum + 1 < clean_lines.NumLines():
3169 next_line = raw[linenum + 1]
3170 if (next_line
3171 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003172 and next_line.find('} else ') == -1):
3173 error(filename, linenum, 'whitespace/blank_line', 3,
erg@google.com2aa59982013-10-28 19:09:25 +00003174 'Redundant blank line at the end of a code block '
3175 'should be deleted.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003176
erg@google.com8a95ecc2011-09-08 00:45:54 +00003177 matched = Match(r'\s*(public|protected|private):', prev_line)
3178 if matched:
3179 error(filename, linenum, 'whitespace/blank_line', 3,
3180 'Do not leave a blank line after "%s:"' % matched.group(1))
3181
avakulenko@google.com02af6282014-06-04 18:53:25 +00003182 # Next, check comments
3183 next_line_start = 0
3184 if linenum + 1 < clean_lines.NumLines():
3185 next_line = raw[linenum + 1]
3186 next_line_start = len(next_line) - len(next_line.lstrip())
3187 CheckComment(line, filename, linenum, next_line_start, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003188
avakulenko@google.com02af6282014-06-04 18:53:25 +00003189 # get rid of comments and strings
3190 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003191
avakulenko@google.com02af6282014-06-04 18:53:25 +00003192 # You shouldn't have spaces before your brackets, except maybe after
3193 # 'delete []' or 'return []() {};'
3194 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3195 error(filename, linenum, 'whitespace/braces', 5,
3196 'Extra space before [')
3197
3198 # In range-based for, we wanted spaces before and after the colon, but
3199 # not around "::" tokens that might appear.
3200 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3201 Search(r'for *\(.*[^: ]:[^:]', line)):
3202 error(filename, linenum, 'whitespace/forcolon', 2,
3203 'Missing space around colon in range-based for loop')
3204
3205
3206def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3207 """Checks for horizontal spacing around operators.
3208
3209 Args:
3210 filename: The name of the current file.
3211 clean_lines: A CleansedLines instance containing the file.
3212 linenum: The number of the line to check.
3213 error: The function to call with any errors found.
3214 """
3215 line = clean_lines.elided[linenum]
3216
3217 # Don't try to do spacing checks for operator methods. Do this by
3218 # replacing the troublesome characters with something else,
3219 # preserving column position for all other characters.
3220 #
3221 # The replacement is done repeatedly to avoid false positives from
3222 # operators that call operators.
3223 while True:
3224 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3225 if match:
3226 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3227 else:
3228 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00003229
3230 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3231 # Otherwise not. Note we only check for non-spaces on *both* sides;
3232 # sometimes people put non-spaces on one side when aligning ='s among
3233 # many lines (not that this is behavior that I approve of...)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003234 if ((Search(r'[\w.]=', line) or
3235 Search(r'=[\w.]', line))
3236 and not Search(r'\b(if|while|for) ', line)
3237 # Operators taken from [lex.operators] in C++11 standard.
3238 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3239 and not Search(r'operator=', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003240 error(filename, linenum, 'whitespace/operators', 4,
3241 'Missing spaces around =')
3242
3243 # It's ok not to have spaces around binary operators like + - * /, but if
3244 # there's too little whitespace, we get concerned. It's hard to tell,
3245 # though, so we punt on this one for now. TODO.
3246
3247 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00003248 #
3249 # Check <= and >= first to avoid false positives with < and >, then
3250 # check non-include lines for spacing around < and >.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003251 #
3252 # If the operator is followed by a comma, assume it's be used in a
3253 # macro context and don't do any checks. This avoids false
3254 # positives.
3255 #
Alex Vakulenko01e47232016-05-06 13:54:15 -07003256 # Note that && is not included here. This is because there are too
3257 # many false positives due to RValue references.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003258 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003259 if match:
3260 error(filename, linenum, 'whitespace/operators', 3,
3261 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00003262 elif not Match(r'#.*include', line):
erg@google.comd350fe52013-01-14 17:51:48 +00003263 # Look for < that is not surrounded by spaces. This is only
3264 # triggered if both sides are missing spaces, even though
3265 # technically should should flag if at least one side is missing a
3266 # space. This is done to avoid some false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003267 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3268 if match:
3269 (_, _, end_pos) = CloseExpression(
3270 clean_lines, linenum, len(match.group(1)))
3271 if end_pos <= -1:
3272 error(filename, linenum, 'whitespace/operators', 3,
3273 'Missing spaces around <')
erg@google.comd350fe52013-01-14 17:51:48 +00003274
3275 # Look for > that is not surrounded by spaces. Similar to the
3276 # above, we only trigger if both sides are missing spaces to avoid
3277 # false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003278 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3279 if match:
3280 (_, _, start_pos) = ReverseCloseExpression(
3281 clean_lines, linenum, len(match.group(1)))
3282 if start_pos <= -1:
3283 error(filename, linenum, 'whitespace/operators', 3,
3284 'Missing spaces around >')
3285
3286 # We allow no-spaces around << when used like this: 10<<20, but
3287 # not otherwise (particularly, not when used as streams)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003288 #
avakulenko@google.com02af6282014-06-04 18:53:25 +00003289 # We also allow operators following an opening parenthesis, since
3290 # those tend to be macros that deal with operators.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003291 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003292 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00003293 not (match.group(1) == 'operator' and match.group(2) == ';')):
3294 error(filename, linenum, 'whitespace/operators', 3,
3295 'Missing spaces around <<')
erg@google.comd350fe52013-01-14 17:51:48 +00003296
3297 # We allow no-spaces around >> for almost anything. This is because
3298 # C++11 allows ">>" to close nested templates, which accounts for
3299 # most cases when ">>" is not followed by a space.
3300 #
3301 # We still warn on ">>" followed by alpha character, because that is
3302 # likely due to ">>" being used for right shifts, e.g.:
3303 # value >> alpha
3304 #
3305 # When ">>" is used to close templates, the alphanumeric letter that
3306 # follows would be part of an identifier, and there should still be
3307 # a space separating the template type and the identifier.
3308 # type<type<type>> alpha
3309 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003310 if match:
3311 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00003312 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003313
3314 # There shouldn't be space around unary operators
3315 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3316 if match:
3317 error(filename, linenum, 'whitespace/operators', 4,
3318 'Extra space for operator %s' % match.group(1))
3319
avakulenko@google.com02af6282014-06-04 18:53:25 +00003320
3321def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3322 """Checks for horizontal spacing around parentheses.
3323
3324 Args:
3325 filename: The name of the current file.
3326 clean_lines: A CleansedLines instance containing the file.
3327 linenum: The number of the line to check.
3328 error: The function to call with any errors found.
3329 """
3330 line = clean_lines.elided[linenum]
3331
3332 # No spaces after an if, while, switch, or for
erg@google.com4e00b9a2009-01-12 23:05:11 +00003333 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3334 if match:
3335 error(filename, linenum, 'whitespace/parens', 5,
3336 'Missing space before ( in %s' % match.group(1))
3337
3338 # For if/for/while/switch, the left and right parens should be
3339 # consistent about how many spaces are inside the parens, and
3340 # there should either be zero or one spaces inside the parens.
3341 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00003342 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003343 match = Search(r'\b(if|for|while|switch)\s*'
3344 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3345 line)
3346 if match:
3347 if len(match.group(2)) != len(match.group(4)):
3348 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00003349 len(match.group(2)) == 1 + len(match.group(4)) or
3350 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003351 error(filename, linenum, 'whitespace/parens', 5,
3352 'Mismatching spaces inside () in %s' % match.group(1))
erg@google.comc6671232013-10-25 21:44:03 +00003353 if len(match.group(2)) not in [0, 1]:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003354 error(filename, linenum, 'whitespace/parens', 5,
3355 'Should have zero or one spaces inside ( and ) in %s' %
3356 match.group(1))
3357
avakulenko@google.com02af6282014-06-04 18:53:25 +00003358
3359def CheckCommaSpacing(filename, clean_lines, linenum, error):
3360 """Checks for horizontal spacing near commas and semicolons.
3361
3362 Args:
3363 filename: The name of the current file.
3364 clean_lines: A CleansedLines instance containing the file.
3365 linenum: The number of the line to check.
3366 error: The function to call with any errors found.
3367 """
3368 raw = clean_lines.lines_without_raw_strings
3369 line = clean_lines.elided[linenum]
3370
erg@google.com4e00b9a2009-01-12 23:05:11 +00003371 # You should always have a space after a comma (either as fn arg or operator)
erg@google.comc6671232013-10-25 21:44:03 +00003372 #
3373 # This does not apply when the non-space character following the
3374 # comma is another comma, since the only time when that happens is
3375 # for empty macro arguments.
erg@google.com2aa59982013-10-28 19:09:25 +00003376 #
3377 # We run this check in two passes: first pass on elided lines to
3378 # verify that lines contain missing whitespaces, second pass on raw
3379 # lines to confirm that those missing whitespaces are not due to
3380 # elided comments.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003381 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3382 Search(r',[^,\s]', raw[linenum])):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003383 error(filename, linenum, 'whitespace/comma', 3,
3384 'Missing space after ,')
3385
erg@google.comd7d27472011-09-07 17:36:35 +00003386 # You should always have a space after a semicolon
3387 # except for few corner cases
3388 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3389 # space after ;
3390 if Search(r';[^\s};\\)/]', line):
3391 error(filename, linenum, 'whitespace/semicolon', 3,
3392 'Missing space after ;')
3393
avakulenko@google.com02af6282014-06-04 18:53:25 +00003394
Alex Vakulenko01e47232016-05-06 13:54:15 -07003395def _IsType(clean_lines, nesting_state, expr):
3396 """Check if expression looks like a type name, returns true if so.
3397
3398 Args:
3399 clean_lines: A CleansedLines instance containing the file.
3400 nesting_state: A NestingState instance which maintains information about
3401 the current stack of nested blocks being parsed.
3402 expr: The expression to check.
3403 Returns:
3404 True, if token looks like a type.
3405 """
3406 # Keep only the last token in the expression
3407 last_word = Match(r'^.*(\b\S+)$', expr)
3408 if last_word:
3409 token = last_word.group(1)
3410 else:
3411 token = expr
3412
3413 # Match native types and stdint types
3414 if _TYPES.match(token):
3415 return True
3416
3417 # Try a bit harder to match templated types. Walk up the nesting
3418 # stack until we find something that resembles a typename
3419 # declaration for what we are looking for.
3420 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) +
3421 r'\b')
3422 block_index = len(nesting_state.stack) - 1
3423 while block_index >= 0:
3424 if isinstance(nesting_state.stack[block_index], _NamespaceInfo):
3425 return False
3426
3427 # Found where the opening brace is. We want to scan from this
3428 # line up to the beginning of the function, minus a few lines.
3429 # template <typename Type1, // stop scanning here
3430 # ...>
3431 # class C
3432 # : public ... { // start scanning here
3433 last_line = nesting_state.stack[block_index].starting_linenum
3434
3435 next_block_start = 0
3436 if block_index > 0:
3437 next_block_start = nesting_state.stack[block_index - 1].starting_linenum
3438 first_line = last_line
3439 while first_line >= next_block_start:
3440 if clean_lines.elided[first_line].find('template') >= 0:
3441 break
3442 first_line -= 1
3443 if first_line < next_block_start:
3444 # Didn't find any "template" keyword before reaching the next block,
3445 # there are probably no template things to check for this block
3446 block_index -= 1
3447 continue
3448
3449 # Look for typename in the specified range
3450 for i in xrange(first_line, last_line + 1, 1):
3451 if Search(typename_pattern, clean_lines.elided[i]):
3452 return True
3453 block_index -= 1
3454
3455 return False
3456
3457
3458def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003459 """Checks for horizontal spacing near commas.
3460
3461 Args:
3462 filename: The name of the current file.
3463 clean_lines: A CleansedLines instance containing the file.
3464 linenum: The number of the line to check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003465 nesting_state: A NestingState instance which maintains information about
3466 the current stack of nested blocks being parsed.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003467 error: The function to call with any errors found.
3468 """
3469 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003470
erg@google.com8a95ecc2011-09-08 00:45:54 +00003471 # Except after an opening paren, or after another opening brace (in case of
3472 # an initializer list, for instance), you should have spaces before your
Alex Vakulenko01e47232016-05-06 13:54:15 -07003473 # braces when they are delimiting blocks, classes, namespaces etc.
3474 # And since you should never have braces at the beginning of a line,
3475 # this is an easy test. Except that braces used for initialization don't
3476 # follow the same rule; we often don't want spaces before those.
avakulenko@google.com554223d2014-12-04 22:00:20 +00003477 match = Match(r'^(.*[^ ({>]){', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07003478
erg@google.com2aa59982013-10-28 19:09:25 +00003479 if match:
3480 # Try a bit harder to check for brace initialization. This
3481 # happens in one of the following forms:
3482 # Constructor() : initializer_list_{} { ... }
3483 # Constructor{}.MemberFunction()
3484 # Type variable{};
3485 # FunctionCall(type{}, ...);
3486 # LastArgument(..., type{});
3487 # LOG(INFO) << type{} << " ...";
3488 # map_of_type[{...}] = ...;
avakulenko@google.com02af6282014-06-04 18:53:25 +00003489 # ternary = expr ? new type{} : nullptr;
3490 # OuterTemplate<InnerTemplateConstructor<Type>{}>
erg@google.com2aa59982013-10-28 19:09:25 +00003491 #
3492 # We check for the character following the closing brace, and
3493 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003494 # "{.;,)<>]:".
erg@google.com2aa59982013-10-28 19:09:25 +00003495 #
3496 # To account for nested initializer list, we allow any number of
3497 # closing braces up to "{;,)<". We can't simply silence the
3498 # warning on first sight of closing brace, because that would
3499 # cause false negatives for things that are not initializer lists.
3500 # Silence this: But not this:
3501 # Outer{ if (...) {
3502 # Inner{...} if (...){ // Missing space before {
3503 # }; }
3504 #
3505 # There is a false negative with this approach if people inserted
3506 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3507 # spurious semicolon with a separate check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003508 leading_text = match.group(1)
erg@google.com2aa59982013-10-28 19:09:25 +00003509 (endline, endlinenum, endpos) = CloseExpression(
3510 clean_lines, linenum, len(match.group(1)))
3511 trailing_text = ''
3512 if endpos > -1:
3513 trailing_text = endline[endpos:]
3514 for offset in xrange(endlinenum + 1,
3515 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3516 trailing_text += clean_lines.elided[offset]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003517 # We also suppress warnings for `uint64_t{expression}` etc., as the style
3518 # guide recommends brace initialization for integral types to avoid
3519 # overflow/truncation.
3520 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text)
3521 and not _IsType(clean_lines, nesting_state, leading_text)):
erg@google.com2aa59982013-10-28 19:09:25 +00003522 error(filename, linenum, 'whitespace/braces', 5,
3523 'Missing space before {')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003524
3525 # Make sure '} else {' has spaces.
3526 if Search(r'}else', line):
3527 error(filename, linenum, 'whitespace/braces', 5,
3528 'Missing space before else')
3529
erg@google.com4e00b9a2009-01-12 23:05:11 +00003530 # You shouldn't have a space before a semicolon at the end of the line.
3531 # There's a special case for "for" since the style guide allows space before
3532 # the semicolon there.
3533 if Search(r':\s*;\s*$', line):
3534 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00003535 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003536 elif Search(r'^\s*;\s*$', line):
3537 error(filename, linenum, 'whitespace/semicolon', 5,
3538 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00003539 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003540 elif (Search(r'\s+;\s*$', line) and
3541 not Search(r'\bfor\b', line)):
3542 error(filename, linenum, 'whitespace/semicolon', 5,
3543 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00003544 'statement, use {} instead.')
3545
avakulenko@google.com02af6282014-06-04 18:53:25 +00003546
3547def IsDecltype(clean_lines, linenum, column):
3548 """Check if the token ending on (linenum, column) is decltype().
3549
3550 Args:
3551 clean_lines: A CleansedLines instance containing the file.
3552 linenum: the number of the line to check.
3553 column: end column of the token to check.
3554 Returns:
3555 True if this token is decltype() expression, False otherwise.
3556 """
3557 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3558 if start_col < 0:
3559 return False
3560 if Search(r'\bdecltype\s*$', text[0:start_col]):
3561 return True
3562 return False
3563
3564
erg@google.com8a95ecc2011-09-08 00:45:54 +00003565def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3566 """Checks for additional blank line issues related to sections.
3567
3568 Currently the only thing checked here is blank line before protected/private.
3569
3570 Args:
3571 filename: The name of the current file.
3572 clean_lines: A CleansedLines instance containing the file.
3573 class_info: A _ClassInfo objects.
3574 linenum: The number of the line to check.
3575 error: The function to call with any errors found.
3576 """
3577 # Skip checks if the class is small, where small means 25 lines or less.
3578 # 25 lines seems like a good cutoff since that's the usual height of
3579 # terminals, and any class that can't fit in one screen can't really
3580 # be considered "small".
3581 #
3582 # Also skip checks if we are on the first line. This accounts for
3583 # classes that look like
3584 # class Foo { public: ... };
3585 #
3586 # If we didn't find the end of the class, last_line would be zero,
3587 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00003588 if (class_info.last_line - class_info.starting_linenum <= 24 or
3589 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003590 return
3591
3592 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3593 if matched:
3594 # Issue warning if the line before public/protected/private was
3595 # not a blank line, but don't do this if the previous line contains
3596 # "class" or "struct". This can happen two ways:
3597 # - We are at the beginning of the class.
3598 # - We are forward-declaring an inner class that is semantically
3599 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00003600 # Also ignores cases where the previous line ends with a backslash as can be
3601 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003602 prev_line = clean_lines.lines[linenum - 1]
3603 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00003604 not Search(r'\b(class|struct)\b', prev_line) and
3605 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003606 # Try a bit harder to find the beginning of the class. This is to
3607 # account for multi-line base-specifier lists, e.g.:
3608 # class Derived
3609 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00003610 end_class_head = class_info.starting_linenum
3611 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003612 if Search(r'\{\s*$', clean_lines.lines[i]):
3613 end_class_head = i
3614 break
3615 if end_class_head < linenum - 1:
3616 error(filename, linenum, 'whitespace/blank_line', 3,
3617 '"%s:" should be preceded by a blank line' % matched.group(1))
3618
3619
erg@google.com4e00b9a2009-01-12 23:05:11 +00003620def GetPreviousNonBlankLine(clean_lines, linenum):
3621 """Return the most recent non-blank line and its line number.
3622
3623 Args:
3624 clean_lines: A CleansedLines instance containing the file contents.
3625 linenum: The number of the line to check.
3626
3627 Returns:
3628 A tuple with two elements. The first element is the contents of the last
3629 non-blank line before the current line, or the empty string if this is the
3630 first non-blank line. The second is the line number of that line, or -1
3631 if this is the first non-blank line.
3632 """
3633
3634 prevlinenum = linenum - 1
3635 while prevlinenum >= 0:
3636 prevline = clean_lines.elided[prevlinenum]
3637 if not IsBlankLine(prevline): # if not a blank line...
3638 return (prevline, prevlinenum)
3639 prevlinenum -= 1
3640 return ('', -1)
3641
3642
3643def CheckBraces(filename, clean_lines, linenum, error):
3644 """Looks for misplaced braces (e.g. at the end of line).
3645
3646 Args:
3647 filename: The name of the current file.
3648 clean_lines: A CleansedLines instance containing the file.
3649 linenum: The number of the line to check.
3650 error: The function to call with any errors found.
3651 """
3652
3653 line = clean_lines.elided[linenum] # get rid of comments and strings
3654
3655 if Match(r'\s*{\s*$', line):
erg@google.com2aa59982013-10-28 19:09:25 +00003656 # We allow an open brace to start a line in the case where someone is using
3657 # braces in a block to explicitly create a new scope, which is commonly used
3658 # to control the lifetime of stack-allocated variables. Braces are also
3659 # used for brace initializers inside function calls. We don't detect this
3660 # perfectly: we just don't complain if the last non-whitespace character on
3661 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
Alex Vakulenko01e47232016-05-06 13:54:15 -07003662 # previous line starts a preprocessor block. We also allow a brace on the
3663 # following line if it is part of an array initialization and would not fit
3664 # within the 80 character limit of the preceding line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003665 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.com2aa59982013-10-28 19:09:25 +00003666 if (not Search(r'[,;:}{(]\s*$', prevline) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07003667 not Match(r'\s*#', prevline) and
3668 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003669 error(filename, linenum, 'whitespace/braces', 4,
3670 '{ should almost always be at the end of the previous line')
3671
3672 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003673 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003674 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3675 if Match(r'\s*}\s*$', prevline):
3676 error(filename, linenum, 'whitespace/newline', 4,
3677 'An else should appear on the same line as the preceding }')
3678
3679 # If braces come on one side of an else, they should be on both.
3680 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.com02af6282014-06-04 18:53:25 +00003681 if Search(r'else if\s*\(', line): # could be multi-line if
3682 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3683 # find the ( after the if
3684 pos = line.find('else if')
3685 pos = line.find('(', pos)
3686 if pos > 0:
3687 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3688 brace_on_right = endline[endpos:].find('{') != -1
3689 if brace_on_left != brace_on_right: # must be brace after if
3690 error(filename, linenum, 'readability/braces', 5,
3691 'If an else has a brace on one side, it should have it on both')
3692 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3693 error(filename, linenum, 'readability/braces', 5,
3694 'If an else has a brace on one side, it should have it on both')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003695
3696 # Likewise, an else should never have the else clause on the same line
3697 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3698 error(filename, linenum, 'whitespace/newline', 4,
3699 'Else clause should never be on same line as else (use 2 lines)')
3700
3701 # In the same way, a do/while should never be on one line
3702 if Match(r'\s*do [^\s{]', line):
3703 error(filename, linenum, 'whitespace/newline', 4,
3704 'do/while clauses should not be on a single line')
3705
avakulenko@google.com02af6282014-06-04 18:53:25 +00003706 # Check single-line if/else bodies. The style guide says 'curly braces are not
3707 # required for single-line statements'. We additionally allow multi-line,
3708 # single statements, but we reject anything with more than one semicolon in
3709 # it. This means that the first semicolon after the if should be at the end of
3710 # its line, and the line after that should have an indent level equal to or
3711 # lower than the if. We also check for ambiguous if/else nesting without
3712 # braces.
3713 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3714 if if_else_match and not Match(r'\s*#', line):
3715 if_indent = GetIndentLevel(line)
3716 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3717 if_match = Search(r'\bif\s*\(', line)
3718 if if_match:
3719 # This could be a multiline if condition, so find the end first.
3720 pos = if_match.end() - 1
3721 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3722 # Check for an opening brace, either directly after the if or on the next
3723 # line. If found, this isn't a single-statement conditional.
3724 if (not Match(r'\s*{', endline[endpos:])
3725 and not (Match(r'\s*$', endline[endpos:])
3726 and endlinenum < (len(clean_lines.elided) - 1)
3727 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3728 while (endlinenum < len(clean_lines.elided)
3729 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3730 endlinenum += 1
3731 endpos = 0
3732 if endlinenum < len(clean_lines.elided):
3733 endline = clean_lines.elided[endlinenum]
3734 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3735 # methods) and a single \ after the semicolon (for macros)
3736 endpos = endline.find(';')
3737 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003738 # Semicolon isn't the last character, there's something trailing.
3739 # Output a warning if the semicolon is not contained inside
3740 # a lambda expression.
3741 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3742 endline):
3743 error(filename, linenum, 'readability/braces', 4,
3744 'If/else bodies with multiple statements require braces')
avakulenko@google.com02af6282014-06-04 18:53:25 +00003745 elif endlinenum < len(clean_lines.elided) - 1:
3746 # Make sure the next line is dedented
3747 next_line = clean_lines.elided[endlinenum + 1]
3748 next_indent = GetIndentLevel(next_line)
3749 # With ambiguous nested if statements, this will error out on the
3750 # if that *doesn't* match the else, regardless of whether it's the
3751 # inner one or outer one.
3752 if (if_match and Match(r'\s*else\b', next_line)
3753 and next_indent != if_indent):
3754 error(filename, linenum, 'readability/braces', 4,
3755 'Else clause should be indented at the same level as if. '
3756 'Ambiguous nested if/else chains require braces.')
3757 elif next_indent > if_indent:
3758 error(filename, linenum, 'readability/braces', 4,
3759 'If/else bodies with multiple statements require braces')
3760
3761
3762def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3763 """Looks for redundant trailing semicolon.
3764
3765 Args:
3766 filename: The name of the current file.
3767 clean_lines: A CleansedLines instance containing the file.
3768 linenum: The number of the line to check.
3769 error: The function to call with any errors found.
3770 """
3771
3772 line = clean_lines.elided[linenum]
3773
erg@google.com2aa59982013-10-28 19:09:25 +00003774 # Block bodies should not be followed by a semicolon. Due to C++11
3775 # brace initialization, there are more places where semicolons are
3776 # required than not, so we use a whitelist approach to check these
3777 # rather than a blacklist. These are the places where "};" should
3778 # be replaced by just "}":
3779 # 1. Some flavor of block following closing parenthesis:
3780 # for (;;) {};
3781 # while (...) {};
3782 # switch (...) {};
3783 # Function(...) {};
3784 # if (...) {};
3785 # if (...) else if (...) {};
3786 #
3787 # 2. else block:
3788 # if (...) else {};
3789 #
3790 # 3. const member function:
3791 # Function(...) const {};
3792 #
3793 # 4. Block following some statement:
3794 # x = 42;
3795 # {};
3796 #
3797 # 5. Block at the beginning of a function:
3798 # Function(...) {
3799 # {};
3800 # }
3801 #
3802 # Note that naively checking for the preceding "{" will also match
3803 # braces inside multi-dimensional arrays, but this is fine since
3804 # that expression will not contain semicolons.
3805 #
3806 # 6. Block following another block:
3807 # while (true) {}
3808 # {};
3809 #
3810 # 7. End of namespaces:
3811 # namespace {};
3812 #
3813 # These semicolons seems far more common than other kinds of
3814 # redundant semicolons, possibly due to people converting classes
3815 # to namespaces. For now we do not warn for this case.
3816 #
3817 # Try matching case 1 first.
3818 match = Match(r'^(.*\)\s*)\{', line)
3819 if match:
3820 # Matched closing parenthesis (case 1). Check the token before the
3821 # matching opening parenthesis, and don't warn if it looks like a
3822 # macro. This avoids these false positives:
3823 # - macro that defines a base class
3824 # - multi-line macro that defines a base class
3825 # - macro that defines the whole class-head
3826 #
3827 # But we still issue warnings for macros that we know are safe to
3828 # warn, specifically:
3829 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3830 # - TYPED_TEST
3831 # - INTERFACE_DEF
3832 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3833 #
3834 # We implement a whitelist of safe macros instead of a blacklist of
3835 # unsafe macros, even though the latter appears less frequently in
3836 # google code and would have been easier to implement. This is because
3837 # the downside for getting the whitelist wrong means some extra
3838 # semicolons, while the downside for getting the blacklist wrong
3839 # would result in compile errors.
3840 #
avakulenko@google.com554223d2014-12-04 22:00:20 +00003841 # In addition to macros, we also don't want to warn on
3842 # - Compound literals
3843 # - Lambdas
Alex Vakulenko01e47232016-05-06 13:54:15 -07003844 # - alignas specifier with anonymous structs
3845 # - decltype
erg@google.com2aa59982013-10-28 19:09:25 +00003846 closing_brace_pos = match.group(1).rfind(')')
3847 opening_parenthesis = ReverseCloseExpression(
3848 clean_lines, linenum, closing_brace_pos)
3849 if opening_parenthesis[2] > -1:
3850 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003851 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix)
avakulenko@google.com02af6282014-06-04 18:53:25 +00003852 func = Match(r'^(.*\])\s*$', line_prefix)
erg@google.com2aa59982013-10-28 19:09:25 +00003853 if ((macro and
3854 macro.group(1) not in (
3855 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3856 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3857 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.com02af6282014-06-04 18:53:25 +00003858 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
avakulenko@google.com554223d2014-12-04 22:00:20 +00003859 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or
Alex Vakulenko01e47232016-05-06 13:54:15 -07003860 Search(r'\bdecltype$', line_prefix) or
erg@google.com2aa59982013-10-28 19:09:25 +00003861 Search(r'\s+=\s*$', line_prefix)):
3862 match = None
avakulenko@google.com02af6282014-06-04 18:53:25 +00003863 if (match and
3864 opening_parenthesis[1] > 1 and
3865 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3866 # Multi-line lambda-expression
3867 match = None
erg@google.com2aa59982013-10-28 19:09:25 +00003868
3869 else:
3870 # Try matching cases 2-3.
3871 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3872 if not match:
3873 # Try matching cases 4-6. These are always matched on separate lines.
3874 #
3875 # Note that we can't simply concatenate the previous line to the
3876 # current line and do a single match, otherwise we may output
3877 # duplicate warnings for the blank line case:
3878 # if (cond) {
3879 # // blank line
3880 # }
3881 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3882 if prevline and Search(r'[;{}]\s*$', prevline):
3883 match = Match(r'^(\s*)\{', line)
3884
3885 # Check matching closing brace
3886 if match:
3887 (endline, endlinenum, endpos) = CloseExpression(
3888 clean_lines, linenum, len(match.group(1)))
3889 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3890 # Current {} pair is eligible for semicolon check, and we have found
3891 # the redundant semicolon, output warning here.
3892 #
3893 # Note: because we are scanning forward for opening braces, and
3894 # outputting warnings for the matching closing brace, if there are
3895 # nested blocks with trailing semicolons, we will get the error
3896 # messages in reversed order.
Piotr Semenovb7e2ef62016-05-20 18:39:34 +03003897
3898 # We need to check the line forward for NOLINT
3899 raw_lines = clean_lines.raw_lines
3900 ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1,
3901 error)
3902 ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum,
3903 error)
3904
erg@google.com2aa59982013-10-28 19:09:25 +00003905 error(filename, endlinenum, 'readability/braces', 4,
3906 "You don't need a ; after a }")
erg@google.com4e00b9a2009-01-12 23:05:11 +00003907
3908
erg@google.comc6671232013-10-25 21:44:03 +00003909def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3910 """Look for empty loop/conditional body with only a single semicolon.
erg@google.comd350fe52013-01-14 17:51:48 +00003911
3912 Args:
3913 filename: The name of the current file.
3914 clean_lines: A CleansedLines instance containing the file.
3915 linenum: The number of the line to check.
3916 error: The function to call with any errors found.
3917 """
3918
3919 # Search for loop keywords at the beginning of the line. Because only
3920 # whitespaces are allowed before the keywords, this will also ignore most
3921 # do-while-loops, since those lines should start with closing brace.
erg@google.comc6671232013-10-25 21:44:03 +00003922 #
3923 # We also check "if" blocks here, since an empty conditional block
3924 # is likely an error.
erg@google.comd350fe52013-01-14 17:51:48 +00003925 line = clean_lines.elided[linenum]
erg@google.comc6671232013-10-25 21:44:03 +00003926 matched = Match(r'\s*(for|while|if)\s*\(', line)
3927 if matched:
Alex Vakulenko01e47232016-05-06 13:54:15 -07003928 # Find the end of the conditional expression.
erg@google.comd350fe52013-01-14 17:51:48 +00003929 (end_line, end_linenum, end_pos) = CloseExpression(
3930 clean_lines, linenum, line.find('('))
3931
3932 # Output warning if what follows the condition expression is a semicolon.
3933 # No warning for all other cases, including whitespace or newline, since we
3934 # have a separate check for semicolons preceded by whitespace.
3935 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
erg@google.comc6671232013-10-25 21:44:03 +00003936 if matched.group(1) == 'if':
3937 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
3938 'Empty conditional bodies should use {}')
3939 else:
3940 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
3941 'Empty loop bodies should use {} or continue')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003942
Alex Vakulenko01e47232016-05-06 13:54:15 -07003943 # Check for if statements that have completely empty bodies (no comments)
3944 # and no else clauses.
3945 if end_pos >= 0 and matched.group(1) == 'if':
3946 # Find the position of the opening { for the if statement.
3947 # Return without logging an error if it has no brackets.
3948 opening_linenum = end_linenum
3949 opening_line_fragment = end_line[end_pos:]
3950 # Loop until EOF or find anything that's not whitespace or opening {.
3951 while not Search(r'^\s*\{', opening_line_fragment):
3952 if Search(r'^(?!\s*$)', opening_line_fragment):
3953 # Conditional has no brackets.
3954 return
3955 opening_linenum += 1
3956 if opening_linenum == len(clean_lines.elided):
3957 # Couldn't find conditional's opening { or any code before EOF.
3958 return
3959 opening_line_fragment = clean_lines.elided[opening_linenum]
3960 # Set opening_line (opening_line_fragment may not be entire opening line).
3961 opening_line = clean_lines.elided[opening_linenum]
3962
3963 # Find the position of the closing }.
3964 opening_pos = opening_line_fragment.find('{')
3965 if opening_linenum == end_linenum:
3966 # We need to make opening_pos relative to the start of the entire line.
3967 opening_pos += end_pos
3968 (closing_line, closing_linenum, closing_pos) = CloseExpression(
3969 clean_lines, opening_linenum, opening_pos)
3970 if closing_pos < 0:
3971 return
3972
3973 # Now construct the body of the conditional. This consists of the portion
3974 # of the opening line after the {, all lines until the closing line,
3975 # and the portion of the closing line before the }.
3976 if (clean_lines.raw_lines[opening_linenum] !=
3977 CleanseComments(clean_lines.raw_lines[opening_linenum])):
3978 # Opening line ends with a comment, so conditional isn't empty.
3979 return
3980 if closing_linenum > opening_linenum:
3981 # Opening line after the {. Ignore comments here since we checked above.
3982 body = list(opening_line[opening_pos+1:])
3983 # All lines until closing line, excluding closing line, with comments.
3984 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum])
3985 # Closing line before the }. Won't (and can't) have comments.
3986 body.append(clean_lines.elided[closing_linenum][:closing_pos-1])
3987 body = '\n'.join(body)
3988 else:
3989 # If statement has brackets and fits on a single line.
3990 body = opening_line[opening_pos+1:closing_pos-1]
3991
3992 # Check if the body is empty
3993 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body):
3994 return
3995 # The body is empty. Now make sure there's not an else clause.
3996 current_linenum = closing_linenum
3997 current_line_fragment = closing_line[closing_pos:]
3998 # Loop until EOF or find anything that's not whitespace or else clause.
3999 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment):
4000 if Search(r'^(?=\s*else)', current_line_fragment):
4001 # Found an else clause, so don't log an error.
4002 return
4003 current_linenum += 1
4004 if current_linenum == len(clean_lines.elided):
4005 break
4006 current_line_fragment = clean_lines.elided[current_linenum]
4007
4008 # The body is empty and there's no else clause until EOF or other code.
4009 error(filename, end_linenum, 'whitespace/empty_if_body', 4,
4010 ('If statement had no body and no else clause'))
4011
erg@google.com4e00b9a2009-01-12 23:05:11 +00004012
avakulenko@google.com02af6282014-06-04 18:53:25 +00004013def FindCheckMacro(line):
4014 """Find a replaceable CHECK-like macro.
4015
4016 Args:
4017 line: line to search on.
4018 Returns:
4019 (macro name, start position), or (None, -1) if no replaceable
4020 macro is found.
4021 """
4022 for macro in _CHECK_MACROS:
4023 i = line.find(macro)
4024 if i >= 0:
4025 # Find opening parenthesis. Do a regular expression match here
4026 # to make sure that we are matching the expected CHECK macro, as
4027 # opposed to some other macro that happens to contain the CHECK
4028 # substring.
4029 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4030 if not matched:
4031 continue
4032 return (macro, len(matched.group(1)))
4033 return (None, -1)
4034
4035
erg@google.com4e00b9a2009-01-12 23:05:11 +00004036def CheckCheck(filename, clean_lines, linenum, error):
4037 """Checks the use of CHECK and EXPECT macros.
4038
4039 Args:
4040 filename: The name of the current file.
4041 clean_lines: A CleansedLines instance containing the file.
4042 linenum: The number of the line to check.
4043 error: The function to call with any errors found.
4044 """
4045
4046 # Decide the set of replacement macros that should be suggested
erg@google.comc6671232013-10-25 21:44:03 +00004047 lines = clean_lines.elided
avakulenko@google.com02af6282014-06-04 18:53:25 +00004048 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4049 if not check_macro:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004050 return
4051
erg@google.comc6671232013-10-25 21:44:03 +00004052 # Find end of the boolean expression by matching parentheses
4053 (last_line, end_line, end_pos) = CloseExpression(
4054 clean_lines, linenum, start_pos)
4055 if end_pos < 0:
4056 return
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004057
4058 # If the check macro is followed by something other than a
4059 # semicolon, assume users will log their own custom error messages
4060 # and don't suggest any replacements.
4061 if not Match(r'\s*;', last_line[end_pos:]):
4062 return
4063
erg@google.comc6671232013-10-25 21:44:03 +00004064 if linenum == end_line:
4065 expression = lines[linenum][start_pos + 1:end_pos - 1]
4066 else:
4067 expression = lines[linenum][start_pos + 1:]
4068 for i in xrange(linenum + 1, end_line):
4069 expression += lines[i]
4070 expression += last_line[0:end_pos - 1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004071
erg@google.comc6671232013-10-25 21:44:03 +00004072 # Parse expression so that we can take parentheses into account.
4073 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4074 # which is not replaceable by CHECK_LE.
4075 lhs = ''
4076 rhs = ''
4077 operator = None
4078 while expression:
4079 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4080 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4081 if matched:
4082 token = matched.group(1)
4083 if token == '(':
4084 # Parenthesized operand
4085 expression = matched.group(2)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004086 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
erg@google.comc6671232013-10-25 21:44:03 +00004087 if end < 0:
4088 return # Unmatched parenthesis
4089 lhs += '(' + expression[0:end]
4090 expression = expression[end:]
4091 elif token in ('&&', '||'):
4092 # Logical and/or operators. This means the expression
4093 # contains more than one term, for example:
4094 # CHECK(42 < a && a < b);
4095 #
4096 # These are not replaceable with CHECK_LE, so bail out early.
4097 return
4098 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4099 # Non-relational operator
4100 lhs += token
4101 expression = matched.group(2)
4102 else:
4103 # Relational operator
4104 operator = token
4105 rhs = matched.group(2)
4106 break
4107 else:
4108 # Unparenthesized operand. Instead of appending to lhs one character
4109 # at a time, we do another regular expression match to consume several
4110 # characters at once if possible. Trivial benchmark shows that this
4111 # is more efficient when the operands are longer than a single
4112 # character, which is generally the case.
4113 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4114 if not matched:
4115 matched = Match(r'^(\s*\S)(.*)$', expression)
4116 if not matched:
4117 break
4118 lhs += matched.group(1)
4119 expression = matched.group(2)
4120
4121 # Only apply checks if we got all parts of the boolean expression
4122 if not (lhs and operator and rhs):
4123 return
4124
4125 # Check that rhs do not contain logical operators. We already know
4126 # that lhs is fine since the loop above parses out && and ||.
4127 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4128 return
4129
4130 # At least one of the operands must be a constant literal. This is
4131 # to avoid suggesting replacements for unprintable things like
4132 # CHECK(variable != iterator)
4133 #
4134 # The following pattern matches decimal, hex integers, strings, and
4135 # characters (in that order).
4136 lhs = lhs.strip()
4137 rhs = rhs.strip()
4138 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4139 if Match(match_constant, lhs) or Match(match_constant, rhs):
4140 # Note: since we know both lhs and rhs, we can provide a more
4141 # descriptive error message like:
4142 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4143 # Instead of:
4144 # Consider using CHECK_EQ instead of CHECK(a == b)
4145 #
4146 # We are still keeping the less descriptive message because if lhs
4147 # or rhs gets long, the error message might become unreadable.
4148 error(filename, linenum, 'readability/check', 2,
4149 'Consider using %s instead of %s(a %s b)' % (
4150 _CHECK_REPLACEMENT[check_macro][operator],
4151 check_macro, operator))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004152
4153
erg@google.comd350fe52013-01-14 17:51:48 +00004154def CheckAltTokens(filename, clean_lines, linenum, error):
4155 """Check alternative keywords being used in boolean expressions.
4156
4157 Args:
4158 filename: The name of the current file.
4159 clean_lines: A CleansedLines instance containing the file.
4160 linenum: The number of the line to check.
4161 error: The function to call with any errors found.
4162 """
4163 line = clean_lines.elided[linenum]
4164
4165 # Avoid preprocessor lines
4166 if Match(r'^\s*#', line):
4167 return
4168
4169 # Last ditch effort to avoid multi-line comments. This will not help
4170 # if the comment started before the current line or ended after the
4171 # current line, but it catches most of the false positives. At least,
4172 # it provides a way to workaround this warning for people who use
4173 # multi-line comments in preprocessor macros.
4174 #
4175 # TODO(unknown): remove this once cpplint has better support for
4176 # multi-line comments.
4177 if line.find('/*') >= 0 or line.find('*/') >= 0:
4178 return
4179
4180 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4181 error(filename, linenum, 'readability/alt_tokens', 2,
4182 'Use operator %s instead of %s' % (
4183 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4184
4185
erg@google.com4e00b9a2009-01-12 23:05:11 +00004186def GetLineWidth(line):
4187 """Determines the width of the line in column positions.
4188
4189 Args:
4190 line: A string, which may be a Unicode string.
4191
4192 Returns:
4193 The width of the line in column positions, accounting for Unicode
4194 combining characters and wide characters.
4195 """
4196 if isinstance(line, unicode):
4197 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00004198 for uc in unicodedata.normalize('NFC', line):
4199 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004200 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00004201 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004202 width += 1
4203 return width
4204 else:
4205 return len(line)
4206
4207
erg@google.comd350fe52013-01-14 17:51:48 +00004208def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00004209 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004210 """Checks rules from the 'C++ style rules' section of cppguide.html.
4211
4212 Most of these rules are hard to test (naming, comment style), but we
4213 do what we can. In particular we check for 2-space indents, line lengths,
4214 tab usage, spaces inside code, etc.
4215
4216 Args:
4217 filename: The name of the current file.
4218 clean_lines: A CleansedLines instance containing the file.
4219 linenum: The number of the line to check.
4220 file_extension: The extension (without the dot) of the filename.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004221 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00004222 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004223 error: The function to call with any errors found.
4224 """
4225
erg@google.com2aa59982013-10-28 19:09:25 +00004226 # Don't use "elided" lines here, otherwise we can't check commented lines.
4227 # Don't want to use "raw" either, because we don't want to check inside C++11
4228 # raw strings,
4229 raw_lines = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00004230 line = raw_lines[linenum]
Alex Vakulenko01e47232016-05-06 13:54:15 -07004231 prev = raw_lines[linenum - 1] if linenum > 0 else ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00004232
4233 if line.find('\t') != -1:
4234 error(filename, linenum, 'whitespace/tab', 1,
4235 'Tab found; better to use spaces')
4236
4237 # One or three blank spaces at the beginning of the line is weird; it's
4238 # hard to reconcile that with 2-space indents.
4239 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4240 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4241 # if(RLENGTH > 20) complain = 0;
4242 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4243 # if(match(prev, "&& *$")) complain = 0;
4244 # if(match(prev, "\\|\\| *$")) complain = 0;
4245 # if(match(prev, "[\",=><] *$")) complain = 0;
4246 # if(match($0, " <<")) complain = 0;
4247 # if(match(prev, " +for \\(")) complain = 0;
4248 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.com02af6282014-06-04 18:53:25 +00004249 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4250 classinfo = nesting_state.InnermostClass()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004251 initial_spaces = 0
4252 cleansed_line = clean_lines.elided[linenum]
4253 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4254 initial_spaces += 1
avakulenko@google.com02af6282014-06-04 18:53:25 +00004255 # There are certain situations we allow one space, notably for
4256 # section labels, and also lines containing multi-line raw strings.
Alex Vakulenko01e47232016-05-06 13:54:15 -07004257 # We also don't check for lines that look like continuation lines
4258 # (of lines ending in double quotes, commas, equals, or angle brackets)
4259 # because the rules for how to indent those are non-trivial.
4260 if (not Search(r'[",=><] *$', prev) and
4261 (initial_spaces == 1 or initial_spaces == 3) and
4262 not Match(scope_or_label_pattern, cleansed_line) and
4263 not (clean_lines.raw_lines[linenum] != line and
4264 Match(r'^\s*""', line))):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004265 error(filename, linenum, 'whitespace/indent', 3,
4266 'Weird number of spaces at line-start. '
4267 'Are you using a 2-space indent?')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004268
Alex Vakulenko01e47232016-05-06 13:54:15 -07004269 if line and line[-1].isspace():
4270 error(filename, linenum, 'whitespace/end_of_line', 4,
4271 'Line ends in whitespace. Consider deleting these extra spaces.')
4272
erg@google.com4e00b9a2009-01-12 23:05:11 +00004273 # Check if the line is a header guard.
4274 is_header_guard = False
4275 if file_extension == 'h':
4276 cppvar = GetHeaderGuardCPPVariable(filename)
4277 if (line.startswith('#ifndef %s' % cppvar) or
4278 line.startswith('#define %s' % cppvar) or
4279 line.startswith('#endif // %s' % cppvar)):
4280 is_header_guard = True
4281 # #include lines and header guards can be long, since there's no clean way to
4282 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00004283 #
4284 # URLs can be long too. It's possible to split these, but it makes them
4285 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00004286 #
4287 # The "$Id:...$" comment may also get very long without it being the
4288 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00004289 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00004290 not Match(r'^\s*//.*http(s?)://\S*$', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004291 not Match(r'^\s*//\s*[^\s]*$', line) and
erg@google.comd7d27472011-09-07 17:36:35 +00004292 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004293 line_width = GetLineWidth(line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004294 if line_width > _line_length:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004295 error(filename, linenum, 'whitespace/line_length', 2,
erg@google.comab53edf2013-11-05 22:23:37 +00004296 'Lines should be <= %i characters long' % _line_length)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004297
4298 if (cleansed_line.count(';') > 1 and
4299 # for loops are allowed two ;'s (and may run over two lines).
4300 cleansed_line.find('for') == -1 and
4301 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4302 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4303 # It's ok to have many commands in a switch case that fits in 1 line
4304 not ((cleansed_line.find('case ') != -1 or
4305 cleansed_line.find('default:') != -1) and
4306 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00004307 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00004308 'More than one command on the same line')
4309
4310 # Some more style checks
4311 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004312 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
erg@google.comc6671232013-10-25 21:44:03 +00004313 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004314 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
4315 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004316 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4317 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4318 CheckCommaSpacing(filename, clean_lines, linenum, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004319 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004320 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004321 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004322 CheckAltTokens(filename, clean_lines, linenum, error)
4323 classinfo = nesting_state.InnermostClass()
4324 if classinfo:
4325 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004326
4327
erg@google.com4e00b9a2009-01-12 23:05:11 +00004328_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4329# Matches the first component of a filename delimited by -s and _s. That is:
4330# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4331# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4332# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4333# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4334_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4335
4336
4337def _DropCommonSuffixes(filename):
4338 """Drops common suffixes like _test.cc or -inl.h from filename.
4339
4340 For example:
4341 >>> _DropCommonSuffixes('foo/foo-inl.h')
4342 'foo/foo'
4343 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4344 'foo/bar/foo'
4345 >>> _DropCommonSuffixes('foo/foo_internal.h')
4346 'foo/foo'
4347 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4348 'foo/foo_unusualinternal'
4349
4350 Args:
4351 filename: The input filename.
4352
4353 Returns:
4354 The filename with the common suffix removed.
4355 """
4356 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4357 'inl.h', 'impl.h', 'internal.h'):
4358 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4359 filename[-len(suffix) - 1] in ('-', '_')):
4360 return filename[:-len(suffix) - 1]
4361 return os.path.splitext(filename)[0]
4362
4363
erg@google.com4e00b9a2009-01-12 23:05:11 +00004364def _ClassifyInclude(fileinfo, include, is_system):
4365 """Figures out what kind of header 'include' is.
4366
4367 Args:
4368 fileinfo: The current file cpplint is running over. A FileInfo instance.
4369 include: The path to a #included file.
4370 is_system: True if the #include used <> rather than "".
4371
4372 Returns:
4373 One of the _XXX_HEADER constants.
4374
4375 For example:
4376 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4377 _C_SYS_HEADER
4378 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4379 _CPP_SYS_HEADER
4380 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4381 _LIKELY_MY_HEADER
4382 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4383 ... 'bar/foo_other_ext.h', False)
4384 _POSSIBLE_MY_HEADER
4385 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4386 _OTHER_HEADER
4387 """
4388 # This is a list of all standard c++ header files, except
4389 # those already checked for above.
erg@google.comfd5da632013-10-25 17:39:45 +00004390 is_cpp_h = include in _CPP_HEADERS
erg@google.com4e00b9a2009-01-12 23:05:11 +00004391
4392 if is_system:
4393 if is_cpp_h:
4394 return _CPP_SYS_HEADER
4395 else:
4396 return _C_SYS_HEADER
4397
4398 # If the target file and the include we're checking share a
4399 # basename when we drop common extensions, and the include
4400 # lives in . , then it's likely to be owned by the target file.
4401 target_dir, target_base = (
4402 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4403 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4404 if target_base == include_base and (
4405 include_dir == target_dir or
4406 include_dir == os.path.normpath(target_dir + '/../public')):
4407 return _LIKELY_MY_HEADER
4408
4409 # If the target and include share some initial basename
4410 # component, it's possible the target is implementing the
4411 # include, so it's allowed to be first, but we'll never
4412 # complain if it's not there.
4413 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4414 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4415 if (target_first_component and include_first_component and
4416 target_first_component.group(0) ==
4417 include_first_component.group(0)):
4418 return _POSSIBLE_MY_HEADER
4419
4420 return _OTHER_HEADER
4421
4422
erg@google.coma87abb82009-02-24 01:41:01 +00004423
erg@google.come35f7652009-06-19 20:52:09 +00004424def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4425 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004426
erg@google.come35f7652009-06-19 20:52:09 +00004427 Strings on #include lines are NOT removed from elided line, to make
4428 certain tasks easier. However, to prevent false positives, checks
4429 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004430
4431 Args:
4432 filename: The name of the current file.
4433 clean_lines: A CleansedLines instance containing the file.
4434 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004435 include_state: An _IncludeState instance in which the headers are inserted.
4436 error: The function to call with any errors found.
4437 """
4438 fileinfo = FileInfo(filename)
erg@google.come35f7652009-06-19 20:52:09 +00004439 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004440
4441 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004442 # Only do this check if the included header follows google naming
4443 # conventions. If not, assume that it's a 3rd party API that
4444 # requires special include conventions.
4445 #
4446 # We also make an exception for Lua headers, which follow google
4447 # naming convention but not the include convention.
4448 match = Match(r'#include\s*"([^/]+\.h)"', line)
4449 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004450 error(filename, linenum, 'build/include', 4,
4451 'Include the directory when naming .h files')
4452
4453 # we shouldn't include a file more than once. actually, there are a
4454 # handful of instances where doing so is okay, but in general it's
4455 # not.
erg@google.come35f7652009-06-19 20:52:09 +00004456 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004457 if match:
4458 include = match.group(2)
4459 is_system = (match.group(1) == '<')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004460 duplicate_line = include_state.FindHeader(include)
4461 if duplicate_line >= 0:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004462 error(filename, linenum, 'build/include', 4,
4463 '"%s" already included at %s:%s' %
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004464 (include, filename, duplicate_line))
avakulenko@google.com554223d2014-12-04 22:00:20 +00004465 elif (include.endswith('.cc') and
4466 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)):
4467 error(filename, linenum, 'build/include', 4,
4468 'Do not include .cc files from other packages')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004469 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4470 include_state.include_list[-1].append((include, linenum))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004471
4472 # We want to ensure that headers appear in the right order:
4473 # 1) for foo.cc, foo.h (preferred location)
4474 # 2) c system files
4475 # 3) cpp system files
4476 # 4) for foo.cc, foo.h (deprecated location)
4477 # 5) other google headers
4478 #
4479 # We classify each include statement as one of those 5 types
4480 # using a number of techniques. The include_state object keeps
4481 # track of the highest type seen, and complains if we see a
4482 # lower type after that.
4483 error_message = include_state.CheckNextIncludeOrder(
4484 _ClassifyInclude(fileinfo, include, is_system))
4485 if error_message:
4486 error(filename, linenum, 'build/include_order', 4,
4487 '%s. Should be: %s.h, c system, c++ system, other.' %
4488 (error_message, fileinfo.BaseName()))
erg@google.comfd5da632013-10-25 17:39:45 +00004489 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4490 if not include_state.IsInAlphabeticalOrder(
4491 clean_lines, linenum, canonical_include):
erg@google.coma868d2d2009-10-09 21:18:45 +00004492 error(filename, linenum, 'build/include_alpha', 4,
4493 'Include "%s" not in alphabetical order' % include)
erg@google.comfd5da632013-10-25 17:39:45 +00004494 include_state.SetLastHeader(canonical_include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004495
erg@google.come35f7652009-06-19 20:52:09 +00004496
erg@google.com8a95ecc2011-09-08 00:45:54 +00004497
4498def _GetTextInside(text, start_pattern):
erg@google.com2aa59982013-10-28 19:09:25 +00004499 r"""Retrieves all the text between matching open and close parentheses.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004500
4501 Given a string of lines and a regular expression string, retrieve all the text
4502 following the expression and between opening punctuation symbols like
4503 (, [, or {, and the matching close-punctuation symbol. This properly nested
4504 occurrences of the punctuations, so for the text like
4505 printf(a(), b(c()));
4506 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4507 start_pattern must match string having an open punctuation symbol at the end.
4508
4509 Args:
4510 text: The lines to extract text. Its comments and strings must be elided.
4511 It can be single line and can span multiple lines.
4512 start_pattern: The regexp string indicating where to start extracting
4513 the text.
4514 Returns:
4515 The extracted text.
4516 None if either the opening string or ending punctuation could not be found.
4517 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00004518 # TODO(unknown): Audit cpplint.py to see what places could be profitably
erg@google.com8a95ecc2011-09-08 00:45:54 +00004519 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4520
4521 # Give opening punctuations to get the matching close-punctuations.
4522 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4523 closing_punctuation = set(matching_punctuation.itervalues())
4524
4525 # Find the position to start extracting text.
4526 match = re.search(start_pattern, text, re.M)
4527 if not match: # start_pattern not found in text.
4528 return None
4529 start_position = match.end(0)
4530
4531 assert start_position > 0, (
4532 'start_pattern must ends with an opening punctuation.')
4533 assert text[start_position - 1] in matching_punctuation, (
4534 'start_pattern must ends with an opening punctuation.')
4535 # Stack of closing punctuations we expect to have in text after position.
4536 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4537 position = start_position
4538 while punctuation_stack and position < len(text):
4539 if text[position] == punctuation_stack[-1]:
4540 punctuation_stack.pop()
4541 elif text[position] in closing_punctuation:
4542 # A closing punctuation without matching opening punctuations.
4543 return None
4544 elif text[position] in matching_punctuation:
4545 punctuation_stack.append(matching_punctuation[text[position]])
4546 position += 1
4547 if punctuation_stack:
4548 # Opening punctuations left without matching close-punctuations.
4549 return None
4550 # punctuations match.
4551 return text[start_position:position - 1]
4552
4553
erg@google.comfd5da632013-10-25 17:39:45 +00004554# Patterns for matching call-by-reference parameters.
erg@google.com2aa59982013-10-28 19:09:25 +00004555#
4556# Supports nested templates up to 2 levels deep using this messy pattern:
4557# < (?: < (?: < [^<>]*
4558# >
4559# | [^<>] )*
4560# >
4561# | [^<>] )*
4562# >
erg@google.comfd5da632013-10-25 17:39:45 +00004563_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4564_RE_PATTERN_TYPE = (
4565 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
erg@google.com2aa59982013-10-28 19:09:25 +00004566 r'(?:\w|'
4567 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4568 r'::)+')
erg@google.comfd5da632013-10-25 17:39:45 +00004569# A call-by-reference parameter ends with '& identifier'.
4570_RE_PATTERN_REF_PARAM = re.compile(
4571 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4572 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4573# A call-by-const-reference parameter either ends with 'const& identifier'
4574# or looks like 'const type& identifier' when 'type' is atomic.
4575_RE_PATTERN_CONST_REF_PARAM = (
4576 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4577 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004578# Stream types.
4579_RE_PATTERN_REF_STREAM_PARAM = (
4580 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')')
erg@google.comfd5da632013-10-25 17:39:45 +00004581
4582
4583def CheckLanguage(filename, clean_lines, linenum, file_extension,
4584 include_state, nesting_state, error):
erg@google.come35f7652009-06-19 20:52:09 +00004585 """Checks rules from the 'C++ language rules' section of cppguide.html.
4586
4587 Some of these rules are hard to test (function overloading, using
4588 uint32 inappropriately), but we do the best we can.
4589
4590 Args:
4591 filename: The name of the current file.
4592 clean_lines: A CleansedLines instance containing the file.
4593 linenum: The number of the line to check.
4594 file_extension: The extension (without the dot) of the filename.
4595 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004596 nesting_state: A NestingState instance which maintains information about
erg@google.comfd5da632013-10-25 17:39:45 +00004597 the current stack of nested blocks being parsed.
erg@google.come35f7652009-06-19 20:52:09 +00004598 error: The function to call with any errors found.
4599 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00004600 # If the line is empty or consists of entirely a comment, no need to
4601 # check it.
4602 line = clean_lines.elided[linenum]
4603 if not line:
4604 return
4605
erg@google.come35f7652009-06-19 20:52:09 +00004606 match = _RE_PATTERN_INCLUDE.search(line)
4607 if match:
4608 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4609 return
4610
erg@google.com2aa59982013-10-28 19:09:25 +00004611 # Reset include state across preprocessor directives. This is meant
4612 # to silence warnings for conditional includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004613 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4614 if match:
4615 include_state.ResetSection(match.group(1))
erg@google.com2aa59982013-10-28 19:09:25 +00004616
erg@google.com4e00b9a2009-01-12 23:05:11 +00004617 # Make Windows paths like Unix.
4618 fullname = os.path.abspath(filename).replace('\\', '/')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004619
avakulenko@google.com02af6282014-06-04 18:53:25 +00004620 # Perform other checks now that we are sure that this is not an include line
4621 CheckCasts(filename, clean_lines, linenum, error)
4622 CheckGlobalStatic(filename, clean_lines, linenum, error)
4623 CheckPrintf(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004624
4625 if file_extension == 'h':
4626 # TODO(unknown): check that 1-arg constructors are explicit.
4627 # How to tell it's a constructor?
4628 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004629 # TODO(unknown): check that classes declare or disable copy/assign
erg@google.com4e00b9a2009-01-12 23:05:11 +00004630 # (level 1 error)
4631 pass
4632
4633 # Check if people are using the verboten C basic types. The only exception
4634 # we regularly allow is "unsigned short port" for port.
4635 if Search(r'\bshort port\b', line):
4636 if not Search(r'\bunsigned short port\b', line):
4637 error(filename, linenum, 'runtime/int', 4,
4638 'Use "unsigned short" for ports, not "short"')
4639 else:
4640 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4641 if match:
4642 error(filename, linenum, 'runtime/int', 4,
4643 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4644
erg@google.coma868d2d2009-10-09 21:18:45 +00004645 # Check if some verboten operator overloading is going on
4646 # TODO(unknown): catch out-of-line unary operator&:
4647 # class X {};
4648 # int operator&(const X& x) { return 42; } // unary operator&
4649 # The trick is it's hard to tell apart from binary operator&:
4650 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4651 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4652 error(filename, linenum, 'runtime/operator', 4,
4653 'Unary operator& is dangerous. Do not use it.')
4654
erg@google.com4e00b9a2009-01-12 23:05:11 +00004655 # Check for suspicious usage of "if" like
4656 # } if (a == b) {
4657 if Search(r'\}\s*if\s*\(', line):
4658 error(filename, linenum, 'readability/braces', 4,
4659 'Did you mean "else if"? If not, start a new line for "if".')
4660
4661 # Check for potential format string bugs like printf(foo).
4662 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4663 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.com02af6282014-06-04 18:53:25 +00004664 # TODO(unknown): Catch the following case. Need to change the calling
erg@google.com8a95ecc2011-09-08 00:45:54 +00004665 # convention of the whole function to process multiple line to handle it.
4666 # printf(
4667 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4668 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4669 if printf_args:
4670 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00004671 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00004672 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4673 line, re.I).group(1)
4674 error(filename, linenum, 'runtime/printf', 4,
4675 'Potential format string bug. Do %s("%%s", %s) instead.'
4676 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004677
4678 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4679 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4680 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4681 error(filename, linenum, 'runtime/memset', 4,
4682 'Did you mean "memset(%s, 0, %s)"?'
4683 % (match.group(1), match.group(2)))
4684
4685 if Search(r'\busing namespace\b', line):
4686 error(filename, linenum, 'build/namespaces', 5,
4687 'Do not use namespace using-directives. '
4688 'Use using-declarations instead.')
4689
4690 # Detect variable-length arrays.
4691 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4692 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4693 match.group(3).find(']') == -1):
4694 # Split the size using space and arithmetic operators as delimiters.
4695 # If any of the resulting tokens are not compile time constants then
4696 # report the error.
4697 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4698 is_const = True
4699 skip_next = False
4700 for tok in tokens:
4701 if skip_next:
4702 skip_next = False
4703 continue
4704
4705 if Search(r'sizeof\(.+\)', tok): continue
4706 if Search(r'arraysize\(\w+\)', tok): continue
4707
4708 tok = tok.lstrip('(')
4709 tok = tok.rstrip(')')
4710 if not tok: continue
4711 if Match(r'\d+', tok): continue
4712 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4713 if Match(r'k[A-Z0-9]\w*', tok): continue
4714 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4715 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4716 # A catch all for tricky sizeof cases, including 'sizeof expression',
4717 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00004718 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004719 if tok.startswith('sizeof'):
4720 skip_next = True
4721 continue
4722 is_const = False
4723 break
4724 if not is_const:
4725 error(filename, linenum, 'runtime/arrays', 1,
4726 'Do not use variable-length arrays. Use an appropriately named '
4727 "('k' followed by CamelCase) compile-time constant for the size.")
4728
erg@google.com4e00b9a2009-01-12 23:05:11 +00004729 # Check for use of unnamed namespaces in header files. Registration
4730 # macros are typically OK, so we allow use of "namespace {" on lines
4731 # that end with backslashes.
4732 if (file_extension == 'h'
4733 and Search(r'\bnamespace\s*{', line)
4734 and line[-1] != '\\'):
4735 error(filename, linenum, 'build/namespaces', 4,
4736 'Do not use unnamed namespaces in header files. See '
Ackermann Yuriy79692902016-04-01 21:41:34 +13004737 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
erg@google.com4e00b9a2009-01-12 23:05:11 +00004738 ' for more information.')
4739
avakulenko@google.com02af6282014-06-04 18:53:25 +00004740
4741def CheckGlobalStatic(filename, clean_lines, linenum, error):
4742 """Check for unsafe global or static objects.
4743
4744 Args:
4745 filename: The name of the current file.
4746 clean_lines: A CleansedLines instance containing the file.
4747 linenum: The number of the line to check.
4748 error: The function to call with any errors found.
4749 """
4750 line = clean_lines.elided[linenum]
4751
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004752 # Match two lines at a time to support multiline declarations
4753 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4754 line += clean_lines.elided[linenum + 1].strip()
4755
avakulenko@google.com02af6282014-06-04 18:53:25 +00004756 # Check for people declaring static/global STL strings at the top level.
4757 # This is dangerous because the C++ language does not guarantee that
Alex Vakulenko01e47232016-05-06 13:54:15 -07004758 # globals with constructors are initialized before the first access, and
4759 # also because globals can be destroyed when some threads are still running.
4760 # TODO(unknown): Generalize this to also find static unique_ptr instances.
4761 # TODO(unknown): File bugs for clang-tidy to find these.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004762 match = Match(
Alex Vakulenko01e47232016-05-06 13:54:15 -07004763 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +'
4764 r'([a-zA-Z0-9_:]+)\b(.*)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00004765 line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004766
avakulenko@google.com02af6282014-06-04 18:53:25 +00004767 # Remove false positives:
4768 # - String pointers (as opposed to values).
4769 # string *pointer
4770 # const string *pointer
4771 # string const *pointer
4772 # string *const pointer
4773 #
4774 # - Functions and template specializations.
4775 # string Function<Type>(...
4776 # string Class<Type>::Method(...
4777 #
4778 # - Operators. These are matched separately because operator names
4779 # cross non-word boundaries, and trying to match both operators
4780 # and functions at the same time would decrease accuracy of
4781 # matching identifiers.
4782 # string Class::operator*()
4783 if (match and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004784 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00004785 not Search(r'\boperator\W', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004786 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))):
4787 if Search(r'\bconst\b', line):
4788 error(filename, linenum, 'runtime/string', 4,
4789 'For a static/global string constant, use a C style string '
4790 'instead: "%schar%s %s[]".' %
4791 (match.group(1), match.group(2) or '', match.group(3)))
4792 else:
4793 error(filename, linenum, 'runtime/string', 4,
4794 'Static/global string variables are not permitted.')
avakulenko@google.com02af6282014-06-04 18:53:25 +00004795
Alex Vakulenko01e47232016-05-06 13:54:15 -07004796 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or
4797 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004798 error(filename, linenum, 'runtime/init', 4,
4799 'You seem to be initializing a member variable with itself.')
4800
4801
4802def CheckPrintf(filename, clean_lines, linenum, error):
4803 """Check for printf related issues.
4804
4805 Args:
4806 filename: The name of the current file.
4807 clean_lines: A CleansedLines instance containing the file.
4808 linenum: The number of the line to check.
4809 error: The function to call with any errors found.
4810 """
4811 line = clean_lines.elided[linenum]
4812
4813 # When snprintf is used, the second argument shouldn't be a literal.
4814 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4815 if match and match.group(2) != '0':
4816 # If 2nd arg is zero, snprintf is used to calculate size.
4817 error(filename, linenum, 'runtime/printf', 3,
4818 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4819 'to snprintf.' % (match.group(1), match.group(2)))
4820
4821 # Check if some verboten C functions are being used.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004822 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004823 error(filename, linenum, 'runtime/printf', 5,
4824 'Never use sprintf. Use snprintf instead.')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004825 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004826 if match:
4827 error(filename, linenum, 'runtime/printf', 4,
4828 'Almost always, snprintf is better than %s' % match.group(1))
4829
4830
4831def IsDerivedFunction(clean_lines, linenum):
4832 """Check if current line contains an inherited function.
4833
4834 Args:
4835 clean_lines: A CleansedLines instance containing the file.
4836 linenum: The number of the line to check.
4837 Returns:
4838 True if current line contains a function with "override"
4839 virt-specifier.
4840 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004841 # Scan back a few lines for start of current function
4842 for i in xrange(linenum, max(-1, linenum - 10), -1):
4843 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4844 if match:
4845 # Look for "override" after the matching closing parenthesis
4846 line, _, closing_paren = CloseExpression(
4847 clean_lines, i, len(match.group(1)))
4848 return (closing_paren >= 0 and
4849 Search(r'\boverride\b', line[closing_paren:]))
4850 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00004851
4852
avakulenko@google.com554223d2014-12-04 22:00:20 +00004853def IsOutOfLineMethodDefinition(clean_lines, linenum):
4854 """Check if current line contains an out-of-line method definition.
4855
4856 Args:
4857 clean_lines: A CleansedLines instance containing the file.
4858 linenum: The number of the line to check.
4859 Returns:
4860 True if current line contains an out-of-line method definition.
4861 """
4862 # Scan back a few lines for start of current function
4863 for i in xrange(linenum, max(-1, linenum - 10), -1):
4864 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
4865 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
4866 return False
4867
4868
avakulenko@google.com02af6282014-06-04 18:53:25 +00004869def IsInitializerList(clean_lines, linenum):
4870 """Check if current line is inside constructor initializer list.
4871
4872 Args:
4873 clean_lines: A CleansedLines instance containing the file.
4874 linenum: The number of the line to check.
4875 Returns:
4876 True if current line appears to be inside constructor initializer
4877 list, False otherwise.
4878 """
4879 for i in xrange(linenum, 1, -1):
4880 line = clean_lines.elided[i]
4881 if i == linenum:
4882 remove_function_body = Match(r'^(.*)\{\s*$', line)
4883 if remove_function_body:
4884 line = remove_function_body.group(1)
4885
4886 if Search(r'\s:\s*\w+[({]', line):
4887 # A lone colon tend to indicate the start of a constructor
4888 # initializer list. It could also be a ternary operator, which
4889 # also tend to appear in constructor initializer lists as
4890 # opposed to parameter lists.
4891 return True
4892 if Search(r'\}\s*,\s*$', line):
4893 # A closing brace followed by a comma is probably the end of a
4894 # brace-initialized member in constructor initializer list.
4895 return True
4896 if Search(r'[{};]\s*$', line):
4897 # Found one of the following:
4898 # - A closing brace or semicolon, probably the end of the previous
4899 # function.
4900 # - An opening brace, probably the start of current class or namespace.
4901 #
4902 # Current line is probably not inside an initializer list since
4903 # we saw one of those things without seeing the starting colon.
4904 return False
4905
4906 # Got to the beginning of the file without seeing the start of
4907 # constructor initializer list.
4908 return False
4909
4910
erg@google.comc6671232013-10-25 21:44:03 +00004911def CheckForNonConstReference(filename, clean_lines, linenum,
4912 nesting_state, error):
4913 """Check for non-const references.
4914
4915 Separate from CheckLanguage since it scans backwards from current
4916 line, instead of scanning forward.
4917
4918 Args:
4919 filename: The name of the current file.
4920 clean_lines: A CleansedLines instance containing the file.
4921 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004922 nesting_state: A NestingState instance which maintains information about
erg@google.comc6671232013-10-25 21:44:03 +00004923 the current stack of nested blocks being parsed.
4924 error: The function to call with any errors found.
4925 """
4926 # Do nothing if there is no '&' on current line.
4927 line = clean_lines.elided[linenum]
4928 if '&' not in line:
4929 return
4930
avakulenko@google.com02af6282014-06-04 18:53:25 +00004931 # If a function is inherited, current function doesn't have much of
4932 # a choice, so any non-const references should not be blamed on
4933 # derived function.
4934 if IsDerivedFunction(clean_lines, linenum):
4935 return
4936
avakulenko@google.com554223d2014-12-04 22:00:20 +00004937 # Don't warn on out-of-line method definitions, as we would warn on the
4938 # in-line declaration, if it isn't marked with 'override'.
4939 if IsOutOfLineMethodDefinition(clean_lines, linenum):
4940 return
4941
erg@google.com2aa59982013-10-28 19:09:25 +00004942 # Long type names may be broken across multiple lines, usually in one
4943 # of these forms:
4944 # LongType
4945 # ::LongTypeContinued &identifier
4946 # LongType::
4947 # LongTypeContinued &identifier
4948 # LongType<
4949 # ...>::LongTypeContinued &identifier
4950 #
4951 # If we detected a type split across two lines, join the previous
4952 # line to current line so that we can match const references
4953 # accordingly.
erg@google.comc6671232013-10-25 21:44:03 +00004954 #
4955 # Note that this only scans back one line, since scanning back
4956 # arbitrary number of lines would be expensive. If you have a type
4957 # that spans more than 2 lines, please use a typedef.
4958 if linenum > 1:
4959 previous = None
erg@google.com2aa59982013-10-28 19:09:25 +00004960 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004961 # previous_line\n + ::current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004962 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004963 clean_lines.elided[linenum - 1])
erg@google.com2aa59982013-10-28 19:09:25 +00004964 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004965 # previous_line::\n + current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004966 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004967 clean_lines.elided[linenum - 1])
4968 if previous:
4969 line = previous.group(1) + line.lstrip()
erg@google.com2aa59982013-10-28 19:09:25 +00004970 else:
4971 # Check for templated parameter that is split across multiple lines
4972 endpos = line.rfind('>')
4973 if endpos > -1:
4974 (_, startline, startpos) = ReverseCloseExpression(
4975 clean_lines, linenum, endpos)
4976 if startpos > -1 and startline < linenum:
4977 # Found the matching < on an earlier line, collect all
4978 # pieces up to current line.
4979 line = ''
4980 for i in xrange(startline, linenum + 1):
4981 line += clean_lines.elided[i].strip()
erg@google.comc6671232013-10-25 21:44:03 +00004982
4983 # Check for non-const references in function parameters. A single '&' may
4984 # found in the following places:
4985 # inside expression: binary & for bitwise AND
4986 # inside expression: unary & for taking the address of something
4987 # inside declarators: reference parameter
4988 # We will exclude the first two cases by checking that we are not inside a
4989 # function body, including one that was just introduced by a trailing '{'.
erg@google.comc6671232013-10-25 21:44:03 +00004990 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.com02af6282014-06-04 18:53:25 +00004991 if (nesting_state.previous_stack_top and
4992 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
4993 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
4994 # Not at toplevel, not within a class, and not within a namespace
4995 return
4996
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004997 # Avoid initializer lists. We only need to scan back from the
4998 # current line for something that starts with ':'.
4999 #
5000 # We don't need to check the current line, since the '&' would
5001 # appear inside the second set of parentheses on the current line as
5002 # opposed to the first set.
5003 if linenum > 0:
5004 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5005 previous_line = clean_lines.elided[i]
5006 if not Search(r'[),]\s*$', previous_line):
5007 break
5008 if Match(r'^\s*:\s+\S', previous_line):
5009 return
5010
avakulenko@google.com02af6282014-06-04 18:53:25 +00005011 # Avoid preprocessors
5012 if Search(r'\\\s*$', line):
5013 return
5014
5015 # Avoid constructor initializer lists
5016 if IsInitializerList(clean_lines, linenum):
5017 return
5018
erg@google.comc6671232013-10-25 21:44:03 +00005019 # We allow non-const references in a few standard places, like functions
5020 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5021 # those function parameters.
5022 #
5023 # We also accept & in static_assert, which looks like a function but
5024 # it's actually a declaration expression.
5025 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5026 r'operator\s*[<>][<>]|'
5027 r'static_assert|COMPILE_ASSERT'
5028 r')\s*\(')
5029 if Search(whitelisted_functions, line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005030 return
erg@google.comc6671232013-10-25 21:44:03 +00005031 elif not Search(r'\S+\([^)]*$', line):
5032 # Don't see a whitelisted function on this line. Actually we
5033 # didn't see any function name on this line, so this is likely a
5034 # multi-line parameter list. Try a bit harder to catch this case.
5035 for i in xrange(2):
5036 if (linenum > i and
5037 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005038 return
erg@google.comc6671232013-10-25 21:44:03 +00005039
avakulenko@google.com02af6282014-06-04 18:53:25 +00005040 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5041 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005042 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and
5043 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005044 error(filename, linenum, 'runtime/references', 2,
5045 'Is this a non-const reference? '
5046 'If so, make const or use a pointer: ' +
5047 ReplaceAll(' *<', '<', parameter))
5048
5049
5050def CheckCasts(filename, clean_lines, linenum, error):
5051 """Various cast related checks.
5052
5053 Args:
5054 filename: The name of the current file.
5055 clean_lines: A CleansedLines instance containing the file.
5056 linenum: The number of the line to check.
5057 error: The function to call with any errors found.
5058 """
5059 line = clean_lines.elided[linenum]
5060
5061 # Check to see if they're using an conversion function cast.
5062 # I just try to capture the most common basic types, though there are more.
5063 # Parameterless conversion functions, such as bool(), are allowed as they are
5064 # probably a member operator declaration or default constructor.
5065 match = Search(
Alex Vakulenko01e47232016-05-06 13:54:15 -07005066 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
avakulenko@google.com02af6282014-06-04 18:53:25 +00005067 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5068 r'(\([^)].*)', line)
5069 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5070 if match and not expecting_function:
5071 matched_type = match.group(2)
5072
5073 # matched_new_or_template is used to silence two false positives:
5074 # - New operators
5075 # - Template arguments with function types
5076 #
5077 # For template arguments, we match on types immediately following
5078 # an opening bracket without any spaces. This is a fast way to
5079 # silence the common case where the function type is the first
5080 # template argument. False negative with less-than comparison is
5081 # avoided because those operators are usually followed by a space.
5082 #
5083 # function<double(double)> // bracket + no space = false positive
5084 # value < double(42) // bracket + space = true positive
5085 matched_new_or_template = match.group(1)
5086
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005087 # Avoid arrays by looking for brackets that come after the closing
5088 # parenthesis.
5089 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5090 return
5091
avakulenko@google.com02af6282014-06-04 18:53:25 +00005092 # Other things to ignore:
5093 # - Function pointers
5094 # - Casts to pointer types
5095 # - Placement new
5096 # - Alias declarations
5097 matched_funcptr = match.group(3)
5098 if (matched_new_or_template is None and
5099 not (matched_funcptr and
5100 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5101 matched_funcptr) or
5102 matched_funcptr.startswith('(*)'))) and
5103 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5104 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5105 error(filename, linenum, 'readability/casting', 4,
5106 'Using deprecated casting style. '
5107 'Use static_cast<%s>(...) instead' %
5108 matched_type)
5109
5110 if not expecting_function:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005111 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005112 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5113
5114 # This doesn't catch all cases. Consider (const char * const)"hello".
5115 #
5116 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5117 # compile).
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005118 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5119 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005120 pass
5121 else:
5122 # Check pointer casts for other than string constants
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005123 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5124 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005125
5126 # In addition, we look for people taking the address of a cast. This
5127 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5128 # point where you think.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005129 #
5130 # Some non-identifier character is required before the '&' for the
5131 # expression to be recognized as a cast. These are casts:
5132 # expression = &static_cast<int*>(temporary());
5133 # function(&(int*)(temporary()));
5134 #
5135 # This is not a cast:
5136 # reference_type&(int* function_param);
avakulenko@google.com02af6282014-06-04 18:53:25 +00005137 match = Search(
avakulenko@google.com554223d2014-12-04 22:00:20 +00005138 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005139 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005140 if match:
avakulenko@google.com02af6282014-06-04 18:53:25 +00005141 # Try a better error message when the & is bound to something
5142 # dereferenced by the casted pointer, as opposed to the casted
5143 # pointer itself.
5144 parenthesis_error = False
5145 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5146 if match:
5147 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5148 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5149 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5150 if x2 >= 0:
5151 extended_line = clean_lines.elided[y2][x2:]
5152 if y2 < clean_lines.NumLines() - 1:
5153 extended_line += clean_lines.elided[y2 + 1]
5154 if Match(r'\s*(?:->|\[)', extended_line):
5155 parenthesis_error = True
5156
5157 if parenthesis_error:
5158 error(filename, linenum, 'readability/casting', 4,
5159 ('Are you taking an address of something dereferenced '
5160 'from a cast? Wrapping the dereferenced expression in '
5161 'parentheses will make the binding more obvious'))
5162 else:
5163 error(filename, linenum, 'runtime/casting', 4,
5164 ('Are you taking an address of a cast? '
5165 'This is dangerous: could be a temp var. '
5166 'Take the address before doing the cast, rather than after'))
erg@google.comc6671232013-10-25 21:44:03 +00005167
erg@google.com4e00b9a2009-01-12 23:05:11 +00005168
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005169def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005170 """Checks for a C-style cast by looking for the pattern.
5171
erg@google.com4e00b9a2009-01-12 23:05:11 +00005172 Args:
5173 filename: The name of the current file.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005174 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005175 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005176 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00005177 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005178 pattern: The regular expression used to find C-style casts.
5179 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005180
5181 Returns:
5182 True if an error was emitted.
5183 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005184 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005185 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00005186 match = Search(pattern, line)
5187 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00005188 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005189
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005190 # Exclude lines with keywords that tend to look like casts
5191 context = line[0:match.start(1) - 1]
5192 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5193 return False
5194
5195 # Try expanding current context to see if we one level of
5196 # parentheses inside a macro.
5197 if linenum > 0:
5198 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5199 context = clean_lines.elided[i] + context
5200 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
erg@google.comfd5da632013-10-25 17:39:45 +00005201 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005202
erg@google.comd350fe52013-01-14 17:51:48 +00005203 # operator++(int) and operator--(int)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005204 if context.endswith(' operator++') or context.endswith(' operator--'):
erg@google.comd350fe52013-01-14 17:51:48 +00005205 return False
5206
Alex Vakulenko01e47232016-05-06 13:54:15 -07005207 # A single unnamed argument for a function tends to look like old style cast.
5208 # If we see those, don't issue warnings for deprecated casts.
erg@google.comc6671232013-10-25 21:44:03 +00005209 remainder = line[match.end(0):]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005210 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005211 remainder):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005212 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005213
5214 # At this point, all that should be left is actual casts.
5215 error(filename, linenum, 'readability/casting', 4,
5216 'Using C-style cast. Use %s<%s>(...) instead' %
5217 (cast_type, match.group(1)))
5218
erg@google.com8a95ecc2011-09-08 00:45:54 +00005219 return True
5220
erg@google.com4e00b9a2009-01-12 23:05:11 +00005221
avakulenko@google.com02af6282014-06-04 18:53:25 +00005222def ExpectingFunctionArgs(clean_lines, linenum):
5223 """Checks whether where function type arguments are expected.
5224
5225 Args:
5226 clean_lines: A CleansedLines instance containing the file.
5227 linenum: The number of the line to check.
5228
5229 Returns:
5230 True if the line at 'linenum' is inside something that expects arguments
5231 of function types.
5232 """
5233 line = clean_lines.elided[linenum]
5234 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5235 (linenum >= 2 and
5236 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5237 clean_lines.elided[linenum - 1]) or
5238 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5239 clean_lines.elided[linenum - 2]) or
5240 Search(r'\bstd::m?function\s*\<\s*$',
5241 clean_lines.elided[linenum - 1]))))
5242
5243
erg@google.com4e00b9a2009-01-12 23:05:11 +00005244_HEADERS_CONTAINING_TEMPLATES = (
5245 ('<deque>', ('deque',)),
5246 ('<functional>', ('unary_function', 'binary_function',
5247 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5248 'negate',
5249 'equal_to', 'not_equal_to', 'greater', 'less',
5250 'greater_equal', 'less_equal',
5251 'logical_and', 'logical_or', 'logical_not',
5252 'unary_negate', 'not1', 'binary_negate', 'not2',
5253 'bind1st', 'bind2nd',
5254 'pointer_to_unary_function',
5255 'pointer_to_binary_function',
5256 'ptr_fun',
5257 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5258 'mem_fun_ref_t',
5259 'const_mem_fun_t', 'const_mem_fun1_t',
5260 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5261 'mem_fun_ref',
5262 )),
5263 ('<limits>', ('numeric_limits',)),
5264 ('<list>', ('list',)),
5265 ('<map>', ('map', 'multimap',)),
lhchavez2890dff2016-07-11 19:37:29 -07005266 ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr',
5267 'unique_ptr', 'weak_ptr')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005268 ('<queue>', ('queue', 'priority_queue',)),
5269 ('<set>', ('set', 'multiset',)),
5270 ('<stack>', ('stack',)),
5271 ('<string>', ('char_traits', 'basic_string',)),
avakulenko@google.com554223d2014-12-04 22:00:20 +00005272 ('<tuple>', ('tuple',)),
lhchavez2890dff2016-07-11 19:37:29 -07005273 ('<unordered_map>', ('unordered_map', 'unordered_multimap')),
5274 ('<unordered_set>', ('unordered_set', 'unordered_multiset')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005275 ('<utility>', ('pair',)),
5276 ('<vector>', ('vector',)),
5277
5278 # gcc extensions.
5279 # Note: std::hash is their hash, ::hash is our hash
5280 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5281 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5282 ('<slist>', ('slist',)),
5283 )
5284
Alex Vakulenko01e47232016-05-06 13:54:15 -07005285_HEADERS_MAYBE_TEMPLATES = (
5286 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort',
5287 'transform',
5288 )),
lhchavez2890dff2016-07-11 19:37:29 -07005289 ('<utility>', ('forward', 'make_pair', 'move', 'swap')),
Alex Vakulenko01e47232016-05-06 13:54:15 -07005290 )
5291
erg@google.com4e00b9a2009-01-12 23:05:11 +00005292_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5293
Alex Vakulenko01e47232016-05-06 13:54:15 -07005294_re_pattern_headers_maybe_templates = []
5295for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
5296 for _template in _templates:
5297 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5298 # type::max().
5299 _re_pattern_headers_maybe_templates.append(
5300 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5301 _template,
5302 _header))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005303
Alex Vakulenko01e47232016-05-06 13:54:15 -07005304# Other scripts may reach in and modify this pattern.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005305_re_pattern_templates = []
5306for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5307 for _template in _templates:
5308 _re_pattern_templates.append(
5309 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5310 _template + '<>',
5311 _header))
5312
5313
erg@google.come35f7652009-06-19 20:52:09 +00005314def FilesBelongToSameModule(filename_cc, filename_h):
5315 """Check if these two filenames belong to the same module.
5316
5317 The concept of a 'module' here is a as follows:
5318 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5319 same 'module' if they are in the same directory.
5320 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5321 to belong to the same module here.
5322
5323 If the filename_cc contains a longer path than the filename_h, for example,
5324 '/absolute/path/to/base/sysinfo.cc', and this file would include
5325 'base/sysinfo.h', this function also produces the prefix needed to open the
5326 header. This is used by the caller of this function to more robustly open the
5327 header file. We don't have access to the real include paths in this context,
5328 so we need this guesswork here.
5329
5330 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5331 according to this implementation. Because of this, this function gives
5332 some false positives. This should be sufficiently rare in practice.
5333
5334 Args:
5335 filename_cc: is the path for the .cc file
5336 filename_h: is the path for the header path
5337
5338 Returns:
5339 Tuple with a bool and a string:
5340 bool: True if filename_cc and filename_h belong to the same module.
5341 string: the additional prefix needed to open the header file.
5342 """
5343
Alex Vakulenko01e47232016-05-06 13:54:15 -07005344 fileinfo = FileInfo(filename_cc)
5345 if not fileinfo.IsSource():
erg@google.come35f7652009-06-19 20:52:09 +00005346 return (False, '')
Alex Vakulenko01e47232016-05-06 13:54:15 -07005347 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5348 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5349 if matched_test_suffix:
5350 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
erg@google.come35f7652009-06-19 20:52:09 +00005351 filename_cc = filename_cc.replace('/public/', '/')
5352 filename_cc = filename_cc.replace('/internal/', '/')
5353
5354 if not filename_h.endswith('.h'):
5355 return (False, '')
5356 filename_h = filename_h[:-len('.h')]
5357 if filename_h.endswith('-inl'):
5358 filename_h = filename_h[:-len('-inl')]
5359 filename_h = filename_h.replace('/public/', '/')
5360 filename_h = filename_h.replace('/internal/', '/')
5361
5362 files_belong_to_same_module = filename_cc.endswith(filename_h)
5363 common_path = ''
5364 if files_belong_to_same_module:
5365 common_path = filename_cc[:-len(filename_h)]
5366 return files_belong_to_same_module, common_path
5367
5368
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005369def UpdateIncludeState(filename, include_dict, io=codecs):
5370 """Fill up the include_dict with new includes found from the file.
erg@google.come35f7652009-06-19 20:52:09 +00005371
5372 Args:
5373 filename: the name of the header to read.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005374 include_dict: a dictionary in which the headers are inserted.
erg@google.come35f7652009-06-19 20:52:09 +00005375 io: The io factory to use to read the file. Provided for testability.
5376
5377 Returns:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005378 True if a header was successfully added. False otherwise.
erg@google.come35f7652009-06-19 20:52:09 +00005379 """
5380 headerfile = None
5381 try:
5382 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5383 except IOError:
5384 return False
5385 linenum = 0
5386 for line in headerfile:
5387 linenum += 1
5388 clean_line = CleanseComments(line)
5389 match = _RE_PATTERN_INCLUDE.search(clean_line)
5390 if match:
5391 include = match.group(2)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005392 include_dict.setdefault(include, linenum)
erg@google.come35f7652009-06-19 20:52:09 +00005393 return True
5394
5395
5396def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5397 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005398 """Reports for missing stl includes.
5399
5400 This function will output warnings to make sure you are including the headers
5401 necessary for the stl containers and functions that you use. We only give one
5402 reason to include a header. For example, if you use both equal_to<> and
5403 less<> in a .h file, only one (the latter in the file) of these will be
5404 reported as a reason to include the <functional>.
5405
erg@google.com4e00b9a2009-01-12 23:05:11 +00005406 Args:
5407 filename: The name of the current file.
5408 clean_lines: A CleansedLines instance containing the file.
5409 include_state: An _IncludeState instance.
5410 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00005411 io: The IO factory to use to read the header file. Provided for unittest
5412 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005413 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00005414 required = {} # A map of header name to linenumber and the template entity.
5415 # Example of required: { '<functional>': (1219, 'less<>') }
5416
5417 for linenum in xrange(clean_lines.NumLines()):
5418 line = clean_lines.elided[linenum]
5419 if not line or line[0] == '#':
5420 continue
5421
5422 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005423 matched = _RE_PATTERN_STRING.search(line)
5424 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00005425 # Don't warn about strings in non-STL namespaces:
5426 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005427 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00005428 if prefix.endswith('std::') or not prefix.endswith('::'):
5429 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00005430
Alex Vakulenko01e47232016-05-06 13:54:15 -07005431 for pattern, template, header in _re_pattern_headers_maybe_templates:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005432 if pattern.search(line):
5433 required[header] = (linenum, template)
5434
5435 # The following function is just a speed up, no semantics are changed.
5436 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5437 continue
5438
5439 for pattern, template, header in _re_pattern_templates:
lhchavez3ae81f12016-07-11 19:00:34 -07005440 matched = pattern.search(line)
5441 if matched:
5442 # Don't warn about IWYU in non-STL namespaces:
5443 # (We check only the first match per line; good enough.)
5444 prefix = line[:matched.start()]
5445 if prefix.endswith('std::') or not prefix.endswith('::'):
5446 required[header] = (linenum, template)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005447
erg@google.come35f7652009-06-19 20:52:09 +00005448 # The policy is that if you #include something in foo.h you don't need to
5449 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005450 # Let's flatten the include_state include_list and copy it into a dictionary.
5451 include_dict = dict([item for sublist in include_state.include_list
5452 for item in sublist])
erg@google.come35f7652009-06-19 20:52:09 +00005453
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005454 # Did we find the header for this file (if any) and successfully load it?
erg@google.come35f7652009-06-19 20:52:09 +00005455 header_found = False
5456
5457 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00005458 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00005459
5460 # For Emacs's flymake.
5461 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5462 # by flymake and that file name might end with '_flymake.cc'. In that case,
5463 # restore original file name here so that the corresponding header file can be
5464 # found.
5465 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5466 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00005467 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00005468
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005469 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.come35f7652009-06-19 20:52:09 +00005470 # the keys.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005471 header_keys = include_dict.keys()
erg@google.com8a95ecc2011-09-08 00:45:54 +00005472 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00005473 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5474 fullpath = common_path + header
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005475 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.come35f7652009-06-19 20:52:09 +00005476 header_found = True
5477
5478 # If we can't find the header file for a .cc, assume it's because we don't
5479 # know where to look. In that case we'll give up as we're not sure they
5480 # didn't include it in the .h file.
5481 # TODO(unknown): Do a better job of finding .h files so we are confident that
5482 # not having the .h file means there isn't one.
5483 if filename.endswith('.cc') and not header_found:
5484 return
5485
erg@google.com4e00b9a2009-01-12 23:05:11 +00005486 # All the lines have been processed, report the errors found.
5487 for required_header_unstripped in required:
5488 template = required[required_header_unstripped][1]
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005489 if required_header_unstripped.strip('<>"') not in include_dict:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005490 error(filename, required[required_header_unstripped][0],
5491 'build/include_what_you_use', 4,
5492 'Add #include ' + required_header_unstripped + ' for ' + template)
5493
5494
erg@google.com8a95ecc2011-09-08 00:45:54 +00005495_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5496
5497
5498def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5499 """Check that make_pair's template arguments are deduced.
5500
avakulenko@google.com02af6282014-06-04 18:53:25 +00005501 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
erg@google.com8a95ecc2011-09-08 00:45:54 +00005502 specified explicitly, and such use isn't intended in any case.
5503
5504 Args:
5505 filename: The name of the current file.
5506 clean_lines: A CleansedLines instance containing the file.
5507 linenum: The number of the line to check.
5508 error: The function to call with any errors found.
5509 """
erg@google.com2aa59982013-10-28 19:09:25 +00005510 line = clean_lines.elided[linenum]
erg@google.com8a95ecc2011-09-08 00:45:54 +00005511 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5512 if match:
5513 error(filename, linenum, 'build/explicit_make_pair',
5514 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00005515 'For C++11-compatibility, omit template arguments from make_pair'
5516 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005517
5518
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005519def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5520 """Check if line contains a redundant "virtual" function-specifier.
5521
5522 Args:
5523 filename: The name of the current file.
5524 clean_lines: A CleansedLines instance containing the file.
5525 linenum: The number of the line to check.
5526 error: The function to call with any errors found.
5527 """
5528 # Look for "virtual" on current line.
5529 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005530 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005531 if not virtual: return
5532
avakulenko@google.com554223d2014-12-04 22:00:20 +00005533 # Ignore "virtual" keywords that are near access-specifiers. These
5534 # are only used in class base-specifier and do not apply to member
5535 # functions.
5536 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or
5537 Match(r'^\s+(public|protected|private)\b', virtual.group(3))):
5538 return
5539
5540 # Ignore the "virtual" keyword from virtual base classes. Usually
5541 # there is a column on the same line in these cases (virtual base
5542 # classes are rare in google3 because multiple inheritance is rare).
5543 if Match(r'^.*[^:]:[^:].*$', line): return
5544
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005545 # Look for the next opening parenthesis. This is the start of the
5546 # parameter list (possibly on the next line shortly after virtual).
5547 # TODO(unknown): doesn't work if there are virtual functions with
5548 # decltype() or other things that use parentheses, but csearch suggests
5549 # that this is rare.
5550 end_col = -1
5551 end_line = -1
avakulenko@google.com554223d2014-12-04 22:00:20 +00005552 start_col = len(virtual.group(2))
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005553 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5554 line = clean_lines.elided[start_line][start_col:]
5555 parameter_list = Match(r'^([^(]*)\(', line)
5556 if parameter_list:
5557 # Match parentheses to find the end of the parameter list
5558 (_, end_line, end_col) = CloseExpression(
5559 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5560 break
5561 start_col = 0
5562
5563 if end_col < 0:
5564 return # Couldn't find end of parameter list, give up
5565
5566 # Look for "override" or "final" after the parameter list
5567 # (possibly on the next few lines).
5568 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5569 line = clean_lines.elided[i][end_col:]
5570 match = Search(r'\b(override|final)\b', line)
5571 if match:
5572 error(filename, linenum, 'readability/inheritance', 4,
5573 ('"virtual" is redundant since function is '
5574 'already declared as "%s"' % match.group(1)))
5575
5576 # Set end_col to check whole lines after we are done with the
5577 # first line.
5578 end_col = 0
5579 if Search(r'[^\w]\s*$', line):
5580 break
5581
5582
5583def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5584 """Check if line contains a redundant "override" or "final" virt-specifier.
5585
5586 Args:
5587 filename: The name of the current file.
5588 clean_lines: A CleansedLines instance containing the file.
5589 linenum: The number of the line to check.
5590 error: The function to call with any errors found.
5591 """
avakulenko@google.com554223d2014-12-04 22:00:20 +00005592 # Look for closing parenthesis nearby. We need one to confirm where
5593 # the declarator ends and where the virt-specifier starts to avoid
5594 # false positives.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005595 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005596 declarator_end = line.rfind(')')
5597 if declarator_end >= 0:
5598 fragment = line[declarator_end:]
5599 else:
5600 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0:
5601 fragment = line
5602 else:
5603 return
5604
5605 # Check that at most one of "override" or "final" is present, not both
5606 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005607 error(filename, linenum, 'readability/inheritance', 4,
5608 ('"override" is redundant since function is '
5609 'already declared as "final"'))
5610
5611
5612
5613
5614# Returns true if we are at a new block, and it is directly
5615# inside of a namespace.
5616def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5617 """Checks that the new block is directly in a namespace.
5618
5619 Args:
5620 nesting_state: The _NestingState object that contains info about our state.
5621 is_forward_declaration: If the class is a forward declared class.
5622 Returns:
5623 Whether or not the new block is directly in a namespace.
5624 """
5625 if is_forward_declaration:
5626 if len(nesting_state.stack) >= 1 and (
5627 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5628 return True
5629 else:
5630 return False
5631
5632 return (len(nesting_state.stack) > 1 and
5633 nesting_state.stack[-1].check_namespace_indentation and
5634 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5635
5636
5637def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5638 raw_lines_no_comments, linenum):
5639 """This method determines if we should apply our namespace indentation check.
5640
5641 Args:
5642 nesting_state: The current nesting state.
5643 is_namespace_indent_item: If we just put a new class on the stack, True.
5644 If the top of the stack is not a class, or we did not recently
5645 add the class, False.
5646 raw_lines_no_comments: The lines without the comments.
5647 linenum: The current line number we are processing.
5648
5649 Returns:
5650 True if we should apply our namespace indentation check. Currently, it
5651 only works for classes and namespaces inside of a namespace.
5652 """
5653
5654 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5655 linenum)
5656
5657 if not (is_namespace_indent_item or is_forward_declaration):
5658 return False
5659
5660 # If we are in a macro, we do not want to check the namespace indentation.
5661 if IsMacroDefinition(raw_lines_no_comments, linenum):
5662 return False
5663
5664 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5665
5666
5667# Call this method if the line is directly inside of a namespace.
5668# If the line above is blank (excluding comments) or the start of
5669# an inner namespace, it cannot be indented.
5670def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5671 error):
5672 line = raw_lines_no_comments[linenum]
5673 if Match(r'^\s+', line):
5674 error(filename, linenum, 'runtime/indentation_namespace', 4,
5675 'Do not indent within a namespace')
erg@google.com8a95ecc2011-09-08 00:45:54 +00005676
5677
erg@google.comd350fe52013-01-14 17:51:48 +00005678def ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005679 include_state, function_state, nesting_state, error,
5680 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005681 """Processes a single line in the file.
5682
5683 Args:
5684 filename: Filename of the file that is being processed.
5685 file_extension: The extension (dot not included) of the file.
5686 clean_lines: An array of strings, each representing a line of the file,
5687 with comments stripped.
5688 line: Number of line being processed.
5689 include_state: An _IncludeState instance in which the headers are inserted.
5690 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005691 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00005692 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005693 error: A callable to which errors are reported, which takes 4 arguments:
5694 filename, line number, error level, and message
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005695 extra_check_functions: An array of additional check functions that will be
5696 run on each source line. Each function takes 4
5697 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005698 """
5699 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00005700 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005701 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005702 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5703 error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005704 if nesting_state.InAsmBlock(): return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005705 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005706 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005707 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005708 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
erg@google.comfd5da632013-10-25 17:39:45 +00005709 nesting_state, error)
erg@google.comc6671232013-10-25 21:44:03 +00005710 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005711 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00005712 nesting_state, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005713 CheckVlogArguments(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005714 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00005715 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005716 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005717 CheckRedundantVirtual(filename, clean_lines, line, error)
5718 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005719 for check_fn in extra_check_functions:
5720 check_fn(filename, clean_lines, line, error)
erg@google.com7430eef2014-07-28 22:33:46 +00005721
avakulenko@google.com02af6282014-06-04 18:53:25 +00005722def FlagCxx11Features(filename, clean_lines, linenum, error):
5723 """Flag those c++11 features that we only allow in certain places.
5724
5725 Args:
5726 filename: The name of the current file.
5727 clean_lines: A CleansedLines instance containing the file.
5728 linenum: The number of the line to check.
5729 error: The function to call with any errors found.
5730 """
5731 line = clean_lines.elided[linenum]
5732
avakulenko@google.com02af6282014-06-04 18:53:25 +00005733 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005734
5735 # Flag unapproved C++ TR1 headers.
5736 if include and include.group(1).startswith('tr1/'):
5737 error(filename, linenum, 'build/c++tr1', 5,
5738 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1))
5739
5740 # Flag unapproved C++11 headers.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005741 if include and include.group(1) in ('cfenv',
5742 'condition_variable',
5743 'fenv.h',
5744 'future',
5745 'mutex',
5746 'thread',
5747 'chrono',
5748 'ratio',
5749 'regex',
5750 'system_error',
5751 ):
5752 error(filename, linenum, 'build/c++11', 5,
5753 ('<%s> is an unapproved C++11 header.') % include.group(1))
5754
5755 # The only place where we need to worry about C++11 keywords and library
5756 # features in preprocessor directives is in macro definitions.
5757 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5758
5759 # These are classes and free functions. The classes are always
5760 # mentioned as std::*, but we only catch the free functions if
5761 # they're not found by ADL. They're alphabetical by header.
5762 for top_name in (
5763 # type_traits
5764 'alignment_of',
5765 'aligned_union',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005766 ):
5767 if Search(r'\bstd::%s\b' % top_name, line):
5768 error(filename, linenum, 'build/c++11', 5,
5769 ('std::%s is an unapproved C++11 class or function. Send c-style '
5770 'an example of where it would make your code more readable, and '
5771 'they may let you use it.') % top_name)
5772
5773
Alex Vakulenko01e47232016-05-06 13:54:15 -07005774def FlagCxx14Features(filename, clean_lines, linenum, error):
5775 """Flag those C++14 features that we restrict.
5776
5777 Args:
5778 filename: The name of the current file.
5779 clean_lines: A CleansedLines instance containing the file.
5780 linenum: The number of the line to check.
5781 error: The function to call with any errors found.
5782 """
5783 line = clean_lines.elided[linenum]
5784
5785 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5786
5787 # Flag unapproved C++14 headers.
5788 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):
5789 error(filename, linenum, 'build/c++14', 5,
5790 ('<%s> is an unapproved C++14 header.') % include.group(1))
5791
5792
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005793def ProcessFileData(filename, file_extension, lines, error,
5794 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005795 """Performs lint checks and reports any errors to the given error function.
5796
5797 Args:
5798 filename: Filename of the file that is being processed.
5799 file_extension: The extension (dot not included) of the file.
5800 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00005801 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005802 error: A callable to which errors are reported, which takes 4 arguments:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005803 filename, line number, error level, and message
5804 extra_check_functions: An array of additional check functions that will be
5805 run on each source line. Each function takes 4
5806 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005807 """
5808 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5809 ['// marker so line numbers end in a known way'])
5810
5811 include_state = _IncludeState()
5812 function_state = _FunctionState()
avakulenko@google.com02af6282014-06-04 18:53:25 +00005813 nesting_state = NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005814
erg+personal@google.com05189642010-04-30 20:43:03 +00005815 ResetNolintSuppressions()
5816
erg@google.com4e00b9a2009-01-12 23:05:11 +00005817 CheckForCopyright(filename, lines, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005818 ProcessGlobalSuppresions(lines)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005819 RemoveMultiLineComments(filename, lines, error)
5820 clean_lines = CleansedLines(lines)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005821
5822 if file_extension == 'h':
5823 CheckForHeaderGuard(filename, clean_lines, error)
5824
erg@google.com4e00b9a2009-01-12 23:05:11 +00005825 for line in xrange(clean_lines.NumLines()):
5826 ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005827 include_state, function_state, nesting_state, error,
5828 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005829 FlagCxx11Features(filename, clean_lines, line, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005830 nesting_state.CheckCompletedBlocks(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005831
5832 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005833
avakulenko@google.com554223d2014-12-04 22:00:20 +00005834 # Check that the .cc file has included its header if it exists.
Alex Vakulenko01e47232016-05-06 13:54:15 -07005835 if _IsSourceExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005836 CheckHeaderFileIncluded(filename, include_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005837
5838 # We check here rather than inside ProcessLine so that we see raw
5839 # lines rather than "cleaned" lines.
erg@google.com2aa59982013-10-28 19:09:25 +00005840 CheckForBadCharacters(filename, lines, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005841
5842 CheckForNewlineAtEOF(filename, lines, error)
5843
erg@google.com7430eef2014-07-28 22:33:46 +00005844def ProcessConfigOverrides(filename):
5845 """ Loads the configuration files and processes the config overrides.
5846
5847 Args:
5848 filename: The name of the file being processed by the linter.
5849
5850 Returns:
5851 False if the current |filename| should not be processed further.
5852 """
5853
5854 abs_filename = os.path.abspath(filename)
5855 cfg_filters = []
5856 keep_looking = True
5857 while keep_looking:
5858 abs_path, base_name = os.path.split(abs_filename)
5859 if not base_name:
5860 break # Reached the root directory.
5861
5862 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5863 abs_filename = abs_path
5864 if not os.path.isfile(cfg_file):
5865 continue
5866
5867 try:
5868 with open(cfg_file) as file_handle:
5869 for line in file_handle:
5870 line, _, _ = line.partition('#') # Remove comments.
5871 if not line.strip():
5872 continue
5873
5874 name, _, val = line.partition('=')
5875 name = name.strip()
5876 val = val.strip()
5877 if name == 'set noparent':
5878 keep_looking = False
5879 elif name == 'filter':
5880 cfg_filters.append(val)
5881 elif name == 'exclude_files':
5882 # When matching exclude_files pattern, use the base_name of
5883 # the current file name or the directory name we are processing.
5884 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5885 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5886 # file's "exclude_files" filter is meant to be checked against "bar"
5887 # and not "baz" nor "bar/baz.cc".
5888 if base_name:
5889 pattern = re.compile(val)
5890 if pattern.match(base_name):
5891 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5892 'File path component "%s" matches '
5893 'pattern "%s"\n' %
5894 (filename, cfg_file, base_name, val))
5895 return False
avakulenko@google.com310681b2014-08-22 19:38:55 +00005896 elif name == 'linelength':
5897 global _line_length
5898 try:
5899 _line_length = int(val)
5900 except ValueError:
5901 sys.stderr.write('Line length must be numeric.')
Fabian Guera2322e4f2016-05-01 17:36:30 +02005902 elif name == 'root':
5903 global _root
5904 _root = val
erg@google.com7430eef2014-07-28 22:33:46 +00005905 else:
5906 sys.stderr.write(
5907 'Invalid configuration option (%s) in file %s\n' %
5908 (name, cfg_file))
5909
5910 except IOError:
5911 sys.stderr.write(
5912 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5913 keep_looking = False
5914
5915 # Apply all the accumulated filters in reverse order (top-level directory
5916 # config options having the least priority).
5917 for filter in reversed(cfg_filters):
5918 _AddFilters(filter)
5919
5920 return True
5921
avakulenko@google.com02af6282014-06-04 18:53:25 +00005922
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005923def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005924 """Does google-lint on a single file.
5925
5926 Args:
5927 filename: The name of the file to parse.
5928
5929 vlevel: The level of errors to report. Every error of confidence
5930 >= verbose_level will be reported. 0 is a good default.
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005931
5932 extra_check_functions: An array of additional check functions that will be
5933 run on each source line. Each function takes 4
5934 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005935 """
5936
5937 _SetVerboseLevel(vlevel)
erg@google.com7430eef2014-07-28 22:33:46 +00005938 _BackupFilters()
5939
5940 if not ProcessConfigOverrides(filename):
5941 _RestoreFilters()
5942 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005943
avakulenko@google.com02af6282014-06-04 18:53:25 +00005944 lf_lines = []
5945 crlf_lines = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00005946 try:
5947 # Support the UNIX convention of using "-" for stdin. Note that
5948 # we are not opening the file with universal newline support
5949 # (which codecs doesn't support anyway), so the resulting lines do
5950 # contain trailing '\r' characters if we are reading a file that
5951 # has CRLF endings.
5952 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.com02af6282014-06-04 18:53:25 +00005953 # below.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005954 if filename == '-':
5955 lines = codecs.StreamReaderWriter(sys.stdin,
5956 codecs.getreader('utf8'),
5957 codecs.getwriter('utf8'),
5958 'replace').read().split('\n')
5959 else:
5960 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
5961
erg@google.com4e00b9a2009-01-12 23:05:11 +00005962 # Remove trailing '\r'.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005963 # The -1 accounts for the extra trailing blank line we get from split()
5964 for linenum in range(len(lines) - 1):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005965 if lines[linenum].endswith('\r'):
5966 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.com02af6282014-06-04 18:53:25 +00005967 crlf_lines.append(linenum + 1)
5968 else:
5969 lf_lines.append(linenum + 1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005970
5971 except IOError:
5972 sys.stderr.write(
5973 "Skipping input '%s': Can't open for reading\n" % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00005974 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005975 return
5976
5977 # Note, if no dot is found, this will give the entire filename as the ext.
5978 file_extension = filename[filename.rfind('.') + 1:]
5979
5980 # When reading from stdin, the extension is unknown, so no cpplint tests
5981 # should rely on the extension.
erg@google.com19680272013-12-16 22:48:54 +00005982 if filename != '-' and file_extension not in _valid_extensions:
erg@google.com2aa59982013-10-28 19:09:25 +00005983 sys.stderr.write('Ignoring %s; not a valid file name '
erg@google.com19680272013-12-16 22:48:54 +00005984 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005985 else:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005986 ProcessFileData(filename, file_extension, lines, Error,
5987 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005988
5989 # If end-of-line sequences are a mix of LF and CR-LF, issue
5990 # warnings on the lines with CR.
5991 #
5992 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
5993 # since critique can handle these just fine, and the style guide
5994 # doesn't dictate a particular end of line sequence.
5995 #
5996 # We can't depend on os.linesep to determine what the desired
5997 # end-of-line sequence should be, since that will return the
5998 # server-side end-of-line sequence.
5999 if lf_lines and crlf_lines:
6000 # Warn on every line with CR. An alternative approach might be to
6001 # check whether the file is mostly CRLF or just LF, and warn on the
6002 # minority, we bias toward LF here since most tools prefer LF.
6003 for linenum in crlf_lines:
6004 Error(filename, linenum, 'whitespace/newline', 1,
6005 'Unexpected \\r (^M) found; better to use only \\n')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006006
6007 sys.stderr.write('Done processing %s\n' % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006008 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006009
6010
6011def PrintUsage(message):
6012 """Prints a brief usage string and exits, optionally with an error message.
6013
6014 Args:
6015 message: The optional error message.
6016 """
6017 sys.stderr.write(_USAGE)
6018 if message:
6019 sys.exit('\nFATAL ERROR: ' + message)
6020 else:
6021 sys.exit(1)
6022
6023
6024def PrintCategories():
6025 """Prints a list of all the error-categories used by error messages.
6026
6027 These are the categories used to filter messages via --filter.
6028 """
erg+personal@google.com05189642010-04-30 20:43:03 +00006029 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006030 sys.exit(0)
6031
6032
6033def ParseArguments(args):
6034 """Parses the command line arguments.
6035
6036 This may set the output format and verbosity level as side-effects.
6037
6038 Args:
6039 args: The command line arguments:
6040
6041 Returns:
6042 The list of filenames to lint.
6043 """
6044 try:
6045 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00006046 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00006047 'filter=',
erg@google.comab53edf2013-11-05 22:23:37 +00006048 'root=',
erg@google.com19680272013-12-16 22:48:54 +00006049 'linelength=',
6050 'extensions='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00006051 except getopt.GetoptError:
6052 PrintUsage('Invalid arguments.')
6053
6054 verbosity = _VerboseLevel()
6055 output_format = _OutputFormat()
6056 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00006057 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00006058
6059 for (opt, val) in opts:
6060 if opt == '--help':
6061 PrintUsage(None)
6062 elif opt == '--output':
erg@google.comc6671232013-10-25 21:44:03 +00006063 if val not in ('emacs', 'vs7', 'eclipse'):
erg@google.com02c27fd2013-05-28 21:34:34 +00006064 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006065 output_format = val
6066 elif opt == '--verbose':
6067 verbosity = int(val)
6068 elif opt == '--filter':
6069 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00006070 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00006071 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00006072 elif opt == '--counting':
6073 if val not in ('total', 'toplevel', 'detailed'):
6074 PrintUsage('Valid counting options are total, toplevel, and detailed')
6075 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00006076 elif opt == '--root':
6077 global _root
6078 _root = val
erg@google.comab53edf2013-11-05 22:23:37 +00006079 elif opt == '--linelength':
6080 global _line_length
6081 try:
6082 _line_length = int(val)
6083 except ValueError:
6084 PrintUsage('Line length must be digits.')
erg@google.com19680272013-12-16 22:48:54 +00006085 elif opt == '--extensions':
6086 global _valid_extensions
6087 try:
6088 _valid_extensions = set(val.split(','))
6089 except ValueError:
6090 PrintUsage('Extensions must be comma seperated list.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006091
6092 if not filenames:
6093 PrintUsage('No files were specified.')
6094
6095 _SetOutputFormat(output_format)
6096 _SetVerboseLevel(verbosity)
6097 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00006098 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006099
6100 return filenames
6101
6102
6103def main():
6104 filenames = ParseArguments(sys.argv[1:])
6105
6106 # Change stderr to write with replacement characters so we don't die
6107 # if we try to print something containing non-ASCII characters.
6108 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6109 codecs.getreader('utf8'),
6110 codecs.getwriter('utf8'),
6111 'replace')
6112
erg@google.coma868d2d2009-10-09 21:18:45 +00006113 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006114 for filename in filenames:
6115 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00006116 _cpplint_state.PrintErrorCounts()
6117
erg@google.com4e00b9a2009-01-12 23:05:11 +00006118 sys.exit(_cpplint_state.error_count > 0)
6119
6120
6121if __name__ == '__main__':
6122 main()