Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: PRESUBMIT.py

Issue 1993243002: ⛲ Add presubmit to prevent SharedPreferences misuse. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add suppression for ContextUtils. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Top-level presubmit script for Chromium. 5 """Top-level presubmit script for Chromium.
6 6
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools. 8 for more details about the presubmit API built into depot_tools.
9 """ 9 """
10 10
11 11
12 _EXCLUDED_PATHS = ( 12 _EXCLUDED_PATHS = (
13 r"^breakpad[\\\/].*", 13 r"^breakpad[\\\/].*",
14 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py", 14 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
15 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py", 15 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
16 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk", 16 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
17 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*", 17 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
18 r"^skia[\\\/].*", 18 r"^skia[\\\/].*",
19 r"^third_party[\\\/]WebKit[\\\/].*", 19 r"^third_party[\\\/]WebKit[\\\/].*",
20 r"^v8[\\\/].*", 20 r"^v8[\\\/].*",
21 r".*MakeFile$", 21 r".*MakeFile$",
22 r".+_autogen\.h$", 22 r".+_autogen\.h$",
23 r".+[\\\/]pnacl_shim\.c$", 23 r".+[\\\/]pnacl_shim\.c$",
24 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$", 24 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
25 r"^chrome[\\\/]browser[\\\/]resources[\\\/]pdf[\\\/]index.js", 25 r"^chrome[\\\/]browser[\\\/]resources[\\\/]pdf[\\\/]index.js",
26 ) 26 )
27 27
28
28 # The NetscapePlugIn library is excluded from pan-project as it will soon 29 # The NetscapePlugIn library is excluded from pan-project as it will soon
29 # be deleted together with the rest of the NPAPI and it's not worthwhile to 30 # be deleted together with the rest of the NPAPI and it's not worthwhile to
30 # update the coding style until then. 31 # update the coding style until then.
31 _TESTRUNNER_PATHS = ( 32 _TESTRUNNER_PATHS = (
32 r"^content[\\\/]shell[\\\/]tools[\\\/]plugin[\\\/].*", 33 r"^content[\\\/]shell[\\\/]tools[\\\/]plugin[\\\/].*",
33 ) 34 )
34 35
36
35 # Fragment of a regular expression that matches C++ and Objective-C++ 37 # Fragment of a regular expression that matches C++ and Objective-C++
36 # implementation files. 38 # implementation files.
37 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' 39 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
38 40
41
39 # Regular expression that matches code only used for test binaries 42 # Regular expression that matches code only used for test binaries
40 # (best effort). 43 # (best effort).
41 _TEST_CODE_EXCLUDED_PATHS = ( 44 _TEST_CODE_EXCLUDED_PATHS = (
42 r'.*[\\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS, 45 r'.*[\\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS, 46 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
44 r'.+_(api|browser|kif|perf|pixel|unit|ui)?test(_[a-z]+)?%s' % 47 r'.+_(api|browser|kif|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
45 _IMPLEMENTATION_EXTENSIONS, 48 _IMPLEMENTATION_EXTENSIONS,
46 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS, 49 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
47 r'.*[\\\/](test|tool(s)?)[\\\/].*', 50 r'.*[\\\/](test|tool(s)?)[\\\/].*',
48 # content_shell is used for running layout tests. 51 # content_shell is used for running layout tests.
49 r'content[\\\/]shell[\\\/].*', 52 r'content[\\\/]shell[\\\/].*',
50 # At request of folks maintaining this folder. 53 # At request of folks maintaining this folder.
51 r'chrome[\\\/]browser[\\\/]automation[\\\/].*', 54 r'chrome[\\\/]browser[\\\/]automation[\\\/].*',
52 # Non-production example code. 55 # Non-production example code.
53 r'mojo[\\\/]examples[\\\/].*', 56 r'mojo[\\\/]examples[\\\/].*',
54 # Launcher for running iOS tests on the simulator. 57 # Launcher for running iOS tests on the simulator.
55 r'testing[\\\/]iossim[\\\/]iossim\.mm$', 58 r'testing[\\\/]iossim[\\\/]iossim\.mm$',
56 ) 59 )
57 60
61
58 _TEST_ONLY_WARNING = ( 62 _TEST_ONLY_WARNING = (
59 'You might be calling functions intended only for testing from\n' 63 'You might be calling functions intended only for testing from\n'
60 'production code. It is OK to ignore this warning if you know what\n' 64 'production code. It is OK to ignore this warning if you know what\n'
61 'you are doing, as the heuristics used to detect the situation are\n' 65 'you are doing, as the heuristics used to detect the situation are\n'
62 'not perfect. The commit queue will not block on this warning.') 66 'not perfect. The commit queue will not block on this warning.')
63 67
64 68
65 _INCLUDE_ORDER_WARNING = ( 69 _INCLUDE_ORDER_WARNING = (
66 'Your #include order seems to be broken. Remember to use the right ' 70 'Your #include order seems to be broken. Remember to use the right '
67 'collation (LC_COLLATE=C) and check\nhttps://google.github.io/styleguide/' 71 'collation (LC_COLLATE=C) and check\nhttps://google.github.io/styleguide/'
68 'cppguide.html#Names_and_Order_of_Includes') 72 'cppguide.html#Names_and_Order_of_Includes')
69 73
74
70 _BANNED_OBJC_FUNCTIONS = ( 75 _BANNED_OBJC_FUNCTIONS = (
71 ( 76 (
72 'addTrackingRect:', 77 'addTrackingRect:',
73 ( 78 (
74 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' 79 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
75 'prohibited. Please use CrTrackingArea instead.', 80 'prohibited. Please use CrTrackingArea instead.',
76 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', 81 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
77 ), 82 ),
78 False, 83 False,
79 ), 84 ),
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 ( 289 (
285 '#pragma comment(lib,', 290 '#pragma comment(lib,',
286 ( 291 (
287 'Specify libraries to link with in build files and not in the source.', 292 'Specify libraries to link with in build files and not in the source.',
288 ), 293 ),
289 True, 294 True,
290 (), 295 (),
291 ), 296 ),
292 ) 297 )
293 298
299
294 _IPC_ENUM_TRAITS_DEPRECATED = ( 300 _IPC_ENUM_TRAITS_DEPRECATED = (
295 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n' 301 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
296 'See http://www.chromium.org/Home/chromium-security/education/security-tips- for-ipc') 302 'See http://www.chromium.org/Home/chromium-security/education/security-tips- for-ipc')
297 303
298 304
299 _VALID_OS_MACROS = ( 305 _VALID_OS_MACROS = (
300 # Please keep sorted. 306 # Please keep sorted.
301 'OS_ANDROID', 307 'OS_ANDROID',
302 'OS_BSD', 308 'OS_BSD',
303 'OS_CAT', # For testing. 309 'OS_CAT', # For testing.
(...skipping 11 matching lines...) Expand all
315 'OS_SOLARIS', 321 'OS_SOLARIS',
316 'OS_WIN', 322 'OS_WIN',
317 ) 323 )
318 324
319 325
320 _ANDROID_SPECIFIC_PYDEPS_FILES = [ 326 _ANDROID_SPECIFIC_PYDEPS_FILES = [
321 'build/android/test_runner.pydeps', 327 'build/android/test_runner.pydeps',
322 'net/tools/testserver/testserver.pydeps', 328 'net/tools/testserver/testserver.pydeps',
323 ] 329 ]
324 330
331
325 _GENERIC_PYDEPS_FILES = [ 332 _GENERIC_PYDEPS_FILES = [
326 'build/secondary/tools/swarming_client/isolate.pydeps', 333 'build/secondary/tools/swarming_client/isolate.pydeps',
327 ] 334 ]
328 335
336
329 _ALL_PYDEPS_FILES = _ANDROID_SPECIFIC_PYDEPS_FILES + _GENERIC_PYDEPS_FILES 337 _ALL_PYDEPS_FILES = _ANDROID_SPECIFIC_PYDEPS_FILES + _GENERIC_PYDEPS_FILES
330 338
331 339
332 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): 340 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
333 """Attempts to prevent use of functions intended only for testing in 341 """Attempts to prevent use of functions intended only for testing in
334 non-testing code. For now this is just a best-effort implementation 342 non-testing code. For now this is just a best-effort implementation
335 that ignores header files and may have some false positives. A 343 that ignores header files and may have some false positives. A
336 better implementation would probably need a proper C++ parser. 344 better implementation would probably need a proper C++ parser.
337 """ 345 """
338 # We only scan .cc files and the like, as the declaration of 346 # We only scan .cc files and the like, as the declaration of
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 if not histogram_name_found: 492 if not histogram_name_found:
485 problems.append(' [%s:%d] %s' % 493 problems.append(' [%s:%d] %s' %
486 (f.LocalPath(), line_num, histogram_name)) 494 (f.LocalPath(), line_num, histogram_name))
487 495
488 if not problems: 496 if not problems:
489 return [] 497 return []
490 return [output_api.PresubmitPromptWarning('Some UMA_HISTOGRAM lines have ' 498 return [output_api.PresubmitPromptWarning('Some UMA_HISTOGRAM lines have '
491 'been modified and the associated histogram name has no match in either ' 499 'been modified and the associated histogram name has no match in either '
492 '%s or the modifications of it:' % (histograms_xml_path), problems)] 500 '%s or the modifications of it:' % (histograms_xml_path), problems)]
493 501
502
494 def _CheckFlakyTestUsage(input_api, output_api): 503 def _CheckFlakyTestUsage(input_api, output_api):
495 """Check that FlakyTest annotation is our own instead of the android one""" 504 """Check that FlakyTest annotation is our own instead of the android one"""
496 pattern = input_api.re.compile(r'import android.test.FlakyTest;') 505 pattern = input_api.re.compile(r'import android.test.FlakyTest;')
497 files = [] 506 files = []
498 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): 507 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
499 if f.LocalPath().endswith('Test.java'): 508 if f.LocalPath().endswith('Test.java'):
500 if pattern.search(input_api.ReadFile(f)): 509 if pattern.search(input_api.ReadFile(f)):
501 files.append(f) 510 files.append(f)
502 if len(files): 511 if len(files):
503 return [output_api.PresubmitError( 512 return [output_api.PresubmitError(
504 'Use org.chromium.base.test.util.FlakyTest instead of ' 513 'Use org.chromium.base.test.util.FlakyTest instead of '
505 'android.test.FlakyTest', 514 'android.test.FlakyTest',
506 files)] 515 files)]
507 return [] 516 return []
508 517
518
509 def _CheckNoNewWStrings(input_api, output_api): 519 def _CheckNoNewWStrings(input_api, output_api):
510 """Checks to make sure we don't introduce use of wstrings.""" 520 """Checks to make sure we don't introduce use of wstrings."""
511 problems = [] 521 problems = []
512 for f in input_api.AffectedFiles(): 522 for f in input_api.AffectedFiles():
513 if (not f.LocalPath().endswith(('.cc', '.h')) or 523 if (not f.LocalPath().endswith(('.cc', '.h')) or
514 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or 524 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or
515 '/win/' in f.LocalPath()): 525 '/win/' in f.LocalPath()):
516 continue 526 continue
517 527
518 allowWString = False 528 allowWString = False
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 return [output_api.PresubmitError( 569 return [output_api.PresubmitError(
560 'DEPS file must have only git dependencies.', 570 'DEPS file must have only git dependencies.',
561 long_text=error.output)] 571 long_text=error.output)]
562 572
563 573
564 def _CheckNoBannedFunctions(input_api, output_api): 574 def _CheckNoBannedFunctions(input_api, output_api):
565 """Make sure that banned functions are not used.""" 575 """Make sure that banned functions are not used."""
566 warnings = [] 576 warnings = []
567 errors = [] 577 errors = []
568 578
579 def IsBlacklisted(affected_file, blacklist):
580 local_path = affected_file.LocalPath()
581 for item in blacklist:
582 if input_api.re.match(item, local_path):
583 return True
584 return False
585
586 def CheckForMatch(affected_file, line_num, line, func_name, message, error):
587 matched = False
588 if func_name[0:1] == '/':
589 regex = func_name[1:]
590 if input_api.re.search(regex, line):
591 matched = True
592 elif func_name in line:
593 matched = True
594 if matched:
595 problems = warnings;
596 if error:
597 problems = errors;
598 problems.append(' %s:%d:' % (affected_file.LocalPath(), line_num))
599 for message_line in message:
600 problems.append(' %s' % message_line)
601
569 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) 602 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
570 for f in input_api.AffectedFiles(file_filter=file_filter): 603 for f in input_api.AffectedFiles(file_filter=file_filter):
571 for line_num, line in f.ChangedContents(): 604 for line_num, line in f.ChangedContents():
572 for func_name, message, error in _BANNED_OBJC_FUNCTIONS: 605 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
573 matched = False 606 CheckForMatch(f, line_num, line, func_name, message, error)
574 if func_name[0:1] == '/':
575 regex = func_name[1:]
576 if input_api.re.search(regex, line):
577 matched = True
578 elif func_name in line:
579 matched = True
580 if matched:
581 problems = warnings;
582 if error:
583 problems = errors;
584 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
585 for message_line in message:
586 problems.append(' %s' % message_line)
587 607
588 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) 608 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
589 for f in input_api.AffectedFiles(file_filter=file_filter): 609 for f in input_api.AffectedFiles(file_filter=file_filter):
590 for line_num, line in f.ChangedContents(): 610 for line_num, line in f.ChangedContents():
591 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS: 611 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
592 def IsBlacklisted(affected_file, blacklist):
593 local_path = affected_file.LocalPath()
594 for item in blacklist:
595 if input_api.re.match(item, local_path):
596 return True
597 return False
598 if IsBlacklisted(f, excluded_paths): 612 if IsBlacklisted(f, excluded_paths):
599 continue 613 continue
600 matched = False 614 CheckForMatch(f, line_num, line, func_name, message, error)
601 if func_name[0:1] == '/':
602 regex = func_name[1:]
603 if input_api.re.search(regex, line):
604 matched = True
605 elif func_name in line:
606 matched = True
607 if matched:
608 problems = warnings;
609 if error:
610 problems = errors;
611 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
612 for message_line in message:
613 problems.append(' %s' % message_line)
614 615
615 result = [] 616 result = []
616 if (warnings): 617 if (warnings):
617 result.append(output_api.PresubmitPromptWarning( 618 result.append(output_api.PresubmitPromptWarning(
618 'Banned functions were used.\n' + '\n'.join(warnings))) 619 'Banned functions were used.\n' + '\n'.join(warnings)))
619 if (errors): 620 if (errors):
620 result.append(output_api.PresubmitError( 621 result.append(output_api.PresubmitError(
621 'Banned functions were used.\n' + '\n'.join(errors))) 622 'Banned functions were used.\n' + '\n'.join(errors)))
622 return result 623 return result
623 624
(...skipping 1231 matching lines...) Expand 10 before | Expand all | Expand 10 after
1855 results.extend(_CheckUserActionUpdate(input_api, output_api)) 1856 results.extend(_CheckUserActionUpdate(input_api, output_api))
1856 results.extend(_CheckNoDeprecatedCSS(input_api, output_api)) 1857 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1857 results.extend(_CheckNoDeprecatedJS(input_api, output_api)) 1858 results.extend(_CheckNoDeprecatedJS(input_api, output_api))
1858 results.extend(_CheckParseErrors(input_api, output_api)) 1859 results.extend(_CheckParseErrors(input_api, output_api))
1859 results.extend(_CheckForIPCRules(input_api, output_api)) 1860 results.extend(_CheckForIPCRules(input_api, output_api))
1860 results.extend(_CheckForCopyrightedCode(input_api, output_api)) 1861 results.extend(_CheckForCopyrightedCode(input_api, output_api))
1861 results.extend(_CheckForWindowsLineEndings(input_api, output_api)) 1862 results.extend(_CheckForWindowsLineEndings(input_api, output_api))
1862 results.extend(_CheckSingletonInHeaders(input_api, output_api)) 1863 results.extend(_CheckSingletonInHeaders(input_api, output_api))
1863 results.extend(_CheckNoDeprecatedCompiledResourcesGYP(input_api, output_api)) 1864 results.extend(_CheckNoDeprecatedCompiledResourcesGYP(input_api, output_api))
1864 results.extend(_CheckPydepsNeedsUpdating(input_api, output_api)) 1865 results.extend(_CheckPydepsNeedsUpdating(input_api, output_api))
1866 results.extend(_CheckJavaStyle(input_api, output_api))
1865 1867
1866 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): 1868 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1867 results.extend(input_api.canned_checks.RunUnitTestsInDirectory( 1869 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1868 input_api, output_api, 1870 input_api, output_api,
1869 input_api.PresubmitLocalPath(), 1871 input_api.PresubmitLocalPath(),
1870 whitelist=[r'^PRESUBMIT_test\.py$'])) 1872 whitelist=[r'^PRESUBMIT_test\.py$']))
1871 return results 1873 return results
1872 1874
1873 1875
1874 def _CheckAuthorizedAuthor(input_api, output_api): 1876 def _CheckAuthorizedAuthor(input_api, output_api):
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
2078 'these files to contain Windows style line endings?\n' + 2080 'these files to contain Windows style line endings?\n' +
2079 '\n'.join(problems))] 2081 '\n'.join(problems))]
2080 2082
2081 return [] 2083 return []
2082 2084
2083 2085
2084 def CheckChangeOnUpload(input_api, output_api): 2086 def CheckChangeOnUpload(input_api, output_api):
2085 results = [] 2087 results = []
2086 results.extend(_CommonChecks(input_api, output_api)) 2088 results.extend(_CommonChecks(input_api, output_api))
2087 results.extend(_CheckValidHostsInDEPS(input_api, output_api)) 2089 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
2088 results.extend(_CheckJavaStyle(input_api, output_api))
2089 results.extend( 2090 results.extend(
2090 input_api.canned_checks.CheckGNFormatted(input_api, output_api)) 2091 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
2091 results.extend(_CheckUmaHistogramChanges(input_api, output_api)) 2092 results.extend(_CheckUmaHistogramChanges(input_api, output_api))
2092 results.extend(_AndroidSpecificOnUploadChecks(input_api, output_api)) 2093 results.extend(_AndroidSpecificOnUploadChecks(input_api, output_api))
2093 return results 2094 return results
2094 2095
2095 2096
2096 def GetTryServerMasterForBot(bot): 2097 def GetTryServerMasterForBot(bot):
2097 """Returns the Try Server master for the given bot. 2098 """Returns the Try Server master for the given bot.
2098 2099
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2135 results.extend(input_api.canned_checks.CheckTreeIsOpen( 2136 results.extend(input_api.canned_checks.CheckTreeIsOpen(
2136 input_api, 2137 input_api,
2137 output_api, 2138 output_api,
2138 json_url='http://chromium-status.appspot.com/current?format=json')) 2139 json_url='http://chromium-status.appspot.com/current?format=json'))
2139 2140
2140 results.extend(input_api.canned_checks.CheckChangeHasBugField( 2141 results.extend(input_api.canned_checks.CheckChangeHasBugField(
2141 input_api, output_api)) 2142 input_api, output_api))
2142 results.extend(input_api.canned_checks.CheckChangeHasDescription( 2143 results.extend(input_api.canned_checks.CheckChangeHasDescription(
2143 input_api, output_api)) 2144 input_api, output_api))
2144 return results 2145 return results
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698