~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to cmake/cpplint.py

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2013-12-20 11:30:16 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131220113016-wre5g9bteeheq6he
Tags: 1.11.1-3
* remove version number from libbost development package names;
* ensure that AUTHORS is correctly shipped in all packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python2.4
 
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
# Here are some issues that I've had people identify in my code during reviews,
 
32
# that I think are possible to flag automatically in a lint tool.  If these were
 
33
# caught by lint, it would save time both for myself and that of my reviewers.
 
34
# Most likely, some of these are beyond the scope of the current lint framework,
 
35
# but I think it is valuable to retain these wish-list items even if they cannot
 
36
# be immediately implemented.
 
37
#
 
38
#  Suggestions
 
39
#  -----------
 
40
#  - Check for no 'explicit' for multi-arg ctor
 
41
#  - Check for boolean assign RHS in parens
 
42
#  - Check for ctor initializer-list colon position and spacing
 
43
#  - Check that if there's a ctor, there should be a dtor
 
44
#  - Check accessors that return non-pointer member variables are
 
45
#    declared const
 
46
#  - Check accessors that return non-const pointer member vars are
 
47
#    *not* declared const
 
48
#  - Check for using public includes for testing
 
49
#  - Check for spaces between brackets in one-line inline method
 
50
#  - Check for no assert()
 
51
#  - Check for spaces surrounding operators
 
52
#  - Check for 0 in pointer context (should be NULL)
 
53
#  - Check for 0 in char context (should be '\0')
 
54
#  - Check for camel-case method name conventions for methods
 
55
#    that are not simple inline getters and setters
 
56
#  - Check that base classes have virtual destructors
 
57
#    put "  // namespace" after } that closes a namespace, with
 
58
#    namespace's name after 'namespace' if it is named.
 
59
#  - Do not indent namespace contents
 
60
#  - Avoid inlining non-trivial constructors in header files
 
61
#    include base/basictypes.h if DISALLOW_EVIL_CONSTRUCTORS is used
 
62
#  - Check for old-school (void) cast for call-sites of functions
 
63
#    ignored return value
 
64
#  - Check gUnit usage of anonymous namespace
 
65
#  - Check for class declaration order (typedefs, consts, enums,
 
66
#    ctor(s?), dtor, friend declarations, methods, member vars)
 
67
#
 
68
 
 
69
"""Does google-lint on c++ files.
 
70
 
 
71
The goal of this script is to identify places in the code that *may*
 
72
be in non-compliance with google style.  It does not attempt to fix
 
73
up these problems -- the point is to educate.  It does also not
 
74
attempt to find all problems, or to ensure that everything it does
 
75
find is legitimately a problem.
 
76
 
 
77
In particular, we can get very confused by /* and // inside strings!
 
78
We do a small hack, which is to ignore //'s with "'s after them on the
 
79
same line, but it is far from perfect (in either direction).
 
80
"""
 
81
 
 
82
import codecs
 
83
import getopt
 
84
import math  # for log
 
85
import os
 
86
import re
 
87
import sre_compile
 
88
import string
 
89
import sys
 
90
import unicodedata
 
91
 
 
92
 
 
93
_USAGE = """
 
94
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
 
95
                   [--counting=total|toplevel|detailed]
 
96
        <file> [file] ...
 
97
 
 
98
  The style guidelines this tries to follow are those in
 
99
    http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
 
100
 
 
101
  Every problem is given a confidence score from 1-5, with 5 meaning we are
 
102
  certain of the problem, and 1 meaning it could be a legitimate construct.
 
103
  This will miss some errors, and is not a substitute for a code review.
 
104
 
 
105
  To suppress false-positive errors of a certain category, add a
 
106
  'NOLINT(category)' comment to the line.  NOLINT or NOLINT(*)
 
107
  suppresses errors of all categories on that line.
 
108
 
 
109
  The files passed in will be linted; at least one file must be provided.
 
110
  Linted extensions are .C, .cpp, and .h.  Other file types will be ignored.
 
111
 
 
112
  Flags:
 
113
 
 
114
    output=vs7
 
115
      By default, the output is formatted to ease emacs parsing.  Visual Studio
 
116
      compatible output (vs7) may also be used.  Other formats are unsupported.
 
117
 
 
118
    verbose=#
 
119
      Specify a number 0-5 to restrict errors to certain verbosity levels.
 
120
 
 
121
    filter=-x,+y,...
 
122
      Specify a comma-separated list of category-filters to apply: only
 
123
      error messages whose category names pass the filters will be printed.
 
124
      (Category names are printed with the message and look like
 
125
      "[whitespace/indent]".)  Filters are evaluated left to right.
 
126
      "-FOO" and "FOO" means "do not print categories that start with FOO".
 
127
      "+FOO" means "do print categories that start with FOO".
 
128
 
 
129
      Examples: --filter=-whitespace,+whitespace/braces
 
130
                --filter=whitespace,runtime/printf,+runtime/printf_format
 
131
                --filter=-,+build/include_what_you_use
 
132
 
 
133
      To see a list of all the categories used in cpplint, pass no arg:
 
134
         --filter=
 
135
 
 
136
    counting=total|toplevel|detailed
 
137
      The total number of errors found is always printed. If
 
138
      'toplevel' is provided, then the count of errors in each of
 
139
      the top-level categories like 'build' and 'whitespace' will
 
140
      also be printed. If 'detailed' is provided, then a count
 
141
      is provided for each category like 'build/class'.
 
142
"""
 
143
 
 
144
# We categorize each error message we print.  Here are the categories.
 
145
# We want an explicit list so we can list them all in cpplint --filter=.
 
146
# If you add a new error message with a new category, add it to the list
 
147
# here!  cpplint_unittest.py should tell you if you forget to do this.
 
148
# \ used for clearer layout -- pylint: disable-msg=C6013
 
149
_ERROR_CATEGORIES = [
 
150
  'build/class',
 
151
  'build/deprecated',
 
152
  'build/endif_comment',
 
153
  'build/explicit_make_pair',
 
154
  'build/forward_decl',
 
155
  'build/header_guard',
 
156
  'build/include',
 
157
  'build/include_alpha',
 
158
  'build/include_order',
 
159
  'build/include_what_you_use',
 
160
  'build/namespaces',
 
161
  'build/printf_format',
 
162
  'build/storage_class',
 
163
  'legal/copyright',
 
164
  'readability/braces',
 
165
  'readability/casting',
 
166
  'readability/check',
 
167
  'readability/constructors',
 
168
  'readability/fn_size',
 
169
  'readability/function',
 
170
  'readability/multiline_comment',
 
171
  'readability/multiline_string',
 
172
  'readability/nolint',
 
173
  'readability/streams',
 
174
  'readability/todo',
 
175
  'readability/utf8',
 
176
  'runtime/arrays',
 
177
  'runtime/casting',
 
178
  'runtime/explicit',
 
179
  'runtime/int',
 
180
  'runtime/init',
 
181
  'runtime/invalid_increment',
 
182
  'runtime/member_string_references',
 
183
  'runtime/memset',
 
184
  'runtime/operator',
 
185
  'runtime/printf',
 
186
  'runtime/printf_format',
 
187
  'runtime/references',
 
188
  'runtime/rtti',
 
189
  'runtime/sizeof',
 
190
  'runtime/string',
 
191
  'runtime/threadsafe_fn',
 
192
  'runtime/virtual',
 
193
  'whitespace/blank_line',
 
194
  'whitespace/braces',
 
195
  'whitespace/comma',
 
196
  'whitespace/comments',
 
197
  'whitespace/end_of_line',
 
198
  'whitespace/ending_newline',
 
199
  'whitespace/indent',
 
200
  'whitespace/labels',
 
201
  'whitespace/line_length',
 
202
  'whitespace/newline',
 
203
  'whitespace/operators',
 
204
  'whitespace/parens',
 
205
  'whitespace/semicolon',
 
206
  'whitespace/tab',
 
207
  'whitespace/todo'
 
208
  ]
 
209
 
 
210
# The default state of the category filter. This is overrided by the --filter=
 
211
# flag. By default all errors are on, so only add here categories that should be
 
212
# off by default (i.e., categories that must be enabled by the --filter= flags).
 
213
# All entries here should start with a '-' or '+', as in the --filter= flag.
 
214
_DEFAULT_FILTERS = ['-build/include_alpha', 
 
215
                    '-whitespace/braces',
 
216
                    '-whitespace/blank_line', 
 
217
                    '-readability/todo', 
 
218
                    '-readability/streams', 
 
219
                    '-build/header_guard',
 
220
                    '-legal/copyright', 
 
221
                    '-runtime/printf', 
 
222
                    '-readability/braces', 
 
223
                    '-readability/multiline_comment', 
 
224
                    '-runtime/rtti',
 
225
                    '-build/namespace']
 
226
 
 
227
# We used to check for high-bit characters, but after much discussion we
 
228
# decided those were OK, as long as they were in UTF-8 and didn't represent
 
229
# hard-coded international strings, which belong in a separate i18n file.
 
230
 
 
231
# Headers that we consider STL headers.
 
232
_STL_HEADERS = frozenset([
 
233
    'algobase.h', 'algorithm', 'alloc.h', 'bitset', 'deque', 'exception',
 
234
    'function.h', 'functional', 'hash_map', 'hash_map.h', 'hash_set',
 
235
    'hash_set.h', 'iterator', 'list', 'list.h', 'map', 'memory', 'new',
 
236
    'pair.h', 'pthread_alloc', 'queue', 'set', 'set.h', 'sstream', 'stack',
 
237
    'stl_alloc.h', 'stl_relops.h', 'type_traits.h',
 
238
    'utility', 'vector', 'vector.h',
 
239
    ])
 
240
 
 
241
 
 
242
# Non-STL C++ system headers.
 
243
_CPP_HEADERS = frozenset([
 
244
    'algo.h', 'builtinbuf.h', 'bvector.h', 'cassert', 'cctype',
 
245
    'cerrno', 'cfloat', 'ciso646', 'climits', 'clocale', 'cmath',
 
246
    'complex', 'complex.h', 'csetjmp', 'csignal', 'cstdarg', 'cstddef',
 
247
    'cstdio', 'cstdlib', 'cstring', 'ctime', 'cwchar', 'cwctype',
 
248
    'defalloc.h', 'deque.h', 'editbuf.h', 'exception', 'fstream',
 
249
    'fstream.h', 'hashtable.h', 'heap.h', 'indstream.h', 'iomanip',
 
250
    'iomanip.h', 'ios', 'iosfwd', 'iostream', 'iostream.h', 'istream',
 
251
    'istream.h', 'iterator.h', 'limits', 'map.h', 'multimap.h', 'multiset.h',
 
252
    'numeric', 'ostream', 'ostream.h', 'parsestream.h', 'pfstream.h',
 
253
    'PlotFile.h', 'procbuf.h', 'pthread_alloc.h', 'rope', 'rope.h',
 
254
    'ropeimpl.h', 'SFile.h', 'slist', 'slist.h', 'stack.h', 'stdexcept',
 
255
    'stdiostream.h', 'streambuf.h', 'stream.h', 'strfile.h', 'string',
 
256
    'strstream', 'strstream.h', 'tempbuf.h', 'tree.h', 'typeinfo', 'valarray',
 
257
    ])
 
258
 
 
259
 
 
260
# Assertion macros.  These are defined in base/logging.h and
 
261
# testing/base/gunit.h.  Note that the _M versions need to come first
 
262
# for substring matching to work.
 
263
_CHECK_MACROS = [
 
264
    'DCHECK', 'CHECK',
 
265
    'EXPECT_TRUE_M', 'EXPECT_TRUE',
 
266
    'ASSERT_TRUE_M', 'ASSERT_TRUE',
 
267
    'EXPECT_FALSE_M', 'EXPECT_FALSE',
 
268
    'ASSERT_FALSE_M', 'ASSERT_FALSE',
 
269
    ]
 
270
 
 
271
# Replacement macros for CHECK/DCHECK/EXPECT_TRUE/EXPECT_FALSE
 
272
_CHECK_REPLACEMENT = dict([(m, {}) for m in _CHECK_MACROS])
 
273
 
 
274
for op, replacement in [('==', 'EQ'), ('!=', 'NE'),
 
275
                        ('>=', 'GE'), ('>', 'GT'),
 
276
                        ('<=', 'LE'), ('<', 'LT')]:
 
277
  _CHECK_REPLACEMENT['DCHECK'][op] = 'DCHECK_%s' % replacement
 
278
  _CHECK_REPLACEMENT['CHECK'][op] = 'CHECK_%s' % replacement
 
279
  _CHECK_REPLACEMENT['EXPECT_TRUE'][op] = 'EXPECT_%s' % replacement
 
280
  _CHECK_REPLACEMENT['ASSERT_TRUE'][op] = 'ASSERT_%s' % replacement
 
281
  _CHECK_REPLACEMENT['EXPECT_TRUE_M'][op] = 'EXPECT_%s_M' % replacement
 
282
  _CHECK_REPLACEMENT['ASSERT_TRUE_M'][op] = 'ASSERT_%s_M' % replacement
 
283
 
 
284
for op, inv_replacement in [('==', 'NE'), ('!=', 'EQ'),
 
285
                            ('>=', 'LT'), ('>', 'LE'),
 
286
                            ('<=', 'GT'), ('<', 'GE')]:
 
287
  _CHECK_REPLACEMENT['EXPECT_FALSE'][op] = 'EXPECT_%s' % inv_replacement
 
288
  _CHECK_REPLACEMENT['ASSERT_FALSE'][op] = 'ASSERT_%s' % inv_replacement
 
289
  _CHECK_REPLACEMENT['EXPECT_FALSE_M'][op] = 'EXPECT_%s_M' % inv_replacement
 
290
  _CHECK_REPLACEMENT['ASSERT_FALSE_M'][op] = 'ASSERT_%s_M' % inv_replacement
 
291
 
 
292
 
 
293
# These constants define types of headers for use with
 
294
# _IncludeState.CheckNextIncludeOrder().
 
295
_C_SYS_HEADER = 1
 
296
_CPP_SYS_HEADER = 2
 
297
_LIKELY_MY_HEADER = 3
 
298
_POSSIBLE_MY_HEADER = 4
 
299
_OTHER_HEADER = 5
 
300
 
 
301
 
 
302
_regexp_compile_cache = {}
 
303
 
 
304
# Finds occurrences of NOLINT or NOLINT(...).
 
305
_RE_SUPPRESSION = re.compile(r'\bNOLINT\b(\([^)]*\))?')
 
306
 
 
307
# {str, set(int)}: a map from error categories to sets of linenumbers
 
308
# on which those errors are expected and should be suppressed.
 
309
_error_suppressions = {}
 
310
 
 
311
def ParseNolintSuppressions(filename, raw_line, linenum, error):
 
312
  """Updates the global list of error-suppressions.
 
313
 
 
314
  Parses any NOLINT comments on the current line, updating the global
 
315
  error_suppressions store.  Reports an error if the NOLINT comment
 
316
  was malformed.
 
317
 
 
318
  Args:
 
319
    filename: str, the name of the input file.
 
320
    raw_line: str, the line of input text, with comments.
 
321
    linenum: int, the number of the current line.
 
322
    error: function, an error handler.
 
323
  """
 
324
  # FIXME(adonovan): "NOLINT(" is misparsed as NOLINT(*).
 
325
  matched = _RE_SUPPRESSION.search(raw_line)
 
326
  if matched:
 
327
    category = matched.group(1)
 
328
    if category in (None, '(*)'):  # => "suppress all"
 
329
      _error_suppressions.setdefault(None, set()).add(linenum)
 
330
    else:
 
331
      if category.startswith('(') and category.endswith(')'):
 
332
        category = category[1:-1]
 
333
        if category in _ERROR_CATEGORIES:
 
334
          _error_suppressions.setdefault(category, set()).add(linenum)
 
335
        else:
 
336
          error(filename, linenum, 'readability/nolint', 5,
 
337
                'Unknown NOLINT error category: %s' % category)
 
338
 
 
339
 
 
340
def ResetNolintSuppressions():
 
341
  "Resets the set of NOLINT suppressions to empty."
 
342
  _error_suppressions.clear()
 
343
 
 
344
 
 
345
def IsErrorSuppressedByNolint(category, linenum):
 
346
  """Returns true if the specified error category is suppressed on this line.
 
347
 
 
348
  Consults the global error_suppressions map populated by
 
349
  ParseNolintSuppressions/ResetNolintSuppressions.
 
350
 
 
351
  Args:
 
352
    category: str, the category of the error.
 
353
    linenum: int, the current line number.
 
354
  Returns:
 
355
    bool, True iff the error should be suppressed due to a NOLINT comment.
 
356
  """
 
357
  return (linenum in _error_suppressions.get(category, set()) or
 
358
          linenum in _error_suppressions.get(None, set()))
 
359
 
 
360
def Match(pattern, s):
 
361
  """Matches the string with the pattern, caching the compiled regexp."""
 
362
  # The regexp compilation caching is inlined in both Match and Search for
 
363
  # performance reasons; factoring it out into a separate function turns out
 
364
  # to be noticeably expensive.
 
365
  if not pattern in _regexp_compile_cache:
 
366
    _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
 
367
  return _regexp_compile_cache[pattern].match(s)
 
368
 
 
369
 
 
370
def Search(pattern, s):
 
371
  """Searches the string for the pattern, caching the compiled regexp."""
 
372
  if not pattern in _regexp_compile_cache:
 
373
    _regexp_compile_cache[pattern] = sre_compile.compile(pattern)
 
374
  return _regexp_compile_cache[pattern].search(s)
 
375
 
 
376
 
 
377
class _IncludeState(dict):
 
378
  """Tracks line numbers for includes, and the order in which includes appear.
 
379
 
 
380
  As a dict, an _IncludeState object serves as a mapping between include
 
381
  filename and line number on which that file was included.
 
382
 
 
383
  Call CheckNextIncludeOrder() once for each header in the file, passing
 
384
  in the type constants defined above. Calls in an illegal order will
 
385
  raise an _IncludeError with an appropriate error message.
 
386
 
 
387
  """
 
388
  # self._section will move monotonically through this set. If it ever
 
389
  # needs to move backwards, CheckNextIncludeOrder will raise an error.
 
390
  _INITIAL_SECTION = 0
 
391
  _MY_H_SECTION = 1
 
392
  _C_SECTION = 2
 
393
  _CPP_SECTION = 3
 
394
  _OTHER_H_SECTION = 4
 
395
 
 
396
  _TYPE_NAMES = {
 
397
      _C_SYS_HEADER: 'C system header',
 
398
      _CPP_SYS_HEADER: 'C++ system header',
 
399
      _LIKELY_MY_HEADER: 'header this file implements',
 
400
      _POSSIBLE_MY_HEADER: 'header this file may implement',
 
401
      _OTHER_HEADER: 'other header',
 
402
      }
 
403
  _SECTION_NAMES = {
 
404
      _INITIAL_SECTION: "... nothing. (This can't be an error.)",
 
405
      _MY_H_SECTION: 'a header this file implements',
 
406
      _C_SECTION: 'C system header',
 
407
      _CPP_SECTION: 'C++ system header',
 
408
      _OTHER_H_SECTION: 'other header',
 
409
      }
 
410
 
 
411
  def __init__(self):
 
412
    dict.__init__(self)
 
413
    # The name of the current section.
 
414
    self._section = self._INITIAL_SECTION
 
415
    # The path of last found header.
 
416
    self._last_header = ''
 
417
 
 
418
  def CanonicalizeAlphabeticalOrder(self, header_path):
 
419
    """Returns a path canonicalized for alphabetical comparison.
 
420
 
 
421
    - replaces "-" with "_" so they both cmp the same.
 
422
    - removes '-inl' since we don't require them to be after the main header.
 
423
    - lowercase everything, just in case.
 
424
 
 
425
    Args:
 
426
      header_path: Path to be canonicalized.
 
427
 
 
428
    Returns:
 
429
      Canonicalized path.
 
430
    """
 
431
    return header_path.replace('-inl.h', '.h').replace('-', '_').lower()
 
432
 
 
433
  def IsInAlphabeticalOrder(self, header_path):
 
434
    """Check if a header is in alphabetical order with the previous header.
 
435
 
 
436
    Args:
 
437
      header_path: Header to be checked.
 
438
 
 
439
    Returns:
 
440
      Returns true if the header is in alphabetical order.
 
441
    """
 
442
    canonical_header = self.CanonicalizeAlphabeticalOrder(header_path)
 
443
    if self._last_header > canonical_header:
 
444
      return False
 
445
    self._last_header = canonical_header
 
