blob: 949c5107b08f3338bea82b17becb1fb1843ffd16 [file] [log] [blame]
avakulenko@google.com554223d2014-12-04 22:00:20 +00001#!/usr/bin/env python
erg@google.com4e00b9a2009-01-12 23:05:11 +00002#
erg@google.com8f91ab22011-09-06 21:04:45 +00003# Copyright (c) 2009 Google Inc. All rights reserved.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004#
erg@google.com969161c2009-06-26 22:06:46 +00005# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
erg@google.com4e00b9a2009-01-12 23:05:11 +00008#
erg@google.com969161c2009-06-26 22:06:46 +00009# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
erg@google.com4e00b9a2009-01-12 23:05:11 +000018#
erg@google.com969161c2009-06-26 22:06:46 +000019# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
erg@google.com4e00b9a2009-01-12 23:05:11 +000030
erg@google.com4e00b9a2009-01-12 23:05:11 +000031"""Does google-lint on c++ files.
32
33The goal of this script is to identify places in the code that *may*
34be in non-compliance with google style. It does not attempt to fix
35up these problems -- the point is to educate. It does also not
36attempt to find all problems, or to ensure that everything it does
37find is legitimately a problem.
38
39In particular, we can get very confused by /* and // inside strings!
40We do a small hack, which is to ignore //'s with "'s after them on the
41same line, but it is far from perfect (in either direction).
42"""
43
44import codecs
erg@google.comd350fe52013-01-14 17:51:48 +000045import copy
erg@google.com4e00b9a2009-01-12 23:05:11 +000046import getopt
47import math # for log
48import os
49import re
50import sre_compile
51import string
52import sys
53import unicodedata
54
55
56_USAGE = """
57Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
erg@google.comab53edf2013-11-05 22:23:37 +000058 [--counting=total|toplevel|detailed] [--root=subdir]
LukeCz7197a242016-09-24 13:27:35 -050059 [--linelength=digits] [--headers=x,y,...]
erg@google.com4e00b9a2009-01-12 23:05:11 +000060 <file> [file] ...
61
62 The style guidelines this tries to follow are those in
Ackermann Yuriy79692902016-04-01 21:41:34 +130063 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
erg@google.com4e00b9a2009-01-12 23:05:11 +000064
65 Every problem is given a confidence score from 1-5, with 5 meaning we are
66 certain of the problem, and 1 meaning it could be a legitimate construct.
67 This will miss some errors, and is not a substitute for a code review.
68
erg+personal@google.com05189642010-04-30 20:43:03 +000069 To suppress false-positive errors of a certain category, add a
70 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
71 suppresses errors of all categories on that line.
erg@google.com4e00b9a2009-01-12 23:05:11 +000072
73 The files passed in will be linted; at least one file must be provided.
erg@google.com19680272013-12-16 22:48:54 +000074 Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the
75 extensions with the --extensions flag.
erg@google.com4e00b9a2009-01-12 23:05:11 +000076
77 Flags:
78
79 output=vs7
80 By default, the output is formatted to ease emacs parsing. Visual Studio
81 compatible output (vs7) may also be used. Other formats are unsupported.
82
83 verbose=#
84 Specify a number 0-5 to restrict errors to certain verbosity levels.
85
86 filter=-x,+y,...
87 Specify a comma-separated list of category-filters to apply: only
88 error messages whose category names pass the filters will be printed.
89 (Category names are printed with the message and look like
90 "[whitespace/indent]".) Filters are evaluated left to right.
91 "-FOO" and "FOO" means "do not print categories that start with FOO".
92 "+FOO" means "do print categories that start with FOO".
93
94 Examples: --filter=-whitespace,+whitespace/braces
95 --filter=whitespace,runtime/printf,+runtime/printf_format
96 --filter=-,+build/include_what_you_use
97
98 To see a list of all the categories used in cpplint, pass no arg:
99 --filter=
erg@google.coma868d2d2009-10-09 21:18:45 +0000100
101 counting=total|toplevel|detailed
102 The total number of errors found is always printed. If
103 'toplevel' is provided, then the count of errors in each of
104 the top-level categories like 'build' and 'whitespace' will
105 also be printed. If 'detailed' is provided, then a count
106 is provided for each category like 'build/class'.
erg@google.com4d70a882013-04-16 21:06:32 +0000107
108 root=subdir
109 The root directory used for deriving header guard CPP variable.
110 By default, the header guard CPP variable is calculated as the relative
111 path to the directory that contains .git, .hg, or .svn. When this flag
112 is specified, the relative path is calculated from the specified
113 directory. If the specified directory does not exist, this flag is
114 ignored.
115
116 Examples:
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
LukeCz7197a242016-09-24 13:27:35 -0500137 headers=x,y,...
138 The header extensions that cpplint will treat as .h in checks. Values are
139 automatically added to --extensions list.
140
141 Examples:
142 --headers=hpp,hxx
143 --headers=hpp
144
erg@google.com7430eef2014-07-28 22:33:46 +0000145 cpplint.py supports per-directory configurations specified in CPPLINT.cfg
146 files. CPPLINT.cfg file can contain a number of key=value pairs.
147 Currently the following options are supported:
148
149 set noparent
150 filter=+filter1,-filter2,...
151 exclude_files=regex
avakulenko@google.com310681b2014-08-22 19:38:55 +0000152 linelength=80
Fabian Guera2322e4f2016-05-01 17:36:30 +0200153 root=subdir
LukeCz7197a242016-09-24 13:27:35 -0500154 headers=x,y,...
erg@google.com7430eef2014-07-28 22:33:46 +0000155
156 "set noparent" option prevents cpplint from traversing directory tree
157 upwards looking for more .cfg files in parent directories. This option
158 is usually placed in the top-level project directory.
159
160 The "filter" option is similar in function to --filter flag. It specifies
161 message filters in addition to the |_DEFAULT_FILTERS| and those specified
162 through --filter command-line flag.
163
164 "exclude_files" allows to specify a regular expression to be matched against
165 a file name. If the expression matches, the file is skipped and not run
166 through liner.
167
avakulenko@google.com310681b2014-08-22 19:38:55 +0000168 "linelength" allows to specify the allowed line length for the project.
169
Fabian Guera2322e4f2016-05-01 17:36:30 +0200170 The "root" option is similar in function to the --root flag (see example
171 above).
LukeCz7197a242016-09-24 13:27:35 -0500172
173 The "headers" option is similar in function to the --headers flag
174 (see example above).
Fabian Guera2322e4f2016-05-01 17:36:30 +0200175
erg@google.com7430eef2014-07-28 22:33:46 +0000176 CPPLINT.cfg has an effect on files in the same directory and all
177 sub-directories, unless overridden by a nested configuration file.
178
179 Example file:
180 filter=-build/include_order,+build/include_alpha
181 exclude_files=.*\.cc
182
183 The above example disables build/include_order warning and enables
184 build/include_alpha as well as excludes all .cc from being
185 processed by linter, in the current directory (where the .cfg
186 file is located) and all sub-directories.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000187"""
188
189# We categorize each error message we print. Here are the categories.
190# We want an explicit list so we can list them all in cpplint --filter=.
191# If you add a new error message with a new category, add it to the list
192# here! cpplint_unittest.py should tell you if you forget to do this.
erg+personal@google.com05189642010-04-30 20:43:03 +0000193_ERROR_CATEGORIES = [
avakulenko@google.com554223d2014-12-04 22:00:20 +0000194 'build/class',
195 'build/c++11',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700196 'build/c++14',
197 'build/c++tr1',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000198 'build/deprecated',
199 'build/endif_comment',
200 'build/explicit_make_pair',
201 'build/forward_decl',
202 'build/header_guard',
203 'build/include',
204 'build/include_alpha',
205 'build/include_order',
206 'build/include_what_you_use',
207 'build/namespaces',
208 'build/printf_format',
209 'build/storage_class',
210 'legal/copyright',
211 'readability/alt_tokens',
212 'readability/braces',
213 'readability/casting',
214 'readability/check',
215 'readability/constructors',
216 'readability/fn_size',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000217 'readability/inheritance',
218 'readability/multiline_comment',
219 'readability/multiline_string',
220 'readability/namespace',
221 'readability/nolint',
222 'readability/nul',
223 'readability/strings',
224 'readability/todo',
225 'readability/utf8',
226 'runtime/arrays',
227 'runtime/casting',
228 'runtime/explicit',
229 'runtime/int',
230 'runtime/init',
231 'runtime/invalid_increment',
232 'runtime/member_string_references',
233 'runtime/memset',
234 'runtime/indentation_namespace',
235 'runtime/operator',
236 'runtime/printf',
237 'runtime/printf_format',
238 'runtime/references',
239 'runtime/string',
240 'runtime/threadsafe_fn',
241 'runtime/vlog',
242 'whitespace/blank_line',
243 'whitespace/braces',
244 'whitespace/comma',
245 'whitespace/comments',
246 'whitespace/empty_conditional_body',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700247 'whitespace/empty_if_body',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000248 'whitespace/empty_loop_body',
249 'whitespace/end_of_line',
250 'whitespace/ending_newline',
251 'whitespace/forcolon',
252 'whitespace/indent',
253 'whitespace/line_length',
254 'whitespace/newline',
255 'whitespace/operators',
256 'whitespace/parens',
257 'whitespace/semicolon',
258 'whitespace/tab',
259 'whitespace/todo',
260 ]
261
262# These error categories are no longer enforced by cpplint, but for backwards-
263# compatibility they may still appear in NOLINT comments.
264_LEGACY_ERROR_CATEGORIES = [
265 'readability/streams',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700266 'readability/function',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000267 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000268
avakulenko@google.com02af6282014-06-04 18:53:25 +0000269# The default state of the category filter. This is overridden by the --filter=
erg@google.come35f7652009-06-19 20:52:09 +0000270# flag. By default all errors are on, so only add here categories that should be
271# off by default (i.e., categories that must be enabled by the --filter= flags).
272# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000273_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000274
Alex Vakulenko01e47232016-05-06 13:54:15 -0700275# The default list of categories suppressed for C (not C++) files.
276_DEFAULT_C_SUPPRESSED_CATEGORIES = [
277 'readability/casting',
278 ]
279
280# The default list of categories suppressed for Linux Kernel files.
281_DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [
282 'whitespace/tab',
283 ]
284
erg@google.com4e00b9a2009-01-12 23:05:11 +0000285# We used to check for high-bit characters, but after much discussion we
286# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000287# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000288
erg@google.comfd5da632013-10-25 17:39:45 +0000289# C++ headers
erg@google.com4e00b9a2009-01-12 23:05:11 +0000290_CPP_HEADERS = frozenset([
erg@google.comfd5da632013-10-25 17:39:45 +0000291 # Legacy
292 'algobase.h',
293 'algo.h',
294 'alloc.h',
295 'builtinbuf.h',
296 'bvector.h',
297 'complex.h',
298 'defalloc.h',
299 'deque.h',
300 'editbuf.h',
301 'fstream.h',
302 'function.h',
303 'hash_map',
304 'hash_map.h',
305 'hash_set',
306 'hash_set.h',
307 'hashtable.h',
308 'heap.h',
309 'indstream.h',
310 'iomanip.h',
311 'iostream.h',
312 'istream.h',
313 'iterator.h',
314 'list.h',
315 'map.h',
316 'multimap.h',
317 'multiset.h',
318 'ostream.h',
319 'pair.h',
320 'parsestream.h',
321 'pfstream.h',
322 'procbuf.h',
323 'pthread_alloc',
324 'pthread_alloc.h',
325 'rope',
326 'rope.h',
327 'ropeimpl.h',
328 'set.h',
329 'slist',
330 'slist.h',
331 'stack.h',
332 'stdiostream.h',
333 'stl_alloc.h',
334 'stl_relops.h',
335 'streambuf.h',
336 'stream.h',
337 'strfile.h',
338 'strstream.h',
339 'tempbuf.h',
340 'tree.h',
341 'type_traits.h',
342 'vector.h',
343 # 17.6.1.2 C++ library headers
344 'algorithm',
345 'array',
346 'atomic',
347 'bitset',
348 'chrono',
349 'codecvt',
350 'complex',
351 'condition_variable',
352 'deque',
353 'exception',
354 'forward_list',
355 'fstream',
356 'functional',
357 'future',
358 'initializer_list',
359 'iomanip',
360 'ios',
361 'iosfwd',
362 'iostream',
363 'istream',
364 'iterator',
365 'limits',
366 'list',
367 'locale',
368 'map',
369 'memory',
370 'mutex',
371 'new',
372 'numeric',
373 'ostream',
374 'queue',
375 'random',
376 'ratio',
377 'regex',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700378 'scoped_allocator',
erg@google.comfd5da632013-10-25 17:39:45 +0000379 'set',
380 'sstream',
381 'stack',
382 'stdexcept',
383 'streambuf',
384 'string',
385 'strstream',
386 'system_error',
387 'thread',
388 'tuple',
389 'typeindex',
390 'typeinfo',
391 'type_traits',
392 'unordered_map',
393 'unordered_set',
394 'utility',
erg@google.com5d00c562013-07-12 19:57:05 +0000395 'valarray',
erg@google.comfd5da632013-10-25 17:39:45 +0000396 'vector',
397 # 17.6.1.2 C++ headers for C library facilities
398 'cassert',
399 'ccomplex',
400 'cctype',
401 'cerrno',
402 'cfenv',
403 'cfloat',
404 'cinttypes',
405 'ciso646',
406 'climits',
407 'clocale',
408 'cmath',
409 'csetjmp',
410 'csignal',
411 'cstdalign',
412 'cstdarg',
413 'cstdbool',
414 'cstddef',
415 'cstdint',
416 'cstdio',
417 'cstdlib',
418 'cstring',
419 'ctgmath',
420 'ctime',
421 'cuchar',
422 'cwchar',
423 'cwctype',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000424 ])
425
Alex Vakulenko01e47232016-05-06 13:54:15 -0700426# Type names
427_TYPES = re.compile(
428 r'^(?:'
429 # [dcl.type.simple]
430 r'(char(16_t|32_t)?)|wchar_t|'
431 r'bool|short|int|long|signed|unsigned|float|double|'
432 # [support.types]
433 r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|'
434 # [cstdint.syn]
435 r'(u?int(_fast|_least)?(8|16|32|64)_t)|'
436 r'(u?int(max|ptr)_t)|'
437 r')$')
438
avakulenko@google.com02af6282014-06-04 18:53:25 +0000439
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000440# These headers are excluded from [build/include] and [build/include_order]
441# checks:
442# - Anything not following google file name conventions (containing an
443# uppercase character, such as Python.h or nsStringAPI.h, for example).
444# - Lua headers.
445_THIRD_PARTY_HEADERS_PATTERN = re.compile(
446 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
447
Alex Vakulenko01e47232016-05-06 13:54:15 -0700448# Pattern for matching FileInfo.BaseName() against test file name
449_TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$'
450
451# Pattern that matches only complete whitespace, possibly across multiple lines.
452_EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000453
erg@google.com4e00b9a2009-01-12 23:05:11 +0000454# Assertion macros. These are defined in base/logging.h and
Alex Vakulenko01e47232016-05-06 13:54:15 -0700455# testing/base/public/gunit.h.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000456_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000457 'DCHECK', 'CHECK',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700458 'EXPECT_TRUE', 'ASSERT_TRUE',
459 'EXPECT_FALSE', 'ASSERT_FALSE',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000460 ]
461
erg@google.come35f7652009-06-19 20:52:09 +0000462# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000463_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
464
465for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
466 ('>=', 'GE'), ('>', 'GT'),
467 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000468 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000469 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
470 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
471 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000472
473for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
474 ('>=', 'LT'), ('>', 'LE'),
475 ('<=', 'GT'), ('<', 'GE')]:
476 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
477 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000478
erg@google.comd350fe52013-01-14 17:51:48 +0000479# Alternative tokens and their replacements. For full list, see section 2.5
480# Alternative tokens [lex.digraph] in the C++ standard.
481#
482# Digraphs (such as '%:') are not included here since it's a mess to
483# match those on a word boundary.
484_ALT_TOKEN_REPLACEMENT = {
485 'and': '&&',
486 'bitor': '|',
487 'or': '||',
488 'xor': '^',
489 'compl': '~',
490 'bitand': '&',
491 'and_eq': '&=',
492 'or_eq': '|=',
493 'xor_eq': '^=',
494 'not': '!',
495 'not_eq': '!='
496 }
497
498# Compile regular expression that matches all the above keywords. The "[ =()]"
499# bit is meant to avoid matching these keywords outside of boolean expressions.
500#
erg@google.comc6671232013-10-25 21:44:03 +0000501# False positives include C-style multi-line comments and multi-line strings
502# but those have always been troublesome for cpplint.
erg@google.comd350fe52013-01-14 17:51:48 +0000503_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
504 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
505
erg@google.com4e00b9a2009-01-12 23:05:11 +0000506
507# These constants define types of headers for use with
508# _IncludeState.CheckNextIncludeOrder().
509_C_SYS_HEADER = 1
510_CPP_SYS_HEADER = 2
511_LIKELY_MY_HEADER = 3
512_POSSIBLE_MY_HEADER = 4
513_OTHER_HEADER = 5
514
erg@google.comd350fe52013-01-14 17:51:48 +0000515# These constants define the current inline assembly state
516_NO_ASM = 0 # Outside of inline assembly block
517_INSIDE_ASM = 1 # Inside inline assembly block
518_END_ASM = 2 # Last line of inline assembly block
519_BLOCK_ASM = 3 # The whole block is an inline assembly block
520
521# Match start of assembly blocks
522_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
523 r'(?:\s+(volatile|__volatile__))?'
524 r'\s*[{(]')
525
Alex Vakulenko01e47232016-05-06 13:54:15 -0700526# Match strings that indicate we're working on a C (not C++) file.
527_SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|'
528 r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))')
529
530# Match string that indicates we're working on a Linux Kernel file.
531_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
erg@google.com4e00b9a2009-01-12 23:05:11 +0000532
533_regexp_compile_cache = {}
534
erg+personal@google.com05189642010-04-30 20:43:03 +0000535# {str, set(int)}: a map from error categories to sets of linenumbers
536# on which those errors are expected and should be suppressed.
537_error_suppressions = {}
538
erg@google.com4d70a882013-04-16 21:06:32 +0000539# The root directory used for deriving header guard CPP variable.
540# This is set by --root flag.
541_root = None
542
erg@google.comab53edf2013-11-05 22:23:37 +0000543# The allowed line length of files.
544# This is set by --linelength flag.
545_line_length = 80
546
erg@google.com19680272013-12-16 22:48:54 +0000547# The allowed extensions for file names
548# This is set by --extensions flag.
549_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
550
LukeCz7197a242016-09-24 13:27:35 -0500551# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc.
552# This is set by --headers flag.
LukeCz8920b132016-09-26 19:40:47 -0500553_hpp_headers = set(['h'])
LukeCz7197a242016-09-24 13:27:35 -0500554
Alex Vakulenko01e47232016-05-06 13:54:15 -0700555# {str, bool}: a map from error categories to booleans which indicate if the
556# category should be suppressed for every line.
557_global_error_suppressions = {}
558
LukeCz7197a242016-09-24 13:27:35 -0500559def ProcessHppHeadersOption(val):
560 global _hpp_headers
561 try:
562 _hpp_headers = set(val.split(','))
563 # Automatically append to extensions list so it does not have to be set 2 times
564 _valid_extensions.update(_hpp_headers)
565 except ValueError:
566 PrintUsage('Header extensions must be comma seperated list.')
567
568def IsHeaderExtension(file_extension):
LukeCz8920b132016-09-26 19:40:47 -0500569 return file_extension in _hpp_headers
Alex Vakulenko01e47232016-05-06 13:54:15 -0700570
erg+personal@google.com05189642010-04-30 20:43:03 +0000571def ParseNolintSuppressions(filename, raw_line, linenum, error):
Alex Vakulenko01e47232016-05-06 13:54:15 -0700572 """Updates the global list of line error-suppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000573
574 Parses any NOLINT comments on the current line, updating the global
575 error_suppressions store. Reports an error if the NOLINT comment
576 was malformed.
577
578 Args:
579 filename: str, the name of the input file.
580 raw_line: str, the line of input text, with comments.
581 linenum: int, the number of the current line.
582 error: function, an error handler.
583 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000584 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
erg@google.com8a95ecc2011-09-08 00:45:54 +0000585 if matched:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000586 if matched.group(1):
587 suppressed_line = linenum + 1
588 else:
589 suppressed_line = linenum
590 category = matched.group(2)
erg+personal@google.com05189642010-04-30 20:43:03 +0000591 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000592 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg+personal@google.com05189642010-04-30 20:43:03 +0000593 else:
594 if category.startswith('(') and category.endswith(')'):
595 category = category[1:-1]
596 if category in _ERROR_CATEGORIES:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000597 _error_suppressions.setdefault(category, set()).add(suppressed_line)
avakulenko@google.com554223d2014-12-04 22:00:20 +0000598 elif category not in _LEGACY_ERROR_CATEGORIES:
erg+personal@google.com05189642010-04-30 20:43:03 +0000599 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000600 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000601
602
Alex Vakulenko01e47232016-05-06 13:54:15 -0700603def ProcessGlobalSuppresions(lines):
604 """Updates the list of global error suppressions.
605
606 Parses any lint directives in the file that have global effect.
607
608 Args:
609 lines: An array of strings, each representing a line of the file, with the
610 last element being empty if the file is terminated with a newline.
611 """
612 for line in lines:
613 if _SEARCH_C_FILE.search(line):
614 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
615 _global_error_suppressions[category] = True
616 if _SEARCH_KERNEL_FILE.search(line):
617 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES:
618 _global_error_suppressions[category] = True
619
620
erg+personal@google.com05189642010-04-30 20:43:03 +0000621def ResetNolintSuppressions():
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000622 """Resets the set of NOLINT suppressions to empty."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000623 _error_suppressions.clear()
Alex Vakulenko01e47232016-05-06 13:54:15 -0700624 _global_error_suppressions.clear()
erg+personal@google.com05189642010-04-30 20:43:03 +0000625
626
627def IsErrorSuppressedByNolint(category, linenum):
628 """Returns true if the specified error category is suppressed on this line.
629
630 Consults the global error_suppressions map populated by
Alex Vakulenko01e47232016-05-06 13:54:15 -0700631 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000632
633 Args:
634 category: str, the category of the error.
635 linenum: int, the current line number.
636 Returns:
Alex Vakulenko01e47232016-05-06 13:54:15 -0700637 bool, True iff the error should be suppressed due to a NOLINT comment or
638 global suppression.
erg+personal@google.com05189642010-04-30 20:43:03 +0000639 """
Alex Vakulenko01e47232016-05-06 13:54:15 -0700640 return (_global_error_suppressions.get(category, False) or
641 linenum in _error_suppressions.get(category, set()) or
erg+personal@google.com05189642010-04-30 20:43:03 +0000642 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000643
avakulenko@google.com02af6282014-06-04 18:53:25 +0000644
erg@google.com4e00b9a2009-01-12 23:05:11 +0000645def Match(pattern, s):
646 """Matches the string with the pattern, caching the compiled regexp."""
647 # The regexp compilation caching is inlined in both Match and Search for
648 # performance reasons; factoring it out into a separate function turns out
649 # to be noticeably expensive.
erg@google.comc6671232013-10-25 21:44:03 +0000650 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000651 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
652 return _regexp_compile_cache[pattern].match(s)
653
654
erg@google.comfd5da632013-10-25 17:39:45 +0000655def ReplaceAll(pattern, rep, s):
656 """Replaces instances of pattern in a string with a replacement.
657
658 The compiled regex is kept in a cache shared by Match and Search.
659
660 Args:
661 pattern: regex pattern
662 rep: replacement text
663 s: search string
664
665 Returns:
666 string with replacements made (or original string if no replacements)
667 """
668 if pattern not in _regexp_compile_cache:
669 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
670 return _regexp_compile_cache[pattern].sub(rep, s)
671
672
erg@google.com4e00b9a2009-01-12 23:05:11 +0000673def Search(pattern, s):
674 """Searches the string for the pattern, caching the compiled regexp."""
erg@google.comc6671232013-10-25 21:44:03 +0000675 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000676 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
677 return _regexp_compile_cache[pattern].search(s)
678
679
Alex Vakulenko01e47232016-05-06 13:54:15 -0700680def _IsSourceExtension(s):
681 """File extension (excluding dot) matches a source file extension."""
682 return s in ('c', 'cc', 'cpp', 'cxx')
683
684
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000685class _IncludeState(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000686 """Tracks line numbers for includes, and the order in which includes appear.
687
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000688 include_list contains list of lists of (header, line number) pairs.
689 It's a lists of lists rather than just one flat list to make it
690 easier to update across preprocessor boundaries.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000691
692 Call CheckNextIncludeOrder() once for each header in the file, passing
693 in the type constants defined above. Calls in an illegal order will
694 raise an _IncludeError with an appropriate error message.
695
696 """
697 # self._section will move monotonically through this set. If it ever
698 # needs to move backwards, CheckNextIncludeOrder will raise an error.
699 _INITIAL_SECTION = 0
700 _MY_H_SECTION = 1
701 _C_SECTION = 2
702 _CPP_SECTION = 3
703 _OTHER_H_SECTION = 4
704
705 _TYPE_NAMES = {
706 _C_SYS_HEADER: 'C system header',
707 _CPP_SYS_HEADER: 'C++ system header',
708 _LIKELY_MY_HEADER: 'header this file implements',
709 _POSSIBLE_MY_HEADER: 'header this file may implement',
710 _OTHER_HEADER: 'other header',
711 }
712 _SECTION_NAMES = {
713 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
714 _MY_H_SECTION: 'a header this file implements',
715 _C_SECTION: 'C system header',
716 _CPP_SECTION: 'C++ system header',
717 _OTHER_H_SECTION: 'other header',
718 }
719
720 def __init__(self):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000721 self.include_list = [[]]
722 self.ResetSection('')
erg@google.com2aa59982013-10-28 19:09:25 +0000723
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000724 def FindHeader(self, header):
725 """Check if a header has already been included.
726
727 Args:
728 header: header to check.
729 Returns:
730 Line number of previous occurrence, or -1 if the header has not
731 been seen before.
732 """
733 for section_list in self.include_list:
734 for f in section_list:
735 if f[0] == header:
736 return f[1]
737 return -1
738
739 def ResetSection(self, directive):
740 """Reset section checking for preprocessor directive.
741
742 Args:
743 directive: preprocessor directive (e.g. "if", "else").
744 """
erg@google.coma868d2d2009-10-09 21:18:45 +0000745 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000746 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000747 # The path of last found header.
748 self._last_header = ''
749
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000750 # Update list of includes. Note that we never pop from the
751 # include list.
752 if directive in ('if', 'ifdef', 'ifndef'):
753 self.include_list.append([])
754 elif directive in ('else', 'elif'):
755 self.include_list[-1] = []
756
erg@google.comfd5da632013-10-25 17:39:45 +0000757 def SetLastHeader(self, header_path):
758 self._last_header = header_path
759
erg@google.coma868d2d2009-10-09 21:18:45 +0000760 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000761 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000762
763 - replaces "-" with "_" so they both cmp the same.
764 - removes '-inl' since we don't require them to be after the main header.
765 - lowercase everything, just in case.
766
767 Args:
768 header_path: Path to be canonicalized.
769
770 Returns:
771 Canonicalized path.
772 """
773 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
774
erg@google.comfd5da632013-10-25 17:39:45 +0000775 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.coma868d2d2009-10-09 21:18:45 +0000776 """Check if a header is in alphabetical order with the previous header.
777
778 Args:
erg@google.comfd5da632013-10-25 17:39:45 +0000779 clean_lines: A CleansedLines instance containing the file.
780 linenum: The number of the line to check.
781 header_path: Canonicalized header to be checked.
erg@google.coma868d2d2009-10-09 21:18:45 +0000782
783 Returns:
784 Returns true if the header is in alphabetical order.
785 """
erg@google.comfd5da632013-10-25 17:39:45 +0000786 # If previous section is different from current section, _last_header will
787 # be reset to empty string, so it's always less than current header.
788 #
789 # If previous line was a blank line, assume that the headers are
790 # intentionally sorted the way they are.
791 if (self._last_header > header_path and
avakulenko@google.com554223d2014-12-04 22:00:20 +0000792 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])):
erg@google.coma868d2d2009-10-09 21:18:45 +0000793 return False
erg@google.coma868d2d2009-10-09 21:18:45 +0000794 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000795
796 def CheckNextIncludeOrder(self, header_type):
797 """Returns a non-empty error message if the next header is out of order.
798
799 This function also updates the internal state to be ready to check
800 the next include.
801
802 Args:
803 header_type: One of the _XXX_HEADER constants defined above.
804
805 Returns:
806 The empty string if the header is in the right order, or an
807 error message describing what's wrong.
808
809 """
810 error_message = ('Found %s after %s' %
811 (self._TYPE_NAMES[header_type],
812 self._SECTION_NAMES[self._section]))
813
erg@google.coma868d2d2009-10-09 21:18:45 +0000814 last_section = self._section
815
erg@google.com4e00b9a2009-01-12 23:05:11 +0000816 if header_type == _C_SYS_HEADER:
817 if self._section <= self._C_SECTION:
818 self._section = self._C_SECTION
819 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000820 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000821 return error_message
822 elif header_type == _CPP_SYS_HEADER:
823 if self._section <= self._CPP_SECTION:
824 self._section = self._CPP_SECTION
825 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000826 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000827 return error_message
828 elif header_type == _LIKELY_MY_HEADER:
829 if self._section <= self._MY_H_SECTION:
830 self._section = self._MY_H_SECTION
831 else:
832 self._section = self._OTHER_H_SECTION
833 elif header_type == _POSSIBLE_MY_HEADER:
834 if self._section <= self._MY_H_SECTION:
835 self._section = self._MY_H_SECTION
836 else:
837 # This will always be the fallback because we're not sure
838 # enough that the header is associated with this file.
839 self._section = self._OTHER_H_SECTION
840 else:
841 assert header_type == _OTHER_HEADER
842 self._section = self._OTHER_H_SECTION
843
erg@google.coma868d2d2009-10-09 21:18:45 +0000844 if last_section != self._section:
845 self._last_header = ''
846
erg@google.com4e00b9a2009-01-12 23:05:11 +0000847 return ''
848
849
850class _CppLintState(object):
851 """Maintains module-wide state.."""
852
853 def __init__(self):
854 self.verbose_level = 1 # global setting.
855 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000856 # filters to apply when emitting error messages
857 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000858 # backup of filter list. Used to restore the state after each file.
859 self._filters_backup = self.filters[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000860 self.counting = 'total' # In what way are we counting errors?
861 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000862
863 # output format:
864 # "emacs" - format that emacs can parse (default)
865 # "vs7" - format that Microsoft Visual Studio 7 can parse
866 self.output_format = 'emacs'
867
868 def SetOutputFormat(self, output_format):
869 """Sets the output format for errors."""
870 self.output_format = output_format
871
872 def SetVerboseLevel(self, level):
873 """Sets the module's verbosity, and returns the previous setting."""
874 last_verbose_level = self.verbose_level
875 self.verbose_level = level
876 return last_verbose_level
877
erg@google.coma868d2d2009-10-09 21:18:45 +0000878 def SetCountingStyle(self, counting_style):
879 """Sets the module's counting options."""
880 self.counting = counting_style
881
erg@google.com4e00b9a2009-01-12 23:05:11 +0000882 def SetFilters(self, filters):
883 """Sets the error-message filters.
884
885 These filters are applied when deciding whether to emit a given
886 error message.
887
888 Args:
889 filters: A string of comma-separated filters (eg "+whitespace/indent").
890 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000891
892 Raises:
893 ValueError: The comma-separated filters did not all start with '+' or '-'.
894 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000895 """
erg@google.come35f7652009-06-19 20:52:09 +0000896 # Default filters always have less priority than the flag ones.
897 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000898 self.AddFilters(filters)
899
900 def AddFilters(self, filters):
901 """ Adds more filters to the existing list of error-message filters. """
erg@google.come35f7652009-06-19 20:52:09 +0000902 for filt in filters.split(','):
903 clean_filt = filt.strip()
904 if clean_filt:
905 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000906 for filt in self.filters:
907 if not (filt.startswith('+') or filt.startswith('-')):
908 raise ValueError('Every filter in --filters must start with + or -'
909 ' (%s does not)' % filt)
910
erg@google.com7430eef2014-07-28 22:33:46 +0000911 def BackupFilters(self):
912 """ Saves the current filter list to backup storage."""
913 self._filters_backup = self.filters[:]
914
915 def RestoreFilters(self):
916 """ Restores filters previously backed up."""
917 self.filters = self._filters_backup[:]
918
erg@google.coma868d2d2009-10-09 21:18:45 +0000919 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000920 """Sets the module's error statistic back to zero."""
921 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000922 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000923
erg@google.coma868d2d2009-10-09 21:18:45 +0000924 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000925 """Bumps the module's error statistic."""
926 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000927 if self.counting in ('toplevel', 'detailed'):
928 if self.counting != 'detailed':
929 category = category.split('/')[0]
930 if category not in self.errors_by_category:
931 self.errors_by_category[category] = 0
932 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000933
erg@google.coma868d2d2009-10-09 21:18:45 +0000934 def PrintErrorCounts(self):
935 """Print a summary of errors by category, and the total."""
936 for category, count in self.errors_by_category.iteritems():
937 sys.stderr.write('Category \'%s\' errors found: %d\n' %
938 (category, count))
LukeCze09f4782016-09-28 19:13:37 -0500939 sys.stdout.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000940
941_cpplint_state = _CppLintState()
942
943
944def _OutputFormat():
945 """Gets the module's output format."""
946 return _cpplint_state.output_format
947
948
949def _SetOutputFormat(output_format):
950 """Sets the module's output format."""
951 _cpplint_state.SetOutputFormat(output_format)
952
953
954def _VerboseLevel():
955 """Returns the module's verbosity setting."""
956 return _cpplint_state.verbose_level
957
958
959def _SetVerboseLevel(level):
960 """Sets the module's verbosity, and returns the previous setting."""
961 return _cpplint_state.SetVerboseLevel(level)
962
963
erg@google.coma868d2d2009-10-09 21:18:45 +0000964def _SetCountingStyle(level):
965 """Sets the module's counting options."""
966 _cpplint_state.SetCountingStyle(level)
967
968
erg@google.com4e00b9a2009-01-12 23:05:11 +0000969def _Filters():
970 """Returns the module's list of output filters, as a list."""
971 return _cpplint_state.filters
972
973
974def _SetFilters(filters):
975 """Sets the module's error-message filters.
976
977 These filters are applied when deciding whether to emit a given
978 error message.
979
980 Args:
981 filters: A string of comma-separated filters (eg "whitespace/indent").
982 Each filter should start with + or -; else we die.
983 """
984 _cpplint_state.SetFilters(filters)
985
erg@google.com7430eef2014-07-28 22:33:46 +0000986def _AddFilters(filters):
987 """Adds more filter overrides.
988
989 Unlike _SetFilters, this function does not reset the current list of filters
990 available.
991
992 Args:
993 filters: A string of comma-separated filters (eg "whitespace/indent").
994 Each filter should start with + or -; else we die.
995 """
996 _cpplint_state.AddFilters(filters)
997
998def _BackupFilters():
999 """ Saves the current filter list to backup storage."""
1000 _cpplint_state.BackupFilters()
1001
1002def _RestoreFilters():
1003 """ Restores filters previously backed up."""
1004 _cpplint_state.RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001005
1006class _FunctionState(object):
1007 """Tracks current function name and the number of lines in its body."""
1008
1009 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
1010 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
1011
1012 def __init__(self):
1013 self.in_a_function = False
1014 self.lines_in_function = 0
1015 self.current_function = ''
1016
1017 def Begin(self, function_name):
1018 """Start analyzing function body.
1019
1020 Args:
1021 function_name: The name of the function being tracked.
1022 """
1023 self.in_a_function = True
1024 self.lines_in_function = 0
1025 self.current_function = function_name
1026
1027 def Count(self):
1028 """Count line in current function body."""
1029 if self.in_a_function:
1030 self.lines_in_function += 1
1031
1032 def Check(self, error, filename, linenum):
1033 """Report if too many lines in function body.
1034
1035 Args:
1036 error: The function to call with any errors found.
1037 filename: The name of the current file.
1038 linenum: The number of the line to check.
1039 """
Alex Vakulenko01e47232016-05-06 13:54:15 -07001040 if not self.in_a_function:
1041 return
1042
erg@google.com4e00b9a2009-01-12 23:05:11 +00001043 if Match(r'T(EST|est)', self.current_function):
1044 base_trigger = self._TEST_TRIGGER
1045 else:
1046 base_trigger = self._NORMAL_TRIGGER
1047 trigger = base_trigger * 2**_VerboseLevel()
1048
1049 if self.lines_in_function > trigger:
1050 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
1051 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1052 if error_level > 5:
1053 error_level = 5
1054 error(filename, linenum, 'readability/fn_size', error_level,
1055 'Small and focused functions are preferred:'
1056 ' %s has %d non-comment lines'
1057 ' (error triggered by exceeding %d lines).' % (
1058 self.current_function, self.lines_in_function, trigger))
1059
1060 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001061 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001062 self.in_a_function = False
1063
1064
1065class _IncludeError(Exception):
1066 """Indicates a problem with the include order in a file."""
1067 pass
1068
1069
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001070class FileInfo(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001071 """Provides utility functions for filenames.
1072
1073 FileInfo provides easy access to the components of a file's path
1074 relative to the project root.
1075 """
1076
1077 def __init__(self, filename):
1078 self._filename = filename
1079
1080 def FullName(self):
1081 """Make Windows paths like Unix."""
1082 return os.path.abspath(self._filename).replace('\\', '/')
1083
1084 def RepositoryName(self):
1085 """FullName after removing the local path to the repository.
1086
1087 If we have a real absolute path name here we can try to do something smart:
1088 detecting the root of the checkout and truncating /path/to/checkout from
1089 the name so that we get header guards that don't include things like
1090 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
1091 people on different computers who have checked the source out to different
1092 locations won't see bogus errors.
1093 """
1094 fullname = self.FullName()
1095
1096 if os.path.exists(fullname):
1097 project_dir = os.path.dirname(fullname)
1098
1099 if os.path.exists(os.path.join(project_dir, ".svn")):
1100 # If there's a .svn file in the current directory, we recursively look
1101 # up the directory tree for the top of the SVN checkout
1102 root_dir = project_dir
1103 one_up_dir = os.path.dirname(root_dir)
1104 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1105 root_dir = os.path.dirname(root_dir)
1106 one_up_dir = os.path.dirname(one_up_dir)
1107
1108 prefix = os.path.commonprefix([root_dir, project_dir])
1109 return fullname[len(prefix) + 1:]
1110
erg@google.com3dc74262011-11-30 01:12:00 +00001111 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1112 # searching up from the current path.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001113 root_dir = current_dir = os.path.dirname(fullname)
1114 while current_dir != os.path.dirname(current_dir):
1115 if (os.path.exists(os.path.join(current_dir, ".git")) or
1116 os.path.exists(os.path.join(current_dir, ".hg")) or
1117 os.path.exists(os.path.join(current_dir, ".svn"))):
1118 root_dir = current_dir
1119 current_dir = os.path.dirname(current_dir)
erg@google.com42e59b02010-10-04 22:18:07 +00001120
1121 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +00001122 os.path.exists(os.path.join(root_dir, ".hg")) or
1123 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +00001124 prefix = os.path.commonprefix([root_dir, project_dir])
1125 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +00001126
1127 # Don't know what to do; header guard warnings may be wrong...
1128 return fullname
1129
1130 def Split(self):
1131 """Splits the file into the directory, basename, and extension.
1132
1133 For 'chrome/browser/browser.cc', Split() would
1134 return ('chrome/browser', 'browser', '.cc')
1135
1136 Returns:
1137 A tuple of (directory, basename, extension).
1138 """
1139
1140 googlename = self.RepositoryName()
1141 project, rest = os.path.split(googlename)
1142 return (project,) + os.path.splitext(rest)
1143
1144 def BaseName(self):
1145 """File base name - text after the final slash, before the final period."""
1146 return self.Split()[1]
1147
1148 def Extension(self):
1149 """File extension - text following the final period."""
1150 return self.Split()[2]
1151
1152 def NoExtension(self):
1153 """File has no source file extension."""
1154 return '/'.join(self.Split()[0:2])
1155
1156 def IsSource(self):
1157 """File has a source file extension."""
Alex Vakulenko01e47232016-05-06 13:54:15 -07001158 return _IsSourceExtension(self.Extension()[1:])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001159
1160
erg+personal@google.com05189642010-04-30 20:43:03 +00001161def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001162 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +00001163
1164 # There are three ways we might decide not to print an error message:
1165 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +00001166 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +00001167 if IsErrorSuppressedByNolint(category, linenum):
1168 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00001169
erg@google.com4e00b9a2009-01-12 23:05:11 +00001170 if confidence < _cpplint_state.verbose_level:
1171 return False
1172
1173 is_filtered = False
1174 for one_filter in _Filters():
1175 if one_filter.startswith('-'):
1176 if category.startswith(one_filter[1:]):
1177 is_filtered = True
1178 elif one_filter.startswith('+'):
1179 if category.startswith(one_filter[1:]):
1180 is_filtered = False
1181 else:
1182 assert False # should have been checked for in SetFilter.
1183 if is_filtered:
1184 return False
1185
1186 return True
1187
1188
1189def Error(filename, linenum, category, confidence, message):
1190 """Logs the fact we've found a lint error.
1191
1192 We log where the error was found, and also our confidence in the error,
1193 that is, how certain we are this is a legitimate style regression, and
1194 not a misidentification or a use that's sometimes justified.
1195
erg+personal@google.com05189642010-04-30 20:43:03 +00001196 False positives can be suppressed by the use of
1197 "cpplint(category)" comments on the offending line. These are
1198 parsed into _error_suppressions.
1199
erg@google.com4e00b9a2009-01-12 23:05:11 +00001200 Args:
1201 filename: The name of the file containing the error.
1202 linenum: The number of the line containing the error.
1203 category: A string used to describe the "category" this bug
1204 falls under: "whitespace", say, or "runtime". Categories
1205 may have a hierarchy separated by slashes: "whitespace/indent".
1206 confidence: A number from 1-5 representing a confidence score for
1207 the error, with 5 meaning that we are certain of the problem,
1208 and 1 meaning that it could be a legitimate construct.
1209 message: The error message.
1210 """
erg+personal@google.com05189642010-04-30 20:43:03 +00001211 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +00001212 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001213 if _cpplint_state.output_format == 'vs7':
schoetbi819c5722017-05-03 10:09:12 +02001214 sys.stderr.write('%s(%s): error cpplint: [%s] %s [%d]\n' % (
1215 filename, linenum, category, message, confidence))
erg@google.com02c27fd2013-05-28 21:34:34 +00001216 elif _cpplint_state.output_format == 'eclipse':
1217 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1218 filename, linenum, message, category, confidence))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001219 else:
1220 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1221 filename, linenum, message, category, confidence))
1222
1223
erg@google.com2aa59982013-10-28 19:09:25 +00001224# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001225_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1226 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.com02af6282014-06-04 18:53:25 +00001227# Match a single C style comment on the same line.
1228_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1229# Matches multi-line C style comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001230# This RE is a little bit more complicated than one might expect, because we
1231# have to take care of space removals tools so we can handle comments inside
1232# statements better.
1233# The current rule is: We only clear spaces from both sides when we're at the
1234# end of the line. Otherwise, we try to remove spaces from the right side,
1235# if this doesn't work we try on left side but only if there's a non-character
1236# on the right.
1237_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.com02af6282014-06-04 18:53:25 +00001238 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1239 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1240 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1241 _RE_PATTERN_C_COMMENTS + r')')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001242
1243
1244def IsCppString(line):
1245 """Does line terminate so, that the next symbol is in string constant.
1246
1247 This function does not consider single-line nor multi-line comments.
1248
1249 Args:
1250 line: is a partial line of code starting from the 0..n.
1251
1252 Returns:
1253 True, if next character appended to 'line' is inside a
1254 string constant.
1255 """
1256
1257 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1258 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1259
1260
erg@google.com2aa59982013-10-28 19:09:25 +00001261def CleanseRawStrings(raw_lines):
1262 """Removes C++11 raw strings from lines.
1263
1264 Before:
1265 static const char kData[] = R"(
1266 multi-line string
1267 )";
1268
1269 After:
1270 static const char kData[] = ""
1271 (replaced by blank line)
1272 "";
1273
1274 Args:
1275 raw_lines: list of raw lines.
1276
1277 Returns:
1278 list of lines with C++11 raw strings replaced by empty strings.
1279 """
1280
1281 delimiter = None
1282 lines_without_raw_strings = []
1283 for line in raw_lines:
1284 if delimiter:
1285 # Inside a raw string, look for the end
1286 end = line.find(delimiter)
1287 if end >= 0:
1288 # Found the end of the string, match leading space for this
1289 # line and resume copying the original lines, and also insert
1290 # a "" on the last line.
1291 leading_space = Match(r'^(\s*)\S', line)
1292 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1293 delimiter = None
1294 else:
1295 # Haven't found the end yet, append a blank line.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001296 line = '""'
erg@google.com2aa59982013-10-28 19:09:25 +00001297
avakulenko@google.com02af6282014-06-04 18:53:25 +00001298 # Look for beginning of a raw string, and replace them with
1299 # empty strings. This is done in a loop to handle multiple raw
1300 # strings on the same line.
1301 while delimiter is None:
erg@google.com2aa59982013-10-28 19:09:25 +00001302 # Look for beginning of a raw string.
1303 # See 2.14.15 [lex.string] for syntax.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001304 #
1305 # Once we have matched a raw string, we check the prefix of the
1306 # line to make sure that the line is not part of a single line
1307 # comment. It's done this way because we remove raw strings
1308 # before removing comments as opposed to removing comments
1309 # before removing raw strings. This is because there are some
1310 # cpplint checks that requires the comments to be preserved, but
1311 # we don't want to check comments that are inside raw strings.
1312 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1313 if (matched and
1314 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
1315 matched.group(1))):
erg@google.com2aa59982013-10-28 19:09:25 +00001316 delimiter = ')' + matched.group(2) + '"'
1317
1318 end = matched.group(3).find(delimiter)
1319 if end >= 0:
1320 # Raw string ended on same line
1321 line = (matched.group(1) + '""' +
1322 matched.group(3)[end + len(delimiter):])
1323 delimiter = None
1324 else:
1325 # Start of a multi-line raw string
1326 line = matched.group(1) + '""'
avakulenko@google.com02af6282014-06-04 18:53:25 +00001327 else:
1328 break
erg@google.com2aa59982013-10-28 19:09:25 +00001329
1330 lines_without_raw_strings.append(line)
1331
1332 # TODO(unknown): if delimiter is not None here, we might want to
1333 # emit a warning for unterminated string.
1334 return lines_without_raw_strings
1335
1336
erg@google.com4e00b9a2009-01-12 23:05:11 +00001337def FindNextMultiLineCommentStart(lines, lineix):
1338 """Find the beginning marker for a multiline comment."""
1339 while lineix < len(lines):
1340 if lines[lineix].strip().startswith('/*'):
1341 # Only return this marker if the comment goes beyond this line
1342 if lines[lineix].strip().find('*/', 2) < 0:
1343 return lineix
1344 lineix += 1
1345 return len(lines)
1346
1347
1348def FindNextMultiLineCommentEnd(lines, lineix):
1349 """We are inside a comment, find the end marker."""
1350 while lineix < len(lines):
1351 if lines[lineix].strip().endswith('*/'):
1352 return lineix
1353 lineix += 1
1354 return len(lines)
1355
1356
1357def RemoveMultiLineCommentsFromRange(lines, begin, end):
1358 """Clears a range of lines for multi-line comments."""
1359 # Having // dummy comments makes the lines non-empty, so we will not get
1360 # unnecessary blank line warnings later in the code.
1361 for i in range(begin, end):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001362 lines[i] = '/**/'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001363
1364
1365def RemoveMultiLineComments(filename, lines, error):
1366 """Removes multiline (c-style) comments from lines."""
1367 lineix = 0
1368 while lineix < len(lines):
1369 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1370 if lineix_begin >= len(lines):
1371 return
1372 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1373 if lineix_end >= len(lines):
1374 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1375 'Could not find end of multi-line comment')
1376 return
1377 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1378 lineix = lineix_end + 1
1379
1380
1381def CleanseComments(line):
1382 """Removes //-comments and single-line C-style /* */ comments.
1383
1384 Args:
1385 line: A line of C++ source.
1386
1387 Returns:
1388 The line with single-line comments removed.
1389 """
1390 commentpos = line.find('//')
1391 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +00001392 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001393 # get rid of /* ... */
1394 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1395
1396
erg@google.coma87abb82009-02-24 01:41:01 +00001397class CleansedLines(object):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001398 """Holds 4 copies of all lines with different preprocessing applied to them.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001399
avakulenko@google.com554223d2014-12-04 22:00:20 +00001400 1) elided member contains lines without strings and comments.
1401 2) lines member contains lines without comments.
erg@google.comd350fe52013-01-14 17:51:48 +00001402 3) raw_lines member contains all the lines without processing.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001403 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw
1404 strings removed.
1405 All these members are of <type 'list'>, and of the same length.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001406 """
1407
1408 def __init__(self, lines):
1409 self.elided = []
1410 self.lines = []
1411 self.raw_lines = lines
1412 self.num_lines = len(lines)
erg@google.com2aa59982013-10-28 19:09:25 +00001413 self.lines_without_raw_strings = CleanseRawStrings(lines)
1414 for linenum in range(len(self.lines_without_raw_strings)):
1415 self.lines.append(CleanseComments(
1416 self.lines_without_raw_strings[linenum]))
1417 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001418 self.elided.append(CleanseComments(elided))
1419
1420 def NumLines(self):
1421 """Returns the number of lines represented."""
1422 return self.num_lines
1423
1424 @staticmethod
1425 def _CollapseStrings(elided):
1426 """Collapses strings and chars on a line to simple "" or '' blocks.
1427
1428 We nix strings first so we're not fooled by text like '"http://"'
1429
1430 Args:
1431 elided: The line being processed.
1432
1433 Returns:
1434 The line with collapsed strings.
1435 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001436 if _RE_PATTERN_INCLUDE.match(elided):
1437 return elided
1438
1439 # Remove escaped characters first to make quote/single quote collapsing
1440 # basic. Things that look like escaped characters shouldn't occur
1441 # outside of strings and chars.
1442 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1443
1444 # Replace quoted strings and digit separators. Both single quotes
1445 # and double quotes are processed in the same loop, otherwise
1446 # nested quotes wouldn't work.
1447 collapsed = ''
1448 while True:
1449 # Find the first quote character
1450 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1451 if not match:
1452 collapsed += elided
1453 break
1454 head, quote, tail = match.groups()
1455
1456 if quote == '"':
1457 # Collapse double quoted strings
1458 second_quote = tail.find('"')
1459 if second_quote >= 0:
1460 collapsed += head + '""'
1461 elided = tail[second_quote + 1:]
1462 else:
1463 # Unmatched double quote, don't bother processing the rest
1464 # of the line since this is probably a multiline string.
1465 collapsed += elided
1466 break
1467 else:
1468 # Found single quote, check nearby text to eliminate digit separators.
1469 #
1470 # There is no special handling for floating point here, because
1471 # the integer/fractional/exponent parts would all be parsed
1472 # correctly as long as there are digits on both sides of the
1473 # separator. So we are fine as long as we don't see something
1474 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1475 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1476 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1477 collapsed += head + match_literal.group(1).replace("'", '')
1478 elided = match_literal.group(2)
1479 else:
1480 second_quote = tail.find('\'')
1481 if second_quote >= 0:
1482 collapsed += head + "''"
1483 elided = tail[second_quote + 1:]
1484 else:
1485 # Unmatched single quote
1486 collapsed += elided
1487 break
1488
1489 return collapsed
erg@google.com4e00b9a2009-01-12 23:05:11 +00001490
1491
avakulenko@google.com02af6282014-06-04 18:53:25 +00001492def FindEndOfExpressionInLine(line, startpos, stack):
1493 """Find the position just after the end of current parenthesized expression.
erg@google.comd350fe52013-01-14 17:51:48 +00001494
1495 Args:
1496 line: a CleansedLines line.
1497 startpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001498 stack: nesting stack at startpos.
erg@google.comd350fe52013-01-14 17:51:48 +00001499
1500 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001501 On finding matching end: (index just after matching end, None)
1502 On finding an unclosed expression: (-1, None)
1503 Otherwise: (-1, new stack at end of this line)
erg@google.comd350fe52013-01-14 17:51:48 +00001504 """
1505 for i in xrange(startpos, len(line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00001506 char = line[i]
1507 if char in '([{':
1508 # Found start of parenthesized expression, push to expression stack
1509 stack.append(char)
1510 elif char == '<':
1511 # Found potential start of template argument list
1512 if i > 0 and line[i - 1] == '<':
1513 # Left shift operator
1514 if stack and stack[-1] == '<':
1515 stack.pop()
1516 if not stack:
1517 return (-1, None)
1518 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1519 # operator<, don't add to stack
1520 continue
1521 else:
1522 # Tentative start of template argument list
1523 stack.append('<')
1524 elif char in ')]}':
1525 # Found end of parenthesized expression.
1526 #
1527 # If we are currently expecting a matching '>', the pending '<'
1528 # must have been an operator. Remove them from expression stack.
1529 while stack and stack[-1] == '<':
1530 stack.pop()
1531 if not stack:
1532 return (-1, None)
1533 if ((stack[-1] == '(' and char == ')') or
1534 (stack[-1] == '[' and char == ']') or
1535 (stack[-1] == '{' and char == '}')):
1536 stack.pop()
1537 if not stack:
1538 return (i + 1, None)
1539 else:
1540 # Mismatched parentheses
1541 return (-1, None)
1542 elif char == '>':
1543 # Found potential end of template argument list.
1544
1545 # Ignore "->" and operator functions
1546 if (i > 0 and
1547 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1548 continue
1549
1550 # Pop the stack if there is a matching '<'. Otherwise, ignore
1551 # this '>' since it must be an operator.
1552 if stack:
1553 if stack[-1] == '<':
1554 stack.pop()
1555 if not stack:
1556 return (i + 1, None)
1557 elif char == ';':
1558 # Found something that look like end of statements. If we are currently
1559 # expecting a '>', the matching '<' must have been an operator, since
1560 # template argument list should not contain statements.
1561 while stack and stack[-1] == '<':
1562 stack.pop()
1563 if not stack:
1564 return (-1, None)
1565
1566 # Did not find end of expression or unbalanced parentheses on this line
1567 return (-1, stack)
erg@google.comd350fe52013-01-14 17:51:48 +00001568
1569
erg@google.com4e00b9a2009-01-12 23:05:11 +00001570def CloseExpression(clean_lines, linenum, pos):
erg@google.com2aa59982013-10-28 19:09:25 +00001571 """If input points to ( or { or [ or <, finds the position that closes it.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001572
erg@google.com2aa59982013-10-28 19:09:25 +00001573 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001574 linenum/pos that correspond to the closing of the expression.
1575
avakulenko@google.com02af6282014-06-04 18:53:25 +00001576 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1577 Ideally we would want to index all opening and closing parentheses once
1578 and have CloseExpression be just a simple lookup, but due to preprocessor
1579 tricks, this is not so easy.
1580
erg@google.com4e00b9a2009-01-12 23:05:11 +00001581 Args:
1582 clean_lines: A CleansedLines instance containing the file.
1583 linenum: The number of the line to check.
1584 pos: A position on the line.
1585
1586 Returns:
1587 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1588 (line, len(lines), -1) if we never find a close. Note we ignore
1589 strings and comments when matching; and the line we return is the
1590 'cleansed' line at linenum.
1591 """
1592
1593 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001594 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001595 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001596
erg@google.comd350fe52013-01-14 17:51:48 +00001597 # Check first line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001598 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
erg@google.comd350fe52013-01-14 17:51:48 +00001599 if end_pos > -1:
1600 return (line, linenum, end_pos)
erg@google.com2aa59982013-10-28 19:09:25 +00001601
1602 # Continue scanning forward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001603 while stack and linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001604 linenum += 1
1605 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001606 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001607 if end_pos > -1:
1608 return (line, linenum, end_pos)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001609
avakulenko@google.com02af6282014-06-04 18:53:25 +00001610 # Did not find end of expression before end of file, give up
erg@google.comd350fe52013-01-14 17:51:48 +00001611 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001612
erg@google.com2aa59982013-10-28 19:09:25 +00001613
avakulenko@google.com02af6282014-06-04 18:53:25 +00001614def FindStartOfExpressionInLine(line, endpos, stack):
1615 """Find position at the matching start of current expression.
erg@google.com2aa59982013-10-28 19:09:25 +00001616
1617 This is almost the reverse of FindEndOfExpressionInLine, but note
1618 that the input position and returned position differs by 1.
1619
1620 Args:
1621 line: a CleansedLines line.
1622 endpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001623 stack: nesting stack at endpos.
erg@google.com2aa59982013-10-28 19:09:25 +00001624
1625 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001626 On finding matching start: (index at matching start, None)
1627 On finding an unclosed expression: (-1, None)
1628 Otherwise: (-1, new stack at beginning of this line)
erg@google.com2aa59982013-10-28 19:09:25 +00001629 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001630 i = endpos
1631 while i >= 0:
1632 char = line[i]
1633 if char in ')]}':
1634 # Found end of expression, push to expression stack
1635 stack.append(char)
1636 elif char == '>':
1637 # Found potential end of template argument list.
1638 #
1639 # Ignore it if it's a "->" or ">=" or "operator>"
1640 if (i > 0 and
1641 (line[i - 1] == '-' or
1642 Match(r'\s>=\s', line[i - 1:]) or
1643 Search(r'\boperator\s*$', line[0:i]))):
1644 i -= 1
1645 else:
1646 stack.append('>')
1647 elif char == '<':
1648 # Found potential start of template argument list
1649 if i > 0 and line[i - 1] == '<':
1650 # Left shift operator
1651 i -= 1
1652 else:
1653 # If there is a matching '>', we can pop the expression stack.
1654 # Otherwise, ignore this '<' since it must be an operator.
1655 if stack and stack[-1] == '>':
1656 stack.pop()
1657 if not stack:
1658 return (i, None)
1659 elif char in '([{':
1660 # Found start of expression.
1661 #
1662 # If there are any unmatched '>' on the stack, they must be
1663 # operators. Remove those.
1664 while stack and stack[-1] == '>':
1665 stack.pop()
1666 if not stack:
1667 return (-1, None)
1668 if ((char == '(' and stack[-1] == ')') or
1669 (char == '[' and stack[-1] == ']') or
1670 (char == '{' and stack[-1] == '}')):
1671 stack.pop()
1672 if not stack:
1673 return (i, None)
1674 else:
1675 # Mismatched parentheses
1676 return (-1, None)
1677 elif char == ';':
1678 # Found something that look like end of statements. If we are currently
1679 # expecting a '<', the matching '>' must have been an operator, since
1680 # template argument list should not contain statements.
1681 while stack and stack[-1] == '>':
1682 stack.pop()
1683 if not stack:
1684 return (-1, None)
1685
1686 i -= 1
1687
1688 return (-1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001689
1690
1691def ReverseCloseExpression(clean_lines, linenum, pos):
1692 """If input points to ) or } or ] or >, finds the position that opens it.
1693
1694 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1695 linenum/pos that correspond to the opening of the expression.
1696
1697 Args:
1698 clean_lines: A CleansedLines instance containing the file.
1699 linenum: The number of the line to check.
1700 pos: A position on the line.
1701
1702 Returns:
1703 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1704 (line, 0, -1) if we never find the matching opening brace. Note
1705 we ignore strings and comments when matching; and the line we
1706 return is the 'cleansed' line at linenum.
1707 """
1708 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001709 if line[pos] not in ')}]>':
erg@google.com2aa59982013-10-28 19:09:25 +00001710 return (line, 0, -1)
erg@google.com2aa59982013-10-28 19:09:25 +00001711
1712 # Check last line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001713 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
erg@google.com2aa59982013-10-28 19:09:25 +00001714 if start_pos > -1:
1715 return (line, linenum, start_pos)
1716
1717 # Continue scanning backward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001718 while stack and linenum > 0:
erg@google.com2aa59982013-10-28 19:09:25 +00001719 linenum -= 1
1720 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001721 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001722 if start_pos > -1:
1723 return (line, linenum, start_pos)
1724
avakulenko@google.com02af6282014-06-04 18:53:25 +00001725 # Did not find start of expression before beginning of file, give up
erg@google.com2aa59982013-10-28 19:09:25 +00001726 return (line, 0, -1)
1727
1728
erg@google.com4e00b9a2009-01-12 23:05:11 +00001729def CheckForCopyright(filename, lines, error):
1730 """Logs an error if no Copyright message appears at the top of the file."""
1731
1732 # We'll say it should occur by line 10. Don't forget there's a
1733 # dummy line at the front.
1734 for line in xrange(1, min(len(lines), 11)):
1735 if re.search(r'Copyright', lines[line], re.I): break
1736 else: # means no copyright line was found
1737 error(filename, 0, 'legal/copyright', 5,
1738 'No copyright message found. '
1739 'You should have a line: "Copyright [year] <Copyright Owner>"')
1740
1741
avakulenko@google.com02af6282014-06-04 18:53:25 +00001742def GetIndentLevel(line):
1743 """Return the number of leading spaces in line.
1744
1745 Args:
1746 line: A string to check.
1747
1748 Returns:
1749 An integer count of leading spaces, possibly zero.
1750 """
1751 indent = Match(r'^( *)\S', line)
1752 if indent:
1753 return len(indent.group(1))
1754 else:
1755 return 0
1756
1757
erg@google.com4e00b9a2009-01-12 23:05:11 +00001758def GetHeaderGuardCPPVariable(filename):
1759 """Returns the CPP variable that should be used as a header guard.
1760
1761 Args:
1762 filename: The name of a C++ header file.
1763
1764 Returns:
1765 The CPP variable that should be used as a header guard in the
1766 named file.
1767
1768 """
1769
erg+personal@google.com05189642010-04-30 20:43:03 +00001770 # Restores original filename in case that cpplint is invoked from Emacs's
1771 # flymake.
1772 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001773 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001774 # Replace 'c++' with 'cpp'.
1775 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')
Alex Vakulenko01e47232016-05-06 13:54:15 -07001776
erg@google.com4e00b9a2009-01-12 23:05:11 +00001777 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001778 file_path_from_root = fileinfo.RepositoryName()
1779 if _root:
Sergey Sharybin3b0ea892016-05-31 00:21:14 +02001780 suffix = os.sep
1781 # On Windows using directory separator will leave us with
1782 # "bogus escape error" unless we properly escape regex.
1783 if suffix == '\\':
1784 suffix += '\\'
1785 file_path_from_root = re.sub('^' + _root + suffix, '', file_path_from_root)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001786 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001787
1788
avakulenko@google.com554223d2014-12-04 22:00:20 +00001789def CheckForHeaderGuard(filename, clean_lines, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001790 """Checks that the file contains a header guard.
1791
erg@google.coma87abb82009-02-24 01:41:01 +00001792 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001793 headers, checks that the full pathname is used.
1794
1795 Args:
1796 filename: The name of the C++ header file.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001797 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001798 error: The function to call with any errors found.
1799 """
1800
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001801 # Don't check for header guards if there are error suppression
1802 # comments somewhere in this file.
1803 #
1804 # Because this is silencing a warning for a nonexistent line, we
1805 # only support the very specific NOLINT(build/header_guard) syntax,
1806 # and not the general NOLINT or NOLINT(*) syntax.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001807 raw_lines = clean_lines.lines_without_raw_strings
1808 for i in raw_lines:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001809 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1810 return
1811
erg@google.com4e00b9a2009-01-12 23:05:11 +00001812 cppvar = GetHeaderGuardCPPVariable(filename)
1813
avakulenko@google.com554223d2014-12-04 22:00:20 +00001814 ifndef = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001815 ifndef_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001816 define = ''
1817 endif = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001818 endif_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001819 for linenum, line in enumerate(raw_lines):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001820 linesplit = line.split()
1821 if len(linesplit) >= 2:
1822 # find the first occurrence of #ifndef and #define, save arg
1823 if not ifndef and linesplit[0] == '#ifndef':
1824 # set ifndef to the header guard presented on the #ifndef line.
1825 ifndef = linesplit[1]
1826 ifndef_linenum = linenum
1827 if not define and linesplit[0] == '#define':
1828 define = linesplit[1]
1829 # find the last occurrence of #endif, save entire line
1830 if line.startswith('#endif'):
1831 endif = line
1832 endif_linenum = linenum
1833
avakulenko@google.com554223d2014-12-04 22:00:20 +00001834 if not ifndef or not define or ifndef != define:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001835 error(filename, 0, 'build/header_guard', 5,
1836 'No #ifndef header guard found, suggested CPP variable is: %s' %
1837 cppvar)
1838 return
1839
1840 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1841 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001842 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001843 error_level = 0
1844 if ifndef != cppvar + '_':
1845 error_level = 5
1846
avakulenko@google.com554223d2014-12-04 22:00:20 +00001847 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum,
erg+personal@google.com05189642010-04-30 20:43:03 +00001848 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001849 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1850 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1851
avakulenko@google.com554223d2014-12-04 22:00:20 +00001852 # Check for "//" comments on endif line.
1853 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum,
1854 error)
1855 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif)
1856 if match:
1857 if match.group(1) == '_':
1858 # Issue low severity warning for deprecated double trailing underscore
1859 error(filename, endif_linenum, 'build/header_guard', 0,
1860 '#endif line should be "#endif // %s"' % cppvar)
erg@google.comdc289702012-01-26 20:30:03 +00001861 return
1862
avakulenko@google.com554223d2014-12-04 22:00:20 +00001863 # Didn't find the corresponding "//" comment. If this file does not
1864 # contain any "//" comments at all, it could be that the compiler
1865 # only wants "/**/" comments, look for those instead.
1866 no_single_line_comments = True
1867 for i in xrange(1, len(raw_lines) - 1):
1868 line = raw_lines[i]
1869 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
1870 no_single_line_comments = False
1871 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00001872
avakulenko@google.com554223d2014-12-04 22:00:20 +00001873 if no_single_line_comments:
1874 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif)
1875 if match:
1876 if match.group(1) == '_':
1877 # Low severity warning for double trailing underscore
1878 error(filename, endif_linenum, 'build/header_guard', 0,
1879 '#endif line should be "#endif /* %s */"' % cppvar)
1880 return
1881
1882 # Didn't find anything
1883 error(filename, endif_linenum, 'build/header_guard', 5,
1884 '#endif line should be "#endif // %s"' % cppvar)
1885
1886
1887def CheckHeaderFileIncluded(filename, include_state, error):
1888 """Logs an error if a .cc file does not include its header."""
1889
1890 # Do not check test files
Alex Vakulenko01e47232016-05-06 13:54:15 -07001891 fileinfo = FileInfo(filename)
1892 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001893 return
1894
Alex Vakulenko01e47232016-05-06 13:54:15 -07001895 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h'
avakulenko@google.com554223d2014-12-04 22:00:20 +00001896 if not os.path.exists(headerfile):
1897 return
1898 headername = FileInfo(headerfile).RepositoryName()
1899 first_include = 0
1900 for section_list in include_state.include_list:
1901 for f in section_list:
1902 if headername in f[0] or f[0] in headername:
1903 return
1904 if not first_include:
1905 first_include = f[1]
1906
1907 error(filename, first_include, 'build/include', 5,
1908 '%s should include its header file %s' % (fileinfo.RepositoryName(),
1909 headername))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001910
1911
erg@google.com2aa59982013-10-28 19:09:25 +00001912def CheckForBadCharacters(filename, lines, error):
1913 """Logs an error for each line containing bad characters.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001914
erg@google.com2aa59982013-10-28 19:09:25 +00001915 Two kinds of bad characters:
1916
1917 1. Unicode replacement characters: These indicate that either the file
1918 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1919 it shouldn't). Note that it's possible for this to throw off line
1920 numbering if the invalid UTF-8 occurred adjacent to a newline.
1921
1922 2. NUL bytes. These are problematic for some tools.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001923
1924 Args:
1925 filename: The name of the current file.
1926 lines: An array of strings, each representing a line of the file.
1927 error: The function to call with any errors found.
1928 """
1929 for linenum, line in enumerate(lines):
1930 if u'\ufffd' in line:
1931 error(filename, linenum, 'readability/utf8', 5,
1932 'Line contains invalid UTF-8 (or Unicode replacement character).')
erg@google.com2aa59982013-10-28 19:09:25 +00001933 if '\0' in line:
1934 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001935
1936
1937def CheckForNewlineAtEOF(filename, lines, error):
1938 """Logs an error if there is no newline char at the end of the file.
1939
1940 Args:
1941 filename: The name of the current file.
1942 lines: An array of strings, each representing a line of the file.
1943 error: The function to call with any errors found.
1944 """
1945
1946 # The array lines() was created by adding two newlines to the
1947 # original file (go figure), then splitting on \n.
1948 # To verify that the file ends in \n, we just have to make sure the
1949 # last-but-two element of lines() exists and is empty.
1950 if len(lines) < 3 or lines[-2]:
1951 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1952 'Could not find a newline character at the end of the file.')
1953
1954
1955def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1956 """Logs an error if we see /* ... */ or "..." that extend past one line.
1957
1958 /* ... */ comments are legit inside macros, for one line.
1959 Otherwise, we prefer // comments, so it's ok to warn about the
1960 other. Likewise, it's ok for strings to extend across multiple
1961 lines, as long as a line continuation character (backslash)
1962 terminates each line. Although not currently prohibited by the C++
1963 style guide, it's ugly and unnecessary. We don't do well with either
1964 in this lint program, so we warn about both.
1965
1966 Args:
1967 filename: The name of the current file.
1968 clean_lines: A CleansedLines instance containing the file.
1969 linenum: The number of the line to check.
1970 error: The function to call with any errors found.
1971 """
1972 line = clean_lines.elided[linenum]
1973
1974 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1975 # second (escaped) slash may trigger later \" detection erroneously.
1976 line = line.replace('\\\\', '')
1977
1978 if line.count('/*') > line.count('*/'):
1979 error(filename, linenum, 'readability/multiline_comment', 5,
1980 'Complex multi-line /*...*/-style comment found. '
1981 'Lint may give bogus warnings. '
1982 'Consider replacing these with //-style comments, '
1983 'with #if 0...#endif, '
1984 'or with more clearly structured multi-line comments.')
1985
1986 if (line.count('"') - line.count('\\"')) % 2:
1987 error(filename, linenum, 'readability/multiline_string', 5,
1988 'Multi-line string ("...") found. This lint script doesn\'t '
erg@google.com2aa59982013-10-28 19:09:25 +00001989 'do well with such strings, and may give bogus warnings. '
1990 'Use C++11 raw strings or concatenation instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001991
1992
avakulenko@google.com02af6282014-06-04 18:53:25 +00001993# (non-threadsafe name, thread-safe alternative, validation pattern)
1994#
1995# The validation pattern is used to eliminate false positives such as:
1996# _rand(); // false positive due to substring match.
1997# ->rand(); // some member function rand().
1998# ACMRandom rand(seed); // some variable named rand.
1999# ISAACRandom rand(); // another variable named rand.
2000#
2001# Basically we require the return value of these functions to be used
2002# in some expression context on the same line by matching on some
2003# operator before the function name. This eliminates constructors and
2004# member function calls.
2005_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
2006_THREADING_LIST = (
2007 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
2008 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
2009 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
2010 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
2011 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
2012 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
2013 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
2014 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
2015 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
2016 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
2017 ('strtok(', 'strtok_r(',
2018 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
2019 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
erg@google.com4e00b9a2009-01-12 23:05:11 +00002020 )
2021
2022
2023def CheckPosixThreading(filename, clean_lines, linenum, error):
2024 """Checks for calls to thread-unsafe functions.
2025
2026 Much code has been originally written without consideration of
2027 multi-threading. Also, engineers are relying on their old experience;
2028 they have learned posix before threading extensions were added. These
2029 tests guide the engineers to use thread-safe functions (when using
2030 posix directly).
2031
2032 Args:
2033 filename: The name of the current file.
2034 clean_lines: A CleansedLines instance containing the file.
2035 linenum: The number of the line to check.
2036 error: The function to call with any errors found.
2037 """
2038 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00002039 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
2040 # Additional pattern matching check to confirm that this is the
2041 # function we are looking for
2042 if Search(pattern, line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002043 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.com02af6282014-06-04 18:53:25 +00002044 'Consider using ' + multithread_safe_func +
2045 '...) instead of ' + single_thread_func +
erg@google.com4e00b9a2009-01-12 23:05:11 +00002046 '...) for improved thread safety.')
2047
2048
erg@google.com2aa59982013-10-28 19:09:25 +00002049def CheckVlogArguments(filename, clean_lines, linenum, error):
2050 """Checks that VLOG() is only used for defining a logging level.
2051
2052 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
2053 VLOG(FATAL) are not.
2054
2055 Args:
2056 filename: The name of the current file.
2057 clean_lines: A CleansedLines instance containing the file.
2058 linenum: The number of the line to check.
2059 error: The function to call with any errors found.
2060 """
2061 line = clean_lines.elided[linenum]
2062 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
2063 error(filename, linenum, 'runtime/vlog', 5,
2064 'VLOG() should be used with numeric verbosity level. '
2065 'Use LOG() if you want symbolic severity levels.')
2066
erg@google.coma868d2d2009-10-09 21:18:45 +00002067# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00002068# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00002069_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00002070 r'^\s*\*\w+(\+\+|--);')
2071
2072
2073def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00002074 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00002075
2076 For example following function:
2077 void increment_counter(int* count) {
2078 *count++;
2079 }
2080 is invalid, because it effectively does count++, moving pointer, and should
2081 be replaced with ++*count, (*count)++ or *count += 1.
2082
2083 Args:
2084 filename: The name of the current file.
2085 clean_lines: A CleansedLines instance containing the file.
2086 linenum: The number of the line to check.
2087 error: The function to call with any errors found.
2088 """
2089 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00002090 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00002091 error(filename, linenum, 'runtime/invalid_increment', 5,
2092 'Changing pointer instead of value (or unused value of operator*).')
2093
2094
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002095def IsMacroDefinition(clean_lines, linenum):
2096 if Search(r'^#define', clean_lines[linenum]):
2097 return True
2098
2099 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
2100 return True
2101
2102 return False
2103
2104
2105def IsForwardClassDeclaration(clean_lines, linenum):
2106 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
2107
2108
erg@google.comd350fe52013-01-14 17:51:48 +00002109class _BlockInfo(object):
2110 """Stores information about a generic block of code."""
2111
Alex Vakulenko01e47232016-05-06 13:54:15 -07002112 def __init__(self, linenum, seen_open_brace):
2113 self.starting_linenum = linenum
erg@google.comd350fe52013-01-14 17:51:48 +00002114 self.seen_open_brace = seen_open_brace
2115 self.open_parentheses = 0
2116 self.inline_asm = _NO_ASM
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002117 self.check_namespace_indentation = False
erg@google.comd350fe52013-01-14 17:51:48 +00002118
2119 def CheckBegin(self, filename, clean_lines, linenum, error):
2120 """Run checks that applies to text up to the opening brace.
2121
2122 This is mostly for checking the text after the class identifier
2123 and the "{", usually where the base class is specified. For other
2124 blocks, there isn't much to check, so we always pass.
2125
2126 Args:
2127 filename: The name of the current file.
2128 clean_lines: A CleansedLines instance containing the file.
2129 linenum: The number of the line to check.
2130 error: The function to call with any errors found.
2131 """
2132 pass
2133
2134 def CheckEnd(self, filename, clean_lines, linenum, error):
2135 """Run checks that applies to text after the closing brace.
2136
2137 This is mostly used for checking end of namespace comments.
2138
2139 Args:
2140 filename: The name of the current file.
2141 clean_lines: A CleansedLines instance containing the file.
2142 linenum: The number of the line to check.
2143 error: The function to call with any errors found.
2144 """
2145 pass
2146
avakulenko@google.com02af6282014-06-04 18:53:25 +00002147 def IsBlockInfo(self):
2148 """Returns true if this block is a _BlockInfo.
2149
2150 This is convenient for verifying that an object is an instance of
2151 a _BlockInfo, but not an instance of any of the derived classes.
2152
2153 Returns:
2154 True for this class, False for derived classes.
2155 """
2156 return self.__class__ == _BlockInfo
2157
2158
2159class _ExternCInfo(_BlockInfo):
2160 """Stores information about an 'extern "C"' block."""
2161
Alex Vakulenko01e47232016-05-06 13:54:15 -07002162 def __init__(self, linenum):
2163 _BlockInfo.__init__(self, linenum, True)
avakulenko@google.com02af6282014-06-04 18:53:25 +00002164
erg@google.comd350fe52013-01-14 17:51:48 +00002165
2166class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002167 """Stores information about a class."""
2168
erg@google.comd350fe52013-01-14 17:51:48 +00002169 def __init__(self, name, class_or_struct, clean_lines, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002170 _BlockInfo.__init__(self, linenum, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002171 self.name = name
erg@google.com4e00b9a2009-01-12 23:05:11 +00002172 self.is_derived = False
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002173 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002174 if class_or_struct == 'struct':
2175 self.access = 'public'
erg@google.comfd5da632013-10-25 17:39:45 +00002176 self.is_struct = True
erg@google.comd350fe52013-01-14 17:51:48 +00002177 else:
2178 self.access = 'private'
erg@google.comfd5da632013-10-25 17:39:45 +00002179 self.is_struct = False
2180
2181 # Remember initial indentation level for this class. Using raw_lines here
erg@google.comc6671232013-10-25 21:44:03 +00002182 # instead of elided to account for leading comments.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002183 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00002184
erg@google.com8a95ecc2011-09-08 00:45:54 +00002185 # Try to find the end of the class. This will be confused by things like:
2186 # class A {
2187 # } *x = { ...
2188 #
2189 # But it's still good enough for CheckSectionSpacing.
2190 self.last_line = 0
2191 depth = 0
2192 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00002193 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00002194 depth += line.count('{') - line.count('}')
2195 if not depth:
2196 self.last_line = i
2197 break
2198
erg@google.comd350fe52013-01-14 17:51:48 +00002199 def CheckBegin(self, filename, clean_lines, linenum, error):
2200 # Look for a bare ':'
2201 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2202 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00002203
erg@google.comfd5da632013-10-25 17:39:45 +00002204 def CheckEnd(self, filename, clean_lines, linenum, error):
avakulenko@google.com554223d2014-12-04 22:00:20 +00002205 # If there is a DISALLOW macro, it should appear near the end of
2206 # the class.
2207 seen_last_thing_in_class = False
2208 for i in xrange(linenum - 1, self.starting_linenum, -1):
2209 match = Search(
2210 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
2211 self.name + r'\)',
2212 clean_lines.elided[i])
2213 if match:
2214 if seen_last_thing_in_class:
2215 error(filename, i, 'readability/constructors', 3,
2216 match.group(1) + ' should be the last thing in the class')
2217 break
2218
2219 if not Match(r'^\s*$', clean_lines.elided[i]):
2220 seen_last_thing_in_class = True
2221
erg@google.comfd5da632013-10-25 17:39:45 +00002222 # Check that closing brace is aligned with beginning of the class.
2223 # Only do this if the closing brace is indented by only whitespaces.
2224 # This means we will not check single-line class definitions.
2225 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2226 if indent and len(indent.group(1)) != self.class_indent:
2227 if self.is_struct:
2228 parent = 'struct ' + self.name
2229 else:
2230 parent = 'class ' + self.name
2231 error(filename, linenum, 'whitespace/indent', 3,
2232 'Closing brace should be aligned with beginning of %s' % parent)
2233
erg@google.com4e00b9a2009-01-12 23:05:11 +00002234
erg@google.comd350fe52013-01-14 17:51:48 +00002235class _NamespaceInfo(_BlockInfo):
2236 """Stores information about a namespace."""
2237
2238 def __init__(self, name, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002239 _BlockInfo.__init__(self, linenum, False)
erg@google.comd350fe52013-01-14 17:51:48 +00002240 self.name = name or ''
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002241 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002242
2243 def CheckEnd(self, filename, clean_lines, linenum, error):
2244 """Check end of namespace comments."""
2245 line = clean_lines.raw_lines[linenum]
2246
2247 # Check how many lines is enclosed in this namespace. Don't issue
2248 # warning for missing namespace comments if there aren't enough
2249 # lines. However, do apply checks if there is already an end of
2250 # namespace comment and it's incorrect.
2251 #
2252 # TODO(unknown): We always want to check end of namespace comments
2253 # if a namespace is large, but sometimes we also want to apply the
2254 # check if a short namespace contained nontrivial things (something
2255 # other than forward declarations). There is currently no logic on
2256 # deciding what these nontrivial things are, so this check is
2257 # triggered by namespace size only, which works most of the time.
2258 if (linenum - self.starting_linenum < 10
Alex Vakulenko01e47232016-05-06 13:54:15 -07002259 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)):
erg@google.comd350fe52013-01-14 17:51:48 +00002260 return
2261
2262 # Look for matching comment at end of namespace.
2263 #
2264 # Note that we accept C style "/* */" comments for terminating
2265 # namespaces, so that code that terminate namespaces inside
erg@google.comc6671232013-10-25 21:44:03 +00002266 # preprocessor macros can be cpplint clean.
erg@google.comd350fe52013-01-14 17:51:48 +00002267 #
2268 # We also accept stuff like "// end of namespace <name>." with the
2269 # period at the end.
2270 #
2271 # Besides these, we don't accept anything else, otherwise we might
2272 # get false negatives when existing comment is a substring of the
erg@google.comc6671232013-10-25 21:44:03 +00002273 # expected namespace.
erg@google.comd350fe52013-01-14 17:51:48 +00002274 if self.name:
2275 # Named namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002276 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
2277 re.escape(self.name) + r'[\*/\.\\\s]*$'),
erg@google.comd350fe52013-01-14 17:51:48 +00002278 line):
2279 error(filename, linenum, 'readability/namespace', 5,
2280 'Namespace should be terminated with "// namespace %s"' %
2281 self.name)
2282 else:
2283 # Anonymous namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002284 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002285 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2286 # mention "// anonymous namespace" as an acceptable form
Alex Vakulenko01e47232016-05-06 13:54:15 -07002287 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002288 error(filename, linenum, 'readability/namespace', 5,
2289 'Anonymous namespace should be terminated with "// namespace"'
2290 ' or "// anonymous namespace"')
2291 else:
2292 error(filename, linenum, 'readability/namespace', 5,
2293 'Anonymous namespace should be terminated with "// namespace"')
erg@google.comd350fe52013-01-14 17:51:48 +00002294
2295
2296class _PreprocessorInfo(object):
2297 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2298
2299 def __init__(self, stack_before_if):
2300 # The entire nesting stack before #if
2301 self.stack_before_if = stack_before_if
2302
2303 # The entire nesting stack up to #else
2304 self.stack_before_else = []
2305
2306 # Whether we have already seen #else or #elif
2307 self.seen_else = False
2308
2309
avakulenko@google.com02af6282014-06-04 18:53:25 +00002310class NestingState(object):
erg@google.comd350fe52013-01-14 17:51:48 +00002311 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00002312
2313 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00002314 # Stack for tracking all braces. An object is pushed whenever we
2315 # see a "{", and popped when we see a "}". Only 3 types of
2316 # objects are possible:
2317 # - _ClassInfo: a class or struct.
2318 # - _NamespaceInfo: a namespace.
2319 # - _BlockInfo: some other type of block.
2320 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00002321
avakulenko@google.com02af6282014-06-04 18:53:25 +00002322 # Top of the previous stack before each Update().
2323 #
2324 # Because the nesting_stack is updated at the end of each line, we
2325 # had to do some convoluted checks to find out what is the current
2326 # scope at the beginning of the line. This check is simplified by
2327 # saving the previous top of nesting stack.
2328 #
2329 # We could save the full stack, but we only need the top. Copying
2330 # the full nesting stack would slow down cpplint by ~10%.
2331 self.previous_stack_top = []
2332
erg@google.comd350fe52013-01-14 17:51:48 +00002333 # Stack of _PreprocessorInfo objects.
2334 self.pp_stack = []
2335
2336 def SeenOpenBrace(self):
2337 """Check if we have seen the opening brace for the innermost block.
2338
2339 Returns:
2340 True if we have seen the opening brace, False if the innermost
2341 block is still expecting an opening brace.
2342 """
2343 return (not self.stack) or self.stack[-1].seen_open_brace
2344
2345 def InNamespaceBody(self):
2346 """Check if we are currently one level inside a namespace body.
2347
2348 Returns:
2349 True if top of the stack is a namespace block, False otherwise.
2350 """
2351 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2352
avakulenko@google.com02af6282014-06-04 18:53:25 +00002353 def InExternC(self):
2354 """Check if we are currently one level inside an 'extern "C"' block.
2355
2356 Returns:
2357 True if top of the stack is an extern block, False otherwise.
2358 """
2359 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2360
2361 def InClassDeclaration(self):
2362 """Check if we are currently one level inside a class or struct declaration.
2363
2364 Returns:
2365 True if top of the stack is a class/struct, False otherwise.
2366 """
2367 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2368
2369 def InAsmBlock(self):
2370 """Check if we are currently one level inside an inline ASM block.
2371
2372 Returns:
2373 True if the top of the stack is a block containing inline ASM.
2374 """
2375 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2376
2377 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2378 """Check if current position is inside template argument list.
2379
2380 Args:
2381 clean_lines: A CleansedLines instance containing the file.
2382 linenum: The number of the line to check.
2383 pos: position just after the suspected template argument.
2384 Returns:
2385 True if (linenum, pos) is inside template arguments.
2386 """
2387 while linenum < clean_lines.NumLines():
2388 # Find the earliest character that might indicate a template argument
2389 line = clean_lines.elided[linenum]
2390 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2391 if not match:
2392 linenum += 1
2393 pos = 0
2394 continue
2395 token = match.group(1)
2396 pos += len(match.group(0))
2397
2398 # These things do not look like template argument list:
2399 # class Suspect {
2400 # class Suspect x; }
2401 if token in ('{', '}', ';'): return False
2402
2403 # These things look like template argument list:
2404 # template <class Suspect>
2405 # template <class Suspect = default_value>
2406 # template <class Suspect[]>
2407 # template <class Suspect...>
2408 if token in ('>', '=', '[', ']', '.'): return True
2409
2410 # Check if token is an unmatched '<'.
2411 # If not, move on to the next character.
2412 if token != '<':
2413 pos += 1
2414 if pos >= len(line):
2415 linenum += 1
2416 pos = 0
2417 continue
2418
2419 # We can't be sure if we just find a single '<', and need to
2420 # find the matching '>'.
2421 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2422 if end_pos < 0:
2423 # Not sure if template argument list or syntax error in file
2424 return False
2425 linenum = end_line
2426 pos = end_pos
2427 return False
2428
erg@google.comd350fe52013-01-14 17:51:48 +00002429 def UpdatePreprocessor(self, line):
2430 """Update preprocessor stack.
2431
2432 We need to handle preprocessors due to classes like this:
2433 #ifdef SWIG
2434 struct ResultDetailsPageElementExtensionPoint {
2435 #else
2436 struct ResultDetailsPageElementExtensionPoint : public Extension {
2437 #endif
erg@google.comd350fe52013-01-14 17:51:48 +00002438
2439 We make the following assumptions (good enough for most files):
2440 - Preprocessor condition evaluates to true from #if up to first
2441 #else/#elif/#endif.
2442
2443 - Preprocessor condition evaluates to false from #else/#elif up
2444 to #endif. We still perform lint checks on these lines, but
2445 these do not affect nesting stack.
2446
2447 Args:
2448 line: current line to check.
2449 """
2450 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2451 # Beginning of #if block, save the nesting stack here. The saved
2452 # stack will allow us to restore the parsing state in the #else case.
2453 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2454 elif Match(r'^\s*#\s*(else|elif)\b', line):
2455 # Beginning of #else block
2456 if self.pp_stack:
2457 if not self.pp_stack[-1].seen_else:
2458 # This is the first #else or #elif block. Remember the
2459 # whole nesting stack up to this point. This is what we
2460 # keep after the #endif.
2461 self.pp_stack[-1].seen_else = True
2462 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2463
2464 # Restore the stack to how it was before the #if
2465 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2466 else:
2467 # TODO(unknown): unexpected #else, issue warning?
2468 pass
2469 elif Match(r'^\s*#\s*endif\b', line):
2470 # End of #if or #else blocks.
2471 if self.pp_stack:
2472 # If we saw an #else, we will need to restore the nesting
2473 # stack to its former state before the #else, otherwise we
2474 # will just continue from where we left off.
2475 if self.pp_stack[-1].seen_else:
2476 # Here we can just use a shallow copy since we are the last
2477 # reference to it.
2478 self.stack = self.pp_stack[-1].stack_before_else
2479 # Drop the corresponding #if
2480 self.pp_stack.pop()
2481 else:
2482 # TODO(unknown): unexpected #endif, issue warning?
2483 pass
2484
avakulenko@google.com02af6282014-06-04 18:53:25 +00002485 # TODO(unknown): Update() is too long, but we will refactor later.
erg@google.comd350fe52013-01-14 17:51:48 +00002486 def Update(self, filename, clean_lines, linenum, error):
2487 """Update nesting state with current line.
2488
2489 Args:
2490 filename: The name of the current file.
2491 clean_lines: A CleansedLines instance containing the file.
2492 linenum: The number of the line to check.
2493 error: The function to call with any errors found.
2494 """
2495 line = clean_lines.elided[linenum]
2496
avakulenko@google.com02af6282014-06-04 18:53:25 +00002497 # Remember top of the previous nesting stack.
2498 #
2499 # The stack is always pushed/popped and not modified in place, so
2500 # we can just do a shallow copy instead of copy.deepcopy. Using
2501 # deepcopy would slow down cpplint by ~28%.
2502 if self.stack:
2503 self.previous_stack_top = self.stack[-1]
2504 else:
2505 self.previous_stack_top = None
2506
2507 # Update pp_stack
erg@google.comd350fe52013-01-14 17:51:48 +00002508 self.UpdatePreprocessor(line)
2509
2510 # Count parentheses. This is to avoid adding struct arguments to
2511 # the nesting stack.
2512 if self.stack:
2513 inner_block = self.stack[-1]
2514 depth_change = line.count('(') - line.count(')')
2515 inner_block.open_parentheses += depth_change
2516
2517 # Also check if we are starting or ending an inline assembly block.
2518 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2519 if (depth_change != 0 and
2520 inner_block.open_parentheses == 1 and
2521 _MATCH_ASM.match(line)):
2522 # Enter assembly block
2523 inner_block.inline_asm = _INSIDE_ASM
2524 else:
2525 # Not entering assembly block. If previous line was _END_ASM,
2526 # we will now shift to _NO_ASM state.
2527 inner_block.inline_asm = _NO_ASM
2528 elif (inner_block.inline_asm == _INSIDE_ASM and
2529 inner_block.open_parentheses == 0):
2530 # Exit assembly block
2531 inner_block.inline_asm = _END_ASM
2532
2533 # Consume namespace declaration at the beginning of the line. Do
2534 # this in a loop so that we catch same line declarations like this:
2535 # namespace proto2 { namespace bridge { class MessageSet; } }
2536 while True:
2537 # Match start of namespace. The "\b\s*" below catches namespace
2538 # declarations even if it weren't followed by a whitespace, this
2539 # is so that we don't confuse our namespace checker. The
2540 # missing spaces will be flagged by CheckSpacing.
2541 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2542 if not namespace_decl_match:
2543 break
2544
2545 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2546 self.stack.append(new_namespace)
2547
2548 line = namespace_decl_match.group(2)
2549 if line.find('{') != -1:
2550 new_namespace.seen_open_brace = True
2551 line = line[line.find('{') + 1:]
2552
2553 # Look for a class declaration in whatever is left of the line
2554 # after parsing namespaces. The regexp accounts for decorated classes
2555 # such as in:
2556 # class LOCKABLE API Object {
2557 # };
erg@google.comd350fe52013-01-14 17:51:48 +00002558 class_decl_match = Match(
avakulenko@google.com02af6282014-06-04 18:53:25 +00002559 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2560 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2561 r'(.*)$', line)
erg@google.comd350fe52013-01-14 17:51:48 +00002562 if (class_decl_match and
2563 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002564 # We do not want to accept classes that are actually template arguments:
2565 # template <class Ignore1,
2566 # class Ignore2 = Default<Args>,
2567 # template <Args> class Ignore3>
2568 # void Function() {};
2569 #
2570 # To avoid template argument cases, we scan forward and look for
2571 # an unmatched '>'. If we see one, assume we are inside a
2572 # template argument list.
2573 end_declaration = len(class_decl_match.group(1))
2574 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2575 self.stack.append(_ClassInfo(
2576 class_decl_match.group(3), class_decl_match.group(2),
2577 clean_lines, linenum))
2578 line = class_decl_match.group(4)
erg@google.comd350fe52013-01-14 17:51:48 +00002579
2580 # If we have not yet seen the opening brace for the innermost block,
2581 # run checks here.
2582 if not self.SeenOpenBrace():
2583 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2584
2585 # Update access control if we are inside a class/struct
2586 if self.stack and isinstance(self.stack[-1], _ClassInfo):
erg@google.comfd5da632013-10-25 17:39:45 +00002587 classinfo = self.stack[-1]
2588 access_match = Match(
2589 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2590 r':(?:[^:]|$)',
2591 line)
erg@google.comd350fe52013-01-14 17:51:48 +00002592 if access_match:
erg@google.comfd5da632013-10-25 17:39:45 +00002593 classinfo.access = access_match.group(2)
2594
2595 # Check that access keywords are indented +1 space. Skip this
erg@google.comc6671232013-10-25 21:44:03 +00002596 # check if the keywords are not preceded by whitespaces.
erg@google.comfd5da632013-10-25 17:39:45 +00002597 indent = access_match.group(1)
2598 if (len(indent) != classinfo.class_indent + 1 and
2599 Match(r'^\s*$', indent)):
2600 if classinfo.is_struct:
2601 parent = 'struct ' + classinfo.name
2602 else:
2603 parent = 'class ' + classinfo.name
2604 slots = ''
2605 if access_match.group(3):
2606 slots = access_match.group(3)
2607 error(filename, linenum, 'whitespace/indent', 3,
2608 '%s%s: should be indented +1 space inside %s' % (
2609 access_match.group(2), slots, parent))
erg@google.comd350fe52013-01-14 17:51:48 +00002610
2611 # Consume braces or semicolons from what's left of the line
2612 while True:
2613 # Match first brace, semicolon, or closed parenthesis.
2614 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2615 if not matched:
2616 break
2617
2618 token = matched.group(1)
2619 if token == '{':
2620 # If namespace or class hasn't seen a opening brace yet, mark
2621 # namespace/class head as complete. Push a new block onto the
2622 # stack otherwise.
2623 if not self.SeenOpenBrace():
2624 self.stack[-1].seen_open_brace = True
avakulenko@google.com02af6282014-06-04 18:53:25 +00002625 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002626 self.stack.append(_ExternCInfo(linenum))
erg@google.comd350fe52013-01-14 17:51:48 +00002627 else:
Alex Vakulenko01e47232016-05-06 13:54:15 -07002628 self.stack.append(_BlockInfo(linenum, True))
erg@google.comd350fe52013-01-14 17:51:48 +00002629 if _MATCH_ASM.match(line):
2630 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.com02af6282014-06-04 18:53:25 +00002631
erg@google.comd350fe52013-01-14 17:51:48 +00002632 elif token == ';' or token == ')':
2633 # If we haven't seen an opening brace yet, but we already saw
2634 # a semicolon, this is probably a forward declaration. Pop
2635 # the stack for these.
2636 #
2637 # Similarly, if we haven't seen an opening brace yet, but we
2638 # already saw a closing parenthesis, then these are probably
2639 # function arguments with extra "class" or "struct" keywords.
2640 # Also pop these stack for these.
2641 if not self.SeenOpenBrace():
2642 self.stack.pop()
2643 else: # token == '}'
2644 # Perform end of block checks and pop the stack.
2645 if self.stack:
2646 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2647 self.stack.pop()
2648 line = matched.group(2)
2649
2650 def InnermostClass(self):
2651 """Get class info on the top of the stack.
2652
2653 Returns:
2654 A _ClassInfo object if we are inside a class, or None otherwise.
2655 """
2656 for i in range(len(self.stack), 0, -1):
2657 classinfo = self.stack[i - 1]
2658 if isinstance(classinfo, _ClassInfo):
2659 return classinfo
2660 return None
2661
erg@google.com2aa59982013-10-28 19:09:25 +00002662 def CheckCompletedBlocks(self, filename, error):
2663 """Checks that all classes and namespaces have been completely parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002664
2665 Call this when all lines in a file have been processed.
2666 Args:
2667 filename: The name of the current file.
2668 error: The function to call with any errors found.
2669 """
erg@google.comd350fe52013-01-14 17:51:48 +00002670 # Note: This test can result in false positives if #ifdef constructs
2671 # get in the way of brace matching. See the testBuildClass test in
2672 # cpplint_unittest.py for an example of this.
2673 for obj in self.stack:
2674 if isinstance(obj, _ClassInfo):
2675 error(filename, obj.starting_linenum, 'build/class', 5,
2676 'Failed to find complete declaration of class %s' %
2677 obj.name)
erg@google.com2aa59982013-10-28 19:09:25 +00002678 elif isinstance(obj, _NamespaceInfo):
2679 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2680 'Failed to find complete declaration of namespace %s' %
2681 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002682
2683
2684def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00002685 nesting_state, error):
erg@google.com2aa59982013-10-28 19:09:25 +00002686 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002687
2688 Complain about several constructs which gcc-2 accepts, but which are
2689 not standard C++. Warning about these in lint is one way to ease the
2690 transition to new compilers.
2691 - put storage class first (e.g. "static const" instead of "const static").
2692 - "%lld" instead of %qd" in printf-type functions.
2693 - "%1$d" is non-standard in printf-type functions.
2694 - "\%" is an undefined character escape sequence.
2695 - text after #endif is not allowed.
2696 - invalid inner-style forward declaration.
2697 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002698
erg@google.coma868d2d2009-10-09 21:18:45 +00002699 Additionally, check for constructor/destructor style violations and reference
2700 members, as it is very convenient to do so while checking for
2701 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002702
2703 Args:
2704 filename: The name of the current file.
2705 clean_lines: A CleansedLines instance containing the file.
2706 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002707 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00002708 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002709 error: A callable to which errors are reported, which takes 4 arguments:
2710 filename, line number, error level, and message
2711 """
2712
2713 # Remove comments from the line, but leave in strings for now.
2714 line = clean_lines.lines[linenum]
2715
2716 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2717 error(filename, linenum, 'runtime/printf_format', 3,
2718 '%q in format strings is deprecated. Use %ll instead.')
2719
2720 if Search(r'printf\s*\(.*".*%\d+\$', line):
2721 error(filename, linenum, 'runtime/printf_format', 2,
2722 '%N$ formats are unconventional. Try rewriting to avoid them.')
2723
2724 # Remove escaped backslashes before looking for undefined escapes.
2725 line = line.replace('\\\\', '')
2726
2727 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2728 error(filename, linenum, 'build/printf_format', 3,
2729 '%, [, (, and { are undefined character escapes. Unescape them.')
2730
2731 # For the rest, work with both comments and strings removed.
2732 line = clean_lines.elided[linenum]
2733
2734 if Search(r'\b(const|volatile|void|char|short|int|long'
2735 r'|float|double|signed|unsigned'
2736 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00002737 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00002738 line):
2739 error(filename, linenum, 'build/storage_class', 5,
Alex Vakulenko01e47232016-05-06 13:54:15 -07002740 'Storage-class specifier (static, extern, typedef, etc) should be '
2741 'at the beginning of the declaration.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002742
Elliot Glaysherae118112016-09-30 15:34:26 -07002743 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2744 error(filename, linenum, 'build/endif_comment', 5,
2745 'Uncommented text after #endif is non-standard. Use a comment.')
2746
erg@google.com4e00b9a2009-01-12 23:05:11 +00002747 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2748 error(filename, linenum, 'build/forward_decl', 5,
2749 'Inner-style forward declarations are invalid. Remove this line.')
2750
2751 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2752 line):
2753 error(filename, linenum, 'build/deprecated', 3,
2754 '>? and <? (max and min) operators are non-standard and deprecated.')
2755
erg@google.coma868d2d2009-10-09 21:18:45 +00002756 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2757 # TODO(unknown): Could it be expanded safely to arbitrary references,
2758 # without triggering too many false positives? The first
2759 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2760 # the restriction.
2761 # Here's the original regexp, for the reference:
2762 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2763 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2764 error(filename, linenum, 'runtime/member_string_references', 2,
2765 'const string& members are dangerous. It is much better to use '
2766 'alternatives, such as pointers or simple constants.')
2767
erg@google.comd350fe52013-01-14 17:51:48 +00002768 # Everything else in this function operates on class declarations.
2769 # Return early if the top of the nesting stack is not a class, or if
2770 # the class head is not completed yet.
2771 classinfo = nesting_state.InnermostClass()
2772 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002773 return
2774
erg@google.com4e00b9a2009-01-12 23:05:11 +00002775 # The class may have been declared with namespace or classname qualifiers.
2776 # The constructor and destructor will not have those qualifiers.
2777 base_classname = classinfo.name.split('::')[-1]
2778
2779 # Look for single-argument constructors that aren't marked explicit.
Alex Vakulenko01e47232016-05-06 13:54:15 -07002780 # Technically a valid construct, but against style.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002781 explicit_constructor_match = Match(
Dana Jansenscf4071c2017-02-22 12:02:39 -05002782 r'\s+(?:(?:inline|constexpr)\s+)*(explicit\s+)?'
2783 r'(?:(?:inline|constexpr)\s+)*%s\s*'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002784 r'\(((?:[^()]|\([^()]*\))*)\)'
2785 % re.escape(base_classname),
2786 line)
2787
2788 if explicit_constructor_match:
2789 is_marked_explicit = explicit_constructor_match.group(1)
2790
2791 if not explicit_constructor_match.group(2):
2792 constructor_args = []
2793 else:
2794 constructor_args = explicit_constructor_match.group(2).split(',')
2795
2796 # collapse arguments so that commas in template parameter lists and function
2797 # argument parameter lists don't split arguments in two
2798 i = 0
2799 while i < len(constructor_args):
2800 constructor_arg = constructor_args[i]
2801 while (constructor_arg.count('<') > constructor_arg.count('>') or
2802 constructor_arg.count('(') > constructor_arg.count(')')):
2803 constructor_arg += ',' + constructor_args[i + 1]
2804 del constructor_args[i + 1]
2805 constructor_args[i] = constructor_arg
2806 i += 1
2807
2808 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2809 noarg_constructor = (not constructor_args or # empty arg list
2810 # 'void' arg specifier
2811 (len(constructor_args) == 1 and
2812 constructor_args[0].strip() == 'void'))
2813 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2814 not noarg_constructor) or
2815 # all but at most one arg defaulted
2816 (len(constructor_args) >= 1 and
2817 not noarg_constructor and
2818 len(defaulted_args) >= len(constructor_args) - 1))
2819 initializer_list_constructor = bool(
2820 onearg_constructor and
2821 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2822 copy_constructor = bool(
2823 onearg_constructor and
2824 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2825 % re.escape(base_classname), constructor_args[0].strip()))
2826
2827 if (not is_marked_explicit and
2828 onearg_constructor and
2829 not initializer_list_constructor and
2830 not copy_constructor):
2831 if defaulted_args:
2832 error(filename, linenum, 'runtime/explicit', 5,
2833 'Constructors callable with one argument '
2834 'should be marked explicit.')
2835 else:
2836 error(filename, linenum, 'runtime/explicit', 5,
2837 'Single-parameter constructors should be marked explicit.')
2838 elif is_marked_explicit and not onearg_constructor:
2839 if noarg_constructor:
2840 error(filename, linenum, 'runtime/explicit', 5,
2841 'Zero-parameter constructors should not be marked explicit.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002842
erg@google.com4e00b9a2009-01-12 23:05:11 +00002843
avakulenko@google.com02af6282014-06-04 18:53:25 +00002844def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002845 """Checks for the correctness of various spacing around function calls.
2846
2847 Args:
2848 filename: The name of the current file.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002849 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002850 linenum: The number of the line to check.
2851 error: The function to call with any errors found.
2852 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00002853 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002854
2855 # Since function calls often occur inside if/for/while/switch
2856 # expressions - which have their own, more liberal conventions - we
2857 # first see if we should be looking inside such an expression for a
2858 # function call, to which we can apply more strict standards.
2859 fncall = line # if there's no control flow construct, look at whole line
2860 for pattern in (r'\bif\s*\((.*)\)\s*{',
2861 r'\bfor\s*\((.*)\)\s*{',
2862 r'\bwhile\s*\((.*)\)\s*[{;]',
2863 r'\bswitch\s*\((.*)\)\s*{'):
2864 match = Search(pattern, line)
2865 if match:
2866 fncall = match.group(1) # look inside the parens for function calls
2867 break
2868
2869 # Except in if/for/while/switch, there should never be space
2870 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2871 # for nested parens ( (a+b) + c ). Likewise, there should never be
2872 # a space before a ( when it's a function argument. I assume it's a
2873 # function argument when the char before the whitespace is legal in
2874 # a function name (alnum + _) and we're not starting a macro. Also ignore
2875 # pointers and references to arrays and functions coz they're too tricky:
2876 # we use a very simple way to recognize these:
2877 # " (something)(maybe-something)" or
2878 # " (something)(maybe-something," or
2879 # " (something)[something]"
2880 # Note that we assume the contents of [] to be short enough that
2881 # they'll never need to wrap.
2882 if ( # Ignore control structures.
erg@google.com2aa59982013-10-28 19:09:25 +00002883 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
erg@google.comc6671232013-10-25 21:44:03 +00002884 fncall) and
erg@google.com4e00b9a2009-01-12 23:05:11 +00002885 # Ignore pointers/references to functions.
2886 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2887 # Ignore pointers/references to arrays.
2888 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00002889 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00002890 error(filename, linenum, 'whitespace/parens', 4,
2891 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00002892 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002893 error(filename, linenum, 'whitespace/parens', 2,
2894 'Extra space after (')
2895 if (Search(r'\w\s+\(', fncall) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07002896 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 +00002897 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
avakulenko@google.com554223d2014-12-04 22:00:20 +00002898 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and
2899 not Search(r'\bcase\s+\(', fncall)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002900 # TODO(unknown): Space after an operator function seem to be a common
2901 # error, silence those for now by restricting them to highest verbosity.
2902 if Search(r'\boperator_*\b', line):
2903 error(filename, linenum, 'whitespace/parens', 0,
2904 'Extra space before ( in function call')
2905 else:
2906 error(filename, linenum, 'whitespace/parens', 4,
2907 'Extra space before ( in function call')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002908 # If the ) is followed only by a newline or a { + newline, assume it's
2909 # part of a control statement (if/while/etc), and don't complain
2910 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002911 # If the closing parenthesis is preceded by only whitespaces,
2912 # try to give a more descriptive error message.
2913 if Search(r'^\s+\)', fncall):
2914 error(filename, linenum, 'whitespace/parens', 2,
2915 'Closing ) should be moved to the previous line')
2916 else:
2917 error(filename, linenum, 'whitespace/parens', 2,
2918 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002919
2920
2921def IsBlankLine(line):
2922 """Returns true if the given line is blank.
2923
2924 We consider a line to be blank if the line is empty or consists of
2925 only white spaces.
2926
2927 Args:
2928 line: A line of a string.
2929
2930 Returns:
2931 True, if the given line is blank.
2932 """
2933 return not line or line.isspace()
2934
2935
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002936def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
2937 error):
2938 is_namespace_indent_item = (
2939 len(nesting_state.stack) > 1 and
2940 nesting_state.stack[-1].check_namespace_indentation and
2941 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
2942 nesting_state.previous_stack_top == nesting_state.stack[-2])
2943
2944 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
2945 clean_lines.elided, line):
2946 CheckItemIndentationInNamespace(filename, clean_lines.elided,
2947 line, error)
2948
2949
erg@google.com4e00b9a2009-01-12 23:05:11 +00002950def CheckForFunctionLengths(filename, clean_lines, linenum,
2951 function_state, error):
2952 """Reports for long function bodies.
2953
2954 For an overview why this is done, see:
Ackermann Yuriy79692902016-04-01 21:41:34 +13002955 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
erg@google.com4e00b9a2009-01-12 23:05:11 +00002956
2957 Uses a simplistic algorithm assuming other style guidelines
2958 (especially spacing) are followed.
2959 Only checks unindented functions, so class members are unchecked.
2960 Trivial bodies are unchecked, so constructors with huge initializer lists
2961 may be missed.
2962 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00002963 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002964 NOLINT *on the last line of a function* disables this check.
2965
2966 Args:
2967 filename: The name of the current file.
2968 clean_lines: A CleansedLines instance containing the file.
2969 linenum: The number of the line to check.
2970 function_state: Current function name and lines in body so far.
2971 error: The function to call with any errors found.
2972 """
2973 lines = clean_lines.lines
2974 line = lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002975 joined_line = ''
2976
2977 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00002978 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00002979 match_result = Match(regexp, line)
2980 if match_result:
2981 # If the name is all caps and underscores, figure it's a macro and
2982 # ignore it, unless it's TEST or TEST_F.
2983 function_name = match_result.group(1).split()[-1]
2984 if function_name == 'TEST' or function_name == 'TEST_F' or (
2985 not Match(r'[A-Z_]+$', function_name)):
2986 starting_func = True
2987
2988 if starting_func:
2989 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00002990 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002991 start_line = lines[start_linenum]
2992 joined_line += ' ' + start_line.lstrip()
2993 if Search(r'(;|})', start_line): # Declarations and trivial functions
2994 body_found = True
2995 break # ... ignore
2996 elif Search(r'{', start_line):
2997 body_found = True
2998 function = Search(r'((\w|:)*)\(', line).group(1)
2999 if Match(r'TEST', function): # Handle TEST... macros
3000 parameter_regexp = Search(r'(\(.*\))', joined_line)
3001 if parameter_regexp: # Ignore bad syntax
3002 function += parameter_regexp.group(1)
3003 else:
3004 function += '()'
3005 function_state.Begin(function)
3006 break
3007 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00003008 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003009 error(filename, linenum, 'readability/fn_size', 5,
3010 'Lint failed to find start of function body.')
3011 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00003012 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003013 function_state.End()
3014 elif not Match(r'^\s*$', line):
3015 function_state.Count() # Count non-blank/non-comment lines.
3016
3017
3018_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
3019
3020
avakulenko@google.com02af6282014-06-04 18:53:25 +00003021def CheckComment(line, filename, linenum, next_line_start, error):
3022 """Checks for common mistakes in comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003023
3024 Args:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003025 line: The line in question.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003026 filename: The name of the current file.
3027 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003028 next_line_start: The first non-whitespace column of the next line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003029 error: The function to call with any errors found.
3030 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00003031 commentpos = line.find('//')
3032 if commentpos != -1:
3033 # Check if the // may be in quotes. If so, ignore it
Alex Vakulenko01e47232016-05-06 13:54:15 -07003034 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003035 # Allow one space for new scopes, two spaces otherwise:
3036 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
3037 ((commentpos >= 1 and
3038 line[commentpos-1] not in string.whitespace) or
3039 (commentpos >= 2 and
3040 line[commentpos-2] not in string.whitespace))):
3041 error(filename, linenum, 'whitespace/comments', 2,
3042 'At least two spaces is best between code and comments')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003043
avakulenko@google.com02af6282014-06-04 18:53:25 +00003044 # Checks for common mistakes in TODO comments.
3045 comment = line[commentpos:]
3046 match = _RE_PATTERN_TODO.match(comment)
3047 if match:
3048 # One whitespace is correct; zero whitespace is handled elsewhere.
3049 leading_whitespace = match.group(1)
3050 if len(leading_whitespace) > 1:
3051 error(filename, linenum, 'whitespace/todo', 2,
3052 'Too many spaces before TODO')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003053
avakulenko@google.com02af6282014-06-04 18:53:25 +00003054 username = match.group(2)
3055 if not username:
3056 error(filename, linenum, 'readability/todo', 2,
3057 'Missing username in TODO; it should look like '
3058 '"// TODO(my_username): Stuff."')
3059
3060 middle_whitespace = match.group(3)
3061 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
3062 if middle_whitespace != ' ' and middle_whitespace != '':
3063 error(filename, linenum, 'whitespace/todo', 2,
3064 'TODO(my_username) should be followed by a space')
3065
3066 # If the comment contains an alphanumeric character, there
avakulenko@google.com554223d2014-12-04 22:00:20 +00003067 # should be a space somewhere between it and the // unless
3068 # it's a /// or //! Doxygen comment.
3069 if (Match(r'//[^ ]*\w', comment) and
3070 not Match(r'(///|//\!)(\s+|$)', comment)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003071 error(filename, linenum, 'whitespace/comments', 4,
3072 'Should have a space between // and comment')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003073
avakulenko@google.com554223d2014-12-04 22:00:20 +00003074
erg@google.comd350fe52013-01-14 17:51:48 +00003075def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
3076 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003077
erg@google.comd350fe52013-01-14 17:51:48 +00003078 Args:
3079 filename: The name of the current file.
3080 clean_lines: A CleansedLines instance containing the file.
3081 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003082 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003083 the current stack of nested blocks being parsed.
3084 error: The function to call with any errors found.
3085 """
3086 line = clean_lines.elided[linenum] # get rid of comments and strings
3087
3088 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
erg@google.comd350fe52013-01-14 17:51:48 +00003089 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
3090 if not matched:
3091 return
3092 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
3093 if nesting_state.stack[-1].access != 'private':
3094 error(filename, linenum, 'readability/constructors', 3,
3095 '%s must be in the private: section' % matched.group(1))
3096
3097 else:
3098 # Found DISALLOW* macro outside a class declaration, or perhaps it
3099 # was used inside a function when it should have been part of the
3100 # class declaration. We could issue a warning here, but it
3101 # probably resulted in a compiler error already.
3102 pass
3103
3104
erg@google.comd350fe52013-01-14 17:51:48 +00003105def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003106 """Checks for the correctness of various spacing issues in the code.
3107
3108 Things we check for: spaces around operators, spaces after
3109 if/for/while/switch, no spaces around parens in function calls, two
3110 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00003111 line, don't end a function with a blank line, don't add a blank line
3112 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003113
3114 Args:
3115 filename: The name of the current file.
3116 clean_lines: A CleansedLines instance containing the file.
3117 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003118 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003119 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003120 error: The function to call with any errors found.
3121 """
3122
erg@google.com2aa59982013-10-28 19:09:25 +00003123 # Don't use "elided" lines here, otherwise we can't check commented lines.
3124 # Don't want to use "raw" either, because we don't want to check inside C++11
3125 # raw strings,
3126 raw = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00003127 line = raw[linenum]
3128
3129 # Before nixing comments, check if the line is blank for no good
3130 # reason. This includes the first line after a block is opened, and
3131 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00003132 #
3133 # Skip all the blank line checks if we are immediately inside a
3134 # namespace body. In other words, don't issue blank line warnings
3135 # for this block:
3136 # namespace {
3137 #
3138 # }
3139 #
3140 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003141 #
3142 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
3143 # like namespaces.
3144 if (IsBlankLine(line) and
3145 not nesting_state.InNamespaceBody() and
3146 not nesting_state.InExternC()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003147 elided = clean_lines.elided
3148 prev_line = elided[linenum - 1]
3149 prevbrace = prev_line.rfind('{')
3150 # TODO(unknown): Don't complain if line before blank line, and line after,
3151 # both start with alnums and are indented the same amount.
3152 # This ignores whitespace at the start of a namespace block
3153 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00003154 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003155 # OK, we have a blank line at the start of a code block. Before we
3156 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00003157 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00003158 # 4 spaces (because they did not fit in a 80 column line when placed on
3159 # the same line as the function name). We also check for the case where
3160 # the previous line is indented 6 spaces, which may happen when the
3161 # initializers of a constructor do not fit into a 80 column line.
3162 exception = False
3163 if Match(r' {6}\w', prev_line): # Initializer list?
3164 # We are looking for the opening column of initializer list, which
3165 # should be indented 4 spaces to cause 6 space indentation afterwards.
3166 search_position = linenum-2
3167 while (search_position >= 0
3168 and Match(r' {6}\w', elided[search_position])):
3169 search_position -= 1
3170 exception = (search_position >= 0
3171 and elided[search_position][:5] == ' :')
3172 else:
3173 # Search for the function arguments or an initializer list. We use a
3174 # simple heuristic here: If the line is indented 4 spaces; and we have a
3175 # closing paren, without the opening paren, followed by an opening brace
3176 # or colon (for initializer lists) we assume that it is the last line of
3177 # a function header. If we have a colon indented 4 spaces, it is an
3178 # initializer list.
3179 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3180 prev_line)
3181 or Match(r' {4}:', prev_line))
3182
3183 if not exception:
3184 error(filename, linenum, 'whitespace/blank_line', 2,
erg@google.com2aa59982013-10-28 19:09:25 +00003185 'Redundant blank line at the start of a code block '
3186 'should be deleted.')
erg@google.comd350fe52013-01-14 17:51:48 +00003187 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00003188 # chain, like this:
3189 # if (condition1) {
3190 # // Something followed by a blank line
3191 #
3192 # } else if (condition2) {
3193 # // Something else
3194 # }
3195 if linenum + 1 < clean_lines.NumLines():
3196 next_line = raw[linenum + 1]
3197 if (next_line
3198 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003199 and next_line.find('} else ') == -1):
3200 error(filename, linenum, 'whitespace/blank_line', 3,
erg@google.com2aa59982013-10-28 19:09:25 +00003201 'Redundant blank line at the end of a code block '
3202 'should be deleted.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003203
erg@google.com8a95ecc2011-09-08 00:45:54 +00003204 matched = Match(r'\s*(public|protected|private):', prev_line)
3205 if matched:
3206 error(filename, linenum, 'whitespace/blank_line', 3,
3207 'Do not leave a blank line after "%s:"' % matched.group(1))
3208
avakulenko@google.com02af6282014-06-04 18:53:25 +00003209 # Next, check comments
3210 next_line_start = 0
3211 if linenum + 1 < clean_lines.NumLines():
3212 next_line = raw[linenum + 1]
3213 next_line_start = len(next_line) - len(next_line.lstrip())
3214 CheckComment(line, filename, linenum, next_line_start, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003215
avakulenko@google.com02af6282014-06-04 18:53:25 +00003216 # get rid of comments and strings
3217 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003218
avakulenko@google.com02af6282014-06-04 18:53:25 +00003219 # You shouldn't have spaces before your brackets, except maybe after
3220 # 'delete []' or 'return []() {};'
3221 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3222 error(filename, linenum, 'whitespace/braces', 5,
3223 'Extra space before [')
3224
3225 # In range-based for, we wanted spaces before and after the colon, but
3226 # not around "::" tokens that might appear.
3227 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3228 Search(r'for *\(.*[^: ]:[^:]', line)):
3229 error(filename, linenum, 'whitespace/forcolon', 2,
3230 'Missing space around colon in range-based for loop')
3231
3232
3233def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3234 """Checks for horizontal spacing around operators.
3235
3236 Args:
3237 filename: The name of the current file.
3238 clean_lines: A CleansedLines instance containing the file.
3239 linenum: The number of the line to check.
3240 error: The function to call with any errors found.
3241 """
3242 line = clean_lines.elided[linenum]
3243
3244 # Don't try to do spacing checks for operator methods. Do this by
3245 # replacing the troublesome characters with something else,
3246 # preserving column position for all other characters.
3247 #
3248 # The replacement is done repeatedly to avoid false positives from
3249 # operators that call operators.
3250 while True:
3251 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3252 if match:
3253 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3254 else:
3255 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00003256
3257 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3258 # Otherwise not. Note we only check for non-spaces on *both* sides;
3259 # sometimes people put non-spaces on one side when aligning ='s among
3260 # many lines (not that this is behavior that I approve of...)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003261 if ((Search(r'[\w.]=', line) or
3262 Search(r'=[\w.]', line))
3263 and not Search(r'\b(if|while|for) ', line)
3264 # Operators taken from [lex.operators] in C++11 standard.
3265 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3266 and not Search(r'operator=', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003267 error(filename, linenum, 'whitespace/operators', 4,
3268 'Missing spaces around =')
3269
3270 # It's ok not to have spaces around binary operators like + - * /, but if
3271 # there's too little whitespace, we get concerned. It's hard to tell,
3272 # though, so we punt on this one for now. TODO.
3273
3274 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00003275 #
3276 # Check <= and >= first to avoid false positives with < and >, then
3277 # check non-include lines for spacing around < and >.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003278 #
3279 # If the operator is followed by a comma, assume it's be used in a
3280 # macro context and don't do any checks. This avoids false
3281 # positives.
3282 #
Alex Vakulenko01e47232016-05-06 13:54:15 -07003283 # Note that && is not included here. This is because there are too
3284 # many false positives due to RValue references.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003285 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003286 if match:
3287 error(filename, linenum, 'whitespace/operators', 3,
3288 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00003289 elif not Match(r'#.*include', line):
erg@google.comd350fe52013-01-14 17:51:48 +00003290 # Look for < that is not surrounded by spaces. This is only
3291 # triggered if both sides are missing spaces, even though
3292 # technically should should flag if at least one side is missing a
3293 # space. This is done to avoid some false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003294 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3295 if match:
3296 (_, _, end_pos) = CloseExpression(
3297 clean_lines, linenum, len(match.group(1)))
3298 if end_pos <= -1:
3299 error(filename, linenum, 'whitespace/operators', 3,
3300 'Missing spaces around <')
erg@google.comd350fe52013-01-14 17:51:48 +00003301
3302 # Look for > that is not surrounded by spaces. Similar to the
3303 # above, we only trigger if both sides are missing spaces to avoid
3304 # false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003305 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3306 if match:
3307 (_, _, start_pos) = ReverseCloseExpression(
3308 clean_lines, linenum, len(match.group(1)))
3309 if start_pos <= -1:
3310 error(filename, linenum, 'whitespace/operators', 3,
3311 'Missing spaces around >')
3312
3313 # We allow no-spaces around << when used like this: 10<<20, but
3314 # not otherwise (particularly, not when used as streams)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003315 #
avakulenko@google.com02af6282014-06-04 18:53:25 +00003316 # We also allow operators following an opening parenthesis, since
3317 # those tend to be macros that deal with operators.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003318 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003319 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00003320 not (match.group(1) == 'operator' and match.group(2) == ';')):
3321 error(filename, linenum, 'whitespace/operators', 3,
3322 'Missing spaces around <<')
erg@google.comd350fe52013-01-14 17:51:48 +00003323
3324 # We allow no-spaces around >> for almost anything. This is because
3325 # C++11 allows ">>" to close nested templates, which accounts for
3326 # most cases when ">>" is not followed by a space.
3327 #
3328 # We still warn on ">>" followed by alpha character, because that is
3329 # likely due to ">>" being used for right shifts, e.g.:
3330 # value >> alpha
3331 #
3332 # When ">>" is used to close templates, the alphanumeric letter that
3333 # follows would be part of an identifier, and there should still be
3334 # a space separating the template type and the identifier.
3335 # type<type<type>> alpha
3336 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003337 if match:
3338 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00003339 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003340
3341 # There shouldn't be space around unary operators
3342 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3343 if match:
3344 error(filename, linenum, 'whitespace/operators', 4,
3345 'Extra space for operator %s' % match.group(1))
3346
avakulenko@google.com02af6282014-06-04 18:53:25 +00003347
3348def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3349 """Checks for horizontal spacing around parentheses.
3350
3351 Args:
3352 filename: The name of the current file.
3353 clean_lines: A CleansedLines instance containing the file.
3354 linenum: The number of the line to check.
3355 error: The function to call with any errors found.
3356 """
3357 line = clean_lines.elided[linenum]
3358
3359 # No spaces after an if, while, switch, or for
erg@google.com4e00b9a2009-01-12 23:05:11 +00003360 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3361 if match:
3362 error(filename, linenum, 'whitespace/parens', 5,
3363 'Missing space before ( in %s' % match.group(1))
3364
3365 # For if/for/while/switch, the left and right parens should be
3366 # consistent about how many spaces are inside the parens, and
3367 # there should either be zero or one spaces inside the parens.
3368 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00003369 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003370 match = Search(r'\b(if|for|while|switch)\s*'
3371 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3372 line)
3373 if match:
3374 if len(match.group(2)) != len(match.group(4)):
3375 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00003376 len(match.group(2)) == 1 + len(match.group(4)) or
3377 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003378 error(filename, linenum, 'whitespace/parens', 5,
3379 'Mismatching spaces inside () in %s' % match.group(1))
erg@google.comc6671232013-10-25 21:44:03 +00003380 if len(match.group(2)) not in [0, 1]:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003381 error(filename, linenum, 'whitespace/parens', 5,
3382 'Should have zero or one spaces inside ( and ) in %s' %
3383 match.group(1))
3384
avakulenko@google.com02af6282014-06-04 18:53:25 +00003385
3386def CheckCommaSpacing(filename, clean_lines, linenum, error):
3387 """Checks for horizontal spacing near commas and semicolons.
3388
3389 Args:
3390 filename: The name of the current file.
3391 clean_lines: A CleansedLines instance containing the file.
3392 linenum: The number of the line to check.
3393 error: The function to call with any errors found.
3394 """
3395 raw = clean_lines.lines_without_raw_strings
3396 line = clean_lines.elided[linenum]
3397
erg@google.com4e00b9a2009-01-12 23:05:11 +00003398 # You should always have a space after a comma (either as fn arg or operator)
erg@google.comc6671232013-10-25 21:44:03 +00003399 #
3400 # This does not apply when the non-space character following the
3401 # comma is another comma, since the only time when that happens is
3402 # for empty macro arguments.
erg@google.com2aa59982013-10-28 19:09:25 +00003403 #
3404 # We run this check in two passes: first pass on elided lines to
3405 # verify that lines contain missing whitespaces, second pass on raw
3406 # lines to confirm that those missing whitespaces are not due to
3407 # elided comments.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003408 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3409 Search(r',[^,\s]', raw[linenum])):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003410 error(filename, linenum, 'whitespace/comma', 3,
3411 'Missing space after ,')
3412
erg@google.comd7d27472011-09-07 17:36:35 +00003413 # You should always have a space after a semicolon
3414 # except for few corner cases
3415 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3416 # space after ;
3417 if Search(r';[^\s};\\)/]', line):
3418 error(filename, linenum, 'whitespace/semicolon', 3,
3419 'Missing space after ;')
3420
avakulenko@google.com02af6282014-06-04 18:53:25 +00003421
Alex Vakulenko01e47232016-05-06 13:54:15 -07003422def _IsType(clean_lines, nesting_state, expr):
3423 """Check if expression looks like a type name, returns true if so.
3424
3425 Args:
3426 clean_lines: A CleansedLines instance containing the file.
3427 nesting_state: A NestingState instance which maintains information about
3428 the current stack of nested blocks being parsed.
3429 expr: The expression to check.
3430 Returns:
3431 True, if token looks like a type.
3432 """
3433 # Keep only the last token in the expression
3434 last_word = Match(r'^.*(\b\S+)$', expr)
3435 if last_word:
3436 token = last_word.group(1)
3437 else:
3438 token = expr
3439
3440 # Match native types and stdint types
3441 if _TYPES.match(token):
3442 return True
3443
3444 # Try a bit harder to match templated types. Walk up the nesting
3445 # stack until we find something that resembles a typename
3446 # declaration for what we are looking for.
3447 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) +
3448 r'\b')
3449 block_index = len(nesting_state.stack) - 1
3450 while block_index >= 0:
3451 if isinstance(nesting_state.stack[block_index], _NamespaceInfo):
3452 return False
3453
3454 # Found where the opening brace is. We want to scan from this
3455 # line up to the beginning of the function, minus a few lines.
3456 # template <typename Type1, // stop scanning here
3457 # ...>
3458 # class C
3459 # : public ... { // start scanning here
3460 last_line = nesting_state.stack[block_index].starting_linenum
3461
3462 next_block_start = 0
3463 if block_index > 0:
3464 next_block_start = nesting_state.stack[block_index - 1].starting_linenum
3465 first_line = last_line
3466 while first_line >= next_block_start:
3467 if clean_lines.elided[first_line].find('template') >= 0:
3468 break
3469 first_line -= 1
3470 if first_line < next_block_start:
3471 # Didn't find any "template" keyword before reaching the next block,
3472 # there are probably no template things to check for this block
3473 block_index -= 1
3474 continue
3475
3476 # Look for typename in the specified range
3477 for i in xrange(first_line, last_line + 1, 1):
3478 if Search(typename_pattern, clean_lines.elided[i]):
3479 return True
3480 block_index -= 1
3481
3482 return False
3483
3484
3485def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003486 """Checks for horizontal spacing near commas.
3487
3488 Args:
3489 filename: The name of the current file.
3490 clean_lines: A CleansedLines instance containing the file.
3491 linenum: The number of the line to check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003492 nesting_state: A NestingState instance which maintains information about
3493 the current stack of nested blocks being parsed.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003494 error: The function to call with any errors found.
3495 """
3496 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003497
erg@google.com8a95ecc2011-09-08 00:45:54 +00003498 # Except after an opening paren, or after another opening brace (in case of
3499 # an initializer list, for instance), you should have spaces before your
Alex Vakulenko01e47232016-05-06 13:54:15 -07003500 # braces when they are delimiting blocks, classes, namespaces etc.
3501 # And since you should never have braces at the beginning of a line,
3502 # this is an easy test. Except that braces used for initialization don't
3503 # follow the same rule; we often don't want spaces before those.
avakulenko@google.com554223d2014-12-04 22:00:20 +00003504 match = Match(r'^(.*[^ ({>]){', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07003505
erg@google.com2aa59982013-10-28 19:09:25 +00003506 if match:
3507 # Try a bit harder to check for brace initialization. This
3508 # happens in one of the following forms:
3509 # Constructor() : initializer_list_{} { ... }
3510 # Constructor{}.MemberFunction()
3511 # Type variable{};
3512 # FunctionCall(type{}, ...);
3513 # LastArgument(..., type{});
3514 # LOG(INFO) << type{} << " ...";
3515 # map_of_type[{...}] = ...;
avakulenko@google.com02af6282014-06-04 18:53:25 +00003516 # ternary = expr ? new type{} : nullptr;
3517 # OuterTemplate<InnerTemplateConstructor<Type>{}>
erg@google.com2aa59982013-10-28 19:09:25 +00003518 #
3519 # We check for the character following the closing brace, and
3520 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003521 # "{.;,)<>]:".
erg@google.com2aa59982013-10-28 19:09:25 +00003522 #
3523 # To account for nested initializer list, we allow any number of
3524 # closing braces up to "{;,)<". We can't simply silence the
3525 # warning on first sight of closing brace, because that would
3526 # cause false negatives for things that are not initializer lists.
3527 # Silence this: But not this:
3528 # Outer{ if (...) {
3529 # Inner{...} if (...){ // Missing space before {
3530 # }; }
3531 #
3532 # There is a false negative with this approach if people inserted
3533 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3534 # spurious semicolon with a separate check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003535 leading_text = match.group(1)
erg@google.com2aa59982013-10-28 19:09:25 +00003536 (endline, endlinenum, endpos) = CloseExpression(
3537 clean_lines, linenum, len(match.group(1)))
3538 trailing_text = ''
3539 if endpos > -1:
3540 trailing_text = endline[endpos:]
3541 for offset in xrange(endlinenum + 1,
3542 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3543 trailing_text += clean_lines.elided[offset]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003544 # We also suppress warnings for `uint64_t{expression}` etc., as the style
3545 # guide recommends brace initialization for integral types to avoid
3546 # overflow/truncation.
3547 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text)
3548 and not _IsType(clean_lines, nesting_state, leading_text)):
erg@google.com2aa59982013-10-28 19:09:25 +00003549 error(filename, linenum, 'whitespace/braces', 5,
3550 'Missing space before {')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003551
3552 # Make sure '} else {' has spaces.
3553 if Search(r'}else', line):
3554 error(filename, linenum, 'whitespace/braces', 5,
3555 'Missing space before else')
3556
erg@google.com4e00b9a2009-01-12 23:05:11 +00003557 # You shouldn't have a space before a semicolon at the end of the line.
3558 # There's a special case for "for" since the style guide allows space before
3559 # the semicolon there.
3560 if Search(r':\s*;\s*$', line):
3561 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00003562 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003563 elif Search(r'^\s*;\s*$', line):
3564 error(filename, linenum, 'whitespace/semicolon', 5,
3565 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00003566 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003567 elif (Search(r'\s+;\s*$', line) and
3568 not Search(r'\bfor\b', line)):
3569 error(filename, linenum, 'whitespace/semicolon', 5,
3570 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00003571 'statement, use {} instead.')
3572
avakulenko@google.com02af6282014-06-04 18:53:25 +00003573
3574def IsDecltype(clean_lines, linenum, column):
3575 """Check if the token ending on (linenum, column) is decltype().
3576
3577 Args:
3578 clean_lines: A CleansedLines instance containing the file.
3579 linenum: the number of the line to check.
3580 column: end column of the token to check.
3581 Returns:
3582 True if this token is decltype() expression, False otherwise.
3583 """
3584 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3585 if start_col < 0:
3586 return False
3587 if Search(r'\bdecltype\s*$', text[0:start_col]):
3588 return True
3589 return False
3590
3591
erg@google.com8a95ecc2011-09-08 00:45:54 +00003592def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3593 """Checks for additional blank line issues related to sections.
3594
3595 Currently the only thing checked here is blank line before protected/private.
3596
3597 Args:
3598 filename: The name of the current file.
3599 clean_lines: A CleansedLines instance containing the file.
3600 class_info: A _ClassInfo objects.
3601 linenum: The number of the line to check.
3602 error: The function to call with any errors found.
3603 """
3604 # Skip checks if the class is small, where small means 25 lines or less.
3605 # 25 lines seems like a good cutoff since that's the usual height of
3606 # terminals, and any class that can't fit in one screen can't really
3607 # be considered "small".
3608 #
3609 # Also skip checks if we are on the first line. This accounts for
3610 # classes that look like
3611 # class Foo { public: ... };
3612 #
3613 # If we didn't find the end of the class, last_line would be zero,
3614 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00003615 if (class_info.last_line - class_info.starting_linenum <= 24 or
3616 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003617 return
3618
3619 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3620 if matched:
3621 # Issue warning if the line before public/protected/private was
3622 # not a blank line, but don't do this if the previous line contains
3623 # "class" or "struct". This can happen two ways:
3624 # - We are at the beginning of the class.
3625 # - We are forward-declaring an inner class that is semantically
3626 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00003627 # Also ignores cases where the previous line ends with a backslash as can be
3628 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003629 prev_line = clean_lines.lines[linenum - 1]
3630 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00003631 not Search(r'\b(class|struct)\b', prev_line) and
3632 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003633 # Try a bit harder to find the beginning of the class. This is to
3634 # account for multi-line base-specifier lists, e.g.:
3635 # class Derived
3636 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00003637 end_class_head = class_info.starting_linenum
3638 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003639 if Search(r'\{\s*$', clean_lines.lines[i]):
3640 end_class_head = i
3641 break
3642 if end_class_head < linenum - 1:
3643 error(filename, linenum, 'whitespace/blank_line', 3,
3644 '"%s:" should be preceded by a blank line' % matched.group(1))
3645
3646
erg@google.com4e00b9a2009-01-12 23:05:11 +00003647def GetPreviousNonBlankLine(clean_lines, linenum):
3648 """Return the most recent non-blank line and its line number.
3649
3650 Args:
3651 clean_lines: A CleansedLines instance containing the file contents.
3652 linenum: The number of the line to check.
3653
3654 Returns:
3655 A tuple with two elements. The first element is the contents of the last
3656 non-blank line before the current line, or the empty string if this is the
3657 first non-blank line. The second is the line number of that line, or -1
3658 if this is the first non-blank line.
3659 """
3660
3661 prevlinenum = linenum - 1
3662 while prevlinenum >= 0:
3663 prevline = clean_lines.elided[prevlinenum]
3664 if not IsBlankLine(prevline): # if not a blank line...
3665 return (prevline, prevlinenum)
3666 prevlinenum -= 1
3667 return ('', -1)
3668
3669
3670def CheckBraces(filename, clean_lines, linenum, error):
3671 """Looks for misplaced braces (e.g. at the end of line).
3672
3673 Args:
3674 filename: The name of the current file.
3675 clean_lines: A CleansedLines instance containing the file.
3676 linenum: The number of the line to check.
3677 error: The function to call with any errors found.
3678 """
3679
3680 line = clean_lines.elided[linenum] # get rid of comments and strings
3681
3682 if Match(r'\s*{\s*$', line):
erg@google.com2aa59982013-10-28 19:09:25 +00003683 # We allow an open brace to start a line in the case where someone is using
3684 # braces in a block to explicitly create a new scope, which is commonly used
3685 # to control the lifetime of stack-allocated variables. Braces are also
3686 # used for brace initializers inside function calls. We don't detect this
3687 # perfectly: we just don't complain if the last non-whitespace character on
3688 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
Alex Vakulenko01e47232016-05-06 13:54:15 -07003689 # previous line starts a preprocessor block. We also allow a brace on the
3690 # following line if it is part of an array initialization and would not fit
3691 # within the 80 character limit of the preceding line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003692 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.com2aa59982013-10-28 19:09:25 +00003693 if (not Search(r'[,;:}{(]\s*$', prevline) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07003694 not Match(r'\s*#', prevline) and
3695 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003696 error(filename, linenum, 'whitespace/braces', 4,
3697 '{ should almost always be at the end of the previous line')
3698
3699 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003700 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003701 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3702 if Match(r'\s*}\s*$', prevline):
3703 error(filename, linenum, 'whitespace/newline', 4,
3704 'An else should appear on the same line as the preceding }')
3705
3706 # If braces come on one side of an else, they should be on both.
3707 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.com02af6282014-06-04 18:53:25 +00003708 if Search(r'else if\s*\(', line): # could be multi-line if
3709 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3710 # find the ( after the if
3711 pos = line.find('else if')
3712 pos = line.find('(', pos)
3713 if pos > 0:
3714 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3715 brace_on_right = endline[endpos:].find('{') != -1
3716 if brace_on_left != brace_on_right: # must be brace after if
3717 error(filename, linenum, 'readability/braces', 5,
3718 'If an else has a brace on one side, it should have it on both')
3719 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3720 error(filename, linenum, 'readability/braces', 5,
3721 'If an else has a brace on one side, it should have it on both')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003722
3723 # Likewise, an else should never have the else clause on the same line
3724 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3725 error(filename, linenum, 'whitespace/newline', 4,
3726 'Else clause should never be on same line as else (use 2 lines)')
3727
3728 # In the same way, a do/while should never be on one line
3729 if Match(r'\s*do [^\s{]', line):
3730 error(filename, linenum, 'whitespace/newline', 4,
3731 'do/while clauses should not be on a single line')
3732
avakulenko@google.com02af6282014-06-04 18:53:25 +00003733 # Check single-line if/else bodies. The style guide says 'curly braces are not
3734 # required for single-line statements'. We additionally allow multi-line,
3735 # single statements, but we reject anything with more than one semicolon in
3736 # it. This means that the first semicolon after the if should be at the end of
3737 # its line, and the line after that should have an indent level equal to or
3738 # lower than the if. We also check for ambiguous if/else nesting without
3739 # braces.
3740 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3741 if if_else_match and not Match(r'\s*#', line):
3742 if_indent = GetIndentLevel(line)
3743 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3744 if_match = Search(r'\bif\s*\(', line)
3745 if if_match:
3746 # This could be a multiline if condition, so find the end first.
3747 pos = if_match.end() - 1
3748 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3749 # Check for an opening brace, either directly after the if or on the next
3750 # line. If found, this isn't a single-statement conditional.
3751 if (not Match(r'\s*{', endline[endpos:])
3752 and not (Match(r'\s*$', endline[endpos:])
3753 and endlinenum < (len(clean_lines.elided) - 1)
3754 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3755 while (endlinenum < len(clean_lines.elided)
3756 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3757 endlinenum += 1
3758 endpos = 0
3759 if endlinenum < len(clean_lines.elided):
3760 endline = clean_lines.elided[endlinenum]
3761 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3762 # methods) and a single \ after the semicolon (for macros)
3763 endpos = endline.find(';')
3764 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003765 # Semicolon isn't the last character, there's something trailing.
3766 # Output a warning if the semicolon is not contained inside
3767 # a lambda expression.
3768 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3769 endline):
3770 error(filename, linenum, 'readability/braces', 4,
3771 'If/else bodies with multiple statements require braces')
avakulenko@google.com02af6282014-06-04 18:53:25 +00003772 elif endlinenum < len(clean_lines.elided) - 1:
3773 # Make sure the next line is dedented
3774 next_line = clean_lines.elided[endlinenum + 1]
3775 next_indent = GetIndentLevel(next_line)
3776 # With ambiguous nested if statements, this will error out on the
3777 # if that *doesn't* match the else, regardless of whether it's the
3778 # inner one or outer one.
3779 if (if_match and Match(r'\s*else\b', next_line)
3780 and next_indent != if_indent):
3781 error(filename, linenum, 'readability/braces', 4,
3782 'Else clause should be indented at the same level as if. '
3783 'Ambiguous nested if/else chains require braces.')
3784 elif next_indent > if_indent:
3785 error(filename, linenum, 'readability/braces', 4,
3786 'If/else bodies with multiple statements require braces')
3787
3788
3789def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3790 """Looks for redundant trailing semicolon.
3791
3792 Args:
3793 filename: The name of the current file.
3794 clean_lines: A CleansedLines instance containing the file.
3795 linenum: The number of the line to check.
3796 error: The function to call with any errors found.
3797 """
3798
3799 line = clean_lines.elided[linenum]
3800
erg@google.com2aa59982013-10-28 19:09:25 +00003801 # Block bodies should not be followed by a semicolon. Due to C++11
3802 # brace initialization, there are more places where semicolons are
3803 # required than not, so we use a whitelist approach to check these
3804 # rather than a blacklist. These are the places where "};" should
3805 # be replaced by just "}":
3806 # 1. Some flavor of block following closing parenthesis:
3807 # for (;;) {};
3808 # while (...) {};
3809 # switch (...) {};
3810 # Function(...) {};
3811 # if (...) {};
3812 # if (...) else if (...) {};
3813 #
3814 # 2. else block:
3815 # if (...) else {};
3816 #
3817 # 3. const member function:
3818 # Function(...) const {};
3819 #
3820 # 4. Block following some statement:
3821 # x = 42;
3822 # {};
3823 #
3824 # 5. Block at the beginning of a function:
3825 # Function(...) {
3826 # {};
3827 # }
3828 #
3829 # Note that naively checking for the preceding "{" will also match
3830 # braces inside multi-dimensional arrays, but this is fine since
3831 # that expression will not contain semicolons.
3832 #
3833 # 6. Block following another block:
3834 # while (true) {}
3835 # {};
3836 #
3837 # 7. End of namespaces:
3838 # namespace {};
3839 #
3840 # These semicolons seems far more common than other kinds of
3841 # redundant semicolons, possibly due to people converting classes
3842 # to namespaces. For now we do not warn for this case.
3843 #
3844 # Try matching case 1 first.
3845 match = Match(r'^(.*\)\s*)\{', line)
3846 if match:
3847 # Matched closing parenthesis (case 1). Check the token before the
3848 # matching opening parenthesis, and don't warn if it looks like a
3849 # macro. This avoids these false positives:
3850 # - macro that defines a base class
3851 # - multi-line macro that defines a base class
3852 # - macro that defines the whole class-head
3853 #
3854 # But we still issue warnings for macros that we know are safe to
3855 # warn, specifically:
3856 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3857 # - TYPED_TEST
3858 # - INTERFACE_DEF
3859 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3860 #
3861 # We implement a whitelist of safe macros instead of a blacklist of
3862 # unsafe macros, even though the latter appears less frequently in
3863 # google code and would have been easier to implement. This is because
3864 # the downside for getting the whitelist wrong means some extra
3865 # semicolons, while the downside for getting the blacklist wrong
3866 # would result in compile errors.
3867 #
avakulenko@google.com554223d2014-12-04 22:00:20 +00003868 # In addition to macros, we also don't want to warn on
3869 # - Compound literals
3870 # - Lambdas
Alex Vakulenko01e47232016-05-06 13:54:15 -07003871 # - alignas specifier with anonymous structs
3872 # - decltype
erg@google.com2aa59982013-10-28 19:09:25 +00003873 closing_brace_pos = match.group(1).rfind(')')
3874 opening_parenthesis = ReverseCloseExpression(
3875 clean_lines, linenum, closing_brace_pos)
3876 if opening_parenthesis[2] > -1:
3877 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003878 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix)
avakulenko@google.com02af6282014-06-04 18:53:25 +00003879 func = Match(r'^(.*\])\s*$', line_prefix)
erg@google.com2aa59982013-10-28 19:09:25 +00003880 if ((macro and
3881 macro.group(1) not in (
3882 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3883 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3884 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.com02af6282014-06-04 18:53:25 +00003885 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
avakulenko@google.com554223d2014-12-04 22:00:20 +00003886 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or
Alex Vakulenko01e47232016-05-06 13:54:15 -07003887 Search(r'\bdecltype$', line_prefix) or
erg@google.com2aa59982013-10-28 19:09:25 +00003888 Search(r'\s+=\s*$', line_prefix)):
3889 match = None
avakulenko@google.com02af6282014-06-04 18:53:25 +00003890 if (match and
3891 opening_parenthesis[1] > 1 and
3892 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3893 # Multi-line lambda-expression
3894 match = None
erg@google.com2aa59982013-10-28 19:09:25 +00003895
3896 else:
3897 # Try matching cases 2-3.
3898 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3899 if not match:
3900 # Try matching cases 4-6. These are always matched on separate lines.
3901 #
3902 # Note that we can't simply concatenate the previous line to the
3903 # current line and do a single match, otherwise we may output
3904 # duplicate warnings for the blank line case:
3905 # if (cond) {
3906 # // blank line
3907 # }
3908 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3909 if prevline and Search(r'[;{}]\s*$', prevline):
3910 match = Match(r'^(\s*)\{', line)
3911
3912 # Check matching closing brace
3913 if match:
3914 (endline, endlinenum, endpos) = CloseExpression(
3915 clean_lines, linenum, len(match.group(1)))
3916 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3917 # Current {} pair is eligible for semicolon check, and we have found
3918 # the redundant semicolon, output warning here.
3919 #
3920 # Note: because we are scanning forward for opening braces, and
3921 # outputting warnings for the matching closing brace, if there are
3922 # nested blocks with trailing semicolons, we will get the error
3923 # messages in reversed order.
Piotr Semenovb7e2ef62016-05-20 18:39:34 +03003924
3925 # We need to check the line forward for NOLINT
3926 raw_lines = clean_lines.raw_lines
3927 ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1,
3928 error)
3929 ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum,
3930 error)
3931
erg@google.com2aa59982013-10-28 19:09:25 +00003932 error(filename, endlinenum, 'readability/braces', 4,
3933 "You don't need a ; after a }")
erg@google.com4e00b9a2009-01-12 23:05:11 +00003934
3935
erg@google.comc6671232013-10-25 21:44:03 +00003936def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3937 """Look for empty loop/conditional body with only a single semicolon.
erg@google.comd350fe52013-01-14 17:51:48 +00003938
3939 Args:
3940 filename: The name of the current file.
3941 clean_lines: A CleansedLines instance containing the file.
3942 linenum: The number of the line to check.
3943 error: The function to call with any errors found.
3944 """
3945
3946 # Search for loop keywords at the beginning of the line. Because only
3947 # whitespaces are allowed before the keywords, this will also ignore most
3948 # do-while-loops, since those lines should start with closing brace.
erg@google.comc6671232013-10-25 21:44:03 +00003949 #
3950 # We also check "if" blocks here, since an empty conditional block
3951 # is likely an error.
erg@google.comd350fe52013-01-14 17:51:48 +00003952 line = clean_lines.elided[linenum]
erg@google.comc6671232013-10-25 21:44:03 +00003953 matched = Match(r'\s*(for|while|if)\s*\(', line)
3954 if matched:
Alex Vakulenko01e47232016-05-06 13:54:15 -07003955 # Find the end of the conditional expression.
erg@google.comd350fe52013-01-14 17:51:48 +00003956 (end_line, end_linenum, end_pos) = CloseExpression(
3957 clean_lines, linenum, line.find('('))
3958
3959 # Output warning if what follows the condition expression is a semicolon.
3960 # No warning for all other cases, including whitespace or newline, since we
3961 # have a separate check for semicolons preceded by whitespace.
3962 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
erg@google.comc6671232013-10-25 21:44:03 +00003963 if matched.group(1) == 'if':
3964 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
3965 'Empty conditional bodies should use {}')
3966 else:
3967 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
3968 'Empty loop bodies should use {} or continue')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003969
Alex Vakulenko01e47232016-05-06 13:54:15 -07003970 # Check for if statements that have completely empty bodies (no comments)
3971 # and no else clauses.
3972 if end_pos >= 0 and matched.group(1) == 'if':
3973 # Find the position of the opening { for the if statement.
3974 # Return without logging an error if it has no brackets.
3975 opening_linenum = end_linenum
3976 opening_line_fragment = end_line[end_pos:]
3977 # Loop until EOF or find anything that's not whitespace or opening {.
3978 while not Search(r'^\s*\{', opening_line_fragment):
3979 if Search(r'^(?!\s*$)', opening_line_fragment):
3980 # Conditional has no brackets.
3981 return
3982 opening_linenum += 1
3983 if opening_linenum == len(clean_lines.elided):
3984 # Couldn't find conditional's opening { or any code before EOF.
3985 return
3986 opening_line_fragment = clean_lines.elided[opening_linenum]
3987 # Set opening_line (opening_line_fragment may not be entire opening line).
3988 opening_line = clean_lines.elided[opening_linenum]
3989
3990 # Find the position of the closing }.
3991 opening_pos = opening_line_fragment.find('{')
3992 if opening_linenum == end_linenum:
3993 # We need to make opening_pos relative to the start of the entire line.
3994 opening_pos += end_pos
3995 (closing_line, closing_linenum, closing_pos) = CloseExpression(
3996 clean_lines, opening_linenum, opening_pos)
3997 if closing_pos < 0:
3998 return
3999
4000 # Now construct the body of the conditional. This consists of the portion
4001 # of the opening line after the {, all lines until the closing line,
4002 # and the portion of the closing line before the }.
4003 if (clean_lines.raw_lines[opening_linenum] !=
4004 CleanseComments(clean_lines.raw_lines[opening_linenum])):
4005 # Opening line ends with a comment, so conditional isn't empty.
4006 return
4007 if closing_linenum > opening_linenum:
4008 # Opening line after the {. Ignore comments here since we checked above.
4009 body = list(opening_line[opening_pos+1:])
4010 # All lines until closing line, excluding closing line, with comments.
4011 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum])
4012 # Closing line before the }. Won't (and can't) have comments.
4013 body.append(clean_lines.elided[closing_linenum][:closing_pos-1])
4014 body = '\n'.join(body)
4015 else:
4016 # If statement has brackets and fits on a single line.
4017 body = opening_line[opening_pos+1:closing_pos-1]
4018
4019 # Check if the body is empty
4020 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body):
4021 return
4022 # The body is empty. Now make sure there's not an else clause.
4023 current_linenum = closing_linenum
4024 current_line_fragment = closing_line[closing_pos:]
4025 # Loop until EOF or find anything that's not whitespace or else clause.
4026 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment):
4027 if Search(r'^(?=\s*else)', current_line_fragment):
4028 # Found an else clause, so don't log an error.
4029 return
4030 current_linenum += 1
4031 if current_linenum == len(clean_lines.elided):
4032 break
4033 current_line_fragment = clean_lines.elided[current_linenum]
4034
4035 # The body is empty and there's no else clause until EOF or other code.
4036 error(filename, end_linenum, 'whitespace/empty_if_body', 4,
4037 ('If statement had no body and no else clause'))
4038
erg@google.com4e00b9a2009-01-12 23:05:11 +00004039
avakulenko@google.com02af6282014-06-04 18:53:25 +00004040def FindCheckMacro(line):
4041 """Find a replaceable CHECK-like macro.
4042
4043 Args:
4044 line: line to search on.
4045 Returns:
4046 (macro name, start position), or (None, -1) if no replaceable
4047 macro is found.
4048 """
4049 for macro in _CHECK_MACROS:
4050 i = line.find(macro)
4051 if i >= 0:
4052 # Find opening parenthesis. Do a regular expression match here
4053 # to make sure that we are matching the expected CHECK macro, as
4054 # opposed to some other macro that happens to contain the CHECK
4055 # substring.
4056 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4057 if not matched:
4058 continue
4059 return (macro, len(matched.group(1)))
4060 return (None, -1)
4061
4062
erg@google.com4e00b9a2009-01-12 23:05:11 +00004063def CheckCheck(filename, clean_lines, linenum, error):
4064 """Checks the use of CHECK and EXPECT macros.
4065
4066 Args:
4067 filename: The name of the current file.
4068 clean_lines: A CleansedLines instance containing the file.
4069 linenum: The number of the line to check.
4070 error: The function to call with any errors found.
4071 """
4072
4073 # Decide the set of replacement macros that should be suggested
erg@google.comc6671232013-10-25 21:44:03 +00004074 lines = clean_lines.elided
avakulenko@google.com02af6282014-06-04 18:53:25 +00004075 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4076 if not check_macro:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004077 return
4078
erg@google.comc6671232013-10-25 21:44:03 +00004079 # Find end of the boolean expression by matching parentheses
4080 (last_line, end_line, end_pos) = CloseExpression(
4081 clean_lines, linenum, start_pos)
4082 if end_pos < 0:
4083 return
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004084
4085 # If the check macro is followed by something other than a
4086 # semicolon, assume users will log their own custom error messages
4087 # and don't suggest any replacements.
4088 if not Match(r'\s*;', last_line[end_pos:]):
4089 return
4090
erg@google.comc6671232013-10-25 21:44:03 +00004091 if linenum == end_line:
4092 expression = lines[linenum][start_pos + 1:end_pos - 1]
4093 else:
4094 expression = lines[linenum][start_pos + 1:]
4095 for i in xrange(linenum + 1, end_line):
4096 expression += lines[i]
4097 expression += last_line[0:end_pos - 1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004098
erg@google.comc6671232013-10-25 21:44:03 +00004099 # Parse expression so that we can take parentheses into account.
4100 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4101 # which is not replaceable by CHECK_LE.
4102 lhs = ''
4103 rhs = ''
4104 operator = None
4105 while expression:
4106 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4107 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4108 if matched:
4109 token = matched.group(1)
4110 if token == '(':
4111 # Parenthesized operand
4112 expression = matched.group(2)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004113 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
erg@google.comc6671232013-10-25 21:44:03 +00004114 if end < 0:
4115 return # Unmatched parenthesis
4116 lhs += '(' + expression[0:end]
4117 expression = expression[end:]
4118 elif token in ('&&', '||'):
4119 # Logical and/or operators. This means the expression
4120 # contains more than one term, for example:
4121 # CHECK(42 < a && a < b);
4122 #
4123 # These are not replaceable with CHECK_LE, so bail out early.
4124 return
4125 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4126 # Non-relational operator
4127 lhs += token
4128 expression = matched.group(2)
4129 else:
4130 # Relational operator
4131 operator = token
4132 rhs = matched.group(2)
4133 break
4134 else:
4135 # Unparenthesized operand. Instead of appending to lhs one character
4136 # at a time, we do another regular expression match to consume several
4137 # characters at once if possible. Trivial benchmark shows that this
4138 # is more efficient when the operands are longer than a single
4139 # character, which is generally the case.
4140 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4141 if not matched:
4142 matched = Match(r'^(\s*\S)(.*)$', expression)
4143 if not matched:
4144 break
4145 lhs += matched.group(1)
4146 expression = matched.group(2)
4147
4148 # Only apply checks if we got all parts of the boolean expression
4149 if not (lhs and operator and rhs):
4150 return
4151
4152 # Check that rhs do not contain logical operators. We already know
4153 # that lhs is fine since the loop above parses out && and ||.
4154 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4155 return
4156
4157 # At least one of the operands must be a constant literal. This is
4158 # to avoid suggesting replacements for unprintable things like
4159 # CHECK(variable != iterator)
4160 #
4161 # The following pattern matches decimal, hex integers, strings, and
4162 # characters (in that order).
4163 lhs = lhs.strip()
4164 rhs = rhs.strip()
4165 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4166 if Match(match_constant, lhs) or Match(match_constant, rhs):
4167 # Note: since we know both lhs and rhs, we can provide a more
4168 # descriptive error message like:
4169 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4170 # Instead of:
4171 # Consider using CHECK_EQ instead of CHECK(a == b)
4172 #
4173 # We are still keeping the less descriptive message because if lhs
4174 # or rhs gets long, the error message might become unreadable.
4175 error(filename, linenum, 'readability/check', 2,
4176 'Consider using %s instead of %s(a %s b)' % (
4177 _CHECK_REPLACEMENT[check_macro][operator],
4178 check_macro, operator))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004179
4180
erg@google.comd350fe52013-01-14 17:51:48 +00004181def CheckAltTokens(filename, clean_lines, linenum, error):
4182 """Check alternative keywords being used in boolean expressions.
4183
4184 Args:
4185 filename: The name of the current file.
4186 clean_lines: A CleansedLines instance containing the file.
4187 linenum: The number of the line to check.
4188 error: The function to call with any errors found.
4189 """
4190 line = clean_lines.elided[linenum]
4191
4192 # Avoid preprocessor lines
4193 if Match(r'^\s*#', line):
4194 return
4195
4196 # Last ditch effort to avoid multi-line comments. This will not help
4197 # if the comment started before the current line or ended after the
4198 # current line, but it catches most of the false positives. At least,
4199 # it provides a way to workaround this warning for people who use
4200 # multi-line comments in preprocessor macros.
4201 #
4202 # TODO(unknown): remove this once cpplint has better support for
4203 # multi-line comments.
4204 if line.find('/*') >= 0 or line.find('*/') >= 0:
4205 return
4206
4207 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4208 error(filename, linenum, 'readability/alt_tokens', 2,
4209 'Use operator %s instead of %s' % (
4210 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4211
4212
erg@google.com4e00b9a2009-01-12 23:05:11 +00004213def GetLineWidth(line):
4214 """Determines the width of the line in column positions.
4215
4216 Args:
4217 line: A string, which may be a Unicode string.
4218
4219 Returns:
4220 The width of the line in column positions, accounting for Unicode
4221 combining characters and wide characters.
4222 """
4223 if isinstance(line, unicode):
4224 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00004225 for uc in unicodedata.normalize('NFC', line):
4226 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004227 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00004228 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004229 width += 1
4230 return width
4231 else:
4232 return len(line)
4233
4234
erg@google.comd350fe52013-01-14 17:51:48 +00004235def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00004236 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004237 """Checks rules from the 'C++ style rules' section of cppguide.html.
4238
4239 Most of these rules are hard to test (naming, comment style), but we
4240 do what we can. In particular we check for 2-space indents, line lengths,
4241 tab usage, spaces inside code, etc.
4242
4243 Args:
4244 filename: The name of the current file.
4245 clean_lines: A CleansedLines instance containing the file.
4246 linenum: The number of the line to check.
4247 file_extension: The extension (without the dot) of the filename.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004248 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00004249 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004250 error: The function to call with any errors found.
4251 """
4252
erg@google.com2aa59982013-10-28 19:09:25 +00004253 # Don't use "elided" lines here, otherwise we can't check commented lines.
4254 # Don't want to use "raw" either, because we don't want to check inside C++11
4255 # raw strings,
4256 raw_lines = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00004257 line = raw_lines[linenum]
Alex Vakulenko01e47232016-05-06 13:54:15 -07004258 prev = raw_lines[linenum - 1] if linenum > 0 else ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00004259
4260 if line.find('\t') != -1:
4261 error(filename, linenum, 'whitespace/tab', 1,
4262 'Tab found; better to use spaces')
4263
4264 # One or three blank spaces at the beginning of the line is weird; it's
4265 # hard to reconcile that with 2-space indents.
4266 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4267 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4268 # if(RLENGTH > 20) complain = 0;
4269 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4270 # if(match(prev, "&& *$")) complain = 0;
4271 # if(match(prev, "\\|\\| *$")) complain = 0;
4272 # if(match(prev, "[\",=><] *$")) complain = 0;
4273 # if(match($0, " <<")) complain = 0;
4274 # if(match(prev, " +for \\(")) complain = 0;
4275 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.com02af6282014-06-04 18:53:25 +00004276 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4277 classinfo = nesting_state.InnermostClass()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004278 initial_spaces = 0
4279 cleansed_line = clean_lines.elided[linenum]
4280 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4281 initial_spaces += 1
avakulenko@google.com02af6282014-06-04 18:53:25 +00004282 # There are certain situations we allow one space, notably for
4283 # section labels, and also lines containing multi-line raw strings.
Alex Vakulenko01e47232016-05-06 13:54:15 -07004284 # We also don't check for lines that look like continuation lines
4285 # (of lines ending in double quotes, commas, equals, or angle brackets)
4286 # because the rules for how to indent those are non-trivial.
4287 if (not Search(r'[",=><] *$', prev) and
4288 (initial_spaces == 1 or initial_spaces == 3) and
4289 not Match(scope_or_label_pattern, cleansed_line) and
4290 not (clean_lines.raw_lines[linenum] != line and
4291 Match(r'^\s*""', line))):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004292 error(filename, linenum, 'whitespace/indent', 3,
4293 'Weird number of spaces at line-start. '
4294 'Are you using a 2-space indent?')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004295
Alex Vakulenko01e47232016-05-06 13:54:15 -07004296 if line and line[-1].isspace():
4297 error(filename, linenum, 'whitespace/end_of_line', 4,
4298 'Line ends in whitespace. Consider deleting these extra spaces.')
4299
erg@google.com4e00b9a2009-01-12 23:05:11 +00004300 # Check if the line is a header guard.
4301 is_header_guard = False
LukeCz7197a242016-09-24 13:27:35 -05004302 if IsHeaderExtension(file_extension):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004303 cppvar = GetHeaderGuardCPPVariable(filename)
4304 if (line.startswith('#ifndef %s' % cppvar) or
4305 line.startswith('#define %s' % cppvar) or
4306 line.startswith('#endif // %s' % cppvar)):
4307 is_header_guard = True
4308 # #include lines and header guards can be long, since there's no clean way to
4309 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00004310 #
4311 # URLs can be long too. It's possible to split these, but it makes them
4312 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00004313 #
4314 # The "$Id:...$" comment may also get very long without it being the
4315 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00004316 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00004317 not Match(r'^\s*//.*http(s?)://\S*$', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004318 not Match(r'^\s*//\s*[^\s]*$', line) and
erg@google.comd7d27472011-09-07 17:36:35 +00004319 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004320 line_width = GetLineWidth(line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004321 if line_width > _line_length:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004322 error(filename, linenum, 'whitespace/line_length', 2,
erg@google.comab53edf2013-11-05 22:23:37 +00004323 'Lines should be <= %i characters long' % _line_length)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004324
4325 if (cleansed_line.count(';') > 1 and
4326 # for loops are allowed two ;'s (and may run over two lines).
4327 cleansed_line.find('for') == -1 and
4328 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4329 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4330 # It's ok to have many commands in a switch case that fits in 1 line
4331 not ((cleansed_line.find('case ') != -1 or
4332 cleansed_line.find('default:') != -1) and
4333 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00004334 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00004335 'More than one command on the same line')
4336
4337 # Some more style checks
4338 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004339 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
erg@google.comc6671232013-10-25 21:44:03 +00004340 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004341 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
4342 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004343 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4344 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4345 CheckCommaSpacing(filename, clean_lines, linenum, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004346 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004347 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004348 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004349 CheckAltTokens(filename, clean_lines, linenum, error)
4350 classinfo = nesting_state.InnermostClass()
4351 if classinfo:
4352 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004353
4354
erg@google.com4e00b9a2009-01-12 23:05:11 +00004355_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4356# Matches the first component of a filename delimited by -s and _s. That is:
4357# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4358# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4359# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4360# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4361_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4362
4363
4364def _DropCommonSuffixes(filename):
4365 """Drops common suffixes like _test.cc or -inl.h from filename.
4366
4367 For example:
4368 >>> _DropCommonSuffixes('foo/foo-inl.h')
4369 'foo/foo'
4370 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4371 'foo/bar/foo'
4372 >>> _DropCommonSuffixes('foo/foo_internal.h')
4373 'foo/foo'
4374 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4375 'foo/foo_unusualinternal'
4376
4377 Args:
4378 filename: The input filename.
4379
4380 Returns:
4381 The filename with the common suffix removed.
4382 """
4383 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4384 'inl.h', 'impl.h', 'internal.h'):
4385 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4386 filename[-len(suffix) - 1] in ('-', '_')):
4387 return filename[:-len(suffix) - 1]
4388 return os.path.splitext(filename)[0]
4389
4390
erg@google.com4e00b9a2009-01-12 23:05:11 +00004391def _ClassifyInclude(fileinfo, include, is_system):
4392 """Figures out what kind of header 'include' is.
4393
4394 Args:
4395 fileinfo: The current file cpplint is running over. A FileInfo instance.
4396 include: The path to a #included file.
4397 is_system: True if the #include used <> rather than "".
4398
4399 Returns:
4400 One of the _XXX_HEADER constants.
4401
4402 For example:
4403 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4404 _C_SYS_HEADER
4405 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4406 _CPP_SYS_HEADER
4407 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4408 _LIKELY_MY_HEADER
4409 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4410 ... 'bar/foo_other_ext.h', False)
4411 _POSSIBLE_MY_HEADER
4412 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4413 _OTHER_HEADER
4414 """
4415 # This is a list of all standard c++ header files, except
4416 # those already checked for above.
erg@google.comfd5da632013-10-25 17:39:45 +00004417 is_cpp_h = include in _CPP_HEADERS
erg@google.com4e00b9a2009-01-12 23:05:11 +00004418
4419 if is_system:
4420 if is_cpp_h:
4421 return _CPP_SYS_HEADER
4422 else:
4423 return _C_SYS_HEADER
4424
4425 # If the target file and the include we're checking share a
4426 # basename when we drop common extensions, and the include
4427 # lives in . , then it's likely to be owned by the target file.
4428 target_dir, target_base = (
4429 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4430 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4431 if target_base == include_base and (
4432 include_dir == target_dir or
4433 include_dir == os.path.normpath(target_dir + '/../public')):
4434 return _LIKELY_MY_HEADER
4435
4436 # If the target and include share some initial basename
4437 # component, it's possible the target is implementing the
4438 # include, so it's allowed to be first, but we'll never
4439 # complain if it's not there.
4440 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4441 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4442 if (target_first_component and include_first_component and
4443 target_first_component.group(0) ==
4444 include_first_component.group(0)):
4445 return _POSSIBLE_MY_HEADER
4446
4447 return _OTHER_HEADER
4448
4449
erg@google.coma87abb82009-02-24 01:41:01 +00004450
erg@google.come35f7652009-06-19 20:52:09 +00004451def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4452 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004453
erg@google.come35f7652009-06-19 20:52:09 +00004454 Strings on #include lines are NOT removed from elided line, to make
4455 certain tasks easier. However, to prevent false positives, checks
4456 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004457
4458 Args:
4459 filename: The name of the current file.
4460 clean_lines: A CleansedLines instance containing the file.
4461 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004462 include_state: An _IncludeState instance in which the headers are inserted.
4463 error: The function to call with any errors found.
4464 """
4465 fileinfo = FileInfo(filename)
erg@google.come35f7652009-06-19 20:52:09 +00004466 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004467
4468 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004469 # Only do this check if the included header follows google naming
4470 # conventions. If not, assume that it's a 3rd party API that
4471 # requires special include conventions.
4472 #
4473 # We also make an exception for Lua headers, which follow google
4474 # naming convention but not the include convention.
4475 match = Match(r'#include\s*"([^/]+\.h)"', line)
4476 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004477 error(filename, linenum, 'build/include', 4,
4478 'Include the directory when naming .h files')
4479
4480 # we shouldn't include a file more than once. actually, there are a
4481 # handful of instances where doing so is okay, but in general it's
4482 # not.
erg@google.come35f7652009-06-19 20:52:09 +00004483 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004484 if match:
4485 include = match.group(2)
4486 is_system = (match.group(1) == '<')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004487 duplicate_line = include_state.FindHeader(include)
4488 if duplicate_line >= 0:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004489 error(filename, linenum, 'build/include', 4,
4490 '"%s" already included at %s:%s' %
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004491 (include, filename, duplicate_line))
avakulenko@google.com554223d2014-12-04 22:00:20 +00004492 elif (include.endswith('.cc') and
4493 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)):
4494 error(filename, linenum, 'build/include', 4,
4495 'Do not include .cc files from other packages')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004496 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4497 include_state.include_list[-1].append((include, linenum))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004498
4499 # We want to ensure that headers appear in the right order:
4500 # 1) for foo.cc, foo.h (preferred location)
4501 # 2) c system files
4502 # 3) cpp system files
4503 # 4) for foo.cc, foo.h (deprecated location)
4504 # 5) other google headers
4505 #
4506 # We classify each include statement as one of those 5 types
4507 # using a number of techniques. The include_state object keeps
4508 # track of the highest type seen, and complains if we see a
4509 # lower type after that.
4510 error_message = include_state.CheckNextIncludeOrder(
4511 _ClassifyInclude(fileinfo, include, is_system))
4512 if error_message:
4513 error(filename, linenum, 'build/include_order', 4,
4514 '%s. Should be: %s.h, c system, c++ system, other.' %
4515 (error_message, fileinfo.BaseName()))
erg@google.comfd5da632013-10-25 17:39:45 +00004516 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4517 if not include_state.IsInAlphabeticalOrder(
4518 clean_lines, linenum, canonical_include):
erg@google.coma868d2d2009-10-09 21:18:45 +00004519 error(filename, linenum, 'build/include_alpha', 4,
4520 'Include "%s" not in alphabetical order' % include)
erg@google.comfd5da632013-10-25 17:39:45 +00004521 include_state.SetLastHeader(canonical_include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004522
erg@google.come35f7652009-06-19 20:52:09 +00004523
erg@google.com8a95ecc2011-09-08 00:45:54 +00004524
4525def _GetTextInside(text, start_pattern):
erg@google.com2aa59982013-10-28 19:09:25 +00004526 r"""Retrieves all the text between matching open and close parentheses.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004527
4528 Given a string of lines and a regular expression string, retrieve all the text
4529 following the expression and between opening punctuation symbols like
4530 (, [, or {, and the matching close-punctuation symbol. This properly nested
4531 occurrences of the punctuations, so for the text like
4532 printf(a(), b(c()));
4533 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4534 start_pattern must match string having an open punctuation symbol at the end.
4535
4536 Args:
4537 text: The lines to extract text. Its comments and strings must be elided.
4538 It can be single line and can span multiple lines.
4539 start_pattern: The regexp string indicating where to start extracting
4540 the text.
4541 Returns:
4542 The extracted text.
4543 None if either the opening string or ending punctuation could not be found.
4544 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00004545 # TODO(unknown): Audit cpplint.py to see what places could be profitably
erg@google.com8a95ecc2011-09-08 00:45:54 +00004546 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4547
4548 # Give opening punctuations to get the matching close-punctuations.
4549 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4550 closing_punctuation = set(matching_punctuation.itervalues())
4551
4552 # Find the position to start extracting text.
4553 match = re.search(start_pattern, text, re.M)
4554 if not match: # start_pattern not found in text.
4555 return None
4556 start_position = match.end(0)
4557
4558 assert start_position > 0, (
4559 'start_pattern must ends with an opening punctuation.')
4560 assert text[start_position - 1] in matching_punctuation, (
4561 'start_pattern must ends with an opening punctuation.')
4562 # Stack of closing punctuations we expect to have in text after position.
4563 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4564 position = start_position
4565 while punctuation_stack and position < len(text):
4566 if text[position] == punctuation_stack[-1]:
4567 punctuation_stack.pop()
4568 elif text[position] in closing_punctuation:
4569 # A closing punctuation without matching opening punctuations.
4570 return None
4571 elif text[position] in matching_punctuation:
4572 punctuation_stack.append(matching_punctuation[text[position]])
4573 position += 1
4574 if punctuation_stack:
4575 # Opening punctuations left without matching close-punctuations.
4576 return None
4577 # punctuations match.
4578 return text[start_position:position - 1]
4579
4580
erg@google.comfd5da632013-10-25 17:39:45 +00004581# Patterns for matching call-by-reference parameters.
erg@google.com2aa59982013-10-28 19:09:25 +00004582#
4583# Supports nested templates up to 2 levels deep using this messy pattern:
4584# < (?: < (?: < [^<>]*
4585# >
4586# | [^<>] )*
4587# >
4588# | [^<>] )*
4589# >
erg@google.comfd5da632013-10-25 17:39:45 +00004590_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4591_RE_PATTERN_TYPE = (
4592 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
erg@google.com2aa59982013-10-28 19:09:25 +00004593 r'(?:\w|'
4594 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4595 r'::)+')
erg@google.comfd5da632013-10-25 17:39:45 +00004596# A call-by-reference parameter ends with '& identifier'.
4597_RE_PATTERN_REF_PARAM = re.compile(
4598 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4599 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4600# A call-by-const-reference parameter either ends with 'const& identifier'
4601# or looks like 'const type& identifier' when 'type' is atomic.
4602_RE_PATTERN_CONST_REF_PARAM = (
4603 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4604 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004605# Stream types.
4606_RE_PATTERN_REF_STREAM_PARAM = (
4607 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')')
erg@google.comfd5da632013-10-25 17:39:45 +00004608
4609
4610def CheckLanguage(filename, clean_lines, linenum, file_extension,
4611 include_state, nesting_state, error):
erg@google.come35f7652009-06-19 20:52:09 +00004612 """Checks rules from the 'C++ language rules' section of cppguide.html.
4613
4614 Some of these rules are hard to test (function overloading, using
4615 uint32 inappropriately), but we do the best we can.
4616
4617 Args:
4618 filename: The name of the current file.
4619 clean_lines: A CleansedLines instance containing the file.
4620 linenum: The number of the line to check.
4621 file_extension: The extension (without the dot) of the filename.
4622 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004623 nesting_state: A NestingState instance which maintains information about
erg@google.comfd5da632013-10-25 17:39:45 +00004624 the current stack of nested blocks being parsed.
erg@google.come35f7652009-06-19 20:52:09 +00004625 error: The function to call with any errors found.
4626 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00004627 # If the line is empty or consists of entirely a comment, no need to
4628 # check it.
4629 line = clean_lines.elided[linenum]
4630 if not line:
4631 return
4632
erg@google.come35f7652009-06-19 20:52:09 +00004633 match = _RE_PATTERN_INCLUDE.search(line)
4634 if match:
4635 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4636 return
4637
erg@google.com2aa59982013-10-28 19:09:25 +00004638 # Reset include state across preprocessor directives. This is meant
4639 # to silence warnings for conditional includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004640 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4641 if match:
4642 include_state.ResetSection(match.group(1))
erg@google.com2aa59982013-10-28 19:09:25 +00004643
erg@google.com4e00b9a2009-01-12 23:05:11 +00004644 # Make Windows paths like Unix.
4645 fullname = os.path.abspath(filename).replace('\\', '/')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004646
avakulenko@google.com02af6282014-06-04 18:53:25 +00004647 # Perform other checks now that we are sure that this is not an include line
4648 CheckCasts(filename, clean_lines, linenum, error)
4649 CheckGlobalStatic(filename, clean_lines, linenum, error)
4650 CheckPrintf(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004651
LukeCz7197a242016-09-24 13:27:35 -05004652 if IsHeaderExtension(file_extension):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004653 # TODO(unknown): check that 1-arg constructors are explicit.
4654 # How to tell it's a constructor?
4655 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004656 # TODO(unknown): check that classes declare or disable copy/assign
erg@google.com4e00b9a2009-01-12 23:05:11 +00004657 # (level 1 error)
4658 pass
4659
4660 # Check if people are using the verboten C basic types. The only exception
4661 # we regularly allow is "unsigned short port" for port.
4662 if Search(r'\bshort port\b', line):
4663 if not Search(r'\bunsigned short port\b', line):
4664 error(filename, linenum, 'runtime/int', 4,
4665 'Use "unsigned short" for ports, not "short"')
4666 else:
4667 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4668 if match:
4669 error(filename, linenum, 'runtime/int', 4,
4670 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4671
erg@google.coma868d2d2009-10-09 21:18:45 +00004672 # Check if some verboten operator overloading is going on
4673 # TODO(unknown): catch out-of-line unary operator&:
4674 # class X {};
4675 # int operator&(const X& x) { return 42; } // unary operator&
4676 # The trick is it's hard to tell apart from binary operator&:
4677 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4678 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4679 error(filename, linenum, 'runtime/operator', 4,
4680 'Unary operator& is dangerous. Do not use it.')
4681
erg@google.com4e00b9a2009-01-12 23:05:11 +00004682 # Check for suspicious usage of "if" like
4683 # } if (a == b) {
4684 if Search(r'\}\s*if\s*\(', line):
4685 error(filename, linenum, 'readability/braces', 4,
4686 'Did you mean "else if"? If not, start a new line for "if".')
4687
4688 # Check for potential format string bugs like printf(foo).
4689 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4690 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.com02af6282014-06-04 18:53:25 +00004691 # TODO(unknown): Catch the following case. Need to change the calling
erg@google.com8a95ecc2011-09-08 00:45:54 +00004692 # convention of the whole function to process multiple line to handle it.
4693 # printf(
4694 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4695 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4696 if printf_args:
4697 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00004698 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00004699 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4700 line, re.I).group(1)
4701 error(filename, linenum, 'runtime/printf', 4,
4702 'Potential format string bug. Do %s("%%s", %s) instead.'
4703 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004704
4705 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4706 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4707 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4708 error(filename, linenum, 'runtime/memset', 4,
4709 'Did you mean "memset(%s, 0, %s)"?'
4710 % (match.group(1), match.group(2)))
4711
4712 if Search(r'\busing namespace\b', line):
4713 error(filename, linenum, 'build/namespaces', 5,
4714 'Do not use namespace using-directives. '
4715 'Use using-declarations instead.')
4716
4717 # Detect variable-length arrays.
4718 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4719 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4720 match.group(3).find(']') == -1):
4721 # Split the size using space and arithmetic operators as delimiters.
4722 # If any of the resulting tokens are not compile time constants then
4723 # report the error.
4724 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4725 is_const = True
4726 skip_next = False
4727 for tok in tokens:
4728 if skip_next:
4729 skip_next = False
4730 continue
4731
4732 if Search(r'sizeof\(.+\)', tok): continue
4733 if Search(r'arraysize\(\w+\)', tok): continue
4734
4735 tok = tok.lstrip('(')
4736 tok = tok.rstrip(')')
4737 if not tok: continue
4738 if Match(r'\d+', tok): continue
4739 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4740 if Match(r'k[A-Z0-9]\w*', tok): continue
4741 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4742 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4743 # A catch all for tricky sizeof cases, including 'sizeof expression',
4744 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00004745 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004746 if tok.startswith('sizeof'):
4747 skip_next = True
4748 continue
4749 is_const = False
4750 break
4751 if not is_const:
4752 error(filename, linenum, 'runtime/arrays', 1,
4753 'Do not use variable-length arrays. Use an appropriately named '
4754 "('k' followed by CamelCase) compile-time constant for the size.")
4755
erg@google.com4e00b9a2009-01-12 23:05:11 +00004756 # Check for use of unnamed namespaces in header files. Registration
4757 # macros are typically OK, so we allow use of "namespace {" on lines
4758 # that end with backslashes.
LukeCz7197a242016-09-24 13:27:35 -05004759 if (IsHeaderExtension(file_extension)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004760 and Search(r'\bnamespace\s*{', line)
4761 and line[-1] != '\\'):
4762 error(filename, linenum, 'build/namespaces', 4,
4763 'Do not use unnamed namespaces in header files. See '
Ackermann Yuriy79692902016-04-01 21:41:34 +13004764 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
erg@google.com4e00b9a2009-01-12 23:05:11 +00004765 ' for more information.')
4766
avakulenko@google.com02af6282014-06-04 18:53:25 +00004767
4768def CheckGlobalStatic(filename, clean_lines, linenum, error):
4769 """Check for unsafe global or static objects.
4770
4771 Args:
4772 filename: The name of the current file.
4773 clean_lines: A CleansedLines instance containing the file.
4774 linenum: The number of the line to check.
4775 error: The function to call with any errors found.
4776 """
4777 line = clean_lines.elided[linenum]
4778
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004779 # Match two lines at a time to support multiline declarations
4780 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4781 line += clean_lines.elided[linenum + 1].strip()
4782
avakulenko@google.com02af6282014-06-04 18:53:25 +00004783 # Check for people declaring static/global STL strings at the top level.
4784 # This is dangerous because the C++ language does not guarantee that
Alex Vakulenko01e47232016-05-06 13:54:15 -07004785 # globals with constructors are initialized before the first access, and
4786 # also because globals can be destroyed when some threads are still running.
4787 # TODO(unknown): Generalize this to also find static unique_ptr instances.
4788 # TODO(unknown): File bugs for clang-tidy to find these.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004789 match = Match(
Alex Vakulenko01e47232016-05-06 13:54:15 -07004790 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +'
4791 r'([a-zA-Z0-9_:]+)\b(.*)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00004792 line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004793
avakulenko@google.com02af6282014-06-04 18:53:25 +00004794 # Remove false positives:
4795 # - String pointers (as opposed to values).
4796 # string *pointer
4797 # const string *pointer
4798 # string const *pointer
4799 # string *const pointer
4800 #
4801 # - Functions and template specializations.
4802 # string Function<Type>(...
4803 # string Class<Type>::Method(...
4804 #
4805 # - Operators. These are matched separately because operator names
4806 # cross non-word boundaries, and trying to match both operators
4807 # and functions at the same time would decrease accuracy of
4808 # matching identifiers.
4809 # string Class::operator*()
4810 if (match and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004811 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00004812 not Search(r'\boperator\W', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004813 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))):
4814 if Search(r'\bconst\b', line):
4815 error(filename, linenum, 'runtime/string', 4,
4816 'For a static/global string constant, use a C style string '
4817 'instead: "%schar%s %s[]".' %
4818 (match.group(1), match.group(2) or '', match.group(3)))
4819 else:
4820 error(filename, linenum, 'runtime/string', 4,
4821 'Static/global string variables are not permitted.')
avakulenko@google.com02af6282014-06-04 18:53:25 +00004822
Alex Vakulenko01e47232016-05-06 13:54:15 -07004823 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or
4824 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004825 error(filename, linenum, 'runtime/init', 4,
4826 'You seem to be initializing a member variable with itself.')
4827
4828
4829def CheckPrintf(filename, clean_lines, linenum, error):
4830 """Check for printf related issues.
4831
4832 Args:
4833 filename: The name of the current file.
4834 clean_lines: A CleansedLines instance containing the file.
4835 linenum: The number of the line to check.
4836 error: The function to call with any errors found.
4837 """
4838 line = clean_lines.elided[linenum]
4839
4840 # When snprintf is used, the second argument shouldn't be a literal.
4841 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4842 if match and match.group(2) != '0':
4843 # If 2nd arg is zero, snprintf is used to calculate size.
4844 error(filename, linenum, 'runtime/printf', 3,
4845 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4846 'to snprintf.' % (match.group(1), match.group(2)))
4847
4848 # Check if some verboten C functions are being used.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004849 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004850 error(filename, linenum, 'runtime/printf', 5,
4851 'Never use sprintf. Use snprintf instead.')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004852 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004853 if match:
4854 error(filename, linenum, 'runtime/printf', 4,
4855 'Almost always, snprintf is better than %s' % match.group(1))
4856
4857
4858def IsDerivedFunction(clean_lines, linenum):
4859 """Check if current line contains an inherited function.
4860
4861 Args:
4862 clean_lines: A CleansedLines instance containing the file.
4863 linenum: The number of the line to check.
4864 Returns:
4865 True if current line contains a function with "override"
4866 virt-specifier.
4867 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004868 # Scan back a few lines for start of current function
4869 for i in xrange(linenum, max(-1, linenum - 10), -1):
4870 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4871 if match:
4872 # Look for "override" after the matching closing parenthesis
4873 line, _, closing_paren = CloseExpression(
4874 clean_lines, i, len(match.group(1)))
4875 return (closing_paren >= 0 and
4876 Search(r'\boverride\b', line[closing_paren:]))
4877 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00004878
4879
avakulenko@google.com554223d2014-12-04 22:00:20 +00004880def IsOutOfLineMethodDefinition(clean_lines, linenum):
4881 """Check if current line contains an out-of-line method definition.
4882
4883 Args:
4884 clean_lines: A CleansedLines instance containing the file.
4885 linenum: The number of the line to check.
4886 Returns:
4887 True if current line contains an out-of-line method definition.
4888 """
4889 # Scan back a few lines for start of current function
4890 for i in xrange(linenum, max(-1, linenum - 10), -1):
4891 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
4892 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
4893 return False
4894
4895
avakulenko@google.com02af6282014-06-04 18:53:25 +00004896def IsInitializerList(clean_lines, linenum):
4897 """Check if current line is inside constructor initializer list.
4898
4899 Args:
4900 clean_lines: A CleansedLines instance containing the file.
4901 linenum: The number of the line to check.
4902 Returns:
4903 True if current line appears to be inside constructor initializer
4904 list, False otherwise.
4905 """
4906 for i in xrange(linenum, 1, -1):
4907 line = clean_lines.elided[i]
4908 if i == linenum:
4909 remove_function_body = Match(r'^(.*)\{\s*$', line)
4910 if remove_function_body:
4911 line = remove_function_body.group(1)
4912
4913 if Search(r'\s:\s*\w+[({]', line):
4914 # A lone colon tend to indicate the start of a constructor
4915 # initializer list. It could also be a ternary operator, which
4916 # also tend to appear in constructor initializer lists as
4917 # opposed to parameter lists.
4918 return True
4919 if Search(r'\}\s*,\s*$', line):
4920 # A closing brace followed by a comma is probably the end of a
4921 # brace-initialized member in constructor initializer list.
4922 return True
4923 if Search(r'[{};]\s*$', line):
4924 # Found one of the following:
4925 # - A closing brace or semicolon, probably the end of the previous
4926 # function.
4927 # - An opening brace, probably the start of current class or namespace.
4928 #
4929 # Current line is probably not inside an initializer list since
4930 # we saw one of those things without seeing the starting colon.
4931 return False
4932
4933 # Got to the beginning of the file without seeing the start of
4934 # constructor initializer list.
4935 return False
4936
4937
erg@google.comc6671232013-10-25 21:44:03 +00004938def CheckForNonConstReference(filename, clean_lines, linenum,
4939 nesting_state, error):
4940 """Check for non-const references.
4941
4942 Separate from CheckLanguage since it scans backwards from current
4943 line, instead of scanning forward.
4944
4945 Args:
4946 filename: The name of the current file.
4947 clean_lines: A CleansedLines instance containing the file.
4948 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004949 nesting_state: A NestingState instance which maintains information about
erg@google.comc6671232013-10-25 21:44:03 +00004950 the current stack of nested blocks being parsed.
4951 error: The function to call with any errors found.
4952 """
4953 # Do nothing if there is no '&' on current line.
4954 line = clean_lines.elided[linenum]
4955 if '&' not in line:
4956 return
4957
avakulenko@google.com02af6282014-06-04 18:53:25 +00004958 # If a function is inherited, current function doesn't have much of
4959 # a choice, so any non-const references should not be blamed on
4960 # derived function.
4961 if IsDerivedFunction(clean_lines, linenum):
4962 return
4963
avakulenko@google.com554223d2014-12-04 22:00:20 +00004964 # Don't warn on out-of-line method definitions, as we would warn on the
4965 # in-line declaration, if it isn't marked with 'override'.
4966 if IsOutOfLineMethodDefinition(clean_lines, linenum):
4967 return
4968
erg@google.com2aa59982013-10-28 19:09:25 +00004969 # Long type names may be broken across multiple lines, usually in one
4970 # of these forms:
4971 # LongType
4972 # ::LongTypeContinued &identifier
4973 # LongType::
4974 # LongTypeContinued &identifier
4975 # LongType<
4976 # ...>::LongTypeContinued &identifier
4977 #
4978 # If we detected a type split across two lines, join the previous
4979 # line to current line so that we can match const references
4980 # accordingly.
erg@google.comc6671232013-10-25 21:44:03 +00004981 #
4982 # Note that this only scans back one line, since scanning back
4983 # arbitrary number of lines would be expensive. If you have a type
4984 # that spans more than 2 lines, please use a typedef.
4985 if linenum > 1:
4986 previous = None
erg@google.com2aa59982013-10-28 19:09:25 +00004987 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004988 # previous_line\n + ::current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004989 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004990 clean_lines.elided[linenum - 1])
erg@google.com2aa59982013-10-28 19:09:25 +00004991 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004992 # previous_line::\n + current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004993 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004994 clean_lines.elided[linenum - 1])
4995 if previous:
4996 line = previous.group(1) + line.lstrip()
erg@google.com2aa59982013-10-28 19:09:25 +00004997 else:
4998 # Check for templated parameter that is split across multiple lines
4999 endpos = line.rfind('>')
5000 if endpos > -1:
5001 (_, startline, startpos) = ReverseCloseExpression(
5002 clean_lines, linenum, endpos)
5003 if startpos > -1 and startline < linenum:
5004 # Found the matching < on an earlier line, collect all
5005 # pieces up to current line.
5006 line = ''
5007 for i in xrange(startline, linenum + 1):
5008 line += clean_lines.elided[i].strip()
erg@google.comc6671232013-10-25 21:44:03 +00005009
5010 # Check for non-const references in function parameters. A single '&' may
5011 # found in the following places:
5012 # inside expression: binary & for bitwise AND
5013 # inside expression: unary & for taking the address of something
5014 # inside declarators: reference parameter
5015 # We will exclude the first two cases by checking that we are not inside a
5016 # function body, including one that was just introduced by a trailing '{'.
erg@google.comc6671232013-10-25 21:44:03 +00005017 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.com02af6282014-06-04 18:53:25 +00005018 if (nesting_state.previous_stack_top and
5019 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
5020 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
5021 # Not at toplevel, not within a class, and not within a namespace
5022 return
5023
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005024 # Avoid initializer lists. We only need to scan back from the
5025 # current line for something that starts with ':'.
5026 #
5027 # We don't need to check the current line, since the '&' would
5028 # appear inside the second set of parentheses on the current line as
5029 # opposed to the first set.
5030 if linenum > 0:
5031 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5032 previous_line = clean_lines.elided[i]
5033 if not Search(r'[),]\s*$', previous_line):
5034 break
5035 if Match(r'^\s*:\s+\S', previous_line):
5036 return
5037
avakulenko@google.com02af6282014-06-04 18:53:25 +00005038 # Avoid preprocessors
5039 if Search(r'\\\s*$', line):
5040 return
5041
5042 # Avoid constructor initializer lists
5043 if IsInitializerList(clean_lines, linenum):
5044 return
5045
erg@google.comc6671232013-10-25 21:44:03 +00005046 # We allow non-const references in a few standard places, like functions
5047 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5048 # those function parameters.
5049 #
5050 # We also accept & in static_assert, which looks like a function but
5051 # it's actually a declaration expression.
5052 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5053 r'operator\s*[<>][<>]|'
5054 r'static_assert|COMPILE_ASSERT'
5055 r')\s*\(')
5056 if Search(whitelisted_functions, line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005057 return
erg@google.comc6671232013-10-25 21:44:03 +00005058 elif not Search(r'\S+\([^)]*$', line):
5059 # Don't see a whitelisted function on this line. Actually we
5060 # didn't see any function name on this line, so this is likely a
5061 # multi-line parameter list. Try a bit harder to catch this case.
5062 for i in xrange(2):
5063 if (linenum > i and
5064 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005065 return
erg@google.comc6671232013-10-25 21:44:03 +00005066
avakulenko@google.com02af6282014-06-04 18:53:25 +00005067 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5068 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005069 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and
5070 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005071 error(filename, linenum, 'runtime/references', 2,
5072 'Is this a non-const reference? '
5073 'If so, make const or use a pointer: ' +
5074 ReplaceAll(' *<', '<', parameter))
5075
5076
5077def CheckCasts(filename, clean_lines, linenum, error):
5078 """Various cast related checks.
5079
5080 Args:
5081 filename: The name of the current file.
5082 clean_lines: A CleansedLines instance containing the file.
5083 linenum: The number of the line to check.
5084 error: The function to call with any errors found.
5085 """
5086 line = clean_lines.elided[linenum]
5087
5088 # Check to see if they're using an conversion function cast.
5089 # I just try to capture the most common basic types, though there are more.
5090 # Parameterless conversion functions, such as bool(), are allowed as they are
5091 # probably a member operator declaration or default constructor.
5092 match = Search(
Alex Vakulenko01e47232016-05-06 13:54:15 -07005093 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
avakulenko@google.com02af6282014-06-04 18:53:25 +00005094 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5095 r'(\([^)].*)', line)
5096 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5097 if match and not expecting_function:
5098 matched_type = match.group(2)
5099
5100 # matched_new_or_template is used to silence two false positives:
5101 # - New operators
5102 # - Template arguments with function types
5103 #
5104 # For template arguments, we match on types immediately following
5105 # an opening bracket without any spaces. This is a fast way to
5106 # silence the common case where the function type is the first
5107 # template argument. False negative with less-than comparison is
5108 # avoided because those operators are usually followed by a space.
5109 #
5110 # function<double(double)> // bracket + no space = false positive
5111 # value < double(42) // bracket + space = true positive
5112 matched_new_or_template = match.group(1)
5113
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005114 # Avoid arrays by looking for brackets that come after the closing
5115 # parenthesis.
5116 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5117 return
5118
avakulenko@google.com02af6282014-06-04 18:53:25 +00005119 # Other things to ignore:
5120 # - Function pointers
5121 # - Casts to pointer types
5122 # - Placement new
5123 # - Alias declarations
5124 matched_funcptr = match.group(3)
5125 if (matched_new_or_template is None and
5126 not (matched_funcptr and
5127 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5128 matched_funcptr) or
5129 matched_funcptr.startswith('(*)'))) and
5130 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5131 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5132 error(filename, linenum, 'readability/casting', 4,
5133 'Using deprecated casting style. '
5134 'Use static_cast<%s>(...) instead' %
5135 matched_type)
5136
5137 if not expecting_function:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005138 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005139 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5140
5141 # This doesn't catch all cases. Consider (const char * const)"hello".
5142 #
5143 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5144 # compile).
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005145 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5146 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005147 pass
5148 else:
5149 # Check pointer casts for other than string constants
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005150 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5151 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005152
5153 # In addition, we look for people taking the address of a cast. This
5154 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5155 # point where you think.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005156 #
5157 # Some non-identifier character is required before the '&' for the
5158 # expression to be recognized as a cast. These are casts:
5159 # expression = &static_cast<int*>(temporary());
5160 # function(&(int*)(temporary()));
5161 #
5162 # This is not a cast:
5163 # reference_type&(int* function_param);
avakulenko@google.com02af6282014-06-04 18:53:25 +00005164 match = Search(
avakulenko@google.com554223d2014-12-04 22:00:20 +00005165 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005166 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005167 if match:
avakulenko@google.com02af6282014-06-04 18:53:25 +00005168 # Try a better error message when the & is bound to something
5169 # dereferenced by the casted pointer, as opposed to the casted
5170 # pointer itself.
5171 parenthesis_error = False
5172 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5173 if match:
5174 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5175 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5176 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5177 if x2 >= 0:
5178 extended_line = clean_lines.elided[y2][x2:]
5179 if y2 < clean_lines.NumLines() - 1:
5180 extended_line += clean_lines.elided[y2 + 1]
5181 if Match(r'\s*(?:->|\[)', extended_line):
5182 parenthesis_error = True
5183
5184 if parenthesis_error:
5185 error(filename, linenum, 'readability/casting', 4,
5186 ('Are you taking an address of something dereferenced '
5187 'from a cast? Wrapping the dereferenced expression in '
5188 'parentheses will make the binding more obvious'))
5189 else:
5190 error(filename, linenum, 'runtime/casting', 4,
5191 ('Are you taking an address of a cast? '
5192 'This is dangerous: could be a temp var. '
5193 'Take the address before doing the cast, rather than after'))
erg@google.comc6671232013-10-25 21:44:03 +00005194
erg@google.com4e00b9a2009-01-12 23:05:11 +00005195
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005196def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005197 """Checks for a C-style cast by looking for the pattern.
5198
erg@google.com4e00b9a2009-01-12 23:05:11 +00005199 Args:
5200 filename: The name of the current file.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005201 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005202 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005203 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00005204 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005205 pattern: The regular expression used to find C-style casts.
5206 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005207
5208 Returns:
5209 True if an error was emitted.
5210 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005211 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005212 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00005213 match = Search(pattern, line)
5214 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00005215 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005216
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005217 # Exclude lines with keywords that tend to look like casts
5218 context = line[0:match.start(1) - 1]
5219 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5220 return False
5221
5222 # Try expanding current context to see if we one level of
5223 # parentheses inside a macro.
5224 if linenum > 0:
5225 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5226 context = clean_lines.elided[i] + context
5227 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
erg@google.comfd5da632013-10-25 17:39:45 +00005228 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005229
erg@google.comd350fe52013-01-14 17:51:48 +00005230 # operator++(int) and operator--(int)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005231 if context.endswith(' operator++') or context.endswith(' operator--'):
erg@google.comd350fe52013-01-14 17:51:48 +00005232 return False
5233
Alex Vakulenko01e47232016-05-06 13:54:15 -07005234 # A single unnamed argument for a function tends to look like old style cast.
5235 # If we see those, don't issue warnings for deprecated casts.
erg@google.comc6671232013-10-25 21:44:03 +00005236 remainder = line[match.end(0):]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005237 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005238 remainder):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005239 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005240
5241 # At this point, all that should be left is actual casts.
5242 error(filename, linenum, 'readability/casting', 4,
5243 'Using C-style cast. Use %s<%s>(...) instead' %
5244 (cast_type, match.group(1)))
5245
erg@google.com8a95ecc2011-09-08 00:45:54 +00005246 return True
5247
erg@google.com4e00b9a2009-01-12 23:05:11 +00005248
avakulenko@google.com02af6282014-06-04 18:53:25 +00005249def ExpectingFunctionArgs(clean_lines, linenum):
5250 """Checks whether where function type arguments are expected.
5251
5252 Args:
5253 clean_lines: A CleansedLines instance containing the file.
5254 linenum: The number of the line to check.
5255
5256 Returns:
5257 True if the line at 'linenum' is inside something that expects arguments
5258 of function types.
5259 """
5260 line = clean_lines.elided[linenum]
5261 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5262 (linenum >= 2 and
5263 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5264 clean_lines.elided[linenum - 1]) or
5265 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5266 clean_lines.elided[linenum - 2]) or
5267 Search(r'\bstd::m?function\s*\<\s*$',
5268 clean_lines.elided[linenum - 1]))))
5269
5270
erg@google.com4e00b9a2009-01-12 23:05:11 +00005271_HEADERS_CONTAINING_TEMPLATES = (
5272 ('<deque>', ('deque',)),
5273 ('<functional>', ('unary_function', 'binary_function',
5274 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5275 'negate',
5276 'equal_to', 'not_equal_to', 'greater', 'less',
5277 'greater_equal', 'less_equal',
5278 'logical_and', 'logical_or', 'logical_not',
5279 'unary_negate', 'not1', 'binary_negate', 'not2',
5280 'bind1st', 'bind2nd',
5281 'pointer_to_unary_function',
5282 'pointer_to_binary_function',
5283 'ptr_fun',
5284 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5285 'mem_fun_ref_t',
5286 'const_mem_fun_t', 'const_mem_fun1_t',
5287 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5288 'mem_fun_ref',
5289 )),
5290 ('<limits>', ('numeric_limits',)),
5291 ('<list>', ('list',)),
5292 ('<map>', ('map', 'multimap',)),
lhchavez2890dff2016-07-11 19:37:29 -07005293 ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr',
5294 'unique_ptr', 'weak_ptr')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005295 ('<queue>', ('queue', 'priority_queue',)),
5296 ('<set>', ('set', 'multiset',)),
5297 ('<stack>', ('stack',)),
5298 ('<string>', ('char_traits', 'basic_string',)),
avakulenko@google.com554223d2014-12-04 22:00:20 +00005299 ('<tuple>', ('tuple',)),
lhchavez2890dff2016-07-11 19:37:29 -07005300 ('<unordered_map>', ('unordered_map', 'unordered_multimap')),
5301 ('<unordered_set>', ('unordered_set', 'unordered_multiset')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005302 ('<utility>', ('pair',)),
5303 ('<vector>', ('vector',)),
5304
5305 # gcc extensions.
5306 # Note: std::hash is their hash, ::hash is our hash
5307 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5308 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5309 ('<slist>', ('slist',)),
5310 )
5311
Alex Vakulenko01e47232016-05-06 13:54:15 -07005312_HEADERS_MAYBE_TEMPLATES = (
5313 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort',
5314 'transform',
5315 )),
lhchavez2890dff2016-07-11 19:37:29 -07005316 ('<utility>', ('forward', 'make_pair', 'move', 'swap')),
Alex Vakulenko01e47232016-05-06 13:54:15 -07005317 )
5318
erg@google.com4e00b9a2009-01-12 23:05:11 +00005319_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5320
Alex Vakulenko01e47232016-05-06 13:54:15 -07005321_re_pattern_headers_maybe_templates = []
5322for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
5323 for _template in _templates:
5324 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5325 # type::max().
5326 _re_pattern_headers_maybe_templates.append(
5327 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5328 _template,
5329 _header))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005330
Alex Vakulenko01e47232016-05-06 13:54:15 -07005331# Other scripts may reach in and modify this pattern.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005332_re_pattern_templates = []
5333for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5334 for _template in _templates:
5335 _re_pattern_templates.append(
5336 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5337 _template + '<>',
5338 _header))
5339
5340
erg@google.come35f7652009-06-19 20:52:09 +00005341def FilesBelongToSameModule(filename_cc, filename_h):
5342 """Check if these two filenames belong to the same module.
5343
5344 The concept of a 'module' here is a as follows:
5345 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5346 same 'module' if they are in the same directory.
5347 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5348 to belong to the same module here.
5349
5350 If the filename_cc contains a longer path than the filename_h, for example,
5351 '/absolute/path/to/base/sysinfo.cc', and this file would include
5352 'base/sysinfo.h', this function also produces the prefix needed to open the
5353 header. This is used by the caller of this function to more robustly open the
5354 header file. We don't have access to the real include paths in this context,
5355 so we need this guesswork here.
5356
5357 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5358 according to this implementation. Because of this, this function gives
5359 some false positives. This should be sufficiently rare in practice.
5360
5361 Args:
5362 filename_cc: is the path for the .cc file
5363 filename_h: is the path for the header path
5364
5365 Returns:
5366 Tuple with a bool and a string:
5367 bool: True if filename_cc and filename_h belong to the same module.
5368 string: the additional prefix needed to open the header file.
5369 """
5370
Alex Vakulenko01e47232016-05-06 13:54:15 -07005371 fileinfo = FileInfo(filename_cc)
5372 if not fileinfo.IsSource():
erg@google.come35f7652009-06-19 20:52:09 +00005373 return (False, '')
Alex Vakulenko01e47232016-05-06 13:54:15 -07005374 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5375 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5376 if matched_test_suffix:
5377 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
erg@google.come35f7652009-06-19 20:52:09 +00005378 filename_cc = filename_cc.replace('/public/', '/')
5379 filename_cc = filename_cc.replace('/internal/', '/')
5380
5381 if not filename_h.endswith('.h'):
5382 return (False, '')
5383 filename_h = filename_h[:-len('.h')]
5384 if filename_h.endswith('-inl'):
5385 filename_h = filename_h[:-len('-inl')]
5386 filename_h = filename_h.replace('/public/', '/')
5387 filename_h = filename_h.replace('/internal/', '/')
5388
5389 files_belong_to_same_module = filename_cc.endswith(filename_h)
5390 common_path = ''
5391 if files_belong_to_same_module:
5392 common_path = filename_cc[:-len(filename_h)]
5393 return files_belong_to_same_module, common_path
5394
5395
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005396def UpdateIncludeState(filename, include_dict, io=codecs):
5397 """Fill up the include_dict with new includes found from the file.
erg@google.come35f7652009-06-19 20:52:09 +00005398
5399 Args:
5400 filename: the name of the header to read.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005401 include_dict: a dictionary in which the headers are inserted.
erg@google.come35f7652009-06-19 20:52:09 +00005402 io: The io factory to use to read the file. Provided for testability.
5403
5404 Returns:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005405 True if a header was successfully added. False otherwise.
erg@google.come35f7652009-06-19 20:52:09 +00005406 """
5407 headerfile = None
5408 try:
5409 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5410 except IOError:
5411 return False
5412 linenum = 0
5413 for line in headerfile:
5414 linenum += 1
5415 clean_line = CleanseComments(line)
5416 match = _RE_PATTERN_INCLUDE.search(clean_line)
5417 if match:
5418 include = match.group(2)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005419 include_dict.setdefault(include, linenum)
erg@google.come35f7652009-06-19 20:52:09 +00005420 return True
5421
5422
5423def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5424 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005425 """Reports for missing stl includes.
5426
5427 This function will output warnings to make sure you are including the headers
5428 necessary for the stl containers and functions that you use. We only give one
5429 reason to include a header. For example, if you use both equal_to<> and
5430 less<> in a .h file, only one (the latter in the file) of these will be
5431 reported as a reason to include the <functional>.
5432
erg@google.com4e00b9a2009-01-12 23:05:11 +00005433 Args:
5434 filename: The name of the current file.
5435 clean_lines: A CleansedLines instance containing the file.
5436 include_state: An _IncludeState instance.
5437 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00005438 io: The IO factory to use to read the header file. Provided for unittest
5439 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005440 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00005441 required = {} # A map of header name to linenumber and the template entity.
5442 # Example of required: { '<functional>': (1219, 'less<>') }
5443
5444 for linenum in xrange(clean_lines.NumLines()):
5445 line = clean_lines.elided[linenum]
5446 if not line or line[0] == '#':
5447 continue
5448
5449 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005450 matched = _RE_PATTERN_STRING.search(line)
5451 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00005452 # Don't warn about strings in non-STL namespaces:
5453 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005454 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00005455 if prefix.endswith('std::') or not prefix.endswith('::'):
5456 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00005457
Alex Vakulenko01e47232016-05-06 13:54:15 -07005458 for pattern, template, header in _re_pattern_headers_maybe_templates:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005459 if pattern.search(line):
5460 required[header] = (linenum, template)
5461
5462 # The following function is just a speed up, no semantics are changed.
5463 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5464 continue
5465
5466 for pattern, template, header in _re_pattern_templates:
lhchavez3ae81f12016-07-11 19:00:34 -07005467 matched = pattern.search(line)
5468 if matched:
5469 # Don't warn about IWYU in non-STL namespaces:
5470 # (We check only the first match per line; good enough.)
5471 prefix = line[:matched.start()]
5472 if prefix.endswith('std::') or not prefix.endswith('::'):
5473 required[header] = (linenum, template)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005474
erg@google.come35f7652009-06-19 20:52:09 +00005475 # The policy is that if you #include something in foo.h you don't need to
5476 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005477 # Let's flatten the include_state include_list and copy it into a dictionary.
5478 include_dict = dict([item for sublist in include_state.include_list
5479 for item in sublist])
erg@google.come35f7652009-06-19 20:52:09 +00005480
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005481 # Did we find the header for this file (if any) and successfully load it?
erg@google.come35f7652009-06-19 20:52:09 +00005482 header_found = False
5483
5484 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00005485 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00005486
5487 # For Emacs's flymake.
5488 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5489 # by flymake and that file name might end with '_flymake.cc'. In that case,
5490 # restore original file name here so that the corresponding header file can be
5491 # found.
5492 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5493 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00005494 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00005495
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005496 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.come35f7652009-06-19 20:52:09 +00005497 # the keys.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005498 header_keys = include_dict.keys()
erg@google.com8a95ecc2011-09-08 00:45:54 +00005499 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00005500 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5501 fullpath = common_path + header
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005502 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.come35f7652009-06-19 20:52:09 +00005503 header_found = True
5504
5505 # If we can't find the header file for a .cc, assume it's because we don't
5506 # know where to look. In that case we'll give up as we're not sure they
5507 # didn't include it in the .h file.
5508 # TODO(unknown): Do a better job of finding .h files so we are confident that
5509 # not having the .h file means there isn't one.
5510 if filename.endswith('.cc') and not header_found:
5511 return
5512
erg@google.com4e00b9a2009-01-12 23:05:11 +00005513 # All the lines have been processed, report the errors found.
5514 for required_header_unstripped in required:
5515 template = required[required_header_unstripped][1]
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005516 if required_header_unstripped.strip('<>"') not in include_dict:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005517 error(filename, required[required_header_unstripped][0],
5518 'build/include_what_you_use', 4,
5519 'Add #include ' + required_header_unstripped + ' for ' + template)
5520
5521
erg@google.com8a95ecc2011-09-08 00:45:54 +00005522_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5523
5524
5525def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5526 """Check that make_pair's template arguments are deduced.
5527
avakulenko@google.com02af6282014-06-04 18:53:25 +00005528 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
erg@google.com8a95ecc2011-09-08 00:45:54 +00005529 specified explicitly, and such use isn't intended in any case.
5530
5531 Args:
5532 filename: The name of the current file.
5533 clean_lines: A CleansedLines instance containing the file.
5534 linenum: The number of the line to check.
5535 error: The function to call with any errors found.
5536 """
erg@google.com2aa59982013-10-28 19:09:25 +00005537 line = clean_lines.elided[linenum]
erg@google.com8a95ecc2011-09-08 00:45:54 +00005538 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5539 if match:
5540 error(filename, linenum, 'build/explicit_make_pair',
5541 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00005542 'For C++11-compatibility, omit template arguments from make_pair'
5543 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005544
5545
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005546def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5547 """Check if line contains a redundant "virtual" function-specifier.
5548
5549 Args:
5550 filename: The name of the current file.
5551 clean_lines: A CleansedLines instance containing the file.
5552 linenum: The number of the line to check.
5553 error: The function to call with any errors found.
5554 """
5555 # Look for "virtual" on current line.
5556 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005557 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005558 if not virtual: return
5559
avakulenko@google.com554223d2014-12-04 22:00:20 +00005560 # Ignore "virtual" keywords that are near access-specifiers. These
5561 # are only used in class base-specifier and do not apply to member
5562 # functions.
5563 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or
5564 Match(r'^\s+(public|protected|private)\b', virtual.group(3))):
5565 return
5566
5567 # Ignore the "virtual" keyword from virtual base classes. Usually
5568 # there is a column on the same line in these cases (virtual base
5569 # classes are rare in google3 because multiple inheritance is rare).
5570 if Match(r'^.*[^:]:[^:].*$', line): return
5571
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005572 # Look for the next opening parenthesis. This is the start of the
5573 # parameter list (possibly on the next line shortly after virtual).
5574 # TODO(unknown): doesn't work if there are virtual functions with
5575 # decltype() or other things that use parentheses, but csearch suggests
5576 # that this is rare.
5577 end_col = -1
5578 end_line = -1
avakulenko@google.com554223d2014-12-04 22:00:20 +00005579 start_col = len(virtual.group(2))
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005580 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5581 line = clean_lines.elided[start_line][start_col:]
5582 parameter_list = Match(r'^([^(]*)\(', line)
5583 if parameter_list:
5584 # Match parentheses to find the end of the parameter list
5585 (_, end_line, end_col) = CloseExpression(
5586 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5587 break
5588 start_col = 0
5589
5590 if end_col < 0:
5591 return # Couldn't find end of parameter list, give up
5592
5593 # Look for "override" or "final" after the parameter list
5594 # (possibly on the next few lines).
5595 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5596 line = clean_lines.elided[i][end_col:]
5597 match = Search(r'\b(override|final)\b', line)
5598 if match:
5599 error(filename, linenum, 'readability/inheritance', 4,
5600 ('"virtual" is redundant since function is '
5601 'already declared as "%s"' % match.group(1)))
5602
5603 # Set end_col to check whole lines after we are done with the
5604 # first line.
5605 end_col = 0
5606 if Search(r'[^\w]\s*$', line):
5607 break
5608
5609
5610def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5611 """Check if line contains a redundant "override" or "final" virt-specifier.
5612
5613 Args:
5614 filename: The name of the current file.
5615 clean_lines: A CleansedLines instance containing the file.
5616 linenum: The number of the line to check.
5617 error: The function to call with any errors found.
5618 """
avakulenko@google.com554223d2014-12-04 22:00:20 +00005619 # Look for closing parenthesis nearby. We need one to confirm where
5620 # the declarator ends and where the virt-specifier starts to avoid
5621 # false positives.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005622 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005623 declarator_end = line.rfind(')')
5624 if declarator_end >= 0:
5625 fragment = line[declarator_end:]
5626 else:
5627 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0:
5628 fragment = line
5629 else:
5630 return
5631
5632 # Check that at most one of "override" or "final" is present, not both
5633 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005634 error(filename, linenum, 'readability/inheritance', 4,
5635 ('"override" is redundant since function is '
5636 'already declared as "final"'))
5637
5638
5639
5640
5641# Returns true if we are at a new block, and it is directly
5642# inside of a namespace.
5643def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5644 """Checks that the new block is directly in a namespace.
5645
5646 Args:
5647 nesting_state: The _NestingState object that contains info about our state.
5648 is_forward_declaration: If the class is a forward declared class.
5649 Returns:
5650 Whether or not the new block is directly in a namespace.
5651 """
5652 if is_forward_declaration:
5653 if len(nesting_state.stack) >= 1 and (
5654 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5655 return True
5656 else:
5657 return False
5658
5659 return (len(nesting_state.stack) > 1 and
5660 nesting_state.stack[-1].check_namespace_indentation and
5661 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5662
5663
5664def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5665 raw_lines_no_comments, linenum):
5666 """This method determines if we should apply our namespace indentation check.
5667
5668 Args:
5669 nesting_state: The current nesting state.
5670 is_namespace_indent_item: If we just put a new class on the stack, True.
5671 If the top of the stack is not a class, or we did not recently
5672 add the class, False.
5673 raw_lines_no_comments: The lines without the comments.
5674 linenum: The current line number we are processing.
5675
5676 Returns:
5677 True if we should apply our namespace indentation check. Currently, it
5678 only works for classes and namespaces inside of a namespace.
5679 """
5680
5681 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5682 linenum)
5683
5684 if not (is_namespace_indent_item or is_forward_declaration):
5685 return False
5686
5687 # If we are in a macro, we do not want to check the namespace indentation.
5688 if IsMacroDefinition(raw_lines_no_comments, linenum):
5689 return False
5690
5691 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5692
5693
5694# Call this method if the line is directly inside of a namespace.
5695# If the line above is blank (excluding comments) or the start of
5696# an inner namespace, it cannot be indented.
5697def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5698 error):
5699 line = raw_lines_no_comments[linenum]
5700 if Match(r'^\s+', line):
5701 error(filename, linenum, 'runtime/indentation_namespace', 4,
5702 'Do not indent within a namespace')
erg@google.com8a95ecc2011-09-08 00:45:54 +00005703
5704
erg@google.comd350fe52013-01-14 17:51:48 +00005705def ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005706 include_state, function_state, nesting_state, error,
5707 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005708 """Processes a single line in the file.
5709
5710 Args:
5711 filename: Filename of the file that is being processed.
5712 file_extension: The extension (dot not included) of the file.
5713 clean_lines: An array of strings, each representing a line of the file,
5714 with comments stripped.
5715 line: Number of line being processed.
5716 include_state: An _IncludeState instance in which the headers are inserted.
5717 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005718 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00005719 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005720 error: A callable to which errors are reported, which takes 4 arguments:
5721 filename, line number, error level, and message
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005722 extra_check_functions: An array of additional check functions that will be
5723 run on each source line. Each function takes 4
5724 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005725 """
5726 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00005727 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005728 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005729 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5730 error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005731 if nesting_state.InAsmBlock(): return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005732 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005733 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005734 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005735 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
erg@google.comfd5da632013-10-25 17:39:45 +00005736 nesting_state, error)
erg@google.comc6671232013-10-25 21:44:03 +00005737 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005738 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00005739 nesting_state, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005740 CheckVlogArguments(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005741 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00005742 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005743 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005744 CheckRedundantVirtual(filename, clean_lines, line, error)
5745 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005746 for check_fn in extra_check_functions:
5747 check_fn(filename, clean_lines, line, error)
erg@google.com7430eef2014-07-28 22:33:46 +00005748
avakulenko@google.com02af6282014-06-04 18:53:25 +00005749def FlagCxx11Features(filename, clean_lines, linenum, error):
5750 """Flag those c++11 features that we only allow in certain places.
5751
5752 Args:
5753 filename: The name of the current file.
5754 clean_lines: A CleansedLines instance containing the file.
5755 linenum: The number of the line to check.
5756 error: The function to call with any errors found.
5757 """
5758 line = clean_lines.elided[linenum]
5759
avakulenko@google.com02af6282014-06-04 18:53:25 +00005760 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005761
5762 # Flag unapproved C++ TR1 headers.
5763 if include and include.group(1).startswith('tr1/'):
5764 error(filename, linenum, 'build/c++tr1', 5,
5765 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1))
5766
5767 # Flag unapproved C++11 headers.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005768 if include and include.group(1) in ('cfenv',
5769 'condition_variable',
5770 'fenv.h',
5771 'future',
5772 'mutex',
5773 'thread',
5774 'chrono',
5775 'ratio',
5776 'regex',
5777 'system_error',
5778 ):
5779 error(filename, linenum, 'build/c++11', 5,
5780 ('<%s> is an unapproved C++11 header.') % include.group(1))
5781
5782 # The only place where we need to worry about C++11 keywords and library
5783 # features in preprocessor directives is in macro definitions.
5784 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5785
5786 # These are classes and free functions. The classes are always
5787 # mentioned as std::*, but we only catch the free functions if
5788 # they're not found by ADL. They're alphabetical by header.
5789 for top_name in (
5790 # type_traits
5791 'alignment_of',
5792 'aligned_union',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005793 ):
5794 if Search(r'\bstd::%s\b' % top_name, line):
5795 error(filename, linenum, 'build/c++11', 5,
5796 ('std::%s is an unapproved C++11 class or function. Send c-style '
5797 'an example of where it would make your code more readable, and '
5798 'they may let you use it.') % top_name)
5799
5800
Alex Vakulenko01e47232016-05-06 13:54:15 -07005801def FlagCxx14Features(filename, clean_lines, linenum, error):
5802 """Flag those C++14 features that we restrict.
5803
5804 Args:
5805 filename: The name of the current file.
5806 clean_lines: A CleansedLines instance containing the file.
5807 linenum: The number of the line to check.
5808 error: The function to call with any errors found.
5809 """
5810 line = clean_lines.elided[linenum]
5811
5812 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5813
5814 # Flag unapproved C++14 headers.
5815 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):
5816 error(filename, linenum, 'build/c++14', 5,
5817 ('<%s> is an unapproved C++14 header.') % include.group(1))
5818
5819
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005820def ProcessFileData(filename, file_extension, lines, error,
5821 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005822 """Performs lint checks and reports any errors to the given error function.
5823
5824 Args:
5825 filename: Filename of the file that is being processed.
5826 file_extension: The extension (dot not included) of the file.
5827 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00005828 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005829 error: A callable to which errors are reported, which takes 4 arguments:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005830 filename, line number, error level, and message
5831 extra_check_functions: An array of additional check functions that will be
5832 run on each source line. Each function takes 4
5833 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005834 """
5835 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5836 ['// marker so line numbers end in a known way'])
5837
5838 include_state = _IncludeState()
5839 function_state = _FunctionState()
avakulenko@google.com02af6282014-06-04 18:53:25 +00005840 nesting_state = NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005841
erg+personal@google.com05189642010-04-30 20:43:03 +00005842 ResetNolintSuppressions()
5843
erg@google.com4e00b9a2009-01-12 23:05:11 +00005844 CheckForCopyright(filename, lines, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005845 ProcessGlobalSuppresions(lines)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005846 RemoveMultiLineComments(filename, lines, error)
5847 clean_lines = CleansedLines(lines)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005848
LukeCz7197a242016-09-24 13:27:35 -05005849 if IsHeaderExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005850 CheckForHeaderGuard(filename, clean_lines, error)
5851
erg@google.com4e00b9a2009-01-12 23:05:11 +00005852 for line in xrange(clean_lines.NumLines()):
5853 ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005854 include_state, function_state, nesting_state, error,
5855 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005856 FlagCxx11Features(filename, clean_lines, line, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005857 nesting_state.CheckCompletedBlocks(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005858
5859 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005860
avakulenko@google.com554223d2014-12-04 22:00:20 +00005861 # Check that the .cc file has included its header if it exists.
Alex Vakulenko01e47232016-05-06 13:54:15 -07005862 if _IsSourceExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005863 CheckHeaderFileIncluded(filename, include_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005864
5865 # We check here rather than inside ProcessLine so that we see raw
5866 # lines rather than "cleaned" lines.
erg@google.com2aa59982013-10-28 19:09:25 +00005867 CheckForBadCharacters(filename, lines, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005868
5869 CheckForNewlineAtEOF(filename, lines, error)
5870
erg@google.com7430eef2014-07-28 22:33:46 +00005871def ProcessConfigOverrides(filename):
5872 """ Loads the configuration files and processes the config overrides.
5873
5874 Args:
5875 filename: The name of the file being processed by the linter.
5876
5877 Returns:
5878 False if the current |filename| should not be processed further.
5879 """
5880
5881 abs_filename = os.path.abspath(filename)
5882 cfg_filters = []
5883 keep_looking = True
5884 while keep_looking:
5885 abs_path, base_name = os.path.split(abs_filename)
5886 if not base_name:
5887 break # Reached the root directory.
5888
5889 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5890 abs_filename = abs_path
5891 if not os.path.isfile(cfg_file):
5892 continue
5893
5894 try:
5895 with open(cfg_file) as file_handle:
5896 for line in file_handle:
5897 line, _, _ = line.partition('#') # Remove comments.
5898 if not line.strip():
5899 continue
5900
5901 name, _, val = line.partition('=')
5902 name = name.strip()
5903 val = val.strip()
5904 if name == 'set noparent':
5905 keep_looking = False
5906 elif name == 'filter':
5907 cfg_filters.append(val)
5908 elif name == 'exclude_files':
5909 # When matching exclude_files pattern, use the base_name of
5910 # the current file name or the directory name we are processing.
5911 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5912 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5913 # file's "exclude_files" filter is meant to be checked against "bar"
5914 # and not "baz" nor "bar/baz.cc".
5915 if base_name:
5916 pattern = re.compile(val)
5917 if pattern.match(base_name):
5918 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5919 'File path component "%s" matches '
5920 'pattern "%s"\n' %
5921 (filename, cfg_file, base_name, val))
5922 return False
avakulenko@google.com310681b2014-08-22 19:38:55 +00005923 elif name == 'linelength':
5924 global _line_length
5925 try:
5926 _line_length = int(val)
5927 except ValueError:
5928 sys.stderr.write('Line length must be numeric.')
Fabian Guera2322e4f2016-05-01 17:36:30 +02005929 elif name == 'root':
5930 global _root
5931 _root = val
LukeCz7197a242016-09-24 13:27:35 -05005932 elif name == 'headers':
5933 ProcessHppHeadersOption(val)
erg@google.com7430eef2014-07-28 22:33:46 +00005934 else:
5935 sys.stderr.write(
5936 'Invalid configuration option (%s) in file %s\n' %
5937 (name, cfg_file))
5938
5939 except IOError:
5940 sys.stderr.write(
5941 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5942 keep_looking = False
5943
5944 # Apply all the accumulated filters in reverse order (top-level directory
5945 # config options having the least priority).
5946 for filter in reversed(cfg_filters):
5947 _AddFilters(filter)
5948
5949 return True
5950
avakulenko@google.com02af6282014-06-04 18:53:25 +00005951
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005952def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005953 """Does google-lint on a single file.
5954
5955 Args:
5956 filename: The name of the file to parse.
5957
5958 vlevel: The level of errors to report. Every error of confidence
5959 >= verbose_level will be reported. 0 is a good default.
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005960
5961 extra_check_functions: An array of additional check functions that will be
5962 run on each source line. Each function takes 4
5963 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005964 """
5965
5966 _SetVerboseLevel(vlevel)
erg@google.com7430eef2014-07-28 22:33:46 +00005967 _BackupFilters()
5968
5969 if not ProcessConfigOverrides(filename):
5970 _RestoreFilters()
5971 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005972
avakulenko@google.com02af6282014-06-04 18:53:25 +00005973 lf_lines = []
5974 crlf_lines = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00005975 try:
5976 # Support the UNIX convention of using "-" for stdin. Note that
5977 # we are not opening the file with universal newline support
5978 # (which codecs doesn't support anyway), so the resulting lines do
5979 # contain trailing '\r' characters if we are reading a file that
5980 # has CRLF endings.
5981 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.com02af6282014-06-04 18:53:25 +00005982 # below.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005983 if filename == '-':
5984 lines = codecs.StreamReaderWriter(sys.stdin,
5985 codecs.getreader('utf8'),
5986 codecs.getwriter('utf8'),
5987 'replace').read().split('\n')
5988 else:
5989 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
5990
erg@google.com4e00b9a2009-01-12 23:05:11 +00005991 # Remove trailing '\r'.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005992 # The -1 accounts for the extra trailing blank line we get from split()
5993 for linenum in range(len(lines) - 1):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005994 if lines[linenum].endswith('\r'):
5995 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.com02af6282014-06-04 18:53:25 +00005996 crlf_lines.append(linenum + 1)
5997 else:
5998 lf_lines.append(linenum + 1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005999
6000 except IOError:
6001 sys.stderr.write(
6002 "Skipping input '%s': Can't open for reading\n" % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006003 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006004 return
6005
6006 # Note, if no dot is found, this will give the entire filename as the ext.
6007 file_extension = filename[filename.rfind('.') + 1:]
6008
6009 # When reading from stdin, the extension is unknown, so no cpplint tests
6010 # should rely on the extension.
erg@google.com19680272013-12-16 22:48:54 +00006011 if filename != '-' and file_extension not in _valid_extensions:
erg@google.com2aa59982013-10-28 19:09:25 +00006012 sys.stderr.write('Ignoring %s; not a valid file name '
erg@google.com19680272013-12-16 22:48:54 +00006013 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006014 else:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00006015 ProcessFileData(filename, file_extension, lines, Error,
6016 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00006017
6018 # If end-of-line sequences are a mix of LF and CR-LF, issue
6019 # warnings on the lines with CR.
6020 #
6021 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
6022 # since critique can handle these just fine, and the style guide
6023 # doesn't dictate a particular end of line sequence.
6024 #
6025 # We can't depend on os.linesep to determine what the desired
6026 # end-of-line sequence should be, since that will return the
6027 # server-side end-of-line sequence.
6028 if lf_lines and crlf_lines:
6029 # Warn on every line with CR. An alternative approach might be to
6030 # check whether the file is mostly CRLF or just LF, and warn on the
6031 # minority, we bias toward LF here since most tools prefer LF.
6032 for linenum in crlf_lines:
6033 Error(filename, linenum, 'whitespace/newline', 1,
6034 'Unexpected \\r (^M) found; better to use only \\n')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006035
LukeCze09f4782016-09-28 19:13:37 -05006036 sys.stdout.write('Done processing %s\n' % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006037 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006038
6039
6040def PrintUsage(message):
6041 """Prints a brief usage string and exits, optionally with an error message.
6042
6043 Args:
6044 message: The optional error message.
6045 """
6046 sys.stderr.write(_USAGE)
6047 if message:
6048 sys.exit('\nFATAL ERROR: ' + message)
6049 else:
6050 sys.exit(1)
6051
6052
6053def PrintCategories():
6054 """Prints a list of all the error-categories used by error messages.
6055
6056 These are the categories used to filter messages via --filter.
6057 """
erg+personal@google.com05189642010-04-30 20:43:03 +00006058 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006059 sys.exit(0)
6060
6061
6062def ParseArguments(args):
6063 """Parses the command line arguments.
6064
6065 This may set the output format and verbosity level as side-effects.
6066
6067 Args:
6068 args: The command line arguments:
6069
6070 Returns:
6071 The list of filenames to lint.
6072 """
6073 try:
6074 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00006075 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00006076 'filter=',
erg@google.comab53edf2013-11-05 22:23:37 +00006077 'root=',
erg@google.com19680272013-12-16 22:48:54 +00006078 'linelength=',
LukeCz7197a242016-09-24 13:27:35 -05006079 'extensions=',
6080 'headers='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00006081 except getopt.GetoptError:
6082 PrintUsage('Invalid arguments.')
6083
6084 verbosity = _VerboseLevel()
6085 output_format = _OutputFormat()
6086 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00006087 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00006088
6089 for (opt, val) in opts:
6090 if opt == '--help':
6091 PrintUsage(None)
6092 elif opt == '--output':
erg@google.comc6671232013-10-25 21:44:03 +00006093 if val not in ('emacs', 'vs7', 'eclipse'):
erg@google.com02c27fd2013-05-28 21:34:34 +00006094 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006095 output_format = val
6096 elif opt == '--verbose':
6097 verbosity = int(val)
6098 elif opt == '--filter':
6099 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00006100 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00006101 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00006102 elif opt == '--counting':
6103 if val not in ('total', 'toplevel', 'detailed'):
6104 PrintUsage('Valid counting options are total, toplevel, and detailed')
6105 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00006106 elif opt == '--root':
6107 global _root
6108 _root = val
erg@google.comab53edf2013-11-05 22:23:37 +00006109 elif opt == '--linelength':
6110 global _line_length
6111 try:
6112 _line_length = int(val)
6113 except ValueError:
6114 PrintUsage('Line length must be digits.')
erg@google.com19680272013-12-16 22:48:54 +00006115 elif opt == '--extensions':
6116 global _valid_extensions
6117 try:
6118 _valid_extensions = set(val.split(','))
6119 except ValueError:
6120 PrintUsage('Extensions must be comma seperated list.')
LukeCz7197a242016-09-24 13:27:35 -05006121 elif opt == '--headers':
6122 ProcessHppHeadersOption(val)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006123
6124 if not filenames:
6125 PrintUsage('No files were specified.')
6126
6127 _SetOutputFormat(output_format)
6128 _SetVerboseLevel(verbosity)
6129 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00006130 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006131
6132 return filenames
6133
6134
6135def main():
6136 filenames = ParseArguments(sys.argv[1:])
6137
6138 # Change stderr to write with replacement characters so we don't die
6139 # if we try to print something containing non-ASCII characters.
6140 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6141 codecs.getreader('utf8'),
6142 codecs.getwriter('utf8'),
6143 'replace')
6144
erg@google.coma868d2d2009-10-09 21:18:45 +00006145 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006146 for filename in filenames:
6147 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00006148 _cpplint_state.PrintErrorCounts()
6149
erg@google.com4e00b9a2009-01-12 23:05:11 +00006150 sys.exit(_cpplint_state.error_count > 0)
6151
6152
6153if __name__ == '__main__':
6154 main()