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 |
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')) | |
15 | |
11 | 16 |
12 def _CheckForUseOfWrongClock(input_api, output_api): | 17 def _CheckForUseOfWrongClock(input_api, output_api): |
13 """Make sure new lines of media code don't use a clock susceptible to skew.""" | 18 """Make sure new lines of media code don't use a clock susceptible to skew.""" |
14 | 19 |
15 def FilterFile(affected_file): | |
16 """Return true if the file could contain code referencing base::Time.""" | |
17 return affected_file.LocalPath().endswith( | |
18 ('.h', '.cc', '.cpp', '.cxx', '.mm')) | |
19 | |
20 # Regular expression that should detect any explicit references to the | 20 # Regular expression that should detect any explicit references to the |
21 # base::Time type (or base::Clock/DefaultClock), whether in using decls, | 21 # base::Time type (or base::Clock/DefaultClock), whether in using decls, |
22 # typedefs, or to call static methods. | 22 # typedefs, or to call static methods. |
23 base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)' | 23 base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)' |
24 | 24 |
25 # Regular expression that should detect references to the base::Time class | 25 # Regular expression that should detect references to the base::Time class |
26 # members, such as a call to base::Time::Now. | 26 # members, such as a call to base::Time::Now. |
27 base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::' | 27 base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::' |
28 | 28 |
29 # Regular expression to detect "using base::Time" declarations. We want to | 29 # Regular expression to detect "using base::Time" declarations. We want to |
30 # prevent these from triggerring a warning. For example, it's perfectly | 30 # prevent these from triggerring a warning. For example, it's perfectly |
31 # reasonable for code to be written like this: | 31 # reasonable for code to be written like this: |
32 # | 32 # |
33 # using base::Time; | 33 # using base::Time; |
34 # ... | 34 # ... |
35 # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond; | 35 # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond; |
36 using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;' | 36 using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;' |
37 | 37 |
38 # Regular expression to detect references to the kXXX constants in the | 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. | 39 # base::Time class. We want to prevent these from triggerring a warning. |
40 base_time_konstant_pattern = r'(^|\W)Time::k\w+' | 40 base_time_konstant_pattern = r'(^|\W)Time::k\w+' |
41 | 41 |
42 problem_re = input_api.re.compile( | 42 problem_re = input_api.re.compile( |
43 r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')') | 43 r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')') |
44 exception_re = input_api.re.compile( | 44 exception_re = input_api.re.compile( |
45 r'(' + using_base_time_decl_pattern + r')|(' + | 45 r'(' + using_base_time_decl_pattern + r')|(' + |
46 base_time_konstant_pattern + r')') | 46 base_time_konstant_pattern + r')') |
47 problems = [] | 47 problems = [] |
48 for f in input_api.AffectedSourceFiles(FilterFile): | 48 for f in input_api.AffectedSourceFiles(_FilterFile): |
49 for line_number, line in f.ChangedContents(): | 49 for line_number, line in f.ChangedContents(): |
50 if problem_re.search(line): | 50 if problem_re.search(line): |
51 if not exception_re.search(line): | 51 if not exception_re.search(line): |
52 problems.append( | 52 problems.append( |
53 ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip())) | 53 ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip())) |
54 | 54 |
55 if problems: | 55 if problems: |
56 return [output_api.PresubmitPromptOrNotify( | 56 return [output_api.PresubmitPromptOrNotify( |
57 'You added one or more references to the base::Time class and/or one\n' | 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' | 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' | 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' | 60 'Instead, could you use base::TimeTicks to track the passage of\n' |
61 'real-world time?\n\n' + | 61 'real-world time?\n\n' + |
62 '\n'.join(problems))] | 62 '\n'.join(problems))] |
63 else: | 63 else: |
64 return [] | 64 return [] |
65 | 65 |
66 | 66 |
67 def _CheckForMessageLoopProxy(input_api, output_api): | |
68 """Make sure media code only uses MessageLoopProxy for accessing the current | |
69 loop.""" | |
70 | |
71 message_loop_proxy_re = r'\bMessageLoopProxy(::current\(\))?' | |
72 | |
73 bad_files = [] | |
74 for f in input_api.AffectedSourceFiles(_FilterFile): | |
75 contents = input_api.ReadFile(f, 'rb') | |
DaleCurtis
2014/01/07 20:13:46
Instead of reading the file you should only need t
scherkus (not reviewing)
2014/01/07 21:40:22
Done.
(I initially had it read the whole file to
| |
76 for match in input_api.re.finditer(message_loop_proxy_re, contents): | |
77 if match.lastindex == 1: | |
78 continue | |
79 | |
80 # No need to check remaining matches in an offending file. | |
81 bad_files.append(f) | |
82 break | |
83 | |
84 if bad_files: | |
85 return [output_api.PresubmitError( | |
86 'These files reference MessageLoopProxy for uses other than accessing ' | |
87 'the current loop:', bad_files)] | |
88 | |
89 return [] | |
90 | |
91 | |
67 def _CheckChange(input_api, output_api): | 92 def _CheckChange(input_api, output_api): |
68 results = [] | 93 results = [] |
69 results.extend(_CheckForUseOfWrongClock(input_api, output_api)) | 94 results.extend(_CheckForUseOfWrongClock(input_api, output_api)) |
95 results.extend(_CheckForMessageLoopProxy(input_api, output_api)) | |
70 return results | 96 return results |
71 | 97 |
72 | 98 |
73 def CheckChangeOnUpload(input_api, output_api): | 99 def CheckChangeOnUpload(input_api, output_api): |
74 return _CheckChange(input_api, output_api) | 100 return _CheckChange(input_api, output_api) |
75 | 101 |
76 | 102 |
77 def CheckChangeOnCommit(input_api, output_api): | 103 def CheckChangeOnCommit(input_api, output_api): |
78 return _CheckChange(input_api, output_api) | 104 return _CheckChange(input_api, output_api) |
OLD | NEW |