446
    return True
 
447
 
 
448
  def CheckNextIncludeOrder(self, header_type):
 
449
    """Returns a non-empty error message if the next header is out of order.
 
450
 
 
451
    This function also updates the internal state to be ready to check
 
452
    the next include.
 
453
 
 
454
    Args:
 
455
      header_type: One of the _XXX_HEADER constants defined above.
 
456
 
 
457
    Returns:
 
458
      The empty string if the header is in the right order, or an
 
459
      error message describing what's wrong.
 
460
 
 
461
    """
 
462
    error_message = ('Found %s after %s' %
 
463
                     (self._TYPE_NAMES[header_type],
 
464
                      self._SECTION_NAMES[self._section]))
 
465
 
 
466
    last_section = self._section
 
467
 
 
468
    if header_type == _C_SYS_HEADER:
 
469
      if self._section <= self._C_SECTION:
 
470
        self._section = self._C_SECTION
 
471
      else:
 
472
        self._last_header = ''
 
473
        return error_message
 
474
    elif header_type == _CPP_SYS_HEADER:
 
475
      if self._section <= self._CPP_SECTION:
 
476
        self._section = self._CPP_SECTION
 
477
      else:
 
478
        self._last_header = ''
 
479
        return error_message
 
480
    elif header_type == _LIKELY_MY_HEADER:
 
481
      if self._section <= self._MY_H_SECTION:
 
482
        self._section = self._MY_H_SECTION
 
483
      else:
 
484
        self._section = self._OTHER_H_SECTION
 
485
    elif header_type == _POSSIBLE_MY_HEADER:
 
486
      if self._section <= self._MY_H_SECTION:
 
487
        self._section = self._MY_H_SECTION
 
488
      else:
 
489
        # This will always be the fallback because we're not sure
 
490
        # enough that the header is associated with this file.
 
491
        self._section = self._OTHER_H_SECTION
 
492
    else:
 
493
      assert header_type == _OTHER_HEADER
 
494
      self._section = self._OTHER_H_SECTION
 
495
 
 
496
    if last_section != self._section:
 
497
      self._last_header = ''
 
498
 
 
499
    return ''
 
500
 
 
501
 
 
502
class _CppLintState(object):
 
503
  """Maintains module-wide state.."""
 
504
 
 
505
  def __init__(self):
 
506
    self.verbose_level = 1  # global setting.
 
507
    self.error_count = 0    # global count of reported errors
 
508
    # filters to apply when emitting error messages
 
509
    self.filters = _DEFAULT_FILTERS[:]
 
510
    self.counting = 'total'  # In what way are we counting errors?
 
511
    self.errors_by_category = {}  # string to int dict storing error counts
 
512
 
 
513
    # output format:
 
514
    # "emacs" - format that emacs can parse (default)
 
515
    # "vs7" - format that Microsoft Visual Studio 7 can parse
 
516
    self.output_format = 'emacs'
 
517
 
 
518
  def SetOutputFormat(self, output_format):
 
519
    """Sets the output format for errors."""
 
520
    self.output_format = output_format
 
521
 
 
522
  def SetVerboseLevel(self, level):
 
523
    """Sets the module's verbosity, and returns the previous setting."""
 
524
    last_verbose_level = self.verbose_level
 
525
    self.verbose_level = level
 
526
    return last_verbose_level
 
527
 
 
528
  def SetCountingStyle(self, counting_style):
 
529
    """Sets the module's counting options."""
 
530
    self.counting = counting_style
 
531
 
 
532
  def SetFilters(self, filters):
 
533
    """Sets the error-message filters.
 
534
 
 
535
    These filters are applied when deciding whether to emit a given
 
536
    error message.
 
537
 
 
538
    Args:
 
539
      filters: A string of comma-separated filters (eg "+whitespace/indent").
 
540
               Each filter should start with + or -; else we die.
 
541
 
 
542
    Raises:
 
543
      ValueError: The comma-separated filters did not all start with '+' or '-'.
 
544
                  E.g. "-,+whitespace,-whitespace/indent,whitespace/badfilter"
 
545
    """
 
546
    # Default filters always have less priority than the flag ones.
 
547
    self.filters = _DEFAULT_FILTERS[:]
 
548
    for filt in filters.split(','):
 
549
      clean_filt = filt.strip()
 
550
      if clean_filt:
 
551
        self.filters.append(clean_filt)
 
552
    for filt in self.filters:
 
553
      if not (filt.startswith('+') or filt.startswith('-')):
 
554
        raise ValueError('Every filter in --filters must start with + or -'
 
555
                         ' (%s does not)' % filt)
 
556
 
 
557
  def ResetErrorCounts(self):
 
558
    """Sets the module's error statistic back to zero."""
 
559
    self.error_count = 0
 
560
    self.errors_by_category = {}
 
561
 
 
562
  def IncrementErrorCount(self, category):
 
563
    """Bumps the module's error statistic."""
 
564
    self.error_count += 1
 
565
    if self.counting in ('toplevel', 'detailed'):
 
566
      if self.counting != 'detailed':
 
567
        category = category.split('/')[0]
 
568
      if category not in self.errors_by_category:
 
569
        self.errors_by_category[category] = 0
 
570
      self.errors_by_category[category] += 1
 
571
 
 
572
  def PrintErrorCounts(self):
 
573
    """Print a summary of errors by category, and the total."""
 
574
    for category, count in self.errors_by_category.iteritems():
 
575
      sys.stderr.write('Category \'%s\' errors found: %d\n' %
 
576
                       (category, count))
 
577
    sys.stderr.write('Total errors found: %d\n' % self.error_count)
 
578
 
 
579
_cpplint_state = _CppLintState()
 
580
 
 
581
 
 
582
def _OutputFormat():
 
583
  """Gets the module's output format."""
 
584
  return _cpplint_state.output_format
 
585
 
 
586
 
 
587
def _SetOutputFormat(output_format):
 
588
  """Sets the module's output format."""
 
589
  _cpplint_state.SetOutputFormat(output_format)
 
590
 
 
591
 
 
592
def _VerboseLevel():
 
593
  """Returns the module's verbosity setting."""
 
594
  return _cpplint_state.verbose_level
 
595
 
 
596
 
 
597
def _SetVerboseLevel(level):
 
598
  """Sets the module's verbosity, and returns the previous setting."""
 
599
  return _cpplint_state.SetVerboseLevel(level)
 
600
 
 
601
 
 
602
def _SetCountingStyle(level):
 
603
  """Sets the module's counting options."""
 
604
  _cpplint_state.SetCountingStyle(level)
 
605
 
 
606
 
 
607
def _Filters():
 
608
  """Returns the module's list of output filters, as a list."""
 
609
  return _cpplint_state.filters
 
610
 
 
611
 
 
612
def _SetFilters(filters):
 
613
  """Sets the module's error-message filters.
 
614
 
 
615
  These filters are applied when deciding whether to emit a given
 
616
  error message.
 
617
 
 
618
  Args:
 
619
    filters: A string of comma-separated filters (eg "whitespace/indent").
 
620
             Each filter should start with + or -; else we die.
 
621
  """
 
622
  _cpplint_state.SetFilters(filters)
 
623
 
 
624
 
 
625
class _FunctionState(object):
 
626
  """Tracks current function name and the number of lines in its body."""
 
627
 
 
628
  _NORMAL_TRIGGER = 250  # for --v=0, 500 for --v=1, etc.
 
629
  _TEST_TRIGGER = 400    # about 50% more than _NORMAL_TRIGGER.
 
630
 
 
631
  def __init__(self):
 
632
    self.in_a_function = False
 
633
    self.lines_in_function = 0
 
634
    self.current_function = ''
 
635
 
 
636
  def Begin(self, function_name):
 
637
    """Start analyzing function body.
 
638
 
 
639
    Args:
 
640
      function_name: The name of the function being tracked.
 
641
    """
 
642
    self.in_a_function = True
 
643
    self.lines_in_function = 0
 
644
    self.current_function = function_name
 
645
 
 
646
  def Count(self):
 
647
    """Count line in current function body."""
 
648
    if self.in_a_function:
 
649
      self.lines_in_function += 1
 
650
 
 
651
  def Check(self, error, filename, linenum):
 
652
    """Report if too many lines in function body.
 
653
 
 
654
    Args:
 
655
      error: The function to call with any errors found.
 
656
      filename: The name of the current file.
 
657
      linenum: The number of the line to check.
 
658
    """
 
659
    if Match(r'T(EST|est)', self.current_function):
 
660
      base_trigger = self._TEST_TRIGGER
 
661
    else:
 
662
      base_trigger = self._NORMAL_TRIGGER
 
663
    trigger = base_trigger * 2**_VerboseLevel()
 
664
 
 
665
    if self.lines_in_function > trigger:
 
666
      error_level = int(math.log(self.lines_in_function / base_trigger, 2))
 
667
      # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ...
 
668
      if error_level > 5:
 
669
        error_level = 5
 
670
      error(filename, linenum, 'readability/fn_size', error_level,
 
671
            'Small and focused functions are preferred:'
 
672
            ' %s has %d non-comment lines'
 
673
            ' (error triggered by exceeding %d lines).'  % (
 
674
                self.current_function, self.lines_in_function, trigger))
 
675
 
 
676
  def End(self):
 
677
    """Stop analyzing function body."""
 
678
    self.in_a_function = False
 
679
 
 
680
 
 
681
class _IncludeError(Exception):
 
682
  """Indicates a problem with the include order in a file."""
 
683
  pass
 
684
 
 
685
 
 
686
class FileInfo:
 
687
  """Provides utility functions for filenames.
 
688
 
 
689
  FileInfo provides easy access to the components of a file's path
 
690
  relative to the project root.
 
691
  """
 
692
 
 
693
  def __init__(self, filename):
 
694
    self._filename = filename
 
695
 
 
696
  def FullName(self):
 
697
    """Make Windows paths like Unix."""
 
698
    return os.path.abspath(self._filename).replace('\\', '/')
 
699
 
 
700
  def RepositoryName(self):
 
701
    """FullName after removing the local path to the repository.
 
702
 
 
703
    If we have a real absolute path name here we can try to do something smart:
 
704
    detecting the root of the checkout and truncating /path/to/checkout from
 
705
    the name so that we get header guards that don't include things like
 
706
    "C:\Documents and Settings\..." or "/home/username/..." in them and thus
 
707
    people on different computers who have checked the source out to different
 
708
    locations won't see bogus errors.
 
709
    """
 
710
    fullname = self.FullName()
 
711
 
 
712
    if os.path.exists(fullname):
 
713
      project_dir = os.path.dirname(fullname)
 
714
 
 
715
      if os.path.exists(os.path.join(project_dir, ".svn")):
 
716
        # If there's a .svn file in the current directory, we recursively look
 
717
        # up the directory tree for the top of the SVN checkout
 
718
        root_dir = project_dir
 
719
        one_up_dir = os.path.dirname(root_dir)
 
720
        while os.path.exists(os.path.join(one_up_dir, ".svn")):
 
721
          root_dir = os.path.dirname(root_dir)
 
722
          one_up_dir = os.path.dirname(one_up_dir)
 
723
 
 
724
        prefix = os.path.commonprefix([root_dir, project_dir])
 
725
        return fullname[len(prefix) + 1:]
 
726
 
 
727
      # Not SVN <= 1.6? Try to find a git, hg, or svn top level directory by
 
728
      # searching up from the current path.
 
729
      root_dir = os.path.dirname(fullname)
 
730
      while (root_dir != os.path.dirname(root_dir) and
 
731
             not os.path.exists(os.path.join(root_dir, ".git")) and
 
732
             not os.path.exists(os.path.join(root_dir, ".hg")) and
 
733
             not os.path.exists(os.path.join(root_dir, ".svn"))):
 
734
        root_dir = os.path.dirname(root_dir)
 
735
 
 
736
      if (os.path.exists(os.path.join(root_dir, ".git")) or
 
737
          os.path.exists(os.path.join(root_dir, ".hg")) or
 
738
          os.path.exists(os.path.join(root_dir, ".svn"))):
 
739
        prefix = os.path.commonprefix([root_dir, project_dir])
 
740
        return fullname[len(prefix) + 1:]
 
741
 
 
742
    # Don't know what to do; header guard warnings may be wrong...
 
743
    return fullname
 
744
 
 
745
  def Split(self):
 
746
    """Splits the file into the directory, basename, and extension.
 
747
 
 
748
    For 'chrome/browser/browser.cc', Split() would
 
749
    return ('chrome/browser', 'browser', '.cc')
 
750
 
 
751
    Returns:
 
752
      A tuple of (directory, basename, extension).
 
753
    """
 
754
 
 
755
    googlename = self.RepositoryName()
 
756
    project, rest = os.path.split(googlename)
 
757
    return (project,) + os.path.splitext(rest)
 
758
 
 
759
  def BaseName(self):
 
760
    """File base name - text after the final slash, before the final period."""
 
761
    return self.Split()[1]
 
762
 
 
763
  def Extension(self):
 
764
    """File extension - text following the final period."""
 
765
    return self.Split()[2]
 
766
 
 
767
  def NoExtension(self):
 
768
    """File has no source file extension."""
 
769
    return '/'.join(self.Split()[0:2])
 
770
 
 
771
  def IsSource(self):
 
772
    """File has a source file extension."""
 
773
    return self.Extension()[1:] in ('c', 'cc', 'cpp', 'cxx', 'C')
 
774
 
 
775
 
 
776
def _ShouldPrintError(category, confidence, linenum):
 
777
  """If confidence >= verbose, category passes filter and is not suppressed."""
 
778
 
 
779
  # There are three ways we might decide not to print an error message:
 
780
  # a "NOLINT(category)" comment appears in the source,
 
781
  # the verbosity level isn't high enough, or the filters filter it out.
 
782
  if IsErrorSuppressedByNolint(category, linenum):
 
783
    return False
 
784
  if confidence < _cpplint_state.verbose_level:
 
785
    return False
 
786
 
 
787
  is_filtered = False
 
788
  for one_filter in _Filters():
 
789
    if one_filter.startswith('-'):
 
790
      if category.startswith(one_filter[1:]):
 
791
        is_filtered = True
 
792
    elif one_filter.startswith('+'):
 
793
      if category.startswith(one_filter[1:]):
 
794
        is_filtered = False
 
795
    else:
 
796
      assert False  # should have been checked for in SetFilter.
 
797
  if is_filtered:
 
798
    return False
 
799
 
 
800
  return True
 
801
 
 
802
 
 
803
def Error(filename, linenum, category, confidence, message):
 
804
  """Logs the fact we've found a lint error.
 
805
 
 
806
  We log where the error was found, and also our confidence in the error,
 
807
  that is, how certain we are this is a legitimate style regression, and
 
808
  not a misidentification or a use that's sometimes justified.
 
809
 
 
810
  False positives can be suppressed by the use of
 
811
  "cpplint(category)"  comments on the offending line.  These are
 
812
  parsed into _error_suppressions.
 
813
 
 
814
  Args:
 
815
    filename: The name of the file containing the error.
 
816
    linenum: The number of the line containing the error.
 
817
    category: A string used to describe the "category" this bug
 
818
      falls under: "whitespace", say, or "runtime".  Categories
 
819
      may have a hierarchy separated by slashes: "whitespace/indent".
 
820
    confidence: A number from 1-5 representing a confidence score for
 
821
      the error, with 5 meaning that we are certain of the problem,
 
822
      and 1 meaning that it could be a legitimate construct.
 
823
    message: The error message.
 
824
  """
 
825
  if _ShouldPrintError(category, confidence, linenum):
 
826
    _cpplint_state.IncrementErrorCount(category)
 
827
    if _cpplint_state.output_format == 'vs7':
 
828
      sys.stderr.write('%s(%s):  %s  [%s] [%d]\n' % (
 
829
          filename, linenum, message, category, confidence))
 
830
    else:
 
831
      sys.stderr.write('%s:%s:  %s  [%s] [%d]\n' % (
 
832
          filename, linenum, message, category, confidence))
 
833
 
 
834
 
 
835
# Matches standard C++ escape esequences per 2.13.2.3 of the C++ standard.
 
836
_RE_PATTERN_CLEANSE_LINE_ESCAPES = re.compile(
 
837
    r'\\([abfnrtv?"\\\']|\d+|x[0-9a-fA-F]+)')
 
838
# Matches strings.  Escape codes should already be removed by ESCAPES.
 
839
_RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES = re.compile(r'"[^"]*"')
 
840
# Matches characters.  Escape codes should already be removed by ESCAPES.
 
841
_RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES = re.compile(r"'.'")
 
842
# Matches multi-line C++ comments.
 
843
# This RE is a little bit more complicated than one might expect, because we
 
844
# have to take care of space removals tools so we can handle comments inside
 
845
# statements better.
 
846
# The current rule is: We only clear spaces from both sides when we're at the
 
847
# end of the line. Otherwise, we try to remove spaces from the right side,
 
848
# if this doesn't work we try on left side but only if there's a non-character
 
849
# on the right.
 
850
_RE_PATTERN_CLEANSE_LINE_C_COMMENTS = re.compile(
 
851
    r"""(\s*/\*.*\*/\s*$|
 
852
            /\*.*\*/\s+|
 
853
         \s+/\*.*\*/(?=\W)|
 
854
            /\*.*\*/)""", re.VERBOSE)
 
855
 
 
856
 
 
857
def IsCppString(line):
 
858
  """Does line terminate so, that the next symbol is in string constant.
 
859
 
 
860
  This function does not consider single-line nor multi-line comments.
 
861
 
 
862
  Args:
 
863
    line: is a partial line of code starting from the 0..n.
 
864
 
 
865
  Returns:
 
866
    True, if next character appended to 'line' is inside a
 
867
    string constant.
 
868
  """
 
869
 
 
870
  line = line.replace(r'\\', 'XX')  # after this, \\" does not match to \"
 
871
  return ((line.count('"') - line.count(r'\"') - line.count("'\"'")) & 1) == 1
 
872
 
 
873
 
 
874
def FindNextMultiLineCommentStart(lines, lineix):
 
875
  """Find the beginning marker for a multiline comment."""
 
876
  while lineix < len(lines):
 
877
    if lines[lineix].strip().startswith('/*'):
 
878
      # Only return this marker if the comment goes beyond this line
 
879
      if lines[lineix].strip().find('*/', 2) < 0:
 
880
        return lineix
 
881
    lineix += 1
 
882
  return len(lines)
 
883
 
 
884
 
 
885
def FindNextMultiLineCommentEnd(lines, lineix):
 
886
  """We are inside a comment, find the end marker."""
 
887
  while lineix < len(lines):
 
888
    if lines[lineix].strip().endswith('*/'):
 
889
      return lineix
 
890
    lineix += 1
 
891
  return len(lines)
 
892
 
 
893
 
 
894
def RemoveMultiLineCommentsFromRange(lines, begin, end):
 
895
  """Clears a range of lines for multi-line comments."""
 
896
  # Having // dummy comments makes the lines non-empty, so we will not get
 
897
  # unnecessary blank line warnings later in the code.
 
898
  for i in range(begin, end):
 
899
    lines[i] = '// dummy'
 
900
 
 
901
 
 
902
def RemoveMultiLineComments(filename, lines, error):
 
903
  """Removes multiline (c-style) comments from lines."""
 
904
  lineix = 0
 
905
  while lineix < len(lines):
 
906
    lineix_begin = FindNextMultiLineCommentStart(lines, lineix)
 
907
    if lineix_begin >= len(lines):
 
908
      return
 
909
    lineix_end = FindNextMultiLineCommentEnd(lines, lineix_begin)
 
910
    if lineix_end >= len(lines):
 
911
      error(filename, lineix_begin + 1, 'readability/multiline_comment', 5,
 
912
            'Could not find end of multi-line comment')
 
913
      return
 
914
    RemoveMultiLineCommentsFromRange(lines, lineix_begin, lineix_end + 1)
 
915
    lineix = lineix_end + 1
 
916
 
 
917
 
 
918
def CleanseComments(line):
 
919
  """Removes //-comments and single-line C-style /* */ comments.
 
920
 
 
921
  Args:
 
922
    line: A line of C++ source.
 
923
 
 
924
  Returns:
 
925
    The line with single-line comments removed.
 
926
  """
 
927
  commentpos = line.find('//')
 
928
  if commentpos != -1 and not IsCppString(line[:commentpos]):
 
929
    line = line[:commentpos].rstrip()
 
930
  # get rid of /* ... */
 
931
  return _RE_PATTERN_CLEANSE_LINE_C_COMMENTS.sub('', line)
 
932
 
 
933
 
 
934
class CleansedLines(object):
 
