[email protected] | 2c92353 | 2012-03-02 21:16:55 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 11 | def _CheckNoInterfacesInBase(input_api, output_api): |
| 12 | """Checks to make sure no files in libbase.a have |@interface|.""" |
| 13 | pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE) |
| 14 | files = [] |
| 15 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 16 | if (f.LocalPath().startswith('base/') and |
droger | 114a60b | 2014-11-06 11:06:51 | [diff] [blame] | 17 | not "/ios/" in f.LocalPath() and |
[email protected] | a35203a4 | 2012-07-12 15:12:55 | [diff] [blame] | 18 | not "/test/" in f.LocalPath() and |
bttk | ec17659 | 2020-01-23 17:04:17 | [diff] [blame] | 19 | not f.LocalPath().endswith('.java') and |
[email protected] | 43016609 | 2013-05-30 16:09:14 | [diff] [blame] | 20 | not f.LocalPath().endswith('_unittest.mm') and |
| 21 | not f.LocalPath().endswith('mac/sdk_forward_declarations.h')): |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 22 | contents = input_api.ReadFile(f) |
| 23 | if pattern.search(contents): |
| 24 | files.append(f) |
| 25 | |
| 26 | if len(files): |
| 27 | return [ output_api.PresubmitError( |
| 28 | 'Objective-C interfaces or categories are forbidden in libbase. ' + |
| 29 | 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' + |
| 30 | 'browse_thread/thread/efb28c10435987fd', |
| 31 | files) ] |
| 32 | return [] |
| 33 | |
| 34 | |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 35 | def _CheckNoTraceEventInclude(input_api, output_api): |
| 36 | """Verify that //base includes base_tracing.h instead of trace event headers. |
| 37 | |
| 38 | Checks that files outside trace event implementation include the |
| 39 | base_tracing.h header instead of specific trace event implementation headers |
| 40 | to maintain compatibility with the gn flag "enable_base_tracing = false". |
| 41 | """ |
| 42 | discouraged_includes = [ |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 43 | r'^#include "base/trace_event/(?!base_tracing\.h)', |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 44 | ] |
| 45 | |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 46 | files_to_check = [ |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 47 | r".*\.(h|cc|mm)$", |
| 48 | ] |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 49 | files_to_skip = [ |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 50 | r".*[\\/]test[\\/].*", |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 51 | r".*[\\/]trace_event[\\/].*", |
| 52 | r".*[\\/]tracing[\\/].*", |
| 53 | ] |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 54 | no_presubmit = r"// no-presubmit-check" |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 55 | |
| 56 | def FilterFile(affected_file): |
| 57 | return input_api.FilterSourceFile( |
| 58 | affected_file, |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 59 | files_to_check=files_to_check, |
| 60 | files_to_skip=files_to_skip) |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 61 | |
| 62 | locations = [] |
| 63 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 64 | for line_num, line in f.ChangedContents(): |
| 65 | for include in discouraged_includes: |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 66 | if (input_api.re.search(include, line) and |
| 67 | not input_api.re.search(no_presubmit, line)): |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 68 | locations.append(" %s:%d" % (f.LocalPath(), line_num)) |
| 69 | break |
| 70 | |
| 71 | if locations: |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 72 | return [ output_api.PresubmitError( |
| 73 | 'Base code should include "base/trace_event/base_tracing.h" instead\n' + |
| 74 | 'of trace_event implementation headers. If you need to include an\n' + |
| 75 | 'implementation header, verify that base_unittests still passes\n' + |
| 76 | 'with gn arg "enable_base_tracing = false" and add\n' + |
| 77 | '"// no-presubmit-check" after the include. \n' + |
| 78 | '\n'.join(locations)) ] |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 79 | return [] |
| 80 | |
| 81 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 82 | def _CommonChecks(input_api, output_api): |
| 83 | """Checks common to both upload and commit.""" |
| 84 | results = [] |
| 85 | results.extend(_CheckNoInterfacesInBase(input_api, output_api)) |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 86 | results.extend(_CheckNoTraceEventInclude(input_api, output_api)) |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 87 | return results |
| 88 | |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 89 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 90 | def CheckChangeOnUpload(input_api, output_api): |
| 91 | results = [] |
| 92 | results.extend(_CommonChecks(input_api, output_api)) |
| 93 | return results |
| 94 | |
| 95 | |
| 96 | def CheckChangeOnCommit(input_api, output_api): |
| 97 | results = [] |
| 98 | results.extend(_CommonChecks(input_api, output_api)) |
| 99 | return results |