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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 if '\\' in include_path: | 55 if '\\' in include_path: |
56 return True, results.DependencyViolation( | 56 return True, results.DependencyViolation( |
57 include_path, | 57 include_path, |
58 MessageRule('Include paths may not include backslashes.'), | 58 MessageRule('Include paths may not include backslashes.'), |
59 rules) | 59 rules) |
60 | 60 |
61 if '/' not in include_path: | 61 if '/' not in include_path: |
62 # Don't fail when no directory is specified. We may want to be more | 62 # Don't fail when no directory is specified. We may want to be more |
63 # strict about this in the future. | 63 # strict about this in the future. |
64 if self._verbose: | 64 if self._verbose: |
65 print ' WARNING: directory specified with no path: ' + include_path | 65 print ' WARNING: include specified with no directory: ' + include_path |
66 return True, None | 66 return True, None |
67 | 67 |
68 rule = rules.RuleApplyingTo(include_path, dependee_path) | 68 rule = rules.RuleApplyingTo(include_path, dependee_path) |
69 if (rule.allow == Rule.DISALLOW or | 69 if (rule.allow == Rule.DISALLOW or |
70 (fail_on_temp_allow and rule.allow == Rule.TEMP_ALLOW)): | 70 (fail_on_temp_allow and rule.allow == Rule.TEMP_ALLOW)): |
71 return True, results.DependencyViolation(include_path, rule, rules) | 71 return True, results.DependencyViolation(include_path, rule, rules) |
72 return True, None | 72 return True, None |
73 | 73 |
74 def CheckFile(self, rules, filepath): | 74 def CheckFile(self, rules, filepath): |
75 if self._verbose: | 75 if self._verbose: |
76 print 'Checking: ' + filepath | 76 print 'Checking: ' + filepath |
77 | 77 |
78 dependee_status = results.DependeeStatus(filepath) | 78 dependee_status = results.DependeeStatus(filepath) |
79 ret_val = '' # We'll collect the error messages in here | 79 ret_val = '' # We'll collect the error messages in here |
80 last_include = 0 | 80 last_include = 0 |
81 with codecs.open(filepath, encoding='utf-8') as f: | 81 with codecs.open(filepath, encoding='utf-8') as f: |
82 in_if0 = 0 | 82 in_if0 = 0 |
83 for line_num, line in enumerate(f): | 83 for line_num, line in enumerate(f): |
84 if line_num - last_include > self._MAX_UNINTERESTING_LINES: | 84 if line_num - last_include > self._MAX_UNINTERESTING_LINES: |
85 break | 85 break |
86 | 86 |
87 line = line.strip() | 87 line = line.strip() |
88 | 88 |
89 # Check to see if we're at / inside a #if 0 block | 89 # Check to see if we're at / inside an #if 0 block |
90 if line.startswith('#if 0'): | 90 if line.startswith('#if 0'): |
91 in_if0 += 1 | 91 in_if0 += 1 |
92 continue | 92 continue |
93 if in_if0 > 0: | 93 if in_if0 > 0: |
94 if line.startswith('#if'): | 94 if line.startswith('#if'): |
95 in_if0 += 1 | 95 in_if0 += 1 |
96 elif line.startswith('#endif'): | 96 elif line.startswith('#endif'): |
97 in_if0 -= 1 | 97 in_if0 -= 1 |
98 continue | 98 continue |
99 | 99 |
100 is_include, violation = self.CheckLine(rules, line, filepath) | 100 is_include, violation = self.CheckLine(rules, line, filepath) |
101 if is_include: | 101 if is_include: |
102 last_include = line_num | 102 last_include = line_num |
103 if violation: | 103 if violation: |
104 dependee_status.AddViolation(violation) | 104 dependee_status.AddViolation(violation) |
105 | 105 |
106 return dependee_status | 106 return dependee_status |
107 | 107 |
108 @staticmethod | 108 @staticmethod |
109 def IsCppFile(file_path): | 109 def IsCppFile(file_path): |
110 """Returns True iff the given path ends in one of the extensions | 110 """Returns True iff the given path ends in one of the extensions |
111 handled by this checker. | 111 handled by this checker. |
112 """ | 112 """ |
113 return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS | 113 return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS |
OLD | NEW |