blob: b12d60db6ed598fc82277527a4db90a41e1c449e [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.
553_hpp_headers = None
554
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):
569 if _hpp_headers and file_extension in _hpp_headers:
570 return True
571 else:
572 return file_extension == 'h'
Alex Vakulenko01e47232016-05-06 13:54:15 -0700573
erg+personal@google.com05189642010-04-30 20:43:03 +0000574def ParseNolintSuppressions(filename, raw_line, linenum, error):
Alex Vakulenko01e47232016-05-06 13:54:15 -0700575 """Updates the global list of line error-suppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000576
577 Parses any NOLINT comments on the current line, updating the global
578 error_suppressions store. Reports an error if the NOLINT comment
579 was malformed.
580
581 Args:
582 filename: str, the name of the input file.
583 raw_line: str, the line of input text, with comments.
584 linenum: int, the number of the current line.
585 error: function, an error handler.
586 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000587 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
erg@google.com8a95ecc2011-09-08 00:45:54 +0000588 if matched:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000589 if matched.group(1):
590 suppressed_line = linenum + 1
591 else:
592 suppressed_line = linenum
593 category = matched.group(2)
erg+personal@google.com05189642010-04-30 20:43:03 +0000594 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000595 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg+personal@google.com05189642010-04-30 20:43:03 +0000596 else:
597 if category.startswith('(') and category.endswith(')'):
598 category = category[1:-1]
599 if category in _ERROR_CATEGORIES:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000600 _error_suppressions.setdefault(category, set()).add(suppressed_line)
avakulenko@google.com554223d2014-12-04 22:00:20 +0000601 elif category not in _LEGACY_ERROR_CATEGORIES:
erg+personal@google.com05189642010-04-30 20:43:03 +0000602 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000603 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000604
605
Alex Vakulenko01e47232016-05-06 13:54:15 -0700606def ProcessGlobalSuppresions(lines):
607 """Updates the list of global error suppressions.
608
609 Parses any lint directives in the file that have global effect.
610
611 Args:
612 lines: An array of strings, each representing a line of the file, with the
613 last element being empty if the file is terminated with a newline.
614 """
615 for line in lines:
616 if _SEARCH_C_FILE.search(line):
617 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
618 _global_error_suppressions[category] = True
619 if _SEARCH_KERNEL_FILE.search(line):
620 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES:
621 _global_error_suppressions[category] = True
622
623
erg+personal@google.com05189642010-04-30 20:43:03 +0000624def ResetNolintSuppressions():
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000625 """Resets the set of NOLINT suppressions to empty."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000626 _error_suppressions.clear()
Alex Vakulenko01e47232016-05-06 13:54:15 -0700627 _global_error_suppressions.clear()
erg+personal@google.com05189642010-04-30 20:43:03 +0000628
629
630def IsErrorSuppressedByNolint(category, linenum):
631 """Returns true if the specified error category is suppressed on this line.
632
633 Consults the global error_suppressions map populated by
Alex Vakulenko01e47232016-05-06 13:54:15 -0700634 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000635
636 Args:
637 category: str, the category of the error.
638 linenum: int, the current line number.
639 Returns:
Alex Vakulenko01e47232016-05-06 13:54:15 -0700640 bool, True iff the error should be suppressed due to a NOLINT comment or
641 global suppression.
erg+personal@google.com05189642010-04-30 20:43:03 +0000642 """
Alex Vakulenko01e47232016-05-06 13:54:15 -0700643 return (_global_error_suppressions.get(category, False) or
644 linenum in _error_suppressions.get(category, set()) or
erg+personal@google.com05189642010-04-30 20:43:03 +0000645 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000646
avakulenko@google.com02af6282014-06-04 18:53:25 +0000647
erg@google.com4e00b9a2009-01-12 23:05:11 +0000648def Match(pattern, s):
649 """Matches the string with the pattern, caching the compiled regexp."""
650 # The regexp compilation caching is inlined in both Match and Search for
651 # performance reasons; factoring it out into a separate function turns out
652 # to be noticeably expensive.
erg@google.comc6671232013-10-25 21:44:03 +0000653 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000654 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
655 return _regexp_compile_cache[pattern].match(s)
656
657
erg@google.comfd5da632013-10-25 17:39:45 +0000658def ReplaceAll(pattern, rep, s):
659 """Replaces instances of pattern in a string with a replacement.
660
661 The compiled regex is kept in a cache shared by Match and Search.
662
663 Args:
664 pattern: regex pattern
665 rep: replacement text
666 s: search string
667
668 Returns:
669 string with replacements made (or original string if no replacements)
670 """
671 if pattern not in _regexp_compile_cache:
672 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
673 return _regexp_compile_cache[pattern].sub(rep, s)
674
675
erg@google.com4e00b9a2009-01-12 23:05:11 +0000676def Search(pattern, s):
677 """Searches the string for the pattern, caching the compiled regexp."""
erg@google.comc6671232013-10-25 21:44:03 +0000678 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000679 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
680 return _regexp_compile_cache[pattern].search(s)
681
682
Alex Vakulenko01e47232016-05-06 13:54:15 -0700683def _IsSourceExtension(s):
684 """File extension (excluding dot) matches a source file extension."""
685 return s in ('c', 'cc', 'cpp', 'cxx')
686
687
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000688class _IncludeState(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000689 """Tracks line numbers for includes, and the order in which includes appear.
690
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000691 include_list contains list of lists of (header, line number) pairs.
692 It's a lists of lists rather than just one flat list to make it
693 easier to update across preprocessor boundaries.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000694
695 Call CheckNextIncludeOrder() once for each header in the file, passing
696 in the type constants defined above. Calls in an illegal order will
697 raise an _IncludeError with an appropriate error message.
698
699 """
700 # self._section will move monotonically through this set. If it ever
701 # needs to move backwards, CheckNextIncludeOrder will raise an error.
702 _INITIAL_SECTION = 0
703 _MY_H_SECTION = 1
704 _C_SECTION = 2
705 _CPP_SECTION = 3
706 _OTHER_H_SECTION = 4
707
708 _TYPE_NAMES = {
709 _C_SYS_HEADER: 'C system header',
710 _CPP_SYS_HEADER: 'C++ system header',
711 _LIKELY_MY_HEADER: 'header this file implements',
712 _POSSIBLE_MY_HEADER: 'header this file may implement',
713 _OTHER_HEADER: 'other header',
714 }
715 _SECTION_NAMES = {
716 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
717 _MY_H_SECTION: 'a header this file implements',
718 _C_SECTION: 'C system header',
719 _CPP_SECTION: 'C++ system header',
720 _OTHER_H_SECTION: 'other header',
721 }
722
723 def __init__(self):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000724 self.include_list = [[]]
725 self.ResetSection('')
erg@google.com2aa59982013-10-28 19:09:25 +0000726
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000727 def FindHeader(self, header):
728 """Check if a header has already been included.
729
730 Args:
731 header: header to check.
732 Returns:
733 Line number of previous occurrence, or -1 if the header has not
734 been seen before.
735 """
736 for section_list in self.include_list:
737 for f in section_list:
738 if f[0] == header:
739 return f[1]
740 return -1
741
742 def ResetSection(self, directive):
743 """Reset section checking for preprocessor directive.
744
745 Args:
746 directive: preprocessor directive (e.g. "if", "else").
747 """
erg@google.coma868d2d2009-10-09 21:18:45 +0000748 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000749 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000750 # The path of last found header.
751 self._last_header = ''
752
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000753 # Update list of includes. Note that we never pop from the
754 # include list.
755 if directive in ('if', 'ifdef', 'ifndef'):
756 self.include_list.append([])
757 elif directive in ('else', 'elif'):
758 self.include_list[-1] = []
759
erg@google.comfd5da632013-10-25 17:39:45 +0000760 def SetLastHeader(self, header_path):
761 self._last_header = header_path
762
erg@google.coma868d2d2009-10-09 21:18:45 +0000763 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000764 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000765
766 - replaces "-" with "_" so they both cmp the same.
767 - removes '-inl' since we don't require them to be after the main header.
768 - lowercase everything, just in case.
769
770 Args:
771 header_path: Path to be canonicalized.
772
773 Returns:
774 Canonicalized path.
775 """
776 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
777
erg@google.comfd5da632013-10-25 17:39:45 +0000778 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.coma868d2d2009-10-09 21:18:45 +0000779 """Check if a header is in alphabetical order with the previous header.
780
781 Args:
erg@google.comfd5da632013-10-25 17:39:45 +0000782 clean_lines: A CleansedLines instance containing the file.
783 linenum: The number of the line to check.
784 header_path: Canonicalized header to be checked.
erg@google.coma868d2d2009-10-09 21:18:45 +0000785
786 Returns:
787 Returns true if the header is in alphabetical order.
788 """
erg@google.comfd5da632013-10-25 17:39:45 +0000789 # If previous section is different from current section, _last_header will
790 # be reset to empty string, so it's always less than current header.
791 #
792 # If previous line was a blank line, assume that the headers are
793 # intentionally sorted the way they are.
794 if (self._last_header > header_path and
avakulenko@google.com554223d2014-12-04 22:00:20 +0000795 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])):
erg@google.coma868d2d2009-10-09 21:18:45 +0000796 return False
erg@google.coma868d2d2009-10-09 21:18:45 +0000797 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000798
799 def CheckNextIncludeOrder(self, header_type):
800 """Returns a non-empty error message if the next header is out of order.
801
802 This function also updates the internal state to be ready to check
803 the next include.
804
805 Args:
806 header_type: One of the _XXX_HEADER constants defined above.
807
808 Returns:
809 The empty string if the header is in the right order, or an
810 error message describing what's wrong.
811
812 """
813 error_message = ('Found %s after %s' %
814 (self._TYPE_NAMES[header_type],
815 self._SECTION_NAMES[self._section]))
816
erg@google.coma868d2d2009-10-09 21:18:45 +0000817 last_section = self._section
818
erg@google.com4e00b9a2009-01-12 23:05:11 +0000819 if header_type == _C_SYS_HEADER:
820 if self._section <= self._C_SECTION:
821 self._section = self._C_SECTION
822 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000823 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000824 return error_message
825 elif header_type == _CPP_SYS_HEADER:
826 if self._section <= self._CPP_SECTION:
827 self._section = self._CPP_SECTION
828 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000829 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000830 return error_message
831 elif header_type == _LIKELY_MY_HEADER:
832 if self._section <= self._MY_H_SECTION:
833 self._section = self._MY_H_SECTION
834 else:
835 self._section = self._OTHER_H_SECTION
836 elif header_type == _POSSIBLE_MY_HEADER:
837 if self._section <= self._MY_H_SECTION:
838 self._section = self._MY_H_SECTION
839 else:
840 # This will always be the fallback because we're not sure
841 # enough that the header is associated with this file.
842 self._section = self._OTHER_H_SECTION
843 else:
844 assert header_type == _OTHER_HEADER
845 self._section = self._OTHER_H_SECTION
846
erg@google.coma868d2d2009-10-09 21:18:45 +0000847 if last_section != self._section:
848 self._last_header = ''
849
erg@google.com4e00b9a2009-01-12 23:05:11 +0000850 return ''
851
852
853class _CppLintState(object):
854 """Maintains module-wide state.."""
855
856 def __init__(self):
857 self.verbose_level = 1 # global setting.
858 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000859 # filters to apply when emitting error messages
860 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000861 # backup of filter list. Used to restore the state after each file.
862 self._filters_backup = self.filters[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000863 self.counting = 'total' # In what way are we counting errors?
864 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000865
866 # output format:
867 # "emacs" - format that emacs can parse (default)
868 # "vs7" - format that Microsoft Visual Studio 7 can parse
869 self.output_format = 'emacs'
870
871 def SetOutputFormat(self, output_format):
872 """Sets the output format for errors."""
873 self.output_format = output_format
874
875 def SetVerboseLevel(self, level):
876 """Sets the module's verbosity, and returns the previous setting."""
877 last_verbose_level = self.verbose_level
878 self.verbose_level = level
879 return last_verbose_level
880
erg@google.coma868d2d2009-10-09 21:18:45 +0000881 def SetCountingStyle(self, counting_style):
882 """Sets the module's counting options."""
883 self.counting = counting_style
884
erg@google.com4e00b9a2009-01-12 23:05:11 +0000885 def SetFilters(self, filters):
886 """Sets the error-message filters.
887
888 These filters are applied when deciding whether to emit a given
889 error message.
890
891 Args:
892 filters: A string of comma-separated filters (eg "+whitespace/indent").
893 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000894
895 Raises:
896 ValueError: The comma-separated filters did not all start with '+' or '-'.
897 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000898 """
erg@google.come35f7652009-06-19 20:52:09 +0000899 # Default filters always have less priority than the flag ones.
900 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000901 self.AddFilters(filters)
902
903 def AddFilters(self, filters):
904 """ Adds more filters to the existing list of error-message filters. """
erg@google.come35f7652009-06-19 20:52:09 +0000905 for filt in filters.split(','):
906 clean_filt = filt.strip()
907 if clean_filt:
908 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000909 for filt in self.filters:
910 if not (filt.startswith('+') or filt.startswith('-')):
911 raise ValueError('Every filter in --filters must start with + or -'
912 ' (%s does not)' % filt)
913
erg@google.com7430eef2014-07-28 22:33:46 +0000914 def BackupFilters(self):
915 """ Saves the current filter list to backup storage."""
916 self._filters_backup = self.filters[:]
917
918 def RestoreFilters(self):
919 """ Restores filters previously backed up."""
920 self.filters = self._filters_backup[:]
921
erg@google.coma868d2d2009-10-09 21:18:45 +0000922 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000923 """Sets the module's error statistic back to zero."""
924 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000925 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000926
erg@google.coma868d2d2009-10-09 21:18:45 +0000927 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000928 """Bumps the module's error statistic."""
929 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000930 if self.counting in ('toplevel', 'detailed'):
931 if self.counting != 'detailed':
932 category = category.split('/')[0]
933 if category not in self.errors_by_category:
934 self.errors_by_category[category] = 0
935 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000936
erg@google.coma868d2d2009-10-09 21:18:45 +0000937 def PrintErrorCounts(self):
938 """Print a summary of errors by category, and the total."""
939 for category, count in self.errors_by_category.iteritems():
940 sys.stderr.write('Category \'%s\' errors found: %d\n' %
941 (category, count))
942 sys.stderr.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000943
944_cpplint_state = _CppLintState()
945
946
947def _OutputFormat():
948 """Gets the module's output format."""
949 return _cpplint_state.output_format
950
951
952def _SetOutputFormat(output_format):
953 """Sets the module's output format."""
954 _cpplint_state.SetOutputFormat(output_format)
955
956
957def _VerboseLevel():
958 """Returns the module's verbosity setting."""
959 return _cpplint_state.verbose_level
960
961
962def _SetVerboseLevel(level):
963 """Sets the module's verbosity, and returns the previous setting."""
964 return _cpplint_state.SetVerboseLevel(level)
965
966
erg@google.coma868d2d2009-10-09 21:18:45 +0000967def _SetCountingStyle(level):
968 """Sets the module's counting options."""
969 _cpplint_state.SetCountingStyle(level)
970
971
erg@google.com4e00b9a2009-01-12 23:05:11 +0000972def _Filters():
973 """Returns the module's list of output filters, as a list."""
974 return _cpplint_state.filters
975
976
977def _SetFilters(filters):
978 """Sets the module's error-message filters.
979
980 These filters are applied when deciding whether to emit a given
981 error message.
982
983 Args:
984 filters: A string of comma-separated filters (eg "whitespace/indent").
985 Each filter should start with + or -; else we die.
986 """
987 _cpplint_state.SetFilters(filters)
988
erg@google.com7430eef2014-07-28 22:33:46 +0000989def _AddFilters(filters):
990 """Adds more filter overrides.
991
992 Unlike _SetFilters, this function does not reset the current list of filters
993 available.
994
995 Args:
996 filters: A string of comma-separated filters (eg "whitespace/indent").
997 Each filter should start with + or -; else we die.
998 """
999 _cpplint_state.AddFilters(filters)
1000
1001def _BackupFilters():
1002 """ Saves the current filter list to backup storage."""
1003 _cpplint_state.BackupFilters()
1004
1005def _RestoreFilters():
1006 """ Restores filters previously backed up."""
1007 _cpplint_state.RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001008
1009class _FunctionState(object):
1010 """Tracks current function name and the number of lines in its body."""
1011
1012 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
1013 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
1014
1015 def __init__(self):
1016 self.in_a_function = False
1017 self.lines_in_function = 0
1018 self.current_function = ''
1019
1020 def Begin(self, function_name):
1021 """Start analyzing function body.
1022
1023 Args:
1024 function_name: The name of the function being tracked.
1025 """
1026 self.in_a_function = True
1027 self.lines_in_function = 0
1028 self.current_function = function_name
1029
1030 def Count(self):
1031 """Count line in current function body."""
1032 if self.in_a_function:
1033 self.lines_in_function += 1
1034
1035 def Check(self, error, filename, linenum):
1036 """Report if too many lines in function body.
1037
1038 Args:
1039 error: The function to call with any errors found.
1040 filename: The name of the current file.
1041 linenum: The number of the line to check.
1042 """
Alex Vakulenko01e47232016-05-06 13:54:15 -07001043 if not self.in_a_function:
1044 return
1045
erg@google.com4e00b9a2009-01-12 23:05:11 +00001046 if Match(r'T(EST|est)', self.current_function):
1047 base_trigger = self._TEST_TRIGGER
1048 else:
1049 base_trigger = self._NORMAL_TRIGGER
1050 trigger = base_trigger * 2**_VerboseLevel()
1051
1052 if self.lines_in_function > trigger:
1053 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
1054 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1055 if error_level > 5:
1056 error_level = 5
1057 error(filename, linenum, 'readability/fn_size', error_level,
1058 'Small and focused functions are preferred:'
1059 ' %s has %d non-comment lines'
1060 ' (error triggered by exceeding %d lines).' % (
1061 self.current_function, self.lines_in_function, trigger))
1062
1063 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001064 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001065 self.in_a_function = False
1066
1067
1068class _IncludeError(Exception):
1069 """Indicates a problem with the include order in a file."""
1070 pass
1071
1072
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001073class FileInfo(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001074 """Provides utility functions for filenames.
1075
1076 FileInfo provides easy access to the components of a file's path
1077 relative to the project root.
1078 """
1079
1080 def __init__(self, filename):
1081 self._filename = filename
1082
1083 def FullName(self):
1084 """Make Windows paths like Unix."""
1085 return os.path.abspath(self._filename).replace('\\', '/')
1086
1087 def RepositoryName(self):
1088 """FullName after removing the local path to the repository.
1089
1090 If we have a real absolute path name here we can try to do something smart:
1091 detecting the root of the checkout and truncating /path/to/checkout from
1092 the name so that we get header guards that don't include things like
1093 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
1094 people on different computers who have checked the source out to different
1095 locations won't see bogus errors.
1096 """
1097 fullname = self.FullName()
1098
1099 if os.path.exists(fullname):
1100 project_dir = os.path.dirname(fullname)
1101
1102 if os.path.exists(os.path.join(project_dir, ".svn")):
1103 # If there's a .svn file in the current directory, we recursively look
1104 # up the directory tree for the top of the SVN checkout
1105 root_dir = project_dir
1106 one_up_dir = os.path.dirname(root_dir)
1107 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1108 root_dir = os.path.dirname(root_dir)
1109 one_up_dir = os.path.dirname(one_up_dir)
1110
1111 prefix = os.path.commonprefix([root_dir, project_dir])
1112 return fullname[len(prefix) + 1:]
1113
erg@google.com3dc74262011-11-30 01:12:00 +00001114 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1115 # searching up from the current path.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001116 root_dir = current_dir = os.path.dirname(fullname)
1117 while current_dir != os.path.dirname(current_dir):
1118 if (os.path.exists(os.path.join(current_dir, ".git")) or
1119 os.path.exists(os.path.join(current_dir, ".hg")) or
1120 os.path.exists(os.path.join(current_dir, ".svn"))):
1121 root_dir = current_dir
1122 current_dir = os.path.dirname(current_dir)
erg@google.com42e59b02010-10-04 22:18:07 +00001123
1124 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +00001125 os.path.exists(os.path.join(root_dir, ".hg")) or
1126 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +00001127 prefix = os.path.commonprefix([root_dir, project_dir])
1128 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +00001129
1130 # Don't know what to do; header guard warnings may be wrong...
1131 return fullname
1132
1133 def Split(self):
1134 """Splits the file into the directory, basename, and extension.
1135
1136 For 'chrome/browser/browser.cc', Split() would
1137 return ('chrome/browser', 'browser', '.cc')
1138
1139 Returns:
1140 A tuple of (directory, basename, extension).
1141 """
1142
1143 googlename = self.RepositoryName()
1144 project, rest = os.path.split(googlename)
1145 return (project,) + os.path.splitext(rest)
1146
1147 def BaseName(self):
1148 """File base name - text after the final slash, before the final period."""
1149 return self.Split()[1]
1150
1151 def Extension(self):
1152 """File extension - text following the final period."""
1153 return self.Split()[2]
1154
1155 def NoExtension(self):
1156 """File has no source file extension."""
1157 return '/'.join(self.Split()[0:2])
1158
1159 def IsSource(self):
1160 """File has a source file extension."""
Alex Vakulenko01e47232016-05-06 13:54:15 -07001161 return _IsSourceExtension(self.Extension()[1:])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001162
1163
erg+personal@google.com05189642010-04-30 20:43:03 +00001164def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001165 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +00001166
1167 # There are three ways we might decide not to print an error message:
1168 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +00001169 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +00001170 if IsErrorSuppressedByNolint(category, linenum):
1171 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00001172
erg@google.com4e00b9a2009-01-12 23:05:11 +00001173 if confidence < _cpplint_state.verbose_level:
1174 return False
1175
1176 is_filtered = False
1177 for one_filter in _Filters():
1178 if one_filter.startswith('-'):
1179 if category.startswith(one_filter[1:]):
1180 is_filtered = True
1181 elif one_filter.startswith('+'):
1182 if category.startswith(one_filter[1:]):
1183 is_filtered = False
1184 else:
1185 assert False # should have been checked for in SetFilter.
1186 if is_filtered:
1187 return False
1188
1189 return True
1190
1191
1192def Error(filename, linenum, category, confidence, message):
1193 """Logs the fact we've found a lint error.
1194
1195 We log where the error was found, and also our confidence in the error,
1196 that is, how certain we are this is a legitimate style regression, and
1197 not a misidentification or a use that's sometimes justified.
1198
erg+personal@google.com05189642010-04-30 20:43:03 +00001199 False positives can be suppressed by the use of
1200 "cpplint(category)" comments on the offending line. These are
1201 parsed into _error_suppressions.
1202
erg@google.com4e00b9a2009-01-12 23:05:11 +00001203 Args:
1204 filename: The name of the file containing the error.
1205 linenum: The number of the line containing the error.
1206 category: A string used to describe the "category" this bug
1207 falls under: "whitespace", say, or "runtime". Categories
1208 may have a hierarchy separated by slashes: "whitespace/indent".
1209 confidence: A number from 1-5 representing a confidence score for
1210 the error, with 5 meaning that we are certain of the problem,
1211 and 1 meaning that it could be a legitimate construct.
1212 message: The error message.
1213 """
erg+personal@google.com05189642010-04-30 20:43:03 +00001214 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +00001215 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001216 if _cpplint_state.output_format == 'vs7':
1217 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
1218 filename, linenum, message, category, confidence))
erg@google.com02c27fd2013-05-28 21:34:34 +00001219 elif _cpplint_state.output_format == 'eclipse':
1220 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1221 filename, linenum, message, category, confidence))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001222 else:
1223 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1224 filename, linenum, message, category, confidence))
1225
1226
erg@google.com2aa59982013-10-28 19:09:25 +00001227# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001228_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1229 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.com02af6282014-06-04 18:53:25 +00001230# Match a single C style comment on the same line.
1231_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1232# Matches multi-line C style comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001233# This RE is a little bit more complicated than one might expect, because we
1234# have to take care of space removals tools so we can handle comments inside
1235# statements better.
1236# The current rule is: We only clear spaces from both sides when we're at the
1237# end of the line. Otherwise, we try to remove spaces from the right side,
1238# if this doesn't work we try on left side but only if there's a non-character
1239# on the right.
1240_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.com02af6282014-06-04 18:53:25 +00001241 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1242 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1243 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1244 _RE_PATTERN_C_COMMENTS + r')')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001245
1246
1247def IsCppString(line):
1248 """Does line terminate so, that the next symbol is in string constant.
1249
1250 This function does not consider single-line nor multi-line comments.
1251
1252 Args:
1253 line: is a partial line of code starting from the 0..n.
1254
1255 Returns:
1256 True, if next character appended to 'line' is inside a
1257 string constant.
1258 """
1259
1260 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1261 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1262
1263
erg@google.com2aa59982013-10-28 19:09:25 +00001264def CleanseRawStrings(raw_lines):
1265 """Removes C++11 raw strings from lines.
1266
1267 Before:
1268 static const char kData[] = R"(
1269 multi-line string
1270 )";
1271
1272 After:
1273 static const char kData[] = ""
1274 (replaced by blank line)
1275 "";
1276
1277 Args:
1278 raw_lines: list of raw lines.
1279
1280 Returns:
1281 list of lines with C++11 raw strings replaced by empty strings.
1282 """
1283
1284 delimiter = None
1285 lines_without_raw_strings = []
1286 for line in raw_lines:
1287 if delimiter:
1288 # Inside a raw string, look for the end
1289 end = line.find(delimiter)
1290 if end >= 0:
1291 # Found the end of the string, match leading space for this
1292 # line and resume copying the original lines, and also insert
1293 # a "" on the last line.
1294 leading_space = Match(r'^(\s*)\S', line)
1295 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1296 delimiter = None
1297 else:
1298 # Haven't found the end yet, append a blank line.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001299 line = '""'
erg@google.com2aa59982013-10-28 19:09:25 +00001300
avakulenko@google.com02af6282014-06-04 18:53:25 +00001301 # Look for beginning of a raw string, and replace them with
1302 # empty strings. This is done in a loop to handle multiple raw
1303 # strings on the same line.
1304 while delimiter is None:
erg@google.com2aa59982013-10-28 19:09:25 +00001305 # Look for beginning of a raw string.
1306 # See 2.14.15 [lex.string] for syntax.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001307 #
1308 # Once we have matched a raw string, we check the prefix of the
1309 # line to make sure that the line is not part of a single line
1310 # comment. It's done this way because we remove raw strings
1311 # before removing comments as opposed to removing comments
1312 # before removing raw strings. This is because there are some
1313 # cpplint checks that requires the comments to be preserved, but
1314 # we don't want to check comments that are inside raw strings.
1315 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1316 if (matched and
1317 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
1318 matched.group(1))):
erg@google.com2aa59982013-10-28 19:09:25 +00001319 delimiter = ')' + matched.group(2) + '"'
1320
1321 end = matched.group(3).find(delimiter)
1322 if end >= 0:
1323 # Raw string ended on same line
1324 line = (matched.group(1) + '""' +
1325 matched.group(3)[end + len(delimiter):])
1326 delimiter = None
1327 else:
1328 # Start of a multi-line raw string
1329 line = matched.group(1) + '""'
avakulenko@google.com02af6282014-06-04 18:53:25 +00001330 else:
1331 break
erg@google.com2aa59982013-10-28 19:09:25 +00001332
1333 lines_without_raw_strings.append(line)
1334
1335 # TODO(unknown): if delimiter is not None here, we might want to
1336 # emit a warning for unterminated string.
1337 return lines_without_raw_strings
1338
1339
erg@google.com4e00b9a2009-01-12 23:05:11 +00001340def FindNextMultiLineCommentStart(lines, lineix):
1341 """Find the beginning marker for a multiline comment."""
1342 while lineix < len(lines):
1343 if lines[lineix].strip().startswith('/*'):
1344 # Only return this marker if the comment goes beyond this line
1345 if lines[lineix].strip().find('*/', 2) < 0:
1346 return lineix
1347 lineix += 1
1348 return len(lines)
1349
1350
1351def FindNextMultiLineCommentEnd(lines, lineix):
1352 """We are inside a comment, find the end marker."""
1353 while lineix < len(lines):
1354 if lines[lineix].strip().endswith('*/'):
1355 return lineix
1356 lineix += 1
1357 return len(lines)
1358
1359
1360def RemoveMultiLineCommentsFromRange(lines, begin, end):
1361 """Clears a range of lines for multi-line comments."""
1362 # Having // dummy comments makes the lines non-empty, so we will not get
1363 # unnecessary blank line warnings later in the code.
1364 for i in range(begin, end):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001365 lines[i] = '/**/'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001366
1367
1368def RemoveMultiLineComments(filename, lines, error):
1369 """Removes multiline (c-style) comments from lines."""
1370 lineix = 0
1371 while lineix < len(lines):
1372 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1373 if lineix_begin >= len(lines):
1374 return
1375 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1376 if lineix_end >= len(lines):
1377 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1378 'Could not find end of multi-line comment')
1379 return
1380 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1381 lineix = lineix_end + 1
1382
1383
1384def CleanseComments(line):
1385 """Removes //-comments and single-line C-style /* */ comments.
1386
1387 Args:
1388 line: A line of C++ source.
1389
1390 Returns:
1391 The line with single-line comments removed.
1392 """
1393 commentpos = line.find('//')
1394 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +00001395 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001396 # get rid of /* ... */
1397 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1398
1399
erg@google.coma87abb82009-02-24 01:41:01 +00001400class CleansedLines(object):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001401 """Holds 4 copies of all lines with different preprocessing applied to them.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001402
avakulenko@google.com554223d2014-12-04 22:00:20 +00001403 1) elided member contains lines without strings and comments.
1404 2) lines member contains lines without comments.
erg@google.comd350fe52013-01-14 17:51:48 +00001405 3) raw_lines member contains all the lines without processing.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001406 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw
1407 strings removed.
1408 All these members are of <type 'list'>, and of the same length.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001409 """
1410
1411 def __init__(self, lines):
1412 self.elided = []
1413 self.lines = []
1414 self.raw_lines = lines
1415 self.num_lines = len(lines)
erg@google.com2aa59982013-10-28 19:09:25 +00001416 self.lines_without_raw_strings = CleanseRawStrings(lines)
1417 for linenum in range(len(self.lines_without_raw_strings)):
1418 self.lines.append(CleanseComments(
1419 self.lines_without_raw_strings[linenum]))
1420 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001421 self.elided.append(CleanseComments(elided))
1422
1423 def NumLines(self):
1424 """Returns the number of lines represented."""
1425 return self.num_lines
1426
1427 @staticmethod
1428 def _CollapseStrings(elided):
1429 """Collapses strings and chars on a line to simple "" or '' blocks.
1430
1431 We nix strings first so we're not fooled by text like '"http://"'
1432
1433 Args:
1434 elided: The line being processed.
1435
1436 Returns:
1437 The line with collapsed strings.
1438 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001439 if _RE_PATTERN_INCLUDE.match(elided):
1440 return elided
1441
1442 # Remove escaped characters first to make quote/single quote collapsing
1443 # basic. Things that look like escaped characters shouldn't occur
1444 # outside of strings and chars.
1445 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1446
1447 # Replace quoted strings and digit separators. Both single quotes
1448 # and double quotes are processed in the same loop, otherwise
1449 # nested quotes wouldn't work.
1450 collapsed = ''
1451 while True:
1452 # Find the first quote character
1453 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1454 if not match:
1455 collapsed += elided
1456 break
1457 head, quote, tail = match.groups()
1458
1459 if quote == '"':
1460 # Collapse double quoted strings
1461 second_quote = tail.find('"')
1462 if second_quote >= 0:
1463 collapsed += head + '""'
1464 elided = tail[second_quote + 1:]
1465 else:
1466 # Unmatched double quote, don't bother processing the rest
1467 # of the line since this is probably a multiline string.
1468 collapsed += elided
1469 break
1470 else:
1471 # Found single quote, check nearby text to eliminate digit separators.
1472 #
1473 # There is no special handling for floating point here, because
1474 # the integer/fractional/exponent parts would all be parsed
1475 # correctly as long as there are digits on both sides of the
1476 # separator. So we are fine as long as we don't see something
1477 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1478 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1479 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1480 collapsed += head + match_literal.group(1).replace("'", '')
1481 elided = match_literal.group(2)
1482 else:
1483 second_quote = tail.find('\'')
1484 if second_quote >= 0:
1485 collapsed += head + "''"
1486 elided = tail[second_quote + 1:]
1487 else:
1488 # Unmatched single quote
1489 collapsed += elided
1490 break
1491
1492 return collapsed
erg@google.com4e00b9a2009-01-12 23:05:11 +00001493
1494
avakulenko@google.com02af6282014-06-04 18:53:25 +00001495def FindEndOfExpressionInLine(line, startpos, stack):
1496 """Find the position just after the end of current parenthesized expression.
erg@google.comd350fe52013-01-14 17:51:48 +00001497
1498 Args:
1499 line: a CleansedLines line.
1500 startpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001501 stack: nesting stack at startpos.
erg@google.comd350fe52013-01-14 17:51:48 +00001502
1503 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001504 On finding matching end: (index just after matching end, None)
1505 On finding an unclosed expression: (-1, None)
1506 Otherwise: (-1, new stack at end of this line)
erg@google.comd350fe52013-01-14 17:51:48 +00001507 """
1508 for i in xrange(startpos, len(line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00001509 char = line[i]
1510 if char in '([{':
1511 # Found start of parenthesized expression, push to expression stack
1512 stack.append(char)
1513 elif char == '<':
1514 # Found potential start of template argument list
1515 if i > 0 and line[i - 1] == '<':
1516 # Left shift operator
1517 if stack and stack[-1] == '<':
1518 stack.pop()
1519 if not stack:
1520 return (-1, None)
1521 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1522 # operator<, don't add to stack
1523 continue
1524 else:
1525 # Tentative start of template argument list
1526 stack.append('<')
1527 elif char in ')]}':
1528 # Found end of parenthesized expression.
1529 #
1530 # If we are currently expecting a matching '>', the pending '<'
1531 # must have been an operator. Remove them from expression stack.
1532 while stack and stack[-1] == '<':
1533 stack.pop()
1534 if not stack:
1535 return (-1, None)
1536 if ((stack[-1] == '(' and char == ')') or
1537 (stack[-1] == '[' and char == ']') or
1538 (stack[-1] == '{' and char == '}')):
1539 stack.pop()
1540 if not stack:
1541 return (i + 1, None)
1542 else:
1543 # Mismatched parentheses
1544 return (-1, None)
1545 elif char == '>':
1546 # Found potential end of template argument list.
1547
1548 # Ignore "->" and operator functions
1549 if (i > 0 and
1550 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1551 continue
1552
1553 # Pop the stack if there is a matching '<'. Otherwise, ignore
1554 # this '>' since it must be an operator.
1555 if stack:
1556 if stack[-1] == '<':
1557 stack.pop()
1558 if not stack:
1559 return (i + 1, None)
1560 elif char == ';':
1561 # Found something that look like end of statements. If we are currently
1562 # expecting a '>', the matching '<' must have been an operator, since
1563 # template argument list should not contain statements.
1564 while stack and stack[-1] == '<':
1565 stack.pop()
1566 if not stack:
1567 return (-1, None)
1568
1569 # Did not find end of expression or unbalanced parentheses on this line
1570 return (-1, stack)
erg@google.comd350fe52013-01-14 17:51:48 +00001571
1572
erg@google.com4e00b9a2009-01-12 23:05:11 +00001573def CloseExpression(clean_lines, linenum, pos):
erg@google.com2aa59982013-10-28 19:09:25 +00001574 """If input points to ( or { or [ or <, finds the position that closes it.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001575
erg@google.com2aa59982013-10-28 19:09:25 +00001576 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001577 linenum/pos that correspond to the closing of the expression.
1578
avakulenko@google.com02af6282014-06-04 18:53:25 +00001579 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1580 Ideally we would want to index all opening and closing parentheses once
1581 and have CloseExpression be just a simple lookup, but due to preprocessor
1582 tricks, this is not so easy.
1583
erg@google.com4e00b9a2009-01-12 23:05:11 +00001584 Args:
1585 clean_lines: A CleansedLines instance containing the file.
1586 linenum: The number of the line to check.
1587 pos: A position on the line.
1588
1589 Returns:
1590 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1591 (line, len(lines), -1) if we never find a close. Note we ignore
1592 strings and comments when matching; and the line we return is the
1593 'cleansed' line at linenum.
1594 """
1595
1596 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001597 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001598 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001599
erg@google.comd350fe52013-01-14 17:51:48 +00001600 # Check first line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001601 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
erg@google.comd350fe52013-01-14 17:51:48 +00001602 if end_pos > -1:
1603 return (line, linenum, end_pos)
erg@google.com2aa59982013-10-28 19:09:25 +00001604
1605 # Continue scanning forward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001606 while stack and linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001607 linenum += 1
1608 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001609 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001610 if end_pos > -1:
1611 return (line, linenum, end_pos)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001612
avakulenko@google.com02af6282014-06-04 18:53:25 +00001613 # Did not find end of expression before end of file, give up
erg@google.comd350fe52013-01-14 17:51:48 +00001614 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001615
erg@google.com2aa59982013-10-28 19:09:25 +00001616
avakulenko@google.com02af6282014-06-04 18:53:25 +00001617def FindStartOfExpressionInLine(line, endpos, stack):
1618 """Find position at the matching start of current expression.
erg@google.com2aa59982013-10-28 19:09:25 +00001619
1620 This is almost the reverse of FindEndOfExpressionInLine, but note
1621 that the input position and returned position differs by 1.
1622
1623 Args:
1624 line: a CleansedLines line.
1625 endpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001626 stack: nesting stack at endpos.
erg@google.com2aa59982013-10-28 19:09:25 +00001627
1628 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001629 On finding matching start: (index at matching start, None)
1630 On finding an unclosed expression: (-1, None)
1631 Otherwise: (-1, new stack at beginning of this line)
erg@google.com2aa59982013-10-28 19:09:25 +00001632 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001633 i = endpos
1634 while i >= 0:
1635 char = line[i]
1636 if char in ')]}':
1637 # Found end of expression, push to expression stack
1638 stack.append(char)
1639 elif char == '>':
1640 # Found potential end of template argument list.
1641 #
1642 # Ignore it if it's a "->" or ">=" or "operator>"
1643 if (i > 0 and
1644 (line[i - 1] == '-' or
1645 Match(r'\s>=\s', line[i - 1:]) or
1646 Search(r'\boperator\s*$', line[0:i]))):
1647 i -= 1
1648 else:
1649 stack.append('>')
1650 elif char == '<':
1651 # Found potential start of template argument list
1652 if i > 0 and line[i - 1] == '<':
1653 # Left shift operator
1654 i -= 1
1655 else:
1656 # If there is a matching '>', we can pop the expression stack.
1657 # Otherwise, ignore this '<' since it must be an operator.
1658 if stack and stack[-1] == '>':
1659 stack.pop()
1660 if not stack:
1661 return (i, None)
1662 elif char in '([{':
1663 # Found start of expression.
1664 #
1665 # If there are any unmatched '>' on the stack, they must be
1666 # operators. Remove those.
1667 while stack and stack[-1] == '>':
1668 stack.pop()
1669 if not stack:
1670 return (-1, None)
1671 if ((char == '(' and stack[-1] == ')') or
1672 (char == '[' and stack[-1] == ']') or
1673 (char == '{' and stack[-1] == '}')):
1674 stack.pop()
1675 if not stack:
1676 return (i, None)
1677 else:
1678 # Mismatched parentheses
1679 return (-1, None)
1680 elif char == ';':
1681 # Found something that look like end of statements. If we are currently
1682 # expecting a '<', the matching '>' must have been an operator, since
1683 # template argument list should not contain statements.
1684 while stack and stack[-1] == '>':
1685 stack.pop()
1686 if not stack:
1687 return (-1, None)
1688
1689 i -= 1
1690
1691 return (-1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001692
1693
1694def ReverseCloseExpression(clean_lines, linenum, pos):
1695 """If input points to ) or } or ] or >, finds the position that opens it.
1696
1697 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1698 linenum/pos that correspond to the opening of the expression.
1699
1700 Args:
1701 clean_lines: A CleansedLines instance containing the file.
1702 linenum: The number of the line to check.
1703 pos: A position on the line.
1704
1705 Returns:
1706 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1707 (line, 0, -1) if we never find the matching opening brace. Note
1708 we ignore strings and comments when matching; and the line we
1709 return is the 'cleansed' line at linenum.
1710 """
1711 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001712 if line[pos] not in ')}]>':
erg@google.com2aa59982013-10-28 19:09:25 +00001713 return (line, 0, -1)
erg@google.com2aa59982013-10-28 19:09:25 +00001714
1715 # Check last line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001716 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
erg@google.com2aa59982013-10-28 19:09:25 +00001717 if start_pos > -1:
1718 return (line, linenum, start_pos)
1719
1720 # Continue scanning backward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001721 while stack and linenum > 0:
erg@google.com2aa59982013-10-28 19:09:25 +00001722 linenum -= 1
1723 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001724 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001725 if start_pos > -1:
1726 return (line, linenum, start_pos)
1727
avakulenko@google.com02af6282014-06-04 18:53:25 +00001728 # Did not find start of expression before beginning of file, give up
erg@google.com2aa59982013-10-28 19:09:25 +00001729 return (line, 0, -1)
1730
1731
erg@google.com4e00b9a2009-01-12 23:05:11 +00001732def CheckForCopyright(filename, lines, error):
1733 """Logs an error if no Copyright message appears at the top of the file."""
1734
1735 # We'll say it should occur by line 10. Don't forget there's a
1736 # dummy line at the front.
1737 for line in xrange(1, min(len(lines), 11)):
1738 if re.search(r'Copyright', lines[line], re.I): break
1739 else: # means no copyright line was found
1740 error(filename, 0, 'legal/copyright', 5,
1741 'No copyright message found. '
1742 'You should have a line: "Copyright [year] <Copyright Owner>"')
1743
1744
avakulenko@google.com02af6282014-06-04 18:53:25 +00001745def GetIndentLevel(line):
1746 """Return the number of leading spaces in line.
1747
1748 Args:
1749 line: A string to check.
1750
1751 Returns:
1752 An integer count of leading spaces, possibly zero.
1753 """
1754 indent = Match(r'^( *)\S', line)
1755 if indent:
1756 return len(indent.group(1))
1757 else:
1758 return 0
1759
1760
erg@google.com4e00b9a2009-01-12 23:05:11 +00001761def GetHeaderGuardCPPVariable(filename):
1762 """Returns the CPP variable that should be used as a header guard.
1763
1764 Args:
1765 filename: The name of a C++ header file.
1766
1767 Returns:
1768 The CPP variable that should be used as a header guard in the
1769 named file.
1770
1771 """
1772
erg+personal@google.com05189642010-04-30 20:43:03 +00001773 # Restores original filename in case that cpplint is invoked from Emacs's
1774 # flymake.
1775 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001776 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001777 # Replace 'c++' with 'cpp'.
1778 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')
Alex Vakulenko01e47232016-05-06 13:54:15 -07001779
erg@google.com4e00b9a2009-01-12 23:05:11 +00001780 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001781 file_path_from_root = fileinfo.RepositoryName()
1782 if _root:
Sergey Sharybin3b0ea892016-05-31 00:21:14 +02001783 suffix = os.sep
1784 # On Windows using directory separator will leave us with
1785 # "bogus escape error" unless we properly escape regex.
1786 if suffix == '\\':
1787 suffix += '\\'
1788 file_path_from_root = re.sub('^' + _root + suffix, '', file_path_from_root)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001789 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001790
1791
avakulenko@google.com554223d2014-12-04 22:00:20 +00001792def CheckForHeaderGuard(filename, clean_lines, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001793 """Checks that the file contains a header guard.
1794
erg@google.coma87abb82009-02-24 01:41:01 +00001795 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001796 headers, checks that the full pathname is used.
1797
1798 Args:
1799 filename: The name of the C++ header file.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001800 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001801 error: The function to call with any errors found.
1802 """
1803
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001804 # Don't check for header guards if there are error suppression
1805 # comments somewhere in this file.
1806 #
1807 # Because this is silencing a warning for a nonexistent line, we
1808 # only support the very specific NOLINT(build/header_guard) syntax,
1809 # and not the general NOLINT or NOLINT(*) syntax.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001810 raw_lines = clean_lines.lines_without_raw_strings
1811 for i in raw_lines:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001812 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1813 return
1814
erg@google.com4e00b9a2009-01-12 23:05:11 +00001815 cppvar = GetHeaderGuardCPPVariable(filename)
1816
avakulenko@google.com554223d2014-12-04 22:00:20 +00001817 ifndef = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001818 ifndef_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001819 define = ''
1820 endif = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001821 endif_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001822 for linenum, line in enumerate(raw_lines):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001823 linesplit = line.split()
1824 if len(linesplit) >= 2:
1825 # find the first occurrence of #ifndef and #define, save arg
1826 if not ifndef and linesplit[0] == '#ifndef':
1827 # set ifndef to the header guard presented on the #ifndef line.
1828 ifndef = linesplit[1]
1829 ifndef_linenum = linenum
1830 if not define and linesplit[0] == '#define':
1831 define = linesplit[1]
1832 # find the last occurrence of #endif, save entire line
1833 if line.startswith('#endif'):
1834 endif = line
1835 endif_linenum = linenum
1836
avakulenko@google.com554223d2014-12-04 22:00:20 +00001837 if not ifndef or not define or ifndef != define:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001838 error(filename, 0, 'build/header_guard', 5,
1839 'No #ifndef header guard found, suggested CPP variable is: %s' %
1840 cppvar)
1841 return
1842
1843 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1844 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001845 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001846 error_level = 0
1847 if ifndef != cppvar + '_':
1848 error_level = 5
1849
avakulenko@google.com554223d2014-12-04 22:00:20 +00001850 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum,
erg+personal@google.com05189642010-04-30 20:43:03 +00001851 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001852 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1853 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1854
avakulenko@google.com554223d2014-12-04 22:00:20 +00001855 # Check for "//" comments on endif line.
1856 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum,
1857 error)
1858 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif)
1859 if match:
1860 if match.group(1) == '_':
1861 # Issue low severity warning for deprecated double trailing underscore
1862 error(filename, endif_linenum, 'build/header_guard', 0,
1863 '#endif line should be "#endif // %s"' % cppvar)
erg@google.comdc289702012-01-26 20:30:03 +00001864 return
1865
avakulenko@google.com554223d2014-12-04 22:00:20 +00001866 # Didn't find the corresponding "//" comment. If this file does not
1867 # contain any "//" comments at all, it could be that the compiler
1868 # only wants "/**/" comments, look for those instead.
1869 no_single_line_comments = True
1870 for i in xrange(1, len(raw_lines) - 1):
1871 line = raw_lines[i]
1872 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
1873 no_single_line_comments = False
1874 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00001875
avakulenko@google.com554223d2014-12-04 22:00:20 +00001876 if no_single_line_comments:
1877 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif)
1878 if match:
1879 if match.group(1) == '_':
1880 # Low severity warning for double trailing underscore
1881 error(filename, endif_linenum, 'build/header_guard', 0,
1882 '#endif line should be "#endif /* %s */"' % cppvar)
1883 return
1884
1885 # Didn't find anything
1886 error(filename, endif_linenum, 'build/header_guard', 5,
1887 '#endif line should be "#endif // %s"' % cppvar)
1888
1889
1890def CheckHeaderFileIncluded(filename, include_state, error):
1891 """Logs an error if a .cc file does not include its header."""
1892
1893 # Do not check test files
Alex Vakulenko01e47232016-05-06 13:54:15 -07001894 fileinfo = FileInfo(filename)
1895 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001896 return
1897
Alex Vakulenko01e47232016-05-06 13:54:15 -07001898 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h'
avakulenko@google.com554223d2014-12-04 22:00:20 +00001899 if not os.path.exists(headerfile):
1900 return
1901 headername = FileInfo(headerfile).RepositoryName()
1902 first_include = 0
1903 for section_list in include_state.include_list:
1904 for f in section_list:
1905 if headername in f[0] or f[0] in headername:
1906 return
1907 if not first_include:
1908 first_include = f[1]
1909
1910 error(filename, first_include, 'build/include', 5,
1911 '%s should include its header file %s' % (fileinfo.RepositoryName(),
1912 headername))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001913
1914
erg@google.com2aa59982013-10-28 19:09:25 +00001915def CheckForBadCharacters(filename, lines, error):
1916 """Logs an error for each line containing bad characters.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001917
erg@google.com2aa59982013-10-28 19:09:25 +00001918 Two kinds of bad characters:
1919
1920 1. Unicode replacement characters: These indicate that either the file
1921 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1922 it shouldn't). Note that it's possible for this to throw off line
1923 numbering if the invalid UTF-8 occurred adjacent to a newline.
1924
1925 2. NUL bytes. These are problematic for some tools.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001926
1927 Args:
1928 filename: The name of the current file.
1929 lines: An array of strings, each representing a line of the file.
1930 error: The function to call with any errors found.
1931 """
1932 for linenum, line in enumerate(lines):
1933 if u'\ufffd' in line:
1934 error(filename, linenum, 'readability/utf8', 5,
1935 'Line contains invalid UTF-8 (or Unicode replacement character).')
erg@google.com2aa59982013-10-28 19:09:25 +00001936 if '\0' in line:
1937 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001938
1939
1940def CheckForNewlineAtEOF(filename, lines, error):
1941 """Logs an error if there is no newline char at the end of the file.
1942
1943 Args:
1944 filename: The name of the current file.
1945 lines: An array of strings, each representing a line of the file.
1946 error: The function to call with any errors found.
1947 """
1948
1949 # The array lines() was created by adding two newlines to the
1950 # original file (go figure), then splitting on \n.
1951 # To verify that the file ends in \n, we just have to make sure the
1952 # last-but-two element of lines() exists and is empty.
1953 if len(lines) < 3 or lines[-2]:
1954 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1955 'Could not find a newline character at the end of the file.')
1956
1957
1958def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1959 """Logs an error if we see /* ... */ or "..." that extend past one line.
1960
1961 /* ... */ comments are legit inside macros, for one line.
1962 Otherwise, we prefer // comments, so it's ok to warn about the
1963 other. Likewise, it's ok for strings to extend across multiple
1964 lines, as long as a line continuation character (backslash)
1965 terminates each line. Although not currently prohibited by the C++
1966 style guide, it's ugly and unnecessary. We don't do well with either
1967 in this lint program, so we warn about both.
1968
1969 Args:
1970 filename: The name of the current file.
1971 clean_lines: A CleansedLines instance containing the file.
1972 linenum: The number of the line to check.
1973 error: The function to call with any errors found.
1974 """
1975 line = clean_lines.elided[linenum]
1976
1977 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1978 # second (escaped) slash may trigger later \" detection erroneously.
1979 line = line.replace('\\\\', '')
1980
1981 if line.count('/*') > line.count('*/'):
1982 error(filename, linenum, 'readability/multiline_comment', 5,
1983 'Complex multi-line /*...*/-style comment found. '
1984 'Lint may give bogus warnings. '
1985 'Consider replacing these with //-style comments, '
1986 'with #if 0...#endif, '
1987 'or with more clearly structured multi-line comments.')
1988
1989 if (line.count('"') - line.count('\\"')) % 2:
1990 error(filename, linenum, 'readability/multiline_string', 5,
1991 'Multi-line string ("...") found. This lint script doesn\'t '
erg@google.com2aa59982013-10-28 19:09:25 +00001992 'do well with such strings, and may give bogus warnings. '
1993 'Use C++11 raw strings or concatenation instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001994
1995
avakulenko@google.com02af6282014-06-04 18:53:25 +00001996# (non-threadsafe name, thread-safe alternative, validation pattern)
1997#
1998# The validation pattern is used to eliminate false positives such as:
1999# _rand(); // false positive due to substring match.
2000# ->rand(); // some member function rand().
2001# ACMRandom rand(seed); // some variable named rand.
2002# ISAACRandom rand(); // another variable named rand.
2003#
2004# Basically we require the return value of these functions to be used
2005# in some expression context on the same line by matching on some
2006# operator before the function name. This eliminates constructors and
2007# member function calls.
2008_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
2009_THREADING_LIST = (
2010 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
2011 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
2012 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
2013 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
2014 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
2015 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
2016 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
2017 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
2018 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
2019 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
2020 ('strtok(', 'strtok_r(',
2021 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
2022 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
erg@google.com4e00b9a2009-01-12 23:05:11 +00002023 )
2024
2025
2026def CheckPosixThreading(filename, clean_lines, linenum, error):
2027 """Checks for calls to thread-unsafe functions.
2028
2029 Much code has been originally written without consideration of
2030 multi-threading. Also, engineers are relying on their old experience;
2031 they have learned posix before threading extensions were added. These
2032 tests guide the engineers to use thread-safe functions (when using
2033 posix directly).
2034
2035 Args:
2036 filename: The name of the current file.
2037 clean_lines: A CleansedLines instance containing the file.
2038 linenum: The number of the line to check.
2039 error: The function to call with any errors found.
2040 """
2041 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00002042 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
2043 # Additional pattern matching check to confirm that this is the
2044 # function we are looking for
2045 if Search(pattern, line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002046 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.com02af6282014-06-04 18:53:25 +00002047 'Consider using ' + multithread_safe_func +
2048 '...) instead of ' + single_thread_func +
erg@google.com4e00b9a2009-01-12 23:05:11 +00002049 '...) for improved thread safety.')
2050
2051
erg@google.com2aa59982013-10-28 19:09:25 +00002052def CheckVlogArguments(filename, clean_lines, linenum, error):
2053 """Checks that VLOG() is only used for defining a logging level.
2054
2055 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
2056 VLOG(FATAL) are not.
2057
2058 Args:
2059 filename: The name of the current file.
2060 clean_lines: A CleansedLines instance containing the file.
2061 linenum: The number of the line to check.
2062 error: The function to call with any errors found.
2063 """
2064 line = clean_lines.elided[linenum]
2065 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
2066 error(filename, linenum, 'runtime/vlog', 5,
2067 'VLOG() should be used with numeric verbosity level. '
2068 'Use LOG() if you want symbolic severity levels.')
2069
erg@google.coma868d2d2009-10-09 21:18:45 +00002070# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00002071# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00002072_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00002073 r'^\s*\*\w+(\+\+|--);')
2074
2075
2076def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00002077 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00002078
2079 For example following function:
2080 void increment_counter(int* count) {
2081 *count++;
2082 }
2083 is invalid, because it effectively does count++, moving pointer, and should
2084 be replaced with ++*count, (*count)++ or *count += 1.
2085
2086 Args:
2087 filename: The name of the current file.
2088 clean_lines: A CleansedLines instance containing the file.
2089 linenum: The number of the line to check.
2090 error: The function to call with any errors found.
2091 """
2092 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00002093 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00002094 error(filename, linenum, 'runtime/invalid_increment', 5,
2095 'Changing pointer instead of value (or unused value of operator*).')
2096
2097
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002098def IsMacroDefinition(clean_lines, linenum):
2099 if Search(r'^#define', clean_lines[linenum]):
2100 return True
2101
2102 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
2103 return True
2104
2105 return False
2106
2107
2108def IsForwardClassDeclaration(clean_lines, linenum):
2109 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
2110
2111
erg@google.comd350fe52013-01-14 17:51:48 +00002112class _BlockInfo(object):
2113 """Stores information about a generic block of code."""
2114
Alex Vakulenko01e47232016-05-06 13:54:15 -07002115 def __init__(self, linenum, seen_open_brace):
2116 self.starting_linenum = linenum
erg@google.comd350fe52013-01-14 17:51:48 +00002117 self.seen_open_brace = seen_open_brace
2118 self.open_parentheses = 0
2119 self.inline_asm = _NO_ASM
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002120 self.check_namespace_indentation = False
erg@google.comd350fe52013-01-14 17:51:48 +00002121
2122 def CheckBegin(self, filename, clean_lines, linenum, error):
2123 """Run checks that applies to text up to the opening brace.
2124
2125 This is mostly for checking the text after the class identifier
2126 and the "{", usually where the base class is specified. For other
2127 blocks, there isn't much to check, so we always pass.
2128
2129 Args:
2130 filename: The name of the current file.
2131 clean_lines: A CleansedLines instance containing the file.
2132 linenum: The number of the line to check.
2133 error: The function to call with any errors found.
2134 """
2135 pass
2136
2137 def CheckEnd(self, filename, clean_lines, linenum, error):
2138 """Run checks that applies to text after the closing brace.
2139
2140 This is mostly used for checking end of namespace comments.
2141
2142 Args:
2143 filename: The name of the current file.
2144 clean_lines: A CleansedLines instance containing the file.
2145 linenum: The number of the line to check.
2146 error: The function to call with any errors found.
2147 """
2148 pass
2149
avakulenko@google.com02af6282014-06-04 18:53:25 +00002150 def IsBlockInfo(self):
2151 """Returns true if this block is a _BlockInfo.
2152
2153 This is convenient for verifying that an object is an instance of
2154 a _BlockInfo, but not an instance of any of the derived classes.
2155
2156 Returns:
2157 True for this class, False for derived classes.
2158 """
2159 return self.__class__ == _BlockInfo
2160
2161
2162class _ExternCInfo(_BlockInfo):
2163 """Stores information about an 'extern "C"' block."""
2164
Alex Vakulenko01e47232016-05-06 13:54:15 -07002165 def __init__(self, linenum):
2166 _BlockInfo.__init__(self, linenum, True)
avakulenko@google.com02af6282014-06-04 18:53:25 +00002167
erg@google.comd350fe52013-01-14 17:51:48 +00002168
2169class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002170 """Stores information about a class."""
2171
erg@google.comd350fe52013-01-14 17:51:48 +00002172 def __init__(self, name, class_or_struct, clean_lines, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002173 _BlockInfo.__init__(self, linenum, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002174 self.name = name
erg@google.com4e00b9a2009-01-12 23:05:11 +00002175 self.is_derived = False
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002176 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002177 if class_or_struct == 'struct':
2178 self.access = 'public'
erg@google.comfd5da632013-10-25 17:39:45 +00002179 self.is_struct = True
erg@google.comd350fe52013-01-14 17:51:48 +00002180 else:
2181 self.access = 'private'
erg@google.comfd5da632013-10-25 17:39:45 +00002182 self.is_struct = False
2183
2184 # Remember initial indentation level for this class. Using raw_lines here
erg@google.comc6671232013-10-25 21:44:03 +00002185 # instead of elided to account for leading comments.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002186 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00002187
erg@google.com8a95ecc2011-09-08 00:45:54 +00002188 # Try to find the end of the class. This will be confused by things like:
2189 # class A {
2190 # } *x = { ...
2191 #
2192 # But it's still good enough for CheckSectionSpacing.
2193 self.last_line = 0
2194 depth = 0
2195 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00002196 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00002197 depth += line.count('{') - line.count('}')
2198 if not depth:
2199 self.last_line = i
2200 break
2201
erg@google.comd350fe52013-01-14 17:51:48 +00002202 def CheckBegin(self, filename, clean_lines, linenum, error):
2203 # Look for a bare ':'
2204 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2205 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00002206
erg@google.comfd5da632013-10-25 17:39:45 +00002207 def CheckEnd(self, filename, clean_lines, linenum, error):
avakulenko@google.com554223d2014-12-04 22:00:20 +00002208 # If there is a DISALLOW macro, it should appear near the end of
2209 # the class.
2210 seen_last_thing_in_class = False
2211 for i in xrange(linenum - 1, self.starting_linenum, -1):
2212 match = Search(
2213 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
2214 self.name + r'\)',
2215 clean_lines.elided[i])
2216 if match:
2217 if seen_last_thing_in_class:
2218 error(filename, i, 'readability/constructors', 3,
2219 match.group(1) + ' should be the last thing in the class')
2220 break
2221
2222 if not Match(r'^\s*$', clean_lines.elided[i]):
2223 seen_last_thing_in_class = True
2224
erg@google.comfd5da632013-10-25 17:39:45 +00002225 # Check that closing brace is aligned with beginning of the class.
2226 # Only do this if the closing brace is indented by only whitespaces.
2227 # This means we will not check single-line class definitions.
2228 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2229 if indent and len(indent.group(1)) != self.class_indent:
2230 if self.is_struct:
2231 parent = 'struct ' + self.name
2232 else:
2233 parent = 'class ' + self.name
2234 error(filename, linenum, 'whitespace/indent', 3,
2235 'Closing brace should be aligned with beginning of %s' % parent)
2236
erg@google.com4e00b9a2009-01-12 23:05:11 +00002237
erg@google.comd350fe52013-01-14 17:51:48 +00002238class _NamespaceInfo(_BlockInfo):
2239 """Stores information about a namespace."""
2240
2241 def __init__(self, name, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002242 _BlockInfo.__init__(self, linenum, False)
erg@google.comd350fe52013-01-14 17:51:48 +00002243 self.name = name or ''
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002244 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002245
2246 def CheckEnd(self, filename, clean_lines, linenum, error):
2247 """Check end of namespace comments."""
2248 line = clean_lines.raw_lines[linenum]
2249
2250 # Check how many lines is enclosed in this namespace. Don't issue
2251 # warning for missing namespace comments if there aren't enough
2252 # lines. However, do apply checks if there is already an end of
2253 # namespace comment and it's incorrect.
2254 #
2255 # TODO(unknown): We always want to check end of namespace comments
2256 # if a namespace is large, but sometimes we also want to apply the
2257 # check if a short namespace contained nontrivial things (something
2258 # other than forward declarations). There is currently no logic on
2259 # deciding what these nontrivial things are, so this check is
2260 # triggered by namespace size only, which works most of the time.
2261 if (linenum - self.starting_linenum < 10
Alex Vakulenko01e47232016-05-06 13:54:15 -07002262 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)):
erg@google.comd350fe52013-01-14 17:51:48 +00002263 return
2264
2265 # Look for matching comment at end of namespace.
2266 #
2267 # Note that we accept C style "/* */" comments for terminating
2268 # namespaces, so that code that terminate namespaces inside
erg@google.comc6671232013-10-25 21:44:03 +00002269 # preprocessor macros can be cpplint clean.
erg@google.comd350fe52013-01-14 17:51:48 +00002270 #
2271 # We also accept stuff like "// end of namespace <name>." with the
2272 # period at the end.
2273 #
2274 # Besides these, we don't accept anything else, otherwise we might
2275 # get false negatives when existing comment is a substring of the
erg@google.comc6671232013-10-25 21:44:03 +00002276 # expected namespace.
erg@google.comd350fe52013-01-14 17:51:48 +00002277 if self.name:
2278 # Named namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002279 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
2280 re.escape(self.name) + r'[\*/\.\\\s]*$'),
erg@google.comd350fe52013-01-14 17:51:48 +00002281 line):
2282 error(filename, linenum, 'readability/namespace', 5,
2283 'Namespace should be terminated with "// namespace %s"' %
2284 self.name)
2285 else:
2286 # Anonymous namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002287 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002288 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2289 # mention "// anonymous namespace" as an acceptable form
Alex Vakulenko01e47232016-05-06 13:54:15 -07002290 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002291 error(filename, linenum, 'readability/namespace', 5,
2292 'Anonymous namespace should be terminated with "// namespace"'
2293 ' or "// anonymous namespace"')
2294 else:
2295 error(filename, linenum, 'readability/namespace', 5,
2296 'Anonymous namespace should be terminated with "// namespace"')
erg@google.comd350fe52013-01-14 17:51:48 +00002297
2298
2299class _PreprocessorInfo(object):
2300 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2301
2302 def __init__(self, stack_before_if):
2303 # The entire nesting stack before #if
2304 self.stack_before_if = stack_before_if
2305
2306 # The entire nesting stack up to #else
2307 self.stack_before_else = []
2308
2309 # Whether we have already seen #else or #elif
2310 self.seen_else = False
2311
2312
avakulenko@google.com02af6282014-06-04 18:53:25 +00002313class NestingState(object):
erg@google.comd350fe52013-01-14 17:51:48 +00002314 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00002315
2316 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00002317 # Stack for tracking all braces. An object is pushed whenever we
2318 # see a "{", and popped when we see a "}". Only 3 types of
2319 # objects are possible:
2320 # - _ClassInfo: a class or struct.
2321 # - _NamespaceInfo: a namespace.
2322 # - _BlockInfo: some other type of block.
2323 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00002324
avakulenko@google.com02af6282014-06-04 18:53:25 +00002325 # Top of the previous stack before each Update().
2326 #
2327 # Because the nesting_stack is updated at the end of each line, we
2328 # had to do some convoluted checks to find out what is the current
2329 # scope at the beginning of the line. This check is simplified by
2330 # saving the previous top of nesting stack.
2331 #
2332 # We could save the full stack, but we only need the top. Copying
2333 # the full nesting stack would slow down cpplint by ~10%.
2334 self.previous_stack_top = []
2335
erg@google.comd350fe52013-01-14 17:51:48 +00002336 # Stack of _PreprocessorInfo objects.
2337 self.pp_stack = []
2338
2339 def SeenOpenBrace(self):
2340 """Check if we have seen the opening brace for the innermost block.
2341
2342 Returns:
2343 True if we have seen the opening brace, False if the innermost
2344 block is still expecting an opening brace.
2345 """
2346 return (not self.stack) or self.stack[-1].seen_open_brace
2347
2348 def InNamespaceBody(self):
2349 """Check if we are currently one level inside a namespace body.
2350
2351 Returns:
2352 True if top of the stack is a namespace block, False otherwise.
2353 """
2354 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2355
avakulenko@google.com02af6282014-06-04 18:53:25 +00002356 def InExternC(self):
2357 """Check if we are currently one level inside an 'extern "C"' block.
2358
2359 Returns:
2360 True if top of the stack is an extern block, False otherwise.
2361 """
2362 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2363
2364 def InClassDeclaration(self):
2365 """Check if we are currently one level inside a class or struct declaration.
2366
2367 Returns:
2368 True if top of the stack is a class/struct, False otherwise.
2369 """
2370 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2371
2372 def InAsmBlock(self):
2373 """Check if we are currently one level inside an inline ASM block.
2374
2375 Returns:
2376 True if the top of the stack is a block containing inline ASM.
2377 """
2378 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2379
2380 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2381 """Check if current position is inside template argument list.
2382
2383 Args:
2384 clean_lines: A CleansedLines instance containing the file.
2385 linenum: The number of the line to check.
2386 pos: position just after the suspected template argument.
2387 Returns:
2388 True if (linenum, pos) is inside template arguments.
2389 """
2390 while linenum < clean_lines.NumLines():
2391 # Find the earliest character that might indicate a template argument
2392 line = clean_lines.elided[linenum]
2393 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2394 if not match:
2395 linenum += 1
2396 pos = 0
2397 continue
2398 token = match.group(1)
2399 pos += len(match.group(0))
2400
2401 # These things do not look like template argument list:
2402 # class Suspect {
2403 # class Suspect x; }
2404 if token in ('{', '}', ';'): return False
2405
2406 # These things look like template argument list:
2407 # template <class Suspect>
2408 # template <class Suspect = default_value>
2409 # template <class Suspect[]>
2410 # template <class Suspect...>
2411 if token in ('>', '=', '[', ']', '.'): return True
2412
2413 # Check if token is an unmatched '<'.
2414 # If not, move on to the next character.
2415 if token != '<':
2416 pos += 1
2417 if pos >= len(line):
2418 linenum += 1
2419 pos = 0
2420 continue
2421
2422 # We can't be sure if we just find a single '<', and need to
2423 # find the matching '>'.
2424 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2425 if end_pos < 0:
2426 # Not sure if template argument list or syntax error in file
2427 return False
2428 linenum = end_line
2429 pos = end_pos
2430 return False
2431
erg@google.comd350fe52013-01-14 17:51:48 +00002432 def UpdatePreprocessor(self, line):
2433 """Update preprocessor stack.
2434
2435 We need to handle preprocessors due to classes like this:
2436 #ifdef SWIG
2437 struct ResultDetailsPageElementExtensionPoint {
2438 #else
2439 struct ResultDetailsPageElementExtensionPoint : public Extension {
2440 #endif
erg@google.comd350fe52013-01-14 17:51:48 +00002441
2442 We make the following assumptions (good enough for most files):
2443 - Preprocessor condition evaluates to true from #if up to first
2444 #else/#elif/#endif.
2445
2446 - Preprocessor condition evaluates to false from #else/#elif up
2447 to #endif. We still perform lint checks on these lines, but
2448 these do not affect nesting stack.
2449
2450 Args:
2451 line: current line to check.
2452 """
2453 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2454 # Beginning of #if block, save the nesting stack here. The saved
2455 # stack will allow us to restore the parsing state in the #else case.
2456 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2457 elif Match(r'^\s*#\s*(else|elif)\b', line):
2458 # Beginning of #else block
2459 if self.pp_stack:
2460 if not self.pp_stack[-1].seen_else:
2461 # This is the first #else or #elif block. Remember the
2462 # whole nesting stack up to this point. This is what we
2463 # keep after the #endif.
2464 self.pp_stack[-1].seen_else = True
2465 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2466
2467 # Restore the stack to how it was before the #if
2468 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2469 else:
2470 # TODO(unknown): unexpected #else, issue warning?
2471 pass
2472 elif Match(r'^\s*#\s*endif\b', line):
2473 # End of #if or #else blocks.
2474 if self.pp_stack:
2475 # If we saw an #else, we will need to restore the nesting
2476 # stack to its former state before the #else, otherwise we
2477 # will just continue from where we left off.
2478 if self.pp_stack[-1].seen_else:
2479 # Here we can just use a shallow copy since we are the last
2480 # reference to it.
2481 self.stack = self.pp_stack[-1].stack_before_else
2482 # Drop the corresponding #if
2483 self.pp_stack.pop()
2484 else:
2485 # TODO(unknown): unexpected #endif, issue warning?
2486 pass
2487
avakulenko@google.com02af6282014-06-04 18:53:25 +00002488 # TODO(unknown): Update() is too long, but we will refactor later.
erg@google.comd350fe52013-01-14 17:51:48 +00002489 def Update(self, filename, clean_lines, linenum, error):
2490 """Update nesting state with current line.
2491
2492 Args:
2493 filename: The name of the current file.
2494 clean_lines: A CleansedLines instance containing the file.
2495 linenum: The number of the line to check.
2496 error: The function to call with any errors found.
2497 """
2498 line = clean_lines.elided[linenum]
2499
avakulenko@google.com02af6282014-06-04 18:53:25 +00002500 # Remember top of the previous nesting stack.
2501 #
2502 # The stack is always pushed/popped and not modified in place, so
2503 # we can just do a shallow copy instead of copy.deepcopy. Using
2504 # deepcopy would slow down cpplint by ~28%.
2505 if self.stack:
2506 self.previous_stack_top = self.stack[-1]
2507 else:
2508 self.previous_stack_top = None
2509
2510 # Update pp_stack
erg@google.comd350fe52013-01-14 17:51:48 +00002511 self.UpdatePreprocessor(line)
2512
2513 # Count parentheses. This is to avoid adding struct arguments to
2514 # the nesting stack.
2515 if self.stack:
2516 inner_block = self.stack[-1]
2517 depth_change = line.count('(') - line.count(')')
2518 inner_block.open_parentheses += depth_change
2519
2520 # Also check if we are starting or ending an inline assembly block.
2521 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2522 if (depth_change != 0 and
2523 inner_block.open_parentheses == 1 and
2524 _MATCH_ASM.match(line)):
2525 # Enter assembly block
2526 inner_block.inline_asm = _INSIDE_ASM
2527 else:
2528 # Not entering assembly block. If previous line was _END_ASM,
2529 # we will now shift to _NO_ASM state.
2530 inner_block.inline_asm = _NO_ASM
2531 elif (inner_block.inline_asm == _INSIDE_ASM and
2532 inner_block.open_parentheses == 0):
2533 # Exit assembly block
2534 inner_block.inline_asm = _END_ASM
2535
2536 # Consume namespace declaration at the beginning of the line. Do
2537 # this in a loop so that we catch same line declarations like this:
2538 # namespace proto2 { namespace bridge { class MessageSet; } }
2539 while True:
2540 # Match start of namespace. The "\b\s*" below catches namespace
2541 # declarations even if it weren't followed by a whitespace, this
2542 # is so that we don't confuse our namespace checker. The
2543 # missing spaces will be flagged by CheckSpacing.
2544 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2545 if not namespace_decl_match:
2546 break
2547
2548 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2549 self.stack.append(new_namespace)
2550
2551 line = namespace_decl_match.group(2)
2552 if line.find('{') != -1:
2553 new_namespace.seen_open_brace = True
2554 line = line[line.find('{') + 1:]
2555
2556 # Look for a class declaration in whatever is left of the line
2557 # after parsing namespaces. The regexp accounts for decorated classes
2558 # such as in:
2559 # class LOCKABLE API Object {
2560 # };
erg@google.comd350fe52013-01-14 17:51:48 +00002561 class_decl_match = Match(
avakulenko@google.com02af6282014-06-04 18:53:25 +00002562 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2563 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2564 r'(.*)$', line)
erg@google.comd350fe52013-01-14 17:51:48 +00002565 if (class_decl_match and
2566 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002567 # We do not want to accept classes that are actually template arguments:
2568 # template <class Ignore1,
2569 # class Ignore2 = Default<Args>,
2570 # template <Args> class Ignore3>
2571 # void Function() {};
2572 #
2573 # To avoid template argument cases, we scan forward and look for
2574 # an unmatched '>'. If we see one, assume we are inside a
2575 # template argument list.
2576 end_declaration = len(class_decl_match.group(1))
2577 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2578 self.stack.append(_ClassInfo(
2579 class_decl_match.group(3), class_decl_match.group(2),
2580 clean_lines, linenum))
2581 line = class_decl_match.group(4)
erg@google.comd350fe52013-01-14 17:51:48 +00002582
2583 # If we have not yet seen the opening brace for the innermost block,
2584 # run checks here.
2585 if not self.SeenOpenBrace():
2586 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2587
2588 # Update access control if we are inside a class/struct
2589 if self.stack and isinstance(self.stack[-1], _ClassInfo):
erg@google.comfd5da632013-10-25 17:39:45 +00002590 classinfo = self.stack[-1]
2591 access_match = Match(
2592 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2593 r':(?:[^:]|$)',
2594 line)
erg@google.comd350fe52013-01-14 17:51:48 +00002595 if access_match:
erg@google.comfd5da632013-10-25 17:39:45 +00002596 classinfo.access = access_match.group(2)
2597
2598 # Check that access keywords are indented +1 space. Skip this
erg@google.comc6671232013-10-25 21:44:03 +00002599 # check if the keywords are not preceded by whitespaces.
erg@google.comfd5da632013-10-25 17:39:45 +00002600 indent = access_match.group(1)
2601 if (len(indent) != classinfo.class_indent + 1 and
2602 Match(r'^\s*$', indent)):
2603 if classinfo.is_struct:
2604 parent = 'struct ' + classinfo.name
2605 else:
2606 parent = 'class ' + classinfo.name
2607 slots = ''
2608 if access_match.group(3):
2609 slots = access_match.group(3)
2610 error(filename, linenum, 'whitespace/indent', 3,
2611 '%s%s: should be indented +1 space inside %s' % (
2612 access_match.group(2), slots, parent))
erg@google.comd350fe52013-01-14 17:51:48 +00002613
2614 # Consume braces or semicolons from what's left of the line
2615 while True:
2616 # Match first brace, semicolon, or closed parenthesis.
2617 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2618 if not matched:
2619 break
2620
2621 token = matched.group(1)
2622 if token == '{':
2623 # If namespace or class hasn't seen a opening brace yet, mark
2624 # namespace/class head as complete. Push a new block onto the
2625 # stack otherwise.
2626 if not self.SeenOpenBrace():
2627 self.stack[-1].seen_open_brace = True
avakulenko@google.com02af6282014-06-04 18:53:25 +00002628 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002629 self.stack.append(_ExternCInfo(linenum))
erg@google.comd350fe52013-01-14 17:51:48 +00002630 else:
Alex Vakulenko01e47232016-05-06 13:54:15 -07002631 self.stack.append(_BlockInfo(linenum, True))
erg@google.comd350fe52013-01-14 17:51:48 +00002632 if _MATCH_ASM.match(line):
2633 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.com02af6282014-06-04 18:53:25 +00002634
erg@google.comd350fe52013-01-14 17:51:48 +00002635 elif token == ';' or token == ')':
2636 # If we haven't seen an opening brace yet, but we already saw
2637 # a semicolon, this is probably a forward declaration. Pop
2638 # the stack for these.
2639 #
2640 # Similarly, if we haven't seen an opening brace yet, but we
2641 # already saw a closing parenthesis, then these are probably
2642 # function arguments with extra "class" or "struct" keywords.
2643 # Also pop these stack for these.
2644 if not self.SeenOpenBrace():
2645 self.stack.pop()
2646 else: # token == '}'
2647 # Perform end of block checks and pop the stack.
2648 if self.stack:
2649 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2650 self.stack.pop()
2651 line = matched.group(2)
2652
2653 def InnermostClass(self):
2654 """Get class info on the top of the stack.
2655
2656 Returns:
2657 A _ClassInfo object if we are inside a class, or None otherwise.
2658 """
2659 for i in range(len(self.stack), 0, -1):
2660 classinfo = self.stack[i - 1]
2661 if isinstance(classinfo, _ClassInfo):
2662 return classinfo
2663 return None
2664
erg@google.com2aa59982013-10-28 19:09:25 +00002665 def CheckCompletedBlocks(self, filename, error):
2666 """Checks that all classes and namespaces have been completely parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002667
2668 Call this when all lines in a file have been processed.
2669 Args:
2670 filename: The name of the current file.
2671 error: The function to call with any errors found.
2672 """
erg@google.comd350fe52013-01-14 17:51:48 +00002673 # Note: This test can result in false positives if #ifdef constructs
2674 # get in the way of brace matching. See the testBuildClass test in
2675 # cpplint_unittest.py for an example of this.
2676 for obj in self.stack:
2677 if isinstance(obj, _ClassInfo):
2678 error(filename, obj.starting_linenum, 'build/class', 5,
2679 'Failed to find complete declaration of class %s' %
2680 obj.name)
erg@google.com2aa59982013-10-28 19:09:25 +00002681 elif isinstance(obj, _NamespaceInfo):
2682 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2683 'Failed to find complete declaration of namespace %s' %
2684 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002685
2686
2687def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00002688 nesting_state, error):
erg@google.com2aa59982013-10-28 19:09:25 +00002689 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002690
2691 Complain about several constructs which gcc-2 accepts, but which are
2692 not standard C++. Warning about these in lint is one way to ease the
2693 transition to new compilers.
2694 - put storage class first (e.g. "static const" instead of "const static").
2695 - "%lld" instead of %qd" in printf-type functions.
2696 - "%1$d" is non-standard in printf-type functions.
2697 - "\%" is an undefined character escape sequence.
2698 - text after #endif is not allowed.
2699 - invalid inner-style forward declaration.
2700 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002701
erg@google.coma868d2d2009-10-09 21:18:45 +00002702 Additionally, check for constructor/destructor style violations and reference
2703 members, as it is very convenient to do so while checking for
2704 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002705
2706 Args:
2707 filename: The name of the current file.
2708 clean_lines: A CleansedLines instance containing the file.
2709 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002710 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00002711 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002712 error: A callable to which errors are reported, which takes 4 arguments:
2713 filename, line number, error level, and message
2714 """
2715
Alexandre Ramesf5589442016-09-21 11:03:34 +01002716 line = clean_lines.lines_without_raw_strings[linenum]
2717 if Match(r'\s*#\s*endif\s*([^/\s]|/[^/]|$)', line):
2718 error(filename, linenum, 'build/endif_comment', 5,
2719 'Uncommented text after #endif is non-standard. Use a comment.')
2720
erg@google.com4e00b9a2009-01-12 23:05:11 +00002721 # Remove comments from the line, but leave in strings for now.
2722 line = clean_lines.lines[linenum]
2723
2724 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2725 error(filename, linenum, 'runtime/printf_format', 3,
2726 '%q in format strings is deprecated. Use %ll instead.')
2727
2728 if Search(r'printf\s*\(.*".*%\d+\$', line):
2729 error(filename, linenum, 'runtime/printf_format', 2,
2730 '%N$ formats are unconventional. Try rewriting to avoid them.')
2731
2732 # Remove escaped backslashes before looking for undefined escapes.
2733 line = line.replace('\\\\', '')
2734
2735 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2736 error(filename, linenum, 'build/printf_format', 3,
2737 '%, [, (, and { are undefined character escapes. Unescape them.')
2738
2739 # For the rest, work with both comments and strings removed.
2740 line = clean_lines.elided[linenum]
2741
2742 if Search(r'\b(const|volatile|void|char|short|int|long'
2743 r'|float|double|signed|unsigned'
2744 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00002745 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00002746 line):
2747 error(filename, linenum, 'build/storage_class', 5,
Alex Vakulenko01e47232016-05-06 13:54:15 -07002748 'Storage-class specifier (static, extern, typedef, etc) should be '
2749 'at the beginning of the declaration.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002750
erg@google.com4e00b9a2009-01-12 23:05:11 +00002751 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2752 error(filename, linenum, 'build/forward_decl', 5,
2753 'Inner-style forward declarations are invalid. Remove this line.')
2754
2755 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2756 line):
2757 error(filename, linenum, 'build/deprecated', 3,
2758 '>? and <? (max and min) operators are non-standard and deprecated.')
2759
erg@google.coma868d2d2009-10-09 21:18:45 +00002760 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2761 # TODO(unknown): Could it be expanded safely to arbitrary references,
2762 # without triggering too many false positives? The first
2763 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2764 # the restriction.
2765 # Here's the original regexp, for the reference:
2766 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2767 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2768 error(filename, linenum, 'runtime/member_string_references', 2,
2769 'const string& members are dangerous. It is much better to use '
2770 'alternatives, such as pointers or simple constants.')
2771
erg@google.comd350fe52013-01-14 17:51:48 +00002772 # Everything else in this function operates on class declarations.
2773 # Return early if the top of the nesting stack is not a class, or if
2774 # the class head is not completed yet.
2775 classinfo = nesting_state.InnermostClass()
2776 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002777 return
2778
erg@google.com4e00b9a2009-01-12 23:05:11 +00002779 # The class may have been declared with namespace or classname qualifiers.
2780 # The constructor and destructor will not have those qualifiers.
2781 base_classname = classinfo.name.split('::')[-1]
2782
2783 # Look for single-argument constructors that aren't marked explicit.
Alex Vakulenko01e47232016-05-06 13:54:15 -07002784 # Technically a valid construct, but against style.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002785 explicit_constructor_match = Match(
2786 r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*'
2787 r'\(((?:[^()]|\([^()]*\))*)\)'
2788 % re.escape(base_classname),
2789 line)
2790
2791 if explicit_constructor_match:
2792 is_marked_explicit = explicit_constructor_match.group(1)
2793
2794 if not explicit_constructor_match.group(2):
2795 constructor_args = []
2796 else:
2797 constructor_args = explicit_constructor_match.group(2).split(',')
2798
2799 # collapse arguments so that commas in template parameter lists and function
2800 # argument parameter lists don't split arguments in two
2801 i = 0
2802 while i < len(constructor_args):
2803 constructor_arg = constructor_args[i]
2804 while (constructor_arg.count('<') > constructor_arg.count('>') or
2805 constructor_arg.count('(') > constructor_arg.count(')')):
2806 constructor_arg += ',' + constructor_args[i + 1]
2807 del constructor_args[i + 1]
2808 constructor_args[i] = constructor_arg
2809 i += 1
2810
2811 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2812 noarg_constructor = (not constructor_args or # empty arg list
2813 # 'void' arg specifier
2814 (len(constructor_args) == 1 and
2815 constructor_args[0].strip() == 'void'))
2816 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2817 not noarg_constructor) or
2818 # all but at most one arg defaulted
2819 (len(constructor_args) >= 1 and
2820 not noarg_constructor and
2821 len(defaulted_args) >= len(constructor_args) - 1))
2822 initializer_list_constructor = bool(
2823 onearg_constructor and
2824 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2825 copy_constructor = bool(
2826 onearg_constructor and
2827 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2828 % re.escape(base_classname), constructor_args[0].strip()))
2829
2830 if (not is_marked_explicit and
2831 onearg_constructor and
2832 not initializer_list_constructor and
2833 not copy_constructor):
2834 if defaulted_args:
2835 error(filename, linenum, 'runtime/explicit', 5,
2836 'Constructors callable with one argument '
2837 'should be marked explicit.')
2838 else:
2839 error(filename, linenum, 'runtime/explicit', 5,
2840 'Single-parameter constructors should be marked explicit.')
2841 elif is_marked_explicit and not onearg_constructor:
2842 if noarg_constructor:
2843 error(filename, linenum, 'runtime/explicit', 5,
2844 'Zero-parameter constructors should not be marked explicit.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002845
erg@google.com4e00b9a2009-01-12 23:05:11 +00002846
avakulenko@google.com02af6282014-06-04 18:53:25 +00002847def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002848 """Checks for the correctness of various spacing around function calls.
2849
2850 Args:
2851 filename: The name of the current file.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002852 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002853 linenum: The number of the line to check.
2854 error: The function to call with any errors found.
2855 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00002856 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002857
2858 # Since function calls often occur inside if/for/while/switch
2859 # expressions - which have their own, more liberal conventions - we
2860 # first see if we should be looking inside such an expression for a
2861 # function call, to which we can apply more strict standards.
2862 fncall = line # if there's no control flow construct, look at whole line
2863 for pattern in (r'\bif\s*\((.*)\)\s*{',
2864 r'\bfor\s*\((.*)\)\s*{',
2865 r'\bwhile\s*\((.*)\)\s*[{;]',
2866 r'\bswitch\s*\((.*)\)\s*{'):
2867 match = Search(pattern, line)
2868 if match:
2869 fncall = match.group(1) # look inside the parens for function calls
2870 break
2871
2872 # Except in if/for/while/switch, there should never be space
2873 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2874 # for nested parens ( (a+b) + c ). Likewise, there should never be
2875 # a space before a ( when it's a function argument. I assume it's a
2876 # function argument when the char before the whitespace is legal in
2877 # a function name (alnum + _) and we're not starting a macro. Also ignore
2878 # pointers and references to arrays and functions coz they're too tricky:
2879 # we use a very simple way to recognize these:
2880 # " (something)(maybe-something)" or
2881 # " (something)(maybe-something," or
2882 # " (something)[something]"
2883 # Note that we assume the contents of [] to be short enough that
2884 # they'll never need to wrap.
2885 if ( # Ignore control structures.
erg@google.com2aa59982013-10-28 19:09:25 +00002886 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
erg@google.comc6671232013-10-25 21:44:03 +00002887 fncall) and
erg@google.com4e00b9a2009-01-12 23:05:11 +00002888 # Ignore pointers/references to functions.
2889 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2890 # Ignore pointers/references to arrays.
2891 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00002892 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00002893 error(filename, linenum, 'whitespace/parens', 4,
2894 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00002895 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002896 error(filename, linenum, 'whitespace/parens', 2,
2897 'Extra space after (')
2898 if (Search(r'\w\s+\(', fncall) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07002899 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 +00002900 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
avakulenko@google.com554223d2014-12-04 22:00:20 +00002901 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and
2902 not Search(r'\bcase\s+\(', fncall)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002903 # TODO(unknown): Space after an operator function seem to be a common
2904 # error, silence those for now by restricting them to highest verbosity.
2905 if Search(r'\boperator_*\b', line):
2906 error(filename, linenum, 'whitespace/parens', 0,
2907 'Extra space before ( in function call')
2908 else:
2909 error(filename, linenum, 'whitespace/parens', 4,
2910 'Extra space before ( in function call')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002911 # If the ) is followed only by a newline or a { + newline, assume it's
2912 # part of a control statement (if/while/etc), and don't complain
2913 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002914 # If the closing parenthesis is preceded by only whitespaces,
2915 # try to give a more descriptive error message.
2916 if Search(r'^\s+\)', fncall):
2917 error(filename, linenum, 'whitespace/parens', 2,
2918 'Closing ) should be moved to the previous line')
2919 else:
2920 error(filename, linenum, 'whitespace/parens', 2,
2921 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002922
2923
2924def IsBlankLine(line):
2925 """Returns true if the given line is blank.
2926
2927 We consider a line to be blank if the line is empty or consists of
2928 only white spaces.
2929
2930 Args:
2931 line: A line of a string.
2932
2933 Returns:
2934 True, if the given line is blank.
2935 """
2936 return not line or line.isspace()
2937
2938
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002939def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
2940 error):
2941 is_namespace_indent_item = (
2942 len(nesting_state.stack) > 1 and
2943 nesting_state.stack[-1].check_namespace_indentation and
2944 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
2945 nesting_state.previous_stack_top == nesting_state.stack[-2])
2946
2947 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
2948 clean_lines.elided, line):
2949 CheckItemIndentationInNamespace(filename, clean_lines.elided,
2950 line, error)
2951
2952
erg@google.com4e00b9a2009-01-12 23:05:11 +00002953def CheckForFunctionLengths(filename, clean_lines, linenum,
2954 function_state, error):
2955 """Reports for long function bodies.
2956
2957 For an overview why this is done, see:
Ackermann Yuriy79692902016-04-01 21:41:34 +13002958 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
erg@google.com4e00b9a2009-01-12 23:05:11 +00002959
2960 Uses a simplistic algorithm assuming other style guidelines
2961 (especially spacing) are followed.
2962 Only checks unindented functions, so class members are unchecked.
2963 Trivial bodies are unchecked, so constructors with huge initializer lists
2964 may be missed.
2965 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00002966 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002967 NOLINT *on the last line of a function* disables this check.
2968
2969 Args:
2970 filename: The name of the current file.
2971 clean_lines: A CleansedLines instance containing the file.
2972 linenum: The number of the line to check.
2973 function_state: Current function name and lines in body so far.
2974 error: The function to call with any errors found.
2975 """
2976 lines = clean_lines.lines
2977 line = lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002978 joined_line = ''
2979
2980 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00002981 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00002982 match_result = Match(regexp, line)
2983 if match_result:
2984 # If the name is all caps and underscores, figure it's a macro and
2985 # ignore it, unless it's TEST or TEST_F.
2986 function_name = match_result.group(1).split()[-1]
2987 if function_name == 'TEST' or function_name == 'TEST_F' or (
2988 not Match(r'[A-Z_]+$', function_name)):
2989 starting_func = True
2990
2991 if starting_func:
2992 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00002993 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002994 start_line = lines[start_linenum]
2995 joined_line += ' ' + start_line.lstrip()
2996 if Search(r'(;|})', start_line): # Declarations and trivial functions
2997 body_found = True
2998 break # ... ignore
2999 elif Search(r'{', start_line):
3000 body_found = True
3001 function = Search(r'((\w|:)*)\(', line).group(1)
3002 if Match(r'TEST', function): # Handle TEST... macros
3003 parameter_regexp = Search(r'(\(.*\))', joined_line)
3004 if parameter_regexp: # Ignore bad syntax
3005 function += parameter_regexp.group(1)
3006 else:
3007 function += '()'
3008 function_state.Begin(function)
3009 break
3010 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00003011 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003012 error(filename, linenum, 'readability/fn_size', 5,
3013 'Lint failed to find start of function body.')
3014 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00003015 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003016 function_state.End()
3017 elif not Match(r'^\s*$', line):
3018 function_state.Count() # Count non-blank/non-comment lines.
3019
3020
3021_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
3022
3023
avakulenko@google.com02af6282014-06-04 18:53:25 +00003024def CheckComment(line, filename, linenum, next_line_start, error):
3025 """Checks for common mistakes in comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003026
3027 Args:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003028 line: The line in question.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003029 filename: The name of the current file.
3030 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003031 next_line_start: The first non-whitespace column of the next line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003032 error: The function to call with any errors found.
3033 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00003034 commentpos = line.find('//')
3035 if commentpos != -1:
3036 # Check if the // may be in quotes. If so, ignore it
Alex Vakulenko01e47232016-05-06 13:54:15 -07003037 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003038 # Allow one space for new scopes, two spaces otherwise:
3039 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
3040 ((commentpos >= 1 and
3041 line[commentpos-1] not in string.whitespace) or
3042 (commentpos >= 2 and
3043 line[commentpos-2] not in string.whitespace))):
3044 error(filename, linenum, 'whitespace/comments', 2,
3045 'At least two spaces is best between code and comments')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003046
avakulenko@google.com02af6282014-06-04 18:53:25 +00003047 # Checks for common mistakes in TODO comments.
3048 comment = line[commentpos:]
3049 match = _RE_PATTERN_TODO.match(comment)
3050 if match:
3051 # One whitespace is correct; zero whitespace is handled elsewhere.
3052 leading_whitespace = match.group(1)
3053 if len(leading_whitespace) > 1:
3054 error(filename, linenum, 'whitespace/todo', 2,
3055 'Too many spaces before TODO')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003056
avakulenko@google.com02af6282014-06-04 18:53:25 +00003057 username = match.group(2)
3058 if not username:
3059 error(filename, linenum, 'readability/todo', 2,
3060 'Missing username in TODO; it should look like '
3061 '"// TODO(my_username): Stuff."')
3062
3063 middle_whitespace = match.group(3)
3064 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
3065 if middle_whitespace != ' ' and middle_whitespace != '':
3066 error(filename, linenum, 'whitespace/todo', 2,
3067 'TODO(my_username) should be followed by a space')
3068
3069 # If the comment contains an alphanumeric character, there
avakulenko@google.com554223d2014-12-04 22:00:20 +00003070 # should be a space somewhere between it and the // unless
3071 # it's a /// or //! Doxygen comment.
3072 if (Match(r'//[^ ]*\w', comment) and
3073 not Match(r'(///|//\!)(\s+|$)', comment)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003074 error(filename, linenum, 'whitespace/comments', 4,
3075 'Should have a space between // and comment')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003076
avakulenko@google.com554223d2014-12-04 22:00:20 +00003077
erg@google.comd350fe52013-01-14 17:51:48 +00003078def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
3079 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003080
erg@google.comd350fe52013-01-14 17:51:48 +00003081 Args:
3082 filename: The name of the current file.
3083 clean_lines: A CleansedLines instance containing the file.
3084 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003085 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003086 the current stack of nested blocks being parsed.
3087 error: The function to call with any errors found.
3088 """
3089 line = clean_lines.elided[linenum] # get rid of comments and strings
3090
3091 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
erg@google.comd350fe52013-01-14 17:51:48 +00003092 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
3093 if not matched:
3094 return
3095 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
3096 if nesting_state.stack[-1].access != 'private':
3097 error(filename, linenum, 'readability/constructors', 3,
3098 '%s must be in the private: section' % matched.group(1))
3099
3100 else:
3101 # Found DISALLOW* macro outside a class declaration, or perhaps it
3102 # was used inside a function when it should have been part of the
3103 # class declaration. We could issue a warning here, but it
3104 # probably resulted in a compiler error already.
3105 pass
3106
3107
erg@google.comd350fe52013-01-14 17:51:48 +00003108def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003109 """Checks for the correctness of various spacing issues in the code.
3110
3111 Things we check for: spaces around operators, spaces after
3112 if/for/while/switch, no spaces around parens in function calls, two
3113 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00003114 line, don't end a function with a blank line, don't add a blank line
3115 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003116
3117 Args:
3118 filename: The name of the current file.
3119 clean_lines: A CleansedLines instance containing the file.
3120 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003121 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003122 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003123 error: The function to call with any errors found.
3124 """
3125
erg@google.com2aa59982013-10-28 19:09:25 +00003126 # Don't use "elided" lines here, otherwise we can't check commented lines.
3127 # Don't want to use "raw" either, because we don't want to check inside C++11
3128 # raw strings,
3129 raw = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00003130 line = raw[linenum]
3131
3132 # Before nixing comments, check if the line is blank for no good
3133 # reason. This includes the first line after a block is opened, and
3134 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00003135 #
3136 # Skip all the blank line checks if we are immediately inside a
3137 # namespace body. In other words, don't issue blank line warnings
3138 # for this block:
3139 # namespace {
3140 #
3141 # }
3142 #
3143 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003144 #
3145 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
3146 # like namespaces.
3147 if (IsBlankLine(line) and
3148 not nesting_state.InNamespaceBody() and
3149 not nesting_state.InExternC()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003150 elided = clean_lines.elided
3151 prev_line = elided[linenum - 1]
3152 prevbrace = prev_line.rfind('{')
3153 # TODO(unknown): Don't complain if line before blank line, and line after,
3154 # both start with alnums and are indented the same amount.
3155 # This ignores whitespace at the start of a namespace block
3156 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00003157 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003158 # OK, we have a blank line at the start of a code block. Before we
3159 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00003160 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00003161 # 4 spaces (because they did not fit in a 80 column line when placed on
3162 # the same line as the function name). We also check for the case where
3163 # the previous line is indented 6 spaces, which may happen when the
3164 # initializers of a constructor do not fit into a 80 column line.
3165 exception = False
3166 if Match(r' {6}\w', prev_line): # Initializer list?
3167 # We are looking for the opening column of initializer list, which
3168 # should be indented 4 spaces to cause 6 space indentation afterwards.
3169 search_position = linenum-2
3170 while (search_position >= 0
3171 and Match(r' {6}\w', elided[search_position])):
3172 search_position -= 1
3173 exception = (search_position >= 0
3174 and elided[search_position][:5] == ' :')
3175 else:
3176 # Search for the function arguments or an initializer list. We use a
3177 # simple heuristic here: If the line is indented 4 spaces; and we have a
3178 # closing paren, without the opening paren, followed by an opening brace
3179 # or colon (for initializer lists) we assume that it is the last line of
3180 # a function header. If we have a colon indented 4 spaces, it is an
3181 # initializer list.
3182 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3183 prev_line)
3184 or Match(r' {4}:', prev_line))
3185
3186 if not exception:
3187 error(filename, linenum, 'whitespace/blank_line', 2,
erg@google.com2aa59982013-10-28 19:09:25 +00003188 'Redundant blank line at the start of a code block '
3189 'should be deleted.')
erg@google.comd350fe52013-01-14 17:51:48 +00003190 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00003191 # chain, like this:
3192 # if (condition1) {
3193 # // Something followed by a blank line
3194 #
3195 # } else if (condition2) {
3196 # // Something else
3197 # }
3198 if linenum + 1 < clean_lines.NumLines():
3199 next_line = raw[linenum + 1]
3200 if (next_line
3201 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003202 and next_line.find('} else ') == -1):
3203 error(filename, linenum, 'whitespace/blank_line', 3,
erg@google.com2aa59982013-10-28 19:09:25 +00003204 'Redundant blank line at the end of a code block '
3205 'should be deleted.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003206
erg@google.com8a95ecc2011-09-08 00:45:54 +00003207 matched = Match(r'\s*(public|protected|private):', prev_line)
3208 if matched:
3209 error(filename, linenum, 'whitespace/blank_line', 3,
3210 'Do not leave a blank line after "%s:"' % matched.group(1))
3211
avakulenko@google.com02af6282014-06-04 18:53:25 +00003212 # Next, check comments
3213 next_line_start = 0
3214 if linenum + 1 < clean_lines.NumLines():
3215 next_line = raw[linenum + 1]
3216 next_line_start = len(next_line) - len(next_line.lstrip())
3217 CheckComment(line, filename, linenum, next_line_start, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003218
avakulenko@google.com02af6282014-06-04 18:53:25 +00003219 # get rid of comments and strings
3220 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003221
avakulenko@google.com02af6282014-06-04 18:53:25 +00003222 # You shouldn't have spaces before your brackets, except maybe after
3223 # 'delete []' or 'return []() {};'
3224 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3225 error(filename, linenum, 'whitespace/braces', 5,
3226 'Extra space before [')
3227
3228 # In range-based for, we wanted spaces before and after the colon, but
3229 # not around "::" tokens that might appear.
3230 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3231 Search(r'for *\(.*[^: ]:[^:]', line)):
3232 error(filename, linenum, 'whitespace/forcolon', 2,
3233 'Missing space around colon in range-based for loop')
3234
3235
3236def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3237 """Checks for horizontal spacing around operators.
3238
3239 Args:
3240 filename: The name of the current file.
3241 clean_lines: A CleansedLines instance containing the file.
3242 linenum: The number of the line to check.
3243 error: The function to call with any errors found.
3244 """
3245 line = clean_lines.elided[linenum]
3246
3247 # Don't try to do spacing checks for operator methods. Do this by
3248 # replacing the troublesome characters with something else,
3249 # preserving column position for all other characters.
3250 #
3251 # The replacement is done repeatedly to avoid false positives from
3252 # operators that call operators.
3253 while True:
3254 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3255 if match:
3256 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3257 else:
3258 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00003259
3260 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3261 # Otherwise not. Note we only check for non-spaces on *both* sides;
3262 # sometimes people put non-spaces on one side when aligning ='s among
3263 # many lines (not that this is behavior that I approve of...)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003264 if ((Search(r'[\w.]=', line) or
3265 Search(r'=[\w.]', line))
3266 and not Search(r'\b(if|while|for) ', line)
3267 # Operators taken from [lex.operators] in C++11 standard.
3268 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3269 and not Search(r'operator=', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003270 error(filename, linenum, 'whitespace/operators', 4,
3271 'Missing spaces around =')
3272
3273 # It's ok not to have spaces around binary operators like + - * /, but if
3274 # there's too little whitespace, we get concerned. It's hard to tell,
3275 # though, so we punt on this one for now. TODO.
3276
3277 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00003278 #
3279 # Check <= and >= first to avoid false positives with < and >, then
3280 # check non-include lines for spacing around < and >.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003281 #
3282 # If the operator is followed by a comma, assume it's be used in a
3283 # macro context and don't do any checks. This avoids false
3284 # positives.
3285 #
Alex Vakulenko01e47232016-05-06 13:54:15 -07003286 # Note that && is not included here. This is because there are too
3287 # many false positives due to RValue references.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003288 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003289 if match:
3290 error(filename, linenum, 'whitespace/operators', 3,
3291 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00003292 elif not Match(r'#.*include', line):
erg@google.comd350fe52013-01-14 17:51:48 +00003293 # Look for < that is not surrounded by spaces. This is only
3294 # triggered if both sides are missing spaces, even though
3295 # technically should should flag if at least one side is missing a
3296 # space. This is done to avoid some false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003297 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3298 if match:
3299 (_, _, end_pos) = CloseExpression(
3300 clean_lines, linenum, len(match.group(1)))
3301 if end_pos <= -1:
3302 error(filename, linenum, 'whitespace/operators', 3,
3303 'Missing spaces around <')
erg@google.comd350fe52013-01-14 17:51:48 +00003304
3305 # Look for > that is not surrounded by spaces. Similar to the
3306 # above, we only trigger if both sides are missing spaces to avoid
3307 # false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003308 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3309 if match:
3310 (_, _, start_pos) = ReverseCloseExpression(
3311 clean_lines, linenum, len(match.group(1)))
3312 if start_pos <= -1:
3313 error(filename, linenum, 'whitespace/operators', 3,
3314 'Missing spaces around >')
3315
3316 # We allow no-spaces around << when used like this: 10<<20, but
3317 # not otherwise (particularly, not when used as streams)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003318 #
avakulenko@google.com02af6282014-06-04 18:53:25 +00003319 # We also allow operators following an opening parenthesis, since
3320 # those tend to be macros that deal with operators.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003321 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003322 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00003323 not (match.group(1) == 'operator' and match.group(2) == ';')):
3324 error(filename, linenum, 'whitespace/operators', 3,
3325 'Missing spaces around <<')
erg@google.comd350fe52013-01-14 17:51:48 +00003326
3327 # We allow no-spaces around >> for almost anything. This is because
3328 # C++11 allows ">>" to close nested templates, which accounts for
3329 # most cases when ">>" is not followed by a space.
3330 #
3331 # We still warn on ">>" followed by alpha character, because that is
3332 # likely due to ">>" being used for right shifts, e.g.:
3333 # value >> alpha
3334 #
3335 # When ">>" is used to close templates, the alphanumeric letter that
3336 # follows would be part of an identifier, and there should still be
3337 # a space separating the template type and the identifier.
3338 # type<type<type>> alpha
3339 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003340 if match:
3341 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00003342 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003343
3344 # There shouldn't be space around unary operators
3345 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3346 if match:
3347 error(filename, linenum, 'whitespace/operators', 4,
3348 'Extra space for operator %s' % match.group(1))
3349
avakulenko@google.com02af6282014-06-04 18:53:25 +00003350
3351def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3352 """Checks for horizontal spacing around parentheses.
3353
3354 Args:
3355 filename: The name of the current file.
3356 clean_lines: A CleansedLines instance containing the file.
3357 linenum: The number of the line to check.
3358 error: The function to call with any errors found.
3359 """
3360 line = clean_lines.elided[linenum]
3361
3362 # No spaces after an if, while, switch, or for
erg@google.com4e00b9a2009-01-12 23:05:11 +00003363 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3364 if match:
3365 error(filename, linenum, 'whitespace/parens', 5,
3366 'Missing space before ( in %s' % match.group(1))
3367
3368 # For if/for/while/switch, the left and right parens should be
3369 # consistent about how many spaces are inside the parens, and
3370 # there should either be zero or one spaces inside the parens.
3371 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00003372 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003373 match = Search(r'\b(if|for|while|switch)\s*'
3374 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3375 line)
3376 if match:
3377 if len(match.group(2)) != len(match.group(4)):
3378 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00003379 len(match.group(2)) == 1 + len(match.group(4)) or
3380 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003381 error(filename, linenum, 'whitespace/parens', 5,
3382 'Mismatching spaces inside () in %s' % match.group(1))
erg@google.comc6671232013-10-25 21:44:03 +00003383 if len(match.group(2)) not in [0, 1]:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003384 error(filename, linenum, 'whitespace/parens', 5,
3385 'Should have zero or one spaces inside ( and ) in %s' %
3386 match.group(1))
3387
avakulenko@google.com02af6282014-06-04 18:53:25 +00003388
3389def CheckCommaSpacing(filename, clean_lines, linenum, error):
3390 """Checks for horizontal spacing near commas and semicolons.
3391
3392 Args:
3393 filename: The name of the current file.
3394 clean_lines: A CleansedLines instance containing the file.
3395 linenum: The number of the line to check.
3396 error: The function to call with any errors found.
3397 """
3398 raw = clean_lines.lines_without_raw_strings
3399 line = clean_lines.elided[linenum]
3400
erg@google.com4e00b9a2009-01-12 23:05:11 +00003401 # You should always have a space after a comma (either as fn arg or operator)
erg@google.comc6671232013-10-25 21:44:03 +00003402 #
3403 # This does not apply when the non-space character following the
3404 # comma is another comma, since the only time when that happens is
3405 # for empty macro arguments.
erg@google.com2aa59982013-10-28 19:09:25 +00003406 #
3407 # We run this check in two passes: first pass on elided lines to
3408 # verify that lines contain missing whitespaces, second pass on raw
3409 # lines to confirm that those missing whitespaces are not due to
3410 # elided comments.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003411 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3412 Search(r',[^,\s]', raw[linenum])):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003413 error(filename, linenum, 'whitespace/comma', 3,
3414 'Missing space after ,')
3415
erg@google.comd7d27472011-09-07 17:36:35 +00003416 # You should always have a space after a semicolon
3417 # except for few corner cases
3418 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3419 # space after ;
3420 if Search(r';[^\s};\\)/]', line):
3421 error(filename, linenum, 'whitespace/semicolon', 3,
3422 'Missing space after ;')
3423
avakulenko@google.com02af6282014-06-04 18:53:25 +00003424
Alex Vakulenko01e47232016-05-06 13:54:15 -07003425def _IsType(clean_lines, nesting_state, expr):
3426 """Check if expression looks like a type name, returns true if so.
3427
3428 Args:
3429 clean_lines: A CleansedLines instance containing the file.
3430 nesting_state: A NestingState instance which maintains information about
3431 the current stack of nested blocks being parsed.
3432 expr: The expression to check.
3433 Returns:
3434 True, if token looks like a type.
3435 """
3436 # Keep only the last token in the expression
3437 last_word = Match(r'^.*(\b\S+)$', expr)
3438 if last_word:
3439 token = last_word.group(1)
3440 else:
3441 token = expr
3442
3443 # Match native types and stdint types
3444 if _TYPES.match(token):
3445 return True
3446
3447 # Try a bit harder to match templated types. Walk up the nesting
3448 # stack until we find something that resembles a typename
3449 # declaration for what we are looking for.
3450 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) +
3451 r'\b')
3452 block_index = len(nesting_state.stack) - 1
3453 while block_index >= 0:
3454 if isinstance(nesting_state.stack[block_index], _NamespaceInfo):
3455 return False
3456
3457 # Found where the opening brace is. We want to scan from this
3458 # line up to the beginning of the function, minus a few lines.
3459 # template <typename Type1, // stop scanning here
3460 # ...>
3461 # class C
3462 # : public ... { // start scanning here
3463 last_line = nesting_state.stack[block_index].starting_linenum
3464
3465 next_block_start = 0
3466 if block_index > 0:
3467 next_block_start = nesting_state.stack[block_index - 1].starting_linenum
3468 first_line = last_line
3469 while first_line >= next_block_start:
3470 if clean_lines.elided[first_line].find('template') >= 0:
3471 break
3472 first_line -= 1
3473 if first_line < next_block_start:
3474 # Didn't find any "template" keyword before reaching the next block,
3475 # there are probably no template things to check for this block
3476 block_index -= 1
3477 continue
3478
3479 # Look for typename in the specified range
3480 for i in xrange(first_line, last_line + 1, 1):
3481 if Search(typename_pattern, clean_lines.elided[i]):
3482 return True
3483 block_index -= 1
3484
3485 return False
3486
3487
3488def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003489 """Checks for horizontal spacing near commas.
3490
3491 Args:
3492 filename: The name of the current file.
3493 clean_lines: A CleansedLines instance containing the file.
3494 linenum: The number of the line to check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003495 nesting_state: A NestingState instance which maintains information about
3496 the current stack of nested blocks being parsed.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003497 error: The function to call with any errors found.
3498 """
3499 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003500
erg@google.com8a95ecc2011-09-08 00:45:54 +00003501 # Except after an opening paren, or after another opening brace (in case of
3502 # an initializer list, for instance), you should have spaces before your
Alex Vakulenko01e47232016-05-06 13:54:15 -07003503 # braces when they are delimiting blocks, classes, namespaces etc.
3504 # And since you should never have braces at the beginning of a line,
3505 # this is an easy test. Except that braces used for initialization don't
3506 # follow the same rule; we often don't want spaces before those.
avakulenko@google.com554223d2014-12-04 22:00:20 +00003507 match = Match(r'^(.*[^ ({>]){', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07003508
erg@google.com2aa59982013-10-28 19:09:25 +00003509 if match:
3510 # Try a bit harder to check for brace initialization. This
3511 # happens in one of the following forms:
3512 # Constructor() : initializer_list_{} { ... }
3513 # Constructor{}.MemberFunction()
3514 # Type variable{};
3515 # FunctionCall(type{}, ...);
3516 # LastArgument(..., type{});
3517 # LOG(INFO) << type{} << " ...";
3518 # map_of_type[{...}] = ...;
avakulenko@google.com02af6282014-06-04 18:53:25 +00003519 # ternary = expr ? new type{} : nullptr;
3520 # OuterTemplate<InnerTemplateConstructor<Type>{}>
erg@google.com2aa59982013-10-28 19:09:25 +00003521 #
3522 # We check for the character following the closing brace, and
3523 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003524 # "{.;,)<>]:".
erg@google.com2aa59982013-10-28 19:09:25 +00003525 #
3526 # To account for nested initializer list, we allow any number of
3527 # closing braces up to "{;,)<". We can't simply silence the
3528 # warning on first sight of closing brace, because that would
3529 # cause false negatives for things that are not initializer lists.
3530 # Silence this: But not this:
3531 # Outer{ if (...) {
3532 # Inner{...} if (...){ // Missing space before {
3533 # }; }
3534 #
3535 # There is a false negative with this approach if people inserted
3536 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3537 # spurious semicolon with a separate check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003538 leading_text = match.group(1)
erg@google.com2aa59982013-10-28 19:09:25 +00003539 (endline, endlinenum, endpos) = CloseExpression(
3540 clean_lines, linenum, len(match.group(1)))
3541 trailing_text = ''
3542 if endpos > -1:
3543 trailing_text = endline[endpos:]
3544 for offset in xrange(endlinenum + 1,
3545 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3546 trailing_text += clean_lines.elided[offset]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003547 # We also suppress warnings for `uint64_t{expression}` etc., as the style
3548 # guide recommends brace initialization for integral types to avoid
3549 # overflow/truncation.
3550 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text)
3551 and not _IsType(clean_lines, nesting_state, leading_text)):
erg@google.com2aa59982013-10-28 19:09:25 +00003552 error(filename, linenum, 'whitespace/braces', 5,
3553 'Missing space before {')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003554
3555 # Make sure '} else {' has spaces.
3556 if Search(r'}else', line):
3557 error(filename, linenum, 'whitespace/braces', 5,
3558 'Missing space before else')
3559
erg@google.com4e00b9a2009-01-12 23:05:11 +00003560 # You shouldn't have a space before a semicolon at the end of the line.
3561 # There's a special case for "for" since the style guide allows space before
3562 # the semicolon there.
3563 if Search(r':\s*;\s*$', line):
3564 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00003565 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003566 elif Search(r'^\s*;\s*$', line):
3567 error(filename, linenum, 'whitespace/semicolon', 5,
3568 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00003569 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003570 elif (Search(r'\s+;\s*$', line) and
3571 not Search(r'\bfor\b', line)):
3572 error(filename, linenum, 'whitespace/semicolon', 5,
3573 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00003574 'statement, use {} instead.')
3575
avakulenko@google.com02af6282014-06-04 18:53:25 +00003576
3577def IsDecltype(clean_lines, linenum, column):
3578 """Check if the token ending on (linenum, column) is decltype().
3579
3580 Args:
3581 clean_lines: A CleansedLines instance containing the file.
3582 linenum: the number of the line to check.
3583 column: end column of the token to check.
3584 Returns:
3585 True if this token is decltype() expression, False otherwise.
3586 """
3587 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3588 if start_col < 0:
3589 return False
3590 if Search(r'\bdecltype\s*$', text[0:start_col]):
3591 return True
3592 return False
3593
3594
erg@google.com8a95ecc2011-09-08 00:45:54 +00003595def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3596 """Checks for additional blank line issues related to sections.
3597
3598 Currently the only thing checked here is blank line before protected/private.
3599
3600 Args:
3601 filename: The name of the current file.
3602 clean_lines: A CleansedLines instance containing the file.
3603 class_info: A _ClassInfo objects.
3604 linenum: The number of the line to check.
3605 error: The function to call with any errors found.
3606 """
3607 # Skip checks if the class is small, where small means 25 lines or less.
3608 # 25 lines seems like a good cutoff since that's the usual height of
3609 # terminals, and any class that can't fit in one screen can't really
3610 # be considered "small".
3611 #
3612 # Also skip checks if we are on the first line. This accounts for
3613 # classes that look like
3614 # class Foo { public: ... };
3615 #
3616 # If we didn't find the end of the class, last_line would be zero,
3617 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00003618 if (class_info.last_line - class_info.starting_linenum <= 24 or
3619 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003620 return
3621
3622 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3623 if matched:
3624 # Issue warning if the line before public/protected/private was
3625 # not a blank line, but don't do this if the previous line contains
3626 # "class" or "struct". This can happen two ways:
3627 # - We are at the beginning of the class.
3628 # - We are forward-declaring an inner class that is semantically
3629 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00003630 # Also ignores cases where the previous line ends with a backslash as can be
3631 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003632 prev_line = clean_lines.lines[linenum - 1]
3633 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00003634 not Search(r'\b(class|struct)\b', prev_line) and
3635 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003636 # Try a bit harder to find the beginning of the class. This is to
3637 # account for multi-line base-specifier lists, e.g.:
3638 # class Derived
3639 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00003640 end_class_head = class_info.starting_linenum
3641 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003642 if Search(r'\{\s*$', clean_lines.lines[i]):
3643 end_class_head = i
3644 break
3645 if end_class_head < linenum - 1:
3646 error(filename, linenum, 'whitespace/blank_line', 3,
3647 '"%s:" should be preceded by a blank line' % matched.group(1))
3648
3649
erg@google.com4e00b9a2009-01-12 23:05:11 +00003650def GetPreviousNonBlankLine(clean_lines, linenum):
3651 """Return the most recent non-blank line and its line number.
3652
3653 Args:
3654 clean_lines: A CleansedLines instance containing the file contents.
3655 linenum: The number of the line to check.
3656
3657 Returns:
3658 A tuple with two elements. The first element is the contents of the last
3659 non-blank line before the current line, or the empty string if this is the
3660 first non-blank line. The second is the line number of that line, or -1
3661 if this is the first non-blank line.
3662 """
3663
3664 prevlinenum = linenum - 1
3665 while prevlinenum >= 0:
3666 prevline = clean_lines.elided[prevlinenum]
3667 if not IsBlankLine(prevline): # if not a blank line...
3668 return (prevline, prevlinenum)
3669 prevlinenum -= 1
3670 return ('', -1)
3671
3672
3673def CheckBraces(filename, clean_lines, linenum, error):
3674 """Looks for misplaced braces (e.g. at the end of line).
3675
3676 Args:
3677 filename: The name of the current file.
3678 clean_lines: A CleansedLines instance containing the file.
3679 linenum: The number of the line to check.
3680 error: The function to call with any errors found.
3681 """
3682
3683 line = clean_lines.elided[linenum] # get rid of comments and strings
3684
3685 if Match(r'\s*{\s*$', line):
erg@google.com2aa59982013-10-28 19:09:25 +00003686 # We allow an open brace to start a line in the case where someone is using
3687 # braces in a block to explicitly create a new scope, which is commonly used
3688 # to control the lifetime of stack-allocated variables. Braces are also
3689 # used for brace initializers inside function calls. We don't detect this
3690 # perfectly: we just don't complain if the last non-whitespace character on
3691 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
Alex Vakulenko01e47232016-05-06 13:54:15 -07003692 # previous line starts a preprocessor block. We also allow a brace on the
3693 # following line if it is part of an array initialization and would not fit
3694 # within the 80 character limit of the preceding line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003695 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.com2aa59982013-10-28 19:09:25 +00003696 if (not Search(r'[,;:}{(]\s*$', prevline) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07003697 not Match(r'\s*#', prevline) and
3698 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003699 error(filename, linenum, 'whitespace/braces', 4,
3700 '{ should almost always be at the end of the previous line')
3701
3702 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003703 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003704 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3705 if Match(r'\s*}\s*$', prevline):
3706 error(filename, linenum, 'whitespace/newline', 4,
3707 'An else should appear on the same line as the preceding }')
3708
3709 # If braces come on one side of an else, they should be on both.
3710 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.com02af6282014-06-04 18:53:25 +00003711 if Search(r'else if\s*\(', line): # could be multi-line if
3712 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3713 # find the ( after the if
3714 pos = line.find('else if')
3715 pos = line.find('(', pos)
3716 if pos > 0:
3717 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3718 brace_on_right = endline[endpos:].find('{') != -1
3719 if brace_on_left != brace_on_right: # must be brace after if
3720 error(filename, linenum, 'readability/braces', 5,
3721 'If an else has a brace on one side, it should have it on both')
3722 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3723 error(filename, linenum, 'readability/braces', 5,
3724 'If an else has a brace on one side, it should have it on both')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003725
3726 # Likewise, an else should never have the else clause on the same line
3727 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3728 error(filename, linenum, 'whitespace/newline', 4,
3729 'Else clause should never be on same line as else (use 2 lines)')
3730
3731 # In the same way, a do/while should never be on one line
3732 if Match(r'\s*do [^\s{]', line):
3733 error(filename, linenum, 'whitespace/newline', 4,
3734 'do/while clauses should not be on a single line')
3735
avakulenko@google.com02af6282014-06-04 18:53:25 +00003736 # Check single-line if/else bodies. The style guide says 'curly braces are not
3737 # required for single-line statements'. We additionally allow multi-line,
3738 # single statements, but we reject anything with more than one semicolon in
3739 # it. This means that the first semicolon after the if should be at the end of
3740 # its line, and the line after that should have an indent level equal to or
3741 # lower than the if. We also check for ambiguous if/else nesting without
3742 # braces.
3743 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3744 if if_else_match and not Match(r'\s*#', line):
3745 if_indent = GetIndentLevel(line)
3746 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3747 if_match = Search(r'\bif\s*\(', line)
3748 if if_match:
3749 # This could be a multiline if condition, so find the end first.
3750 pos = if_match.end() - 1
3751 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3752 # Check for an opening brace, either directly after the if or on the next
3753 # line. If found, this isn't a single-statement conditional.
3754 if (not Match(r'\s*{', endline[endpos:])
3755 and not (Match(r'\s*$', endline[endpos:])
3756 and endlinenum < (len(clean_lines.elided) - 1)
3757 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3758 while (endlinenum < len(clean_lines.elided)
3759 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3760 endlinenum += 1
3761 endpos = 0
3762 if endlinenum < len(clean_lines.elided):
3763 endline = clean_lines.elided[endlinenum]
3764 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3765 # methods) and a single \ after the semicolon (for macros)
3766 endpos = endline.find(';')
3767 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003768 # Semicolon isn't the last character, there's something trailing.
3769 # Output a warning if the semicolon is not contained inside
3770 # a lambda expression.
3771 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3772 endline):
3773 error(filename, linenum, 'readability/braces', 4,
3774 'If/else bodies with multiple statements require braces')
avakulenko@google.com02af6282014-06-04 18:53:25 +00003775 elif endlinenum < len(clean_lines.elided) - 1:
3776 # Make sure the next line is dedented
3777 next_line = clean_lines.elided[endlinenum + 1]
3778 next_indent = GetIndentLevel(next_line)
3779 # With ambiguous nested if statements, this will error out on the
3780 # if that *doesn't* match the else, regardless of whether it's the
3781 # inner one or outer one.
3782 if (if_match and Match(r'\s*else\b', next_line)
3783 and next_indent != if_indent):
3784 error(filename, linenum, 'readability/braces', 4,
3785 'Else clause should be indented at the same level as if. '
3786 'Ambiguous nested if/else chains require braces.')
3787 elif next_indent > if_indent:
3788 error(filename, linenum, 'readability/braces', 4,
3789 'If/else bodies with multiple statements require braces')
3790
3791
3792def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3793 """Looks for redundant trailing semicolon.
3794
3795 Args:
3796 filename: The name of the current file.
3797 clean_lines: A CleansedLines instance containing the file.
3798 linenum: The number of the line to check.
3799 error: The function to call with any errors found.
3800 """
3801
3802 line = clean_lines.elided[linenum]
3803
erg@google.com2aa59982013-10-28 19:09:25 +00003804 # Block bodies should not be followed by a semicolon. Due to C++11
3805 # brace initialization, there are more places where semicolons are
3806 # required than not, so we use a whitelist approach to check these
3807 # rather than a blacklist. These are the places where "};" should
3808 # be replaced by just "}":
3809 # 1. Some flavor of block following closing parenthesis:
3810 # for (;;) {};
3811 # while (...) {};
3812 # switch (...) {};
3813 # Function(...) {};
3814 # if (...) {};
3815 # if (...) else if (...) {};
3816 #
3817 # 2. else block:
3818 # if (...) else {};
3819 #
3820 # 3. const member function:
3821 # Function(...) const {};
3822 #
3823 # 4. Block following some statement:
3824 # x = 42;
3825 # {};
3826 #
3827 # 5. Block at the beginning of a function:
3828 # Function(...) {
3829 # {};
3830 # }
3831 #
3832 # Note that naively checking for the preceding "{" will also match
3833 # braces inside multi-dimensional arrays, but this is fine since
3834 # that expression will not contain semicolons.
3835 #
3836 # 6. Block following another block:
3837 # while (true) {}
3838 # {};
3839 #
3840 # 7. End of namespaces:
3841 # namespace {};
3842 #
3843 # These semicolons seems far more common than other kinds of
3844 # redundant semicolons, possibly due to people converting classes
3845 # to namespaces. For now we do not warn for this case.
3846 #
3847 # Try matching case 1 first.
3848 match = Match(r'^(.*\)\s*)\{', line)
3849 if match:
3850 # Matched closing parenthesis (case 1). Check the token before the
3851 # matching opening parenthesis, and don't warn if it looks like a
3852 # macro. This avoids these false positives:
3853 # - macro that defines a base class
3854 # - multi-line macro that defines a base class
3855 # - macro that defines the whole class-head
3856 #
3857 # But we still issue warnings for macros that we know are safe to
3858 # warn, specifically:
3859 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3860 # - TYPED_TEST
3861 # - INTERFACE_DEF
3862 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3863 #
3864 # We implement a whitelist of safe macros instead of a blacklist of
3865 # unsafe macros, even though the latter appears less frequently in
3866 # google code and would have been easier to implement. This is because
3867 # the downside for getting the whitelist wrong means some extra
3868 # semicolons, while the downside for getting the blacklist wrong
3869 # would result in compile errors.
3870 #
avakulenko@google.com554223d2014-12-04 22:00:20 +00003871 # In addition to macros, we also don't want to warn on
3872 # - Compound literals
3873 # - Lambdas
Alex Vakulenko01e47232016-05-06 13:54:15 -07003874 # - alignas specifier with anonymous structs
3875 # - decltype
erg@google.com2aa59982013-10-28 19:09:25 +00003876 closing_brace_pos = match.group(1).rfind(')')
3877 opening_parenthesis = ReverseCloseExpression(
3878 clean_lines, linenum, closing_brace_pos)
3879 if opening_parenthesis[2] > -1:
3880 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003881 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix)
avakulenko@google.com02af6282014-06-04 18:53:25 +00003882 func = Match(r'^(.*\])\s*$', line_prefix)
erg@google.com2aa59982013-10-28 19:09:25 +00003883 if ((macro and
3884 macro.group(1) not in (
3885 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3886 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3887 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.com02af6282014-06-04 18:53:25 +00003888 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
avakulenko@google.com554223d2014-12-04 22:00:20 +00003889 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or
Alex Vakulenko01e47232016-05-06 13:54:15 -07003890 Search(r'\bdecltype$', line_prefix) or
erg@google.com2aa59982013-10-28 19:09:25 +00003891 Search(r'\s+=\s*$', line_prefix)):
3892 match = None
avakulenko@google.com02af6282014-06-04 18:53:25 +00003893 if (match and
3894 opening_parenthesis[1] > 1 and
3895 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3896 # Multi-line lambda-expression
3897 match = None
erg@google.com2aa59982013-10-28 19:09:25 +00003898
3899 else:
3900 # Try matching cases 2-3.
3901 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3902 if not match:
3903 # Try matching cases 4-6. These are always matched on separate lines.
3904 #
3905 # Note that we can't simply concatenate the previous line to the
3906 # current line and do a single match, otherwise we may output
3907 # duplicate warnings for the blank line case:
3908 # if (cond) {
3909 # // blank line
3910 # }
3911 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3912 if prevline and Search(r'[;{}]\s*$', prevline):
3913 match = Match(r'^(\s*)\{', line)
3914
3915 # Check matching closing brace
3916 if match:
3917 (endline, endlinenum, endpos) = CloseExpression(
3918 clean_lines, linenum, len(match.group(1)))
3919 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3920 # Current {} pair is eligible for semicolon check, and we have found
3921 # the redundant semicolon, output warning here.
3922 #
3923 # Note: because we are scanning forward for opening braces, and
3924 # outputting warnings for the matching closing brace, if there are
3925 # nested blocks with trailing semicolons, we will get the error
3926 # messages in reversed order.
Piotr Semenovb7e2ef62016-05-20 18:39:34 +03003927
3928 # We need to check the line forward for NOLINT
3929 raw_lines = clean_lines.raw_lines
3930 ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1,
3931 error)
3932 ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum,
3933 error)
3934
erg@google.com2aa59982013-10-28 19:09:25 +00003935 error(filename, endlinenum, 'readability/braces', 4,
3936 "You don't need a ; after a }")
erg@google.com4e00b9a2009-01-12 23:05:11 +00003937
3938
erg@google.comc6671232013-10-25 21:44:03 +00003939def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3940 """Look for empty loop/conditional body with only a single semicolon.
erg@google.comd350fe52013-01-14 17:51:48 +00003941
3942 Args:
3943 filename: The name of the current file.
3944 clean_lines: A CleansedLines instance containing the file.
3945 linenum: The number of the line to check.
3946 error: The function to call with any errors found.
3947 """
3948
3949 # Search for loop keywords at the beginning of the line. Because only
3950 # whitespaces are allowed before the keywords, this will also ignore most
3951 # do-while-loops, since those lines should start with closing brace.
erg@google.comc6671232013-10-25 21:44:03 +00003952 #
3953 # We also check "if" blocks here, since an empty conditional block
3954 # is likely an error.
erg@google.comd350fe52013-01-14 17:51:48 +00003955 line = clean_lines.elided[linenum]
erg@google.comc6671232013-10-25 21:44:03 +00003956 matched = Match(r'\s*(for|while|if)\s*\(', line)
3957 if matched:
Alex Vakulenko01e47232016-05-06 13:54:15 -07003958 # Find the end of the conditional expression.
erg@google.comd350fe52013-01-14 17:51:48 +00003959 (end_line, end_linenum, end_pos) = CloseExpression(
3960 clean_lines, linenum, line.find('('))
3961
3962 # Output warning if what follows the condition expression is a semicolon.
3963 # No warning for all other cases, including whitespace or newline, since we
3964 # have a separate check for semicolons preceded by whitespace.
3965 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
erg@google.comc6671232013-10-25 21:44:03 +00003966 if matched.group(1) == 'if':
3967 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
3968 'Empty conditional bodies should use {}')
3969 else:
3970 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
3971 'Empty loop bodies should use {} or continue')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003972
Alex Vakulenko01e47232016-05-06 13:54:15 -07003973 # Check for if statements that have completely empty bodies (no comments)
3974 # and no else clauses.
3975 if end_pos >= 0 and matched.group(1) == 'if':
3976 # Find the position of the opening { for the if statement.
3977 # Return without logging an error if it has no brackets.
3978 opening_linenum = end_linenum
3979 opening_line_fragment = end_line[end_pos:]
3980 # Loop until EOF or find anything that's not whitespace or opening {.
3981 while not Search(r'^\s*\{', opening_line_fragment):
3982 if Search(r'^(?!\s*$)', opening_line_fragment):
3983 # Conditional has no brackets.
3984 return
3985 opening_linenum += 1
3986 if opening_linenum == len(clean_lines.elided):
3987 # Couldn't find conditional's opening { or any code before EOF.
3988 return
3989 opening_line_fragment = clean_lines.elided[opening_linenum]
3990 # Set opening_line (opening_line_fragment may not be entire opening line).
3991 opening_line = clean_lines.elided[opening_linenum]
3992
3993 # Find the position of the closing }.
3994 opening_pos = opening_line_fragment.find('{')
3995 if opening_linenum == end_linenum:
3996 # We need to make opening_pos relative to the start of the entire line.
3997 opening_pos += end_pos
3998 (closing_line, closing_linenum, closing_pos) = CloseExpression(
3999 clean_lines, opening_linenum, opening_pos)
4000 if closing_pos < 0:
4001 return
4002
4003 # Now construct the body of the conditional. This consists of the portion
4004 # of the opening line after the {, all lines until the closing line,
4005 # and the portion of the closing line before the }.
4006 if (clean_lines.raw_lines[opening_linenum] !=
4007 CleanseComments(clean_lines.raw_lines[opening_linenum])):
4008 # Opening line ends with a comment, so conditional isn't empty.
4009 return
4010 if closing_linenum > opening_linenum:
4011 # Opening line after the {. Ignore comments here since we checked above.
4012 body = list(opening_line[opening_pos+1:])
4013 # All lines until closing line, excluding closing line, with comments.
4014 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum])
4015 # Closing line before the }. Won't (and can't) have comments.
4016 body.append(clean_lines.elided[closing_linenum][:closing_pos-1])
4017 body = '\n'.join(body)
4018 else:
4019 # If statement has brackets and fits on a single line.
4020 body = opening_line[opening_pos+1:closing_pos-1]
4021
4022 # Check if the body is empty
4023 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body):
4024 return
4025 # The body is empty. Now make sure there's not an else clause.
4026 current_linenum = closing_linenum
4027 current_line_fragment = closing_line[closing_pos:]
4028 # Loop until EOF or find anything that's not whitespace or else clause.
4029 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment):
4030 if Search(r'^(?=\s*else)', current_line_fragment):
4031 # Found an else clause, so don't log an error.
4032 return
4033 current_linenum += 1
4034 if current_linenum == len(clean_lines.elided):
4035 break
4036 current_line_fragment = clean_lines.elided[current_linenum]
4037
4038 # The body is empty and there's no else clause until EOF or other code.
4039 error(filename, end_linenum, 'whitespace/empty_if_body', 4,
4040 ('If statement had no body and no else clause'))
4041
erg@google.com4e00b9a2009-01-12 23:05:11 +00004042
avakulenko@google.com02af6282014-06-04 18:53:25 +00004043def FindCheckMacro(line):
4044 """Find a replaceable CHECK-like macro.
4045
4046 Args:
4047 line: line to search on.
4048 Returns:
4049 (macro name, start position), or (None, -1) if no replaceable
4050 macro is found.
4051 """
4052 for macro in _CHECK_MACROS:
4053 i = line.find(macro)
4054 if i >= 0:
4055 # Find opening parenthesis. Do a regular expression match here
4056 # to make sure that we are matching the expected CHECK macro, as
4057 # opposed to some other macro that happens to contain the CHECK
4058 # substring.
4059 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4060 if not matched:
4061 continue
4062 return (macro, len(matched.group(1)))
4063 return (None, -1)
4064
4065
erg@google.com4e00b9a2009-01-12 23:05:11 +00004066def CheckCheck(filename, clean_lines, linenum, error):
4067 """Checks the use of CHECK and EXPECT macros.
4068
4069 Args:
4070 filename: The name of the current file.
4071 clean_lines: A CleansedLines instance containing the file.
4072 linenum: The number of the line to check.
4073 error: The function to call with any errors found.
4074 """
4075
4076 # Decide the set of replacement macros that should be suggested
erg@google.comc6671232013-10-25 21:44:03 +00004077 lines = clean_lines.elided
avakulenko@google.com02af6282014-06-04 18:53:25 +00004078 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4079 if not check_macro:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004080 return
4081
erg@google.comc6671232013-10-25 21:44:03 +00004082 # Find end of the boolean expression by matching parentheses
4083 (last_line, end_line, end_pos) = CloseExpression(
4084 clean_lines, linenum, start_pos)
4085 if end_pos < 0:
4086 return
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004087
4088 # If the check macro is followed by something other than a
4089 # semicolon, assume users will log their own custom error messages
4090 # and don't suggest any replacements.
4091 if not Match(r'\s*;', last_line[end_pos:]):
4092 return
4093
erg@google.comc6671232013-10-25 21:44:03 +00004094 if linenum == end_line:
4095 expression = lines[linenum][start_pos + 1:end_pos - 1]
4096 else:
4097 expression = lines[linenum][start_pos + 1:]
4098 for i in xrange(linenum + 1, end_line):
4099 expression += lines[i]
4100 expression += last_line[0:end_pos - 1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004101
erg@google.comc6671232013-10-25 21:44:03 +00004102 # Parse expression so that we can take parentheses into account.
4103 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4104 # which is not replaceable by CHECK_LE.
4105 lhs = ''
4106 rhs = ''
4107 operator = None
4108 while expression:
4109 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4110 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4111 if matched:
4112 token = matched.group(1)
4113 if token == '(':
4114 # Parenthesized operand
4115 expression = matched.group(2)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004116 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
erg@google.comc6671232013-10-25 21:44:03 +00004117 if end < 0:
4118 return # Unmatched parenthesis
4119 lhs += '(' + expression[0:end]
4120 expression = expression[end:]
4121 elif token in ('&&', '||'):
4122 # Logical and/or operators. This means the expression
4123 # contains more than one term, for example:
4124 # CHECK(42 < a && a < b);
4125 #
4126 # These are not replaceable with CHECK_LE, so bail out early.
4127 return
4128 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4129 # Non-relational operator
4130 lhs += token
4131 expression = matched.group(2)
4132 else:
4133 # Relational operator
4134 operator = token
4135 rhs = matched.group(2)
4136 break
4137 else:
4138 # Unparenthesized operand. Instead of appending to lhs one character
4139 # at a time, we do another regular expression match to consume several
4140 # characters at once if possible. Trivial benchmark shows that this
4141 # is more efficient when the operands are longer than a single
4142 # character, which is generally the case.
4143 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4144 if not matched:
4145 matched = Match(r'^(\s*\S)(.*)$', expression)
4146 if not matched:
4147 break
4148 lhs += matched.group(1)
4149 expression = matched.group(2)
4150
4151 # Only apply checks if we got all parts of the boolean expression
4152 if not (lhs and operator and rhs):
4153 return
4154
4155 # Check that rhs do not contain logical operators. We already know
4156 # that lhs is fine since the loop above parses out && and ||.
4157 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4158 return
4159
4160 # At least one of the operands must be a constant literal. This is
4161 # to avoid suggesting replacements for unprintable things like
4162 # CHECK(variable != iterator)
4163 #
4164 # The following pattern matches decimal, hex integers, strings, and
4165 # characters (in that order).
4166 lhs = lhs.strip()
4167 rhs = rhs.strip()
4168 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4169 if Match(match_constant, lhs) or Match(match_constant, rhs):
4170 # Note: since we know both lhs and rhs, we can provide a more
4171 # descriptive error message like:
4172 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4173 # Instead of:
4174 # Consider using CHECK_EQ instead of CHECK(a == b)
4175 #
4176 # We are still keeping the less descriptive message because if lhs
4177 # or rhs gets long, the error message might become unreadable.
4178 error(filename, linenum, 'readability/check', 2,
4179 'Consider using %s instead of %s(a %s b)' % (
4180 _CHECK_REPLACEMENT[check_macro][operator],
4181 check_macro, operator))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004182
4183
erg@google.comd350fe52013-01-14 17:51:48 +00004184def CheckAltTokens(filename, clean_lines, linenum, error):
4185 """Check alternative keywords being used in boolean expressions.
4186
4187 Args:
4188 filename: The name of the current file.
4189 clean_lines: A CleansedLines instance containing the file.
4190 linenum: The number of the line to check.
4191 error: The function to call with any errors found.
4192 """
4193 line = clean_lines.elided[linenum]
4194
4195 # Avoid preprocessor lines
4196 if Match(r'^\s*#', line):
4197 return
4198
4199 # Last ditch effort to avoid multi-line comments. This will not help
4200 # if the comment started before the current line or ended after the
4201 # current line, but it catches most of the false positives. At least,
4202 # it provides a way to workaround this warning for people who use
4203 # multi-line comments in preprocessor macros.
4204 #
4205 # TODO(unknown): remove this once cpplint has better support for
4206 # multi-line comments.
4207 if line.find('/*') >= 0 or line.find('*/') >= 0:
4208 return
4209
4210 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4211 error(filename, linenum, 'readability/alt_tokens', 2,
4212 'Use operator %s instead of %s' % (
4213 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4214
4215
erg@google.com4e00b9a2009-01-12 23:05:11 +00004216def GetLineWidth(line):
4217 """Determines the width of the line in column positions.
4218
4219 Args:
4220 line: A string, which may be a Unicode string.
4221
4222 Returns:
4223 The width of the line in column positions, accounting for Unicode
4224 combining characters and wide characters.
4225 """
4226 if isinstance(line, unicode):
4227 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00004228 for uc in unicodedata.normalize('NFC', line):
4229 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004230 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00004231 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004232 width += 1
4233 return width
4234 else:
4235 return len(line)
4236
4237
erg@google.comd350fe52013-01-14 17:51:48 +00004238def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00004239 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004240 """Checks rules from the 'C++ style rules' section of cppguide.html.
4241
4242 Most of these rules are hard to test (naming, comment style), but we
4243 do what we can. In particular we check for 2-space indents, line lengths,
4244 tab usage, spaces inside code, etc.
4245
4246 Args:
4247 filename: The name of the current file.
4248 clean_lines: A CleansedLines instance containing the file.
4249 linenum: The number of the line to check.
4250 file_extension: The extension (without the dot) of the filename.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004251 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00004252 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004253 error: The function to call with any errors found.
4254 """
4255
erg@google.com2aa59982013-10-28 19:09:25 +00004256 # Don't use "elided" lines here, otherwise we can't check commented lines.
4257 # Don't want to use "raw" either, because we don't want to check inside C++11
4258 # raw strings,
4259 raw_lines = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00004260 line = raw_lines[linenum]
Alex Vakulenko01e47232016-05-06 13:54:15 -07004261 prev = raw_lines[linenum - 1] if linenum > 0 else ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00004262
4263 if line.find('\t') != -1:
4264 error(filename, linenum, 'whitespace/tab', 1,
4265 'Tab found; better to use spaces')
4266
4267 # One or three blank spaces at the beginning of the line is weird; it's
4268 # hard to reconcile that with 2-space indents.
4269 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4270 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4271 # if(RLENGTH > 20) complain = 0;
4272 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4273 # if(match(prev, "&& *$")) complain = 0;
4274 # if(match(prev, "\\|\\| *$")) complain = 0;
4275 # if(match(prev, "[\",=><] *$")) complain = 0;
4276 # if(match($0, " <<")) complain = 0;
4277 # if(match(prev, " +for \\(")) complain = 0;
4278 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.com02af6282014-06-04 18:53:25 +00004279 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4280 classinfo = nesting_state.InnermostClass()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004281 initial_spaces = 0
4282 cleansed_line = clean_lines.elided[linenum]
4283 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4284 initial_spaces += 1
avakulenko@google.com02af6282014-06-04 18:53:25 +00004285 # There are certain situations we allow one space, notably for
4286 # section labels, and also lines containing multi-line raw strings.
Alex Vakulenko01e47232016-05-06 13:54:15 -07004287 # We also don't check for lines that look like continuation lines
4288 # (of lines ending in double quotes, commas, equals, or angle brackets)
4289 # because the rules for how to indent those are non-trivial.
4290 if (not Search(r'[",=><] *$', prev) and
4291 (initial_spaces == 1 or initial_spaces == 3) and
4292 not Match(scope_or_label_pattern, cleansed_line) and
4293 not (clean_lines.raw_lines[linenum] != line and
4294 Match(r'^\s*""', line))):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004295 error(filename, linenum, 'whitespace/indent', 3,
4296 'Weird number of spaces at line-start. '
4297 'Are you using a 2-space indent?')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004298
Alex Vakulenko01e47232016-05-06 13:54:15 -07004299 if line and line[-1].isspace():
4300 error(filename, linenum, 'whitespace/end_of_line', 4,
4301 'Line ends in whitespace. Consider deleting these extra spaces.')
4302
erg@google.com4e00b9a2009-01-12 23:05:11 +00004303 # Check if the line is a header guard.
4304 is_header_guard = False
LukeCz7197a242016-09-24 13:27:35 -05004305 if IsHeaderExtension(file_extension):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004306 cppvar = GetHeaderGuardCPPVariable(filename)
4307 if (line.startswith('#ifndef %s' % cppvar) or
4308 line.startswith('#define %s' % cppvar) or
4309 line.startswith('#endif // %s' % cppvar)):
4310 is_header_guard = True
4311 # #include lines and header guards can be long, since there's no clean way to
4312 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00004313 #
4314 # URLs can be long too. It's possible to split these, but it makes them
4315 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00004316 #
4317 # The "$Id:...$" comment may also get very long without it being the
4318 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00004319 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00004320 not Match(r'^\s*//.*http(s?)://\S*$', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004321 not Match(r'^\s*//\s*[^\s]*$', line) and
erg@google.comd7d27472011-09-07 17:36:35 +00004322 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004323 line_width = GetLineWidth(line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004324 if line_width > _line_length:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004325 error(filename, linenum, 'whitespace/line_length', 2,
erg@google.comab53edf2013-11-05 22:23:37 +00004326 'Lines should be <= %i characters long' % _line_length)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004327
4328 if (cleansed_line.count(';') > 1 and
4329 # for loops are allowed two ;'s (and may run over two lines).
4330 cleansed_line.find('for') == -1 and
4331 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4332 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4333 # It's ok to have many commands in a switch case that fits in 1 line
4334 not ((cleansed_line.find('case ') != -1 or
4335 cleansed_line.find('default:') != -1) and
4336 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00004337 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00004338 'More than one command on the same line')
4339
4340 # Some more style checks
4341 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004342 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
erg@google.comc6671232013-10-25 21:44:03 +00004343 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004344 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
4345 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004346 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4347 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4348 CheckCommaSpacing(filename, clean_lines, linenum, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004349 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004350 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004351 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004352 CheckAltTokens(filename, clean_lines, linenum, error)
4353 classinfo = nesting_state.InnermostClass()
4354 if classinfo:
4355 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004356
4357
erg@google.com4e00b9a2009-01-12 23:05:11 +00004358_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4359# Matches the first component of a filename delimited by -s and _s. That is:
4360# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4361# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4362# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4363# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4364_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4365
4366
4367def _DropCommonSuffixes(filename):
4368 """Drops common suffixes like _test.cc or -inl.h from filename.
4369
4370 For example:
4371 >>> _DropCommonSuffixes('foo/foo-inl.h')
4372 'foo/foo'
4373 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4374 'foo/bar/foo'
4375 >>> _DropCommonSuffixes('foo/foo_internal.h')
4376 'foo/foo'
4377 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4378 'foo/foo_unusualinternal'
4379
4380 Args:
4381 filename: The input filename.
4382
4383 Returns:
4384 The filename with the common suffix removed.
4385 """
4386 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4387 'inl.h', 'impl.h', 'internal.h'):
4388 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4389 filename[-len(suffix) - 1] in ('-', '_')):
4390 return filename[:-len(suffix) - 1]
4391 return os.path.splitext(filename)[0]
4392
4393
erg@google.com4e00b9a2009-01-12 23:05:11 +00004394def _ClassifyInclude(fileinfo, include, is_system):
4395 """Figures out what kind of header 'include' is.
4396
4397 Args:
4398 fileinfo: The current file cpplint is running over. A FileInfo instance.
4399 include: The path to a #included file.
4400 is_system: True if the #include used <> rather than "".
4401
4402 Returns:
4403 One of the _XXX_HEADER constants.
4404
4405 For example:
4406 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4407 _C_SYS_HEADER
4408 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4409 _CPP_SYS_HEADER
4410 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4411 _LIKELY_MY_HEADER
4412 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4413 ... 'bar/foo_other_ext.h', False)
4414 _POSSIBLE_MY_HEADER
4415 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4416 _OTHER_HEADER
4417 """
4418 # This is a list of all standard c++ header files, except
4419 # those already checked for above.
erg@google.comfd5da632013-10-25 17:39:45 +00004420 is_cpp_h = include in _CPP_HEADERS
erg@google.com4e00b9a2009-01-12 23:05:11 +00004421
4422 if is_system:
4423 if is_cpp_h:
4424 return _CPP_SYS_HEADER
4425 else:
4426 return _C_SYS_HEADER
4427
4428 # If the target file and the include we're checking share a
4429 # basename when we drop common extensions, and the include
4430 # lives in . , then it's likely to be owned by the target file.
4431 target_dir, target_base = (
4432 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4433 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4434 if target_base == include_base and (
4435 include_dir == target_dir or
4436 include_dir == os.path.normpath(target_dir + '/../public')):
4437 return _LIKELY_MY_HEADER
4438
4439 # If the target and include share some initial basename
4440 # component, it's possible the target is implementing the
4441 # include, so it's allowed to be first, but we'll never
4442 # complain if it's not there.
4443 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4444 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4445 if (target_first_component and include_first_component and
4446 target_first_component.group(0) ==
4447 include_first_component.group(0)):
4448 return _POSSIBLE_MY_HEADER
4449
4450 return _OTHER_HEADER
4451
4452
erg@google.coma87abb82009-02-24 01:41:01 +00004453
erg@google.come35f7652009-06-19 20:52:09 +00004454def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4455 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004456
erg@google.come35f7652009-06-19 20:52:09 +00004457 Strings on #include lines are NOT removed from elided line, to make
4458 certain tasks easier. However, to prevent false positives, checks
4459 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004460
4461 Args:
4462 filename: The name of the current file.
4463 clean_lines: A CleansedLines instance containing the file.
4464 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004465 include_state: An _IncludeState instance in which the headers are inserted.
4466 error: The function to call with any errors found.
4467 """
4468 fileinfo = FileInfo(filename)
erg@google.come35f7652009-06-19 20:52:09 +00004469 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004470
4471 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004472 # Only do this check if the included header follows google naming
4473 # conventions. If not, assume that it's a 3rd party API that
4474 # requires special include conventions.
4475 #
4476 # We also make an exception for Lua headers, which follow google
4477 # naming convention but not the include convention.
4478 match = Match(r'#include\s*"([^/]+\.h)"', line)
4479 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004480 error(filename, linenum, 'build/include', 4,
4481 'Include the directory when naming .h files')
4482
4483 # we shouldn't include a file more than once. actually, there are a
4484 # handful of instances where doing so is okay, but in general it's
4485 # not.
erg@google.come35f7652009-06-19 20:52:09 +00004486 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004487 if match:
4488 include = match.group(2)
4489 is_system = (match.group(1) == '<')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004490 duplicate_line = include_state.FindHeader(include)
4491 if duplicate_line >= 0:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004492 error(filename, linenum, 'build/include', 4,
4493 '"%s" already included at %s:%s' %
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004494 (include, filename, duplicate_line))
avakulenko@google.com554223d2014-12-04 22:00:20 +00004495 elif (include.endswith('.cc') and
4496 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)):
4497 error(filename, linenum, 'build/include', 4,
4498 'Do not include .cc files from other packages')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004499 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4500 include_state.include_list[-1].append((include, linenum))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004501
4502 # We want to ensure that headers appear in the right order:
4503 # 1) for foo.cc, foo.h (preferred location)
4504 # 2) c system files
4505 # 3) cpp system files
4506 # 4) for foo.cc, foo.h (deprecated location)
4507 # 5) other google headers
4508 #
4509 # We classify each include statement as one of those 5 types
4510 # using a number of techniques. The include_state object keeps
4511 # track of the highest type seen, and complains if we see a
4512 # lower type after that.
4513 error_message = include_state.CheckNextIncludeOrder(
4514 _ClassifyInclude(fileinfo, include, is_system))
4515 if error_message:
4516 error(filename, linenum, 'build/include_order', 4,
4517 '%s. Should be: %s.h, c system, c++ system, other.' %
4518 (error_message, fileinfo.BaseName()))
erg@google.comfd5da632013-10-25 17:39:45 +00004519 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4520 if not include_state.IsInAlphabeticalOrder(
4521 clean_lines, linenum, canonical_include):
erg@google.coma868d2d2009-10-09 21:18:45 +00004522 error(filename, linenum, 'build/include_alpha', 4,
4523 'Include "%s" not in alphabetical order' % include)
erg@google.comfd5da632013-10-25 17:39:45 +00004524 include_state.SetLastHeader(canonical_include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004525
erg@google.come35f7652009-06-19 20:52:09 +00004526
erg@google.com8a95ecc2011-09-08 00:45:54 +00004527
4528def _GetTextInside(text, start_pattern):
erg@google.com2aa59982013-10-28 19:09:25 +00004529 r"""Retrieves all the text between matching open and close parentheses.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004530
4531 Given a string of lines and a regular expression string, retrieve all the text
4532 following the expression and between opening punctuation symbols like
4533 (, [, or {, and the matching close-punctuation symbol. This properly nested
4534 occurrences of the punctuations, so for the text like
4535 printf(a(), b(c()));
4536 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4537 start_pattern must match string having an open punctuation symbol at the end.
4538
4539 Args:
4540 text: The lines to extract text. Its comments and strings must be elided.
4541 It can be single line and can span multiple lines.
4542 start_pattern: The regexp string indicating where to start extracting
4543 the text.
4544 Returns:
4545 The extracted text.
4546 None if either the opening string or ending punctuation could not be found.
4547 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00004548 # TODO(unknown): Audit cpplint.py to see what places could be profitably
erg@google.com8a95ecc2011-09-08 00:45:54 +00004549 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4550
4551 # Give opening punctuations to get the matching close-punctuations.
4552 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4553 closing_punctuation = set(matching_punctuation.itervalues())
4554
4555 # Find the position to start extracting text.
4556 match = re.search(start_pattern, text, re.M)
4557 if not match: # start_pattern not found in text.
4558 return None
4559 start_position = match.end(0)
4560
4561 assert start_position > 0, (
4562 'start_pattern must ends with an opening punctuation.')
4563 assert text[start_position - 1] in matching_punctuation, (
4564 'start_pattern must ends with an opening punctuation.')
4565 # Stack of closing punctuations we expect to have in text after position.
4566 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4567 position = start_position
4568 while punctuation_stack and position < len(text):
4569 if text[position] == punctuation_stack[-1]:
4570 punctuation_stack.pop()
4571 elif text[position] in closing_punctuation:
4572 # A closing punctuation without matching opening punctuations.
4573 return None
4574 elif text[position] in matching_punctuation:
4575 punctuation_stack.append(matching_punctuation[text[position]])
4576 position += 1
4577 if punctuation_stack:
4578 # Opening punctuations left without matching close-punctuations.
4579 return None
4580 # punctuations match.
4581 return text[start_position:position - 1]
4582
4583
erg@google.comfd5da632013-10-25 17:39:45 +00004584# Patterns for matching call-by-reference parameters.
erg@google.com2aa59982013-10-28 19:09:25 +00004585#
4586# Supports nested templates up to 2 levels deep using this messy pattern:
4587# < (?: < (?: < [^<>]*
4588# >
4589# | [^<>] )*
4590# >
4591# | [^<>] )*
4592# >
erg@google.comfd5da632013-10-25 17:39:45 +00004593_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4594_RE_PATTERN_TYPE = (
4595 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
erg@google.com2aa59982013-10-28 19:09:25 +00004596 r'(?:\w|'
4597 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4598 r'::)+')
erg@google.comfd5da632013-10-25 17:39:45 +00004599# A call-by-reference parameter ends with '& identifier'.
4600_RE_PATTERN_REF_PARAM = re.compile(
4601 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4602 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4603# A call-by-const-reference parameter either ends with 'const& identifier'
4604# or looks like 'const type& identifier' when 'type' is atomic.
4605_RE_PATTERN_CONST_REF_PARAM = (
4606 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4607 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004608# Stream types.
4609_RE_PATTERN_REF_STREAM_PARAM = (
4610 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')')
erg@google.comfd5da632013-10-25 17:39:45 +00004611
4612
4613def CheckLanguage(filename, clean_lines, linenum, file_extension,
4614 include_state, nesting_state, error):
erg@google.come35f7652009-06-19 20:52:09 +00004615 """Checks rules from the 'C++ language rules' section of cppguide.html.
4616
4617 Some of these rules are hard to test (function overloading, using
4618 uint32 inappropriately), but we do the best we can.
4619
4620 Args:
4621 filename: The name of the current file.
4622 clean_lines: A CleansedLines instance containing the file.
4623 linenum: The number of the line to check.
4624 file_extension: The extension (without the dot) of the filename.
4625 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004626 nesting_state: A NestingState instance which maintains information about
erg@google.comfd5da632013-10-25 17:39:45 +00004627 the current stack of nested blocks being parsed.
erg@google.come35f7652009-06-19 20:52:09 +00004628 error: The function to call with any errors found.
4629 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00004630 # If the line is empty or consists of entirely a comment, no need to
4631 # check it.
4632 line = clean_lines.elided[linenum]
4633 if not line:
4634 return
4635
erg@google.come35f7652009-06-19 20:52:09 +00004636 match = _RE_PATTERN_INCLUDE.search(line)
4637 if match:
4638 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4639 return
4640
erg@google.com2aa59982013-10-28 19:09:25 +00004641 # Reset include state across preprocessor directives. This is meant
4642 # to silence warnings for conditional includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004643 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4644 if match:
4645 include_state.ResetSection(match.group(1))
erg@google.com2aa59982013-10-28 19:09:25 +00004646
erg@google.com4e00b9a2009-01-12 23:05:11 +00004647 # Make Windows paths like Unix.
4648 fullname = os.path.abspath(filename).replace('\\', '/')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004649
avakulenko@google.com02af6282014-06-04 18:53:25 +00004650 # Perform other checks now that we are sure that this is not an include line
4651 CheckCasts(filename, clean_lines, linenum, error)
4652 CheckGlobalStatic(filename, clean_lines, linenum, error)
4653 CheckPrintf(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004654
LukeCz7197a242016-09-24 13:27:35 -05004655 if IsHeaderExtension(file_extension):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004656 # TODO(unknown): check that 1-arg constructors are explicit.
4657 # How to tell it's a constructor?
4658 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004659 # TODO(unknown): check that classes declare or disable copy/assign
erg@google.com4e00b9a2009-01-12 23:05:11 +00004660 # (level 1 error)
4661 pass
4662
4663 # Check if people are using the verboten C basic types. The only exception
4664 # we regularly allow is "unsigned short port" for port.
4665 if Search(r'\bshort port\b', line):
4666 if not Search(r'\bunsigned short port\b', line):
4667 error(filename, linenum, 'runtime/int', 4,
4668 'Use "unsigned short" for ports, not "short"')
4669 else:
4670 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4671 if match:
4672 error(filename, linenum, 'runtime/int', 4,
4673 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4674
erg@google.coma868d2d2009-10-09 21:18:45 +00004675 # Check if some verboten operator overloading is going on
4676 # TODO(unknown): catch out-of-line unary operator&:
4677 # class X {};
4678 # int operator&(const X& x) { return 42; } // unary operator&
4679 # The trick is it's hard to tell apart from binary operator&:
4680 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4681 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4682 error(filename, linenum, 'runtime/operator', 4,
4683 'Unary operator& is dangerous. Do not use it.')
4684
erg@google.com4e00b9a2009-01-12 23:05:11 +00004685 # Check for suspicious usage of "if" like
4686 # } if (a == b) {
4687 if Search(r'\}\s*if\s*\(', line):
4688 error(filename, linenum, 'readability/braces', 4,
4689 'Did you mean "else if"? If not, start a new line for "if".')
4690
4691 # Check for potential format string bugs like printf(foo).
4692 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4693 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.com02af6282014-06-04 18:53:25 +00004694 # TODO(unknown): Catch the following case. Need to change the calling
erg@google.com8a95ecc2011-09-08 00:45:54 +00004695 # convention of the whole function to process multiple line to handle it.
4696 # printf(
4697 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4698 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4699 if printf_args:
4700 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00004701 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00004702 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4703 line, re.I).group(1)
4704 error(filename, linenum, 'runtime/printf', 4,
4705 'Potential format string bug. Do %s("%%s", %s) instead.'
4706 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004707
4708 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4709 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4710 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4711 error(filename, linenum, 'runtime/memset', 4,
4712 'Did you mean "memset(%s, 0, %s)"?'
4713 % (match.group(1), match.group(2)))
4714
4715 if Search(r'\busing namespace\b', line):
4716 error(filename, linenum, 'build/namespaces', 5,
4717 'Do not use namespace using-directives. '
4718 'Use using-declarations instead.')
4719
4720 # Detect variable-length arrays.
4721 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4722 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4723 match.group(3).find(']') == -1):
4724 # Split the size using space and arithmetic operators as delimiters.
4725 # If any of the resulting tokens are not compile time constants then
4726 # report the error.
4727 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4728 is_const = True
4729 skip_next = False
4730 for tok in tokens:
4731 if skip_next:
4732 skip_next = False
4733 continue
4734
4735 if Search(r'sizeof\(.+\)', tok): continue
4736 if Search(r'arraysize\(\w+\)', tok): continue
4737
4738 tok = tok.lstrip('(')
4739 tok = tok.rstrip(')')
4740 if not tok: continue
4741 if Match(r'\d+', tok): continue
4742 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4743 if Match(r'k[A-Z0-9]\w*', tok): continue
4744 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4745 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4746 # A catch all for tricky sizeof cases, including 'sizeof expression',
4747 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00004748 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004749 if tok.startswith('sizeof'):
4750 skip_next = True
4751 continue
4752 is_const = False
4753 break
4754 if not is_const:
4755 error(filename, linenum, 'runtime/arrays', 1,
4756 'Do not use variable-length arrays. Use an appropriately named '
4757 "('k' followed by CamelCase) compile-time constant for the size.")
4758
erg@google.com4e00b9a2009-01-12 23:05:11 +00004759 # Check for use of unnamed namespaces in header files. Registration
4760 # macros are typically OK, so we allow use of "namespace {" on lines
4761 # that end with backslashes.
LukeCz7197a242016-09-24 13:27:35 -05004762 if (IsHeaderExtension(file_extension)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004763 and Search(r'\bnamespace\s*{', line)
4764 and line[-1] != '\\'):
4765 error(filename, linenum, 'build/namespaces', 4,
4766 'Do not use unnamed namespaces in header files. See '
Ackermann Yuriy79692902016-04-01 21:41:34 +13004767 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
erg@google.com4e00b9a2009-01-12 23:05:11 +00004768 ' for more information.')
4769
avakulenko@google.com02af6282014-06-04 18:53:25 +00004770
4771def CheckGlobalStatic(filename, clean_lines, linenum, error):
4772 """Check for unsafe global or static objects.
4773
4774 Args:
4775 filename: The name of the current file.
4776 clean_lines: A CleansedLines instance containing the file.
4777 linenum: The number of the line to check.
4778 error: The function to call with any errors found.
4779 """
4780 line = clean_lines.elided[linenum]
4781
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004782 # Match two lines at a time to support multiline declarations
4783 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4784 line += clean_lines.elided[linenum + 1].strip()
4785
avakulenko@google.com02af6282014-06-04 18:53:25 +00004786 # Check for people declaring static/global STL strings at the top level.
4787 # This is dangerous because the C++ language does not guarantee that
Alex Vakulenko01e47232016-05-06 13:54:15 -07004788 # globals with constructors are initialized before the first access, and
4789 # also because globals can be destroyed when some threads are still running.
4790 # TODO(unknown): Generalize this to also find static unique_ptr instances.
4791 # TODO(unknown): File bugs for clang-tidy to find these.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004792 match = Match(
Alex Vakulenko01e47232016-05-06 13:54:15 -07004793 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +'
4794 r'([a-zA-Z0-9_:]+)\b(.*)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00004795 line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004796
avakulenko@google.com02af6282014-06-04 18:53:25 +00004797 # Remove false positives:
4798 # - String pointers (as opposed to values).
4799 # string *pointer
4800 # const string *pointer
4801 # string const *pointer
4802 # string *const pointer
4803 #
4804 # - Functions and template specializations.
4805 # string Function<Type>(...
4806 # string Class<Type>::Method(...
4807 #
4808 # - Operators. These are matched separately because operator names
4809 # cross non-word boundaries, and trying to match both operators
4810 # and functions at the same time would decrease accuracy of
4811 # matching identifiers.
4812 # string Class::operator*()
4813 if (match and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004814 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00004815 not Search(r'\boperator\W', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004816 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))):
4817 if Search(r'\bconst\b', line):
4818 error(filename, linenum, 'runtime/string', 4,
4819 'For a static/global string constant, use a C style string '
4820 'instead: "%schar%s %s[]".' %
4821 (match.group(1), match.group(2) or '', match.group(3)))
4822 else:
4823 error(filename, linenum, 'runtime/string', 4,
4824 'Static/global string variables are not permitted.')
avakulenko@google.com02af6282014-06-04 18:53:25 +00004825
Alex Vakulenko01e47232016-05-06 13:54:15 -07004826 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or
4827 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004828 error(filename, linenum, 'runtime/init', 4,
4829 'You seem to be initializing a member variable with itself.')
4830
4831
4832def CheckPrintf(filename, clean_lines, linenum, error):
4833 """Check for printf related issues.
4834
4835 Args:
4836 filename: The name of the current file.
4837 clean_lines: A CleansedLines instance containing the file.
4838 linenum: The number of the line to check.
4839 error: The function to call with any errors found.
4840 """
4841 line = clean_lines.elided[linenum]
4842
4843 # When snprintf is used, the second argument shouldn't be a literal.
4844 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4845 if match and match.group(2) != '0':
4846 # If 2nd arg is zero, snprintf is used to calculate size.
4847 error(filename, linenum, 'runtime/printf', 3,
4848 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4849 'to snprintf.' % (match.group(1), match.group(2)))
4850
4851 # Check if some verboten C functions are being used.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004852 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004853 error(filename, linenum, 'runtime/printf', 5,
4854 'Never use sprintf. Use snprintf instead.')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004855 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004856 if match:
4857 error(filename, linenum, 'runtime/printf', 4,
4858 'Almost always, snprintf is better than %s' % match.group(1))
4859
4860
4861def IsDerivedFunction(clean_lines, linenum):
4862 """Check if current line contains an inherited function.
4863
4864 Args:
4865 clean_lines: A CleansedLines instance containing the file.
4866 linenum: The number of the line to check.
4867 Returns:
4868 True if current line contains a function with "override"
4869 virt-specifier.
4870 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004871 # Scan back a few lines for start of current function
4872 for i in xrange(linenum, max(-1, linenum - 10), -1):
4873 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4874 if match:
4875 # Look for "override" after the matching closing parenthesis
4876 line, _, closing_paren = CloseExpression(
4877 clean_lines, i, len(match.group(1)))
4878 return (closing_paren >= 0 and
4879 Search(r'\boverride\b', line[closing_paren:]))
4880 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00004881
4882
avakulenko@google.com554223d2014-12-04 22:00:20 +00004883def IsOutOfLineMethodDefinition(clean_lines, linenum):
4884 """Check if current line contains an out-of-line method definition.
4885
4886 Args:
4887 clean_lines: A CleansedLines instance containing the file.
4888 linenum: The number of the line to check.
4889 Returns:
4890 True if current line contains an out-of-line method definition.
4891 """
4892 # Scan back a few lines for start of current function
4893 for i in xrange(linenum, max(-1, linenum - 10), -1):
4894 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
4895 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
4896 return False
4897
4898
avakulenko@google.com02af6282014-06-04 18:53:25 +00004899def IsInitializerList(clean_lines, linenum):
4900 """Check if current line is inside constructor initializer list.
4901
4902 Args:
4903 clean_lines: A CleansedLines instance containing the file.
4904 linenum: The number of the line to check.
4905 Returns:
4906 True if current line appears to be inside constructor initializer
4907 list, False otherwise.
4908 """
4909 for i in xrange(linenum, 1, -1):
4910 line = clean_lines.elided[i]
4911 if i == linenum:
4912 remove_function_body = Match(r'^(.*)\{\s*$', line)
4913 if remove_function_body:
4914 line = remove_function_body.group(1)
4915
4916 if Search(r'\s:\s*\w+[({]', line):
4917 # A lone colon tend to indicate the start of a constructor
4918 # initializer list. It could also be a ternary operator, which
4919 # also tend to appear in constructor initializer lists as
4920 # opposed to parameter lists.
4921 return True
4922 if Search(r'\}\s*,\s*$', line):
4923 # A closing brace followed by a comma is probably the end of a
4924 # brace-initialized member in constructor initializer list.
4925 return True
4926 if Search(r'[{};]\s*$', line):
4927 # Found one of the following:
4928 # - A closing brace or semicolon, probably the end of the previous
4929 # function.
4930 # - An opening brace, probably the start of current class or namespace.
4931 #
4932 # Current line is probably not inside an initializer list since
4933 # we saw one of those things without seeing the starting colon.
4934 return False
4935
4936 # Got to the beginning of the file without seeing the start of
4937 # constructor initializer list.
4938 return False
4939
4940
erg@google.comc6671232013-10-25 21:44:03 +00004941def CheckForNonConstReference(filename, clean_lines, linenum,
4942 nesting_state, error):
4943 """Check for non-const references.
4944
4945 Separate from CheckLanguage since it scans backwards from current
4946 line, instead of scanning forward.
4947
4948 Args:
4949 filename: The name of the current file.
4950 clean_lines: A CleansedLines instance containing the file.
4951 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004952 nesting_state: A NestingState instance which maintains information about
erg@google.comc6671232013-10-25 21:44:03 +00004953 the current stack of nested blocks being parsed.
4954 error: The function to call with any errors found.
4955 """
4956 # Do nothing if there is no '&' on current line.
4957 line = clean_lines.elided[linenum]
4958 if '&' not in line:
4959 return
4960
avakulenko@google.com02af6282014-06-04 18:53:25 +00004961 # If a function is inherited, current function doesn't have much of
4962 # a choice, so any non-const references should not be blamed on
4963 # derived function.
4964 if IsDerivedFunction(clean_lines, linenum):
4965 return
4966
avakulenko@google.com554223d2014-12-04 22:00:20 +00004967 # Don't warn on out-of-line method definitions, as we would warn on the
4968 # in-line declaration, if it isn't marked with 'override'.
4969 if IsOutOfLineMethodDefinition(clean_lines, linenum):
4970 return
4971
erg@google.com2aa59982013-10-28 19:09:25 +00004972 # Long type names may be broken across multiple lines, usually in one
4973 # of these forms:
4974 # LongType
4975 # ::LongTypeContinued &identifier
4976 # LongType::
4977 # LongTypeContinued &identifier
4978 # LongType<
4979 # ...>::LongTypeContinued &identifier
4980 #
4981 # If we detected a type split across two lines, join the previous
4982 # line to current line so that we can match const references
4983 # accordingly.
erg@google.comc6671232013-10-25 21:44:03 +00004984 #
4985 # Note that this only scans back one line, since scanning back
4986 # arbitrary number of lines would be expensive. If you have a type
4987 # that spans more than 2 lines, please use a typedef.
4988 if linenum > 1:
4989 previous = None
erg@google.com2aa59982013-10-28 19:09:25 +00004990 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004991 # previous_line\n + ::current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004992 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004993 clean_lines.elided[linenum - 1])
erg@google.com2aa59982013-10-28 19:09:25 +00004994 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004995 # previous_line::\n + current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004996 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004997 clean_lines.elided[linenum - 1])
4998 if previous:
4999 line = previous.group(1) + line.lstrip()
erg@google.com2aa59982013-10-28 19:09:25 +00005000 else:
5001 # Check for templated parameter that is split across multiple lines
5002 endpos = line.rfind('>')
5003 if endpos > -1:
5004 (_, startline, startpos) = ReverseCloseExpression(
5005 clean_lines, linenum, endpos)
5006 if startpos > -1 and startline < linenum:
5007 # Found the matching < on an earlier line, collect all
5008 # pieces up to current line.
5009 line = ''
5010 for i in xrange(startline, linenum + 1):
5011 line += clean_lines.elided[i].strip()
erg@google.comc6671232013-10-25 21:44:03 +00005012
5013 # Check for non-const references in function parameters. A single '&' may
5014 # found in the following places:
5015 # inside expression: binary & for bitwise AND
5016 # inside expression: unary & for taking the address of something
5017 # inside declarators: reference parameter
5018 # We will exclude the first two cases by checking that we are not inside a
5019 # function body, including one that was just introduced by a trailing '{'.
erg@google.comc6671232013-10-25 21:44:03 +00005020 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.com02af6282014-06-04 18:53:25 +00005021 if (nesting_state.previous_stack_top and
5022 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
5023 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
5024 # Not at toplevel, not within a class, and not within a namespace
5025 return
5026
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005027 # Avoid initializer lists. We only need to scan back from the
5028 # current line for something that starts with ':'.
5029 #
5030 # We don't need to check the current line, since the '&' would
5031 # appear inside the second set of parentheses on the current line as
5032 # opposed to the first set.
5033 if linenum > 0:
5034 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
5035 previous_line = clean_lines.elided[i]
5036 if not Search(r'[),]\s*$', previous_line):
5037 break
5038 if Match(r'^\s*:\s+\S', previous_line):
5039 return
5040
avakulenko@google.com02af6282014-06-04 18:53:25 +00005041 # Avoid preprocessors
5042 if Search(r'\\\s*$', line):
5043 return
5044
5045 # Avoid constructor initializer lists
5046 if IsInitializerList(clean_lines, linenum):
5047 return
5048
erg@google.comc6671232013-10-25 21:44:03 +00005049 # We allow non-const references in a few standard places, like functions
5050 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5051 # those function parameters.
5052 #
5053 # We also accept & in static_assert, which looks like a function but
5054 # it's actually a declaration expression.
5055 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5056 r'operator\s*[<>][<>]|'
5057 r'static_assert|COMPILE_ASSERT'
5058 r')\s*\(')
5059 if Search(whitelisted_functions, line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005060 return
erg@google.comc6671232013-10-25 21:44:03 +00005061 elif not Search(r'\S+\([^)]*$', line):
5062 # Don't see a whitelisted function on this line. Actually we
5063 # didn't see any function name on this line, so this is likely a
5064 # multi-line parameter list. Try a bit harder to catch this case.
5065 for i in xrange(2):
5066 if (linenum > i and
5067 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005068 return
erg@google.comc6671232013-10-25 21:44:03 +00005069
avakulenko@google.com02af6282014-06-04 18:53:25 +00005070 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5071 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005072 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and
5073 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005074 error(filename, linenum, 'runtime/references', 2,
5075 'Is this a non-const reference? '
5076 'If so, make const or use a pointer: ' +
5077 ReplaceAll(' *<', '<', parameter))
5078
5079
5080def CheckCasts(filename, clean_lines, linenum, error):
5081 """Various cast related checks.
5082
5083 Args:
5084 filename: The name of the current file.
5085 clean_lines: A CleansedLines instance containing the file.
5086 linenum: The number of the line to check.
5087 error: The function to call with any errors found.
5088 """
5089 line = clean_lines.elided[linenum]
5090
5091 # Check to see if they're using an conversion function cast.
5092 # I just try to capture the most common basic types, though there are more.
5093 # Parameterless conversion functions, such as bool(), are allowed as they are
5094 # probably a member operator declaration or default constructor.
5095 match = Search(
Alex Vakulenko01e47232016-05-06 13:54:15 -07005096 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
avakulenko@google.com02af6282014-06-04 18:53:25 +00005097 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5098 r'(\([^)].*)', line)
5099 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5100 if match and not expecting_function:
5101 matched_type = match.group(2)
5102
5103 # matched_new_or_template is used to silence two false positives:
5104 # - New operators
5105 # - Template arguments with function types
5106 #
5107 # For template arguments, we match on types immediately following
5108 # an opening bracket without any spaces. This is a fast way to
5109 # silence the common case where the function type is the first
5110 # template argument. False negative with less-than comparison is
5111 # avoided because those operators are usually followed by a space.
5112 #
5113 # function<double(double)> // bracket + no space = false positive
5114 # value < double(42) // bracket + space = true positive
5115 matched_new_or_template = match.group(1)
5116
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005117 # Avoid arrays by looking for brackets that come after the closing
5118 # parenthesis.
5119 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5120 return
5121
avakulenko@google.com02af6282014-06-04 18:53:25 +00005122 # Other things to ignore:
5123 # - Function pointers
5124 # - Casts to pointer types
5125 # - Placement new
5126 # - Alias declarations
5127 matched_funcptr = match.group(3)
5128 if (matched_new_or_template is None and
5129 not (matched_funcptr and
5130 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5131 matched_funcptr) or
5132 matched_funcptr.startswith('(*)'))) and
5133 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5134 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5135 error(filename, linenum, 'readability/casting', 4,
5136 'Using deprecated casting style. '
5137 'Use static_cast<%s>(...) instead' %
5138 matched_type)
5139
5140 if not expecting_function:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005141 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005142 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5143
5144 # This doesn't catch all cases. Consider (const char * const)"hello".
5145 #
5146 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5147 # compile).
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005148 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5149 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005150 pass
5151 else:
5152 # Check pointer casts for other than string constants
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005153 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5154 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005155
5156 # In addition, we look for people taking the address of a cast. This
5157 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5158 # point where you think.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005159 #
5160 # Some non-identifier character is required before the '&' for the
5161 # expression to be recognized as a cast. These are casts:
5162 # expression = &static_cast<int*>(temporary());
5163 # function(&(int*)(temporary()));
5164 #
5165 # This is not a cast:
5166 # reference_type&(int* function_param);
avakulenko@google.com02af6282014-06-04 18:53:25 +00005167 match = Search(
avakulenko@google.com554223d2014-12-04 22:00:20 +00005168 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005169 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005170 if match:
avakulenko@google.com02af6282014-06-04 18:53:25 +00005171 # Try a better error message when the & is bound to something
5172 # dereferenced by the casted pointer, as opposed to the casted
5173 # pointer itself.
5174 parenthesis_error = False
5175 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5176 if match:
5177 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5178 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5179 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5180 if x2 >= 0:
5181 extended_line = clean_lines.elided[y2][x2:]
5182 if y2 < clean_lines.NumLines() - 1:
5183 extended_line += clean_lines.elided[y2 + 1]
5184 if Match(r'\s*(?:->|\[)', extended_line):
5185 parenthesis_error = True
5186
5187 if parenthesis_error:
5188 error(filename, linenum, 'readability/casting', 4,
5189 ('Are you taking an address of something dereferenced '
5190 'from a cast? Wrapping the dereferenced expression in '
5191 'parentheses will make the binding more obvious'))
5192 else:
5193 error(filename, linenum, 'runtime/casting', 4,
5194 ('Are you taking an address of a cast? '
5195 'This is dangerous: could be a temp var. '
5196 'Take the address before doing the cast, rather than after'))
erg@google.comc6671232013-10-25 21:44:03 +00005197
erg@google.com4e00b9a2009-01-12 23:05:11 +00005198
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005199def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005200 """Checks for a C-style cast by looking for the pattern.
5201
erg@google.com4e00b9a2009-01-12 23:05:11 +00005202 Args:
5203 filename: The name of the current file.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005204 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005205 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005206 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00005207 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005208 pattern: The regular expression used to find C-style casts.
5209 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005210
5211 Returns:
5212 True if an error was emitted.
5213 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005214 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005215 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00005216 match = Search(pattern, line)
5217 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00005218 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005219
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005220 # Exclude lines with keywords that tend to look like casts
5221 context = line[0:match.start(1) - 1]
5222 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5223 return False
5224
5225 # Try expanding current context to see if we one level of
5226 # parentheses inside a macro.
5227 if linenum > 0:
5228 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5229 context = clean_lines.elided[i] + context
5230 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
erg@google.comfd5da632013-10-25 17:39:45 +00005231 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005232
erg@google.comd350fe52013-01-14 17:51:48 +00005233 # operator++(int) and operator--(int)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005234 if context.endswith(' operator++') or context.endswith(' operator--'):
erg@google.comd350fe52013-01-14 17:51:48 +00005235 return False
5236
Alex Vakulenko01e47232016-05-06 13:54:15 -07005237 # A single unnamed argument for a function tends to look like old style cast.
5238 # If we see those, don't issue warnings for deprecated casts.
erg@google.comc6671232013-10-25 21:44:03 +00005239 remainder = line[match.end(0):]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005240 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005241 remainder):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005242 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005243
5244 # At this point, all that should be left is actual casts.
5245 error(filename, linenum, 'readability/casting', 4,
5246 'Using C-style cast. Use %s<%s>(...) instead' %
5247 (cast_type, match.group(1)))
5248
erg@google.com8a95ecc2011-09-08 00:45:54 +00005249 return True
5250
erg@google.com4e00b9a2009-01-12 23:05:11 +00005251
avakulenko@google.com02af6282014-06-04 18:53:25 +00005252def ExpectingFunctionArgs(clean_lines, linenum):
5253 """Checks whether where function type arguments are expected.
5254
5255 Args:
5256 clean_lines: A CleansedLines instance containing the file.
5257 linenum: The number of the line to check.
5258
5259 Returns:
5260 True if the line at 'linenum' is inside something that expects arguments
5261 of function types.
5262 """
5263 line = clean_lines.elided[linenum]
5264 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5265 (linenum >= 2 and
5266 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5267 clean_lines.elided[linenum - 1]) or
5268 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5269 clean_lines.elided[linenum - 2]) or
5270 Search(r'\bstd::m?function\s*\<\s*$',
5271 clean_lines.elided[linenum - 1]))))
5272
5273
erg@google.com4e00b9a2009-01-12 23:05:11 +00005274_HEADERS_CONTAINING_TEMPLATES = (
5275 ('<deque>', ('deque',)),
5276 ('<functional>', ('unary_function', 'binary_function',
5277 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5278 'negate',
5279 'equal_to', 'not_equal_to', 'greater', 'less',
5280 'greater_equal', 'less_equal',
5281 'logical_and', 'logical_or', 'logical_not',
5282 'unary_negate', 'not1', 'binary_negate', 'not2',
5283 'bind1st', 'bind2nd',
5284 'pointer_to_unary_function',
5285 'pointer_to_binary_function',
5286 'ptr_fun',
5287 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5288 'mem_fun_ref_t',
5289 'const_mem_fun_t', 'const_mem_fun1_t',
5290 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5291 'mem_fun_ref',
5292 )),
5293 ('<limits>', ('numeric_limits',)),
5294 ('<list>', ('list',)),
5295 ('<map>', ('map', 'multimap',)),
lhchavez2890dff2016-07-11 19:37:29 -07005296 ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr',
5297 'unique_ptr', 'weak_ptr')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005298 ('<queue>', ('queue', 'priority_queue',)),
5299 ('<set>', ('set', 'multiset',)),
5300 ('<stack>', ('stack',)),
5301 ('<string>', ('char_traits', 'basic_string',)),
avakulenko@google.com554223d2014-12-04 22:00:20 +00005302 ('<tuple>', ('tuple',)),
lhchavez2890dff2016-07-11 19:37:29 -07005303 ('<unordered_map>', ('unordered_map', 'unordered_multimap')),
5304 ('<unordered_set>', ('unordered_set', 'unordered_multiset')),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005305 ('<utility>', ('pair',)),
5306 ('<vector>', ('vector',)),
5307
5308 # gcc extensions.
5309 # Note: std::hash is their hash, ::hash is our hash
5310 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5311 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5312 ('<slist>', ('slist',)),
5313 )
5314
Alex Vakulenko01e47232016-05-06 13:54:15 -07005315_HEADERS_MAYBE_TEMPLATES = (
5316 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort',
5317 'transform',
5318 )),
lhchavez2890dff2016-07-11 19:37:29 -07005319 ('<utility>', ('forward', 'make_pair', 'move', 'swap')),
Alex Vakulenko01e47232016-05-06 13:54:15 -07005320 )
5321
erg@google.com4e00b9a2009-01-12 23:05:11 +00005322_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5323
Alex Vakulenko01e47232016-05-06 13:54:15 -07005324_re_pattern_headers_maybe_templates = []
5325for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
5326 for _template in _templates:
5327 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5328 # type::max().
5329 _re_pattern_headers_maybe_templates.append(
5330 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5331 _template,
5332 _header))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005333
Alex Vakulenko01e47232016-05-06 13:54:15 -07005334# Other scripts may reach in and modify this pattern.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005335_re_pattern_templates = []
5336for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5337 for _template in _templates:
5338 _re_pattern_templates.append(
5339 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5340 _template + '<>',
5341 _header))
5342
5343
erg@google.come35f7652009-06-19 20:52:09 +00005344def FilesBelongToSameModule(filename_cc, filename_h):
5345 """Check if these two filenames belong to the same module.
5346
5347 The concept of a 'module' here is a as follows:
5348 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5349 same 'module' if they are in the same directory.
5350 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5351 to belong to the same module here.
5352
5353 If the filename_cc contains a longer path than the filename_h, for example,
5354 '/absolute/path/to/base/sysinfo.cc', and this file would include
5355 'base/sysinfo.h', this function also produces the prefix needed to open the
5356 header. This is used by the caller of this function to more robustly open the
5357 header file. We don't have access to the real include paths in this context,
5358 so we need this guesswork here.
5359
5360 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5361 according to this implementation. Because of this, this function gives
5362 some false positives. This should be sufficiently rare in practice.
5363
5364 Args:
5365 filename_cc: is the path for the .cc file
5366 filename_h: is the path for the header path
5367
5368 Returns:
5369 Tuple with a bool and a string:
5370 bool: True if filename_cc and filename_h belong to the same module.
5371 string: the additional prefix needed to open the header file.
5372 """
5373
Alex Vakulenko01e47232016-05-06 13:54:15 -07005374 fileinfo = FileInfo(filename_cc)
5375 if not fileinfo.IsSource():
erg@google.come35f7652009-06-19 20:52:09 +00005376 return (False, '')
Alex Vakulenko01e47232016-05-06 13:54:15 -07005377 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5378 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5379 if matched_test_suffix:
5380 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
erg@google.come35f7652009-06-19 20:52:09 +00005381 filename_cc = filename_cc.replace('/public/', '/')
5382 filename_cc = filename_cc.replace('/internal/', '/')
5383
5384 if not filename_h.endswith('.h'):
5385 return (False, '')
5386 filename_h = filename_h[:-len('.h')]
5387 if filename_h.endswith('-inl'):
5388 filename_h = filename_h[:-len('-inl')]
5389 filename_h = filename_h.replace('/public/', '/')
5390 filename_h = filename_h.replace('/internal/', '/')
5391
5392 files_belong_to_same_module = filename_cc.endswith(filename_h)
5393 common_path = ''
5394 if files_belong_to_same_module:
5395 common_path = filename_cc[:-len(filename_h)]
5396 return files_belong_to_same_module, common_path
5397
5398
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005399def UpdateIncludeState(filename, include_dict, io=codecs):
5400 """Fill up the include_dict with new includes found from the file.
erg@google.come35f7652009-06-19 20:52:09 +00005401
5402 Args:
5403 filename: the name of the header to read.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005404 include_dict: a dictionary in which the headers are inserted.
erg@google.come35f7652009-06-19 20:52:09 +00005405 io: The io factory to use to read the file. Provided for testability.
5406
5407 Returns:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005408 True if a header was successfully added. False otherwise.
erg@google.come35f7652009-06-19 20:52:09 +00005409 """
5410 headerfile = None
5411 try:
5412 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5413 except IOError:
5414 return False
5415 linenum = 0
5416 for line in headerfile:
5417 linenum += 1
5418 clean_line = CleanseComments(line)
5419 match = _RE_PATTERN_INCLUDE.search(clean_line)
5420 if match:
5421 include = match.group(2)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005422 include_dict.setdefault(include, linenum)
erg@google.come35f7652009-06-19 20:52:09 +00005423 return True
5424
5425
5426def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5427 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005428 """Reports for missing stl includes.
5429
5430 This function will output warnings to make sure you are including the headers
5431 necessary for the stl containers and functions that you use. We only give one
5432 reason to include a header. For example, if you use both equal_to<> and
5433 less<> in a .h file, only one (the latter in the file) of these will be
5434 reported as a reason to include the <functional>.
5435
erg@google.com4e00b9a2009-01-12 23:05:11 +00005436 Args:
5437 filename: The name of the current file.
5438 clean_lines: A CleansedLines instance containing the file.
5439 include_state: An _IncludeState instance.
5440 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00005441 io: The IO factory to use to read the header file. Provided for unittest
5442 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005443 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00005444 required = {} # A map of header name to linenumber and the template entity.
5445 # Example of required: { '<functional>': (1219, 'less<>') }
5446
5447 for linenum in xrange(clean_lines.NumLines()):
5448 line = clean_lines.elided[linenum]
5449 if not line or line[0] == '#':
5450 continue
5451
5452 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005453 matched = _RE_PATTERN_STRING.search(line)
5454 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00005455 # Don't warn about strings in non-STL namespaces:
5456 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005457 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00005458 if prefix.endswith('std::') or not prefix.endswith('::'):
5459 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00005460
Alex Vakulenko01e47232016-05-06 13:54:15 -07005461 for pattern, template, header in _re_pattern_headers_maybe_templates:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005462 if pattern.search(line):
5463 required[header] = (linenum, template)
5464
5465 # The following function is just a speed up, no semantics are changed.
5466 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5467 continue
5468
5469 for pattern, template, header in _re_pattern_templates:
lhchavez3ae81f12016-07-11 19:00:34 -07005470 matched = pattern.search(line)
5471 if matched:
5472 # Don't warn about IWYU in non-STL namespaces:
5473 # (We check only the first match per line; good enough.)
5474 prefix = line[:matched.start()]
5475 if prefix.endswith('std::') or not prefix.endswith('::'):
5476 required[header] = (linenum, template)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005477
erg@google.come35f7652009-06-19 20:52:09 +00005478 # The policy is that if you #include something in foo.h you don't need to
5479 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005480 # Let's flatten the include_state include_list and copy it into a dictionary.
5481 include_dict = dict([item for sublist in include_state.include_list
5482 for item in sublist])
erg@google.come35f7652009-06-19 20:52:09 +00005483
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005484 # Did we find the header for this file (if any) and successfully load it?
erg@google.come35f7652009-06-19 20:52:09 +00005485 header_found = False
5486
5487 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00005488 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00005489
5490 # For Emacs's flymake.
5491 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5492 # by flymake and that file name might end with '_flymake.cc'. In that case,
5493 # restore original file name here so that the corresponding header file can be
5494 # found.
5495 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5496 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00005497 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00005498
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005499 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.come35f7652009-06-19 20:52:09 +00005500 # the keys.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005501 header_keys = include_dict.keys()
erg@google.com8a95ecc2011-09-08 00:45:54 +00005502 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00005503 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5504 fullpath = common_path + header
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005505 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.come35f7652009-06-19 20:52:09 +00005506 header_found = True
5507
5508 # If we can't find the header file for a .cc, assume it's because we don't
5509 # know where to look. In that case we'll give up as we're not sure they
5510 # didn't include it in the .h file.
5511 # TODO(unknown): Do a better job of finding .h files so we are confident that
5512 # not having the .h file means there isn't one.
5513 if filename.endswith('.cc') and not header_found:
5514 return
5515
erg@google.com4e00b9a2009-01-12 23:05:11 +00005516 # All the lines have been processed, report the errors found.
5517 for required_header_unstripped in required:
5518 template = required[required_header_unstripped][1]
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005519 if required_header_unstripped.strip('<>"') not in include_dict:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005520 error(filename, required[required_header_unstripped][0],
5521 'build/include_what_you_use', 4,
5522 'Add #include ' + required_header_unstripped + ' for ' + template)
5523
5524
erg@google.com8a95ecc2011-09-08 00:45:54 +00005525_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5526
5527
5528def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5529 """Check that make_pair's template arguments are deduced.
5530
avakulenko@google.com02af6282014-06-04 18:53:25 +00005531 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
erg@google.com8a95ecc2011-09-08 00:45:54 +00005532 specified explicitly, and such use isn't intended in any case.
5533
5534 Args:
5535 filename: The name of the current file.
5536 clean_lines: A CleansedLines instance containing the file.
5537 linenum: The number of the line to check.
5538 error: The function to call with any errors found.
5539 """
erg@google.com2aa59982013-10-28 19:09:25 +00005540 line = clean_lines.elided[linenum]
erg@google.com8a95ecc2011-09-08 00:45:54 +00005541 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5542 if match:
5543 error(filename, linenum, 'build/explicit_make_pair',
5544 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00005545 'For C++11-compatibility, omit template arguments from make_pair'
5546 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005547
5548
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005549def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5550 """Check if line contains a redundant "virtual" function-specifier.
5551
5552 Args:
5553 filename: The name of the current file.
5554 clean_lines: A CleansedLines instance containing the file.
5555 linenum: The number of the line to check.
5556 error: The function to call with any errors found.
5557 """
5558 # Look for "virtual" on current line.
5559 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005560 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005561 if not virtual: return
5562
avakulenko@google.com554223d2014-12-04 22:00:20 +00005563 # Ignore "virtual" keywords that are near access-specifiers. These
5564 # are only used in class base-specifier and do not apply to member
5565 # functions.
5566 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or
5567 Match(r'^\s+(public|protected|private)\b', virtual.group(3))):
5568 return
5569
5570 # Ignore the "virtual" keyword from virtual base classes. Usually
5571 # there is a column on the same line in these cases (virtual base
5572 # classes are rare in google3 because multiple inheritance is rare).
5573 if Match(r'^.*[^:]:[^:].*$', line): return
5574
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005575 # Look for the next opening parenthesis. This is the start of the
5576 # parameter list (possibly on the next line shortly after virtual).
5577 # TODO(unknown): doesn't work if there are virtual functions with
5578 # decltype() or other things that use parentheses, but csearch suggests
5579 # that this is rare.
5580 end_col = -1
5581 end_line = -1
avakulenko@google.com554223d2014-12-04 22:00:20 +00005582 start_col = len(virtual.group(2))
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005583 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5584 line = clean_lines.elided[start_line][start_col:]
5585 parameter_list = Match(r'^([^(]*)\(', line)
5586 if parameter_list:
5587 # Match parentheses to find the end of the parameter list
5588 (_, end_line, end_col) = CloseExpression(
5589 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5590 break
5591 start_col = 0
5592
5593 if end_col < 0:
5594 return # Couldn't find end of parameter list, give up
5595
5596 # Look for "override" or "final" after the parameter list
5597 # (possibly on the next few lines).
5598 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5599 line = clean_lines.elided[i][end_col:]
5600 match = Search(r'\b(override|final)\b', line)
5601 if match:
5602 error(filename, linenum, 'readability/inheritance', 4,
5603 ('"virtual" is redundant since function is '
5604 'already declared as "%s"' % match.group(1)))
5605
5606 # Set end_col to check whole lines after we are done with the
5607 # first line.
5608 end_col = 0
5609 if Search(r'[^\w]\s*$', line):
5610 break
5611
5612
5613def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5614 """Check if line contains a redundant "override" or "final" virt-specifier.
5615
5616 Args:
5617 filename: The name of the current file.
5618 clean_lines: A CleansedLines instance containing the file.
5619 linenum: The number of the line to check.
5620 error: The function to call with any errors found.
5621 """
avakulenko@google.com554223d2014-12-04 22:00:20 +00005622 # Look for closing parenthesis nearby. We need one to confirm where
5623 # the declarator ends and where the virt-specifier starts to avoid
5624 # false positives.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005625 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005626 declarator_end = line.rfind(')')
5627 if declarator_end >= 0:
5628 fragment = line[declarator_end:]
5629 else:
5630 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0:
5631 fragment = line
5632 else:
5633 return
5634
5635 # Check that at most one of "override" or "final" is present, not both
5636 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005637 error(filename, linenum, 'readability/inheritance', 4,
5638 ('"override" is redundant since function is '
5639 'already declared as "final"'))
5640
5641
5642
5643
5644# Returns true if we are at a new block, and it is directly
5645# inside of a namespace.
5646def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5647 """Checks that the new block is directly in a namespace.
5648
5649 Args:
5650 nesting_state: The _NestingState object that contains info about our state.
5651 is_forward_declaration: If the class is a forward declared class.
5652 Returns:
5653 Whether or not the new block is directly in a namespace.
5654 """
5655 if is_forward_declaration:
5656 if len(nesting_state.stack) >= 1 and (
5657 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5658 return True
5659 else:
5660 return False
5661
5662 return (len(nesting_state.stack) > 1 and
5663 nesting_state.stack[-1].check_namespace_indentation and
5664 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5665
5666
5667def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5668 raw_lines_no_comments, linenum):
5669 """This method determines if we should apply our namespace indentation check.
5670
5671 Args:
5672 nesting_state: The current nesting state.
5673 is_namespace_indent_item: If we just put a new class on the stack, True.
5674 If the top of the stack is not a class, or we did not recently
5675 add the class, False.
5676 raw_lines_no_comments: The lines without the comments.
5677 linenum: The current line number we are processing.
5678
5679 Returns:
5680 True if we should apply our namespace indentation check. Currently, it
5681 only works for classes and namespaces inside of a namespace.
5682 """
5683
5684 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5685 linenum)
5686
5687 if not (is_namespace_indent_item or is_forward_declaration):
5688 return False
5689
5690 # If we are in a macro, we do not want to check the namespace indentation.
5691 if IsMacroDefinition(raw_lines_no_comments, linenum):
5692 return False
5693
5694 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5695
5696
5697# Call this method if the line is directly inside of a namespace.
5698# If the line above is blank (excluding comments) or the start of
5699# an inner namespace, it cannot be indented.
5700def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5701 error):
5702 line = raw_lines_no_comments[linenum]
5703 if Match(r'^\s+', line):
5704 error(filename, linenum, 'runtime/indentation_namespace', 4,
5705 'Do not indent within a namespace')
erg@google.com8a95ecc2011-09-08 00:45:54 +00005706
5707
erg@google.comd350fe52013-01-14 17:51:48 +00005708def ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005709 include_state, function_state, nesting_state, error,
5710 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005711 """Processes a single line in the file.
5712
5713 Args:
5714 filename: Filename of the file that is being processed.
5715 file_extension: The extension (dot not included) of the file.
5716 clean_lines: An array of strings, each representing a line of the file,
5717 with comments stripped.
5718 line: Number of line being processed.
5719 include_state: An _IncludeState instance in which the headers are inserted.
5720 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005721 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00005722 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005723 error: A callable to which errors are reported, which takes 4 arguments:
5724 filename, line number, error level, and message
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005725 extra_check_functions: An array of additional check functions that will be
5726 run on each source line. Each function takes 4
5727 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005728 """
5729 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00005730 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005731 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005732 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5733 error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005734 if nesting_state.InAsmBlock(): return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005735 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005736 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005737 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005738 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
erg@google.comfd5da632013-10-25 17:39:45 +00005739 nesting_state, error)
erg@google.comc6671232013-10-25 21:44:03 +00005740 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005741 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00005742 nesting_state, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005743 CheckVlogArguments(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005744 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00005745 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005746 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005747 CheckRedundantVirtual(filename, clean_lines, line, error)
5748 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005749 for check_fn in extra_check_functions:
5750 check_fn(filename, clean_lines, line, error)
erg@google.com7430eef2014-07-28 22:33:46 +00005751
avakulenko@google.com02af6282014-06-04 18:53:25 +00005752def FlagCxx11Features(filename, clean_lines, linenum, error):
5753 """Flag those c++11 features that we only allow in certain places.
5754
5755 Args:
5756 filename: The name of the current file.
5757 clean_lines: A CleansedLines instance containing the file.
5758 linenum: The number of the line to check.
5759 error: The function to call with any errors found.
5760 """
5761 line = clean_lines.elided[linenum]
5762
avakulenko@google.com02af6282014-06-04 18:53:25 +00005763 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005764
5765 # Flag unapproved C++ TR1 headers.
5766 if include and include.group(1).startswith('tr1/'):
5767 error(filename, linenum, 'build/c++tr1', 5,
5768 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1))
5769
5770 # Flag unapproved C++11 headers.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005771 if include and include.group(1) in ('cfenv',
5772 'condition_variable',
5773 'fenv.h',
5774 'future',
5775 'mutex',
5776 'thread',
5777 'chrono',
5778 'ratio',
5779 'regex',
5780 'system_error',
5781 ):
5782 error(filename, linenum, 'build/c++11', 5,
5783 ('<%s> is an unapproved C++11 header.') % include.group(1))
5784
5785 # The only place where we need to worry about C++11 keywords and library
5786 # features in preprocessor directives is in macro definitions.
5787 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5788
5789 # These are classes and free functions. The classes are always
5790 # mentioned as std::*, but we only catch the free functions if
5791 # they're not found by ADL. They're alphabetical by header.
5792 for top_name in (
5793 # type_traits
5794 'alignment_of',
5795 'aligned_union',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005796 ):
5797 if Search(r'\bstd::%s\b' % top_name, line):
5798 error(filename, linenum, 'build/c++11', 5,
5799 ('std::%s is an unapproved C++11 class or function. Send c-style '
5800 'an example of where it would make your code more readable, and '
5801 'they may let you use it.') % top_name)
5802
5803
Alex Vakulenko01e47232016-05-06 13:54:15 -07005804def FlagCxx14Features(filename, clean_lines, linenum, error):
5805 """Flag those C++14 features that we restrict.
5806
5807 Args:
5808 filename: The name of the current file.
5809 clean_lines: A CleansedLines instance containing the file.
5810 linenum: The number of the line to check.
5811 error: The function to call with any errors found.
5812 """
5813 line = clean_lines.elided[linenum]
5814
5815 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5816
5817 # Flag unapproved C++14 headers.
5818 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):
5819 error(filename, linenum, 'build/c++14', 5,
5820 ('<%s> is an unapproved C++14 header.') % include.group(1))
5821
5822
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005823def ProcessFileData(filename, file_extension, lines, error,
5824 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005825 """Performs lint checks and reports any errors to the given error function.
5826
5827 Args:
5828 filename: Filename of the file that is being processed.
5829 file_extension: The extension (dot not included) of the file.
5830 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00005831 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005832 error: A callable to which errors are reported, which takes 4 arguments:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005833 filename, line number, error level, and message
5834 extra_check_functions: An array of additional check functions that will be
5835 run on each source line. Each function takes 4
5836 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005837 """
5838 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5839 ['// marker so line numbers end in a known way'])
5840
5841 include_state = _IncludeState()
5842 function_state = _FunctionState()
avakulenko@google.com02af6282014-06-04 18:53:25 +00005843 nesting_state = NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005844
erg+personal@google.com05189642010-04-30 20:43:03 +00005845 ResetNolintSuppressions()
5846
erg@google.com4e00b9a2009-01-12 23:05:11 +00005847 CheckForCopyright(filename, lines, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005848 ProcessGlobalSuppresions(lines)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005849 RemoveMultiLineComments(filename, lines, error)
5850 clean_lines = CleansedLines(lines)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005851
LukeCz7197a242016-09-24 13:27:35 -05005852 if IsHeaderExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005853 CheckForHeaderGuard(filename, clean_lines, error)
5854
erg@google.com4e00b9a2009-01-12 23:05:11 +00005855 for line in xrange(clean_lines.NumLines()):
5856 ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005857 include_state, function_state, nesting_state, error,
5858 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005859 FlagCxx11Features(filename, clean_lines, line, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005860 nesting_state.CheckCompletedBlocks(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005861
5862 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005863
avakulenko@google.com554223d2014-12-04 22:00:20 +00005864 # Check that the .cc file has included its header if it exists.
Alex Vakulenko01e47232016-05-06 13:54:15 -07005865 if _IsSourceExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005866 CheckHeaderFileIncluded(filename, include_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005867
5868 # We check here rather than inside ProcessLine so that we see raw
5869 # lines rather than "cleaned" lines.
erg@google.com2aa59982013-10-28 19:09:25 +00005870 CheckForBadCharacters(filename, lines, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005871
5872 CheckForNewlineAtEOF(filename, lines, error)
5873
erg@google.com7430eef2014-07-28 22:33:46 +00005874def ProcessConfigOverrides(filename):
5875 """ Loads the configuration files and processes the config overrides.
5876
5877 Args:
5878 filename: The name of the file being processed by the linter.
5879
5880 Returns:
5881 False if the current |filename| should not be processed further.
5882 """
5883
5884 abs_filename = os.path.abspath(filename)
5885 cfg_filters = []
5886 keep_looking = True
5887 while keep_looking:
5888 abs_path, base_name = os.path.split(abs_filename)
5889 if not base_name:
5890 break # Reached the root directory.
5891
5892 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5893 abs_filename = abs_path
5894 if not os.path.isfile(cfg_file):
5895 continue
5896
5897 try:
5898 with open(cfg_file) as file_handle:
5899 for line in file_handle:
5900 line, _, _ = line.partition('#') # Remove comments.
5901 if not line.strip():
5902 continue
5903
5904 name, _, val = line.partition('=')
5905 name = name.strip()
5906 val = val.strip()
5907 if name == 'set noparent':
5908 keep_looking = False
5909 elif name == 'filter':
5910 cfg_filters.append(val)
5911 elif name == 'exclude_files':
5912 # When matching exclude_files pattern, use the base_name of
5913 # the current file name or the directory name we are processing.
5914 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5915 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5916 # file's "exclude_files" filter is meant to be checked against "bar"
5917 # and not "baz" nor "bar/baz.cc".
5918 if base_name:
5919 pattern = re.compile(val)
5920 if pattern.match(base_name):
5921 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5922 'File path component "%s" matches '
5923 'pattern "%s"\n' %
5924 (filename, cfg_file, base_name, val))
5925 return False
avakulenko@google.com310681b2014-08-22 19:38:55 +00005926 elif name == 'linelength':
5927 global _line_length
5928 try:
5929 _line_length = int(val)
5930 except ValueError:
5931 sys.stderr.write('Line length must be numeric.')
Fabian Guera2322e4f2016-05-01 17:36:30 +02005932 elif name == 'root':
5933 global _root
5934 _root = val
LukeCz7197a242016-09-24 13:27:35 -05005935 elif name == 'headers':
5936 ProcessHppHeadersOption(val)
erg@google.com7430eef2014-07-28 22:33:46 +00005937 else:
5938 sys.stderr.write(
5939 'Invalid configuration option (%s) in file %s\n' %
5940 (name, cfg_file))
5941
5942 except IOError:
5943 sys.stderr.write(
5944 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5945 keep_looking = False
5946
5947 # Apply all the accumulated filters in reverse order (top-level directory
5948 # config options having the least priority).
5949 for filter in reversed(cfg_filters):
5950 _AddFilters(filter)
5951
5952 return True
5953
avakulenko@google.com02af6282014-06-04 18:53:25 +00005954
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005955def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005956 """Does google-lint on a single file.
5957
5958 Args:
5959 filename: The name of the file to parse.
5960
5961 vlevel: The level of errors to report. Every error of confidence
5962 >= verbose_level will be reported. 0 is a good default.
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005963
5964 extra_check_functions: An array of additional check functions that will be
5965 run on each source line. Each function takes 4
5966 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005967 """
5968
5969 _SetVerboseLevel(vlevel)
erg@google.com7430eef2014-07-28 22:33:46 +00005970 _BackupFilters()
5971
5972 if not ProcessConfigOverrides(filename):
5973 _RestoreFilters()
5974 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005975
avakulenko@google.com02af6282014-06-04 18:53:25 +00005976 lf_lines = []
5977 crlf_lines = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00005978 try:
5979 # Support the UNIX convention of using "-" for stdin. Note that
5980 # we are not opening the file with universal newline support
5981 # (which codecs doesn't support anyway), so the resulting lines do
5982 # contain trailing '\r' characters if we are reading a file that
5983 # has CRLF endings.
5984 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.com02af6282014-06-04 18:53:25 +00005985 # below.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005986 if filename == '-':
5987 lines = codecs.StreamReaderWriter(sys.stdin,
5988 codecs.getreader('utf8'),
5989 codecs.getwriter('utf8'),
5990 'replace').read().split('\n')
5991 else:
5992 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
5993
erg@google.com4e00b9a2009-01-12 23:05:11 +00005994 # Remove trailing '\r'.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005995 # The -1 accounts for the extra trailing blank line we get from split()
5996 for linenum in range(len(lines) - 1):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005997 if lines[linenum].endswith('\r'):
5998 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.com02af6282014-06-04 18:53:25 +00005999 crlf_lines.append(linenum + 1)
6000 else:
6001 lf_lines.append(linenum + 1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006002
6003 except IOError:
6004 sys.stderr.write(
6005 "Skipping input '%s': Can't open for reading\n" % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006006 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006007 return
6008
6009 # Note, if no dot is found, this will give the entire filename as the ext.
6010 file_extension = filename[filename.rfind('.') + 1:]
6011
6012 # When reading from stdin, the extension is unknown, so no cpplint tests
6013 # should rely on the extension.
erg@google.com19680272013-12-16 22:48:54 +00006014 if filename != '-' and file_extension not in _valid_extensions:
erg@google.com2aa59982013-10-28 19:09:25 +00006015 sys.stderr.write('Ignoring %s; not a valid file name '
erg@google.com19680272013-12-16 22:48:54 +00006016 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006017 else:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00006018 ProcessFileData(filename, file_extension, lines, Error,
6019 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00006020
6021 # If end-of-line sequences are a mix of LF and CR-LF, issue
6022 # warnings on the lines with CR.
6023 #
6024 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
6025 # since critique can handle these just fine, and the style guide
6026 # doesn't dictate a particular end of line sequence.
6027 #
6028 # We can't depend on os.linesep to determine what the desired
6029 # end-of-line sequence should be, since that will return the
6030 # server-side end-of-line sequence.
6031 if lf_lines and crlf_lines:
6032 # Warn on every line with CR. An alternative approach might be to
6033 # check whether the file is mostly CRLF or just LF, and warn on the
6034 # minority, we bias toward LF here since most tools prefer LF.
6035 for linenum in crlf_lines:
6036 Error(filename, linenum, 'whitespace/newline', 1,
6037 'Unexpected \\r (^M) found; better to use only \\n')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006038
6039 sys.stderr.write('Done processing %s\n' % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00006040 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006041
6042
6043def PrintUsage(message):
6044 """Prints a brief usage string and exits, optionally with an error message.
6045
6046 Args:
6047 message: The optional error message.
6048 """
6049 sys.stderr.write(_USAGE)
6050 if message:
6051 sys.exit('\nFATAL ERROR: ' + message)
6052 else:
6053 sys.exit(1)
6054
6055
6056def PrintCategories():
6057 """Prints a list of all the error-categories used by error messages.
6058
6059 These are the categories used to filter messages via --filter.
6060 """
erg+personal@google.com05189642010-04-30 20:43:03 +00006061 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006062 sys.exit(0)
6063
6064
6065def ParseArguments(args):
6066 """Parses the command line arguments.
6067
6068 This may set the output format and verbosity level as side-effects.
6069
6070 Args:
6071 args: The command line arguments:
6072
6073 Returns:
6074 The list of filenames to lint.
6075 """
6076 try:
6077 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00006078 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00006079 'filter=',
erg@google.comab53edf2013-11-05 22:23:37 +00006080 'root=',
erg@google.com19680272013-12-16 22:48:54 +00006081 'linelength=',
LukeCz7197a242016-09-24 13:27:35 -05006082 'extensions=',
6083 'headers='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00006084 except getopt.GetoptError:
6085 PrintUsage('Invalid arguments.')
6086
6087 verbosity = _VerboseLevel()
6088 output_format = _OutputFormat()
6089 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00006090 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00006091
6092 for (opt, val) in opts:
6093 if opt == '--help':
6094 PrintUsage(None)
6095 elif opt == '--output':
erg@google.comc6671232013-10-25 21:44:03 +00006096 if val not in ('emacs', 'vs7', 'eclipse'):
erg@google.com02c27fd2013-05-28 21:34:34 +00006097 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006098 output_format = val
6099 elif opt == '--verbose':
6100 verbosity = int(val)
6101 elif opt == '--filter':
6102 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00006103 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00006104 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00006105 elif opt == '--counting':
6106 if val not in ('total', 'toplevel', 'detailed'):
6107 PrintUsage('Valid counting options are total, toplevel, and detailed')
6108 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00006109 elif opt == '--root':
6110 global _root
6111 _root = val
erg@google.comab53edf2013-11-05 22:23:37 +00006112 elif opt == '--linelength':
6113 global _line_length
6114 try:
6115 _line_length = int(val)
6116 except ValueError:
6117 PrintUsage('Line length must be digits.')
erg@google.com19680272013-12-16 22:48:54 +00006118 elif opt == '--extensions':
6119 global _valid_extensions
6120 try:
6121 _valid_extensions = set(val.split(','))
6122 except ValueError:
6123 PrintUsage('Extensions must be comma seperated list.')
LukeCz7197a242016-09-24 13:27:35 -05006124 elif opt == '--headers':
6125 ProcessHppHeadersOption(val)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006126
6127 if not filenames:
6128 PrintUsage('No files were specified.')
6129
6130 _SetOutputFormat(output_format)
6131 _SetVerboseLevel(verbosity)
6132 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00006133 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006134
6135 return filenames
6136
6137
6138def main():
6139 filenames = ParseArguments(sys.argv[1:])
6140
6141 # Change stderr to write with replacement characters so we don't die
6142 # if we try to print something containing non-ASCII characters.
6143 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6144 codecs.getreader('utf8'),
6145 codecs.getwriter('utf8'),
6146 'replace')
6147
erg@google.coma868d2d2009-10-09 21:18:45 +00006148 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006149 for filename in filenames:
6150 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00006151 _cpplint_state.PrintErrorCounts()
6152
erg@google.com4e00b9a2009-01-12 23:05:11 +00006153 sys.exit(_cpplint_state.error_count > 0)
6154
6155
6156if __name__ == '__main__':
6157 main()