blob: b9fc9e16a955c5d15fbc71a153b27cb1783f65ec [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 Seckler05f7e902021-02-25 14:47:3635def _FindLocations(input_api, search_regexes, files_to_check, files_to_skip):
36 """Returns locations matching one of the search_regexes."""
37 def FilterFile(affected_file):
38 return input_api.FilterSourceFile(
39 affected_file,
40 files_to_check=files_to_check,
41 files_to_skip=files_to_skip)
42
43 no_presubmit = r"// no-presubmit-check"
44 locations = []
45 for f in input_api.AffectedSourceFiles(FilterFile):
46 for line_num, line in f.ChangedContents():
47 for search_regex in search_regexes:
48 if (input_api.re.search(search_regex, line) and
49 not input_api.re.search(no_presubmit, line)):
50 locations.append(" %s:%d" % (f.LocalPath(), line_num))
51 break
52 return locations
53
54
Eric Secklerf6c544f2020-06-02 10:49:2155def _CheckNoTraceEventInclude(input_api, output_api):
56 """Verify that //base includes base_tracing.h instead of trace event headers.
57
58 Checks that files outside trace event implementation include the
59 base_tracing.h header instead of specific trace event implementation headers
60 to maintain compatibility with the gn flag "enable_base_tracing = false".
61 """
62 discouraged_includes = [
Eric Seckler7d7dd3c2020-06-26 09:24:1263 r'^#include "base/trace_event/(?!base_tracing\.h)',
Eric Seckler05f7e902021-02-25 14:47:3664 r'^#include "third_party/perfetto/include/',
Eric Secklerf6c544f2020-06-02 10:49:2165 ]
66
Josip Sokcevic8b6cc432020-08-05 17:45:3367 files_to_check = [
Eric Secklerf6c544f2020-06-02 10:49:2168 r".*\.(h|cc|mm)$",
69 ]
Josip Sokcevic8b6cc432020-08-05 17:45:3370 files_to_skip = [
Eric Seckler7d7dd3c2020-06-26 09:24:1271 r".*[\\/]test[\\/].*",
Eric Secklerf6c544f2020-06-02 10:49:2172 r".*[\\/]trace_event[\\/].*",
73 r".*[\\/]tracing[\\/].*",
74 ]
Eric Secklerf6c544f2020-06-02 10:49:2175
Eric Seckler05f7e902021-02-25 14:47:3676 locations = _FindLocations(input_api, discouraged_includes, files_to_check,
77 files_to_skip)
Eric Secklerf6c544f2020-06-02 10:49:2178 if locations:
Eric Seckler7d7dd3c2020-06-26 09:24:1279 return [ output_api.PresubmitError(
80 'Base code should include "base/trace_event/base_tracing.h" instead\n' +
81 'of trace_event implementation headers. If you need to include an\n' +
82 'implementation header, verify that base_unittests still passes\n' +
83 'with gn arg "enable_base_tracing = false" and add\n' +
84 '"// no-presubmit-check" after the include. \n' +
85 '\n'.join(locations)) ]
Eric Secklerf6c544f2020-06-02 10:49:2186 return []
87
88
Eric Seckler05f7e902021-02-25 14:47:3689def _WarnPbzeroIncludes(input_api, output_api):
90 """Warn to check enable_base_tracing=false when including a pbzero header.
91
92 Emits a warning when including a perfetto pbzero header, encouraging the
93 user to verify that //base still builds with enable_base_tracing=false.
94 """
95 warn_includes = [
96 r'^#include "third_party/perfetto/protos/',
97 r'^#include "base/tracing/protos/',
98 ]
99
100 files_to_check = [
101 r".*\.(h|cc|mm)$",
102 ]
103 files_to_skip = [
104 r".*[\\/]test[\\/].*",
105 r".*[\\/]trace_event[\\/].*",
106 r".*[\\/]tracing[\\/].*",
107 ]
108
109 locations = _FindLocations(input_api, warn_includes, files_to_check,
110 files_to_skip)
111 if locations:
112 return [ output_api.PresubmitPromptWarning(
113 'Please verify that base_unittests still builds & passes with gn\n' +
114 'arg "enable_base_tracing = false" when adding typed trace events\n' +
115 'to //base. You can use "#if BUILDFLAG(ENABLE_BASE_TRACING)" to\n' +
116 'exclude pbzero headers and anything not supported by\n' +
117 '//base/trace_event/trace_event_stub.h.\n' +
118 '\n'.join(locations)) ]
119 return []
120
121
[email protected]23e6cbc2012-06-16 18:51:20122def _CommonChecks(input_api, output_api):
123 """Checks common to both upload and commit."""
124 results = []
125 results.extend(_CheckNoInterfacesInBase(input_api, output_api))
Eric Secklerf6c544f2020-06-02 10:49:21126 results.extend(_CheckNoTraceEventInclude(input_api, output_api))
Eric Seckler05f7e902021-02-25 14:47:36127 results.extend(_WarnPbzeroIncludes(input_api, output_api))
[email protected]23e6cbc2012-06-16 18:51:20128 return results
129
Eric Secklerf6c544f2020-06-02 10:49:21130
[email protected]23e6cbc2012-06-16 18:51:20131def CheckChangeOnUpload(input_api, output_api):
132 results = []
133 results.extend(_CommonChecks(input_api, output_api))
134 return results
135
136
137def CheckChangeOnCommit(input_api, output_api):
138 results = []
139 results.extend(_CommonChecks(input_api, output_api))
140 return results