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
3 # found in the LICENSE file.
5 """Top-level presubmit script for Chromium media component.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
11 def _FilterFile(affected_file
):
12 """Return true if the file could contain code requiring a presubmit check."""
13 return affected_file
.LocalPath().endswith(
14 ('.h', '.cc', '.cpp', '.cxx', '.mm'))
17 def _CheckForUseOfWrongClock(input_api
, output_api
):
18 """Make sure new lines of media code don't use a clock susceptible to skew."""
20 # Regular expression that should detect any explicit references to the
21 # base::Time type (or base::Clock/DefaultClock), whether in using decls,
22 # typedefs, or to call static methods.
23 base_time_type_pattern
= r
'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)'
25 # Regular expression that should detect references to the base::Time class
26 # members, such as a call to base::Time::Now.
27 base_time_member_pattern
= r
'(^|\W)(Time|Clock|DefaultClock)::'
29 # Regular expression to detect "using base::Time" declarations. We want to
30 # prevent these from triggerring a warning. For example, it's perfectly
31 # reasonable for code to be written like this:
35 # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond;
36 using_base_time_decl_pattern
= r
'^\s*using\s+(::)?base::Time\s*;'
38 # Regular expression to detect references to the kXXX constants in the
39 # base::Time class. We want to prevent these from triggerring a warning.
40 base_time_konstant_pattern
= r
'(^|\W)Time::k\w+'
42 problem_re
= input_api
.re
.compile(
43 r
'(' + base_time_type_pattern
+ r
')|(' + base_time_member_pattern
+ r
')')
44 exception_re
= input_api
.re
.compile(
45 r
'(' + using_base_time_decl_pattern
+ r
')|(' +
46 base_time_konstant_pattern
+ r
')')
48 for f
in input_api
.AffectedSourceFiles(_FilterFile
):
49 for line_number
, line
in f
.ChangedContents():
50 if problem_re
.search(line
):
51 if not exception_re
.search(line
):
53 ' %s:%d\n %s' % (f
.LocalPath(), line_number
, line
.strip()))
56 return [output_api
.PresubmitPromptOrNotify(
57 'You added one or more references to the base::Time class and/or one\n'
58 'of its member functions (or base::Clock/DefaultClock). In media\n'
59 'code, it is rarely correct to use a clock susceptible to time skew!\n'
60 'Instead, could you use base::TimeTicks to track the passage of\n'
61 'real-world time?\n\n' +
67 def _CheckForMessageLoopProxy(input_api
, output_api
):
68 """Make sure media code only uses MessageLoopProxy for accessing the current
71 message_loop_proxy_re
= input_api
.re
.compile(
72 r
'\bMessageLoopProxy(?!::current\(\))')
75 for f
in input_api
.AffectedSourceFiles(_FilterFile
):
76 for line_number
, line
in f
.ChangedContents():
77 if message_loop_proxy_re
.search(line
):
78 problems
.append('%s:%d' % (f
.LocalPath(), line_number
))
81 return [output_api
.PresubmitError(
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'
84 'the run-time characteristics. In most cases, SingleThreadTaskRunner\n'
85 'is a drop-in replacement for MessageLoopProxy.', problems
)]
90 def _CheckForHistogramOffByOne(input_api
, output_api
):
91 """Make sure histogram enum maxes are used properly"""
93 # A general-purpose chunk of regex to match whitespace and/or comments
94 # that may be interspersed with the code we're interested in:
95 comment
= r
'/\*.*?\*/|//[^\n]*'
96 whitespace
= r
'(?:[\n\t ]|(?:' + comment
+ r
'))*'
98 # The name is assumed to be a literal string.
99 histogram_name
= r
'"[^"]*"'
101 # This can be an arbitrary expression, so just ensure it isn't a ; to prevent
102 # matching past the end of this statement.
103 histogram_value
= r
'[^;]*'
105 # In parens so we can retrieve it for further checks.
106 histogram_max
= r
'([^;,]*)'
108 # This should match a uma histogram enumeration macro expression.
109 uma_macro_re
= input_api
.re
.compile(
110 r
'\bUMA_HISTOGRAM_ENUMERATION\(' + whitespace
+ histogram_name
+ r
',' +
111 whitespace
+ histogram_value
+ r
',' + whitespace
+ histogram_max
+
112 whitespace
+ r
'\)' + whitespace
+ r
';(?:' + whitespace
+
113 r
'\/\/ (PRESUBMIT_IGNORE_UMA_MAX))?')
115 uma_max_re
= input_api
.re
.compile(r
'.*(?:Max|MAX).* \+ 1')
119 for f
in input_api
.AffectedSourceFiles(_FilterFile
):
120 contents
= input_api
.ReadFile(f
)
122 # We want to match across lines, but still report a line number, so we keep
123 # track of the line we're on as we search through the file.
126 # We search the entire file, then check if any violations are in the changed
127 # areas, this is inefficient, but simple. A UMA_HISTOGRAM_ENUMERATION call
128 # will often span multiple lines, so finding a match looking just at the
129 # deltas line-by-line won't catch problems.
130 match
= uma_macro_re
.search(contents
)
132 line_number
+= contents
.count('\n', 0, match
.start())
133 max_arg
= match
.group(1) # The third argument.
135 if (not uma_max_re
.match(max_arg
) and match
.group(2) !=
136 'PRESUBMIT_IGNORE_UMA_MAX'):
137 uma_range
= range(match
.start(), match
.end() + 1)
138 # Check if any part of the match is in the changed lines:
139 for num
, line
in f
.ChangedContents():
140 if line_number
<= num
<= line_number
+ match
.group().count('\n'):
141 problems
.append('%s:%d' % (f
, line_number
))
144 # Strip off the file contents up to the end of the match and update the
146 contents
= contents
[match
.end():]
147 line_number
+= match
.group().count('\n')
148 match
= uma_macro_re
.search(contents
)
151 return [output_api
.PresubmitError(
152 'UMA_HISTOGRAM_ENUMERATION reports in src/media/ are expected to adhere\n'
153 'to the following guidelines:\n'
154 ' - The max value (3rd argument) should be an enum value equal to the\n'
155 ' last valid value, e.g. FOO_MAX = LAST_VALID_FOO.\n'
156 ' - 1 must be added to that max value.\n'
157 'Contact rileya@chromium.org if you have questions.' , problems
)]
162 def _CheckChange(input_api
, output_api
):
164 results
.extend(_CheckForUseOfWrongClock(input_api
, output_api
))
165 results
.extend(_CheckForMessageLoopProxy(input_api
, output_api
))
166 results
.extend(_CheckForHistogramOffByOne(input_api
, output_api
))
170 def CheckChangeOnUpload(input_api
, output_api
):
171 return _CheckChange(input_api
, output_api
)
174 def CheckChangeOnCommit(input_api
, output_api
):
175 return _CheckChange(input_api
, output_api
)