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=[ |
Lalit Maganti | 3dc0ffe | 2021-06-09 13:27:52 +0100 | [diff] [blame] | 27 | 'Android[.]bp', |
| 28 | '.*[.]json$', |
| 29 | '.*[.]sql$', |
| 30 | '.*[.]out$', |
| 31 | 'test/trace_processor/.*/index$', |
| 32 | '(.*/)?BUILD$', |
| 33 | 'WORKSPACE', |
| 34 | '.*/Makefile$', |
| 35 | '/perfetto_build_flags.h$', |
| 36 | "infra/luci/.*", |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 37 | ]) |
| 38 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 39 | results = [] |
| 40 | results += input.canned_checks.CheckDoNotSubmit(input, output) |
| 41 | results += input.canned_checks.CheckChangeHasNoTabs(input, output) |
| 42 | results += input.canned_checks.CheckLongLines( |
| 43 | input, output, 80, source_file_filter=long_line_sources) |
| 44 | results += input.canned_checks.CheckPatchFormatted( |
| 45 | input, output, check_js=True) |
| 46 | results += input.canned_checks.CheckGNFormatted(input, output) |
| 47 | results += CheckIncludeGuards(input, output) |
| 48 | results += CheckIncludeViolations(input, output) |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 49 | results += CheckProtoComments(input, output) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 50 | results += CheckBuild(input, output) |
| 51 | results += CheckAndroidBlueprint(input, output) |
| 52 | results += CheckBinaryDescriptors(input, output) |
| 53 | results += CheckMergedTraceConfigProto(input, output) |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 54 | results += CheckProtoEventList(input, output) |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 55 | results += CheckBannedCpp(input, output) |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 56 | results += CheckSqlMetrics(input, output) |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 57 | return results |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 58 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 59 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 60 | def CheckChangeOnUpload(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 61 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 62 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 63 | |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 64 | def CheckChangeOnCommit(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 65 | return CheckChange(input_api, output_api) |
Hector Dearman | 534765e | 2017-11-01 11:17:38 +0000 | [diff] [blame] | 66 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 67 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 68 | def CheckBuild(input_api, output_api): |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 69 | tool = 'tools/gen_bazel' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 70 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 71 | # If no GN files were modified, bail out. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 72 | def build_file_filter(x): |
| 73 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 74 | x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', 'BUILD\.extras', tool)) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 75 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 76 | if not input_api.AffectedSourceFiles(build_file_filter): |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 77 | return [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 78 | if subprocess.call([tool, '--check-only']): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 79 | return [ |
| 80 | output_api.PresubmitError('Bazel BUILD(s) are out of date. Run ' + |
| 81 | tool + ' to update them.') |
| 82 | ] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 83 | return [] |
| 84 | |
Lalit Maganti | 279ecde | 2019-04-01 16:57:12 +0100 | [diff] [blame] | 85 | |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 86 | def CheckAndroidBlueprint(input_api, output_api): |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 87 | tool = 'tools/gen_android_bp' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 88 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 89 | # If no GN files were modified, bail out. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 90 | def build_file_filter(x): |
| 91 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 92 | x, files_to_check=('.*BUILD[.]gn$', '.*[.]gni$', tool)) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 93 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 94 | if not input_api.AffectedSourceFiles(build_file_filter): |
Sami Kyostila | b27619f | 2017-12-13 19:22:16 +0000 | [diff] [blame] | 95 | return [] |
Primiano Tucci | 9c41165 | 2019-08-27 07:13:59 +0200 | [diff] [blame] | 96 | if subprocess.call([tool, '--check-only']): |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 97 | return [ |
| 98 | output_api.PresubmitError('Android build files are out of date. ' + |
| 99 | 'Run ' + tool + ' to update them.') |
| 100 | ] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 101 | return [] |
| 102 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 103 | |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 104 | def CheckIncludeGuards(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 105 | tool = 'tools/fix_include_guards' |
| 106 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 107 | def file_filter(x): |
| 108 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 109 | x, files_to_check=['.*[.]cc$', '.*[.]h$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 110 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 111 | if not input_api.AffectedSourceFiles(file_filter): |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 112 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 113 | if subprocess.call([tool, '--check-only']): |
| 114 | return [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 115 | output_api.PresubmitError('Please run ' + tool + |
| 116 | ' to fix include guards.') |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 117 | ] |
| 118 | return [] |
| 119 | |
| 120 | |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 121 | def CheckBannedCpp(input_api, output_api): |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 122 | bad_cpp = [ |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 123 | (r'\bstd::stoi\b', |
| 124 | 'std::stoi throws exceptions prefer base::StringToInt32()'), |
| 125 | (r'\bstd::stol\b', |
| 126 | 'std::stoull throws exceptions prefer base::StringToInt32()'), |
| 127 | (r'\bstd::stoul\b', |
| 128 | 'std::stoull throws exceptions prefer base::StringToUint32()'), |
| 129 | (r'\bstd::stoll\b', |
| 130 | 'std::stoull throws exceptions prefer base::StringToInt64()'), |
| 131 | (r'\bstd::stoull\b', |
| 132 | 'std::stoull throws exceptions prefer base::StringToUint64()'), |
| 133 | (r'\bstd::stof\b', |
| 134 | 'std::stof throws exceptions prefer base::StringToDouble()'), |
| 135 | (r'\bstd::stod\b', |
| 136 | 'std::stod throws exceptions prefer base::StringToDouble()'), |
| 137 | (r'\bstd::stold\b', |
| 138 | 'std::stold throws exceptions prefer base::StringToDouble()'), |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 139 | (r'\bPERFETTO_EINTR\(close\(', |
| 140 | 'close(2) must not be retried on EINTR on Linux and other OSes ' |
| 141 | 'that we run on, as the fd will be closed.'), |
Primiano Tucci | 58d2dc6 | 2021-06-24 16:03:24 +0100 | [diff] [blame^] | 142 | (r'^#include <inttypes.h>', 'Use <cinttypes> rather than <inttypes.h>. ' + |
| 143 | 'See https://github.com/google/perfetto/issues/146'), |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 144 | ] |
| 145 | |
| 146 | def file_filter(x): |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 147 | return input_api.FilterSourceFile(x, files_to_check=[r'.*\.h$', r'.*\.cc$']) |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 148 | |
| 149 | errors = [] |
| 150 | for f in input_api.AffectedSourceFiles(file_filter): |
| 151 | for line_number, line in f.ChangedContents(): |
| 152 | for regex, message in bad_cpp: |
| 153 | if input_api.re.search(regex, line): |
| 154 | errors.append( |
Ryan | 704bc82 | 2020-03-31 02:31:13 +0100 | [diff] [blame] | 155 | output_api.PresubmitError('Banned pattern:\n {}:{} {}'.format( |
Hector Dearman | 53d91b9 | 2019-12-13 17:07:52 +0000 | [diff] [blame] | 156 | f.LocalPath(), line_number, message))) |
| 157 | return errors |
| 158 | |
| 159 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 160 | def CheckIncludeViolations(input_api, output_api): |
| 161 | tool = 'tools/check_include_violations' |
| 162 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 163 | def file_filter(x): |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 164 | return input_api.FilterSourceFile( |
| 165 | x, files_to_check=['include/.*[.]h$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 166 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 167 | if not input_api.AffectedSourceFiles(file_filter): |
| 168 | return [] |
| 169 | if subprocess.call([tool]): |
| 170 | return [output_api.PresubmitError(tool + ' failed.')] |
| 171 | return [] |
Primiano Tucci | 40e9872 | 2018-02-16 11:50:17 +0000 | [diff] [blame] | 172 | |
| 173 | |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 174 | def CheckBinaryDescriptors(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 175 | tool = 'tools/gen_binary_descriptors' |
| 176 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 177 | def file_filter(x): |
| 178 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 179 | x, files_to_check=['protos/perfetto/.*[.]proto$', '.*[.]h', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 180 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 181 | if not input_api.AffectedSourceFiles(file_filter): |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 182 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 183 | if subprocess.call([tool, '--check-only']): |
| 184 | return [ |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 185 | output_api.PresubmitError('Please run ' + tool + |
| 186 | ' to update binary descriptors.') |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 187 | ] |
| 188 | return [] |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 189 | |
| 190 | |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 191 | def CheckMergedTraceConfigProto(input_api, output_api): |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 192 | tool = 'tools/gen_merged_protos' |
| 193 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 194 | def build_file_filter(x): |
| 195 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 196 | x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 197 | |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 198 | if not input_api.AffectedSourceFiles(build_file_filter): |
Primiano Tucci | c501080 | 2018-01-19 17:13:21 +0000 | [diff] [blame] | 199 | return [] |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 200 | if subprocess.call([tool, '--check-only']): |
| 201 | return [ |
| 202 | output_api.PresubmitError( |
| 203 | 'perfetto_config.proto or perfetto_trace.proto is out of ' + |
| 204 | 'date. Please run ' + tool + ' to update it.') |
| 205 | ] |
| 206 | return [] |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 207 | |
| 208 | |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 209 | # Prevent removing or changing lines in event_list. |
| 210 | def CheckProtoEventList(input_api, output_api): |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 211 | for f in input_api.AffectedFiles(): |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 212 | if f.LocalPath() != 'tools/ftrace_proto_gen/event_list': |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 213 | continue |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 214 | if any((not new_line.startswith('removed')) and new_line != old_line |
| 215 | for old_line, new_line in itertools.izip(f.OldContents(), |
| 216 | f.NewContents())): |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 217 | return [ |
Primiano Tucci | bf3b19c | 2019-06-01 08:40:26 +0100 | [diff] [blame] | 218 | output_api.PresubmitError( |
Primiano Tucci | a364520 | 2020-08-03 16:28:18 +0200 | [diff] [blame] | 219 | 'event_list only has two supported changes: ' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 220 | 'appending a new line, and replacing a line with removed.') |
Florian Mayer | 640b7ae | 2018-05-09 15:28:32 +0100 | [diff] [blame] | 221 | ] |
| 222 | return [] |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 223 | |
| 224 | |
| 225 | def CheckProtoComments(input_api, output_api): |
| 226 | tool = 'tools/check_proto_comments' |
| 227 | |
| 228 | def file_filter(x): |
| 229 | return input_api.FilterSourceFile( |
Ryan Savitski | 135038e | 2020-11-12 20:57:57 +0000 | [diff] [blame] | 230 | x, files_to_check=['protos/perfetto/.*[.]proto$', tool]) |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 231 | |
| 232 | if not input_api.AffectedSourceFiles(file_filter): |
| 233 | return [] |
| 234 | if subprocess.call([tool]): |
| 235 | return [output_api.PresubmitError(tool + ' failed')] |
| 236 | return [] |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 237 | |
| 238 | |
| 239 | def CheckSqlMetrics(input_api, output_api): |
| 240 | tool = 'tools/check_sql_metrics.py' |
| 241 | |
| 242 | def file_filter(x): |
| 243 | return input_api.FilterSourceFile( |
| 244 | x, files_to_check=['src/trace_processor/metrics/.*[.]sql$', tool]) |
| 245 | |
| 246 | if not input_api.AffectedSourceFiles(file_filter): |
| 247 | return [] |
| 248 | if subprocess.call([tool]): |
| 249 | return [output_api.PresubmitError(tool + ' failed')] |
| 250 | return [] |