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

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

Powered by Google App Engine
This is Rietveld 408576698