935
  """Holds 3 copies of all lines with different preprocessing applied to them.
 
936
 
 
937
  1) elided member contains lines without strings and comments,
 
938
  2) lines member contains lines without comments, and
 
939
  3) raw member contains all the lines without processing.
 
940
  All these three members are of <type 'list'>, and of the same length.
 
941
  """
 
942
 
 
943
  def __init__(self, lines):
 
944
    self.elided = []
 
945
    self.lines = []
 
946
    self.raw_lines = lines
 
947
    self.num_lines = len(lines)
 
948
    for linenum in range(len(lines)):
 
949
      self.lines.append(CleanseComments(lines[linenum]))
 
950
      elided = self._CollapseStrings(lines[linenum])
 
951
      self.elided.append(CleanseComments(elided))
 
952
 
 
953
  def NumLines(self):
 
954
    """Returns the number of lines represented."""
 
955
    return self.num_lines
 
956
 
 
957
  @staticmethod
 
958
  def _CollapseStrings(elided):
 
959
    """Collapses strings and chars on a line to simple "" or '' blocks.
 
960
 
 
961
    We nix strings first so we're not fooled by text like '"http://"'
 
962
 
 
963
    Args:
 
964
      elided: The line being processed.
 
965
 
 
966
    Returns:
 
967
      The line with collapsed strings.
 
968
    """
 
969
    if not _RE_PATTERN_INCLUDE.match(elided):
 
970
      # Remove escaped characters first to make quote/single quote collapsing
 
971
      # basic.  Things that look like escaped characters shouldn't occur
 
972
      # outside of strings and chars.
 
973
      elided = _RE_PATTERN_CLEANSE_LINE_ESCAPES.sub('', elided)
 
974
      elided = _RE_PATTERN_CLEANSE_LINE_SINGLE_QUOTES.sub("''", elided)
 
975
      elided = _RE_PATTERN_CLEANSE_LINE_DOUBLE_QUOTES.sub('""', elided)
 
976
    return elided
 
977
 
 
978
 
 
979
def CloseExpression(clean_lines, linenum, pos):
 
980
  """If input points to ( or { or [, finds the position that closes it.
 
981
 
 
982
  If lines[linenum][pos] points to a '(' or '{' or '[', finds the
 
983
  linenum/pos that correspond to the closing of the expression.
 
984
 
 
985
  Args:
 
986
    clean_lines: A CleansedLines instance containing the file.
 
987
    linenum: The number of the line to check.
 
988
    pos: A position on the line.
 
989
 
 
990
  Returns:
 
991
    A tuple (line, linenum, pos) pointer *past* the closing brace, or
 
992
    (line, len(lines), -1) if we never find a close.  Note we ignore
 
993
    strings and comments when matching; and the line we return is the
 
994
    'cleansed' line at linenum.
 
995
  """
 
996
 
 
997
  line = clean_lines.elided[linenum]
 
998
  startchar = line[pos]
 
999
  if startchar not in '({[':
 
1000
    return (line, clean_lines.NumLines(), -1)
 
1001
  if startchar == '(': endchar = ')'
 
1002
  if startchar == '[': endchar = ']'
 
1003
  if startchar == '{': endchar = '}'
 
1004
 
 
1005
  num_open = line.count(startchar) - line.count(endchar)
 
1006
  while linenum < clean_lines.NumLines() and num_open > 0:
 
1007
    linenum += 1
 
1008
    line = clean_lines.elided[linenum]
 
1009
    num_open += line.count(startchar) - line.count(endchar)
 
1010
  # OK, now find the endchar that actually got us back to even
 
1011
  endpos = len(line)
 
1012
  while num_open >= 0:
 
1013
    endpos = line.rfind(')', 0, endpos)
 
1014
    num_open -= 1                 # chopped off another )
 
1015
  return (line, linenum, endpos + 1)
 
1016
 
 
1017
 
 
1018
def CheckForCopyright(filename, lines, error):
 
1019
  """Logs an error if no Copyright message appears at the top of the file."""
 
1020
 
 
1021
  # We'll say it should occur by line 10. Don't forget there's a
 
1022
  # dummy line at the front.
 
1023
  for line in xrange(1, min(len(lines), 11)):
 
1024
    if re.search(r'Copyright', lines[line], re.I): break
 
1025
  else:                       # means no copyright line was found
 
1026
    error(filename, 0, 'legal/copyright', 5,
 
1027
          'No copyright message found.  '
 
1028
          'You should have a line: "Copyright [year] <Copyright Owner>"')
 
1029
 
 
1030
 
 
1031
def GetHeaderGuardCPPVariable(filename):
 
1032
  """Returns the CPP variable that should be used as a header guard.
 
1033
 
 
1034
  Args:
 
1035
    filename: The name of a C++ header file.
 
1036
 
 
1037
  Returns:
 
1038
    The CPP variable that should be used as a header guard in the
 
1039
    named file.
 
1040
 
 
1041
  """
 
1042
 
 
1043
  # Restores original filename in case that cpplint is invoked from Emacs's
 
1044
  # flymake.
 
1045
  filename = re.sub(r'_flymake\.h$', '.h', filename)
 
1046
 
 
1047
  fileinfo = FileInfo(filename)
 
1048
  return re.sub(r'[-./\s]', '_', fileinfo.RepositoryName()).upper() + '_'
 
1049
 
 
1050
 
 
1051
def CheckForHeaderGuard(filename, lines, error):
 
1052
  """Checks that the file contains a header guard.
 
1053
 
 
1054
  Logs an error if no #ifndef header guard is present.  For other
 
1055
  headers, checks that the full pathname is used.
 
1056
 
 
1057
  Args:
 
1058
    filename: The name of the C++ header file.
 
1059
    lines: An array of strings, each representing a line of the file.
 
1060
    error: The function to call with any errors found.
 
1061
  """
 
1062
 
 
1063
  cppvar = GetHeaderGuardCPPVariable(filename)
 
1064
 
 
1065
  ifndef = None
 
1066
  ifndef_linenum = 0
 
1067
  define = None
 
1068
  endif = None
 
1069
  endif_linenum = 0
 
1070
  for linenum, line in enumerate(lines):
 
1071
    linesplit = line.split()
 
1072
    if len(linesplit) >= 2:
 
1073
      # find the first occurrence of #ifndef and #define, save arg
 
1074
      if not ifndef and linesplit[0] == '#ifndef':
 
1075
        # set ifndef to the header guard presented on the #ifndef line.
 
1076
        ifndef = linesplit[1]
 
1077
        ifndef_linenum = linenum
 
1078
      if not define and linesplit[0] == '#define':
 
1079
        define = linesplit[1]
 
1080
    # find the last occurrence of #endif, save entire line
 
1081
    if line.startswith('#endif'):
 
1082
      endif = line
 
1083
      endif_linenum = linenum
 
1084
 
 
1085
  if not ifndef or not define or ifndef != define:
 
1086
    error(filename, 0, 'build/header_guard', 5,
 
1087
          'No #ifndef header guard found, suggested CPP variable is: %s' %
 
1088
          cppvar)
 
1089
    return
 
1090
 
 
1091
  # The guard should be PATH_FILE_H_, but we also allow PATH_FILE_H__
 
1092
  # for backward compatibility.
 
1093
  if ifndef != cppvar:
 
1094
    error_level = 0
 
1095
    if ifndef != cppvar + '_':
 
1096
      error_level = 5
 
1097
 
 
1098
    ParseNolintSuppressions(filename, lines[ifndef_linenum], ifndef_linenum,
 
1099
                            error)
 
1100
    error(filename, ifndef_linenum, 'build/header_guard', error_level,
 
1101
          '#ifndef header guard has wrong style, please use: %s' % cppvar)
 
1102
 
 
1103
  if endif != ('#endif  // %s' % cppvar):
 
1104
    error_level = 0
 
1105
    if endif != ('#endif  // %s' % (cppvar + '_')):
 
1106
      error_level = 5
 
1107
 
 
1108
    ParseNolintSuppressions(filename, lines[endif_linenum], endif_linenum,
 
1109
                            error)
 
1110
    error(filename, endif_linenum, 'build/header_guard', error_level,
 
1111
          '#endif line should be "#endif  // %s"' % cppvar)
 
1112
 
 
1113
 
 
1114
def CheckForUnicodeReplacementCharacters(filename, lines, error):
 
1115
  """Logs an error for each line containing Unicode replacement characters.
 
1116
 
 
1117
  These indicate that either the file contained invalid UTF-8 (likely)
 
1118
  or Unicode replacement characters (which it shouldn't).  Note that
 
1119
  it's possible for this to throw off line numbering if the invalid
 
1120
  UTF-8 occurred adjacent to a newline.
 
1121
 
 
1122
  Args:
 
1123
    filename: The name of the current file.
 
1124
    lines: An array of strings, each representing a line of the file.
 
1125
    error: The function to call with any errors found.
 
1126
  """
 
1127
  for linenum, line in enumerate(lines):
 
1128
    if u'\ufffd' in line:
 
1129
      error(filename, linenum, 'readability/utf8', 5,
 
1130
            'Line contains invalid UTF-8 (or Unicode replacement character).')
 
1131
 
 
1132
 
 
1133
def CheckForNewlineAtEOF(filename, lines, error):
 
1134
  """Logs an error if there is no newline char at the end of the file.
 
1135
 
 
1136
  Args:
 
1137
    filename: The name of the current file.
 
1138
    lines: An array of strings, each representing a line of the file.
 
1139
    error: The function to call with any errors found.
 
1140
  """
 
1141
 
 
1142
  # The array lines() was created by adding two newlines to the
 
1143
  # original file (go figure), then splitting on \n.
 
1144
  # To verify that the file ends in \n, we just have to make sure the
 
1145
  # last-but-two element of lines() exists and is empty.
 
1146
  if len(lines) < 3 or lines[-2]:
 
1147
    error(filename, len(lines) - 2, 'whitespace/ending_newline', 5,
 
1148
          'Could not find a newline character at the end of the file.')
 
1149
 
 
1150
 
 
1151
def CheckForMultilineCommentsAndStrings(filename, clean_lines, linenum, error):
 
1152
  """Logs an error if we see /* ... */ or "..." that extend past one line.
 
1153
 
 
1154
  /* ... */ comments are legit inside macros, for one line.
 
1155
  Otherwise, we prefer // comments, so it's ok to warn about the
 
1156
  other.  Likewise, it's ok for strings to extend across multiple
 
1157
  lines, as long as a line continuation character (backslash)
 
1158
  terminates each line. Although not currently prohibited by the C++
 
1159
  style guide, it's ugly and unnecessary. We don't do well with either
 
1160
  in this lint program, so we warn about both.
 
1161
 
 
1162
  Args:
 
1163
    filename: The name of the current file.
 
1164
    clean_lines: A CleansedLines instance containing the file.
 
1165
    linenum: The number of the line to check.
 
1166
    error: The function to call with any errors found.
 
1167
  """
 
1168
  line = clean_lines.elided[linenum]
 
1169
 
 
1170
  # Remove all \\ (escaped backslashes) from the line. They are OK, and the
 
1171
  # second (escaped) slash may trigger later \" detection erroneously.
 
1172
  line = line.replace('\\\\', '')
 
1173
 
 
1174
  if line.count('/*') > line.count('*/'):
 
1175
    error(filename, linenum, 'readability/multiline_comment', 5,
 
1176
          'Complex multi-line /*...*/-style comment found. '
 
1177
          'Lint may give bogus warnings.  '
 
1178
          'Consider replacing these with //-style comments, '
 
1179
          'with #if 0...#endif, '
 
1180
          'or with more clearly structured multi-line comments.')
 
1181
 
 
1182
  if (line.count('"') - line.count('\\"')) % 2:
 
1183
    error(filename, linenum, 'readability/multiline_string', 5,
 
1184
          'Multi-line string ("...") found.  This lint script doesn\'t '
 
1185
          'do well with such strings, and may give bogus warnings.  They\'re '
 
1186
          'ugly and unnecessary, and you should use concatenation instead".')
 
1187
 
 
1188
 
 
1189
threading_list = (
 
1190
    ('asctime(', 'asctime_r('),
 
1191
    ('ctime(', 'ctime_r('),
 
1192
    ('getgrgid(', 'getgrgid_r('),
 
1193
    ('getgrnam(', 'getgrnam_r('),
 
1194
    ('getlogin(', 'getlogin_r('),
 
1195
    ('getpwnam(', 'getpwnam_r('),
 
1196
    ('getpwuid(', 'getpwuid_r('),
 
1197
    ('gmtime(', 'gmtime_r('),
 
1198
    ('localtime(', 'localtime_r('),
 
1199
    ('rand(', 'rand_r('),
 
1200
    ('readdir(', 'readdir_r('),
 
1201
    ('strtok(', 'strtok_r('),
 
1202
    ('ttyname(', 'ttyname_r('),
 
1203
    )
 
1204
 
 
1205
 
 
1206
def CheckPosixThreading(filename, clean_lines, linenum, error):
 
1207
  """Checks for calls to thread-unsafe functions.
 
1208
 
 
1209
  Much code has been originally written without consideration of
 
1210
  multi-threading. Also, engineers are relying on their old experience;
 
1211
  they have learned posix before threading extensions were added. These
 
1212
  tests guide the engineers to use thread-safe functions (when using
 
1213
  posix directly).
 
1214
 
 
1215
  Args:
 
1216
    filename: The name of the current file.
 
1217
    clean_lines: A CleansedLines instance containing the file.
 
1218
    linenum: The number of the line to check.
 
1219
    error: The function to call with any errors found.
 
1220
  """
 
1221
  line = clean_lines.elided[linenum]
 
1222
  for single_thread_function, multithread_safe_function in threading_list:
 
1223
    ix = line.find(single_thread_function)
 
1224
    # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
 
1225
    if ix >= 0 and (ix == 0 or (not line[ix - 1].isalnum() and
 
1226
                                line[ix - 1] not in ('_', '.', '>'))):
 
1227
      error(filename, linenum, 'runtime/threadsafe_fn', 2,
 
1228
            'Consider using ' + multithread_safe_function +
 
1229
            '...) instead of ' + single_thread_function +
 
1230
            '...) for improved thread safety.')
 
1231
 
 
1232
 
 
1233
# Matches invalid increment: *count++, which moves pointer instead of
 
1234
# incrementing a value.
 
1235
_RE_PATTERN_INVALID_INCREMENT = re.compile(
 
1236
    r'^\s*\*\w+(\+\+|--);')
 
1237
 
 
1238
 
 
1239
def CheckInvalidIncrement(filename, clean_lines, linenum, error):
 
1240
  """Checks for invalid increment *count++.
 
1241
 
 
1242
  For example following function:
 
1243
  void increment_counter(int* count) {
 
1244
    *count++;
 
1245
  }
 
1246
  is invalid, because it effectively does count++, moving pointer, and should
 
1247
  be replaced with ++*count, (*count)++ or *count += 1.
 
1248
 
 
1249
  Args:
 
1250
    filename: The name of the current file.
 
1251
    clean_lines: A CleansedLines instance containing the file.
 
1252
    linenum: The number of the line to check.
 
1253
    error: The function to call with any errors found.
 
1254
  """
 
1255
  line = clean_lines.elided[linenum]
 
1256
  if _RE_PATTERN_INVALID_INCREMENT.match(line):
 
1257
    error(filename, linenum, 'runtime/invalid_increment', 5,
 
1258
          'Changing pointer instead of value (or unused value of operator*).')
 
1259
 
 
1260
 
 
1261
class _ClassInfo(object):
 
1262
  """Stores information about a class."""
 
1263
 
 
1264
  def __init__(self, name, clean_lines, linenum):
 
1265
    self.name = name
 
1266
    self.linenum = linenum
 
1267
    self.seen_open_brace = False
 
1268
    self.is_derived = False
 
1269
    self.virtual_method_linenumber = None
 
1270
    self.has_virtual_destructor = False
 
1271
    self.brace_depth = 0
 
1272
 
 
1273
    # Try to find the end of the class.  This will be confused by things like:
 
1274
    #   class A {
 
1275
    #   } *x = { ...
 
1276
    #
 
1277
    # But it's still good enough for CheckSectionSpacing.
 
1278
    self.last_line = 0
 
1279
    depth = 0
 
1280
    for i in range(linenum, clean_lines.NumLines()):
 
1281
      line = clean_lines.lines[i]
 
1282
      depth += line.count('{') - line.count('}')
 
1283
      if not depth:
 
1284
        self.last_line = i
 
1285
        break
 
1286
 
 
1287
 
 
1288
class _ClassState(object):
 
1289
  """Holds the current state of the parse relating to class declarations.
 
1290
 
 
1291
  It maintains a stack of _ClassInfos representing the parser's guess
 
1292
  as to the current nesting of class declarations. The innermost class
 
1293
  is at the top (back) of the stack. Typically, the stack will either
 
1294
  be empty or have exactly one entry.
 
1295
  """
 
1296
 
 
1297
  def __init__(self):
 
1298
    self.classinfo_stack = []
 
1299
 
 
1300
  def CheckFinished(self, filename, error):
 
1301
    """Checks that all classes have been completely parsed.
 
1302
 
 
1303
    Call this when all lines in a file have been processed.
 
1304
    Args:
 
1305
      filename: The name of the current file.
 
1306
      error: The function to call with any errors found.
 
1307
    """
 
1308
    if self.classinfo_stack:
 
1309
      # Note: This test can result in false positives if #ifdef constructs
 
1310
      # get in the way of brace matching. See the testBuildClass test in
 
1311
      # cpplint_unittest.py for an example of this.
 
1312
      error(filename, self.classinfo_stack[0].linenum, 'build/class', 5,
 
1313
            'Failed to find complete declaration of class %s' %
 
1314
            self.classinfo_stack[0].name)
 
1315
 
 
1316
 
 
1317
def CheckForNonStandardConstructs(filename, clean_lines, linenum,
 
1318
                                  class_state, error):
 
1319
  """Logs an error if we see certain non-ANSI constructs ignored by gcc-2.
 
1320
 
 
1321
  Complain about several constructs which gcc-2 accepts, but which are
 
1322
  not standard C++.  Warning about these in lint is one way to ease the
 
1323
  transition to new compilers.
 
1324
  - put storage class first (e.g. "static const" instead of "const static").
 
1325
  - "%lld" instead of %qd" in printf-type functions.
 
1326
  - "%1$d" is non-standard in printf-type functions.
 
1327
  - "\%" is an undefined character escape sequence.
 
1328
  - text after #endif is not allowed.
 
1329
  - invalid inner-style forward declaration.
 
1330
  - >? and <? operators, and their >?= and <?= cousins.
 
1331
  - classes with virtual methods need virtual destructors (compiler warning
 
1332
    available, but not turned on yet.)
 
1333
 
 
1334
  Additionally, check for constructor/destructor style violations and reference
 
1335
  members, as it is very convenient to do so while checking for
 
1336
  gcc-2 compliance.
 
1337
 
 
1338
  Args:
 
1339
    filename: The name of the current file.
 
1340
    clean_lines: A CleansedLines instance containing the file.
 
1341
    linenum: The number of the line to check.
 
1342
    class_state: A _ClassState instance which maintains information about
 
1343
                 the current stack of nested class declarations being parsed.
 
1344
    error: A callable to which errors are reported, which takes 4 arguments:
 
1345
           filename, line number, error level, and message
 
1346
  """
 
1347
 
 
1348
  # Remove comments from the line, but leave in strings for now.
 
1349
  line = clean_lines.lines[linenum]
 
1350
 
 
1351
  if Search(r'printf\s*\(.*".*%[-+ ]?\d*q', line):
 
1352
    error(filename, linenum, 'runtime/printf_format', 3,
 
1353
          '%q in format strings is deprecated.  Use %ll instead.')
 
1354
 
 
1355
  if Search(r'printf\s*\(.*".*%\d+\$', line):
 
1356
    error(filename, linenum, 'runtime/printf_format', 2,
 
1357
          '%N$ formats are unconventional.  Try rewriting to avoid them.')
 
1358
 
 
1359
  # Remove escaped backslashes before looking for undefined escapes.
 
1360
  line = line.replace('\\\\', '')
 
1361
 
 
1362
  if Search(r'("|\').*\\(%|\[|\(|{)', line):
 
1363
    error(filename, linenum, 'build/printf_format', 3,
 
1364
          '%, [, (, and { are undefined character escapes.  Unescape them.')
 
1365
 
 
1366
  # For the rest, work with both comments and strings removed.
 
1367
  line = clean_lines.elided[linenum]
 
