OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2009 Google Inc. All rights reserved. | 3 # Copyright (c) 2009 Google Inc. All rights reserved. |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 1648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1659 named file. | 1659 named file. |
1660 | 1660 |
1661 """ | 1661 """ |
1662 | 1662 |
1663 # Restores original filename in case that cpplint is invoked from Emacs's | 1663 # Restores original filename in case that cpplint is invoked from Emacs's |
1664 # flymake. | 1664 # flymake. |
1665 filename = re.sub(r'_flymake\.h$', '.h', filename) | 1665 filename = re.sub(r'_flymake\.h$', '.h', filename) |
1666 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename) | 1666 filename = re.sub(r'/\.flymake/([^/]*)$', r'/\1', filename) |
1667 # Replace 'c++' with 'cpp'. | 1667 # Replace 'c++' with 'cpp'. |
1668 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp') | 1668 filename = filename.replace('C++', 'cpp').replace('c++', 'cpp') |
1669 | 1669 |
1670 fileinfo = FileInfo(filename) | 1670 fileinfo = FileInfo(filename) |
1671 file_path_from_root = fileinfo.RepositoryName() | 1671 file_path_from_root = fileinfo.RepositoryName() |
1672 if _root: | 1672 if _root: |
1673 file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root) | 1673 file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root) |
1674 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_' | 1674 return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_' |
1675 | 1675 |
1676 | 1676 |
1677 def CheckForHeaderGuard(filename, clean_lines, error): | 1677 def CheckForHeaderGuard(filename, clean_lines, error): |
1678 """Checks that the file contains a header guard. | 1678 """Checks that the file contains a header guard. |
1679 | 1679 |
(...skipping 3107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4787 return | 4787 return |
4788 | 4788 |
4789 # Reset include state across preprocessor directives. This is meant | 4789 # Reset include state across preprocessor directives. This is meant |
4790 # to silence warnings for conditional includes. | 4790 # to silence warnings for conditional includes. |
4791 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line) | 4791 match = Match(r'^\s*#\s*(if|ifdef|ifndef|elif|else|endif)\b', line) |
4792 if match: | 4792 if match: |
4793 include_state.ResetSection(match.group(1)) | 4793 include_state.ResetSection(match.group(1)) |
4794 | 4794 |
4795 # Make Windows paths like Unix. | 4795 # Make Windows paths like Unix. |
4796 fullname = os.path.abspath(filename).replace('\\', '/') | 4796 fullname = os.path.abspath(filename).replace('\\', '/') |
4797 | 4797 |
4798 # Perform other checks now that we are sure that this is not an include line | 4798 # Perform other checks now that we are sure that this is not an include line |
4799 CheckCasts(filename, clean_lines, linenum, error) | 4799 CheckCasts(filename, clean_lines, linenum, error) |
4800 CheckGlobalStatic(filename, clean_lines, linenum, error) | 4800 CheckGlobalStatic(filename, clean_lines, linenum, error) |
4801 CheckPrintf(filename, clean_lines, linenum, error) | 4801 CheckPrintf(filename, clean_lines, linenum, error) |
4802 | 4802 |
4803 if file_extension == 'h': | 4803 if file_extension == 'h': |
4804 # TODO(unknown): check that 1-arg constructors are explicit. | 4804 # TODO(unknown): check that 1-arg constructors are explicit. |
4805 # How to tell it's a constructor? | 4805 # How to tell it's a constructor? |
4806 # (handled in CheckForNonStandardConstructs for now) | 4806 # (handled in CheckForNonStandardConstructs for now) |
4807 # TODO(unknown): check that classes declare or disable copy/assign | 4807 # TODO(unknown): check that classes declare or disable copy/assign |
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5491 ('<utility>', ('pair',)), | 5491 ('<utility>', ('pair',)), |
5492 ('<vector>', ('vector',)), | 5492 ('<vector>', ('vector',)), |
5493 | 5493 |
5494 # gcc extensions. | 5494 # gcc extensions. |
5495 # Note: std::hash is their hash, ::hash is our hash | 5495 # Note: std::hash is their hash, ::hash is our hash |
5496 ('<hash_map>', ('hash_map', 'hash_multimap',)), | 5496 ('<hash_map>', ('hash_map', 'hash_multimap',)), |
5497 ('<hash_set>', ('hash_set', 'hash_multiset',)), | 5497 ('<hash_set>', ('hash_set', 'hash_multiset',)), |
5498 ('<slist>', ('slist',)), | 5498 ('<slist>', ('slist',)), |
5499 ) | 5499 ) |
5500 | 5500 |
5501 _HEADERS_MAYBE_TEMPLATES = ( | |
5502 ('<algorithm>', ('copy', 'max', 'min', 'min_element', 'sort', | |
5503 'transform', | |
5504 )), | |
5505 ('<utility>', ('swap',)), | |
5506 ) | |
5507 | |
5501 _RE_PATTERN_STRING = re.compile(r'\bstring\b') | 5508 _RE_PATTERN_STRING = re.compile(r'\bstring\b') |
5502 | 5509 |
5503 _re_pattern_algorithm_header = [] | 5510 _RE_PATTERN_HEADERS_MAYBE_TEMPLATES = [] |
Dirk Pranke
2016/02/05 20:40:38
this isn't a constant, so I wouldn't use all-caps
skym
2016/02/05 20:52:58
Done.
| |
5504 for _template in ('copy', 'max', 'min', 'min_element', 'sort', 'swap', | 5511 for _header, _templates in _HEADERS_MAYBE_TEMPLATES: |
5505 'transform'): | 5512 for _template in _templates: |
5506 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or | 5513 # Match max<type>(..., ...), max(..., ...), but not foo->max, foo.max or |
5507 # type::max(). | 5514 # type::max(). |
5508 _re_pattern_algorithm_header.append( | 5515 _RE_PATTERN_HEADERS_MAYBE_TEMPLATES.append( |
5509 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'), | 5516 (re.compile(r'[^>.]\b' + _template + r'(<.*?>)?\([^\)]'), |
5510 _template, | 5517 _template, |
5511 '<algorithm>')) | 5518 _header)) |
5512 | 5519 |
5520 # Other scripts may reach in and modify this pattern. | |
5513 _re_pattern_templates = [] | 5521 _re_pattern_templates = [] |
5514 for _header, _templates in _HEADERS_CONTAINING_TEMPLATES: | 5522 for _header, _templates in _HEADERS_CONTAINING_TEMPLATES: |
5515 for _template in _templates: | 5523 for _template in _templates: |
5516 _re_pattern_templates.append( | 5524 _re_pattern_templates.append( |
5517 (re.compile(r'(\<|\b)' + _template + r'\s*\<'), | 5525 (re.compile(r'(\<|\b)' + _template + r'\s*\<'), |
5518 _template + '<>', | 5526 _template + '<>', |
5519 _header)) | 5527 _header)) |
5520 | 5528 |
5521 | 5529 |
5522 def FilesBelongToSameModule(filename_cc, filename_h): | 5530 def FilesBelongToSameModule(filename_cc, filename_h): |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5629 | 5637 |
5630 # String is special -- it is a non-templatized type in STL. | 5638 # String is special -- it is a non-templatized type in STL. |
5631 matched = _RE_PATTERN_STRING.search(line) | 5639 matched = _RE_PATTERN_STRING.search(line) |
5632 if matched: | 5640 if matched: |
5633 # Don't warn about strings in non-STL namespaces: | 5641 # Don't warn about strings in non-STL namespaces: |
5634 # (We check only the first match per line; good enough.) | 5642 # (We check only the first match per line; good enough.) |
5635 prefix = line[:matched.start()] | 5643 prefix = line[:matched.start()] |
5636 if prefix.endswith('std::') or not prefix.endswith('::'): | 5644 if prefix.endswith('std::') or not prefix.endswith('::'): |
5637 required['<string>'] = (linenum, 'string') | 5645 required['<string>'] = (linenum, 'string') |
5638 | 5646 |
5639 for pattern, template, header in _re_pattern_algorithm_header: | 5647 for pattern, template, header in _RE_PATTERN_HEADERS_MAYBE_TEMPLATES: |
5640 if pattern.search(line): | 5648 if pattern.search(line): |
5641 required[header] = (linenum, template) | 5649 required[header] = (linenum, template) |
5642 | 5650 |
5643 # The following function is just a speed up, no semantics are changed. | 5651 # The following function is just a speed up, no semantics are changed. |
5644 if not '<' in line: # Reduces the cpu time usage by skipping lines. | 5652 if not '<' in line: # Reduces the cpu time usage by skipping lines. |
5645 continue | 5653 continue |
5646 | 5654 |
5647 for pattern, template, header in _re_pattern_templates: | 5655 for pattern, template, header in _re_pattern_templates: |
5648 if pattern.search(line): | 5656 if pattern.search(line): |
5649 required[header] = (linenum, template) | 5657 required[header] = (linenum, template) |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6027 CheckForHeaderGuard(filename, clean_lines, error) | 6035 CheckForHeaderGuard(filename, clean_lines, error) |
6028 | 6036 |
6029 for line in xrange(clean_lines.NumLines()): | 6037 for line in xrange(clean_lines.NumLines()): |
6030 ProcessLine(filename, file_extension, clean_lines, line, | 6038 ProcessLine(filename, file_extension, clean_lines, line, |
6031 include_state, function_state, nesting_state, error, | 6039 include_state, function_state, nesting_state, error, |
6032 extra_check_functions) | 6040 extra_check_functions) |
6033 FlagCxx11Features(filename, clean_lines, line, error) | 6041 FlagCxx11Features(filename, clean_lines, line, error) |
6034 nesting_state.CheckCompletedBlocks(filename, error) | 6042 nesting_state.CheckCompletedBlocks(filename, error) |
6035 | 6043 |
6036 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) | 6044 CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error) |
6037 | 6045 |
6038 # Check that the .cc file has included its header if it exists. | 6046 # Check that the .cc file has included its header if it exists. |
6039 if file_extension == 'cc': | 6047 if file_extension == 'cc': |
6040 CheckHeaderFileIncluded(filename, include_state, error) | 6048 CheckHeaderFileIncluded(filename, include_state, error) |
6041 | 6049 |
6042 # We check here rather than inside ProcessLine so that we see raw | 6050 # We check here rather than inside ProcessLine so that we see raw |
6043 # lines rather than "cleaned" lines. | 6051 # lines rather than "cleaned" lines. |
6044 CheckForBadCharacters(filename, lines, error) | 6052 CheckForBadCharacters(filename, lines, error) |
6045 | 6053 |
6046 CheckForNewlineAtEOF(filename, lines, error) | 6054 CheckForNewlineAtEOF(filename, lines, error) |
6047 | 6055 |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6314 _cpplint_state.ResetErrorCounts() | 6322 _cpplint_state.ResetErrorCounts() |
6315 for filename in filenames: | 6323 for filename in filenames: |
6316 ProcessFile(filename, _cpplint_state.verbose_level) | 6324 ProcessFile(filename, _cpplint_state.verbose_level) |
6317 _cpplint_state.PrintErrorCounts() | 6325 _cpplint_state.PrintErrorCounts() |
6318 | 6326 |
6319 sys.exit(_cpplint_state.error_count > 0) | 6327 sys.exit(_cpplint_state.error_count > 0) |
6320 | 6328 |
6321 | 6329 |
6322 if __name__ == '__main__': | 6330 if __name__ == '__main__': |
6323 main() | 6331 main() |
OLD | NEW |