blob: 33ec547b2b67ffebb8f8266548b650544d27db87 [file] [log] [blame]
[email protected]2c923532012-03-02 21:16:551# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0732a49d92011-03-08 03:37:282# 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
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
tfarina78bb92f42015-01-31 00:20:488for more details on the presubmit API built into depot_tools.
[email protected]0732a49d92011-03-08 03:37:289"""
10
[email protected]23e6cbc2012-06-16 18:51:2011def _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
droger114a60b2014-11-06 11:06:5117 not "/ios/" in f.LocalPath() and
[email protected]a35203a42012-07-12 15:12:5518 not "/test/" in f.LocalPath() and
bttkec176592020-01-23 17:04:1719 not f.LocalPath().endswith('.java') and
[email protected]430166092013-05-30 16:09:1420 not f.LocalPath().endswith('_unittest.mm') and
21 not f.LocalPath().endswith('mac/sdk_forward_declarations.h')):
[email protected]23e6cbc2012-06-16 18:51:2022 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 Secklerf6c544f2020-06-02 10:49:2135def _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 Seckler7d7dd3c2020-06-26 09:24:1243 r'^#include "base/trace_event/(?!base_tracing\.h)',
Eric Secklerf6c544f2020-06-02 10:49:2144 ]
45
Josip Sokcevic8b6cc432020-08-05 17:45:3346 files_to_check = [
Eric Secklerf6c544f2020-06-02 10:49:2147 r".*\.(h|cc|mm)$",
48 ]
Josip Sokcevic8b6cc432020-08-05 17:45:3349 files_to_skip = [
Eric Seckler7d7dd3c2020-06-26 09:24:1250 r".*[\\/]test[\\/].*",
Eric Secklerf6c544f2020-06-02 10:49:2151 r".*[\\/]trace_event[\\/].*",
52 r".*[\\/]tracing[\\/].*",
53 ]
Eric Seckler7d7dd3c2020-06-26 09:24:1254 no_presubmit = r"// no-presubmit-check"
Eric Secklerf6c544f2020-06-02 10:49:2155
56 def FilterFile(affected_file):
57 return input_api.FilterSourceFile(
58 affected_file,
Josip Sokcevic8b6cc432020-08-05 17:45:3359 files_to_check=files_to_check,
60 files_to_skip=files_to_skip)
Eric Secklerf6c544f2020-06-02 10:49:2161
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 Seckler7d7dd3c2020-06-26 09:24:1266 if (input_api.re.search(include, line) and
67 not input_api.re.search(no_presubmit, line)):
Eric Secklerf6c544f2020-06-02 10:49:2168 locations.append(" %s:%d" % (f.LocalPath(), line_num))
69 break
70
71 if locations:
Eric Seckler7d7dd3c2020-06-26 09:24:1272 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 Secklerf6c544f2020-06-02 10:49:2179 return []
80
81
[email protected]23e6cbc2012-06-16 18:51:2082def _CommonChecks(input_api, output_api):
83 """Checks common to both upload and commit."""
84 results = []
85 results.extend(_CheckNoInterfacesInBase(input_api, output_api))
Eric Secklerf6c544f2020-06-02 10:49:2186 results.extend(_CheckNoTraceEventInclude(input_api, output_api))
[email protected]23e6cbc2012-06-16 18:51:2087 return results
88
Eric Secklerf6c544f2020-06-02 10:49:2189
[email protected]23e6cbc2012-06-16 18:51:2090def CheckChangeOnUpload(input_api, output_api):
91 results = []
92 results.extend(_CommonChecks(input_api, output_api))
93 return results
94
95
96def CheckChangeOnCommit(input_api, output_api):
97 results = []
98 results.extend(_CommonChecks(input_api, output_api))
99 return results