1368
 
 
1369
  if Search(r'\b(const|volatile|void|char|short|int|long'
 
1370
            r'|float|double|signed|unsigned'
 
1371
            r'|schar|u?int8|u?int16|u?int32|u?int64)'
 
1372
            r'\s+(auto|register|static|extern|typedef)\b',
 
1373
            line):
 
1374
    error(filename, linenum, 'build/storage_class', 5,
 
1375
          'Storage class (static, extern, typedef, etc) should be first.')
 
1376
 
 
1377
  if Match(r'\s*#\s*endif\s*[^/\s]+', line):
 
1378
    error(filename, linenum, 'build/endif_comment', 5,
 
1379
          'Uncommented text after #endif is non-standard.  Use a comment.')
 
1380
 
 
1381
  if Match(r'\s*class\s+(\w+\s*::\s*)+\w+\s*;', line):
 
1382
    error(filename, linenum, 'build/forward_decl', 5,
 
1383
          'Inner-style forward declarations are invalid.  Remove this line.')
 
1384
 
 
1385
  if Search(r'(\w+|[+-]?\d+(\.\d*)?)\s*(<|>)\?=?\s*(\w+|[+-]?\d+)(\.\d*)?',
 
1386
            line):
 
1387
    error(filename, linenum, 'build/deprecated', 3,
 
1388
          '>? and <? (max and min) operators are non-standard and deprecated.')
 
1389
 
 
1390
  if Search(r'^\s*const\s*string\s*&\s*\w+\s*;', line):
 
1391
    # TODO(unknown): Could it be expanded safely to arbitrary references,
 
1392
    # without triggering too many false positives? The first
 
1393
    # attempt triggered 5 warnings for mostly benign code in the regtest, hence
 
1394
    # the restriction.
 
1395
    # Here's the original regexp, for the reference:
 
1396
    # type_name = r'\w+((\s*::\s*\w+)|(\s*<\s*\w+?\s*>))?'
 
1397
    # r'\s*const\s*' + type_name + '\s*&\s*\w+\s*;'
 
1398
    error(filename, linenum, 'runtime/member_string_references', 2,
 
1399
          'const string& members are dangerous. It is much better to use '
 
1400
          'alternatives, such as pointers or simple constants.')
 
1401
 
 
1402
  # Track class entry and exit, and attempt to find cases within the
 
1403
  # class declaration that don't meet the C++ style
 
1404
  # guidelines. Tracking is very dependent on the code matching Google
 
1405
  # style guidelines, but it seems to perform well enough in testing
 
1406
  # to be a worthwhile addition to the checks.
 
1407
  classinfo_stack = class_state.classinfo_stack
 
1408
  # Look for a class declaration. The regexp accounts for decorated classes
 
1409
  # such as in:
 
1410
  # class LOCKABLE API Object {
 
1411
  # };
 
1412
  class_decl_match = Match(
 
1413
      r'\s*(template\s*<[\w\s<>,:]*>\s*)?'
 
1414
      '(class|struct)\s+([A-Z_]+\s+)*(\w+(::\w+)*)', line)
 
1415
  if class_decl_match:
 
1416
    classinfo_stack.append(_ClassInfo(
 
1417
        class_decl_match.group(4), clean_lines, linenum))
 
1418
 
 
1419
  # Everything else in this function uses the top of the stack if it's
 
1420
  # not empty.
 
1421
  if not classinfo_stack:
 
1422
    return
 
1423
 
 
1424
  classinfo = classinfo_stack[-1]
 
1425
 
 
1426
  # If the opening brace hasn't been seen look for it and also
 
1427
  # parent class declarations.
 
1428
  if not classinfo.seen_open_brace:
 
1429
    # If the line has a ';' in it, assume it's a forward declaration or
 
1430
    # a single-line class declaration, which we won't process.
 
1431
    if line.find(';') != -1:
 
1432
      classinfo_stack.pop()
 
1433
      return
 
1434
    classinfo.seen_open_brace = (line.find('{') != -1)
 
1435
    # Look for a bare ':'
 
1436
    if Search('(^|[^:]):($|[^:])', line):
 
1437
      classinfo.is_derived = True
 
1438
    if not classinfo.seen_open_brace:
 
1439
      return  # Everything else in this function is for after open brace
 
1440
 
 
1441
  # The class may have been declared with namespace or classname qualifiers.
 
1442
  # The constructor and destructor will not have those qualifiers.
 
1443
  base_classname = classinfo.name.split('::')[-1]
 
1444
 
 
1445
  # Look for single-argument constructors that aren't marked explicit.
 
1446
  # Technically a valid construct, but against style.
 
1447
  args = Match(r'\s+(?:inline\s+)?%s\s*\(([^,()]+)\)'
 
1448
               % re.escape(base_classname),
 
1449
               line)
 
1450
  if (args and
 
1451
      args.group(1) != 'void' and
 
1452
      not Match(r'(const\s+)?%s\s*(?:<\w+>\s*)?&' % re.escape(base_classname),
 
1453
                args.group(1).strip())):
 
1454
    error(filename, linenum, 'runtime/explicit', 4,
 
1455
          'Single-argument constructors should be marked explicit.')
 
1456
 
 
1457
  # Look for methods declared virtual.
 
1458
  if Search(r'\bvirtual\b', line):
 
1459
    classinfo.virtual_method_linenumber = linenum
 
1460
    # Only look for a destructor declaration on the same line. It would
 
1461
    # be extremely unlikely for the destructor declaration to occupy
 
1462
    # more than one line.
 
1463
    if Search(r'~%s\s*\(' % base_classname, line):
 
1464
      classinfo.has_virtual_destructor = True
 
1465
 
 
1466
  # Look for class end.
 
1467
  brace_depth = classinfo.brace_depth
 
1468
  brace_depth = brace_depth + line.count('{') - line.count('}')
 
1469
  if brace_depth <= 0:
 
1470
    classinfo = classinfo_stack.pop()
 
1471
    # Try to detect missing virtual destructor declarations.
 
1472
    # For now, only warn if a non-derived class with virtual methods lacks
 
1473
    # a virtual destructor. This is to make it less likely that people will
 
1474
    # declare derived virtual destructors without declaring the base
 
1475
    # destructor virtual.
 
1476
    if ((classinfo.virtual_method_linenumber is not None) and
 
1477
        (not classinfo.has_virtual_destructor) and
 
1478
        (not classinfo.is_derived)):  # Only warn for base classes
 
1479
      error(filename, classinfo.linenum, 'runtime/virtual', 4,
 
1480
            'The class %s probably needs a virtual destructor due to '
 
1481
            'having virtual method(s), one declared at line %d.'
 
1482
            % (classinfo.name, classinfo.virtual_method_linenumber))
 
1483
  else:
 
1484
    classinfo.brace_depth = brace_depth
 
1485
 
 
1486
 
 
1487
def CheckSpacingForFunctionCall(filename, line, linenum, error):
 
1488
  """Checks for the correctness of various spacing around function calls.
 
1489
 
 
1490
  Args:
 
1491
    filename: The name of the current file.
 
1492
    line: The text of the line to check.
 
1493
    linenum: The number of the line to check.
 
1494
    error: The function to call with any errors found.
 
1495
  """
 
1496
 
 
1497
  # Since function calls often occur inside if/for/while/switch
 
1498
  # expressions - which have their own, more liberal conventions - we
 
1499
  # first see if we should be looking inside such an expression for a
 
1500
  # function call, to which we can apply more strict standards.
 
1501
  fncall = line    # if there's no control flow construct, look at whole line
 
1502
  for pattern in (r'\bif\s*\((.*)\)\s*{',
 
1503
                  r'\bfor\s*\((.*)\)\s*{',
 
1504
                  r'\bwhile\s*\((.*)\)\s*[{;]',
 
1505
                  r'\bswitch\s*\((.*)\)\s*{'):
 
1506
    match = Search(pattern, line)
 
1507
    if match:
 
1508
      fncall = match.group(1)    # look inside the parens for function calls
 
1509
      break
 
1510
 
 
1511
  # Except in if/for/while/switch, there should never be space
 
1512
  # immediately inside parens (eg "f( 3, 4 )").  We make an exception
 
1513
  # for nested parens ( (a+b) + c ).  Likewise, there should never be
 
1514
  # a space before a ( when it's a function argument.  I assume it's a
 
1515
  # function argument when the char before the whitespace is legal in
 
1516
  # a function name (alnum + _) and we're not starting a macro. Also ignore
 
1517
  # pointers and references to arrays and functions coz they're too tricky:
 
1518
  # we use a very simple way to recognize these:
 
1519
  # " (something)(maybe-something)" or
 
1520
  # " (something)(maybe-something," or
 
1521
  # " (something)[something]"
 
1522
  # Note that we assume the contents of [] to be short enough that
 
1523
  # they'll never need to wrap.
 
1524
  if (  # Ignore control structures.
 
1525
      not Search(r'\b(if|for|while|switch|return|delete)\b', fncall) and
 
1526
      # Ignore pointers/references to functions.
 
1527
      not Search(r' \([^)]+\)\([^)]*(\)|,$)', fncall) and
 
1528
      # Ignore pointers/references to arrays.
 
1529
      not Search(r' \([^)]+\)\[[^\]]+\]', fncall)):
 
1530
    if Search(r'\w\s*\(\s(?!\s*\\$)', fncall):      # a ( used for a fn call
 
1531
      error(filename, linenum, 'whitespace/parens', 4,
 
1532
            'Extra space after ( in function call')
 
1533
    elif Search(r'\(\s+(?!(\s*\\)|\()', fncall):
 
1534
      error(filename, linenum, 'whitespace/parens', 2,
 
1535
            'Extra space after (')
 
1536
    if (Search(r'\w\s+\(', fncall) and
 
1537
        not Search(r'#\s*define|typedef', fncall)):
 
1538
      error(filename, linenum, 'whitespace/parens', 4,
 
1539
            'Extra space before ( in function call')
 
1540
    # If the ) is followed only by a newline or a { + newline, assume it's
 
1541
    # part of a control statement (if/while/etc), and don't complain
 
1542
    if Search(r'[^)]\s+\)\s*[^{\s]', fncall):
 
1543
      # If the closing parenthesis is preceded by only whitespaces,
 
1544
      # try to give a more descriptive error message.
 
1545
      if Search(r'^\s+\)', fncall):
 
1546
        error(filename, linenum, 'whitespace/parens', 2,
 
1547
              'Closing ) should be moved to the previous line')
 
1548
      else:
 
1549
        error(filename, linenum, 'whitespace/parens', 2,
 
1550
              'Extra space before )')
 
1551
 
 
1552
 
 
1553
def IsBlankLine(line):
 
1554
  """Returns true if the given line is blank.
 
1555
 
 
1556
  We consider a line to be blank if the line is empty or consists of
 
1557
  only white spaces.
 
1558
 
 
1559
  Args:
 
1560
    line: A line of a string.
 
1561
 
 
1562
  Returns:
 
1563
    True, if the given line is blank.
 
1564
  """
 
1565
  return not line or line.isspace()
 
1566
 
 
1567
 
 
1568
def CheckForFunctionLengths(filename, clean_lines, linenum,
 
1569
                            function_state, error):
 
1570
  """Reports for long function bodies.
 
1571
 
 
1572
  For an overview why this is done, see:
 
1573
  http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
 
1574
 
 
1575
  Uses a simplistic algorithm assuming other style guidelines
 
1576
  (especially spacing) are followed.
 
1577
  Only checks unindented functions, so class members are unchecked.
 
1578
  Trivial bodies are unchecked, so constructors with huge initializer lists
 
1579
  may be missed.
 
1580
  Blank/comment lines are not counted so as to avoid encouraging the removal
 
1581
  of vertical space and comments just to get through a lint check.
 
1582
  NOLINT *on the last line of a function* disables this check.
 
1583
 
 
1584
  Args:
 
1585
    filename: The name of the current file.
 
1586
    clean_lines: A CleansedLines instance containing the file.
 
1587
    linenum: The number of the line to check.
 
1588
    function_state: Current function name and lines in body so far.
 
1589
    error: The function to call with any errors found.
 
1590
  """
 
1591
  lines = clean_lines.lines
 
1592
  line = lines[linenum]
 
1593
  raw = clean_lines.raw_lines
 
1594
  raw_line = raw[linenum]
 
1595
  joined_line = ''
 
1596
 
 
1597
  starting_func = False
 
1598
  regexp = r'(\w(\w|::|\*|\&|\s)*)\('  # decls * & space::name( ...
 
1599
  match_result = Match(regexp, line)
 
1600
  if match_result:
 
1601
    # If the name is all caps and underscores, figure it's a macro and
 
1602
    # ignore it, unless it's TEST or TEST_F.
 
1603
    function_name = match_result.group(1).split()[-1]
 
1604
    if function_name == 'TEST' or function_name == 'TEST_F' or (
 
1605
        not Match(r'[A-Z_]+$', function_name)):
 
1606
      starting_func = True
 
1607
 
 
1608
  if starting_func:
 
1609
    body_found = False
 
1610
    for start_linenum in xrange(linenum, clean_lines.NumLines()):
 
1611
      start_line = lines[start_linenum]
 
1612
      joined_line += ' ' + start_line.lstrip()
 
1613
      if Search(r'(;|})', start_line):  # Declarations and trivial functions
 
1614
        body_found = True
 
1615
        break                              # ... ignore
 
1616
      elif Search(r'{', start_line):
 
1617
        body_found = True
 
1618
        function = Search(r'((\w|:)*)\(', line).group(1)
 
1619
        if Match(r'TEST', function):    # Handle TEST... macros
 
1620
          parameter_regexp = Search(r'(\(.*\))', joined_line)
 
1621
          if parameter_regexp:             # Ignore bad syntax
 
1622
            function += parameter_regexp.group(1)
 
1623
        else:
 
1624
          function += '()'
 
1625
        function_state.Begin(function)
 
1626
        break
 
1627
    if not body_found:
 
1628
      # No body for the function (or evidence of a non-function) was found.
 
1629
      error(filename, linenum, 'readability/fn_size', 5,
 
1630
            'Lint failed to find start of function body.')
 
1631
  elif Match(r'^\}\s*$', line):  # function end
 
1632
    function_state.Check(error, filename, linenum)
 
1633
    function_state.End()
 
1634
  elif not Match(r'^\s*$', line):
 
1635
    function_state.Count()  # Count non-blank/non-comment lines.
 
1636
 
 
1637
 
 
1638
_RE_PATTERN_TODO = re.compile(r'^//(\s*)TODO(\(.+?\))?:?(\s|$)?')
 
1639
 
 
1640
 
 
1641
def CheckComment(comment, filename, linenum, error):
 
1642
  """Checks for common mistakes in TODO comments.
 
1643
 
 
1644
  Args:
 
1645
    comment: The text of the comment from the line in question.
 
1646
    filename: The name of the current file.
 
1647
    linenum: The number of the line to check.
 
1648
    error: The function to call with any errors found.
 
1649
  """
 
1650
  match = _RE_PATTERN_TODO.match(comment)
 
1651
  if match:
 
1652
    # One whitespace is correct; zero whitespace is handled elsewhere.
 
1653
    leading_whitespace = match.group(1)
 
1654
    if len(leading_whitespace) > 1:
 
1655
      error(filename, linenum, 'whitespace/todo', 2,
 
1656
            'Too many spaces before TODO')
 
1657
 
 
1658
    username = match.group(2)
 
1659
    if not username:
 
1660
      error(filename, linenum, 'readability/todo', 2,
 
1661
            'Missing username in TODO; it should look like '
 
1662
            '"// TODO(my_username): Stuff."')
 
1663
 
 
1664
    middle_whitespace = match.group(3)
 
1665
    # Comparisons made explicit for correctness -- pylint: disable-msg=C6403
 
1666
    if middle_whitespace != ' ' and middle_whitespace != '':
 
1667
      error(filename, linenum, 'whitespace/todo', 2,
 
1668
            'TODO(my_username) should be followed by a space')
 
1669
 
 
1670
 
 
1671
def CheckSpacing(filename, clean_lines, linenum, error):
 
1672
  """Checks for the correctness of various spacing issues in the code.
 
1673
 
 
1674
  Things we check for: spaces around operators, spaces after
 
1675
  if/for/while/switch, no spaces around parens in function calls, two
 
1676
  spaces between code and comment, don't start a block with a blank
 
1677
  line, don't end a function with a blank line, don't add a blank line
 
1678
  after public/protected/private, don't have too many blank lines in a row.
 
1679
 
 
1680
  Args:
 
1681
    filename: The name of the current file.
 
1682
    clean_lines: A CleansedLines instance containing the file.
 
1683
    linenum: The number of the line to check.
 
1684
    error: The function to call with any errors found.
 
1685
  """
 
1686
 
 
1687
  raw = clean_lines.raw_lines
 
1688
  line = raw[linenum]
 
1689
 
 
1690
  # Before nixing comments, check if the line is blank for no good
 
1691
  # reason.  This includes the first line after a block is opened, and
 
1692
  # blank lines at the end of a function (ie, right before a line like '}'
 
1693
  if IsBlankLine(line):
 
1694
    elided = clean_lines.elided
 
1695
    prev_line = elided[linenum - 1]
 
1696
    prevbrace = prev_line.rfind('{')
 
1697
    # TODO(unknown): Don't complain if line before blank line, and line after,
 
1698
    #                both start with alnums and are indented the same amount.
 
1699
    #                This ignores whitespace at the start of a namespace block
 
1700
    #                because those are not usually indented.
 
1701
    if (prevbrace != -1 and prev_line[prevbrace:].find('}') == -1
 
1702
        and prev_line[:prevbrace].find('namespace') == -1):
 
1703
      # OK, we have a blank line at the start of a code block.  Before we
 
1704
      # complain, we check if it is an exception to the rule: The previous
 
1705
      # non-empty line has the parameters of a function header that are indented
 
1706
      # 4 spaces (because they did not fit in a 80 column line when placed on
 
1707
      # the same line as the function name).  We also check for the case where
 
1708
      # the previous line is indented 6 spaces, which may happen when the
 
1709
      # initializers of a constructor do not fit into a 80 column line.
 
1710
      exception = False
 
1711
      if Match(r' {6}\w', prev_line):  # Initializer list?
 
1712
        # We are looking for the opening column of initializer list, which
 
1713
        # should be indented 4 spaces to cause 6 space indentation afterwards.
 
1714
        search_position = linenum-2
 
1715
        while (search_position >= 0
 
1716
               and Match(r' {6}\w', elided[search_position])):
 
1717
          search_position -= 1
 
1718
        exception = (search_position >= 0
 
1719
                     and elided[search_position][:5] == '    :')
 
1720
      else:
 
1721
        # Search for the function arguments or an initializer list.  We use a
 
1722
        # simple heuristic here: If the line is indented 4 spaces; and we have a
 
1723
        # closing paren, without the opening paren, followed by an opening brace
 
1724
        # or colon (for initializer lists) we assume that it is the last line of
 
1725
        # a function header.  If we have a colon indented 4 spaces, it is an
 
1726
        # initializer list.
 
1727
        exception = (Match(r' {4}\w[^\(]*\)\s*(const\s*)?(\{\s*$|:)',
 
1728
                           prev_line)
 
1729
                     or Match(r' {4}:', prev_line))
 
1730
 
 
1731
      if not exception:
 
1732
        error(filename, linenum, 'whitespace/blank_line', 5,
 
1733
              'Blank line at the start of a code block.  Is this needed?')
 
1734
    # This doesn't ignore whitespace at the end of a namespace block
 
1735
    # because that is too hard without pairing open/close braces;
 
1736
    # however, a special exception is made for namespace closing
 
1737
    # brackets which have a comment containing "namespace".
 
1738
    #
 
1739
    # Also, ignore blank lines at the end of a block in a long if-else
 
1740
    # chain, like this:
 
1741
    #   if (condition1) {
 
1742
    #     // Something followed by a blank line
 
1743
    #
 
1744
    #   } else if (condition2) {
 
1745
    #     // Something else
 
1746
    #   }
 
1747
    if linenum + 1 < clean_lines.NumLines():
 
1748
      next_line = raw[linenum + 1]
 
1749
      if (next_line
 
1750
          and Match(r'\s*}', next_line)
 
1751
          and next_line.find('namespace') == -1
 
1752
          and next_line.find('} else ') == -1):
 
1753
        error(filename, linenum, 'whitespace/blank_line', 5,
 
1754
              'Blank line at the end of a code block.  Is this needed?')
 
1755
 
 
1756
    matched = Match(r'\s*(public|protected|private):', prev_line)
 
1757
    if matched:
 
