blob: cf9941e7e1e396632b3c7d6c0b406dda3cca124d [file] [log] [blame]
[email protected]a18130a2012-01-03 17:52:081# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ca8d1982009-02-19 16:33:122# 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 Chromium.
6
[email protected]f1293792009-07-31 18:09:567See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
[email protected]50d7d721e2009-11-15 17:56:188for more details about the presubmit API built into gcl.
[email protected]ca8d1982009-02-19 16:33:129"""
10
[email protected]eea609a2011-11-18 13:10:1211
[email protected]9d16ad12011-12-14 20:49:4712import re
[email protected]fbcafe5a2012-08-08 15:31:2213import subprocess
[email protected]55f9f382012-07-31 11:02:1814import sys
[email protected]9d16ad12011-12-14 20:49:4715
16
[email protected]379e7dd2010-01-28 17:39:2117_EXCLUDED_PATHS = (
[email protected]3e4eb112011-01-18 03:29:5418 r"^breakpad[\\\/].*",
[email protected]40d1dbb2012-10-26 07:18:0019 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
[email protected]8886ffcb2013-02-12 04:56:2821 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
[email protected]a18130a2012-01-03 17:52:0822 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
[email protected]3e4eb112011-01-18 03:29:5423 r"^skia[\\\/].*",
24 r"^v8[\\\/].*",
25 r".*MakeFile$",
[email protected]1084ccc2012-03-14 03:22:5326 r".+_autogen\.h$",
[email protected]ce145c02012-09-06 09:49:3427 r".+[\\\/]pnacl_shim\.c$",
[email protected]e07b6ac72013-08-20 00:30:4228 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
[email protected]4306417642009-06-11 00:33:4029)
[email protected]ca8d1982009-02-19 16:33:1230
[email protected]06e6d0ff2012-12-11 01:36:4431# Fragment of a regular expression that matches C++ and Objective-C++
32# implementation files.
33_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
34
35# Regular expression that matches code only used for test binaries
36# (best effort).
37_TEST_CODE_EXCLUDED_PATHS = (
38 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
39 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
[email protected]11e06082013-04-26 19:09:0340 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
[email protected]e2d7e6f2013-04-23 12:57:1241 _IMPLEMENTATION_EXTENSIONS,
[email protected]06e6d0ff2012-12-11 01:36:4442 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.*[/\\](test|tool(s)?)[/\\].*',
[email protected]ef070cc2013-05-03 11:53:0544 # content_shell is used for running layout tests.
45 r'content[/\\]shell[/\\].*',
[email protected]06e6d0ff2012-12-11 01:36:4446 # At request of folks maintaining this folder.
47 r'chrome[/\\]browser[/\\]automation[/\\].*',
48)
[email protected]ca8d1982009-02-19 16:33:1249
[email protected]eea609a2011-11-18 13:10:1250_TEST_ONLY_WARNING = (
51 'You might be calling functions intended only for testing from\n'
52 'production code. It is OK to ignore this warning if you know what\n'
53 'you are doing, as the heuristics used to detect the situation are\n'
54 'not perfect. The commit queue will not block on this warning.\n'
55 'Email [email protected] if you have questions.')
56
57
[email protected]cf9b78f2012-11-14 11:40:2858_INCLUDE_ORDER_WARNING = (
59 'Your #include order seems to be broken. Send mail to\n'
60 '[email protected] if this is not the case.')
61
62
[email protected]127f18ec2012-06-16 05:05:5963_BANNED_OBJC_FUNCTIONS = (
64 (
65 'addTrackingRect:',
[email protected]23e6cbc2012-06-16 18:51:2066 (
67 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
[email protected]127f18ec2012-06-16 05:05:5968 'prohibited. Please use CrTrackingArea instead.',
69 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
70 ),
71 False,
72 ),
73 (
74 'NSTrackingArea',
[email protected]23e6cbc2012-06-16 18:51:2075 (
76 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
[email protected]127f18ec2012-06-16 05:05:5977 'instead.',
78 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
79 ),
80 False,
81 ),
82 (
83 'convertPointFromBase:',
[email protected]23e6cbc2012-06-16 18:51:2084 (
85 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5986 'Please use |convertPoint:(point) fromView:nil| instead.',
87 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
88 ),
89 True,
90 ),
91 (
92 'convertPointToBase:',
[email protected]23e6cbc2012-06-16 18:51:2093 (
94 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5995 'Please use |convertPoint:(point) toView:nil| instead.',
96 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
97 ),
98 True,
99 ),
100 (
101 'convertRectFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20102 (
103 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59104 'Please use |convertRect:(point) fromView:nil| instead.',
105 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
106 ),
107 True,
108 ),
109 (
110 'convertRectToBase:',
[email protected]23e6cbc2012-06-16 18:51:20111 (
112 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59113 'Please use |convertRect:(point) toView:nil| instead.',
114 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
115 ),
116 True,
117 ),
118 (
119 'convertSizeFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20120 (
121 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59122 'Please use |convertSize:(point) fromView:nil| instead.',
123 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
124 ),
125 True,
126 ),
127 (
128 'convertSizeToBase:',
[email protected]23e6cbc2012-06-16 18:51:20129 (
130 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59131 'Please use |convertSize:(point) toView:nil| instead.',
132 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
133 ),
134 True,
135 ),
136)
137
138
139_BANNED_CPP_FUNCTIONS = (
[email protected]23e6cbc2012-06-16 18:51:20140 # Make sure that gtest's FRIEND_TEST() macro is not used; the
141 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
[email protected]e00ccc92012-11-01 17:32:30142 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
[email protected]23e6cbc2012-06-16 18:51:20143 (
144 'FRIEND_TEST(',
145 (
[email protected]e3c945502012-06-26 20:01:49146 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
[email protected]23e6cbc2012-06-16 18:51:20147 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
148 ),
149 False,
[email protected]7345da02012-11-27 14:31:49150 (),
[email protected]23e6cbc2012-06-16 18:51:20151 ),
152 (
153 'ScopedAllowIO',
154 (
[email protected]e3c945502012-06-26 20:01:49155 'New code should not use ScopedAllowIO. Post a task to the blocking',
156 'pool or the FILE thread instead.',
[email protected]23e6cbc2012-06-16 18:51:20157 ),
[email protected]e3c945502012-06-26 20:01:49158 True,
[email protected]7345da02012-11-27 14:31:49159 (
[email protected]de7d61ff2013-08-20 11:30:41160 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
161 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
[email protected]398ad132013-04-02 15:11:01162 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
[email protected]7345da02012-11-27 14:31:49163 ),
[email protected]23e6cbc2012-06-16 18:51:20164 ),
[email protected]52657f62013-05-20 05:30:31165 (
166 'SkRefPtr',
167 (
168 'The use of SkRefPtr is prohibited. ',
169 'Please use skia::RefPtr instead.'
170 ),
171 True,
172 (),
173 ),
174 (
175 'SkAutoRef',
176 (
177 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
178 'Please use skia::RefPtr instead.'
179 ),
180 True,
181 (),
182 ),
183 (
184 'SkAutoTUnref',
185 (
186 'The use of SkAutoTUnref is dangerous because it implicitly ',
187 'converts to a raw pointer. Please use skia::RefPtr instead.'
188 ),
189 True,
190 (),
191 ),
192 (
193 'SkAutoUnref',
194 (
195 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
196 'because it implicitly converts to a raw pointer. ',
197 'Please use skia::RefPtr instead.'
198 ),
199 True,
200 (),
201 ),
[email protected]127f18ec2012-06-16 05:05:59202)
203
204
[email protected]b00342e7f2013-03-26 16:21:54205_VALID_OS_MACROS = (
206 # Please keep sorted.
207 'OS_ANDROID',
208 'OS_BSD',
209 'OS_CAT', # For testing.
210 'OS_CHROMEOS',
211 'OS_FREEBSD',
212 'OS_IOS',
213 'OS_LINUX',
214 'OS_MACOSX',
215 'OS_NACL',
216 'OS_OPENBSD',
217 'OS_POSIX',
218 'OS_SOLARIS',
[email protected]b00342e7f2013-03-26 16:21:54219 'OS_WIN',
220)
221
222
[email protected]55459852011-08-10 15:17:19223def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
224 """Attempts to prevent use of functions intended only for testing in
225 non-testing code. For now this is just a best-effort implementation
226 that ignores header files and may have some false positives. A
227 better implementation would probably need a proper C++ parser.
228 """
229 # We only scan .cc files and the like, as the declaration of
230 # for-testing functions in header files are hard to distinguish from
231 # calls to such functions without a proper C++ parser.
[email protected]06e6d0ff2012-12-11 01:36:44232 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
[email protected]55459852011-08-10 15:17:19233
234 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
235 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
[email protected]de4f7d22013-05-23 14:27:46236 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
[email protected]55459852011-08-10 15:17:19237 exclusion_pattern = input_api.re.compile(
238 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
239 base_function_pattern, base_function_pattern))
240
241 def FilterFile(affected_file):
[email protected]06e6d0ff2012-12-11 01:36:44242 black_list = (_EXCLUDED_PATHS +
243 _TEST_CODE_EXCLUDED_PATHS +
244 input_api.DEFAULT_BLACK_LIST)
[email protected]55459852011-08-10 15:17:19245 return input_api.FilterSourceFile(
246 affected_file,
247 white_list=(file_inclusion_pattern, ),
248 black_list=black_list)
249
250 problems = []
251 for f in input_api.AffectedSourceFiles(FilterFile):
252 local_path = f.LocalPath()
[email protected]2fdd1f362013-01-16 03:56:03253 lines = input_api.ReadFile(f).splitlines()
254 line_number = 0
255 for line in lines:
256 if (inclusion_pattern.search(line) and
[email protected]de4f7d22013-05-23 14:27:46257 not comment_pattern.search(line) and
[email protected]2fdd1f362013-01-16 03:56:03258 not exclusion_pattern.search(line)):
[email protected]55459852011-08-10 15:17:19259 problems.append(
[email protected]2fdd1f362013-01-16 03:56:03260 '%s:%d\n %s' % (local_path, line_number, line.strip()))
261 line_number += 1
[email protected]55459852011-08-10 15:17:19262
263 if problems:
[email protected]f7051d52013-04-02 18:31:42264 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
[email protected]2fdd1f362013-01-16 03:56:03265 else:
266 return []
[email protected]55459852011-08-10 15:17:19267
268
[email protected]10689ca2011-09-02 02:31:54269def _CheckNoIOStreamInHeaders(input_api, output_api):
270 """Checks to make sure no .h files include <iostream>."""
271 files = []
272 pattern = input_api.re.compile(r'^#include\s*<iostream>',
273 input_api.re.MULTILINE)
274 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
275 if not f.LocalPath().endswith('.h'):
276 continue
277 contents = input_api.ReadFile(f)
278 if pattern.search(contents):
279 files.append(f)
280
281 if len(files):
282 return [ output_api.PresubmitError(
[email protected]6c063c62012-07-11 19:11:06283 'Do not #include <iostream> in header files, since it inserts static '
284 'initialization into every file including the header. Instead, '
[email protected]10689ca2011-09-02 02:31:54285 '#include <ostream>. See http://crbug.com/94794',
286 files) ]
287 return []
288
289
[email protected]72df4e782012-06-21 16:28:18290def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
291 """Checks to make sure no source files use UNIT_TEST"""
292 problems = []
293 for f in input_api.AffectedFiles():
294 if (not f.LocalPath().endswith(('.cc', '.mm'))):
295 continue
296
297 for line_num, line in f.ChangedContents():
298 if 'UNIT_TEST' in line:
299 problems.append(' %s:%d' % (f.LocalPath(), line_num))
300
301 if not problems:
302 return []
303 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
304 '\n'.join(problems))]
305
306
[email protected]8ea5d4b2011-09-13 21:49:22307def _CheckNoNewWStrings(input_api, output_api):
308 """Checks to make sure we don't introduce use of wstrings."""
[email protected]55463aa62011-10-12 00:48:27309 problems = []
[email protected]8ea5d4b2011-09-13 21:49:22310 for f in input_api.AffectedFiles():
[email protected]b5c24292011-11-28 14:38:20311 if (not f.LocalPath().endswith(('.cc', '.h')) or
[email protected]24be83c2013-08-29 23:01:23312 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
[email protected]b5c24292011-11-28 14:38:20313 continue
[email protected]8ea5d4b2011-09-13 21:49:22314
[email protected]a11dbe9b2012-08-07 01:32:58315 allowWString = False
[email protected]b5c24292011-11-28 14:38:20316 for line_num, line in f.ChangedContents():
[email protected]a11dbe9b2012-08-07 01:32:58317 if 'presubmit: allow wstring' in line:
318 allowWString = True
319 elif not allowWString and 'wstring' in line:
[email protected]55463aa62011-10-12 00:48:27320 problems.append(' %s:%d' % (f.LocalPath(), line_num))
[email protected]a11dbe9b2012-08-07 01:32:58321 allowWString = False
322 else:
323 allowWString = False
[email protected]8ea5d4b2011-09-13 21:49:22324
[email protected]55463aa62011-10-12 00:48:27325 if not problems:
326 return []
327 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
[email protected]a11dbe9b2012-08-07 01:32:58328 ' If you are calling a cross-platform API that accepts a wstring, '
329 'fix the API.\n' +
[email protected]55463aa62011-10-12 00:48:27330 '\n'.join(problems))]
[email protected]8ea5d4b2011-09-13 21:49:22331
332
[email protected]2a8ac9c2011-10-19 17:20:44333def _CheckNoDEPSGIT(input_api, output_api):
334 """Make sure .DEPS.git is never modified manually."""
335 if any(f.LocalPath().endswith('.DEPS.git') for f in
336 input_api.AffectedFiles()):
337 return [output_api.PresubmitError(
338 'Never commit changes to .DEPS.git. This file is maintained by an\n'
339 'automated system based on what\'s in DEPS and your changes will be\n'
340 'overwritten.\n'
341 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
342 'for more information')]
343 return []
344
345
[email protected]127f18ec2012-06-16 05:05:59346def _CheckNoBannedFunctions(input_api, output_api):
347 """Make sure that banned functions are not used."""
348 warnings = []
349 errors = []
350
351 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
352 for f in input_api.AffectedFiles(file_filter=file_filter):
353 for line_num, line in f.ChangedContents():
354 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
355 if func_name in line:
356 problems = warnings;
357 if error:
358 problems = errors;
359 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
360 for message_line in message:
361 problems.append(' %s' % message_line)
362
363 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
364 for f in input_api.AffectedFiles(file_filter=file_filter):
365 for line_num, line in f.ChangedContents():
[email protected]7345da02012-11-27 14:31:49366 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
367 def IsBlacklisted(affected_file, blacklist):
368 local_path = affected_file.LocalPath()
369 for item in blacklist:
370 if input_api.re.match(item, local_path):
371 return True
372 return False
373 if IsBlacklisted(f, excluded_paths):
374 continue
[email protected]127f18ec2012-06-16 05:05:59375 if func_name in line:
376 problems = warnings;
377 if error:
378 problems = errors;
379 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
380 for message_line in message:
381 problems.append(' %s' % message_line)
382
383 result = []
384 if (warnings):
385 result.append(output_api.PresubmitPromptWarning(
386 'Banned functions were used.\n' + '\n'.join(warnings)))
387 if (errors):
388 result.append(output_api.PresubmitError(
389 'Banned functions were used.\n' + '\n'.join(errors)))
390 return result
391
392
[email protected]6c063c62012-07-11 19:11:06393def _CheckNoPragmaOnce(input_api, output_api):
394 """Make sure that banned functions are not used."""
395 files = []
396 pattern = input_api.re.compile(r'^#pragma\s+once',
397 input_api.re.MULTILINE)
398 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
399 if not f.LocalPath().endswith('.h'):
400 continue
401 contents = input_api.ReadFile(f)
402 if pattern.search(contents):
403 files.append(f)
404
405 if files:
406 return [output_api.PresubmitError(
407 'Do not use #pragma once in header files.\n'
408 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
409 files)]
410 return []
411
[email protected]127f18ec2012-06-16 05:05:59412
[email protected]e7479052012-09-19 00:26:12413def _CheckNoTrinaryTrueFalse(input_api, output_api):
414 """Checks to make sure we don't introduce use of foo ? true : false."""
415 problems = []
416 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
417 for f in input_api.AffectedFiles():
418 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
419 continue
420
421 for line_num, line in f.ChangedContents():
422 if pattern.match(line):
423 problems.append(' %s:%d' % (f.LocalPath(), line_num))
424
425 if not problems:
426 return []
427 return [output_api.PresubmitPromptWarning(
428 'Please consider avoiding the "? true : false" pattern if possible.\n' +
429 '\n'.join(problems))]
430
431
[email protected]55f9f382012-07-31 11:02:18432def _CheckUnwantedDependencies(input_api, output_api):
433 """Runs checkdeps on #include statements added in this
434 change. Breaking - rules is an error, breaking ! rules is a
435 warning.
436 """
437 # We need to wait until we have an input_api object and use this
438 # roundabout construct to import checkdeps because this file is
439 # eval-ed and thus doesn't have __file__.
440 original_sys_path = sys.path
441 try:
442 sys.path = sys.path + [input_api.os_path.join(
443 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
444 import checkdeps
445 from cpp_checker import CppChecker
446 from rules import Rule
447 finally:
448 # Restore sys.path to what it was before.
449 sys.path = original_sys_path
450
451 added_includes = []
452 for f in input_api.AffectedFiles():
453 if not CppChecker.IsCppFile(f.LocalPath()):
454 continue
455
456 changed_lines = [line for line_num, line in f.ChangedContents()]
457 added_includes.append([f.LocalPath(), changed_lines])
458
[email protected]26385172013-05-09 23:11:35459 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
[email protected]55f9f382012-07-31 11:02:18460
461 error_descriptions = []
462 warning_descriptions = []
463 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
464 added_includes):
465 description_with_path = '%s\n %s' % (path, rule_description)
466 if rule_type == Rule.DISALLOW:
467 error_descriptions.append(description_with_path)
468 else:
469 warning_descriptions.append(description_with_path)
470
471 results = []
472 if error_descriptions:
473 results.append(output_api.PresubmitError(
474 'You added one or more #includes that violate checkdeps rules.',
475 error_descriptions))
476 if warning_descriptions:
[email protected]f7051d52013-04-02 18:31:42477 results.append(output_api.PresubmitPromptOrNotify(
[email protected]55f9f382012-07-31 11:02:18478 'You added one or more #includes of files that are temporarily\n'
479 'allowed but being removed. Can you avoid introducing the\n'
480 '#include? See relevant DEPS file(s) for details and contacts.',
481 warning_descriptions))
482 return results
483
484
[email protected]fbcafe5a2012-08-08 15:31:22485def _CheckFilePermissions(input_api, output_api):
486 """Check that all files have their permissions properly set."""
487 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
488 input_api.change.RepositoryRoot()]
489 for f in input_api.AffectedFiles():
490 args += ['--file', f.LocalPath()]
491 errors = []
492 (errors, stderrdata) = subprocess.Popen(args).communicate()
493
494 results = []
495 if errors:
[email protected]c8278b32012-10-30 20:35:49496 results.append(output_api.PresubmitError('checkperms.py failed.',
[email protected]fbcafe5a2012-08-08 15:31:22497 errors))
498 return results
499
500
[email protected]c8278b32012-10-30 20:35:49501def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
502 """Makes sure we don't include ui/aura/window_property.h
503 in header files.
504 """
505 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
506 errors = []
507 for f in input_api.AffectedFiles():
508 if not f.LocalPath().endswith('.h'):
509 continue
510 for line_num, line in f.ChangedContents():
511 if pattern.match(line):
512 errors.append(' %s:%d' % (f.LocalPath(), line_num))
513
514 results = []
515 if errors:
516 results.append(output_api.PresubmitError(
517 'Header files should not include ui/aura/window_property.h', errors))
518 return results
519
520
[email protected]cf9b78f2012-11-14 11:40:28521def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
522 """Checks that the lines in scope occur in the right order.
523
524 1. C system files in alphabetical order
525 2. C++ system files in alphabetical order
526 3. Project's .h files
527 """
528
529 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
530 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
531 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
532
533 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
534
535 state = C_SYSTEM_INCLUDES
536
537 previous_line = ''
[email protected]728b9bb2012-11-14 20:38:57538 previous_line_num = 0
[email protected]cf9b78f2012-11-14 11:40:28539 problem_linenums = []
540 for line_num, line in scope:
541 if c_system_include_pattern.match(line):
542 if state != C_SYSTEM_INCLUDES:
[email protected]728b9bb2012-11-14 20:38:57543 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28544 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57545 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28546 elif cpp_system_include_pattern.match(line):
547 if state == C_SYSTEM_INCLUDES:
548 state = CPP_SYSTEM_INCLUDES
549 elif state == CUSTOM_INCLUDES:
[email protected]728b9bb2012-11-14 20:38:57550 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28551 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57552 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28553 elif custom_include_pattern.match(line):
554 if state != CUSTOM_INCLUDES:
555 state = CUSTOM_INCLUDES
556 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57557 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28558 else:
559 problem_linenums.append(line_num)
560 previous_line = line
[email protected]728b9bb2012-11-14 20:38:57561 previous_line_num = line_num
[email protected]cf9b78f2012-11-14 11:40:28562
563 warnings = []
[email protected]728b9bb2012-11-14 20:38:57564 for (line_num, previous_line_num) in problem_linenums:
565 if line_num in changed_linenums or previous_line_num in changed_linenums:
[email protected]cf9b78f2012-11-14 11:40:28566 warnings.append(' %s:%d' % (file_path, line_num))
567 return warnings
568
569
[email protected]ac294a12012-12-06 16:38:43570def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
[email protected]cf9b78f2012-11-14 11:40:28571 """Checks the #include order for the given file f."""
572
[email protected]2299dcf2012-11-15 19:56:24573 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
[email protected]23093b62013-09-20 12:16:30574 # Exclude the following includes from the check:
575 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
576 # specific order.
577 # 2) <atlbase.h>, "build/build_config.h"
578 excluded_include_pattern = input_api.re.compile(
579 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
[email protected]2299dcf2012-11-15 19:56:24580 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
[email protected]0e5c1852012-12-18 20:17:11581 if_pattern = input_api.re.compile(
582 r'\s*#\s*(if|elif|else|endif|define|undef).*')
583 # Some files need specialized order of includes; exclude such files from this
584 # check.
585 uncheckable_includes_pattern = input_api.re.compile(
586 r'\s*#include '
587 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
[email protected]cf9b78f2012-11-14 11:40:28588
589 contents = f.NewContents()
590 warnings = []
591 line_num = 0
592
[email protected]ac294a12012-12-06 16:38:43593 # Handle the special first include. If the first include file is
594 # some/path/file.h, the corresponding including file can be some/path/file.cc,
595 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
596 # etc. It's also possible that no special first include exists.
597 for line in contents:
598 line_num += 1
599 if system_include_pattern.match(line):
600 # No special first include -> process the line again along with normal
601 # includes.
602 line_num -= 1
603 break
604 match = custom_include_pattern.match(line)
605 if match:
606 match_dict = match.groupdict()
607 header_basename = input_api.os_path.basename(
608 match_dict['FILE']).replace('.h', '')
609 if header_basename not in input_api.os_path.basename(f.LocalPath()):
[email protected]2299dcf2012-11-15 19:56:24610 # No special first include -> process the line again along with normal
611 # includes.
612 line_num -= 1
[email protected]ac294a12012-12-06 16:38:43613 break
[email protected]cf9b78f2012-11-14 11:40:28614
615 # Split into scopes: Each region between #if and #endif is its own scope.
616 scopes = []
617 current_scope = []
618 for line in contents[line_num:]:
619 line_num += 1
[email protected]0e5c1852012-12-18 20:17:11620 if uncheckable_includes_pattern.match(line):
621 return []
[email protected]2309b0fa02012-11-16 12:18:27622 if if_pattern.match(line):
[email protected]cf9b78f2012-11-14 11:40:28623 scopes.append(current_scope)
624 current_scope = []
[email protected]962f117e2012-11-22 18:11:56625 elif ((system_include_pattern.match(line) or
626 custom_include_pattern.match(line)) and
627 not excluded_include_pattern.match(line)):
[email protected]cf9b78f2012-11-14 11:40:28628 current_scope.append((line_num, line))
629 scopes.append(current_scope)
630
631 for scope in scopes:
632 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
633 changed_linenums))
634 return warnings
635
636
637def _CheckIncludeOrder(input_api, output_api):
638 """Checks that the #include order is correct.
639
640 1. The corresponding header for source files.
641 2. C system files in alphabetical order
642 3. C++ system files in alphabetical order
643 4. Project's .h files in alphabetical order
644
[email protected]ac294a12012-12-06 16:38:43645 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
646 these rules separately.
[email protected]cf9b78f2012-11-14 11:40:28647 """
648
649 warnings = []
650 for f in input_api.AffectedFiles():
[email protected]ac294a12012-12-06 16:38:43651 if f.LocalPath().endswith(('.cc', '.h')):
652 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
653 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
[email protected]cf9b78f2012-11-14 11:40:28654
655 results = []
656 if warnings:
[email protected]f7051d52013-04-02 18:31:42657 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
[email protected]120cf540d2012-12-10 17:55:53658 warnings))
[email protected]cf9b78f2012-11-14 11:40:28659 return results
660
661
[email protected]70ca77752012-11-20 03:45:03662def _CheckForVersionControlConflictsInFile(input_api, f):
663 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
664 errors = []
665 for line_num, line in f.ChangedContents():
666 if pattern.match(line):
667 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
668 return errors
669
670
671def _CheckForVersionControlConflicts(input_api, output_api):
672 """Usually this is not intentional and will cause a compile failure."""
673 errors = []
674 for f in input_api.AffectedFiles():
675 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
676
677 results = []
678 if errors:
679 results.append(output_api.PresubmitError(
680 'Version control conflict markers found, please resolve.', errors))
681 return results
682
683
[email protected]06e6d0ff2012-12-11 01:36:44684def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
685 def FilterFile(affected_file):
686 """Filter function for use with input_api.AffectedSourceFiles,
687 below. This filters out everything except non-test files from
688 top-level directories that generally speaking should not hard-code
689 service URLs (e.g. src/android_webview/, src/content/ and others).
690 """
691 return input_api.FilterSourceFile(
692 affected_file,
[email protected]78bb39d62012-12-11 15:11:56693 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
[email protected]06e6d0ff2012-12-11 01:36:44694 black_list=(_EXCLUDED_PATHS +
695 _TEST_CODE_EXCLUDED_PATHS +
696 input_api.DEFAULT_BLACK_LIST))
697
[email protected]de4f7d22013-05-23 14:27:46698 base_pattern = '"[^"]*google\.com[^"]*"'
699 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
700 pattern = input_api.re.compile(base_pattern)
[email protected]06e6d0ff2012-12-11 01:36:44701 problems = [] # items are (filename, line_number, line)
702 for f in input_api.AffectedSourceFiles(FilterFile):
703 for line_num, line in f.ChangedContents():
[email protected]de4f7d22013-05-23 14:27:46704 if not comment_pattern.search(line) and pattern.search(line):
[email protected]06e6d0ff2012-12-11 01:36:44705 problems.append((f.LocalPath(), line_num, line))
706
707 if problems:
[email protected]f7051d52013-04-02 18:31:42708 return [output_api.PresubmitPromptOrNotify(
[email protected]06e6d0ff2012-12-11 01:36:44709 'Most layers below src/chrome/ should not hardcode service URLs.\n'
710 'Are you sure this is correct? (Contact: [email protected])',
711 [' %s:%d: %s' % (
712 problem[0], problem[1], problem[2]) for problem in problems])]
[email protected]2fdd1f362013-01-16 03:56:03713 else:
714 return []
[email protected]06e6d0ff2012-12-11 01:36:44715
716
[email protected]d2530012013-01-25 16:39:27717def _CheckNoAbbreviationInPngFileName(input_api, output_api):
718 """Makes sure there are no abbreviations in the name of PNG files.
719 """
[email protected]4053a48e2013-01-25 21:43:04720 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
[email protected]d2530012013-01-25 16:39:27721 errors = []
722 for f in input_api.AffectedFiles(include_deletes=False):
723 if pattern.match(f.LocalPath()):
724 errors.append(' %s' % f.LocalPath())
725
726 results = []
727 if errors:
728 results.append(output_api.PresubmitError(
729 'The name of PNG files should not have abbreviations. \n'
730 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
731 'Contact [email protected] if you have questions.', errors))
732 return results
733
734
[email protected]f32e2d1e2013-07-26 21:39:08735def _DepsFilesToCheck(re, changed_lines):
736 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
737 a set of DEPS entries that we should look up."""
738 results = set()
739
740 # This pattern grabs the path without basename in the first
741 # parentheses, and the basename (if present) in the second. It
742 # relies on the simple heuristic that if there is a basename it will
743 # be a header file ending in ".h".
744 pattern = re.compile(
745 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
746 for changed_line in changed_lines:
747 m = pattern.match(changed_line)
748 if m:
749 path = m.group(1)
750 if not (path.startswith('grit/') or path == 'grit'):
751 results.add('%s/DEPS' % m.group(1))
752 return results
753
754
[email protected]e871964c2013-05-13 14:14:55755def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
756 """When a dependency prefixed with + is added to a DEPS file, we
757 want to make sure that the change is reviewed by an OWNER of the
758 target file or directory, to avoid layering violations from being
759 introduced. This check verifies that this happens.
760 """
761 changed_lines = set()
762 for f in input_api.AffectedFiles():
763 filename = input_api.os_path.basename(f.LocalPath())
764 if filename == 'DEPS':
765 changed_lines |= set(line.strip()
766 for line_num, line
767 in f.ChangedContents())
768 if not changed_lines:
769 return []
770
[email protected]f32e2d1e2013-07-26 21:39:08771 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
[email protected]e871964c2013-05-13 14:14:55772 if not virtual_depended_on_files:
773 return []
774
775 if input_api.is_committing:
776 if input_api.tbr:
777 return [output_api.PresubmitNotifyResult(
778 '--tbr was specified, skipping OWNERS check for DEPS additions')]
779 if not input_api.change.issue:
780 return [output_api.PresubmitError(
781 "DEPS approval by OWNERS check failed: this change has "
782 "no Rietveld issue number, so we can't check it for approvals.")]
783 output = output_api.PresubmitError
784 else:
785 output = output_api.PresubmitNotifyResult
786
787 owners_db = input_api.owners_db
788 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
789 input_api,
790 owners_db.email_regexp,
791 approval_needed=input_api.is_committing)
792
793 owner_email = owner_email or input_api.change.author_email
794
[email protected]de4f7d22013-05-23 14:27:46795 reviewers_plus_owner = set(reviewers)
[email protected]e71c6082013-05-22 02:28:51796 if owner_email:
[email protected]de4f7d22013-05-23 14:27:46797 reviewers_plus_owner.add(owner_email)
[email protected]e871964c2013-05-13 14:14:55798 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
799 reviewers_plus_owner)
800 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
801 for path in missing_files]
802
803 if unapproved_dependencies:
804 output_list = [
805 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
806 '\n '.join(sorted(unapproved_dependencies)))]
807 if not input_api.is_committing:
808 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
809 output_list.append(output(
810 'Suggested missing target path OWNERS:\n %s' %
811 '\n '.join(suggested_owners or [])))
812 return output_list
813
814 return []
815
816
[email protected]22c9bd72011-03-27 16:47:39817def _CommonChecks(input_api, output_api):
818 """Checks common to both upload and commit."""
819 results = []
820 results.extend(input_api.canned_checks.PanProjectChecks(
821 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
[email protected]66daa702011-05-28 14:41:46822 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
[email protected]55459852011-08-10 15:17:19823 results.extend(
824 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
[email protected]10689ca2011-09-02 02:31:54825 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
[email protected]72df4e782012-06-21 16:28:18826 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
[email protected]8ea5d4b2011-09-13 21:49:22827 results.extend(_CheckNoNewWStrings(input_api, output_api))
[email protected]2a8ac9c2011-10-19 17:20:44828 results.extend(_CheckNoDEPSGIT(input_api, output_api))
[email protected]127f18ec2012-06-16 05:05:59829 results.extend(_CheckNoBannedFunctions(input_api, output_api))
[email protected]6c063c62012-07-11 19:11:06830 results.extend(_CheckNoPragmaOnce(input_api, output_api))
[email protected]e7479052012-09-19 00:26:12831 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
[email protected]55f9f382012-07-31 11:02:18832 results.extend(_CheckUnwantedDependencies(input_api, output_api))
[email protected]fbcafe5a2012-08-08 15:31:22833 results.extend(_CheckFilePermissions(input_api, output_api))
[email protected]c8278b32012-10-30 20:35:49834 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
[email protected]2309b0fa02012-11-16 12:18:27835 results.extend(_CheckIncludeOrder(input_api, output_api))
[email protected]70ca77752012-11-20 03:45:03836 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
[email protected]b8079ae4a2012-12-05 19:56:49837 results.extend(_CheckPatchFiles(input_api, output_api))
[email protected]06e6d0ff2012-12-11 01:36:44838 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
[email protected]d2530012013-01-25 16:39:27839 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
[email protected]b00342e7f2013-03-26 16:21:54840 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
[email protected]e871964c2013-05-13 14:14:55841 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
[email protected]9f919cc2013-07-31 03:04:04842 results.extend(
843 input_api.canned_checks.CheckChangeHasNoTabs(
844 input_api,
845 output_api,
846 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
[email protected]2299dcf2012-11-15 19:56:24847
848 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
849 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
850 input_api, output_api,
851 input_api.PresubmitLocalPath(),
[email protected]6be63382013-01-21 15:42:38852 whitelist=[r'^PRESUBMIT_test\.py$']))
[email protected]22c9bd72011-03-27 16:47:39853 return results
[email protected]1f7b4172010-01-28 01:17:34854
[email protected]b337cb5b2011-01-23 21:24:05855
856def _CheckSubversionConfig(input_api, output_api):
857 """Verifies the subversion config file is correctly setup.
858
859 Checks that autoprops are enabled, returns an error otherwise.
860 """
861 join = input_api.os_path.join
862 if input_api.platform == 'win32':
863 appdata = input_api.environ.get('APPDATA', '')
864 if not appdata:
865 return [output_api.PresubmitError('%APPDATA% is not configured.')]
866 path = join(appdata, 'Subversion', 'config')
867 else:
868 home = input_api.environ.get('HOME', '')
869 if not home:
870 return [output_api.PresubmitError('$HOME is not configured.')]
871 path = join(home, '.subversion', 'config')
872
873 error_msg = (
874 'Please look at http://dev.chromium.org/developers/coding-style to\n'
875 'configure your subversion configuration file. This enables automatic\n'
[email protected]c6a3c10b2011-01-24 16:14:20876 'properties to simplify the project maintenance.\n'
877 'Pro-tip: just download and install\n'
878 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
[email protected]b337cb5b2011-01-23 21:24:05879
880 try:
881 lines = open(path, 'r').read().splitlines()
882 # Make sure auto-props is enabled and check for 2 Chromium standard
883 # auto-prop.
884 if (not '*.cc = svn:eol-style=LF' in lines or
885 not '*.pdf = svn:mime-type=application/pdf' in lines or
886 not 'enable-auto-props = yes' in lines):
887 return [
[email protected]79ed7e62011-02-21 21:08:53888 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05889 'It looks like you have not configured your subversion config '
[email protected]b5359c02011-02-01 20:29:56890 'file or it is not up-to-date.\n' + error_msg)
[email protected]b337cb5b2011-01-23 21:24:05891 ]
892 except (OSError, IOError):
893 return [
[email protected]79ed7e62011-02-21 21:08:53894 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05895 'Can\'t find your subversion config file.\n' + error_msg)
896 ]
897 return []
898
899
[email protected]66daa702011-05-28 14:41:46900def _CheckAuthorizedAuthor(input_api, output_api):
901 """For non-googler/chromites committers, verify the author's email address is
902 in AUTHORS.
903 """
[email protected]9bb9cb82011-06-13 20:43:01904 # TODO(maruel): Add it to input_api?
905 import fnmatch
906
[email protected]66daa702011-05-28 14:41:46907 author = input_api.change.author_email
[email protected]9bb9cb82011-06-13 20:43:01908 if not author:
909 input_api.logging.info('No author, skipping AUTHOR check')
[email protected]66daa702011-05-28 14:41:46910 return []
[email protected]c99663292011-05-31 19:46:08911 authors_path = input_api.os_path.join(
[email protected]66daa702011-05-28 14:41:46912 input_api.PresubmitLocalPath(), 'AUTHORS')
913 valid_authors = (
914 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
915 for line in open(authors_path))
[email protected]ac54b132011-06-06 18:11:18916 valid_authors = [item.group(1).lower() for item in valid_authors if item]
[email protected]d8b50be2011-06-15 14:19:44917 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
[email protected]5861efb2013-01-07 18:33:23918 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
[email protected]66daa702011-05-28 14:41:46919 return [output_api.PresubmitPromptWarning(
920 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
921 '\n'
922 'http://www.chromium.org/developers/contributing-code and read the '
923 '"Legal" section\n'
924 'If you are a chromite, verify the contributor signed the CLA.') %
925 author)]
926 return []
927
928
[email protected]b8079ae4a2012-12-05 19:56:49929def _CheckPatchFiles(input_api, output_api):
930 problems = [f.LocalPath() for f in input_api.AffectedFiles()
931 if f.LocalPath().endswith(('.orig', '.rej'))]
932 if problems:
933 return [output_api.PresubmitError(
934 "Don't commit .rej and .orig files.", problems)]
[email protected]2fdd1f362013-01-16 03:56:03935 else:
936 return []
[email protected]b8079ae4a2012-12-05 19:56:49937
938
[email protected]b00342e7f2013-03-26 16:21:54939def _DidYouMeanOSMacro(bad_macro):
940 try:
941 return {'A': 'OS_ANDROID',
942 'B': 'OS_BSD',
943 'C': 'OS_CHROMEOS',
944 'F': 'OS_FREEBSD',
945 'L': 'OS_LINUX',
946 'M': 'OS_MACOSX',
947 'N': 'OS_NACL',
948 'O': 'OS_OPENBSD',
949 'P': 'OS_POSIX',
950 'S': 'OS_SOLARIS',
951 'W': 'OS_WIN'}[bad_macro[3].upper()]
952 except KeyError:
953 return ''
954
955
956def _CheckForInvalidOSMacrosInFile(input_api, f):
957 """Check for sensible looking, totally invalid OS macros."""
958 preprocessor_statement = input_api.re.compile(r'^\s*#')
959 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
960 results = []
961 for lnum, line in f.ChangedContents():
962 if preprocessor_statement.search(line):
963 for match in os_macro.finditer(line):
964 if not match.group(1) in _VALID_OS_MACROS:
965 good = _DidYouMeanOSMacro(match.group(1))
966 did_you_mean = ' (did you mean %s?)' % good if good else ''
967 results.append(' %s:%d %s%s' % (f.LocalPath(),
968 lnum,
969 match.group(1),
970 did_you_mean))
971 return results
972
973
974def _CheckForInvalidOSMacros(input_api, output_api):
975 """Check all affected files for invalid OS macros."""
976 bad_macros = []
977 for f in input_api.AffectedFiles():
978 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
979 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
980
981 if not bad_macros:
982 return []
983
984 return [output_api.PresubmitError(
985 'Possibly invalid OS macro[s] found. Please fix your code\n'
986 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
987
988
[email protected]1f7b4172010-01-28 01:17:34989def CheckChangeOnUpload(input_api, output_api):
990 results = []
991 results.extend(_CommonChecks(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:54992 return results
[email protected]ca8d1982009-02-19 16:33:12993
994
995def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:54996 results = []
[email protected]1f7b4172010-01-28 01:17:34997 results.extend(_CommonChecks(input_api, output_api))
[email protected]dd805fe2009-10-01 08:11:51998 # TODO(thestig) temporarily disabled, doesn't work in third_party/
999 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1000 # input_api, output_api, sources))
[email protected]fe5f57c52009-06-05 14:25:541001 # Make sure the tree is 'open'.
[email protected]806e98e2010-03-19 17:49:271002 results.extend(input_api.canned_checks.CheckTreeIsOpen(
[email protected]7f238152009-08-12 19:00:341003 input_api,
1004 output_api,
[email protected]2fdd1f362013-01-16 03:56:031005 json_url='http://chromium-status.appspot.com/current?format=json'))
[email protected]806e98e2010-03-19 17:49:271006 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
[email protected]2fdd1f362013-01-16 03:56:031007 output_api, 'http://codereview.chromium.org',
[email protected]c1ba4c52012-03-09 14:23:281008 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1009 '[email protected]'))
[email protected]806e98e2010-03-19 17:49:271010
[email protected]3e4eb112011-01-18 03:29:541011 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1012 input_api, output_api))
[email protected]c4b47562011-12-05 23:39:411013 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1014 input_api, output_api))
[email protected]b337cb5b2011-01-23 21:24:051015 results.extend(_CheckSubversionConfig(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541016 return results
[email protected]ca8d1982009-02-19 16:33:121017
1018
[email protected]5efb2a822011-09-27 23:06:131019def GetPreferredTrySlaves(project, change):
[email protected]4ce995ea2012-06-27 02:13:101020 files = change.LocalPaths()
1021
[email protected]751b05f2013-01-10 23:12:171022 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
[email protected]3019c902012-06-29 00:09:031023 return []
1024
[email protected]d668899a2012-09-06 18:16:591025 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
[email protected]5753cff2013-07-19 23:57:521026 return ['mac_rel', 'mac:compile']
[email protected]d668899a2012-09-06 18:16:591027 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
[email protected]7fab6202013-02-21 17:54:351028 return ['win_rel', 'win7_aura', 'win:compile']
[email protected]d668899a2012-09-06 18:16:591029 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
[email protected]d95948ef2013-07-02 10:51:001030 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
[email protected]356aa542012-09-19 23:31:291031 if all(re.search('^native_client_sdk', f) for f in files):
1032 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
[email protected]de142152012-10-03 23:02:451033 if all(re.search('[/_]ios[/_.]', f) for f in files):
1034 return ['ios_rel_device', 'ios_dbg_simulator']
[email protected]4ce995ea2012-06-27 02:13:101035
[email protected]3e2f0402012-11-02 16:28:011036 trybots = [
1037 'android_clang_dbg',
1038 'android_dbg',
1039 'ios_dbg_simulator',
1040 'ios_rel_device',
1041 'linux_asan',
[email protected]95c989162012-11-29 05:58:251042 'linux_aura',
[email protected]3e2f0402012-11-02 16:28:011043 'linux_chromeos',
1044 'linux_clang:compile',
1045 'linux_rel',
[email protected]3e2f0402012-11-02 16:28:011046 'mac_rel',
[email protected]7fab6202013-02-21 17:54:351047 'mac:compile',
[email protected]aa85c8b2013-01-11 04:20:281048 'win7_aura',
[email protected]3e2f0402012-11-02 16:28:011049 'win_rel',
[email protected]7fab6202013-02-21 17:54:351050 'win:compile',
[email protected]24d870f2013-07-23 00:45:461051 'win_x64_rel:compile',
[email protected]3e2f0402012-11-02 16:28:011052 ]
[email protected]911753b2012-08-02 12:11:541053
1054 # Match things like path/aura/file.cc and path/file_aura.cc.
[email protected]95c989162012-11-29 05:58:251055 # Same for chromeos.
1056 if any(re.search('[/_](aura|chromeos)', f) for f in files):
[email protected]3e2f0402012-11-02 16:28:011057 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
[email protected]4ce995ea2012-06-27 02:13:101058
[email protected]d95948ef2013-07-02 10:51:001059 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1060 # unless they're .gyp(i) files as changes to those files can break the gyp
1061 # step on that bot.
1062 if (not all(re.search('^chrome', f) for f in files) or
1063 any(re.search('\.gypi?$', f) for f in files)):
1064 trybots += ['android_aosp']
1065
[email protected]4ce995ea2012-06-27 02:13:101066 return trybots