| 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 # The maximum line length, this is to be efficient in the case of very long | 28 # The maximum line length, this is to be efficient in the case of very long |
| 29 # lines (which can't be #includes). | 29 # lines (which can't be #includes). |
| 30 _MAX_LINE_LENGTH = 128 | 30 _MAX_LINE_LENGTH = 128 |
| 31 | 31 |
| 32 # This regular expression will be used to extract filenames from include | 32 # This regular expression will be used to extract filenames from include |
| 33 # statements. | 33 # statements. |
| 34 _EXTRACT_INCLUDE_PATH = re.compile( | 34 _EXTRACT_INCLUDE_PATH = re.compile( |
| 35 '[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') | 35 '[ \t]*#[ \t]*(?:include|import)[ \t]+"(.*)"') |
| 36 | 36 |
| 37 def __init__(self, verbose): | 37 def __init__(self, verbose, resolve_dotdot=False, root_dir=''): |
| 38 self._verbose = verbose | 38 self._verbose = verbose |
| 39 self._resolve_dotdot = resolve_dotdot |
| 40 self._root_dir = root_dir |
| 39 | 41 |
| 40 def CheckLine(self, rules, line, dependee_path, fail_on_temp_allow=False): | 42 def CheckLine(self, rules, line, dependee_path, fail_on_temp_allow=False): |
| 41 """Checks the given line with the given rule set. | 43 """Checks the given line with the given rule set. |
| 42 | 44 |
| 43 Returns a tuple (is_include, dependency_violation) where | 45 Returns a tuple (is_include, dependency_violation) where |
| 44 is_include is True only if the line is an #include or #import | 46 is_include is True only if the line is an #include or #import |
| 45 statement, and dependency_violation is an instance of | 47 statement, and dependency_violation is an instance of |
| 46 results.DependencyViolation if the line violates a rule, or None | 48 results.DependencyViolation if the line violates a rule, or None |
| 47 if it does not. | 49 if it does not. |
| 48 """ | 50 """ |
| 49 found_item = self._EXTRACT_INCLUDE_PATH.match(line) | 51 found_item = self._EXTRACT_INCLUDE_PATH.match(line) |
| 50 if not found_item: | 52 if not found_item: |
| 51 return False, None # Not a match | 53 return False, None # Not a match |
| 52 | 54 |
| 53 include_path = found_item.group(1) | 55 include_path = found_item.group(1) |
| 54 | 56 |
| 55 if '\\' in include_path: | 57 if '\\' in include_path: |
| 56 return True, results.DependencyViolation( | 58 return True, results.DependencyViolation( |
| 57 include_path, | 59 include_path, |
| 58 MessageRule('Include paths may not include backslashes.'), | 60 MessageRule('Include paths may not include backslashes.'), |
| 59 rules) | 61 rules) |
| 60 | 62 |
| 61 if '/' not in include_path: | 63 if '/' not in include_path: |
| 62 # Don't fail when no directory is specified. We may want to be more | 64 # Don't fail when no directory is specified. We may want to be more |
| 63 # strict about this in the future. | 65 # strict about this in the future. |
| 64 if self._verbose: | 66 if self._verbose: |
| 65 print ' WARNING: include specified with no directory: ' + include_path | 67 print ' WARNING: include specified with no directory: ' + include_path |
| 66 return True, None | 68 return True, None |
| 67 | 69 |
| 70 if self._resolve_dotdot and '../' in include_path: |
| 71 dependee_dir = os.path.dirname(dependee_path) |
| 72 include_path = os.path.normpath(os.path.join(dependee_dir, include_path)) |
| 73 include_path = os.path.relpath(include_path, self._root_dir) |
| 74 |
| 68 rule = rules.RuleApplyingTo(include_path, dependee_path) | 75 rule = rules.RuleApplyingTo(include_path, dependee_path) |
| 69 if (rule.allow == Rule.DISALLOW or | 76 if (rule.allow == Rule.DISALLOW or |
| 70 (fail_on_temp_allow and rule.allow == Rule.TEMP_ALLOW)): | 77 (fail_on_temp_allow and rule.allow == Rule.TEMP_ALLOW)): |
| 71 return True, results.DependencyViolation(include_path, rule, rules) | 78 return True, results.DependencyViolation(include_path, rule, rules) |
| 72 return True, None | 79 return True, None |
| 73 | 80 |
| 74 def CheckFile(self, rules, filepath): | 81 def CheckFile(self, rules, filepath): |
| 75 if self._verbose: | 82 if self._verbose: |
| 76 print 'Checking: ' + filepath | 83 print 'Checking: ' + filepath |
| 77 | 84 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 104 dependee_status.AddViolation(violation) | 111 dependee_status.AddViolation(violation) |
| 105 | 112 |
| 106 return dependee_status | 113 return dependee_status |
| 107 | 114 |
| 108 @staticmethod | 115 @staticmethod |
| 109 def IsCppFile(file_path): | 116 def IsCppFile(file_path): |
| 110 """Returns True iff the given path ends in one of the extensions | 117 """Returns True iff the given path ends in one of the extensions |
| 111 handled by this checker. | 118 handled by this checker. |
| 112 """ | 119 """ |
| 113 return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS | 120 return os.path.splitext(file_path)[1] in CppChecker.EXTENSIONS |
| OLD | NEW |