Chromium Code Reviews| Index: tools/checkdeps/cpp_checker.py |
| diff --git a/tools/checkdeps/cpp_checker.py b/tools/checkdeps/cpp_checker.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e75611097490ad1f9f947b89628ca712f43fd0ec |
| --- /dev/null |
| +++ b/tools/checkdeps/cpp_checker.py |
| @@ -0,0 +1,118 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Checker implementation that checks C++ and Objective-C files. |
| + |
| +If you import this module it will register itself with checker.Factory. |
| +""" |
| + |
| +import re |
| +import sys |
| + |
| +import checker |
| + |
| + |
| +# The maximum number of non-include lines we can see before giving up. |
| +MAX_UNINTERESTING_LINES = 50 |
| + |
| +# The maximum line length, this is to be efficient in the case of very long |
| +# lines (which can't be #includes). |
| +MAX_LINE_LENGTH = 128 |
| + |
| +# This regular expression will be used to extract filenames from include |
| +# statements. |
| +EXTRACT_INCLUDE_PATH = re.compile('[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') |
|
M-A Ruel
2012/07/17 14:23:20
This could be a protected class member instead.
Iain Merrick
2012/07/17 15:55:08
Done.
|
| + |
| + |
| +class CppChecker(checker.Checker): |
| + |
| + def Extensions(self): |
|
M-A Ruel
2012/07/17 14:23:20
EXTENSIONS = [
Iain Merrick
2012/07/17 15:55:08
Done.
|
| + return [ |
| + '.h', |
| + '.cc', |
| + '.m', |
| + '.mm', |
| + ] |
| + |
| + def _CheckLine(self, rules, line): |
| + """Checks the given file with the given rule set. |
| + Returns a tuple (is_include, illegal_description). |
| + If the line is an #include directive the first value will be True. |
| + If it is also an illegal include, the second value will be a string |
| + describing the error. Otherwise, it will be None.""" |
| + found_item = EXTRACT_INCLUDE_PATH.match(line) |
| + if not found_item: |
| + return False, None # Not a match |
| + |
| + include_path = found_item.group(1) |
| + |
| + # Fix up backslashes in case somebody accidentally used them. |
| + include_path.replace('\\', '/') |
|
M-A Ruel
2012/07/17 14:23:20
Err. The return value is ignored.
I'm glad we don
Iain Merrick
2012/07/17 15:55:08
Sounds good to me. (Though I'd better check there
|
| + |
| + if include_path.find('/') < 0: |
|
M-A Ruel
2012/07/17 14:23:20
if '/' not in include_path:
Iain Merrick
2012/07/17 15:55:08
Done.
|
| + # Don't fail when no directory is specified. We may want to be more |
| + # strict about this in the future. |
| + if self._verbose: |
| + print ' WARNING: directory specified with no path: ' + include_path |
| + return True, None |
| + |
| + (allowed, why_failed) = rules.DirAllowed(include_path) |
| + if not allowed: |
| + if self._verbose: |
| + retval = '\nFor ' + rules.__str__() |
|
M-A Ruel
2012/07/17 14:23:20
retval = '\nFor %s' % rules
Iain Merrick
2012/07/17 15:55:08
Wow, yes
|
| + else: |
| + retval = '' |
| + return True, retval + ('Illegal include: "%s"\n Because of %s' % |
| + (include_path, why_failed)) |
| + |
| + return True, None |
| + |
| + def CheckFile(self, rules, file_name): |
| + if self._verbose: |
| + print 'Checking: ' + file_name |
| + |
| + ret_val = '' # We'll collect the error messages in here |
| + last_include = 0 |
| + try: |
| + cur_file = open(file_name, 'r') |
|
M-A Ruel
2012/07/17 14:23:20
with codecs.open(filepath, encoding='utf-8') as f:
Iain Merrick
2012/07/17 15:55:08
Done.
|
| + in_if0 = 0 |
| + for line_num in xrange(sys.maxint): |
| + if line_num - last_include > MAX_UNINTERESTING_LINES: |
| + break |
| + |
| + cur_line = cur_file.readline(MAX_LINE_LENGTH) |
| + if cur_line == '': |
| + break |
| + cur_line = cur_line.strip() |
| + |
| + # Check to see if we're at / inside a #if 0 block |
| + if cur_line == '#if 0': |
| + in_if0 += 1 |
| + continue |
| + if in_if0 > 0: |
| + if cur_line.startswith('#if'): |
| + in_if0 += 1 |
| + elif cur_line == '#endif': |
| + in_if0 -= 1 |
| + continue |
| + |
| + is_include, line_status = self._CheckLine(rules, cur_line) |
| + if is_include: |
| + last_include = line_num |
| + if line_status is not None: |
| + if len(line_status) > 0: # Add newline to separate messages. |
| + line_status += '\n' |
| + ret_val += line_status |
| + cur_file.close() |
| + |
| + except IOError: |
| + if self._verbose: |
| + print 'Unable to open file: ' + file_name |
| + cur_file.close() |
| + |
| + # Map empty string to None for easier checking. |
|
M-A Ruel
2012/07/17 14:23:20
This check is completely unnecessary. Just add "re
Iain Merrick
2012/07/17 15:55:08
I just copied the existing code as is as far as po
|
| + if len(ret_val) == 0: |
| + return None |
| + return ret_val |
| + |
|
M-A Ruel
2012/07/17 14:23:20
Remove extra line.
Iain Merrick
2012/07/17 15:55:08
Done.
|