Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Checks C++ and Objective-C files for illegal includes.""" | |
| 6 | |
| 7 import codecs | |
| 8 import re | |
| 9 import sys | |
| 10 | |
| 11 | |
| 12 class CppChecker(object): | |
| 13 | |
| 14 EXTENSIONS = [ | |
| 15 '.h', | |
| 16 '.cc', | |
| 17 '.m', | |
| 18 '.mm', | |
| 19 ] | |
| 20 | |
| 21 # The maximum number of non-include lines we can see before giving up. | |
| 22 _MAX_UNINTERESTING_LINES = 50 | |
| 23 | |
| 24 # The maximum line length, this is to be efficient in the case of very long | |
| 25 # lines (which can't be #includes). | |
| 26 _MAX_LINE_LENGTH = 128 | |
| 27 | |
| 28 # This regular expression will be used to extract filenames from include | |
| 29 # statements. | |
| 30 _EXTRACT_INCLUDE_PATH = re.compile( | |
| 31 '[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') | |
| 32 | |
| 33 def __init__(self, verbose): | |
| 34 self._verbose = verbose | |
| 35 | |
| 36 def _CheckLine(self, rules, line): | |
| 37 """Checks the given file with the given rule set. | |
| 38 Returns a tuple (is_include, illegal_description). | |
| 39 If the line is an #include directive the first value will be True. | |
| 40 If it is also an illegal include, the second value will be a string | |
| 41 describing the error. Otherwise, it will be None.""" | |
| 42 found_item = self._EXTRACT_INCLUDE_PATH.match(line) | |
| 43 if not found_item: | |
| 44 return False, None # Not a match | |
| 45 | |
| 46 include_path = found_item.group(1) | |
| 47 | |
| 48 if '\\' in include_path: | |
| 49 return True, 'Include paths may not include backslashes' | |
| 50 | |
| 51 if '/' not in include_path: | |
| 52 # Don't fail when no directory is specified. We may want to be more | |
| 53 # strict about this in the future. | |
| 54 if self._verbose: | |
| 55 print ' WARNING: directory specified with no path: ' + include_path | |
| 56 return True, None | |
| 57 | |
| 58 (allowed, why_failed) = rules.DirAllowed(include_path) | |
| 59 if not allowed: | |
| 60 if self._verbose: | |
| 61 retval = '\nFor %s' % rules | |
| 62 else: | |
| 63 retval = '' | |
| 64 return True, retval + ('Illegal include: "%s"\n Because of %s' % | |
| 65 (include_path, why_failed)) | |
| 66 | |
| 67 return True, None | |
| 68 | |
| 69 def CheckFile(self, rules, filepath): | |
| 70 if self._verbose: | |
| 71 print 'Checking: ' + filepath | |
| 72 | |
| 73 ret_val = '' # We'll collect the error messages in here | |
| 74 last_include = 0 | |
| 75 with codecs.open(filepath, encoding='utf-8') as f: | |
| 76 in_if0 = 0 | |
| 77 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
| |
| 78 if line_num - last_include > self._MAX_UNINTERESTING_LINES: | |
| 79 break | |
| 80 | |
| 81 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
| |
| 82 if cur_line == '': | |
| 83 break | |
| 84 | |
| 85 # Check to see if we're at / inside a #if 0 block | |
| 86 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
| |
| 87 in_if0 += 1 | |
| 88 continue | |
| 89 if in_if0 > 0: | |
| 90 if cur_line.startswith('#if'): | |
| 91 in_if0 += 1 | |
| 92 elif cur_line == '#endif': | |
| 93 in_if0 -= 1 | |
| 94 continue | |
| 95 | |
| 96 is_include, line_status = self._CheckLine(rules, cur_line) | |
| 97 if is_include: | |
| 98 last_include = line_num | |
| 99 if line_status is not None: | |
| 100 if len(line_status) > 0: # Add newline to separate messages. | |
| 101 line_status += '\n' | |
| 102 ret_val += line_status | |
| 103 | |
| 104 return ret_val | |
| OLD | NEW |