blob: 30eb1c1f979079e4019734bbe119e75b0f419795 [file] [log] [blame]
Hector Dearman534765e2017-11-01 11:17:38 +00001# Copyright (C) 2017 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Florian Mayer640b7ae2018-05-09 15:28:32 +010015import itertools
Sami Kyostilab27619f2017-12-13 19:22:16 +000016import subprocess
17
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010018
Hector Dearman534765e2017-11-01 11:17:38 +000019def CheckChange(input, output):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010020 # There apparently is no way to wrap strings in blueprints, so ignore long
21 # lines in them.
Primiano Tucci834fdc72019-10-04 11:33:44 +010022 def long_line_sources(x):
23 return input.FilterSourceFile(
24 x,
25 white_list=".*",
26 black_list=[
27 'Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$',
28 'test/trace_processor/index$', '.*\bBUILD$', 'WORKSPACE',
29 '.*/Makefile$', '/perfetto_build_flags.h$'
30 ])
31
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010032 results = []
33 results += input.canned_checks.CheckDoNotSubmit(input, output)
34 results += input.canned_checks.CheckChangeHasNoTabs(input, output)
35 results += input.canned_checks.CheckLongLines(
36 input, output, 80, source_file_filter=long_line_sources)
37 results += input.canned_checks.CheckPatchFormatted(
38 input, output, check_js=True)
39 results += input.canned_checks.CheckGNFormatted(input, output)
40 results += CheckIncludeGuards(input, output)
41 results += CheckIncludeViolations(input, output)
42 results += CheckBuild(input, output)
43 results += CheckAndroidBlueprint(input, output)
44 results += CheckBinaryDescriptors(input, output)
45 results += CheckMergedTraceConfigProto(input, output)
46 results += CheckWhitelist(input, output)
Ryan704bc822020-03-31 02:31:13 +010047 results += CheckBannedCpp(input, output)
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010048 return results
Hector Dearman534765e2017-11-01 11:17:38 +000049
Sami Kyostilab27619f2017-12-13 19:22:16 +000050
Hector Dearman534765e2017-11-01 11:17:38 +000051def CheckChangeOnUpload(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010052 return CheckChange(input_api, output_api)
Hector Dearman534765e2017-11-01 11:17:38 +000053
Sami Kyostilab27619f2017-12-13 19:22:16 +000054
Hector Dearman534765e2017-11-01 11:17:38 +000055def CheckChangeOnCommit(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010056 return CheckChange(input_api, output_api)
Hector Dearman534765e2017-11-01 11:17:38 +000057
Sami Kyostilab27619f2017-12-13 19:22:16 +000058
Lalit Maganti279ecde2019-04-01 16:57:12 +010059def CheckBuild(input_api, output_api):
Primiano Tucci9c411652019-08-27 07:13:59 +020060 tool = 'tools/gen_bazel'
Primiano Tucci834fdc72019-10-04 11:33:44 +010061
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010062 # If no GN files were modified, bail out.
Primiano Tucci834fdc72019-10-04 11:33:44 +010063 def build_file_filter(x):
64 return input_api.FilterSourceFile(
65 x, white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool))
66
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010067 if not input_api.AffectedSourceFiles(build_file_filter):
Lalit Maganti279ecde2019-04-01 16:57:12 +010068 return []
Primiano Tucci9c411652019-08-27 07:13:59 +020069 if subprocess.call([tool, '--check-only']):
Primiano Tucci834fdc72019-10-04 11:33:44 +010070 return [
71 output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' +
72 tool + ' to update them.')
73 ]
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010074 return []
75
Lalit Maganti279ecde2019-04-01 16:57:12 +010076
Sami Kyostilab27619f2017-12-13 19:22:16 +000077def CheckAndroidBlueprint(input_api, output_api):
Primiano Tucci9c411652019-08-27 07:13:59 +020078 tool = 'tools/gen_android_bp'
Primiano Tucci834fdc72019-10-04 11:33:44 +010079
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010080 # If no GN files were modified, bail out.
Primiano Tucci834fdc72019-10-04 11:33:44 +010081 def build_file_filter(x):
82 return input_api.FilterSourceFile(
83 x, white_list=('.*BUILD[.]gn$', '.*[.]gni$', tool))
84
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010085 if not input_api.AffectedSourceFiles(build_file_filter):
Sami Kyostilab27619f2017-12-13 19:22:16 +000086 return []
Primiano Tucci9c411652019-08-27 07:13:59 +020087 if subprocess.call([tool, '--check-only']):
Primiano Tucci834fdc72019-10-04 11:33:44 +010088 return [
89 output_api.PresubmitError('Android build files are out of date. ' +
90 'Run ' + tool + ' to update them.')
91 ]
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010092 return []
93
Primiano Tuccic5010802018-01-19 17:13:21 +000094
Primiano Tucci40e98722018-02-16 11:50:17 +000095def CheckIncludeGuards(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010096 tool = 'tools/fix_include_guards'
97
Primiano Tucci834fdc72019-10-04 11:33:44 +010098 def file_filter(x):
99 return input_api.FilterSourceFile(
100 x, white_list=['.*[.]cc$', '.*[.]h$', tool])
101
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100102 if not input_api.AffectedSourceFiles(file_filter):
Primiano Tucci40e98722018-02-16 11:50:17 +0000103 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100104 if subprocess.call([tool, '--check-only']):
105 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100106 output_api.PresubmitError('Please run ' + tool +
107 ' to fix include guards.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100108 ]
109 return []
110
111
Ryan704bc822020-03-31 02:31:13 +0100112def CheckBannedCpp(input_api, output_api):
Hector Dearman53d91b92019-12-13 17:07:52 +0000113 bad_cpp = [
114 (r'\bNULL\b', 'New code should not use NULL prefer nullptr'),
115 (r'\bstd::stoi\b',
116 'std::stoi throws exceptions prefer base::StringToInt32()'),
117 (r'\bstd::stol\b',
118 'std::stoull throws exceptions prefer base::StringToInt32()'),
119 (r'\bstd::stoul\b',
120 'std::stoull throws exceptions prefer base::StringToUint32()'),
121 (r'\bstd::stoll\b',
122 'std::stoull throws exceptions prefer base::StringToInt64()'),
123 (r'\bstd::stoull\b',
124 'std::stoull throws exceptions prefer base::StringToUint64()'),
125 (r'\bstd::stof\b',
126 'std::stof throws exceptions prefer base::StringToDouble()'),
127 (r'\bstd::stod\b',
128 'std::stod throws exceptions prefer base::StringToDouble()'),
129 (r'\bstd::stold\b',
130 'std::stold throws exceptions prefer base::StringToDouble()'),
Ryan704bc822020-03-31 02:31:13 +0100131 (r'\bPERFETTO_EINTR\(close\(',
132 'close(2) must not be retried on EINTR on Linux and other OSes '
133 'that we run on, as the fd will be closed.'),
Hector Dearman53d91b92019-12-13 17:07:52 +0000134 ]
135
136 def file_filter(x):
137 return input_api.FilterSourceFile(x, white_list=[r'.*\.h$', r'.*\.cc$'])
138
139 errors = []
140 for f in input_api.AffectedSourceFiles(file_filter):
141 for line_number, line in f.ChangedContents():
142 for regex, message in bad_cpp:
143 if input_api.re.search(regex, line):
144 errors.append(
Ryan704bc822020-03-31 02:31:13 +0100145 output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format(
Hector Dearman53d91b92019-12-13 17:07:52 +0000146 f.LocalPath(), line_number, message)))
147 return errors
148
149
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100150def CheckIncludeViolations(input_api, output_api):
151 tool = 'tools/check_include_violations'
152
Primiano Tucci834fdc72019-10-04 11:33:44 +0100153 def file_filter(x):
154 return input_api.FilterSourceFile(x, white_list=['include/.*[.]h$', tool])
155
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100156 if not input_api.AffectedSourceFiles(file_filter):
157 return []
158 if subprocess.call([tool]):
159 return [output_api.PresubmitError(tool + ' failed.')]
160 return []
Primiano Tucci40e98722018-02-16 11:50:17 +0000161
162
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000163def CheckBinaryDescriptors(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100164 tool = 'tools/gen_binary_descriptors'
165
Primiano Tucci834fdc72019-10-04 11:33:44 +0100166 def file_filter(x):
167 return input_api.FilterSourceFile(
168 x, white_list=['protos/perfetto/.*[.]proto$', '.*[.]h', tool])
169
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100170 if not input_api.AffectedSourceFiles(file_filter):
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000171 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100172 if subprocess.call([tool, '--check-only']):
173 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100174 output_api.PresubmitError('Please run ' + tool +
175 ' to update binary descriptors.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100176 ]
177 return []
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000178
179
Primiano Tuccic5010802018-01-19 17:13:21 +0000180def CheckMergedTraceConfigProto(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100181 tool = 'tools/gen_merged_protos'
182
Primiano Tucci834fdc72019-10-04 11:33:44 +0100183 def build_file_filter(x):
184 return input_api.FilterSourceFile(
185 x, white_list=['protos/perfetto/.*[.]proto$', tool])
186
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100187 if not input_api.AffectedSourceFiles(build_file_filter):
Primiano Tuccic5010802018-01-19 17:13:21 +0000188 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100189 if subprocess.call([tool, '--check-only']):
190 return [
191 output_api.PresubmitError(
192 'perfetto_config.proto or perfetto_trace.proto is out of ' +
193 'date. Please run ' + tool + ' to update it.')
194 ]
195 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +0100196
197
198# Prevent removing or changing lines in event_whitelist.
199def CheckWhitelist(input_api, output_api):
200 for f in input_api.AffectedFiles():
201 if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist':
202 continue
Primiano Tucci834fdc72019-10-04 11:33:44 +0100203 if any((not new_line.startswith('removed')) and new_line != old_line
204 for old_line, new_line in itertools.izip(f.OldContents(),
205 f.NewContents())):
Florian Mayer640b7ae2018-05-09 15:28:32 +0100206 return [
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100207 output_api.PresubmitError(
208 'event_whitelist only has two supported changes: '
Primiano Tucci834fdc72019-10-04 11:33:44 +0100209 'appending a new line, and replacing a line with removed.')
Florian Mayer640b7ae2018-05-09 15:28:32 +0100210 ]
211 return []