Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: tools/checkdeps/cpp_checker.py

Issue 10790014: Add Java support to checkdeps.py (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 """Checker implementation that checks C++ and Objective-C files.
6
7 If you import this module it will register itself with checker.Factory.
8 """
9
10 import re
11 import sys
12
13 import checker
14
15
16 # The maximum number of non-include lines we can see before giving up.
17 MAX_UNINTERESTING_LINES = 50
18
19 # The maximum line length, this is to be efficient in the case of very long
20 # lines (which can't be #includes).
21 MAX_LINE_LENGTH = 128
22
23 # This regular expression will be used to extract filenames from include
24 # statements.
25 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.
26
27
28 class CppChecker(checker.Checker):
29
30 def Extensions(self):
M-A Ruel 2012/07/17 14:23:20 EXTENSIONS = [
Iain Merrick 2012/07/17 15:55:08 Done.
31 return [
32 '.h',
33 '.cc',
34 '.m',
35 '.mm',
36 ]
37
38 def _CheckLine(self, rules, line):
39 """Checks the given file with the given rule set.
40 Returns a tuple (is_include, illegal_description).
41 If the line is an #include directive the first value will be True.
42 If it is also an illegal include, the second value will be a string
43 describing the error. Otherwise, it will be None."""
44 found_item = EXTRACT_INCLUDE_PATH.match(line)
45 if not found_item:
46 return False, None # Not a match
47
48 include_path = found_item.group(1)
49
50 # Fix up backslashes in case somebody accidentally used them.
51 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
52
53 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.
54 # Don't fail when no directory is specified. We may want to be more
55 # strict about this in the future.
56 if self._verbose:
57 print ' WARNING: directory specified with no path: ' + include_path
58 return True, None
59
60 (allowed, why_failed) = rules.DirAllowed(include_path)
61 if not allowed:
62 if self._verbose:
63 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
64 else:
65 retval = ''
66 return True, retval + ('Illegal include: "%s"\n Because of %s' %
67 (include_path, why_failed))
68
69 return True, None
70
71 def CheckFile(self, rules, file_name):
72 if self._verbose:
73 print 'Checking: ' + file_name
74
75 ret_val = '' # We'll collect the error messages in here
76 last_include = 0
77 try:
78 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.
79 in_if0 = 0
80 for line_num in xrange(sys.maxint):
81 if line_num - last_include > MAX_UNINTERESTING_LINES:
82 break
83
84 cur_line = cur_file.readline(MAX_LINE_LENGTH)
85 if cur_line == '':
86 break
87 cur_line = cur_line.strip()
88
89 # Check to see if we're at / inside a #if 0 block
90 if cur_line == '#if 0':
91 in_if0 += 1
92 continue
93 if in_if0 > 0:
94 if cur_line.startswith('#if'):
95 in_if0 += 1
96 elif cur_line == '#endif':
97 in_if0 -= 1
98 continue
99
100 is_include, line_status = self._CheckLine(rules, cur_line)
101 if is_include:
102 last_include = line_num
103 if line_status is not None:
104 if len(line_status) > 0: # Add newline to separate messages.
105 line_status += '\n'
106 ret_val += line_status
107 cur_file.close()
108
109 except IOError:
110 if self._verbose:
111 print 'Unable to open file: ' + file_name
112 cur_file.close()
113
114 # 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
115 if len(ret_val) == 0:
116 return None
117 return ret_val
118
M-A Ruel 2012/07/17 14:23:20 Remove extra line.
Iain Merrick 2012/07/17 15:55:08 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698