1758
      error(filename, linenum, 'whitespace/blank_line', 3,
 
1759
            'Do not leave a blank line after "%s:"' % matched.group(1))
 
1760
 
 
1761
  # Next, we complain if there's a comment too near the text
 
1762
  commentpos = line.find('//')
 
1763
  if commentpos != -1:
 
1764
    # Check if the // may be in quotes.  If so, ignore it
 
1765
    # Comparisons made explicit for clarity -- pylint: disable-msg=C6403
 
1766
    if (line.count('"', 0, commentpos) -
 
1767
        line.count('\\"', 0, commentpos)) % 2 == 0:   # not in quotes
 
1768
      # Allow one space for new scopes, two spaces otherwise:
 
1769
      if (not Match(r'^\s*{ //', line) and
 
1770
          ((commentpos >= 1 and
 
1771
            line[commentpos-1] not in string.whitespace) or
 
1772
           (commentpos >= 2 and
 
1773
            line[commentpos-2] not in string.whitespace))):
 
1774
        error(filename, linenum, 'whitespace/comments', 2,
 
1775
              'At least two spaces is best between code and comments')
 
1776
      # There should always be a space between the // and the comment
 
1777
      commentend = commentpos + 2
 
1778
      if commentend < len(line) and not line[commentend] == ' ':
 
1779
        # but some lines are exceptions -- e.g. if they're big
 
1780
        # comment delimiters like:
 
1781
        # //----------------------------------------------------------
 
1782
        # or are an empty C++ style Doxygen comment, like:
 
1783
        # ///
 
1784
        # or they begin with multiple slashes followed by a space:
 
1785
        # //////// Header comment
 
1786
        match = (Search(r'[=/-]{4,}\s*$', line[commentend:]) or
 
1787
                 Search(r'^/$', line[commentend:]) or
 
1788
                 Search(r'^/+ ', line[commentend:]))
 
1789
        if not match:
 
1790
          error(filename, linenum, 'whitespace/comments', 4,
 
1791
                'Should have a space between // and comment')
 
1792
      CheckComment(line[commentpos:], filename, linenum, error)
 
1793
 
 
1794
  line = clean_lines.elided[linenum]  # get rid of comments and strings
 
1795
 
 
1796
  # Don't try to do spacing checks for operator methods
 
1797
  line = re.sub(r'operator(==|!=|<|<<|<=|>=|>>|>)\(', 'operator\(', line)
 
1798
 
 
1799
  # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
 
1800
  # Otherwise not.  Note we only check for non-spaces on *both* sides;
 
1801
  # sometimes people put non-spaces on one side when aligning ='s among
 
1802
  # many lines (not that this is behavior that I approve of...)
 
1803
  if Search(r'[\w.]=[\w.]', line) and not Search(r'\b(if|while) ', line):
 
1804
    error(filename, linenum, 'whitespace/operators', 4,
 
1805
          'Missing spaces around =')
 
1806
 
 
1807
  # It's ok not to have spaces around binary operators like + - * /, but if
 
1808
  # there's too little whitespace, we get concerned.  It's hard to tell,
 
1809
  # though, so we punt on this one for now.  TODO.
 
1810
 
 
1811
  # You should always have whitespace around binary operators.
 
1812
  # Alas, we can't test < or > because they're legitimately used sans spaces
 
1813
  # (a->b, vector<int> a).  The only time we can tell is a < with no >, and
 
1814
  # only if it's not template params list spilling into the next line.
 
1815
  match = Search(r'[^<>=!\s](==|!=|<=|>=)[^<>=!\s]', line)
 
1816
  if not match:
 
1817
    # Note that while it seems that the '<[^<]*' term in the following
 
1818
    # regexp could be simplified to '<.*', which would indeed match
 
1819
    # the same class of strings, the [^<] means that searching for the
 
1820
    # regexp takes linear rather than quadratic time.
 
1821
    if not Search(r'<[^<]*,\s*$', line):  # template params spill
 
1822
      match = Search(r'[^<>=!\s](<)[^<>=!\s]([^>]|->)*$', line)
 
1823
  if match:
 
1824
    error(filename, linenum, 'whitespace/operators', 3,
 
1825
          'Missing spaces around %s' % match.group(1))
 
1826
  # We allow no-spaces around << and >> when used like this: 10<<20, but
 
1827
  # not otherwise (particularly, not when used as streams)
 
1828
  match = Search(r'[^0-9\s](<<|>>)[^0-9\s]', line)
 
1829
  if match:
 
1830
    error(filename, linenum, 'whitespace/operators', 3,
 
1831
          'Missing spaces around %s' % match.group(1))
 
1832
 
 
1833
  # There shouldn't be space around unary operators
 
1834
  match = Search(r'(!\s|~\s|[\s]--[\s;]|[\s]\+\+[\s;])', line)
 
1835
  if match:
 
1836
    error(filename, linenum, 'whitespace/operators', 4,
 
1837
          'Extra space for operator %s' % match.group(1))
 
1838
 
 
1839
  # A pet peeve of mine: no spaces after an if, while, switch, or for
 
1840
  match = Search(r' (if\(|for\(|while\(|switch\()', line)
 
1841
  if match:
 
1842
    error(filename, linenum, 'whitespace/parens', 5,
 
1843
          'Missing space before ( in %s' % match.group(1))
 
1844
 
 
1845
  # For if/for/while/switch, the left and right parens should be
 
1846
  # consistent about how many spaces are inside the parens, and
 
1847
  # there should either be zero or one spaces inside the parens.
 
1848
  # We don't want: "if ( foo)" or "if ( foo   )".
 
1849
  # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed.
 
1850
  match = Search(r'\b(if|for|while|switch)\s*'
 
1851
                 r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$',
 
1852
                 line)
 
1853
  if match:
 
1854
    if len(match.group(2)) != len(match.group(4)):
 
1855
      if not (match.group(3) == ';' and
 
1856
              len(match.group(2)) == 1 + len(match.group(4)) or
 
1857
              not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)):
 
1858
        error(filename, linenum, 'whitespace/parens', 5,
 
1859
              'Mismatching spaces inside () in %s' % match.group(1))
 
1860
    if not len(match.group(2)) in [0, 1]:
 
1861
      error(filename, linenum, 'whitespace/parens', 5,
 
1862
            'Should have zero or one spaces inside ( and ) in %s' %
 
1863
            match.group(1))
 
1864
 
 
1865
  # You should always have a space after a comma (either as fn arg or operator)
 
1866
  if Search(r',[^\s]', line):
 
1867
    error(filename, linenum, 'whitespace/comma', 3,
 
1868
          'Missing space after ,')
 
1869
 
 
1870
  # You should always have a space after a semicolon
 
1871
  # except for few corner cases
 
1872
  # TODO(unknown): clarify if 'if (1) { return 1;}' is requires one more
 
1873
  # space after ;
 
1874
  if Search(r';[^\s};\\)/]', line):
 
1875
    error(filename, linenum, 'whitespace/semicolon', 3,
 
1876
          'Missing space after ;')
 
1877
 
 
1878
  # Next we will look for issues with function calls.
 
1879
  CheckSpacingForFunctionCall(filename, line, linenum, error)
 
1880
 
 
1881
  # Except after an opening paren, or after another opening brace (in case of
 
1882
  # an initializer list, for instance), you should have spaces before your
 
1883
  # braces. And since you should never have braces at the beginning of a line,
 
1884
  # this is an easy test.
 
1885
  if Search(r'[^ ({]{', line):
 
1886
    error(filename, linenum, 'whitespace/braces', 5,
 
1887
          'Missing space before {')
 
1888
 
 
1889
  # Make sure '} else {' has spaces.
 
1890
  if Search(r'}else', line):
 
1891
    error(filename, linenum, 'whitespace/braces', 5,
 
1892
          'Missing space before else')
 
1893
 
 
1894
  # You shouldn't have spaces before your brackets, except maybe after
 
1895
  # 'delete []' or 'new char * []'.
 
1896
  if Search(r'\w\s+\[', line) and not Search(r'delete\s+\[', line):
 
1897
    error(filename, linenum, 'whitespace/braces', 5,
 
1898
          'Extra space before [')
 
1899
 
 
1900
  # You shouldn't have a space before a semicolon at the end of the line.
 
1901
  # There's a special case for "for" since the style guide allows space before
 
1902
  # the semicolon there.
 
1903
  if Search(r':\s*;\s*$', line):
 
1904
    error(filename, linenum, 'whitespace/semicolon', 5,
 
1905
          'Semicolon defining empty statement. Use { } instead.')
 
1906
  elif Search(r'^\s*;\s*$', line):
 
1907
    error(filename, linenum, 'whitespace/semicolon', 5,
 
1908
          'Line contains only semicolon. If this should be an empty statement, '
 
1909
          'use { } instead.')
 
1910
  elif (Search(r'\s+;\s*$', line) and
 
1911
        not Search(r'\bfor\b', line)):
 
1912
    error(filename, linenum, 'whitespace/semicolon', 5,
 
1913
          'Extra space before last semicolon. If this should be an empty '
 
1914
          'statement, use { } instead.')
 
1915
 
 
1916
 
 
1917
def CheckSectionSpacing(filename, clean_lines, class_info, linenum, error):
 
1918
  """Checks for additional blank line issues related to sections.
 
1919
 
 
1920
  Currently the only thing checked here is blank line before protected/private.
 
1921
 
 
1922
  Args:
 
1923
    filename: The name of the current file.
 
1924
    clean_lines: A CleansedLines instance containing the file.
 
1925
    class_info: A _ClassInfo objects.
 
1926
    linenum: The number of the line to check.
 
1927
    error: The function to call with any errors found.
 
1928
  """
 
1929
  # Skip checks if the class is small, where small means 25 lines or less.
 
1930
  # 25 lines seems like a good cutoff since that's the usual height of
 
1931
  # terminals, and any class that can't fit in one screen can't really
 
1932
  # be considered "small".
 
1933
  #
 
1934
  # Also skip checks if we are on the first line.  This accounts for
 
1935
  # classes that look like
 
1936
  #   class Foo { public: ... };
 
1937
  #
 
1938
  # If we didn't find the end of the class, last_line would be zero,
 
1939
  # and the check will be skipped by the first condition.
 
1940
  if (class_info.last_line - class_info.linenum <= 24 or
 
1941
      linenum <= class_info.linenum):
 
1942
    return
 
1943
 
 
1944
  matched = Match(r'\s*(public|protected|private):', clean_lines.lines[linenum])
 
1945
  if matched:
 
1946
    # Issue warning if the line before public/protected/private was
 
1947
    # not a blank line, but don't do this if the previous line contains
 
1948
    # "class" or "struct".  This can happen two ways:
 
1949
    #  - We are at the beginning of the class.
 
1950
    #  - We are forward-declaring an inner class that is semantically
 
1951
    #    private, but needed to be public for implementation reasons.
 
1952
    prev_line = clean_lines.lines[linenum - 1]
 
1953
    if (not IsBlankLine(prev_line) and
 
1954
        not Search(r'\b(class|struct)\b', prev_line)):
 
1955
      # Try a bit harder to find the beginning of the class.  This is to
 
1956
      # account for multi-line base-specifier lists, e.g.:
 
1957
      #   class Derived
 
1958
      #       : public Base {
 
1959
      end_class_head = class_info.linenum
 
1960
      for i in range(class_info.linenum, linenum):
 
1961
        if Search(r'\{\s*$', clean_lines.lines[i]):
 
1962
          end_class_head = i
 
1963
          break
 
1964
      if end_class_head < linenum - 1:
 
1965
        error(filename, linenum, 'whitespace/blank_line', 3,
 
1966
              '"%s:" should be preceded by a blank line' % matched.group(1))
 
1967
 
 
1968
 
 
1969
def GetPreviousNonBlankLine(clean_lines, linenum):
 
1970
  """Return the most recent non-blank line and its line number.
 
1971
 
 
1972
  Args:
 
1973
    clean_lines: A CleansedLines instance containing the file contents.
 
1974
    linenum: The number of the line to check.
 
1975
 
 
1976
  Returns:
 
1977
    A tuple with two elements.  The first element is the contents of the last
 
1978
    non-blank line before the current line, or the empty string if this is the
 
1979
    first non-blank line.  The second is the line number of that line, or -1
 
1980
    if this is the first non-blank line.
 
1981
  """
 
1982
 
 
1983
  prevlinenum = linenum - 1
 
1984
  while prevlinenum >= 0:
 
1985
    prevline = clean_lines.elided[prevlinenum]
 
1986
    if not IsBlankLine(prevline):     # if not a blank line...
 
1987
      return (prevline, prevlinenum)
 
1988
    prevlinenum -= 1
 
1989
  return ('', -1)
 
1990
 
 
1991
 
 
1992
def CheckBraces(filename, clean_lines, linenum, error):
 
1993
  """Looks for misplaced braces (e.g. at the end of line).
 
1994
 
 
1995
  Args:
 
1996
    filename: The name of the current file.
 
1997
    clean_lines: A CleansedLines instance containing the file.
 
1998
    linenum: The number of the line to check.
 
1999
    error: The function to call with any errors found.
 
2000
  """
 
2001
 
 
2002
  line = clean_lines.elided[linenum]        # get rid of comments and strings
 
2003
 
 
2004
  if Match(r'\s*{\s*$', line):
 
2005
    # We allow an open brace to start a line in the case where someone
 
2006
    # is using braces in a block to explicitly create a new scope,
 
2007
    # which is commonly used to control the lifetime of
 
2008
    # stack-allocated variables.  We don't detect this perfectly: we
 
2009
    # just don't complain if the last non-whitespace character on the
 
2010
    # previous non-blank line is ';', ':', '{', or '}'.
 
2011
    prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
 
2012
    if not Search(r'[;:}{]\s*$', prevline):
 
2013
      error(filename, linenum, 'whitespace/braces', 4,
 
2014
            '{ should almost always be at the end of the previous line')
 
2015
 
 
2016
  # An else clause should be on the same line as the preceding closing brace.
 
2017
  if Match(r'\s*else\s*', line):
 
2018
    prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
 
2019
    if Match(r'\s*}\s*$', prevline):
 
2020
      error(filename, linenum, 'whitespace/newline', 4,
 
2021
            'An else should appear on the same line as the preceding }')
 
2022
 
 
2023
  # If braces come on one side of an else, they should be on both.
 
2024
  # However, we have to worry about "else if" that spans multiple lines!
 
2025
  if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
 
2026
    if Search(r'}\s*else if([^{]*)$', line):       # could be multi-line if
 
2027
      # find the ( after the if
 
2028
      pos = line.find('else if')
 
2029
      pos = line.find('(', pos)
 
2030
      if pos > 0:
 
