1#!/usr/bin/env python 2# 3# Copyright (c) 2009 Google Inc. All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * 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. 18# 19# 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. 30 31"""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 45import copy 46import getopt 47import math # for log 48import os 49import re 50import sre_compile 51import string 52import sys 53import unicodedata 54import sysconfig 55 56try: 57 xrange # Python 2 58except NameError: 59 xrange = range # Python 3 60 61 62_USAGE = """ 63Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] 64 [--counting=total|toplevel|detailed] [--root=subdir] 65 [--linelength=digits] [--headers=x,y,...] 66 [--quiet] 67 <file> [file] ... 68 69 The style guidelines this tries to follow are those in 70 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml 71 72 Every problem is given a confidence score from 1-5, with 5 meaning we are 73 certain of the problem, and 1 meaning it could be a legitimate construct. 74 This will miss some errors, and is not a substitute for a code review. 75 76 To suppress false-positive errors of a certain category, add a 77 'NOLINT(category)' comment to the line. NOLINT or NOLINT(*) 78 suppresses errors of all categories on that line. 79 80 The files passed in will be linted; at least one file must be provided. 81 Default linted extensions are .cc, .cpp, .cu, .cuh and .h. Change the 82 extensions with the --extensions flag. 83 84 Flags: 85 86 output=vs7 87 By default, the output is formatted to ease emacs parsing. Visual Studio 88 compatible output (vs7) may also be used. Other formats are unsupported. 89 90 verbose=# 91 Specify a number 0-5 to restrict errors to certain verbosity levels. 92 93 quiet 94 Don't print anything if no errors are found. 95 96 filter=-x,+y,... 97 Specify a comma-separated list of category-filters to apply: only 98 error messages whose category names pass the filters will be printed. 99 (Category names are printed with the message and look like 100 "[whitespace/indent]".) Filters are evaluated left to right. 101 "-FOO" and "FOO" means "do not print categories that start with FOO". 102 "+FOO" means "do print categories that start with FOO". 103 104 Examples: --filter=-whitespace,+whitespace/braces 105 --filter=whitespace,runtime/printf,+runtime/printf_format 106 --filter=-,+build/include_what_you_use 107 108 To see a list of all the categories used in cpplint, pass no arg: 109 --filter= 110 111 counting=total|toplevel|detailed 112 The total number of errors found is always printed. If 113 'toplevel' is provided, then the count of errors in each of 114 the top-level categories like 'build' and 'whitespace' will 115 also be printed. If 'detailed' is provided, then a count 116 is provided for each category like 'build/class'. 117 118 root=subdir 119 The root directory used for deriving header guard CPP variable. 120 By default, the header guard CPP variable is calculated as the relative 121 path to the directory that contains .git, .hg, or .svn. When this flag 122 is specified, the relative path is calculated from the specified 123 directory. If the specified directory does not exist, this flag is 124 ignored. 125 126 Examples: 127 Assuming that top/src/.git exists (and cwd=top/src), the header guard 128 CPP variables for top/src/chrome/browser/ui/browser.h are: 129 130 No flag => CHROME_BROWSER_UI_BROWSER_H_ 131 --root=chrome => BROWSER_UI_BROWSER_H_ 132 --root=chrome/browser => UI_BROWSER_H_ 133 --root=.. => SRC_CHROME_BROWSER_UI_BROWSER_H_ 134 135 linelength=digits 136 This is the allowed line length for the project. The default value is 137 80 characters. 138 139 Examples: 140 --linelength=120 141 142 extensions=extension,extension,... 143 The allowed file extensions that cpplint will check 144 145 Examples: 146 --extensions=hpp,cpp 147 148 headers=x,y,... 149 The header extensions that cpplint will treat as .h in checks. Values are 150 automatically added to --extensions list. 151 152 Examples: 153 --headers=hpp,hxx 154 --headers=hpp 155 156 cpplint.py supports per-directory configurations specified in CPPLINT.cfg 157 files. CPPLINT.cfg file can contain a number of key=value pairs. 158 Currently the following options are supported: 159 160 set noparent 161 filter=+filter1,-filter2,... 162 exclude_files=regex 163 linelength=80 164 root=subdir 165 headers=x,y,... 166 167 "set noparent" option prevents cpplint from traversing directory tree 168 upwards looking for more .cfg files in parent directories. This option 169 is usually placed in the top-level project directory. 170 171 The "filter" option is similar in function to --filter flag. It specifies 172 message filters in addition to the |_DEFAULT_FILTERS| and those specified 173 through --filter command-line flag. 174 175 "exclude_files" allows to specify a regular expression to be matched against 176 a file name. If the expression matches, the file is skipped and not run 177 through liner. 178 179 "linelength" allows to specify the allowed line length for the project. 180 181 The "root" option is similar in function to the --root flag (see example 182 above). Paths are relative to the directory of the CPPLINT.cfg. 183 184 The "headers" option is similar in function to the --headers flag 185 (see example above). 186 187 CPPLINT.cfg has an effect on files in the same directory and all 188 sub-directories, unless overridden by a nested configuration file. 189 190 Example file: 191 filter=-build/include_order,+build/include_alpha 192 exclude_files=.*\.cc 193 194 The above example disables build/include_order warning and enables 195 build/include_alpha as well as excludes all .cc from being 196 processed by linter, in the current directory (where the .cfg 197 file is located) and all sub-directories. 198""" 199 200# We categorize each error message we print. Here are the categories. 201# We want an explicit list so we can list them all in cpplint --filter=. 202# If you add a new error message with a new category, add it to the list 203# here! cpplint_unittest.py should tell you if you forget to do this. 204_ERROR_CATEGORIES = [ 205 'build/class', 206 'build/c++11', 207 'build/c++14', 208 'build/c++tr1', 209 'build/deprecated', 210 'build/endif_comment', 211 'build/explicit_make_pair', 212 'build/forward_decl', 213 'build/header_guard', 214 'build/include', 215 'build/include_alpha', 216 'build/include_order', 217 'build/include_what_you_use', 218 'build/namespaces', 219 'build/printf_format', 220 'build/storage_class', 221 'legal/copyright', 222 'readability/alt_tokens', 223 'readability/braces', 224 'readability/casting', 225 'readability/check', 226 'readability/constructors', 227 'readability/fn_size', 228 'readability/inheritance', 229 'readability/multiline_comment', 230 'readability/multiline_string', 231 'readability/namespace', 232 'readability/nolint', 233 'readability/nul', 234 'readability/strings', 235 'readability/todo', 236 'readability/utf8', 237 'runtime/arrays', 238 'runtime/casting', 239 'runtime/explicit', 240 'runtime/int', 241 'runtime/init', 242 'runtime/invalid_increment', 243 'runtime/member_string_references', 244 'runtime/memset', 245 'runtime/indentation_namespace', 246 'runtime/operator', 247 'runtime/printf', 248 'runtime/printf_format', 249 'runtime/references', 250 'runtime/string', 251 'runtime/threadsafe_fn', 252 'runtime/vlog', 253 'whitespace/blank_line', 254 'whitespace/braces', 255 'whitespace/comma', 256 'whitespace/comments', 257 'whitespace/empty_conditional_body', 258 'whitespace/empty_if_body', 259 'whitespace/empty_loop_body', 260 'whitespace/end_of_line', 261 'whitespace/ending_newline', 262 'whitespace/forcolon', 263 'whitespace/indent', 264 'whitespace/line_length', 265 'whitespace/newline', 266 'whitespace/operators', 267 'whitespace/parens', 268 'whitespace/semicolon', 269 'whitespace/tab', 270 'whitespace/todo', 271 ] 272 273# These error categories are no longer enforced by cpplint, but for backwards- 274# compatibility they may still appear in NOLINT comments. 275_LEGACY_ERROR_CATEGORIES = [ 276 'readability/streams', 277 'readability/function', 278 ] 279 280# The default state of the category filter. This is overridden by the --filter= 281# flag. By default all errors are on, so only add here categories that should be 282# off by default (i.e., categories that must be enabled by the --filter= flags). 283# All entries here should start with a '-' or '+', as in the --filter= flag. 284_DEFAULT_FILTERS = ['-build/include_alpha'] 285 286# The default list of categories suppressed for C (not C++) files. 287_DEFAULT_C_SUPPRESSED_CATEGORIES = [ 288 'readability/casting', 289 ] 290 291# The default list of categories suppressed for Linux Kernel files. 292_DEFAULT_KERNEL_SUPPRESSED_CATEGORIES = [ 293 'whitespace/tab', 294 ] 295 296# We used to check for high-bit characters, but after much discussion we 297# decided those were OK, as long as they were in UTF-8 and didn't represent 298# hard-coded international strings, which belong in a separate i18n file. 299 300# C++ headers 301_CPP_HEADERS = frozenset([ 302 # Legacy 303 'algobase.h', 304 'algo.h', 305 'alloc.h', 306 'builtinbuf.h', 307 'bvector.h', 308 'complex.h', 309 'defalloc.h', 310 'deque.h', 311 'editbuf.h', 312 'fstream.h', 313 'function.h', 314 'hash_map', 315 'hash_map.h', 316 'hash_set', 317 'hash_set.h', 318 'hashtable.h', 319 'heap.h', 320 'indstream.h', 321 'iomanip.h', 322 'iostream.h', 323 'istream.h', 324 'iterator.h', 325 'list.h', 326 'map.h', 327 'multimap.h', 328 'multiset.h', 329 'ostream.h', 330 'pair.h', 331 'parsestream.h', 332 'pfstream.h', 333 'procbuf.h', 334 'pthread_alloc', 335 'pthread_alloc.h', 336 'rope', 337 'rope.h', 338 'ropeimpl.h', 339 'set.h', 340 'slist', 341 'slist.h', 342 'stack.h', 343 'stdiostream.h', 344 'stl_alloc.h', 345 'stl_relops.h', 346 'streambuf.h', 347 'stream.h', 348 'strfile.h', 349 'strstream.h', 350 'tempbuf.h', 351 'tree.h', 352 'type_traits.h', 353 'vector.h', 354 # 17.6.1.2 C++ library headers 355 'algorithm', 356 'array', 357 'atomic', 358 'bitset', 359 'chrono', 360 'codecvt', 361 'complex', 362 'condition_variable', 363 'deque', 364 'exception', 365 'forward_list', 366 'fstream', 367 'functional', 368 'future', 369 'initializer_list', 370 'iomanip', 371 'ios', 372 'iosfwd', 373 'iostream', 374 'istream', 375 'iterator', 376 'limits', 377 'list', 378 'locale', 379 'map', 380 'memory', 381 'mutex', 382 'new', 383 'numeric', 384 'ostream', 385 'queue', 386 'random', 387 'ratio', 388 'regex', 389 'scoped_allocator', 390 'set', 391 'sstream', 392 'stack', 393 'stdexcept', 394 'streambuf', 395 'string', 396 'strstream', 397 'system_error', 398 'thread', 399 'tuple', 400 'typeindex', 401 'typeinfo', 402 'type_traits', 403 'unordered_map', 404 'unordered_set', 405 'utility', 406 'valarray', 407 'vector', 408 # 17.6.1.2 C++ headers for C library facilities 409 'cassert', 410 'ccomplex', 411 'cctype', 412 'cerrno', 413 'cfenv', 414 'cfloat', 415 'cinttypes', 416 'ciso646', 417 'climits', 418 'clocale', 419 'cmath', 420 'csetjmp', 421 'csignal', 422 'cstdalign', 423 'cstdarg', 424 'cstdbool', 425 'cstddef', 426 'cstdint', 427 'cstdio', 428 'cstdlib', 429 'cstring', 430 'ctgmath', 431 'ctime', 432 'cuchar', 433 'cwchar', 434 'cwctype', 435 ]) 436 437# Type names 438_TYPES = re.compile( 439 r'^(?:' 440 # [dcl.type.simple] 441 r'(char(16_t|32_t)?)|wchar_t|' 442 r'bool|short|int|long|signed|unsigned|float|double|' 443 # [support.types] 444 r'(ptrdiff_t|size_t|max_align_t|nullptr_t)|' 445 # [cstdint.syn] 446 r'(u?int(_fast|_least)?(8|16|32|64)_t)|' 447 r'(u?int(max|ptr)_t)|' 448 r')$') 449 450 451# These headers are excluded from [build/include] and [build/include_order] 452# checks: 453# - Anything not following google file name conventions (containing an 454# uppercase character, such as Python.h or nsStringAPI.h, for example). 455# - Lua headers. 456_THIRD_PARTY_HEADERS_PATTERN = re.compile( 457 r'^(?:[^/]*[A-Z][^/]*\.h|lua\.h|lauxlib\.h|lualib\.h)$') 458 459# Pattern for matching FileInfo.BaseName() against test file name 460_TEST_FILE_SUFFIX = r'(_test|_unittest|_regtest)$' 461 462# Pattern that matches only complete whitespace, possibly across multiple lines. 463_EMPTY_CONDITIONAL_BODY_PATTERN = re.compile(r'^\s*$', re.DOTALL) 464 465# Assertion macros. These are defined in base/logging.h and 466# testing/base/public/gunit.h. 467_CHECK_MACROS = [ 468 'DCHECK', 'CHECK', 469 'EXPECT_TRUE', 'ASSERT_TRUE', 470 'EXPECT_FALSE', 'ASSERT_FALSE', 471 ] 472 473# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE 474_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS]) 475 476for op, replacement in [('==', 'EQ'), ('!=', 'NE'), 477 ('>=', 'GE'), ('>', 'GT'), 478 ('<=', 'LE'), ('<', 'LT')]: 479 _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement 480 _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement 481 _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement 482 _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement 483 484for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'), 485 ('>=', 'LT'), ('>', 'LE'), 486 ('<=', 'GT'), ('<', 'GE')]: 487 _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement 488 _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement 489 490# Alternative tokens and their replacements. For full list, see section 2.5 491# Alternative tokens [lex.digraph] in the C++ standard. 492# 493# Digraphs (such as '%:') are not included here since it's a mess to 494# match those on a word boundary. 495_ALT_TOKEN_REPLACEMENT = { 496 'and': '&&', 497 'bitor': '|', 498 'or': '||', 499 'xor': '^', 500 'compl': '~', 501 'bitand': '&', 502 'and_eq': '&=', 503 'or_eq': '|=', 504 'xor_eq': '^=', 505 'not': '!', 506 'not_eq': '!=' 507 } 508 509# Compile regular expression that matches all the above keywords. The "[ =()]" 510# bit is meant to avoid matching these keywords outside of boolean expressions. 511# 512# False positives include C-style multi-line comments and multi-line strings 513# but those have always been troublesome for cpplint. 514_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile( 515 r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)') 516 517 518# These constants define types of headers for use with 519# _IncludeState.CheckNextIncludeOrder(). 520_C_SYS_HEADER = 1 521_CPP_SYS_HEADER = 2 522_LIKELY_MY_HEADER = 3 523_POSSIBLE_MY_HEADER = 4 524_OTHER_HEADER = 5 525 526# These constants define the current inline assembly state 527_NO_ASM = 0 # Outside of inline assembly block 528_INSIDE_ASM = 1 # Inside inline assembly block 529_END_ASM = 2 # Last line of inline assembly block 530_BLOCK_ASM = 3 # The whole block is an inline assembly block 531 532# Match start of assembly blocks 533_MATCH_ASM = re.compile(r'^\s*(?:asm|_asm|__asm|__asm__)' 534 r'(?:\s+(volatile|__volatile__))?' 535 r'\s*[{(]') 536 537# Match strings that indicate we're working on a C (not C++) file. 538_SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|' 539 r'vim?:\s*.*(\s*|:)filetype=c(\s*|:|$))') 540 541# Match string that indicates we're working on a Linux Kernel file. 542_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)') 543 544_regexp_compile_cache = {} 545 546# {str, set(int)}: a map from error categories to sets of linenumbers 547# on which those errors are expected and should be suppressed. 548_error_suppressions = {} 549 550# The root directory used for deriving header guard CPP variable. 551# This is set by --root flag. 552_root = None 553_root_debug = False 554 555# The allowed line length of files. 556# This is set by --linelength flag. 557_line_length = 80 558 559# The allowed extensions for file names 560# This is set by --extensions flag. 561_valid_extensions = set(['cc', 'h', 'cpp', 'cu', 'cuh']) 562 563# Treat all headers starting with 'h' equally: .h, .hpp, .hxx etc. 564# This is set by --headers flag. 565_hpp_headers = set(['h']) 566 567# {str, bool}: a map from error categories to booleans which indicate if the 568# category should be suppressed for every line. 569_global_error_suppressions = {} 570 571def ProcessHppHeadersOption(val): 572 global _hpp_headers 573 try: 574 _hpp_headers = set(val.split(',')) 575 # Automatically append to extensions list so it does not have to be set 2 times 576 _valid_extensions.update(_hpp_headers) 577 except ValueError: 578 PrintUsage('Header extensions must be comma separated list.') 579 580def IsHeaderExtension(file_extension): 581 return file_extension in _hpp_headers 582 583def ParseNolintSuppressions(filename, raw_line, linenum, error): 584 """Updates the global list of line error-suppressions. 585 586 Parses any NOLINT comments on the current line, updating the global 587 error_suppressions store. Reports an error if the NOLINT comment 588 was malformed. 589 590 Args: 591 filename: str, the name of the input file. 592 raw_line: str, the line of input text, with comments. 593 linenum: int, the number of the current line. 594 error: function, an error handler. 595 """ 596 matched = Search(r'\bNOLINT(NEXTLINE)?\b(\([^)]+\))?', raw_line) 597 if matched: 598 if matched.group(1): 599 suppressed_line = linenum + 1 600 else: 601 suppressed_line = linenum 602 category = matched.group(2) 603 if category in (None, '(*)'): # => "suppress all" 604 _error_suppressions.setdefault(None, set()).add(suppressed_line) 605 else: 606 if category.startswith('(') and category.endswith(')'): 607 category = category[1:-1] 608 if category in _ERROR_CATEGORIES: 609 _error_suppressions.setdefault(category, set()).add(suppressed_line) 610 elif category not in _LEGACY_ERROR_CATEGORIES: 611 error(filename, linenum, 'readability/nolint', 5, 612 'Unknown NOLINT error category: %s' % category) 613 614 615def ProcessGlobalSuppresions(lines): 616 """Updates the list of global error suppressions. 617 618 Parses any lint directives in the file that have global effect. 619 620 Args: 621 lines: An array of strings, each representing a line of the file, with the 622 last element being empty if the file is terminated with a newline. 623 """ 624 for line in lines: 625 if _SEARCH_C_FILE.search(line): 626 for category in _DEFAULT_C_SUPPRESSED_CATEGORIES: 627 _global_error_suppressions[category] = True 628 if _SEARCH_KERNEL_FILE.search(line): 629 for category in _DEFAULT_KERNEL_SUPPRESSED_CATEGORIES: 630 _global_error_suppressions[category] = True 631 632 633def ResetNolintSuppressions(): 634 """Resets the set of NOLINT suppressions to empty.""" 635 _error_suppressions.clear() 636 _global_error_suppressions.clear() 637 638 639def IsErrorSuppressedByNolint(category, linenum): 640 """Returns true if the specified error category is suppressed on this line. 641 642 Consults the global error_suppressions map populated by 643 ParseNolintSuppressions/ProcessGlobalSuppresions/ResetNolintSuppressions. 644 645 Args: 646 category: str, the category of the error. 647 linenum: int, the current line number. 648 Returns: 649 bool, True iff the error should be suppressed due to a NOLINT comment or 650 global suppression. 651 """ 652 return (_global_error_suppressions.get(category, False) or 653 linenum in _error_suppressions.get(category, set()) or 654 linenum in _error_suppressions.get(None, set())) 655 656 657def Match(pattern, s): 658 """Matches the string with the pattern, caching the compiled regexp.""" 659 # The regexp compilation caching is inlined in both Match and Search for 660 # performance reasons; factoring it out into a separate function turns out 661 # to be noticeably expensive. 662 if pattern not in _regexp_compile_cache: 663 _regexp_compile_cache[pattern] = sre_compile.compile(pattern) 664 return _regexp_compile_cache[pattern].match(s) 665 666 667def ReplaceAll(pattern, rep, s): 668 """Replaces instances of pattern in a string with a replacement. 669 670 The compiled regex is kept in a cache shared by Match and Search. 671 672 Args: 673 pattern: regex pattern 674 rep: replacement text 675 s: search string 676 677 Returns: 678 string with replacements made (or original string if no replacements) 679 """ 680 if pattern not in _regexp_compile_cache: 681 _regexp_compile_cache[pattern] = sre_compile.compile(pattern) 682 return _regexp_compile_cache[pattern].sub(rep, s) 683 684 685def Search(pattern, s): 686 """Searches the string for the pattern, caching the compiled regexp.""" 687 if pattern not in _regexp_compile_cache: 688 _regexp_compile_cache[pattern] = sre_compile.compile(pattern) 689 return _regexp_compile_cache[pattern].search(s) 690 691 692def _IsSourceExtension(s): 693 """File extension (excluding dot) matches a source file extension.""" 694 return s in ('c', 'cc', 'cpp', 'cxx') 695 696 697class _IncludeState(object): 698 """Tracks line numbers for includes, and the order in which includes appear. 699 700 include_list contains list of lists of (header, line number) pairs. 701 It's a lists of lists rather than just one flat list to make it 702 easier to update across preprocessor boundaries. 703 704 Call CheckNextIncludeOrder() once for each header in the file, passing 705 in the type constants defined above. Calls in an illegal order will 706 raise an _IncludeError with an appropriate error message. 707 708 """ 709 # self._section will move monotonically through this set. If it ever 710 # needs to move backwards, CheckNextIncludeOrder will raise an error. 711 _INITIAL_SECTION = 0 712 _MY_H_SECTION = 1 713 _C_SECTION = 2 714 _CPP_SECTION = 3 715 _OTHER_H_SECTION = 4 716 717 _TYPE_NAMES = { 718 _C_SYS_HEADER: 'C system header', 719 _CPP_SYS_HEADER: 'C++ system header', 720 _LIKELY_MY_HEADER: 'header this file implements', 721 _POSSIBLE_MY_HEADER: 'header this file may implement', 722 _OTHER_HEADER: 'other header', 723 } 724 _SECTION_NAMES = { 725 _INITIAL_SECTION: "... nothing. (This can't be an error.)", 726 _MY_H_SECTION: 'a header this file implements', 727 _C_SECTION: 'C system header', 728 _CPP_SECTION: 'C++ system header', 729 _OTHER_H_SECTION: 'other header', 730 } 731 732 def __init__(self): 733 self.include_list = [[]] 734 self.ResetSection('') 735 736 def FindHeader(self, header): 737 """Check if a header has already been included. 738 739 Args: 740 header: header to check. 741 Returns: 742 Line number of previous occurrence, or -1 if the header has not 743 been seen before. 744 """ 745 for section_list in self.include_list: 746 for f in section_list: 747 if f[0] == header: 748 return f[1] 749 return -1 750 751 def ResetSection(self, directive): 752 """Reset section checking for preprocessor directive. 753 754 Args: 755 directive: preprocessor directive (e.g. "if", "else"). 756 """ 757 # The name of the current section. 758 self._section = self._INITIAL_SECTION 759 # The path of last found header. 760 self._last_header = '' 761 762 # Update list of includes. Note that we never pop from the 763 # include list. 764 if directive in ('if', 'ifdef', 'ifndef'): 765 self.include_list.append([]) 766 elif directive in ('else', 'elif'): 767 self.include_list[-1] = [] 768 769 def SetLastHeader(self, header_path): 770 self._last_header = header_path 771 772 def CanonicalizeAlphabeticalOrder(self, header_path): 773 """Returns a path canonicalized for alphabetical comparison. 774 775 - replaces "-" with "_" so they both cmp the same. 776 - removes '-inl' since we don't require them to be after the main header. 777 - lowercase everything, just in case. 778 779 Args: 780 header_path: Path to be canonicalized. 781 782 Returns: 783 Canonicalized path. 784 """ 785 return header_path.replace('-inl.h', '.h').replace('-', '_').lower() 786 787 def IsInAlphabeticalOrder(self, clean_lines, linenum, header_path): 788 """Check if a header is in alphabetical order with the previous header. 789 790 Args: 791 clean_lines: A CleansedLines instance containing the file. 792 linenum: The number of the line to check. 793 header_path: Canonicalized header to be checked. 794 795 Returns: 796 Returns true if the header is in alphabetical order. 797 """ 798 # If previous section is different from current section, _last_header will 799 # be reset to empty string, so it's always less than current header. 800 # 801 # If previous line was a blank line, assume that the headers are 802 # intentionally sorted the way they are. 803 if (self._last_header > header_path and 804 Match(r'^\s*#\s*include\b', clean_lines.elided[linenum - 1])): 805 return False 806 return True 807 808 def CheckNextIncludeOrder(self, header_type): 809 """Returns a non-empty error message if the next header is out of order. 810 811 This function also updates the internal state to be ready to check 812 the next include. 813 814 Args: 815 header_type: One of the _XXX_HEADER constants defined above. 816 817 Returns: 818 The empty string if the header is in the right order, or an 819 error message describing what's wrong. 820 821 """ 822 error_message = ('Found %s after %s' % 823 (self._TYPE_NAMES[header_type], 824 self._SECTION_NAMES[self._section])) 825 826 last_section = self._section 827 828 if header_type == _C_SYS_HEADER: 829 if self._section <= self._C_SECTION: 830 self._section = self._C_SECTION 831 else: 832 self._last_header = '' 833 return error_message 834 elif header_type == _CPP_SYS_HEADER: 835 if self._section <= self._CPP_SECTION: 836 self._section = self._CPP_SECTION 837 else: 838 self._last_header = '' 839 return error_message 840 elif header_type == _LIKELY_MY_HEADER: 841 if self._section <= self._MY_H_SECTION: 842 self._section = self._MY_H_SECTION 843 else: 844 self._section = self._OTHER_H_SECTION 845 elif header_type == _POSSIBLE_MY_HEADER: 846 if self._section <= self._MY_H_SECTION: 847 self._section = self._MY_H_SECTION 848 else: 849 # This will always be the fallback because we're not sure 850 # enough that the header is associated with this file. 851 self._section = self._OTHER_H_SECTION 852 else: 853 assert header_type == _OTHER_HEADER 854 self._section = self._OTHER_H_SECTION 855 856 if last_section != self._section: 857 self._last_header = '' 858 859 return '' 860 861 862class _CppLintState(object): 863 """Maintains module-wide state..""" 864 865 def __init__(self): 866 self.verbose_level = 1 # global setting. 867 self.error_count = 0 # global count of reported errors 868 # filters to apply when emitting error messages 869 self.filters = _DEFAULT_FILTERS[:] 870 # backup of filter list. Used to restore the state after each file. 871 self._filters_backup = self.filters[:] 872 self.counting = 'total' # In what way are we counting errors? 873 self.errors_by_category = {} # string to int dict storing error counts 874 self.quiet = False # Suppress non-error messagess? 875 876 # output format: 877 # "emacs" - format that emacs can parse (default) 878 # "vs7" - format that Microsoft Visual Studio 7 can parse 879 self.output_format = 'emacs' 880 881 def SetOutputFormat(self, output_format): 882 """Sets the output format for errors.""" 883 self.output_format = output_format 884 885 def SetQuiet(self, quiet): 886 """Sets the module's quiet settings, and returns the previous setting.""" 887 last_quiet = self.quiet 888 self.quiet = quiet 889 return last_quiet 890 891 def SetVerboseLevel(self, level): 892 """Sets the module's verbosity, and returns the previous setting.""" 893 last_verbose_level = self.verbose_level 894 self.verbose_level = level 895 return last_verbose_level 896 897 def SetCountingStyle(self, counting_style): 898 """Sets the module's counting options.""" 899 self.counting = counting_style 900 901 def SetFilters(self, filters): 902 """Sets the error-message filters. 903 904 These filters are applied when deciding whether to emit a given 905 error message. 906 907 Args: 908 filters: A string of comma-separated filters (eg "+whitespace/indent"). 909 Each filter should start with + or -; else we die. 910 911 Raises: 912 ValueError: The comma-separated filters did not all start with '+' or '-'. 913 E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter" 914 """ 915 # Default filters always have less priority than the flag ones. 916 self.filters = _DEFAULT_FILTERS[:] 917 self.AddFilters(filters) 918 919 def AddFilters(self, filters): 920 """ Adds more filters to the existing list of error-message filters. """ 921 for filt in filters.split(','): 922 clean_filt = filt.strip() 923 if clean_filt: 924 self.filters.append(clean_filt) 925 for filt in self.filters: 926 if not (filt.startswith('+') or filt.startswith('-')): 927 raise ValueError('Every filter in --filters must start with + or -' 928 ' (%s does not)' % filt) 929 930 def BackupFilters(self): 931 """ Saves the current filter list to backup storage.""" 932 self._filters_backup = self.filters[:] 933 934 def RestoreFilters(self): 935 """ Restores filters previously backed up.""" 936 self.filters = self._filters_backup[:] 937 938 def ResetErrorCounts(self): 939 """Sets the module's error statistic back to zero.""" 940 self.error_count = 0 941 self.errors_by_category = {} 942 943 def IncrementErrorCount(self, category): 944 """Bumps the module's error statistic.""" 945 self.error_count += 1 946 if self.counting in ('toplevel', 'detailed'): 947 if self.counting != 'detailed': 948 category = category.split('/')[0] 949 if category not in self.errors_by_category: 950 self.errors_by_category[category] = 0 951 self.errors_by_category[category] += 1 952 953 def PrintErrorCounts(self): 954 """Print a summary of errors by category, and the total.""" 955 for category, count in self.errors_by_category.iteritems(): 956 sys.stderr.write('Category \'%s\' errors found: %d\n' % 957 (category, count)) 958 sys.stdout.write('Total errors found: %d\n' % self.error_count) 959 960_cpplint_state = _CppLintState() 961 962 963def _OutputFormat(): 964 """Gets the module's output format.""" 965 return _cpplint_state.output_format 966 967 968def _SetOutputFormat(output_format): 969 """Sets the module's output format.""" 970 _cpplint_state.SetOutputFormat(output_format) 971 972def _Quiet(): 973 """Return's the module's quiet setting.""" 974 return _cpplint_state.quiet 975 976def _SetQuiet(quiet): 977 """Set the module's quiet status, and return previous setting.""" 978 return _cpplint_state.SetQuiet(quiet) 979 980 981def _VerboseLevel(): 982 """Returns the module's verbosity setting.""" 983 return _cpplint_state.verbose_level 984 985 986def _SetVerboseLevel(level): 987 """Sets the module's verbosity, and returns the previous setting.""" 988 return _cpplint_state.SetVerboseLevel(level) 989 990 991def _SetCountingStyle(level): 992 """Sets the module's counting options.""" 993 _cpplint_state.SetCountingStyle(level) 994 995 996def _Filters(): 997 """Returns the module's list of output filters, as a list.""" 998 return _cpplint_state.filters 999 1000 1001def _SetFilters(filters): 1002 """Sets the module's error-message filters. 1003 1004 These filters are applied when deciding whether to emit a given 1005 error message. 1006 1007 Args: 1008 filters: A string of comma-separated filters (eg "whitespace/indent"). 1009 Each filter should start with + or -; else we die. 1010 """ 1011 _cpplint_state.SetFilters(filters) 1012 1013def _AddFilters(filters): 1014 """Adds more filter overrides. 1015 1016 Unlike _SetFilters, this function does not reset the current list of filters 1017 available. 1018 1019 Args: 1020 filters: A string of comma-separated filters (eg "whitespace/indent"). 1021 Each filter should start with + or -; else we die. 1022 """ 1023 _cpplint_state.AddFilters(filters) 1024 1025def _BackupFilters(): 1026 """ Saves the current filter list to backup storage.""" 1027 _cpplint_state.BackupFilters() 1028 1029def _RestoreFilters(): 1030 """ Restores filters previously backed up.""" 1031 _cpplint_state.RestoreFilters() 1032 1033class _FunctionState(object): 1034 """Tracks current function name and the number of lines in its body.""" 1035 1036 _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. 1037 _TEST_TRIGGER = 400 # about 50% more than _NORMAL_TRIGGER. 1038 1039 def __init__(self): 1040 self.in_a_function = False 1041 self.lines_in_function = 0 1042 self.current_function = '' 1043 1044 def Begin(self, function_name): 1045 """Start analyzing function body. 1046 1047 Args: 1048 function_name: The name of the function being tracked. 1049 """ 1050 self.in_a_function = True 1051 self.lines_in_function = 0 1052 self.current_function = function_name 1053 1054 def Count(self): 1055 """Count line in current function body.""" 1056 if self.in_a_function: 1057 self.lines_in_function += 1 1058 1059 def Check(self, error, filename, linenum): 1060 """Report if too many lines in function body. 1061 1062 Args: 1063 error: The function to call with any errors found. 1064 filename: The name of the current file. 1065 linenum: The number of the line to check. 1066 """ 1067 if not self.in_a_function: 1068 return 1069 1070 if Match(r'T(EST|est)', self.current_function): 1071 base_trigger = self._TEST_TRIGGER 1072 else: 1073 base_trigger = self._NORMAL_TRIGGER 1074 trigger = base_trigger * 2**_VerboseLevel() 1075 1076 if self.lines_in_function > trigger: 1077 error_level = int(math.log(self.lines_in_function / base_trigger, 2)) 1078 # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... 1079 if error_level > 5: 1080 error_level = 5 1081 error(filename, linenum, 'readability/fn_size', error_level, 1082 'Small and focused functions are preferred:' 1083 ' %s has %d non-comment lines' 1084 ' (error triggered by exceeding %d lines).' % ( 1085 self.current_function, self.lines_in_function, trigger)) 1086 1087 def End(self): 1088 """Stop analyzing function body.""" 1089 self.in_a_function = False 1090 1091 1092class _IncludeError(Exception): 1093 """Indicates a problem with the include order in a file.""" 1094 pass 1095 1096 1097class FileInfo(object): 1098 """Provides utility functions for filenames. 1099 1100 FileInfo provides easy access to the components of a file's path 1101 relative to the project root. 1102 """ 1103 1104 def __init__(self, filename): 1105 self._filename = filename 1106 1107 def FullName(self): 1108 """Make Windows paths like Unix.""" 1109 return os.path.abspath(self._filename).replace('\\', '/') 1110 1111 def RepositoryName(self): 1112 """FullName after removing the local path to the repository. 1113 1114 If we have a real absolute path name here we can try to do something smart: 1115 detecting the root of the checkout and truncating /path/to/checkout from 1116 the name so that we get header guards that don't include things like 1117 "C:\Documents and Settings\..." or "/home/username/..." in them and thus 1118 people on different computers who have checked the source out to different 1119 locations won't see bogus errors. 1120 """ 1121 fullname = self.FullName() 1122 1123 if os.path.exists(fullname): 1124 project_dir = os.path.dirname(fullname) 1125 1126 if os.path.exists(os.path.join(project_dir, ".svn")): 1127 # If there's a .svn file in the current directory, we recursively look 1128 # up the directory tree for the top of the SVN checkout 1129 root_dir = project_dir 1130 one_up_dir = os.path.dirname(root_dir) 1131 while os.path.exists(os.path.join(one_up_dir, ".svn")): 1132 root_dir = os.path.dirname(root_dir) 1133 one_up_dir = os.path.dirname(one_up_dir) 1134 1135 prefix = os.path.commonprefix([root_dir, project_dir]) 1136 return fullname[len(prefix) + 1:] 1137 1138 # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by 1139 # searching up from the current path. 1140 root_dir = current_dir = os.path.dirname(fullname) 1141 while current_dir != os.path.dirname(current_dir): 1142 if (os.path.exists(os.path.join(current_dir, ".git")) or 1143 os.path.exists(os.path.join(current_dir, ".hg")) or 1144 os.path.exists(os.path.join(current_dir, ".svn"))): 1145 root_dir = current_dir 1146 current_dir = os.path.dirname(current_dir) 1147 1148 if (os.path.exists(os.path.join(root_dir, ".git")) or 1149 os.path.exists(os.path.join(root_dir, ".hg")) or 1150 os.path.exists(os.path.join(root_dir, ".svn"))): 1151 prefix = os.path.commonprefix([root_dir, project_dir]) 1152 return fullname[len(prefix) + 1:] 1153 1154 # Don't know what to do; header guard warnings may be wrong... 1155 return fullname 1156 1157 def Split(self): 1158 """Splits the file into the directory, basename, and extension. 1159 1160 For 'chrome/browser/browser.cc', Split() would 1161 return ('chrome/browser', 'browser', '.cc') 1162 1163 Returns: 1164 A tuple of (directory, basename, extension). 1165 """ 1166 1167 googlename = self.RepositoryName() 1168 project, rest = os.path.split(googlename) 1169 return (project,) + os.path.splitext(rest) 1170 1171 def BaseName(self): 1172 """File base name - text after the final slash, before the final period.""" 1173 return self.Split()[1] 1174 1175 def Extension(self): 1176 """File extension - text following the final period.""" 1177 return self.Split()[2] 1178 1179 def NoExtension(self): 1180 """File has no source file extension.""" 1181 return '/'.join(self.Split()[0:2]) 1182 1183 def IsSource(self): 1184 """File has a source file extension.""" 1185 return _IsSourceExtension(self.Extension()[1:]) 1186 1187 1188def _ShouldPrintError(category, confidence, linenum): 1189 """If confidence >= verbose, category passes filter and is not suppressed.""" 1190 1191 # There are three ways we might decide not to print an error message: 1192 # a "NOLINT(category)" comment appears in the source, 1193 # the verbosity level isn't high enough, or the filters filter it out. 1194 if IsErrorSuppressedByNolint(category, linenum): 1195 return False 1196 1197 if confidence < _cpplint_state.verbose_level: 1198 return False 1199 1200 is_filtered = False 1201 for one_filter in _Filters(): 1202 if one_filter.startswith('-'): 1203 if category.startswith(one_filter[1:]): 1204 is_filtered = True 1205 elif one_filter.startswith('+'): 1206 if category.startswith(one_filter[1:]): 1207 is_filtered = False 1208 else: 1209 assert False # should have been checked for in SetFilter. 1210 if is_filtered: 1211 return False 1212 1213 return True 1214 1215 1216def Error(filename, linenum, category, confidence, message): 1217 """Logs the fact we've found a lint error. 1218 1219 We log where the error was found, and also our confidence in the error, 1220 that is, how certain we are this is a legitimate style regression, and 1221 not a misidentification or a use that's sometimes justified. 1222 1223 False positives can be suppressed by the use of 1224 "cpplint(category)" comments on the offending line. These are 1225 parsed into _error_suppressions. 1226 1227 Args: 1228 filename: The name of the file containing the error. 1229 linenum: The number of the line containing the error. 1230 category: A string used to describe the "category" this bug 1231 falls under: "whitespace", say, or "runtime". Categories 1232 may have a hierarchy separated by slashes: "whitespace/indent". 1233 confidence: A number from 1-5 representing a confidence score for 1234 the error, with 5 meaning that we are certain of the problem, 1235 and 1 meaning that it could be a legitimate construct. 1236 message: The error message. 1237 """ 1238 if _ShouldPrintError(category, confidence, linenum): 1239 _cpplint_state.IncrementErrorCount(category) 1240 if _cpplint_state.output_format == 'vs7': 1241 sys.stderr.write('%s(%s): error cpplint: [%s] %s [%d]\n' % ( 1242 filename, linenum, category, message, confidence)) 1243 elif _cpplint_state.output_format == 'eclipse': 1244 sys.stderr.write('%s:%s: warning: %s [%s] [%d]\n' % ( 1245 filename, linenum, message, category, confidence)) 1246 else: 1247 sys.stderr.write('%s:%s: %s [%s] [%d]\n' % ( 1248 filename, linenum, message, category, confidence)) 1249 1250 1251# Matches standard C++ escape sequences per 2.13.2.3 of the C++ standard. 1252_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile( 1253 r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)') 1254# Match a single C style comment on the same line. 1255_RE_PATTERN_C_COMMENTS = r'/\*(?:[^*]|\*(?!/))*\*/' 1256# Matches multi-line C style comments. 1257# This RE is a little bit more complicated than one might expect, because we 1258# have to take care of space removals tools so we can handle comments inside 1259# statements better. 1260# The current rule is: We only clear spaces from both sides when we're at the 1261# end of the line. Otherwise, we try to remove spaces from the right side, 1262# if this doesn't work we try on left side but only if there's a non-character 1263# on the right. 1264_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile( 1265 r'(\s*' + _RE_PATTERN_C_COMMENTS + r'\s*$|' + 1266 _RE_PATTERN_C_COMMENTS + r'\s+|' + 1267 r'\s+' + _RE_PATTERN_C_COMMENTS + r'(?=\W)|' + 1268 _RE_PATTERN_C_COMMENTS + r')') 1269 1270 1271def IsCppString(line): 1272 """Does line terminate so, that the next symbol is in string constant. 1273 1274 This function does not consider single-line nor multi-line comments. 1275 1276 Args: 1277 line: is a partial line of code starting from the 0..n. 1278 1279 Returns: 1280 True, if next character appended to 'line' is inside a 1281 string constant. 1282 """ 1283 1284 line = line.replace(r'\\', 'XX') # after this, \\" does not match to \" 1285 return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1 1286 1287 1288def CleanseRawStrings(raw_lines): 1289 """Removes C++11 raw strings from lines. 1290 1291 Before: 1292 static const char kData[] = R"( 1293 multi-line string 1294 )"; 1295 1296 After: 1297 static const char kData[] = "" 1298 (replaced by blank line) 1299 ""; 1300 1301 Args: 1302 raw_lines: list of raw lines. 1303 1304 Returns: 1305 list of lines with C++11 raw strings replaced by empty strings. 1306 """ 1307 1308 delimiter = None 1309 lines_without_raw_strings = [] 1310 for line in raw_lines: 1311 if delimiter: 1312 # Inside a raw string, look for the end 1313 end = line.find(delimiter) 1314 if end >= 0: 1315 # Found the end of the string, match leading space for this 1316 # line and resume copying the original lines, and also insert 1317 # a "" on the last line. 1318 leading_space = Match(r'^(\s*)\S', line) 1319 line = leading_space.group(1) + '""' + line[end + len(delimiter):] 1320 delimiter = None 1321 else: 1322 # Haven't found the end yet, append a blank line. 1323 line = '""' 1324 1325 # Look for beginning of a raw string, and replace them with 1326 # empty strings. This is done in a loop to handle multiple raw 1327 # strings on the same line. 1328 while delimiter is None: 1329 # Look for beginning of a raw string. 1330 # See 2.14.15 [lex.string] for syntax. 1331 # 1332 # Once we have matched a raw string, we check the prefix of the 1333 # line to make sure that the line is not part of a single line 1334 # comment. It's done this way because we remove raw strings 1335 # before removing comments as opposed to removing comments 1336 # before removing raw strings. This is because there are some 1337 # cpplint checks that requires the comments to be preserved, but 1338 # we don't want to check comments that are inside raw strings. 1339 matched = Match(r'^(.*?)\b(?:R|u8R|uR|UR|LR)"([^\s\\()]*)\((.*)$', line) 1340 if (matched and 1341 not Match(r'^([^\'"]|\'(\\.|[^\'])*\'|"(\\.|[^"])*")*//', 1342 matched.group(1))): 1343 delimiter = ')' + matched.group(2) + '"' 1344 1345 end = matched.group(3).find(delimiter) 1346 if end >= 0: 1347 # Raw string ended on same line 1348 line = (matched.group(1) + '""' + 1349 matched.group(3)[end + len(delimiter):]) 1350 delimiter = None 1351 else: 1352 # Start of a multi-line raw string 1353 line = matched.group(1) + '""' 1354 else: 1355 break 1356 1357 lines_without_raw_strings.append(line) 1358 1359 # TODO(unknown): if delimiter is not None here, we might want to 1360 # emit a warning for unterminated string. 1361 return lines_without_raw_strings 1362 1363 1364def FindNextMultiLineCommentStart(lines, lineix): 1365 """Find the beginning marker for a multiline comment.""" 1366 while lineix < len(lines): 1367 if lines[lineix].strip().startswith('/*'): 1368 # Only return this marker if the comment goes beyond this line 1369 if lines[lineix].strip().find('*/', 2) < 0: 1370 return lineix 1371 lineix += 1 1372 return len(lines) 1373 1374 1375def FindNextMultiLineCommentEnd(lines, lineix): 1376 """We are inside a comment, find the end marker.""" 1377 while lineix < len(lines): 1378 if lines[lineix].strip().endswith('*/'): 1379 return lineix 1380 lineix += 1 1381 return len(lines) 1382 1383 1384def RemoveMultiLineCommentsFromRange(lines, begin, end): 1385 """Clears a range of lines for multi-line comments.""" 1386 # Having // dummy comments makes the lines non-empty, so we will not get 1387 # unnecessary blank line warnings later in the code. 1388 for i in range(begin, end): 1389 lines[i] = '/**/' 1390 1391 1392def RemoveMultiLineComments(filename, lines, error): 1393 """Removes multiline (c-style) comments from lines.""" 1394 lineix = 0 1395 while lineix < len(lines): 1396 lineix_begin = FindNextMultiLineCommentStart(lines, lineix) 1397 if lineix_begin >= len(lines): 1398 return 1399 lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin) 1400 if lineix_end >= len(lines): 1401 error(filename, lineix_begin + 1, 'readability/multiline_comment', 5, 1402 'Could not find end of multi-line comment') 1403 return 1404 RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1) 1405 lineix = lineix_end + 1 1406 1407 1408def CleanseComments(line): 1409 """Removes //-comments and single-line C-style /* */ comments. 1410 1411 Args: 1412 line: A line of C++ source. 1413 1414 Returns: 1415 The line with single-line comments removed. 1416 """ 1417 commentpos = line.find('//') 1418 if commentpos != -1 and not IsCppString(line[:commentpos]): 1419 line = line[:commentpos].rstrip() 1420 # get rid of /* ... */ 1421 return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line) 1422 1423 1424class CleansedLines(object): 1425 """Holds 4 copies of all lines with different preprocessing applied to them. 1426 1427 1) elided member contains lines without strings and comments. 1428 2) lines member contains lines without comments. 1429 3) raw_lines member contains all the lines without processing. 1430 4) lines_without_raw_strings member is same as raw_lines, but with C++11 raw 1431 strings removed. 1432 All these members are of <type 'list'>, and of the same length. 1433 """ 1434 1435 def __init__(self, lines): 1436 self.elided = [] 1437 self.lines = [] 1438 self.raw_lines = lines 1439 self.num_lines = len(lines) 1440 self.lines_without_raw_strings = CleanseRawStrings(lines) 1441 for linenum in range(len(self.lines_without_raw_strings)): 1442 self.lines.append(CleanseComments( 1443 self.lines_without_raw_strings[linenum])) 1444 elided = self._CollapseStrings(self.lines_without_raw_strings[linenum]) 1445 self.elided.append(CleanseComments(elided)) 1446 1447 def NumLines(self): 1448 """Returns the number of lines represented.""" 1449 return self.num_lines 1450 1451 @staticmethod 1452 def _CollapseStrings(elided): 1453 """Collapses strings and chars on a line to simple "" or '' blocks. 1454 1455 We nix strings first so we're not fooled by text like '"http://"' 1456 1457 Args: 1458 elided: The line being processed. 1459 1460 Returns: 1461 The line with collapsed strings. 1462 """ 1463 if _RE_PATTERN_INCLUDE.match(elided): 1464 return elided 1465 1466 # Remove escaped characters first to make quote/single quote collapsing 1467 # basic. Things that look like escaped characters shouldn't occur 1468 # outside of strings and chars. 1469 elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided) 1470 1471 # Replace quoted strings and digit separators. Both single quotes 1472 # and double quotes are processed in the same loop, otherwise 1473 # nested quotes wouldn't work. 1474 collapsed = '' 1475 while True: 1476 # Find the first quote character 1477 match = Match(r'^([^\'"]*)([\'"])(.*)$', elided) 1478 if not match: 1479 collapsed += elided 1480 break 1481 head, quote, tail = match.groups() 1482 1483 if quote == '"': 1484 # Collapse double quoted strings 1485 second_quote = tail.find('"') 1486 if second_quote >= 0: 1487 collapsed += head + '""' 1488 elided = tail[second_quote + 1:] 1489 else: 1490 # Unmatched double quote, don't bother processing the rest 1491 # of the line since this is probably a multiline string. 1492 collapsed += elided 1493 break 1494 else: 1495 # Found single quote, check nearby text to eliminate digit separators. 1496 # 1497 # There is no special handling for floating point here, because 1498 # the integer/fractional/exponent parts would all be parsed 1499 # correctly as long as there are digits on both sides of the 1500 # separator. So we are fine as long as we don't see something 1501 # like "0.'3" (gcc 4.9.0 will not allow this literal). 1502 if Search(r'\b(?:0[bBxX]?|[1-9])[0-9a-fA-F]*$', head): 1503 match_literal = Match(r'^((?:\'?[0-9a-zA-Z_])*)(.*)$', "'" + tail) 1504 collapsed += head + match_literal.group(1).replace("'", '') 1505 elided = match_literal.group(2) 1506 else: 1507 second_quote = tail.find('\'') 1508 if second_quote >= 0: 1509 collapsed += head + "''" 1510 elided = tail[second_quote + 1:] 1511 else: 1512 # Unmatched single quote 1513 collapsed += elided 1514 break 1515 1516 return collapsed 1517 1518 1519def FindEndOfExpressionInLine(line, startpos, stack): 1520 """Find the position just after the end of current parenthesized expression. 1521 1522 Args: 1523 line: a CleansedLines line. 1524 startpos: start searching at this position. 1525 stack: nesting stack at startpos. 1526 1527 Returns: 1528 On finding matching end: (index just after matching end, None) 1529 On finding an unclosed expression: (-1, None) 1530 Otherwise: (-1, new stack at end of this line) 1531 """ 1532 for i in xrange(startpos, len(line)): 1533 char = line[i] 1534 if char in '([{': 1535 # Found start of parenthesized expression, push to expression stack 1536 stack.append(char) 1537 elif char == '<': 1538 # Found potential start of template argument list 1539 if i > 0 and line[i - 1] == '<': 1540 # Left shift operator 1541 if stack and stack[-1] == '<': 1542 stack.pop() 1543 if not stack: 1544 return (-1, None) 1545 elif i > 0 and Search(r'\boperator\s*$', line[0:i]): 1546 # operator<, don't add to stack 1547 continue 1548 else: 1549 # Tentative start of template argument list 1550 stack.append('<') 1551 elif char in ')]}': 1552 # Found end of parenthesized expression. 1553 # 1554 # If we are currently expecting a matching '>', the pending '<' 1555 # must have been an operator. Remove them from expression stack. 1556 while stack and stack[-1] == '<': 1557 stack.pop() 1558 if not stack: 1559 return (-1, None) 1560 if ((stack[-1] == '(' and char == ')') or 1561 (stack[-1] == '[' and char == ']') or 1562 (stack[-1] == '{' and char == '}')): 1563 stack.pop() 1564 if not stack: 1565 return (i + 1, None) 1566 else: 1567 # Mismatched parentheses 1568 return (-1, None) 1569 elif char == '>': 1570 # Found potential end of template argument list. 1571 1572 # Ignore "->" and operator functions 1573 if (i > 0 and 1574 (line[i - 1] == '-' or Search(r'\boperator\s*$', line[0:i - 1]))): 1575 continue 1576 1577 # Pop the stack if there is a matching '<'. Otherwise, ignore 1578 # this '>' since it must be an operator. 1579 if stack: 1580 if stack[-1] == '<': 1581 stack.pop() 1582 if not stack: 1583 return (i + 1, None) 1584 elif char == ';': 1585 # Found something that look like end of statements. If we are currently 1586 # expecting a '>', the matching '<' must have been an operator, since 1587 # template argument list should not contain statements. 1588 while stack and stack[-1] == '<': 1589 stack.pop() 1590 if not stack: 1591 return (-1, None) 1592 1593 # Did not find end of expression or unbalanced parentheses on this line 1594 return (-1, stack) 1595 1596 1597def CloseExpression(clean_lines, linenum, pos): 1598 """If input points to ( or { or [ or <, finds the position that closes it. 1599 1600 If lines[linenum][pos] points to a '(' or '{' or '[' or '<', finds the 1601 linenum/pos that correspond to the closing of the expression. 1602 1603 TODO(unknown): cpplint spends a fair bit of time matching parentheses. 1604 Ideally we would want to index all opening and closing parentheses once 1605 and have CloseExpression be just a simple lookup, but due to preprocessor 1606 tricks, this is not so easy. 1607 1608 Args: 1609 clean_lines: A CleansedLines instance containing the file. 1610 linenum: The number of the line to check. 1611 pos: A position on the line. 1612 1613 Returns: 1614 A tuple (line, linenum, pos) pointer *past* the closing brace, or 1615 (line, len(lines), -1) if we never find a close. Note we ignore 1616 strings and comments when matching; and the line we return is the 1617 'cleansed' line at linenum. 1618 """ 1619 1620 line = clean_lines.elided[linenum] 1621 if (line[pos] not in '({[<') or Match(r'<[<=]', line[pos:]): 1622 return (line, clean_lines.NumLines(), -1) 1623 1624 # Check first line 1625 (end_pos, stack) = FindEndOfExpressionInLine(line, pos, []) 1626 if end_pos > -1: 1627 return (line, linenum, end_pos) 1628 1629 # Continue scanning forward 1630 while stack and linenum < clean_lines.NumLines() - 1: 1631 linenum += 1 1632 line = clean_lines.elided[linenum] 1633 (end_pos, stack) = FindEndOfExpressionInLine(line, 0, stack) 1634 if end_pos > -1: 1635 return (line, linenum, end_pos) 1636 1637 # Did not find end of expression before end of file, give up 1638 return (line, clean_lines.NumLines(), -1) 1639 1640 1641def FindStartOfExpressionInLine(line, endpos, stack): 1642 """Find position at the matching start of current expression. 1643 1644 This is almost the reverse of FindEndOfExpressionInLine, but note 1645 that the input position and returned position differs by 1. 1646 1647 Args: 1648 line: a CleansedLines line. 1649 endpos: start searching at this position. 1650 stack: nesting stack at endpos. 1651 1652 Returns: 1653 On finding matching start: (index at matching start, None) 1654 On finding an unclosed expression: (-1, None) 1655 Otherwise: (-1, new stack at beginning of this line) 1656 """ 1657 i = endpos 1658 while i >= 0: 1659 char = line[i] 1660 if char in ')]}': 1661 # Found end of expression, push to expression stack 1662 stack.append(char) 1663 elif char == '>': 1664 # Found potential end of template argument list. 1665 # 1666 # Ignore it if it's a "->" or ">=" or "operator>" 1667 if (i > 0 and 1668 (line[i - 1] == '-' or 1669 Match(r'\s>=\s', line[i - 1:]) or 1670 Search(r'\boperator\s*$', line[0:i]))): 1671 i -= 1 1672 else: 1673 stack.append('>') 1674 elif char == '<': 1675 # Found potential start of template argument list 1676 if i > 0 and line[i - 1] == '<': 1677 # Left shift operator 1678 i -= 1 1679 else: 1680 # If there is a matching '>', we can pop the expression stack. 1681 # Otherwise, ignore this '<' since it must be an operator. 1682 if stack and stack[-1] == '>': 1683 stack.pop() 1684 if not stack: 1685 return (i, None) 1686 elif char in '([{': 1687 # Found start of expression. 1688 # 1689 # If there are any unmatched '>' on the stack, they must be 1690 # operators. Remove those. 1691 while stack and stack[-1] == '>': 1692 stack.pop() 1693 if not stack: 1694 return (-1, None) 1695 if ((char == '(' and stack[-1] == ')') or 1696 (char == '[' and stack[-1] == ']') or 1697 (char == '{' and stack[-1] == '}')): 1698 stack.pop() 1699 if not stack: 1700 return (i, None) 1701 else: 1702 # Mismatched parentheses 1703 return (-1, None) 1704 elif char == ';': 1705 # Found something that look like end of statements. If we are currently 1706 # expecting a '<', the matching '>' must have been an operator, since 1707 # template argument list should not contain statements. 1708 while stack and stack[-1] == '>': 1709 stack.pop() 1710 if not stack: 1711 return (-1, None) 1712 1713 i -= 1 1714 1715 return (-1, stack) 1716 1717 1718def ReverseCloseExpression(clean_lines, linenum, pos): 1719 """If input points to ) or } or ] or >, finds the position that opens it. 1720 1721 If lines[linenum][pos] points to a ')' or '}' or ']' or '>', finds the 1722 linenum/pos that correspond to the opening of the expression. 1723 1724 Args: 1725 clean_lines: A CleansedLines instance containing the file. 1726 linenum: The number of the line to check. 1727 pos: A position on the line. 1728 1729 Returns: 1730 A tuple (line, linenum, pos) pointer *at* the opening brace, or 1731 (line, 0, -1) if we never find the matching opening brace. Note 1732 we ignore strings and comments when matching; and the line we 1733 return is the 'cleansed' line at linenum. 1734 """ 1735 line = clean_lines.elided[linenum] 1736 if line[pos] not in ')}]>': 1737 return (line, 0, -1) 1738 1739 # Check last line 1740 (start_pos, stack) = FindStartOfExpressionInLine(line, pos, []) 1741 if start_pos > -1: 1742 return (line, linenum, start_pos) 1743 1744 # Continue scanning backward 1745 while stack and linenum > 0: 1746 linenum -= 1 1747 line = clean_lines.elided[linenum] 1748 (start_pos, stack) = FindStartOfExpressionInLine(line, len(line) - 1, stack) 1749 if start_pos > -1: 1750 return (line, linenum, start_pos) 1751 1752 # Did not find start of expression before beginning of file, give up 1753 return (line, 0, -1) 1754 1755 1756def CheckForCopyright(filename, lines, error): 1757 """Logs an error if no Copyright message appears at the top of the file.""" 1758 1759 # We'll say it should occur by line 10. Don't forget there's a 1760 # dummy line at the front. 1761 for line in xrange(1, min(len(lines), 11)): 1762 if re.search(r'Copyright', lines[line], re.I): break 1763 else: # means no copyright line was found 1764 error(filename, 0, 'legal/copyright', 5, 1765 'No copyright message found. ' 1766 'You should have a line: "Copyright [year] <Copyright Owner>"') 1767 1768 1769def GetIndentLevel(line): 1770 """Return the number of leading spaces in line. 1771 1772 Args: 1773 line: A string to check. 1774 1775 Returns: 1776 An integer count of leading spaces, possibly zero. 1777 """ 1778 indent = Match(r'^( *)\S', line) 1779 if indent: 1780 return len(indent.group(1)) 1781 else: 1782 return 0 1783 1784def PathSplitToList(path): 1785 """Returns the path split into a list by the separator. 1786 1787 Args: 1788 path: An absolute or relative path (e.g. '/a/b/c/' or '../a') 1789 1790 Returns: 1791 A list of path components (e.g. ['a', 'b', 'c]). 1792 """ 1793 lst = [] 1794 while True: 1795 (head, tail) = os.path.split(path) 1796 if head == path: # absolute paths end 1797 lst.append(head) 1798 break 1799 if tail == path: # relative paths end 1800 lst.append(tail) 1801 break 1802 1803 path = head 1804 lst.append(tail) 1805 1806 lst.reverse() 1807 return lst 1808 1809def GetHeaderGuardCPPVariable(filename): 1810 """Returns the CPP variable that should be used as a header guard. 1811 1812 Args: 1813 filename: The name of a C++ header file. 1814 1815 Returns: 1816 The CPP variable that should be used as a header guard in the 1817 named file. 1818 1819 """ 1820 1821 # Restores original filename in case that cpplint is invoked from Emacs's 1822 # flymake. 1823 filename = re.sub(r'_flymake\.h$', '.h', filename) 1824 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename) 1825 # Replace 'c++' with 'cpp'. 1826 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp') 1827 1828 fileinfo = FileInfo(filename) 1829 file_path_from_root = fileinfo.RepositoryName() 1830 1831 def FixupPathFromRoot(): 1832 if _root_debug: 1833 sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n" 1834 %(_root, fileinfo.RepositoryName())) 1835 1836 # Process the file path with the --root flag if it was set. 1837 if not _root: 1838 if _root_debug: 1839 sys.stderr.write("_root unspecified\n") 1840 return file_path_from_root 1841 1842 def StripListPrefix(lst, prefix): 1843 # f(['x', 'y'], ['w, z']) -> None (not a valid prefix) 1844 if lst[:len(prefix)] != prefix: 1845 return None 1846 # f(['a, 'b', 'c', 'd'], ['a', 'b']) -> ['c', 'd'] 1847 return lst[(len(prefix)):] 1848 1849 # root behavior: 1850 # --root=subdir , lstrips subdir from the header guard 1851 maybe_path = StripListPrefix(PathSplitToList(file_path_from_root), 1852 PathSplitToList(_root)) 1853 1854 if _root_debug: 1855 sys.stderr.write(("_root lstrip (maybe_path=%s, file_path_from_root=%s," + 1856 " _root=%s)\n") %(maybe_path, file_path_from_root, _root)) 1857 1858 if maybe_path: 1859 return os.path.join(*maybe_path) 1860 1861 # --root=.. , will prepend the outer directory to the header guard 1862 full_path = fileinfo.FullName() 1863 root_abspath = os.path.abspath(_root) 1864 1865 maybe_path = StripListPrefix(PathSplitToList(full_path), 1866 PathSplitToList(root_abspath)) 1867 1868 if _root_debug: 1869 sys.stderr.write(("_root prepend (maybe_path=%s, full_path=%s, " + 1870 "root_abspath=%s)\n") %(maybe_path, full_path, root_abspath)) 1871 1872 if maybe_path: 1873 return os.path.join(*maybe_path) 1874 1875 if _root_debug: 1876 sys.stderr.write("_root ignore, returning %s\n" %(file_path_from_root)) 1877 1878 # --root=FAKE_DIR is ignored 1879 return file_path_from_root 1880 1881 file_path_from_root = FixupPathFromRoot() 1882 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_' 1883 1884 1885def CheckForHeaderGuard(filename, clean_lines, error): 1886 """Checks that the file contains a header guard. 1887 1888 Logs an error if no #ifndef header guard is present. For other 1889 headers, checks that the full pathname is used. 1890 1891 Args: 1892 filename: The name of the C++ header file. 1893 clean_lines: A CleansedLines instance containing the file. 1894 error: The function to call with any errors found. 1895 """ 1896 1897 # Don't check for header guards if there are error suppression 1898 # comments somewhere in this file. 1899 # 1900 # Because this is silencing a warning for a nonexistent line, we 1901 # only support the very specific NOLINT(build/header_guard) syntax, 1902 # and not the general NOLINT or NOLINT(*) syntax. 1903 raw_lines = clean_lines.lines_without_raw_strings 1904 for i in raw_lines: 1905 if Search(r'//\s*NOLINT\(build/header_guard\)', i): 1906 return 1907 1908 cppvar = GetHeaderGuardCPPVariable(filename) 1909 1910 ifndef = '' 1911 ifndef_linenum = 0 1912 define = '' 1913 endif = '' 1914 endif_linenum = 0 1915 for linenum, line in enumerate(raw_lines): 1916 linesplit = line.split() 1917 if len(linesplit) >= 2: 1918 # find the first occurrence of #ifndef and #define, save arg 1919 if not ifndef and linesplit[0] == '#ifndef': 1920 # set ifndef to the header guard presented on the #ifndef line. 1921 ifndef = linesplit[1] 1922 ifndef_linenum = linenum 1923 if not define and linesplit[0] == '#define': 1924 define = linesplit[1] 1925 # find the last occurrence of #endif, save entire line 1926 if line.startswith('#endif'): 1927 endif = line 1928 endif_linenum = linenum 1929 1930 if not ifndef or not define or ifndef != define: 1931 error(filename, 0, 'build/header_guard', 5, 1932 'No #ifndef header guard found, suggested CPP variable is: %s' % 1933 cppvar) 1934 return 1935 1936 # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__ 1937 # for backward compatibility. 1938 if ifndef != cppvar: 1939 error_level = 0 1940 if ifndef != cppvar + '_': 1941 error_level = 5 1942 1943 ParseNolintSuppressions(filename, raw_lines[ifndef_linenum], ifndef_linenum, 1944 error) 1945 error(filename, ifndef_linenum, 'build/header_guard', error_level, 1946 '#ifndef header guard has wrong style, please use: %s' % cppvar) 1947 1948 # Check for "//" comments on endif line. 1949 ParseNolintSuppressions(filename, raw_lines[endif_linenum], endif_linenum, 1950 error) 1951 match = Match(r'#endif\s*//\s*' + cppvar + r'(_)?\b', endif) 1952 if match: 1953 if match.group(1) == '_': 1954 # Issue low severity warning for deprecated double trailing underscore 1955 error(filename, endif_linenum, 'build/header_guard', 0, 1956 '#endif line should be "#endif // %s"' % cppvar) 1957 return 1958 1959 # Didn't find the corresponding "//" comment. If this file does not 1960 # contain any "//" comments at all, it could be that the compiler 1961 # only wants "/**/" comments, look for those instead. 1962 no_single_line_comments = True 1963 for i in xrange(1, len(raw_lines) - 1): 1964 line = raw_lines[i] 1965 if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line): 1966 no_single_line_comments = False 1967 break 1968 1969 if no_single_line_comments: 1970 match = Match(r'#endif\s*/\*\s*' + cppvar + r'(_)?\s*\*/', endif) 1971 if match: 1972 if match.group(1) == '_': 1973 # Low severity warning for double trailing underscore 1974 error(filename, endif_linenum, 'build/header_guard', 0, 1975 '#endif line should be "#endif /* %s */"' % cppvar) 1976 return 1977 1978 # Didn't find anything 1979 error(filename, endif_linenum, 'build/header_guard', 5, 1980 '#endif line should be "#endif // %s"' % cppvar) 1981 1982 1983def CheckHeaderFileIncluded(filename, include_state, error): 1984 """Logs an error if a .cc file does not include its header.""" 1985 1986 # Do not check test files 1987 fileinfo = FileInfo(filename) 1988 if Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()): 1989 return 1990 1991 headerfile = filename[0:len(filename) - len(fileinfo.Extension())] + '.h' 1992 if not os.path.exists(headerfile): 1993 return 1994 headername = FileInfo(headerfile).RepositoryName() 1995 first_include = 0 1996 for section_list in include_state.include_list: 1997 for f in section_list: 1998 if headername in f[0] or f[0] in headername: 1999 return 2000 if not first_include: 2001 first_include = f[1] 2002 2003 error(filename, first_include, 'build/include', 5, 2004 '%s should include its header file %s' % (fileinfo.RepositoryName(), 2005 headername)) 2006 2007 2008def CheckForBadCharacters(filename, lines, error): 2009 """Logs an error for each line containing bad characters. 2010 2011 Two kinds of bad characters: 2012 2013 1. Unicode replacement characters: These indicate that either the file 2014 contained invalid UTF-8 (likely) or Unicode replacement characters (which 2015 it shouldn't). Note that it's possible for this to throw off line 2016 numbering if the invalid UTF-8 occurred adjacent to a newline. 2017 2018 2. NUL bytes. These are problematic for some tools. 2019 2020 Args: 2021 filename: The name of the current file. 2022 lines: An array of strings, each representing a line of the file. 2023 error: The function to call with any errors found. 2024 """ 2025 for linenum, line in enumerate(lines): 2026 if u'\ufffd' in line: 2027 error(filename, linenum, 'readability/utf8', 5, 2028 'Line contains invalid UTF-8 (or Unicode replacement character).') 2029 if '\0' in line: 2030 error(filename, linenum, 'readability/nul', 5, 'Line contains NUL byte.') 2031 2032 2033def CheckForNewlineAtEOF(filename, lines, error): 2034 """Logs an error if there is no newline char at the end of the file. 2035 2036 Args: 2037 filename: The name of the current file. 2038 lines: An array of strings, each representing a line of the file. 2039 error: The function to call with any errors found. 2040 """ 2041 2042 # The array lines() was created by adding two newlines to the 2043 # original file (go figure), then splitting on \n. 2044 # To verify that the file ends in \n, we just have to make sure the 2045 # last-but-two element of lines() exists and is empty. 2046 if len(lines) < 3 or lines[-2]: 2047 error(filename, len(lines) - 2, 'whitespace/ending_newline', 5, 2048 'Could not find a newline character at the end of the file.') 2049 2050 2051def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error): 2052 """Logs an error if we see /* ... */ or "..." that extend past one line. 2053 2054 /* ... */ comments are legit inside macros, for one line. 2055 Otherwise, we prefer // comments, so it's ok to warn about the 2056 other. Likewise, it's ok for strings to extend across multiple 2057 lines, as long as a line continuation character (backslash) 2058 terminates each line. Although not currently prohibited by the C++ 2059 style guide, it's ugly and unnecessary. We don't do well with either 2060 in this lint program, so we warn about both. 2061 2062 Args: 2063 filename: The name of the current file. 2064 clean_lines: A CleansedLines instance containing the file. 2065 linenum: The number of the line to check. 2066 error: The function to call with any errors found. 2067 """ 2068 line = clean_lines.elided[linenum] 2069 2070 # Remove all \\ (escaped backslashes) from the line. They are OK, and the 2071 # second (escaped) slash may trigger later \" detection erroneously. 2072 line = line.replace('\\\\', '') 2073 2074 if line.count('/*') > line.count('*/'): 2075 error(filename, linenum, 'readability/multiline_comment', 5, 2076 'Complex multi-line /*...*/-style comment found. ' 2077 'Lint may give bogus warnings. ' 2078 'Consider replacing these with //-style comments, ' 2079 'with #if 0...#endif, ' 2080 'or with more clearly structured multi-line comments.') 2081 2082 if (line.count('"') - line.count('\\"')) % 2: 2083 error(filename, linenum, 'readability/multiline_string', 5, 2084 'Multi-line string ("...") found. This lint script doesn\'t ' 2085 'do well with such strings, and may give bogus warnings. ' 2086 'Use C++11 raw strings or concatenation instead.') 2087 2088 2089# (non-threadsafe name, thread-safe alternative, validation pattern) 2090# 2091# The validation pattern is used to eliminate false positives such as: 2092# _rand(); // false positive due to substring match. 2093# ->rand(); // some member function rand(). 2094# ACMRandom rand(seed); // some variable named rand. 2095# ISAACRandom rand(); // another variable named rand. 2096# 2097# Basically we require the return value of these functions to be used 2098# in some expression context on the same line by matching on some 2099# operator before the function name. This eliminates constructors and 2100# member function calls. 2101_UNSAFE_FUNC_PREFIX = r'(?:[-+*/=%^&|(<]\s*|>\s+)' 2102_THREADING_LIST = ( 2103 ('asctime(', 'asctime_r(', _UNSAFE_FUNC_PREFIX + r'asctime\([^)]+\)'), 2104 ('ctime(', 'ctime_r(', _UNSAFE_FUNC_PREFIX + r'ctime\([^)]+\)'), 2105 ('getgrgid(', 'getgrgid_r(', _UNSAFE_FUNC_PREFIX + r'getgrgid\([^)]+\)'), 2106 ('getgrnam(', 'getgrnam_r(', _UNSAFE_FUNC_PREFIX + r'getgrnam\([^)]+\)'), 2107 ('getlogin(', 'getlogin_r(', _UNSAFE_FUNC_PREFIX + r'getlogin\(\)'), 2108 ('getpwnam(', 'getpwnam_r(', _UNSAFE_FUNC_PREFIX + r'getpwnam\([^)]+\)'), 2109 ('getpwuid(', 'getpwuid_r(', _UNSAFE_FUNC_PREFIX + r'getpwuid\([^)]+\)'), 2110 ('gmtime(', 'gmtime_r(', _UNSAFE_FUNC_PREFIX + r'gmtime\([^)]+\)'), 2111 ('localtime(', 'localtime_r(', _UNSAFE_FUNC_PREFIX + r'localtime\([^)]+\)'), 2112 ('rand(', 'rand_r(', _UNSAFE_FUNC_PREFIX + r'rand\(\)'), 2113 ('strtok(', 'strtok_r(', 2114 _UNSAFE_FUNC_PREFIX + r'strtok\([^)]+\)'), 2115 ('ttyname(', 'ttyname_r(', _UNSAFE_FUNC_PREFIX + r'ttyname\([^)]+\)'), 2116 ) 2117 2118 2119def CheckPosixThreading(filename, clean_lines, linenum, error): 2120 """Checks for calls to thread-unsafe functions. 2121 2122 Much code has been originally written without consideration of 2123 multi-threading. Also, engineers are relying on their old experience; 2124 they have learned posix before threading extensions were added. These 2125 tests guide the engineers to use thread-safe functions (when using 2126 posix directly). 2127 2128 Args: 2129 filename: The name of the current file. 2130 clean_lines: A CleansedLines instance containing the file. 2131 linenum: The number of the line to check. 2132 error: The function to call with any errors found. 2133 """ 2134 line = clean_lines.elided[linenum] 2135 for single_thread_func, multithread_safe_func, pattern in _THREADING_LIST: 2136 # Additional pattern matching check to confirm that this is the 2137 # function we are looking for 2138 if Search(pattern, line): 2139 error(filename, linenum, 'runtime/threadsafe_fn', 2, 2140 'Consider using ' + multithread_safe_func + 2141 '...) instead of ' + single_thread_func + 2142 '...) for improved thread safety.') 2143 2144 2145def CheckVlogArguments(filename, clean_lines, linenum, error): 2146 """Checks that VLOG() is only used for defining a logging level. 2147 2148 For example, VLOG(2) is correct. VLOG(INFO), VLOG(WARNING), VLOG(ERROR), and 2149 VLOG(FATAL) are not. 2150 2151 Args: 2152 filename: The name of the current file. 2153 clean_lines: A CleansedLines instance containing the file. 2154 linenum: The number of the line to check. 2155 error: The function to call with any errors found. 2156 """ 2157 line = clean_lines.elided[linenum] 2158 if Search(r'\bVLOG\((INFO|ERROR|WARNING|DFATAL|FATAL)\)', line): 2159 error(filename, linenum, 'runtime/vlog', 5, 2160 'VLOG() should be used with numeric verbosity level. ' 2161 'Use LOG() if you want symbolic severity levels.') 2162 2163# Matches invalid increment: *count++, which moves pointer instead of 2164# incrementing a value. 2165_RE_PATTERN_INVALID_INCREMENT = re.compile( 2166 r'^\s*\*\w+(\+\+|--);') 2167 2168 2169def CheckInvalidIncrement(filename, clean_lines, linenum, error): 2170 """Checks for invalid increment *count++. 2171 2172 For example following function: 2173 void increment_counter(int* count) { 2174 *count++; 2175 } 2176 is invalid, because it effectively does count++, moving pointer, and should 2177 be replaced with ++*count, (*count)++ or *count += 1. 2178 2179 Args: 2180 filename: The name of the current file. 2181 clean_lines: A CleansedLines instance containing the file. 2182 linenum: The number of the line to check. 2183 error: The function to call with any errors found. 2184 """ 2185 line = clean_lines.elided[linenum] 2186 if _RE_PATTERN_INVALID_INCREMENT.match(line): 2187 error(filename, linenum, 'runtime/invalid_increment', 5, 2188 'Changing pointer instead of value (or unused value of operator*).') 2189 2190 2191def IsMacroDefinition(clean_lines, linenum): 2192 if Search(r'^#define', clean_lines[linenum]): 2193 return True 2194 2195 if linenum > 0 and Search(r'\\$', clean_lines[linenum - 1]): 2196 return True 2197 2198 return False 2199 2200 2201def IsForwardClassDeclaration(clean_lines, linenum): 2202 return Match(r'^\s*(\btemplate\b)*.*class\s+\w+;\s*$', clean_lines[linenum]) 2203 2204 2205class _BlockInfo(object): 2206 """Stores information about a generic block of code.""" 2207 2208 def __init__(self, linenum, seen_open_brace): 2209 self.starting_linenum = linenum 2210 self.seen_open_brace = seen_open_brace 2211 self.open_parentheses = 0 2212 self.inline_asm = _NO_ASM 2213 self.check_namespace_indentation = False 2214 2215 def CheckBegin(self, filename, clean_lines, linenum, error): 2216 """Run checks that applies to text up to the opening brace. 2217 2218 This is mostly for checking the text after the class identifier 2219 and the "{", usually where the base class is specified. For other 2220 blocks, there isn't much to check, so we always pass. 2221 2222 Args: 2223 filename: The name of the current file. 2224 clean_lines: A CleansedLines instance containing the file. 2225 linenum: The number of the line to check. 2226 error: The function to call with any errors found. 2227 """ 2228 pass 2229 2230 def CheckEnd(self, filename, clean_lines, linenum, error): 2231 """Run checks that applies to text after the closing brace. 2232 2233 This is mostly used for checking end of namespace comments. 2234 2235 Args: 2236 filename: The name of the current file. 2237 clean_lines: A CleansedLines instance containing the file. 2238 linenum: The number of the line to check. 2239 error: The function to call with any errors found. 2240 """ 2241 pass 2242 2243 def IsBlockInfo(self): 2244 """Returns true if this block is a _BlockInfo. 2245 2246 This is convenient for verifying that an object is an instance of 2247 a _BlockInfo, but not an instance of any of the derived classes. 2248 2249 Returns: 2250 True for this class, False for derived classes. 2251 """ 2252 return self.__class__ == _BlockInfo 2253 2254 2255class _ExternCInfo(_BlockInfo): 2256 """Stores information about an 'extern "C"' block.""" 2257 2258 def __init__(self, linenum): 2259 _BlockInfo.__init__(self, linenum, True) 2260 2261 2262class _ClassInfo(_BlockInfo): 2263 """Stores information about a class.""" 2264 2265 def __init__(self, name, class_or_struct, clean_lines, linenum): 2266 _BlockInfo.__init__(self, linenum, False) 2267 self.name = name 2268 self.is_derived = False 2269 self.check_namespace_indentation = True 2270 if class_or_struct == 'struct': 2271 self.access = 'public' 2272 self.is_struct = True 2273 else: 2274 self.access = 'private' 2275 self.is_struct = False 2276 2277 # Remember initial indentation level for this class. Using raw_lines here 2278 # instead of elided to account for leading comments. 2279 self.class_indent = GetIndentLevel(clean_lines.raw_lines[linenum]) 2280 2281 # Try to find the end of the class. This will be confused by things like: 2282 # class A { 2283 # } *x = { ... 2284 # 2285 # But it's still good enough for CheckSectionSpacing. 2286 self.last_line = 0 2287 depth = 0 2288 for i in range(linenum, clean_lines.NumLines()): 2289 line = clean_lines.elided[i] 2290 depth += line.count('{') - line.count('}') 2291 if not depth: 2292 self.last_line = i 2293 break 2294 2295 def CheckBegin(self, filename, clean_lines, linenum, error): 2296 # Look for a bare ':' 2297 if Search('(^|[^:]):($|[^:])', clean_lines.elided[linenum]): 2298 self.is_derived = True 2299 2300 def CheckEnd(self, filename, clean_lines, linenum, error): 2301 # If there is a DISALLOW macro, it should appear near the end of 2302 # the class. 2303 seen_last_thing_in_class = False 2304 for i in xrange(linenum - 1, self.starting_linenum, -1): 2305 match = Search( 2306 r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' + 2307 self.name + r'\)', 2308 clean_lines.elided[i]) 2309 if match: 2310 if seen_last_thing_in_class: 2311 error(filename, i, 'readability/constructors', 3, 2312 match.group(1) + ' should be the last thing in the class') 2313 break 2314 2315 if not Match(r'^\s*$', clean_lines.elided[i]): 2316 seen_last_thing_in_class = True 2317 2318 # Check that closing brace is aligned with beginning of the class. 2319 # Only do this if the closing brace is indented by only whitespaces. 2320 # This means we will not check single-line class definitions. 2321 indent = Match(r'^( *)\}', clean_lines.elided[linenum]) 2322 if indent and len(indent.group(1)) != self.class_indent: 2323 if self.is_struct: 2324 parent = 'struct ' + self.name 2325 else: 2326 parent = 'class ' + self.name 2327 error(filename, linenum, 'whitespace/indent', 3, 2328 'Closing brace should be aligned with beginning of %s' % parent) 2329 2330 2331class _NamespaceInfo(_BlockInfo): 2332 """Stores information about a namespace.""" 2333 2334 def __init__(self, name, linenum): 2335 _BlockInfo.__init__(self, linenum, False) 2336 self.name = name or '' 2337 self.check_namespace_indentation = True 2338 2339 def CheckEnd(self, filename, clean_lines, linenum, error): 2340 """Check end of namespace comments.""" 2341 line = clean_lines.raw_lines[linenum] 2342 2343 # Check how many lines is enclosed in this namespace. Don't issue 2344 # warning for missing namespace comments if there aren't enough 2345 # lines. However, do apply checks if there is already an end of 2346 # namespace comment and it's incorrect. 2347 # 2348 # TODO(unknown): We always want to check end of namespace comments 2349 # if a namespace is large, but sometimes we also want to apply the 2350 # check if a short namespace contained nontrivial things (something 2351 # other than forward declarations). There is currently no logic on 2352 # deciding what these nontrivial things are, so this check is 2353 # triggered by namespace size only, which works most of the time. 2354 if (linenum - self.starting_linenum < 10 2355 and not Match(r'^\s*};*\s*(//|/\*).*\bnamespace\b', line)): 2356 return 2357 2358 # Look for matching comment at end of namespace. 2359 # 2360 # Note that we accept C style "/* */" comments for terminating 2361 # namespaces, so that code that terminate namespaces inside 2362 # preprocessor macros can be cpplint clean. 2363 # 2364 # We also accept stuff like "// end of namespace <name>." with the 2365 # period at the end. 2366 # 2367 # Besides these, we don't accept anything else, otherwise we might 2368 # get false negatives when existing comment is a substring of the 2369 # expected namespace. 2370 if self.name: 2371 # Named namespace 2372 if not Match((r'^\s*};*\s*(//|/\*).*\bnamespace\s+' + 2373 re.escape(self.name) + r'[\*/\.\\\s]*$'), 2374 line): 2375 error(filename, linenum, 'readability/namespace', 5, 2376 'Namespace should be terminated with "// namespace %s"' % 2377 self.name) 2378 else: 2379 # Anonymous namespace 2380 if not Match(r'^\s*};*\s*(//|/\*).*\bnamespace[\*/\.\\\s]*$', line): 2381 # If "// namespace anonymous" or "// anonymous namespace (more text)", 2382 # mention "// anonymous namespace" as an acceptable form 2383 if Match(r'^\s*}.*\b(namespace anonymous|anonymous namespace)\b', line): 2384 error(filename, linenum, 'readability/namespace', 5, 2385 'Anonymous namespace should be terminated with "// namespace"' 2386 ' or "// anonymous namespace"') 2387 else: 2388 error(filename, linenum, 'readability/namespace', 5, 2389 'Anonymous namespace should be terminated with "// namespace"') 2390 2391 2392class _PreprocessorInfo(object): 2393 """Stores checkpoints of nesting stacks when #if/#else is seen.""" 2394 2395 def __init__(self, stack_before_if): 2396 # The entire nesting stack before #if 2397 self.stack_before_if = stack_before_if 2398 2399 # The entire nesting stack up to #else 2400 self.stack_before_else = [] 2401 2402 # Whether we have already seen #else or #elif 2403 self.seen_else = False 2404 2405 2406class NestingState(object): 2407 """Holds states related to parsing braces.""" 2408 2409 def __init__(self): 2410 # Stack for tracking all braces. An object is pushed whenever we 2411 # see a "{", and popped when we see a "}". Only 3 types of 2412 # objects are possible: 2413 # - _ClassInfo: a class or struct. 2414 # - _NamespaceInfo: a namespace. 2415 # - _BlockInfo: some other type of block. 2416 self.stack = [] 2417 2418 # Top of the previous stack before each Update(). 2419 # 2420 # Because the nesting_stack is updated at the end of each line, we 2421 # had to do some convoluted checks to find out what is the current 2422 # scope at the beginning of the line. This check is simplified by 2423 # saving the previous top of nesting stack. 2424 # 2425 # We could save the full stack, but we only need the top. Copying 2426 # the full nesting stack would slow down cpplint by ~10%. 2427 self.previous_stack_top = [] 2428 2429 # Stack of _PreprocessorInfo objects. 2430 self.pp_stack = [] 2431 2432 def SeenOpenBrace(self): 2433 """Check if we have seen the opening brace for the innermost block. 2434 2435 Returns: 2436 True if we have seen the opening brace, False if the innermost 2437 block is still expecting an opening brace. 2438 """ 2439 return (not self.stack) or self.stack[-1].seen_open_brace 2440 2441 def InNamespaceBody(self): 2442 """Check if we are currently one level inside a namespace body. 2443 2444 Returns: 2445 True if top of the stack is a namespace block, False otherwise. 2446 """ 2447 return self.stack and isinstance(self.stack[-1], _NamespaceInfo) 2448 2449 def InExternC(self): 2450 """Check if we are currently one level inside an 'extern "C"' block. 2451 2452 Returns: 2453 True if top of the stack is an extern block, False otherwise. 2454 """ 2455 return self.stack and isinstance(self.stack[-1], _ExternCInfo) 2456 2457 def InClassDeclaration(self): 2458 """Check if we are currently one level inside a class or struct declaration. 2459 2460 Returns: 2461 True if top of the stack is a class/struct, False otherwise. 2462 """ 2463 return self.stack and isinstance(self.stack[-1], _ClassInfo) 2464 2465 def InAsmBlock(self): 2466 """Check if we are currently one level inside an inline ASM block. 2467 2468 Returns: 2469 True if the top of the stack is a block containing inline ASM. 2470 """ 2471 return self.stack and self.stack[-1].inline_asm != _NO_ASM 2472 2473 def InTemplateArgumentList(self, clean_lines, linenum, pos): 2474 """Check if current position is inside template argument list. 2475 2476 Args: 2477 clean_lines: A CleansedLines instance containing the file. 2478 linenum: The number of the line to check. 2479 pos: position just after the suspected template argument. 2480 Returns: 2481 True if (linenum, pos) is inside template arguments. 2482 """ 2483 while linenum < clean_lines.NumLines(): 2484 # Find the earliest character that might indicate a template argument 2485 line = clean_lines.elided[linenum] 2486 match = Match(r'^[^{};=\[\]\.<>]*(.)', line[pos:]) 2487 if not match: 2488 linenum += 1 2489 pos = 0 2490 continue 2491 token = match.group(1) 2492 pos += len(match.group(0)) 2493 2494 # These things do not look like template argument list: 2495 # class Suspect { 2496 # class Suspect x; } 2497 if token in ('{', '}', ';'): return False 2498 2499 # These things look like template argument list: 2500 # template <class Suspect> 2501 # template <class Suspect = default_value> 2502 # template <class Suspect[]> 2503 # template <class Suspect...> 2504 if token in ('>', '=', '[', ']', '.'): return True 2505 2506 # Check if token is an unmatched '<'. 2507 # If not, move on to the next character. 2508 if token != '<': 2509 pos += 1 2510 if pos >= len(line): 2511 linenum += 1 2512 pos = 0 2513 continue 2514 2515 # We can't be sure if we just find a single '<', and need to 2516 # find the matching '>'. 2517 (_, end_line, end_pos) = CloseExpression(clean_lines, linenum, pos - 1) 2518 if end_pos < 0: 2519 # Not sure if template argument list or syntax error in file 2520 return False 2521 linenum = end_line 2522 pos = end_pos 2523 return False 2524 2525 def UpdatePreprocessor(self, line): 2526 """Update preprocessor stack. 2527 2528 We need to handle preprocessors due to classes like this: 2529 #ifdef SWIG 2530 struct ResultDetailsPageElementExtensionPoint { 2531 #else 2532 struct ResultDetailsPageElementExtensionPoint : public Extension { 2533 #endif 2534 2535 We make the following assumptions (good enough for most files): 2536 - Preprocessor condition evaluates to true from #if up to first 2537 #else/#elif/#endif. 2538 2539 - Preprocessor condition evaluates to false from #else/#elif up 2540 to #endif. We still perform lint checks on these lines, but 2541 these do not affect nesting stack. 2542 2543 Args: 2544 line: current line to check. 2545 """ 2546 if Match(r'^\s*#\s*(if|ifdef|ifndef)\b', line): 2547 # Beginning of #if block, save the nesting stack here. The saved 2548 # stack will allow us to restore the parsing state in the #else case. 2549 self.pp_stack.append(_PreprocessorInfo(copy.deepcopy(self.stack))) 2550 elif Match(r'^\s*#\s*(else|elif)\b', line): 2551 # Beginning of #else block 2552 if self.pp_stack: 2553 if not self.pp_stack[-1].seen_else: 2554 # This is the first #else or #elif block. Remember the 2555 # whole nesting stack up to this point. This is what we 2556 # keep after the #endif. 2557 self.pp_stack[-1].seen_else = True 2558 self.pp_stack[-1].stack_before_else = copy.deepcopy(self.stack) 2559 2560 # Restore the stack to how it was before the #if 2561 self.stack = copy.deepcopy(self.pp_stack[-1].stack_before_if) 2562 else: 2563 # TODO(unknown): unexpected #else, issue warning? 2564 pass 2565 elif Match(r'^\s*#\s*endif\b', line): 2566 # End of #if or #else blocks. 2567 if self.pp_stack: 2568 # If we saw an #else, we will need to restore the nesting 2569 # stack to its former state before the #else, otherwise we 2570 # will just continue from where we left off. 2571 if self.pp_stack[-1].seen_else: 2572 # Here we can just use a shallow copy since we are the last 2573 # reference to it. 2574 self.stack = self.pp_stack[-1].stack_before_else 2575 # Drop the corresponding #if 2576 self.pp_stack.pop() 2577 else: 2578 # TODO(unknown): unexpected #endif, issue warning? 2579 pass 2580 2581 # TODO(unknown): Update() is too long, but we will refactor later. 2582 def Update(self, filename, clean_lines, linenum, error): 2583 """Update nesting state with current line. 2584 2585 Args: 2586 filename: The name of the current file. 2587 clean_lines: A CleansedLines instance containing the file. 2588 linenum: The number of the line to check. 2589 error: The function to call with any errors found. 2590 """ 2591 line = clean_lines.elided[linenum] 2592 2593 # Remember top of the previous nesting stack. 2594 # 2595 # The stack is always pushed/popped and not modified in place, so 2596 # we can just do a shallow copy instead of copy.deepcopy. Using 2597 # deepcopy would slow down cpplint by ~28%. 2598 if self.stack: 2599 self.previous_stack_top = self.stack[-1] 2600 else: 2601 self.previous_stack_top = None 2602 2603 # Update pp_stack 2604 self.UpdatePreprocessor(line) 2605 2606 # Count parentheses. This is to avoid adding struct arguments to 2607 # the nesting stack. 2608 if self.stack: 2609 inner_block = self.stack[-1] 2610 depth_change = line.count('(') - line.count(')') 2611 inner_block.open_parentheses += depth_change 2612 2613 # Also check if we are starting or ending an inline assembly block. 2614 if inner_block.inline_asm in (_NO_ASM, _END_ASM): 2615 if (depth_change != 0 and 2616 inner_block.open_parentheses == 1 and 2617 _MATCH_ASM.match(line)): 2618 # Enter assembly block 2619 inner_block.inline_asm = _INSIDE_ASM 2620 else: 2621 # Not entering assembly block. If previous line was _END_ASM, 2622 # we will now shift to _NO_ASM state. 2623 inner_block.inline_asm = _NO_ASM 2624 elif (inner_block.inline_asm == _INSIDE_ASM and 2625 inner_block.open_parentheses == 0): 2626 # Exit assembly block 2627 inner_block.inline_asm = _END_ASM 2628 2629 # Consume namespace declaration at the beginning of the line. Do 2630 # this in a loop so that we catch same line declarations like this: 2631 # namespace proto2 { namespace bridge { class MessageSet; } } 2632 while True: 2633 # Match start of namespace. The "\b\s*" below catches namespace 2634 # declarations even if it weren't followed by a whitespace, this 2635 # is so that we don't confuse our namespace checker. The 2636 # missing spaces will be flagged by CheckSpacing. 2637 namespace_decl_match = Match(r'^\s*namespace\b\s*([:\w]+)?(.*)$', line) 2638 if not namespace_decl_match: 2639 break 2640 2641 new_namespace = _NamespaceInfo(namespace_decl_match.group(1), linenum) 2642 self.stack.append(new_namespace) 2643 2644 line = namespace_decl_match.group(2) 2645 if line.find('{') != -1: 2646 new_namespace.seen_open_brace = True 2647 line = line[line.find('{') + 1:] 2648 2649 # Look for a class declaration in whatever is left of the line 2650 # after parsing namespaces. The regexp accounts for decorated classes 2651 # such as in: 2652 # class LOCKABLE API Object { 2653 # }; 2654 class_decl_match = Match( 2655 r'^(\s*(?:template\s*<[\w\s<>,:]*>\s*)?' 2656 r'(class|struct)\s+(?:[A-Z_]+\s+)*(\w+(?:::\w+)*))' 2657 r'(.*)$', line) 2658 if (class_decl_match and 2659 (not self.stack or self.stack[-1].open_parentheses == 0)): 2660 # We do not want to accept classes that are actually template arguments: 2661 # template <class Ignore1, 2662 # class Ignore2 = Default<Args>, 2663 # template <Args> class Ignore3> 2664 # void Function() {}; 2665 # 2666 # To avoid template argument cases, we scan forward and look for 2667 # an unmatched '>'. If we see one, assume we are inside a 2668 # template argument list. 2669 end_declaration = len(class_decl_match.group(1)) 2670 if not self.InTemplateArgumentList(clean_lines, linenum, end_declaration): 2671 self.stack.append(_ClassInfo( 2672 class_decl_match.group(3), class_decl_match.group(2), 2673 clean_lines, linenum)) 2674 line = class_decl_match.group(4) 2675 2676 # If we have not yet seen the opening brace for the innermost block, 2677 # run checks here. 2678 if not self.SeenOpenBrace(): 2679 self.stack[-1].CheckBegin(filename, clean_lines, linenum, error) 2680 2681 # Update access control if we are inside a class/struct 2682 if self.stack and isinstance(self.stack[-1], _ClassInfo): 2683 classinfo = self.stack[-1] 2684 access_match = Match( 2685 r'^(.*)\b(public|private|protected|signals)(\s+(?:slots\s*)?)?' 2686 r':(?:[^:]|$)', 2687 line) 2688 if access_match: 2689 classinfo.access = access_match.group(2) 2690 2691 # Check that access keywords are indented +1 space. Skip this 2692 # check if the keywords are not preceded by whitespaces. 2693 indent = access_match.group(1) 2694 if (len(indent) != classinfo.class_indent + 1 and 2695 Match(r'^\s*$', indent)): 2696 if classinfo.is_struct: 2697 parent = 'struct ' + classinfo.name 2698 else: 2699 parent = 'class ' + classinfo.name 2700 slots = '' 2701 if access_match.group(3): 2702 slots = access_match.group(3) 2703 error(filename, linenum, 'whitespace/indent', 3, 2704 '%s%s: should be indented +1 space inside %s' % ( 2705 access_match.group(2), slots, parent)) 2706 2707 # Consume braces or semicolons from what's left of the line 2708 while True: 2709 # Match first brace, semicolon, or closed parenthesis. 2710 matched = Match(r'^[^{;)}]*([{;)}])(.*)$', line) 2711 if not matched: 2712 break 2713 2714 token = matched.group(1) 2715 if token == '{': 2716 # If namespace or class hasn't seen a opening brace yet, mark 2717 # namespace/class head as complete. Push a new block onto the 2718 # stack otherwise. 2719 if not self.SeenOpenBrace(): 2720 self.stack[-1].seen_open_brace = True 2721 elif Match(r'^extern\s*"[^"]*"\s*\{', line): 2722 self.stack.append(_ExternCInfo(linenum)) 2723 else: 2724 self.stack.append(_BlockInfo(linenum, True)) 2725 if _MATCH_ASM.match(line): 2726 self.stack[-1].inline_asm = _BLOCK_ASM 2727 2728 elif token == ';' or token == ')': 2729 # If we haven't seen an opening brace yet, but we already saw 2730 # a semicolon, this is probably a forward declaration. Pop 2731 # the stack for these. 2732 # 2733 # Similarly, if we haven't seen an opening brace yet, but we 2734 # already saw a closing parenthesis, then these are probably 2735 # function arguments with extra "class" or "struct" keywords. 2736 # Also pop these stack for these. 2737 if not self.SeenOpenBrace(): 2738 self.stack.pop() 2739 else: # token == '}' 2740 # Perform end of block checks and pop the stack. 2741 if self.stack: 2742 self.stack[-1].CheckEnd(filename, clean_lines, linenum, error) 2743 self.stack.pop() 2744 line = matched.group(2) 2745 2746 def InnermostClass(self): 2747 """Get class info on the top of the stack. 2748 2749 Returns: 2750 A _ClassInfo object if we are inside a class, or None otherwise. 2751 """ 2752 for i in range(len(self.stack), 0, -1): 2753 classinfo = self.stack[i - 1] 2754 if isinstance(classinfo, _ClassInfo): 2755 return classinfo 2756 return None 2757 2758 def CheckCompletedBlocks(self, filename, error): 2759 """Checks that all classes and namespaces have been completely parsed. 2760 2761 Call this when all lines in a file have been processed. 2762 Args: 2763 filename: The name of the current file. 2764 error: The function to call with any errors found. 2765 """ 2766 # Note: This test can result in false positives if #ifdef constructs 2767 # get in the way of brace matching. See the testBuildClass test in 2768 # cpplint_unittest.py for an example of this. 2769 for obj in self.stack: 2770 if isinstance(obj, _ClassInfo): 2771 error(filename, obj.starting_linenum, 'build/class', 5, 2772 'Failed to find complete declaration of class %s' % 2773 obj.name) 2774 elif isinstance(obj, _NamespaceInfo): 2775 error(filename, obj.starting_linenum, 'build/namespaces', 5, 2776 'Failed to find complete declaration of namespace %s' % 2777 obj.name) 2778 2779 2780def CheckForNonStandardConstructs(filename, clean_lines, linenum, 2781 nesting_state, error): 2782 r"""Logs an error if we see certain non-ANSI constructs ignored by gcc-2. 2783 2784 Complain about several constructs which gcc-2 accepts, but which are 2785 not standard C++. Warning about these in lint is one way to ease the 2786 transition to new compilers. 2787 - put storage class first (e.g. "static const" instead of "const static"). 2788 - "%lld" instead of %qd" in printf-type functions. 2789 - "%1$d" is non-standard in printf-type functions. 2790 - "\%" is an undefined character escape sequence. 2791 - text after #endif is not allowed. 2792 - invalid inner-style forward declaration. 2793 - >? and <? operators, and their >?= and <?= cousins. 2794 2795 Additionally, check for constructor/destructor style violations and reference 2796 members, as it is very convenient to do so while checking for 2797 gcc-2 compliance. 2798 2799 Args: 2800 filename: The name of the current file. 2801 clean_lines: A CleansedLines instance containing the file. 2802 linenum: The number of the line to check. 2803 nesting_state: A NestingState instance which maintains information about 2804 the current stack of nested blocks being parsed. 2805 error: A callable to which errors are reported, which takes 4 arguments: 2806 filename, line number, error level, and message 2807 """ 2808 2809 # Remove comments from the line, but leave in strings for now. 2810 line = clean_lines.lines[linenum] 2811 2812 if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line): 2813 error(filename, linenum, 'runtime/printf_format', 3, 2814 '%q in format strings is deprecated. Use %ll instead.') 2815 2816 if Search(r'printf\s*\(.*".*%\d+\$', line): 2817 error(filename, linenum, 'runtime/printf_format', 2, 2818 '%N$ formats are unconventional. Try rewriting to avoid them.') 2819 2820 # Remove escaped backslashes before looking for undefined escapes. 2821 line = line.replace('\\\\', '') 2822 2823 if Search(r'("|\').*\\(%|\[|\(|{)', line): 2824 error(filename, linenum, 'build/printf_format', 3, 2825 '%, [, (, and { are undefined character escapes. Unescape them.') 2826 2827 # For the rest, work with both comments and strings removed. 2828 line = clean_lines.elided[linenum] 2829 2830 if Search(r'\b(const|volatile|void|char|short|int|long' 2831 r'|float|double|signed|unsigned' 2832 r'|schar|u?int8|u?int16|u?int32|u?int64)' 2833 r'\s+(register|static|extern|typedef)\b', 2834 line): 2835 error(filename, linenum, 'build/storage_class', 5, 2836 'Storage-class specifier (static, extern, typedef, etc) should be ' 2837 'at the beginning of the declaration.') 2838 2839 if Match(r'\s*#\s*endif\s*[^/\s]+', line): 2840 error(filename, linenum, 'build/endif_comment', 5, 2841 'Uncommented text after #endif is non-standard. Use a comment.') 2842 2843 if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line): 2844 error(filename, linenum, 'build/forward_decl', 5, 2845 'Inner-style forward declarations are invalid. Remove this line.') 2846 2847 if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?', 2848 line): 2849 error(filename, linenum, 'build/deprecated', 3, 2850 '>? and <? (max and min) operators are non-standard and deprecated.') 2851 2852 if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line): 2853 # TODO(unknown): Could it be expanded safely to arbitrary references, 2854 # without triggering too many false positives? The first 2855 # attempt triggered 5 warnings for mostly benign code in the regtest, hence 2856 # the restriction. 2857 # Here's the original regexp, for the reference: 2858 # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?' 2859 # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;' 2860 error(filename, linenum, 'runtime/member_string_references', 2, 2861 'const string& members are dangerous. It is much better to use ' 2862 'alternatives, such as pointers or simple constants.') 2863 2864 # Everything else in this function operates on class declarations. 2865 # Return early if the top of the nesting stack is not a class, or if 2866 # the class head is not completed yet. 2867 classinfo = nesting_state.InnermostClass() 2868 if not classinfo or not classinfo.seen_open_brace: 2869 return 2870 2871 # The class may have been declared with namespace or classname qualifiers. 2872 # The constructor and destructor will not have those qualifiers. 2873 base_classname = classinfo.name.split('::')[-1] 2874 2875 # Look for single-argument constructors that aren't marked explicit. 2876 # Technically a valid construct, but against style. 2877 explicit_constructor_match = Match( 2878 r'\s+(?:(?:inline|constexpr)\s+)*(explicit\s+)?' 2879 r'(?:(?:inline|constexpr)\s+)*%s\s*' 2880 r'\(((?:[^()]|\([^()]*\))*)\)' 2881 % re.escape(base_classname), 2882 line) 2883 2884 if explicit_constructor_match: 2885 is_marked_explicit = explicit_constructor_match.group(1) 2886 2887 if not explicit_constructor_match.group(2): 2888 constructor_args = [] 2889 else: 2890 constructor_args = explicit_constructor_match.group(2).split(',') 2891 2892 # collapse arguments so that commas in template parameter lists and function 2893 # argument parameter lists don't split arguments in two 2894 i = 0 2895 while i < len(constructor_args): 2896 constructor_arg = constructor_args[i] 2897 while (constructor_arg.count('<') > constructor_arg.count('>') or 2898 constructor_arg.count('(') > constructor_arg.count(')')): 2899 constructor_arg += ',' + constructor_args[i + 1] 2900 del constructor_args[i + 1] 2901 constructor_args[i] = constructor_arg 2902 i += 1 2903 2904 defaulted_args = [arg for arg in constructor_args if '=' in arg] 2905 noarg_constructor = (not constructor_args or # empty arg list 2906 # 'void' arg specifier 2907 (len(constructor_args) == 1 and 2908 constructor_args[0].strip() == 'void')) 2909 onearg_constructor = ((len(constructor_args) == 1 and # exactly one arg 2910 not noarg_constructor) or 2911 # all but at most one arg defaulted 2912 (len(constructor_args) >= 1 and 2913 not noarg_constructor and 2914 len(defaulted_args) >= len(constructor_args) - 1)) 2915 initializer_list_constructor = bool( 2916 onearg_constructor and 2917 Search(r'\bstd\s*::\s*initializer_list\b', constructor_args[0])) 2918 copy_constructor = bool( 2919 onearg_constructor and 2920 Match(r'(const\s+)?%s(\s*<[^>]*>)?(\s+const)?\s*(?:<\w+>\s*)?&' 2921 % re.escape(base_classname), constructor_args[0].strip())) 2922 2923 if (not is_marked_explicit and 2924 onearg_constructor and 2925 not initializer_list_constructor and 2926 not copy_constructor): 2927 if defaulted_args: 2928 error(filename, linenum, 'runtime/explicit', 5, 2929 'Constructors callable with one argument ' 2930 'should be marked explicit.') 2931 else: 2932 error(filename, linenum, 'runtime/explicit', 5, 2933 'Single-parameter constructors should be marked explicit.') 2934 elif is_marked_explicit and not onearg_constructor: 2935 if noarg_constructor: 2936 error(filename, linenum, 'runtime/explicit', 5, 2937 'Zero-parameter constructors should not be marked explicit.') 2938 2939 2940def CheckSpacingForFunctionCall(filename, clean_lines, linenum, error): 2941 """Checks for the correctness of various spacing around function calls. 2942 2943 Args: 2944 filename: The name of the current file. 2945 clean_lines: A CleansedLines instance containing the file. 2946 linenum: The number of the line to check. 2947 error: The function to call with any errors found. 2948 """ 2949 line = clean_lines.elided[linenum] 2950 2951 # Since function calls often occur inside if/for/while/switch 2952 # expressions - which have their own, more liberal conventions - we 2953 # first see if we should be looking inside such an expression for a 2954 # function call, to which we can apply more strict standards. 2955 fncall = line # if there's no control flow construct, look at whole line 2956 for pattern in (r'\bif\s*\((.*)\)\s*{', 2957 r'\bfor\s*\((.*)\)\s*{', 2958 r'\bwhile\s*\((.*)\)\s*[{;]', 2959 r'\bswitch\s*\((.*)\)\s*{'): 2960 match = Search(pattern, line) 2961 if match: 2962 fncall = match.group(1) # look inside the parens for function calls 2963 break 2964 2965 # Except in if/for/while/switch, there should never be space 2966 # immediately inside parens (eg "f( 3, 4 )"). We make an exception 2967 # for nested parens ( (a+b) + c ). Likewise, there should never be 2968 # a space before a ( when it's a function argument. I assume it's a 2969 # function argument when the char before the whitespace is legal in 2970 # a function name (alnum + _) and we're not starting a macro. Also ignore 2971 # pointers and references to arrays and functions coz they're too tricky: 2972 # we use a very simple way to recognize these: 2973 # " (something)(maybe-something)" or 2974 # " (something)(maybe-something," or 2975 # " (something)[something]" 2976 # Note that we assume the contents of [] to be short enough that 2977 # they'll never need to wrap. 2978 if ( # Ignore control structures. 2979 not Search(r'\b(if|for|while|switch|return|new|delete|catch|sizeof)\b', 2980 fncall) and 2981 # Ignore pointers/references to functions. 2982 not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and 2983 # Ignore pointers/references to arrays. 2984 not Search(r' \([^)]+\)\[[^\]]+\]', fncall)): 2985 if Search(r'\w\s*\(\s(?!\s*\\$)', fncall): # a ( used for a fn call 2986 error(filename, linenum, 'whitespace/parens', 4, 2987 'Extra space after ( in function call') 2988 elif Search(r'\(\s+(?!(\s*\\)|\()', fncall): 2989 error(filename, linenum, 'whitespace/parens', 2, 2990 'Extra space after (') 2991 if (Search(r'\w\s+\(', fncall) and 2992 not Search(r'_{0,2}asm_{0,2}\s+_{0,2}volatile_{0,2}\s+\(', fncall) and 2993 not Search(r'#\s*define|typedef|using\s+\w+\s*=', fncall) and 2994 not Search(r'\w\s+\((\w+::)*\*\w+\)\(', fncall) and 2995 not Search(r'\bcase\s+\(', fncall)): 2996 # TODO(unknown): Space after an operator function seem to be a common 2997 # error, silence those for now by restricting them to highest verbosity. 2998 if Search(r'\boperator_*\b', line): 2999 error(filename, linenum, 'whitespace/parens', 0, 3000 'Extra space before ( in function call') 3001 else: 3002 error(filename, linenum, 'whitespace/parens', 4, 3003 'Extra space before ( in function call') 3004 # If the ) is followed only by a newline or a { + newline, assume it's 3005 # part of a control statement (if/while/etc), and don't complain 3006 if Search(r'[^)]\s+\)\s*[^{\s]', fncall): 3007 # If the closing parenthesis is preceded by only whitespaces, 3008 # try to give a more descriptive error message. 3009 if Search(r'^\s+\)', fncall): 3010 error(filename, linenum, 'whitespace/parens', 2, 3011 'Closing ) should be moved to the previous line') 3012 else: 3013 error(filename, linenum, 'whitespace/parens', 2, 3014 'Extra space before )') 3015 3016 3017def IsBlankLine(line): 3018 """Returns true if the given line is blank. 3019 3020 We consider a line to be blank if the line is empty or consists of 3021 only white spaces. 3022 3023 Args: 3024 line: A line of a string. 3025 3026 Returns: 3027 True, if the given line is blank. 3028 """ 3029 return not line or line.isspace() 3030 3031 3032def CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line, 3033 error): 3034 is_namespace_indent_item = ( 3035 len(nesting_state.stack) > 1 and 3036 nesting_state.stack[-1].check_namespace_indentation and 3037 isinstance(nesting_state.previous_stack_top, _NamespaceInfo) and 3038 nesting_state.previous_stack_top == nesting_state.stack[-2]) 3039 3040 if ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item, 3041 clean_lines.elided, line): 3042 CheckItemIndentationInNamespace(filename, clean_lines.elided, 3043 line, error) 3044 3045 3046def CheckForFunctionLengths(filename, clean_lines, linenum, 3047 function_state, error): 3048 """Reports for long function bodies. 3049 3050 For an overview why this is done, see: 3051 https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions 3052 3053 Uses a simplistic algorithm assuming other style guidelines 3054 (especially spacing) are followed. 3055 Only checks unindented functions, so class members are unchecked. 3056 Trivial bodies are unchecked, so constructors with huge initializer lists 3057 may be missed. 3058 Blank/comment lines are not counted so as to avoid encouraging the removal 3059 of vertical space and comments just to get through a lint check. 3060 NOLINT *on the last line of a function* disables this check. 3061 3062 Args: 3063 filename: The name of the current file. 3064 clean_lines: A CleansedLines instance containing the file. 3065 linenum: The number of the line to check. 3066 function_state: Current function name and lines in body so far. 3067 error: The function to call with any errors found. 3068 """ 3069 lines = clean_lines.lines 3070 line = lines[linenum] 3071 joined_line = '' 3072 3073 starting_func = False 3074 regexp = r'(\w(\w|::|\*|\&|\s)*)\(' # decls * & space::name( ... 3075 match_result = Match(regexp, line) 3076 if match_result: 3077 # If the name is all caps and underscores, figure it's a macro and 3078 # ignore it, unless it's TEST or TEST_F. 3079 function_name = match_result.group(1).split()[-1] 3080 if function_name == 'TEST' or function_name == 'TEST_F' or ( 3081 not Match(r'[A-Z_]+$', function_name)): 3082 starting_func = True 3083 3084 if starting_func: 3085 body_found = False 3086 for start_linenum in xrange(linenum, clean_lines.NumLines()): 3087 start_line = lines[start_linenum] 3088 joined_line += ' ' + start_line.lstrip() 3089 if Search(r'(;|})', start_line): # Declarations and trivial functions 3090 body_found = True 3091 break # ... ignore 3092 elif Search(r'{', start_line): 3093 body_found = True 3094 function = Search(r'((\w|:)*)\(', line).group(1) 3095 if Match(r'TEST', function): # Handle TEST... macros 3096 parameter_regexp = Search(r'(\(.*\))', joined_line) 3097 if parameter_regexp: # Ignore bad syntax 3098 function += parameter_regexp.group(1) 3099 else: 3100 function += '()' 3101 function_state.Begin(function) 3102 break 3103 if not body_found: 3104 # No body for the function (or evidence of a non-function) was found. 3105 error(filename, linenum, 'readability/fn_size', 5, 3106 'Lint failed to find start of function body.') 3107 elif Match(r'^\}\s*$', line): # function end 3108 function_state.Check(error, filename, linenum) 3109 function_state.End() 3110 elif not Match(r'^\s*$', line): 3111 function_state.Count() # Count non-blank/non-comment lines. 3112 3113 3114_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?') 3115 3116 3117def CheckComment(line, filename, linenum, next_line_start, error): 3118 """Checks for common mistakes in comments. 3119 3120 Args: 3121 line: The line in question. 3122 filename: The name of the current file. 3123 linenum: The number of the line to check. 3124 next_line_start: The first non-whitespace column of the next line. 3125 error: The function to call with any errors found. 3126 """ 3127 commentpos = line.find('//') 3128 if commentpos != -1: 3129 # Check if the // may be in quotes. If so, ignore it 3130 if re.sub(r'\\.', '', line[0:commentpos]).count('"') % 2 == 0: 3131 # Allow one space for new scopes, two spaces otherwise: 3132 if (not (Match(r'^.*{ *//', line) and next_line_start == commentpos) and 3133 ((commentpos >= 1 and 3134 line[commentpos-1] not in string.whitespace) or 3135 (commentpos >= 2 and 3136 line[commentpos-2] not in string.whitespace))): 3137 error(filename, linenum, 'whitespace/comments', 2, 3138 'At least two spaces is best between code and comments') 3139 3140 # Checks for common mistakes in TODO comments. 3141 comment = line[commentpos:] 3142 match = _RE_PATTERN_TODO.match(comment) 3143 if match: 3144 # One whitespace is correct; zero whitespace is handled elsewhere. 3145 leading_whitespace = match.group(1) 3146 if len(leading_whitespace) > 1: 3147 error(filename, linenum, 'whitespace/todo', 2, 3148 'Too many spaces before TODO') 3149 3150 username = match.group(2) 3151 if not username: 3152 error(filename, linenum, 'readability/todo', 2, 3153 'Missing username in TODO; it should look like ' 3154 '"// TODO(my_username): Stuff."') 3155 3156 middle_whitespace = match.group(3) 3157 # Comparisons made explicit for correctness -- pylint: disable=g-explicit-bool-comparison 3158 if middle_whitespace != ' ' and middle_whitespace != '': 3159 error(filename, linenum, 'whitespace/todo', 2, 3160 'TODO(my_username) should be followed by a space') 3161 3162 # If the comment contains an alphanumeric character, there 3163 # should be a space somewhere between it and the // unless 3164 # it's a /// or //! Doxygen comment. 3165 if (Match(r'//[^ ]*\w', comment) and 3166 not Match(r'(///|//\!)(\s+|$)', comment)): 3167 error(filename, linenum, 'whitespace/comments', 4, 3168 'Should have a space between // and comment') 3169 3170 3171def CheckSpacing(filename, clean_lines, linenum, nesting_state, error): 3172 """Checks for the correctness of various spacing issues in the code. 3173 3174 Things we check for: spaces around operators, spaces after 3175 if/for/while/switch, no spaces around parens in function calls, two 3176 spaces between code and comment, don't start a block with a blank 3177 line, don't end a function with a blank line, don't add a blank line 3178 after public/protected/private, don't have too many blank lines in a row. 3179 3180 Args: 3181 filename: The name of the current file. 3182 clean_lines: A CleansedLines instance containing the file. 3183 linenum: The number of the line to check. 3184 nesting_state: A NestingState instance which maintains information about 3185 the current stack of nested blocks being parsed. 3186 error: The function to call with any errors found. 3187 """ 3188 3189 # Don't use "elided" lines here, otherwise we can't check commented lines. 3190 # Don't want to use "raw" either, because we don't want to check inside C++11 3191 # raw strings, 3192 raw = clean_lines.lines_without_raw_strings 3193 line = raw[linenum] 3194 3195 # Before nixing comments, check if the line is blank for no good 3196 # reason. This includes the first line after a block is opened, and 3197 # blank lines at the end of a function (ie, right before a line like '}' 3198 # 3199 # Skip all the blank line checks if we are immediately inside a 3200 # namespace body. In other words, don't issue blank line warnings 3201 # for this block: 3202 # namespace { 3203 # 3204 # } 3205 # 3206 # A warning about missing end of namespace comments will be issued instead. 3207 # 3208 # Also skip blank line checks for 'extern "C"' blocks, which are formatted 3209 # like namespaces. 3210 if (IsBlankLine(line) and 3211 not nesting_state.InNamespaceBody() and 3212 not nesting_state.InExternC()): 3213 elided = clean_lines.elided 3214 prev_line = elided[linenum - 1] 3215 prevbrace = prev_line.rfind('{') 3216 # TODO(unknown): Don't complain if line before blank line, and line after, 3217 # both start with alnums and are indented the same amount. 3218 # This ignores whitespace at the start of a namespace block 3219 # because those are not usually indented. 3220 if prevbrace != -1 and prev_line[prevbrace:].find('}') == -1: 3221 # OK, we have a blank line at the start of a code block. Before we 3222 # complain, we check if it is an exception to the rule: The previous 3223 # non-empty line has the parameters of a function header that are indented 3224 # 4 spaces (because they did not fit in a 80 column line when placed on 3225 # the same line as the function name). We also check for the case where 3226 # the previous line is indented 6 spaces, which may happen when the 3227 # initializers of a constructor do not fit into a 80 column line. 3228 exception = False 3229 if Match(r' {6}\w', prev_line): # Initializer list? 3230 # We are looking for the opening column of initializer list, which 3231 # should be indented 4 spaces to cause 6 space indentation afterwards. 3232 search_position = linenum-2 3233 while (search_position >= 0 3234 and Match(r' {6}\w', elided[search_position])): 3235 search_position -= 1 3236 exception = (search_position >= 0 3237 and elided[search_position][:5] == ' :') 3238 else: 3239 # Search for the function arguments or an initializer list. We use a 3240 # simple heuristic here: If the line is indented 4 spaces; and we have a 3241 # closing paren, without the opening paren, followed by an opening brace 3242 # or colon (for initializer lists) we assume that it is the last line of 3243 # a function header. If we have a colon indented 4 spaces, it is an 3244 # initializer list. 3245 exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)', 3246 prev_line) 3247 or Match(r' {4}:', prev_line)) 3248 3249 if not exception: 3250 error(filename, linenum, 'whitespace/blank_line', 2, 3251 'Redundant blank line at the start of a code block ' 3252 'should be deleted.') 3253 # Ignore blank lines at the end of a block in a long if-else 3254 # chain, like this: 3255 # if (condition1) { 3256 # // Something followed by a blank line 3257 # 3258 # } else if (condition2) { 3259 # // Something else 3260 # } 3261 if linenum + 1 < clean_lines.NumLines(): 3262 next_line = raw[linenum + 1] 3263 if (next_line 3264 and Match(r'\s*}', next_line) 3265 and next_line.find('} else ') == -1): 3266 error(filename, linenum, 'whitespace/blank_line', 3, 3267 'Redundant blank line at the end of a code block ' 3268 'should be deleted.') 3269 3270 matched = Match(r'\s*(public|protected|private):', prev_line) 3271 if matched: 3272 error(filename, linenum, 'whitespace/blank_line', 3, 3273 'Do not leave a blank line after "%s:"' % matched.group(1)) 3274 3275 # Next, check comments 3276 next_line_start = 0 3277 if linenum + 1 < clean_lines.NumLines(): 3278 next_line = raw[linenum + 1] 3279 next_line_start = len(next_line) - len(next_line.lstrip()) 3280 CheckComment(line, filename, linenum, next_line_start, error) 3281 3282 # get rid of comments and strings 3283 line = clean_lines.elided[linenum] 3284 3285 # You shouldn't have spaces before your brackets, except maybe after 3286 # 'delete []', 'return []() {};', or 'auto [abc, ...] = ...;'. 3287 if Search(r'\w\s+\[', line) and not Search(r'(?:auto&?|delete|return)\s+\[', line): 3288 error(filename, linenum, 'whitespace/braces', 5, 3289 'Extra space before [') 3290 3291 # In range-based for, we wanted spaces before and after the colon, but 3292 # not around "::" tokens that might appear. 3293 if (Search(r'for *\(.*[^:]:[^: ]', line) or 3294 Search(r'for *\(.*[^: ]:[^:]', line)): 3295 error(filename, linenum, 'whitespace/forcolon', 2, 3296 'Missing space around colon in range-based for loop') 3297 3298 3299def CheckOperatorSpacing(filename, clean_lines, linenum, error): 3300 """Checks for horizontal spacing around operators. 3301 3302 Args: 3303 filename: The name of the current file. 3304 clean_lines: A CleansedLines instance containing the file. 3305 linenum: The number of the line to check. 3306 error: The function to call with any errors found. 3307 """ 3308 line = clean_lines.elided[linenum] 3309 3310 # Don't try to do spacing checks for operator methods. Do this by 3311 # replacing the troublesome characters with something else, 3312 # preserving column position for all other characters. 3313 # 3314 # The replacement is done repeatedly to avoid false positives from 3315 # operators that call operators. 3316 while True: 3317 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line) 3318 if match: 3319 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3) 3320 else: 3321 break 3322 3323 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". 3324 # Otherwise not. Note we only check for non-spaces on *both* sides; 3325 # sometimes people put non-spaces on one side when aligning ='s among 3326 # many lines (not that this is behavior that I approve of...) 3327 if ((Search(r'[\w.]=', line) or 3328 Search(r'=[\w.]', line)) 3329 and not Search(r'\b(if|while|for) ', line) 3330 # Operators taken from [lex.operators] in C++11 standard. 3331 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line) 3332 and not Search(r'operator=', line)): 3333 error(filename, linenum, 'whitespace/operators', 4, 3334 'Missing spaces around =') 3335 3336 # It's ok not to have spaces around binary operators like + - * /, but if 3337 # there's too little whitespace, we get concerned. It's hard to tell, 3338 # though, so we punt on this one for now. TODO. 3339 3340 # You should always have whitespace around binary operators. 3341 # 3342 # Check <= and >= first to avoid false positives with < and >, then 3343 # check non-include lines for spacing around < and >. 3344 # 3345 # If the operator is followed by a comma, assume it's be used in a 3346 # macro context and don't do any checks. This avoids false 3347 # positives. 3348 # 3349 # Note that && is not included here. This is because there are too 3350 # many false positives due to RValue references. 3351 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line) 3352 if match: 3353 error(filename, linenum, 'whitespace/operators', 3, 3354 'Missing spaces around %s' % match.group(1)) 3355 elif not Match(r'#.*include', line): 3356 # Look for < that is not surrounded by spaces. This is only 3357 # triggered if both sides are missing spaces, even though 3358 # technically should should flag if at least one side is missing a 3359 # space. This is done to avoid some false positives with shifts. 3360 match = Match(r'^(.*[^\s<])<[^\s=<,]', line) 3361 if match: 3362 (_, _, end_pos) = CloseExpression( 3363 clean_lines, linenum, len(match.group(1))) 3364 if end_pos <= -1: 3365 error(filename, linenum, 'whitespace/operators', 3, 3366 'Missing spaces around <') 3367 3368 # Look for > that is not surrounded by spaces. Similar to the 3369 # above, we only trigger if both sides are missing spaces to avoid 3370 # false positives with shifts. 3371 match = Match(r'^(.*[^-\s>])>[^\s=>,]', line) 3372 if match: 3373 (_, _, start_pos) = ReverseCloseExpression( 3374 clean_lines, linenum, len(match.group(1))) 3375 if start_pos <= -1: 3376 error(filename, linenum, 'whitespace/operators', 3, 3377 'Missing spaces around >') 3378 3379 # We allow no-spaces around << when used like this: 10<<20, but 3380 # not otherwise (particularly, not when used as streams) 3381 # 3382 # We also allow operators following an opening parenthesis, since 3383 # those tend to be macros that deal with operators. 3384 match = Search(r'(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])', line) 3385 if (match and not (match.group(1).isdigit() and match.group(2).isdigit()) and 3386 not (match.group(1) == 'operator' and match.group(2) == ';')): 3387 error(filename, linenum, 'whitespace/operators', 3, 3388 'Missing spaces around <<') 3389 3390 # We allow no-spaces around >> for almost anything. This is because 3391 # C++11 allows ">>" to close nested templates, which accounts for 3392 # most cases when ">>" is not followed by a space. 3393 # 3394 # We still warn on ">>" followed by alpha character, because that is 3395 # likely due to ">>" being used for right shifts, e.g.: 3396 # value >> alpha 3397 # 3398 # When ">>" is used to close templates, the alphanumeric letter that 3399 # follows would be part of an identifier, and there should still be 3400 # a space separating the template type and the identifier. 3401 # type<type<type>> alpha 3402 match = Search(r'>>[a-zA-Z_]', line) 3403 if match: 3404 error(filename, linenum, 'whitespace/operators', 3, 3405 'Missing spaces around >>') 3406 3407 # There shouldn't be space around unary operators 3408 match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line) 3409 if match: 3410 error(filename, linenum, 'whitespace/operators', 4, 3411 'Extra space for operator %s' % match.group(1)) 3412 3413 3414def CheckParenthesisSpacing(filename, clean_lines, linenum, error): 3415 """Checks for horizontal spacing around parentheses. 3416 3417 Args: 3418 filename: The name of the current file. 3419 clean_lines: A CleansedLines instance containing the file. 3420 linenum: The number of the line to check. 3421 error: The function to call with any errors found. 3422 """ 3423 line = clean_lines.elided[linenum] 3424 3425 # No spaces after an if, while, switch, or for 3426 match = Search(r' (if\(|for\(|while\(|switch\()', line) 3427 if match: 3428 error(filename, linenum, 'whitespace/parens', 5, 3429 'Missing space before ( in %s' % match.group(1)) 3430 3431 # For if/for/while/switch, the left and right parens should be 3432 # consistent about how many spaces are inside the parens, and 3433 # there should either be zero or one spaces inside the parens. 3434 # We don't want: "if ( foo)" or "if ( foo )". 3435 # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed. 3436 match = Search(r'\b(if|for|while|switch)\s*' 3437 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$', 3438 line) 3439 if match: 3440 if len(match.group(2)) != len(match.group(4)): 3441 if not (match.group(3) == ';' and 3442 len(match.group(2)) == 1 + len(match.group(4)) or 3443 not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)): 3444 error(filename, linenum, 'whitespace/parens', 5, 3445 'Mismatching spaces inside () in %s' % match.group(1)) 3446 if len(match.group(2)) not in [0, 1]: 3447 error(filename, linenum, 'whitespace/parens', 5, 3448 'Should have zero or one spaces inside ( and ) in %s' % 3449 match.group(1)) 3450 3451 3452def CheckCommaSpacing(filename, clean_lines, linenum, error): 3453 """Checks for horizontal spacing near commas and semicolons. 3454 3455 Args: 3456 filename: The name of the current file. 3457 clean_lines: A CleansedLines instance containing the file. 3458 linenum: The number of the line to check. 3459 error: The function to call with any errors found. 3460 """ 3461 raw = clean_lines.lines_without_raw_strings 3462 line = clean_lines.elided[linenum] 3463 3464 # You should always have a space after a comma (either as fn arg or operator) 3465 # 3466 # This does not apply when the non-space character following the 3467 # comma is another comma, since the only time when that happens is 3468 # for empty macro arguments. 3469 # 3470 # We run this check in two passes: first pass on elided lines to 3471 # verify that lines contain missing whitespaces, second pass on raw 3472 # lines to confirm that those missing whitespaces are not due to 3473 # elided comments. 3474 if (Search(r',[^,\s]', ReplaceAll(r'\boperator\s*,\s*\(', 'F(', line)) and 3475 Search(r',[^,\s]', raw[linenum])): 3476 error(filename, linenum, 'whitespace/comma', 3, 3477 'Missing space after ,') 3478 3479 # You should always have a space after a semicolon 3480 # except for few corner cases 3481 # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more 3482 # space after ; 3483 if Search(r';[^\s};\\)/]', line): 3484 error(filename, linenum, 'whitespace/semicolon', 3, 3485 'Missing space after ;') 3486 3487 3488def _IsType(clean_lines, nesting_state, expr): 3489 """Check if expression looks like a type name, returns true if so. 3490 3491 Args: 3492 clean_lines: A CleansedLines instance containing the file. 3493 nesting_state: A NestingState instance which maintains information about 3494 the current stack of nested blocks being parsed. 3495 expr: The expression to check. 3496 Returns: 3497 True, if token looks like a type. 3498 """ 3499 # Keep only the last token in the expression 3500 last_word = Match(r'^.*(\b\S+)$', expr) 3501 if last_word: 3502 token = last_word.group(1) 3503 else: 3504 token = expr 3505 3506 # Match native types and stdint types 3507 if _TYPES.match(token): 3508 return True 3509 3510 # Try a bit harder to match templated types. Walk up the nesting 3511 # stack until we find something that resembles a typename 3512 # declaration for what we are looking for. 3513 typename_pattern = (r'\b(?:typename|class|struct)\s+' + re.escape(token) + 3514 r'\b') 3515 block_index = len(nesting_state.stack) - 1 3516 while block_index >= 0: 3517 if isinstance(nesting_state.stack[block_index], _NamespaceInfo): 3518 return False 3519 3520 # Found where the opening brace is. We want to scan from this 3521 # line up to the beginning of the function, minus a few lines. 3522 # template <typename Type1, // stop scanning here 3523 # ...> 3524 # class C 3525 # : public ... { // start scanning here 3526 last_line = nesting_state.stack[block_index].starting_linenum 3527 3528 next_block_start = 0 3529 if block_index > 0: 3530 next_block_start = nesting_state.stack[block_index - 1].starting_linenum 3531 first_line = last_line 3532 while first_line >= next_block_start: 3533 if clean_lines.elided[first_line].find('template') >= 0: 3534 break 3535 first_line -= 1 3536 if first_line < next_block_start: 3537 # Didn't find any "template" keyword before reaching the next block, 3538 # there are probably no template things to check for this block 3539 block_index -= 1 3540 continue 3541 3542 # Look for typename in the specified range 3543 for i in xrange(first_line, last_line + 1, 1): 3544 if Search(typename_pattern, clean_lines.elided[i]): 3545 return True 3546 block_index -= 1 3547 3548 return False 3549 3550 3551def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): 3552 """Checks for horizontal spacing near commas. 3553 3554 Args: 3555 filename: The name of the current file. 3556 clean_lines: A CleansedLines instance containing the file. 3557 linenum: The number of the line to check. 3558 nesting_state: A NestingState instance which maintains information about 3559 the current stack of nested blocks being parsed. 3560 error: The function to call with any errors found. 3561 """ 3562 line = clean_lines.elided[linenum] 3563 3564 # Except after an opening paren, or after another opening brace (in case of 3565 # an initializer list, for instance), you should have spaces before your 3566 # braces when they are delimiting blocks, classes, namespaces etc. 3567 # And since you should never have braces at the beginning of a line, 3568 # this is an easy test. Except that braces used for initialization don't 3569 # follow the same rule; we often don't want spaces before those. 3570 match = Match(r'^(.*[^ ({>]){', line) 3571 3572 if match: 3573 # Try a bit harder to check for brace initialization. This 3574 # happens in one of the following forms: 3575 # Constructor() : initializer_list_{} { ... } 3576 # Constructor{}.MemberFunction() 3577 # Type variable{}; 3578 # FunctionCall(type{}, ...); 3579 # LastArgument(..., type{}); 3580 # LOG(INFO) << type{} << " ..."; 3581 # map_of_type[{...}] = ...; 3582 # ternary = expr ? new type{} : nullptr; 3583 # OuterTemplate<InnerTemplateConstructor<Type>{}> 3584 # 3585 # We check for the character following the closing brace, and 3586 # silence the warning if it's one of those listed above, i.e. 3587 # "{.;,)<>]:". 3588 # 3589 # To account for nested initializer list, we allow any number of 3590 # closing braces up to "{;,)<". We can't simply silence the 3591 # warning on first sight of closing brace, because that would 3592 # cause false negatives for things that are not initializer lists. 3593 # Silence this: But not this: 3594 # Outer{ if (...) { 3595 # Inner{...} if (...){ // Missing space before { 3596 # }; } 3597 # 3598 # There is a false negative with this approach if people inserted 3599 # spurious semicolons, e.g. "if (cond){};", but we will catch the 3600 # spurious semicolon with a separate check. 3601 leading_text = match.group(1) 3602 (endline, endlinenum, endpos) = CloseExpression( 3603 clean_lines, linenum, len(match.group(1))) 3604 trailing_text = '' 3605 if endpos > -1: 3606 trailing_text = endline[endpos:] 3607 for offset in xrange(endlinenum + 1, 3608 min(endlinenum + 3, clean_lines.NumLines() - 1)): 3609 trailing_text += clean_lines.elided[offset] 3610 # We also suppress warnings for `uint64_t{expression}` etc., as the style 3611 # guide recommends brace initialization for integral types to avoid 3612 # overflow/truncation. 3613 if (not Match(r'^[\s}]*[{.;,)<>\]:]', trailing_text) 3614 and not _IsType(clean_lines, nesting_state, leading_text)): 3615 error(filename, linenum, 'whitespace/braces', 5, 3616 'Missing space before {') 3617 3618 # Make sure '} else {' has spaces. 3619 if Search(r'}else', line): 3620 error(filename, linenum, 'whitespace/braces', 5, 3621 'Missing space before else') 3622 3623 # You shouldn't have a space before a semicolon at the end of the line. 3624 # There's a special case for "for" since the style guide allows space before 3625 # the semicolon there. 3626 if Search(r':\s*;\s*$', line): 3627 error(filename, linenum, 'whitespace/semicolon', 5, 3628 'Semicolon defining empty statement. Use {} instead.') 3629 elif Search(r'^\s*;\s*$', line): 3630 error(filename, linenum, 'whitespace/semicolon', 5, 3631 'Line contains only semicolon. If this should be an empty statement, ' 3632 'use {} instead.') 3633 elif (Search(r'\s+;\s*$', line) and 3634 not Search(r'\bfor\b', line)): 3635 error(filename, linenum, 'whitespace/semicolon', 5, 3636 'Extra space before last semicolon. If this should be an empty ' 3637 'statement, use {} instead.') 3638 3639 3640def IsDecltype(clean_lines, linenum, column): 3641 """Check if the token ending on (linenum, column) is decltype(). 3642 3643 Args: 3644 clean_lines: A CleansedLines instance containing the file. 3645 linenum: the number of the line to check. 3646 column: end column of the token to check. 3647 Returns: 3648 True if this token is decltype() expression, False otherwise. 3649 """ 3650 (text, _, start_col) = ReverseCloseExpression(clean_lines, linenum, column) 3651 if start_col < 0: 3652 return False 3653 if Search(r'\bdecltype\s*$', text[0:start_col]): 3654 return True 3655 return False 3656 3657 3658def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error): 3659 """Checks for additional blank line issues related to sections. 3660 3661 Currently the only thing checked here is blank line before protected/private. 3662 3663 Args: 3664 filename: The name of the current file. 3665 clean_lines: A CleansedLines instance containing the file. 3666 class_info: A _ClassInfo objects. 3667 linenum: The number of the line to check. 3668 error: The function to call with any errors found. 3669 """ 3670 # Skip checks if the class is small, where small means 25 lines or less. 3671 # 25 lines seems like a good cutoff since that's the usual height of 3672 # terminals, and any class that can't fit in one screen can't really 3673 # be considered "small". 3674 # 3675 # Also skip checks if we are on the first line. This accounts for 3676 # classes that look like 3677 # class Foo { public: ... }; 3678 # 3679 # If we didn't find the end of the class, last_line would be zero, 3680 # and the check will be skipped by the first condition. 3681 if (class_info.last_line - class_info.starting_linenum <= 24 or 3682 linenum <= class_info.starting_linenum): 3683 return 3684 3685 matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum]) 3686 if matched: 3687 # Issue warning if the line before public/protected/private was 3688 # not a blank line, but don't do this if the previous line contains 3689 # "class" or "struct". This can happen two ways: 3690 # - We are at the beginning of the class. 3691 # - We are forward-declaring an inner class that is semantically 3692 # private, but needed to be public for implementation reasons. 3693 # Also ignores cases where the previous line ends with a backslash as can be 3694 # common when defining classes in C macros. 3695 prev_line = clean_lines.lines[linenum - 1] 3696 if (not IsBlankLine(prev_line) and 3697 not Search(r'\b(class|struct)\b', prev_line) and 3698 not Search(r'\\$', prev_line)): 3699 # Try a bit harder to find the beginning of the class. This is to 3700 # account for multi-line base-specifier lists, e.g.: 3701 # class Derived 3702 # : public Base { 3703 end_class_head = class_info.starting_linenum 3704 for i in range(class_info.starting_linenum, linenum): 3705 if Search(r'\{\s*$', clean_lines.lines[i]): 3706 end_class_head = i 3707 break 3708 if end_class_head < linenum - 1: 3709 error(filename, linenum, 'whitespace/blank_line', 3, 3710 '"%s:" should be preceded by a blank line' % matched.group(1)) 3711 3712 3713def GetPreviousNonBlankLine(clean_lines, linenum): 3714 """Return the most recent non-blank line and its line number. 3715 3716 Args: 3717 clean_lines: A CleansedLines instance containing the file contents. 3718 linenum: The number of the line to check. 3719 3720 Returns: 3721 A tuple with two elements. The first element is the contents of the last 3722 non-blank line before the current line, or the empty string if this is the 3723 first non-blank line. The second is the line number of that line, or -1 3724 if this is the first non-blank line. 3725 """ 3726 3727 prevlinenum = linenum - 1 3728 while prevlinenum >= 0: 3729 prevline = clean_lines.elided[prevlinenum] 3730 if not IsBlankLine(prevline): # if not a blank line... 3731 return (prevline, prevlinenum) 3732 prevlinenum -= 1 3733 return ('', -1) 3734 3735 3736def CheckBraces(filename, clean_lines, linenum, error): 3737 """Looks for misplaced braces (e.g. at the end of line). 3738 3739 Args: 3740 filename: The name of the current file. 3741 clean_lines: A CleansedLines instance containing the file. 3742 linenum: The number of the line to check. 3743 error: The function to call with any errors found. 3744 """ 3745 3746 line = clean_lines.elided[linenum] # get rid of comments and strings 3747 3748 if Match(r'\s*{\s*$', line): 3749 # We allow an open brace to start a line in the case where someone is using 3750 # braces in a block to explicitly create a new scope, which is commonly used 3751 # to control the lifetime of stack-allocated variables. Braces are also 3752 # used for brace initializers inside function calls. We don't detect this 3753 # perfectly: we just don't complain if the last non-whitespace character on 3754 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the 3755 # previous line starts a preprocessor block. We also allow a brace on the 3756 # following line if it is part of an array initialization and would not fit 3757 # within the 80 character limit of the preceding line. 3758 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] 3759 if (not Search(r'[,;:}{(]\s*$', prevline) and 3760 not Match(r'\s*#', prevline) and 3761 not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)): 3762 error(filename, linenum, 'whitespace/braces', 4, 3763 '{ should almost always be at the end of the previous line') 3764 3765 # An else clause should be on the same line as the preceding closing brace. 3766 if Match(r'\s*else\b\s*(?:if\b|\{|$)', line): 3767 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] 3768 if Match(r'\s*}\s*$', prevline): 3769 error(filename, linenum, 'whitespace/newline', 4, 3770 'An else should appear on the same line as the preceding }') 3771 3772 # If braces come on one side of an else, they should be on both. 3773 # However, we have to worry about "else if" that spans multiple lines! 3774 if Search(r'else if\s*\(', line): # could be multi-line if 3775 brace_on_left = bool(Search(r'}\s*else if\s*\(', line)) 3776 # find the ( after the if 3777 pos = line.find('else if') 3778 pos = line.find('(', pos) 3779 if pos > 0: 3780 (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos) 3781 brace_on_right = endline[endpos:].find('{') != -1 3782 if brace_on_left != brace_on_right: # must be brace after if 3783 error(filename, linenum, 'readability/braces', 5, 3784 'If an else has a brace on one side, it should have it on both') 3785 elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line): 3786 error(filename, linenum, 'readability/braces', 5, 3787 'If an else has a brace on one side, it should have it on both') 3788 3789 # Likewise, an else should never have the else clause on the same line 3790 if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line): 3791 error(filename, linenum, 'whitespace/newline', 4, 3792 'Else clause should never be on same line as else (use 2 lines)') 3793 3794 # In the same way, a do/while should never be on one line 3795 if Match(r'\s*do [^\s{]', line): 3796 error(filename, linenum, 'whitespace/newline', 4, 3797 'do/while clauses should not be on a single line') 3798 3799 # Check single-line if/else bodies. The style guide says 'curly braces are not 3800 # required for single-line statements'. We additionally allow multi-line, 3801 # single statements, but we reject anything with more than one semicolon in 3802 # it. This means that the first semicolon after the if should be at the end of 3803 # its line, and the line after that should have an indent level equal to or 3804 # lower than the if. We also check for ambiguous if/else nesting without 3805 # braces. 3806 if_else_match = Search(r'\b(if\s*\(|else\b)', line) 3807 if if_else_match and not Match(r'\s*#', line): 3808 if_indent = GetIndentLevel(line) 3809 endline, endlinenum, endpos = line, linenum, if_else_match.end() 3810 if_match = Search(r'\bif\s*\(', line) 3811 if if_match: 3812 # This could be a multiline if condition, so find the end first. 3813 pos = if_match.end() - 1 3814 (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, pos) 3815 # Check for an opening brace, either directly after the if or on the next 3816 # line. If found, this isn't a single-statement conditional. 3817 if (not Match(r'\s*{', endline[endpos:]) 3818 and not (Match(r'\s*$', endline[endpos:]) 3819 and endlinenum < (len(clean_lines.elided) - 1) 3820 and Match(r'\s*{', clean_lines.elided[endlinenum + 1]))): 3821 while (endlinenum < len(clean_lines.elided) 3822 and ';' not in clean_lines.elided[endlinenum][endpos:]): 3823 endlinenum += 1 3824 endpos = 0 3825 if endlinenum < len(clean_lines.elided): 3826 endline = clean_lines.elided[endlinenum] 3827 # We allow a mix of whitespace and closing braces (e.g. for one-liner 3828 # methods) and a single \ after the semicolon (for macros) 3829 endpos = endline.find(';') 3830 if not Match(r';[\s}]*(\\?)$', endline[endpos:]): 3831 # Semicolon isn't the last character, there's something trailing. 3832 # Output a warning if the semicolon is not contained inside 3833 # a lambda expression. 3834 if not Match(r'^[^{};]*\[[^\[\]]*\][^{}]*\{[^{}]*\}\s*\)*[;,]\s*$', 3835 endline): 3836 error(filename, linenum, 'readability/braces', 4, 3837 'If/else bodies with multiple statements require braces') 3838 elif endlinenum < len(clean_lines.elided) - 1: 3839 # Make sure the next line is dedented 3840 next_line = clean_lines.elided[endlinenum + 1] 3841 next_indent = GetIndentLevel(next_line) 3842 # With ambiguous nested if statements, this will error out on the 3843 # if that *doesn't* match the else, regardless of whether it's the 3844 # inner one or outer one. 3845 if (if_match and Match(r'\s*else\b', next_line) 3846 and next_indent != if_indent): 3847 error(filename, linenum, 'readability/braces', 4, 3848 'Else clause should be indented at the same level as if. ' 3849 'Ambiguous nested if/else chains require braces.') 3850 elif next_indent > if_indent: 3851 error(filename, linenum, 'readability/braces', 4, 3852 'If/else bodies with multiple statements require braces') 3853 3854 3855def CheckTrailingSemicolon(filename, clean_lines, linenum, error): 3856 """Looks for redundant trailing semicolon. 3857 3858 Args: 3859 filename: The name of the current file. 3860 clean_lines: A CleansedLines instance containing the file. 3861 linenum: The number of the line to check. 3862 error: The function to call with any errors found. 3863 """ 3864 3865 line = clean_lines.elided[linenum] 3866 3867 # Block bodies should not be followed by a semicolon. Due to C++11 3868 # brace initialization, there are more places where semicolons are 3869 # required than not, so we use a whitelist approach to check these 3870 # rather than a blacklist. These are the places where "};" should 3871 # be replaced by just "}": 3872 # 1. Some flavor of block following closing parenthesis: 3873 # for (;;) {}; 3874 # while (...) {}; 3875 # switch (...) {}; 3876 # Function(...) {}; 3877 # if (...) {}; 3878 # if (...) else if (...) {}; 3879 # 3880 # 2. else block: 3881 # if (...) else {}; 3882 # 3883 # 3. const member function: 3884 # Function(...) const {}; 3885 # 3886 # 4. Block following some statement: 3887 # x = 42; 3888 # {}; 3889 # 3890 # 5. Block at the beginning of a function: 3891 # Function(...) { 3892 # {}; 3893 # } 3894 # 3895 # Note that naively checking for the preceding "{" will also match 3896 # braces inside multi-dimensional arrays, but this is fine since 3897 # that expression will not contain semicolons. 3898 # 3899 # 6. Block following another block: 3900 # while (true) {} 3901 # {}; 3902 # 3903 # 7. End of namespaces: 3904 # namespace {}; 3905 # 3906 # These semicolons seems far more common than other kinds of 3907 # redundant semicolons, possibly due to people converting classes 3908 # to namespaces. For now we do not warn for this case. 3909 # 3910 # Try matching case 1 first. 3911 match = Match(r'^(.*\)\s*)\{', line) 3912 if match: 3913 # Matched closing parenthesis (case 1). Check the token before the 3914 # matching opening parenthesis, and don't warn if it looks like a 3915 # macro. This avoids these false positives: 3916 # - macro that defines a base class 3917 # - multi-line macro that defines a base class 3918 # - macro that defines the whole class-head 3919 # 3920 # But we still issue warnings for macros that we know are safe to 3921 # warn, specifically: 3922 # - TEST, TEST_F, TEST_P, MATCHER, MATCHER_P 3923 # - TYPED_TEST 3924 # - INTERFACE_DEF 3925 # - EXCLUSIVE_LOCKS_REQUIRED, SHARED_LOCKS_REQUIRED, LOCKS_EXCLUDED: 3926 # 3927 # We implement a whitelist of safe macros instead of a blacklist of 3928 # unsafe macros, even though the latter appears less frequently in 3929 # google code and would have been easier to implement. This is because 3930 # the downside for getting the whitelist wrong means some extra 3931 # semicolons, while the downside for getting the blacklist wrong 3932 # would result in compile errors. 3933 # 3934 # In addition to macros, we also don't want to warn on 3935 # - Compound literals 3936 # - Lambdas 3937 # - alignas specifier with anonymous structs 3938 # - decltype 3939 closing_brace_pos = match.group(1).rfind(')') 3940 opening_parenthesis = ReverseCloseExpression( 3941 clean_lines, linenum, closing_brace_pos) 3942 if opening_parenthesis[2] > -1: 3943 line_prefix = opening_parenthesis[0][0:opening_parenthesis[2]] 3944 macro = Search(r'\b([A-Z_][A-Z0-9_]*)\s*$', line_prefix) 3945 func = Match(r'^(.*\])\s*$', line_prefix) 3946 if ((macro and 3947 macro.group(1) not in ( 3948 'TEST', 'TEST_F', 'MATCHER', 'MATCHER_P', 'TYPED_TEST', 3949 'EXCLUSIVE_LOCKS_REQUIRED', 'SHARED_LOCKS_REQUIRED', 3950 'LOCKS_EXCLUDED', 'INTERFACE_DEF')) or 3951 (func and not Search(r'\boperator\s*\[\s*\]', func.group(1))) or 3952 Search(r'\b(?:struct|union)\s+alignas\s*$', line_prefix) or 3953 Search(r'\bdecltype$', line_prefix) or 3954 Search(r'\s+=\s*$', line_prefix)): 3955 match = None 3956 if (match and 3957 opening_parenthesis[1] > 1 and 3958 Search(r'\]\s*$', clean_lines.elided[opening_parenthesis[1] - 1])): 3959 # Multi-line lambda-expression 3960 match = None 3961 3962 else: 3963 # Try matching cases 2-3. 3964 match = Match(r'^(.*(?:else|\)\s*const)\s*)\{', line) 3965 if not match: 3966 # Try matching cases 4-6. These are always matched on separate lines. 3967 # 3968 # Note that we can't simply concatenate the previous line to the 3969 # current line and do a single match, otherwise we may output 3970 # duplicate warnings for the blank line case: 3971 # if (cond) { 3972 # // blank line 3973 # } 3974 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] 3975 if prevline and Search(r'[;{}]\s*$', prevline): 3976 match = Match(r'^(\s*)\{', line) 3977 3978 # Check matching closing brace 3979 if match: 3980 (endline, endlinenum, endpos) = CloseExpression( 3981 clean_lines, linenum, len(match.group(1))) 3982 if endpos > -1 and Match(r'^\s*;', endline[endpos:]): 3983 # Current {} pair is eligible for semicolon check, and we have found 3984 # the redundant semicolon, output warning here. 3985 # 3986 # Note: because we are scanning forward for opening braces, and 3987 # outputting warnings for the matching closing brace, if there are 3988 # nested blocks with trailing semicolons, we will get the error 3989 # messages in reversed order. 3990 3991 # We need to check the line forward for NOLINT 3992 raw_lines = clean_lines.raw_lines 3993 ParseNolintSuppressions(filename, raw_lines[endlinenum-1], endlinenum-1, 3994 error) 3995 ParseNolintSuppressions(filename, raw_lines[endlinenum], endlinenum, 3996 error) 3997 3998 error(filename, endlinenum, 'readability/braces', 4, 3999 "You don't need a ; after a }") 4000 4001 4002def CheckEmptyBlockBody(filename, clean_lines, linenum, error): 4003 """Look for empty loop/conditional body with only a single semicolon. 4004 4005 Args: 4006 filename: The name of the current file. 4007 clean_lines: A CleansedLines instance containing the file. 4008 linenum: The number of the line to check. 4009 error: The function to call with any errors found. 4010 """ 4011 4012 # Search for loop keywords at the beginning of the line. Because only 4013 # whitespaces are allowed before the keywords, this will also ignore most 4014 # do-while-loops, since those lines should start with closing brace. 4015 # 4016 # We also check "if" blocks here, since an empty conditional block 4017 # is likely an error. 4018 line = clean_lines.elided[linenum] 4019 matched = Match(r'\s*(for|while|if)\s*\(', line) 4020 if matched: 4021 # Find the end of the conditional expression. 4022 (end_line, end_linenum, end_pos) = CloseExpression( 4023 clean_lines, linenum, line.find('(')) 4024 4025 # Output warning if what follows the condition expression is a semicolon. 4026 # No warning for all other cases, including whitespace or newline, since we 4027 # have a separate check for semicolons preceded by whitespace. 4028 if end_pos >= 0 and Match(r';', end_line[end_pos:]): 4029 if matched.group(1) == 'if': 4030 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5, 4031 'Empty conditional bodies should use {}') 4032 else: 4033 error(filename, end_linenum, 'whitespace/empty_loop_body', 5, 4034 'Empty loop bodies should use {} or continue') 4035 4036 # Check for if statements that have completely empty bodies (no comments) 4037 # and no else clauses. 4038 if end_pos >= 0 and matched.group(1) == 'if': 4039 # Find the position of the opening { for the if statement. 4040 # Return without logging an error if it has no brackets. 4041 opening_linenum = end_linenum 4042 opening_line_fragment = end_line[end_pos:] 4043 # Loop until EOF or find anything that's not whitespace or opening {. 4044 while not Search(r'^\s*\{', opening_line_fragment): 4045 if Search(r'^(?!\s*$)', opening_line_fragment): 4046 # Conditional has no brackets. 4047 return 4048 opening_linenum += 1 4049 if opening_linenum == len(clean_lines.elided): 4050 # Couldn't find conditional's opening { or any code before EOF. 4051 return 4052 opening_line_fragment = clean_lines.elided[opening_linenum] 4053 # Set opening_line (opening_line_fragment may not be entire opening line). 4054 opening_line = clean_lines.elided[opening_linenum] 4055 4056 # Find the position of the closing }. 4057 opening_pos = opening_line_fragment.find('{') 4058 if opening_linenum == end_linenum: 4059 # We need to make opening_pos relative to the start of the entire line. 4060 opening_pos += end_pos 4061 (closing_line, closing_linenum, closing_pos) = CloseExpression( 4062 clean_lines, opening_linenum, opening_pos) 4063 if closing_pos < 0: 4064 return 4065 4066 # Now construct the body of the conditional. This consists of the portion 4067 # of the opening line after the {, all lines until the closing line, 4068 # and the portion of the closing line before the }. 4069 if (clean_lines.raw_lines[opening_linenum] != 4070 CleanseComments(clean_lines.raw_lines[opening_linenum])): 4071 # Opening line ends with a comment, so conditional isn't empty. 4072 return 4073 if closing_linenum > opening_linenum: 4074 # Opening line after the {. Ignore comments here since we checked above. 4075 body = list(opening_line[opening_pos+1:]) 4076 # All lines until closing line, excluding closing line, with comments. 4077 body.extend(clean_lines.raw_lines[opening_linenum+1:closing_linenum]) 4078 # Closing line before the }. Won't (and can't) have comments. 4079 body.append(clean_lines.elided[closing_linenum][:closing_pos-1]) 4080 body = '\n'.join(body) 4081 else: 4082 # If statement has brackets and fits on a single line. 4083 body = opening_line[opening_pos+1:closing_pos-1] 4084 4085 # Check if the body is empty 4086 if not _EMPTY_CONDITIONAL_BODY_PATTERN.search(body): 4087 return 4088 # The body is empty. Now make sure there's not an else clause. 4089 current_linenum = closing_linenum 4090 current_line_fragment = closing_line[closing_pos:] 4091 # Loop until EOF or find anything that's not whitespace or else clause. 4092 while Search(r'^\s*$|^(?=\s*else)', current_line_fragment): 4093 if Search(r'^(?=\s*else)', current_line_fragment): 4094 # Found an else clause, so don't log an error. 4095 return 4096 current_linenum += 1 4097 if current_linenum == len(clean_lines.elided): 4098 break 4099 current_line_fragment = clean_lines.elided[current_linenum] 4100 4101 # The body is empty and there's no else clause until EOF or other code. 4102 error(filename, end_linenum, 'whitespace/empty_if_body', 4, 4103 ('If statement had no body and no else clause')) 4104 4105 4106def FindCheckMacro(line): 4107 """Find a replaceable CHECK-like macro. 4108 4109 Args: 4110 line: line to search on. 4111 Returns: 4112 (macro name, start position), or (None, -1) if no replaceable 4113 macro is found. 4114 """ 4115 for macro in _CHECK_MACROS: 4116 i = line.find(macro) 4117 if i >= 0: 4118 # Find opening parenthesis. Do a regular expression match here 4119 # to make sure that we are matching the expected CHECK macro, as 4120 # opposed to some other macro that happens to contain the CHECK 4121 # substring. 4122 matched = Match(r'^(.*\b' + macro + r'\s*)\(', line) 4123 if not matched: 4124 continue 4125 return (macro, len(matched.group(1))) 4126 return (None, -1) 4127 4128 4129def CheckCheck(filename, clean_lines, linenum, error): 4130 """Checks the use of CHECK and EXPECT macros. 4131 4132 Args: 4133 filename: The name of the current file. 4134 clean_lines: A CleansedLines instance containing the file. 4135 linenum: The number of the line to check. 4136 error: The function to call with any errors found. 4137 """ 4138 4139 # Decide the set of replacement macros that should be suggested 4140 lines = clean_lines.elided 4141 (check_macro, start_pos) = FindCheckMacro(lines[linenum]) 4142 if not check_macro: 4143 return 4144 4145 # Find end of the boolean expression by matching parentheses 4146 (last_line, end_line, end_pos) = CloseExpression( 4147 clean_lines, linenum, start_pos) 4148 if end_pos < 0: 4149 return 4150 4151 # If the check macro is followed by something other than a 4152 # semicolon, assume users will log their own custom error messages 4153 # and don't suggest any replacements. 4154 if not Match(r'\s*;', last_line[end_pos:]): 4155 return 4156 4157 if linenum == end_line: 4158 expression = lines[linenum][start_pos + 1:end_pos - 1] 4159 else: 4160 expression = lines[linenum][start_pos + 1:] 4161 for i in xrange(linenum + 1, end_line): 4162 expression += lines[i] 4163 expression += last_line[0:end_pos - 1] 4164 4165 # Parse expression so that we can take parentheses into account. 4166 # This avoids false positives for inputs like "CHECK((a < 4) == b)", 4167 # which is not replaceable by CHECK_LE. 4168 lhs = '' 4169 rhs = '' 4170 operator = None 4171 while expression: 4172 matched = Match(r'^\s*(<<|<<=|>>|>>=|->\*|->|&&|\|\||' 4173 r'==|!=|>=|>|<=|<|\()(.*)$', expression) 4174 if matched: 4175 token = matched.group(1) 4176 if token == '(': 4177 # Parenthesized operand 4178 expression = matched.group(2) 4179 (end, _) = FindEndOfExpressionInLine(expression, 0, ['(']) 4180 if end < 0: 4181 return # Unmatched parenthesis 4182 lhs += '(' + expression[0:end] 4183 expression = expression[end:] 4184 elif token in ('&&', '||'): 4185 # Logical and/or operators. This means the expression 4186 # contains more than one term, for example: 4187 # CHECK(42 < a && a < b); 4188 # 4189 # These are not replaceable with CHECK_LE, so bail out early. 4190 return 4191 elif token in ('<<', '<<=', '>>', '>>=', '->*', '->'): 4192 # Non-relational operator 4193 lhs += token 4194 expression = matched.group(2) 4195 else: 4196 # Relational operator 4197 operator = token 4198 rhs = matched.group(2) 4199 break 4200 else: 4201 # Unparenthesized operand. Instead of appending to lhs one character 4202 # at a time, we do another regular expression match to consume several 4203 # characters at once if possible. Trivial benchmark shows that this 4204 # is more efficient when the operands are longer than a single 4205 # character, which is generally the case. 4206 matched = Match(r'^([^-=!<>()&|]+)(.*)$', expression) 4207 if not matched: 4208 matched = Match(r'^(\s*\S)(.*)$', expression) 4209 if not matched: 4210 break 4211 lhs += matched.group(1) 4212 expression = matched.group(2) 4213 4214 # Only apply checks if we got all parts of the boolean expression 4215 if not (lhs and operator and rhs): 4216 return 4217 4218 # Check that rhs do not contain logical operators. We already know 4219 # that lhs is fine since the loop above parses out && and ||. 4220 if rhs.find('&&') > -1 or rhs.find('||') > -1: 4221 return 4222 4223 # At least one of the operands must be a constant literal. This is 4224 # to avoid suggesting replacements for unprintable things like 4225 # CHECK(variable != iterator) 4226 # 4227 # The following pattern matches decimal, hex integers, strings, and 4228 # characters (in that order). 4229 lhs = lhs.strip() 4230 rhs = rhs.strip() 4231 match_constant = r'^([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')$' 4232 if Match(match_constant, lhs) or Match(match_constant, rhs): 4233 # Note: since we know both lhs and rhs, we can provide a more 4234 # descriptive error message like: 4235 # Consider using CHECK_EQ(x, 42) instead of CHECK(x == 42) 4236 # Instead of: 4237 # Consider using CHECK_EQ instead of CHECK(a == b) 4238 # 4239 # We are still keeping the less descriptive message because if lhs 4240 # or rhs gets long, the error message might become unreadable. 4241 error(filename, linenum, 'readability/check', 2, 4242 'Consider using %s instead of %s(a %s b)' % ( 4243 _CHECK_REPLACEMENT[check_macro][operator], 4244 check_macro, operator)) 4245 4246 4247def CheckAltTokens(filename, clean_lines, linenum, error): 4248 """Check alternative keywords being used in boolean expressions. 4249 4250 Args: 4251 filename: The name of the current file. 4252 clean_lines: A CleansedLines instance containing the file. 4253 linenum: The number of the line to check. 4254 error: The function to call with any errors found. 4255 """ 4256 line = clean_lines.elided[linenum] 4257 4258 # Avoid preprocessor lines 4259 if Match(r'^\s*#', line): 4260 return 4261 4262 # Last ditch effort to avoid multi-line comments. This will not help 4263 # if the comment started before the current line or ended after the 4264 # current line, but it catches most of the false positives. At least, 4265 # it provides a way to workaround this warning for people who use 4266 # multi-line comments in preprocessor macros. 4267 # 4268 # TODO(unknown): remove this once cpplint has better support for 4269 # multi-line comments. 4270 if line.find('/*') >= 0 or line.find('*/') >= 0: 4271 return 4272 4273 for match in _ALT_TOKEN_REPLACEMENT_PATTERN.finditer(line): 4274 error(filename, linenum, 'readability/alt_tokens', 2, 4275 'Use operator %s instead of %s' % ( 4276 _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1))) 4277 4278 4279def GetLineWidth(line): 4280 """Determines the width of the line in column positions. 4281 4282 Args: 4283 line: A string, which may be a Unicode string. 4284 4285 Returns: 4286 The width of the line in column positions, accounting for Unicode 4287 combining characters and wide characters. 4288 """ 4289 if isinstance(line, unicode): 4290 width = 0 4291 for uc in unicodedata.normalize('NFC', line): 4292 if unicodedata.east_asian_width(uc) in ('W', 'F'): 4293 width += 2 4294 elif not unicodedata.combining(uc): 4295 # Issue 337 4296 # https://mail.python.org/pipermail/python-list/2012-August/628809.html 4297 if (sys.version_info.major, sys.version_info.minor) <= (3, 2): 4298 try: 4299 # https://github.com/python/cpython/blob/2.7/Include/unicodeobject.h#L81 4300 is_wide_build = sysconfig.get_config_var("Py_UNICODE_SIZE") >= 4 4301 # https://github.com/python/cpython/blob/2.7/Objects/unicodeobject.c#L564 4302 is_low_surrogate = 0xDC00 <= ord(uc) <= 0xDFFF 4303 if not is_wide_build and is_low_surrogate: 4304 width -= 1 4305 except ImportError: 4306 # Android prebuilt python ends up CHECK-failing here due to how it's compiled. 4307 pass 4308 width += 1 4309 return width 4310 else: 4311 return len(line) 4312 4313 4314def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, 4315 error): 4316 """Checks rules from the 'C++ style rules' section of cppguide.html. 4317 4318 Most of these rules are hard to test (naming, comment style), but we 4319 do what we can. In particular we check for 2-space indents, line lengths, 4320 tab usage, spaces inside code, etc. 4321 4322 Args: 4323 filename: The name of the current file. 4324 clean_lines: A CleansedLines instance containing the file. 4325 linenum: The number of the line to check. 4326 file_extension: The extension (without the dot) of the filename. 4327 nesting_state: A NestingState instance which maintains information about 4328 the current stack of nested blocks being parsed. 4329 error: The function to call with any errors found. 4330 """ 4331 4332 # Don't use "elided" lines here, otherwise we can't check commented lines. 4333 # Don't want to use "raw" either, because we don't want to check inside C++11 4334 # raw strings, 4335 raw_lines = clean_lines.lines_without_raw_strings 4336 line = raw_lines[linenum] 4337 prev = raw_lines[linenum - 1] if linenum > 0 else '' 4338 4339 if line.find('\t') != -1: 4340 error(filename, linenum, 'whitespace/tab', 1, 4341 'Tab found; better to use spaces') 4342 4343 # One or three blank spaces at the beginning of the line is weird; it's 4344 # hard to reconcile that with 2-space indents. 4345 # NOTE: here are the conditions rob pike used for his tests. Mine aren't 4346 # as sophisticated, but it may be worth becoming so: RLENGTH==initial_spaces 4347 # if(RLENGTH > 20) complain = 0; 4348 # if(match($0, " +(error|private|public|protected):")) complain = 0; 4349 # if(match(prev, "&& *$")) complain = 0; 4350 # if(match(prev, "\\|\\| *$")) complain = 0; 4351 # if(match(prev, "[\",=><] *$")) complain = 0; 4352 # if(match($0, " <<")) complain = 0; 4353 # if(match(prev, " +for \\(")) complain = 0; 4354 # if(prevodd && match(prevprev, " +for \\(")) complain = 0; 4355 scope_or_label_pattern = r'\s*\w+\s*:\s*\\?$' 4356 classinfo = nesting_state.InnermostClass() 4357 initial_spaces = 0 4358 cleansed_line = clean_lines.elided[linenum] 4359 while initial_spaces < len(line) and line[initial_spaces] == ' ': 4360 initial_spaces += 1 4361 # There are certain situations we allow one space, notably for 4362 # section labels, and also lines containing multi-line raw strings. 4363 # We also don't check for lines that look like continuation lines 4364 # (of lines ending in double quotes, commas, equals, or angle brackets) 4365 # because the rules for how to indent those are non-trivial. 4366 if (not Search(r'[",=><] *$', prev) and 4367 (initial_spaces == 1 or initial_spaces == 3) and 4368 not Match(scope_or_label_pattern, cleansed_line) and 4369 not (clean_lines.raw_lines[linenum] != line and 4370 Match(r'^\s*""', line))): 4371 error(filename, linenum, 'whitespace/indent', 3, 4372 'Weird number of spaces at line-start. ' 4373 'Are you using a 2-space indent?') 4374 4375 if line and line[-1].isspace(): 4376 error(filename, linenum, 'whitespace/end_of_line', 4, 4377 'Line ends in whitespace. Consider deleting these extra spaces.') 4378 4379 # Check if the line is a header guard. 4380 is_header_guard = False 4381 if IsHeaderExtension(file_extension): 4382 cppvar = GetHeaderGuardCPPVariable(filename) 4383 if (line.startswith('#ifndef %s' % cppvar) or 4384 line.startswith('#define %s' % cppvar) or 4385 line.startswith('#endif // %s' % cppvar)): 4386 is_header_guard = True 4387 # #include lines and header guards can be long, since there's no clean way to 4388 # split them. 4389 # 4390 # URLs can be long too. It's possible to split these, but it makes them 4391 # harder to cut&paste. 4392 # 4393 # The "$Id:...$" comment may also get very long without it being the 4394 # developers fault. 4395 if (not line.startswith('#include') and not is_header_guard and 4396 not Match(r'^\s*//.*http(s?)://\S*$', line) and 4397 not Match(r'^\s*//\s*[^\s]*$', line) and 4398 not Match(r'^// \$Id:.*#[0-9]+ \$$', line)): 4399 line_width = GetLineWidth(line) 4400 if line_width > _line_length: 4401 error(filename, linenum, 'whitespace/line_length', 2, 4402 'Lines should be <= %i characters long' % _line_length) 4403 4404 if (cleansed_line.count(';') > 1 and 4405 # for loops are allowed two ;'s (and may run over two lines). 4406 cleansed_line.find('for') == -1 and 4407 (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or 4408 GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and 4409 # It's ok to have many commands in a switch case that fits in 1 line 4410 not ((cleansed_line.find('case ') != -1 or 4411 cleansed_line.find('default:') != -1) and 4412 cleansed_line.find('break;') != -1)): 4413 error(filename, linenum, 'whitespace/newline', 0, 4414 'More than one command on the same line') 4415 4416 # Some more style checks 4417 CheckBraces(filename, clean_lines, linenum, error) 4418 CheckTrailingSemicolon(filename, clean_lines, linenum, error) 4419 CheckEmptyBlockBody(filename, clean_lines, linenum, error) 4420 CheckSpacing(filename, clean_lines, linenum, nesting_state, error) 4421 CheckOperatorSpacing(filename, clean_lines, linenum, error) 4422 CheckParenthesisSpacing(filename, clean_lines, linenum, error) 4423 CheckCommaSpacing(filename, clean_lines, linenum, error) 4424 CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error) 4425 CheckSpacingForFunctionCall(filename, clean_lines, linenum, error) 4426 CheckCheck(filename, clean_lines, linenum, error) 4427 CheckAltTokens(filename, clean_lines, linenum, error) 4428 classinfo = nesting_state.InnermostClass() 4429 if classinfo: 4430 CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error) 4431 4432 4433_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$') 4434# Matches the first component of a filename delimited by -s and _s. That is: 4435# _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo' 4436# _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo' 4437# _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo' 4438# _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo' 4439_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+') 4440 4441 4442def _DropCommonSuffixes(filename): 4443 """Drops common suffixes like _test.cc or -inl.h from filename. 4444 4445 For example: 4446 >>> _DropCommonSuffixes('foo/foo-inl.h') 4447 'foo/foo' 4448 >>> _DropCommonSuffixes('foo/bar/foo.cc') 4449 'foo/bar/foo' 4450 >>> _DropCommonSuffixes('foo/foo_internal.h') 4451 'foo/foo' 4452 >>> _DropCommonSuffixes('foo/foo_unusualinternal.h') 4453 'foo/foo_unusualinternal' 4454 4455 Args: 4456 filename: The input filename. 4457 4458 Returns: 4459 The filename with the common suffix removed. 4460 """ 4461 for suffix in ('test.cc', 'regtest.cc', 'unittest.cc', 4462 'inl.h', 'impl.h', 'internal.h'): 4463 if (filename.endswith(suffix) and len(filename) > len(suffix) and 4464 filename[-len(suffix) - 1] in ('-', '_')): 4465 return filename[:-len(suffix) - 1] 4466 return os.path.splitext(filename)[0] 4467 4468 4469def _ClassifyInclude(fileinfo, include, is_system): 4470 """Figures out what kind of header 'include' is. 4471 4472 Args: 4473 fileinfo: The current file cpplint is running over. A FileInfo instance. 4474 include: The path to a #included file. 4475 is_system: True if the #include used <> rather than "". 4476 4477 Returns: 4478 One of the _XXX_HEADER constants. 4479 4480 For example: 4481 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True) 4482 _C_SYS_HEADER 4483 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True) 4484 _CPP_SYS_HEADER 4485 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False) 4486 _LIKELY_MY_HEADER 4487 >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'), 4488 ... 'bar/foo_other_ext.h', False) 4489 _POSSIBLE_MY_HEADER 4490 >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False) 4491 _OTHER_HEADER 4492 """ 4493 # This is a list of all standard c++ header files, except 4494 # those already checked for above. 4495 is_cpp_h = include in _CPP_HEADERS 4496 4497 if is_system: 4498 if is_cpp_h: 4499 return _CPP_SYS_HEADER 4500 else: 4501 return _C_SYS_HEADER 4502 4503 # If the target file and the include we're checking share a 4504 # basename when we drop common extensions, and the include 4505 # lives in . , then it's likely to be owned by the target file. 4506 target_dir, target_base = ( 4507 os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName()))) 4508 include_dir, include_base = os.path.split(_DropCommonSuffixes(include)) 4509 if target_base == include_base and ( 4510 include_dir == target_dir or 4511 include_dir == os.path.normpath(target_dir + '/../public')): 4512 return _LIKELY_MY_HEADER 4513 4514 # If the target and include share some initial basename 4515 # component, it's possible the target is implementing the 4516 # include, so it's allowed to be first, but we'll never 4517 # complain if it's not there. 4518 target_first_component = _RE_FIRST_COMPONENT.match(target_base) 4519 include_first_component = _RE_FIRST_COMPONENT.match(include_base) 4520 if (target_first_component and include_first_component and 4521 target_first_component.group(0) == 4522 include_first_component.group(0)): 4523 return _POSSIBLE_MY_HEADER 4524 4525 return _OTHER_HEADER 4526 4527 4528 4529def CheckIncludeLine(filename, clean_lines, linenum, include_state, error): 4530 """Check rules that are applicable to #include lines. 4531 4532 Strings on #include lines are NOT removed from elided line, to make 4533 certain tasks easier. However, to prevent false positives, checks 4534 applicable to #include lines in CheckLanguage must be put here. 4535 4536 Args: 4537 filename: The name of the current file. 4538 clean_lines: A CleansedLines instance containing the file. 4539 linenum: The number of the line to check. 4540 include_state: An _IncludeState instance in which the headers are inserted. 4541 error: The function to call with any errors found. 4542 """ 4543 fileinfo = FileInfo(filename) 4544 line = clean_lines.lines[linenum] 4545 4546 # "include" should use the new style "foo/bar.h" instead of just "bar.h" 4547 # Only do this check if the included header follows google naming 4548 # conventions. If not, assume that it's a 3rd party API that 4549 # requires special include conventions. 4550 # 4551 # We also make an exception for Lua headers, which follow google 4552 # naming convention but not the include convention. 4553 match = Match(r'#include\s*"([^/]+\.h)"', line) 4554 if match and not _THIRD_PARTY_HEADERS_PATTERN.match(match.group(1)): 4555 error(filename, linenum, 'build/include', 4, 4556 'Include the directory when naming .h files') 4557 4558 # we shouldn't include a file more than once. actually, there are a 4559 # handful of instances where doing so is okay, but in general it's 4560 # not. 4561 match = _RE_PATTERN_INCLUDE.search(line) 4562 if match: 4563 include = match.group(2) 4564 is_system = (match.group(1) == '<') 4565 duplicate_line = include_state.FindHeader(include) 4566 if duplicate_line >= 0: 4567 error(filename, linenum, 'build/include', 4, 4568 '"%s" already included at %s:%s' % 4569 (include, filename, duplicate_line)) 4570 elif (include.endswith('.cc') and 4571 os.path.dirname(fileinfo.RepositoryName()) != os.path.dirname(include)): 4572 error(filename, linenum, 'build/include', 4, 4573 'Do not include .cc files from other packages') 4574 elif not _THIRD_PARTY_HEADERS_PATTERN.match(include): 4575 include_state.include_list[-1].append((include, linenum)) 4576 4577 # We want to ensure that headers appear in the right order: 4578 # 1) for foo.cc, foo.h (preferred location) 4579 # 2) c system files 4580 # 3) cpp system files 4581 # 4) for foo.cc, foo.h (deprecated location) 4582 # 5) other google headers 4583 # 4584 # We classify each include statement as one of those 5 types 4585 # using a number of techniques. The include_state object keeps 4586 # track of the highest type seen, and complains if we see a 4587 # lower type after that. 4588 error_message = include_state.CheckNextIncludeOrder( 4589 _ClassifyInclude(fileinfo, include, is_system)) 4590 if error_message: 4591 error(filename, linenum, 'build/include_order', 4, 4592 '%s. Should be: %s.h, c system, c++ system, other.' % 4593 (error_message, fileinfo.BaseName())) 4594 canonical_include = include_state.CanonicalizeAlphabeticalOrder(include) 4595 if not include_state.IsInAlphabeticalOrder( 4596 clean_lines, linenum, canonical_include): 4597 error(filename, linenum, 'build/include_alpha', 4, 4598 'Include "%s" not in alphabetical order' % include) 4599 include_state.SetLastHeader(canonical_include) 4600 4601 4602 4603def _GetTextInside(text, start_pattern): 4604 r"""Retrieves all the text between matching open and close parentheses. 4605 4606 Given a string of lines and a regular expression string, retrieve all the text 4607 following the expression and between opening punctuation symbols like 4608 (, [, or {, and the matching close-punctuation symbol. This properly nested 4609 occurrences of the punctuations, so for the text like 4610 printf(a(), b(c())); 4611 a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'. 4612 start_pattern must match string having an open punctuation symbol at the end. 4613 4614 Args: 4615 text: The lines to extract text. Its comments and strings must be elided. 4616 It can be single line and can span multiple lines. 4617 start_pattern: The regexp string indicating where to start extracting 4618 the text. 4619 Returns: 4620 The extracted text. 4621 None if either the opening string or ending punctuation could not be found. 4622 """ 4623 # TODO(unknown): Audit cpplint.py to see what places could be profitably 4624 # rewritten to use _GetTextInside (and use inferior regexp matching today). 4625 4626 # Give opening punctuations to get the matching close-punctuations. 4627 matching_punctuation = {'(': ')', '{': '}', '[': ']'} 4628 closing_punctuation = set(matching_punctuation.itervalues()) 4629 4630 # Find the position to start extracting text. 4631 match = re.search(start_pattern, text, re.M) 4632 if not match: # start_pattern not found in text. 4633 return None 4634 start_position = match.end(0) 4635 4636 assert start_position > 0, ( 4637 'start_pattern must ends with an opening punctuation.') 4638 assert text[start_position - 1] in matching_punctuation, ( 4639 'start_pattern must ends with an opening punctuation.') 4640 # Stack of closing punctuations we expect to have in text after position. 4641 punctuation_stack = [matching_punctuation[text[start_position - 1]]] 4642 position = start_position 4643 while punctuation_stack and position < len(text): 4644 if text[position] == punctuation_stack[-1]: 4645 punctuation_stack.pop() 4646 elif text[position] in closing_punctuation: 4647 # A closing punctuation without matching opening punctuations. 4648 return None 4649 elif text[position] in matching_punctuation: 4650 punctuation_stack.append(matching_punctuation[text[position]]) 4651 position += 1 4652 if punctuation_stack: 4653 # Opening punctuations left without matching close-punctuations. 4654 return None 4655 # punctuations match. 4656 return text[start_position:position - 1] 4657 4658 4659# Patterns for matching call-by-reference parameters. 4660# 4661# Supports nested templates up to 2 levels deep using this messy pattern: 4662# < (?: < (?: < [^<>]* 4663# > 4664# | [^<>] )* 4665# > 4666# | [^<>] )* 4667# > 4668_RE_PATTERN_IDENT = r'[_a-zA-Z]\w*' # =~ [[:alpha:]][[:alnum:]]* 4669_RE_PATTERN_TYPE = ( 4670 r'(?:const\s+)?(?:typename\s+|class\s+|struct\s+|union\s+|enum\s+)?' 4671 r'(?:\w|' 4672 r'\s*<(?:<(?:<[^<>]*>|[^<>])*>|[^<>])*>|' 4673 r'::)+') 4674# A call-by-reference parameter ends with '& identifier'. 4675_RE_PATTERN_REF_PARAM = re.compile( 4676 r'(' + _RE_PATTERN_TYPE + r'(?:\s*(?:\bconst\b|[*]))*\s*' 4677 r'&\s*' + _RE_PATTERN_IDENT + r')\s*(?:=[^,()]+)?[,)]') 4678# A call-by-const-reference parameter either ends with 'const& identifier' 4679# or looks like 'const type& identifier' when 'type' is atomic. 4680_RE_PATTERN_CONST_REF_PARAM = ( 4681 r'(?:.*\s*\bconst\s*&\s*' + _RE_PATTERN_IDENT + 4682 r'|const\s+' + _RE_PATTERN_TYPE + r'\s*&\s*' + _RE_PATTERN_IDENT + r')') 4683# Stream types. 4684_RE_PATTERN_REF_STREAM_PARAM = ( 4685 r'(?:.*stream\s*&\s*' + _RE_PATTERN_IDENT + r')') 4686 4687 4688def CheckLanguage(filename, clean_lines, linenum, file_extension, 4689 include_state, nesting_state, error): 4690 """Checks rules from the 'C++ language rules' section of cppguide.html. 4691 4692 Some of these rules are hard to test (function overloading, using 4693 uint32 inappropriately), but we do the best we can. 4694 4695 Args: 4696 filename: The name of the current file. 4697 clean_lines: A CleansedLines instance containing the file. 4698 linenum: The number of the line to check. 4699 file_extension: The extension (without the dot) of the filename. 4700 include_state: An _IncludeState instance in which the headers are inserted. 4701 nesting_state: A NestingState instance which maintains information about 4702 the current stack of nested blocks being parsed. 4703 error: The function to call with any errors found. 4704 """ 4705 # If the line is empty or consists of entirely a comment, no need to 4706 # check it. 4707 line = clean_lines.elided[linenum] 4708 if not line: 4709 return 4710 4711 match = _RE_PATTERN_INCLUDE.search(line) 4712 if match: 4713 CheckIncludeLine(filename, clean_lines, linenum, include_state, error) 4714 return 4715 4716 # Reset include state across preprocessor directives. This is meant 4717 # to silence warnings for conditional includes. 4718 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line) 4719 if match: 4720 include_state.ResetSection(match.group(1)) 4721 4722 # Make Windows paths like Unix. 4723 fullname = os.path.abspath(filename).replace('\\', '/') 4724 4725 # Perform other checks now that we are sure that this is not an include line 4726 CheckCasts(filename, clean_lines, linenum, error) 4727 CheckGlobalStatic(filename, clean_lines, linenum, error) 4728 CheckPrintf(filename, clean_lines, linenum, error) 4729 4730 if IsHeaderExtension(file_extension): 4731 # TODO(unknown): check that 1-arg constructors are explicit. 4732 # How to tell it's a constructor? 4733 # (handled in CheckForNonStandardConstructs for now) 4734 # TODO(unknown): check that classes declare or disable copy/assign 4735 # (level 1 error) 4736 pass 4737 4738 # Check if people are using the verboten C basic types. The only exception 4739 # we regularly allow is "unsigned short port" for port. 4740 if Search(r'\bshort port\b', line): 4741 if not Search(r'\bunsigned short port\b', line): 4742 error(filename, linenum, 'runtime/int', 4, 4743 'Use "unsigned short" for ports, not "short"') 4744 else: 4745 match = Search(r'\b(short|long(?! +double)|long long)\b', line) 4746 if match: 4747 error(filename, linenum, 'runtime/int', 4, 4748 'Use int16/int64/etc, rather than the C type %s' % match.group(1)) 4749 4750 # Check if some verboten operator overloading is going on 4751 # TODO(unknown): catch out-of-line unary operator&: 4752 # class X {}; 4753 # int operator&(const X& x) { return 42; } // unary operator& 4754 # The trick is it's hard to tell apart from binary operator&: 4755 # class Y { int operator&(const Y& x) { return 23; } }; // binary operator& 4756 if Search(r'\boperator\s*&\s*\(\s*\)', line): 4757 error(filename, linenum, 'runtime/operator', 4, 4758 'Unary operator& is dangerous. Do not use it.') 4759 4760 # Check for suspicious usage of "if" like 4761 # } if (a == b) { 4762 if Search(r'\}\s*if\s*\(', line): 4763 error(filename, linenum, 'readability/braces', 4, 4764 'Did you mean "else if"? If not, start a new line for "if".') 4765 4766 # Check for potential format string bugs like printf(foo). 4767 # We constrain the pattern not to pick things like DocidForPrintf(foo). 4768 # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str()) 4769 # TODO(unknown): Catch the following case. Need to change the calling 4770 # convention of the whole function to process multiple line to handle it. 4771 # printf( 4772 # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line); 4773 printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(') 4774 if printf_args: 4775 match = Match(r'([\w.\->()]+)$', printf_args) 4776 if match and match.group(1) != '__VA_ARGS__': 4777 function_name = re.search(r'\b((?:string)?printf)\s*\(', 4778 line, re.I).group(1) 4779 error(filename, linenum, 'runtime/printf', 4, 4780 'Potential format string bug. Do %s("%%s", %s) instead.' 4781 % (function_name, match.group(1))) 4782 4783 # Check for potential memset bugs like memset(buf, sizeof(buf), 0). 4784 match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line) 4785 if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)): 4786 error(filename, linenum, 'runtime/memset', 4, 4787 'Did you mean "memset(%s, 0, %s)"?' 4788 % (match.group(1), match.group(2))) 4789 4790 if Search(r'\busing namespace\b', line): 4791 error(filename, linenum, 'build/namespaces', 5, 4792 'Do not use namespace using-directives. ' 4793 'Use using-declarations instead.') 4794 4795 # Detect variable-length arrays. 4796 match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line) 4797 if (match and match.group(2) != 'return' and match.group(2) != 'delete' and 4798 match.group(3).find(']') == -1): 4799 # Split the size using space and arithmetic operators as delimiters. 4800 # If any of the resulting tokens are not compile time constants then 4801 # report the error. 4802 tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3)) 4803 is_const = True 4804 skip_next = False 4805 for tok in tokens: 4806 if skip_next: 4807 skip_next = False 4808 continue 4809 4810 if Search(r'sizeof\(.+\)', tok): continue 4811 if Search(r'arraysize\(\w+\)', tok): continue 4812 4813 tok = tok.lstrip('(') 4814 tok = tok.rstrip(')') 4815 if not tok: continue 4816 if Match(r'\d+', tok): continue 4817 if Match(r'0[xX][0-9a-fA-F]+', tok): continue 4818 if Match(r'k[A-Z0-9]\w*', tok): continue 4819 if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue 4820 if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue 4821 # A catch all for tricky sizeof cases, including 'sizeof expression', 4822 # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)' 4823 # requires skipping the next token because we split on ' ' and '*'. 4824 if tok.startswith('sizeof'): 4825 skip_next = True 4826 continue 4827 is_const = False 4828 break 4829 if not is_const: 4830 error(filename, linenum, 'runtime/arrays', 1, 4831 'Do not use variable-length arrays. Use an appropriately named ' 4832 "('k' followed by CamelCase) compile-time constant for the size.") 4833 4834 # Check for use of unnamed namespaces in header files. Registration 4835 # macros are typically OK, so we allow use of "namespace {" on lines 4836 # that end with backslashes. 4837 if (IsHeaderExtension(file_extension) 4838 and Search(r'\bnamespace\s*{', line) 4839 and line[-1] != '\\'): 4840 error(filename, linenum, 'build/namespaces', 4, 4841 'Do not use unnamed namespaces in header files. See ' 4842 'https://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces' 4843 ' for more information.') 4844 4845 4846def CheckGlobalStatic(filename, clean_lines, linenum, error): 4847 """Check for unsafe global or static objects. 4848 4849 Args: 4850 filename: The name of the current file. 4851 clean_lines: A CleansedLines instance containing the file. 4852 linenum: The number of the line to check. 4853 error: The function to call with any errors found. 4854 """ 4855 line = clean_lines.elided[linenum] 4856 4857 # Match two lines at a time to support multiline declarations 4858 if linenum + 1 < clean_lines.NumLines() and not Search(r'[;({]', line): 4859 line += clean_lines.elided[linenum + 1].strip() 4860 4861 # Check for people declaring static/global STL strings at the top level. 4862 # This is dangerous because the C++ language does not guarantee that 4863 # globals with constructors are initialized before the first access, and 4864 # also because globals can be destroyed when some threads are still running. 4865 # TODO(unknown): Generalize this to also find static unique_ptr instances. 4866 # TODO(unknown): File bugs for clang-tidy to find these. 4867 match = Match( 4868 r'((?:|static +)(?:|const +))(?::*std::)?string( +const)? +' 4869 r'([a-zA-Z0-9_:]+)\b(.*)', 4870 line) 4871 4872 # Remove false positives: 4873 # - String pointers (as opposed to values). 4874 # string *pointer 4875 # const string *pointer 4876 # string const *pointer 4877 # string *const pointer 4878 # 4879 # - Functions and template specializations. 4880 # string Function<Type>(... 4881 # string Class<Type>::Method(... 4882 # 4883 # - Operators. These are matched separately because operator names 4884 # cross non-word boundaries, and trying to match both operators 4885 # and functions at the same time would decrease accuracy of 4886 # matching identifiers. 4887 # string Class::operator*() 4888 if (match and 4889 not Search(r'\bstring\b(\s+const)?\s*[\*\&]\s*(const\s+)?\w', line) and 4890 not Search(r'\boperator\W', line) and 4891 not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)*\s*\(([^"]|$)', match.group(4))): 4892 if Search(r'\bconst\b', line): 4893 error(filename, linenum, 'runtime/string', 4, 4894 'For a static/global string constant, use a C style string ' 4895 'instead: "%schar%s %s[]".' % 4896 (match.group(1), match.group(2) or '', match.group(3))) 4897 else: 4898 error(filename, linenum, 'runtime/string', 4, 4899 'Static/global string variables are not permitted.') 4900 4901 if (Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line) or 4902 Search(r'\b([A-Za-z0-9_]*_)\(CHECK_NOTNULL\(\1\)\)', line)): 4903 error(filename, linenum, 'runtime/init', 4, 4904 'You seem to be initializing a member variable with itself.') 4905 4906 4907def CheckPrintf(filename, clean_lines, linenum, error): 4908 """Check for printf related issues. 4909 4910 Args: 4911 filename: The name of the current file. 4912 clean_lines: A CleansedLines instance containing the file. 4913 linenum: The number of the line to check. 4914 error: The function to call with any errors found. 4915 """ 4916 line = clean_lines.elided[linenum] 4917 4918 # When snprintf is used, the second argument shouldn't be a literal. 4919 match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line) 4920 if match and match.group(2) != '0': 4921 # If 2nd arg is zero, snprintf is used to calculate size. 4922 error(filename, linenum, 'runtime/printf', 3, 4923 'If you can, use sizeof(%s) instead of %s as the 2nd arg ' 4924 'to snprintf.' % (match.group(1), match.group(2))) 4925 4926 # Check if some verboten C functions are being used. 4927 if Search(r'\bsprintf\s*\(', line): 4928 error(filename, linenum, 'runtime/printf', 5, 4929 'Never use sprintf. Use snprintf instead.') 4930 match = Search(r'\b(strcpy|strcat)\s*\(', line) 4931 if match: 4932 error(filename, linenum, 'runtime/printf', 4, 4933 'Almost always, snprintf is better than %s' % match.group(1)) 4934 4935 4936def IsDerivedFunction(clean_lines, linenum): 4937 """Check if current line contains an inherited function. 4938 4939 Args: 4940 clean_lines: A CleansedLines instance containing the file. 4941 linenum: The number of the line to check. 4942 Returns: 4943 True if current line contains a function with "override" 4944 virt-specifier. 4945 """ 4946 # Scan back a few lines for start of current function 4947 for i in xrange(linenum, max(-1, linenum - 10), -1): 4948 match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i]) 4949 if match: 4950 # Look for "override" after the matching closing parenthesis 4951 line, _, closing_paren = CloseExpression( 4952 clean_lines, i, len(match.group(1))) 4953 return (closing_paren >= 0 and 4954 Search(r'\boverride\b', line[closing_paren:])) 4955 return False 4956 4957 4958def IsOutOfLineMethodDefinition(clean_lines, linenum): 4959 """Check if current line contains an out-of-line method definition. 4960 4961 Args: 4962 clean_lines: A CleansedLines instance containing the file. 4963 linenum: The number of the line to check. 4964 Returns: 4965 True if current line contains an out-of-line method definition. 4966 """ 4967 # Scan back a few lines for start of current function 4968 for i in xrange(linenum, max(-1, linenum - 10), -1): 4969 if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]): 4970 return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None 4971 return False 4972 4973 4974def IsInitializerList(clean_lines, linenum): 4975 """Check if current line is inside constructor initializer list. 4976 4977 Args: 4978 clean_lines: A CleansedLines instance containing the file. 4979 linenum: The number of the line to check. 4980 Returns: 4981 True if current line appears to be inside constructor initializer 4982 list, False otherwise. 4983 """ 4984 for i in xrange(linenum, 1, -1): 4985 line = clean_lines.elided[i] 4986 if i == linenum: 4987 remove_function_body = Match(r'^(.*)\{\s*$', line) 4988 if remove_function_body: 4989 line = remove_function_body.group(1) 4990 4991 if Search(r'\s:\s*\w+[({]', line): 4992 # A lone colon tend to indicate the start of a constructor 4993 # initializer list. It could also be a ternary operator, which 4994 # also tend to appear in constructor initializer lists as 4995 # opposed to parameter lists. 4996 return True 4997 if Search(r'\}\s*,\s*$', line): 4998 # A closing brace followed by a comma is probably the end of a 4999 # brace-initialized member in constructor initializer list. 5000 return True 5001 if Search(r'[{};]\s*$', line): 5002 # Found one of the following: 5003 # - A closing brace or semicolon, probably the end of the previous 5004 # function. 5005 # - An opening brace, probably the start of current class or namespace. 5006 # 5007 # Current line is probably not inside an initializer list since 5008 # we saw one of those things without seeing the starting colon. 5009 return False 5010 5011 # Got to the beginning of the file without seeing the start of 5012 # constructor initializer list. 5013 return False 5014 5015 5016def CheckForNonConstReference(filename, clean_lines, linenum, 5017 nesting_state, error): 5018 """Check for non-const references. 5019 5020 Separate from CheckLanguage since it scans backwards from current 5021 line, instead of scanning forward. 5022 5023 Args: 5024 filename: The name of the current file. 5025 clean_lines: A CleansedLines instance containing the file. 5026 linenum: The number of the line to check. 5027 nesting_state: A NestingState instance which maintains information about 5028 the current stack of nested blocks being parsed. 5029 error: The function to call with any errors found. 5030 """ 5031 # Do nothing if there is no '&' on current line. 5032 line = clean_lines.elided[linenum] 5033 if '&' not in line: 5034 return 5035 5036 # If a function is inherited, current function doesn't have much of 5037 # a choice, so any non-const references should not be blamed on 5038 # derived function. 5039 if IsDerivedFunction(clean_lines, linenum): 5040 return 5041 5042 # Don't warn on out-of-line method definitions, as we would warn on the 5043 # in-line declaration, if it isn't marked with 'override'. 5044 if IsOutOfLineMethodDefinition(clean_lines, linenum): 5045 return 5046 5047 # Long type names may be broken across multiple lines, usually in one 5048 # of these forms: 5049 # LongType 5050 # ::LongTypeContinued &identifier 5051 # LongType:: 5052 # LongTypeContinued &identifier 5053 # LongType< 5054 # ...>::LongTypeContinued &identifier 5055 # 5056 # If we detected a type split across two lines, join the previous 5057 # line to current line so that we can match const references 5058 # accordingly. 5059 # 5060 # Note that this only scans back one line, since scanning back 5061 # arbitrary number of lines would be expensive. If you have a type 5062 # that spans more than 2 lines, please use a typedef. 5063 if linenum > 1: 5064 previous = None 5065 if Match(r'\s*::(?:[\w<>]|::)+\s*&\s*\S', line): 5066 # previous_line\n + ::current_line 5067 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+[\w<>])\s*$', 5068 clean_lines.elided[linenum - 1]) 5069 elif Match(r'\s*[a-zA-Z_]([\w<>]|::)+\s*&\s*\S', line): 5070 # previous_line::\n + current_line 5071 previous = Search(r'\b((?:const\s*)?(?:[\w<>]|::)+::)\s*$', 5072 clean_lines.elided[linenum - 1]) 5073 if previous: 5074 line = previous.group(1) + line.lstrip() 5075 else: 5076 # Check for templated parameter that is split across multiple lines 5077 endpos = line.rfind('>') 5078 if endpos > -1: 5079 (_, startline, startpos) = ReverseCloseExpression( 5080 clean_lines, linenum, endpos) 5081 if startpos > -1 and startline < linenum: 5082 # Found the matching < on an earlier line, collect all 5083 # pieces up to current line. 5084 line = '' 5085 for i in xrange(startline, linenum + 1): 5086 line += clean_lines.elided[i].strip() 5087 5088 # Check for non-const references in function parameters. A single '&' may 5089 # found in the following places: 5090 # inside expression: binary & for bitwise AND 5091 # inside expression: unary & for taking the address of something 5092 # inside declarators: reference parameter 5093 # We will exclude the first two cases by checking that we are not inside a 5094 # function body, including one that was just introduced by a trailing '{'. 5095 # TODO(unknown): Doesn't account for 'catch(Exception& e)' [rare]. 5096 if (nesting_state.previous_stack_top and 5097 not (isinstance(nesting_state.previous_stack_top, _ClassInfo) or 5098 isinstance(nesting_state.previous_stack_top, _NamespaceInfo))): 5099 # Not at toplevel, not within a class, and not within a namespace 5100 return 5101 5102 # Avoid initializer lists. We only need to scan back from the 5103 # current line for something that starts with ':'. 5104 # 5105 # We don't need to check the current line, since the '&' would 5106 # appear inside the second set of parentheses on the current line as 5107 # opposed to the first set. 5108 if linenum > 0: 5109 for i in xrange(linenum - 1, max(0, linenum - 10), -1): 5110 previous_line = clean_lines.elided[i] 5111 if not Search(r'[),]\s*$', previous_line): 5112 break 5113 if Match(r'^\s*:\s+\S', previous_line): 5114 return 5115 5116 # Avoid preprocessors 5117 if Search(r'\\\s*$', line): 5118 return 5119 5120 # Avoid constructor initializer lists 5121 if IsInitializerList(clean_lines, linenum): 5122 return 5123 5124 # We allow non-const references in a few standard places, like functions 5125 # called "swap()" or iostream operators like "<<" or ">>". Do not check 5126 # those function parameters. 5127 # 5128 # We also accept & in static_assert, which looks like a function but 5129 # it's actually a declaration expression. 5130 whitelisted_functions = (r'(?:[sS]wap(?:<\w:+>)?|' 5131 r'operator\s*[<>][<>]|' 5132 r'static_assert|COMPILE_ASSERT' 5133 r')\s*\(') 5134 if Search(whitelisted_functions, line): 5135 return 5136 elif not Search(r'\S+\([^)]*$', line): 5137 # Don't see a whitelisted function on this line. Actually we 5138 # didn't see any function name on this line, so this is likely a 5139 # multi-line parameter list. Try a bit harder to catch this case. 5140 for i in xrange(2): 5141 if (linenum > i and 5142 Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])): 5143 return 5144 5145 decls = ReplaceAll(r'{[^}]*}', ' ', line) # exclude function body 5146 for parameter in re.findall(_RE_PATTERN_REF_PARAM, decls): 5147 if (not Match(_RE_PATTERN_CONST_REF_PARAM, parameter) and 5148 not Match(_RE_PATTERN_REF_STREAM_PARAM, parameter)): 5149 error(filename, linenum, 'runtime/references', 2, 5150 'Is this a non-const reference? ' 5151 'If so, make const or use a pointer: ' + 5152 ReplaceAll(' *<', '<', parameter)) 5153 5154 5155def CheckCasts(filename, clean_lines, linenum, error): 5156 """Various cast related checks. 5157 5158 Args: 5159 filename: The name of the current file. 5160 clean_lines: A CleansedLines instance containing the file. 5161 linenum: The number of the line to check. 5162 error: The function to call with any errors found. 5163 """ 5164 line = clean_lines.elided[linenum] 5165 5166 # Check to see if they're using an conversion function cast. 5167 # I just try to capture the most common basic types, though there are more. 5168 # Parameterless conversion functions, such as bool(), are allowed as they are 5169 # probably a member operator declaration or default constructor. 5170 match = Search( 5171 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b' 5172 r'(int|float|double|bool|char|int32|uint32|int64|uint64)' 5173 r'(\([^)].*)', line) 5174 expecting_function = ExpectingFunctionArgs(clean_lines, linenum) 5175 if match and not expecting_function: 5176 matched_type = match.group(2) 5177 5178 # matched_new_or_template is used to silence two false positives: 5179 # - New operators 5180 # - Template arguments with function types 5181 # 5182 # For template arguments, we match on types immediately following 5183 # an opening bracket without any spaces. This is a fast way to 5184 # silence the common case where the function type is the first 5185 # template argument. False negative with less-than comparison is 5186 # avoided because those operators are usually followed by a space. 5187 # 5188 # function<double(double)> // bracket + no space = false positive 5189 # value < double(42) // bracket + space = true positive 5190 matched_new_or_template = match.group(1) 5191 5192 # Avoid arrays by looking for brackets that come after the closing 5193 # parenthesis. 5194 if Match(r'\([^()]+\)\s*\[', match.group(3)): 5195 return 5196 5197 # Other things to ignore: 5198 # - Function pointers 5199 # - Casts to pointer types 5200 # - Placement new 5201 # - Alias declarations 5202 matched_funcptr = match.group(3) 5203 if (matched_new_or_template is None and 5204 not (matched_funcptr and 5205 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(', 5206 matched_funcptr) or 5207 matched_funcptr.startswith('(*)'))) and 5208 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and 5209 not Search(r'new\(\S+\)\s*' + matched_type, line)): 5210 error(filename, linenum, 'readability/casting', 4, 5211 'Using deprecated casting style. ' 5212 'Use static_cast<%s>(...) instead' % 5213 matched_type) 5214 5215 if not expecting_function: 5216 CheckCStyleCast(filename, clean_lines, linenum, 'static_cast', 5217 r'\((int|float|double|bool|char|u?int(16|32|64))\)', error) 5218 5219 # This doesn't catch all cases. Consider (const char * const)"hello". 5220 # 5221 # (char *) "foo" should always be a const_cast (reinterpret_cast won't 5222 # compile). 5223 if CheckCStyleCast(filename, clean_lines, linenum, 'const_cast', 5224 r'\((char\s?\*+\s?)\)\s*"', error): 5225 pass 5226 else: 5227 # Check pointer casts for other than string constants 5228 CheckCStyleCast(filename, clean_lines, linenum, 'reinterpret_cast', 5229 r'\((\w+\s?\*+\s?)\)', error) 5230 5231 # In addition, we look for people taking the address of a cast. This 5232 # is dangerous -- casts can assign to temporaries, so the pointer doesn't 5233 # point where you think. 5234 # 5235 # Some non-identifier character is required before the '&' for the 5236 # expression to be recognized as a cast. These are casts: 5237 # expression = &static_cast<int*>(temporary()); 5238 # function(&(int*)(temporary())); 5239 # 5240 # This is not a cast: 5241 # reference_type&(int* function_param); 5242 match = Search( 5243 r'(?:[^\w]&\(([^)*][^)]*)\)[\w(])|' 5244 r'(?:[^\w]&(static|dynamic|down|reinterpret)_cast\b)', line) 5245 if match: 5246 # Try a better error message when the & is bound to something 5247 # dereferenced by the casted pointer, as opposed to the casted 5248 # pointer itself. 5249 parenthesis_error = False 5250 match = Match(r'^(.*&(?:static|dynamic|down|reinterpret)_cast\b)<', line) 5251 if match: 5252 _, y1, x1 = CloseExpression(clean_lines, linenum, len(match.group(1))) 5253 if x1 >= 0 and clean_lines.elided[y1][x1] == '(': 5254 _, y2, x2 = CloseExpression(clean_lines, y1, x1) 5255 if x2 >= 0: 5256 extended_line = clean_lines.elided[y2][x2:] 5257 if y2 < clean_lines.NumLines() - 1: 5258 extended_line += clean_lines.elided[y2 + 1] 5259 if Match(r'\s*(?:->|\[)', extended_line): 5260 parenthesis_error = True 5261 5262 if parenthesis_error: 5263 error(filename, linenum, 'readability/casting', 4, 5264 ('Are you taking an address of something dereferenced ' 5265 'from a cast? Wrapping the dereferenced expression in ' 5266 'parentheses will make the binding more obvious')) 5267 else: 5268 error(filename, linenum, 'runtime/casting', 4, 5269 ('Are you taking an address of a cast? ' 5270 'This is dangerous: could be a temp var. ' 5271 'Take the address before doing the cast, rather than after')) 5272 5273 5274def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error): 5275 """Checks for a C-style cast by looking for the pattern. 5276 5277 Args: 5278 filename: The name of the current file. 5279 clean_lines: A CleansedLines instance containing the file. 5280 linenum: The number of the line to check. 5281 cast_type: The string for the C++ cast to recommend. This is either 5282 reinterpret_cast, static_cast, or const_cast, depending. 5283 pattern: The regular expression used to find C-style casts. 5284 error: The function to call with any errors found. 5285 5286 Returns: 5287 True if an error was emitted. 5288 False otherwise. 5289 """ 5290 line = clean_lines.elided[linenum] 5291 match = Search(pattern, line) 5292 if not match: 5293 return False 5294 5295 # Exclude lines with keywords that tend to look like casts 5296 context = line[0:match.start(1) - 1] 5297 if Match(r'.*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$', context): 5298 return False 5299 5300 # Try expanding current context to see if we one level of 5301 # parentheses inside a macro. 5302 if linenum > 0: 5303 for i in xrange(linenum - 1, max(0, linenum - 5), -1): 5304 context = clean_lines.elided[i] + context 5305 if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context): 5306 return False 5307 5308 # operator++(int) and operator--(int) 5309 if context.endswith(' operator++') or context.endswith(' operator--'): 5310 return False 5311 5312 # A single unnamed argument for a function tends to look like old style cast. 5313 # If we see those, don't issue warnings for deprecated casts. 5314 remainder = line[match.end(0):] 5315 if Match(r'^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)', 5316 remainder): 5317 return False 5318 5319 # At this point, all that should be left is actual casts. 5320 error(filename, linenum, 'readability/casting', 4, 5321 'Using C-style cast. Use %s<%s>(...) instead' % 5322 (cast_type, match.group(1))) 5323 5324 return True 5325 5326 5327def ExpectingFunctionArgs(clean_lines, linenum): 5328 """Checks whether where function type arguments are expected. 5329 5330 Args: 5331 clean_lines: A CleansedLines instance containing the file. 5332 linenum: The number of the line to check. 5333 5334 Returns: 5335 True if the line at 'linenum' is inside something that expects arguments 5336 of function types. 5337 """ 5338 line = clean_lines.elided[linenum] 5339 return (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or 5340 (linenum >= 2 and 5341 (Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\((?:\S+,)?\s*$', 5342 clean_lines.elided[linenum - 1]) or 5343 Match(r'^\s*MOCK_(?:CONST_)?METHOD\d+(?:_T)?\(\s*$', 5344 clean_lines.elided[linenum - 2]) or 5345 Search(r'\bstd::m?function\s*\<\s*$', 5346 clean_lines.elided[linenum - 1])))) 5347 5348 5349_HEADERS_CONTAINING_TEMPLATES = ( 5350 ('<deque>', ('deque',)), 5351 ('<functional>', ('unary_function', 'binary_function', 5352 'plus', 'minus', 'multiplies', 'divides', 'modulus', 5353 'negate', 5354 'equal_to', 'not_equal_to', 'greater', 'less', 5355 'greater_equal', 'less_equal', 5356 'logical_and', 'logical_or', 'logical_not', 5357 'unary_negate', 'not1', 'binary_negate', 'not2', 5358 'bind1st', 'bind2nd', 5359 'pointer_to_unary_function', 5360 'pointer_to_binary_function', 5361 'ptr_fun', 5362 'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t', 5363 'mem_fun_ref_t', 5364 'const_mem_fun_t', 'const_mem_fun1_t', 5365 'const_mem_fun_ref_t', 'const_mem_fun1_ref_t', 5366 'mem_fun_ref', 5367 )), 5368 ('<limits>', ('numeric_limits',)), 5369 ('<list>', ('list',)), 5370 ('<map>', ('map', 'multimap',)), 5371 ('<memory>', ('allocator', 'make_shared', 'make_unique', 'shared_ptr', 5372 'unique_ptr', 'weak_ptr')), 5373 ('<queue>', ('queue', 'priority_queue',)), 5374 ('<set>', ('set', 'multiset',)), 5375 ('<stack>', ('stack',)), 5376 ('<string>', ('char_traits', 'basic_string',)), 5377 ('<tuple>', ('tuple',)), 5378 ('<unordered_map>', ('unordered_map', 'unordered_multimap')), 5379 ('<unordered_set>', ('unordered_set', 'unordered_multiset')), 5380 ('<utility>', ('pair',)), 5381 ('<vector>', ('vector',)), 5382 5383 # gcc extensions. 5384 # Note: std::hash is their hash, ::hash is our hash 5385 ('<hash_map>', ('hash_map', 'hash_multimap',)), 5386 ('<hash_set>', ('hash_set', 'hash_multiset',)), 5387 ('<slist>', ('slist',)), 5388 ) 5389 5390_HEADERS_MAYBE_TEMPLATES = ( 5391 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort', 5392 'transform', 5393 )), 5394 ('<utility>', ('forward', 'make_pair', 'move', 'swap')), 5395 ) 5396 5397_RE_PATTERN_STRING = re.compile(r'\bstring\b') 5398 5399_re_pattern_headers_maybe_templates = [] 5400for _header, _templates in _HEADERS_MAYBE_TEMPLATES: 5401 for _template in _templates: 5402 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or 5403 # type::max(). 5404 _re_pattern_headers_maybe_templates.append( 5405 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'), 5406 _template, 5407 _header)) 5408 5409# Other scripts may reach in and modify this pattern. 5410_re_pattern_templates = [] 5411for _header, _templates in _HEADERS_CONTAINING_TEMPLATES: 5412 for _template in _templates: 5413 _re_pattern_templates.append( 5414 (re.compile(r'(\<|\b)' + _template + r'\s*\<'), 5415 _template + '<>', 5416 _header)) 5417 5418 5419def FilesBelongToSameModule(filename_cc, filename_h): 5420 """Check if these two filenames belong to the same module. 5421 5422 The concept of a 'module' here is a as follows: 5423 foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the 5424 same 'module' if they are in the same directory. 5425 some/path/public/xyzzy and some/path/internal/xyzzy are also considered 5426 to belong to the same module here. 5427 5428 If the filename_cc contains a longer path than the filename_h, for example, 5429 '/absolute/path/to/base/sysinfo.cc', and this file would include 5430 'base/sysinfo.h', this function also produces the prefix needed to open the 5431 header. This is used by the caller of this function to more robustly open the 5432 header file. We don't have access to the real include paths in this context, 5433 so we need this guesswork here. 5434 5435 Known bugs: tools/base/bar.cc and base/bar.h belong to the same module 5436 according to this implementation. Because of this, this function gives 5437 some false positives. This should be sufficiently rare in practice. 5438 5439 Args: 5440 filename_cc: is the path for the .cc file 5441 filename_h: is the path for the header path 5442 5443 Returns: 5444 Tuple with a bool and a string: 5445 bool: True if filename_cc and filename_h belong to the same module. 5446 string: the additional prefix needed to open the header file. 5447 """ 5448 5449 fileinfo = FileInfo(filename_cc) 5450 if not fileinfo.IsSource(): 5451 return (False, '') 5452 filename_cc = filename_cc[:-len(fileinfo.Extension())] 5453 matched_test_suffix = Search(_TEST_FILE_SUFFIX, fileinfo.BaseName()) 5454 if matched_test_suffix: 5455 filename_cc = filename_cc[:-len(matched_test_suffix.group(1))] 5456 filename_cc = filename_cc.replace('/public/', '/') 5457 filename_cc = filename_cc.replace('/internal/', '/') 5458 5459 if not filename_h.endswith('.h'): 5460 return (False, '') 5461 filename_h = filename_h[:-len('.h')] 5462 if filename_h.endswith('-inl'): 5463 filename_h = filename_h[:-len('-inl')] 5464 filename_h = filename_h.replace('/public/', '/') 5465 filename_h = filename_h.replace('/internal/', '/') 5466 5467 files_belong_to_same_module = filename_cc.endswith(filename_h) 5468 common_path = '' 5469 if files_belong_to_same_module: 5470 common_path = filename_cc[:-len(filename_h)] 5471 return files_belong_to_same_module, common_path 5472 5473 5474def UpdateIncludeState(filename, include_dict, io=codecs): 5475 """Fill up the include_dict with new includes found from the file. 5476 5477 Args: 5478 filename: the name of the header to read. 5479 include_dict: a dictionary in which the headers are inserted. 5480 io: The io factory to use to read the file. Provided for testability. 5481 5482 Returns: 5483 True if a header was successfully added. False otherwise. 5484 """ 5485 headerfile = None 5486 try: 5487 headerfile = io.open(filename, 'r', 'utf8', 'replace') 5488 except IOError: 5489 return False 5490 linenum = 0 5491 for line in headerfile: 5492 linenum += 1 5493 clean_line = CleanseComments(line) 5494 match = _RE_PATTERN_INCLUDE.search(clean_line) 5495 if match: 5496 include = match.group(2) 5497 include_dict.setdefault(include, linenum) 5498 return True 5499 5500 5501def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, 5502 io=codecs): 5503 """Reports for missing stl includes. 5504 5505 This function will output warnings to make sure you are including the headers 5506 necessary for the stl containers and functions that you use. We only give one 5507 reason to include a header. For example, if you use both equal_to<> and 5508 less<> in a .h file, only one (the latter in the file) of these will be 5509 reported as a reason to include the <functional>. 5510 5511 Args: 5512 filename: The name of the current file. 5513 clean_lines: A CleansedLines instance containing the file. 5514 include_state: An _IncludeState instance. 5515 error: The function to call with any errors found. 5516 io: The IO factory to use to read the header file. Provided for unittest 5517 injection. 5518 """ 5519 required = {} # A map of header name to linenumber and the template entity. 5520 # Example of required: { '<functional>': (1219, 'less<>') } 5521 5522 for linenum in xrange(clean_lines.NumLines()): 5523 line = clean_lines.elided[linenum] 5524 if not line or line[0] == '#': 5525 continue 5526 5527 # String is special -- it is a non-templatized type in STL. 5528 matched = _RE_PATTERN_STRING.search(line) 5529 if matched: 5530 # Don't warn about strings in non-STL namespaces: 5531 # (We check only the first match per line; good enough.) 5532 prefix = line[:matched.start()] 5533 if prefix.endswith('std::') or not prefix.endswith('::'): 5534 required['<string>'] = (linenum, 'string') 5535 5536 for pattern, template, header in _re_pattern_headers_maybe_templates: 5537 if pattern.search(line): 5538 required[header] = (linenum, template) 5539 5540 # The following function is just a speed up, no semantics are changed. 5541 if not '<' in line: # Reduces the cpu time usage by skipping lines. 5542 continue 5543 5544 for pattern, template, header in _re_pattern_templates: 5545 matched = pattern.search(line) 5546 if matched: 5547 # Don't warn about IWYU in non-STL namespaces: 5548 # (We check only the first match per line; good enough.) 5549 prefix = line[:matched.start()] 5550 if prefix.endswith('std::') or not prefix.endswith('::'): 5551 required[header] = (linenum, template) 5552 5553 # The policy is that if you #include something in foo.h you don't need to 5554 # include it again in foo.cc. Here, we will look at possible includes. 5555 # Let's flatten the include_state include_list and copy it into a dictionary. 5556 include_dict = dict([item for sublist in include_state.include_list 5557 for item in sublist]) 5558 5559 # Did we find the header for this file (if any) and successfully load it? 5560 header_found = False 5561 5562 # Use the absolute path so that matching works properly. 5563 abs_filename = FileInfo(filename).FullName() 5564 5565 # For Emacs's flymake. 5566 # If cpplint is invoked from Emacs's flymake, a temporary file is generated 5567 # by flymake and that file name might end with '_flymake.cc'. In that case, 5568 # restore original file name here so that the corresponding header file can be 5569 # found. 5570 # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h' 5571 # instead of 'foo_flymake.h' 5572 abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename) 5573 5574 # include_dict is modified during iteration, so we iterate over a copy of 5575 # the keys. 5576 header_keys = include_dict.keys() 5577 for header in header_keys: 5578 (same_module, common_path) = FilesBelongToSameModule(abs_filename, header) 5579 fullpath = common_path + header 5580 if same_module and UpdateIncludeState(fullpath, include_dict, io): 5581 header_found = True 5582 5583 # If we can't find the header file for a .cc, assume it's because we don't 5584 # know where to look. In that case we'll give up as we're not sure they 5585 # didn't include it in the .h file. 5586 # TODO(unknown): Do a better job of finding .h files so we are confident that 5587 # not having the .h file means there isn't one. 5588 if filename.endswith('.cc') and not header_found: 5589 return 5590 5591 # All the lines have been processed, report the errors found. 5592 for required_header_unstripped in required: 5593 template = required[required_header_unstripped][1] 5594 if required_header_unstripped.strip('<>"') not in include_dict: 5595 error(filename, required[required_header_unstripped][0], 5596 'build/include_what_you_use', 4, 5597 'Add #include ' + required_header_unstripped + ' for ' + template) 5598 5599 5600_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<') 5601 5602 5603def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error): 5604 """Check that make_pair's template arguments are deduced. 5605 5606 G++ 4.6 in C++11 mode fails badly if make_pair's template arguments are 5607 specified explicitly, and such use isn't intended in any case. 5608 5609 Args: 5610 filename: The name of the current file. 5611 clean_lines: A CleansedLines instance containing the file. 5612 linenum: The number of the line to check. 5613 error: The function to call with any errors found. 5614 """ 5615 line = clean_lines.elided[linenum] 5616 match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line) 5617 if match: 5618 error(filename, linenum, 'build/explicit_make_pair', 5619 4, # 4 = high confidence 5620 'For C++11-compatibility, omit template arguments from make_pair' 5621 ' OR use pair directly OR if appropriate, construct a pair directly') 5622 5623 5624def CheckRedundantVirtual(filename, clean_lines, linenum, error): 5625 """Check if line contains a redundant "virtual" function-specifier. 5626 5627 Args: 5628 filename: The name of the current file. 5629 clean_lines: A CleansedLines instance containing the file. 5630 linenum: The number of the line to check. 5631 error: The function to call with any errors found. 5632 """ 5633 # Look for "virtual" on current line. 5634 line = clean_lines.elided[linenum] 5635 virtual = Match(r'^(.*)(\bvirtual\b)(.*)$', line) 5636 if not virtual: return 5637 5638 # Ignore "virtual" keywords that are near access-specifiers. These 5639 # are only used in class base-specifier and do not apply to member 5640 # functions. 5641 if (Search(r'\b(public|protected|private)\s+$', virtual.group(1)) or 5642 Match(r'^\s+(public|protected|private)\b', virtual.group(3))): 5643 return 5644 5645 # Ignore the "virtual" keyword from virtual base classes. Usually 5646 # there is a column on the same line in these cases (virtual base 5647 # classes are rare in google3 because multiple inheritance is rare). 5648 if Match(r'^.*[^:]:[^:].*$', line): return 5649 5650 # Look for the next opening parenthesis. This is the start of the 5651 # parameter list (possibly on the next line shortly after virtual). 5652 # TODO(unknown): doesn't work if there are virtual functions with 5653 # decltype() or other things that use parentheses, but csearch suggests 5654 # that this is rare. 5655 end_col = -1 5656 end_line = -1 5657 start_col = len(virtual.group(2)) 5658 for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): 5659 line = clean_lines.elided[start_line][start_col:] 5660 parameter_list = Match(r'^([^(]*)\(', line) 5661 if parameter_list: 5662 # Match parentheses to find the end of the parameter list 5663 (_, end_line, end_col) = CloseExpression( 5664 clean_lines, start_line, start_col + len(parameter_list.group(1))) 5665 break 5666 start_col = 0 5667 5668 if end_col < 0: 5669 return # Couldn't find end of parameter list, give up 5670 5671 # Look for "override" or "final" after the parameter list 5672 # (possibly on the next few lines). 5673 for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): 5674 line = clean_lines.elided[i][end_col:] 5675 match = Search(r'\b(override|final)\b', line) 5676 if match: 5677 error(filename, linenum, 'readability/inheritance', 4, 5678 ('"virtual" is redundant since function is ' 5679 'already declared as "%s"' % match.group(1))) 5680 5681 # Set end_col to check whole lines after we are done with the 5682 # first line. 5683 end_col = 0 5684 if Search(r'[^\w]\s*$', line): 5685 break 5686 5687 5688def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error): 5689 """Check if line contains a redundant "override" or "final" virt-specifier. 5690 5691 Args: 5692 filename: The name of the current file. 5693 clean_lines: A CleansedLines instance containing the file. 5694 linenum: The number of the line to check. 5695 error: The function to call with any errors found. 5696 """ 5697 # Look for closing parenthesis nearby. We need one to confirm where 5698 # the declarator ends and where the virt-specifier starts to avoid 5699 # false positives. 5700 line = clean_lines.elided[linenum] 5701 declarator_end = line.rfind(')') 5702 if declarator_end >= 0: 5703 fragment = line[declarator_end:] 5704 else: 5705 if linenum > 1 and clean_lines.elided[linenum - 1].rfind(')') >= 0: 5706 fragment = line 5707 else: 5708 return 5709 5710 # Check that at most one of "override" or "final" is present, not both 5711 if Search(r'\boverride\b', fragment) and Search(r'\bfinal\b', fragment): 5712 error(filename, linenum, 'readability/inheritance', 4, 5713 ('"override" is redundant since function is ' 5714 'already declared as "final"')) 5715 5716 5717 5718 5719# Returns true if we are at a new block, and it is directly 5720# inside of a namespace. 5721def IsBlockInNameSpace(nesting_state, is_forward_declaration): 5722 """Checks that the new block is directly in a namespace. 5723 5724 Args: 5725 nesting_state: The _NestingState object that contains info about our state. 5726 is_forward_declaration: If the class is a forward declared class. 5727 Returns: 5728 Whether or not the new block is directly in a namespace. 5729 """ 5730 if is_forward_declaration: 5731 if len(nesting_state.stack) >= 1 and ( 5732 isinstance(nesting_state.stack[-1], _NamespaceInfo)): 5733 return True 5734 else: 5735 return False 5736 5737 return (len(nesting_state.stack) > 1 and 5738 nesting_state.stack[-1].check_namespace_indentation and 5739 isinstance(nesting_state.stack[-2], _NamespaceInfo)) 5740 5741 5742def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item, 5743 raw_lines_no_comments, linenum): 5744 """This method determines if we should apply our namespace indentation check. 5745 5746 Args: 5747 nesting_state: The current nesting state. 5748 is_namespace_indent_item: If we just put a new class on the stack, True. 5749 If the top of the stack is not a class, or we did not recently 5750 add the class, False. 5751 raw_lines_no_comments: The lines without the comments. 5752 linenum: The current line number we are processing. 5753 5754 Returns: 5755 True if we should apply our namespace indentation check. Currently, it 5756 only works for classes and namespaces inside of a namespace. 5757 """ 5758 5759 is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments, 5760 linenum) 5761 5762 if not (is_namespace_indent_item or is_forward_declaration): 5763 return False 5764 5765 # If we are in a macro, we do not want to check the namespace indentation. 5766 if IsMacroDefinition(raw_lines_no_comments, linenum): 5767 return False 5768 5769 return IsBlockInNameSpace(nesting_state, is_forward_declaration) 5770 5771 5772# Call this method if the line is directly inside of a namespace. 5773# If the line above is blank (excluding comments) or the start of 5774# an inner namespace, it cannot be indented. 5775def CheckItemIndentationInNamespace(filename, raw_lines_no_comments, linenum, 5776 error): 5777 line = raw_lines_no_comments[linenum] 5778 if Match(r'^\s+', line): 5779 error(filename, linenum, 'runtime/indentation_namespace', 4, 5780 'Do not indent within a namespace') 5781 5782 5783def ProcessLine(filename, file_extension, clean_lines, line, 5784 include_state, function_state, nesting_state, error, 5785 extra_check_functions=[]): 5786 """Processes a single line in the file. 5787 5788 Args: 5789 filename: Filename of the file that is being processed. 5790 file_extension: The extension (dot not included) of the file. 5791 clean_lines: An array of strings, each representing a line of the file, 5792 with comments stripped. 5793 line: Number of line being processed. 5794 include_state: An _IncludeState instance in which the headers are inserted. 5795 function_state: A _FunctionState instance which counts function lines, etc. 5796 nesting_state: A NestingState instance which maintains information about 5797 the current stack of nested blocks being parsed. 5798 error: A callable to which errors are reported, which takes 4 arguments: 5799 filename, line number, error level, and message 5800 extra_check_functions: An array of additional check functions that will be 5801 run on each source line. Each function takes 4 5802 arguments: filename, clean_lines, line, error 5803 """ 5804 raw_lines = clean_lines.raw_lines 5805 ParseNolintSuppressions(filename, raw_lines[line], line, error) 5806 nesting_state.Update(filename, clean_lines, line, error) 5807 CheckForNamespaceIndentation(filename, nesting_state, clean_lines, line, 5808 error) 5809 if nesting_state.InAsmBlock(): return 5810 CheckForFunctionLengths(filename, clean_lines, line, function_state, error) 5811 CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error) 5812 CheckStyle(filename, clean_lines, line, file_extension, nesting_state, error) 5813 CheckLanguage(filename, clean_lines, line, file_extension, include_state, 5814 nesting_state, error) 5815 CheckForNonConstReference(filename, clean_lines, line, nesting_state, error) 5816 CheckForNonStandardConstructs(filename, clean_lines, line, 5817 nesting_state, error) 5818 CheckVlogArguments(filename, clean_lines, line, error) 5819 CheckPosixThreading(filename, clean_lines, line, error) 5820 CheckInvalidIncrement(filename, clean_lines, line, error) 5821 CheckMakePairUsesDeduction(filename, clean_lines, line, error) 5822 CheckRedundantVirtual(filename, clean_lines, line, error) 5823 CheckRedundantOverrideOrFinal(filename, clean_lines, line, error) 5824 for check_fn in extra_check_functions: 5825 check_fn(filename, clean_lines, line, error) 5826 5827def FlagCxx11Features(filename, clean_lines, linenum, error): 5828 """Flag those c++11 features that we only allow in certain places. 5829 5830 Args: 5831 filename: The name of the current file. 5832 clean_lines: A CleansedLines instance containing the file. 5833 linenum: The number of the line to check. 5834 error: The function to call with any errors found. 5835 """ 5836 line = clean_lines.elided[linenum] 5837 5838 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line) 5839 5840 # Flag unapproved C++ TR1 headers. 5841 if include and include.group(1).startswith('tr1/'): 5842 error(filename, linenum, 'build/c++tr1', 5, 5843 ('C++ TR1 headers such as <%s> are unapproved.') % include.group(1)) 5844 5845 # Flag unapproved C++11 headers. 5846 if include and include.group(1) in ('cfenv', 5847 'condition_variable', 5848 'fenv.h', 5849 'future', 5850 'mutex', 5851 'thread', 5852 'chrono', 5853 'ratio', 5854 'regex', 5855 'system_error', 5856 ): 5857 error(filename, linenum, 'build/c++11', 5, 5858 ('<%s> is an unapproved C++11 header.') % include.group(1)) 5859 5860 # The only place where we need to worry about C++11 keywords and library 5861 # features in preprocessor directives is in macro definitions. 5862 if Match(r'\s*#', line) and not Match(r'\s*#\s*define\b', line): return 5863 5864 # These are classes and free functions. The classes are always 5865 # mentioned as std::*, but we only catch the free functions if 5866 # they're not found by ADL. They're alphabetical by header. 5867 for top_name in ( 5868 # type_traits 5869 'alignment_of', 5870 'aligned_union', 5871 ): 5872 if Search(r'\bstd::%s\b' % top_name, line): 5873 error(filename, linenum, 'build/c++11', 5, 5874 ('std::%s is an unapproved C++11 class or function. Send c-style ' 5875 'an example of where it would make your code more readable, and ' 5876 'they may let you use it.') % top_name) 5877 5878 5879def FlagCxx14Features(filename, clean_lines, linenum, error): 5880 """Flag those C++14 features that we restrict. 5881 5882 Args: 5883 filename: The name of the current file. 5884 clean_lines: A CleansedLines instance containing the file. 5885 linenum: The number of the line to check. 5886 error: The function to call with any errors found. 5887 """ 5888 line = clean_lines.elided[linenum] 5889 5890 include = Match(r'\s*#\s*include\s+[<"]([^<"]+)[">]', line) 5891 5892 # Flag unapproved C++14 headers. 5893 if include and include.group(1) in ('scoped_allocator', 'shared_mutex'): 5894 error(filename, linenum, 'build/c++14', 5, 5895 ('<%s> is an unapproved C++14 header.') % include.group(1)) 5896 5897 5898def ProcessFileData(filename, file_extension, lines, error, 5899 extra_check_functions=[]): 5900 """Performs lint checks and reports any errors to the given error function. 5901 5902 Args: 5903 filename: Filename of the file that is being processed. 5904 file_extension: The extension (dot not included) of the file. 5905 lines: An array of strings, each representing a line of the file, with the 5906 last element being empty if the file is terminated with a newline. 5907 error: A callable to which errors are reported, which takes 4 arguments: 5908 filename, line number, error level, and message 5909 extra_check_functions: An array of additional check functions that will be 5910 run on each source line. Each function takes 4 5911 arguments: filename, clean_lines, line, error 5912 """ 5913 lines = (['// marker so line numbers and indices both start at 1'] + lines + 5914 ['// marker so line numbers end in a known way']) 5915 5916 include_state = _IncludeState() 5917 function_state = _FunctionState() 5918 nesting_state = NestingState() 5919 5920 ResetNolintSuppressions() 5921 5922 CheckForCopyright(filename, lines, error) 5923 ProcessGlobalSuppresions(lines) 5924 RemoveMultiLineComments(filename, lines, error) 5925 clean_lines = CleansedLines(lines) 5926 5927 if IsHeaderExtension(file_extension): 5928 CheckForHeaderGuard(filename, clean_lines, error) 5929 5930 for line in xrange(clean_lines.NumLines()): 5931 ProcessLine(filename, file_extension, clean_lines, line, 5932 include_state, function_state, nesting_state, error, 5933 extra_check_functions) 5934 FlagCxx11Features(filename, clean_lines, line, error) 5935 nesting_state.CheckCompletedBlocks(filename, error) 5936 5937 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) 5938 5939 # Check that the .cc file has included its header if it exists. 5940 if _IsSourceExtension(file_extension): 5941 CheckHeaderFileIncluded(filename, include_state, error) 5942 5943 # We check here rather than inside ProcessLine so that we see raw 5944 # lines rather than "cleaned" lines. 5945 CheckForBadCharacters(filename, lines, error) 5946 5947 CheckForNewlineAtEOF(filename, lines, error) 5948 5949def ProcessConfigOverrides(filename): 5950 """ Loads the configuration files and processes the config overrides. 5951 5952 Args: 5953 filename: The name of the file being processed by the linter. 5954 5955 Returns: 5956 False if the current |filename| should not be processed further. 5957 """ 5958 5959 abs_filename = os.path.abspath(filename) 5960 cfg_filters = [] 5961 keep_looking = True 5962 while keep_looking: 5963 abs_path, base_name = os.path.split(abs_filename) 5964 if not base_name: 5965 break # Reached the root directory. 5966 5967 cfg_file = os.path.join(abs_path, "CPPLINT.cfg") 5968 abs_filename = abs_path 5969 if not os.path.isfile(cfg_file): 5970 continue 5971 5972 try: 5973 with open(cfg_file) as file_handle: 5974 for line in file_handle: 5975 line, _, _ = line.partition('#') # Remove comments. 5976 if not line.strip(): 5977 continue 5978 5979 name, _, val = line.partition('=') 5980 name = name.strip() 5981 val = val.strip() 5982 if name == 'set noparent': 5983 keep_looking = False 5984 elif name == 'filter': 5985 cfg_filters.append(val) 5986 elif name == 'exclude_files': 5987 # When matching exclude_files pattern, use the base_name of 5988 # the current file name or the directory name we are processing. 5989 # For example, if we are checking for lint errors in /foo/bar/baz.cc 5990 # and we found the .cfg file at /foo/CPPLINT.cfg, then the config 5991 # file's "exclude_files" filter is meant to be checked against "bar" 5992 # and not "baz" nor "bar/baz.cc". 5993 if base_name: 5994 pattern = re.compile(val) 5995 if pattern.match(base_name): 5996 if _cpplint_state.quiet: 5997 # Suppress "Ignoring file" warning when using --quiet. 5998 return False 5999 sys.stderr.write('Ignoring "%s": file excluded by "%s". ' 6000 'File path component "%s" matches ' 6001 'pattern "%s"\n' % 6002 (filename, cfg_file, base_name, val)) 6003 return False 6004 elif name == 'linelength': 6005 global _line_length 6006 try: 6007 _line_length = int(val) 6008 except ValueError: 6009 sys.stderr.write('Line length must be numeric.') 6010 elif name == 'root': 6011 global _root 6012 # root directories are specified relative to CPPLINT.cfg dir. 6013 _root = os.path.join(os.path.dirname(cfg_file), val) 6014 elif name == 'headers': 6015 ProcessHppHeadersOption(val) 6016 else: 6017 sys.stderr.write( 6018 'Invalid configuration option (%s) in file %s\n' % 6019 (name, cfg_file)) 6020 6021 except IOError: 6022 sys.stderr.write( 6023 "Skipping config file '%s': Can't open for reading\n" % cfg_file) 6024 keep_looking = False 6025 6026 # Apply all the accumulated filters in reverse order (top-level directory 6027 # config options having the least priority). 6028 for filter in reversed(cfg_filters): 6029 _AddFilters(filter) 6030 6031 return True 6032 6033 6034def ProcessFile(filename, vlevel, extra_check_functions=[]): 6035 """Does google-lint on a single file. 6036 6037 Args: 6038 filename: The name of the file to parse. 6039 6040 vlevel: The level of errors to report. Every error of confidence 6041 >= verbose_level will be reported. 0 is a good default. 6042 6043 extra_check_functions: An array of additional check functions that will be 6044 run on each source line. Each function takes 4 6045 arguments: filename, clean_lines, line, error 6046 """ 6047 6048 _SetVerboseLevel(vlevel) 6049 _BackupFilters() 6050 old_errors = _cpplint_state.error_count 6051 6052 if not ProcessConfigOverrides(filename): 6053 _RestoreFilters() 6054 return 6055 6056 lf_lines = [] 6057 crlf_lines = [] 6058 try: 6059 # Support the UNIX convention of using "-" for stdin. Note that 6060 # we are not opening the file with universal newline support 6061 # (which codecs doesn't support anyway), so the resulting lines do 6062 # contain trailing '\r' characters if we are reading a file that 6063 # has CRLF endings. 6064 # If after the split a trailing '\r' is present, it is removed 6065 # below. 6066 if filename == '-': 6067 lines = codecs.StreamReaderWriter(sys.stdin, 6068 codecs.getreader('utf8'), 6069 codecs.getwriter('utf8'), 6070 'replace').read().split('\n') 6071 else: 6072 lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n') 6073 6074 # Remove trailing '\r'. 6075 # The -1 accounts for the extra trailing blank line we get from split() 6076 for linenum in range(len(lines) - 1): 6077 if lines[linenum].endswith('\r'): 6078 lines[linenum] = lines[linenum].rstrip('\r') 6079 crlf_lines.append(linenum + 1) 6080 else: 6081 lf_lines.append(linenum + 1) 6082 6083 except IOError: 6084 sys.stderr.write( 6085 "Skipping input '%s': Can't open for reading\n" % filename) 6086 _RestoreFilters() 6087 return 6088 6089 # Note, if no dot is found, this will give the entire filename as the ext. 6090 file_extension = filename[filename.rfind('.') + 1:] 6091 6092 # When reading from stdin, the extension is unknown, so no cpplint tests 6093 # should rely on the extension. 6094 if filename != '-' and file_extension not in _valid_extensions: 6095 sys.stderr.write('Ignoring %s; not a valid file name ' 6096 '(%s)\n' % (filename, ', '.join(_valid_extensions))) 6097 else: 6098 ProcessFileData(filename, file_extension, lines, Error, 6099 extra_check_functions) 6100 6101 # If end-of-line sequences are a mix of LF and CR-LF, issue 6102 # warnings on the lines with CR. 6103 # 6104 # Don't issue any warnings if all lines are uniformly LF or CR-LF, 6105 # since critique can handle these just fine, and the style guide 6106 # doesn't dictate a particular end of line sequence. 6107 # 6108 # We can't depend on os.linesep to determine what the desired 6109 # end-of-line sequence should be, since that will return the 6110 # server-side end-of-line sequence. 6111 if lf_lines and crlf_lines: 6112 # Warn on every line with CR. An alternative approach might be to 6113 # check whether the file is mostly CRLF or just LF, and warn on the 6114 # minority, we bias toward LF here since most tools prefer LF. 6115 for linenum in crlf_lines: 6116 Error(filename, linenum, 'whitespace/newline', 1, 6117 'Unexpected \\r (^M) found; better to use only \\n') 6118 6119 # Suppress printing anything if --quiet was passed unless the error 6120 # count has increased after processing this file. 6121 if not _cpplint_state.quiet or old_errors != _cpplint_state.error_count: 6122 sys.stdout.write('Done processing %s\n' % filename) 6123 _RestoreFilters() 6124 6125 6126def PrintUsage(message): 6127 """Prints a brief usage string and exits, optionally with an error message. 6128 6129 Args: 6130 message: The optional error message. 6131 """ 6132 sys.stderr.write(_USAGE) 6133 if message: 6134 sys.exit('\nFATAL ERROR: ' + message) 6135 else: 6136 sys.exit(1) 6137 6138 6139def PrintCategories(): 6140 """Prints a list of all the error-categories used by error messages. 6141 6142 These are the categories used to filter messages via --filter. 6143 """ 6144 sys.stderr.write(''.join(' %s\n' % cat for cat in _ERROR_CATEGORIES)) 6145 sys.exit(0) 6146 6147 6148def ParseArguments(args): 6149 """Parses the command line arguments. 6150 6151 This may set the output format and verbosity level as side-effects. 6152 6153 Args: 6154 args: The command line arguments: 6155 6156 Returns: 6157 The list of filenames to lint. 6158 """ 6159 try: 6160 (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=', 6161 'counting=', 6162 'filter=', 6163 'root=', 6164 'linelength=', 6165 'extensions=', 6166 'headers=', 6167 'quiet']) 6168 except getopt.GetoptError: 6169 PrintUsage('Invalid arguments.') 6170 6171 verbosity = _VerboseLevel() 6172 output_format = _OutputFormat() 6173 filters = '' 6174 quiet = _Quiet() 6175 counting_style = '' 6176 6177 for (opt, val) in opts: 6178 if opt == '--help': 6179 PrintUsage(None) 6180 elif opt == '--output': 6181 if val not in ('emacs', 'vs7', 'eclipse'): 6182 PrintUsage('The only allowed output formats are emacs, vs7 and eclipse.') 6183 output_format = val 6184 elif opt == '--quiet': 6185 quiet = True 6186 elif opt == '--verbose': 6187 verbosity = int(val) 6188 elif opt == '--filter': 6189 filters = val 6190 if not filters: 6191 PrintCategories() 6192 elif opt == '--counting': 6193 if val not in ('total', 'toplevel', 'detailed'): 6194 PrintUsage('Valid counting options are total, toplevel, and detailed') 6195 counting_style = val 6196 elif opt == '--root': 6197 global _root 6198 _root = val 6199 elif opt == '--linelength': 6200 global _line_length 6201 try: 6202 _line_length = int(val) 6203 except ValueError: 6204 PrintUsage('Line length must be digits.') 6205 elif opt == '--extensions': 6206 global _valid_extensions 6207 try: 6208 _valid_extensions = set(val.split(',')) 6209 except ValueError: 6210 PrintUsage('Extensions must be comma separated list.') 6211 elif opt == '--headers': 6212 ProcessHppHeadersOption(val) 6213 6214 if not filenames: 6215 PrintUsage('No files were specified.') 6216 6217 _SetOutputFormat(output_format) 6218 _SetQuiet(quiet) 6219 _SetVerboseLevel(verbosity) 6220 _SetFilters(filters) 6221 _SetCountingStyle(counting_style) 6222 6223 return filenames 6224 6225 6226def main(): 6227 filenames = ParseArguments(sys.argv[1:]) 6228 6229 # Change stderr to write with replacement characters so we don't die 6230 # if we try to print something containing non-ASCII characters. 6231 sys.stderr = codecs.StreamReaderWriter(sys.stderr, 6232 codecs.getreader('utf8'), 6233 codecs.getwriter('utf8'), 6234 'replace') 6235 6236 _cpplint_state.ResetErrorCounts() 6237 for filename in filenames: 6238 ProcessFile(filename, _cpplint_state.verbose_level) 6239 # If --quiet is passed, suppress printing error count unless there are errors. 6240 if not _cpplint_state.quiet or _cpplint_state.error_count > 0: 6241 _cpplint_state.PrintErrorCounts() 6242 6243 sys.exit(_cpplint_state.error_count > 0) 6244 6245 6246if __name__ == '__main__': 6247 main() 6248