Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 1 | # 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 Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 15 | import itertools |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 16 | import subprocess |
| 17 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 18 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 19 | def CheckChange(input, output): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 20 | # There apparently is no way to wrap strings in blueprints, so ignore long |
| 21 | # lines in them. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 22 | def long_line_sources(x): |
| 23 | return input.FilterSourceFile( |
| 24 | x, |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 25 | files_to_check='.*', |
| 26 | files_to_skip=[ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 27 | 'Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$', |
Primiano Tucci | f0ed1d4 | 2020-11-18 16:30:18 +0100 | [diff] [blame] | 28 | 'test/trace_processor/.*/index$', '(.*/)?BUILD$', 'WORKSPACE', |
Lalit Maganti | a951d3d | 2020-07-16 23:57:00 +0100 | [diff] [blame] | 29 | '.*/Makefile$', '/perfetto_build_flags.h$' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 30 | ]) |
| 31 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 32 | 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 Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 42 | results += CheckProtoComments(input, output) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 43 | results += CheckBuild(input, output) |
| 44 | results += CheckAndroidBlueprint(input, output) |
| 45 | results += CheckBinaryDescriptors(input, output) |
| 46 | results += CheckMergedTraceConfigProto(input, output) |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 47 | results += CheckProtoEventList(input, output) |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 48 | results += CheckBannedCpp(input, output) |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 49 | results += CheckSqlMetrics(input, output) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 50 | return results |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 51 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 52 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 53 | def CheckChangeOnUpload(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 54 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 55 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 56 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 57 | def CheckChangeOnCommit(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 58 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 59 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 60 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 61 | def CheckBuild(input_api, output_api): |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 62 | tool = 'tools/gen_bazel' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 63 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 64 | # If no GN files were modified, bail out. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 65 | def build_file_filter(x): |
| 66 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 67 | x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool)) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 68 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 69 | if not input_api.AffectedSourceFiles(build_file_filter): |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 70 | return [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 71 | if subprocess.call([tool, '--check-only']): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 72 | return [ |
| 73 | output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' + |
| 74 | tool + ' to update them.') |
| 75 | ] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 76 | return [] |
| 77 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 78 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 79 | def CheckAndroidBlueprint(input_api, output_api): |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 80 | tool = 'tools/gen_android_bp' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 81 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 82 | # If no GN files were modified, bail out. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 83 | def build_file_filter(x): |
| 84 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 85 | x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', tool)) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 86 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 87 | if not input_api.AffectedSourceFiles(build_file_filter): |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 88 | return [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 89 | if subprocess.call([tool, '--check-only']): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 90 | return [ |
| 91 | output_api.PresubmitError('Android build files are out of date. ' + |
| 92 | 'Run ' + tool + ' to update them.') |
| 93 | ] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 94 | return [] |
| 95 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 96 | |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 97 | def CheckIncludeGuards(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 98 | tool = 'tools/fix_include_guards' |
| 99 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 100 | def file_filter(x): |
| 101 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 102 | x, files_to_check=['.*[.]cc$', '.*[.]h$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 103 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 104 | if not input_api.AffectedSourceFiles(file_filter): |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 105 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 106 | if subprocess.call([tool, '--check-only']): |
| 107 | return [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 108 | output_api.PresubmitError('Please run ' + tool + |
| 109 | ' to fix include guards.') |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 110 | ] |
| 111 | return [] |
| 112 | |
| 113 | |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 114 | def CheckBannedCpp(input_api, output_api): |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 115 | bad_cpp = [ |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 116 | (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()'), |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 132 | (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 Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 135 | ] |
| 136 | |
| 137 | def file_filter(x): |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 138 | return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$']) |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 139 | |
| 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( |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 146 | output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format( |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 147 | f.LocalPath(), line_number, message))) |
| 148 | return errors |
| 149 | |
| 150 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 151 | def CheckIncludeViolations(input_api, output_api): |
| 152 | tool = 'tools/check_include_violations' |
| 153 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 154 | def file_filter(x): |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 155 | return input_api.FilterSourceFile( |
| 156 | x, files_to_check=['include/.*[.]h$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 157 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 158 | if not input_api.AffectedSourceFiles(file_filter): |
| 159 | return [] |
| 160 | if subprocess.call([tool]): |
| 161 | return [output_api.PresubmitError(tool + ' failed.')] |
| 162 | return [] |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 163 | |
| 164 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 165 | def CheckBinaryDescriptors(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 166 | tool = 'tools/gen_binary_descriptors' |
| 167 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 168 | def file_filter(x): |
| 169 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 170 | x, files_to_check=['protos/perfetto/.*[.]proto$', '.*[.]h', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 171 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 172 | if not input_api.AffectedSourceFiles(file_filter): |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 173 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 174 | if subprocess.call([tool, '--check-only']): |
| 175 | return [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 176 | output_api.PresubmitError('Please run ' + tool + |
| 177 | ' to update binary descriptors.') |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 178 | ] |
| 179 | return [] |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 180 | |
| 181 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 182 | def CheckMergedTraceConfigProto(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 183 | tool = 'tools/gen_merged_protos' |
| 184 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 185 | def build_file_filter(x): |
| 186 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 187 | x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 188 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 189 | if not input_api.AffectedSourceFiles(build_file_filter): |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 190 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 191 | 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 Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 198 | |
| 199 | |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 200 | # Prevent removing or changing lines in event_list. |
| 201 | def CheckProtoEventList(input_api, output_api): |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 202 | for f in input_api.AffectedFiles(): |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 203 | if f.LocalPath() != 'tools/ftrace_proto_gen/event_list': |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 204 | continue |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 205 | 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 Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 208 | return [ |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 209 | output_api.PresubmitError( |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 210 | 'event_list only has two supported changes: ' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 211 | 'appending a new line, and replacing a line with removed.') |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 212 | ] |
| 213 | return [] |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 214 | |
| 215 | |
| 216 | def CheckProtoComments(input_api, output_api): |
| 217 | tool = 'tools/check_proto_comments' |
| 218 | |
| 219 | def file_filter(x): |
| 220 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 221 | x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 222 | |
| 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 Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def 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 [] |