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