blob: 1fec1b08ecd2c252760464f28fcb3b766cda65d5 [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)
Hector Dearman53d91b92019-12-13 17:07:52 +000047 results += CheckBadCpp(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
Hector Dearman53d91b92019-12-13 17:07:52 +0000112def CheckBadCpp(input_api, output_api):
113 tool = 'tools/check_include_violations'
114
115 bad_cpp = [
116 (r'\bNULL\b', 'New code should not use NULL prefer nullptr'),
117 (r'\bstd::stoi\b',
118 'std::stoi throws exceptions prefer base::StringToInt32()'),
119 (r'\bstd::stol\b',
120 'std::stoull throws exceptions prefer base::StringToInt32()'),
121 (r'\bstd::stoul\b',
122 'std::stoull throws exceptions prefer base::StringToUint32()'),
123 (r'\bstd::stoll\b',
124 'std::stoull throws exceptions prefer base::StringToInt64()'),
125 (r'\bstd::stoull\b',
126 'std::stoull throws exceptions prefer base::StringToUint64()'),
127 (r'\bstd::stof\b',
128 'std::stof throws exceptions prefer base::StringToDouble()'),
129 (r'\bstd::stod\b',
130 'std::stod throws exceptions prefer base::StringToDouble()'),
131 (r'\bstd::stold\b',
132 'std::stold throws exceptions prefer base::StringToDouble()'),
133 ]
134
135 def file_filter(x):
136 return input_api.FilterSourceFile(x, white_list=[r'.*\.h$', r'.*\.cc$'])
137
138 errors = []
139 for f in input_api.AffectedSourceFiles(file_filter):
140 for line_number, line in f.ChangedContents():
141 for regex, message in bad_cpp:
142 if input_api.re.search(regex, line):
143 errors.append(
144 output_api.PresubmitError('Banned C++:\n {}:{} {}'.format(
145 f.LocalPath(), line_number, message)))
146 return errors
147
148
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100149def CheckIncludeViolations(input_api, output_api):
150 tool = 'tools/check_include_violations'
151
Primiano Tucci834fdc72019-10-04 11:33:44 +0100152 def file_filter(x):
153 return input_api.FilterSourceFile(x, white_list=['include/.*[.]h$', tool])
154
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100155 if not input_api.AffectedSourceFiles(file_filter):
156 return []
157 if subprocess.call([tool]):
158 return [output_api.PresubmitError(tool + ' failed.')]
159 return []
Primiano Tucci40e98722018-02-16 11:50:17 +0000160
161
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000162def CheckBinaryDescriptors(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100163 tool = 'tools/gen_binary_descriptors'
164
Primiano Tucci834fdc72019-10-04 11:33:44 +0100165 def file_filter(x):
166 return input_api.FilterSourceFile(
167 x, white_list=['protos/perfetto/.*[.]proto$', '.*[.]h', tool])
168
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100169 if not input_api.AffectedSourceFiles(file_filter):
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000170 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100171 if subprocess.call([tool, '--check-only']):
172 return [
Primiano Tucci834fdc72019-10-04 11:33:44 +0100173 output_api.PresubmitError('Please run ' + tool +
174 ' to update binary descriptors.')
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100175 ]
176 return []
Hector Dearmanb7fa5442018-11-08 18:39:32 +0000177
178
Primiano Tuccic5010802018-01-19 17:13:21 +0000179def CheckMergedTraceConfigProto(input_api, output_api):
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100180 tool = 'tools/gen_merged_protos'
181
Primiano Tucci834fdc72019-10-04 11:33:44 +0100182 def build_file_filter(x):
183 return input_api.FilterSourceFile(
184 x, white_list=['protos/perfetto/.*[.]proto$', tool])
185
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100186 if not input_api.AffectedSourceFiles(build_file_filter):
Primiano Tuccic5010802018-01-19 17:13:21 +0000187 return []
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100188 if subprocess.call([tool, '--check-only']):
189 return [
190 output_api.PresubmitError(
191 'perfetto_config.proto or perfetto_trace.proto is out of ' +
192 'date. Please run ' + tool + ' to update it.')
193 ]
194 return []
Florian Mayer640b7ae2018-05-09 15:28:32 +0100195
196
197# Prevent removing or changing lines in event_whitelist.
198def CheckWhitelist(input_api, output_api):
199 for f in input_api.AffectedFiles():
200 if f.LocalPath() != 'tools/ftrace_proto_gen/event_whitelist':
201 continue
Primiano Tucci834fdc72019-10-04 11:33:44 +0100202 if any((not new_line.startswith('removed')) and new_line != old_line
203 for old_line, new_line in itertools.izip(f.OldContents(),
204 f.NewContents())):
Florian Mayer640b7ae2018-05-09 15:28:32 +0100205 return [
Primiano Tuccibf3b19c2019-06-01 08:40:26 +0100206 output_api.PresubmitError(
207 'event_whitelist only has two supported changes: '
Primiano Tucci834fdc72019-10-04 11:33:44 +0100208 'appending a new line, and replacing a line with removed.')
Florian Mayer640b7ae2018-05-09 15:28:32 +0100209 ]
210 return []