blob: a1b5642f2f56a44dbc66c9d53313c23c685ba26b [file] [log] [blame]
avakulenko@google.com554223d2014-12-04 22:00:20 +00001#!/usr/bin/env python
erg@google.com4e00b9a2009-01-12 23:05:11 +00002#
erg@google.com8f91ab22011-09-06 21:04:45 +00003# Copyright (c) 2009 Google Inc. All rights reserved.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004#
erg@google.com969161c2009-06-26 22:06:46 +00005# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
erg@google.com4e00b9a2009-01-12 23:05:11 +00008#
erg@google.com969161c2009-06-26 22:06:46 +00009# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
erg@google.com4e00b9a2009-01-12 23:05:11 +000018#
erg@google.com969161c2009-06-26 22:06:46 +000019# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
erg@google.com4e00b9a2009-01-12 23:05:11 +000030
erg@google.com4e00b9a2009-01-12 23:05:11 +000031"""Does google-lint on c++ files.
32
33The goal of this script is to identify places in the code that *may*
34be in non-compliance with google style. It does not attempt to fix
35up these problems -- the point is to educate. It does also not
36attempt to find all problems, or to ensure that everything it does
37find is legitimately a problem.
38
39In particular, we can get very confused by /* and // inside strings!
40We do a small hack, which is to ignore //'s with "'s after them on the
41same line, but it is far from perfect (in either direction).
42"""
43
44import codecs
erg@google.comd350fe52013-01-14 17:51:48 +000045import copy
erg@google.com4e00b9a2009-01-12 23:05:11 +000046import getopt
47import math # for log
48import os
49import re
50import sre_compile
51import string
52import sys
53import unicodedata
54
55
56_USAGE = """
57Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
erg@google.comab53edf2013-11-05 22:23:37 +000058 [--counting=total|toplevel|detailed] [--root=subdir]
59 [--linelength=digits]
erg@google.com4e00b9a2009-01-12 23:05:11 +000060 <file> [file] ...
61
62 The style guidelines this tries to follow are those in
Ackermann Yuriy79692902016-04-01 21:41:34 +130063 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
erg@google.com4e00b9a2009-01-12 23:05:11 +000064
65 Every problem is given a confidence score from 1-5, with 5 meaning we are
66 certain of the problem, and 1 meaning it could be a legitimate construct.
67 This will miss some errors, and is not a substitute for a code review.
68
erg+personal@google.com05189642010-04-30 20:43:03 +000069 To suppress false-positive errors of a certain category, add a
70 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*)
71 suppresses errors of all categories on that line.
erg@google.com4e00b9a2009-01-12 23:05:11 +000072
73 The files passed in will be linted; at least one file must be provided.
erg@google.com19680272013-12-16 22:48:54 +000074 Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the
75 extensions with the --extensions flag.
erg@google.com4e00b9a2009-01-12 23:05:11 +000076
77 Flags:
78
79 output=vs7
80 By default, the output is formatted to ease emacs parsing. Visual Studio
81 compatible output (vs7) may also be used. Other formats are unsupported.
82
83 verbose=#
84 Specify a number 0-5 to restrict errors to certain verbosity levels.
85
86 filter=-x,+y,...
87 Specify a comma-separated list of category-filters to apply: only
88 error messages whose category names pass the filters will be printed.
89 (Category names are printed with the message and look like
90 "[whitespace/indent]".) Filters are evaluated left to right.
91 "-FOO" and "FOO" means "do not print categories that start with FOO".
92 "+FOO" means "do print categories that start with FOO".
93
94 Examples: --filter=-whitespace,+whitespace/braces
95 --filter=whitespace,runtime/printf,+runtime/printf_format
96 --filter=-,+build/include_what_you_use
97
98 To see a list of all the categories used in cpplint, pass no arg:
99 --filter=
erg@google.coma868d2d2009-10-09 21:18:45 +0000100
101 counting=total|toplevel|detailed
102 The total number of errors found is always printed. If
103 'toplevel' is provided, then the count of errors in each of
104 the top-level categories like 'build' and 'whitespace' will
105 also be printed. If 'detailed' is provided, then a count
106 is provided for each category like 'build/class'.
erg@google.com4d70a882013-04-16 21:06:32 +0000107
108 root=subdir
109 The root directory used for deriving header guard CPP variable.
110 By default, the header guard CPP variable is calculated as the relative
111 path to the directory that contains .git, .hg, or .svn. When this flag
112 is specified, the relative path is calculated from the specified
113 directory. If the specified directory does not exist, this flag is
114 ignored.
115
116 Examples:
avakulenko@google.com02af6282014-06-04 18:53:25 +0000117 Assuming that src/.git exists, the header guard CPP variables for
erg@google.com4d70a882013-04-16 21:06:32 +0000118 src/chrome/browser/ui/browser.h are:
119
120 No flag => CHROME_BROWSER_UI_BROWSER_H_
121 --root=chrome => BROWSER_UI_BROWSER_H_
122 --root=chrome/browser => UI_BROWSER_H_
erg@google.comab53edf2013-11-05 22:23:37 +0000123
124 linelength=digits
125 This is the allowed line length for the project. The default value is
126 80 characters.
127
128 Examples:
129 --linelength=120
erg@google.com19680272013-12-16 22:48:54 +0000130
131 extensions=extension,extension,...
132 The allowed file extensions that cpplint will check
133
134 Examples:
135 --extensions=hpp,cpp
erg@google.com7430eef2014-07-28 22:33:46 +0000136
137 cpplint.py supports per-directory configurations specified in CPPLINT.cfg
138 files. CPPLINT.cfg file can contain a number of key=value pairs.
139 Currently the following options are supported:
140
141 set noparent
142 filter=+filter1,-filter2,...
143 exclude_files=regex
avakulenko@google.com310681b2014-08-22 19:38:55 +0000144 linelength=80
erg@google.com7430eef2014-07-28 22:33:46 +0000145
146 "set noparent" option prevents cpplint from traversing directory tree
147 upwards looking for more .cfg files in parent directories. This option
148 is usually placed in the top-level project directory.
149
150 The "filter" option is similar in function to --filter flag. It specifies
151 message filters in addition to the |_DEFAULT_FILTERS| and those specified
152 through --filter command-line flag.
153
154 "exclude_files" allows to specify a regular expression to be matched against
155 a file name. If the expression matches, the file is skipped and not run
156 through liner.
157
avakulenko@google.com310681b2014-08-22 19:38:55 +0000158 "linelength" allows to specify the allowed line length for the project.
159
erg@google.com7430eef2014-07-28 22:33:46 +0000160 CPPLINT.cfg has an effect on files in the same directory and all
161 sub-directories, unless overridden by a nested configuration file.
162
163 Example file:
164 filter=-build/include_order,+build/include_alpha
165 exclude_files=.*\.cc
166
167 The above example disables build/include_order warning and enables
168 build/include_alpha as well as excludes all .cc from being
169 processed by linter, in the current directory (where the .cfg
170 file is located) and all sub-directories.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000171"""
172
173# We categorize each error message we print. Here are the categories.
174# We want an explicit list so we can list them all in cpplint --filter=.
175# If you add a new error message with a new category, add it to the list
176# here! cpplint_unittest.py should tell you if you forget to do this.
erg+personal@google.com05189642010-04-30 20:43:03 +0000177_ERROR_CATEGORIES = [
avakulenko@google.com554223d2014-12-04 22:00:20 +0000178 'build/class',
179 'build/c++11',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700180 'build/c++14',
181 'build/c++tr1',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000182 'build/deprecated',
183 'build/endif_comment',
184 'build/explicit_make_pair',
185 'build/forward_decl',
186 'build/header_guard',
187 'build/include',
188 'build/include_alpha',
189 'build/include_order',
190 'build/include_what_you_use',
191 'build/namespaces',
192 'build/printf_format',
193 'build/storage_class',
194 'legal/copyright',
195 'readability/alt_tokens',
196 'readability/braces',
197 'readability/casting',
198 'readability/check',
199 'readability/constructors',
200 'readability/fn_size',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000201 'readability/inheritance',
202 'readability/multiline_comment',
203 'readability/multiline_string',
204 'readability/namespace',
205 'readability/nolint',
206 'readability/nul',
207 'readability/strings',
208 'readability/todo',
209 'readability/utf8',
210 'runtime/arrays',
211 'runtime/casting',
212 'runtime/explicit',
213 'runtime/int',
214 'runtime/init',
215 'runtime/invalid_increment',
216 'runtime/member_string_references',
217 'runtime/memset',
218 'runtime/indentation_namespace',
219 'runtime/operator',
220 'runtime/printf',
221 'runtime/printf_format',
222 'runtime/references',
223 'runtime/string',
224 'runtime/threadsafe_fn',
225 'runtime/vlog',
226 'whitespace/blank_line',
227 'whitespace/braces',
228 'whitespace/comma',
229 'whitespace/comments',
230 'whitespace/empty_conditional_body',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700231 'whitespace/empty_if_body',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000232 'whitespace/empty_loop_body',
233 'whitespace/end_of_line',
234 'whitespace/ending_newline',
235 'whitespace/forcolon',
236 'whitespace/indent',
237 'whitespace/line_length',
238 'whitespace/newline',
239 'whitespace/operators',
240 'whitespace/parens',
241 'whitespace/semicolon',
242 'whitespace/tab',
243 'whitespace/todo',
244 ]
245
246# These error categories are no longer enforced by cpplint, but for backwards-
247# compatibility they may still appear in NOLINT comments.
248_LEGACY_ERROR_CATEGORIES = [
249 'readability/streams',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700250 'readability/function',
avakulenko@google.com554223d2014-12-04 22:00:20 +0000251 ]
erg@google.com4e00b9a2009-01-12 23:05:11 +0000252
avakulenko@google.com02af6282014-06-04 18:53:25 +0000253# The default state of the category filter. This is overridden by the --filter=
erg@google.come35f7652009-06-19 20:52:09 +0000254# flag. By default all errors are on, so only add here categories that should be
255# off by default (i.e., categories that must be enabled by the --filter= flags).
256# All entries here should start with a '-' or '+', as in the --filter= flag.
erg@google.com8a95ecc2011-09-08 00:45:54 +0000257_DEFAULT_FILTERS = ['-build/include_alpha']
erg@google.come35f7652009-06-19 20:52:09 +0000258
Alex Vakulenko01e47232016-05-06 13:54:15 -0700259# The default list of categories suppressed for C (not C++) files.
260_DEFAULT_C_SUPPRESSED_CATEGORIES = [
261 'readability/casting',
262 ]
263
264# The default list of categories suppressed for Linux Kernel files.
265_DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [
266 'whitespace/tab',
267 ]
268
erg@google.com4e00b9a2009-01-12 23:05:11 +0000269# We used to check for high-bit characters, but after much discussion we
270# decided those were OK, as long as they were in UTF-8 and didn't represent
erg@google.com8a95ecc2011-09-08 00:45:54 +0000271# hard-coded international strings, which belong in a separate i18n file.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000272
erg@google.comfd5da632013-10-25 17:39:45 +0000273# C++ headers
erg@google.com4e00b9a2009-01-12 23:05:11 +0000274_CPP_HEADERS = frozenset([
erg@google.comfd5da632013-10-25 17:39:45 +0000275 # Legacy
276 'algobase.h',
277 'algo.h',
278 'alloc.h',
279 'builtinbuf.h',
280 'bvector.h',
281 'complex.h',
282 'defalloc.h',
283 'deque.h',
284 'editbuf.h',
285 'fstream.h',
286 'function.h',
287 'hash_map',
288 'hash_map.h',
289 'hash_set',
290 'hash_set.h',
291 'hashtable.h',
292 'heap.h',
293 'indstream.h',
294 'iomanip.h',
295 'iostream.h',
296 'istream.h',
297 'iterator.h',
298 'list.h',
299 'map.h',
300 'multimap.h',
301 'multiset.h',
302 'ostream.h',
303 'pair.h',
304 'parsestream.h',
305 'pfstream.h',
306 'procbuf.h',
307 'pthread_alloc',
308 'pthread_alloc.h',
309 'rope',
310 'rope.h',
311 'ropeimpl.h',
312 'set.h',
313 'slist',
314 'slist.h',
315 'stack.h',
316 'stdiostream.h',
317 'stl_alloc.h',
318 'stl_relops.h',
319 'streambuf.h',
320 'stream.h',
321 'strfile.h',
322 'strstream.h',
323 'tempbuf.h',
324 'tree.h',
325 'type_traits.h',
326 'vector.h',
327 # 17.6.1.2 C++ library headers
328 'algorithm',
329 'array',
330 'atomic',
331 'bitset',
332 'chrono',
333 'codecvt',
334 'complex',
335 'condition_variable',
336 'deque',
337 'exception',
338 'forward_list',
339 'fstream',
340 'functional',
341 'future',
342 'initializer_list',
343 'iomanip',
344 'ios',
345 'iosfwd',
346 'iostream',
347 'istream',
348 'iterator',
349 'limits',
350 'list',
351 'locale',
352 'map',
353 'memory',
354 'mutex',
355 'new',
356 'numeric',
357 'ostream',
358 'queue',
359 'random',
360 'ratio',
361 'regex',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700362 'scoped_allocator',
erg@google.comfd5da632013-10-25 17:39:45 +0000363 'set',
364 'sstream',
365 'stack',
366 'stdexcept',
367 'streambuf',
368 'string',
369 'strstream',
370 'system_error',
371 'thread',
372 'tuple',
373 'typeindex',
374 'typeinfo',
375 'type_traits',
376 'unordered_map',
377 'unordered_set',
378 'utility',
erg@google.com5d00c562013-07-12 19:57:05 +0000379 'valarray',
erg@google.comfd5da632013-10-25 17:39:45 +0000380 'vector',
381 # 17.6.1.2 C++ headers for C library facilities
382 'cassert',
383 'ccomplex',
384 'cctype',
385 'cerrno',
386 'cfenv',
387 'cfloat',
388 'cinttypes',
389 'ciso646',
390 'climits',
391 'clocale',
392 'cmath',
393 'csetjmp',
394 'csignal',
395 'cstdalign',
396 'cstdarg',
397 'cstdbool',
398 'cstddef',
399 'cstdint',
400 'cstdio',
401 'cstdlib',
402 'cstring',
403 'ctgmath',
404 'ctime',
405 'cuchar',
406 'cwchar',
407 'cwctype',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000408 ])
409
Alex Vakulenko01e47232016-05-06 13:54:15 -0700410# Type names
411_TYPES = re.compile(
412 r'^(?:'
413 # [dcl.type.simple]
414 r'(char(16_t|32_t)?)|wchar_t|'
415 r'bool|short|int|long|signed|unsigned|float|double|'
416 # [support.types]
417 r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|'
418 # [cstdint.syn]
419 r'(u?int(_fast|_least)?(8|16|32|64)_t)|'
420 r'(u?int(max|ptr)_t)|'
421 r')$')
422
avakulenko@google.com02af6282014-06-04 18:53:25 +0000423
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000424# These headers are excluded from [build/include] and [build/include_order]
425# checks:
426# - Anything not following google file name conventions (containing an
427# uppercase character, such as Python.h or nsStringAPI.h, for example).
428# - Lua headers.
429_THIRD_PARTY_HEADERS_PATTERN = re.compile(
430 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$')
431
Alex Vakulenko01e47232016-05-06 13:54:15 -0700432# Pattern for matching FileInfo.BaseName() against test file name
433_TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$'
434
435# Pattern that matches only complete whitespace, possibly across multiple lines.
436_EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000437
erg@google.com4e00b9a2009-01-12 23:05:11 +0000438# Assertion macros. These are defined in base/logging.h and
Alex Vakulenko01e47232016-05-06 13:54:15 -0700439# testing/base/public/gunit.h.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000440_CHECK_MACROS = [
erg@google.come35f7652009-06-19 20:52:09 +0000441 'DCHECK', 'CHECK',
Alex Vakulenko01e47232016-05-06 13:54:15 -0700442 'EXPECT_TRUE', 'ASSERT_TRUE',
443 'EXPECT_FALSE', 'ASSERT_FALSE',
erg@google.com4e00b9a2009-01-12 23:05:11 +0000444 ]
445
erg@google.come35f7652009-06-19 20:52:09 +0000446# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
erg@google.com4e00b9a2009-01-12 23:05:11 +0000447_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
448
449for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
450 ('>=', 'GE'), ('>', 'GT'),
451 ('<=', 'LE'), ('<', 'LT')]:
erg@google.come35f7652009-06-19 20:52:09 +0000452 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000453 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
454 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
455 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000456
457for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
458 ('>=', 'LT'), ('>', 'LE'),
459 ('<=', 'GT'), ('<', 'GE')]:
460 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
461 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
erg@google.com4e00b9a2009-01-12 23:05:11 +0000462
erg@google.comd350fe52013-01-14 17:51:48 +0000463# Alternative tokens and their replacements. For full list, see section 2.5
464# Alternative tokens [lex.digraph] in the C++ standard.
465#
466# Digraphs (such as '%:') are not included here since it's a mess to
467# match those on a word boundary.
468_ALT_TOKEN_REPLACEMENT = {
469 'and': '&&',
470 'bitor': '|',
471 'or': '||',
472 'xor': '^',
473 'compl': '~',
474 'bitand': '&',
475 'and_eq': '&=',
476 'or_eq': '|=',
477 'xor_eq': '^=',
478 'not': '!',
479 'not_eq': '!='
480 }
481
482# Compile regular expression that matches all the above keywords. The "[ =()]"
483# bit is meant to avoid matching these keywords outside of boolean expressions.
484#
erg@google.comc6671232013-10-25 21:44:03 +0000485# False positives include C-style multi-line comments and multi-line strings
486# but those have always been troublesome for cpplint.
erg@google.comd350fe52013-01-14 17:51:48 +0000487_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
488 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
489
erg@google.com4e00b9a2009-01-12 23:05:11 +0000490
491# These constants define types of headers for use with
492# _IncludeState.CheckNextIncludeOrder().
493_C_SYS_HEADER = 1
494_CPP_SYS_HEADER = 2
495_LIKELY_MY_HEADER = 3
496_POSSIBLE_MY_HEADER = 4
497_OTHER_HEADER = 5
498
erg@google.comd350fe52013-01-14 17:51:48 +0000499# These constants define the current inline assembly state
500_NO_ASM = 0 # Outside of inline assembly block
501_INSIDE_ASM = 1 # Inside inline assembly block
502_END_ASM = 2 # Last line of inline assembly block
503_BLOCK_ASM = 3 # The whole block is an inline assembly block
504
505# Match start of assembly blocks
506_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)'
507 r'(?:\s+(volatile|__volatile__))?'
508 r'\s*[{(]')
509
Alex Vakulenko01e47232016-05-06 13:54:15 -0700510# Match strings that indicate we're working on a C (not C++) file.
511_SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|'
512 r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))')
513
514# Match string that indicates we're working on a Linux Kernel file.
515_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
erg@google.com4e00b9a2009-01-12 23:05:11 +0000516
517_regexp_compile_cache = {}
518
erg+personal@google.com05189642010-04-30 20:43:03 +0000519# {str, set(int)}: a map from error categories to sets of linenumbers
520# on which those errors are expected and should be suppressed.
521_error_suppressions = {}
522
erg@google.com4d70a882013-04-16 21:06:32 +0000523# The root directory used for deriving header guard CPP variable.
524# This is set by --root flag.
525_root = None
526
erg@google.comab53edf2013-11-05 22:23:37 +0000527# The allowed line length of files.
528# This is set by --linelength flag.
529_line_length = 80
530
erg@google.com19680272013-12-16 22:48:54 +0000531# The allowed extensions for file names
532# This is set by --extensions flag.
533_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh'])
534
Alex Vakulenko01e47232016-05-06 13:54:15 -0700535# {str, bool}: a map from error categories to booleans which indicate if the
536# category should be suppressed for every line.
537_global_error_suppressions = {}
538
539
erg+personal@google.com05189642010-04-30 20:43:03 +0000540def ParseNolintSuppressions(filename, raw_line, linenum, error):
Alex Vakulenko01e47232016-05-06 13:54:15 -0700541 """Updates the global list of line error-suppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000542
543 Parses any NOLINT comments on the current line, updating the global
544 error_suppressions store. Reports an error if the NOLINT comment
545 was malformed.
546
547 Args:
548 filename: str, the name of the input file.
549 raw_line: str, the line of input text, with comments.
550 linenum: int, the number of the current line.
551 error: function, an error handler.
552 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000553 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line)
erg@google.com8a95ecc2011-09-08 00:45:54 +0000554 if matched:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000555 if matched.group(1):
556 suppressed_line = linenum + 1
557 else:
558 suppressed_line = linenum
559 category = matched.group(2)
erg+personal@google.com05189642010-04-30 20:43:03 +0000560 if category in (None, '(*)'): # => "suppress all"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000561 _error_suppressions.setdefault(None, set()).add(suppressed_line)
erg+personal@google.com05189642010-04-30 20:43:03 +0000562 else:
563 if category.startswith('(') and category.endswith(')'):
564 category = category[1:-1]
565 if category in _ERROR_CATEGORIES:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000566 _error_suppressions.setdefault(category, set()).add(suppressed_line)
avakulenko@google.com554223d2014-12-04 22:00:20 +0000567 elif category not in _LEGACY_ERROR_CATEGORIES:
erg+personal@google.com05189642010-04-30 20:43:03 +0000568 error(filename, linenum, 'readability/nolint', 5,
erg@google.com8a95ecc2011-09-08 00:45:54 +0000569 'Unknown NOLINT error category: %s' % category)
erg+personal@google.com05189642010-04-30 20:43:03 +0000570
571
Alex Vakulenko01e47232016-05-06 13:54:15 -0700572def ProcessGlobalSuppresions(lines):
573 """Updates the list of global error suppressions.
574
575 Parses any lint directives in the file that have global effect.
576
577 Args:
578 lines: An array of strings, each representing a line of the file, with the
579 last element being empty if the file is terminated with a newline.
580 """
581 for line in lines:
582 if _SEARCH_C_FILE.search(line):
583 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES:
584 _global_error_suppressions[category] = True
585 if _SEARCH_KERNEL_FILE.search(line):
586 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES:
587 _global_error_suppressions[category] = True
588
589
erg+personal@google.com05189642010-04-30 20:43:03 +0000590def ResetNolintSuppressions():
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000591 """Resets the set of NOLINT suppressions to empty."""
erg+personal@google.com05189642010-04-30 20:43:03 +0000592 _error_suppressions.clear()
Alex Vakulenko01e47232016-05-06 13:54:15 -0700593 _global_error_suppressions.clear()
erg+personal@google.com05189642010-04-30 20:43:03 +0000594
595
596def IsErrorSuppressedByNolint(category, linenum):
597 """Returns true if the specified error category is suppressed on this line.
598
599 Consults the global error_suppressions map populated by
Alex Vakulenko01e47232016-05-06 13:54:15 -0700600 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions.
erg+personal@google.com05189642010-04-30 20:43:03 +0000601
602 Args:
603 category: str, the category of the error.
604 linenum: int, the current line number.
605 Returns:
Alex Vakulenko01e47232016-05-06 13:54:15 -0700606 bool, True iff the error should be suppressed due to a NOLINT comment or
607 global suppression.
erg+personal@google.com05189642010-04-30 20:43:03 +0000608 """
Alex Vakulenko01e47232016-05-06 13:54:15 -0700609 return (_global_error_suppressions.get(category, False) or
610 linenum in _error_suppressions.get(category, set()) or
erg+personal@google.com05189642010-04-30 20:43:03 +0000611 linenum in _error_suppressions.get(None, set()))
erg@google.com4e00b9a2009-01-12 23:05:11 +0000612
avakulenko@google.com02af6282014-06-04 18:53:25 +0000613
erg@google.com4e00b9a2009-01-12 23:05:11 +0000614def Match(pattern, s):
615 """Matches the string with the pattern, caching the compiled regexp."""
616 # The regexp compilation caching is inlined in both Match and Search for
617 # performance reasons; factoring it out into a separate function turns out
618 # to be noticeably expensive.
erg@google.comc6671232013-10-25 21:44:03 +0000619 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000620 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
621 return _regexp_compile_cache[pattern].match(s)
622
623
erg@google.comfd5da632013-10-25 17:39:45 +0000624def ReplaceAll(pattern, rep, s):
625 """Replaces instances of pattern in a string with a replacement.
626
627 The compiled regex is kept in a cache shared by Match and Search.
628
629 Args:
630 pattern: regex pattern
631 rep: replacement text
632 s: search string
633
634 Returns:
635 string with replacements made (or original string if no replacements)
636 """
637 if pattern not in _regexp_compile_cache:
638 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
639 return _regexp_compile_cache[pattern].sub(rep, s)
640
641
erg@google.com4e00b9a2009-01-12 23:05:11 +0000642def Search(pattern, s):
643 """Searches the string for the pattern, caching the compiled regexp."""
erg@google.comc6671232013-10-25 21:44:03 +0000644 if pattern not in _regexp_compile_cache:
erg@google.com4e00b9a2009-01-12 23:05:11 +0000645 _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
646 return _regexp_compile_cache[pattern].search(s)
647
648
Alex Vakulenko01e47232016-05-06 13:54:15 -0700649def _IsSourceExtension(s):
650 """File extension (excluding dot) matches a source file extension."""
651 return s in ('c', 'cc', 'cpp', 'cxx')
652
653
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000654class _IncludeState(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000655 """Tracks line numbers for includes, and the order in which includes appear.
656
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000657 include_list contains list of lists of (header, line number) pairs.
658 It's a lists of lists rather than just one flat list to make it
659 easier to update across preprocessor boundaries.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000660
661 Call CheckNextIncludeOrder() once for each header in the file, passing
662 in the type constants defined above. Calls in an illegal order will
663 raise an _IncludeError with an appropriate error message.
664
665 """
666 # self._section will move monotonically through this set. If it ever
667 # needs to move backwards, CheckNextIncludeOrder will raise an error.
668 _INITIAL_SECTION = 0
669 _MY_H_SECTION = 1
670 _C_SECTION = 2
671 _CPP_SECTION = 3
672 _OTHER_H_SECTION = 4
673
674 _TYPE_NAMES = {
675 _C_SYS_HEADER: 'C system header',
676 _CPP_SYS_HEADER: 'C++ system header',
677 _LIKELY_MY_HEADER: 'header this file implements',
678 _POSSIBLE_MY_HEADER: 'header this file may implement',
679 _OTHER_HEADER: 'other header',
680 }
681 _SECTION_NAMES = {
682 _INITIAL_SECTION: "... nothing. (This can't be an error.)",
683 _MY_H_SECTION: 'a header this file implements',
684 _C_SECTION: 'C system header',
685 _CPP_SECTION: 'C++ system header',
686 _OTHER_H_SECTION: 'other header',
687 }
688
689 def __init__(self):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000690 self.include_list = [[]]
691 self.ResetSection('')
erg@google.com2aa59982013-10-28 19:09:25 +0000692
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000693 def FindHeader(self, header):
694 """Check if a header has already been included.
695
696 Args:
697 header: header to check.
698 Returns:
699 Line number of previous occurrence, or -1 if the header has not
700 been seen before.
701 """
702 for section_list in self.include_list:
703 for f in section_list:
704 if f[0] == header:
705 return f[1]
706 return -1
707
708 def ResetSection(self, directive):
709 """Reset section checking for preprocessor directive.
710
711 Args:
712 directive: preprocessor directive (e.g. "if", "else").
713 """
erg@google.coma868d2d2009-10-09 21:18:45 +0000714 # The name of the current section.
erg@google.com4e00b9a2009-01-12 23:05:11 +0000715 self._section = self._INITIAL_SECTION
erg@google.coma868d2d2009-10-09 21:18:45 +0000716 # The path of last found header.
717 self._last_header = ''
718
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +0000719 # Update list of includes. Note that we never pop from the
720 # include list.
721 if directive in ('if', 'ifdef', 'ifndef'):
722 self.include_list.append([])
723 elif directive in ('else', 'elif'):
724 self.include_list[-1] = []
725
erg@google.comfd5da632013-10-25 17:39:45 +0000726 def SetLastHeader(self, header_path):
727 self._last_header = header_path
728
erg@google.coma868d2d2009-10-09 21:18:45 +0000729 def CanonicalizeAlphabeticalOrder(self, header_path):
erg@google.com8a95ecc2011-09-08 00:45:54 +0000730 """Returns a path canonicalized for alphabetical comparison.
erg@google.coma868d2d2009-10-09 21:18:45 +0000731
732 - replaces "-" with "_" so they both cmp the same.
733 - removes '-inl' since we don't require them to be after the main header.
734 - lowercase everything, just in case.
735
736 Args:
737 header_path: Path to be canonicalized.
738
739 Returns:
740 Canonicalized path.
741 """
742 return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
743
erg@google.comfd5da632013-10-25 17:39:45 +0000744 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path):
erg@google.coma868d2d2009-10-09 21:18:45 +0000745 """Check if a header is in alphabetical order with the previous header.
746
747 Args:
erg@google.comfd5da632013-10-25 17:39:45 +0000748 clean_lines: A CleansedLines instance containing the file.
749 linenum: The number of the line to check.
750 header_path: Canonicalized header to be checked.
erg@google.coma868d2d2009-10-09 21:18:45 +0000751
752 Returns:
753 Returns true if the header is in alphabetical order.
754 """
erg@google.comfd5da632013-10-25 17:39:45 +0000755 # If previous section is different from current section, _last_header will
756 # be reset to empty string, so it's always less than current header.
757 #
758 # If previous line was a blank line, assume that the headers are
759 # intentionally sorted the way they are.
760 if (self._last_header > header_path and
avakulenko@google.com554223d2014-12-04 22:00:20 +0000761 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])):
erg@google.coma868d2d2009-10-09 21:18:45 +0000762 return False
erg@google.coma868d2d2009-10-09 21:18:45 +0000763 return True
erg@google.com4e00b9a2009-01-12 23:05:11 +0000764
765 def CheckNextIncludeOrder(self, header_type):
766 """Returns a non-empty error message if the next header is out of order.
767
768 This function also updates the internal state to be ready to check
769 the next include.
770
771 Args:
772 header_type: One of the _XXX_HEADER constants defined above.
773
774 Returns:
775 The empty string if the header is in the right order, or an
776 error message describing what's wrong.
777
778 """
779 error_message = ('Found %s after %s' %
780 (self._TYPE_NAMES[header_type],
781 self._SECTION_NAMES[self._section]))
782
erg@google.coma868d2d2009-10-09 21:18:45 +0000783 last_section = self._section
784
erg@google.com4e00b9a2009-01-12 23:05:11 +0000785 if header_type == _C_SYS_HEADER:
786 if self._section <= self._C_SECTION:
787 self._section = self._C_SECTION
788 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000789 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000790 return error_message
791 elif header_type == _CPP_SYS_HEADER:
792 if self._section <= self._CPP_SECTION:
793 self._section = self._CPP_SECTION
794 else:
erg@google.coma868d2d2009-10-09 21:18:45 +0000795 self._last_header = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +0000796 return error_message
797 elif header_type == _LIKELY_MY_HEADER:
798 if self._section <= self._MY_H_SECTION:
799 self._section = self._MY_H_SECTION
800 else:
801 self._section = self._OTHER_H_SECTION
802 elif header_type == _POSSIBLE_MY_HEADER:
803 if self._section <= self._MY_H_SECTION:
804 self._section = self._MY_H_SECTION
805 else:
806 # This will always be the fallback because we're not sure
807 # enough that the header is associated with this file.
808 self._section = self._OTHER_H_SECTION
809 else:
810 assert header_type == _OTHER_HEADER
811 self._section = self._OTHER_H_SECTION
812
erg@google.coma868d2d2009-10-09 21:18:45 +0000813 if last_section != self._section:
814 self._last_header = ''
815
erg@google.com4e00b9a2009-01-12 23:05:11 +0000816 return ''
817
818
819class _CppLintState(object):
820 """Maintains module-wide state.."""
821
822 def __init__(self):
823 self.verbose_level = 1 # global setting.
824 self.error_count = 0 # global count of reported errors
erg@google.come35f7652009-06-19 20:52:09 +0000825 # filters to apply when emitting error messages
826 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000827 # backup of filter list. Used to restore the state after each file.
828 self._filters_backup = self.filters[:]
erg@google.coma868d2d2009-10-09 21:18:45 +0000829 self.counting = 'total' # In what way are we counting errors?
830 self.errors_by_category = {} # string to int dict storing error counts
erg@google.com4e00b9a2009-01-12 23:05:11 +0000831
832 # output format:
833 # "emacs" - format that emacs can parse (default)
834 # "vs7" - format that Microsoft Visual Studio 7 can parse
835 self.output_format = 'emacs'
836
837 def SetOutputFormat(self, output_format):
838 """Sets the output format for errors."""
839 self.output_format = output_format
840
841 def SetVerboseLevel(self, level):
842 """Sets the module's verbosity, and returns the previous setting."""
843 last_verbose_level = self.verbose_level
844 self.verbose_level = level
845 return last_verbose_level
846
erg@google.coma868d2d2009-10-09 21:18:45 +0000847 def SetCountingStyle(self, counting_style):
848 """Sets the module's counting options."""
849 self.counting = counting_style
850
erg@google.com4e00b9a2009-01-12 23:05:11 +0000851 def SetFilters(self, filters):
852 """Sets the error-message filters.
853
854 These filters are applied when deciding whether to emit a given
855 error message.
856
857 Args:
858 filters: A string of comma-separated filters (eg "+whitespace/indent").
859 Each filter should start with + or -; else we die.
erg@google.coma87abb82009-02-24 01:41:01 +0000860
861 Raises:
862 ValueError: The comma-separated filters did not all start with '+' or '-'.
863 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
erg@google.com4e00b9a2009-01-12 23:05:11 +0000864 """
erg@google.come35f7652009-06-19 20:52:09 +0000865 # Default filters always have less priority than the flag ones.
866 self.filters = _DEFAULT_FILTERS[:]
erg@google.com7430eef2014-07-28 22:33:46 +0000867 self.AddFilters(filters)
868
869 def AddFilters(self, filters):
870 """ Adds more filters to the existing list of error-message filters. """
erg@google.come35f7652009-06-19 20:52:09 +0000871 for filt in filters.split(','):
872 clean_filt = filt.strip()
873 if clean_filt:
874 self.filters.append(clean_filt)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000875 for filt in self.filters:
876 if not (filt.startswith('+') or filt.startswith('-')):
877 raise ValueError('Every filter in --filters must start with + or -'
878 ' (%s does not)' % filt)
879
erg@google.com7430eef2014-07-28 22:33:46 +0000880 def BackupFilters(self):
881 """ Saves the current filter list to backup storage."""
882 self._filters_backup = self.filters[:]
883
884 def RestoreFilters(self):
885 """ Restores filters previously backed up."""
886 self.filters = self._filters_backup[:]
887
erg@google.coma868d2d2009-10-09 21:18:45 +0000888 def ResetErrorCounts(self):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000889 """Sets the module's error statistic back to zero."""
890 self.error_count = 0
erg@google.coma868d2d2009-10-09 21:18:45 +0000891 self.errors_by_category = {}
erg@google.com4e00b9a2009-01-12 23:05:11 +0000892
erg@google.coma868d2d2009-10-09 21:18:45 +0000893 def IncrementErrorCount(self, category):
erg@google.com4e00b9a2009-01-12 23:05:11 +0000894 """Bumps the module's error statistic."""
895 self.error_count += 1
erg@google.coma868d2d2009-10-09 21:18:45 +0000896 if self.counting in ('toplevel', 'detailed'):
897 if self.counting != 'detailed':
898 category = category.split('/')[0]
899 if category not in self.errors_by_category:
900 self.errors_by_category[category] = 0
901 self.errors_by_category[category] += 1
erg@google.com4e00b9a2009-01-12 23:05:11 +0000902
erg@google.coma868d2d2009-10-09 21:18:45 +0000903 def PrintErrorCounts(self):
904 """Print a summary of errors by category, and the total."""
905 for category, count in self.errors_by_category.iteritems():
906 sys.stderr.write('Category \'%s\' errors found: %d\n' %
907 (category, count))
908 sys.stderr.write('Total errors found: %d\n' % self.error_count)
erg@google.com4e00b9a2009-01-12 23:05:11 +0000909
910_cpplint_state = _CppLintState()
911
912
913def _OutputFormat():
914 """Gets the module's output format."""
915 return _cpplint_state.output_format
916
917
918def _SetOutputFormat(output_format):
919 """Sets the module's output format."""
920 _cpplint_state.SetOutputFormat(output_format)
921
922
923def _VerboseLevel():
924 """Returns the module's verbosity setting."""
925 return _cpplint_state.verbose_level
926
927
928def _SetVerboseLevel(level):
929 """Sets the module's verbosity, and returns the previous setting."""
930 return _cpplint_state.SetVerboseLevel(level)
931
932
erg@google.coma868d2d2009-10-09 21:18:45 +0000933def _SetCountingStyle(level):
934 """Sets the module's counting options."""
935 _cpplint_state.SetCountingStyle(level)
936
937
erg@google.com4e00b9a2009-01-12 23:05:11 +0000938def _Filters():
939 """Returns the module's list of output filters, as a list."""
940 return _cpplint_state.filters
941
942
943def _SetFilters(filters):
944 """Sets the module's error-message filters.
945
946 These filters are applied when deciding whether to emit a given
947 error message.
948
949 Args:
950 filters: A string of comma-separated filters (eg "whitespace/indent").
951 Each filter should start with + or -; else we die.
952 """
953 _cpplint_state.SetFilters(filters)
954
erg@google.com7430eef2014-07-28 22:33:46 +0000955def _AddFilters(filters):
956 """Adds more filter overrides.
957
958 Unlike _SetFilters, this function does not reset the current list of filters
959 available.
960
961 Args:
962 filters: A string of comma-separated filters (eg "whitespace/indent").
963 Each filter should start with + or -; else we die.
964 """
965 _cpplint_state.AddFilters(filters)
966
967def _BackupFilters():
968 """ Saves the current filter list to backup storage."""
969 _cpplint_state.BackupFilters()
970
971def _RestoreFilters():
972 """ Restores filters previously backed up."""
973 _cpplint_state.RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +0000974
975class _FunctionState(object):
976 """Tracks current function name and the number of lines in its body."""
977
978 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc.
979 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER.
980
981 def __init__(self):
982 self.in_a_function = False
983 self.lines_in_function = 0
984 self.current_function = ''
985
986 def Begin(self, function_name):
987 """Start analyzing function body.
988
989 Args:
990 function_name: The name of the function being tracked.
991 """
992 self.in_a_function = True
993 self.lines_in_function = 0
994 self.current_function = function_name
995
996 def Count(self):
997 """Count line in current function body."""
998 if self.in_a_function:
999 self.lines_in_function += 1
1000
1001 def Check(self, error, filename, linenum):
1002 """Report if too many lines in function body.
1003
1004 Args:
1005 error: The function to call with any errors found.
1006 filename: The name of the current file.
1007 linenum: The number of the line to check.
1008 """
Alex Vakulenko01e47232016-05-06 13:54:15 -07001009 if not self.in_a_function:
1010 return
1011
erg@google.com4e00b9a2009-01-12 23:05:11 +00001012 if Match(r'T(EST|est)', self.current_function):
1013 base_trigger = self._TEST_TRIGGER
1014 else:
1015 base_trigger = self._NORMAL_TRIGGER
1016 trigger = base_trigger * 2**_VerboseLevel()
1017
1018 if self.lines_in_function > trigger:
1019 error_level = int(math.log(self.lines_in_function / base_trigger, 2))
1020 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
1021 if error_level > 5:
1022 error_level = 5
1023 error(filename, linenum, 'readability/fn_size', error_level,
1024 'Small and focused functions are preferred:'
1025 ' %s has %d non-comment lines'
1026 ' (error triggered by exceeding %d lines).' % (
1027 self.current_function, self.lines_in_function, trigger))
1028
1029 def End(self):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001030 """Stop analyzing function body."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00001031 self.in_a_function = False
1032
1033
1034class _IncludeError(Exception):
1035 """Indicates a problem with the include order in a file."""
1036 pass
1037
1038
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001039class FileInfo(object):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001040 """Provides utility functions for filenames.
1041
1042 FileInfo provides easy access to the components of a file's path
1043 relative to the project root.
1044 """
1045
1046 def __init__(self, filename):
1047 self._filename = filename
1048
1049 def FullName(self):
1050 """Make Windows paths like Unix."""
1051 return os.path.abspath(self._filename).replace('\\', '/')
1052
1053 def RepositoryName(self):
1054 """FullName after removing the local path to the repository.
1055
1056 If we have a real absolute path name here we can try to do something smart:
1057 detecting the root of the checkout and truncating /path/to/checkout from
1058 the name so that we get header guards that don't include things like
1059 "C:\Documents and Settings\..." or "/home/username/..." in them and thus
1060 people on different computers who have checked the source out to different
1061 locations won't see bogus errors.
1062 """
1063 fullname = self.FullName()
1064
1065 if os.path.exists(fullname):
1066 project_dir = os.path.dirname(fullname)
1067
1068 if os.path.exists(os.path.join(project_dir, ".svn")):
1069 # If there's a .svn file in the current directory, we recursively look
1070 # up the directory tree for the top of the SVN checkout
1071 root_dir = project_dir
1072 one_up_dir = os.path.dirname(root_dir)
1073 while os.path.exists(os.path.join(one_up_dir, ".svn")):
1074 root_dir = os.path.dirname(root_dir)
1075 one_up_dir = os.path.dirname(one_up_dir)
1076
1077 prefix = os.path.commonprefix([root_dir, project_dir])
1078 return fullname[len(prefix) + 1:]
1079
erg@google.com3dc74262011-11-30 01:12:00 +00001080 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
1081 # searching up from the current path.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001082 root_dir = current_dir = os.path.dirname(fullname)
1083 while current_dir != os.path.dirname(current_dir):
1084 if (os.path.exists(os.path.join(current_dir, ".git")) or
1085 os.path.exists(os.path.join(current_dir, ".hg")) or
1086 os.path.exists(os.path.join(current_dir, ".svn"))):
1087 root_dir = current_dir
1088 current_dir = os.path.dirname(current_dir)
erg@google.com42e59b02010-10-04 22:18:07 +00001089
1090 if (os.path.exists(os.path.join(root_dir, ".git")) or
erg@google.com3dc74262011-11-30 01:12:00 +00001091 os.path.exists(os.path.join(root_dir, ".hg")) or
1092 os.path.exists(os.path.join(root_dir, ".svn"))):
erg@google.com42e59b02010-10-04 22:18:07 +00001093 prefix = os.path.commonprefix([root_dir, project_dir])
1094 return fullname[len(prefix) + 1:]
erg@google.com4e00b9a2009-01-12 23:05:11 +00001095
1096 # Don't know what to do; header guard warnings may be wrong...
1097 return fullname
1098
1099 def Split(self):
1100 """Splits the file into the directory, basename, and extension.
1101
1102 For 'chrome/browser/browser.cc', Split() would
1103 return ('chrome/browser', 'browser', '.cc')
1104
1105 Returns:
1106 A tuple of (directory, basename, extension).
1107 """
1108
1109 googlename = self.RepositoryName()
1110 project, rest = os.path.split(googlename)
1111 return (project,) + os.path.splitext(rest)
1112
1113 def BaseName(self):
1114 """File base name - text after the final slash, before the final period."""
1115 return self.Split()[1]
1116
1117 def Extension(self):
1118 """File extension - text following the final period."""
1119 return self.Split()[2]
1120
1121 def NoExtension(self):
1122 """File has no source file extension."""
1123 return '/'.join(self.Split()[0:2])
1124
1125 def IsSource(self):
1126 """File has a source file extension."""
Alex Vakulenko01e47232016-05-06 13:54:15 -07001127 return _IsSourceExtension(self.Extension()[1:])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001128
1129
erg+personal@google.com05189642010-04-30 20:43:03 +00001130def _ShouldPrintError(category, confidence, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00001131 """If confidence >= verbose, category passes filter and is not suppressed."""
erg+personal@google.com05189642010-04-30 20:43:03 +00001132
1133 # There are three ways we might decide not to print an error message:
1134 # a "NOLINT(category)" comment appears in the source,
erg@google.com4e00b9a2009-01-12 23:05:11 +00001135 # the verbosity level isn't high enough, or the filters filter it out.
erg+personal@google.com05189642010-04-30 20:43:03 +00001136 if IsErrorSuppressedByNolint(category, linenum):
1137 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00001138
erg@google.com4e00b9a2009-01-12 23:05:11 +00001139 if confidence < _cpplint_state.verbose_level:
1140 return False
1141
1142 is_filtered = False
1143 for one_filter in _Filters():
1144 if one_filter.startswith('-'):
1145 if category.startswith(one_filter[1:]):
1146 is_filtered = True
1147 elif one_filter.startswith('+'):
1148 if category.startswith(one_filter[1:]):
1149 is_filtered = False
1150 else:
1151 assert False # should have been checked for in SetFilter.
1152 if is_filtered:
1153 return False
1154
1155 return True
1156
1157
1158def Error(filename, linenum, category, confidence, message):
1159 """Logs the fact we've found a lint error.
1160
1161 We log where the error was found, and also our confidence in the error,
1162 that is, how certain we are this is a legitimate style regression, and
1163 not a misidentification or a use that's sometimes justified.
1164
erg+personal@google.com05189642010-04-30 20:43:03 +00001165 False positives can be suppressed by the use of
1166 "cpplint(category)" comments on the offending line. These are
1167 parsed into _error_suppressions.
1168
erg@google.com4e00b9a2009-01-12 23:05:11 +00001169 Args:
1170 filename: The name of the file containing the error.
1171 linenum: The number of the line containing the error.
1172 category: A string used to describe the "category" this bug
1173 falls under: "whitespace", say, or "runtime". Categories
1174 may have a hierarchy separated by slashes: "whitespace/indent".
1175 confidence: A number from 1-5 representing a confidence score for
1176 the error, with 5 meaning that we are certain of the problem,
1177 and 1 meaning that it could be a legitimate construct.
1178 message: The error message.
1179 """
erg+personal@google.com05189642010-04-30 20:43:03 +00001180 if _ShouldPrintError(category, confidence, linenum):
erg@google.coma868d2d2009-10-09 21:18:45 +00001181 _cpplint_state.IncrementErrorCount(category)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001182 if _cpplint_state.output_format == 'vs7':
1183 sys.stderr.write('%s(%s): %s [%s] [%d]\n' % (
1184 filename, linenum, message, category, confidence))
erg@google.com02c27fd2013-05-28 21:34:34 +00001185 elif _cpplint_state.output_format == 'eclipse':
1186 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % (
1187 filename, linenum, message, category, confidence))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001188 else:
1189 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % (
1190 filename, linenum, message, category, confidence))
1191
1192
erg@google.com2aa59982013-10-28 19:09:25 +00001193# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001194_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
1195 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
avakulenko@google.com02af6282014-06-04 18:53:25 +00001196# Match a single C style comment on the same line.
1197_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/'
1198# Matches multi-line C style comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001199# This RE is a little bit more complicated than one might expect, because we
1200# have to take care of space removals tools so we can handle comments inside
1201# statements better.
1202# The current rule is: We only clear spaces from both sides when we're at the
1203# end of the line. Otherwise, we try to remove spaces from the right side,
1204# if this doesn't work we try on left side but only if there's a non-character
1205# on the right.
1206_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
avakulenko@google.com02af6282014-06-04 18:53:25 +00001207 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' +
1208 _RE_PATTERN_C_COMMENTS + r'\s+|' +
1209 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' +
1210 _RE_PATTERN_C_COMMENTS + r')')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001211
1212
1213def IsCppString(line):
1214 """Does line terminate so, that the next symbol is in string constant.
1215
1216 This function does not consider single-line nor multi-line comments.
1217
1218 Args:
1219 line: is a partial line of code starting from the 0..n.
1220
1221 Returns:
1222 True, if next character appended to 'line' is inside a
1223 string constant.
1224 """
1225
1226 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \"
1227 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
1228
1229
erg@google.com2aa59982013-10-28 19:09:25 +00001230def CleanseRawStrings(raw_lines):
1231 """Removes C++11 raw strings from lines.
1232
1233 Before:
1234 static const char kData[] = R"(
1235 multi-line string
1236 )";
1237
1238 After:
1239 static const char kData[] = ""
1240 (replaced by blank line)
1241 "";
1242
1243 Args:
1244 raw_lines: list of raw lines.
1245
1246 Returns:
1247 list of lines with C++11 raw strings replaced by empty strings.
1248 """
1249
1250 delimiter = None
1251 lines_without_raw_strings = []
1252 for line in raw_lines:
1253 if delimiter:
1254 # Inside a raw string, look for the end
1255 end = line.find(delimiter)
1256 if end >= 0:
1257 # Found the end of the string, match leading space for this
1258 # line and resume copying the original lines, and also insert
1259 # a "" on the last line.
1260 leading_space = Match(r'^(\s*)\S', line)
1261 line = leading_space.group(1) + '""' + line[end + len(delimiter):]
1262 delimiter = None
1263 else:
1264 # Haven't found the end yet, append a blank line.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001265 line = '""'
erg@google.com2aa59982013-10-28 19:09:25 +00001266
avakulenko@google.com02af6282014-06-04 18:53:25 +00001267 # Look for beginning of a raw string, and replace them with
1268 # empty strings. This is done in a loop to handle multiple raw
1269 # strings on the same line.
1270 while delimiter is None:
erg@google.com2aa59982013-10-28 19:09:25 +00001271 # Look for beginning of a raw string.
1272 # See 2.14.15 [lex.string] for syntax.
Alex Vakulenko01e47232016-05-06 13:54:15 -07001273 #
1274 # Once we have matched a raw string, we check the prefix of the
1275 # line to make sure that the line is not part of a single line
1276 # comment. It's done this way because we remove raw strings
1277 # before removing comments as opposed to removing comments
1278 # before removing raw strings. This is because there are some
1279 # cpplint checks that requires the comments to be preserved, but
1280 # we don't want to check comments that are inside raw strings.
1281 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line)
1282 if (matched and
1283 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//',
1284 matched.group(1))):
erg@google.com2aa59982013-10-28 19:09:25 +00001285 delimiter = ')' + matched.group(2) + '"'
1286
1287 end = matched.group(3).find(delimiter)
1288 if end >= 0:
1289 # Raw string ended on same line
1290 line = (matched.group(1) + '""' +
1291 matched.group(3)[end + len(delimiter):])
1292 delimiter = None
1293 else:
1294 # Start of a multi-line raw string
1295 line = matched.group(1) + '""'
avakulenko@google.com02af6282014-06-04 18:53:25 +00001296 else:
1297 break
erg@google.com2aa59982013-10-28 19:09:25 +00001298
1299 lines_without_raw_strings.append(line)
1300
1301 # TODO(unknown): if delimiter is not None here, we might want to
1302 # emit a warning for unterminated string.
1303 return lines_without_raw_strings
1304
1305
erg@google.com4e00b9a2009-01-12 23:05:11 +00001306def FindNextMultiLineCommentStart(lines, lineix):
1307 """Find the beginning marker for a multiline comment."""
1308 while lineix < len(lines):
1309 if lines[lineix].strip().startswith('/*'):
1310 # Only return this marker if the comment goes beyond this line
1311 if lines[lineix].strip().find('*/', 2) < 0:
1312 return lineix
1313 lineix += 1
1314 return len(lines)
1315
1316
1317def FindNextMultiLineCommentEnd(lines, lineix):
1318 """We are inside a comment, find the end marker."""
1319 while lineix < len(lines):
1320 if lines[lineix].strip().endswith('*/'):
1321 return lineix
1322 lineix += 1
1323 return len(lines)
1324
1325
1326def RemoveMultiLineCommentsFromRange(lines, begin, end):
1327 """Clears a range of lines for multi-line comments."""
1328 # Having // dummy comments makes the lines non-empty, so we will not get
1329 # unnecessary blank line warnings later in the code.
1330 for i in range(begin, end):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001331 lines[i] = '/**/'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001332
1333
1334def RemoveMultiLineComments(filename, lines, error):
1335 """Removes multiline (c-style) comments from lines."""
1336 lineix = 0
1337 while lineix < len(lines):
1338 lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
1339 if lineix_begin >= len(lines):
1340 return
1341 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
1342 if lineix_end >= len(lines):
1343 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
1344 'Could not find end of multi-line comment')
1345 return
1346 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
1347 lineix = lineix_end + 1
1348
1349
1350def CleanseComments(line):
1351 """Removes //-comments and single-line C-style /* */ comments.
1352
1353 Args:
1354 line: A line of C++ source.
1355
1356 Returns:
1357 The line with single-line comments removed.
1358 """
1359 commentpos = line.find('//')
1360 if commentpos != -1 and not IsCppString(line[:commentpos]):
erg@google.comd7d27472011-09-07 17:36:35 +00001361 line = line[:commentpos].rstrip()
erg@google.com4e00b9a2009-01-12 23:05:11 +00001362 # get rid of /* ... */
1363 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
1364
1365
erg@google.coma87abb82009-02-24 01:41:01 +00001366class CleansedLines(object):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001367 """Holds 4 copies of all lines with different preprocessing applied to them.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001368
avakulenko@google.com554223d2014-12-04 22:00:20 +00001369 1) elided member contains lines without strings and comments.
1370 2) lines member contains lines without comments.
erg@google.comd350fe52013-01-14 17:51:48 +00001371 3) raw_lines member contains all the lines without processing.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001372 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw
1373 strings removed.
1374 All these members are of <type 'list'>, and of the same length.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001375 """
1376
1377 def __init__(self, lines):
1378 self.elided = []
1379 self.lines = []
1380 self.raw_lines = lines
1381 self.num_lines = len(lines)
erg@google.com2aa59982013-10-28 19:09:25 +00001382 self.lines_without_raw_strings = CleanseRawStrings(lines)
1383 for linenum in range(len(self.lines_without_raw_strings)):
1384 self.lines.append(CleanseComments(
1385 self.lines_without_raw_strings[linenum]))
1386 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00001387 self.elided.append(CleanseComments(elided))
1388
1389 def NumLines(self):
1390 """Returns the number of lines represented."""
1391 return self.num_lines
1392
1393 @staticmethod
1394 def _CollapseStrings(elided):
1395 """Collapses strings and chars on a line to simple "" or '' blocks.
1396
1397 We nix strings first so we're not fooled by text like '"http://"'
1398
1399 Args:
1400 elided: The line being processed.
1401
1402 Returns:
1403 The line with collapsed strings.
1404 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001405 if _RE_PATTERN_INCLUDE.match(elided):
1406 return elided
1407
1408 # Remove escaped characters first to make quote/single quote collapsing
1409 # basic. Things that look like escaped characters shouldn't occur
1410 # outside of strings and chars.
1411 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
1412
1413 # Replace quoted strings and digit separators. Both single quotes
1414 # and double quotes are processed in the same loop, otherwise
1415 # nested quotes wouldn't work.
1416 collapsed = ''
1417 while True:
1418 # Find the first quote character
1419 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided)
1420 if not match:
1421 collapsed += elided
1422 break
1423 head, quote, tail = match.groups()
1424
1425 if quote == '"':
1426 # Collapse double quoted strings
1427 second_quote = tail.find('"')
1428 if second_quote >= 0:
1429 collapsed += head + '""'
1430 elided = tail[second_quote + 1:]
1431 else:
1432 # Unmatched double quote, don't bother processing the rest
1433 # of the line since this is probably a multiline string.
1434 collapsed += elided
1435 break
1436 else:
1437 # Found single quote, check nearby text to eliminate digit separators.
1438 #
1439 # There is no special handling for floating point here, because
1440 # the integer/fractional/exponent parts would all be parsed
1441 # correctly as long as there are digits on both sides of the
1442 # separator. So we are fine as long as we don't see something
1443 # like "0.'3" (gcc 4.9.0 will not allow this literal).
1444 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head):
1445 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail)
1446 collapsed += head + match_literal.group(1).replace("'", '')
1447 elided = match_literal.group(2)
1448 else:
1449 second_quote = tail.find('\'')
1450 if second_quote >= 0:
1451 collapsed += head + "''"
1452 elided = tail[second_quote + 1:]
1453 else:
1454 # Unmatched single quote
1455 collapsed += elided
1456 break
1457
1458 return collapsed
erg@google.com4e00b9a2009-01-12 23:05:11 +00001459
1460
avakulenko@google.com02af6282014-06-04 18:53:25 +00001461def FindEndOfExpressionInLine(line, startpos, stack):
1462 """Find the position just after the end of current parenthesized expression.
erg@google.comd350fe52013-01-14 17:51:48 +00001463
1464 Args:
1465 line: a CleansedLines line.
1466 startpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001467 stack: nesting stack at startpos.
erg@google.comd350fe52013-01-14 17:51:48 +00001468
1469 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001470 On finding matching end: (index just after matching end, None)
1471 On finding an unclosed expression: (-1, None)
1472 Otherwise: (-1, new stack at end of this line)
erg@google.comd350fe52013-01-14 17:51:48 +00001473 """
1474 for i in xrange(startpos, len(line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00001475 char = line[i]
1476 if char in '([{':
1477 # Found start of parenthesized expression, push to expression stack
1478 stack.append(char)
1479 elif char == '<':
1480 # Found potential start of template argument list
1481 if i > 0 and line[i - 1] == '<':
1482 # Left shift operator
1483 if stack and stack[-1] == '<':
1484 stack.pop()
1485 if not stack:
1486 return (-1, None)
1487 elif i > 0 and Search(r'\boperator\s*$', line[0:i]):
1488 # operator<, don't add to stack
1489 continue
1490 else:
1491 # Tentative start of template argument list
1492 stack.append('<')
1493 elif char in ')]}':
1494 # Found end of parenthesized expression.
1495 #
1496 # If we are currently expecting a matching '>', the pending '<'
1497 # must have been an operator. Remove them from expression stack.
1498 while stack and stack[-1] == '<':
1499 stack.pop()
1500 if not stack:
1501 return (-1, None)
1502 if ((stack[-1] == '(' and char == ')') or
1503 (stack[-1] == '[' and char == ']') or
1504 (stack[-1] == '{' and char == '}')):
1505 stack.pop()
1506 if not stack:
1507 return (i + 1, None)
1508 else:
1509 # Mismatched parentheses
1510 return (-1, None)
1511 elif char == '>':
1512 # Found potential end of template argument list.
1513
1514 # Ignore "->" and operator functions
1515 if (i > 0 and
1516 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))):
1517 continue
1518
1519 # Pop the stack if there is a matching '<'. Otherwise, ignore
1520 # this '>' since it must be an operator.
1521 if stack:
1522 if stack[-1] == '<':
1523 stack.pop()
1524 if not stack:
1525 return (i + 1, None)
1526 elif char == ';':
1527 # Found something that look like end of statements. If we are currently
1528 # expecting a '>', the matching '<' must have been an operator, since
1529 # template argument list should not contain statements.
1530 while stack and stack[-1] == '<':
1531 stack.pop()
1532 if not stack:
1533 return (-1, None)
1534
1535 # Did not find end of expression or unbalanced parentheses on this line
1536 return (-1, stack)
erg@google.comd350fe52013-01-14 17:51:48 +00001537
1538
erg@google.com4e00b9a2009-01-12 23:05:11 +00001539def CloseExpression(clean_lines, linenum, pos):
erg@google.com2aa59982013-10-28 19:09:25 +00001540 """If input points to ( or { or [ or <, finds the position that closes it.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001541
erg@google.com2aa59982013-10-28 19:09:25 +00001542 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the
erg@google.com4e00b9a2009-01-12 23:05:11 +00001543 linenum/pos that correspond to the closing of the expression.
1544
avakulenko@google.com02af6282014-06-04 18:53:25 +00001545 TODO(unknown): cpplint spends a fair bit of time matching parentheses.
1546 Ideally we would want to index all opening and closing parentheses once
1547 and have CloseExpression be just a simple lookup, but due to preprocessor
1548 tricks, this is not so easy.
1549
erg@google.com4e00b9a2009-01-12 23:05:11 +00001550 Args:
1551 clean_lines: A CleansedLines instance containing the file.
1552 linenum: The number of the line to check.
1553 pos: A position on the line.
1554
1555 Returns:
1556 A tuple (line, linenum, pos) pointer *past* the closing brace, or
1557 (line, len(lines), -1) if we never find a close. Note we ignore
1558 strings and comments when matching; and the line we return is the
1559 'cleansed' line at linenum.
1560 """
1561
1562 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001563 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001564 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001565
erg@google.comd350fe52013-01-14 17:51:48 +00001566 # Check first line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001567 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, [])
erg@google.comd350fe52013-01-14 17:51:48 +00001568 if end_pos > -1:
1569 return (line, linenum, end_pos)
erg@google.com2aa59982013-10-28 19:09:25 +00001570
1571 # Continue scanning forward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001572 while stack and linenum < clean_lines.NumLines() - 1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001573 linenum += 1
1574 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001575 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001576 if end_pos > -1:
1577 return (line, linenum, end_pos)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001578
avakulenko@google.com02af6282014-06-04 18:53:25 +00001579 # Did not find end of expression before end of file, give up
erg@google.comd350fe52013-01-14 17:51:48 +00001580 return (line, clean_lines.NumLines(), -1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001581
erg@google.com2aa59982013-10-28 19:09:25 +00001582
avakulenko@google.com02af6282014-06-04 18:53:25 +00001583def FindStartOfExpressionInLine(line, endpos, stack):
1584 """Find position at the matching start of current expression.
erg@google.com2aa59982013-10-28 19:09:25 +00001585
1586 This is almost the reverse of FindEndOfExpressionInLine, but note
1587 that the input position and returned position differs by 1.
1588
1589 Args:
1590 line: a CleansedLines line.
1591 endpos: start searching at this position.
avakulenko@google.com02af6282014-06-04 18:53:25 +00001592 stack: nesting stack at endpos.
erg@google.com2aa59982013-10-28 19:09:25 +00001593
1594 Returns:
avakulenko@google.com02af6282014-06-04 18:53:25 +00001595 On finding matching start: (index at matching start, None)
1596 On finding an unclosed expression: (-1, None)
1597 Otherwise: (-1, new stack at beginning of this line)
erg@google.com2aa59982013-10-28 19:09:25 +00001598 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00001599 i = endpos
1600 while i >= 0:
1601 char = line[i]
1602 if char in ')]}':
1603 # Found end of expression, push to expression stack
1604 stack.append(char)
1605 elif char == '>':
1606 # Found potential end of template argument list.
1607 #
1608 # Ignore it if it's a "->" or ">=" or "operator>"
1609 if (i > 0 and
1610 (line[i - 1] == '-' or
1611 Match(r'\s>=\s', line[i - 1:]) or
1612 Search(r'\boperator\s*$', line[0:i]))):
1613 i -= 1
1614 else:
1615 stack.append('>')
1616 elif char == '<':
1617 # Found potential start of template argument list
1618 if i > 0 and line[i - 1] == '<':
1619 # Left shift operator
1620 i -= 1
1621 else:
1622 # If there is a matching '>', we can pop the expression stack.
1623 # Otherwise, ignore this '<' since it must be an operator.
1624 if stack and stack[-1] == '>':
1625 stack.pop()
1626 if not stack:
1627 return (i, None)
1628 elif char in '([{':
1629 # Found start of expression.
1630 #
1631 # If there are any unmatched '>' on the stack, they must be
1632 # operators. Remove those.
1633 while stack and stack[-1] == '>':
1634 stack.pop()
1635 if not stack:
1636 return (-1, None)
1637 if ((char == '(' and stack[-1] == ')') or
1638 (char == '[' and stack[-1] == ']') or
1639 (char == '{' and stack[-1] == '}')):
1640 stack.pop()
1641 if not stack:
1642 return (i, None)
1643 else:
1644 # Mismatched parentheses
1645 return (-1, None)
1646 elif char == ';':
1647 # Found something that look like end of statements. If we are currently
1648 # expecting a '<', the matching '>' must have been an operator, since
1649 # template argument list should not contain statements.
1650 while stack and stack[-1] == '>':
1651 stack.pop()
1652 if not stack:
1653 return (-1, None)
1654
1655 i -= 1
1656
1657 return (-1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001658
1659
1660def ReverseCloseExpression(clean_lines, linenum, pos):
1661 """If input points to ) or } or ] or >, finds the position that opens it.
1662
1663 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the
1664 linenum/pos that correspond to the opening of the expression.
1665
1666 Args:
1667 clean_lines: A CleansedLines instance containing the file.
1668 linenum: The number of the line to check.
1669 pos: A position on the line.
1670
1671 Returns:
1672 A tuple (line, linenum, pos) pointer *at* the opening brace, or
1673 (line, 0, -1) if we never find the matching opening brace. Note
1674 we ignore strings and comments when matching; and the line we
1675 return is the 'cleansed' line at linenum.
1676 """
1677 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001678 if line[pos] not in ')}]>':
erg@google.com2aa59982013-10-28 19:09:25 +00001679 return (line, 0, -1)
erg@google.com2aa59982013-10-28 19:09:25 +00001680
1681 # Check last line
avakulenko@google.com02af6282014-06-04 18:53:25 +00001682 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, [])
erg@google.com2aa59982013-10-28 19:09:25 +00001683 if start_pos > -1:
1684 return (line, linenum, start_pos)
1685
1686 # Continue scanning backward
avakulenko@google.com02af6282014-06-04 18:53:25 +00001687 while stack and linenum > 0:
erg@google.com2aa59982013-10-28 19:09:25 +00001688 linenum -= 1
1689 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00001690 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack)
erg@google.com2aa59982013-10-28 19:09:25 +00001691 if start_pos > -1:
1692 return (line, linenum, start_pos)
1693
avakulenko@google.com02af6282014-06-04 18:53:25 +00001694 # Did not find start of expression before beginning of file, give up
erg@google.com2aa59982013-10-28 19:09:25 +00001695 return (line, 0, -1)
1696
1697
erg@google.com4e00b9a2009-01-12 23:05:11 +00001698def CheckForCopyright(filename, lines, error):
1699 """Logs an error if no Copyright message appears at the top of the file."""
1700
1701 # We'll say it should occur by line 10. Don't forget there's a
1702 # dummy line at the front.
1703 for line in xrange(1, min(len(lines), 11)):
1704 if re.search(r'Copyright', lines[line], re.I): break
1705 else: # means no copyright line was found
1706 error(filename, 0, 'legal/copyright', 5,
1707 'No copyright message found. '
1708 'You should have a line: "Copyright [year] <Copyright Owner>"')
1709
1710
avakulenko@google.com02af6282014-06-04 18:53:25 +00001711def GetIndentLevel(line):
1712 """Return the number of leading spaces in line.
1713
1714 Args:
1715 line: A string to check.
1716
1717 Returns:
1718 An integer count of leading spaces, possibly zero.
1719 """
1720 indent = Match(r'^( *)\S', line)
1721 if indent:
1722 return len(indent.group(1))
1723 else:
1724 return 0
1725
1726
erg@google.com4e00b9a2009-01-12 23:05:11 +00001727def GetHeaderGuardCPPVariable(filename):
1728 """Returns the CPP variable that should be used as a header guard.
1729
1730 Args:
1731 filename: The name of a C++ header file.
1732
1733 Returns:
1734 The CPP variable that should be used as a header guard in the
1735 named file.
1736
1737 """
1738
erg+personal@google.com05189642010-04-30 20:43:03 +00001739 # Restores original filename in case that cpplint is invoked from Emacs's
1740 # flymake.
1741 filename = re.sub(r'_flymake\.h$', '.h', filename)
erg@google.comd350fe52013-01-14 17:51:48 +00001742 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001743 # Replace 'c++' with 'cpp'.
1744 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp')
Alex Vakulenko01e47232016-05-06 13:54:15 -07001745
erg@google.com4e00b9a2009-01-12 23:05:11 +00001746 fileinfo = FileInfo(filename)
erg@google.com4d70a882013-04-16 21:06:32 +00001747 file_path_from_root = fileinfo.RepositoryName()
1748 if _root:
Sergey Sharybin3b0ea892016-05-31 00:21:14 +02001749 suffix = os.sep
1750 # On Windows using directory separator will leave us with
1751 # "bogus escape error" unless we properly escape regex.
1752 if suffix == '\\':
1753 suffix += '\\'
1754 file_path_from_root = re.sub('^' + _root + suffix, '', file_path_from_root)
avakulenko@google.com554223d2014-12-04 22:00:20 +00001755 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
erg@google.com4e00b9a2009-01-12 23:05:11 +00001756
1757
avakulenko@google.com554223d2014-12-04 22:00:20 +00001758def CheckForHeaderGuard(filename, clean_lines, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001759 """Checks that the file contains a header guard.
1760
erg@google.coma87abb82009-02-24 01:41:01 +00001761 Logs an error if no #ifndef header guard is present. For other
erg@google.com4e00b9a2009-01-12 23:05:11 +00001762 headers, checks that the full pathname is used.
1763
1764 Args:
1765 filename: The name of the C++ header file.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001766 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001767 error: The function to call with any errors found.
1768 """
1769
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001770 # Don't check for header guards if there are error suppression
1771 # comments somewhere in this file.
1772 #
1773 # Because this is silencing a warning for a nonexistent line, we
1774 # only support the very specific NOLINT(build/header_guard) syntax,
1775 # and not the general NOLINT or NOLINT(*) syntax.
avakulenko@google.com554223d2014-12-04 22:00:20 +00001776 raw_lines = clean_lines.lines_without_raw_strings
1777 for i in raw_lines:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00001778 if Search(r'//\s*NOLINT\(build/header_guard\)', i):
1779 return
1780
erg@google.com4e00b9a2009-01-12 23:05:11 +00001781 cppvar = GetHeaderGuardCPPVariable(filename)
1782
avakulenko@google.com554223d2014-12-04 22:00:20 +00001783 ifndef = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001784 ifndef_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001785 define = ''
1786 endif = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00001787 endif_linenum = 0
avakulenko@google.com554223d2014-12-04 22:00:20 +00001788 for linenum, line in enumerate(raw_lines):
erg@google.com4e00b9a2009-01-12 23:05:11 +00001789 linesplit = line.split()
1790 if len(linesplit) >= 2:
1791 # find the first occurrence of #ifndef and #define, save arg
1792 if not ifndef and linesplit[0] == '#ifndef':
1793 # set ifndef to the header guard presented on the #ifndef line.
1794 ifndef = linesplit[1]
1795 ifndef_linenum = linenum
1796 if not define and linesplit[0] == '#define':
1797 define = linesplit[1]
1798 # find the last occurrence of #endif, save entire line
1799 if line.startswith('#endif'):
1800 endif = line
1801 endif_linenum = linenum
1802
avakulenko@google.com554223d2014-12-04 22:00:20 +00001803 if not ifndef or not define or ifndef != define:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001804 error(filename, 0, 'build/header_guard', 5,
1805 'No #ifndef header guard found, suggested CPP variable is: %s' %
1806 cppvar)
1807 return
1808
1809 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
1810 # for backward compatibility.
erg+personal@google.com05189642010-04-30 20:43:03 +00001811 if ifndef != cppvar:
erg@google.com4e00b9a2009-01-12 23:05:11 +00001812 error_level = 0
1813 if ifndef != cppvar + '_':
1814 error_level = 5
1815
avakulenko@google.com554223d2014-12-04 22:00:20 +00001816 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum,
erg+personal@google.com05189642010-04-30 20:43:03 +00001817 error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00001818 error(filename, ifndef_linenum, 'build/header_guard', error_level,
1819 '#ifndef header guard has wrong style, please use: %s' % cppvar)
1820
avakulenko@google.com554223d2014-12-04 22:00:20 +00001821 # Check for "//" comments on endif line.
1822 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum,
1823 error)
1824 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif)
1825 if match:
1826 if match.group(1) == '_':
1827 # Issue low severity warning for deprecated double trailing underscore
1828 error(filename, endif_linenum, 'build/header_guard', 0,
1829 '#endif line should be "#endif // %s"' % cppvar)
erg@google.comdc289702012-01-26 20:30:03 +00001830 return
1831
avakulenko@google.com554223d2014-12-04 22:00:20 +00001832 # Didn't find the corresponding "//" comment. If this file does not
1833 # contain any "//" comments at all, it could be that the compiler
1834 # only wants "/**/" comments, look for those instead.
1835 no_single_line_comments = True
1836 for i in xrange(1, len(raw_lines) - 1):
1837 line = raw_lines[i]
1838 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
1839 no_single_line_comments = False
1840 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00001841
avakulenko@google.com554223d2014-12-04 22:00:20 +00001842 if no_single_line_comments:
1843 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif)
1844 if match:
1845 if match.group(1) == '_':
1846 # Low severity warning for double trailing underscore
1847 error(filename, endif_linenum, 'build/header_guard', 0,
1848 '#endif line should be "#endif /* %s */"' % cppvar)
1849 return
1850
1851 # Didn't find anything
1852 error(filename, endif_linenum, 'build/header_guard', 5,
1853 '#endif line should be "#endif // %s"' % cppvar)
1854
1855
1856def CheckHeaderFileIncluded(filename, include_state, error):
1857 """Logs an error if a .cc file does not include its header."""
1858
1859 # Do not check test files
Alex Vakulenko01e47232016-05-06 13:54:15 -07001860 fileinfo = FileInfo(filename)
1861 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()):
avakulenko@google.com554223d2014-12-04 22:00:20 +00001862 return
1863
Alex Vakulenko01e47232016-05-06 13:54:15 -07001864 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h'
avakulenko@google.com554223d2014-12-04 22:00:20 +00001865 if not os.path.exists(headerfile):
1866 return
1867 headername = FileInfo(headerfile).RepositoryName()
1868 first_include = 0
1869 for section_list in include_state.include_list:
1870 for f in section_list:
1871 if headername in f[0] or f[0] in headername:
1872 return
1873 if not first_include:
1874 first_include = f[1]
1875
1876 error(filename, first_include, 'build/include', 5,
1877 '%s should include its header file %s' % (fileinfo.RepositoryName(),
1878 headername))
erg@google.com4e00b9a2009-01-12 23:05:11 +00001879
1880
erg@google.com2aa59982013-10-28 19:09:25 +00001881def CheckForBadCharacters(filename, lines, error):
1882 """Logs an error for each line containing bad characters.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001883
erg@google.com2aa59982013-10-28 19:09:25 +00001884 Two kinds of bad characters:
1885
1886 1. Unicode replacement characters: These indicate that either the file
1887 contained invalid UTF-8 (likely) or Unicode replacement characters (which
1888 it shouldn't). Note that it's possible for this to throw off line
1889 numbering if the invalid UTF-8 occurred adjacent to a newline.
1890
1891 2. NUL bytes. These are problematic for some tools.
erg@google.com4e00b9a2009-01-12 23:05:11 +00001892
1893 Args:
1894 filename: The name of the current file.
1895 lines: An array of strings, each representing a line of the file.
1896 error: The function to call with any errors found.
1897 """
1898 for linenum, line in enumerate(lines):
1899 if u'\ufffd' in line:
1900 error(filename, linenum, 'readability/utf8', 5,
1901 'Line contains invalid UTF-8 (or Unicode replacement character).')
erg@google.com2aa59982013-10-28 19:09:25 +00001902 if '\0' in line:
1903 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001904
1905
1906def CheckForNewlineAtEOF(filename, lines, error):
1907 """Logs an error if there is no newline char at the end of the file.
1908
1909 Args:
1910 filename: The name of the current file.
1911 lines: An array of strings, each representing a line of the file.
1912 error: The function to call with any errors found.
1913 """
1914
1915 # The array lines() was created by adding two newlines to the
1916 # original file (go figure), then splitting on \n.
1917 # To verify that the file ends in \n, we just have to make sure the
1918 # last-but-two element of lines() exists and is empty.
1919 if len(lines) < 3 or lines[-2]:
1920 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
1921 'Could not find a newline character at the end of the file.')
1922
1923
1924def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
1925 """Logs an error if we see /* ... */ or "..." that extend past one line.
1926
1927 /* ... */ comments are legit inside macros, for one line.
1928 Otherwise, we prefer // comments, so it's ok to warn about the
1929 other. Likewise, it's ok for strings to extend across multiple
1930 lines, as long as a line continuation character (backslash)
1931 terminates each line. Although not currently prohibited by the C++
1932 style guide, it's ugly and unnecessary. We don't do well with either
1933 in this lint program, so we warn about both.
1934
1935 Args:
1936 filename: The name of the current file.
1937 clean_lines: A CleansedLines instance containing the file.
1938 linenum: The number of the line to check.
1939 error: The function to call with any errors found.
1940 """
1941 line = clean_lines.elided[linenum]
1942
1943 # Remove all \\ (escaped backslashes) from the line. They are OK, and the
1944 # second (escaped) slash may trigger later \" detection erroneously.
1945 line = line.replace('\\\\', '')
1946
1947 if line.count('/*') > line.count('*/'):
1948 error(filename, linenum, 'readability/multiline_comment', 5,
1949 'Complex multi-line /*...*/-style comment found. '
1950 'Lint may give bogus warnings. '
1951 'Consider replacing these with //-style comments, '
1952 'with #if 0...#endif, '
1953 'or with more clearly structured multi-line comments.')
1954
1955 if (line.count('"') - line.count('\\"')) % 2:
1956 error(filename, linenum, 'readability/multiline_string', 5,
1957 'Multi-line string ("...") found. This lint script doesn\'t '
erg@google.com2aa59982013-10-28 19:09:25 +00001958 'do well with such strings, and may give bogus warnings. '
1959 'Use C++11 raw strings or concatenation instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00001960
1961
avakulenko@google.com02af6282014-06-04 18:53:25 +00001962# (non-threadsafe name, thread-safe alternative, validation pattern)
1963#
1964# The validation pattern is used to eliminate false positives such as:
1965# _rand(); // false positive due to substring match.
1966# ->rand(); // some member function rand().
1967# ACMRandom rand(seed); // some variable named rand.
1968# ISAACRandom rand(); // another variable named rand.
1969#
1970# Basically we require the return value of these functions to be used
1971# in some expression context on the same line by matching on some
1972# operator before the function name. This eliminates constructors and
1973# member function calls.
1974_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)'
1975_THREADING_LIST = (
1976 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'),
1977 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'),
1978 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'),
1979 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'),
1980 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'),
1981 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'),
1982 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'),
1983 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'),
1984 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'),
1985 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'),
1986 ('strtok(', 'strtok_r(',
1987 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'),
1988 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'),
erg@google.com4e00b9a2009-01-12 23:05:11 +00001989 )
1990
1991
1992def CheckPosixThreading(filename, clean_lines, linenum, error):
1993 """Checks for calls to thread-unsafe functions.
1994
1995 Much code has been originally written without consideration of
1996 multi-threading. Also, engineers are relying on their old experience;
1997 they have learned posix before threading extensions were added. These
1998 tests guide the engineers to use thread-safe functions (when using
1999 posix directly).
2000
2001 Args:
2002 filename: The name of the current file.
2003 clean_lines: A CleansedLines instance containing the file.
2004 linenum: The number of the line to check.
2005 error: The function to call with any errors found.
2006 """
2007 line = clean_lines.elided[linenum]
avakulenko@google.com02af6282014-06-04 18:53:25 +00002008 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST:
2009 # Additional pattern matching check to confirm that this is the
2010 # function we are looking for
2011 if Search(pattern, line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002012 error(filename, linenum, 'runtime/threadsafe_fn', 2,
avakulenko@google.com02af6282014-06-04 18:53:25 +00002013 'Consider using ' + multithread_safe_func +
2014 '...) instead of ' + single_thread_func +
erg@google.com4e00b9a2009-01-12 23:05:11 +00002015 '...) for improved thread safety.')
2016
2017
erg@google.com2aa59982013-10-28 19:09:25 +00002018def CheckVlogArguments(filename, clean_lines, linenum, error):
2019 """Checks that VLOG() is only used for defining a logging level.
2020
2021 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and
2022 VLOG(FATAL) are not.
2023
2024 Args:
2025 filename: The name of the current file.
2026 clean_lines: A CleansedLines instance containing the file.
2027 linenum: The number of the line to check.
2028 error: The function to call with any errors found.
2029 """
2030 line = clean_lines.elided[linenum]
2031 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line):
2032 error(filename, linenum, 'runtime/vlog', 5,
2033 'VLOG() should be used with numeric verbosity level. '
2034 'Use LOG() if you want symbolic severity levels.')
2035
erg@google.coma868d2d2009-10-09 21:18:45 +00002036# Matches invalid increment: *count++, which moves pointer instead of
erg@google.com36649102009-03-25 21:18:36 +00002037# incrementing a value.
erg@google.coma868d2d2009-10-09 21:18:45 +00002038_RE_PATTERN_INVALID_INCREMENT = re.compile(
erg@google.com36649102009-03-25 21:18:36 +00002039 r'^\s*\*\w+(\+\+|--);')
2040
2041
2042def CheckInvalidIncrement(filename, clean_lines, linenum, error):
erg@google.coma868d2d2009-10-09 21:18:45 +00002043 """Checks for invalid increment *count++.
erg@google.com36649102009-03-25 21:18:36 +00002044
2045 For example following function:
2046 void increment_counter(int* count) {
2047 *count++;
2048 }
2049 is invalid, because it effectively does count++, moving pointer, and should
2050 be replaced with ++*count, (*count)++ or *count += 1.
2051
2052 Args:
2053 filename: The name of the current file.
2054 clean_lines: A CleansedLines instance containing the file.
2055 linenum: The number of the line to check.
2056 error: The function to call with any errors found.
2057 """
2058 line = clean_lines.elided[linenum]
erg@google.coma868d2d2009-10-09 21:18:45 +00002059 if _RE_PATTERN_INVALID_INCREMENT.match(line):
erg@google.com36649102009-03-25 21:18:36 +00002060 error(filename, linenum, 'runtime/invalid_increment', 5,
2061 'Changing pointer instead of value (or unused value of operator*).')
2062
2063
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002064def IsMacroDefinition(clean_lines, linenum):
2065 if Search(r'^#define', clean_lines[linenum]):
2066 return True
2067
2068 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]):
2069 return True
2070
2071 return False
2072
2073
2074def IsForwardClassDeclaration(clean_lines, linenum):
2075 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum])
2076
2077
erg@google.comd350fe52013-01-14 17:51:48 +00002078class _BlockInfo(object):
2079 """Stores information about a generic block of code."""
2080
Alex Vakulenko01e47232016-05-06 13:54:15 -07002081 def __init__(self, linenum, seen_open_brace):
2082 self.starting_linenum = linenum
erg@google.comd350fe52013-01-14 17:51:48 +00002083 self.seen_open_brace = seen_open_brace
2084 self.open_parentheses = 0
2085 self.inline_asm = _NO_ASM
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002086 self.check_namespace_indentation = False
erg@google.comd350fe52013-01-14 17:51:48 +00002087
2088 def CheckBegin(self, filename, clean_lines, linenum, error):
2089 """Run checks that applies to text up to the opening brace.
2090
2091 This is mostly for checking the text after the class identifier
2092 and the "{", usually where the base class is specified. For other
2093 blocks, there isn't much to check, so we always pass.
2094
2095 Args:
2096 filename: The name of the current file.
2097 clean_lines: A CleansedLines instance containing the file.
2098 linenum: The number of the line to check.
2099 error: The function to call with any errors found.
2100 """
2101 pass
2102
2103 def CheckEnd(self, filename, clean_lines, linenum, error):
2104 """Run checks that applies to text after the closing brace.
2105
2106 This is mostly used for checking end of namespace comments.
2107
2108 Args:
2109 filename: The name of the current file.
2110 clean_lines: A CleansedLines instance containing the file.
2111 linenum: The number of the line to check.
2112 error: The function to call with any errors found.
2113 """
2114 pass
2115
avakulenko@google.com02af6282014-06-04 18:53:25 +00002116 def IsBlockInfo(self):
2117 """Returns true if this block is a _BlockInfo.
2118
2119 This is convenient for verifying that an object is an instance of
2120 a _BlockInfo, but not an instance of any of the derived classes.
2121
2122 Returns:
2123 True for this class, False for derived classes.
2124 """
2125 return self.__class__ == _BlockInfo
2126
2127
2128class _ExternCInfo(_BlockInfo):
2129 """Stores information about an 'extern "C"' block."""
2130
Alex Vakulenko01e47232016-05-06 13:54:15 -07002131 def __init__(self, linenum):
2132 _BlockInfo.__init__(self, linenum, True)
avakulenko@google.com02af6282014-06-04 18:53:25 +00002133
erg@google.comd350fe52013-01-14 17:51:48 +00002134
2135class _ClassInfo(_BlockInfo):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002136 """Stores information about a class."""
2137
erg@google.comd350fe52013-01-14 17:51:48 +00002138 def __init__(self, name, class_or_struct, clean_lines, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002139 _BlockInfo.__init__(self, linenum, False)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002140 self.name = name
erg@google.com4e00b9a2009-01-12 23:05:11 +00002141 self.is_derived = False
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002142 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002143 if class_or_struct == 'struct':
2144 self.access = 'public'
erg@google.comfd5da632013-10-25 17:39:45 +00002145 self.is_struct = True
erg@google.comd350fe52013-01-14 17:51:48 +00002146 else:
2147 self.access = 'private'
erg@google.comfd5da632013-10-25 17:39:45 +00002148 self.is_struct = False
2149
2150 # Remember initial indentation level for this class. Using raw_lines here
erg@google.comc6671232013-10-25 21:44:03 +00002151 # instead of elided to account for leading comments.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002152 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum])
erg@google.com4e00b9a2009-01-12 23:05:11 +00002153
erg@google.com8a95ecc2011-09-08 00:45:54 +00002154 # Try to find the end of the class. This will be confused by things like:
2155 # class A {
2156 # } *x = { ...
2157 #
2158 # But it's still good enough for CheckSectionSpacing.
2159 self.last_line = 0
2160 depth = 0
2161 for i in range(linenum, clean_lines.NumLines()):
erg@google.comd350fe52013-01-14 17:51:48 +00002162 line = clean_lines.elided[i]
erg@google.com8a95ecc2011-09-08 00:45:54 +00002163 depth += line.count('{') - line.count('}')
2164 if not depth:
2165 self.last_line = i
2166 break
2167
erg@google.comd350fe52013-01-14 17:51:48 +00002168 def CheckBegin(self, filename, clean_lines, linenum, error):
2169 # Look for a bare ':'
2170 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]):
2171 self.is_derived = True
erg@google.com4e00b9a2009-01-12 23:05:11 +00002172
erg@google.comfd5da632013-10-25 17:39:45 +00002173 def CheckEnd(self, filename, clean_lines, linenum, error):
avakulenko@google.com554223d2014-12-04 22:00:20 +00002174 # If there is a DISALLOW macro, it should appear near the end of
2175 # the class.
2176 seen_last_thing_in_class = False
2177 for i in xrange(linenum - 1, self.starting_linenum, -1):
2178 match = Search(
2179 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
2180 self.name + r'\)',
2181 clean_lines.elided[i])
2182 if match:
2183 if seen_last_thing_in_class:
2184 error(filename, i, 'readability/constructors', 3,
2185 match.group(1) + ' should be the last thing in the class')
2186 break
2187
2188 if not Match(r'^\s*$', clean_lines.elided[i]):
2189 seen_last_thing_in_class = True
2190
erg@google.comfd5da632013-10-25 17:39:45 +00002191 # Check that closing brace is aligned with beginning of the class.
2192 # Only do this if the closing brace is indented by only whitespaces.
2193 # This means we will not check single-line class definitions.
2194 indent = Match(r'^( *)\}', clean_lines.elided[linenum])
2195 if indent and len(indent.group(1)) != self.class_indent:
2196 if self.is_struct:
2197 parent = 'struct ' + self.name
2198 else:
2199 parent = 'class ' + self.name
2200 error(filename, linenum, 'whitespace/indent', 3,
2201 'Closing brace should be aligned with beginning of %s' % parent)
2202
erg@google.com4e00b9a2009-01-12 23:05:11 +00002203
erg@google.comd350fe52013-01-14 17:51:48 +00002204class _NamespaceInfo(_BlockInfo):
2205 """Stores information about a namespace."""
2206
2207 def __init__(self, name, linenum):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002208 _BlockInfo.__init__(self, linenum, False)
erg@google.comd350fe52013-01-14 17:51:48 +00002209 self.name = name or ''
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002210 self.check_namespace_indentation = True
erg@google.comd350fe52013-01-14 17:51:48 +00002211
2212 def CheckEnd(self, filename, clean_lines, linenum, error):
2213 """Check end of namespace comments."""
2214 line = clean_lines.raw_lines[linenum]
2215
2216 # Check how many lines is enclosed in this namespace. Don't issue
2217 # warning for missing namespace comments if there aren't enough
2218 # lines. However, do apply checks if there is already an end of
2219 # namespace comment and it's incorrect.
2220 #
2221 # TODO(unknown): We always want to check end of namespace comments
2222 # if a namespace is large, but sometimes we also want to apply the
2223 # check if a short namespace contained nontrivial things (something
2224 # other than forward declarations). There is currently no logic on
2225 # deciding what these nontrivial things are, so this check is
2226 # triggered by namespace size only, which works most of the time.
2227 if (linenum - self.starting_linenum < 10
Alex Vakulenko01e47232016-05-06 13:54:15 -07002228 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)):
erg@google.comd350fe52013-01-14 17:51:48 +00002229 return
2230
2231 # Look for matching comment at end of namespace.
2232 #
2233 # Note that we accept C style "/* */" comments for terminating
2234 # namespaces, so that code that terminate namespaces inside
erg@google.comc6671232013-10-25 21:44:03 +00002235 # preprocessor macros can be cpplint clean.
erg@google.comd350fe52013-01-14 17:51:48 +00002236 #
2237 # We also accept stuff like "// end of namespace <name>." with the
2238 # period at the end.
2239 #
2240 # Besides these, we don't accept anything else, otherwise we might
2241 # get false negatives when existing comment is a substring of the
erg@google.comc6671232013-10-25 21:44:03 +00002242 # expected namespace.
erg@google.comd350fe52013-01-14 17:51:48 +00002243 if self.name:
2244 # Named namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002245 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' +
2246 re.escape(self.name) + r'[\*/\.\\\s]*$'),
erg@google.comd350fe52013-01-14 17:51:48 +00002247 line):
2248 error(filename, linenum, 'readability/namespace', 5,
2249 'Namespace should be terminated with "// namespace %s"' %
2250 self.name)
2251 else:
2252 # Anonymous namespace
Alex Vakulenko01e47232016-05-06 13:54:15 -07002253 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002254 # If "// namespace anonymous" or "// anonymous namespace (more text)",
2255 # mention "// anonymous namespace" as an acceptable form
Alex Vakulenko01e47232016-05-06 13:54:15 -07002256 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002257 error(filename, linenum, 'readability/namespace', 5,
2258 'Anonymous namespace should be terminated with "// namespace"'
2259 ' or "// anonymous namespace"')
2260 else:
2261 error(filename, linenum, 'readability/namespace', 5,
2262 'Anonymous namespace should be terminated with "// namespace"')
erg@google.comd350fe52013-01-14 17:51:48 +00002263
2264
2265class _PreprocessorInfo(object):
2266 """Stores checkpoints of nesting stacks when #if/#else is seen."""
2267
2268 def __init__(self, stack_before_if):
2269 # The entire nesting stack before #if
2270 self.stack_before_if = stack_before_if
2271
2272 # The entire nesting stack up to #else
2273 self.stack_before_else = []
2274
2275 # Whether we have already seen #else or #elif
2276 self.seen_else = False
2277
2278
avakulenko@google.com02af6282014-06-04 18:53:25 +00002279class NestingState(object):
erg@google.comd350fe52013-01-14 17:51:48 +00002280 """Holds states related to parsing braces."""
erg@google.com4e00b9a2009-01-12 23:05:11 +00002281
2282 def __init__(self):
erg@google.comd350fe52013-01-14 17:51:48 +00002283 # Stack for tracking all braces. An object is pushed whenever we
2284 # see a "{", and popped when we see a "}". Only 3 types of
2285 # objects are possible:
2286 # - _ClassInfo: a class or struct.
2287 # - _NamespaceInfo: a namespace.
2288 # - _BlockInfo: some other type of block.
2289 self.stack = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00002290
avakulenko@google.com02af6282014-06-04 18:53:25 +00002291 # Top of the previous stack before each Update().
2292 #
2293 # Because the nesting_stack is updated at the end of each line, we
2294 # had to do some convoluted checks to find out what is the current
2295 # scope at the beginning of the line. This check is simplified by
2296 # saving the previous top of nesting stack.
2297 #
2298 # We could save the full stack, but we only need the top. Copying
2299 # the full nesting stack would slow down cpplint by ~10%.
2300 self.previous_stack_top = []
2301
erg@google.comd350fe52013-01-14 17:51:48 +00002302 # Stack of _PreprocessorInfo objects.
2303 self.pp_stack = []
2304
2305 def SeenOpenBrace(self):
2306 """Check if we have seen the opening brace for the innermost block.
2307
2308 Returns:
2309 True if we have seen the opening brace, False if the innermost
2310 block is still expecting an opening brace.
2311 """
2312 return (not self.stack) or self.stack[-1].seen_open_brace
2313
2314 def InNamespaceBody(self):
2315 """Check if we are currently one level inside a namespace body.
2316
2317 Returns:
2318 True if top of the stack is a namespace block, False otherwise.
2319 """
2320 return self.stack and isinstance(self.stack[-1], _NamespaceInfo)
2321
avakulenko@google.com02af6282014-06-04 18:53:25 +00002322 def InExternC(self):
2323 """Check if we are currently one level inside an 'extern "C"' block.
2324
2325 Returns:
2326 True if top of the stack is an extern block, False otherwise.
2327 """
2328 return self.stack and isinstance(self.stack[-1], _ExternCInfo)
2329
2330 def InClassDeclaration(self):
2331 """Check if we are currently one level inside a class or struct declaration.
2332
2333 Returns:
2334 True if top of the stack is a class/struct, False otherwise.
2335 """
2336 return self.stack and isinstance(self.stack[-1], _ClassInfo)
2337
2338 def InAsmBlock(self):
2339 """Check if we are currently one level inside an inline ASM block.
2340
2341 Returns:
2342 True if the top of the stack is a block containing inline ASM.
2343 """
2344 return self.stack and self.stack[-1].inline_asm != _NO_ASM
2345
2346 def InTemplateArgumentList(self, clean_lines, linenum, pos):
2347 """Check if current position is inside template argument list.
2348
2349 Args:
2350 clean_lines: A CleansedLines instance containing the file.
2351 linenum: The number of the line to check.
2352 pos: position just after the suspected template argument.
2353 Returns:
2354 True if (linenum, pos) is inside template arguments.
2355 """
2356 while linenum < clean_lines.NumLines():
2357 # Find the earliest character that might indicate a template argument
2358 line = clean_lines.elided[linenum]
2359 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:])
2360 if not match:
2361 linenum += 1
2362 pos = 0
2363 continue
2364 token = match.group(1)
2365 pos += len(match.group(0))
2366
2367 # These things do not look like template argument list:
2368 # class Suspect {
2369 # class Suspect x; }
2370 if token in ('{', '}', ';'): return False
2371
2372 # These things look like template argument list:
2373 # template <class Suspect>
2374 # template <class Suspect = default_value>
2375 # template <class Suspect[]>
2376 # template <class Suspect...>
2377 if token in ('>', '=', '[', ']', '.'): return True
2378
2379 # Check if token is an unmatched '<'.
2380 # If not, move on to the next character.
2381 if token != '<':
2382 pos += 1
2383 if pos >= len(line):
2384 linenum += 1
2385 pos = 0
2386 continue
2387
2388 # We can't be sure if we just find a single '<', and need to
2389 # find the matching '>'.
2390 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1)
2391 if end_pos < 0:
2392 # Not sure if template argument list or syntax error in file
2393 return False
2394 linenum = end_line
2395 pos = end_pos
2396 return False
2397
erg@google.comd350fe52013-01-14 17:51:48 +00002398 def UpdatePreprocessor(self, line):
2399 """Update preprocessor stack.
2400
2401 We need to handle preprocessors due to classes like this:
2402 #ifdef SWIG
2403 struct ResultDetailsPageElementExtensionPoint {
2404 #else
2405 struct ResultDetailsPageElementExtensionPoint : public Extension {
2406 #endif
erg@google.comd350fe52013-01-14 17:51:48 +00002407
2408 We make the following assumptions (good enough for most files):
2409 - Preprocessor condition evaluates to true from #if up to first
2410 #else/#elif/#endif.
2411
2412 - Preprocessor condition evaluates to false from #else/#elif up
2413 to #endif. We still perform lint checks on these lines, but
2414 these do not affect nesting stack.
2415
2416 Args:
2417 line: current line to check.
2418 """
2419 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line):
2420 # Beginning of #if block, save the nesting stack here. The saved
2421 # stack will allow us to restore the parsing state in the #else case.
2422 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack)))
2423 elif Match(r'^\s*#\s*(else|elif)\b', line):
2424 # Beginning of #else block
2425 if self.pp_stack:
2426 if not self.pp_stack[-1].seen_else:
2427 # This is the first #else or #elif block. Remember the
2428 # whole nesting stack up to this point. This is what we
2429 # keep after the #endif.
2430 self.pp_stack[-1].seen_else = True
2431 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack)
2432
2433 # Restore the stack to how it was before the #if
2434 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if)
2435 else:
2436 # TODO(unknown): unexpected #else, issue warning?
2437 pass
2438 elif Match(r'^\s*#\s*endif\b', line):
2439 # End of #if or #else blocks.
2440 if self.pp_stack:
2441 # If we saw an #else, we will need to restore the nesting
2442 # stack to its former state before the #else, otherwise we
2443 # will just continue from where we left off.
2444 if self.pp_stack[-1].seen_else:
2445 # Here we can just use a shallow copy since we are the last
2446 # reference to it.
2447 self.stack = self.pp_stack[-1].stack_before_else
2448 # Drop the corresponding #if
2449 self.pp_stack.pop()
2450 else:
2451 # TODO(unknown): unexpected #endif, issue warning?
2452 pass
2453
avakulenko@google.com02af6282014-06-04 18:53:25 +00002454 # TODO(unknown): Update() is too long, but we will refactor later.
erg@google.comd350fe52013-01-14 17:51:48 +00002455 def Update(self, filename, clean_lines, linenum, error):
2456 """Update nesting state with current line.
2457
2458 Args:
2459 filename: The name of the current file.
2460 clean_lines: A CleansedLines instance containing the file.
2461 linenum: The number of the line to check.
2462 error: The function to call with any errors found.
2463 """
2464 line = clean_lines.elided[linenum]
2465
avakulenko@google.com02af6282014-06-04 18:53:25 +00002466 # Remember top of the previous nesting stack.
2467 #
2468 # The stack is always pushed/popped and not modified in place, so
2469 # we can just do a shallow copy instead of copy.deepcopy. Using
2470 # deepcopy would slow down cpplint by ~28%.
2471 if self.stack:
2472 self.previous_stack_top = self.stack[-1]
2473 else:
2474 self.previous_stack_top = None
2475
2476 # Update pp_stack
erg@google.comd350fe52013-01-14 17:51:48 +00002477 self.UpdatePreprocessor(line)
2478
2479 # Count parentheses. This is to avoid adding struct arguments to
2480 # the nesting stack.
2481 if self.stack:
2482 inner_block = self.stack[-1]
2483 depth_change = line.count('(') - line.count(')')
2484 inner_block.open_parentheses += depth_change
2485
2486 # Also check if we are starting or ending an inline assembly block.
2487 if inner_block.inline_asm in (_NO_ASM, _END_ASM):
2488 if (depth_change != 0 and
2489 inner_block.open_parentheses == 1 and
2490 _MATCH_ASM.match(line)):
2491 # Enter assembly block
2492 inner_block.inline_asm = _INSIDE_ASM
2493 else:
2494 # Not entering assembly block. If previous line was _END_ASM,
2495 # we will now shift to _NO_ASM state.
2496 inner_block.inline_asm = _NO_ASM
2497 elif (inner_block.inline_asm == _INSIDE_ASM and
2498 inner_block.open_parentheses == 0):
2499 # Exit assembly block
2500 inner_block.inline_asm = _END_ASM
2501
2502 # Consume namespace declaration at the beginning of the line. Do
2503 # this in a loop so that we catch same line declarations like this:
2504 # namespace proto2 { namespace bridge { class MessageSet; } }
2505 while True:
2506 # Match start of namespace. The "\b\s*" below catches namespace
2507 # declarations even if it weren't followed by a whitespace, this
2508 # is so that we don't confuse our namespace checker. The
2509 # missing spaces will be flagged by CheckSpacing.
2510 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line)
2511 if not namespace_decl_match:
2512 break
2513
2514 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum)
2515 self.stack.append(new_namespace)
2516
2517 line = namespace_decl_match.group(2)
2518 if line.find('{') != -1:
2519 new_namespace.seen_open_brace = True
2520 line = line[line.find('{') + 1:]
2521
2522 # Look for a class declaration in whatever is left of the line
2523 # after parsing namespaces. The regexp accounts for decorated classes
2524 # such as in:
2525 # class LOCKABLE API Object {
2526 # };
erg@google.comd350fe52013-01-14 17:51:48 +00002527 class_decl_match = Match(
avakulenko@google.com02af6282014-06-04 18:53:25 +00002528 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?'
2529 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))'
2530 r'(.*)$', line)
erg@google.comd350fe52013-01-14 17:51:48 +00002531 if (class_decl_match and
2532 (not self.stack or self.stack[-1].open_parentheses == 0)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002533 # We do not want to accept classes that are actually template arguments:
2534 # template <class Ignore1,
2535 # class Ignore2 = Default<Args>,
2536 # template <Args> class Ignore3>
2537 # void Function() {};
2538 #
2539 # To avoid template argument cases, we scan forward and look for
2540 # an unmatched '>'. If we see one, assume we are inside a
2541 # template argument list.
2542 end_declaration = len(class_decl_match.group(1))
2543 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration):
2544 self.stack.append(_ClassInfo(
2545 class_decl_match.group(3), class_decl_match.group(2),
2546 clean_lines, linenum))
2547 line = class_decl_match.group(4)
erg@google.comd350fe52013-01-14 17:51:48 +00002548
2549 # If we have not yet seen the opening brace for the innermost block,
2550 # run checks here.
2551 if not self.SeenOpenBrace():
2552 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error)
2553
2554 # Update access control if we are inside a class/struct
2555 if self.stack and isinstance(self.stack[-1], _ClassInfo):
erg@google.comfd5da632013-10-25 17:39:45 +00002556 classinfo = self.stack[-1]
2557 access_match = Match(
2558 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?'
2559 r':(?:[^:]|$)',
2560 line)
erg@google.comd350fe52013-01-14 17:51:48 +00002561 if access_match:
erg@google.comfd5da632013-10-25 17:39:45 +00002562 classinfo.access = access_match.group(2)
2563
2564 # Check that access keywords are indented +1 space. Skip this
erg@google.comc6671232013-10-25 21:44:03 +00002565 # check if the keywords are not preceded by whitespaces.
erg@google.comfd5da632013-10-25 17:39:45 +00002566 indent = access_match.group(1)
2567 if (len(indent) != classinfo.class_indent + 1 and
2568 Match(r'^\s*$', indent)):
2569 if classinfo.is_struct:
2570 parent = 'struct ' + classinfo.name
2571 else:
2572 parent = 'class ' + classinfo.name
2573 slots = ''
2574 if access_match.group(3):
2575 slots = access_match.group(3)
2576 error(filename, linenum, 'whitespace/indent', 3,
2577 '%s%s: should be indented +1 space inside %s' % (
2578 access_match.group(2), slots, parent))
erg@google.comd350fe52013-01-14 17:51:48 +00002579
2580 # Consume braces or semicolons from what's left of the line
2581 while True:
2582 # Match first brace, semicolon, or closed parenthesis.
2583 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line)
2584 if not matched:
2585 break
2586
2587 token = matched.group(1)
2588 if token == '{':
2589 # If namespace or class hasn't seen a opening brace yet, mark
2590 # namespace/class head as complete. Push a new block onto the
2591 # stack otherwise.
2592 if not self.SeenOpenBrace():
2593 self.stack[-1].seen_open_brace = True
avakulenko@google.com02af6282014-06-04 18:53:25 +00002594 elif Match(r'^extern\s*"[^"]*"\s*\{', line):
Alex Vakulenko01e47232016-05-06 13:54:15 -07002595 self.stack.append(_ExternCInfo(linenum))
erg@google.comd350fe52013-01-14 17:51:48 +00002596 else:
Alex Vakulenko01e47232016-05-06 13:54:15 -07002597 self.stack.append(_BlockInfo(linenum, True))
erg@google.comd350fe52013-01-14 17:51:48 +00002598 if _MATCH_ASM.match(line):
2599 self.stack[-1].inline_asm = _BLOCK_ASM
avakulenko@google.com02af6282014-06-04 18:53:25 +00002600
erg@google.comd350fe52013-01-14 17:51:48 +00002601 elif token == ';' or token == ')':
2602 # If we haven't seen an opening brace yet, but we already saw
2603 # a semicolon, this is probably a forward declaration. Pop
2604 # the stack for these.
2605 #
2606 # Similarly, if we haven't seen an opening brace yet, but we
2607 # already saw a closing parenthesis, then these are probably
2608 # function arguments with extra "class" or "struct" keywords.
2609 # Also pop these stack for these.
2610 if not self.SeenOpenBrace():
2611 self.stack.pop()
2612 else: # token == '}'
2613 # Perform end of block checks and pop the stack.
2614 if self.stack:
2615 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error)
2616 self.stack.pop()
2617 line = matched.group(2)
2618
2619 def InnermostClass(self):
2620 """Get class info on the top of the stack.
2621
2622 Returns:
2623 A _ClassInfo object if we are inside a class, or None otherwise.
2624 """
2625 for i in range(len(self.stack), 0, -1):
2626 classinfo = self.stack[i - 1]
2627 if isinstance(classinfo, _ClassInfo):
2628 return classinfo
2629 return None
2630
erg@google.com2aa59982013-10-28 19:09:25 +00002631 def CheckCompletedBlocks(self, filename, error):
2632 """Checks that all classes and namespaces have been completely parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002633
2634 Call this when all lines in a file have been processed.
2635 Args:
2636 filename: The name of the current file.
2637 error: The function to call with any errors found.
2638 """
erg@google.comd350fe52013-01-14 17:51:48 +00002639 # Note: This test can result in false positives if #ifdef constructs
2640 # get in the way of brace matching. See the testBuildClass test in
2641 # cpplint_unittest.py for an example of this.
2642 for obj in self.stack:
2643 if isinstance(obj, _ClassInfo):
2644 error(filename, obj.starting_linenum, 'build/class', 5,
2645 'Failed to find complete declaration of class %s' %
2646 obj.name)
erg@google.com2aa59982013-10-28 19:09:25 +00002647 elif isinstance(obj, _NamespaceInfo):
2648 error(filename, obj.starting_linenum, 'build/namespaces', 5,
2649 'Failed to find complete declaration of namespace %s' %
2650 obj.name)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002651
2652
2653def CheckForNonStandardConstructs(filename, clean_lines, linenum,
erg@google.comd350fe52013-01-14 17:51:48 +00002654 nesting_state, error):
erg@google.com2aa59982013-10-28 19:09:25 +00002655 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002656
2657 Complain about several constructs which gcc-2 accepts, but which are
2658 not standard C++. Warning about these in lint is one way to ease the
2659 transition to new compilers.
2660 - put storage class first (e.g. "static const" instead of "const static").
2661 - "%lld" instead of %qd" in printf-type functions.
2662 - "%1$d" is non-standard in printf-type functions.
2663 - "\%" is an undefined character escape sequence.
2664 - text after #endif is not allowed.
2665 - invalid inner-style forward declaration.
2666 - >? and <? operators, and their >?= and <?= cousins.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002667
erg@google.coma868d2d2009-10-09 21:18:45 +00002668 Additionally, check for constructor/destructor style violations and reference
2669 members, as it is very convenient to do so while checking for
2670 gcc-2 compliance.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002671
2672 Args:
2673 filename: The name of the current file.
2674 clean_lines: A CleansedLines instance containing the file.
2675 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002676 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00002677 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002678 error: A callable to which errors are reported, which takes 4 arguments:
2679 filename, line number, error level, and message
2680 """
2681
2682 # Remove comments from the line, but leave in strings for now.
2683 line = clean_lines.lines[linenum]
2684
2685 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
2686 error(filename, linenum, 'runtime/printf_format', 3,
2687 '%q in format strings is deprecated. Use %ll instead.')
2688
2689 if Search(r'printf\s*\(.*".*%\d+\$', line):
2690 error(filename, linenum, 'runtime/printf_format', 2,
2691 '%N$ formats are unconventional. Try rewriting to avoid them.')
2692
2693 # Remove escaped backslashes before looking for undefined escapes.
2694 line = line.replace('\\\\', '')
2695
2696 if Search(r'("|\').*\\(%|\[|\(|{)', line):
2697 error(filename, linenum, 'build/printf_format', 3,
2698 '%, [, (, and { are undefined character escapes. Unescape them.')
2699
2700 # For the rest, work with both comments and strings removed.
2701 line = clean_lines.elided[linenum]
2702
2703 if Search(r'\b(const|volatile|void|char|short|int|long'
2704 r'|float|double|signed|unsigned'
2705 r'|schar|u?int8|u?int16|u?int32|u?int64)'
erg@google.comd350fe52013-01-14 17:51:48 +00002706 r'\s+(register|static|extern|typedef)\b',
erg@google.com4e00b9a2009-01-12 23:05:11 +00002707 line):
2708 error(filename, linenum, 'build/storage_class', 5,
Alex Vakulenko01e47232016-05-06 13:54:15 -07002709 'Storage-class specifier (static, extern, typedef, etc) should be '
2710 'at the beginning of the declaration.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002711
2712 if Match(r'\s*#\s*endif\s*[^/\s]+', line):
2713 error(filename, linenum, 'build/endif_comment', 5,
2714 'Uncommented text after #endif is non-standard. Use a comment.')
2715
2716 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
2717 error(filename, linenum, 'build/forward_decl', 5,
2718 'Inner-style forward declarations are invalid. Remove this line.')
2719
2720 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
2721 line):
2722 error(filename, linenum, 'build/deprecated', 3,
2723 '>? and <? (max and min) operators are non-standard and deprecated.')
2724
erg@google.coma868d2d2009-10-09 21:18:45 +00002725 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
2726 # TODO(unknown): Could it be expanded safely to arbitrary references,
2727 # without triggering too many false positives? The first
2728 # attempt triggered 5 warnings for mostly benign code in the regtest, hence
2729 # the restriction.
2730 # Here's the original regexp, for the reference:
2731 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
2732 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
2733 error(filename, linenum, 'runtime/member_string_references', 2,
2734 'const string& members are dangerous. It is much better to use '
2735 'alternatives, such as pointers or simple constants.')
2736
erg@google.comd350fe52013-01-14 17:51:48 +00002737 # Everything else in this function operates on class declarations.
2738 # Return early if the top of the nesting stack is not a class, or if
2739 # the class head is not completed yet.
2740 classinfo = nesting_state.InnermostClass()
2741 if not classinfo or not classinfo.seen_open_brace:
erg@google.com4e00b9a2009-01-12 23:05:11 +00002742 return
2743
erg@google.com4e00b9a2009-01-12 23:05:11 +00002744 # The class may have been declared with namespace or classname qualifiers.
2745 # The constructor and destructor will not have those qualifiers.
2746 base_classname = classinfo.name.split('::')[-1]
2747
2748 # Look for single-argument constructors that aren't marked explicit.
Alex Vakulenko01e47232016-05-06 13:54:15 -07002749 # Technically a valid construct, but against style.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002750 explicit_constructor_match = Match(
2751 r'\s+(?:inline\s+)?(explicit\s+)?(?:inline\s+)?%s\s*'
2752 r'\(((?:[^()]|\([^()]*\))*)\)'
2753 % re.escape(base_classname),
2754 line)
2755
2756 if explicit_constructor_match:
2757 is_marked_explicit = explicit_constructor_match.group(1)
2758
2759 if not explicit_constructor_match.group(2):
2760 constructor_args = []
2761 else:
2762 constructor_args = explicit_constructor_match.group(2).split(',')
2763
2764 # collapse arguments so that commas in template parameter lists and function
2765 # argument parameter lists don't split arguments in two
2766 i = 0
2767 while i < len(constructor_args):
2768 constructor_arg = constructor_args[i]
2769 while (constructor_arg.count('<') > constructor_arg.count('>') or
2770 constructor_arg.count('(') > constructor_arg.count(')')):
2771 constructor_arg += ',' + constructor_args[i + 1]
2772 del constructor_args[i + 1]
2773 constructor_args[i] = constructor_arg
2774 i += 1
2775
2776 defaulted_args = [arg for arg in constructor_args if '=' in arg]
2777 noarg_constructor = (not constructor_args or # empty arg list
2778 # 'void' arg specifier
2779 (len(constructor_args) == 1 and
2780 constructor_args[0].strip() == 'void'))
2781 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg
2782 not noarg_constructor) or
2783 # all but at most one arg defaulted
2784 (len(constructor_args) >= 1 and
2785 not noarg_constructor and
2786 len(defaulted_args) >= len(constructor_args) - 1))
2787 initializer_list_constructor = bool(
2788 onearg_constructor and
2789 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0]))
2790 copy_constructor = bool(
2791 onearg_constructor and
2792 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&'
2793 % re.escape(base_classname), constructor_args[0].strip()))
2794
2795 if (not is_marked_explicit and
2796 onearg_constructor and
2797 not initializer_list_constructor and
2798 not copy_constructor):
2799 if defaulted_args:
2800 error(filename, linenum, 'runtime/explicit', 5,
2801 'Constructors callable with one argument '
2802 'should be marked explicit.')
2803 else:
2804 error(filename, linenum, 'runtime/explicit', 5,
2805 'Single-parameter constructors should be marked explicit.')
2806 elif is_marked_explicit and not onearg_constructor:
2807 if noarg_constructor:
2808 error(filename, linenum, 'runtime/explicit', 5,
2809 'Zero-parameter constructors should not be marked explicit.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002810
erg@google.com4e00b9a2009-01-12 23:05:11 +00002811
avakulenko@google.com02af6282014-06-04 18:53:25 +00002812def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002813 """Checks for the correctness of various spacing around function calls.
2814
2815 Args:
2816 filename: The name of the current file.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002817 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002818 linenum: The number of the line to check.
2819 error: The function to call with any errors found.
2820 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00002821 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002822
2823 # Since function calls often occur inside if/for/while/switch
2824 # expressions - which have their own, more liberal conventions - we
2825 # first see if we should be looking inside such an expression for a
2826 # function call, to which we can apply more strict standards.
2827 fncall = line # if there's no control flow construct, look at whole line
2828 for pattern in (r'\bif\s*\((.*)\)\s*{',
2829 r'\bfor\s*\((.*)\)\s*{',
2830 r'\bwhile\s*\((.*)\)\s*[{;]',
2831 r'\bswitch\s*\((.*)\)\s*{'):
2832 match = Search(pattern, line)
2833 if match:
2834 fncall = match.group(1) # look inside the parens for function calls
2835 break
2836
2837 # Except in if/for/while/switch, there should never be space
2838 # immediately inside parens (eg "f( 3, 4 )"). We make an exception
2839 # for nested parens ( (a+b) + c ). Likewise, there should never be
2840 # a space before a ( when it's a function argument. I assume it's a
2841 # function argument when the char before the whitespace is legal in
2842 # a function name (alnum + _) and we're not starting a macro. Also ignore
2843 # pointers and references to arrays and functions coz they're too tricky:
2844 # we use a very simple way to recognize these:
2845 # " (something)(maybe-something)" or
2846 # " (something)(maybe-something," or
2847 # " (something)[something]"
2848 # Note that we assume the contents of [] to be short enough that
2849 # they'll never need to wrap.
2850 if ( # Ignore control structures.
erg@google.com2aa59982013-10-28 19:09:25 +00002851 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b',
erg@google.comc6671232013-10-25 21:44:03 +00002852 fncall) and
erg@google.com4e00b9a2009-01-12 23:05:11 +00002853 # Ignore pointers/references to functions.
2854 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
2855 # Ignore pointers/references to arrays.
2856 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
erg@google.com36649102009-03-25 21:18:36 +00002857 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call
erg@google.com4e00b9a2009-01-12 23:05:11 +00002858 error(filename, linenum, 'whitespace/parens', 4,
2859 'Extra space after ( in function call')
erg@google.com36649102009-03-25 21:18:36 +00002860 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002861 error(filename, linenum, 'whitespace/parens', 2,
2862 'Extra space after (')
2863 if (Search(r'\w\s+\(', fncall) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07002864 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 +00002865 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and
avakulenko@google.com554223d2014-12-04 22:00:20 +00002866 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and
2867 not Search(r'\bcase\s+\(', fncall)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00002868 # TODO(unknown): Space after an operator function seem to be a common
2869 # error, silence those for now by restricting them to highest verbosity.
2870 if Search(r'\boperator_*\b', line):
2871 error(filename, linenum, 'whitespace/parens', 0,
2872 'Extra space before ( in function call')
2873 else:
2874 error(filename, linenum, 'whitespace/parens', 4,
2875 'Extra space before ( in function call')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002876 # If the ) is followed only by a newline or a { + newline, assume it's
2877 # part of a control statement (if/while/etc), and don't complain
2878 if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
erg@google.com8a95ecc2011-09-08 00:45:54 +00002879 # If the closing parenthesis is preceded by only whitespaces,
2880 # try to give a more descriptive error message.
2881 if Search(r'^\s+\)', fncall):
2882 error(filename, linenum, 'whitespace/parens', 2,
2883 'Closing ) should be moved to the previous line')
2884 else:
2885 error(filename, linenum, 'whitespace/parens', 2,
2886 'Extra space before )')
erg@google.com4e00b9a2009-01-12 23:05:11 +00002887
2888
2889def IsBlankLine(line):
2890 """Returns true if the given line is blank.
2891
2892 We consider a line to be blank if the line is empty or consists of
2893 only white spaces.
2894
2895 Args:
2896 line: A line of a string.
2897
2898 Returns:
2899 True, if the given line is blank.
2900 """
2901 return not line or line.isspace()
2902
2903
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00002904def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
2905 error):
2906 is_namespace_indent_item = (
2907 len(nesting_state.stack) > 1 and
2908 nesting_state.stack[-1].check_namespace_indentation and
2909 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and
2910 nesting_state.previous_stack_top == nesting_state.stack[-2])
2911
2912 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
2913 clean_lines.elided, line):
2914 CheckItemIndentationInNamespace(filename, clean_lines.elided,
2915 line, error)
2916
2917
erg@google.com4e00b9a2009-01-12 23:05:11 +00002918def CheckForFunctionLengths(filename, clean_lines, linenum,
2919 function_state, error):
2920 """Reports for long function bodies.
2921
2922 For an overview why this is done, see:
Ackermann Yuriy79692902016-04-01 21:41:34 +13002923 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
erg@google.com4e00b9a2009-01-12 23:05:11 +00002924
2925 Uses a simplistic algorithm assuming other style guidelines
2926 (especially spacing) are followed.
2927 Only checks unindented functions, so class members are unchecked.
2928 Trivial bodies are unchecked, so constructors with huge initializer lists
2929 may be missed.
2930 Blank/comment lines are not counted so as to avoid encouraging the removal
erg@google.com8a95ecc2011-09-08 00:45:54 +00002931 of vertical space and comments just to get through a lint check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002932 NOLINT *on the last line of a function* disables this check.
2933
2934 Args:
2935 filename: The name of the current file.
2936 clean_lines: A CleansedLines instance containing the file.
2937 linenum: The number of the line to check.
2938 function_state: Current function name and lines in body so far.
2939 error: The function to call with any errors found.
2940 """
2941 lines = clean_lines.lines
2942 line = lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00002943 joined_line = ''
2944
2945 starting_func = False
erg@google.coma87abb82009-02-24 01:41:01 +00002946 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ...
erg@google.com4e00b9a2009-01-12 23:05:11 +00002947 match_result = Match(regexp, line)
2948 if match_result:
2949 # If the name is all caps and underscores, figure it's a macro and
2950 # ignore it, unless it's TEST or TEST_F.
2951 function_name = match_result.group(1).split()[-1]
2952 if function_name == 'TEST' or function_name == 'TEST_F' or (
2953 not Match(r'[A-Z_]+$', function_name)):
2954 starting_func = True
2955
2956 if starting_func:
2957 body_found = False
erg@google.coma87abb82009-02-24 01:41:01 +00002958 for start_linenum in xrange(linenum, clean_lines.NumLines()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00002959 start_line = lines[start_linenum]
2960 joined_line += ' ' + start_line.lstrip()
2961 if Search(r'(;|})', start_line): # Declarations and trivial functions
2962 body_found = True
2963 break # ... ignore
2964 elif Search(r'{', start_line):
2965 body_found = True
2966 function = Search(r'((\w|:)*)\(', line).group(1)
2967 if Match(r'TEST', function): # Handle TEST... macros
2968 parameter_regexp = Search(r'(\(.*\))', joined_line)
2969 if parameter_regexp: # Ignore bad syntax
2970 function += parameter_regexp.group(1)
2971 else:
2972 function += '()'
2973 function_state.Begin(function)
2974 break
2975 if not body_found:
erg@google.coma87abb82009-02-24 01:41:01 +00002976 # No body for the function (or evidence of a non-function) was found.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002977 error(filename, linenum, 'readability/fn_size', 5,
2978 'Lint failed to find start of function body.')
2979 elif Match(r'^\}\s*$', line): # function end
erg+personal@google.com05189642010-04-30 20:43:03 +00002980 function_state.Check(error, filename, linenum)
erg@google.com4e00b9a2009-01-12 23:05:11 +00002981 function_state.End()
2982 elif not Match(r'^\s*$', line):
2983 function_state.Count() # Count non-blank/non-comment lines.
2984
2985
2986_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
2987
2988
avakulenko@google.com02af6282014-06-04 18:53:25 +00002989def CheckComment(line, filename, linenum, next_line_start, error):
2990 """Checks for common mistakes in comments.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002991
2992 Args:
avakulenko@google.com02af6282014-06-04 18:53:25 +00002993 line: The line in question.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002994 filename: The name of the current file.
2995 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00002996 next_line_start: The first non-whitespace column of the next line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00002997 error: The function to call with any errors found.
2998 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00002999 commentpos = line.find('//')
3000 if commentpos != -1:
3001 # Check if the // may be in quotes. If so, ignore it
Alex Vakulenko01e47232016-05-06 13:54:15 -07003002 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0:
avakulenko@google.com02af6282014-06-04 18:53:25 +00003003 # Allow one space for new scopes, two spaces otherwise:
3004 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and
3005 ((commentpos >= 1 and
3006 line[commentpos-1] not in string.whitespace) or
3007 (commentpos >= 2 and
3008 line[commentpos-2] not in string.whitespace))):
3009 error(filename, linenum, 'whitespace/comments', 2,
3010 'At least two spaces is best between code and comments')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003011
avakulenko@google.com02af6282014-06-04 18:53:25 +00003012 # Checks for common mistakes in TODO comments.
3013 comment = line[commentpos:]
3014 match = _RE_PATTERN_TODO.match(comment)
3015 if match:
3016 # One whitespace is correct; zero whitespace is handled elsewhere.
3017 leading_whitespace = match.group(1)
3018 if len(leading_whitespace) > 1:
3019 error(filename, linenum, 'whitespace/todo', 2,
3020 'Too many spaces before TODO')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003021
avakulenko@google.com02af6282014-06-04 18:53:25 +00003022 username = match.group(2)
3023 if not username:
3024 error(filename, linenum, 'readability/todo', 2,
3025 'Missing username in TODO; it should look like '
3026 '"// TODO(my_username): Stuff."')
3027
3028 middle_whitespace = match.group(3)
3029 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison
3030 if middle_whitespace != ' ' and middle_whitespace != '':
3031 error(filename, linenum, 'whitespace/todo', 2,
3032 'TODO(my_username) should be followed by a space')
3033
3034 # If the comment contains an alphanumeric character, there
avakulenko@google.com554223d2014-12-04 22:00:20 +00003035 # should be a space somewhere between it and the // unless
3036 # it's a /// or //! Doxygen comment.
3037 if (Match(r'//[^ ]*\w', comment) and
3038 not Match(r'(///|//\!)(\s+|$)', comment)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003039 error(filename, linenum, 'whitespace/comments', 4,
3040 'Should have a space between // and comment')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003041
avakulenko@google.com554223d2014-12-04 22:00:20 +00003042
erg@google.comd350fe52013-01-14 17:51:48 +00003043def CheckAccess(filename, clean_lines, linenum, nesting_state, error):
3044 """Checks for improper use of DISALLOW* macros.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003045
erg@google.comd350fe52013-01-14 17:51:48 +00003046 Args:
3047 filename: The name of the current file.
3048 clean_lines: A CleansedLines instance containing the file.
3049 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003050 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003051 the current stack of nested blocks being parsed.
3052 error: The function to call with any errors found.
3053 """
3054 line = clean_lines.elided[linenum] # get rid of comments and strings
3055
3056 matched = Match((r'\s*(DISALLOW_COPY_AND_ASSIGN|'
erg@google.comd350fe52013-01-14 17:51:48 +00003057 r'DISALLOW_IMPLICIT_CONSTRUCTORS)'), line)
3058 if not matched:
3059 return
3060 if nesting_state.stack and isinstance(nesting_state.stack[-1], _ClassInfo):
3061 if nesting_state.stack[-1].access != 'private':
3062 error(filename, linenum, 'readability/constructors', 3,
3063 '%s must be in the private: section' % matched.group(1))
3064
3065 else:
3066 # Found DISALLOW* macro outside a class declaration, or perhaps it
3067 # was used inside a function when it should have been part of the
3068 # class declaration. We could issue a warning here, but it
3069 # probably resulted in a compiler error already.
3070 pass
3071
3072
erg@google.comd350fe52013-01-14 17:51:48 +00003073def CheckSpacing(filename, clean_lines, linenum, nesting_state, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003074 """Checks for the correctness of various spacing issues in the code.
3075
3076 Things we check for: spaces around operators, spaces after
3077 if/for/while/switch, no spaces around parens in function calls, two
3078 spaces between code and comment, don't start a block with a blank
erg@google.com8a95ecc2011-09-08 00:45:54 +00003079 line, don't end a function with a blank line, don't add a blank line
3080 after public/protected/private, don't have too many blank lines in a row.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003081
3082 Args:
3083 filename: The name of the current file.
3084 clean_lines: A CleansedLines instance containing the file.
3085 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003086 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00003087 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003088 error: The function to call with any errors found.
3089 """
3090
erg@google.com2aa59982013-10-28 19:09:25 +00003091 # Don't use "elided" lines here, otherwise we can't check commented lines.
3092 # Don't want to use "raw" either, because we don't want to check inside C++11
3093 # raw strings,
3094 raw = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00003095 line = raw[linenum]
3096
3097 # Before nixing comments, check if the line is blank for no good
3098 # reason. This includes the first line after a block is opened, and
3099 # blank lines at the end of a function (ie, right before a line like '}'
erg@google.comd350fe52013-01-14 17:51:48 +00003100 #
3101 # Skip all the blank line checks if we are immediately inside a
3102 # namespace body. In other words, don't issue blank line warnings
3103 # for this block:
3104 # namespace {
3105 #
3106 # }
3107 #
3108 # A warning about missing end of namespace comments will be issued instead.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003109 #
3110 # Also skip blank line checks for 'extern "C"' blocks, which are formatted
3111 # like namespaces.
3112 if (IsBlankLine(line) and
3113 not nesting_state.InNamespaceBody() and
3114 not nesting_state.InExternC()):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003115 elided = clean_lines.elided
3116 prev_line = elided[linenum - 1]
3117 prevbrace = prev_line.rfind('{')
3118 # TODO(unknown): Don't complain if line before blank line, and line after,
3119 # both start with alnums and are indented the same amount.
3120 # This ignores whitespace at the start of a namespace block
3121 # because those are not usually indented.
erg@google.comd350fe52013-01-14 17:51:48 +00003122 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003123 # OK, we have a blank line at the start of a code block. Before we
3124 # complain, we check if it is an exception to the rule: The previous
erg@google.com8a95ecc2011-09-08 00:45:54 +00003125 # non-empty line has the parameters of a function header that are indented
erg@google.com4e00b9a2009-01-12 23:05:11 +00003126 # 4 spaces (because they did not fit in a 80 column line when placed on
3127 # the same line as the function name). We also check for the case where
3128 # the previous line is indented 6 spaces, which may happen when the
3129 # initializers of a constructor do not fit into a 80 column line.
3130 exception = False
3131 if Match(r' {6}\w', prev_line): # Initializer list?
3132 # We are looking for the opening column of initializer list, which
3133 # should be indented 4 spaces to cause 6 space indentation afterwards.
3134 search_position = linenum-2
3135 while (search_position >= 0
3136 and Match(r' {6}\w', elided[search_position])):
3137 search_position -= 1
3138 exception = (search_position >= 0
3139 and elided[search_position][:5] == ' :')
3140 else:
3141 # Search for the function arguments or an initializer list. We use a
3142 # simple heuristic here: If the line is indented 4 spaces; and we have a
3143 # closing paren, without the opening paren, followed by an opening brace
3144 # or colon (for initializer lists) we assume that it is the last line of
3145 # a function header. If we have a colon indented 4 spaces, it is an
3146 # initializer list.
3147 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
3148 prev_line)
3149 or Match(r' {4}:', prev_line))
3150
3151 if not exception:
3152 error(filename, linenum, 'whitespace/blank_line', 2,
erg@google.com2aa59982013-10-28 19:09:25 +00003153 'Redundant blank line at the start of a code block '
3154 'should be deleted.')
erg@google.comd350fe52013-01-14 17:51:48 +00003155 # Ignore blank lines at the end of a block in a long if-else
erg@google.com4e00b9a2009-01-12 23:05:11 +00003156 # chain, like this:
3157 # if (condition1) {
3158 # // Something followed by a blank line
3159 #
3160 # } else if (condition2) {
3161 # // Something else
3162 # }
3163 if linenum + 1 < clean_lines.NumLines():
3164 next_line = raw[linenum + 1]
3165 if (next_line
3166 and Match(r'\s*}', next_line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003167 and next_line.find('} else ') == -1):
3168 error(filename, linenum, 'whitespace/blank_line', 3,
erg@google.com2aa59982013-10-28 19:09:25 +00003169 'Redundant blank line at the end of a code block '
3170 'should be deleted.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003171
erg@google.com8a95ecc2011-09-08 00:45:54 +00003172 matched = Match(r'\s*(public|protected|private):', prev_line)
3173 if matched:
3174 error(filename, linenum, 'whitespace/blank_line', 3,
3175 'Do not leave a blank line after "%s:"' % matched.group(1))
3176
avakulenko@google.com02af6282014-06-04 18:53:25 +00003177 # Next, check comments
3178 next_line_start = 0
3179 if linenum + 1 < clean_lines.NumLines():
3180 next_line = raw[linenum + 1]
3181 next_line_start = len(next_line) - len(next_line.lstrip())
3182 CheckComment(line, filename, linenum, next_line_start, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003183
avakulenko@google.com02af6282014-06-04 18:53:25 +00003184 # get rid of comments and strings
3185 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003186
avakulenko@google.com02af6282014-06-04 18:53:25 +00003187 # You shouldn't have spaces before your brackets, except maybe after
3188 # 'delete []' or 'return []() {};'
3189 if Search(r'\w\s+\[', line) and not Search(r'(?:delete|return)\s+\[', line):
3190 error(filename, linenum, 'whitespace/braces', 5,
3191 'Extra space before [')
3192
3193 # In range-based for, we wanted spaces before and after the colon, but
3194 # not around "::" tokens that might appear.
3195 if (Search(r'for *\(.*[^:]:[^: ]', line) or
3196 Search(r'for *\(.*[^: ]:[^:]', line)):
3197 error(filename, linenum, 'whitespace/forcolon', 2,
3198 'Missing space around colon in range-based for loop')
3199
3200
3201def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3202 """Checks for horizontal spacing around operators.
3203
3204 Args:
3205 filename: The name of the current file.
3206 clean_lines: A CleansedLines instance containing the file.
3207 linenum: The number of the line to check.
3208 error: The function to call with any errors found.
3209 """
3210 line = clean_lines.elided[linenum]
3211
3212 # Don't try to do spacing checks for operator methods. Do this by
3213 # replacing the troublesome characters with something else,
3214 # preserving column position for all other characters.
3215 #
3216 # The replacement is done repeatedly to avoid false positives from
3217 # operators that call operators.
3218 while True:
3219 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3220 if match:
3221 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3222 else:
3223 break
erg@google.com4e00b9a2009-01-12 23:05:11 +00003224
3225 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3226 # Otherwise not. Note we only check for non-spaces on *both* sides;
3227 # sometimes people put non-spaces on one side when aligning ='s among
3228 # many lines (not that this is behavior that I approve of...)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003229 if ((Search(r'[\w.]=', line) or
3230 Search(r'=[\w.]', line))
3231 and not Search(r'\b(if|while|for) ', line)
3232 # Operators taken from [lex.operators] in C++11 standard.
3233 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3234 and not Search(r'operator=', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003235 error(filename, linenum, 'whitespace/operators', 4,
3236 'Missing spaces around =')
3237
3238 # It's ok not to have spaces around binary operators like + - * /, but if
3239 # there's too little whitespace, we get concerned. It's hard to tell,
3240 # though, so we punt on this one for now. TODO.
3241
3242 # You should always have whitespace around binary operators.
erg@google.comd350fe52013-01-14 17:51:48 +00003243 #
3244 # Check <= and >= first to avoid false positives with < and >, then
3245 # check non-include lines for spacing around < and >.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003246 #
3247 # If the operator is followed by a comma, assume it's be used in a
3248 # macro context and don't do any checks. This avoids false
3249 # positives.
3250 #
Alex Vakulenko01e47232016-05-06 13:54:15 -07003251 # Note that && is not included here. This is because there are too
3252 # many false positives due to RValue references.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003253 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003254 if match:
3255 error(filename, linenum, 'whitespace/operators', 3,
3256 'Missing spaces around %s' % match.group(1))
erg@google.comd350fe52013-01-14 17:51:48 +00003257 elif not Match(r'#.*include', line):
erg@google.comd350fe52013-01-14 17:51:48 +00003258 # Look for < that is not surrounded by spaces. This is only
3259 # triggered if both sides are missing spaces, even though
3260 # technically should should flag if at least one side is missing a
3261 # space. This is done to avoid some false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003262 match = Match(r'^(.*[^\s<])<[^\s=<,]', line)
3263 if match:
3264 (_, _, end_pos) = CloseExpression(
3265 clean_lines, linenum, len(match.group(1)))
3266 if end_pos <= -1:
3267 error(filename, linenum, 'whitespace/operators', 3,
3268 'Missing spaces around <')
erg@google.comd350fe52013-01-14 17:51:48 +00003269
3270 # Look for > that is not surrounded by spaces. Similar to the
3271 # above, we only trigger if both sides are missing spaces to avoid
3272 # false positives with shifts.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003273 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line)
3274 if match:
3275 (_, _, start_pos) = ReverseCloseExpression(
3276 clean_lines, linenum, len(match.group(1)))
3277 if start_pos <= -1:
3278 error(filename, linenum, 'whitespace/operators', 3,
3279 'Missing spaces around >')
3280
3281 # We allow no-spaces around << when used like this: 10<<20, but
3282 # not otherwise (particularly, not when used as streams)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003283 #
avakulenko@google.com02af6282014-06-04 18:53:25 +00003284 # We also allow operators following an opening parenthesis, since
3285 # those tend to be macros that deal with operators.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003286 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00003287 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00003288 not (match.group(1) == 'operator' and match.group(2) == ';')):
3289 error(filename, linenum, 'whitespace/operators', 3,
3290 'Missing spaces around <<')
erg@google.comd350fe52013-01-14 17:51:48 +00003291
3292 # We allow no-spaces around >> for almost anything. This is because
3293 # C++11 allows ">>" to close nested templates, which accounts for
3294 # most cases when ">>" is not followed by a space.
3295 #
3296 # We still warn on ">>" followed by alpha character, because that is
3297 # likely due to ">>" being used for right shifts, e.g.:
3298 # value >> alpha
3299 #
3300 # When ">>" is used to close templates, the alphanumeric letter that
3301 # follows would be part of an identifier, and there should still be
3302 # a space separating the template type and the identifier.
3303 # type<type<type>> alpha
3304 match = Search(r'>>[a-zA-Z_]', line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00003305 if match:
3306 error(filename, linenum, 'whitespace/operators', 3,
erg@google.comd350fe52013-01-14 17:51:48 +00003307 'Missing spaces around >>')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003308
3309 # There shouldn't be space around unary operators
3310 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
3311 if match:
3312 error(filename, linenum, 'whitespace/operators', 4,
3313 'Extra space for operator %s' % match.group(1))
3314
avakulenko@google.com02af6282014-06-04 18:53:25 +00003315
3316def CheckParenthesisSpacing(filename, clean_lines, linenum, error):
3317 """Checks for horizontal spacing around parentheses.
3318
3319 Args:
3320 filename: The name of the current file.
3321 clean_lines: A CleansedLines instance containing the file.
3322 linenum: The number of the line to check.
3323 error: The function to call with any errors found.
3324 """
3325 line = clean_lines.elided[linenum]
3326
3327 # No spaces after an if, while, switch, or for
erg@google.com4e00b9a2009-01-12 23:05:11 +00003328 match = Search(r' (if\(|for\(|while\(|switch\()', line)
3329 if match:
3330 error(filename, linenum, 'whitespace/parens', 5,
3331 'Missing space before ( in %s' % match.group(1))
3332
3333 # For if/for/while/switch, the left and right parens should be
3334 # consistent about how many spaces are inside the parens, and
3335 # there should either be zero or one spaces inside the parens.
3336 # We don't want: "if ( foo)" or "if ( foo )".
erg@google.come35f7652009-06-19 20:52:09 +00003337 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003338 match = Search(r'\b(if|for|while|switch)\s*'
3339 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
3340 line)
3341 if match:
3342 if len(match.group(2)) != len(match.group(4)):
3343 if not (match.group(3) == ';' and
erg@google.come35f7652009-06-19 20:52:09 +00003344 len(match.group(2)) == 1 + len(match.group(4)) or
3345 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003346 error(filename, linenum, 'whitespace/parens', 5,
3347 'Mismatching spaces inside () in %s' % match.group(1))
erg@google.comc6671232013-10-25 21:44:03 +00003348 if len(match.group(2)) not in [0, 1]:
erg@google.com4e00b9a2009-01-12 23:05:11 +00003349 error(filename, linenum, 'whitespace/parens', 5,
3350 'Should have zero or one spaces inside ( and ) in %s' %
3351 match.group(1))
3352
avakulenko@google.com02af6282014-06-04 18:53:25 +00003353
3354def CheckCommaSpacing(filename, clean_lines, linenum, error):
3355 """Checks for horizontal spacing near commas and semicolons.
3356
3357 Args:
3358 filename: The name of the current file.
3359 clean_lines: A CleansedLines instance containing the file.
3360 linenum: The number of the line to check.
3361 error: The function to call with any errors found.
3362 """
3363 raw = clean_lines.lines_without_raw_strings
3364 line = clean_lines.elided[linenum]
3365
erg@google.com4e00b9a2009-01-12 23:05:11 +00003366 # You should always have a space after a comma (either as fn arg or operator)
erg@google.comc6671232013-10-25 21:44:03 +00003367 #
3368 # This does not apply when the non-space character following the
3369 # comma is another comma, since the only time when that happens is
3370 # for empty macro arguments.
erg@google.com2aa59982013-10-28 19:09:25 +00003371 #
3372 # We run this check in two passes: first pass on elided lines to
3373 # verify that lines contain missing whitespaces, second pass on raw
3374 # lines to confirm that those missing whitespaces are not due to
3375 # elided comments.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003376 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and
3377 Search(r',[^,\s]', raw[linenum])):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003378 error(filename, linenum, 'whitespace/comma', 3,
3379 'Missing space after ,')
3380
erg@google.comd7d27472011-09-07 17:36:35 +00003381 # You should always have a space after a semicolon
3382 # except for few corner cases
3383 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
3384 # space after ;
3385 if Search(r';[^\s};\\)/]', line):
3386 error(filename, linenum, 'whitespace/semicolon', 3,
3387 'Missing space after ;')
3388
avakulenko@google.com02af6282014-06-04 18:53:25 +00003389
Alex Vakulenko01e47232016-05-06 13:54:15 -07003390def _IsType(clean_lines, nesting_state, expr):
3391 """Check if expression looks like a type name, returns true if so.
3392
3393 Args:
3394 clean_lines: A CleansedLines instance containing the file.
3395 nesting_state: A NestingState instance which maintains information about
3396 the current stack of nested blocks being parsed.
3397 expr: The expression to check.
3398 Returns:
3399 True, if token looks like a type.
3400 """
3401 # Keep only the last token in the expression
3402 last_word = Match(r'^.*(\b\S+)$', expr)
3403 if last_word:
3404 token = last_word.group(1)
3405 else:
3406 token = expr
3407
3408 # Match native types and stdint types
3409 if _TYPES.match(token):
3410 return True
3411
3412 # Try a bit harder to match templated types. Walk up the nesting
3413 # stack until we find something that resembles a typename
3414 # declaration for what we are looking for.
3415 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) +
3416 r'\b')
3417 block_index = len(nesting_state.stack) - 1
3418 while block_index >= 0:
3419 if isinstance(nesting_state.stack[block_index], _NamespaceInfo):
3420 return False
3421
3422 # Found where the opening brace is. We want to scan from this
3423 # line up to the beginning of the function, minus a few lines.
3424 # template <typename Type1, // stop scanning here
3425 # ...>
3426 # class C
3427 # : public ... { // start scanning here
3428 last_line = nesting_state.stack[block_index].starting_linenum
3429
3430 next_block_start = 0
3431 if block_index > 0:
3432 next_block_start = nesting_state.stack[block_index - 1].starting_linenum
3433 first_line = last_line
3434 while first_line >= next_block_start:
3435 if clean_lines.elided[first_line].find('template') >= 0:
3436 break
3437 first_line -= 1
3438 if first_line < next_block_start:
3439 # Didn't find any "template" keyword before reaching the next block,
3440 # there are probably no template things to check for this block
3441 block_index -= 1
3442 continue
3443
3444 # Look for typename in the specified range
3445 for i in xrange(first_line, last_line + 1, 1):
3446 if Search(typename_pattern, clean_lines.elided[i]):
3447 return True
3448 block_index -= 1
3449
3450 return False
3451
3452
3453def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00003454 """Checks for horizontal spacing near commas.
3455
3456 Args:
3457 filename: The name of the current file.
3458 clean_lines: A CleansedLines instance containing the file.
3459 linenum: The number of the line to check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003460 nesting_state: A NestingState instance which maintains information about
3461 the current stack of nested blocks being parsed.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003462 error: The function to call with any errors found.
3463 """
3464 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00003465
erg@google.com8a95ecc2011-09-08 00:45:54 +00003466 # Except after an opening paren, or after another opening brace (in case of
3467 # an initializer list, for instance), you should have spaces before your
Alex Vakulenko01e47232016-05-06 13:54:15 -07003468 # braces when they are delimiting blocks, classes, namespaces etc.
3469 # And since you should never have braces at the beginning of a line,
3470 # this is an easy test. Except that braces used for initialization don't
3471 # follow the same rule; we often don't want spaces before those.
avakulenko@google.com554223d2014-12-04 22:00:20 +00003472 match = Match(r'^(.*[^ ({>]){', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07003473
erg@google.com2aa59982013-10-28 19:09:25 +00003474 if match:
3475 # Try a bit harder to check for brace initialization. This
3476 # happens in one of the following forms:
3477 # Constructor() : initializer_list_{} { ... }
3478 # Constructor{}.MemberFunction()
3479 # Type variable{};
3480 # FunctionCall(type{}, ...);
3481 # LastArgument(..., type{});
3482 # LOG(INFO) << type{} << " ...";
3483 # map_of_type[{...}] = ...;
avakulenko@google.com02af6282014-06-04 18:53:25 +00003484 # ternary = expr ? new type{} : nullptr;
3485 # OuterTemplate<InnerTemplateConstructor<Type>{}>
erg@google.com2aa59982013-10-28 19:09:25 +00003486 #
3487 # We check for the character following the closing brace, and
3488 # silence the warning if it's one of those listed above, i.e.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003489 # "{.;,)<>]:".
erg@google.com2aa59982013-10-28 19:09:25 +00003490 #
3491 # To account for nested initializer list, we allow any number of
3492 # closing braces up to "{;,)<". We can't simply silence the
3493 # warning on first sight of closing brace, because that would
3494 # cause false negatives for things that are not initializer lists.
3495 # Silence this: But not this:
3496 # Outer{ if (...) {
3497 # Inner{...} if (...){ // Missing space before {
3498 # }; }
3499 #
3500 # There is a false negative with this approach if people inserted
3501 # spurious semicolons, e.g. "if (cond){};", but we will catch the
3502 # spurious semicolon with a separate check.
Alex Vakulenko01e47232016-05-06 13:54:15 -07003503 leading_text = match.group(1)
erg@google.com2aa59982013-10-28 19:09:25 +00003504 (endline, endlinenum, endpos) = CloseExpression(
3505 clean_lines, linenum, len(match.group(1)))
3506 trailing_text = ''
3507 if endpos > -1:
3508 trailing_text = endline[endpos:]
3509 for offset in xrange(endlinenum + 1,
3510 min(endlinenum + 3, clean_lines.NumLines() - 1)):
3511 trailing_text += clean_lines.elided[offset]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003512 # We also suppress warnings for `uint64_t{expression}` etc., as the style
3513 # guide recommends brace initialization for integral types to avoid
3514 # overflow/truncation.
3515 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text)
3516 and not _IsType(clean_lines, nesting_state, leading_text)):
erg@google.com2aa59982013-10-28 19:09:25 +00003517 error(filename, linenum, 'whitespace/braces', 5,
3518 'Missing space before {')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003519
3520 # Make sure '} else {' has spaces.
3521 if Search(r'}else', line):
3522 error(filename, linenum, 'whitespace/braces', 5,
3523 'Missing space before else')
3524
erg@google.com4e00b9a2009-01-12 23:05:11 +00003525 # You shouldn't have a space before a semicolon at the end of the line.
3526 # There's a special case for "for" since the style guide allows space before
3527 # the semicolon there.
3528 if Search(r':\s*;\s*$', line):
3529 error(filename, linenum, 'whitespace/semicolon', 5,
erg@google.comd350fe52013-01-14 17:51:48 +00003530 'Semicolon defining empty statement. Use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003531 elif Search(r'^\s*;\s*$', line):
3532 error(filename, linenum, 'whitespace/semicolon', 5,
3533 'Line contains only semicolon. If this should be an empty statement, '
erg@google.comd350fe52013-01-14 17:51:48 +00003534 'use {} instead.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003535 elif (Search(r'\s+;\s*$', line) and
3536 not Search(r'\bfor\b', line)):
3537 error(filename, linenum, 'whitespace/semicolon', 5,
3538 'Extra space before last semicolon. If this should be an empty '
erg@google.comd350fe52013-01-14 17:51:48 +00003539 'statement, use {} instead.')
3540
avakulenko@google.com02af6282014-06-04 18:53:25 +00003541
3542def IsDecltype(clean_lines, linenum, column):
3543 """Check if the token ending on (linenum, column) is decltype().
3544
3545 Args:
3546 clean_lines: A CleansedLines instance containing the file.
3547 linenum: the number of the line to check.
3548 column: end column of the token to check.
3549 Returns:
3550 True if this token is decltype() expression, False otherwise.
3551 """
3552 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column)
3553 if start_col < 0:
3554 return False
3555 if Search(r'\bdecltype\s*$', text[0:start_col]):
3556 return True
3557 return False
3558
3559
erg@google.com8a95ecc2011-09-08 00:45:54 +00003560def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
3561 """Checks for additional blank line issues related to sections.
3562
3563 Currently the only thing checked here is blank line before protected/private.
3564
3565 Args:
3566 filename: The name of the current file.
3567 clean_lines: A CleansedLines instance containing the file.
3568 class_info: A _ClassInfo objects.
3569 linenum: The number of the line to check.
3570 error: The function to call with any errors found.
3571 """
3572 # Skip checks if the class is small, where small means 25 lines or less.
3573 # 25 lines seems like a good cutoff since that's the usual height of
3574 # terminals, and any class that can't fit in one screen can't really
3575 # be considered "small".
3576 #
3577 # Also skip checks if we are on the first line. This accounts for
3578 # classes that look like
3579 # class Foo { public: ... };
3580 #
3581 # If we didn't find the end of the class, last_line would be zero,
3582 # and the check will be skipped by the first condition.
erg@google.comd350fe52013-01-14 17:51:48 +00003583 if (class_info.last_line - class_info.starting_linenum <= 24 or
3584 linenum <= class_info.starting_linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003585 return
3586
3587 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
3588 if matched:
3589 # Issue warning if the line before public/protected/private was
3590 # not a blank line, but don't do this if the previous line contains
3591 # "class" or "struct". This can happen two ways:
3592 # - We are at the beginning of the class.
3593 # - We are forward-declaring an inner class that is semantically
3594 # private, but needed to be public for implementation reasons.
erg@google.comd350fe52013-01-14 17:51:48 +00003595 # Also ignores cases where the previous line ends with a backslash as can be
3596 # common when defining classes in C macros.
erg@google.com8a95ecc2011-09-08 00:45:54 +00003597 prev_line = clean_lines.lines[linenum - 1]
3598 if (not IsBlankLine(prev_line) and
erg@google.comd350fe52013-01-14 17:51:48 +00003599 not Search(r'\b(class|struct)\b', prev_line) and
3600 not Search(r'\\$', prev_line)):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003601 # Try a bit harder to find the beginning of the class. This is to
3602 # account for multi-line base-specifier lists, e.g.:
3603 # class Derived
3604 # : public Base {
erg@google.comd350fe52013-01-14 17:51:48 +00003605 end_class_head = class_info.starting_linenum
3606 for i in range(class_info.starting_linenum, linenum):
erg@google.com8a95ecc2011-09-08 00:45:54 +00003607 if Search(r'\{\s*$', clean_lines.lines[i]):
3608 end_class_head = i
3609 break
3610 if end_class_head < linenum - 1:
3611 error(filename, linenum, 'whitespace/blank_line', 3,
3612 '"%s:" should be preceded by a blank line' % matched.group(1))
3613
3614
erg@google.com4e00b9a2009-01-12 23:05:11 +00003615def GetPreviousNonBlankLine(clean_lines, linenum):
3616 """Return the most recent non-blank line and its line number.
3617
3618 Args:
3619 clean_lines: A CleansedLines instance containing the file contents.
3620 linenum: The number of the line to check.
3621
3622 Returns:
3623 A tuple with two elements. The first element is the contents of the last
3624 non-blank line before the current line, or the empty string if this is the
3625 first non-blank line. The second is the line number of that line, or -1
3626 if this is the first non-blank line.
3627 """
3628
3629 prevlinenum = linenum - 1
3630 while prevlinenum >= 0:
3631 prevline = clean_lines.elided[prevlinenum]
3632 if not IsBlankLine(prevline): # if not a blank line...
3633 return (prevline, prevlinenum)
3634 prevlinenum -= 1
3635 return ('', -1)
3636
3637
3638def CheckBraces(filename, clean_lines, linenum, error):
3639 """Looks for misplaced braces (e.g. at the end of line).
3640
3641 Args:
3642 filename: The name of the current file.
3643 clean_lines: A CleansedLines instance containing the file.
3644 linenum: The number of the line to check.
3645 error: The function to call with any errors found.
3646 """
3647
3648 line = clean_lines.elided[linenum] # get rid of comments and strings
3649
3650 if Match(r'\s*{\s*$', line):
erg@google.com2aa59982013-10-28 19:09:25 +00003651 # We allow an open brace to start a line in the case where someone is using
3652 # braces in a block to explicitly create a new scope, which is commonly used
3653 # to control the lifetime of stack-allocated variables. Braces are also
3654 # used for brace initializers inside function calls. We don't detect this
3655 # perfectly: we just don't complain if the last non-whitespace character on
3656 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
Alex Vakulenko01e47232016-05-06 13:54:15 -07003657 # previous line starts a preprocessor block. We also allow a brace on the
3658 # following line if it is part of an array initialization and would not fit
3659 # within the 80 character limit of the preceding line.
erg@google.com4e00b9a2009-01-12 23:05:11 +00003660 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
erg@google.com2aa59982013-10-28 19:09:25 +00003661 if (not Search(r'[,;:}{(]\s*$', prevline) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07003662 not Match(r'\s*#', prevline) and
3663 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003664 error(filename, linenum, 'whitespace/braces', 4,
3665 '{ should almost always be at the end of the previous line')
3666
3667 # An else clause should be on the same line as the preceding closing brace.
avakulenko@google.com02af6282014-06-04 18:53:25 +00003668 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line):
erg@google.com4e00b9a2009-01-12 23:05:11 +00003669 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3670 if Match(r'\s*}\s*$', prevline):
3671 error(filename, linenum, 'whitespace/newline', 4,
3672 'An else should appear on the same line as the preceding }')
3673
3674 # If braces come on one side of an else, they should be on both.
3675 # However, we have to worry about "else if" that spans multiple lines!
avakulenko@google.com02af6282014-06-04 18:53:25 +00003676 if Search(r'else if\s*\(', line): # could be multi-line if
3677 brace_on_left = bool(Search(r'}\s*else if\s*\(', line))
3678 # find the ( after the if
3679 pos = line.find('else if')
3680 pos = line.find('(', pos)
3681 if pos > 0:
3682 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
3683 brace_on_right = endline[endpos:].find('{') != -1
3684 if brace_on_left != brace_on_right: # must be brace after if
3685 error(filename, linenum, 'readability/braces', 5,
3686 'If an else has a brace on one side, it should have it on both')
3687 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
3688 error(filename, linenum, 'readability/braces', 5,
3689 'If an else has a brace on one side, it should have it on both')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003690
3691 # Likewise, an else should never have the else clause on the same line
3692 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
3693 error(filename, linenum, 'whitespace/newline', 4,
3694 'Else clause should never be on same line as else (use 2 lines)')
3695
3696 # In the same way, a do/while should never be on one line
3697 if Match(r'\s*do [^\s{]', line):
3698 error(filename, linenum, 'whitespace/newline', 4,
3699 'do/while clauses should not be on a single line')
3700
avakulenko@google.com02af6282014-06-04 18:53:25 +00003701 # Check single-line if/else bodies. The style guide says 'curly braces are not
3702 # required for single-line statements'. We additionally allow multi-line,
3703 # single statements, but we reject anything with more than one semicolon in
3704 # it. This means that the first semicolon after the if should be at the end of
3705 # its line, and the line after that should have an indent level equal to or
3706 # lower than the if. We also check for ambiguous if/else nesting without
3707 # braces.
3708 if_else_match = Search(r'\b(if\s*\(|else\b)', line)
3709 if if_else_match and not Match(r'\s*#', line):
3710 if_indent = GetIndentLevel(line)
3711 endline, endlinenum, endpos = line, linenum, if_else_match.end()
3712 if_match = Search(r'\bif\s*\(', line)
3713 if if_match:
3714 # This could be a multiline if condition, so find the end first.
3715 pos = if_match.end() - 1
3716 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos)
3717 # Check for an opening brace, either directly after the if or on the next
3718 # line. If found, this isn't a single-statement conditional.
3719 if (not Match(r'\s*{', endline[endpos:])
3720 and not (Match(r'\s*$', endline[endpos:])
3721 and endlinenum < (len(clean_lines.elided) - 1)
3722 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))):
3723 while (endlinenum < len(clean_lines.elided)
3724 and ';' not in clean_lines.elided[endlinenum][endpos:]):
3725 endlinenum += 1
3726 endpos = 0
3727 if endlinenum < len(clean_lines.elided):
3728 endline = clean_lines.elided[endlinenum]
3729 # We allow a mix of whitespace and closing braces (e.g. for one-liner
3730 # methods) and a single \ after the semicolon (for macros)
3731 endpos = endline.find(';')
3732 if not Match(r';[\s}]*(\\?)$', endline[endpos:]):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00003733 # Semicolon isn't the last character, there's something trailing.
3734 # Output a warning if the semicolon is not contained inside
3735 # a lambda expression.
3736 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$',
3737 endline):
3738 error(filename, linenum, 'readability/braces', 4,
3739 'If/else bodies with multiple statements require braces')
avakulenko@google.com02af6282014-06-04 18:53:25 +00003740 elif endlinenum < len(clean_lines.elided) - 1:
3741 # Make sure the next line is dedented
3742 next_line = clean_lines.elided[endlinenum + 1]
3743 next_indent = GetIndentLevel(next_line)
3744 # With ambiguous nested if statements, this will error out on the
3745 # if that *doesn't* match the else, regardless of whether it's the
3746 # inner one or outer one.
3747 if (if_match and Match(r'\s*else\b', next_line)
3748 and next_indent != if_indent):
3749 error(filename, linenum, 'readability/braces', 4,
3750 'Else clause should be indented at the same level as if. '
3751 'Ambiguous nested if/else chains require braces.')
3752 elif next_indent > if_indent:
3753 error(filename, linenum, 'readability/braces', 4,
3754 'If/else bodies with multiple statements require braces')
3755
3756
3757def CheckTrailingSemicolon(filename, clean_lines, linenum, error):
3758 """Looks for redundant trailing semicolon.
3759
3760 Args:
3761 filename: The name of the current file.
3762 clean_lines: A CleansedLines instance containing the file.
3763 linenum: The number of the line to check.
3764 error: The function to call with any errors found.
3765 """
3766
3767 line = clean_lines.elided[linenum]
3768
erg@google.com2aa59982013-10-28 19:09:25 +00003769 # Block bodies should not be followed by a semicolon. Due to C++11
3770 # brace initialization, there are more places where semicolons are
3771 # required than not, so we use a whitelist approach to check these
3772 # rather than a blacklist. These are the places where "};" should
3773 # be replaced by just "}":
3774 # 1. Some flavor of block following closing parenthesis:
3775 # for (;;) {};
3776 # while (...) {};
3777 # switch (...) {};
3778 # Function(...) {};
3779 # if (...) {};
3780 # if (...) else if (...) {};
3781 #
3782 # 2. else block:
3783 # if (...) else {};
3784 #
3785 # 3. const member function:
3786 # Function(...) const {};
3787 #
3788 # 4. Block following some statement:
3789 # x = 42;
3790 # {};
3791 #
3792 # 5. Block at the beginning of a function:
3793 # Function(...) {
3794 # {};
3795 # }
3796 #
3797 # Note that naively checking for the preceding "{" will also match
3798 # braces inside multi-dimensional arrays, but this is fine since
3799 # that expression will not contain semicolons.
3800 #
3801 # 6. Block following another block:
3802 # while (true) {}
3803 # {};
3804 #
3805 # 7. End of namespaces:
3806 # namespace {};
3807 #
3808 # These semicolons seems far more common than other kinds of
3809 # redundant semicolons, possibly due to people converting classes
3810 # to namespaces. For now we do not warn for this case.
3811 #
3812 # Try matching case 1 first.
3813 match = Match(r'^(.*\)\s*)\{', line)
3814 if match:
3815 # Matched closing parenthesis (case 1). Check the token before the
3816 # matching opening parenthesis, and don't warn if it looks like a
3817 # macro. This avoids these false positives:
3818 # - macro that defines a base class
3819 # - multi-line macro that defines a base class
3820 # - macro that defines the whole class-head
3821 #
3822 # But we still issue warnings for macros that we know are safe to
3823 # warn, specifically:
3824 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P
3825 # - TYPED_TEST
3826 # - INTERFACE_DEF
3827 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED:
3828 #
3829 # We implement a whitelist of safe macros instead of a blacklist of
3830 # unsafe macros, even though the latter appears less frequently in
3831 # google code and would have been easier to implement. This is because
3832 # the downside for getting the whitelist wrong means some extra
3833 # semicolons, while the downside for getting the blacklist wrong
3834 # would result in compile errors.
3835 #
avakulenko@google.com554223d2014-12-04 22:00:20 +00003836 # In addition to macros, we also don't want to warn on
3837 # - Compound literals
3838 # - Lambdas
Alex Vakulenko01e47232016-05-06 13:54:15 -07003839 # - alignas specifier with anonymous structs
3840 # - decltype
erg@google.com2aa59982013-10-28 19:09:25 +00003841 closing_brace_pos = match.group(1).rfind(')')
3842 opening_parenthesis = ReverseCloseExpression(
3843 clean_lines, linenum, closing_brace_pos)
3844 if opening_parenthesis[2] > -1:
3845 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]]
Alex Vakulenko01e47232016-05-06 13:54:15 -07003846 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix)
avakulenko@google.com02af6282014-06-04 18:53:25 +00003847 func = Match(r'^(.*\])\s*$', line_prefix)
erg@google.com2aa59982013-10-28 19:09:25 +00003848 if ((macro and
3849 macro.group(1) not in (
3850 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST',
3851 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED',
3852 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or
avakulenko@google.com02af6282014-06-04 18:53:25 +00003853 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or
avakulenko@google.com554223d2014-12-04 22:00:20 +00003854 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or
Alex Vakulenko01e47232016-05-06 13:54:15 -07003855 Search(r'\bdecltype$', line_prefix) or
erg@google.com2aa59982013-10-28 19:09:25 +00003856 Search(r'\s+=\s*$', line_prefix)):
3857 match = None
avakulenko@google.com02af6282014-06-04 18:53:25 +00003858 if (match and
3859 opening_parenthesis[1] > 1 and
3860 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])):
3861 # Multi-line lambda-expression
3862 match = None
erg@google.com2aa59982013-10-28 19:09:25 +00003863
3864 else:
3865 # Try matching cases 2-3.
3866 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line)
3867 if not match:
3868 # Try matching cases 4-6. These are always matched on separate lines.
3869 #
3870 # Note that we can't simply concatenate the previous line to the
3871 # current line and do a single match, otherwise we may output
3872 # duplicate warnings for the blank line case:
3873 # if (cond) {
3874 # // blank line
3875 # }
3876 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
3877 if prevline and Search(r'[;{}]\s*$', prevline):
3878 match = Match(r'^(\s*)\{', line)
3879
3880 # Check matching closing brace
3881 if match:
3882 (endline, endlinenum, endpos) = CloseExpression(
3883 clean_lines, linenum, len(match.group(1)))
3884 if endpos > -1 and Match(r'^\s*;', endline[endpos:]):
3885 # Current {} pair is eligible for semicolon check, and we have found
3886 # the redundant semicolon, output warning here.
3887 #
3888 # Note: because we are scanning forward for opening braces, and
3889 # outputting warnings for the matching closing brace, if there are
3890 # nested blocks with trailing semicolons, we will get the error
3891 # messages in reversed order.
3892 error(filename, endlinenum, 'readability/braces', 4,
3893 "You don't need a ; after a }")
erg@google.com4e00b9a2009-01-12 23:05:11 +00003894
3895
erg@google.comc6671232013-10-25 21:44:03 +00003896def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3897 """Look for empty loop/conditional body with only a single semicolon.
erg@google.comd350fe52013-01-14 17:51:48 +00003898
3899 Args:
3900 filename: The name of the current file.
3901 clean_lines: A CleansedLines instance containing the file.
3902 linenum: The number of the line to check.
3903 error: The function to call with any errors found.
3904 """
3905
3906 # Search for loop keywords at the beginning of the line. Because only
3907 # whitespaces are allowed before the keywords, this will also ignore most
3908 # do-while-loops, since those lines should start with closing brace.
erg@google.comc6671232013-10-25 21:44:03 +00003909 #
3910 # We also check "if" blocks here, since an empty conditional block
3911 # is likely an error.
erg@google.comd350fe52013-01-14 17:51:48 +00003912 line = clean_lines.elided[linenum]
erg@google.comc6671232013-10-25 21:44:03 +00003913 matched = Match(r'\s*(for|while|if)\s*\(', line)
3914 if matched:
Alex Vakulenko01e47232016-05-06 13:54:15 -07003915 # Find the end of the conditional expression.
erg@google.comd350fe52013-01-14 17:51:48 +00003916 (end_line, end_linenum, end_pos) = CloseExpression(
3917 clean_lines, linenum, line.find('('))
3918
3919 # Output warning if what follows the condition expression is a semicolon.
3920 # No warning for all other cases, including whitespace or newline, since we
3921 # have a separate check for semicolons preceded by whitespace.
3922 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
erg@google.comc6671232013-10-25 21:44:03 +00003923 if matched.group(1) == 'if':
3924 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
3925 'Empty conditional bodies should use {}')
3926 else:
3927 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
3928 'Empty loop bodies should use {} or continue')
erg@google.com4e00b9a2009-01-12 23:05:11 +00003929
Alex Vakulenko01e47232016-05-06 13:54:15 -07003930 # Check for if statements that have completely empty bodies (no comments)
3931 # and no else clauses.
3932 if end_pos >= 0 and matched.group(1) == 'if':
3933 # Find the position of the opening { for the if statement.
3934 # Return without logging an error if it has no brackets.
3935 opening_linenum = end_linenum
3936 opening_line_fragment = end_line[end_pos:]
3937 # Loop until EOF or find anything that's not whitespace or opening {.
3938 while not Search(r'^\s*\{', opening_line_fragment):
3939 if Search(r'^(?!\s*$)', opening_line_fragment):
3940 # Conditional has no brackets.
3941 return
3942 opening_linenum += 1
3943 if opening_linenum == len(clean_lines.elided):
3944 # Couldn't find conditional's opening { or any code before EOF.
3945 return
3946 opening_line_fragment = clean_lines.elided[opening_linenum]
3947 # Set opening_line (opening_line_fragment may not be entire opening line).
3948 opening_line = clean_lines.elided[opening_linenum]
3949
3950 # Find the position of the closing }.
3951 opening_pos = opening_line_fragment.find('{')
3952 if opening_linenum == end_linenum:
3953 # We need to make opening_pos relative to the start of the entire line.
3954 opening_pos += end_pos
3955 (closing_line, closing_linenum, closing_pos) = CloseExpression(
3956 clean_lines, opening_linenum, opening_pos)
3957 if closing_pos < 0:
3958 return
3959
3960 # Now construct the body of the conditional. This consists of the portion
3961 # of the opening line after the {, all lines until the closing line,
3962 # and the portion of the closing line before the }.
3963 if (clean_lines.raw_lines[opening_linenum] !=
3964 CleanseComments(clean_lines.raw_lines[opening_linenum])):
3965 # Opening line ends with a comment, so conditional isn't empty.
3966 return
3967 if closing_linenum > opening_linenum:
3968 # Opening line after the {. Ignore comments here since we checked above.
3969 body = list(opening_line[opening_pos+1:])
3970 # All lines until closing line, excluding closing line, with comments.
3971 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum])
3972 # Closing line before the }. Won't (and can't) have comments.
3973 body.append(clean_lines.elided[closing_linenum][:closing_pos-1])
3974 body = '\n'.join(body)
3975 else:
3976 # If statement has brackets and fits on a single line.
3977 body = opening_line[opening_pos+1:closing_pos-1]
3978
3979 # Check if the body is empty
3980 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body):
3981 return
3982 # The body is empty. Now make sure there's not an else clause.
3983 current_linenum = closing_linenum
3984 current_line_fragment = closing_line[closing_pos:]
3985 # Loop until EOF or find anything that's not whitespace or else clause.
3986 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment):
3987 if Search(r'^(?=\s*else)', current_line_fragment):
3988 # Found an else clause, so don't log an error.
3989 return
3990 current_linenum += 1
3991 if current_linenum == len(clean_lines.elided):
3992 break
3993 current_line_fragment = clean_lines.elided[current_linenum]
3994
3995 # The body is empty and there's no else clause until EOF or other code.
3996 error(filename, end_linenum, 'whitespace/empty_if_body', 4,
3997 ('If statement had no body and no else clause'))
3998
erg@google.com4e00b9a2009-01-12 23:05:11 +00003999
avakulenko@google.com02af6282014-06-04 18:53:25 +00004000def FindCheckMacro(line):
4001 """Find a replaceable CHECK-like macro.
4002
4003 Args:
4004 line: line to search on.
4005 Returns:
4006 (macro name, start position), or (None, -1) if no replaceable
4007 macro is found.
4008 """
4009 for macro in _CHECK_MACROS:
4010 i = line.find(macro)
4011 if i >= 0:
4012 # Find opening parenthesis. Do a regular expression match here
4013 # to make sure that we are matching the expected CHECK macro, as
4014 # opposed to some other macro that happens to contain the CHECK
4015 # substring.
4016 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line)
4017 if not matched:
4018 continue
4019 return (macro, len(matched.group(1)))
4020 return (None, -1)
4021
4022
erg@google.com4e00b9a2009-01-12 23:05:11 +00004023def CheckCheck(filename, clean_lines, linenum, error):
4024 """Checks the use of CHECK and EXPECT macros.
4025
4026 Args:
4027 filename: The name of the current file.
4028 clean_lines: A CleansedLines instance containing the file.
4029 linenum: The number of the line to check.
4030 error: The function to call with any errors found.
4031 """
4032
4033 # Decide the set of replacement macros that should be suggested
erg@google.comc6671232013-10-25 21:44:03 +00004034 lines = clean_lines.elided
avakulenko@google.com02af6282014-06-04 18:53:25 +00004035 (check_macro, start_pos) = FindCheckMacro(lines[linenum])
4036 if not check_macro:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004037 return
4038
erg@google.comc6671232013-10-25 21:44:03 +00004039 # Find end of the boolean expression by matching parentheses
4040 (last_line, end_line, end_pos) = CloseExpression(
4041 clean_lines, linenum, start_pos)
4042 if end_pos < 0:
4043 return
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004044
4045 # If the check macro is followed by something other than a
4046 # semicolon, assume users will log their own custom error messages
4047 # and don't suggest any replacements.
4048 if not Match(r'\s*;', last_line[end_pos:]):
4049 return
4050
erg@google.comc6671232013-10-25 21:44:03 +00004051 if linenum == end_line:
4052 expression = lines[linenum][start_pos + 1:end_pos - 1]
4053 else:
4054 expression = lines[linenum][start_pos + 1:]
4055 for i in xrange(linenum + 1, end_line):
4056 expression += lines[i]
4057 expression += last_line[0:end_pos - 1]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004058
erg@google.comc6671232013-10-25 21:44:03 +00004059 # Parse expression so that we can take parentheses into account.
4060 # This avoids false positives for inputs like "CHECK((a < 4) == b)",
4061 # which is not replaceable by CHECK_LE.
4062 lhs = ''
4063 rhs = ''
4064 operator = None
4065 while expression:
4066 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||'
4067 r'==|!=|>=|>|<=|<|\()(.*)$', expression)
4068 if matched:
4069 token = matched.group(1)
4070 if token == '(':
4071 # Parenthesized operand
4072 expression = matched.group(2)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004073 (end, _) = FindEndOfExpressionInLine(expression, 0, ['('])
erg@google.comc6671232013-10-25 21:44:03 +00004074 if end < 0:
4075 return # Unmatched parenthesis
4076 lhs += '(' + expression[0:end]
4077 expression = expression[end:]
4078 elif token in ('&&', '||'):
4079 # Logical and/or operators. This means the expression
4080 # contains more than one term, for example:
4081 # CHECK(42 < a && a < b);
4082 #
4083 # These are not replaceable with CHECK_LE, so bail out early.
4084 return
4085 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'):
4086 # Non-relational operator
4087 lhs += token
4088 expression = matched.group(2)
4089 else:
4090 # Relational operator
4091 operator = token
4092 rhs = matched.group(2)
4093 break
4094 else:
4095 # Unparenthesized operand. Instead of appending to lhs one character
4096 # at a time, we do another regular expression match to consume several
4097 # characters at once if possible. Trivial benchmark shows that this
4098 # is more efficient when the operands are longer than a single
4099 # character, which is generally the case.
4100 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression)
4101 if not matched:
4102 matched = Match(r'^(\s*\S)(.*)$', expression)
4103 if not matched:
4104 break
4105 lhs += matched.group(1)
4106 expression = matched.group(2)
4107
4108 # Only apply checks if we got all parts of the boolean expression
4109 if not (lhs and operator and rhs):
4110 return
4111
4112 # Check that rhs do not contain logical operators. We already know
4113 # that lhs is fine since the loop above parses out && and ||.
4114 if rhs.find('&&') > -1 or rhs.find('||') > -1:
4115 return
4116
4117 # At least one of the operands must be a constant literal. This is
4118 # to avoid suggesting replacements for unprintable things like
4119 # CHECK(variable != iterator)
4120 #
4121 # The following pattern matches decimal, hex integers, strings, and
4122 # characters (in that order).
4123 lhs = lhs.strip()
4124 rhs = rhs.strip()
4125 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$'
4126 if Match(match_constant, lhs) or Match(match_constant, rhs):
4127 # Note: since we know both lhs and rhs, we can provide a more
4128 # descriptive error message like:
4129 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42)
4130 # Instead of:
4131 # Consider using CHECK_EQ instead of CHECK(a == b)
4132 #
4133 # We are still keeping the less descriptive message because if lhs
4134 # or rhs gets long, the error message might become unreadable.
4135 error(filename, linenum, 'readability/check', 2,
4136 'Consider using %s instead of %s(a %s b)' % (
4137 _CHECK_REPLACEMENT[check_macro][operator],
4138 check_macro, operator))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004139
4140
erg@google.comd350fe52013-01-14 17:51:48 +00004141def CheckAltTokens(filename, clean_lines, linenum, error):
4142 """Check alternative keywords being used in boolean expressions.
4143
4144 Args:
4145 filename: The name of the current file.
4146 clean_lines: A CleansedLines instance containing the file.
4147 linenum: The number of the line to check.
4148 error: The function to call with any errors found.
4149 """
4150 line = clean_lines.elided[linenum]
4151
4152 # Avoid preprocessor lines
4153 if Match(r'^\s*#', line):
4154 return
4155
4156 # Last ditch effort to avoid multi-line comments. This will not help
4157 # if the comment started before the current line or ended after the
4158 # current line, but it catches most of the false positives. At least,
4159 # it provides a way to workaround this warning for people who use
4160 # multi-line comments in preprocessor macros.
4161 #
4162 # TODO(unknown): remove this once cpplint has better support for
4163 # multi-line comments.
4164 if line.find('/*') >= 0 or line.find('*/') >= 0:
4165 return
4166
4167 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line):
4168 error(filename, linenum, 'readability/alt_tokens', 2,
4169 'Use operator %s instead of %s' % (
4170 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
4171
4172
erg@google.com4e00b9a2009-01-12 23:05:11 +00004173def GetLineWidth(line):
4174 """Determines the width of the line in column positions.
4175
4176 Args:
4177 line: A string, which may be a Unicode string.
4178
4179 Returns:
4180 The width of the line in column positions, accounting for Unicode
4181 combining characters and wide characters.
4182 """
4183 if isinstance(line, unicode):
4184 width = 0
erg@google.com8a95ecc2011-09-08 00:45:54 +00004185 for uc in unicodedata.normalize('NFC', line):
4186 if unicodedata.east_asian_width(uc) in ('W', 'F'):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004187 width += 2
erg@google.com8a95ecc2011-09-08 00:45:54 +00004188 elif not unicodedata.combining(uc):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004189 width += 1
4190 return width
4191 else:
4192 return len(line)
4193
4194
erg@google.comd350fe52013-01-14 17:51:48 +00004195def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
erg@google.com8a95ecc2011-09-08 00:45:54 +00004196 error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004197 """Checks rules from the 'C++ style rules' section of cppguide.html.
4198
4199 Most of these rules are hard to test (naming, comment style), but we
4200 do what we can. In particular we check for 2-space indents, line lengths,
4201 tab usage, spaces inside code, etc.
4202
4203 Args:
4204 filename: The name of the current file.
4205 clean_lines: A CleansedLines instance containing the file.
4206 linenum: The number of the line to check.
4207 file_extension: The extension (without the dot) of the filename.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004208 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00004209 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004210 error: The function to call with any errors found.
4211 """
4212
erg@google.com2aa59982013-10-28 19:09:25 +00004213 # Don't use "elided" lines here, otherwise we can't check commented lines.
4214 # Don't want to use "raw" either, because we don't want to check inside C++11
4215 # raw strings,
4216 raw_lines = clean_lines.lines_without_raw_strings
erg@google.com4e00b9a2009-01-12 23:05:11 +00004217 line = raw_lines[linenum]
Alex Vakulenko01e47232016-05-06 13:54:15 -07004218 prev = raw_lines[linenum - 1] if linenum > 0 else ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00004219
4220 if line.find('\t') != -1:
4221 error(filename, linenum, 'whitespace/tab', 1,
4222 'Tab found; better to use spaces')
4223
4224 # One or three blank spaces at the beginning of the line is weird; it's
4225 # hard to reconcile that with 2-space indents.
4226 # NOTE: here are the conditions rob pike used for his tests. Mine aren't
4227 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces
4228 # if(RLENGTH > 20) complain = 0;
4229 # if(match($0, " +(error|private|public|protected):")) complain = 0;
4230 # if(match(prev, "&& *$")) complain = 0;
4231 # if(match(prev, "\\|\\| *$")) complain = 0;
4232 # if(match(prev, "[\",=><] *$")) complain = 0;
4233 # if(match($0, " <<")) complain = 0;
4234 # if(match(prev, " +for \\(")) complain = 0;
4235 # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
avakulenko@google.com02af6282014-06-04 18:53:25 +00004236 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$'
4237 classinfo = nesting_state.InnermostClass()
erg@google.com4e00b9a2009-01-12 23:05:11 +00004238 initial_spaces = 0
4239 cleansed_line = clean_lines.elided[linenum]
4240 while initial_spaces < len(line) and line[initial_spaces] == ' ':
4241 initial_spaces += 1
avakulenko@google.com02af6282014-06-04 18:53:25 +00004242 # There are certain situations we allow one space, notably for
4243 # section labels, and also lines containing multi-line raw strings.
Alex Vakulenko01e47232016-05-06 13:54:15 -07004244 # We also don't check for lines that look like continuation lines
4245 # (of lines ending in double quotes, commas, equals, or angle brackets)
4246 # because the rules for how to indent those are non-trivial.
4247 if (not Search(r'[",=><] *$', prev) and
4248 (initial_spaces == 1 or initial_spaces == 3) and
4249 not Match(scope_or_label_pattern, cleansed_line) and
4250 not (clean_lines.raw_lines[linenum] != line and
4251 Match(r'^\s*""', line))):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004252 error(filename, linenum, 'whitespace/indent', 3,
4253 'Weird number of spaces at line-start. '
4254 'Are you using a 2-space indent?')
erg@google.com4e00b9a2009-01-12 23:05:11 +00004255
Alex Vakulenko01e47232016-05-06 13:54:15 -07004256 if line and line[-1].isspace():
4257 error(filename, linenum, 'whitespace/end_of_line', 4,
4258 'Line ends in whitespace. Consider deleting these extra spaces.')
4259
erg@google.com4e00b9a2009-01-12 23:05:11 +00004260 # Check if the line is a header guard.
4261 is_header_guard = False
4262 if file_extension == 'h':
4263 cppvar = GetHeaderGuardCPPVariable(filename)
4264 if (line.startswith('#ifndef %s' % cppvar) or
4265 line.startswith('#define %s' % cppvar) or
4266 line.startswith('#endif // %s' % cppvar)):
4267 is_header_guard = True
4268 # #include lines and header guards can be long, since there's no clean way to
4269 # split them.
erg@google.coma87abb82009-02-24 01:41:01 +00004270 #
4271 # URLs can be long too. It's possible to split these, but it makes them
4272 # harder to cut&paste.
erg@google.comd7d27472011-09-07 17:36:35 +00004273 #
4274 # The "$Id:...$" comment may also get very long without it being the
4275 # developers fault.
erg@google.coma87abb82009-02-24 01:41:01 +00004276 if (not line.startswith('#include') and not is_header_guard and
erg@google.comd7d27472011-09-07 17:36:35 +00004277 not Match(r'^\s*//.*http(s?)://\S*$', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004278 not Match(r'^\s*//\s*[^\s]*$', line) and
erg@google.comd7d27472011-09-07 17:36:35 +00004279 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004280 line_width = GetLineWidth(line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004281 if line_width > _line_length:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004282 error(filename, linenum, 'whitespace/line_length', 2,
erg@google.comab53edf2013-11-05 22:23:37 +00004283 'Lines should be <= %i characters long' % _line_length)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004284
4285 if (cleansed_line.count(';') > 1 and
4286 # for loops are allowed two ;'s (and may run over two lines).
4287 cleansed_line.find('for') == -1 and
4288 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
4289 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
4290 # It's ok to have many commands in a switch case that fits in 1 line
4291 not ((cleansed_line.find('case ') != -1 or
4292 cleansed_line.find('default:') != -1) and
4293 cleansed_line.find('break;') != -1)):
erg@google.comd350fe52013-01-14 17:51:48 +00004294 error(filename, linenum, 'whitespace/newline', 0,
erg@google.com4e00b9a2009-01-12 23:05:11 +00004295 'More than one command on the same line')
4296
4297 # Some more style checks
4298 CheckBraces(filename, clean_lines, linenum, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004299 CheckTrailingSemicolon(filename, clean_lines, linenum, error)
erg@google.comc6671232013-10-25 21:44:03 +00004300 CheckEmptyBlockBody(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004301 CheckAccess(filename, clean_lines, linenum, nesting_state, error)
4302 CheckSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004303 CheckOperatorSpacing(filename, clean_lines, linenum, error)
4304 CheckParenthesisSpacing(filename, clean_lines, linenum, error)
4305 CheckCommaSpacing(filename, clean_lines, linenum, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07004306 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004307 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004308 CheckCheck(filename, clean_lines, linenum, error)
erg@google.comd350fe52013-01-14 17:51:48 +00004309 CheckAltTokens(filename, clean_lines, linenum, error)
4310 classinfo = nesting_state.InnermostClass()
4311 if classinfo:
4312 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004313
4314
erg@google.com4e00b9a2009-01-12 23:05:11 +00004315_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
4316# Matches the first component of a filename delimited by -s and _s. That is:
4317# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
4318# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
4319# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
4320# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
4321_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
4322
4323
4324def _DropCommonSuffixes(filename):
4325 """Drops common suffixes like _test.cc or -inl.h from filename.
4326
4327 For example:
4328 >>> _DropCommonSuffixes('foo/foo-inl.h')
4329 'foo/foo'
4330 >>> _DropCommonSuffixes('foo/bar/foo.cc')
4331 'foo/bar/foo'
4332 >>> _DropCommonSuffixes('foo/foo_internal.h')
4333 'foo/foo'
4334 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
4335 'foo/foo_unusualinternal'
4336
4337 Args:
4338 filename: The input filename.
4339
4340 Returns:
4341 The filename with the common suffix removed.
4342 """
4343 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
4344 'inl.h', 'impl.h', 'internal.h'):
4345 if (filename.endswith(suffix) and len(filename) > len(suffix) and
4346 filename[-len(suffix) - 1] in ('-', '_')):
4347 return filename[:-len(suffix) - 1]
4348 return os.path.splitext(filename)[0]
4349
4350
erg@google.com4e00b9a2009-01-12 23:05:11 +00004351def _ClassifyInclude(fileinfo, include, is_system):
4352 """Figures out what kind of header 'include' is.
4353
4354 Args:
4355 fileinfo: The current file cpplint is running over. A FileInfo instance.
4356 include: The path to a #included file.
4357 is_system: True if the #include used <> rather than "".
4358
4359 Returns:
4360 One of the _XXX_HEADER constants.
4361
4362 For example:
4363 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
4364 _C_SYS_HEADER
4365 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
4366 _CPP_SYS_HEADER
4367 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
4368 _LIKELY_MY_HEADER
4369 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
4370 ... 'bar/foo_other_ext.h', False)
4371 _POSSIBLE_MY_HEADER
4372 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
4373 _OTHER_HEADER
4374 """
4375 # This is a list of all standard c++ header files, except
4376 # those already checked for above.
erg@google.comfd5da632013-10-25 17:39:45 +00004377 is_cpp_h = include in _CPP_HEADERS
erg@google.com4e00b9a2009-01-12 23:05:11 +00004378
4379 if is_system:
4380 if is_cpp_h:
4381 return _CPP_SYS_HEADER
4382 else:
4383 return _C_SYS_HEADER
4384
4385 # If the target file and the include we're checking share a
4386 # basename when we drop common extensions, and the include
4387 # lives in . , then it's likely to be owned by the target file.
4388 target_dir, target_base = (
4389 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
4390 include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
4391 if target_base == include_base and (
4392 include_dir == target_dir or
4393 include_dir == os.path.normpath(target_dir + '/../public')):
4394 return _LIKELY_MY_HEADER
4395
4396 # If the target and include share some initial basename
4397 # component, it's possible the target is implementing the
4398 # include, so it's allowed to be first, but we'll never
4399 # complain if it's not there.
4400 target_first_component = _RE_FIRST_COMPONENT.match(target_base)
4401 include_first_component = _RE_FIRST_COMPONENT.match(include_base)
4402 if (target_first_component and include_first_component and
4403 target_first_component.group(0) ==
4404 include_first_component.group(0)):
4405 return _POSSIBLE_MY_HEADER
4406
4407 return _OTHER_HEADER
4408
4409
erg@google.coma87abb82009-02-24 01:41:01 +00004410
erg@google.come35f7652009-06-19 20:52:09 +00004411def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
4412 """Check rules that are applicable to #include lines.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004413
erg@google.come35f7652009-06-19 20:52:09 +00004414 Strings on #include lines are NOT removed from elided line, to make
4415 certain tasks easier. However, to prevent false positives, checks
4416 applicable to #include lines in CheckLanguage must be put here.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004417
4418 Args:
4419 filename: The name of the current file.
4420 clean_lines: A CleansedLines instance containing the file.
4421 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004422 include_state: An _IncludeState instance in which the headers are inserted.
4423 error: The function to call with any errors found.
4424 """
4425 fileinfo = FileInfo(filename)
erg@google.come35f7652009-06-19 20:52:09 +00004426 line = clean_lines.lines[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00004427
4428 # "include" should use the new style "foo/bar.h" instead of just "bar.h"
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004429 # Only do this check if the included header follows google naming
4430 # conventions. If not, assume that it's a 3rd party API that
4431 # requires special include conventions.
4432 #
4433 # We also make an exception for Lua headers, which follow google
4434 # naming convention but not the include convention.
4435 match = Match(r'#include\s*"([^/]+\.h)"', line)
4436 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)):
erg@google.com4e00b9a2009-01-12 23:05:11 +00004437 error(filename, linenum, 'build/include', 4,
4438 'Include the directory when naming .h files')
4439
4440 # we shouldn't include a file more than once. actually, there are a
4441 # handful of instances where doing so is okay, but in general it's
4442 # not.
erg@google.come35f7652009-06-19 20:52:09 +00004443 match = _RE_PATTERN_INCLUDE.search(line)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004444 if match:
4445 include = match.group(2)
4446 is_system = (match.group(1) == '<')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004447 duplicate_line = include_state.FindHeader(include)
4448 if duplicate_line >= 0:
erg@google.com4e00b9a2009-01-12 23:05:11 +00004449 error(filename, linenum, 'build/include', 4,
4450 '"%s" already included at %s:%s' %
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004451 (include, filename, duplicate_line))
avakulenko@google.com554223d2014-12-04 22:00:20 +00004452 elif (include.endswith('.cc') and
4453 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)):
4454 error(filename, linenum, 'build/include', 4,
4455 'Do not include .cc files from other packages')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004456 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include):
4457 include_state.include_list[-1].append((include, linenum))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004458
4459 # We want to ensure that headers appear in the right order:
4460 # 1) for foo.cc, foo.h (preferred location)
4461 # 2) c system files
4462 # 3) cpp system files
4463 # 4) for foo.cc, foo.h (deprecated location)
4464 # 5) other google headers
4465 #
4466 # We classify each include statement as one of those 5 types
4467 # using a number of techniques. The include_state object keeps
4468 # track of the highest type seen, and complains if we see a
4469 # lower type after that.
4470 error_message = include_state.CheckNextIncludeOrder(
4471 _ClassifyInclude(fileinfo, include, is_system))
4472 if error_message:
4473 error(filename, linenum, 'build/include_order', 4,
4474 '%s. Should be: %s.h, c system, c++ system, other.' %
4475 (error_message, fileinfo.BaseName()))
erg@google.comfd5da632013-10-25 17:39:45 +00004476 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include)
4477 if not include_state.IsInAlphabeticalOrder(
4478 clean_lines, linenum, canonical_include):
erg@google.coma868d2d2009-10-09 21:18:45 +00004479 error(filename, linenum, 'build/include_alpha', 4,
4480 'Include "%s" not in alphabetical order' % include)
erg@google.comfd5da632013-10-25 17:39:45 +00004481 include_state.SetLastHeader(canonical_include)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004482
erg@google.come35f7652009-06-19 20:52:09 +00004483
erg@google.com8a95ecc2011-09-08 00:45:54 +00004484
4485def _GetTextInside(text, start_pattern):
erg@google.com2aa59982013-10-28 19:09:25 +00004486 r"""Retrieves all the text between matching open and close parentheses.
erg@google.com8a95ecc2011-09-08 00:45:54 +00004487
4488 Given a string of lines and a regular expression string, retrieve all the text
4489 following the expression and between opening punctuation symbols like
4490 (, [, or {, and the matching close-punctuation symbol. This properly nested
4491 occurrences of the punctuations, so for the text like
4492 printf(a(), b(c()));
4493 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
4494 start_pattern must match string having an open punctuation symbol at the end.
4495
4496 Args:
4497 text: The lines to extract text. Its comments and strings must be elided.
4498 It can be single line and can span multiple lines.
4499 start_pattern: The regexp string indicating where to start extracting
4500 the text.
4501 Returns:
4502 The extracted text.
4503 None if either the opening string or ending punctuation could not be found.
4504 """
avakulenko@google.com02af6282014-06-04 18:53:25 +00004505 # TODO(unknown): Audit cpplint.py to see what places could be profitably
erg@google.com8a95ecc2011-09-08 00:45:54 +00004506 # rewritten to use _GetTextInside (and use inferior regexp matching today).
4507
4508 # Give opening punctuations to get the matching close-punctuations.
4509 matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4510 closing_punctuation = set(matching_punctuation.itervalues())
4511
4512 # Find the position to start extracting text.
4513 match = re.search(start_pattern, text, re.M)
4514 if not match: # start_pattern not found in text.
4515 return None
4516 start_position = match.end(0)
4517
4518 assert start_position > 0, (
4519 'start_pattern must ends with an opening punctuation.')
4520 assert text[start_position - 1] in matching_punctuation, (
4521 'start_pattern must ends with an opening punctuation.')
4522 # Stack of closing punctuations we expect to have in text after position.
4523 punctuation_stack = [matching_punctuation[text[start_position - 1]]]
4524 position = start_position
4525 while punctuation_stack and position < len(text):
4526 if text[position] == punctuation_stack[-1]:
4527 punctuation_stack.pop()
4528 elif text[position] in closing_punctuation:
4529 # A closing punctuation without matching opening punctuations.
4530 return None
4531 elif text[position] in matching_punctuation:
4532 punctuation_stack.append(matching_punctuation[text[position]])
4533 position += 1
4534 if punctuation_stack:
4535 # Opening punctuations left without matching close-punctuations.
4536 return None
4537 # punctuations match.
4538 return text[start_position:position - 1]
4539
4540
erg@google.comfd5da632013-10-25 17:39:45 +00004541# Patterns for matching call-by-reference parameters.
erg@google.com2aa59982013-10-28 19:09:25 +00004542#
4543# Supports nested templates up to 2 levels deep using this messy pattern:
4544# < (?: < (?: < [^<>]*
4545# >
4546# | [^<>] )*
4547# >
4548# | [^<>] )*
4549# >
erg@google.comfd5da632013-10-25 17:39:45 +00004550_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]*
4551_RE_PATTERN_TYPE = (
4552 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?'
erg@google.com2aa59982013-10-28 19:09:25 +00004553 r'(?:\w|'
4554 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|'
4555 r'::)+')
erg@google.comfd5da632013-10-25 17:39:45 +00004556# A call-by-reference parameter ends with '& identifier'.
4557_RE_PATTERN_REF_PARAM = re.compile(
4558 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*'
4559 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]')
4560# A call-by-const-reference parameter either ends with 'const& identifier'
4561# or looks like 'const type& identifier' when 'type' is atomic.
4562_RE_PATTERN_CONST_REF_PARAM = (
4563 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT +
4564 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004565# Stream types.
4566_RE_PATTERN_REF_STREAM_PARAM = (
4567 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')')
erg@google.comfd5da632013-10-25 17:39:45 +00004568
4569
4570def CheckLanguage(filename, clean_lines, linenum, file_extension,
4571 include_state, nesting_state, error):
erg@google.come35f7652009-06-19 20:52:09 +00004572 """Checks rules from the 'C++ language rules' section of cppguide.html.
4573
4574 Some of these rules are hard to test (function overloading, using
4575 uint32 inappropriately), but we do the best we can.
4576
4577 Args:
4578 filename: The name of the current file.
4579 clean_lines: A CleansedLines instance containing the file.
4580 linenum: The number of the line to check.
4581 file_extension: The extension (without the dot) of the filename.
4582 include_state: An _IncludeState instance in which the headers are inserted.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004583 nesting_state: A NestingState instance which maintains information about
erg@google.comfd5da632013-10-25 17:39:45 +00004584 the current stack of nested blocks being parsed.
erg@google.come35f7652009-06-19 20:52:09 +00004585 error: The function to call with any errors found.
4586 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00004587 # If the line is empty or consists of entirely a comment, no need to
4588 # check it.
4589 line = clean_lines.elided[linenum]
4590 if not line:
4591 return
4592
erg@google.come35f7652009-06-19 20:52:09 +00004593 match = _RE_PATTERN_INCLUDE.search(line)
4594 if match:
4595 CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
4596 return
4597
erg@google.com2aa59982013-10-28 19:09:25 +00004598 # Reset include state across preprocessor directives. This is meant
4599 # to silence warnings for conditional includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004600 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line)
4601 if match:
4602 include_state.ResetSection(match.group(1))
erg@google.com2aa59982013-10-28 19:09:25 +00004603
erg@google.com4e00b9a2009-01-12 23:05:11 +00004604 # Make Windows paths like Unix.
4605 fullname = os.path.abspath(filename).replace('\\', '/')
Alex Vakulenko01e47232016-05-06 13:54:15 -07004606
avakulenko@google.com02af6282014-06-04 18:53:25 +00004607 # Perform other checks now that we are sure that this is not an include line
4608 CheckCasts(filename, clean_lines, linenum, error)
4609 CheckGlobalStatic(filename, clean_lines, linenum, error)
4610 CheckPrintf(filename, clean_lines, linenum, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00004611
4612 if file_extension == 'h':
4613 # TODO(unknown): check that 1-arg constructors are explicit.
4614 # How to tell it's a constructor?
4615 # (handled in CheckForNonStandardConstructs for now)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004616 # TODO(unknown): check that classes declare or disable copy/assign
erg@google.com4e00b9a2009-01-12 23:05:11 +00004617 # (level 1 error)
4618 pass
4619
4620 # Check if people are using the verboten C basic types. The only exception
4621 # we regularly allow is "unsigned short port" for port.
4622 if Search(r'\bshort port\b', line):
4623 if not Search(r'\bunsigned short port\b', line):
4624 error(filename, linenum, 'runtime/int', 4,
4625 'Use "unsigned short" for ports, not "short"')
4626 else:
4627 match = Search(r'\b(short|long(?! +double)|long long)\b', line)
4628 if match:
4629 error(filename, linenum, 'runtime/int', 4,
4630 'Use int16/int64/etc, rather than the C type %s' % match.group(1))
4631
erg@google.coma868d2d2009-10-09 21:18:45 +00004632 # Check if some verboten operator overloading is going on
4633 # TODO(unknown): catch out-of-line unary operator&:
4634 # class X {};
4635 # int operator&(const X& x) { return 42; } // unary operator&
4636 # The trick is it's hard to tell apart from binary operator&:
4637 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
4638 if Search(r'\boperator\s*&\s*\(\s*\)', line):
4639 error(filename, linenum, 'runtime/operator', 4,
4640 'Unary operator& is dangerous. Do not use it.')
4641
erg@google.com4e00b9a2009-01-12 23:05:11 +00004642 # Check for suspicious usage of "if" like
4643 # } if (a == b) {
4644 if Search(r'\}\s*if\s*\(', line):
4645 error(filename, linenum, 'readability/braces', 4,
4646 'Did you mean "else if"? If not, start a new line for "if".')
4647
4648 # Check for potential format string bugs like printf(foo).
4649 # We constrain the pattern not to pick things like DocidForPrintf(foo).
4650 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
avakulenko@google.com02af6282014-06-04 18:53:25 +00004651 # TODO(unknown): Catch the following case. Need to change the calling
erg@google.com8a95ecc2011-09-08 00:45:54 +00004652 # convention of the whole function to process multiple line to handle it.
4653 # printf(
4654 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
4655 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
4656 if printf_args:
4657 match = Match(r'([\w.\->()]+)$', printf_args)
erg@google.comd350fe52013-01-14 17:51:48 +00004658 if match and match.group(1) != '__VA_ARGS__':
erg@google.com8a95ecc2011-09-08 00:45:54 +00004659 function_name = re.search(r'\b((?:string)?printf)\s*\(',
4660 line, re.I).group(1)
4661 error(filename, linenum, 'runtime/printf', 4,
4662 'Potential format string bug. Do %s("%%s", %s) instead.'
4663 % (function_name, match.group(1)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00004664
4665 # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
4666 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
4667 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
4668 error(filename, linenum, 'runtime/memset', 4,
4669 'Did you mean "memset(%s, 0, %s)"?'
4670 % (match.group(1), match.group(2)))
4671
4672 if Search(r'\busing namespace\b', line):
4673 error(filename, linenum, 'build/namespaces', 5,
4674 'Do not use namespace using-directives. '
4675 'Use using-declarations instead.')
4676
4677 # Detect variable-length arrays.
4678 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
4679 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
4680 match.group(3).find(']') == -1):
4681 # Split the size using space and arithmetic operators as delimiters.
4682 # If any of the resulting tokens are not compile time constants then
4683 # report the error.
4684 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
4685 is_const = True
4686 skip_next = False
4687 for tok in tokens:
4688 if skip_next:
4689 skip_next = False
4690 continue
4691
4692 if Search(r'sizeof\(.+\)', tok): continue
4693 if Search(r'arraysize\(\w+\)', tok): continue
4694
4695 tok = tok.lstrip('(')
4696 tok = tok.rstrip(')')
4697 if not tok: continue
4698 if Match(r'\d+', tok): continue
4699 if Match(r'0[xX][0-9a-fA-F]+', tok): continue
4700 if Match(r'k[A-Z0-9]\w*', tok): continue
4701 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
4702 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
4703 # A catch all for tricky sizeof cases, including 'sizeof expression',
4704 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
erg@google.com8a95ecc2011-09-08 00:45:54 +00004705 # requires skipping the next token because we split on ' ' and '*'.
erg@google.com4e00b9a2009-01-12 23:05:11 +00004706 if tok.startswith('sizeof'):
4707 skip_next = True
4708 continue
4709 is_const = False
4710 break
4711 if not is_const:
4712 error(filename, linenum, 'runtime/arrays', 1,
4713 'Do not use variable-length arrays. Use an appropriately named '
4714 "('k' followed by CamelCase) compile-time constant for the size.")
4715
erg@google.com4e00b9a2009-01-12 23:05:11 +00004716 # Check for use of unnamed namespaces in header files. Registration
4717 # macros are typically OK, so we allow use of "namespace {" on lines
4718 # that end with backslashes.
4719 if (file_extension == 'h'
4720 and Search(r'\bnamespace\s*{', line)
4721 and line[-1] != '\\'):
4722 error(filename, linenum, 'build/namespaces', 4,
4723 'Do not use unnamed namespaces in header files. See '
Ackermann Yuriy79692902016-04-01 21:41:34 +13004724 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
erg@google.com4e00b9a2009-01-12 23:05:11 +00004725 ' for more information.')
4726
avakulenko@google.com02af6282014-06-04 18:53:25 +00004727
4728def CheckGlobalStatic(filename, clean_lines, linenum, error):
4729 """Check for unsafe global or static objects.
4730
4731 Args:
4732 filename: The name of the current file.
4733 clean_lines: A CleansedLines instance containing the file.
4734 linenum: The number of the line to check.
4735 error: The function to call with any errors found.
4736 """
4737 line = clean_lines.elided[linenum]
4738
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004739 # Match two lines at a time to support multiline declarations
4740 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line):
4741 line += clean_lines.elided[linenum + 1].strip()
4742
avakulenko@google.com02af6282014-06-04 18:53:25 +00004743 # Check for people declaring static/global STL strings at the top level.
4744 # This is dangerous because the C++ language does not guarantee that
Alex Vakulenko01e47232016-05-06 13:54:15 -07004745 # globals with constructors are initialized before the first access, and
4746 # also because globals can be destroyed when some threads are still running.
4747 # TODO(unknown): Generalize this to also find static unique_ptr instances.
4748 # TODO(unknown): File bugs for clang-tidy to find these.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004749 match = Match(
Alex Vakulenko01e47232016-05-06 13:54:15 -07004750 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +'
4751 r'([a-zA-Z0-9_:]+)\b(.*)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00004752 line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004753
avakulenko@google.com02af6282014-06-04 18:53:25 +00004754 # Remove false positives:
4755 # - String pointers (as opposed to values).
4756 # string *pointer
4757 # const string *pointer
4758 # string const *pointer
4759 # string *const pointer
4760 #
4761 # - Functions and template specializations.
4762 # string Function<Type>(...
4763 # string Class<Type>::Method(...
4764 #
4765 # - Operators. These are matched separately because operator names
4766 # cross non-word boundaries, and trying to match both operators
4767 # and functions at the same time would decrease accuracy of
4768 # matching identifiers.
4769 # string Class::operator*()
4770 if (match and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004771 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and
avakulenko@google.com02af6282014-06-04 18:53:25 +00004772 not Search(r'\boperator\W', line) and
Alex Vakulenko01e47232016-05-06 13:54:15 -07004773 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))):
4774 if Search(r'\bconst\b', line):
4775 error(filename, linenum, 'runtime/string', 4,
4776 'For a static/global string constant, use a C style string '
4777 'instead: "%schar%s %s[]".' %
4778 (match.group(1), match.group(2) or '', match.group(3)))
4779 else:
4780 error(filename, linenum, 'runtime/string', 4,
4781 'Static/global string variables are not permitted.')
avakulenko@google.com02af6282014-06-04 18:53:25 +00004782
Alex Vakulenko01e47232016-05-06 13:54:15 -07004783 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or
4784 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004785 error(filename, linenum, 'runtime/init', 4,
4786 'You seem to be initializing a member variable with itself.')
4787
4788
4789def CheckPrintf(filename, clean_lines, linenum, error):
4790 """Check for printf related issues.
4791
4792 Args:
4793 filename: The name of the current file.
4794 clean_lines: A CleansedLines instance containing the file.
4795 linenum: The number of the line to check.
4796 error: The function to call with any errors found.
4797 """
4798 line = clean_lines.elided[linenum]
4799
4800 # When snprintf is used, the second argument shouldn't be a literal.
4801 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
4802 if match and match.group(2) != '0':
4803 # If 2nd arg is zero, snprintf is used to calculate size.
4804 error(filename, linenum, 'runtime/printf', 3,
4805 'If you can, use sizeof(%s) instead of %s as the 2nd arg '
4806 'to snprintf.' % (match.group(1), match.group(2)))
4807
4808 # Check if some verboten C functions are being used.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004809 if Search(r'\bsprintf\s*\(', line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00004810 error(filename, linenum, 'runtime/printf', 5,
4811 'Never use sprintf. Use snprintf instead.')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004812 match = Search(r'\b(strcpy|strcat)\s*\(', line)
avakulenko@google.com02af6282014-06-04 18:53:25 +00004813 if match:
4814 error(filename, linenum, 'runtime/printf', 4,
4815 'Almost always, snprintf is better than %s' % match.group(1))
4816
4817
4818def IsDerivedFunction(clean_lines, linenum):
4819 """Check if current line contains an inherited function.
4820
4821 Args:
4822 clean_lines: A CleansedLines instance containing the file.
4823 linenum: The number of the line to check.
4824 Returns:
4825 True if current line contains a function with "override"
4826 virt-specifier.
4827 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004828 # Scan back a few lines for start of current function
4829 for i in xrange(linenum, max(-1, linenum - 10), -1):
4830 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
4831 if match:
4832 # Look for "override" after the matching closing parenthesis
4833 line, _, closing_paren = CloseExpression(
4834 clean_lines, i, len(match.group(1)))
4835 return (closing_paren >= 0 and
4836 Search(r'\boverride\b', line[closing_paren:]))
4837 return False
avakulenko@google.com02af6282014-06-04 18:53:25 +00004838
4839
avakulenko@google.com554223d2014-12-04 22:00:20 +00004840def IsOutOfLineMethodDefinition(clean_lines, linenum):
4841 """Check if current line contains an out-of-line method definition.
4842
4843 Args:
4844 clean_lines: A CleansedLines instance containing the file.
4845 linenum: The number of the line to check.
4846 Returns:
4847 True if current line contains an out-of-line method definition.
4848 """
4849 # Scan back a few lines for start of current function
4850 for i in xrange(linenum, max(-1, linenum - 10), -1):
4851 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
4852 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
4853 return False
4854
4855
avakulenko@google.com02af6282014-06-04 18:53:25 +00004856def IsInitializerList(clean_lines, linenum):
4857 """Check if current line is inside constructor initializer list.
4858
4859 Args:
4860 clean_lines: A CleansedLines instance containing the file.
4861 linenum: The number of the line to check.
4862 Returns:
4863 True if current line appears to be inside constructor initializer
4864 list, False otherwise.
4865 """
4866 for i in xrange(linenum, 1, -1):
4867 line = clean_lines.elided[i]
4868 if i == linenum:
4869 remove_function_body = Match(r'^(.*)\{\s*$', line)
4870 if remove_function_body:
4871 line = remove_function_body.group(1)
4872
4873 if Search(r'\s:\s*\w+[({]', line):
4874 # A lone colon tend to indicate the start of a constructor
4875 # initializer list. It could also be a ternary operator, which
4876 # also tend to appear in constructor initializer lists as
4877 # opposed to parameter lists.
4878 return True
4879 if Search(r'\}\s*,\s*$', line):
4880 # A closing brace followed by a comma is probably the end of a
4881 # brace-initialized member in constructor initializer list.
4882 return True
4883 if Search(r'[{};]\s*$', line):
4884 # Found one of the following:
4885 # - A closing brace or semicolon, probably the end of the previous
4886 # function.
4887 # - An opening brace, probably the start of current class or namespace.
4888 #
4889 # Current line is probably not inside an initializer list since
4890 # we saw one of those things without seeing the starting colon.
4891 return False
4892
4893 # Got to the beginning of the file without seeing the start of
4894 # constructor initializer list.
4895 return False
4896
4897
erg@google.comc6671232013-10-25 21:44:03 +00004898def CheckForNonConstReference(filename, clean_lines, linenum,
4899 nesting_state, error):
4900 """Check for non-const references.
4901
4902 Separate from CheckLanguage since it scans backwards from current
4903 line, instead of scanning forward.
4904
4905 Args:
4906 filename: The name of the current file.
4907 clean_lines: A CleansedLines instance containing the file.
4908 linenum: The number of the line to check.
avakulenko@google.com02af6282014-06-04 18:53:25 +00004909 nesting_state: A NestingState instance which maintains information about
erg@google.comc6671232013-10-25 21:44:03 +00004910 the current stack of nested blocks being parsed.
4911 error: The function to call with any errors found.
4912 """
4913 # Do nothing if there is no '&' on current line.
4914 line = clean_lines.elided[linenum]
4915 if '&' not in line:
4916 return
4917
avakulenko@google.com02af6282014-06-04 18:53:25 +00004918 # If a function is inherited, current function doesn't have much of
4919 # a choice, so any non-const references should not be blamed on
4920 # derived function.
4921 if IsDerivedFunction(clean_lines, linenum):
4922 return
4923
avakulenko@google.com554223d2014-12-04 22:00:20 +00004924 # Don't warn on out-of-line method definitions, as we would warn on the
4925 # in-line declaration, if it isn't marked with 'override'.
4926 if IsOutOfLineMethodDefinition(clean_lines, linenum):
4927 return
4928
erg@google.com2aa59982013-10-28 19:09:25 +00004929 # Long type names may be broken across multiple lines, usually in one
4930 # of these forms:
4931 # LongType
4932 # ::LongTypeContinued &identifier
4933 # LongType::
4934 # LongTypeContinued &identifier
4935 # LongType<
4936 # ...>::LongTypeContinued &identifier
4937 #
4938 # If we detected a type split across two lines, join the previous
4939 # line to current line so that we can match const references
4940 # accordingly.
erg@google.comc6671232013-10-25 21:44:03 +00004941 #
4942 # Note that this only scans back one line, since scanning back
4943 # arbitrary number of lines would be expensive. If you have a type
4944 # that spans more than 2 lines, please use a typedef.
4945 if linenum > 1:
4946 previous = None
erg@google.com2aa59982013-10-28 19:09:25 +00004947 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004948 # previous_line\n + ::current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004949 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004950 clean_lines.elided[linenum - 1])
erg@google.com2aa59982013-10-28 19:09:25 +00004951 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line):
erg@google.comc6671232013-10-25 21:44:03 +00004952 # previous_line::\n + current_line
erg@google.com2aa59982013-10-28 19:09:25 +00004953 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$',
erg@google.comc6671232013-10-25 21:44:03 +00004954 clean_lines.elided[linenum - 1])
4955 if previous:
4956 line = previous.group(1) + line.lstrip()
erg@google.com2aa59982013-10-28 19:09:25 +00004957 else:
4958 # Check for templated parameter that is split across multiple lines
4959 endpos = line.rfind('>')
4960 if endpos > -1:
4961 (_, startline, startpos) = ReverseCloseExpression(
4962 clean_lines, linenum, endpos)
4963 if startpos > -1 and startline < linenum:
4964 # Found the matching < on an earlier line, collect all
4965 # pieces up to current line.
4966 line = ''
4967 for i in xrange(startline, linenum + 1):
4968 line += clean_lines.elided[i].strip()
erg@google.comc6671232013-10-25 21:44:03 +00004969
4970 # Check for non-const references in function parameters. A single '&' may
4971 # found in the following places:
4972 # inside expression: binary & for bitwise AND
4973 # inside expression: unary & for taking the address of something
4974 # inside declarators: reference parameter
4975 # We will exclude the first two cases by checking that we are not inside a
4976 # function body, including one that was just introduced by a trailing '{'.
erg@google.comc6671232013-10-25 21:44:03 +00004977 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare].
avakulenko@google.com02af6282014-06-04 18:53:25 +00004978 if (nesting_state.previous_stack_top and
4979 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or
4980 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))):
4981 # Not at toplevel, not within a class, and not within a namespace
4982 return
4983
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00004984 # Avoid initializer lists. We only need to scan back from the
4985 # current line for something that starts with ':'.
4986 #
4987 # We don't need to check the current line, since the '&' would
4988 # appear inside the second set of parentheses on the current line as
4989 # opposed to the first set.
4990 if linenum > 0:
4991 for i in xrange(linenum - 1, max(0, linenum - 10), -1):
4992 previous_line = clean_lines.elided[i]
4993 if not Search(r'[),]\s*$', previous_line):
4994 break
4995 if Match(r'^\s*:\s+\S', previous_line):
4996 return
4997
avakulenko@google.com02af6282014-06-04 18:53:25 +00004998 # Avoid preprocessors
4999 if Search(r'\\\s*$', line):
5000 return
5001
5002 # Avoid constructor initializer lists
5003 if IsInitializerList(clean_lines, linenum):
5004 return
5005
erg@google.comc6671232013-10-25 21:44:03 +00005006 # We allow non-const references in a few standard places, like functions
5007 # called "swap()" or iostream operators like "<<" or ">>". Do not check
5008 # those function parameters.
5009 #
5010 # We also accept & in static_assert, which looks like a function but
5011 # it's actually a declaration expression.
5012 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|'
5013 r'operator\s*[<>][<>]|'
5014 r'static_assert|COMPILE_ASSERT'
5015 r')\s*\(')
5016 if Search(whitelisted_functions, line):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005017 return
erg@google.comc6671232013-10-25 21:44:03 +00005018 elif not Search(r'\S+\([^)]*$', line):
5019 # Don't see a whitelisted function on this line. Actually we
5020 # didn't see any function name on this line, so this is likely a
5021 # multi-line parameter list. Try a bit harder to catch this case.
5022 for i in xrange(2):
5023 if (linenum > i and
5024 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005025 return
erg@google.comc6671232013-10-25 21:44:03 +00005026
avakulenko@google.com02af6282014-06-04 18:53:25 +00005027 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body
5028 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005029 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and
5030 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005031 error(filename, linenum, 'runtime/references', 2,
5032 'Is this a non-const reference? '
5033 'If so, make const or use a pointer: ' +
5034 ReplaceAll(' *<', '<', parameter))
5035
5036
5037def CheckCasts(filename, clean_lines, linenum, error):
5038 """Various cast related checks.
5039
5040 Args:
5041 filename: The name of the current file.
5042 clean_lines: A CleansedLines instance containing the file.
5043 linenum: The number of the line to check.
5044 error: The function to call with any errors found.
5045 """
5046 line = clean_lines.elided[linenum]
5047
5048 # Check to see if they're using an conversion function cast.
5049 # I just try to capture the most common basic types, though there are more.
5050 # Parameterless conversion functions, such as bool(), are allowed as they are
5051 # probably a member operator declaration or default constructor.
5052 match = Search(
Alex Vakulenko01e47232016-05-06 13:54:15 -07005053 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
avakulenko@google.com02af6282014-06-04 18:53:25 +00005054 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5055 r'(\([^)].*)', line)
5056 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5057 if match and not expecting_function:
5058 matched_type = match.group(2)
5059
5060 # matched_new_or_template is used to silence two false positives:
5061 # - New operators
5062 # - Template arguments with function types
5063 #
5064 # For template arguments, we match on types immediately following
5065 # an opening bracket without any spaces. This is a fast way to
5066 # silence the common case where the function type is the first
5067 # template argument. False negative with less-than comparison is
5068 # avoided because those operators are usually followed by a space.
5069 #
5070 # function<double(double)> // bracket + no space = false positive
5071 # value < double(42) // bracket + space = true positive
5072 matched_new_or_template = match.group(1)
5073
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005074 # Avoid arrays by looking for brackets that come after the closing
5075 # parenthesis.
5076 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5077 return
5078
avakulenko@google.com02af6282014-06-04 18:53:25 +00005079 # Other things to ignore:
5080 # - Function pointers
5081 # - Casts to pointer types
5082 # - Placement new
5083 # - Alias declarations
5084 matched_funcptr = match.group(3)
5085 if (matched_new_or_template is None and
5086 not (matched_funcptr and
5087 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5088 matched_funcptr) or
5089 matched_funcptr.startswith('(*)'))) and
5090 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5091 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5092 error(filename, linenum, 'readability/casting', 4,
5093 'Using deprecated casting style. '
5094 'Use static_cast<%s>(...) instead' %
5095 matched_type)
5096
5097 if not expecting_function:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005098 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005099 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
5100
5101 # This doesn't catch all cases. Consider (const char * const)"hello".
5102 #
5103 # (char *) "foo" should always be a const_cast (reinterpret_cast won't
5104 # compile).
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005105 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast',
5106 r'\((char\s?\*+\s?)\)\s*"', error):
avakulenko@google.com02af6282014-06-04 18:53:25 +00005107 pass
5108 else:
5109 # Check pointer casts for other than string constants
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005110 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast',
5111 r'\((\w+\s?\*+\s?)\)', error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005112
5113 # In addition, we look for people taking the address of a cast. This
5114 # is dangerous -- casts can assign to temporaries, so the pointer doesn't
5115 # point where you think.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005116 #
5117 # Some non-identifier character is required before the '&' for the
5118 # expression to be recognized as a cast. These are casts:
5119 # expression = &static_cast<int*>(temporary());
5120 # function(&(int*)(temporary()));
5121 #
5122 # This is not a cast:
5123 # reference_type&(int* function_param);
avakulenko@google.com02af6282014-06-04 18:53:25 +00005124 match = Search(
avakulenko@google.com554223d2014-12-04 22:00:20 +00005125 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|'
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005126 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005127 if match:
avakulenko@google.com02af6282014-06-04 18:53:25 +00005128 # Try a better error message when the & is bound to something
5129 # dereferenced by the casted pointer, as opposed to the casted
5130 # pointer itself.
5131 parenthesis_error = False
5132 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line)
5133 if match:
5134 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1)))
5135 if x1 >= 0 and clean_lines.elided[y1][x1] == '(':
5136 _, y2, x2 = CloseExpression(clean_lines, y1, x1)
5137 if x2 >= 0:
5138 extended_line = clean_lines.elided[y2][x2:]
5139 if y2 < clean_lines.NumLines() - 1:
5140 extended_line += clean_lines.elided[y2 + 1]
5141 if Match(r'\s*(?:->|\[)', extended_line):
5142 parenthesis_error = True
5143
5144 if parenthesis_error:
5145 error(filename, linenum, 'readability/casting', 4,
5146 ('Are you taking an address of something dereferenced '
5147 'from a cast? Wrapping the dereferenced expression in '
5148 'parentheses will make the binding more obvious'))
5149 else:
5150 error(filename, linenum, 'runtime/casting', 4,
5151 ('Are you taking an address of a cast? '
5152 'This is dangerous: could be a temp var. '
5153 'Take the address before doing the cast, rather than after'))
erg@google.comc6671232013-10-25 21:44:03 +00005154
erg@google.com4e00b9a2009-01-12 23:05:11 +00005155
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005156def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005157 """Checks for a C-style cast by looking for the pattern.
5158
erg@google.com4e00b9a2009-01-12 23:05:11 +00005159 Args:
5160 filename: The name of the current file.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005161 clean_lines: A CleansedLines instance containing the file.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005162 linenum: The number of the line to check.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005163 cast_type: The string for the C++ cast to recommend. This is either
erg@google.com8a95ecc2011-09-08 00:45:54 +00005164 reinterpret_cast, static_cast, or const_cast, depending.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005165 pattern: The regular expression used to find C-style casts.
5166 error: The function to call with any errors found.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005167
5168 Returns:
5169 True if an error was emitted.
5170 False otherwise.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005171 """
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005172 line = clean_lines.elided[linenum]
erg@google.com4e00b9a2009-01-12 23:05:11 +00005173 match = Search(pattern, line)
5174 if not match:
erg@google.com8a95ecc2011-09-08 00:45:54 +00005175 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005176
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005177 # Exclude lines with keywords that tend to look like casts
5178 context = line[0:match.start(1) - 1]
5179 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context):
5180 return False
5181
5182 # Try expanding current context to see if we one level of
5183 # parentheses inside a macro.
5184 if linenum > 0:
5185 for i in xrange(linenum - 1, max(0, linenum - 5), -1):
5186 context = clean_lines.elided[i] + context
5187 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
erg@google.comfd5da632013-10-25 17:39:45 +00005188 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005189
erg@google.comd350fe52013-01-14 17:51:48 +00005190 # operator++(int) and operator--(int)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005191 if context.endswith(' operator++') or context.endswith(' operator--'):
erg@google.comd350fe52013-01-14 17:51:48 +00005192 return False
5193
Alex Vakulenko01e47232016-05-06 13:54:15 -07005194 # A single unnamed argument for a function tends to look like old style cast.
5195 # If we see those, don't issue warnings for deprecated casts.
erg@google.comc6671232013-10-25 21:44:03 +00005196 remainder = line[match.end(0):]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005197 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005198 remainder):
Alex Vakulenko01e47232016-05-06 13:54:15 -07005199 return False
erg@google.com4e00b9a2009-01-12 23:05:11 +00005200
5201 # At this point, all that should be left is actual casts.
5202 error(filename, linenum, 'readability/casting', 4,
5203 'Using C-style cast. Use %s<%s>(...) instead' %
5204 (cast_type, match.group(1)))
5205
erg@google.com8a95ecc2011-09-08 00:45:54 +00005206 return True
5207
erg@google.com4e00b9a2009-01-12 23:05:11 +00005208
avakulenko@google.com02af6282014-06-04 18:53:25 +00005209def ExpectingFunctionArgs(clean_lines, linenum):
5210 """Checks whether where function type arguments are expected.
5211
5212 Args:
5213 clean_lines: A CleansedLines instance containing the file.
5214 linenum: The number of the line to check.
5215
5216 Returns:
5217 True if the line at 'linenum' is inside something that expects arguments
5218 of function types.
5219 """
5220 line = clean_lines.elided[linenum]
5221 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
5222 (linenum >= 2 and
5223 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$',
5224 clean_lines.elided[linenum - 1]) or
5225 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$',
5226 clean_lines.elided[linenum - 2]) or
5227 Search(r'\bstd::m?function\s*\<\s*$',
5228 clean_lines.elided[linenum - 1]))))
5229
5230
erg@google.com4e00b9a2009-01-12 23:05:11 +00005231_HEADERS_CONTAINING_TEMPLATES = (
5232 ('<deque>', ('deque',)),
5233 ('<functional>', ('unary_function', 'binary_function',
5234 'plus', 'minus', 'multiplies', 'divides', 'modulus',
5235 'negate',
5236 'equal_to', 'not_equal_to', 'greater', 'less',
5237 'greater_equal', 'less_equal',
5238 'logical_and', 'logical_or', 'logical_not',
5239 'unary_negate', 'not1', 'binary_negate', 'not2',
5240 'bind1st', 'bind2nd',
5241 'pointer_to_unary_function',
5242 'pointer_to_binary_function',
5243 'ptr_fun',
5244 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
5245 'mem_fun_ref_t',
5246 'const_mem_fun_t', 'const_mem_fun1_t',
5247 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
5248 'mem_fun_ref',
5249 )),
5250 ('<limits>', ('numeric_limits',)),
5251 ('<list>', ('list',)),
5252 ('<map>', ('map', 'multimap',)),
5253 ('<memory>', ('allocator',)),
5254 ('<queue>', ('queue', 'priority_queue',)),
5255 ('<set>', ('set', 'multiset',)),
5256 ('<stack>', ('stack',)),
5257 ('<string>', ('char_traits', 'basic_string',)),
avakulenko@google.com554223d2014-12-04 22:00:20 +00005258 ('<tuple>', ('tuple',)),
erg@google.com4e00b9a2009-01-12 23:05:11 +00005259 ('<utility>', ('pair',)),
5260 ('<vector>', ('vector',)),
5261
5262 # gcc extensions.
5263 # Note: std::hash is their hash, ::hash is our hash
5264 ('<hash_map>', ('hash_map', 'hash_multimap',)),
5265 ('<hash_set>', ('hash_set', 'hash_multiset',)),
5266 ('<slist>', ('slist',)),
5267 )
5268
Alex Vakulenko01e47232016-05-06 13:54:15 -07005269_HEADERS_MAYBE_TEMPLATES = (
5270 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort',
5271 'transform',
5272 )),
5273 ('<utility>', ('swap',)),
5274 )
5275
erg@google.com4e00b9a2009-01-12 23:05:11 +00005276_RE_PATTERN_STRING = re.compile(r'\bstring\b')
5277
Alex Vakulenko01e47232016-05-06 13:54:15 -07005278_re_pattern_headers_maybe_templates = []
5279for _header, _templates in _HEADERS_MAYBE_TEMPLATES:
5280 for _template in _templates:
5281 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
5282 # type::max().
5283 _re_pattern_headers_maybe_templates.append(
5284 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
5285 _template,
5286 _header))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005287
Alex Vakulenko01e47232016-05-06 13:54:15 -07005288# Other scripts may reach in and modify this pattern.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005289_re_pattern_templates = []
5290for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
5291 for _template in _templates:
5292 _re_pattern_templates.append(
5293 (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
5294 _template + '<>',
5295 _header))
5296
5297
erg@google.come35f7652009-06-19 20:52:09 +00005298def FilesBelongToSameModule(filename_cc, filename_h):
5299 """Check if these two filenames belong to the same module.
5300
5301 The concept of a 'module' here is a as follows:
5302 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
5303 same 'module' if they are in the same directory.
5304 some/path/public/xyzzy and some/path/internal/xyzzy are also considered
5305 to belong to the same module here.
5306
5307 If the filename_cc contains a longer path than the filename_h, for example,
5308 '/absolute/path/to/base/sysinfo.cc', and this file would include
5309 'base/sysinfo.h', this function also produces the prefix needed to open the
5310 header. This is used by the caller of this function to more robustly open the
5311 header file. We don't have access to the real include paths in this context,
5312 so we need this guesswork here.
5313
5314 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
5315 according to this implementation. Because of this, this function gives
5316 some false positives. This should be sufficiently rare in practice.
5317
5318 Args:
5319 filename_cc: is the path for the .cc file
5320 filename_h: is the path for the header path
5321
5322 Returns:
5323 Tuple with a bool and a string:
5324 bool: True if filename_cc and filename_h belong to the same module.
5325 string: the additional prefix needed to open the header file.
5326 """
5327
Alex Vakulenko01e47232016-05-06 13:54:15 -07005328 fileinfo = FileInfo(filename_cc)
5329 if not fileinfo.IsSource():
erg@google.come35f7652009-06-19 20:52:09 +00005330 return (False, '')
Alex Vakulenko01e47232016-05-06 13:54:15 -07005331 filename_cc = filename_cc[:-len(fileinfo.Extension())]
5332 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName())
5333 if matched_test_suffix:
5334 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))]
erg@google.come35f7652009-06-19 20:52:09 +00005335 filename_cc = filename_cc.replace('/public/', '/')
5336 filename_cc = filename_cc.replace('/internal/', '/')
5337
5338 if not filename_h.endswith('.h'):
5339 return (False, '')
5340 filename_h = filename_h[:-len('.h')]
5341 if filename_h.endswith('-inl'):
5342 filename_h = filename_h[:-len('-inl')]
5343 filename_h = filename_h.replace('/public/', '/')
5344 filename_h = filename_h.replace('/internal/', '/')
5345
5346 files_belong_to_same_module = filename_cc.endswith(filename_h)
5347 common_path = ''
5348 if files_belong_to_same_module:
5349 common_path = filename_cc[:-len(filename_h)]
5350 return files_belong_to_same_module, common_path
5351
5352
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005353def UpdateIncludeState(filename, include_dict, io=codecs):
5354 """Fill up the include_dict with new includes found from the file.
erg@google.come35f7652009-06-19 20:52:09 +00005355
5356 Args:
5357 filename: the name of the header to read.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005358 include_dict: a dictionary in which the headers are inserted.
erg@google.come35f7652009-06-19 20:52:09 +00005359 io: The io factory to use to read the file. Provided for testability.
5360
5361 Returns:
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005362 True if a header was successfully added. False otherwise.
erg@google.come35f7652009-06-19 20:52:09 +00005363 """
5364 headerfile = None
5365 try:
5366 headerfile = io.open(filename, 'r', 'utf8', 'replace')
5367 except IOError:
5368 return False
5369 linenum = 0
5370 for line in headerfile:
5371 linenum += 1
5372 clean_line = CleanseComments(line)
5373 match = _RE_PATTERN_INCLUDE.search(clean_line)
5374 if match:
5375 include = match.group(2)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005376 include_dict.setdefault(include, linenum)
erg@google.come35f7652009-06-19 20:52:09 +00005377 return True
5378
5379
5380def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5381 io=codecs):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005382 """Reports for missing stl includes.
5383
5384 This function will output warnings to make sure you are including the headers
5385 necessary for the stl containers and functions that you use. We only give one
5386 reason to include a header. For example, if you use both equal_to<> and
5387 less<> in a .h file, only one (the latter in the file) of these will be
5388 reported as a reason to include the <functional>.
5389
erg@google.com4e00b9a2009-01-12 23:05:11 +00005390 Args:
5391 filename: The name of the current file.
5392 clean_lines: A CleansedLines instance containing the file.
5393 include_state: An _IncludeState instance.
5394 error: The function to call with any errors found.
erg@google.come35f7652009-06-19 20:52:09 +00005395 io: The IO factory to use to read the header file. Provided for unittest
5396 injection.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005397 """
erg@google.com4e00b9a2009-01-12 23:05:11 +00005398 required = {} # A map of header name to linenumber and the template entity.
5399 # Example of required: { '<functional>': (1219, 'less<>') }
5400
5401 for linenum in xrange(clean_lines.NumLines()):
5402 line = clean_lines.elided[linenum]
5403 if not line or line[0] == '#':
5404 continue
5405
5406 # String is special -- it is a non-templatized type in STL.
erg@google.com8a95ecc2011-09-08 00:45:54 +00005407 matched = _RE_PATTERN_STRING.search(line)
5408 if matched:
erg+personal@google.com05189642010-04-30 20:43:03 +00005409 # Don't warn about strings in non-STL namespaces:
5410 # (We check only the first match per line; good enough.)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005411 prefix = line[:matched.start()]
erg+personal@google.com05189642010-04-30 20:43:03 +00005412 if prefix.endswith('std::') or not prefix.endswith('::'):
5413 required['<string>'] = (linenum, 'string')
erg@google.com4e00b9a2009-01-12 23:05:11 +00005414
Alex Vakulenko01e47232016-05-06 13:54:15 -07005415 for pattern, template, header in _re_pattern_headers_maybe_templates:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005416 if pattern.search(line):
5417 required[header] = (linenum, template)
5418
5419 # The following function is just a speed up, no semantics are changed.
5420 if not '<' in line: # Reduces the cpu time usage by skipping lines.
5421 continue
5422
5423 for pattern, template, header in _re_pattern_templates:
5424 if pattern.search(line):
5425 required[header] = (linenum, template)
5426
erg@google.come35f7652009-06-19 20:52:09 +00005427 # The policy is that if you #include something in foo.h you don't need to
5428 # include it again in foo.cc. Here, we will look at possible includes.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005429 # Let's flatten the include_state include_list and copy it into a dictionary.
5430 include_dict = dict([item for sublist in include_state.include_list
5431 for item in sublist])
erg@google.come35f7652009-06-19 20:52:09 +00005432
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005433 # Did we find the header for this file (if any) and successfully load it?
erg@google.come35f7652009-06-19 20:52:09 +00005434 header_found = False
5435
5436 # Use the absolute path so that matching works properly.
erg@google.com90ecb622012-01-30 19:34:23 +00005437 abs_filename = FileInfo(filename).FullName()
erg@google.come35f7652009-06-19 20:52:09 +00005438
5439 # For Emacs's flymake.
5440 # If cpplint is invoked from Emacs's flymake, a temporary file is generated
5441 # by flymake and that file name might end with '_flymake.cc'. In that case,
5442 # restore original file name here so that the corresponding header file can be
5443 # found.
5444 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
5445 # instead of 'foo_flymake.h'
erg+personal@google.com05189642010-04-30 20:43:03 +00005446 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
erg@google.come35f7652009-06-19 20:52:09 +00005447
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005448 # include_dict is modified during iteration, so we iterate over a copy of
erg@google.come35f7652009-06-19 20:52:09 +00005449 # the keys.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005450 header_keys = include_dict.keys()
erg@google.com8a95ecc2011-09-08 00:45:54 +00005451 for header in header_keys:
erg@google.come35f7652009-06-19 20:52:09 +00005452 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
5453 fullpath = common_path + header
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005454 if same_module and UpdateIncludeState(fullpath, include_dict, io):
erg@google.come35f7652009-06-19 20:52:09 +00005455 header_found = True
5456
5457 # If we can't find the header file for a .cc, assume it's because we don't
5458 # know where to look. In that case we'll give up as we're not sure they
5459 # didn't include it in the .h file.
5460 # TODO(unknown): Do a better job of finding .h files so we are confident that
5461 # not having the .h file means there isn't one.
5462 if filename.endswith('.cc') and not header_found:
5463 return
5464
erg@google.com4e00b9a2009-01-12 23:05:11 +00005465 # All the lines have been processed, report the errors found.
5466 for required_header_unstripped in required:
5467 template = required[required_header_unstripped][1]
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005468 if required_header_unstripped.strip('<>"') not in include_dict:
erg@google.com4e00b9a2009-01-12 23:05:11 +00005469 error(filename, required[required_header_unstripped][0],
5470 'build/include_what_you_use', 4,
5471 'Add #include ' + required_header_unstripped + ' for ' + template)
5472
5473
erg@google.com8a95ecc2011-09-08 00:45:54 +00005474_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
5475
5476
5477def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
5478 """Check that make_pair's template arguments are deduced.
5479
avakulenko@google.com02af6282014-06-04 18:53:25 +00005480 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are
erg@google.com8a95ecc2011-09-08 00:45:54 +00005481 specified explicitly, and such use isn't intended in any case.
5482
5483 Args:
5484 filename: The name of the current file.
5485 clean_lines: A CleansedLines instance containing the file.
5486 linenum: The number of the line to check.
5487 error: The function to call with any errors found.
5488 """
erg@google.com2aa59982013-10-28 19:09:25 +00005489 line = clean_lines.elided[linenum]
erg@google.com8a95ecc2011-09-08 00:45:54 +00005490 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
5491 if match:
5492 error(filename, linenum, 'build/explicit_make_pair',
5493 4, # 4 = high confidence
erg@google.comd350fe52013-01-14 17:51:48 +00005494 'For C++11-compatibility, omit template arguments from make_pair'
5495 ' OR use pair directly OR if appropriate, construct a pair directly')
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005496
5497
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005498def CheckRedundantVirtual(filename, clean_lines, linenum, error):
5499 """Check if line contains a redundant "virtual" function-specifier.
5500
5501 Args:
5502 filename: The name of the current file.
5503 clean_lines: A CleansedLines instance containing the file.
5504 linenum: The number of the line to check.
5505 error: The function to call with any errors found.
5506 """
5507 # Look for "virtual" on current line.
5508 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005509 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005510 if not virtual: return
5511
avakulenko@google.com554223d2014-12-04 22:00:20 +00005512 # Ignore "virtual" keywords that are near access-specifiers. These
5513 # are only used in class base-specifier and do not apply to member
5514 # functions.
5515 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or
5516 Match(r'^\s+(public|protected|private)\b', virtual.group(3))):
5517 return
5518
5519 # Ignore the "virtual" keyword from virtual base classes. Usually
5520 # there is a column on the same line in these cases (virtual base
5521 # classes are rare in google3 because multiple inheritance is rare).
5522 if Match(r'^.*[^:]:[^:].*$', line): return
5523
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005524 # Look for the next opening parenthesis. This is the start of the
5525 # parameter list (possibly on the next line shortly after virtual).
5526 # TODO(unknown): doesn't work if there are virtual functions with
5527 # decltype() or other things that use parentheses, but csearch suggests
5528 # that this is rare.
5529 end_col = -1
5530 end_line = -1
avakulenko@google.com554223d2014-12-04 22:00:20 +00005531 start_col = len(virtual.group(2))
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005532 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())):
5533 line = clean_lines.elided[start_line][start_col:]
5534 parameter_list = Match(r'^([^(]*)\(', line)
5535 if parameter_list:
5536 # Match parentheses to find the end of the parameter list
5537 (_, end_line, end_col) = CloseExpression(
5538 clean_lines, start_line, start_col + len(parameter_list.group(1)))
5539 break
5540 start_col = 0
5541
5542 if end_col < 0:
5543 return # Couldn't find end of parameter list, give up
5544
5545 # Look for "override" or "final" after the parameter list
5546 # (possibly on the next few lines).
5547 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())):
5548 line = clean_lines.elided[i][end_col:]
5549 match = Search(r'\b(override|final)\b', line)
5550 if match:
5551 error(filename, linenum, 'readability/inheritance', 4,
5552 ('"virtual" is redundant since function is '
5553 'already declared as "%s"' % match.group(1)))
5554
5555 # Set end_col to check whole lines after we are done with the
5556 # first line.
5557 end_col = 0
5558 if Search(r'[^\w]\s*$', line):
5559 break
5560
5561
5562def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
5563 """Check if line contains a redundant "override" or "final" virt-specifier.
5564
5565 Args:
5566 filename: The name of the current file.
5567 clean_lines: A CleansedLines instance containing the file.
5568 linenum: The number of the line to check.
5569 error: The function to call with any errors found.
5570 """
avakulenko@google.com554223d2014-12-04 22:00:20 +00005571 # Look for closing parenthesis nearby. We need one to confirm where
5572 # the declarator ends and where the virt-specifier starts to avoid
5573 # false positives.
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005574 line = clean_lines.elided[linenum]
avakulenko@google.com554223d2014-12-04 22:00:20 +00005575 declarator_end = line.rfind(')')
5576 if declarator_end >= 0:
5577 fragment = line[declarator_end:]
5578 else:
5579 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0:
5580 fragment = line
5581 else:
5582 return
5583
5584 # Check that at most one of "override" or "final" is present, not both
5585 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment):
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005586 error(filename, linenum, 'readability/inheritance', 4,
5587 ('"override" is redundant since function is '
5588 'already declared as "final"'))
5589
5590
5591
5592
5593# Returns true if we are at a new block, and it is directly
5594# inside of a namespace.
5595def IsBlockInNameSpace(nesting_state, is_forward_declaration):
5596 """Checks that the new block is directly in a namespace.
5597
5598 Args:
5599 nesting_state: The _NestingState object that contains info about our state.
5600 is_forward_declaration: If the class is a forward declared class.
5601 Returns:
5602 Whether or not the new block is directly in a namespace.
5603 """
5604 if is_forward_declaration:
5605 if len(nesting_state.stack) >= 1 and (
5606 isinstance(nesting_state.stack[-1], _NamespaceInfo)):
5607 return True
5608 else:
5609 return False
5610
5611 return (len(nesting_state.stack) > 1 and
5612 nesting_state.stack[-1].check_namespace_indentation and
5613 isinstance(nesting_state.stack[-2], _NamespaceInfo))
5614
5615
5616def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item,
5617 raw_lines_no_comments, linenum):
5618 """This method determines if we should apply our namespace indentation check.
5619
5620 Args:
5621 nesting_state: The current nesting state.
5622 is_namespace_indent_item: If we just put a new class on the stack, True.
5623 If the top of the stack is not a class, or we did not recently
5624 add the class, False.
5625 raw_lines_no_comments: The lines without the comments.
5626 linenum: The current line number we are processing.
5627
5628 Returns:
5629 True if we should apply our namespace indentation check. Currently, it
5630 only works for classes and namespaces inside of a namespace.
5631 """
5632
5633 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments,
5634 linenum)
5635
5636 if not (is_namespace_indent_item or is_forward_declaration):
5637 return False
5638
5639 # If we are in a macro, we do not want to check the namespace indentation.
5640 if IsMacroDefinition(raw_lines_no_comments, linenum):
5641 return False
5642
5643 return IsBlockInNameSpace(nesting_state, is_forward_declaration)
5644
5645
5646# Call this method if the line is directly inside of a namespace.
5647# If the line above is blank (excluding comments) or the start of
5648# an inner namespace, it cannot be indented.
5649def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum,
5650 error):
5651 line = raw_lines_no_comments[linenum]
5652 if Match(r'^\s+', line):
5653 error(filename, linenum, 'runtime/indentation_namespace', 4,
5654 'Do not indent within a namespace')
erg@google.com8a95ecc2011-09-08 00:45:54 +00005655
5656
erg@google.comd350fe52013-01-14 17:51:48 +00005657def ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005658 include_state, function_state, nesting_state, error,
5659 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005660 """Processes a single line in the file.
5661
5662 Args:
5663 filename: Filename of the file that is being processed.
5664 file_extension: The extension (dot not included) of the file.
5665 clean_lines: An array of strings, each representing a line of the file,
5666 with comments stripped.
5667 line: Number of line being processed.
5668 include_state: An _IncludeState instance in which the headers are inserted.
5669 function_state: A _FunctionState instance which counts function lines, etc.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005670 nesting_state: A NestingState instance which maintains information about
erg@google.comd350fe52013-01-14 17:51:48 +00005671 the current stack of nested blocks being parsed.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005672 error: A callable to which errors are reported, which takes 4 arguments:
5673 filename, line number, error level, and message
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005674 extra_check_functions: An array of additional check functions that will be
5675 run on each source line. Each function takes 4
5676 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005677 """
5678 raw_lines = clean_lines.raw_lines
erg+personal@google.com05189642010-04-30 20:43:03 +00005679 ParseNolintSuppressions(filename, raw_lines[line], line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005680 nesting_state.Update(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005681 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line,
5682 error)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005683 if nesting_state.InAsmBlock(): return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005684 CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005685 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
erg@google.comd350fe52013-01-14 17:51:48 +00005686 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005687 CheckLanguage(filename, clean_lines, line, file_extension, include_state,
erg@google.comfd5da632013-10-25 17:39:45 +00005688 nesting_state, error)
erg@google.comc6671232013-10-25 21:44:03 +00005689 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005690 CheckForNonStandardConstructs(filename, clean_lines, line,
erg@google.comd350fe52013-01-14 17:51:48 +00005691 nesting_state, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005692 CheckVlogArguments(filename, clean_lines, line, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005693 CheckPosixThreading(filename, clean_lines, line, error)
erg@google.com36649102009-03-25 21:18:36 +00005694 CheckInvalidIncrement(filename, clean_lines, line, error)
erg@google.com8a95ecc2011-09-08 00:45:54 +00005695 CheckMakePairUsesDeduction(filename, clean_lines, line, error)
avakulenko@google.coma8ee7ea2014-08-11 19:41:35 +00005696 CheckRedundantVirtual(filename, clean_lines, line, error)
5697 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error)
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005698 for check_fn in extra_check_functions:
5699 check_fn(filename, clean_lines, line, error)
erg@google.com7430eef2014-07-28 22:33:46 +00005700
avakulenko@google.com02af6282014-06-04 18:53:25 +00005701def FlagCxx11Features(filename, clean_lines, linenum, error):
5702 """Flag those c++11 features that we only allow in certain places.
5703
5704 Args:
5705 filename: The name of the current file.
5706 clean_lines: A CleansedLines instance containing the file.
5707 linenum: The number of the line to check.
5708 error: The function to call with any errors found.
5709 """
5710 line = clean_lines.elided[linenum]
5711
avakulenko@google.com02af6282014-06-04 18:53:25 +00005712 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005713
5714 # Flag unapproved C++ TR1 headers.
5715 if include and include.group(1).startswith('tr1/'):
5716 error(filename, linenum, 'build/c++tr1', 5,
5717 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1))
5718
5719 # Flag unapproved C++11 headers.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005720 if include and include.group(1) in ('cfenv',
5721 'condition_variable',
5722 'fenv.h',
5723 'future',
5724 'mutex',
5725 'thread',
5726 'chrono',
5727 'ratio',
5728 'regex',
5729 'system_error',
5730 ):
5731 error(filename, linenum, 'build/c++11', 5,
5732 ('<%s> is an unapproved C++11 header.') % include.group(1))
5733
5734 # The only place where we need to worry about C++11 keywords and library
5735 # features in preprocessor directives is in macro definitions.
5736 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return
5737
5738 # These are classes and free functions. The classes are always
5739 # mentioned as std::*, but we only catch the free functions if
5740 # they're not found by ADL. They're alphabetical by header.
5741 for top_name in (
5742 # type_traits
5743 'alignment_of',
5744 'aligned_union',
avakulenko@google.com02af6282014-06-04 18:53:25 +00005745 ):
5746 if Search(r'\bstd::%s\b' % top_name, line):
5747 error(filename, linenum, 'build/c++11', 5,
5748 ('std::%s is an unapproved C++11 class or function. Send c-style '
5749 'an example of where it would make your code more readable, and '
5750 'they may let you use it.') % top_name)
5751
5752
Alex Vakulenko01e47232016-05-06 13:54:15 -07005753def FlagCxx14Features(filename, clean_lines, linenum, error):
5754 """Flag those C++14 features that we restrict.
5755
5756 Args:
5757 filename: The name of the current file.
5758 clean_lines: A CleansedLines instance containing the file.
5759 linenum: The number of the line to check.
5760 error: The function to call with any errors found.
5761 """
5762 line = clean_lines.elided[linenum]
5763
5764 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line)
5765
5766 # Flag unapproved C++14 headers.
5767 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'):
5768 error(filename, linenum, 'build/c++14', 5,
5769 ('<%s> is an unapproved C++14 header.') % include.group(1))
5770
5771
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005772def ProcessFileData(filename, file_extension, lines, error,
5773 extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005774 """Performs lint checks and reports any errors to the given error function.
5775
5776 Args:
5777 filename: Filename of the file that is being processed.
5778 file_extension: The extension (dot not included) of the file.
5779 lines: An array of strings, each representing a line of the file, with the
erg@google.com8a95ecc2011-09-08 00:45:54 +00005780 last element being empty if the file is terminated with a newline.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005781 error: A callable to which errors are reported, which takes 4 arguments:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005782 filename, line number, error level, and message
5783 extra_check_functions: An array of additional check functions that will be
5784 run on each source line. Each function takes 4
5785 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005786 """
5787 lines = (['// marker so line numbers and indices both start at 1'] + lines +
5788 ['// marker so line numbers end in a known way'])
5789
5790 include_state = _IncludeState()
5791 function_state = _FunctionState()
avakulenko@google.com02af6282014-06-04 18:53:25 +00005792 nesting_state = NestingState()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005793
erg+personal@google.com05189642010-04-30 20:43:03 +00005794 ResetNolintSuppressions()
5795
erg@google.com4e00b9a2009-01-12 23:05:11 +00005796 CheckForCopyright(filename, lines, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005797 ProcessGlobalSuppresions(lines)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005798 RemoveMultiLineComments(filename, lines, error)
5799 clean_lines = CleansedLines(lines)
avakulenko@google.com554223d2014-12-04 22:00:20 +00005800
5801 if file_extension == 'h':
5802 CheckForHeaderGuard(filename, clean_lines, error)
5803
erg@google.com4e00b9a2009-01-12 23:05:11 +00005804 for line in xrange(clean_lines.NumLines()):
5805 ProcessLine(filename, file_extension, clean_lines, line,
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005806 include_state, function_state, nesting_state, error,
5807 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005808 FlagCxx11Features(filename, clean_lines, line, error)
erg@google.com2aa59982013-10-28 19:09:25 +00005809 nesting_state.CheckCompletedBlocks(filename, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005810
5811 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
Alex Vakulenko01e47232016-05-06 13:54:15 -07005812
avakulenko@google.com554223d2014-12-04 22:00:20 +00005813 # Check that the .cc file has included its header if it exists.
Alex Vakulenko01e47232016-05-06 13:54:15 -07005814 if _IsSourceExtension(file_extension):
avakulenko@google.com554223d2014-12-04 22:00:20 +00005815 CheckHeaderFileIncluded(filename, include_state, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005816
5817 # We check here rather than inside ProcessLine so that we see raw
5818 # lines rather than "cleaned" lines.
erg@google.com2aa59982013-10-28 19:09:25 +00005819 CheckForBadCharacters(filename, lines, error)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005820
5821 CheckForNewlineAtEOF(filename, lines, error)
5822
erg@google.com7430eef2014-07-28 22:33:46 +00005823def ProcessConfigOverrides(filename):
5824 """ Loads the configuration files and processes the config overrides.
5825
5826 Args:
5827 filename: The name of the file being processed by the linter.
5828
5829 Returns:
5830 False if the current |filename| should not be processed further.
5831 """
5832
5833 abs_filename = os.path.abspath(filename)
5834 cfg_filters = []
5835 keep_looking = True
5836 while keep_looking:
5837 abs_path, base_name = os.path.split(abs_filename)
5838 if not base_name:
5839 break # Reached the root directory.
5840
5841 cfg_file = os.path.join(abs_path, "CPPLINT.cfg")
5842 abs_filename = abs_path
5843 if not os.path.isfile(cfg_file):
5844 continue
5845
5846 try:
5847 with open(cfg_file) as file_handle:
5848 for line in file_handle:
5849 line, _, _ = line.partition('#') # Remove comments.
5850 if not line.strip():
5851 continue
5852
5853 name, _, val = line.partition('=')
5854 name = name.strip()
5855 val = val.strip()
5856 if name == 'set noparent':
5857 keep_looking = False
5858 elif name == 'filter':
5859 cfg_filters.append(val)
5860 elif name == 'exclude_files':
5861 # When matching exclude_files pattern, use the base_name of
5862 # the current file name or the directory name we are processing.
5863 # For example, if we are checking for lint errors in /foo/bar/baz.cc
5864 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config
5865 # file's "exclude_files" filter is meant to be checked against "bar"
5866 # and not "baz" nor "bar/baz.cc".
5867 if base_name:
5868 pattern = re.compile(val)
5869 if pattern.match(base_name):
5870 sys.stderr.write('Ignoring "%s": file excluded by "%s". '
5871 'File path component "%s" matches '
5872 'pattern "%s"\n' %
5873 (filename, cfg_file, base_name, val))
5874 return False
avakulenko@google.com310681b2014-08-22 19:38:55 +00005875 elif name == 'linelength':
5876 global _line_length
5877 try:
5878 _line_length = int(val)
5879 except ValueError:
5880 sys.stderr.write('Line length must be numeric.')
erg@google.com7430eef2014-07-28 22:33:46 +00005881 else:
5882 sys.stderr.write(
5883 'Invalid configuration option (%s) in file %s\n' %
5884 (name, cfg_file))
5885
5886 except IOError:
5887 sys.stderr.write(
5888 "Skipping config file '%s': Can't open for reading\n" % cfg_file)
5889 keep_looking = False
5890
5891 # Apply all the accumulated filters in reverse order (top-level directory
5892 # config options having the least priority).
5893 for filter in reversed(cfg_filters):
5894 _AddFilters(filter)
5895
5896 return True
5897
avakulenko@google.com02af6282014-06-04 18:53:25 +00005898
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005899def ProcessFile(filename, vlevel, extra_check_functions=[]):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005900 """Does google-lint on a single file.
5901
5902 Args:
5903 filename: The name of the file to parse.
5904
5905 vlevel: The level of errors to report. Every error of confidence
5906 >= verbose_level will be reported. 0 is a good default.
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005907
5908 extra_check_functions: An array of additional check functions that will be
5909 run on each source line. Each function takes 4
5910 arguments: filename, clean_lines, line, error
erg@google.com4e00b9a2009-01-12 23:05:11 +00005911 """
5912
5913 _SetVerboseLevel(vlevel)
erg@google.com7430eef2014-07-28 22:33:46 +00005914 _BackupFilters()
5915
5916 if not ProcessConfigOverrides(filename):
5917 _RestoreFilters()
5918 return
erg@google.com4e00b9a2009-01-12 23:05:11 +00005919
avakulenko@google.com02af6282014-06-04 18:53:25 +00005920 lf_lines = []
5921 crlf_lines = []
erg@google.com4e00b9a2009-01-12 23:05:11 +00005922 try:
5923 # Support the UNIX convention of using "-" for stdin. Note that
5924 # we are not opening the file with universal newline support
5925 # (which codecs doesn't support anyway), so the resulting lines do
5926 # contain trailing '\r' characters if we are reading a file that
5927 # has CRLF endings.
5928 # If after the split a trailing '\r' is present, it is removed
avakulenko@google.com02af6282014-06-04 18:53:25 +00005929 # below.
erg@google.com4e00b9a2009-01-12 23:05:11 +00005930 if filename == '-':
5931 lines = codecs.StreamReaderWriter(sys.stdin,
5932 codecs.getreader('utf8'),
5933 codecs.getwriter('utf8'),
5934 'replace').read().split('\n')
5935 else:
5936 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
5937
erg@google.com4e00b9a2009-01-12 23:05:11 +00005938 # Remove trailing '\r'.
avakulenko@google.com02af6282014-06-04 18:53:25 +00005939 # The -1 accounts for the extra trailing blank line we get from split()
5940 for linenum in range(len(lines) - 1):
erg@google.com4e00b9a2009-01-12 23:05:11 +00005941 if lines[linenum].endswith('\r'):
5942 lines[linenum] = lines[linenum].rstrip('\r')
avakulenko@google.com02af6282014-06-04 18:53:25 +00005943 crlf_lines.append(linenum + 1)
5944 else:
5945 lf_lines.append(linenum + 1)
erg@google.com4e00b9a2009-01-12 23:05:11 +00005946
5947 except IOError:
5948 sys.stderr.write(
5949 "Skipping input '%s': Can't open for reading\n" % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00005950 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005951 return
5952
5953 # Note, if no dot is found, this will give the entire filename as the ext.
5954 file_extension = filename[filename.rfind('.') + 1:]
5955
5956 # When reading from stdin, the extension is unknown, so no cpplint tests
5957 # should rely on the extension.
erg@google.com19680272013-12-16 22:48:54 +00005958 if filename != '-' and file_extension not in _valid_extensions:
erg@google.com2aa59982013-10-28 19:09:25 +00005959 sys.stderr.write('Ignoring %s; not a valid file name '
erg@google.com19680272013-12-16 22:48:54 +00005960 '(%s)\n' % (filename, ', '.join(_valid_extensions)))
erg@google.com4e00b9a2009-01-12 23:05:11 +00005961 else:
avakulenko@google.com4b957b22014-06-04 22:48:14 +00005962 ProcessFileData(filename, file_extension, lines, Error,
5963 extra_check_functions)
avakulenko@google.com02af6282014-06-04 18:53:25 +00005964
5965 # If end-of-line sequences are a mix of LF and CR-LF, issue
5966 # warnings on the lines with CR.
5967 #
5968 # Don't issue any warnings if all lines are uniformly LF or CR-LF,
5969 # since critique can handle these just fine, and the style guide
5970 # doesn't dictate a particular end of line sequence.
5971 #
5972 # We can't depend on os.linesep to determine what the desired
5973 # end-of-line sequence should be, since that will return the
5974 # server-side end-of-line sequence.
5975 if lf_lines and crlf_lines:
5976 # Warn on every line with CR. An alternative approach might be to
5977 # check whether the file is mostly CRLF or just LF, and warn on the
5978 # minority, we bias toward LF here since most tools prefer LF.
5979 for linenum in crlf_lines:
5980 Error(filename, linenum, 'whitespace/newline', 1,
5981 'Unexpected \\r (^M) found; better to use only \\n')
erg@google.com4e00b9a2009-01-12 23:05:11 +00005982
5983 sys.stderr.write('Done processing %s\n' % filename)
erg@google.com7430eef2014-07-28 22:33:46 +00005984 _RestoreFilters()
erg@google.com4e00b9a2009-01-12 23:05:11 +00005985
5986
5987def PrintUsage(message):
5988 """Prints a brief usage string and exits, optionally with an error message.
5989
5990 Args:
5991 message: The optional error message.
5992 """
5993 sys.stderr.write(_USAGE)
5994 if message:
5995 sys.exit('\nFATAL ERROR: ' + message)
5996 else:
5997 sys.exit(1)
5998
5999
6000def PrintCategories():
6001 """Prints a list of all the error-categories used by error messages.
6002
6003 These are the categories used to filter messages via --filter.
6004 """
erg+personal@google.com05189642010-04-30 20:43:03 +00006005 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES))
erg@google.com4e00b9a2009-01-12 23:05:11 +00006006 sys.exit(0)
6007
6008
6009def ParseArguments(args):
6010 """Parses the command line arguments.
6011
6012 This may set the output format and verbosity level as side-effects.
6013
6014 Args:
6015 args: The command line arguments:
6016
6017 Returns:
6018 The list of filenames to lint.
6019 """
6020 try:
6021 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
erg@google.coma868d2d2009-10-09 21:18:45 +00006022 'counting=',
erg@google.com4d70a882013-04-16 21:06:32 +00006023 'filter=',
erg@google.comab53edf2013-11-05 22:23:37 +00006024 'root=',
erg@google.com19680272013-12-16 22:48:54 +00006025 'linelength=',
6026 'extensions='])
erg@google.com4e00b9a2009-01-12 23:05:11 +00006027 except getopt.GetoptError:
6028 PrintUsage('Invalid arguments.')
6029
6030 verbosity = _VerboseLevel()
6031 output_format = _OutputFormat()
6032 filters = ''
erg@google.coma868d2d2009-10-09 21:18:45 +00006033 counting_style = ''
erg@google.com4e00b9a2009-01-12 23:05:11 +00006034
6035 for (opt, val) in opts:
6036 if opt == '--help':
6037 PrintUsage(None)
6038 elif opt == '--output':
erg@google.comc6671232013-10-25 21:44:03 +00006039 if val not in ('emacs', 'vs7', 'eclipse'):
erg@google.com02c27fd2013-05-28 21:34:34 +00006040 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006041 output_format = val
6042 elif opt == '--verbose':
6043 verbosity = int(val)
6044 elif opt == '--filter':
6045 filters = val
erg@google.coma87abb82009-02-24 01:41:01 +00006046 if not filters:
erg@google.com4e00b9a2009-01-12 23:05:11 +00006047 PrintCategories()
erg@google.coma868d2d2009-10-09 21:18:45 +00006048 elif opt == '--counting':
6049 if val not in ('total', 'toplevel', 'detailed'):
6050 PrintUsage('Valid counting options are total, toplevel, and detailed')
6051 counting_style = val
erg@google.com4d70a882013-04-16 21:06:32 +00006052 elif opt == '--root':
6053 global _root
6054 _root = val
erg@google.comab53edf2013-11-05 22:23:37 +00006055 elif opt == '--linelength':
6056 global _line_length
6057 try:
6058 _line_length = int(val)
6059 except ValueError:
6060 PrintUsage('Line length must be digits.')
erg@google.com19680272013-12-16 22:48:54 +00006061 elif opt == '--extensions':
6062 global _valid_extensions
6063 try:
6064 _valid_extensions = set(val.split(','))
6065 except ValueError:
6066 PrintUsage('Extensions must be comma seperated list.')
erg@google.com4e00b9a2009-01-12 23:05:11 +00006067
6068 if not filenames:
6069 PrintUsage('No files were specified.')
6070
6071 _SetOutputFormat(output_format)
6072 _SetVerboseLevel(verbosity)
6073 _SetFilters(filters)
erg@google.coma868d2d2009-10-09 21:18:45 +00006074 _SetCountingStyle(counting_style)
erg@google.com4e00b9a2009-01-12 23:05:11 +00006075
6076 return filenames
6077
6078
6079def main():
6080 filenames = ParseArguments(sys.argv[1:])
6081
6082 # Change stderr to write with replacement characters so we don't die
6083 # if we try to print something containing non-ASCII characters.
6084 sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6085 codecs.getreader('utf8'),
6086 codecs.getwriter('utf8'),
6087 'replace')
6088
erg@google.coma868d2d2009-10-09 21:18:45 +00006089 _cpplint_state.ResetErrorCounts()
erg@google.com4e00b9a2009-01-12 23:05:11 +00006090 for filename in filenames:
6091 ProcessFile(filename, _cpplint_state.verbose_level)
erg@google.coma868d2d2009-10-09 21:18:45 +00006092 _cpplint_state.PrintErrorCounts()
6093
erg@google.com4e00b9a2009-01-12 23:05:11 +00006094 sys.exit(_cpplint_state.error_count > 0)
6095
6096
6097if __name__ == '__main__':
6098 main()