blob: 7f4c5a9a8de5d6b1b7de7e027ed2af8b362777af [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Top-level presubmit script for Chromium.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details about the presubmit API built into gcl.
9"""
10
11
12import re
13import subprocess
14import sys
15
16
17_EXCLUDED_PATHS = (
18 r"^breakpad[\\\/].*",
19 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000021 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
Torne (Richard Coles)58218062012-11-14 11:43:16 +000022 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
23 r"^skia[\\\/].*",
24 r"^v8[\\\/].*",
25 r".*MakeFile$",
26 r".+_autogen\.h$",
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027 r".+[\\\/]pnacl_shim\.c$",
28)
29
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030# Fragment of a regular expression that matches C++ and Objective-C++
31# implementation files.
32_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
33
34# Regular expression that matches code only used for test binaries
35# (best effort).
36_TEST_CODE_EXCLUDED_PATHS = (
37 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
38 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010039 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
40 _IMPLEMENTATION_EXTENSIONS,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000041 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
42 r'.*[/\\](test|tool(s)?)[/\\].*',
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010043 # content_shell is used for running layout tests.
44 r'content[/\\]shell[/\\].*',
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000045 # At request of folks maintaining this folder.
46 r'chrome[/\\]browser[/\\]automation[/\\].*',
47)
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048
49_TEST_ONLY_WARNING = (
50 'You might be calling functions intended only for testing from\n'
51 'production code. It is OK to ignore this warning if you know what\n'
52 'you are doing, as the heuristics used to detect the situation are\n'
53 'not perfect. The commit queue will not block on this warning.\n'
54 'Email joi@chromium.org if you have questions.')
55
56
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000057_INCLUDE_ORDER_WARNING = (
58 'Your #include order seems to be broken. Send mail to\n'
59 'marja@chromium.org if this is not the case.')
60
61
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062_BANNED_OBJC_FUNCTIONS = (
63 (
64 'addTrackingRect:',
65 (
66 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
67 'prohibited. Please use CrTrackingArea instead.',
68 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
69 ),
70 False,
71 ),
72 (
73 'NSTrackingArea',
74 (
75 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
76 'instead.',
77 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
78 ),
79 False,
80 ),
81 (
82 'convertPointFromBase:',
83 (
84 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
85 'Please use |convertPoint:(point) fromView:nil| instead.',
86 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
87 ),
88 True,
89 ),
90 (
91 'convertPointToBase:',
92 (
93 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
94 'Please use |convertPoint:(point) toView:nil| instead.',
95 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
96 ),
97 True,
98 ),
99 (
100 'convertRectFromBase:',
101 (
102 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
103 'Please use |convertRect:(point) fromView:nil| instead.',
104 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
105 ),
106 True,
107 ),
108 (
109 'convertRectToBase:',
110 (
111 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
112 'Please use |convertRect:(point) toView:nil| instead.',
113 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
114 ),
115 True,
116 ),
117 (
118 'convertSizeFromBase:',
119 (
120 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
121 'Please use |convertSize:(point) fromView:nil| instead.',
122 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
123 ),
124 True,
125 ),
126 (
127 'convertSizeToBase:',
128 (
129 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
130 'Please use |convertSize:(point) toView:nil| instead.',
131 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
132 ),
133 True,
134 ),
135)
136
137
138_BANNED_CPP_FUNCTIONS = (
139 # Make sure that gtest's FRIEND_TEST() macro is not used; the
140 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
141 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
142 (
143 'FRIEND_TEST(',
144 (
145 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
146 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
147 ),
148 False,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000149 (),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000150 ),
151 (
152 'ScopedAllowIO',
153 (
154 'New code should not use ScopedAllowIO. Post a task to the blocking',
155 'pool or the FILE thread instead.',
156 ),
157 True,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000158 (
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000159 r"^content[\\\/]shell[\\\/]shell_browser_main\.cc$",
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100160 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000161 ),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000162 ),
163)
164
165
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100166_VALID_OS_MACROS = (
167 # Please keep sorted.
168 'OS_ANDROID',
169 'OS_BSD',
170 'OS_CAT', # For testing.
171 'OS_CHROMEOS',
172 'OS_FREEBSD',
173 'OS_IOS',
174 'OS_LINUX',
175 'OS_MACOSX',
176 'OS_NACL',
177 'OS_OPENBSD',
178 'OS_POSIX',
179 'OS_SOLARIS',
180 'OS_WIN',
181)
182
183
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000184def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
185 """Attempts to prevent use of functions intended only for testing in
186 non-testing code. For now this is just a best-effort implementation
187 that ignores header files and may have some false positives. A
188 better implementation would probably need a proper C++ parser.
189 """
190 # We only scan .cc files and the like, as the declaration of
191 # for-testing functions in header files are hard to distinguish from
192 # calls to such functions without a proper C++ parser.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000193 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000194
195 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
196 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
197 exclusion_pattern = input_api.re.compile(
198 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
199 base_function_pattern, base_function_pattern))
200
201 def FilterFile(affected_file):
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000202 black_list = (_EXCLUDED_PATHS +
203 _TEST_CODE_EXCLUDED_PATHS +
204 input_api.DEFAULT_BLACK_LIST)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000205 return input_api.FilterSourceFile(
206 affected_file,
207 white_list=(file_inclusion_pattern, ),
208 black_list=black_list)
209
210 problems = []
211 for f in input_api.AffectedSourceFiles(FilterFile):
212 local_path = f.LocalPath()
213 lines = input_api.ReadFile(f).splitlines()
214 line_number = 0
215 for line in lines:
216 if (inclusion_pattern.search(line) and
217 not exclusion_pattern.search(line)):
218 problems.append(
219 '%s:%d\n %s' % (local_path, line_number, line.strip()))
220 line_number += 1
221
222 if problems:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100223 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000224 else:
225 return []
226
227
228def _CheckNoIOStreamInHeaders(input_api, output_api):
229 """Checks to make sure no .h files include <iostream>."""
230 files = []
231 pattern = input_api.re.compile(r'^#include\s*<iostream>',
232 input_api.re.MULTILINE)
233 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
234 if not f.LocalPath().endswith('.h'):
235 continue
236 contents = input_api.ReadFile(f)
237 if pattern.search(contents):
238 files.append(f)
239
240 if len(files):
241 return [ output_api.PresubmitError(
242 'Do not #include <iostream> in header files, since it inserts static '
243 'initialization into every file including the header. Instead, '
244 '#include <ostream>. See http://crbug.com/94794',
245 files) ]
246 return []
247
248
249def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
250 """Checks to make sure no source files use UNIT_TEST"""
251 problems = []
252 for f in input_api.AffectedFiles():
253 if (not f.LocalPath().endswith(('.cc', '.mm'))):
254 continue
255
256 for line_num, line in f.ChangedContents():
257 if 'UNIT_TEST' in line:
258 problems.append(' %s:%d' % (f.LocalPath(), line_num))
259
260 if not problems:
261 return []
262 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
263 '\n'.join(problems))]
264
265
266def _CheckNoNewWStrings(input_api, output_api):
267 """Checks to make sure we don't introduce use of wstrings."""
268 problems = []
269 for f in input_api.AffectedFiles():
270 if (not f.LocalPath().endswith(('.cc', '.h')) or
271 f.LocalPath().endswith('test.cc')):
272 continue
273
274 allowWString = False
275 for line_num, line in f.ChangedContents():
276 if 'presubmit: allow wstring' in line:
277 allowWString = True
278 elif not allowWString and 'wstring' in line:
279 problems.append(' %s:%d' % (f.LocalPath(), line_num))
280 allowWString = False
281 else:
282 allowWString = False
283
284 if not problems:
285 return []
286 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
287 ' If you are calling a cross-platform API that accepts a wstring, '
288 'fix the API.\n' +
289 '\n'.join(problems))]
290
291
292def _CheckNoDEPSGIT(input_api, output_api):
293 """Make sure .DEPS.git is never modified manually."""
294 if any(f.LocalPath().endswith('.DEPS.git') for f in
295 input_api.AffectedFiles()):
296 return [output_api.PresubmitError(
297 'Never commit changes to .DEPS.git. This file is maintained by an\n'
298 'automated system based on what\'s in DEPS and your changes will be\n'
299 'overwritten.\n'
300 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
301 'for more information')]
302 return []
303
304
305def _CheckNoBannedFunctions(input_api, output_api):
306 """Make sure that banned functions are not used."""
307 warnings = []
308 errors = []
309
310 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
311 for f in input_api.AffectedFiles(file_filter=file_filter):
312 for line_num, line in f.ChangedContents():
313 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
314 if func_name in line:
315 problems = warnings;
316 if error:
317 problems = errors;
318 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
319 for message_line in message:
320 problems.append(' %s' % message_line)
321
322 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
323 for f in input_api.AffectedFiles(file_filter=file_filter):
324 for line_num, line in f.ChangedContents():
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000325 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
326 def IsBlacklisted(affected_file, blacklist):
327 local_path = affected_file.LocalPath()
328 for item in blacklist:
329 if input_api.re.match(item, local_path):
330 return True
331 return False
332 if IsBlacklisted(f, excluded_paths):
333 continue
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000334 if func_name in line:
335 problems = warnings;
336 if error:
337 problems = errors;
338 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
339 for message_line in message:
340 problems.append(' %s' % message_line)
341
342 result = []
343 if (warnings):
344 result.append(output_api.PresubmitPromptWarning(
345 'Banned functions were used.\n' + '\n'.join(warnings)))
346 if (errors):
347 result.append(output_api.PresubmitError(
348 'Banned functions were used.\n' + '\n'.join(errors)))
349 return result
350
351
352def _CheckNoPragmaOnce(input_api, output_api):
353 """Make sure that banned functions are not used."""
354 files = []
355 pattern = input_api.re.compile(r'^#pragma\s+once',
356 input_api.re.MULTILINE)
357 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
358 if not f.LocalPath().endswith('.h'):
359 continue
360 contents = input_api.ReadFile(f)
361 if pattern.search(contents):
362 files.append(f)
363
364 if files:
365 return [output_api.PresubmitError(
366 'Do not use #pragma once in header files.\n'
367 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
368 files)]
369 return []
370
371
372def _CheckNoTrinaryTrueFalse(input_api, output_api):
373 """Checks to make sure we don't introduce use of foo ? true : false."""
374 problems = []
375 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
376 for f in input_api.AffectedFiles():
377 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
378 continue
379
380 for line_num, line in f.ChangedContents():
381 if pattern.match(line):
382 problems.append(' %s:%d' % (f.LocalPath(), line_num))
383
384 if not problems:
385 return []
386 return [output_api.PresubmitPromptWarning(
387 'Please consider avoiding the "? true : false" pattern if possible.\n' +
388 '\n'.join(problems))]
389
390
391def _CheckUnwantedDependencies(input_api, output_api):
392 """Runs checkdeps on #include statements added in this
393 change. Breaking - rules is an error, breaking ! rules is a
394 warning.
395 """
396 # We need to wait until we have an input_api object and use this
397 # roundabout construct to import checkdeps because this file is
398 # eval-ed and thus doesn't have __file__.
399 original_sys_path = sys.path
400 try:
401 sys.path = sys.path + [input_api.os_path.join(
402 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
403 import checkdeps
404 from cpp_checker import CppChecker
405 from rules import Rule
406 finally:
407 # Restore sys.path to what it was before.
408 sys.path = original_sys_path
409
410 added_includes = []
411 for f in input_api.AffectedFiles():
412 if not CppChecker.IsCppFile(f.LocalPath()):
413 continue
414
415 changed_lines = [line for line_num, line in f.ChangedContents()]
416 added_includes.append([f.LocalPath(), changed_lines])
417
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +0100418 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000419
420 error_descriptions = []
421 warning_descriptions = []
422 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
423 added_includes):
424 description_with_path = '%s\n %s' % (path, rule_description)
425 if rule_type == Rule.DISALLOW:
426 error_descriptions.append(description_with_path)
427 else:
428 warning_descriptions.append(description_with_path)
429
430 results = []
431 if error_descriptions:
432 results.append(output_api.PresubmitError(
433 'You added one or more #includes that violate checkdeps rules.',
434 error_descriptions))
435 if warning_descriptions:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100436 results.append(output_api.PresubmitPromptOrNotify(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000437 'You added one or more #includes of files that are temporarily\n'
438 'allowed but being removed. Can you avoid introducing the\n'
439 '#include? See relevant DEPS file(s) for details and contacts.',
440 warning_descriptions))
441 return results
442
443
444def _CheckFilePermissions(input_api, output_api):
445 """Check that all files have their permissions properly set."""
446 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
447 input_api.change.RepositoryRoot()]
448 for f in input_api.AffectedFiles():
449 args += ['--file', f.LocalPath()]
450 errors = []
451 (errors, stderrdata) = subprocess.Popen(args).communicate()
452
453 results = []
454 if errors:
455 results.append(output_api.PresubmitError('checkperms.py failed.',
456 errors))
457 return results
458
459
460def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
461 """Makes sure we don't include ui/aura/window_property.h
462 in header files.
463 """
464 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
465 errors = []
466 for f in input_api.AffectedFiles():
467 if not f.LocalPath().endswith('.h'):
468 continue
469 for line_num, line in f.ChangedContents():
470 if pattern.match(line):
471 errors.append(' %s:%d' % (f.LocalPath(), line_num))
472
473 results = []
474 if errors:
475 results.append(output_api.PresubmitError(
476 'Header files should not include ui/aura/window_property.h', errors))
477 return results
478
479
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000480def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
481 """Checks that the lines in scope occur in the right order.
482
483 1. C system files in alphabetical order
484 2. C++ system files in alphabetical order
485 3. Project's .h files
486 """
487
488 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
489 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
490 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
491
492 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
493
494 state = C_SYSTEM_INCLUDES
495
496 previous_line = ''
497 previous_line_num = 0
498 problem_linenums = []
499 for line_num, line in scope:
500 if c_system_include_pattern.match(line):
501 if state != C_SYSTEM_INCLUDES:
502 problem_linenums.append((line_num, previous_line_num))
503 elif previous_line and previous_line > line:
504 problem_linenums.append((line_num, previous_line_num))
505 elif cpp_system_include_pattern.match(line):
506 if state == C_SYSTEM_INCLUDES:
507 state = CPP_SYSTEM_INCLUDES
508 elif state == CUSTOM_INCLUDES:
509 problem_linenums.append((line_num, previous_line_num))
510 elif previous_line and previous_line > line:
511 problem_linenums.append((line_num, previous_line_num))
512 elif custom_include_pattern.match(line):
513 if state != CUSTOM_INCLUDES:
514 state = CUSTOM_INCLUDES
515 elif previous_line and previous_line > line:
516 problem_linenums.append((line_num, previous_line_num))
517 else:
518 problem_linenums.append(line_num)
519 previous_line = line
520 previous_line_num = line_num
521
522 warnings = []
523 for (line_num, previous_line_num) in problem_linenums:
524 if line_num in changed_linenums or previous_line_num in changed_linenums:
525 warnings.append(' %s:%d' % (file_path, line_num))
526 return warnings
527
528
529def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
530 """Checks the #include order for the given file f."""
531
532 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
533 # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes
534 # often need to appear in a specific order.
535 excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*')
536 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
537 if_pattern = input_api.re.compile(
538 r'\s*#\s*(if|elif|else|endif|define|undef).*')
539 # Some files need specialized order of includes; exclude such files from this
540 # check.
541 uncheckable_includes_pattern = input_api.re.compile(
542 r'\s*#include '
543 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
544
545 contents = f.NewContents()
546 warnings = []
547 line_num = 0
548
549 # Handle the special first include. If the first include file is
550 # some/path/file.h, the corresponding including file can be some/path/file.cc,
551 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
552 # etc. It's also possible that no special first include exists.
553 for line in contents:
554 line_num += 1
555 if system_include_pattern.match(line):
556 # No special first include -> process the line again along with normal
557 # includes.
558 line_num -= 1
559 break
560 match = custom_include_pattern.match(line)
561 if match:
562 match_dict = match.groupdict()
563 header_basename = input_api.os_path.basename(
564 match_dict['FILE']).replace('.h', '')
565 if header_basename not in input_api.os_path.basename(f.LocalPath()):
566 # No special first include -> process the line again along with normal
567 # includes.
568 line_num -= 1
569 break
570
571 # Split into scopes: Each region between #if and #endif is its own scope.
572 scopes = []
573 current_scope = []
574 for line in contents[line_num:]:
575 line_num += 1
576 if uncheckable_includes_pattern.match(line):
577 return []
578 if if_pattern.match(line):
579 scopes.append(current_scope)
580 current_scope = []
581 elif ((system_include_pattern.match(line) or
582 custom_include_pattern.match(line)) and
583 not excluded_include_pattern.match(line)):
584 current_scope.append((line_num, line))
585 scopes.append(current_scope)
586
587 for scope in scopes:
588 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
589 changed_linenums))
590 return warnings
591
592
593def _CheckIncludeOrder(input_api, output_api):
594 """Checks that the #include order is correct.
595
596 1. The corresponding header for source files.
597 2. C system files in alphabetical order
598 3. C++ system files in alphabetical order
599 4. Project's .h files in alphabetical order
600
601 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
602 these rules separately.
603 """
604
605 warnings = []
606 for f in input_api.AffectedFiles():
607 if f.LocalPath().endswith(('.cc', '.h')):
608 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
609 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
610
611 results = []
612 if warnings:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100613 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000614 warnings))
615 return results
616
617
618def _CheckForVersionControlConflictsInFile(input_api, f):
619 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
620 errors = []
621 for line_num, line in f.ChangedContents():
622 if pattern.match(line):
623 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
624 return errors
625
626
627def _CheckForVersionControlConflicts(input_api, output_api):
628 """Usually this is not intentional and will cause a compile failure."""
629 errors = []
630 for f in input_api.AffectedFiles():
631 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
632
633 results = []
634 if errors:
635 results.append(output_api.PresubmitError(
636 'Version control conflict markers found, please resolve.', errors))
637 return results
638
639
640def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
641 def FilterFile(affected_file):
642 """Filter function for use with input_api.AffectedSourceFiles,
643 below. This filters out everything except non-test files from
644 top-level directories that generally speaking should not hard-code
645 service URLs (e.g. src/android_webview/, src/content/ and others).
646 """
647 return input_api.FilterSourceFile(
648 affected_file,
649 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
650 black_list=(_EXCLUDED_PATHS +
651 _TEST_CODE_EXCLUDED_PATHS +
652 input_api.DEFAULT_BLACK_LIST))
653
654 pattern = input_api.re.compile('"[^"]*google\.com[^"]*"')
655 problems = [] # items are (filename, line_number, line)
656 for f in input_api.AffectedSourceFiles(FilterFile):
657 for line_num, line in f.ChangedContents():
658 if pattern.search(line):
659 problems.append((f.LocalPath(), line_num, line))
660
661 if problems:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100662 return [output_api.PresubmitPromptOrNotify(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000663 'Most layers below src/chrome/ should not hardcode service URLs.\n'
664 'Are you sure this is correct? (Contact: joi@chromium.org)',
665 [' %s:%d: %s' % (
666 problem[0], problem[1], problem[2]) for problem in problems])]
667 else:
668 return []
669
670
671def _CheckNoAbbreviationInPngFileName(input_api, output_api):
672 """Makes sure there are no abbreviations in the name of PNG files.
673 """
674 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
675 errors = []
676 for f in input_api.AffectedFiles(include_deletes=False):
677 if pattern.match(f.LocalPath()):
678 errors.append(' %s' % f.LocalPath())
679
680 results = []
681 if errors:
682 results.append(output_api.PresubmitError(
683 'The name of PNG files should not have abbreviations. \n'
684 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
685 'Contact oshima@chromium.org if you have questions.', errors))
686 return results
687
688
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000689def _CommonChecks(input_api, output_api):
690 """Checks common to both upload and commit."""
691 results = []
692 results.extend(input_api.canned_checks.PanProjectChecks(
693 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
694 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
695 results.extend(
696 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
697 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
698 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
699 results.extend(_CheckNoNewWStrings(input_api, output_api))
700 results.extend(_CheckNoDEPSGIT(input_api, output_api))
701 results.extend(_CheckNoBannedFunctions(input_api, output_api))
702 results.extend(_CheckNoPragmaOnce(input_api, output_api))
703 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
704 results.extend(_CheckUnwantedDependencies(input_api, output_api))
705 results.extend(_CheckFilePermissions(input_api, output_api))
706 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000707 results.extend(_CheckIncludeOrder(input_api, output_api))
708 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
709 results.extend(_CheckPatchFiles(input_api, output_api))
710 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
711 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100712 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000713
714 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
715 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
716 input_api, output_api,
717 input_api.PresubmitLocalPath(),
718 whitelist=[r'^PRESUBMIT_test\.py$']))
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000719 return results
720
721
722def _CheckSubversionConfig(input_api, output_api):
723 """Verifies the subversion config file is correctly setup.
724
725 Checks that autoprops are enabled, returns an error otherwise.
726 """
727 join = input_api.os_path.join
728 if input_api.platform == 'win32':
729 appdata = input_api.environ.get('APPDATA', '')
730 if not appdata:
731 return [output_api.PresubmitError('%APPDATA% is not configured.')]
732 path = join(appdata, 'Subversion', 'config')
733 else:
734 home = input_api.environ.get('HOME', '')
735 if not home:
736 return [output_api.PresubmitError('$HOME is not configured.')]
737 path = join(home, '.subversion', 'config')
738
739 error_msg = (
740 'Please look at http://dev.chromium.org/developers/coding-style to\n'
741 'configure your subversion configuration file. This enables automatic\n'
742 'properties to simplify the project maintenance.\n'
743 'Pro-tip: just download and install\n'
744 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
745
746 try:
747 lines = open(path, 'r').read().splitlines()
748 # Make sure auto-props is enabled and check for 2 Chromium standard
749 # auto-prop.
750 if (not '*.cc = svn:eol-style=LF' in lines or
751 not '*.pdf = svn:mime-type=application/pdf' in lines or
752 not 'enable-auto-props = yes' in lines):
753 return [
754 output_api.PresubmitNotifyResult(
755 'It looks like you have not configured your subversion config '
756 'file or it is not up-to-date.\n' + error_msg)
757 ]
758 except (OSError, IOError):
759 return [
760 output_api.PresubmitNotifyResult(
761 'Can\'t find your subversion config file.\n' + error_msg)
762 ]
763 return []
764
765
766def _CheckAuthorizedAuthor(input_api, output_api):
767 """For non-googler/chromites committers, verify the author's email address is
768 in AUTHORS.
769 """
770 # TODO(maruel): Add it to input_api?
771 import fnmatch
772
773 author = input_api.change.author_email
774 if not author:
775 input_api.logging.info('No author, skipping AUTHOR check')
776 return []
777 authors_path = input_api.os_path.join(
778 input_api.PresubmitLocalPath(), 'AUTHORS')
779 valid_authors = (
780 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
781 for line in open(authors_path))
782 valid_authors = [item.group(1).lower() for item in valid_authors if item]
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000783 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000784 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000785 return [output_api.PresubmitPromptWarning(
786 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
787 '\n'
788 'http://www.chromium.org/developers/contributing-code and read the '
789 '"Legal" section\n'
790 'If you are a chromite, verify the contributor signed the CLA.') %
791 author)]
792 return []
793
794
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000795def _CheckPatchFiles(input_api, output_api):
796 problems = [f.LocalPath() for f in input_api.AffectedFiles()
797 if f.LocalPath().endswith(('.orig', '.rej'))]
798 if problems:
799 return [output_api.PresubmitError(
800 "Don't commit .rej and .orig files.", problems)]
801 else:
802 return []
803
804
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100805def _DidYouMeanOSMacro(bad_macro):
806 try:
807 return {'A': 'OS_ANDROID',
808 'B': 'OS_BSD',
809 'C': 'OS_CHROMEOS',
810 'F': 'OS_FREEBSD',
811 'L': 'OS_LINUX',
812 'M': 'OS_MACOSX',
813 'N': 'OS_NACL',
814 'O': 'OS_OPENBSD',
815 'P': 'OS_POSIX',
816 'S': 'OS_SOLARIS',
817 'W': 'OS_WIN'}[bad_macro[3].upper()]
818 except KeyError:
819 return ''
820
821
822def _CheckForInvalidOSMacrosInFile(input_api, f):
823 """Check for sensible looking, totally invalid OS macros."""
824 preprocessor_statement = input_api.re.compile(r'^\s*#')
825 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
826 results = []
827 for lnum, line in f.ChangedContents():
828 if preprocessor_statement.search(line):
829 for match in os_macro.finditer(line):
830 if not match.group(1) in _VALID_OS_MACROS:
831 good = _DidYouMeanOSMacro(match.group(1))
832 did_you_mean = ' (did you mean %s?)' % good if good else ''
833 results.append(' %s:%d %s%s' % (f.LocalPath(),
834 lnum,
835 match.group(1),
836 did_you_mean))
837 return results
838
839
840def _CheckForInvalidOSMacros(input_api, output_api):
841 """Check all affected files for invalid OS macros."""
842 bad_macros = []
843 for f in input_api.AffectedFiles():
844 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
845 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
846
847 if not bad_macros:
848 return []
849
850 return [output_api.PresubmitError(
851 'Possibly invalid OS macro[s] found. Please fix your code\n'
852 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
853
854
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000855def CheckChangeOnUpload(input_api, output_api):
856 results = []
857 results.extend(_CommonChecks(input_api, output_api))
858 return results
859
860
861def CheckChangeOnCommit(input_api, output_api):
862 results = []
863 results.extend(_CommonChecks(input_api, output_api))
864 # TODO(thestig) temporarily disabled, doesn't work in third_party/
865 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
866 # input_api, output_api, sources))
867 # Make sure the tree is 'open'.
868 results.extend(input_api.canned_checks.CheckTreeIsOpen(
869 input_api,
870 output_api,
871 json_url='http://chromium-status.appspot.com/current?format=json'))
872 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
873 output_api, 'http://codereview.chromium.org',
874 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
875 'tryserver@chromium.org'))
876
877 results.extend(input_api.canned_checks.CheckChangeHasBugField(
878 input_api, output_api))
879 results.extend(input_api.canned_checks.CheckChangeHasDescription(
880 input_api, output_api))
881 results.extend(_CheckSubversionConfig(input_api, output_api))
882 return results
883
884
885def GetPreferredTrySlaves(project, change):
886 files = change.LocalPaths()
887
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000888 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000889 return []
890
891 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000892 return ['mac_rel', 'mac_asan', 'mac:compile']
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000893 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000894 return ['win_rel', 'win7_aura', 'win:compile']
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000895 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
896 return ['android_dbg', 'android_clang_dbg']
897 if all(re.search('^native_client_sdk', f) for f in files):
898 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
899 if all(re.search('[/_]ios[/_.]', f) for f in files):
900 return ['ios_rel_device', 'ios_dbg_simulator']
901
902 trybots = [
903 'android_clang_dbg',
904 'android_dbg',
905 'ios_dbg_simulator',
906 'ios_rel_device',
907 'linux_asan',
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000908 'linux_aura',
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000909 'linux_chromeos',
910 'linux_clang:compile',
911 'linux_rel',
912 'mac_asan',
913 'mac_rel',
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000914 'mac:compile',
915 'win7_aura',
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000916 'win_rel',
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000917 'win:compile',
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000918 ]
919
920 # Match things like path/aura/file.cc and path/file_aura.cc.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000921 # Same for chromeos.
922 if any(re.search('[/_](aura|chromeos)', f) for f in files):
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000923 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
924
925 return trybots