2031
        (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
 
2032
        if endline[endpos:].find('{') == -1:    # must be brace after if
 
2033
          error(filename, linenum, 'readability/braces', 5,
 
2034
                'If an else has a brace on one side, it should have it on both')
 
2035
    else:            # common case: else not followed by a multi-line if
 
2036
      error(filename, linenum, 'readability/braces', 5,
 
2037
            'If an else has a brace on one side, it should have it on both')
 
2038
 
 
2039
  # Likewise, an else should never have the else clause on the same line
 
2040
  if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
 
2041
    error(filename, linenum, 'whitespace/newline', 4,
 
2042
          'Else clause should never be on same line as else (use 2 lines)')
 
2043
 
 
2044
  # In the same way, a do/while should never be on one line
 
2045
  if Match(r'\s*do [^\s{]', line):
 
2046
    error(filename, linenum, 'whitespace/newline', 4,
 
2047
          'do/while clauses should not be on a single line')
 
2048
 
 
2049
  # Braces shouldn't be followed by a ; unless they're defining a struct
 
2050
  # or initializing an array.
 
2051
  # We can't tell in general, but we can for some common cases.
 
2052
  prevlinenum = linenum
 
2053
  while True:
 
2054
    (prevline, prevlinenum) = GetPreviousNonBlankLine(clean_lines, prevlinenum)
 
2055
    if Match(r'\s+{.*}\s*;', line) and not prevline.count(';'):
 
2056
      line = prevline + line
 
2057
    else:
 
2058
      break
 
2059
  if (Search(r'{.*}\s*;', line) and
 
2060
      line.count('{') == line.count('}') and
 
2061
      not Search(r'struct|class|enum|\s*=\s*{', line)):
 
2062
    error(filename, linenum, 'readability/braces', 4,
 
2063
          "You don't need a ; after a }")
 
2064
 
 
2065
 
 
2066
def ReplaceableCheck(operator, macro, line):
 
2067
  """Determine whether a basic CHECK can be replaced with a more specific one.
 
2068
 
 
2069
  For example suggest using CHECK_EQ instead of CHECK(a == b) and
 
2070
  similarly for CHECK_GE, CHECK_GT, CHECK_LE, CHECK_LT, CHECK_NE.
 
2071
 
 
2072
  Args:
 
2073
    operator: The C++ operator used in the CHECK.
 
2074
    macro: The CHECK or EXPECT macro being called.
 
2075
    line: The current source line.
 
2076
 
 
2077
  Returns:
 
2078
    True if the CHECK can be replaced with a more specific one.
 
2079
  """
 
2080
 
 
2081
  # This matches decimal and hex integers, strings, and chars (in that order).
 
2082
  match_constant = r'([-+]?(\d+|0[xX][0-9a-fA-F]+)[lLuU]{0,3}|".*"|\'.*\')'
 
2083
 
 
2084
  # Expression to match two sides of the operator with something that
 
2085
  # looks like a literal, since CHECK(x == iterator) won't compile.
 
2086
  # This means we can't catch all the cases where a more specific
 
2087
  # CHECK is possible, but it's less annoying than dealing with
 
2088
  # extraneous warnings.
 
2089
  match_this = (r'\s*' + macro + r'\((\s*' +
 
2090
                match_constant + r'\s*' + operator + r'[^<>].*|'
 
2091
                r'.*[^<>]' + operator + r'\s*' + match_constant +
 
2092
                r'\s*\))')
 
2093
 
 
2094
  # Don't complain about CHECK(x == NULL) or similar because
 
2095
  # CHECK_EQ(x, NULL) won't compile (requires a cast).
 
2096
  # Also, don't complain about more complex boolean expressions
 
2097
  # involving && or || such as CHECK(a == b || c == d).
 
2098
  return Match(match_this, line) and not Search(r'NULL|&&|\|\|', line)
 
2099
 
 
2100
 
 
2101
def CheckCheck(filename, clean_lines, linenum, error):
 
2102
  """Checks the use of CHECK and EXPECT macros.
 
2103
 
 
2104
  Args:
 
2105
    filename: The name of the current file.
 
2106
    clean_lines: A CleansedLines instance containing the file.
 
2107
    linenum: The number of the line to check.
 
2108
    error: The function to call with any errors found.
 
2109
  """
 
2110
 
 
2111
  # Decide the set of replacement macros that should be suggested
 
2112
  raw_lines = clean_lines.raw_lines
 
2113
  current_macro = ''
 
2114
  for macro in _CHECK_MACROS:
 
2115
    if raw_lines[linenum].find(macro) >= 0:
 
2116
      current_macro = macro
 
2117
      break
 
2118
  if not current_macro:
 
2119
    # Don't waste time here if line doesn't contain 'CHECK' or 'EXPECT'
 
2120
    return
 
2121
 
 
2122
  line = clean_lines.elided[linenum]        # get rid of comments and strings
 
2123
 
 
2124
  # Encourage replacing plain CHECKs with CHECK_EQ/CHECK_NE/etc.
 
2125
  for operator in ['==', '!=', '>=', '>', '<=', '<']:
 
2126
    if ReplaceableCheck(operator, current_macro, line):
 
2127
      error(filename, linenum, 'readability/check', 2,
 
2128
            'Consider using %s instead of %s(a %s b)' % (
 
2129
                _CHECK_REPLACEMENT[current_macro][operator],
 
2130
                current_macro, operator))
 
2131
      break
 
2132
 
 
2133
 
 
2134
def GetLineWidth(line):
 
2135
  """Determines the width of the line in column positions.
 
2136
 
 
2137
  Args:
 
2138
    line: A string, which may be a Unicode string.
 
2139
 
 
2140
  Returns:
 
2141
    The width of the line in column positions, accounting for Unicode
 
2142
    combining characters and wide characters.
 
2143
  """
 
2144
  if isinstance(line, unicode):
 
2145
    width = 0
 
2146
    for uc in unicodedata.normalize('NFC', line):
 
2147
      if unicodedata.east_asian_width(uc) in ('W', 'F'):
 
2148
        width += 2
 
2149
      elif not unicodedata.combining(uc):
 
2150
        width += 1
 
2151
    return width
 
2152
  else:
 
2153
    return len(line)
 
2154
 
 
2155
 
 
2156
def CheckStyle(filename, clean_lines, linenum, file_extension, class_state,
 
2157
               error):
 
2158
  """Checks rules from the 'C++ style rules' section of cppguide.html.
 
2159
 
 
2160
  Most of these rules are hard to test (naming, comment style), but we
 
2161
  do what we can.  In particular we check for 2-space indents, line lengths,
 
2162
  tab usage, spaces inside code, etc.
 
2163
 
 
2164
  Args:
 
2165
    filename: The name of the current file.
 
2166
    clean_lines: A CleansedLines instance containing the file.
 
2167
    linenum: The number of the line to check.
 
2168
    file_extension: The extension (without the dot) of the filename.
 
2169
    error: The function to call with any errors found.
 
2170
  """
 
2171
 
 
2172
  raw_lines = clean_lines.raw_lines
 
2173
  line = raw_lines[linenum]
 
2174
 
 
2175
  if line.find('\t') != -1:
 
2176
    error(filename, linenum, 'whitespace/tab', 5,
 
2177
          'Tab found; better to use spaces')
 
2178
 
 
2179
  # One or three blank spaces at the beginning of the line is weird; it's
 
2180
  # hard to reconcile that with 2-space indents.
 
2181
  # NOTE: here are the conditions rob pike used for his tests.  Mine aren't
 
2182
  # as sophisticated, but it may be worth becoming so:  RLENGTH==initial_spaces
 
2183
  # if(RLENGTH > 20) complain = 0;
 
2184
  # if(match($0, " +(error|private|public|protected):")) complain = 0;
 
2185
  # if(match(prev, "&& *$")) complain = 0;
 
2186
  # if(match(prev, "\\|\\| *$")) complain = 0;
 
2187
  # if(match(prev, "[\",=><] *$")) complain = 0;
 
2188
  # if(match($0, " <<")) complain = 0;
 
2189
  # if(match(prev, " +for \\(")) complain = 0;
 
2190
  # if(prevodd && match(prevprev, " +for \\(")) complain = 0;
 
2191
  initial_spaces = 0
 
2192
  cleansed_line = clean_lines.elided[linenum]
 
2193
  while initial_spaces < len(line) and line[initial_spaces] == ' ':
 
2194
    initial_spaces += 1
 
2195
  if line and line[-1].isspace():
 
2196
    error(filename, linenum, 'whitespace/end_of_line', 4,
 
2197
          'Line ends in whitespace.  Consider deleting these extra spaces.')
 
2198
  # There are certain situations we allow one space, notably for labels
 
2199
  elif ((initial_spaces == 1 or initial_spaces == 3) and
 
2200
        not Match(r'\s*\w+\s*:\s*$', cleansed_line)):
 
2201
    error(filename, linenum, 'whitespace/indent', 3,
 
2202
          'Weird number of spaces at line-start.  '
 
2203
          'Are you using a 2-space indent?')
 
2204
  # Labels should always be indented at least one space.
 
2205
  elif not initial_spaces and line[:2] != '//' and Search(r'[^:]:\s*$',
 
2206
                                                          line):
 
2207
    error(filename, linenum, 'whitespace/labels', 4,
 
2208
          'Labels should always be indented at least one space.  '
 
2209
          'If this is a member-initializer list in a constructor or '
 
2210
          'the base class list in a class definition, the colon should '
 
2211
          'be on the following line.')
 
2212
 
 
2213
 
 
2214
  # Check if the line is a header guard.
 
2215
  is_header_guard = False
 
2216
  if file_extension == 'h':
 
2217
    cppvar = GetHeaderGuardCPPVariable(filename)
 
2218
    if (line.startswith('#ifndef %s' % cppvar) or
 
2219
        line.startswith('#define %s' % cppvar) or
 
2220
        line.startswith('#endif  // %s' % cppvar)):
 
2221
      is_header_guard = True
 
2222
  # #include lines and header guards can be long, since there's no clean way to
 
2223
  # split them.
 
2224
  #
 
2225
  # URLs can be long too.  It's possible to split these, but it makes them
 
2226
  # harder to cut&paste.
 
2227
  #
 
2228
  # The "$Id:...$" comment may also get very long without it being the
 
2229
  # developers fault.
 
2230
  if (not line.startswith('#include') and not is_header_guard and
 
2231
      not Match(r'^\s*//.*http(s?)://\S*$', line) and
 
2232
      not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
 
2233
    line_width = GetLineWidth(line)
 
2234
    if line_width > 100:
 
2235
      error(filename, linenum, 'whitespace/line_length', 4,
 
2236
            'Lines should very rarely be longer than 100 characters')
 
2237
    elif line_width > 80:
 
2238
      error(filename, linenum, 'whitespace/line_length', 2,
 
2239
            'Lines should be <= 80 characters long')
 
2240
 
 
2241
  if (cleansed_line.count(';') > 1 and
 
2242
      # for loops are allowed two ;'s (and may run over two lines).
 
2243
      cleansed_line.find('for') == -1 and
 
2244
      (GetPreviousNonBlankLine(clean_lines, linenum)[0].find('for') == -1 or
 
2245
       GetPreviousNonBlankLine(clean_lines, linenum)[0].find(';') != -1) and
 
2246
      # It's ok to have many commands in a switch case that fits in 1 line
 
2247
      not ((cleansed_line.find('case ') != -1 or
 
2248
            cleansed_line.find('default:') != -1) and
 
2249
           cleansed_line.find('break;') != -1)):
 
2250
    error(filename, linenum, 'whitespace/newline', 4,
 
2251
          'More than one command on the same line')
 
2252
 
 
2253
  # Some more style checks
 
2254
  CheckBraces(filename, clean_lines, linenum, error)
 
2255
  CheckSpacing(filename, clean_lines, linenum, error)
 
2256
  CheckCheck(filename, clean_lines, linenum, error)
 
2257
  if class_state and class_state.classinfo_stack:
 
2258
    CheckSectionSpacing(filename, clean_lines,
 
2259
                        class_state.classinfo_stack[-1], linenum, error)
 
2260
 
 
2261
 
 
2262
_RE_PATTERN_INCLUDE_NEW_STYLE = re.compile(r'#include +"[^/]+\.h"')
 
2263
_RE_PATTERN_INCLUDE = re.compile(r'^\s*#\s*include\s*([<"])([^>"]*)[>"].*$')
 
2264
# Matches the first component of a filename delimited by -s and _s. That is:
 
2265
#  _RE_FIRST_COMPONENT.match('foo').group(0) == 'foo'
 
2266
#  _RE_FIRST_COMPONENT.match('foo.cc').group(0) == 'foo'
 
2267
#  _RE_FIRST_COMPONENT.match('foo-bar_baz.cc').group(0) == 'foo'
 
2268
#  _RE_FIRST_COMPONENT.match('foo_bar-baz.cc').group(0) == 'foo'
 
2269
_RE_FIRST_COMPONENT = re.compile(r'^[^-_.]+')
 
2270
 
 
2271
 
 
2272
def _DropCommonSuffixes(filename):
 
2273
  """Drops common suffixes like _test.cc or -inl.h from filename.
 
2274
 
 
2275
  For example:
 
2276
    >>> _DropCommonSuffixes('foo/foo-inl.h')
 
2277
    'foo/foo'
 
2278
    >>> _DropCommonSuffixes('foo/bar/foo.cc')
 
2279
    'foo/bar/foo'
 
2280
    >>> _DropCommonSuffixes('foo/foo_internal.h')
 
2281
    'foo/foo'
 
2282
    >>> _DropCommonSuffixes('foo/foo_unusualinternal.h')
 
2283
    'foo/foo_unusualinternal'
 
2284
 
 
2285
  Args:
 
2286
    filename: The input filename.
 
2287
 
 
2288
  Returns:
 
2289
    The filename with the common suffix removed.
 
2290
  """
 
2291
  for suffix in ('test.cc', 'regtest.cc', 'unittest.cc',
 
2292
                 'inl.h', 'impl.h', 'internal.h'):
 
2293
    if (filename.endswith(suffix) and len(filename) > len(suffix) and
 
2294
        filename[-len(suffix) - 1] in ('-', '_')):
 
2295
      return filename[:-len(suffix) - 1]
 
2296
  return os.path.splitext(filename)[0]
 
2297
 
 
2298
 
 
2299
def _IsTestFilename(filename):
 
2300
  """Determines if the given filename has a suffix that identifies it as a test.
 
2301
 
 
2302
  Args:
 
2303
    filename: The input filename.
 
2304
 
 
2305
  Returns:
 
2306
    True if 'filename' looks like a test, False otherwise.
 
2307
  """
 
2308
  if (filename.endswith('_test.cc') or
 
2309
      filename.endswith('_unittest.cc') or
 
2310
      filename.endswith('_regtest.cc')):
 
2311
    return True
 
2312
  else:
 
2313
    return False
 
2314
 
 
2315
 
 
2316
def _ClassifyInclude(fileinfo, include, is_system):
 
2317
  """Figures out what kind of header 'include' is.
 
2318
 
 
2319
  Args:
 
2320
    fileinfo: The current file cpplint is running over. A FileInfo instance.
 
2321
    include: The path to a #included file.
 
2322
    is_system: True if the #include used <> rather than "".
 
2323
 
 
2324
  Returns:
 
2325
    One of the _XXX_HEADER constants.
 
2326
 
 
2327
  For example:
 
2328
    >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'stdio.h', True)
 
2329
    _C_SYS_HEADER
 
2330
    >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'string', True)
 
2331
    _CPP_SYS_HEADER
 
2332
    >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/foo.h', False)
 
2333
    _LIKELY_MY_HEADER
 
2334
    >>> _ClassifyInclude(FileInfo('foo/foo_unknown_extension.cc'),
 
2335
    ...                  'bar/foo_other_ext.h', False)
 
2336
    _POSSIBLE_MY_HEADER
 
2337
    >>> _ClassifyInclude(FileInfo('foo/foo.cc'), 'foo/bar.h', False)
 
2338
    _OTHER_HEADER
 
2339
  """
 
2340
  # This is a list of all standard c++ header files, except
 
2341
  # those already checked for above.
 
2342
  is_stl_h = include in _STL_HEADERS
 
2343
  is_cpp_h = is_stl_h or include in _CPP_HEADERS
 
2344
 
 
2345
  if is_system:
 
2346
    if is_cpp_h:
 
2347
      return _CPP_SYS_HEADER
 
2348
    else:
 
2349
      return _C_SYS_HEADER
 
2350
 
 
2351
  # If the target file and the include we're checking share a
 
2352
  # basename when we drop common extensions, and the include
 
2353
  # lives in . , then it's likely to be owned by the target file.
 
2354
  target_dir, target_base = (
 
2355
      os.path.split(_DropCommonSuffixes(fileinfo.RepositoryName())))
 
2356
  include_dir, include_base = os.path.split(_DropCommonSuffixes(include))
 
2357
  if target_base == include_base and (
 
2358
      include_dir == target_dir or
 
2359
      include_dir == os.path.normpath(target_dir + '/../public')):
 
2360
    return _LIKELY_MY_HEADER
 
2361
 
 
2362
  # If the target and include share some initial basename
 
2363
  # component, it's possible the target is implementing the
 
2364
  # include, so it's allowed to be first, but we'll never
 
2365
  # complain if it's not there.
 
2366
  target_first_component = _RE_FIRST_COMPONENT.match(target_base)
 
2367
  include_first_component = _RE_FIRST_COMPONENT.match(include_base)
 
2368
  if (target_first_component and include_first_component and
 
2369
      target_first_component.group(0) ==
 
2370
      include_first_component.group(0)):
 
2371
    return _POSSIBLE_MY_HEADER
 
2372
 
 
2373
  return _OTHER_HEADER
 
2374
 
 
2375
 
 
2376
 
 
2377
def CheckIncludeLine(filename, clean_lines, linenum, include_state, error):
 
2378
  """Check rules that are applicable to #include lines.
 
2379
 
 
2380
  Strings on #include lines are NOT removed from elided line, to make
 
2381
  certain tasks easier. However, to prevent false positives, checks
 
2382
  applicable to #include lines in CheckLanguage must be put here.
 
2383
 
 
2384
  Args:
 
2385
    filename: The name of the current file.
 
2386
    clean_lines: A CleansedLines instance containing the file.
 
2387
    linenum: The number of the line to check.
 
2388
    include_state: An _IncludeState instance in which the headers are inserted.
 
2389
    error: The function to call with any errors found.
 
2390
  """
 
2391
  fileinfo = FileInfo(filename)
 
2392
 
 
2393
  line = clean_lines.lines[linenum]
 
2394
 
 
2395
  # "include" should use the new style "foo/bar.h" instead of just "bar.h"
 
2396
  if _RE_PATTERN_INCLUDE_NEW_STYLE.search(line):
 
2397
    error(filename, linenum, 'build/include', 4,
 
2398
          'Include the directory when naming .h files')
 
2399
 
 
2400
  # we shouldn't include a file more than once. actually, there are a
 
2401
  # handful of instances where doing so is okay, but in general it's
 
2402
  # not.
 
2403
  match = _RE_PATTERN_INCLUDE.search(line)
 
2404
  if match:
 
2405
    include = match.group(2)
 
2406
    is_system = (match.group(1) == '<')
 
2407
    if include in include_state:
 
2408
      error(filename, linenum, 'build/include', 4,
 
2409
            '"%s" already included at %s:%s' %
 
2410
            (include, filename, include_state[include]))
 
2411
    else:
 
2412
      include_state[include] = linenum
 
2413
 
 
2414
      # We want to ensure that headers appear in the right order:
 
2415
      # 1) for foo.cc, foo.h  (preferred location)
 
2416
      # 2) c system files
 
2417
      # 3) cpp system files
 
2418
      # 4) for foo.cc, foo.h  (deprecated location)
 
2419
      # 5) other google headers
 
2420
      #
 
2421
      # We classify each include statement as one of those 5 types
 
2422
      # using a number of techniques. The include_state object keeps
 
2423
      # track of the highest type seen, and complains if we see a
 
2424
      # lower type after that.
 
2425
      error_message = include_state.CheckNextIncludeOrder(
 
2426
          _ClassifyInclude(fileinfo, include, is_system))
 
2427
      if error_message:
 
2428
        error(filename, linenum, 'build/include_order', 4,
 
2429
              '%s. Should be: %s.h, c system, c++ system, other.' %
 
2430
              (error_message, fileinfo.BaseName()))
 
2431
      if not include_state.IsInAlphabeticalOrder(include):
 
2432
        error(filename, linenum, 'build/include_alpha', 4,
 
2433
              'Include "%s" not in alphabetical order' % include)
 
2434
 
 
2435
  # Look for any of the stream classes that are part of standard C++.
 
2436
  match = _RE_PATTERN_INCLUDE.match(line)
 
2437
  if match:
 
2438
    include = match.group(2)
 
2439
    if Match(r'(f|ind|io|i|o|parse|pf|stdio|str|)?stream$', include):
 
2440
      # Many unit tests use cout, so we exempt them.
 
2441
      if not _IsTestFilename(filename):
 
2442
        error(filename, linenum, 'readability/streams', 3,
 
2443
              'Streams are highly discouraged.')
 
2444
 
 
2445
 
 
2446
def _GetTextInside(text, start_pattern):
 
2447
  """Retrieves all the text between matching open and close parentheses.
 
2448
 
 
2449
  Given a string of lines and a regular expression string, retrieve all the text
 
2450
  following the expression and between opening punctuation symbols like
 
2451
  (, [, or {, and the matching close-punctuation symbol. This properly nested
 
2452
  occurrences of the punctuations, so for the text like
 
2453
    printf(a(), b(c()));
 
2454
  a call to _GetTextInside(text, r'printf\(') will return 'a(), b(c())'.
 
2455
  start_pattern must match string having an open punctuation symbol at the end.
 
2456
 
 
2457
  Args:
 
2458
    text: The lines to extract text. Its comments and strings must be elided.
 
2459
           It can be single line and can span multiple lines.
 
2460
    start_pattern: The regexp string indicating where to start extracting
 
2461
                   the text.
 
2462
  Returns:
 
2463
    The extracted text.
 
2464
    None if either the opening string or ending punctuation could not be found.
 
2465
  """
 
2466
  # TODO(sugawarayu): Audit cpplint.py to see what places could be profitably
 
2467
  # rewritten to use _GetTextInside (and use inferior regexp matching today).
 
2468
 
 
2469
  # Give opening punctuations to get the matching close-punctuations.
 
2470
  matching_punctuation = {'(': ')', '{': '}', '[': ']'}
 
2471
  closing_punctuation = set(matching_punctuation.itervalues())
 
2472
 
 
2473
  # Find the position to start extracting text.
 
2474
  match = re.search(start_pattern, text, re.M)
 
2475
  if not match:  # start_pattern not found in text.
 
2476
    return None
 
2477
  start_position = match.end(0)
 
2478
 
 
2479
  assert start_position > 0, (
 
2480
      'start_pattern must ends with an opening punctuation.')
 
2481
  assert text[start_position - 1] in matching_punctuation, (
 
2482
      'start_pattern must ends with an opening punctuation.')
 
2483
  # Stack of closing punctuations we expect to have in text after position.
 
2484
  punctuation_stack = [matching_punctuation[text[start_position - 1]]]
 
2485
  position = start_position
 
2486
  while punctuation_stack and position < len(text):
 
2487
    if text[position] == punctuation_stack[-1]:
 
2488
      punctuation_stack.pop()
 
2489
    elif text[position] in closing_punctuation:
 
2490
      # A closing punctuation without matching opening punctuations.
 
2491
      return None
 
2492
    elif text[position] in matching_punctuation:
 
2493
      punctuation_stack.append(matching_punctuation[text[position]])
 
2494
    position += 1
 
2495
  if punctuation_stack:
 
2496
    # Opening punctuations left without matching close-punctuations.
 
2497
    return None
 
2498
  # punctuations match.
 
2499
  return text[start_position:position - 1]
 
2500
 
 
2501
 
 
2502
def CheckLanguage(filename, clean_lines, linenum, file_extension, include_state,
 
2503
                  error):
 
2504
  """Checks rules from the 'C++ language rules' section of cppguide.html.
 
2505
 
 
2506
  Some of these rules are hard to test (function overloading, using
 
2507
  uint32 inappropriately), but we do the best we can.
 
2508
 
 
2509
  Args:
 
2510
    filename: The name of the current file.
 
2511
    clean_lines: A CleansedLines instance containing the file.
 
2512
    linenum: The number of the line to check.
 
2513
    file_extension: The extension (without the dot) of the filename.
 
2514
    include_state: An _IncludeState instance in which the headers are inserted.
 
2515
    error: The function to call with any errors found.
 
2516
  """
 
2517
  # If the line is empty or consists of entirely a comment, no need to
 
2518
  # check it.
 
2519
  line = clean_lines.elided[linenum]
 
2520
  if not line:
 
2521
    return
 
2522
 
 
2523
  match = _RE_PATTERN_INCLUDE.search(line)
 
2524
  if match:
 
2525
    CheckIncludeLine(filename, clean_lines, linenum, include_state, error)
 
2526
    return
 
2527
 
 
2528
  # Create an extended_line, which is the concatenation of the current and
 
2529
  # next lines, for more effective checking of code that may span more than one
 
2530
  # line.
 
2531
  if linenum + 1 < clean_lines.NumLines():
 
2532
    extended_line = line + clean_lines.elided[linenum + 1]
 
2533
  else:
 
2534
    extended_line = line
 
2535
 
 
2536
  # Make Windows paths like Unix.
 
2537
  fullname = os.path.abspath(filename).replace('\\', '/')
 
2538
 
 
2539
  # TODO(unknown): figure out if they're using default arguments in fn proto.
 
2540
 
 
2541
  # Check for non-const references in functions.  This is tricky because &
 
2542
  # is also used to take the address of something.  We allow <> for templates,
 
2543
  # (ignoring whatever is between the braces) and : for classes.
 
2544
  # These are complicated re's.  They try to capture the following:
 
2545
  # paren (for fn-prototype start), typename, &, varname.  For the const
 
2546
  # version, we're willing for const to be before typename or after
 
2547
  # Don't check the implementation on same line.
 
2548
  fnline = line.split('{', 1)[0]
 
2549
  if (len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) >
 
2550
      len(re.findall(r'\([^()]*\bconst\s+(?:typename\s+)?(?:struct\s+)?'
 
2551
                     r'(?:[\w:]|<[^()]*>)+(\s?&|&\s?)\w+', fnline)) +
 
2552
      len(re.findall(r'\([^()]*\b(?:[\w:]|<[^()]*>)+\s+const(\s?&|&\s?)[\w]+',
 
2553
                     fnline))):
 
2554
 
 
2555
    # We allow non-const references in a few standard places, like functions
 
2556
    # called "swap()" or iostream operators like "<<" or ">>".
 
2557
    if not Search(
 
2558
        r'(swap|Swap|operator[<>][<>])\s*\(\s*(?:[\w:]|<.*>)+\s*&',
 
2559
        fnline):
 
2560
      error(filename, linenum, 'runtime/references', 2,
 
2561
            'Is this a non-const reference? '
 
2562
            'If so, make const or use a pointer.')
 
2563
 
 
2564
  # Check to see if they're using an conversion function cast.
 
2565
  # I just try to capture the most common basic types, though there are more.
 
2566
  # Parameterless conversion functions, such as bool(), are allowed as they are
 
2567
  # probably a member operator declaration or default constructor.
 
2568
  match = Search(
 
2569
      r'(\bnew\s+)?\b'  # Grab 'new' operator, if it's there
 
2570
      r'(int|float|double|bool|char|int32|uint32|int64|uint64)\([^)]', line)
 
2571
  if match:
 
2572
    # gMock methods are defined using some variant of MOCK_METHODx(name, type)
 
2573
    # where type may be float(), int(string), etc.  Without context they are
 
2574
    # virtually indistinguishable from int(x) casts. Likewise, gMock's
 
2575
    # MockCallback takes a template parameter of the form return_type(arg_type),
 
2576
    # which looks much like the cast we're trying to detect.
 
2577
    if (match.group(1) is None and  # If new operator, then this isn't a cast
 
2578
        not (Match(r'^\s*MOCK_(CONST_)?METHOD\d+(_T)?\(', line) or
 
2579
             Match(r'^\s*MockCallback<.*>', line))):
 
2580
      error(filename, linenum, 'readability/casting', 4,
 
2581
            'Using deprecated casting style.  '
 
2582
            'Use static_cast<%s>(...) instead' %
 
2583
            match.group(2))
 
2584
 
 
2585
  CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
 
2586
                  'static_cast',
 
2587
                  r'\((int|float|double|bool|char|u?int(16|32|64))\)', error)
 
