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