blob: 701fd21f3e813717d22de1ea983952365ad6abbf [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]3e83618c2013-10-09 22:32:33581 # Match the final or penultimate token if it is xxxtest so we can ignore it
582 # when considering the special first include.
583 test_file_tag_pattern = input_api.re.compile(
584 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
[email protected]0e5c1852012-12-18 20:17:11585 if_pattern = input_api.re.compile(
586 r'\s*#\s*(if|elif|else|endif|define|undef).*')
587 # Some files need specialized order of includes; exclude such files from this
588 # check.
589 uncheckable_includes_pattern = input_api.re.compile(
590 r'\s*#include '
591 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
[email protected]cf9b78f2012-11-14 11:40:28592
593 contents = f.NewContents()
594 warnings = []
595 line_num = 0
596
[email protected]ac294a12012-12-06 16:38:43597 # Handle the special first include. If the first include file is
598 # some/path/file.h, the corresponding including file can be some/path/file.cc,
599 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
600 # etc. It's also possible that no special first include exists.
[email protected]3e83618c2013-10-09 22:32:33601 # If the included file is some/path/file_platform.h the including file could
602 # also be some/path/file_xxxtest_platform.h.
603 including_file_base_name = test_file_tag_pattern.sub(
604 '', input_api.os_path.basename(f.LocalPath()))
605
[email protected]ac294a12012-12-06 16:38:43606 for line in contents:
607 line_num += 1
608 if system_include_pattern.match(line):
609 # No special first include -> process the line again along with normal
610 # includes.
611 line_num -= 1
612 break
613 match = custom_include_pattern.match(line)
614 if match:
615 match_dict = match.groupdict()
[email protected]3e83618c2013-10-09 22:32:33616 header_basename = test_file_tag_pattern.sub(
617 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
618
619 if header_basename not in including_file_base_name:
[email protected]2299dcf2012-11-15 19:56:24620 # No special first include -> process the line again along with normal
621 # includes.
622 line_num -= 1
[email protected]ac294a12012-12-06 16:38:43623 break
[email protected]cf9b78f2012-11-14 11:40:28624
625 # Split into scopes: Each region between #if and #endif is its own scope.
626 scopes = []
627 current_scope = []
628 for line in contents[line_num:]:
629 line_num += 1
[email protected]0e5c1852012-12-18 20:17:11630 if uncheckable_includes_pattern.match(line):
631 return []
[email protected]2309b0fa02012-11-16 12:18:27632 if if_pattern.match(line):
[email protected]cf9b78f2012-11-14 11:40:28633 scopes.append(current_scope)
634 current_scope = []
[email protected]962f117e2012-11-22 18:11:56635 elif ((system_include_pattern.match(line) or
636 custom_include_pattern.match(line)) and
637 not excluded_include_pattern.match(line)):
[email protected]cf9b78f2012-11-14 11:40:28638 current_scope.append((line_num, line))
639 scopes.append(current_scope)
640
641 for scope in scopes:
642 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
643 changed_linenums))
644 return warnings
645
646
647def _CheckIncludeOrder(input_api, output_api):
648 """Checks that the #include order is correct.
649
650 1. The corresponding header for source files.
651 2. C system files in alphabetical order
652 3. C++ system files in alphabetical order
653 4. Project's .h files in alphabetical order
654
[email protected]ac294a12012-12-06 16:38:43655 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
656 these rules separately.
[email protected]cf9b78f2012-11-14 11:40:28657 """
658
659 warnings = []
660 for f in input_api.AffectedFiles():
[email protected]ac294a12012-12-06 16:38:43661 if f.LocalPath().endswith(('.cc', '.h')):
662 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
663 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
[email protected]cf9b78f2012-11-14 11:40:28664
665 results = []
666 if warnings:
[email protected]f7051d52013-04-02 18:31:42667 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
[email protected]120cf540d2012-12-10 17:55:53668 warnings))
[email protected]cf9b78f2012-11-14 11:40:28669 return results
670
671
[email protected]70ca77752012-11-20 03:45:03672def _CheckForVersionControlConflictsInFile(input_api, f):
673 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
674 errors = []
675 for line_num, line in f.ChangedContents():
676 if pattern.match(line):
677 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
678 return errors
679
680
681def _CheckForVersionControlConflicts(input_api, output_api):
682 """Usually this is not intentional and will cause a compile failure."""
683 errors = []
684 for f in input_api.AffectedFiles():
685 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
686
687 results = []
688 if errors:
689 results.append(output_api.PresubmitError(
690 'Version control conflict markers found, please resolve.', errors))
691 return results
692
693
[email protected]06e6d0ff2012-12-11 01:36:44694def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
695 def FilterFile(affected_file):
696 """Filter function for use with input_api.AffectedSourceFiles,
697 below. This filters out everything except non-test files from
698 top-level directories that generally speaking should not hard-code
699 service URLs (e.g. src/android_webview/, src/content/ and others).
700 """
701 return input_api.FilterSourceFile(
702 affected_file,
[email protected]78bb39d62012-12-11 15:11:56703 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
[email protected]06e6d0ff2012-12-11 01:36:44704 black_list=(_EXCLUDED_PATHS +
705 _TEST_CODE_EXCLUDED_PATHS +
706 input_api.DEFAULT_BLACK_LIST))
707
[email protected]de4f7d22013-05-23 14:27:46708 base_pattern = '"[^"]*google\.com[^"]*"'
709 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
710 pattern = input_api.re.compile(base_pattern)
[email protected]06e6d0ff2012-12-11 01:36:44711 problems = [] # items are (filename, line_number, line)
712 for f in input_api.AffectedSourceFiles(FilterFile):
713 for line_num, line in f.ChangedContents():
[email protected]de4f7d22013-05-23 14:27:46714 if not comment_pattern.search(line) and pattern.search(line):
[email protected]06e6d0ff2012-12-11 01:36:44715 problems.append((f.LocalPath(), line_num, line))
716
717 if problems:
[email protected]f7051d52013-04-02 18:31:42718 return [output_api.PresubmitPromptOrNotify(
[email protected]06e6d0ff2012-12-11 01:36:44719 'Most layers below src/chrome/ should not hardcode service URLs.\n'
720 'Are you sure this is correct? (Contact: [email protected])',
721 [' %s:%d: %s' % (
722 problem[0], problem[1], problem[2]) for problem in problems])]
[email protected]2fdd1f362013-01-16 03:56:03723 else:
724 return []
[email protected]06e6d0ff2012-12-11 01:36:44725
726
[email protected]d2530012013-01-25 16:39:27727def _CheckNoAbbreviationInPngFileName(input_api, output_api):
728 """Makes sure there are no abbreviations in the name of PNG files.
729 """
[email protected]4053a48e2013-01-25 21:43:04730 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
[email protected]d2530012013-01-25 16:39:27731 errors = []
732 for f in input_api.AffectedFiles(include_deletes=False):
733 if pattern.match(f.LocalPath()):
734 errors.append(' %s' % f.LocalPath())
735
736 results = []
737 if errors:
738 results.append(output_api.PresubmitError(
739 'The name of PNG files should not have abbreviations. \n'
740 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
741 'Contact [email protected] if you have questions.', errors))
742 return results
743
744
[email protected]f32e2d1e2013-07-26 21:39:08745def _DepsFilesToCheck(re, changed_lines):
746 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
747 a set of DEPS entries that we should look up."""
748 results = set()
749
750 # This pattern grabs the path without basename in the first
751 # parentheses, and the basename (if present) in the second. It
752 # relies on the simple heuristic that if there is a basename it will
753 # be a header file ending in ".h".
754 pattern = re.compile(
755 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
756 for changed_line in changed_lines:
757 m = pattern.match(changed_line)
758 if m:
759 path = m.group(1)
760 if not (path.startswith('grit/') or path == 'grit'):
761 results.add('%s/DEPS' % m.group(1))
762 return results
763
764
[email protected]e871964c2013-05-13 14:14:55765def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
766 """When a dependency prefixed with + is added to a DEPS file, we
767 want to make sure that the change is reviewed by an OWNER of the
768 target file or directory, to avoid layering violations from being
769 introduced. This check verifies that this happens.
770 """
771 changed_lines = set()
772 for f in input_api.AffectedFiles():
773 filename = input_api.os_path.basename(f.LocalPath())
774 if filename == 'DEPS':
775 changed_lines |= set(line.strip()
776 for line_num, line
777 in f.ChangedContents())
778 if not changed_lines:
779 return []
780
[email protected]f32e2d1e2013-07-26 21:39:08781 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
[email protected]e871964c2013-05-13 14:14:55782 if not virtual_depended_on_files:
783 return []
784
785 if input_api.is_committing:
786 if input_api.tbr:
787 return [output_api.PresubmitNotifyResult(
788 '--tbr was specified, skipping OWNERS check for DEPS additions')]
789 if not input_api.change.issue:
790 return [output_api.PresubmitError(
791 "DEPS approval by OWNERS check failed: this change has "
792 "no Rietveld issue number, so we can't check it for approvals.")]
793 output = output_api.PresubmitError
794 else:
795 output = output_api.PresubmitNotifyResult
796
797 owners_db = input_api.owners_db
798 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
799 input_api,
800 owners_db.email_regexp,
801 approval_needed=input_api.is_committing)
802
803 owner_email = owner_email or input_api.change.author_email
804
[email protected]de4f7d22013-05-23 14:27:46805 reviewers_plus_owner = set(reviewers)
[email protected]e71c6082013-05-22 02:28:51806 if owner_email:
[email protected]de4f7d22013-05-23 14:27:46807 reviewers_plus_owner.add(owner_email)
[email protected]e871964c2013-05-13 14:14:55808 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
809 reviewers_plus_owner)
810 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
811 for path in missing_files]
812
813 if unapproved_dependencies:
814 output_list = [
815 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
816 '\n '.join(sorted(unapproved_dependencies)))]
817 if not input_api.is_committing:
818 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
819 output_list.append(output(
820 'Suggested missing target path OWNERS:\n %s' %
821 '\n '.join(suggested_owners or [])))
822 return output_list
823
824 return []
825
826
[email protected]22c9bd72011-03-27 16:47:39827def _CommonChecks(input_api, output_api):
828 """Checks common to both upload and commit."""
829 results = []
830 results.extend(input_api.canned_checks.PanProjectChecks(
831 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
[email protected]66daa702011-05-28 14:41:46832 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
[email protected]55459852011-08-10 15:17:19833 results.extend(
834 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
[email protected]10689ca2011-09-02 02:31:54835 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
[email protected]72df4e782012-06-21 16:28:18836 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
[email protected]8ea5d4b2011-09-13 21:49:22837 results.extend(_CheckNoNewWStrings(input_api, output_api))
[email protected]2a8ac9c2011-10-19 17:20:44838 results.extend(_CheckNoDEPSGIT(input_api, output_api))
[email protected]127f18ec2012-06-16 05:05:59839 results.extend(_CheckNoBannedFunctions(input_api, output_api))
[email protected]6c063c62012-07-11 19:11:06840 results.extend(_CheckNoPragmaOnce(input_api, output_api))
[email protected]e7479052012-09-19 00:26:12841 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
[email protected]55f9f382012-07-31 11:02:18842 results.extend(_CheckUnwantedDependencies(input_api, output_api))
[email protected]fbcafe5a2012-08-08 15:31:22843 results.extend(_CheckFilePermissions(input_api, output_api))
[email protected]c8278b32012-10-30 20:35:49844 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
[email protected]2309b0fa02012-11-16 12:18:27845 results.extend(_CheckIncludeOrder(input_api, output_api))
[email protected]70ca77752012-11-20 03:45:03846 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
[email protected]b8079ae4a2012-12-05 19:56:49847 results.extend(_CheckPatchFiles(input_api, output_api))
[email protected]06e6d0ff2012-12-11 01:36:44848 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
[email protected]d2530012013-01-25 16:39:27849 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
[email protected]b00342e7f2013-03-26 16:21:54850 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
[email protected]e871964c2013-05-13 14:14:55851 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
[email protected]9f919cc2013-07-31 03:04:04852 results.extend(
853 input_api.canned_checks.CheckChangeHasNoTabs(
854 input_api,
855 output_api,
856 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
[email protected]2299dcf2012-11-15 19:56:24857
858 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
859 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
860 input_api, output_api,
861 input_api.PresubmitLocalPath(),
[email protected]6be63382013-01-21 15:42:38862 whitelist=[r'^PRESUBMIT_test\.py$']))
[email protected]22c9bd72011-03-27 16:47:39863 return results
[email protected]1f7b4172010-01-28 01:17:34864
[email protected]b337cb5b2011-01-23 21:24:05865
866def _CheckSubversionConfig(input_api, output_api):
867 """Verifies the subversion config file is correctly setup.
868
869 Checks that autoprops are enabled, returns an error otherwise.
870 """
871 join = input_api.os_path.join
872 if input_api.platform == 'win32':
873 appdata = input_api.environ.get('APPDATA', '')
874 if not appdata:
875 return [output_api.PresubmitError('%APPDATA% is not configured.')]
876 path = join(appdata, 'Subversion', 'config')
877 else:
878 home = input_api.environ.get('HOME', '')
879 if not home:
880 return [output_api.PresubmitError('$HOME is not configured.')]
881 path = join(home, '.subversion', 'config')
882
883 error_msg = (
884 'Please look at http://dev.chromium.org/developers/coding-style to\n'
885 'configure your subversion configuration file. This enables automatic\n'
[email protected]c6a3c10b2011-01-24 16:14:20886 'properties to simplify the project maintenance.\n'
887 'Pro-tip: just download and install\n'
888 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
[email protected]b337cb5b2011-01-23 21:24:05889
890 try:
891 lines = open(path, 'r').read().splitlines()
892 # Make sure auto-props is enabled and check for 2 Chromium standard
893 # auto-prop.
894 if (not '*.cc = svn:eol-style=LF' in lines or
895 not '*.pdf = svn:mime-type=application/pdf' in lines or
896 not 'enable-auto-props = yes' in lines):
897 return [
[email protected]79ed7e62011-02-21 21:08:53898 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05899 'It looks like you have not configured your subversion config '
[email protected]b5359c02011-02-01 20:29:56900 'file or it is not up-to-date.\n' + error_msg)
[email protected]b337cb5b2011-01-23 21:24:05901 ]
902 except (OSError, IOError):
903 return [
[email protected]79ed7e62011-02-21 21:08:53904 output_api.PresubmitNotifyResult(
[email protected]b337cb5b2011-01-23 21:24:05905 'Can\'t find your subversion config file.\n' + error_msg)
906 ]
907 return []
908
909
[email protected]66daa702011-05-28 14:41:46910def _CheckAuthorizedAuthor(input_api, output_api):
911 """For non-googler/chromites committers, verify the author's email address is
912 in AUTHORS.
913 """
[email protected]9bb9cb82011-06-13 20:43:01914 # TODO(maruel): Add it to input_api?
915 import fnmatch
916
[email protected]66daa702011-05-28 14:41:46917 author = input_api.change.author_email
[email protected]9bb9cb82011-06-13 20:43:01918 if not author:
919 input_api.logging.info('No author, skipping AUTHOR check')
[email protected]66daa702011-05-28 14:41:46920 return []
[email protected]c99663292011-05-31 19:46:08921 authors_path = input_api.os_path.join(
[email protected]66daa702011-05-28 14:41:46922 input_api.PresubmitLocalPath(), 'AUTHORS')
923 valid_authors = (
924 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
925 for line in open(authors_path))
[email protected]ac54b132011-06-06 18:11:18926 valid_authors = [item.group(1).lower() for item in valid_authors if item]
[email protected]d8b50be2011-06-15 14:19:44927 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
[email protected]5861efb2013-01-07 18:33:23928 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
[email protected]66daa702011-05-28 14:41:46929 return [output_api.PresubmitPromptWarning(
930 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
931 '\n'
932 'http://www.chromium.org/developers/contributing-code and read the '
933 '"Legal" section\n'
934 'If you are a chromite, verify the contributor signed the CLA.') %
935 author)]
936 return []
937
938
[email protected]b8079ae4a2012-12-05 19:56:49939def _CheckPatchFiles(input_api, output_api):
940 problems = [f.LocalPath() for f in input_api.AffectedFiles()
941 if f.LocalPath().endswith(('.orig', '.rej'))]
942 if problems:
943 return [output_api.PresubmitError(
944 "Don't commit .rej and .orig files.", problems)]
[email protected]2fdd1f362013-01-16 03:56:03945 else:
946 return []
[email protected]b8079ae4a2012-12-05 19:56:49947
948
[email protected]b00342e7f2013-03-26 16:21:54949def _DidYouMeanOSMacro(bad_macro):
950 try:
951 return {'A': 'OS_ANDROID',
952 'B': 'OS_BSD',
953 'C': 'OS_CHROMEOS',
954 'F': 'OS_FREEBSD',
955 'L': 'OS_LINUX',
956 'M': 'OS_MACOSX',
957 'N': 'OS_NACL',
958 'O': 'OS_OPENBSD',
959 'P': 'OS_POSIX',
960 'S': 'OS_SOLARIS',
961 'W': 'OS_WIN'}[bad_macro[3].upper()]
962 except KeyError:
963 return ''
964
965
966def _CheckForInvalidOSMacrosInFile(input_api, f):
967 """Check for sensible looking, totally invalid OS macros."""
968 preprocessor_statement = input_api.re.compile(r'^\s*#')
969 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
970 results = []
971 for lnum, line in f.ChangedContents():
972 if preprocessor_statement.search(line):
973 for match in os_macro.finditer(line):
974 if not match.group(1) in _VALID_OS_MACROS:
975 good = _DidYouMeanOSMacro(match.group(1))
976 did_you_mean = ' (did you mean %s?)' % good if good else ''
977 results.append(' %s:%d %s%s' % (f.LocalPath(),
978 lnum,
979 match.group(1),
980 did_you_mean))
981 return results
982
983
984def _CheckForInvalidOSMacros(input_api, output_api):
985 """Check all affected files for invalid OS macros."""
986 bad_macros = []
987 for f in input_api.AffectedFiles():
988 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
989 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
990
991 if not bad_macros:
992 return []
993
994 return [output_api.PresubmitError(
995 'Possibly invalid OS macro[s] found. Please fix your code\n'
996 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
997
998
[email protected]1f7b4172010-01-28 01:17:34999def CheckChangeOnUpload(input_api, output_api):
1000 results = []
1001 results.extend(_CommonChecks(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541002 return results
[email protected]ca8d1982009-02-19 16:33:121003
1004
1005def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:541006 results = []
[email protected]1f7b4172010-01-28 01:17:341007 results.extend(_CommonChecks(input_api, output_api))
[email protected]dd805fe2009-10-01 08:11:511008 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1009 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1010 # input_api, output_api, sources))
[email protected]fe5f57c52009-06-05 14:25:541011 # Make sure the tree is 'open'.
[email protected]806e98e2010-03-19 17:49:271012 results.extend(input_api.canned_checks.CheckTreeIsOpen(
[email protected]7f238152009-08-12 19:00:341013 input_api,
1014 output_api,
[email protected]2fdd1f362013-01-16 03:56:031015 json_url='http://chromium-status.appspot.com/current?format=json'))
[email protected]806e98e2010-03-19 17:49:271016 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
[email protected]2fdd1f362013-01-16 03:56:031017 output_api, 'http://codereview.chromium.org',
[email protected]c1ba4c52012-03-09 14:23:281018 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1019 '[email protected]'))
[email protected]806e98e2010-03-19 17:49:271020
[email protected]3e4eb112011-01-18 03:29:541021 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1022 input_api, output_api))
[email protected]c4b47562011-12-05 23:39:411023 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1024 input_api, output_api))
[email protected]b337cb5b2011-01-23 21:24:051025 results.extend(_CheckSubversionConfig(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541026 return results
[email protected]ca8d1982009-02-19 16:33:121027
1028
[email protected]5efb2a822011-09-27 23:06:131029def GetPreferredTrySlaves(project, change):
[email protected]4ce995ea2012-06-27 02:13:101030 files = change.LocalPaths()
1031
[email protected]751b05f2013-01-10 23:12:171032 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
[email protected]3019c902012-06-29 00:09:031033 return []
1034
[email protected]d668899a2012-09-06 18:16:591035 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
[email protected]5753cff2013-07-19 23:57:521036 return ['mac_rel', 'mac:compile']
[email protected]d668899a2012-09-06 18:16:591037 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
[email protected]7fab6202013-02-21 17:54:351038 return ['win_rel', 'win7_aura', 'win:compile']
[email protected]d668899a2012-09-06 18:16:591039 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
[email protected]d95948ef2013-07-02 10:51:001040 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
[email protected]356aa542012-09-19 23:31:291041 if all(re.search('^native_client_sdk', f) for f in files):
1042 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
[email protected]de142152012-10-03 23:02:451043 if all(re.search('[/_]ios[/_.]', f) for f in files):
1044 return ['ios_rel_device', 'ios_dbg_simulator']
[email protected]4ce995ea2012-06-27 02:13:101045
[email protected]3e2f0402012-11-02 16:28:011046 trybots = [
1047 'android_clang_dbg',
1048 'android_dbg',
1049 'ios_dbg_simulator',
1050 'ios_rel_device',
1051 'linux_asan',
[email protected]95c989162012-11-29 05:58:251052 'linux_aura',
[email protected]3e2f0402012-11-02 16:28:011053 'linux_chromeos',
1054 'linux_clang:compile',
1055 'linux_rel',
[email protected]3e2f0402012-11-02 16:28:011056 'mac_rel',
[email protected]7fab6202013-02-21 17:54:351057 'mac:compile',
[email protected]aa85c8b2013-01-11 04:20:281058 'win7_aura',
[email protected]3e2f0402012-11-02 16:28:011059 'win_rel',
[email protected]7fab6202013-02-21 17:54:351060 'win:compile',
[email protected]24d870f2013-07-23 00:45:461061 'win_x64_rel:compile',
[email protected]3e2f0402012-11-02 16:28:011062 ]
[email protected]911753b2012-08-02 12:11:541063
1064 # Match things like path/aura/file.cc and path/file_aura.cc.
[email protected]95c989162012-11-29 05:58:251065 # Same for chromeos.
1066 if any(re.search('[/_](aura|chromeos)', f) for f in files):
[email protected]3e2f0402012-11-02 16:28:011067 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
[email protected]4ce995ea2012-06-27 02:13:101068
[email protected]e8df48f2013-09-30 20:07:541069 # If there are gyp changes to base, build, or chromeos, run a full cros build
1070 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1071 # files have a much higher chance of breaking the cros build, which is
1072 # differnt from the linux_chromeos build that most chrome developers test
1073 # with.
1074 if any(re.search('^(base|build|chromeos).*\.gypi?$', f) for f in files):
1075 trybots += ['cros_x86']
1076
[email protected]d95948ef2013-07-02 10:51:001077 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1078 # unless they're .gyp(i) files as changes to those files can break the gyp
1079 # step on that bot.
1080 if (not all(re.search('^chrome', f) for f in files) or
1081 any(re.search('\.gypi?$', f) for f in files)):
1082 trybots += ['android_aosp']
1083
[email protected]4ce995ea2012-06-27 02:13:101084 return trybots