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..f1be2e2b79341f71e4cbade2070728423879a087 |
--- /dev/null |
+++ b/tools/checkdeps/cpp_checker.py |
@@ -0,0 +1,104 @@ |
+# 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. |
+ |
+"""Checks C++ and Objective-C files for illegal includes.""" |
+ |
+import codecs |
+import re |
+import sys |
+ |
+ |
+class CppChecker(object): |
+ |
+ EXTENSIONS = [ |
+ '.h', |
+ '.cc', |
+ '.m', |
+ '.mm', |
+ ] |
+ |
+ # 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]+"(.*)"') |
+ |
+ def __init__(self, verbose): |
+ self._verbose = verbose |
+ |
+ 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 = self._EXTRACT_INCLUDE_PATH.match(line) |
+ if not found_item: |
+ return False, None # Not a match |
+ |
+ include_path = found_item.group(1) |
+ |
+ if '\\' in include_path: |
+ return True, 'Include paths may not include backslashes' |
+ |
+ if '/' not in include_path: |
+ # 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 %s' % rules |
+ else: |
+ retval = '' |
+ return True, retval + ('Illegal include: "%s"\n Because of %s' % |
+ (include_path, why_failed)) |
+ |
+ return True, None |
+ |
+ def CheckFile(self, rules, filepath): |
+ if self._verbose: |
+ print 'Checking: ' + filepath |
+ |
+ ret_val = '' # We'll collect the error messages in here |
+ last_include = 0 |
+ with codecs.open(filepath, encoding='utf-8') as f: |
+ in_if0 = 0 |
+ for line_num in xrange(sys.maxint): |
M-A Ruel
2012/07/18 14:19:37
An alternative is:
for line_num, line in enumerat
Iain Merrick
2012/07/19 12:44:34
Good call, done. And I think that's the only reaso
|
+ if line_num - last_include > self._MAX_UNINTERESTING_LINES: |
+ break |
+ |
+ cur_line = f.readline(self._MAX_LINE_LENGTH).strip() |
M-A Ruel
2012/07/18 14:19:37
line = line.strip()
I don't think line 82-83 are
Iain Merrick
2012/07/19 12:44:34
Seemed like a harmless optimization to me, so I le
|
+ if cur_line == '': |
+ break |
+ |
+ # Check to see if we're at / inside a #if 0 block |
+ if cur_line == '#if 0': |
M-A Ruel
2012/07/18 14:19:37
if line.startswith('#if 0'):
would catch: #if 0
Iain Merrick
2012/07/19 12:44:34
Good idea, done.
This is just the original algori
|
+ 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 |
+ |
+ return ret_val |