2588
 
 
2589
  # This doesn't catch all cases. Consider (const char * const)"hello".
 
2590
  #
 
2591
  # (char *) "foo" should always be a const_cast (reinterpret_cast won't
 
2592
  # compile).
 
2593
  if CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
 
2594
                     'const_cast', r'\((char\s?\*+\s?)\)\s*"', error):
 
2595
    pass
 
2596
  else:
 
2597
    # Check pointer casts for other than string constants
 
2598
    CheckCStyleCast(filename, linenum, line, clean_lines.raw_lines[linenum],
 
2599
                    'reinterpret_cast', r'\((\w+\s?\*+\s?)\)', error)
 
2600
 
 
2601
  # In addition, we look for people taking the address of a cast.  This
 
2602
  # is dangerous -- casts can assign to temporaries, so the pointer doesn't
 
2603
  # point where you think.
 
2604
  if Search(
 
2605
      r'(&\([^)]+\)[\w(])|(&(static|dynamic|reinterpret)_cast\b)', line):
 
2606
    error(filename, linenum, 'runtime/casting', 4,
 
2607
          ('Are you taking an address of a cast?  '
 
2608
           'This is dangerous: could be a temp var.  '
 
2609
           'Take the address before doing the cast, rather than after'))
 
2610
 
 
2611
  # Check for people declaring static/global STL strings at the top level.
 
2612
  # This is dangerous because the C++ language does not guarantee that
 
2613
  # globals with constructors are initialized before the first access.
 
2614
  match = Match(
 
2615
      r'((?:|static +)(?:|const +))string +([a-zA-Z0-9_:]+)\b(.*)',
 
2616
      line)
 
2617
  # Make sure it's not a function.
 
2618
  # Function template specialization looks like: "string foo<Type>(...".
 
2619
  # Class template definitions look like: "string Foo<Type>::Method(...".
 
2620
  if match and not Match(r'\s*(<.*>)?(::[a-zA-Z0-9_]+)?\s*\(([^"]|$)',
 
2621
                         match.group(3)):
 
2622
    error(filename, linenum, 'runtime/string', 4,
 
2623
          'For a static/global string constant, use a C style string instead: '
 
2624
          '"%schar %s[]".' %
 
2625
          (match.group(1), match.group(2)))
 
2626
 
 
2627
  # Check that we're not using RTTI outside of testing code.
 
2628
  if Search(r'\bdynamic_cast<', line) and not _IsTestFilename(filename):
 
2629
    error(filename, linenum, 'runtime/rtti', 5,
 
2630
          'Do not use dynamic_cast<>.  If you need to cast within a class '
 
2631
          "hierarchy, use static_cast<> to upcast.  Google doesn't support "
 
2632
          'RTTI.')
 
2633
 
 
2634
  if Search(r'\b([A-Za-z0-9_]*_)\(\1\)', line):
 
2635
    error(filename, linenum, 'runtime/init', 4,
 
2636
          'You seem to be initializing a member variable with itself.')
 
2637
 
 
2638
  if file_extension == 'h':
 
2639
    # TODO(unknown): check that 1-arg constructors are explicit.
 
2640
    #                How to tell it's a constructor?
 
2641
    #                (handled in CheckForNonStandardConstructs for now)
 
2642
    # TODO(unknown): check that classes have DISALLOW_EVIL_CONSTRUCTORS
 
2643
    #                (level 1 error)
 
2644
    pass
 
2645
 
 
2646
  # Check if people are using the verboten C basic types.  The only exception
 
2647
  # we regularly allow is "unsigned short port" for port.
 
2648
  if Search(r'\bshort port\b', line):
 
2649
    if not Search(r'\bunsigned short port\b', line):
 
2650
      error(filename, linenum, 'runtime/int', 4,
 
2651
            'Use "unsigned short" for ports, not "short"')
 
2652
  else:
 
2653
    match = Search(r'\b(short|long(?! +double)|long long)\b', line)
 
2654
    if match:
 
2655
      error(filename, linenum, 'runtime/int', 4,
 
2656
            'Use int16/int64/etc, rather than the C type %s' % match.group(1))
 
2657
 
 
2658
  # When snprintf is used, the second argument shouldn't be a literal.
 
2659
  match = Search(r'snprintf\s*\(([^,]*),\s*([0-9]*)\s*,', line)
 
2660
  if match and match.group(2) != '0':
 
2661
    # If 2nd arg is zero, snprintf is used to calculate size.
 
2662
    error(filename, linenum, 'runtime/printf', 3,
 
2663
          'If you can, use sizeof(%s) instead of %s as the 2nd arg '
 
2664
          'to snprintf.' % (match.group(1), match.group(2)))
 
2665
 
 
2666
  # Check if some verboten C functions are being used.
 
2667
  if Search(r'\bsprintf\b', line):
 
2668
    error(filename, linenum, 'runtime/printf', 5,
 
2669
          'Never use sprintf.  Use snprintf instead.')
 
2670
  match = Search(r'\b(strcpy|strcat)\b', line)
 
2671
  if match:
 
2672
    error(filename, linenum, 'runtime/printf', 4,
 
2673
          'Almost always, snprintf is better than %s' % match.group(1))
 
2674
 
 
2675
  if Search(r'\bsscanf\b', line):
 
2676
    error(filename, linenum, 'runtime/printf', 1,
 
2677
          'sscanf can be ok, but is slow and can overflow buffers.')
 
2678
 
 
2679
  # Check if some verboten operator overloading is going on
 
2680
  # TODO(unknown): catch out-of-line unary operator&:
 
2681
  #   class X {};
 
2682
  #   int operator&(const X& x) { return 42; }  // unary operator&
 
2683
  # The trick is it's hard to tell apart from binary operator&:
 
2684
  #   class Y { int operator&(const Y& x) { return 23; } }; // binary operator&
 
2685
  if Search(r'\boperator\s*&\s*\(\s*\)', line):
 
2686
    error(filename, linenum, 'runtime/operator', 4,
 
2687
          'Unary operator& is dangerous.  Do not use it.')
 
2688
 
 
2689
  # Check for suspicious usage of "if" like
 
2690
  # } if (a == b) {
 
2691
  if Search(r'\}\s*if\s*\(', line):
 
2692
    error(filename, linenum, 'readability/braces', 4,
 
2693
          'Did you mean "else if"? If not, start a new line for "if".')
 
2694
 
 
2695
  # Check for potential format string bugs like printf(foo).
 
2696
  # We constrain the pattern not to pick things like DocidForPrintf(foo).
 
2697
  # Not perfect but it can catch printf(foo.c_str()) and printf(foo->c_str())
 
2698
  # TODO(sugawarayu): Catch the following case. Need to change the calling
 
2699
  # convention of the whole function to process multiple line to handle it.
 
2700
  #   printf(
 
2701
  #       boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
 
2702
  printf_args = _GetTextInside(line, r'(?i)\b(string)?printf\s*\(')
 
2703
  if printf_args:
 
2704
    match = Match(r'([\w.\->()]+)$', printf_args)
 
2705
    if match:
 
2706
      function_name = re.search(r'\b((?:string)?printf)\s*\(',
 
2707
                                line, re.I).group(1)
 
2708
      error(filename, linenum, 'runtime/printf', 4,
 
2709
            'Potential format string bug. Do %s("%%s", %s) instead.'
 
2710
            % (function_name, match.group(1)))
 
2711
 
 
2712
  # Check for potential memset bugs like memset(buf, sizeof(buf), 0).
 
2713
  match = Search(r'memset\s*\(([^,]*),\s*([^,]*),\s*0\s*\)', line)
 
2714
  if match and not Match(r"^''|-?[0-9]+|0x[0-9A-Fa-f]$", match.group(2)):
 
2715
    error(filename, linenum, 'runtime/memset', 4,
 
2716
          'Did you mean "memset(%s, 0, %s)"?'
 
2717
          % (match.group(1), match.group(2)))
 
2718
 
 
2719
  if Search(r'\busing namespace\b', line):
 
2720
    error(filename, linenum, 'build/namespaces', 5,
 
2721
          'Do not use namespace using-directives.  '
 
2722
          'Use using-declarations instead.')
 
2723
 
 
2724
  # Detect variable-length arrays.
 
2725
  match = Match(r'\s*(.+::)?(\w+) [a-z]\w*\[(.+)];', line)
 
2726
  if (match and match.group(2) != 'return' and match.group(2) != 'delete' and
 
2727
      match.group(3).find(']') == -1):
 
2728
    # Split the size using space and arithmetic operators as delimiters.
 
2729
    # If any of the resulting tokens are not compile time constants then
 
2730
    # report the error.
 
2731
    tokens = re.split(r'\s|\+|\-|\*|\/|<<|>>]', match.group(3))
 
2732
    is_const = True
 
2733
    skip_next = False
 
2734
    for tok in tokens:
 
2735
      if skip_next:
 
2736
        skip_next = False
 
2737
        continue
 
2738
 
 
2739
      if Search(r'sizeof\(.+\)', tok): continue
 
2740
      if Search(r'arraysize\(\w+\)', tok): continue
 
2741
 
 
2742
      tok = tok.lstrip('(')
 
2743
      tok = tok.rstrip(')')
 
2744
      if not tok: continue
 
2745
      if Match(r'\d+', tok): continue
 
2746
      if Match(r'0[xX][0-9a-fA-F]+', tok): continue
 
2747
      if Match(r'k[A-Z0-9]\w*', tok): continue
 
2748
      if Match(r'(.+::)?k[A-Z0-9]\w*', tok): continue
 
2749
      if Match(r'(.+::)?[A-Z][A-Z0-9_]*', tok): continue
 
2750
      # A catch all for tricky sizeof cases, including 'sizeof expression',
 
2751
      # 'sizeof(*type)', 'sizeof(const type)', 'sizeof(struct StructName)'
 
2752
      # requires skipping the next token because we split on ' ' and '*'.
 
2753
      if tok.startswith('sizeof'):
 
2754
        skip_next = True
 
2755
        continue
 
2756
      is_const = False
 
2757
      break
 
2758
    if not is_const:
 
2759
      error(filename, linenum, 'runtime/arrays', 1,
 
2760
            'Do not use variable-length arrays.  Use an appropriately named '
 
2761
            "('k' followed by CamelCase) compile-time constant for the size.")
 
2762
 
 
2763
  # If DISALLOW_EVIL_CONSTRUCTORS, DISALLOW_COPY_AND_ASSIGN, or
 
2764
  # DISALLOW_IMPLICIT_CONSTRUCTORS is present, then it should be the last thing
 
2765
  # in the class declaration.
 
2766
  match = Match(
 
2767
      (r'\s*'
 
2768
       r'(DISALLOW_(EVIL_CONSTRUCTORS|COPY_AND_ASSIGN|IMPLICIT_CONSTRUCTORS))'
 
2769
       r'\(.*\);$'),
 
2770
      line)
 
2771
  if match and linenum + 1 < clean_lines.NumLines():
 
2772
    next_line = clean_lines.elided[linenum + 1]
 
2773
    # We allow some, but not all, declarations of variables to be present
 
2774
    # in the statement that defines the class.  The [\w\*,\s]* fragment of
 
2775
    # the regular expression below allows users to declare instances of
 
2776
    # the class or pointers to instances, but not less common types such
 
2777
    # as function pointers or arrays.  It's a tradeoff between allowing
 
2778
    # reasonable code and avoiding trying to parse more C++ using regexps.
 
2779
    if not Search(r'^\s*}[\w\*,\s]*;', next_line):
 
2780
      error(filename, linenum, 'readability/constructors', 3,
 
2781
            match.group(1) + ' should be the last thing in the class')
 
2782
 
 
2783
  # Check for use of unnamed namespaces in header files.  Registration
 
2784
  # macros are typically OK, so we allow use of "namespace {" on lines
 
2785
  # that end with backslashes.
 
2786
  if (file_extension == 'h'
 
2787
      and Search(r'\bnamespace\s*{', line)
 
2788
      and line[-1] != '\\'):
 
2789
    error(filename, linenum, 'build/namespaces', 4,
 
2790
          'Do not use unnamed namespaces in header files.  See '
 
2791
          'http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces'
 
2792
          ' for more information.')
 
2793
 
 
2794
 
 
2795
def CheckCStyleCast(filename, linenum, line, raw_line, cast_type, pattern,
 
2796
                    error):
 
2797
  """Checks for a C-style cast by looking for the pattern.
 
2798
 
 
2799
  This also handles sizeof(type) warnings, due to similarity of content.
 
2800
 
 
2801
  Args:
 
2802
    filename: The name of the current file.
 
2803
    linenum: The number of the line to check.
 
2804
    line: The line of code to check.
 
2805
    raw_line: The raw line of code to check, with comments.
 
2806
    cast_type: The string for the C++ cast to recommend.  This is either
 
2807
      reinterpret_cast, static_cast, or const_cast, depending.
 
2808
    pattern: The regular expression used to find C-style casts.
 
2809
    error: The function to call with any errors found.
 
2810
 
 
2811
  Returns:
 
2812
    True if an error was emitted.
 
2813
    False otherwise.
 
2814
  """
 
2815
  match = Search(pattern, line)
 
2816
  if not match:
 
2817
    return False
 
2818
 
 
2819
  # e.g., sizeof(int)
 
2820
  sizeof_match = Match(r'.*sizeof\s*$', line[0:match.start(1) - 1])
 
2821
  if sizeof_match:
 
2822
    error(filename, linenum, 'runtime/sizeof', 1,
 
2823
          'Using sizeof(type).  Use sizeof(varname) instead if possible')
 
2824
    return True
 
2825
 
 
2826
  remainder = line[match.end(0):]
 
2827
 
 
2828
  # The close paren is for function pointers as arguments to a function.
 
2829
  # eg, void foo(void (*bar)(int));
 
2830
  # The semicolon check is a more basic function check; also possibly a
 
2831
  # function pointer typedef.
 
2832
  # eg, void foo(int); or void foo(int) const;
 
2833
  # The equals check is for function pointer assignment.
 
2834
  # eg, void *(*foo)(int) = ...
 
2835
  # The > is for MockCallback<...> ...
 
2836
  #
 
2837
  # Right now, this will only catch cases where there's a single argument, and
 
2838
  # it's unnamed.  It should probably be expanded to check for multiple
 
2839
  # arguments with some unnamed.
 
2840
  function_match = Match(r'\s*(\)|=|(const)?\s*(;|\{|throw\(\)|>))', remainder)
 
2841
  if function_match:
 
2842
    if (not function_match.group(3) or
 
2843
        function_match.group(3) == ';' or
 
2844
        ('MockCallback<' not in raw_line and
 
2845
         '/*' not in raw_line)):
 
2846
      error(filename, linenum, 'readability/function', 3,
 
2847
            'All parameters should be named in a function')
 
2848
    return True
 
2849
 
 
2850
  # At this point, all that should be left is actual casts.
 
2851
  error(filename, linenum, 'readability/casting', 4,
 
2852
        'Using C-style cast.  Use %s<%s>(...) instead' %
 
2853
        (cast_type, match.group(1)))
 
2854
 
 
2855
  return True
 
2856
 
 
2857
 
 
2858
_HEADERS_CONTAINING_TEMPLATES = (
 
2859
    ('<deque>', ('deque',)),
 
2860
    ('<functional>', ('unary_function', 'binary_function',
 
2861
                      'plus', 'minus', 'multiplies', 'divides', 'modulus',
 
2862
                      'negate',
 
2863
                      'equal_to', 'not_equal_to', 'greater', 'less',
 
2864
                      'greater_equal', 'less_equal',
 
2865
                      'logical_and', 'logical_or', 'logical_not',
 
2866
                      'unary_negate', 'not1', 'binary_negate', 'not2',
 
2867
                      'bind1st', 'bind2nd',
 
2868
                      'pointer_to_unary_function',
 
2869
                      'pointer_to_binary_function',
 
2870
                      'ptr_fun',
 
2871
                      'mem_fun_t', 'mem_fun', 'mem_fun1_t', 'mem_fun1_ref_t',
 
2872
                      'mem_fun_ref_t',
 
2873
                      'const_mem_fun_t', 'const_mem_fun1_t',
 
2874
                      'const_mem_fun_ref_t', 'const_mem_fun1_ref_t',
 
2875
                      'mem_fun_ref',
 
2876
                     )),
 
2877
    ('<limits>', ('numeric_limits',)),
 
2878
    ('<list>', ('list',)),
 
2879
    ('<map>', ('map', 'multimap',)),
 
2880
    ('<memory>', ('allocator',)),
 
2881
    ('<queue>', ('queue', 'priority_queue',)),
 
2882
    ('<set>', ('set', 'multiset',)),
 
2883
    ('<stack>', ('stack',)),
 
2884
    ('<string>', ('char_traits', 'basic_string',)),
 
2885
    ('<utility>', ('pair',)),
 
2886
    ('<vector>', ('vector',)),
 
2887
 
 
2888
    # gcc extensions.
 
2889
    # Note: std::hash is their hash, ::hash is our hash
 
2890
    ('<hash_map>', ('hash_map', 'hash_multimap',)),
 
2891
    ('<hash_set>', ('hash_set', 'hash_multiset',)),
 
2892
    ('<slist>', ('slist',)),
 
2893
    )
 
2894
 
 
2895
_RE_PATTERN_STRING = re.compile(r'\bstring\b')
 
2896
 
 
2897
_re_pattern_algorithm_header = []
 
2898
for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap',
 
2899
                  'transform'):
 
2900
  # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or
 
2901
  # type::max().
 
2902
  _re_pattern_algorithm_header.append(
 
2903
      (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'),
 
2904
       _template,
 
2905
       '<algorithm>'))
 
2906
 
 
2907
_re_pattern_templates = []
 
2908
for _header, _templates in _HEADERS_CONTAINING_TEMPLATES:
 
2909
  for _template in _templates:
 
2910
    _re_pattern_templates.append(
 
2911
        (re.compile(r'(\<|\b)' + _template + r'\s*\<'),
 
2912
         _template + '<>',
 
2913
         _header))
 
2914
 
 
2915
 
 
2916
def FilesBelongToSameModule(filename_cc, filename_h):
 
