blob: 97acb3eb44505378a31f287966d64635883b7f71 [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,
Ryan Savitski135038e2020-11-12 20:57:57 +000025 files_to_check='.*',
26 files_to_skip=[
Primiano Tucci834fdc72019-10-04 11:33:44 +010027 'Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$',
Primiano Tuccif0ed1d42020-11-18 16:30:18 +010028 'test/trace_processor/.*/index$', '(.*/)?BUILD$', 'WORKSPACE',
Lalit Magantia951d3d2020-07-16 23:57:00 +010029 '.*/Makefile$', '/perfetto_build_flags.h$'
Primiano Tucci834fdc72019-10-04 11:33:44 +010030 ])
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)
Anindita Ghosh2aa4e422020-06-26 15:48:32 +010042 results += CheckProtoComments(input, output)
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010043 results += CheckBuild(input, output)
44 results += CheckAndroidBlueprint(input, output)
45 results += CheckBinaryDescriptors(input, output)
46 results += CheckMergedTraceConfigProto(input, output)
Primiano Tuccia3645202020-08-03 16:28:18 +020047 results += CheckProtoEventList(input, output)
Ryan704bc822020-03-31 02:31:13 +010048 results += CheckBannedCpp(input, output)
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000049 results += CheckSqlMetrics(input, output)
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010050 return results
Hector Dearman534765e2017-11-01 11:17:38 +000051
Sami Kyostilab27619f2017-12-13 19:22:16 +000052
Hector Dearman534765e2017-11-01 11:17:38 +000053def CheckChangeOnUpload(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010054 return CheckChange(input_api, output_api)
Hector Dearman534765e2017-11-01 11:17:38 +000055
Sami Kyostilab27619f2017-12-13 19:22:16 +000056
Hector Dearman534765e2017-11-01 11:17:38 +000057def CheckChangeOnCommit(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010058 return CheckChange(input_api, output_api)
Hector Dearman534765e2017-11-01 11:17:38 +000059
Sami Kyostilab27619f2017-12-13 19:22:16 +000060
Lalit Maganti279ecde2019-04-01 16:57:12 +010061def CheckBuild(input_api, output_api):
Primiano Tucci9c411652019-08-27 07:13:59 +020062 tool = 'tools/gen_bazel'
Primiano Tucci834fdc72019-10-04 11:33:44 +010063
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010064 # If no GN files were modified, bail out.
Primiano Tucci834fdc72019-10-04 11:33:44 +010065 def build_file_filter(x):
66 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +000067 x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool))
Primiano Tucci834fdc72019-10-04 11:33:44 +010068
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010069 if not input_api.AffectedSourceFiles(build_file_filter):
Lalit Maganti279ecde2019-04-01 16:57:12 +010070 return []
Primiano Tucci9c411652019-08-27 07:13:59 +020071 if subprocess.call([tool, '--check-only']):
Primiano Tucci834fdc72019-10-04 11:33:44 +010072 return [
73 output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' +
74 tool + ' to update them.')
75 ]
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010076 return []
77
Lalit Maganti279ecde2019-04-01 16:57:12 +010078
Sami Kyostilab27619f2017-12-13 19:22:16 +000079def CheckAndroidBlueprint(input_api, output_api):
Primiano Tucci9c411652019-08-27 07:13:59 +020080 tool = 'tools/gen_android_bp'
Primiano Tucci834fdc72019-10-04 11:33:44 +010081
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010082 # If no GN files were modified, bail out.
Primiano Tucci834fdc72019-10-04 11:33:44 +010083 def build_file_filter(x):
84 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +000085 x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', tool))
Primiano Tucci834fdc72019-10-04 11:33:44 +010086
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010087 if not input_api.AffectedSourceFiles(build_file_filter):
Sami Kyostilab27619f2017-12-13 19:22:16 +000088 return []
Primiano Tucci9c411652019-08-27 07:13:59 +020089 if subprocess.call([tool, '--check-only']):
Primiano Tucci834fdc72019-10-04 11:33:44 +010090 return [
91 output_api.PresubmitError('Android build files are out of date. ' +
92 'Run ' + tool + ' to update them.')
93 ]
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010094 return []
95
Primiano Tuccic5010802018-01-19 17:13:21 +000096
Primiano Tucci40e98722018-02-16 11:50:17 +000097def CheckIncludeGuards(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +010098 tool = 'tools/fix_include_guards'
99
Primiano Tucci834fdc72019-10-04 11:33:44 +0100100 def file_filter(x):
101 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000102 x, files_to_check=['.*[.]cc$', '.*[.]h$', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100103
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100104 if not input_api.AffectedSourceFiles(file_filter):
Primiano Tucci40e98722018-02-16 11:50:17 +0000105 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100106 if subprocess.call([tool, '--check-only']):
107 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100108 output_api.PresubmitError('Please run ' + tool +
109 ' to fix include guards.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100110 ]
111 return []
112
113
Ryan704bc822020-03-31 02:31:13 +0100114def CheckBannedCpp(input_api, output_api):
Hector Dearman53d91b92019-12-13 17:07:52 +0000115 bad_cpp = [
Hector Dearman53d91b92019-12-13 17:07:52 +0000116 (r'\bstd::stoi\b',
117 'std::stoi throws exceptions prefer base::StringToInt32()'),
118 (r'\bstd::stol\b',
119 'std::stoull throws exceptions prefer base::StringToInt32()'),
120 (r'\bstd::stoul\b',
121 'std::stoull throws exceptions prefer base::StringToUint32()'),
122 (r'\bstd::stoll\b',
123 'std::stoull throws exceptions prefer base::StringToInt64()'),
124 (r'\bstd::stoull\b',
125 'std::stoull throws exceptions prefer base::StringToUint64()'),
126 (r'\bstd::stof\b',
127 'std::stof throws exceptions prefer base::StringToDouble()'),
128 (r'\bstd::stod\b',
129 'std::stod throws exceptions prefer base::StringToDouble()'),
130 (r'\bstd::stold\b',
131 'std::stold throws exceptions prefer base::StringToDouble()'),
Ryan704bc822020-03-31 02:31:13 +0100132 (r'\bPERFETTO_EINTR\(close\(',
133 'close(2) must not be retried on EINTR on Linux and other OSes '
134 'that we run on, as the fd will be closed.'),
Hector Dearman53d91b92019-12-13 17:07:52 +0000135 ]
136
137 def file_filter(x):
Ryan Savitski135038e2020-11-12 20:57:57 +0000138 return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$'])
Hector Dearman53d91b92019-12-13 17:07:52 +0000139
140 errors = []
141 for f in input_api.AffectedSourceFiles(file_filter):
142 for line_number, line in f.ChangedContents():
143 for regex, message in bad_cpp:
144 if input_api.re.search(regex, line):
145 errors.append(
Ryan704bc822020-03-31 02:31:13 +0100146 output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format(
Hector Dearman53d91b92019-12-13 17:07:52 +0000147 f.LocalPath(), line_number, message)))
148 return errors
149
150
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100151def CheckIncludeViolations(input_api, output_api):
152 tool = 'tools/check_include_violations'
153
Primiano Tucci834fdc72019-10-04 11:33:44 +0100154 def file_filter(x):
Ryan Savitski135038e2020-11-12 20:57:57 +0000155 return input_api.FilterSourceFile(
156 x, files_to_check=['include/.*[.]h$', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100157
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100158 if not input_api.AffectedSourceFiles(file_filter):
159 return []
160 if subprocess.call([tool]):
161 return [output_api.PresubmitError(tool + ' failed.')]
162 return []
Primiano Tucci40e98722018-02-16 11:50:17 +0000163
164
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000165def CheckBinaryDescriptors(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100166 tool = 'tools/gen_binary_descriptors'
167
Primiano Tucci834fdc72019-10-04 11:33:44 +0100168 def file_filter(x):
169 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000170 x, files_to_check=['protos/perfetto/.*[.]proto$', '.*[.]h', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100171
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100172 if not input_api.AffectedSourceFiles(file_filter):
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000173 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100174 if subprocess.call([tool, '--check-only']):
175 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100176 output_api.PresubmitError('Please run ' + tool +
177 ' to update binary descriptors.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100178 ]
179 return []
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000180
181
Primiano Tuccic5010802018-01-19 17:13:21 +0000182def CheckMergedTraceConfigProto(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100183 tool = 'tools/gen_merged_protos'
184
Primiano Tucci834fdc72019-10-04 11:33:44 +0100185 def build_file_filter(x):
186 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000187 x, files_to_check=['protos/perfetto/.*[.]proto$', tool])
Primiano Tucci834fdc72019-10-04 11:33:44 +0100188
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100189 if not input_api.AffectedSourceFiles(build_file_filter):
Primiano Tuccic5010802018-01-19 17:13:21 +0000190 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100191 if subprocess.call([tool, '--check-only']):
192 return [
193 output_api.PresubmitError(
194 'perfetto_config.proto or perfetto_trace.proto is out of ' +
195 'date. Please run ' + tool + ' to update it.')
196 ]
197 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +0100198
199
Primiano Tuccia3645202020-08-03 16:28:18 +0200200# Prevent removing or changing lines in event_list.
201def CheckProtoEventList(input_api, output_api):
Florian Mayer640b7ae2018-05-09 15:28:32 +0100202 for f in input_api.AffectedFiles():
Primiano Tuccia3645202020-08-03 16:28:18 +0200203 if f.LocalPath() != 'tools/ftrace_proto_gen/event_list':
Florian Mayer640b7ae2018-05-09 15:28:32 +0100204 continue
Primiano Tucci834fdc72019-10-04 11:33:44 +0100205 if any((not new_line.startswith('removed')) and new_line != old_line
206 for old_line, new_line in itertools.izip(f.OldContents(),
207 f.NewContents())):
Florian Mayer640b7ae2018-05-09 15:28:32 +0100208 return [
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100209 output_api.PresubmitError(
Primiano Tuccia3645202020-08-03 16:28:18 +0200210 'event_list only has two supported changes: '
Primiano Tucci834fdc72019-10-04 11:33:44 +0100211 'appending a new line, and replacing a line with removed.')
Florian Mayer640b7ae2018-05-09 15:28:32 +0100212 ]
213 return []
Anindita Ghosh2aa4e422020-06-26 15:48:32 +0100214
215
216def CheckProtoComments(input_api, output_api):
217 tool = 'tools/check_proto_comments'
218
219 def file_filter(x):
220 return input_api.FilterSourceFile(
Ryan Savitski135038e2020-11-12 20:57:57 +0000221 x, files_to_check=['protos/perfetto/.*[.]proto$', tool])
Anindita Ghosh2aa4e422020-06-26 15:48:32 +0100222
223 if not input_api.AffectedSourceFiles(file_filter):
224 return []
225 if subprocess.call([tool]):
226 return [output_api.PresubmitError(tool + ' failed')]
227 return []
Lalit Magantie0e8bdb2021-01-11 19:43:55 +0000228
229
230def CheckSqlMetrics(input_api, output_api):
231 tool = 'tools/check_sql_metrics.py'
232
233 def file_filter(x):
234 return input_api.FilterSourceFile(
235 x, files_to_check=['src/trace_processor/metrics/.*[.]sql$', tool])
236
237 if not input_api.AffectedSourceFiles(file_filter):
238 return []
239 if subprocess.call([tool]):
240 return [output_api.PresubmitError(tool + ' failed')]
241 return []