OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Checks C++ and Objective-C files for illegal includes.""" | 5 """Checks C++ and Objective-C files for illegal includes.""" |
6 | 6 |
7 import codecs | 7 import codecs |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 _MAX_LINE_LENGTH = 128 | 29 _MAX_LINE_LENGTH = 128 |
30 | 30 |
31 # This regular expression will be used to extract filenames from include | 31 # This regular expression will be used to extract filenames from include |
32 # statements. | 32 # statements. |
33 _EXTRACT_INCLUDE_PATH = re.compile( | 33 _EXTRACT_INCLUDE_PATH = re.compile( |
34 '[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') | 34 '[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') |
35 | 35 |
36 def __init__(self, verbose): | 36 def __init__(self, verbose): |
37 self._verbose = verbose | 37 self._verbose = verbose |
38 | 38 |
39 def CheckLine(self, rules, line, fail_on_temp_allow=False): | 39 def CheckLine(self, rules, line, dependee_path, fail_on_temp_allow=False): |
40 """Checks the given line with the given rule set. | 40 """Checks the given line with the given rule set. |
41 | 41 |
42 Returns a tuple (is_include, dependency_violation) where | 42 Returns a tuple (is_include, dependency_violation) where |
43 is_include is True only if the line is an #include or #import | 43 is_include is True only if the line is an #include or #import |
44 statement, and dependency_violation is an instance of | 44 statement, and dependency_violation is an instance of |
45 results.DependencyViolation if the line violates a rule, or None | 45 results.DependencyViolation if the line violates a rule, or None |
46 if it does not. | 46 if it does not. |
47 """ | 47 """ |
48 found_item = self._EXTRACT_INCLUDE_PATH.match(line) | 48 found_item = self._EXTRACT_INCLUDE_PATH.match(line) |
49 if not found_item: | 49 if not found_item: |
50 return False, None # Not a match | 50 return False, None # Not a match |
51 | 51 |
52 include_path = found_item.group(1) | 52 include_path = found_item.group(1) |
53 | 53 |
54 if '\\' in include_path: | 54 if '\\' in include_path: |
55 return True, rules.SpecificRule( | 55 return True, rules.SpecificRule( |
56 'Include paths may not include backslashes.') | 56 'Include paths may not include backslashes.') |
57 | 57 |
58 if '/' not in include_path: | 58 if '/' not in include_path: |
59 # Don't fail when no directory is specified. We may want to be more | 59 # Don't fail when no directory is specified. We may want to be more |
60 # strict about this in the future. | 60 # strict about this in the future. |
61 if self._verbose: | 61 if self._verbose: |
62 print ' WARNING: directory specified with no path: ' + include_path | 62 print ' WARNING: directory specified with no path: ' + include_path |
63 return True, None | 63 return True, None |
64 | 64 |
65 rule = rules.RuleApplyingTo(include_path) | 65 rule = rules.RuleApplyingTo(include_path, dependee_path) |
66 if (rule.allow == Rule.DISALLOW or | 66 if (rule.allow == Rule.DISALLOW or |
67 (fail_on_temp_allow and rule.allow == Rule.TEMP_ALLOW)): | 67 (fail_on_temp_allow and rule.allow == Rule.TEMP_ALLOW)): |
68 return True, results.DependencyViolation(include_path, rule, rules) | 68 return True, results.DependencyViolation(include_path, rule, rules) |
69 return True, None | 69 return True, None |
70 | 70 |
71 def CheckFile(self, rules, filepath): | 71 def CheckFile(self, rules, filepath): |
72 if self._verbose: | 72 if self._verbose: |
73 print 'Checking: ' + filepath | 73 print 'Checking: ' + filepath |
74 | 74 |
75 dependee_status = results.DependeeStatus(filepath) | 75 dependee_status = results.DependeeStatus(filepath) |
(...skipping 11 matching lines...) Expand all Loading... |
87 if line.startswith('#if 0'): | 87 if line.startswith('#if 0'): |
88 in_if0 += 1 | 88 in_if0 += 1 |
89 continue | 89 continue |
90 if in_if0 > 0: | 90 if in_if0 > 0: |
91 if line.startswith('#if'): | 91 if line.startswith('#if'): |
92 in_if0 += 1 | 92 in_if0 += 1 |
93 elif line.startswith('#endif'): | 93 elif line.startswith('#endif'): |
94 in_if0 -= 1 | 94 in_if0 -= 1 |
95 continue | 95 continue |
96 | 96 |
97 is_include, violation = self.CheckLine(rules, line) | 97 is_include, violation = self.CheckLine(rules, line, filepath) |
98 if is_include: | 98 if is_include: |
99 last_include = line_num | 99 last_include = line_num |
100 if violation: | 100 if violation: |
101 dependee_status.AddViolation(violation) | 101 dependee_status.AddViolation(violation) |
102 | 102 |
103 return dependee_status | 103 return dependee_status |
104 | 104 |
105 @staticmethod | 105 @staticmethod |
106 def IsCppFile(file_path): | 106 def IsCppFile(file_path): |
107 """Returns True iff the given path ends in one of the extensions | 107 """Returns True iff the given path ends in one of the extensions |
108 handled by this checker. | 108 handled by this checker. |
109 """ | 109 """ |
110 return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS | 110 return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS |
OLD | NEW |