source: rtos_arduino/trunk/arduino_lib/libraries/ArduinoJson/third-party/cpplint/cpplint.py@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

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