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