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