Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | # Copyright 2012 The Chromium Authors |
[email protected] | 0732a49d9 | 2011-03-08 03:37:28 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Chromium presubmit script for src/base. |
| 6 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
tfarina | 78bb92f4 | 2015-01-31 00:20:48 | [diff] [blame] | 8 | for more details on the presubmit API built into depot_tools. |
[email protected] | 0732a49d9 | 2011-03-08 03:37:28 | [diff] [blame] | 9 | """ |
| 10 | |
Bruce Dawson | df604017 | 2022-05-24 05:33:37 | [diff] [blame] | 11 | def CheckChangeLintsClean(input_api, output_api): |
| 12 | """Makes sure that the code is cpplint clean.""" |
| 13 | # lint_filters=[] stops the OFF_BY_DEFAULT_LINT_FILTERS from being disabled, |
| 14 | # finding many more issues. verbose_level=1 finds a small number of additional |
| 15 | # issues. |
| 16 | # The only valid extensions for cpplint are .cc, .h, .cpp, .cu, and .ch. |
| 17 | # Only process those extensions which are used in Chromium, in directories |
| 18 | # that currently lint clean. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 19 | CLEAN_CPP_FILES_ONLY = (r'base/win/.*\.(cc|h)$', ) |
Bruce Dawson | df604017 | 2022-05-24 05:33:37 | [diff] [blame] | 20 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 21 | x, |
| 22 | files_to_check=CLEAN_CPP_FILES_ONLY, |
| 23 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP) |
| 24 | return input_api.canned_checks.CheckChangeLintsClean( |
| 25 | input_api, output_api, source_file_filter=source_file_filter, |
| 26 | lint_filters=[], verbose_level=1) |
| 27 | |
| 28 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 29 | def _CheckNoInterfacesInBase(input_api, output_api): |
| 30 | """Checks to make sure no files in libbase.a have |@interface|.""" |
| 31 | pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE) |
| 32 | files = [] |
| 33 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 34 | if (f.LocalPath().startswith('base/') and |
droger | 114a60b | 2014-11-06 11:06:51 | [diff] [blame] | 35 | not "/ios/" in f.LocalPath() and |
[email protected] | a35203a4 | 2012-07-12 15:12:55 | [diff] [blame] | 36 | not "/test/" in f.LocalPath() and |
bttk | ec17659 | 2020-01-23 17:04:17 | [diff] [blame] | 37 | not f.LocalPath().endswith('.java') and |
[email protected] | 43016609 | 2013-05-30 16:09:14 | [diff] [blame] | 38 | not f.LocalPath().endswith('_unittest.mm') and |
Marijn Kruisselbrink | 88fa5d5f | 2023-08-09 18:27:56 | [diff] [blame] | 39 | not f.LocalPath().endswith('_spi.h')): |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 40 | contents = input_api.ReadFile(f) |
| 41 | if pattern.search(contents): |
| 42 | files.append(f) |
| 43 | |
| 44 | if len(files): |
| 45 | return [ output_api.PresubmitError( |
| 46 | 'Objective-C interfaces or categories are forbidden in libbase. ' + |
| 47 | 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' + |
| 48 | 'browse_thread/thread/efb28c10435987fd', |
| 49 | files) ] |
| 50 | return [] |
| 51 | |
| 52 | |
Eric Seckler | 05f7e90 | 2021-02-25 14:47:36 | [diff] [blame] | 53 | def _FindLocations(input_api, search_regexes, files_to_check, files_to_skip): |
| 54 | """Returns locations matching one of the search_regexes.""" |
| 55 | def FilterFile(affected_file): |
| 56 | return input_api.FilterSourceFile( |
| 57 | affected_file, |
| 58 | files_to_check=files_to_check, |
| 59 | files_to_skip=files_to_skip) |
| 60 | |
| 61 | no_presubmit = r"// no-presubmit-check" |
| 62 | locations = [] |
| 63 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 64 | for line_num, line in f.ChangedContents(): |
| 65 | for search_regex in search_regexes: |
| 66 | if (input_api.re.search(search_regex, line) and |
| 67 | not input_api.re.search(no_presubmit, line)): |
| 68 | locations.append(" %s:%d" % (f.LocalPath(), line_num)) |
| 69 | break |
| 70 | return locations |
| 71 | |
| 72 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 73 | def _CommonChecks(input_api, output_api): |
| 74 | """Checks common to both upload and commit.""" |
| 75 | results = [] |
| 76 | results.extend(_CheckNoInterfacesInBase(input_api, output_api)) |
Bruce Dawson | df604017 | 2022-05-24 05:33:37 | [diff] [blame] | 77 | results.extend(CheckChangeLintsClean(input_api, output_api)) |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 78 | return results |
| 79 | |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 80 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 81 | def CheckChangeOnUpload(input_api, output_api): |
| 82 | results = [] |
| 83 | results.extend(_CommonChecks(input_api, output_api)) |
| 84 | return results |
| 85 | |
| 86 | |
| 87 | def CheckChangeOnCommit(input_api, output_api): |
| 88 | results = [] |
| 89 | results.extend(_CommonChecks(input_api, output_api)) |
| 90 | return results |