[email protected] | 69085bc5 | 2012-10-15 16:41:58 | [diff] [blame] | 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 |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for cc. |
| 6 | |
[email protected] | 94be7f70 | 2014-02-03 19:06:45 | [diff] [blame] | 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 8 | for more details about the presubmit API built into depot_tools. |
[email protected] | 69085bc5 | 2012-10-15 16:41:58 | [diff] [blame] | 9 | """ |
| 10 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 11 | import re |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 12 | import string |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 13 | |
tfarina | 26ef0f0 | 2015-02-02 14:50:25 | [diff] [blame] | 14 | CC_SOURCE_FILES=(r'^cc[\\/].*\.(cc|h)$',) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 15 | |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 16 | def CheckChangeLintsClean(input_api, output_api): |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 17 | source_filter = lambda x: input_api.FilterSourceFile( |
| 18 | x, white_list=CC_SOURCE_FILES, black_list=None) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 19 | |
tfarina | 2d64901 | 2015-02-26 12:58:26 | [diff] [blame] | 20 | return input_api.canned_checks.CheckChangeLintsClean( |
| 21 | input_api, output_api, source_filter, lint_filters=[], verbose_level=1) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 22 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 23 | def CheckAsserts(input_api, output_api, white_list=CC_SOURCE_FILES, black_list=None): |
| 24 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 25 | source_file_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list) |
| 26 | |
| 27 | assert_files = [] |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 28 | |
| 29 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 30 | contents = input_api.ReadFile(f, 'rb') |
| 31 | # WebKit ASSERT() is not allowed. |
[email protected] | f4a4b0e | 2012-10-22 22:01:37 | [diff] [blame] | 32 | if re.search(r"\bASSERT\(", contents): |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 33 | assert_files.append(f.LocalPath()) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 34 | |
| 35 | if assert_files: |
| 36 | return [output_api.PresubmitError( |
| 37 | 'These files use ASSERT instead of using DCHECK:', |
| 38 | items=assert_files)] |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 39 | return [] |
| 40 | |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 41 | def CheckStdAbs(input_api, output_api, |
| 42 | white_list=CC_SOURCE_FILES, black_list=None): |
| 43 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 44 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 45 | white_list, |
| 46 | black_list) |
| 47 | |
| 48 | using_std_abs_files = [] |
| 49 | found_fabs_files = [] |
| 50 | missing_std_prefix_files = [] |
| 51 | |
| 52 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 53 | contents = input_api.ReadFile(f, 'rb') |
| 54 | if re.search(r"using std::f?abs;", contents): |
| 55 | using_std_abs_files.append(f.LocalPath()) |
| 56 | if re.search(r"\bfabsf?\(", contents): |
| 57 | found_fabs_files.append(f.LocalPath()); |
[email protected] | dbddc746 | 2013-09-06 06:33:31 | [diff] [blame] | 58 | |
| 59 | no_std_prefix = r"(?<!std::)" |
| 60 | # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix. |
| 61 | abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix |
| 62 | fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix |
| 63 | # Skips matching any lines that have "// NOLINT". |
| 64 | no_nolint = r"(?![^\n]*//\s+NOLINT)" |
| 65 | |
| 66 | expression = re.compile("(%s|%s)%s" % |
| 67 | (abs_without_prefix, fabs_without_prefix, no_nolint)) |
| 68 | if expression.search(contents): |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 69 | missing_std_prefix_files.append(f.LocalPath()) |
| 70 | |
| 71 | result = [] |
| 72 | if using_std_abs_files: |
| 73 | result.append(output_api.PresubmitError( |
| 74 | 'These files have "using std::abs" which is not permitted.', |
| 75 | items=using_std_abs_files)) |
| 76 | if found_fabs_files: |
| 77 | result.append(output_api.PresubmitError( |
| 78 | 'std::abs() should be used instead of std::fabs() for consistency.', |
| 79 | items=found_fabs_files)) |
| 80 | if missing_std_prefix_files: |
| 81 | result.append(output_api.PresubmitError( |
| 82 | 'These files use abs(), absf(), fabs(), or fabsf() without qualifying ' |
| 83 | 'the std namespace. Please use std::abs() in all places.', |
| 84 | items=missing_std_prefix_files)) |
| 85 | return result |
| 86 | |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 87 | def CheckPassByValue(input_api, |
| 88 | output_api, |
| 89 | white_list=CC_SOURCE_FILES, |
| 90 | black_list=None): |
| 91 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 92 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 93 | white_list, |
| 94 | black_list) |
| 95 | |
| 96 | local_errors = [] |
| 97 | |
kylechar | e68cc4a | 2017-04-05 18:01:05 | [diff] [blame] | 98 | # Well-defined simple classes the same size as a primitive type. |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 99 | pass_by_value_types = ['base::Time', |
| 100 | 'base::TimeTicks', |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 101 | ] |
| 102 | |
| 103 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 104 | contents = input_api.ReadFile(f, 'rb') |
| 105 | match = re.search( |
| 106 | r'\bconst +' + '(?P<type>(%s))&' % |
| 107 | string.join(pass_by_value_types, '|'), |
| 108 | contents) |
| 109 | if match: |
| 110 | local_errors.append(output_api.PresubmitError( |
| 111 | '%s passes %s by const ref instead of by value.' % |
| 112 | (f.LocalPath(), match.group('type')))) |
| 113 | return local_errors |
[email protected] | d936f90 | 2013-01-06 05:08:07 | [diff] [blame] | 114 | |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 115 | def CheckTodos(input_api, output_api): |
| 116 | errors = [] |
| 117 | |
| 118 | source_file_filter = lambda x: x |
| 119 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 120 | contents = input_api.ReadFile(f, 'rb') |
[email protected] | 932aff4 | 2013-06-27 12:59:27 | [diff] [blame] | 121 | if ('FIX'+'ME') in contents: |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 122 | errors.append(f.LocalPath()) |
| 123 | |
| 124 | if errors: |
| 125 | return [output_api.PresubmitError( |
kylechar | e68cc4a | 2017-04-05 18:01:05 | [diff] [blame] | 126 | 'All TODO comments should be of the form TODO(name/bug). ' + |
weiliangc | 941dbec | 2014-08-28 18:56:16 | [diff] [blame] | 127 | 'Use TODO instead of FIX' + 'ME', |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 128 | items=errors)] |
| 129 | return [] |
| 130 | |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 131 | def CheckDoubleAngles(input_api, output_api, white_list=CC_SOURCE_FILES, |
| 132 | black_list=None): |
| 133 | errors = [] |
| 134 | |
| 135 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 136 | white_list, |
| 137 | black_list) |
| 138 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 139 | contents = input_api.ReadFile(f, 'rb') |
| 140 | if ('> >') in contents: |
| 141 | errors.append(f.LocalPath()) |
| 142 | |
| 143 | if errors: |
| 144 | return [output_api.PresubmitError('Use >> instead of > >:', items=errors)] |
| 145 | return [] |
| 146 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 147 | def CheckUniquePtr(input_api, output_api, |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 148 | white_list=CC_SOURCE_FILES, black_list=None): |
| 149 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 150 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 151 | white_list, |
| 152 | black_list) |
| 153 | errors = [] |
| 154 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 155 | for line_number, line in f.ChangedContents(): |
| 156 | # Disallow: |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 157 | # return std::unique_ptr<T>(foo); |
| 158 | # bar = std::unique_ptr<T>(foo); |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 159 | # But allow: |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 160 | # return std::unique_ptr<T[]>(foo); |
| 161 | # bar = std::unique_ptr<T[]>(foo); |
| 162 | if re.search(r'(=|\breturn)\s*std::unique_ptr<.*?(?<!])>\([^)]+\)', line): |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 163 | errors.append(output_api.PresubmitError( |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 164 | ('%s:%d uses explicit std::unique_ptr constructor. ' + |
Jeremy Roman | 909d927b | 2017-08-27 18:34:09 | [diff] [blame] | 165 | 'Use std::make_unique<T>() instead.') % |
ricea | 87fe14eb | 2016-09-02 04:27:07 | [diff] [blame] | 166 | (f.LocalPath(), line_number))) |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 167 | # Disallow: |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 168 | # std::unique_ptr<T>() |
| 169 | if re.search(r'\bstd::unique_ptr<.*?>\(\)', line): |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 170 | errors.append(output_api.PresubmitError( |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 171 | '%s:%d uses std::unique_ptr<T>(). Use nullptr instead.' % |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 172 | (f.LocalPath(), line_number))) |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 173 | return errors |
| 174 | |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 175 | def FindUnquotedQuote(contents, pos): |
| 176 | match = re.search(r"(?<!\\)(?P<quote>\")", contents[pos:]) |
| 177 | return -1 if not match else match.start("quote") + pos |
| 178 | |
[email protected] | 3f52c893 | 2014-06-13 08:46:38 | [diff] [blame] | 179 | def FindUselessIfdefs(input_api, output_api): |
| 180 | errors = [] |
| 181 | source_file_filter = lambda x: x |
| 182 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 183 | contents = input_api.ReadFile(f, 'rb') |
| 184 | if re.search(r'#if\s*0\s', contents): |
| 185 | errors.append(f.LocalPath()) |
| 186 | if errors: |
| 187 | return [output_api.PresubmitError( |
| 188 | 'Don\'t use #if '+'0; just delete the code.', |
| 189 | items=errors)] |
| 190 | return [] |
| 191 | |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 192 | def FindNamespaceInBlock(pos, namespace, contents, whitelist=[]): |
| 193 | open_brace = -1 |
| 194 | close_brace = -1 |
| 195 | quote = -1 |
| 196 | name = -1 |
| 197 | brace_count = 1 |
| 198 | quote_count = 0 |
| 199 | while pos < len(contents) and brace_count > 0: |
| 200 | if open_brace < pos: open_brace = contents.find("{", pos) |
| 201 | if close_brace < pos: close_brace = contents.find("}", pos) |
| 202 | if quote < pos: quote = FindUnquotedQuote(contents, pos) |
| 203 | if name < pos: name = contents.find(("%s::" % namespace), pos) |
| 204 | |
| 205 | if name < 0: |
| 206 | return False # The namespace is not used at all. |
| 207 | if open_brace < 0: |
| 208 | open_brace = len(contents) |
| 209 | if close_brace < 0: |
| 210 | close_brace = len(contents) |
| 211 | if quote < 0: |
| 212 | quote = len(contents) |
| 213 | |
| 214 | next = min(open_brace, min(close_brace, min(quote, name))) |
| 215 | |
| 216 | if next == open_brace: |
| 217 | brace_count += 1 |
| 218 | elif next == close_brace: |
| 219 | brace_count -= 1 |
| 220 | elif next == quote: |
| 221 | quote_count = 0 if quote_count else 1 |
| 222 | elif next == name and not quote_count: |
| 223 | in_whitelist = False |
| 224 | for w in whitelist: |
| 225 | if re.match(w, contents[next:]): |
| 226 | in_whitelist = True |
| 227 | break |
| 228 | if not in_whitelist: |
| 229 | return True |
| 230 | pos = next + 1 |
| 231 | return False |
| 232 | |
| 233 | # Checks for the use of cc:: within the cc namespace, which is usually |
| 234 | # redundant. |
| 235 | def CheckNamespace(input_api, output_api): |
| 236 | errors = [] |
| 237 | |
| 238 | source_file_filter = lambda x: x |
| 239 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 240 | contents = input_api.ReadFile(f, 'rb') |
| 241 | match = re.search(r'namespace\s*cc\s*{', contents) |
| 242 | if match: |
vmpstr | a370ef5 | 2015-11-18 10:41:28 | [diff] [blame] | 243 | whitelist = [] |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 244 | if FindNamespaceInBlock(match.end(), 'cc', contents, whitelist=whitelist): |
| 245 | errors.append(f.LocalPath()) |
| 246 | |
| 247 | if errors: |
| 248 | return [output_api.PresubmitError( |
| 249 | 'Do not use cc:: inside of the cc namespace.', |
| 250 | items=errors)] |
| 251 | return [] |
| 252 | |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 253 | def CheckForUseOfWrongClock(input_api, |
| 254 | output_api, |
| 255 | white_list=CC_SOURCE_FILES, |
| 256 | black_list=None): |
| 257 | """Make sure new lines of code don't use a clock susceptible to skew.""" |
| 258 | black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST) |
| 259 | source_file_filter = lambda x: input_api.FilterSourceFile(x, |
| 260 | white_list, |
| 261 | black_list) |
| 262 | # Regular expression that should detect any explicit references to the |
| 263 | # base::Time type (or base::Clock/DefaultClock), whether in using decls, |
| 264 | # typedefs, or to call static methods. |
| 265 | base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)' |
| 266 | |
| 267 | # Regular expression that should detect references to the base::Time class |
| 268 | # members, such as a call to base::Time::Now. |
| 269 | base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::' |
| 270 | |
| 271 | # Regular expression to detect "using base::Time" declarations. We want to |
| 272 | # prevent these from triggerring a warning. For example, it's perfectly |
| 273 | # reasonable for code to be written like this: |
| 274 | # |
| 275 | # using base::Time; |
| 276 | # ... |
| 277 | # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond; |
| 278 | using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;' |
| 279 | |
| 280 | # Regular expression to detect references to the kXXX constants in the |
| 281 | # base::Time class. We want to prevent these from triggerring a warning. |
| 282 | base_time_konstant_pattern = r'(^|\W)Time::k\w+' |
| 283 | |
| 284 | problem_re = input_api.re.compile( |
| 285 | r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')') |
| 286 | exception_re = input_api.re.compile( |
| 287 | r'(' + using_base_time_decl_pattern + r')|(' + |
| 288 | base_time_konstant_pattern + r')') |
| 289 | problems = [] |
| 290 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 291 | for line_number, line in f.ChangedContents(): |
| 292 | if problem_re.search(line): |
| 293 | if not exception_re.search(line): |
| 294 | problems.append( |
| 295 | ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip())) |
| 296 | |
| 297 | if problems: |
| 298 | return [output_api.PresubmitPromptOrNotify( |
| 299 | 'You added one or more references to the base::Time class and/or one\n' |
| 300 | 'of its member functions (or base::Clock/DefaultClock). In cc code,\n' |
| 301 | 'it is most certainly incorrect! Instead use base::TimeTicks.\n\n' |
| 302 | '\n'.join(problems))] |
| 303 | else: |
| 304 | return [] |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 305 | |
Bo Liu | 67e76f0 | 2017-08-07 17:57:50 | [diff] [blame] | 306 | def CheckIpcUpdatedWithMojo(input_api, output_api): |
| 307 | """Make sure IPC is updated whenever Mojo serialization is updated""" |
| 308 | def match_ipc(affected_file): |
| 309 | match = re.match(r'.*_param_traits.*', affected_file.LocalPath()) |
| 310 | return match is not None |
| 311 | |
| 312 | def match_mojo(affected_file): |
| 313 | mojo_patterns = (r'.*_struct_traits.*', r'.*\.mojom$', r'.*\.typemap$') |
| 314 | matches = (re.match(pattern, affected_file.LocalPath()) |
| 315 | for pattern in mojo_patterns) |
| 316 | return any(matches) |
| 317 | |
| 318 | ipc_files = input_api.AffectedFiles(file_filter=match_ipc) |
| 319 | mojo_files = input_api.AffectedFiles(file_filter=match_mojo) |
| 320 | if mojo_files and not ipc_files: |
| 321 | return [output_api.PresubmitPromptOrNotify( |
| 322 | 'Make sure to update IPC ParamTraits along with mojo types.\n\n'),] |
| 323 | return [] |
| 324 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 325 | def CheckChangeOnUpload(input_api, output_api): |
| 326 | results = [] |
| 327 | results += CheckAsserts(input_api, output_api) |
[email protected] | 81d20544 | 2013-07-29 21:42:03 | [diff] [blame] | 328 | results += CheckStdAbs(input_api, output_api) |
[email protected] | 7add2e0b | 2013-04-23 05:16:42 | [diff] [blame] | 329 | results += CheckPassByValue(input_api, output_api) |
[email protected] | cf82459 | 2013-04-09 05:46:00 | [diff] [blame] | 330 | results += CheckChangeLintsClean(input_api, output_api) |
[email protected] | 6dc0b6f | 2013-06-26 20:21:59 | [diff] [blame] | 331 | results += CheckTodos(input_api, output_api) |
danakj | 6496cba | 2014-10-16 01:31:08 | [diff] [blame] | 332 | results += CheckDoubleAngles(input_api, output_api) |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 333 | results += CheckUniquePtr(input_api, output_api) |
[email protected] | e51444a | 2013-12-10 23:05:01 | [diff] [blame] | 334 | results += CheckNamespace(input_api, output_api) |
[email protected] | d2f1d58 | 2014-05-01 08:43:53 | [diff] [blame] | 335 | results += CheckForUseOfWrongClock(input_api, output_api) |
[email protected] | 3f52c893 | 2014-06-13 08:46:38 | [diff] [blame] | 336 | results += FindUselessIfdefs(input_api, output_api) |
Bo Liu | 67e76f0 | 2017-08-07 17:57:50 | [diff] [blame] | 337 | results += CheckIpcUpdatedWithMojo(input_api, output_api) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 338 | return results |
| 339 | |
mithro | cf788752 | 2015-05-21 07:26:32 | [diff] [blame] | 340 | def PostUploadHook(cl, change, output_api): |
| 341 | """git cl upload will call this hook after the issue is created/modified. |
| 342 | |
kbr | 4384263 | 2017-02-16 19:06:56 | [diff] [blame] | 343 | This hook adds an extra try bot list to the CL description in order to run |
Kenneth Russell | fbedc5d | 2017-11-16 20:53:21 | [diff] [blame] | 344 | Blink tests and additional GPU tests in addition to the CQ try bots. |
mithro | cf788752 | 2015-05-21 07:26:32 | [diff] [blame] | 345 | """ |
kbr | 4384263 | 2017-02-16 19:06:56 | [diff] [blame] | 346 | return output_api.EnsureCQIncludeTrybotsAreAdded( |
| 347 | cl, |
Kenneth Russell | fbedc5d | 2017-11-16 20:53:21 | [diff] [blame] | 348 | [ |
| 349 | 'master.tryserver.blink:linux_trusty_blink_rel', |
| 350 | 'master.tryserver.chromium.android:android_optional_gpu_tests_rel', |
| 351 | ], |
| 352 | 'Automatically added Blink and Android GPU trybots for CQ.') |