OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """Top-level presubmit script for Chromium media component. | 5 """Top-level presubmit script for Chromium media component. |
6 | 6 |
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts | 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
8 for more details about the presubmit API built into gcl. | 8 for more details about the presubmit API built into gcl. |
9 """ | 9 """ |
10 | 10 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 79 |
80 if problems: | 80 if problems: |
81 return [output_api.PresubmitError( | 81 return [output_api.PresubmitError( |
82 'MessageLoopProxy should only be used for accessing the current loop.\n' | 82 'MessageLoopProxy should only be used for accessing the current loop.\n' |
83 'Use the TaskRunner interfaces instead as they are more explicit about\n' | 83 'Use the TaskRunner interfaces instead as they are more explicit about\n' |
84 'the run-time characteristics. In most cases, SingleThreadTaskRunner\n' | 84 'the run-time characteristics. In most cases, SingleThreadTaskRunner\n' |
85 'is a drop-in replacement for MessageLoopProxy.', problems)] | 85 'is a drop-in replacement for MessageLoopProxy.', problems)] |
86 | 86 |
87 return [] | 87 return [] |
88 | 88 |
| 89 def _CheckForHistogramOffByOne(input_api, output_api): |
| 90 """Make sure histogram enum maxes are used properly""" |
| 91 |
| 92 # A general-purpose chunk of regex to match whitespace and/or comments |
| 93 # that may be interspersed with the code we're interested in: |
| 94 comment = r'/\*.*?\*/|//[^\n]*' |
| 95 whitespace = r'(?:[\n\t ]|(?:' + comment + '))*' |
| 96 |
| 97 # The name is assumed to be a literal string. |
| 98 histogram_name = r'"[^"]*"' |
| 99 |
| 100 # This can be an arbitrary expression, so just ensure it isn't a ; to prevent |
| 101 # matching past the end of this statement. |
| 102 histogram_value = r'[^;]*' |
| 103 |
| 104 # In parens so we can retrieve it for further checks. |
| 105 histogram_max = r'([^;,]*)' |
| 106 |
| 107 # This should match a uma histogram enumeration macro expression. |
| 108 uma_macro_re = input_api.re.compile( |
| 109 r'\bUMA_HISTOGRAM_ENUMERATION\(' + whitespace + histogram_name + r',' + |
| 110 whitespace + histogram_value + r',' + whitespace + histogram_max + |
| 111 whitespace + r'\)' + whitespace + ';') |
| 112 |
| 113 uma_max_re = input_api.re.compile(r'.*(?:Max|MAX).* \+ 1') |
| 114 |
| 115 problems = [] |
| 116 |
| 117 for f in input_api.AffectedSourceFiles(_FilterFile): |
| 118 contents = input_api.ReadFile(f) |
| 119 |
| 120 # We want to match across lines, but still report a line number, so we keep |
| 121 # track of the line we're on as we search through the file. |
| 122 line_number = 1 |
| 123 |
| 124 # We search the entire file, then check if any violations are in the changed |
| 125 # areas, this is inefficient, but simple. A UMA_HISTOGRAM_ENUMERATION call |
| 126 # will often span multiple lines, so finding a match looking just at the |
| 127 # deltas line-by-line won't catch problems. |
| 128 match = uma_macro_re.search(contents) |
| 129 while match: |
| 130 line_number += contents.count('\n', 0, match.start()) |
| 131 max_arg = match.group(1) # The third argument. |
| 132 |
| 133 if not uma_max_re.match(max_arg): |
| 134 uma_range = range(match.start(), match.end() + 1) |
| 135 # Check if any part of the match is in the changed lines: |
| 136 for num, line in f.ChangedContents(): |
| 137 if line_number <= num <= line_number + match.group().count('\n'): |
| 138 problems.append('%s:%d' % (f, line_number)) |
| 139 break |
| 140 |
| 141 # Strip off the file contents up to the end of the match and update the |
| 142 # line number. |
| 143 contents = contents[match.end():] |
| 144 line_number += match.group().count('\n') |
| 145 match = uma_macro_re.search(contents) |
| 146 |
| 147 if problems: |
| 148 return [output_api.PresubmitError( |
| 149 'UMA_HISTOGRAM_ENUMERATION reports in src/media/ are expected to adhere\n' |
| 150 'to the following guidelines:\n' |
| 151 ' - The max value (3rd argument) should be an enum value equal to the\n' |
| 152 ' last valid value, e.g. FOO_MAX = LAST_VALID_FOO.\n' |
| 153 ' - 1 must be added to that max value.\n' |
| 154 'Contact rileya@chromium.org if you have questions.' , problems)] |
| 155 |
| 156 return [] |
| 157 |
89 | 158 |
90 def _CheckChange(input_api, output_api): | 159 def _CheckChange(input_api, output_api): |
91 results = [] | 160 results = [] |
92 results.extend(_CheckForUseOfWrongClock(input_api, output_api)) | 161 results.extend(_CheckForUseOfWrongClock(input_api, output_api)) |
93 results.extend(_CheckForMessageLoopProxy(input_api, output_api)) | 162 results.extend(_CheckForMessageLoopProxy(input_api, output_api)) |
| 163 results.extend(_CheckForHistogramOffByOne(input_api, output_api)) |
94 return results | 164 return results |
95 | 165 |
96 | 166 |
97 def CheckChangeOnUpload(input_api, output_api): | 167 def CheckChangeOnUpload(input_api, output_api): |
98 return _CheckChange(input_api, output_api) | 168 return _CheckChange(input_api, output_api) |
99 | 169 |
100 | 170 |
101 def CheckChangeOnCommit(input_api, output_api): | 171 def CheckChangeOnCommit(input_api, output_api): |
102 return _CheckChange(input_api, output_api) | 172 return _CheckChange(input_api, output_api) |
OLD | NEW |