2917
  """Check if these two filenames belong to the same module.
 
2918
 
 
2919
  The concept of a 'module' here is a as follows:
 
2920
  foo.h, foo-inl.h, foo.cc, foo_test.cc and foo_unittest.cc belong to the
 
2921
  same 'module' if they are in the same directory.
 
2922
  some/path/public/xyzzy and some/path/internal/xyzzy are also considered
 
2923
  to belong to the same module here.
 
2924
 
 
2925
  If the filename_cc contains a longer path than the filename_h, for example,
 
2926
  '/absolute/path/to/base/sysinfo.cc', and this file would include
 
2927
  'base/sysinfo.h', this function also produces the prefix needed to open the
 
2928
  header. This is used by the caller of this function to more robustly open the
 
2929
  header file. We don't have access to the real include paths in this context,
 
2930
  so we need this guesswork here.
 
2931
 
 
2932
  Known bugs: tools/base/bar.cc and base/bar.h belong to the same module
 
2933
  according to this implementation. Because of this, this function gives
 
2934
  some false positives. This should be sufficiently rare in practice.
 
2935
 
 
2936
  Args:
 
2937
    filename_cc: is the path for the .cc file
 
2938
    filename_h: is the path for the header path
 
2939
 
 
2940
  Returns:
 
2941
    Tuple with a bool and a string:
 
2942
    bool: True if filename_cc and filename_h belong to the same module.
 
2943
    string: the additional prefix needed to open the header file.
 
2944
  """
 
2945
 
 
2946
  if not filename_cc.endswith('.C'):
 
2947
    return (False, '')
 
2948
  filename_cc = filename_cc[:-len('.C')]
 
2949
  if filename_cc.endswith('_unittest'):
 
2950
    filename_cc = filename_cc[:-len('_unittest')]
 
2951
  elif filename_cc.endswith('_test'):
 
2952
    filename_cc = filename_cc[:-len('_test')]
 
2953
  filename_cc = filename_cc.replace('/public/', '/')
 
2954
  filename_cc = filename_cc.replace('/internal/', '/')
 
2955
 
 
2956
  if not filename_h.endswith('.h'):
 
2957
    return (False, '')
 
2958
  filename_h = filename_h[:-len('.h')]
 
2959
  if filename_h.endswith('-inl'):
 
2960
    filename_h = filename_h[:-len('-inl')]
 
2961
  filename_h = filename_h.replace('/public/', '/')
 
2962
  filename_h = filename_h.replace('/internal/', '/')
 
2963
 
 
2964
  files_belong_to_same_module = filename_cc.endswith(filename_h)
 
2965
  common_path = ''
 
2966
  if files_belong_to_same_module:
 
2967
    common_path = filename_cc[:-len(filename_h)]
 
2968
  return files_belong_to_same_module, common_path
 
2969
 
 
2970
 
 
2971
def UpdateIncludeState(filename, include_state, io=codecs):
 
2972
  """Fill up the include_state with new includes found from the file.
 
2973
 
 
2974
  Args:
 
2975
    filename: the name of the header to read.
 
2976
    include_state: an _IncludeState instance in which the headers are inserted.
 
2977
    io: The io factory to use to read the file. Provided for testability.
 
2978
 
 
2979
  Returns:
 
2980
    True if a header was succesfully added. False otherwise.
 
2981
  """
 
2982
  headerfile = None
 
2983
  try:
 
2984
    headerfile = io.open(filename, 'r', 'utf8', 'replace')
 
2985
  except IOError:
 
2986
    return False
 
2987
  linenum = 0
 
2988
  for line in headerfile:
 
2989
    linenum += 1
 
2990
    clean_line = CleanseComments(line)
 
2991
    match = _RE_PATTERN_INCLUDE.search(clean_line)
 
2992
    if match:
 
2993
      include = match.group(2)
 
2994
      # The value formatting is cute, but not really used right now.
 
2995
      # What matters here is that the key is in include_state.
 
2996
      include_state.setdefault(include, '%s:%d' % (filename, linenum))
 
2997
  return True
 
2998
 
 
2999
 
 
3000
def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
 
3001
                              io=codecs):
 
3002
  """Reports for missing stl includes.
 
3003
 
 
3004
  This function will output warnings to make sure you are including the headers
 
3005
  necessary for the stl containers and functions that you use. We only give one
 
3006
  reason to include a header. For example, if you use both equal_to<> and
 
3007
  less<> in a .h file, only one (the latter in the file) of these will be
 
3008
  reported as a reason to include the <functional>.
 
3009
 
 
3010
  Args:
 
3011
    filename: The name of the current file.
 
3012
    clean_lines: A CleansedLines instance containing the file.
 
3013
    include_state: An _IncludeState instance.
 
3014
    error: The function to call with any errors found.
 
3015
    io: The IO factory to use to read the header file. Provided for unittest
 
3016
        injection.
 
3017
  """
 
3018
  required = {}  # A map of header name to linenumber and the template entity.
 
3019
                 # Example of required: { '<functional>': (1219, 'less<>') }
 
3020
 
 
3021
  for linenum in xrange(clean_lines.NumLines()):
 
3022
    line = clean_lines.elided[linenum]
 
3023
    if not line or line[0] == '#':
 
3024
      continue
 
3025
 
 
3026
    # String is special -- it is a non-templatized type in STL.
 
3027
    matched = _RE_PATTERN_STRING.search(line)
 
3028
    if matched:
 
3029
      # Don't warn about strings in non-STL namespaces:
 
3030
      # (We check only the first match per line; good enough.)
 
3031
      prefix = line[:matched.start()]
 
3032
      if prefix.endswith('std::') or not prefix.endswith('::'):
 
3033
        required['<string>'] = (linenum, 'string')
 
3034
 
 
3035
    for pattern, template, header in _re_pattern_algorithm_header:
 
3036
      if pattern.search(line):
 
3037
        required[header] = (linenum, template)
 
3038
 
 
3039
    # The following function is just a speed up, no semantics are changed.
 
3040
    if not '<' in line:  # Reduces the cpu time usage by skipping lines.
 
3041
      continue
 
3042
 
 
3043
    for pattern, template, header in _re_pattern_templates:
 
3044
      if pattern.search(line):
 
3045
        required[header] = (linenum, template)
 
3046
 
 
3047
  # The policy is that if you #include something in foo.h you don't need to
 
3048
  # include it again in foo.cc. Here, we will look at possible includes.
 
3049
  # Let's copy the include_state so it is only messed up within this function.
 
3050
  include_state = include_state.copy()
 
3051
 
 
3052
  # Did we find the header for this file (if any) and succesfully load it?
 
3053
  header_found = False
 
3054
 
 
3055
  # Use the absolute path so that matching works properly.
 
3056
  abs_filename = os.path.abspath(filename)
 
3057
 
 
3058
  # For Emacs's flymake.
 
3059
  # If cpplint is invoked from Emacs's flymake, a temporary file is generated
 
3060
  # by flymake and that file name might end with '_flymake.cc'. In that case,
 
3061
  # restore original file name here so that the corresponding header file can be
 
3062
  # found.
 
3063
  # e.g. If the file name is 'foo_flymake.cc', we should search for 'foo.h'
 
3064
  # instead of 'foo_flymake.h'
 
3065
  abs_filename = re.sub(r'_flymake\.cc$', '.cc', abs_filename)
 
3066
 
 
3067
  # include_state is modified during iteration, so we iterate over a copy of
 
3068
  # the keys.
 
3069
  header_keys = include_state.keys()
 
3070
  for header in header_keys:
 
3071
    (same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
 
3072
    fullpath = common_path + header
 
3073
    if same_module and UpdateIncludeState(fullpath, include_state, io):
 
3074
      header_found = True
 
3075
 
 
3076
  # If we can't find the header file for a .cc, assume it's because we don't
 
3077
  # know where to look. In that case we'll give up as we're not sure they
 
3078
  # didn't include it in the .h file.
 
3079
  # TODO(unknown): Do a better job of finding .h files so we are confident that
 
3080
  # not having the .h file means there isn't one.
 
3081
  if filename.endswith('.C') and not header_found:
 
3082
    return
 
3083
 
 
3084
  # All the lines have been processed, report the errors found.
 
3085
  for required_header_unstripped in required:
 
3086
    template = required[required_header_unstripped][1]
 
3087
    if required_header_unstripped.strip('<>"') not in include_state:
 
3088
      error(filename, required[required_header_unstripped][0],
 
3089
            'build/include_what_you_use', 4,
 
3090
            'Add #include ' + required_header_unstripped + ' for ' + template)
 
3091
 
 
3092
 
 
3093
_RE_PATTERN_EXPLICIT_MAKEPAIR = re.compile(r'\bmake_pair\s*<')
 
3094
 
 
3095
 
 
3096
def CheckMakePairUsesDeduction(filename, clean_lines, linenum, error):
 
3097
  """Check that make_pair's template arguments are deduced.
 
3098
 
 
3099
  G++ 4.6 in C++0x mode fails badly if make_pair's template arguments are
 
3100
  specified explicitly, and such use isn't intended in any case.
 
3101
 
 
3102
  Args:
 
3103
    filename: The name of the current file.
 
3104
    clean_lines: A CleansedLines instance containing the file.
 
3105
    linenum: The number of the line to check.
 
3106
    error: The function to call with any errors found.
 
3107
  """
 
3108
  raw = clean_lines.raw_lines
 
3109
  line = raw[linenum]
 
3110
  match = _RE_PATTERN_EXPLICIT_MAKEPAIR.search(line)
 
3111
  if match:
 
3112
    error(filename, linenum, 'build/explicit_make_pair',
 
3113
          4,  # 4 = high confidence
 
3114
          'Omit template arguments from make_pair OR use pair directly OR'
 
3115
          ' if appropriate, construct a pair directly')
 
3116
 
 
3117
 
 
3118
def ProcessLine(filename, file_extension,
 
3119
                clean_lines, line, include_state, function_state,
 
3120
                class_state, error, extra_check_functions=[]):
 
3121
  """Processes a single line in the file.
 
3122
 
 
3123
  Args:
 
3124
    filename: Filename of the file that is being processed.
 
3125
    file_extension: The extension (dot not included) of the file.
 
3126
    clean_lines: An array of strings, each representing a line of the file,
 
3127
                 with comments stripped.
 
3128
    line: Number of line being processed.
 
3129
    include_state: An _IncludeState instance in which the headers are inserted.
 
3130
    function_state: A _FunctionState instance which counts function lines, etc.
 
3131
    class_state: A _ClassState instance which maintains information about
 
3132
                 the current stack of nested class declarations being parsed.
 
3133
    error: A callable to which errors are reported, which takes 4 arguments:
 
3134
           filename, line number, error level, and message
 
3135
    extra_check_functions: An array of additional check functions that will be
 
3136
                           run on each source line. Each function takes 4
 
3137
                           arguments: filename, clean_lines, line, error
 
3138
  """
 
3139
  raw_lines = clean_lines.raw_lines
 
3140
  ParseNolintSuppressions(filename, raw_lines[line], line, error)
 
3141
  CheckForFunctionLengths(filename, clean_lines, line, function_state, error)
 
3142
  CheckForMultilineCommentsAndStrings(filename, clean_lines, line, error)
 
3143
  CheckStyle(filename, clean_lines, line, file_extension, class_state, error)
 
3144
  CheckLanguage(filename, clean_lines, line, file_extension, include_state,
 
3145
                error)
 
3146
  CheckForNonStandardConstructs(filename, clean_lines, line,
 
3147
                                class_state, error)
 
3148
  CheckPosixThreading(filename, clean_lines, line, error)
 
3149
  CheckInvalidIncrement(filename, clean_lines, line, error)
 
3150
  CheckMakePairUsesDeduction(filename, clean_lines, line, error)
 
3151
  for check_fn in extra_check_functions:
 
3152
    check_fn(filename, clean_lines, line, error)
 
3153
 
 
3154
def ProcessFileData(filename, file_extension, lines, error,
 
3155
                    extra_check_functions=[]):
 
3156
  """Performs lint checks and reports any errors to the given error function.
 
3157
 
 
3158
  Args:
 
3159
    filename: Filename of the file that is being processed.
 
3160
    file_extension: The extension (dot not included) of the file.
 
3161
    lines: An array of strings, each representing a line of the file, with the
 
3162
           last element being empty if the file is terminated with a newline.
 
3163
    error: A callable to which errors are reported, which takes 4 arguments:
 
3164
           filename, line number, error level, and message
 
3165
    extra_check_functions: An array of additional check functions that will be
 
3166
                           run on each source line. Each function takes 4
 
3167
                           arguments: filename, clean_lines, line, error
 
3168
  """
 
3169
  lines = (['// marker so line numbers and indices both start at 1'] + lines +
 
3170
           ['// marker so line numbers end in a known way'])
 
3171
 
 
3172
  include_state = _IncludeState()
 
3173
  function_state = _FunctionState()
 
3174
  class_state = _ClassState()
 
3175
 
 
3176
  ResetNolintSuppressions()
 
3177
 
 
3178
  CheckForCopyright(filename, lines, error)
 
3179
 
 
3180
  if file_extension == 'h':
 
3181
    CheckForHeaderGuard(filename, lines, error)
 
3182
 
 
3183
  RemoveMultiLineComments(filename, lines, error)
 
3184
  clean_lines = CleansedLines(lines)
 
3185
  for line in xrange(clean_lines.NumLines()):
 
3186
    ProcessLine(filename, file_extension, clean_lines, line,
 
3187
                include_state, function_state, class_state, error,
 
3188
                extra_check_functions)
 
3189
  class_state.CheckFinished(filename, error)
 
3190
 
 
3191
  CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error)
 
3192
 
 
3193
  # We check here rather than inside ProcessLine so that we see raw
 
3194
  # lines rather than "cleaned" lines.
 
3195
  CheckForUnicodeReplacementCharacters(filename, lines, error)
 
3196
 
 
3197
  CheckForNewlineAtEOF(filename, lines, error)
 
3198
 
 
3199
def ProcessFile(filename, vlevel, extra_check_functions=[]):
 
3200
  """Does google-lint on a single file.
 
3201
 
 
3202
  Args:
 
3203
    filename: The name of the file to parse.
 
3204
 
 
3205
    vlevel: The level of errors to report.  Every error of confidence
 
3206
    >= verbose_level will be reported.  0 is a good default.
 
3207
 
 
3208
    extra_check_functions: An array of additional check functions that will be
 
3209
                           run on each source line. Each function takes 4
 
3210
                           arguments: filename, clean_lines, line, error
 
3211
  """
 
3212
 
 
3213
  _SetVerboseLevel(vlevel)
 
3214
 
 
3215
  try:
 
3216
    # Support the UNIX convention of using "-" for stdin.  Note that
 
3217
    # we are not opening the file with universal newline support
 
3218
    # (which codecs doesn't support anyway), so the resulting lines do
 
3219
    # contain trailing '\r' characters if we are reading a file that
 
3220
    # has CRLF endings.
 
3221
    # If after the split a trailing '\r' is present, it is removed
 
3222
    # below. If it is not expected to be present (i.e. os.linesep !=
 
3223
    # '\r\n' as in Windows), a warning is issued below if this file
 
3224
    # is processed.
 
3225
 
 
3226
    if filename == '-':
 
3227
      lines = codecs.StreamReaderWriter(sys.stdin,
 
3228
                                        codecs.getreader('utf8'),
 
3229
                                        codecs.getwriter('utf8'),
 
3230
                                        'replace').read().split('\n')
 
3231
    else:
 
3232
      lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
 
3233
 
 
3234
    carriage_return_found = False
 
3235
    # Remove trailing '\r'.
 
3236
    for linenum in range(len(lines)):
 
3237
      if lines[linenum].endswith('\r'):
 
3238
        lines[linenum] = lines[linenum].rstrip('\r')
 
3239
        carriage_return_found = True
 
3240
 
 
3241
  except IOError:
 
3242
    sys.stderr.write(
 
3243
        "Skipping input '%s': Can't open for reading\n" % filename)
 
3244
    return
 
3245
 
 
3246
  # Note, if no dot is found, this will give the entire filename as the ext.
 
3247
  file_extension = filename[filename.rfind('.') + 1:]
 
3248
 
 
3249
  # When reading from stdin, the extension is unknown, so no cpplint tests
 
3250
  # should rely on the extension.
 
3251
  if (filename != '-' and file_extension != 'C' and file_extension != 'h'
 
3252
      and file_extension != 'cpp'):
 
3253
    sys.stderr.write('Ignoring %s; not a .C or .h file\n' % filename)
 
3254
  else:
 
3255
    ProcessFileData(filename, file_extension, lines, Error,
 
3256
                    extra_check_functions)
 
3257
    if carriage_return_found and os.linesep != '\r\n':
 
3258
      # Use 0 for linenum since outputting only one error for potentially
 
3259
      # several lines.
 
3260
      Error(filename, 0, 'whitespace/newline', 1,
 
3261
            'One or more unexpected \\r (^M) found;'
 
3262
            'better to use only a \\n')
 
3263
 
 
3264
  sys.stderr.write('Done processing %s\n' % filename)
 
3265
 
 
3266
 
 
3267
def PrintUsage(message):
 
3268
  """Prints a brief usage string and exits, optionally with an error message.
 
3269
 
 
3270
  Args:
 
3271
    message: The optional error message.
 
3272
  """
 
3273
  sys.stderr.write(_USAGE)
 
3274
  if message:
 
3275
    sys.exit('\nFATAL ERROR: ' + message)
 
3276
  else:
 
3277
    sys.exit(1)
 
3278
 
 
3279
 
 
3280
def PrintCategories():
 
3281
  """Prints a list of all the error-categories used by error messages.
 
3282
 
 
3283
  These are the categories used to filter messages via --filter.
 
3284
  """
 
3285
  sys.stderr.write(''.join('  %s\n' % cat for cat in _ERROR_CATEGORIES))
 
3286
  sys.exit(0)
 
3287
 
 
3288
 
 
3289
def ParseArguments(args):
 
3290
  """Parses the command line arguments.
 
3291
 
 
3292
  This may set the output format and verbosity level as side-effects.
 
3293
 
 
3294
  Args:
 
3295
    args: The command line arguments:
 
3296
 
 
3297
  Returns:
 
3298
    The list of filenames to lint.
 
3299
  """
 
3300
  try:
 
3301
    (opts, filenames) = getopt.getopt(args, '', ['help', 'output=', 'verbose=',
 
3302
                                                 'counting=',
 
3303
                                                 'filter='])
 
3304
  except getopt.GetoptError:
 
3305
    PrintUsage('Invalid arguments.')
 
3306
 
 
3307
  verbosity = _VerboseLevel()
 
3308
  output_format = _OutputFormat()
 
3309
  filters = ''
 
3310
  counting_style = ''
 
3311
 
 
3312
  for (opt, val) in opts:
 
3313
    if opt == '--help':
 
3314
      PrintUsage(None)
 
3315
    elif opt == '--output':
 
3316
      if not val in ('emacs', 'vs7'):
 
3317
        PrintUsage('The only allowed output formats are emacs and vs7.')
 
3318
      output_format = val
 
3319
    elif opt == '--verbose':
 
3320
      verbosity = int(val)
 
3321
    elif opt == '--filter':
 
3322
      filters = val
 
3323
      if not filters:
 
3324
        PrintCategories()
 
3325
    elif opt == '--counting':
 
3326
      if val not in ('total', 'toplevel', 'detailed'):
 
3327
        PrintUsage('Valid counting options are total, toplevel, and detailed')
 
3328
      counting_style = val
 
3329
 
 
3330
  if not filenames:
 
3331
    PrintUsage('No files were specified.')
 
3332
 
 
3333
  _SetOutputFormat(output_format)
 
3334
  _SetVerboseLevel(verbosity)
 
3335
  _SetFilters(filters)
 
3336
  _SetCountingStyle(counting_style)
 
3337
 
 
3338
  return filenames
 
3339
 
 
3340
 
 
3341
def main():
 
3342
  filenames = ParseArguments(sys.argv[1:])
 
3343
 
 
3344
  # Change stderr to write with replacement characters so we don't die
 
3345
  # if we try to print something containing non-ASCII characters.
 
3346
  sys.stderr = codecs.StreamReaderWriter(sys.stderr,
 
3347
                                         codecs.getreader('utf8'),
 
3348
                                         codecs.getwriter('utf8'),
 
3349
                                         'replace')
 
3350
 
 
3351
  _cpplint_state.ResetErrorCounts()
 
3352
  for filename in filenames:
 
3353
    ProcessFile(filename, _cpplint_state.verbose_level)
 
3354
  _cpplint_state.PrintErrorCounts()
 
3355
 
 
3356
  sys.exit(_cpplint_state.error_count > 0)
 
3357
 
 
3358
 
 
3359
if __name__ == '__main__':
 
3360
  main()