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